3 * Copyright 2001, 2002 by Robert Olsson <robert.olsson@its.uu.se>
4 * Uppsala University and
5 * Swedish University of Agricultural Sciences
7 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
8 * Ben Greear <greearb@candelatech.com>
9 * Jens Låås <jens.laas@data.slu.se>
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.
17 * A tool for loading the network with preconfigurated packets.
18 * The tool is implemented as a linux module. Parameters are output
19 * device, delay (to hard_xmit), number of packets, and whether
20 * to use multiple SKBs or just the same one.
21 * pktgen uses the installed interface's output routine.
23 * Additional hacking by:
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>
47 * Renamed multiskb to clone_skb and cleaned up sending core for two distinct
48 * skb modes. A clone_skb=0 mode for Ben "ranges" work and a clone_skb != 0
49 * as a "fastpath" with a configurable number of clones after alloc's.
50 * clone_skb=0 means all packets are allocated this also means ranges time
51 * stamps etc can be used. clone_skb=100 means 1 malloc is followed by 100
54 * Also moved to /proc/net/pktgen/
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>
61 * Integrated to 2.5.x 021029 --Lucio Maciel (luciomaciel@zipmail.com.br)
64 * 021124 Finished major redesign and rewrite for new functionality.
65 * See Documentation/networking/pktgen.txt for how to use this.
68 * For each CPU one thread/process is created at start. This process checks
69 * for running devices in the if_list and sends packets until count is 0 it
70 * also the thread checks the thread->control which is used for inter-process
71 * communication. controlling process "posts" operations to the threads this
72 * way. The if_lock should be possible to remove when add/rem_device is merged
75 * By design there should only be *one* "controlling" process. In practice
76 * multiple write accesses gives unpredictable result. Understood by "write"
77 * to /proc gives result code thats should be read be the "writer".
78 * For pratical use this should be no problem.
80 * Note when adding devices to a specific CPU there good idea to also assign
81 * /proc/irq/XX/smp_affinity so TX-interrupts gets bound to the same CPU.
84 * Fix refcount off by one if first packet fails, potential null deref,
87 * First "ranges" functionality for ipv6 030726 --ro
89 * Included flow support. 030802 ANK.
91 * Fixed unaligned access on IA-64 Grant Grundler <grundler@parisc-linux.org>
93 * Remove if fix from added Harald Welte <laforge@netfilter.org> 040419
94 * ia64 compilation fix from Aron Griffis <aron@hp.com> 040604
96 * New xmit() return, do_div and misc clean up by Stephen Hemminger
97 * <shemminger@osdl.org> 040923
99 * Rany Dunlap fixed u64 printk compiler waring
101 * Remove FCS from BW calculation. Lennert Buytenhek <buytenh@wantstofly.org>
102 * New time handling. Lennert Buytenhek <buytenh@wantstofly.org> 041213
104 * Corrections from Nikolai Malykh (nmalykh@bilim.com)
105 * Removed unused flags F_SET_SRCMAC & F_SET_SRCIP 041230
107 * interruptible_sleep_on_timeout() replaced Nishanth Aravamudan <nacc@us.ibm.com>
110 #include <linux/sys.h>
111 #include <linux/types.h>
112 #include <linux/module.h>
113 #include <linux/moduleparam.h>
114 #include <linux/kernel.h>
115 #include <linux/smp_lock.h>
116 #include <linux/sched.h>
117 #include <linux/slab.h>
118 #include <linux/vmalloc.h>
119 #include <linux/sched.h>
120 #include <linux/unistd.h>
121 #include <linux/string.h>
122 #include <linux/ptrace.h>
123 #include <linux/errno.h>
124 #include <linux/ioport.h>
125 #include <linux/interrupt.h>
126 #include <linux/delay.h>
127 #include <linux/timer.h>
128 #include <linux/init.h>
129 #include <linux/skbuff.h>
130 #include <linux/netdevice.h>
131 #include <linux/inet.h>
132 #include <linux/inetdevice.h>
133 #include <linux/rtnetlink.h>
134 #include <linux/if_arp.h>
135 #include <linux/in.h>
136 #include <linux/ip.h>
137 #include <linux/ipv6.h>
138 #include <linux/udp.h>
139 #include <linux/proc_fs.h>
140 #include <linux/wait.h>
141 #include <net/checksum.h>
142 #include <net/ipv6.h>
143 #include <net/addrconf.h>
144 #include <asm/byteorder.h>
145 #include <linux/rcupdate.h>
146 #include <asm/bitops.h>
149 #include <asm/uaccess.h>
150 #include <asm/div64.h> /* do_div */
151 #include <asm/timex.h>
154 #define VERSION "pktgen v2.62: Packet Generator for packet performance testing.\n"
156 /* #define PG_DEBUG(a) a */
159 /* The buckets are exponential in 'width' */
160 #define LAT_BUCKETS_MAX 32
161 #define IP_NAME_SZ 32
163 /* Device flag bits */
164 #define F_IPSRC_RND (1<<0) /* IP-Src Random */
165 #define F_IPDST_RND (1<<1) /* IP-Dst Random */
166 #define F_UDPSRC_RND (1<<2) /* UDP-Src Random */
167 #define F_UDPDST_RND (1<<3) /* UDP-Dst Random */
168 #define F_MACSRC_RND (1<<4) /* MAC-Src Random */
169 #define F_MACDST_RND (1<<5) /* MAC-Dst Random */
170 #define F_TXSIZE_RND (1<<6) /* Transmit size is random */
171 #define F_IPV6 (1<<7) /* Interface in IPV6 Mode */
173 /* Thread control flag bits */
174 #define T_TERMINATE (1<<0)
175 #define T_STOP (1<<1) /* Stop run */
176 #define T_RUN (1<<2) /* Start run */
177 #define T_REMDEV (1<<3) /* Remove all devs */
180 #define thread_lock() spin_lock(&_thread_lock)
181 #define thread_unlock() spin_unlock(&_thread_lock)
183 /* If lock -- can be removed after some work */
184 #define if_lock(t) spin_lock(&(t->if_lock));
185 #define if_unlock(t) spin_unlock(&(t->if_lock));
187 /* Used to help with determining the pkts on receive */
188 #define PKTGEN_MAGIC 0xbe9be955
189 #define PG_PROC_DIR "pktgen"
191 #define MAX_CFLOWS 65536
202 * Try to keep frequent/infrequent used vars. separated.
206 struct proc_dir_entry
*proc_ent
;
208 /* proc file names */
211 struct pktgen_thread
* pg_thread
; /* the owner */
212 struct pktgen_dev
*next
; /* Used for chaining in the thread's run-queue */
214 int running
; /* if this changes to false, the test will stop */
216 /* If min != max, then we will either do a linear iteration, or
217 * we will do a random selection from within the range.
221 int min_pkt_size
; /* = ETH_ZLEN; */
222 int max_pkt_size
; /* = ETH_ZLEN; */
224 __u32 delay_us
; /* Default delay */
226 __u64 count
; /* Default No packets to send */
227 __u64 sofar
; /* How many pkts we've sent so far */
228 __u64 tx_bytes
; /* How many bytes we've transmitted */
229 __u64 errors
; /* Errors when trying to transmit, pkts will be re-sent */
231 /* runtime counters relating to clone_skb */
232 __u64 next_tx_us
; /* timestamp of when to tx next */
235 __u64 allocated_skbs
;
237 int last_ok
; /* Was last skb sent?
238 * Or a failed transmit of some sort? This will keep
239 * sequence numbers in order, for example.
241 __u64 started_at
; /* micro-seconds */
242 __u64 stopped_at
; /* micro-seconds */
243 __u64 idle_acc
; /* micro-seconds */
246 int clone_skb
; /* Use multiple SKBs during packet gen. If this number
247 * is greater than 1, then that many coppies of the same
248 * packet will be sent before a new packet is allocated.
249 * For instance, if you want to send 1024 identical packets
250 * before creating a new packet, set clone_skb to 1024.
253 char dst_min
[IP_NAME_SZ
]; /* IP, ie 1.2.3.4 */
254 char dst_max
[IP_NAME_SZ
]; /* IP, ie 1.2.3.4 */
255 char src_min
[IP_NAME_SZ
]; /* IP, ie 1.2.3.4 */
256 char src_max
[IP_NAME_SZ
]; /* IP, ie 1.2.3.4 */
258 struct in6_addr in6_saddr
;
259 struct in6_addr in6_daddr
;
260 struct in6_addr cur_in6_daddr
;
261 struct in6_addr cur_in6_saddr
;
263 struct in6_addr min_in6_daddr
;
264 struct in6_addr max_in6_daddr
;
265 struct in6_addr min_in6_saddr
;
266 struct in6_addr max_in6_saddr
;
268 /* If we're doing ranges, random or incremental, then this
269 * defines the min/max for those ranges.
271 __u32 saddr_min
; /* inclusive, source IP address */
272 __u32 saddr_max
; /* exclusive, source IP address */
273 __u32 daddr_min
; /* inclusive, dest IP address */
274 __u32 daddr_max
; /* exclusive, dest IP address */
276 __u16 udp_src_min
; /* inclusive, source UDP port */
277 __u16 udp_src_max
; /* exclusive, source UDP port */
278 __u16 udp_dst_min
; /* inclusive, dest UDP port */
279 __u16 udp_dst_max
; /* exclusive, dest UDP port */
281 __u32 src_mac_count
; /* How many MACs to iterate through */
282 __u32 dst_mac_count
; /* How many MACs to iterate through */
284 unsigned char dst_mac
[6];
285 unsigned char src_mac
[6];
287 __u32 cur_dst_mac_offset
;
288 __u32 cur_src_mac_offset
;
297 0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
299 We fill in SRC address later
300 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
304 __u16 pad
; /* pad out the hh struct to an even 16 bytes */
306 struct sk_buff
* skb
; /* skb we are to transmit next, mainly used for when we
307 * are transmitting the same one multiple times
309 struct net_device
* odev
; /* The out-going device. Note that the device should
310 * have it's pg_info pointer pointing back to this
311 * device. This will be set when the user specifies
312 * the out-going device name (not when the inject is
313 * started as it used to do.)
315 struct flow_state
*flows
;
316 unsigned cflows
; /* Concurrent flows (config) */
317 unsigned lflow
; /* Flow length (config) */
318 unsigned nflows
; /* accumulated flows (stats) */
328 struct pktgen_thread
{
330 struct pktgen_dev
*if_list
; /* All device here */
331 struct pktgen_thread
* next
;
333 char fname
[128]; /* name of proc file */
334 struct proc_dir_entry
*proc_ent
;
336 u32 max_before_softirq
; /* We'll call do_softirq to prevent starvation. */
338 /* Field for thread to receive "posted" events terminate, stop ifs etc.*/
344 wait_queue_head_t queue
;
350 /* This code works around the fact that do_div cannot handle two 64-bit
351 numbers, and regular 64-bit division doesn't work on x86 kernels.
357 /* This was emailed to LMKL by: Chris Caputo <ccaputo@alt.net>
358 * Function copied/adapted/optimized from:
360 * nemesis.sourceforge.net/browse/lib/static/intmath/ix86/intmath.c.html
362 * Copyright 1994, University of Cambridge Computer Laboratory
363 * All Rights Reserved.
366 static inline s64
divremdi3(s64 x
, s64 y
, int type
)
368 u64 a
= (x
< 0) ? -x
: x
;
369 u64 b
= (y
< 0) ? -y
: y
;
389 if (PG_DIV
== type
) {
390 return (((x
^ y
) & (1ll<<63)) == 0) ? res
: -(s64
)res
;
393 return ((x
& (1ll<<63)) == 0) ? a
: -(s64
)a
;
397 /* End of hacks to deal with 64-bit math on x86 */
399 /** Convert to miliseconds */
400 static inline __u64
tv_to_ms(const struct timeval
* tv
)
402 __u64 ms
= tv
->tv_usec
/ 1000;
403 ms
+= (__u64
)tv
->tv_sec
* (__u64
)1000;
408 /** Convert to micro-seconds */
409 static inline __u64
tv_to_us(const struct timeval
* tv
)
411 __u64 us
= tv
->tv_usec
;
412 us
+= (__u64
)tv
->tv_sec
* (__u64
)1000000;
416 static inline __u64
pg_div(__u64 n
, __u32 base
) {
419 /* printk("pktgen: pg_div, n: %llu base: %d rv: %llu\n",
424 static inline __u64
pg_div64(__u64 n
, __u64 base
)
428 * How do we know if the architectrure we are running on
429 * supports division with 64 bit base?
432 #if defined(__sparc_v9__) || defined(__powerpc64__) || defined(__alpha__) || defined(__x86_64__) || defined(__ia64__)
436 tmp
= divremdi3(n
, base
, PG_DIV
);
441 static inline u32
pktgen_random(void)
445 get_random_bytes(&n
, 4);
452 static inline __u64
getCurMs(void)
455 do_gettimeofday(&tv
);
456 return tv_to_ms(&tv
);
459 static inline __u64
getCurUs(void)
462 do_gettimeofday(&tv
);
463 return tv_to_us(&tv
);
466 static inline __u64
tv_diff(const struct timeval
* a
, const struct timeval
* b
)
468 return tv_to_us(a
) - tv_to_us(b
);
472 /* old include end */
474 static char version
[] __initdata
= VERSION
;
476 static ssize_t
proc_pgctrl_read(struct file
* file
, char __user
* buf
, size_t count
, loff_t
*ppos
);
477 static ssize_t
proc_pgctrl_write(struct file
* file
, const char __user
* buf
, size_t count
, loff_t
*ppos
);
478 static int proc_if_read(char *buf
, char **start
, off_t offset
, int len
, int *eof
, void *data
);
480 static int proc_thread_read(char *buf
, char **start
, off_t offset
, int len
, int *eof
, void *data
);
481 static int proc_if_write(struct file
*file
, const char __user
*user_buffer
, unsigned long count
, void *data
);
482 static int proc_thread_write(struct file
*file
, const char __user
*user_buffer
, unsigned long count
, void *data
);
483 static int create_proc_dir(void);
484 static int remove_proc_dir(void);
486 static int pktgen_remove_device(struct pktgen_thread
* t
, struct pktgen_dev
*i
);
487 static int pktgen_add_device(struct pktgen_thread
* t
, const char* ifname
);
488 static struct pktgen_thread
* pktgen_find_thread(const char* name
);
489 static struct pktgen_dev
*pktgen_find_dev(struct pktgen_thread
* t
, const char* ifname
);
490 static int pktgen_device_event(struct notifier_block
*, unsigned long, void *);
491 static void pktgen_run_all_threads(void);
492 static void pktgen_stop_all_threads_ifs(void);
493 static int pktgen_stop_device(struct pktgen_dev
*pkt_dev
);
494 static void pktgen_stop(struct pktgen_thread
* t
);
495 static void pktgen_clear_counters(struct pktgen_dev
*pkt_dev
);
496 static struct pktgen_dev
*pktgen_NN_threads(const char* dev_name
, int remove
);
497 static unsigned int scan_ip6(const char *s
,char ip
[16]);
498 static unsigned int fmt_ip6(char *s
,const char ip
[16]);
500 /* Module parameters, defaults. */
501 static int pg_count_d
= 1000; /* 1000 pkts by default */
502 static int pg_delay_d
= 0;
503 static int pg_clone_skb_d
= 0;
504 static int debug
= 0;
506 static DEFINE_SPINLOCK(_thread_lock
);
507 static struct pktgen_thread
*pktgen_threads
= NULL
;
509 static char module_fname
[128];
510 static struct proc_dir_entry
*module_proc_ent
= NULL
;
512 static struct notifier_block pktgen_notifier_block
= {
513 .notifier_call
= pktgen_device_event
,
516 static struct file_operations pktgen_fops
= {
517 .read
= proc_pgctrl_read
,
518 .write
= proc_pgctrl_write
,
519 /* .ioctl = pktgen_ioctl, later maybe */
523 * /proc handling functions
527 static struct proc_dir_entry
*pg_proc_dir
= NULL
;
528 static int proc_pgctrl_read_eof
=0;
530 static ssize_t
proc_pgctrl_read(struct file
* file
, char __user
* buf
,
531 size_t count
, loff_t
*ppos
)
536 if(proc_pgctrl_read_eof
) {
537 proc_pgctrl_read_eof
=0;
542 sprintf(data
, "%s", VERSION
);
551 if (copy_to_user(buf
, data
, len
)) {
557 proc_pgctrl_read_eof
=1; /* EOF next call */
563 static ssize_t
proc_pgctrl_write(struct file
* file
,const char __user
* buf
,
564 size_t count
, loff_t
*ppos
)
569 if (!capable(CAP_NET_ADMIN
)){
574 data
= (void*)vmalloc ((unsigned int)count
);
580 if (copy_from_user(data
, buf
, count
)) {
584 data
[count
-1] = 0; /* Make string */
586 if (!strcmp(data
, "stop"))
587 pktgen_stop_all_threads_ifs();
589 else if (!strcmp(data
, "start"))
590 pktgen_run_all_threads();
593 printk("pktgen: Unknown command: %s\n", data
);
603 static int proc_if_read(char *buf
, char **start
, off_t offset
,
604 int len
, int *eof
, void *data
)
608 struct pktgen_dev
*pkt_dev
= (struct pktgen_dev
*)(data
);
611 __u64 now
= getCurUs();
614 p
+= sprintf(p
, "Params: count %llu min_pkt_size: %u max_pkt_size: %u\n",
615 (unsigned long long) pkt_dev
->count
,
616 pkt_dev
->min_pkt_size
, pkt_dev
->max_pkt_size
);
618 p
+= sprintf(p
, " frags: %d delay: %u clone_skb: %d ifname: %s\n",
619 pkt_dev
->nfrags
, 1000*pkt_dev
->delay_us
+pkt_dev
->delay_ns
, pkt_dev
->clone_skb
, pkt_dev
->ifname
);
621 p
+= sprintf(p
, " flows: %u flowlen: %u\n", pkt_dev
->cflows
, pkt_dev
->lflow
);
624 if(pkt_dev
->flags
& F_IPV6
) {
625 char b1
[128], b2
[128], b3
[128];
626 fmt_ip6(b1
, pkt_dev
->in6_saddr
.s6_addr
);
627 fmt_ip6(b2
, pkt_dev
->min_in6_saddr
.s6_addr
);
628 fmt_ip6(b3
, pkt_dev
->max_in6_saddr
.s6_addr
);
629 p
+= sprintf(p
, " saddr: %s min_saddr: %s max_saddr: %s\n", b1
, b2
, b3
);
631 fmt_ip6(b1
, pkt_dev
->in6_daddr
.s6_addr
);
632 fmt_ip6(b2
, pkt_dev
->min_in6_daddr
.s6_addr
);
633 fmt_ip6(b3
, pkt_dev
->max_in6_daddr
.s6_addr
);
634 p
+= sprintf(p
, " daddr: %s min_daddr: %s max_daddr: %s\n", b1
, b2
, b3
);
638 p
+= sprintf(p
, " dst_min: %s dst_max: %s\n src_min: %s src_max: %s\n",
639 pkt_dev
->dst_min
, pkt_dev
->dst_max
, pkt_dev
->src_min
, pkt_dev
->src_max
);
641 p
+= sprintf(p
, " src_mac: ");
643 if ((pkt_dev
->src_mac
[0] == 0) &&
644 (pkt_dev
->src_mac
[1] == 0) &&
645 (pkt_dev
->src_mac
[2] == 0) &&
646 (pkt_dev
->src_mac
[3] == 0) &&
647 (pkt_dev
->src_mac
[4] == 0) &&
648 (pkt_dev
->src_mac
[5] == 0))
650 for (i
= 0; i
< 6; i
++)
651 p
+= sprintf(p
, "%02X%s", pkt_dev
->odev
->dev_addr
[i
], i
== 5 ? " " : ":");
654 for (i
= 0; i
< 6; i
++)
655 p
+= sprintf(p
, "%02X%s", pkt_dev
->src_mac
[i
], i
== 5 ? " " : ":");
657 p
+= sprintf(p
, "dst_mac: ");
658 for (i
= 0; i
< 6; i
++)
659 p
+= sprintf(p
, "%02X%s", pkt_dev
->dst_mac
[i
], i
== 5 ? "\n" : ":");
661 p
+= sprintf(p
, " udp_src_min: %d udp_src_max: %d udp_dst_min: %d udp_dst_max: %d\n",
662 pkt_dev
->udp_src_min
, pkt_dev
->udp_src_max
, pkt_dev
->udp_dst_min
,
663 pkt_dev
->udp_dst_max
);
665 p
+= sprintf(p
, " src_mac_count: %d dst_mac_count: %d \n Flags: ",
666 pkt_dev
->src_mac_count
, pkt_dev
->dst_mac_count
);
669 if (pkt_dev
->flags
& F_IPV6
)
670 p
+= sprintf(p
, "IPV6 ");
672 if (pkt_dev
->flags
& F_IPSRC_RND
)
673 p
+= sprintf(p
, "IPSRC_RND ");
675 if (pkt_dev
->flags
& F_IPDST_RND
)
676 p
+= sprintf(p
, "IPDST_RND ");
678 if (pkt_dev
->flags
& F_TXSIZE_RND
)
679 p
+= sprintf(p
, "TXSIZE_RND ");
681 if (pkt_dev
->flags
& F_UDPSRC_RND
)
682 p
+= sprintf(p
, "UDPSRC_RND ");
684 if (pkt_dev
->flags
& F_UDPDST_RND
)
685 p
+= sprintf(p
, "UDPDST_RND ");
687 if (pkt_dev
->flags
& F_MACSRC_RND
)
688 p
+= sprintf(p
, "MACSRC_RND ");
690 if (pkt_dev
->flags
& F_MACDST_RND
)
691 p
+= sprintf(p
, "MACDST_RND ");
694 p
+= sprintf(p
, "\n");
696 sa
= pkt_dev
->started_at
;
697 stopped
= pkt_dev
->stopped_at
;
698 if (pkt_dev
->running
)
699 stopped
= now
; /* not really stopped, more like last-running-at */
701 p
+= sprintf(p
, "Current:\n pkts-sofar: %llu errors: %llu\n started: %lluus stopped: %lluus idle: %lluus\n",
702 (unsigned long long) pkt_dev
->sofar
,
703 (unsigned long long) pkt_dev
->errors
,
704 (unsigned long long) sa
,
705 (unsigned long long) stopped
,
706 (unsigned long long) pkt_dev
->idle_acc
);
708 p
+= sprintf(p
, " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n",
709 pkt_dev
->seq_num
, pkt_dev
->cur_dst_mac_offset
, pkt_dev
->cur_src_mac_offset
);
711 if(pkt_dev
->flags
& F_IPV6
) {
712 char b1
[128], b2
[128];
713 fmt_ip6(b1
, pkt_dev
->cur_in6_daddr
.s6_addr
);
714 fmt_ip6(b2
, pkt_dev
->cur_in6_saddr
.s6_addr
);
715 p
+= sprintf(p
, " cur_saddr: %s cur_daddr: %s\n", b2
, b1
);
718 p
+= sprintf(p
, " cur_saddr: 0x%x cur_daddr: 0x%x\n",
719 pkt_dev
->cur_saddr
, pkt_dev
->cur_daddr
);
722 p
+= sprintf(p
, " cur_udp_dst: %d cur_udp_src: %d\n",
723 pkt_dev
->cur_udp_dst
, pkt_dev
->cur_udp_src
);
725 p
+= sprintf(p
, " flows: %u\n", pkt_dev
->nflows
);
727 if (pkt_dev
->result
[0])
728 p
+= sprintf(p
, "Result: %s\n", pkt_dev
->result
);
730 p
+= sprintf(p
, "Result: Idle\n");
737 static int count_trail_chars(const char __user
*user_buffer
, unsigned int maxlen
)
741 for (i
= 0; i
< maxlen
; i
++) {
743 if (get_user(c
, &user_buffer
[i
]))
761 static unsigned long num_arg(const char __user
*user_buffer
, unsigned long maxlen
,
767 for(; i
< maxlen
; i
++) {
769 if (get_user(c
, &user_buffer
[i
]))
771 if ((c
>= '0') && (c
<= '9')) {
780 static int strn_len(const char __user
*user_buffer
, unsigned int maxlen
)
784 for(; i
< maxlen
; i
++) {
786 if (get_user(c
, &user_buffer
[i
]))
805 static int proc_if_write(struct file
*file
, const char __user
*user_buffer
,
806 unsigned long count
, void *data
)
809 char name
[16], valstr
[32];
810 unsigned long value
= 0;
811 struct pktgen_dev
*pkt_dev
= (struct pktgen_dev
*)(data
);
812 char* pg_result
= NULL
;
816 pg_result
= &(pkt_dev
->result
[0]);
819 printk("pktgen: wrong command format\n");
824 tmp
= count_trail_chars(&user_buffer
[i
], max
);
826 printk("pktgen: illegal format\n");
831 /* Read variable name */
833 len
= strn_len(&user_buffer
[i
], sizeof(name
) - 1);
834 if (len
< 0) { return len
; }
835 memset(name
, 0, sizeof(name
));
836 if (copy_from_user(name
, &user_buffer
[i
], len
) )
841 len
= count_trail_chars(&user_buffer
[i
], max
);
849 if (copy_from_user(tb
, user_buffer
, count
))
852 printk("pktgen: %s,%lu buffer -:%s:-\n", name
, count
, tb
);
855 if (!strcmp(name
, "min_pkt_size")) {
856 len
= num_arg(&user_buffer
[i
], 10, &value
);
857 if (len
< 0) { return len
; }
861 if (value
!= pkt_dev
->min_pkt_size
) {
862 pkt_dev
->min_pkt_size
= value
;
863 pkt_dev
->cur_pkt_size
= value
;
865 sprintf(pg_result
, "OK: min_pkt_size=%u", pkt_dev
->min_pkt_size
);
869 if (!strcmp(name
, "max_pkt_size")) {
870 len
= num_arg(&user_buffer
[i
], 10, &value
);
871 if (len
< 0) { return len
; }
875 if (value
!= pkt_dev
->max_pkt_size
) {
876 pkt_dev
->max_pkt_size
= value
;
877 pkt_dev
->cur_pkt_size
= value
;
879 sprintf(pg_result
, "OK: max_pkt_size=%u", pkt_dev
->max_pkt_size
);
883 /* Shortcut for min = max */
885 if (!strcmp(name
, "pkt_size")) {
886 len
= num_arg(&user_buffer
[i
], 10, &value
);
887 if (len
< 0) { return len
; }
891 if (value
!= pkt_dev
->min_pkt_size
) {
892 pkt_dev
->min_pkt_size
= value
;
893 pkt_dev
->max_pkt_size
= value
;
894 pkt_dev
->cur_pkt_size
= value
;
896 sprintf(pg_result
, "OK: pkt_size=%u", pkt_dev
->min_pkt_size
);
900 if (!strcmp(name
, "debug")) {
901 len
= num_arg(&user_buffer
[i
], 10, &value
);
902 if (len
< 0) { return len
; }
905 sprintf(pg_result
, "OK: debug=%u", debug
);
909 if (!strcmp(name
, "frags")) {
910 len
= num_arg(&user_buffer
[i
], 10, &value
);
911 if (len
< 0) { return len
; }
913 pkt_dev
->nfrags
= value
;
914 sprintf(pg_result
, "OK: frags=%u", pkt_dev
->nfrags
);
917 if (!strcmp(name
, "delay")) {
918 len
= num_arg(&user_buffer
[i
], 10, &value
);
919 if (len
< 0) { return len
; }
921 if (value
== 0x7FFFFFFF) {
922 pkt_dev
->delay_us
= 0x7FFFFFFF;
923 pkt_dev
->delay_ns
= 0;
925 pkt_dev
->delay_us
= value
/ 1000;
926 pkt_dev
->delay_ns
= value
% 1000;
928 sprintf(pg_result
, "OK: delay=%u", 1000*pkt_dev
->delay_us
+pkt_dev
->delay_ns
);
931 if (!strcmp(name
, "udp_src_min")) {
932 len
= num_arg(&user_buffer
[i
], 10, &value
);
933 if (len
< 0) { return len
; }
935 if (value
!= pkt_dev
->udp_src_min
) {
936 pkt_dev
->udp_src_min
= value
;
937 pkt_dev
->cur_udp_src
= value
;
939 sprintf(pg_result
, "OK: udp_src_min=%u", pkt_dev
->udp_src_min
);
942 if (!strcmp(name
, "udp_dst_min")) {
943 len
= num_arg(&user_buffer
[i
], 10, &value
);
944 if (len
< 0) { return len
; }
946 if (value
!= pkt_dev
->udp_dst_min
) {
947 pkt_dev
->udp_dst_min
= value
;
948 pkt_dev
->cur_udp_dst
= value
;
950 sprintf(pg_result
, "OK: udp_dst_min=%u", pkt_dev
->udp_dst_min
);
953 if (!strcmp(name
, "udp_src_max")) {
954 len
= num_arg(&user_buffer
[i
], 10, &value
);
955 if (len
< 0) { return len
; }
957 if (value
!= pkt_dev
->udp_src_max
) {
958 pkt_dev
->udp_src_max
= value
;
959 pkt_dev
->cur_udp_src
= value
;
961 sprintf(pg_result
, "OK: udp_src_max=%u", pkt_dev
->udp_src_max
);
964 if (!strcmp(name
, "udp_dst_max")) {
965 len
= num_arg(&user_buffer
[i
], 10, &value
);
966 if (len
< 0) { return len
; }
968 if (value
!= pkt_dev
->udp_dst_max
) {
969 pkt_dev
->udp_dst_max
= value
;
970 pkt_dev
->cur_udp_dst
= value
;
972 sprintf(pg_result
, "OK: udp_dst_max=%u", pkt_dev
->udp_dst_max
);
975 if (!strcmp(name
, "clone_skb")) {
976 len
= num_arg(&user_buffer
[i
], 10, &value
);
977 if (len
< 0) { return len
; }
979 pkt_dev
->clone_skb
= value
;
981 sprintf(pg_result
, "OK: clone_skb=%d", pkt_dev
->clone_skb
);
984 if (!strcmp(name
, "count")) {
985 len
= num_arg(&user_buffer
[i
], 10, &value
);
986 if (len
< 0) { return len
; }
988 pkt_dev
->count
= value
;
989 sprintf(pg_result
, "OK: count=%llu",
990 (unsigned long long) pkt_dev
->count
);
993 if (!strcmp(name
, "src_mac_count")) {
994 len
= num_arg(&user_buffer
[i
], 10, &value
);
995 if (len
< 0) { return len
; }
997 if (pkt_dev
->src_mac_count
!= value
) {
998 pkt_dev
->src_mac_count
= value
;
999 pkt_dev
->cur_src_mac_offset
= 0;
1001 sprintf(pg_result
, "OK: src_mac_count=%d", pkt_dev
->src_mac_count
);
1004 if (!strcmp(name
, "dst_mac_count")) {
1005 len
= num_arg(&user_buffer
[i
], 10, &value
);
1006 if (len
< 0) { return len
; }
1008 if (pkt_dev
->dst_mac_count
!= value
) {
1009 pkt_dev
->dst_mac_count
= value
;
1010 pkt_dev
->cur_dst_mac_offset
= 0;
1012 sprintf(pg_result
, "OK: dst_mac_count=%d", pkt_dev
->dst_mac_count
);
1015 if (!strcmp(name
, "flag")) {
1018 len
= strn_len(&user_buffer
[i
], sizeof(f
) - 1);
1019 if (len
< 0) { return len
; }
1020 if (copy_from_user(f
, &user_buffer
[i
], len
))
1023 if (strcmp(f
, "IPSRC_RND") == 0)
1024 pkt_dev
->flags
|= F_IPSRC_RND
;
1026 else if (strcmp(f
, "!IPSRC_RND") == 0)
1027 pkt_dev
->flags
&= ~F_IPSRC_RND
;
1029 else if (strcmp(f
, "TXSIZE_RND") == 0)
1030 pkt_dev
->flags
|= F_TXSIZE_RND
;
1032 else if (strcmp(f
, "!TXSIZE_RND") == 0)
1033 pkt_dev
->flags
&= ~F_TXSIZE_RND
;
1035 else if (strcmp(f
, "IPDST_RND") == 0)
1036 pkt_dev
->flags
|= F_IPDST_RND
;
1038 else if (strcmp(f
, "!IPDST_RND") == 0)
1039 pkt_dev
->flags
&= ~F_IPDST_RND
;
1041 else if (strcmp(f
, "UDPSRC_RND") == 0)
1042 pkt_dev
->flags
|= F_UDPSRC_RND
;
1044 else if (strcmp(f
, "!UDPSRC_RND") == 0)
1045 pkt_dev
->flags
&= ~F_UDPSRC_RND
;
1047 else if (strcmp(f
, "UDPDST_RND") == 0)
1048 pkt_dev
->flags
|= F_UDPDST_RND
;
1050 else if (strcmp(f
, "!UDPDST_RND") == 0)
1051 pkt_dev
->flags
&= ~F_UDPDST_RND
;
1053 else if (strcmp(f
, "MACSRC_RND") == 0)
1054 pkt_dev
->flags
|= F_MACSRC_RND
;
1056 else if (strcmp(f
, "!MACSRC_RND") == 0)
1057 pkt_dev
->flags
&= ~F_MACSRC_RND
;
1059 else if (strcmp(f
, "MACDST_RND") == 0)
1060 pkt_dev
->flags
|= F_MACDST_RND
;
1062 else if (strcmp(f
, "!MACDST_RND") == 0)
1063 pkt_dev
->flags
&= ~F_MACDST_RND
;
1066 sprintf(pg_result
, "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
1068 "IPSRC_RND, IPDST_RND, TXSIZE_RND, UDPSRC_RND, UDPDST_RND, MACSRC_RND, MACDST_RND\n");
1071 sprintf(pg_result
, "OK: flags=0x%x", pkt_dev
->flags
);
1074 if (!strcmp(name
, "dst_min") || !strcmp(name
, "dst")) {
1075 len
= strn_len(&user_buffer
[i
], sizeof(pkt_dev
->dst_min
) - 1);
1076 if (len
< 0) { return len
; }
1078 if (copy_from_user(buf
, &user_buffer
[i
], len
))
1081 if (strcmp(buf
, pkt_dev
->dst_min
) != 0) {
1082 memset(pkt_dev
->dst_min
, 0, sizeof(pkt_dev
->dst_min
));
1083 strncpy(pkt_dev
->dst_min
, buf
, len
);
1084 pkt_dev
->daddr_min
= in_aton(pkt_dev
->dst_min
);
1085 pkt_dev
->cur_daddr
= pkt_dev
->daddr_min
;
1088 printk("pktgen: dst_min set to: %s\n", pkt_dev
->dst_min
);
1090 sprintf(pg_result
, "OK: dst_min=%s", pkt_dev
->dst_min
);
1093 if (!strcmp(name
, "dst_max")) {
1094 len
= strn_len(&user_buffer
[i
], sizeof(pkt_dev
->dst_max
) - 1);
1095 if (len
< 0) { return len
; }
1097 if (copy_from_user(buf
, &user_buffer
[i
], len
))
1101 if (strcmp(buf
, pkt_dev
->dst_max
) != 0) {
1102 memset(pkt_dev
->dst_max
, 0, sizeof(pkt_dev
->dst_max
));
1103 strncpy(pkt_dev
->dst_max
, buf
, len
);
1104 pkt_dev
->daddr_max
= in_aton(pkt_dev
->dst_max
);
1105 pkt_dev
->cur_daddr
= pkt_dev
->daddr_max
;
1108 printk("pktgen: dst_max set to: %s\n", pkt_dev
->dst_max
);
1110 sprintf(pg_result
, "OK: dst_max=%s", pkt_dev
->dst_max
);
1113 if (!strcmp(name
, "dst6")) {
1114 len
= strn_len(&user_buffer
[i
], sizeof(buf
) - 1);
1115 if (len
< 0) return len
;
1117 pkt_dev
->flags
|= F_IPV6
;
1119 if (copy_from_user(buf
, &user_buffer
[i
], len
))
1123 scan_ip6(buf
, pkt_dev
->in6_daddr
.s6_addr
);
1124 fmt_ip6(buf
, pkt_dev
->in6_daddr
.s6_addr
);
1126 ipv6_addr_copy(&pkt_dev
->cur_in6_daddr
, &pkt_dev
->in6_daddr
);
1129 printk("pktgen: dst6 set to: %s\n", buf
);
1132 sprintf(pg_result
, "OK: dst6=%s", buf
);
1135 if (!strcmp(name
, "dst6_min")) {
1136 len
= strn_len(&user_buffer
[i
], sizeof(buf
) - 1);
1137 if (len
< 0) return len
;
1139 pkt_dev
->flags
|= F_IPV6
;
1141 if (copy_from_user(buf
, &user_buffer
[i
], len
))
1145 scan_ip6(buf
, pkt_dev
->min_in6_daddr
.s6_addr
);
1146 fmt_ip6(buf
, pkt_dev
->min_in6_daddr
.s6_addr
);
1148 ipv6_addr_copy(&pkt_dev
->cur_in6_daddr
, &pkt_dev
->min_in6_daddr
);
1150 printk("pktgen: dst6_min set to: %s\n", buf
);
1153 sprintf(pg_result
, "OK: dst6_min=%s", buf
);
1156 if (!strcmp(name
, "dst6_max")) {
1157 len
= strn_len(&user_buffer
[i
], sizeof(buf
) - 1);
1158 if (len
< 0) return len
;
1160 pkt_dev
->flags
|= F_IPV6
;
1162 if (copy_from_user(buf
, &user_buffer
[i
], len
))
1166 scan_ip6(buf
, pkt_dev
->max_in6_daddr
.s6_addr
);
1167 fmt_ip6(buf
, pkt_dev
->max_in6_daddr
.s6_addr
);
1170 printk("pktgen: dst6_max set to: %s\n", buf
);
1173 sprintf(pg_result
, "OK: dst6_max=%s", buf
);
1176 if (!strcmp(name
, "src6")) {
1177 len
= strn_len(&user_buffer
[i
], sizeof(buf
) - 1);
1178 if (len
< 0) return len
;
1180 pkt_dev
->flags
|= F_IPV6
;
1182 if (copy_from_user(buf
, &user_buffer
[i
], len
))
1186 scan_ip6(buf
, pkt_dev
->in6_saddr
.s6_addr
);
1187 fmt_ip6(buf
, pkt_dev
->in6_saddr
.s6_addr
);
1189 ipv6_addr_copy(&pkt_dev
->cur_in6_saddr
, &pkt_dev
->in6_saddr
);
1192 printk("pktgen: src6 set to: %s\n", buf
);
1195 sprintf(pg_result
, "OK: src6=%s", buf
);
1198 if (!strcmp(name
, "src_min")) {
1199 len
= strn_len(&user_buffer
[i
], sizeof(pkt_dev
->src_min
) - 1);
1200 if (len
< 0) { return len
; }
1201 if (copy_from_user(buf
, &user_buffer
[i
], len
))
1204 if (strcmp(buf
, pkt_dev
->src_min
) != 0) {
1205 memset(pkt_dev
->src_min
, 0, sizeof(pkt_dev
->src_min
));
1206 strncpy(pkt_dev
->src_min
, buf
, len
);
1207 pkt_dev
->saddr_min
= in_aton(pkt_dev
->src_min
);
1208 pkt_dev
->cur_saddr
= pkt_dev
->saddr_min
;
1211 printk("pktgen: src_min set to: %s\n", pkt_dev
->src_min
);
1213 sprintf(pg_result
, "OK: src_min=%s", pkt_dev
->src_min
);
1216 if (!strcmp(name
, "src_max")) {
1217 len
= strn_len(&user_buffer
[i
], sizeof(pkt_dev
->src_max
) - 1);
1218 if (len
< 0) { return len
; }
1219 if (copy_from_user(buf
, &user_buffer
[i
], len
))
1222 if (strcmp(buf
, pkt_dev
->src_max
) != 0) {
1223 memset(pkt_dev
->src_max
, 0, sizeof(pkt_dev
->src_max
));
1224 strncpy(pkt_dev
->src_max
, buf
, len
);
1225 pkt_dev
->saddr_max
= in_aton(pkt_dev
->src_max
);
1226 pkt_dev
->cur_saddr
= pkt_dev
->saddr_max
;
1229 printk("pktgen: src_max set to: %s\n", pkt_dev
->src_max
);
1231 sprintf(pg_result
, "OK: src_max=%s", pkt_dev
->src_max
);
1234 if (!strcmp(name
, "dst_mac")) {
1236 unsigned char old_dmac
[6];
1237 unsigned char *m
= pkt_dev
->dst_mac
;
1238 memcpy(old_dmac
, pkt_dev
->dst_mac
, 6);
1240 len
= strn_len(&user_buffer
[i
], sizeof(valstr
) - 1);
1241 if (len
< 0) { return len
; }
1242 memset(valstr
, 0, sizeof(valstr
));
1243 if( copy_from_user(valstr
, &user_buffer
[i
], len
))
1247 for(*m
= 0;*v
&& m
< pkt_dev
->dst_mac
+ 6; v
++) {
1248 if (*v
>= '0' && *v
<= '9') {
1252 if (*v
>= 'A' && *v
<= 'F') {
1254 *m
+= *v
- 'A' + 10;
1256 if (*v
>= 'a' && *v
<= 'f') {
1258 *m
+= *v
- 'a' + 10;
1266 /* Set up Dest MAC */
1267 if (memcmp(old_dmac
, pkt_dev
->dst_mac
, 6) != 0)
1268 memcpy(&(pkt_dev
->hh
[0]), pkt_dev
->dst_mac
, 6);
1270 sprintf(pg_result
, "OK: dstmac");
1273 if (!strcmp(name
, "src_mac")) {
1275 unsigned char *m
= pkt_dev
->src_mac
;
1277 len
= strn_len(&user_buffer
[i
], sizeof(valstr
) - 1);
1278 if (len
< 0) { return len
; }
1279 memset(valstr
, 0, sizeof(valstr
));
1280 if( copy_from_user(valstr
, &user_buffer
[i
], len
))
1284 for(*m
= 0;*v
&& m
< pkt_dev
->src_mac
+ 6; v
++) {
1285 if (*v
>= '0' && *v
<= '9') {
1289 if (*v
>= 'A' && *v
<= 'F') {
1291 *m
+= *v
- 'A' + 10;
1293 if (*v
>= 'a' && *v
<= 'f') {
1295 *m
+= *v
- 'a' + 10;
1303 sprintf(pg_result
, "OK: srcmac");
1307 if (!strcmp(name
, "clear_counters")) {
1308 pktgen_clear_counters(pkt_dev
);
1309 sprintf(pg_result
, "OK: Clearing counters.\n");
1313 if (!strcmp(name
, "flows")) {
1314 len
= num_arg(&user_buffer
[i
], 10, &value
);
1315 if (len
< 0) { return len
; }
1317 if (value
> MAX_CFLOWS
)
1320 pkt_dev
->cflows
= value
;
1321 sprintf(pg_result
, "OK: flows=%u", pkt_dev
->cflows
);
1325 if (!strcmp(name
, "flowlen")) {
1326 len
= num_arg(&user_buffer
[i
], 10, &value
);
1327 if (len
< 0) { return len
; }
1329 pkt_dev
->lflow
= value
;
1330 sprintf(pg_result
, "OK: flowlen=%u", pkt_dev
->lflow
);
1334 sprintf(pkt_dev
->result
, "No such parameter \"%s\"", name
);
1338 static int proc_thread_read(char *buf
, char **start
, off_t offset
,
1339 int len
, int *eof
, void *data
)
1342 struct pktgen_thread
*t
= (struct pktgen_thread
*)(data
);
1343 struct pktgen_dev
*pkt_dev
= NULL
;
1347 printk("pktgen: ERROR: could not find thread in proc_thread_read\n");
1352 p
+= sprintf(p
, "Name: %s max_before_softirq: %d\n",
1353 t
->name
, t
->max_before_softirq
);
1355 p
+= sprintf(p
, "Running: ");
1358 for(pkt_dev
= t
->if_list
;pkt_dev
; pkt_dev
= pkt_dev
->next
)
1359 if(pkt_dev
->running
)
1360 p
+= sprintf(p
, "%s ", pkt_dev
->ifname
);
1362 p
+= sprintf(p
, "\nStopped: ");
1364 for(pkt_dev
= t
->if_list
;pkt_dev
; pkt_dev
= pkt_dev
->next
)
1365 if(!pkt_dev
->running
)
1366 p
+= sprintf(p
, "%s ", pkt_dev
->ifname
);
1369 p
+= sprintf(p
, "\nResult: %s\n", t
->result
);
1371 p
+= sprintf(p
, "\nResult: NA\n");
1380 static int proc_thread_write(struct file
*file
, const char __user
*user_buffer
,
1381 unsigned long count
, void *data
)
1383 int i
= 0, max
, len
, ret
;
1385 struct pktgen_thread
*t
;
1387 unsigned long value
= 0;
1390 // sprintf(pg_result, "Wrong command format");
1395 len
= count_trail_chars(&user_buffer
[i
], max
);
1401 /* Read variable name */
1403 len
= strn_len(&user_buffer
[i
], sizeof(name
) - 1);
1407 memset(name
, 0, sizeof(name
));
1408 if (copy_from_user(name
, &user_buffer
[i
], len
))
1413 len
= count_trail_chars(&user_buffer
[i
], max
);
1420 printk("pktgen: t=%s, count=%lu\n", name
, count
);
1423 t
= (struct pktgen_thread
*)(data
);
1425 printk("pktgen: ERROR: No thread\n");
1430 pg_result
= &(t
->result
[0]);
1432 if (!strcmp(name
, "add_device")) {
1435 len
= strn_len(&user_buffer
[i
], sizeof(f
) - 1);
1440 if( copy_from_user(f
, &user_buffer
[i
], len
) )
1444 pktgen_add_device(t
, f
);
1447 sprintf(pg_result
, "OK: add_device=%s", f
);
1451 if (!strcmp(name
, "rem_device_all")) {
1453 t
->control
|= T_REMDEV
;
1455 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
1457 sprintf(pg_result
, "OK: rem_device_all");
1461 if (!strcmp(name
, "max_before_softirq")) {
1462 len
= num_arg(&user_buffer
[i
], 10, &value
);
1464 t
->max_before_softirq
= value
;
1467 sprintf(pg_result
, "OK: max_before_softirq=%lu", value
);
1477 static int create_proc_dir(void)
1480 /* does proc_dir already exists */
1481 len
= strlen(PG_PROC_DIR
);
1483 for (pg_proc_dir
= proc_net
->subdir
; pg_proc_dir
; pg_proc_dir
=pg_proc_dir
->next
) {
1484 if ((pg_proc_dir
->namelen
== len
) &&
1485 (! memcmp(pg_proc_dir
->name
, PG_PROC_DIR
, len
)))
1490 pg_proc_dir
= create_proc_entry(PG_PROC_DIR
, S_IFDIR
, proc_net
);
1498 static int remove_proc_dir(void)
1500 remove_proc_entry(PG_PROC_DIR
, proc_net
);
1504 /* Think find or remove for NN */
1505 static struct pktgen_dev
*__pktgen_NN_threads(const char* ifname
, int remove
)
1507 struct pktgen_thread
*t
;
1508 struct pktgen_dev
*pkt_dev
= NULL
;
1513 pkt_dev
= pktgen_find_dev(t
, ifname
);
1517 pktgen_remove_device(t
, pkt_dev
);
1527 static struct pktgen_dev
*pktgen_NN_threads(const char* ifname
, int remove
)
1529 struct pktgen_dev
*pkt_dev
= NULL
;
1531 pkt_dev
= __pktgen_NN_threads(ifname
, remove
);
1536 static int pktgen_device_event(struct notifier_block
*unused
, unsigned long event
, void *ptr
)
1538 struct net_device
*dev
= (struct net_device
*)(ptr
);
1540 /* It is OK that we do not hold the group lock right now,
1541 * as we run under the RTNL lock.
1545 case NETDEV_CHANGEADDR
:
1546 case NETDEV_GOING_DOWN
:
1549 /* Ignore for now */
1552 case NETDEV_UNREGISTER
:
1553 pktgen_NN_threads(dev
->name
, REMOVE
);
1560 /* Associate pktgen_dev with a device. */
1562 static struct net_device
* pktgen_setup_dev(struct pktgen_dev
*pkt_dev
) {
1563 struct net_device
*odev
;
1565 /* Clean old setups */
1567 if (pkt_dev
->odev
) {
1568 dev_put(pkt_dev
->odev
);
1569 pkt_dev
->odev
= NULL
;
1572 odev
= dev_get_by_name(pkt_dev
->ifname
);
1575 printk("pktgen: no such netdevice: \"%s\"\n", pkt_dev
->ifname
);
1578 if (odev
->type
!= ARPHRD_ETHER
) {
1579 printk("pktgen: not an ethernet device: \"%s\"\n", pkt_dev
->ifname
);
1582 if (!netif_running(odev
)) {
1583 printk("pktgen: device is down: \"%s\"\n", pkt_dev
->ifname
);
1586 pkt_dev
->odev
= odev
;
1588 return pkt_dev
->odev
;
1597 /* Read pkt_dev from the interface and set up internal pktgen_dev
1598 * structure to have the right information to create/send packets
1600 static void pktgen_setup_inject(struct pktgen_dev
*pkt_dev
)
1602 /* Try once more, just in case it works now. */
1604 pktgen_setup_dev(pkt_dev
);
1606 if (!pkt_dev
->odev
) {
1607 printk("pktgen: ERROR: pkt_dev->odev == NULL in setup_inject.\n");
1608 sprintf(pkt_dev
->result
, "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
1612 /* Default to the interface's mac if not explicitly set. */
1614 if ((pkt_dev
->src_mac
[0] == 0) &&
1615 (pkt_dev
->src_mac
[1] == 0) &&
1616 (pkt_dev
->src_mac
[2] == 0) &&
1617 (pkt_dev
->src_mac
[3] == 0) &&
1618 (pkt_dev
->src_mac
[4] == 0) &&
1619 (pkt_dev
->src_mac
[5] == 0)) {
1621 memcpy(&(pkt_dev
->hh
[6]), pkt_dev
->odev
->dev_addr
, 6);
1623 /* Set up Dest MAC */
1624 memcpy(&(pkt_dev
->hh
[0]), pkt_dev
->dst_mac
, 6);
1626 /* Set up pkt size */
1627 pkt_dev
->cur_pkt_size
= pkt_dev
->min_pkt_size
;
1629 if(pkt_dev
->flags
& F_IPV6
) {
1631 * Skip this automatic address setting until locks or functions
1636 int i
, set
= 0, err
=1;
1637 struct inet6_dev
*idev
;
1639 for(i
=0; i
< IN6_ADDR_HSIZE
; i
++)
1640 if(pkt_dev
->cur_in6_saddr
.s6_addr
[i
]) {
1648 * Use linklevel address if unconfigured.
1650 * use ipv6_get_lladdr if/when it's get exported
1654 read_lock(&addrconf_lock
);
1655 if ((idev
= __in6_dev_get(pkt_dev
->odev
)) != NULL
) {
1656 struct inet6_ifaddr
*ifp
;
1658 read_lock_bh(&idev
->lock
);
1659 for (ifp
=idev
->addr_list
; ifp
; ifp
=ifp
->if_next
) {
1660 if (ifp
->scope
== IFA_LINK
&& !(ifp
->flags
&IFA_F_TENTATIVE
)) {
1661 ipv6_addr_copy(&pkt_dev
->cur_in6_saddr
, &ifp
->addr
);
1666 read_unlock_bh(&idev
->lock
);
1668 read_unlock(&addrconf_lock
);
1669 if(err
) printk("pktgen: ERROR: IPv6 link address not availble.\n");
1674 pkt_dev
->saddr_min
= 0;
1675 pkt_dev
->saddr_max
= 0;
1676 if (strlen(pkt_dev
->src_min
) == 0) {
1678 struct in_device
*in_dev
;
1681 in_dev
= __in_dev_get(pkt_dev
->odev
);
1683 if (in_dev
->ifa_list
) {
1684 pkt_dev
->saddr_min
= in_dev
->ifa_list
->ifa_address
;
1685 pkt_dev
->saddr_max
= pkt_dev
->saddr_min
;
1687 __in_dev_put(in_dev
);
1692 pkt_dev
->saddr_min
= in_aton(pkt_dev
->src_min
);
1693 pkt_dev
->saddr_max
= in_aton(pkt_dev
->src_max
);
1696 pkt_dev
->daddr_min
= in_aton(pkt_dev
->dst_min
);
1697 pkt_dev
->daddr_max
= in_aton(pkt_dev
->dst_max
);
1699 /* Initialize current values. */
1700 pkt_dev
->cur_dst_mac_offset
= 0;
1701 pkt_dev
->cur_src_mac_offset
= 0;
1702 pkt_dev
->cur_saddr
= pkt_dev
->saddr_min
;
1703 pkt_dev
->cur_daddr
= pkt_dev
->daddr_min
;
1704 pkt_dev
->cur_udp_dst
= pkt_dev
->udp_dst_min
;
1705 pkt_dev
->cur_udp_src
= pkt_dev
->udp_src_min
;
1706 pkt_dev
->nflows
= 0;
1709 static void spin(struct pktgen_dev
*pkt_dev
, __u64 spin_until_us
)
1714 start
= now
= getCurUs();
1715 printk(KERN_INFO
"sleeping for %d\n", (int)(spin_until_us
- now
));
1716 while (now
< spin_until_us
) {
1717 /* TODO: optimise sleeping behavior */
1718 if (spin_until_us
- now
> jiffies_to_usecs(1)+1)
1719 schedule_timeout_interruptible(1);
1720 else if (spin_until_us
- now
> 100) {
1722 if (!pkt_dev
->running
)
1731 pkt_dev
->idle_acc
+= now
- start
;
1735 /* Increment/randomize headers according to flags and current values
1736 * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
1738 static void mod_cur_headers(struct pktgen_dev
*pkt_dev
) {
1743 if(pkt_dev
->cflows
) {
1744 flow
= pktgen_random() % pkt_dev
->cflows
;
1746 if (pkt_dev
->flows
[flow
].count
> pkt_dev
->lflow
)
1747 pkt_dev
->flows
[flow
].count
= 0;
1751 /* Deal with source MAC */
1752 if (pkt_dev
->src_mac_count
> 1) {
1756 if (pkt_dev
->flags
& F_MACSRC_RND
)
1757 mc
= pktgen_random() % (pkt_dev
->src_mac_count
);
1759 mc
= pkt_dev
->cur_src_mac_offset
++;
1760 if (pkt_dev
->cur_src_mac_offset
> pkt_dev
->src_mac_count
)
1761 pkt_dev
->cur_src_mac_offset
= 0;
1764 tmp
= pkt_dev
->src_mac
[5] + (mc
& 0xFF);
1765 pkt_dev
->hh
[11] = tmp
;
1766 tmp
= (pkt_dev
->src_mac
[4] + ((mc
>> 8) & 0xFF) + (tmp
>> 8));
1767 pkt_dev
->hh
[10] = tmp
;
1768 tmp
= (pkt_dev
->src_mac
[3] + ((mc
>> 16) & 0xFF) + (tmp
>> 8));
1769 pkt_dev
->hh
[9] = tmp
;
1770 tmp
= (pkt_dev
->src_mac
[2] + ((mc
>> 24) & 0xFF) + (tmp
>> 8));
1771 pkt_dev
->hh
[8] = tmp
;
1772 tmp
= (pkt_dev
->src_mac
[1] + (tmp
>> 8));
1773 pkt_dev
->hh
[7] = tmp
;
1776 /* Deal with Destination MAC */
1777 if (pkt_dev
->dst_mac_count
> 1) {
1781 if (pkt_dev
->flags
& F_MACDST_RND
)
1782 mc
= pktgen_random() % (pkt_dev
->dst_mac_count
);
1785 mc
= pkt_dev
->cur_dst_mac_offset
++;
1786 if (pkt_dev
->cur_dst_mac_offset
> pkt_dev
->dst_mac_count
) {
1787 pkt_dev
->cur_dst_mac_offset
= 0;
1791 tmp
= pkt_dev
->dst_mac
[5] + (mc
& 0xFF);
1792 pkt_dev
->hh
[5] = tmp
;
1793 tmp
= (pkt_dev
->dst_mac
[4] + ((mc
>> 8) & 0xFF) + (tmp
>> 8));
1794 pkt_dev
->hh
[4] = tmp
;
1795 tmp
= (pkt_dev
->dst_mac
[3] + ((mc
>> 16) & 0xFF) + (tmp
>> 8));
1796 pkt_dev
->hh
[3] = tmp
;
1797 tmp
= (pkt_dev
->dst_mac
[2] + ((mc
>> 24) & 0xFF) + (tmp
>> 8));
1798 pkt_dev
->hh
[2] = tmp
;
1799 tmp
= (pkt_dev
->dst_mac
[1] + (tmp
>> 8));
1800 pkt_dev
->hh
[1] = tmp
;
1803 if (pkt_dev
->udp_src_min
< pkt_dev
->udp_src_max
) {
1804 if (pkt_dev
->flags
& F_UDPSRC_RND
)
1805 pkt_dev
->cur_udp_src
= ((pktgen_random() % (pkt_dev
->udp_src_max
- pkt_dev
->udp_src_min
)) + pkt_dev
->udp_src_min
);
1808 pkt_dev
->cur_udp_src
++;
1809 if (pkt_dev
->cur_udp_src
>= pkt_dev
->udp_src_max
)
1810 pkt_dev
->cur_udp_src
= pkt_dev
->udp_src_min
;
1814 if (pkt_dev
->udp_dst_min
< pkt_dev
->udp_dst_max
) {
1815 if (pkt_dev
->flags
& F_UDPDST_RND
) {
1816 pkt_dev
->cur_udp_dst
= ((pktgen_random() % (pkt_dev
->udp_dst_max
- pkt_dev
->udp_dst_min
)) + pkt_dev
->udp_dst_min
);
1819 pkt_dev
->cur_udp_dst
++;
1820 if (pkt_dev
->cur_udp_dst
>= pkt_dev
->udp_dst_max
)
1821 pkt_dev
->cur_udp_dst
= pkt_dev
->udp_dst_min
;
1825 if (!(pkt_dev
->flags
& F_IPV6
)) {
1827 if ((imn
= ntohl(pkt_dev
->saddr_min
)) < (imx
= ntohl(pkt_dev
->saddr_max
))) {
1829 if (pkt_dev
->flags
& F_IPSRC_RND
)
1830 t
= ((pktgen_random() % (imx
- imn
)) + imn
);
1832 t
= ntohl(pkt_dev
->cur_saddr
);
1838 pkt_dev
->cur_saddr
= htonl(t
);
1841 if (pkt_dev
->cflows
&& pkt_dev
->flows
[flow
].count
!= 0) {
1842 pkt_dev
->cur_daddr
= pkt_dev
->flows
[flow
].cur_daddr
;
1845 if ((imn
= ntohl(pkt_dev
->daddr_min
)) < (imx
= ntohl(pkt_dev
->daddr_max
))) {
1847 if (pkt_dev
->flags
& F_IPDST_RND
) {
1849 t
= ((pktgen_random() % (imx
- imn
)) + imn
);
1852 while( LOOPBACK(t
) || MULTICAST(t
) || BADCLASS(t
) || ZERONET(t
) || LOCAL_MCAST(t
) ) {
1853 t
= ((pktgen_random() % (imx
- imn
)) + imn
);
1856 pkt_dev
->cur_daddr
= t
;
1860 t
= ntohl(pkt_dev
->cur_daddr
);
1865 pkt_dev
->cur_daddr
= htonl(t
);
1868 if(pkt_dev
->cflows
) {
1869 pkt_dev
->flows
[flow
].cur_daddr
= pkt_dev
->cur_daddr
;
1876 if(pkt_dev
->min_in6_daddr
.s6_addr32
[0] == 0 &&
1877 pkt_dev
->min_in6_daddr
.s6_addr32
[1] == 0 &&
1878 pkt_dev
->min_in6_daddr
.s6_addr32
[2] == 0 &&
1879 pkt_dev
->min_in6_daddr
.s6_addr32
[3] == 0);
1883 /* Only random destinations yet */
1885 for(i
=0; i
< 4; i
++) {
1886 pkt_dev
->cur_in6_daddr
.s6_addr32
[i
] =
1888 pkt_dev
->min_in6_daddr
.s6_addr32
[i
]) &
1889 pkt_dev
->max_in6_daddr
.s6_addr32
[i
]);
1894 if (pkt_dev
->min_pkt_size
< pkt_dev
->max_pkt_size
) {
1896 if (pkt_dev
->flags
& F_TXSIZE_RND
) {
1897 t
= ((pktgen_random() % (pkt_dev
->max_pkt_size
- pkt_dev
->min_pkt_size
))
1898 + pkt_dev
->min_pkt_size
);
1901 t
= pkt_dev
->cur_pkt_size
+ 1;
1902 if (t
> pkt_dev
->max_pkt_size
)
1903 t
= pkt_dev
->min_pkt_size
;
1905 pkt_dev
->cur_pkt_size
= t
;
1908 pkt_dev
->flows
[flow
].count
++;
1912 static struct sk_buff
*fill_packet_ipv4(struct net_device
*odev
,
1913 struct pktgen_dev
*pkt_dev
)
1915 struct sk_buff
*skb
= NULL
;
1917 struct udphdr
*udph
;
1920 struct pktgen_hdr
*pgh
= NULL
;
1922 /* Update any of the values, used when we're incrementing various
1925 mod_cur_headers(pkt_dev
);
1927 skb
= alloc_skb(pkt_dev
->cur_pkt_size
+ 64 + 16, GFP_ATOMIC
);
1929 sprintf(pkt_dev
->result
, "No memory");
1933 skb_reserve(skb
, 16);
1935 /* Reserve for ethernet and IP header */
1936 eth
= (__u8
*) skb_push(skb
, 14);
1937 iph
= (struct iphdr
*)skb_put(skb
, sizeof(struct iphdr
));
1938 udph
= (struct udphdr
*)skb_put(skb
, sizeof(struct udphdr
));
1940 memcpy(eth
, pkt_dev
->hh
, 12);
1941 *(u16
*)ð
[12] = __constant_htons(ETH_P_IP
);
1943 datalen
= pkt_dev
->cur_pkt_size
- 14 - 20 - 8; /* Eth + IPh + UDPh */
1944 if (datalen
< sizeof(struct pktgen_hdr
))
1945 datalen
= sizeof(struct pktgen_hdr
);
1947 udph
->source
= htons(pkt_dev
->cur_udp_src
);
1948 udph
->dest
= htons(pkt_dev
->cur_udp_dst
);
1949 udph
->len
= htons(datalen
+ 8); /* DATA + udphdr */
1950 udph
->check
= 0; /* No checksum */
1956 iph
->protocol
= IPPROTO_UDP
; /* UDP */
1957 iph
->saddr
= pkt_dev
->cur_saddr
;
1958 iph
->daddr
= pkt_dev
->cur_daddr
;
1960 iplen
= 20 + 8 + datalen
;
1961 iph
->tot_len
= htons(iplen
);
1963 iph
->check
= ip_fast_csum((void *) iph
, iph
->ihl
);
1964 skb
->protocol
= __constant_htons(ETH_P_IP
);
1965 skb
->mac
.raw
= ((u8
*)iph
) - 14;
1967 skb
->pkt_type
= PACKET_HOST
;
1969 if (pkt_dev
->nfrags
<= 0)
1970 pgh
= (struct pktgen_hdr
*)skb_put(skb
, datalen
);
1972 int frags
= pkt_dev
->nfrags
;
1975 pgh
= (struct pktgen_hdr
*)(((char*)(udph
)) + 8);
1977 if (frags
> MAX_SKB_FRAGS
)
1978 frags
= MAX_SKB_FRAGS
;
1979 if (datalen
> frags
*PAGE_SIZE
) {
1980 skb_put(skb
, datalen
-frags
*PAGE_SIZE
);
1981 datalen
= frags
*PAGE_SIZE
;
1985 while (datalen
> 0) {
1986 struct page
*page
= alloc_pages(GFP_KERNEL
, 0);
1987 skb_shinfo(skb
)->frags
[i
].page
= page
;
1988 skb_shinfo(skb
)->frags
[i
].page_offset
= 0;
1989 skb_shinfo(skb
)->frags
[i
].size
=
1990 (datalen
< PAGE_SIZE
? datalen
: PAGE_SIZE
);
1991 datalen
-= skb_shinfo(skb
)->frags
[i
].size
;
1992 skb
->len
+= skb_shinfo(skb
)->frags
[i
].size
;
1993 skb
->data_len
+= skb_shinfo(skb
)->frags
[i
].size
;
1995 skb_shinfo(skb
)->nr_frags
= i
;
2004 rem
= skb_shinfo(skb
)->frags
[i
- 1].size
/ 2;
2008 skb_shinfo(skb
)->frags
[i
- 1].size
-= rem
;
2010 skb_shinfo(skb
)->frags
[i
] = skb_shinfo(skb
)->frags
[i
- 1];
2011 get_page(skb_shinfo(skb
)->frags
[i
].page
);
2012 skb_shinfo(skb
)->frags
[i
].page
= skb_shinfo(skb
)->frags
[i
- 1].page
;
2013 skb_shinfo(skb
)->frags
[i
].page_offset
+= skb_shinfo(skb
)->frags
[i
- 1].size
;
2014 skb_shinfo(skb
)->frags
[i
].size
= rem
;
2016 skb_shinfo(skb
)->nr_frags
= i
;
2020 /* Stamp the time, and sequence number, convert them to network byte order */
2023 struct timeval timestamp
;
2025 pgh
->pgh_magic
= htonl(PKTGEN_MAGIC
);
2026 pgh
->seq_num
= htonl(pkt_dev
->seq_num
);
2028 do_gettimeofday(×tamp
);
2029 pgh
->tv_sec
= htonl(timestamp
.tv_sec
);
2030 pgh
->tv_usec
= htonl(timestamp
.tv_usec
);
2038 * scan_ip6, fmt_ip taken from dietlibc-0.21
2039 * Author Felix von Leitner <felix-dietlibc@fefe.de>
2041 * Slightly modified for kernel.
2042 * Should be candidate for net/ipv4/utils.c
2046 static unsigned int scan_ip6(const char *s
,char ip
[16])
2052 unsigned int prefixlen
=0;
2053 unsigned int suffixlen
=0;
2056 for (i
=0; i
<16; i
++) ip
[i
]=0;
2061 if (s
[1] == ':') { /* Found "::", skip to part 2 */
2070 u
=simple_strtoul(s
,&tmp
,16);
2075 if (prefixlen
==12 && s
[i
]=='.') {
2077 /* the last 4 bytes may be written as IPv4 address */
2080 memcpy((struct in_addr
*)(ip
+12), &tmp
, sizeof(tmp
));
2083 ip
[prefixlen
++] = (u
>> 8);
2084 ip
[prefixlen
++] = (u
& 255);
2090 /* part 2, after "::" */
2097 } else if (suffixlen
!=0)
2101 u
=simple_strtol(s
,&tmp
,16);
2108 if (suffixlen
+prefixlen
<=12 && s
[i
]=='.') {
2110 memcpy((struct in_addr
*)(suffix
+suffixlen
), &tmp
, sizeof(tmp
));
2115 suffix
[suffixlen
++] = (u
>> 8);
2116 suffix
[suffixlen
++] = (u
& 255);
2118 if (prefixlen
+suffixlen
==16)
2121 for (i
=0; i
<suffixlen
; i
++)
2122 ip
[16-suffixlen
+i
] = suffix
[i
];
2126 static char tohex(char hexdigit
) {
2127 return hexdigit
>9?hexdigit
+'a'-10:hexdigit
+'0';
2130 static int fmt_xlong(char* s
,unsigned int i
) {
2132 *s
=tohex((i
>>12)&0xf); if (s
!=bak
|| *s
!='0') ++s
;
2133 *s
=tohex((i
>>8)&0xf); if (s
!=bak
|| *s
!='0') ++s
;
2134 *s
=tohex((i
>>4)&0xf); if (s
!=bak
|| *s
!='0') ++s
;
2139 static unsigned int fmt_ip6(char *s
,const char ip
[16]) {
2143 unsigned int compressing
;
2146 len
= 0; compressing
= 0;
2147 for (j
=0; j
<16; j
+=2) {
2149 #ifdef V4MAPPEDPREFIX
2150 if (j
==12 && !memcmp(ip
,V4mappedprefix
,12)) {
2151 inet_ntoa_r(*(struct in_addr
*)(ip
+12),s
);
2156 temp
= ((unsigned long) (unsigned char) ip
[j
] << 8) +
2157 (unsigned long) (unsigned char) ip
[j
+1];
2170 i
= fmt_xlong(s
,temp
); len
+= i
; s
+= i
;
2184 static struct sk_buff
*fill_packet_ipv6(struct net_device
*odev
,
2185 struct pktgen_dev
*pkt_dev
)
2187 struct sk_buff
*skb
= NULL
;
2189 struct udphdr
*udph
;
2191 struct ipv6hdr
*iph
;
2192 struct pktgen_hdr
*pgh
= NULL
;
2194 /* Update any of the values, used when we're incrementing various
2197 mod_cur_headers(pkt_dev
);
2199 skb
= alloc_skb(pkt_dev
->cur_pkt_size
+ 64 + 16, GFP_ATOMIC
);
2201 sprintf(pkt_dev
->result
, "No memory");
2205 skb_reserve(skb
, 16);
2207 /* Reserve for ethernet and IP header */
2208 eth
= (__u8
*) skb_push(skb
, 14);
2209 iph
= (struct ipv6hdr
*)skb_put(skb
, sizeof(struct ipv6hdr
));
2210 udph
= (struct udphdr
*)skb_put(skb
, sizeof(struct udphdr
));
2212 memcpy(eth
, pkt_dev
->hh
, 12);
2213 *(u16
*)ð
[12] = __constant_htons(ETH_P_IPV6
);
2215 datalen
= pkt_dev
->cur_pkt_size
-14-
2216 sizeof(struct ipv6hdr
)-sizeof(struct udphdr
); /* Eth + IPh + UDPh */
2218 if (datalen
< sizeof(struct pktgen_hdr
)) {
2219 datalen
= sizeof(struct pktgen_hdr
);
2220 if (net_ratelimit())
2221 printk(KERN_INFO
"pktgen: increased datalen to %d\n", datalen
);
2224 udph
->source
= htons(pkt_dev
->cur_udp_src
);
2225 udph
->dest
= htons(pkt_dev
->cur_udp_dst
);
2226 udph
->len
= htons(datalen
+ sizeof(struct udphdr
));
2227 udph
->check
= 0; /* No checksum */
2229 *(u32
*)iph
= __constant_htonl(0x60000000); /* Version + flow */
2231 iph
->hop_limit
= 32;
2233 iph
->payload_len
= htons(sizeof(struct udphdr
) + datalen
);
2234 iph
->nexthdr
= IPPROTO_UDP
;
2236 ipv6_addr_copy(&iph
->daddr
, &pkt_dev
->cur_in6_daddr
);
2237 ipv6_addr_copy(&iph
->saddr
, &pkt_dev
->cur_in6_saddr
);
2239 skb
->mac
.raw
= ((u8
*)iph
) - 14;
2240 skb
->protocol
= __constant_htons(ETH_P_IPV6
);
2242 skb
->pkt_type
= PACKET_HOST
;
2244 if (pkt_dev
->nfrags
<= 0)
2245 pgh
= (struct pktgen_hdr
*)skb_put(skb
, datalen
);
2247 int frags
= pkt_dev
->nfrags
;
2250 pgh
= (struct pktgen_hdr
*)(((char*)(udph
)) + 8);
2252 if (frags
> MAX_SKB_FRAGS
)
2253 frags
= MAX_SKB_FRAGS
;
2254 if (datalen
> frags
*PAGE_SIZE
) {
2255 skb_put(skb
, datalen
-frags
*PAGE_SIZE
);
2256 datalen
= frags
*PAGE_SIZE
;
2260 while (datalen
> 0) {
2261 struct page
*page
= alloc_pages(GFP_KERNEL
, 0);
2262 skb_shinfo(skb
)->frags
[i
].page
= page
;
2263 skb_shinfo(skb
)->frags
[i
].page_offset
= 0;
2264 skb_shinfo(skb
)->frags
[i
].size
=
2265 (datalen
< PAGE_SIZE
? datalen
: PAGE_SIZE
);
2266 datalen
-= skb_shinfo(skb
)->frags
[i
].size
;
2267 skb
->len
+= skb_shinfo(skb
)->frags
[i
].size
;
2268 skb
->data_len
+= skb_shinfo(skb
)->frags
[i
].size
;
2270 skb_shinfo(skb
)->nr_frags
= i
;
2279 rem
= skb_shinfo(skb
)->frags
[i
- 1].size
/ 2;
2283 skb_shinfo(skb
)->frags
[i
- 1].size
-= rem
;
2285 skb_shinfo(skb
)->frags
[i
] = skb_shinfo(skb
)->frags
[i
- 1];
2286 get_page(skb_shinfo(skb
)->frags
[i
].page
);
2287 skb_shinfo(skb
)->frags
[i
].page
= skb_shinfo(skb
)->frags
[i
- 1].page
;
2288 skb_shinfo(skb
)->frags
[i
].page_offset
+= skb_shinfo(skb
)->frags
[i
- 1].size
;
2289 skb_shinfo(skb
)->frags
[i
].size
= rem
;
2291 skb_shinfo(skb
)->nr_frags
= i
;
2295 /* Stamp the time, and sequence number, convert them to network byte order */
2296 /* should we update cloned packets too ? */
2298 struct timeval timestamp
;
2300 pgh
->pgh_magic
= htonl(PKTGEN_MAGIC
);
2301 pgh
->seq_num
= htonl(pkt_dev
->seq_num
);
2303 do_gettimeofday(×tamp
);
2304 pgh
->tv_sec
= htonl(timestamp
.tv_sec
);
2305 pgh
->tv_usec
= htonl(timestamp
.tv_usec
);
2312 static inline struct sk_buff
*fill_packet(struct net_device
*odev
,
2313 struct pktgen_dev
*pkt_dev
)
2315 if(pkt_dev
->flags
& F_IPV6
)
2316 return fill_packet_ipv6(odev
, pkt_dev
);
2318 return fill_packet_ipv4(odev
, pkt_dev
);
2321 static void pktgen_clear_counters(struct pktgen_dev
*pkt_dev
)
2323 pkt_dev
->seq_num
= 1;
2324 pkt_dev
->idle_acc
= 0;
2326 pkt_dev
->tx_bytes
= 0;
2327 pkt_dev
->errors
= 0;
2330 /* Set up structure for sending pkts, clear counters */
2332 static void pktgen_run(struct pktgen_thread
*t
)
2334 struct pktgen_dev
*pkt_dev
= NULL
;
2337 PG_DEBUG(printk("pktgen: entering pktgen_run. %p\n", t
));
2340 for (pkt_dev
= t
->if_list
; pkt_dev
; pkt_dev
= pkt_dev
->next
) {
2343 * setup odev and create initial packet.
2345 pktgen_setup_inject(pkt_dev
);
2348 pktgen_clear_counters(pkt_dev
);
2349 pkt_dev
->running
= 1; /* Cranke yeself! */
2350 pkt_dev
->skb
= NULL
;
2351 pkt_dev
->started_at
= getCurUs();
2352 pkt_dev
->next_tx_us
= getCurUs(); /* Transmit immediately */
2353 pkt_dev
->next_tx_ns
= 0;
2355 strcpy(pkt_dev
->result
, "Starting");
2359 strcpy(pkt_dev
->result
, "Error starting");
2362 if(started
) t
->control
&= ~(T_STOP
);
2365 static void pktgen_stop_all_threads_ifs(void)
2367 struct pktgen_thread
*t
= pktgen_threads
;
2369 PG_DEBUG(printk("pktgen: entering pktgen_stop_all_threads.\n"));
2379 static int thread_is_running(struct pktgen_thread
*t
)
2381 struct pktgen_dev
*next
;
2384 for(next
=t
->if_list
; next
; next
=next
->next
) {
2393 static int pktgen_wait_thread_run(struct pktgen_thread
*t
)
2397 while(thread_is_running(t
)) {
2401 msleep_interruptible(100);
2403 if (signal_pending(current
))
2413 static int pktgen_wait_all_threads_run(void)
2415 struct pktgen_thread
*t
= pktgen_threads
;
2419 sig
= pktgen_wait_thread_run(t
);
2420 if( sig
== 0 ) break;
2428 t
->control
|= (T_STOP
);
2436 static void pktgen_run_all_threads(void)
2438 struct pktgen_thread
*t
= pktgen_threads
;
2440 PG_DEBUG(printk("pktgen: entering pktgen_run_all_threads.\n"));
2445 t
->control
|= (T_RUN
);
2450 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
2452 pktgen_wait_all_threads_run();
2456 static void show_results(struct pktgen_dev
*pkt_dev
, int nr_frags
)
2458 __u64 total_us
, bps
, mbps
, pps
, idle
;
2459 char *p
= pkt_dev
->result
;
2461 total_us
= pkt_dev
->stopped_at
- pkt_dev
->started_at
;
2463 idle
= pkt_dev
->idle_acc
;
2465 p
+= sprintf(p
, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
2466 (unsigned long long) total_us
,
2467 (unsigned long long)(total_us
- idle
),
2468 (unsigned long long) idle
,
2469 (unsigned long long) pkt_dev
->sofar
,
2470 pkt_dev
->cur_pkt_size
, nr_frags
);
2472 pps
= pkt_dev
->sofar
* USEC_PER_SEC
;
2474 while ((total_us
>> 32) != 0) {
2479 do_div(pps
, total_us
);
2481 bps
= pps
* 8 * pkt_dev
->cur_pkt_size
;
2484 do_div(mbps
, 1000000);
2485 p
+= sprintf(p
, " %llupps %lluMb/sec (%llubps) errors: %llu",
2486 (unsigned long long) pps
,
2487 (unsigned long long) mbps
,
2488 (unsigned long long) bps
,
2489 (unsigned long long) pkt_dev
->errors
);
2493 /* Set stopped-at timer, remove from running list, do counters & statistics */
2495 static int pktgen_stop_device(struct pktgen_dev
*pkt_dev
)
2498 if (!pkt_dev
->running
) {
2499 printk("pktgen: interface: %s is already stopped\n", pkt_dev
->ifname
);
2503 pkt_dev
->stopped_at
= getCurUs();
2504 pkt_dev
->running
= 0;
2506 show_results(pkt_dev
, skb_shinfo(pkt_dev
->skb
)->nr_frags
);
2509 kfree_skb(pkt_dev
->skb
);
2511 pkt_dev
->skb
= NULL
;
2516 static struct pktgen_dev
*next_to_run(struct pktgen_thread
*t
)
2518 struct pktgen_dev
*next
, *best
= NULL
;
2522 for(next
=t
->if_list
; next
; next
=next
->next
) {
2523 if(!next
->running
) continue;
2524 if(best
== NULL
) best
=next
;
2525 else if ( next
->next_tx_us
< best
->next_tx_us
)
2532 static void pktgen_stop(struct pktgen_thread
*t
) {
2533 struct pktgen_dev
*next
= NULL
;
2535 PG_DEBUG(printk("pktgen: entering pktgen_stop.\n"));
2539 for(next
=t
->if_list
; next
; next
=next
->next
)
2540 pktgen_stop_device(next
);
2545 static void pktgen_rem_all_ifs(struct pktgen_thread
*t
)
2547 struct pktgen_dev
*cur
, *next
= NULL
;
2549 /* Remove all devices, free mem */
2553 for(cur
=t
->if_list
; cur
; cur
=next
) {
2555 pktgen_remove_device(t
, cur
);
2561 static void pktgen_rem_thread(struct pktgen_thread
*t
)
2563 /* Remove from the thread list */
2565 struct pktgen_thread
*tmp
= pktgen_threads
;
2567 if (strlen(t
->fname
))
2568 remove_proc_entry(t
->fname
, NULL
);
2573 pktgen_threads
= tmp
->next
;
2576 if (tmp
->next
== t
) {
2577 tmp
->next
= t
->next
;
2587 static __inline__
void pktgen_xmit(struct pktgen_dev
*pkt_dev
)
2589 struct net_device
*odev
= NULL
;
2590 __u64 idle_start
= 0;
2593 odev
= pkt_dev
->odev
;
2595 if (pkt_dev
->delay_us
|| pkt_dev
->delay_ns
) {
2599 if (now
< pkt_dev
->next_tx_us
)
2600 spin(pkt_dev
, pkt_dev
->next_tx_us
);
2602 /* This is max DELAY, this has special meaning of
2605 if (pkt_dev
->delay_us
== 0x7FFFFFFF) {
2606 pkt_dev
->next_tx_us
= getCurUs() + pkt_dev
->delay_us
;
2607 pkt_dev
->next_tx_ns
= pkt_dev
->delay_ns
;
2612 if (netif_queue_stopped(odev
) || need_resched()) {
2613 idle_start
= getCurUs();
2615 if (!netif_running(odev
)) {
2616 pktgen_stop_device(pkt_dev
);
2622 pkt_dev
->idle_acc
+= getCurUs() - idle_start
;
2624 if (netif_queue_stopped(odev
)) {
2625 pkt_dev
->next_tx_us
= getCurUs(); /* TODO */
2626 pkt_dev
->next_tx_ns
= 0;
2627 goto out
; /* Try the next interface */
2631 if (pkt_dev
->last_ok
|| !pkt_dev
->skb
) {
2632 if ((++pkt_dev
->clone_count
>= pkt_dev
->clone_skb
) || (!pkt_dev
->skb
)) {
2633 /* build a new pkt */
2635 kfree_skb(pkt_dev
->skb
);
2637 pkt_dev
->skb
= fill_packet(odev
, pkt_dev
);
2638 if (pkt_dev
->skb
== NULL
) {
2639 printk("pktgen: ERROR: couldn't allocate skb in fill_packet.\n");
2641 pkt_dev
->clone_count
--; /* back out increment, OOM */
2644 pkt_dev
->allocated_skbs
++;
2645 pkt_dev
->clone_count
= 0; /* reset counter */
2649 spin_lock_bh(&odev
->xmit_lock
);
2650 if (!netif_queue_stopped(odev
)) {
2652 atomic_inc(&(pkt_dev
->skb
->users
));
2654 ret
= odev
->hard_start_xmit(pkt_dev
->skb
, odev
);
2655 if (likely(ret
== NETDEV_TX_OK
)) {
2656 pkt_dev
->last_ok
= 1;
2659 pkt_dev
->tx_bytes
+= pkt_dev
->cur_pkt_size
;
2661 } else if (ret
== NETDEV_TX_LOCKED
2662 && (odev
->features
& NETIF_F_LLTX
)) {
2665 } else { /* Retry it next time */
2667 atomic_dec(&(pkt_dev
->skb
->users
));
2669 if (debug
&& net_ratelimit())
2670 printk(KERN_INFO
"pktgen: Hard xmit error\n");
2673 pkt_dev
->last_ok
= 0;
2676 pkt_dev
->next_tx_us
= getCurUs();
2677 pkt_dev
->next_tx_ns
= 0;
2679 pkt_dev
->next_tx_us
+= pkt_dev
->delay_us
;
2680 pkt_dev
->next_tx_ns
+= pkt_dev
->delay_ns
;
2682 if (pkt_dev
->next_tx_ns
> 1000) {
2683 pkt_dev
->next_tx_us
++;
2684 pkt_dev
->next_tx_ns
-= 1000;
2688 else { /* Retry it next time */
2689 pkt_dev
->last_ok
= 0;
2690 pkt_dev
->next_tx_us
= getCurUs(); /* TODO */
2691 pkt_dev
->next_tx_ns
= 0;
2694 spin_unlock_bh(&odev
->xmit_lock
);
2696 /* If pkt_dev->count is zero, then run forever */
2697 if ((pkt_dev
->count
!= 0) && (pkt_dev
->sofar
>= pkt_dev
->count
)) {
2698 if (atomic_read(&(pkt_dev
->skb
->users
)) != 1) {
2699 idle_start
= getCurUs();
2700 while (atomic_read(&(pkt_dev
->skb
->users
)) != 1) {
2701 if (signal_pending(current
)) {
2706 pkt_dev
->idle_acc
+= getCurUs() - idle_start
;
2709 /* Done with this */
2710 pktgen_stop_device(pkt_dev
);
2716 * Main loop of the thread goes here
2719 static void pktgen_thread_worker(struct pktgen_thread
*t
)
2722 struct pktgen_dev
*pkt_dev
= NULL
;
2725 u32 max_before_softirq
;
2726 u32 tx_since_softirq
= 0;
2728 daemonize("pktgen/%d", cpu
);
2730 /* Block all signals except SIGKILL, SIGSTOP and SIGTERM */
2732 spin_lock_irq(¤t
->sighand
->siglock
);
2733 tmpsig
= current
->blocked
;
2734 siginitsetinv(¤t
->blocked
,
2739 recalc_sigpending();
2740 spin_unlock_irq(¤t
->sighand
->siglock
);
2742 /* Migrate to the right CPU */
2743 set_cpus_allowed(current
, cpumask_of_cpu(cpu
));
2744 if (smp_processor_id() != cpu
)
2747 init_waitqueue_head(&t
->queue
);
2749 t
->control
&= ~(T_TERMINATE
);
2750 t
->control
&= ~(T_RUN
);
2751 t
->control
&= ~(T_STOP
);
2752 t
->control
&= ~(T_REMDEV
);
2754 t
->pid
= current
->pid
;
2756 PG_DEBUG(printk("pktgen: starting pktgen/%d: pid=%d\n", cpu
, current
->pid
));
2758 max_before_softirq
= t
->max_before_softirq
;
2760 __set_current_state(TASK_INTERRUPTIBLE
);
2765 __set_current_state(TASK_RUNNING
);
2768 * Get next dev to xmit -- if any.
2771 pkt_dev
= next_to_run(t
);
2775 pktgen_xmit(pkt_dev
);
2778 * We like to stay RUNNING but must also give
2779 * others fair share.
2782 tx_since_softirq
+= pkt_dev
->last_ok
;
2784 if (tx_since_softirq
> max_before_softirq
) {
2785 if (local_softirq_pending())
2787 tx_since_softirq
= 0;
2790 prepare_to_wait(&(t
->queue
), &wait
, TASK_INTERRUPTIBLE
);
2791 schedule_timeout(HZ
/10);
2792 finish_wait(&(t
->queue
), &wait
);
2796 * Back from sleep, either due to the timeout or signal.
2797 * We check if we have any "posted" work for us.
2800 if (t
->control
& T_TERMINATE
|| signal_pending(current
))
2801 /* we received a request to terminate ourself */
2805 if(t
->control
& T_STOP
) {
2807 t
->control
&= ~(T_STOP
);
2810 if(t
->control
& T_RUN
) {
2812 t
->control
&= ~(T_RUN
);
2815 if(t
->control
& T_REMDEV
) {
2816 pktgen_rem_all_ifs(t
);
2817 t
->control
&= ~(T_REMDEV
);
2824 PG_DEBUG(printk("pktgen: %s stopping all device\n", t
->name
));
2827 PG_DEBUG(printk("pktgen: %s removing all device\n", t
->name
));
2828 pktgen_rem_all_ifs(t
);
2830 PG_DEBUG(printk("pktgen: %s removing thread.\n", t
->name
));
2831 pktgen_rem_thread(t
);
2834 static struct pktgen_dev
*pktgen_find_dev(struct pktgen_thread
*t
, const char* ifname
)
2836 struct pktgen_dev
*pkt_dev
= NULL
;
2839 for(pkt_dev
=t
->if_list
; pkt_dev
; pkt_dev
= pkt_dev
->next
) {
2840 if (strcmp(pkt_dev
->ifname
, ifname
) == 0) {
2846 PG_DEBUG(printk("pktgen: find_dev(%s) returning %p\n", ifname
,pkt_dev
));
2851 * Adds a dev at front of if_list.
2854 static int add_dev_to_thread(struct pktgen_thread
*t
, struct pktgen_dev
*pkt_dev
)
2860 if (pkt_dev
->pg_thread
) {
2861 printk("pktgen: ERROR: already assigned to a thread.\n");
2865 pkt_dev
->next
=t
->if_list
; t
->if_list
=pkt_dev
;
2866 pkt_dev
->pg_thread
= t
;
2867 pkt_dev
->running
= 0;
2874 /* Called under thread lock */
2876 static int pktgen_add_device(struct pktgen_thread
*t
, const char* ifname
)
2878 struct pktgen_dev
*pkt_dev
;
2880 /* We don't allow a device to be on several threads */
2882 if( (pkt_dev
= __pktgen_NN_threads(ifname
, FIND
)) == NULL
) {
2884 pkt_dev
= kmalloc(sizeof(struct pktgen_dev
), GFP_KERNEL
);
2888 memset(pkt_dev
, 0, sizeof(struct pktgen_dev
));
2890 pkt_dev
->flows
= vmalloc(MAX_CFLOWS
*sizeof(struct flow_state
));
2891 if (pkt_dev
->flows
== NULL
) {
2895 memset(pkt_dev
->flows
, 0, MAX_CFLOWS
*sizeof(struct flow_state
));
2897 pkt_dev
->min_pkt_size
= ETH_ZLEN
;
2898 pkt_dev
->max_pkt_size
= ETH_ZLEN
;
2899 pkt_dev
->nfrags
= 0;
2900 pkt_dev
->clone_skb
= pg_clone_skb_d
;
2901 pkt_dev
->delay_us
= pg_delay_d
/ 1000;
2902 pkt_dev
->delay_ns
= pg_delay_d
% 1000;
2903 pkt_dev
->count
= pg_count_d
;
2905 pkt_dev
->udp_src_min
= 9; /* sink port */
2906 pkt_dev
->udp_src_max
= 9;
2907 pkt_dev
->udp_dst_min
= 9;
2908 pkt_dev
->udp_dst_max
= 9;
2910 strncpy(pkt_dev
->ifname
, ifname
, 31);
2911 sprintf(pkt_dev
->fname
, "net/%s/%s", PG_PROC_DIR
, ifname
);
2913 if (! pktgen_setup_dev(pkt_dev
)) {
2914 printk("pktgen: ERROR: pktgen_setup_dev failed.\n");
2916 vfree(pkt_dev
->flows
);
2921 pkt_dev
->proc_ent
= create_proc_entry(pkt_dev
->fname
, 0600, NULL
);
2922 if (!pkt_dev
->proc_ent
) {
2923 printk("pktgen: cannot create %s procfs entry.\n", pkt_dev
->fname
);
2925 vfree(pkt_dev
->flows
);
2929 pkt_dev
->proc_ent
->read_proc
= proc_if_read
;
2930 pkt_dev
->proc_ent
->write_proc
= proc_if_write
;
2931 pkt_dev
->proc_ent
->data
= (void*)(pkt_dev
);
2932 pkt_dev
->proc_ent
->owner
= THIS_MODULE
;
2934 return add_dev_to_thread(t
, pkt_dev
);
2937 printk("pktgen: ERROR: interface already used.\n");
2942 static struct pktgen_thread
*pktgen_find_thread(const char* name
)
2944 struct pktgen_thread
*t
= NULL
;
2950 if (strcmp(t
->name
, name
) == 0)
2959 static int pktgen_create_thread(const char* name
, int cpu
)
2961 struct pktgen_thread
*t
= NULL
;
2963 if (strlen(name
) > 31) {
2964 printk("pktgen: ERROR: Thread name cannot be more than 31 characters.\n");
2968 if (pktgen_find_thread(name
)) {
2969 printk("pktgen: ERROR: thread: %s already exists\n", name
);
2973 t
= (struct pktgen_thread
*)(kmalloc(sizeof(struct pktgen_thread
), GFP_KERNEL
));
2975 printk("pktgen: ERROR: out of memory, can't create new thread.\n");
2979 memset(t
, 0, sizeof(struct pktgen_thread
));
2980 strcpy(t
->name
, name
);
2981 spin_lock_init(&t
->if_lock
);
2984 sprintf(t
->fname
, "net/%s/%s", PG_PROC_DIR
, t
->name
);
2985 t
->proc_ent
= create_proc_entry(t
->fname
, 0600, NULL
);
2987 printk("pktgen: cannot create %s procfs entry.\n", t
->fname
);
2991 t
->proc_ent
->read_proc
= proc_thread_read
;
2992 t
->proc_ent
->write_proc
= proc_thread_write
;
2993 t
->proc_ent
->data
= (void*)(t
);
2994 t
->proc_ent
->owner
= THIS_MODULE
;
2996 t
->next
= pktgen_threads
;
2999 if (kernel_thread((void *) pktgen_thread_worker
, (void *) t
,
3000 CLONE_FS
| CLONE_FILES
| CLONE_SIGHAND
) < 0)
3001 printk("pktgen: kernel_thread() failed for cpu %d\n", t
->cpu
);
3007 * Removes a device from the thread if_list.
3009 static void _rem_dev_from_if_list(struct pktgen_thread
*t
, struct pktgen_dev
*pkt_dev
)
3011 struct pktgen_dev
*i
, *prev
= NULL
;
3017 if(prev
) prev
->next
= i
->next
;
3018 else t
->if_list
= NULL
;
3026 static int pktgen_remove_device(struct pktgen_thread
*t
, struct pktgen_dev
*pkt_dev
)
3029 PG_DEBUG(printk("pktgen: remove_device pkt_dev=%p\n", pkt_dev
));
3031 if (pkt_dev
->running
) {
3032 printk("pktgen:WARNING: trying to remove a running interface, stopping it now.\n");
3033 pktgen_stop_device(pkt_dev
);
3036 /* Dis-associate from the interface */
3038 if (pkt_dev
->odev
) {
3039 dev_put(pkt_dev
->odev
);
3040 pkt_dev
->odev
= NULL
;
3043 /* And update the thread if_list */
3045 _rem_dev_from_if_list(t
, pkt_dev
);
3047 /* Clean up proc file system */
3049 if (strlen(pkt_dev
->fname
))
3050 remove_proc_entry(pkt_dev
->fname
, NULL
);
3053 vfree(pkt_dev
->flows
);
3058 static int __init
pg_init(void)
3063 module_fname
[0] = 0;
3067 sprintf(module_fname
, "net/%s/pgctrl", PG_PROC_DIR
);
3068 module_proc_ent
= create_proc_entry(module_fname
, 0600, NULL
);
3069 if (!module_proc_ent
) {
3070 printk("pktgen: ERROR: cannot create %s procfs entry.\n", module_fname
);
3074 module_proc_ent
->proc_fops
= &pktgen_fops
;
3075 module_proc_ent
->data
= NULL
;
3077 /* Register us to receive netdevice events */
3078 register_netdevice_notifier(&pktgen_notifier_block
);
3080 for (cpu
= 0; cpu
< NR_CPUS
; cpu
++) {
3083 if (!cpu_online(cpu
))
3086 sprintf(buf
, "kpktgend_%i", cpu
);
3087 pktgen_create_thread(buf
, cpu
);
3092 static void __exit
pg_cleanup(void)
3094 wait_queue_head_t queue
;
3095 init_waitqueue_head(&queue
);
3097 /* Stop all interfaces & threads */
3099 while (pktgen_threads
) {
3100 struct pktgen_thread
*t
= pktgen_threads
;
3101 pktgen_threads
->control
|= (T_TERMINATE
);
3103 wait_event_interruptible_timeout(queue
, (t
!= pktgen_threads
), HZ
);
3106 /* Un-register us from receiving netdevice events */
3107 unregister_netdevice_notifier(&pktgen_notifier_block
);
3109 /* Clean up proc file system */
3111 remove_proc_entry(module_fname
, NULL
);
3117 module_init(pg_init
);
3118 module_exit(pg_cleanup
);
3120 MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se");
3121 MODULE_DESCRIPTION("Packet Generator tool");
3122 MODULE_LICENSE("GPL");
3123 module_param(pg_count_d
, int, 0);
3124 module_param(pg_delay_d
, int, 0);
3125 module_param(pg_clone_skb_d
, int, 0);
3126 module_param(debug
, int, 0);