blob: 9cd3a1cb60ef33b485075cb4ce49183d13f9c67b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Authors:
3 * Copyright 2001, 2002 by Robert Olsson <robert.olsson@its.uu.se>
4 * Uppsala University and
5 * Swedish University of Agricultural Sciences
6 *
7 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
8 * Ben Greear <greearb@candelatech.com>
9 * Jens Låås <jens.laas@data.slu.se>
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version
14 * 2 of the License, or (at your option) any later version.
15 *
16 *
17 * A tool for loading the network with preconfigurated packets.
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090018 * The tool is implemented as a linux module. Parameters are output
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * device, delay (to hard_xmit), number of packets, and whether
20 * to use multiple SKBs or just the same one.
21 * pktgen uses the installed interface's output routine.
22 *
23 * Additional hacking by:
24 *
25 * Jens.Laas@data.slu.se
26 * Improved by ANK. 010120.
27 * Improved by ANK even more. 010212.
28 * MAC address typo fixed. 010417 --ro
29 * Integrated. 020301 --DaveM
30 * Added multiskb option 020301 --DaveM
31 * Scaling of results. 020417--sigurdur@linpro.no
32 * Significant re-work of the module:
33 * * Convert to threaded model to more efficiently be able to transmit
34 * and receive on multiple interfaces at once.
35 * * Converted many counters to __u64 to allow longer runs.
36 * * Allow configuration of ranges, like min/max IP address, MACs,
37 * and UDP-ports, for both source and destination, and can
38 * set to use a random distribution or sequentially walk the range.
39 * * Can now change most values after starting.
40 * * Place 12-byte packet in UDP payload with magic number,
41 * sequence number, and timestamp.
42 * * Add receiver code that detects dropped pkts, re-ordered pkts, and
43 * latencies (with micro-second) precision.
44 * * Add IOCTL interface to easily get counters & configuration.
45 * --Ben Greear <greearb@candelatech.com>
46 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090047 * Renamed multiskb to clone_skb and cleaned up sending core for two distinct
48 * skb modes. A clone_skb=0 mode for Ben "ranges" work and a clone_skb != 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 * as a "fastpath" with a configurable number of clones after alloc's.
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090050 * clone_skb=0 means all packets are allocated this also means ranges time
51 * stamps etc can be used. clone_skb=100 means 1 malloc is followed by 100
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 * clones.
53 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090054 * Also moved to /proc/net/pktgen/
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 * --ro
56 *
57 * Sept 10: Fixed threading/locking. Lots of bone-headed and more clever
58 * mistakes. Also merged in DaveM's patch in the -pre6 patch.
59 * --Ben Greear <greearb@candelatech.com>
60 *
61 * Integrated to 2.5.x 021029 --Lucio Maciel (luciomaciel@zipmail.com.br)
62 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090063 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 * 021124 Finished major redesign and rewrite for new functionality.
65 * See Documentation/networking/pktgen.txt for how to use this.
66 *
67 * The new operation:
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090068 * For each CPU one thread/process is created at start. This process checks
69 * for running devices in the if_list and sends packets until count is 0 it
70 * also the thread checks the thread->control which is used for inter-process
71 * communication. controlling process "posts" operations to the threads this
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 * way. The if_lock should be possible to remove when add/rem_device is merged
73 * into this too.
74 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090075 * By design there should only be *one* "controlling" process. In practice
76 * multiple write accesses gives unpredictable result. Understood by "write"
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 * to /proc gives result code thats should be read be the "writer".
Stephen Hemmingerb4099fa2005-10-14 15:32:22 -070078 * For practical use this should be no problem.
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090080 * Note when adding devices to a specific CPU there good idea to also assign
81 * /proc/irq/XX/smp_affinity so TX-interrupts gets bound to the same CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 * --ro
83 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090084 * Fix refcount off by one if first packet fails, potential null deref,
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 * memleak 030710- KJP
86 *
87 * First "ranges" functionality for ipv6 030726 --ro
88 *
89 * Included flow support. 030802 ANK.
90 *
91 * Fixed unaligned access on IA-64 Grant Grundler <grundler@parisc-linux.org>
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090092 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 * Remove if fix from added Harald Welte <laforge@netfilter.org> 040419
94 * ia64 compilation fix from Aron Griffis <aron@hp.com> 040604
95 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090096 * New xmit() return, do_div and misc clean up by Stephen Hemminger
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 * <shemminger@osdl.org> 040923
98 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090099 * Randy Dunlap fixed u64 printk compiler waring
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 *
101 * Remove FCS from BW calculation. Lennert Buytenhek <buytenh@wantstofly.org>
102 * New time handling. Lennert Buytenhek <buytenh@wantstofly.org> 041213
103 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900104 * Corrections from Nikolai Malykh (nmalykh@bilim.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 * Removed unused flags F_SET_SRCMAC & F_SET_SRCIP 041230
106 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900107 * interruptible_sleep_on_timeout() replaced Nishanth Aravamudan <nacc@us.ibm.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 * 050103
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800109 *
110 * MPLS support by Steven Whitehouse <steve@chygwyn.com>
111 *
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700112 * 802.1Q/Q-in-Q support by Francesco Fondelli (FF) <francesco.fondelli@gmail.com>
113 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 */
115#include <linux/sys.h>
116#include <linux/types.h>
117#include <linux/module.h>
118#include <linux/moduleparam.h>
119#include <linux/kernel.h>
Luiz Capitulino222fa072006-03-20 22:24:27 -0800120#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121#include <linux/sched.h>
122#include <linux/slab.h>
123#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124#include <linux/unistd.h>
125#include <linux/string.h>
126#include <linux/ptrace.h>
127#include <linux/errno.h>
128#include <linux/ioport.h>
129#include <linux/interrupt.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -0800130#include <linux/capability.h>
Andrew Morton09fe3ef2007-04-12 14:45:32 -0700131#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132#include <linux/delay.h>
133#include <linux/timer.h>
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -0800134#include <linux/list.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135#include <linux/init.h>
136#include <linux/skbuff.h>
137#include <linux/netdevice.h>
138#include <linux/inet.h>
139#include <linux/inetdevice.h>
140#include <linux/rtnetlink.h>
141#include <linux/if_arp.h>
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700142#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143#include <linux/in.h>
144#include <linux/ip.h>
145#include <linux/ipv6.h>
146#include <linux/udp.h>
147#include <linux/proc_fs.h>
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700148#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149#include <linux/wait.h>
Kris Katterjohnf404e9a2006-01-17 13:04:57 -0800150#include <linux/etherdevice.h>
David S. Milleree74baa2007-01-01 20:51:53 -0800151#include <linux/kthread.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152#include <net/checksum.h>
153#include <net/ipv6.h>
154#include <net/addrconf.h>
155#include <asm/byteorder.h>
156#include <linux/rcupdate.h>
157#include <asm/bitops.h>
158#include <asm/io.h>
159#include <asm/dma.h>
160#include <asm/uaccess.h>
Luiz Capitulino222f1802006-03-20 22:16:13 -0800161#include <asm/div64.h> /* do_div */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162#include <asm/timex.h>
163
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700164#define VERSION "pktgen v2.68: Packet Generator for packet performance testing.\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166/* The buckets are exponential in 'width' */
167#define LAT_BUCKETS_MAX 32
168#define IP_NAME_SZ 32
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800169#define MAX_MPLS_LABELS 16 /* This is the max label stack depth */
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -0800170#define MPLS_STACK_BOTTOM htonl(0x00000100)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172/* Device flag bits */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800173#define F_IPSRC_RND (1<<0) /* IP-Src Random */
174#define F_IPDST_RND (1<<1) /* IP-Dst Random */
175#define F_UDPSRC_RND (1<<2) /* UDP-Src Random */
176#define F_UDPDST_RND (1<<3) /* UDP-Dst Random */
177#define F_MACSRC_RND (1<<4) /* MAC-Src Random */
178#define F_MACDST_RND (1<<5) /* MAC-Dst Random */
179#define F_TXSIZE_RND (1<<6) /* Transmit size is random */
180#define F_IPV6 (1<<7) /* Interface in IPV6 Mode */
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800181#define F_MPLS_RND (1<<8) /* Random MPLS labels */
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700182#define F_VID_RND (1<<9) /* Random VLAN ID */
183#define F_SVID_RND (1<<10) /* Random SVLAN ID */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185/* Thread control flag bits */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800186#define T_TERMINATE (1<<0)
187#define T_STOP (1<<1) /* Stop run */
188#define T_RUN (1<<2) /* Start run */
189#define T_REMDEVALL (1<<3) /* Remove all devs */
190#define T_REMDEV (1<<4) /* Remove one dev */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192/* If lock -- can be removed after some work */
193#define if_lock(t) spin_lock(&(t->if_lock));
194#define if_unlock(t) spin_unlock(&(t->if_lock));
195
196/* Used to help with determining the pkts on receive */
197#define PKTGEN_MAGIC 0xbe9be955
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700198#define PG_PROC_DIR "pktgen"
199#define PGCTRL "pgctrl"
200static struct proc_dir_entry *pg_proc_dir = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202#define MAX_CFLOWS 65536
203
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700204#define VLAN_TAG_SIZE(x) ((x)->vlan_id == 0xffff ? 0 : 4)
205#define SVLAN_TAG_SIZE(x) ((x)->svlan_id == 0xffff ? 0 : 4)
206
Luiz Capitulino222f1802006-03-20 22:16:13 -0800207struct flow_state {
Al Viro252e3342006-11-14 20:48:11 -0800208 __be32 cur_daddr;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800209 int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210};
211
212struct pktgen_dev {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 /*
214 * Try to keep frequent/infrequent used vars. separated.
215 */
Stephen Hemminger39df2322007-03-04 16:11:51 -0800216 struct proc_dir_entry *entry; /* proc file */
217 struct pktgen_thread *pg_thread;/* the owner */
Luiz Capitulinoc26a8012006-03-20 22:18:16 -0800218 struct list_head list; /* Used for chaining in the thread's run-queue */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Luiz Capitulino222f1802006-03-20 22:16:13 -0800220 int running; /* if this changes to false, the test will stop */
221
222 /* If min != max, then we will either do a linear iteration, or
223 * we will do a random selection from within the range.
224 */
225 __u32 flags;
Arthur Kepner95ed63f2006-03-20 21:26:56 -0800226 int removal_mark; /* non-zero => the device is marked for
227 * removal by worker thread */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Luiz Capitulino222f1802006-03-20 22:16:13 -0800229 int min_pkt_size; /* = ETH_ZLEN; */
230 int max_pkt_size; /* = ETH_ZLEN; */
231 int nfrags;
232 __u32 delay_us; /* Default delay */
233 __u32 delay_ns;
234 __u64 count; /* Default No packets to send */
235 __u64 sofar; /* How many pkts we've sent so far */
236 __u64 tx_bytes; /* How many bytes we've transmitted */
237 __u64 errors; /* Errors when trying to transmit, pkts will be re-sent */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Luiz Capitulino222f1802006-03-20 22:16:13 -0800239 /* runtime counters relating to clone_skb */
240 __u64 next_tx_us; /* timestamp of when to tx next */
241 __u32 next_tx_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Luiz Capitulino222f1802006-03-20 22:16:13 -0800243 __u64 allocated_skbs;
244 __u32 clone_count;
245 int last_ok; /* Was last skb sent?
246 * Or a failed transmit of some sort? This will keep
247 * sequence numbers in order, for example.
248 */
249 __u64 started_at; /* micro-seconds */
250 __u64 stopped_at; /* micro-seconds */
251 __u64 idle_acc; /* micro-seconds */
252 __u32 seq_num;
253
254 int clone_skb; /* Use multiple SKBs during packet gen. If this number
255 * is greater than 1, then that many copies of the same
256 * packet will be sent before a new packet is allocated.
257 * For instance, if you want to send 1024 identical packets
258 * before creating a new packet, set clone_skb to 1024.
259 */
260
261 char dst_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
262 char dst_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
263 char src_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
264 char src_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
265
266 struct in6_addr in6_saddr;
267 struct in6_addr in6_daddr;
268 struct in6_addr cur_in6_daddr;
269 struct in6_addr cur_in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 /* For ranges */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800271 struct in6_addr min_in6_daddr;
272 struct in6_addr max_in6_daddr;
273 struct in6_addr min_in6_saddr;
274 struct in6_addr max_in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Luiz Capitulino222f1802006-03-20 22:16:13 -0800276 /* If we're doing ranges, random or incremental, then this
277 * defines the min/max for those ranges.
278 */
Al Viro252e3342006-11-14 20:48:11 -0800279 __be32 saddr_min; /* inclusive, source IP address */
280 __be32 saddr_max; /* exclusive, source IP address */
281 __be32 daddr_min; /* inclusive, dest IP address */
282 __be32 daddr_max; /* exclusive, dest IP address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Luiz Capitulino222f1802006-03-20 22:16:13 -0800284 __u16 udp_src_min; /* inclusive, source UDP port */
285 __u16 udp_src_max; /* exclusive, source UDP port */
286 __u16 udp_dst_min; /* inclusive, dest UDP port */
287 __u16 udp_dst_max; /* exclusive, dest UDP port */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700289 /* DSCP + ECN */
290 __u8 tos; /* six most significant bits of (former) IPv4 TOS are for dscp codepoint */
291 __u8 traffic_class; /* ditto for the (former) Traffic Class in IPv6 (see RFC 3260, sec. 4) */
292
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800293 /* MPLS */
294 unsigned nr_labels; /* Depth of stack, 0 = no MPLS */
295 __be32 labels[MAX_MPLS_LABELS];
296
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700297 /* VLAN/SVLAN (802.1Q/Q-in-Q) */
298 __u8 vlan_p;
299 __u8 vlan_cfi;
300 __u16 vlan_id; /* 0xffff means no vlan tag */
301
302 __u8 svlan_p;
303 __u8 svlan_cfi;
304 __u16 svlan_id; /* 0xffff means no svlan tag */
305
Luiz Capitulino222f1802006-03-20 22:16:13 -0800306 __u32 src_mac_count; /* How many MACs to iterate through */
307 __u32 dst_mac_count; /* How many MACs to iterate through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Luiz Capitulino222f1802006-03-20 22:16:13 -0800309 unsigned char dst_mac[ETH_ALEN];
310 unsigned char src_mac[ETH_ALEN];
311
312 __u32 cur_dst_mac_offset;
313 __u32 cur_src_mac_offset;
Al Viro252e3342006-11-14 20:48:11 -0800314 __be32 cur_saddr;
315 __be32 cur_daddr;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800316 __u16 cur_udp_dst;
317 __u16 cur_udp_src;
318 __u32 cur_pkt_size;
319
320 __u8 hh[14];
321 /* = {
322 0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
323
324 We fill in SRC address later
325 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
326 0x08, 0x00
327 };
328 */
329 __u16 pad; /* pad out the hh struct to an even 16 bytes */
330
331 struct sk_buff *skb; /* skb we are to transmit next, mainly used for when we
332 * are transmitting the same one multiple times
333 */
334 struct net_device *odev; /* The out-going device. Note that the device should
335 * have it's pg_info pointer pointing back to this
336 * device. This will be set when the user specifies
337 * the out-going device name (not when the inject is
338 * started as it used to do.)
339 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 struct flow_state *flows;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800341 unsigned cflows; /* Concurrent flows (config) */
342 unsigned lflow; /* Flow length (config) */
343 unsigned nflows; /* accumulated flows (stats) */
Stephen Hemminger39df2322007-03-04 16:11:51 -0800344
345 char result[512];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346};
347
348struct pktgen_hdr {
Al Viro252e3342006-11-14 20:48:11 -0800349 __be32 pgh_magic;
350 __be32 seq_num;
351 __be32 tv_sec;
352 __be32 tv_usec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353};
354
355struct pktgen_thread {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800356 spinlock_t if_lock;
Luiz Capitulinoc26a8012006-03-20 22:18:16 -0800357 struct list_head if_list; /* All device here */
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -0800358 struct list_head th_list;
David S. Milleree74baa2007-01-01 20:51:53 -0800359 struct task_struct *tsk;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800360 char result[512];
361 u32 max_before_softirq; /* We'll call do_softirq to prevent starvation. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Luiz Capitulino222f1802006-03-20 22:16:13 -0800363 /* Field for thread to receive "posted" events terminate, stop ifs etc. */
364
365 u32 control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 int pid;
367 int cpu;
368
Luiz Capitulino222f1802006-03-20 22:16:13 -0800369 wait_queue_head_t queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370};
371
372#define REMOVE 1
373#define FIND 0
374
375/* This code works around the fact that do_div cannot handle two 64-bit
376 numbers, and regular 64-bit division doesn't work on x86 kernels.
377 --Ben
378*/
379
380#define PG_DIV 0
381
382/* This was emailed to LMKL by: Chris Caputo <ccaputo@alt.net>
383 * Function copied/adapted/optimized from:
384 *
385 * nemesis.sourceforge.net/browse/lib/static/intmath/ix86/intmath.c.html
386 *
387 * Copyright 1994, University of Cambridge Computer Laboratory
388 * All Rights Reserved.
389 *
390 */
Jesper Juhl77933d72005-07-27 11:46:09 -0700391static inline s64 divremdi3(s64 x, s64 y, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800393 u64 a = (x < 0) ? -x : x;
394 u64 b = (y < 0) ? -y : y;
395 u64 res = 0, d = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Luiz Capitulino222f1802006-03-20 22:16:13 -0800397 if (b > 0) {
398 while (b < a) {
399 b <<= 1;
400 d <<= 1;
401 }
402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Luiz Capitulino222f1802006-03-20 22:16:13 -0800404 do {
405 if (a >= b) {
406 a -= b;
407 res += d;
408 }
409 b >>= 1;
410 d >>= 1;
411 }
412 while (d);
413
414 if (PG_DIV == type) {
415 return (((x ^ y) & (1ll << 63)) == 0) ? res : -(s64) res;
416 } else {
417 return ((x & (1ll << 63)) == 0) ? a : -(s64) a;
418 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419}
420
421/* End of hacks to deal with 64-bit math on x86 */
422
Stephen Hemmingerb4099fa2005-10-14 15:32:22 -0700423/** Convert to milliseconds */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800424static inline __u64 tv_to_ms(const struct timeval *tv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800426 __u64 ms = tv->tv_usec / 1000;
427 ms += (__u64) tv->tv_sec * (__u64) 1000;
428 return ms;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429}
430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431/** Convert to micro-seconds */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800432static inline __u64 tv_to_us(const struct timeval *tv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800434 __u64 us = tv->tv_usec;
435 us += (__u64) tv->tv_sec * (__u64) 1000000;
436 return us;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437}
438
Luiz Capitulino222f1802006-03-20 22:16:13 -0800439static inline __u64 pg_div(__u64 n, __u32 base)
440{
441 __u64 tmp = n;
442 do_div(tmp, base);
443 /* printk("pktgen: pg_div, n: %llu base: %d rv: %llu\n",
444 n, base, tmp); */
445 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446}
447
Luiz Capitulino222f1802006-03-20 22:16:13 -0800448static inline __u64 pg_div64(__u64 n, __u64 base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800450 __u64 tmp = n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451/*
Stephen Hemmingerb4099fa2005-10-14 15:32:22 -0700452 * How do we know if the architecture we are running on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 * supports division with 64 bit base?
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900454 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800456#if defined(__sparc_v9__) || defined(__powerpc64__) || defined(__alpha__) || defined(__x86_64__) || defined(__ia64__)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
Luiz Capitulino222f1802006-03-20 22:16:13 -0800458 do_div(tmp, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459#else
Luiz Capitulino222f1802006-03-20 22:16:13 -0800460 tmp = divremdi3(n, base, PG_DIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461#endif
Luiz Capitulino222f1802006-03-20 22:16:13 -0800462 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463}
464
Luiz Capitulino222f1802006-03-20 22:16:13 -0800465static inline __u64 getCurMs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800467 struct timeval tv;
468 do_gettimeofday(&tv);
469 return tv_to_ms(&tv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470}
471
Luiz Capitulino222f1802006-03-20 22:16:13 -0800472static inline __u64 getCurUs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800474 struct timeval tv;
475 do_gettimeofday(&tv);
476 return tv_to_us(&tv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477}
478
Luiz Capitulino222f1802006-03-20 22:16:13 -0800479static inline __u64 tv_diff(const struct timeval *a, const struct timeval *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800481 return tv_to_us(a) - tv_to_us(b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482}
483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484/* old include end */
485
486static char version[] __initdata = VERSION;
487
Luiz Capitulino222f1802006-03-20 22:16:13 -0800488static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *i);
489static int pktgen_add_device(struct pktgen_thread *t, const char *ifname);
490static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
491 const char *ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492static int pktgen_device_event(struct notifier_block *, unsigned long, void *);
493static void pktgen_run_all_threads(void);
494static void pktgen_stop_all_threads_ifs(void);
495static int pktgen_stop_device(struct pktgen_dev *pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800496static void pktgen_stop(struct pktgen_thread *t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497static void pktgen_clear_counters(struct pktgen_dev *pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -0800498
Luiz Capitulino222f1802006-03-20 22:16:13 -0800499static unsigned int scan_ip6(const char *s, char ip[16]);
500static unsigned int fmt_ip6(char *s, const char ip[16]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502/* Module parameters, defaults. */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800503static int pg_count_d = 1000; /* 1000 pkts by default */
Jaco Kroonf34fbb972005-12-22 12:51:46 -0800504static int pg_delay_d;
505static int pg_clone_skb_d;
506static int debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Luiz Capitulino222fa072006-03-20 22:24:27 -0800508static DEFINE_MUTEX(pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -0800509static LIST_HEAD(pktgen_threads);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511static struct notifier_block pktgen_notifier_block = {
512 .notifier_call = pktgen_device_event,
513};
514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515/*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900516 * /proc handling functions
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 *
518 */
519
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700520static int pgctrl_show(struct seq_file *seq, void *v)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800521{
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700522 seq_puts(seq, VERSION);
523 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524}
525
Luiz Capitulino222f1802006-03-20 22:16:13 -0800526static ssize_t pgctrl_write(struct file *file, const char __user * buf,
527 size_t count, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 int err = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700530 char data[128];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Luiz Capitulino222f1802006-03-20 22:16:13 -0800532 if (!capable(CAP_NET_ADMIN)) {
533 err = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 goto out;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800535 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700537 if (count > sizeof(data))
538 count = sizeof(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 if (copy_from_user(data, buf, count)) {
Stephen Hemmingerb4099fa2005-10-14 15:32:22 -0700541 err = -EFAULT;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700542 goto out;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800543 }
544 data[count - 1] = 0; /* Make string */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Luiz Capitulino222f1802006-03-20 22:16:13 -0800546 if (!strcmp(data, "stop"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 pktgen_stop_all_threads_ifs();
548
Luiz Capitulino222f1802006-03-20 22:16:13 -0800549 else if (!strcmp(data, "start"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 pktgen_run_all_threads();
551
Luiz Capitulino222f1802006-03-20 22:16:13 -0800552 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 printk("pktgen: Unknown command: %s\n", data);
554
555 err = count;
556
Luiz Capitulino222f1802006-03-20 22:16:13 -0800557out:
558 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559}
560
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700561static int pgctrl_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700563 return single_open(file, pgctrl_show, PDE(inode)->data);
564}
565
Arjan van de Ven9a321442007-02-12 00:55:35 -0800566static const struct file_operations pktgen_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800567 .owner = THIS_MODULE,
568 .open = pgctrl_open,
569 .read = seq_read,
570 .llseek = seq_lseek,
571 .write = pgctrl_write,
572 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700573};
574
575static int pktgen_if_show(struct seq_file *seq, void *v)
576{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 int i;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800578 struct pktgen_dev *pkt_dev = seq->private;
579 __u64 sa;
580 __u64 stopped;
581 __u64 now = getCurUs();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Luiz Capitulino222f1802006-03-20 22:16:13 -0800583 seq_printf(seq,
584 "Params: count %llu min_pkt_size: %u max_pkt_size: %u\n",
585 (unsigned long long)pkt_dev->count, pkt_dev->min_pkt_size,
586 pkt_dev->max_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Luiz Capitulino222f1802006-03-20 22:16:13 -0800588 seq_printf(seq,
589 " frags: %d delay: %u clone_skb: %d ifname: %s\n",
590 pkt_dev->nfrags,
591 1000 * pkt_dev->delay_us + pkt_dev->delay_ns,
Stephen Hemminger39df2322007-03-04 16:11:51 -0800592 pkt_dev->clone_skb, pkt_dev->odev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Luiz Capitulino222f1802006-03-20 22:16:13 -0800594 seq_printf(seq, " flows: %u flowlen: %u\n", pkt_dev->cflows,
595 pkt_dev->lflow);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Luiz Capitulino222f1802006-03-20 22:16:13 -0800597 if (pkt_dev->flags & F_IPV6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 char b1[128], b2[128], b3[128];
Luiz Capitulino222f1802006-03-20 22:16:13 -0800599 fmt_ip6(b1, pkt_dev->in6_saddr.s6_addr);
600 fmt_ip6(b2, pkt_dev->min_in6_saddr.s6_addr);
601 fmt_ip6(b3, pkt_dev->max_in6_saddr.s6_addr);
602 seq_printf(seq,
603 " saddr: %s min_saddr: %s max_saddr: %s\n", b1,
604 b2, b3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Luiz Capitulino222f1802006-03-20 22:16:13 -0800606 fmt_ip6(b1, pkt_dev->in6_daddr.s6_addr);
607 fmt_ip6(b2, pkt_dev->min_in6_daddr.s6_addr);
608 fmt_ip6(b3, pkt_dev->max_in6_daddr.s6_addr);
609 seq_printf(seq,
610 " daddr: %s min_daddr: %s max_daddr: %s\n", b1,
611 b2, b3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Luiz Capitulino222f1802006-03-20 22:16:13 -0800613 } else
614 seq_printf(seq,
615 " dst_min: %s dst_max: %s\n src_min: %s src_max: %s\n",
616 pkt_dev->dst_min, pkt_dev->dst_max, pkt_dev->src_min,
617 pkt_dev->src_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700619 seq_puts(seq, " src_mac: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Kris Katterjohnf404e9a2006-01-17 13:04:57 -0800621 if (is_zero_ether_addr(pkt_dev->src_mac))
Luiz Capitulino222f1802006-03-20 22:16:13 -0800622 for (i = 0; i < 6; i++)
623 seq_printf(seq, "%02X%s", pkt_dev->odev->dev_addr[i],
624 i == 5 ? " " : ":");
625 else
626 for (i = 0; i < 6; i++)
627 seq_printf(seq, "%02X%s", pkt_dev->src_mac[i],
628 i == 5 ? " " : ":");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Luiz Capitulino222f1802006-03-20 22:16:13 -0800630 seq_printf(seq, "dst_mac: ");
631 for (i = 0; i < 6; i++)
632 seq_printf(seq, "%02X%s", pkt_dev->dst_mac[i],
633 i == 5 ? "\n" : ":");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Luiz Capitulino222f1802006-03-20 22:16:13 -0800635 seq_printf(seq,
636 " udp_src_min: %d udp_src_max: %d udp_dst_min: %d udp_dst_max: %d\n",
637 pkt_dev->udp_src_min, pkt_dev->udp_src_max,
638 pkt_dev->udp_dst_min, pkt_dev->udp_dst_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Luiz Capitulino222f1802006-03-20 22:16:13 -0800640 seq_printf(seq,
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800641 " src_mac_count: %d dst_mac_count: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700642 pkt_dev->src_mac_count, pkt_dev->dst_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800644 if (pkt_dev->nr_labels) {
645 unsigned i;
646 seq_printf(seq, " mpls: ");
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700647 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800648 seq_printf(seq, "%08x%s", ntohl(pkt_dev->labels[i]),
649 i == pkt_dev->nr_labels-1 ? "\n" : ", ");
650 }
651
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700652 if (pkt_dev->vlan_id != 0xffff) {
653 seq_printf(seq, " vlan_id: %u vlan_p: %u vlan_cfi: %u\n",
654 pkt_dev->vlan_id, pkt_dev->vlan_p, pkt_dev->vlan_cfi);
655 }
656
657 if (pkt_dev->svlan_id != 0xffff) {
658 seq_printf(seq, " svlan_id: %u vlan_p: %u vlan_cfi: %u\n",
659 pkt_dev->svlan_id, pkt_dev->svlan_p, pkt_dev->svlan_cfi);
660 }
661
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700662 if (pkt_dev->tos) {
663 seq_printf(seq, " tos: 0x%02x\n", pkt_dev->tos);
664 }
665
666 if (pkt_dev->traffic_class) {
667 seq_printf(seq, " traffic_class: 0x%02x\n", pkt_dev->traffic_class);
668 }
669
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800670 seq_printf(seq, " Flags: ");
671
Luiz Capitulino222f1802006-03-20 22:16:13 -0800672 if (pkt_dev->flags & F_IPV6)
673 seq_printf(seq, "IPV6 ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Luiz Capitulino222f1802006-03-20 22:16:13 -0800675 if (pkt_dev->flags & F_IPSRC_RND)
676 seq_printf(seq, "IPSRC_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Luiz Capitulino222f1802006-03-20 22:16:13 -0800678 if (pkt_dev->flags & F_IPDST_RND)
679 seq_printf(seq, "IPDST_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Luiz Capitulino222f1802006-03-20 22:16:13 -0800681 if (pkt_dev->flags & F_TXSIZE_RND)
682 seq_printf(seq, "TXSIZE_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Luiz Capitulino222f1802006-03-20 22:16:13 -0800684 if (pkt_dev->flags & F_UDPSRC_RND)
685 seq_printf(seq, "UDPSRC_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Luiz Capitulino222f1802006-03-20 22:16:13 -0800687 if (pkt_dev->flags & F_UDPDST_RND)
688 seq_printf(seq, "UDPDST_RND ");
689
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800690 if (pkt_dev->flags & F_MPLS_RND)
691 seq_printf(seq, "MPLS_RND ");
692
Luiz Capitulino222f1802006-03-20 22:16:13 -0800693 if (pkt_dev->flags & F_MACSRC_RND)
694 seq_printf(seq, "MACSRC_RND ");
695
696 if (pkt_dev->flags & F_MACDST_RND)
697 seq_printf(seq, "MACDST_RND ");
698
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700699 if (pkt_dev->flags & F_VID_RND)
700 seq_printf(seq, "VID_RND ");
701
702 if (pkt_dev->flags & F_SVID_RND)
703 seq_printf(seq, "SVID_RND ");
704
Luiz Capitulino222f1802006-03-20 22:16:13 -0800705 seq_puts(seq, "\n");
706
707 sa = pkt_dev->started_at;
708 stopped = pkt_dev->stopped_at;
709 if (pkt_dev->running)
710 stopped = now; /* not really stopped, more like last-running-at */
711
712 seq_printf(seq,
713 "Current:\n pkts-sofar: %llu errors: %llu\n started: %lluus stopped: %lluus idle: %lluus\n",
714 (unsigned long long)pkt_dev->sofar,
715 (unsigned long long)pkt_dev->errors, (unsigned long long)sa,
716 (unsigned long long)stopped,
717 (unsigned long long)pkt_dev->idle_acc);
718
719 seq_printf(seq,
720 " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700721 pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset,
722 pkt_dev->cur_src_mac_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Luiz Capitulino222f1802006-03-20 22:16:13 -0800724 if (pkt_dev->flags & F_IPV6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 char b1[128], b2[128];
Luiz Capitulino222f1802006-03-20 22:16:13 -0800726 fmt_ip6(b1, pkt_dev->cur_in6_daddr.s6_addr);
727 fmt_ip6(b2, pkt_dev->cur_in6_saddr.s6_addr);
728 seq_printf(seq, " cur_saddr: %s cur_daddr: %s\n", b2, b1);
729 } else
730 seq_printf(seq, " cur_saddr: 0x%x cur_daddr: 0x%x\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700731 pkt_dev->cur_saddr, pkt_dev->cur_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Luiz Capitulino222f1802006-03-20 22:16:13 -0800733 seq_printf(seq, " cur_udp_dst: %d cur_udp_src: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700734 pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Luiz Capitulino222f1802006-03-20 22:16:13 -0800736 seq_printf(seq, " flows: %u\n", pkt_dev->nflows);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
738 if (pkt_dev->result[0])
Luiz Capitulino222f1802006-03-20 22:16:13 -0800739 seq_printf(seq, "Result: %s\n", pkt_dev->result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 else
Luiz Capitulino222f1802006-03-20 22:16:13 -0800741 seq_printf(seq, "Result: Idle\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700743 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744}
745
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800746
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700747static int hex32_arg(const char __user *user_buffer, unsigned long maxlen, __u32 *num)
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800748{
749 int i = 0;
750 *num = 0;
751
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700752 for (; i < maxlen; i++) {
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800753 char c;
754 *num <<= 4;
755 if (get_user(c, &user_buffer[i]))
756 return -EFAULT;
757 if ((c >= '0') && (c <= '9'))
758 *num |= c - '0';
759 else if ((c >= 'a') && (c <= 'f'))
760 *num |= c - 'a' + 10;
761 else if ((c >= 'A') && (c <= 'F'))
762 *num |= c - 'A' + 10;
763 else
764 break;
765 }
766 return i;
767}
768
Luiz Capitulino222f1802006-03-20 22:16:13 -0800769static int count_trail_chars(const char __user * user_buffer,
770 unsigned int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
772 int i;
773
774 for (i = 0; i < maxlen; i++) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800775 char c;
776 if (get_user(c, &user_buffer[i]))
777 return -EFAULT;
778 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 case '\"':
780 case '\n':
781 case '\r':
782 case '\t':
783 case ' ':
784 case '=':
785 break;
786 default:
787 goto done;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700788 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 }
790done:
791 return i;
792}
793
Luiz Capitulino222f1802006-03-20 22:16:13 -0800794static unsigned long num_arg(const char __user * user_buffer,
795 unsigned long maxlen, unsigned long *num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796{
797 int i = 0;
798 *num = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800799
800 for (; i < maxlen; i++) {
801 char c;
802 if (get_user(c, &user_buffer[i]))
803 return -EFAULT;
804 if ((c >= '0') && (c <= '9')) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 *num *= 10;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800806 *num += c - '0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 } else
808 break;
809 }
810 return i;
811}
812
Luiz Capitulino222f1802006-03-20 22:16:13 -0800813static int strn_len(const char __user * user_buffer, unsigned int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814{
815 int i = 0;
816
Luiz Capitulino222f1802006-03-20 22:16:13 -0800817 for (; i < maxlen; i++) {
818 char c;
819 if (get_user(c, &user_buffer[i]))
820 return -EFAULT;
821 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 case '\"':
823 case '\n':
824 case '\r':
825 case '\t':
826 case ' ':
827 goto done_str;
828 break;
829 default:
830 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700831 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 }
833done_str:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 return i;
835}
836
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800837static ssize_t get_labels(const char __user *buffer, struct pktgen_dev *pkt_dev)
838{
839 unsigned n = 0;
840 char c;
841 ssize_t i = 0;
842 int len;
843
844 pkt_dev->nr_labels = 0;
845 do {
846 __u32 tmp;
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700847 len = hex32_arg(&buffer[i], 8, &tmp);
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800848 if (len <= 0)
849 return len;
850 pkt_dev->labels[n] = htonl(tmp);
851 if (pkt_dev->labels[n] & MPLS_STACK_BOTTOM)
852 pkt_dev->flags |= F_MPLS_RND;
853 i += len;
854 if (get_user(c, &buffer[i]))
855 return -EFAULT;
856 i++;
857 n++;
858 if (n >= MAX_MPLS_LABELS)
859 return -E2BIG;
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700860 } while (c == ',');
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800861
862 pkt_dev->nr_labels = n;
863 return i;
864}
865
Luiz Capitulino222f1802006-03-20 22:16:13 -0800866static ssize_t pktgen_if_write(struct file *file,
867 const char __user * user_buffer, size_t count,
868 loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800870 struct seq_file *seq = (struct seq_file *)file->private_data;
871 struct pktgen_dev *pkt_dev = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 int i = 0, max, len;
873 char name[16], valstr[32];
874 unsigned long value = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800875 char *pg_result = NULL;
876 int tmp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 char buf[128];
Luiz Capitulino222f1802006-03-20 22:16:13 -0800878
879 pg_result = &(pkt_dev->result[0]);
880
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 if (count < 1) {
882 printk("pktgen: wrong command format\n");
883 return -EINVAL;
884 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800885
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 max = count - i;
887 tmp = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800888 if (tmp < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 printk("pktgen: illegal format\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -0800890 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800892 i += tmp;
893
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 /* Read variable name */
895
896 len = strn_len(&user_buffer[i], sizeof(name) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800897 if (len < 0) {
898 return len;
899 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 memset(name, 0, sizeof(name));
Luiz Capitulino222f1802006-03-20 22:16:13 -0800901 if (copy_from_user(name, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 return -EFAULT;
903 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800904
905 max = count - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 len = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800907 if (len < 0)
908 return len;
909
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 i += len;
911
912 if (debug) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800913 char tb[count + 1];
914 if (copy_from_user(tb, user_buffer, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800916 tb[count] = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700917 printk("pktgen: %s,%lu buffer -:%s:-\n", name,
Luiz Capitulino222f1802006-03-20 22:16:13 -0800918 (unsigned long)count, tb);
919 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
921 if (!strcmp(name, "min_pkt_size")) {
922 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800923 if (len < 0) {
924 return len;
925 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800927 if (value < 14 + 20 + 8)
928 value = 14 + 20 + 8;
929 if (value != pkt_dev->min_pkt_size) {
930 pkt_dev->min_pkt_size = value;
931 pkt_dev->cur_pkt_size = value;
932 }
933 sprintf(pg_result, "OK: min_pkt_size=%u",
934 pkt_dev->min_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 return count;
936 }
937
Luiz Capitulino222f1802006-03-20 22:16:13 -0800938 if (!strcmp(name, "max_pkt_size")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800940 if (len < 0) {
941 return len;
942 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800944 if (value < 14 + 20 + 8)
945 value = 14 + 20 + 8;
946 if (value != pkt_dev->max_pkt_size) {
947 pkt_dev->max_pkt_size = value;
948 pkt_dev->cur_pkt_size = value;
949 }
950 sprintf(pg_result, "OK: max_pkt_size=%u",
951 pkt_dev->max_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 return count;
953 }
954
Luiz Capitulino222f1802006-03-20 22:16:13 -0800955 /* Shortcut for min = max */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
957 if (!strcmp(name, "pkt_size")) {
958 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800959 if (len < 0) {
960 return len;
961 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800963 if (value < 14 + 20 + 8)
964 value = 14 + 20 + 8;
965 if (value != pkt_dev->min_pkt_size) {
966 pkt_dev->min_pkt_size = value;
967 pkt_dev->max_pkt_size = value;
968 pkt_dev->cur_pkt_size = value;
969 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 sprintf(pg_result, "OK: pkt_size=%u", pkt_dev->min_pkt_size);
971 return count;
972 }
973
Luiz Capitulino222f1802006-03-20 22:16:13 -0800974 if (!strcmp(name, "debug")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800976 if (len < 0) {
977 return len;
978 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800980 debug = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 sprintf(pg_result, "OK: debug=%u", debug);
982 return count;
983 }
984
Luiz Capitulino222f1802006-03-20 22:16:13 -0800985 if (!strcmp(name, "frags")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800987 if (len < 0) {
988 return len;
989 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 i += len;
991 pkt_dev->nfrags = value;
992 sprintf(pg_result, "OK: frags=%u", pkt_dev->nfrags);
993 return count;
994 }
995 if (!strcmp(name, "delay")) {
996 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800997 if (len < 0) {
998 return len;
999 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 i += len;
1001 if (value == 0x7FFFFFFF) {
1002 pkt_dev->delay_us = 0x7FFFFFFF;
1003 pkt_dev->delay_ns = 0;
1004 } else {
1005 pkt_dev->delay_us = value / 1000;
1006 pkt_dev->delay_ns = value % 1000;
1007 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001008 sprintf(pg_result, "OK: delay=%u",
1009 1000 * pkt_dev->delay_us + pkt_dev->delay_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 return count;
1011 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001012 if (!strcmp(name, "udp_src_min")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001014 if (len < 0) {
1015 return len;
1016 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001018 if (value != pkt_dev->udp_src_min) {
1019 pkt_dev->udp_src_min = value;
1020 pkt_dev->cur_udp_src = value;
1021 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min);
1023 return count;
1024 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001025 if (!strcmp(name, "udp_dst_min")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001027 if (len < 0) {
1028 return len;
1029 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001031 if (value != pkt_dev->udp_dst_min) {
1032 pkt_dev->udp_dst_min = value;
1033 pkt_dev->cur_udp_dst = value;
1034 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min);
1036 return count;
1037 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001038 if (!strcmp(name, "udp_src_max")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001040 if (len < 0) {
1041 return len;
1042 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001044 if (value != pkt_dev->udp_src_max) {
1045 pkt_dev->udp_src_max = value;
1046 pkt_dev->cur_udp_src = value;
1047 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max);
1049 return count;
1050 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001051 if (!strcmp(name, "udp_dst_max")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001053 if (len < 0) {
1054 return len;
1055 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001057 if (value != pkt_dev->udp_dst_max) {
1058 pkt_dev->udp_dst_max = value;
1059 pkt_dev->cur_udp_dst = value;
1060 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max);
1062 return count;
1063 }
1064 if (!strcmp(name, "clone_skb")) {
1065 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001066 if (len < 0) {
1067 return len;
1068 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001070 pkt_dev->clone_skb = value;
1071
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb);
1073 return count;
1074 }
1075 if (!strcmp(name, "count")) {
1076 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001077 if (len < 0) {
1078 return len;
1079 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 i += len;
1081 pkt_dev->count = value;
1082 sprintf(pg_result, "OK: count=%llu",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001083 (unsigned long long)pkt_dev->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 return count;
1085 }
1086 if (!strcmp(name, "src_mac_count")) {
1087 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001088 if (len < 0) {
1089 return len;
1090 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 i += len;
1092 if (pkt_dev->src_mac_count != value) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001093 pkt_dev->src_mac_count = value;
1094 pkt_dev->cur_src_mac_offset = 0;
1095 }
1096 sprintf(pg_result, "OK: src_mac_count=%d",
1097 pkt_dev->src_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 return count;
1099 }
1100 if (!strcmp(name, "dst_mac_count")) {
1101 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001102 if (len < 0) {
1103 return len;
1104 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 i += len;
1106 if (pkt_dev->dst_mac_count != value) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001107 pkt_dev->dst_mac_count = value;
1108 pkt_dev->cur_dst_mac_offset = 0;
1109 }
1110 sprintf(pg_result, "OK: dst_mac_count=%d",
1111 pkt_dev->dst_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 return count;
1113 }
1114 if (!strcmp(name, "flag")) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001115 char f[32];
1116 memset(f, 0, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 len = strn_len(&user_buffer[i], sizeof(f) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001118 if (len < 0) {
1119 return len;
1120 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 if (copy_from_user(f, &user_buffer[i], len))
1122 return -EFAULT;
1123 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001124 if (strcmp(f, "IPSRC_RND") == 0)
1125 pkt_dev->flags |= F_IPSRC_RND;
1126
1127 else if (strcmp(f, "!IPSRC_RND") == 0)
1128 pkt_dev->flags &= ~F_IPSRC_RND;
1129
1130 else if (strcmp(f, "TXSIZE_RND") == 0)
1131 pkt_dev->flags |= F_TXSIZE_RND;
1132
1133 else if (strcmp(f, "!TXSIZE_RND") == 0)
1134 pkt_dev->flags &= ~F_TXSIZE_RND;
1135
1136 else if (strcmp(f, "IPDST_RND") == 0)
1137 pkt_dev->flags |= F_IPDST_RND;
1138
1139 else if (strcmp(f, "!IPDST_RND") == 0)
1140 pkt_dev->flags &= ~F_IPDST_RND;
1141
1142 else if (strcmp(f, "UDPSRC_RND") == 0)
1143 pkt_dev->flags |= F_UDPSRC_RND;
1144
1145 else if (strcmp(f, "!UDPSRC_RND") == 0)
1146 pkt_dev->flags &= ~F_UDPSRC_RND;
1147
1148 else if (strcmp(f, "UDPDST_RND") == 0)
1149 pkt_dev->flags |= F_UDPDST_RND;
1150
1151 else if (strcmp(f, "!UDPDST_RND") == 0)
1152 pkt_dev->flags &= ~F_UDPDST_RND;
1153
1154 else if (strcmp(f, "MACSRC_RND") == 0)
1155 pkt_dev->flags |= F_MACSRC_RND;
1156
1157 else if (strcmp(f, "!MACSRC_RND") == 0)
1158 pkt_dev->flags &= ~F_MACSRC_RND;
1159
1160 else if (strcmp(f, "MACDST_RND") == 0)
1161 pkt_dev->flags |= F_MACDST_RND;
1162
1163 else if (strcmp(f, "!MACDST_RND") == 0)
1164 pkt_dev->flags &= ~F_MACDST_RND;
1165
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001166 else if (strcmp(f, "MPLS_RND") == 0)
1167 pkt_dev->flags |= F_MPLS_RND;
1168
1169 else if (strcmp(f, "!MPLS_RND") == 0)
1170 pkt_dev->flags &= ~F_MPLS_RND;
1171
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001172 else if (strcmp(f, "VID_RND") == 0)
1173 pkt_dev->flags |= F_VID_RND;
1174
1175 else if (strcmp(f, "!VID_RND") == 0)
1176 pkt_dev->flags &= ~F_VID_RND;
1177
1178 else if (strcmp(f, "SVID_RND") == 0)
1179 pkt_dev->flags |= F_SVID_RND;
1180
1181 else if (strcmp(f, "!SVID_RND") == 0)
1182 pkt_dev->flags &= ~F_SVID_RND;
1183
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001184 else if (strcmp(f, "!IPV6") == 0)
1185 pkt_dev->flags &= ~F_IPV6;
1186
Luiz Capitulino222f1802006-03-20 22:16:13 -08001187 else {
1188 sprintf(pg_result,
1189 "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
1190 f,
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001191 "IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
1192 "MACSRC_RND, MACDST_RND, TXSIZE_RND, IPV6, MPLS_RND, VID_RND, SVID_RND\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08001193 return count;
1194 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
1196 return count;
1197 }
1198 if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
1199 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001200 if (len < 0) {
1201 return len;
1202 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
Luiz Capitulino222f1802006-03-20 22:16:13 -08001204 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001206 buf[len] = 0;
1207 if (strcmp(buf, pkt_dev->dst_min) != 0) {
1208 memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
1209 strncpy(pkt_dev->dst_min, buf, len);
1210 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
1211 pkt_dev->cur_daddr = pkt_dev->daddr_min;
1212 }
1213 if (debug)
1214 printk("pktgen: dst_min set to: %s\n",
1215 pkt_dev->dst_min);
1216 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min);
1218 return count;
1219 }
1220 if (!strcmp(name, "dst_max")) {
1221 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001222 if (len < 0) {
1223 return len;
1224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
Luiz Capitulino222f1802006-03-20 22:16:13 -08001226 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 return -EFAULT;
1228
Luiz Capitulino222f1802006-03-20 22:16:13 -08001229 buf[len] = 0;
1230 if (strcmp(buf, pkt_dev->dst_max) != 0) {
1231 memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
1232 strncpy(pkt_dev->dst_max, buf, len);
1233 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
1234 pkt_dev->cur_daddr = pkt_dev->daddr_max;
1235 }
1236 if (debug)
1237 printk("pktgen: dst_max set to: %s\n",
1238 pkt_dev->dst_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 i += len;
1240 sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max);
1241 return count;
1242 }
1243 if (!strcmp(name, "dst6")) {
1244 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001245 if (len < 0)
1246 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
1248 pkt_dev->flags |= F_IPV6;
1249
Luiz Capitulino222f1802006-03-20 22:16:13 -08001250 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001252 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
1254 scan_ip6(buf, pkt_dev->in6_daddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001255 fmt_ip6(buf, pkt_dev->in6_daddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
1257 ipv6_addr_copy(&pkt_dev->cur_in6_daddr, &pkt_dev->in6_daddr);
1258
Luiz Capitulino222f1802006-03-20 22:16:13 -08001259 if (debug)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 printk("pktgen: dst6 set to: %s\n", buf);
1261
Luiz Capitulino222f1802006-03-20 22:16:13 -08001262 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 sprintf(pg_result, "OK: dst6=%s", buf);
1264 return count;
1265 }
1266 if (!strcmp(name, "dst6_min")) {
1267 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001268 if (len < 0)
1269 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
1271 pkt_dev->flags |= F_IPV6;
1272
Luiz Capitulino222f1802006-03-20 22:16:13 -08001273 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001275 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
1277 scan_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001278 fmt_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
Luiz Capitulino222f1802006-03-20 22:16:13 -08001280 ipv6_addr_copy(&pkt_dev->cur_in6_daddr,
1281 &pkt_dev->min_in6_daddr);
1282 if (debug)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 printk("pktgen: dst6_min set to: %s\n", buf);
1284
Luiz Capitulino222f1802006-03-20 22:16:13 -08001285 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 sprintf(pg_result, "OK: dst6_min=%s", buf);
1287 return count;
1288 }
1289 if (!strcmp(name, "dst6_max")) {
1290 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001291 if (len < 0)
1292 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
1294 pkt_dev->flags |= F_IPV6;
1295
Luiz Capitulino222f1802006-03-20 22:16:13 -08001296 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001298 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
1300 scan_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001301 fmt_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
Luiz Capitulino222f1802006-03-20 22:16:13 -08001303 if (debug)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 printk("pktgen: dst6_max set to: %s\n", buf);
1305
Luiz Capitulino222f1802006-03-20 22:16:13 -08001306 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 sprintf(pg_result, "OK: dst6_max=%s", buf);
1308 return count;
1309 }
1310 if (!strcmp(name, "src6")) {
1311 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001312 if (len < 0)
1313 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
1315 pkt_dev->flags |= F_IPV6;
1316
Luiz Capitulino222f1802006-03-20 22:16:13 -08001317 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001319 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
1321 scan_ip6(buf, pkt_dev->in6_saddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001322 fmt_ip6(buf, pkt_dev->in6_saddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
1324 ipv6_addr_copy(&pkt_dev->cur_in6_saddr, &pkt_dev->in6_saddr);
1325
Luiz Capitulino222f1802006-03-20 22:16:13 -08001326 if (debug)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 printk("pktgen: src6 set to: %s\n", buf);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001328
1329 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 sprintf(pg_result, "OK: src6=%s", buf);
1331 return count;
1332 }
1333 if (!strcmp(name, "src_min")) {
1334 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001335 if (len < 0) {
1336 return len;
1337 }
1338 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001340 buf[len] = 0;
1341 if (strcmp(buf, pkt_dev->src_min) != 0) {
1342 memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
1343 strncpy(pkt_dev->src_min, buf, len);
1344 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
1345 pkt_dev->cur_saddr = pkt_dev->saddr_min;
1346 }
1347 if (debug)
1348 printk("pktgen: src_min set to: %s\n",
1349 pkt_dev->src_min);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 i += len;
1351 sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min);
1352 return count;
1353 }
1354 if (!strcmp(name, "src_max")) {
1355 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001356 if (len < 0) {
1357 return len;
1358 }
1359 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001361 buf[len] = 0;
1362 if (strcmp(buf, pkt_dev->src_max) != 0) {
1363 memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
1364 strncpy(pkt_dev->src_max, buf, len);
1365 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
1366 pkt_dev->cur_saddr = pkt_dev->saddr_max;
1367 }
1368 if (debug)
1369 printk("pktgen: src_max set to: %s\n",
1370 pkt_dev->src_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 i += len;
1372 sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max);
1373 return count;
1374 }
1375 if (!strcmp(name, "dst_mac")) {
1376 char *v = valstr;
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001377 unsigned char old_dmac[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 unsigned char *m = pkt_dev->dst_mac;
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001379 memcpy(old_dmac, pkt_dev->dst_mac, ETH_ALEN);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001380
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001382 if (len < 0) {
1383 return len;
1384 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 memset(valstr, 0, sizeof(valstr));
Luiz Capitulino222f1802006-03-20 22:16:13 -08001386 if (copy_from_user(valstr, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 return -EFAULT;
1388 i += len;
1389
Luiz Capitulino222f1802006-03-20 22:16:13 -08001390 for (*m = 0; *v && m < pkt_dev->dst_mac + 6; v++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 if (*v >= '0' && *v <= '9') {
1392 *m *= 16;
1393 *m += *v - '0';
1394 }
1395 if (*v >= 'A' && *v <= 'F') {
1396 *m *= 16;
1397 *m += *v - 'A' + 10;
1398 }
1399 if (*v >= 'a' && *v <= 'f') {
1400 *m *= 16;
1401 *m += *v - 'a' + 10;
1402 }
1403 if (*v == ':') {
1404 m++;
1405 *m = 0;
1406 }
1407 }
1408
1409 /* Set up Dest MAC */
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001410 if (compare_ether_addr(old_dmac, pkt_dev->dst_mac))
1411 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001412
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 sprintf(pg_result, "OK: dstmac");
1414 return count;
1415 }
1416 if (!strcmp(name, "src_mac")) {
1417 char *v = valstr;
1418 unsigned char *m = pkt_dev->src_mac;
1419
1420 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001421 if (len < 0) {
1422 return len;
1423 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 memset(valstr, 0, sizeof(valstr));
Luiz Capitulino222f1802006-03-20 22:16:13 -08001425 if (copy_from_user(valstr, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 return -EFAULT;
1427 i += len;
1428
Luiz Capitulino222f1802006-03-20 22:16:13 -08001429 for (*m = 0; *v && m < pkt_dev->src_mac + 6; v++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 if (*v >= '0' && *v <= '9') {
1431 *m *= 16;
1432 *m += *v - '0';
1433 }
1434 if (*v >= 'A' && *v <= 'F') {
1435 *m *= 16;
1436 *m += *v - 'A' + 10;
1437 }
1438 if (*v >= 'a' && *v <= 'f') {
1439 *m *= 16;
1440 *m += *v - 'a' + 10;
1441 }
1442 if (*v == ':') {
1443 m++;
1444 *m = 0;
1445 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001446 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
Luiz Capitulino222f1802006-03-20 22:16:13 -08001448 sprintf(pg_result, "OK: srcmac");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 return count;
1450 }
1451
Luiz Capitulino222f1802006-03-20 22:16:13 -08001452 if (!strcmp(name, "clear_counters")) {
1453 pktgen_clear_counters(pkt_dev);
1454 sprintf(pg_result, "OK: Clearing counters.\n");
1455 return count;
1456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
1458 if (!strcmp(name, "flows")) {
1459 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001460 if (len < 0) {
1461 return len;
1462 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 i += len;
1464 if (value > MAX_CFLOWS)
1465 value = MAX_CFLOWS;
1466
1467 pkt_dev->cflows = value;
1468 sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
1469 return count;
1470 }
1471
1472 if (!strcmp(name, "flowlen")) {
1473 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001474 if (len < 0) {
1475 return len;
1476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 i += len;
1478 pkt_dev->lflow = value;
1479 sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
1480 return count;
1481 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001482
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001483 if (!strcmp(name, "mpls")) {
1484 unsigned n, offset;
1485 len = get_labels(&user_buffer[i], pkt_dev);
1486 if (len < 0) { return len; }
1487 i += len;
1488 offset = sprintf(pg_result, "OK: mpls=");
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001489 for (n = 0; n < pkt_dev->nr_labels; n++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001490 offset += sprintf(pg_result + offset,
1491 "%08x%s", ntohl(pkt_dev->labels[n]),
1492 n == pkt_dev->nr_labels-1 ? "" : ",");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001493
1494 if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) {
1495 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1496 pkt_dev->svlan_id = 0xffff;
1497
1498 if (debug)
1499 printk("pktgen: VLAN/SVLAN auto turned off\n");
1500 }
1501 return count;
1502 }
1503
1504 if (!strcmp(name, "vlan_id")) {
1505 len = num_arg(&user_buffer[i], 4, &value);
1506 if (len < 0) {
1507 return len;
1508 }
1509 i += len;
1510 if (value <= 4095) {
1511 pkt_dev->vlan_id = value; /* turn on VLAN */
1512
1513 if (debug)
1514 printk("pktgen: VLAN turned on\n");
1515
1516 if (debug && pkt_dev->nr_labels)
1517 printk("pktgen: MPLS auto turned off\n");
1518
1519 pkt_dev->nr_labels = 0; /* turn off MPLS */
1520 sprintf(pg_result, "OK: vlan_id=%u", pkt_dev->vlan_id);
1521 } else {
1522 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1523 pkt_dev->svlan_id = 0xffff;
1524
1525 if (debug)
1526 printk("pktgen: VLAN/SVLAN turned off\n");
1527 }
1528 return count;
1529 }
1530
1531 if (!strcmp(name, "vlan_p")) {
1532 len = num_arg(&user_buffer[i], 1, &value);
1533 if (len < 0) {
1534 return len;
1535 }
1536 i += len;
1537 if ((value <= 7) && (pkt_dev->vlan_id != 0xffff)) {
1538 pkt_dev->vlan_p = value;
1539 sprintf(pg_result, "OK: vlan_p=%u", pkt_dev->vlan_p);
1540 } else {
1541 sprintf(pg_result, "ERROR: vlan_p must be 0-7");
1542 }
1543 return count;
1544 }
1545
1546 if (!strcmp(name, "vlan_cfi")) {
1547 len = num_arg(&user_buffer[i], 1, &value);
1548 if (len < 0) {
1549 return len;
1550 }
1551 i += len;
1552 if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) {
1553 pkt_dev->vlan_cfi = value;
1554 sprintf(pg_result, "OK: vlan_cfi=%u", pkt_dev->vlan_cfi);
1555 } else {
1556 sprintf(pg_result, "ERROR: vlan_cfi must be 0-1");
1557 }
1558 return count;
1559 }
1560
1561 if (!strcmp(name, "svlan_id")) {
1562 len = num_arg(&user_buffer[i], 4, &value);
1563 if (len < 0) {
1564 return len;
1565 }
1566 i += len;
1567 if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) {
1568 pkt_dev->svlan_id = value; /* turn on SVLAN */
1569
1570 if (debug)
1571 printk("pktgen: SVLAN turned on\n");
1572
1573 if (debug && pkt_dev->nr_labels)
1574 printk("pktgen: MPLS auto turned off\n");
1575
1576 pkt_dev->nr_labels = 0; /* turn off MPLS */
1577 sprintf(pg_result, "OK: svlan_id=%u", pkt_dev->svlan_id);
1578 } else {
1579 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1580 pkt_dev->svlan_id = 0xffff;
1581
1582 if (debug)
1583 printk("pktgen: VLAN/SVLAN turned off\n");
1584 }
1585 return count;
1586 }
1587
1588 if (!strcmp(name, "svlan_p")) {
1589 len = num_arg(&user_buffer[i], 1, &value);
1590 if (len < 0) {
1591 return len;
1592 }
1593 i += len;
1594 if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) {
1595 pkt_dev->svlan_p = value;
1596 sprintf(pg_result, "OK: svlan_p=%u", pkt_dev->svlan_p);
1597 } else {
1598 sprintf(pg_result, "ERROR: svlan_p must be 0-7");
1599 }
1600 return count;
1601 }
1602
1603 if (!strcmp(name, "svlan_cfi")) {
1604 len = num_arg(&user_buffer[i], 1, &value);
1605 if (len < 0) {
1606 return len;
1607 }
1608 i += len;
1609 if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) {
1610 pkt_dev->svlan_cfi = value;
1611 sprintf(pg_result, "OK: svlan_cfi=%u", pkt_dev->svlan_cfi);
1612 } else {
1613 sprintf(pg_result, "ERROR: svlan_cfi must be 0-1");
1614 }
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001615 return count;
1616 }
1617
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001618 if (!strcmp(name, "tos")) {
1619 __u32 tmp_value = 0;
1620 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1621 if (len < 0) {
1622 return len;
1623 }
1624 i += len;
1625 if (len == 2) {
1626 pkt_dev->tos = tmp_value;
1627 sprintf(pg_result, "OK: tos=0x%02x", pkt_dev->tos);
1628 } else {
1629 sprintf(pg_result, "ERROR: tos must be 00-ff");
1630 }
1631 return count;
1632 }
1633
1634 if (!strcmp(name, "traffic_class")) {
1635 __u32 tmp_value = 0;
1636 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1637 if (len < 0) {
1638 return len;
1639 }
1640 i += len;
1641 if (len == 2) {
1642 pkt_dev->traffic_class = tmp_value;
1643 sprintf(pg_result, "OK: traffic_class=0x%02x", pkt_dev->traffic_class);
1644 } else {
1645 sprintf(pg_result, "ERROR: traffic_class must be 00-ff");
1646 }
1647 return count;
1648 }
1649
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
1651 return -EINVAL;
1652}
1653
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001654static int pktgen_if_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655{
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001656 return single_open(file, pktgen_if_show, PDE(inode)->data);
1657}
1658
Arjan van de Ven9a321442007-02-12 00:55:35 -08001659static const struct file_operations pktgen_if_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001660 .owner = THIS_MODULE,
1661 .open = pktgen_if_open,
1662 .read = seq_read,
1663 .llseek = seq_lseek,
1664 .write = pktgen_if_write,
1665 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001666};
1667
1668static int pktgen_thread_show(struct seq_file *seq, void *v)
1669{
Luiz Capitulino222f1802006-03-20 22:16:13 -08001670 struct pktgen_thread *t = seq->private;
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08001671 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001673 BUG_ON(!t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001675 seq_printf(seq, "Name: %s max_before_softirq: %d\n",
David S. Milleree74baa2007-01-01 20:51:53 -08001676 t->tsk->comm, t->max_before_softirq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
Luiz Capitulino222f1802006-03-20 22:16:13 -08001678 seq_printf(seq, "Running: ");
1679
1680 if_lock(t);
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08001681 list_for_each_entry(pkt_dev, &t->if_list, list)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001682 if (pkt_dev->running)
Stephen Hemminger39df2322007-03-04 16:11:51 -08001683 seq_printf(seq, "%s ", pkt_dev->odev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
Luiz Capitulino222f1802006-03-20 22:16:13 -08001685 seq_printf(seq, "\nStopped: ");
1686
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08001687 list_for_each_entry(pkt_dev, &t->if_list, list)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001688 if (!pkt_dev->running)
Stephen Hemminger39df2322007-03-04 16:11:51 -08001689 seq_printf(seq, "%s ", pkt_dev->odev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
1691 if (t->result[0])
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001692 seq_printf(seq, "\nResult: %s\n", t->result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 else
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001694 seq_printf(seq, "\nResult: NA\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
Luiz Capitulino222f1802006-03-20 22:16:13 -08001696 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001698 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699}
1700
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001701static ssize_t pktgen_thread_write(struct file *file,
Luiz Capitulino222f1802006-03-20 22:16:13 -08001702 const char __user * user_buffer,
1703 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704{
Luiz Capitulino222f1802006-03-20 22:16:13 -08001705 struct seq_file *seq = (struct seq_file *)file->private_data;
1706 struct pktgen_thread *t = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 int i = 0, max, len, ret;
1708 char name[40];
Luiz Capitulino222f1802006-03-20 22:16:13 -08001709 char *pg_result;
1710 unsigned long value = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001711
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 if (count < 1) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001713 // sprintf(pg_result, "Wrong command format");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 return -EINVAL;
1715 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001716
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 max = count - i;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001718 len = count_trail_chars(&user_buffer[i], max);
1719 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001720 return len;
1721
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 i += len;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001723
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 /* Read variable name */
1725
1726 len = strn_len(&user_buffer[i], sizeof(name) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001727 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001728 return len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001729
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 memset(name, 0, sizeof(name));
1731 if (copy_from_user(name, &user_buffer[i], len))
1732 return -EFAULT;
1733 i += len;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001734
Luiz Capitulino222f1802006-03-20 22:16:13 -08001735 max = count - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 len = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001737 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001738 return len;
1739
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 i += len;
1741
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001742 if (debug)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001743 printk("pktgen: t=%s, count=%lu\n", name, (unsigned long)count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
Luiz Capitulino222f1802006-03-20 22:16:13 -08001745 if (!t) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 printk("pktgen: ERROR: No thread\n");
1747 ret = -EINVAL;
1748 goto out;
1749 }
1750
1751 pg_result = &(t->result[0]);
1752
Luiz Capitulino222f1802006-03-20 22:16:13 -08001753 if (!strcmp(name, "add_device")) {
1754 char f[32];
1755 memset(f, 0, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 len = strn_len(&user_buffer[i], sizeof(f) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001757 if (len < 0) {
1758 ret = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 goto out;
1760 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001761 if (copy_from_user(f, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 return -EFAULT;
1763 i += len;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001764 mutex_lock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001765 pktgen_add_device(t, f);
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001766 mutex_unlock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001767 ret = count;
1768 sprintf(pg_result, "OK: add_device=%s", f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 goto out;
1770 }
1771
Luiz Capitulino222f1802006-03-20 22:16:13 -08001772 if (!strcmp(name, "rem_device_all")) {
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001773 mutex_lock(&pktgen_thread_lock);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001774 t->control |= T_REMDEVALL;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001775 mutex_unlock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001776 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 ret = count;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001778 sprintf(pg_result, "OK: rem_device_all");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 goto out;
1780 }
1781
Luiz Capitulino222f1802006-03-20 22:16:13 -08001782 if (!strcmp(name, "max_before_softirq")) {
1783 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001784 mutex_lock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001785 t->max_before_softirq = value;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001786 mutex_unlock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001787 ret = count;
1788 sprintf(pg_result, "OK: max_before_softirq=%lu", value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 goto out;
1790 }
1791
1792 ret = -EINVAL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001793out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 return ret;
1795}
1796
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001797static int pktgen_thread_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798{
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001799 return single_open(file, pktgen_thread_show, PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800}
1801
Arjan van de Ven9a321442007-02-12 00:55:35 -08001802static const struct file_operations pktgen_thread_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001803 .owner = THIS_MODULE,
1804 .open = pktgen_thread_open,
1805 .read = seq_read,
1806 .llseek = seq_lseek,
1807 .write = pktgen_thread_write,
1808 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001809};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
1811/* Think find or remove for NN */
Luiz Capitulino222f1802006-03-20 22:16:13 -08001812static struct pktgen_dev *__pktgen_NN_threads(const char *ifname, int remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813{
1814 struct pktgen_thread *t;
1815 struct pktgen_dev *pkt_dev = NULL;
1816
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08001817 list_for_each_entry(t, &pktgen_threads, th_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 pkt_dev = pktgen_find_dev(t, ifname);
1819 if (pkt_dev) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001820 if (remove) {
1821 if_lock(t);
1822 pkt_dev->removal_mark = 1;
1823 t->control |= T_REMDEV;
1824 if_unlock(t);
1825 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 break;
1827 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001829 return pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830}
1831
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001832/*
1833 * mark a device for removal
1834 */
Stephen Hemminger39df2322007-03-04 16:11:51 -08001835static void pktgen_mark_device(const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836{
1837 struct pktgen_dev *pkt_dev = NULL;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001838 const int max_tries = 10, msec_per_try = 125;
1839 int i = 0;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001840
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001841 mutex_lock(&pktgen_thread_lock);
Stephen Hemminger25c4e532007-03-04 16:06:47 -08001842 pr_debug("pktgen: pktgen_mark_device marking %s for removal\n", ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001843
Luiz Capitulino222f1802006-03-20 22:16:13 -08001844 while (1) {
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001845
1846 pkt_dev = __pktgen_NN_threads(ifname, REMOVE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001847 if (pkt_dev == NULL)
1848 break; /* success */
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001849
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001850 mutex_unlock(&pktgen_thread_lock);
Stephen Hemminger25c4e532007-03-04 16:06:47 -08001851 pr_debug("pktgen: pktgen_mark_device waiting for %s "
1852 "to disappear....\n", ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001853 schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001854 mutex_lock(&pktgen_thread_lock);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001855
1856 if (++i >= max_tries) {
1857 printk("pktgen_mark_device: timed out after waiting "
Luiz Capitulino222f1802006-03-20 22:16:13 -08001858 "%d msec for device %s to be removed\n",
1859 msec_per_try * i, ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001860 break;
1861 }
1862
1863 }
1864
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001865 mutex_unlock(&pktgen_thread_lock);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001866}
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001867
Stephen Hemminger39df2322007-03-04 16:11:51 -08001868static void pktgen_change_name(struct net_device *dev)
1869{
1870 struct pktgen_thread *t;
1871
1872 list_for_each_entry(t, &pktgen_threads, th_list) {
1873 struct pktgen_dev *pkt_dev;
1874
1875 list_for_each_entry(pkt_dev, &t->if_list, list) {
1876 if (pkt_dev->odev != dev)
1877 continue;
1878
1879 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
1880
1881 pkt_dev->entry = create_proc_entry(dev->name, 0600,
1882 pg_proc_dir);
1883 if (!pkt_dev->entry)
1884 printk(KERN_ERR "pktgen: can't move proc "
1885 " entry for '%s'\n", dev->name);
1886 break;
1887 }
1888 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889}
1890
Luiz Capitulino222f1802006-03-20 22:16:13 -08001891static int pktgen_device_event(struct notifier_block *unused,
1892 unsigned long event, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893{
Stephen Hemminger39df2322007-03-04 16:11:51 -08001894 struct net_device *dev = ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895
1896 /* It is OK that we do not hold the group lock right now,
1897 * as we run under the RTNL lock.
1898 */
1899
1900 switch (event) {
Stephen Hemminger39df2322007-03-04 16:11:51 -08001901 case NETDEV_CHANGENAME:
1902 pktgen_change_name(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 break;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001904
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 case NETDEV_UNREGISTER:
Luiz Capitulino222f1802006-03-20 22:16:13 -08001906 pktgen_mark_device(dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001908 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909
1910 return NOTIFY_DONE;
1911}
1912
1913/* Associate pktgen_dev with a device. */
1914
Stephen Hemminger39df2322007-03-04 16:11:51 -08001915static int pktgen_setup_dev(struct pktgen_dev *pkt_dev, const char *ifname)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001916{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 struct net_device *odev;
Stephen Hemminger39df2322007-03-04 16:11:51 -08001918 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
1920 /* Clean old setups */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 if (pkt_dev->odev) {
1922 dev_put(pkt_dev->odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001923 pkt_dev->odev = NULL;
1924 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925
Stephen Hemminger39df2322007-03-04 16:11:51 -08001926 odev = dev_get_by_name(ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 if (!odev) {
Stephen Hemminger39df2322007-03-04 16:11:51 -08001928 printk("pktgen: no such netdevice: \"%s\"\n", ifname);
1929 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 }
Stephen Hemminger39df2322007-03-04 16:11:51 -08001931
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 if (odev->type != ARPHRD_ETHER) {
Stephen Hemminger39df2322007-03-04 16:11:51 -08001933 printk("pktgen: not an ethernet device: \"%s\"\n", ifname);
1934 err = -EINVAL;
1935 } else if (!netif_running(odev)) {
1936 printk("pktgen: device is down: \"%s\"\n", ifname);
1937 err = -ENETDOWN;
1938 } else {
1939 pkt_dev->odev = odev;
1940 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001942
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 dev_put(odev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001944 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945}
1946
1947/* Read pkt_dev from the interface and set up internal pktgen_dev
1948 * structure to have the right information to create/send packets
1949 */
1950static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
1951{
Luiz Capitulino222f1802006-03-20 22:16:13 -08001952 if (!pkt_dev->odev) {
1953 printk("pktgen: ERROR: pkt_dev->odev == NULL in setup_inject.\n");
1954 sprintf(pkt_dev->result,
1955 "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
1956 return;
1957 }
1958
1959 /* Default to the interface's mac if not explicitly set. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001961 if (is_zero_ether_addr(pkt_dev->src_mac))
Luiz Capitulino222f1802006-03-20 22:16:13 -08001962 memcpy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
Luiz Capitulino222f1802006-03-20 22:16:13 -08001964 /* Set up Dest MAC */
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001965 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
Luiz Capitulino222f1802006-03-20 22:16:13 -08001967 /* Set up pkt size */
1968 pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
1969
1970 if (pkt_dev->flags & F_IPV6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 /*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001972 * Skip this automatic address setting until locks or functions
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 * gets exported
1974 */
1975
1976#ifdef NOTNOW
Luiz Capitulino222f1802006-03-20 22:16:13 -08001977 int i, set = 0, err = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 struct inet6_dev *idev;
1979
Luiz Capitulino222f1802006-03-20 22:16:13 -08001980 for (i = 0; i < IN6_ADDR_HSIZE; i++)
1981 if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 set = 1;
1983 break;
1984 }
1985
Luiz Capitulino222f1802006-03-20 22:16:13 -08001986 if (!set) {
1987
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 /*
1989 * Use linklevel address if unconfigured.
1990 *
1991 * use ipv6_get_lladdr if/when it's get exported
1992 */
1993
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001994 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 if ((idev = __in6_dev_get(pkt_dev->odev)) != NULL) {
1996 struct inet6_ifaddr *ifp;
1997
1998 read_lock_bh(&idev->lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001999 for (ifp = idev->addr_list; ifp;
2000 ifp = ifp->if_next) {
2001 if (ifp->scope == IFA_LINK
2002 && !(ifp->
2003 flags & IFA_F_TENTATIVE)) {
2004 ipv6_addr_copy(&pkt_dev->
2005 cur_in6_saddr,
2006 &ifp->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 err = 0;
2008 break;
2009 }
2010 }
2011 read_unlock_bh(&idev->lock);
2012 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002013 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002014 if (err)
2015 printk("pktgen: ERROR: IPv6 link address not availble.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 }
2017#endif
Luiz Capitulino222f1802006-03-20 22:16:13 -08002018 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 pkt_dev->saddr_min = 0;
2020 pkt_dev->saddr_max = 0;
2021 if (strlen(pkt_dev->src_min) == 0) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002022
2023 struct in_device *in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024
2025 rcu_read_lock();
Herbert Xue5ed6392005-10-03 14:35:55 -07002026 in_dev = __in_dev_get_rcu(pkt_dev->odev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 if (in_dev) {
2028 if (in_dev->ifa_list) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002029 pkt_dev->saddr_min =
2030 in_dev->ifa_list->ifa_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 pkt_dev->saddr_max = pkt_dev->saddr_min;
2032 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 }
2034 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002035 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
2037 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
2038 }
2039
2040 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
2041 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
2042 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002043 /* Initialize current values. */
2044 pkt_dev->cur_dst_mac_offset = 0;
2045 pkt_dev->cur_src_mac_offset = 0;
2046 pkt_dev->cur_saddr = pkt_dev->saddr_min;
2047 pkt_dev->cur_daddr = pkt_dev->daddr_min;
2048 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2049 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 pkt_dev->nflows = 0;
2051}
2052
2053static void spin(struct pktgen_dev *pkt_dev, __u64 spin_until_us)
2054{
2055 __u64 start;
2056 __u64 now;
2057
2058 start = now = getCurUs();
2059 printk(KERN_INFO "sleeping for %d\n", (int)(spin_until_us - now));
2060 while (now < spin_until_us) {
Stephen Hemmingerb4099fa2005-10-14 15:32:22 -07002061 /* TODO: optimize sleeping behavior */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002062 if (spin_until_us - now > jiffies_to_usecs(1) + 1)
Nishanth Aravamudan121caf52005-09-12 14:15:34 -07002063 schedule_timeout_interruptible(1);
2064 else if (spin_until_us - now > 100) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 do_softirq();
2066 if (!pkt_dev->running)
2067 return;
2068 if (need_resched())
2069 schedule();
2070 }
2071
2072 now = getCurUs();
2073 }
2074
2075 pkt_dev->idle_acc += now - start;
2076}
2077
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078/* Increment/randomize headers according to flags and current values
2079 * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
2080 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002081static void mod_cur_headers(struct pktgen_dev *pkt_dev)
2082{
2083 __u32 imn;
2084 __u32 imx;
2085 int flow = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
Luiz Capitulino222f1802006-03-20 22:16:13 -08002087 if (pkt_dev->cflows) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002088 flow = random32() % pkt_dev->cflows;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002089
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 if (pkt_dev->flows[flow].count > pkt_dev->lflow)
2091 pkt_dev->flows[flow].count = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002092 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093
2094 /* Deal with source MAC */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002095 if (pkt_dev->src_mac_count > 1) {
2096 __u32 mc;
2097 __u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098
Luiz Capitulino222f1802006-03-20 22:16:13 -08002099 if (pkt_dev->flags & F_MACSRC_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002100 mc = random32() % pkt_dev->src_mac_count;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002101 else {
2102 mc = pkt_dev->cur_src_mac_offset++;
2103 if (pkt_dev->cur_src_mac_offset >
2104 pkt_dev->src_mac_count)
2105 pkt_dev->cur_src_mac_offset = 0;
2106 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107
Luiz Capitulino222f1802006-03-20 22:16:13 -08002108 tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
2109 pkt_dev->hh[11] = tmp;
2110 tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2111 pkt_dev->hh[10] = tmp;
2112 tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2113 pkt_dev->hh[9] = tmp;
2114 tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2115 pkt_dev->hh[8] = tmp;
2116 tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
2117 pkt_dev->hh[7] = tmp;
2118 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119
Luiz Capitulino222f1802006-03-20 22:16:13 -08002120 /* Deal with Destination MAC */
2121 if (pkt_dev->dst_mac_count > 1) {
2122 __u32 mc;
2123 __u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124
Luiz Capitulino222f1802006-03-20 22:16:13 -08002125 if (pkt_dev->flags & F_MACDST_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002126 mc = random32() % pkt_dev->dst_mac_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127
Luiz Capitulino222f1802006-03-20 22:16:13 -08002128 else {
2129 mc = pkt_dev->cur_dst_mac_offset++;
2130 if (pkt_dev->cur_dst_mac_offset >
2131 pkt_dev->dst_mac_count) {
2132 pkt_dev->cur_dst_mac_offset = 0;
2133 }
2134 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135
Luiz Capitulino222f1802006-03-20 22:16:13 -08002136 tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
2137 pkt_dev->hh[5] = tmp;
2138 tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2139 pkt_dev->hh[4] = tmp;
2140 tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2141 pkt_dev->hh[3] = tmp;
2142 tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2143 pkt_dev->hh[2] = tmp;
2144 tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
2145 pkt_dev->hh[1] = tmp;
2146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002148 if (pkt_dev->flags & F_MPLS_RND) {
2149 unsigned i;
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002150 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002151 if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
2152 pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002153 ((__force __be32)random32() &
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002154 htonl(0x000fffff));
2155 }
2156
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002157 if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002158 pkt_dev->vlan_id = random32() & (4096-1);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002159 }
2160
2161 if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002162 pkt_dev->svlan_id = random32() & (4096 - 1);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002163 }
2164
Luiz Capitulino222f1802006-03-20 22:16:13 -08002165 if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
2166 if (pkt_dev->flags & F_UDPSRC_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002167 pkt_dev->cur_udp_src = random32() %
2168 (pkt_dev->udp_src_max - pkt_dev->udp_src_min)
2169 + pkt_dev->udp_src_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170
Luiz Capitulino222f1802006-03-20 22:16:13 -08002171 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 pkt_dev->cur_udp_src++;
2173 if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
2174 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002175 }
2176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177
Luiz Capitulino222f1802006-03-20 22:16:13 -08002178 if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
2179 if (pkt_dev->flags & F_UDPDST_RND) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002180 pkt_dev->cur_udp_dst = random32() %
2181 (pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)
2182 + pkt_dev->udp_dst_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002183 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 pkt_dev->cur_udp_dst++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002185 if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002187 }
2188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189
2190 if (!(pkt_dev->flags & F_IPV6)) {
2191
Luiz Capitulino222f1802006-03-20 22:16:13 -08002192 if ((imn = ntohl(pkt_dev->saddr_min)) < (imx =
2193 ntohl(pkt_dev->
2194 saddr_max))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 __u32 t;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002196 if (pkt_dev->flags & F_IPSRC_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002197 t = random32() % (imx - imn) + imn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 else {
2199 t = ntohl(pkt_dev->cur_saddr);
2200 t++;
2201 if (t > imx) {
2202 t = imn;
2203 }
2204 }
2205 pkt_dev->cur_saddr = htonl(t);
2206 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002207
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 if (pkt_dev->cflows && pkt_dev->flows[flow].count != 0) {
2209 pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
2210 } else {
Al Viro252e3342006-11-14 20:48:11 -08002211 imn = ntohl(pkt_dev->daddr_min);
2212 imx = ntohl(pkt_dev->daddr_max);
2213 if (imn < imx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 __u32 t;
Al Viro252e3342006-11-14 20:48:11 -08002215 __be32 s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 if (pkt_dev->flags & F_IPDST_RND) {
2217
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002218 t = random32() % (imx - imn) + imn;
Al Viro252e3342006-11-14 20:48:11 -08002219 s = htonl(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220
Al Viro252e3342006-11-14 20:48:11 -08002221 while (LOOPBACK(s) || MULTICAST(s)
2222 || BADCLASS(s) || ZERONET(s)
2223 || LOCAL_MCAST(s)) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002224 t = random32() % (imx - imn) + imn;
Al Viro252e3342006-11-14 20:48:11 -08002225 s = htonl(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 }
Al Viro252e3342006-11-14 20:48:11 -08002227 pkt_dev->cur_daddr = s;
2228 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 t = ntohl(pkt_dev->cur_daddr);
2230 t++;
2231 if (t > imx) {
2232 t = imn;
2233 }
2234 pkt_dev->cur_daddr = htonl(t);
2235 }
2236 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002237 if (pkt_dev->cflows) {
2238 pkt_dev->flows[flow].cur_daddr =
2239 pkt_dev->cur_daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 pkt_dev->nflows++;
2241 }
2242 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002243 } else { /* IPV6 * */
2244
2245 if (pkt_dev->min_in6_daddr.s6_addr32[0] == 0 &&
2246 pkt_dev->min_in6_daddr.s6_addr32[1] == 0 &&
2247 pkt_dev->min_in6_daddr.s6_addr32[2] == 0 &&
2248 pkt_dev->min_in6_daddr.s6_addr32[3] == 0) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 else {
2250 int i;
2251
2252 /* Only random destinations yet */
2253
Luiz Capitulino222f1802006-03-20 22:16:13 -08002254 for (i = 0; i < 4; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 pkt_dev->cur_in6_daddr.s6_addr32[i] =
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002256 (((__force __be32)random32() |
Luiz Capitulino222f1802006-03-20 22:16:13 -08002257 pkt_dev->min_in6_daddr.s6_addr32[i]) &
2258 pkt_dev->max_in6_daddr.s6_addr32[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 }
2262
Luiz Capitulino222f1802006-03-20 22:16:13 -08002263 if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
2264 __u32 t;
2265 if (pkt_dev->flags & F_TXSIZE_RND) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002266 t = random32() %
2267 (pkt_dev->max_pkt_size - pkt_dev->min_pkt_size)
2268 + pkt_dev->min_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002269 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 t = pkt_dev->cur_pkt_size + 1;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002271 if (t > pkt_dev->max_pkt_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 t = pkt_dev->min_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002273 }
2274 pkt_dev->cur_pkt_size = t;
2275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276
2277 pkt_dev->flows[flow].count++;
2278}
2279
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002280static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
2281{
2282 unsigned i;
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002283 for (i = 0; i < pkt_dev->nr_labels; i++) {
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002284 *mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
2285 }
2286 mpls--;
2287 *mpls |= MPLS_STACK_BOTTOM;
2288}
2289
Al Viro0f37c602006-11-03 03:49:56 -08002290static inline __be16 build_tci(unsigned int id, unsigned int cfi,
2291 unsigned int prio)
2292{
2293 return htons(id | (cfi << 12) | (prio << 13));
2294}
2295
Luiz Capitulino222f1802006-03-20 22:16:13 -08002296static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
2297 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298{
2299 struct sk_buff *skb = NULL;
2300 __u8 *eth;
2301 struct udphdr *udph;
2302 int datalen, iplen;
2303 struct iphdr *iph;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002304 struct pktgen_hdr *pgh = NULL;
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002305 __be16 protocol = htons(ETH_P_IP);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002306 __be32 *mpls;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002307 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2308 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2309 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2310 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2311
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002312
2313 if (pkt_dev->nr_labels)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002314 protocol = htons(ETH_P_MPLS_UC);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002315
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002316 if (pkt_dev->vlan_id != 0xffff)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002317 protocol = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002318
Robert Olsson64053be2005-06-26 15:27:10 -07002319 /* Update any of the values, used when we're incrementing various
2320 * fields.
2321 */
2322 mod_cur_headers(pkt_dev);
2323
David S. Miller7ac54592006-01-18 14:19:10 -08002324 datalen = (odev->hard_header_len + 16) & ~0xf;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002325 skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + datalen +
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002326 pkt_dev->nr_labels*sizeof(u32) +
2327 VLAN_TAG_SIZE(pkt_dev) + SVLAN_TAG_SIZE(pkt_dev),
2328 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 if (!skb) {
2330 sprintf(pkt_dev->result, "No memory");
2331 return NULL;
2332 }
2333
David S. Miller7ac54592006-01-18 14:19:10 -08002334 skb_reserve(skb, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335
2336 /* Reserve for ethernet and IP header */
2337 eth = (__u8 *) skb_push(skb, 14);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002338 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2339 if (pkt_dev->nr_labels)
2340 mpls_push(mpls, pkt_dev);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002341
2342 if (pkt_dev->vlan_id != 0xffff) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002343 if (pkt_dev->svlan_id != 0xffff) {
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002344 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002345 *svlan_tci = build_tci(pkt_dev->svlan_id,
2346 pkt_dev->svlan_cfi,
2347 pkt_dev->svlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002348 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002349 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002350 }
2351 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002352 *vlan_tci = build_tci(pkt_dev->vlan_id,
2353 pkt_dev->vlan_cfi,
2354 pkt_dev->vlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002355 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002356 *vlan_encapsulated_proto = htons(ETH_P_IP);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002357 }
2358
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002359 skb->network_header = skb->tail;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002360 skb->transport_header = skb->network_header + sizeof(struct iphdr);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002361 skb_put(skb, sizeof(struct iphdr) + sizeof(struct udphdr));
2362
2363 iph = ip_hdr(skb);
2364 udph = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 memcpy(eth, pkt_dev->hh, 12);
Al Viro252e3342006-11-14 20:48:11 -08002367 *(__be16 *) & eth[12] = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002369 /* Eth + IPh + UDPh + mpls */
2370 datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002371 pkt_dev->nr_labels*sizeof(u32) - VLAN_TAG_SIZE(pkt_dev) - SVLAN_TAG_SIZE(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002372 if (datalen < sizeof(struct pktgen_hdr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 datalen = sizeof(struct pktgen_hdr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002374
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 udph->source = htons(pkt_dev->cur_udp_src);
2376 udph->dest = htons(pkt_dev->cur_udp_dst);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002377 udph->len = htons(datalen + 8); /* DATA + udphdr */
2378 udph->check = 0; /* No checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379
2380 iph->ihl = 5;
2381 iph->version = 4;
2382 iph->ttl = 32;
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002383 iph->tos = pkt_dev->tos;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002384 iph->protocol = IPPROTO_UDP; /* UDP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 iph->saddr = pkt_dev->cur_saddr;
2386 iph->daddr = pkt_dev->cur_daddr;
2387 iph->frag_off = 0;
2388 iplen = 20 + 8 + datalen;
2389 iph->tot_len = htons(iplen);
2390 iph->check = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002391 iph->check = ip_fast_csum((void *)iph, iph->ihl);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002392 skb->protocol = protocol;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002393 skb->mac_header = (skb->network_header - ETH_HLEN -
2394 pkt_dev->nr_labels * sizeof(u32) -
2395 VLAN_TAG_SIZE(pkt_dev) - SVLAN_TAG_SIZE(pkt_dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 skb->dev = odev;
2397 skb->pkt_type = PACKET_HOST;
2398
Luiz Capitulino222f1802006-03-20 22:16:13 -08002399 if (pkt_dev->nfrags <= 0)
2400 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 else {
2402 int frags = pkt_dev->nfrags;
2403 int i;
2404
Luiz Capitulino222f1802006-03-20 22:16:13 -08002405 pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
2406
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 if (frags > MAX_SKB_FRAGS)
2408 frags = MAX_SKB_FRAGS;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002409 if (datalen > frags * PAGE_SIZE) {
2410 skb_put(skb, datalen - frags * PAGE_SIZE);
2411 datalen = frags * PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 }
2413
2414 i = 0;
2415 while (datalen > 0) {
2416 struct page *page = alloc_pages(GFP_KERNEL, 0);
2417 skb_shinfo(skb)->frags[i].page = page;
2418 skb_shinfo(skb)->frags[i].page_offset = 0;
2419 skb_shinfo(skb)->frags[i].size =
Luiz Capitulino222f1802006-03-20 22:16:13 -08002420 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 datalen -= skb_shinfo(skb)->frags[i].size;
2422 skb->len += skb_shinfo(skb)->frags[i].size;
2423 skb->data_len += skb_shinfo(skb)->frags[i].size;
2424 i++;
2425 skb_shinfo(skb)->nr_frags = i;
2426 }
2427
2428 while (i < frags) {
2429 int rem;
2430
2431 if (i == 0)
2432 break;
2433
2434 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
2435 if (rem == 0)
2436 break;
2437
2438 skb_shinfo(skb)->frags[i - 1].size -= rem;
2439
Luiz Capitulino222f1802006-03-20 22:16:13 -08002440 skb_shinfo(skb)->frags[i] =
2441 skb_shinfo(skb)->frags[i - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 get_page(skb_shinfo(skb)->frags[i].page);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002443 skb_shinfo(skb)->frags[i].page =
2444 skb_shinfo(skb)->frags[i - 1].page;
2445 skb_shinfo(skb)->frags[i].page_offset +=
2446 skb_shinfo(skb)->frags[i - 1].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 skb_shinfo(skb)->frags[i].size = rem;
2448 i++;
2449 skb_shinfo(skb)->nr_frags = i;
2450 }
2451 }
2452
Luiz Capitulino222f1802006-03-20 22:16:13 -08002453 /* Stamp the time, and sequence number, convert them to network byte order */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454
Luiz Capitulino222f1802006-03-20 22:16:13 -08002455 if (pgh) {
2456 struct timeval timestamp;
2457
2458 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2459 pgh->seq_num = htonl(pkt_dev->seq_num);
2460
2461 do_gettimeofday(&timestamp);
2462 pgh->tv_sec = htonl(timestamp.tv_sec);
2463 pgh->tv_usec = htonl(timestamp.tv_usec);
2464 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002465
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 return skb;
2467}
2468
2469/*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002470 * scan_ip6, fmt_ip taken from dietlibc-0.21
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 * Author Felix von Leitner <felix-dietlibc@fefe.de>
2472 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002473 * Slightly modified for kernel.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 * Should be candidate for net/ipv4/utils.c
2475 * --ro
2476 */
2477
Luiz Capitulino222f1802006-03-20 22:16:13 -08002478static unsigned int scan_ip6(const char *s, char ip[16])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479{
2480 unsigned int i;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002481 unsigned int len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 unsigned long u;
2483 char suffix[16];
Luiz Capitulino222f1802006-03-20 22:16:13 -08002484 unsigned int prefixlen = 0;
2485 unsigned int suffixlen = 0;
Al Viro252e3342006-11-14 20:48:11 -08002486 __be32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487
Luiz Capitulino222f1802006-03-20 22:16:13 -08002488 for (i = 0; i < 16; i++)
2489 ip[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490
2491 for (;;) {
2492 if (*s == ':') {
2493 len++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002494 if (s[1] == ':') { /* Found "::", skip to part 2 */
2495 s += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 len++;
2497 break;
2498 }
2499 s++;
2500 }
2501 {
2502 char *tmp;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002503 u = simple_strtoul(s, &tmp, 16);
2504 i = tmp - s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 }
2506
Luiz Capitulino222f1802006-03-20 22:16:13 -08002507 if (!i)
2508 return 0;
2509 if (prefixlen == 12 && s[i] == '.') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510
2511 /* the last 4 bytes may be written as IPv4 address */
2512
2513 tmp = in_aton(s);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002514 memcpy((struct in_addr *)(ip + 12), &tmp, sizeof(tmp));
2515 return i + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 }
2517 ip[prefixlen++] = (u >> 8);
2518 ip[prefixlen++] = (u & 255);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002519 s += i;
2520 len += i;
2521 if (prefixlen == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 return len;
2523 }
2524
2525/* part 2, after "::" */
2526 for (;;) {
2527 if (*s == ':') {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002528 if (suffixlen == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 break;
2530 s++;
2531 len++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002532 } else if (suffixlen != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 break;
2534 {
2535 char *tmp;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002536 u = simple_strtol(s, &tmp, 16);
2537 i = tmp - s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 }
2539 if (!i) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002540 if (*s)
2541 len--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 break;
2543 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002544 if (suffixlen + prefixlen <= 12 && s[i] == '.') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 tmp = in_aton(s);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002546 memcpy((struct in_addr *)(suffix + suffixlen), &tmp,
2547 sizeof(tmp));
2548 suffixlen += 4;
2549 len += strlen(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 break;
2551 }
2552 suffix[suffixlen++] = (u >> 8);
2553 suffix[suffixlen++] = (u & 255);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002554 s += i;
2555 len += i;
2556 if (prefixlen + suffixlen == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 break;
2558 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002559 for (i = 0; i < suffixlen; i++)
2560 ip[16 - suffixlen + i] = suffix[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 return len;
2562}
2563
Luiz Capitulino222f1802006-03-20 22:16:13 -08002564static char tohex(char hexdigit)
2565{
2566 return hexdigit > 9 ? hexdigit + 'a' - 10 : hexdigit + '0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567}
2568
Luiz Capitulino222f1802006-03-20 22:16:13 -08002569static int fmt_xlong(char *s, unsigned int i)
2570{
2571 char *bak = s;
2572 *s = tohex((i >> 12) & 0xf);
2573 if (s != bak || *s != '0')
2574 ++s;
2575 *s = tohex((i >> 8) & 0xf);
2576 if (s != bak || *s != '0')
2577 ++s;
2578 *s = tohex((i >> 4) & 0xf);
2579 if (s != bak || *s != '0')
2580 ++s;
2581 *s = tohex(i & 0xf);
2582 return s - bak + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583}
2584
Luiz Capitulino222f1802006-03-20 22:16:13 -08002585static unsigned int fmt_ip6(char *s, const char ip[16])
2586{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 unsigned int len;
2588 unsigned int i;
2589 unsigned int temp;
2590 unsigned int compressing;
2591 int j;
2592
Luiz Capitulino222f1802006-03-20 22:16:13 -08002593 len = 0;
2594 compressing = 0;
2595 for (j = 0; j < 16; j += 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596
2597#ifdef V4MAPPEDPREFIX
Luiz Capitulino222f1802006-03-20 22:16:13 -08002598 if (j == 12 && !memcmp(ip, V4mappedprefix, 12)) {
2599 inet_ntoa_r(*(struct in_addr *)(ip + 12), s);
2600 temp = strlen(s);
2601 return len + temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 }
2603#endif
Luiz Capitulino222f1802006-03-20 22:16:13 -08002604 temp = ((unsigned long)(unsigned char)ip[j] << 8) +
2605 (unsigned long)(unsigned char)ip[j + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 if (temp == 0) {
2607 if (!compressing) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002608 compressing = 1;
2609 if (j == 0) {
2610 *s++ = ':';
2611 ++len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 }
2613 }
2614 } else {
2615 if (compressing) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002616 compressing = 0;
2617 *s++ = ':';
2618 ++len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002620 i = fmt_xlong(s, temp);
2621 len += i;
2622 s += i;
2623 if (j < 14) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 *s++ = ':';
2625 ++len;
2626 }
2627 }
2628 }
2629 if (compressing) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002630 *s++ = ':';
2631 ++len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002633 *s = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 return len;
2635}
2636
Luiz Capitulino222f1802006-03-20 22:16:13 -08002637static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
2638 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639{
2640 struct sk_buff *skb = NULL;
2641 __u8 *eth;
2642 struct udphdr *udph;
2643 int datalen;
2644 struct ipv6hdr *iph;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002645 struct pktgen_hdr *pgh = NULL;
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002646 __be16 protocol = htons(ETH_P_IPV6);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002647 __be32 *mpls;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002648 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2649 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2650 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2651 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002652
2653 if (pkt_dev->nr_labels)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002654 protocol = htons(ETH_P_MPLS_UC);
Robert Olsson64053be2005-06-26 15:27:10 -07002655
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002656 if (pkt_dev->vlan_id != 0xffff)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002657 protocol = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002658
Robert Olsson64053be2005-06-26 15:27:10 -07002659 /* Update any of the values, used when we're incrementing various
2660 * fields.
2661 */
2662 mod_cur_headers(pkt_dev);
2663
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002664 skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + 16 +
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002665 pkt_dev->nr_labels*sizeof(u32) +
2666 VLAN_TAG_SIZE(pkt_dev) + SVLAN_TAG_SIZE(pkt_dev),
2667 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 if (!skb) {
2669 sprintf(pkt_dev->result, "No memory");
2670 return NULL;
2671 }
2672
2673 skb_reserve(skb, 16);
2674
2675 /* Reserve for ethernet and IP header */
2676 eth = (__u8 *) skb_push(skb, 14);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002677 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2678 if (pkt_dev->nr_labels)
2679 mpls_push(mpls, pkt_dev);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002680
2681 if (pkt_dev->vlan_id != 0xffff) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002682 if (pkt_dev->svlan_id != 0xffff) {
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002683 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002684 *svlan_tci = build_tci(pkt_dev->svlan_id,
2685 pkt_dev->svlan_cfi,
2686 pkt_dev->svlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002687 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002688 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002689 }
2690 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002691 *vlan_tci = build_tci(pkt_dev->vlan_id,
2692 pkt_dev->vlan_cfi,
2693 pkt_dev->vlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002694 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002695 *vlan_encapsulated_proto = htons(ETH_P_IPV6);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002696 }
2697
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002698 skb->network_header = skb->tail;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002699 skb->transport_header = skb->network_header + sizeof(struct ipv6hdr);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002700 skb_put(skb, sizeof(struct ipv6hdr) + sizeof(struct udphdr));
2701
2702 iph = ipv6_hdr(skb);
2703 udph = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 memcpy(eth, pkt_dev->hh, 12);
Al Viro252e3342006-11-14 20:48:11 -08002706 *(__be16 *) & eth[12] = protocol;
Robert Olsson64053be2005-06-26 15:27:10 -07002707
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002708 /* Eth + IPh + UDPh + mpls */
2709 datalen = pkt_dev->cur_pkt_size - 14 -
2710 sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002711 pkt_dev->nr_labels*sizeof(u32) - VLAN_TAG_SIZE(pkt_dev) - SVLAN_TAG_SIZE(pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712
Luiz Capitulino222f1802006-03-20 22:16:13 -08002713 if (datalen < sizeof(struct pktgen_hdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 datalen = sizeof(struct pktgen_hdr);
2715 if (net_ratelimit())
Luiz Capitulino222f1802006-03-20 22:16:13 -08002716 printk(KERN_INFO "pktgen: increased datalen to %d\n",
2717 datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 }
2719
2720 udph->source = htons(pkt_dev->cur_udp_src);
2721 udph->dest = htons(pkt_dev->cur_udp_dst);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002722 udph->len = htons(datalen + sizeof(struct udphdr));
2723 udph->check = 0; /* No checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002725 *(__be32 *) iph = htonl(0x60000000); /* Version + flow */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002727 if (pkt_dev->traffic_class) {
2728 /* Version + traffic class + flow (0) */
Al Viro252e3342006-11-14 20:48:11 -08002729 *(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002730 }
2731
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 iph->hop_limit = 32;
2733
2734 iph->payload_len = htons(sizeof(struct udphdr) + datalen);
2735 iph->nexthdr = IPPROTO_UDP;
2736
2737 ipv6_addr_copy(&iph->daddr, &pkt_dev->cur_in6_daddr);
2738 ipv6_addr_copy(&iph->saddr, &pkt_dev->cur_in6_saddr);
2739
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002740 skb->mac_header = (skb->network_header - ETH_HLEN -
2741 pkt_dev->nr_labels * sizeof(u32) -
2742 VLAN_TAG_SIZE(pkt_dev) - SVLAN_TAG_SIZE(pkt_dev));
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002743 skb->protocol = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 skb->dev = odev;
2745 skb->pkt_type = PACKET_HOST;
2746
Luiz Capitulino222f1802006-03-20 22:16:13 -08002747 if (pkt_dev->nfrags <= 0)
2748 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 else {
2750 int frags = pkt_dev->nfrags;
2751 int i;
2752
Luiz Capitulino222f1802006-03-20 22:16:13 -08002753 pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
2754
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 if (frags > MAX_SKB_FRAGS)
2756 frags = MAX_SKB_FRAGS;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002757 if (datalen > frags * PAGE_SIZE) {
2758 skb_put(skb, datalen - frags * PAGE_SIZE);
2759 datalen = frags * PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 }
2761
2762 i = 0;
2763 while (datalen > 0) {
2764 struct page *page = alloc_pages(GFP_KERNEL, 0);
2765 skb_shinfo(skb)->frags[i].page = page;
2766 skb_shinfo(skb)->frags[i].page_offset = 0;
2767 skb_shinfo(skb)->frags[i].size =
Luiz Capitulino222f1802006-03-20 22:16:13 -08002768 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 datalen -= skb_shinfo(skb)->frags[i].size;
2770 skb->len += skb_shinfo(skb)->frags[i].size;
2771 skb->data_len += skb_shinfo(skb)->frags[i].size;
2772 i++;
2773 skb_shinfo(skb)->nr_frags = i;
2774 }
2775
2776 while (i < frags) {
2777 int rem;
2778
2779 if (i == 0)
2780 break;
2781
2782 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
2783 if (rem == 0)
2784 break;
2785
2786 skb_shinfo(skb)->frags[i - 1].size -= rem;
2787
Luiz Capitulino222f1802006-03-20 22:16:13 -08002788 skb_shinfo(skb)->frags[i] =
2789 skb_shinfo(skb)->frags[i - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 get_page(skb_shinfo(skb)->frags[i].page);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002791 skb_shinfo(skb)->frags[i].page =
2792 skb_shinfo(skb)->frags[i - 1].page;
2793 skb_shinfo(skb)->frags[i].page_offset +=
2794 skb_shinfo(skb)->frags[i - 1].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 skb_shinfo(skb)->frags[i].size = rem;
2796 i++;
2797 skb_shinfo(skb)->nr_frags = i;
2798 }
2799 }
2800
Luiz Capitulino222f1802006-03-20 22:16:13 -08002801 /* Stamp the time, and sequence number, convert them to network byte order */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 /* should we update cloned packets too ? */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002803 if (pgh) {
2804 struct timeval timestamp;
2805
2806 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2807 pgh->seq_num = htonl(pkt_dev->seq_num);
2808
2809 do_gettimeofday(&timestamp);
2810 pgh->tv_sec = htonl(timestamp.tv_sec);
2811 pgh->tv_usec = htonl(timestamp.tv_usec);
2812 }
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002813 /* pkt_dev->seq_num++; FF: you really mean this? */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002814
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 return skb;
2816}
2817
Luiz Capitulino222f1802006-03-20 22:16:13 -08002818static inline struct sk_buff *fill_packet(struct net_device *odev,
2819 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820{
Luiz Capitulino222f1802006-03-20 22:16:13 -08002821 if (pkt_dev->flags & F_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 return fill_packet_ipv6(odev, pkt_dev);
2823 else
2824 return fill_packet_ipv4(odev, pkt_dev);
2825}
2826
Luiz Capitulino222f1802006-03-20 22:16:13 -08002827static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828{
Luiz Capitulino222f1802006-03-20 22:16:13 -08002829 pkt_dev->seq_num = 1;
2830 pkt_dev->idle_acc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002832 pkt_dev->tx_bytes = 0;
2833 pkt_dev->errors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834}
2835
2836/* Set up structure for sending pkts, clear counters */
2837
2838static void pktgen_run(struct pktgen_thread *t)
2839{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08002840 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 int started = 0;
2842
Stephen Hemminger25c4e532007-03-04 16:06:47 -08002843 pr_debug("pktgen: entering pktgen_run. %p\n", t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844
2845 if_lock(t);
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08002846 list_for_each_entry(pkt_dev, &t->if_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847
2848 /*
2849 * setup odev and create initial packet.
2850 */
2851 pktgen_setup_inject(pkt_dev);
2852
Luiz Capitulino222f1802006-03-20 22:16:13 -08002853 if (pkt_dev->odev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 pktgen_clear_counters(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002855 pkt_dev->running = 1; /* Cranke yeself! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 pkt_dev->skb = NULL;
2857 pkt_dev->started_at = getCurUs();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002858 pkt_dev->next_tx_us = getCurUs(); /* Transmit immediately */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 pkt_dev->next_tx_ns = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002860
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 strcpy(pkt_dev->result, "Starting");
2862 started++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002863 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 strcpy(pkt_dev->result, "Error starting");
2865 }
2866 if_unlock(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002867 if (started)
2868 t->control &= ~(T_STOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869}
2870
2871static void pktgen_stop_all_threads_ifs(void)
2872{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002873 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874
Stephen Hemminger25c4e532007-03-04 16:06:47 -08002875 pr_debug("pktgen: entering pktgen_stop_all_threads_ifs.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08002877 mutex_lock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002878
2879 list_for_each_entry(t, &pktgen_threads, th_list)
Arthur Kepner95ed63f2006-03-20 21:26:56 -08002880 t->control |= T_STOP;
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002881
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08002882 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883}
2884
Luiz Capitulino222f1802006-03-20 22:16:13 -08002885static int thread_is_running(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08002887 struct pktgen_dev *pkt_dev;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002888 int res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08002890 list_for_each_entry(pkt_dev, &t->if_list, list)
2891 if (pkt_dev->running) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 res = 1;
2893 break;
2894 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002895 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896}
2897
Luiz Capitulino222f1802006-03-20 22:16:13 -08002898static int pktgen_wait_thread_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899{
Luiz Capitulino222f1802006-03-20 22:16:13 -08002900 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901
Luiz Capitulino222f1802006-03-20 22:16:13 -08002902 while (thread_is_running(t)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903
Luiz Capitulino222f1802006-03-20 22:16:13 -08002904 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905
Luiz Capitulino222f1802006-03-20 22:16:13 -08002906 msleep_interruptible(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907
Luiz Capitulino222f1802006-03-20 22:16:13 -08002908 if (signal_pending(current))
2909 goto signal;
2910 if_lock(t);
2911 }
2912 if_unlock(t);
2913 return 1;
2914signal:
2915 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916}
2917
2918static int pktgen_wait_all_threads_run(void)
2919{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002920 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 int sig = 1;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002922
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08002923 mutex_lock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002924
2925 list_for_each_entry(t, &pktgen_threads, th_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 sig = pktgen_wait_thread_run(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002927 if (sig == 0)
2928 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 }
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002930
2931 if (sig == 0)
2932 list_for_each_entry(t, &pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 t->control |= (T_STOP);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002934
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08002935 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 return sig;
2937}
2938
2939static void pktgen_run_all_threads(void)
2940{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002941 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942
Stephen Hemminger25c4e532007-03-04 16:06:47 -08002943 pr_debug("pktgen: entering pktgen_run_all_threads.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08002945 mutex_lock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002947 list_for_each_entry(t, &pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 t->control |= (T_RUN);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002949
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08002950 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951
Luiz Capitulino222f1802006-03-20 22:16:13 -08002952 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
2953
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 pktgen_wait_all_threads_run();
2955}
2956
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
2958{
Luiz Capitulino222f1802006-03-20 22:16:13 -08002959 __u64 total_us, bps, mbps, pps, idle;
2960 char *p = pkt_dev->result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961
Luiz Capitulino222f1802006-03-20 22:16:13 -08002962 total_us = pkt_dev->stopped_at - pkt_dev->started_at;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963
Luiz Capitulino222f1802006-03-20 22:16:13 -08002964 idle = pkt_dev->idle_acc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965
Luiz Capitulino222f1802006-03-20 22:16:13 -08002966 p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
2967 (unsigned long long)total_us,
2968 (unsigned long long)(total_us - idle),
2969 (unsigned long long)idle,
2970 (unsigned long long)pkt_dev->sofar,
2971 pkt_dev->cur_pkt_size, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972
Luiz Capitulino222f1802006-03-20 22:16:13 -08002973 pps = pkt_dev->sofar * USEC_PER_SEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974
Luiz Capitulino222f1802006-03-20 22:16:13 -08002975 while ((total_us >> 32) != 0) {
2976 pps >>= 1;
2977 total_us >>= 1;
2978 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979
Luiz Capitulino222f1802006-03-20 22:16:13 -08002980 do_div(pps, total_us);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981
Luiz Capitulino222f1802006-03-20 22:16:13 -08002982 bps = pps * 8 * pkt_dev->cur_pkt_size;
2983
2984 mbps = bps;
2985 do_div(mbps, 1000000);
2986 p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu",
2987 (unsigned long long)pps,
2988 (unsigned long long)mbps,
2989 (unsigned long long)bps,
2990 (unsigned long long)pkt_dev->errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992
2993/* Set stopped-at timer, remove from running list, do counters & statistics */
2994
Luiz Capitulino222f1802006-03-20 22:16:13 -08002995static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996{
Luiz Capitulino222f1802006-03-20 22:16:13 -08002997 int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08002998
Luiz Capitulino222f1802006-03-20 22:16:13 -08002999 if (!pkt_dev->running) {
3000 printk("pktgen: interface: %s is already stopped\n",
Stephen Hemminger39df2322007-03-04 16:11:51 -08003001 pkt_dev->odev->name);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003002 return -EINVAL;
3003 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004
Luiz Capitulino222f1802006-03-20 22:16:13 -08003005 pkt_dev->stopped_at = getCurUs();
3006 pkt_dev->running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003008 show_results(pkt_dev, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009
Luiz Capitulino222f1802006-03-20 22:16:13 -08003010 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011}
3012
Luiz Capitulino222f1802006-03-20 22:16:13 -08003013static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003015 struct pktgen_dev *pkt_dev, *best = NULL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003016
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 if_lock(t);
3018
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003019 list_for_each_entry(pkt_dev, &t->if_list, list) {
3020 if (!pkt_dev->running)
Luiz Capitulino222f1802006-03-20 22:16:13 -08003021 continue;
3022 if (best == NULL)
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003023 best = pkt_dev;
3024 else if (pkt_dev->next_tx_us < best->next_tx_us)
3025 best = pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 }
3027 if_unlock(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003028 return best;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029}
3030
Luiz Capitulino222f1802006-03-20 22:16:13 -08003031static void pktgen_stop(struct pktgen_thread *t)
3032{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003033 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003035 pr_debug("pktgen: entering pktgen_stop\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036
Luiz Capitulino222f1802006-03-20 22:16:13 -08003037 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003039 list_for_each_entry(pkt_dev, &t->if_list, list) {
3040 pktgen_stop_device(pkt_dev);
3041 if (pkt_dev->skb)
3042 kfree_skb(pkt_dev->skb);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003043
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003044 pkt_dev->skb = NULL;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003045 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046
Luiz Capitulino222f1802006-03-20 22:16:13 -08003047 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048}
3049
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003050/*
3051 * one of our devices needs to be removed - find it
3052 * and remove it
3053 */
3054static void pktgen_rem_one_if(struct pktgen_thread *t)
3055{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003056 struct list_head *q, *n;
3057 struct pktgen_dev *cur;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003058
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003059 pr_debug("pktgen: entering pktgen_rem_one_if\n");
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003060
3061 if_lock(t);
3062
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003063 list_for_each_safe(q, n, &t->if_list) {
3064 cur = list_entry(q, struct pktgen_dev, list);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003065
Luiz Capitulino222f1802006-03-20 22:16:13 -08003066 if (!cur->removal_mark)
3067 continue;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003068
3069 if (cur->skb)
3070 kfree_skb(cur->skb);
3071 cur->skb = NULL;
3072
3073 pktgen_remove_device(t, cur);
3074
3075 break;
3076 }
3077
3078 if_unlock(t);
3079}
3080
Luiz Capitulino222f1802006-03-20 22:16:13 -08003081static void pktgen_rem_all_ifs(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003083 struct list_head *q, *n;
3084 struct pktgen_dev *cur;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003085
3086 /* Remove all devices, free mem */
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003087
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003088 pr_debug("pktgen: entering pktgen_rem_all_ifs\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003089 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003091 list_for_each_safe(q, n, &t->if_list) {
3092 cur = list_entry(q, struct pktgen_dev, list);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003093
3094 if (cur->skb)
3095 kfree_skb(cur->skb);
3096 cur->skb = NULL;
3097
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 pktgen_remove_device(t, cur);
3099 }
3100
Luiz Capitulino222f1802006-03-20 22:16:13 -08003101 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102}
3103
Luiz Capitulino222f1802006-03-20 22:16:13 -08003104static void pktgen_rem_thread(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003106 /* Remove from the thread list */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107
David S. Milleree74baa2007-01-01 20:51:53 -08003108 remove_proc_entry(t->tsk->comm, pg_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003110 mutex_lock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003112 list_del(&t->th_list);
3113
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003114 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115}
3116
3117static __inline__ void pktgen_xmit(struct pktgen_dev *pkt_dev)
3118{
3119 struct net_device *odev = NULL;
3120 __u64 idle_start = 0;
3121 int ret;
3122
3123 odev = pkt_dev->odev;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003124
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 if (pkt_dev->delay_us || pkt_dev->delay_ns) {
3126 u64 now;
3127
3128 now = getCurUs();
3129 if (now < pkt_dev->next_tx_us)
3130 spin(pkt_dev, pkt_dev->next_tx_us);
3131
3132 /* This is max DELAY, this has special meaning of
3133 * "never transmit"
3134 */
3135 if (pkt_dev->delay_us == 0x7FFFFFFF) {
3136 pkt_dev->next_tx_us = getCurUs() + pkt_dev->delay_us;
3137 pkt_dev->next_tx_ns = pkt_dev->delay_ns;
3138 goto out;
3139 }
3140 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003141
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 if (netif_queue_stopped(odev) || need_resched()) {
3143 idle_start = getCurUs();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003144
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 if (!netif_running(odev)) {
3146 pktgen_stop_device(pkt_dev);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003147 if (pkt_dev->skb)
3148 kfree_skb(pkt_dev->skb);
3149 pkt_dev->skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 goto out;
3151 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003152 if (need_resched())
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 schedule();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003154
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 pkt_dev->idle_acc += getCurUs() - idle_start;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003156
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 if (netif_queue_stopped(odev)) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08003158 pkt_dev->next_tx_us = getCurUs(); /* TODO */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 pkt_dev->next_tx_ns = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003160 goto out; /* Try the next interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 }
3162 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003163
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 if (pkt_dev->last_ok || !pkt_dev->skb) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08003165 if ((++pkt_dev->clone_count >= pkt_dev->clone_skb)
3166 || (!pkt_dev->skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 /* build a new pkt */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003168 if (pkt_dev->skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 kfree_skb(pkt_dev->skb);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003170
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 pkt_dev->skb = fill_packet(odev, pkt_dev);
3172 if (pkt_dev->skb == NULL) {
3173 printk("pktgen: ERROR: couldn't allocate skb in fill_packet.\n");
3174 schedule();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003175 pkt_dev->clone_count--; /* back out increment, OOM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 goto out;
3177 }
3178 pkt_dev->allocated_skbs++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003179 pkt_dev->clone_count = 0; /* reset counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 }
3181 }
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003182
Herbert Xu932ff272006-06-09 12:20:56 -07003183 netif_tx_lock_bh(odev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 if (!netif_queue_stopped(odev)) {
3185
3186 atomic_inc(&(pkt_dev->skb->users));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003187 retry_now:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 ret = odev->hard_start_xmit(pkt_dev->skb, odev);
3189 if (likely(ret == NETDEV_TX_OK)) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08003190 pkt_dev->last_ok = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 pkt_dev->sofar++;
3192 pkt_dev->seq_num++;
3193 pkt_dev->tx_bytes += pkt_dev->cur_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003194
3195 } else if (ret == NETDEV_TX_LOCKED
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 && (odev->features & NETIF_F_LLTX)) {
3197 cpu_relax();
3198 goto retry_now;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003199 } else { /* Retry it next time */
3200
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201 atomic_dec(&(pkt_dev->skb->users));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003202
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 if (debug && net_ratelimit())
3204 printk(KERN_INFO "pktgen: Hard xmit error\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003205
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 pkt_dev->errors++;
3207 pkt_dev->last_ok = 0;
3208 }
3209
3210 pkt_dev->next_tx_us = getCurUs();
3211 pkt_dev->next_tx_ns = 0;
3212
3213 pkt_dev->next_tx_us += pkt_dev->delay_us;
3214 pkt_dev->next_tx_ns += pkt_dev->delay_ns;
3215
3216 if (pkt_dev->next_tx_ns > 1000) {
3217 pkt_dev->next_tx_us++;
3218 pkt_dev->next_tx_ns -= 1000;
3219 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221
Luiz Capitulino222f1802006-03-20 22:16:13 -08003222 else { /* Retry it next time */
3223 pkt_dev->last_ok = 0;
3224 pkt_dev->next_tx_us = getCurUs(); /* TODO */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 pkt_dev->next_tx_ns = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227
Herbert Xu932ff272006-06-09 12:20:56 -07003228 netif_tx_unlock_bh(odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003229
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 /* If pkt_dev->count is zero, then run forever */
3231 if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
3232 if (atomic_read(&(pkt_dev->skb->users)) != 1) {
3233 idle_start = getCurUs();
3234 while (atomic_read(&(pkt_dev->skb->users)) != 1) {
3235 if (signal_pending(current)) {
3236 break;
3237 }
3238 schedule();
3239 }
3240 pkt_dev->idle_acc += getCurUs() - idle_start;
3241 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003242
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 /* Done with this */
3244 pktgen_stop_device(pkt_dev);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003245 if (pkt_dev->skb)
3246 kfree_skb(pkt_dev->skb);
3247 pkt_dev->skb = NULL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003248 }
3249out:;
3250}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003252/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 * Main loop of the thread goes here
3254 */
3255
David S. Milleree74baa2007-01-01 20:51:53 -08003256static int pktgen_thread_worker(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257{
3258 DEFINE_WAIT(wait);
David S. Milleree74baa2007-01-01 20:51:53 -08003259 struct pktgen_thread *t = arg;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003260 struct pktgen_dev *pkt_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 int cpu = t->cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 u32 max_before_softirq;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003263 u32 tx_since_softirq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264
David S. Milleree74baa2007-01-01 20:51:53 -08003265 BUG_ON(smp_processor_id() != cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266
3267 init_waitqueue_head(&t->queue);
3268
Luiz Capitulino222f1802006-03-20 22:16:13 -08003269 t->pid = current->pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003271 pr_debug("pktgen: starting pktgen/%d: pid=%d\n", cpu, current->pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272
3273 max_before_softirq = t->max_before_softirq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274
David S. Milleree74baa2007-01-01 20:51:53 -08003275 set_current_state(TASK_INTERRUPTIBLE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003276
David S. Milleree74baa2007-01-01 20:51:53 -08003277 while (!kthread_should_stop()) {
3278 pkt_dev = next_to_run(t);
3279
3280 if (!pkt_dev &&
3281 (t->control & (T_STOP | T_RUN | T_REMDEVALL | T_REMDEV))
3282 == 0) {
3283 prepare_to_wait(&(t->queue), &wait,
3284 TASK_INTERRUPTIBLE);
3285 schedule_timeout(HZ / 10);
3286 finish_wait(&(t->queue), &wait);
3287 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003288
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 __set_current_state(TASK_RUNNING);
3290
Luiz Capitulino222f1802006-03-20 22:16:13 -08003291 if (pkt_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292
3293 pktgen_xmit(pkt_dev);
3294
3295 /*
3296 * We like to stay RUNNING but must also give
3297 * others fair share.
3298 */
3299
3300 tx_since_softirq += pkt_dev->last_ok;
3301
3302 if (tx_since_softirq > max_before_softirq) {
3303 if (local_softirq_pending())
3304 do_softirq();
3305 tx_since_softirq = 0;
3306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 }
3308
Luiz Capitulino222f1802006-03-20 22:16:13 -08003309 if (t->control & T_STOP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310 pktgen_stop(t);
3311 t->control &= ~(T_STOP);
3312 }
3313
Luiz Capitulino222f1802006-03-20 22:16:13 -08003314 if (t->control & T_RUN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 pktgen_run(t);
3316 t->control &= ~(T_RUN);
3317 }
3318
Luiz Capitulino222f1802006-03-20 22:16:13 -08003319 if (t->control & T_REMDEVALL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 pktgen_rem_all_ifs(t);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003321 t->control &= ~(T_REMDEVALL);
3322 }
3323
Luiz Capitulino222f1802006-03-20 22:16:13 -08003324 if (t->control & T_REMDEV) {
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003325 pktgen_rem_one_if(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326 t->control &= ~(T_REMDEV);
3327 }
3328
Andrew Morton09fe3ef2007-04-12 14:45:32 -07003329 try_to_freeze();
3330
David S. Milleree74baa2007-01-01 20:51:53 -08003331 set_current_state(TASK_INTERRUPTIBLE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003332 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003334 pr_debug("pktgen: %s stopping all device\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003335 pktgen_stop(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003337 pr_debug("pktgen: %s removing all device\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003338 pktgen_rem_all_ifs(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003340 pr_debug("pktgen: %s removing thread.\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003341 pktgen_rem_thread(t);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003342
David S. Milleree74baa2007-01-01 20:51:53 -08003343 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344}
3345
Luiz Capitulino222f1802006-03-20 22:16:13 -08003346static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
3347 const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003349 struct pktgen_dev *p, *pkt_dev = NULL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003350 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003352 list_for_each_entry(p, &t->if_list, list)
Stephen Hemminger39df2322007-03-04 16:11:51 -08003353 if (strncmp(p->odev->name, ifname, IFNAMSIZ) == 0) {
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003354 pkt_dev = p;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003355 break;
3356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357
Luiz Capitulino222f1802006-03-20 22:16:13 -08003358 if_unlock(t);
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003359 pr_debug("pktgen: find_dev(%s) returning %p\n", ifname, pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003360 return pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361}
3362
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003363/*
3364 * Adds a dev at front of if_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 */
3366
Luiz Capitulino222f1802006-03-20 22:16:13 -08003367static int add_dev_to_thread(struct pktgen_thread *t,
3368 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369{
3370 int rv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371
Luiz Capitulino222f1802006-03-20 22:16:13 -08003372 if_lock(t);
3373
3374 if (pkt_dev->pg_thread) {
3375 printk("pktgen: ERROR: already assigned to a thread.\n");
3376 rv = -EBUSY;
3377 goto out;
3378 }
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003379
3380 list_add(&pkt_dev->list, &t->if_list);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003381 pkt_dev->pg_thread = t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 pkt_dev->running = 0;
3383
Luiz Capitulino222f1802006-03-20 22:16:13 -08003384out:
3385 if_unlock(t);
3386 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387}
3388
3389/* Called under thread lock */
3390
Luiz Capitulino222f1802006-03-20 22:16:13 -08003391static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003393 struct pktgen_dev *pkt_dev;
Stephen Hemminger39df2322007-03-04 16:11:51 -08003394 int err;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003395
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 /* We don't allow a device to be on several threads */
3397
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003398 pkt_dev = __pktgen_NN_threads(ifname, FIND);
3399 if (pkt_dev) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08003400 printk("pktgen: ERROR: interface already used.\n");
3401 return -EBUSY;
3402 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003403
3404 pkt_dev = kzalloc(sizeof(struct pktgen_dev), GFP_KERNEL);
3405 if (!pkt_dev)
3406 return -ENOMEM;
3407
Luiz Capitulino222f1802006-03-20 22:16:13 -08003408 pkt_dev->flows = vmalloc(MAX_CFLOWS * sizeof(struct flow_state));
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003409 if (pkt_dev->flows == NULL) {
3410 kfree(pkt_dev);
3411 return -ENOMEM;
3412 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003413 memset(pkt_dev->flows, 0, MAX_CFLOWS * sizeof(struct flow_state));
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003414
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003415 pkt_dev->removal_mark = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003416 pkt_dev->min_pkt_size = ETH_ZLEN;
3417 pkt_dev->max_pkt_size = ETH_ZLEN;
3418 pkt_dev->nfrags = 0;
3419 pkt_dev->clone_skb = pg_clone_skb_d;
3420 pkt_dev->delay_us = pg_delay_d / 1000;
3421 pkt_dev->delay_ns = pg_delay_d % 1000;
3422 pkt_dev->count = pg_count_d;
3423 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003424 pkt_dev->udp_src_min = 9; /* sink port */
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003425 pkt_dev->udp_src_max = 9;
3426 pkt_dev->udp_dst_min = 9;
3427 pkt_dev->udp_dst_max = 9;
3428
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003429 pkt_dev->vlan_p = 0;
3430 pkt_dev->vlan_cfi = 0;
3431 pkt_dev->vlan_id = 0xffff;
3432 pkt_dev->svlan_p = 0;
3433 pkt_dev->svlan_cfi = 0;
3434 pkt_dev->svlan_id = 0xffff;
3435
Stephen Hemminger39df2322007-03-04 16:11:51 -08003436 err = pktgen_setup_dev(pkt_dev, ifname);
3437 if (err)
3438 goto out1;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003439
Stephen Hemminger39df2322007-03-04 16:11:51 -08003440 pkt_dev->entry = create_proc_entry(ifname, 0600, pg_proc_dir);
3441 if (!pkt_dev->entry) {
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003442 printk("pktgen: cannot create %s/%s procfs entry.\n",
3443 PG_PROC_DIR, ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003444 err = -EINVAL;
3445 goto out2;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003446 }
Stephen Hemminger39df2322007-03-04 16:11:51 -08003447 pkt_dev->entry->proc_fops = &pktgen_if_fops;
3448 pkt_dev->entry->data = pkt_dev;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003449
3450 return add_dev_to_thread(t, pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003451out2:
3452 dev_put(pkt_dev->odev);
3453out1:
3454 if (pkt_dev->flows)
3455 vfree(pkt_dev->flows);
3456 kfree(pkt_dev);
3457 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458}
3459
David S. Milleree74baa2007-01-01 20:51:53 -08003460static int __init pktgen_create_thread(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003462 struct pktgen_thread *t;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003463 struct proc_dir_entry *pe;
David S. Milleree74baa2007-01-01 20:51:53 -08003464 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465
Luiz Capitulino222f1802006-03-20 22:16:13 -08003466 t = kzalloc(sizeof(struct pktgen_thread), GFP_KERNEL);
3467 if (!t) {
3468 printk("pktgen: ERROR: out of memory, can't create new thread.\n");
3469 return -ENOMEM;
3470 }
3471
Luiz Capitulino222f1802006-03-20 22:16:13 -08003472 spin_lock_init(&t->if_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473 t->cpu = cpu;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003474
David S. Milleree74baa2007-01-01 20:51:53 -08003475 INIT_LIST_HEAD(&t->if_list);
3476
3477 list_add_tail(&t->th_list, &pktgen_threads);
3478
3479 p = kthread_create(pktgen_thread_worker, t, "kpktgend_%d", cpu);
3480 if (IS_ERR(p)) {
3481 printk("pktgen: kernel_thread() failed for cpu %d\n", t->cpu);
3482 list_del(&t->th_list);
3483 kfree(t);
3484 return PTR_ERR(p);
3485 }
3486 kthread_bind(p, cpu);
3487 t->tsk = p;
3488
3489 pe = create_proc_entry(t->tsk->comm, 0600, pg_proc_dir);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003490 if (!pe) {
3491 printk("pktgen: cannot create %s/%s procfs entry.\n",
David S. Milleree74baa2007-01-01 20:51:53 -08003492 PG_PROC_DIR, t->tsk->comm);
3493 kthread_stop(p);
3494 list_del(&t->th_list);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003495 kfree(t);
3496 return -EINVAL;
3497 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003498
3499 pe->proc_fops = &pktgen_thread_fops;
3500 pe->data = t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501
David S. Milleree74baa2007-01-01 20:51:53 -08003502 wake_up_process(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503
3504 return 0;
3505}
3506
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003507/*
3508 * Removes a device from the thread if_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003510static void _rem_dev_from_if_list(struct pktgen_thread *t,
3511 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003513 struct list_head *q, *n;
3514 struct pktgen_dev *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003516 list_for_each_safe(q, n, &t->if_list) {
3517 p = list_entry(q, struct pktgen_dev, list);
3518 if (p == pkt_dev)
3519 list_del(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520 }
3521}
3522
Luiz Capitulino222f1802006-03-20 22:16:13 -08003523static int pktgen_remove_device(struct pktgen_thread *t,
3524 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525{
3526
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003527 pr_debug("pktgen: remove_device pkt_dev=%p\n", pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528
Luiz Capitulino222f1802006-03-20 22:16:13 -08003529 if (pkt_dev->running) {
3530 printk("pktgen:WARNING: trying to remove a running interface, stopping it now.\n");
3531 pktgen_stop_device(pkt_dev);
3532 }
3533
3534 /* Dis-associate from the interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535
3536 if (pkt_dev->odev) {
3537 dev_put(pkt_dev->odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003538 pkt_dev->odev = NULL;
3539 }
3540
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 /* And update the thread if_list */
3542
3543 _rem_dev_from_if_list(t, pkt_dev);
3544
Stephen Hemminger39df2322007-03-04 16:11:51 -08003545 if (pkt_dev->entry)
3546 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547
3548 if (pkt_dev->flows)
3549 vfree(pkt_dev->flows);
3550 kfree(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003551 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552}
3553
Luiz Capitulino222f1802006-03-20 22:16:13 -08003554static int __init pg_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555{
3556 int cpu;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003557 struct proc_dir_entry *pe;
3558
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559 printk(version);
3560
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003561 pg_proc_dir = proc_mkdir(PG_PROC_DIR, proc_net);
3562 if (!pg_proc_dir)
3563 return -ENODEV;
3564 pg_proc_dir->owner = THIS_MODULE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003566 pe = create_proc_entry(PGCTRL, 0600, pg_proc_dir);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003567 if (pe == NULL) {
3568 printk("pktgen: ERROR: cannot create %s procfs entry.\n",
3569 PGCTRL);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003570 proc_net_remove(PG_PROC_DIR);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003571 return -EINVAL;
3572 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573
Luiz Capitulino222f1802006-03-20 22:16:13 -08003574 pe->proc_fops = &pktgen_fops;
3575 pe->data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576
3577 /* Register us to receive netdevice events */
3578 register_netdevice_notifier(&pktgen_notifier_block);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003579
John Hawkes670c02c2005-10-13 09:30:31 -07003580 for_each_online_cpu(cpu) {
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003581 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582
David S. Milleree74baa2007-01-01 20:51:53 -08003583 err = pktgen_create_thread(cpu);
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003584 if (err)
3585 printk("pktgen: WARNING: Cannot create thread for cpu %d (%d)\n",
3586 cpu, err);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003587 }
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003588
3589 if (list_empty(&pktgen_threads)) {
3590 printk("pktgen: ERROR: Initialization failed for all threads\n");
3591 unregister_netdevice_notifier(&pktgen_notifier_block);
3592 remove_proc_entry(PGCTRL, pg_proc_dir);
3593 proc_net_remove(PG_PROC_DIR);
3594 return -ENODEV;
3595 }
3596
Luiz Capitulino222f1802006-03-20 22:16:13 -08003597 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598}
3599
3600static void __exit pg_cleanup(void)
3601{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003602 struct pktgen_thread *t;
3603 struct list_head *q, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604 wait_queue_head_t queue;
3605 init_waitqueue_head(&queue);
3606
Luiz Capitulino222f1802006-03-20 22:16:13 -08003607 /* Stop all interfaces & threads */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003609 list_for_each_safe(q, n, &pktgen_threads) {
3610 t = list_entry(q, struct pktgen_thread, th_list);
David S. Milleree74baa2007-01-01 20:51:53 -08003611 kthread_stop(t->tsk);
3612 kfree(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003613 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614
Luiz Capitulino222f1802006-03-20 22:16:13 -08003615 /* Un-register us from receiving netdevice events */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 unregister_netdevice_notifier(&pktgen_notifier_block);
3617
Luiz Capitulino222f1802006-03-20 22:16:13 -08003618 /* Clean up proc file system */
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003619 remove_proc_entry(PGCTRL, pg_proc_dir);
3620 proc_net_remove(PG_PROC_DIR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621}
3622
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623module_init(pg_init);
3624module_exit(pg_cleanup);
3625
3626MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se");
3627MODULE_DESCRIPTION("Packet Generator tool");
3628MODULE_LICENSE("GPL");
3629module_param(pg_count_d, int, 0);
3630module_param(pg_delay_d, int, 0);
3631module_param(pg_clone_skb_d, int, 0);
3632module_param(debug, int, 0);