blob: eef1392b7f8e3e62170ec0df8927dc6b5e1ae0ec [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
109 */
110#include <linux/sys.h>
111#include <linux/types.h>
112#include <linux/module.h>
113#include <linux/moduleparam.h>
114#include <linux/kernel.h>
115#include <linux/smp_lock.h>
116#include <linux/sched.h>
117#include <linux/slab.h>
118#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119#include <linux/unistd.h>
120#include <linux/string.h>
121#include <linux/ptrace.h>
122#include <linux/errno.h>
123#include <linux/ioport.h>
124#include <linux/interrupt.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -0800125#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126#include <linux/delay.h>
127#include <linux/timer.h>
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -0800128#include <linux/list.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129#include <linux/init.h>
130#include <linux/skbuff.h>
131#include <linux/netdevice.h>
132#include <linux/inet.h>
133#include <linux/inetdevice.h>
134#include <linux/rtnetlink.h>
135#include <linux/if_arp.h>
136#include <linux/in.h>
137#include <linux/ip.h>
138#include <linux/ipv6.h>
139#include <linux/udp.h>
140#include <linux/proc_fs.h>
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700141#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142#include <linux/wait.h>
Kris Katterjohnf404e9a2006-01-17 13:04:57 -0800143#include <linux/etherdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144#include <net/checksum.h>
145#include <net/ipv6.h>
146#include <net/addrconf.h>
147#include <asm/byteorder.h>
148#include <linux/rcupdate.h>
149#include <asm/bitops.h>
150#include <asm/io.h>
151#include <asm/dma.h>
152#include <asm/uaccess.h>
Luiz Capitulino222f1802006-03-20 22:16:13 -0800153#include <asm/div64.h> /* do_div */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154#include <asm/timex.h>
155
Arthur Kepner95ed63f2006-03-20 21:26:56 -0800156#define VERSION "pktgen v2.64: Packet Generator for packet performance testing.\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158/* #define PG_DEBUG(a) a */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800159#define PG_DEBUG(a)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
161/* The buckets are exponential in 'width' */
162#define LAT_BUCKETS_MAX 32
163#define IP_NAME_SZ 32
164
165/* Device flag bits */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800166#define F_IPSRC_RND (1<<0) /* IP-Src Random */
167#define F_IPDST_RND (1<<1) /* IP-Dst Random */
168#define F_UDPSRC_RND (1<<2) /* UDP-Src Random */
169#define F_UDPDST_RND (1<<3) /* UDP-Dst Random */
170#define F_MACSRC_RND (1<<4) /* MAC-Src Random */
171#define F_MACDST_RND (1<<5) /* MAC-Dst Random */
172#define F_TXSIZE_RND (1<<6) /* Transmit size is random */
173#define F_IPV6 (1<<7) /* Interface in IPV6 Mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175/* Thread control flag bits */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800176#define T_TERMINATE (1<<0)
177#define T_STOP (1<<1) /* Stop run */
178#define T_RUN (1<<2) /* Start run */
179#define T_REMDEVALL (1<<3) /* Remove all devs */
180#define T_REMDEV (1<<4) /* Remove one dev */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182/* Locks */
Stephen Hemmingerb7c89212005-10-14 15:26:34 -0700183#define thread_lock() down(&pktgen_sem)
184#define thread_unlock() up(&pktgen_sem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186/* If lock -- can be removed after some work */
187#define if_lock(t) spin_lock(&(t->if_lock));
188#define if_unlock(t) spin_unlock(&(t->if_lock));
189
190/* Used to help with determining the pkts on receive */
191#define PKTGEN_MAGIC 0xbe9be955
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700192#define PG_PROC_DIR "pktgen"
193#define PGCTRL "pgctrl"
194static struct proc_dir_entry *pg_proc_dir = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196#define MAX_CFLOWS 65536
197
Luiz Capitulino222f1802006-03-20 22:16:13 -0800198struct flow_state {
199 __u32 cur_daddr;
200 int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201};
202
203struct pktgen_dev {
204
205 /*
206 * Try to keep frequent/infrequent used vars. separated.
207 */
208
Luiz Capitulino222f1802006-03-20 22:16:13 -0800209 char ifname[IFNAMSIZ];
210 char result[512];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Luiz Capitulino222f1802006-03-20 22:16:13 -0800212 struct pktgen_thread *pg_thread; /* the owner */
213 struct pktgen_dev *next; /* Used for chaining in the thread's run-queue */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Luiz Capitulino222f1802006-03-20 22:16:13 -0800215 int running; /* if this changes to false, the test will stop */
216
217 /* If min != max, then we will either do a linear iteration, or
218 * we will do a random selection from within the range.
219 */
220 __u32 flags;
Arthur Kepner95ed63f2006-03-20 21:26:56 -0800221 int removal_mark; /* non-zero => the device is marked for
222 * removal by worker thread */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Luiz Capitulino222f1802006-03-20 22:16:13 -0800224 int min_pkt_size; /* = ETH_ZLEN; */
225 int max_pkt_size; /* = ETH_ZLEN; */
226 int nfrags;
227 __u32 delay_us; /* Default delay */
228 __u32 delay_ns;
229 __u64 count; /* Default No packets to send */
230 __u64 sofar; /* How many pkts we've sent so far */
231 __u64 tx_bytes; /* How many bytes we've transmitted */
232 __u64 errors; /* Errors when trying to transmit, pkts will be re-sent */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Luiz Capitulino222f1802006-03-20 22:16:13 -0800234 /* runtime counters relating to clone_skb */
235 __u64 next_tx_us; /* timestamp of when to tx next */
236 __u32 next_tx_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Luiz Capitulino222f1802006-03-20 22:16:13 -0800238 __u64 allocated_skbs;
239 __u32 clone_count;
240 int last_ok; /* Was last skb sent?
241 * Or a failed transmit of some sort? This will keep
242 * sequence numbers in order, for example.
243 */
244 __u64 started_at; /* micro-seconds */
245 __u64 stopped_at; /* micro-seconds */
246 __u64 idle_acc; /* micro-seconds */
247 __u32 seq_num;
248
249 int clone_skb; /* Use multiple SKBs during packet gen. If this number
250 * is greater than 1, then that many copies of the same
251 * packet will be sent before a new packet is allocated.
252 * For instance, if you want to send 1024 identical packets
253 * before creating a new packet, set clone_skb to 1024.
254 */
255
256 char dst_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
257 char dst_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
258 char src_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
259 char src_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
260
261 struct in6_addr in6_saddr;
262 struct in6_addr in6_daddr;
263 struct in6_addr cur_in6_daddr;
264 struct in6_addr cur_in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 /* For ranges */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800266 struct in6_addr min_in6_daddr;
267 struct in6_addr max_in6_daddr;
268 struct in6_addr min_in6_saddr;
269 struct in6_addr max_in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Luiz Capitulino222f1802006-03-20 22:16:13 -0800271 /* If we're doing ranges, random or incremental, then this
272 * defines the min/max for those ranges.
273 */
274 __u32 saddr_min; /* inclusive, source IP address */
275 __u32 saddr_max; /* exclusive, source IP address */
276 __u32 daddr_min; /* inclusive, dest IP address */
277 __u32 daddr_max; /* exclusive, dest IP address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Luiz Capitulino222f1802006-03-20 22:16:13 -0800279 __u16 udp_src_min; /* inclusive, source UDP port */
280 __u16 udp_src_max; /* exclusive, source UDP port */
281 __u16 udp_dst_min; /* inclusive, dest UDP port */
282 __u16 udp_dst_max; /* exclusive, dest UDP port */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Luiz Capitulino222f1802006-03-20 22:16:13 -0800284 __u32 src_mac_count; /* How many MACs to iterate through */
285 __u32 dst_mac_count; /* How many MACs to iterate through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Luiz Capitulino222f1802006-03-20 22:16:13 -0800287 unsigned char dst_mac[ETH_ALEN];
288 unsigned char src_mac[ETH_ALEN];
289
290 __u32 cur_dst_mac_offset;
291 __u32 cur_src_mac_offset;
292 __u32 cur_saddr;
293 __u32 cur_daddr;
294 __u16 cur_udp_dst;
295 __u16 cur_udp_src;
296 __u32 cur_pkt_size;
297
298 __u8 hh[14];
299 /* = {
300 0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
301
302 We fill in SRC address later
303 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
304 0x08, 0x00
305 };
306 */
307 __u16 pad; /* pad out the hh struct to an even 16 bytes */
308
309 struct sk_buff *skb; /* skb we are to transmit next, mainly used for when we
310 * are transmitting the same one multiple times
311 */
312 struct net_device *odev; /* The out-going device. Note that the device should
313 * have it's pg_info pointer pointing back to this
314 * device. This will be set when the user specifies
315 * the out-going device name (not when the inject is
316 * started as it used to do.)
317 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 struct flow_state *flows;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800319 unsigned cflows; /* Concurrent flows (config) */
320 unsigned lflow; /* Flow length (config) */
321 unsigned nflows; /* accumulated flows (stats) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322};
323
324struct pktgen_hdr {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800325 __u32 pgh_magic;
326 __u32 seq_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 __u32 tv_sec;
328 __u32 tv_usec;
329};
330
331struct pktgen_thread {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800332 spinlock_t if_lock;
333 struct pktgen_dev *if_list; /* All device here */
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -0800334 struct list_head th_list;
335 int removed;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800336 char name[32];
337 char result[512];
338 u32 max_before_softirq; /* We'll call do_softirq to prevent starvation. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Luiz Capitulino222f1802006-03-20 22:16:13 -0800340 /* Field for thread to receive "posted" events terminate, stop ifs etc. */
341
342 u32 control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 int pid;
344 int cpu;
345
Luiz Capitulino222f1802006-03-20 22:16:13 -0800346 wait_queue_head_t queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347};
348
349#define REMOVE 1
350#define FIND 0
351
352/* This code works around the fact that do_div cannot handle two 64-bit
353 numbers, and regular 64-bit division doesn't work on x86 kernels.
354 --Ben
355*/
356
357#define PG_DIV 0
358
359/* This was emailed to LMKL by: Chris Caputo <ccaputo@alt.net>
360 * Function copied/adapted/optimized from:
361 *
362 * nemesis.sourceforge.net/browse/lib/static/intmath/ix86/intmath.c.html
363 *
364 * Copyright 1994, University of Cambridge Computer Laboratory
365 * All Rights Reserved.
366 *
367 */
Jesper Juhl77933d72005-07-27 11:46:09 -0700368static inline s64 divremdi3(s64 x, s64 y, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800370 u64 a = (x < 0) ? -x : x;
371 u64 b = (y < 0) ? -y : y;
372 u64 res = 0, d = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Luiz Capitulino222f1802006-03-20 22:16:13 -0800374 if (b > 0) {
375 while (b < a) {
376 b <<= 1;
377 d <<= 1;
378 }
379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Luiz Capitulino222f1802006-03-20 22:16:13 -0800381 do {
382 if (a >= b) {
383 a -= b;
384 res += d;
385 }
386 b >>= 1;
387 d >>= 1;
388 }
389 while (d);
390
391 if (PG_DIV == type) {
392 return (((x ^ y) & (1ll << 63)) == 0) ? res : -(s64) res;
393 } else {
394 return ((x & (1ll << 63)) == 0) ? a : -(s64) a;
395 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396}
397
398/* End of hacks to deal with 64-bit math on x86 */
399
Stephen Hemmingerb4099fa2005-10-14 15:32:22 -0700400/** Convert to milliseconds */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800401static inline __u64 tv_to_ms(const struct timeval *tv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800403 __u64 ms = tv->tv_usec / 1000;
404 ms += (__u64) tv->tv_sec * (__u64) 1000;
405 return ms;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406}
407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408/** Convert to micro-seconds */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800409static inline __u64 tv_to_us(const struct timeval *tv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800411 __u64 us = tv->tv_usec;
412 us += (__u64) tv->tv_sec * (__u64) 1000000;
413 return us;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414}
415
Luiz Capitulino222f1802006-03-20 22:16:13 -0800416static inline __u64 pg_div(__u64 n, __u32 base)
417{
418 __u64 tmp = n;
419 do_div(tmp, base);
420 /* printk("pktgen: pg_div, n: %llu base: %d rv: %llu\n",
421 n, base, tmp); */
422 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423}
424
Luiz Capitulino222f1802006-03-20 22:16:13 -0800425static inline __u64 pg_div64(__u64 n, __u64 base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800427 __u64 tmp = n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428/*
Stephen Hemmingerb4099fa2005-10-14 15:32:22 -0700429 * How do we know if the architecture we are running on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 * supports division with 64 bit base?
431 *
432 */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800433#if defined(__sparc_v9__) || defined(__powerpc64__) || defined(__alpha__) || defined(__x86_64__) || defined(__ia64__)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Luiz Capitulino222f1802006-03-20 22:16:13 -0800435 do_div(tmp, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436#else
Luiz Capitulino222f1802006-03-20 22:16:13 -0800437 tmp = divremdi3(n, base, PG_DIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438#endif
Luiz Capitulino222f1802006-03-20 22:16:13 -0800439 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440}
441
442static inline u32 pktgen_random(void)
443{
444#if 0
445 __u32 n;
446 get_random_bytes(&n, 4);
447 return n;
448#else
449 return net_random();
450#endif
451}
452
Luiz Capitulino222f1802006-03-20 22:16:13 -0800453static inline __u64 getCurMs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800455 struct timeval tv;
456 do_gettimeofday(&tv);
457 return tv_to_ms(&tv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458}
459
Luiz Capitulino222f1802006-03-20 22:16:13 -0800460static inline __u64 getCurUs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800462 struct timeval tv;
463 do_gettimeofday(&tv);
464 return tv_to_us(&tv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465}
466
Luiz Capitulino222f1802006-03-20 22:16:13 -0800467static inline __u64 tv_diff(const struct timeval *a, const struct timeval *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800469 return tv_to_us(a) - tv_to_us(b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470}
471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472/* old include end */
473
474static char version[] __initdata = VERSION;
475
Luiz Capitulino222f1802006-03-20 22:16:13 -0800476static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *i);
477static int pktgen_add_device(struct pktgen_thread *t, const char *ifname);
478static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
479 const char *ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480static int pktgen_device_event(struct notifier_block *, unsigned long, void *);
481static void pktgen_run_all_threads(void);
482static void pktgen_stop_all_threads_ifs(void);
483static int pktgen_stop_device(struct pktgen_dev *pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800484static void pktgen_stop(struct pktgen_thread *t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485static void pktgen_clear_counters(struct pktgen_dev *pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800486static int pktgen_mark_device(const char *ifname);
487static unsigned int scan_ip6(const char *s, char ip[16]);
488static unsigned int fmt_ip6(char *s, const char ip[16]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490/* Module parameters, defaults. */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800491static int pg_count_d = 1000; /* 1000 pkts by default */
Jaco Kroonf34fbb972005-12-22 12:51:46 -0800492static int pg_delay_d;
493static int pg_clone_skb_d;
494static int debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Stephen Hemmingerb7c89212005-10-14 15:26:34 -0700496static DECLARE_MUTEX(pktgen_sem);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -0800497static LIST_HEAD(pktgen_threads);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499static struct notifier_block pktgen_notifier_block = {
500 .notifier_call = pktgen_device_event,
501};
502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503/*
504 * /proc handling functions
505 *
506 */
507
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700508static int pgctrl_show(struct seq_file *seq, void *v)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800509{
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700510 seq_puts(seq, VERSION);
511 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512}
513
Luiz Capitulino222f1802006-03-20 22:16:13 -0800514static ssize_t pgctrl_write(struct file *file, const char __user * buf,
515 size_t count, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 int err = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700518 char data[128];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Luiz Capitulino222f1802006-03-20 22:16:13 -0800520 if (!capable(CAP_NET_ADMIN)) {
521 err = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 goto out;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800523 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700525 if (count > sizeof(data))
526 count = sizeof(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 if (copy_from_user(data, buf, count)) {
Stephen Hemmingerb4099fa2005-10-14 15:32:22 -0700529 err = -EFAULT;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700530 goto out;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800531 }
532 data[count - 1] = 0; /* Make string */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Luiz Capitulino222f1802006-03-20 22:16:13 -0800534 if (!strcmp(data, "stop"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 pktgen_stop_all_threads_ifs();
536
Luiz Capitulino222f1802006-03-20 22:16:13 -0800537 else if (!strcmp(data, "start"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 pktgen_run_all_threads();
539
Luiz Capitulino222f1802006-03-20 22:16:13 -0800540 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 printk("pktgen: Unknown command: %s\n", data);
542
543 err = count;
544
Luiz Capitulino222f1802006-03-20 22:16:13 -0800545out:
546 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547}
548
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700549static int pgctrl_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700551 return single_open(file, pgctrl_show, PDE(inode)->data);
552}
553
554static struct file_operations pktgen_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800555 .owner = THIS_MODULE,
556 .open = pgctrl_open,
557 .read = seq_read,
558 .llseek = seq_lseek,
559 .write = pgctrl_write,
560 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700561};
562
563static int pktgen_if_show(struct seq_file *seq, void *v)
564{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 int i;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800566 struct pktgen_dev *pkt_dev = seq->private;
567 __u64 sa;
568 __u64 stopped;
569 __u64 now = getCurUs();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Luiz Capitulino222f1802006-03-20 22:16:13 -0800571 seq_printf(seq,
572 "Params: count %llu min_pkt_size: %u max_pkt_size: %u\n",
573 (unsigned long long)pkt_dev->count, pkt_dev->min_pkt_size,
574 pkt_dev->max_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Luiz Capitulino222f1802006-03-20 22:16:13 -0800576 seq_printf(seq,
577 " frags: %d delay: %u clone_skb: %d ifname: %s\n",
578 pkt_dev->nfrags,
579 1000 * pkt_dev->delay_us + pkt_dev->delay_ns,
580 pkt_dev->clone_skb, pkt_dev->ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Luiz Capitulino222f1802006-03-20 22:16:13 -0800582 seq_printf(seq, " flows: %u flowlen: %u\n", pkt_dev->cflows,
583 pkt_dev->lflow);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Luiz Capitulino222f1802006-03-20 22:16:13 -0800585 if (pkt_dev->flags & F_IPV6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 char b1[128], b2[128], b3[128];
Luiz Capitulino222f1802006-03-20 22:16:13 -0800587 fmt_ip6(b1, pkt_dev->in6_saddr.s6_addr);
588 fmt_ip6(b2, pkt_dev->min_in6_saddr.s6_addr);
589 fmt_ip6(b3, pkt_dev->max_in6_saddr.s6_addr);
590 seq_printf(seq,
591 " saddr: %s min_saddr: %s max_saddr: %s\n", b1,
592 b2, b3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Luiz Capitulino222f1802006-03-20 22:16:13 -0800594 fmt_ip6(b1, pkt_dev->in6_daddr.s6_addr);
595 fmt_ip6(b2, pkt_dev->min_in6_daddr.s6_addr);
596 fmt_ip6(b3, pkt_dev->max_in6_daddr.s6_addr);
597 seq_printf(seq,
598 " daddr: %s min_daddr: %s max_daddr: %s\n", b1,
599 b2, b3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Luiz Capitulino222f1802006-03-20 22:16:13 -0800601 } else
602 seq_printf(seq,
603 " dst_min: %s dst_max: %s\n src_min: %s src_max: %s\n",
604 pkt_dev->dst_min, pkt_dev->dst_max, pkt_dev->src_min,
605 pkt_dev->src_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700607 seq_puts(seq, " src_mac: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Kris Katterjohnf404e9a2006-01-17 13:04:57 -0800609 if (is_zero_ether_addr(pkt_dev->src_mac))
Luiz Capitulino222f1802006-03-20 22:16:13 -0800610 for (i = 0; i < 6; i++)
611 seq_printf(seq, "%02X%s", pkt_dev->odev->dev_addr[i],
612 i == 5 ? " " : ":");
613 else
614 for (i = 0; i < 6; i++)
615 seq_printf(seq, "%02X%s", pkt_dev->src_mac[i],
616 i == 5 ? " " : ":");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Luiz Capitulino222f1802006-03-20 22:16:13 -0800618 seq_printf(seq, "dst_mac: ");
619 for (i = 0; i < 6; i++)
620 seq_printf(seq, "%02X%s", pkt_dev->dst_mac[i],
621 i == 5 ? "\n" : ":");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Luiz Capitulino222f1802006-03-20 22:16:13 -0800623 seq_printf(seq,
624 " udp_src_min: %d udp_src_max: %d udp_dst_min: %d udp_dst_max: %d\n",
625 pkt_dev->udp_src_min, pkt_dev->udp_src_max,
626 pkt_dev->udp_dst_min, pkt_dev->udp_dst_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Luiz Capitulino222f1802006-03-20 22:16:13 -0800628 seq_printf(seq,
629 " src_mac_count: %d dst_mac_count: %d \n Flags: ",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700630 pkt_dev->src_mac_count, pkt_dev->dst_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Luiz Capitulino222f1802006-03-20 22:16:13 -0800632 if (pkt_dev->flags & F_IPV6)
633 seq_printf(seq, "IPV6 ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Luiz Capitulino222f1802006-03-20 22:16:13 -0800635 if (pkt_dev->flags & F_IPSRC_RND)
636 seq_printf(seq, "IPSRC_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Luiz Capitulino222f1802006-03-20 22:16:13 -0800638 if (pkt_dev->flags & F_IPDST_RND)
639 seq_printf(seq, "IPDST_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
Luiz Capitulino222f1802006-03-20 22:16:13 -0800641 if (pkt_dev->flags & F_TXSIZE_RND)
642 seq_printf(seq, "TXSIZE_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Luiz Capitulino222f1802006-03-20 22:16:13 -0800644 if (pkt_dev->flags & F_UDPSRC_RND)
645 seq_printf(seq, "UDPSRC_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Luiz Capitulino222f1802006-03-20 22:16:13 -0800647 if (pkt_dev->flags & F_UDPDST_RND)
648 seq_printf(seq, "UDPDST_RND ");
649
650 if (pkt_dev->flags & F_MACSRC_RND)
651 seq_printf(seq, "MACSRC_RND ");
652
653 if (pkt_dev->flags & F_MACDST_RND)
654 seq_printf(seq, "MACDST_RND ");
655
656 seq_puts(seq, "\n");
657
658 sa = pkt_dev->started_at;
659 stopped = pkt_dev->stopped_at;
660 if (pkt_dev->running)
661 stopped = now; /* not really stopped, more like last-running-at */
662
663 seq_printf(seq,
664 "Current:\n pkts-sofar: %llu errors: %llu\n started: %lluus stopped: %lluus idle: %lluus\n",
665 (unsigned long long)pkt_dev->sofar,
666 (unsigned long long)pkt_dev->errors, (unsigned long long)sa,
667 (unsigned long long)stopped,
668 (unsigned long long)pkt_dev->idle_acc);
669
670 seq_printf(seq,
671 " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700672 pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset,
673 pkt_dev->cur_src_mac_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Luiz Capitulino222f1802006-03-20 22:16:13 -0800675 if (pkt_dev->flags & F_IPV6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 char b1[128], b2[128];
Luiz Capitulino222f1802006-03-20 22:16:13 -0800677 fmt_ip6(b1, pkt_dev->cur_in6_daddr.s6_addr);
678 fmt_ip6(b2, pkt_dev->cur_in6_saddr.s6_addr);
679 seq_printf(seq, " cur_saddr: %s cur_daddr: %s\n", b2, b1);
680 } else
681 seq_printf(seq, " cur_saddr: 0x%x cur_daddr: 0x%x\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700682 pkt_dev->cur_saddr, pkt_dev->cur_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Luiz Capitulino222f1802006-03-20 22:16:13 -0800684 seq_printf(seq, " cur_udp_dst: %d cur_udp_src: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700685 pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Luiz Capitulino222f1802006-03-20 22:16:13 -0800687 seq_printf(seq, " flows: %u\n", pkt_dev->nflows);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
689 if (pkt_dev->result[0])
Luiz Capitulino222f1802006-03-20 22:16:13 -0800690 seq_printf(seq, "Result: %s\n", pkt_dev->result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 else
Luiz Capitulino222f1802006-03-20 22:16:13 -0800692 seq_printf(seq, "Result: Idle\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700694 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695}
696
Luiz Capitulino222f1802006-03-20 22:16:13 -0800697static int count_trail_chars(const char __user * user_buffer,
698 unsigned int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
700 int i;
701
702 for (i = 0; i < maxlen; i++) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800703 char c;
704 if (get_user(c, &user_buffer[i]))
705 return -EFAULT;
706 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 case '\"':
708 case '\n':
709 case '\r':
710 case '\t':
711 case ' ':
712 case '=':
713 break;
714 default:
715 goto done;
716 };
717 }
718done:
719 return i;
720}
721
Luiz Capitulino222f1802006-03-20 22:16:13 -0800722static unsigned long num_arg(const char __user * user_buffer,
723 unsigned long maxlen, unsigned long *num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724{
725 int i = 0;
726 *num = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800727
728 for (; i < maxlen; i++) {
729 char c;
730 if (get_user(c, &user_buffer[i]))
731 return -EFAULT;
732 if ((c >= '0') && (c <= '9')) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 *num *= 10;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800734 *num += c - '0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 } else
736 break;
737 }
738 return i;
739}
740
Luiz Capitulino222f1802006-03-20 22:16:13 -0800741static int strn_len(const char __user * user_buffer, unsigned int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742{
743 int i = 0;
744
Luiz Capitulino222f1802006-03-20 22:16:13 -0800745 for (; i < maxlen; i++) {
746 char c;
747 if (get_user(c, &user_buffer[i]))
748 return -EFAULT;
749 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 case '\"':
751 case '\n':
752 case '\r':
753 case '\t':
754 case ' ':
755 goto done_str;
756 break;
757 default:
758 break;
759 };
760 }
761done_str:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 return i;
763}
764
Luiz Capitulino222f1802006-03-20 22:16:13 -0800765static ssize_t pktgen_if_write(struct file *file,
766 const char __user * user_buffer, size_t count,
767 loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800769 struct seq_file *seq = (struct seq_file *)file->private_data;
770 struct pktgen_dev *pkt_dev = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 int i = 0, max, len;
772 char name[16], valstr[32];
773 unsigned long value = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800774 char *pg_result = NULL;
775 int tmp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 char buf[128];
Luiz Capitulino222f1802006-03-20 22:16:13 -0800777
778 pg_result = &(pkt_dev->result[0]);
779
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 if (count < 1) {
781 printk("pktgen: wrong command format\n");
782 return -EINVAL;
783 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800784
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 max = count - i;
786 tmp = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800787 if (tmp < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 printk("pktgen: illegal format\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -0800789 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800791 i += tmp;
792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 /* Read variable name */
794
795 len = strn_len(&user_buffer[i], sizeof(name) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800796 if (len < 0) {
797 return len;
798 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 memset(name, 0, sizeof(name));
Luiz Capitulino222f1802006-03-20 22:16:13 -0800800 if (copy_from_user(name, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 return -EFAULT;
802 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800803
804 max = count - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 len = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800806 if (len < 0)
807 return len;
808
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 i += len;
810
811 if (debug) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800812 char tb[count + 1];
813 if (copy_from_user(tb, user_buffer, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800815 tb[count] = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700816 printk("pktgen: %s,%lu buffer -:%s:-\n", name,
Luiz Capitulino222f1802006-03-20 22:16:13 -0800817 (unsigned long)count, tb);
818 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
820 if (!strcmp(name, "min_pkt_size")) {
821 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800822 if (len < 0) {
823 return len;
824 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800826 if (value < 14 + 20 + 8)
827 value = 14 + 20 + 8;
828 if (value != pkt_dev->min_pkt_size) {
829 pkt_dev->min_pkt_size = value;
830 pkt_dev->cur_pkt_size = value;
831 }
832 sprintf(pg_result, "OK: min_pkt_size=%u",
833 pkt_dev->min_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 return count;
835 }
836
Luiz Capitulino222f1802006-03-20 22:16:13 -0800837 if (!strcmp(name, "max_pkt_size")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800839 if (len < 0) {
840 return len;
841 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800843 if (value < 14 + 20 + 8)
844 value = 14 + 20 + 8;
845 if (value != pkt_dev->max_pkt_size) {
846 pkt_dev->max_pkt_size = value;
847 pkt_dev->cur_pkt_size = value;
848 }
849 sprintf(pg_result, "OK: max_pkt_size=%u",
850 pkt_dev->max_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 return count;
852 }
853
Luiz Capitulino222f1802006-03-20 22:16:13 -0800854 /* Shortcut for min = max */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
856 if (!strcmp(name, "pkt_size")) {
857 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800858 if (len < 0) {
859 return len;
860 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800862 if (value < 14 + 20 + 8)
863 value = 14 + 20 + 8;
864 if (value != pkt_dev->min_pkt_size) {
865 pkt_dev->min_pkt_size = value;
866 pkt_dev->max_pkt_size = value;
867 pkt_dev->cur_pkt_size = value;
868 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 sprintf(pg_result, "OK: pkt_size=%u", pkt_dev->min_pkt_size);
870 return count;
871 }
872
Luiz Capitulino222f1802006-03-20 22:16:13 -0800873 if (!strcmp(name, "debug")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800875 if (len < 0) {
876 return len;
877 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800879 debug = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 sprintf(pg_result, "OK: debug=%u", debug);
881 return count;
882 }
883
Luiz Capitulino222f1802006-03-20 22:16:13 -0800884 if (!strcmp(name, "frags")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800886 if (len < 0) {
887 return len;
888 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 i += len;
890 pkt_dev->nfrags = value;
891 sprintf(pg_result, "OK: frags=%u", pkt_dev->nfrags);
892 return count;
893 }
894 if (!strcmp(name, "delay")) {
895 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800896 if (len < 0) {
897 return len;
898 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 i += len;
900 if (value == 0x7FFFFFFF) {
901 pkt_dev->delay_us = 0x7FFFFFFF;
902 pkt_dev->delay_ns = 0;
903 } else {
904 pkt_dev->delay_us = value / 1000;
905 pkt_dev->delay_ns = value % 1000;
906 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800907 sprintf(pg_result, "OK: delay=%u",
908 1000 * pkt_dev->delay_us + pkt_dev->delay_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 return count;
910 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800911 if (!strcmp(name, "udp_src_min")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800913 if (len < 0) {
914 return len;
915 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800917 if (value != pkt_dev->udp_src_min) {
918 pkt_dev->udp_src_min = value;
919 pkt_dev->cur_udp_src = value;
920 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min);
922 return count;
923 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800924 if (!strcmp(name, "udp_dst_min")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800926 if (len < 0) {
927 return len;
928 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800930 if (value != pkt_dev->udp_dst_min) {
931 pkt_dev->udp_dst_min = value;
932 pkt_dev->cur_udp_dst = value;
933 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min);
935 return count;
936 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800937 if (!strcmp(name, "udp_src_max")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 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 != pkt_dev->udp_src_max) {
944 pkt_dev->udp_src_max = value;
945 pkt_dev->cur_udp_src = value;
946 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max);
948 return count;
949 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800950 if (!strcmp(name, "udp_dst_max")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800952 if (len < 0) {
953 return len;
954 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800956 if (value != pkt_dev->udp_dst_max) {
957 pkt_dev->udp_dst_max = value;
958 pkt_dev->cur_udp_dst = value;
959 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max);
961 return count;
962 }
963 if (!strcmp(name, "clone_skb")) {
964 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800965 if (len < 0) {
966 return len;
967 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800969 pkt_dev->clone_skb = value;
970
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb);
972 return count;
973 }
974 if (!strcmp(name, "count")) {
975 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800976 if (len < 0) {
977 return len;
978 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 i += len;
980 pkt_dev->count = value;
981 sprintf(pg_result, "OK: count=%llu",
Luiz Capitulino222f1802006-03-20 22:16:13 -0800982 (unsigned long long)pkt_dev->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 return count;
984 }
985 if (!strcmp(name, "src_mac_count")) {
986 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800987 if (len < 0) {
988 return len;
989 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 i += len;
991 if (pkt_dev->src_mac_count != value) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800992 pkt_dev->src_mac_count = value;
993 pkt_dev->cur_src_mac_offset = 0;
994 }
995 sprintf(pg_result, "OK: src_mac_count=%d",
996 pkt_dev->src_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 return count;
998 }
999 if (!strcmp(name, "dst_mac_count")) {
1000 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001001 if (len < 0) {
1002 return len;
1003 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 i += len;
1005 if (pkt_dev->dst_mac_count != value) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001006 pkt_dev->dst_mac_count = value;
1007 pkt_dev->cur_dst_mac_offset = 0;
1008 }
1009 sprintf(pg_result, "OK: dst_mac_count=%d",
1010 pkt_dev->dst_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 return count;
1012 }
1013 if (!strcmp(name, "flag")) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001014 char f[32];
1015 memset(f, 0, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 len = strn_len(&user_buffer[i], sizeof(f) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001017 if (len < 0) {
1018 return len;
1019 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 if (copy_from_user(f, &user_buffer[i], len))
1021 return -EFAULT;
1022 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001023 if (strcmp(f, "IPSRC_RND") == 0)
1024 pkt_dev->flags |= F_IPSRC_RND;
1025
1026 else if (strcmp(f, "!IPSRC_RND") == 0)
1027 pkt_dev->flags &= ~F_IPSRC_RND;
1028
1029 else if (strcmp(f, "TXSIZE_RND") == 0)
1030 pkt_dev->flags |= F_TXSIZE_RND;
1031
1032 else if (strcmp(f, "!TXSIZE_RND") == 0)
1033 pkt_dev->flags &= ~F_TXSIZE_RND;
1034
1035 else if (strcmp(f, "IPDST_RND") == 0)
1036 pkt_dev->flags |= F_IPDST_RND;
1037
1038 else if (strcmp(f, "!IPDST_RND") == 0)
1039 pkt_dev->flags &= ~F_IPDST_RND;
1040
1041 else if (strcmp(f, "UDPSRC_RND") == 0)
1042 pkt_dev->flags |= F_UDPSRC_RND;
1043
1044 else if (strcmp(f, "!UDPSRC_RND") == 0)
1045 pkt_dev->flags &= ~F_UDPSRC_RND;
1046
1047 else if (strcmp(f, "UDPDST_RND") == 0)
1048 pkt_dev->flags |= F_UDPDST_RND;
1049
1050 else if (strcmp(f, "!UDPDST_RND") == 0)
1051 pkt_dev->flags &= ~F_UDPDST_RND;
1052
1053 else if (strcmp(f, "MACSRC_RND") == 0)
1054 pkt_dev->flags |= F_MACSRC_RND;
1055
1056 else if (strcmp(f, "!MACSRC_RND") == 0)
1057 pkt_dev->flags &= ~F_MACSRC_RND;
1058
1059 else if (strcmp(f, "MACDST_RND") == 0)
1060 pkt_dev->flags |= F_MACDST_RND;
1061
1062 else if (strcmp(f, "!MACDST_RND") == 0)
1063 pkt_dev->flags &= ~F_MACDST_RND;
1064
1065 else {
1066 sprintf(pg_result,
1067 "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
1068 f,
1069 "IPSRC_RND, IPDST_RND, TXSIZE_RND, UDPSRC_RND, UDPDST_RND, MACSRC_RND, MACDST_RND\n");
1070 return count;
1071 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
1073 return count;
1074 }
1075 if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
1076 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001077 if (len < 0) {
1078 return len;
1079 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
Luiz Capitulino222f1802006-03-20 22:16:13 -08001081 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001083 buf[len] = 0;
1084 if (strcmp(buf, pkt_dev->dst_min) != 0) {
1085 memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
1086 strncpy(pkt_dev->dst_min, buf, len);
1087 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
1088 pkt_dev->cur_daddr = pkt_dev->daddr_min;
1089 }
1090 if (debug)
1091 printk("pktgen: dst_min set to: %s\n",
1092 pkt_dev->dst_min);
1093 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min);
1095 return count;
1096 }
1097 if (!strcmp(name, "dst_max")) {
1098 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001099 if (len < 0) {
1100 return len;
1101 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
Luiz Capitulino222f1802006-03-20 22:16:13 -08001103 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 return -EFAULT;
1105
Luiz Capitulino222f1802006-03-20 22:16:13 -08001106 buf[len] = 0;
1107 if (strcmp(buf, pkt_dev->dst_max) != 0) {
1108 memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
1109 strncpy(pkt_dev->dst_max, buf, len);
1110 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
1111 pkt_dev->cur_daddr = pkt_dev->daddr_max;
1112 }
1113 if (debug)
1114 printk("pktgen: dst_max set to: %s\n",
1115 pkt_dev->dst_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 i += len;
1117 sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max);
1118 return count;
1119 }
1120 if (!strcmp(name, "dst6")) {
1121 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001122 if (len < 0)
1123 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
1125 pkt_dev->flags |= F_IPV6;
1126
Luiz Capitulino222f1802006-03-20 22:16:13 -08001127 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001129 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
1131 scan_ip6(buf, pkt_dev->in6_daddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001132 fmt_ip6(buf, pkt_dev->in6_daddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
1134 ipv6_addr_copy(&pkt_dev->cur_in6_daddr, &pkt_dev->in6_daddr);
1135
Luiz Capitulino222f1802006-03-20 22:16:13 -08001136 if (debug)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 printk("pktgen: dst6 set to: %s\n", buf);
1138
Luiz Capitulino222f1802006-03-20 22:16:13 -08001139 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 sprintf(pg_result, "OK: dst6=%s", buf);
1141 return count;
1142 }
1143 if (!strcmp(name, "dst6_min")) {
1144 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001145 if (len < 0)
1146 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
1148 pkt_dev->flags |= F_IPV6;
1149
Luiz Capitulino222f1802006-03-20 22:16:13 -08001150 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001152 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
1154 scan_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001155 fmt_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
Luiz Capitulino222f1802006-03-20 22:16:13 -08001157 ipv6_addr_copy(&pkt_dev->cur_in6_daddr,
1158 &pkt_dev->min_in6_daddr);
1159 if (debug)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 printk("pktgen: dst6_min set to: %s\n", buf);
1161
Luiz Capitulino222f1802006-03-20 22:16:13 -08001162 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 sprintf(pg_result, "OK: dst6_min=%s", buf);
1164 return count;
1165 }
1166 if (!strcmp(name, "dst6_max")) {
1167 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001168 if (len < 0)
1169 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
1171 pkt_dev->flags |= F_IPV6;
1172
Luiz Capitulino222f1802006-03-20 22:16:13 -08001173 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001175 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
1177 scan_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001178 fmt_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
Luiz Capitulino222f1802006-03-20 22:16:13 -08001180 if (debug)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 printk("pktgen: dst6_max set to: %s\n", buf);
1182
Luiz Capitulino222f1802006-03-20 22:16:13 -08001183 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 sprintf(pg_result, "OK: dst6_max=%s", buf);
1185 return count;
1186 }
1187 if (!strcmp(name, "src6")) {
1188 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001189 if (len < 0)
1190 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
1192 pkt_dev->flags |= F_IPV6;
1193
Luiz Capitulino222f1802006-03-20 22:16:13 -08001194 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001196 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
1198 scan_ip6(buf, pkt_dev->in6_saddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001199 fmt_ip6(buf, pkt_dev->in6_saddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
1201 ipv6_addr_copy(&pkt_dev->cur_in6_saddr, &pkt_dev->in6_saddr);
1202
Luiz Capitulino222f1802006-03-20 22:16:13 -08001203 if (debug)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 printk("pktgen: src6 set to: %s\n", buf);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001205
1206 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 sprintf(pg_result, "OK: src6=%s", buf);
1208 return count;
1209 }
1210 if (!strcmp(name, "src_min")) {
1211 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001212 if (len < 0) {
1213 return len;
1214 }
1215 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001217 buf[len] = 0;
1218 if (strcmp(buf, pkt_dev->src_min) != 0) {
1219 memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
1220 strncpy(pkt_dev->src_min, buf, len);
1221 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
1222 pkt_dev->cur_saddr = pkt_dev->saddr_min;
1223 }
1224 if (debug)
1225 printk("pktgen: src_min set to: %s\n",
1226 pkt_dev->src_min);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 i += len;
1228 sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min);
1229 return count;
1230 }
1231 if (!strcmp(name, "src_max")) {
1232 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001233 if (len < 0) {
1234 return len;
1235 }
1236 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001238 buf[len] = 0;
1239 if (strcmp(buf, pkt_dev->src_max) != 0) {
1240 memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
1241 strncpy(pkt_dev->src_max, buf, len);
1242 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
1243 pkt_dev->cur_saddr = pkt_dev->saddr_max;
1244 }
1245 if (debug)
1246 printk("pktgen: src_max set to: %s\n",
1247 pkt_dev->src_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 i += len;
1249 sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max);
1250 return count;
1251 }
1252 if (!strcmp(name, "dst_mac")) {
1253 char *v = valstr;
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001254 unsigned char old_dmac[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 unsigned char *m = pkt_dev->dst_mac;
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001256 memcpy(old_dmac, pkt_dev->dst_mac, ETH_ALEN);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001257
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001259 if (len < 0) {
1260 return len;
1261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 memset(valstr, 0, sizeof(valstr));
Luiz Capitulino222f1802006-03-20 22:16:13 -08001263 if (copy_from_user(valstr, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 return -EFAULT;
1265 i += len;
1266
Luiz Capitulino222f1802006-03-20 22:16:13 -08001267 for (*m = 0; *v && m < pkt_dev->dst_mac + 6; v++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 if (*v >= '0' && *v <= '9') {
1269 *m *= 16;
1270 *m += *v - '0';
1271 }
1272 if (*v >= 'A' && *v <= 'F') {
1273 *m *= 16;
1274 *m += *v - 'A' + 10;
1275 }
1276 if (*v >= 'a' && *v <= 'f') {
1277 *m *= 16;
1278 *m += *v - 'a' + 10;
1279 }
1280 if (*v == ':') {
1281 m++;
1282 *m = 0;
1283 }
1284 }
1285
1286 /* Set up Dest MAC */
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001287 if (compare_ether_addr(old_dmac, pkt_dev->dst_mac))
1288 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001289
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 sprintf(pg_result, "OK: dstmac");
1291 return count;
1292 }
1293 if (!strcmp(name, "src_mac")) {
1294 char *v = valstr;
1295 unsigned char *m = pkt_dev->src_mac;
1296
1297 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001298 if (len < 0) {
1299 return len;
1300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 memset(valstr, 0, sizeof(valstr));
Luiz Capitulino222f1802006-03-20 22:16:13 -08001302 if (copy_from_user(valstr, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 return -EFAULT;
1304 i += len;
1305
Luiz Capitulino222f1802006-03-20 22:16:13 -08001306 for (*m = 0; *v && m < pkt_dev->src_mac + 6; v++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 if (*v >= '0' && *v <= '9') {
1308 *m *= 16;
1309 *m += *v - '0';
1310 }
1311 if (*v >= 'A' && *v <= 'F') {
1312 *m *= 16;
1313 *m += *v - 'A' + 10;
1314 }
1315 if (*v >= 'a' && *v <= 'f') {
1316 *m *= 16;
1317 *m += *v - 'a' + 10;
1318 }
1319 if (*v == ':') {
1320 m++;
1321 *m = 0;
1322 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001323 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
Luiz Capitulino222f1802006-03-20 22:16:13 -08001325 sprintf(pg_result, "OK: srcmac");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 return count;
1327 }
1328
Luiz Capitulino222f1802006-03-20 22:16:13 -08001329 if (!strcmp(name, "clear_counters")) {
1330 pktgen_clear_counters(pkt_dev);
1331 sprintf(pg_result, "OK: Clearing counters.\n");
1332 return count;
1333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
1335 if (!strcmp(name, "flows")) {
1336 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001337 if (len < 0) {
1338 return len;
1339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 i += len;
1341 if (value > MAX_CFLOWS)
1342 value = MAX_CFLOWS;
1343
1344 pkt_dev->cflows = value;
1345 sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
1346 return count;
1347 }
1348
1349 if (!strcmp(name, "flowlen")) {
1350 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001351 if (len < 0) {
1352 return len;
1353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 i += len;
1355 pkt_dev->lflow = value;
1356 sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
1357 return count;
1358 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001359
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
1361 return -EINVAL;
1362}
1363
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001364static int pktgen_if_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365{
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001366 return single_open(file, pktgen_if_show, PDE(inode)->data);
1367}
1368
1369static struct file_operations pktgen_if_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001370 .owner = THIS_MODULE,
1371 .open = pktgen_if_open,
1372 .read = seq_read,
1373 .llseek = seq_lseek,
1374 .write = pktgen_if_write,
1375 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001376};
1377
1378static int pktgen_thread_show(struct seq_file *seq, void *v)
1379{
Luiz Capitulino222f1802006-03-20 22:16:13 -08001380 struct pktgen_thread *t = seq->private;
1381 struct pktgen_dev *pkt_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001383 BUG_ON(!t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001385 seq_printf(seq, "Name: %s max_before_softirq: %d\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001386 t->name, t->max_before_softirq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
Luiz Capitulino222f1802006-03-20 22:16:13 -08001388 seq_printf(seq, "Running: ");
1389
1390 if_lock(t);
1391 for (pkt_dev = t->if_list; pkt_dev; pkt_dev = pkt_dev->next)
1392 if (pkt_dev->running)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001393 seq_printf(seq, "%s ", pkt_dev->ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
Luiz Capitulino222f1802006-03-20 22:16:13 -08001395 seq_printf(seq, "\nStopped: ");
1396
1397 for (pkt_dev = t->if_list; pkt_dev; pkt_dev = pkt_dev->next)
1398 if (!pkt_dev->running)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001399 seq_printf(seq, "%s ", pkt_dev->ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
1401 if (t->result[0])
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001402 seq_printf(seq, "\nResult: %s\n", t->result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 else
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001404 seq_printf(seq, "\nResult: NA\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
Luiz Capitulino222f1802006-03-20 22:16:13 -08001406 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001408 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409}
1410
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001411static ssize_t pktgen_thread_write(struct file *file,
Luiz Capitulino222f1802006-03-20 22:16:13 -08001412 const char __user * user_buffer,
1413 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414{
Luiz Capitulino222f1802006-03-20 22:16:13 -08001415 struct seq_file *seq = (struct seq_file *)file->private_data;
1416 struct pktgen_thread *t = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 int i = 0, max, len, ret;
1418 char name[40];
Luiz Capitulino222f1802006-03-20 22:16:13 -08001419 char *pg_result;
1420 unsigned long value = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001421
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 if (count < 1) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001423 // sprintf(pg_result, "Wrong command format");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 return -EINVAL;
1425 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001426
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 max = count - i;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001428 len = count_trail_chars(&user_buffer[i], max);
1429 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001430 return len;
1431
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 i += len;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001433
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 /* Read variable name */
1435
1436 len = strn_len(&user_buffer[i], sizeof(name) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001437 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001438 return len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001439
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 memset(name, 0, sizeof(name));
1441 if (copy_from_user(name, &user_buffer[i], len))
1442 return -EFAULT;
1443 i += len;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001444
Luiz Capitulino222f1802006-03-20 22:16:13 -08001445 max = count - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 len = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001447 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001448 return len;
1449
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 i += len;
1451
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001452 if (debug)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001453 printk("pktgen: t=%s, count=%lu\n", name, (unsigned long)count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
Luiz Capitulino222f1802006-03-20 22:16:13 -08001455 if (!t) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 printk("pktgen: ERROR: No thread\n");
1457 ret = -EINVAL;
1458 goto out;
1459 }
1460
1461 pg_result = &(t->result[0]);
1462
Luiz Capitulino222f1802006-03-20 22:16:13 -08001463 if (!strcmp(name, "add_device")) {
1464 char f[32];
1465 memset(f, 0, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 len = strn_len(&user_buffer[i], sizeof(f) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001467 if (len < 0) {
1468 ret = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 goto out;
1470 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001471 if (copy_from_user(f, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 return -EFAULT;
1473 i += len;
1474 thread_lock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08001475 pktgen_add_device(t, f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 thread_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08001477 ret = count;
1478 sprintf(pg_result, "OK: add_device=%s", f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 goto out;
1480 }
1481
Luiz Capitulino222f1802006-03-20 22:16:13 -08001482 if (!strcmp(name, "rem_device_all")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 thread_lock();
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001484 t->control |= T_REMDEVALL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 thread_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08001486 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 ret = count;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001488 sprintf(pg_result, "OK: rem_device_all");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 goto out;
1490 }
1491
Luiz Capitulino222f1802006-03-20 22:16:13 -08001492 if (!strcmp(name, "max_before_softirq")) {
1493 len = num_arg(&user_buffer[i], 10, &value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 thread_lock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08001495 t->max_before_softirq = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 thread_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08001497 ret = count;
1498 sprintf(pg_result, "OK: max_before_softirq=%lu", value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 goto out;
1500 }
1501
1502 ret = -EINVAL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001503out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 return ret;
1505}
1506
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001507static int pktgen_thread_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508{
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001509 return single_open(file, pktgen_thread_show, PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510}
1511
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001512static struct file_operations pktgen_thread_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001513 .owner = THIS_MODULE,
1514 .open = pktgen_thread_open,
1515 .read = seq_read,
1516 .llseek = seq_lseek,
1517 .write = pktgen_thread_write,
1518 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001519};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
1521/* Think find or remove for NN */
Luiz Capitulino222f1802006-03-20 22:16:13 -08001522static struct pktgen_dev *__pktgen_NN_threads(const char *ifname, int remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523{
1524 struct pktgen_thread *t;
1525 struct pktgen_dev *pkt_dev = NULL;
1526
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08001527 list_for_each_entry(t, &pktgen_threads, th_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 pkt_dev = pktgen_find_dev(t, ifname);
1529 if (pkt_dev) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001530 if (remove) {
1531 if_lock(t);
1532 pkt_dev->removal_mark = 1;
1533 t->control |= T_REMDEV;
1534 if_unlock(t);
1535 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 break;
1537 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001539 return pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540}
1541
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001542/*
1543 * mark a device for removal
1544 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08001545static int pktgen_mark_device(const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546{
1547 struct pktgen_dev *pkt_dev = NULL;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001548 const int max_tries = 10, msec_per_try = 125;
1549 int i = 0;
1550 int ret = 0;
1551
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 thread_lock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08001553 PG_DEBUG(printk("pktgen: pktgen_mark_device marking %s for removal\n",
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001554 ifname));
1555
Luiz Capitulino222f1802006-03-20 22:16:13 -08001556 while (1) {
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001557
1558 pkt_dev = __pktgen_NN_threads(ifname, REMOVE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001559 if (pkt_dev == NULL)
1560 break; /* success */
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001561
1562 thread_unlock();
1563 PG_DEBUG(printk("pktgen: pktgen_mark_device waiting for %s "
Luiz Capitulino222f1802006-03-20 22:16:13 -08001564 "to disappear....\n", ifname));
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001565 schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
1566 thread_lock();
1567
1568 if (++i >= max_tries) {
1569 printk("pktgen_mark_device: timed out after waiting "
Luiz Capitulino222f1802006-03-20 22:16:13 -08001570 "%d msec for device %s to be removed\n",
1571 msec_per_try * i, ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001572 ret = 1;
1573 break;
1574 }
1575
1576 }
1577
1578 thread_unlock();
1579
1580 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581}
1582
Luiz Capitulino222f1802006-03-20 22:16:13 -08001583static int pktgen_device_event(struct notifier_block *unused,
1584 unsigned long event, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585{
1586 struct net_device *dev = (struct net_device *)(ptr);
1587
1588 /* It is OK that we do not hold the group lock right now,
1589 * as we run under the RTNL lock.
1590 */
1591
1592 switch (event) {
1593 case NETDEV_CHANGEADDR:
1594 case NETDEV_GOING_DOWN:
1595 case NETDEV_DOWN:
1596 case NETDEV_UP:
1597 /* Ignore for now */
1598 break;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001599
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 case NETDEV_UNREGISTER:
Luiz Capitulino222f1802006-03-20 22:16:13 -08001601 pktgen_mark_device(dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 break;
1603 };
1604
1605 return NOTIFY_DONE;
1606}
1607
1608/* Associate pktgen_dev with a device. */
1609
Luiz Capitulino222f1802006-03-20 22:16:13 -08001610static struct net_device *pktgen_setup_dev(struct pktgen_dev *pkt_dev)
1611{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 struct net_device *odev;
1613
1614 /* Clean old setups */
1615
1616 if (pkt_dev->odev) {
1617 dev_put(pkt_dev->odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001618 pkt_dev->odev = NULL;
1619 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
1621 odev = dev_get_by_name(pkt_dev->ifname);
1622
1623 if (!odev) {
1624 printk("pktgen: no such netdevice: \"%s\"\n", pkt_dev->ifname);
1625 goto out;
1626 }
1627 if (odev->type != ARPHRD_ETHER) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001628 printk("pktgen: not an ethernet device: \"%s\"\n",
1629 pkt_dev->ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 goto out_put;
1631 }
1632 if (!netif_running(odev)) {
1633 printk("pktgen: device is down: \"%s\"\n", pkt_dev->ifname);
1634 goto out_put;
1635 }
1636 pkt_dev->odev = odev;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001637
1638 return pkt_dev->odev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639
1640out_put:
1641 dev_put(odev);
1642out:
Luiz Capitulino222f1802006-03-20 22:16:13 -08001643 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
1645}
1646
1647/* Read pkt_dev from the interface and set up internal pktgen_dev
1648 * structure to have the right information to create/send packets
1649 */
1650static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
1651{
1652 /* Try once more, just in case it works now. */
Luiz Capitulino222f1802006-03-20 22:16:13 -08001653 if (!pkt_dev->odev)
1654 pktgen_setup_dev(pkt_dev);
1655
1656 if (!pkt_dev->odev) {
1657 printk("pktgen: ERROR: pkt_dev->odev == NULL in setup_inject.\n");
1658 sprintf(pkt_dev->result,
1659 "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
1660 return;
1661 }
1662
1663 /* Default to the interface's mac if not explicitly set. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001665 if (is_zero_ether_addr(pkt_dev->src_mac))
Luiz Capitulino222f1802006-03-20 22:16:13 -08001666 memcpy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
Luiz Capitulino222f1802006-03-20 22:16:13 -08001668 /* Set up Dest MAC */
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001669 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
Luiz Capitulino222f1802006-03-20 22:16:13 -08001671 /* Set up pkt size */
1672 pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
1673
1674 if (pkt_dev->flags & F_IPV6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 /*
1676 * Skip this automatic address setting until locks or functions
1677 * gets exported
1678 */
1679
1680#ifdef NOTNOW
Luiz Capitulino222f1802006-03-20 22:16:13 -08001681 int i, set = 0, err = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 struct inet6_dev *idev;
1683
Luiz Capitulino222f1802006-03-20 22:16:13 -08001684 for (i = 0; i < IN6_ADDR_HSIZE; i++)
1685 if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 set = 1;
1687 break;
1688 }
1689
Luiz Capitulino222f1802006-03-20 22:16:13 -08001690 if (!set) {
1691
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 /*
1693 * Use linklevel address if unconfigured.
1694 *
1695 * use ipv6_get_lladdr if/when it's get exported
1696 */
1697
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 read_lock(&addrconf_lock);
1699 if ((idev = __in6_dev_get(pkt_dev->odev)) != NULL) {
1700 struct inet6_ifaddr *ifp;
1701
1702 read_lock_bh(&idev->lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001703 for (ifp = idev->addr_list; ifp;
1704 ifp = ifp->if_next) {
1705 if (ifp->scope == IFA_LINK
1706 && !(ifp->
1707 flags & IFA_F_TENTATIVE)) {
1708 ipv6_addr_copy(&pkt_dev->
1709 cur_in6_saddr,
1710 &ifp->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 err = 0;
1712 break;
1713 }
1714 }
1715 read_unlock_bh(&idev->lock);
1716 }
1717 read_unlock(&addrconf_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001718 if (err)
1719 printk("pktgen: ERROR: IPv6 link address not availble.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 }
1721#endif
Luiz Capitulino222f1802006-03-20 22:16:13 -08001722 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 pkt_dev->saddr_min = 0;
1724 pkt_dev->saddr_max = 0;
1725 if (strlen(pkt_dev->src_min) == 0) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001726
1727 struct in_device *in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
1729 rcu_read_lock();
Herbert Xue5ed6392005-10-03 14:35:55 -07001730 in_dev = __in_dev_get_rcu(pkt_dev->odev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 if (in_dev) {
1732 if (in_dev->ifa_list) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001733 pkt_dev->saddr_min =
1734 in_dev->ifa_list->ifa_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 pkt_dev->saddr_max = pkt_dev->saddr_min;
1736 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 }
1738 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08001739 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
1741 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
1742 }
1743
1744 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
1745 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
1746 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001747 /* Initialize current values. */
1748 pkt_dev->cur_dst_mac_offset = 0;
1749 pkt_dev->cur_src_mac_offset = 0;
1750 pkt_dev->cur_saddr = pkt_dev->saddr_min;
1751 pkt_dev->cur_daddr = pkt_dev->daddr_min;
1752 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
1753 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 pkt_dev->nflows = 0;
1755}
1756
1757static void spin(struct pktgen_dev *pkt_dev, __u64 spin_until_us)
1758{
1759 __u64 start;
1760 __u64 now;
1761
1762 start = now = getCurUs();
1763 printk(KERN_INFO "sleeping for %d\n", (int)(spin_until_us - now));
1764 while (now < spin_until_us) {
Stephen Hemmingerb4099fa2005-10-14 15:32:22 -07001765 /* TODO: optimize sleeping behavior */
Luiz Capitulino222f1802006-03-20 22:16:13 -08001766 if (spin_until_us - now > jiffies_to_usecs(1) + 1)
Nishanth Aravamudan121caf52005-09-12 14:15:34 -07001767 schedule_timeout_interruptible(1);
1768 else if (spin_until_us - now > 100) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 do_softirq();
1770 if (!pkt_dev->running)
1771 return;
1772 if (need_resched())
1773 schedule();
1774 }
1775
1776 now = getCurUs();
1777 }
1778
1779 pkt_dev->idle_acc += now - start;
1780}
1781
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782/* Increment/randomize headers according to flags and current values
1783 * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
1784 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08001785static void mod_cur_headers(struct pktgen_dev *pkt_dev)
1786{
1787 __u32 imn;
1788 __u32 imx;
1789 int flow = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
Luiz Capitulino222f1802006-03-20 22:16:13 -08001791 if (pkt_dev->cflows) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 flow = pktgen_random() % pkt_dev->cflows;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001793
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 if (pkt_dev->flows[flow].count > pkt_dev->lflow)
1795 pkt_dev->flows[flow].count = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001796 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
1798 /* Deal with source MAC */
Luiz Capitulino222f1802006-03-20 22:16:13 -08001799 if (pkt_dev->src_mac_count > 1) {
1800 __u32 mc;
1801 __u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802
Luiz Capitulino222f1802006-03-20 22:16:13 -08001803 if (pkt_dev->flags & F_MACSRC_RND)
1804 mc = pktgen_random() % (pkt_dev->src_mac_count);
1805 else {
1806 mc = pkt_dev->cur_src_mac_offset++;
1807 if (pkt_dev->cur_src_mac_offset >
1808 pkt_dev->src_mac_count)
1809 pkt_dev->cur_src_mac_offset = 0;
1810 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811
Luiz Capitulino222f1802006-03-20 22:16:13 -08001812 tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
1813 pkt_dev->hh[11] = tmp;
1814 tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
1815 pkt_dev->hh[10] = tmp;
1816 tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
1817 pkt_dev->hh[9] = tmp;
1818 tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
1819 pkt_dev->hh[8] = tmp;
1820 tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
1821 pkt_dev->hh[7] = tmp;
1822 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
Luiz Capitulino222f1802006-03-20 22:16:13 -08001824 /* Deal with Destination MAC */
1825 if (pkt_dev->dst_mac_count > 1) {
1826 __u32 mc;
1827 __u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828
Luiz Capitulino222f1802006-03-20 22:16:13 -08001829 if (pkt_dev->flags & F_MACDST_RND)
1830 mc = pktgen_random() % (pkt_dev->dst_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831
Luiz Capitulino222f1802006-03-20 22:16:13 -08001832 else {
1833 mc = pkt_dev->cur_dst_mac_offset++;
1834 if (pkt_dev->cur_dst_mac_offset >
1835 pkt_dev->dst_mac_count) {
1836 pkt_dev->cur_dst_mac_offset = 0;
1837 }
1838 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
Luiz Capitulino222f1802006-03-20 22:16:13 -08001840 tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
1841 pkt_dev->hh[5] = tmp;
1842 tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
1843 pkt_dev->hh[4] = tmp;
1844 tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
1845 pkt_dev->hh[3] = tmp;
1846 tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
1847 pkt_dev->hh[2] = tmp;
1848 tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
1849 pkt_dev->hh[1] = tmp;
1850 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
Luiz Capitulino222f1802006-03-20 22:16:13 -08001852 if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
1853 if (pkt_dev->flags & F_UDPSRC_RND)
1854 pkt_dev->cur_udp_src =
1855 ((pktgen_random() %
1856 (pkt_dev->udp_src_max - pkt_dev->udp_src_min)) +
1857 pkt_dev->udp_src_min);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858
Luiz Capitulino222f1802006-03-20 22:16:13 -08001859 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 pkt_dev->cur_udp_src++;
1861 if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
1862 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001863 }
1864 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865
Luiz Capitulino222f1802006-03-20 22:16:13 -08001866 if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
1867 if (pkt_dev->flags & F_UDPDST_RND) {
1868 pkt_dev->cur_udp_dst =
1869 ((pktgen_random() %
1870 (pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)) +
1871 pkt_dev->udp_dst_min);
1872 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 pkt_dev->cur_udp_dst++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001874 if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001876 }
1877 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
1879 if (!(pkt_dev->flags & F_IPV6)) {
1880
Luiz Capitulino222f1802006-03-20 22:16:13 -08001881 if ((imn = ntohl(pkt_dev->saddr_min)) < (imx =
1882 ntohl(pkt_dev->
1883 saddr_max))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 __u32 t;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001885 if (pkt_dev->flags & F_IPSRC_RND)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 t = ((pktgen_random() % (imx - imn)) + imn);
1887 else {
1888 t = ntohl(pkt_dev->cur_saddr);
1889 t++;
1890 if (t > imx) {
1891 t = imn;
1892 }
1893 }
1894 pkt_dev->cur_saddr = htonl(t);
1895 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001896
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 if (pkt_dev->cflows && pkt_dev->flows[flow].count != 0) {
1898 pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
1899 } else {
1900
Luiz Capitulino222f1802006-03-20 22:16:13 -08001901 if ((imn = ntohl(pkt_dev->daddr_min)) < (imx =
1902 ntohl(pkt_dev->
1903 daddr_max)))
1904 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 __u32 t;
1906 if (pkt_dev->flags & F_IPDST_RND) {
1907
Luiz Capitulino222f1802006-03-20 22:16:13 -08001908 t = ((pktgen_random() % (imx - imn)) +
1909 imn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 t = htonl(t);
1911
Luiz Capitulino222f1802006-03-20 22:16:13 -08001912 while (LOOPBACK(t) || MULTICAST(t)
1913 || BADCLASS(t) || ZERONET(t)
1914 || LOCAL_MCAST(t)) {
1915 t = ((pktgen_random() %
1916 (imx - imn)) + imn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 t = htonl(t);
1918 }
1919 pkt_dev->cur_daddr = t;
1920 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001921
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 else {
1923 t = ntohl(pkt_dev->cur_daddr);
1924 t++;
1925 if (t > imx) {
1926 t = imn;
1927 }
1928 pkt_dev->cur_daddr = htonl(t);
1929 }
1930 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001931 if (pkt_dev->cflows) {
1932 pkt_dev->flows[flow].cur_daddr =
1933 pkt_dev->cur_daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 pkt_dev->nflows++;
1935 }
1936 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001937 } else { /* IPV6 * */
1938
1939 if (pkt_dev->min_in6_daddr.s6_addr32[0] == 0 &&
1940 pkt_dev->min_in6_daddr.s6_addr32[1] == 0 &&
1941 pkt_dev->min_in6_daddr.s6_addr32[2] == 0 &&
1942 pkt_dev->min_in6_daddr.s6_addr32[3] == 0) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 else {
1944 int i;
1945
1946 /* Only random destinations yet */
1947
Luiz Capitulino222f1802006-03-20 22:16:13 -08001948 for (i = 0; i < 4; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 pkt_dev->cur_in6_daddr.s6_addr32[i] =
Luiz Capitulino222f1802006-03-20 22:16:13 -08001950 ((pktgen_random() |
1951 pkt_dev->min_in6_daddr.s6_addr32[i]) &
1952 pkt_dev->max_in6_daddr.s6_addr32[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001954 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 }
1956
Luiz Capitulino222f1802006-03-20 22:16:13 -08001957 if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
1958 __u32 t;
1959 if (pkt_dev->flags & F_TXSIZE_RND) {
1960 t = ((pktgen_random() %
1961 (pkt_dev->max_pkt_size - pkt_dev->min_pkt_size))
1962 + pkt_dev->min_pkt_size);
1963 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 t = pkt_dev->cur_pkt_size + 1;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001965 if (t > pkt_dev->max_pkt_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 t = pkt_dev->min_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001967 }
1968 pkt_dev->cur_pkt_size = t;
1969 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
1971 pkt_dev->flows[flow].count++;
1972}
1973
Luiz Capitulino222f1802006-03-20 22:16:13 -08001974static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
1975 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976{
1977 struct sk_buff *skb = NULL;
1978 __u8 *eth;
1979 struct udphdr *udph;
1980 int datalen, iplen;
1981 struct iphdr *iph;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001982 struct pktgen_hdr *pgh = NULL;
1983
Robert Olsson64053be2005-06-26 15:27:10 -07001984 /* Update any of the values, used when we're incrementing various
1985 * fields.
1986 */
1987 mod_cur_headers(pkt_dev);
1988
David S. Miller7ac54592006-01-18 14:19:10 -08001989 datalen = (odev->hard_header_len + 16) & ~0xf;
1990 skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + datalen, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 if (!skb) {
1992 sprintf(pkt_dev->result, "No memory");
1993 return NULL;
1994 }
1995
David S. Miller7ac54592006-01-18 14:19:10 -08001996 skb_reserve(skb, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
1998 /* Reserve for ethernet and IP header */
1999 eth = (__u8 *) skb_push(skb, 14);
2000 iph = (struct iphdr *)skb_put(skb, sizeof(struct iphdr));
2001 udph = (struct udphdr *)skb_put(skb, sizeof(struct udphdr));
2002
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 memcpy(eth, pkt_dev->hh, 12);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002004 *(u16 *) & eth[12] = __constant_htons(ETH_P_IP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005
Luiz Capitulino222f1802006-03-20 22:16:13 -08002006 datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8; /* Eth + IPh + UDPh */
2007 if (datalen < sizeof(struct pktgen_hdr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 datalen = sizeof(struct pktgen_hdr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002009
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 udph->source = htons(pkt_dev->cur_udp_src);
2011 udph->dest = htons(pkt_dev->cur_udp_dst);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002012 udph->len = htons(datalen + 8); /* DATA + udphdr */
2013 udph->check = 0; /* No checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
2015 iph->ihl = 5;
2016 iph->version = 4;
2017 iph->ttl = 32;
2018 iph->tos = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002019 iph->protocol = IPPROTO_UDP; /* UDP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 iph->saddr = pkt_dev->cur_saddr;
2021 iph->daddr = pkt_dev->cur_daddr;
2022 iph->frag_off = 0;
2023 iplen = 20 + 8 + datalen;
2024 iph->tot_len = htons(iplen);
2025 iph->check = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002026 iph->check = ip_fast_csum((void *)iph, iph->ihl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 skb->protocol = __constant_htons(ETH_P_IP);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002028 skb->mac.raw = ((u8 *) iph) - 14;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 skb->dev = odev;
2030 skb->pkt_type = PACKET_HOST;
2031
Luiz Capitulino222f1802006-03-20 22:16:13 -08002032 if (pkt_dev->nfrags <= 0)
2033 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 else {
2035 int frags = pkt_dev->nfrags;
2036 int i;
2037
Luiz Capitulino222f1802006-03-20 22:16:13 -08002038 pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
2039
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 if (frags > MAX_SKB_FRAGS)
2041 frags = MAX_SKB_FRAGS;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002042 if (datalen > frags * PAGE_SIZE) {
2043 skb_put(skb, datalen - frags * PAGE_SIZE);
2044 datalen = frags * PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 }
2046
2047 i = 0;
2048 while (datalen > 0) {
2049 struct page *page = alloc_pages(GFP_KERNEL, 0);
2050 skb_shinfo(skb)->frags[i].page = page;
2051 skb_shinfo(skb)->frags[i].page_offset = 0;
2052 skb_shinfo(skb)->frags[i].size =
Luiz Capitulino222f1802006-03-20 22:16:13 -08002053 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 datalen -= skb_shinfo(skb)->frags[i].size;
2055 skb->len += skb_shinfo(skb)->frags[i].size;
2056 skb->data_len += skb_shinfo(skb)->frags[i].size;
2057 i++;
2058 skb_shinfo(skb)->nr_frags = i;
2059 }
2060
2061 while (i < frags) {
2062 int rem;
2063
2064 if (i == 0)
2065 break;
2066
2067 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
2068 if (rem == 0)
2069 break;
2070
2071 skb_shinfo(skb)->frags[i - 1].size -= rem;
2072
Luiz Capitulino222f1802006-03-20 22:16:13 -08002073 skb_shinfo(skb)->frags[i] =
2074 skb_shinfo(skb)->frags[i - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 get_page(skb_shinfo(skb)->frags[i].page);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002076 skb_shinfo(skb)->frags[i].page =
2077 skb_shinfo(skb)->frags[i - 1].page;
2078 skb_shinfo(skb)->frags[i].page_offset +=
2079 skb_shinfo(skb)->frags[i - 1].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 skb_shinfo(skb)->frags[i].size = rem;
2081 i++;
2082 skb_shinfo(skb)->nr_frags = i;
2083 }
2084 }
2085
Luiz Capitulino222f1802006-03-20 22:16:13 -08002086 /* Stamp the time, and sequence number, convert them to network byte order */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
Luiz Capitulino222f1802006-03-20 22:16:13 -08002088 if (pgh) {
2089 struct timeval timestamp;
2090
2091 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2092 pgh->seq_num = htonl(pkt_dev->seq_num);
2093
2094 do_gettimeofday(&timestamp);
2095 pgh->tv_sec = htonl(timestamp.tv_sec);
2096 pgh->tv_usec = htonl(timestamp.tv_usec);
2097 }
2098 pkt_dev->seq_num++;
2099
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 return skb;
2101}
2102
2103/*
2104 * scan_ip6, fmt_ip taken from dietlibc-0.21
2105 * Author Felix von Leitner <felix-dietlibc@fefe.de>
2106 *
2107 * Slightly modified for kernel.
2108 * Should be candidate for net/ipv4/utils.c
2109 * --ro
2110 */
2111
Luiz Capitulino222f1802006-03-20 22:16:13 -08002112static unsigned int scan_ip6(const char *s, char ip[16])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113{
2114 unsigned int i;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002115 unsigned int len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 unsigned long u;
2117 char suffix[16];
Luiz Capitulino222f1802006-03-20 22:16:13 -08002118 unsigned int prefixlen = 0;
2119 unsigned int suffixlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 __u32 tmp;
2121
Luiz Capitulino222f1802006-03-20 22:16:13 -08002122 for (i = 0; i < 16; i++)
2123 ip[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124
2125 for (;;) {
2126 if (*s == ':') {
2127 len++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002128 if (s[1] == ':') { /* Found "::", skip to part 2 */
2129 s += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 len++;
2131 break;
2132 }
2133 s++;
2134 }
2135 {
2136 char *tmp;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002137 u = simple_strtoul(s, &tmp, 16);
2138 i = tmp - s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 }
2140
Luiz Capitulino222f1802006-03-20 22:16:13 -08002141 if (!i)
2142 return 0;
2143 if (prefixlen == 12 && s[i] == '.') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144
2145 /* the last 4 bytes may be written as IPv4 address */
2146
2147 tmp = in_aton(s);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002148 memcpy((struct in_addr *)(ip + 12), &tmp, sizeof(tmp));
2149 return i + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 }
2151 ip[prefixlen++] = (u >> 8);
2152 ip[prefixlen++] = (u & 255);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002153 s += i;
2154 len += i;
2155 if (prefixlen == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 return len;
2157 }
2158
2159/* part 2, after "::" */
2160 for (;;) {
2161 if (*s == ':') {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002162 if (suffixlen == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 break;
2164 s++;
2165 len++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002166 } else if (suffixlen != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 break;
2168 {
2169 char *tmp;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002170 u = simple_strtol(s, &tmp, 16);
2171 i = tmp - s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 }
2173 if (!i) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002174 if (*s)
2175 len--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 break;
2177 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002178 if (suffixlen + prefixlen <= 12 && s[i] == '.') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 tmp = in_aton(s);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002180 memcpy((struct in_addr *)(suffix + suffixlen), &tmp,
2181 sizeof(tmp));
2182 suffixlen += 4;
2183 len += strlen(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 break;
2185 }
2186 suffix[suffixlen++] = (u >> 8);
2187 suffix[suffixlen++] = (u & 255);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002188 s += i;
2189 len += i;
2190 if (prefixlen + suffixlen == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 break;
2192 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002193 for (i = 0; i < suffixlen; i++)
2194 ip[16 - suffixlen + i] = suffix[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 return len;
2196}
2197
Luiz Capitulino222f1802006-03-20 22:16:13 -08002198static char tohex(char hexdigit)
2199{
2200 return hexdigit > 9 ? hexdigit + 'a' - 10 : hexdigit + '0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201}
2202
Luiz Capitulino222f1802006-03-20 22:16:13 -08002203static int fmt_xlong(char *s, unsigned int i)
2204{
2205 char *bak = s;
2206 *s = tohex((i >> 12) & 0xf);
2207 if (s != bak || *s != '0')
2208 ++s;
2209 *s = tohex((i >> 8) & 0xf);
2210 if (s != bak || *s != '0')
2211 ++s;
2212 *s = tohex((i >> 4) & 0xf);
2213 if (s != bak || *s != '0')
2214 ++s;
2215 *s = tohex(i & 0xf);
2216 return s - bak + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217}
2218
Luiz Capitulino222f1802006-03-20 22:16:13 -08002219static unsigned int fmt_ip6(char *s, const char ip[16])
2220{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 unsigned int len;
2222 unsigned int i;
2223 unsigned int temp;
2224 unsigned int compressing;
2225 int j;
2226
Luiz Capitulino222f1802006-03-20 22:16:13 -08002227 len = 0;
2228 compressing = 0;
2229 for (j = 0; j < 16; j += 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230
2231#ifdef V4MAPPEDPREFIX
Luiz Capitulino222f1802006-03-20 22:16:13 -08002232 if (j == 12 && !memcmp(ip, V4mappedprefix, 12)) {
2233 inet_ntoa_r(*(struct in_addr *)(ip + 12), s);
2234 temp = strlen(s);
2235 return len + temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 }
2237#endif
Luiz Capitulino222f1802006-03-20 22:16:13 -08002238 temp = ((unsigned long)(unsigned char)ip[j] << 8) +
2239 (unsigned long)(unsigned char)ip[j + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 if (temp == 0) {
2241 if (!compressing) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002242 compressing = 1;
2243 if (j == 0) {
2244 *s++ = ':';
2245 ++len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 }
2247 }
2248 } else {
2249 if (compressing) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002250 compressing = 0;
2251 *s++ = ':';
2252 ++len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002254 i = fmt_xlong(s, temp);
2255 len += i;
2256 s += i;
2257 if (j < 14) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 *s++ = ':';
2259 ++len;
2260 }
2261 }
2262 }
2263 if (compressing) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002264 *s++ = ':';
2265 ++len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002267 *s = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 return len;
2269}
2270
Luiz Capitulino222f1802006-03-20 22:16:13 -08002271static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
2272 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273{
2274 struct sk_buff *skb = NULL;
2275 __u8 *eth;
2276 struct udphdr *udph;
2277 int datalen;
2278 struct ipv6hdr *iph;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002279 struct pktgen_hdr *pgh = NULL;
Robert Olsson64053be2005-06-26 15:27:10 -07002280
2281 /* Update any of the values, used when we're incrementing various
2282 * fields.
2283 */
2284 mod_cur_headers(pkt_dev);
2285
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + 16, GFP_ATOMIC);
2287 if (!skb) {
2288 sprintf(pkt_dev->result, "No memory");
2289 return NULL;
2290 }
2291
2292 skb_reserve(skb, 16);
2293
2294 /* Reserve for ethernet and IP header */
2295 eth = (__u8 *) skb_push(skb, 14);
2296 iph = (struct ipv6hdr *)skb_put(skb, sizeof(struct ipv6hdr));
2297 udph = (struct udphdr *)skb_put(skb, sizeof(struct udphdr));
2298
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 memcpy(eth, pkt_dev->hh, 12);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002300 *(u16 *) & eth[12] = __constant_htons(ETH_P_IPV6);
Robert Olsson64053be2005-06-26 15:27:10 -07002301
Luiz Capitulino222f1802006-03-20 22:16:13 -08002302 datalen = pkt_dev->cur_pkt_size - 14 - sizeof(struct ipv6hdr) - sizeof(struct udphdr); /* Eth + IPh + UDPh */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303
Luiz Capitulino222f1802006-03-20 22:16:13 -08002304 if (datalen < sizeof(struct pktgen_hdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 datalen = sizeof(struct pktgen_hdr);
2306 if (net_ratelimit())
Luiz Capitulino222f1802006-03-20 22:16:13 -08002307 printk(KERN_INFO "pktgen: increased datalen to %d\n",
2308 datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 }
2310
2311 udph->source = htons(pkt_dev->cur_udp_src);
2312 udph->dest = htons(pkt_dev->cur_udp_dst);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002313 udph->len = htons(datalen + sizeof(struct udphdr));
2314 udph->check = 0; /* No checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315
Luiz Capitulino222f1802006-03-20 22:16:13 -08002316 *(u32 *) iph = __constant_htonl(0x60000000); /* Version + flow */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317
2318 iph->hop_limit = 32;
2319
2320 iph->payload_len = htons(sizeof(struct udphdr) + datalen);
2321 iph->nexthdr = IPPROTO_UDP;
2322
2323 ipv6_addr_copy(&iph->daddr, &pkt_dev->cur_in6_daddr);
2324 ipv6_addr_copy(&iph->saddr, &pkt_dev->cur_in6_saddr);
2325
Luiz Capitulino222f1802006-03-20 22:16:13 -08002326 skb->mac.raw = ((u8 *) iph) - 14;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 skb->protocol = __constant_htons(ETH_P_IPV6);
2328 skb->dev = odev;
2329 skb->pkt_type = PACKET_HOST;
2330
Luiz Capitulino222f1802006-03-20 22:16:13 -08002331 if (pkt_dev->nfrags <= 0)
2332 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 else {
2334 int frags = pkt_dev->nfrags;
2335 int i;
2336
Luiz Capitulino222f1802006-03-20 22:16:13 -08002337 pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
2338
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 if (frags > MAX_SKB_FRAGS)
2340 frags = MAX_SKB_FRAGS;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002341 if (datalen > frags * PAGE_SIZE) {
2342 skb_put(skb, datalen - frags * PAGE_SIZE);
2343 datalen = frags * PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 }
2345
2346 i = 0;
2347 while (datalen > 0) {
2348 struct page *page = alloc_pages(GFP_KERNEL, 0);
2349 skb_shinfo(skb)->frags[i].page = page;
2350 skb_shinfo(skb)->frags[i].page_offset = 0;
2351 skb_shinfo(skb)->frags[i].size =
Luiz Capitulino222f1802006-03-20 22:16:13 -08002352 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 datalen -= skb_shinfo(skb)->frags[i].size;
2354 skb->len += skb_shinfo(skb)->frags[i].size;
2355 skb->data_len += skb_shinfo(skb)->frags[i].size;
2356 i++;
2357 skb_shinfo(skb)->nr_frags = i;
2358 }
2359
2360 while (i < frags) {
2361 int rem;
2362
2363 if (i == 0)
2364 break;
2365
2366 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
2367 if (rem == 0)
2368 break;
2369
2370 skb_shinfo(skb)->frags[i - 1].size -= rem;
2371
Luiz Capitulino222f1802006-03-20 22:16:13 -08002372 skb_shinfo(skb)->frags[i] =
2373 skb_shinfo(skb)->frags[i - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 get_page(skb_shinfo(skb)->frags[i].page);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002375 skb_shinfo(skb)->frags[i].page =
2376 skb_shinfo(skb)->frags[i - 1].page;
2377 skb_shinfo(skb)->frags[i].page_offset +=
2378 skb_shinfo(skb)->frags[i - 1].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 skb_shinfo(skb)->frags[i].size = rem;
2380 i++;
2381 skb_shinfo(skb)->nr_frags = i;
2382 }
2383 }
2384
Luiz Capitulino222f1802006-03-20 22:16:13 -08002385 /* Stamp the time, and sequence number, convert them to network byte order */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 /* should we update cloned packets too ? */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002387 if (pgh) {
2388 struct timeval timestamp;
2389
2390 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2391 pgh->seq_num = htonl(pkt_dev->seq_num);
2392
2393 do_gettimeofday(&timestamp);
2394 pgh->tv_sec = htonl(timestamp.tv_sec);
2395 pgh->tv_usec = htonl(timestamp.tv_usec);
2396 }
2397 pkt_dev->seq_num++;
2398
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 return skb;
2400}
2401
Luiz Capitulino222f1802006-03-20 22:16:13 -08002402static inline struct sk_buff *fill_packet(struct net_device *odev,
2403 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404{
Luiz Capitulino222f1802006-03-20 22:16:13 -08002405 if (pkt_dev->flags & F_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 return fill_packet_ipv6(odev, pkt_dev);
2407 else
2408 return fill_packet_ipv4(odev, pkt_dev);
2409}
2410
Luiz Capitulino222f1802006-03-20 22:16:13 -08002411static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412{
Luiz Capitulino222f1802006-03-20 22:16:13 -08002413 pkt_dev->seq_num = 1;
2414 pkt_dev->idle_acc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002416 pkt_dev->tx_bytes = 0;
2417 pkt_dev->errors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418}
2419
2420/* Set up structure for sending pkts, clear counters */
2421
2422static void pktgen_run(struct pktgen_thread *t)
2423{
Luiz Capitulino222f1802006-03-20 22:16:13 -08002424 struct pktgen_dev *pkt_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 int started = 0;
2426
2427 PG_DEBUG(printk("pktgen: entering pktgen_run. %p\n", t));
2428
2429 if_lock(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002430 for (pkt_dev = t->if_list; pkt_dev; pkt_dev = pkt_dev->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431
2432 /*
2433 * setup odev and create initial packet.
2434 */
2435 pktgen_setup_inject(pkt_dev);
2436
Luiz Capitulino222f1802006-03-20 22:16:13 -08002437 if (pkt_dev->odev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 pktgen_clear_counters(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002439 pkt_dev->running = 1; /* Cranke yeself! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 pkt_dev->skb = NULL;
2441 pkt_dev->started_at = getCurUs();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002442 pkt_dev->next_tx_us = getCurUs(); /* Transmit immediately */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 pkt_dev->next_tx_ns = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002444
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 strcpy(pkt_dev->result, "Starting");
2446 started++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002447 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 strcpy(pkt_dev->result, "Error starting");
2449 }
2450 if_unlock(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002451 if (started)
2452 t->control &= ~(T_STOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453}
2454
2455static void pktgen_stop_all_threads_ifs(void)
2456{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002457 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458
Arthur Kepner95ed63f2006-03-20 21:26:56 -08002459 PG_DEBUG(printk("pktgen: entering pktgen_stop_all_threads_ifs.\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460
2461 thread_lock();
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002462
2463 list_for_each_entry(t, &pktgen_threads, th_list)
Arthur Kepner95ed63f2006-03-20 21:26:56 -08002464 t->control |= T_STOP;
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002465
Stephen Hemmingerb4099fa2005-10-14 15:32:22 -07002466 thread_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467}
2468
Luiz Capitulino222f1802006-03-20 22:16:13 -08002469static int thread_is_running(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470{
Luiz Capitulino222f1802006-03-20 22:16:13 -08002471 struct pktgen_dev *next;
2472 int res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473
Luiz Capitulino222f1802006-03-20 22:16:13 -08002474 for (next = t->if_list; next; next = next->next) {
2475 if (next->running) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 res = 1;
2477 break;
2478 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002479 }
2480 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481}
2482
Luiz Capitulino222f1802006-03-20 22:16:13 -08002483static int pktgen_wait_thread_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484{
Luiz Capitulino222f1802006-03-20 22:16:13 -08002485 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486
Luiz Capitulino222f1802006-03-20 22:16:13 -08002487 while (thread_is_running(t)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488
Luiz Capitulino222f1802006-03-20 22:16:13 -08002489 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490
Luiz Capitulino222f1802006-03-20 22:16:13 -08002491 msleep_interruptible(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492
Luiz Capitulino222f1802006-03-20 22:16:13 -08002493 if (signal_pending(current))
2494 goto signal;
2495 if_lock(t);
2496 }
2497 if_unlock(t);
2498 return 1;
2499signal:
2500 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501}
2502
2503static int pktgen_wait_all_threads_run(void)
2504{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002505 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 int sig = 1;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002507
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002508 thread_lock();
2509
2510 list_for_each_entry(t, &pktgen_threads, th_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 sig = pktgen_wait_thread_run(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002512 if (sig == 0)
2513 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 }
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002515
2516 if (sig == 0)
2517 list_for_each_entry(t, &pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 t->control |= (T_STOP);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002519
2520 thread_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 return sig;
2522}
2523
2524static void pktgen_run_all_threads(void)
2525{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002526 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527
2528 PG_DEBUG(printk("pktgen: entering pktgen_run_all_threads.\n"));
2529
2530 thread_lock();
2531
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002532 list_for_each_entry(t, &pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 t->control |= (T_RUN);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002534
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 thread_unlock();
2536
Luiz Capitulino222f1802006-03-20 22:16:13 -08002537 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
2538
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 pktgen_wait_all_threads_run();
2540}
2541
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
2543{
Luiz Capitulino222f1802006-03-20 22:16:13 -08002544 __u64 total_us, bps, mbps, pps, idle;
2545 char *p = pkt_dev->result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546
Luiz Capitulino222f1802006-03-20 22:16:13 -08002547 total_us = pkt_dev->stopped_at - pkt_dev->started_at;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548
Luiz Capitulino222f1802006-03-20 22:16:13 -08002549 idle = pkt_dev->idle_acc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550
Luiz Capitulino222f1802006-03-20 22:16:13 -08002551 p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
2552 (unsigned long long)total_us,
2553 (unsigned long long)(total_us - idle),
2554 (unsigned long long)idle,
2555 (unsigned long long)pkt_dev->sofar,
2556 pkt_dev->cur_pkt_size, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557
Luiz Capitulino222f1802006-03-20 22:16:13 -08002558 pps = pkt_dev->sofar * USEC_PER_SEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559
Luiz Capitulino222f1802006-03-20 22:16:13 -08002560 while ((total_us >> 32) != 0) {
2561 pps >>= 1;
2562 total_us >>= 1;
2563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564
Luiz Capitulino222f1802006-03-20 22:16:13 -08002565 do_div(pps, total_us);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566
Luiz Capitulino222f1802006-03-20 22:16:13 -08002567 bps = pps * 8 * pkt_dev->cur_pkt_size;
2568
2569 mbps = bps;
2570 do_div(mbps, 1000000);
2571 p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu",
2572 (unsigned long long)pps,
2573 (unsigned long long)mbps,
2574 (unsigned long long)bps,
2575 (unsigned long long)pkt_dev->errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577
2578/* Set stopped-at timer, remove from running list, do counters & statistics */
2579
Luiz Capitulino222f1802006-03-20 22:16:13 -08002580static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581{
Luiz Capitulino222f1802006-03-20 22:16:13 -08002582 int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08002583
Luiz Capitulino222f1802006-03-20 22:16:13 -08002584 if (!pkt_dev->running) {
2585 printk("pktgen: interface: %s is already stopped\n",
2586 pkt_dev->ifname);
2587 return -EINVAL;
2588 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589
Luiz Capitulino222f1802006-03-20 22:16:13 -08002590 pkt_dev->stopped_at = getCurUs();
2591 pkt_dev->running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592
Arthur Kepner95ed63f2006-03-20 21:26:56 -08002593 show_results(pkt_dev, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594
Luiz Capitulino222f1802006-03-20 22:16:13 -08002595 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596}
2597
Luiz Capitulino222f1802006-03-20 22:16:13 -08002598static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599{
2600 struct pktgen_dev *next, *best = NULL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002601
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 if_lock(t);
2603
Luiz Capitulino222f1802006-03-20 22:16:13 -08002604 for (next = t->if_list; next; next = next->next) {
2605 if (!next->running)
2606 continue;
2607 if (best == NULL)
2608 best = next;
2609 else if (next->next_tx_us < best->next_tx_us)
2610 best = next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 }
2612 if_unlock(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002613 return best;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614}
2615
Luiz Capitulino222f1802006-03-20 22:16:13 -08002616static void pktgen_stop(struct pktgen_thread *t)
2617{
2618 struct pktgen_dev *next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619
Arthur Kepner95ed63f2006-03-20 21:26:56 -08002620 PG_DEBUG(printk("pktgen: entering pktgen_stop\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621
Luiz Capitulino222f1802006-03-20 22:16:13 -08002622 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623
Luiz Capitulino222f1802006-03-20 22:16:13 -08002624 for (next = t->if_list; next; next = next->next) {
2625 pktgen_stop_device(next);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08002626 if (next->skb)
2627 kfree_skb(next->skb);
2628
2629 next->skb = NULL;
2630 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631
Luiz Capitulino222f1802006-03-20 22:16:13 -08002632 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633}
2634
Arthur Kepner95ed63f2006-03-20 21:26:56 -08002635/*
2636 * one of our devices needs to be removed - find it
2637 * and remove it
2638 */
2639static void pktgen_rem_one_if(struct pktgen_thread *t)
2640{
2641 struct pktgen_dev *cur, *next = NULL;
2642
2643 PG_DEBUG(printk("pktgen: entering pktgen_rem_one_if\n"));
2644
2645 if_lock(t);
2646
Luiz Capitulino222f1802006-03-20 22:16:13 -08002647 for (cur = t->if_list; cur; cur = next) {
Arthur Kepner95ed63f2006-03-20 21:26:56 -08002648 next = cur->next;
2649
Luiz Capitulino222f1802006-03-20 22:16:13 -08002650 if (!cur->removal_mark)
2651 continue;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08002652
2653 if (cur->skb)
2654 kfree_skb(cur->skb);
2655 cur->skb = NULL;
2656
2657 pktgen_remove_device(t, cur);
2658
2659 break;
2660 }
2661
2662 if_unlock(t);
2663}
2664
Luiz Capitulino222f1802006-03-20 22:16:13 -08002665static void pktgen_rem_all_ifs(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666{
Luiz Capitulino222f1802006-03-20 22:16:13 -08002667 struct pktgen_dev *cur, *next = NULL;
2668
2669 /* Remove all devices, free mem */
Arthur Kepner95ed63f2006-03-20 21:26:56 -08002670
2671 PG_DEBUG(printk("pktgen: entering pktgen_rem_all_ifs\n"));
Luiz Capitulino222f1802006-03-20 22:16:13 -08002672 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673
Luiz Capitulino222f1802006-03-20 22:16:13 -08002674 for (cur = t->if_list; cur; cur = next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 next = cur->next;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08002676
2677 if (cur->skb)
2678 kfree_skb(cur->skb);
2679 cur->skb = NULL;
2680
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 pktgen_remove_device(t, cur);
2682 }
2683
Luiz Capitulino222f1802006-03-20 22:16:13 -08002684 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685}
2686
Luiz Capitulino222f1802006-03-20 22:16:13 -08002687static void pktgen_rem_thread(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688{
Luiz Capitulino222f1802006-03-20 22:16:13 -08002689 /* Remove from the thread list */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07002691 remove_proc_entry(t->name, pg_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692
Stephen Hemmingerb4099fa2005-10-14 15:32:22 -07002693 thread_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002695 list_del(&t->th_list);
2696
Luiz Capitulino222f1802006-03-20 22:16:13 -08002697 thread_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698}
2699
2700static __inline__ void pktgen_xmit(struct pktgen_dev *pkt_dev)
2701{
2702 struct net_device *odev = NULL;
2703 __u64 idle_start = 0;
2704 int ret;
2705
2706 odev = pkt_dev->odev;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002707
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 if (pkt_dev->delay_us || pkt_dev->delay_ns) {
2709 u64 now;
2710
2711 now = getCurUs();
2712 if (now < pkt_dev->next_tx_us)
2713 spin(pkt_dev, pkt_dev->next_tx_us);
2714
2715 /* This is max DELAY, this has special meaning of
2716 * "never transmit"
2717 */
2718 if (pkt_dev->delay_us == 0x7FFFFFFF) {
2719 pkt_dev->next_tx_us = getCurUs() + pkt_dev->delay_us;
2720 pkt_dev->next_tx_ns = pkt_dev->delay_ns;
2721 goto out;
2722 }
2723 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002724
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 if (netif_queue_stopped(odev) || need_resched()) {
2726 idle_start = getCurUs();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002727
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 if (!netif_running(odev)) {
2729 pktgen_stop_device(pkt_dev);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08002730 if (pkt_dev->skb)
2731 kfree_skb(pkt_dev->skb);
2732 pkt_dev->skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 goto out;
2734 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002735 if (need_resched())
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 schedule();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002737
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 pkt_dev->idle_acc += getCurUs() - idle_start;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002739
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 if (netif_queue_stopped(odev)) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002741 pkt_dev->next_tx_us = getCurUs(); /* TODO */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 pkt_dev->next_tx_ns = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002743 goto out; /* Try the next interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 }
2745 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002746
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 if (pkt_dev->last_ok || !pkt_dev->skb) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002748 if ((++pkt_dev->clone_count >= pkt_dev->clone_skb)
2749 || (!pkt_dev->skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 /* build a new pkt */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002751 if (pkt_dev->skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 kfree_skb(pkt_dev->skb);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002753
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 pkt_dev->skb = fill_packet(odev, pkt_dev);
2755 if (pkt_dev->skb == NULL) {
2756 printk("pktgen: ERROR: couldn't allocate skb in fill_packet.\n");
2757 schedule();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002758 pkt_dev->clone_count--; /* back out increment, OOM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 goto out;
2760 }
2761 pkt_dev->allocated_skbs++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002762 pkt_dev->clone_count = 0; /* reset counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 }
2764 }
Arthur Kepner95ed63f2006-03-20 21:26:56 -08002765
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 spin_lock_bh(&odev->xmit_lock);
2767 if (!netif_queue_stopped(odev)) {
2768
2769 atomic_inc(&(pkt_dev->skb->users));
Luiz Capitulino222f1802006-03-20 22:16:13 -08002770 retry_now:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 ret = odev->hard_start_xmit(pkt_dev->skb, odev);
2772 if (likely(ret == NETDEV_TX_OK)) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002773 pkt_dev->last_ok = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 pkt_dev->sofar++;
2775 pkt_dev->seq_num++;
2776 pkt_dev->tx_bytes += pkt_dev->cur_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002777
2778 } else if (ret == NETDEV_TX_LOCKED
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 && (odev->features & NETIF_F_LLTX)) {
2780 cpu_relax();
2781 goto retry_now;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002782 } else { /* Retry it next time */
2783
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 atomic_dec(&(pkt_dev->skb->users));
Luiz Capitulino222f1802006-03-20 22:16:13 -08002785
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 if (debug && net_ratelimit())
2787 printk(KERN_INFO "pktgen: Hard xmit error\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08002788
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 pkt_dev->errors++;
2790 pkt_dev->last_ok = 0;
2791 }
2792
2793 pkt_dev->next_tx_us = getCurUs();
2794 pkt_dev->next_tx_ns = 0;
2795
2796 pkt_dev->next_tx_us += pkt_dev->delay_us;
2797 pkt_dev->next_tx_ns += pkt_dev->delay_ns;
2798
2799 if (pkt_dev->next_tx_ns > 1000) {
2800 pkt_dev->next_tx_us++;
2801 pkt_dev->next_tx_ns -= 1000;
2802 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002803 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804
Luiz Capitulino222f1802006-03-20 22:16:13 -08002805 else { /* Retry it next time */
2806 pkt_dev->last_ok = 0;
2807 pkt_dev->next_tx_us = getCurUs(); /* TODO */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 pkt_dev->next_tx_ns = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002809 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810
2811 spin_unlock_bh(&odev->xmit_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002812
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 /* If pkt_dev->count is zero, then run forever */
2814 if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
2815 if (atomic_read(&(pkt_dev->skb->users)) != 1) {
2816 idle_start = getCurUs();
2817 while (atomic_read(&(pkt_dev->skb->users)) != 1) {
2818 if (signal_pending(current)) {
2819 break;
2820 }
2821 schedule();
2822 }
2823 pkt_dev->idle_acc += getCurUs() - idle_start;
2824 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002825
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 /* Done with this */
2827 pktgen_stop_device(pkt_dev);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08002828 if (pkt_dev->skb)
2829 kfree_skb(pkt_dev->skb);
2830 pkt_dev->skb = NULL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002831 }
2832out:;
2833}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834
2835/*
2836 * Main loop of the thread goes here
2837 */
2838
Luiz Capitulino222f1802006-03-20 22:16:13 -08002839static void pktgen_thread_worker(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840{
2841 DEFINE_WAIT(wait);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002842 struct pktgen_dev *pkt_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 int cpu = t->cpu;
2844 sigset_t tmpsig;
2845 u32 max_before_softirq;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002846 u32 tx_since_softirq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847
2848 daemonize("pktgen/%d", cpu);
2849
Luiz Capitulino222f1802006-03-20 22:16:13 -08002850 /* Block all signals except SIGKILL, SIGSTOP and SIGTERM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851
Luiz Capitulino222f1802006-03-20 22:16:13 -08002852 spin_lock_irq(&current->sighand->siglock);
2853 tmpsig = current->blocked;
2854 siginitsetinv(&current->blocked,
2855 sigmask(SIGKILL) | sigmask(SIGSTOP) | sigmask(SIGTERM));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856
Luiz Capitulino222f1802006-03-20 22:16:13 -08002857 recalc_sigpending();
2858 spin_unlock_irq(&current->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859
2860 /* Migrate to the right CPU */
2861 set_cpus_allowed(current, cpumask_of_cpu(cpu));
Luiz Capitulino222f1802006-03-20 22:16:13 -08002862 if (smp_processor_id() != cpu)
2863 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864
2865 init_waitqueue_head(&t->queue);
2866
2867 t->control &= ~(T_TERMINATE);
2868 t->control &= ~(T_RUN);
2869 t->control &= ~(T_STOP);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08002870 t->control &= ~(T_REMDEVALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 t->control &= ~(T_REMDEV);
2872
Luiz Capitulino222f1802006-03-20 22:16:13 -08002873 t->pid = current->pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874
Luiz Capitulino222f1802006-03-20 22:16:13 -08002875 PG_DEBUG(printk("pktgen: starting pktgen/%d: pid=%d\n", cpu, current->pid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876
2877 max_before_softirq = t->max_before_softirq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878
Luiz Capitulino222f1802006-03-20 22:16:13 -08002879 __set_current_state(TASK_INTERRUPTIBLE);
2880 mb();
2881
2882 while (1) {
2883
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 __set_current_state(TASK_RUNNING);
2885
2886 /*
2887 * Get next dev to xmit -- if any.
2888 */
2889
Luiz Capitulino222f1802006-03-20 22:16:13 -08002890 pkt_dev = next_to_run(t);
2891
2892 if (pkt_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893
2894 pktgen_xmit(pkt_dev);
2895
2896 /*
2897 * We like to stay RUNNING but must also give
2898 * others fair share.
2899 */
2900
2901 tx_since_softirq += pkt_dev->last_ok;
2902
2903 if (tx_since_softirq > max_before_softirq) {
2904 if (local_softirq_pending())
2905 do_softirq();
2906 tx_since_softirq = 0;
2907 }
2908 } else {
2909 prepare_to_wait(&(t->queue), &wait, TASK_INTERRUPTIBLE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002910 schedule_timeout(HZ / 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 finish_wait(&(t->queue), &wait);
2912 }
2913
Luiz Capitulino222f1802006-03-20 22:16:13 -08002914 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 * Back from sleep, either due to the timeout or signal.
2916 * We check if we have any "posted" work for us.
2917 */
2918
Luiz Capitulino222f1802006-03-20 22:16:13 -08002919 if (t->control & T_TERMINATE || signal_pending(current))
2920 /* we received a request to terminate ourself */
2921 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922
Luiz Capitulino222f1802006-03-20 22:16:13 -08002923 if (t->control & T_STOP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 pktgen_stop(t);
2925 t->control &= ~(T_STOP);
2926 }
2927
Luiz Capitulino222f1802006-03-20 22:16:13 -08002928 if (t->control & T_RUN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 pktgen_run(t);
2930 t->control &= ~(T_RUN);
2931 }
2932
Luiz Capitulino222f1802006-03-20 22:16:13 -08002933 if (t->control & T_REMDEVALL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 pktgen_rem_all_ifs(t);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08002935 t->control &= ~(T_REMDEVALL);
2936 }
2937
Luiz Capitulino222f1802006-03-20 22:16:13 -08002938 if (t->control & T_REMDEV) {
Arthur Kepner95ed63f2006-03-20 21:26:56 -08002939 pktgen_rem_one_if(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 t->control &= ~(T_REMDEV);
2941 }
2942
Luiz Capitulino222f1802006-03-20 22:16:13 -08002943 if (need_resched())
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 schedule();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002945 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946
Luiz Capitulino222f1802006-03-20 22:16:13 -08002947 PG_DEBUG(printk("pktgen: %s stopping all device\n", t->name));
2948 pktgen_stop(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949
Luiz Capitulino222f1802006-03-20 22:16:13 -08002950 PG_DEBUG(printk("pktgen: %s removing all device\n", t->name));
2951 pktgen_rem_all_ifs(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952
Luiz Capitulino222f1802006-03-20 22:16:13 -08002953 PG_DEBUG(printk("pktgen: %s removing thread.\n", t->name));
2954 pktgen_rem_thread(t);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002955
2956 t->removed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957}
2958
Luiz Capitulino222f1802006-03-20 22:16:13 -08002959static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
2960 const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961{
Luiz Capitulino222f1802006-03-20 22:16:13 -08002962 struct pktgen_dev *pkt_dev = NULL;
2963 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964
Luiz Capitulino222f1802006-03-20 22:16:13 -08002965 for (pkt_dev = t->if_list; pkt_dev; pkt_dev = pkt_dev->next) {
2966 if (strncmp(pkt_dev->ifname, ifname, IFNAMSIZ) == 0) {
2967 break;
2968 }
2969 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970
Luiz Capitulino222f1802006-03-20 22:16:13 -08002971 if_unlock(t);
2972 PG_DEBUG(printk("pktgen: find_dev(%s) returning %p\n", ifname, pkt_dev));
2973 return pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974}
2975
2976/*
2977 * Adds a dev at front of if_list.
2978 */
2979
Luiz Capitulino222f1802006-03-20 22:16:13 -08002980static int add_dev_to_thread(struct pktgen_thread *t,
2981 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982{
2983 int rv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984
Luiz Capitulino222f1802006-03-20 22:16:13 -08002985 if_lock(t);
2986
2987 if (pkt_dev->pg_thread) {
2988 printk("pktgen: ERROR: already assigned to a thread.\n");
2989 rv = -EBUSY;
2990 goto out;
2991 }
2992 pkt_dev->next = t->if_list;
2993 t->if_list = pkt_dev;
2994 pkt_dev->pg_thread = t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 pkt_dev->running = 0;
2996
Luiz Capitulino222f1802006-03-20 22:16:13 -08002997out:
2998 if_unlock(t);
2999 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000}
3001
3002/* Called under thread lock */
3003
Luiz Capitulino222f1802006-03-20 22:16:13 -08003004static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003006 struct pktgen_dev *pkt_dev;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003007 struct proc_dir_entry *pe;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003008
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 /* We don't allow a device to be on several threads */
3010
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003011 pkt_dev = __pktgen_NN_threads(ifname, FIND);
3012 if (pkt_dev) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08003013 printk("pktgen: ERROR: interface already used.\n");
3014 return -EBUSY;
3015 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003016
3017 pkt_dev = kzalloc(sizeof(struct pktgen_dev), GFP_KERNEL);
3018 if (!pkt_dev)
3019 return -ENOMEM;
3020
Luiz Capitulino222f1802006-03-20 22:16:13 -08003021 pkt_dev->flows = vmalloc(MAX_CFLOWS * sizeof(struct flow_state));
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003022 if (pkt_dev->flows == NULL) {
3023 kfree(pkt_dev);
3024 return -ENOMEM;
3025 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003026 memset(pkt_dev->flows, 0, MAX_CFLOWS * sizeof(struct flow_state));
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003027
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003028 pkt_dev->removal_mark = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003029 pkt_dev->min_pkt_size = ETH_ZLEN;
3030 pkt_dev->max_pkt_size = ETH_ZLEN;
3031 pkt_dev->nfrags = 0;
3032 pkt_dev->clone_skb = pg_clone_skb_d;
3033 pkt_dev->delay_us = pg_delay_d / 1000;
3034 pkt_dev->delay_ns = pg_delay_d % 1000;
3035 pkt_dev->count = pg_count_d;
3036 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003037 pkt_dev->udp_src_min = 9; /* sink port */
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003038 pkt_dev->udp_src_max = 9;
3039 pkt_dev->udp_dst_min = 9;
3040 pkt_dev->udp_dst_max = 9;
3041
3042 strncpy(pkt_dev->ifname, ifname, IFNAMSIZ);
3043
Luiz Capitulino222f1802006-03-20 22:16:13 -08003044 if (!pktgen_setup_dev(pkt_dev)) {
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003045 printk("pktgen: ERROR: pktgen_setup_dev failed.\n");
3046 if (pkt_dev->flows)
3047 vfree(pkt_dev->flows);
3048 kfree(pkt_dev);
3049 return -ENODEV;
3050 }
3051
3052 pe = create_proc_entry(ifname, 0600, pg_proc_dir);
3053 if (!pe) {
3054 printk("pktgen: cannot create %s/%s procfs entry.\n",
3055 PG_PROC_DIR, ifname);
3056 if (pkt_dev->flows)
3057 vfree(pkt_dev->flows);
3058 kfree(pkt_dev);
3059 return -EINVAL;
3060 }
3061 pe->proc_fops = &pktgen_if_fops;
3062 pe->data = pkt_dev;
3063
3064 return add_dev_to_thread(t, pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065}
3066
Luiz Capitulino222f1802006-03-20 22:16:13 -08003067static struct pktgen_thread *__init pktgen_find_thread(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003069 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070
Stephen Hemmingerb4099fa2005-10-14 15:32:22 -07003071 thread_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003073 list_for_each_entry(t, &pktgen_threads, th_list)
3074 if (strcmp(t->name, name) == 0) {
3075 thread_unlock();
3076 return t;
3077 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078
Luiz Capitulino222f1802006-03-20 22:16:13 -08003079 thread_unlock();
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003080 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081}
3082
Luiz Capitulino222f1802006-03-20 22:16:13 -08003083static int __init pktgen_create_thread(const char *name, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084{
Luiz Capitulino12e18722006-03-20 22:17:00 -08003085 int err;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003086 struct pktgen_thread *t = NULL;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003087 struct proc_dir_entry *pe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088
Luiz Capitulino222f1802006-03-20 22:16:13 -08003089 if (strlen(name) > 31) {
3090 printk("pktgen: ERROR: Thread name cannot be more than 31 characters.\n");
3091 return -EINVAL;
3092 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093
Luiz Capitulino222f1802006-03-20 22:16:13 -08003094 if (pktgen_find_thread(name)) {
3095 printk("pktgen: ERROR: thread: %s already exists\n", name);
3096 return -EINVAL;
3097 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098
Luiz Capitulino222f1802006-03-20 22:16:13 -08003099 t = kzalloc(sizeof(struct pktgen_thread), GFP_KERNEL);
3100 if (!t) {
3101 printk("pktgen: ERROR: out of memory, can't create new thread.\n");
3102 return -ENOMEM;
3103 }
3104
3105 strcpy(t->name, name);
3106 spin_lock_init(&t->if_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 t->cpu = cpu;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003108
3109 pe = create_proc_entry(t->name, 0600, pg_proc_dir);
3110 if (!pe) {
3111 printk("pktgen: cannot create %s/%s procfs entry.\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003112 PG_PROC_DIR, t->name);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003113 kfree(t);
3114 return -EINVAL;
3115 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003116
3117 pe->proc_fops = &pktgen_thread_fops;
3118 pe->data = t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003120 list_add_tail(&t->th_list, &pktgen_threads);
3121
3122 t->removed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123
Luiz Capitulino12e18722006-03-20 22:17:00 -08003124 err = kernel_thread((void *)pktgen_thread_worker, (void *)t,
3125 CLONE_FS | CLONE_FILES | CLONE_SIGHAND);
3126 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 printk("pktgen: kernel_thread() failed for cpu %d\n", t->cpu);
Luiz Capitulino12e18722006-03-20 22:17:00 -08003128 remove_proc_entry(t->name, pg_proc_dir);
3129 list_del(&t->th_list);
3130 kfree(t);
3131 return err;
3132 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133
3134 return 0;
3135}
3136
3137/*
3138 * Removes a device from the thread if_list.
3139 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003140static void _rem_dev_from_if_list(struct pktgen_thread *t,
3141 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142{
3143 struct pktgen_dev *i, *prev = NULL;
3144
3145 i = t->if_list;
3146
Luiz Capitulino222f1802006-03-20 22:16:13 -08003147 while (i) {
3148 if (i == pkt_dev) {
3149 if (prev)
3150 prev->next = i->next;
3151 else
3152 t->if_list = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 break;
3154 }
3155 prev = i;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003156 i = i->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 }
3158}
3159
Luiz Capitulino222f1802006-03-20 22:16:13 -08003160static int pktgen_remove_device(struct pktgen_thread *t,
3161 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162{
3163
3164 PG_DEBUG(printk("pktgen: remove_device pkt_dev=%p\n", pkt_dev));
3165
Luiz Capitulino222f1802006-03-20 22:16:13 -08003166 if (pkt_dev->running) {
3167 printk("pktgen:WARNING: trying to remove a running interface, stopping it now.\n");
3168 pktgen_stop_device(pkt_dev);
3169 }
3170
3171 /* Dis-associate from the interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172
3173 if (pkt_dev->odev) {
3174 dev_put(pkt_dev->odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003175 pkt_dev->odev = NULL;
3176 }
3177
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 /* And update the thread if_list */
3179
3180 _rem_dev_from_if_list(t, pkt_dev);
3181
Luiz Capitulino222f1802006-03-20 22:16:13 -08003182 /* Clean up proc file system */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003184 remove_proc_entry(pkt_dev->ifname, pg_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185
3186 if (pkt_dev->flows)
3187 vfree(pkt_dev->flows);
3188 kfree(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003189 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190}
3191
Luiz Capitulino222f1802006-03-20 22:16:13 -08003192static int __init pg_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193{
3194 int cpu;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003195 struct proc_dir_entry *pe;
3196
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 printk(version);
3198
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003199 pg_proc_dir = proc_mkdir(PG_PROC_DIR, proc_net);
3200 if (!pg_proc_dir)
3201 return -ENODEV;
3202 pg_proc_dir->owner = THIS_MODULE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003204 pe = create_proc_entry(PGCTRL, 0600, pg_proc_dir);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003205 if (pe == NULL) {
3206 printk("pktgen: ERROR: cannot create %s procfs entry.\n",
3207 PGCTRL);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003208 proc_net_remove(PG_PROC_DIR);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003209 return -EINVAL;
3210 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211
Luiz Capitulino222f1802006-03-20 22:16:13 -08003212 pe->proc_fops = &pktgen_fops;
3213 pe->data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214
3215 /* Register us to receive netdevice events */
3216 register_netdevice_notifier(&pktgen_notifier_block);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003217
John Hawkes670c02c2005-10-13 09:30:31 -07003218 for_each_online_cpu(cpu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 char buf[30];
3220
Luiz Capitulino222f1802006-03-20 22:16:13 -08003221 sprintf(buf, "kpktgend_%i", cpu);
3222 pktgen_create_thread(buf, cpu);
3223 }
3224 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225}
3226
3227static void __exit pg_cleanup(void)
3228{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003229 struct pktgen_thread *t;
3230 struct list_head *q, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 wait_queue_head_t queue;
3232 init_waitqueue_head(&queue);
3233
Luiz Capitulino222f1802006-03-20 22:16:13 -08003234 /* Stop all interfaces & threads */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003236 list_for_each_safe(q, n, &pktgen_threads) {
3237 t = list_entry(q, struct pktgen_thread, th_list);
3238 t->control |= (T_TERMINATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003240 wait_event_interruptible_timeout(queue, (t->removed == 1), HZ);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242
Luiz Capitulino222f1802006-03-20 22:16:13 -08003243 /* Un-register us from receiving netdevice events */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 unregister_netdevice_notifier(&pktgen_notifier_block);
3245
Luiz Capitulino222f1802006-03-20 22:16:13 -08003246 /* Clean up proc file system */
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003247 remove_proc_entry(PGCTRL, pg_proc_dir);
3248 proc_net_remove(PG_PROC_DIR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249}
3250
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251module_init(pg_init);
3252module_exit(pg_cleanup);
3253
3254MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se");
3255MODULE_DESCRIPTION("Packet Generator tool");
3256MODULE_LICENSE("GPL");
3257module_param(pg_count_d, int, 0);
3258module_param(pg_delay_d, int, 0);
3259module_param(pg_clone_skb_d, int, 0);
3260module_param(debug, int, 0);