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 practical 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 * Randy 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/unistd.h>
120 #include <linux/string.h>
121 #include <linux/ptrace.h>
122 #include <linux/errno.h>
123 #include <linux/ioport.h>
124 #include <linux/interrupt.h>
125 #include <linux/capability.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/seq_file.h>
141 #include <linux/wait.h>
142 #include <linux/completion.h>
143 #include <linux/etherdevice.h>
144 #include <net/checksum.h>
145 #include <net/ipv6.h>
146 #include <net/addrconf.h>
147 #include <asm/byteorder.h>
148 #include <linux/rcupdate.h>
149 #include <asm/bitops.h>
152 #include <asm/uaccess.h>
153 #include <asm/div64.h> /* do_div */
154 #include <asm/timex.h>
157 #define VERSION "pktgen v2.63: Packet Generator for packet performance testing.\n"
159 /* #define PG_DEBUG(a) a */
162 /* The buckets are exponential in 'width' */
163 #define LAT_BUCKETS_MAX 32
164 #define IP_NAME_SZ 32
166 /* Device flag bits */
167 #define F_IPSRC_RND (1<<0) /* IP-Src Random */
168 #define F_IPDST_RND (1<<1) /* IP-Dst Random */
169 #define F_UDPSRC_RND (1<<2) /* UDP-Src Random */
170 #define F_UDPDST_RND (1<<3) /* UDP-Dst Random */
171 #define F_MACSRC_RND (1<<4) /* MAC-Src Random */
172 #define F_MACDST_RND (1<<5) /* MAC-Dst Random */
173 #define F_TXSIZE_RND (1<<6) /* Transmit size is random */
174 #define F_IPV6 (1<<7) /* Interface in IPV6 Mode */
176 /* Thread control flag bits */
177 #define T_TERMINATE (1<<0)
178 #define T_STOP (1<<1) /* Stop run */
179 #define T_RUN (1<<2) /* Start run */
180 #define T_REMDEV (1<<3) /* Remove all devs */
183 #define thread_lock() down(&pktgen_sem)
184 #define thread_unlock() up(&pktgen_sem)
186 /* If lock -- can be removed after some work */
187 #define if_lock(t) spin_lock(&(t->if_lock));
188 #define if_unlock(t) spin_unlock(&(t->if_lock));
190 /* Used to help with determining the pkts on receive */
191 #define PKTGEN_MAGIC 0xbe9be955
192 #define PG_PROC_DIR "pktgen"
193 #define PGCTRL "pgctrl"
194 static struct proc_dir_entry
*pg_proc_dir
= NULL
;
196 #define MAX_CFLOWS 65536
198 struct pktgen_thread_info
{
199 struct pktgen_thread
*t
;
200 struct completion
*c
;
212 * Try to keep frequent/infrequent used vars. separated.
215 char ifname
[IFNAMSIZ
];
218 struct pktgen_thread
* pg_thread
; /* the owner */
219 struct pktgen_dev
*next
; /* Used for chaining in the thread's run-queue */
221 int running
; /* if this changes to false, the test will stop */
223 /* If min != max, then we will either do a linear iteration, or
224 * we will do a random selection from within the range.
228 int min_pkt_size
; /* = ETH_ZLEN; */
229 int max_pkt_size
; /* = ETH_ZLEN; */
231 __u32 delay_us
; /* Default delay */
233 __u64 count
; /* Default No packets to send */
234 __u64 sofar
; /* How many pkts we've sent so far */
235 __u64 tx_bytes
; /* How many bytes we've transmitted */
236 __u64 errors
; /* Errors when trying to transmit, pkts will be re-sent */
238 /* runtime counters relating to clone_skb */
239 __u64 next_tx_us
; /* timestamp of when to tx next */
242 __u64 allocated_skbs
;
244 int last_ok
; /* Was last skb sent?
245 * Or a failed transmit of some sort? This will keep
246 * sequence numbers in order, for example.
248 __u64 started_at
; /* micro-seconds */
249 __u64 stopped_at
; /* micro-seconds */
250 __u64 idle_acc
; /* micro-seconds */
253 int clone_skb
; /* Use multiple SKBs during packet gen. If this number
254 * is greater than 1, then that many copies of the same
255 * packet will be sent before a new packet is allocated.
256 * For instance, if you want to send 1024 identical packets
257 * before creating a new packet, set clone_skb to 1024.
260 char dst_min
[IP_NAME_SZ
]; /* IP, ie 1.2.3.4 */
261 char dst_max
[IP_NAME_SZ
]; /* IP, ie 1.2.3.4 */
262 char src_min
[IP_NAME_SZ
]; /* IP, ie 1.2.3.4 */
263 char src_max
[IP_NAME_SZ
]; /* IP, ie 1.2.3.4 */
265 struct in6_addr in6_saddr
;
266 struct in6_addr in6_daddr
;
267 struct in6_addr cur_in6_daddr
;
268 struct in6_addr cur_in6_saddr
;
270 struct in6_addr min_in6_daddr
;
271 struct in6_addr max_in6_daddr
;
272 struct in6_addr min_in6_saddr
;
273 struct in6_addr max_in6_saddr
;
275 /* If we're doing ranges, random or incremental, then this
276 * defines the min/max for those ranges.
278 __u32 saddr_min
; /* inclusive, source IP address */
279 __u32 saddr_max
; /* exclusive, source IP address */
280 __u32 daddr_min
; /* inclusive, dest IP address */
281 __u32 daddr_max
; /* exclusive, dest IP address */
283 __u16 udp_src_min
; /* inclusive, source UDP port */
284 __u16 udp_src_max
; /* exclusive, source UDP port */
285 __u16 udp_dst_min
; /* inclusive, dest UDP port */
286 __u16 udp_dst_max
; /* exclusive, dest UDP port */
288 __u32 src_mac_count
; /* How many MACs to iterate through */
289 __u32 dst_mac_count
; /* How many MACs to iterate through */
291 unsigned char dst_mac
[ETH_ALEN
];
292 unsigned char src_mac
[ETH_ALEN
];
294 __u32 cur_dst_mac_offset
;
295 __u32 cur_src_mac_offset
;
304 0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
306 We fill in SRC address later
307 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
311 __u16 pad
; /* pad out the hh struct to an even 16 bytes */
313 struct sk_buff
* skb
; /* skb we are to transmit next, mainly used for when we
314 * are transmitting the same one multiple times
316 struct net_device
* odev
; /* The out-going device. Note that the device should
317 * have it's pg_info pointer pointing back to this
318 * device. This will be set when the user specifies
319 * the out-going device name (not when the inject is
320 * started as it used to do.)
322 struct flow_state
*flows
;
323 unsigned cflows
; /* Concurrent flows (config) */
324 unsigned lflow
; /* Flow length (config) */
325 unsigned nflows
; /* accumulated flows (stats) */
335 struct pktgen_thread
{
337 struct pktgen_dev
*if_list
; /* All device here */
338 struct pktgen_thread
* next
;
341 u32 max_before_softirq
; /* We'll call do_softirq to prevent starvation. */
343 /* Field for thread to receive "posted" events terminate, stop ifs etc.*/
349 wait_queue_head_t queue
;
355 /* This code works around the fact that do_div cannot handle two 64-bit
356 numbers, and regular 64-bit division doesn't work on x86 kernels.
362 /* This was emailed to LMKL by: Chris Caputo <ccaputo@alt.net>
363 * Function copied/adapted/optimized from:
365 * nemesis.sourceforge.net/browse/lib/static/intmath/ix86/intmath.c.html
367 * Copyright 1994, University of Cambridge Computer Laboratory
368 * All Rights Reserved.
371 static inline s64
divremdi3(s64 x
, s64 y
, int type
)
373 u64 a
= (x
< 0) ? -x
: x
;
374 u64 b
= (y
< 0) ? -y
: y
;
394 if (PG_DIV
== type
) {
395 return (((x
^ y
) & (1ll<<63)) == 0) ? res
: -(s64
)res
;
398 return ((x
& (1ll<<63)) == 0) ? a
: -(s64
)a
;
402 /* End of hacks to deal with 64-bit math on x86 */
404 /** Convert to milliseconds */
405 static inline __u64
tv_to_ms(const struct timeval
* tv
)
407 __u64 ms
= tv
->tv_usec
/ 1000;
408 ms
+= (__u64
)tv
->tv_sec
* (__u64
)1000;
413 /** Convert to micro-seconds */
414 static inline __u64
tv_to_us(const struct timeval
* tv
)
416 __u64 us
= tv
->tv_usec
;
417 us
+= (__u64
)tv
->tv_sec
* (__u64
)1000000;
421 static inline __u64
pg_div(__u64 n
, __u32 base
) {
424 /* printk("pktgen: pg_div, n: %llu base: %d rv: %llu\n",
429 static inline __u64
pg_div64(__u64 n
, __u64 base
)
433 * How do we know if the architecture we are running on
434 * supports division with 64 bit base?
437 #if defined(__sparc_v9__) || defined(__powerpc64__) || defined(__alpha__) || defined(__x86_64__) || defined(__ia64__)
441 tmp
= divremdi3(n
, base
, PG_DIV
);
446 static inline u32
pktgen_random(void)
450 get_random_bytes(&n
, 4);
457 static inline __u64
getCurMs(void)
460 do_gettimeofday(&tv
);
461 return tv_to_ms(&tv
);
464 static inline __u64
getCurUs(void)
467 do_gettimeofday(&tv
);
468 return tv_to_us(&tv
);
471 static inline __u64
tv_diff(const struct timeval
* a
, const struct timeval
* b
)
473 return tv_to_us(a
) - tv_to_us(b
);
477 /* old include end */
479 static char version
[] __initdata
= VERSION
;
481 static int pktgen_remove_device(struct pktgen_thread
* t
, struct pktgen_dev
*i
);
482 static int pktgen_add_device(struct pktgen_thread
* t
, const char* ifname
);
483 static struct pktgen_dev
*pktgen_find_dev(struct pktgen_thread
* t
, const char* ifname
);
484 static int pktgen_device_event(struct notifier_block
*, unsigned long, void *);
485 static void pktgen_run_all_threads(void);
486 static void pktgen_stop_all_threads_ifs(void);
487 static int pktgen_stop_device(struct pktgen_dev
*pkt_dev
);
488 static void pktgen_stop(struct pktgen_thread
* t
);
489 static void pktgen_clear_counters(struct pktgen_dev
*pkt_dev
);
490 static struct pktgen_dev
*pktgen_NN_threads(const char* dev_name
, int remove
);
491 static unsigned int scan_ip6(const char *s
,char ip
[16]);
492 static unsigned int fmt_ip6(char *s
,const char ip
[16]);
494 /* Module parameters, defaults. */
495 static int pg_count_d
= 1000; /* 1000 pkts by default */
496 static int pg_delay_d
;
497 static int pg_clone_skb_d
;
500 static DECLARE_MUTEX(pktgen_sem
);
501 static struct pktgen_thread
*pktgen_threads
= NULL
;
503 static struct notifier_block pktgen_notifier_block
= {
504 .notifier_call
= pktgen_device_event
,
508 * /proc handling functions
512 static int pgctrl_show(struct seq_file
*seq
, void *v
)
514 seq_puts(seq
, VERSION
);
518 static ssize_t
pgctrl_write(struct file
* file
,const char __user
* buf
,
519 size_t count
, loff_t
*ppos
)
524 if (!capable(CAP_NET_ADMIN
)){
529 if (count
> sizeof(data
))
530 count
= sizeof(data
);
532 if (copy_from_user(data
, buf
, count
)) {
536 data
[count
-1] = 0; /* Make string */
538 if (!strcmp(data
, "stop"))
539 pktgen_stop_all_threads_ifs();
541 else if (!strcmp(data
, "start"))
542 pktgen_run_all_threads();
545 printk("pktgen: Unknown command: %s\n", data
);
553 static int pgctrl_open(struct inode
*inode
, struct file
*file
)
555 return single_open(file
, pgctrl_show
, PDE(inode
)->data
);
558 static struct file_operations pktgen_fops
= {
559 .owner
= THIS_MODULE
,
563 .write
= pgctrl_write
,
564 .release
= single_release
,
567 static int pktgen_if_show(struct seq_file
*seq
, void *v
)
570 struct pktgen_dev
*pkt_dev
= seq
->private;
573 __u64 now
= getCurUs();
575 seq_printf(seq
, "Params: count %llu min_pkt_size: %u max_pkt_size: %u\n",
576 (unsigned long long) pkt_dev
->count
,
577 pkt_dev
->min_pkt_size
, pkt_dev
->max_pkt_size
);
579 seq_printf(seq
, " frags: %d delay: %u clone_skb: %d ifname: %s\n",
580 pkt_dev
->nfrags
, 1000*pkt_dev
->delay_us
+pkt_dev
->delay_ns
, pkt_dev
->clone_skb
, pkt_dev
->ifname
);
582 seq_printf(seq
, " flows: %u flowlen: %u\n", pkt_dev
->cflows
, pkt_dev
->lflow
);
585 if(pkt_dev
->flags
& F_IPV6
) {
586 char b1
[128], b2
[128], b3
[128];
587 fmt_ip6(b1
, pkt_dev
->in6_saddr
.s6_addr
);
588 fmt_ip6(b2
, pkt_dev
->min_in6_saddr
.s6_addr
);
589 fmt_ip6(b3
, pkt_dev
->max_in6_saddr
.s6_addr
);
590 seq_printf(seq
, " saddr: %s min_saddr: %s max_saddr: %s\n", b1
, b2
, b3
);
592 fmt_ip6(b1
, pkt_dev
->in6_daddr
.s6_addr
);
593 fmt_ip6(b2
, pkt_dev
->min_in6_daddr
.s6_addr
);
594 fmt_ip6(b3
, pkt_dev
->max_in6_daddr
.s6_addr
);
595 seq_printf(seq
, " daddr: %s min_daddr: %s max_daddr: %s\n", b1
, b2
, b3
);
599 seq_printf(seq
," dst_min: %s dst_max: %s\n src_min: %s src_max: %s\n",
600 pkt_dev
->dst_min
, pkt_dev
->dst_max
, pkt_dev
->src_min
, pkt_dev
->src_max
);
602 seq_puts(seq
, " src_mac: ");
604 if (is_zero_ether_addr(pkt_dev
->src_mac
))
605 for (i
= 0; i
< 6; i
++)
606 seq_printf(seq
, "%02X%s", pkt_dev
->odev
->dev_addr
[i
], i
== 5 ? " " : ":");
608 for (i
= 0; i
< 6; i
++)
609 seq_printf(seq
, "%02X%s", pkt_dev
->src_mac
[i
], i
== 5 ? " " : ":");
611 seq_printf(seq
, "dst_mac: ");
612 for (i
= 0; i
< 6; i
++)
613 seq_printf(seq
, "%02X%s", pkt_dev
->dst_mac
[i
], i
== 5 ? "\n" : ":");
615 seq_printf(seq
, " udp_src_min: %d udp_src_max: %d udp_dst_min: %d udp_dst_max: %d\n",
616 pkt_dev
->udp_src_min
, pkt_dev
->udp_src_max
, pkt_dev
->udp_dst_min
,
617 pkt_dev
->udp_dst_max
);
619 seq_printf(seq
, " src_mac_count: %d dst_mac_count: %d \n Flags: ",
620 pkt_dev
->src_mac_count
, pkt_dev
->dst_mac_count
);
623 if (pkt_dev
->flags
& F_IPV6
)
624 seq_printf(seq
, "IPV6 ");
626 if (pkt_dev
->flags
& F_IPSRC_RND
)
627 seq_printf(seq
, "IPSRC_RND ");
629 if (pkt_dev
->flags
& F_IPDST_RND
)
630 seq_printf(seq
, "IPDST_RND ");
632 if (pkt_dev
->flags
& F_TXSIZE_RND
)
633 seq_printf(seq
, "TXSIZE_RND ");
635 if (pkt_dev
->flags
& F_UDPSRC_RND
)
636 seq_printf(seq
, "UDPSRC_RND ");
638 if (pkt_dev
->flags
& F_UDPDST_RND
)
639 seq_printf(seq
, "UDPDST_RND ");
641 if (pkt_dev
->flags
& F_MACSRC_RND
)
642 seq_printf(seq
, "MACSRC_RND ");
644 if (pkt_dev
->flags
& F_MACDST_RND
)
645 seq_printf(seq
, "MACDST_RND ");
650 sa
= pkt_dev
->started_at
;
651 stopped
= pkt_dev
->stopped_at
;
652 if (pkt_dev
->running
)
653 stopped
= now
; /* not really stopped, more like last-running-at */
655 seq_printf(seq
, "Current:\n pkts-sofar: %llu errors: %llu\n started: %lluus stopped: %lluus idle: %lluus\n",
656 (unsigned long long) pkt_dev
->sofar
,
657 (unsigned long long) pkt_dev
->errors
,
658 (unsigned long long) sa
,
659 (unsigned long long) stopped
,
660 (unsigned long long) pkt_dev
->idle_acc
);
662 seq_printf(seq
, " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n",
663 pkt_dev
->seq_num
, pkt_dev
->cur_dst_mac_offset
,
664 pkt_dev
->cur_src_mac_offset
);
666 if(pkt_dev
->flags
& F_IPV6
) {
667 char b1
[128], b2
[128];
668 fmt_ip6(b1
, pkt_dev
->cur_in6_daddr
.s6_addr
);
669 fmt_ip6(b2
, pkt_dev
->cur_in6_saddr
.s6_addr
);
670 seq_printf(seq
, " cur_saddr: %s cur_daddr: %s\n", b2
, b1
);
673 seq_printf(seq
, " cur_saddr: 0x%x cur_daddr: 0x%x\n",
674 pkt_dev
->cur_saddr
, pkt_dev
->cur_daddr
);
677 seq_printf(seq
, " cur_udp_dst: %d cur_udp_src: %d\n",
678 pkt_dev
->cur_udp_dst
, pkt_dev
->cur_udp_src
);
680 seq_printf(seq
, " flows: %u\n", pkt_dev
->nflows
);
682 if (pkt_dev
->result
[0])
683 seq_printf(seq
, "Result: %s\n", pkt_dev
->result
);
685 seq_printf(seq
, "Result: Idle\n");
691 static int count_trail_chars(const char __user
*user_buffer
, unsigned int maxlen
)
695 for (i
= 0; i
< maxlen
; i
++) {
697 if (get_user(c
, &user_buffer
[i
]))
715 static unsigned long num_arg(const char __user
*user_buffer
, unsigned long maxlen
,
721 for(; i
< maxlen
; i
++) {
723 if (get_user(c
, &user_buffer
[i
]))
725 if ((c
>= '0') && (c
<= '9')) {
734 static int strn_len(const char __user
*user_buffer
, unsigned int maxlen
)
738 for(; i
< maxlen
; i
++) {
740 if (get_user(c
, &user_buffer
[i
]))
759 static ssize_t
pktgen_if_write(struct file
*file
, const char __user
*user_buffer
,
760 size_t count
, loff_t
*offset
)
762 struct seq_file
*seq
= (struct seq_file
*) file
->private_data
;
763 struct pktgen_dev
*pkt_dev
= seq
->private;
765 char name
[16], valstr
[32];
766 unsigned long value
= 0;
767 char* pg_result
= NULL
;
771 pg_result
= &(pkt_dev
->result
[0]);
774 printk("pktgen: wrong command format\n");
779 tmp
= count_trail_chars(&user_buffer
[i
], max
);
781 printk("pktgen: illegal format\n");
786 /* Read variable name */
788 len
= strn_len(&user_buffer
[i
], sizeof(name
) - 1);
789 if (len
< 0) { return len
; }
790 memset(name
, 0, sizeof(name
));
791 if (copy_from_user(name
, &user_buffer
[i
], len
) )
796 len
= count_trail_chars(&user_buffer
[i
], max
);
804 if (copy_from_user(tb
, user_buffer
, count
))
807 printk("pktgen: %s,%lu buffer -:%s:-\n", name
,
808 (unsigned long) count
, tb
);
811 if (!strcmp(name
, "min_pkt_size")) {
812 len
= num_arg(&user_buffer
[i
], 10, &value
);
813 if (len
< 0) { return len
; }
817 if (value
!= pkt_dev
->min_pkt_size
) {
818 pkt_dev
->min_pkt_size
= value
;
819 pkt_dev
->cur_pkt_size
= value
;
821 sprintf(pg_result
, "OK: min_pkt_size=%u", pkt_dev
->min_pkt_size
);
825 if (!strcmp(name
, "max_pkt_size")) {
826 len
= num_arg(&user_buffer
[i
], 10, &value
);
827 if (len
< 0) { return len
; }
831 if (value
!= pkt_dev
->max_pkt_size
) {
832 pkt_dev
->max_pkt_size
= value
;
833 pkt_dev
->cur_pkt_size
= value
;
835 sprintf(pg_result
, "OK: max_pkt_size=%u", pkt_dev
->max_pkt_size
);
839 /* Shortcut for min = max */
841 if (!strcmp(name
, "pkt_size")) {
842 len
= num_arg(&user_buffer
[i
], 10, &value
);
843 if (len
< 0) { return len
; }
847 if (value
!= pkt_dev
->min_pkt_size
) {
848 pkt_dev
->min_pkt_size
= value
;
849 pkt_dev
->max_pkt_size
= value
;
850 pkt_dev
->cur_pkt_size
= value
;
852 sprintf(pg_result
, "OK: pkt_size=%u", pkt_dev
->min_pkt_size
);
856 if (!strcmp(name
, "debug")) {
857 len
= num_arg(&user_buffer
[i
], 10, &value
);
858 if (len
< 0) { return len
; }
861 sprintf(pg_result
, "OK: debug=%u", debug
);
865 if (!strcmp(name
, "frags")) {
866 len
= num_arg(&user_buffer
[i
], 10, &value
);
867 if (len
< 0) { return len
; }
869 pkt_dev
->nfrags
= value
;
870 sprintf(pg_result
, "OK: frags=%u", pkt_dev
->nfrags
);
873 if (!strcmp(name
, "delay")) {
874 len
= num_arg(&user_buffer
[i
], 10, &value
);
875 if (len
< 0) { return len
; }
877 if (value
== 0x7FFFFFFF) {
878 pkt_dev
->delay_us
= 0x7FFFFFFF;
879 pkt_dev
->delay_ns
= 0;
881 pkt_dev
->delay_us
= value
/ 1000;
882 pkt_dev
->delay_ns
= value
% 1000;
884 sprintf(pg_result
, "OK: delay=%u", 1000*pkt_dev
->delay_us
+pkt_dev
->delay_ns
);
887 if (!strcmp(name
, "udp_src_min")) {
888 len
= num_arg(&user_buffer
[i
], 10, &value
);
889 if (len
< 0) { return len
; }
891 if (value
!= pkt_dev
->udp_src_min
) {
892 pkt_dev
->udp_src_min
= value
;
893 pkt_dev
->cur_udp_src
= value
;
895 sprintf(pg_result
, "OK: udp_src_min=%u", pkt_dev
->udp_src_min
);
898 if (!strcmp(name
, "udp_dst_min")) {
899 len
= num_arg(&user_buffer
[i
], 10, &value
);
900 if (len
< 0) { return len
; }
902 if (value
!= pkt_dev
->udp_dst_min
) {
903 pkt_dev
->udp_dst_min
= value
;
904 pkt_dev
->cur_udp_dst
= value
;
906 sprintf(pg_result
, "OK: udp_dst_min=%u", pkt_dev
->udp_dst_min
);
909 if (!strcmp(name
, "udp_src_max")) {
910 len
= num_arg(&user_buffer
[i
], 10, &value
);
911 if (len
< 0) { return len
; }
913 if (value
!= pkt_dev
->udp_src_max
) {
914 pkt_dev
->udp_src_max
= value
;
915 pkt_dev
->cur_udp_src
= value
;
917 sprintf(pg_result
, "OK: udp_src_max=%u", pkt_dev
->udp_src_max
);
920 if (!strcmp(name
, "udp_dst_max")) {
921 len
= num_arg(&user_buffer
[i
], 10, &value
);
922 if (len
< 0) { return len
; }
924 if (value
!= pkt_dev
->udp_dst_max
) {
925 pkt_dev
->udp_dst_max
= value
;
926 pkt_dev
->cur_udp_dst
= value
;
928 sprintf(pg_result
, "OK: udp_dst_max=%u", pkt_dev
->udp_dst_max
);
931 if (!strcmp(name
, "clone_skb")) {
932 len
= num_arg(&user_buffer
[i
], 10, &value
);
933 if (len
< 0) { return len
; }
935 pkt_dev
->clone_skb
= value
;
937 sprintf(pg_result
, "OK: clone_skb=%d", pkt_dev
->clone_skb
);
940 if (!strcmp(name
, "count")) {
941 len
= num_arg(&user_buffer
[i
], 10, &value
);
942 if (len
< 0) { return len
; }
944 pkt_dev
->count
= value
;
945 sprintf(pg_result
, "OK: count=%llu",
946 (unsigned long long) pkt_dev
->count
);
949 if (!strcmp(name
, "src_mac_count")) {
950 len
= num_arg(&user_buffer
[i
], 10, &value
);
951 if (len
< 0) { return len
; }
953 if (pkt_dev
->src_mac_count
!= value
) {
954 pkt_dev
->src_mac_count
= value
;
955 pkt_dev
->cur_src_mac_offset
= 0;
957 sprintf(pg_result
, "OK: src_mac_count=%d", pkt_dev
->src_mac_count
);
960 if (!strcmp(name
, "dst_mac_count")) {
961 len
= num_arg(&user_buffer
[i
], 10, &value
);
962 if (len
< 0) { return len
; }
964 if (pkt_dev
->dst_mac_count
!= value
) {
965 pkt_dev
->dst_mac_count
= value
;
966 pkt_dev
->cur_dst_mac_offset
= 0;
968 sprintf(pg_result
, "OK: dst_mac_count=%d", pkt_dev
->dst_mac_count
);
971 if (!strcmp(name
, "flag")) {
974 len
= strn_len(&user_buffer
[i
], sizeof(f
) - 1);
975 if (len
< 0) { return len
; }
976 if (copy_from_user(f
, &user_buffer
[i
], len
))
979 if (strcmp(f
, "IPSRC_RND") == 0)
980 pkt_dev
->flags
|= F_IPSRC_RND
;
982 else if (strcmp(f
, "!IPSRC_RND") == 0)
983 pkt_dev
->flags
&= ~F_IPSRC_RND
;
985 else if (strcmp(f
, "TXSIZE_RND") == 0)
986 pkt_dev
->flags
|= F_TXSIZE_RND
;
988 else if (strcmp(f
, "!TXSIZE_RND") == 0)
989 pkt_dev
->flags
&= ~F_TXSIZE_RND
;
991 else if (strcmp(f
, "IPDST_RND") == 0)
992 pkt_dev
->flags
|= F_IPDST_RND
;
994 else if (strcmp(f
, "!IPDST_RND") == 0)
995 pkt_dev
->flags
&= ~F_IPDST_RND
;
997 else if (strcmp(f
, "UDPSRC_RND") == 0)
998 pkt_dev
->flags
|= F_UDPSRC_RND
;
1000 else if (strcmp(f
, "!UDPSRC_RND") == 0)
1001 pkt_dev
->flags
&= ~F_UDPSRC_RND
;
1003 else if (strcmp(f
, "UDPDST_RND") == 0)
1004 pkt_dev
->flags
|= F_UDPDST_RND
;
1006 else if (strcmp(f
, "!UDPDST_RND") == 0)
1007 pkt_dev
->flags
&= ~F_UDPDST_RND
;
1009 else if (strcmp(f
, "MACSRC_RND") == 0)
1010 pkt_dev
->flags
|= F_MACSRC_RND
;
1012 else if (strcmp(f
, "!MACSRC_RND") == 0)
1013 pkt_dev
->flags
&= ~F_MACSRC_RND
;
1015 else if (strcmp(f
, "MACDST_RND") == 0)
1016 pkt_dev
->flags
|= F_MACDST_RND
;
1018 else if (strcmp(f
, "!MACDST_RND") == 0)
1019 pkt_dev
->flags
&= ~F_MACDST_RND
;
1022 sprintf(pg_result
, "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
1024 "IPSRC_RND, IPDST_RND, TXSIZE_RND, UDPSRC_RND, UDPDST_RND, MACSRC_RND, MACDST_RND\n");
1027 sprintf(pg_result
, "OK: flags=0x%x", pkt_dev
->flags
);
1030 if (!strcmp(name
, "dst_min") || !strcmp(name
, "dst")) {
1031 len
= strn_len(&user_buffer
[i
], sizeof(pkt_dev
->dst_min
) - 1);
1032 if (len
< 0) { return len
; }
1034 if (copy_from_user(buf
, &user_buffer
[i
], len
))
1037 if (strcmp(buf
, pkt_dev
->dst_min
) != 0) {
1038 memset(pkt_dev
->dst_min
, 0, sizeof(pkt_dev
->dst_min
));
1039 strncpy(pkt_dev
->dst_min
, buf
, len
);
1040 pkt_dev
->daddr_min
= in_aton(pkt_dev
->dst_min
);
1041 pkt_dev
->cur_daddr
= pkt_dev
->daddr_min
;
1044 printk("pktgen: dst_min set to: %s\n", pkt_dev
->dst_min
);
1046 sprintf(pg_result
, "OK: dst_min=%s", pkt_dev
->dst_min
);
1049 if (!strcmp(name
, "dst_max")) {
1050 len
= strn_len(&user_buffer
[i
], sizeof(pkt_dev
->dst_max
) - 1);
1051 if (len
< 0) { return len
; }
1053 if (copy_from_user(buf
, &user_buffer
[i
], len
))
1057 if (strcmp(buf
, pkt_dev
->dst_max
) != 0) {
1058 memset(pkt_dev
->dst_max
, 0, sizeof(pkt_dev
->dst_max
));
1059 strncpy(pkt_dev
->dst_max
, buf
, len
);
1060 pkt_dev
->daddr_max
= in_aton(pkt_dev
->dst_max
);
1061 pkt_dev
->cur_daddr
= pkt_dev
->daddr_max
;
1064 printk("pktgen: dst_max set to: %s\n", pkt_dev
->dst_max
);
1066 sprintf(pg_result
, "OK: dst_max=%s", pkt_dev
->dst_max
);
1069 if (!strcmp(name
, "dst6")) {
1070 len
= strn_len(&user_buffer
[i
], sizeof(buf
) - 1);
1071 if (len
< 0) return len
;
1073 pkt_dev
->flags
|= F_IPV6
;
1075 if (copy_from_user(buf
, &user_buffer
[i
], len
))
1079 scan_ip6(buf
, pkt_dev
->in6_daddr
.s6_addr
);
1080 fmt_ip6(buf
, pkt_dev
->in6_daddr
.s6_addr
);
1082 ipv6_addr_copy(&pkt_dev
->cur_in6_daddr
, &pkt_dev
->in6_daddr
);
1085 printk("pktgen: dst6 set to: %s\n", buf
);
1088 sprintf(pg_result
, "OK: dst6=%s", buf
);
1091 if (!strcmp(name
, "dst6_min")) {
1092 len
= strn_len(&user_buffer
[i
], sizeof(buf
) - 1);
1093 if (len
< 0) return len
;
1095 pkt_dev
->flags
|= F_IPV6
;
1097 if (copy_from_user(buf
, &user_buffer
[i
], len
))
1101 scan_ip6(buf
, pkt_dev
->min_in6_daddr
.s6_addr
);
1102 fmt_ip6(buf
, pkt_dev
->min_in6_daddr
.s6_addr
);
1104 ipv6_addr_copy(&pkt_dev
->cur_in6_daddr
, &pkt_dev
->min_in6_daddr
);
1106 printk("pktgen: dst6_min set to: %s\n", buf
);
1109 sprintf(pg_result
, "OK: dst6_min=%s", buf
);
1112 if (!strcmp(name
, "dst6_max")) {
1113 len
= strn_len(&user_buffer
[i
], sizeof(buf
) - 1);
1114 if (len
< 0) return len
;
1116 pkt_dev
->flags
|= F_IPV6
;
1118 if (copy_from_user(buf
, &user_buffer
[i
], len
))
1122 scan_ip6(buf
, pkt_dev
->max_in6_daddr
.s6_addr
);
1123 fmt_ip6(buf
, pkt_dev
->max_in6_daddr
.s6_addr
);
1126 printk("pktgen: dst6_max set to: %s\n", buf
);
1129 sprintf(pg_result
, "OK: dst6_max=%s", buf
);
1132 if (!strcmp(name
, "src6")) {
1133 len
= strn_len(&user_buffer
[i
], sizeof(buf
) - 1);
1134 if (len
< 0) return len
;
1136 pkt_dev
->flags
|= F_IPV6
;
1138 if (copy_from_user(buf
, &user_buffer
[i
], len
))
1142 scan_ip6(buf
, pkt_dev
->in6_saddr
.s6_addr
);
1143 fmt_ip6(buf
, pkt_dev
->in6_saddr
.s6_addr
);
1145 ipv6_addr_copy(&pkt_dev
->cur_in6_saddr
, &pkt_dev
->in6_saddr
);
1148 printk("pktgen: src6 set to: %s\n", buf
);
1151 sprintf(pg_result
, "OK: src6=%s", buf
);
1154 if (!strcmp(name
, "src_min")) {
1155 len
= strn_len(&user_buffer
[i
], sizeof(pkt_dev
->src_min
) - 1);
1156 if (len
< 0) { return len
; }
1157 if (copy_from_user(buf
, &user_buffer
[i
], len
))
1160 if (strcmp(buf
, pkt_dev
->src_min
) != 0) {
1161 memset(pkt_dev
->src_min
, 0, sizeof(pkt_dev
->src_min
));
1162 strncpy(pkt_dev
->src_min
, buf
, len
);
1163 pkt_dev
->saddr_min
= in_aton(pkt_dev
->src_min
);
1164 pkt_dev
->cur_saddr
= pkt_dev
->saddr_min
;
1167 printk("pktgen: src_min set to: %s\n", pkt_dev
->src_min
);
1169 sprintf(pg_result
, "OK: src_min=%s", pkt_dev
->src_min
);
1172 if (!strcmp(name
, "src_max")) {
1173 len
= strn_len(&user_buffer
[i
], sizeof(pkt_dev
->src_max
) - 1);
1174 if (len
< 0) { return len
; }
1175 if (copy_from_user(buf
, &user_buffer
[i
], len
))
1178 if (strcmp(buf
, pkt_dev
->src_max
) != 0) {
1179 memset(pkt_dev
->src_max
, 0, sizeof(pkt_dev
->src_max
));
1180 strncpy(pkt_dev
->src_max
, buf
, len
);
1181 pkt_dev
->saddr_max
= in_aton(pkt_dev
->src_max
);
1182 pkt_dev
->cur_saddr
= pkt_dev
->saddr_max
;
1185 printk("pktgen: src_max set to: %s\n", pkt_dev
->src_max
);
1187 sprintf(pg_result
, "OK: src_max=%s", pkt_dev
->src_max
);
1190 if (!strcmp(name
, "dst_mac")) {
1192 unsigned char old_dmac
[ETH_ALEN
];
1193 unsigned char *m
= pkt_dev
->dst_mac
;
1194 memcpy(old_dmac
, pkt_dev
->dst_mac
, ETH_ALEN
);
1196 len
= strn_len(&user_buffer
[i
], sizeof(valstr
) - 1);
1197 if (len
< 0) { return len
; }
1198 memset(valstr
, 0, sizeof(valstr
));
1199 if( copy_from_user(valstr
, &user_buffer
[i
], len
))
1203 for(*m
= 0;*v
&& m
< pkt_dev
->dst_mac
+ 6; v
++) {
1204 if (*v
>= '0' && *v
<= '9') {
1208 if (*v
>= 'A' && *v
<= 'F') {
1210 *m
+= *v
- 'A' + 10;
1212 if (*v
>= 'a' && *v
<= 'f') {
1214 *m
+= *v
- 'a' + 10;
1222 /* Set up Dest MAC */
1223 if (compare_ether_addr(old_dmac
, pkt_dev
->dst_mac
))
1224 memcpy(&(pkt_dev
->hh
[0]), pkt_dev
->dst_mac
, ETH_ALEN
);
1226 sprintf(pg_result
, "OK: dstmac");
1229 if (!strcmp(name
, "src_mac")) {
1231 unsigned char *m
= pkt_dev
->src_mac
;
1233 len
= strn_len(&user_buffer
[i
], sizeof(valstr
) - 1);
1234 if (len
< 0) { return len
; }
1235 memset(valstr
, 0, sizeof(valstr
));
1236 if( copy_from_user(valstr
, &user_buffer
[i
], len
))
1240 for(*m
= 0;*v
&& m
< pkt_dev
->src_mac
+ 6; v
++) {
1241 if (*v
>= '0' && *v
<= '9') {
1245 if (*v
>= 'A' && *v
<= 'F') {
1247 *m
+= *v
- 'A' + 10;
1249 if (*v
>= 'a' && *v
<= 'f') {
1251 *m
+= *v
- 'a' + 10;
1259 sprintf(pg_result
, "OK: srcmac");
1263 if (!strcmp(name
, "clear_counters")) {
1264 pktgen_clear_counters(pkt_dev
);
1265 sprintf(pg_result
, "OK: Clearing counters.\n");
1269 if (!strcmp(name
, "flows")) {
1270 len
= num_arg(&user_buffer
[i
], 10, &value
);
1271 if (len
< 0) { return len
; }
1273 if (value
> MAX_CFLOWS
)
1276 pkt_dev
->cflows
= value
;
1277 sprintf(pg_result
, "OK: flows=%u", pkt_dev
->cflows
);
1281 if (!strcmp(name
, "flowlen")) {
1282 len
= num_arg(&user_buffer
[i
], 10, &value
);
1283 if (len
< 0) { return len
; }
1285 pkt_dev
->lflow
= value
;
1286 sprintf(pg_result
, "OK: flowlen=%u", pkt_dev
->lflow
);
1290 sprintf(pkt_dev
->result
, "No such parameter \"%s\"", name
);
1294 static int pktgen_if_open(struct inode
*inode
, struct file
*file
)
1296 return single_open(file
, pktgen_if_show
, PDE(inode
)->data
);
1299 static struct file_operations pktgen_if_fops
= {
1300 .owner
= THIS_MODULE
,
1301 .open
= pktgen_if_open
,
1303 .llseek
= seq_lseek
,
1304 .write
= pktgen_if_write
,
1305 .release
= single_release
,
1308 static int pktgen_thread_show(struct seq_file
*seq
, void *v
)
1310 struct pktgen_thread
*t
= seq
->private;
1311 struct pktgen_dev
*pkt_dev
= NULL
;
1315 seq_printf(seq
, "Name: %s max_before_softirq: %d\n",
1316 t
->name
, t
->max_before_softirq
);
1318 seq_printf(seq
, "Running: ");
1321 for(pkt_dev
= t
->if_list
;pkt_dev
; pkt_dev
= pkt_dev
->next
)
1322 if(pkt_dev
->running
)
1323 seq_printf(seq
, "%s ", pkt_dev
->ifname
);
1325 seq_printf(seq
, "\nStopped: ");
1327 for(pkt_dev
= t
->if_list
;pkt_dev
; pkt_dev
= pkt_dev
->next
)
1328 if(!pkt_dev
->running
)
1329 seq_printf(seq
, "%s ", pkt_dev
->ifname
);
1332 seq_printf(seq
, "\nResult: %s\n", t
->result
);
1334 seq_printf(seq
, "\nResult: NA\n");
1341 static ssize_t
pktgen_thread_write(struct file
*file
,
1342 const char __user
*user_buffer
,
1343 size_t count
, loff_t
*offset
)
1345 struct seq_file
*seq
= (struct seq_file
*) file
->private_data
;
1346 struct pktgen_thread
*t
= seq
->private;
1347 int i
= 0, max
, len
, ret
;
1350 unsigned long value
= 0;
1353 // sprintf(pg_result, "Wrong command format");
1358 len
= count_trail_chars(&user_buffer
[i
], max
);
1364 /* Read variable name */
1366 len
= strn_len(&user_buffer
[i
], sizeof(name
) - 1);
1370 memset(name
, 0, sizeof(name
));
1371 if (copy_from_user(name
, &user_buffer
[i
], len
))
1376 len
= count_trail_chars(&user_buffer
[i
], max
);
1383 printk("pktgen: t=%s, count=%lu\n", name
,
1384 (unsigned long) count
);
1387 printk("pktgen: ERROR: No thread\n");
1392 pg_result
= &(t
->result
[0]);
1394 if (!strcmp(name
, "add_device")) {
1397 len
= strn_len(&user_buffer
[i
], sizeof(f
) - 1);
1402 if( copy_from_user(f
, &user_buffer
[i
], len
) )
1406 pktgen_add_device(t
, f
);
1409 sprintf(pg_result
, "OK: add_device=%s", f
);
1413 if (!strcmp(name
, "rem_device_all")) {
1415 t
->control
|= T_REMDEV
;
1417 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
1419 sprintf(pg_result
, "OK: rem_device_all");
1423 if (!strcmp(name
, "max_before_softirq")) {
1424 len
= num_arg(&user_buffer
[i
], 10, &value
);
1426 t
->max_before_softirq
= value
;
1429 sprintf(pg_result
, "OK: max_before_softirq=%lu", value
);
1439 static int pktgen_thread_open(struct inode
*inode
, struct file
*file
)
1441 return single_open(file
, pktgen_thread_show
, PDE(inode
)->data
);
1444 static struct file_operations pktgen_thread_fops
= {
1445 .owner
= THIS_MODULE
,
1446 .open
= pktgen_thread_open
,
1448 .llseek
= seq_lseek
,
1449 .write
= pktgen_thread_write
,
1450 .release
= single_release
,
1453 /* Think find or remove for NN */
1454 static struct pktgen_dev
*__pktgen_NN_threads(const char* ifname
, int remove
)
1456 struct pktgen_thread
*t
;
1457 struct pktgen_dev
*pkt_dev
= NULL
;
1462 pkt_dev
= pktgen_find_dev(t
, ifname
);
1466 pktgen_remove_device(t
, pkt_dev
);
1476 static struct pktgen_dev
*pktgen_NN_threads(const char* ifname
, int remove
)
1478 struct pktgen_dev
*pkt_dev
= NULL
;
1480 pkt_dev
= __pktgen_NN_threads(ifname
, remove
);
1485 static int pktgen_device_event(struct notifier_block
*unused
, unsigned long event
, void *ptr
)
1487 struct net_device
*dev
= (struct net_device
*)(ptr
);
1489 /* It is OK that we do not hold the group lock right now,
1490 * as we run under the RTNL lock.
1494 case NETDEV_CHANGEADDR
:
1495 case NETDEV_GOING_DOWN
:
1498 /* Ignore for now */
1501 case NETDEV_UNREGISTER
:
1502 pktgen_NN_threads(dev
->name
, REMOVE
);
1509 /* Associate pktgen_dev with a device. */
1511 static struct net_device
* pktgen_setup_dev(struct pktgen_dev
*pkt_dev
) {
1512 struct net_device
*odev
;
1514 /* Clean old setups */
1516 if (pkt_dev
->odev
) {
1517 dev_put(pkt_dev
->odev
);
1518 pkt_dev
->odev
= NULL
;
1521 odev
= dev_get_by_name(pkt_dev
->ifname
);
1524 printk("pktgen: no such netdevice: \"%s\"\n", pkt_dev
->ifname
);
1527 if (odev
->type
!= ARPHRD_ETHER
) {
1528 printk("pktgen: not an ethernet device: \"%s\"\n", pkt_dev
->ifname
);
1531 if (!netif_running(odev
)) {
1532 printk("pktgen: device is down: \"%s\"\n", pkt_dev
->ifname
);
1535 pkt_dev
->odev
= odev
;
1537 return pkt_dev
->odev
;
1546 /* Read pkt_dev from the interface and set up internal pktgen_dev
1547 * structure to have the right information to create/send packets
1549 static void pktgen_setup_inject(struct pktgen_dev
*pkt_dev
)
1551 /* Try once more, just in case it works now. */
1553 pktgen_setup_dev(pkt_dev
);
1555 if (!pkt_dev
->odev
) {
1556 printk("pktgen: ERROR: pkt_dev->odev == NULL in setup_inject.\n");
1557 sprintf(pkt_dev
->result
, "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
1561 /* Default to the interface's mac if not explicitly set. */
1563 if (is_zero_ether_addr(pkt_dev
->src_mac
))
1564 memcpy(&(pkt_dev
->hh
[6]), pkt_dev
->odev
->dev_addr
, ETH_ALEN
);
1566 /* Set up Dest MAC */
1567 memcpy(&(pkt_dev
->hh
[0]), pkt_dev
->dst_mac
, ETH_ALEN
);
1569 /* Set up pkt size */
1570 pkt_dev
->cur_pkt_size
= pkt_dev
->min_pkt_size
;
1572 if(pkt_dev
->flags
& F_IPV6
) {
1574 * Skip this automatic address setting until locks or functions
1579 int i
, set
= 0, err
=1;
1580 struct inet6_dev
*idev
;
1582 for(i
=0; i
< IN6_ADDR_HSIZE
; i
++)
1583 if(pkt_dev
->cur_in6_saddr
.s6_addr
[i
]) {
1591 * Use linklevel address if unconfigured.
1593 * use ipv6_get_lladdr if/when it's get exported
1597 read_lock(&addrconf_lock
);
1598 if ((idev
= __in6_dev_get(pkt_dev
->odev
)) != NULL
) {
1599 struct inet6_ifaddr
*ifp
;
1601 read_lock_bh(&idev
->lock
);
1602 for (ifp
=idev
->addr_list
; ifp
; ifp
=ifp
->if_next
) {
1603 if (ifp
->scope
== IFA_LINK
&& !(ifp
->flags
&IFA_F_TENTATIVE
)) {
1604 ipv6_addr_copy(&pkt_dev
->cur_in6_saddr
, &ifp
->addr
);
1609 read_unlock_bh(&idev
->lock
);
1611 read_unlock(&addrconf_lock
);
1612 if(err
) printk("pktgen: ERROR: IPv6 link address not availble.\n");
1617 pkt_dev
->saddr_min
= 0;
1618 pkt_dev
->saddr_max
= 0;
1619 if (strlen(pkt_dev
->src_min
) == 0) {
1621 struct in_device
*in_dev
;
1624 in_dev
= __in_dev_get_rcu(pkt_dev
->odev
);
1626 if (in_dev
->ifa_list
) {
1627 pkt_dev
->saddr_min
= in_dev
->ifa_list
->ifa_address
;
1628 pkt_dev
->saddr_max
= pkt_dev
->saddr_min
;
1634 pkt_dev
->saddr_min
= in_aton(pkt_dev
->src_min
);
1635 pkt_dev
->saddr_max
= in_aton(pkt_dev
->src_max
);
1638 pkt_dev
->daddr_min
= in_aton(pkt_dev
->dst_min
);
1639 pkt_dev
->daddr_max
= in_aton(pkt_dev
->dst_max
);
1641 /* Initialize current values. */
1642 pkt_dev
->cur_dst_mac_offset
= 0;
1643 pkt_dev
->cur_src_mac_offset
= 0;
1644 pkt_dev
->cur_saddr
= pkt_dev
->saddr_min
;
1645 pkt_dev
->cur_daddr
= pkt_dev
->daddr_min
;
1646 pkt_dev
->cur_udp_dst
= pkt_dev
->udp_dst_min
;
1647 pkt_dev
->cur_udp_src
= pkt_dev
->udp_src_min
;
1648 pkt_dev
->nflows
= 0;
1651 static void spin(struct pktgen_dev
*pkt_dev
, __u64 spin_until_us
)
1656 start
= now
= getCurUs();
1657 printk(KERN_INFO
"sleeping for %d\n", (int)(spin_until_us
- now
));
1658 while (now
< spin_until_us
) {
1659 /* TODO: optimize sleeping behavior */
1660 if (spin_until_us
- now
> jiffies_to_usecs(1)+1)
1661 schedule_timeout_interruptible(1);
1662 else if (spin_until_us
- now
> 100) {
1664 if (!pkt_dev
->running
)
1673 pkt_dev
->idle_acc
+= now
- start
;
1677 /* Increment/randomize headers according to flags and current values
1678 * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
1680 static void mod_cur_headers(struct pktgen_dev
*pkt_dev
) {
1685 if(pkt_dev
->cflows
) {
1686 flow
= pktgen_random() % pkt_dev
->cflows
;
1688 if (pkt_dev
->flows
[flow
].count
> pkt_dev
->lflow
)
1689 pkt_dev
->flows
[flow
].count
= 0;
1693 /* Deal with source MAC */
1694 if (pkt_dev
->src_mac_count
> 1) {
1698 if (pkt_dev
->flags
& F_MACSRC_RND
)
1699 mc
= pktgen_random() % (pkt_dev
->src_mac_count
);
1701 mc
= pkt_dev
->cur_src_mac_offset
++;
1702 if (pkt_dev
->cur_src_mac_offset
> pkt_dev
->src_mac_count
)
1703 pkt_dev
->cur_src_mac_offset
= 0;
1706 tmp
= pkt_dev
->src_mac
[5] + (mc
& 0xFF);
1707 pkt_dev
->hh
[11] = tmp
;
1708 tmp
= (pkt_dev
->src_mac
[4] + ((mc
>> 8) & 0xFF) + (tmp
>> 8));
1709 pkt_dev
->hh
[10] = tmp
;
1710 tmp
= (pkt_dev
->src_mac
[3] + ((mc
>> 16) & 0xFF) + (tmp
>> 8));
1711 pkt_dev
->hh
[9] = tmp
;
1712 tmp
= (pkt_dev
->src_mac
[2] + ((mc
>> 24) & 0xFF) + (tmp
>> 8));
1713 pkt_dev
->hh
[8] = tmp
;
1714 tmp
= (pkt_dev
->src_mac
[1] + (tmp
>> 8));
1715 pkt_dev
->hh
[7] = tmp
;
1718 /* Deal with Destination MAC */
1719 if (pkt_dev
->dst_mac_count
> 1) {
1723 if (pkt_dev
->flags
& F_MACDST_RND
)
1724 mc
= pktgen_random() % (pkt_dev
->dst_mac_count
);
1727 mc
= pkt_dev
->cur_dst_mac_offset
++;
1728 if (pkt_dev
->cur_dst_mac_offset
> pkt_dev
->dst_mac_count
) {
1729 pkt_dev
->cur_dst_mac_offset
= 0;
1733 tmp
= pkt_dev
->dst_mac
[5] + (mc
& 0xFF);
1734 pkt_dev
->hh
[5] = tmp
;
1735 tmp
= (pkt_dev
->dst_mac
[4] + ((mc
>> 8) & 0xFF) + (tmp
>> 8));
1736 pkt_dev
->hh
[4] = tmp
;
1737 tmp
= (pkt_dev
->dst_mac
[3] + ((mc
>> 16) & 0xFF) + (tmp
>> 8));
1738 pkt_dev
->hh
[3] = tmp
;
1739 tmp
= (pkt_dev
->dst_mac
[2] + ((mc
>> 24) & 0xFF) + (tmp
>> 8));
1740 pkt_dev
->hh
[2] = tmp
;
1741 tmp
= (pkt_dev
->dst_mac
[1] + (tmp
>> 8));
1742 pkt_dev
->hh
[1] = tmp
;
1745 if (pkt_dev
->udp_src_min
< pkt_dev
->udp_src_max
) {
1746 if (pkt_dev
->flags
& F_UDPSRC_RND
)
1747 pkt_dev
->cur_udp_src
= ((pktgen_random() % (pkt_dev
->udp_src_max
- pkt_dev
->udp_src_min
)) + pkt_dev
->udp_src_min
);
1750 pkt_dev
->cur_udp_src
++;
1751 if (pkt_dev
->cur_udp_src
>= pkt_dev
->udp_src_max
)
1752 pkt_dev
->cur_udp_src
= pkt_dev
->udp_src_min
;
1756 if (pkt_dev
->udp_dst_min
< pkt_dev
->udp_dst_max
) {
1757 if (pkt_dev
->flags
& F_UDPDST_RND
) {
1758 pkt_dev
->cur_udp_dst
= ((pktgen_random() % (pkt_dev
->udp_dst_max
- pkt_dev
->udp_dst_min
)) + pkt_dev
->udp_dst_min
);
1761 pkt_dev
->cur_udp_dst
++;
1762 if (pkt_dev
->cur_udp_dst
>= pkt_dev
->udp_dst_max
)
1763 pkt_dev
->cur_udp_dst
= pkt_dev
->udp_dst_min
;
1767 if (!(pkt_dev
->flags
& F_IPV6
)) {
1769 if ((imn
= ntohl(pkt_dev
->saddr_min
)) < (imx
= ntohl(pkt_dev
->saddr_max
))) {
1771 if (pkt_dev
->flags
& F_IPSRC_RND
)
1772 t
= ((pktgen_random() % (imx
- imn
)) + imn
);
1774 t
= ntohl(pkt_dev
->cur_saddr
);
1780 pkt_dev
->cur_saddr
= htonl(t
);
1783 if (pkt_dev
->cflows
&& pkt_dev
->flows
[flow
].count
!= 0) {
1784 pkt_dev
->cur_daddr
= pkt_dev
->flows
[flow
].cur_daddr
;
1787 if ((imn
= ntohl(pkt_dev
->daddr_min
)) < (imx
= ntohl(pkt_dev
->daddr_max
))) {
1789 if (pkt_dev
->flags
& F_IPDST_RND
) {
1791 t
= ((pktgen_random() % (imx
- imn
)) + imn
);
1794 while( LOOPBACK(t
) || MULTICAST(t
) || BADCLASS(t
) || ZERONET(t
) || LOCAL_MCAST(t
) ) {
1795 t
= ((pktgen_random() % (imx
- imn
)) + imn
);
1798 pkt_dev
->cur_daddr
= t
;
1802 t
= ntohl(pkt_dev
->cur_daddr
);
1807 pkt_dev
->cur_daddr
= htonl(t
);
1810 if(pkt_dev
->cflows
) {
1811 pkt_dev
->flows
[flow
].cur_daddr
= pkt_dev
->cur_daddr
;
1818 if(pkt_dev
->min_in6_daddr
.s6_addr32
[0] == 0 &&
1819 pkt_dev
->min_in6_daddr
.s6_addr32
[1] == 0 &&
1820 pkt_dev
->min_in6_daddr
.s6_addr32
[2] == 0 &&
1821 pkt_dev
->min_in6_daddr
.s6_addr32
[3] == 0);
1825 /* Only random destinations yet */
1827 for(i
=0; i
< 4; i
++) {
1828 pkt_dev
->cur_in6_daddr
.s6_addr32
[i
] =
1830 pkt_dev
->min_in6_daddr
.s6_addr32
[i
]) &
1831 pkt_dev
->max_in6_daddr
.s6_addr32
[i
]);
1836 if (pkt_dev
->min_pkt_size
< pkt_dev
->max_pkt_size
) {
1838 if (pkt_dev
->flags
& F_TXSIZE_RND
) {
1839 t
= ((pktgen_random() % (pkt_dev
->max_pkt_size
- pkt_dev
->min_pkt_size
))
1840 + pkt_dev
->min_pkt_size
);
1843 t
= pkt_dev
->cur_pkt_size
+ 1;
1844 if (t
> pkt_dev
->max_pkt_size
)
1845 t
= pkt_dev
->min_pkt_size
;
1847 pkt_dev
->cur_pkt_size
= t
;
1850 pkt_dev
->flows
[flow
].count
++;
1854 static struct sk_buff
*fill_packet_ipv4(struct net_device
*odev
,
1855 struct pktgen_dev
*pkt_dev
)
1857 struct sk_buff
*skb
= NULL
;
1859 struct udphdr
*udph
;
1862 struct pktgen_hdr
*pgh
= NULL
;
1864 /* Update any of the values, used when we're incrementing various
1867 mod_cur_headers(pkt_dev
);
1869 datalen
= (odev
->hard_header_len
+ 16) & ~0xf;
1870 skb
= alloc_skb(pkt_dev
->cur_pkt_size
+ 64 + datalen
, GFP_ATOMIC
);
1872 sprintf(pkt_dev
->result
, "No memory");
1876 skb_reserve(skb
, datalen
);
1878 /* Reserve for ethernet and IP header */
1879 eth
= (__u8
*) skb_push(skb
, 14);
1880 iph
= (struct iphdr
*)skb_put(skb
, sizeof(struct iphdr
));
1881 udph
= (struct udphdr
*)skb_put(skb
, sizeof(struct udphdr
));
1883 memcpy(eth
, pkt_dev
->hh
, 12);
1884 *(u16
*)ð
[12] = __constant_htons(ETH_P_IP
);
1886 datalen
= pkt_dev
->cur_pkt_size
- 14 - 20 - 8; /* Eth + IPh + UDPh */
1887 if (datalen
< sizeof(struct pktgen_hdr
))
1888 datalen
= sizeof(struct pktgen_hdr
);
1890 udph
->source
= htons(pkt_dev
->cur_udp_src
);
1891 udph
->dest
= htons(pkt_dev
->cur_udp_dst
);
1892 udph
->len
= htons(datalen
+ 8); /* DATA + udphdr */
1893 udph
->check
= 0; /* No checksum */
1899 iph
->protocol
= IPPROTO_UDP
; /* UDP */
1900 iph
->saddr
= pkt_dev
->cur_saddr
;
1901 iph
->daddr
= pkt_dev
->cur_daddr
;
1903 iplen
= 20 + 8 + datalen
;
1904 iph
->tot_len
= htons(iplen
);
1906 iph
->check
= ip_fast_csum((void *) iph
, iph
->ihl
);
1907 skb
->protocol
= __constant_htons(ETH_P_IP
);
1908 skb
->mac
.raw
= ((u8
*)iph
) - 14;
1910 skb
->pkt_type
= PACKET_HOST
;
1914 if (pkt_dev
->nfrags
<= 0)
1915 pgh
= (struct pktgen_hdr
*)skb_put(skb
, datalen
);
1917 int frags
= pkt_dev
->nfrags
;
1920 pgh
= (struct pktgen_hdr
*)(((char*)(udph
)) + 8);
1922 if (frags
> MAX_SKB_FRAGS
)
1923 frags
= MAX_SKB_FRAGS
;
1924 if (datalen
> frags
*PAGE_SIZE
) {
1925 skb_put(skb
, datalen
-frags
*PAGE_SIZE
);
1926 datalen
= frags
*PAGE_SIZE
;
1930 while (datalen
> 0) {
1931 struct page
*page
= alloc_pages(GFP_KERNEL
, 0);
1932 skb_shinfo(skb
)->frags
[i
].page
= page
;
1933 skb_shinfo(skb
)->frags
[i
].page_offset
= 0;
1934 skb_shinfo(skb
)->frags
[i
].size
=
1935 (datalen
< PAGE_SIZE
? datalen
: PAGE_SIZE
);
1936 datalen
-= skb_shinfo(skb
)->frags
[i
].size
;
1937 skb
->len
+= skb_shinfo(skb
)->frags
[i
].size
;
1938 skb
->data_len
+= skb_shinfo(skb
)->frags
[i
].size
;
1940 skb_shinfo(skb
)->nr_frags
= i
;
1949 rem
= skb_shinfo(skb
)->frags
[i
- 1].size
/ 2;
1953 skb_shinfo(skb
)->frags
[i
- 1].size
-= rem
;
1955 skb_shinfo(skb
)->frags
[i
] = skb_shinfo(skb
)->frags
[i
- 1];
1956 get_page(skb_shinfo(skb
)->frags
[i
].page
);
1957 skb_shinfo(skb
)->frags
[i
].page
= skb_shinfo(skb
)->frags
[i
- 1].page
;
1958 skb_shinfo(skb
)->frags
[i
].page_offset
+= skb_shinfo(skb
)->frags
[i
- 1].size
;
1959 skb_shinfo(skb
)->frags
[i
].size
= rem
;
1961 skb_shinfo(skb
)->nr_frags
= i
;
1965 /* Stamp the time, and sequence number, convert them to network byte order */
1968 struct timeval timestamp
;
1970 pgh
->pgh_magic
= htonl(PKTGEN_MAGIC
);
1971 pgh
->seq_num
= htonl(pkt_dev
->seq_num
);
1973 do_gettimeofday(×tamp
);
1974 pgh
->tv_sec
= htonl(timestamp
.tv_sec
);
1975 pgh
->tv_usec
= htonl(timestamp
.tv_usec
);
1983 * scan_ip6, fmt_ip taken from dietlibc-0.21
1984 * Author Felix von Leitner <felix-dietlibc@fefe.de>
1986 * Slightly modified for kernel.
1987 * Should be candidate for net/ipv4/utils.c
1991 static unsigned int scan_ip6(const char *s
,char ip
[16])
1997 unsigned int prefixlen
=0;
1998 unsigned int suffixlen
=0;
2001 for (i
=0; i
<16; i
++) ip
[i
]=0;
2006 if (s
[1] == ':') { /* Found "::", skip to part 2 */
2015 u
=simple_strtoul(s
,&tmp
,16);
2020 if (prefixlen
==12 && s
[i
]=='.') {
2022 /* the last 4 bytes may be written as IPv4 address */
2025 memcpy((struct in_addr
*)(ip
+12), &tmp
, sizeof(tmp
));
2028 ip
[prefixlen
++] = (u
>> 8);
2029 ip
[prefixlen
++] = (u
& 255);
2035 /* part 2, after "::" */
2042 } else if (suffixlen
!=0)
2046 u
=simple_strtol(s
,&tmp
,16);
2053 if (suffixlen
+prefixlen
<=12 && s
[i
]=='.') {
2055 memcpy((struct in_addr
*)(suffix
+suffixlen
), &tmp
, sizeof(tmp
));
2060 suffix
[suffixlen
++] = (u
>> 8);
2061 suffix
[suffixlen
++] = (u
& 255);
2063 if (prefixlen
+suffixlen
==16)
2066 for (i
=0; i
<suffixlen
; i
++)
2067 ip
[16-suffixlen
+i
] = suffix
[i
];
2071 static char tohex(char hexdigit
) {
2072 return hexdigit
>9?hexdigit
+'a'-10:hexdigit
+'0';
2075 static int fmt_xlong(char* s
,unsigned int i
) {
2077 *s
=tohex((i
>>12)&0xf); if (s
!=bak
|| *s
!='0') ++s
;
2078 *s
=tohex((i
>>8)&0xf); if (s
!=bak
|| *s
!='0') ++s
;
2079 *s
=tohex((i
>>4)&0xf); if (s
!=bak
|| *s
!='0') ++s
;
2084 static unsigned int fmt_ip6(char *s
,const char ip
[16]) {
2088 unsigned int compressing
;
2091 len
= 0; compressing
= 0;
2092 for (j
=0; j
<16; j
+=2) {
2094 #ifdef V4MAPPEDPREFIX
2095 if (j
==12 && !memcmp(ip
,V4mappedprefix
,12)) {
2096 inet_ntoa_r(*(struct in_addr
*)(ip
+12),s
);
2101 temp
= ((unsigned long) (unsigned char) ip
[j
] << 8) +
2102 (unsigned long) (unsigned char) ip
[j
+1];
2115 i
= fmt_xlong(s
,temp
); len
+= i
; s
+= i
;
2129 static struct sk_buff
*fill_packet_ipv6(struct net_device
*odev
,
2130 struct pktgen_dev
*pkt_dev
)
2132 struct sk_buff
*skb
= NULL
;
2134 struct udphdr
*udph
;
2136 struct ipv6hdr
*iph
;
2137 struct pktgen_hdr
*pgh
= NULL
;
2139 /* Update any of the values, used when we're incrementing various
2142 mod_cur_headers(pkt_dev
);
2144 skb
= alloc_skb(pkt_dev
->cur_pkt_size
+ 64 + 16, GFP_ATOMIC
);
2146 sprintf(pkt_dev
->result
, "No memory");
2150 skb_reserve(skb
, 16);
2152 /* Reserve for ethernet and IP header */
2153 eth
= (__u8
*) skb_push(skb
, 14);
2154 iph
= (struct ipv6hdr
*)skb_put(skb
, sizeof(struct ipv6hdr
));
2155 udph
= (struct udphdr
*)skb_put(skb
, sizeof(struct udphdr
));
2157 memcpy(eth
, pkt_dev
->hh
, 12);
2158 *(u16
*)ð
[12] = __constant_htons(ETH_P_IPV6
);
2160 datalen
= pkt_dev
->cur_pkt_size
-14-
2161 sizeof(struct ipv6hdr
)-sizeof(struct udphdr
); /* Eth + IPh + UDPh */
2163 if (datalen
< sizeof(struct pktgen_hdr
)) {
2164 datalen
= sizeof(struct pktgen_hdr
);
2165 if (net_ratelimit())
2166 printk(KERN_INFO
"pktgen: increased datalen to %d\n", datalen
);
2169 udph
->source
= htons(pkt_dev
->cur_udp_src
);
2170 udph
->dest
= htons(pkt_dev
->cur_udp_dst
);
2171 udph
->len
= htons(datalen
+ sizeof(struct udphdr
));
2172 udph
->check
= 0; /* No checksum */
2174 *(u32
*)iph
= __constant_htonl(0x60000000); /* Version + flow */
2176 iph
->hop_limit
= 32;
2178 iph
->payload_len
= htons(sizeof(struct udphdr
) + datalen
);
2179 iph
->nexthdr
= IPPROTO_UDP
;
2181 ipv6_addr_copy(&iph
->daddr
, &pkt_dev
->cur_in6_daddr
);
2182 ipv6_addr_copy(&iph
->saddr
, &pkt_dev
->cur_in6_saddr
);
2184 skb
->mac
.raw
= ((u8
*)iph
) - 14;
2185 skb
->protocol
= __constant_htons(ETH_P_IPV6
);
2187 skb
->pkt_type
= PACKET_HOST
;
2188 skb
->nh
.ipv6h
= iph
;
2191 if (pkt_dev
->nfrags
<= 0)
2192 pgh
= (struct pktgen_hdr
*)skb_put(skb
, datalen
);
2194 int frags
= pkt_dev
->nfrags
;
2197 pgh
= (struct pktgen_hdr
*)(((char*)(udph
)) + 8);
2199 if (frags
> MAX_SKB_FRAGS
)
2200 frags
= MAX_SKB_FRAGS
;
2201 if (datalen
> frags
*PAGE_SIZE
) {
2202 skb_put(skb
, datalen
-frags
*PAGE_SIZE
);
2203 datalen
= frags
*PAGE_SIZE
;
2207 while (datalen
> 0) {
2208 struct page
*page
= alloc_pages(GFP_KERNEL
, 0);
2209 skb_shinfo(skb
)->frags
[i
].page
= page
;
2210 skb_shinfo(skb
)->frags
[i
].page_offset
= 0;
2211 skb_shinfo(skb
)->frags
[i
].size
=
2212 (datalen
< PAGE_SIZE
? datalen
: PAGE_SIZE
);
2213 datalen
-= skb_shinfo(skb
)->frags
[i
].size
;
2214 skb
->len
+= skb_shinfo(skb
)->frags
[i
].size
;
2215 skb
->data_len
+= skb_shinfo(skb
)->frags
[i
].size
;
2217 skb_shinfo(skb
)->nr_frags
= i
;
2226 rem
= skb_shinfo(skb
)->frags
[i
- 1].size
/ 2;
2230 skb_shinfo(skb
)->frags
[i
- 1].size
-= rem
;
2232 skb_shinfo(skb
)->frags
[i
] = skb_shinfo(skb
)->frags
[i
- 1];
2233 get_page(skb_shinfo(skb
)->frags
[i
].page
);
2234 skb_shinfo(skb
)->frags
[i
].page
= skb_shinfo(skb
)->frags
[i
- 1].page
;
2235 skb_shinfo(skb
)->frags
[i
].page_offset
+= skb_shinfo(skb
)->frags
[i
- 1].size
;
2236 skb_shinfo(skb
)->frags
[i
].size
= rem
;
2238 skb_shinfo(skb
)->nr_frags
= i
;
2242 /* Stamp the time, and sequence number, convert them to network byte order */
2243 /* should we update cloned packets too ? */
2245 struct timeval timestamp
;
2247 pgh
->pgh_magic
= htonl(PKTGEN_MAGIC
);
2248 pgh
->seq_num
= htonl(pkt_dev
->seq_num
);
2250 do_gettimeofday(×tamp
);
2251 pgh
->tv_sec
= htonl(timestamp
.tv_sec
);
2252 pgh
->tv_usec
= htonl(timestamp
.tv_usec
);
2259 static inline struct sk_buff
*fill_packet(struct net_device
*odev
,
2260 struct pktgen_dev
*pkt_dev
)
2262 if(pkt_dev
->flags
& F_IPV6
)
2263 return fill_packet_ipv6(odev
, pkt_dev
);
2265 return fill_packet_ipv4(odev
, pkt_dev
);
2268 static void pktgen_clear_counters(struct pktgen_dev
*pkt_dev
)
2270 pkt_dev
->seq_num
= 1;
2271 pkt_dev
->idle_acc
= 0;
2273 pkt_dev
->tx_bytes
= 0;
2274 pkt_dev
->errors
= 0;
2277 /* Set up structure for sending pkts, clear counters */
2279 static void pktgen_run(struct pktgen_thread
*t
)
2281 struct pktgen_dev
*pkt_dev
= NULL
;
2284 PG_DEBUG(printk("pktgen: entering pktgen_run. %p\n", t
));
2287 for (pkt_dev
= t
->if_list
; pkt_dev
; pkt_dev
= pkt_dev
->next
) {
2290 * setup odev and create initial packet.
2292 pktgen_setup_inject(pkt_dev
);
2295 pktgen_clear_counters(pkt_dev
);
2296 pkt_dev
->running
= 1; /* Cranke yeself! */
2297 pkt_dev
->skb
= NULL
;
2298 pkt_dev
->started_at
= getCurUs();
2299 pkt_dev
->next_tx_us
= getCurUs(); /* Transmit immediately */
2300 pkt_dev
->next_tx_ns
= 0;
2302 strcpy(pkt_dev
->result
, "Starting");
2306 strcpy(pkt_dev
->result
, "Error starting");
2309 if(started
) t
->control
&= ~(T_STOP
);
2312 static void pktgen_stop_all_threads_ifs(void)
2314 struct pktgen_thread
*t
= pktgen_threads
;
2316 PG_DEBUG(printk("pktgen: entering pktgen_stop_all_threads.\n"));
2326 static int thread_is_running(struct pktgen_thread
*t
)
2328 struct pktgen_dev
*next
;
2331 for(next
=t
->if_list
; next
; next
=next
->next
) {
2340 static int pktgen_wait_thread_run(struct pktgen_thread
*t
)
2344 while(thread_is_running(t
)) {
2348 msleep_interruptible(100);
2350 if (signal_pending(current
))
2360 static int pktgen_wait_all_threads_run(void)
2362 struct pktgen_thread
*t
= pktgen_threads
;
2366 sig
= pktgen_wait_thread_run(t
);
2367 if( sig
== 0 ) break;
2375 t
->control
|= (T_STOP
);
2383 static void pktgen_run_all_threads(void)
2385 struct pktgen_thread
*t
= pktgen_threads
;
2387 PG_DEBUG(printk("pktgen: entering pktgen_run_all_threads.\n"));
2392 t
->control
|= (T_RUN
);
2397 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
2399 pktgen_wait_all_threads_run();
2403 static void show_results(struct pktgen_dev
*pkt_dev
, int nr_frags
)
2405 __u64 total_us
, bps
, mbps
, pps
, idle
;
2406 char *p
= pkt_dev
->result
;
2408 total_us
= pkt_dev
->stopped_at
- pkt_dev
->started_at
;
2410 idle
= pkt_dev
->idle_acc
;
2412 p
+= sprintf(p
, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
2413 (unsigned long long) total_us
,
2414 (unsigned long long)(total_us
- idle
),
2415 (unsigned long long) idle
,
2416 (unsigned long long) pkt_dev
->sofar
,
2417 pkt_dev
->cur_pkt_size
, nr_frags
);
2419 pps
= pkt_dev
->sofar
* USEC_PER_SEC
;
2421 while ((total_us
>> 32) != 0) {
2426 do_div(pps
, total_us
);
2428 bps
= pps
* 8 * pkt_dev
->cur_pkt_size
;
2431 do_div(mbps
, 1000000);
2432 p
+= sprintf(p
, " %llupps %lluMb/sec (%llubps) errors: %llu",
2433 (unsigned long long) pps
,
2434 (unsigned long long) mbps
,
2435 (unsigned long long) bps
,
2436 (unsigned long long) pkt_dev
->errors
);
2440 /* Set stopped-at timer, remove from running list, do counters & statistics */
2442 static int pktgen_stop_device(struct pktgen_dev
*pkt_dev
)
2445 if (!pkt_dev
->running
) {
2446 printk("pktgen: interface: %s is already stopped\n", pkt_dev
->ifname
);
2450 pkt_dev
->stopped_at
= getCurUs();
2451 pkt_dev
->running
= 0;
2453 show_results(pkt_dev
, skb_shinfo(pkt_dev
->skb
)->nr_frags
);
2456 kfree_skb(pkt_dev
->skb
);
2458 pkt_dev
->skb
= NULL
;
2463 static struct pktgen_dev
*next_to_run(struct pktgen_thread
*t
)
2465 struct pktgen_dev
*next
, *best
= NULL
;
2469 for(next
=t
->if_list
; next
; next
=next
->next
) {
2470 if(!next
->running
) continue;
2471 if(best
== NULL
) best
=next
;
2472 else if ( next
->next_tx_us
< best
->next_tx_us
)
2479 static void pktgen_stop(struct pktgen_thread
*t
) {
2480 struct pktgen_dev
*next
= NULL
;
2482 PG_DEBUG(printk("pktgen: entering pktgen_stop.\n"));
2486 for(next
=t
->if_list
; next
; next
=next
->next
)
2487 pktgen_stop_device(next
);
2492 static void pktgen_rem_all_ifs(struct pktgen_thread
*t
)
2494 struct pktgen_dev
*cur
, *next
= NULL
;
2496 /* Remove all devices, free mem */
2500 for(cur
=t
->if_list
; cur
; cur
=next
) {
2502 pktgen_remove_device(t
, cur
);
2508 static void pktgen_rem_thread(struct pktgen_thread
*t
)
2510 /* Remove from the thread list */
2512 struct pktgen_thread
*tmp
= pktgen_threads
;
2514 remove_proc_entry(t
->name
, pg_proc_dir
);
2519 pktgen_threads
= tmp
->next
;
2522 if (tmp
->next
== t
) {
2523 tmp
->next
= t
->next
;
2533 static __inline__
void pktgen_xmit(struct pktgen_dev
*pkt_dev
)
2535 struct net_device
*odev
= NULL
;
2536 __u64 idle_start
= 0;
2539 odev
= pkt_dev
->odev
;
2541 if (pkt_dev
->delay_us
|| pkt_dev
->delay_ns
) {
2545 if (now
< pkt_dev
->next_tx_us
)
2546 spin(pkt_dev
, pkt_dev
->next_tx_us
);
2548 /* This is max DELAY, this has special meaning of
2551 if (pkt_dev
->delay_us
== 0x7FFFFFFF) {
2552 pkt_dev
->next_tx_us
= getCurUs() + pkt_dev
->delay_us
;
2553 pkt_dev
->next_tx_ns
= pkt_dev
->delay_ns
;
2558 if (netif_queue_stopped(odev
) || need_resched()) {
2559 idle_start
= getCurUs();
2561 if (!netif_running(odev
)) {
2562 pktgen_stop_device(pkt_dev
);
2568 pkt_dev
->idle_acc
+= getCurUs() - idle_start
;
2570 if (netif_queue_stopped(odev
)) {
2571 pkt_dev
->next_tx_us
= getCurUs(); /* TODO */
2572 pkt_dev
->next_tx_ns
= 0;
2573 goto out
; /* Try the next interface */
2577 if (pkt_dev
->last_ok
|| !pkt_dev
->skb
) {
2578 if ((++pkt_dev
->clone_count
>= pkt_dev
->clone_skb
) || (!pkt_dev
->skb
)) {
2579 /* build a new pkt */
2581 kfree_skb(pkt_dev
->skb
);
2583 pkt_dev
->skb
= fill_packet(odev
, pkt_dev
);
2584 if (pkt_dev
->skb
== NULL
) {
2585 printk("pktgen: ERROR: couldn't allocate skb in fill_packet.\n");
2587 pkt_dev
->clone_count
--; /* back out increment, OOM */
2590 pkt_dev
->allocated_skbs
++;
2591 pkt_dev
->clone_count
= 0; /* reset counter */
2595 spin_lock_bh(&odev
->xmit_lock
);
2596 if (!netif_queue_stopped(odev
)) {
2598 atomic_inc(&(pkt_dev
->skb
->users
));
2600 ret
= odev
->hard_start_xmit(pkt_dev
->skb
, odev
);
2601 if (likely(ret
== NETDEV_TX_OK
)) {
2602 pkt_dev
->last_ok
= 1;
2605 pkt_dev
->tx_bytes
+= pkt_dev
->cur_pkt_size
;
2607 } else if (ret
== NETDEV_TX_LOCKED
2608 && (odev
->features
& NETIF_F_LLTX
)) {
2611 } else { /* Retry it next time */
2613 atomic_dec(&(pkt_dev
->skb
->users
));
2615 if (debug
&& net_ratelimit())
2616 printk(KERN_INFO
"pktgen: Hard xmit error\n");
2619 pkt_dev
->last_ok
= 0;
2622 pkt_dev
->next_tx_us
= getCurUs();
2623 pkt_dev
->next_tx_ns
= 0;
2625 pkt_dev
->next_tx_us
+= pkt_dev
->delay_us
;
2626 pkt_dev
->next_tx_ns
+= pkt_dev
->delay_ns
;
2628 if (pkt_dev
->next_tx_ns
> 1000) {
2629 pkt_dev
->next_tx_us
++;
2630 pkt_dev
->next_tx_ns
-= 1000;
2634 else { /* Retry it next time */
2635 pkt_dev
->last_ok
= 0;
2636 pkt_dev
->next_tx_us
= getCurUs(); /* TODO */
2637 pkt_dev
->next_tx_ns
= 0;
2640 spin_unlock_bh(&odev
->xmit_lock
);
2642 /* If pkt_dev->count is zero, then run forever */
2643 if ((pkt_dev
->count
!= 0) && (pkt_dev
->sofar
>= pkt_dev
->count
)) {
2644 if (atomic_read(&(pkt_dev
->skb
->users
)) != 1) {
2645 idle_start
= getCurUs();
2646 while (atomic_read(&(pkt_dev
->skb
->users
)) != 1) {
2647 if (signal_pending(current
)) {
2652 pkt_dev
->idle_acc
+= getCurUs() - idle_start
;
2655 /* Done with this */
2656 pktgen_stop_device(pkt_dev
);
2662 * Main loop of the thread goes here
2665 static void pktgen_thread_worker(struct pktgen_thread_info
*info
)
2668 struct pktgen_dev
*pkt_dev
= NULL
;
2669 struct pktgen_thread
*t
= info
->t
;
2672 u32 max_before_softirq
;
2673 u32 tx_since_softirq
= 0;
2675 daemonize("pktgen/%d", cpu
);
2677 /* Block all signals except SIGKILL, SIGSTOP and SIGTERM */
2679 spin_lock_irq(¤t
->sighand
->siglock
);
2680 tmpsig
= current
->blocked
;
2681 siginitsetinv(¤t
->blocked
,
2686 recalc_sigpending();
2687 spin_unlock_irq(¤t
->sighand
->siglock
);
2689 /* Migrate to the right CPU */
2690 set_cpus_allowed(current
, cpumask_of_cpu(cpu
));
2691 if (smp_processor_id() != cpu
)
2694 init_waitqueue_head(&t
->queue
);
2696 t
->control
&= ~(T_TERMINATE
);
2697 t
->control
&= ~(T_RUN
);
2698 t
->control
&= ~(T_STOP
);
2699 t
->control
&= ~(T_REMDEV
);
2701 t
->pid
= current
->pid
;
2703 PG_DEBUG(printk("pktgen: starting pktgen/%d: pid=%d\n", cpu
, current
->pid
));
2705 max_before_softirq
= t
->max_before_softirq
;
2707 __set_current_state(TASK_INTERRUPTIBLE
);
2714 __set_current_state(TASK_RUNNING
);
2717 * Get next dev to xmit -- if any.
2720 pkt_dev
= next_to_run(t
);
2724 pktgen_xmit(pkt_dev
);
2727 * We like to stay RUNNING but must also give
2728 * others fair share.
2731 tx_since_softirq
+= pkt_dev
->last_ok
;
2733 if (tx_since_softirq
> max_before_softirq
) {
2734 if (local_softirq_pending())
2736 tx_since_softirq
= 0;
2739 prepare_to_wait(&(t
->queue
), &wait
, TASK_INTERRUPTIBLE
);
2740 schedule_timeout(HZ
/10);
2741 finish_wait(&(t
->queue
), &wait
);
2745 * Back from sleep, either due to the timeout or signal.
2746 * We check if we have any "posted" work for us.
2749 if (t
->control
& T_TERMINATE
|| signal_pending(current
))
2750 /* we received a request to terminate ourself */
2754 if(t
->control
& T_STOP
) {
2756 t
->control
&= ~(T_STOP
);
2759 if(t
->control
& T_RUN
) {
2761 t
->control
&= ~(T_RUN
);
2764 if(t
->control
& T_REMDEV
) {
2765 pktgen_rem_all_ifs(t
);
2766 t
->control
&= ~(T_REMDEV
);
2773 PG_DEBUG(printk("pktgen: %s stopping all device\n", t
->name
));
2776 PG_DEBUG(printk("pktgen: %s removing all device\n", t
->name
));
2777 pktgen_rem_all_ifs(t
);
2779 PG_DEBUG(printk("pktgen: %s removing thread.\n", t
->name
));
2780 pktgen_rem_thread(t
);
2783 static struct pktgen_dev
*pktgen_find_dev(struct pktgen_thread
*t
, const char* ifname
)
2785 struct pktgen_dev
*pkt_dev
= NULL
;
2788 for(pkt_dev
=t
->if_list
; pkt_dev
; pkt_dev
= pkt_dev
->next
) {
2789 if (strncmp(pkt_dev
->ifname
, ifname
, IFNAMSIZ
) == 0) {
2795 PG_DEBUG(printk("pktgen: find_dev(%s) returning %p\n", ifname
,pkt_dev
));
2800 * Adds a dev at front of if_list.
2803 static int add_dev_to_thread(struct pktgen_thread
*t
, struct pktgen_dev
*pkt_dev
)
2809 if (pkt_dev
->pg_thread
) {
2810 printk("pktgen: ERROR: already assigned to a thread.\n");
2814 pkt_dev
->next
=t
->if_list
; t
->if_list
=pkt_dev
;
2815 pkt_dev
->pg_thread
= t
;
2816 pkt_dev
->running
= 0;
2823 /* Called under thread lock */
2825 static int pktgen_add_device(struct pktgen_thread
*t
, const char* ifname
)
2827 struct pktgen_dev
*pkt_dev
;
2828 struct proc_dir_entry
*pe
;
2830 /* We don't allow a device to be on several threads */
2832 pkt_dev
= __pktgen_NN_threads(ifname
, FIND
);
2834 printk("pktgen: ERROR: interface already used.\n");
2838 pkt_dev
= kzalloc(sizeof(struct pktgen_dev
), GFP_KERNEL
);
2842 pkt_dev
->flows
= vmalloc(MAX_CFLOWS
*sizeof(struct flow_state
));
2843 if (pkt_dev
->flows
== NULL
) {
2847 memset(pkt_dev
->flows
, 0, MAX_CFLOWS
*sizeof(struct flow_state
));
2849 pkt_dev
->min_pkt_size
= ETH_ZLEN
;
2850 pkt_dev
->max_pkt_size
= ETH_ZLEN
;
2851 pkt_dev
->nfrags
= 0;
2852 pkt_dev
->clone_skb
= pg_clone_skb_d
;
2853 pkt_dev
->delay_us
= pg_delay_d
/ 1000;
2854 pkt_dev
->delay_ns
= pg_delay_d
% 1000;
2855 pkt_dev
->count
= pg_count_d
;
2857 pkt_dev
->udp_src_min
= 9; /* sink port */
2858 pkt_dev
->udp_src_max
= 9;
2859 pkt_dev
->udp_dst_min
= 9;
2860 pkt_dev
->udp_dst_max
= 9;
2862 strncpy(pkt_dev
->ifname
, ifname
, IFNAMSIZ
);
2864 if (! pktgen_setup_dev(pkt_dev
)) {
2865 printk("pktgen: ERROR: pktgen_setup_dev failed.\n");
2867 vfree(pkt_dev
->flows
);
2872 pe
= create_proc_entry(ifname
, 0600, pg_proc_dir
);
2874 printk("pktgen: cannot create %s/%s procfs entry.\n",
2875 PG_PROC_DIR
, ifname
);
2877 vfree(pkt_dev
->flows
);
2881 pe
->proc_fops
= &pktgen_if_fops
;
2884 return add_dev_to_thread(t
, pkt_dev
);
2887 static struct pktgen_thread
* __init
pktgen_find_thread(const char* name
)
2889 struct pktgen_thread
*t
= NULL
;
2895 if (strcmp(t
->name
, name
) == 0)
2904 static int __init
pktgen_create_thread(const char* name
, int cpu
)
2906 struct pktgen_thread_info info
;
2907 struct completion started
;
2908 struct pktgen_thread
*t
= NULL
;
2909 struct proc_dir_entry
*pe
;
2911 if (strlen(name
) > 31) {
2912 printk("pktgen: ERROR: Thread name cannot be more than 31 characters.\n");
2916 if (pktgen_find_thread(name
)) {
2917 printk("pktgen: ERROR: thread: %s already exists\n", name
);
2921 t
= kzalloc(sizeof(struct pktgen_thread
), GFP_KERNEL
);
2923 printk("pktgen: ERROR: out of memory, can't create new thread.\n");
2927 strcpy(t
->name
, name
);
2928 spin_lock_init(&t
->if_lock
);
2931 pe
= create_proc_entry(t
->name
, 0600, pg_proc_dir
);
2933 printk("pktgen: cannot create %s/%s procfs entry.\n",
2934 PG_PROC_DIR
, t
->name
);
2939 pe
->proc_fops
= &pktgen_thread_fops
;
2942 t
->next
= pktgen_threads
;
2945 init_completion(&started
);
2949 if (kernel_thread((void *) pktgen_thread_worker
, (void *)&info
,
2950 CLONE_FS
| CLONE_FILES
| CLONE_SIGHAND
) < 0)
2951 printk("pktgen: kernel_thread() failed for cpu %d\n", t
->cpu
);
2953 wait_for_completion(&started
);
2958 * Removes a device from the thread if_list.
2960 static void _rem_dev_from_if_list(struct pktgen_thread
*t
, struct pktgen_dev
*pkt_dev
)
2962 struct pktgen_dev
*i
, *prev
= NULL
;
2968 if(prev
) prev
->next
= i
->next
;
2969 else t
->if_list
= NULL
;
2977 static int pktgen_remove_device(struct pktgen_thread
*t
, struct pktgen_dev
*pkt_dev
)
2980 PG_DEBUG(printk("pktgen: remove_device pkt_dev=%p\n", pkt_dev
));
2982 if (pkt_dev
->running
) {
2983 printk("pktgen:WARNING: trying to remove a running interface, stopping it now.\n");
2984 pktgen_stop_device(pkt_dev
);
2987 /* Dis-associate from the interface */
2989 if (pkt_dev
->odev
) {
2990 dev_put(pkt_dev
->odev
);
2991 pkt_dev
->odev
= NULL
;
2994 /* And update the thread if_list */
2996 _rem_dev_from_if_list(t
, pkt_dev
);
2998 /* Clean up proc file system */
3000 remove_proc_entry(pkt_dev
->ifname
, pg_proc_dir
);
3003 vfree(pkt_dev
->flows
);
3008 static int __init
pg_init(void)
3011 struct proc_dir_entry
*pe
;
3015 pg_proc_dir
= proc_mkdir(PG_PROC_DIR
, proc_net
);
3018 pg_proc_dir
->owner
= THIS_MODULE
;
3020 pe
= create_proc_entry(PGCTRL
, 0600, pg_proc_dir
);
3022 printk("pktgen: ERROR: cannot create %s procfs entry.\n", PGCTRL
);
3023 proc_net_remove(PG_PROC_DIR
);
3027 pe
->proc_fops
= &pktgen_fops
;
3030 /* Register us to receive netdevice events */
3031 register_netdevice_notifier(&pktgen_notifier_block
);
3033 for_each_online_cpu(cpu
) {
3036 sprintf(buf
, "kpktgend_%i", cpu
);
3037 pktgen_create_thread(buf
, cpu
);
3042 static void __exit
pg_cleanup(void)
3044 wait_queue_head_t queue
;
3045 init_waitqueue_head(&queue
);
3047 /* Stop all interfaces & threads */
3049 while (pktgen_threads
) {
3050 struct pktgen_thread
*t
= pktgen_threads
;
3051 pktgen_threads
->control
|= (T_TERMINATE
);
3053 wait_event_interruptible_timeout(queue
, (t
!= pktgen_threads
), HZ
);
3056 /* Un-register us from receiving netdevice events */
3057 unregister_netdevice_notifier(&pktgen_notifier_block
);
3059 /* Clean up proc file system */
3060 remove_proc_entry(PGCTRL
, pg_proc_dir
);
3061 proc_net_remove(PG_PROC_DIR
);
3065 module_init(pg_init
);
3066 module_exit(pg_cleanup
);
3068 MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se");
3069 MODULE_DESCRIPTION("Packet Generator tool");
3070 MODULE_LICENSE("GPL");
3071 module_param(pg_count_d
, int, 0);
3072 module_param(pg_delay_d
, int, 0);
3073 module_param(pg_clone_skb_d
, int, 0);
3074 module_param(debug
, int, 0);