11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * Authors: 31da177e4SLinus Torvalds * Copyright 2001, 2002 by Robert Olsson <robert.olsson@its.uu.se> 41da177e4SLinus Torvalds * Uppsala University and 51da177e4SLinus Torvalds * Swedish University of Agricultural Sciences 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru> 81da177e4SLinus Torvalds * Ben Greear <greearb@candelatech.com> 91da177e4SLinus Torvalds * Jens L��s <jens.laas@data.slu.se> 101da177e4SLinus Torvalds * 111da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or 121da177e4SLinus Torvalds * modify it under the terms of the GNU General Public License 131da177e4SLinus Torvalds * as published by the Free Software Foundation; either version 141da177e4SLinus Torvalds * 2 of the License, or (at your option) any later version. 151da177e4SLinus Torvalds * 161da177e4SLinus Torvalds * 171da177e4SLinus Torvalds * A tool for loading the network with preconfigurated packets. 181da177e4SLinus Torvalds * The tool is implemented as a linux module. Parameters are output 191da177e4SLinus Torvalds * device, delay (to hard_xmit), number of packets, and whether 201da177e4SLinus Torvalds * to use multiple SKBs or just the same one. 211da177e4SLinus Torvalds * pktgen uses the installed interface's output routine. 221da177e4SLinus Torvalds * 231da177e4SLinus Torvalds * Additional hacking by: 241da177e4SLinus Torvalds * 251da177e4SLinus Torvalds * Jens.Laas@data.slu.se 261da177e4SLinus Torvalds * Improved by ANK. 010120. 271da177e4SLinus Torvalds * Improved by ANK even more. 010212. 281da177e4SLinus Torvalds * MAC address typo fixed. 010417 --ro 291da177e4SLinus Torvalds * Integrated. 020301 --DaveM 301da177e4SLinus Torvalds * Added multiskb option 020301 --DaveM 311da177e4SLinus Torvalds * Scaling of results. 020417--sigurdur@linpro.no 321da177e4SLinus Torvalds * Significant re-work of the module: 331da177e4SLinus Torvalds * * Convert to threaded model to more efficiently be able to transmit 341da177e4SLinus Torvalds * and receive on multiple interfaces at once. 351da177e4SLinus Torvalds * * Converted many counters to __u64 to allow longer runs. 361da177e4SLinus Torvalds * * Allow configuration of ranges, like min/max IP address, MACs, 371da177e4SLinus Torvalds * and UDP-ports, for both source and destination, and can 381da177e4SLinus Torvalds * set to use a random distribution or sequentially walk the range. 391da177e4SLinus Torvalds * * Can now change most values after starting. 401da177e4SLinus Torvalds * * Place 12-byte packet in UDP payload with magic number, 411da177e4SLinus Torvalds * sequence number, and timestamp. 421da177e4SLinus Torvalds * * Add receiver code that detects dropped pkts, re-ordered pkts, and 431da177e4SLinus Torvalds * latencies (with micro-second) precision. 441da177e4SLinus Torvalds * * Add IOCTL interface to easily get counters & configuration. 451da177e4SLinus Torvalds * --Ben Greear <greearb@candelatech.com> 461da177e4SLinus Torvalds * 471da177e4SLinus Torvalds * Renamed multiskb to clone_skb and cleaned up sending core for two distinct 481da177e4SLinus Torvalds * skb modes. A clone_skb=0 mode for Ben "ranges" work and a clone_skb != 0 491da177e4SLinus Torvalds * as a "fastpath" with a configurable number of clones after alloc's. 501da177e4SLinus Torvalds * clone_skb=0 means all packets are allocated this also means ranges time 511da177e4SLinus Torvalds * stamps etc can be used. clone_skb=100 means 1 malloc is followed by 100 521da177e4SLinus Torvalds * clones. 531da177e4SLinus Torvalds * 541da177e4SLinus Torvalds * Also moved to /proc/net/pktgen/ 551da177e4SLinus Torvalds * --ro 561da177e4SLinus Torvalds * 571da177e4SLinus Torvalds * Sept 10: Fixed threading/locking. Lots of bone-headed and more clever 581da177e4SLinus Torvalds * mistakes. Also merged in DaveM's patch in the -pre6 patch. 591da177e4SLinus Torvalds * --Ben Greear <greearb@candelatech.com> 601da177e4SLinus Torvalds * 611da177e4SLinus Torvalds * Integrated to 2.5.x 021029 --Lucio Maciel (luciomaciel@zipmail.com.br) 621da177e4SLinus Torvalds * 631da177e4SLinus Torvalds * 641da177e4SLinus Torvalds * 021124 Finished major redesign and rewrite for new functionality. 651da177e4SLinus Torvalds * See Documentation/networking/pktgen.txt for how to use this. 661da177e4SLinus Torvalds * 671da177e4SLinus Torvalds * The new operation: 681da177e4SLinus Torvalds * For each CPU one thread/process is created at start. This process checks 691da177e4SLinus Torvalds * for running devices in the if_list and sends packets until count is 0 it 701da177e4SLinus Torvalds * also the thread checks the thread->control which is used for inter-process 711da177e4SLinus Torvalds * communication. controlling process "posts" operations to the threads this 721da177e4SLinus Torvalds * way. The if_lock should be possible to remove when add/rem_device is merged 731da177e4SLinus Torvalds * into this too. 741da177e4SLinus Torvalds * 751da177e4SLinus Torvalds * By design there should only be *one* "controlling" process. In practice 761da177e4SLinus Torvalds * multiple write accesses gives unpredictable result. Understood by "write" 771da177e4SLinus Torvalds * to /proc gives result code thats should be read be the "writer". 78b4099fabSStephen Hemminger * For practical use this should be no problem. 791da177e4SLinus Torvalds * 801da177e4SLinus Torvalds * Note when adding devices to a specific CPU there good idea to also assign 811da177e4SLinus Torvalds * /proc/irq/XX/smp_affinity so TX-interrupts gets bound to the same CPU. 821da177e4SLinus Torvalds * --ro 831da177e4SLinus Torvalds * 841da177e4SLinus Torvalds * Fix refcount off by one if first packet fails, potential null deref, 851da177e4SLinus Torvalds * memleak 030710- KJP 861da177e4SLinus Torvalds * 871da177e4SLinus Torvalds * First "ranges" functionality for ipv6 030726 --ro 881da177e4SLinus Torvalds * 891da177e4SLinus Torvalds * Included flow support. 030802 ANK. 901da177e4SLinus Torvalds * 911da177e4SLinus Torvalds * Fixed unaligned access on IA-64 Grant Grundler <grundler@parisc-linux.org> 921da177e4SLinus Torvalds * 931da177e4SLinus Torvalds * Remove if fix from added Harald Welte <laforge@netfilter.org> 040419 941da177e4SLinus Torvalds * ia64 compilation fix from Aron Griffis <aron@hp.com> 040604 951da177e4SLinus Torvalds * 961da177e4SLinus Torvalds * New xmit() return, do_div and misc clean up by Stephen Hemminger 971da177e4SLinus Torvalds * <shemminger@osdl.org> 040923 981da177e4SLinus Torvalds * 99b4099fabSStephen Hemminger * Randy Dunlap fixed u64 printk compiler waring 1001da177e4SLinus Torvalds * 1011da177e4SLinus Torvalds * Remove FCS from BW calculation. Lennert Buytenhek <buytenh@wantstofly.org> 1021da177e4SLinus Torvalds * New time handling. Lennert Buytenhek <buytenh@wantstofly.org> 041213 1031da177e4SLinus Torvalds * 1041da177e4SLinus Torvalds * Corrections from Nikolai Malykh (nmalykh@bilim.com) 1051da177e4SLinus Torvalds * Removed unused flags F_SET_SRCMAC & F_SET_SRCIP 041230 1061da177e4SLinus Torvalds * 1071da177e4SLinus Torvalds * interruptible_sleep_on_timeout() replaced Nishanth Aravamudan <nacc@us.ibm.com> 1081da177e4SLinus Torvalds * 050103 109ca6549afSSteven Whitehouse * 110ca6549afSSteven Whitehouse * MPLS support by Steven Whitehouse <steve@chygwyn.com> 111ca6549afSSteven Whitehouse * 11234954ddcSFrancesco Fondelli * 802.1Q/Q-in-Q support by Francesco Fondelli (FF) <francesco.fondelli@gmail.com> 11334954ddcSFrancesco Fondelli * 1141da177e4SLinus Torvalds */ 1151da177e4SLinus Torvalds #include <linux/sys.h> 1161da177e4SLinus Torvalds #include <linux/types.h> 1171da177e4SLinus Torvalds #include <linux/module.h> 1181da177e4SLinus Torvalds #include <linux/moduleparam.h> 1191da177e4SLinus Torvalds #include <linux/kernel.h> 120222fa076SLuiz Capitulino #include <linux/mutex.h> 1211da177e4SLinus Torvalds #include <linux/sched.h> 1221da177e4SLinus Torvalds #include <linux/slab.h> 1231da177e4SLinus Torvalds #include <linux/vmalloc.h> 1241da177e4SLinus Torvalds #include <linux/unistd.h> 1251da177e4SLinus Torvalds #include <linux/string.h> 1261da177e4SLinus Torvalds #include <linux/ptrace.h> 1271da177e4SLinus Torvalds #include <linux/errno.h> 1281da177e4SLinus Torvalds #include <linux/ioport.h> 1291da177e4SLinus Torvalds #include <linux/interrupt.h> 1304fc268d2SRandy Dunlap #include <linux/capability.h> 13109fe3ef4SAndrew Morton #include <linux/freezer.h> 1321da177e4SLinus Torvalds #include <linux/delay.h> 1331da177e4SLinus Torvalds #include <linux/timer.h> 134cdcdbe0bSLuiz Capitulino #include <linux/list.h> 1351da177e4SLinus Torvalds #include <linux/init.h> 1361da177e4SLinus Torvalds #include <linux/skbuff.h> 1371da177e4SLinus Torvalds #include <linux/netdevice.h> 1381da177e4SLinus Torvalds #include <linux/inet.h> 1391da177e4SLinus Torvalds #include <linux/inetdevice.h> 1401da177e4SLinus Torvalds #include <linux/rtnetlink.h> 1411da177e4SLinus Torvalds #include <linux/if_arp.h> 14234954ddcSFrancesco Fondelli #include <linux/if_vlan.h> 1431da177e4SLinus Torvalds #include <linux/in.h> 1441da177e4SLinus Torvalds #include <linux/ip.h> 1451da177e4SLinus Torvalds #include <linux/ipv6.h> 1461da177e4SLinus Torvalds #include <linux/udp.h> 1471da177e4SLinus Torvalds #include <linux/proc_fs.h> 148d50a6b56SStephen Hemminger #include <linux/seq_file.h> 1491da177e4SLinus Torvalds #include <linux/wait.h> 150f404e9a6SKris Katterjohn #include <linux/etherdevice.h> 151ee74baa7SDavid S. Miller #include <linux/kthread.h> 1521da177e4SLinus Torvalds #include <net/checksum.h> 1531da177e4SLinus Torvalds #include <net/ipv6.h> 1541da177e4SLinus Torvalds #include <net/addrconf.h> 1551da177e4SLinus Torvalds #include <asm/byteorder.h> 1561da177e4SLinus Torvalds #include <linux/rcupdate.h> 1571da177e4SLinus Torvalds #include <asm/bitops.h> 1581da177e4SLinus Torvalds #include <asm/io.h> 1591da177e4SLinus Torvalds #include <asm/dma.h> 1601da177e4SLinus Torvalds #include <asm/uaccess.h> 1611da177e4SLinus Torvalds #include <asm/div64.h> /* do_div */ 1621da177e4SLinus Torvalds #include <asm/timex.h> 1631da177e4SLinus Torvalds 1641ca7768cSFrancesco Fondelli #define VERSION "pktgen v2.68: Packet Generator for packet performance testing.\n" 1651da177e4SLinus Torvalds 1661da177e4SLinus Torvalds /* The buckets are exponential in 'width' */ 1671da177e4SLinus Torvalds #define LAT_BUCKETS_MAX 32 1681da177e4SLinus Torvalds #define IP_NAME_SZ 32 169ca6549afSSteven Whitehouse #define MAX_MPLS_LABELS 16 /* This is the max label stack depth */ 170d5f1ce9aSStephen Hemminger #define MPLS_STACK_BOTTOM htonl(0x00000100) 1711da177e4SLinus Torvalds 1721da177e4SLinus Torvalds /* Device flag bits */ 1731da177e4SLinus Torvalds #define F_IPSRC_RND (1<<0) /* IP-Src Random */ 1741da177e4SLinus Torvalds #define F_IPDST_RND (1<<1) /* IP-Dst Random */ 1751da177e4SLinus Torvalds #define F_UDPSRC_RND (1<<2) /* UDP-Src Random */ 1761da177e4SLinus Torvalds #define F_UDPDST_RND (1<<3) /* UDP-Dst Random */ 1771da177e4SLinus Torvalds #define F_MACSRC_RND (1<<4) /* MAC-Src Random */ 1781da177e4SLinus Torvalds #define F_MACDST_RND (1<<5) /* MAC-Dst Random */ 1791da177e4SLinus Torvalds #define F_TXSIZE_RND (1<<6) /* Transmit size is random */ 1801da177e4SLinus Torvalds #define F_IPV6 (1<<7) /* Interface in IPV6 Mode */ 181ca6549afSSteven Whitehouse #define F_MPLS_RND (1<<8) /* Random MPLS labels */ 18234954ddcSFrancesco Fondelli #define F_VID_RND (1<<9) /* Random VLAN ID */ 18334954ddcSFrancesco Fondelli #define F_SVID_RND (1<<10) /* Random SVLAN ID */ 1841da177e4SLinus Torvalds 1851da177e4SLinus Torvalds /* Thread control flag bits */ 1861da177e4SLinus Torvalds #define T_TERMINATE (1<<0) 1871da177e4SLinus Torvalds #define T_STOP (1<<1) /* Stop run */ 1881da177e4SLinus Torvalds #define T_RUN (1<<2) /* Start run */ 18995ed63f7SArthur Kepner #define T_REMDEVALL (1<<3) /* Remove all devs */ 19095ed63f7SArthur Kepner #define T_REMDEV (1<<4) /* Remove one dev */ 1911da177e4SLinus Torvalds 1921da177e4SLinus Torvalds /* If lock -- can be removed after some work */ 1931da177e4SLinus Torvalds #define if_lock(t) spin_lock(&(t->if_lock)); 1941da177e4SLinus Torvalds #define if_unlock(t) spin_unlock(&(t->if_lock)); 1951da177e4SLinus Torvalds 1961da177e4SLinus Torvalds /* Used to help with determining the pkts on receive */ 1971da177e4SLinus Torvalds #define PKTGEN_MAGIC 0xbe9be955 198d50a6b56SStephen Hemminger #define PG_PROC_DIR "pktgen" 199d50a6b56SStephen Hemminger #define PGCTRL "pgctrl" 200d50a6b56SStephen Hemminger static struct proc_dir_entry *pg_proc_dir = NULL; 2011da177e4SLinus Torvalds 2021da177e4SLinus Torvalds #define MAX_CFLOWS 65536 2031da177e4SLinus Torvalds 20434954ddcSFrancesco Fondelli #define VLAN_TAG_SIZE(x) ((x)->vlan_id == 0xffff ? 0 : 4) 20534954ddcSFrancesco Fondelli #define SVLAN_TAG_SIZE(x) ((x)->svlan_id == 0xffff ? 0 : 4) 20634954ddcSFrancesco Fondelli 207222f1806SLuiz Capitulino struct flow_state { 208252e3346SAl Viro __be32 cur_daddr; 2091da177e4SLinus Torvalds int count; 2101da177e4SLinus Torvalds }; 2111da177e4SLinus Torvalds 2121da177e4SLinus Torvalds struct pktgen_dev { 2131da177e4SLinus Torvalds /* 2141da177e4SLinus Torvalds * Try to keep frequent/infrequent used vars. separated. 2151da177e4SLinus Torvalds */ 21639df232fSStephen Hemminger struct proc_dir_entry *entry; /* proc file */ 2171da177e4SLinus Torvalds struct pktgen_thread *pg_thread;/* the owner */ 218c26a8016SLuiz Capitulino struct list_head list; /* Used for chaining in the thread's run-queue */ 2191da177e4SLinus Torvalds 2201da177e4SLinus Torvalds int running; /* if this changes to false, the test will stop */ 2211da177e4SLinus Torvalds 2221da177e4SLinus Torvalds /* If min != max, then we will either do a linear iteration, or 2231da177e4SLinus Torvalds * we will do a random selection from within the range. 2241da177e4SLinus Torvalds */ 2251da177e4SLinus Torvalds __u32 flags; 22695ed63f7SArthur Kepner int removal_mark; /* non-zero => the device is marked for 22795ed63f7SArthur Kepner * removal by worker thread */ 2281da177e4SLinus Torvalds 2291da177e4SLinus Torvalds int min_pkt_size; /* = ETH_ZLEN; */ 2301da177e4SLinus Torvalds int max_pkt_size; /* = ETH_ZLEN; */ 2311da177e4SLinus Torvalds int nfrags; 2321da177e4SLinus Torvalds __u32 delay_us; /* Default delay */ 2331da177e4SLinus Torvalds __u32 delay_ns; 2341da177e4SLinus Torvalds __u64 count; /* Default No packets to send */ 2351da177e4SLinus Torvalds __u64 sofar; /* How many pkts we've sent so far */ 2361da177e4SLinus Torvalds __u64 tx_bytes; /* How many bytes we've transmitted */ 2371da177e4SLinus Torvalds __u64 errors; /* Errors when trying to transmit, pkts will be re-sent */ 2381da177e4SLinus Torvalds 2391da177e4SLinus Torvalds /* runtime counters relating to clone_skb */ 2401da177e4SLinus Torvalds __u64 next_tx_us; /* timestamp of when to tx next */ 2411da177e4SLinus Torvalds __u32 next_tx_ns; 2421da177e4SLinus Torvalds 2431da177e4SLinus Torvalds __u64 allocated_skbs; 2441da177e4SLinus Torvalds __u32 clone_count; 2451da177e4SLinus Torvalds int last_ok; /* Was last skb sent? 2461da177e4SLinus Torvalds * Or a failed transmit of some sort? This will keep 2471da177e4SLinus Torvalds * sequence numbers in order, for example. 2481da177e4SLinus Torvalds */ 2491da177e4SLinus Torvalds __u64 started_at; /* micro-seconds */ 2501da177e4SLinus Torvalds __u64 stopped_at; /* micro-seconds */ 2511da177e4SLinus Torvalds __u64 idle_acc; /* micro-seconds */ 2521da177e4SLinus Torvalds __u32 seq_num; 2531da177e4SLinus Torvalds 2541da177e4SLinus Torvalds int clone_skb; /* Use multiple SKBs during packet gen. If this number 255b4099fabSStephen Hemminger * is greater than 1, then that many copies of the same 2561da177e4SLinus Torvalds * packet will be sent before a new packet is allocated. 2571da177e4SLinus Torvalds * For instance, if you want to send 1024 identical packets 2581da177e4SLinus Torvalds * before creating a new packet, set clone_skb to 1024. 2591da177e4SLinus Torvalds */ 2601da177e4SLinus Torvalds 2611da177e4SLinus Torvalds char dst_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */ 2621da177e4SLinus Torvalds char dst_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */ 2631da177e4SLinus Torvalds char src_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */ 2641da177e4SLinus Torvalds char src_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */ 2651da177e4SLinus Torvalds 2661da177e4SLinus Torvalds struct in6_addr in6_saddr; 2671da177e4SLinus Torvalds struct in6_addr in6_daddr; 2681da177e4SLinus Torvalds struct in6_addr cur_in6_daddr; 2691da177e4SLinus Torvalds struct in6_addr cur_in6_saddr; 2701da177e4SLinus Torvalds /* For ranges */ 2711da177e4SLinus Torvalds struct in6_addr min_in6_daddr; 2721da177e4SLinus Torvalds struct in6_addr max_in6_daddr; 2731da177e4SLinus Torvalds struct in6_addr min_in6_saddr; 2741da177e4SLinus Torvalds struct in6_addr max_in6_saddr; 2751da177e4SLinus Torvalds 2761da177e4SLinus Torvalds /* If we're doing ranges, random or incremental, then this 2771da177e4SLinus Torvalds * defines the min/max for those ranges. 2781da177e4SLinus Torvalds */ 279252e3346SAl Viro __be32 saddr_min; /* inclusive, source IP address */ 280252e3346SAl Viro __be32 saddr_max; /* exclusive, source IP address */ 281252e3346SAl Viro __be32 daddr_min; /* inclusive, dest IP address */ 282252e3346SAl Viro __be32 daddr_max; /* exclusive, dest IP address */ 2831da177e4SLinus Torvalds 2841da177e4SLinus Torvalds __u16 udp_src_min; /* inclusive, source UDP port */ 2851da177e4SLinus Torvalds __u16 udp_src_max; /* exclusive, source UDP port */ 2861da177e4SLinus Torvalds __u16 udp_dst_min; /* inclusive, dest UDP port */ 2871da177e4SLinus Torvalds __u16 udp_dst_max; /* exclusive, dest UDP port */ 2881da177e4SLinus Torvalds 2891ca7768cSFrancesco Fondelli /* DSCP + ECN */ 2901ca7768cSFrancesco Fondelli __u8 tos; /* six most significant bits of (former) IPv4 TOS are for dscp codepoint */ 2911ca7768cSFrancesco Fondelli __u8 traffic_class; /* ditto for the (former) Traffic Class in IPv6 (see RFC 3260, sec. 4) */ 2921ca7768cSFrancesco Fondelli 293ca6549afSSteven Whitehouse /* MPLS */ 294ca6549afSSteven Whitehouse unsigned nr_labels; /* Depth of stack, 0 = no MPLS */ 295ca6549afSSteven Whitehouse __be32 labels[MAX_MPLS_LABELS]; 296ca6549afSSteven Whitehouse 29734954ddcSFrancesco Fondelli /* VLAN/SVLAN (802.1Q/Q-in-Q) */ 29834954ddcSFrancesco Fondelli __u8 vlan_p; 29934954ddcSFrancesco Fondelli __u8 vlan_cfi; 30034954ddcSFrancesco Fondelli __u16 vlan_id; /* 0xffff means no vlan tag */ 30134954ddcSFrancesco Fondelli 30234954ddcSFrancesco Fondelli __u8 svlan_p; 30334954ddcSFrancesco Fondelli __u8 svlan_cfi; 30434954ddcSFrancesco Fondelli __u16 svlan_id; /* 0xffff means no svlan tag */ 30534954ddcSFrancesco Fondelli 3061da177e4SLinus Torvalds __u32 src_mac_count; /* How many MACs to iterate through */ 3071da177e4SLinus Torvalds __u32 dst_mac_count; /* How many MACs to iterate through */ 3081da177e4SLinus Torvalds 309f404e9a6SKris Katterjohn unsigned char dst_mac[ETH_ALEN]; 310f404e9a6SKris Katterjohn unsigned char src_mac[ETH_ALEN]; 3111da177e4SLinus Torvalds 3121da177e4SLinus Torvalds __u32 cur_dst_mac_offset; 3131da177e4SLinus Torvalds __u32 cur_src_mac_offset; 314252e3346SAl Viro __be32 cur_saddr; 315252e3346SAl Viro __be32 cur_daddr; 3161da177e4SLinus Torvalds __u16 cur_udp_dst; 3171da177e4SLinus Torvalds __u16 cur_udp_src; 3181da177e4SLinus Torvalds __u32 cur_pkt_size; 3191da177e4SLinus Torvalds 3201da177e4SLinus Torvalds __u8 hh[14]; 3211da177e4SLinus Torvalds /* = { 3221da177e4SLinus Torvalds 0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB, 3231da177e4SLinus Torvalds 3241da177e4SLinus Torvalds We fill in SRC address later 3251da177e4SLinus Torvalds 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 3261da177e4SLinus Torvalds 0x08, 0x00 3271da177e4SLinus Torvalds }; 3281da177e4SLinus Torvalds */ 3291da177e4SLinus Torvalds __u16 pad; /* pad out the hh struct to an even 16 bytes */ 3301da177e4SLinus Torvalds 3311da177e4SLinus Torvalds struct sk_buff *skb; /* skb we are to transmit next, mainly used for when we 3321da177e4SLinus Torvalds * are transmitting the same one multiple times 3331da177e4SLinus Torvalds */ 3341da177e4SLinus Torvalds struct net_device *odev; /* The out-going device. Note that the device should 3351da177e4SLinus Torvalds * have it's pg_info pointer pointing back to this 3361da177e4SLinus Torvalds * device. This will be set when the user specifies 3371da177e4SLinus Torvalds * the out-going device name (not when the inject is 3381da177e4SLinus Torvalds * started as it used to do.) 3391da177e4SLinus Torvalds */ 3401da177e4SLinus Torvalds struct flow_state *flows; 3411da177e4SLinus Torvalds unsigned cflows; /* Concurrent flows (config) */ 3421da177e4SLinus Torvalds unsigned lflow; /* Flow length (config) */ 3431da177e4SLinus Torvalds unsigned nflows; /* accumulated flows (stats) */ 34439df232fSStephen Hemminger 34539df232fSStephen Hemminger char result[512]; 3461da177e4SLinus Torvalds }; 3471da177e4SLinus Torvalds 3481da177e4SLinus Torvalds struct pktgen_hdr { 349252e3346SAl Viro __be32 pgh_magic; 350252e3346SAl Viro __be32 seq_num; 351252e3346SAl Viro __be32 tv_sec; 352252e3346SAl Viro __be32 tv_usec; 3531da177e4SLinus Torvalds }; 3541da177e4SLinus Torvalds 3551da177e4SLinus Torvalds struct pktgen_thread { 3561da177e4SLinus Torvalds spinlock_t if_lock; 357c26a8016SLuiz Capitulino struct list_head if_list; /* All device here */ 358cdcdbe0bSLuiz Capitulino struct list_head th_list; 359ee74baa7SDavid S. Miller struct task_struct *tsk; 3601da177e4SLinus Torvalds char result[512]; 3611da177e4SLinus Torvalds u32 max_before_softirq; /* We'll call do_softirq to prevent starvation. */ 3621da177e4SLinus Torvalds 3631da177e4SLinus Torvalds /* Field for thread to receive "posted" events terminate, stop ifs etc. */ 3641da177e4SLinus Torvalds 3651da177e4SLinus Torvalds u32 control; 3661da177e4SLinus Torvalds int pid; 3671da177e4SLinus Torvalds int cpu; 3681da177e4SLinus Torvalds 3691da177e4SLinus Torvalds wait_queue_head_t queue; 3701da177e4SLinus Torvalds }; 3711da177e4SLinus Torvalds 3721da177e4SLinus Torvalds #define REMOVE 1 3731da177e4SLinus Torvalds #define FIND 0 3741da177e4SLinus Torvalds 3751da177e4SLinus Torvalds /* This code works around the fact that do_div cannot handle two 64-bit 3761da177e4SLinus Torvalds numbers, and regular 64-bit division doesn't work on x86 kernels. 3771da177e4SLinus Torvalds --Ben 3781da177e4SLinus Torvalds */ 3791da177e4SLinus Torvalds 3801da177e4SLinus Torvalds #define PG_DIV 0 3811da177e4SLinus Torvalds 3821da177e4SLinus Torvalds /* This was emailed to LMKL by: Chris Caputo <ccaputo@alt.net> 3831da177e4SLinus Torvalds * Function copied/adapted/optimized from: 3841da177e4SLinus Torvalds * 3851da177e4SLinus Torvalds * nemesis.sourceforge.net/browse/lib/static/intmath/ix86/intmath.c.html 3861da177e4SLinus Torvalds * 3871da177e4SLinus Torvalds * Copyright 1994, University of Cambridge Computer Laboratory 3881da177e4SLinus Torvalds * All Rights Reserved. 3891da177e4SLinus Torvalds * 3901da177e4SLinus Torvalds */ 39177933d72SJesper Juhl static inline s64 divremdi3(s64 x, s64 y, int type) 3921da177e4SLinus Torvalds { 3931da177e4SLinus Torvalds u64 a = (x < 0) ? -x : x; 3941da177e4SLinus Torvalds u64 b = (y < 0) ? -y : y; 3951da177e4SLinus Torvalds u64 res = 0, d = 1; 3961da177e4SLinus Torvalds 3971da177e4SLinus Torvalds if (b > 0) { 3981da177e4SLinus Torvalds while (b < a) { 3991da177e4SLinus Torvalds b <<= 1; 4001da177e4SLinus Torvalds d <<= 1; 4011da177e4SLinus Torvalds } 4021da177e4SLinus Torvalds } 4031da177e4SLinus Torvalds 4041da177e4SLinus Torvalds do { 4051da177e4SLinus Torvalds if (a >= b) { 4061da177e4SLinus Torvalds a -= b; 4071da177e4SLinus Torvalds res += d; 4081da177e4SLinus Torvalds } 4091da177e4SLinus Torvalds b >>= 1; 4101da177e4SLinus Torvalds d >>= 1; 4111da177e4SLinus Torvalds } 4121da177e4SLinus Torvalds while (d); 4131da177e4SLinus Torvalds 4141da177e4SLinus Torvalds if (PG_DIV == type) { 4151da177e4SLinus Torvalds return (((x ^ y) & (1ll << 63)) == 0) ? res : -(s64) res; 416222f1806SLuiz Capitulino } else { 4171da177e4SLinus Torvalds return ((x & (1ll << 63)) == 0) ? a : -(s64) a; 4181da177e4SLinus Torvalds } 4191da177e4SLinus Torvalds } 4201da177e4SLinus Torvalds 4211da177e4SLinus Torvalds /* End of hacks to deal with 64-bit math on x86 */ 4221da177e4SLinus Torvalds 423b4099fabSStephen Hemminger /** Convert to milliseconds */ 4241da177e4SLinus Torvalds static inline __u64 tv_to_ms(const struct timeval *tv) 4251da177e4SLinus Torvalds { 4261da177e4SLinus Torvalds __u64 ms = tv->tv_usec / 1000; 4271da177e4SLinus Torvalds ms += (__u64) tv->tv_sec * (__u64) 1000; 4281da177e4SLinus Torvalds return ms; 4291da177e4SLinus Torvalds } 4301da177e4SLinus Torvalds 4311da177e4SLinus Torvalds /** Convert to micro-seconds */ 4321da177e4SLinus Torvalds static inline __u64 tv_to_us(const struct timeval *tv) 4331da177e4SLinus Torvalds { 4341da177e4SLinus Torvalds __u64 us = tv->tv_usec; 4351da177e4SLinus Torvalds us += (__u64) tv->tv_sec * (__u64) 1000000; 4361da177e4SLinus Torvalds return us; 4371da177e4SLinus Torvalds } 4381da177e4SLinus Torvalds 439222f1806SLuiz Capitulino static inline __u64 pg_div(__u64 n, __u32 base) 440222f1806SLuiz Capitulino { 4411da177e4SLinus Torvalds __u64 tmp = n; 4421da177e4SLinus Torvalds do_div(tmp, base); 4431da177e4SLinus Torvalds /* printk("pktgen: pg_div, n: %llu base: %d rv: %llu\n", 4441da177e4SLinus Torvalds n, base, tmp); */ 4451da177e4SLinus Torvalds return tmp; 4461da177e4SLinus Torvalds } 4471da177e4SLinus Torvalds 4481da177e4SLinus Torvalds static inline __u64 pg_div64(__u64 n, __u64 base) 4491da177e4SLinus Torvalds { 4501da177e4SLinus Torvalds __u64 tmp = n; 4511da177e4SLinus Torvalds /* 452b4099fabSStephen Hemminger * How do we know if the architecture we are running on 4531da177e4SLinus Torvalds * supports division with 64 bit base? 4541da177e4SLinus Torvalds * 4551da177e4SLinus Torvalds */ 4561da177e4SLinus Torvalds #if defined(__sparc_v9__) || defined(__powerpc64__) || defined(__alpha__) || defined(__x86_64__) || defined(__ia64__) 4571da177e4SLinus Torvalds 4581da177e4SLinus Torvalds do_div(tmp, base); 4591da177e4SLinus Torvalds #else 4601da177e4SLinus Torvalds tmp = divremdi3(n, base, PG_DIV); 4611da177e4SLinus Torvalds #endif 4621da177e4SLinus Torvalds return tmp; 4631da177e4SLinus Torvalds } 4641da177e4SLinus Torvalds 4651da177e4SLinus Torvalds static inline __u64 getCurMs(void) 4661da177e4SLinus Torvalds { 4671da177e4SLinus Torvalds struct timeval tv; 4681da177e4SLinus Torvalds do_gettimeofday(&tv); 4691da177e4SLinus Torvalds return tv_to_ms(&tv); 4701da177e4SLinus Torvalds } 4711da177e4SLinus Torvalds 4721da177e4SLinus Torvalds static inline __u64 getCurUs(void) 4731da177e4SLinus Torvalds { 4741da177e4SLinus Torvalds struct timeval tv; 4751da177e4SLinus Torvalds do_gettimeofday(&tv); 4761da177e4SLinus Torvalds return tv_to_us(&tv); 4771da177e4SLinus Torvalds } 4781da177e4SLinus Torvalds 4791da177e4SLinus Torvalds static inline __u64 tv_diff(const struct timeval *a, const struct timeval *b) 4801da177e4SLinus Torvalds { 4811da177e4SLinus Torvalds return tv_to_us(a) - tv_to_us(b); 4821da177e4SLinus Torvalds } 4831da177e4SLinus Torvalds 4841da177e4SLinus Torvalds /* old include end */ 4851da177e4SLinus Torvalds 4861da177e4SLinus Torvalds static char version[] __initdata = VERSION; 4871da177e4SLinus Torvalds 4881da177e4SLinus Torvalds static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *i); 4891da177e4SLinus Torvalds static int pktgen_add_device(struct pktgen_thread *t, const char *ifname); 490222f1806SLuiz Capitulino static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t, 491222f1806SLuiz Capitulino const char *ifname); 4921da177e4SLinus Torvalds static int pktgen_device_event(struct notifier_block *, unsigned long, void *); 4931da177e4SLinus Torvalds static void pktgen_run_all_threads(void); 4941da177e4SLinus Torvalds static void pktgen_stop_all_threads_ifs(void); 4951da177e4SLinus Torvalds static int pktgen_stop_device(struct pktgen_dev *pkt_dev); 4961da177e4SLinus Torvalds static void pktgen_stop(struct pktgen_thread *t); 4971da177e4SLinus Torvalds static void pktgen_clear_counters(struct pktgen_dev *pkt_dev); 49839df232fSStephen Hemminger 4991da177e4SLinus Torvalds static unsigned int scan_ip6(const char *s, char ip[16]); 5001da177e4SLinus Torvalds static unsigned int fmt_ip6(char *s, const char ip[16]); 5011da177e4SLinus Torvalds 5021da177e4SLinus Torvalds /* Module parameters, defaults. */ 5031da177e4SLinus Torvalds static int pg_count_d = 1000; /* 1000 pkts by default */ 504f34fbb97SJaco Kroon static int pg_delay_d; 505f34fbb97SJaco Kroon static int pg_clone_skb_d; 506f34fbb97SJaco Kroon static int debug; 5071da177e4SLinus Torvalds 508222fa076SLuiz Capitulino static DEFINE_MUTEX(pktgen_thread_lock); 509cdcdbe0bSLuiz Capitulino static LIST_HEAD(pktgen_threads); 5101da177e4SLinus Torvalds 5111da177e4SLinus Torvalds static struct notifier_block pktgen_notifier_block = { 5121da177e4SLinus Torvalds .notifier_call = pktgen_device_event, 5131da177e4SLinus Torvalds }; 5141da177e4SLinus Torvalds 5151da177e4SLinus Torvalds /* 5161da177e4SLinus Torvalds * /proc handling functions 5171da177e4SLinus Torvalds * 5181da177e4SLinus Torvalds */ 5191da177e4SLinus Torvalds 520d50a6b56SStephen Hemminger static int pgctrl_show(struct seq_file *seq, void *v) 521d50a6b56SStephen Hemminger { 522d50a6b56SStephen Hemminger seq_puts(seq, VERSION); 523d50a6b56SStephen Hemminger return 0; 524d50a6b56SStephen Hemminger } 5251da177e4SLinus Torvalds 526d50a6b56SStephen Hemminger static ssize_t pgctrl_write(struct file *file, const char __user * buf, 5271da177e4SLinus Torvalds size_t count, loff_t * ppos) 5281da177e4SLinus Torvalds { 5291da177e4SLinus Torvalds int err = 0; 530d50a6b56SStephen Hemminger char data[128]; 5311da177e4SLinus Torvalds 5321da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN)) { 5331da177e4SLinus Torvalds err = -EPERM; 5341da177e4SLinus Torvalds goto out; 5351da177e4SLinus Torvalds } 5361da177e4SLinus Torvalds 537d50a6b56SStephen Hemminger if (count > sizeof(data)) 538d50a6b56SStephen Hemminger count = sizeof(data); 5391da177e4SLinus Torvalds 5401da177e4SLinus Torvalds if (copy_from_user(data, buf, count)) { 5411da177e4SLinus Torvalds err = -EFAULT; 542d50a6b56SStephen Hemminger goto out; 5431da177e4SLinus Torvalds } 5441da177e4SLinus Torvalds data[count - 1] = 0; /* Make string */ 5451da177e4SLinus Torvalds 5461da177e4SLinus Torvalds if (!strcmp(data, "stop")) 5471da177e4SLinus Torvalds pktgen_stop_all_threads_ifs(); 5481da177e4SLinus Torvalds 5491da177e4SLinus Torvalds else if (!strcmp(data, "start")) 5501da177e4SLinus Torvalds pktgen_run_all_threads(); 5511da177e4SLinus Torvalds 5521da177e4SLinus Torvalds else 5531da177e4SLinus Torvalds printk("pktgen: Unknown command: %s\n", data); 5541da177e4SLinus Torvalds 5551da177e4SLinus Torvalds err = count; 5561da177e4SLinus Torvalds 5571da177e4SLinus Torvalds out: 5581da177e4SLinus Torvalds return err; 5591da177e4SLinus Torvalds } 5601da177e4SLinus Torvalds 561d50a6b56SStephen Hemminger static int pgctrl_open(struct inode *inode, struct file *file) 5621da177e4SLinus Torvalds { 563d50a6b56SStephen Hemminger return single_open(file, pgctrl_show, PDE(inode)->data); 564d50a6b56SStephen Hemminger } 565d50a6b56SStephen Hemminger 5669a32144eSArjan van de Ven static const struct file_operations pktgen_fops = { 567d50a6b56SStephen Hemminger .owner = THIS_MODULE, 568d50a6b56SStephen Hemminger .open = pgctrl_open, 569d50a6b56SStephen Hemminger .read = seq_read, 570d50a6b56SStephen Hemminger .llseek = seq_lseek, 571d50a6b56SStephen Hemminger .write = pgctrl_write, 572d50a6b56SStephen Hemminger .release = single_release, 573d50a6b56SStephen Hemminger }; 574d50a6b56SStephen Hemminger 575d50a6b56SStephen Hemminger static int pktgen_if_show(struct seq_file *seq, void *v) 576d50a6b56SStephen Hemminger { 5771da177e4SLinus Torvalds int i; 578d50a6b56SStephen Hemminger struct pktgen_dev *pkt_dev = seq->private; 5791da177e4SLinus Torvalds __u64 sa; 5801da177e4SLinus Torvalds __u64 stopped; 5811da177e4SLinus Torvalds __u64 now = getCurUs(); 5821da177e4SLinus Torvalds 583222f1806SLuiz Capitulino seq_printf(seq, 584222f1806SLuiz Capitulino "Params: count %llu min_pkt_size: %u max_pkt_size: %u\n", 585222f1806SLuiz Capitulino (unsigned long long)pkt_dev->count, pkt_dev->min_pkt_size, 586222f1806SLuiz Capitulino pkt_dev->max_pkt_size); 5871da177e4SLinus Torvalds 588222f1806SLuiz Capitulino seq_printf(seq, 589222f1806SLuiz Capitulino " frags: %d delay: %u clone_skb: %d ifname: %s\n", 590222f1806SLuiz Capitulino pkt_dev->nfrags, 591222f1806SLuiz Capitulino 1000 * pkt_dev->delay_us + pkt_dev->delay_ns, 59239df232fSStephen Hemminger pkt_dev->clone_skb, pkt_dev->odev->name); 5931da177e4SLinus Torvalds 594222f1806SLuiz Capitulino seq_printf(seq, " flows: %u flowlen: %u\n", pkt_dev->cflows, 595222f1806SLuiz Capitulino pkt_dev->lflow); 5961da177e4SLinus Torvalds 5971da177e4SLinus Torvalds if (pkt_dev->flags & F_IPV6) { 5981da177e4SLinus Torvalds char b1[128], b2[128], b3[128]; 5991da177e4SLinus Torvalds fmt_ip6(b1, pkt_dev->in6_saddr.s6_addr); 6001da177e4SLinus Torvalds fmt_ip6(b2, pkt_dev->min_in6_saddr.s6_addr); 6011da177e4SLinus Torvalds fmt_ip6(b3, pkt_dev->max_in6_saddr.s6_addr); 602222f1806SLuiz Capitulino seq_printf(seq, 603222f1806SLuiz Capitulino " saddr: %s min_saddr: %s max_saddr: %s\n", b1, 604222f1806SLuiz Capitulino b2, b3); 6051da177e4SLinus Torvalds 6061da177e4SLinus Torvalds fmt_ip6(b1, pkt_dev->in6_daddr.s6_addr); 6071da177e4SLinus Torvalds fmt_ip6(b2, pkt_dev->min_in6_daddr.s6_addr); 6081da177e4SLinus Torvalds fmt_ip6(b3, pkt_dev->max_in6_daddr.s6_addr); 609222f1806SLuiz Capitulino seq_printf(seq, 610222f1806SLuiz Capitulino " daddr: %s min_daddr: %s max_daddr: %s\n", b1, 611222f1806SLuiz Capitulino b2, b3); 6121da177e4SLinus Torvalds 613222f1806SLuiz Capitulino } else 614222f1806SLuiz Capitulino seq_printf(seq, 615222f1806SLuiz Capitulino " dst_min: %s dst_max: %s\n src_min: %s src_max: %s\n", 616222f1806SLuiz Capitulino pkt_dev->dst_min, pkt_dev->dst_max, pkt_dev->src_min, 617222f1806SLuiz Capitulino pkt_dev->src_max); 6181da177e4SLinus Torvalds 619d50a6b56SStephen Hemminger seq_puts(seq, " src_mac: "); 6201da177e4SLinus Torvalds 621f404e9a6SKris Katterjohn if (is_zero_ether_addr(pkt_dev->src_mac)) 6221da177e4SLinus Torvalds for (i = 0; i < 6; i++) 623222f1806SLuiz Capitulino seq_printf(seq, "%02X%s", pkt_dev->odev->dev_addr[i], 624222f1806SLuiz Capitulino i == 5 ? " " : ":"); 6251da177e4SLinus Torvalds else 6261da177e4SLinus Torvalds for (i = 0; i < 6; i++) 627222f1806SLuiz Capitulino seq_printf(seq, "%02X%s", pkt_dev->src_mac[i], 628222f1806SLuiz Capitulino i == 5 ? " " : ":"); 6291da177e4SLinus Torvalds 630d50a6b56SStephen Hemminger seq_printf(seq, "dst_mac: "); 6311da177e4SLinus Torvalds for (i = 0; i < 6; i++) 632222f1806SLuiz Capitulino seq_printf(seq, "%02X%s", pkt_dev->dst_mac[i], 633222f1806SLuiz Capitulino i == 5 ? "\n" : ":"); 6341da177e4SLinus Torvalds 635222f1806SLuiz Capitulino seq_printf(seq, 636222f1806SLuiz Capitulino " udp_src_min: %d udp_src_max: %d udp_dst_min: %d udp_dst_max: %d\n", 637222f1806SLuiz Capitulino pkt_dev->udp_src_min, pkt_dev->udp_src_max, 638222f1806SLuiz Capitulino pkt_dev->udp_dst_min, pkt_dev->udp_dst_max); 6391da177e4SLinus Torvalds 640222f1806SLuiz Capitulino seq_printf(seq, 641ca6549afSSteven Whitehouse " src_mac_count: %d dst_mac_count: %d\n", 6421da177e4SLinus Torvalds pkt_dev->src_mac_count, pkt_dev->dst_mac_count); 6431da177e4SLinus Torvalds 644ca6549afSSteven Whitehouse if (pkt_dev->nr_labels) { 645ca6549afSSteven Whitehouse unsigned i; 646ca6549afSSteven Whitehouse seq_printf(seq, " mpls: "); 647ca6549afSSteven Whitehouse for (i = 0; i < pkt_dev->nr_labels; i++) 648ca6549afSSteven Whitehouse seq_printf(seq, "%08x%s", ntohl(pkt_dev->labels[i]), 649ca6549afSSteven Whitehouse i == pkt_dev->nr_labels-1 ? "\n" : ", "); 650ca6549afSSteven Whitehouse } 651ca6549afSSteven Whitehouse 65234954ddcSFrancesco Fondelli if (pkt_dev->vlan_id != 0xffff) { 65334954ddcSFrancesco Fondelli seq_printf(seq, " vlan_id: %u vlan_p: %u vlan_cfi: %u\n", 65434954ddcSFrancesco Fondelli pkt_dev->vlan_id, pkt_dev->vlan_p, pkt_dev->vlan_cfi); 65534954ddcSFrancesco Fondelli } 65634954ddcSFrancesco Fondelli 65734954ddcSFrancesco Fondelli if (pkt_dev->svlan_id != 0xffff) { 65834954ddcSFrancesco Fondelli seq_printf(seq, " svlan_id: %u vlan_p: %u vlan_cfi: %u\n", 65934954ddcSFrancesco Fondelli pkt_dev->svlan_id, pkt_dev->svlan_p, pkt_dev->svlan_cfi); 66034954ddcSFrancesco Fondelli } 66134954ddcSFrancesco Fondelli 6621ca7768cSFrancesco Fondelli if (pkt_dev->tos) { 6631ca7768cSFrancesco Fondelli seq_printf(seq, " tos: 0x%02x\n", pkt_dev->tos); 6641ca7768cSFrancesco Fondelli } 6651ca7768cSFrancesco Fondelli 6661ca7768cSFrancesco Fondelli if (pkt_dev->traffic_class) { 6671ca7768cSFrancesco Fondelli seq_printf(seq, " traffic_class: 0x%02x\n", pkt_dev->traffic_class); 6681ca7768cSFrancesco Fondelli } 6691ca7768cSFrancesco Fondelli 670ca6549afSSteven Whitehouse seq_printf(seq, " Flags: "); 671ca6549afSSteven Whitehouse 6721da177e4SLinus Torvalds if (pkt_dev->flags & F_IPV6) 673d50a6b56SStephen Hemminger seq_printf(seq, "IPV6 "); 6741da177e4SLinus Torvalds 6751da177e4SLinus Torvalds if (pkt_dev->flags & F_IPSRC_RND) 676d50a6b56SStephen Hemminger seq_printf(seq, "IPSRC_RND "); 6771da177e4SLinus Torvalds 6781da177e4SLinus Torvalds if (pkt_dev->flags & F_IPDST_RND) 679d50a6b56SStephen Hemminger seq_printf(seq, "IPDST_RND "); 6801da177e4SLinus Torvalds 6811da177e4SLinus Torvalds if (pkt_dev->flags & F_TXSIZE_RND) 682d50a6b56SStephen Hemminger seq_printf(seq, "TXSIZE_RND "); 6831da177e4SLinus Torvalds 6841da177e4SLinus Torvalds if (pkt_dev->flags & F_UDPSRC_RND) 685d50a6b56SStephen Hemminger seq_printf(seq, "UDPSRC_RND "); 6861da177e4SLinus Torvalds 6871da177e4SLinus Torvalds if (pkt_dev->flags & F_UDPDST_RND) 688d50a6b56SStephen Hemminger seq_printf(seq, "UDPDST_RND "); 6891da177e4SLinus Torvalds 690ca6549afSSteven Whitehouse if (pkt_dev->flags & F_MPLS_RND) 691ca6549afSSteven Whitehouse seq_printf(seq, "MPLS_RND "); 692ca6549afSSteven Whitehouse 6931da177e4SLinus Torvalds if (pkt_dev->flags & F_MACSRC_RND) 694d50a6b56SStephen Hemminger seq_printf(seq, "MACSRC_RND "); 6951da177e4SLinus Torvalds 6961da177e4SLinus Torvalds if (pkt_dev->flags & F_MACDST_RND) 697d50a6b56SStephen Hemminger seq_printf(seq, "MACDST_RND "); 6981da177e4SLinus Torvalds 69934954ddcSFrancesco Fondelli if (pkt_dev->flags & F_VID_RND) 70034954ddcSFrancesco Fondelli seq_printf(seq, "VID_RND "); 70134954ddcSFrancesco Fondelli 70234954ddcSFrancesco Fondelli if (pkt_dev->flags & F_SVID_RND) 70334954ddcSFrancesco Fondelli seq_printf(seq, "SVID_RND "); 70434954ddcSFrancesco Fondelli 705d50a6b56SStephen Hemminger seq_puts(seq, "\n"); 7061da177e4SLinus Torvalds 7071da177e4SLinus Torvalds sa = pkt_dev->started_at; 7081da177e4SLinus Torvalds stopped = pkt_dev->stopped_at; 7091da177e4SLinus Torvalds if (pkt_dev->running) 7101da177e4SLinus Torvalds stopped = now; /* not really stopped, more like last-running-at */ 7111da177e4SLinus Torvalds 712222f1806SLuiz Capitulino seq_printf(seq, 713222f1806SLuiz Capitulino "Current:\n pkts-sofar: %llu errors: %llu\n started: %lluus stopped: %lluus idle: %lluus\n", 7141da177e4SLinus Torvalds (unsigned long long)pkt_dev->sofar, 715222f1806SLuiz Capitulino (unsigned long long)pkt_dev->errors, (unsigned long long)sa, 7161da177e4SLinus Torvalds (unsigned long long)stopped, 7171da177e4SLinus Torvalds (unsigned long long)pkt_dev->idle_acc); 7181da177e4SLinus Torvalds 719222f1806SLuiz Capitulino seq_printf(seq, 720222f1806SLuiz Capitulino " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n", 721d50a6b56SStephen Hemminger pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset, 722d50a6b56SStephen Hemminger pkt_dev->cur_src_mac_offset); 7231da177e4SLinus Torvalds 7241da177e4SLinus Torvalds if (pkt_dev->flags & F_IPV6) { 7251da177e4SLinus Torvalds char b1[128], b2[128]; 7261da177e4SLinus Torvalds fmt_ip6(b1, pkt_dev->cur_in6_daddr.s6_addr); 7271da177e4SLinus Torvalds fmt_ip6(b2, pkt_dev->cur_in6_saddr.s6_addr); 728d50a6b56SStephen Hemminger seq_printf(seq, " cur_saddr: %s cur_daddr: %s\n", b2, b1); 729222f1806SLuiz Capitulino } else 730d50a6b56SStephen Hemminger seq_printf(seq, " cur_saddr: 0x%x cur_daddr: 0x%x\n", 7311da177e4SLinus Torvalds pkt_dev->cur_saddr, pkt_dev->cur_daddr); 7321da177e4SLinus Torvalds 733d50a6b56SStephen Hemminger seq_printf(seq, " cur_udp_dst: %d cur_udp_src: %d\n", 7341da177e4SLinus Torvalds pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src); 7351da177e4SLinus Torvalds 736d50a6b56SStephen Hemminger seq_printf(seq, " flows: %u\n", pkt_dev->nflows); 7371da177e4SLinus Torvalds 7381da177e4SLinus Torvalds if (pkt_dev->result[0]) 739d50a6b56SStephen Hemminger seq_printf(seq, "Result: %s\n", pkt_dev->result); 7401da177e4SLinus Torvalds else 741d50a6b56SStephen Hemminger seq_printf(seq, "Result: Idle\n"); 7421da177e4SLinus Torvalds 743d50a6b56SStephen Hemminger return 0; 7441da177e4SLinus Torvalds } 7451da177e4SLinus Torvalds 746ca6549afSSteven Whitehouse 7471ca7768cSFrancesco Fondelli static int hex32_arg(const char __user *user_buffer, unsigned long maxlen, __u32 *num) 748ca6549afSSteven Whitehouse { 749ca6549afSSteven Whitehouse int i = 0; 750ca6549afSSteven Whitehouse *num = 0; 751ca6549afSSteven Whitehouse 7521ca7768cSFrancesco Fondelli for (; i < maxlen; i++) { 753ca6549afSSteven Whitehouse char c; 754ca6549afSSteven Whitehouse *num <<= 4; 755ca6549afSSteven Whitehouse if (get_user(c, &user_buffer[i])) 756ca6549afSSteven Whitehouse return -EFAULT; 757ca6549afSSteven Whitehouse if ((c >= '0') && (c <= '9')) 758ca6549afSSteven Whitehouse *num |= c - '0'; 759ca6549afSSteven Whitehouse else if ((c >= 'a') && (c <= 'f')) 760ca6549afSSteven Whitehouse *num |= c - 'a' + 10; 761ca6549afSSteven Whitehouse else if ((c >= 'A') && (c <= 'F')) 762ca6549afSSteven Whitehouse *num |= c - 'A' + 10; 763ca6549afSSteven Whitehouse else 764ca6549afSSteven Whitehouse break; 765ca6549afSSteven Whitehouse } 766ca6549afSSteven Whitehouse return i; 767ca6549afSSteven Whitehouse } 768ca6549afSSteven Whitehouse 769222f1806SLuiz Capitulino static int count_trail_chars(const char __user * user_buffer, 770222f1806SLuiz Capitulino unsigned int maxlen) 7711da177e4SLinus Torvalds { 7721da177e4SLinus Torvalds int i; 7731da177e4SLinus Torvalds 7741da177e4SLinus Torvalds for (i = 0; i < maxlen; i++) { 7751da177e4SLinus Torvalds char c; 7761da177e4SLinus Torvalds if (get_user(c, &user_buffer[i])) 7771da177e4SLinus Torvalds return -EFAULT; 7781da177e4SLinus Torvalds switch (c) { 7791da177e4SLinus Torvalds case '\"': 7801da177e4SLinus Torvalds case '\n': 7811da177e4SLinus Torvalds case '\r': 7821da177e4SLinus Torvalds case '\t': 7831da177e4SLinus Torvalds case ' ': 7841da177e4SLinus Torvalds case '=': 7851da177e4SLinus Torvalds break; 7861da177e4SLinus Torvalds default: 7871da177e4SLinus Torvalds goto done; 7883ff50b79SStephen Hemminger } 7891da177e4SLinus Torvalds } 7901da177e4SLinus Torvalds done: 7911da177e4SLinus Torvalds return i; 7921da177e4SLinus Torvalds } 7931da177e4SLinus Torvalds 794222f1806SLuiz Capitulino static unsigned long num_arg(const char __user * user_buffer, 795222f1806SLuiz Capitulino unsigned long maxlen, unsigned long *num) 7961da177e4SLinus Torvalds { 7971da177e4SLinus Torvalds int i = 0; 7981da177e4SLinus Torvalds *num = 0; 7991da177e4SLinus Torvalds 8001da177e4SLinus Torvalds for (; i < maxlen; i++) { 8011da177e4SLinus Torvalds char c; 8021da177e4SLinus Torvalds if (get_user(c, &user_buffer[i])) 8031da177e4SLinus Torvalds return -EFAULT; 8041da177e4SLinus Torvalds if ((c >= '0') && (c <= '9')) { 8051da177e4SLinus Torvalds *num *= 10; 8061da177e4SLinus Torvalds *num += c - '0'; 8071da177e4SLinus Torvalds } else 8081da177e4SLinus Torvalds break; 8091da177e4SLinus Torvalds } 8101da177e4SLinus Torvalds return i; 8111da177e4SLinus Torvalds } 8121da177e4SLinus Torvalds 8131da177e4SLinus Torvalds static int strn_len(const char __user * user_buffer, unsigned int maxlen) 8141da177e4SLinus Torvalds { 8151da177e4SLinus Torvalds int i = 0; 8161da177e4SLinus Torvalds 8171da177e4SLinus Torvalds for (; i < maxlen; i++) { 8181da177e4SLinus Torvalds char c; 8191da177e4SLinus Torvalds if (get_user(c, &user_buffer[i])) 8201da177e4SLinus Torvalds return -EFAULT; 8211da177e4SLinus Torvalds switch (c) { 8221da177e4SLinus Torvalds case '\"': 8231da177e4SLinus Torvalds case '\n': 8241da177e4SLinus Torvalds case '\r': 8251da177e4SLinus Torvalds case '\t': 8261da177e4SLinus Torvalds case ' ': 8271da177e4SLinus Torvalds goto done_str; 8281da177e4SLinus Torvalds break; 8291da177e4SLinus Torvalds default: 8301da177e4SLinus Torvalds break; 8313ff50b79SStephen Hemminger } 8321da177e4SLinus Torvalds } 8331da177e4SLinus Torvalds done_str: 8341da177e4SLinus Torvalds return i; 8351da177e4SLinus Torvalds } 8361da177e4SLinus Torvalds 837ca6549afSSteven Whitehouse static ssize_t get_labels(const char __user *buffer, struct pktgen_dev *pkt_dev) 838ca6549afSSteven Whitehouse { 839ca6549afSSteven Whitehouse unsigned n = 0; 840ca6549afSSteven Whitehouse char c; 841ca6549afSSteven Whitehouse ssize_t i = 0; 842ca6549afSSteven Whitehouse int len; 843ca6549afSSteven Whitehouse 844ca6549afSSteven Whitehouse pkt_dev->nr_labels = 0; 845ca6549afSSteven Whitehouse do { 846ca6549afSSteven Whitehouse __u32 tmp; 8471ca7768cSFrancesco Fondelli len = hex32_arg(&buffer[i], 8, &tmp); 848ca6549afSSteven Whitehouse if (len <= 0) 849ca6549afSSteven Whitehouse return len; 850ca6549afSSteven Whitehouse pkt_dev->labels[n] = htonl(tmp); 851ca6549afSSteven Whitehouse if (pkt_dev->labels[n] & MPLS_STACK_BOTTOM) 852ca6549afSSteven Whitehouse pkt_dev->flags |= F_MPLS_RND; 853ca6549afSSteven Whitehouse i += len; 854ca6549afSSteven Whitehouse if (get_user(c, &buffer[i])) 855ca6549afSSteven Whitehouse return -EFAULT; 856ca6549afSSteven Whitehouse i++; 857ca6549afSSteven Whitehouse n++; 858ca6549afSSteven Whitehouse if (n >= MAX_MPLS_LABELS) 859ca6549afSSteven Whitehouse return -E2BIG; 860ca6549afSSteven Whitehouse } while (c == ','); 861ca6549afSSteven Whitehouse 862ca6549afSSteven Whitehouse pkt_dev->nr_labels = n; 863ca6549afSSteven Whitehouse return i; 864ca6549afSSteven Whitehouse } 865ca6549afSSteven Whitehouse 866222f1806SLuiz Capitulino static ssize_t pktgen_if_write(struct file *file, 867222f1806SLuiz Capitulino const char __user * user_buffer, size_t count, 868222f1806SLuiz Capitulino loff_t * offset) 8691da177e4SLinus Torvalds { 870d50a6b56SStephen Hemminger struct seq_file *seq = (struct seq_file *)file->private_data; 871d50a6b56SStephen Hemminger struct pktgen_dev *pkt_dev = seq->private; 8721da177e4SLinus Torvalds int i = 0, max, len; 8731da177e4SLinus Torvalds char name[16], valstr[32]; 8741da177e4SLinus Torvalds unsigned long value = 0; 8751da177e4SLinus Torvalds char *pg_result = NULL; 8761da177e4SLinus Torvalds int tmp = 0; 8771da177e4SLinus Torvalds char buf[128]; 8781da177e4SLinus Torvalds 8791da177e4SLinus Torvalds pg_result = &(pkt_dev->result[0]); 8801da177e4SLinus Torvalds 8811da177e4SLinus Torvalds if (count < 1) { 8821da177e4SLinus Torvalds printk("pktgen: wrong command format\n"); 8831da177e4SLinus Torvalds return -EINVAL; 8841da177e4SLinus Torvalds } 8851da177e4SLinus Torvalds 8861da177e4SLinus Torvalds max = count - i; 8871da177e4SLinus Torvalds tmp = count_trail_chars(&user_buffer[i], max); 8881da177e4SLinus Torvalds if (tmp < 0) { 8891da177e4SLinus Torvalds printk("pktgen: illegal format\n"); 8901da177e4SLinus Torvalds return tmp; 8911da177e4SLinus Torvalds } 8921da177e4SLinus Torvalds i += tmp; 8931da177e4SLinus Torvalds 8941da177e4SLinus Torvalds /* Read variable name */ 8951da177e4SLinus Torvalds 8961da177e4SLinus Torvalds len = strn_len(&user_buffer[i], sizeof(name) - 1); 897222f1806SLuiz Capitulino if (len < 0) { 898222f1806SLuiz Capitulino return len; 899222f1806SLuiz Capitulino } 9001da177e4SLinus Torvalds memset(name, 0, sizeof(name)); 9011da177e4SLinus Torvalds if (copy_from_user(name, &user_buffer[i], len)) 9021da177e4SLinus Torvalds return -EFAULT; 9031da177e4SLinus Torvalds i += len; 9041da177e4SLinus Torvalds 9051da177e4SLinus Torvalds max = count - i; 9061da177e4SLinus Torvalds len = count_trail_chars(&user_buffer[i], max); 9071da177e4SLinus Torvalds if (len < 0) 9081da177e4SLinus Torvalds return len; 9091da177e4SLinus Torvalds 9101da177e4SLinus Torvalds i += len; 9111da177e4SLinus Torvalds 9121da177e4SLinus Torvalds if (debug) { 9131da177e4SLinus Torvalds char tb[count + 1]; 9141da177e4SLinus Torvalds if (copy_from_user(tb, user_buffer, count)) 9151da177e4SLinus Torvalds return -EFAULT; 9161da177e4SLinus Torvalds tb[count] = 0; 917d50a6b56SStephen Hemminger printk("pktgen: %s,%lu buffer -:%s:-\n", name, 918d50a6b56SStephen Hemminger (unsigned long)count, tb); 9191da177e4SLinus Torvalds } 9201da177e4SLinus Torvalds 9211da177e4SLinus Torvalds if (!strcmp(name, "min_pkt_size")) { 9221da177e4SLinus Torvalds len = num_arg(&user_buffer[i], 10, &value); 923222f1806SLuiz Capitulino if (len < 0) { 924222f1806SLuiz Capitulino return len; 925222f1806SLuiz Capitulino } 9261da177e4SLinus Torvalds i += len; 9271da177e4SLinus Torvalds if (value < 14 + 20 + 8) 9281da177e4SLinus Torvalds value = 14 + 20 + 8; 9291da177e4SLinus Torvalds if (value != pkt_dev->min_pkt_size) { 9301da177e4SLinus Torvalds pkt_dev->min_pkt_size = value; 9311da177e4SLinus Torvalds pkt_dev->cur_pkt_size = value; 9321da177e4SLinus Torvalds } 933222f1806SLuiz Capitulino sprintf(pg_result, "OK: min_pkt_size=%u", 934222f1806SLuiz Capitulino pkt_dev->min_pkt_size); 9351da177e4SLinus Torvalds return count; 9361da177e4SLinus Torvalds } 9371da177e4SLinus Torvalds 9381da177e4SLinus Torvalds if (!strcmp(name, "max_pkt_size")) { 9391da177e4SLinus Torvalds len = num_arg(&user_buffer[i], 10, &value); 940222f1806SLuiz Capitulino if (len < 0) { 941222f1806SLuiz Capitulino return len; 942222f1806SLuiz Capitulino } 9431da177e4SLinus Torvalds i += len; 9441da177e4SLinus Torvalds if (value < 14 + 20 + 8) 9451da177e4SLinus Torvalds value = 14 + 20 + 8; 9461da177e4SLinus Torvalds if (value != pkt_dev->max_pkt_size) { 9471da177e4SLinus Torvalds pkt_dev->max_pkt_size = value; 9481da177e4SLinus Torvalds pkt_dev->cur_pkt_size = value; 9491da177e4SLinus Torvalds } 950222f1806SLuiz Capitulino sprintf(pg_result, "OK: max_pkt_size=%u", 951222f1806SLuiz Capitulino pkt_dev->max_pkt_size); 9521da177e4SLinus Torvalds return count; 9531da177e4SLinus Torvalds } 9541da177e4SLinus Torvalds 9551da177e4SLinus Torvalds /* Shortcut for min = max */ 9561da177e4SLinus Torvalds 9571da177e4SLinus Torvalds if (!strcmp(name, "pkt_size")) { 9581da177e4SLinus Torvalds len = num_arg(&user_buffer[i], 10, &value); 959222f1806SLuiz Capitulino if (len < 0) { 960222f1806SLuiz Capitulino return len; 961222f1806SLuiz Capitulino } 9621da177e4SLinus Torvalds i += len; 9631da177e4SLinus Torvalds if (value < 14 + 20 + 8) 9641da177e4SLinus Torvalds value = 14 + 20 + 8; 9651da177e4SLinus Torvalds if (value != pkt_dev->min_pkt_size) { 9661da177e4SLinus Torvalds pkt_dev->min_pkt_size = value; 9671da177e4SLinus Torvalds pkt_dev->max_pkt_size = value; 9681da177e4SLinus Torvalds pkt_dev->cur_pkt_size = value; 9691da177e4SLinus Torvalds } 9701da177e4SLinus Torvalds sprintf(pg_result, "OK: pkt_size=%u", pkt_dev->min_pkt_size); 9711da177e4SLinus Torvalds return count; 9721da177e4SLinus Torvalds } 9731da177e4SLinus Torvalds 9741da177e4SLinus Torvalds if (!strcmp(name, "debug")) { 9751da177e4SLinus Torvalds len = num_arg(&user_buffer[i], 10, &value); 976222f1806SLuiz Capitulino if (len < 0) { 977222f1806SLuiz Capitulino return len; 978222f1806SLuiz Capitulino } 9791da177e4SLinus Torvalds i += len; 9801da177e4SLinus Torvalds debug = value; 9811da177e4SLinus Torvalds sprintf(pg_result, "OK: debug=%u", debug); 9821da177e4SLinus Torvalds return count; 9831da177e4SLinus Torvalds } 9841da177e4SLinus Torvalds 9851da177e4SLinus Torvalds if (!strcmp(name, "frags")) { 9861da177e4SLinus Torvalds len = num_arg(&user_buffer[i], 10, &value); 987222f1806SLuiz Capitulino if (len < 0) { 988222f1806SLuiz Capitulino return len; 989222f1806SLuiz Capitulino } 9901da177e4SLinus Torvalds i += len; 9911da177e4SLinus Torvalds pkt_dev->nfrags = value; 9921da177e4SLinus Torvalds sprintf(pg_result, "OK: frags=%u", pkt_dev->nfrags); 9931da177e4SLinus Torvalds return count; 9941da177e4SLinus Torvalds } 9951da177e4SLinus Torvalds if (!strcmp(name, "delay")) { 9961da177e4SLinus Torvalds len = num_arg(&user_buffer[i], 10, &value); 997222f1806SLuiz Capitulino if (len < 0) { 998222f1806SLuiz Capitulino return len; 999222f1806SLuiz Capitulino } 10001da177e4SLinus Torvalds i += len; 10011da177e4SLinus Torvalds if (value == 0x7FFFFFFF) { 10021da177e4SLinus Torvalds pkt_dev->delay_us = 0x7FFFFFFF; 10031da177e4SLinus Torvalds pkt_dev->delay_ns = 0; 10041da177e4SLinus Torvalds } else { 10051da177e4SLinus Torvalds pkt_dev->delay_us = value / 1000; 10061da177e4SLinus Torvalds pkt_dev->delay_ns = value % 1000; 10071da177e4SLinus Torvalds } 1008222f1806SLuiz Capitulino sprintf(pg_result, "OK: delay=%u", 1009222f1806SLuiz Capitulino 1000 * pkt_dev->delay_us + pkt_dev->delay_ns); 10101da177e4SLinus Torvalds return count; 10111da177e4SLinus Torvalds } 10121da177e4SLinus Torvalds if (!strcmp(name, "udp_src_min")) { 10131da177e4SLinus Torvalds len = num_arg(&user_buffer[i], 10, &value); 1014222f1806SLuiz Capitulino if (len < 0) { 1015222f1806SLuiz Capitulino return len; 1016222f1806SLuiz Capitulino } 10171da177e4SLinus Torvalds i += len; 10181da177e4SLinus Torvalds if (value != pkt_dev->udp_src_min) { 10191da177e4SLinus Torvalds pkt_dev->udp_src_min = value; 10201da177e4SLinus Torvalds pkt_dev->cur_udp_src = value; 10211da177e4SLinus Torvalds } 10221da177e4SLinus Torvalds sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min); 10231da177e4SLinus Torvalds return count; 10241da177e4SLinus Torvalds } 10251da177e4SLinus Torvalds if (!strcmp(name, "udp_dst_min")) { 10261da177e4SLinus Torvalds len = num_arg(&user_buffer[i], 10, &value); 1027222f1806SLuiz Capitulino if (len < 0) { 1028222f1806SLuiz Capitulino return len; 1029222f1806SLuiz Capitulino } 10301da177e4SLinus Torvalds i += len; 10311da177e4SLinus Torvalds if (value != pkt_dev->udp_dst_min) { 10321da177e4SLinus Torvalds pkt_dev->udp_dst_min = value; 10331da177e4SLinus Torvalds pkt_dev->cur_udp_dst = value; 10341da177e4SLinus Torvalds } 10351da177e4SLinus Torvalds sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min); 10361da177e4SLinus Torvalds return count; 10371da177e4SLinus Torvalds } 10381da177e4SLinus Torvalds if (!strcmp(name, "udp_src_max")) { 10391da177e4SLinus Torvalds len = num_arg(&user_buffer[i], 10, &value); 1040222f1806SLuiz Capitulino if (len < 0) { 1041222f1806SLuiz Capitulino return len; 1042222f1806SLuiz Capitulino } 10431da177e4SLinus Torvalds i += len; 10441da177e4SLinus Torvalds if (value != pkt_dev->udp_src_max) { 10451da177e4SLinus Torvalds pkt_dev->udp_src_max = value; 10461da177e4SLinus Torvalds pkt_dev->cur_udp_src = value; 10471da177e4SLinus Torvalds } 10481da177e4SLinus Torvalds sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max); 10491da177e4SLinus Torvalds return count; 10501da177e4SLinus Torvalds } 10511da177e4SLinus Torvalds if (!strcmp(name, "udp_dst_max")) { 10521da177e4SLinus Torvalds len = num_arg(&user_buffer[i], 10, &value); 1053222f1806SLuiz Capitulino if (len < 0) { 1054222f1806SLuiz Capitulino return len; 1055222f1806SLuiz Capitulino } 10561da177e4SLinus Torvalds i += len; 10571da177e4SLinus Torvalds if (value != pkt_dev->udp_dst_max) { 10581da177e4SLinus Torvalds pkt_dev->udp_dst_max = value; 10591da177e4SLinus Torvalds pkt_dev->cur_udp_dst = value; 10601da177e4SLinus Torvalds } 10611da177e4SLinus Torvalds sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max); 10621da177e4SLinus Torvalds return count; 10631da177e4SLinus Torvalds } 10641da177e4SLinus Torvalds if (!strcmp(name, "clone_skb")) { 10651da177e4SLinus Torvalds len = num_arg(&user_buffer[i], 10, &value); 1066222f1806SLuiz Capitulino if (len < 0) { 1067222f1806SLuiz Capitulino return len; 1068222f1806SLuiz Capitulino } 10691da177e4SLinus Torvalds i += len; 10701da177e4SLinus Torvalds pkt_dev->clone_skb = value; 10711da177e4SLinus Torvalds 10721da177e4SLinus Torvalds sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb); 10731da177e4SLinus Torvalds return count; 10741da177e4SLinus Torvalds } 10751da177e4SLinus Torvalds if (!strcmp(name, "count")) { 10761da177e4SLinus Torvalds len = num_arg(&user_buffer[i], 10, &value); 1077222f1806SLuiz Capitulino if (len < 0) { 1078222f1806SLuiz Capitulino return len; 1079222f1806SLuiz Capitulino } 10801da177e4SLinus Torvalds i += len; 10811da177e4SLinus Torvalds pkt_dev->count = value; 10821da177e4SLinus Torvalds sprintf(pg_result, "OK: count=%llu", 10831da177e4SLinus Torvalds (unsigned long long)pkt_dev->count); 10841da177e4SLinus Torvalds return count; 10851da177e4SLinus Torvalds } 10861da177e4SLinus Torvalds if (!strcmp(name, "src_mac_count")) { 10871da177e4SLinus Torvalds len = num_arg(&user_buffer[i], 10, &value); 1088222f1806SLuiz Capitulino if (len < 0) { 1089222f1806SLuiz Capitulino return len; 1090222f1806SLuiz Capitulino } 10911da177e4SLinus Torvalds i += len; 10921da177e4SLinus Torvalds if (pkt_dev->src_mac_count != value) { 10931da177e4SLinus Torvalds pkt_dev->src_mac_count = value; 10941da177e4SLinus Torvalds pkt_dev->cur_src_mac_offset = 0; 10951da177e4SLinus Torvalds } 1096222f1806SLuiz Capitulino sprintf(pg_result, "OK: src_mac_count=%d", 1097222f1806SLuiz Capitulino pkt_dev->src_mac_count); 10981da177e4SLinus Torvalds return count; 10991da177e4SLinus Torvalds } 11001da177e4SLinus Torvalds if (!strcmp(name, "dst_mac_count")) { 11011da177e4SLinus Torvalds len = num_arg(&user_buffer[i], 10, &value); 1102222f1806SLuiz Capitulino if (len < 0) { 1103222f1806SLuiz Capitulino return len; 1104222f1806SLuiz Capitulino } 11051da177e4SLinus Torvalds i += len; 11061da177e4SLinus Torvalds if (pkt_dev->dst_mac_count != value) { 11071da177e4SLinus Torvalds pkt_dev->dst_mac_count = value; 11081da177e4SLinus Torvalds pkt_dev->cur_dst_mac_offset = 0; 11091da177e4SLinus Torvalds } 1110222f1806SLuiz Capitulino sprintf(pg_result, "OK: dst_mac_count=%d", 1111222f1806SLuiz Capitulino pkt_dev->dst_mac_count); 11121da177e4SLinus Torvalds return count; 11131da177e4SLinus Torvalds } 11141da177e4SLinus Torvalds if (!strcmp(name, "flag")) { 11151da177e4SLinus Torvalds char f[32]; 11161da177e4SLinus Torvalds memset(f, 0, 32); 11171da177e4SLinus Torvalds len = strn_len(&user_buffer[i], sizeof(f) - 1); 1118222f1806SLuiz Capitulino if (len < 0) { 1119222f1806SLuiz Capitulino return len; 1120222f1806SLuiz Capitulino } 11211da177e4SLinus Torvalds if (copy_from_user(f, &user_buffer[i], len)) 11221da177e4SLinus Torvalds return -EFAULT; 11231da177e4SLinus Torvalds i += len; 11241da177e4SLinus Torvalds if (strcmp(f, "IPSRC_RND") == 0) 11251da177e4SLinus Torvalds pkt_dev->flags |= F_IPSRC_RND; 11261da177e4SLinus Torvalds 11271da177e4SLinus Torvalds else if (strcmp(f, "!IPSRC_RND") == 0) 11281da177e4SLinus Torvalds pkt_dev->flags &= ~F_IPSRC_RND; 11291da177e4SLinus Torvalds 11301da177e4SLinus Torvalds else if (strcmp(f, "TXSIZE_RND") == 0) 11311da177e4SLinus Torvalds pkt_dev->flags |= F_TXSIZE_RND; 11321da177e4SLinus Torvalds 11331da177e4SLinus Torvalds else if (strcmp(f, "!TXSIZE_RND") == 0) 11341da177e4SLinus Torvalds pkt_dev->flags &= ~F_TXSIZE_RND; 11351da177e4SLinus Torvalds 11361da177e4SLinus Torvalds else if (strcmp(f, "IPDST_RND") == 0) 11371da177e4SLinus Torvalds pkt_dev->flags |= F_IPDST_RND; 11381da177e4SLinus Torvalds 11391da177e4SLinus Torvalds else if (strcmp(f, "!IPDST_RND") == 0) 11401da177e4SLinus Torvalds pkt_dev->flags &= ~F_IPDST_RND; 11411da177e4SLinus Torvalds 11421da177e4SLinus Torvalds else if (strcmp(f, "UDPSRC_RND") == 0) 11431da177e4SLinus Torvalds pkt_dev->flags |= F_UDPSRC_RND; 11441da177e4SLinus Torvalds 11451da177e4SLinus Torvalds else if (strcmp(f, "!UDPSRC_RND") == 0) 11461da177e4SLinus Torvalds pkt_dev->flags &= ~F_UDPSRC_RND; 11471da177e4SLinus Torvalds 11481da177e4SLinus Torvalds else if (strcmp(f, "UDPDST_RND") == 0) 11491da177e4SLinus Torvalds pkt_dev->flags |= F_UDPDST_RND; 11501da177e4SLinus Torvalds 11511da177e4SLinus Torvalds else if (strcmp(f, "!UDPDST_RND") == 0) 11521da177e4SLinus Torvalds pkt_dev->flags &= ~F_UDPDST_RND; 11531da177e4SLinus Torvalds 11541da177e4SLinus Torvalds else if (strcmp(f, "MACSRC_RND") == 0) 11551da177e4SLinus Torvalds pkt_dev->flags |= F_MACSRC_RND; 11561da177e4SLinus Torvalds 11571da177e4SLinus Torvalds else if (strcmp(f, "!MACSRC_RND") == 0) 11581da177e4SLinus Torvalds pkt_dev->flags &= ~F_MACSRC_RND; 11591da177e4SLinus Torvalds 11601da177e4SLinus Torvalds else if (strcmp(f, "MACDST_RND") == 0) 11611da177e4SLinus Torvalds pkt_dev->flags |= F_MACDST_RND; 11621da177e4SLinus Torvalds 11631da177e4SLinus Torvalds else if (strcmp(f, "!MACDST_RND") == 0) 11641da177e4SLinus Torvalds pkt_dev->flags &= ~F_MACDST_RND; 11651da177e4SLinus Torvalds 1166ca6549afSSteven Whitehouse else if (strcmp(f, "MPLS_RND") == 0) 1167ca6549afSSteven Whitehouse pkt_dev->flags |= F_MPLS_RND; 1168ca6549afSSteven Whitehouse 1169ca6549afSSteven Whitehouse else if (strcmp(f, "!MPLS_RND") == 0) 1170ca6549afSSteven Whitehouse pkt_dev->flags &= ~F_MPLS_RND; 1171ca6549afSSteven Whitehouse 117234954ddcSFrancesco Fondelli else if (strcmp(f, "VID_RND") == 0) 117334954ddcSFrancesco Fondelli pkt_dev->flags |= F_VID_RND; 117434954ddcSFrancesco Fondelli 117534954ddcSFrancesco Fondelli else if (strcmp(f, "!VID_RND") == 0) 117634954ddcSFrancesco Fondelli pkt_dev->flags &= ~F_VID_RND; 117734954ddcSFrancesco Fondelli 117834954ddcSFrancesco Fondelli else if (strcmp(f, "SVID_RND") == 0) 117934954ddcSFrancesco Fondelli pkt_dev->flags |= F_SVID_RND; 118034954ddcSFrancesco Fondelli 118134954ddcSFrancesco Fondelli else if (strcmp(f, "!SVID_RND") == 0) 118234954ddcSFrancesco Fondelli pkt_dev->flags &= ~F_SVID_RND; 118334954ddcSFrancesco Fondelli 11841ca7768cSFrancesco Fondelli else if (strcmp(f, "!IPV6") == 0) 11851ca7768cSFrancesco Fondelli pkt_dev->flags &= ~F_IPV6; 11861ca7768cSFrancesco Fondelli 11871da177e4SLinus Torvalds else { 1188222f1806SLuiz Capitulino sprintf(pg_result, 1189222f1806SLuiz Capitulino "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s", 11901da177e4SLinus Torvalds f, 11911ca7768cSFrancesco Fondelli "IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, " 11921ca7768cSFrancesco Fondelli "MACSRC_RND, MACDST_RND, TXSIZE_RND, IPV6, MPLS_RND, VID_RND, SVID_RND\n"); 11931da177e4SLinus Torvalds return count; 11941da177e4SLinus Torvalds } 11951da177e4SLinus Torvalds sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags); 11961da177e4SLinus Torvalds return count; 11971da177e4SLinus Torvalds } 11981da177e4SLinus Torvalds if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) { 11991da177e4SLinus Torvalds len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1); 1200222f1806SLuiz Capitulino if (len < 0) { 1201222f1806SLuiz Capitulino return len; 1202222f1806SLuiz Capitulino } 12031da177e4SLinus Torvalds 12041da177e4SLinus Torvalds if (copy_from_user(buf, &user_buffer[i], len)) 12051da177e4SLinus Torvalds return -EFAULT; 12061da177e4SLinus Torvalds buf[len] = 0; 12071da177e4SLinus Torvalds if (strcmp(buf, pkt_dev->dst_min) != 0) { 12081da177e4SLinus Torvalds memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min)); 12091da177e4SLinus Torvalds strncpy(pkt_dev->dst_min, buf, len); 12101da177e4SLinus Torvalds pkt_dev->daddr_min = in_aton(pkt_dev->dst_min); 12111da177e4SLinus Torvalds pkt_dev->cur_daddr = pkt_dev->daddr_min; 12121da177e4SLinus Torvalds } 12131da177e4SLinus Torvalds if (debug) 1214222f1806SLuiz Capitulino printk("pktgen: dst_min set to: %s\n", 1215222f1806SLuiz Capitulino pkt_dev->dst_min); 12161da177e4SLinus Torvalds i += len; 12171da177e4SLinus Torvalds sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min); 12181da177e4SLinus Torvalds return count; 12191da177e4SLinus Torvalds } 12201da177e4SLinus Torvalds if (!strcmp(name, "dst_max")) { 12211da177e4SLinus Torvalds len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1); 1222222f1806SLuiz Capitulino if (len < 0) { 1223222f1806SLuiz Capitulino return len; 1224222f1806SLuiz Capitulino } 12251da177e4SLinus Torvalds 12261da177e4SLinus Torvalds if (copy_from_user(buf, &user_buffer[i], len)) 12271da177e4SLinus Torvalds return -EFAULT; 12281da177e4SLinus Torvalds 12291da177e4SLinus Torvalds buf[len] = 0; 12301da177e4SLinus Torvalds if (strcmp(buf, pkt_dev->dst_max) != 0) { 12311da177e4SLinus Torvalds memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max)); 12321da177e4SLinus Torvalds strncpy(pkt_dev->dst_max, buf, len); 12331da177e4SLinus Torvalds pkt_dev->daddr_max = in_aton(pkt_dev->dst_max); 12341da177e4SLinus Torvalds pkt_dev->cur_daddr = pkt_dev->daddr_max; 12351da177e4SLinus Torvalds } 12361da177e4SLinus Torvalds if (debug) 1237222f1806SLuiz Capitulino printk("pktgen: dst_max set to: %s\n", 1238222f1806SLuiz Capitulino pkt_dev->dst_max); 12391da177e4SLinus Torvalds i += len; 12401da177e4SLinus Torvalds sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max); 12411da177e4SLinus Torvalds return count; 12421da177e4SLinus Torvalds } 12431da177e4SLinus Torvalds if (!strcmp(name, "dst6")) { 12441da177e4SLinus Torvalds len = strn_len(&user_buffer[i], sizeof(buf) - 1); 1245222f1806SLuiz Capitulino if (len < 0) 1246222f1806SLuiz Capitulino return len; 12471da177e4SLinus Torvalds 12481da177e4SLinus Torvalds pkt_dev->flags |= F_IPV6; 12491da177e4SLinus Torvalds 12501da177e4SLinus Torvalds if (copy_from_user(buf, &user_buffer[i], len)) 12511da177e4SLinus Torvalds return -EFAULT; 12521da177e4SLinus Torvalds buf[len] = 0; 12531da177e4SLinus Torvalds 12541da177e4SLinus Torvalds scan_ip6(buf, pkt_dev->in6_daddr.s6_addr); 12551da177e4SLinus Torvalds fmt_ip6(buf, pkt_dev->in6_daddr.s6_addr); 12561da177e4SLinus Torvalds 12571da177e4SLinus Torvalds ipv6_addr_copy(&pkt_dev->cur_in6_daddr, &pkt_dev->in6_daddr); 12581da177e4SLinus Torvalds 12591da177e4SLinus Torvalds if (debug) 12601da177e4SLinus Torvalds printk("pktgen: dst6 set to: %s\n", buf); 12611da177e4SLinus Torvalds 12621da177e4SLinus Torvalds i += len; 12631da177e4SLinus Torvalds sprintf(pg_result, "OK: dst6=%s", buf); 12641da177e4SLinus Torvalds return count; 12651da177e4SLinus Torvalds } 12661da177e4SLinus Torvalds if (!strcmp(name, "dst6_min")) { 12671da177e4SLinus Torvalds len = strn_len(&user_buffer[i], sizeof(buf) - 1); 1268222f1806SLuiz Capitulino if (len < 0) 1269222f1806SLuiz Capitulino return len; 12701da177e4SLinus Torvalds 12711da177e4SLinus Torvalds pkt_dev->flags |= F_IPV6; 12721da177e4SLinus Torvalds 12731da177e4SLinus Torvalds if (copy_from_user(buf, &user_buffer[i], len)) 12741da177e4SLinus Torvalds return -EFAULT; 12751da177e4SLinus Torvalds buf[len] = 0; 12761da177e4SLinus Torvalds 12771da177e4SLinus Torvalds scan_ip6(buf, pkt_dev->min_in6_daddr.s6_addr); 12781da177e4SLinus Torvalds fmt_ip6(buf, pkt_dev->min_in6_daddr.s6_addr); 12791da177e4SLinus Torvalds 1280222f1806SLuiz Capitulino ipv6_addr_copy(&pkt_dev->cur_in6_daddr, 1281222f1806SLuiz Capitulino &pkt_dev->min_in6_daddr); 12821da177e4SLinus Torvalds if (debug) 12831da177e4SLinus Torvalds printk("pktgen: dst6_min set to: %s\n", buf); 12841da177e4SLinus Torvalds 12851da177e4SLinus Torvalds i += len; 12861da177e4SLinus Torvalds sprintf(pg_result, "OK: dst6_min=%s", buf); 12871da177e4SLinus Torvalds return count; 12881da177e4SLinus Torvalds } 12891da177e4SLinus Torvalds if (!strcmp(name, "dst6_max")) { 12901da177e4SLinus Torvalds len = strn_len(&user_buffer[i], sizeof(buf) - 1); 1291222f1806SLuiz Capitulino if (len < 0) 1292222f1806SLuiz Capitulino return len; 12931da177e4SLinus Torvalds 12941da177e4SLinus Torvalds pkt_dev->flags |= F_IPV6; 12951da177e4SLinus Torvalds 12961da177e4SLinus Torvalds if (copy_from_user(buf, &user_buffer[i], len)) 12971da177e4SLinus Torvalds return -EFAULT; 12981da177e4SLinus Torvalds buf[len] = 0; 12991da177e4SLinus Torvalds 13001da177e4SLinus Torvalds scan_ip6(buf, pkt_dev->max_in6_daddr.s6_addr); 13011da177e4SLinus Torvalds fmt_ip6(buf, pkt_dev->max_in6_daddr.s6_addr); 13021da177e4SLinus Torvalds 13031da177e4SLinus Torvalds if (debug) 13041da177e4SLinus Torvalds printk("pktgen: dst6_max set to: %s\n", buf); 13051da177e4SLinus Torvalds 13061da177e4SLinus Torvalds i += len; 13071da177e4SLinus Torvalds sprintf(pg_result, "OK: dst6_max=%s", buf); 13081da177e4SLinus Torvalds return count; 13091da177e4SLinus Torvalds } 13101da177e4SLinus Torvalds if (!strcmp(name, "src6")) { 13111da177e4SLinus Torvalds len = strn_len(&user_buffer[i], sizeof(buf) - 1); 1312222f1806SLuiz Capitulino if (len < 0) 1313222f1806SLuiz Capitulino return len; 13141da177e4SLinus Torvalds 13151da177e4SLinus Torvalds pkt_dev->flags |= F_IPV6; 13161da177e4SLinus Torvalds 13171da177e4SLinus Torvalds if (copy_from_user(buf, &user_buffer[i], len)) 13181da177e4SLinus Torvalds return -EFAULT; 13191da177e4SLinus Torvalds buf[len] = 0; 13201da177e4SLinus Torvalds 13211da177e4SLinus Torvalds scan_ip6(buf, pkt_dev->in6_saddr.s6_addr); 13221da177e4SLinus Torvalds fmt_ip6(buf, pkt_dev->in6_saddr.s6_addr); 13231da177e4SLinus Torvalds 13241da177e4SLinus Torvalds ipv6_addr_copy(&pkt_dev->cur_in6_saddr, &pkt_dev->in6_saddr); 13251da177e4SLinus Torvalds 13261da177e4SLinus Torvalds if (debug) 13271da177e4SLinus Torvalds printk("pktgen: src6 set to: %s\n", buf); 13281da177e4SLinus Torvalds 13291da177e4SLinus Torvalds i += len; 13301da177e4SLinus Torvalds sprintf(pg_result, "OK: src6=%s", buf); 13311da177e4SLinus Torvalds return count; 13321da177e4SLinus Torvalds } 13331da177e4SLinus Torvalds if (!strcmp(name, "src_min")) { 13341da177e4SLinus Torvalds len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1); 1335222f1806SLuiz Capitulino if (len < 0) { 1336222f1806SLuiz Capitulino return len; 1337222f1806SLuiz Capitulino } 13381da177e4SLinus Torvalds if (copy_from_user(buf, &user_buffer[i], len)) 13391da177e4SLinus Torvalds return -EFAULT; 13401da177e4SLinus Torvalds buf[len] = 0; 13411da177e4SLinus Torvalds if (strcmp(buf, pkt_dev->src_min) != 0) { 13421da177e4SLinus Torvalds memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min)); 13431da177e4SLinus Torvalds strncpy(pkt_dev->src_min, buf, len); 13441da177e4SLinus Torvalds pkt_dev->saddr_min = in_aton(pkt_dev->src_min); 13451da177e4SLinus Torvalds pkt_dev->cur_saddr = pkt_dev->saddr_min; 13461da177e4SLinus Torvalds } 13471da177e4SLinus Torvalds if (debug) 1348222f1806SLuiz Capitulino printk("pktgen: src_min set to: %s\n", 1349222f1806SLuiz Capitulino pkt_dev->src_min); 13501da177e4SLinus Torvalds i += len; 13511da177e4SLinus Torvalds sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min); 13521da177e4SLinus Torvalds return count; 13531da177e4SLinus Torvalds } 13541da177e4SLinus Torvalds if (!strcmp(name, "src_max")) { 13551da177e4SLinus Torvalds len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1); 1356222f1806SLuiz Capitulino if (len < 0) { 1357222f1806SLuiz Capitulino return len; 1358222f1806SLuiz Capitulino } 13591da177e4SLinus Torvalds if (copy_from_user(buf, &user_buffer[i], len)) 13601da177e4SLinus Torvalds return -EFAULT; 13611da177e4SLinus Torvalds buf[len] = 0; 13621da177e4SLinus Torvalds if (strcmp(buf, pkt_dev->src_max) != 0) { 13631da177e4SLinus Torvalds memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max)); 13641da177e4SLinus Torvalds strncpy(pkt_dev->src_max, buf, len); 13651da177e4SLinus Torvalds pkt_dev->saddr_max = in_aton(pkt_dev->src_max); 13661da177e4SLinus Torvalds pkt_dev->cur_saddr = pkt_dev->saddr_max; 13671da177e4SLinus Torvalds } 13681da177e4SLinus Torvalds if (debug) 1369222f1806SLuiz Capitulino printk("pktgen: src_max set to: %s\n", 1370222f1806SLuiz Capitulino pkt_dev->src_max); 13711da177e4SLinus Torvalds i += len; 13721da177e4SLinus Torvalds sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max); 13731da177e4SLinus Torvalds return count; 13741da177e4SLinus Torvalds } 13751da177e4SLinus Torvalds if (!strcmp(name, "dst_mac")) { 13761da177e4SLinus Torvalds char *v = valstr; 1377f404e9a6SKris Katterjohn unsigned char old_dmac[ETH_ALEN]; 13781da177e4SLinus Torvalds unsigned char *m = pkt_dev->dst_mac; 1379f404e9a6SKris Katterjohn memcpy(old_dmac, pkt_dev->dst_mac, ETH_ALEN); 13801da177e4SLinus Torvalds 13811da177e4SLinus Torvalds len = strn_len(&user_buffer[i], sizeof(valstr) - 1); 1382222f1806SLuiz Capitulino if (len < 0) { 1383222f1806SLuiz Capitulino return len; 1384222f1806SLuiz Capitulino } 13851da177e4SLinus Torvalds memset(valstr, 0, sizeof(valstr)); 13861da177e4SLinus Torvalds if (copy_from_user(valstr, &user_buffer[i], len)) 13871da177e4SLinus Torvalds return -EFAULT; 13881da177e4SLinus Torvalds i += len; 13891da177e4SLinus Torvalds 13901da177e4SLinus Torvalds for (*m = 0; *v && m < pkt_dev->dst_mac + 6; v++) { 13911da177e4SLinus Torvalds if (*v >= '0' && *v <= '9') { 13921da177e4SLinus Torvalds *m *= 16; 13931da177e4SLinus Torvalds *m += *v - '0'; 13941da177e4SLinus Torvalds } 13951da177e4SLinus Torvalds if (*v >= 'A' && *v <= 'F') { 13961da177e4SLinus Torvalds *m *= 16; 13971da177e4SLinus Torvalds *m += *v - 'A' + 10; 13981da177e4SLinus Torvalds } 13991da177e4SLinus Torvalds if (*v >= 'a' && *v <= 'f') { 14001da177e4SLinus Torvalds *m *= 16; 14011da177e4SLinus Torvalds *m += *v - 'a' + 10; 14021da177e4SLinus Torvalds } 14031da177e4SLinus Torvalds if (*v == ':') { 14041da177e4SLinus Torvalds m++; 14051da177e4SLinus Torvalds *m = 0; 14061da177e4SLinus Torvalds } 14071da177e4SLinus Torvalds } 14081da177e4SLinus Torvalds 14091da177e4SLinus Torvalds /* Set up Dest MAC */ 1410f404e9a6SKris Katterjohn if (compare_ether_addr(old_dmac, pkt_dev->dst_mac)) 1411f404e9a6SKris Katterjohn memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN); 14121da177e4SLinus Torvalds 14131da177e4SLinus Torvalds sprintf(pg_result, "OK: dstmac"); 14141da177e4SLinus Torvalds return count; 14151da177e4SLinus Torvalds } 14161da177e4SLinus Torvalds if (!strcmp(name, "src_mac")) { 14171da177e4SLinus Torvalds char *v = valstr; 14181da177e4SLinus Torvalds unsigned char *m = pkt_dev->src_mac; 14191da177e4SLinus Torvalds 14201da177e4SLinus Torvalds len = strn_len(&user_buffer[i], sizeof(valstr) - 1); 1421222f1806SLuiz Capitulino if (len < 0) { 1422222f1806SLuiz Capitulino return len; 1423222f1806SLuiz Capitulino } 14241da177e4SLinus Torvalds memset(valstr, 0, sizeof(valstr)); 14251da177e4SLinus Torvalds if (copy_from_user(valstr, &user_buffer[i], len)) 14261da177e4SLinus Torvalds return -EFAULT; 14271da177e4SLinus Torvalds i += len; 14281da177e4SLinus Torvalds 14291da177e4SLinus Torvalds for (*m = 0; *v && m < pkt_dev->src_mac + 6; v++) { 14301da177e4SLinus Torvalds if (*v >= '0' && *v <= '9') { 14311da177e4SLinus Torvalds *m *= 16; 14321da177e4SLinus Torvalds *m += *v - '0'; 14331da177e4SLinus Torvalds } 14341da177e4SLinus Torvalds if (*v >= 'A' && *v <= 'F') { 14351da177e4SLinus Torvalds *m *= 16; 14361da177e4SLinus Torvalds *m += *v - 'A' + 10; 14371da177e4SLinus Torvalds } 14381da177e4SLinus Torvalds if (*v >= 'a' && *v <= 'f') { 14391da177e4SLinus Torvalds *m *= 16; 14401da177e4SLinus Torvalds *m += *v - 'a' + 10; 14411da177e4SLinus Torvalds } 14421da177e4SLinus Torvalds if (*v == ':') { 14431da177e4SLinus Torvalds m++; 14441da177e4SLinus Torvalds *m = 0; 14451da177e4SLinus Torvalds } 14461da177e4SLinus Torvalds } 14471da177e4SLinus Torvalds 14481da177e4SLinus Torvalds sprintf(pg_result, "OK: srcmac"); 14491da177e4SLinus Torvalds return count; 14501da177e4SLinus Torvalds } 14511da177e4SLinus Torvalds 14521da177e4SLinus Torvalds if (!strcmp(name, "clear_counters")) { 14531da177e4SLinus Torvalds pktgen_clear_counters(pkt_dev); 14541da177e4SLinus Torvalds sprintf(pg_result, "OK: Clearing counters.\n"); 14551da177e4SLinus Torvalds return count; 14561da177e4SLinus Torvalds } 14571da177e4SLinus Torvalds 14581da177e4SLinus Torvalds if (!strcmp(name, "flows")) { 14591da177e4SLinus Torvalds len = num_arg(&user_buffer[i], 10, &value); 1460222f1806SLuiz Capitulino if (len < 0) { 1461222f1806SLuiz Capitulino return len; 1462222f1806SLuiz Capitulino } 14631da177e4SLinus Torvalds i += len; 14641da177e4SLinus Torvalds if (value > MAX_CFLOWS) 14651da177e4SLinus Torvalds value = MAX_CFLOWS; 14661da177e4SLinus Torvalds 14671da177e4SLinus Torvalds pkt_dev->cflows = value; 14681da177e4SLinus Torvalds sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows); 14691da177e4SLinus Torvalds return count; 14701da177e4SLinus Torvalds } 14711da177e4SLinus Torvalds 14721da177e4SLinus Torvalds if (!strcmp(name, "flowlen")) { 14731da177e4SLinus Torvalds len = num_arg(&user_buffer[i], 10, &value); 1474222f1806SLuiz Capitulino if (len < 0) { 1475222f1806SLuiz Capitulino return len; 1476222f1806SLuiz Capitulino } 14771da177e4SLinus Torvalds i += len; 14781da177e4SLinus Torvalds pkt_dev->lflow = value; 14791da177e4SLinus Torvalds sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow); 14801da177e4SLinus Torvalds return count; 14811da177e4SLinus Torvalds } 14821da177e4SLinus Torvalds 1483ca6549afSSteven Whitehouse if (!strcmp(name, "mpls")) { 1484ca6549afSSteven Whitehouse unsigned n, offset; 1485ca6549afSSteven Whitehouse len = get_labels(&user_buffer[i], pkt_dev); 1486ca6549afSSteven Whitehouse if (len < 0) { return len; } 1487ca6549afSSteven Whitehouse i += len; 1488ca6549afSSteven Whitehouse offset = sprintf(pg_result, "OK: mpls="); 1489ca6549afSSteven Whitehouse for (n = 0; n < pkt_dev->nr_labels; n++) 1490ca6549afSSteven Whitehouse offset += sprintf(pg_result + offset, 1491ca6549afSSteven Whitehouse "%08x%s", ntohl(pkt_dev->labels[n]), 1492ca6549afSSteven Whitehouse n == pkt_dev->nr_labels-1 ? "" : ","); 149334954ddcSFrancesco Fondelli 149434954ddcSFrancesco Fondelli if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) { 149534954ddcSFrancesco Fondelli pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */ 149634954ddcSFrancesco Fondelli pkt_dev->svlan_id = 0xffff; 149734954ddcSFrancesco Fondelli 149834954ddcSFrancesco Fondelli if (debug) 149934954ddcSFrancesco Fondelli printk("pktgen: VLAN/SVLAN auto turned off\n"); 150034954ddcSFrancesco Fondelli } 150134954ddcSFrancesco Fondelli return count; 150234954ddcSFrancesco Fondelli } 150334954ddcSFrancesco Fondelli 150434954ddcSFrancesco Fondelli if (!strcmp(name, "vlan_id")) { 150534954ddcSFrancesco Fondelli len = num_arg(&user_buffer[i], 4, &value); 150634954ddcSFrancesco Fondelli if (len < 0) { 150734954ddcSFrancesco Fondelli return len; 150834954ddcSFrancesco Fondelli } 150934954ddcSFrancesco Fondelli i += len; 151034954ddcSFrancesco Fondelli if (value <= 4095) { 151134954ddcSFrancesco Fondelli pkt_dev->vlan_id = value; /* turn on VLAN */ 151234954ddcSFrancesco Fondelli 151334954ddcSFrancesco Fondelli if (debug) 151434954ddcSFrancesco Fondelli printk("pktgen: VLAN turned on\n"); 151534954ddcSFrancesco Fondelli 151634954ddcSFrancesco Fondelli if (debug && pkt_dev->nr_labels) 151734954ddcSFrancesco Fondelli printk("pktgen: MPLS auto turned off\n"); 151834954ddcSFrancesco Fondelli 151934954ddcSFrancesco Fondelli pkt_dev->nr_labels = 0; /* turn off MPLS */ 152034954ddcSFrancesco Fondelli sprintf(pg_result, "OK: vlan_id=%u", pkt_dev->vlan_id); 152134954ddcSFrancesco Fondelli } else { 152234954ddcSFrancesco Fondelli pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */ 152334954ddcSFrancesco Fondelli pkt_dev->svlan_id = 0xffff; 152434954ddcSFrancesco Fondelli 152534954ddcSFrancesco Fondelli if (debug) 152634954ddcSFrancesco Fondelli printk("pktgen: VLAN/SVLAN turned off\n"); 152734954ddcSFrancesco Fondelli } 152834954ddcSFrancesco Fondelli return count; 152934954ddcSFrancesco Fondelli } 153034954ddcSFrancesco Fondelli 153134954ddcSFrancesco Fondelli if (!strcmp(name, "vlan_p")) { 153234954ddcSFrancesco Fondelli len = num_arg(&user_buffer[i], 1, &value); 153334954ddcSFrancesco Fondelli if (len < 0) { 153434954ddcSFrancesco Fondelli return len; 153534954ddcSFrancesco Fondelli } 153634954ddcSFrancesco Fondelli i += len; 153734954ddcSFrancesco Fondelli if ((value <= 7) && (pkt_dev->vlan_id != 0xffff)) { 153834954ddcSFrancesco Fondelli pkt_dev->vlan_p = value; 153934954ddcSFrancesco Fondelli sprintf(pg_result, "OK: vlan_p=%u", pkt_dev->vlan_p); 154034954ddcSFrancesco Fondelli } else { 154134954ddcSFrancesco Fondelli sprintf(pg_result, "ERROR: vlan_p must be 0-7"); 154234954ddcSFrancesco Fondelli } 154334954ddcSFrancesco Fondelli return count; 154434954ddcSFrancesco Fondelli } 154534954ddcSFrancesco Fondelli 154634954ddcSFrancesco Fondelli if (!strcmp(name, "vlan_cfi")) { 154734954ddcSFrancesco Fondelli len = num_arg(&user_buffer[i], 1, &value); 154834954ddcSFrancesco Fondelli if (len < 0) { 154934954ddcSFrancesco Fondelli return len; 155034954ddcSFrancesco Fondelli } 155134954ddcSFrancesco Fondelli i += len; 155234954ddcSFrancesco Fondelli if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) { 155334954ddcSFrancesco Fondelli pkt_dev->vlan_cfi = value; 155434954ddcSFrancesco Fondelli sprintf(pg_result, "OK: vlan_cfi=%u", pkt_dev->vlan_cfi); 155534954ddcSFrancesco Fondelli } else { 155634954ddcSFrancesco Fondelli sprintf(pg_result, "ERROR: vlan_cfi must be 0-1"); 155734954ddcSFrancesco Fondelli } 155834954ddcSFrancesco Fondelli return count; 155934954ddcSFrancesco Fondelli } 156034954ddcSFrancesco Fondelli 156134954ddcSFrancesco Fondelli if (!strcmp(name, "svlan_id")) { 156234954ddcSFrancesco Fondelli len = num_arg(&user_buffer[i], 4, &value); 156334954ddcSFrancesco Fondelli if (len < 0) { 156434954ddcSFrancesco Fondelli return len; 156534954ddcSFrancesco Fondelli } 156634954ddcSFrancesco Fondelli i += len; 156734954ddcSFrancesco Fondelli if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) { 156834954ddcSFrancesco Fondelli pkt_dev->svlan_id = value; /* turn on SVLAN */ 156934954ddcSFrancesco Fondelli 157034954ddcSFrancesco Fondelli if (debug) 157134954ddcSFrancesco Fondelli printk("pktgen: SVLAN turned on\n"); 157234954ddcSFrancesco Fondelli 157334954ddcSFrancesco Fondelli if (debug && pkt_dev->nr_labels) 157434954ddcSFrancesco Fondelli printk("pktgen: MPLS auto turned off\n"); 157534954ddcSFrancesco Fondelli 157634954ddcSFrancesco Fondelli pkt_dev->nr_labels = 0; /* turn off MPLS */ 157734954ddcSFrancesco Fondelli sprintf(pg_result, "OK: svlan_id=%u", pkt_dev->svlan_id); 157834954ddcSFrancesco Fondelli } else { 157934954ddcSFrancesco Fondelli pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */ 158034954ddcSFrancesco Fondelli pkt_dev->svlan_id = 0xffff; 158134954ddcSFrancesco Fondelli 158234954ddcSFrancesco Fondelli if (debug) 158334954ddcSFrancesco Fondelli printk("pktgen: VLAN/SVLAN turned off\n"); 158434954ddcSFrancesco Fondelli } 158534954ddcSFrancesco Fondelli return count; 158634954ddcSFrancesco Fondelli } 158734954ddcSFrancesco Fondelli 158834954ddcSFrancesco Fondelli if (!strcmp(name, "svlan_p")) { 158934954ddcSFrancesco Fondelli len = num_arg(&user_buffer[i], 1, &value); 159034954ddcSFrancesco Fondelli if (len < 0) { 159134954ddcSFrancesco Fondelli return len; 159234954ddcSFrancesco Fondelli } 159334954ddcSFrancesco Fondelli i += len; 159434954ddcSFrancesco Fondelli if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) { 159534954ddcSFrancesco Fondelli pkt_dev->svlan_p = value; 159634954ddcSFrancesco Fondelli sprintf(pg_result, "OK: svlan_p=%u", pkt_dev->svlan_p); 159734954ddcSFrancesco Fondelli } else { 159834954ddcSFrancesco Fondelli sprintf(pg_result, "ERROR: svlan_p must be 0-7"); 159934954ddcSFrancesco Fondelli } 160034954ddcSFrancesco Fondelli return count; 160134954ddcSFrancesco Fondelli } 160234954ddcSFrancesco Fondelli 160334954ddcSFrancesco Fondelli if (!strcmp(name, "svlan_cfi")) { 160434954ddcSFrancesco Fondelli len = num_arg(&user_buffer[i], 1, &value); 160534954ddcSFrancesco Fondelli if (len < 0) { 160634954ddcSFrancesco Fondelli return len; 160734954ddcSFrancesco Fondelli } 160834954ddcSFrancesco Fondelli i += len; 160934954ddcSFrancesco Fondelli if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) { 161034954ddcSFrancesco Fondelli pkt_dev->svlan_cfi = value; 161134954ddcSFrancesco Fondelli sprintf(pg_result, "OK: svlan_cfi=%u", pkt_dev->svlan_cfi); 161234954ddcSFrancesco Fondelli } else { 161334954ddcSFrancesco Fondelli sprintf(pg_result, "ERROR: svlan_cfi must be 0-1"); 161434954ddcSFrancesco Fondelli } 1615ca6549afSSteven Whitehouse return count; 1616ca6549afSSteven Whitehouse } 1617ca6549afSSteven Whitehouse 16181ca7768cSFrancesco Fondelli if (!strcmp(name, "tos")) { 16191ca7768cSFrancesco Fondelli __u32 tmp_value = 0; 16201ca7768cSFrancesco Fondelli len = hex32_arg(&user_buffer[i], 2, &tmp_value); 16211ca7768cSFrancesco Fondelli if (len < 0) { 16221ca7768cSFrancesco Fondelli return len; 16231ca7768cSFrancesco Fondelli } 16241ca7768cSFrancesco Fondelli i += len; 16251ca7768cSFrancesco Fondelli if (len == 2) { 16261ca7768cSFrancesco Fondelli pkt_dev->tos = tmp_value; 16271ca7768cSFrancesco Fondelli sprintf(pg_result, "OK: tos=0x%02x", pkt_dev->tos); 16281ca7768cSFrancesco Fondelli } else { 16291ca7768cSFrancesco Fondelli sprintf(pg_result, "ERROR: tos must be 00-ff"); 16301ca7768cSFrancesco Fondelli } 16311ca7768cSFrancesco Fondelli return count; 16321ca7768cSFrancesco Fondelli } 16331ca7768cSFrancesco Fondelli 16341ca7768cSFrancesco Fondelli if (!strcmp(name, "traffic_class")) { 16351ca7768cSFrancesco Fondelli __u32 tmp_value = 0; 16361ca7768cSFrancesco Fondelli len = hex32_arg(&user_buffer[i], 2, &tmp_value); 16371ca7768cSFrancesco Fondelli if (len < 0) { 16381ca7768cSFrancesco Fondelli return len; 16391ca7768cSFrancesco Fondelli } 16401ca7768cSFrancesco Fondelli i += len; 16411ca7768cSFrancesco Fondelli if (len == 2) { 16421ca7768cSFrancesco Fondelli pkt_dev->traffic_class = tmp_value; 16431ca7768cSFrancesco Fondelli sprintf(pg_result, "OK: traffic_class=0x%02x", pkt_dev->traffic_class); 16441ca7768cSFrancesco Fondelli } else { 16451ca7768cSFrancesco Fondelli sprintf(pg_result, "ERROR: traffic_class must be 00-ff"); 16461ca7768cSFrancesco Fondelli } 16471ca7768cSFrancesco Fondelli return count; 16481ca7768cSFrancesco Fondelli } 16491ca7768cSFrancesco Fondelli 16501da177e4SLinus Torvalds sprintf(pkt_dev->result, "No such parameter \"%s\"", name); 16511da177e4SLinus Torvalds return -EINVAL; 16521da177e4SLinus Torvalds } 16531da177e4SLinus Torvalds 1654d50a6b56SStephen Hemminger static int pktgen_if_open(struct inode *inode, struct file *file) 16551da177e4SLinus Torvalds { 1656d50a6b56SStephen Hemminger return single_open(file, pktgen_if_show, PDE(inode)->data); 16571da177e4SLinus Torvalds } 16581da177e4SLinus Torvalds 16599a32144eSArjan van de Ven static const struct file_operations pktgen_if_fops = { 1660d50a6b56SStephen Hemminger .owner = THIS_MODULE, 1661d50a6b56SStephen Hemminger .open = pktgen_if_open, 1662d50a6b56SStephen Hemminger .read = seq_read, 1663d50a6b56SStephen Hemminger .llseek = seq_lseek, 1664d50a6b56SStephen Hemminger .write = pktgen_if_write, 1665d50a6b56SStephen Hemminger .release = single_release, 1666d50a6b56SStephen Hemminger }; 1667d50a6b56SStephen Hemminger 1668d50a6b56SStephen Hemminger static int pktgen_thread_show(struct seq_file *seq, void *v) 1669d50a6b56SStephen Hemminger { 1670d50a6b56SStephen Hemminger struct pktgen_thread *t = seq->private; 1671c26a8016SLuiz Capitulino struct pktgen_dev *pkt_dev; 1672d50a6b56SStephen Hemminger 1673d50a6b56SStephen Hemminger BUG_ON(!t); 1674d50a6b56SStephen Hemminger 1675d50a6b56SStephen Hemminger seq_printf(seq, "Name: %s max_before_softirq: %d\n", 1676ee74baa7SDavid S. Miller t->tsk->comm, t->max_before_softirq); 16771da177e4SLinus Torvalds 1678d50a6b56SStephen Hemminger seq_printf(seq, "Running: "); 16791da177e4SLinus Torvalds 16801da177e4SLinus Torvalds if_lock(t); 1681c26a8016SLuiz Capitulino list_for_each_entry(pkt_dev, &t->if_list, list) 16821da177e4SLinus Torvalds if (pkt_dev->running) 168339df232fSStephen Hemminger seq_printf(seq, "%s ", pkt_dev->odev->name); 16841da177e4SLinus Torvalds 1685d50a6b56SStephen Hemminger seq_printf(seq, "\nStopped: "); 16861da177e4SLinus Torvalds 1687c26a8016SLuiz Capitulino list_for_each_entry(pkt_dev, &t->if_list, list) 16881da177e4SLinus Torvalds if (!pkt_dev->running) 168939df232fSStephen Hemminger seq_printf(seq, "%s ", pkt_dev->odev->name); 16901da177e4SLinus Torvalds 16911da177e4SLinus Torvalds if (t->result[0]) 1692d50a6b56SStephen Hemminger seq_printf(seq, "\nResult: %s\n", t->result); 16931da177e4SLinus Torvalds else 1694d50a6b56SStephen Hemminger seq_printf(seq, "\nResult: NA\n"); 16951da177e4SLinus Torvalds 16961da177e4SLinus Torvalds if_unlock(t); 16971da177e4SLinus Torvalds 1698d50a6b56SStephen Hemminger return 0; 16991da177e4SLinus Torvalds } 17001da177e4SLinus Torvalds 1701d50a6b56SStephen Hemminger static ssize_t pktgen_thread_write(struct file *file, 1702d50a6b56SStephen Hemminger const char __user * user_buffer, 1703d50a6b56SStephen Hemminger size_t count, loff_t * offset) 17041da177e4SLinus Torvalds { 1705d50a6b56SStephen Hemminger struct seq_file *seq = (struct seq_file *)file->private_data; 1706d50a6b56SStephen Hemminger struct pktgen_thread *t = seq->private; 17071da177e4SLinus Torvalds int i = 0, max, len, ret; 17081da177e4SLinus Torvalds char name[40]; 17091da177e4SLinus Torvalds char *pg_result; 17101da177e4SLinus Torvalds unsigned long value = 0; 17111da177e4SLinus Torvalds 17121da177e4SLinus Torvalds if (count < 1) { 17131da177e4SLinus Torvalds // sprintf(pg_result, "Wrong command format"); 17141da177e4SLinus Torvalds return -EINVAL; 17151da177e4SLinus Torvalds } 17161da177e4SLinus Torvalds 17171da177e4SLinus Torvalds max = count - i; 17181da177e4SLinus Torvalds len = count_trail_chars(&user_buffer[i], max); 17191da177e4SLinus Torvalds if (len < 0) 17201da177e4SLinus Torvalds return len; 17211da177e4SLinus Torvalds 17221da177e4SLinus Torvalds i += len; 17231da177e4SLinus Torvalds 17241da177e4SLinus Torvalds /* Read variable name */ 17251da177e4SLinus Torvalds 17261da177e4SLinus Torvalds len = strn_len(&user_buffer[i], sizeof(name) - 1); 17271da177e4SLinus Torvalds if (len < 0) 17281da177e4SLinus Torvalds return len; 17291da177e4SLinus Torvalds 17301da177e4SLinus Torvalds memset(name, 0, sizeof(name)); 17311da177e4SLinus Torvalds if (copy_from_user(name, &user_buffer[i], len)) 17321da177e4SLinus Torvalds return -EFAULT; 17331da177e4SLinus Torvalds i += len; 17341da177e4SLinus Torvalds 17351da177e4SLinus Torvalds max = count - i; 17361da177e4SLinus Torvalds len = count_trail_chars(&user_buffer[i], max); 17371da177e4SLinus Torvalds if (len < 0) 17381da177e4SLinus Torvalds return len; 17391da177e4SLinus Torvalds 17401da177e4SLinus Torvalds i += len; 17411da177e4SLinus Torvalds 17421da177e4SLinus Torvalds if (debug) 1743222f1806SLuiz Capitulino printk("pktgen: t=%s, count=%lu\n", name, (unsigned long)count); 17441da177e4SLinus Torvalds 17451da177e4SLinus Torvalds if (!t) { 17461da177e4SLinus Torvalds printk("pktgen: ERROR: No thread\n"); 17471da177e4SLinus Torvalds ret = -EINVAL; 17481da177e4SLinus Torvalds goto out; 17491da177e4SLinus Torvalds } 17501da177e4SLinus Torvalds 17511da177e4SLinus Torvalds pg_result = &(t->result[0]); 17521da177e4SLinus Torvalds 17531da177e4SLinus Torvalds if (!strcmp(name, "add_device")) { 17541da177e4SLinus Torvalds char f[32]; 17551da177e4SLinus Torvalds memset(f, 0, 32); 17561da177e4SLinus Torvalds len = strn_len(&user_buffer[i], sizeof(f) - 1); 17571da177e4SLinus Torvalds if (len < 0) { 17581da177e4SLinus Torvalds ret = len; 17591da177e4SLinus Torvalds goto out; 17601da177e4SLinus Torvalds } 17611da177e4SLinus Torvalds if (copy_from_user(f, &user_buffer[i], len)) 17621da177e4SLinus Torvalds return -EFAULT; 17631da177e4SLinus Torvalds i += len; 17646146e6a4SLuiz Capitulino mutex_lock(&pktgen_thread_lock); 17651da177e4SLinus Torvalds pktgen_add_device(t, f); 17666146e6a4SLuiz Capitulino mutex_unlock(&pktgen_thread_lock); 17671da177e4SLinus Torvalds ret = count; 17681da177e4SLinus Torvalds sprintf(pg_result, "OK: add_device=%s", f); 17691da177e4SLinus Torvalds goto out; 17701da177e4SLinus Torvalds } 17711da177e4SLinus Torvalds 17721da177e4SLinus Torvalds if (!strcmp(name, "rem_device_all")) { 17736146e6a4SLuiz Capitulino mutex_lock(&pktgen_thread_lock); 177495ed63f7SArthur Kepner t->control |= T_REMDEVALL; 17756146e6a4SLuiz Capitulino mutex_unlock(&pktgen_thread_lock); 1776121caf57SNishanth Aravamudan schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */ 17771da177e4SLinus Torvalds ret = count; 17781da177e4SLinus Torvalds sprintf(pg_result, "OK: rem_device_all"); 17791da177e4SLinus Torvalds goto out; 17801da177e4SLinus Torvalds } 17811da177e4SLinus Torvalds 17821da177e4SLinus Torvalds if (!strcmp(name, "max_before_softirq")) { 17831da177e4SLinus Torvalds len = num_arg(&user_buffer[i], 10, &value); 17846146e6a4SLuiz Capitulino mutex_lock(&pktgen_thread_lock); 17851da177e4SLinus Torvalds t->max_before_softirq = value; 17866146e6a4SLuiz Capitulino mutex_unlock(&pktgen_thread_lock); 17871da177e4SLinus Torvalds ret = count; 17881da177e4SLinus Torvalds sprintf(pg_result, "OK: max_before_softirq=%lu", value); 17891da177e4SLinus Torvalds goto out; 17901da177e4SLinus Torvalds } 17911da177e4SLinus Torvalds 17921da177e4SLinus Torvalds ret = -EINVAL; 17931da177e4SLinus Torvalds out: 17941da177e4SLinus Torvalds return ret; 17951da177e4SLinus Torvalds } 17961da177e4SLinus Torvalds 1797d50a6b56SStephen Hemminger static int pktgen_thread_open(struct inode *inode, struct file *file) 17981da177e4SLinus Torvalds { 1799d50a6b56SStephen Hemminger return single_open(file, pktgen_thread_show, PDE(inode)->data); 18001da177e4SLinus Torvalds } 18011da177e4SLinus Torvalds 18029a32144eSArjan van de Ven static const struct file_operations pktgen_thread_fops = { 1803d50a6b56SStephen Hemminger .owner = THIS_MODULE, 1804d50a6b56SStephen Hemminger .open = pktgen_thread_open, 1805d50a6b56SStephen Hemminger .read = seq_read, 1806d50a6b56SStephen Hemminger .llseek = seq_lseek, 1807d50a6b56SStephen Hemminger .write = pktgen_thread_write, 1808d50a6b56SStephen Hemminger .release = single_release, 1809d50a6b56SStephen Hemminger }; 18101da177e4SLinus Torvalds 18111da177e4SLinus Torvalds /* Think find or remove for NN */ 18121da177e4SLinus Torvalds static struct pktgen_dev *__pktgen_NN_threads(const char *ifname, int remove) 18131da177e4SLinus Torvalds { 18141da177e4SLinus Torvalds struct pktgen_thread *t; 18151da177e4SLinus Torvalds struct pktgen_dev *pkt_dev = NULL; 18161da177e4SLinus Torvalds 1817cdcdbe0bSLuiz Capitulino list_for_each_entry(t, &pktgen_threads, th_list) { 18181da177e4SLinus Torvalds pkt_dev = pktgen_find_dev(t, ifname); 18191da177e4SLinus Torvalds if (pkt_dev) { 18201da177e4SLinus Torvalds if (remove) { 18211da177e4SLinus Torvalds if_lock(t); 182295ed63f7SArthur Kepner pkt_dev->removal_mark = 1; 182395ed63f7SArthur Kepner t->control |= T_REMDEV; 18241da177e4SLinus Torvalds if_unlock(t); 18251da177e4SLinus Torvalds } 18261da177e4SLinus Torvalds break; 18271da177e4SLinus Torvalds } 18281da177e4SLinus Torvalds } 18291da177e4SLinus Torvalds return pkt_dev; 18301da177e4SLinus Torvalds } 18311da177e4SLinus Torvalds 183295ed63f7SArthur Kepner /* 183395ed63f7SArthur Kepner * mark a device for removal 183495ed63f7SArthur Kepner */ 183539df232fSStephen Hemminger static void pktgen_mark_device(const char *ifname) 18361da177e4SLinus Torvalds { 18371da177e4SLinus Torvalds struct pktgen_dev *pkt_dev = NULL; 183895ed63f7SArthur Kepner const int max_tries = 10, msec_per_try = 125; 183995ed63f7SArthur Kepner int i = 0; 184095ed63f7SArthur Kepner 18416146e6a4SLuiz Capitulino mutex_lock(&pktgen_thread_lock); 184225c4e53aSStephen Hemminger pr_debug("pktgen: pktgen_mark_device marking %s for removal\n", ifname); 184395ed63f7SArthur Kepner 184495ed63f7SArthur Kepner while (1) { 184595ed63f7SArthur Kepner 184695ed63f7SArthur Kepner pkt_dev = __pktgen_NN_threads(ifname, REMOVE); 1847222f1806SLuiz Capitulino if (pkt_dev == NULL) 1848222f1806SLuiz Capitulino break; /* success */ 184995ed63f7SArthur Kepner 18506146e6a4SLuiz Capitulino mutex_unlock(&pktgen_thread_lock); 185125c4e53aSStephen Hemminger pr_debug("pktgen: pktgen_mark_device waiting for %s " 185225c4e53aSStephen Hemminger "to disappear....\n", ifname); 185395ed63f7SArthur Kepner schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try)); 18546146e6a4SLuiz Capitulino mutex_lock(&pktgen_thread_lock); 185595ed63f7SArthur Kepner 185695ed63f7SArthur Kepner if (++i >= max_tries) { 185795ed63f7SArthur Kepner printk("pktgen_mark_device: timed out after waiting " 185895ed63f7SArthur Kepner "%d msec for device %s to be removed\n", 185995ed63f7SArthur Kepner msec_per_try * i, ifname); 186095ed63f7SArthur Kepner break; 186195ed63f7SArthur Kepner } 186295ed63f7SArthur Kepner 186395ed63f7SArthur Kepner } 186495ed63f7SArthur Kepner 18656146e6a4SLuiz Capitulino mutex_unlock(&pktgen_thread_lock); 186639df232fSStephen Hemminger } 186795ed63f7SArthur Kepner 186839df232fSStephen Hemminger static void pktgen_change_name(struct net_device *dev) 186939df232fSStephen Hemminger { 187039df232fSStephen Hemminger struct pktgen_thread *t; 187139df232fSStephen Hemminger 187239df232fSStephen Hemminger list_for_each_entry(t, &pktgen_threads, th_list) { 187339df232fSStephen Hemminger struct pktgen_dev *pkt_dev; 187439df232fSStephen Hemminger 187539df232fSStephen Hemminger list_for_each_entry(pkt_dev, &t->if_list, list) { 187639df232fSStephen Hemminger if (pkt_dev->odev != dev) 187739df232fSStephen Hemminger continue; 187839df232fSStephen Hemminger 187939df232fSStephen Hemminger remove_proc_entry(pkt_dev->entry->name, pg_proc_dir); 188039df232fSStephen Hemminger 188139df232fSStephen Hemminger pkt_dev->entry = create_proc_entry(dev->name, 0600, 188239df232fSStephen Hemminger pg_proc_dir); 188339df232fSStephen Hemminger if (!pkt_dev->entry) 188439df232fSStephen Hemminger printk(KERN_ERR "pktgen: can't move proc " 188539df232fSStephen Hemminger " entry for '%s'\n", dev->name); 188639df232fSStephen Hemminger break; 188739df232fSStephen Hemminger } 188839df232fSStephen Hemminger } 18891da177e4SLinus Torvalds } 18901da177e4SLinus Torvalds 1891222f1806SLuiz Capitulino static int pktgen_device_event(struct notifier_block *unused, 1892222f1806SLuiz Capitulino unsigned long event, void *ptr) 18931da177e4SLinus Torvalds { 189439df232fSStephen Hemminger struct net_device *dev = ptr; 18951da177e4SLinus Torvalds 18961da177e4SLinus Torvalds /* It is OK that we do not hold the group lock right now, 18971da177e4SLinus Torvalds * as we run under the RTNL lock. 18981da177e4SLinus Torvalds */ 18991da177e4SLinus Torvalds 19001da177e4SLinus Torvalds switch (event) { 190139df232fSStephen Hemminger case NETDEV_CHANGENAME: 190239df232fSStephen Hemminger pktgen_change_name(dev); 19031da177e4SLinus Torvalds break; 19041da177e4SLinus Torvalds 19051da177e4SLinus Torvalds case NETDEV_UNREGISTER: 190695ed63f7SArthur Kepner pktgen_mark_device(dev->name); 19071da177e4SLinus Torvalds break; 19083ff50b79SStephen Hemminger } 19091da177e4SLinus Torvalds 19101da177e4SLinus Torvalds return NOTIFY_DONE; 19111da177e4SLinus Torvalds } 19121da177e4SLinus Torvalds 19131da177e4SLinus Torvalds /* Associate pktgen_dev with a device. */ 19141da177e4SLinus Torvalds 191539df232fSStephen Hemminger static int pktgen_setup_dev(struct pktgen_dev *pkt_dev, const char *ifname) 1916222f1806SLuiz Capitulino { 19171da177e4SLinus Torvalds struct net_device *odev; 191839df232fSStephen Hemminger int err; 19191da177e4SLinus Torvalds 19201da177e4SLinus Torvalds /* Clean old setups */ 19211da177e4SLinus Torvalds if (pkt_dev->odev) { 19221da177e4SLinus Torvalds dev_put(pkt_dev->odev); 19231da177e4SLinus Torvalds pkt_dev->odev = NULL; 19241da177e4SLinus Torvalds } 19251da177e4SLinus Torvalds 192639df232fSStephen Hemminger odev = dev_get_by_name(ifname); 19271da177e4SLinus Torvalds if (!odev) { 192839df232fSStephen Hemminger printk("pktgen: no such netdevice: \"%s\"\n", ifname); 192939df232fSStephen Hemminger return -ENODEV; 19301da177e4SLinus Torvalds } 193139df232fSStephen Hemminger 19321da177e4SLinus Torvalds if (odev->type != ARPHRD_ETHER) { 193339df232fSStephen Hemminger printk("pktgen: not an ethernet device: \"%s\"\n", ifname); 193439df232fSStephen Hemminger err = -EINVAL; 193539df232fSStephen Hemminger } else if (!netif_running(odev)) { 193639df232fSStephen Hemminger printk("pktgen: device is down: \"%s\"\n", ifname); 193739df232fSStephen Hemminger err = -ENETDOWN; 193839df232fSStephen Hemminger } else { 19391da177e4SLinus Torvalds pkt_dev->odev = odev; 194039df232fSStephen Hemminger return 0; 194139df232fSStephen Hemminger } 19421da177e4SLinus Torvalds 19431da177e4SLinus Torvalds dev_put(odev); 194439df232fSStephen Hemminger return err; 19451da177e4SLinus Torvalds } 19461da177e4SLinus Torvalds 19471da177e4SLinus Torvalds /* Read pkt_dev from the interface and set up internal pktgen_dev 19481da177e4SLinus Torvalds * structure to have the right information to create/send packets 19491da177e4SLinus Torvalds */ 19501da177e4SLinus Torvalds static void pktgen_setup_inject(struct pktgen_dev *pkt_dev) 19511da177e4SLinus Torvalds { 19521da177e4SLinus Torvalds if (!pkt_dev->odev) { 19531da177e4SLinus Torvalds printk("pktgen: ERROR: pkt_dev->odev == NULL in setup_inject.\n"); 1954222f1806SLuiz Capitulino sprintf(pkt_dev->result, 1955222f1806SLuiz Capitulino "ERROR: pkt_dev->odev == NULL in setup_inject.\n"); 19561da177e4SLinus Torvalds return; 19571da177e4SLinus Torvalds } 19581da177e4SLinus Torvalds 19591da177e4SLinus Torvalds /* Default to the interface's mac if not explicitly set. */ 19601da177e4SLinus Torvalds 1961f404e9a6SKris Katterjohn if (is_zero_ether_addr(pkt_dev->src_mac)) 1962f404e9a6SKris Katterjohn memcpy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr, ETH_ALEN); 19631da177e4SLinus Torvalds 19641da177e4SLinus Torvalds /* Set up Dest MAC */ 1965f404e9a6SKris Katterjohn memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN); 19661da177e4SLinus Torvalds 19671da177e4SLinus Torvalds /* Set up pkt size */ 19681da177e4SLinus Torvalds pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size; 19691da177e4SLinus Torvalds 19701da177e4SLinus Torvalds if (pkt_dev->flags & F_IPV6) { 19711da177e4SLinus Torvalds /* 19721da177e4SLinus Torvalds * Skip this automatic address setting until locks or functions 19731da177e4SLinus Torvalds * gets exported 19741da177e4SLinus Torvalds */ 19751da177e4SLinus Torvalds 19761da177e4SLinus Torvalds #ifdef NOTNOW 19771da177e4SLinus Torvalds int i, set = 0, err = 1; 19781da177e4SLinus Torvalds struct inet6_dev *idev; 19791da177e4SLinus Torvalds 19801da177e4SLinus Torvalds for (i = 0; i < IN6_ADDR_HSIZE; i++) 19811da177e4SLinus Torvalds if (pkt_dev->cur_in6_saddr.s6_addr[i]) { 19821da177e4SLinus Torvalds set = 1; 19831da177e4SLinus Torvalds break; 19841da177e4SLinus Torvalds } 19851da177e4SLinus Torvalds 19861da177e4SLinus Torvalds if (!set) { 19871da177e4SLinus Torvalds 19881da177e4SLinus Torvalds /* 19891da177e4SLinus Torvalds * Use linklevel address if unconfigured. 19901da177e4SLinus Torvalds * 19911da177e4SLinus Torvalds * use ipv6_get_lladdr if/when it's get exported 19921da177e4SLinus Torvalds */ 19931da177e4SLinus Torvalds 19948814c4b5SYOSHIFUJI Hideaki rcu_read_lock(); 19951da177e4SLinus Torvalds if ((idev = __in6_dev_get(pkt_dev->odev)) != NULL) { 19961da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 19971da177e4SLinus Torvalds 19981da177e4SLinus Torvalds read_lock_bh(&idev->lock); 1999222f1806SLuiz Capitulino for (ifp = idev->addr_list; ifp; 2000222f1806SLuiz Capitulino ifp = ifp->if_next) { 2001222f1806SLuiz Capitulino if (ifp->scope == IFA_LINK 2002222f1806SLuiz Capitulino && !(ifp-> 2003222f1806SLuiz Capitulino flags & IFA_F_TENTATIVE)) { 2004222f1806SLuiz Capitulino ipv6_addr_copy(&pkt_dev-> 2005222f1806SLuiz Capitulino cur_in6_saddr, 2006222f1806SLuiz Capitulino &ifp->addr); 20071da177e4SLinus Torvalds err = 0; 20081da177e4SLinus Torvalds break; 20091da177e4SLinus Torvalds } 20101da177e4SLinus Torvalds } 20111da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 20121da177e4SLinus Torvalds } 20138814c4b5SYOSHIFUJI Hideaki rcu_read_unlock(); 2014222f1806SLuiz Capitulino if (err) 2015222f1806SLuiz Capitulino printk("pktgen: ERROR: IPv6 link address not availble.\n"); 20161da177e4SLinus Torvalds } 20171da177e4SLinus Torvalds #endif 2018222f1806SLuiz Capitulino } else { 20191da177e4SLinus Torvalds pkt_dev->saddr_min = 0; 20201da177e4SLinus Torvalds pkt_dev->saddr_max = 0; 20211da177e4SLinus Torvalds if (strlen(pkt_dev->src_min) == 0) { 20221da177e4SLinus Torvalds 20231da177e4SLinus Torvalds struct in_device *in_dev; 20241da177e4SLinus Torvalds 20251da177e4SLinus Torvalds rcu_read_lock(); 2026e5ed6399SHerbert Xu in_dev = __in_dev_get_rcu(pkt_dev->odev); 20271da177e4SLinus Torvalds if (in_dev) { 20281da177e4SLinus Torvalds if (in_dev->ifa_list) { 2029222f1806SLuiz Capitulino pkt_dev->saddr_min = 2030222f1806SLuiz Capitulino in_dev->ifa_list->ifa_address; 20311da177e4SLinus Torvalds pkt_dev->saddr_max = pkt_dev->saddr_min; 20321da177e4SLinus Torvalds } 20331da177e4SLinus Torvalds } 20341da177e4SLinus Torvalds rcu_read_unlock(); 2035222f1806SLuiz Capitulino } else { 20361da177e4SLinus Torvalds pkt_dev->saddr_min = in_aton(pkt_dev->src_min); 20371da177e4SLinus Torvalds pkt_dev->saddr_max = in_aton(pkt_dev->src_max); 20381da177e4SLinus Torvalds } 20391da177e4SLinus Torvalds 20401da177e4SLinus Torvalds pkt_dev->daddr_min = in_aton(pkt_dev->dst_min); 20411da177e4SLinus Torvalds pkt_dev->daddr_max = in_aton(pkt_dev->dst_max); 20421da177e4SLinus Torvalds } 20431da177e4SLinus Torvalds /* Initialize current values. */ 20441da177e4SLinus Torvalds pkt_dev->cur_dst_mac_offset = 0; 20451da177e4SLinus Torvalds pkt_dev->cur_src_mac_offset = 0; 20461da177e4SLinus Torvalds pkt_dev->cur_saddr = pkt_dev->saddr_min; 20471da177e4SLinus Torvalds pkt_dev->cur_daddr = pkt_dev->daddr_min; 20481da177e4SLinus Torvalds pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min; 20491da177e4SLinus Torvalds pkt_dev->cur_udp_src = pkt_dev->udp_src_min; 20501da177e4SLinus Torvalds pkt_dev->nflows = 0; 20511da177e4SLinus Torvalds } 20521da177e4SLinus Torvalds 20531da177e4SLinus Torvalds static void spin(struct pktgen_dev *pkt_dev, __u64 spin_until_us) 20541da177e4SLinus Torvalds { 20551da177e4SLinus Torvalds __u64 start; 20561da177e4SLinus Torvalds __u64 now; 20571da177e4SLinus Torvalds 20581da177e4SLinus Torvalds start = now = getCurUs(); 20591da177e4SLinus Torvalds printk(KERN_INFO "sleeping for %d\n", (int)(spin_until_us - now)); 20601da177e4SLinus Torvalds while (now < spin_until_us) { 2061b4099fabSStephen Hemminger /* TODO: optimize sleeping behavior */ 2062121caf57SNishanth Aravamudan if (spin_until_us - now > jiffies_to_usecs(1) + 1) 2063121caf57SNishanth Aravamudan schedule_timeout_interruptible(1); 2064121caf57SNishanth Aravamudan else if (spin_until_us - now > 100) { 20651da177e4SLinus Torvalds do_softirq(); 20661da177e4SLinus Torvalds if (!pkt_dev->running) 20671da177e4SLinus Torvalds return; 20681da177e4SLinus Torvalds if (need_resched()) 20691da177e4SLinus Torvalds schedule(); 20701da177e4SLinus Torvalds } 20711da177e4SLinus Torvalds 20721da177e4SLinus Torvalds now = getCurUs(); 20731da177e4SLinus Torvalds } 20741da177e4SLinus Torvalds 20751da177e4SLinus Torvalds pkt_dev->idle_acc += now - start; 20761da177e4SLinus Torvalds } 20771da177e4SLinus Torvalds 20781da177e4SLinus Torvalds /* Increment/randomize headers according to flags and current values 20791da177e4SLinus Torvalds * for IP src/dest, UDP src/dst port, MAC-Addr src/dst 20801da177e4SLinus Torvalds */ 2081222f1806SLuiz Capitulino static void mod_cur_headers(struct pktgen_dev *pkt_dev) 2082222f1806SLuiz Capitulino { 20831da177e4SLinus Torvalds __u32 imn; 20841da177e4SLinus Torvalds __u32 imx; 20851da177e4SLinus Torvalds int flow = 0; 20861da177e4SLinus Torvalds 20871da177e4SLinus Torvalds if (pkt_dev->cflows) { 20885fa6fc76SStephen Hemminger flow = random32() % pkt_dev->cflows; 20891da177e4SLinus Torvalds 20901da177e4SLinus Torvalds if (pkt_dev->flows[flow].count > pkt_dev->lflow) 20911da177e4SLinus Torvalds pkt_dev->flows[flow].count = 0; 20921da177e4SLinus Torvalds } 20931da177e4SLinus Torvalds 20941da177e4SLinus Torvalds /* Deal with source MAC */ 20951da177e4SLinus Torvalds if (pkt_dev->src_mac_count > 1) { 20961da177e4SLinus Torvalds __u32 mc; 20971da177e4SLinus Torvalds __u32 tmp; 20981da177e4SLinus Torvalds 20991da177e4SLinus Torvalds if (pkt_dev->flags & F_MACSRC_RND) 21005fa6fc76SStephen Hemminger mc = random32() % pkt_dev->src_mac_count; 21011da177e4SLinus Torvalds else { 21021da177e4SLinus Torvalds mc = pkt_dev->cur_src_mac_offset++; 2103222f1806SLuiz Capitulino if (pkt_dev->cur_src_mac_offset > 2104222f1806SLuiz Capitulino pkt_dev->src_mac_count) 21051da177e4SLinus Torvalds pkt_dev->cur_src_mac_offset = 0; 21061da177e4SLinus Torvalds } 21071da177e4SLinus Torvalds 21081da177e4SLinus Torvalds tmp = pkt_dev->src_mac[5] + (mc & 0xFF); 21091da177e4SLinus Torvalds pkt_dev->hh[11] = tmp; 21101da177e4SLinus Torvalds tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8)); 21111da177e4SLinus Torvalds pkt_dev->hh[10] = tmp; 21121da177e4SLinus Torvalds tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8)); 21131da177e4SLinus Torvalds pkt_dev->hh[9] = tmp; 21141da177e4SLinus Torvalds tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8)); 21151da177e4SLinus Torvalds pkt_dev->hh[8] = tmp; 21161da177e4SLinus Torvalds tmp = (pkt_dev->src_mac[1] + (tmp >> 8)); 21171da177e4SLinus Torvalds pkt_dev->hh[7] = tmp; 21181da177e4SLinus Torvalds } 21191da177e4SLinus Torvalds 21201da177e4SLinus Torvalds /* Deal with Destination MAC */ 21211da177e4SLinus Torvalds if (pkt_dev->dst_mac_count > 1) { 21221da177e4SLinus Torvalds __u32 mc; 21231da177e4SLinus Torvalds __u32 tmp; 21241da177e4SLinus Torvalds 21251da177e4SLinus Torvalds if (pkt_dev->flags & F_MACDST_RND) 21265fa6fc76SStephen Hemminger mc = random32() % pkt_dev->dst_mac_count; 21271da177e4SLinus Torvalds 21281da177e4SLinus Torvalds else { 21291da177e4SLinus Torvalds mc = pkt_dev->cur_dst_mac_offset++; 2130222f1806SLuiz Capitulino if (pkt_dev->cur_dst_mac_offset > 2131222f1806SLuiz Capitulino pkt_dev->dst_mac_count) { 21321da177e4SLinus Torvalds pkt_dev->cur_dst_mac_offset = 0; 21331da177e4SLinus Torvalds } 21341da177e4SLinus Torvalds } 21351da177e4SLinus Torvalds 21361da177e4SLinus Torvalds tmp = pkt_dev->dst_mac[5] + (mc & 0xFF); 21371da177e4SLinus Torvalds pkt_dev->hh[5] = tmp; 21381da177e4SLinus Torvalds tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8)); 21391da177e4SLinus Torvalds pkt_dev->hh[4] = tmp; 21401da177e4SLinus Torvalds tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8)); 21411da177e4SLinus Torvalds pkt_dev->hh[3] = tmp; 21421da177e4SLinus Torvalds tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8)); 21431da177e4SLinus Torvalds pkt_dev->hh[2] = tmp; 21441da177e4SLinus Torvalds tmp = (pkt_dev->dst_mac[1] + (tmp >> 8)); 21451da177e4SLinus Torvalds pkt_dev->hh[1] = tmp; 21461da177e4SLinus Torvalds } 21471da177e4SLinus Torvalds 2148ca6549afSSteven Whitehouse if (pkt_dev->flags & F_MPLS_RND) { 2149ca6549afSSteven Whitehouse unsigned i; 2150ca6549afSSteven Whitehouse for (i = 0; i < pkt_dev->nr_labels; i++) 2151ca6549afSSteven Whitehouse if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM) 2152ca6549afSSteven Whitehouse pkt_dev->labels[i] = MPLS_STACK_BOTTOM | 21535fa6fc76SStephen Hemminger ((__force __be32)random32() & 2154ca6549afSSteven Whitehouse htonl(0x000fffff)); 2155ca6549afSSteven Whitehouse } 2156ca6549afSSteven Whitehouse 215734954ddcSFrancesco Fondelli if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) { 21585fa6fc76SStephen Hemminger pkt_dev->vlan_id = random32() & (4096-1); 215934954ddcSFrancesco Fondelli } 216034954ddcSFrancesco Fondelli 216134954ddcSFrancesco Fondelli if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) { 21625fa6fc76SStephen Hemminger pkt_dev->svlan_id = random32() & (4096 - 1); 216334954ddcSFrancesco Fondelli } 216434954ddcSFrancesco Fondelli 21651da177e4SLinus Torvalds if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) { 21661da177e4SLinus Torvalds if (pkt_dev->flags & F_UDPSRC_RND) 21675fa6fc76SStephen Hemminger pkt_dev->cur_udp_src = random32() % 21685fa6fc76SStephen Hemminger (pkt_dev->udp_src_max - pkt_dev->udp_src_min) 21695fa6fc76SStephen Hemminger + pkt_dev->udp_src_min; 21701da177e4SLinus Torvalds 21711da177e4SLinus Torvalds else { 21721da177e4SLinus Torvalds pkt_dev->cur_udp_src++; 21731da177e4SLinus Torvalds if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max) 21741da177e4SLinus Torvalds pkt_dev->cur_udp_src = pkt_dev->udp_src_min; 21751da177e4SLinus Torvalds } 21761da177e4SLinus Torvalds } 21771da177e4SLinus Torvalds 21781da177e4SLinus Torvalds if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) { 21791da177e4SLinus Torvalds if (pkt_dev->flags & F_UDPDST_RND) { 21805fa6fc76SStephen Hemminger pkt_dev->cur_udp_dst = random32() % 21815fa6fc76SStephen Hemminger (pkt_dev->udp_dst_max - pkt_dev->udp_dst_min) 21825fa6fc76SStephen Hemminger + pkt_dev->udp_dst_min; 2183222f1806SLuiz Capitulino } else { 21841da177e4SLinus Torvalds pkt_dev->cur_udp_dst++; 21851da177e4SLinus Torvalds if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max) 21861da177e4SLinus Torvalds pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min; 21871da177e4SLinus Torvalds } 21881da177e4SLinus Torvalds } 21891da177e4SLinus Torvalds 21901da177e4SLinus Torvalds if (!(pkt_dev->flags & F_IPV6)) { 21911da177e4SLinus Torvalds 2192222f1806SLuiz Capitulino if ((imn = ntohl(pkt_dev->saddr_min)) < (imx = 2193222f1806SLuiz Capitulino ntohl(pkt_dev-> 2194222f1806SLuiz Capitulino saddr_max))) { 21951da177e4SLinus Torvalds __u32 t; 21961da177e4SLinus Torvalds if (pkt_dev->flags & F_IPSRC_RND) 21975fa6fc76SStephen Hemminger t = random32() % (imx - imn) + imn; 21981da177e4SLinus Torvalds else { 21991da177e4SLinus Torvalds t = ntohl(pkt_dev->cur_saddr); 22001da177e4SLinus Torvalds t++; 22011da177e4SLinus Torvalds if (t > imx) { 22021da177e4SLinus Torvalds t = imn; 22031da177e4SLinus Torvalds } 22041da177e4SLinus Torvalds } 22051da177e4SLinus Torvalds pkt_dev->cur_saddr = htonl(t); 22061da177e4SLinus Torvalds } 22071da177e4SLinus Torvalds 22081da177e4SLinus Torvalds if (pkt_dev->cflows && pkt_dev->flows[flow].count != 0) { 22091da177e4SLinus Torvalds pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr; 22101da177e4SLinus Torvalds } else { 2211252e3346SAl Viro imn = ntohl(pkt_dev->daddr_min); 2212252e3346SAl Viro imx = ntohl(pkt_dev->daddr_max); 2213252e3346SAl Viro if (imn < imx) { 22141da177e4SLinus Torvalds __u32 t; 2215252e3346SAl Viro __be32 s; 22161da177e4SLinus Torvalds if (pkt_dev->flags & F_IPDST_RND) { 22171da177e4SLinus Torvalds 22185fa6fc76SStephen Hemminger t = random32() % (imx - imn) + imn; 2219252e3346SAl Viro s = htonl(t); 22201da177e4SLinus Torvalds 2221252e3346SAl Viro while (LOOPBACK(s) || MULTICAST(s) 2222252e3346SAl Viro || BADCLASS(s) || ZERONET(s) 2223252e3346SAl Viro || LOCAL_MCAST(s)) { 22245fa6fc76SStephen Hemminger t = random32() % (imx - imn) + imn; 2225252e3346SAl Viro s = htonl(t); 22261da177e4SLinus Torvalds } 2227252e3346SAl Viro pkt_dev->cur_daddr = s; 2228252e3346SAl Viro } else { 22291da177e4SLinus Torvalds t = ntohl(pkt_dev->cur_daddr); 22301da177e4SLinus Torvalds t++; 22311da177e4SLinus Torvalds if (t > imx) { 22321da177e4SLinus Torvalds t = imn; 22331da177e4SLinus Torvalds } 22341da177e4SLinus Torvalds pkt_dev->cur_daddr = htonl(t); 22351da177e4SLinus Torvalds } 22361da177e4SLinus Torvalds } 22371da177e4SLinus Torvalds if (pkt_dev->cflows) { 2238222f1806SLuiz Capitulino pkt_dev->flows[flow].cur_daddr = 2239222f1806SLuiz Capitulino pkt_dev->cur_daddr; 22401da177e4SLinus Torvalds pkt_dev->nflows++; 22411da177e4SLinus Torvalds } 22421da177e4SLinus Torvalds } 2243222f1806SLuiz Capitulino } else { /* IPV6 * */ 2244222f1806SLuiz Capitulino 22451da177e4SLinus Torvalds if (pkt_dev->min_in6_daddr.s6_addr32[0] == 0 && 22461da177e4SLinus Torvalds pkt_dev->min_in6_daddr.s6_addr32[1] == 0 && 22471da177e4SLinus Torvalds pkt_dev->min_in6_daddr.s6_addr32[2] == 0 && 22481da177e4SLinus Torvalds pkt_dev->min_in6_daddr.s6_addr32[3] == 0) ; 22491da177e4SLinus Torvalds else { 22501da177e4SLinus Torvalds int i; 22511da177e4SLinus Torvalds 22521da177e4SLinus Torvalds /* Only random destinations yet */ 22531da177e4SLinus Torvalds 22541da177e4SLinus Torvalds for (i = 0; i < 4; i++) { 22551da177e4SLinus Torvalds pkt_dev->cur_in6_daddr.s6_addr32[i] = 22565fa6fc76SStephen Hemminger (((__force __be32)random32() | 22571da177e4SLinus Torvalds pkt_dev->min_in6_daddr.s6_addr32[i]) & 22581da177e4SLinus Torvalds pkt_dev->max_in6_daddr.s6_addr32[i]); 22591da177e4SLinus Torvalds } 22601da177e4SLinus Torvalds } 22611da177e4SLinus Torvalds } 22621da177e4SLinus Torvalds 22631da177e4SLinus Torvalds if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) { 22641da177e4SLinus Torvalds __u32 t; 22651da177e4SLinus Torvalds if (pkt_dev->flags & F_TXSIZE_RND) { 22665fa6fc76SStephen Hemminger t = random32() % 22675fa6fc76SStephen Hemminger (pkt_dev->max_pkt_size - pkt_dev->min_pkt_size) 22685fa6fc76SStephen Hemminger + pkt_dev->min_pkt_size; 2269222f1806SLuiz Capitulino } else { 22701da177e4SLinus Torvalds t = pkt_dev->cur_pkt_size + 1; 22711da177e4SLinus Torvalds if (t > pkt_dev->max_pkt_size) 22721da177e4SLinus Torvalds t = pkt_dev->min_pkt_size; 22731da177e4SLinus Torvalds } 22741da177e4SLinus Torvalds pkt_dev->cur_pkt_size = t; 22751da177e4SLinus Torvalds } 22761da177e4SLinus Torvalds 22771da177e4SLinus Torvalds pkt_dev->flows[flow].count++; 22781da177e4SLinus Torvalds } 22791da177e4SLinus Torvalds 2280ca6549afSSteven Whitehouse static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev) 2281ca6549afSSteven Whitehouse { 2282ca6549afSSteven Whitehouse unsigned i; 2283ca6549afSSteven Whitehouse for (i = 0; i < pkt_dev->nr_labels; i++) { 2284ca6549afSSteven Whitehouse *mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM; 2285ca6549afSSteven Whitehouse } 2286ca6549afSSteven Whitehouse mpls--; 2287ca6549afSSteven Whitehouse *mpls |= MPLS_STACK_BOTTOM; 2288ca6549afSSteven Whitehouse } 2289ca6549afSSteven Whitehouse 22900f37c605SAl Viro static inline __be16 build_tci(unsigned int id, unsigned int cfi, 22910f37c605SAl Viro unsigned int prio) 22920f37c605SAl Viro { 22930f37c605SAl Viro return htons(id | (cfi << 12) | (prio << 13)); 22940f37c605SAl Viro } 22950f37c605SAl Viro 22961da177e4SLinus Torvalds static struct sk_buff *fill_packet_ipv4(struct net_device *odev, 22971da177e4SLinus Torvalds struct pktgen_dev *pkt_dev) 22981da177e4SLinus Torvalds { 22991da177e4SLinus Torvalds struct sk_buff *skb = NULL; 23001da177e4SLinus Torvalds __u8 *eth; 23011da177e4SLinus Torvalds struct udphdr *udph; 23021da177e4SLinus Torvalds int datalen, iplen; 23031da177e4SLinus Torvalds struct iphdr *iph; 23041da177e4SLinus Torvalds struct pktgen_hdr *pgh = NULL; 2305d5f1ce9aSStephen Hemminger __be16 protocol = htons(ETH_P_IP); 2306ca6549afSSteven Whitehouse __be32 *mpls; 230734954ddcSFrancesco Fondelli __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */ 230834954ddcSFrancesco Fondelli __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */ 230934954ddcSFrancesco Fondelli __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */ 231034954ddcSFrancesco Fondelli __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */ 231134954ddcSFrancesco Fondelli 2312ca6549afSSteven Whitehouse 2313ca6549afSSteven Whitehouse if (pkt_dev->nr_labels) 2314d5f1ce9aSStephen Hemminger protocol = htons(ETH_P_MPLS_UC); 23151da177e4SLinus Torvalds 231634954ddcSFrancesco Fondelli if (pkt_dev->vlan_id != 0xffff) 2317d5f1ce9aSStephen Hemminger protocol = htons(ETH_P_8021Q); 231834954ddcSFrancesco Fondelli 231964053beeSRobert Olsson /* Update any of the values, used when we're incrementing various 232064053beeSRobert Olsson * fields. 232164053beeSRobert Olsson */ 232264053beeSRobert Olsson mod_cur_headers(pkt_dev); 232364053beeSRobert Olsson 23247ac5459eSDavid S. Miller datalen = (odev->hard_header_len + 16) & ~0xf; 2325ca6549afSSteven Whitehouse skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + datalen + 232634954ddcSFrancesco Fondelli pkt_dev->nr_labels*sizeof(u32) + 232734954ddcSFrancesco Fondelli VLAN_TAG_SIZE(pkt_dev) + SVLAN_TAG_SIZE(pkt_dev), 232834954ddcSFrancesco Fondelli GFP_ATOMIC); 23291da177e4SLinus Torvalds if (!skb) { 23301da177e4SLinus Torvalds sprintf(pkt_dev->result, "No memory"); 23311da177e4SLinus Torvalds return NULL; 23321da177e4SLinus Torvalds } 23331da177e4SLinus Torvalds 23347ac5459eSDavid S. Miller skb_reserve(skb, datalen); 23351da177e4SLinus Torvalds 23361da177e4SLinus Torvalds /* Reserve for ethernet and IP header */ 23371da177e4SLinus Torvalds eth = (__u8 *) skb_push(skb, 14); 2338ca6549afSSteven Whitehouse mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32)); 2339ca6549afSSteven Whitehouse if (pkt_dev->nr_labels) 2340ca6549afSSteven Whitehouse mpls_push(mpls, pkt_dev); 234134954ddcSFrancesco Fondelli 234234954ddcSFrancesco Fondelli if (pkt_dev->vlan_id != 0xffff) { 234334954ddcSFrancesco Fondelli if (pkt_dev->svlan_id != 0xffff) { 234434954ddcSFrancesco Fondelli svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16)); 23450f37c605SAl Viro *svlan_tci = build_tci(pkt_dev->svlan_id, 23460f37c605SAl Viro pkt_dev->svlan_cfi, 23470f37c605SAl Viro pkt_dev->svlan_p); 234834954ddcSFrancesco Fondelli svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16)); 2349d5f1ce9aSStephen Hemminger *svlan_encapsulated_proto = htons(ETH_P_8021Q); 235034954ddcSFrancesco Fondelli } 235134954ddcSFrancesco Fondelli vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16)); 23520f37c605SAl Viro *vlan_tci = build_tci(pkt_dev->vlan_id, 23530f37c605SAl Viro pkt_dev->vlan_cfi, 23540f37c605SAl Viro pkt_dev->vlan_p); 235534954ddcSFrancesco Fondelli vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16)); 2356d5f1ce9aSStephen Hemminger *vlan_encapsulated_proto = htons(ETH_P_IP); 235734954ddcSFrancesco Fondelli } 235834954ddcSFrancesco Fondelli 235927a884dcSArnaldo Carvalho de Melo skb->network_header = skb->tail; 2360b0e380b1SArnaldo Carvalho de Melo skb->transport_header = skb->network_header + sizeof(struct iphdr); 2361ddc7b8e3SArnaldo Carvalho de Melo skb_put(skb, sizeof(struct iphdr) + sizeof(struct udphdr)); 2362ddc7b8e3SArnaldo Carvalho de Melo 2363ddc7b8e3SArnaldo Carvalho de Melo iph = ip_hdr(skb); 2364ddc7b8e3SArnaldo Carvalho de Melo udph = udp_hdr(skb); 23651da177e4SLinus Torvalds 23661da177e4SLinus Torvalds memcpy(eth, pkt_dev->hh, 12); 2367252e3346SAl Viro *(__be16 *) & eth[12] = protocol; 23681da177e4SLinus Torvalds 2369ca6549afSSteven Whitehouse /* Eth + IPh + UDPh + mpls */ 2370ca6549afSSteven Whitehouse datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 - 237134954ddcSFrancesco Fondelli pkt_dev->nr_labels*sizeof(u32) - VLAN_TAG_SIZE(pkt_dev) - SVLAN_TAG_SIZE(pkt_dev); 23721da177e4SLinus Torvalds if (datalen < sizeof(struct pktgen_hdr)) 23731da177e4SLinus Torvalds datalen = sizeof(struct pktgen_hdr); 23741da177e4SLinus Torvalds 23751da177e4SLinus Torvalds udph->source = htons(pkt_dev->cur_udp_src); 23761da177e4SLinus Torvalds udph->dest = htons(pkt_dev->cur_udp_dst); 23771da177e4SLinus Torvalds udph->len = htons(datalen + 8); /* DATA + udphdr */ 23781da177e4SLinus Torvalds udph->check = 0; /* No checksum */ 23791da177e4SLinus Torvalds 23801da177e4SLinus Torvalds iph->ihl = 5; 23811da177e4SLinus Torvalds iph->version = 4; 23821da177e4SLinus Torvalds iph->ttl = 32; 23831ca7768cSFrancesco Fondelli iph->tos = pkt_dev->tos; 23841da177e4SLinus Torvalds iph->protocol = IPPROTO_UDP; /* UDP */ 23851da177e4SLinus Torvalds iph->saddr = pkt_dev->cur_saddr; 23861da177e4SLinus Torvalds iph->daddr = pkt_dev->cur_daddr; 23871da177e4SLinus Torvalds iph->frag_off = 0; 23881da177e4SLinus Torvalds iplen = 20 + 8 + datalen; 23891da177e4SLinus Torvalds iph->tot_len = htons(iplen); 23901da177e4SLinus Torvalds iph->check = 0; 23911da177e4SLinus Torvalds iph->check = ip_fast_csum((void *)iph, iph->ihl); 2392ca6549afSSteven Whitehouse skb->protocol = protocol; 2393b0e380b1SArnaldo Carvalho de Melo skb->mac_header = (skb->network_header - ETH_HLEN - 2394ddc7b8e3SArnaldo Carvalho de Melo pkt_dev->nr_labels * sizeof(u32) - 2395ddc7b8e3SArnaldo Carvalho de Melo VLAN_TAG_SIZE(pkt_dev) - SVLAN_TAG_SIZE(pkt_dev)); 23961da177e4SLinus Torvalds skb->dev = odev; 23971da177e4SLinus Torvalds skb->pkt_type = PACKET_HOST; 23981da177e4SLinus Torvalds 23991da177e4SLinus Torvalds if (pkt_dev->nfrags <= 0) 24001da177e4SLinus Torvalds pgh = (struct pktgen_hdr *)skb_put(skb, datalen); 24011da177e4SLinus Torvalds else { 24021da177e4SLinus Torvalds int frags = pkt_dev->nfrags; 24031da177e4SLinus Torvalds int i; 24041da177e4SLinus Torvalds 24051da177e4SLinus Torvalds pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8); 24061da177e4SLinus Torvalds 24071da177e4SLinus Torvalds if (frags > MAX_SKB_FRAGS) 24081da177e4SLinus Torvalds frags = MAX_SKB_FRAGS; 24091da177e4SLinus Torvalds if (datalen > frags * PAGE_SIZE) { 24101da177e4SLinus Torvalds skb_put(skb, datalen - frags * PAGE_SIZE); 24111da177e4SLinus Torvalds datalen = frags * PAGE_SIZE; 24121da177e4SLinus Torvalds } 24131da177e4SLinus Torvalds 24141da177e4SLinus Torvalds i = 0; 24151da177e4SLinus Torvalds while (datalen > 0) { 24161da177e4SLinus Torvalds struct page *page = alloc_pages(GFP_KERNEL, 0); 24171da177e4SLinus Torvalds skb_shinfo(skb)->frags[i].page = page; 24181da177e4SLinus Torvalds skb_shinfo(skb)->frags[i].page_offset = 0; 24191da177e4SLinus Torvalds skb_shinfo(skb)->frags[i].size = 24201da177e4SLinus Torvalds (datalen < PAGE_SIZE ? datalen : PAGE_SIZE); 24211da177e4SLinus Torvalds datalen -= skb_shinfo(skb)->frags[i].size; 24221da177e4SLinus Torvalds skb->len += skb_shinfo(skb)->frags[i].size; 24231da177e4SLinus Torvalds skb->data_len += skb_shinfo(skb)->frags[i].size; 24241da177e4SLinus Torvalds i++; 24251da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = i; 24261da177e4SLinus Torvalds } 24271da177e4SLinus Torvalds 24281da177e4SLinus Torvalds while (i < frags) { 24291da177e4SLinus Torvalds int rem; 24301da177e4SLinus Torvalds 24311da177e4SLinus Torvalds if (i == 0) 24321da177e4SLinus Torvalds break; 24331da177e4SLinus Torvalds 24341da177e4SLinus Torvalds rem = skb_shinfo(skb)->frags[i - 1].size / 2; 24351da177e4SLinus Torvalds if (rem == 0) 24361da177e4SLinus Torvalds break; 24371da177e4SLinus Torvalds 24381da177e4SLinus Torvalds skb_shinfo(skb)->frags[i - 1].size -= rem; 24391da177e4SLinus Torvalds 2440222f1806SLuiz Capitulino skb_shinfo(skb)->frags[i] = 2441222f1806SLuiz Capitulino skb_shinfo(skb)->frags[i - 1]; 24421da177e4SLinus Torvalds get_page(skb_shinfo(skb)->frags[i].page); 2443222f1806SLuiz Capitulino skb_shinfo(skb)->frags[i].page = 2444222f1806SLuiz Capitulino skb_shinfo(skb)->frags[i - 1].page; 2445222f1806SLuiz Capitulino skb_shinfo(skb)->frags[i].page_offset += 2446222f1806SLuiz Capitulino skb_shinfo(skb)->frags[i - 1].size; 24471da177e4SLinus Torvalds skb_shinfo(skb)->frags[i].size = rem; 24481da177e4SLinus Torvalds i++; 24491da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = i; 24501da177e4SLinus Torvalds } 24511da177e4SLinus Torvalds } 24521da177e4SLinus Torvalds 24531da177e4SLinus Torvalds /* Stamp the time, and sequence number, convert them to network byte order */ 24541da177e4SLinus Torvalds 24551da177e4SLinus Torvalds if (pgh) { 24561da177e4SLinus Torvalds struct timeval timestamp; 24571da177e4SLinus Torvalds 24581da177e4SLinus Torvalds pgh->pgh_magic = htonl(PKTGEN_MAGIC); 24591da177e4SLinus Torvalds pgh->seq_num = htonl(pkt_dev->seq_num); 24601da177e4SLinus Torvalds 24611da177e4SLinus Torvalds do_gettimeofday(×tamp); 24621da177e4SLinus Torvalds pgh->tv_sec = htonl(timestamp.tv_sec); 24631da177e4SLinus Torvalds pgh->tv_usec = htonl(timestamp.tv_usec); 24641da177e4SLinus Torvalds } 24651da177e4SLinus Torvalds 24661da177e4SLinus Torvalds return skb; 24671da177e4SLinus Torvalds } 24681da177e4SLinus Torvalds 24691da177e4SLinus Torvalds /* 24701da177e4SLinus Torvalds * scan_ip6, fmt_ip taken from dietlibc-0.21 24711da177e4SLinus Torvalds * Author Felix von Leitner <felix-dietlibc@fefe.de> 24721da177e4SLinus Torvalds * 24731da177e4SLinus Torvalds * Slightly modified for kernel. 24741da177e4SLinus Torvalds * Should be candidate for net/ipv4/utils.c 24751da177e4SLinus Torvalds * --ro 24761da177e4SLinus Torvalds */ 24771da177e4SLinus Torvalds 24781da177e4SLinus Torvalds static unsigned int scan_ip6(const char *s, char ip[16]) 24791da177e4SLinus Torvalds { 24801da177e4SLinus Torvalds unsigned int i; 24811da177e4SLinus Torvalds unsigned int len = 0; 24821da177e4SLinus Torvalds unsigned long u; 24831da177e4SLinus Torvalds char suffix[16]; 24841da177e4SLinus Torvalds unsigned int prefixlen = 0; 24851da177e4SLinus Torvalds unsigned int suffixlen = 0; 2486252e3346SAl Viro __be32 tmp; 24871da177e4SLinus Torvalds 2488222f1806SLuiz Capitulino for (i = 0; i < 16; i++) 2489222f1806SLuiz Capitulino ip[i] = 0; 24901da177e4SLinus Torvalds 24911da177e4SLinus Torvalds for (;;) { 24921da177e4SLinus Torvalds if (*s == ':') { 24931da177e4SLinus Torvalds len++; 24941da177e4SLinus Torvalds if (s[1] == ':') { /* Found "::", skip to part 2 */ 24951da177e4SLinus Torvalds s += 2; 24961da177e4SLinus Torvalds len++; 24971da177e4SLinus Torvalds break; 24981da177e4SLinus Torvalds } 24991da177e4SLinus Torvalds s++; 25001da177e4SLinus Torvalds } 25011da177e4SLinus Torvalds { 25021da177e4SLinus Torvalds char *tmp; 25031da177e4SLinus Torvalds u = simple_strtoul(s, &tmp, 16); 25041da177e4SLinus Torvalds i = tmp - s; 25051da177e4SLinus Torvalds } 25061da177e4SLinus Torvalds 2507222f1806SLuiz Capitulino if (!i) 2508222f1806SLuiz Capitulino return 0; 25091da177e4SLinus Torvalds if (prefixlen == 12 && s[i] == '.') { 25101da177e4SLinus Torvalds 25111da177e4SLinus Torvalds /* the last 4 bytes may be written as IPv4 address */ 25121da177e4SLinus Torvalds 25131da177e4SLinus Torvalds tmp = in_aton(s); 25141da177e4SLinus Torvalds memcpy((struct in_addr *)(ip + 12), &tmp, sizeof(tmp)); 25151da177e4SLinus Torvalds return i + len; 25161da177e4SLinus Torvalds } 25171da177e4SLinus Torvalds ip[prefixlen++] = (u >> 8); 25181da177e4SLinus Torvalds ip[prefixlen++] = (u & 255); 2519222f1806SLuiz Capitulino s += i; 2520222f1806SLuiz Capitulino len += i; 25211da177e4SLinus Torvalds if (prefixlen == 16) 25221da177e4SLinus Torvalds return len; 25231da177e4SLinus Torvalds } 25241da177e4SLinus Torvalds 25251da177e4SLinus Torvalds /* part 2, after "::" */ 25261da177e4SLinus Torvalds for (;;) { 25271da177e4SLinus Torvalds if (*s == ':') { 25281da177e4SLinus Torvalds if (suffixlen == 0) 25291da177e4SLinus Torvalds break; 25301da177e4SLinus Torvalds s++; 25311da177e4SLinus Torvalds len++; 25321da177e4SLinus Torvalds } else if (suffixlen != 0) 25331da177e4SLinus Torvalds break; 25341da177e4SLinus Torvalds { 25351da177e4SLinus Torvalds char *tmp; 25361da177e4SLinus Torvalds u = simple_strtol(s, &tmp, 16); 25371da177e4SLinus Torvalds i = tmp - s; 25381da177e4SLinus Torvalds } 25391da177e4SLinus Torvalds if (!i) { 2540222f1806SLuiz Capitulino if (*s) 2541222f1806SLuiz Capitulino len--; 25421da177e4SLinus Torvalds break; 25431da177e4SLinus Torvalds } 25441da177e4SLinus Torvalds if (suffixlen + prefixlen <= 12 && s[i] == '.') { 25451da177e4SLinus Torvalds tmp = in_aton(s); 2546222f1806SLuiz Capitulino memcpy((struct in_addr *)(suffix + suffixlen), &tmp, 2547222f1806SLuiz Capitulino sizeof(tmp)); 25481da177e4SLinus Torvalds suffixlen += 4; 25491da177e4SLinus Torvalds len += strlen(s); 25501da177e4SLinus Torvalds break; 25511da177e4SLinus Torvalds } 25521da177e4SLinus Torvalds suffix[suffixlen++] = (u >> 8); 25531da177e4SLinus Torvalds suffix[suffixlen++] = (u & 255); 2554222f1806SLuiz Capitulino s += i; 2555222f1806SLuiz Capitulino len += i; 25561da177e4SLinus Torvalds if (prefixlen + suffixlen == 16) 25571da177e4SLinus Torvalds break; 25581da177e4SLinus Torvalds } 25591da177e4SLinus Torvalds for (i = 0; i < suffixlen; i++) 25601da177e4SLinus Torvalds ip[16 - suffixlen + i] = suffix[i]; 25611da177e4SLinus Torvalds return len; 25621da177e4SLinus Torvalds } 25631da177e4SLinus Torvalds 2564222f1806SLuiz Capitulino static char tohex(char hexdigit) 2565222f1806SLuiz Capitulino { 25661da177e4SLinus Torvalds return hexdigit > 9 ? hexdigit + 'a' - 10 : hexdigit + '0'; 25671da177e4SLinus Torvalds } 25681da177e4SLinus Torvalds 2569222f1806SLuiz Capitulino static int fmt_xlong(char *s, unsigned int i) 2570222f1806SLuiz Capitulino { 25711da177e4SLinus Torvalds char *bak = s; 2572222f1806SLuiz Capitulino *s = tohex((i >> 12) & 0xf); 2573222f1806SLuiz Capitulino if (s != bak || *s != '0') 2574222f1806SLuiz Capitulino ++s; 2575222f1806SLuiz Capitulino *s = tohex((i >> 8) & 0xf); 2576222f1806SLuiz Capitulino if (s != bak || *s != '0') 2577222f1806SLuiz Capitulino ++s; 2578222f1806SLuiz Capitulino *s = tohex((i >> 4) & 0xf); 2579222f1806SLuiz Capitulino if (s != bak || *s != '0') 2580222f1806SLuiz Capitulino ++s; 25811da177e4SLinus Torvalds *s = tohex(i & 0xf); 25821da177e4SLinus Torvalds return s - bak + 1; 25831da177e4SLinus Torvalds } 25841da177e4SLinus Torvalds 2585222f1806SLuiz Capitulino static unsigned int fmt_ip6(char *s, const char ip[16]) 2586222f1806SLuiz Capitulino { 25871da177e4SLinus Torvalds unsigned int len; 25881da177e4SLinus Torvalds unsigned int i; 25891da177e4SLinus Torvalds unsigned int temp; 25901da177e4SLinus Torvalds unsigned int compressing; 25911da177e4SLinus Torvalds int j; 25921da177e4SLinus Torvalds 2593222f1806SLuiz Capitulino len = 0; 2594222f1806SLuiz Capitulino compressing = 0; 25951da177e4SLinus Torvalds for (j = 0; j < 16; j += 2) { 25961da177e4SLinus Torvalds 25971da177e4SLinus Torvalds #ifdef V4MAPPEDPREFIX 25981da177e4SLinus Torvalds if (j == 12 && !memcmp(ip, V4mappedprefix, 12)) { 25991da177e4SLinus Torvalds inet_ntoa_r(*(struct in_addr *)(ip + 12), s); 26001da177e4SLinus Torvalds temp = strlen(s); 26011da177e4SLinus Torvalds return len + temp; 26021da177e4SLinus Torvalds } 26031da177e4SLinus Torvalds #endif 26041da177e4SLinus Torvalds temp = ((unsigned long)(unsigned char)ip[j] << 8) + 26051da177e4SLinus Torvalds (unsigned long)(unsigned char)ip[j + 1]; 26061da177e4SLinus Torvalds if (temp == 0) { 26071da177e4SLinus Torvalds if (!compressing) { 26081da177e4SLinus Torvalds compressing = 1; 26091da177e4SLinus Torvalds if (j == 0) { 2610222f1806SLuiz Capitulino *s++ = ':'; 2611222f1806SLuiz Capitulino ++len; 26121da177e4SLinus Torvalds } 26131da177e4SLinus Torvalds } 26141da177e4SLinus Torvalds } else { 26151da177e4SLinus Torvalds if (compressing) { 26161da177e4SLinus Torvalds compressing = 0; 2617222f1806SLuiz Capitulino *s++ = ':'; 2618222f1806SLuiz Capitulino ++len; 26191da177e4SLinus Torvalds } 2620222f1806SLuiz Capitulino i = fmt_xlong(s, temp); 2621222f1806SLuiz Capitulino len += i; 2622222f1806SLuiz Capitulino s += i; 26231da177e4SLinus Torvalds if (j < 14) { 26241da177e4SLinus Torvalds *s++ = ':'; 26251da177e4SLinus Torvalds ++len; 26261da177e4SLinus Torvalds } 26271da177e4SLinus Torvalds } 26281da177e4SLinus Torvalds } 26291da177e4SLinus Torvalds if (compressing) { 2630222f1806SLuiz Capitulino *s++ = ':'; 2631222f1806SLuiz Capitulino ++len; 26321da177e4SLinus Torvalds } 26331da177e4SLinus Torvalds *s = 0; 26341da177e4SLinus Torvalds return len; 26351da177e4SLinus Torvalds } 26361da177e4SLinus Torvalds 26371da177e4SLinus Torvalds static struct sk_buff *fill_packet_ipv6(struct net_device *odev, 26381da177e4SLinus Torvalds struct pktgen_dev *pkt_dev) 26391da177e4SLinus Torvalds { 26401da177e4SLinus Torvalds struct sk_buff *skb = NULL; 26411da177e4SLinus Torvalds __u8 *eth; 26421da177e4SLinus Torvalds struct udphdr *udph; 26431da177e4SLinus Torvalds int datalen; 26441da177e4SLinus Torvalds struct ipv6hdr *iph; 26451da177e4SLinus Torvalds struct pktgen_hdr *pgh = NULL; 2646d5f1ce9aSStephen Hemminger __be16 protocol = htons(ETH_P_IPV6); 2647ca6549afSSteven Whitehouse __be32 *mpls; 264834954ddcSFrancesco Fondelli __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */ 264934954ddcSFrancesco Fondelli __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */ 265034954ddcSFrancesco Fondelli __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */ 265134954ddcSFrancesco Fondelli __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */ 2652ca6549afSSteven Whitehouse 2653ca6549afSSteven Whitehouse if (pkt_dev->nr_labels) 2654d5f1ce9aSStephen Hemminger protocol = htons(ETH_P_MPLS_UC); 26551da177e4SLinus Torvalds 265634954ddcSFrancesco Fondelli if (pkt_dev->vlan_id != 0xffff) 2657d5f1ce9aSStephen Hemminger protocol = htons(ETH_P_8021Q); 265834954ddcSFrancesco Fondelli 265964053beeSRobert Olsson /* Update any of the values, used when we're incrementing various 266064053beeSRobert Olsson * fields. 266164053beeSRobert Olsson */ 266264053beeSRobert Olsson mod_cur_headers(pkt_dev); 266364053beeSRobert Olsson 2664ca6549afSSteven Whitehouse skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + 16 + 266534954ddcSFrancesco Fondelli pkt_dev->nr_labels*sizeof(u32) + 266634954ddcSFrancesco Fondelli VLAN_TAG_SIZE(pkt_dev) + SVLAN_TAG_SIZE(pkt_dev), 266734954ddcSFrancesco Fondelli GFP_ATOMIC); 26681da177e4SLinus Torvalds if (!skb) { 26691da177e4SLinus Torvalds sprintf(pkt_dev->result, "No memory"); 26701da177e4SLinus Torvalds return NULL; 26711da177e4SLinus Torvalds } 26721da177e4SLinus Torvalds 26731da177e4SLinus Torvalds skb_reserve(skb, 16); 26741da177e4SLinus Torvalds 26751da177e4SLinus Torvalds /* Reserve for ethernet and IP header */ 26761da177e4SLinus Torvalds eth = (__u8 *) skb_push(skb, 14); 2677ca6549afSSteven Whitehouse mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32)); 2678ca6549afSSteven Whitehouse if (pkt_dev->nr_labels) 2679ca6549afSSteven Whitehouse mpls_push(mpls, pkt_dev); 268034954ddcSFrancesco Fondelli 268134954ddcSFrancesco Fondelli if (pkt_dev->vlan_id != 0xffff) { 268234954ddcSFrancesco Fondelli if (pkt_dev->svlan_id != 0xffff) { 268334954ddcSFrancesco Fondelli svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16)); 26840f37c605SAl Viro *svlan_tci = build_tci(pkt_dev->svlan_id, 26850f37c605SAl Viro pkt_dev->svlan_cfi, 26860f37c605SAl Viro pkt_dev->svlan_p); 268734954ddcSFrancesco Fondelli svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16)); 2688d5f1ce9aSStephen Hemminger *svlan_encapsulated_proto = htons(ETH_P_8021Q); 268934954ddcSFrancesco Fondelli } 269034954ddcSFrancesco Fondelli vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16)); 26910f37c605SAl Viro *vlan_tci = build_tci(pkt_dev->vlan_id, 26920f37c605SAl Viro pkt_dev->vlan_cfi, 26930f37c605SAl Viro pkt_dev->vlan_p); 269434954ddcSFrancesco Fondelli vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16)); 2695d5f1ce9aSStephen Hemminger *vlan_encapsulated_proto = htons(ETH_P_IPV6); 269634954ddcSFrancesco Fondelli } 269734954ddcSFrancesco Fondelli 269827a884dcSArnaldo Carvalho de Melo skb->network_header = skb->tail; 2699b0e380b1SArnaldo Carvalho de Melo skb->transport_header = skb->network_header + sizeof(struct ipv6hdr); 2700ddc7b8e3SArnaldo Carvalho de Melo skb_put(skb, sizeof(struct ipv6hdr) + sizeof(struct udphdr)); 2701ddc7b8e3SArnaldo Carvalho de Melo 2702ddc7b8e3SArnaldo Carvalho de Melo iph = ipv6_hdr(skb); 2703ddc7b8e3SArnaldo Carvalho de Melo udph = udp_hdr(skb); 27041da177e4SLinus Torvalds 27051da177e4SLinus Torvalds memcpy(eth, pkt_dev->hh, 12); 2706252e3346SAl Viro *(__be16 *) & eth[12] = protocol; 27071da177e4SLinus Torvalds 2708ca6549afSSteven Whitehouse /* Eth + IPh + UDPh + mpls */ 2709ca6549afSSteven Whitehouse datalen = pkt_dev->cur_pkt_size - 14 - 2710ca6549afSSteven Whitehouse sizeof(struct ipv6hdr) - sizeof(struct udphdr) - 271134954ddcSFrancesco Fondelli pkt_dev->nr_labels*sizeof(u32) - VLAN_TAG_SIZE(pkt_dev) - SVLAN_TAG_SIZE(pkt_dev); 27121da177e4SLinus Torvalds 27131da177e4SLinus Torvalds if (datalen < sizeof(struct pktgen_hdr)) { 27141da177e4SLinus Torvalds datalen = sizeof(struct pktgen_hdr); 27151da177e4SLinus Torvalds if (net_ratelimit()) 2716222f1806SLuiz Capitulino printk(KERN_INFO "pktgen: increased datalen to %d\n", 2717222f1806SLuiz Capitulino datalen); 27181da177e4SLinus Torvalds } 27191da177e4SLinus Torvalds 27201da177e4SLinus Torvalds udph->source = htons(pkt_dev->cur_udp_src); 27211da177e4SLinus Torvalds udph->dest = htons(pkt_dev->cur_udp_dst); 27221da177e4SLinus Torvalds udph->len = htons(datalen + sizeof(struct udphdr)); 27231da177e4SLinus Torvalds udph->check = 0; /* No checksum */ 27241da177e4SLinus Torvalds 2725d5f1ce9aSStephen Hemminger *(__be32 *) iph = htonl(0x60000000); /* Version + flow */ 27261da177e4SLinus Torvalds 27271ca7768cSFrancesco Fondelli if (pkt_dev->traffic_class) { 27281ca7768cSFrancesco Fondelli /* Version + traffic class + flow (0) */ 2729252e3346SAl Viro *(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20)); 27301ca7768cSFrancesco Fondelli } 27311ca7768cSFrancesco Fondelli 27321da177e4SLinus Torvalds iph->hop_limit = 32; 27331da177e4SLinus Torvalds 27341da177e4SLinus Torvalds iph->payload_len = htons(sizeof(struct udphdr) + datalen); 27351da177e4SLinus Torvalds iph->nexthdr = IPPROTO_UDP; 27361da177e4SLinus Torvalds 27371da177e4SLinus Torvalds ipv6_addr_copy(&iph->daddr, &pkt_dev->cur_in6_daddr); 27381da177e4SLinus Torvalds ipv6_addr_copy(&iph->saddr, &pkt_dev->cur_in6_saddr); 27391da177e4SLinus Torvalds 2740b0e380b1SArnaldo Carvalho de Melo skb->mac_header = (skb->network_header - ETH_HLEN - 2741ddc7b8e3SArnaldo Carvalho de Melo pkt_dev->nr_labels * sizeof(u32) - 2742ddc7b8e3SArnaldo Carvalho de Melo VLAN_TAG_SIZE(pkt_dev) - SVLAN_TAG_SIZE(pkt_dev)); 2743ca6549afSSteven Whitehouse skb->protocol = protocol; 27441da177e4SLinus Torvalds skb->dev = odev; 27451da177e4SLinus Torvalds skb->pkt_type = PACKET_HOST; 27461da177e4SLinus Torvalds 27471da177e4SLinus Torvalds if (pkt_dev->nfrags <= 0) 27481da177e4SLinus Torvalds pgh = (struct pktgen_hdr *)skb_put(skb, datalen); 27491da177e4SLinus Torvalds else { 27501da177e4SLinus Torvalds int frags = pkt_dev->nfrags; 27511da177e4SLinus Torvalds int i; 27521da177e4SLinus Torvalds 27531da177e4SLinus Torvalds pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8); 27541da177e4SLinus Torvalds 27551da177e4SLinus Torvalds if (frags > MAX_SKB_FRAGS) 27561da177e4SLinus Torvalds frags = MAX_SKB_FRAGS; 27571da177e4SLinus Torvalds if (datalen > frags * PAGE_SIZE) { 27581da177e4SLinus Torvalds skb_put(skb, datalen - frags * PAGE_SIZE); 27591da177e4SLinus Torvalds datalen = frags * PAGE_SIZE; 27601da177e4SLinus Torvalds } 27611da177e4SLinus Torvalds 27621da177e4SLinus Torvalds i = 0; 27631da177e4SLinus Torvalds while (datalen > 0) { 27641da177e4SLinus Torvalds struct page *page = alloc_pages(GFP_KERNEL, 0); 27651da177e4SLinus Torvalds skb_shinfo(skb)->frags[i].page = page; 27661da177e4SLinus Torvalds skb_shinfo(skb)->frags[i].page_offset = 0; 27671da177e4SLinus Torvalds skb_shinfo(skb)->frags[i].size = 27681da177e4SLinus Torvalds (datalen < PAGE_SIZE ? datalen : PAGE_SIZE); 27691da177e4SLinus Torvalds datalen -= skb_shinfo(skb)->frags[i].size; 27701da177e4SLinus Torvalds skb->len += skb_shinfo(skb)->frags[i].size; 27711da177e4SLinus Torvalds skb->data_len += skb_shinfo(skb)->frags[i].size; 27721da177e4SLinus Torvalds i++; 27731da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = i; 27741da177e4SLinus Torvalds } 27751da177e4SLinus Torvalds 27761da177e4SLinus Torvalds while (i < frags) { 27771da177e4SLinus Torvalds int rem; 27781da177e4SLinus Torvalds 27791da177e4SLinus Torvalds if (i == 0) 27801da177e4SLinus Torvalds break; 27811da177e4SLinus Torvalds 27821da177e4SLinus Torvalds rem = skb_shinfo(skb)->frags[i - 1].size / 2; 27831da177e4SLinus Torvalds if (rem == 0) 27841da177e4SLinus Torvalds break; 27851da177e4SLinus Torvalds 27861da177e4SLinus Torvalds skb_shinfo(skb)->frags[i - 1].size -= rem; 27871da177e4SLinus Torvalds 2788222f1806SLuiz Capitulino skb_shinfo(skb)->frags[i] = 2789222f1806SLuiz Capitulino skb_shinfo(skb)->frags[i - 1]; 27901da177e4SLinus Torvalds get_page(skb_shinfo(skb)->frags[i].page); 2791222f1806SLuiz Capitulino skb_shinfo(skb)->frags[i].page = 2792222f1806SLuiz Capitulino skb_shinfo(skb)->frags[i - 1].page; 2793222f1806SLuiz Capitulino skb_shinfo(skb)->frags[i].page_offset += 2794222f1806SLuiz Capitulino skb_shinfo(skb)->frags[i - 1].size; 27951da177e4SLinus Torvalds skb_shinfo(skb)->frags[i].size = rem; 27961da177e4SLinus Torvalds i++; 27971da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = i; 27981da177e4SLinus Torvalds } 27991da177e4SLinus Torvalds } 28001da177e4SLinus Torvalds 28011da177e4SLinus Torvalds /* Stamp the time, and sequence number, convert them to network byte order */ 28021da177e4SLinus Torvalds /* should we update cloned packets too ? */ 28031da177e4SLinus Torvalds if (pgh) { 28041da177e4SLinus Torvalds struct timeval timestamp; 28051da177e4SLinus Torvalds 28061da177e4SLinus Torvalds pgh->pgh_magic = htonl(PKTGEN_MAGIC); 28071da177e4SLinus Torvalds pgh->seq_num = htonl(pkt_dev->seq_num); 28081da177e4SLinus Torvalds 28091da177e4SLinus Torvalds do_gettimeofday(×tamp); 28101da177e4SLinus Torvalds pgh->tv_sec = htonl(timestamp.tv_sec); 28111da177e4SLinus Torvalds pgh->tv_usec = htonl(timestamp.tv_usec); 28121da177e4SLinus Torvalds } 281334954ddcSFrancesco Fondelli /* pkt_dev->seq_num++; FF: you really mean this? */ 28141da177e4SLinus Torvalds 28151da177e4SLinus Torvalds return skb; 28161da177e4SLinus Torvalds } 28171da177e4SLinus Torvalds 28181da177e4SLinus Torvalds static inline struct sk_buff *fill_packet(struct net_device *odev, 28191da177e4SLinus Torvalds struct pktgen_dev *pkt_dev) 28201da177e4SLinus Torvalds { 28211da177e4SLinus Torvalds if (pkt_dev->flags & F_IPV6) 28221da177e4SLinus Torvalds return fill_packet_ipv6(odev, pkt_dev); 28231da177e4SLinus Torvalds else 28241da177e4SLinus Torvalds return fill_packet_ipv4(odev, pkt_dev); 28251da177e4SLinus Torvalds } 28261da177e4SLinus Torvalds 28271da177e4SLinus Torvalds static void pktgen_clear_counters(struct pktgen_dev *pkt_dev) 28281da177e4SLinus Torvalds { 28291da177e4SLinus Torvalds pkt_dev->seq_num = 1; 28301da177e4SLinus Torvalds pkt_dev->idle_acc = 0; 28311da177e4SLinus Torvalds pkt_dev->sofar = 0; 28321da177e4SLinus Torvalds pkt_dev->tx_bytes = 0; 28331da177e4SLinus Torvalds pkt_dev->errors = 0; 28341da177e4SLinus Torvalds } 28351da177e4SLinus Torvalds 28361da177e4SLinus Torvalds /* Set up structure for sending pkts, clear counters */ 28371da177e4SLinus Torvalds 28381da177e4SLinus Torvalds static void pktgen_run(struct pktgen_thread *t) 28391da177e4SLinus Torvalds { 2840c26a8016SLuiz Capitulino struct pktgen_dev *pkt_dev; 28411da177e4SLinus Torvalds int started = 0; 28421da177e4SLinus Torvalds 284325c4e53aSStephen Hemminger pr_debug("pktgen: entering pktgen_run. %p\n", t); 28441da177e4SLinus Torvalds 28451da177e4SLinus Torvalds if_lock(t); 2846c26a8016SLuiz Capitulino list_for_each_entry(pkt_dev, &t->if_list, list) { 28471da177e4SLinus Torvalds 28481da177e4SLinus Torvalds /* 28491da177e4SLinus Torvalds * setup odev and create initial packet. 28501da177e4SLinus Torvalds */ 28511da177e4SLinus Torvalds pktgen_setup_inject(pkt_dev); 28521da177e4SLinus Torvalds 28531da177e4SLinus Torvalds if (pkt_dev->odev) { 28541da177e4SLinus Torvalds pktgen_clear_counters(pkt_dev); 28551da177e4SLinus Torvalds pkt_dev->running = 1; /* Cranke yeself! */ 28561da177e4SLinus Torvalds pkt_dev->skb = NULL; 28571da177e4SLinus Torvalds pkt_dev->started_at = getCurUs(); 28581da177e4SLinus Torvalds pkt_dev->next_tx_us = getCurUs(); /* Transmit immediately */ 28591da177e4SLinus Torvalds pkt_dev->next_tx_ns = 0; 28601da177e4SLinus Torvalds 28611da177e4SLinus Torvalds strcpy(pkt_dev->result, "Starting"); 28621da177e4SLinus Torvalds started++; 2863222f1806SLuiz Capitulino } else 28641da177e4SLinus Torvalds strcpy(pkt_dev->result, "Error starting"); 28651da177e4SLinus Torvalds } 28661da177e4SLinus Torvalds if_unlock(t); 2867222f1806SLuiz Capitulino if (started) 2868222f1806SLuiz Capitulino t->control &= ~(T_STOP); 28691da177e4SLinus Torvalds } 28701da177e4SLinus Torvalds 28711da177e4SLinus Torvalds static void pktgen_stop_all_threads_ifs(void) 28721da177e4SLinus Torvalds { 2873cdcdbe0bSLuiz Capitulino struct pktgen_thread *t; 28741da177e4SLinus Torvalds 287525c4e53aSStephen Hemminger pr_debug("pktgen: entering pktgen_stop_all_threads_ifs.\n"); 28761da177e4SLinus Torvalds 28776146e6a4SLuiz Capitulino mutex_lock(&pktgen_thread_lock); 2878cdcdbe0bSLuiz Capitulino 2879cdcdbe0bSLuiz Capitulino list_for_each_entry(t, &pktgen_threads, th_list) 288095ed63f7SArthur Kepner t->control |= T_STOP; 2881cdcdbe0bSLuiz Capitulino 28826146e6a4SLuiz Capitulino mutex_unlock(&pktgen_thread_lock); 28831da177e4SLinus Torvalds } 28841da177e4SLinus Torvalds 28851da177e4SLinus Torvalds static int thread_is_running(struct pktgen_thread *t) 28861da177e4SLinus Torvalds { 2887c26a8016SLuiz Capitulino struct pktgen_dev *pkt_dev; 28881da177e4SLinus Torvalds int res = 0; 28891da177e4SLinus Torvalds 2890c26a8016SLuiz Capitulino list_for_each_entry(pkt_dev, &t->if_list, list) 2891c26a8016SLuiz Capitulino if (pkt_dev->running) { 28921da177e4SLinus Torvalds res = 1; 28931da177e4SLinus Torvalds break; 28941da177e4SLinus Torvalds } 28951da177e4SLinus Torvalds return res; 28961da177e4SLinus Torvalds } 28971da177e4SLinus Torvalds 28981da177e4SLinus Torvalds static int pktgen_wait_thread_run(struct pktgen_thread *t) 28991da177e4SLinus Torvalds { 29001da177e4SLinus Torvalds if_lock(t); 29011da177e4SLinus Torvalds 29021da177e4SLinus Torvalds while (thread_is_running(t)) { 29031da177e4SLinus Torvalds 29041da177e4SLinus Torvalds if_unlock(t); 29051da177e4SLinus Torvalds 29061da177e4SLinus Torvalds msleep_interruptible(100); 29071da177e4SLinus Torvalds 29081da177e4SLinus Torvalds if (signal_pending(current)) 29091da177e4SLinus Torvalds goto signal; 29101da177e4SLinus Torvalds if_lock(t); 29111da177e4SLinus Torvalds } 29121da177e4SLinus Torvalds if_unlock(t); 29131da177e4SLinus Torvalds return 1; 29141da177e4SLinus Torvalds signal: 29151da177e4SLinus Torvalds return 0; 29161da177e4SLinus Torvalds } 29171da177e4SLinus Torvalds 29181da177e4SLinus Torvalds static int pktgen_wait_all_threads_run(void) 29191da177e4SLinus Torvalds { 2920cdcdbe0bSLuiz Capitulino struct pktgen_thread *t; 29211da177e4SLinus Torvalds int sig = 1; 29221da177e4SLinus Torvalds 29236146e6a4SLuiz Capitulino mutex_lock(&pktgen_thread_lock); 2924cdcdbe0bSLuiz Capitulino 2925cdcdbe0bSLuiz Capitulino list_for_each_entry(t, &pktgen_threads, th_list) { 29261da177e4SLinus Torvalds sig = pktgen_wait_thread_run(t); 2927222f1806SLuiz Capitulino if (sig == 0) 2928222f1806SLuiz Capitulino break; 29291da177e4SLinus Torvalds } 2930cdcdbe0bSLuiz Capitulino 2931cdcdbe0bSLuiz Capitulino if (sig == 0) 2932cdcdbe0bSLuiz Capitulino list_for_each_entry(t, &pktgen_threads, th_list) 29331da177e4SLinus Torvalds t->control |= (T_STOP); 2934cdcdbe0bSLuiz Capitulino 29356146e6a4SLuiz Capitulino mutex_unlock(&pktgen_thread_lock); 29361da177e4SLinus Torvalds return sig; 29371da177e4SLinus Torvalds } 29381da177e4SLinus Torvalds 29391da177e4SLinus Torvalds static void pktgen_run_all_threads(void) 29401da177e4SLinus Torvalds { 2941cdcdbe0bSLuiz Capitulino struct pktgen_thread *t; 29421da177e4SLinus Torvalds 294325c4e53aSStephen Hemminger pr_debug("pktgen: entering pktgen_run_all_threads.\n"); 29441da177e4SLinus Torvalds 29456146e6a4SLuiz Capitulino mutex_lock(&pktgen_thread_lock); 29461da177e4SLinus Torvalds 2947cdcdbe0bSLuiz Capitulino list_for_each_entry(t, &pktgen_threads, th_list) 29481da177e4SLinus Torvalds t->control |= (T_RUN); 2949cdcdbe0bSLuiz Capitulino 29506146e6a4SLuiz Capitulino mutex_unlock(&pktgen_thread_lock); 29511da177e4SLinus Torvalds 2952121caf57SNishanth Aravamudan schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */ 29531da177e4SLinus Torvalds 29541da177e4SLinus Torvalds pktgen_wait_all_threads_run(); 29551da177e4SLinus Torvalds } 29561da177e4SLinus Torvalds 29571da177e4SLinus Torvalds static void show_results(struct pktgen_dev *pkt_dev, int nr_frags) 29581da177e4SLinus Torvalds { 29591da177e4SLinus Torvalds __u64 total_us, bps, mbps, pps, idle; 29601da177e4SLinus Torvalds char *p = pkt_dev->result; 29611da177e4SLinus Torvalds 29621da177e4SLinus Torvalds total_us = pkt_dev->stopped_at - pkt_dev->started_at; 29631da177e4SLinus Torvalds 29641da177e4SLinus Torvalds idle = pkt_dev->idle_acc; 29651da177e4SLinus Torvalds 29661da177e4SLinus Torvalds p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n", 29671da177e4SLinus Torvalds (unsigned long long)total_us, 29681da177e4SLinus Torvalds (unsigned long long)(total_us - idle), 29691da177e4SLinus Torvalds (unsigned long long)idle, 29701da177e4SLinus Torvalds (unsigned long long)pkt_dev->sofar, 29711da177e4SLinus Torvalds pkt_dev->cur_pkt_size, nr_frags); 29721da177e4SLinus Torvalds 29731da177e4SLinus Torvalds pps = pkt_dev->sofar * USEC_PER_SEC; 29741da177e4SLinus Torvalds 29751da177e4SLinus Torvalds while ((total_us >> 32) != 0) { 29761da177e4SLinus Torvalds pps >>= 1; 29771da177e4SLinus Torvalds total_us >>= 1; 29781da177e4SLinus Torvalds } 29791da177e4SLinus Torvalds 29801da177e4SLinus Torvalds do_div(pps, total_us); 29811da177e4SLinus Torvalds 29821da177e4SLinus Torvalds bps = pps * 8 * pkt_dev->cur_pkt_size; 29831da177e4SLinus Torvalds 29841da177e4SLinus Torvalds mbps = bps; 29851da177e4SLinus Torvalds do_div(mbps, 1000000); 29861da177e4SLinus Torvalds p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu", 29871da177e4SLinus Torvalds (unsigned long long)pps, 29881da177e4SLinus Torvalds (unsigned long long)mbps, 29891da177e4SLinus Torvalds (unsigned long long)bps, 29901da177e4SLinus Torvalds (unsigned long long)pkt_dev->errors); 29911da177e4SLinus Torvalds } 29921da177e4SLinus Torvalds 29931da177e4SLinus Torvalds /* Set stopped-at timer, remove from running list, do counters & statistics */ 29941da177e4SLinus Torvalds 29951da177e4SLinus Torvalds static int pktgen_stop_device(struct pktgen_dev *pkt_dev) 29961da177e4SLinus Torvalds { 2997222f1806SLuiz Capitulino int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1; 29981da177e4SLinus Torvalds 29991da177e4SLinus Torvalds if (!pkt_dev->running) { 3000222f1806SLuiz Capitulino printk("pktgen: interface: %s is already stopped\n", 300139df232fSStephen Hemminger pkt_dev->odev->name); 30021da177e4SLinus Torvalds return -EINVAL; 30031da177e4SLinus Torvalds } 30041da177e4SLinus Torvalds 30051da177e4SLinus Torvalds pkt_dev->stopped_at = getCurUs(); 30061da177e4SLinus Torvalds pkt_dev->running = 0; 30071da177e4SLinus Torvalds 300895ed63f7SArthur Kepner show_results(pkt_dev, nr_frags); 30091da177e4SLinus Torvalds 30101da177e4SLinus Torvalds return 0; 30111da177e4SLinus Torvalds } 30121da177e4SLinus Torvalds 30131da177e4SLinus Torvalds static struct pktgen_dev *next_to_run(struct pktgen_thread *t) 30141da177e4SLinus Torvalds { 3015c26a8016SLuiz Capitulino struct pktgen_dev *pkt_dev, *best = NULL; 30161da177e4SLinus Torvalds 30171da177e4SLinus Torvalds if_lock(t); 30181da177e4SLinus Torvalds 3019c26a8016SLuiz Capitulino list_for_each_entry(pkt_dev, &t->if_list, list) { 3020c26a8016SLuiz Capitulino if (!pkt_dev->running) 3021222f1806SLuiz Capitulino continue; 3022222f1806SLuiz Capitulino if (best == NULL) 3023c26a8016SLuiz Capitulino best = pkt_dev; 3024c26a8016SLuiz Capitulino else if (pkt_dev->next_tx_us < best->next_tx_us) 3025c26a8016SLuiz Capitulino best = pkt_dev; 30261da177e4SLinus Torvalds } 30271da177e4SLinus Torvalds if_unlock(t); 30281da177e4SLinus Torvalds return best; 30291da177e4SLinus Torvalds } 30301da177e4SLinus Torvalds 3031222f1806SLuiz Capitulino static void pktgen_stop(struct pktgen_thread *t) 3032222f1806SLuiz Capitulino { 3033c26a8016SLuiz Capitulino struct pktgen_dev *pkt_dev; 30341da177e4SLinus Torvalds 303525c4e53aSStephen Hemminger pr_debug("pktgen: entering pktgen_stop\n"); 30361da177e4SLinus Torvalds 30371da177e4SLinus Torvalds if_lock(t); 30381da177e4SLinus Torvalds 3039c26a8016SLuiz Capitulino list_for_each_entry(pkt_dev, &t->if_list, list) { 3040c26a8016SLuiz Capitulino pktgen_stop_device(pkt_dev); 3041c26a8016SLuiz Capitulino if (pkt_dev->skb) 3042c26a8016SLuiz Capitulino kfree_skb(pkt_dev->skb); 304395ed63f7SArthur Kepner 3044c26a8016SLuiz Capitulino pkt_dev->skb = NULL; 304595ed63f7SArthur Kepner } 304695ed63f7SArthur Kepner 304795ed63f7SArthur Kepner if_unlock(t); 304895ed63f7SArthur Kepner } 304995ed63f7SArthur Kepner 305095ed63f7SArthur Kepner /* 305195ed63f7SArthur Kepner * one of our devices needs to be removed - find it 305295ed63f7SArthur Kepner * and remove it 305395ed63f7SArthur Kepner */ 305495ed63f7SArthur Kepner static void pktgen_rem_one_if(struct pktgen_thread *t) 305595ed63f7SArthur Kepner { 3056c26a8016SLuiz Capitulino struct list_head *q, *n; 3057c26a8016SLuiz Capitulino struct pktgen_dev *cur; 305895ed63f7SArthur Kepner 305925c4e53aSStephen Hemminger pr_debug("pktgen: entering pktgen_rem_one_if\n"); 306095ed63f7SArthur Kepner 306195ed63f7SArthur Kepner if_lock(t); 306295ed63f7SArthur Kepner 3063c26a8016SLuiz Capitulino list_for_each_safe(q, n, &t->if_list) { 3064c26a8016SLuiz Capitulino cur = list_entry(q, struct pktgen_dev, list); 306595ed63f7SArthur Kepner 3066222f1806SLuiz Capitulino if (!cur->removal_mark) 3067222f1806SLuiz Capitulino continue; 306895ed63f7SArthur Kepner 306995ed63f7SArthur Kepner if (cur->skb) 307095ed63f7SArthur Kepner kfree_skb(cur->skb); 307195ed63f7SArthur Kepner cur->skb = NULL; 307295ed63f7SArthur Kepner 307395ed63f7SArthur Kepner pktgen_remove_device(t, cur); 307495ed63f7SArthur Kepner 307595ed63f7SArthur Kepner break; 307695ed63f7SArthur Kepner } 30771da177e4SLinus Torvalds 30781da177e4SLinus Torvalds if_unlock(t); 30791da177e4SLinus Torvalds } 30801da177e4SLinus Torvalds 30811da177e4SLinus Torvalds static void pktgen_rem_all_ifs(struct pktgen_thread *t) 30821da177e4SLinus Torvalds { 3083c26a8016SLuiz Capitulino struct list_head *q, *n; 3084c26a8016SLuiz Capitulino struct pktgen_dev *cur; 30851da177e4SLinus Torvalds 30861da177e4SLinus Torvalds /* Remove all devices, free mem */ 30871da177e4SLinus Torvalds 308825c4e53aSStephen Hemminger pr_debug("pktgen: entering pktgen_rem_all_ifs\n"); 30891da177e4SLinus Torvalds if_lock(t); 30901da177e4SLinus Torvalds 3091c26a8016SLuiz Capitulino list_for_each_safe(q, n, &t->if_list) { 3092c26a8016SLuiz Capitulino cur = list_entry(q, struct pktgen_dev, list); 309395ed63f7SArthur Kepner 309495ed63f7SArthur Kepner if (cur->skb) 309595ed63f7SArthur Kepner kfree_skb(cur->skb); 309695ed63f7SArthur Kepner cur->skb = NULL; 309795ed63f7SArthur Kepner 30981da177e4SLinus Torvalds pktgen_remove_device(t, cur); 30991da177e4SLinus Torvalds } 31001da177e4SLinus Torvalds 31011da177e4SLinus Torvalds if_unlock(t); 31021da177e4SLinus Torvalds } 31031da177e4SLinus Torvalds 31041da177e4SLinus Torvalds static void pktgen_rem_thread(struct pktgen_thread *t) 31051da177e4SLinus Torvalds { 31061da177e4SLinus Torvalds /* Remove from the thread list */ 31071da177e4SLinus Torvalds 3108ee74baa7SDavid S. Miller remove_proc_entry(t->tsk->comm, pg_proc_dir); 31091da177e4SLinus Torvalds 31106146e6a4SLuiz Capitulino mutex_lock(&pktgen_thread_lock); 31111da177e4SLinus Torvalds 3112cdcdbe0bSLuiz Capitulino list_del(&t->th_list); 3113cdcdbe0bSLuiz Capitulino 31146146e6a4SLuiz Capitulino mutex_unlock(&pktgen_thread_lock); 31151da177e4SLinus Torvalds } 31161da177e4SLinus Torvalds 31171da177e4SLinus Torvalds static __inline__ void pktgen_xmit(struct pktgen_dev *pkt_dev) 31181da177e4SLinus Torvalds { 31191da177e4SLinus Torvalds struct net_device *odev = NULL; 31201da177e4SLinus Torvalds __u64 idle_start = 0; 31211da177e4SLinus Torvalds int ret; 31221da177e4SLinus Torvalds 31231da177e4SLinus Torvalds odev = pkt_dev->odev; 31241da177e4SLinus Torvalds 31251da177e4SLinus Torvalds if (pkt_dev->delay_us || pkt_dev->delay_ns) { 31261da177e4SLinus Torvalds u64 now; 31271da177e4SLinus Torvalds 31281da177e4SLinus Torvalds now = getCurUs(); 31291da177e4SLinus Torvalds if (now < pkt_dev->next_tx_us) 31301da177e4SLinus Torvalds spin(pkt_dev, pkt_dev->next_tx_us); 31311da177e4SLinus Torvalds 31321da177e4SLinus Torvalds /* This is max DELAY, this has special meaning of 31331da177e4SLinus Torvalds * "never transmit" 31341da177e4SLinus Torvalds */ 31351da177e4SLinus Torvalds if (pkt_dev->delay_us == 0x7FFFFFFF) { 31361da177e4SLinus Torvalds pkt_dev->next_tx_us = getCurUs() + pkt_dev->delay_us; 31371da177e4SLinus Torvalds pkt_dev->next_tx_ns = pkt_dev->delay_ns; 31381da177e4SLinus Torvalds goto out; 31391da177e4SLinus Torvalds } 31401da177e4SLinus Torvalds } 31411da177e4SLinus Torvalds 3142*f25f4e44SPeter P Waskiewicz Jr if ((netif_queue_stopped(odev) || 3143*f25f4e44SPeter P Waskiewicz Jr netif_subqueue_stopped(odev, pkt_dev->skb->queue_mapping)) || 3144*f25f4e44SPeter P Waskiewicz Jr need_resched()) { 31451da177e4SLinus Torvalds idle_start = getCurUs(); 31461da177e4SLinus Torvalds 31471da177e4SLinus Torvalds if (!netif_running(odev)) { 31481da177e4SLinus Torvalds pktgen_stop_device(pkt_dev); 314995ed63f7SArthur Kepner if (pkt_dev->skb) 315095ed63f7SArthur Kepner kfree_skb(pkt_dev->skb); 315195ed63f7SArthur Kepner pkt_dev->skb = NULL; 31521da177e4SLinus Torvalds goto out; 31531da177e4SLinus Torvalds } 31541da177e4SLinus Torvalds if (need_resched()) 31551da177e4SLinus Torvalds schedule(); 31561da177e4SLinus Torvalds 31571da177e4SLinus Torvalds pkt_dev->idle_acc += getCurUs() - idle_start; 31581da177e4SLinus Torvalds 3159*f25f4e44SPeter P Waskiewicz Jr if (netif_queue_stopped(odev) || 3160*f25f4e44SPeter P Waskiewicz Jr netif_subqueue_stopped(odev, pkt_dev->skb->queue_mapping)) { 31611da177e4SLinus Torvalds pkt_dev->next_tx_us = getCurUs(); /* TODO */ 31621da177e4SLinus Torvalds pkt_dev->next_tx_ns = 0; 31631da177e4SLinus Torvalds goto out; /* Try the next interface */ 31641da177e4SLinus Torvalds } 31651da177e4SLinus Torvalds } 31661da177e4SLinus Torvalds 31671da177e4SLinus Torvalds if (pkt_dev->last_ok || !pkt_dev->skb) { 3168222f1806SLuiz Capitulino if ((++pkt_dev->clone_count >= pkt_dev->clone_skb) 3169222f1806SLuiz Capitulino || (!pkt_dev->skb)) { 31701da177e4SLinus Torvalds /* build a new pkt */ 31711da177e4SLinus Torvalds if (pkt_dev->skb) 31721da177e4SLinus Torvalds kfree_skb(pkt_dev->skb); 31731da177e4SLinus Torvalds 31741da177e4SLinus Torvalds pkt_dev->skb = fill_packet(odev, pkt_dev); 31751da177e4SLinus Torvalds if (pkt_dev->skb == NULL) { 31761da177e4SLinus Torvalds printk("pktgen: ERROR: couldn't allocate skb in fill_packet.\n"); 31771da177e4SLinus Torvalds schedule(); 31781da177e4SLinus Torvalds pkt_dev->clone_count--; /* back out increment, OOM */ 31791da177e4SLinus Torvalds goto out; 31801da177e4SLinus Torvalds } 31811da177e4SLinus Torvalds pkt_dev->allocated_skbs++; 31821da177e4SLinus Torvalds pkt_dev->clone_count = 0; /* reset counter */ 31831da177e4SLinus Torvalds } 31841da177e4SLinus Torvalds } 31851da177e4SLinus Torvalds 3186932ff279SHerbert Xu netif_tx_lock_bh(odev); 3187*f25f4e44SPeter P Waskiewicz Jr if (!netif_queue_stopped(odev) && 3188*f25f4e44SPeter P Waskiewicz Jr !netif_subqueue_stopped(odev, pkt_dev->skb->queue_mapping)) { 31891da177e4SLinus Torvalds 31901da177e4SLinus Torvalds atomic_inc(&(pkt_dev->skb->users)); 31911da177e4SLinus Torvalds retry_now: 31921da177e4SLinus Torvalds ret = odev->hard_start_xmit(pkt_dev->skb, odev); 31931da177e4SLinus Torvalds if (likely(ret == NETDEV_TX_OK)) { 31941da177e4SLinus Torvalds pkt_dev->last_ok = 1; 31951da177e4SLinus Torvalds pkt_dev->sofar++; 31961da177e4SLinus Torvalds pkt_dev->seq_num++; 31971da177e4SLinus Torvalds pkt_dev->tx_bytes += pkt_dev->cur_pkt_size; 31981da177e4SLinus Torvalds 31991da177e4SLinus Torvalds } else if (ret == NETDEV_TX_LOCKED 32001da177e4SLinus Torvalds && (odev->features & NETIF_F_LLTX)) { 32011da177e4SLinus Torvalds cpu_relax(); 32021da177e4SLinus Torvalds goto retry_now; 32031da177e4SLinus Torvalds } else { /* Retry it next time */ 32041da177e4SLinus Torvalds 32051da177e4SLinus Torvalds atomic_dec(&(pkt_dev->skb->users)); 32061da177e4SLinus Torvalds 32071da177e4SLinus Torvalds if (debug && net_ratelimit()) 32081da177e4SLinus Torvalds printk(KERN_INFO "pktgen: Hard xmit error\n"); 32091da177e4SLinus Torvalds 32101da177e4SLinus Torvalds pkt_dev->errors++; 32111da177e4SLinus Torvalds pkt_dev->last_ok = 0; 32121da177e4SLinus Torvalds } 32131da177e4SLinus Torvalds 32141da177e4SLinus Torvalds pkt_dev->next_tx_us = getCurUs(); 32151da177e4SLinus Torvalds pkt_dev->next_tx_ns = 0; 32161da177e4SLinus Torvalds 32171da177e4SLinus Torvalds pkt_dev->next_tx_us += pkt_dev->delay_us; 32181da177e4SLinus Torvalds pkt_dev->next_tx_ns += pkt_dev->delay_ns; 32191da177e4SLinus Torvalds 32201da177e4SLinus Torvalds if (pkt_dev->next_tx_ns > 1000) { 32211da177e4SLinus Torvalds pkt_dev->next_tx_us++; 32221da177e4SLinus Torvalds pkt_dev->next_tx_ns -= 1000; 32231da177e4SLinus Torvalds } 32241da177e4SLinus Torvalds } 32251da177e4SLinus Torvalds 32261da177e4SLinus Torvalds else { /* Retry it next time */ 32271da177e4SLinus Torvalds pkt_dev->last_ok = 0; 32281da177e4SLinus Torvalds pkt_dev->next_tx_us = getCurUs(); /* TODO */ 32291da177e4SLinus Torvalds pkt_dev->next_tx_ns = 0; 32301da177e4SLinus Torvalds } 32311da177e4SLinus Torvalds 3232932ff279SHerbert Xu netif_tx_unlock_bh(odev); 32331da177e4SLinus Torvalds 32341da177e4SLinus Torvalds /* If pkt_dev->count is zero, then run forever */ 32351da177e4SLinus Torvalds if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) { 32361da177e4SLinus Torvalds if (atomic_read(&(pkt_dev->skb->users)) != 1) { 32371da177e4SLinus Torvalds idle_start = getCurUs(); 32381da177e4SLinus Torvalds while (atomic_read(&(pkt_dev->skb->users)) != 1) { 32391da177e4SLinus Torvalds if (signal_pending(current)) { 32401da177e4SLinus Torvalds break; 32411da177e4SLinus Torvalds } 32421da177e4SLinus Torvalds schedule(); 32431da177e4SLinus Torvalds } 32441da177e4SLinus Torvalds pkt_dev->idle_acc += getCurUs() - idle_start; 32451da177e4SLinus Torvalds } 32461da177e4SLinus Torvalds 32471da177e4SLinus Torvalds /* Done with this */ 32481da177e4SLinus Torvalds pktgen_stop_device(pkt_dev); 324995ed63f7SArthur Kepner if (pkt_dev->skb) 325095ed63f7SArthur Kepner kfree_skb(pkt_dev->skb); 325195ed63f7SArthur Kepner pkt_dev->skb = NULL; 32521da177e4SLinus Torvalds } 32531da177e4SLinus Torvalds out:; 32541da177e4SLinus Torvalds } 32551da177e4SLinus Torvalds 32561da177e4SLinus Torvalds /* 32571da177e4SLinus Torvalds * Main loop of the thread goes here 32581da177e4SLinus Torvalds */ 32591da177e4SLinus Torvalds 3260ee74baa7SDavid S. Miller static int pktgen_thread_worker(void *arg) 32611da177e4SLinus Torvalds { 32621da177e4SLinus Torvalds DEFINE_WAIT(wait); 3263ee74baa7SDavid S. Miller struct pktgen_thread *t = arg; 32641da177e4SLinus Torvalds struct pktgen_dev *pkt_dev = NULL; 32651da177e4SLinus Torvalds int cpu = t->cpu; 32661da177e4SLinus Torvalds u32 max_before_softirq; 32671da177e4SLinus Torvalds u32 tx_since_softirq = 0; 32681da177e4SLinus Torvalds 3269ee74baa7SDavid S. Miller BUG_ON(smp_processor_id() != cpu); 32701da177e4SLinus Torvalds 32711da177e4SLinus Torvalds init_waitqueue_head(&t->queue); 32721da177e4SLinus Torvalds 32731da177e4SLinus Torvalds t->pid = current->pid; 32741da177e4SLinus Torvalds 327525c4e53aSStephen Hemminger pr_debug("pktgen: starting pktgen/%d: pid=%d\n", cpu, current->pid); 32761da177e4SLinus Torvalds 32771da177e4SLinus Torvalds max_before_softirq = t->max_before_softirq; 32781da177e4SLinus Torvalds 3279ee74baa7SDavid S. Miller set_current_state(TASK_INTERRUPTIBLE); 32801da177e4SLinus Torvalds 3281ee74baa7SDavid S. Miller while (!kthread_should_stop()) { 3282ee74baa7SDavid S. Miller pkt_dev = next_to_run(t); 3283ee74baa7SDavid S. Miller 3284ee74baa7SDavid S. Miller if (!pkt_dev && 3285ee74baa7SDavid S. Miller (t->control & (T_STOP | T_RUN | T_REMDEVALL | T_REMDEV)) 3286ee74baa7SDavid S. Miller == 0) { 3287ee74baa7SDavid S. Miller prepare_to_wait(&(t->queue), &wait, 3288ee74baa7SDavid S. Miller TASK_INTERRUPTIBLE); 3289ee74baa7SDavid S. Miller schedule_timeout(HZ / 10); 3290ee74baa7SDavid S. Miller finish_wait(&(t->queue), &wait); 3291ee74baa7SDavid S. Miller } 32921da177e4SLinus Torvalds 32931da177e4SLinus Torvalds __set_current_state(TASK_RUNNING); 32941da177e4SLinus Torvalds 32951da177e4SLinus Torvalds if (pkt_dev) { 32961da177e4SLinus Torvalds 32971da177e4SLinus Torvalds pktgen_xmit(pkt_dev); 32981da177e4SLinus Torvalds 32991da177e4SLinus Torvalds /* 33001da177e4SLinus Torvalds * We like to stay RUNNING but must also give 33011da177e4SLinus Torvalds * others fair share. 33021da177e4SLinus Torvalds */ 33031da177e4SLinus Torvalds 33041da177e4SLinus Torvalds tx_since_softirq += pkt_dev->last_ok; 33051da177e4SLinus Torvalds 33061da177e4SLinus Torvalds if (tx_since_softirq > max_before_softirq) { 33071da177e4SLinus Torvalds if (local_softirq_pending()) 33081da177e4SLinus Torvalds do_softirq(); 33091da177e4SLinus Torvalds tx_since_softirq = 0; 33101da177e4SLinus Torvalds } 33111da177e4SLinus Torvalds } 33121da177e4SLinus Torvalds 33131da177e4SLinus Torvalds if (t->control & T_STOP) { 33141da177e4SLinus Torvalds pktgen_stop(t); 33151da177e4SLinus Torvalds t->control &= ~(T_STOP); 33161da177e4SLinus Torvalds } 33171da177e4SLinus Torvalds 33181da177e4SLinus Torvalds if (t->control & T_RUN) { 33191da177e4SLinus Torvalds pktgen_run(t); 33201da177e4SLinus Torvalds t->control &= ~(T_RUN); 33211da177e4SLinus Torvalds } 33221da177e4SLinus Torvalds 332395ed63f7SArthur Kepner if (t->control & T_REMDEVALL) { 33241da177e4SLinus Torvalds pktgen_rem_all_ifs(t); 332595ed63f7SArthur Kepner t->control &= ~(T_REMDEVALL); 332695ed63f7SArthur Kepner } 332795ed63f7SArthur Kepner 332895ed63f7SArthur Kepner if (t->control & T_REMDEV) { 332995ed63f7SArthur Kepner pktgen_rem_one_if(t); 33301da177e4SLinus Torvalds t->control &= ~(T_REMDEV); 33311da177e4SLinus Torvalds } 33321da177e4SLinus Torvalds 333309fe3ef4SAndrew Morton try_to_freeze(); 333409fe3ef4SAndrew Morton 3335ee74baa7SDavid S. Miller set_current_state(TASK_INTERRUPTIBLE); 33361da177e4SLinus Torvalds } 33371da177e4SLinus Torvalds 333825c4e53aSStephen Hemminger pr_debug("pktgen: %s stopping all device\n", t->tsk->comm); 33391da177e4SLinus Torvalds pktgen_stop(t); 33401da177e4SLinus Torvalds 334125c4e53aSStephen Hemminger pr_debug("pktgen: %s removing all device\n", t->tsk->comm); 33421da177e4SLinus Torvalds pktgen_rem_all_ifs(t); 33431da177e4SLinus Torvalds 334425c4e53aSStephen Hemminger pr_debug("pktgen: %s removing thread.\n", t->tsk->comm); 33451da177e4SLinus Torvalds pktgen_rem_thread(t); 3346cdcdbe0bSLuiz Capitulino 3347ee74baa7SDavid S. Miller return 0; 33481da177e4SLinus Torvalds } 33491da177e4SLinus Torvalds 3350222f1806SLuiz Capitulino static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t, 3351222f1806SLuiz Capitulino const char *ifname) 33521da177e4SLinus Torvalds { 3353c26a8016SLuiz Capitulino struct pktgen_dev *p, *pkt_dev = NULL; 33541da177e4SLinus Torvalds if_lock(t); 33551da177e4SLinus Torvalds 3356c26a8016SLuiz Capitulino list_for_each_entry(p, &t->if_list, list) 335739df232fSStephen Hemminger if (strncmp(p->odev->name, ifname, IFNAMSIZ) == 0) { 3358c26a8016SLuiz Capitulino pkt_dev = p; 33591da177e4SLinus Torvalds break; 33601da177e4SLinus Torvalds } 33611da177e4SLinus Torvalds 33621da177e4SLinus Torvalds if_unlock(t); 336325c4e53aSStephen Hemminger pr_debug("pktgen: find_dev(%s) returning %p\n", ifname, pkt_dev); 33641da177e4SLinus Torvalds return pkt_dev; 33651da177e4SLinus Torvalds } 33661da177e4SLinus Torvalds 33671da177e4SLinus Torvalds /* 33681da177e4SLinus Torvalds * Adds a dev at front of if_list. 33691da177e4SLinus Torvalds */ 33701da177e4SLinus Torvalds 3371222f1806SLuiz Capitulino static int add_dev_to_thread(struct pktgen_thread *t, 3372222f1806SLuiz Capitulino struct pktgen_dev *pkt_dev) 33731da177e4SLinus Torvalds { 33741da177e4SLinus Torvalds int rv = 0; 33751da177e4SLinus Torvalds 33761da177e4SLinus Torvalds if_lock(t); 33771da177e4SLinus Torvalds 33781da177e4SLinus Torvalds if (pkt_dev->pg_thread) { 33791da177e4SLinus Torvalds printk("pktgen: ERROR: already assigned to a thread.\n"); 33801da177e4SLinus Torvalds rv = -EBUSY; 33811da177e4SLinus Torvalds goto out; 33821da177e4SLinus Torvalds } 3383c26a8016SLuiz Capitulino 3384c26a8016SLuiz Capitulino list_add(&pkt_dev->list, &t->if_list); 33851da177e4SLinus Torvalds pkt_dev->pg_thread = t; 33861da177e4SLinus Torvalds pkt_dev->running = 0; 33871da177e4SLinus Torvalds 33881da177e4SLinus Torvalds out: 33891da177e4SLinus Torvalds if_unlock(t); 33901da177e4SLinus Torvalds return rv; 33911da177e4SLinus Torvalds } 33921da177e4SLinus Torvalds 33931da177e4SLinus Torvalds /* Called under thread lock */ 33941da177e4SLinus Torvalds 33951da177e4SLinus Torvalds static int pktgen_add_device(struct pktgen_thread *t, const char *ifname) 33961da177e4SLinus Torvalds { 33971da177e4SLinus Torvalds struct pktgen_dev *pkt_dev; 339839df232fSStephen Hemminger int err; 33991da177e4SLinus Torvalds 34001da177e4SLinus Torvalds /* We don't allow a device to be on several threads */ 34011da177e4SLinus Torvalds 3402d50a6b56SStephen Hemminger pkt_dev = __pktgen_NN_threads(ifname, FIND); 3403d50a6b56SStephen Hemminger if (pkt_dev) { 3404d50a6b56SStephen Hemminger printk("pktgen: ERROR: interface already used.\n"); 3405d50a6b56SStephen Hemminger return -EBUSY; 3406d50a6b56SStephen Hemminger } 34071da177e4SLinus Torvalds 34082845b63bSStephen Hemminger pkt_dev = kzalloc(sizeof(struct pktgen_dev), GFP_KERNEL); 34091da177e4SLinus Torvalds if (!pkt_dev) 34101da177e4SLinus Torvalds return -ENOMEM; 34111da177e4SLinus Torvalds 34121da177e4SLinus Torvalds pkt_dev->flows = vmalloc(MAX_CFLOWS * sizeof(struct flow_state)); 34131da177e4SLinus Torvalds if (pkt_dev->flows == NULL) { 34141da177e4SLinus Torvalds kfree(pkt_dev); 34151da177e4SLinus Torvalds return -ENOMEM; 34161da177e4SLinus Torvalds } 34171da177e4SLinus Torvalds memset(pkt_dev->flows, 0, MAX_CFLOWS * sizeof(struct flow_state)); 34181da177e4SLinus Torvalds 341995ed63f7SArthur Kepner pkt_dev->removal_mark = 0; 34201da177e4SLinus Torvalds pkt_dev->min_pkt_size = ETH_ZLEN; 34211da177e4SLinus Torvalds pkt_dev->max_pkt_size = ETH_ZLEN; 34221da177e4SLinus Torvalds pkt_dev->nfrags = 0; 34231da177e4SLinus Torvalds pkt_dev->clone_skb = pg_clone_skb_d; 34241da177e4SLinus Torvalds pkt_dev->delay_us = pg_delay_d / 1000; 34251da177e4SLinus Torvalds pkt_dev->delay_ns = pg_delay_d % 1000; 34261da177e4SLinus Torvalds pkt_dev->count = pg_count_d; 34271da177e4SLinus Torvalds pkt_dev->sofar = 0; 34281da177e4SLinus Torvalds pkt_dev->udp_src_min = 9; /* sink port */ 34291da177e4SLinus Torvalds pkt_dev->udp_src_max = 9; 34301da177e4SLinus Torvalds pkt_dev->udp_dst_min = 9; 34311da177e4SLinus Torvalds pkt_dev->udp_dst_max = 9; 34321da177e4SLinus Torvalds 343334954ddcSFrancesco Fondelli pkt_dev->vlan_p = 0; 343434954ddcSFrancesco Fondelli pkt_dev->vlan_cfi = 0; 343534954ddcSFrancesco Fondelli pkt_dev->vlan_id = 0xffff; 343634954ddcSFrancesco Fondelli pkt_dev->svlan_p = 0; 343734954ddcSFrancesco Fondelli pkt_dev->svlan_cfi = 0; 343834954ddcSFrancesco Fondelli pkt_dev->svlan_id = 0xffff; 343934954ddcSFrancesco Fondelli 344039df232fSStephen Hemminger err = pktgen_setup_dev(pkt_dev, ifname); 344139df232fSStephen Hemminger if (err) 344239df232fSStephen Hemminger goto out1; 34431da177e4SLinus Torvalds 344439df232fSStephen Hemminger pkt_dev->entry = create_proc_entry(ifname, 0600, pg_proc_dir); 344539df232fSStephen Hemminger if (!pkt_dev->entry) { 3446d50a6b56SStephen Hemminger printk("pktgen: cannot create %s/%s procfs entry.\n", 3447d50a6b56SStephen Hemminger PG_PROC_DIR, ifname); 344839df232fSStephen Hemminger err = -EINVAL; 344939df232fSStephen Hemminger goto out2; 345039df232fSStephen Hemminger } 345139df232fSStephen Hemminger pkt_dev->entry->proc_fops = &pktgen_if_fops; 345239df232fSStephen Hemminger pkt_dev->entry->data = pkt_dev; 345339df232fSStephen Hemminger 345439df232fSStephen Hemminger return add_dev_to_thread(t, pkt_dev); 345539df232fSStephen Hemminger out2: 345639df232fSStephen Hemminger dev_put(pkt_dev->odev); 345739df232fSStephen Hemminger out1: 34581da177e4SLinus Torvalds if (pkt_dev->flows) 34591da177e4SLinus Torvalds vfree(pkt_dev->flows); 34601da177e4SLinus Torvalds kfree(pkt_dev); 346139df232fSStephen Hemminger return err; 34621da177e4SLinus Torvalds } 34631da177e4SLinus Torvalds 3464ee74baa7SDavid S. Miller static int __init pktgen_create_thread(int cpu) 34651da177e4SLinus Torvalds { 3466cdcdbe0bSLuiz Capitulino struct pktgen_thread *t; 3467d50a6b56SStephen Hemminger struct proc_dir_entry *pe; 3468ee74baa7SDavid S. Miller struct task_struct *p; 34691da177e4SLinus Torvalds 34702845b63bSStephen Hemminger t = kzalloc(sizeof(struct pktgen_thread), GFP_KERNEL); 34711da177e4SLinus Torvalds if (!t) { 34721da177e4SLinus Torvalds printk("pktgen: ERROR: out of memory, can't create new thread.\n"); 34731da177e4SLinus Torvalds return -ENOMEM; 34741da177e4SLinus Torvalds } 34751da177e4SLinus Torvalds 34761da177e4SLinus Torvalds spin_lock_init(&t->if_lock); 34771da177e4SLinus Torvalds t->cpu = cpu; 34781da177e4SLinus Torvalds 3479ee74baa7SDavid S. Miller INIT_LIST_HEAD(&t->if_list); 3480ee74baa7SDavid S. Miller 3481ee74baa7SDavid S. Miller list_add_tail(&t->th_list, &pktgen_threads); 3482ee74baa7SDavid S. Miller 3483ee74baa7SDavid S. Miller p = kthread_create(pktgen_thread_worker, t, "kpktgend_%d", cpu); 3484ee74baa7SDavid S. Miller if (IS_ERR(p)) { 3485ee74baa7SDavid S. Miller printk("pktgen: kernel_thread() failed for cpu %d\n", t->cpu); 3486ee74baa7SDavid S. Miller list_del(&t->th_list); 3487ee74baa7SDavid S. Miller kfree(t); 3488ee74baa7SDavid S. Miller return PTR_ERR(p); 3489ee74baa7SDavid S. Miller } 3490ee74baa7SDavid S. Miller kthread_bind(p, cpu); 3491ee74baa7SDavid S. Miller t->tsk = p; 3492ee74baa7SDavid S. Miller 3493ee74baa7SDavid S. Miller pe = create_proc_entry(t->tsk->comm, 0600, pg_proc_dir); 3494d50a6b56SStephen Hemminger if (!pe) { 3495d50a6b56SStephen Hemminger printk("pktgen: cannot create %s/%s procfs entry.\n", 3496ee74baa7SDavid S. Miller PG_PROC_DIR, t->tsk->comm); 3497ee74baa7SDavid S. Miller kthread_stop(p); 3498ee74baa7SDavid S. Miller list_del(&t->th_list); 34991da177e4SLinus Torvalds kfree(t); 35001da177e4SLinus Torvalds return -EINVAL; 35011da177e4SLinus Torvalds } 3502d50a6b56SStephen Hemminger 3503d50a6b56SStephen Hemminger pe->proc_fops = &pktgen_thread_fops; 3504d50a6b56SStephen Hemminger pe->data = t; 35051da177e4SLinus Torvalds 3506ee74baa7SDavid S. Miller wake_up_process(p); 35071da177e4SLinus Torvalds 35081da177e4SLinus Torvalds return 0; 35091da177e4SLinus Torvalds } 35101da177e4SLinus Torvalds 35111da177e4SLinus Torvalds /* 35121da177e4SLinus Torvalds * Removes a device from the thread if_list. 35131da177e4SLinus Torvalds */ 3514222f1806SLuiz Capitulino static void _rem_dev_from_if_list(struct pktgen_thread *t, 3515222f1806SLuiz Capitulino struct pktgen_dev *pkt_dev) 35161da177e4SLinus Torvalds { 3517c26a8016SLuiz Capitulino struct list_head *q, *n; 3518c26a8016SLuiz Capitulino struct pktgen_dev *p; 35191da177e4SLinus Torvalds 3520c26a8016SLuiz Capitulino list_for_each_safe(q, n, &t->if_list) { 3521c26a8016SLuiz Capitulino p = list_entry(q, struct pktgen_dev, list); 3522c26a8016SLuiz Capitulino if (p == pkt_dev) 3523c26a8016SLuiz Capitulino list_del(&p->list); 35241da177e4SLinus Torvalds } 35251da177e4SLinus Torvalds } 35261da177e4SLinus Torvalds 3527222f1806SLuiz Capitulino static int pktgen_remove_device(struct pktgen_thread *t, 3528222f1806SLuiz Capitulino struct pktgen_dev *pkt_dev) 35291da177e4SLinus Torvalds { 35301da177e4SLinus Torvalds 353125c4e53aSStephen Hemminger pr_debug("pktgen: remove_device pkt_dev=%p\n", pkt_dev); 35321da177e4SLinus Torvalds 35331da177e4SLinus Torvalds if (pkt_dev->running) { 35341da177e4SLinus Torvalds printk("pktgen:WARNING: trying to remove a running interface, stopping it now.\n"); 35351da177e4SLinus Torvalds pktgen_stop_device(pkt_dev); 35361da177e4SLinus Torvalds } 35371da177e4SLinus Torvalds 35381da177e4SLinus Torvalds /* Dis-associate from the interface */ 35391da177e4SLinus Torvalds 35401da177e4SLinus Torvalds if (pkt_dev->odev) { 35411da177e4SLinus Torvalds dev_put(pkt_dev->odev); 35421da177e4SLinus Torvalds pkt_dev->odev = NULL; 35431da177e4SLinus Torvalds } 35441da177e4SLinus Torvalds 35451da177e4SLinus Torvalds /* And update the thread if_list */ 35461da177e4SLinus Torvalds 35471da177e4SLinus Torvalds _rem_dev_from_if_list(t, pkt_dev); 35481da177e4SLinus Torvalds 354939df232fSStephen Hemminger if (pkt_dev->entry) 355039df232fSStephen Hemminger remove_proc_entry(pkt_dev->entry->name, pg_proc_dir); 35511da177e4SLinus Torvalds 35521da177e4SLinus Torvalds if (pkt_dev->flows) 35531da177e4SLinus Torvalds vfree(pkt_dev->flows); 35541da177e4SLinus Torvalds kfree(pkt_dev); 35551da177e4SLinus Torvalds return 0; 35561da177e4SLinus Torvalds } 35571da177e4SLinus Torvalds 35581da177e4SLinus Torvalds static int __init pg_init(void) 35591da177e4SLinus Torvalds { 35601da177e4SLinus Torvalds int cpu; 3561d50a6b56SStephen Hemminger struct proc_dir_entry *pe; 3562d50a6b56SStephen Hemminger 35631da177e4SLinus Torvalds printk(version); 35641da177e4SLinus Torvalds 3565d50a6b56SStephen Hemminger pg_proc_dir = proc_mkdir(PG_PROC_DIR, proc_net); 3566d50a6b56SStephen Hemminger if (!pg_proc_dir) 3567d50a6b56SStephen Hemminger return -ENODEV; 3568d50a6b56SStephen Hemminger pg_proc_dir->owner = THIS_MODULE; 35691da177e4SLinus Torvalds 3570d50a6b56SStephen Hemminger pe = create_proc_entry(PGCTRL, 0600, pg_proc_dir); 3571d50a6b56SStephen Hemminger if (pe == NULL) { 3572222f1806SLuiz Capitulino printk("pktgen: ERROR: cannot create %s procfs entry.\n", 3573222f1806SLuiz Capitulino PGCTRL); 3574d50a6b56SStephen Hemminger proc_net_remove(PG_PROC_DIR); 35751da177e4SLinus Torvalds return -EINVAL; 35761da177e4SLinus Torvalds } 35771da177e4SLinus Torvalds 3578d50a6b56SStephen Hemminger pe->proc_fops = &pktgen_fops; 3579d50a6b56SStephen Hemminger pe->data = NULL; 35801da177e4SLinus Torvalds 35811da177e4SLinus Torvalds /* Register us to receive netdevice events */ 35821da177e4SLinus Torvalds register_netdevice_notifier(&pktgen_notifier_block); 35831da177e4SLinus Torvalds 3584670c02c2SJohn Hawkes for_each_online_cpu(cpu) { 35858024bb24SLuiz Capitulino int err; 35861da177e4SLinus Torvalds 3587ee74baa7SDavid S. Miller err = pktgen_create_thread(cpu); 35888024bb24SLuiz Capitulino if (err) 35898024bb24SLuiz Capitulino printk("pktgen: WARNING: Cannot create thread for cpu %d (%d)\n", 35908024bb24SLuiz Capitulino cpu, err); 35911da177e4SLinus Torvalds } 35928024bb24SLuiz Capitulino 35938024bb24SLuiz Capitulino if (list_empty(&pktgen_threads)) { 35948024bb24SLuiz Capitulino printk("pktgen: ERROR: Initialization failed for all threads\n"); 35958024bb24SLuiz Capitulino unregister_netdevice_notifier(&pktgen_notifier_block); 35968024bb24SLuiz Capitulino remove_proc_entry(PGCTRL, pg_proc_dir); 35978024bb24SLuiz Capitulino proc_net_remove(PG_PROC_DIR); 35988024bb24SLuiz Capitulino return -ENODEV; 35998024bb24SLuiz Capitulino } 36008024bb24SLuiz Capitulino 36011da177e4SLinus Torvalds return 0; 36021da177e4SLinus Torvalds } 36031da177e4SLinus Torvalds 36041da177e4SLinus Torvalds static void __exit pg_cleanup(void) 36051da177e4SLinus Torvalds { 3606cdcdbe0bSLuiz Capitulino struct pktgen_thread *t; 3607cdcdbe0bSLuiz Capitulino struct list_head *q, *n; 36081da177e4SLinus Torvalds wait_queue_head_t queue; 36091da177e4SLinus Torvalds init_waitqueue_head(&queue); 36101da177e4SLinus Torvalds 36111da177e4SLinus Torvalds /* Stop all interfaces & threads */ 36121da177e4SLinus Torvalds 3613cdcdbe0bSLuiz Capitulino list_for_each_safe(q, n, &pktgen_threads) { 3614cdcdbe0bSLuiz Capitulino t = list_entry(q, struct pktgen_thread, th_list); 3615ee74baa7SDavid S. Miller kthread_stop(t->tsk); 3616ee74baa7SDavid S. Miller kfree(t); 36171da177e4SLinus Torvalds } 36181da177e4SLinus Torvalds 36191da177e4SLinus Torvalds /* Un-register us from receiving netdevice events */ 36201da177e4SLinus Torvalds unregister_netdevice_notifier(&pktgen_notifier_block); 36211da177e4SLinus Torvalds 36221da177e4SLinus Torvalds /* Clean up proc file system */ 3623d50a6b56SStephen Hemminger remove_proc_entry(PGCTRL, pg_proc_dir); 3624d50a6b56SStephen Hemminger proc_net_remove(PG_PROC_DIR); 36251da177e4SLinus Torvalds } 36261da177e4SLinus Torvalds 36271da177e4SLinus Torvalds module_init(pg_init); 36281da177e4SLinus Torvalds module_exit(pg_cleanup); 36291da177e4SLinus Torvalds 36301da177e4SLinus Torvalds MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se"); 36311da177e4SLinus Torvalds MODULE_DESCRIPTION("Packet Generator tool"); 36321da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 36331da177e4SLinus Torvalds module_param(pg_count_d, int, 0); 36341da177e4SLinus Torvalds module_param(pg_delay_d, int, 0); 36351da177e4SLinus Torvalds module_param(pg_clone_skb_d, int, 0); 36361da177e4SLinus Torvalds module_param(debug, int, 0); 3637