blob: 733d86d0a4fb7f3b97a2ae23291c34281fe4f2c8 [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.
18 * The tool is implemented as a linux module. Parameters are output
19 * device, delay (to hard_xmit), number of packets, and whether
20 * to use multiple SKBs or just the same one.
21 * pktgen uses the installed interface's output routine.
22 *
23 * Additional hacking by:
24 *
25 * Jens.Laas@data.slu.se
26 * Improved by ANK. 010120.
27 * Improved by ANK even more. 010212.
28 * MAC address typo fixed. 010417 --ro
29 * Integrated. 020301 --DaveM
30 * Added multiskb option 020301 --DaveM
31 * Scaling of results. 020417--sigurdur@linpro.no
32 * Significant re-work of the module:
33 * * Convert to threaded model to more efficiently be able to transmit
34 * and receive on multiple interfaces at once.
35 * * Converted many counters to __u64 to allow longer runs.
36 * * Allow configuration of ranges, like min/max IP address, MACs,
37 * and UDP-ports, for both source and destination, and can
38 * set to use a random distribution or sequentially walk the range.
39 * * Can now change most values after starting.
40 * * Place 12-byte packet in UDP payload with magic number,
41 * sequence number, and timestamp.
42 * * Add receiver code that detects dropped pkts, re-ordered pkts, and
43 * latencies (with micro-second) precision.
44 * * Add IOCTL interface to easily get counters & configuration.
45 * --Ben Greear <greearb@candelatech.com>
46 *
47 * Renamed multiskb to clone_skb and cleaned up sending core for two distinct
48 * skb modes. A clone_skb=0 mode for Ben "ranges" work and a clone_skb != 0
49 * as a "fastpath" with a configurable number of clones after alloc's.
50 * clone_skb=0 means all packets are allocated this also means ranges time
51 * stamps etc can be used. clone_skb=100 means 1 malloc is followed by 100
52 * clones.
53 *
54 * Also moved to /proc/net/pktgen/
55 * --ro
56 *
57 * Sept 10: Fixed threading/locking. Lots of bone-headed and more clever
58 * mistakes. Also merged in DaveM's patch in the -pre6 patch.
59 * --Ben Greear <greearb@candelatech.com>
60 *
61 * Integrated to 2.5.x 021029 --Lucio Maciel (luciomaciel@zipmail.com.br)
62 *
63 *
64 * 021124 Finished major redesign and rewrite for new functionality.
65 * See Documentation/networking/pktgen.txt for how to use this.
66 *
67 * The new operation:
68 * For each CPU one thread/process is created at start. This process checks
69 * for running devices in the if_list and sends packets until count is 0 it
70 * also the thread checks the thread->control which is used for inter-process
71 * communication. controlling process "posts" operations to the threads this
72 * way. The if_lock should be possible to remove when add/rem_device is merged
73 * into this too.
74 *
75 * By design there should only be *one* "controlling" process. In practice
76 * multiple write accesses gives unpredictable result. Understood by "write"
77 * 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 *
80 * Note when adding devices to a specific CPU there good idea to also assign
81 * /proc/irq/XX/smp_affinity so TX-interrupts gets bound to the same CPU.
82 * --ro
83 *
84 * Fix refcount off by one if first packet fails, potential null deref,
85 * memleak 030710- KJP
86 *
87 * First "ranges" functionality for ipv6 030726 --ro
88 *
89 * Included flow support. 030802 ANK.
90 *
91 * Fixed unaligned access on IA-64 Grant Grundler <grundler@parisc-linux.org>
92 *
93 * Remove if fix from added Harald Welte <laforge@netfilter.org> 040419
94 * ia64 compilation fix from Aron Griffis <aron@hp.com> 040604
95 *
96 * New xmit() return, do_div and misc clean up by Stephen Hemminger
97 * <shemminger@osdl.org> 040923
98 *
Stephen Hemmingerb4099fa2005-10-14 15:32:22 -070099 * 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 *
104 * Corrections from Nikolai Malykh (nmalykh@bilim.com)
105 * Removed unused flags F_SET_SRCMAC & F_SET_SRCIP 041230
106 *
107 * interruptible_sleep_on_timeout() replaced Nishanth Aravamudan <nacc@us.ibm.com>
108 * 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>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132#include <linux/delay.h>
133#include <linux/timer.h>
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -0800134#include <linux/list.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135#include <linux/init.h>
136#include <linux/skbuff.h>
137#include <linux/netdevice.h>
138#include <linux/inet.h>
139#include <linux/inetdevice.h>
140#include <linux/rtnetlink.h>
141#include <linux/if_arp.h>
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700142#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143#include <linux/in.h>
144#include <linux/ip.h>
145#include <linux/ipv6.h>
146#include <linux/udp.h>
147#include <linux/proc_fs.h>
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700148#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149#include <linux/wait.h>
Kris Katterjohnf404e9a2006-01-17 13:04:57 -0800150#include <linux/etherdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151#include <net/checksum.h>
152#include <net/ipv6.h>
153#include <net/addrconf.h>
154#include <asm/byteorder.h>
155#include <linux/rcupdate.h>
156#include <asm/bitops.h>
157#include <asm/io.h>
158#include <asm/dma.h>
159#include <asm/uaccess.h>
Luiz Capitulino222f1802006-03-20 22:16:13 -0800160#include <asm/div64.h> /* do_div */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161#include <asm/timex.h>
162
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700163#define VERSION "pktgen v2.68: Packet Generator for packet performance testing.\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165/* #define PG_DEBUG(a) a */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800166#define PG_DEBUG(a)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168/* The buckets are exponential in 'width' */
169#define LAT_BUCKETS_MAX 32
170#define IP_NAME_SZ 32
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800171#define MAX_MPLS_LABELS 16 /* This is the max label stack depth */
172#define MPLS_STACK_BOTTOM __constant_htonl(0x00000100)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174/* Device flag bits */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800175#define F_IPSRC_RND (1<<0) /* IP-Src Random */
176#define F_IPDST_RND (1<<1) /* IP-Dst Random */
177#define F_UDPSRC_RND (1<<2) /* UDP-Src Random */
178#define F_UDPDST_RND (1<<3) /* UDP-Dst Random */
179#define F_MACSRC_RND (1<<4) /* MAC-Src Random */
180#define F_MACDST_RND (1<<5) /* MAC-Dst Random */
181#define F_TXSIZE_RND (1<<6) /* Transmit size is random */
182#define F_IPV6 (1<<7) /* Interface in IPV6 Mode */
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800183#define F_MPLS_RND (1<<8) /* Random MPLS labels */
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700184#define F_VID_RND (1<<9) /* Random VLAN ID */
185#define F_SVID_RND (1<<10) /* Random SVLAN ID */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187/* Thread control flag bits */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800188#define T_TERMINATE (1<<0)
189#define T_STOP (1<<1) /* Stop run */
190#define T_RUN (1<<2) /* Start run */
191#define T_REMDEVALL (1<<3) /* Remove all devs */
192#define T_REMDEV (1<<4) /* Remove one dev */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194/* If lock -- can be removed after some work */
195#define if_lock(t) spin_lock(&(t->if_lock));
196#define if_unlock(t) spin_unlock(&(t->if_lock));
197
198/* Used to help with determining the pkts on receive */
199#define PKTGEN_MAGIC 0xbe9be955
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700200#define PG_PROC_DIR "pktgen"
201#define PGCTRL "pgctrl"
202static struct proc_dir_entry *pg_proc_dir = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
204#define MAX_CFLOWS 65536
205
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700206#define VLAN_TAG_SIZE(x) ((x)->vlan_id == 0xffff ? 0 : 4)
207#define SVLAN_TAG_SIZE(x) ((x)->svlan_id == 0xffff ? 0 : 4)
208
Luiz Capitulino222f1802006-03-20 22:16:13 -0800209struct flow_state {
210 __u32 cur_daddr;
211 int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212};
213
214struct pktgen_dev {
215
216 /*
217 * Try to keep frequent/infrequent used vars. separated.
218 */
219
Luiz Capitulino222f1802006-03-20 22:16:13 -0800220 char ifname[IFNAMSIZ];
221 char result[512];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Luiz Capitulino222f1802006-03-20 22:16:13 -0800223 struct pktgen_thread *pg_thread; /* the owner */
Luiz Capitulinoc26a8012006-03-20 22:18:16 -0800224 struct list_head list; /* Used for chaining in the thread's run-queue */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Luiz Capitulino222f1802006-03-20 22:16:13 -0800226 int running; /* if this changes to false, the test will stop */
227
228 /* If min != max, then we will either do a linear iteration, or
229 * we will do a random selection from within the range.
230 */
231 __u32 flags;
Arthur Kepner95ed63f2006-03-20 21:26:56 -0800232 int removal_mark; /* non-zero => the device is marked for
233 * removal by worker thread */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Luiz Capitulino222f1802006-03-20 22:16:13 -0800235 int min_pkt_size; /* = ETH_ZLEN; */
236 int max_pkt_size; /* = ETH_ZLEN; */
237 int nfrags;
238 __u32 delay_us; /* Default delay */
239 __u32 delay_ns;
240 __u64 count; /* Default No packets to send */
241 __u64 sofar; /* How many pkts we've sent so far */
242 __u64 tx_bytes; /* How many bytes we've transmitted */
243 __u64 errors; /* Errors when trying to transmit, pkts will be re-sent */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Luiz Capitulino222f1802006-03-20 22:16:13 -0800245 /* runtime counters relating to clone_skb */
246 __u64 next_tx_us; /* timestamp of when to tx next */
247 __u32 next_tx_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Luiz Capitulino222f1802006-03-20 22:16:13 -0800249 __u64 allocated_skbs;
250 __u32 clone_count;
251 int last_ok; /* Was last skb sent?
252 * Or a failed transmit of some sort? This will keep
253 * sequence numbers in order, for example.
254 */
255 __u64 started_at; /* micro-seconds */
256 __u64 stopped_at; /* micro-seconds */
257 __u64 idle_acc; /* micro-seconds */
258 __u32 seq_num;
259
260 int clone_skb; /* Use multiple SKBs during packet gen. If this number
261 * is greater than 1, then that many copies of the same
262 * packet will be sent before a new packet is allocated.
263 * For instance, if you want to send 1024 identical packets
264 * before creating a new packet, set clone_skb to 1024.
265 */
266
267 char dst_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
268 char dst_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
269 char src_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
270 char src_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
271
272 struct in6_addr in6_saddr;
273 struct in6_addr in6_daddr;
274 struct in6_addr cur_in6_daddr;
275 struct in6_addr cur_in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 /* For ranges */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800277 struct in6_addr min_in6_daddr;
278 struct in6_addr max_in6_daddr;
279 struct in6_addr min_in6_saddr;
280 struct in6_addr max_in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Luiz Capitulino222f1802006-03-20 22:16:13 -0800282 /* If we're doing ranges, random or incremental, then this
283 * defines the min/max for those ranges.
284 */
285 __u32 saddr_min; /* inclusive, source IP address */
286 __u32 saddr_max; /* exclusive, source IP address */
287 __u32 daddr_min; /* inclusive, dest IP address */
288 __u32 daddr_max; /* exclusive, dest IP address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Luiz Capitulino222f1802006-03-20 22:16:13 -0800290 __u16 udp_src_min; /* inclusive, source UDP port */
291 __u16 udp_src_max; /* exclusive, source UDP port */
292 __u16 udp_dst_min; /* inclusive, dest UDP port */
293 __u16 udp_dst_max; /* exclusive, dest UDP port */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700295 /* DSCP + ECN */
296 __u8 tos; /* six most significant bits of (former) IPv4 TOS are for dscp codepoint */
297 __u8 traffic_class; /* ditto for the (former) Traffic Class in IPv6 (see RFC 3260, sec. 4) */
298
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800299 /* MPLS */
300 unsigned nr_labels; /* Depth of stack, 0 = no MPLS */
301 __be32 labels[MAX_MPLS_LABELS];
302
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700303 /* VLAN/SVLAN (802.1Q/Q-in-Q) */
304 __u8 vlan_p;
305 __u8 vlan_cfi;
306 __u16 vlan_id; /* 0xffff means no vlan tag */
307
308 __u8 svlan_p;
309 __u8 svlan_cfi;
310 __u16 svlan_id; /* 0xffff means no svlan tag */
311
Luiz Capitulino222f1802006-03-20 22:16:13 -0800312 __u32 src_mac_count; /* How many MACs to iterate through */
313 __u32 dst_mac_count; /* How many MACs to iterate through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Luiz Capitulino222f1802006-03-20 22:16:13 -0800315 unsigned char dst_mac[ETH_ALEN];
316 unsigned char src_mac[ETH_ALEN];
317
318 __u32 cur_dst_mac_offset;
319 __u32 cur_src_mac_offset;
320 __u32 cur_saddr;
321 __u32 cur_daddr;
322 __u16 cur_udp_dst;
323 __u16 cur_udp_src;
324 __u32 cur_pkt_size;
325
326 __u8 hh[14];
327 /* = {
328 0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
329
330 We fill in SRC address later
331 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
332 0x08, 0x00
333 };
334 */
335 __u16 pad; /* pad out the hh struct to an even 16 bytes */
336
337 struct sk_buff *skb; /* skb we are to transmit next, mainly used for when we
338 * are transmitting the same one multiple times
339 */
340 struct net_device *odev; /* The out-going device. Note that the device should
341 * have it's pg_info pointer pointing back to this
342 * device. This will be set when the user specifies
343 * the out-going device name (not when the inject is
344 * started as it used to do.)
345 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 struct flow_state *flows;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800347 unsigned cflows; /* Concurrent flows (config) */
348 unsigned lflow; /* Flow length (config) */
349 unsigned nflows; /* accumulated flows (stats) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350};
351
352struct pktgen_hdr {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800353 __u32 pgh_magic;
354 __u32 seq_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 __u32 tv_sec;
356 __u32 tv_usec;
357};
358
359struct pktgen_thread {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800360 spinlock_t if_lock;
Luiz Capitulinoc26a8012006-03-20 22:18:16 -0800361 struct list_head if_list; /* All device here */
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -0800362 struct list_head th_list;
363 int removed;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800364 char name[32];
365 char result[512];
366 u32 max_before_softirq; /* We'll call do_softirq to prevent starvation. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Luiz Capitulino222f1802006-03-20 22:16:13 -0800368 /* Field for thread to receive "posted" events terminate, stop ifs etc. */
369
370 u32 control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 int pid;
372 int cpu;
373
Luiz Capitulino222f1802006-03-20 22:16:13 -0800374 wait_queue_head_t queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375};
376
377#define REMOVE 1
378#define FIND 0
379
380/* This code works around the fact that do_div cannot handle two 64-bit
381 numbers, and regular 64-bit division doesn't work on x86 kernels.
382 --Ben
383*/
384
385#define PG_DIV 0
386
387/* This was emailed to LMKL by: Chris Caputo <ccaputo@alt.net>
388 * Function copied/adapted/optimized from:
389 *
390 * nemesis.sourceforge.net/browse/lib/static/intmath/ix86/intmath.c.html
391 *
392 * Copyright 1994, University of Cambridge Computer Laboratory
393 * All Rights Reserved.
394 *
395 */
Jesper Juhl77933d72005-07-27 11:46:09 -0700396static inline s64 divremdi3(s64 x, s64 y, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800398 u64 a = (x < 0) ? -x : x;
399 u64 b = (y < 0) ? -y : y;
400 u64 res = 0, d = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Luiz Capitulino222f1802006-03-20 22:16:13 -0800402 if (b > 0) {
403 while (b < a) {
404 b <<= 1;
405 d <<= 1;
406 }
407 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Luiz Capitulino222f1802006-03-20 22:16:13 -0800409 do {
410 if (a >= b) {
411 a -= b;
412 res += d;
413 }
414 b >>= 1;
415 d >>= 1;
416 }
417 while (d);
418
419 if (PG_DIV == type) {
420 return (((x ^ y) & (1ll << 63)) == 0) ? res : -(s64) res;
421 } else {
422 return ((x & (1ll << 63)) == 0) ? a : -(s64) a;
423 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424}
425
426/* End of hacks to deal with 64-bit math on x86 */
427
Stephen Hemmingerb4099fa2005-10-14 15:32:22 -0700428/** Convert to milliseconds */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800429static inline __u64 tv_to_ms(const struct timeval *tv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800431 __u64 ms = tv->tv_usec / 1000;
432 ms += (__u64) tv->tv_sec * (__u64) 1000;
433 return ms;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434}
435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436/** Convert to micro-seconds */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800437static inline __u64 tv_to_us(const struct timeval *tv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800439 __u64 us = tv->tv_usec;
440 us += (__u64) tv->tv_sec * (__u64) 1000000;
441 return us;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442}
443
Luiz Capitulino222f1802006-03-20 22:16:13 -0800444static inline __u64 pg_div(__u64 n, __u32 base)
445{
446 __u64 tmp = n;
447 do_div(tmp, base);
448 /* printk("pktgen: pg_div, n: %llu base: %d rv: %llu\n",
449 n, base, tmp); */
450 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451}
452
Luiz Capitulino222f1802006-03-20 22:16:13 -0800453static inline __u64 pg_div64(__u64 n, __u64 base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800455 __u64 tmp = n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456/*
Stephen Hemmingerb4099fa2005-10-14 15:32:22 -0700457 * How do we know if the architecture we are running on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 * supports division with 64 bit base?
459 *
460 */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800461#if defined(__sparc_v9__) || defined(__powerpc64__) || defined(__alpha__) || defined(__x86_64__) || defined(__ia64__)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Luiz Capitulino222f1802006-03-20 22:16:13 -0800463 do_div(tmp, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464#else
Luiz Capitulino222f1802006-03-20 22:16:13 -0800465 tmp = divremdi3(n, base, PG_DIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466#endif
Luiz Capitulino222f1802006-03-20 22:16:13 -0800467 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468}
469
470static inline u32 pktgen_random(void)
471{
472#if 0
473 __u32 n;
474 get_random_bytes(&n, 4);
475 return n;
476#else
477 return net_random();
478#endif
479}
480
Luiz Capitulino222f1802006-03-20 22:16:13 -0800481static inline __u64 getCurMs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800483 struct timeval tv;
484 do_gettimeofday(&tv);
485 return tv_to_ms(&tv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486}
487
Luiz Capitulino222f1802006-03-20 22:16:13 -0800488static inline __u64 getCurUs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800490 struct timeval tv;
491 do_gettimeofday(&tv);
492 return tv_to_us(&tv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493}
494
Luiz Capitulino222f1802006-03-20 22:16:13 -0800495static inline __u64 tv_diff(const struct timeval *a, const struct timeval *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800497 return tv_to_us(a) - tv_to_us(b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498}
499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500/* old include end */
501
502static char version[] __initdata = VERSION;
503
Luiz Capitulino222f1802006-03-20 22:16:13 -0800504static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *i);
505static int pktgen_add_device(struct pktgen_thread *t, const char *ifname);
506static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
507 const char *ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508static int pktgen_device_event(struct notifier_block *, unsigned long, void *);
509static void pktgen_run_all_threads(void);
510static void pktgen_stop_all_threads_ifs(void);
511static int pktgen_stop_device(struct pktgen_dev *pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800512static void pktgen_stop(struct pktgen_thread *t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513static void pktgen_clear_counters(struct pktgen_dev *pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800514static int pktgen_mark_device(const char *ifname);
515static unsigned int scan_ip6(const char *s, char ip[16]);
516static unsigned int fmt_ip6(char *s, const char ip[16]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
518/* Module parameters, defaults. */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800519static int pg_count_d = 1000; /* 1000 pkts by default */
Jaco Kroonf34fbb92005-12-22 12:51:46 -0800520static int pg_delay_d;
521static int pg_clone_skb_d;
522static int debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Luiz Capitulino222fa072006-03-20 22:24:27 -0800524static DEFINE_MUTEX(pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -0800525static LIST_HEAD(pktgen_threads);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527static struct notifier_block pktgen_notifier_block = {
528 .notifier_call = pktgen_device_event,
529};
530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531/*
532 * /proc handling functions
533 *
534 */
535
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700536static int pgctrl_show(struct seq_file *seq, void *v)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800537{
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700538 seq_puts(seq, VERSION);
539 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540}
541
Luiz Capitulino222f1802006-03-20 22:16:13 -0800542static ssize_t pgctrl_write(struct file *file, const char __user * buf,
543 size_t count, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 int err = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700546 char data[128];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Luiz Capitulino222f1802006-03-20 22:16:13 -0800548 if (!capable(CAP_NET_ADMIN)) {
549 err = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 goto out;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800551 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700553 if (count > sizeof(data))
554 count = sizeof(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 if (copy_from_user(data, buf, count)) {
Stephen Hemmingerb4099fa2005-10-14 15:32:22 -0700557 err = -EFAULT;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700558 goto out;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800559 }
560 data[count - 1] = 0; /* Make string */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Luiz Capitulino222f1802006-03-20 22:16:13 -0800562 if (!strcmp(data, "stop"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 pktgen_stop_all_threads_ifs();
564
Luiz Capitulino222f1802006-03-20 22:16:13 -0800565 else if (!strcmp(data, "start"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 pktgen_run_all_threads();
567
Luiz Capitulino222f1802006-03-20 22:16:13 -0800568 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 printk("pktgen: Unknown command: %s\n", data);
570
571 err = count;
572
Luiz Capitulino222f1802006-03-20 22:16:13 -0800573out:
574 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575}
576
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700577static int pgctrl_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700579 return single_open(file, pgctrl_show, PDE(inode)->data);
580}
581
582static struct file_operations pktgen_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800583 .owner = THIS_MODULE,
584 .open = pgctrl_open,
585 .read = seq_read,
586 .llseek = seq_lseek,
587 .write = pgctrl_write,
588 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700589};
590
591static int pktgen_if_show(struct seq_file *seq, void *v)
592{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 int i;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800594 struct pktgen_dev *pkt_dev = seq->private;
595 __u64 sa;
596 __u64 stopped;
597 __u64 now = getCurUs();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Luiz Capitulino222f1802006-03-20 22:16:13 -0800599 seq_printf(seq,
600 "Params: count %llu min_pkt_size: %u max_pkt_size: %u\n",
601 (unsigned long long)pkt_dev->count, pkt_dev->min_pkt_size,
602 pkt_dev->max_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Luiz Capitulino222f1802006-03-20 22:16:13 -0800604 seq_printf(seq,
605 " frags: %d delay: %u clone_skb: %d ifname: %s\n",
606 pkt_dev->nfrags,
607 1000 * pkt_dev->delay_us + pkt_dev->delay_ns,
608 pkt_dev->clone_skb, pkt_dev->ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Luiz Capitulino222f1802006-03-20 22:16:13 -0800610 seq_printf(seq, " flows: %u flowlen: %u\n", pkt_dev->cflows,
611 pkt_dev->lflow);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Luiz Capitulino222f1802006-03-20 22:16:13 -0800613 if (pkt_dev->flags & F_IPV6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 char b1[128], b2[128], b3[128];
Luiz Capitulino222f1802006-03-20 22:16:13 -0800615 fmt_ip6(b1, pkt_dev->in6_saddr.s6_addr);
616 fmt_ip6(b2, pkt_dev->min_in6_saddr.s6_addr);
617 fmt_ip6(b3, pkt_dev->max_in6_saddr.s6_addr);
618 seq_printf(seq,
619 " saddr: %s min_saddr: %s max_saddr: %s\n", b1,
620 b2, b3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Luiz Capitulino222f1802006-03-20 22:16:13 -0800622 fmt_ip6(b1, pkt_dev->in6_daddr.s6_addr);
623 fmt_ip6(b2, pkt_dev->min_in6_daddr.s6_addr);
624 fmt_ip6(b3, pkt_dev->max_in6_daddr.s6_addr);
625 seq_printf(seq,
626 " daddr: %s min_daddr: %s max_daddr: %s\n", b1,
627 b2, b3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Luiz Capitulino222f1802006-03-20 22:16:13 -0800629 } else
630 seq_printf(seq,
631 " dst_min: %s dst_max: %s\n src_min: %s src_max: %s\n",
632 pkt_dev->dst_min, pkt_dev->dst_max, pkt_dev->src_min,
633 pkt_dev->src_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700635 seq_puts(seq, " src_mac: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Kris Katterjohnf404e9a2006-01-17 13:04:57 -0800637 if (is_zero_ether_addr(pkt_dev->src_mac))
Luiz Capitulino222f1802006-03-20 22:16:13 -0800638 for (i = 0; i < 6; i++)
639 seq_printf(seq, "%02X%s", pkt_dev->odev->dev_addr[i],
640 i == 5 ? " " : ":");
641 else
642 for (i = 0; i < 6; i++)
643 seq_printf(seq, "%02X%s", pkt_dev->src_mac[i],
644 i == 5 ? " " : ":");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Luiz Capitulino222f1802006-03-20 22:16:13 -0800646 seq_printf(seq, "dst_mac: ");
647 for (i = 0; i < 6; i++)
648 seq_printf(seq, "%02X%s", pkt_dev->dst_mac[i],
649 i == 5 ? "\n" : ":");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Luiz Capitulino222f1802006-03-20 22:16:13 -0800651 seq_printf(seq,
652 " udp_src_min: %d udp_src_max: %d udp_dst_min: %d udp_dst_max: %d\n",
653 pkt_dev->udp_src_min, pkt_dev->udp_src_max,
654 pkt_dev->udp_dst_min, pkt_dev->udp_dst_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
Luiz Capitulino222f1802006-03-20 22:16:13 -0800656 seq_printf(seq,
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800657 " src_mac_count: %d dst_mac_count: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700658 pkt_dev->src_mac_count, pkt_dev->dst_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800660 if (pkt_dev->nr_labels) {
661 unsigned i;
662 seq_printf(seq, " mpls: ");
663 for(i = 0; i < pkt_dev->nr_labels; i++)
664 seq_printf(seq, "%08x%s", ntohl(pkt_dev->labels[i]),
665 i == pkt_dev->nr_labels-1 ? "\n" : ", ");
666 }
667
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700668 if (pkt_dev->vlan_id != 0xffff) {
669 seq_printf(seq, " vlan_id: %u vlan_p: %u vlan_cfi: %u\n",
670 pkt_dev->vlan_id, pkt_dev->vlan_p, pkt_dev->vlan_cfi);
671 }
672
673 if (pkt_dev->svlan_id != 0xffff) {
674 seq_printf(seq, " svlan_id: %u vlan_p: %u vlan_cfi: %u\n",
675 pkt_dev->svlan_id, pkt_dev->svlan_p, pkt_dev->svlan_cfi);
676 }
677
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700678 if (pkt_dev->tos) {
679 seq_printf(seq, " tos: 0x%02x\n", pkt_dev->tos);
680 }
681
682 if (pkt_dev->traffic_class) {
683 seq_printf(seq, " traffic_class: 0x%02x\n", pkt_dev->traffic_class);
684 }
685
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800686 seq_printf(seq, " Flags: ");
687
Luiz Capitulino222f1802006-03-20 22:16:13 -0800688 if (pkt_dev->flags & F_IPV6)
689 seq_printf(seq, "IPV6 ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Luiz Capitulino222f1802006-03-20 22:16:13 -0800691 if (pkt_dev->flags & F_IPSRC_RND)
692 seq_printf(seq, "IPSRC_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Luiz Capitulino222f1802006-03-20 22:16:13 -0800694 if (pkt_dev->flags & F_IPDST_RND)
695 seq_printf(seq, "IPDST_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Luiz Capitulino222f1802006-03-20 22:16:13 -0800697 if (pkt_dev->flags & F_TXSIZE_RND)
698 seq_printf(seq, "TXSIZE_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Luiz Capitulino222f1802006-03-20 22:16:13 -0800700 if (pkt_dev->flags & F_UDPSRC_RND)
701 seq_printf(seq, "UDPSRC_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Luiz Capitulino222f1802006-03-20 22:16:13 -0800703 if (pkt_dev->flags & F_UDPDST_RND)
704 seq_printf(seq, "UDPDST_RND ");
705
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800706 if (pkt_dev->flags & F_MPLS_RND)
707 seq_printf(seq, "MPLS_RND ");
708
Luiz Capitulino222f1802006-03-20 22:16:13 -0800709 if (pkt_dev->flags & F_MACSRC_RND)
710 seq_printf(seq, "MACSRC_RND ");
711
712 if (pkt_dev->flags & F_MACDST_RND)
713 seq_printf(seq, "MACDST_RND ");
714
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700715 if (pkt_dev->flags & F_VID_RND)
716 seq_printf(seq, "VID_RND ");
717
718 if (pkt_dev->flags & F_SVID_RND)
719 seq_printf(seq, "SVID_RND ");
720
Luiz Capitulino222f1802006-03-20 22:16:13 -0800721 seq_puts(seq, "\n");
722
723 sa = pkt_dev->started_at;
724 stopped = pkt_dev->stopped_at;
725 if (pkt_dev->running)
726 stopped = now; /* not really stopped, more like last-running-at */
727
728 seq_printf(seq,
729 "Current:\n pkts-sofar: %llu errors: %llu\n started: %lluus stopped: %lluus idle: %lluus\n",
730 (unsigned long long)pkt_dev->sofar,
731 (unsigned long long)pkt_dev->errors, (unsigned long long)sa,
732 (unsigned long long)stopped,
733 (unsigned long long)pkt_dev->idle_acc);
734
735 seq_printf(seq,
736 " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700737 pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset,
738 pkt_dev->cur_src_mac_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
Luiz Capitulino222f1802006-03-20 22:16:13 -0800740 if (pkt_dev->flags & F_IPV6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 char b1[128], b2[128];
Luiz Capitulino222f1802006-03-20 22:16:13 -0800742 fmt_ip6(b1, pkt_dev->cur_in6_daddr.s6_addr);
743 fmt_ip6(b2, pkt_dev->cur_in6_saddr.s6_addr);
744 seq_printf(seq, " cur_saddr: %s cur_daddr: %s\n", b2, b1);
745 } else
746 seq_printf(seq, " cur_saddr: 0x%x cur_daddr: 0x%x\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700747 pkt_dev->cur_saddr, pkt_dev->cur_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
Luiz Capitulino222f1802006-03-20 22:16:13 -0800749 seq_printf(seq, " cur_udp_dst: %d cur_udp_src: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700750 pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
Luiz Capitulino222f1802006-03-20 22:16:13 -0800752 seq_printf(seq, " flows: %u\n", pkt_dev->nflows);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
754 if (pkt_dev->result[0])
Luiz Capitulino222f1802006-03-20 22:16:13 -0800755 seq_printf(seq, "Result: %s\n", pkt_dev->result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 else
Luiz Capitulino222f1802006-03-20 22:16:13 -0800757 seq_printf(seq, "Result: Idle\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700759 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760}
761
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800762
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700763static int hex32_arg(const char __user *user_buffer, unsigned long maxlen, __u32 *num)
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800764{
765 int i = 0;
766 *num = 0;
767
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700768 for(; i < maxlen; i++) {
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800769 char c;
770 *num <<= 4;
771 if (get_user(c, &user_buffer[i]))
772 return -EFAULT;
773 if ((c >= '0') && (c <= '9'))
774 *num |= c - '0';
775 else if ((c >= 'a') && (c <= 'f'))
776 *num |= c - 'a' + 10;
777 else if ((c >= 'A') && (c <= 'F'))
778 *num |= c - 'A' + 10;
779 else
780 break;
781 }
782 return i;
783}
784
Luiz Capitulino222f1802006-03-20 22:16:13 -0800785static int count_trail_chars(const char __user * user_buffer,
786 unsigned int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
788 int i;
789
790 for (i = 0; i < maxlen; i++) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800791 char c;
792 if (get_user(c, &user_buffer[i]))
793 return -EFAULT;
794 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 case '\"':
796 case '\n':
797 case '\r':
798 case '\t':
799 case ' ':
800 case '=':
801 break;
802 default:
803 goto done;
804 };
805 }
806done:
807 return i;
808}
809
Luiz Capitulino222f1802006-03-20 22:16:13 -0800810static unsigned long num_arg(const char __user * user_buffer,
811 unsigned long maxlen, unsigned long *num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812{
813 int i = 0;
814 *num = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800815
816 for (; i < maxlen; i++) {
817 char c;
818 if (get_user(c, &user_buffer[i]))
819 return -EFAULT;
820 if ((c >= '0') && (c <= '9')) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 *num *= 10;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800822 *num += c - '0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 } else
824 break;
825 }
826 return i;
827}
828
Luiz Capitulino222f1802006-03-20 22:16:13 -0800829static int strn_len(const char __user * user_buffer, unsigned int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830{
831 int i = 0;
832
Luiz Capitulino222f1802006-03-20 22:16:13 -0800833 for (; i < maxlen; i++) {
834 char c;
835 if (get_user(c, &user_buffer[i]))
836 return -EFAULT;
837 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 case '\"':
839 case '\n':
840 case '\r':
841 case '\t':
842 case ' ':
843 goto done_str;
844 break;
845 default:
846 break;
847 };
848 }
849done_str:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 return i;
851}
852
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800853static ssize_t get_labels(const char __user *buffer, struct pktgen_dev *pkt_dev)
854{
855 unsigned n = 0;
856 char c;
857 ssize_t i = 0;
858 int len;
859
860 pkt_dev->nr_labels = 0;
861 do {
862 __u32 tmp;
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700863 len = hex32_arg(&buffer[i], 8, &tmp);
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800864 if (len <= 0)
865 return len;
866 pkt_dev->labels[n] = htonl(tmp);
867 if (pkt_dev->labels[n] & MPLS_STACK_BOTTOM)
868 pkt_dev->flags |= F_MPLS_RND;
869 i += len;
870 if (get_user(c, &buffer[i]))
871 return -EFAULT;
872 i++;
873 n++;
874 if (n >= MAX_MPLS_LABELS)
875 return -E2BIG;
876 } while(c == ',');
877
878 pkt_dev->nr_labels = n;
879 return i;
880}
881
Luiz Capitulino222f1802006-03-20 22:16:13 -0800882static ssize_t pktgen_if_write(struct file *file,
883 const char __user * user_buffer, size_t count,
884 loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800886 struct seq_file *seq = (struct seq_file *)file->private_data;
887 struct pktgen_dev *pkt_dev = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 int i = 0, max, len;
889 char name[16], valstr[32];
890 unsigned long value = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800891 char *pg_result = NULL;
892 int tmp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 char buf[128];
Luiz Capitulino222f1802006-03-20 22:16:13 -0800894
895 pg_result = &(pkt_dev->result[0]);
896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 if (count < 1) {
898 printk("pktgen: wrong command format\n");
899 return -EINVAL;
900 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800901
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 max = count - i;
903 tmp = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800904 if (tmp < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 printk("pktgen: illegal format\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -0800906 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800908 i += tmp;
909
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 /* Read variable name */
911
912 len = strn_len(&user_buffer[i], sizeof(name) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800913 if (len < 0) {
914 return len;
915 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 memset(name, 0, sizeof(name));
Luiz Capitulino222f1802006-03-20 22:16:13 -0800917 if (copy_from_user(name, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 return -EFAULT;
919 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800920
921 max = count - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 len = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800923 if (len < 0)
924 return len;
925
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 i += len;
927
928 if (debug) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800929 char tb[count + 1];
930 if (copy_from_user(tb, user_buffer, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800932 tb[count] = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700933 printk("pktgen: %s,%lu buffer -:%s:-\n", name,
Luiz Capitulino222f1802006-03-20 22:16:13 -0800934 (unsigned long)count, tb);
935 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
937 if (!strcmp(name, "min_pkt_size")) {
938 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800939 if (len < 0) {
940 return len;
941 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800943 if (value < 14 + 20 + 8)
944 value = 14 + 20 + 8;
945 if (value != pkt_dev->min_pkt_size) {
946 pkt_dev->min_pkt_size = value;
947 pkt_dev->cur_pkt_size = value;
948 }
949 sprintf(pg_result, "OK: min_pkt_size=%u",
950 pkt_dev->min_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 return count;
952 }
953
Luiz Capitulino222f1802006-03-20 22:16:13 -0800954 if (!strcmp(name, "max_pkt_size")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800956 if (len < 0) {
957 return len;
958 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800960 if (value < 14 + 20 + 8)
961 value = 14 + 20 + 8;
962 if (value != pkt_dev->max_pkt_size) {
963 pkt_dev->max_pkt_size = value;
964 pkt_dev->cur_pkt_size = value;
965 }
966 sprintf(pg_result, "OK: max_pkt_size=%u",
967 pkt_dev->max_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 return count;
969 }
970
Luiz Capitulino222f1802006-03-20 22:16:13 -0800971 /* Shortcut for min = max */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
973 if (!strcmp(name, "pkt_size")) {
974 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800975 if (len < 0) {
976 return len;
977 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800979 if (value < 14 + 20 + 8)
980 value = 14 + 20 + 8;
981 if (value != pkt_dev->min_pkt_size) {
982 pkt_dev->min_pkt_size = value;
983 pkt_dev->max_pkt_size = value;
984 pkt_dev->cur_pkt_size = value;
985 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 sprintf(pg_result, "OK: pkt_size=%u", pkt_dev->min_pkt_size);
987 return count;
988 }
989
Luiz Capitulino222f1802006-03-20 22:16:13 -0800990 if (!strcmp(name, "debug")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800992 if (len < 0) {
993 return len;
994 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800996 debug = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 sprintf(pg_result, "OK: debug=%u", debug);
998 return count;
999 }
1000
Luiz Capitulino222f1802006-03-20 22:16:13 -08001001 if (!strcmp(name, "frags")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001003 if (len < 0) {
1004 return len;
1005 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 i += len;
1007 pkt_dev->nfrags = value;
1008 sprintf(pg_result, "OK: frags=%u", pkt_dev->nfrags);
1009 return count;
1010 }
1011 if (!strcmp(name, "delay")) {
1012 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001013 if (len < 0) {
1014 return len;
1015 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 i += len;
1017 if (value == 0x7FFFFFFF) {
1018 pkt_dev->delay_us = 0x7FFFFFFF;
1019 pkt_dev->delay_ns = 0;
1020 } else {
1021 pkt_dev->delay_us = value / 1000;
1022 pkt_dev->delay_ns = value % 1000;
1023 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001024 sprintf(pg_result, "OK: delay=%u",
1025 1000 * pkt_dev->delay_us + pkt_dev->delay_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 return count;
1027 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001028 if (!strcmp(name, "udp_src_min")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001030 if (len < 0) {
1031 return len;
1032 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001034 if (value != pkt_dev->udp_src_min) {
1035 pkt_dev->udp_src_min = value;
1036 pkt_dev->cur_udp_src = value;
1037 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min);
1039 return count;
1040 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001041 if (!strcmp(name, "udp_dst_min")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001043 if (len < 0) {
1044 return len;
1045 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001047 if (value != pkt_dev->udp_dst_min) {
1048 pkt_dev->udp_dst_min = value;
1049 pkt_dev->cur_udp_dst = value;
1050 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min);
1052 return count;
1053 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001054 if (!strcmp(name, "udp_src_max")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001056 if (len < 0) {
1057 return len;
1058 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001060 if (value != pkt_dev->udp_src_max) {
1061 pkt_dev->udp_src_max = value;
1062 pkt_dev->cur_udp_src = value;
1063 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max);
1065 return count;
1066 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001067 if (!strcmp(name, "udp_dst_max")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001069 if (len < 0) {
1070 return len;
1071 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001073 if (value != pkt_dev->udp_dst_max) {
1074 pkt_dev->udp_dst_max = value;
1075 pkt_dev->cur_udp_dst = value;
1076 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max);
1078 return count;
1079 }
1080 if (!strcmp(name, "clone_skb")) {
1081 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001082 if (len < 0) {
1083 return len;
1084 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001086 pkt_dev->clone_skb = value;
1087
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb);
1089 return count;
1090 }
1091 if (!strcmp(name, "count")) {
1092 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001093 if (len < 0) {
1094 return len;
1095 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 i += len;
1097 pkt_dev->count = value;
1098 sprintf(pg_result, "OK: count=%llu",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001099 (unsigned long long)pkt_dev->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 return count;
1101 }
1102 if (!strcmp(name, "src_mac_count")) {
1103 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001104 if (len < 0) {
1105 return len;
1106 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 i += len;
1108 if (pkt_dev->src_mac_count != value) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001109 pkt_dev->src_mac_count = value;
1110 pkt_dev->cur_src_mac_offset = 0;
1111 }
1112 sprintf(pg_result, "OK: src_mac_count=%d",
1113 pkt_dev->src_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 return count;
1115 }
1116 if (!strcmp(name, "dst_mac_count")) {
1117 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001118 if (len < 0) {
1119 return len;
1120 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 i += len;
1122 if (pkt_dev->dst_mac_count != value) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001123 pkt_dev->dst_mac_count = value;
1124 pkt_dev->cur_dst_mac_offset = 0;
1125 }
1126 sprintf(pg_result, "OK: dst_mac_count=%d",
1127 pkt_dev->dst_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 return count;
1129 }
1130 if (!strcmp(name, "flag")) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001131 char f[32];
1132 memset(f, 0, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 len = strn_len(&user_buffer[i], sizeof(f) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001134 if (len < 0) {
1135 return len;
1136 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 if (copy_from_user(f, &user_buffer[i], len))
1138 return -EFAULT;
1139 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001140 if (strcmp(f, "IPSRC_RND") == 0)
1141 pkt_dev->flags |= F_IPSRC_RND;
1142
1143 else if (strcmp(f, "!IPSRC_RND") == 0)
1144 pkt_dev->flags &= ~F_IPSRC_RND;
1145
1146 else if (strcmp(f, "TXSIZE_RND") == 0)
1147 pkt_dev->flags |= F_TXSIZE_RND;
1148
1149 else if (strcmp(f, "!TXSIZE_RND") == 0)
1150 pkt_dev->flags &= ~F_TXSIZE_RND;
1151
1152 else if (strcmp(f, "IPDST_RND") == 0)
1153 pkt_dev->flags |= F_IPDST_RND;
1154
1155 else if (strcmp(f, "!IPDST_RND") == 0)
1156 pkt_dev->flags &= ~F_IPDST_RND;
1157
1158 else if (strcmp(f, "UDPSRC_RND") == 0)
1159 pkt_dev->flags |= F_UDPSRC_RND;
1160
1161 else if (strcmp(f, "!UDPSRC_RND") == 0)
1162 pkt_dev->flags &= ~F_UDPSRC_RND;
1163
1164 else if (strcmp(f, "UDPDST_RND") == 0)
1165 pkt_dev->flags |= F_UDPDST_RND;
1166
1167 else if (strcmp(f, "!UDPDST_RND") == 0)
1168 pkt_dev->flags &= ~F_UDPDST_RND;
1169
1170 else if (strcmp(f, "MACSRC_RND") == 0)
1171 pkt_dev->flags |= F_MACSRC_RND;
1172
1173 else if (strcmp(f, "!MACSRC_RND") == 0)
1174 pkt_dev->flags &= ~F_MACSRC_RND;
1175
1176 else if (strcmp(f, "MACDST_RND") == 0)
1177 pkt_dev->flags |= F_MACDST_RND;
1178
1179 else if (strcmp(f, "!MACDST_RND") == 0)
1180 pkt_dev->flags &= ~F_MACDST_RND;
1181
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001182 else if (strcmp(f, "MPLS_RND") == 0)
1183 pkt_dev->flags |= F_MPLS_RND;
1184
1185 else if (strcmp(f, "!MPLS_RND") == 0)
1186 pkt_dev->flags &= ~F_MPLS_RND;
1187
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001188 else if (strcmp(f, "VID_RND") == 0)
1189 pkt_dev->flags |= F_VID_RND;
1190
1191 else if (strcmp(f, "!VID_RND") == 0)
1192 pkt_dev->flags &= ~F_VID_RND;
1193
1194 else if (strcmp(f, "SVID_RND") == 0)
1195 pkt_dev->flags |= F_SVID_RND;
1196
1197 else if (strcmp(f, "!SVID_RND") == 0)
1198 pkt_dev->flags &= ~F_SVID_RND;
1199
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001200 else if (strcmp(f, "!IPV6") == 0)
1201 pkt_dev->flags &= ~F_IPV6;
1202
Luiz Capitulino222f1802006-03-20 22:16:13 -08001203 else {
1204 sprintf(pg_result,
1205 "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
1206 f,
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001207 "IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
1208 "MACSRC_RND, MACDST_RND, TXSIZE_RND, IPV6, MPLS_RND, VID_RND, SVID_RND\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08001209 return count;
1210 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
1212 return count;
1213 }
1214 if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
1215 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001216 if (len < 0) {
1217 return len;
1218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
Luiz Capitulino222f1802006-03-20 22:16:13 -08001220 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001222 buf[len] = 0;
1223 if (strcmp(buf, pkt_dev->dst_min) != 0) {
1224 memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
1225 strncpy(pkt_dev->dst_min, buf, len);
1226 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
1227 pkt_dev->cur_daddr = pkt_dev->daddr_min;
1228 }
1229 if (debug)
1230 printk("pktgen: dst_min set to: %s\n",
1231 pkt_dev->dst_min);
1232 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min);
1234 return count;
1235 }
1236 if (!strcmp(name, "dst_max")) {
1237 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001238 if (len < 0) {
1239 return len;
1240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
Luiz Capitulino222f1802006-03-20 22:16:13 -08001242 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 return -EFAULT;
1244
Luiz Capitulino222f1802006-03-20 22:16:13 -08001245 buf[len] = 0;
1246 if (strcmp(buf, pkt_dev->dst_max) != 0) {
1247 memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
1248 strncpy(pkt_dev->dst_max, buf, len);
1249 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
1250 pkt_dev->cur_daddr = pkt_dev->daddr_max;
1251 }
1252 if (debug)
1253 printk("pktgen: dst_max set to: %s\n",
1254 pkt_dev->dst_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 i += len;
1256 sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max);
1257 return count;
1258 }
1259 if (!strcmp(name, "dst6")) {
1260 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001261 if (len < 0)
1262 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
1264 pkt_dev->flags |= F_IPV6;
1265
Luiz Capitulino222f1802006-03-20 22:16:13 -08001266 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001268 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
1270 scan_ip6(buf, pkt_dev->in6_daddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001271 fmt_ip6(buf, pkt_dev->in6_daddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
1273 ipv6_addr_copy(&pkt_dev->cur_in6_daddr, &pkt_dev->in6_daddr);
1274
Luiz Capitulino222f1802006-03-20 22:16:13 -08001275 if (debug)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 printk("pktgen: dst6 set to: %s\n", buf);
1277
Luiz Capitulino222f1802006-03-20 22:16:13 -08001278 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 sprintf(pg_result, "OK: dst6=%s", buf);
1280 return count;
1281 }
1282 if (!strcmp(name, "dst6_min")) {
1283 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001284 if (len < 0)
1285 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
1287 pkt_dev->flags |= F_IPV6;
1288
Luiz Capitulino222f1802006-03-20 22:16:13 -08001289 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001291 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
1293 scan_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001294 fmt_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
Luiz Capitulino222f1802006-03-20 22:16:13 -08001296 ipv6_addr_copy(&pkt_dev->cur_in6_daddr,
1297 &pkt_dev->min_in6_daddr);
1298 if (debug)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 printk("pktgen: dst6_min set to: %s\n", buf);
1300
Luiz Capitulino222f1802006-03-20 22:16:13 -08001301 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 sprintf(pg_result, "OK: dst6_min=%s", buf);
1303 return count;
1304 }
1305 if (!strcmp(name, "dst6_max")) {
1306 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001307 if (len < 0)
1308 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
1310 pkt_dev->flags |= F_IPV6;
1311
Luiz Capitulino222f1802006-03-20 22:16:13 -08001312 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001314 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
1316 scan_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001317 fmt_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
Luiz Capitulino222f1802006-03-20 22:16:13 -08001319 if (debug)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 printk("pktgen: dst6_max set to: %s\n", buf);
1321
Luiz Capitulino222f1802006-03-20 22:16:13 -08001322 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 sprintf(pg_result, "OK: dst6_max=%s", buf);
1324 return count;
1325 }
1326 if (!strcmp(name, "src6")) {
1327 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001328 if (len < 0)
1329 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
1331 pkt_dev->flags |= F_IPV6;
1332
Luiz Capitulino222f1802006-03-20 22:16:13 -08001333 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001335 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
1337 scan_ip6(buf, pkt_dev->in6_saddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001338 fmt_ip6(buf, pkt_dev->in6_saddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
1340 ipv6_addr_copy(&pkt_dev->cur_in6_saddr, &pkt_dev->in6_saddr);
1341
Luiz Capitulino222f1802006-03-20 22:16:13 -08001342 if (debug)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 printk("pktgen: src6 set to: %s\n", buf);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001344
1345 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 sprintf(pg_result, "OK: src6=%s", buf);
1347 return count;
1348 }
1349 if (!strcmp(name, "src_min")) {
1350 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001351 if (len < 0) {
1352 return len;
1353 }
1354 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001356 buf[len] = 0;
1357 if (strcmp(buf, pkt_dev->src_min) != 0) {
1358 memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
1359 strncpy(pkt_dev->src_min, buf, len);
1360 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
1361 pkt_dev->cur_saddr = pkt_dev->saddr_min;
1362 }
1363 if (debug)
1364 printk("pktgen: src_min set to: %s\n",
1365 pkt_dev->src_min);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 i += len;
1367 sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min);
1368 return count;
1369 }
1370 if (!strcmp(name, "src_max")) {
1371 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001372 if (len < 0) {
1373 return len;
1374 }
1375 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001377 buf[len] = 0;
1378 if (strcmp(buf, pkt_dev->src_max) != 0) {
1379 memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
1380 strncpy(pkt_dev->src_max, buf, len);
1381 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
1382 pkt_dev->cur_saddr = pkt_dev->saddr_max;
1383 }
1384 if (debug)
1385 printk("pktgen: src_max set to: %s\n",
1386 pkt_dev->src_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 i += len;
1388 sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max);
1389 return count;
1390 }
1391 if (!strcmp(name, "dst_mac")) {
1392 char *v = valstr;
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001393 unsigned char old_dmac[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 unsigned char *m = pkt_dev->dst_mac;
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001395 memcpy(old_dmac, pkt_dev->dst_mac, ETH_ALEN);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001396
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001398 if (len < 0) {
1399 return len;
1400 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 memset(valstr, 0, sizeof(valstr));
Luiz Capitulino222f1802006-03-20 22:16:13 -08001402 if (copy_from_user(valstr, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 return -EFAULT;
1404 i += len;
1405
Luiz Capitulino222f1802006-03-20 22:16:13 -08001406 for (*m = 0; *v && m < pkt_dev->dst_mac + 6; v++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 if (*v >= '0' && *v <= '9') {
1408 *m *= 16;
1409 *m += *v - '0';
1410 }
1411 if (*v >= 'A' && *v <= 'F') {
1412 *m *= 16;
1413 *m += *v - 'A' + 10;
1414 }
1415 if (*v >= 'a' && *v <= 'f') {
1416 *m *= 16;
1417 *m += *v - 'a' + 10;
1418 }
1419 if (*v == ':') {
1420 m++;
1421 *m = 0;
1422 }
1423 }
1424
1425 /* Set up Dest MAC */
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001426 if (compare_ether_addr(old_dmac, pkt_dev->dst_mac))
1427 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001428
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 sprintf(pg_result, "OK: dstmac");
1430 return count;
1431 }
1432 if (!strcmp(name, "src_mac")) {
1433 char *v = valstr;
1434 unsigned char *m = pkt_dev->src_mac;
1435
1436 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001437 if (len < 0) {
1438 return len;
1439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 memset(valstr, 0, sizeof(valstr));
Luiz Capitulino222f1802006-03-20 22:16:13 -08001441 if (copy_from_user(valstr, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 return -EFAULT;
1443 i += len;
1444
Luiz Capitulino222f1802006-03-20 22:16:13 -08001445 for (*m = 0; *v && m < pkt_dev->src_mac + 6; v++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 if (*v >= '0' && *v <= '9') {
1447 *m *= 16;
1448 *m += *v - '0';
1449 }
1450 if (*v >= 'A' && *v <= 'F') {
1451 *m *= 16;
1452 *m += *v - 'A' + 10;
1453 }
1454 if (*v >= 'a' && *v <= 'f') {
1455 *m *= 16;
1456 *m += *v - 'a' + 10;
1457 }
1458 if (*v == ':') {
1459 m++;
1460 *m = 0;
1461 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001462 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
Luiz Capitulino222f1802006-03-20 22:16:13 -08001464 sprintf(pg_result, "OK: srcmac");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 return count;
1466 }
1467
Luiz Capitulino222f1802006-03-20 22:16:13 -08001468 if (!strcmp(name, "clear_counters")) {
1469 pktgen_clear_counters(pkt_dev);
1470 sprintf(pg_result, "OK: Clearing counters.\n");
1471 return count;
1472 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
1474 if (!strcmp(name, "flows")) {
1475 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001476 if (len < 0) {
1477 return len;
1478 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 i += len;
1480 if (value > MAX_CFLOWS)
1481 value = MAX_CFLOWS;
1482
1483 pkt_dev->cflows = value;
1484 sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
1485 return count;
1486 }
1487
1488 if (!strcmp(name, "flowlen")) {
1489 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001490 if (len < 0) {
1491 return len;
1492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 i += len;
1494 pkt_dev->lflow = value;
1495 sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
1496 return count;
1497 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001498
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001499 if (!strcmp(name, "mpls")) {
1500 unsigned n, offset;
1501 len = get_labels(&user_buffer[i], pkt_dev);
1502 if (len < 0) { return len; }
1503 i += len;
1504 offset = sprintf(pg_result, "OK: mpls=");
1505 for(n = 0; n < pkt_dev->nr_labels; n++)
1506 offset += sprintf(pg_result + offset,
1507 "%08x%s", ntohl(pkt_dev->labels[n]),
1508 n == pkt_dev->nr_labels-1 ? "" : ",");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001509
1510 if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) {
1511 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1512 pkt_dev->svlan_id = 0xffff;
1513
1514 if (debug)
1515 printk("pktgen: VLAN/SVLAN auto turned off\n");
1516 }
1517 return count;
1518 }
1519
1520 if (!strcmp(name, "vlan_id")) {
1521 len = num_arg(&user_buffer[i], 4, &value);
1522 if (len < 0) {
1523 return len;
1524 }
1525 i += len;
1526 if (value <= 4095) {
1527 pkt_dev->vlan_id = value; /* turn on VLAN */
1528
1529 if (debug)
1530 printk("pktgen: VLAN turned on\n");
1531
1532 if (debug && pkt_dev->nr_labels)
1533 printk("pktgen: MPLS auto turned off\n");
1534
1535 pkt_dev->nr_labels = 0; /* turn off MPLS */
1536 sprintf(pg_result, "OK: vlan_id=%u", pkt_dev->vlan_id);
1537 } else {
1538 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1539 pkt_dev->svlan_id = 0xffff;
1540
1541 if (debug)
1542 printk("pktgen: VLAN/SVLAN turned off\n");
1543 }
1544 return count;
1545 }
1546
1547 if (!strcmp(name, "vlan_p")) {
1548 len = num_arg(&user_buffer[i], 1, &value);
1549 if (len < 0) {
1550 return len;
1551 }
1552 i += len;
1553 if ((value <= 7) && (pkt_dev->vlan_id != 0xffff)) {
1554 pkt_dev->vlan_p = value;
1555 sprintf(pg_result, "OK: vlan_p=%u", pkt_dev->vlan_p);
1556 } else {
1557 sprintf(pg_result, "ERROR: vlan_p must be 0-7");
1558 }
1559 return count;
1560 }
1561
1562 if (!strcmp(name, "vlan_cfi")) {
1563 len = num_arg(&user_buffer[i], 1, &value);
1564 if (len < 0) {
1565 return len;
1566 }
1567 i += len;
1568 if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) {
1569 pkt_dev->vlan_cfi = value;
1570 sprintf(pg_result, "OK: vlan_cfi=%u", pkt_dev->vlan_cfi);
1571 } else {
1572 sprintf(pg_result, "ERROR: vlan_cfi must be 0-1");
1573 }
1574 return count;
1575 }
1576
1577 if (!strcmp(name, "svlan_id")) {
1578 len = num_arg(&user_buffer[i], 4, &value);
1579 if (len < 0) {
1580 return len;
1581 }
1582 i += len;
1583 if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) {
1584 pkt_dev->svlan_id = value; /* turn on SVLAN */
1585
1586 if (debug)
1587 printk("pktgen: SVLAN turned on\n");
1588
1589 if (debug && pkt_dev->nr_labels)
1590 printk("pktgen: MPLS auto turned off\n");
1591
1592 pkt_dev->nr_labels = 0; /* turn off MPLS */
1593 sprintf(pg_result, "OK: svlan_id=%u", pkt_dev->svlan_id);
1594 } else {
1595 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1596 pkt_dev->svlan_id = 0xffff;
1597
1598 if (debug)
1599 printk("pktgen: VLAN/SVLAN turned off\n");
1600 }
1601 return count;
1602 }
1603
1604 if (!strcmp(name, "svlan_p")) {
1605 len = num_arg(&user_buffer[i], 1, &value);
1606 if (len < 0) {
1607 return len;
1608 }
1609 i += len;
1610 if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) {
1611 pkt_dev->svlan_p = value;
1612 sprintf(pg_result, "OK: svlan_p=%u", pkt_dev->svlan_p);
1613 } else {
1614 sprintf(pg_result, "ERROR: svlan_p must be 0-7");
1615 }
1616 return count;
1617 }
1618
1619 if (!strcmp(name, "svlan_cfi")) {
1620 len = num_arg(&user_buffer[i], 1, &value);
1621 if (len < 0) {
1622 return len;
1623 }
1624 i += len;
1625 if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) {
1626 pkt_dev->svlan_cfi = value;
1627 sprintf(pg_result, "OK: svlan_cfi=%u", pkt_dev->svlan_cfi);
1628 } else {
1629 sprintf(pg_result, "ERROR: svlan_cfi must be 0-1");
1630 }
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001631 return count;
1632 }
1633
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001634 if (!strcmp(name, "tos")) {
1635 __u32 tmp_value = 0;
1636 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1637 if (len < 0) {
1638 return len;
1639 }
1640 i += len;
1641 if (len == 2) {
1642 pkt_dev->tos = tmp_value;
1643 sprintf(pg_result, "OK: tos=0x%02x", pkt_dev->tos);
1644 } else {
1645 sprintf(pg_result, "ERROR: tos must be 00-ff");
1646 }
1647 return count;
1648 }
1649
1650 if (!strcmp(name, "traffic_class")) {
1651 __u32 tmp_value = 0;
1652 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1653 if (len < 0) {
1654 return len;
1655 }
1656 i += len;
1657 if (len == 2) {
1658 pkt_dev->traffic_class = tmp_value;
1659 sprintf(pg_result, "OK: traffic_class=0x%02x", pkt_dev->traffic_class);
1660 } else {
1661 sprintf(pg_result, "ERROR: traffic_class must be 00-ff");
1662 }
1663 return count;
1664 }
1665
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
1667 return -EINVAL;
1668}
1669
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001670static int pktgen_if_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671{
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001672 return single_open(file, pktgen_if_show, PDE(inode)->data);
1673}
1674
1675static struct file_operations pktgen_if_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001676 .owner = THIS_MODULE,
1677 .open = pktgen_if_open,
1678 .read = seq_read,
1679 .llseek = seq_lseek,
1680 .write = pktgen_if_write,
1681 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001682};
1683
1684static int pktgen_thread_show(struct seq_file *seq, void *v)
1685{
Luiz Capitulino222f1802006-03-20 22:16:13 -08001686 struct pktgen_thread *t = seq->private;
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08001687 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001689 BUG_ON(!t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001691 seq_printf(seq, "Name: %s max_before_softirq: %d\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001692 t->name, t->max_before_softirq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
Luiz Capitulino222f1802006-03-20 22:16:13 -08001694 seq_printf(seq, "Running: ");
1695
1696 if_lock(t);
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08001697 list_for_each_entry(pkt_dev, &t->if_list, list)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001698 if (pkt_dev->running)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001699 seq_printf(seq, "%s ", pkt_dev->ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
Luiz Capitulino222f1802006-03-20 22:16:13 -08001701 seq_printf(seq, "\nStopped: ");
1702
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08001703 list_for_each_entry(pkt_dev, &t->if_list, list)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001704 if (!pkt_dev->running)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001705 seq_printf(seq, "%s ", pkt_dev->ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
1707 if (t->result[0])
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001708 seq_printf(seq, "\nResult: %s\n", t->result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 else
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001710 seq_printf(seq, "\nResult: NA\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
Luiz Capitulino222f1802006-03-20 22:16:13 -08001712 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001714 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715}
1716
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001717static ssize_t pktgen_thread_write(struct file *file,
Luiz Capitulino222f1802006-03-20 22:16:13 -08001718 const char __user * user_buffer,
1719 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720{
Luiz Capitulino222f1802006-03-20 22:16:13 -08001721 struct seq_file *seq = (struct seq_file *)file->private_data;
1722 struct pktgen_thread *t = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 int i = 0, max, len, ret;
1724 char name[40];
Luiz Capitulino222f1802006-03-20 22:16:13 -08001725 char *pg_result;
1726 unsigned long value = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001727
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 if (count < 1) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001729 // sprintf(pg_result, "Wrong command format");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 return -EINVAL;
1731 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001732
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 max = count - i;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001734 len = count_trail_chars(&user_buffer[i], max);
1735 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001736 return len;
1737
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 i += len;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001739
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 /* Read variable name */
1741
1742 len = strn_len(&user_buffer[i], sizeof(name) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001743 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001744 return len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001745
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 memset(name, 0, sizeof(name));
1747 if (copy_from_user(name, &user_buffer[i], len))
1748 return -EFAULT;
1749 i += len;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001750
Luiz Capitulino222f1802006-03-20 22:16:13 -08001751 max = count - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 len = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001753 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001754 return len;
1755
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 i += len;
1757
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001758 if (debug)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001759 printk("pktgen: t=%s, count=%lu\n", name, (unsigned long)count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760
Luiz Capitulino222f1802006-03-20 22:16:13 -08001761 if (!t) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 printk("pktgen: ERROR: No thread\n");
1763 ret = -EINVAL;
1764 goto out;
1765 }
1766
1767 pg_result = &(t->result[0]);
1768
Luiz Capitulino222f1802006-03-20 22:16:13 -08001769 if (!strcmp(name, "add_device")) {
1770 char f[32];
1771 memset(f, 0, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 len = strn_len(&user_buffer[i], sizeof(f) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001773 if (len < 0) {
1774 ret = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 goto out;
1776 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001777 if (copy_from_user(f, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 return -EFAULT;
1779 i += len;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001780 mutex_lock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001781 pktgen_add_device(t, f);
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001782 mutex_unlock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001783 ret = count;
1784 sprintf(pg_result, "OK: add_device=%s", f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 goto out;
1786 }
1787
Luiz Capitulino222f1802006-03-20 22:16:13 -08001788 if (!strcmp(name, "rem_device_all")) {
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001789 mutex_lock(&pktgen_thread_lock);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001790 t->control |= T_REMDEVALL;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001791 mutex_unlock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001792 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 ret = count;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001794 sprintf(pg_result, "OK: rem_device_all");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 goto out;
1796 }
1797
Luiz Capitulino222f1802006-03-20 22:16:13 -08001798 if (!strcmp(name, "max_before_softirq")) {
1799 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001800 mutex_lock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001801 t->max_before_softirq = value;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001802 mutex_unlock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001803 ret = count;
1804 sprintf(pg_result, "OK: max_before_softirq=%lu", value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 goto out;
1806 }
1807
1808 ret = -EINVAL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001809out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 return ret;
1811}
1812
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001813static int pktgen_thread_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814{
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001815 return single_open(file, pktgen_thread_show, PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816}
1817
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001818static struct file_operations pktgen_thread_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001819 .owner = THIS_MODULE,
1820 .open = pktgen_thread_open,
1821 .read = seq_read,
1822 .llseek = seq_lseek,
1823 .write = pktgen_thread_write,
1824 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001825};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
1827/* Think find or remove for NN */
Luiz Capitulino222f1802006-03-20 22:16:13 -08001828static struct pktgen_dev *__pktgen_NN_threads(const char *ifname, int remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829{
1830 struct pktgen_thread *t;
1831 struct pktgen_dev *pkt_dev = NULL;
1832
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08001833 list_for_each_entry(t, &pktgen_threads, th_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 pkt_dev = pktgen_find_dev(t, ifname);
1835 if (pkt_dev) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001836 if (remove) {
1837 if_lock(t);
1838 pkt_dev->removal_mark = 1;
1839 t->control |= T_REMDEV;
1840 if_unlock(t);
1841 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 break;
1843 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001845 return pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846}
1847
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001848/*
1849 * mark a device for removal
1850 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08001851static int pktgen_mark_device(const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852{
1853 struct pktgen_dev *pkt_dev = NULL;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001854 const int max_tries = 10, msec_per_try = 125;
1855 int i = 0;
1856 int ret = 0;
1857
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001858 mutex_lock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001859 PG_DEBUG(printk("pktgen: pktgen_mark_device marking %s for removal\n",
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001860 ifname));
1861
Luiz Capitulino222f1802006-03-20 22:16:13 -08001862 while (1) {
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001863
1864 pkt_dev = __pktgen_NN_threads(ifname, REMOVE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001865 if (pkt_dev == NULL)
1866 break; /* success */
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001867
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001868 mutex_unlock(&pktgen_thread_lock);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001869 PG_DEBUG(printk("pktgen: pktgen_mark_device waiting for %s "
Luiz Capitulino222f1802006-03-20 22:16:13 -08001870 "to disappear....\n", ifname));
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001871 schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001872 mutex_lock(&pktgen_thread_lock);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001873
1874 if (++i >= max_tries) {
1875 printk("pktgen_mark_device: timed out after waiting "
Luiz Capitulino222f1802006-03-20 22:16:13 -08001876 "%d msec for device %s to be removed\n",
1877 msec_per_try * i, ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001878 ret = 1;
1879 break;
1880 }
1881
1882 }
1883
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001884 mutex_unlock(&pktgen_thread_lock);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001885
1886 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887}
1888
Luiz Capitulino222f1802006-03-20 22:16:13 -08001889static int pktgen_device_event(struct notifier_block *unused,
1890 unsigned long event, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891{
1892 struct net_device *dev = (struct net_device *)(ptr);
1893
1894 /* It is OK that we do not hold the group lock right now,
1895 * as we run under the RTNL lock.
1896 */
1897
1898 switch (event) {
1899 case NETDEV_CHANGEADDR:
1900 case NETDEV_GOING_DOWN:
1901 case NETDEV_DOWN:
1902 case NETDEV_UP:
1903 /* Ignore for now */
1904 break;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001905
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 case NETDEV_UNREGISTER:
Luiz Capitulino222f1802006-03-20 22:16:13 -08001907 pktgen_mark_device(dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 break;
1909 };
1910
1911 return NOTIFY_DONE;
1912}
1913
1914/* Associate pktgen_dev with a device. */
1915
Luiz Capitulino222f1802006-03-20 22:16:13 -08001916static struct net_device *pktgen_setup_dev(struct pktgen_dev *pkt_dev)
1917{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 struct net_device *odev;
1919
1920 /* Clean old setups */
1921
1922 if (pkt_dev->odev) {
1923 dev_put(pkt_dev->odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001924 pkt_dev->odev = NULL;
1925 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
1927 odev = dev_get_by_name(pkt_dev->ifname);
1928
1929 if (!odev) {
1930 printk("pktgen: no such netdevice: \"%s\"\n", pkt_dev->ifname);
1931 goto out;
1932 }
1933 if (odev->type != ARPHRD_ETHER) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001934 printk("pktgen: not an ethernet device: \"%s\"\n",
1935 pkt_dev->ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 goto out_put;
1937 }
1938 if (!netif_running(odev)) {
1939 printk("pktgen: device is down: \"%s\"\n", pkt_dev->ifname);
1940 goto out_put;
1941 }
1942 pkt_dev->odev = odev;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001943
1944 return pkt_dev->odev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
1946out_put:
1947 dev_put(odev);
1948out:
Luiz Capitulino222f1802006-03-20 22:16:13 -08001949 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950
1951}
1952
1953/* Read pkt_dev from the interface and set up internal pktgen_dev
1954 * structure to have the right information to create/send packets
1955 */
1956static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
1957{
1958 /* Try once more, just in case it works now. */
Luiz Capitulino222f1802006-03-20 22:16:13 -08001959 if (!pkt_dev->odev)
1960 pktgen_setup_dev(pkt_dev);
1961
1962 if (!pkt_dev->odev) {
1963 printk("pktgen: ERROR: pkt_dev->odev == NULL in setup_inject.\n");
1964 sprintf(pkt_dev->result,
1965 "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
1966 return;
1967 }
1968
1969 /* Default to the interface's mac if not explicitly set. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001971 if (is_zero_ether_addr(pkt_dev->src_mac))
Luiz Capitulino222f1802006-03-20 22:16:13 -08001972 memcpy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973
Luiz Capitulino222f1802006-03-20 22:16:13 -08001974 /* Set up Dest MAC */
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001975 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
Luiz Capitulino222f1802006-03-20 22:16:13 -08001977 /* Set up pkt size */
1978 pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
1979
1980 if (pkt_dev->flags & F_IPV6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 /*
1982 * Skip this automatic address setting until locks or functions
1983 * gets exported
1984 */
1985
1986#ifdef NOTNOW
Luiz Capitulino222f1802006-03-20 22:16:13 -08001987 int i, set = 0, err = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 struct inet6_dev *idev;
1989
Luiz Capitulino222f1802006-03-20 22:16:13 -08001990 for (i = 0; i < IN6_ADDR_HSIZE; i++)
1991 if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 set = 1;
1993 break;
1994 }
1995
Luiz Capitulino222f1802006-03-20 22:16:13 -08001996 if (!set) {
1997
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 /*
1999 * Use linklevel address if unconfigured.
2000 *
2001 * use ipv6_get_lladdr if/when it's get exported
2002 */
2003
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002004 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 if ((idev = __in6_dev_get(pkt_dev->odev)) != NULL) {
2006 struct inet6_ifaddr *ifp;
2007
2008 read_lock_bh(&idev->lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002009 for (ifp = idev->addr_list; ifp;
2010 ifp = ifp->if_next) {
2011 if (ifp->scope == IFA_LINK
2012 && !(ifp->
2013 flags & IFA_F_TENTATIVE)) {
2014 ipv6_addr_copy(&pkt_dev->
2015 cur_in6_saddr,
2016 &ifp->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 err = 0;
2018 break;
2019 }
2020 }
2021 read_unlock_bh(&idev->lock);
2022 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002023 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002024 if (err)
2025 printk("pktgen: ERROR: IPv6 link address not availble.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 }
2027#endif
Luiz Capitulino222f1802006-03-20 22:16:13 -08002028 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 pkt_dev->saddr_min = 0;
2030 pkt_dev->saddr_max = 0;
2031 if (strlen(pkt_dev->src_min) == 0) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002032
2033 struct in_device *in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034
2035 rcu_read_lock();
Herbert Xue5ed6392005-10-03 14:35:55 -07002036 in_dev = __in_dev_get_rcu(pkt_dev->odev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 if (in_dev) {
2038 if (in_dev->ifa_list) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002039 pkt_dev->saddr_min =
2040 in_dev->ifa_list->ifa_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 pkt_dev->saddr_max = pkt_dev->saddr_min;
2042 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 }
2044 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002045 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
2047 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
2048 }
2049
2050 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
2051 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
2052 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002053 /* Initialize current values. */
2054 pkt_dev->cur_dst_mac_offset = 0;
2055 pkt_dev->cur_src_mac_offset = 0;
2056 pkt_dev->cur_saddr = pkt_dev->saddr_min;
2057 pkt_dev->cur_daddr = pkt_dev->daddr_min;
2058 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2059 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 pkt_dev->nflows = 0;
2061}
2062
2063static void spin(struct pktgen_dev *pkt_dev, __u64 spin_until_us)
2064{
2065 __u64 start;
2066 __u64 now;
2067
2068 start = now = getCurUs();
2069 printk(KERN_INFO "sleeping for %d\n", (int)(spin_until_us - now));
2070 while (now < spin_until_us) {
Stephen Hemmingerb4099fa2005-10-14 15:32:22 -07002071 /* TODO: optimize sleeping behavior */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002072 if (spin_until_us - now > jiffies_to_usecs(1) + 1)
Nishanth Aravamudan121caf52005-09-12 14:15:34 -07002073 schedule_timeout_interruptible(1);
2074 else if (spin_until_us - now > 100) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 do_softirq();
2076 if (!pkt_dev->running)
2077 return;
2078 if (need_resched())
2079 schedule();
2080 }
2081
2082 now = getCurUs();
2083 }
2084
2085 pkt_dev->idle_acc += now - start;
2086}
2087
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088/* Increment/randomize headers according to flags and current values
2089 * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
2090 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002091static void mod_cur_headers(struct pktgen_dev *pkt_dev)
2092{
2093 __u32 imn;
2094 __u32 imx;
2095 int flow = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096
Luiz Capitulino222f1802006-03-20 22:16:13 -08002097 if (pkt_dev->cflows) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 flow = pktgen_random() % pkt_dev->cflows;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002099
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 if (pkt_dev->flows[flow].count > pkt_dev->lflow)
2101 pkt_dev->flows[flow].count = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103
2104 /* Deal with source MAC */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002105 if (pkt_dev->src_mac_count > 1) {
2106 __u32 mc;
2107 __u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108
Luiz Capitulino222f1802006-03-20 22:16:13 -08002109 if (pkt_dev->flags & F_MACSRC_RND)
2110 mc = pktgen_random() % (pkt_dev->src_mac_count);
2111 else {
2112 mc = pkt_dev->cur_src_mac_offset++;
2113 if (pkt_dev->cur_src_mac_offset >
2114 pkt_dev->src_mac_count)
2115 pkt_dev->cur_src_mac_offset = 0;
2116 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117
Luiz Capitulino222f1802006-03-20 22:16:13 -08002118 tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
2119 pkt_dev->hh[11] = tmp;
2120 tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2121 pkt_dev->hh[10] = tmp;
2122 tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2123 pkt_dev->hh[9] = tmp;
2124 tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2125 pkt_dev->hh[8] = tmp;
2126 tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
2127 pkt_dev->hh[7] = tmp;
2128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129
Luiz Capitulino222f1802006-03-20 22:16:13 -08002130 /* Deal with Destination MAC */
2131 if (pkt_dev->dst_mac_count > 1) {
2132 __u32 mc;
2133 __u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134
Luiz Capitulino222f1802006-03-20 22:16:13 -08002135 if (pkt_dev->flags & F_MACDST_RND)
2136 mc = pktgen_random() % (pkt_dev->dst_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137
Luiz Capitulino222f1802006-03-20 22:16:13 -08002138 else {
2139 mc = pkt_dev->cur_dst_mac_offset++;
2140 if (pkt_dev->cur_dst_mac_offset >
2141 pkt_dev->dst_mac_count) {
2142 pkt_dev->cur_dst_mac_offset = 0;
2143 }
2144 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145
Luiz Capitulino222f1802006-03-20 22:16:13 -08002146 tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
2147 pkt_dev->hh[5] = tmp;
2148 tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2149 pkt_dev->hh[4] = tmp;
2150 tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2151 pkt_dev->hh[3] = tmp;
2152 tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2153 pkt_dev->hh[2] = tmp;
2154 tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
2155 pkt_dev->hh[1] = tmp;
2156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002158 if (pkt_dev->flags & F_MPLS_RND) {
2159 unsigned i;
2160 for(i = 0; i < pkt_dev->nr_labels; i++)
2161 if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
2162 pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
2163 (pktgen_random() &
2164 htonl(0x000fffff));
2165 }
2166
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002167 if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
2168 pkt_dev->vlan_id = pktgen_random() % 4096;
2169 }
2170
2171 if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
2172 pkt_dev->svlan_id = pktgen_random() % 4096;
2173 }
2174
Luiz Capitulino222f1802006-03-20 22:16:13 -08002175 if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
2176 if (pkt_dev->flags & F_UDPSRC_RND)
2177 pkt_dev->cur_udp_src =
2178 ((pktgen_random() %
2179 (pkt_dev->udp_src_max - pkt_dev->udp_src_min)) +
2180 pkt_dev->udp_src_min);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181
Luiz Capitulino222f1802006-03-20 22:16:13 -08002182 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 pkt_dev->cur_udp_src++;
2184 if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
2185 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002186 }
2187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188
Luiz Capitulino222f1802006-03-20 22:16:13 -08002189 if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
2190 if (pkt_dev->flags & F_UDPDST_RND) {
2191 pkt_dev->cur_udp_dst =
2192 ((pktgen_random() %
2193 (pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)) +
2194 pkt_dev->udp_dst_min);
2195 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 pkt_dev->cur_udp_dst++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002197 if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002199 }
2200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201
2202 if (!(pkt_dev->flags & F_IPV6)) {
2203
Luiz Capitulino222f1802006-03-20 22:16:13 -08002204 if ((imn = ntohl(pkt_dev->saddr_min)) < (imx =
2205 ntohl(pkt_dev->
2206 saddr_max))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 __u32 t;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002208 if (pkt_dev->flags & F_IPSRC_RND)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 t = ((pktgen_random() % (imx - imn)) + imn);
2210 else {
2211 t = ntohl(pkt_dev->cur_saddr);
2212 t++;
2213 if (t > imx) {
2214 t = imn;
2215 }
2216 }
2217 pkt_dev->cur_saddr = htonl(t);
2218 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002219
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 if (pkt_dev->cflows && pkt_dev->flows[flow].count != 0) {
2221 pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
2222 } else {
2223
Luiz Capitulino222f1802006-03-20 22:16:13 -08002224 if ((imn = ntohl(pkt_dev->daddr_min)) < (imx =
2225 ntohl(pkt_dev->
2226 daddr_max)))
2227 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 __u32 t;
2229 if (pkt_dev->flags & F_IPDST_RND) {
2230
Luiz Capitulino222f1802006-03-20 22:16:13 -08002231 t = ((pktgen_random() % (imx - imn)) +
2232 imn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 t = htonl(t);
2234
Luiz Capitulino222f1802006-03-20 22:16:13 -08002235 while (LOOPBACK(t) || MULTICAST(t)
2236 || BADCLASS(t) || ZERONET(t)
2237 || LOCAL_MCAST(t)) {
2238 t = ((pktgen_random() %
2239 (imx - imn)) + imn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 t = htonl(t);
2241 }
2242 pkt_dev->cur_daddr = t;
2243 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002244
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 else {
2246 t = ntohl(pkt_dev->cur_daddr);
2247 t++;
2248 if (t > imx) {
2249 t = imn;
2250 }
2251 pkt_dev->cur_daddr = htonl(t);
2252 }
2253 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002254 if (pkt_dev->cflows) {
2255 pkt_dev->flows[flow].cur_daddr =
2256 pkt_dev->cur_daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 pkt_dev->nflows++;
2258 }
2259 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002260 } else { /* IPV6 * */
2261
2262 if (pkt_dev->min_in6_daddr.s6_addr32[0] == 0 &&
2263 pkt_dev->min_in6_daddr.s6_addr32[1] == 0 &&
2264 pkt_dev->min_in6_daddr.s6_addr32[2] == 0 &&
2265 pkt_dev->min_in6_daddr.s6_addr32[3] == 0) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 else {
2267 int i;
2268
2269 /* Only random destinations yet */
2270
Luiz Capitulino222f1802006-03-20 22:16:13 -08002271 for (i = 0; i < 4; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 pkt_dev->cur_in6_daddr.s6_addr32[i] =
Luiz Capitulino222f1802006-03-20 22:16:13 -08002273 ((pktgen_random() |
2274 pkt_dev->min_in6_daddr.s6_addr32[i]) &
2275 pkt_dev->max_in6_daddr.s6_addr32[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 }
2279
Luiz Capitulino222f1802006-03-20 22:16:13 -08002280 if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
2281 __u32 t;
2282 if (pkt_dev->flags & F_TXSIZE_RND) {
2283 t = ((pktgen_random() %
2284 (pkt_dev->max_pkt_size - pkt_dev->min_pkt_size))
2285 + pkt_dev->min_pkt_size);
2286 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 t = pkt_dev->cur_pkt_size + 1;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002288 if (t > pkt_dev->max_pkt_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 t = pkt_dev->min_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002290 }
2291 pkt_dev->cur_pkt_size = t;
2292 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293
2294 pkt_dev->flows[flow].count++;
2295}
2296
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002297static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
2298{
2299 unsigned i;
2300 for(i = 0; i < pkt_dev->nr_labels; i++) {
2301 *mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
2302 }
2303 mpls--;
2304 *mpls |= MPLS_STACK_BOTTOM;
2305}
2306
Al Viro0f37c602006-11-03 03:49:56 -08002307static inline __be16 build_tci(unsigned int id, unsigned int cfi,
2308 unsigned int prio)
2309{
2310 return htons(id | (cfi << 12) | (prio << 13));
2311}
2312
Luiz Capitulino222f1802006-03-20 22:16:13 -08002313static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
2314 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315{
2316 struct sk_buff *skb = NULL;
2317 __u8 *eth;
2318 struct udphdr *udph;
2319 int datalen, iplen;
2320 struct iphdr *iph;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002321 struct pktgen_hdr *pgh = NULL;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002322 __be16 protocol = __constant_htons(ETH_P_IP);
2323 __be32 *mpls;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002324 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2325 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2326 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2327 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2328
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002329
2330 if (pkt_dev->nr_labels)
2331 protocol = __constant_htons(ETH_P_MPLS_UC);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002332
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002333 if (pkt_dev->vlan_id != 0xffff)
2334 protocol = __constant_htons(ETH_P_8021Q);
2335
Robert Olsson64053be2005-06-26 15:27:10 -07002336 /* Update any of the values, used when we're incrementing various
2337 * fields.
2338 */
2339 mod_cur_headers(pkt_dev);
2340
David S. Miller7ac54592006-01-18 14:19:10 -08002341 datalen = (odev->hard_header_len + 16) & ~0xf;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002342 skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + datalen +
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002343 pkt_dev->nr_labels*sizeof(u32) +
2344 VLAN_TAG_SIZE(pkt_dev) + SVLAN_TAG_SIZE(pkt_dev),
2345 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 if (!skb) {
2347 sprintf(pkt_dev->result, "No memory");
2348 return NULL;
2349 }
2350
David S. Miller7ac54592006-01-18 14:19:10 -08002351 skb_reserve(skb, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352
2353 /* Reserve for ethernet and IP header */
2354 eth = (__u8 *) skb_push(skb, 14);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002355 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2356 if (pkt_dev->nr_labels)
2357 mpls_push(mpls, pkt_dev);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002358
2359 if (pkt_dev->vlan_id != 0xffff) {
2360 if(pkt_dev->svlan_id != 0xffff) {
2361 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002362 *svlan_tci = build_tci(pkt_dev->svlan_id,
2363 pkt_dev->svlan_cfi,
2364 pkt_dev->svlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002365 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2366 *svlan_encapsulated_proto = __constant_htons(ETH_P_8021Q);
2367 }
2368 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002369 *vlan_tci = build_tci(pkt_dev->vlan_id,
2370 pkt_dev->vlan_cfi,
2371 pkt_dev->vlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002372 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2373 *vlan_encapsulated_proto = __constant_htons(ETH_P_IP);
2374 }
2375
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 iph = (struct iphdr *)skb_put(skb, sizeof(struct iphdr));
2377 udph = (struct udphdr *)skb_put(skb, sizeof(struct udphdr));
2378
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 memcpy(eth, pkt_dev->hh, 12);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002380 *(u16 *) & eth[12] = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002382 /* Eth + IPh + UDPh + mpls */
2383 datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002384 pkt_dev->nr_labels*sizeof(u32) - VLAN_TAG_SIZE(pkt_dev) - SVLAN_TAG_SIZE(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002385 if (datalen < sizeof(struct pktgen_hdr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 datalen = sizeof(struct pktgen_hdr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002387
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 udph->source = htons(pkt_dev->cur_udp_src);
2389 udph->dest = htons(pkt_dev->cur_udp_dst);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002390 udph->len = htons(datalen + 8); /* DATA + udphdr */
2391 udph->check = 0; /* No checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392
2393 iph->ihl = 5;
2394 iph->version = 4;
2395 iph->ttl = 32;
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002396 iph->tos = pkt_dev->tos;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002397 iph->protocol = IPPROTO_UDP; /* UDP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 iph->saddr = pkt_dev->cur_saddr;
2399 iph->daddr = pkt_dev->cur_daddr;
2400 iph->frag_off = 0;
2401 iplen = 20 + 8 + datalen;
2402 iph->tot_len = htons(iplen);
2403 iph->check = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002404 iph->check = ip_fast_csum((void *)iph, iph->ihl);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002405 skb->protocol = protocol;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002406 skb->mac.raw = ((u8 *) iph) - 14 - pkt_dev->nr_labels*sizeof(u32) -
2407 VLAN_TAG_SIZE(pkt_dev) - SVLAN_TAG_SIZE(pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 skb->dev = odev;
2409 skb->pkt_type = PACKET_HOST;
Chen-Li Tienaaf58062006-08-07 20:49:07 -07002410 skb->nh.iph = iph;
2411 skb->h.uh = udph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412
Luiz Capitulino222f1802006-03-20 22:16:13 -08002413 if (pkt_dev->nfrags <= 0)
2414 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 else {
2416 int frags = pkt_dev->nfrags;
2417 int i;
2418
Luiz Capitulino222f1802006-03-20 22:16:13 -08002419 pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
2420
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 if (frags > MAX_SKB_FRAGS)
2422 frags = MAX_SKB_FRAGS;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002423 if (datalen > frags * PAGE_SIZE) {
2424 skb_put(skb, datalen - frags * PAGE_SIZE);
2425 datalen = frags * PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 }
2427
2428 i = 0;
2429 while (datalen > 0) {
2430 struct page *page = alloc_pages(GFP_KERNEL, 0);
2431 skb_shinfo(skb)->frags[i].page = page;
2432 skb_shinfo(skb)->frags[i].page_offset = 0;
2433 skb_shinfo(skb)->frags[i].size =
Luiz Capitulino222f1802006-03-20 22:16:13 -08002434 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 datalen -= skb_shinfo(skb)->frags[i].size;
2436 skb->len += skb_shinfo(skb)->frags[i].size;
2437 skb->data_len += skb_shinfo(skb)->frags[i].size;
2438 i++;
2439 skb_shinfo(skb)->nr_frags = i;
2440 }
2441
2442 while (i < frags) {
2443 int rem;
2444
2445 if (i == 0)
2446 break;
2447
2448 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
2449 if (rem == 0)
2450 break;
2451
2452 skb_shinfo(skb)->frags[i - 1].size -= rem;
2453
Luiz Capitulino222f1802006-03-20 22:16:13 -08002454 skb_shinfo(skb)->frags[i] =
2455 skb_shinfo(skb)->frags[i - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 get_page(skb_shinfo(skb)->frags[i].page);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002457 skb_shinfo(skb)->frags[i].page =
2458 skb_shinfo(skb)->frags[i - 1].page;
2459 skb_shinfo(skb)->frags[i].page_offset +=
2460 skb_shinfo(skb)->frags[i - 1].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 skb_shinfo(skb)->frags[i].size = rem;
2462 i++;
2463 skb_shinfo(skb)->nr_frags = i;
2464 }
2465 }
2466
Luiz Capitulino222f1802006-03-20 22:16:13 -08002467 /* Stamp the time, and sequence number, convert them to network byte order */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468
Luiz Capitulino222f1802006-03-20 22:16:13 -08002469 if (pgh) {
2470 struct timeval timestamp;
2471
2472 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2473 pgh->seq_num = htonl(pkt_dev->seq_num);
2474
2475 do_gettimeofday(&timestamp);
2476 pgh->tv_sec = htonl(timestamp.tv_sec);
2477 pgh->tv_usec = htonl(timestamp.tv_usec);
2478 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002479
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 return skb;
2481}
2482
2483/*
2484 * scan_ip6, fmt_ip taken from dietlibc-0.21
2485 * Author Felix von Leitner <felix-dietlibc@fefe.de>
2486 *
2487 * Slightly modified for kernel.
2488 * Should be candidate for net/ipv4/utils.c
2489 * --ro
2490 */
2491
Luiz Capitulino222f1802006-03-20 22:16:13 -08002492static unsigned int scan_ip6(const char *s, char ip[16])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493{
2494 unsigned int i;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002495 unsigned int len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 unsigned long u;
2497 char suffix[16];
Luiz Capitulino222f1802006-03-20 22:16:13 -08002498 unsigned int prefixlen = 0;
2499 unsigned int suffixlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 __u32 tmp;
2501
Luiz Capitulino222f1802006-03-20 22:16:13 -08002502 for (i = 0; i < 16; i++)
2503 ip[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504
2505 for (;;) {
2506 if (*s == ':') {
2507 len++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002508 if (s[1] == ':') { /* Found "::", skip to part 2 */
2509 s += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 len++;
2511 break;
2512 }
2513 s++;
2514 }
2515 {
2516 char *tmp;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002517 u = simple_strtoul(s, &tmp, 16);
2518 i = tmp - s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 }
2520
Luiz Capitulino222f1802006-03-20 22:16:13 -08002521 if (!i)
2522 return 0;
2523 if (prefixlen == 12 && s[i] == '.') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524
2525 /* the last 4 bytes may be written as IPv4 address */
2526
2527 tmp = in_aton(s);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002528 memcpy((struct in_addr *)(ip + 12), &tmp, sizeof(tmp));
2529 return i + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 }
2531 ip[prefixlen++] = (u >> 8);
2532 ip[prefixlen++] = (u & 255);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002533 s += i;
2534 len += i;
2535 if (prefixlen == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 return len;
2537 }
2538
2539/* part 2, after "::" */
2540 for (;;) {
2541 if (*s == ':') {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002542 if (suffixlen == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 break;
2544 s++;
2545 len++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002546 } else if (suffixlen != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 break;
2548 {
2549 char *tmp;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002550 u = simple_strtol(s, &tmp, 16);
2551 i = tmp - s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 }
2553 if (!i) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002554 if (*s)
2555 len--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 break;
2557 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002558 if (suffixlen + prefixlen <= 12 && s[i] == '.') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 tmp = in_aton(s);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002560 memcpy((struct in_addr *)(suffix + suffixlen), &tmp,
2561 sizeof(tmp));
2562 suffixlen += 4;
2563 len += strlen(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 break;
2565 }
2566 suffix[suffixlen++] = (u >> 8);
2567 suffix[suffixlen++] = (u & 255);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002568 s += i;
2569 len += i;
2570 if (prefixlen + suffixlen == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 break;
2572 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002573 for (i = 0; i < suffixlen; i++)
2574 ip[16 - suffixlen + i] = suffix[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 return len;
2576}
2577
Luiz Capitulino222f1802006-03-20 22:16:13 -08002578static char tohex(char hexdigit)
2579{
2580 return hexdigit > 9 ? hexdigit + 'a' - 10 : hexdigit + '0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581}
2582
Luiz Capitulino222f1802006-03-20 22:16:13 -08002583static int fmt_xlong(char *s, unsigned int i)
2584{
2585 char *bak = s;
2586 *s = tohex((i >> 12) & 0xf);
2587 if (s != bak || *s != '0')
2588 ++s;
2589 *s = tohex((i >> 8) & 0xf);
2590 if (s != bak || *s != '0')
2591 ++s;
2592 *s = tohex((i >> 4) & 0xf);
2593 if (s != bak || *s != '0')
2594 ++s;
2595 *s = tohex(i & 0xf);
2596 return s - bak + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597}
2598
Luiz Capitulino222f1802006-03-20 22:16:13 -08002599static unsigned int fmt_ip6(char *s, const char ip[16])
2600{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 unsigned int len;
2602 unsigned int i;
2603 unsigned int temp;
2604 unsigned int compressing;
2605 int j;
2606
Luiz Capitulino222f1802006-03-20 22:16:13 -08002607 len = 0;
2608 compressing = 0;
2609 for (j = 0; j < 16; j += 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610
2611#ifdef V4MAPPEDPREFIX
Luiz Capitulino222f1802006-03-20 22:16:13 -08002612 if (j == 12 && !memcmp(ip, V4mappedprefix, 12)) {
2613 inet_ntoa_r(*(struct in_addr *)(ip + 12), s);
2614 temp = strlen(s);
2615 return len + temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 }
2617#endif
Luiz Capitulino222f1802006-03-20 22:16:13 -08002618 temp = ((unsigned long)(unsigned char)ip[j] << 8) +
2619 (unsigned long)(unsigned char)ip[j + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 if (temp == 0) {
2621 if (!compressing) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002622 compressing = 1;
2623 if (j == 0) {
2624 *s++ = ':';
2625 ++len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 }
2627 }
2628 } else {
2629 if (compressing) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002630 compressing = 0;
2631 *s++ = ':';
2632 ++len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002634 i = fmt_xlong(s, temp);
2635 len += i;
2636 s += i;
2637 if (j < 14) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 *s++ = ':';
2639 ++len;
2640 }
2641 }
2642 }
2643 if (compressing) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002644 *s++ = ':';
2645 ++len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002647 *s = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 return len;
2649}
2650
Luiz Capitulino222f1802006-03-20 22:16:13 -08002651static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
2652 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653{
2654 struct sk_buff *skb = NULL;
2655 __u8 *eth;
2656 struct udphdr *udph;
2657 int datalen;
2658 struct ipv6hdr *iph;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002659 struct pktgen_hdr *pgh = NULL;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002660 __be16 protocol = __constant_htons(ETH_P_IPV6);
2661 __be32 *mpls;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002662 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2663 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2664 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2665 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002666
2667 if (pkt_dev->nr_labels)
2668 protocol = __constant_htons(ETH_P_MPLS_UC);
Robert Olsson64053be2005-06-26 15:27:10 -07002669
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002670 if (pkt_dev->vlan_id != 0xffff)
2671 protocol = __constant_htons(ETH_P_8021Q);
2672
Robert Olsson64053be2005-06-26 15:27:10 -07002673 /* Update any of the values, used when we're incrementing various
2674 * fields.
2675 */
2676 mod_cur_headers(pkt_dev);
2677
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002678 skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + 16 +
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002679 pkt_dev->nr_labels*sizeof(u32) +
2680 VLAN_TAG_SIZE(pkt_dev) + SVLAN_TAG_SIZE(pkt_dev),
2681 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 if (!skb) {
2683 sprintf(pkt_dev->result, "No memory");
2684 return NULL;
2685 }
2686
2687 skb_reserve(skb, 16);
2688
2689 /* Reserve for ethernet and IP header */
2690 eth = (__u8 *) skb_push(skb, 14);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002691 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2692 if (pkt_dev->nr_labels)
2693 mpls_push(mpls, pkt_dev);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002694
2695 if (pkt_dev->vlan_id != 0xffff) {
2696 if(pkt_dev->svlan_id != 0xffff) {
2697 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002698 *svlan_tci = build_tci(pkt_dev->svlan_id,
2699 pkt_dev->svlan_cfi,
2700 pkt_dev->svlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002701 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2702 *svlan_encapsulated_proto = __constant_htons(ETH_P_8021Q);
2703 }
2704 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002705 *vlan_tci = build_tci(pkt_dev->vlan_id,
2706 pkt_dev->vlan_cfi,
2707 pkt_dev->vlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002708 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2709 *vlan_encapsulated_proto = __constant_htons(ETH_P_IPV6);
2710 }
2711
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 iph = (struct ipv6hdr *)skb_put(skb, sizeof(struct ipv6hdr));
2713 udph = (struct udphdr *)skb_put(skb, sizeof(struct udphdr));
2714
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 memcpy(eth, pkt_dev->hh, 12);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002716 *(u16 *) & eth[12] = protocol;
Robert Olsson64053be2005-06-26 15:27:10 -07002717
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002718 /* Eth + IPh + UDPh + mpls */
2719 datalen = pkt_dev->cur_pkt_size - 14 -
2720 sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002721 pkt_dev->nr_labels*sizeof(u32) - VLAN_TAG_SIZE(pkt_dev) - SVLAN_TAG_SIZE(pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722
Luiz Capitulino222f1802006-03-20 22:16:13 -08002723 if (datalen < sizeof(struct pktgen_hdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 datalen = sizeof(struct pktgen_hdr);
2725 if (net_ratelimit())
Luiz Capitulino222f1802006-03-20 22:16:13 -08002726 printk(KERN_INFO "pktgen: increased datalen to %d\n",
2727 datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 }
2729
2730 udph->source = htons(pkt_dev->cur_udp_src);
2731 udph->dest = htons(pkt_dev->cur_udp_dst);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002732 udph->len = htons(datalen + sizeof(struct udphdr));
2733 udph->check = 0; /* No checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734
Luiz Capitulino222f1802006-03-20 22:16:13 -08002735 *(u32 *) iph = __constant_htonl(0x60000000); /* Version + flow */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002737 if (pkt_dev->traffic_class) {
2738 /* Version + traffic class + flow (0) */
2739 *(u32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
2740 }
2741
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 iph->hop_limit = 32;
2743
2744 iph->payload_len = htons(sizeof(struct udphdr) + datalen);
2745 iph->nexthdr = IPPROTO_UDP;
2746
2747 ipv6_addr_copy(&iph->daddr, &pkt_dev->cur_in6_daddr);
2748 ipv6_addr_copy(&iph->saddr, &pkt_dev->cur_in6_saddr);
2749
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002750 skb->mac.raw = ((u8 *) iph) - 14 - pkt_dev->nr_labels*sizeof(u32) -
2751 VLAN_TAG_SIZE(pkt_dev) - SVLAN_TAG_SIZE(pkt_dev);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002752 skb->protocol = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 skb->dev = odev;
2754 skb->pkt_type = PACKET_HOST;
David S. Miller69d8c282006-08-07 20:52:10 -07002755 skb->nh.ipv6h = iph;
2756 skb->h.uh = udph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757
Luiz Capitulino222f1802006-03-20 22:16:13 -08002758 if (pkt_dev->nfrags <= 0)
2759 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 else {
2761 int frags = pkt_dev->nfrags;
2762 int i;
2763
Luiz Capitulino222f1802006-03-20 22:16:13 -08002764 pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
2765
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 if (frags > MAX_SKB_FRAGS)
2767 frags = MAX_SKB_FRAGS;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002768 if (datalen > frags * PAGE_SIZE) {
2769 skb_put(skb, datalen - frags * PAGE_SIZE);
2770 datalen = frags * PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 }
2772
2773 i = 0;
2774 while (datalen > 0) {
2775 struct page *page = alloc_pages(GFP_KERNEL, 0);
2776 skb_shinfo(skb)->frags[i].page = page;
2777 skb_shinfo(skb)->frags[i].page_offset = 0;
2778 skb_shinfo(skb)->frags[i].size =
Luiz Capitulino222f1802006-03-20 22:16:13 -08002779 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 datalen -= skb_shinfo(skb)->frags[i].size;
2781 skb->len += skb_shinfo(skb)->frags[i].size;
2782 skb->data_len += skb_shinfo(skb)->frags[i].size;
2783 i++;
2784 skb_shinfo(skb)->nr_frags = i;
2785 }
2786
2787 while (i < frags) {
2788 int rem;
2789
2790 if (i == 0)
2791 break;
2792
2793 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
2794 if (rem == 0)
2795 break;
2796
2797 skb_shinfo(skb)->frags[i - 1].size -= rem;
2798
Luiz Capitulino222f1802006-03-20 22:16:13 -08002799 skb_shinfo(skb)->frags[i] =
2800 skb_shinfo(skb)->frags[i - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 get_page(skb_shinfo(skb)->frags[i].page);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002802 skb_shinfo(skb)->frags[i].page =
2803 skb_shinfo(skb)->frags[i - 1].page;
2804 skb_shinfo(skb)->frags[i].page_offset +=
2805 skb_shinfo(skb)->frags[i - 1].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 skb_shinfo(skb)->frags[i].size = rem;
2807 i++;
2808 skb_shinfo(skb)->nr_frags = i;
2809 }
2810 }
2811
Luiz Capitulino222f1802006-03-20 22:16:13 -08002812 /* Stamp the time, and sequence number, convert them to network byte order */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 /* should we update cloned packets too ? */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002814 if (pgh) {
2815 struct timeval timestamp;
2816
2817 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2818 pgh->seq_num = htonl(pkt_dev->seq_num);
2819
2820 do_gettimeofday(&timestamp);
2821 pgh->tv_sec = htonl(timestamp.tv_sec);
2822 pgh->tv_usec = htonl(timestamp.tv_usec);
2823 }
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002824 /* pkt_dev->seq_num++; FF: you really mean this? */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002825
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 return skb;
2827}
2828
Luiz Capitulino222f1802006-03-20 22:16:13 -08002829static inline struct sk_buff *fill_packet(struct net_device *odev,
2830 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831{
Luiz Capitulino222f1802006-03-20 22:16:13 -08002832 if (pkt_dev->flags & F_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 return fill_packet_ipv6(odev, pkt_dev);
2834 else
2835 return fill_packet_ipv4(odev, pkt_dev);
2836}
2837
Luiz Capitulino222f1802006-03-20 22:16:13 -08002838static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839{
Luiz Capitulino222f1802006-03-20 22:16:13 -08002840 pkt_dev->seq_num = 1;
2841 pkt_dev->idle_acc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002843 pkt_dev->tx_bytes = 0;
2844 pkt_dev->errors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845}
2846
2847/* Set up structure for sending pkts, clear counters */
2848
2849static void pktgen_run(struct pktgen_thread *t)
2850{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08002851 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 int started = 0;
2853
2854 PG_DEBUG(printk("pktgen: entering pktgen_run. %p\n", t));
2855
2856 if_lock(t);
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08002857 list_for_each_entry(pkt_dev, &t->if_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858
2859 /*
2860 * setup odev and create initial packet.
2861 */
2862 pktgen_setup_inject(pkt_dev);
2863
Luiz Capitulino222f1802006-03-20 22:16:13 -08002864 if (pkt_dev->odev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 pktgen_clear_counters(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002866 pkt_dev->running = 1; /* Cranke yeself! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 pkt_dev->skb = NULL;
2868 pkt_dev->started_at = getCurUs();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002869 pkt_dev->next_tx_us = getCurUs(); /* Transmit immediately */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 pkt_dev->next_tx_ns = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002871
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 strcpy(pkt_dev->result, "Starting");
2873 started++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002874 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 strcpy(pkt_dev->result, "Error starting");
2876 }
2877 if_unlock(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002878 if (started)
2879 t->control &= ~(T_STOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880}
2881
2882static void pktgen_stop_all_threads_ifs(void)
2883{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002884 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885
Arthur Kepner95ed63f2006-03-20 21:26:56 -08002886 PG_DEBUG(printk("pktgen: entering pktgen_stop_all_threads_ifs.\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08002888 mutex_lock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002889
2890 list_for_each_entry(t, &pktgen_threads, th_list)
Arthur Kepner95ed63f2006-03-20 21:26:56 -08002891 t->control |= T_STOP;
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002892
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08002893 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894}
2895
Luiz Capitulino222f1802006-03-20 22:16:13 -08002896static int thread_is_running(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08002898 struct pktgen_dev *pkt_dev;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002899 int res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08002901 list_for_each_entry(pkt_dev, &t->if_list, list)
2902 if (pkt_dev->running) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 res = 1;
2904 break;
2905 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002906 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907}
2908
Luiz Capitulino222f1802006-03-20 22:16:13 -08002909static int pktgen_wait_thread_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910{
Luiz Capitulino222f1802006-03-20 22:16:13 -08002911 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912
Luiz Capitulino222f1802006-03-20 22:16:13 -08002913 while (thread_is_running(t)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914
Luiz Capitulino222f1802006-03-20 22:16:13 -08002915 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916
Luiz Capitulino222f1802006-03-20 22:16:13 -08002917 msleep_interruptible(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918
Luiz Capitulino222f1802006-03-20 22:16:13 -08002919 if (signal_pending(current))
2920 goto signal;
2921 if_lock(t);
2922 }
2923 if_unlock(t);
2924 return 1;
2925signal:
2926 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927}
2928
2929static int pktgen_wait_all_threads_run(void)
2930{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002931 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 int sig = 1;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002933
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08002934 mutex_lock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002935
2936 list_for_each_entry(t, &pktgen_threads, th_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 sig = pktgen_wait_thread_run(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002938 if (sig == 0)
2939 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 }
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002941
2942 if (sig == 0)
2943 list_for_each_entry(t, &pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 t->control |= (T_STOP);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002945
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08002946 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 return sig;
2948}
2949
2950static void pktgen_run_all_threads(void)
2951{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002952 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953
2954 PG_DEBUG(printk("pktgen: entering pktgen_run_all_threads.\n"));
2955
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08002956 mutex_lock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002958 list_for_each_entry(t, &pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 t->control |= (T_RUN);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002960
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08002961 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962
Luiz Capitulino222f1802006-03-20 22:16:13 -08002963 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
2964
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 pktgen_wait_all_threads_run();
2966}
2967
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
2969{
Luiz Capitulino222f1802006-03-20 22:16:13 -08002970 __u64 total_us, bps, mbps, pps, idle;
2971 char *p = pkt_dev->result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972
Luiz Capitulino222f1802006-03-20 22:16:13 -08002973 total_us = pkt_dev->stopped_at - pkt_dev->started_at;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974
Luiz Capitulino222f1802006-03-20 22:16:13 -08002975 idle = pkt_dev->idle_acc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976
Luiz Capitulino222f1802006-03-20 22:16:13 -08002977 p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
2978 (unsigned long long)total_us,
2979 (unsigned long long)(total_us - idle),
2980 (unsigned long long)idle,
2981 (unsigned long long)pkt_dev->sofar,
2982 pkt_dev->cur_pkt_size, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983
Luiz Capitulino222f1802006-03-20 22:16:13 -08002984 pps = pkt_dev->sofar * USEC_PER_SEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985
Luiz Capitulino222f1802006-03-20 22:16:13 -08002986 while ((total_us >> 32) != 0) {
2987 pps >>= 1;
2988 total_us >>= 1;
2989 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990
Luiz Capitulino222f1802006-03-20 22:16:13 -08002991 do_div(pps, total_us);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992
Luiz Capitulino222f1802006-03-20 22:16:13 -08002993 bps = pps * 8 * pkt_dev->cur_pkt_size;
2994
2995 mbps = bps;
2996 do_div(mbps, 1000000);
2997 p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu",
2998 (unsigned long long)pps,
2999 (unsigned long long)mbps,
3000 (unsigned long long)bps,
3001 (unsigned long long)pkt_dev->errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003
3004/* Set stopped-at timer, remove from running list, do counters & statistics */
3005
Luiz Capitulino222f1802006-03-20 22:16:13 -08003006static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003008 int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003009
Luiz Capitulino222f1802006-03-20 22:16:13 -08003010 if (!pkt_dev->running) {
3011 printk("pktgen: interface: %s is already stopped\n",
3012 pkt_dev->ifname);
3013 return -EINVAL;
3014 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015
Luiz Capitulino222f1802006-03-20 22:16:13 -08003016 pkt_dev->stopped_at = getCurUs();
3017 pkt_dev->running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003019 show_results(pkt_dev, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020
Luiz Capitulino222f1802006-03-20 22:16:13 -08003021 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022}
3023
Luiz Capitulino222f1802006-03-20 22:16:13 -08003024static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003026 struct pktgen_dev *pkt_dev, *best = NULL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003027
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 if_lock(t);
3029
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003030 list_for_each_entry(pkt_dev, &t->if_list, list) {
3031 if (!pkt_dev->running)
Luiz Capitulino222f1802006-03-20 22:16:13 -08003032 continue;
3033 if (best == NULL)
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003034 best = pkt_dev;
3035 else if (pkt_dev->next_tx_us < best->next_tx_us)
3036 best = pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 }
3038 if_unlock(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003039 return best;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040}
3041
Luiz Capitulino222f1802006-03-20 22:16:13 -08003042static void pktgen_stop(struct pktgen_thread *t)
3043{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003044 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003046 PG_DEBUG(printk("pktgen: entering pktgen_stop\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047
Luiz Capitulino222f1802006-03-20 22:16:13 -08003048 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003050 list_for_each_entry(pkt_dev, &t->if_list, list) {
3051 pktgen_stop_device(pkt_dev);
3052 if (pkt_dev->skb)
3053 kfree_skb(pkt_dev->skb);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003054
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003055 pkt_dev->skb = NULL;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003056 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057
Luiz Capitulino222f1802006-03-20 22:16:13 -08003058 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059}
3060
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003061/*
3062 * one of our devices needs to be removed - find it
3063 * and remove it
3064 */
3065static void pktgen_rem_one_if(struct pktgen_thread *t)
3066{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003067 struct list_head *q, *n;
3068 struct pktgen_dev *cur;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003069
3070 PG_DEBUG(printk("pktgen: entering pktgen_rem_one_if\n"));
3071
3072 if_lock(t);
3073
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003074 list_for_each_safe(q, n, &t->if_list) {
3075 cur = list_entry(q, struct pktgen_dev, list);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003076
Luiz Capitulino222f1802006-03-20 22:16:13 -08003077 if (!cur->removal_mark)
3078 continue;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003079
3080 if (cur->skb)
3081 kfree_skb(cur->skb);
3082 cur->skb = NULL;
3083
3084 pktgen_remove_device(t, cur);
3085
3086 break;
3087 }
3088
3089 if_unlock(t);
3090}
3091
Luiz Capitulino222f1802006-03-20 22:16:13 -08003092static void pktgen_rem_all_ifs(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003094 struct list_head *q, *n;
3095 struct pktgen_dev *cur;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003096
3097 /* Remove all devices, free mem */
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003098
3099 PG_DEBUG(printk("pktgen: entering pktgen_rem_all_ifs\n"));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003100 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003102 list_for_each_safe(q, n, &t->if_list) {
3103 cur = list_entry(q, struct pktgen_dev, list);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003104
3105 if (cur->skb)
3106 kfree_skb(cur->skb);
3107 cur->skb = NULL;
3108
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 pktgen_remove_device(t, cur);
3110 }
3111
Luiz Capitulino222f1802006-03-20 22:16:13 -08003112 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113}
3114
Luiz Capitulino222f1802006-03-20 22:16:13 -08003115static void pktgen_rem_thread(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003117 /* Remove from the thread list */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003119 remove_proc_entry(t->name, pg_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003121 mutex_lock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003123 list_del(&t->th_list);
3124
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003125 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126}
3127
3128static __inline__ void pktgen_xmit(struct pktgen_dev *pkt_dev)
3129{
3130 struct net_device *odev = NULL;
3131 __u64 idle_start = 0;
3132 int ret;
3133
3134 odev = pkt_dev->odev;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003135
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 if (pkt_dev->delay_us || pkt_dev->delay_ns) {
3137 u64 now;
3138
3139 now = getCurUs();
3140 if (now < pkt_dev->next_tx_us)
3141 spin(pkt_dev, pkt_dev->next_tx_us);
3142
3143 /* This is max DELAY, this has special meaning of
3144 * "never transmit"
3145 */
3146 if (pkt_dev->delay_us == 0x7FFFFFFF) {
3147 pkt_dev->next_tx_us = getCurUs() + pkt_dev->delay_us;
3148 pkt_dev->next_tx_ns = pkt_dev->delay_ns;
3149 goto out;
3150 }
3151 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003152
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 if (netif_queue_stopped(odev) || need_resched()) {
3154 idle_start = getCurUs();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003155
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 if (!netif_running(odev)) {
3157 pktgen_stop_device(pkt_dev);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003158 if (pkt_dev->skb)
3159 kfree_skb(pkt_dev->skb);
3160 pkt_dev->skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 goto out;
3162 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003163 if (need_resched())
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 schedule();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003165
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 pkt_dev->idle_acc += getCurUs() - idle_start;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003167
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 if (netif_queue_stopped(odev)) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08003169 pkt_dev->next_tx_us = getCurUs(); /* TODO */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170 pkt_dev->next_tx_ns = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003171 goto out; /* Try the next interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 }
3173 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003174
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 if (pkt_dev->last_ok || !pkt_dev->skb) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08003176 if ((++pkt_dev->clone_count >= pkt_dev->clone_skb)
3177 || (!pkt_dev->skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 /* build a new pkt */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003179 if (pkt_dev->skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 kfree_skb(pkt_dev->skb);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003181
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 pkt_dev->skb = fill_packet(odev, pkt_dev);
3183 if (pkt_dev->skb == NULL) {
3184 printk("pktgen: ERROR: couldn't allocate skb in fill_packet.\n");
3185 schedule();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003186 pkt_dev->clone_count--; /* back out increment, OOM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 goto out;
3188 }
3189 pkt_dev->allocated_skbs++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003190 pkt_dev->clone_count = 0; /* reset counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 }
3192 }
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003193
Herbert Xu932ff272006-06-09 12:20:56 -07003194 netif_tx_lock_bh(odev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 if (!netif_queue_stopped(odev)) {
3196
3197 atomic_inc(&(pkt_dev->skb->users));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003198 retry_now:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 ret = odev->hard_start_xmit(pkt_dev->skb, odev);
3200 if (likely(ret == NETDEV_TX_OK)) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08003201 pkt_dev->last_ok = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 pkt_dev->sofar++;
3203 pkt_dev->seq_num++;
3204 pkt_dev->tx_bytes += pkt_dev->cur_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003205
3206 } else if (ret == NETDEV_TX_LOCKED
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 && (odev->features & NETIF_F_LLTX)) {
3208 cpu_relax();
3209 goto retry_now;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003210 } else { /* Retry it next time */
3211
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 atomic_dec(&(pkt_dev->skb->users));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003213
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 if (debug && net_ratelimit())
3215 printk(KERN_INFO "pktgen: Hard xmit error\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003216
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 pkt_dev->errors++;
3218 pkt_dev->last_ok = 0;
3219 }
3220
3221 pkt_dev->next_tx_us = getCurUs();
3222 pkt_dev->next_tx_ns = 0;
3223
3224 pkt_dev->next_tx_us += pkt_dev->delay_us;
3225 pkt_dev->next_tx_ns += pkt_dev->delay_ns;
3226
3227 if (pkt_dev->next_tx_ns > 1000) {
3228 pkt_dev->next_tx_us++;
3229 pkt_dev->next_tx_ns -= 1000;
3230 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232
Luiz Capitulino222f1802006-03-20 22:16:13 -08003233 else { /* Retry it next time */
3234 pkt_dev->last_ok = 0;
3235 pkt_dev->next_tx_us = getCurUs(); /* TODO */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 pkt_dev->next_tx_ns = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238
Herbert Xu932ff272006-06-09 12:20:56 -07003239 netif_tx_unlock_bh(odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003240
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 /* If pkt_dev->count is zero, then run forever */
3242 if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
3243 if (atomic_read(&(pkt_dev->skb->users)) != 1) {
3244 idle_start = getCurUs();
3245 while (atomic_read(&(pkt_dev->skb->users)) != 1) {
3246 if (signal_pending(current)) {
3247 break;
3248 }
3249 schedule();
3250 }
3251 pkt_dev->idle_acc += getCurUs() - idle_start;
3252 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003253
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 /* Done with this */
3255 pktgen_stop_device(pkt_dev);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003256 if (pkt_dev->skb)
3257 kfree_skb(pkt_dev->skb);
3258 pkt_dev->skb = NULL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003259 }
3260out:;
3261}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262
3263/*
3264 * Main loop of the thread goes here
3265 */
3266
Luiz Capitulino222f1802006-03-20 22:16:13 -08003267static void pktgen_thread_worker(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268{
3269 DEFINE_WAIT(wait);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003270 struct pktgen_dev *pkt_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 int cpu = t->cpu;
3272 sigset_t tmpsig;
3273 u32 max_before_softirq;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003274 u32 tx_since_softirq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275
3276 daemonize("pktgen/%d", cpu);
3277
Luiz Capitulino222f1802006-03-20 22:16:13 -08003278 /* Block all signals except SIGKILL, SIGSTOP and SIGTERM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279
Luiz Capitulino222f1802006-03-20 22:16:13 -08003280 spin_lock_irq(&current->sighand->siglock);
3281 tmpsig = current->blocked;
3282 siginitsetinv(&current->blocked,
3283 sigmask(SIGKILL) | sigmask(SIGSTOP) | sigmask(SIGTERM));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284
Luiz Capitulino222f1802006-03-20 22:16:13 -08003285 recalc_sigpending();
3286 spin_unlock_irq(&current->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287
3288 /* Migrate to the right CPU */
3289 set_cpus_allowed(current, cpumask_of_cpu(cpu));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003290 if (smp_processor_id() != cpu)
3291 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292
3293 init_waitqueue_head(&t->queue);
3294
3295 t->control &= ~(T_TERMINATE);
3296 t->control &= ~(T_RUN);
3297 t->control &= ~(T_STOP);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003298 t->control &= ~(T_REMDEVALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 t->control &= ~(T_REMDEV);
3300
Luiz Capitulino222f1802006-03-20 22:16:13 -08003301 t->pid = current->pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302
Luiz Capitulino222f1802006-03-20 22:16:13 -08003303 PG_DEBUG(printk("pktgen: starting pktgen/%d: pid=%d\n", cpu, current->pid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304
3305 max_before_softirq = t->max_before_softirq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306
Luiz Capitulino222f1802006-03-20 22:16:13 -08003307 __set_current_state(TASK_INTERRUPTIBLE);
3308 mb();
3309
3310 while (1) {
3311
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 __set_current_state(TASK_RUNNING);
3313
3314 /*
3315 * Get next dev to xmit -- if any.
3316 */
3317
Luiz Capitulino222f1802006-03-20 22:16:13 -08003318 pkt_dev = next_to_run(t);
3319
3320 if (pkt_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321
3322 pktgen_xmit(pkt_dev);
3323
3324 /*
3325 * We like to stay RUNNING but must also give
3326 * others fair share.
3327 */
3328
3329 tx_since_softirq += pkt_dev->last_ok;
3330
3331 if (tx_since_softirq > max_before_softirq) {
3332 if (local_softirq_pending())
3333 do_softirq();
3334 tx_since_softirq = 0;
3335 }
3336 } else {
3337 prepare_to_wait(&(t->queue), &wait, TASK_INTERRUPTIBLE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003338 schedule_timeout(HZ / 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 finish_wait(&(t->queue), &wait);
3340 }
3341
Luiz Capitulino222f1802006-03-20 22:16:13 -08003342 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 * Back from sleep, either due to the timeout or signal.
3344 * We check if we have any "posted" work for us.
3345 */
3346
Luiz Capitulino222f1802006-03-20 22:16:13 -08003347 if (t->control & T_TERMINATE || signal_pending(current))
3348 /* we received a request to terminate ourself */
3349 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350
Luiz Capitulino222f1802006-03-20 22:16:13 -08003351 if (t->control & T_STOP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 pktgen_stop(t);
3353 t->control &= ~(T_STOP);
3354 }
3355
Luiz Capitulino222f1802006-03-20 22:16:13 -08003356 if (t->control & T_RUN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 pktgen_run(t);
3358 t->control &= ~(T_RUN);
3359 }
3360
Luiz Capitulino222f1802006-03-20 22:16:13 -08003361 if (t->control & T_REMDEVALL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362 pktgen_rem_all_ifs(t);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003363 t->control &= ~(T_REMDEVALL);
3364 }
3365
Luiz Capitulino222f1802006-03-20 22:16:13 -08003366 if (t->control & T_REMDEV) {
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003367 pktgen_rem_one_if(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 t->control &= ~(T_REMDEV);
3369 }
3370
Luiz Capitulino222f1802006-03-20 22:16:13 -08003371 if (need_resched())
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 schedule();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003373 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374
Luiz Capitulino222f1802006-03-20 22:16:13 -08003375 PG_DEBUG(printk("pktgen: %s stopping all device\n", t->name));
3376 pktgen_stop(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377
Luiz Capitulino222f1802006-03-20 22:16:13 -08003378 PG_DEBUG(printk("pktgen: %s removing all device\n", t->name));
3379 pktgen_rem_all_ifs(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380
Luiz Capitulino222f1802006-03-20 22:16:13 -08003381 PG_DEBUG(printk("pktgen: %s removing thread.\n", t->name));
3382 pktgen_rem_thread(t);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003383
3384 t->removed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385}
3386
Luiz Capitulino222f1802006-03-20 22:16:13 -08003387static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
3388 const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003390 struct pktgen_dev *p, *pkt_dev = NULL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003391 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003393 list_for_each_entry(p, &t->if_list, list)
3394 if (strncmp(p->ifname, ifname, IFNAMSIZ) == 0) {
3395 pkt_dev = p;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003396 break;
3397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398
Luiz Capitulino222f1802006-03-20 22:16:13 -08003399 if_unlock(t);
3400 PG_DEBUG(printk("pktgen: find_dev(%s) returning %p\n", ifname, pkt_dev));
3401 return pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402}
3403
3404/*
3405 * Adds a dev at front of if_list.
3406 */
3407
Luiz Capitulino222f1802006-03-20 22:16:13 -08003408static int add_dev_to_thread(struct pktgen_thread *t,
3409 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410{
3411 int rv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412
Luiz Capitulino222f1802006-03-20 22:16:13 -08003413 if_lock(t);
3414
3415 if (pkt_dev->pg_thread) {
3416 printk("pktgen: ERROR: already assigned to a thread.\n");
3417 rv = -EBUSY;
3418 goto out;
3419 }
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003420
3421 list_add(&pkt_dev->list, &t->if_list);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003422 pkt_dev->pg_thread = t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 pkt_dev->running = 0;
3424
Luiz Capitulino222f1802006-03-20 22:16:13 -08003425out:
3426 if_unlock(t);
3427 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428}
3429
3430/* Called under thread lock */
3431
Luiz Capitulino222f1802006-03-20 22:16:13 -08003432static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003434 struct pktgen_dev *pkt_dev;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003435 struct proc_dir_entry *pe;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003436
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437 /* We don't allow a device to be on several threads */
3438
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003439 pkt_dev = __pktgen_NN_threads(ifname, FIND);
3440 if (pkt_dev) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08003441 printk("pktgen: ERROR: interface already used.\n");
3442 return -EBUSY;
3443 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003444
3445 pkt_dev = kzalloc(sizeof(struct pktgen_dev), GFP_KERNEL);
3446 if (!pkt_dev)
3447 return -ENOMEM;
3448
Luiz Capitulino222f1802006-03-20 22:16:13 -08003449 pkt_dev->flows = vmalloc(MAX_CFLOWS * sizeof(struct flow_state));
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003450 if (pkt_dev->flows == NULL) {
3451 kfree(pkt_dev);
3452 return -ENOMEM;
3453 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003454 memset(pkt_dev->flows, 0, MAX_CFLOWS * sizeof(struct flow_state));
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003455
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003456 pkt_dev->removal_mark = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003457 pkt_dev->min_pkt_size = ETH_ZLEN;
3458 pkt_dev->max_pkt_size = ETH_ZLEN;
3459 pkt_dev->nfrags = 0;
3460 pkt_dev->clone_skb = pg_clone_skb_d;
3461 pkt_dev->delay_us = pg_delay_d / 1000;
3462 pkt_dev->delay_ns = pg_delay_d % 1000;
3463 pkt_dev->count = pg_count_d;
3464 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003465 pkt_dev->udp_src_min = 9; /* sink port */
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003466 pkt_dev->udp_src_max = 9;
3467 pkt_dev->udp_dst_min = 9;
3468 pkt_dev->udp_dst_max = 9;
3469
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003470 pkt_dev->vlan_p = 0;
3471 pkt_dev->vlan_cfi = 0;
3472 pkt_dev->vlan_id = 0xffff;
3473 pkt_dev->svlan_p = 0;
3474 pkt_dev->svlan_cfi = 0;
3475 pkt_dev->svlan_id = 0xffff;
3476
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003477 strncpy(pkt_dev->ifname, ifname, IFNAMSIZ);
3478
Luiz Capitulino222f1802006-03-20 22:16:13 -08003479 if (!pktgen_setup_dev(pkt_dev)) {
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003480 printk("pktgen: ERROR: pktgen_setup_dev failed.\n");
3481 if (pkt_dev->flows)
3482 vfree(pkt_dev->flows);
3483 kfree(pkt_dev);
3484 return -ENODEV;
3485 }
3486
3487 pe = create_proc_entry(ifname, 0600, pg_proc_dir);
3488 if (!pe) {
3489 printk("pktgen: cannot create %s/%s procfs entry.\n",
3490 PG_PROC_DIR, ifname);
3491 if (pkt_dev->flows)
3492 vfree(pkt_dev->flows);
3493 kfree(pkt_dev);
3494 return -EINVAL;
3495 }
3496 pe->proc_fops = &pktgen_if_fops;
3497 pe->data = pkt_dev;
3498
3499 return add_dev_to_thread(t, pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500}
3501
Luiz Capitulino222f1802006-03-20 22:16:13 -08003502static struct pktgen_thread *__init pktgen_find_thread(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003504 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003506 mutex_lock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003508 list_for_each_entry(t, &pktgen_threads, th_list)
3509 if (strcmp(t->name, name) == 0) {
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003510 mutex_unlock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003511 return t;
3512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003514 mutex_unlock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003515 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516}
3517
Luiz Capitulino222f1802006-03-20 22:16:13 -08003518static int __init pktgen_create_thread(const char *name, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519{
Luiz Capitulino12e18722006-03-20 22:17:00 -08003520 int err;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003521 struct pktgen_thread *t = NULL;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003522 struct proc_dir_entry *pe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523
Luiz Capitulino222f1802006-03-20 22:16:13 -08003524 if (strlen(name) > 31) {
3525 printk("pktgen: ERROR: Thread name cannot be more than 31 characters.\n");
3526 return -EINVAL;
3527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528
Luiz Capitulino222f1802006-03-20 22:16:13 -08003529 if (pktgen_find_thread(name)) {
3530 printk("pktgen: ERROR: thread: %s already exists\n", name);
3531 return -EINVAL;
3532 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533
Luiz Capitulino222f1802006-03-20 22:16:13 -08003534 t = kzalloc(sizeof(struct pktgen_thread), GFP_KERNEL);
3535 if (!t) {
3536 printk("pktgen: ERROR: out of memory, can't create new thread.\n");
3537 return -ENOMEM;
3538 }
3539
3540 strcpy(t->name, name);
3541 spin_lock_init(&t->if_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542 t->cpu = cpu;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003543
3544 pe = create_proc_entry(t->name, 0600, pg_proc_dir);
3545 if (!pe) {
3546 printk("pktgen: cannot create %s/%s procfs entry.\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003547 PG_PROC_DIR, t->name);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003548 kfree(t);
3549 return -EINVAL;
3550 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003551
3552 pe->proc_fops = &pktgen_thread_fops;
3553 pe->data = t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003555 INIT_LIST_HEAD(&t->if_list);
3556
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003557 list_add_tail(&t->th_list, &pktgen_threads);
3558
3559 t->removed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560
Luiz Capitulino12e18722006-03-20 22:17:00 -08003561 err = kernel_thread((void *)pktgen_thread_worker, (void *)t,
3562 CLONE_FS | CLONE_FILES | CLONE_SIGHAND);
3563 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 printk("pktgen: kernel_thread() failed for cpu %d\n", t->cpu);
Luiz Capitulino12e18722006-03-20 22:17:00 -08003565 remove_proc_entry(t->name, pg_proc_dir);
3566 list_del(&t->th_list);
3567 kfree(t);
3568 return err;
3569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570
3571 return 0;
3572}
3573
3574/*
3575 * Removes a device from the thread if_list.
3576 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003577static void _rem_dev_from_if_list(struct pktgen_thread *t,
3578 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003580 struct list_head *q, *n;
3581 struct pktgen_dev *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003583 list_for_each_safe(q, n, &t->if_list) {
3584 p = list_entry(q, struct pktgen_dev, list);
3585 if (p == pkt_dev)
3586 list_del(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587 }
3588}
3589
Luiz Capitulino222f1802006-03-20 22:16:13 -08003590static int pktgen_remove_device(struct pktgen_thread *t,
3591 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592{
3593
3594 PG_DEBUG(printk("pktgen: remove_device pkt_dev=%p\n", pkt_dev));
3595
Luiz Capitulino222f1802006-03-20 22:16:13 -08003596 if (pkt_dev->running) {
3597 printk("pktgen:WARNING: trying to remove a running interface, stopping it now.\n");
3598 pktgen_stop_device(pkt_dev);
3599 }
3600
3601 /* Dis-associate from the interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602
3603 if (pkt_dev->odev) {
3604 dev_put(pkt_dev->odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003605 pkt_dev->odev = NULL;
3606 }
3607
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608 /* And update the thread if_list */
3609
3610 _rem_dev_from_if_list(t, pkt_dev);
3611
Luiz Capitulino222f1802006-03-20 22:16:13 -08003612 /* Clean up proc file system */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003614 remove_proc_entry(pkt_dev->ifname, pg_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615
3616 if (pkt_dev->flows)
3617 vfree(pkt_dev->flows);
3618 kfree(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003619 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620}
3621
Luiz Capitulino222f1802006-03-20 22:16:13 -08003622static int __init pg_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623{
3624 int cpu;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003625 struct proc_dir_entry *pe;
3626
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627 printk(version);
3628
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003629 pg_proc_dir = proc_mkdir(PG_PROC_DIR, proc_net);
3630 if (!pg_proc_dir)
3631 return -ENODEV;
3632 pg_proc_dir->owner = THIS_MODULE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003634 pe = create_proc_entry(PGCTRL, 0600, pg_proc_dir);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003635 if (pe == NULL) {
3636 printk("pktgen: ERROR: cannot create %s procfs entry.\n",
3637 PGCTRL);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003638 proc_net_remove(PG_PROC_DIR);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003639 return -EINVAL;
3640 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641
Luiz Capitulino222f1802006-03-20 22:16:13 -08003642 pe->proc_fops = &pktgen_fops;
3643 pe->data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644
3645 /* Register us to receive netdevice events */
3646 register_netdevice_notifier(&pktgen_notifier_block);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003647
John Hawkes670c02c2005-10-13 09:30:31 -07003648 for_each_online_cpu(cpu) {
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003649 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 char buf[30];
3651
Luiz Capitulino222f1802006-03-20 22:16:13 -08003652 sprintf(buf, "kpktgend_%i", cpu);
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003653 err = pktgen_create_thread(buf, cpu);
3654 if (err)
3655 printk("pktgen: WARNING: Cannot create thread for cpu %d (%d)\n",
3656 cpu, err);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003657 }
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003658
3659 if (list_empty(&pktgen_threads)) {
3660 printk("pktgen: ERROR: Initialization failed for all threads\n");
3661 unregister_netdevice_notifier(&pktgen_notifier_block);
3662 remove_proc_entry(PGCTRL, pg_proc_dir);
3663 proc_net_remove(PG_PROC_DIR);
3664 return -ENODEV;
3665 }
3666
Luiz Capitulino222f1802006-03-20 22:16:13 -08003667 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668}
3669
3670static void __exit pg_cleanup(void)
3671{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003672 struct pktgen_thread *t;
3673 struct list_head *q, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674 wait_queue_head_t queue;
3675 init_waitqueue_head(&queue);
3676
Luiz Capitulino222f1802006-03-20 22:16:13 -08003677 /* Stop all interfaces & threads */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003679 list_for_each_safe(q, n, &pktgen_threads) {
3680 t = list_entry(q, struct pktgen_thread, th_list);
3681 t->control |= (T_TERMINATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003683 wait_event_interruptible_timeout(queue, (t->removed == 1), HZ);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003684 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685
Luiz Capitulino222f1802006-03-20 22:16:13 -08003686 /* Un-register us from receiving netdevice events */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687 unregister_netdevice_notifier(&pktgen_notifier_block);
3688
Luiz Capitulino222f1802006-03-20 22:16:13 -08003689 /* Clean up proc file system */
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003690 remove_proc_entry(PGCTRL, pg_proc_dir);
3691 proc_net_remove(PG_PROC_DIR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692}
3693
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694module_init(pg_init);
3695module_exit(pg_cleanup);
3696
3697MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se");
3698MODULE_DESCRIPTION("Packet Generator tool");
3699MODULE_LICENSE("GPL");
3700module_param(pg_count_d, int, 0);
3701module_param(pg_delay_d, int, 0);
3702module_param(pg_clone_skb_d, int, 0);
3703module_param(debug, int, 0);