Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Authors: |
| 3 | * Copyright 2001, 2002 by Robert Olsson <robert.olsson@its.uu.se> |
| 4 | * Uppsala University and |
| 5 | * Swedish University of Agricultural Sciences |
| 6 | * |
| 7 | * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru> |
| 8 | * Ben Greear <greearb@candelatech.com> |
| 9 | * Jens Låås <jens.laas@data.slu.se> |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or |
| 12 | * modify it under the terms of the GNU General Public License |
| 13 | * as published by the Free Software Foundation; either version |
| 14 | * 2 of the License, or (at your option) any later version. |
| 15 | * |
| 16 | * |
| 17 | * A tool for loading the network with preconfigurated packets. |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 18 | * The tool is implemented as a linux module. Parameters are output |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | * device, delay (to hard_xmit), number of packets, and whether |
| 20 | * to use multiple SKBs or just the same one. |
| 21 | * pktgen uses the installed interface's output routine. |
| 22 | * |
| 23 | * Additional hacking by: |
| 24 | * |
| 25 | * Jens.Laas@data.slu.se |
| 26 | * Improved by ANK. 010120. |
| 27 | * Improved by ANK even more. 010212. |
| 28 | * MAC address typo fixed. 010417 --ro |
| 29 | * Integrated. 020301 --DaveM |
| 30 | * Added multiskb option 020301 --DaveM |
| 31 | * Scaling of results. 020417--sigurdur@linpro.no |
| 32 | * Significant re-work of the module: |
| 33 | * * Convert to threaded model to more efficiently be able to transmit |
| 34 | * and receive on multiple interfaces at once. |
| 35 | * * Converted many counters to __u64 to allow longer runs. |
| 36 | * * Allow configuration of ranges, like min/max IP address, MACs, |
| 37 | * and UDP-ports, for both source and destination, and can |
| 38 | * set to use a random distribution or sequentially walk the range. |
| 39 | * * Can now change most values after starting. |
| 40 | * * Place 12-byte packet in UDP payload with magic number, |
| 41 | * sequence number, and timestamp. |
| 42 | * * Add receiver code that detects dropped pkts, re-ordered pkts, and |
| 43 | * latencies (with micro-second) precision. |
| 44 | * * Add IOCTL interface to easily get counters & configuration. |
| 45 | * --Ben Greear <greearb@candelatech.com> |
| 46 | * |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 47 | * Renamed multiskb to clone_skb and cleaned up sending core for two distinct |
| 48 | * skb modes. A clone_skb=0 mode for Ben "ranges" work and a clone_skb != 0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | * as a "fastpath" with a configurable number of clones after alloc's. |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 50 | * clone_skb=0 means all packets are allocated this also means ranges time |
| 51 | * stamps etc can be used. clone_skb=100 means 1 malloc is followed by 100 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | * clones. |
| 53 | * |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 54 | * Also moved to /proc/net/pktgen/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | * --ro |
| 56 | * |
| 57 | * Sept 10: Fixed threading/locking. Lots of bone-headed and more clever |
| 58 | * mistakes. Also merged in DaveM's patch in the -pre6 patch. |
| 59 | * --Ben Greear <greearb@candelatech.com> |
| 60 | * |
| 61 | * Integrated to 2.5.x 021029 --Lucio Maciel (luciomaciel@zipmail.com.br) |
| 62 | * |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 63 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | * 021124 Finished major redesign and rewrite for new functionality. |
| 65 | * See Documentation/networking/pktgen.txt for how to use this. |
| 66 | * |
| 67 | * The new operation: |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 68 | * For each CPU one thread/process is created at start. This process checks |
| 69 | * for running devices in the if_list and sends packets until count is 0 it |
| 70 | * also the thread checks the thread->control which is used for inter-process |
| 71 | * communication. controlling process "posts" operations to the threads this |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | * way. The if_lock should be possible to remove when add/rem_device is merged |
| 73 | * into this too. |
| 74 | * |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 75 | * By design there should only be *one* "controlling" process. In practice |
| 76 | * multiple write accesses gives unpredictable result. Understood by "write" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | * to /proc gives result code thats should be read be the "writer". |
Stephen Hemminger | b4099fa | 2005-10-14 15:32:22 -0700 | [diff] [blame] | 78 | * For practical use this should be no problem. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | * |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 80 | * Note when adding devices to a specific CPU there good idea to also assign |
| 81 | * /proc/irq/XX/smp_affinity so TX-interrupts gets bound to the same CPU. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | * --ro |
| 83 | * |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 84 | * Fix refcount off by one if first packet fails, potential null deref, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | * memleak 030710- KJP |
| 86 | * |
| 87 | * First "ranges" functionality for ipv6 030726 --ro |
| 88 | * |
| 89 | * Included flow support. 030802 ANK. |
| 90 | * |
| 91 | * Fixed unaligned access on IA-64 Grant Grundler <grundler@parisc-linux.org> |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 92 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | * Remove if fix from added Harald Welte <laforge@netfilter.org> 040419 |
| 94 | * ia64 compilation fix from Aron Griffis <aron@hp.com> 040604 |
| 95 | * |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 96 | * New xmit() return, do_div and misc clean up by Stephen Hemminger |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | * <shemminger@osdl.org> 040923 |
| 98 | * |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 99 | * Randy Dunlap fixed u64 printk compiler waring |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | * |
| 101 | * Remove FCS from BW calculation. Lennert Buytenhek <buytenh@wantstofly.org> |
| 102 | * New time handling. Lennert Buytenhek <buytenh@wantstofly.org> 041213 |
| 103 | * |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 104 | * Corrections from Nikolai Malykh (nmalykh@bilim.com) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | * Removed unused flags F_SET_SRCMAC & F_SET_SRCIP 041230 |
| 106 | * |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 107 | * interruptible_sleep_on_timeout() replaced Nishanth Aravamudan <nacc@us.ibm.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | * 050103 |
Steven Whitehouse | ca6549a | 2006-03-23 01:10:26 -0800 | [diff] [blame] | 109 | * |
| 110 | * MPLS support by Steven Whitehouse <steve@chygwyn.com> |
| 111 | * |
Francesco Fondelli | 34954dd | 2006-09-27 16:30:44 -0700 | [diff] [blame] | 112 | * 802.1Q/Q-in-Q support by Francesco Fondelli (FF) <francesco.fondelli@gmail.com> |
| 113 | * |
Adit Ranadive | ce5d0b4 | 2007-09-16 14:52:15 -0700 | [diff] [blame] | 114 | * Fixed src_mac command to set source mac of packet to value specified in |
| 115 | * command by Adit Ranadive <adit.262@gmail.com> |
| 116 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | */ |
| 118 | #include <linux/sys.h> |
| 119 | #include <linux/types.h> |
| 120 | #include <linux/module.h> |
| 121 | #include <linux/moduleparam.h> |
| 122 | #include <linux/kernel.h> |
Luiz Capitulino | 222fa07 | 2006-03-20 22:24:27 -0800 | [diff] [blame] | 123 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | #include <linux/sched.h> |
| 125 | #include <linux/slab.h> |
| 126 | #include <linux/vmalloc.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | #include <linux/unistd.h> |
| 128 | #include <linux/string.h> |
| 129 | #include <linux/ptrace.h> |
| 130 | #include <linux/errno.h> |
| 131 | #include <linux/ioport.h> |
| 132 | #include <linux/interrupt.h> |
Randy Dunlap | 4fc268d | 2006-01-11 12:17:47 -0800 | [diff] [blame] | 133 | #include <linux/capability.h> |
Andrew Morton | 09fe3ef | 2007-04-12 14:45:32 -0700 | [diff] [blame] | 134 | #include <linux/freezer.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | #include <linux/delay.h> |
| 136 | #include <linux/timer.h> |
Luiz Capitulino | cdcdbe0 | 2006-03-20 22:16:40 -0800 | [diff] [blame] | 137 | #include <linux/list.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | #include <linux/init.h> |
| 139 | #include <linux/skbuff.h> |
| 140 | #include <linux/netdevice.h> |
| 141 | #include <linux/inet.h> |
| 142 | #include <linux/inetdevice.h> |
| 143 | #include <linux/rtnetlink.h> |
| 144 | #include <linux/if_arp.h> |
Francesco Fondelli | 34954dd | 2006-09-27 16:30:44 -0700 | [diff] [blame] | 145 | #include <linux/if_vlan.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | #include <linux/in.h> |
| 147 | #include <linux/ip.h> |
| 148 | #include <linux/ipv6.h> |
| 149 | #include <linux/udp.h> |
| 150 | #include <linux/proc_fs.h> |
Stephen Hemminger | d50a6b5 | 2005-10-14 15:42:33 -0700 | [diff] [blame] | 151 | #include <linux/seq_file.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | #include <linux/wait.h> |
Kris Katterjohn | f404e9a | 2006-01-17 13:04:57 -0800 | [diff] [blame] | 153 | #include <linux/etherdevice.h> |
David S. Miller | ee74baa | 2007-01-01 20:51:53 -0800 | [diff] [blame] | 154 | #include <linux/kthread.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | #include <net/checksum.h> |
| 156 | #include <net/ipv6.h> |
| 157 | #include <net/addrconf.h> |
Jamal Hadi Salim | a553e4a | 2007-07-02 22:41:59 -0700 | [diff] [blame] | 158 | #ifdef CONFIG_XFRM |
| 159 | #include <net/xfrm.h> |
| 160 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | #include <asm/byteorder.h> |
| 162 | #include <linux/rcupdate.h> |
| 163 | #include <asm/bitops.h> |
| 164 | #include <asm/io.h> |
| 165 | #include <asm/dma.h> |
| 166 | #include <asm/uaccess.h> |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 167 | #include <asm/div64.h> /* do_div */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | #include <asm/timex.h> |
| 169 | |
Francesco Fondelli | 1ca7768 | 2006-09-27 16:32:03 -0700 | [diff] [blame] | 170 | #define VERSION "pktgen v2.68: Packet Generator for packet performance testing.\n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | /* The buckets are exponential in 'width' */ |
| 173 | #define LAT_BUCKETS_MAX 32 |
| 174 | #define IP_NAME_SZ 32 |
Steven Whitehouse | ca6549a | 2006-03-23 01:10:26 -0800 | [diff] [blame] | 175 | #define MAX_MPLS_LABELS 16 /* This is the max label stack depth */ |
Stephen Hemminger | d5f1ce9 | 2007-03-04 16:08:08 -0800 | [diff] [blame] | 176 | #define MPLS_STACK_BOTTOM htonl(0x00000100) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | |
| 178 | /* Device flag bits */ |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 179 | #define F_IPSRC_RND (1<<0) /* IP-Src Random */ |
| 180 | #define F_IPDST_RND (1<<1) /* IP-Dst Random */ |
| 181 | #define F_UDPSRC_RND (1<<2) /* UDP-Src Random */ |
| 182 | #define F_UDPDST_RND (1<<3) /* UDP-Dst Random */ |
| 183 | #define F_MACSRC_RND (1<<4) /* MAC-Src Random */ |
| 184 | #define F_MACDST_RND (1<<5) /* MAC-Dst Random */ |
| 185 | #define F_TXSIZE_RND (1<<6) /* Transmit size is random */ |
| 186 | #define F_IPV6 (1<<7) /* Interface in IPV6 Mode */ |
Steven Whitehouse | ca6549a | 2006-03-23 01:10:26 -0800 | [diff] [blame] | 187 | #define F_MPLS_RND (1<<8) /* Random MPLS labels */ |
Francesco Fondelli | 34954dd | 2006-09-27 16:30:44 -0700 | [diff] [blame] | 188 | #define F_VID_RND (1<<9) /* Random VLAN ID */ |
| 189 | #define F_SVID_RND (1<<10) /* Random SVLAN ID */ |
Jamal Hadi Salim | 007a531 | 2007-07-02 22:40:36 -0700 | [diff] [blame] | 190 | #define F_FLOW_SEQ (1<<11) /* Sequential flows */ |
Jamal Hadi Salim | a553e4a | 2007-07-02 22:41:59 -0700 | [diff] [blame] | 191 | #define F_IPSEC_ON (1<<12) /* ipsec on for flows */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | |
| 193 | /* Thread control flag bits */ |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 194 | #define T_TERMINATE (1<<0) |
| 195 | #define T_STOP (1<<1) /* Stop run */ |
| 196 | #define T_RUN (1<<2) /* Start run */ |
| 197 | #define T_REMDEVALL (1<<3) /* Remove all devs */ |
| 198 | #define T_REMDEV (1<<4) /* Remove one dev */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | /* If lock -- can be removed after some work */ |
| 201 | #define if_lock(t) spin_lock(&(t->if_lock)); |
| 202 | #define if_unlock(t) spin_unlock(&(t->if_lock)); |
| 203 | |
| 204 | /* Used to help with determining the pkts on receive */ |
| 205 | #define PKTGEN_MAGIC 0xbe9be955 |
Stephen Hemminger | d50a6b5 | 2005-10-14 15:42:33 -0700 | [diff] [blame] | 206 | #define PG_PROC_DIR "pktgen" |
| 207 | #define PGCTRL "pgctrl" |
| 208 | static struct proc_dir_entry *pg_proc_dir = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | |
| 210 | #define MAX_CFLOWS 65536 |
| 211 | |
Francesco Fondelli | 34954dd | 2006-09-27 16:30:44 -0700 | [diff] [blame] | 212 | #define VLAN_TAG_SIZE(x) ((x)->vlan_id == 0xffff ? 0 : 4) |
| 213 | #define SVLAN_TAG_SIZE(x) ((x)->svlan_id == 0xffff ? 0 : 4) |
| 214 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 215 | struct flow_state { |
Al Viro | 252e334 | 2006-11-14 20:48:11 -0800 | [diff] [blame] | 216 | __be32 cur_daddr; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 217 | int count; |
Jamal Hadi Salim | a553e4a | 2007-07-02 22:41:59 -0700 | [diff] [blame] | 218 | #ifdef CONFIG_XFRM |
| 219 | struct xfrm_state *x; |
| 220 | #endif |
Jamal Hadi Salim | 007a531 | 2007-07-02 22:40:36 -0700 | [diff] [blame] | 221 | __u32 flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | }; |
| 223 | |
Jamal Hadi Salim | 007a531 | 2007-07-02 22:40:36 -0700 | [diff] [blame] | 224 | /* flow flag bits */ |
| 225 | #define F_INIT (1<<0) /* flow has been initialized */ |
| 226 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | struct pktgen_dev { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | /* |
| 229 | * Try to keep frequent/infrequent used vars. separated. |
| 230 | */ |
Stephen Hemminger | 39df232 | 2007-03-04 16:11:51 -0800 | [diff] [blame] | 231 | struct proc_dir_entry *entry; /* proc file */ |
| 232 | struct pktgen_thread *pg_thread;/* the owner */ |
Luiz Capitulino | c26a801 | 2006-03-20 22:18:16 -0800 | [diff] [blame] | 233 | struct list_head list; /* Used for chaining in the thread's run-queue */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 235 | int running; /* if this changes to false, the test will stop */ |
| 236 | |
| 237 | /* If min != max, then we will either do a linear iteration, or |
| 238 | * we will do a random selection from within the range. |
| 239 | */ |
| 240 | __u32 flags; |
Arthur Kepner | 95ed63f | 2006-03-20 21:26:56 -0800 | [diff] [blame] | 241 | int removal_mark; /* non-zero => the device is marked for |
| 242 | * removal by worker thread */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 244 | int min_pkt_size; /* = ETH_ZLEN; */ |
| 245 | int max_pkt_size; /* = ETH_ZLEN; */ |
Jamal Hadi Salim | 16dab72 | 2007-07-02 22:39:50 -0700 | [diff] [blame] | 246 | int pkt_overhead; /* overhead for MPLS, VLANs, IPSEC etc */ |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 247 | int nfrags; |
| 248 | __u32 delay_us; /* Default delay */ |
| 249 | __u32 delay_ns; |
| 250 | __u64 count; /* Default No packets to send */ |
| 251 | __u64 sofar; /* How many pkts we've sent so far */ |
| 252 | __u64 tx_bytes; /* How many bytes we've transmitted */ |
| 253 | __u64 errors; /* Errors when trying to transmit, pkts will be re-sent */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 255 | /* runtime counters relating to clone_skb */ |
| 256 | __u64 next_tx_us; /* timestamp of when to tx next */ |
| 257 | __u32 next_tx_ns; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 259 | __u64 allocated_skbs; |
| 260 | __u32 clone_count; |
| 261 | int last_ok; /* Was last skb sent? |
| 262 | * Or a failed transmit of some sort? This will keep |
| 263 | * sequence numbers in order, for example. |
| 264 | */ |
| 265 | __u64 started_at; /* micro-seconds */ |
| 266 | __u64 stopped_at; /* micro-seconds */ |
| 267 | __u64 idle_acc; /* micro-seconds */ |
| 268 | __u32 seq_num; |
| 269 | |
| 270 | int clone_skb; /* Use multiple SKBs during packet gen. If this number |
| 271 | * is greater than 1, then that many copies of the same |
| 272 | * packet will be sent before a new packet is allocated. |
| 273 | * For instance, if you want to send 1024 identical packets |
| 274 | * before creating a new packet, set clone_skb to 1024. |
| 275 | */ |
| 276 | |
| 277 | char dst_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */ |
| 278 | char dst_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */ |
| 279 | char src_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */ |
| 280 | char src_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */ |
| 281 | |
| 282 | struct in6_addr in6_saddr; |
| 283 | struct in6_addr in6_daddr; |
| 284 | struct in6_addr cur_in6_daddr; |
| 285 | struct in6_addr cur_in6_saddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | /* For ranges */ |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 287 | struct in6_addr min_in6_daddr; |
| 288 | struct in6_addr max_in6_daddr; |
| 289 | struct in6_addr min_in6_saddr; |
| 290 | struct in6_addr max_in6_saddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 292 | /* If we're doing ranges, random or incremental, then this |
| 293 | * defines the min/max for those ranges. |
| 294 | */ |
Al Viro | 252e334 | 2006-11-14 20:48:11 -0800 | [diff] [blame] | 295 | __be32 saddr_min; /* inclusive, source IP address */ |
| 296 | __be32 saddr_max; /* exclusive, source IP address */ |
| 297 | __be32 daddr_min; /* inclusive, dest IP address */ |
| 298 | __be32 daddr_max; /* exclusive, dest IP address */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 300 | __u16 udp_src_min; /* inclusive, source UDP port */ |
| 301 | __u16 udp_src_max; /* exclusive, source UDP port */ |
| 302 | __u16 udp_dst_min; /* inclusive, dest UDP port */ |
| 303 | __u16 udp_dst_max; /* exclusive, dest UDP port */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | |
Francesco Fondelli | 1ca7768 | 2006-09-27 16:32:03 -0700 | [diff] [blame] | 305 | /* DSCP + ECN */ |
| 306 | __u8 tos; /* six most significant bits of (former) IPv4 TOS are for dscp codepoint */ |
| 307 | __u8 traffic_class; /* ditto for the (former) Traffic Class in IPv6 (see RFC 3260, sec. 4) */ |
| 308 | |
Steven Whitehouse | ca6549a | 2006-03-23 01:10:26 -0800 | [diff] [blame] | 309 | /* MPLS */ |
| 310 | unsigned nr_labels; /* Depth of stack, 0 = no MPLS */ |
| 311 | __be32 labels[MAX_MPLS_LABELS]; |
| 312 | |
Francesco Fondelli | 34954dd | 2006-09-27 16:30:44 -0700 | [diff] [blame] | 313 | /* VLAN/SVLAN (802.1Q/Q-in-Q) */ |
| 314 | __u8 vlan_p; |
| 315 | __u8 vlan_cfi; |
| 316 | __u16 vlan_id; /* 0xffff means no vlan tag */ |
| 317 | |
| 318 | __u8 svlan_p; |
| 319 | __u8 svlan_cfi; |
| 320 | __u16 svlan_id; /* 0xffff means no svlan tag */ |
| 321 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 322 | __u32 src_mac_count; /* How many MACs to iterate through */ |
| 323 | __u32 dst_mac_count; /* How many MACs to iterate through */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 325 | unsigned char dst_mac[ETH_ALEN]; |
| 326 | unsigned char src_mac[ETH_ALEN]; |
| 327 | |
| 328 | __u32 cur_dst_mac_offset; |
| 329 | __u32 cur_src_mac_offset; |
Al Viro | 252e334 | 2006-11-14 20:48:11 -0800 | [diff] [blame] | 330 | __be32 cur_saddr; |
| 331 | __be32 cur_daddr; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 332 | __u16 cur_udp_dst; |
| 333 | __u16 cur_udp_src; |
| 334 | __u32 cur_pkt_size; |
| 335 | |
| 336 | __u8 hh[14]; |
| 337 | /* = { |
| 338 | 0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB, |
| 339 | |
| 340 | We fill in SRC address later |
| 341 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 342 | 0x08, 0x00 |
| 343 | }; |
| 344 | */ |
| 345 | __u16 pad; /* pad out the hh struct to an even 16 bytes */ |
| 346 | |
| 347 | struct sk_buff *skb; /* skb we are to transmit next, mainly used for when we |
| 348 | * are transmitting the same one multiple times |
| 349 | */ |
| 350 | struct net_device *odev; /* The out-going device. Note that the device should |
| 351 | * have it's pg_info pointer pointing back to this |
| 352 | * device. This will be set when the user specifies |
| 353 | * the out-going device name (not when the inject is |
| 354 | * started as it used to do.) |
| 355 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | struct flow_state *flows; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 357 | unsigned cflows; /* Concurrent flows (config) */ |
| 358 | unsigned lflow; /* Flow length (config) */ |
| 359 | unsigned nflows; /* accumulated flows (stats) */ |
Jamal Hadi Salim | 007a531 | 2007-07-02 22:40:36 -0700 | [diff] [blame] | 360 | unsigned curfl; /* current sequenced flow (state)*/ |
Jamal Hadi Salim | a553e4a | 2007-07-02 22:41:59 -0700 | [diff] [blame] | 361 | #ifdef CONFIG_XFRM |
| 362 | __u8 ipsmode; /* IPSEC mode (config) */ |
| 363 | __u8 ipsproto; /* IPSEC type (config) */ |
| 364 | #endif |
Stephen Hemminger | 39df232 | 2007-03-04 16:11:51 -0800 | [diff] [blame] | 365 | char result[512]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | }; |
| 367 | |
| 368 | struct pktgen_hdr { |
Al Viro | 252e334 | 2006-11-14 20:48:11 -0800 | [diff] [blame] | 369 | __be32 pgh_magic; |
| 370 | __be32 seq_num; |
| 371 | __be32 tv_sec; |
| 372 | __be32 tv_usec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | }; |
| 374 | |
| 375 | struct pktgen_thread { |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 376 | spinlock_t if_lock; |
Luiz Capitulino | c26a801 | 2006-03-20 22:18:16 -0800 | [diff] [blame] | 377 | struct list_head if_list; /* All device here */ |
Luiz Capitulino | cdcdbe0 | 2006-03-20 22:16:40 -0800 | [diff] [blame] | 378 | struct list_head th_list; |
David S. Miller | ee74baa | 2007-01-01 20:51:53 -0800 | [diff] [blame] | 379 | struct task_struct *tsk; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 380 | char result[512]; |
| 381 | u32 max_before_softirq; /* We'll call do_softirq to prevent starvation. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 383 | /* Field for thread to receive "posted" events terminate, stop ifs etc. */ |
| 384 | |
| 385 | u32 control; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | int cpu; |
| 387 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 388 | wait_queue_head_t queue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | }; |
| 390 | |
| 391 | #define REMOVE 1 |
| 392 | #define FIND 0 |
| 393 | |
| 394 | /* This code works around the fact that do_div cannot handle two 64-bit |
| 395 | numbers, and regular 64-bit division doesn't work on x86 kernels. |
| 396 | --Ben |
| 397 | */ |
| 398 | |
| 399 | #define PG_DIV 0 |
| 400 | |
| 401 | /* This was emailed to LMKL by: Chris Caputo <ccaputo@alt.net> |
| 402 | * Function copied/adapted/optimized from: |
| 403 | * |
| 404 | * nemesis.sourceforge.net/browse/lib/static/intmath/ix86/intmath.c.html |
| 405 | * |
| 406 | * Copyright 1994, University of Cambridge Computer Laboratory |
| 407 | * All Rights Reserved. |
| 408 | * |
| 409 | */ |
Jesper Juhl | 77933d7 | 2005-07-27 11:46:09 -0700 | [diff] [blame] | 410 | static inline s64 divremdi3(s64 x, s64 y, int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | { |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 412 | u64 a = (x < 0) ? -x : x; |
| 413 | u64 b = (y < 0) ? -y : y; |
| 414 | u64 res = 0, d = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 416 | if (b > 0) { |
| 417 | while (b < a) { |
| 418 | b <<= 1; |
| 419 | d <<= 1; |
| 420 | } |
| 421 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 423 | do { |
| 424 | if (a >= b) { |
| 425 | a -= b; |
| 426 | res += d; |
| 427 | } |
| 428 | b >>= 1; |
| 429 | d >>= 1; |
| 430 | } |
| 431 | while (d); |
| 432 | |
| 433 | if (PG_DIV == type) { |
| 434 | return (((x ^ y) & (1ll << 63)) == 0) ? res : -(s64) res; |
| 435 | } else { |
| 436 | return ((x & (1ll << 63)) == 0) ? a : -(s64) a; |
| 437 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | /* End of hacks to deal with 64-bit math on x86 */ |
| 441 | |
Stephen Hemminger | b4099fa | 2005-10-14 15:32:22 -0700 | [diff] [blame] | 442 | /** Convert to milliseconds */ |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 443 | static inline __u64 tv_to_ms(const struct timeval *tv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | { |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 445 | __u64 ms = tv->tv_usec / 1000; |
| 446 | ms += (__u64) tv->tv_sec * (__u64) 1000; |
| 447 | return ms; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | } |
| 449 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | /** Convert to micro-seconds */ |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 451 | static inline __u64 tv_to_us(const struct timeval *tv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | { |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 453 | __u64 us = tv->tv_usec; |
| 454 | us += (__u64) tv->tv_sec * (__u64) 1000000; |
| 455 | return us; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | } |
| 457 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 458 | static inline __u64 pg_div(__u64 n, __u32 base) |
| 459 | { |
| 460 | __u64 tmp = n; |
| 461 | do_div(tmp, base); |
| 462 | /* printk("pktgen: pg_div, n: %llu base: %d rv: %llu\n", |
| 463 | n, base, tmp); */ |
| 464 | return tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | } |
| 466 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 467 | static inline __u64 pg_div64(__u64 n, __u64 base) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | { |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 469 | __u64 tmp = n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | /* |
Stephen Hemminger | b4099fa | 2005-10-14 15:32:22 -0700 | [diff] [blame] | 471 | * How do we know if the architecture we are running on |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | * supports division with 64 bit base? |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 473 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | */ |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 475 | #if defined(__sparc_v9__) || defined(__powerpc64__) || defined(__alpha__) || defined(__x86_64__) || defined(__ia64__) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 477 | do_div(tmp, base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | #else |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 479 | tmp = divremdi3(n, base, PG_DIV); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | #endif |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 481 | return tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | } |
| 483 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 484 | static inline __u64 getCurMs(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | { |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 486 | struct timeval tv; |
| 487 | do_gettimeofday(&tv); |
| 488 | return tv_to_ms(&tv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | } |
| 490 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 491 | static inline __u64 getCurUs(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | { |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 493 | struct timeval tv; |
| 494 | do_gettimeofday(&tv); |
| 495 | return tv_to_us(&tv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | } |
| 497 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 498 | static inline __u64 tv_diff(const struct timeval *a, const struct timeval *b) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | { |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 500 | return tv_to_us(a) - tv_to_us(b); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | } |
| 502 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | /* old include end */ |
| 504 | |
| 505 | static char version[] __initdata = VERSION; |
| 506 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 507 | static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *i); |
| 508 | static int pktgen_add_device(struct pktgen_thread *t, const char *ifname); |
| 509 | static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t, |
| 510 | const char *ifname); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | static int pktgen_device_event(struct notifier_block *, unsigned long, void *); |
| 512 | static void pktgen_run_all_threads(void); |
| 513 | static void pktgen_stop_all_threads_ifs(void); |
| 514 | static int pktgen_stop_device(struct pktgen_dev *pkt_dev); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 515 | static void pktgen_stop(struct pktgen_thread *t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | static void pktgen_clear_counters(struct pktgen_dev *pkt_dev); |
Stephen Hemminger | 39df232 | 2007-03-04 16:11:51 -0800 | [diff] [blame] | 517 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 518 | static unsigned int scan_ip6(const char *s, char ip[16]); |
| 519 | static unsigned int fmt_ip6(char *s, const char ip[16]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | |
| 521 | /* Module parameters, defaults. */ |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 522 | static int pg_count_d = 1000; /* 1000 pkts by default */ |
Jaco Kroon | f34fbb9 | 2005-12-22 12:51:46 -0800 | [diff] [blame] | 523 | static int pg_delay_d; |
| 524 | static int pg_clone_skb_d; |
| 525 | static int debug; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | |
Luiz Capitulino | 222fa07 | 2006-03-20 22:24:27 -0800 | [diff] [blame] | 527 | static DEFINE_MUTEX(pktgen_thread_lock); |
Luiz Capitulino | cdcdbe0 | 2006-03-20 22:16:40 -0800 | [diff] [blame] | 528 | static LIST_HEAD(pktgen_threads); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | static struct notifier_block pktgen_notifier_block = { |
| 531 | .notifier_call = pktgen_device_event, |
| 532 | }; |
| 533 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | /* |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 535 | * /proc handling functions |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | * |
| 537 | */ |
| 538 | |
Stephen Hemminger | d50a6b5 | 2005-10-14 15:42:33 -0700 | [diff] [blame] | 539 | static int pgctrl_show(struct seq_file *seq, void *v) |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 540 | { |
Stephen Hemminger | d50a6b5 | 2005-10-14 15:42:33 -0700 | [diff] [blame] | 541 | seq_puts(seq, VERSION); |
| 542 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | } |
| 544 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 545 | static ssize_t pgctrl_write(struct file *file, const char __user * buf, |
| 546 | size_t count, loff_t * ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | int err = 0; |
Stephen Hemminger | d50a6b5 | 2005-10-14 15:42:33 -0700 | [diff] [blame] | 549 | char data[128]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 551 | if (!capable(CAP_NET_ADMIN)) { |
| 552 | err = -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | goto out; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 554 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | |
Stephen Hemminger | d50a6b5 | 2005-10-14 15:42:33 -0700 | [diff] [blame] | 556 | if (count > sizeof(data)) |
| 557 | count = sizeof(data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | if (copy_from_user(data, buf, count)) { |
Stephen Hemminger | b4099fa | 2005-10-14 15:32:22 -0700 | [diff] [blame] | 560 | err = -EFAULT; |
Stephen Hemminger | d50a6b5 | 2005-10-14 15:42:33 -0700 | [diff] [blame] | 561 | goto out; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 562 | } |
| 563 | data[count - 1] = 0; /* Make string */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 565 | if (!strcmp(data, "stop")) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | pktgen_stop_all_threads_ifs(); |
| 567 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 568 | else if (!strcmp(data, "start")) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | pktgen_run_all_threads(); |
| 570 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 571 | else |
David S. Miller | 25a8b25 | 2007-07-30 16:11:48 -0700 | [diff] [blame] | 572 | printk(KERN_WARNING "pktgen: Unknown command: %s\n", data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | |
| 574 | err = count; |
| 575 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 576 | out: |
| 577 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | } |
| 579 | |
Stephen Hemminger | d50a6b5 | 2005-10-14 15:42:33 -0700 | [diff] [blame] | 580 | static int pgctrl_open(struct inode *inode, struct file *file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | { |
Stephen Hemminger | d50a6b5 | 2005-10-14 15:42:33 -0700 | [diff] [blame] | 582 | return single_open(file, pgctrl_show, PDE(inode)->data); |
| 583 | } |
| 584 | |
Arjan van de Ven | 9a32144 | 2007-02-12 00:55:35 -0800 | [diff] [blame] | 585 | static const struct file_operations pktgen_fops = { |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 586 | .owner = THIS_MODULE, |
| 587 | .open = pgctrl_open, |
| 588 | .read = seq_read, |
| 589 | .llseek = seq_lseek, |
| 590 | .write = pgctrl_write, |
| 591 | .release = single_release, |
Stephen Hemminger | d50a6b5 | 2005-10-14 15:42:33 -0700 | [diff] [blame] | 592 | }; |
| 593 | |
| 594 | static int pktgen_if_show(struct seq_file *seq, void *v) |
| 595 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | int i; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 597 | struct pktgen_dev *pkt_dev = seq->private; |
| 598 | __u64 sa; |
| 599 | __u64 stopped; |
| 600 | __u64 now = getCurUs(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 602 | seq_printf(seq, |
| 603 | "Params: count %llu min_pkt_size: %u max_pkt_size: %u\n", |
| 604 | (unsigned long long)pkt_dev->count, pkt_dev->min_pkt_size, |
| 605 | pkt_dev->max_pkt_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 607 | seq_printf(seq, |
| 608 | " frags: %d delay: %u clone_skb: %d ifname: %s\n", |
| 609 | pkt_dev->nfrags, |
| 610 | 1000 * pkt_dev->delay_us + pkt_dev->delay_ns, |
Stephen Hemminger | 39df232 | 2007-03-04 16:11:51 -0800 | [diff] [blame] | 611 | pkt_dev->clone_skb, pkt_dev->odev->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 613 | seq_printf(seq, " flows: %u flowlen: %u\n", pkt_dev->cflows, |
| 614 | pkt_dev->lflow); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 616 | if (pkt_dev->flags & F_IPV6) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | char b1[128], b2[128], b3[128]; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 618 | fmt_ip6(b1, pkt_dev->in6_saddr.s6_addr); |
| 619 | fmt_ip6(b2, pkt_dev->min_in6_saddr.s6_addr); |
| 620 | fmt_ip6(b3, pkt_dev->max_in6_saddr.s6_addr); |
| 621 | seq_printf(seq, |
| 622 | " saddr: %s min_saddr: %s max_saddr: %s\n", b1, |
| 623 | b2, b3); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 625 | fmt_ip6(b1, pkt_dev->in6_daddr.s6_addr); |
| 626 | fmt_ip6(b2, pkt_dev->min_in6_daddr.s6_addr); |
| 627 | fmt_ip6(b3, pkt_dev->max_in6_daddr.s6_addr); |
| 628 | seq_printf(seq, |
| 629 | " daddr: %s min_daddr: %s max_daddr: %s\n", b1, |
| 630 | b2, b3); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 632 | } else |
| 633 | seq_printf(seq, |
| 634 | " dst_min: %s dst_max: %s\n src_min: %s src_max: %s\n", |
| 635 | pkt_dev->dst_min, pkt_dev->dst_max, pkt_dev->src_min, |
| 636 | pkt_dev->src_max); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | |
Stephen Hemminger | d50a6b5 | 2005-10-14 15:42:33 -0700 | [diff] [blame] | 638 | seq_puts(seq, " src_mac: "); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | |
Kris Katterjohn | f404e9a | 2006-01-17 13:04:57 -0800 | [diff] [blame] | 640 | if (is_zero_ether_addr(pkt_dev->src_mac)) |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 641 | for (i = 0; i < 6; i++) |
| 642 | seq_printf(seq, "%02X%s", pkt_dev->odev->dev_addr[i], |
| 643 | i == 5 ? " " : ":"); |
| 644 | else |
| 645 | for (i = 0; i < 6; i++) |
| 646 | seq_printf(seq, "%02X%s", pkt_dev->src_mac[i], |
| 647 | i == 5 ? " " : ":"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 649 | seq_printf(seq, "dst_mac: "); |
| 650 | for (i = 0; i < 6; i++) |
| 651 | seq_printf(seq, "%02X%s", pkt_dev->dst_mac[i], |
| 652 | i == 5 ? "\n" : ":"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 654 | seq_printf(seq, |
| 655 | " udp_src_min: %d udp_src_max: %d udp_dst_min: %d udp_dst_max: %d\n", |
| 656 | pkt_dev->udp_src_min, pkt_dev->udp_src_max, |
| 657 | pkt_dev->udp_dst_min, pkt_dev->udp_dst_max); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 659 | seq_printf(seq, |
Steven Whitehouse | ca6549a | 2006-03-23 01:10:26 -0800 | [diff] [blame] | 660 | " src_mac_count: %d dst_mac_count: %d\n", |
Stephen Hemminger | d50a6b5 | 2005-10-14 15:42:33 -0700 | [diff] [blame] | 661 | pkt_dev->src_mac_count, pkt_dev->dst_mac_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | |
Steven Whitehouse | ca6549a | 2006-03-23 01:10:26 -0800 | [diff] [blame] | 663 | if (pkt_dev->nr_labels) { |
| 664 | unsigned i; |
| 665 | seq_printf(seq, " mpls: "); |
Stephen Hemminger | e71a478 | 2007-04-10 20:10:33 -0700 | [diff] [blame] | 666 | for (i = 0; i < pkt_dev->nr_labels; i++) |
Steven Whitehouse | ca6549a | 2006-03-23 01:10:26 -0800 | [diff] [blame] | 667 | seq_printf(seq, "%08x%s", ntohl(pkt_dev->labels[i]), |
| 668 | i == pkt_dev->nr_labels-1 ? "\n" : ", "); |
| 669 | } |
| 670 | |
Francesco Fondelli | 34954dd | 2006-09-27 16:30:44 -0700 | [diff] [blame] | 671 | if (pkt_dev->vlan_id != 0xffff) { |
| 672 | seq_printf(seq, " vlan_id: %u vlan_p: %u vlan_cfi: %u\n", |
| 673 | pkt_dev->vlan_id, pkt_dev->vlan_p, pkt_dev->vlan_cfi); |
| 674 | } |
| 675 | |
| 676 | if (pkt_dev->svlan_id != 0xffff) { |
| 677 | seq_printf(seq, " svlan_id: %u vlan_p: %u vlan_cfi: %u\n", |
| 678 | pkt_dev->svlan_id, pkt_dev->svlan_p, pkt_dev->svlan_cfi); |
| 679 | } |
| 680 | |
Francesco Fondelli | 1ca7768 | 2006-09-27 16:32:03 -0700 | [diff] [blame] | 681 | if (pkt_dev->tos) { |
| 682 | seq_printf(seq, " tos: 0x%02x\n", pkt_dev->tos); |
| 683 | } |
| 684 | |
| 685 | if (pkt_dev->traffic_class) { |
| 686 | seq_printf(seq, " traffic_class: 0x%02x\n", pkt_dev->traffic_class); |
| 687 | } |
| 688 | |
Steven Whitehouse | ca6549a | 2006-03-23 01:10:26 -0800 | [diff] [blame] | 689 | seq_printf(seq, " Flags: "); |
| 690 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 691 | if (pkt_dev->flags & F_IPV6) |
| 692 | seq_printf(seq, "IPV6 "); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 694 | if (pkt_dev->flags & F_IPSRC_RND) |
| 695 | seq_printf(seq, "IPSRC_RND "); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 697 | if (pkt_dev->flags & F_IPDST_RND) |
| 698 | seq_printf(seq, "IPDST_RND "); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 700 | if (pkt_dev->flags & F_TXSIZE_RND) |
| 701 | seq_printf(seq, "TXSIZE_RND "); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 703 | if (pkt_dev->flags & F_UDPSRC_RND) |
| 704 | seq_printf(seq, "UDPSRC_RND "); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 706 | if (pkt_dev->flags & F_UDPDST_RND) |
| 707 | seq_printf(seq, "UDPDST_RND "); |
| 708 | |
Steven Whitehouse | ca6549a | 2006-03-23 01:10:26 -0800 | [diff] [blame] | 709 | if (pkt_dev->flags & F_MPLS_RND) |
| 710 | seq_printf(seq, "MPLS_RND "); |
| 711 | |
Jamal Hadi Salim | 007a531 | 2007-07-02 22:40:36 -0700 | [diff] [blame] | 712 | if (pkt_dev->cflows) { |
| 713 | if (pkt_dev->flags & F_FLOW_SEQ) |
| 714 | seq_printf(seq, "FLOW_SEQ "); /*in sequence flows*/ |
| 715 | else |
| 716 | seq_printf(seq, "FLOW_RND "); |
| 717 | } |
| 718 | |
Jamal Hadi Salim | a553e4a | 2007-07-02 22:41:59 -0700 | [diff] [blame] | 719 | #ifdef CONFIG_XFRM |
| 720 | if (pkt_dev->flags & F_IPSEC_ON) |
| 721 | seq_printf(seq, "IPSEC "); |
| 722 | #endif |
| 723 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 724 | if (pkt_dev->flags & F_MACSRC_RND) |
| 725 | seq_printf(seq, "MACSRC_RND "); |
| 726 | |
| 727 | if (pkt_dev->flags & F_MACDST_RND) |
| 728 | seq_printf(seq, "MACDST_RND "); |
| 729 | |
Francesco Fondelli | 34954dd | 2006-09-27 16:30:44 -0700 | [diff] [blame] | 730 | if (pkt_dev->flags & F_VID_RND) |
| 731 | seq_printf(seq, "VID_RND "); |
| 732 | |
| 733 | if (pkt_dev->flags & F_SVID_RND) |
| 734 | seq_printf(seq, "SVID_RND "); |
| 735 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 736 | seq_puts(seq, "\n"); |
| 737 | |
| 738 | sa = pkt_dev->started_at; |
| 739 | stopped = pkt_dev->stopped_at; |
| 740 | if (pkt_dev->running) |
| 741 | stopped = now; /* not really stopped, more like last-running-at */ |
| 742 | |
| 743 | seq_printf(seq, |
| 744 | "Current:\n pkts-sofar: %llu errors: %llu\n started: %lluus stopped: %lluus idle: %lluus\n", |
| 745 | (unsigned long long)pkt_dev->sofar, |
| 746 | (unsigned long long)pkt_dev->errors, (unsigned long long)sa, |
| 747 | (unsigned long long)stopped, |
| 748 | (unsigned long long)pkt_dev->idle_acc); |
| 749 | |
| 750 | seq_printf(seq, |
| 751 | " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n", |
Stephen Hemminger | d50a6b5 | 2005-10-14 15:42:33 -0700 | [diff] [blame] | 752 | pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset, |
| 753 | pkt_dev->cur_src_mac_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 755 | if (pkt_dev->flags & F_IPV6) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | char b1[128], b2[128]; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 757 | fmt_ip6(b1, pkt_dev->cur_in6_daddr.s6_addr); |
| 758 | fmt_ip6(b2, pkt_dev->cur_in6_saddr.s6_addr); |
| 759 | seq_printf(seq, " cur_saddr: %s cur_daddr: %s\n", b2, b1); |
| 760 | } else |
| 761 | seq_printf(seq, " cur_saddr: 0x%x cur_daddr: 0x%x\n", |
Stephen Hemminger | d50a6b5 | 2005-10-14 15:42:33 -0700 | [diff] [blame] | 762 | pkt_dev->cur_saddr, pkt_dev->cur_daddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 764 | seq_printf(seq, " cur_udp_dst: %d cur_udp_src: %d\n", |
Stephen Hemminger | d50a6b5 | 2005-10-14 15:42:33 -0700 | [diff] [blame] | 765 | pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 767 | seq_printf(seq, " flows: %u\n", pkt_dev->nflows); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | |
| 769 | if (pkt_dev->result[0]) |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 770 | seq_printf(seq, "Result: %s\n", pkt_dev->result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | else |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 772 | seq_printf(seq, "Result: Idle\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | |
Stephen Hemminger | d50a6b5 | 2005-10-14 15:42:33 -0700 | [diff] [blame] | 774 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | } |
| 776 | |
Steven Whitehouse | ca6549a | 2006-03-23 01:10:26 -0800 | [diff] [blame] | 777 | |
Francesco Fondelli | 1ca7768 | 2006-09-27 16:32:03 -0700 | [diff] [blame] | 778 | static int hex32_arg(const char __user *user_buffer, unsigned long maxlen, __u32 *num) |
Steven Whitehouse | ca6549a | 2006-03-23 01:10:26 -0800 | [diff] [blame] | 779 | { |
| 780 | int i = 0; |
| 781 | *num = 0; |
| 782 | |
Stephen Hemminger | e71a478 | 2007-04-10 20:10:33 -0700 | [diff] [blame] | 783 | for (; i < maxlen; i++) { |
Steven Whitehouse | ca6549a | 2006-03-23 01:10:26 -0800 | [diff] [blame] | 784 | char c; |
| 785 | *num <<= 4; |
| 786 | if (get_user(c, &user_buffer[i])) |
| 787 | return -EFAULT; |
| 788 | if ((c >= '0') && (c <= '9')) |
| 789 | *num |= c - '0'; |
| 790 | else if ((c >= 'a') && (c <= 'f')) |
| 791 | *num |= c - 'a' + 10; |
| 792 | else if ((c >= 'A') && (c <= 'F')) |
| 793 | *num |= c - 'A' + 10; |
| 794 | else |
| 795 | break; |
| 796 | } |
| 797 | return i; |
| 798 | } |
| 799 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 800 | static int count_trail_chars(const char __user * user_buffer, |
| 801 | unsigned int maxlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | { |
| 803 | int i; |
| 804 | |
| 805 | for (i = 0; i < maxlen; i++) { |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 806 | char c; |
| 807 | if (get_user(c, &user_buffer[i])) |
| 808 | return -EFAULT; |
| 809 | switch (c) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | case '\"': |
| 811 | case '\n': |
| 812 | case '\r': |
| 813 | case '\t': |
| 814 | case ' ': |
| 815 | case '=': |
| 816 | break; |
| 817 | default: |
| 818 | goto done; |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 819 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | } |
| 821 | done: |
| 822 | return i; |
| 823 | } |
| 824 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 825 | static unsigned long num_arg(const char __user * user_buffer, |
| 826 | unsigned long maxlen, unsigned long *num) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | { |
| 828 | int i = 0; |
| 829 | *num = 0; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 830 | |
| 831 | for (; i < maxlen; i++) { |
| 832 | char c; |
| 833 | if (get_user(c, &user_buffer[i])) |
| 834 | return -EFAULT; |
| 835 | if ((c >= '0') && (c <= '9')) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 836 | *num *= 10; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 837 | *num += c - '0'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | } else |
| 839 | break; |
| 840 | } |
| 841 | return i; |
| 842 | } |
| 843 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 844 | static int strn_len(const char __user * user_buffer, unsigned int maxlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | { |
| 846 | int i = 0; |
| 847 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 848 | for (; i < maxlen; i++) { |
| 849 | char c; |
| 850 | if (get_user(c, &user_buffer[i])) |
| 851 | return -EFAULT; |
| 852 | switch (c) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | case '\"': |
| 854 | case '\n': |
| 855 | case '\r': |
| 856 | case '\t': |
| 857 | case ' ': |
| 858 | goto done_str; |
| 859 | break; |
| 860 | default: |
| 861 | break; |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 862 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 863 | } |
| 864 | done_str: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 865 | return i; |
| 866 | } |
| 867 | |
Steven Whitehouse | ca6549a | 2006-03-23 01:10:26 -0800 | [diff] [blame] | 868 | static ssize_t get_labels(const char __user *buffer, struct pktgen_dev *pkt_dev) |
| 869 | { |
| 870 | unsigned n = 0; |
| 871 | char c; |
| 872 | ssize_t i = 0; |
| 873 | int len; |
| 874 | |
| 875 | pkt_dev->nr_labels = 0; |
| 876 | do { |
| 877 | __u32 tmp; |
Francesco Fondelli | 1ca7768 | 2006-09-27 16:32:03 -0700 | [diff] [blame] | 878 | len = hex32_arg(&buffer[i], 8, &tmp); |
Steven Whitehouse | ca6549a | 2006-03-23 01:10:26 -0800 | [diff] [blame] | 879 | if (len <= 0) |
| 880 | return len; |
| 881 | pkt_dev->labels[n] = htonl(tmp); |
| 882 | if (pkt_dev->labels[n] & MPLS_STACK_BOTTOM) |
| 883 | pkt_dev->flags |= F_MPLS_RND; |
| 884 | i += len; |
| 885 | if (get_user(c, &buffer[i])) |
| 886 | return -EFAULT; |
| 887 | i++; |
| 888 | n++; |
| 889 | if (n >= MAX_MPLS_LABELS) |
| 890 | return -E2BIG; |
Stephen Hemminger | e71a478 | 2007-04-10 20:10:33 -0700 | [diff] [blame] | 891 | } while (c == ','); |
Steven Whitehouse | ca6549a | 2006-03-23 01:10:26 -0800 | [diff] [blame] | 892 | |
| 893 | pkt_dev->nr_labels = n; |
| 894 | return i; |
| 895 | } |
| 896 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 897 | static ssize_t pktgen_if_write(struct file *file, |
| 898 | const char __user * user_buffer, size_t count, |
| 899 | loff_t * offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | { |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 901 | struct seq_file *seq = (struct seq_file *)file->private_data; |
| 902 | struct pktgen_dev *pkt_dev = seq->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | int i = 0, max, len; |
| 904 | char name[16], valstr[32]; |
| 905 | unsigned long value = 0; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 906 | char *pg_result = NULL; |
| 907 | int tmp = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 908 | char buf[128]; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 909 | |
| 910 | pg_result = &(pkt_dev->result[0]); |
| 911 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 912 | if (count < 1) { |
David S. Miller | 25a8b25 | 2007-07-30 16:11:48 -0700 | [diff] [blame] | 913 | printk(KERN_WARNING "pktgen: wrong command format\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 | return -EINVAL; |
| 915 | } |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 916 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | max = count - i; |
| 918 | tmp = count_trail_chars(&user_buffer[i], max); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 919 | if (tmp < 0) { |
David S. Miller | 25a8b25 | 2007-07-30 16:11:48 -0700 | [diff] [blame] | 920 | printk(KERN_WARNING "pktgen: illegal format\n"); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 921 | return tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 922 | } |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 923 | i += tmp; |
| 924 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | /* Read variable name */ |
| 926 | |
| 927 | len = strn_len(&user_buffer[i], sizeof(name) - 1); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 928 | if (len < 0) { |
| 929 | return len; |
| 930 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | memset(name, 0, sizeof(name)); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 932 | if (copy_from_user(name, &user_buffer[i], len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 933 | return -EFAULT; |
| 934 | i += len; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 935 | |
| 936 | max = count - i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | len = count_trail_chars(&user_buffer[i], max); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 938 | if (len < 0) |
| 939 | return len; |
| 940 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 941 | i += len; |
| 942 | |
| 943 | if (debug) { |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 944 | char tb[count + 1]; |
| 945 | if (copy_from_user(tb, user_buffer, count)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | return -EFAULT; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 947 | tb[count] = 0; |
David S. Miller | 25a8b25 | 2007-07-30 16:11:48 -0700 | [diff] [blame] | 948 | printk(KERN_DEBUG "pktgen: %s,%lu buffer -:%s:-\n", name, |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 949 | (unsigned long)count, tb); |
| 950 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | |
| 952 | if (!strcmp(name, "min_pkt_size")) { |
| 953 | len = num_arg(&user_buffer[i], 10, &value); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 954 | if (len < 0) { |
| 955 | return len; |
| 956 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | i += len; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 958 | if (value < 14 + 20 + 8) |
| 959 | value = 14 + 20 + 8; |
| 960 | if (value != pkt_dev->min_pkt_size) { |
| 961 | pkt_dev->min_pkt_size = value; |
| 962 | pkt_dev->cur_pkt_size = value; |
| 963 | } |
| 964 | sprintf(pg_result, "OK: min_pkt_size=%u", |
| 965 | pkt_dev->min_pkt_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 966 | return count; |
| 967 | } |
| 968 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 969 | if (!strcmp(name, "max_pkt_size")) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | len = num_arg(&user_buffer[i], 10, &value); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 971 | if (len < 0) { |
| 972 | return len; |
| 973 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 974 | i += len; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 975 | if (value < 14 + 20 + 8) |
| 976 | value = 14 + 20 + 8; |
| 977 | if (value != pkt_dev->max_pkt_size) { |
| 978 | pkt_dev->max_pkt_size = value; |
| 979 | pkt_dev->cur_pkt_size = value; |
| 980 | } |
| 981 | sprintf(pg_result, "OK: max_pkt_size=%u", |
| 982 | pkt_dev->max_pkt_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 983 | return count; |
| 984 | } |
| 985 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 986 | /* Shortcut for min = max */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 987 | |
| 988 | if (!strcmp(name, "pkt_size")) { |
| 989 | len = num_arg(&user_buffer[i], 10, &value); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 990 | if (len < 0) { |
| 991 | return len; |
| 992 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | i += len; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 994 | if (value < 14 + 20 + 8) |
| 995 | value = 14 + 20 + 8; |
| 996 | if (value != pkt_dev->min_pkt_size) { |
| 997 | pkt_dev->min_pkt_size = value; |
| 998 | pkt_dev->max_pkt_size = value; |
| 999 | pkt_dev->cur_pkt_size = value; |
| 1000 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1001 | sprintf(pg_result, "OK: pkt_size=%u", pkt_dev->min_pkt_size); |
| 1002 | return count; |
| 1003 | } |
| 1004 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1005 | if (!strcmp(name, "debug")) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1006 | len = num_arg(&user_buffer[i], 10, &value); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1007 | if (len < 0) { |
| 1008 | return len; |
| 1009 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1010 | i += len; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1011 | debug = value; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1012 | sprintf(pg_result, "OK: debug=%u", debug); |
| 1013 | return count; |
| 1014 | } |
| 1015 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1016 | if (!strcmp(name, "frags")) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1017 | len = num_arg(&user_buffer[i], 10, &value); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1018 | if (len < 0) { |
| 1019 | return len; |
| 1020 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1021 | i += len; |
| 1022 | pkt_dev->nfrags = value; |
| 1023 | sprintf(pg_result, "OK: frags=%u", pkt_dev->nfrags); |
| 1024 | return count; |
| 1025 | } |
| 1026 | if (!strcmp(name, "delay")) { |
| 1027 | len = num_arg(&user_buffer[i], 10, &value); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1028 | if (len < 0) { |
| 1029 | return len; |
| 1030 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1031 | i += len; |
| 1032 | if (value == 0x7FFFFFFF) { |
| 1033 | pkt_dev->delay_us = 0x7FFFFFFF; |
| 1034 | pkt_dev->delay_ns = 0; |
| 1035 | } else { |
| 1036 | pkt_dev->delay_us = value / 1000; |
| 1037 | pkt_dev->delay_ns = value % 1000; |
| 1038 | } |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1039 | sprintf(pg_result, "OK: delay=%u", |
| 1040 | 1000 * pkt_dev->delay_us + pkt_dev->delay_ns); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1041 | return count; |
| 1042 | } |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1043 | if (!strcmp(name, "udp_src_min")) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1044 | len = num_arg(&user_buffer[i], 10, &value); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1045 | if (len < 0) { |
| 1046 | return len; |
| 1047 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1048 | i += len; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1049 | if (value != pkt_dev->udp_src_min) { |
| 1050 | pkt_dev->udp_src_min = value; |
| 1051 | pkt_dev->cur_udp_src = value; |
| 1052 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1053 | sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min); |
| 1054 | return count; |
| 1055 | } |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1056 | if (!strcmp(name, "udp_dst_min")) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1057 | len = num_arg(&user_buffer[i], 10, &value); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1058 | if (len < 0) { |
| 1059 | return len; |
| 1060 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1061 | i += len; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1062 | if (value != pkt_dev->udp_dst_min) { |
| 1063 | pkt_dev->udp_dst_min = value; |
| 1064 | pkt_dev->cur_udp_dst = value; |
| 1065 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1066 | sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min); |
| 1067 | return count; |
| 1068 | } |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1069 | if (!strcmp(name, "udp_src_max")) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1070 | len = num_arg(&user_buffer[i], 10, &value); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1071 | if (len < 0) { |
| 1072 | return len; |
| 1073 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1074 | i += len; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1075 | if (value != pkt_dev->udp_src_max) { |
| 1076 | pkt_dev->udp_src_max = value; |
| 1077 | pkt_dev->cur_udp_src = value; |
| 1078 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1079 | sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max); |
| 1080 | return count; |
| 1081 | } |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1082 | if (!strcmp(name, "udp_dst_max")) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1083 | len = num_arg(&user_buffer[i], 10, &value); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1084 | if (len < 0) { |
| 1085 | return len; |
| 1086 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1087 | i += len; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1088 | if (value != pkt_dev->udp_dst_max) { |
| 1089 | pkt_dev->udp_dst_max = value; |
| 1090 | pkt_dev->cur_udp_dst = value; |
| 1091 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1092 | sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max); |
| 1093 | return count; |
| 1094 | } |
| 1095 | if (!strcmp(name, "clone_skb")) { |
| 1096 | len = num_arg(&user_buffer[i], 10, &value); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1097 | if (len < 0) { |
| 1098 | return len; |
| 1099 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1100 | i += len; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1101 | pkt_dev->clone_skb = value; |
| 1102 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1103 | sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb); |
| 1104 | return count; |
| 1105 | } |
| 1106 | if (!strcmp(name, "count")) { |
| 1107 | len = num_arg(&user_buffer[i], 10, &value); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1108 | if (len < 0) { |
| 1109 | return len; |
| 1110 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1111 | i += len; |
| 1112 | pkt_dev->count = value; |
| 1113 | sprintf(pg_result, "OK: count=%llu", |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1114 | (unsigned long long)pkt_dev->count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1115 | return count; |
| 1116 | } |
| 1117 | if (!strcmp(name, "src_mac_count")) { |
| 1118 | len = num_arg(&user_buffer[i], 10, &value); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1119 | if (len < 0) { |
| 1120 | return len; |
| 1121 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1122 | i += len; |
| 1123 | if (pkt_dev->src_mac_count != value) { |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1124 | pkt_dev->src_mac_count = value; |
| 1125 | pkt_dev->cur_src_mac_offset = 0; |
| 1126 | } |
| 1127 | sprintf(pg_result, "OK: src_mac_count=%d", |
| 1128 | pkt_dev->src_mac_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1129 | return count; |
| 1130 | } |
| 1131 | if (!strcmp(name, "dst_mac_count")) { |
| 1132 | len = num_arg(&user_buffer[i], 10, &value); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1133 | if (len < 0) { |
| 1134 | return len; |
| 1135 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1136 | i += len; |
| 1137 | if (pkt_dev->dst_mac_count != value) { |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1138 | pkt_dev->dst_mac_count = value; |
| 1139 | pkt_dev->cur_dst_mac_offset = 0; |
| 1140 | } |
| 1141 | sprintf(pg_result, "OK: dst_mac_count=%d", |
| 1142 | pkt_dev->dst_mac_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1143 | return count; |
| 1144 | } |
| 1145 | if (!strcmp(name, "flag")) { |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1146 | char f[32]; |
| 1147 | memset(f, 0, 32); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1148 | len = strn_len(&user_buffer[i], sizeof(f) - 1); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1149 | if (len < 0) { |
| 1150 | return len; |
| 1151 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1152 | if (copy_from_user(f, &user_buffer[i], len)) |
| 1153 | return -EFAULT; |
| 1154 | i += len; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1155 | if (strcmp(f, "IPSRC_RND") == 0) |
| 1156 | pkt_dev->flags |= F_IPSRC_RND; |
| 1157 | |
| 1158 | else if (strcmp(f, "!IPSRC_RND") == 0) |
| 1159 | pkt_dev->flags &= ~F_IPSRC_RND; |
| 1160 | |
| 1161 | else if (strcmp(f, "TXSIZE_RND") == 0) |
| 1162 | pkt_dev->flags |= F_TXSIZE_RND; |
| 1163 | |
| 1164 | else if (strcmp(f, "!TXSIZE_RND") == 0) |
| 1165 | pkt_dev->flags &= ~F_TXSIZE_RND; |
| 1166 | |
| 1167 | else if (strcmp(f, "IPDST_RND") == 0) |
| 1168 | pkt_dev->flags |= F_IPDST_RND; |
| 1169 | |
| 1170 | else if (strcmp(f, "!IPDST_RND") == 0) |
| 1171 | pkt_dev->flags &= ~F_IPDST_RND; |
| 1172 | |
| 1173 | else if (strcmp(f, "UDPSRC_RND") == 0) |
| 1174 | pkt_dev->flags |= F_UDPSRC_RND; |
| 1175 | |
| 1176 | else if (strcmp(f, "!UDPSRC_RND") == 0) |
| 1177 | pkt_dev->flags &= ~F_UDPSRC_RND; |
| 1178 | |
| 1179 | else if (strcmp(f, "UDPDST_RND") == 0) |
| 1180 | pkt_dev->flags |= F_UDPDST_RND; |
| 1181 | |
| 1182 | else if (strcmp(f, "!UDPDST_RND") == 0) |
| 1183 | pkt_dev->flags &= ~F_UDPDST_RND; |
| 1184 | |
| 1185 | else if (strcmp(f, "MACSRC_RND") == 0) |
| 1186 | pkt_dev->flags |= F_MACSRC_RND; |
| 1187 | |
| 1188 | else if (strcmp(f, "!MACSRC_RND") == 0) |
| 1189 | pkt_dev->flags &= ~F_MACSRC_RND; |
| 1190 | |
| 1191 | else if (strcmp(f, "MACDST_RND") == 0) |
| 1192 | pkt_dev->flags |= F_MACDST_RND; |
| 1193 | |
| 1194 | else if (strcmp(f, "!MACDST_RND") == 0) |
| 1195 | pkt_dev->flags &= ~F_MACDST_RND; |
| 1196 | |
Steven Whitehouse | ca6549a | 2006-03-23 01:10:26 -0800 | [diff] [blame] | 1197 | else if (strcmp(f, "MPLS_RND") == 0) |
| 1198 | pkt_dev->flags |= F_MPLS_RND; |
| 1199 | |
| 1200 | else if (strcmp(f, "!MPLS_RND") == 0) |
| 1201 | pkt_dev->flags &= ~F_MPLS_RND; |
| 1202 | |
Francesco Fondelli | 34954dd | 2006-09-27 16:30:44 -0700 | [diff] [blame] | 1203 | else if (strcmp(f, "VID_RND") == 0) |
| 1204 | pkt_dev->flags |= F_VID_RND; |
| 1205 | |
| 1206 | else if (strcmp(f, "!VID_RND") == 0) |
| 1207 | pkt_dev->flags &= ~F_VID_RND; |
| 1208 | |
| 1209 | else if (strcmp(f, "SVID_RND") == 0) |
| 1210 | pkt_dev->flags |= F_SVID_RND; |
| 1211 | |
| 1212 | else if (strcmp(f, "!SVID_RND") == 0) |
| 1213 | pkt_dev->flags &= ~F_SVID_RND; |
| 1214 | |
Jamal Hadi Salim | 007a531 | 2007-07-02 22:40:36 -0700 | [diff] [blame] | 1215 | else if (strcmp(f, "FLOW_SEQ") == 0) |
| 1216 | pkt_dev->flags |= F_FLOW_SEQ; |
| 1217 | |
Jamal Hadi Salim | a553e4a | 2007-07-02 22:41:59 -0700 | [diff] [blame] | 1218 | #ifdef CONFIG_XFRM |
| 1219 | else if (strcmp(f, "IPSEC") == 0) |
| 1220 | pkt_dev->flags |= F_IPSEC_ON; |
| 1221 | #endif |
| 1222 | |
Francesco Fondelli | 1ca7768 | 2006-09-27 16:32:03 -0700 | [diff] [blame] | 1223 | else if (strcmp(f, "!IPV6") == 0) |
| 1224 | pkt_dev->flags &= ~F_IPV6; |
| 1225 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1226 | else { |
| 1227 | sprintf(pg_result, |
| 1228 | "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s", |
| 1229 | f, |
Francesco Fondelli | 1ca7768 | 2006-09-27 16:32:03 -0700 | [diff] [blame] | 1230 | "IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, " |
Jamal Hadi Salim | a553e4a | 2007-07-02 22:41:59 -0700 | [diff] [blame] | 1231 | "MACSRC_RND, MACDST_RND, TXSIZE_RND, IPV6, MPLS_RND, VID_RND, SVID_RND, FLOW_SEQ, IPSEC\n"); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1232 | return count; |
| 1233 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1234 | sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags); |
| 1235 | return count; |
| 1236 | } |
| 1237 | if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) { |
| 1238 | len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1239 | if (len < 0) { |
| 1240 | return len; |
| 1241 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1242 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1243 | if (copy_from_user(buf, &user_buffer[i], len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1244 | return -EFAULT; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1245 | buf[len] = 0; |
| 1246 | if (strcmp(buf, pkt_dev->dst_min) != 0) { |
| 1247 | memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min)); |
| 1248 | strncpy(pkt_dev->dst_min, buf, len); |
| 1249 | pkt_dev->daddr_min = in_aton(pkt_dev->dst_min); |
| 1250 | pkt_dev->cur_daddr = pkt_dev->daddr_min; |
| 1251 | } |
| 1252 | if (debug) |
David S. Miller | 25a8b25 | 2007-07-30 16:11:48 -0700 | [diff] [blame] | 1253 | printk(KERN_DEBUG "pktgen: dst_min set to: %s\n", |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1254 | pkt_dev->dst_min); |
| 1255 | i += len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1256 | sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min); |
| 1257 | return count; |
| 1258 | } |
| 1259 | if (!strcmp(name, "dst_max")) { |
| 1260 | len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1261 | if (len < 0) { |
| 1262 | return len; |
| 1263 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1264 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1265 | if (copy_from_user(buf, &user_buffer[i], len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1266 | return -EFAULT; |
| 1267 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1268 | buf[len] = 0; |
| 1269 | if (strcmp(buf, pkt_dev->dst_max) != 0) { |
| 1270 | memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max)); |
| 1271 | strncpy(pkt_dev->dst_max, buf, len); |
| 1272 | pkt_dev->daddr_max = in_aton(pkt_dev->dst_max); |
| 1273 | pkt_dev->cur_daddr = pkt_dev->daddr_max; |
| 1274 | } |
| 1275 | if (debug) |
David S. Miller | 25a8b25 | 2007-07-30 16:11:48 -0700 | [diff] [blame] | 1276 | printk(KERN_DEBUG "pktgen: dst_max set to: %s\n", |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1277 | pkt_dev->dst_max); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1278 | i += len; |
| 1279 | sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max); |
| 1280 | return count; |
| 1281 | } |
| 1282 | if (!strcmp(name, "dst6")) { |
| 1283 | len = strn_len(&user_buffer[i], sizeof(buf) - 1); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1284 | if (len < 0) |
| 1285 | return len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1286 | |
| 1287 | pkt_dev->flags |= F_IPV6; |
| 1288 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1289 | if (copy_from_user(buf, &user_buffer[i], len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1290 | return -EFAULT; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1291 | buf[len] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1292 | |
| 1293 | scan_ip6(buf, pkt_dev->in6_daddr.s6_addr); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1294 | fmt_ip6(buf, pkt_dev->in6_daddr.s6_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1295 | |
| 1296 | ipv6_addr_copy(&pkt_dev->cur_in6_daddr, &pkt_dev->in6_daddr); |
| 1297 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1298 | if (debug) |
David S. Miller | 25a8b25 | 2007-07-30 16:11:48 -0700 | [diff] [blame] | 1299 | printk(KERN_DEBUG "pktgen: dst6 set to: %s\n", buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1300 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1301 | i += len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1302 | sprintf(pg_result, "OK: dst6=%s", buf); |
| 1303 | return count; |
| 1304 | } |
| 1305 | if (!strcmp(name, "dst6_min")) { |
| 1306 | len = strn_len(&user_buffer[i], sizeof(buf) - 1); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1307 | if (len < 0) |
| 1308 | return len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1309 | |
| 1310 | pkt_dev->flags |= F_IPV6; |
| 1311 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1312 | if (copy_from_user(buf, &user_buffer[i], len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1313 | return -EFAULT; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1314 | buf[len] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1315 | |
| 1316 | scan_ip6(buf, pkt_dev->min_in6_daddr.s6_addr); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1317 | fmt_ip6(buf, pkt_dev->min_in6_daddr.s6_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1318 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1319 | ipv6_addr_copy(&pkt_dev->cur_in6_daddr, |
| 1320 | &pkt_dev->min_in6_daddr); |
| 1321 | if (debug) |
David S. Miller | 25a8b25 | 2007-07-30 16:11:48 -0700 | [diff] [blame] | 1322 | printk(KERN_DEBUG "pktgen: dst6_min set to: %s\n", buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1323 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1324 | i += len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1325 | sprintf(pg_result, "OK: dst6_min=%s", buf); |
| 1326 | return count; |
| 1327 | } |
| 1328 | if (!strcmp(name, "dst6_max")) { |
| 1329 | len = strn_len(&user_buffer[i], sizeof(buf) - 1); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1330 | if (len < 0) |
| 1331 | return len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1332 | |
| 1333 | pkt_dev->flags |= F_IPV6; |
| 1334 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1335 | if (copy_from_user(buf, &user_buffer[i], len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1336 | return -EFAULT; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1337 | buf[len] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1338 | |
| 1339 | scan_ip6(buf, pkt_dev->max_in6_daddr.s6_addr); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1340 | fmt_ip6(buf, pkt_dev->max_in6_daddr.s6_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1341 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1342 | if (debug) |
David S. Miller | 25a8b25 | 2007-07-30 16:11:48 -0700 | [diff] [blame] | 1343 | printk(KERN_DEBUG "pktgen: dst6_max set to: %s\n", buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1344 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1345 | i += len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1346 | sprintf(pg_result, "OK: dst6_max=%s", buf); |
| 1347 | return count; |
| 1348 | } |
| 1349 | if (!strcmp(name, "src6")) { |
| 1350 | len = strn_len(&user_buffer[i], sizeof(buf) - 1); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1351 | if (len < 0) |
| 1352 | return len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1353 | |
| 1354 | pkt_dev->flags |= F_IPV6; |
| 1355 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1356 | if (copy_from_user(buf, &user_buffer[i], len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1357 | return -EFAULT; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1358 | buf[len] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1359 | |
| 1360 | scan_ip6(buf, pkt_dev->in6_saddr.s6_addr); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1361 | fmt_ip6(buf, pkt_dev->in6_saddr.s6_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1362 | |
| 1363 | ipv6_addr_copy(&pkt_dev->cur_in6_saddr, &pkt_dev->in6_saddr); |
| 1364 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1365 | if (debug) |
David S. Miller | 25a8b25 | 2007-07-30 16:11:48 -0700 | [diff] [blame] | 1366 | printk(KERN_DEBUG "pktgen: src6 set to: %s\n", buf); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1367 | |
| 1368 | i += len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1369 | sprintf(pg_result, "OK: src6=%s", buf); |
| 1370 | return count; |
| 1371 | } |
| 1372 | if (!strcmp(name, "src_min")) { |
| 1373 | len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1374 | if (len < 0) { |
| 1375 | return len; |
| 1376 | } |
| 1377 | if (copy_from_user(buf, &user_buffer[i], len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1378 | return -EFAULT; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1379 | buf[len] = 0; |
| 1380 | if (strcmp(buf, pkt_dev->src_min) != 0) { |
| 1381 | memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min)); |
| 1382 | strncpy(pkt_dev->src_min, buf, len); |
| 1383 | pkt_dev->saddr_min = in_aton(pkt_dev->src_min); |
| 1384 | pkt_dev->cur_saddr = pkt_dev->saddr_min; |
| 1385 | } |
| 1386 | if (debug) |
David S. Miller | 25a8b25 | 2007-07-30 16:11:48 -0700 | [diff] [blame] | 1387 | printk(KERN_DEBUG "pktgen: src_min set to: %s\n", |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1388 | pkt_dev->src_min); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1389 | i += len; |
| 1390 | sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min); |
| 1391 | return count; |
| 1392 | } |
| 1393 | if (!strcmp(name, "src_max")) { |
| 1394 | len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1395 | if (len < 0) { |
| 1396 | return len; |
| 1397 | } |
| 1398 | if (copy_from_user(buf, &user_buffer[i], len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1399 | return -EFAULT; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1400 | buf[len] = 0; |
| 1401 | if (strcmp(buf, pkt_dev->src_max) != 0) { |
| 1402 | memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max)); |
| 1403 | strncpy(pkt_dev->src_max, buf, len); |
| 1404 | pkt_dev->saddr_max = in_aton(pkt_dev->src_max); |
| 1405 | pkt_dev->cur_saddr = pkt_dev->saddr_max; |
| 1406 | } |
| 1407 | if (debug) |
David S. Miller | 25a8b25 | 2007-07-30 16:11:48 -0700 | [diff] [blame] | 1408 | printk(KERN_DEBUG "pktgen: src_max set to: %s\n", |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1409 | pkt_dev->src_max); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1410 | i += len; |
| 1411 | sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max); |
| 1412 | return count; |
| 1413 | } |
| 1414 | if (!strcmp(name, "dst_mac")) { |
| 1415 | char *v = valstr; |
Kris Katterjohn | f404e9a | 2006-01-17 13:04:57 -0800 | [diff] [blame] | 1416 | unsigned char old_dmac[ETH_ALEN]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1417 | unsigned char *m = pkt_dev->dst_mac; |
Kris Katterjohn | f404e9a | 2006-01-17 13:04:57 -0800 | [diff] [blame] | 1418 | memcpy(old_dmac, pkt_dev->dst_mac, ETH_ALEN); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1419 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1420 | len = strn_len(&user_buffer[i], sizeof(valstr) - 1); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1421 | if (len < 0) { |
| 1422 | return len; |
| 1423 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1424 | memset(valstr, 0, sizeof(valstr)); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1425 | if (copy_from_user(valstr, &user_buffer[i], len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1426 | return -EFAULT; |
| 1427 | i += len; |
| 1428 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1429 | for (*m = 0; *v && m < pkt_dev->dst_mac + 6; v++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1430 | if (*v >= '0' && *v <= '9') { |
| 1431 | *m *= 16; |
| 1432 | *m += *v - '0'; |
| 1433 | } |
| 1434 | if (*v >= 'A' && *v <= 'F') { |
| 1435 | *m *= 16; |
| 1436 | *m += *v - 'A' + 10; |
| 1437 | } |
| 1438 | if (*v >= 'a' && *v <= 'f') { |
| 1439 | *m *= 16; |
| 1440 | *m += *v - 'a' + 10; |
| 1441 | } |
| 1442 | if (*v == ':') { |
| 1443 | m++; |
| 1444 | *m = 0; |
| 1445 | } |
| 1446 | } |
| 1447 | |
| 1448 | /* Set up Dest MAC */ |
Kris Katterjohn | f404e9a | 2006-01-17 13:04:57 -0800 | [diff] [blame] | 1449 | if (compare_ether_addr(old_dmac, pkt_dev->dst_mac)) |
| 1450 | memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1451 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1452 | sprintf(pg_result, "OK: dstmac"); |
| 1453 | return count; |
| 1454 | } |
| 1455 | if (!strcmp(name, "src_mac")) { |
| 1456 | char *v = valstr; |
Adit Ranadive | ce5d0b4 | 2007-09-16 14:52:15 -0700 | [diff] [blame] | 1457 | unsigned char old_smac[ETH_ALEN]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1458 | unsigned char *m = pkt_dev->src_mac; |
| 1459 | |
Adit Ranadive | ce5d0b4 | 2007-09-16 14:52:15 -0700 | [diff] [blame] | 1460 | memcpy(old_smac, pkt_dev->src_mac, ETH_ALEN); |
| 1461 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1462 | len = strn_len(&user_buffer[i], sizeof(valstr) - 1); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1463 | if (len < 0) { |
| 1464 | return len; |
| 1465 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1466 | memset(valstr, 0, sizeof(valstr)); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1467 | if (copy_from_user(valstr, &user_buffer[i], len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1468 | return -EFAULT; |
| 1469 | i += len; |
| 1470 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1471 | for (*m = 0; *v && m < pkt_dev->src_mac + 6; v++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1472 | if (*v >= '0' && *v <= '9') { |
| 1473 | *m *= 16; |
| 1474 | *m += *v - '0'; |
| 1475 | } |
| 1476 | if (*v >= 'A' && *v <= 'F') { |
| 1477 | *m *= 16; |
| 1478 | *m += *v - 'A' + 10; |
| 1479 | } |
| 1480 | if (*v >= 'a' && *v <= 'f') { |
| 1481 | *m *= 16; |
| 1482 | *m += *v - 'a' + 10; |
| 1483 | } |
| 1484 | if (*v == ':') { |
| 1485 | m++; |
| 1486 | *m = 0; |
| 1487 | } |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1488 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1489 | |
Adit Ranadive | ce5d0b4 | 2007-09-16 14:52:15 -0700 | [diff] [blame] | 1490 | /* Set up Src MAC */ |
| 1491 | if (compare_ether_addr(old_smac, pkt_dev->src_mac)) |
| 1492 | memcpy(&(pkt_dev->hh[6]), pkt_dev->src_mac, ETH_ALEN); |
| 1493 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1494 | sprintf(pg_result, "OK: srcmac"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1495 | return count; |
| 1496 | } |
| 1497 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1498 | if (!strcmp(name, "clear_counters")) { |
| 1499 | pktgen_clear_counters(pkt_dev); |
| 1500 | sprintf(pg_result, "OK: Clearing counters.\n"); |
| 1501 | return count; |
| 1502 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1503 | |
| 1504 | if (!strcmp(name, "flows")) { |
| 1505 | len = num_arg(&user_buffer[i], 10, &value); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1506 | if (len < 0) { |
| 1507 | return len; |
| 1508 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1509 | i += len; |
| 1510 | if (value > MAX_CFLOWS) |
| 1511 | value = MAX_CFLOWS; |
| 1512 | |
| 1513 | pkt_dev->cflows = value; |
| 1514 | sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows); |
| 1515 | return count; |
| 1516 | } |
| 1517 | |
| 1518 | if (!strcmp(name, "flowlen")) { |
| 1519 | len = num_arg(&user_buffer[i], 10, &value); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1520 | if (len < 0) { |
| 1521 | return len; |
| 1522 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1523 | i += len; |
| 1524 | pkt_dev->lflow = value; |
| 1525 | sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow); |
| 1526 | return count; |
| 1527 | } |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1528 | |
Steven Whitehouse | ca6549a | 2006-03-23 01:10:26 -0800 | [diff] [blame] | 1529 | if (!strcmp(name, "mpls")) { |
| 1530 | unsigned n, offset; |
| 1531 | len = get_labels(&user_buffer[i], pkt_dev); |
| 1532 | if (len < 0) { return len; } |
| 1533 | i += len; |
| 1534 | offset = sprintf(pg_result, "OK: mpls="); |
Stephen Hemminger | e71a478 | 2007-04-10 20:10:33 -0700 | [diff] [blame] | 1535 | for (n = 0; n < pkt_dev->nr_labels; n++) |
Steven Whitehouse | ca6549a | 2006-03-23 01:10:26 -0800 | [diff] [blame] | 1536 | offset += sprintf(pg_result + offset, |
| 1537 | "%08x%s", ntohl(pkt_dev->labels[n]), |
| 1538 | n == pkt_dev->nr_labels-1 ? "" : ","); |
Francesco Fondelli | 34954dd | 2006-09-27 16:30:44 -0700 | [diff] [blame] | 1539 | |
| 1540 | if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) { |
| 1541 | pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */ |
| 1542 | pkt_dev->svlan_id = 0xffff; |
| 1543 | |
| 1544 | if (debug) |
David S. Miller | 25a8b25 | 2007-07-30 16:11:48 -0700 | [diff] [blame] | 1545 | printk(KERN_DEBUG "pktgen: VLAN/SVLAN auto turned off\n"); |
Francesco Fondelli | 34954dd | 2006-09-27 16:30:44 -0700 | [diff] [blame] | 1546 | } |
| 1547 | return count; |
| 1548 | } |
| 1549 | |
| 1550 | if (!strcmp(name, "vlan_id")) { |
| 1551 | len = num_arg(&user_buffer[i], 4, &value); |
| 1552 | if (len < 0) { |
| 1553 | return len; |
| 1554 | } |
| 1555 | i += len; |
| 1556 | if (value <= 4095) { |
| 1557 | pkt_dev->vlan_id = value; /* turn on VLAN */ |
| 1558 | |
| 1559 | if (debug) |
David S. Miller | 25a8b25 | 2007-07-30 16:11:48 -0700 | [diff] [blame] | 1560 | printk(KERN_DEBUG "pktgen: VLAN turned on\n"); |
Francesco Fondelli | 34954dd | 2006-09-27 16:30:44 -0700 | [diff] [blame] | 1561 | |
| 1562 | if (debug && pkt_dev->nr_labels) |
David S. Miller | 25a8b25 | 2007-07-30 16:11:48 -0700 | [diff] [blame] | 1563 | printk(KERN_DEBUG "pktgen: MPLS auto turned off\n"); |
Francesco Fondelli | 34954dd | 2006-09-27 16:30:44 -0700 | [diff] [blame] | 1564 | |
| 1565 | pkt_dev->nr_labels = 0; /* turn off MPLS */ |
| 1566 | sprintf(pg_result, "OK: vlan_id=%u", pkt_dev->vlan_id); |
| 1567 | } else { |
| 1568 | pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */ |
| 1569 | pkt_dev->svlan_id = 0xffff; |
| 1570 | |
| 1571 | if (debug) |
David S. Miller | 25a8b25 | 2007-07-30 16:11:48 -0700 | [diff] [blame] | 1572 | printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n"); |
Francesco Fondelli | 34954dd | 2006-09-27 16:30:44 -0700 | [diff] [blame] | 1573 | } |
| 1574 | return count; |
| 1575 | } |
| 1576 | |
| 1577 | if (!strcmp(name, "vlan_p")) { |
| 1578 | len = num_arg(&user_buffer[i], 1, &value); |
| 1579 | if (len < 0) { |
| 1580 | return len; |
| 1581 | } |
| 1582 | i += len; |
| 1583 | if ((value <= 7) && (pkt_dev->vlan_id != 0xffff)) { |
| 1584 | pkt_dev->vlan_p = value; |
| 1585 | sprintf(pg_result, "OK: vlan_p=%u", pkt_dev->vlan_p); |
| 1586 | } else { |
| 1587 | sprintf(pg_result, "ERROR: vlan_p must be 0-7"); |
| 1588 | } |
| 1589 | return count; |
| 1590 | } |
| 1591 | |
| 1592 | if (!strcmp(name, "vlan_cfi")) { |
| 1593 | len = num_arg(&user_buffer[i], 1, &value); |
| 1594 | if (len < 0) { |
| 1595 | return len; |
| 1596 | } |
| 1597 | i += len; |
| 1598 | if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) { |
| 1599 | pkt_dev->vlan_cfi = value; |
| 1600 | sprintf(pg_result, "OK: vlan_cfi=%u", pkt_dev->vlan_cfi); |
| 1601 | } else { |
| 1602 | sprintf(pg_result, "ERROR: vlan_cfi must be 0-1"); |
| 1603 | } |
| 1604 | return count; |
| 1605 | } |
| 1606 | |
| 1607 | if (!strcmp(name, "svlan_id")) { |
| 1608 | len = num_arg(&user_buffer[i], 4, &value); |
| 1609 | if (len < 0) { |
| 1610 | return len; |
| 1611 | } |
| 1612 | i += len; |
| 1613 | if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) { |
| 1614 | pkt_dev->svlan_id = value; /* turn on SVLAN */ |
| 1615 | |
| 1616 | if (debug) |
David S. Miller | 25a8b25 | 2007-07-30 16:11:48 -0700 | [diff] [blame] | 1617 | printk(KERN_DEBUG "pktgen: SVLAN turned on\n"); |
Francesco Fondelli | 34954dd | 2006-09-27 16:30:44 -0700 | [diff] [blame] | 1618 | |
| 1619 | if (debug && pkt_dev->nr_labels) |
David S. Miller | 25a8b25 | 2007-07-30 16:11:48 -0700 | [diff] [blame] | 1620 | printk(KERN_DEBUG "pktgen: MPLS auto turned off\n"); |
Francesco Fondelli | 34954dd | 2006-09-27 16:30:44 -0700 | [diff] [blame] | 1621 | |
| 1622 | pkt_dev->nr_labels = 0; /* turn off MPLS */ |
| 1623 | sprintf(pg_result, "OK: svlan_id=%u", pkt_dev->svlan_id); |
| 1624 | } else { |
| 1625 | pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */ |
| 1626 | pkt_dev->svlan_id = 0xffff; |
| 1627 | |
| 1628 | if (debug) |
David S. Miller | 25a8b25 | 2007-07-30 16:11:48 -0700 | [diff] [blame] | 1629 | printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n"); |
Francesco Fondelli | 34954dd | 2006-09-27 16:30:44 -0700 | [diff] [blame] | 1630 | } |
| 1631 | return count; |
| 1632 | } |
| 1633 | |
| 1634 | if (!strcmp(name, "svlan_p")) { |
| 1635 | len = num_arg(&user_buffer[i], 1, &value); |
| 1636 | if (len < 0) { |
| 1637 | return len; |
| 1638 | } |
| 1639 | i += len; |
| 1640 | if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) { |
| 1641 | pkt_dev->svlan_p = value; |
| 1642 | sprintf(pg_result, "OK: svlan_p=%u", pkt_dev->svlan_p); |
| 1643 | } else { |
| 1644 | sprintf(pg_result, "ERROR: svlan_p must be 0-7"); |
| 1645 | } |
| 1646 | return count; |
| 1647 | } |
| 1648 | |
| 1649 | if (!strcmp(name, "svlan_cfi")) { |
| 1650 | len = num_arg(&user_buffer[i], 1, &value); |
| 1651 | if (len < 0) { |
| 1652 | return len; |
| 1653 | } |
| 1654 | i += len; |
| 1655 | if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) { |
| 1656 | pkt_dev->svlan_cfi = value; |
| 1657 | sprintf(pg_result, "OK: svlan_cfi=%u", pkt_dev->svlan_cfi); |
| 1658 | } else { |
| 1659 | sprintf(pg_result, "ERROR: svlan_cfi must be 0-1"); |
| 1660 | } |
Steven Whitehouse | ca6549a | 2006-03-23 01:10:26 -0800 | [diff] [blame] | 1661 | return count; |
| 1662 | } |
| 1663 | |
Francesco Fondelli | 1ca7768 | 2006-09-27 16:32:03 -0700 | [diff] [blame] | 1664 | if (!strcmp(name, "tos")) { |
| 1665 | __u32 tmp_value = 0; |
| 1666 | len = hex32_arg(&user_buffer[i], 2, &tmp_value); |
| 1667 | if (len < 0) { |
| 1668 | return len; |
| 1669 | } |
| 1670 | i += len; |
| 1671 | if (len == 2) { |
| 1672 | pkt_dev->tos = tmp_value; |
| 1673 | sprintf(pg_result, "OK: tos=0x%02x", pkt_dev->tos); |
| 1674 | } else { |
| 1675 | sprintf(pg_result, "ERROR: tos must be 00-ff"); |
| 1676 | } |
| 1677 | return count; |
| 1678 | } |
| 1679 | |
| 1680 | if (!strcmp(name, "traffic_class")) { |
| 1681 | __u32 tmp_value = 0; |
| 1682 | len = hex32_arg(&user_buffer[i], 2, &tmp_value); |
| 1683 | if (len < 0) { |
| 1684 | return len; |
| 1685 | } |
| 1686 | i += len; |
| 1687 | if (len == 2) { |
| 1688 | pkt_dev->traffic_class = tmp_value; |
| 1689 | sprintf(pg_result, "OK: traffic_class=0x%02x", pkt_dev->traffic_class); |
| 1690 | } else { |
| 1691 | sprintf(pg_result, "ERROR: traffic_class must be 00-ff"); |
| 1692 | } |
| 1693 | return count; |
| 1694 | } |
| 1695 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1696 | sprintf(pkt_dev->result, "No such parameter \"%s\"", name); |
| 1697 | return -EINVAL; |
| 1698 | } |
| 1699 | |
Stephen Hemminger | d50a6b5 | 2005-10-14 15:42:33 -0700 | [diff] [blame] | 1700 | static int pktgen_if_open(struct inode *inode, struct file *file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1701 | { |
Stephen Hemminger | d50a6b5 | 2005-10-14 15:42:33 -0700 | [diff] [blame] | 1702 | return single_open(file, pktgen_if_show, PDE(inode)->data); |
| 1703 | } |
| 1704 | |
Arjan van de Ven | 9a32144 | 2007-02-12 00:55:35 -0800 | [diff] [blame] | 1705 | static const struct file_operations pktgen_if_fops = { |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1706 | .owner = THIS_MODULE, |
| 1707 | .open = pktgen_if_open, |
| 1708 | .read = seq_read, |
| 1709 | .llseek = seq_lseek, |
| 1710 | .write = pktgen_if_write, |
| 1711 | .release = single_release, |
Stephen Hemminger | d50a6b5 | 2005-10-14 15:42:33 -0700 | [diff] [blame] | 1712 | }; |
| 1713 | |
| 1714 | static int pktgen_thread_show(struct seq_file *seq, void *v) |
| 1715 | { |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1716 | struct pktgen_thread *t = seq->private; |
Luiz Capitulino | c26a801 | 2006-03-20 22:18:16 -0800 | [diff] [blame] | 1717 | struct pktgen_dev *pkt_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1718 | |
Stephen Hemminger | d50a6b5 | 2005-10-14 15:42:33 -0700 | [diff] [blame] | 1719 | BUG_ON(!t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1720 | |
Stephen Hemminger | d50a6b5 | 2005-10-14 15:42:33 -0700 | [diff] [blame] | 1721 | seq_printf(seq, "Name: %s max_before_softirq: %d\n", |
David S. Miller | ee74baa | 2007-01-01 20:51:53 -0800 | [diff] [blame] | 1722 | t->tsk->comm, t->max_before_softirq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1723 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1724 | seq_printf(seq, "Running: "); |
| 1725 | |
| 1726 | if_lock(t); |
Luiz Capitulino | c26a801 | 2006-03-20 22:18:16 -0800 | [diff] [blame] | 1727 | list_for_each_entry(pkt_dev, &t->if_list, list) |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1728 | if (pkt_dev->running) |
Stephen Hemminger | 39df232 | 2007-03-04 16:11:51 -0800 | [diff] [blame] | 1729 | seq_printf(seq, "%s ", pkt_dev->odev->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1730 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1731 | seq_printf(seq, "\nStopped: "); |
| 1732 | |
Luiz Capitulino | c26a801 | 2006-03-20 22:18:16 -0800 | [diff] [blame] | 1733 | list_for_each_entry(pkt_dev, &t->if_list, list) |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1734 | if (!pkt_dev->running) |
Stephen Hemminger | 39df232 | 2007-03-04 16:11:51 -0800 | [diff] [blame] | 1735 | seq_printf(seq, "%s ", pkt_dev->odev->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1736 | |
| 1737 | if (t->result[0]) |
Stephen Hemminger | d50a6b5 | 2005-10-14 15:42:33 -0700 | [diff] [blame] | 1738 | seq_printf(seq, "\nResult: %s\n", t->result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1739 | else |
Stephen Hemminger | d50a6b5 | 2005-10-14 15:42:33 -0700 | [diff] [blame] | 1740 | seq_printf(seq, "\nResult: NA\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1741 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1742 | if_unlock(t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1743 | |
Stephen Hemminger | d50a6b5 | 2005-10-14 15:42:33 -0700 | [diff] [blame] | 1744 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1745 | } |
| 1746 | |
Stephen Hemminger | d50a6b5 | 2005-10-14 15:42:33 -0700 | [diff] [blame] | 1747 | static ssize_t pktgen_thread_write(struct file *file, |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1748 | const char __user * user_buffer, |
| 1749 | size_t count, loff_t * offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1750 | { |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1751 | struct seq_file *seq = (struct seq_file *)file->private_data; |
| 1752 | struct pktgen_thread *t = seq->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1753 | int i = 0, max, len, ret; |
| 1754 | char name[40]; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1755 | char *pg_result; |
| 1756 | unsigned long value = 0; |
Stephen Hemminger | d50a6b5 | 2005-10-14 15:42:33 -0700 | [diff] [blame] | 1757 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1758 | if (count < 1) { |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1759 | // sprintf(pg_result, "Wrong command format"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1760 | return -EINVAL; |
| 1761 | } |
Stephen Hemminger | d50a6b5 | 2005-10-14 15:42:33 -0700 | [diff] [blame] | 1762 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1763 | max = count - i; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1764 | len = count_trail_chars(&user_buffer[i], max); |
| 1765 | if (len < 0) |
Stephen Hemminger | d50a6b5 | 2005-10-14 15:42:33 -0700 | [diff] [blame] | 1766 | return len; |
| 1767 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1768 | i += len; |
Stephen Hemminger | d50a6b5 | 2005-10-14 15:42:33 -0700 | [diff] [blame] | 1769 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1770 | /* Read variable name */ |
| 1771 | |
| 1772 | len = strn_len(&user_buffer[i], sizeof(name) - 1); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1773 | if (len < 0) |
Stephen Hemminger | d50a6b5 | 2005-10-14 15:42:33 -0700 | [diff] [blame] | 1774 | return len; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1775 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1776 | memset(name, 0, sizeof(name)); |
| 1777 | if (copy_from_user(name, &user_buffer[i], len)) |
| 1778 | return -EFAULT; |
| 1779 | i += len; |
Stephen Hemminger | d50a6b5 | 2005-10-14 15:42:33 -0700 | [diff] [blame] | 1780 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1781 | max = count - i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1782 | len = count_trail_chars(&user_buffer[i], max); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1783 | if (len < 0) |
Stephen Hemminger | d50a6b5 | 2005-10-14 15:42:33 -0700 | [diff] [blame] | 1784 | return len; |
| 1785 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1786 | i += len; |
| 1787 | |
Stephen Hemminger | d50a6b5 | 2005-10-14 15:42:33 -0700 | [diff] [blame] | 1788 | if (debug) |
David S. Miller | 25a8b25 | 2007-07-30 16:11:48 -0700 | [diff] [blame] | 1789 | printk(KERN_DEBUG "pktgen: t=%s, count=%lu\n", |
| 1790 | name, (unsigned long)count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1791 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1792 | if (!t) { |
David S. Miller | 25a8b25 | 2007-07-30 16:11:48 -0700 | [diff] [blame] | 1793 | printk(KERN_ERR "pktgen: ERROR: No thread\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1794 | ret = -EINVAL; |
| 1795 | goto out; |
| 1796 | } |
| 1797 | |
| 1798 | pg_result = &(t->result[0]); |
| 1799 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1800 | if (!strcmp(name, "add_device")) { |
| 1801 | char f[32]; |
| 1802 | memset(f, 0, 32); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1803 | len = strn_len(&user_buffer[i], sizeof(f) - 1); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1804 | if (len < 0) { |
| 1805 | ret = len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1806 | goto out; |
| 1807 | } |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1808 | if (copy_from_user(f, &user_buffer[i], len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1809 | return -EFAULT; |
| 1810 | i += len; |
Luiz Capitulino | 6146e6a | 2006-03-20 22:24:45 -0800 | [diff] [blame] | 1811 | mutex_lock(&pktgen_thread_lock); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1812 | pktgen_add_device(t, f); |
Luiz Capitulino | 6146e6a | 2006-03-20 22:24:45 -0800 | [diff] [blame] | 1813 | mutex_unlock(&pktgen_thread_lock); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1814 | ret = count; |
| 1815 | sprintf(pg_result, "OK: add_device=%s", f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1816 | goto out; |
| 1817 | } |
| 1818 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1819 | if (!strcmp(name, "rem_device_all")) { |
Luiz Capitulino | 6146e6a | 2006-03-20 22:24:45 -0800 | [diff] [blame] | 1820 | mutex_lock(&pktgen_thread_lock); |
Arthur Kepner | 95ed63f | 2006-03-20 21:26:56 -0800 | [diff] [blame] | 1821 | t->control |= T_REMDEVALL; |
Luiz Capitulino | 6146e6a | 2006-03-20 22:24:45 -0800 | [diff] [blame] | 1822 | mutex_unlock(&pktgen_thread_lock); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1823 | schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1824 | ret = count; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1825 | sprintf(pg_result, "OK: rem_device_all"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1826 | goto out; |
| 1827 | } |
| 1828 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1829 | if (!strcmp(name, "max_before_softirq")) { |
| 1830 | len = num_arg(&user_buffer[i], 10, &value); |
Luiz Capitulino | 6146e6a | 2006-03-20 22:24:45 -0800 | [diff] [blame] | 1831 | mutex_lock(&pktgen_thread_lock); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1832 | t->max_before_softirq = value; |
Luiz Capitulino | 6146e6a | 2006-03-20 22:24:45 -0800 | [diff] [blame] | 1833 | mutex_unlock(&pktgen_thread_lock); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1834 | ret = count; |
| 1835 | sprintf(pg_result, "OK: max_before_softirq=%lu", value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1836 | goto out; |
| 1837 | } |
| 1838 | |
| 1839 | ret = -EINVAL; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1840 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1841 | return ret; |
| 1842 | } |
| 1843 | |
Stephen Hemminger | d50a6b5 | 2005-10-14 15:42:33 -0700 | [diff] [blame] | 1844 | static int pktgen_thread_open(struct inode *inode, struct file *file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1845 | { |
Stephen Hemminger | d50a6b5 | 2005-10-14 15:42:33 -0700 | [diff] [blame] | 1846 | return single_open(file, pktgen_thread_show, PDE(inode)->data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1847 | } |
| 1848 | |
Arjan van de Ven | 9a32144 | 2007-02-12 00:55:35 -0800 | [diff] [blame] | 1849 | static const struct file_operations pktgen_thread_fops = { |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1850 | .owner = THIS_MODULE, |
| 1851 | .open = pktgen_thread_open, |
| 1852 | .read = seq_read, |
| 1853 | .llseek = seq_lseek, |
| 1854 | .write = pktgen_thread_write, |
| 1855 | .release = single_release, |
Stephen Hemminger | d50a6b5 | 2005-10-14 15:42:33 -0700 | [diff] [blame] | 1856 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1857 | |
| 1858 | /* Think find or remove for NN */ |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1859 | static struct pktgen_dev *__pktgen_NN_threads(const char *ifname, int remove) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1860 | { |
| 1861 | struct pktgen_thread *t; |
| 1862 | struct pktgen_dev *pkt_dev = NULL; |
| 1863 | |
Luiz Capitulino | cdcdbe0 | 2006-03-20 22:16:40 -0800 | [diff] [blame] | 1864 | list_for_each_entry(t, &pktgen_threads, th_list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1865 | pkt_dev = pktgen_find_dev(t, ifname); |
| 1866 | if (pkt_dev) { |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1867 | if (remove) { |
| 1868 | if_lock(t); |
| 1869 | pkt_dev->removal_mark = 1; |
| 1870 | t->control |= T_REMDEV; |
| 1871 | if_unlock(t); |
| 1872 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1873 | break; |
| 1874 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1875 | } |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1876 | return pkt_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1877 | } |
| 1878 | |
Arthur Kepner | 95ed63f | 2006-03-20 21:26:56 -0800 | [diff] [blame] | 1879 | /* |
| 1880 | * mark a device for removal |
| 1881 | */ |
Stephen Hemminger | 39df232 | 2007-03-04 16:11:51 -0800 | [diff] [blame] | 1882 | static void pktgen_mark_device(const char *ifname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1883 | { |
| 1884 | struct pktgen_dev *pkt_dev = NULL; |
Arthur Kepner | 95ed63f | 2006-03-20 21:26:56 -0800 | [diff] [blame] | 1885 | const int max_tries = 10, msec_per_try = 125; |
| 1886 | int i = 0; |
Arthur Kepner | 95ed63f | 2006-03-20 21:26:56 -0800 | [diff] [blame] | 1887 | |
Luiz Capitulino | 6146e6a | 2006-03-20 22:24:45 -0800 | [diff] [blame] | 1888 | mutex_lock(&pktgen_thread_lock); |
Stephen Hemminger | 25c4e53 | 2007-03-04 16:06:47 -0800 | [diff] [blame] | 1889 | pr_debug("pktgen: pktgen_mark_device marking %s for removal\n", ifname); |
Arthur Kepner | 95ed63f | 2006-03-20 21:26:56 -0800 | [diff] [blame] | 1890 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1891 | while (1) { |
Arthur Kepner | 95ed63f | 2006-03-20 21:26:56 -0800 | [diff] [blame] | 1892 | |
| 1893 | pkt_dev = __pktgen_NN_threads(ifname, REMOVE); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1894 | if (pkt_dev == NULL) |
| 1895 | break; /* success */ |
Arthur Kepner | 95ed63f | 2006-03-20 21:26:56 -0800 | [diff] [blame] | 1896 | |
Luiz Capitulino | 6146e6a | 2006-03-20 22:24:45 -0800 | [diff] [blame] | 1897 | mutex_unlock(&pktgen_thread_lock); |
Stephen Hemminger | 25c4e53 | 2007-03-04 16:06:47 -0800 | [diff] [blame] | 1898 | pr_debug("pktgen: pktgen_mark_device waiting for %s " |
| 1899 | "to disappear....\n", ifname); |
Arthur Kepner | 95ed63f | 2006-03-20 21:26:56 -0800 | [diff] [blame] | 1900 | schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try)); |
Luiz Capitulino | 6146e6a | 2006-03-20 22:24:45 -0800 | [diff] [blame] | 1901 | mutex_lock(&pktgen_thread_lock); |
Arthur Kepner | 95ed63f | 2006-03-20 21:26:56 -0800 | [diff] [blame] | 1902 | |
| 1903 | if (++i >= max_tries) { |
David S. Miller | 25a8b25 | 2007-07-30 16:11:48 -0700 | [diff] [blame] | 1904 | printk(KERN_ERR "pktgen_mark_device: timed out after " |
| 1905 | "waiting %d msec for device %s to be removed\n", |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1906 | msec_per_try * i, ifname); |
Arthur Kepner | 95ed63f | 2006-03-20 21:26:56 -0800 | [diff] [blame] | 1907 | break; |
| 1908 | } |
| 1909 | |
| 1910 | } |
| 1911 | |
Luiz Capitulino | 6146e6a | 2006-03-20 22:24:45 -0800 | [diff] [blame] | 1912 | mutex_unlock(&pktgen_thread_lock); |
Stephen Hemminger | 39df232 | 2007-03-04 16:11:51 -0800 | [diff] [blame] | 1913 | } |
Arthur Kepner | 95ed63f | 2006-03-20 21:26:56 -0800 | [diff] [blame] | 1914 | |
Stephen Hemminger | 39df232 | 2007-03-04 16:11:51 -0800 | [diff] [blame] | 1915 | static void pktgen_change_name(struct net_device *dev) |
| 1916 | { |
| 1917 | struct pktgen_thread *t; |
| 1918 | |
| 1919 | list_for_each_entry(t, &pktgen_threads, th_list) { |
| 1920 | struct pktgen_dev *pkt_dev; |
| 1921 | |
| 1922 | list_for_each_entry(pkt_dev, &t->if_list, list) { |
| 1923 | if (pkt_dev->odev != dev) |
| 1924 | continue; |
| 1925 | |
| 1926 | remove_proc_entry(pkt_dev->entry->name, pg_proc_dir); |
| 1927 | |
| 1928 | pkt_dev->entry = create_proc_entry(dev->name, 0600, |
| 1929 | pg_proc_dir); |
| 1930 | if (!pkt_dev->entry) |
| 1931 | printk(KERN_ERR "pktgen: can't move proc " |
| 1932 | " entry for '%s'\n", dev->name); |
| 1933 | break; |
| 1934 | } |
| 1935 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1936 | } |
| 1937 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1938 | static int pktgen_device_event(struct notifier_block *unused, |
| 1939 | unsigned long event, void *ptr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1940 | { |
Stephen Hemminger | 39df232 | 2007-03-04 16:11:51 -0800 | [diff] [blame] | 1941 | struct net_device *dev = ptr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1942 | |
| 1943 | /* It is OK that we do not hold the group lock right now, |
| 1944 | * as we run under the RTNL lock. |
| 1945 | */ |
| 1946 | |
| 1947 | switch (event) { |
Stephen Hemminger | 39df232 | 2007-03-04 16:11:51 -0800 | [diff] [blame] | 1948 | case NETDEV_CHANGENAME: |
| 1949 | pktgen_change_name(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1950 | break; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1951 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1952 | case NETDEV_UNREGISTER: |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1953 | pktgen_mark_device(dev->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1954 | break; |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 1955 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1956 | |
| 1957 | return NOTIFY_DONE; |
| 1958 | } |
| 1959 | |
| 1960 | /* Associate pktgen_dev with a device. */ |
| 1961 | |
Stephen Hemminger | 39df232 | 2007-03-04 16:11:51 -0800 | [diff] [blame] | 1962 | static int pktgen_setup_dev(struct pktgen_dev *pkt_dev, const char *ifname) |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1963 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1964 | struct net_device *odev; |
Stephen Hemminger | 39df232 | 2007-03-04 16:11:51 -0800 | [diff] [blame] | 1965 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1966 | |
| 1967 | /* Clean old setups */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1968 | if (pkt_dev->odev) { |
| 1969 | dev_put(pkt_dev->odev); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1970 | pkt_dev->odev = NULL; |
| 1971 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1972 | |
Stephen Hemminger | 39df232 | 2007-03-04 16:11:51 -0800 | [diff] [blame] | 1973 | odev = dev_get_by_name(ifname); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1974 | if (!odev) { |
David S. Miller | 25a8b25 | 2007-07-30 16:11:48 -0700 | [diff] [blame] | 1975 | printk(KERN_ERR "pktgen: no such netdevice: \"%s\"\n", ifname); |
Stephen Hemminger | 39df232 | 2007-03-04 16:11:51 -0800 | [diff] [blame] | 1976 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1977 | } |
Stephen Hemminger | 39df232 | 2007-03-04 16:11:51 -0800 | [diff] [blame] | 1978 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1979 | if (odev->type != ARPHRD_ETHER) { |
David S. Miller | 25a8b25 | 2007-07-30 16:11:48 -0700 | [diff] [blame] | 1980 | printk(KERN_ERR "pktgen: not an ethernet device: \"%s\"\n", ifname); |
Stephen Hemminger | 39df232 | 2007-03-04 16:11:51 -0800 | [diff] [blame] | 1981 | err = -EINVAL; |
| 1982 | } else if (!netif_running(odev)) { |
David S. Miller | 25a8b25 | 2007-07-30 16:11:48 -0700 | [diff] [blame] | 1983 | printk(KERN_ERR "pktgen: device is down: \"%s\"\n", ifname); |
Stephen Hemminger | 39df232 | 2007-03-04 16:11:51 -0800 | [diff] [blame] | 1984 | err = -ENETDOWN; |
| 1985 | } else { |
| 1986 | pkt_dev->odev = odev; |
| 1987 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1988 | } |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1989 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1990 | dev_put(odev); |
Stephen Hemminger | 39df232 | 2007-03-04 16:11:51 -0800 | [diff] [blame] | 1991 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1992 | } |
| 1993 | |
| 1994 | /* Read pkt_dev from the interface and set up internal pktgen_dev |
| 1995 | * structure to have the right information to create/send packets |
| 1996 | */ |
| 1997 | static void pktgen_setup_inject(struct pktgen_dev *pkt_dev) |
| 1998 | { |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 1999 | if (!pkt_dev->odev) { |
David S. Miller | 25a8b25 | 2007-07-30 16:11:48 -0700 | [diff] [blame] | 2000 | printk(KERN_ERR "pktgen: ERROR: pkt_dev->odev == NULL in " |
| 2001 | "setup_inject.\n"); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2002 | sprintf(pkt_dev->result, |
| 2003 | "ERROR: pkt_dev->odev == NULL in setup_inject.\n"); |
| 2004 | return; |
| 2005 | } |
| 2006 | |
| 2007 | /* Default to the interface's mac if not explicitly set. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2008 | |
Kris Katterjohn | f404e9a | 2006-01-17 13:04:57 -0800 | [diff] [blame] | 2009 | if (is_zero_ether_addr(pkt_dev->src_mac)) |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2010 | memcpy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr, ETH_ALEN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2011 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2012 | /* Set up Dest MAC */ |
Kris Katterjohn | f404e9a | 2006-01-17 13:04:57 -0800 | [diff] [blame] | 2013 | memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2014 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2015 | /* Set up pkt size */ |
| 2016 | pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size; |
| 2017 | |
| 2018 | if (pkt_dev->flags & F_IPV6) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2019 | /* |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 2020 | * Skip this automatic address setting until locks or functions |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2021 | * gets exported |
| 2022 | */ |
| 2023 | |
| 2024 | #ifdef NOTNOW |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2025 | int i, set = 0, err = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2026 | struct inet6_dev *idev; |
| 2027 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2028 | for (i = 0; i < IN6_ADDR_HSIZE; i++) |
| 2029 | if (pkt_dev->cur_in6_saddr.s6_addr[i]) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2030 | set = 1; |
| 2031 | break; |
| 2032 | } |
| 2033 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2034 | if (!set) { |
| 2035 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2036 | /* |
| 2037 | * Use linklevel address if unconfigured. |
| 2038 | * |
| 2039 | * use ipv6_get_lladdr if/when it's get exported |
| 2040 | */ |
| 2041 | |
YOSHIFUJI Hideaki | 8814c4b | 2006-09-22 14:44:24 -0700 | [diff] [blame] | 2042 | rcu_read_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2043 | if ((idev = __in6_dev_get(pkt_dev->odev)) != NULL) { |
| 2044 | struct inet6_ifaddr *ifp; |
| 2045 | |
| 2046 | read_lock_bh(&idev->lock); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2047 | for (ifp = idev->addr_list; ifp; |
| 2048 | ifp = ifp->if_next) { |
| 2049 | if (ifp->scope == IFA_LINK |
| 2050 | && !(ifp-> |
| 2051 | flags & IFA_F_TENTATIVE)) { |
| 2052 | ipv6_addr_copy(&pkt_dev-> |
| 2053 | cur_in6_saddr, |
| 2054 | &ifp->addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2055 | err = 0; |
| 2056 | break; |
| 2057 | } |
| 2058 | } |
| 2059 | read_unlock_bh(&idev->lock); |
| 2060 | } |
YOSHIFUJI Hideaki | 8814c4b | 2006-09-22 14:44:24 -0700 | [diff] [blame] | 2061 | rcu_read_unlock(); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2062 | if (err) |
David S. Miller | 25a8b25 | 2007-07-30 16:11:48 -0700 | [diff] [blame] | 2063 | printk(KERN_ERR "pktgen: ERROR: IPv6 link " |
| 2064 | "address not availble.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2065 | } |
| 2066 | #endif |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2067 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2068 | pkt_dev->saddr_min = 0; |
| 2069 | pkt_dev->saddr_max = 0; |
| 2070 | if (strlen(pkt_dev->src_min) == 0) { |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2071 | |
| 2072 | struct in_device *in_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2073 | |
| 2074 | rcu_read_lock(); |
Herbert Xu | e5ed639 | 2005-10-03 14:35:55 -0700 | [diff] [blame] | 2075 | in_dev = __in_dev_get_rcu(pkt_dev->odev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2076 | if (in_dev) { |
| 2077 | if (in_dev->ifa_list) { |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2078 | pkt_dev->saddr_min = |
| 2079 | in_dev->ifa_list->ifa_address; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2080 | pkt_dev->saddr_max = pkt_dev->saddr_min; |
| 2081 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2082 | } |
| 2083 | rcu_read_unlock(); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2084 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2085 | pkt_dev->saddr_min = in_aton(pkt_dev->src_min); |
| 2086 | pkt_dev->saddr_max = in_aton(pkt_dev->src_max); |
| 2087 | } |
| 2088 | |
| 2089 | pkt_dev->daddr_min = in_aton(pkt_dev->dst_min); |
| 2090 | pkt_dev->daddr_max = in_aton(pkt_dev->dst_max); |
| 2091 | } |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2092 | /* Initialize current values. */ |
| 2093 | pkt_dev->cur_dst_mac_offset = 0; |
| 2094 | pkt_dev->cur_src_mac_offset = 0; |
| 2095 | pkt_dev->cur_saddr = pkt_dev->saddr_min; |
| 2096 | pkt_dev->cur_daddr = pkt_dev->daddr_min; |
| 2097 | pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min; |
| 2098 | pkt_dev->cur_udp_src = pkt_dev->udp_src_min; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2099 | pkt_dev->nflows = 0; |
| 2100 | } |
| 2101 | |
| 2102 | static void spin(struct pktgen_dev *pkt_dev, __u64 spin_until_us) |
| 2103 | { |
| 2104 | __u64 start; |
| 2105 | __u64 now; |
| 2106 | |
| 2107 | start = now = getCurUs(); |
| 2108 | printk(KERN_INFO "sleeping for %d\n", (int)(spin_until_us - now)); |
| 2109 | while (now < spin_until_us) { |
Stephen Hemminger | b4099fa | 2005-10-14 15:32:22 -0700 | [diff] [blame] | 2110 | /* TODO: optimize sleeping behavior */ |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2111 | if (spin_until_us - now > jiffies_to_usecs(1) + 1) |
Nishanth Aravamudan | 121caf5 | 2005-09-12 14:15:34 -0700 | [diff] [blame] | 2112 | schedule_timeout_interruptible(1); |
| 2113 | else if (spin_until_us - now > 100) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2114 | do_softirq(); |
| 2115 | if (!pkt_dev->running) |
| 2116 | return; |
| 2117 | if (need_resched()) |
| 2118 | schedule(); |
| 2119 | } |
| 2120 | |
| 2121 | now = getCurUs(); |
| 2122 | } |
| 2123 | |
| 2124 | pkt_dev->idle_acc += now - start; |
| 2125 | } |
| 2126 | |
Jamal Hadi Salim | 16dab72 | 2007-07-02 22:39:50 -0700 | [diff] [blame] | 2127 | static inline void set_pkt_overhead(struct pktgen_dev *pkt_dev) |
| 2128 | { |
Jamal Hadi Salim | a553e4a | 2007-07-02 22:41:59 -0700 | [diff] [blame] | 2129 | pkt_dev->pkt_overhead = 0; |
Jamal Hadi Salim | 16dab72 | 2007-07-02 22:39:50 -0700 | [diff] [blame] | 2130 | pkt_dev->pkt_overhead += pkt_dev->nr_labels*sizeof(u32); |
| 2131 | pkt_dev->pkt_overhead += VLAN_TAG_SIZE(pkt_dev); |
| 2132 | pkt_dev->pkt_overhead += SVLAN_TAG_SIZE(pkt_dev); |
| 2133 | } |
| 2134 | |
Jamal Hadi Salim | 007a531 | 2007-07-02 22:40:36 -0700 | [diff] [blame] | 2135 | static inline int f_seen(struct pktgen_dev *pkt_dev, int flow) |
| 2136 | { |
| 2137 | |
| 2138 | if (pkt_dev->flows[flow].flags & F_INIT) |
| 2139 | return 1; |
| 2140 | else |
| 2141 | return 0; |
| 2142 | } |
| 2143 | |
| 2144 | static inline int f_pick(struct pktgen_dev *pkt_dev) |
| 2145 | { |
| 2146 | int flow = pkt_dev->curfl; |
| 2147 | |
| 2148 | if (pkt_dev->flags & F_FLOW_SEQ) { |
| 2149 | if (pkt_dev->flows[flow].count >= pkt_dev->lflow) { |
| 2150 | /* reset time */ |
| 2151 | pkt_dev->flows[flow].count = 0; |
| 2152 | pkt_dev->curfl += 1; |
| 2153 | if (pkt_dev->curfl >= pkt_dev->cflows) |
| 2154 | pkt_dev->curfl = 0; /*reset */ |
| 2155 | } |
| 2156 | } else { |
| 2157 | flow = random32() % pkt_dev->cflows; |
| 2158 | |
| 2159 | if (pkt_dev->flows[flow].count > pkt_dev->lflow) |
| 2160 | pkt_dev->flows[flow].count = 0; |
| 2161 | } |
| 2162 | |
| 2163 | return pkt_dev->curfl; |
| 2164 | } |
| 2165 | |
Jamal Hadi Salim | a553e4a | 2007-07-02 22:41:59 -0700 | [diff] [blame] | 2166 | |
| 2167 | #ifdef CONFIG_XFRM |
| 2168 | /* If there was already an IPSEC SA, we keep it as is, else |
| 2169 | * we go look for it ... |
| 2170 | */ |
Adrian Bunk | fea1ab0 | 2007-07-30 18:04:09 -0700 | [diff] [blame] | 2171 | static void get_ipsec_sa(struct pktgen_dev *pkt_dev, int flow) |
Jamal Hadi Salim | a553e4a | 2007-07-02 22:41:59 -0700 | [diff] [blame] | 2172 | { |
| 2173 | struct xfrm_state *x = pkt_dev->flows[flow].x; |
| 2174 | if (!x) { |
| 2175 | /*slow path: we dont already have xfrm_state*/ |
| 2176 | x = xfrm_stateonly_find((xfrm_address_t *)&pkt_dev->cur_daddr, |
| 2177 | (xfrm_address_t *)&pkt_dev->cur_saddr, |
| 2178 | AF_INET, |
| 2179 | pkt_dev->ipsmode, |
| 2180 | pkt_dev->ipsproto, 0); |
| 2181 | if (x) { |
| 2182 | pkt_dev->flows[flow].x = x; |
| 2183 | set_pkt_overhead(pkt_dev); |
| 2184 | pkt_dev->pkt_overhead+=x->props.header_len; |
| 2185 | } |
| 2186 | |
| 2187 | } |
| 2188 | } |
| 2189 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2190 | /* Increment/randomize headers according to flags and current values |
| 2191 | * for IP src/dest, UDP src/dst port, MAC-Addr src/dst |
| 2192 | */ |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2193 | static void mod_cur_headers(struct pktgen_dev *pkt_dev) |
| 2194 | { |
| 2195 | __u32 imn; |
| 2196 | __u32 imx; |
| 2197 | int flow = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2198 | |
Jamal Hadi Salim | 007a531 | 2007-07-02 22:40:36 -0700 | [diff] [blame] | 2199 | if (pkt_dev->cflows) |
| 2200 | flow = f_pick(pkt_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2201 | |
| 2202 | /* Deal with source MAC */ |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2203 | if (pkt_dev->src_mac_count > 1) { |
| 2204 | __u32 mc; |
| 2205 | __u32 tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2206 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2207 | if (pkt_dev->flags & F_MACSRC_RND) |
Stephen Hemminger | 5fa6fc7 | 2007-03-04 16:07:28 -0800 | [diff] [blame] | 2208 | mc = random32() % pkt_dev->src_mac_count; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2209 | else { |
| 2210 | mc = pkt_dev->cur_src_mac_offset++; |
| 2211 | if (pkt_dev->cur_src_mac_offset > |
| 2212 | pkt_dev->src_mac_count) |
| 2213 | pkt_dev->cur_src_mac_offset = 0; |
| 2214 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2215 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2216 | tmp = pkt_dev->src_mac[5] + (mc & 0xFF); |
| 2217 | pkt_dev->hh[11] = tmp; |
| 2218 | tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8)); |
| 2219 | pkt_dev->hh[10] = tmp; |
| 2220 | tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8)); |
| 2221 | pkt_dev->hh[9] = tmp; |
| 2222 | tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8)); |
| 2223 | pkt_dev->hh[8] = tmp; |
| 2224 | tmp = (pkt_dev->src_mac[1] + (tmp >> 8)); |
| 2225 | pkt_dev->hh[7] = tmp; |
| 2226 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2227 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2228 | /* Deal with Destination MAC */ |
| 2229 | if (pkt_dev->dst_mac_count > 1) { |
| 2230 | __u32 mc; |
| 2231 | __u32 tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2232 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2233 | if (pkt_dev->flags & F_MACDST_RND) |
Stephen Hemminger | 5fa6fc7 | 2007-03-04 16:07:28 -0800 | [diff] [blame] | 2234 | mc = random32() % pkt_dev->dst_mac_count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2235 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2236 | else { |
| 2237 | mc = pkt_dev->cur_dst_mac_offset++; |
| 2238 | if (pkt_dev->cur_dst_mac_offset > |
| 2239 | pkt_dev->dst_mac_count) { |
| 2240 | pkt_dev->cur_dst_mac_offset = 0; |
| 2241 | } |
| 2242 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2243 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2244 | tmp = pkt_dev->dst_mac[5] + (mc & 0xFF); |
| 2245 | pkt_dev->hh[5] = tmp; |
| 2246 | tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8)); |
| 2247 | pkt_dev->hh[4] = tmp; |
| 2248 | tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8)); |
| 2249 | pkt_dev->hh[3] = tmp; |
| 2250 | tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8)); |
| 2251 | pkt_dev->hh[2] = tmp; |
| 2252 | tmp = (pkt_dev->dst_mac[1] + (tmp >> 8)); |
| 2253 | pkt_dev->hh[1] = tmp; |
| 2254 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2255 | |
Steven Whitehouse | ca6549a | 2006-03-23 01:10:26 -0800 | [diff] [blame] | 2256 | if (pkt_dev->flags & F_MPLS_RND) { |
| 2257 | unsigned i; |
Stephen Hemminger | e71a478 | 2007-04-10 20:10:33 -0700 | [diff] [blame] | 2258 | for (i = 0; i < pkt_dev->nr_labels; i++) |
Steven Whitehouse | ca6549a | 2006-03-23 01:10:26 -0800 | [diff] [blame] | 2259 | if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM) |
| 2260 | pkt_dev->labels[i] = MPLS_STACK_BOTTOM | |
Stephen Hemminger | 5fa6fc7 | 2007-03-04 16:07:28 -0800 | [diff] [blame] | 2261 | ((__force __be32)random32() & |
Steven Whitehouse | ca6549a | 2006-03-23 01:10:26 -0800 | [diff] [blame] | 2262 | htonl(0x000fffff)); |
| 2263 | } |
| 2264 | |
Francesco Fondelli | 34954dd | 2006-09-27 16:30:44 -0700 | [diff] [blame] | 2265 | if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) { |
Stephen Hemminger | 5fa6fc7 | 2007-03-04 16:07:28 -0800 | [diff] [blame] | 2266 | pkt_dev->vlan_id = random32() & (4096-1); |
Francesco Fondelli | 34954dd | 2006-09-27 16:30:44 -0700 | [diff] [blame] | 2267 | } |
| 2268 | |
| 2269 | if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) { |
Stephen Hemminger | 5fa6fc7 | 2007-03-04 16:07:28 -0800 | [diff] [blame] | 2270 | pkt_dev->svlan_id = random32() & (4096 - 1); |
Francesco Fondelli | 34954dd | 2006-09-27 16:30:44 -0700 | [diff] [blame] | 2271 | } |
| 2272 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2273 | if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) { |
| 2274 | if (pkt_dev->flags & F_UDPSRC_RND) |
Stephen Hemminger | 5fa6fc7 | 2007-03-04 16:07:28 -0800 | [diff] [blame] | 2275 | pkt_dev->cur_udp_src = random32() % |
| 2276 | (pkt_dev->udp_src_max - pkt_dev->udp_src_min) |
| 2277 | + pkt_dev->udp_src_min; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2278 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2279 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2280 | pkt_dev->cur_udp_src++; |
| 2281 | if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max) |
| 2282 | pkt_dev->cur_udp_src = pkt_dev->udp_src_min; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2283 | } |
| 2284 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2285 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2286 | if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) { |
| 2287 | if (pkt_dev->flags & F_UDPDST_RND) { |
Stephen Hemminger | 5fa6fc7 | 2007-03-04 16:07:28 -0800 | [diff] [blame] | 2288 | pkt_dev->cur_udp_dst = random32() % |
| 2289 | (pkt_dev->udp_dst_max - pkt_dev->udp_dst_min) |
| 2290 | + pkt_dev->udp_dst_min; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2291 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2292 | pkt_dev->cur_udp_dst++; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2293 | if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2294 | pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2295 | } |
| 2296 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2297 | |
| 2298 | if (!(pkt_dev->flags & F_IPV6)) { |
| 2299 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2300 | if ((imn = ntohl(pkt_dev->saddr_min)) < (imx = |
| 2301 | ntohl(pkt_dev-> |
| 2302 | saddr_max))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2303 | __u32 t; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2304 | if (pkt_dev->flags & F_IPSRC_RND) |
Stephen Hemminger | 5fa6fc7 | 2007-03-04 16:07:28 -0800 | [diff] [blame] | 2305 | t = random32() % (imx - imn) + imn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2306 | else { |
| 2307 | t = ntohl(pkt_dev->cur_saddr); |
| 2308 | t++; |
| 2309 | if (t > imx) { |
| 2310 | t = imn; |
| 2311 | } |
| 2312 | } |
| 2313 | pkt_dev->cur_saddr = htonl(t); |
| 2314 | } |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2315 | |
Jamal Hadi Salim | 007a531 | 2007-07-02 22:40:36 -0700 | [diff] [blame] | 2316 | if (pkt_dev->cflows && f_seen(pkt_dev, flow)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2317 | pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr; |
| 2318 | } else { |
Al Viro | 252e334 | 2006-11-14 20:48:11 -0800 | [diff] [blame] | 2319 | imn = ntohl(pkt_dev->daddr_min); |
| 2320 | imx = ntohl(pkt_dev->daddr_max); |
| 2321 | if (imn < imx) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2322 | __u32 t; |
Al Viro | 252e334 | 2006-11-14 20:48:11 -0800 | [diff] [blame] | 2323 | __be32 s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2324 | if (pkt_dev->flags & F_IPDST_RND) { |
| 2325 | |
Stephen Hemminger | 5fa6fc7 | 2007-03-04 16:07:28 -0800 | [diff] [blame] | 2326 | t = random32() % (imx - imn) + imn; |
Al Viro | 252e334 | 2006-11-14 20:48:11 -0800 | [diff] [blame] | 2327 | s = htonl(t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2328 | |
Al Viro | 252e334 | 2006-11-14 20:48:11 -0800 | [diff] [blame] | 2329 | while (LOOPBACK(s) || MULTICAST(s) |
| 2330 | || BADCLASS(s) || ZERONET(s) |
| 2331 | || LOCAL_MCAST(s)) { |
Stephen Hemminger | 5fa6fc7 | 2007-03-04 16:07:28 -0800 | [diff] [blame] | 2332 | t = random32() % (imx - imn) + imn; |
Al Viro | 252e334 | 2006-11-14 20:48:11 -0800 | [diff] [blame] | 2333 | s = htonl(t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2334 | } |
Al Viro | 252e334 | 2006-11-14 20:48:11 -0800 | [diff] [blame] | 2335 | pkt_dev->cur_daddr = s; |
| 2336 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2337 | t = ntohl(pkt_dev->cur_daddr); |
| 2338 | t++; |
| 2339 | if (t > imx) { |
| 2340 | t = imn; |
| 2341 | } |
| 2342 | pkt_dev->cur_daddr = htonl(t); |
| 2343 | } |
| 2344 | } |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2345 | if (pkt_dev->cflows) { |
Jamal Hadi Salim | 007a531 | 2007-07-02 22:40:36 -0700 | [diff] [blame] | 2346 | pkt_dev->flows[flow].flags |= F_INIT; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2347 | pkt_dev->flows[flow].cur_daddr = |
| 2348 | pkt_dev->cur_daddr; |
Jamal Hadi Salim | a553e4a | 2007-07-02 22:41:59 -0700 | [diff] [blame] | 2349 | #ifdef CONFIG_XFRM |
| 2350 | if (pkt_dev->flags & F_IPSEC_ON) |
| 2351 | get_ipsec_sa(pkt_dev, flow); |
| 2352 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2353 | pkt_dev->nflows++; |
| 2354 | } |
| 2355 | } |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2356 | } else { /* IPV6 * */ |
| 2357 | |
| 2358 | if (pkt_dev->min_in6_daddr.s6_addr32[0] == 0 && |
| 2359 | pkt_dev->min_in6_daddr.s6_addr32[1] == 0 && |
| 2360 | pkt_dev->min_in6_daddr.s6_addr32[2] == 0 && |
| 2361 | pkt_dev->min_in6_daddr.s6_addr32[3] == 0) ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2362 | else { |
| 2363 | int i; |
| 2364 | |
| 2365 | /* Only random destinations yet */ |
| 2366 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2367 | for (i = 0; i < 4; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2368 | pkt_dev->cur_in6_daddr.s6_addr32[i] = |
Stephen Hemminger | 5fa6fc7 | 2007-03-04 16:07:28 -0800 | [diff] [blame] | 2369 | (((__force __be32)random32() | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2370 | pkt_dev->min_in6_daddr.s6_addr32[i]) & |
| 2371 | pkt_dev->max_in6_daddr.s6_addr32[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2372 | } |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2373 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2374 | } |
| 2375 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2376 | if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) { |
| 2377 | __u32 t; |
| 2378 | if (pkt_dev->flags & F_TXSIZE_RND) { |
Stephen Hemminger | 5fa6fc7 | 2007-03-04 16:07:28 -0800 | [diff] [blame] | 2379 | t = random32() % |
| 2380 | (pkt_dev->max_pkt_size - pkt_dev->min_pkt_size) |
| 2381 | + pkt_dev->min_pkt_size; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2382 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2383 | t = pkt_dev->cur_pkt_size + 1; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2384 | if (t > pkt_dev->max_pkt_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2385 | t = pkt_dev->min_pkt_size; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2386 | } |
| 2387 | pkt_dev->cur_pkt_size = t; |
| 2388 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2389 | |
| 2390 | pkt_dev->flows[flow].count++; |
| 2391 | } |
| 2392 | |
Jamal Hadi Salim | a553e4a | 2007-07-02 22:41:59 -0700 | [diff] [blame] | 2393 | |
| 2394 | #ifdef CONFIG_XFRM |
| 2395 | static int pktgen_output_ipsec(struct sk_buff *skb, struct pktgen_dev *pkt_dev) |
| 2396 | { |
| 2397 | struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x; |
| 2398 | int err = 0; |
| 2399 | struct iphdr *iph; |
| 2400 | |
| 2401 | if (!x) |
| 2402 | return 0; |
| 2403 | /* XXX: we dont support tunnel mode for now until |
| 2404 | * we resolve the dst issue */ |
| 2405 | if (x->props.mode != XFRM_MODE_TRANSPORT) |
| 2406 | return 0; |
| 2407 | |
| 2408 | spin_lock(&x->lock); |
| 2409 | iph = ip_hdr(skb); |
| 2410 | |
| 2411 | err = x->mode->output(x, skb); |
| 2412 | if (err) |
| 2413 | goto error; |
| 2414 | err = x->type->output(x, skb); |
| 2415 | if (err) |
| 2416 | goto error; |
| 2417 | |
| 2418 | x->curlft.bytes +=skb->len; |
| 2419 | x->curlft.packets++; |
| 2420 | spin_unlock(&x->lock); |
| 2421 | |
| 2422 | error: |
| 2423 | spin_unlock(&x->lock); |
| 2424 | return err; |
| 2425 | } |
| 2426 | |
| 2427 | static inline void free_SAs(struct pktgen_dev *pkt_dev) |
| 2428 | { |
| 2429 | if (pkt_dev->cflows) { |
| 2430 | /* let go of the SAs if we have them */ |
| 2431 | int i = 0; |
| 2432 | for (; i < pkt_dev->nflows; i++){ |
| 2433 | struct xfrm_state *x = pkt_dev->flows[i].x; |
| 2434 | if (x) { |
| 2435 | xfrm_state_put(x); |
| 2436 | pkt_dev->flows[i].x = NULL; |
| 2437 | } |
| 2438 | } |
| 2439 | } |
| 2440 | } |
| 2441 | |
| 2442 | static inline int process_ipsec(struct pktgen_dev *pkt_dev, |
| 2443 | struct sk_buff *skb, __be16 protocol) |
| 2444 | { |
| 2445 | if (pkt_dev->flags & F_IPSEC_ON) { |
| 2446 | struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x; |
| 2447 | int nhead = 0; |
| 2448 | if (x) { |
| 2449 | int ret; |
| 2450 | __u8 *eth; |
| 2451 | nhead = x->props.header_len - skb_headroom(skb); |
| 2452 | if (nhead >0) { |
| 2453 | ret = pskb_expand_head(skb, nhead, 0, GFP_ATOMIC); |
| 2454 | if (ret < 0) { |
David S. Miller | 25a8b25 | 2007-07-30 16:11:48 -0700 | [diff] [blame] | 2455 | printk(KERN_ERR "Error expanding " |
| 2456 | "ipsec packet %d\n",ret); |
Jamal Hadi Salim | a553e4a | 2007-07-02 22:41:59 -0700 | [diff] [blame] | 2457 | return 0; |
| 2458 | } |
| 2459 | } |
| 2460 | |
| 2461 | /* ipsec is not expecting ll header */ |
| 2462 | skb_pull(skb, ETH_HLEN); |
| 2463 | ret = pktgen_output_ipsec(skb, pkt_dev); |
| 2464 | if (ret) { |
David S. Miller | 25a8b25 | 2007-07-30 16:11:48 -0700 | [diff] [blame] | 2465 | printk(KERN_ERR "Error creating ipsec " |
| 2466 | "packet %d\n",ret); |
Jamal Hadi Salim | a553e4a | 2007-07-02 22:41:59 -0700 | [diff] [blame] | 2467 | kfree_skb(skb); |
| 2468 | return 0; |
| 2469 | } |
| 2470 | /* restore ll */ |
| 2471 | eth = (__u8 *) skb_push(skb, ETH_HLEN); |
| 2472 | memcpy(eth, pkt_dev->hh, 12); |
| 2473 | *(u16 *) & eth[12] = protocol; |
| 2474 | } |
| 2475 | } |
| 2476 | return 1; |
| 2477 | } |
| 2478 | #endif |
| 2479 | |
Steven Whitehouse | ca6549a | 2006-03-23 01:10:26 -0800 | [diff] [blame] | 2480 | static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev) |
| 2481 | { |
| 2482 | unsigned i; |
Stephen Hemminger | e71a478 | 2007-04-10 20:10:33 -0700 | [diff] [blame] | 2483 | for (i = 0; i < pkt_dev->nr_labels; i++) { |
Steven Whitehouse | ca6549a | 2006-03-23 01:10:26 -0800 | [diff] [blame] | 2484 | *mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM; |
| 2485 | } |
| 2486 | mpls--; |
| 2487 | *mpls |= MPLS_STACK_BOTTOM; |
| 2488 | } |
| 2489 | |
Al Viro | 0f37c60 | 2006-11-03 03:49:56 -0800 | [diff] [blame] | 2490 | static inline __be16 build_tci(unsigned int id, unsigned int cfi, |
| 2491 | unsigned int prio) |
| 2492 | { |
| 2493 | return htons(id | (cfi << 12) | (prio << 13)); |
| 2494 | } |
| 2495 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2496 | static struct sk_buff *fill_packet_ipv4(struct net_device *odev, |
| 2497 | struct pktgen_dev *pkt_dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2498 | { |
| 2499 | struct sk_buff *skb = NULL; |
| 2500 | __u8 *eth; |
| 2501 | struct udphdr *udph; |
| 2502 | int datalen, iplen; |
| 2503 | struct iphdr *iph; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2504 | struct pktgen_hdr *pgh = NULL; |
Stephen Hemminger | d5f1ce9 | 2007-03-04 16:08:08 -0800 | [diff] [blame] | 2505 | __be16 protocol = htons(ETH_P_IP); |
Steven Whitehouse | ca6549a | 2006-03-23 01:10:26 -0800 | [diff] [blame] | 2506 | __be32 *mpls; |
Francesco Fondelli | 34954dd | 2006-09-27 16:30:44 -0700 | [diff] [blame] | 2507 | __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */ |
| 2508 | __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */ |
| 2509 | __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */ |
| 2510 | __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */ |
| 2511 | |
Steven Whitehouse | ca6549a | 2006-03-23 01:10:26 -0800 | [diff] [blame] | 2512 | |
| 2513 | if (pkt_dev->nr_labels) |
Stephen Hemminger | d5f1ce9 | 2007-03-04 16:08:08 -0800 | [diff] [blame] | 2514 | protocol = htons(ETH_P_MPLS_UC); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2515 | |
Francesco Fondelli | 34954dd | 2006-09-27 16:30:44 -0700 | [diff] [blame] | 2516 | if (pkt_dev->vlan_id != 0xffff) |
Stephen Hemminger | d5f1ce9 | 2007-03-04 16:08:08 -0800 | [diff] [blame] | 2517 | protocol = htons(ETH_P_8021Q); |
Francesco Fondelli | 34954dd | 2006-09-27 16:30:44 -0700 | [diff] [blame] | 2518 | |
Robert Olsson | 64053be | 2005-06-26 15:27:10 -0700 | [diff] [blame] | 2519 | /* Update any of the values, used when we're incrementing various |
| 2520 | * fields. |
| 2521 | */ |
| 2522 | mod_cur_headers(pkt_dev); |
| 2523 | |
David S. Miller | 7ac5459 | 2006-01-18 14:19:10 -0800 | [diff] [blame] | 2524 | datalen = (odev->hard_header_len + 16) & ~0xf; |
Steven Whitehouse | ca6549a | 2006-03-23 01:10:26 -0800 | [diff] [blame] | 2525 | skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + datalen + |
Jamal Hadi Salim | 16dab72 | 2007-07-02 22:39:50 -0700 | [diff] [blame] | 2526 | pkt_dev->pkt_overhead, GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2527 | if (!skb) { |
| 2528 | sprintf(pkt_dev->result, "No memory"); |
| 2529 | return NULL; |
| 2530 | } |
| 2531 | |
David S. Miller | 7ac5459 | 2006-01-18 14:19:10 -0800 | [diff] [blame] | 2532 | skb_reserve(skb, datalen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2533 | |
| 2534 | /* Reserve for ethernet and IP header */ |
| 2535 | eth = (__u8 *) skb_push(skb, 14); |
Steven Whitehouse | ca6549a | 2006-03-23 01:10:26 -0800 | [diff] [blame] | 2536 | mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32)); |
| 2537 | if (pkt_dev->nr_labels) |
| 2538 | mpls_push(mpls, pkt_dev); |
Francesco Fondelli | 34954dd | 2006-09-27 16:30:44 -0700 | [diff] [blame] | 2539 | |
| 2540 | if (pkt_dev->vlan_id != 0xffff) { |
Stephen Hemminger | e71a478 | 2007-04-10 20:10:33 -0700 | [diff] [blame] | 2541 | if (pkt_dev->svlan_id != 0xffff) { |
Francesco Fondelli | 34954dd | 2006-09-27 16:30:44 -0700 | [diff] [blame] | 2542 | svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16)); |
Al Viro | 0f37c60 | 2006-11-03 03:49:56 -0800 | [diff] [blame] | 2543 | *svlan_tci = build_tci(pkt_dev->svlan_id, |
| 2544 | pkt_dev->svlan_cfi, |
| 2545 | pkt_dev->svlan_p); |
Francesco Fondelli | 34954dd | 2006-09-27 16:30:44 -0700 | [diff] [blame] | 2546 | svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16)); |
Stephen Hemminger | d5f1ce9 | 2007-03-04 16:08:08 -0800 | [diff] [blame] | 2547 | *svlan_encapsulated_proto = htons(ETH_P_8021Q); |
Francesco Fondelli | 34954dd | 2006-09-27 16:30:44 -0700 | [diff] [blame] | 2548 | } |
| 2549 | vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16)); |
Al Viro | 0f37c60 | 2006-11-03 03:49:56 -0800 | [diff] [blame] | 2550 | *vlan_tci = build_tci(pkt_dev->vlan_id, |
| 2551 | pkt_dev->vlan_cfi, |
| 2552 | pkt_dev->vlan_p); |
Francesco Fondelli | 34954dd | 2006-09-27 16:30:44 -0700 | [diff] [blame] | 2553 | vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16)); |
Stephen Hemminger | d5f1ce9 | 2007-03-04 16:08:08 -0800 | [diff] [blame] | 2554 | *vlan_encapsulated_proto = htons(ETH_P_IP); |
Francesco Fondelli | 34954dd | 2006-09-27 16:30:44 -0700 | [diff] [blame] | 2555 | } |
| 2556 | |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 2557 | skb->network_header = skb->tail; |
Arnaldo Carvalho de Melo | b0e380b | 2007-04-10 21:21:55 -0700 | [diff] [blame] | 2558 | skb->transport_header = skb->network_header + sizeof(struct iphdr); |
Arnaldo Carvalho de Melo | ddc7b8e | 2007-03-15 21:42:27 -0300 | [diff] [blame] | 2559 | skb_put(skb, sizeof(struct iphdr) + sizeof(struct udphdr)); |
| 2560 | |
| 2561 | iph = ip_hdr(skb); |
| 2562 | udph = udp_hdr(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2563 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2564 | memcpy(eth, pkt_dev->hh, 12); |
Al Viro | 252e334 | 2006-11-14 20:48:11 -0800 | [diff] [blame] | 2565 | *(__be16 *) & eth[12] = protocol; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2566 | |
Steven Whitehouse | ca6549a | 2006-03-23 01:10:26 -0800 | [diff] [blame] | 2567 | /* Eth + IPh + UDPh + mpls */ |
| 2568 | datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 - |
Jamal Hadi Salim | 16dab72 | 2007-07-02 22:39:50 -0700 | [diff] [blame] | 2569 | pkt_dev->pkt_overhead; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2570 | if (datalen < sizeof(struct pktgen_hdr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2571 | datalen = sizeof(struct pktgen_hdr); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2572 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2573 | udph->source = htons(pkt_dev->cur_udp_src); |
| 2574 | udph->dest = htons(pkt_dev->cur_udp_dst); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2575 | udph->len = htons(datalen + 8); /* DATA + udphdr */ |
| 2576 | udph->check = 0; /* No checksum */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2577 | |
| 2578 | iph->ihl = 5; |
| 2579 | iph->version = 4; |
| 2580 | iph->ttl = 32; |
Francesco Fondelli | 1ca7768 | 2006-09-27 16:32:03 -0700 | [diff] [blame] | 2581 | iph->tos = pkt_dev->tos; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2582 | iph->protocol = IPPROTO_UDP; /* UDP */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2583 | iph->saddr = pkt_dev->cur_saddr; |
| 2584 | iph->daddr = pkt_dev->cur_daddr; |
| 2585 | iph->frag_off = 0; |
| 2586 | iplen = 20 + 8 + datalen; |
| 2587 | iph->tot_len = htons(iplen); |
| 2588 | iph->check = 0; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2589 | iph->check = ip_fast_csum((void *)iph, iph->ihl); |
Steven Whitehouse | ca6549a | 2006-03-23 01:10:26 -0800 | [diff] [blame] | 2590 | skb->protocol = protocol; |
Arnaldo Carvalho de Melo | b0e380b | 2007-04-10 21:21:55 -0700 | [diff] [blame] | 2591 | skb->mac_header = (skb->network_header - ETH_HLEN - |
Jamal Hadi Salim | 16dab72 | 2007-07-02 22:39:50 -0700 | [diff] [blame] | 2592 | pkt_dev->pkt_overhead); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2593 | skb->dev = odev; |
| 2594 | skb->pkt_type = PACKET_HOST; |
| 2595 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2596 | if (pkt_dev->nfrags <= 0) |
| 2597 | pgh = (struct pktgen_hdr *)skb_put(skb, datalen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2598 | else { |
| 2599 | int frags = pkt_dev->nfrags; |
| 2600 | int i; |
| 2601 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2602 | pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8); |
| 2603 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2604 | if (frags > MAX_SKB_FRAGS) |
| 2605 | frags = MAX_SKB_FRAGS; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2606 | if (datalen > frags * PAGE_SIZE) { |
| 2607 | skb_put(skb, datalen - frags * PAGE_SIZE); |
| 2608 | datalen = frags * PAGE_SIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2609 | } |
| 2610 | |
| 2611 | i = 0; |
| 2612 | while (datalen > 0) { |
| 2613 | struct page *page = alloc_pages(GFP_KERNEL, 0); |
| 2614 | skb_shinfo(skb)->frags[i].page = page; |
| 2615 | skb_shinfo(skb)->frags[i].page_offset = 0; |
| 2616 | skb_shinfo(skb)->frags[i].size = |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2617 | (datalen < PAGE_SIZE ? datalen : PAGE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2618 | datalen -= skb_shinfo(skb)->frags[i].size; |
| 2619 | skb->len += skb_shinfo(skb)->frags[i].size; |
| 2620 | skb->data_len += skb_shinfo(skb)->frags[i].size; |
| 2621 | i++; |
| 2622 | skb_shinfo(skb)->nr_frags = i; |
| 2623 | } |
| 2624 | |
| 2625 | while (i < frags) { |
| 2626 | int rem; |
| 2627 | |
| 2628 | if (i == 0) |
| 2629 | break; |
| 2630 | |
| 2631 | rem = skb_shinfo(skb)->frags[i - 1].size / 2; |
| 2632 | if (rem == 0) |
| 2633 | break; |
| 2634 | |
| 2635 | skb_shinfo(skb)->frags[i - 1].size -= rem; |
| 2636 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2637 | skb_shinfo(skb)->frags[i] = |
| 2638 | skb_shinfo(skb)->frags[i - 1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2639 | get_page(skb_shinfo(skb)->frags[i].page); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2640 | skb_shinfo(skb)->frags[i].page = |
| 2641 | skb_shinfo(skb)->frags[i - 1].page; |
| 2642 | skb_shinfo(skb)->frags[i].page_offset += |
| 2643 | skb_shinfo(skb)->frags[i - 1].size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2644 | skb_shinfo(skb)->frags[i].size = rem; |
| 2645 | i++; |
| 2646 | skb_shinfo(skb)->nr_frags = i; |
| 2647 | } |
| 2648 | } |
| 2649 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2650 | /* Stamp the time, and sequence number, convert them to network byte order */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2651 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2652 | if (pgh) { |
| 2653 | struct timeval timestamp; |
| 2654 | |
| 2655 | pgh->pgh_magic = htonl(PKTGEN_MAGIC); |
| 2656 | pgh->seq_num = htonl(pkt_dev->seq_num); |
| 2657 | |
| 2658 | do_gettimeofday(×tamp); |
| 2659 | pgh->tv_sec = htonl(timestamp.tv_sec); |
| 2660 | pgh->tv_usec = htonl(timestamp.tv_usec); |
| 2661 | } |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2662 | |
Jamal Hadi Salim | a553e4a | 2007-07-02 22:41:59 -0700 | [diff] [blame] | 2663 | #ifdef CONFIG_XFRM |
| 2664 | if (!process_ipsec(pkt_dev, skb, protocol)) |
| 2665 | return NULL; |
| 2666 | #endif |
| 2667 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2668 | return skb; |
| 2669 | } |
| 2670 | |
| 2671 | /* |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 2672 | * scan_ip6, fmt_ip taken from dietlibc-0.21 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2673 | * Author Felix von Leitner <felix-dietlibc@fefe.de> |
| 2674 | * |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 2675 | * Slightly modified for kernel. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2676 | * Should be candidate for net/ipv4/utils.c |
| 2677 | * --ro |
| 2678 | */ |
| 2679 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2680 | static unsigned int scan_ip6(const char *s, char ip[16]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2681 | { |
| 2682 | unsigned int i; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2683 | unsigned int len = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2684 | unsigned long u; |
| 2685 | char suffix[16]; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2686 | unsigned int prefixlen = 0; |
| 2687 | unsigned int suffixlen = 0; |
Al Viro | 252e334 | 2006-11-14 20:48:11 -0800 | [diff] [blame] | 2688 | __be32 tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2689 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2690 | for (i = 0; i < 16; i++) |
| 2691 | ip[i] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2692 | |
| 2693 | for (;;) { |
| 2694 | if (*s == ':') { |
| 2695 | len++; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2696 | if (s[1] == ':') { /* Found "::", skip to part 2 */ |
| 2697 | s += 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2698 | len++; |
| 2699 | break; |
| 2700 | } |
| 2701 | s++; |
| 2702 | } |
| 2703 | { |
| 2704 | char *tmp; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2705 | u = simple_strtoul(s, &tmp, 16); |
| 2706 | i = tmp - s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2707 | } |
| 2708 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2709 | if (!i) |
| 2710 | return 0; |
| 2711 | if (prefixlen == 12 && s[i] == '.') { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2712 | |
| 2713 | /* the last 4 bytes may be written as IPv4 address */ |
| 2714 | |
| 2715 | tmp = in_aton(s); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2716 | memcpy((struct in_addr *)(ip + 12), &tmp, sizeof(tmp)); |
| 2717 | return i + len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2718 | } |
| 2719 | ip[prefixlen++] = (u >> 8); |
| 2720 | ip[prefixlen++] = (u & 255); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2721 | s += i; |
| 2722 | len += i; |
| 2723 | if (prefixlen == 16) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2724 | return len; |
| 2725 | } |
| 2726 | |
| 2727 | /* part 2, after "::" */ |
| 2728 | for (;;) { |
| 2729 | if (*s == ':') { |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2730 | if (suffixlen == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2731 | break; |
| 2732 | s++; |
| 2733 | len++; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2734 | } else if (suffixlen != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2735 | break; |
| 2736 | { |
| 2737 | char *tmp; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2738 | u = simple_strtol(s, &tmp, 16); |
| 2739 | i = tmp - s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2740 | } |
| 2741 | if (!i) { |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2742 | if (*s) |
| 2743 | len--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2744 | break; |
| 2745 | } |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2746 | if (suffixlen + prefixlen <= 12 && s[i] == '.') { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2747 | tmp = in_aton(s); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2748 | memcpy((struct in_addr *)(suffix + suffixlen), &tmp, |
| 2749 | sizeof(tmp)); |
| 2750 | suffixlen += 4; |
| 2751 | len += strlen(s); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2752 | break; |
| 2753 | } |
| 2754 | suffix[suffixlen++] = (u >> 8); |
| 2755 | suffix[suffixlen++] = (u & 255); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2756 | s += i; |
| 2757 | len += i; |
| 2758 | if (prefixlen + suffixlen == 16) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2759 | break; |
| 2760 | } |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2761 | for (i = 0; i < suffixlen; i++) |
| 2762 | ip[16 - suffixlen + i] = suffix[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2763 | return len; |
| 2764 | } |
| 2765 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2766 | static char tohex(char hexdigit) |
| 2767 | { |
| 2768 | return hexdigit > 9 ? hexdigit + 'a' - 10 : hexdigit + '0'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2769 | } |
| 2770 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2771 | static int fmt_xlong(char *s, unsigned int i) |
| 2772 | { |
| 2773 | char *bak = s; |
| 2774 | *s = tohex((i >> 12) & 0xf); |
| 2775 | if (s != bak || *s != '0') |
| 2776 | ++s; |
| 2777 | *s = tohex((i >> 8) & 0xf); |
| 2778 | if (s != bak || *s != '0') |
| 2779 | ++s; |
| 2780 | *s = tohex((i >> 4) & 0xf); |
| 2781 | if (s != bak || *s != '0') |
| 2782 | ++s; |
| 2783 | *s = tohex(i & 0xf); |
| 2784 | return s - bak + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2785 | } |
| 2786 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2787 | static unsigned int fmt_ip6(char *s, const char ip[16]) |
| 2788 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2789 | unsigned int len; |
| 2790 | unsigned int i; |
| 2791 | unsigned int temp; |
| 2792 | unsigned int compressing; |
| 2793 | int j; |
| 2794 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2795 | len = 0; |
| 2796 | compressing = 0; |
| 2797 | for (j = 0; j < 16; j += 2) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2798 | |
| 2799 | #ifdef V4MAPPEDPREFIX |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2800 | if (j == 12 && !memcmp(ip, V4mappedprefix, 12)) { |
| 2801 | inet_ntoa_r(*(struct in_addr *)(ip + 12), s); |
| 2802 | temp = strlen(s); |
| 2803 | return len + temp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2804 | } |
| 2805 | #endif |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2806 | temp = ((unsigned long)(unsigned char)ip[j] << 8) + |
| 2807 | (unsigned long)(unsigned char)ip[j + 1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2808 | if (temp == 0) { |
| 2809 | if (!compressing) { |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2810 | compressing = 1; |
| 2811 | if (j == 0) { |
| 2812 | *s++ = ':'; |
| 2813 | ++len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2814 | } |
| 2815 | } |
| 2816 | } else { |
| 2817 | if (compressing) { |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2818 | compressing = 0; |
| 2819 | *s++ = ':'; |
| 2820 | ++len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2821 | } |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2822 | i = fmt_xlong(s, temp); |
| 2823 | len += i; |
| 2824 | s += i; |
| 2825 | if (j < 14) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2826 | *s++ = ':'; |
| 2827 | ++len; |
| 2828 | } |
| 2829 | } |
| 2830 | } |
| 2831 | if (compressing) { |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2832 | *s++ = ':'; |
| 2833 | ++len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2834 | } |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2835 | *s = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2836 | return len; |
| 2837 | } |
| 2838 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2839 | static struct sk_buff *fill_packet_ipv6(struct net_device *odev, |
| 2840 | struct pktgen_dev *pkt_dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2841 | { |
| 2842 | struct sk_buff *skb = NULL; |
| 2843 | __u8 *eth; |
| 2844 | struct udphdr *udph; |
| 2845 | int datalen; |
| 2846 | struct ipv6hdr *iph; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2847 | struct pktgen_hdr *pgh = NULL; |
Stephen Hemminger | d5f1ce9 | 2007-03-04 16:08:08 -0800 | [diff] [blame] | 2848 | __be16 protocol = htons(ETH_P_IPV6); |
Steven Whitehouse | ca6549a | 2006-03-23 01:10:26 -0800 | [diff] [blame] | 2849 | __be32 *mpls; |
Francesco Fondelli | 34954dd | 2006-09-27 16:30:44 -0700 | [diff] [blame] | 2850 | __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */ |
| 2851 | __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */ |
| 2852 | __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */ |
| 2853 | __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */ |
Steven Whitehouse | ca6549a | 2006-03-23 01:10:26 -0800 | [diff] [blame] | 2854 | |
| 2855 | if (pkt_dev->nr_labels) |
Stephen Hemminger | d5f1ce9 | 2007-03-04 16:08:08 -0800 | [diff] [blame] | 2856 | protocol = htons(ETH_P_MPLS_UC); |
Robert Olsson | 64053be | 2005-06-26 15:27:10 -0700 | [diff] [blame] | 2857 | |
Francesco Fondelli | 34954dd | 2006-09-27 16:30:44 -0700 | [diff] [blame] | 2858 | if (pkt_dev->vlan_id != 0xffff) |
Stephen Hemminger | d5f1ce9 | 2007-03-04 16:08:08 -0800 | [diff] [blame] | 2859 | protocol = htons(ETH_P_8021Q); |
Francesco Fondelli | 34954dd | 2006-09-27 16:30:44 -0700 | [diff] [blame] | 2860 | |
Robert Olsson | 64053be | 2005-06-26 15:27:10 -0700 | [diff] [blame] | 2861 | /* Update any of the values, used when we're incrementing various |
| 2862 | * fields. |
| 2863 | */ |
| 2864 | mod_cur_headers(pkt_dev); |
| 2865 | |
Steven Whitehouse | ca6549a | 2006-03-23 01:10:26 -0800 | [diff] [blame] | 2866 | skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + 16 + |
Jamal Hadi Salim | 16dab72 | 2007-07-02 22:39:50 -0700 | [diff] [blame] | 2867 | pkt_dev->pkt_overhead, GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2868 | if (!skb) { |
| 2869 | sprintf(pkt_dev->result, "No memory"); |
| 2870 | return NULL; |
| 2871 | } |
| 2872 | |
| 2873 | skb_reserve(skb, 16); |
| 2874 | |
| 2875 | /* Reserve for ethernet and IP header */ |
| 2876 | eth = (__u8 *) skb_push(skb, 14); |
Steven Whitehouse | ca6549a | 2006-03-23 01:10:26 -0800 | [diff] [blame] | 2877 | mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32)); |
| 2878 | if (pkt_dev->nr_labels) |
| 2879 | mpls_push(mpls, pkt_dev); |
Francesco Fondelli | 34954dd | 2006-09-27 16:30:44 -0700 | [diff] [blame] | 2880 | |
| 2881 | if (pkt_dev->vlan_id != 0xffff) { |
Stephen Hemminger | e71a478 | 2007-04-10 20:10:33 -0700 | [diff] [blame] | 2882 | if (pkt_dev->svlan_id != 0xffff) { |
Francesco Fondelli | 34954dd | 2006-09-27 16:30:44 -0700 | [diff] [blame] | 2883 | svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16)); |
Al Viro | 0f37c60 | 2006-11-03 03:49:56 -0800 | [diff] [blame] | 2884 | *svlan_tci = build_tci(pkt_dev->svlan_id, |
| 2885 | pkt_dev->svlan_cfi, |
| 2886 | pkt_dev->svlan_p); |
Francesco Fondelli | 34954dd | 2006-09-27 16:30:44 -0700 | [diff] [blame] | 2887 | svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16)); |
Stephen Hemminger | d5f1ce9 | 2007-03-04 16:08:08 -0800 | [diff] [blame] | 2888 | *svlan_encapsulated_proto = htons(ETH_P_8021Q); |
Francesco Fondelli | 34954dd | 2006-09-27 16:30:44 -0700 | [diff] [blame] | 2889 | } |
| 2890 | vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16)); |
Al Viro | 0f37c60 | 2006-11-03 03:49:56 -0800 | [diff] [blame] | 2891 | *vlan_tci = build_tci(pkt_dev->vlan_id, |
| 2892 | pkt_dev->vlan_cfi, |
| 2893 | pkt_dev->vlan_p); |
Francesco Fondelli | 34954dd | 2006-09-27 16:30:44 -0700 | [diff] [blame] | 2894 | vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16)); |
Stephen Hemminger | d5f1ce9 | 2007-03-04 16:08:08 -0800 | [diff] [blame] | 2895 | *vlan_encapsulated_proto = htons(ETH_P_IPV6); |
Francesco Fondelli | 34954dd | 2006-09-27 16:30:44 -0700 | [diff] [blame] | 2896 | } |
| 2897 | |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 2898 | skb->network_header = skb->tail; |
Arnaldo Carvalho de Melo | b0e380b | 2007-04-10 21:21:55 -0700 | [diff] [blame] | 2899 | skb->transport_header = skb->network_header + sizeof(struct ipv6hdr); |
Arnaldo Carvalho de Melo | ddc7b8e | 2007-03-15 21:42:27 -0300 | [diff] [blame] | 2900 | skb_put(skb, sizeof(struct ipv6hdr) + sizeof(struct udphdr)); |
| 2901 | |
| 2902 | iph = ipv6_hdr(skb); |
| 2903 | udph = udp_hdr(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2904 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2905 | memcpy(eth, pkt_dev->hh, 12); |
Al Viro | 252e334 | 2006-11-14 20:48:11 -0800 | [diff] [blame] | 2906 | *(__be16 *) & eth[12] = protocol; |
Robert Olsson | 64053be | 2005-06-26 15:27:10 -0700 | [diff] [blame] | 2907 | |
Steven Whitehouse | ca6549a | 2006-03-23 01:10:26 -0800 | [diff] [blame] | 2908 | /* Eth + IPh + UDPh + mpls */ |
| 2909 | datalen = pkt_dev->cur_pkt_size - 14 - |
| 2910 | sizeof(struct ipv6hdr) - sizeof(struct udphdr) - |
Jamal Hadi Salim | 16dab72 | 2007-07-02 22:39:50 -0700 | [diff] [blame] | 2911 | pkt_dev->pkt_overhead; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2912 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2913 | if (datalen < sizeof(struct pktgen_hdr)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2914 | datalen = sizeof(struct pktgen_hdr); |
| 2915 | if (net_ratelimit()) |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2916 | printk(KERN_INFO "pktgen: increased datalen to %d\n", |
| 2917 | datalen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2918 | } |
| 2919 | |
| 2920 | udph->source = htons(pkt_dev->cur_udp_src); |
| 2921 | udph->dest = htons(pkt_dev->cur_udp_dst); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2922 | udph->len = htons(datalen + sizeof(struct udphdr)); |
| 2923 | udph->check = 0; /* No checksum */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2924 | |
Stephen Hemminger | d5f1ce9 | 2007-03-04 16:08:08 -0800 | [diff] [blame] | 2925 | *(__be32 *) iph = htonl(0x60000000); /* Version + flow */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2926 | |
Francesco Fondelli | 1ca7768 | 2006-09-27 16:32:03 -0700 | [diff] [blame] | 2927 | if (pkt_dev->traffic_class) { |
| 2928 | /* Version + traffic class + flow (0) */ |
Al Viro | 252e334 | 2006-11-14 20:48:11 -0800 | [diff] [blame] | 2929 | *(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20)); |
Francesco Fondelli | 1ca7768 | 2006-09-27 16:32:03 -0700 | [diff] [blame] | 2930 | } |
| 2931 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2932 | iph->hop_limit = 32; |
| 2933 | |
| 2934 | iph->payload_len = htons(sizeof(struct udphdr) + datalen); |
| 2935 | iph->nexthdr = IPPROTO_UDP; |
| 2936 | |
| 2937 | ipv6_addr_copy(&iph->daddr, &pkt_dev->cur_in6_daddr); |
| 2938 | ipv6_addr_copy(&iph->saddr, &pkt_dev->cur_in6_saddr); |
| 2939 | |
Arnaldo Carvalho de Melo | b0e380b | 2007-04-10 21:21:55 -0700 | [diff] [blame] | 2940 | skb->mac_header = (skb->network_header - ETH_HLEN - |
Jamal Hadi Salim | 16dab72 | 2007-07-02 22:39:50 -0700 | [diff] [blame] | 2941 | pkt_dev->pkt_overhead); |
Steven Whitehouse | ca6549a | 2006-03-23 01:10:26 -0800 | [diff] [blame] | 2942 | skb->protocol = protocol; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2943 | skb->dev = odev; |
| 2944 | skb->pkt_type = PACKET_HOST; |
| 2945 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2946 | if (pkt_dev->nfrags <= 0) |
| 2947 | pgh = (struct pktgen_hdr *)skb_put(skb, datalen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2948 | else { |
| 2949 | int frags = pkt_dev->nfrags; |
| 2950 | int i; |
| 2951 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2952 | pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8); |
| 2953 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2954 | if (frags > MAX_SKB_FRAGS) |
| 2955 | frags = MAX_SKB_FRAGS; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2956 | if (datalen > frags * PAGE_SIZE) { |
| 2957 | skb_put(skb, datalen - frags * PAGE_SIZE); |
| 2958 | datalen = frags * PAGE_SIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2959 | } |
| 2960 | |
| 2961 | i = 0; |
| 2962 | while (datalen > 0) { |
| 2963 | struct page *page = alloc_pages(GFP_KERNEL, 0); |
| 2964 | skb_shinfo(skb)->frags[i].page = page; |
| 2965 | skb_shinfo(skb)->frags[i].page_offset = 0; |
| 2966 | skb_shinfo(skb)->frags[i].size = |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2967 | (datalen < PAGE_SIZE ? datalen : PAGE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2968 | datalen -= skb_shinfo(skb)->frags[i].size; |
| 2969 | skb->len += skb_shinfo(skb)->frags[i].size; |
| 2970 | skb->data_len += skb_shinfo(skb)->frags[i].size; |
| 2971 | i++; |
| 2972 | skb_shinfo(skb)->nr_frags = i; |
| 2973 | } |
| 2974 | |
| 2975 | while (i < frags) { |
| 2976 | int rem; |
| 2977 | |
| 2978 | if (i == 0) |
| 2979 | break; |
| 2980 | |
| 2981 | rem = skb_shinfo(skb)->frags[i - 1].size / 2; |
| 2982 | if (rem == 0) |
| 2983 | break; |
| 2984 | |
| 2985 | skb_shinfo(skb)->frags[i - 1].size -= rem; |
| 2986 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2987 | skb_shinfo(skb)->frags[i] = |
| 2988 | skb_shinfo(skb)->frags[i - 1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2989 | get_page(skb_shinfo(skb)->frags[i].page); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 2990 | skb_shinfo(skb)->frags[i].page = |
| 2991 | skb_shinfo(skb)->frags[i - 1].page; |
| 2992 | skb_shinfo(skb)->frags[i].page_offset += |
| 2993 | skb_shinfo(skb)->frags[i - 1].size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2994 | skb_shinfo(skb)->frags[i].size = rem; |
| 2995 | i++; |
| 2996 | skb_shinfo(skb)->nr_frags = i; |
| 2997 | } |
| 2998 | } |
| 2999 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3000 | /* Stamp the time, and sequence number, convert them to network byte order */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3001 | /* should we update cloned packets too ? */ |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3002 | if (pgh) { |
| 3003 | struct timeval timestamp; |
| 3004 | |
| 3005 | pgh->pgh_magic = htonl(PKTGEN_MAGIC); |
| 3006 | pgh->seq_num = htonl(pkt_dev->seq_num); |
| 3007 | |
| 3008 | do_gettimeofday(×tamp); |
| 3009 | pgh->tv_sec = htonl(timestamp.tv_sec); |
| 3010 | pgh->tv_usec = htonl(timestamp.tv_usec); |
| 3011 | } |
Francesco Fondelli | 34954dd | 2006-09-27 16:30:44 -0700 | [diff] [blame] | 3012 | /* pkt_dev->seq_num++; FF: you really mean this? */ |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3013 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3014 | return skb; |
| 3015 | } |
| 3016 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3017 | static inline struct sk_buff *fill_packet(struct net_device *odev, |
| 3018 | struct pktgen_dev *pkt_dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3019 | { |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3020 | if (pkt_dev->flags & F_IPV6) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3021 | return fill_packet_ipv6(odev, pkt_dev); |
| 3022 | else |
| 3023 | return fill_packet_ipv4(odev, pkt_dev); |
| 3024 | } |
| 3025 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3026 | static void pktgen_clear_counters(struct pktgen_dev *pkt_dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3027 | { |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3028 | pkt_dev->seq_num = 1; |
| 3029 | pkt_dev->idle_acc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3030 | pkt_dev->sofar = 0; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3031 | pkt_dev->tx_bytes = 0; |
| 3032 | pkt_dev->errors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3033 | } |
| 3034 | |
| 3035 | /* Set up structure for sending pkts, clear counters */ |
| 3036 | |
| 3037 | static void pktgen_run(struct pktgen_thread *t) |
| 3038 | { |
Luiz Capitulino | c26a801 | 2006-03-20 22:18:16 -0800 | [diff] [blame] | 3039 | struct pktgen_dev *pkt_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3040 | int started = 0; |
| 3041 | |
Stephen Hemminger | 25c4e53 | 2007-03-04 16:06:47 -0800 | [diff] [blame] | 3042 | pr_debug("pktgen: entering pktgen_run. %p\n", t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3043 | |
| 3044 | if_lock(t); |
Luiz Capitulino | c26a801 | 2006-03-20 22:18:16 -0800 | [diff] [blame] | 3045 | list_for_each_entry(pkt_dev, &t->if_list, list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3046 | |
| 3047 | /* |
| 3048 | * setup odev and create initial packet. |
| 3049 | */ |
| 3050 | pktgen_setup_inject(pkt_dev); |
| 3051 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3052 | if (pkt_dev->odev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3053 | pktgen_clear_counters(pkt_dev); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3054 | pkt_dev->running = 1; /* Cranke yeself! */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3055 | pkt_dev->skb = NULL; |
| 3056 | pkt_dev->started_at = getCurUs(); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3057 | pkt_dev->next_tx_us = getCurUs(); /* Transmit immediately */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3058 | pkt_dev->next_tx_ns = 0; |
Jamal Hadi Salim | 16dab72 | 2007-07-02 22:39:50 -0700 | [diff] [blame] | 3059 | set_pkt_overhead(pkt_dev); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3060 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3061 | strcpy(pkt_dev->result, "Starting"); |
| 3062 | started++; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3063 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3064 | strcpy(pkt_dev->result, "Error starting"); |
| 3065 | } |
| 3066 | if_unlock(t); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3067 | if (started) |
| 3068 | t->control &= ~(T_STOP); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3069 | } |
| 3070 | |
| 3071 | static void pktgen_stop_all_threads_ifs(void) |
| 3072 | { |
Luiz Capitulino | cdcdbe0 | 2006-03-20 22:16:40 -0800 | [diff] [blame] | 3073 | struct pktgen_thread *t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3074 | |
Stephen Hemminger | 25c4e53 | 2007-03-04 16:06:47 -0800 | [diff] [blame] | 3075 | pr_debug("pktgen: entering pktgen_stop_all_threads_ifs.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3076 | |
Luiz Capitulino | 6146e6a | 2006-03-20 22:24:45 -0800 | [diff] [blame] | 3077 | mutex_lock(&pktgen_thread_lock); |
Luiz Capitulino | cdcdbe0 | 2006-03-20 22:16:40 -0800 | [diff] [blame] | 3078 | |
| 3079 | list_for_each_entry(t, &pktgen_threads, th_list) |
Arthur Kepner | 95ed63f | 2006-03-20 21:26:56 -0800 | [diff] [blame] | 3080 | t->control |= T_STOP; |
Luiz Capitulino | cdcdbe0 | 2006-03-20 22:16:40 -0800 | [diff] [blame] | 3081 | |
Luiz Capitulino | 6146e6a | 2006-03-20 22:24:45 -0800 | [diff] [blame] | 3082 | mutex_unlock(&pktgen_thread_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3083 | } |
| 3084 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3085 | static int thread_is_running(struct pktgen_thread *t) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3086 | { |
Luiz Capitulino | c26a801 | 2006-03-20 22:18:16 -0800 | [diff] [blame] | 3087 | struct pktgen_dev *pkt_dev; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3088 | int res = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3089 | |
Luiz Capitulino | c26a801 | 2006-03-20 22:18:16 -0800 | [diff] [blame] | 3090 | list_for_each_entry(pkt_dev, &t->if_list, list) |
| 3091 | if (pkt_dev->running) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3092 | res = 1; |
| 3093 | break; |
| 3094 | } |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3095 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3096 | } |
| 3097 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3098 | static int pktgen_wait_thread_run(struct pktgen_thread *t) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3099 | { |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3100 | if_lock(t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3101 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3102 | while (thread_is_running(t)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3103 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3104 | if_unlock(t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3105 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3106 | msleep_interruptible(100); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3107 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3108 | if (signal_pending(current)) |
| 3109 | goto signal; |
| 3110 | if_lock(t); |
| 3111 | } |
| 3112 | if_unlock(t); |
| 3113 | return 1; |
| 3114 | signal: |
| 3115 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3116 | } |
| 3117 | |
| 3118 | static int pktgen_wait_all_threads_run(void) |
| 3119 | { |
Luiz Capitulino | cdcdbe0 | 2006-03-20 22:16:40 -0800 | [diff] [blame] | 3120 | struct pktgen_thread *t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3121 | int sig = 1; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3122 | |
Luiz Capitulino | 6146e6a | 2006-03-20 22:24:45 -0800 | [diff] [blame] | 3123 | mutex_lock(&pktgen_thread_lock); |
Luiz Capitulino | cdcdbe0 | 2006-03-20 22:16:40 -0800 | [diff] [blame] | 3124 | |
| 3125 | list_for_each_entry(t, &pktgen_threads, th_list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3126 | sig = pktgen_wait_thread_run(t); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3127 | if (sig == 0) |
| 3128 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3129 | } |
Luiz Capitulino | cdcdbe0 | 2006-03-20 22:16:40 -0800 | [diff] [blame] | 3130 | |
| 3131 | if (sig == 0) |
| 3132 | list_for_each_entry(t, &pktgen_threads, th_list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3133 | t->control |= (T_STOP); |
Luiz Capitulino | cdcdbe0 | 2006-03-20 22:16:40 -0800 | [diff] [blame] | 3134 | |
Luiz Capitulino | 6146e6a | 2006-03-20 22:24:45 -0800 | [diff] [blame] | 3135 | mutex_unlock(&pktgen_thread_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3136 | return sig; |
| 3137 | } |
| 3138 | |
| 3139 | static void pktgen_run_all_threads(void) |
| 3140 | { |
Luiz Capitulino | cdcdbe0 | 2006-03-20 22:16:40 -0800 | [diff] [blame] | 3141 | struct pktgen_thread *t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3142 | |
Stephen Hemminger | 25c4e53 | 2007-03-04 16:06:47 -0800 | [diff] [blame] | 3143 | pr_debug("pktgen: entering pktgen_run_all_threads.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3144 | |
Luiz Capitulino | 6146e6a | 2006-03-20 22:24:45 -0800 | [diff] [blame] | 3145 | mutex_lock(&pktgen_thread_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3146 | |
Luiz Capitulino | cdcdbe0 | 2006-03-20 22:16:40 -0800 | [diff] [blame] | 3147 | list_for_each_entry(t, &pktgen_threads, th_list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3148 | t->control |= (T_RUN); |
Luiz Capitulino | cdcdbe0 | 2006-03-20 22:16:40 -0800 | [diff] [blame] | 3149 | |
Luiz Capitulino | 6146e6a | 2006-03-20 22:24:45 -0800 | [diff] [blame] | 3150 | mutex_unlock(&pktgen_thread_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3151 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3152 | schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */ |
| 3153 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3154 | pktgen_wait_all_threads_run(); |
| 3155 | } |
| 3156 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3157 | static void show_results(struct pktgen_dev *pkt_dev, int nr_frags) |
| 3158 | { |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3159 | __u64 total_us, bps, mbps, pps, idle; |
| 3160 | char *p = pkt_dev->result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3161 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3162 | total_us = pkt_dev->stopped_at - pkt_dev->started_at; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3163 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3164 | idle = pkt_dev->idle_acc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3165 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3166 | p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n", |
| 3167 | (unsigned long long)total_us, |
| 3168 | (unsigned long long)(total_us - idle), |
| 3169 | (unsigned long long)idle, |
| 3170 | (unsigned long long)pkt_dev->sofar, |
| 3171 | pkt_dev->cur_pkt_size, nr_frags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3172 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3173 | pps = pkt_dev->sofar * USEC_PER_SEC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3174 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3175 | while ((total_us >> 32) != 0) { |
| 3176 | pps >>= 1; |
| 3177 | total_us >>= 1; |
| 3178 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3179 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3180 | do_div(pps, total_us); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3181 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3182 | bps = pps * 8 * pkt_dev->cur_pkt_size; |
| 3183 | |
| 3184 | mbps = bps; |
| 3185 | do_div(mbps, 1000000); |
| 3186 | p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu", |
| 3187 | (unsigned long long)pps, |
| 3188 | (unsigned long long)mbps, |
| 3189 | (unsigned long long)bps, |
| 3190 | (unsigned long long)pkt_dev->errors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3191 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3192 | |
| 3193 | /* Set stopped-at timer, remove from running list, do counters & statistics */ |
| 3194 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3195 | static int pktgen_stop_device(struct pktgen_dev *pkt_dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3196 | { |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3197 | int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1; |
Arthur Kepner | 95ed63f | 2006-03-20 21:26:56 -0800 | [diff] [blame] | 3198 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3199 | if (!pkt_dev->running) { |
David S. Miller | 25a8b25 | 2007-07-30 16:11:48 -0700 | [diff] [blame] | 3200 | printk(KERN_WARNING "pktgen: interface: %s is already " |
| 3201 | "stopped\n", pkt_dev->odev->name); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3202 | return -EINVAL; |
| 3203 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3204 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3205 | pkt_dev->stopped_at = getCurUs(); |
| 3206 | pkt_dev->running = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3207 | |
Arthur Kepner | 95ed63f | 2006-03-20 21:26:56 -0800 | [diff] [blame] | 3208 | show_results(pkt_dev, nr_frags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3209 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3210 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3211 | } |
| 3212 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3213 | static struct pktgen_dev *next_to_run(struct pktgen_thread *t) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3214 | { |
Luiz Capitulino | c26a801 | 2006-03-20 22:18:16 -0800 | [diff] [blame] | 3215 | struct pktgen_dev *pkt_dev, *best = NULL; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3216 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3217 | if_lock(t); |
| 3218 | |
Luiz Capitulino | c26a801 | 2006-03-20 22:18:16 -0800 | [diff] [blame] | 3219 | list_for_each_entry(pkt_dev, &t->if_list, list) { |
| 3220 | if (!pkt_dev->running) |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3221 | continue; |
| 3222 | if (best == NULL) |
Luiz Capitulino | c26a801 | 2006-03-20 22:18:16 -0800 | [diff] [blame] | 3223 | best = pkt_dev; |
| 3224 | else if (pkt_dev->next_tx_us < best->next_tx_us) |
| 3225 | best = pkt_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3226 | } |
| 3227 | if_unlock(t); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3228 | return best; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3229 | } |
| 3230 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3231 | static void pktgen_stop(struct pktgen_thread *t) |
| 3232 | { |
Luiz Capitulino | c26a801 | 2006-03-20 22:18:16 -0800 | [diff] [blame] | 3233 | struct pktgen_dev *pkt_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3234 | |
Stephen Hemminger | 25c4e53 | 2007-03-04 16:06:47 -0800 | [diff] [blame] | 3235 | pr_debug("pktgen: entering pktgen_stop\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3236 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3237 | if_lock(t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3238 | |
Luiz Capitulino | c26a801 | 2006-03-20 22:18:16 -0800 | [diff] [blame] | 3239 | list_for_each_entry(pkt_dev, &t->if_list, list) { |
| 3240 | pktgen_stop_device(pkt_dev); |
| 3241 | if (pkt_dev->skb) |
| 3242 | kfree_skb(pkt_dev->skb); |
Arthur Kepner | 95ed63f | 2006-03-20 21:26:56 -0800 | [diff] [blame] | 3243 | |
Luiz Capitulino | c26a801 | 2006-03-20 22:18:16 -0800 | [diff] [blame] | 3244 | pkt_dev->skb = NULL; |
Arthur Kepner | 95ed63f | 2006-03-20 21:26:56 -0800 | [diff] [blame] | 3245 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3246 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3247 | if_unlock(t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3248 | } |
| 3249 | |
Arthur Kepner | 95ed63f | 2006-03-20 21:26:56 -0800 | [diff] [blame] | 3250 | /* |
| 3251 | * one of our devices needs to be removed - find it |
| 3252 | * and remove it |
| 3253 | */ |
| 3254 | static void pktgen_rem_one_if(struct pktgen_thread *t) |
| 3255 | { |
Luiz Capitulino | c26a801 | 2006-03-20 22:18:16 -0800 | [diff] [blame] | 3256 | struct list_head *q, *n; |
| 3257 | struct pktgen_dev *cur; |
Arthur Kepner | 95ed63f | 2006-03-20 21:26:56 -0800 | [diff] [blame] | 3258 | |
Stephen Hemminger | 25c4e53 | 2007-03-04 16:06:47 -0800 | [diff] [blame] | 3259 | pr_debug("pktgen: entering pktgen_rem_one_if\n"); |
Arthur Kepner | 95ed63f | 2006-03-20 21:26:56 -0800 | [diff] [blame] | 3260 | |
| 3261 | if_lock(t); |
| 3262 | |
Luiz Capitulino | c26a801 | 2006-03-20 22:18:16 -0800 | [diff] [blame] | 3263 | list_for_each_safe(q, n, &t->if_list) { |
| 3264 | cur = list_entry(q, struct pktgen_dev, list); |
Arthur Kepner | 95ed63f | 2006-03-20 21:26:56 -0800 | [diff] [blame] | 3265 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3266 | if (!cur->removal_mark) |
| 3267 | continue; |
Arthur Kepner | 95ed63f | 2006-03-20 21:26:56 -0800 | [diff] [blame] | 3268 | |
| 3269 | if (cur->skb) |
| 3270 | kfree_skb(cur->skb); |
| 3271 | cur->skb = NULL; |
| 3272 | |
| 3273 | pktgen_remove_device(t, cur); |
| 3274 | |
| 3275 | break; |
| 3276 | } |
| 3277 | |
| 3278 | if_unlock(t); |
| 3279 | } |
| 3280 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3281 | static void pktgen_rem_all_ifs(struct pktgen_thread *t) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3282 | { |
Luiz Capitulino | c26a801 | 2006-03-20 22:18:16 -0800 | [diff] [blame] | 3283 | struct list_head *q, *n; |
| 3284 | struct pktgen_dev *cur; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3285 | |
| 3286 | /* Remove all devices, free mem */ |
Arthur Kepner | 95ed63f | 2006-03-20 21:26:56 -0800 | [diff] [blame] | 3287 | |
Stephen Hemminger | 25c4e53 | 2007-03-04 16:06:47 -0800 | [diff] [blame] | 3288 | pr_debug("pktgen: entering pktgen_rem_all_ifs\n"); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3289 | if_lock(t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3290 | |
Luiz Capitulino | c26a801 | 2006-03-20 22:18:16 -0800 | [diff] [blame] | 3291 | list_for_each_safe(q, n, &t->if_list) { |
| 3292 | cur = list_entry(q, struct pktgen_dev, list); |
Arthur Kepner | 95ed63f | 2006-03-20 21:26:56 -0800 | [diff] [blame] | 3293 | |
| 3294 | if (cur->skb) |
| 3295 | kfree_skb(cur->skb); |
| 3296 | cur->skb = NULL; |
| 3297 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3298 | pktgen_remove_device(t, cur); |
| 3299 | } |
| 3300 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3301 | if_unlock(t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3302 | } |
| 3303 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3304 | static void pktgen_rem_thread(struct pktgen_thread *t) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3305 | { |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3306 | /* Remove from the thread list */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3307 | |
David S. Miller | ee74baa | 2007-01-01 20:51:53 -0800 | [diff] [blame] | 3308 | remove_proc_entry(t->tsk->comm, pg_proc_dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3309 | |
Luiz Capitulino | 6146e6a | 2006-03-20 22:24:45 -0800 | [diff] [blame] | 3310 | mutex_lock(&pktgen_thread_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3311 | |
Luiz Capitulino | cdcdbe0 | 2006-03-20 22:16:40 -0800 | [diff] [blame] | 3312 | list_del(&t->th_list); |
| 3313 | |
Luiz Capitulino | 6146e6a | 2006-03-20 22:24:45 -0800 | [diff] [blame] | 3314 | mutex_unlock(&pktgen_thread_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3315 | } |
| 3316 | |
| 3317 | static __inline__ void pktgen_xmit(struct pktgen_dev *pkt_dev) |
| 3318 | { |
| 3319 | struct net_device *odev = NULL; |
| 3320 | __u64 idle_start = 0; |
| 3321 | int ret; |
| 3322 | |
| 3323 | odev = pkt_dev->odev; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3324 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3325 | if (pkt_dev->delay_us || pkt_dev->delay_ns) { |
| 3326 | u64 now; |
| 3327 | |
| 3328 | now = getCurUs(); |
| 3329 | if (now < pkt_dev->next_tx_us) |
| 3330 | spin(pkt_dev, pkt_dev->next_tx_us); |
| 3331 | |
| 3332 | /* This is max DELAY, this has special meaning of |
| 3333 | * "never transmit" |
| 3334 | */ |
| 3335 | if (pkt_dev->delay_us == 0x7FFFFFFF) { |
| 3336 | pkt_dev->next_tx_us = getCurUs() + pkt_dev->delay_us; |
| 3337 | pkt_dev->next_tx_ns = pkt_dev->delay_ns; |
| 3338 | goto out; |
| 3339 | } |
| 3340 | } |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3341 | |
Peter P Waskiewicz Jr | f25f4e4 | 2007-07-06 13:36:20 -0700 | [diff] [blame] | 3342 | if ((netif_queue_stopped(odev) || |
Robert Olsson | 378be2c | 2007-08-28 15:43:14 -0700 | [diff] [blame] | 3343 | (pkt_dev->skb && |
| 3344 | netif_subqueue_stopped(odev, pkt_dev->skb->queue_mapping))) || |
| 3345 | need_resched()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3346 | idle_start = getCurUs(); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3347 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3348 | if (!netif_running(odev)) { |
| 3349 | pktgen_stop_device(pkt_dev); |
Arthur Kepner | 95ed63f | 2006-03-20 21:26:56 -0800 | [diff] [blame] | 3350 | if (pkt_dev->skb) |
| 3351 | kfree_skb(pkt_dev->skb); |
| 3352 | pkt_dev->skb = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3353 | goto out; |
| 3354 | } |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3355 | if (need_resched()) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3356 | schedule(); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3357 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3358 | pkt_dev->idle_acc += getCurUs() - idle_start; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3359 | |
Peter P Waskiewicz Jr | f25f4e4 | 2007-07-06 13:36:20 -0700 | [diff] [blame] | 3360 | if (netif_queue_stopped(odev) || |
| 3361 | netif_subqueue_stopped(odev, pkt_dev->skb->queue_mapping)) { |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3362 | pkt_dev->next_tx_us = getCurUs(); /* TODO */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3363 | pkt_dev->next_tx_ns = 0; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3364 | goto out; /* Try the next interface */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3365 | } |
| 3366 | } |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3367 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3368 | if (pkt_dev->last_ok || !pkt_dev->skb) { |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3369 | if ((++pkt_dev->clone_count >= pkt_dev->clone_skb) |
| 3370 | || (!pkt_dev->skb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3371 | /* build a new pkt */ |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3372 | if (pkt_dev->skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3373 | kfree_skb(pkt_dev->skb); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3374 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3375 | pkt_dev->skb = fill_packet(odev, pkt_dev); |
| 3376 | if (pkt_dev->skb == NULL) { |
David S. Miller | 25a8b25 | 2007-07-30 16:11:48 -0700 | [diff] [blame] | 3377 | printk(KERN_ERR "pktgen: ERROR: couldn't " |
| 3378 | "allocate skb in fill_packet.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3379 | schedule(); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3380 | pkt_dev->clone_count--; /* back out increment, OOM */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3381 | goto out; |
| 3382 | } |
| 3383 | pkt_dev->allocated_skbs++; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3384 | pkt_dev->clone_count = 0; /* reset counter */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3385 | } |
| 3386 | } |
Arthur Kepner | 95ed63f | 2006-03-20 21:26:56 -0800 | [diff] [blame] | 3387 | |
Herbert Xu | 932ff27 | 2006-06-09 12:20:56 -0700 | [diff] [blame] | 3388 | netif_tx_lock_bh(odev); |
Peter P Waskiewicz Jr | f25f4e4 | 2007-07-06 13:36:20 -0700 | [diff] [blame] | 3389 | if (!netif_queue_stopped(odev) && |
| 3390 | !netif_subqueue_stopped(odev, pkt_dev->skb->queue_mapping)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3391 | |
| 3392 | atomic_inc(&(pkt_dev->skb->users)); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3393 | retry_now: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3394 | ret = odev->hard_start_xmit(pkt_dev->skb, odev); |
| 3395 | if (likely(ret == NETDEV_TX_OK)) { |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3396 | pkt_dev->last_ok = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3397 | pkt_dev->sofar++; |
| 3398 | pkt_dev->seq_num++; |
| 3399 | pkt_dev->tx_bytes += pkt_dev->cur_pkt_size; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3400 | |
| 3401 | } else if (ret == NETDEV_TX_LOCKED |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3402 | && (odev->features & NETIF_F_LLTX)) { |
| 3403 | cpu_relax(); |
| 3404 | goto retry_now; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3405 | } else { /* Retry it next time */ |
| 3406 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3407 | atomic_dec(&(pkt_dev->skb->users)); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3408 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3409 | if (debug && net_ratelimit()) |
| 3410 | printk(KERN_INFO "pktgen: Hard xmit error\n"); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3411 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3412 | pkt_dev->errors++; |
| 3413 | pkt_dev->last_ok = 0; |
| 3414 | } |
| 3415 | |
| 3416 | pkt_dev->next_tx_us = getCurUs(); |
| 3417 | pkt_dev->next_tx_ns = 0; |
| 3418 | |
| 3419 | pkt_dev->next_tx_us += pkt_dev->delay_us; |
| 3420 | pkt_dev->next_tx_ns += pkt_dev->delay_ns; |
| 3421 | |
| 3422 | if (pkt_dev->next_tx_ns > 1000) { |
| 3423 | pkt_dev->next_tx_us++; |
| 3424 | pkt_dev->next_tx_ns -= 1000; |
| 3425 | } |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3426 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3427 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3428 | else { /* Retry it next time */ |
| 3429 | pkt_dev->last_ok = 0; |
| 3430 | pkt_dev->next_tx_us = getCurUs(); /* TODO */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3431 | pkt_dev->next_tx_ns = 0; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3432 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3433 | |
Herbert Xu | 932ff27 | 2006-06-09 12:20:56 -0700 | [diff] [blame] | 3434 | netif_tx_unlock_bh(odev); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3435 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3436 | /* If pkt_dev->count is zero, then run forever */ |
| 3437 | if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) { |
| 3438 | if (atomic_read(&(pkt_dev->skb->users)) != 1) { |
| 3439 | idle_start = getCurUs(); |
| 3440 | while (atomic_read(&(pkt_dev->skb->users)) != 1) { |
| 3441 | if (signal_pending(current)) { |
| 3442 | break; |
| 3443 | } |
| 3444 | schedule(); |
| 3445 | } |
| 3446 | pkt_dev->idle_acc += getCurUs() - idle_start; |
| 3447 | } |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3448 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3449 | /* Done with this */ |
| 3450 | pktgen_stop_device(pkt_dev); |
Arthur Kepner | 95ed63f | 2006-03-20 21:26:56 -0800 | [diff] [blame] | 3451 | if (pkt_dev->skb) |
| 3452 | kfree_skb(pkt_dev->skb); |
| 3453 | pkt_dev->skb = NULL; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3454 | } |
| 3455 | out:; |
| 3456 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3457 | |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 3458 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3459 | * Main loop of the thread goes here |
| 3460 | */ |
| 3461 | |
David S. Miller | ee74baa | 2007-01-01 20:51:53 -0800 | [diff] [blame] | 3462 | static int pktgen_thread_worker(void *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3463 | { |
| 3464 | DEFINE_WAIT(wait); |
David S. Miller | ee74baa | 2007-01-01 20:51:53 -0800 | [diff] [blame] | 3465 | struct pktgen_thread *t = arg; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3466 | struct pktgen_dev *pkt_dev = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3467 | int cpu = t->cpu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3468 | u32 max_before_softirq; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3469 | u32 tx_since_softirq = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3470 | |
David S. Miller | ee74baa | 2007-01-01 20:51:53 -0800 | [diff] [blame] | 3471 | BUG_ON(smp_processor_id() != cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3472 | |
| 3473 | init_waitqueue_head(&t->queue); |
| 3474 | |
Stephen Hemminger | 25c4e53 | 2007-03-04 16:06:47 -0800 | [diff] [blame] | 3475 | pr_debug("pktgen: starting pktgen/%d: pid=%d\n", cpu, current->pid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3476 | |
| 3477 | max_before_softirq = t->max_before_softirq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3478 | |
David S. Miller | ee74baa | 2007-01-01 20:51:53 -0800 | [diff] [blame] | 3479 | set_current_state(TASK_INTERRUPTIBLE); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3480 | |
Rafael J. Wysocki | 8314418 | 2007-07-17 04:03:35 -0700 | [diff] [blame] | 3481 | set_freezable(); |
| 3482 | |
David S. Miller | ee74baa | 2007-01-01 20:51:53 -0800 | [diff] [blame] | 3483 | while (!kthread_should_stop()) { |
| 3484 | pkt_dev = next_to_run(t); |
| 3485 | |
| 3486 | if (!pkt_dev && |
| 3487 | (t->control & (T_STOP | T_RUN | T_REMDEVALL | T_REMDEV)) |
| 3488 | == 0) { |
| 3489 | prepare_to_wait(&(t->queue), &wait, |
| 3490 | TASK_INTERRUPTIBLE); |
| 3491 | schedule_timeout(HZ / 10); |
| 3492 | finish_wait(&(t->queue), &wait); |
| 3493 | } |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3494 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3495 | __set_current_state(TASK_RUNNING); |
| 3496 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3497 | if (pkt_dev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3498 | |
| 3499 | pktgen_xmit(pkt_dev); |
| 3500 | |
| 3501 | /* |
| 3502 | * We like to stay RUNNING but must also give |
| 3503 | * others fair share. |
| 3504 | */ |
| 3505 | |
| 3506 | tx_since_softirq += pkt_dev->last_ok; |
| 3507 | |
| 3508 | if (tx_since_softirq > max_before_softirq) { |
| 3509 | if (local_softirq_pending()) |
| 3510 | do_softirq(); |
| 3511 | tx_since_softirq = 0; |
| 3512 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3513 | } |
| 3514 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3515 | if (t->control & T_STOP) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3516 | pktgen_stop(t); |
| 3517 | t->control &= ~(T_STOP); |
| 3518 | } |
| 3519 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3520 | if (t->control & T_RUN) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3521 | pktgen_run(t); |
| 3522 | t->control &= ~(T_RUN); |
| 3523 | } |
| 3524 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3525 | if (t->control & T_REMDEVALL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3526 | pktgen_rem_all_ifs(t); |
Arthur Kepner | 95ed63f | 2006-03-20 21:26:56 -0800 | [diff] [blame] | 3527 | t->control &= ~(T_REMDEVALL); |
| 3528 | } |
| 3529 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3530 | if (t->control & T_REMDEV) { |
Arthur Kepner | 95ed63f | 2006-03-20 21:26:56 -0800 | [diff] [blame] | 3531 | pktgen_rem_one_if(t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3532 | t->control &= ~(T_REMDEV); |
| 3533 | } |
| 3534 | |
Andrew Morton | 09fe3ef | 2007-04-12 14:45:32 -0700 | [diff] [blame] | 3535 | try_to_freeze(); |
| 3536 | |
David S. Miller | ee74baa | 2007-01-01 20:51:53 -0800 | [diff] [blame] | 3537 | set_current_state(TASK_INTERRUPTIBLE); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3538 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3539 | |
Stephen Hemminger | 25c4e53 | 2007-03-04 16:06:47 -0800 | [diff] [blame] | 3540 | pr_debug("pktgen: %s stopping all device\n", t->tsk->comm); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3541 | pktgen_stop(t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3542 | |
Stephen Hemminger | 25c4e53 | 2007-03-04 16:06:47 -0800 | [diff] [blame] | 3543 | pr_debug("pktgen: %s removing all device\n", t->tsk->comm); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3544 | pktgen_rem_all_ifs(t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3545 | |
Stephen Hemminger | 25c4e53 | 2007-03-04 16:06:47 -0800 | [diff] [blame] | 3546 | pr_debug("pktgen: %s removing thread.\n", t->tsk->comm); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3547 | pktgen_rem_thread(t); |
Luiz Capitulino | cdcdbe0 | 2006-03-20 22:16:40 -0800 | [diff] [blame] | 3548 | |
David S. Miller | ee74baa | 2007-01-01 20:51:53 -0800 | [diff] [blame] | 3549 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3550 | } |
| 3551 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3552 | static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t, |
| 3553 | const char *ifname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3554 | { |
Luiz Capitulino | c26a801 | 2006-03-20 22:18:16 -0800 | [diff] [blame] | 3555 | struct pktgen_dev *p, *pkt_dev = NULL; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3556 | if_lock(t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3557 | |
Luiz Capitulino | c26a801 | 2006-03-20 22:18:16 -0800 | [diff] [blame] | 3558 | list_for_each_entry(p, &t->if_list, list) |
Stephen Hemminger | 39df232 | 2007-03-04 16:11:51 -0800 | [diff] [blame] | 3559 | if (strncmp(p->odev->name, ifname, IFNAMSIZ) == 0) { |
Luiz Capitulino | c26a801 | 2006-03-20 22:18:16 -0800 | [diff] [blame] | 3560 | pkt_dev = p; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3561 | break; |
| 3562 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3563 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3564 | if_unlock(t); |
Stephen Hemminger | 25c4e53 | 2007-03-04 16:06:47 -0800 | [diff] [blame] | 3565 | pr_debug("pktgen: find_dev(%s) returning %p\n", ifname, pkt_dev); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3566 | return pkt_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3567 | } |
| 3568 | |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 3569 | /* |
| 3570 | * Adds a dev at front of if_list. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3571 | */ |
| 3572 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3573 | static int add_dev_to_thread(struct pktgen_thread *t, |
| 3574 | struct pktgen_dev *pkt_dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3575 | { |
| 3576 | int rv = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3577 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3578 | if_lock(t); |
| 3579 | |
| 3580 | if (pkt_dev->pg_thread) { |
David S. Miller | 25a8b25 | 2007-07-30 16:11:48 -0700 | [diff] [blame] | 3581 | printk(KERN_ERR "pktgen: ERROR: already assigned " |
| 3582 | "to a thread.\n"); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3583 | rv = -EBUSY; |
| 3584 | goto out; |
| 3585 | } |
Luiz Capitulino | c26a801 | 2006-03-20 22:18:16 -0800 | [diff] [blame] | 3586 | |
| 3587 | list_add(&pkt_dev->list, &t->if_list); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3588 | pkt_dev->pg_thread = t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3589 | pkt_dev->running = 0; |
| 3590 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3591 | out: |
| 3592 | if_unlock(t); |
| 3593 | return rv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3594 | } |
| 3595 | |
| 3596 | /* Called under thread lock */ |
| 3597 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3598 | static int pktgen_add_device(struct pktgen_thread *t, const char *ifname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3599 | { |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3600 | struct pktgen_dev *pkt_dev; |
Stephen Hemminger | 39df232 | 2007-03-04 16:11:51 -0800 | [diff] [blame] | 3601 | int err; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3602 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3603 | /* We don't allow a device to be on several threads */ |
| 3604 | |
Stephen Hemminger | d50a6b5 | 2005-10-14 15:42:33 -0700 | [diff] [blame] | 3605 | pkt_dev = __pktgen_NN_threads(ifname, FIND); |
| 3606 | if (pkt_dev) { |
David S. Miller | 25a8b25 | 2007-07-30 16:11:48 -0700 | [diff] [blame] | 3607 | printk(KERN_ERR "pktgen: ERROR: interface already used.\n"); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3608 | return -EBUSY; |
| 3609 | } |
Stephen Hemminger | d50a6b5 | 2005-10-14 15:42:33 -0700 | [diff] [blame] | 3610 | |
| 3611 | pkt_dev = kzalloc(sizeof(struct pktgen_dev), GFP_KERNEL); |
| 3612 | if (!pkt_dev) |
| 3613 | return -ENOMEM; |
| 3614 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3615 | pkt_dev->flows = vmalloc(MAX_CFLOWS * sizeof(struct flow_state)); |
Stephen Hemminger | d50a6b5 | 2005-10-14 15:42:33 -0700 | [diff] [blame] | 3616 | if (pkt_dev->flows == NULL) { |
| 3617 | kfree(pkt_dev); |
| 3618 | return -ENOMEM; |
| 3619 | } |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3620 | memset(pkt_dev->flows, 0, MAX_CFLOWS * sizeof(struct flow_state)); |
Stephen Hemminger | d50a6b5 | 2005-10-14 15:42:33 -0700 | [diff] [blame] | 3621 | |
Arthur Kepner | 95ed63f | 2006-03-20 21:26:56 -0800 | [diff] [blame] | 3622 | pkt_dev->removal_mark = 0; |
Stephen Hemminger | d50a6b5 | 2005-10-14 15:42:33 -0700 | [diff] [blame] | 3623 | pkt_dev->min_pkt_size = ETH_ZLEN; |
| 3624 | pkt_dev->max_pkt_size = ETH_ZLEN; |
| 3625 | pkt_dev->nfrags = 0; |
| 3626 | pkt_dev->clone_skb = pg_clone_skb_d; |
| 3627 | pkt_dev->delay_us = pg_delay_d / 1000; |
| 3628 | pkt_dev->delay_ns = pg_delay_d % 1000; |
| 3629 | pkt_dev->count = pg_count_d; |
| 3630 | pkt_dev->sofar = 0; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3631 | pkt_dev->udp_src_min = 9; /* sink port */ |
Stephen Hemminger | d50a6b5 | 2005-10-14 15:42:33 -0700 | [diff] [blame] | 3632 | pkt_dev->udp_src_max = 9; |
| 3633 | pkt_dev->udp_dst_min = 9; |
| 3634 | pkt_dev->udp_dst_max = 9; |
| 3635 | |
Francesco Fondelli | 34954dd | 2006-09-27 16:30:44 -0700 | [diff] [blame] | 3636 | pkt_dev->vlan_p = 0; |
| 3637 | pkt_dev->vlan_cfi = 0; |
| 3638 | pkt_dev->vlan_id = 0xffff; |
| 3639 | pkt_dev->svlan_p = 0; |
| 3640 | pkt_dev->svlan_cfi = 0; |
| 3641 | pkt_dev->svlan_id = 0xffff; |
| 3642 | |
Stephen Hemminger | 39df232 | 2007-03-04 16:11:51 -0800 | [diff] [blame] | 3643 | err = pktgen_setup_dev(pkt_dev, ifname); |
| 3644 | if (err) |
| 3645 | goto out1; |
Stephen Hemminger | d50a6b5 | 2005-10-14 15:42:33 -0700 | [diff] [blame] | 3646 | |
Stephen Hemminger | 39df232 | 2007-03-04 16:11:51 -0800 | [diff] [blame] | 3647 | pkt_dev->entry = create_proc_entry(ifname, 0600, pg_proc_dir); |
| 3648 | if (!pkt_dev->entry) { |
David S. Miller | 25a8b25 | 2007-07-30 16:11:48 -0700 | [diff] [blame] | 3649 | printk(KERN_ERR "pktgen: cannot create %s/%s procfs entry.\n", |
Stephen Hemminger | d50a6b5 | 2005-10-14 15:42:33 -0700 | [diff] [blame] | 3650 | PG_PROC_DIR, ifname); |
Stephen Hemminger | 39df232 | 2007-03-04 16:11:51 -0800 | [diff] [blame] | 3651 | err = -EINVAL; |
| 3652 | goto out2; |
Stephen Hemminger | d50a6b5 | 2005-10-14 15:42:33 -0700 | [diff] [blame] | 3653 | } |
Stephen Hemminger | 39df232 | 2007-03-04 16:11:51 -0800 | [diff] [blame] | 3654 | pkt_dev->entry->proc_fops = &pktgen_if_fops; |
| 3655 | pkt_dev->entry->data = pkt_dev; |
Jamal Hadi Salim | a553e4a | 2007-07-02 22:41:59 -0700 | [diff] [blame] | 3656 | #ifdef CONFIG_XFRM |
| 3657 | pkt_dev->ipsmode = XFRM_MODE_TRANSPORT; |
| 3658 | pkt_dev->ipsproto = IPPROTO_ESP; |
| 3659 | #endif |
Stephen Hemminger | d50a6b5 | 2005-10-14 15:42:33 -0700 | [diff] [blame] | 3660 | |
| 3661 | return add_dev_to_thread(t, pkt_dev); |
Stephen Hemminger | 39df232 | 2007-03-04 16:11:51 -0800 | [diff] [blame] | 3662 | out2: |
| 3663 | dev_put(pkt_dev->odev); |
| 3664 | out1: |
Jamal Hadi Salim | a553e4a | 2007-07-02 22:41:59 -0700 | [diff] [blame] | 3665 | #ifdef CONFIG_XFRM |
| 3666 | free_SAs(pkt_dev); |
| 3667 | #endif |
Stephen Hemminger | 39df232 | 2007-03-04 16:11:51 -0800 | [diff] [blame] | 3668 | if (pkt_dev->flows) |
| 3669 | vfree(pkt_dev->flows); |
| 3670 | kfree(pkt_dev); |
| 3671 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3672 | } |
| 3673 | |
David S. Miller | ee74baa | 2007-01-01 20:51:53 -0800 | [diff] [blame] | 3674 | static int __init pktgen_create_thread(int cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3675 | { |
Luiz Capitulino | cdcdbe0 | 2006-03-20 22:16:40 -0800 | [diff] [blame] | 3676 | struct pktgen_thread *t; |
Stephen Hemminger | d50a6b5 | 2005-10-14 15:42:33 -0700 | [diff] [blame] | 3677 | struct proc_dir_entry *pe; |
David S. Miller | ee74baa | 2007-01-01 20:51:53 -0800 | [diff] [blame] | 3678 | struct task_struct *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3679 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3680 | t = kzalloc(sizeof(struct pktgen_thread), GFP_KERNEL); |
| 3681 | if (!t) { |
David S. Miller | 25a8b25 | 2007-07-30 16:11:48 -0700 | [diff] [blame] | 3682 | printk(KERN_ERR "pktgen: ERROR: out of memory, can't " |
| 3683 | "create new thread.\n"); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3684 | return -ENOMEM; |
| 3685 | } |
| 3686 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3687 | spin_lock_init(&t->if_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3688 | t->cpu = cpu; |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3689 | |
David S. Miller | ee74baa | 2007-01-01 20:51:53 -0800 | [diff] [blame] | 3690 | INIT_LIST_HEAD(&t->if_list); |
| 3691 | |
| 3692 | list_add_tail(&t->th_list, &pktgen_threads); |
| 3693 | |
| 3694 | p = kthread_create(pktgen_thread_worker, t, "kpktgend_%d", cpu); |
| 3695 | if (IS_ERR(p)) { |
David S. Miller | 25a8b25 | 2007-07-30 16:11:48 -0700 | [diff] [blame] | 3696 | printk(KERN_ERR "pktgen: kernel_thread() failed " |
| 3697 | "for cpu %d\n", t->cpu); |
David S. Miller | ee74baa | 2007-01-01 20:51:53 -0800 | [diff] [blame] | 3698 | list_del(&t->th_list); |
| 3699 | kfree(t); |
| 3700 | return PTR_ERR(p); |
| 3701 | } |
| 3702 | kthread_bind(p, cpu); |
| 3703 | t->tsk = p; |
| 3704 | |
| 3705 | pe = create_proc_entry(t->tsk->comm, 0600, pg_proc_dir); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3706 | if (!pe) { |
David S. Miller | 25a8b25 | 2007-07-30 16:11:48 -0700 | [diff] [blame] | 3707 | printk(KERN_ERR "pktgen: cannot create %s/%s procfs entry.\n", |
David S. Miller | ee74baa | 2007-01-01 20:51:53 -0800 | [diff] [blame] | 3708 | PG_PROC_DIR, t->tsk->comm); |
| 3709 | kthread_stop(p); |
| 3710 | list_del(&t->th_list); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3711 | kfree(t); |
| 3712 | return -EINVAL; |
| 3713 | } |
Stephen Hemminger | d50a6b5 | 2005-10-14 15:42:33 -0700 | [diff] [blame] | 3714 | |
| 3715 | pe->proc_fops = &pktgen_thread_fops; |
| 3716 | pe->data = t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3717 | |
David S. Miller | ee74baa | 2007-01-01 20:51:53 -0800 | [diff] [blame] | 3718 | wake_up_process(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3719 | |
| 3720 | return 0; |
| 3721 | } |
| 3722 | |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 3723 | /* |
| 3724 | * Removes a device from the thread if_list. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3725 | */ |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3726 | static void _rem_dev_from_if_list(struct pktgen_thread *t, |
| 3727 | struct pktgen_dev *pkt_dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3728 | { |
Luiz Capitulino | c26a801 | 2006-03-20 22:18:16 -0800 | [diff] [blame] | 3729 | struct list_head *q, *n; |
| 3730 | struct pktgen_dev *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3731 | |
Luiz Capitulino | c26a801 | 2006-03-20 22:18:16 -0800 | [diff] [blame] | 3732 | list_for_each_safe(q, n, &t->if_list) { |
| 3733 | p = list_entry(q, struct pktgen_dev, list); |
| 3734 | if (p == pkt_dev) |
| 3735 | list_del(&p->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3736 | } |
| 3737 | } |
| 3738 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3739 | static int pktgen_remove_device(struct pktgen_thread *t, |
| 3740 | struct pktgen_dev *pkt_dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3741 | { |
| 3742 | |
Stephen Hemminger | 25c4e53 | 2007-03-04 16:06:47 -0800 | [diff] [blame] | 3743 | pr_debug("pktgen: remove_device pkt_dev=%p\n", pkt_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3744 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3745 | if (pkt_dev->running) { |
David S. Miller | 25a8b25 | 2007-07-30 16:11:48 -0700 | [diff] [blame] | 3746 | printk(KERN_WARNING "pktgen: WARNING: trying to remove a " |
| 3747 | "running interface, stopping it now.\n"); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3748 | pktgen_stop_device(pkt_dev); |
| 3749 | } |
| 3750 | |
| 3751 | /* Dis-associate from the interface */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3752 | |
| 3753 | if (pkt_dev->odev) { |
| 3754 | dev_put(pkt_dev->odev); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3755 | pkt_dev->odev = NULL; |
| 3756 | } |
| 3757 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3758 | /* And update the thread if_list */ |
| 3759 | |
| 3760 | _rem_dev_from_if_list(t, pkt_dev); |
| 3761 | |
Stephen Hemminger | 39df232 | 2007-03-04 16:11:51 -0800 | [diff] [blame] | 3762 | if (pkt_dev->entry) |
| 3763 | remove_proc_entry(pkt_dev->entry->name, pg_proc_dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3764 | |
Jamal Hadi Salim | a553e4a | 2007-07-02 22:41:59 -0700 | [diff] [blame] | 3765 | #ifdef CONFIG_XFRM |
| 3766 | free_SAs(pkt_dev); |
| 3767 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3768 | if (pkt_dev->flows) |
| 3769 | vfree(pkt_dev->flows); |
| 3770 | kfree(pkt_dev); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3771 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3772 | } |
| 3773 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3774 | static int __init pg_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3775 | { |
| 3776 | int cpu; |
Stephen Hemminger | d50a6b5 | 2005-10-14 15:42:33 -0700 | [diff] [blame] | 3777 | struct proc_dir_entry *pe; |
| 3778 | |
David S. Miller | 25a8b25 | 2007-07-30 16:11:48 -0700 | [diff] [blame] | 3779 | printk(KERN_INFO "%s", version); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3780 | |
Stephen Hemminger | d50a6b5 | 2005-10-14 15:42:33 -0700 | [diff] [blame] | 3781 | pg_proc_dir = proc_mkdir(PG_PROC_DIR, proc_net); |
| 3782 | if (!pg_proc_dir) |
| 3783 | return -ENODEV; |
| 3784 | pg_proc_dir->owner = THIS_MODULE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3785 | |
Stephen Hemminger | d50a6b5 | 2005-10-14 15:42:33 -0700 | [diff] [blame] | 3786 | pe = create_proc_entry(PGCTRL, 0600, pg_proc_dir); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3787 | if (pe == NULL) { |
David S. Miller | 25a8b25 | 2007-07-30 16:11:48 -0700 | [diff] [blame] | 3788 | printk(KERN_ERR "pktgen: ERROR: cannot create %s " |
| 3789 | "procfs entry.\n", PGCTRL); |
Stephen Hemminger | d50a6b5 | 2005-10-14 15:42:33 -0700 | [diff] [blame] | 3790 | proc_net_remove(PG_PROC_DIR); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3791 | return -EINVAL; |
| 3792 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3793 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3794 | pe->proc_fops = &pktgen_fops; |
| 3795 | pe->data = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3796 | |
| 3797 | /* Register us to receive netdevice events */ |
| 3798 | register_netdevice_notifier(&pktgen_notifier_block); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3799 | |
John Hawkes | 670c02c | 2005-10-13 09:30:31 -0700 | [diff] [blame] | 3800 | for_each_online_cpu(cpu) { |
Luiz Capitulino | 8024bb2 | 2006-03-20 22:17:55 -0800 | [diff] [blame] | 3801 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3802 | |
David S. Miller | ee74baa | 2007-01-01 20:51:53 -0800 | [diff] [blame] | 3803 | err = pktgen_create_thread(cpu); |
Luiz Capitulino | 8024bb2 | 2006-03-20 22:17:55 -0800 | [diff] [blame] | 3804 | if (err) |
David S. Miller | 25a8b25 | 2007-07-30 16:11:48 -0700 | [diff] [blame] | 3805 | printk(KERN_WARNING "pktgen: WARNING: Cannot create " |
| 3806 | "thread for cpu %d (%d)\n", cpu, err); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3807 | } |
Luiz Capitulino | 8024bb2 | 2006-03-20 22:17:55 -0800 | [diff] [blame] | 3808 | |
| 3809 | if (list_empty(&pktgen_threads)) { |
David S. Miller | 25a8b25 | 2007-07-30 16:11:48 -0700 | [diff] [blame] | 3810 | printk(KERN_ERR "pktgen: ERROR: Initialization failed for " |
| 3811 | "all threads\n"); |
Luiz Capitulino | 8024bb2 | 2006-03-20 22:17:55 -0800 | [diff] [blame] | 3812 | unregister_netdevice_notifier(&pktgen_notifier_block); |
| 3813 | remove_proc_entry(PGCTRL, pg_proc_dir); |
| 3814 | proc_net_remove(PG_PROC_DIR); |
| 3815 | return -ENODEV; |
| 3816 | } |
| 3817 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3818 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3819 | } |
| 3820 | |
| 3821 | static void __exit pg_cleanup(void) |
| 3822 | { |
Luiz Capitulino | cdcdbe0 | 2006-03-20 22:16:40 -0800 | [diff] [blame] | 3823 | struct pktgen_thread *t; |
| 3824 | struct list_head *q, *n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3825 | wait_queue_head_t queue; |
| 3826 | init_waitqueue_head(&queue); |
| 3827 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3828 | /* Stop all interfaces & threads */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3829 | |
Luiz Capitulino | cdcdbe0 | 2006-03-20 22:16:40 -0800 | [diff] [blame] | 3830 | list_for_each_safe(q, n, &pktgen_threads) { |
| 3831 | t = list_entry(q, struct pktgen_thread, th_list); |
David S. Miller | ee74baa | 2007-01-01 20:51:53 -0800 | [diff] [blame] | 3832 | kthread_stop(t->tsk); |
| 3833 | kfree(t); |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3834 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3835 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3836 | /* Un-register us from receiving netdevice events */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3837 | unregister_netdevice_notifier(&pktgen_notifier_block); |
| 3838 | |
Luiz Capitulino | 222f180 | 2006-03-20 22:16:13 -0800 | [diff] [blame] | 3839 | /* Clean up proc file system */ |
Stephen Hemminger | d50a6b5 | 2005-10-14 15:42:33 -0700 | [diff] [blame] | 3840 | remove_proc_entry(PGCTRL, pg_proc_dir); |
| 3841 | proc_net_remove(PG_PROC_DIR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3842 | } |
| 3843 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3844 | module_init(pg_init); |
| 3845 | module_exit(pg_cleanup); |
| 3846 | |
| 3847 | MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se"); |
| 3848 | MODULE_DESCRIPTION("Packet Generator tool"); |
| 3849 | MODULE_LICENSE("GPL"); |
| 3850 | module_param(pg_count_d, int, 0); |
| 3851 | module_param(pg_delay_d, int, 0); |
| 3852 | module_param(pg_clone_skb_d, int, 0); |
| 3853 | module_param(debug, int, 0); |