blob: 72145d4a260082fc6e798522e95052a36f1643c6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Authors:
3 * Copyright 2001, 2002 by Robert Olsson <robert.olsson@its.uu.se>
4 * Uppsala University and
5 * Swedish University of Agricultural Sciences
6 *
7 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
8 * Ben Greear <greearb@candelatech.com>
9 * Jens Låås <jens.laas@data.slu.se>
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version
14 * 2 of the License, or (at your option) any later version.
15 *
16 *
17 * A tool for loading the network with preconfigurated packets.
18 * The tool is implemented as a linux module. Parameters are output
19 * device, delay (to hard_xmit), number of packets, and whether
20 * to use multiple SKBs or just the same one.
21 * pktgen uses the installed interface's output routine.
22 *
23 * Additional hacking by:
24 *
25 * Jens.Laas@data.slu.se
26 * Improved by ANK. 010120.
27 * Improved by ANK even more. 010212.
28 * MAC address typo fixed. 010417 --ro
29 * Integrated. 020301 --DaveM
30 * Added multiskb option 020301 --DaveM
31 * Scaling of results. 020417--sigurdur@linpro.no
32 * Significant re-work of the module:
33 * * Convert to threaded model to more efficiently be able to transmit
34 * and receive on multiple interfaces at once.
35 * * Converted many counters to __u64 to allow longer runs.
36 * * Allow configuration of ranges, like min/max IP address, MACs,
37 * and UDP-ports, for both source and destination, and can
38 * set to use a random distribution or sequentially walk the range.
39 * * Can now change most values after starting.
40 * * Place 12-byte packet in UDP payload with magic number,
41 * sequence number, and timestamp.
42 * * Add receiver code that detects dropped pkts, re-ordered pkts, and
43 * latencies (with micro-second) precision.
44 * * Add IOCTL interface to easily get counters & configuration.
45 * --Ben Greear <greearb@candelatech.com>
46 *
47 * Renamed multiskb to clone_skb and cleaned up sending core for two distinct
48 * skb modes. A clone_skb=0 mode for Ben "ranges" work and a clone_skb != 0
49 * as a "fastpath" with a configurable number of clones after alloc's.
50 * clone_skb=0 means all packets are allocated this also means ranges time
51 * stamps etc can be used. clone_skb=100 means 1 malloc is followed by 100
52 * clones.
53 *
54 * Also moved to /proc/net/pktgen/
55 * --ro
56 *
57 * Sept 10: Fixed threading/locking. Lots of bone-headed and more clever
58 * mistakes. Also merged in DaveM's patch in the -pre6 patch.
59 * --Ben Greear <greearb@candelatech.com>
60 *
61 * Integrated to 2.5.x 021029 --Lucio Maciel (luciomaciel@zipmail.com.br)
62 *
63 *
64 * 021124 Finished major redesign and rewrite for new functionality.
65 * See Documentation/networking/pktgen.txt for how to use this.
66 *
67 * The new operation:
68 * For each CPU one thread/process is created at start. This process checks
69 * for running devices in the if_list and sends packets until count is 0 it
70 * also the thread checks the thread->control which is used for inter-process
71 * communication. controlling process "posts" operations to the threads this
72 * way. The if_lock should be possible to remove when add/rem_device is merged
73 * into this too.
74 *
75 * By design there should only be *one* "controlling" process. In practice
76 * multiple write accesses gives unpredictable result. Understood by "write"
77 * to /proc gives result code thats should be read be the "writer".
Stephen Hemmingerb4099fa2005-10-14 15:32:22 -070078 * For practical use this should be no problem.
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 *
80 * Note when adding devices to a specific CPU there good idea to also assign
81 * /proc/irq/XX/smp_affinity so TX-interrupts gets bound to the same CPU.
82 * --ro
83 *
84 * Fix refcount off by one if first packet fails, potential null deref,
85 * memleak 030710- KJP
86 *
87 * First "ranges" functionality for ipv6 030726 --ro
88 *
89 * Included flow support. 030802 ANK.
90 *
91 * Fixed unaligned access on IA-64 Grant Grundler <grundler@parisc-linux.org>
92 *
93 * Remove if fix from added Harald Welte <laforge@netfilter.org> 040419
94 * ia64 compilation fix from Aron Griffis <aron@hp.com> 040604
95 *
96 * New xmit() return, do_div and misc clean up by Stephen Hemminger
97 * <shemminger@osdl.org> 040923
98 *
Stephen Hemmingerb4099fa2005-10-14 15:32:22 -070099 * Randy Dunlap fixed u64 printk compiler waring
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 *
101 * Remove FCS from BW calculation. Lennert Buytenhek <buytenh@wantstofly.org>
102 * New time handling. Lennert Buytenhek <buytenh@wantstofly.org> 041213
103 *
104 * Corrections from Nikolai Malykh (nmalykh@bilim.com)
105 * Removed unused flags F_SET_SRCMAC & F_SET_SRCIP 041230
106 *
107 * interruptible_sleep_on_timeout() replaced Nishanth Aravamudan <nacc@us.ibm.com>
108 * 050103
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800109 *
110 * MPLS support by Steven Whitehouse <steve@chygwyn.com>
111 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 */
113#include <linux/sys.h>
114#include <linux/types.h>
115#include <linux/module.h>
116#include <linux/moduleparam.h>
117#include <linux/kernel.h>
118#include <linux/smp_lock.h>
Luiz Capitulino222fa072006-03-20 22:24:27 -0800119#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120#include <linux/sched.h>
121#include <linux/slab.h>
122#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123#include <linux/unistd.h>
124#include <linux/string.h>
125#include <linux/ptrace.h>
126#include <linux/errno.h>
127#include <linux/ioport.h>
128#include <linux/interrupt.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -0800129#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130#include <linux/delay.h>
131#include <linux/timer.h>
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -0800132#include <linux/list.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133#include <linux/init.h>
134#include <linux/skbuff.h>
135#include <linux/netdevice.h>
136#include <linux/inet.h>
137#include <linux/inetdevice.h>
138#include <linux/rtnetlink.h>
139#include <linux/if_arp.h>
140#include <linux/in.h>
141#include <linux/ip.h>
142#include <linux/ipv6.h>
143#include <linux/udp.h>
144#include <linux/proc_fs.h>
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700145#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146#include <linux/wait.h>
Kris Katterjohnf404e9a2006-01-17 13:04:57 -0800147#include <linux/etherdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148#include <net/checksum.h>
149#include <net/ipv6.h>
150#include <net/addrconf.h>
151#include <asm/byteorder.h>
152#include <linux/rcupdate.h>
153#include <asm/bitops.h>
154#include <asm/io.h>
155#include <asm/dma.h>
156#include <asm/uaccess.h>
Luiz Capitulino222f1802006-03-20 22:16:13 -0800157#include <asm/div64.h> /* do_div */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158#include <asm/timex.h>
159
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800160#define VERSION "pktgen v2.67: Packet Generator for packet performance testing.\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162/* #define PG_DEBUG(a) a */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800163#define PG_DEBUG(a)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165/* The buckets are exponential in 'width' */
166#define LAT_BUCKETS_MAX 32
167#define IP_NAME_SZ 32
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800168#define MAX_MPLS_LABELS 16 /* This is the max label stack depth */
169#define MPLS_STACK_BOTTOM __constant_htonl(0x00000100)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171/* Device flag bits */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800172#define F_IPSRC_RND (1<<0) /* IP-Src Random */
173#define F_IPDST_RND (1<<1) /* IP-Dst Random */
174#define F_UDPSRC_RND (1<<2) /* UDP-Src Random */
175#define F_UDPDST_RND (1<<3) /* UDP-Dst Random */
176#define F_MACSRC_RND (1<<4) /* MAC-Src Random */
177#define F_MACDST_RND (1<<5) /* MAC-Dst Random */
178#define F_TXSIZE_RND (1<<6) /* Transmit size is random */
179#define F_IPV6 (1<<7) /* Interface in IPV6 Mode */
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800180#define F_MPLS_RND (1<<8) /* Random MPLS labels */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182/* Thread control flag bits */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800183#define T_TERMINATE (1<<0)
184#define T_STOP (1<<1) /* Stop run */
185#define T_RUN (1<<2) /* Start run */
186#define T_REMDEVALL (1<<3) /* Remove all devs */
187#define T_REMDEV (1<<4) /* Remove one dev */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189/* If lock -- can be removed after some work */
190#define if_lock(t) spin_lock(&(t->if_lock));
191#define if_unlock(t) spin_unlock(&(t->if_lock));
192
193/* Used to help with determining the pkts on receive */
194#define PKTGEN_MAGIC 0xbe9be955
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700195#define PG_PROC_DIR "pktgen"
196#define PGCTRL "pgctrl"
197static struct proc_dir_entry *pg_proc_dir = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199#define MAX_CFLOWS 65536
200
Luiz Capitulino222f1802006-03-20 22:16:13 -0800201struct flow_state {
202 __u32 cur_daddr;
203 int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204};
205
206struct pktgen_dev {
207
208 /*
209 * Try to keep frequent/infrequent used vars. separated.
210 */
211
Luiz Capitulino222f1802006-03-20 22:16:13 -0800212 char ifname[IFNAMSIZ];
213 char result[512];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Luiz Capitulino222f1802006-03-20 22:16:13 -0800215 struct pktgen_thread *pg_thread; /* the owner */
Luiz Capitulinoc26a8012006-03-20 22:18:16 -0800216 struct list_head list; /* Used for chaining in the thread's run-queue */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Luiz Capitulino222f1802006-03-20 22:16:13 -0800218 int running; /* if this changes to false, the test will stop */
219
220 /* If min != max, then we will either do a linear iteration, or
221 * we will do a random selection from within the range.
222 */
223 __u32 flags;
Arthur Kepner95ed63f2006-03-20 21:26:56 -0800224 int removal_mark; /* non-zero => the device is marked for
225 * removal by worker thread */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Luiz Capitulino222f1802006-03-20 22:16:13 -0800227 int min_pkt_size; /* = ETH_ZLEN; */
228 int max_pkt_size; /* = ETH_ZLEN; */
229 int nfrags;
230 __u32 delay_us; /* Default delay */
231 __u32 delay_ns;
232 __u64 count; /* Default No packets to send */
233 __u64 sofar; /* How many pkts we've sent so far */
234 __u64 tx_bytes; /* How many bytes we've transmitted */
235 __u64 errors; /* Errors when trying to transmit, pkts will be re-sent */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Luiz Capitulino222f1802006-03-20 22:16:13 -0800237 /* runtime counters relating to clone_skb */
238 __u64 next_tx_us; /* timestamp of when to tx next */
239 __u32 next_tx_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Luiz Capitulino222f1802006-03-20 22:16:13 -0800241 __u64 allocated_skbs;
242 __u32 clone_count;
243 int last_ok; /* Was last skb sent?
244 * Or a failed transmit of some sort? This will keep
245 * sequence numbers in order, for example.
246 */
247 __u64 started_at; /* micro-seconds */
248 __u64 stopped_at; /* micro-seconds */
249 __u64 idle_acc; /* micro-seconds */
250 __u32 seq_num;
251
252 int clone_skb; /* Use multiple SKBs during packet gen. If this number
253 * is greater than 1, then that many copies of the same
254 * packet will be sent before a new packet is allocated.
255 * For instance, if you want to send 1024 identical packets
256 * before creating a new packet, set clone_skb to 1024.
257 */
258
259 char dst_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
260 char dst_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
261 char src_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
262 char src_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
263
264 struct in6_addr in6_saddr;
265 struct in6_addr in6_daddr;
266 struct in6_addr cur_in6_daddr;
267 struct in6_addr cur_in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 /* For ranges */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800269 struct in6_addr min_in6_daddr;
270 struct in6_addr max_in6_daddr;
271 struct in6_addr min_in6_saddr;
272 struct in6_addr max_in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Luiz Capitulino222f1802006-03-20 22:16:13 -0800274 /* If we're doing ranges, random or incremental, then this
275 * defines the min/max for those ranges.
276 */
277 __u32 saddr_min; /* inclusive, source IP address */
278 __u32 saddr_max; /* exclusive, source IP address */
279 __u32 daddr_min; /* inclusive, dest IP address */
280 __u32 daddr_max; /* exclusive, dest IP address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Luiz Capitulino222f1802006-03-20 22:16:13 -0800282 __u16 udp_src_min; /* inclusive, source UDP port */
283 __u16 udp_src_max; /* exclusive, source UDP port */
284 __u16 udp_dst_min; /* inclusive, dest UDP port */
285 __u16 udp_dst_max; /* exclusive, dest UDP port */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800287 /* MPLS */
288 unsigned nr_labels; /* Depth of stack, 0 = no MPLS */
289 __be32 labels[MAX_MPLS_LABELS];
290
Luiz Capitulino222f1802006-03-20 22:16:13 -0800291 __u32 src_mac_count; /* How many MACs to iterate through */
292 __u32 dst_mac_count; /* How many MACs to iterate through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Luiz Capitulino222f1802006-03-20 22:16:13 -0800294 unsigned char dst_mac[ETH_ALEN];
295 unsigned char src_mac[ETH_ALEN];
296
297 __u32 cur_dst_mac_offset;
298 __u32 cur_src_mac_offset;
299 __u32 cur_saddr;
300 __u32 cur_daddr;
301 __u16 cur_udp_dst;
302 __u16 cur_udp_src;
303 __u32 cur_pkt_size;
304
305 __u8 hh[14];
306 /* = {
307 0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
308
309 We fill in SRC address later
310 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
311 0x08, 0x00
312 };
313 */
314 __u16 pad; /* pad out the hh struct to an even 16 bytes */
315
316 struct sk_buff *skb; /* skb we are to transmit next, mainly used for when we
317 * are transmitting the same one multiple times
318 */
319 struct net_device *odev; /* The out-going device. Note that the device should
320 * have it's pg_info pointer pointing back to this
321 * device. This will be set when the user specifies
322 * the out-going device name (not when the inject is
323 * started as it used to do.)
324 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 struct flow_state *flows;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800326 unsigned cflows; /* Concurrent flows (config) */
327 unsigned lflow; /* Flow length (config) */
328 unsigned nflows; /* accumulated flows (stats) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329};
330
331struct pktgen_hdr {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800332 __u32 pgh_magic;
333 __u32 seq_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 __u32 tv_sec;
335 __u32 tv_usec;
336};
337
338struct pktgen_thread {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800339 spinlock_t if_lock;
Luiz Capitulinoc26a8012006-03-20 22:18:16 -0800340 struct list_head if_list; /* All device here */
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -0800341 struct list_head th_list;
342 int removed;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800343 char name[32];
344 char result[512];
345 u32 max_before_softirq; /* We'll call do_softirq to prevent starvation. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Luiz Capitulino222f1802006-03-20 22:16:13 -0800347 /* Field for thread to receive "posted" events terminate, stop ifs etc. */
348
349 u32 control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 int pid;
351 int cpu;
352
Luiz Capitulino222f1802006-03-20 22:16:13 -0800353 wait_queue_head_t queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354};
355
356#define REMOVE 1
357#define FIND 0
358
359/* This code works around the fact that do_div cannot handle two 64-bit
360 numbers, and regular 64-bit division doesn't work on x86 kernels.
361 --Ben
362*/
363
364#define PG_DIV 0
365
366/* This was emailed to LMKL by: Chris Caputo <ccaputo@alt.net>
367 * Function copied/adapted/optimized from:
368 *
369 * nemesis.sourceforge.net/browse/lib/static/intmath/ix86/intmath.c.html
370 *
371 * Copyright 1994, University of Cambridge Computer Laboratory
372 * All Rights Reserved.
373 *
374 */
Jesper Juhl77933d72005-07-27 11:46:09 -0700375static inline s64 divremdi3(s64 x, s64 y, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800377 u64 a = (x < 0) ? -x : x;
378 u64 b = (y < 0) ? -y : y;
379 u64 res = 0, d = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Luiz Capitulino222f1802006-03-20 22:16:13 -0800381 if (b > 0) {
382 while (b < a) {
383 b <<= 1;
384 d <<= 1;
385 }
386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Luiz Capitulino222f1802006-03-20 22:16:13 -0800388 do {
389 if (a >= b) {
390 a -= b;
391 res += d;
392 }
393 b >>= 1;
394 d >>= 1;
395 }
396 while (d);
397
398 if (PG_DIV == type) {
399 return (((x ^ y) & (1ll << 63)) == 0) ? res : -(s64) res;
400 } else {
401 return ((x & (1ll << 63)) == 0) ? a : -(s64) a;
402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403}
404
405/* End of hacks to deal with 64-bit math on x86 */
406
Stephen Hemmingerb4099fa2005-10-14 15:32:22 -0700407/** Convert to milliseconds */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800408static inline __u64 tv_to_ms(const struct timeval *tv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800410 __u64 ms = tv->tv_usec / 1000;
411 ms += (__u64) tv->tv_sec * (__u64) 1000;
412 return ms;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413}
414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415/** Convert to micro-seconds */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800416static inline __u64 tv_to_us(const struct timeval *tv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800418 __u64 us = tv->tv_usec;
419 us += (__u64) tv->tv_sec * (__u64) 1000000;
420 return us;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421}
422
Luiz Capitulino222f1802006-03-20 22:16:13 -0800423static inline __u64 pg_div(__u64 n, __u32 base)
424{
425 __u64 tmp = n;
426 do_div(tmp, base);
427 /* printk("pktgen: pg_div, n: %llu base: %d rv: %llu\n",
428 n, base, tmp); */
429 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430}
431
Luiz Capitulino222f1802006-03-20 22:16:13 -0800432static inline __u64 pg_div64(__u64 n, __u64 base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800434 __u64 tmp = n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435/*
Stephen Hemmingerb4099fa2005-10-14 15:32:22 -0700436 * How do we know if the architecture we are running on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 * supports division with 64 bit base?
438 *
439 */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800440#if defined(__sparc_v9__) || defined(__powerpc64__) || defined(__alpha__) || defined(__x86_64__) || defined(__ia64__)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Luiz Capitulino222f1802006-03-20 22:16:13 -0800442 do_div(tmp, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443#else
Luiz Capitulino222f1802006-03-20 22:16:13 -0800444 tmp = divremdi3(n, base, PG_DIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445#endif
Luiz Capitulino222f1802006-03-20 22:16:13 -0800446 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447}
448
449static inline u32 pktgen_random(void)
450{
451#if 0
452 __u32 n;
453 get_random_bytes(&n, 4);
454 return n;
455#else
456 return net_random();
457#endif
458}
459
Luiz Capitulino222f1802006-03-20 22:16:13 -0800460static inline __u64 getCurMs(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_ms(&tv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465}
466
Luiz Capitulino222f1802006-03-20 22:16:13 -0800467static inline __u64 getCurUs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800469 struct timeval tv;
470 do_gettimeofday(&tv);
471 return tv_to_us(&tv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472}
473
Luiz Capitulino222f1802006-03-20 22:16:13 -0800474static inline __u64 tv_diff(const struct timeval *a, const struct timeval *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800476 return tv_to_us(a) - tv_to_us(b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477}
478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479/* old include end */
480
481static char version[] __initdata = VERSION;
482
Luiz Capitulino222f1802006-03-20 22:16:13 -0800483static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *i);
484static int pktgen_add_device(struct pktgen_thread *t, const char *ifname);
485static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
486 const char *ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487static int pktgen_device_event(struct notifier_block *, unsigned long, void *);
488static void pktgen_run_all_threads(void);
489static void pktgen_stop_all_threads_ifs(void);
490static int pktgen_stop_device(struct pktgen_dev *pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800491static void pktgen_stop(struct pktgen_thread *t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492static void pktgen_clear_counters(struct pktgen_dev *pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800493static int pktgen_mark_device(const char *ifname);
494static unsigned int scan_ip6(const char *s, char ip[16]);
495static unsigned int fmt_ip6(char *s, const char ip[16]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
497/* Module parameters, defaults. */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800498static int pg_count_d = 1000; /* 1000 pkts by default */
Jaco Kroonf34fbb92005-12-22 12:51:46 -0800499static int pg_delay_d;
500static int pg_clone_skb_d;
501static int debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
Luiz Capitulino222fa072006-03-20 22:24:27 -0800503static DEFINE_MUTEX(pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -0800504static LIST_HEAD(pktgen_threads);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506static struct notifier_block pktgen_notifier_block = {
507 .notifier_call = pktgen_device_event,
508};
509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510/*
511 * /proc handling functions
512 *
513 */
514
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700515static int pgctrl_show(struct seq_file *seq, void *v)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800516{
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700517 seq_puts(seq, VERSION);
518 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519}
520
Luiz Capitulino222f1802006-03-20 22:16:13 -0800521static ssize_t pgctrl_write(struct file *file, const char __user * buf,
522 size_t count, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 int err = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700525 char data[128];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Luiz Capitulino222f1802006-03-20 22:16:13 -0800527 if (!capable(CAP_NET_ADMIN)) {
528 err = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 goto out;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800530 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700532 if (count > sizeof(data))
533 count = sizeof(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 if (copy_from_user(data, buf, count)) {
Stephen Hemmingerb4099fa2005-10-14 15:32:22 -0700536 err = -EFAULT;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700537 goto out;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800538 }
539 data[count - 1] = 0; /* Make string */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Luiz Capitulino222f1802006-03-20 22:16:13 -0800541 if (!strcmp(data, "stop"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 pktgen_stop_all_threads_ifs();
543
Luiz Capitulino222f1802006-03-20 22:16:13 -0800544 else if (!strcmp(data, "start"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 pktgen_run_all_threads();
546
Luiz Capitulino222f1802006-03-20 22:16:13 -0800547 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 printk("pktgen: Unknown command: %s\n", data);
549
550 err = count;
551
Luiz Capitulino222f1802006-03-20 22:16:13 -0800552out:
553 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554}
555
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700556static int pgctrl_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700558 return single_open(file, pgctrl_show, PDE(inode)->data);
559}
560
561static struct file_operations pktgen_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800562 .owner = THIS_MODULE,
563 .open = pgctrl_open,
564 .read = seq_read,
565 .llseek = seq_lseek,
566 .write = pgctrl_write,
567 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700568};
569
570static int pktgen_if_show(struct seq_file *seq, void *v)
571{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 int i;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800573 struct pktgen_dev *pkt_dev = seq->private;
574 __u64 sa;
575 __u64 stopped;
576 __u64 now = getCurUs();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Luiz Capitulino222f1802006-03-20 22:16:13 -0800578 seq_printf(seq,
579 "Params: count %llu min_pkt_size: %u max_pkt_size: %u\n",
580 (unsigned long long)pkt_dev->count, pkt_dev->min_pkt_size,
581 pkt_dev->max_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Luiz Capitulino222f1802006-03-20 22:16:13 -0800583 seq_printf(seq,
584 " frags: %d delay: %u clone_skb: %d ifname: %s\n",
585 pkt_dev->nfrags,
586 1000 * pkt_dev->delay_us + pkt_dev->delay_ns,
587 pkt_dev->clone_skb, pkt_dev->ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Luiz Capitulino222f1802006-03-20 22:16:13 -0800589 seq_printf(seq, " flows: %u flowlen: %u\n", pkt_dev->cflows,
590 pkt_dev->lflow);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Luiz Capitulino222f1802006-03-20 22:16:13 -0800592 if (pkt_dev->flags & F_IPV6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 char b1[128], b2[128], b3[128];
Luiz Capitulino222f1802006-03-20 22:16:13 -0800594 fmt_ip6(b1, pkt_dev->in6_saddr.s6_addr);
595 fmt_ip6(b2, pkt_dev->min_in6_saddr.s6_addr);
596 fmt_ip6(b3, pkt_dev->max_in6_saddr.s6_addr);
597 seq_printf(seq,
598 " saddr: %s min_saddr: %s max_saddr: %s\n", b1,
599 b2, b3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Luiz Capitulino222f1802006-03-20 22:16:13 -0800601 fmt_ip6(b1, pkt_dev->in6_daddr.s6_addr);
602 fmt_ip6(b2, pkt_dev->min_in6_daddr.s6_addr);
603 fmt_ip6(b3, pkt_dev->max_in6_daddr.s6_addr);
604 seq_printf(seq,
605 " daddr: %s min_daddr: %s max_daddr: %s\n", b1,
606 b2, b3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Luiz Capitulino222f1802006-03-20 22:16:13 -0800608 } else
609 seq_printf(seq,
610 " dst_min: %s dst_max: %s\n src_min: %s src_max: %s\n",
611 pkt_dev->dst_min, pkt_dev->dst_max, pkt_dev->src_min,
612 pkt_dev->src_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700614 seq_puts(seq, " src_mac: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Kris Katterjohnf404e9a2006-01-17 13:04:57 -0800616 if (is_zero_ether_addr(pkt_dev->src_mac))
Luiz Capitulino222f1802006-03-20 22:16:13 -0800617 for (i = 0; i < 6; i++)
618 seq_printf(seq, "%02X%s", pkt_dev->odev->dev_addr[i],
619 i == 5 ? " " : ":");
620 else
621 for (i = 0; i < 6; i++)
622 seq_printf(seq, "%02X%s", pkt_dev->src_mac[i],
623 i == 5 ? " " : ":");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Luiz Capitulino222f1802006-03-20 22:16:13 -0800625 seq_printf(seq, "dst_mac: ");
626 for (i = 0; i < 6; i++)
627 seq_printf(seq, "%02X%s", pkt_dev->dst_mac[i],
628 i == 5 ? "\n" : ":");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Luiz Capitulino222f1802006-03-20 22:16:13 -0800630 seq_printf(seq,
631 " udp_src_min: %d udp_src_max: %d udp_dst_min: %d udp_dst_max: %d\n",
632 pkt_dev->udp_src_min, pkt_dev->udp_src_max,
633 pkt_dev->udp_dst_min, pkt_dev->udp_dst_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Luiz Capitulino222f1802006-03-20 22:16:13 -0800635 seq_printf(seq,
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800636 " src_mac_count: %d dst_mac_count: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700637 pkt_dev->src_mac_count, pkt_dev->dst_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800639 if (pkt_dev->nr_labels) {
640 unsigned i;
641 seq_printf(seq, " mpls: ");
642 for(i = 0; i < pkt_dev->nr_labels; i++)
643 seq_printf(seq, "%08x%s", ntohl(pkt_dev->labels[i]),
644 i == pkt_dev->nr_labels-1 ? "\n" : ", ");
645 }
646
647 seq_printf(seq, " Flags: ");
648
Luiz Capitulino222f1802006-03-20 22:16:13 -0800649 if (pkt_dev->flags & F_IPV6)
650 seq_printf(seq, "IPV6 ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Luiz Capitulino222f1802006-03-20 22:16:13 -0800652 if (pkt_dev->flags & F_IPSRC_RND)
653 seq_printf(seq, "IPSRC_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Luiz Capitulino222f1802006-03-20 22:16:13 -0800655 if (pkt_dev->flags & F_IPDST_RND)
656 seq_printf(seq, "IPDST_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Luiz Capitulino222f1802006-03-20 22:16:13 -0800658 if (pkt_dev->flags & F_TXSIZE_RND)
659 seq_printf(seq, "TXSIZE_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Luiz Capitulino222f1802006-03-20 22:16:13 -0800661 if (pkt_dev->flags & F_UDPSRC_RND)
662 seq_printf(seq, "UDPSRC_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Luiz Capitulino222f1802006-03-20 22:16:13 -0800664 if (pkt_dev->flags & F_UDPDST_RND)
665 seq_printf(seq, "UDPDST_RND ");
666
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800667 if (pkt_dev->flags & F_MPLS_RND)
668 seq_printf(seq, "MPLS_RND ");
669
Luiz Capitulino222f1802006-03-20 22:16:13 -0800670 if (pkt_dev->flags & F_MACSRC_RND)
671 seq_printf(seq, "MACSRC_RND ");
672
673 if (pkt_dev->flags & F_MACDST_RND)
674 seq_printf(seq, "MACDST_RND ");
675
676 seq_puts(seq, "\n");
677
678 sa = pkt_dev->started_at;
679 stopped = pkt_dev->stopped_at;
680 if (pkt_dev->running)
681 stopped = now; /* not really stopped, more like last-running-at */
682
683 seq_printf(seq,
684 "Current:\n pkts-sofar: %llu errors: %llu\n started: %lluus stopped: %lluus idle: %lluus\n",
685 (unsigned long long)pkt_dev->sofar,
686 (unsigned long long)pkt_dev->errors, (unsigned long long)sa,
687 (unsigned long long)stopped,
688 (unsigned long long)pkt_dev->idle_acc);
689
690 seq_printf(seq,
691 " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700692 pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset,
693 pkt_dev->cur_src_mac_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Luiz Capitulino222f1802006-03-20 22:16:13 -0800695 if (pkt_dev->flags & F_IPV6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 char b1[128], b2[128];
Luiz Capitulino222f1802006-03-20 22:16:13 -0800697 fmt_ip6(b1, pkt_dev->cur_in6_daddr.s6_addr);
698 fmt_ip6(b2, pkt_dev->cur_in6_saddr.s6_addr);
699 seq_printf(seq, " cur_saddr: %s cur_daddr: %s\n", b2, b1);
700 } else
701 seq_printf(seq, " cur_saddr: 0x%x cur_daddr: 0x%x\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700702 pkt_dev->cur_saddr, pkt_dev->cur_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Luiz Capitulino222f1802006-03-20 22:16:13 -0800704 seq_printf(seq, " cur_udp_dst: %d cur_udp_src: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700705 pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Luiz Capitulino222f1802006-03-20 22:16:13 -0800707 seq_printf(seq, " flows: %u\n", pkt_dev->nflows);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
709 if (pkt_dev->result[0])
Luiz Capitulino222f1802006-03-20 22:16:13 -0800710 seq_printf(seq, "Result: %s\n", pkt_dev->result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 else
Luiz Capitulino222f1802006-03-20 22:16:13 -0800712 seq_printf(seq, "Result: Idle\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700714 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715}
716
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800717
718static int hex32_arg(const char __user *user_buffer, __u32 *num)
719{
720 int i = 0;
721 *num = 0;
722
723 for(; i < 8; i++) {
724 char c;
725 *num <<= 4;
726 if (get_user(c, &user_buffer[i]))
727 return -EFAULT;
728 if ((c >= '0') && (c <= '9'))
729 *num |= c - '0';
730 else if ((c >= 'a') && (c <= 'f'))
731 *num |= c - 'a' + 10;
732 else if ((c >= 'A') && (c <= 'F'))
733 *num |= c - 'A' + 10;
734 else
735 break;
736 }
737 return i;
738}
739
Luiz Capitulino222f1802006-03-20 22:16:13 -0800740static int count_trail_chars(const char __user * user_buffer,
741 unsigned int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742{
743 int i;
744
745 for (i = 0; i < maxlen; i++) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800746 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 case '=':
756 break;
757 default:
758 goto done;
759 };
760 }
761done:
762 return i;
763}
764
Luiz Capitulino222f1802006-03-20 22:16:13 -0800765static unsigned long num_arg(const char __user * user_buffer,
766 unsigned long maxlen, unsigned long *num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767{
768 int i = 0;
769 *num = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800770
771 for (; i < maxlen; i++) {
772 char c;
773 if (get_user(c, &user_buffer[i]))
774 return -EFAULT;
775 if ((c >= '0') && (c <= '9')) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 *num *= 10;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800777 *num += c - '0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 } else
779 break;
780 }
781 return i;
782}
783
Luiz Capitulino222f1802006-03-20 22:16:13 -0800784static int strn_len(const char __user * user_buffer, unsigned int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785{
786 int i = 0;
787
Luiz Capitulino222f1802006-03-20 22:16:13 -0800788 for (; i < maxlen; i++) {
789 char c;
790 if (get_user(c, &user_buffer[i]))
791 return -EFAULT;
792 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 case '\"':
794 case '\n':
795 case '\r':
796 case '\t':
797 case ' ':
798 goto done_str;
799 break;
800 default:
801 break;
802 };
803 }
804done_str:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 return i;
806}
807
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800808static ssize_t get_labels(const char __user *buffer, struct pktgen_dev *pkt_dev)
809{
810 unsigned n = 0;
811 char c;
812 ssize_t i = 0;
813 int len;
814
815 pkt_dev->nr_labels = 0;
816 do {
817 __u32 tmp;
818 len = hex32_arg(&buffer[i], &tmp);
819 if (len <= 0)
820 return len;
821 pkt_dev->labels[n] = htonl(tmp);
822 if (pkt_dev->labels[n] & MPLS_STACK_BOTTOM)
823 pkt_dev->flags |= F_MPLS_RND;
824 i += len;
825 if (get_user(c, &buffer[i]))
826 return -EFAULT;
827 i++;
828 n++;
829 if (n >= MAX_MPLS_LABELS)
830 return -E2BIG;
831 } while(c == ',');
832
833 pkt_dev->nr_labels = n;
834 return i;
835}
836
Luiz Capitulino222f1802006-03-20 22:16:13 -0800837static ssize_t pktgen_if_write(struct file *file,
838 const char __user * user_buffer, size_t count,
839 loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800841 struct seq_file *seq = (struct seq_file *)file->private_data;
842 struct pktgen_dev *pkt_dev = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 int i = 0, max, len;
844 char name[16], valstr[32];
845 unsigned long value = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800846 char *pg_result = NULL;
847 int tmp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 char buf[128];
Luiz Capitulino222f1802006-03-20 22:16:13 -0800849
850 pg_result = &(pkt_dev->result[0]);
851
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 if (count < 1) {
853 printk("pktgen: wrong command format\n");
854 return -EINVAL;
855 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800856
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 max = count - i;
858 tmp = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800859 if (tmp < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 printk("pktgen: illegal format\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -0800861 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800863 i += tmp;
864
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 /* Read variable name */
866
867 len = strn_len(&user_buffer[i], sizeof(name) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800868 if (len < 0) {
869 return len;
870 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 memset(name, 0, sizeof(name));
Luiz Capitulino222f1802006-03-20 22:16:13 -0800872 if (copy_from_user(name, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 return -EFAULT;
874 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800875
876 max = count - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 len = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800878 if (len < 0)
879 return len;
880
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 i += len;
882
883 if (debug) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800884 char tb[count + 1];
885 if (copy_from_user(tb, user_buffer, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800887 tb[count] = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700888 printk("pktgen: %s,%lu buffer -:%s:-\n", name,
Luiz Capitulino222f1802006-03-20 22:16:13 -0800889 (unsigned long)count, tb);
890 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
892 if (!strcmp(name, "min_pkt_size")) {
893 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800894 if (len < 0) {
895 return len;
896 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800898 if (value < 14 + 20 + 8)
899 value = 14 + 20 + 8;
900 if (value != pkt_dev->min_pkt_size) {
901 pkt_dev->min_pkt_size = value;
902 pkt_dev->cur_pkt_size = value;
903 }
904 sprintf(pg_result, "OK: min_pkt_size=%u",
905 pkt_dev->min_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 return count;
907 }
908
Luiz Capitulino222f1802006-03-20 22:16:13 -0800909 if (!strcmp(name, "max_pkt_size")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800911 if (len < 0) {
912 return len;
913 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800915 if (value < 14 + 20 + 8)
916 value = 14 + 20 + 8;
917 if (value != pkt_dev->max_pkt_size) {
918 pkt_dev->max_pkt_size = value;
919 pkt_dev->cur_pkt_size = value;
920 }
921 sprintf(pg_result, "OK: max_pkt_size=%u",
922 pkt_dev->max_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 return count;
924 }
925
Luiz Capitulino222f1802006-03-20 22:16:13 -0800926 /* Shortcut for min = max */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
928 if (!strcmp(name, "pkt_size")) {
929 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800930 if (len < 0) {
931 return len;
932 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800934 if (value < 14 + 20 + 8)
935 value = 14 + 20 + 8;
936 if (value != pkt_dev->min_pkt_size) {
937 pkt_dev->min_pkt_size = value;
938 pkt_dev->max_pkt_size = value;
939 pkt_dev->cur_pkt_size = value;
940 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 sprintf(pg_result, "OK: pkt_size=%u", pkt_dev->min_pkt_size);
942 return count;
943 }
944
Luiz Capitulino222f1802006-03-20 22:16:13 -0800945 if (!strcmp(name, "debug")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800947 if (len < 0) {
948 return len;
949 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800951 debug = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 sprintf(pg_result, "OK: debug=%u", debug);
953 return count;
954 }
955
Luiz Capitulino222f1802006-03-20 22:16:13 -0800956 if (!strcmp(name, "frags")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800958 if (len < 0) {
959 return len;
960 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 i += len;
962 pkt_dev->nfrags = value;
963 sprintf(pg_result, "OK: frags=%u", pkt_dev->nfrags);
964 return count;
965 }
966 if (!strcmp(name, "delay")) {
967 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800968 if (len < 0) {
969 return len;
970 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 i += len;
972 if (value == 0x7FFFFFFF) {
973 pkt_dev->delay_us = 0x7FFFFFFF;
974 pkt_dev->delay_ns = 0;
975 } else {
976 pkt_dev->delay_us = value / 1000;
977 pkt_dev->delay_ns = value % 1000;
978 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800979 sprintf(pg_result, "OK: delay=%u",
980 1000 * pkt_dev->delay_us + pkt_dev->delay_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 return count;
982 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800983 if (!strcmp(name, "udp_src_min")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800985 if (len < 0) {
986 return len;
987 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800989 if (value != pkt_dev->udp_src_min) {
990 pkt_dev->udp_src_min = value;
991 pkt_dev->cur_udp_src = value;
992 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min);
994 return count;
995 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800996 if (!strcmp(name, "udp_dst_min")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800998 if (len < 0) {
999 return len;
1000 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001002 if (value != pkt_dev->udp_dst_min) {
1003 pkt_dev->udp_dst_min = value;
1004 pkt_dev->cur_udp_dst = value;
1005 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min);
1007 return count;
1008 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001009 if (!strcmp(name, "udp_src_max")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001011 if (len < 0) {
1012 return len;
1013 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001015 if (value != pkt_dev->udp_src_max) {
1016 pkt_dev->udp_src_max = value;
1017 pkt_dev->cur_udp_src = value;
1018 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max);
1020 return count;
1021 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001022 if (!strcmp(name, "udp_dst_max")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001024 if (len < 0) {
1025 return len;
1026 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001028 if (value != pkt_dev->udp_dst_max) {
1029 pkt_dev->udp_dst_max = value;
1030 pkt_dev->cur_udp_dst = value;
1031 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max);
1033 return count;
1034 }
1035 if (!strcmp(name, "clone_skb")) {
1036 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001037 if (len < 0) {
1038 return len;
1039 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001041 pkt_dev->clone_skb = value;
1042
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb);
1044 return count;
1045 }
1046 if (!strcmp(name, "count")) {
1047 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001048 if (len < 0) {
1049 return len;
1050 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 i += len;
1052 pkt_dev->count = value;
1053 sprintf(pg_result, "OK: count=%llu",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001054 (unsigned long long)pkt_dev->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 return count;
1056 }
1057 if (!strcmp(name, "src_mac_count")) {
1058 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001059 if (len < 0) {
1060 return len;
1061 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 i += len;
1063 if (pkt_dev->src_mac_count != value) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001064 pkt_dev->src_mac_count = value;
1065 pkt_dev->cur_src_mac_offset = 0;
1066 }
1067 sprintf(pg_result, "OK: src_mac_count=%d",
1068 pkt_dev->src_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 return count;
1070 }
1071 if (!strcmp(name, "dst_mac_count")) {
1072 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001073 if (len < 0) {
1074 return len;
1075 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 i += len;
1077 if (pkt_dev->dst_mac_count != value) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001078 pkt_dev->dst_mac_count = value;
1079 pkt_dev->cur_dst_mac_offset = 0;
1080 }
1081 sprintf(pg_result, "OK: dst_mac_count=%d",
1082 pkt_dev->dst_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 return count;
1084 }
1085 if (!strcmp(name, "flag")) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001086 char f[32];
1087 memset(f, 0, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 len = strn_len(&user_buffer[i], sizeof(f) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001089 if (len < 0) {
1090 return len;
1091 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 if (copy_from_user(f, &user_buffer[i], len))
1093 return -EFAULT;
1094 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001095 if (strcmp(f, "IPSRC_RND") == 0)
1096 pkt_dev->flags |= F_IPSRC_RND;
1097
1098 else if (strcmp(f, "!IPSRC_RND") == 0)
1099 pkt_dev->flags &= ~F_IPSRC_RND;
1100
1101 else if (strcmp(f, "TXSIZE_RND") == 0)
1102 pkt_dev->flags |= F_TXSIZE_RND;
1103
1104 else if (strcmp(f, "!TXSIZE_RND") == 0)
1105 pkt_dev->flags &= ~F_TXSIZE_RND;
1106
1107 else if (strcmp(f, "IPDST_RND") == 0)
1108 pkt_dev->flags |= F_IPDST_RND;
1109
1110 else if (strcmp(f, "!IPDST_RND") == 0)
1111 pkt_dev->flags &= ~F_IPDST_RND;
1112
1113 else if (strcmp(f, "UDPSRC_RND") == 0)
1114 pkt_dev->flags |= F_UDPSRC_RND;
1115
1116 else if (strcmp(f, "!UDPSRC_RND") == 0)
1117 pkt_dev->flags &= ~F_UDPSRC_RND;
1118
1119 else if (strcmp(f, "UDPDST_RND") == 0)
1120 pkt_dev->flags |= F_UDPDST_RND;
1121
1122 else if (strcmp(f, "!UDPDST_RND") == 0)
1123 pkt_dev->flags &= ~F_UDPDST_RND;
1124
1125 else if (strcmp(f, "MACSRC_RND") == 0)
1126 pkt_dev->flags |= F_MACSRC_RND;
1127
1128 else if (strcmp(f, "!MACSRC_RND") == 0)
1129 pkt_dev->flags &= ~F_MACSRC_RND;
1130
1131 else if (strcmp(f, "MACDST_RND") == 0)
1132 pkt_dev->flags |= F_MACDST_RND;
1133
1134 else if (strcmp(f, "!MACDST_RND") == 0)
1135 pkt_dev->flags &= ~F_MACDST_RND;
1136
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001137 else if (strcmp(f, "MPLS_RND") == 0)
1138 pkt_dev->flags |= F_MPLS_RND;
1139
1140 else if (strcmp(f, "!MPLS_RND") == 0)
1141 pkt_dev->flags &= ~F_MPLS_RND;
1142
Luiz Capitulino222f1802006-03-20 22:16:13 -08001143 else {
1144 sprintf(pg_result,
1145 "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
1146 f,
1147 "IPSRC_RND, IPDST_RND, TXSIZE_RND, UDPSRC_RND, UDPDST_RND, MACSRC_RND, MACDST_RND\n");
1148 return count;
1149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
1151 return count;
1152 }
1153 if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
1154 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001155 if (len < 0) {
1156 return len;
1157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
Luiz Capitulino222f1802006-03-20 22:16:13 -08001159 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001161 buf[len] = 0;
1162 if (strcmp(buf, pkt_dev->dst_min) != 0) {
1163 memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
1164 strncpy(pkt_dev->dst_min, buf, len);
1165 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
1166 pkt_dev->cur_daddr = pkt_dev->daddr_min;
1167 }
1168 if (debug)
1169 printk("pktgen: dst_min set to: %s\n",
1170 pkt_dev->dst_min);
1171 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min);
1173 return count;
1174 }
1175 if (!strcmp(name, "dst_max")) {
1176 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001177 if (len < 0) {
1178 return len;
1179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
Luiz Capitulino222f1802006-03-20 22:16:13 -08001181 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 return -EFAULT;
1183
Luiz Capitulino222f1802006-03-20 22:16:13 -08001184 buf[len] = 0;
1185 if (strcmp(buf, pkt_dev->dst_max) != 0) {
1186 memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
1187 strncpy(pkt_dev->dst_max, buf, len);
1188 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
1189 pkt_dev->cur_daddr = pkt_dev->daddr_max;
1190 }
1191 if (debug)
1192 printk("pktgen: dst_max set to: %s\n",
1193 pkt_dev->dst_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 i += len;
1195 sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max);
1196 return count;
1197 }
1198 if (!strcmp(name, "dst6")) {
1199 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001200 if (len < 0)
1201 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
1203 pkt_dev->flags |= F_IPV6;
1204
Luiz Capitulino222f1802006-03-20 22:16:13 -08001205 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001207 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
1209 scan_ip6(buf, pkt_dev->in6_daddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001210 fmt_ip6(buf, pkt_dev->in6_daddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
1212 ipv6_addr_copy(&pkt_dev->cur_in6_daddr, &pkt_dev->in6_daddr);
1213
Luiz Capitulino222f1802006-03-20 22:16:13 -08001214 if (debug)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 printk("pktgen: dst6 set to: %s\n", buf);
1216
Luiz Capitulino222f1802006-03-20 22:16:13 -08001217 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 sprintf(pg_result, "OK: dst6=%s", buf);
1219 return count;
1220 }
1221 if (!strcmp(name, "dst6_min")) {
1222 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001223 if (len < 0)
1224 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
1226 pkt_dev->flags |= F_IPV6;
1227
Luiz Capitulino222f1802006-03-20 22:16:13 -08001228 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001230 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
1232 scan_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001233 fmt_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
Luiz Capitulino222f1802006-03-20 22:16:13 -08001235 ipv6_addr_copy(&pkt_dev->cur_in6_daddr,
1236 &pkt_dev->min_in6_daddr);
1237 if (debug)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 printk("pktgen: dst6_min set to: %s\n", buf);
1239
Luiz Capitulino222f1802006-03-20 22:16:13 -08001240 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 sprintf(pg_result, "OK: dst6_min=%s", buf);
1242 return count;
1243 }
1244 if (!strcmp(name, "dst6_max")) {
1245 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001246 if (len < 0)
1247 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
1249 pkt_dev->flags |= F_IPV6;
1250
Luiz Capitulino222f1802006-03-20 22:16:13 -08001251 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001253 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
1255 scan_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001256 fmt_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Luiz Capitulino222f1802006-03-20 22:16:13 -08001258 if (debug)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 printk("pktgen: dst6_max set to: %s\n", buf);
1260
Luiz Capitulino222f1802006-03-20 22:16:13 -08001261 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 sprintf(pg_result, "OK: dst6_max=%s", buf);
1263 return count;
1264 }
1265 if (!strcmp(name, "src6")) {
1266 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001267 if (len < 0)
1268 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
1270 pkt_dev->flags |= F_IPV6;
1271
Luiz Capitulino222f1802006-03-20 22:16:13 -08001272 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001274 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
1276 scan_ip6(buf, pkt_dev->in6_saddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001277 fmt_ip6(buf, pkt_dev->in6_saddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
1279 ipv6_addr_copy(&pkt_dev->cur_in6_saddr, &pkt_dev->in6_saddr);
1280
Luiz Capitulino222f1802006-03-20 22:16:13 -08001281 if (debug)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 printk("pktgen: src6 set to: %s\n", buf);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001283
1284 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 sprintf(pg_result, "OK: src6=%s", buf);
1286 return count;
1287 }
1288 if (!strcmp(name, "src_min")) {
1289 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001290 if (len < 0) {
1291 return len;
1292 }
1293 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001295 buf[len] = 0;
1296 if (strcmp(buf, pkt_dev->src_min) != 0) {
1297 memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
1298 strncpy(pkt_dev->src_min, buf, len);
1299 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
1300 pkt_dev->cur_saddr = pkt_dev->saddr_min;
1301 }
1302 if (debug)
1303 printk("pktgen: src_min set to: %s\n",
1304 pkt_dev->src_min);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 i += len;
1306 sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min);
1307 return count;
1308 }
1309 if (!strcmp(name, "src_max")) {
1310 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001311 if (len < 0) {
1312 return len;
1313 }
1314 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001316 buf[len] = 0;
1317 if (strcmp(buf, pkt_dev->src_max) != 0) {
1318 memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
1319 strncpy(pkt_dev->src_max, buf, len);
1320 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
1321 pkt_dev->cur_saddr = pkt_dev->saddr_max;
1322 }
1323 if (debug)
1324 printk("pktgen: src_max set to: %s\n",
1325 pkt_dev->src_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 i += len;
1327 sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max);
1328 return count;
1329 }
1330 if (!strcmp(name, "dst_mac")) {
1331 char *v = valstr;
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001332 unsigned char old_dmac[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 unsigned char *m = pkt_dev->dst_mac;
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001334 memcpy(old_dmac, pkt_dev->dst_mac, ETH_ALEN);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001335
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001337 if (len < 0) {
1338 return len;
1339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 memset(valstr, 0, sizeof(valstr));
Luiz Capitulino222f1802006-03-20 22:16:13 -08001341 if (copy_from_user(valstr, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 return -EFAULT;
1343 i += len;
1344
Luiz Capitulino222f1802006-03-20 22:16:13 -08001345 for (*m = 0; *v && m < pkt_dev->dst_mac + 6; v++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 if (*v >= '0' && *v <= '9') {
1347 *m *= 16;
1348 *m += *v - '0';
1349 }
1350 if (*v >= 'A' && *v <= 'F') {
1351 *m *= 16;
1352 *m += *v - 'A' + 10;
1353 }
1354 if (*v >= 'a' && *v <= 'f') {
1355 *m *= 16;
1356 *m += *v - 'a' + 10;
1357 }
1358 if (*v == ':') {
1359 m++;
1360 *m = 0;
1361 }
1362 }
1363
1364 /* Set up Dest MAC */
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001365 if (compare_ether_addr(old_dmac, pkt_dev->dst_mac))
1366 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001367
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 sprintf(pg_result, "OK: dstmac");
1369 return count;
1370 }
1371 if (!strcmp(name, "src_mac")) {
1372 char *v = valstr;
1373 unsigned char *m = pkt_dev->src_mac;
1374
1375 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001376 if (len < 0) {
1377 return len;
1378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 memset(valstr, 0, sizeof(valstr));
Luiz Capitulino222f1802006-03-20 22:16:13 -08001380 if (copy_from_user(valstr, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 return -EFAULT;
1382 i += len;
1383
Luiz Capitulino222f1802006-03-20 22:16:13 -08001384 for (*m = 0; *v && m < pkt_dev->src_mac + 6; v++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 if (*v >= '0' && *v <= '9') {
1386 *m *= 16;
1387 *m += *v - '0';
1388 }
1389 if (*v >= 'A' && *v <= 'F') {
1390 *m *= 16;
1391 *m += *v - 'A' + 10;
1392 }
1393 if (*v >= 'a' && *v <= 'f') {
1394 *m *= 16;
1395 *m += *v - 'a' + 10;
1396 }
1397 if (*v == ':') {
1398 m++;
1399 *m = 0;
1400 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001401 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
Luiz Capitulino222f1802006-03-20 22:16:13 -08001403 sprintf(pg_result, "OK: srcmac");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 return count;
1405 }
1406
Luiz Capitulino222f1802006-03-20 22:16:13 -08001407 if (!strcmp(name, "clear_counters")) {
1408 pktgen_clear_counters(pkt_dev);
1409 sprintf(pg_result, "OK: Clearing counters.\n");
1410 return count;
1411 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
1413 if (!strcmp(name, "flows")) {
1414 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001415 if (len < 0) {
1416 return len;
1417 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 i += len;
1419 if (value > MAX_CFLOWS)
1420 value = MAX_CFLOWS;
1421
1422 pkt_dev->cflows = value;
1423 sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
1424 return count;
1425 }
1426
1427 if (!strcmp(name, "flowlen")) {
1428 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001429 if (len < 0) {
1430 return len;
1431 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 i += len;
1433 pkt_dev->lflow = value;
1434 sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
1435 return count;
1436 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001437
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001438 if (!strcmp(name, "mpls")) {
1439 unsigned n, offset;
1440 len = get_labels(&user_buffer[i], pkt_dev);
1441 if (len < 0) { return len; }
1442 i += len;
1443 offset = sprintf(pg_result, "OK: mpls=");
1444 for(n = 0; n < pkt_dev->nr_labels; n++)
1445 offset += sprintf(pg_result + offset,
1446 "%08x%s", ntohl(pkt_dev->labels[n]),
1447 n == pkt_dev->nr_labels-1 ? "" : ",");
1448 return count;
1449 }
1450
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
1452 return -EINVAL;
1453}
1454
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001455static int pktgen_if_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456{
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001457 return single_open(file, pktgen_if_show, PDE(inode)->data);
1458}
1459
1460static struct file_operations pktgen_if_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001461 .owner = THIS_MODULE,
1462 .open = pktgen_if_open,
1463 .read = seq_read,
1464 .llseek = seq_lseek,
1465 .write = pktgen_if_write,
1466 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001467};
1468
1469static int pktgen_thread_show(struct seq_file *seq, void *v)
1470{
Luiz Capitulino222f1802006-03-20 22:16:13 -08001471 struct pktgen_thread *t = seq->private;
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08001472 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001474 BUG_ON(!t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001476 seq_printf(seq, "Name: %s max_before_softirq: %d\n",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001477 t->name, t->max_before_softirq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
Luiz Capitulino222f1802006-03-20 22:16:13 -08001479 seq_printf(seq, "Running: ");
1480
1481 if_lock(t);
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08001482 list_for_each_entry(pkt_dev, &t->if_list, list)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001483 if (pkt_dev->running)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001484 seq_printf(seq, "%s ", pkt_dev->ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
Luiz Capitulino222f1802006-03-20 22:16:13 -08001486 seq_printf(seq, "\nStopped: ");
1487
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08001488 list_for_each_entry(pkt_dev, &t->if_list, list)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001489 if (!pkt_dev->running)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001490 seq_printf(seq, "%s ", pkt_dev->ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
1492 if (t->result[0])
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001493 seq_printf(seq, "\nResult: %s\n", t->result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 else
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001495 seq_printf(seq, "\nResult: NA\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
Luiz Capitulino222f1802006-03-20 22:16:13 -08001497 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001499 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500}
1501
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001502static ssize_t pktgen_thread_write(struct file *file,
Luiz Capitulino222f1802006-03-20 22:16:13 -08001503 const char __user * user_buffer,
1504 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505{
Luiz Capitulino222f1802006-03-20 22:16:13 -08001506 struct seq_file *seq = (struct seq_file *)file->private_data;
1507 struct pktgen_thread *t = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 int i = 0, max, len, ret;
1509 char name[40];
Luiz Capitulino222f1802006-03-20 22:16:13 -08001510 char *pg_result;
1511 unsigned long value = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001512
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 if (count < 1) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001514 // sprintf(pg_result, "Wrong command format");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 return -EINVAL;
1516 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001517
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 max = count - i;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001519 len = count_trail_chars(&user_buffer[i], max);
1520 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001521 return len;
1522
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 i += len;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001524
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 /* Read variable name */
1526
1527 len = strn_len(&user_buffer[i], sizeof(name) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001528 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001529 return len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001530
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 memset(name, 0, sizeof(name));
1532 if (copy_from_user(name, &user_buffer[i], len))
1533 return -EFAULT;
1534 i += len;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001535
Luiz Capitulino222f1802006-03-20 22:16:13 -08001536 max = count - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 len = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001538 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001539 return len;
1540
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 i += len;
1542
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001543 if (debug)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001544 printk("pktgen: t=%s, count=%lu\n", name, (unsigned long)count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
Luiz Capitulino222f1802006-03-20 22:16:13 -08001546 if (!t) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 printk("pktgen: ERROR: No thread\n");
1548 ret = -EINVAL;
1549 goto out;
1550 }
1551
1552 pg_result = &(t->result[0]);
1553
Luiz Capitulino222f1802006-03-20 22:16:13 -08001554 if (!strcmp(name, "add_device")) {
1555 char f[32];
1556 memset(f, 0, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 len = strn_len(&user_buffer[i], sizeof(f) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001558 if (len < 0) {
1559 ret = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 goto out;
1561 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001562 if (copy_from_user(f, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 return -EFAULT;
1564 i += len;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001565 mutex_lock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001566 pktgen_add_device(t, f);
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001567 mutex_unlock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001568 ret = count;
1569 sprintf(pg_result, "OK: add_device=%s", f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 goto out;
1571 }
1572
Luiz Capitulino222f1802006-03-20 22:16:13 -08001573 if (!strcmp(name, "rem_device_all")) {
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001574 mutex_lock(&pktgen_thread_lock);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001575 t->control |= T_REMDEVALL;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001576 mutex_unlock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001577 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 ret = count;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001579 sprintf(pg_result, "OK: rem_device_all");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 goto out;
1581 }
1582
Luiz Capitulino222f1802006-03-20 22:16:13 -08001583 if (!strcmp(name, "max_before_softirq")) {
1584 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001585 mutex_lock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001586 t->max_before_softirq = value;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001587 mutex_unlock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001588 ret = count;
1589 sprintf(pg_result, "OK: max_before_softirq=%lu", value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 goto out;
1591 }
1592
1593 ret = -EINVAL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001594out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 return ret;
1596}
1597
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001598static int pktgen_thread_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599{
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001600 return single_open(file, pktgen_thread_show, PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601}
1602
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001603static struct file_operations pktgen_thread_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001604 .owner = THIS_MODULE,
1605 .open = pktgen_thread_open,
1606 .read = seq_read,
1607 .llseek = seq_lseek,
1608 .write = pktgen_thread_write,
1609 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001610};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611
1612/* Think find or remove for NN */
Luiz Capitulino222f1802006-03-20 22:16:13 -08001613static struct pktgen_dev *__pktgen_NN_threads(const char *ifname, int remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614{
1615 struct pktgen_thread *t;
1616 struct pktgen_dev *pkt_dev = NULL;
1617
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08001618 list_for_each_entry(t, &pktgen_threads, th_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 pkt_dev = pktgen_find_dev(t, ifname);
1620 if (pkt_dev) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001621 if (remove) {
1622 if_lock(t);
1623 pkt_dev->removal_mark = 1;
1624 t->control |= T_REMDEV;
1625 if_unlock(t);
1626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 break;
1628 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001630 return pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631}
1632
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001633/*
1634 * mark a device for removal
1635 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08001636static int pktgen_mark_device(const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637{
1638 struct pktgen_dev *pkt_dev = NULL;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001639 const int max_tries = 10, msec_per_try = 125;
1640 int i = 0;
1641 int ret = 0;
1642
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001643 mutex_lock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001644 PG_DEBUG(printk("pktgen: pktgen_mark_device marking %s for removal\n",
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001645 ifname));
1646
Luiz Capitulino222f1802006-03-20 22:16:13 -08001647 while (1) {
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001648
1649 pkt_dev = __pktgen_NN_threads(ifname, REMOVE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001650 if (pkt_dev == NULL)
1651 break; /* success */
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001652
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001653 mutex_unlock(&pktgen_thread_lock);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001654 PG_DEBUG(printk("pktgen: pktgen_mark_device waiting for %s "
Luiz Capitulino222f1802006-03-20 22:16:13 -08001655 "to disappear....\n", ifname));
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001656 schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001657 mutex_lock(&pktgen_thread_lock);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001658
1659 if (++i >= max_tries) {
1660 printk("pktgen_mark_device: timed out after waiting "
Luiz Capitulino222f1802006-03-20 22:16:13 -08001661 "%d msec for device %s to be removed\n",
1662 msec_per_try * i, ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001663 ret = 1;
1664 break;
1665 }
1666
1667 }
1668
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001669 mutex_unlock(&pktgen_thread_lock);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001670
1671 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672}
1673
Luiz Capitulino222f1802006-03-20 22:16:13 -08001674static int pktgen_device_event(struct notifier_block *unused,
1675 unsigned long event, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676{
1677 struct net_device *dev = (struct net_device *)(ptr);
1678
1679 /* It is OK that we do not hold the group lock right now,
1680 * as we run under the RTNL lock.
1681 */
1682
1683 switch (event) {
1684 case NETDEV_CHANGEADDR:
1685 case NETDEV_GOING_DOWN:
1686 case NETDEV_DOWN:
1687 case NETDEV_UP:
1688 /* Ignore for now */
1689 break;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001690
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 case NETDEV_UNREGISTER:
Luiz Capitulino222f1802006-03-20 22:16:13 -08001692 pktgen_mark_device(dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 break;
1694 };
1695
1696 return NOTIFY_DONE;
1697}
1698
1699/* Associate pktgen_dev with a device. */
1700
Luiz Capitulino222f1802006-03-20 22:16:13 -08001701static struct net_device *pktgen_setup_dev(struct pktgen_dev *pkt_dev)
1702{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 struct net_device *odev;
1704
1705 /* Clean old setups */
1706
1707 if (pkt_dev->odev) {
1708 dev_put(pkt_dev->odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001709 pkt_dev->odev = NULL;
1710 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
1712 odev = dev_get_by_name(pkt_dev->ifname);
1713
1714 if (!odev) {
1715 printk("pktgen: no such netdevice: \"%s\"\n", pkt_dev->ifname);
1716 goto out;
1717 }
1718 if (odev->type != ARPHRD_ETHER) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001719 printk("pktgen: not an ethernet device: \"%s\"\n",
1720 pkt_dev->ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 goto out_put;
1722 }
1723 if (!netif_running(odev)) {
1724 printk("pktgen: device is down: \"%s\"\n", pkt_dev->ifname);
1725 goto out_put;
1726 }
1727 pkt_dev->odev = odev;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001728
1729 return pkt_dev->odev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
1731out_put:
1732 dev_put(odev);
1733out:
Luiz Capitulino222f1802006-03-20 22:16:13 -08001734 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
1736}
1737
1738/* Read pkt_dev from the interface and set up internal pktgen_dev
1739 * structure to have the right information to create/send packets
1740 */
1741static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
1742{
1743 /* Try once more, just in case it works now. */
Luiz Capitulino222f1802006-03-20 22:16:13 -08001744 if (!pkt_dev->odev)
1745 pktgen_setup_dev(pkt_dev);
1746
1747 if (!pkt_dev->odev) {
1748 printk("pktgen: ERROR: pkt_dev->odev == NULL in setup_inject.\n");
1749 sprintf(pkt_dev->result,
1750 "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
1751 return;
1752 }
1753
1754 /* Default to the interface's mac if not explicitly set. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001756 if (is_zero_ether_addr(pkt_dev->src_mac))
Luiz Capitulino222f1802006-03-20 22:16:13 -08001757 memcpy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758
Luiz Capitulino222f1802006-03-20 22:16:13 -08001759 /* Set up Dest MAC */
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001760 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
Luiz Capitulino222f1802006-03-20 22:16:13 -08001762 /* Set up pkt size */
1763 pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
1764
1765 if (pkt_dev->flags & F_IPV6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 /*
1767 * Skip this automatic address setting until locks or functions
1768 * gets exported
1769 */
1770
1771#ifdef NOTNOW
Luiz Capitulino222f1802006-03-20 22:16:13 -08001772 int i, set = 0, err = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 struct inet6_dev *idev;
1774
Luiz Capitulino222f1802006-03-20 22:16:13 -08001775 for (i = 0; i < IN6_ADDR_HSIZE; i++)
1776 if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 set = 1;
1778 break;
1779 }
1780
Luiz Capitulino222f1802006-03-20 22:16:13 -08001781 if (!set) {
1782
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 /*
1784 * Use linklevel address if unconfigured.
1785 *
1786 * use ipv6_get_lladdr if/when it's get exported
1787 */
1788
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001789 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 if ((idev = __in6_dev_get(pkt_dev->odev)) != NULL) {
1791 struct inet6_ifaddr *ifp;
1792
1793 read_lock_bh(&idev->lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001794 for (ifp = idev->addr_list; ifp;
1795 ifp = ifp->if_next) {
1796 if (ifp->scope == IFA_LINK
1797 && !(ifp->
1798 flags & IFA_F_TENTATIVE)) {
1799 ipv6_addr_copy(&pkt_dev->
1800 cur_in6_saddr,
1801 &ifp->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 err = 0;
1803 break;
1804 }
1805 }
1806 read_unlock_bh(&idev->lock);
1807 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001808 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08001809 if (err)
1810 printk("pktgen: ERROR: IPv6 link address not availble.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 }
1812#endif
Luiz Capitulino222f1802006-03-20 22:16:13 -08001813 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 pkt_dev->saddr_min = 0;
1815 pkt_dev->saddr_max = 0;
1816 if (strlen(pkt_dev->src_min) == 0) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001817
1818 struct in_device *in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819
1820 rcu_read_lock();
Herbert Xue5ed6392005-10-03 14:35:55 -07001821 in_dev = __in_dev_get_rcu(pkt_dev->odev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 if (in_dev) {
1823 if (in_dev->ifa_list) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001824 pkt_dev->saddr_min =
1825 in_dev->ifa_list->ifa_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 pkt_dev->saddr_max = pkt_dev->saddr_min;
1827 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 }
1829 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08001830 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
1832 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
1833 }
1834
1835 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
1836 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
1837 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001838 /* Initialize current values. */
1839 pkt_dev->cur_dst_mac_offset = 0;
1840 pkt_dev->cur_src_mac_offset = 0;
1841 pkt_dev->cur_saddr = pkt_dev->saddr_min;
1842 pkt_dev->cur_daddr = pkt_dev->daddr_min;
1843 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
1844 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 pkt_dev->nflows = 0;
1846}
1847
1848static void spin(struct pktgen_dev *pkt_dev, __u64 spin_until_us)
1849{
1850 __u64 start;
1851 __u64 now;
1852
1853 start = now = getCurUs();
1854 printk(KERN_INFO "sleeping for %d\n", (int)(spin_until_us - now));
1855 while (now < spin_until_us) {
Stephen Hemmingerb4099fa2005-10-14 15:32:22 -07001856 /* TODO: optimize sleeping behavior */
Luiz Capitulino222f1802006-03-20 22:16:13 -08001857 if (spin_until_us - now > jiffies_to_usecs(1) + 1)
Nishanth Aravamudan121caf52005-09-12 14:15:34 -07001858 schedule_timeout_interruptible(1);
1859 else if (spin_until_us - now > 100) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 do_softirq();
1861 if (!pkt_dev->running)
1862 return;
1863 if (need_resched())
1864 schedule();
1865 }
1866
1867 now = getCurUs();
1868 }
1869
1870 pkt_dev->idle_acc += now - start;
1871}
1872
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873/* Increment/randomize headers according to flags and current values
1874 * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
1875 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08001876static void mod_cur_headers(struct pktgen_dev *pkt_dev)
1877{
1878 __u32 imn;
1879 __u32 imx;
1880 int flow = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
Luiz Capitulino222f1802006-03-20 22:16:13 -08001882 if (pkt_dev->cflows) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 flow = pktgen_random() % pkt_dev->cflows;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001884
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 if (pkt_dev->flows[flow].count > pkt_dev->lflow)
1886 pkt_dev->flows[flow].count = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001887 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888
1889 /* Deal with source MAC */
Luiz Capitulino222f1802006-03-20 22:16:13 -08001890 if (pkt_dev->src_mac_count > 1) {
1891 __u32 mc;
1892 __u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893
Luiz Capitulino222f1802006-03-20 22:16:13 -08001894 if (pkt_dev->flags & F_MACSRC_RND)
1895 mc = pktgen_random() % (pkt_dev->src_mac_count);
1896 else {
1897 mc = pkt_dev->cur_src_mac_offset++;
1898 if (pkt_dev->cur_src_mac_offset >
1899 pkt_dev->src_mac_count)
1900 pkt_dev->cur_src_mac_offset = 0;
1901 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
Luiz Capitulino222f1802006-03-20 22:16:13 -08001903 tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
1904 pkt_dev->hh[11] = tmp;
1905 tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
1906 pkt_dev->hh[10] = tmp;
1907 tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
1908 pkt_dev->hh[9] = tmp;
1909 tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
1910 pkt_dev->hh[8] = tmp;
1911 tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
1912 pkt_dev->hh[7] = tmp;
1913 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914
Luiz Capitulino222f1802006-03-20 22:16:13 -08001915 /* Deal with Destination MAC */
1916 if (pkt_dev->dst_mac_count > 1) {
1917 __u32 mc;
1918 __u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
Luiz Capitulino222f1802006-03-20 22:16:13 -08001920 if (pkt_dev->flags & F_MACDST_RND)
1921 mc = pktgen_random() % (pkt_dev->dst_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
Luiz Capitulino222f1802006-03-20 22:16:13 -08001923 else {
1924 mc = pkt_dev->cur_dst_mac_offset++;
1925 if (pkt_dev->cur_dst_mac_offset >
1926 pkt_dev->dst_mac_count) {
1927 pkt_dev->cur_dst_mac_offset = 0;
1928 }
1929 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930
Luiz Capitulino222f1802006-03-20 22:16:13 -08001931 tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
1932 pkt_dev->hh[5] = tmp;
1933 tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
1934 pkt_dev->hh[4] = tmp;
1935 tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
1936 pkt_dev->hh[3] = tmp;
1937 tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
1938 pkt_dev->hh[2] = tmp;
1939 tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
1940 pkt_dev->hh[1] = tmp;
1941 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001943 if (pkt_dev->flags & F_MPLS_RND) {
1944 unsigned i;
1945 for(i = 0; i < pkt_dev->nr_labels; i++)
1946 if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
1947 pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
1948 (pktgen_random() &
1949 htonl(0x000fffff));
1950 }
1951
Luiz Capitulino222f1802006-03-20 22:16:13 -08001952 if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
1953 if (pkt_dev->flags & F_UDPSRC_RND)
1954 pkt_dev->cur_udp_src =
1955 ((pktgen_random() %
1956 (pkt_dev->udp_src_max - pkt_dev->udp_src_min)) +
1957 pkt_dev->udp_src_min);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958
Luiz Capitulino222f1802006-03-20 22:16:13 -08001959 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 pkt_dev->cur_udp_src++;
1961 if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
1962 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001963 }
1964 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965
Luiz Capitulino222f1802006-03-20 22:16:13 -08001966 if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
1967 if (pkt_dev->flags & F_UDPDST_RND) {
1968 pkt_dev->cur_udp_dst =
1969 ((pktgen_random() %
1970 (pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)) +
1971 pkt_dev->udp_dst_min);
1972 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 pkt_dev->cur_udp_dst++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001974 if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001976 }
1977 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
1979 if (!(pkt_dev->flags & F_IPV6)) {
1980
Luiz Capitulino222f1802006-03-20 22:16:13 -08001981 if ((imn = ntohl(pkt_dev->saddr_min)) < (imx =
1982 ntohl(pkt_dev->
1983 saddr_max))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 __u32 t;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001985 if (pkt_dev->flags & F_IPSRC_RND)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 t = ((pktgen_random() % (imx - imn)) + imn);
1987 else {
1988 t = ntohl(pkt_dev->cur_saddr);
1989 t++;
1990 if (t > imx) {
1991 t = imn;
1992 }
1993 }
1994 pkt_dev->cur_saddr = htonl(t);
1995 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001996
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 if (pkt_dev->cflows && pkt_dev->flows[flow].count != 0) {
1998 pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
1999 } else {
2000
Luiz Capitulino222f1802006-03-20 22:16:13 -08002001 if ((imn = ntohl(pkt_dev->daddr_min)) < (imx =
2002 ntohl(pkt_dev->
2003 daddr_max)))
2004 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 __u32 t;
2006 if (pkt_dev->flags & F_IPDST_RND) {
2007
Luiz Capitulino222f1802006-03-20 22:16:13 -08002008 t = ((pktgen_random() % (imx - imn)) +
2009 imn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 t = htonl(t);
2011
Luiz Capitulino222f1802006-03-20 22:16:13 -08002012 while (LOOPBACK(t) || MULTICAST(t)
2013 || BADCLASS(t) || ZERONET(t)
2014 || LOCAL_MCAST(t)) {
2015 t = ((pktgen_random() %
2016 (imx - imn)) + imn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 t = htonl(t);
2018 }
2019 pkt_dev->cur_daddr = t;
2020 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002021
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 else {
2023 t = ntohl(pkt_dev->cur_daddr);
2024 t++;
2025 if (t > imx) {
2026 t = imn;
2027 }
2028 pkt_dev->cur_daddr = htonl(t);
2029 }
2030 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002031 if (pkt_dev->cflows) {
2032 pkt_dev->flows[flow].cur_daddr =
2033 pkt_dev->cur_daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 pkt_dev->nflows++;
2035 }
2036 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002037 } else { /* IPV6 * */
2038
2039 if (pkt_dev->min_in6_daddr.s6_addr32[0] == 0 &&
2040 pkt_dev->min_in6_daddr.s6_addr32[1] == 0 &&
2041 pkt_dev->min_in6_daddr.s6_addr32[2] == 0 &&
2042 pkt_dev->min_in6_daddr.s6_addr32[3] == 0) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 else {
2044 int i;
2045
2046 /* Only random destinations yet */
2047
Luiz Capitulino222f1802006-03-20 22:16:13 -08002048 for (i = 0; i < 4; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 pkt_dev->cur_in6_daddr.s6_addr32[i] =
Luiz Capitulino222f1802006-03-20 22:16:13 -08002050 ((pktgen_random() |
2051 pkt_dev->min_in6_daddr.s6_addr32[i]) &
2052 pkt_dev->max_in6_daddr.s6_addr32[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002054 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 }
2056
Luiz Capitulino222f1802006-03-20 22:16:13 -08002057 if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
2058 __u32 t;
2059 if (pkt_dev->flags & F_TXSIZE_RND) {
2060 t = ((pktgen_random() %
2061 (pkt_dev->max_pkt_size - pkt_dev->min_pkt_size))
2062 + pkt_dev->min_pkt_size);
2063 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 t = pkt_dev->cur_pkt_size + 1;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002065 if (t > pkt_dev->max_pkt_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 t = pkt_dev->min_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002067 }
2068 pkt_dev->cur_pkt_size = t;
2069 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070
2071 pkt_dev->flows[flow].count++;
2072}
2073
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002074static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
2075{
2076 unsigned i;
2077 for(i = 0; i < pkt_dev->nr_labels; i++) {
2078 *mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
2079 }
2080 mpls--;
2081 *mpls |= MPLS_STACK_BOTTOM;
2082}
2083
Luiz Capitulino222f1802006-03-20 22:16:13 -08002084static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
2085 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086{
2087 struct sk_buff *skb = NULL;
2088 __u8 *eth;
2089 struct udphdr *udph;
2090 int datalen, iplen;
2091 struct iphdr *iph;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002092 struct pktgen_hdr *pgh = NULL;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002093 __be16 protocol = __constant_htons(ETH_P_IP);
2094 __be32 *mpls;
2095
2096 if (pkt_dev->nr_labels)
2097 protocol = __constant_htons(ETH_P_MPLS_UC);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002098
Robert Olsson64053be2005-06-26 15:27:10 -07002099 /* Update any of the values, used when we're incrementing various
2100 * fields.
2101 */
2102 mod_cur_headers(pkt_dev);
2103
David S. Miller7ac54592006-01-18 14:19:10 -08002104 datalen = (odev->hard_header_len + 16) & ~0xf;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002105 skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + datalen +
2106 pkt_dev->nr_labels*sizeof(u32), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 if (!skb) {
2108 sprintf(pkt_dev->result, "No memory");
2109 return NULL;
2110 }
2111
David S. Miller7ac54592006-01-18 14:19:10 -08002112 skb_reserve(skb, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113
2114 /* Reserve for ethernet and IP header */
2115 eth = (__u8 *) skb_push(skb, 14);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002116 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2117 if (pkt_dev->nr_labels)
2118 mpls_push(mpls, pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 iph = (struct iphdr *)skb_put(skb, sizeof(struct iphdr));
2120 udph = (struct udphdr *)skb_put(skb, sizeof(struct udphdr));
2121
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 memcpy(eth, pkt_dev->hh, 12);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002123 *(u16 *) & eth[12] = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002125 /* Eth + IPh + UDPh + mpls */
2126 datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
2127 pkt_dev->nr_labels*sizeof(u32);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002128 if (datalen < sizeof(struct pktgen_hdr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 datalen = sizeof(struct pktgen_hdr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002130
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 udph->source = htons(pkt_dev->cur_udp_src);
2132 udph->dest = htons(pkt_dev->cur_udp_dst);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002133 udph->len = htons(datalen + 8); /* DATA + udphdr */
2134 udph->check = 0; /* No checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135
2136 iph->ihl = 5;
2137 iph->version = 4;
2138 iph->ttl = 32;
2139 iph->tos = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002140 iph->protocol = IPPROTO_UDP; /* UDP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 iph->saddr = pkt_dev->cur_saddr;
2142 iph->daddr = pkt_dev->cur_daddr;
2143 iph->frag_off = 0;
2144 iplen = 20 + 8 + datalen;
2145 iph->tot_len = htons(iplen);
2146 iph->check = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002147 iph->check = ip_fast_csum((void *)iph, iph->ihl);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002148 skb->protocol = protocol;
2149 skb->mac.raw = ((u8 *) iph) - 14 - pkt_dev->nr_labels*sizeof(u32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 skb->dev = odev;
2151 skb->pkt_type = PACKET_HOST;
Chen-Li Tienaaf58062006-08-07 20:49:07 -07002152 skb->nh.iph = iph;
2153 skb->h.uh = udph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154
Luiz Capitulino222f1802006-03-20 22:16:13 -08002155 if (pkt_dev->nfrags <= 0)
2156 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 else {
2158 int frags = pkt_dev->nfrags;
2159 int i;
2160
Luiz Capitulino222f1802006-03-20 22:16:13 -08002161 pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
2162
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 if (frags > MAX_SKB_FRAGS)
2164 frags = MAX_SKB_FRAGS;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002165 if (datalen > frags * PAGE_SIZE) {
2166 skb_put(skb, datalen - frags * PAGE_SIZE);
2167 datalen = frags * PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 }
2169
2170 i = 0;
2171 while (datalen > 0) {
2172 struct page *page = alloc_pages(GFP_KERNEL, 0);
2173 skb_shinfo(skb)->frags[i].page = page;
2174 skb_shinfo(skb)->frags[i].page_offset = 0;
2175 skb_shinfo(skb)->frags[i].size =
Luiz Capitulino222f1802006-03-20 22:16:13 -08002176 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 datalen -= skb_shinfo(skb)->frags[i].size;
2178 skb->len += skb_shinfo(skb)->frags[i].size;
2179 skb->data_len += skb_shinfo(skb)->frags[i].size;
2180 i++;
2181 skb_shinfo(skb)->nr_frags = i;
2182 }
2183
2184 while (i < frags) {
2185 int rem;
2186
2187 if (i == 0)
2188 break;
2189
2190 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
2191 if (rem == 0)
2192 break;
2193
2194 skb_shinfo(skb)->frags[i - 1].size -= rem;
2195
Luiz Capitulino222f1802006-03-20 22:16:13 -08002196 skb_shinfo(skb)->frags[i] =
2197 skb_shinfo(skb)->frags[i - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 get_page(skb_shinfo(skb)->frags[i].page);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002199 skb_shinfo(skb)->frags[i].page =
2200 skb_shinfo(skb)->frags[i - 1].page;
2201 skb_shinfo(skb)->frags[i].page_offset +=
2202 skb_shinfo(skb)->frags[i - 1].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 skb_shinfo(skb)->frags[i].size = rem;
2204 i++;
2205 skb_shinfo(skb)->nr_frags = i;
2206 }
2207 }
2208
Luiz Capitulino222f1802006-03-20 22:16:13 -08002209 /* Stamp the time, and sequence number, convert them to network byte order */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210
Luiz Capitulino222f1802006-03-20 22:16:13 -08002211 if (pgh) {
2212 struct timeval timestamp;
2213
2214 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2215 pgh->seq_num = htonl(pkt_dev->seq_num);
2216
2217 do_gettimeofday(&timestamp);
2218 pgh->tv_sec = htonl(timestamp.tv_sec);
2219 pgh->tv_usec = htonl(timestamp.tv_usec);
2220 }
2221 pkt_dev->seq_num++;
2222
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 return skb;
2224}
2225
2226/*
2227 * scan_ip6, fmt_ip taken from dietlibc-0.21
2228 * Author Felix von Leitner <felix-dietlibc@fefe.de>
2229 *
2230 * Slightly modified for kernel.
2231 * Should be candidate for net/ipv4/utils.c
2232 * --ro
2233 */
2234
Luiz Capitulino222f1802006-03-20 22:16:13 -08002235static unsigned int scan_ip6(const char *s, char ip[16])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236{
2237 unsigned int i;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002238 unsigned int len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 unsigned long u;
2240 char suffix[16];
Luiz Capitulino222f1802006-03-20 22:16:13 -08002241 unsigned int prefixlen = 0;
2242 unsigned int suffixlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 __u32 tmp;
2244
Luiz Capitulino222f1802006-03-20 22:16:13 -08002245 for (i = 0; i < 16; i++)
2246 ip[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247
2248 for (;;) {
2249 if (*s == ':') {
2250 len++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002251 if (s[1] == ':') { /* Found "::", skip to part 2 */
2252 s += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 len++;
2254 break;
2255 }
2256 s++;
2257 }
2258 {
2259 char *tmp;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002260 u = simple_strtoul(s, &tmp, 16);
2261 i = tmp - s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 }
2263
Luiz Capitulino222f1802006-03-20 22:16:13 -08002264 if (!i)
2265 return 0;
2266 if (prefixlen == 12 && s[i] == '.') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267
2268 /* the last 4 bytes may be written as IPv4 address */
2269
2270 tmp = in_aton(s);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002271 memcpy((struct in_addr *)(ip + 12), &tmp, sizeof(tmp));
2272 return i + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 }
2274 ip[prefixlen++] = (u >> 8);
2275 ip[prefixlen++] = (u & 255);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002276 s += i;
2277 len += i;
2278 if (prefixlen == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 return len;
2280 }
2281
2282/* part 2, after "::" */
2283 for (;;) {
2284 if (*s == ':') {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002285 if (suffixlen == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 break;
2287 s++;
2288 len++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002289 } else if (suffixlen != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 break;
2291 {
2292 char *tmp;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002293 u = simple_strtol(s, &tmp, 16);
2294 i = tmp - s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 }
2296 if (!i) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002297 if (*s)
2298 len--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 break;
2300 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002301 if (suffixlen + prefixlen <= 12 && s[i] == '.') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 tmp = in_aton(s);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002303 memcpy((struct in_addr *)(suffix + suffixlen), &tmp,
2304 sizeof(tmp));
2305 suffixlen += 4;
2306 len += strlen(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 break;
2308 }
2309 suffix[suffixlen++] = (u >> 8);
2310 suffix[suffixlen++] = (u & 255);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002311 s += i;
2312 len += i;
2313 if (prefixlen + suffixlen == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 break;
2315 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002316 for (i = 0; i < suffixlen; i++)
2317 ip[16 - suffixlen + i] = suffix[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 return len;
2319}
2320
Luiz Capitulino222f1802006-03-20 22:16:13 -08002321static char tohex(char hexdigit)
2322{
2323 return hexdigit > 9 ? hexdigit + 'a' - 10 : hexdigit + '0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324}
2325
Luiz Capitulino222f1802006-03-20 22:16:13 -08002326static int fmt_xlong(char *s, unsigned int i)
2327{
2328 char *bak = s;
2329 *s = tohex((i >> 12) & 0xf);
2330 if (s != bak || *s != '0')
2331 ++s;
2332 *s = tohex((i >> 8) & 0xf);
2333 if (s != bak || *s != '0')
2334 ++s;
2335 *s = tohex((i >> 4) & 0xf);
2336 if (s != bak || *s != '0')
2337 ++s;
2338 *s = tohex(i & 0xf);
2339 return s - bak + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340}
2341
Luiz Capitulino222f1802006-03-20 22:16:13 -08002342static unsigned int fmt_ip6(char *s, const char ip[16])
2343{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 unsigned int len;
2345 unsigned int i;
2346 unsigned int temp;
2347 unsigned int compressing;
2348 int j;
2349
Luiz Capitulino222f1802006-03-20 22:16:13 -08002350 len = 0;
2351 compressing = 0;
2352 for (j = 0; j < 16; j += 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353
2354#ifdef V4MAPPEDPREFIX
Luiz Capitulino222f1802006-03-20 22:16:13 -08002355 if (j == 12 && !memcmp(ip, V4mappedprefix, 12)) {
2356 inet_ntoa_r(*(struct in_addr *)(ip + 12), s);
2357 temp = strlen(s);
2358 return len + temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 }
2360#endif
Luiz Capitulino222f1802006-03-20 22:16:13 -08002361 temp = ((unsigned long)(unsigned char)ip[j] << 8) +
2362 (unsigned long)(unsigned char)ip[j + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 if (temp == 0) {
2364 if (!compressing) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002365 compressing = 1;
2366 if (j == 0) {
2367 *s++ = ':';
2368 ++len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 }
2370 }
2371 } else {
2372 if (compressing) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002373 compressing = 0;
2374 *s++ = ':';
2375 ++len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002377 i = fmt_xlong(s, temp);
2378 len += i;
2379 s += i;
2380 if (j < 14) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 *s++ = ':';
2382 ++len;
2383 }
2384 }
2385 }
2386 if (compressing) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002387 *s++ = ':';
2388 ++len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002390 *s = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 return len;
2392}
2393
Luiz Capitulino222f1802006-03-20 22:16:13 -08002394static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
2395 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396{
2397 struct sk_buff *skb = NULL;
2398 __u8 *eth;
2399 struct udphdr *udph;
2400 int datalen;
2401 struct ipv6hdr *iph;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002402 struct pktgen_hdr *pgh = NULL;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002403 __be16 protocol = __constant_htons(ETH_P_IPV6);
2404 __be32 *mpls;
2405
2406 if (pkt_dev->nr_labels)
2407 protocol = __constant_htons(ETH_P_MPLS_UC);
Robert Olsson64053be2005-06-26 15:27:10 -07002408
2409 /* Update any of the values, used when we're incrementing various
2410 * fields.
2411 */
2412 mod_cur_headers(pkt_dev);
2413
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002414 skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + 16 +
2415 pkt_dev->nr_labels*sizeof(u32), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 if (!skb) {
2417 sprintf(pkt_dev->result, "No memory");
2418 return NULL;
2419 }
2420
2421 skb_reserve(skb, 16);
2422
2423 /* Reserve for ethernet and IP header */
2424 eth = (__u8 *) skb_push(skb, 14);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002425 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2426 if (pkt_dev->nr_labels)
2427 mpls_push(mpls, pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 iph = (struct ipv6hdr *)skb_put(skb, sizeof(struct ipv6hdr));
2429 udph = (struct udphdr *)skb_put(skb, sizeof(struct udphdr));
2430
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 memcpy(eth, pkt_dev->hh, 12);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002432 *(u16 *) & eth[12] = __constant_htons(ETH_P_IPV6);
Robert Olsson64053be2005-06-26 15:27:10 -07002433
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002434 /* Eth + IPh + UDPh + mpls */
2435 datalen = pkt_dev->cur_pkt_size - 14 -
2436 sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
2437 pkt_dev->nr_labels*sizeof(u32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438
Luiz Capitulino222f1802006-03-20 22:16:13 -08002439 if (datalen < sizeof(struct pktgen_hdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 datalen = sizeof(struct pktgen_hdr);
2441 if (net_ratelimit())
Luiz Capitulino222f1802006-03-20 22:16:13 -08002442 printk(KERN_INFO "pktgen: increased datalen to %d\n",
2443 datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 }
2445
2446 udph->source = htons(pkt_dev->cur_udp_src);
2447 udph->dest = htons(pkt_dev->cur_udp_dst);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002448 udph->len = htons(datalen + sizeof(struct udphdr));
2449 udph->check = 0; /* No checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450
Luiz Capitulino222f1802006-03-20 22:16:13 -08002451 *(u32 *) iph = __constant_htonl(0x60000000); /* Version + flow */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452
2453 iph->hop_limit = 32;
2454
2455 iph->payload_len = htons(sizeof(struct udphdr) + datalen);
2456 iph->nexthdr = IPPROTO_UDP;
2457
2458 ipv6_addr_copy(&iph->daddr, &pkt_dev->cur_in6_daddr);
2459 ipv6_addr_copy(&iph->saddr, &pkt_dev->cur_in6_saddr);
2460
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002461 skb->mac.raw = ((u8 *) iph) - 14 - pkt_dev->nr_labels*sizeof(u32);
2462 skb->protocol = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 skb->dev = odev;
2464 skb->pkt_type = PACKET_HOST;
David S. Miller69d8c282006-08-07 20:52:10 -07002465 skb->nh.ipv6h = iph;
2466 skb->h.uh = udph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467
Luiz Capitulino222f1802006-03-20 22:16:13 -08002468 if (pkt_dev->nfrags <= 0)
2469 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 else {
2471 int frags = pkt_dev->nfrags;
2472 int i;
2473
Luiz Capitulino222f1802006-03-20 22:16:13 -08002474 pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
2475
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 if (frags > MAX_SKB_FRAGS)
2477 frags = MAX_SKB_FRAGS;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002478 if (datalen > frags * PAGE_SIZE) {
2479 skb_put(skb, datalen - frags * PAGE_SIZE);
2480 datalen = frags * PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 }
2482
2483 i = 0;
2484 while (datalen > 0) {
2485 struct page *page = alloc_pages(GFP_KERNEL, 0);
2486 skb_shinfo(skb)->frags[i].page = page;
2487 skb_shinfo(skb)->frags[i].page_offset = 0;
2488 skb_shinfo(skb)->frags[i].size =
Luiz Capitulino222f1802006-03-20 22:16:13 -08002489 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 datalen -= skb_shinfo(skb)->frags[i].size;
2491 skb->len += skb_shinfo(skb)->frags[i].size;
2492 skb->data_len += skb_shinfo(skb)->frags[i].size;
2493 i++;
2494 skb_shinfo(skb)->nr_frags = i;
2495 }
2496
2497 while (i < frags) {
2498 int rem;
2499
2500 if (i == 0)
2501 break;
2502
2503 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
2504 if (rem == 0)
2505 break;
2506
2507 skb_shinfo(skb)->frags[i - 1].size -= rem;
2508
Luiz Capitulino222f1802006-03-20 22:16:13 -08002509 skb_shinfo(skb)->frags[i] =
2510 skb_shinfo(skb)->frags[i - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 get_page(skb_shinfo(skb)->frags[i].page);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002512 skb_shinfo(skb)->frags[i].page =
2513 skb_shinfo(skb)->frags[i - 1].page;
2514 skb_shinfo(skb)->frags[i].page_offset +=
2515 skb_shinfo(skb)->frags[i - 1].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 skb_shinfo(skb)->frags[i].size = rem;
2517 i++;
2518 skb_shinfo(skb)->nr_frags = i;
2519 }
2520 }
2521
Luiz Capitulino222f1802006-03-20 22:16:13 -08002522 /* Stamp the time, and sequence number, convert them to network byte order */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 /* should we update cloned packets too ? */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002524 if (pgh) {
2525 struct timeval timestamp;
2526
2527 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2528 pgh->seq_num = htonl(pkt_dev->seq_num);
2529
2530 do_gettimeofday(&timestamp);
2531 pgh->tv_sec = htonl(timestamp.tv_sec);
2532 pgh->tv_usec = htonl(timestamp.tv_usec);
2533 }
2534 pkt_dev->seq_num++;
2535
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 return skb;
2537}
2538
Luiz Capitulino222f1802006-03-20 22:16:13 -08002539static inline struct sk_buff *fill_packet(struct net_device *odev,
2540 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541{
Luiz Capitulino222f1802006-03-20 22:16:13 -08002542 if (pkt_dev->flags & F_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 return fill_packet_ipv6(odev, pkt_dev);
2544 else
2545 return fill_packet_ipv4(odev, pkt_dev);
2546}
2547
Luiz Capitulino222f1802006-03-20 22:16:13 -08002548static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549{
Luiz Capitulino222f1802006-03-20 22:16:13 -08002550 pkt_dev->seq_num = 1;
2551 pkt_dev->idle_acc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002553 pkt_dev->tx_bytes = 0;
2554 pkt_dev->errors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555}
2556
2557/* Set up structure for sending pkts, clear counters */
2558
2559static void pktgen_run(struct pktgen_thread *t)
2560{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08002561 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 int started = 0;
2563
2564 PG_DEBUG(printk("pktgen: entering pktgen_run. %p\n", t));
2565
2566 if_lock(t);
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08002567 list_for_each_entry(pkt_dev, &t->if_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568
2569 /*
2570 * setup odev and create initial packet.
2571 */
2572 pktgen_setup_inject(pkt_dev);
2573
Luiz Capitulino222f1802006-03-20 22:16:13 -08002574 if (pkt_dev->odev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 pktgen_clear_counters(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002576 pkt_dev->running = 1; /* Cranke yeself! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 pkt_dev->skb = NULL;
2578 pkt_dev->started_at = getCurUs();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002579 pkt_dev->next_tx_us = getCurUs(); /* Transmit immediately */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 pkt_dev->next_tx_ns = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002581
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 strcpy(pkt_dev->result, "Starting");
2583 started++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002584 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 strcpy(pkt_dev->result, "Error starting");
2586 }
2587 if_unlock(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002588 if (started)
2589 t->control &= ~(T_STOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590}
2591
2592static void pktgen_stop_all_threads_ifs(void)
2593{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002594 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595
Arthur Kepner95ed63f2006-03-20 21:26:56 -08002596 PG_DEBUG(printk("pktgen: entering pktgen_stop_all_threads_ifs.\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08002598 mutex_lock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002599
2600 list_for_each_entry(t, &pktgen_threads, th_list)
Arthur Kepner95ed63f2006-03-20 21:26:56 -08002601 t->control |= T_STOP;
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002602
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08002603 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604}
2605
Luiz Capitulino222f1802006-03-20 22:16:13 -08002606static int thread_is_running(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08002608 struct pktgen_dev *pkt_dev;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002609 int res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08002611 list_for_each_entry(pkt_dev, &t->if_list, list)
2612 if (pkt_dev->running) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 res = 1;
2614 break;
2615 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002616 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617}
2618
Luiz Capitulino222f1802006-03-20 22:16:13 -08002619static int pktgen_wait_thread_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620{
Luiz Capitulino222f1802006-03-20 22:16:13 -08002621 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622
Luiz Capitulino222f1802006-03-20 22:16:13 -08002623 while (thread_is_running(t)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624
Luiz Capitulino222f1802006-03-20 22:16:13 -08002625 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626
Luiz Capitulino222f1802006-03-20 22:16:13 -08002627 msleep_interruptible(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628
Luiz Capitulino222f1802006-03-20 22:16:13 -08002629 if (signal_pending(current))
2630 goto signal;
2631 if_lock(t);
2632 }
2633 if_unlock(t);
2634 return 1;
2635signal:
2636 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637}
2638
2639static int pktgen_wait_all_threads_run(void)
2640{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002641 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 int sig = 1;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002643
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08002644 mutex_lock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002645
2646 list_for_each_entry(t, &pktgen_threads, th_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 sig = pktgen_wait_thread_run(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002648 if (sig == 0)
2649 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 }
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002651
2652 if (sig == 0)
2653 list_for_each_entry(t, &pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 t->control |= (T_STOP);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002655
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08002656 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 return sig;
2658}
2659
2660static void pktgen_run_all_threads(void)
2661{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002662 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663
2664 PG_DEBUG(printk("pktgen: entering pktgen_run_all_threads.\n"));
2665
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08002666 mutex_lock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002668 list_for_each_entry(t, &pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 t->control |= (T_RUN);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002670
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08002671 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672
Luiz Capitulino222f1802006-03-20 22:16:13 -08002673 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
2674
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 pktgen_wait_all_threads_run();
2676}
2677
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
2679{
Luiz Capitulino222f1802006-03-20 22:16:13 -08002680 __u64 total_us, bps, mbps, pps, idle;
2681 char *p = pkt_dev->result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682
Luiz Capitulino222f1802006-03-20 22:16:13 -08002683 total_us = pkt_dev->stopped_at - pkt_dev->started_at;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684
Luiz Capitulino222f1802006-03-20 22:16:13 -08002685 idle = pkt_dev->idle_acc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686
Luiz Capitulino222f1802006-03-20 22:16:13 -08002687 p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
2688 (unsigned long long)total_us,
2689 (unsigned long long)(total_us - idle),
2690 (unsigned long long)idle,
2691 (unsigned long long)pkt_dev->sofar,
2692 pkt_dev->cur_pkt_size, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693
Luiz Capitulino222f1802006-03-20 22:16:13 -08002694 pps = pkt_dev->sofar * USEC_PER_SEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695
Luiz Capitulino222f1802006-03-20 22:16:13 -08002696 while ((total_us >> 32) != 0) {
2697 pps >>= 1;
2698 total_us >>= 1;
2699 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700
Luiz Capitulino222f1802006-03-20 22:16:13 -08002701 do_div(pps, total_us);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702
Luiz Capitulino222f1802006-03-20 22:16:13 -08002703 bps = pps * 8 * pkt_dev->cur_pkt_size;
2704
2705 mbps = bps;
2706 do_div(mbps, 1000000);
2707 p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu",
2708 (unsigned long long)pps,
2709 (unsigned long long)mbps,
2710 (unsigned long long)bps,
2711 (unsigned long long)pkt_dev->errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713
2714/* Set stopped-at timer, remove from running list, do counters & statistics */
2715
Luiz Capitulino222f1802006-03-20 22:16:13 -08002716static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717{
Luiz Capitulino222f1802006-03-20 22:16:13 -08002718 int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08002719
Luiz Capitulino222f1802006-03-20 22:16:13 -08002720 if (!pkt_dev->running) {
2721 printk("pktgen: interface: %s is already stopped\n",
2722 pkt_dev->ifname);
2723 return -EINVAL;
2724 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725
Luiz Capitulino222f1802006-03-20 22:16:13 -08002726 pkt_dev->stopped_at = getCurUs();
2727 pkt_dev->running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728
Arthur Kepner95ed63f2006-03-20 21:26:56 -08002729 show_results(pkt_dev, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730
Luiz Capitulino222f1802006-03-20 22:16:13 -08002731 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732}
2733
Luiz Capitulino222f1802006-03-20 22:16:13 -08002734static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08002736 struct pktgen_dev *pkt_dev, *best = NULL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002737
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 if_lock(t);
2739
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08002740 list_for_each_entry(pkt_dev, &t->if_list, list) {
2741 if (!pkt_dev->running)
Luiz Capitulino222f1802006-03-20 22:16:13 -08002742 continue;
2743 if (best == NULL)
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08002744 best = pkt_dev;
2745 else if (pkt_dev->next_tx_us < best->next_tx_us)
2746 best = pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 }
2748 if_unlock(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002749 return best;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750}
2751
Luiz Capitulino222f1802006-03-20 22:16:13 -08002752static void pktgen_stop(struct pktgen_thread *t)
2753{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08002754 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755
Arthur Kepner95ed63f2006-03-20 21:26:56 -08002756 PG_DEBUG(printk("pktgen: entering pktgen_stop\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757
Luiz Capitulino222f1802006-03-20 22:16:13 -08002758 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08002760 list_for_each_entry(pkt_dev, &t->if_list, list) {
2761 pktgen_stop_device(pkt_dev);
2762 if (pkt_dev->skb)
2763 kfree_skb(pkt_dev->skb);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08002764
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08002765 pkt_dev->skb = NULL;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08002766 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767
Luiz Capitulino222f1802006-03-20 22:16:13 -08002768 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769}
2770
Arthur Kepner95ed63f2006-03-20 21:26:56 -08002771/*
2772 * one of our devices needs to be removed - find it
2773 * and remove it
2774 */
2775static void pktgen_rem_one_if(struct pktgen_thread *t)
2776{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08002777 struct list_head *q, *n;
2778 struct pktgen_dev *cur;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08002779
2780 PG_DEBUG(printk("pktgen: entering pktgen_rem_one_if\n"));
2781
2782 if_lock(t);
2783
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08002784 list_for_each_safe(q, n, &t->if_list) {
2785 cur = list_entry(q, struct pktgen_dev, list);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08002786
Luiz Capitulino222f1802006-03-20 22:16:13 -08002787 if (!cur->removal_mark)
2788 continue;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08002789
2790 if (cur->skb)
2791 kfree_skb(cur->skb);
2792 cur->skb = NULL;
2793
2794 pktgen_remove_device(t, cur);
2795
2796 break;
2797 }
2798
2799 if_unlock(t);
2800}
2801
Luiz Capitulino222f1802006-03-20 22:16:13 -08002802static void pktgen_rem_all_ifs(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08002804 struct list_head *q, *n;
2805 struct pktgen_dev *cur;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002806
2807 /* Remove all devices, free mem */
Arthur Kepner95ed63f2006-03-20 21:26:56 -08002808
2809 PG_DEBUG(printk("pktgen: entering pktgen_rem_all_ifs\n"));
Luiz Capitulino222f1802006-03-20 22:16:13 -08002810 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08002812 list_for_each_safe(q, n, &t->if_list) {
2813 cur = list_entry(q, struct pktgen_dev, list);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08002814
2815 if (cur->skb)
2816 kfree_skb(cur->skb);
2817 cur->skb = NULL;
2818
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 pktgen_remove_device(t, cur);
2820 }
2821
Luiz Capitulino222f1802006-03-20 22:16:13 -08002822 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823}
2824
Luiz Capitulino222f1802006-03-20 22:16:13 -08002825static void pktgen_rem_thread(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826{
Luiz Capitulino222f1802006-03-20 22:16:13 -08002827 /* Remove from the thread list */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07002829 remove_proc_entry(t->name, pg_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08002831 mutex_lock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002833 list_del(&t->th_list);
2834
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08002835 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836}
2837
2838static __inline__ void pktgen_xmit(struct pktgen_dev *pkt_dev)
2839{
2840 struct net_device *odev = NULL;
2841 __u64 idle_start = 0;
2842 int ret;
2843
2844 odev = pkt_dev->odev;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002845
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 if (pkt_dev->delay_us || pkt_dev->delay_ns) {
2847 u64 now;
2848
2849 now = getCurUs();
2850 if (now < pkt_dev->next_tx_us)
2851 spin(pkt_dev, pkt_dev->next_tx_us);
2852
2853 /* This is max DELAY, this has special meaning of
2854 * "never transmit"
2855 */
2856 if (pkt_dev->delay_us == 0x7FFFFFFF) {
2857 pkt_dev->next_tx_us = getCurUs() + pkt_dev->delay_us;
2858 pkt_dev->next_tx_ns = pkt_dev->delay_ns;
2859 goto out;
2860 }
2861 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002862
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 if (netif_queue_stopped(odev) || need_resched()) {
2864 idle_start = getCurUs();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002865
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 if (!netif_running(odev)) {
2867 pktgen_stop_device(pkt_dev);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08002868 if (pkt_dev->skb)
2869 kfree_skb(pkt_dev->skb);
2870 pkt_dev->skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 goto out;
2872 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002873 if (need_resched())
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 schedule();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002875
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 pkt_dev->idle_acc += getCurUs() - idle_start;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002877
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 if (netif_queue_stopped(odev)) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002879 pkt_dev->next_tx_us = getCurUs(); /* TODO */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 pkt_dev->next_tx_ns = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002881 goto out; /* Try the next interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 }
2883 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002884
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 if (pkt_dev->last_ok || !pkt_dev->skb) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002886 if ((++pkt_dev->clone_count >= pkt_dev->clone_skb)
2887 || (!pkt_dev->skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 /* build a new pkt */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002889 if (pkt_dev->skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 kfree_skb(pkt_dev->skb);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002891
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 pkt_dev->skb = fill_packet(odev, pkt_dev);
2893 if (pkt_dev->skb == NULL) {
2894 printk("pktgen: ERROR: couldn't allocate skb in fill_packet.\n");
2895 schedule();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002896 pkt_dev->clone_count--; /* back out increment, OOM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 goto out;
2898 }
2899 pkt_dev->allocated_skbs++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002900 pkt_dev->clone_count = 0; /* reset counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 }
2902 }
Arthur Kepner95ed63f2006-03-20 21:26:56 -08002903
Herbert Xu932ff272006-06-09 12:20:56 -07002904 netif_tx_lock_bh(odev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 if (!netif_queue_stopped(odev)) {
2906
2907 atomic_inc(&(pkt_dev->skb->users));
Luiz Capitulino222f1802006-03-20 22:16:13 -08002908 retry_now:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 ret = odev->hard_start_xmit(pkt_dev->skb, odev);
2910 if (likely(ret == NETDEV_TX_OK)) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002911 pkt_dev->last_ok = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 pkt_dev->sofar++;
2913 pkt_dev->seq_num++;
2914 pkt_dev->tx_bytes += pkt_dev->cur_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002915
2916 } else if (ret == NETDEV_TX_LOCKED
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 && (odev->features & NETIF_F_LLTX)) {
2918 cpu_relax();
2919 goto retry_now;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002920 } else { /* Retry it next time */
2921
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 atomic_dec(&(pkt_dev->skb->users));
Luiz Capitulino222f1802006-03-20 22:16:13 -08002923
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 if (debug && net_ratelimit())
2925 printk(KERN_INFO "pktgen: Hard xmit error\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08002926
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 pkt_dev->errors++;
2928 pkt_dev->last_ok = 0;
2929 }
2930
2931 pkt_dev->next_tx_us = getCurUs();
2932 pkt_dev->next_tx_ns = 0;
2933
2934 pkt_dev->next_tx_us += pkt_dev->delay_us;
2935 pkt_dev->next_tx_ns += pkt_dev->delay_ns;
2936
2937 if (pkt_dev->next_tx_ns > 1000) {
2938 pkt_dev->next_tx_us++;
2939 pkt_dev->next_tx_ns -= 1000;
2940 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002941 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942
Luiz Capitulino222f1802006-03-20 22:16:13 -08002943 else { /* Retry it next time */
2944 pkt_dev->last_ok = 0;
2945 pkt_dev->next_tx_us = getCurUs(); /* TODO */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 pkt_dev->next_tx_ns = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002947 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948
Herbert Xu932ff272006-06-09 12:20:56 -07002949 netif_tx_unlock_bh(odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002950
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 /* If pkt_dev->count is zero, then run forever */
2952 if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
2953 if (atomic_read(&(pkt_dev->skb->users)) != 1) {
2954 idle_start = getCurUs();
2955 while (atomic_read(&(pkt_dev->skb->users)) != 1) {
2956 if (signal_pending(current)) {
2957 break;
2958 }
2959 schedule();
2960 }
2961 pkt_dev->idle_acc += getCurUs() - idle_start;
2962 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002963
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 /* Done with this */
2965 pktgen_stop_device(pkt_dev);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08002966 if (pkt_dev->skb)
2967 kfree_skb(pkt_dev->skb);
2968 pkt_dev->skb = NULL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002969 }
2970out:;
2971}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972
2973/*
2974 * Main loop of the thread goes here
2975 */
2976
Luiz Capitulino222f1802006-03-20 22:16:13 -08002977static void pktgen_thread_worker(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978{
2979 DEFINE_WAIT(wait);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002980 struct pktgen_dev *pkt_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 int cpu = t->cpu;
2982 sigset_t tmpsig;
2983 u32 max_before_softirq;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002984 u32 tx_since_softirq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985
2986 daemonize("pktgen/%d", cpu);
2987
Luiz Capitulino222f1802006-03-20 22:16:13 -08002988 /* Block all signals except SIGKILL, SIGSTOP and SIGTERM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989
Luiz Capitulino222f1802006-03-20 22:16:13 -08002990 spin_lock_irq(&current->sighand->siglock);
2991 tmpsig = current->blocked;
2992 siginitsetinv(&current->blocked,
2993 sigmask(SIGKILL) | sigmask(SIGSTOP) | sigmask(SIGTERM));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994
Luiz Capitulino222f1802006-03-20 22:16:13 -08002995 recalc_sigpending();
2996 spin_unlock_irq(&current->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997
2998 /* Migrate to the right CPU */
2999 set_cpus_allowed(current, cpumask_of_cpu(cpu));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003000 if (smp_processor_id() != cpu)
3001 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002
3003 init_waitqueue_head(&t->queue);
3004
3005 t->control &= ~(T_TERMINATE);
3006 t->control &= ~(T_RUN);
3007 t->control &= ~(T_STOP);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003008 t->control &= ~(T_REMDEVALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 t->control &= ~(T_REMDEV);
3010
Luiz Capitulino222f1802006-03-20 22:16:13 -08003011 t->pid = current->pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012
Luiz Capitulino222f1802006-03-20 22:16:13 -08003013 PG_DEBUG(printk("pktgen: starting pktgen/%d: pid=%d\n", cpu, current->pid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014
3015 max_before_softirq = t->max_before_softirq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016
Luiz Capitulino222f1802006-03-20 22:16:13 -08003017 __set_current_state(TASK_INTERRUPTIBLE);
3018 mb();
3019
3020 while (1) {
3021
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 __set_current_state(TASK_RUNNING);
3023
3024 /*
3025 * Get next dev to xmit -- if any.
3026 */
3027
Luiz Capitulino222f1802006-03-20 22:16:13 -08003028 pkt_dev = next_to_run(t);
3029
3030 if (pkt_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031
3032 pktgen_xmit(pkt_dev);
3033
3034 /*
3035 * We like to stay RUNNING but must also give
3036 * others fair share.
3037 */
3038
3039 tx_since_softirq += pkt_dev->last_ok;
3040
3041 if (tx_since_softirq > max_before_softirq) {
3042 if (local_softirq_pending())
3043 do_softirq();
3044 tx_since_softirq = 0;
3045 }
3046 } else {
3047 prepare_to_wait(&(t->queue), &wait, TASK_INTERRUPTIBLE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003048 schedule_timeout(HZ / 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 finish_wait(&(t->queue), &wait);
3050 }
3051
Luiz Capitulino222f1802006-03-20 22:16:13 -08003052 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 * Back from sleep, either due to the timeout or signal.
3054 * We check if we have any "posted" work for us.
3055 */
3056
Luiz Capitulino222f1802006-03-20 22:16:13 -08003057 if (t->control & T_TERMINATE || signal_pending(current))
3058 /* we received a request to terminate ourself */
3059 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060
Luiz Capitulino222f1802006-03-20 22:16:13 -08003061 if (t->control & T_STOP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 pktgen_stop(t);
3063 t->control &= ~(T_STOP);
3064 }
3065
Luiz Capitulino222f1802006-03-20 22:16:13 -08003066 if (t->control & T_RUN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 pktgen_run(t);
3068 t->control &= ~(T_RUN);
3069 }
3070
Luiz Capitulino222f1802006-03-20 22:16:13 -08003071 if (t->control & T_REMDEVALL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 pktgen_rem_all_ifs(t);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003073 t->control &= ~(T_REMDEVALL);
3074 }
3075
Luiz Capitulino222f1802006-03-20 22:16:13 -08003076 if (t->control & T_REMDEV) {
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003077 pktgen_rem_one_if(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 t->control &= ~(T_REMDEV);
3079 }
3080
Luiz Capitulino222f1802006-03-20 22:16:13 -08003081 if (need_resched())
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 schedule();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084
Luiz Capitulino222f1802006-03-20 22:16:13 -08003085 PG_DEBUG(printk("pktgen: %s stopping all device\n", t->name));
3086 pktgen_stop(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087
Luiz Capitulino222f1802006-03-20 22:16:13 -08003088 PG_DEBUG(printk("pktgen: %s removing all device\n", t->name));
3089 pktgen_rem_all_ifs(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090
Luiz Capitulino222f1802006-03-20 22:16:13 -08003091 PG_DEBUG(printk("pktgen: %s removing thread.\n", t->name));
3092 pktgen_rem_thread(t);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003093
3094 t->removed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095}
3096
Luiz Capitulino222f1802006-03-20 22:16:13 -08003097static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
3098 const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003100 struct pktgen_dev *p, *pkt_dev = NULL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003101 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003103 list_for_each_entry(p, &t->if_list, list)
3104 if (strncmp(p->ifname, ifname, IFNAMSIZ) == 0) {
3105 pkt_dev = p;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003106 break;
3107 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108
Luiz Capitulino222f1802006-03-20 22:16:13 -08003109 if_unlock(t);
3110 PG_DEBUG(printk("pktgen: find_dev(%s) returning %p\n", ifname, pkt_dev));
3111 return pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112}
3113
3114/*
3115 * Adds a dev at front of if_list.
3116 */
3117
Luiz Capitulino222f1802006-03-20 22:16:13 -08003118static int add_dev_to_thread(struct pktgen_thread *t,
3119 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120{
3121 int rv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122
Luiz Capitulino222f1802006-03-20 22:16:13 -08003123 if_lock(t);
3124
3125 if (pkt_dev->pg_thread) {
3126 printk("pktgen: ERROR: already assigned to a thread.\n");
3127 rv = -EBUSY;
3128 goto out;
3129 }
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003130
3131 list_add(&pkt_dev->list, &t->if_list);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003132 pkt_dev->pg_thread = t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 pkt_dev->running = 0;
3134
Luiz Capitulino222f1802006-03-20 22:16:13 -08003135out:
3136 if_unlock(t);
3137 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138}
3139
3140/* Called under thread lock */
3141
Luiz Capitulino222f1802006-03-20 22:16:13 -08003142static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003144 struct pktgen_dev *pkt_dev;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003145 struct proc_dir_entry *pe;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003146
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 /* We don't allow a device to be on several threads */
3148
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003149 pkt_dev = __pktgen_NN_threads(ifname, FIND);
3150 if (pkt_dev) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08003151 printk("pktgen: ERROR: interface already used.\n");
3152 return -EBUSY;
3153 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003154
3155 pkt_dev = kzalloc(sizeof(struct pktgen_dev), GFP_KERNEL);
3156 if (!pkt_dev)
3157 return -ENOMEM;
3158
Luiz Capitulino222f1802006-03-20 22:16:13 -08003159 pkt_dev->flows = vmalloc(MAX_CFLOWS * sizeof(struct flow_state));
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003160 if (pkt_dev->flows == NULL) {
3161 kfree(pkt_dev);
3162 return -ENOMEM;
3163 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003164 memset(pkt_dev->flows, 0, MAX_CFLOWS * sizeof(struct flow_state));
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003165
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003166 pkt_dev->removal_mark = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003167 pkt_dev->min_pkt_size = ETH_ZLEN;
3168 pkt_dev->max_pkt_size = ETH_ZLEN;
3169 pkt_dev->nfrags = 0;
3170 pkt_dev->clone_skb = pg_clone_skb_d;
3171 pkt_dev->delay_us = pg_delay_d / 1000;
3172 pkt_dev->delay_ns = pg_delay_d % 1000;
3173 pkt_dev->count = pg_count_d;
3174 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003175 pkt_dev->udp_src_min = 9; /* sink port */
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003176 pkt_dev->udp_src_max = 9;
3177 pkt_dev->udp_dst_min = 9;
3178 pkt_dev->udp_dst_max = 9;
3179
3180 strncpy(pkt_dev->ifname, ifname, IFNAMSIZ);
3181
Luiz Capitulino222f1802006-03-20 22:16:13 -08003182 if (!pktgen_setup_dev(pkt_dev)) {
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003183 printk("pktgen: ERROR: pktgen_setup_dev failed.\n");
3184 if (pkt_dev->flows)
3185 vfree(pkt_dev->flows);
3186 kfree(pkt_dev);
3187 return -ENODEV;
3188 }
3189
3190 pe = create_proc_entry(ifname, 0600, pg_proc_dir);
3191 if (!pe) {
3192 printk("pktgen: cannot create %s/%s procfs entry.\n",
3193 PG_PROC_DIR, ifname);
3194 if (pkt_dev->flows)
3195 vfree(pkt_dev->flows);
3196 kfree(pkt_dev);
3197 return -EINVAL;
3198 }
3199 pe->proc_fops = &pktgen_if_fops;
3200 pe->data = pkt_dev;
3201
3202 return add_dev_to_thread(t, pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203}
3204
Luiz Capitulino222f1802006-03-20 22:16:13 -08003205static struct pktgen_thread *__init pktgen_find_thread(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003207 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003209 mutex_lock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003211 list_for_each_entry(t, &pktgen_threads, th_list)
3212 if (strcmp(t->name, name) == 0) {
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003213 mutex_unlock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003214 return t;
3215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003217 mutex_unlock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003218 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219}
3220
Luiz Capitulino222f1802006-03-20 22:16:13 -08003221static int __init pktgen_create_thread(const char *name, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222{
Luiz Capitulino12e18722006-03-20 22:17:00 -08003223 int err;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003224 struct pktgen_thread *t = NULL;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003225 struct proc_dir_entry *pe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226
Luiz Capitulino222f1802006-03-20 22:16:13 -08003227 if (strlen(name) > 31) {
3228 printk("pktgen: ERROR: Thread name cannot be more than 31 characters.\n");
3229 return -EINVAL;
3230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231
Luiz Capitulino222f1802006-03-20 22:16:13 -08003232 if (pktgen_find_thread(name)) {
3233 printk("pktgen: ERROR: thread: %s already exists\n", name);
3234 return -EINVAL;
3235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236
Luiz Capitulino222f1802006-03-20 22:16:13 -08003237 t = kzalloc(sizeof(struct pktgen_thread), GFP_KERNEL);
3238 if (!t) {
3239 printk("pktgen: ERROR: out of memory, can't create new thread.\n");
3240 return -ENOMEM;
3241 }
3242
3243 strcpy(t->name, name);
3244 spin_lock_init(&t->if_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 t->cpu = cpu;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003246
3247 pe = create_proc_entry(t->name, 0600, pg_proc_dir);
3248 if (!pe) {
3249 printk("pktgen: cannot create %s/%s procfs entry.\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003250 PG_PROC_DIR, t->name);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003251 kfree(t);
3252 return -EINVAL;
3253 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003254
3255 pe->proc_fops = &pktgen_thread_fops;
3256 pe->data = t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003258 INIT_LIST_HEAD(&t->if_list);
3259
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003260 list_add_tail(&t->th_list, &pktgen_threads);
3261
3262 t->removed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263
Luiz Capitulino12e18722006-03-20 22:17:00 -08003264 err = kernel_thread((void *)pktgen_thread_worker, (void *)t,
3265 CLONE_FS | CLONE_FILES | CLONE_SIGHAND);
3266 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 printk("pktgen: kernel_thread() failed for cpu %d\n", t->cpu);
Luiz Capitulino12e18722006-03-20 22:17:00 -08003268 remove_proc_entry(t->name, pg_proc_dir);
3269 list_del(&t->th_list);
3270 kfree(t);
3271 return err;
3272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273
3274 return 0;
3275}
3276
3277/*
3278 * Removes a device from the thread if_list.
3279 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003280static void _rem_dev_from_if_list(struct pktgen_thread *t,
3281 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003283 struct list_head *q, *n;
3284 struct pktgen_dev *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003286 list_for_each_safe(q, n, &t->if_list) {
3287 p = list_entry(q, struct pktgen_dev, list);
3288 if (p == pkt_dev)
3289 list_del(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 }
3291}
3292
Luiz Capitulino222f1802006-03-20 22:16:13 -08003293static int pktgen_remove_device(struct pktgen_thread *t,
3294 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295{
3296
3297 PG_DEBUG(printk("pktgen: remove_device pkt_dev=%p\n", pkt_dev));
3298
Luiz Capitulino222f1802006-03-20 22:16:13 -08003299 if (pkt_dev->running) {
3300 printk("pktgen:WARNING: trying to remove a running interface, stopping it now.\n");
3301 pktgen_stop_device(pkt_dev);
3302 }
3303
3304 /* Dis-associate from the interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305
3306 if (pkt_dev->odev) {
3307 dev_put(pkt_dev->odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003308 pkt_dev->odev = NULL;
3309 }
3310
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 /* And update the thread if_list */
3312
3313 _rem_dev_from_if_list(t, pkt_dev);
3314
Luiz Capitulino222f1802006-03-20 22:16:13 -08003315 /* Clean up proc file system */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003317 remove_proc_entry(pkt_dev->ifname, pg_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318
3319 if (pkt_dev->flows)
3320 vfree(pkt_dev->flows);
3321 kfree(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003322 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323}
3324
Luiz Capitulino222f1802006-03-20 22:16:13 -08003325static int __init pg_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326{
3327 int cpu;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003328 struct proc_dir_entry *pe;
3329
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 printk(version);
3331
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003332 pg_proc_dir = proc_mkdir(PG_PROC_DIR, proc_net);
3333 if (!pg_proc_dir)
3334 return -ENODEV;
3335 pg_proc_dir->owner = THIS_MODULE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003337 pe = create_proc_entry(PGCTRL, 0600, pg_proc_dir);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003338 if (pe == NULL) {
3339 printk("pktgen: ERROR: cannot create %s procfs entry.\n",
3340 PGCTRL);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003341 proc_net_remove(PG_PROC_DIR);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003342 return -EINVAL;
3343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344
Luiz Capitulino222f1802006-03-20 22:16:13 -08003345 pe->proc_fops = &pktgen_fops;
3346 pe->data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347
3348 /* Register us to receive netdevice events */
3349 register_netdevice_notifier(&pktgen_notifier_block);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003350
John Hawkes670c02c2005-10-13 09:30:31 -07003351 for_each_online_cpu(cpu) {
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003352 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 char buf[30];
3354
Luiz Capitulino222f1802006-03-20 22:16:13 -08003355 sprintf(buf, "kpktgend_%i", cpu);
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003356 err = pktgen_create_thread(buf, cpu);
3357 if (err)
3358 printk("pktgen: WARNING: Cannot create thread for cpu %d (%d)\n",
3359 cpu, err);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003360 }
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003361
3362 if (list_empty(&pktgen_threads)) {
3363 printk("pktgen: ERROR: Initialization failed for all threads\n");
3364 unregister_netdevice_notifier(&pktgen_notifier_block);
3365 remove_proc_entry(PGCTRL, pg_proc_dir);
3366 proc_net_remove(PG_PROC_DIR);
3367 return -ENODEV;
3368 }
3369
Luiz Capitulino222f1802006-03-20 22:16:13 -08003370 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371}
3372
3373static void __exit pg_cleanup(void)
3374{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003375 struct pktgen_thread *t;
3376 struct list_head *q, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 wait_queue_head_t queue;
3378 init_waitqueue_head(&queue);
3379
Luiz Capitulino222f1802006-03-20 22:16:13 -08003380 /* Stop all interfaces & threads */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003382 list_for_each_safe(q, n, &pktgen_threads) {
3383 t = list_entry(q, struct pktgen_thread, th_list);
3384 t->control |= (T_TERMINATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003386 wait_event_interruptible_timeout(queue, (t->removed == 1), HZ);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388
Luiz Capitulino222f1802006-03-20 22:16:13 -08003389 /* Un-register us from receiving netdevice events */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 unregister_netdevice_notifier(&pktgen_notifier_block);
3391
Luiz Capitulino222f1802006-03-20 22:16:13 -08003392 /* Clean up proc file system */
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003393 remove_proc_entry(PGCTRL, pg_proc_dir);
3394 proc_net_remove(PG_PROC_DIR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395}
3396
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397module_init(pg_init);
3398module_exit(pg_cleanup);
3399
3400MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se");
3401MODULE_DESCRIPTION("Packet Generator tool");
3402MODULE_LICENSE("GPL");
3403module_param(pg_count_d, int, 0);
3404module_param(pg_delay_d, int, 0);
3405module_param(pg_clone_skb_d, int, 0);
3406module_param(debug, int, 0);