blob: 4b01496dc33d3439eec32b4fafe74bbcc4538b58 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Hideaki4ec93ed2007-02-09 23:24:36 +090018 * The tool is implemented as a linux module. Parameters are output
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * 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 Hideaki4ec93ed2007-02-09 23:24:36 +090047 * 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 Torvalds1da177e2005-04-16 15:20:36 -070049 * as a "fastpath" with a configurable number of clones after alloc's.
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090050 * 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 Torvalds1da177e2005-04-16 15:20:36 -070052 * clones.
53 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090054 * Also moved to /proc/net/pktgen/
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 * --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 Hideaki4ec93ed2007-02-09 23:24:36 +090063 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 * 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 Hideaki4ec93ed2007-02-09 23:24:36 +090068 * 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 Torvalds1da177e2005-04-16 15:20:36 -070072 * way. The if_lock should be possible to remove when add/rem_device is merged
73 * into this too.
74 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090075 * By design there should only be *one* "controlling" process. In practice
76 * multiple write accesses gives unpredictable result. Understood by "write"
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 * to /proc gives result code thats should be read be the "writer".
Stephen Hemmingerb4099fa2005-10-14 15:32:22 -070078 * For practical use this should be no problem.
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090080 * 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 Torvalds1da177e2005-04-16 15:20:36 -070082 * --ro
83 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090084 * Fix refcount off by one if first packet fails, potential null deref,
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 * 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 Hideaki4ec93ed2007-02-09 23:24:36 +090092 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 * Remove if fix from added Harald Welte <laforge@netfilter.org> 040419
94 * ia64 compilation fix from Aron Griffis <aron@hp.com> 040604
95 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090096 * New xmit() return, do_div and misc clean up by Stephen Hemminger
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 * <shemminger@osdl.org> 040923
98 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090099 * Randy Dunlap fixed u64 printk compiler waring
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 *
101 * Remove FCS from BW calculation. Lennert Buytenhek <buytenh@wantstofly.org>
102 * New time handling. Lennert Buytenhek <buytenh@wantstofly.org> 041213
103 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900104 * Corrections from Nikolai Malykh (nmalykh@bilim.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 * Removed unused flags F_SET_SRCMAC & F_SET_SRCIP 041230
106 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900107 * interruptible_sleep_on_timeout() replaced Nishanth Aravamudan <nacc@us.ibm.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 * 050103
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800109 *
110 * MPLS support by Steven Whitehouse <steve@chygwyn.com>
111 *
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700112 * 802.1Q/Q-in-Q support by Francesco Fondelli (FF) <francesco.fondelli@gmail.com>
113 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 */
115#include <linux/sys.h>
116#include <linux/types.h>
117#include <linux/module.h>
118#include <linux/moduleparam.h>
119#include <linux/kernel.h>
120#include <linux/smp_lock.h>
Luiz Capitulino222fa072006-03-20 22:24:27 -0800121#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122#include <linux/sched.h>
123#include <linux/slab.h>
124#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125#include <linux/unistd.h>
126#include <linux/string.h>
127#include <linux/ptrace.h>
128#include <linux/errno.h>
129#include <linux/ioport.h>
130#include <linux/interrupt.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -0800131#include <linux/capability.h>
Andrew Morton09fe3ef2007-04-12 14:45:32 -0700132#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133#include <linux/delay.h>
134#include <linux/timer.h>
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -0800135#include <linux/list.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136#include <linux/init.h>
137#include <linux/skbuff.h>
138#include <linux/netdevice.h>
139#include <linux/inet.h>
140#include <linux/inetdevice.h>
141#include <linux/rtnetlink.h>
142#include <linux/if_arp.h>
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700143#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144#include <linux/in.h>
145#include <linux/ip.h>
146#include <linux/ipv6.h>
147#include <linux/udp.h>
148#include <linux/proc_fs.h>
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700149#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150#include <linux/wait.h>
Kris Katterjohnf404e9a2006-01-17 13:04:57 -0800151#include <linux/etherdevice.h>
David S. Milleree74baa2007-01-01 20:51:53 -0800152#include <linux/kthread.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153#include <net/checksum.h>
154#include <net/ipv6.h>
155#include <net/addrconf.h>
156#include <asm/byteorder.h>
157#include <linux/rcupdate.h>
158#include <asm/bitops.h>
159#include <asm/io.h>
160#include <asm/dma.h>
161#include <asm/uaccess.h>
Luiz Capitulino222f1802006-03-20 22:16:13 -0800162#include <asm/div64.h> /* do_div */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163#include <asm/timex.h>
164
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700165#define VERSION "pktgen v2.68: Packet Generator for packet performance testing.\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167/* #define PG_DEBUG(a) a */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800168#define PG_DEBUG(a)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170/* The buckets are exponential in 'width' */
171#define LAT_BUCKETS_MAX 32
172#define IP_NAME_SZ 32
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800173#define MAX_MPLS_LABELS 16 /* This is the max label stack depth */
174#define MPLS_STACK_BOTTOM __constant_htonl(0x00000100)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176/* Device flag bits */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800177#define F_IPSRC_RND (1<<0) /* IP-Src Random */
178#define F_IPDST_RND (1<<1) /* IP-Dst Random */
179#define F_UDPSRC_RND (1<<2) /* UDP-Src Random */
180#define F_UDPDST_RND (1<<3) /* UDP-Dst Random */
181#define F_MACSRC_RND (1<<4) /* MAC-Src Random */
182#define F_MACDST_RND (1<<5) /* MAC-Dst Random */
183#define F_TXSIZE_RND (1<<6) /* Transmit size is random */
184#define F_IPV6 (1<<7) /* Interface in IPV6 Mode */
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800185#define F_MPLS_RND (1<<8) /* Random MPLS labels */
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700186#define F_VID_RND (1<<9) /* Random VLAN ID */
187#define F_SVID_RND (1<<10) /* Random SVLAN ID */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189/* Thread control flag bits */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800190#define T_TERMINATE (1<<0)
191#define T_STOP (1<<1) /* Stop run */
192#define T_RUN (1<<2) /* Start run */
193#define T_REMDEVALL (1<<3) /* Remove all devs */
194#define T_REMDEV (1<<4) /* Remove one dev */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196/* If lock -- can be removed after some work */
197#define if_lock(t) spin_lock(&(t->if_lock));
198#define if_unlock(t) spin_unlock(&(t->if_lock));
199
200/* Used to help with determining the pkts on receive */
201#define PKTGEN_MAGIC 0xbe9be955
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700202#define PG_PROC_DIR "pktgen"
203#define PGCTRL "pgctrl"
204static struct proc_dir_entry *pg_proc_dir = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206#define MAX_CFLOWS 65536
207
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700208#define VLAN_TAG_SIZE(x) ((x)->vlan_id == 0xffff ? 0 : 4)
209#define SVLAN_TAG_SIZE(x) ((x)->svlan_id == 0xffff ? 0 : 4)
210
Luiz Capitulino222f1802006-03-20 22:16:13 -0800211struct flow_state {
Al Viro252e3342006-11-14 20:48:11 -0800212 __be32 cur_daddr;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800213 int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214};
215
216struct pktgen_dev {
217
218 /*
219 * Try to keep frequent/infrequent used vars. separated.
220 */
221
Luiz Capitulino222f1802006-03-20 22:16:13 -0800222 char ifname[IFNAMSIZ];
223 char result[512];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Luiz Capitulino222f1802006-03-20 22:16:13 -0800225 struct pktgen_thread *pg_thread; /* the owner */
Luiz Capitulinoc26a8012006-03-20 22:18:16 -0800226 struct list_head list; /* Used for chaining in the thread's run-queue */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Luiz Capitulino222f1802006-03-20 22:16:13 -0800228 int running; /* if this changes to false, the test will stop */
229
230 /* If min != max, then we will either do a linear iteration, or
231 * we will do a random selection from within the range.
232 */
233 __u32 flags;
Arthur Kepner95ed63f2006-03-20 21:26:56 -0800234 int removal_mark; /* non-zero => the device is marked for
235 * removal by worker thread */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Luiz Capitulino222f1802006-03-20 22:16:13 -0800237 int min_pkt_size; /* = ETH_ZLEN; */
238 int max_pkt_size; /* = ETH_ZLEN; */
239 int nfrags;
240 __u32 delay_us; /* Default delay */
241 __u32 delay_ns;
242 __u64 count; /* Default No packets to send */
243 __u64 sofar; /* How many pkts we've sent so far */
244 __u64 tx_bytes; /* How many bytes we've transmitted */
245 __u64 errors; /* Errors when trying to transmit, pkts will be re-sent */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Luiz Capitulino222f1802006-03-20 22:16:13 -0800247 /* runtime counters relating to clone_skb */
248 __u64 next_tx_us; /* timestamp of when to tx next */
249 __u32 next_tx_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Luiz Capitulino222f1802006-03-20 22:16:13 -0800251 __u64 allocated_skbs;
252 __u32 clone_count;
253 int last_ok; /* Was last skb sent?
254 * Or a failed transmit of some sort? This will keep
255 * sequence numbers in order, for example.
256 */
257 __u64 started_at; /* micro-seconds */
258 __u64 stopped_at; /* micro-seconds */
259 __u64 idle_acc; /* micro-seconds */
260 __u32 seq_num;
261
262 int clone_skb; /* Use multiple SKBs during packet gen. If this number
263 * is greater than 1, then that many copies of the same
264 * packet will be sent before a new packet is allocated.
265 * For instance, if you want to send 1024 identical packets
266 * before creating a new packet, set clone_skb to 1024.
267 */
268
269 char dst_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
270 char dst_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
271 char src_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
272 char src_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
273
274 struct in6_addr in6_saddr;
275 struct in6_addr in6_daddr;
276 struct in6_addr cur_in6_daddr;
277 struct in6_addr cur_in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 /* For ranges */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800279 struct in6_addr min_in6_daddr;
280 struct in6_addr max_in6_daddr;
281 struct in6_addr min_in6_saddr;
282 struct in6_addr max_in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Luiz Capitulino222f1802006-03-20 22:16:13 -0800284 /* If we're doing ranges, random or incremental, then this
285 * defines the min/max for those ranges.
286 */
Al Viro252e3342006-11-14 20:48:11 -0800287 __be32 saddr_min; /* inclusive, source IP address */
288 __be32 saddr_max; /* exclusive, source IP address */
289 __be32 daddr_min; /* inclusive, dest IP address */
290 __be32 daddr_max; /* exclusive, dest IP address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Luiz Capitulino222f1802006-03-20 22:16:13 -0800292 __u16 udp_src_min; /* inclusive, source UDP port */
293 __u16 udp_src_max; /* exclusive, source UDP port */
294 __u16 udp_dst_min; /* inclusive, dest UDP port */
295 __u16 udp_dst_max; /* exclusive, dest UDP port */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700297 /* DSCP + ECN */
298 __u8 tos; /* six most significant bits of (former) IPv4 TOS are for dscp codepoint */
299 __u8 traffic_class; /* ditto for the (former) Traffic Class in IPv6 (see RFC 3260, sec. 4) */
300
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800301 /* MPLS */
302 unsigned nr_labels; /* Depth of stack, 0 = no MPLS */
303 __be32 labels[MAX_MPLS_LABELS];
304
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700305 /* VLAN/SVLAN (802.1Q/Q-in-Q) */
306 __u8 vlan_p;
307 __u8 vlan_cfi;
308 __u16 vlan_id; /* 0xffff means no vlan tag */
309
310 __u8 svlan_p;
311 __u8 svlan_cfi;
312 __u16 svlan_id; /* 0xffff means no svlan tag */
313
Luiz Capitulino222f1802006-03-20 22:16:13 -0800314 __u32 src_mac_count; /* How many MACs to iterate through */
315 __u32 dst_mac_count; /* How many MACs to iterate through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Luiz Capitulino222f1802006-03-20 22:16:13 -0800317 unsigned char dst_mac[ETH_ALEN];
318 unsigned char src_mac[ETH_ALEN];
319
320 __u32 cur_dst_mac_offset;
321 __u32 cur_src_mac_offset;
Al Viro252e3342006-11-14 20:48:11 -0800322 __be32 cur_saddr;
323 __be32 cur_daddr;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800324 __u16 cur_udp_dst;
325 __u16 cur_udp_src;
326 __u32 cur_pkt_size;
327
328 __u8 hh[14];
329 /* = {
330 0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
331
332 We fill in SRC address later
333 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
334 0x08, 0x00
335 };
336 */
337 __u16 pad; /* pad out the hh struct to an even 16 bytes */
338
339 struct sk_buff *skb; /* skb we are to transmit next, mainly used for when we
340 * are transmitting the same one multiple times
341 */
342 struct net_device *odev; /* The out-going device. Note that the device should
343 * have it's pg_info pointer pointing back to this
344 * device. This will be set when the user specifies
345 * the out-going device name (not when the inject is
346 * started as it used to do.)
347 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 struct flow_state *flows;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800349 unsigned cflows; /* Concurrent flows (config) */
350 unsigned lflow; /* Flow length (config) */
351 unsigned nflows; /* accumulated flows (stats) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352};
353
354struct pktgen_hdr {
Al Viro252e3342006-11-14 20:48:11 -0800355 __be32 pgh_magic;
356 __be32 seq_num;
357 __be32 tv_sec;
358 __be32 tv_usec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359};
360
361struct pktgen_thread {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800362 spinlock_t if_lock;
Luiz Capitulinoc26a8012006-03-20 22:18:16 -0800363 struct list_head if_list; /* All device here */
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -0800364 struct list_head th_list;
David S. Milleree74baa2007-01-01 20:51:53 -0800365 struct task_struct *tsk;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800366 char result[512];
367 u32 max_before_softirq; /* We'll call do_softirq to prevent starvation. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Luiz Capitulino222f1802006-03-20 22:16:13 -0800369 /* Field for thread to receive "posted" events terminate, stop ifs etc. */
370
371 u32 control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 int pid;
373 int cpu;
374
Luiz Capitulino222f1802006-03-20 22:16:13 -0800375 wait_queue_head_t queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376};
377
378#define REMOVE 1
379#define FIND 0
380
381/* This code works around the fact that do_div cannot handle two 64-bit
382 numbers, and regular 64-bit division doesn't work on x86 kernels.
383 --Ben
384*/
385
386#define PG_DIV 0
387
388/* This was emailed to LMKL by: Chris Caputo <ccaputo@alt.net>
389 * Function copied/adapted/optimized from:
390 *
391 * nemesis.sourceforge.net/browse/lib/static/intmath/ix86/intmath.c.html
392 *
393 * Copyright 1994, University of Cambridge Computer Laboratory
394 * All Rights Reserved.
395 *
396 */
Jesper Juhl77933d72005-07-27 11:46:09 -0700397static inline s64 divremdi3(s64 x, s64 y, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800399 u64 a = (x < 0) ? -x : x;
400 u64 b = (y < 0) ? -y : y;
401 u64 res = 0, d = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Luiz Capitulino222f1802006-03-20 22:16:13 -0800403 if (b > 0) {
404 while (b < a) {
405 b <<= 1;
406 d <<= 1;
407 }
408 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Luiz Capitulino222f1802006-03-20 22:16:13 -0800410 do {
411 if (a >= b) {
412 a -= b;
413 res += d;
414 }
415 b >>= 1;
416 d >>= 1;
417 }
418 while (d);
419
420 if (PG_DIV == type) {
421 return (((x ^ y) & (1ll << 63)) == 0) ? res : -(s64) res;
422 } else {
423 return ((x & (1ll << 63)) == 0) ? a : -(s64) a;
424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425}
426
427/* End of hacks to deal with 64-bit math on x86 */
428
Stephen Hemmingerb4099fa2005-10-14 15:32:22 -0700429/** Convert to milliseconds */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800430static inline __u64 tv_to_ms(const struct timeval *tv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800432 __u64 ms = tv->tv_usec / 1000;
433 ms += (__u64) tv->tv_sec * (__u64) 1000;
434 return ms;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435}
436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437/** Convert to micro-seconds */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800438static inline __u64 tv_to_us(const struct timeval *tv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800440 __u64 us = tv->tv_usec;
441 us += (__u64) tv->tv_sec * (__u64) 1000000;
442 return us;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443}
444
Luiz Capitulino222f1802006-03-20 22:16:13 -0800445static inline __u64 pg_div(__u64 n, __u32 base)
446{
447 __u64 tmp = n;
448 do_div(tmp, base);
449 /* printk("pktgen: pg_div, n: %llu base: %d rv: %llu\n",
450 n, base, tmp); */
451 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452}
453
Luiz Capitulino222f1802006-03-20 22:16:13 -0800454static inline __u64 pg_div64(__u64 n, __u64 base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800456 __u64 tmp = n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457/*
Stephen Hemmingerb4099fa2005-10-14 15:32:22 -0700458 * How do we know if the architecture we are running on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 * supports division with 64 bit base?
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900460 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800462#if defined(__sparc_v9__) || defined(__powerpc64__) || defined(__alpha__) || defined(__x86_64__) || defined(__ia64__)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
Luiz Capitulino222f1802006-03-20 22:16:13 -0800464 do_div(tmp, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465#else
Luiz Capitulino222f1802006-03-20 22:16:13 -0800466 tmp = divremdi3(n, base, PG_DIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467#endif
Luiz Capitulino222f1802006-03-20 22:16:13 -0800468 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469}
470
471static inline u32 pktgen_random(void)
472{
473#if 0
474 __u32 n;
475 get_random_bytes(&n, 4);
476 return n;
477#else
478 return net_random();
479#endif
480}
481
Luiz Capitulino222f1802006-03-20 22:16:13 -0800482static inline __u64 getCurMs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800484 struct timeval tv;
485 do_gettimeofday(&tv);
486 return tv_to_ms(&tv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487}
488
Luiz Capitulino222f1802006-03-20 22:16:13 -0800489static inline __u64 getCurUs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800491 struct timeval tv;
492 do_gettimeofday(&tv);
493 return tv_to_us(&tv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494}
495
Luiz Capitulino222f1802006-03-20 22:16:13 -0800496static inline __u64 tv_diff(const struct timeval *a, const struct timeval *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800498 return tv_to_us(a) - tv_to_us(b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499}
500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501/* old include end */
502
503static char version[] __initdata = VERSION;
504
Luiz Capitulino222f1802006-03-20 22:16:13 -0800505static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *i);
506static int pktgen_add_device(struct pktgen_thread *t, const char *ifname);
507static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
508 const char *ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509static int pktgen_device_event(struct notifier_block *, unsigned long, void *);
510static void pktgen_run_all_threads(void);
511static void pktgen_stop_all_threads_ifs(void);
512static int pktgen_stop_device(struct pktgen_dev *pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800513static void pktgen_stop(struct pktgen_thread *t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514static void pktgen_clear_counters(struct pktgen_dev *pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800515static int pktgen_mark_device(const char *ifname);
516static unsigned int scan_ip6(const char *s, char ip[16]);
517static unsigned int fmt_ip6(char *s, const char ip[16]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
519/* Module parameters, defaults. */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800520static int pg_count_d = 1000; /* 1000 pkts by default */
Jaco Kroonf34fbb972005-12-22 12:51:46 -0800521static int pg_delay_d;
522static int pg_clone_skb_d;
523static int debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Luiz Capitulino222fa072006-03-20 22:24:27 -0800525static DEFINE_MUTEX(pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -0800526static LIST_HEAD(pktgen_threads);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528static struct notifier_block pktgen_notifier_block = {
529 .notifier_call = pktgen_device_event,
530};
531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532/*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900533 * /proc handling functions
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 *
535 */
536
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700537static int pgctrl_show(struct seq_file *seq, void *v)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800538{
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700539 seq_puts(seq, VERSION);
540 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541}
542
Luiz Capitulino222f1802006-03-20 22:16:13 -0800543static ssize_t pgctrl_write(struct file *file, const char __user * buf,
544 size_t count, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 int err = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700547 char data[128];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Luiz Capitulino222f1802006-03-20 22:16:13 -0800549 if (!capable(CAP_NET_ADMIN)) {
550 err = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 goto out;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800552 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700554 if (count > sizeof(data))
555 count = sizeof(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 if (copy_from_user(data, buf, count)) {
Stephen Hemmingerb4099fa2005-10-14 15:32:22 -0700558 err = -EFAULT;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700559 goto out;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800560 }
561 data[count - 1] = 0; /* Make string */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Luiz Capitulino222f1802006-03-20 22:16:13 -0800563 if (!strcmp(data, "stop"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 pktgen_stop_all_threads_ifs();
565
Luiz Capitulino222f1802006-03-20 22:16:13 -0800566 else if (!strcmp(data, "start"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 pktgen_run_all_threads();
568
Luiz Capitulino222f1802006-03-20 22:16:13 -0800569 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 printk("pktgen: Unknown command: %s\n", data);
571
572 err = count;
573
Luiz Capitulino222f1802006-03-20 22:16:13 -0800574out:
575 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576}
577
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700578static int pgctrl_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700580 return single_open(file, pgctrl_show, PDE(inode)->data);
581}
582
Arjan van de Ven9a321442007-02-12 00:55:35 -0800583static const struct file_operations pktgen_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800584 .owner = THIS_MODULE,
585 .open = pgctrl_open,
586 .read = seq_read,
587 .llseek = seq_lseek,
588 .write = pgctrl_write,
589 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700590};
591
592static int pktgen_if_show(struct seq_file *seq, void *v)
593{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 int i;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800595 struct pktgen_dev *pkt_dev = seq->private;
596 __u64 sa;
597 __u64 stopped;
598 __u64 now = getCurUs();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Luiz Capitulino222f1802006-03-20 22:16:13 -0800600 seq_printf(seq,
601 "Params: count %llu min_pkt_size: %u max_pkt_size: %u\n",
602 (unsigned long long)pkt_dev->count, pkt_dev->min_pkt_size,
603 pkt_dev->max_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Luiz Capitulino222f1802006-03-20 22:16:13 -0800605 seq_printf(seq,
606 " frags: %d delay: %u clone_skb: %d ifname: %s\n",
607 pkt_dev->nfrags,
608 1000 * pkt_dev->delay_us + pkt_dev->delay_ns,
609 pkt_dev->clone_skb, pkt_dev->ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Luiz Capitulino222f1802006-03-20 22:16:13 -0800611 seq_printf(seq, " flows: %u flowlen: %u\n", pkt_dev->cflows,
612 pkt_dev->lflow);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Luiz Capitulino222f1802006-03-20 22:16:13 -0800614 if (pkt_dev->flags & F_IPV6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 char b1[128], b2[128], b3[128];
Luiz Capitulino222f1802006-03-20 22:16:13 -0800616 fmt_ip6(b1, pkt_dev->in6_saddr.s6_addr);
617 fmt_ip6(b2, pkt_dev->min_in6_saddr.s6_addr);
618 fmt_ip6(b3, pkt_dev->max_in6_saddr.s6_addr);
619 seq_printf(seq,
620 " saddr: %s min_saddr: %s max_saddr: %s\n", b1,
621 b2, b3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Luiz Capitulino222f1802006-03-20 22:16:13 -0800623 fmt_ip6(b1, pkt_dev->in6_daddr.s6_addr);
624 fmt_ip6(b2, pkt_dev->min_in6_daddr.s6_addr);
625 fmt_ip6(b3, pkt_dev->max_in6_daddr.s6_addr);
626 seq_printf(seq,
627 " daddr: %s min_daddr: %s max_daddr: %s\n", b1,
628 b2, b3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Luiz Capitulino222f1802006-03-20 22:16:13 -0800630 } else
631 seq_printf(seq,
632 " dst_min: %s dst_max: %s\n src_min: %s src_max: %s\n",
633 pkt_dev->dst_min, pkt_dev->dst_max, pkt_dev->src_min,
634 pkt_dev->src_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700636 seq_puts(seq, " src_mac: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Kris Katterjohnf404e9a2006-01-17 13:04:57 -0800638 if (is_zero_ether_addr(pkt_dev->src_mac))
Luiz Capitulino222f1802006-03-20 22:16:13 -0800639 for (i = 0; i < 6; i++)
640 seq_printf(seq, "%02X%s", pkt_dev->odev->dev_addr[i],
641 i == 5 ? " " : ":");
642 else
643 for (i = 0; i < 6; i++)
644 seq_printf(seq, "%02X%s", pkt_dev->src_mac[i],
645 i == 5 ? " " : ":");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Luiz Capitulino222f1802006-03-20 22:16:13 -0800647 seq_printf(seq, "dst_mac: ");
648 for (i = 0; i < 6; i++)
649 seq_printf(seq, "%02X%s", pkt_dev->dst_mac[i],
650 i == 5 ? "\n" : ":");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Luiz Capitulino222f1802006-03-20 22:16:13 -0800652 seq_printf(seq,
653 " udp_src_min: %d udp_src_max: %d udp_dst_min: %d udp_dst_max: %d\n",
654 pkt_dev->udp_src_min, pkt_dev->udp_src_max,
655 pkt_dev->udp_dst_min, pkt_dev->udp_dst_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Luiz Capitulino222f1802006-03-20 22:16:13 -0800657 seq_printf(seq,
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800658 " src_mac_count: %d dst_mac_count: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700659 pkt_dev->src_mac_count, pkt_dev->dst_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800661 if (pkt_dev->nr_labels) {
662 unsigned i;
663 seq_printf(seq, " mpls: ");
664 for(i = 0; i < pkt_dev->nr_labels; i++)
665 seq_printf(seq, "%08x%s", ntohl(pkt_dev->labels[i]),
666 i == pkt_dev->nr_labels-1 ? "\n" : ", ");
667 }
668
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700669 if (pkt_dev->vlan_id != 0xffff) {
670 seq_printf(seq, " vlan_id: %u vlan_p: %u vlan_cfi: %u\n",
671 pkt_dev->vlan_id, pkt_dev->vlan_p, pkt_dev->vlan_cfi);
672 }
673
674 if (pkt_dev->svlan_id != 0xffff) {
675 seq_printf(seq, " svlan_id: %u vlan_p: %u vlan_cfi: %u\n",
676 pkt_dev->svlan_id, pkt_dev->svlan_p, pkt_dev->svlan_cfi);
677 }
678
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700679 if (pkt_dev->tos) {
680 seq_printf(seq, " tos: 0x%02x\n", pkt_dev->tos);
681 }
682
683 if (pkt_dev->traffic_class) {
684 seq_printf(seq, " traffic_class: 0x%02x\n", pkt_dev->traffic_class);
685 }
686
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800687 seq_printf(seq, " Flags: ");
688
Luiz Capitulino222f1802006-03-20 22:16:13 -0800689 if (pkt_dev->flags & F_IPV6)
690 seq_printf(seq, "IPV6 ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Luiz Capitulino222f1802006-03-20 22:16:13 -0800692 if (pkt_dev->flags & F_IPSRC_RND)
693 seq_printf(seq, "IPSRC_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Luiz Capitulino222f1802006-03-20 22:16:13 -0800695 if (pkt_dev->flags & F_IPDST_RND)
696 seq_printf(seq, "IPDST_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Luiz Capitulino222f1802006-03-20 22:16:13 -0800698 if (pkt_dev->flags & F_TXSIZE_RND)
699 seq_printf(seq, "TXSIZE_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Luiz Capitulino222f1802006-03-20 22:16:13 -0800701 if (pkt_dev->flags & F_UDPSRC_RND)
702 seq_printf(seq, "UDPSRC_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Luiz Capitulino222f1802006-03-20 22:16:13 -0800704 if (pkt_dev->flags & F_UDPDST_RND)
705 seq_printf(seq, "UDPDST_RND ");
706
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800707 if (pkt_dev->flags & F_MPLS_RND)
708 seq_printf(seq, "MPLS_RND ");
709
Luiz Capitulino222f1802006-03-20 22:16:13 -0800710 if (pkt_dev->flags & F_MACSRC_RND)
711 seq_printf(seq, "MACSRC_RND ");
712
713 if (pkt_dev->flags & F_MACDST_RND)
714 seq_printf(seq, "MACDST_RND ");
715
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700716 if (pkt_dev->flags & F_VID_RND)
717 seq_printf(seq, "VID_RND ");
718
719 if (pkt_dev->flags & F_SVID_RND)
720 seq_printf(seq, "SVID_RND ");
721
Luiz Capitulino222f1802006-03-20 22:16:13 -0800722 seq_puts(seq, "\n");
723
724 sa = pkt_dev->started_at;
725 stopped = pkt_dev->stopped_at;
726 if (pkt_dev->running)
727 stopped = now; /* not really stopped, more like last-running-at */
728
729 seq_printf(seq,
730 "Current:\n pkts-sofar: %llu errors: %llu\n started: %lluus stopped: %lluus idle: %lluus\n",
731 (unsigned long long)pkt_dev->sofar,
732 (unsigned long long)pkt_dev->errors, (unsigned long long)sa,
733 (unsigned long long)stopped,
734 (unsigned long long)pkt_dev->idle_acc);
735
736 seq_printf(seq,
737 " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700738 pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset,
739 pkt_dev->cur_src_mac_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
Luiz Capitulino222f1802006-03-20 22:16:13 -0800741 if (pkt_dev->flags & F_IPV6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 char b1[128], b2[128];
Luiz Capitulino222f1802006-03-20 22:16:13 -0800743 fmt_ip6(b1, pkt_dev->cur_in6_daddr.s6_addr);
744 fmt_ip6(b2, pkt_dev->cur_in6_saddr.s6_addr);
745 seq_printf(seq, " cur_saddr: %s cur_daddr: %s\n", b2, b1);
746 } else
747 seq_printf(seq, " cur_saddr: 0x%x cur_daddr: 0x%x\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700748 pkt_dev->cur_saddr, pkt_dev->cur_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Luiz Capitulino222f1802006-03-20 22:16:13 -0800750 seq_printf(seq, " cur_udp_dst: %d cur_udp_src: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700751 pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Luiz Capitulino222f1802006-03-20 22:16:13 -0800753 seq_printf(seq, " flows: %u\n", pkt_dev->nflows);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
755 if (pkt_dev->result[0])
Luiz Capitulino222f1802006-03-20 22:16:13 -0800756 seq_printf(seq, "Result: %s\n", pkt_dev->result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 else
Luiz Capitulino222f1802006-03-20 22:16:13 -0800758 seq_printf(seq, "Result: Idle\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700760 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761}
762
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800763
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700764static int hex32_arg(const char __user *user_buffer, unsigned long maxlen, __u32 *num)
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800765{
766 int i = 0;
767 *num = 0;
768
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700769 for(; i < maxlen; i++) {
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800770 char c;
771 *num <<= 4;
772 if (get_user(c, &user_buffer[i]))
773 return -EFAULT;
774 if ((c >= '0') && (c <= '9'))
775 *num |= c - '0';
776 else if ((c >= 'a') && (c <= 'f'))
777 *num |= c - 'a' + 10;
778 else if ((c >= 'A') && (c <= 'F'))
779 *num |= c - 'A' + 10;
780 else
781 break;
782 }
783 return i;
784}
785
Luiz Capitulino222f1802006-03-20 22:16:13 -0800786static int count_trail_chars(const char __user * user_buffer,
787 unsigned int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788{
789 int i;
790
791 for (i = 0; i < maxlen; i++) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800792 char c;
793 if (get_user(c, &user_buffer[i]))
794 return -EFAULT;
795 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 case '\"':
797 case '\n':
798 case '\r':
799 case '\t':
800 case ' ':
801 case '=':
802 break;
803 default:
804 goto done;
805 };
806 }
807done:
808 return i;
809}
810
Luiz Capitulino222f1802006-03-20 22:16:13 -0800811static unsigned long num_arg(const char __user * user_buffer,
812 unsigned long maxlen, unsigned long *num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
814 int i = 0;
815 *num = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800816
817 for (; i < maxlen; i++) {
818 char c;
819 if (get_user(c, &user_buffer[i]))
820 return -EFAULT;
821 if ((c >= '0') && (c <= '9')) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 *num *= 10;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800823 *num += c - '0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 } else
825 break;
826 }
827 return i;
828}
829
Luiz Capitulino222f1802006-03-20 22:16:13 -0800830static int strn_len(const char __user * user_buffer, unsigned int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831{
832 int i = 0;
833
Luiz Capitulino222f1802006-03-20 22:16:13 -0800834 for (; i < maxlen; i++) {
835 char c;
836 if (get_user(c, &user_buffer[i]))
837 return -EFAULT;
838 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 case '\"':
840 case '\n':
841 case '\r':
842 case '\t':
843 case ' ':
844 goto done_str;
845 break;
846 default:
847 break;
848 };
849 }
850done_str:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 return i;
852}
853
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800854static ssize_t get_labels(const char __user *buffer, struct pktgen_dev *pkt_dev)
855{
856 unsigned n = 0;
857 char c;
858 ssize_t i = 0;
859 int len;
860
861 pkt_dev->nr_labels = 0;
862 do {
863 __u32 tmp;
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700864 len = hex32_arg(&buffer[i], 8, &tmp);
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800865 if (len <= 0)
866 return len;
867 pkt_dev->labels[n] = htonl(tmp);
868 if (pkt_dev->labels[n] & MPLS_STACK_BOTTOM)
869 pkt_dev->flags |= F_MPLS_RND;
870 i += len;
871 if (get_user(c, &buffer[i]))
872 return -EFAULT;
873 i++;
874 n++;
875 if (n >= MAX_MPLS_LABELS)
876 return -E2BIG;
877 } while(c == ',');
878
879 pkt_dev->nr_labels = n;
880 return i;
881}
882
Luiz Capitulino222f1802006-03-20 22:16:13 -0800883static ssize_t pktgen_if_write(struct file *file,
884 const char __user * user_buffer, size_t count,
885 loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800887 struct seq_file *seq = (struct seq_file *)file->private_data;
888 struct pktgen_dev *pkt_dev = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 int i = 0, max, len;
890 char name[16], valstr[32];
891 unsigned long value = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800892 char *pg_result = NULL;
893 int tmp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 char buf[128];
Luiz Capitulino222f1802006-03-20 22:16:13 -0800895
896 pg_result = &(pkt_dev->result[0]);
897
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 if (count < 1) {
899 printk("pktgen: wrong command format\n");
900 return -EINVAL;
901 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800902
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 max = count - i;
904 tmp = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800905 if (tmp < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 printk("pktgen: illegal format\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -0800907 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800909 i += tmp;
910
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 /* Read variable name */
912
913 len = strn_len(&user_buffer[i], sizeof(name) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800914 if (len < 0) {
915 return len;
916 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 memset(name, 0, sizeof(name));
Luiz Capitulino222f1802006-03-20 22:16:13 -0800918 if (copy_from_user(name, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 return -EFAULT;
920 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800921
922 max = count - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 len = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800924 if (len < 0)
925 return len;
926
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 i += len;
928
929 if (debug) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800930 char tb[count + 1];
931 if (copy_from_user(tb, user_buffer, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800933 tb[count] = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700934 printk("pktgen: %s,%lu buffer -:%s:-\n", name,
Luiz Capitulino222f1802006-03-20 22:16:13 -0800935 (unsigned long)count, tb);
936 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
938 if (!strcmp(name, "min_pkt_size")) {
939 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800940 if (len < 0) {
941 return len;
942 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800944 if (value < 14 + 20 + 8)
945 value = 14 + 20 + 8;
946 if (value != pkt_dev->min_pkt_size) {
947 pkt_dev->min_pkt_size = value;
948 pkt_dev->cur_pkt_size = value;
949 }
950 sprintf(pg_result, "OK: min_pkt_size=%u",
951 pkt_dev->min_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 return count;
953 }
954
Luiz Capitulino222f1802006-03-20 22:16:13 -0800955 if (!strcmp(name, "max_pkt_size")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800957 if (len < 0) {
958 return len;
959 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800961 if (value < 14 + 20 + 8)
962 value = 14 + 20 + 8;
963 if (value != pkt_dev->max_pkt_size) {
964 pkt_dev->max_pkt_size = value;
965 pkt_dev->cur_pkt_size = value;
966 }
967 sprintf(pg_result, "OK: max_pkt_size=%u",
968 pkt_dev->max_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 return count;
970 }
971
Luiz Capitulino222f1802006-03-20 22:16:13 -0800972 /* Shortcut for min = max */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
974 if (!strcmp(name, "pkt_size")) {
975 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800976 if (len < 0) {
977 return len;
978 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800980 if (value < 14 + 20 + 8)
981 value = 14 + 20 + 8;
982 if (value != pkt_dev->min_pkt_size) {
983 pkt_dev->min_pkt_size = value;
984 pkt_dev->max_pkt_size = value;
985 pkt_dev->cur_pkt_size = value;
986 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 sprintf(pg_result, "OK: pkt_size=%u", pkt_dev->min_pkt_size);
988 return count;
989 }
990
Luiz Capitulino222f1802006-03-20 22:16:13 -0800991 if (!strcmp(name, "debug")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800993 if (len < 0) {
994 return len;
995 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800997 debug = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 sprintf(pg_result, "OK: debug=%u", debug);
999 return count;
1000 }
1001
Luiz Capitulino222f1802006-03-20 22:16:13 -08001002 if (!strcmp(name, "frags")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001004 if (len < 0) {
1005 return len;
1006 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 i += len;
1008 pkt_dev->nfrags = value;
1009 sprintf(pg_result, "OK: frags=%u", pkt_dev->nfrags);
1010 return count;
1011 }
1012 if (!strcmp(name, "delay")) {
1013 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001014 if (len < 0) {
1015 return len;
1016 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 i += len;
1018 if (value == 0x7FFFFFFF) {
1019 pkt_dev->delay_us = 0x7FFFFFFF;
1020 pkt_dev->delay_ns = 0;
1021 } else {
1022 pkt_dev->delay_us = value / 1000;
1023 pkt_dev->delay_ns = value % 1000;
1024 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001025 sprintf(pg_result, "OK: delay=%u",
1026 1000 * pkt_dev->delay_us + pkt_dev->delay_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 return count;
1028 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001029 if (!strcmp(name, "udp_src_min")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001031 if (len < 0) {
1032 return len;
1033 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001035 if (value != pkt_dev->udp_src_min) {
1036 pkt_dev->udp_src_min = value;
1037 pkt_dev->cur_udp_src = value;
1038 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min);
1040 return count;
1041 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001042 if (!strcmp(name, "udp_dst_min")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001044 if (len < 0) {
1045 return len;
1046 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001048 if (value != pkt_dev->udp_dst_min) {
1049 pkt_dev->udp_dst_min = value;
1050 pkt_dev->cur_udp_dst = value;
1051 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min);
1053 return count;
1054 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001055 if (!strcmp(name, "udp_src_max")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001057 if (len < 0) {
1058 return len;
1059 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001061 if (value != pkt_dev->udp_src_max) {
1062 pkt_dev->udp_src_max = value;
1063 pkt_dev->cur_udp_src = value;
1064 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max);
1066 return count;
1067 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001068 if (!strcmp(name, "udp_dst_max")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001070 if (len < 0) {
1071 return len;
1072 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001074 if (value != pkt_dev->udp_dst_max) {
1075 pkt_dev->udp_dst_max = value;
1076 pkt_dev->cur_udp_dst = value;
1077 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max);
1079 return count;
1080 }
1081 if (!strcmp(name, "clone_skb")) {
1082 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001083 if (len < 0) {
1084 return len;
1085 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001087 pkt_dev->clone_skb = value;
1088
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb);
1090 return count;
1091 }
1092 if (!strcmp(name, "count")) {
1093 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001094 if (len < 0) {
1095 return len;
1096 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 i += len;
1098 pkt_dev->count = value;
1099 sprintf(pg_result, "OK: count=%llu",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001100 (unsigned long long)pkt_dev->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 return count;
1102 }
1103 if (!strcmp(name, "src_mac_count")) {
1104 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001105 if (len < 0) {
1106 return len;
1107 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 i += len;
1109 if (pkt_dev->src_mac_count != value) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001110 pkt_dev->src_mac_count = value;
1111 pkt_dev->cur_src_mac_offset = 0;
1112 }
1113 sprintf(pg_result, "OK: src_mac_count=%d",
1114 pkt_dev->src_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 return count;
1116 }
1117 if (!strcmp(name, "dst_mac_count")) {
1118 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001119 if (len < 0) {
1120 return len;
1121 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 i += len;
1123 if (pkt_dev->dst_mac_count != value) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001124 pkt_dev->dst_mac_count = value;
1125 pkt_dev->cur_dst_mac_offset = 0;
1126 }
1127 sprintf(pg_result, "OK: dst_mac_count=%d",
1128 pkt_dev->dst_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 return count;
1130 }
1131 if (!strcmp(name, "flag")) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001132 char f[32];
1133 memset(f, 0, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 len = strn_len(&user_buffer[i], sizeof(f) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001135 if (len < 0) {
1136 return len;
1137 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 if (copy_from_user(f, &user_buffer[i], len))
1139 return -EFAULT;
1140 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001141 if (strcmp(f, "IPSRC_RND") == 0)
1142 pkt_dev->flags |= F_IPSRC_RND;
1143
1144 else if (strcmp(f, "!IPSRC_RND") == 0)
1145 pkt_dev->flags &= ~F_IPSRC_RND;
1146
1147 else if (strcmp(f, "TXSIZE_RND") == 0)
1148 pkt_dev->flags |= F_TXSIZE_RND;
1149
1150 else if (strcmp(f, "!TXSIZE_RND") == 0)
1151 pkt_dev->flags &= ~F_TXSIZE_RND;
1152
1153 else if (strcmp(f, "IPDST_RND") == 0)
1154 pkt_dev->flags |= F_IPDST_RND;
1155
1156 else if (strcmp(f, "!IPDST_RND") == 0)
1157 pkt_dev->flags &= ~F_IPDST_RND;
1158
1159 else if (strcmp(f, "UDPSRC_RND") == 0)
1160 pkt_dev->flags |= F_UDPSRC_RND;
1161
1162 else if (strcmp(f, "!UDPSRC_RND") == 0)
1163 pkt_dev->flags &= ~F_UDPSRC_RND;
1164
1165 else if (strcmp(f, "UDPDST_RND") == 0)
1166 pkt_dev->flags |= F_UDPDST_RND;
1167
1168 else if (strcmp(f, "!UDPDST_RND") == 0)
1169 pkt_dev->flags &= ~F_UDPDST_RND;
1170
1171 else if (strcmp(f, "MACSRC_RND") == 0)
1172 pkt_dev->flags |= F_MACSRC_RND;
1173
1174 else if (strcmp(f, "!MACSRC_RND") == 0)
1175 pkt_dev->flags &= ~F_MACSRC_RND;
1176
1177 else if (strcmp(f, "MACDST_RND") == 0)
1178 pkt_dev->flags |= F_MACDST_RND;
1179
1180 else if (strcmp(f, "!MACDST_RND") == 0)
1181 pkt_dev->flags &= ~F_MACDST_RND;
1182
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001183 else if (strcmp(f, "MPLS_RND") == 0)
1184 pkt_dev->flags |= F_MPLS_RND;
1185
1186 else if (strcmp(f, "!MPLS_RND") == 0)
1187 pkt_dev->flags &= ~F_MPLS_RND;
1188
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001189 else if (strcmp(f, "VID_RND") == 0)
1190 pkt_dev->flags |= F_VID_RND;
1191
1192 else if (strcmp(f, "!VID_RND") == 0)
1193 pkt_dev->flags &= ~F_VID_RND;
1194
1195 else if (strcmp(f, "SVID_RND") == 0)
1196 pkt_dev->flags |= F_SVID_RND;
1197
1198 else if (strcmp(f, "!SVID_RND") == 0)
1199 pkt_dev->flags &= ~F_SVID_RND;
1200
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001201 else if (strcmp(f, "!IPV6") == 0)
1202 pkt_dev->flags &= ~F_IPV6;
1203
Luiz Capitulino222f1802006-03-20 22:16:13 -08001204 else {
1205 sprintf(pg_result,
1206 "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
1207 f,
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001208 "IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
1209 "MACSRC_RND, MACDST_RND, TXSIZE_RND, IPV6, MPLS_RND, VID_RND, SVID_RND\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08001210 return count;
1211 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
1213 return count;
1214 }
1215 if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
1216 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001217 if (len < 0) {
1218 return len;
1219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
Luiz Capitulino222f1802006-03-20 22:16:13 -08001221 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001223 buf[len] = 0;
1224 if (strcmp(buf, pkt_dev->dst_min) != 0) {
1225 memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
1226 strncpy(pkt_dev->dst_min, buf, len);
1227 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
1228 pkt_dev->cur_daddr = pkt_dev->daddr_min;
1229 }
1230 if (debug)
1231 printk("pktgen: dst_min set to: %s\n",
1232 pkt_dev->dst_min);
1233 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min);
1235 return count;
1236 }
1237 if (!strcmp(name, "dst_max")) {
1238 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001239 if (len < 0) {
1240 return len;
1241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
Luiz Capitulino222f1802006-03-20 22:16:13 -08001243 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 return -EFAULT;
1245
Luiz Capitulino222f1802006-03-20 22:16:13 -08001246 buf[len] = 0;
1247 if (strcmp(buf, pkt_dev->dst_max) != 0) {
1248 memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
1249 strncpy(pkt_dev->dst_max, buf, len);
1250 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
1251 pkt_dev->cur_daddr = pkt_dev->daddr_max;
1252 }
1253 if (debug)
1254 printk("pktgen: dst_max set to: %s\n",
1255 pkt_dev->dst_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 i += len;
1257 sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max);
1258 return count;
1259 }
1260 if (!strcmp(name, "dst6")) {
1261 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001262 if (len < 0)
1263 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
1265 pkt_dev->flags |= F_IPV6;
1266
Luiz Capitulino222f1802006-03-20 22:16:13 -08001267 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001269 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
1271 scan_ip6(buf, pkt_dev->in6_daddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001272 fmt_ip6(buf, pkt_dev->in6_daddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
1274 ipv6_addr_copy(&pkt_dev->cur_in6_daddr, &pkt_dev->in6_daddr);
1275
Luiz Capitulino222f1802006-03-20 22:16:13 -08001276 if (debug)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 printk("pktgen: dst6 set to: %s\n", buf);
1278
Luiz Capitulino222f1802006-03-20 22:16:13 -08001279 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 sprintf(pg_result, "OK: dst6=%s", buf);
1281 return count;
1282 }
1283 if (!strcmp(name, "dst6_min")) {
1284 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001285 if (len < 0)
1286 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
1288 pkt_dev->flags |= F_IPV6;
1289
Luiz Capitulino222f1802006-03-20 22:16:13 -08001290 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001292 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
1294 scan_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001295 fmt_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
Luiz Capitulino222f1802006-03-20 22:16:13 -08001297 ipv6_addr_copy(&pkt_dev->cur_in6_daddr,
1298 &pkt_dev->min_in6_daddr);
1299 if (debug)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 printk("pktgen: dst6_min set to: %s\n", buf);
1301
Luiz Capitulino222f1802006-03-20 22:16:13 -08001302 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 sprintf(pg_result, "OK: dst6_min=%s", buf);
1304 return count;
1305 }
1306 if (!strcmp(name, "dst6_max")) {
1307 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001308 if (len < 0)
1309 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
1311 pkt_dev->flags |= F_IPV6;
1312
Luiz Capitulino222f1802006-03-20 22:16:13 -08001313 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001315 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
1317 scan_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001318 fmt_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
Luiz Capitulino222f1802006-03-20 22:16:13 -08001320 if (debug)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 printk("pktgen: dst6_max set to: %s\n", buf);
1322
Luiz Capitulino222f1802006-03-20 22:16:13 -08001323 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 sprintf(pg_result, "OK: dst6_max=%s", buf);
1325 return count;
1326 }
1327 if (!strcmp(name, "src6")) {
1328 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001329 if (len < 0)
1330 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
1332 pkt_dev->flags |= F_IPV6;
1333
Luiz Capitulino222f1802006-03-20 22:16:13 -08001334 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001336 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
1338 scan_ip6(buf, pkt_dev->in6_saddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001339 fmt_ip6(buf, pkt_dev->in6_saddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
1341 ipv6_addr_copy(&pkt_dev->cur_in6_saddr, &pkt_dev->in6_saddr);
1342
Luiz Capitulino222f1802006-03-20 22:16:13 -08001343 if (debug)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 printk("pktgen: src6 set to: %s\n", buf);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001345
1346 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 sprintf(pg_result, "OK: src6=%s", buf);
1348 return count;
1349 }
1350 if (!strcmp(name, "src_min")) {
1351 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001352 if (len < 0) {
1353 return len;
1354 }
1355 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001357 buf[len] = 0;
1358 if (strcmp(buf, pkt_dev->src_min) != 0) {
1359 memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
1360 strncpy(pkt_dev->src_min, buf, len);
1361 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
1362 pkt_dev->cur_saddr = pkt_dev->saddr_min;
1363 }
1364 if (debug)
1365 printk("pktgen: src_min set to: %s\n",
1366 pkt_dev->src_min);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 i += len;
1368 sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min);
1369 return count;
1370 }
1371 if (!strcmp(name, "src_max")) {
1372 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001373 if (len < 0) {
1374 return len;
1375 }
1376 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001378 buf[len] = 0;
1379 if (strcmp(buf, pkt_dev->src_max) != 0) {
1380 memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
1381 strncpy(pkt_dev->src_max, buf, len);
1382 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
1383 pkt_dev->cur_saddr = pkt_dev->saddr_max;
1384 }
1385 if (debug)
1386 printk("pktgen: src_max set to: %s\n",
1387 pkt_dev->src_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 i += len;
1389 sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max);
1390 return count;
1391 }
1392 if (!strcmp(name, "dst_mac")) {
1393 char *v = valstr;
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001394 unsigned char old_dmac[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 unsigned char *m = pkt_dev->dst_mac;
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001396 memcpy(old_dmac, pkt_dev->dst_mac, ETH_ALEN);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001397
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001399 if (len < 0) {
1400 return len;
1401 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 memset(valstr, 0, sizeof(valstr));
Luiz Capitulino222f1802006-03-20 22:16:13 -08001403 if (copy_from_user(valstr, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 return -EFAULT;
1405 i += len;
1406
Luiz Capitulino222f1802006-03-20 22:16:13 -08001407 for (*m = 0; *v && m < pkt_dev->dst_mac + 6; v++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 if (*v >= '0' && *v <= '9') {
1409 *m *= 16;
1410 *m += *v - '0';
1411 }
1412 if (*v >= 'A' && *v <= 'F') {
1413 *m *= 16;
1414 *m += *v - 'A' + 10;
1415 }
1416 if (*v >= 'a' && *v <= 'f') {
1417 *m *= 16;
1418 *m += *v - 'a' + 10;
1419 }
1420 if (*v == ':') {
1421 m++;
1422 *m = 0;
1423 }
1424 }
1425
1426 /* Set up Dest MAC */
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001427 if (compare_ether_addr(old_dmac, pkt_dev->dst_mac))
1428 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001429
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 sprintf(pg_result, "OK: dstmac");
1431 return count;
1432 }
1433 if (!strcmp(name, "src_mac")) {
1434 char *v = valstr;
1435 unsigned char *m = pkt_dev->src_mac;
1436
1437 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001438 if (len < 0) {
1439 return len;
1440 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 memset(valstr, 0, sizeof(valstr));
Luiz Capitulino222f1802006-03-20 22:16:13 -08001442 if (copy_from_user(valstr, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 return -EFAULT;
1444 i += len;
1445
Luiz Capitulino222f1802006-03-20 22:16:13 -08001446 for (*m = 0; *v && m < pkt_dev->src_mac + 6; v++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 if (*v >= '0' && *v <= '9') {
1448 *m *= 16;
1449 *m += *v - '0';
1450 }
1451 if (*v >= 'A' && *v <= 'F') {
1452 *m *= 16;
1453 *m += *v - 'A' + 10;
1454 }
1455 if (*v >= 'a' && *v <= 'f') {
1456 *m *= 16;
1457 *m += *v - 'a' + 10;
1458 }
1459 if (*v == ':') {
1460 m++;
1461 *m = 0;
1462 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
Luiz Capitulino222f1802006-03-20 22:16:13 -08001465 sprintf(pg_result, "OK: srcmac");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 return count;
1467 }
1468
Luiz Capitulino222f1802006-03-20 22:16:13 -08001469 if (!strcmp(name, "clear_counters")) {
1470 pktgen_clear_counters(pkt_dev);
1471 sprintf(pg_result, "OK: Clearing counters.\n");
1472 return count;
1473 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
1475 if (!strcmp(name, "flows")) {
1476 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001477 if (len < 0) {
1478 return len;
1479 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 i += len;
1481 if (value > MAX_CFLOWS)
1482 value = MAX_CFLOWS;
1483
1484 pkt_dev->cflows = value;
1485 sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
1486 return count;
1487 }
1488
1489 if (!strcmp(name, "flowlen")) {
1490 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001491 if (len < 0) {
1492 return len;
1493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 i += len;
1495 pkt_dev->lflow = value;
1496 sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
1497 return count;
1498 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001499
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001500 if (!strcmp(name, "mpls")) {
1501 unsigned n, offset;
1502 len = get_labels(&user_buffer[i], pkt_dev);
1503 if (len < 0) { return len; }
1504 i += len;
1505 offset = sprintf(pg_result, "OK: mpls=");
1506 for(n = 0; n < pkt_dev->nr_labels; n++)
1507 offset += sprintf(pg_result + offset,
1508 "%08x%s", ntohl(pkt_dev->labels[n]),
1509 n == pkt_dev->nr_labels-1 ? "" : ",");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001510
1511 if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) {
1512 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1513 pkt_dev->svlan_id = 0xffff;
1514
1515 if (debug)
1516 printk("pktgen: VLAN/SVLAN auto turned off\n");
1517 }
1518 return count;
1519 }
1520
1521 if (!strcmp(name, "vlan_id")) {
1522 len = num_arg(&user_buffer[i], 4, &value);
1523 if (len < 0) {
1524 return len;
1525 }
1526 i += len;
1527 if (value <= 4095) {
1528 pkt_dev->vlan_id = value; /* turn on VLAN */
1529
1530 if (debug)
1531 printk("pktgen: VLAN turned on\n");
1532
1533 if (debug && pkt_dev->nr_labels)
1534 printk("pktgen: MPLS auto turned off\n");
1535
1536 pkt_dev->nr_labels = 0; /* turn off MPLS */
1537 sprintf(pg_result, "OK: vlan_id=%u", pkt_dev->vlan_id);
1538 } else {
1539 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1540 pkt_dev->svlan_id = 0xffff;
1541
1542 if (debug)
1543 printk("pktgen: VLAN/SVLAN turned off\n");
1544 }
1545 return count;
1546 }
1547
1548 if (!strcmp(name, "vlan_p")) {
1549 len = num_arg(&user_buffer[i], 1, &value);
1550 if (len < 0) {
1551 return len;
1552 }
1553 i += len;
1554 if ((value <= 7) && (pkt_dev->vlan_id != 0xffff)) {
1555 pkt_dev->vlan_p = value;
1556 sprintf(pg_result, "OK: vlan_p=%u", pkt_dev->vlan_p);
1557 } else {
1558 sprintf(pg_result, "ERROR: vlan_p must be 0-7");
1559 }
1560 return count;
1561 }
1562
1563 if (!strcmp(name, "vlan_cfi")) {
1564 len = num_arg(&user_buffer[i], 1, &value);
1565 if (len < 0) {
1566 return len;
1567 }
1568 i += len;
1569 if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) {
1570 pkt_dev->vlan_cfi = value;
1571 sprintf(pg_result, "OK: vlan_cfi=%u", pkt_dev->vlan_cfi);
1572 } else {
1573 sprintf(pg_result, "ERROR: vlan_cfi must be 0-1");
1574 }
1575 return count;
1576 }
1577
1578 if (!strcmp(name, "svlan_id")) {
1579 len = num_arg(&user_buffer[i], 4, &value);
1580 if (len < 0) {
1581 return len;
1582 }
1583 i += len;
1584 if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) {
1585 pkt_dev->svlan_id = value; /* turn on SVLAN */
1586
1587 if (debug)
1588 printk("pktgen: SVLAN turned on\n");
1589
1590 if (debug && pkt_dev->nr_labels)
1591 printk("pktgen: MPLS auto turned off\n");
1592
1593 pkt_dev->nr_labels = 0; /* turn off MPLS */
1594 sprintf(pg_result, "OK: svlan_id=%u", pkt_dev->svlan_id);
1595 } else {
1596 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1597 pkt_dev->svlan_id = 0xffff;
1598
1599 if (debug)
1600 printk("pktgen: VLAN/SVLAN turned off\n");
1601 }
1602 return count;
1603 }
1604
1605 if (!strcmp(name, "svlan_p")) {
1606 len = num_arg(&user_buffer[i], 1, &value);
1607 if (len < 0) {
1608 return len;
1609 }
1610 i += len;
1611 if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) {
1612 pkt_dev->svlan_p = value;
1613 sprintf(pg_result, "OK: svlan_p=%u", pkt_dev->svlan_p);
1614 } else {
1615 sprintf(pg_result, "ERROR: svlan_p must be 0-7");
1616 }
1617 return count;
1618 }
1619
1620 if (!strcmp(name, "svlan_cfi")) {
1621 len = num_arg(&user_buffer[i], 1, &value);
1622 if (len < 0) {
1623 return len;
1624 }
1625 i += len;
1626 if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) {
1627 pkt_dev->svlan_cfi = value;
1628 sprintf(pg_result, "OK: svlan_cfi=%u", pkt_dev->svlan_cfi);
1629 } else {
1630 sprintf(pg_result, "ERROR: svlan_cfi must be 0-1");
1631 }
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001632 return count;
1633 }
1634
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001635 if (!strcmp(name, "tos")) {
1636 __u32 tmp_value = 0;
1637 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1638 if (len < 0) {
1639 return len;
1640 }
1641 i += len;
1642 if (len == 2) {
1643 pkt_dev->tos = tmp_value;
1644 sprintf(pg_result, "OK: tos=0x%02x", pkt_dev->tos);
1645 } else {
1646 sprintf(pg_result, "ERROR: tos must be 00-ff");
1647 }
1648 return count;
1649 }
1650
1651 if (!strcmp(name, "traffic_class")) {
1652 __u32 tmp_value = 0;
1653 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1654 if (len < 0) {
1655 return len;
1656 }
1657 i += len;
1658 if (len == 2) {
1659 pkt_dev->traffic_class = tmp_value;
1660 sprintf(pg_result, "OK: traffic_class=0x%02x", pkt_dev->traffic_class);
1661 } else {
1662 sprintf(pg_result, "ERROR: traffic_class must be 00-ff");
1663 }
1664 return count;
1665 }
1666
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
1668 return -EINVAL;
1669}
1670
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001671static int pktgen_if_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672{
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001673 return single_open(file, pktgen_if_show, PDE(inode)->data);
1674}
1675
Arjan van de Ven9a321442007-02-12 00:55:35 -08001676static const struct file_operations pktgen_if_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001677 .owner = THIS_MODULE,
1678 .open = pktgen_if_open,
1679 .read = seq_read,
1680 .llseek = seq_lseek,
1681 .write = pktgen_if_write,
1682 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001683};
1684
1685static int pktgen_thread_show(struct seq_file *seq, void *v)
1686{
Luiz Capitulino222f1802006-03-20 22:16:13 -08001687 struct pktgen_thread *t = seq->private;
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08001688 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001690 BUG_ON(!t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001692 seq_printf(seq, "Name: %s max_before_softirq: %d\n",
David S. Milleree74baa2007-01-01 20:51:53 -08001693 t->tsk->comm, t->max_before_softirq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
Luiz Capitulino222f1802006-03-20 22:16:13 -08001695 seq_printf(seq, "Running: ");
1696
1697 if_lock(t);
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08001698 list_for_each_entry(pkt_dev, &t->if_list, list)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001699 if (pkt_dev->running)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001700 seq_printf(seq, "%s ", pkt_dev->ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
Luiz Capitulino222f1802006-03-20 22:16:13 -08001702 seq_printf(seq, "\nStopped: ");
1703
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08001704 list_for_each_entry(pkt_dev, &t->if_list, list)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001705 if (!pkt_dev->running)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001706 seq_printf(seq, "%s ", pkt_dev->ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707
1708 if (t->result[0])
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001709 seq_printf(seq, "\nResult: %s\n", t->result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 else
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001711 seq_printf(seq, "\nResult: NA\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
Luiz Capitulino222f1802006-03-20 22:16:13 -08001713 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001715 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716}
1717
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001718static ssize_t pktgen_thread_write(struct file *file,
Luiz Capitulino222f1802006-03-20 22:16:13 -08001719 const char __user * user_buffer,
1720 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721{
Luiz Capitulino222f1802006-03-20 22:16:13 -08001722 struct seq_file *seq = (struct seq_file *)file->private_data;
1723 struct pktgen_thread *t = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 int i = 0, max, len, ret;
1725 char name[40];
Luiz Capitulino222f1802006-03-20 22:16:13 -08001726 char *pg_result;
1727 unsigned long value = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001728
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 if (count < 1) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001730 // sprintf(pg_result, "Wrong command format");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 return -EINVAL;
1732 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001733
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 max = count - i;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001735 len = count_trail_chars(&user_buffer[i], max);
1736 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001737 return len;
1738
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 i += len;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001740
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 /* Read variable name */
1742
1743 len = strn_len(&user_buffer[i], sizeof(name) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001744 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001745 return len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001746
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 memset(name, 0, sizeof(name));
1748 if (copy_from_user(name, &user_buffer[i], len))
1749 return -EFAULT;
1750 i += len;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001751
Luiz Capitulino222f1802006-03-20 22:16:13 -08001752 max = count - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 len = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001754 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001755 return len;
1756
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 i += len;
1758
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001759 if (debug)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001760 printk("pktgen: t=%s, count=%lu\n", name, (unsigned long)count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
Luiz Capitulino222f1802006-03-20 22:16:13 -08001762 if (!t) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 printk("pktgen: ERROR: No thread\n");
1764 ret = -EINVAL;
1765 goto out;
1766 }
1767
1768 pg_result = &(t->result[0]);
1769
Luiz Capitulino222f1802006-03-20 22:16:13 -08001770 if (!strcmp(name, "add_device")) {
1771 char f[32];
1772 memset(f, 0, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 len = strn_len(&user_buffer[i], sizeof(f) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001774 if (len < 0) {
1775 ret = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 goto out;
1777 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001778 if (copy_from_user(f, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 return -EFAULT;
1780 i += len;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001781 mutex_lock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001782 pktgen_add_device(t, f);
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001783 mutex_unlock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001784 ret = count;
1785 sprintf(pg_result, "OK: add_device=%s", f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 goto out;
1787 }
1788
Luiz Capitulino222f1802006-03-20 22:16:13 -08001789 if (!strcmp(name, "rem_device_all")) {
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001790 mutex_lock(&pktgen_thread_lock);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001791 t->control |= T_REMDEVALL;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001792 mutex_unlock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001793 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 ret = count;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001795 sprintf(pg_result, "OK: rem_device_all");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 goto out;
1797 }
1798
Luiz Capitulino222f1802006-03-20 22:16:13 -08001799 if (!strcmp(name, "max_before_softirq")) {
1800 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001801 mutex_lock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001802 t->max_before_softirq = value;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001803 mutex_unlock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001804 ret = count;
1805 sprintf(pg_result, "OK: max_before_softirq=%lu", value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 goto out;
1807 }
1808
1809 ret = -EINVAL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001810out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 return ret;
1812}
1813
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001814static int pktgen_thread_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815{
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001816 return single_open(file, pktgen_thread_show, PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817}
1818
Arjan van de Ven9a321442007-02-12 00:55:35 -08001819static const struct file_operations pktgen_thread_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001820 .owner = THIS_MODULE,
1821 .open = pktgen_thread_open,
1822 .read = seq_read,
1823 .llseek = seq_lseek,
1824 .write = pktgen_thread_write,
1825 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001826};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827
1828/* Think find or remove for NN */
Luiz Capitulino222f1802006-03-20 22:16:13 -08001829static struct pktgen_dev *__pktgen_NN_threads(const char *ifname, int remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830{
1831 struct pktgen_thread *t;
1832 struct pktgen_dev *pkt_dev = NULL;
1833
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08001834 list_for_each_entry(t, &pktgen_threads, th_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 pkt_dev = pktgen_find_dev(t, ifname);
1836 if (pkt_dev) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001837 if (remove) {
1838 if_lock(t);
1839 pkt_dev->removal_mark = 1;
1840 t->control |= T_REMDEV;
1841 if_unlock(t);
1842 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 break;
1844 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001846 return pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847}
1848
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001849/*
1850 * mark a device for removal
1851 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08001852static int pktgen_mark_device(const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853{
1854 struct pktgen_dev *pkt_dev = NULL;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001855 const int max_tries = 10, msec_per_try = 125;
1856 int i = 0;
1857 int ret = 0;
1858
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001859 mutex_lock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001860 PG_DEBUG(printk("pktgen: pktgen_mark_device marking %s for removal\n",
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001861 ifname));
1862
Luiz Capitulino222f1802006-03-20 22:16:13 -08001863 while (1) {
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001864
1865 pkt_dev = __pktgen_NN_threads(ifname, REMOVE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001866 if (pkt_dev == NULL)
1867 break; /* success */
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001868
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001869 mutex_unlock(&pktgen_thread_lock);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001870 PG_DEBUG(printk("pktgen: pktgen_mark_device waiting for %s "
Luiz Capitulino222f1802006-03-20 22:16:13 -08001871 "to disappear....\n", ifname));
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001872 schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001873 mutex_lock(&pktgen_thread_lock);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001874
1875 if (++i >= max_tries) {
1876 printk("pktgen_mark_device: timed out after waiting "
Luiz Capitulino222f1802006-03-20 22:16:13 -08001877 "%d msec for device %s to be removed\n",
1878 msec_per_try * i, ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001879 ret = 1;
1880 break;
1881 }
1882
1883 }
1884
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001885 mutex_unlock(&pktgen_thread_lock);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001886
1887 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888}
1889
Luiz Capitulino222f1802006-03-20 22:16:13 -08001890static int pktgen_device_event(struct notifier_block *unused,
1891 unsigned long event, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892{
1893 struct net_device *dev = (struct net_device *)(ptr);
1894
1895 /* It is OK that we do not hold the group lock right now,
1896 * as we run under the RTNL lock.
1897 */
1898
1899 switch (event) {
1900 case NETDEV_CHANGEADDR:
1901 case NETDEV_GOING_DOWN:
1902 case NETDEV_DOWN:
1903 case NETDEV_UP:
1904 /* Ignore for now */
1905 break;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001906
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 case NETDEV_UNREGISTER:
Luiz Capitulino222f1802006-03-20 22:16:13 -08001908 pktgen_mark_device(dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 break;
1910 };
1911
1912 return NOTIFY_DONE;
1913}
1914
1915/* Associate pktgen_dev with a device. */
1916
Luiz Capitulino222f1802006-03-20 22:16:13 -08001917static struct net_device *pktgen_setup_dev(struct pktgen_dev *pkt_dev)
1918{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 struct net_device *odev;
1920
1921 /* Clean old setups */
1922
1923 if (pkt_dev->odev) {
1924 dev_put(pkt_dev->odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001925 pkt_dev->odev = NULL;
1926 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927
1928 odev = dev_get_by_name(pkt_dev->ifname);
1929
1930 if (!odev) {
1931 printk("pktgen: no such netdevice: \"%s\"\n", pkt_dev->ifname);
1932 goto out;
1933 }
1934 if (odev->type != ARPHRD_ETHER) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001935 printk("pktgen: not an ethernet device: \"%s\"\n",
1936 pkt_dev->ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 goto out_put;
1938 }
1939 if (!netif_running(odev)) {
1940 printk("pktgen: device is down: \"%s\"\n", pkt_dev->ifname);
1941 goto out_put;
1942 }
1943 pkt_dev->odev = odev;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001944
1945 return pkt_dev->odev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
1947out_put:
1948 dev_put(odev);
1949out:
Luiz Capitulino222f1802006-03-20 22:16:13 -08001950 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951
1952}
1953
1954/* Read pkt_dev from the interface and set up internal pktgen_dev
1955 * structure to have the right information to create/send packets
1956 */
1957static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
1958{
1959 /* Try once more, just in case it works now. */
Luiz Capitulino222f1802006-03-20 22:16:13 -08001960 if (!pkt_dev->odev)
1961 pktgen_setup_dev(pkt_dev);
1962
1963 if (!pkt_dev->odev) {
1964 printk("pktgen: ERROR: pkt_dev->odev == NULL in setup_inject.\n");
1965 sprintf(pkt_dev->result,
1966 "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
1967 return;
1968 }
1969
1970 /* Default to the interface's mac if not explicitly set. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001972 if (is_zero_ether_addr(pkt_dev->src_mac))
Luiz Capitulino222f1802006-03-20 22:16:13 -08001973 memcpy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
Luiz Capitulino222f1802006-03-20 22:16:13 -08001975 /* Set up Dest MAC */
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001976 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977
Luiz Capitulino222f1802006-03-20 22:16:13 -08001978 /* Set up pkt size */
1979 pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
1980
1981 if (pkt_dev->flags & F_IPV6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 /*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001983 * Skip this automatic address setting until locks or functions
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 * gets exported
1985 */
1986
1987#ifdef NOTNOW
Luiz Capitulino222f1802006-03-20 22:16:13 -08001988 int i, set = 0, err = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 struct inet6_dev *idev;
1990
Luiz Capitulino222f1802006-03-20 22:16:13 -08001991 for (i = 0; i < IN6_ADDR_HSIZE; i++)
1992 if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 set = 1;
1994 break;
1995 }
1996
Luiz Capitulino222f1802006-03-20 22:16:13 -08001997 if (!set) {
1998
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 /*
2000 * Use linklevel address if unconfigured.
2001 *
2002 * use ipv6_get_lladdr if/when it's get exported
2003 */
2004
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002005 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 if ((idev = __in6_dev_get(pkt_dev->odev)) != NULL) {
2007 struct inet6_ifaddr *ifp;
2008
2009 read_lock_bh(&idev->lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002010 for (ifp = idev->addr_list; ifp;
2011 ifp = ifp->if_next) {
2012 if (ifp->scope == IFA_LINK
2013 && !(ifp->
2014 flags & IFA_F_TENTATIVE)) {
2015 ipv6_addr_copy(&pkt_dev->
2016 cur_in6_saddr,
2017 &ifp->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 err = 0;
2019 break;
2020 }
2021 }
2022 read_unlock_bh(&idev->lock);
2023 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002024 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002025 if (err)
2026 printk("pktgen: ERROR: IPv6 link address not availble.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 }
2028#endif
Luiz Capitulino222f1802006-03-20 22:16:13 -08002029 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 pkt_dev->saddr_min = 0;
2031 pkt_dev->saddr_max = 0;
2032 if (strlen(pkt_dev->src_min) == 0) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002033
2034 struct in_device *in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035
2036 rcu_read_lock();
Herbert Xue5ed6392005-10-03 14:35:55 -07002037 in_dev = __in_dev_get_rcu(pkt_dev->odev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 if (in_dev) {
2039 if (in_dev->ifa_list) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002040 pkt_dev->saddr_min =
2041 in_dev->ifa_list->ifa_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 pkt_dev->saddr_max = pkt_dev->saddr_min;
2043 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 }
2045 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002046 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
2048 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
2049 }
2050
2051 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
2052 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
2053 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002054 /* Initialize current values. */
2055 pkt_dev->cur_dst_mac_offset = 0;
2056 pkt_dev->cur_src_mac_offset = 0;
2057 pkt_dev->cur_saddr = pkt_dev->saddr_min;
2058 pkt_dev->cur_daddr = pkt_dev->daddr_min;
2059 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2060 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 pkt_dev->nflows = 0;
2062}
2063
2064static void spin(struct pktgen_dev *pkt_dev, __u64 spin_until_us)
2065{
2066 __u64 start;
2067 __u64 now;
2068
2069 start = now = getCurUs();
2070 printk(KERN_INFO "sleeping for %d\n", (int)(spin_until_us - now));
2071 while (now < spin_until_us) {
Stephen Hemmingerb4099fa2005-10-14 15:32:22 -07002072 /* TODO: optimize sleeping behavior */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002073 if (spin_until_us - now > jiffies_to_usecs(1) + 1)
Nishanth Aravamudan121caf52005-09-12 14:15:34 -07002074 schedule_timeout_interruptible(1);
2075 else if (spin_until_us - now > 100) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 do_softirq();
2077 if (!pkt_dev->running)
2078 return;
2079 if (need_resched())
2080 schedule();
2081 }
2082
2083 now = getCurUs();
2084 }
2085
2086 pkt_dev->idle_acc += now - start;
2087}
2088
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089/* Increment/randomize headers according to flags and current values
2090 * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
2091 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002092static void mod_cur_headers(struct pktgen_dev *pkt_dev)
2093{
2094 __u32 imn;
2095 __u32 imx;
2096 int flow = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097
Luiz Capitulino222f1802006-03-20 22:16:13 -08002098 if (pkt_dev->cflows) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 flow = pktgen_random() % pkt_dev->cflows;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002100
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 if (pkt_dev->flows[flow].count > pkt_dev->lflow)
2102 pkt_dev->flows[flow].count = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002103 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
2105 /* Deal with source MAC */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002106 if (pkt_dev->src_mac_count > 1) {
2107 __u32 mc;
2108 __u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109
Luiz Capitulino222f1802006-03-20 22:16:13 -08002110 if (pkt_dev->flags & F_MACSRC_RND)
2111 mc = pktgen_random() % (pkt_dev->src_mac_count);
2112 else {
2113 mc = pkt_dev->cur_src_mac_offset++;
2114 if (pkt_dev->cur_src_mac_offset >
2115 pkt_dev->src_mac_count)
2116 pkt_dev->cur_src_mac_offset = 0;
2117 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118
Luiz Capitulino222f1802006-03-20 22:16:13 -08002119 tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
2120 pkt_dev->hh[11] = tmp;
2121 tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2122 pkt_dev->hh[10] = tmp;
2123 tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2124 pkt_dev->hh[9] = tmp;
2125 tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2126 pkt_dev->hh[8] = tmp;
2127 tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
2128 pkt_dev->hh[7] = tmp;
2129 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130
Luiz Capitulino222f1802006-03-20 22:16:13 -08002131 /* Deal with Destination MAC */
2132 if (pkt_dev->dst_mac_count > 1) {
2133 __u32 mc;
2134 __u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135
Luiz Capitulino222f1802006-03-20 22:16:13 -08002136 if (pkt_dev->flags & F_MACDST_RND)
2137 mc = pktgen_random() % (pkt_dev->dst_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138
Luiz Capitulino222f1802006-03-20 22:16:13 -08002139 else {
2140 mc = pkt_dev->cur_dst_mac_offset++;
2141 if (pkt_dev->cur_dst_mac_offset >
2142 pkt_dev->dst_mac_count) {
2143 pkt_dev->cur_dst_mac_offset = 0;
2144 }
2145 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146
Luiz Capitulino222f1802006-03-20 22:16:13 -08002147 tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
2148 pkt_dev->hh[5] = tmp;
2149 tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2150 pkt_dev->hh[4] = tmp;
2151 tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2152 pkt_dev->hh[3] = tmp;
2153 tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2154 pkt_dev->hh[2] = tmp;
2155 tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
2156 pkt_dev->hh[1] = tmp;
2157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002159 if (pkt_dev->flags & F_MPLS_RND) {
2160 unsigned i;
2161 for(i = 0; i < pkt_dev->nr_labels; i++)
2162 if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
2163 pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
Al Viro252e3342006-11-14 20:48:11 -08002164 ((__force __be32)pktgen_random() &
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002165 htonl(0x000fffff));
2166 }
2167
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002168 if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
2169 pkt_dev->vlan_id = pktgen_random() % 4096;
2170 }
2171
2172 if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
2173 pkt_dev->svlan_id = pktgen_random() % 4096;
2174 }
2175
Luiz Capitulino222f1802006-03-20 22:16:13 -08002176 if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
2177 if (pkt_dev->flags & F_UDPSRC_RND)
2178 pkt_dev->cur_udp_src =
2179 ((pktgen_random() %
2180 (pkt_dev->udp_src_max - pkt_dev->udp_src_min)) +
2181 pkt_dev->udp_src_min);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182
Luiz Capitulino222f1802006-03-20 22:16:13 -08002183 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 pkt_dev->cur_udp_src++;
2185 if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
2186 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002187 }
2188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189
Luiz Capitulino222f1802006-03-20 22:16:13 -08002190 if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
2191 if (pkt_dev->flags & F_UDPDST_RND) {
2192 pkt_dev->cur_udp_dst =
2193 ((pktgen_random() %
2194 (pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)) +
2195 pkt_dev->udp_dst_min);
2196 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 pkt_dev->cur_udp_dst++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002198 if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002200 }
2201 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202
2203 if (!(pkt_dev->flags & F_IPV6)) {
2204
Luiz Capitulino222f1802006-03-20 22:16:13 -08002205 if ((imn = ntohl(pkt_dev->saddr_min)) < (imx =
2206 ntohl(pkt_dev->
2207 saddr_max))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 __u32 t;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002209 if (pkt_dev->flags & F_IPSRC_RND)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 t = ((pktgen_random() % (imx - imn)) + imn);
2211 else {
2212 t = ntohl(pkt_dev->cur_saddr);
2213 t++;
2214 if (t > imx) {
2215 t = imn;
2216 }
2217 }
2218 pkt_dev->cur_saddr = htonl(t);
2219 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002220
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 if (pkt_dev->cflows && pkt_dev->flows[flow].count != 0) {
2222 pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
2223 } else {
Al Viro252e3342006-11-14 20:48:11 -08002224 imn = ntohl(pkt_dev->daddr_min);
2225 imx = ntohl(pkt_dev->daddr_max);
2226 if (imn < imx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 __u32 t;
Al Viro252e3342006-11-14 20:48:11 -08002228 __be32 s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 if (pkt_dev->flags & F_IPDST_RND) {
2230
Al Viro252e3342006-11-14 20:48:11 -08002231 t = pktgen_random() % (imx - imn) + imn;
2232 s = htonl(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233
Al Viro252e3342006-11-14 20:48:11 -08002234 while (LOOPBACK(s) || MULTICAST(s)
2235 || BADCLASS(s) || ZERONET(s)
2236 || LOCAL_MCAST(s)) {
2237 t = (pktgen_random() %
2238 (imx - imn)) + imn;
2239 s = htonl(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 }
Al Viro252e3342006-11-14 20:48:11 -08002241 pkt_dev->cur_daddr = s;
2242 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 t = ntohl(pkt_dev->cur_daddr);
2244 t++;
2245 if (t > imx) {
2246 t = imn;
2247 }
2248 pkt_dev->cur_daddr = htonl(t);
2249 }
2250 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002251 if (pkt_dev->cflows) {
2252 pkt_dev->flows[flow].cur_daddr =
2253 pkt_dev->cur_daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 pkt_dev->nflows++;
2255 }
2256 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002257 } else { /* IPV6 * */
2258
2259 if (pkt_dev->min_in6_daddr.s6_addr32[0] == 0 &&
2260 pkt_dev->min_in6_daddr.s6_addr32[1] == 0 &&
2261 pkt_dev->min_in6_daddr.s6_addr32[2] == 0 &&
2262 pkt_dev->min_in6_daddr.s6_addr32[3] == 0) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 else {
2264 int i;
2265
2266 /* Only random destinations yet */
2267
Luiz Capitulino222f1802006-03-20 22:16:13 -08002268 for (i = 0; i < 4; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 pkt_dev->cur_in6_daddr.s6_addr32[i] =
Al Viro252e3342006-11-14 20:48:11 -08002270 (((__force __be32)pktgen_random() |
Luiz Capitulino222f1802006-03-20 22:16:13 -08002271 pkt_dev->min_in6_daddr.s6_addr32[i]) &
2272 pkt_dev->max_in6_daddr.s6_addr32[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002274 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 }
2276
Luiz Capitulino222f1802006-03-20 22:16:13 -08002277 if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
2278 __u32 t;
2279 if (pkt_dev->flags & F_TXSIZE_RND) {
2280 t = ((pktgen_random() %
2281 (pkt_dev->max_pkt_size - pkt_dev->min_pkt_size))
2282 + pkt_dev->min_pkt_size);
2283 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 t = pkt_dev->cur_pkt_size + 1;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002285 if (t > pkt_dev->max_pkt_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 t = pkt_dev->min_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002287 }
2288 pkt_dev->cur_pkt_size = t;
2289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290
2291 pkt_dev->flows[flow].count++;
2292}
2293
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002294static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
2295{
2296 unsigned i;
2297 for(i = 0; i < pkt_dev->nr_labels; i++) {
2298 *mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
2299 }
2300 mpls--;
2301 *mpls |= MPLS_STACK_BOTTOM;
2302}
2303
Al Viro0f37c602006-11-03 03:49:56 -08002304static inline __be16 build_tci(unsigned int id, unsigned int cfi,
2305 unsigned int prio)
2306{
2307 return htons(id | (cfi << 12) | (prio << 13));
2308}
2309
Luiz Capitulino222f1802006-03-20 22:16:13 -08002310static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
2311 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312{
2313 struct sk_buff *skb = NULL;
2314 __u8 *eth;
2315 struct udphdr *udph;
2316 int datalen, iplen;
2317 struct iphdr *iph;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002318 struct pktgen_hdr *pgh = NULL;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002319 __be16 protocol = __constant_htons(ETH_P_IP);
2320 __be32 *mpls;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002321 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2322 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2323 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2324 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2325
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002326
2327 if (pkt_dev->nr_labels)
2328 protocol = __constant_htons(ETH_P_MPLS_UC);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002329
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002330 if (pkt_dev->vlan_id != 0xffff)
2331 protocol = __constant_htons(ETH_P_8021Q);
2332
Robert Olsson64053be2005-06-26 15:27:10 -07002333 /* Update any of the values, used when we're incrementing various
2334 * fields.
2335 */
2336 mod_cur_headers(pkt_dev);
2337
David S. Miller7ac54592006-01-18 14:19:10 -08002338 datalen = (odev->hard_header_len + 16) & ~0xf;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002339 skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + datalen +
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002340 pkt_dev->nr_labels*sizeof(u32) +
2341 VLAN_TAG_SIZE(pkt_dev) + SVLAN_TAG_SIZE(pkt_dev),
2342 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 if (!skb) {
2344 sprintf(pkt_dev->result, "No memory");
2345 return NULL;
2346 }
2347
David S. Miller7ac54592006-01-18 14:19:10 -08002348 skb_reserve(skb, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349
2350 /* Reserve for ethernet and IP header */
2351 eth = (__u8 *) skb_push(skb, 14);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002352 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2353 if (pkt_dev->nr_labels)
2354 mpls_push(mpls, pkt_dev);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002355
2356 if (pkt_dev->vlan_id != 0xffff) {
2357 if(pkt_dev->svlan_id != 0xffff) {
2358 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002359 *svlan_tci = build_tci(pkt_dev->svlan_id,
2360 pkt_dev->svlan_cfi,
2361 pkt_dev->svlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002362 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2363 *svlan_encapsulated_proto = __constant_htons(ETH_P_8021Q);
2364 }
2365 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002366 *vlan_tci = build_tci(pkt_dev->vlan_id,
2367 pkt_dev->vlan_cfi,
2368 pkt_dev->vlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002369 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2370 *vlan_encapsulated_proto = __constant_htons(ETH_P_IP);
2371 }
2372
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 iph = (struct iphdr *)skb_put(skb, sizeof(struct iphdr));
2374 udph = (struct udphdr *)skb_put(skb, sizeof(struct udphdr));
2375
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 memcpy(eth, pkt_dev->hh, 12);
Al Viro252e3342006-11-14 20:48:11 -08002377 *(__be16 *) & eth[12] = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002379 /* Eth + IPh + UDPh + mpls */
2380 datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002381 pkt_dev->nr_labels*sizeof(u32) - VLAN_TAG_SIZE(pkt_dev) - SVLAN_TAG_SIZE(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002382 if (datalen < sizeof(struct pktgen_hdr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 datalen = sizeof(struct pktgen_hdr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002384
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 udph->source = htons(pkt_dev->cur_udp_src);
2386 udph->dest = htons(pkt_dev->cur_udp_dst);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002387 udph->len = htons(datalen + 8); /* DATA + udphdr */
2388 udph->check = 0; /* No checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389
2390 iph->ihl = 5;
2391 iph->version = 4;
2392 iph->ttl = 32;
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002393 iph->tos = pkt_dev->tos;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002394 iph->protocol = IPPROTO_UDP; /* UDP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 iph->saddr = pkt_dev->cur_saddr;
2396 iph->daddr = pkt_dev->cur_daddr;
2397 iph->frag_off = 0;
2398 iplen = 20 + 8 + datalen;
2399 iph->tot_len = htons(iplen);
2400 iph->check = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002401 iph->check = ip_fast_csum((void *)iph, iph->ihl);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002402 skb->protocol = protocol;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002403 skb->mac.raw = ((u8 *) iph) - 14 - pkt_dev->nr_labels*sizeof(u32) -
2404 VLAN_TAG_SIZE(pkt_dev) - SVLAN_TAG_SIZE(pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 skb->dev = odev;
2406 skb->pkt_type = PACKET_HOST;
Chen-Li Tienaaf58062006-08-07 20:49:07 -07002407 skb->nh.iph = iph;
2408 skb->h.uh = udph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409
Luiz Capitulino222f1802006-03-20 22:16:13 -08002410 if (pkt_dev->nfrags <= 0)
2411 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 else {
2413 int frags = pkt_dev->nfrags;
2414 int i;
2415
Luiz Capitulino222f1802006-03-20 22:16:13 -08002416 pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
2417
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 if (frags > MAX_SKB_FRAGS)
2419 frags = MAX_SKB_FRAGS;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002420 if (datalen > frags * PAGE_SIZE) {
2421 skb_put(skb, datalen - frags * PAGE_SIZE);
2422 datalen = frags * PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 }
2424
2425 i = 0;
2426 while (datalen > 0) {
2427 struct page *page = alloc_pages(GFP_KERNEL, 0);
2428 skb_shinfo(skb)->frags[i].page = page;
2429 skb_shinfo(skb)->frags[i].page_offset = 0;
2430 skb_shinfo(skb)->frags[i].size =
Luiz Capitulino222f1802006-03-20 22:16:13 -08002431 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 datalen -= skb_shinfo(skb)->frags[i].size;
2433 skb->len += skb_shinfo(skb)->frags[i].size;
2434 skb->data_len += skb_shinfo(skb)->frags[i].size;
2435 i++;
2436 skb_shinfo(skb)->nr_frags = i;
2437 }
2438
2439 while (i < frags) {
2440 int rem;
2441
2442 if (i == 0)
2443 break;
2444
2445 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
2446 if (rem == 0)
2447 break;
2448
2449 skb_shinfo(skb)->frags[i - 1].size -= rem;
2450
Luiz Capitulino222f1802006-03-20 22:16:13 -08002451 skb_shinfo(skb)->frags[i] =
2452 skb_shinfo(skb)->frags[i - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 get_page(skb_shinfo(skb)->frags[i].page);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002454 skb_shinfo(skb)->frags[i].page =
2455 skb_shinfo(skb)->frags[i - 1].page;
2456 skb_shinfo(skb)->frags[i].page_offset +=
2457 skb_shinfo(skb)->frags[i - 1].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 skb_shinfo(skb)->frags[i].size = rem;
2459 i++;
2460 skb_shinfo(skb)->nr_frags = i;
2461 }
2462 }
2463
Luiz Capitulino222f1802006-03-20 22:16:13 -08002464 /* Stamp the time, and sequence number, convert them to network byte order */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465
Luiz Capitulino222f1802006-03-20 22:16:13 -08002466 if (pgh) {
2467 struct timeval timestamp;
2468
2469 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2470 pgh->seq_num = htonl(pkt_dev->seq_num);
2471
2472 do_gettimeofday(&timestamp);
2473 pgh->tv_sec = htonl(timestamp.tv_sec);
2474 pgh->tv_usec = htonl(timestamp.tv_usec);
2475 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002476
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 return skb;
2478}
2479
2480/*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002481 * scan_ip6, fmt_ip taken from dietlibc-0.21
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 * Author Felix von Leitner <felix-dietlibc@fefe.de>
2483 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002484 * Slightly modified for kernel.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 * Should be candidate for net/ipv4/utils.c
2486 * --ro
2487 */
2488
Luiz Capitulino222f1802006-03-20 22:16:13 -08002489static unsigned int scan_ip6(const char *s, char ip[16])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490{
2491 unsigned int i;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002492 unsigned int len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 unsigned long u;
2494 char suffix[16];
Luiz Capitulino222f1802006-03-20 22:16:13 -08002495 unsigned int prefixlen = 0;
2496 unsigned int suffixlen = 0;
Al Viro252e3342006-11-14 20:48:11 -08002497 __be32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498
Luiz Capitulino222f1802006-03-20 22:16:13 -08002499 for (i = 0; i < 16; i++)
2500 ip[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501
2502 for (;;) {
2503 if (*s == ':') {
2504 len++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002505 if (s[1] == ':') { /* Found "::", skip to part 2 */
2506 s += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 len++;
2508 break;
2509 }
2510 s++;
2511 }
2512 {
2513 char *tmp;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002514 u = simple_strtoul(s, &tmp, 16);
2515 i = tmp - s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 }
2517
Luiz Capitulino222f1802006-03-20 22:16:13 -08002518 if (!i)
2519 return 0;
2520 if (prefixlen == 12 && s[i] == '.') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521
2522 /* the last 4 bytes may be written as IPv4 address */
2523
2524 tmp = in_aton(s);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002525 memcpy((struct in_addr *)(ip + 12), &tmp, sizeof(tmp));
2526 return i + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 }
2528 ip[prefixlen++] = (u >> 8);
2529 ip[prefixlen++] = (u & 255);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002530 s += i;
2531 len += i;
2532 if (prefixlen == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 return len;
2534 }
2535
2536/* part 2, after "::" */
2537 for (;;) {
2538 if (*s == ':') {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002539 if (suffixlen == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 break;
2541 s++;
2542 len++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002543 } else if (suffixlen != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 break;
2545 {
2546 char *tmp;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002547 u = simple_strtol(s, &tmp, 16);
2548 i = tmp - s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 }
2550 if (!i) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002551 if (*s)
2552 len--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 break;
2554 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002555 if (suffixlen + prefixlen <= 12 && s[i] == '.') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 tmp = in_aton(s);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002557 memcpy((struct in_addr *)(suffix + suffixlen), &tmp,
2558 sizeof(tmp));
2559 suffixlen += 4;
2560 len += strlen(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 break;
2562 }
2563 suffix[suffixlen++] = (u >> 8);
2564 suffix[suffixlen++] = (u & 255);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002565 s += i;
2566 len += i;
2567 if (prefixlen + suffixlen == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 break;
2569 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002570 for (i = 0; i < suffixlen; i++)
2571 ip[16 - suffixlen + i] = suffix[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 return len;
2573}
2574
Luiz Capitulino222f1802006-03-20 22:16:13 -08002575static char tohex(char hexdigit)
2576{
2577 return hexdigit > 9 ? hexdigit + 'a' - 10 : hexdigit + '0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578}
2579
Luiz Capitulino222f1802006-03-20 22:16:13 -08002580static int fmt_xlong(char *s, unsigned int i)
2581{
2582 char *bak = s;
2583 *s = tohex((i >> 12) & 0xf);
2584 if (s != bak || *s != '0')
2585 ++s;
2586 *s = tohex((i >> 8) & 0xf);
2587 if (s != bak || *s != '0')
2588 ++s;
2589 *s = tohex((i >> 4) & 0xf);
2590 if (s != bak || *s != '0')
2591 ++s;
2592 *s = tohex(i & 0xf);
2593 return s - bak + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594}
2595
Luiz Capitulino222f1802006-03-20 22:16:13 -08002596static unsigned int fmt_ip6(char *s, const char ip[16])
2597{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 unsigned int len;
2599 unsigned int i;
2600 unsigned int temp;
2601 unsigned int compressing;
2602 int j;
2603
Luiz Capitulino222f1802006-03-20 22:16:13 -08002604 len = 0;
2605 compressing = 0;
2606 for (j = 0; j < 16; j += 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607
2608#ifdef V4MAPPEDPREFIX
Luiz Capitulino222f1802006-03-20 22:16:13 -08002609 if (j == 12 && !memcmp(ip, V4mappedprefix, 12)) {
2610 inet_ntoa_r(*(struct in_addr *)(ip + 12), s);
2611 temp = strlen(s);
2612 return len + temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 }
2614#endif
Luiz Capitulino222f1802006-03-20 22:16:13 -08002615 temp = ((unsigned long)(unsigned char)ip[j] << 8) +
2616 (unsigned long)(unsigned char)ip[j + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 if (temp == 0) {
2618 if (!compressing) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002619 compressing = 1;
2620 if (j == 0) {
2621 *s++ = ':';
2622 ++len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 }
2624 }
2625 } else {
2626 if (compressing) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002627 compressing = 0;
2628 *s++ = ':';
2629 ++len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002631 i = fmt_xlong(s, temp);
2632 len += i;
2633 s += i;
2634 if (j < 14) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 *s++ = ':';
2636 ++len;
2637 }
2638 }
2639 }
2640 if (compressing) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002641 *s++ = ':';
2642 ++len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002644 *s = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 return len;
2646}
2647
Luiz Capitulino222f1802006-03-20 22:16:13 -08002648static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
2649 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650{
2651 struct sk_buff *skb = NULL;
2652 __u8 *eth;
2653 struct udphdr *udph;
2654 int datalen;
2655 struct ipv6hdr *iph;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002656 struct pktgen_hdr *pgh = NULL;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002657 __be16 protocol = __constant_htons(ETH_P_IPV6);
2658 __be32 *mpls;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002659 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2660 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2661 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2662 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002663
2664 if (pkt_dev->nr_labels)
2665 protocol = __constant_htons(ETH_P_MPLS_UC);
Robert Olsson64053be2005-06-26 15:27:10 -07002666
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002667 if (pkt_dev->vlan_id != 0xffff)
2668 protocol = __constant_htons(ETH_P_8021Q);
2669
Robert Olsson64053be2005-06-26 15:27:10 -07002670 /* Update any of the values, used when we're incrementing various
2671 * fields.
2672 */
2673 mod_cur_headers(pkt_dev);
2674
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002675 skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + 16 +
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002676 pkt_dev->nr_labels*sizeof(u32) +
2677 VLAN_TAG_SIZE(pkt_dev) + SVLAN_TAG_SIZE(pkt_dev),
2678 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 if (!skb) {
2680 sprintf(pkt_dev->result, "No memory");
2681 return NULL;
2682 }
2683
2684 skb_reserve(skb, 16);
2685
2686 /* Reserve for ethernet and IP header */
2687 eth = (__u8 *) skb_push(skb, 14);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002688 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2689 if (pkt_dev->nr_labels)
2690 mpls_push(mpls, pkt_dev);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002691
2692 if (pkt_dev->vlan_id != 0xffff) {
2693 if(pkt_dev->svlan_id != 0xffff) {
2694 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002695 *svlan_tci = build_tci(pkt_dev->svlan_id,
2696 pkt_dev->svlan_cfi,
2697 pkt_dev->svlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002698 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2699 *svlan_encapsulated_proto = __constant_htons(ETH_P_8021Q);
2700 }
2701 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002702 *vlan_tci = build_tci(pkt_dev->vlan_id,
2703 pkt_dev->vlan_cfi,
2704 pkt_dev->vlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002705 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2706 *vlan_encapsulated_proto = __constant_htons(ETH_P_IPV6);
2707 }
2708
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 iph = (struct ipv6hdr *)skb_put(skb, sizeof(struct ipv6hdr));
2710 udph = (struct udphdr *)skb_put(skb, sizeof(struct udphdr));
2711
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 memcpy(eth, pkt_dev->hh, 12);
Al Viro252e3342006-11-14 20:48:11 -08002713 *(__be16 *) & eth[12] = protocol;
Robert Olsson64053be2005-06-26 15:27:10 -07002714
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002715 /* Eth + IPh + UDPh + mpls */
2716 datalen = pkt_dev->cur_pkt_size - 14 -
2717 sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002718 pkt_dev->nr_labels*sizeof(u32) - VLAN_TAG_SIZE(pkt_dev) - SVLAN_TAG_SIZE(pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719
Luiz Capitulino222f1802006-03-20 22:16:13 -08002720 if (datalen < sizeof(struct pktgen_hdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 datalen = sizeof(struct pktgen_hdr);
2722 if (net_ratelimit())
Luiz Capitulino222f1802006-03-20 22:16:13 -08002723 printk(KERN_INFO "pktgen: increased datalen to %d\n",
2724 datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 }
2726
2727 udph->source = htons(pkt_dev->cur_udp_src);
2728 udph->dest = htons(pkt_dev->cur_udp_dst);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002729 udph->len = htons(datalen + sizeof(struct udphdr));
2730 udph->check = 0; /* No checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731
Al Viro252e3342006-11-14 20:48:11 -08002732 *(__be32 *) iph = __constant_htonl(0x60000000); /* Version + flow */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002734 if (pkt_dev->traffic_class) {
2735 /* Version + traffic class + flow (0) */
Al Viro252e3342006-11-14 20:48:11 -08002736 *(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002737 }
2738
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 iph->hop_limit = 32;
2740
2741 iph->payload_len = htons(sizeof(struct udphdr) + datalen);
2742 iph->nexthdr = IPPROTO_UDP;
2743
2744 ipv6_addr_copy(&iph->daddr, &pkt_dev->cur_in6_daddr);
2745 ipv6_addr_copy(&iph->saddr, &pkt_dev->cur_in6_saddr);
2746
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002747 skb->mac.raw = ((u8 *) iph) - 14 - pkt_dev->nr_labels*sizeof(u32) -
2748 VLAN_TAG_SIZE(pkt_dev) - SVLAN_TAG_SIZE(pkt_dev);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002749 skb->protocol = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 skb->dev = odev;
2751 skb->pkt_type = PACKET_HOST;
David S. Miller69d8c282006-08-07 20:52:10 -07002752 skb->nh.ipv6h = iph;
2753 skb->h.uh = udph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754
Luiz Capitulino222f1802006-03-20 22:16:13 -08002755 if (pkt_dev->nfrags <= 0)
2756 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 else {
2758 int frags = pkt_dev->nfrags;
2759 int i;
2760
Luiz Capitulino222f1802006-03-20 22:16:13 -08002761 pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
2762
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 if (frags > MAX_SKB_FRAGS)
2764 frags = MAX_SKB_FRAGS;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002765 if (datalen > frags * PAGE_SIZE) {
2766 skb_put(skb, datalen - frags * PAGE_SIZE);
2767 datalen = frags * PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 }
2769
2770 i = 0;
2771 while (datalen > 0) {
2772 struct page *page = alloc_pages(GFP_KERNEL, 0);
2773 skb_shinfo(skb)->frags[i].page = page;
2774 skb_shinfo(skb)->frags[i].page_offset = 0;
2775 skb_shinfo(skb)->frags[i].size =
Luiz Capitulino222f1802006-03-20 22:16:13 -08002776 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 datalen -= skb_shinfo(skb)->frags[i].size;
2778 skb->len += skb_shinfo(skb)->frags[i].size;
2779 skb->data_len += skb_shinfo(skb)->frags[i].size;
2780 i++;
2781 skb_shinfo(skb)->nr_frags = i;
2782 }
2783
2784 while (i < frags) {
2785 int rem;
2786
2787 if (i == 0)
2788 break;
2789
2790 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
2791 if (rem == 0)
2792 break;
2793
2794 skb_shinfo(skb)->frags[i - 1].size -= rem;
2795
Luiz Capitulino222f1802006-03-20 22:16:13 -08002796 skb_shinfo(skb)->frags[i] =
2797 skb_shinfo(skb)->frags[i - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 get_page(skb_shinfo(skb)->frags[i].page);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002799 skb_shinfo(skb)->frags[i].page =
2800 skb_shinfo(skb)->frags[i - 1].page;
2801 skb_shinfo(skb)->frags[i].page_offset +=
2802 skb_shinfo(skb)->frags[i - 1].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 skb_shinfo(skb)->frags[i].size = rem;
2804 i++;
2805 skb_shinfo(skb)->nr_frags = i;
2806 }
2807 }
2808
Luiz Capitulino222f1802006-03-20 22:16:13 -08002809 /* Stamp the time, and sequence number, convert them to network byte order */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 /* should we update cloned packets too ? */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002811 if (pgh) {
2812 struct timeval timestamp;
2813
2814 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2815 pgh->seq_num = htonl(pkt_dev->seq_num);
2816
2817 do_gettimeofday(&timestamp);
2818 pgh->tv_sec = htonl(timestamp.tv_sec);
2819 pgh->tv_usec = htonl(timestamp.tv_usec);
2820 }
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002821 /* pkt_dev->seq_num++; FF: you really mean this? */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002822
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 return skb;
2824}
2825
Luiz Capitulino222f1802006-03-20 22:16:13 -08002826static inline struct sk_buff *fill_packet(struct net_device *odev,
2827 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828{
Luiz Capitulino222f1802006-03-20 22:16:13 -08002829 if (pkt_dev->flags & F_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 return fill_packet_ipv6(odev, pkt_dev);
2831 else
2832 return fill_packet_ipv4(odev, pkt_dev);
2833}
2834
Luiz Capitulino222f1802006-03-20 22:16:13 -08002835static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836{
Luiz Capitulino222f1802006-03-20 22:16:13 -08002837 pkt_dev->seq_num = 1;
2838 pkt_dev->idle_acc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002840 pkt_dev->tx_bytes = 0;
2841 pkt_dev->errors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842}
2843
2844/* Set up structure for sending pkts, clear counters */
2845
2846static void pktgen_run(struct pktgen_thread *t)
2847{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08002848 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 int started = 0;
2850
2851 PG_DEBUG(printk("pktgen: entering pktgen_run. %p\n", t));
2852
2853 if_lock(t);
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08002854 list_for_each_entry(pkt_dev, &t->if_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855
2856 /*
2857 * setup odev and create initial packet.
2858 */
2859 pktgen_setup_inject(pkt_dev);
2860
Luiz Capitulino222f1802006-03-20 22:16:13 -08002861 if (pkt_dev->odev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 pktgen_clear_counters(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002863 pkt_dev->running = 1; /* Cranke yeself! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 pkt_dev->skb = NULL;
2865 pkt_dev->started_at = getCurUs();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002866 pkt_dev->next_tx_us = getCurUs(); /* Transmit immediately */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 pkt_dev->next_tx_ns = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002868
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 strcpy(pkt_dev->result, "Starting");
2870 started++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002871 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 strcpy(pkt_dev->result, "Error starting");
2873 }
2874 if_unlock(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002875 if (started)
2876 t->control &= ~(T_STOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877}
2878
2879static void pktgen_stop_all_threads_ifs(void)
2880{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002881 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882
Arthur Kepner95ed63f2006-03-20 21:26:56 -08002883 PG_DEBUG(printk("pktgen: entering pktgen_stop_all_threads_ifs.\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08002885 mutex_lock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002886
2887 list_for_each_entry(t, &pktgen_threads, th_list)
Arthur Kepner95ed63f2006-03-20 21:26:56 -08002888 t->control |= T_STOP;
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002889
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08002890 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891}
2892
Luiz Capitulino222f1802006-03-20 22:16:13 -08002893static int thread_is_running(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08002895 struct pktgen_dev *pkt_dev;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002896 int res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08002898 list_for_each_entry(pkt_dev, &t->if_list, list)
2899 if (pkt_dev->running) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 res = 1;
2901 break;
2902 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002903 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904}
2905
Luiz Capitulino222f1802006-03-20 22:16:13 -08002906static int pktgen_wait_thread_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907{
Luiz Capitulino222f1802006-03-20 22:16:13 -08002908 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909
Luiz Capitulino222f1802006-03-20 22:16:13 -08002910 while (thread_is_running(t)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911
Luiz Capitulino222f1802006-03-20 22:16:13 -08002912 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913
Luiz Capitulino222f1802006-03-20 22:16:13 -08002914 msleep_interruptible(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915
Luiz Capitulino222f1802006-03-20 22:16:13 -08002916 if (signal_pending(current))
2917 goto signal;
2918 if_lock(t);
2919 }
2920 if_unlock(t);
2921 return 1;
2922signal:
2923 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924}
2925
2926static int pktgen_wait_all_threads_run(void)
2927{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002928 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 int sig = 1;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002930
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08002931 mutex_lock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002932
2933 list_for_each_entry(t, &pktgen_threads, th_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 sig = pktgen_wait_thread_run(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002935 if (sig == 0)
2936 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 }
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002938
2939 if (sig == 0)
2940 list_for_each_entry(t, &pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 t->control |= (T_STOP);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002942
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08002943 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 return sig;
2945}
2946
2947static void pktgen_run_all_threads(void)
2948{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002949 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950
2951 PG_DEBUG(printk("pktgen: entering pktgen_run_all_threads.\n"));
2952
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08002953 mutex_lock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002955 list_for_each_entry(t, &pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 t->control |= (T_RUN);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002957
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08002958 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959
Luiz Capitulino222f1802006-03-20 22:16:13 -08002960 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
2961
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 pktgen_wait_all_threads_run();
2963}
2964
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
2966{
Luiz Capitulino222f1802006-03-20 22:16:13 -08002967 __u64 total_us, bps, mbps, pps, idle;
2968 char *p = pkt_dev->result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969
Luiz Capitulino222f1802006-03-20 22:16:13 -08002970 total_us = pkt_dev->stopped_at - pkt_dev->started_at;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971
Luiz Capitulino222f1802006-03-20 22:16:13 -08002972 idle = pkt_dev->idle_acc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973
Luiz Capitulino222f1802006-03-20 22:16:13 -08002974 p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
2975 (unsigned long long)total_us,
2976 (unsigned long long)(total_us - idle),
2977 (unsigned long long)idle,
2978 (unsigned long long)pkt_dev->sofar,
2979 pkt_dev->cur_pkt_size, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980
Luiz Capitulino222f1802006-03-20 22:16:13 -08002981 pps = pkt_dev->sofar * USEC_PER_SEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982
Luiz Capitulino222f1802006-03-20 22:16:13 -08002983 while ((total_us >> 32) != 0) {
2984 pps >>= 1;
2985 total_us >>= 1;
2986 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987
Luiz Capitulino222f1802006-03-20 22:16:13 -08002988 do_div(pps, total_us);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989
Luiz Capitulino222f1802006-03-20 22:16:13 -08002990 bps = pps * 8 * pkt_dev->cur_pkt_size;
2991
2992 mbps = bps;
2993 do_div(mbps, 1000000);
2994 p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu",
2995 (unsigned long long)pps,
2996 (unsigned long long)mbps,
2997 (unsigned long long)bps,
2998 (unsigned long long)pkt_dev->errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000
3001/* Set stopped-at timer, remove from running list, do counters & statistics */
3002
Luiz Capitulino222f1802006-03-20 22:16:13 -08003003static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003005 int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003006
Luiz Capitulino222f1802006-03-20 22:16:13 -08003007 if (!pkt_dev->running) {
3008 printk("pktgen: interface: %s is already stopped\n",
3009 pkt_dev->ifname);
3010 return -EINVAL;
3011 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012
Luiz Capitulino222f1802006-03-20 22:16:13 -08003013 pkt_dev->stopped_at = getCurUs();
3014 pkt_dev->running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003016 show_results(pkt_dev, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017
Luiz Capitulino222f1802006-03-20 22:16:13 -08003018 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019}
3020
Luiz Capitulino222f1802006-03-20 22:16:13 -08003021static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003023 struct pktgen_dev *pkt_dev, *best = NULL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003024
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 if_lock(t);
3026
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003027 list_for_each_entry(pkt_dev, &t->if_list, list) {
3028 if (!pkt_dev->running)
Luiz Capitulino222f1802006-03-20 22:16:13 -08003029 continue;
3030 if (best == NULL)
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003031 best = pkt_dev;
3032 else if (pkt_dev->next_tx_us < best->next_tx_us)
3033 best = pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 }
3035 if_unlock(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003036 return best;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037}
3038
Luiz Capitulino222f1802006-03-20 22:16:13 -08003039static void pktgen_stop(struct pktgen_thread *t)
3040{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003041 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003043 PG_DEBUG(printk("pktgen: entering pktgen_stop\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044
Luiz Capitulino222f1802006-03-20 22:16:13 -08003045 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003047 list_for_each_entry(pkt_dev, &t->if_list, list) {
3048 pktgen_stop_device(pkt_dev);
3049 if (pkt_dev->skb)
3050 kfree_skb(pkt_dev->skb);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003051
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003052 pkt_dev->skb = NULL;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003053 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054
Luiz Capitulino222f1802006-03-20 22:16:13 -08003055 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056}
3057
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003058/*
3059 * one of our devices needs to be removed - find it
3060 * and remove it
3061 */
3062static void pktgen_rem_one_if(struct pktgen_thread *t)
3063{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003064 struct list_head *q, *n;
3065 struct pktgen_dev *cur;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003066
3067 PG_DEBUG(printk("pktgen: entering pktgen_rem_one_if\n"));
3068
3069 if_lock(t);
3070
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003071 list_for_each_safe(q, n, &t->if_list) {
3072 cur = list_entry(q, struct pktgen_dev, list);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003073
Luiz Capitulino222f1802006-03-20 22:16:13 -08003074 if (!cur->removal_mark)
3075 continue;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003076
3077 if (cur->skb)
3078 kfree_skb(cur->skb);
3079 cur->skb = NULL;
3080
3081 pktgen_remove_device(t, cur);
3082
3083 break;
3084 }
3085
3086 if_unlock(t);
3087}
3088
Luiz Capitulino222f1802006-03-20 22:16:13 -08003089static void pktgen_rem_all_ifs(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003091 struct list_head *q, *n;
3092 struct pktgen_dev *cur;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003093
3094 /* Remove all devices, free mem */
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003095
3096 PG_DEBUG(printk("pktgen: entering pktgen_rem_all_ifs\n"));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003097 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003099 list_for_each_safe(q, n, &t->if_list) {
3100 cur = list_entry(q, struct pktgen_dev, list);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003101
3102 if (cur->skb)
3103 kfree_skb(cur->skb);
3104 cur->skb = NULL;
3105
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 pktgen_remove_device(t, cur);
3107 }
3108
Luiz Capitulino222f1802006-03-20 22:16:13 -08003109 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110}
3111
Luiz Capitulino222f1802006-03-20 22:16:13 -08003112static void pktgen_rem_thread(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003114 /* Remove from the thread list */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115
David S. Milleree74baa2007-01-01 20:51:53 -08003116 remove_proc_entry(t->tsk->comm, pg_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003118 mutex_lock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003120 list_del(&t->th_list);
3121
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003122 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123}
3124
3125static __inline__ void pktgen_xmit(struct pktgen_dev *pkt_dev)
3126{
3127 struct net_device *odev = NULL;
3128 __u64 idle_start = 0;
3129 int ret;
3130
3131 odev = pkt_dev->odev;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003132
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 if (pkt_dev->delay_us || pkt_dev->delay_ns) {
3134 u64 now;
3135
3136 now = getCurUs();
3137 if (now < pkt_dev->next_tx_us)
3138 spin(pkt_dev, pkt_dev->next_tx_us);
3139
3140 /* This is max DELAY, this has special meaning of
3141 * "never transmit"
3142 */
3143 if (pkt_dev->delay_us == 0x7FFFFFFF) {
3144 pkt_dev->next_tx_us = getCurUs() + pkt_dev->delay_us;
3145 pkt_dev->next_tx_ns = pkt_dev->delay_ns;
3146 goto out;
3147 }
3148 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003149
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 if (netif_queue_stopped(odev) || need_resched()) {
3151 idle_start = getCurUs();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003152
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 if (!netif_running(odev)) {
3154 pktgen_stop_device(pkt_dev);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003155 if (pkt_dev->skb)
3156 kfree_skb(pkt_dev->skb);
3157 pkt_dev->skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 goto out;
3159 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003160 if (need_resched())
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 schedule();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003162
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 pkt_dev->idle_acc += getCurUs() - idle_start;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003164
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 if (netif_queue_stopped(odev)) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08003166 pkt_dev->next_tx_us = getCurUs(); /* TODO */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 pkt_dev->next_tx_ns = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003168 goto out; /* Try the next interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 }
3170 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003171
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 if (pkt_dev->last_ok || !pkt_dev->skb) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08003173 if ((++pkt_dev->clone_count >= pkt_dev->clone_skb)
3174 || (!pkt_dev->skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 /* build a new pkt */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003176 if (pkt_dev->skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 kfree_skb(pkt_dev->skb);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003178
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 pkt_dev->skb = fill_packet(odev, pkt_dev);
3180 if (pkt_dev->skb == NULL) {
3181 printk("pktgen: ERROR: couldn't allocate skb in fill_packet.\n");
3182 schedule();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003183 pkt_dev->clone_count--; /* back out increment, OOM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 goto out;
3185 }
3186 pkt_dev->allocated_skbs++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003187 pkt_dev->clone_count = 0; /* reset counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 }
3189 }
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003190
Herbert Xu932ff272006-06-09 12:20:56 -07003191 netif_tx_lock_bh(odev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 if (!netif_queue_stopped(odev)) {
3193
3194 atomic_inc(&(pkt_dev->skb->users));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003195 retry_now:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 ret = odev->hard_start_xmit(pkt_dev->skb, odev);
3197 if (likely(ret == NETDEV_TX_OK)) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08003198 pkt_dev->last_ok = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 pkt_dev->sofar++;
3200 pkt_dev->seq_num++;
3201 pkt_dev->tx_bytes += pkt_dev->cur_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003202
3203 } else if (ret == NETDEV_TX_LOCKED
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 && (odev->features & NETIF_F_LLTX)) {
3205 cpu_relax();
3206 goto retry_now;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003207 } else { /* Retry it next time */
3208
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 atomic_dec(&(pkt_dev->skb->users));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003210
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 if (debug && net_ratelimit())
3212 printk(KERN_INFO "pktgen: Hard xmit error\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003213
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 pkt_dev->errors++;
3215 pkt_dev->last_ok = 0;
3216 }
3217
3218 pkt_dev->next_tx_us = getCurUs();
3219 pkt_dev->next_tx_ns = 0;
3220
3221 pkt_dev->next_tx_us += pkt_dev->delay_us;
3222 pkt_dev->next_tx_ns += pkt_dev->delay_ns;
3223
3224 if (pkt_dev->next_tx_ns > 1000) {
3225 pkt_dev->next_tx_us++;
3226 pkt_dev->next_tx_ns -= 1000;
3227 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229
Luiz Capitulino222f1802006-03-20 22:16:13 -08003230 else { /* Retry it next time */
3231 pkt_dev->last_ok = 0;
3232 pkt_dev->next_tx_us = getCurUs(); /* TODO */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 pkt_dev->next_tx_ns = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235
Herbert Xu932ff272006-06-09 12:20:56 -07003236 netif_tx_unlock_bh(odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003237
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 /* If pkt_dev->count is zero, then run forever */
3239 if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
3240 if (atomic_read(&(pkt_dev->skb->users)) != 1) {
3241 idle_start = getCurUs();
3242 while (atomic_read(&(pkt_dev->skb->users)) != 1) {
3243 if (signal_pending(current)) {
3244 break;
3245 }
3246 schedule();
3247 }
3248 pkt_dev->idle_acc += getCurUs() - idle_start;
3249 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003250
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 /* Done with this */
3252 pktgen_stop_device(pkt_dev);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003253 if (pkt_dev->skb)
3254 kfree_skb(pkt_dev->skb);
3255 pkt_dev->skb = NULL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003256 }
3257out:;
3258}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003260/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 * Main loop of the thread goes here
3262 */
3263
David S. Milleree74baa2007-01-01 20:51:53 -08003264static int pktgen_thread_worker(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265{
3266 DEFINE_WAIT(wait);
David S. Milleree74baa2007-01-01 20:51:53 -08003267 struct pktgen_thread *t = arg;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003268 struct pktgen_dev *pkt_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 int cpu = t->cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 u32 max_before_softirq;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003271 u32 tx_since_softirq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272
David S. Milleree74baa2007-01-01 20:51:53 -08003273 BUG_ON(smp_processor_id() != cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274
3275 init_waitqueue_head(&t->queue);
3276
Luiz Capitulino222f1802006-03-20 22:16:13 -08003277 t->pid = current->pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278
Luiz Capitulino222f1802006-03-20 22:16:13 -08003279 PG_DEBUG(printk("pktgen: starting pktgen/%d: pid=%d\n", cpu, current->pid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280
3281 max_before_softirq = t->max_before_softirq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282
David S. Milleree74baa2007-01-01 20:51:53 -08003283 set_current_state(TASK_INTERRUPTIBLE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003284
David S. Milleree74baa2007-01-01 20:51:53 -08003285 while (!kthread_should_stop()) {
3286 pkt_dev = next_to_run(t);
3287
3288 if (!pkt_dev &&
3289 (t->control & (T_STOP | T_RUN | T_REMDEVALL | T_REMDEV))
3290 == 0) {
3291 prepare_to_wait(&(t->queue), &wait,
3292 TASK_INTERRUPTIBLE);
3293 schedule_timeout(HZ / 10);
3294 finish_wait(&(t->queue), &wait);
3295 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003296
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 __set_current_state(TASK_RUNNING);
3298
Luiz Capitulino222f1802006-03-20 22:16:13 -08003299 if (pkt_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300
3301 pktgen_xmit(pkt_dev);
3302
3303 /*
3304 * We like to stay RUNNING but must also give
3305 * others fair share.
3306 */
3307
3308 tx_since_softirq += pkt_dev->last_ok;
3309
3310 if (tx_since_softirq > max_before_softirq) {
3311 if (local_softirq_pending())
3312 do_softirq();
3313 tx_since_softirq = 0;
3314 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 }
3316
Luiz Capitulino222f1802006-03-20 22:16:13 -08003317 if (t->control & T_STOP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 pktgen_stop(t);
3319 t->control &= ~(T_STOP);
3320 }
3321
Luiz Capitulino222f1802006-03-20 22:16:13 -08003322 if (t->control & T_RUN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 pktgen_run(t);
3324 t->control &= ~(T_RUN);
3325 }
3326
Luiz Capitulino222f1802006-03-20 22:16:13 -08003327 if (t->control & T_REMDEVALL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328 pktgen_rem_all_ifs(t);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003329 t->control &= ~(T_REMDEVALL);
3330 }
3331
Luiz Capitulino222f1802006-03-20 22:16:13 -08003332 if (t->control & T_REMDEV) {
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003333 pktgen_rem_one_if(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 t->control &= ~(T_REMDEV);
3335 }
3336
Andrew Morton09fe3ef2007-04-12 14:45:32 -07003337 try_to_freeze();
3338
David S. Milleree74baa2007-01-01 20:51:53 -08003339 set_current_state(TASK_INTERRUPTIBLE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341
David S. Milleree74baa2007-01-01 20:51:53 -08003342 PG_DEBUG(printk("pktgen: %s stopping all device\n", t->tsk->comm));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003343 pktgen_stop(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344
David S. Milleree74baa2007-01-01 20:51:53 -08003345 PG_DEBUG(printk("pktgen: %s removing all device\n", t->tsk->comm));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003346 pktgen_rem_all_ifs(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347
David S. Milleree74baa2007-01-01 20:51:53 -08003348 PG_DEBUG(printk("pktgen: %s removing thread.\n", t->tsk->comm));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003349 pktgen_rem_thread(t);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003350
David S. Milleree74baa2007-01-01 20:51:53 -08003351 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352}
3353
Luiz Capitulino222f1802006-03-20 22:16:13 -08003354static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
3355 const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003357 struct pktgen_dev *p, *pkt_dev = NULL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003358 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003360 list_for_each_entry(p, &t->if_list, list)
3361 if (strncmp(p->ifname, ifname, IFNAMSIZ) == 0) {
3362 pkt_dev = p;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003363 break;
3364 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365
Luiz Capitulino222f1802006-03-20 22:16:13 -08003366 if_unlock(t);
3367 PG_DEBUG(printk("pktgen: find_dev(%s) returning %p\n", ifname, pkt_dev));
3368 return pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369}
3370
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003371/*
3372 * Adds a dev at front of if_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 */
3374
Luiz Capitulino222f1802006-03-20 22:16:13 -08003375static int add_dev_to_thread(struct pktgen_thread *t,
3376 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377{
3378 int rv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379
Luiz Capitulino222f1802006-03-20 22:16:13 -08003380 if_lock(t);
3381
3382 if (pkt_dev->pg_thread) {
3383 printk("pktgen: ERROR: already assigned to a thread.\n");
3384 rv = -EBUSY;
3385 goto out;
3386 }
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003387
3388 list_add(&pkt_dev->list, &t->if_list);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003389 pkt_dev->pg_thread = t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 pkt_dev->running = 0;
3391
Luiz Capitulino222f1802006-03-20 22:16:13 -08003392out:
3393 if_unlock(t);
3394 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395}
3396
3397/* Called under thread lock */
3398
Luiz Capitulino222f1802006-03-20 22:16:13 -08003399static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003401 struct pktgen_dev *pkt_dev;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003402 struct proc_dir_entry *pe;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003403
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 /* We don't allow a device to be on several threads */
3405
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003406 pkt_dev = __pktgen_NN_threads(ifname, FIND);
3407 if (pkt_dev) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08003408 printk("pktgen: ERROR: interface already used.\n");
3409 return -EBUSY;
3410 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003411
3412 pkt_dev = kzalloc(sizeof(struct pktgen_dev), GFP_KERNEL);
3413 if (!pkt_dev)
3414 return -ENOMEM;
3415
Luiz Capitulino222f1802006-03-20 22:16:13 -08003416 pkt_dev->flows = vmalloc(MAX_CFLOWS * sizeof(struct flow_state));
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003417 if (pkt_dev->flows == NULL) {
3418 kfree(pkt_dev);
3419 return -ENOMEM;
3420 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003421 memset(pkt_dev->flows, 0, MAX_CFLOWS * sizeof(struct flow_state));
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003422
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003423 pkt_dev->removal_mark = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003424 pkt_dev->min_pkt_size = ETH_ZLEN;
3425 pkt_dev->max_pkt_size = ETH_ZLEN;
3426 pkt_dev->nfrags = 0;
3427 pkt_dev->clone_skb = pg_clone_skb_d;
3428 pkt_dev->delay_us = pg_delay_d / 1000;
3429 pkt_dev->delay_ns = pg_delay_d % 1000;
3430 pkt_dev->count = pg_count_d;
3431 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003432 pkt_dev->udp_src_min = 9; /* sink port */
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003433 pkt_dev->udp_src_max = 9;
3434 pkt_dev->udp_dst_min = 9;
3435 pkt_dev->udp_dst_max = 9;
3436
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003437 pkt_dev->vlan_p = 0;
3438 pkt_dev->vlan_cfi = 0;
3439 pkt_dev->vlan_id = 0xffff;
3440 pkt_dev->svlan_p = 0;
3441 pkt_dev->svlan_cfi = 0;
3442 pkt_dev->svlan_id = 0xffff;
3443
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003444 strncpy(pkt_dev->ifname, ifname, IFNAMSIZ);
3445
Luiz Capitulino222f1802006-03-20 22:16:13 -08003446 if (!pktgen_setup_dev(pkt_dev)) {
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003447 printk("pktgen: ERROR: pktgen_setup_dev failed.\n");
3448 if (pkt_dev->flows)
3449 vfree(pkt_dev->flows);
3450 kfree(pkt_dev);
3451 return -ENODEV;
3452 }
3453
3454 pe = create_proc_entry(ifname, 0600, pg_proc_dir);
3455 if (!pe) {
3456 printk("pktgen: cannot create %s/%s procfs entry.\n",
3457 PG_PROC_DIR, ifname);
3458 if (pkt_dev->flows)
3459 vfree(pkt_dev->flows);
3460 kfree(pkt_dev);
3461 return -EINVAL;
3462 }
3463 pe->proc_fops = &pktgen_if_fops;
3464 pe->data = pkt_dev;
3465
3466 return add_dev_to_thread(t, pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467}
3468
David S. Milleree74baa2007-01-01 20:51:53 -08003469static int __init pktgen_create_thread(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003471 struct pktgen_thread *t;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003472 struct proc_dir_entry *pe;
David S. Milleree74baa2007-01-01 20:51:53 -08003473 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474
Luiz Capitulino222f1802006-03-20 22:16:13 -08003475 t = kzalloc(sizeof(struct pktgen_thread), GFP_KERNEL);
3476 if (!t) {
3477 printk("pktgen: ERROR: out of memory, can't create new thread.\n");
3478 return -ENOMEM;
3479 }
3480
Luiz Capitulino222f1802006-03-20 22:16:13 -08003481 spin_lock_init(&t->if_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482 t->cpu = cpu;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003483
David S. Milleree74baa2007-01-01 20:51:53 -08003484 INIT_LIST_HEAD(&t->if_list);
3485
3486 list_add_tail(&t->th_list, &pktgen_threads);
3487
3488 p = kthread_create(pktgen_thread_worker, t, "kpktgend_%d", cpu);
3489 if (IS_ERR(p)) {
3490 printk("pktgen: kernel_thread() failed for cpu %d\n", t->cpu);
3491 list_del(&t->th_list);
3492 kfree(t);
3493 return PTR_ERR(p);
3494 }
3495 kthread_bind(p, cpu);
3496 t->tsk = p;
3497
3498 pe = create_proc_entry(t->tsk->comm, 0600, pg_proc_dir);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003499 if (!pe) {
3500 printk("pktgen: cannot create %s/%s procfs entry.\n",
David S. Milleree74baa2007-01-01 20:51:53 -08003501 PG_PROC_DIR, t->tsk->comm);
3502 kthread_stop(p);
3503 list_del(&t->th_list);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003504 kfree(t);
3505 return -EINVAL;
3506 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003507
3508 pe->proc_fops = &pktgen_thread_fops;
3509 pe->data = t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510
David S. Milleree74baa2007-01-01 20:51:53 -08003511 wake_up_process(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512
3513 return 0;
3514}
3515
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003516/*
3517 * Removes a device from the thread if_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003519static void _rem_dev_from_if_list(struct pktgen_thread *t,
3520 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003522 struct list_head *q, *n;
3523 struct pktgen_dev *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003525 list_for_each_safe(q, n, &t->if_list) {
3526 p = list_entry(q, struct pktgen_dev, list);
3527 if (p == pkt_dev)
3528 list_del(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 }
3530}
3531
Luiz Capitulino222f1802006-03-20 22:16:13 -08003532static int pktgen_remove_device(struct pktgen_thread *t,
3533 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534{
3535
3536 PG_DEBUG(printk("pktgen: remove_device pkt_dev=%p\n", pkt_dev));
3537
Luiz Capitulino222f1802006-03-20 22:16:13 -08003538 if (pkt_dev->running) {
3539 printk("pktgen:WARNING: trying to remove a running interface, stopping it now.\n");
3540 pktgen_stop_device(pkt_dev);
3541 }
3542
3543 /* Dis-associate from the interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544
3545 if (pkt_dev->odev) {
3546 dev_put(pkt_dev->odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003547 pkt_dev->odev = NULL;
3548 }
3549
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550 /* And update the thread if_list */
3551
3552 _rem_dev_from_if_list(t, pkt_dev);
3553
Luiz Capitulino222f1802006-03-20 22:16:13 -08003554 /* Clean up proc file system */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003556 remove_proc_entry(pkt_dev->ifname, pg_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557
3558 if (pkt_dev->flows)
3559 vfree(pkt_dev->flows);
3560 kfree(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003561 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562}
3563
Luiz Capitulino222f1802006-03-20 22:16:13 -08003564static int __init pg_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565{
3566 int cpu;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003567 struct proc_dir_entry *pe;
3568
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569 printk(version);
3570
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003571 pg_proc_dir = proc_mkdir(PG_PROC_DIR, proc_net);
3572 if (!pg_proc_dir)
3573 return -ENODEV;
3574 pg_proc_dir->owner = THIS_MODULE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003576 pe = create_proc_entry(PGCTRL, 0600, pg_proc_dir);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003577 if (pe == NULL) {
3578 printk("pktgen: ERROR: cannot create %s procfs entry.\n",
3579 PGCTRL);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003580 proc_net_remove(PG_PROC_DIR);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003581 return -EINVAL;
3582 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583
Luiz Capitulino222f1802006-03-20 22:16:13 -08003584 pe->proc_fops = &pktgen_fops;
3585 pe->data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586
3587 /* Register us to receive netdevice events */
3588 register_netdevice_notifier(&pktgen_notifier_block);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003589
John Hawkes670c02c2005-10-13 09:30:31 -07003590 for_each_online_cpu(cpu) {
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003591 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592
David S. Milleree74baa2007-01-01 20:51:53 -08003593 err = pktgen_create_thread(cpu);
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003594 if (err)
3595 printk("pktgen: WARNING: Cannot create thread for cpu %d (%d)\n",
3596 cpu, err);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003597 }
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003598
3599 if (list_empty(&pktgen_threads)) {
3600 printk("pktgen: ERROR: Initialization failed for all threads\n");
3601 unregister_netdevice_notifier(&pktgen_notifier_block);
3602 remove_proc_entry(PGCTRL, pg_proc_dir);
3603 proc_net_remove(PG_PROC_DIR);
3604 return -ENODEV;
3605 }
3606
Luiz Capitulino222f1802006-03-20 22:16:13 -08003607 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608}
3609
3610static void __exit pg_cleanup(void)
3611{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003612 struct pktgen_thread *t;
3613 struct list_head *q, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 wait_queue_head_t queue;
3615 init_waitqueue_head(&queue);
3616
Luiz Capitulino222f1802006-03-20 22:16:13 -08003617 /* Stop all interfaces & threads */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003619 list_for_each_safe(q, n, &pktgen_threads) {
3620 t = list_entry(q, struct pktgen_thread, th_list);
David S. Milleree74baa2007-01-01 20:51:53 -08003621 kthread_stop(t->tsk);
3622 kfree(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003623 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624
Luiz Capitulino222f1802006-03-20 22:16:13 -08003625 /* Un-register us from receiving netdevice events */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626 unregister_netdevice_notifier(&pktgen_notifier_block);
3627
Luiz Capitulino222f1802006-03-20 22:16:13 -08003628 /* Clean up proc file system */
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003629 remove_proc_entry(PGCTRL, pg_proc_dir);
3630 proc_net_remove(PG_PROC_DIR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631}
3632
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633module_init(pg_init);
3634module_exit(pg_cleanup);
3635
3636MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se");
3637MODULE_DESCRIPTION("Packet Generator tool");
3638MODULE_LICENSE("GPL");
3639module_param(pg_count_d, int, 0);
3640module_param(pg_delay_d, int, 0);
3641module_param(pg_clone_skb_d, int, 0);
3642module_param(debug, int, 0);