blob: b92a322872a8caadbfb592f912393461397d94c8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Authors:
3 * Copyright 2001, 2002 by Robert Olsson <robert.olsson@its.uu.se>
4 * Uppsala University and
5 * Swedish University of Agricultural Sciences
6 *
7 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
8 * Ben Greear <greearb@candelatech.com>
9 * Jens Låås <jens.laas@data.slu.se>
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version
14 * 2 of the License, or (at your option) any later version.
15 *
16 *
17 * A tool for loading the network with preconfigurated packets.
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090018 * The tool is implemented as a linux module. Parameters are output
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * device, delay (to hard_xmit), number of packets, and whether
20 * to use multiple SKBs or just the same one.
21 * pktgen uses the installed interface's output routine.
22 *
23 * Additional hacking by:
24 *
25 * Jens.Laas@data.slu.se
26 * Improved by ANK. 010120.
27 * Improved by ANK even more. 010212.
28 * MAC address typo fixed. 010417 --ro
29 * Integrated. 020301 --DaveM
30 * Added multiskb option 020301 --DaveM
31 * Scaling of results. 020417--sigurdur@linpro.no
32 * Significant re-work of the module:
33 * * Convert to threaded model to more efficiently be able to transmit
34 * and receive on multiple interfaces at once.
35 * * Converted many counters to __u64 to allow longer runs.
36 * * Allow configuration of ranges, like min/max IP address, MACs,
37 * and UDP-ports, for both source and destination, and can
38 * set to use a random distribution or sequentially walk the range.
39 * * Can now change most values after starting.
40 * * Place 12-byte packet in UDP payload with magic number,
41 * sequence number, and timestamp.
42 * * Add receiver code that detects dropped pkts, re-ordered pkts, and
43 * latencies (with micro-second) precision.
44 * * Add IOCTL interface to easily get counters & configuration.
45 * --Ben Greear <greearb@candelatech.com>
46 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090047 * Renamed multiskb to clone_skb and cleaned up sending core for two distinct
48 * skb modes. A clone_skb=0 mode for Ben "ranges" work and a clone_skb != 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 * as a "fastpath" with a configurable number of clones after alloc's.
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090050 * clone_skb=0 means all packets are allocated this also means ranges time
51 * stamps etc can be used. clone_skb=100 means 1 malloc is followed by 100
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 * clones.
53 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090054 * Also moved to /proc/net/pktgen/
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 * --ro
56 *
57 * Sept 10: Fixed threading/locking. Lots of bone-headed and more clever
58 * mistakes. Also merged in DaveM's patch in the -pre6 patch.
59 * --Ben Greear <greearb@candelatech.com>
60 *
61 * Integrated to 2.5.x 021029 --Lucio Maciel (luciomaciel@zipmail.com.br)
62 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090063 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 * 021124 Finished major redesign and rewrite for new functionality.
65 * See Documentation/networking/pktgen.txt for how to use this.
66 *
67 * The new operation:
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090068 * For each CPU one thread/process is created at start. This process checks
69 * for running devices in the if_list and sends packets until count is 0 it
70 * also the thread checks the thread->control which is used for inter-process
71 * communication. controlling process "posts" operations to the threads this
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 * way. The if_lock should be possible to remove when add/rem_device is merged
73 * into this too.
74 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090075 * By design there should only be *one* "controlling" process. In practice
76 * multiple write accesses gives unpredictable result. Understood by "write"
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 * to /proc gives result code thats should be read be the "writer".
Stephen Hemmingerb4099fa2005-10-14 15:32:22 -070078 * For practical use this should be no problem.
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090080 * Note when adding devices to a specific CPU there good idea to also assign
81 * /proc/irq/XX/smp_affinity so TX-interrupts gets bound to the same CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 * --ro
83 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090084 * Fix refcount off by one if first packet fails, potential null deref,
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 * memleak 030710- KJP
86 *
87 * First "ranges" functionality for ipv6 030726 --ro
88 *
89 * Included flow support. 030802 ANK.
90 *
91 * Fixed unaligned access on IA-64 Grant Grundler <grundler@parisc-linux.org>
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090092 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 * Remove if fix from added Harald Welte <laforge@netfilter.org> 040419
94 * ia64 compilation fix from Aron Griffis <aron@hp.com> 040604
95 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090096 * New xmit() return, do_div and misc clean up by Stephen Hemminger
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 * <shemminger@osdl.org> 040923
98 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090099 * Randy Dunlap fixed u64 printk compiler waring
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 *
101 * Remove FCS from BW calculation. Lennert Buytenhek <buytenh@wantstofly.org>
102 * New time handling. Lennert Buytenhek <buytenh@wantstofly.org> 041213
103 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900104 * Corrections from Nikolai Malykh (nmalykh@bilim.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 * Removed unused flags F_SET_SRCMAC & F_SET_SRCIP 041230
106 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900107 * interruptible_sleep_on_timeout() replaced Nishanth Aravamudan <nacc@us.ibm.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 * 050103
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800109 *
110 * MPLS support by Steven Whitehouse <steve@chygwyn.com>
111 *
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700112 * 802.1Q/Q-in-Q support by Francesco Fondelli (FF) <francesco.fondelli@gmail.com>
113 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 */
115#include <linux/sys.h>
116#include <linux/types.h>
117#include <linux/module.h>
118#include <linux/moduleparam.h>
119#include <linux/kernel.h>
120#include <linux/smp_lock.h>
Luiz Capitulino222fa072006-03-20 22:24:27 -0800121#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122#include <linux/sched.h>
123#include <linux/slab.h>
124#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125#include <linux/unistd.h>
126#include <linux/string.h>
127#include <linux/ptrace.h>
128#include <linux/errno.h>
129#include <linux/ioport.h>
130#include <linux/interrupt.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -0800131#include <linux/capability.h>
Andrew Morton09fe3ef2007-04-12 14:45:32 -0700132#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133#include <linux/delay.h>
134#include <linux/timer.h>
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -0800135#include <linux/list.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136#include <linux/init.h>
137#include <linux/skbuff.h>
138#include <linux/netdevice.h>
139#include <linux/inet.h>
140#include <linux/inetdevice.h>
141#include <linux/rtnetlink.h>
142#include <linux/if_arp.h>
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700143#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144#include <linux/in.h>
145#include <linux/ip.h>
146#include <linux/ipv6.h>
147#include <linux/udp.h>
148#include <linux/proc_fs.h>
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700149#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150#include <linux/wait.h>
Kris Katterjohnf404e9a2006-01-17 13:04:57 -0800151#include <linux/etherdevice.h>
David S. Milleree74baa2007-01-01 20:51:53 -0800152#include <linux/kthread.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153#include <net/checksum.h>
154#include <net/ipv6.h>
155#include <net/addrconf.h>
156#include <asm/byteorder.h>
157#include <linux/rcupdate.h>
158#include <asm/bitops.h>
159#include <asm/io.h>
160#include <asm/dma.h>
161#include <asm/uaccess.h>
Luiz Capitulino222f1802006-03-20 22:16:13 -0800162#include <asm/div64.h> /* do_div */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163#include <asm/timex.h>
164
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700165#define VERSION "pktgen v2.68: Packet Generator for packet performance testing.\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167/* The buckets are exponential in 'width' */
168#define LAT_BUCKETS_MAX 32
169#define IP_NAME_SZ 32
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800170#define MAX_MPLS_LABELS 16 /* This is the max label stack depth */
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -0800171#define MPLS_STACK_BOTTOM htonl(0x00000100)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173/* Device flag bits */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800174#define F_IPSRC_RND (1<<0) /* IP-Src Random */
175#define F_IPDST_RND (1<<1) /* IP-Dst Random */
176#define F_UDPSRC_RND (1<<2) /* UDP-Src Random */
177#define F_UDPDST_RND (1<<3) /* UDP-Dst Random */
178#define F_MACSRC_RND (1<<4) /* MAC-Src Random */
179#define F_MACDST_RND (1<<5) /* MAC-Dst Random */
180#define F_TXSIZE_RND (1<<6) /* Transmit size is random */
181#define F_IPV6 (1<<7) /* Interface in IPV6 Mode */
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800182#define F_MPLS_RND (1<<8) /* Random MPLS labels */
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700183#define F_VID_RND (1<<9) /* Random VLAN ID */
184#define F_SVID_RND (1<<10) /* Random SVLAN ID */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186/* Thread control flag bits */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800187#define T_TERMINATE (1<<0)
188#define T_STOP (1<<1) /* Stop run */
189#define T_RUN (1<<2) /* Start run */
190#define T_REMDEVALL (1<<3) /* Remove all devs */
191#define T_REMDEV (1<<4) /* Remove one dev */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193/* If lock -- can be removed after some work */
194#define if_lock(t) spin_lock(&(t->if_lock));
195#define if_unlock(t) spin_unlock(&(t->if_lock));
196
197/* Used to help with determining the pkts on receive */
198#define PKTGEN_MAGIC 0xbe9be955
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700199#define PG_PROC_DIR "pktgen"
200#define PGCTRL "pgctrl"
201static struct proc_dir_entry *pg_proc_dir = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203#define MAX_CFLOWS 65536
204
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700205#define VLAN_TAG_SIZE(x) ((x)->vlan_id == 0xffff ? 0 : 4)
206#define SVLAN_TAG_SIZE(x) ((x)->svlan_id == 0xffff ? 0 : 4)
207
Luiz Capitulino222f1802006-03-20 22:16:13 -0800208struct flow_state {
Al Viro252e3342006-11-14 20:48:11 -0800209 __be32 cur_daddr;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800210 int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211};
212
213struct pktgen_dev {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 /*
215 * Try to keep frequent/infrequent used vars. separated.
216 */
Stephen Hemminger39df2322007-03-04 16:11:51 -0800217 struct proc_dir_entry *entry; /* proc file */
218 struct pktgen_thread *pg_thread;/* the owner */
Luiz Capitulinoc26a8012006-03-20 22:18:16 -0800219 struct list_head list; /* Used for chaining in the thread's run-queue */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Luiz Capitulino222f1802006-03-20 22:16:13 -0800221 int running; /* if this changes to false, the test will stop */
222
223 /* If min != max, then we will either do a linear iteration, or
224 * we will do a random selection from within the range.
225 */
226 __u32 flags;
Arthur Kepner95ed63f2006-03-20 21:26:56 -0800227 int removal_mark; /* non-zero => the device is marked for
228 * removal by worker thread */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Luiz Capitulino222f1802006-03-20 22:16:13 -0800230 int min_pkt_size; /* = ETH_ZLEN; */
231 int max_pkt_size; /* = ETH_ZLEN; */
232 int nfrags;
233 __u32 delay_us; /* Default delay */
234 __u32 delay_ns;
235 __u64 count; /* Default No packets to send */
236 __u64 sofar; /* How many pkts we've sent so far */
237 __u64 tx_bytes; /* How many bytes we've transmitted */
238 __u64 errors; /* Errors when trying to transmit, pkts will be re-sent */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Luiz Capitulino222f1802006-03-20 22:16:13 -0800240 /* runtime counters relating to clone_skb */
241 __u64 next_tx_us; /* timestamp of when to tx next */
242 __u32 next_tx_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Luiz Capitulino222f1802006-03-20 22:16:13 -0800244 __u64 allocated_skbs;
245 __u32 clone_count;
246 int last_ok; /* Was last skb sent?
247 * Or a failed transmit of some sort? This will keep
248 * sequence numbers in order, for example.
249 */
250 __u64 started_at; /* micro-seconds */
251 __u64 stopped_at; /* micro-seconds */
252 __u64 idle_acc; /* micro-seconds */
253 __u32 seq_num;
254
255 int clone_skb; /* Use multiple SKBs during packet gen. If this number
256 * is greater than 1, then that many copies of the same
257 * packet will be sent before a new packet is allocated.
258 * For instance, if you want to send 1024 identical packets
259 * before creating a new packet, set clone_skb to 1024.
260 */
261
262 char dst_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
263 char dst_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
264 char src_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
265 char src_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
266
267 struct in6_addr in6_saddr;
268 struct in6_addr in6_daddr;
269 struct in6_addr cur_in6_daddr;
270 struct in6_addr cur_in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 /* For ranges */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800272 struct in6_addr min_in6_daddr;
273 struct in6_addr max_in6_daddr;
274 struct in6_addr min_in6_saddr;
275 struct in6_addr max_in6_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Luiz Capitulino222f1802006-03-20 22:16:13 -0800277 /* If we're doing ranges, random or incremental, then this
278 * defines the min/max for those ranges.
279 */
Al Viro252e3342006-11-14 20:48:11 -0800280 __be32 saddr_min; /* inclusive, source IP address */
281 __be32 saddr_max; /* exclusive, source IP address */
282 __be32 daddr_min; /* inclusive, dest IP address */
283 __be32 daddr_max; /* exclusive, dest IP address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Luiz Capitulino222f1802006-03-20 22:16:13 -0800285 __u16 udp_src_min; /* inclusive, source UDP port */
286 __u16 udp_src_max; /* exclusive, source UDP port */
287 __u16 udp_dst_min; /* inclusive, dest UDP port */
288 __u16 udp_dst_max; /* exclusive, dest UDP port */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700290 /* DSCP + ECN */
291 __u8 tos; /* six most significant bits of (former) IPv4 TOS are for dscp codepoint */
292 __u8 traffic_class; /* ditto for the (former) Traffic Class in IPv6 (see RFC 3260, sec. 4) */
293
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800294 /* MPLS */
295 unsigned nr_labels; /* Depth of stack, 0 = no MPLS */
296 __be32 labels[MAX_MPLS_LABELS];
297
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700298 /* VLAN/SVLAN (802.1Q/Q-in-Q) */
299 __u8 vlan_p;
300 __u8 vlan_cfi;
301 __u16 vlan_id; /* 0xffff means no vlan tag */
302
303 __u8 svlan_p;
304 __u8 svlan_cfi;
305 __u16 svlan_id; /* 0xffff means no svlan tag */
306
Luiz Capitulino222f1802006-03-20 22:16:13 -0800307 __u32 src_mac_count; /* How many MACs to iterate through */
308 __u32 dst_mac_count; /* How many MACs to iterate through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Luiz Capitulino222f1802006-03-20 22:16:13 -0800310 unsigned char dst_mac[ETH_ALEN];
311 unsigned char src_mac[ETH_ALEN];
312
313 __u32 cur_dst_mac_offset;
314 __u32 cur_src_mac_offset;
Al Viro252e3342006-11-14 20:48:11 -0800315 __be32 cur_saddr;
316 __be32 cur_daddr;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800317 __u16 cur_udp_dst;
318 __u16 cur_udp_src;
319 __u32 cur_pkt_size;
320
321 __u8 hh[14];
322 /* = {
323 0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
324
325 We fill in SRC address later
326 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
327 0x08, 0x00
328 };
329 */
330 __u16 pad; /* pad out the hh struct to an even 16 bytes */
331
332 struct sk_buff *skb; /* skb we are to transmit next, mainly used for when we
333 * are transmitting the same one multiple times
334 */
335 struct net_device *odev; /* The out-going device. Note that the device should
336 * have it's pg_info pointer pointing back to this
337 * device. This will be set when the user specifies
338 * the out-going device name (not when the inject is
339 * started as it used to do.)
340 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 struct flow_state *flows;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800342 unsigned cflows; /* Concurrent flows (config) */
343 unsigned lflow; /* Flow length (config) */
344 unsigned nflows; /* accumulated flows (stats) */
Stephen Hemminger39df2322007-03-04 16:11:51 -0800345
346 char result[512];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347};
348
349struct pktgen_hdr {
Al Viro252e3342006-11-14 20:48:11 -0800350 __be32 pgh_magic;
351 __be32 seq_num;
352 __be32 tv_sec;
353 __be32 tv_usec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354};
355
356struct pktgen_thread {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800357 spinlock_t if_lock;
Luiz Capitulinoc26a8012006-03-20 22:18:16 -0800358 struct list_head if_list; /* All device here */
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -0800359 struct list_head th_list;
David S. Milleree74baa2007-01-01 20:51:53 -0800360 struct task_struct *tsk;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800361 char result[512];
362 u32 max_before_softirq; /* We'll call do_softirq to prevent starvation. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Luiz Capitulino222f1802006-03-20 22:16:13 -0800364 /* Field for thread to receive "posted" events terminate, stop ifs etc. */
365
366 u32 control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 int pid;
368 int cpu;
369
Luiz Capitulino222f1802006-03-20 22:16:13 -0800370 wait_queue_head_t queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371};
372
373#define REMOVE 1
374#define FIND 0
375
376/* This code works around the fact that do_div cannot handle two 64-bit
377 numbers, and regular 64-bit division doesn't work on x86 kernels.
378 --Ben
379*/
380
381#define PG_DIV 0
382
383/* This was emailed to LMKL by: Chris Caputo <ccaputo@alt.net>
384 * Function copied/adapted/optimized from:
385 *
386 * nemesis.sourceforge.net/browse/lib/static/intmath/ix86/intmath.c.html
387 *
388 * Copyright 1994, University of Cambridge Computer Laboratory
389 * All Rights Reserved.
390 *
391 */
Jesper Juhl77933d72005-07-27 11:46:09 -0700392static inline s64 divremdi3(s64 x, s64 y, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800394 u64 a = (x < 0) ? -x : x;
395 u64 b = (y < 0) ? -y : y;
396 u64 res = 0, d = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Luiz Capitulino222f1802006-03-20 22:16:13 -0800398 if (b > 0) {
399 while (b < a) {
400 b <<= 1;
401 d <<= 1;
402 }
403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Luiz Capitulino222f1802006-03-20 22:16:13 -0800405 do {
406 if (a >= b) {
407 a -= b;
408 res += d;
409 }
410 b >>= 1;
411 d >>= 1;
412 }
413 while (d);
414
415 if (PG_DIV == type) {
416 return (((x ^ y) & (1ll << 63)) == 0) ? res : -(s64) res;
417 } else {
418 return ((x & (1ll << 63)) == 0) ? a : -(s64) a;
419 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420}
421
422/* End of hacks to deal with 64-bit math on x86 */
423
Stephen Hemmingerb4099fa2005-10-14 15:32:22 -0700424/** Convert to milliseconds */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800425static inline __u64 tv_to_ms(const struct timeval *tv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800427 __u64 ms = tv->tv_usec / 1000;
428 ms += (__u64) tv->tv_sec * (__u64) 1000;
429 return ms;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430}
431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432/** Convert to micro-seconds */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800433static inline __u64 tv_to_us(const struct timeval *tv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800435 __u64 us = tv->tv_usec;
436 us += (__u64) tv->tv_sec * (__u64) 1000000;
437 return us;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438}
439
Luiz Capitulino222f1802006-03-20 22:16:13 -0800440static inline __u64 pg_div(__u64 n, __u32 base)
441{
442 __u64 tmp = n;
443 do_div(tmp, base);
444 /* printk("pktgen: pg_div, n: %llu base: %d rv: %llu\n",
445 n, base, tmp); */
446 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447}
448
Luiz Capitulino222f1802006-03-20 22:16:13 -0800449static inline __u64 pg_div64(__u64 n, __u64 base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800451 __u64 tmp = n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452/*
Stephen Hemmingerb4099fa2005-10-14 15:32:22 -0700453 * How do we know if the architecture we are running on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 * supports division with 64 bit base?
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900455 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800457#if defined(__sparc_v9__) || defined(__powerpc64__) || defined(__alpha__) || defined(__x86_64__) || defined(__ia64__)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Luiz Capitulino222f1802006-03-20 22:16:13 -0800459 do_div(tmp, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460#else
Luiz Capitulino222f1802006-03-20 22:16:13 -0800461 tmp = divremdi3(n, base, PG_DIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462#endif
Luiz Capitulino222f1802006-03-20 22:16:13 -0800463 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464}
465
Luiz Capitulino222f1802006-03-20 22:16:13 -0800466static inline __u64 getCurMs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800468 struct timeval tv;
469 do_gettimeofday(&tv);
470 return tv_to_ms(&tv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471}
472
Luiz Capitulino222f1802006-03-20 22:16:13 -0800473static inline __u64 getCurUs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800475 struct timeval tv;
476 do_gettimeofday(&tv);
477 return tv_to_us(&tv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478}
479
Luiz Capitulino222f1802006-03-20 22:16:13 -0800480static inline __u64 tv_diff(const struct timeval *a, const struct timeval *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800482 return tv_to_us(a) - tv_to_us(b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483}
484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485/* old include end */
486
487static char version[] __initdata = VERSION;
488
Luiz Capitulino222f1802006-03-20 22:16:13 -0800489static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *i);
490static int pktgen_add_device(struct pktgen_thread *t, const char *ifname);
491static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
492 const char *ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493static int pktgen_device_event(struct notifier_block *, unsigned long, void *);
494static void pktgen_run_all_threads(void);
495static void pktgen_stop_all_threads_ifs(void);
496static int pktgen_stop_device(struct pktgen_dev *pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800497static void pktgen_stop(struct pktgen_thread *t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498static void pktgen_clear_counters(struct pktgen_dev *pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -0800499
Luiz Capitulino222f1802006-03-20 22:16:13 -0800500static unsigned int scan_ip6(const char *s, char ip[16]);
501static unsigned int fmt_ip6(char *s, const char ip[16]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
503/* Module parameters, defaults. */
Luiz Capitulino222f1802006-03-20 22:16:13 -0800504static int pg_count_d = 1000; /* 1000 pkts by default */
Jaco Kroonf34fbb972005-12-22 12:51:46 -0800505static int pg_delay_d;
506static int pg_clone_skb_d;
507static int debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Luiz Capitulino222fa072006-03-20 22:24:27 -0800509static DEFINE_MUTEX(pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -0800510static LIST_HEAD(pktgen_threads);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512static struct notifier_block pktgen_notifier_block = {
513 .notifier_call = pktgen_device_event,
514};
515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516/*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900517 * /proc handling functions
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 *
519 */
520
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700521static int pgctrl_show(struct seq_file *seq, void *v)
Luiz Capitulino222f1802006-03-20 22:16:13 -0800522{
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700523 seq_puts(seq, VERSION);
524 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525}
526
Luiz Capitulino222f1802006-03-20 22:16:13 -0800527static ssize_t pgctrl_write(struct file *file, const char __user * buf,
528 size_t count, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 int err = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700531 char data[128];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Luiz Capitulino222f1802006-03-20 22:16:13 -0800533 if (!capable(CAP_NET_ADMIN)) {
534 err = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 goto out;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800536 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700538 if (count > sizeof(data))
539 count = sizeof(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 if (copy_from_user(data, buf, count)) {
Stephen Hemmingerb4099fa2005-10-14 15:32:22 -0700542 err = -EFAULT;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700543 goto out;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800544 }
545 data[count - 1] = 0; /* Make string */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Luiz Capitulino222f1802006-03-20 22:16:13 -0800547 if (!strcmp(data, "stop"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 pktgen_stop_all_threads_ifs();
549
Luiz Capitulino222f1802006-03-20 22:16:13 -0800550 else if (!strcmp(data, "start"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 pktgen_run_all_threads();
552
Luiz Capitulino222f1802006-03-20 22:16:13 -0800553 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 printk("pktgen: Unknown command: %s\n", data);
555
556 err = count;
557
Luiz Capitulino222f1802006-03-20 22:16:13 -0800558out:
559 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560}
561
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700562static int pgctrl_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700564 return single_open(file, pgctrl_show, PDE(inode)->data);
565}
566
Arjan van de Ven9a321442007-02-12 00:55:35 -0800567static const struct file_operations pktgen_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800568 .owner = THIS_MODULE,
569 .open = pgctrl_open,
570 .read = seq_read,
571 .llseek = seq_lseek,
572 .write = pgctrl_write,
573 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700574};
575
576static int pktgen_if_show(struct seq_file *seq, void *v)
577{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 int i;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800579 struct pktgen_dev *pkt_dev = seq->private;
580 __u64 sa;
581 __u64 stopped;
582 __u64 now = getCurUs();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
Luiz Capitulino222f1802006-03-20 22:16:13 -0800584 seq_printf(seq,
585 "Params: count %llu min_pkt_size: %u max_pkt_size: %u\n",
586 (unsigned long long)pkt_dev->count, pkt_dev->min_pkt_size,
587 pkt_dev->max_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Luiz Capitulino222f1802006-03-20 22:16:13 -0800589 seq_printf(seq,
590 " frags: %d delay: %u clone_skb: %d ifname: %s\n",
591 pkt_dev->nfrags,
592 1000 * pkt_dev->delay_us + pkt_dev->delay_ns,
Stephen Hemminger39df2322007-03-04 16:11:51 -0800593 pkt_dev->clone_skb, pkt_dev->odev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
Luiz Capitulino222f1802006-03-20 22:16:13 -0800595 seq_printf(seq, " flows: %u flowlen: %u\n", pkt_dev->cflows,
596 pkt_dev->lflow);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Luiz Capitulino222f1802006-03-20 22:16:13 -0800598 if (pkt_dev->flags & F_IPV6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 char b1[128], b2[128], b3[128];
Luiz Capitulino222f1802006-03-20 22:16:13 -0800600 fmt_ip6(b1, pkt_dev->in6_saddr.s6_addr);
601 fmt_ip6(b2, pkt_dev->min_in6_saddr.s6_addr);
602 fmt_ip6(b3, pkt_dev->max_in6_saddr.s6_addr);
603 seq_printf(seq,
604 " saddr: %s min_saddr: %s max_saddr: %s\n", b1,
605 b2, b3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Luiz Capitulino222f1802006-03-20 22:16:13 -0800607 fmt_ip6(b1, pkt_dev->in6_daddr.s6_addr);
608 fmt_ip6(b2, pkt_dev->min_in6_daddr.s6_addr);
609 fmt_ip6(b3, pkt_dev->max_in6_daddr.s6_addr);
610 seq_printf(seq,
611 " daddr: %s min_daddr: %s max_daddr: %s\n", b1,
612 b2, b3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Luiz Capitulino222f1802006-03-20 22:16:13 -0800614 } else
615 seq_printf(seq,
616 " dst_min: %s dst_max: %s\n src_min: %s src_max: %s\n",
617 pkt_dev->dst_min, pkt_dev->dst_max, pkt_dev->src_min,
618 pkt_dev->src_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700620 seq_puts(seq, " src_mac: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Kris Katterjohnf404e9a2006-01-17 13:04:57 -0800622 if (is_zero_ether_addr(pkt_dev->src_mac))
Luiz Capitulino222f1802006-03-20 22:16:13 -0800623 for (i = 0; i < 6; i++)
624 seq_printf(seq, "%02X%s", pkt_dev->odev->dev_addr[i],
625 i == 5 ? " " : ":");
626 else
627 for (i = 0; i < 6; i++)
628 seq_printf(seq, "%02X%s", pkt_dev->src_mac[i],
629 i == 5 ? " " : ":");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Luiz Capitulino222f1802006-03-20 22:16:13 -0800631 seq_printf(seq, "dst_mac: ");
632 for (i = 0; i < 6; i++)
633 seq_printf(seq, "%02X%s", pkt_dev->dst_mac[i],
634 i == 5 ? "\n" : ":");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Luiz Capitulino222f1802006-03-20 22:16:13 -0800636 seq_printf(seq,
637 " udp_src_min: %d udp_src_max: %d udp_dst_min: %d udp_dst_max: %d\n",
638 pkt_dev->udp_src_min, pkt_dev->udp_src_max,
639 pkt_dev->udp_dst_min, pkt_dev->udp_dst_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
Luiz Capitulino222f1802006-03-20 22:16:13 -0800641 seq_printf(seq,
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800642 " src_mac_count: %d dst_mac_count: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700643 pkt_dev->src_mac_count, pkt_dev->dst_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800645 if (pkt_dev->nr_labels) {
646 unsigned i;
647 seq_printf(seq, " mpls: ");
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700648 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800649 seq_printf(seq, "%08x%s", ntohl(pkt_dev->labels[i]),
650 i == pkt_dev->nr_labels-1 ? "\n" : ", ");
651 }
652
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700653 if (pkt_dev->vlan_id != 0xffff) {
654 seq_printf(seq, " vlan_id: %u vlan_p: %u vlan_cfi: %u\n",
655 pkt_dev->vlan_id, pkt_dev->vlan_p, pkt_dev->vlan_cfi);
656 }
657
658 if (pkt_dev->svlan_id != 0xffff) {
659 seq_printf(seq, " svlan_id: %u vlan_p: %u vlan_cfi: %u\n",
660 pkt_dev->svlan_id, pkt_dev->svlan_p, pkt_dev->svlan_cfi);
661 }
662
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700663 if (pkt_dev->tos) {
664 seq_printf(seq, " tos: 0x%02x\n", pkt_dev->tos);
665 }
666
667 if (pkt_dev->traffic_class) {
668 seq_printf(seq, " traffic_class: 0x%02x\n", pkt_dev->traffic_class);
669 }
670
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800671 seq_printf(seq, " Flags: ");
672
Luiz Capitulino222f1802006-03-20 22:16:13 -0800673 if (pkt_dev->flags & F_IPV6)
674 seq_printf(seq, "IPV6 ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
Luiz Capitulino222f1802006-03-20 22:16:13 -0800676 if (pkt_dev->flags & F_IPSRC_RND)
677 seq_printf(seq, "IPSRC_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Luiz Capitulino222f1802006-03-20 22:16:13 -0800679 if (pkt_dev->flags & F_IPDST_RND)
680 seq_printf(seq, "IPDST_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Luiz Capitulino222f1802006-03-20 22:16:13 -0800682 if (pkt_dev->flags & F_TXSIZE_RND)
683 seq_printf(seq, "TXSIZE_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Luiz Capitulino222f1802006-03-20 22:16:13 -0800685 if (pkt_dev->flags & F_UDPSRC_RND)
686 seq_printf(seq, "UDPSRC_RND ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Luiz Capitulino222f1802006-03-20 22:16:13 -0800688 if (pkt_dev->flags & F_UDPDST_RND)
689 seq_printf(seq, "UDPDST_RND ");
690
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800691 if (pkt_dev->flags & F_MPLS_RND)
692 seq_printf(seq, "MPLS_RND ");
693
Luiz Capitulino222f1802006-03-20 22:16:13 -0800694 if (pkt_dev->flags & F_MACSRC_RND)
695 seq_printf(seq, "MACSRC_RND ");
696
697 if (pkt_dev->flags & F_MACDST_RND)
698 seq_printf(seq, "MACDST_RND ");
699
Francesco Fondelli34954dd2006-09-27 16:30:44 -0700700 if (pkt_dev->flags & F_VID_RND)
701 seq_printf(seq, "VID_RND ");
702
703 if (pkt_dev->flags & F_SVID_RND)
704 seq_printf(seq, "SVID_RND ");
705
Luiz Capitulino222f1802006-03-20 22:16:13 -0800706 seq_puts(seq, "\n");
707
708 sa = pkt_dev->started_at;
709 stopped = pkt_dev->stopped_at;
710 if (pkt_dev->running)
711 stopped = now; /* not really stopped, more like last-running-at */
712
713 seq_printf(seq,
714 "Current:\n pkts-sofar: %llu errors: %llu\n started: %lluus stopped: %lluus idle: %lluus\n",
715 (unsigned long long)pkt_dev->sofar,
716 (unsigned long long)pkt_dev->errors, (unsigned long long)sa,
717 (unsigned long long)stopped,
718 (unsigned long long)pkt_dev->idle_acc);
719
720 seq_printf(seq,
721 " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700722 pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset,
723 pkt_dev->cur_src_mac_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Luiz Capitulino222f1802006-03-20 22:16:13 -0800725 if (pkt_dev->flags & F_IPV6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 char b1[128], b2[128];
Luiz Capitulino222f1802006-03-20 22:16:13 -0800727 fmt_ip6(b1, pkt_dev->cur_in6_daddr.s6_addr);
728 fmt_ip6(b2, pkt_dev->cur_in6_saddr.s6_addr);
729 seq_printf(seq, " cur_saddr: %s cur_daddr: %s\n", b2, b1);
730 } else
731 seq_printf(seq, " cur_saddr: 0x%x cur_daddr: 0x%x\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700732 pkt_dev->cur_saddr, pkt_dev->cur_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Luiz Capitulino222f1802006-03-20 22:16:13 -0800734 seq_printf(seq, " cur_udp_dst: %d cur_udp_src: %d\n",
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700735 pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
Luiz Capitulino222f1802006-03-20 22:16:13 -0800737 seq_printf(seq, " flows: %u\n", pkt_dev->nflows);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
739 if (pkt_dev->result[0])
Luiz Capitulino222f1802006-03-20 22:16:13 -0800740 seq_printf(seq, "Result: %s\n", pkt_dev->result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 else
Luiz Capitulino222f1802006-03-20 22:16:13 -0800742 seq_printf(seq, "Result: Idle\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700744 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745}
746
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800747
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700748static int hex32_arg(const char __user *user_buffer, unsigned long maxlen, __u32 *num)
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800749{
750 int i = 0;
751 *num = 0;
752
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700753 for (; i < maxlen; i++) {
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800754 char c;
755 *num <<= 4;
756 if (get_user(c, &user_buffer[i]))
757 return -EFAULT;
758 if ((c >= '0') && (c <= '9'))
759 *num |= c - '0';
760 else if ((c >= 'a') && (c <= 'f'))
761 *num |= c - 'a' + 10;
762 else if ((c >= 'A') && (c <= 'F'))
763 *num |= c - 'A' + 10;
764 else
765 break;
766 }
767 return i;
768}
769
Luiz Capitulino222f1802006-03-20 22:16:13 -0800770static int count_trail_chars(const char __user * user_buffer,
771 unsigned int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772{
773 int i;
774
775 for (i = 0; i < maxlen; i++) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800776 char c;
777 if (get_user(c, &user_buffer[i]))
778 return -EFAULT;
779 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 case '\"':
781 case '\n':
782 case '\r':
783 case '\t':
784 case ' ':
785 case '=':
786 break;
787 default:
788 goto done;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700789 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 }
791done:
792 return i;
793}
794
Luiz Capitulino222f1802006-03-20 22:16:13 -0800795static unsigned long num_arg(const char __user * user_buffer,
796 unsigned long maxlen, unsigned long *num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797{
798 int i = 0;
799 *num = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800800
801 for (; i < maxlen; i++) {
802 char c;
803 if (get_user(c, &user_buffer[i]))
804 return -EFAULT;
805 if ((c >= '0') && (c <= '9')) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 *num *= 10;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800807 *num += c - '0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 } else
809 break;
810 }
811 return i;
812}
813
Luiz Capitulino222f1802006-03-20 22:16:13 -0800814static int strn_len(const char __user * user_buffer, unsigned int maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815{
816 int i = 0;
817
Luiz Capitulino222f1802006-03-20 22:16:13 -0800818 for (; i < maxlen; i++) {
819 char c;
820 if (get_user(c, &user_buffer[i]))
821 return -EFAULT;
822 switch (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 case '\"':
824 case '\n':
825 case '\r':
826 case '\t':
827 case ' ':
828 goto done_str;
829 break;
830 default:
831 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700832 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 }
834done_str:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 return i;
836}
837
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800838static ssize_t get_labels(const char __user *buffer, struct pktgen_dev *pkt_dev)
839{
840 unsigned n = 0;
841 char c;
842 ssize_t i = 0;
843 int len;
844
845 pkt_dev->nr_labels = 0;
846 do {
847 __u32 tmp;
Francesco Fondelli1ca77682006-09-27 16:32:03 -0700848 len = hex32_arg(&buffer[i], 8, &tmp);
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800849 if (len <= 0)
850 return len;
851 pkt_dev->labels[n] = htonl(tmp);
852 if (pkt_dev->labels[n] & MPLS_STACK_BOTTOM)
853 pkt_dev->flags |= F_MPLS_RND;
854 i += len;
855 if (get_user(c, &buffer[i]))
856 return -EFAULT;
857 i++;
858 n++;
859 if (n >= MAX_MPLS_LABELS)
860 return -E2BIG;
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700861 } while (c == ',');
Steven Whitehouseca6549a2006-03-23 01:10:26 -0800862
863 pkt_dev->nr_labels = n;
864 return i;
865}
866
Luiz Capitulino222f1802006-03-20 22:16:13 -0800867static ssize_t pktgen_if_write(struct file *file,
868 const char __user * user_buffer, size_t count,
869 loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870{
Luiz Capitulino222f1802006-03-20 22:16:13 -0800871 struct seq_file *seq = (struct seq_file *)file->private_data;
872 struct pktgen_dev *pkt_dev = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 int i = 0, max, len;
874 char name[16], valstr[32];
875 unsigned long value = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800876 char *pg_result = NULL;
877 int tmp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 char buf[128];
Luiz Capitulino222f1802006-03-20 22:16:13 -0800879
880 pg_result = &(pkt_dev->result[0]);
881
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 if (count < 1) {
883 printk("pktgen: wrong command format\n");
884 return -EINVAL;
885 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800886
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 max = count - i;
888 tmp = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800889 if (tmp < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 printk("pktgen: illegal format\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -0800891 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 }
Luiz Capitulino222f1802006-03-20 22:16:13 -0800893 i += tmp;
894
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 /* Read variable name */
896
897 len = strn_len(&user_buffer[i], sizeof(name) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800898 if (len < 0) {
899 return len;
900 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 memset(name, 0, sizeof(name));
Luiz Capitulino222f1802006-03-20 22:16:13 -0800902 if (copy_from_user(name, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 return -EFAULT;
904 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800905
906 max = count - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 len = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800908 if (len < 0)
909 return len;
910
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 i += len;
912
913 if (debug) {
Luiz Capitulino222f1802006-03-20 22:16:13 -0800914 char tb[count + 1];
915 if (copy_from_user(tb, user_buffer, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800917 tb[count] = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -0700918 printk("pktgen: %s,%lu buffer -:%s:-\n", name,
Luiz Capitulino222f1802006-03-20 22:16:13 -0800919 (unsigned long)count, tb);
920 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
922 if (!strcmp(name, "min_pkt_size")) {
923 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800924 if (len < 0) {
925 return len;
926 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800928 if (value < 14 + 20 + 8)
929 value = 14 + 20 + 8;
930 if (value != pkt_dev->min_pkt_size) {
931 pkt_dev->min_pkt_size = value;
932 pkt_dev->cur_pkt_size = value;
933 }
934 sprintf(pg_result, "OK: min_pkt_size=%u",
935 pkt_dev->min_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 return count;
937 }
938
Luiz Capitulino222f1802006-03-20 22:16:13 -0800939 if (!strcmp(name, "max_pkt_size")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800941 if (len < 0) {
942 return len;
943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800945 if (value < 14 + 20 + 8)
946 value = 14 + 20 + 8;
947 if (value != pkt_dev->max_pkt_size) {
948 pkt_dev->max_pkt_size = value;
949 pkt_dev->cur_pkt_size = value;
950 }
951 sprintf(pg_result, "OK: max_pkt_size=%u",
952 pkt_dev->max_pkt_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 return count;
954 }
955
Luiz Capitulino222f1802006-03-20 22:16:13 -0800956 /* Shortcut for min = max */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
958 if (!strcmp(name, "pkt_size")) {
959 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800960 if (len < 0) {
961 return len;
962 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800964 if (value < 14 + 20 + 8)
965 value = 14 + 20 + 8;
966 if (value != pkt_dev->min_pkt_size) {
967 pkt_dev->min_pkt_size = value;
968 pkt_dev->max_pkt_size = value;
969 pkt_dev->cur_pkt_size = value;
970 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 sprintf(pg_result, "OK: pkt_size=%u", pkt_dev->min_pkt_size);
972 return count;
973 }
974
Luiz Capitulino222f1802006-03-20 22:16:13 -0800975 if (!strcmp(name, "debug")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800977 if (len < 0) {
978 return len;
979 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -0800981 debug = value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 sprintf(pg_result, "OK: debug=%u", debug);
983 return count;
984 }
985
Luiz Capitulino222f1802006-03-20 22:16:13 -0800986 if (!strcmp(name, "frags")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800988 if (len < 0) {
989 return len;
990 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 i += len;
992 pkt_dev->nfrags = value;
993 sprintf(pg_result, "OK: frags=%u", pkt_dev->nfrags);
994 return count;
995 }
996 if (!strcmp(name, "delay")) {
997 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -0800998 if (len < 0) {
999 return len;
1000 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 i += len;
1002 if (value == 0x7FFFFFFF) {
1003 pkt_dev->delay_us = 0x7FFFFFFF;
1004 pkt_dev->delay_ns = 0;
1005 } else {
1006 pkt_dev->delay_us = value / 1000;
1007 pkt_dev->delay_ns = value % 1000;
1008 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001009 sprintf(pg_result, "OK: delay=%u",
1010 1000 * pkt_dev->delay_us + pkt_dev->delay_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 return count;
1012 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001013 if (!strcmp(name, "udp_src_min")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001015 if (len < 0) {
1016 return len;
1017 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001019 if (value != pkt_dev->udp_src_min) {
1020 pkt_dev->udp_src_min = value;
1021 pkt_dev->cur_udp_src = value;
1022 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min);
1024 return count;
1025 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001026 if (!strcmp(name, "udp_dst_min")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001028 if (len < 0) {
1029 return len;
1030 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001032 if (value != pkt_dev->udp_dst_min) {
1033 pkt_dev->udp_dst_min = value;
1034 pkt_dev->cur_udp_dst = value;
1035 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min);
1037 return count;
1038 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001039 if (!strcmp(name, "udp_src_max")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001041 if (len < 0) {
1042 return len;
1043 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001045 if (value != pkt_dev->udp_src_max) {
1046 pkt_dev->udp_src_max = value;
1047 pkt_dev->cur_udp_src = value;
1048 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max);
1050 return count;
1051 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001052 if (!strcmp(name, "udp_dst_max")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001054 if (len < 0) {
1055 return len;
1056 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001058 if (value != pkt_dev->udp_dst_max) {
1059 pkt_dev->udp_dst_max = value;
1060 pkt_dev->cur_udp_dst = value;
1061 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max);
1063 return count;
1064 }
1065 if (!strcmp(name, "clone_skb")) {
1066 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001067 if (len < 0) {
1068 return len;
1069 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001071 pkt_dev->clone_skb = value;
1072
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb);
1074 return count;
1075 }
1076 if (!strcmp(name, "count")) {
1077 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001078 if (len < 0) {
1079 return len;
1080 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 i += len;
1082 pkt_dev->count = value;
1083 sprintf(pg_result, "OK: count=%llu",
Luiz Capitulino222f1802006-03-20 22:16:13 -08001084 (unsigned long long)pkt_dev->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 return count;
1086 }
1087 if (!strcmp(name, "src_mac_count")) {
1088 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001089 if (len < 0) {
1090 return len;
1091 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 i += len;
1093 if (pkt_dev->src_mac_count != value) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001094 pkt_dev->src_mac_count = value;
1095 pkt_dev->cur_src_mac_offset = 0;
1096 }
1097 sprintf(pg_result, "OK: src_mac_count=%d",
1098 pkt_dev->src_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 return count;
1100 }
1101 if (!strcmp(name, "dst_mac_count")) {
1102 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001103 if (len < 0) {
1104 return len;
1105 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 i += len;
1107 if (pkt_dev->dst_mac_count != value) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001108 pkt_dev->dst_mac_count = value;
1109 pkt_dev->cur_dst_mac_offset = 0;
1110 }
1111 sprintf(pg_result, "OK: dst_mac_count=%d",
1112 pkt_dev->dst_mac_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 return count;
1114 }
1115 if (!strcmp(name, "flag")) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001116 char f[32];
1117 memset(f, 0, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 len = strn_len(&user_buffer[i], sizeof(f) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001119 if (len < 0) {
1120 return len;
1121 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 if (copy_from_user(f, &user_buffer[i], len))
1123 return -EFAULT;
1124 i += len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001125 if (strcmp(f, "IPSRC_RND") == 0)
1126 pkt_dev->flags |= F_IPSRC_RND;
1127
1128 else if (strcmp(f, "!IPSRC_RND") == 0)
1129 pkt_dev->flags &= ~F_IPSRC_RND;
1130
1131 else if (strcmp(f, "TXSIZE_RND") == 0)
1132 pkt_dev->flags |= F_TXSIZE_RND;
1133
1134 else if (strcmp(f, "!TXSIZE_RND") == 0)
1135 pkt_dev->flags &= ~F_TXSIZE_RND;
1136
1137 else if (strcmp(f, "IPDST_RND") == 0)
1138 pkt_dev->flags |= F_IPDST_RND;
1139
1140 else if (strcmp(f, "!IPDST_RND") == 0)
1141 pkt_dev->flags &= ~F_IPDST_RND;
1142
1143 else if (strcmp(f, "UDPSRC_RND") == 0)
1144 pkt_dev->flags |= F_UDPSRC_RND;
1145
1146 else if (strcmp(f, "!UDPSRC_RND") == 0)
1147 pkt_dev->flags &= ~F_UDPSRC_RND;
1148
1149 else if (strcmp(f, "UDPDST_RND") == 0)
1150 pkt_dev->flags |= F_UDPDST_RND;
1151
1152 else if (strcmp(f, "!UDPDST_RND") == 0)
1153 pkt_dev->flags &= ~F_UDPDST_RND;
1154
1155 else if (strcmp(f, "MACSRC_RND") == 0)
1156 pkt_dev->flags |= F_MACSRC_RND;
1157
1158 else if (strcmp(f, "!MACSRC_RND") == 0)
1159 pkt_dev->flags &= ~F_MACSRC_RND;
1160
1161 else if (strcmp(f, "MACDST_RND") == 0)
1162 pkt_dev->flags |= F_MACDST_RND;
1163
1164 else if (strcmp(f, "!MACDST_RND") == 0)
1165 pkt_dev->flags &= ~F_MACDST_RND;
1166
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001167 else if (strcmp(f, "MPLS_RND") == 0)
1168 pkt_dev->flags |= F_MPLS_RND;
1169
1170 else if (strcmp(f, "!MPLS_RND") == 0)
1171 pkt_dev->flags &= ~F_MPLS_RND;
1172
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001173 else if (strcmp(f, "VID_RND") == 0)
1174 pkt_dev->flags |= F_VID_RND;
1175
1176 else if (strcmp(f, "!VID_RND") == 0)
1177 pkt_dev->flags &= ~F_VID_RND;
1178
1179 else if (strcmp(f, "SVID_RND") == 0)
1180 pkt_dev->flags |= F_SVID_RND;
1181
1182 else if (strcmp(f, "!SVID_RND") == 0)
1183 pkt_dev->flags &= ~F_SVID_RND;
1184
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001185 else if (strcmp(f, "!IPV6") == 0)
1186 pkt_dev->flags &= ~F_IPV6;
1187
Luiz Capitulino222f1802006-03-20 22:16:13 -08001188 else {
1189 sprintf(pg_result,
1190 "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
1191 f,
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001192 "IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
1193 "MACSRC_RND, MACDST_RND, TXSIZE_RND, IPV6, MPLS_RND, VID_RND, SVID_RND\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08001194 return count;
1195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
1197 return count;
1198 }
1199 if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
1200 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001201 if (len < 0) {
1202 return len;
1203 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
Luiz Capitulino222f1802006-03-20 22:16:13 -08001205 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001207 buf[len] = 0;
1208 if (strcmp(buf, pkt_dev->dst_min) != 0) {
1209 memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
1210 strncpy(pkt_dev->dst_min, buf, len);
1211 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
1212 pkt_dev->cur_daddr = pkt_dev->daddr_min;
1213 }
1214 if (debug)
1215 printk("pktgen: dst_min set to: %s\n",
1216 pkt_dev->dst_min);
1217 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min);
1219 return count;
1220 }
1221 if (!strcmp(name, "dst_max")) {
1222 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001223 if (len < 0) {
1224 return len;
1225 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
Luiz Capitulino222f1802006-03-20 22:16:13 -08001227 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 return -EFAULT;
1229
Luiz Capitulino222f1802006-03-20 22:16:13 -08001230 buf[len] = 0;
1231 if (strcmp(buf, pkt_dev->dst_max) != 0) {
1232 memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
1233 strncpy(pkt_dev->dst_max, buf, len);
1234 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
1235 pkt_dev->cur_daddr = pkt_dev->daddr_max;
1236 }
1237 if (debug)
1238 printk("pktgen: dst_max set to: %s\n",
1239 pkt_dev->dst_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 i += len;
1241 sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max);
1242 return count;
1243 }
1244 if (!strcmp(name, "dst6")) {
1245 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001246 if (len < 0)
1247 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
1249 pkt_dev->flags |= F_IPV6;
1250
Luiz Capitulino222f1802006-03-20 22:16:13 -08001251 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001253 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
1255 scan_ip6(buf, pkt_dev->in6_daddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001256 fmt_ip6(buf, pkt_dev->in6_daddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
1258 ipv6_addr_copy(&pkt_dev->cur_in6_daddr, &pkt_dev->in6_daddr);
1259
Luiz Capitulino222f1802006-03-20 22:16:13 -08001260 if (debug)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 printk("pktgen: dst6 set to: %s\n", buf);
1262
Luiz Capitulino222f1802006-03-20 22:16:13 -08001263 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 sprintf(pg_result, "OK: dst6=%s", buf);
1265 return count;
1266 }
1267 if (!strcmp(name, "dst6_min")) {
1268 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001269 if (len < 0)
1270 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
1272 pkt_dev->flags |= F_IPV6;
1273
Luiz Capitulino222f1802006-03-20 22:16:13 -08001274 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001276 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
1278 scan_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001279 fmt_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
Luiz Capitulino222f1802006-03-20 22:16:13 -08001281 ipv6_addr_copy(&pkt_dev->cur_in6_daddr,
1282 &pkt_dev->min_in6_daddr);
1283 if (debug)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 printk("pktgen: dst6_min set to: %s\n", buf);
1285
Luiz Capitulino222f1802006-03-20 22:16:13 -08001286 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 sprintf(pg_result, "OK: dst6_min=%s", buf);
1288 return count;
1289 }
1290 if (!strcmp(name, "dst6_max")) {
1291 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001292 if (len < 0)
1293 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
1295 pkt_dev->flags |= F_IPV6;
1296
Luiz Capitulino222f1802006-03-20 22:16:13 -08001297 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001299 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
1301 scan_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001302 fmt_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
Luiz Capitulino222f1802006-03-20 22:16:13 -08001304 if (debug)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 printk("pktgen: dst6_max set to: %s\n", buf);
1306
Luiz Capitulino222f1802006-03-20 22:16:13 -08001307 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 sprintf(pg_result, "OK: dst6_max=%s", buf);
1309 return count;
1310 }
1311 if (!strcmp(name, "src6")) {
1312 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001313 if (len < 0)
1314 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
1316 pkt_dev->flags |= F_IPV6;
1317
Luiz Capitulino222f1802006-03-20 22:16:13 -08001318 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001320 buf[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
1322 scan_ip6(buf, pkt_dev->in6_saddr.s6_addr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001323 fmt_ip6(buf, pkt_dev->in6_saddr.s6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
1325 ipv6_addr_copy(&pkt_dev->cur_in6_saddr, &pkt_dev->in6_saddr);
1326
Luiz Capitulino222f1802006-03-20 22:16:13 -08001327 if (debug)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 printk("pktgen: src6 set to: %s\n", buf);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001329
1330 i += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 sprintf(pg_result, "OK: src6=%s", buf);
1332 return count;
1333 }
1334 if (!strcmp(name, "src_min")) {
1335 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001336 if (len < 0) {
1337 return len;
1338 }
1339 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001341 buf[len] = 0;
1342 if (strcmp(buf, pkt_dev->src_min) != 0) {
1343 memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
1344 strncpy(pkt_dev->src_min, buf, len);
1345 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
1346 pkt_dev->cur_saddr = pkt_dev->saddr_min;
1347 }
1348 if (debug)
1349 printk("pktgen: src_min set to: %s\n",
1350 pkt_dev->src_min);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 i += len;
1352 sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min);
1353 return count;
1354 }
1355 if (!strcmp(name, "src_max")) {
1356 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001357 if (len < 0) {
1358 return len;
1359 }
1360 if (copy_from_user(buf, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 return -EFAULT;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001362 buf[len] = 0;
1363 if (strcmp(buf, pkt_dev->src_max) != 0) {
1364 memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
1365 strncpy(pkt_dev->src_max, buf, len);
1366 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
1367 pkt_dev->cur_saddr = pkt_dev->saddr_max;
1368 }
1369 if (debug)
1370 printk("pktgen: src_max set to: %s\n",
1371 pkt_dev->src_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 i += len;
1373 sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max);
1374 return count;
1375 }
1376 if (!strcmp(name, "dst_mac")) {
1377 char *v = valstr;
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001378 unsigned char old_dmac[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 unsigned char *m = pkt_dev->dst_mac;
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001380 memcpy(old_dmac, pkt_dev->dst_mac, ETH_ALEN);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001381
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001383 if (len < 0) {
1384 return len;
1385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 memset(valstr, 0, sizeof(valstr));
Luiz Capitulino222f1802006-03-20 22:16:13 -08001387 if (copy_from_user(valstr, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 return -EFAULT;
1389 i += len;
1390
Luiz Capitulino222f1802006-03-20 22:16:13 -08001391 for (*m = 0; *v && m < pkt_dev->dst_mac + 6; v++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 if (*v >= '0' && *v <= '9') {
1393 *m *= 16;
1394 *m += *v - '0';
1395 }
1396 if (*v >= 'A' && *v <= 'F') {
1397 *m *= 16;
1398 *m += *v - 'A' + 10;
1399 }
1400 if (*v >= 'a' && *v <= 'f') {
1401 *m *= 16;
1402 *m += *v - 'a' + 10;
1403 }
1404 if (*v == ':') {
1405 m++;
1406 *m = 0;
1407 }
1408 }
1409
1410 /* Set up Dest MAC */
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001411 if (compare_ether_addr(old_dmac, pkt_dev->dst_mac))
1412 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001413
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 sprintf(pg_result, "OK: dstmac");
1415 return count;
1416 }
1417 if (!strcmp(name, "src_mac")) {
1418 char *v = valstr;
1419 unsigned char *m = pkt_dev->src_mac;
1420
1421 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001422 if (len < 0) {
1423 return len;
1424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 memset(valstr, 0, sizeof(valstr));
Luiz Capitulino222f1802006-03-20 22:16:13 -08001426 if (copy_from_user(valstr, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 return -EFAULT;
1428 i += len;
1429
Luiz Capitulino222f1802006-03-20 22:16:13 -08001430 for (*m = 0; *v && m < pkt_dev->src_mac + 6; v++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 if (*v >= '0' && *v <= '9') {
1432 *m *= 16;
1433 *m += *v - '0';
1434 }
1435 if (*v >= 'A' && *v <= 'F') {
1436 *m *= 16;
1437 *m += *v - 'A' + 10;
1438 }
1439 if (*v >= 'a' && *v <= 'f') {
1440 *m *= 16;
1441 *m += *v - 'a' + 10;
1442 }
1443 if (*v == ':') {
1444 m++;
1445 *m = 0;
1446 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001447 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
Luiz Capitulino222f1802006-03-20 22:16:13 -08001449 sprintf(pg_result, "OK: srcmac");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 return count;
1451 }
1452
Luiz Capitulino222f1802006-03-20 22:16:13 -08001453 if (!strcmp(name, "clear_counters")) {
1454 pktgen_clear_counters(pkt_dev);
1455 sprintf(pg_result, "OK: Clearing counters.\n");
1456 return count;
1457 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
1459 if (!strcmp(name, "flows")) {
1460 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001461 if (len < 0) {
1462 return len;
1463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 i += len;
1465 if (value > MAX_CFLOWS)
1466 value = MAX_CFLOWS;
1467
1468 pkt_dev->cflows = value;
1469 sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
1470 return count;
1471 }
1472
1473 if (!strcmp(name, "flowlen")) {
1474 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001475 if (len < 0) {
1476 return len;
1477 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 i += len;
1479 pkt_dev->lflow = value;
1480 sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
1481 return count;
1482 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001483
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001484 if (!strcmp(name, "mpls")) {
1485 unsigned n, offset;
1486 len = get_labels(&user_buffer[i], pkt_dev);
1487 if (len < 0) { return len; }
1488 i += len;
1489 offset = sprintf(pg_result, "OK: mpls=");
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001490 for (n = 0; n < pkt_dev->nr_labels; n++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001491 offset += sprintf(pg_result + offset,
1492 "%08x%s", ntohl(pkt_dev->labels[n]),
1493 n == pkt_dev->nr_labels-1 ? "" : ",");
Francesco Fondelli34954dd2006-09-27 16:30:44 -07001494
1495 if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) {
1496 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1497 pkt_dev->svlan_id = 0xffff;
1498
1499 if (debug)
1500 printk("pktgen: VLAN/SVLAN auto turned off\n");
1501 }
1502 return count;
1503 }
1504
1505 if (!strcmp(name, "vlan_id")) {
1506 len = num_arg(&user_buffer[i], 4, &value);
1507 if (len < 0) {
1508 return len;
1509 }
1510 i += len;
1511 if (value <= 4095) {
1512 pkt_dev->vlan_id = value; /* turn on VLAN */
1513
1514 if (debug)
1515 printk("pktgen: VLAN turned on\n");
1516
1517 if (debug && pkt_dev->nr_labels)
1518 printk("pktgen: MPLS auto turned off\n");
1519
1520 pkt_dev->nr_labels = 0; /* turn off MPLS */
1521 sprintf(pg_result, "OK: vlan_id=%u", pkt_dev->vlan_id);
1522 } else {
1523 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1524 pkt_dev->svlan_id = 0xffff;
1525
1526 if (debug)
1527 printk("pktgen: VLAN/SVLAN turned off\n");
1528 }
1529 return count;
1530 }
1531
1532 if (!strcmp(name, "vlan_p")) {
1533 len = num_arg(&user_buffer[i], 1, &value);
1534 if (len < 0) {
1535 return len;
1536 }
1537 i += len;
1538 if ((value <= 7) && (pkt_dev->vlan_id != 0xffff)) {
1539 pkt_dev->vlan_p = value;
1540 sprintf(pg_result, "OK: vlan_p=%u", pkt_dev->vlan_p);
1541 } else {
1542 sprintf(pg_result, "ERROR: vlan_p must be 0-7");
1543 }
1544 return count;
1545 }
1546
1547 if (!strcmp(name, "vlan_cfi")) {
1548 len = num_arg(&user_buffer[i], 1, &value);
1549 if (len < 0) {
1550 return len;
1551 }
1552 i += len;
1553 if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) {
1554 pkt_dev->vlan_cfi = value;
1555 sprintf(pg_result, "OK: vlan_cfi=%u", pkt_dev->vlan_cfi);
1556 } else {
1557 sprintf(pg_result, "ERROR: vlan_cfi must be 0-1");
1558 }
1559 return count;
1560 }
1561
1562 if (!strcmp(name, "svlan_id")) {
1563 len = num_arg(&user_buffer[i], 4, &value);
1564 if (len < 0) {
1565 return len;
1566 }
1567 i += len;
1568 if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) {
1569 pkt_dev->svlan_id = value; /* turn on SVLAN */
1570
1571 if (debug)
1572 printk("pktgen: SVLAN turned on\n");
1573
1574 if (debug && pkt_dev->nr_labels)
1575 printk("pktgen: MPLS auto turned off\n");
1576
1577 pkt_dev->nr_labels = 0; /* turn off MPLS */
1578 sprintf(pg_result, "OK: svlan_id=%u", pkt_dev->svlan_id);
1579 } else {
1580 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1581 pkt_dev->svlan_id = 0xffff;
1582
1583 if (debug)
1584 printk("pktgen: VLAN/SVLAN turned off\n");
1585 }
1586 return count;
1587 }
1588
1589 if (!strcmp(name, "svlan_p")) {
1590 len = num_arg(&user_buffer[i], 1, &value);
1591 if (len < 0) {
1592 return len;
1593 }
1594 i += len;
1595 if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) {
1596 pkt_dev->svlan_p = value;
1597 sprintf(pg_result, "OK: svlan_p=%u", pkt_dev->svlan_p);
1598 } else {
1599 sprintf(pg_result, "ERROR: svlan_p must be 0-7");
1600 }
1601 return count;
1602 }
1603
1604 if (!strcmp(name, "svlan_cfi")) {
1605 len = num_arg(&user_buffer[i], 1, &value);
1606 if (len < 0) {
1607 return len;
1608 }
1609 i += len;
1610 if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) {
1611 pkt_dev->svlan_cfi = value;
1612 sprintf(pg_result, "OK: svlan_cfi=%u", pkt_dev->svlan_cfi);
1613 } else {
1614 sprintf(pg_result, "ERROR: svlan_cfi must be 0-1");
1615 }
Steven Whitehouseca6549a2006-03-23 01:10:26 -08001616 return count;
1617 }
1618
Francesco Fondelli1ca77682006-09-27 16:32:03 -07001619 if (!strcmp(name, "tos")) {
1620 __u32 tmp_value = 0;
1621 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1622 if (len < 0) {
1623 return len;
1624 }
1625 i += len;
1626 if (len == 2) {
1627 pkt_dev->tos = tmp_value;
1628 sprintf(pg_result, "OK: tos=0x%02x", pkt_dev->tos);
1629 } else {
1630 sprintf(pg_result, "ERROR: tos must be 00-ff");
1631 }
1632 return count;
1633 }
1634
1635 if (!strcmp(name, "traffic_class")) {
1636 __u32 tmp_value = 0;
1637 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1638 if (len < 0) {
1639 return len;
1640 }
1641 i += len;
1642 if (len == 2) {
1643 pkt_dev->traffic_class = tmp_value;
1644 sprintf(pg_result, "OK: traffic_class=0x%02x", pkt_dev->traffic_class);
1645 } else {
1646 sprintf(pg_result, "ERROR: traffic_class must be 00-ff");
1647 }
1648 return count;
1649 }
1650
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
1652 return -EINVAL;
1653}
1654
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001655static int pktgen_if_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656{
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001657 return single_open(file, pktgen_if_show, PDE(inode)->data);
1658}
1659
Arjan van de Ven9a321442007-02-12 00:55:35 -08001660static const struct file_operations pktgen_if_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001661 .owner = THIS_MODULE,
1662 .open = pktgen_if_open,
1663 .read = seq_read,
1664 .llseek = seq_lseek,
1665 .write = pktgen_if_write,
1666 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001667};
1668
1669static int pktgen_thread_show(struct seq_file *seq, void *v)
1670{
Luiz Capitulino222f1802006-03-20 22:16:13 -08001671 struct pktgen_thread *t = seq->private;
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08001672 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001674 BUG_ON(!t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001676 seq_printf(seq, "Name: %s max_before_softirq: %d\n",
David S. Milleree74baa2007-01-01 20:51:53 -08001677 t->tsk->comm, t->max_before_softirq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
Luiz Capitulino222f1802006-03-20 22:16:13 -08001679 seq_printf(seq, "Running: ");
1680
1681 if_lock(t);
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08001682 list_for_each_entry(pkt_dev, &t->if_list, list)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001683 if (pkt_dev->running)
Stephen Hemminger39df2322007-03-04 16:11:51 -08001684 seq_printf(seq, "%s ", pkt_dev->odev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685
Luiz Capitulino222f1802006-03-20 22:16:13 -08001686 seq_printf(seq, "\nStopped: ");
1687
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08001688 list_for_each_entry(pkt_dev, &t->if_list, list)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001689 if (!pkt_dev->running)
Stephen Hemminger39df2322007-03-04 16:11:51 -08001690 seq_printf(seq, "%s ", pkt_dev->odev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
1692 if (t->result[0])
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001693 seq_printf(seq, "\nResult: %s\n", t->result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 else
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001695 seq_printf(seq, "\nResult: NA\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
Luiz Capitulino222f1802006-03-20 22:16:13 -08001697 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001699 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700}
1701
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001702static ssize_t pktgen_thread_write(struct file *file,
Luiz Capitulino222f1802006-03-20 22:16:13 -08001703 const char __user * user_buffer,
1704 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705{
Luiz Capitulino222f1802006-03-20 22:16:13 -08001706 struct seq_file *seq = (struct seq_file *)file->private_data;
1707 struct pktgen_thread *t = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 int i = 0, max, len, ret;
1709 char name[40];
Luiz Capitulino222f1802006-03-20 22:16:13 -08001710 char *pg_result;
1711 unsigned long value = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001712
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 if (count < 1) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001714 // sprintf(pg_result, "Wrong command format");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 return -EINVAL;
1716 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001717
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 max = count - i;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001719 len = count_trail_chars(&user_buffer[i], max);
1720 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001721 return len;
1722
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 i += len;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001724
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 /* Read variable name */
1726
1727 len = strn_len(&user_buffer[i], sizeof(name) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001728 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001729 return len;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001730
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 memset(name, 0, sizeof(name));
1732 if (copy_from_user(name, &user_buffer[i], len))
1733 return -EFAULT;
1734 i += len;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001735
Luiz Capitulino222f1802006-03-20 22:16:13 -08001736 max = count - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 len = count_trail_chars(&user_buffer[i], max);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001738 if (len < 0)
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001739 return len;
1740
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 i += len;
1742
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001743 if (debug)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001744 printk("pktgen: t=%s, count=%lu\n", name, (unsigned long)count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
Luiz Capitulino222f1802006-03-20 22:16:13 -08001746 if (!t) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 printk("pktgen: ERROR: No thread\n");
1748 ret = -EINVAL;
1749 goto out;
1750 }
1751
1752 pg_result = &(t->result[0]);
1753
Luiz Capitulino222f1802006-03-20 22:16:13 -08001754 if (!strcmp(name, "add_device")) {
1755 char f[32];
1756 memset(f, 0, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 len = strn_len(&user_buffer[i], sizeof(f) - 1);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001758 if (len < 0) {
1759 ret = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 goto out;
1761 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001762 if (copy_from_user(f, &user_buffer[i], len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 return -EFAULT;
1764 i += len;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001765 mutex_lock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001766 pktgen_add_device(t, f);
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001767 mutex_unlock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001768 ret = count;
1769 sprintf(pg_result, "OK: add_device=%s", f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 goto out;
1771 }
1772
Luiz Capitulino222f1802006-03-20 22:16:13 -08001773 if (!strcmp(name, "rem_device_all")) {
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001774 mutex_lock(&pktgen_thread_lock);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001775 t->control |= T_REMDEVALL;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001776 mutex_unlock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001777 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 ret = count;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001779 sprintf(pg_result, "OK: rem_device_all");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 goto out;
1781 }
1782
Luiz Capitulino222f1802006-03-20 22:16:13 -08001783 if (!strcmp(name, "max_before_softirq")) {
1784 len = num_arg(&user_buffer[i], 10, &value);
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001785 mutex_lock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001786 t->max_before_softirq = value;
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001787 mutex_unlock(&pktgen_thread_lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001788 ret = count;
1789 sprintf(pg_result, "OK: max_before_softirq=%lu", value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 goto out;
1791 }
1792
1793 ret = -EINVAL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001794out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 return ret;
1796}
1797
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001798static int pktgen_thread_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799{
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001800 return single_open(file, pktgen_thread_show, PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801}
1802
Arjan van de Ven9a321442007-02-12 00:55:35 -08001803static const struct file_operations pktgen_thread_fops = {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001804 .owner = THIS_MODULE,
1805 .open = pktgen_thread_open,
1806 .read = seq_read,
1807 .llseek = seq_lseek,
1808 .write = pktgen_thread_write,
1809 .release = single_release,
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07001810};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811
1812/* Think find or remove for NN */
Luiz Capitulino222f1802006-03-20 22:16:13 -08001813static struct pktgen_dev *__pktgen_NN_threads(const char *ifname, int remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814{
1815 struct pktgen_thread *t;
1816 struct pktgen_dev *pkt_dev = NULL;
1817
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08001818 list_for_each_entry(t, &pktgen_threads, th_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 pkt_dev = pktgen_find_dev(t, ifname);
1820 if (pkt_dev) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08001821 if (remove) {
1822 if_lock(t);
1823 pkt_dev->removal_mark = 1;
1824 t->control |= T_REMDEV;
1825 if_unlock(t);
1826 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 break;
1828 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001830 return pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831}
1832
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001833/*
1834 * mark a device for removal
1835 */
Stephen Hemminger39df2322007-03-04 16:11:51 -08001836static void pktgen_mark_device(const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837{
1838 struct pktgen_dev *pkt_dev = NULL;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001839 const int max_tries = 10, msec_per_try = 125;
1840 int i = 0;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001841
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001842 mutex_lock(&pktgen_thread_lock);
Stephen Hemminger25c4e532007-03-04 16:06:47 -08001843 pr_debug("pktgen: pktgen_mark_device marking %s for removal\n", ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001844
Luiz Capitulino222f1802006-03-20 22:16:13 -08001845 while (1) {
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001846
1847 pkt_dev = __pktgen_NN_threads(ifname, REMOVE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001848 if (pkt_dev == NULL)
1849 break; /* success */
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001850
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001851 mutex_unlock(&pktgen_thread_lock);
Stephen Hemminger25c4e532007-03-04 16:06:47 -08001852 pr_debug("pktgen: pktgen_mark_device waiting for %s "
1853 "to disappear....\n", ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001854 schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001855 mutex_lock(&pktgen_thread_lock);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001856
1857 if (++i >= max_tries) {
1858 printk("pktgen_mark_device: timed out after waiting "
Luiz Capitulino222f1802006-03-20 22:16:13 -08001859 "%d msec for device %s to be removed\n",
1860 msec_per_try * i, ifname);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001861 break;
1862 }
1863
1864 }
1865
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08001866 mutex_unlock(&pktgen_thread_lock);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001867}
Arthur Kepner95ed63f2006-03-20 21:26:56 -08001868
Stephen Hemminger39df2322007-03-04 16:11:51 -08001869static void pktgen_change_name(struct net_device *dev)
1870{
1871 struct pktgen_thread *t;
1872
1873 list_for_each_entry(t, &pktgen_threads, th_list) {
1874 struct pktgen_dev *pkt_dev;
1875
1876 list_for_each_entry(pkt_dev, &t->if_list, list) {
1877 if (pkt_dev->odev != dev)
1878 continue;
1879
1880 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
1881
1882 pkt_dev->entry = create_proc_entry(dev->name, 0600,
1883 pg_proc_dir);
1884 if (!pkt_dev->entry)
1885 printk(KERN_ERR "pktgen: can't move proc "
1886 " entry for '%s'\n", dev->name);
1887 break;
1888 }
1889 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890}
1891
Luiz Capitulino222f1802006-03-20 22:16:13 -08001892static int pktgen_device_event(struct notifier_block *unused,
1893 unsigned long event, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894{
Stephen Hemminger39df2322007-03-04 16:11:51 -08001895 struct net_device *dev = ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896
1897 /* It is OK that we do not hold the group lock right now,
1898 * as we run under the RTNL lock.
1899 */
1900
1901 switch (event) {
Stephen Hemminger39df2322007-03-04 16:11:51 -08001902 case NETDEV_CHANGENAME:
1903 pktgen_change_name(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 break;
Luiz Capitulino222f1802006-03-20 22:16:13 -08001905
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 case NETDEV_UNREGISTER:
Luiz Capitulino222f1802006-03-20 22:16:13 -08001907 pktgen_mark_device(dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
1911 return NOTIFY_DONE;
1912}
1913
1914/* Associate pktgen_dev with a device. */
1915
Stephen Hemminger39df2322007-03-04 16:11:51 -08001916static int pktgen_setup_dev(struct pktgen_dev *pkt_dev, const char *ifname)
Luiz Capitulino222f1802006-03-20 22:16:13 -08001917{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 struct net_device *odev;
Stephen Hemminger39df2322007-03-04 16:11:51 -08001919 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
1921 /* Clean old setups */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 if (pkt_dev->odev) {
1923 dev_put(pkt_dev->odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08001924 pkt_dev->odev = NULL;
1925 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
Stephen Hemminger39df2322007-03-04 16:11:51 -08001927 odev = dev_get_by_name(ifname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 if (!odev) {
Stephen Hemminger39df2322007-03-04 16:11:51 -08001929 printk("pktgen: no such netdevice: \"%s\"\n", ifname);
1930 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 }
Stephen Hemminger39df2322007-03-04 16:11:51 -08001932
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 if (odev->type != ARPHRD_ETHER) {
Stephen Hemminger39df2322007-03-04 16:11:51 -08001934 printk("pktgen: not an ethernet device: \"%s\"\n", ifname);
1935 err = -EINVAL;
1936 } else if (!netif_running(odev)) {
1937 printk("pktgen: device is down: \"%s\"\n", ifname);
1938 err = -ENETDOWN;
1939 } else {
1940 pkt_dev->odev = odev;
1941 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08001943
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 dev_put(odev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08001945 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946}
1947
1948/* Read pkt_dev from the interface and set up internal pktgen_dev
1949 * structure to have the right information to create/send packets
1950 */
1951static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
1952{
Luiz Capitulino222f1802006-03-20 22:16:13 -08001953 if (!pkt_dev->odev) {
1954 printk("pktgen: ERROR: pkt_dev->odev == NULL in setup_inject.\n");
1955 sprintf(pkt_dev->result,
1956 "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
1957 return;
1958 }
1959
1960 /* Default to the interface's mac if not explicitly set. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001962 if (is_zero_ether_addr(pkt_dev->src_mac))
Luiz Capitulino222f1802006-03-20 22:16:13 -08001963 memcpy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964
Luiz Capitulino222f1802006-03-20 22:16:13 -08001965 /* Set up Dest MAC */
Kris Katterjohnf404e9a2006-01-17 13:04:57 -08001966 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967
Luiz Capitulino222f1802006-03-20 22:16:13 -08001968 /* Set up pkt size */
1969 pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
1970
1971 if (pkt_dev->flags & F_IPV6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 /*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001973 * Skip this automatic address setting until locks or functions
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 * gets exported
1975 */
1976
1977#ifdef NOTNOW
Luiz Capitulino222f1802006-03-20 22:16:13 -08001978 int i, set = 0, err = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 struct inet6_dev *idev;
1980
Luiz Capitulino222f1802006-03-20 22:16:13 -08001981 for (i = 0; i < IN6_ADDR_HSIZE; i++)
1982 if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 set = 1;
1984 break;
1985 }
1986
Luiz Capitulino222f1802006-03-20 22:16:13 -08001987 if (!set) {
1988
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 /*
1990 * Use linklevel address if unconfigured.
1991 *
1992 * use ipv6_get_lladdr if/when it's get exported
1993 */
1994
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001995 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 if ((idev = __in6_dev_get(pkt_dev->odev)) != NULL) {
1997 struct inet6_ifaddr *ifp;
1998
1999 read_lock_bh(&idev->lock);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002000 for (ifp = idev->addr_list; ifp;
2001 ifp = ifp->if_next) {
2002 if (ifp->scope == IFA_LINK
2003 && !(ifp->
2004 flags & IFA_F_TENTATIVE)) {
2005 ipv6_addr_copy(&pkt_dev->
2006 cur_in6_saddr,
2007 &ifp->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 err = 0;
2009 break;
2010 }
2011 }
2012 read_unlock_bh(&idev->lock);
2013 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002014 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002015 if (err)
2016 printk("pktgen: ERROR: IPv6 link address not availble.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 }
2018#endif
Luiz Capitulino222f1802006-03-20 22:16:13 -08002019 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 pkt_dev->saddr_min = 0;
2021 pkt_dev->saddr_max = 0;
2022 if (strlen(pkt_dev->src_min) == 0) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002023
2024 struct in_device *in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
2026 rcu_read_lock();
Herbert Xue5ed6392005-10-03 14:35:55 -07002027 in_dev = __in_dev_get_rcu(pkt_dev->odev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 if (in_dev) {
2029 if (in_dev->ifa_list) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002030 pkt_dev->saddr_min =
2031 in_dev->ifa_list->ifa_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 pkt_dev->saddr_max = pkt_dev->saddr_min;
2033 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 }
2035 rcu_read_unlock();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002036 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
2038 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
2039 }
2040
2041 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
2042 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
2043 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002044 /* Initialize current values. */
2045 pkt_dev->cur_dst_mac_offset = 0;
2046 pkt_dev->cur_src_mac_offset = 0;
2047 pkt_dev->cur_saddr = pkt_dev->saddr_min;
2048 pkt_dev->cur_daddr = pkt_dev->daddr_min;
2049 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2050 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 pkt_dev->nflows = 0;
2052}
2053
2054static void spin(struct pktgen_dev *pkt_dev, __u64 spin_until_us)
2055{
2056 __u64 start;
2057 __u64 now;
2058
2059 start = now = getCurUs();
2060 printk(KERN_INFO "sleeping for %d\n", (int)(spin_until_us - now));
2061 while (now < spin_until_us) {
Stephen Hemmingerb4099fa2005-10-14 15:32:22 -07002062 /* TODO: optimize sleeping behavior */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002063 if (spin_until_us - now > jiffies_to_usecs(1) + 1)
Nishanth Aravamudan121caf52005-09-12 14:15:34 -07002064 schedule_timeout_interruptible(1);
2065 else if (spin_until_us - now > 100) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 do_softirq();
2067 if (!pkt_dev->running)
2068 return;
2069 if (need_resched())
2070 schedule();
2071 }
2072
2073 now = getCurUs();
2074 }
2075
2076 pkt_dev->idle_acc += now - start;
2077}
2078
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079/* Increment/randomize headers according to flags and current values
2080 * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
2081 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002082static void mod_cur_headers(struct pktgen_dev *pkt_dev)
2083{
2084 __u32 imn;
2085 __u32 imx;
2086 int flow = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
Luiz Capitulino222f1802006-03-20 22:16:13 -08002088 if (pkt_dev->cflows) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002089 flow = random32() % pkt_dev->cflows;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002090
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 if (pkt_dev->flows[flow].count > pkt_dev->lflow)
2092 pkt_dev->flows[flow].count = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002093 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094
2095 /* Deal with source MAC */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002096 if (pkt_dev->src_mac_count > 1) {
2097 __u32 mc;
2098 __u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099
Luiz Capitulino222f1802006-03-20 22:16:13 -08002100 if (pkt_dev->flags & F_MACSRC_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002101 mc = random32() % pkt_dev->src_mac_count;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002102 else {
2103 mc = pkt_dev->cur_src_mac_offset++;
2104 if (pkt_dev->cur_src_mac_offset >
2105 pkt_dev->src_mac_count)
2106 pkt_dev->cur_src_mac_offset = 0;
2107 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108
Luiz Capitulino222f1802006-03-20 22:16:13 -08002109 tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
2110 pkt_dev->hh[11] = tmp;
2111 tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2112 pkt_dev->hh[10] = tmp;
2113 tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2114 pkt_dev->hh[9] = tmp;
2115 tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2116 pkt_dev->hh[8] = tmp;
2117 tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
2118 pkt_dev->hh[7] = tmp;
2119 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120
Luiz Capitulino222f1802006-03-20 22:16:13 -08002121 /* Deal with Destination MAC */
2122 if (pkt_dev->dst_mac_count > 1) {
2123 __u32 mc;
2124 __u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125
Luiz Capitulino222f1802006-03-20 22:16:13 -08002126 if (pkt_dev->flags & F_MACDST_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002127 mc = random32() % pkt_dev->dst_mac_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128
Luiz Capitulino222f1802006-03-20 22:16:13 -08002129 else {
2130 mc = pkt_dev->cur_dst_mac_offset++;
2131 if (pkt_dev->cur_dst_mac_offset >
2132 pkt_dev->dst_mac_count) {
2133 pkt_dev->cur_dst_mac_offset = 0;
2134 }
2135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136
Luiz Capitulino222f1802006-03-20 22:16:13 -08002137 tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
2138 pkt_dev->hh[5] = tmp;
2139 tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2140 pkt_dev->hh[4] = tmp;
2141 tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2142 pkt_dev->hh[3] = tmp;
2143 tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2144 pkt_dev->hh[2] = tmp;
2145 tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
2146 pkt_dev->hh[1] = tmp;
2147 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002149 if (pkt_dev->flags & F_MPLS_RND) {
2150 unsigned i;
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002151 for (i = 0; i < pkt_dev->nr_labels; i++)
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002152 if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
2153 pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002154 ((__force __be32)random32() &
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002155 htonl(0x000fffff));
2156 }
2157
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002158 if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002159 pkt_dev->vlan_id = random32() & (4096-1);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002160 }
2161
2162 if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002163 pkt_dev->svlan_id = random32() & (4096 - 1);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002164 }
2165
Luiz Capitulino222f1802006-03-20 22:16:13 -08002166 if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
2167 if (pkt_dev->flags & F_UDPSRC_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002168 pkt_dev->cur_udp_src = random32() %
2169 (pkt_dev->udp_src_max - pkt_dev->udp_src_min)
2170 + pkt_dev->udp_src_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171
Luiz Capitulino222f1802006-03-20 22:16:13 -08002172 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 pkt_dev->cur_udp_src++;
2174 if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
2175 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002176 }
2177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
Luiz Capitulino222f1802006-03-20 22:16:13 -08002179 if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
2180 if (pkt_dev->flags & F_UDPDST_RND) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002181 pkt_dev->cur_udp_dst = random32() %
2182 (pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)
2183 + pkt_dev->udp_dst_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002184 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 pkt_dev->cur_udp_dst++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002186 if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002188 }
2189 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190
2191 if (!(pkt_dev->flags & F_IPV6)) {
2192
Luiz Capitulino222f1802006-03-20 22:16:13 -08002193 if ((imn = ntohl(pkt_dev->saddr_min)) < (imx =
2194 ntohl(pkt_dev->
2195 saddr_max))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 __u32 t;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002197 if (pkt_dev->flags & F_IPSRC_RND)
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002198 t = random32() % (imx - imn) + imn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 else {
2200 t = ntohl(pkt_dev->cur_saddr);
2201 t++;
2202 if (t > imx) {
2203 t = imn;
2204 }
2205 }
2206 pkt_dev->cur_saddr = htonl(t);
2207 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002208
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 if (pkt_dev->cflows && pkt_dev->flows[flow].count != 0) {
2210 pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
2211 } else {
Al Viro252e3342006-11-14 20:48:11 -08002212 imn = ntohl(pkt_dev->daddr_min);
2213 imx = ntohl(pkt_dev->daddr_max);
2214 if (imn < imx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 __u32 t;
Al Viro252e3342006-11-14 20:48:11 -08002216 __be32 s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 if (pkt_dev->flags & F_IPDST_RND) {
2218
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002219 t = random32() % (imx - imn) + imn;
Al Viro252e3342006-11-14 20:48:11 -08002220 s = htonl(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221
Al Viro252e3342006-11-14 20:48:11 -08002222 while (LOOPBACK(s) || MULTICAST(s)
2223 || BADCLASS(s) || ZERONET(s)
2224 || LOCAL_MCAST(s)) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002225 t = random32() % (imx - imn) + imn;
Al Viro252e3342006-11-14 20:48:11 -08002226 s = htonl(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 }
Al Viro252e3342006-11-14 20:48:11 -08002228 pkt_dev->cur_daddr = s;
2229 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 t = ntohl(pkt_dev->cur_daddr);
2231 t++;
2232 if (t > imx) {
2233 t = imn;
2234 }
2235 pkt_dev->cur_daddr = htonl(t);
2236 }
2237 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002238 if (pkt_dev->cflows) {
2239 pkt_dev->flows[flow].cur_daddr =
2240 pkt_dev->cur_daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 pkt_dev->nflows++;
2242 }
2243 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002244 } else { /* IPV6 * */
2245
2246 if (pkt_dev->min_in6_daddr.s6_addr32[0] == 0 &&
2247 pkt_dev->min_in6_daddr.s6_addr32[1] == 0 &&
2248 pkt_dev->min_in6_daddr.s6_addr32[2] == 0 &&
2249 pkt_dev->min_in6_daddr.s6_addr32[3] == 0) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 else {
2251 int i;
2252
2253 /* Only random destinations yet */
2254
Luiz Capitulino222f1802006-03-20 22:16:13 -08002255 for (i = 0; i < 4; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 pkt_dev->cur_in6_daddr.s6_addr32[i] =
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002257 (((__force __be32)random32() |
Luiz Capitulino222f1802006-03-20 22:16:13 -08002258 pkt_dev->min_in6_daddr.s6_addr32[i]) &
2259 pkt_dev->max_in6_daddr.s6_addr32[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 }
2263
Luiz Capitulino222f1802006-03-20 22:16:13 -08002264 if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
2265 __u32 t;
2266 if (pkt_dev->flags & F_TXSIZE_RND) {
Stephen Hemminger5fa6fc72007-03-04 16:07:28 -08002267 t = random32() %
2268 (pkt_dev->max_pkt_size - pkt_dev->min_pkt_size)
2269 + pkt_dev->min_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002270 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 t = pkt_dev->cur_pkt_size + 1;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002272 if (t > pkt_dev->max_pkt_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 t = pkt_dev->min_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002274 }
2275 pkt_dev->cur_pkt_size = t;
2276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277
2278 pkt_dev->flows[flow].count++;
2279}
2280
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002281static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
2282{
2283 unsigned i;
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002284 for (i = 0; i < pkt_dev->nr_labels; i++) {
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002285 *mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
2286 }
2287 mpls--;
2288 *mpls |= MPLS_STACK_BOTTOM;
2289}
2290
Al Viro0f37c602006-11-03 03:49:56 -08002291static inline __be16 build_tci(unsigned int id, unsigned int cfi,
2292 unsigned int prio)
2293{
2294 return htons(id | (cfi << 12) | (prio << 13));
2295}
2296
Luiz Capitulino222f1802006-03-20 22:16:13 -08002297static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
2298 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299{
2300 struct sk_buff *skb = NULL;
2301 __u8 *eth;
2302 struct udphdr *udph;
2303 int datalen, iplen;
2304 struct iphdr *iph;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002305 struct pktgen_hdr *pgh = NULL;
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002306 __be16 protocol = htons(ETH_P_IP);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002307 __be32 *mpls;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002308 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2309 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2310 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2311 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2312
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002313
2314 if (pkt_dev->nr_labels)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002315 protocol = htons(ETH_P_MPLS_UC);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002316
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002317 if (pkt_dev->vlan_id != 0xffff)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002318 protocol = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002319
Robert Olsson64053be2005-06-26 15:27:10 -07002320 /* Update any of the values, used when we're incrementing various
2321 * fields.
2322 */
2323 mod_cur_headers(pkt_dev);
2324
David S. Miller7ac54592006-01-18 14:19:10 -08002325 datalen = (odev->hard_header_len + 16) & ~0xf;
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002326 skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + datalen +
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002327 pkt_dev->nr_labels*sizeof(u32) +
2328 VLAN_TAG_SIZE(pkt_dev) + SVLAN_TAG_SIZE(pkt_dev),
2329 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 if (!skb) {
2331 sprintf(pkt_dev->result, "No memory");
2332 return NULL;
2333 }
2334
David S. Miller7ac54592006-01-18 14:19:10 -08002335 skb_reserve(skb, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336
2337 /* Reserve for ethernet and IP header */
2338 eth = (__u8 *) skb_push(skb, 14);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002339 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2340 if (pkt_dev->nr_labels)
2341 mpls_push(mpls, pkt_dev);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002342
2343 if (pkt_dev->vlan_id != 0xffff) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002344 if (pkt_dev->svlan_id != 0xffff) {
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002345 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002346 *svlan_tci = build_tci(pkt_dev->svlan_id,
2347 pkt_dev->svlan_cfi,
2348 pkt_dev->svlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002349 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002350 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002351 }
2352 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002353 *vlan_tci = build_tci(pkt_dev->vlan_id,
2354 pkt_dev->vlan_cfi,
2355 pkt_dev->vlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002356 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002357 *vlan_encapsulated_proto = htons(ETH_P_IP);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002358 }
2359
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002360 skb->network_header = skb->tail;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002361 skb->transport_header = skb->network_header + sizeof(struct iphdr);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002362 skb_put(skb, sizeof(struct iphdr) + sizeof(struct udphdr));
2363
2364 iph = ip_hdr(skb);
2365 udph = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 memcpy(eth, pkt_dev->hh, 12);
Al Viro252e3342006-11-14 20:48:11 -08002368 *(__be16 *) & eth[12] = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002370 /* Eth + IPh + UDPh + mpls */
2371 datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002372 pkt_dev->nr_labels*sizeof(u32) - VLAN_TAG_SIZE(pkt_dev) - SVLAN_TAG_SIZE(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002373 if (datalen < sizeof(struct pktgen_hdr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 datalen = sizeof(struct pktgen_hdr);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002375
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 udph->source = htons(pkt_dev->cur_udp_src);
2377 udph->dest = htons(pkt_dev->cur_udp_dst);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002378 udph->len = htons(datalen + 8); /* DATA + udphdr */
2379 udph->check = 0; /* No checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380
2381 iph->ihl = 5;
2382 iph->version = 4;
2383 iph->ttl = 32;
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002384 iph->tos = pkt_dev->tos;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002385 iph->protocol = IPPROTO_UDP; /* UDP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 iph->saddr = pkt_dev->cur_saddr;
2387 iph->daddr = pkt_dev->cur_daddr;
2388 iph->frag_off = 0;
2389 iplen = 20 + 8 + datalen;
2390 iph->tot_len = htons(iplen);
2391 iph->check = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002392 iph->check = ip_fast_csum((void *)iph, iph->ihl);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002393 skb->protocol = protocol;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002394 skb->mac_header = (skb->network_header - ETH_HLEN -
2395 pkt_dev->nr_labels * sizeof(u32) -
2396 VLAN_TAG_SIZE(pkt_dev) - SVLAN_TAG_SIZE(pkt_dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 skb->dev = odev;
2398 skb->pkt_type = PACKET_HOST;
2399
Luiz Capitulino222f1802006-03-20 22:16:13 -08002400 if (pkt_dev->nfrags <= 0)
2401 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 else {
2403 int frags = pkt_dev->nfrags;
2404 int i;
2405
Luiz Capitulino222f1802006-03-20 22:16:13 -08002406 pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
2407
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 if (frags > MAX_SKB_FRAGS)
2409 frags = MAX_SKB_FRAGS;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002410 if (datalen > frags * PAGE_SIZE) {
2411 skb_put(skb, datalen - frags * PAGE_SIZE);
2412 datalen = frags * PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 }
2414
2415 i = 0;
2416 while (datalen > 0) {
2417 struct page *page = alloc_pages(GFP_KERNEL, 0);
2418 skb_shinfo(skb)->frags[i].page = page;
2419 skb_shinfo(skb)->frags[i].page_offset = 0;
2420 skb_shinfo(skb)->frags[i].size =
Luiz Capitulino222f1802006-03-20 22:16:13 -08002421 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 datalen -= skb_shinfo(skb)->frags[i].size;
2423 skb->len += skb_shinfo(skb)->frags[i].size;
2424 skb->data_len += skb_shinfo(skb)->frags[i].size;
2425 i++;
2426 skb_shinfo(skb)->nr_frags = i;
2427 }
2428
2429 while (i < frags) {
2430 int rem;
2431
2432 if (i == 0)
2433 break;
2434
2435 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
2436 if (rem == 0)
2437 break;
2438
2439 skb_shinfo(skb)->frags[i - 1].size -= rem;
2440
Luiz Capitulino222f1802006-03-20 22:16:13 -08002441 skb_shinfo(skb)->frags[i] =
2442 skb_shinfo(skb)->frags[i - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 get_page(skb_shinfo(skb)->frags[i].page);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002444 skb_shinfo(skb)->frags[i].page =
2445 skb_shinfo(skb)->frags[i - 1].page;
2446 skb_shinfo(skb)->frags[i].page_offset +=
2447 skb_shinfo(skb)->frags[i - 1].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 skb_shinfo(skb)->frags[i].size = rem;
2449 i++;
2450 skb_shinfo(skb)->nr_frags = i;
2451 }
2452 }
2453
Luiz Capitulino222f1802006-03-20 22:16:13 -08002454 /* Stamp the time, and sequence number, convert them to network byte order */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455
Luiz Capitulino222f1802006-03-20 22:16:13 -08002456 if (pgh) {
2457 struct timeval timestamp;
2458
2459 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2460 pgh->seq_num = htonl(pkt_dev->seq_num);
2461
2462 do_gettimeofday(&timestamp);
2463 pgh->tv_sec = htonl(timestamp.tv_sec);
2464 pgh->tv_usec = htonl(timestamp.tv_usec);
2465 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002466
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 return skb;
2468}
2469
2470/*
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002471 * scan_ip6, fmt_ip taken from dietlibc-0.21
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 * Author Felix von Leitner <felix-dietlibc@fefe.de>
2473 *
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002474 * Slightly modified for kernel.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 * Should be candidate for net/ipv4/utils.c
2476 * --ro
2477 */
2478
Luiz Capitulino222f1802006-03-20 22:16:13 -08002479static unsigned int scan_ip6(const char *s, char ip[16])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480{
2481 unsigned int i;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002482 unsigned int len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 unsigned long u;
2484 char suffix[16];
Luiz Capitulino222f1802006-03-20 22:16:13 -08002485 unsigned int prefixlen = 0;
2486 unsigned int suffixlen = 0;
Al Viro252e3342006-11-14 20:48:11 -08002487 __be32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488
Luiz Capitulino222f1802006-03-20 22:16:13 -08002489 for (i = 0; i < 16; i++)
2490 ip[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491
2492 for (;;) {
2493 if (*s == ':') {
2494 len++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002495 if (s[1] == ':') { /* Found "::", skip to part 2 */
2496 s += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 len++;
2498 break;
2499 }
2500 s++;
2501 }
2502 {
2503 char *tmp;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002504 u = simple_strtoul(s, &tmp, 16);
2505 i = tmp - s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 }
2507
Luiz Capitulino222f1802006-03-20 22:16:13 -08002508 if (!i)
2509 return 0;
2510 if (prefixlen == 12 && s[i] == '.') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511
2512 /* the last 4 bytes may be written as IPv4 address */
2513
2514 tmp = in_aton(s);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002515 memcpy((struct in_addr *)(ip + 12), &tmp, sizeof(tmp));
2516 return i + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 }
2518 ip[prefixlen++] = (u >> 8);
2519 ip[prefixlen++] = (u & 255);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002520 s += i;
2521 len += i;
2522 if (prefixlen == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 return len;
2524 }
2525
2526/* part 2, after "::" */
2527 for (;;) {
2528 if (*s == ':') {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002529 if (suffixlen == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 break;
2531 s++;
2532 len++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002533 } else if (suffixlen != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 break;
2535 {
2536 char *tmp;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002537 u = simple_strtol(s, &tmp, 16);
2538 i = tmp - s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 }
2540 if (!i) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002541 if (*s)
2542 len--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 break;
2544 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002545 if (suffixlen + prefixlen <= 12 && s[i] == '.') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 tmp = in_aton(s);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002547 memcpy((struct in_addr *)(suffix + suffixlen), &tmp,
2548 sizeof(tmp));
2549 suffixlen += 4;
2550 len += strlen(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 break;
2552 }
2553 suffix[suffixlen++] = (u >> 8);
2554 suffix[suffixlen++] = (u & 255);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002555 s += i;
2556 len += i;
2557 if (prefixlen + suffixlen == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 break;
2559 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002560 for (i = 0; i < suffixlen; i++)
2561 ip[16 - suffixlen + i] = suffix[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 return len;
2563}
2564
Luiz Capitulino222f1802006-03-20 22:16:13 -08002565static char tohex(char hexdigit)
2566{
2567 return hexdigit > 9 ? hexdigit + 'a' - 10 : hexdigit + '0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568}
2569
Luiz Capitulino222f1802006-03-20 22:16:13 -08002570static int fmt_xlong(char *s, unsigned int i)
2571{
2572 char *bak = s;
2573 *s = tohex((i >> 12) & 0xf);
2574 if (s != bak || *s != '0')
2575 ++s;
2576 *s = tohex((i >> 8) & 0xf);
2577 if (s != bak || *s != '0')
2578 ++s;
2579 *s = tohex((i >> 4) & 0xf);
2580 if (s != bak || *s != '0')
2581 ++s;
2582 *s = tohex(i & 0xf);
2583 return s - bak + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584}
2585
Luiz Capitulino222f1802006-03-20 22:16:13 -08002586static unsigned int fmt_ip6(char *s, const char ip[16])
2587{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 unsigned int len;
2589 unsigned int i;
2590 unsigned int temp;
2591 unsigned int compressing;
2592 int j;
2593
Luiz Capitulino222f1802006-03-20 22:16:13 -08002594 len = 0;
2595 compressing = 0;
2596 for (j = 0; j < 16; j += 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597
2598#ifdef V4MAPPEDPREFIX
Luiz Capitulino222f1802006-03-20 22:16:13 -08002599 if (j == 12 && !memcmp(ip, V4mappedprefix, 12)) {
2600 inet_ntoa_r(*(struct in_addr *)(ip + 12), s);
2601 temp = strlen(s);
2602 return len + temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 }
2604#endif
Luiz Capitulino222f1802006-03-20 22:16:13 -08002605 temp = ((unsigned long)(unsigned char)ip[j] << 8) +
2606 (unsigned long)(unsigned char)ip[j + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 if (temp == 0) {
2608 if (!compressing) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002609 compressing = 1;
2610 if (j == 0) {
2611 *s++ = ':';
2612 ++len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 }
2614 }
2615 } else {
2616 if (compressing) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002617 compressing = 0;
2618 *s++ = ':';
2619 ++len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002621 i = fmt_xlong(s, temp);
2622 len += i;
2623 s += i;
2624 if (j < 14) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 *s++ = ':';
2626 ++len;
2627 }
2628 }
2629 }
2630 if (compressing) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08002631 *s++ = ':';
2632 ++len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002634 *s = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 return len;
2636}
2637
Luiz Capitulino222f1802006-03-20 22:16:13 -08002638static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
2639 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640{
2641 struct sk_buff *skb = NULL;
2642 __u8 *eth;
2643 struct udphdr *udph;
2644 int datalen;
2645 struct ipv6hdr *iph;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002646 struct pktgen_hdr *pgh = NULL;
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002647 __be16 protocol = htons(ETH_P_IPV6);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002648 __be32 *mpls;
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002649 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2650 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2651 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2652 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002653
2654 if (pkt_dev->nr_labels)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002655 protocol = htons(ETH_P_MPLS_UC);
Robert Olsson64053be2005-06-26 15:27:10 -07002656
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002657 if (pkt_dev->vlan_id != 0xffff)
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002658 protocol = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002659
Robert Olsson64053be2005-06-26 15:27:10 -07002660 /* Update any of the values, used when we're incrementing various
2661 * fields.
2662 */
2663 mod_cur_headers(pkt_dev);
2664
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002665 skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + 16 +
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002666 pkt_dev->nr_labels*sizeof(u32) +
2667 VLAN_TAG_SIZE(pkt_dev) + SVLAN_TAG_SIZE(pkt_dev),
2668 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 if (!skb) {
2670 sprintf(pkt_dev->result, "No memory");
2671 return NULL;
2672 }
2673
2674 skb_reserve(skb, 16);
2675
2676 /* Reserve for ethernet and IP header */
2677 eth = (__u8 *) skb_push(skb, 14);
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002678 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2679 if (pkt_dev->nr_labels)
2680 mpls_push(mpls, pkt_dev);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002681
2682 if (pkt_dev->vlan_id != 0xffff) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002683 if (pkt_dev->svlan_id != 0xffff) {
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002684 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002685 *svlan_tci = build_tci(pkt_dev->svlan_id,
2686 pkt_dev->svlan_cfi,
2687 pkt_dev->svlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002688 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002689 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002690 }
2691 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
Al Viro0f37c602006-11-03 03:49:56 -08002692 *vlan_tci = build_tci(pkt_dev->vlan_id,
2693 pkt_dev->vlan_cfi,
2694 pkt_dev->vlan_p);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002695 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002696 *vlan_encapsulated_proto = htons(ETH_P_IPV6);
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002697 }
2698
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002699 skb->network_header = skb->tail;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002700 skb->transport_header = skb->network_header + sizeof(struct ipv6hdr);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002701 skb_put(skb, sizeof(struct ipv6hdr) + sizeof(struct udphdr));
2702
2703 iph = ipv6_hdr(skb);
2704 udph = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 memcpy(eth, pkt_dev->hh, 12);
Al Viro252e3342006-11-14 20:48:11 -08002707 *(__be16 *) & eth[12] = protocol;
Robert Olsson64053be2005-06-26 15:27:10 -07002708
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002709 /* Eth + IPh + UDPh + mpls */
2710 datalen = pkt_dev->cur_pkt_size - 14 -
2711 sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002712 pkt_dev->nr_labels*sizeof(u32) - VLAN_TAG_SIZE(pkt_dev) - SVLAN_TAG_SIZE(pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713
Luiz Capitulino222f1802006-03-20 22:16:13 -08002714 if (datalen < sizeof(struct pktgen_hdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 datalen = sizeof(struct pktgen_hdr);
2716 if (net_ratelimit())
Luiz Capitulino222f1802006-03-20 22:16:13 -08002717 printk(KERN_INFO "pktgen: increased datalen to %d\n",
2718 datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 }
2720
2721 udph->source = htons(pkt_dev->cur_udp_src);
2722 udph->dest = htons(pkt_dev->cur_udp_dst);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002723 udph->len = htons(datalen + sizeof(struct udphdr));
2724 udph->check = 0; /* No checksum */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725
Stephen Hemmingerd5f1ce92007-03-04 16:08:08 -08002726 *(__be32 *) iph = htonl(0x60000000); /* Version + flow */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002728 if (pkt_dev->traffic_class) {
2729 /* Version + traffic class + flow (0) */
Al Viro252e3342006-11-14 20:48:11 -08002730 *(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
Francesco Fondelli1ca77682006-09-27 16:32:03 -07002731 }
2732
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 iph->hop_limit = 32;
2734
2735 iph->payload_len = htons(sizeof(struct udphdr) + datalen);
2736 iph->nexthdr = IPPROTO_UDP;
2737
2738 ipv6_addr_copy(&iph->daddr, &pkt_dev->cur_in6_daddr);
2739 ipv6_addr_copy(&iph->saddr, &pkt_dev->cur_in6_saddr);
2740
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002741 skb->mac_header = (skb->network_header - ETH_HLEN -
2742 pkt_dev->nr_labels * sizeof(u32) -
2743 VLAN_TAG_SIZE(pkt_dev) - SVLAN_TAG_SIZE(pkt_dev));
Steven Whitehouseca6549a2006-03-23 01:10:26 -08002744 skb->protocol = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 skb->dev = odev;
2746 skb->pkt_type = PACKET_HOST;
2747
Luiz Capitulino222f1802006-03-20 22:16:13 -08002748 if (pkt_dev->nfrags <= 0)
2749 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 else {
2751 int frags = pkt_dev->nfrags;
2752 int i;
2753
Luiz Capitulino222f1802006-03-20 22:16:13 -08002754 pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
2755
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 if (frags > MAX_SKB_FRAGS)
2757 frags = MAX_SKB_FRAGS;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002758 if (datalen > frags * PAGE_SIZE) {
2759 skb_put(skb, datalen - frags * PAGE_SIZE);
2760 datalen = frags * PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 }
2762
2763 i = 0;
2764 while (datalen > 0) {
2765 struct page *page = alloc_pages(GFP_KERNEL, 0);
2766 skb_shinfo(skb)->frags[i].page = page;
2767 skb_shinfo(skb)->frags[i].page_offset = 0;
2768 skb_shinfo(skb)->frags[i].size =
Luiz Capitulino222f1802006-03-20 22:16:13 -08002769 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 datalen -= skb_shinfo(skb)->frags[i].size;
2771 skb->len += skb_shinfo(skb)->frags[i].size;
2772 skb->data_len += skb_shinfo(skb)->frags[i].size;
2773 i++;
2774 skb_shinfo(skb)->nr_frags = i;
2775 }
2776
2777 while (i < frags) {
2778 int rem;
2779
2780 if (i == 0)
2781 break;
2782
2783 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
2784 if (rem == 0)
2785 break;
2786
2787 skb_shinfo(skb)->frags[i - 1].size -= rem;
2788
Luiz Capitulino222f1802006-03-20 22:16:13 -08002789 skb_shinfo(skb)->frags[i] =
2790 skb_shinfo(skb)->frags[i - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 get_page(skb_shinfo(skb)->frags[i].page);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002792 skb_shinfo(skb)->frags[i].page =
2793 skb_shinfo(skb)->frags[i - 1].page;
2794 skb_shinfo(skb)->frags[i].page_offset +=
2795 skb_shinfo(skb)->frags[i - 1].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 skb_shinfo(skb)->frags[i].size = rem;
2797 i++;
2798 skb_shinfo(skb)->nr_frags = i;
2799 }
2800 }
2801
Luiz Capitulino222f1802006-03-20 22:16:13 -08002802 /* Stamp the time, and sequence number, convert them to network byte order */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 /* should we update cloned packets too ? */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002804 if (pgh) {
2805 struct timeval timestamp;
2806
2807 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2808 pgh->seq_num = htonl(pkt_dev->seq_num);
2809
2810 do_gettimeofday(&timestamp);
2811 pgh->tv_sec = htonl(timestamp.tv_sec);
2812 pgh->tv_usec = htonl(timestamp.tv_usec);
2813 }
Francesco Fondelli34954dd2006-09-27 16:30:44 -07002814 /* pkt_dev->seq_num++; FF: you really mean this? */
Luiz Capitulino222f1802006-03-20 22:16:13 -08002815
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 return skb;
2817}
2818
Luiz Capitulino222f1802006-03-20 22:16:13 -08002819static inline struct sk_buff *fill_packet(struct net_device *odev,
2820 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821{
Luiz Capitulino222f1802006-03-20 22:16:13 -08002822 if (pkt_dev->flags & F_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 return fill_packet_ipv6(odev, pkt_dev);
2824 else
2825 return fill_packet_ipv4(odev, pkt_dev);
2826}
2827
Luiz Capitulino222f1802006-03-20 22:16:13 -08002828static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829{
Luiz Capitulino222f1802006-03-20 22:16:13 -08002830 pkt_dev->seq_num = 1;
2831 pkt_dev->idle_acc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002833 pkt_dev->tx_bytes = 0;
2834 pkt_dev->errors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835}
2836
2837/* Set up structure for sending pkts, clear counters */
2838
2839static void pktgen_run(struct pktgen_thread *t)
2840{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08002841 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 int started = 0;
2843
Stephen Hemminger25c4e532007-03-04 16:06:47 -08002844 pr_debug("pktgen: entering pktgen_run. %p\n", t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845
2846 if_lock(t);
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08002847 list_for_each_entry(pkt_dev, &t->if_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848
2849 /*
2850 * setup odev and create initial packet.
2851 */
2852 pktgen_setup_inject(pkt_dev);
2853
Luiz Capitulino222f1802006-03-20 22:16:13 -08002854 if (pkt_dev->odev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 pktgen_clear_counters(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002856 pkt_dev->running = 1; /* Cranke yeself! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 pkt_dev->skb = NULL;
2858 pkt_dev->started_at = getCurUs();
Luiz Capitulino222f1802006-03-20 22:16:13 -08002859 pkt_dev->next_tx_us = getCurUs(); /* Transmit immediately */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 pkt_dev->next_tx_ns = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002861
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 strcpy(pkt_dev->result, "Starting");
2863 started++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002864 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 strcpy(pkt_dev->result, "Error starting");
2866 }
2867 if_unlock(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002868 if (started)
2869 t->control &= ~(T_STOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870}
2871
2872static void pktgen_stop_all_threads_ifs(void)
2873{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002874 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875
Stephen Hemminger25c4e532007-03-04 16:06:47 -08002876 pr_debug("pktgen: entering pktgen_stop_all_threads_ifs.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08002878 mutex_lock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002879
2880 list_for_each_entry(t, &pktgen_threads, th_list)
Arthur Kepner95ed63f2006-03-20 21:26:56 -08002881 t->control |= T_STOP;
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002882
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08002883 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884}
2885
Luiz Capitulino222f1802006-03-20 22:16:13 -08002886static int thread_is_running(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08002888 struct pktgen_dev *pkt_dev;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002889 int res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08002891 list_for_each_entry(pkt_dev, &t->if_list, list)
2892 if (pkt_dev->running) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 res = 1;
2894 break;
2895 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08002896 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897}
2898
Luiz Capitulino222f1802006-03-20 22:16:13 -08002899static int pktgen_wait_thread_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900{
Luiz Capitulino222f1802006-03-20 22:16:13 -08002901 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902
Luiz Capitulino222f1802006-03-20 22:16:13 -08002903 while (thread_is_running(t)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904
Luiz Capitulino222f1802006-03-20 22:16:13 -08002905 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906
Luiz Capitulino222f1802006-03-20 22:16:13 -08002907 msleep_interruptible(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908
Luiz Capitulino222f1802006-03-20 22:16:13 -08002909 if (signal_pending(current))
2910 goto signal;
2911 if_lock(t);
2912 }
2913 if_unlock(t);
2914 return 1;
2915signal:
2916 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917}
2918
2919static int pktgen_wait_all_threads_run(void)
2920{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002921 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 int sig = 1;
Luiz Capitulino222f1802006-03-20 22:16:13 -08002923
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08002924 mutex_lock(&pktgen_thread_lock);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002925
2926 list_for_each_entry(t, &pktgen_threads, th_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 sig = pktgen_wait_thread_run(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08002928 if (sig == 0)
2929 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 }
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002931
2932 if (sig == 0)
2933 list_for_each_entry(t, &pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 t->control |= (T_STOP);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002935
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08002936 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 return sig;
2938}
2939
2940static void pktgen_run_all_threads(void)
2941{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002942 struct pktgen_thread *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943
Stephen Hemminger25c4e532007-03-04 16:06:47 -08002944 pr_debug("pktgen: entering pktgen_run_all_threads.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08002946 mutex_lock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002948 list_for_each_entry(t, &pktgen_threads, th_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 t->control |= (T_RUN);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08002950
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08002951 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952
Luiz Capitulino222f1802006-03-20 22:16:13 -08002953 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
2954
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 pktgen_wait_all_threads_run();
2956}
2957
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
2959{
Luiz Capitulino222f1802006-03-20 22:16:13 -08002960 __u64 total_us, bps, mbps, pps, idle;
2961 char *p = pkt_dev->result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962
Luiz Capitulino222f1802006-03-20 22:16:13 -08002963 total_us = pkt_dev->stopped_at - pkt_dev->started_at;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964
Luiz Capitulino222f1802006-03-20 22:16:13 -08002965 idle = pkt_dev->idle_acc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966
Luiz Capitulino222f1802006-03-20 22:16:13 -08002967 p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
2968 (unsigned long long)total_us,
2969 (unsigned long long)(total_us - idle),
2970 (unsigned long long)idle,
2971 (unsigned long long)pkt_dev->sofar,
2972 pkt_dev->cur_pkt_size, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973
Luiz Capitulino222f1802006-03-20 22:16:13 -08002974 pps = pkt_dev->sofar * USEC_PER_SEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975
Luiz Capitulino222f1802006-03-20 22:16:13 -08002976 while ((total_us >> 32) != 0) {
2977 pps >>= 1;
2978 total_us >>= 1;
2979 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980
Luiz Capitulino222f1802006-03-20 22:16:13 -08002981 do_div(pps, total_us);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982
Luiz Capitulino222f1802006-03-20 22:16:13 -08002983 bps = pps * 8 * pkt_dev->cur_pkt_size;
2984
2985 mbps = bps;
2986 do_div(mbps, 1000000);
2987 p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu",
2988 (unsigned long long)pps,
2989 (unsigned long long)mbps,
2990 (unsigned long long)bps,
2991 (unsigned long long)pkt_dev->errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993
2994/* Set stopped-at timer, remove from running list, do counters & statistics */
2995
Luiz Capitulino222f1802006-03-20 22:16:13 -08002996static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997{
Luiz Capitulino222f1802006-03-20 22:16:13 -08002998 int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08002999
Luiz Capitulino222f1802006-03-20 22:16:13 -08003000 if (!pkt_dev->running) {
3001 printk("pktgen: interface: %s is already stopped\n",
Stephen Hemminger39df2322007-03-04 16:11:51 -08003002 pkt_dev->odev->name);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003003 return -EINVAL;
3004 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005
Luiz Capitulino222f1802006-03-20 22:16:13 -08003006 pkt_dev->stopped_at = getCurUs();
3007 pkt_dev->running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003009 show_results(pkt_dev, nr_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010
Luiz Capitulino222f1802006-03-20 22:16:13 -08003011 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012}
3013
Luiz Capitulino222f1802006-03-20 22:16:13 -08003014static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003016 struct pktgen_dev *pkt_dev, *best = NULL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003017
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 if_lock(t);
3019
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003020 list_for_each_entry(pkt_dev, &t->if_list, list) {
3021 if (!pkt_dev->running)
Luiz Capitulino222f1802006-03-20 22:16:13 -08003022 continue;
3023 if (best == NULL)
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003024 best = pkt_dev;
3025 else if (pkt_dev->next_tx_us < best->next_tx_us)
3026 best = pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 }
3028 if_unlock(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003029 return best;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030}
3031
Luiz Capitulino222f1802006-03-20 22:16:13 -08003032static void pktgen_stop(struct pktgen_thread *t)
3033{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003034 struct pktgen_dev *pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003036 pr_debug("pktgen: entering pktgen_stop\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037
Luiz Capitulino222f1802006-03-20 22:16:13 -08003038 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003040 list_for_each_entry(pkt_dev, &t->if_list, list) {
3041 pktgen_stop_device(pkt_dev);
3042 if (pkt_dev->skb)
3043 kfree_skb(pkt_dev->skb);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003044
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003045 pkt_dev->skb = NULL;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003046 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047
Luiz Capitulino222f1802006-03-20 22:16:13 -08003048 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049}
3050
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003051/*
3052 * one of our devices needs to be removed - find it
3053 * and remove it
3054 */
3055static void pktgen_rem_one_if(struct pktgen_thread *t)
3056{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003057 struct list_head *q, *n;
3058 struct pktgen_dev *cur;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003059
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003060 pr_debug("pktgen: entering pktgen_rem_one_if\n");
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003061
3062 if_lock(t);
3063
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003064 list_for_each_safe(q, n, &t->if_list) {
3065 cur = list_entry(q, struct pktgen_dev, list);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003066
Luiz Capitulino222f1802006-03-20 22:16:13 -08003067 if (!cur->removal_mark)
3068 continue;
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003069
3070 if (cur->skb)
3071 kfree_skb(cur->skb);
3072 cur->skb = NULL;
3073
3074 pktgen_remove_device(t, cur);
3075
3076 break;
3077 }
3078
3079 if_unlock(t);
3080}
3081
Luiz Capitulino222f1802006-03-20 22:16:13 -08003082static void pktgen_rem_all_ifs(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003084 struct list_head *q, *n;
3085 struct pktgen_dev *cur;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003086
3087 /* Remove all devices, free mem */
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003088
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003089 pr_debug("pktgen: entering pktgen_rem_all_ifs\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003090 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003092 list_for_each_safe(q, n, &t->if_list) {
3093 cur = list_entry(q, struct pktgen_dev, list);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003094
3095 if (cur->skb)
3096 kfree_skb(cur->skb);
3097 cur->skb = NULL;
3098
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 pktgen_remove_device(t, cur);
3100 }
3101
Luiz Capitulino222f1802006-03-20 22:16:13 -08003102 if_unlock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103}
3104
Luiz Capitulino222f1802006-03-20 22:16:13 -08003105static void pktgen_rem_thread(struct pktgen_thread *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003107 /* Remove from the thread list */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108
David S. Milleree74baa2007-01-01 20:51:53 -08003109 remove_proc_entry(t->tsk->comm, pg_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003111 mutex_lock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003113 list_del(&t->th_list);
3114
Luiz Capitulino6146e6a2006-03-20 22:24:45 -08003115 mutex_unlock(&pktgen_thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116}
3117
3118static __inline__ void pktgen_xmit(struct pktgen_dev *pkt_dev)
3119{
3120 struct net_device *odev = NULL;
3121 __u64 idle_start = 0;
3122 int ret;
3123
3124 odev = pkt_dev->odev;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003125
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 if (pkt_dev->delay_us || pkt_dev->delay_ns) {
3127 u64 now;
3128
3129 now = getCurUs();
3130 if (now < pkt_dev->next_tx_us)
3131 spin(pkt_dev, pkt_dev->next_tx_us);
3132
3133 /* This is max DELAY, this has special meaning of
3134 * "never transmit"
3135 */
3136 if (pkt_dev->delay_us == 0x7FFFFFFF) {
3137 pkt_dev->next_tx_us = getCurUs() + pkt_dev->delay_us;
3138 pkt_dev->next_tx_ns = pkt_dev->delay_ns;
3139 goto out;
3140 }
3141 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003142
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 if (netif_queue_stopped(odev) || need_resched()) {
3144 idle_start = getCurUs();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003145
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 if (!netif_running(odev)) {
3147 pktgen_stop_device(pkt_dev);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003148 if (pkt_dev->skb)
3149 kfree_skb(pkt_dev->skb);
3150 pkt_dev->skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 goto out;
3152 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003153 if (need_resched())
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 schedule();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003155
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 pkt_dev->idle_acc += getCurUs() - idle_start;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003157
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 if (netif_queue_stopped(odev)) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08003159 pkt_dev->next_tx_us = getCurUs(); /* TODO */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 pkt_dev->next_tx_ns = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003161 goto out; /* Try the next interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 }
3163 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003164
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 if (pkt_dev->last_ok || !pkt_dev->skb) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08003166 if ((++pkt_dev->clone_count >= pkt_dev->clone_skb)
3167 || (!pkt_dev->skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 /* build a new pkt */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003169 if (pkt_dev->skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170 kfree_skb(pkt_dev->skb);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003171
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 pkt_dev->skb = fill_packet(odev, pkt_dev);
3173 if (pkt_dev->skb == NULL) {
3174 printk("pktgen: ERROR: couldn't allocate skb in fill_packet.\n");
3175 schedule();
Luiz Capitulino222f1802006-03-20 22:16:13 -08003176 pkt_dev->clone_count--; /* back out increment, OOM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 goto out;
3178 }
3179 pkt_dev->allocated_skbs++;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003180 pkt_dev->clone_count = 0; /* reset counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 }
3182 }
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003183
Herbert Xu932ff272006-06-09 12:20:56 -07003184 netif_tx_lock_bh(odev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 if (!netif_queue_stopped(odev)) {
3186
3187 atomic_inc(&(pkt_dev->skb->users));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003188 retry_now:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 ret = odev->hard_start_xmit(pkt_dev->skb, odev);
3190 if (likely(ret == NETDEV_TX_OK)) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08003191 pkt_dev->last_ok = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 pkt_dev->sofar++;
3193 pkt_dev->seq_num++;
3194 pkt_dev->tx_bytes += pkt_dev->cur_pkt_size;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003195
3196 } else if (ret == NETDEV_TX_LOCKED
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 && (odev->features & NETIF_F_LLTX)) {
3198 cpu_relax();
3199 goto retry_now;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003200 } else { /* Retry it next time */
3201
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 atomic_dec(&(pkt_dev->skb->users));
Luiz Capitulino222f1802006-03-20 22:16:13 -08003203
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 if (debug && net_ratelimit())
3205 printk(KERN_INFO "pktgen: Hard xmit error\n");
Luiz Capitulino222f1802006-03-20 22:16:13 -08003206
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 pkt_dev->errors++;
3208 pkt_dev->last_ok = 0;
3209 }
3210
3211 pkt_dev->next_tx_us = getCurUs();
3212 pkt_dev->next_tx_ns = 0;
3213
3214 pkt_dev->next_tx_us += pkt_dev->delay_us;
3215 pkt_dev->next_tx_ns += pkt_dev->delay_ns;
3216
3217 if (pkt_dev->next_tx_ns > 1000) {
3218 pkt_dev->next_tx_us++;
3219 pkt_dev->next_tx_ns -= 1000;
3220 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222
Luiz Capitulino222f1802006-03-20 22:16:13 -08003223 else { /* Retry it next time */
3224 pkt_dev->last_ok = 0;
3225 pkt_dev->next_tx_us = getCurUs(); /* TODO */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 pkt_dev->next_tx_ns = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003227 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228
Herbert Xu932ff272006-06-09 12:20:56 -07003229 netif_tx_unlock_bh(odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003230
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 /* If pkt_dev->count is zero, then run forever */
3232 if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
3233 if (atomic_read(&(pkt_dev->skb->users)) != 1) {
3234 idle_start = getCurUs();
3235 while (atomic_read(&(pkt_dev->skb->users)) != 1) {
3236 if (signal_pending(current)) {
3237 break;
3238 }
3239 schedule();
3240 }
3241 pkt_dev->idle_acc += getCurUs() - idle_start;
3242 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003243
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 /* Done with this */
3245 pktgen_stop_device(pkt_dev);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003246 if (pkt_dev->skb)
3247 kfree_skb(pkt_dev->skb);
3248 pkt_dev->skb = NULL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003249 }
3250out:;
3251}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003253/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 * Main loop of the thread goes here
3255 */
3256
David S. Milleree74baa2007-01-01 20:51:53 -08003257static int pktgen_thread_worker(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258{
3259 DEFINE_WAIT(wait);
David S. Milleree74baa2007-01-01 20:51:53 -08003260 struct pktgen_thread *t = arg;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003261 struct pktgen_dev *pkt_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 int cpu = t->cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 u32 max_before_softirq;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003264 u32 tx_since_softirq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265
David S. Milleree74baa2007-01-01 20:51:53 -08003266 BUG_ON(smp_processor_id() != cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267
3268 init_waitqueue_head(&t->queue);
3269
Luiz Capitulino222f1802006-03-20 22:16:13 -08003270 t->pid = current->pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003272 pr_debug("pktgen: starting pktgen/%d: pid=%d\n", cpu, current->pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273
3274 max_before_softirq = t->max_before_softirq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275
David S. Milleree74baa2007-01-01 20:51:53 -08003276 set_current_state(TASK_INTERRUPTIBLE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003277
David S. Milleree74baa2007-01-01 20:51:53 -08003278 while (!kthread_should_stop()) {
3279 pkt_dev = next_to_run(t);
3280
3281 if (!pkt_dev &&
3282 (t->control & (T_STOP | T_RUN | T_REMDEVALL | T_REMDEV))
3283 == 0) {
3284 prepare_to_wait(&(t->queue), &wait,
3285 TASK_INTERRUPTIBLE);
3286 schedule_timeout(HZ / 10);
3287 finish_wait(&(t->queue), &wait);
3288 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003289
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 __set_current_state(TASK_RUNNING);
3291
Luiz Capitulino222f1802006-03-20 22:16:13 -08003292 if (pkt_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293
3294 pktgen_xmit(pkt_dev);
3295
3296 /*
3297 * We like to stay RUNNING but must also give
3298 * others fair share.
3299 */
3300
3301 tx_since_softirq += pkt_dev->last_ok;
3302
3303 if (tx_since_softirq > max_before_softirq) {
3304 if (local_softirq_pending())
3305 do_softirq();
3306 tx_since_softirq = 0;
3307 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 }
3309
Luiz Capitulino222f1802006-03-20 22:16:13 -08003310 if (t->control & T_STOP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 pktgen_stop(t);
3312 t->control &= ~(T_STOP);
3313 }
3314
Luiz Capitulino222f1802006-03-20 22:16:13 -08003315 if (t->control & T_RUN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 pktgen_run(t);
3317 t->control &= ~(T_RUN);
3318 }
3319
Luiz Capitulino222f1802006-03-20 22:16:13 -08003320 if (t->control & T_REMDEVALL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 pktgen_rem_all_ifs(t);
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003322 t->control &= ~(T_REMDEVALL);
3323 }
3324
Luiz Capitulino222f1802006-03-20 22:16:13 -08003325 if (t->control & T_REMDEV) {
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003326 pktgen_rem_one_if(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327 t->control &= ~(T_REMDEV);
3328 }
3329
Andrew Morton09fe3ef2007-04-12 14:45:32 -07003330 try_to_freeze();
3331
David S. Milleree74baa2007-01-01 20:51:53 -08003332 set_current_state(TASK_INTERRUPTIBLE);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003335 pr_debug("pktgen: %s stopping all device\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003336 pktgen_stop(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003338 pr_debug("pktgen: %s removing all device\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003339 pktgen_rem_all_ifs(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003341 pr_debug("pktgen: %s removing thread.\n", t->tsk->comm);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003342 pktgen_rem_thread(t);
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003343
David S. Milleree74baa2007-01-01 20:51:53 -08003344 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345}
3346
Luiz Capitulino222f1802006-03-20 22:16:13 -08003347static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
3348 const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003350 struct pktgen_dev *p, *pkt_dev = NULL;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003351 if_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003353 list_for_each_entry(p, &t->if_list, list)
Stephen Hemminger39df2322007-03-04 16:11:51 -08003354 if (strncmp(p->odev->name, ifname, IFNAMSIZ) == 0) {
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003355 pkt_dev = p;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003356 break;
3357 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358
Luiz Capitulino222f1802006-03-20 22:16:13 -08003359 if_unlock(t);
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003360 pr_debug("pktgen: find_dev(%s) returning %p\n", ifname, pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003361 return pkt_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362}
3363
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003364/*
3365 * Adds a dev at front of if_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 */
3367
Luiz Capitulino222f1802006-03-20 22:16:13 -08003368static int add_dev_to_thread(struct pktgen_thread *t,
3369 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370{
3371 int rv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372
Luiz Capitulino222f1802006-03-20 22:16:13 -08003373 if_lock(t);
3374
3375 if (pkt_dev->pg_thread) {
3376 printk("pktgen: ERROR: already assigned to a thread.\n");
3377 rv = -EBUSY;
3378 goto out;
3379 }
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003380
3381 list_add(&pkt_dev->list, &t->if_list);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003382 pkt_dev->pg_thread = t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 pkt_dev->running = 0;
3384
Luiz Capitulino222f1802006-03-20 22:16:13 -08003385out:
3386 if_unlock(t);
3387 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388}
3389
3390/* Called under thread lock */
3391
Luiz Capitulino222f1802006-03-20 22:16:13 -08003392static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393{
Luiz Capitulino222f1802006-03-20 22:16:13 -08003394 struct pktgen_dev *pkt_dev;
Stephen Hemminger39df2322007-03-04 16:11:51 -08003395 int err;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003396
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 /* We don't allow a device to be on several threads */
3398
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003399 pkt_dev = __pktgen_NN_threads(ifname, FIND);
3400 if (pkt_dev) {
Luiz Capitulino222f1802006-03-20 22:16:13 -08003401 printk("pktgen: ERROR: interface already used.\n");
3402 return -EBUSY;
3403 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003404
3405 pkt_dev = kzalloc(sizeof(struct pktgen_dev), GFP_KERNEL);
3406 if (!pkt_dev)
3407 return -ENOMEM;
3408
Luiz Capitulino222f1802006-03-20 22:16:13 -08003409 pkt_dev->flows = vmalloc(MAX_CFLOWS * sizeof(struct flow_state));
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003410 if (pkt_dev->flows == NULL) {
3411 kfree(pkt_dev);
3412 return -ENOMEM;
3413 }
Luiz Capitulino222f1802006-03-20 22:16:13 -08003414 memset(pkt_dev->flows, 0, MAX_CFLOWS * sizeof(struct flow_state));
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003415
Arthur Kepner95ed63f2006-03-20 21:26:56 -08003416 pkt_dev->removal_mark = 0;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003417 pkt_dev->min_pkt_size = ETH_ZLEN;
3418 pkt_dev->max_pkt_size = ETH_ZLEN;
3419 pkt_dev->nfrags = 0;
3420 pkt_dev->clone_skb = pg_clone_skb_d;
3421 pkt_dev->delay_us = pg_delay_d / 1000;
3422 pkt_dev->delay_ns = pg_delay_d % 1000;
3423 pkt_dev->count = pg_count_d;
3424 pkt_dev->sofar = 0;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003425 pkt_dev->udp_src_min = 9; /* sink port */
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003426 pkt_dev->udp_src_max = 9;
3427 pkt_dev->udp_dst_min = 9;
3428 pkt_dev->udp_dst_max = 9;
3429
Francesco Fondelli34954dd2006-09-27 16:30:44 -07003430 pkt_dev->vlan_p = 0;
3431 pkt_dev->vlan_cfi = 0;
3432 pkt_dev->vlan_id = 0xffff;
3433 pkt_dev->svlan_p = 0;
3434 pkt_dev->svlan_cfi = 0;
3435 pkt_dev->svlan_id = 0xffff;
3436
Stephen Hemminger39df2322007-03-04 16:11:51 -08003437 err = pktgen_setup_dev(pkt_dev, ifname);
3438 if (err)
3439 goto out1;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003440
Stephen Hemminger39df2322007-03-04 16:11:51 -08003441 pkt_dev->entry = create_proc_entry(ifname, 0600, pg_proc_dir);
3442 if (!pkt_dev->entry) {
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003443 printk("pktgen: cannot create %s/%s procfs entry.\n",
3444 PG_PROC_DIR, ifname);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003445 err = -EINVAL;
3446 goto out2;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003447 }
Stephen Hemminger39df2322007-03-04 16:11:51 -08003448 pkt_dev->entry->proc_fops = &pktgen_if_fops;
3449 pkt_dev->entry->data = pkt_dev;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003450
3451 return add_dev_to_thread(t, pkt_dev);
Stephen Hemminger39df2322007-03-04 16:11:51 -08003452out2:
3453 dev_put(pkt_dev->odev);
3454out1:
3455 if (pkt_dev->flows)
3456 vfree(pkt_dev->flows);
3457 kfree(pkt_dev);
3458 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459}
3460
David S. Milleree74baa2007-01-01 20:51:53 -08003461static int __init pktgen_create_thread(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003463 struct pktgen_thread *t;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003464 struct proc_dir_entry *pe;
David S. Milleree74baa2007-01-01 20:51:53 -08003465 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466
Luiz Capitulino222f1802006-03-20 22:16:13 -08003467 t = kzalloc(sizeof(struct pktgen_thread), GFP_KERNEL);
3468 if (!t) {
3469 printk("pktgen: ERROR: out of memory, can't create new thread.\n");
3470 return -ENOMEM;
3471 }
3472
Luiz Capitulino222f1802006-03-20 22:16:13 -08003473 spin_lock_init(&t->if_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474 t->cpu = cpu;
Luiz Capitulino222f1802006-03-20 22:16:13 -08003475
David S. Milleree74baa2007-01-01 20:51:53 -08003476 INIT_LIST_HEAD(&t->if_list);
3477
3478 list_add_tail(&t->th_list, &pktgen_threads);
3479
3480 p = kthread_create(pktgen_thread_worker, t, "kpktgend_%d", cpu);
3481 if (IS_ERR(p)) {
3482 printk("pktgen: kernel_thread() failed for cpu %d\n", t->cpu);
3483 list_del(&t->th_list);
3484 kfree(t);
3485 return PTR_ERR(p);
3486 }
3487 kthread_bind(p, cpu);
3488 t->tsk = p;
3489
3490 pe = create_proc_entry(t->tsk->comm, 0600, pg_proc_dir);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003491 if (!pe) {
3492 printk("pktgen: cannot create %s/%s procfs entry.\n",
David S. Milleree74baa2007-01-01 20:51:53 -08003493 PG_PROC_DIR, t->tsk->comm);
3494 kthread_stop(p);
3495 list_del(&t->th_list);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003496 kfree(t);
3497 return -EINVAL;
3498 }
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003499
3500 pe->proc_fops = &pktgen_thread_fops;
3501 pe->data = t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502
David S. Milleree74baa2007-01-01 20:51:53 -08003503 wake_up_process(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504
3505 return 0;
3506}
3507
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09003508/*
3509 * Removes a device from the thread if_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510 */
Luiz Capitulino222f1802006-03-20 22:16:13 -08003511static void _rem_dev_from_if_list(struct pktgen_thread *t,
3512 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513{
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003514 struct list_head *q, *n;
3515 struct pktgen_dev *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516
Luiz Capitulinoc26a8012006-03-20 22:18:16 -08003517 list_for_each_safe(q, n, &t->if_list) {
3518 p = list_entry(q, struct pktgen_dev, list);
3519 if (p == pkt_dev)
3520 list_del(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 }
3522}
3523
Luiz Capitulino222f1802006-03-20 22:16:13 -08003524static int pktgen_remove_device(struct pktgen_thread *t,
3525 struct pktgen_dev *pkt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526{
3527
Stephen Hemminger25c4e532007-03-04 16:06:47 -08003528 pr_debug("pktgen: remove_device pkt_dev=%p\n", pkt_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529
Luiz Capitulino222f1802006-03-20 22:16:13 -08003530 if (pkt_dev->running) {
3531 printk("pktgen:WARNING: trying to remove a running interface, stopping it now.\n");
3532 pktgen_stop_device(pkt_dev);
3533 }
3534
3535 /* Dis-associate from the interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536
3537 if (pkt_dev->odev) {
3538 dev_put(pkt_dev->odev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003539 pkt_dev->odev = NULL;
3540 }
3541
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542 /* And update the thread if_list */
3543
3544 _rem_dev_from_if_list(t, pkt_dev);
3545
Stephen Hemminger39df2322007-03-04 16:11:51 -08003546 if (pkt_dev->entry)
3547 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548
3549 if (pkt_dev->flows)
3550 vfree(pkt_dev->flows);
3551 kfree(pkt_dev);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003552 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553}
3554
Luiz Capitulino222f1802006-03-20 22:16:13 -08003555static int __init pg_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556{
3557 int cpu;
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003558 struct proc_dir_entry *pe;
3559
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 printk(version);
3561
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003562 pg_proc_dir = proc_mkdir(PG_PROC_DIR, proc_net);
3563 if (!pg_proc_dir)
3564 return -ENODEV;
3565 pg_proc_dir->owner = THIS_MODULE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003567 pe = create_proc_entry(PGCTRL, 0600, pg_proc_dir);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003568 if (pe == NULL) {
3569 printk("pktgen: ERROR: cannot create %s procfs entry.\n",
3570 PGCTRL);
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003571 proc_net_remove(PG_PROC_DIR);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003572 return -EINVAL;
3573 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574
Luiz Capitulino222f1802006-03-20 22:16:13 -08003575 pe->proc_fops = &pktgen_fops;
3576 pe->data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577
3578 /* Register us to receive netdevice events */
3579 register_netdevice_notifier(&pktgen_notifier_block);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003580
John Hawkes670c02c2005-10-13 09:30:31 -07003581 for_each_online_cpu(cpu) {
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003582 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583
David S. Milleree74baa2007-01-01 20:51:53 -08003584 err = pktgen_create_thread(cpu);
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003585 if (err)
3586 printk("pktgen: WARNING: Cannot create thread for cpu %d (%d)\n",
3587 cpu, err);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003588 }
Luiz Capitulino8024bb22006-03-20 22:17:55 -08003589
3590 if (list_empty(&pktgen_threads)) {
3591 printk("pktgen: ERROR: Initialization failed for all threads\n");
3592 unregister_netdevice_notifier(&pktgen_notifier_block);
3593 remove_proc_entry(PGCTRL, pg_proc_dir);
3594 proc_net_remove(PG_PROC_DIR);
3595 return -ENODEV;
3596 }
3597
Luiz Capitulino222f1802006-03-20 22:16:13 -08003598 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599}
3600
3601static void __exit pg_cleanup(void)
3602{
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003603 struct pktgen_thread *t;
3604 struct list_head *q, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605 wait_queue_head_t queue;
3606 init_waitqueue_head(&queue);
3607
Luiz Capitulino222f1802006-03-20 22:16:13 -08003608 /* Stop all interfaces & threads */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609
Luiz Capitulinocdcdbe02006-03-20 22:16:40 -08003610 list_for_each_safe(q, n, &pktgen_threads) {
3611 t = list_entry(q, struct pktgen_thread, th_list);
David S. Milleree74baa2007-01-01 20:51:53 -08003612 kthread_stop(t->tsk);
3613 kfree(t);
Luiz Capitulino222f1802006-03-20 22:16:13 -08003614 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615
Luiz Capitulino222f1802006-03-20 22:16:13 -08003616 /* Un-register us from receiving netdevice events */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617 unregister_netdevice_notifier(&pktgen_notifier_block);
3618
Luiz Capitulino222f1802006-03-20 22:16:13 -08003619 /* Clean up proc file system */
Stephen Hemmingerd50a6b562005-10-14 15:42:33 -07003620 remove_proc_entry(PGCTRL, pg_proc_dir);
3621 proc_net_remove(PG_PROC_DIR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622}
3623
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624module_init(pg_init);
3625module_exit(pg_cleanup);
3626
3627MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se");
3628MODULE_DESCRIPTION("Packet Generator tool");
3629MODULE_LICENSE("GPL");
3630module_param(pg_count_d, int, 0);
3631module_param(pg_delay_d, int, 0);
3632module_param(pg_clone_skb_d, int, 0);
3633module_param(debug, int, 0);