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 * MPLS support by Steven Whitehouse <steve@chygwyn.com>
112 * 802.1Q/Q-in-Q support by Francesco Fondelli (FF) <francesco.fondelli@gmail.com>
114 * Fixed src_mac command to set source mac of packet to value specified in
115 * command by Adit Ranadive <adit.262@gmail.com>
118 #include <linux/sys.h>
119 #include <linux/types.h>
120 #include <linux/module.h>
121 #include <linux/moduleparam.h>
122 #include <linux/kernel.h>
123 #include <linux/mutex.h>
124 #include <linux/sched.h>
125 #include <linux/slab.h>
126 #include <linux/vmalloc.h>
127 #include <linux/unistd.h>
128 #include <linux/string.h>
129 #include <linux/ptrace.h>
130 #include <linux/errno.h>
131 #include <linux/ioport.h>
132 #include <linux/interrupt.h>
133 #include <linux/capability.h>
134 #include <linux/freezer.h>
135 #include <linux/delay.h>
136 #include <linux/timer.h>
137 #include <linux/list.h>
138 #include <linux/init.h>
139 #include <linux/skbuff.h>
140 #include <linux/netdevice.h>
141 #include <linux/inet.h>
142 #include <linux/inetdevice.h>
143 #include <linux/rtnetlink.h>
144 #include <linux/if_arp.h>
145 #include <linux/if_vlan.h>
146 #include <linux/in.h>
147 #include <linux/ip.h>
148 #include <linux/ipv6.h>
149 #include <linux/udp.h>
150 #include <linux/proc_fs.h>
151 #include <linux/seq_file.h>
152 #include <linux/wait.h>
153 #include <linux/etherdevice.h>
154 #include <linux/kthread.h>
155 #include <net/checksum.h>
156 #include <net/ipv6.h>
157 #include <net/addrconf.h>
159 #include <net/xfrm.h>
161 #include <asm/byteorder.h>
162 #include <linux/rcupdate.h>
163 #include <asm/bitops.h>
166 #include <asm/uaccess.h>
167 #include <asm/div64.h> /* do_div */
168 #include <asm/timex.h>
170 #define VERSION "pktgen v2.68: Packet Generator for packet performance testing.\n"
172 /* The buckets are exponential in 'width' */
173 #define LAT_BUCKETS_MAX 32
174 #define IP_NAME_SZ 32
175 #define MAX_MPLS_LABELS 16 /* This is the max label stack depth */
176 #define MPLS_STACK_BOTTOM htonl(0x00000100)
178 /* Device flag bits */
179 #define F_IPSRC_RND (1<<0) /* IP-Src Random */
180 #define F_IPDST_RND (1<<1) /* IP-Dst Random */
181 #define F_UDPSRC_RND (1<<2) /* UDP-Src Random */
182 #define F_UDPDST_RND (1<<3) /* UDP-Dst Random */
183 #define F_MACSRC_RND (1<<4) /* MAC-Src Random */
184 #define F_MACDST_RND (1<<5) /* MAC-Dst Random */
185 #define F_TXSIZE_RND (1<<6) /* Transmit size is random */
186 #define F_IPV6 (1<<7) /* Interface in IPV6 Mode */
187 #define F_MPLS_RND (1<<8) /* Random MPLS labels */
188 #define F_VID_RND (1<<9) /* Random VLAN ID */
189 #define F_SVID_RND (1<<10) /* Random SVLAN ID */
190 #define F_FLOW_SEQ (1<<11) /* Sequential flows */
191 #define F_IPSEC_ON (1<<12) /* ipsec on for flows */
193 /* Thread control flag bits */
194 #define T_TERMINATE (1<<0)
195 #define T_STOP (1<<1) /* Stop run */
196 #define T_RUN (1<<2) /* Start run */
197 #define T_REMDEVALL (1<<3) /* Remove all devs */
198 #define T_REMDEV (1<<4) /* Remove one dev */
200 /* If lock -- can be removed after some work */
201 #define if_lock(t) spin_lock(&(t->if_lock));
202 #define if_unlock(t) spin_unlock(&(t->if_lock));
204 /* Used to help with determining the pkts on receive */
205 #define PKTGEN_MAGIC 0xbe9be955
206 #define PG_PROC_DIR "pktgen"
207 #define PGCTRL "pgctrl"
208 static struct proc_dir_entry
*pg_proc_dir
= NULL
;
210 #define MAX_CFLOWS 65536
212 #define VLAN_TAG_SIZE(x) ((x)->vlan_id == 0xffff ? 0 : 4)
213 #define SVLAN_TAG_SIZE(x) ((x)->svlan_id == 0xffff ? 0 : 4)
219 struct xfrm_state
*x
;
225 #define F_INIT (1<<0) /* flow has been initialized */
229 * Try to keep frequent/infrequent used vars. separated.
231 struct proc_dir_entry
*entry
; /* proc file */
232 struct pktgen_thread
*pg_thread
;/* the owner */
233 struct list_head list
; /* Used for chaining in the thread's run-queue */
235 int running
; /* if this changes to false, the test will stop */
237 /* If min != max, then we will either do a linear iteration, or
238 * we will do a random selection from within the range.
241 int removal_mark
; /* non-zero => the device is marked for
242 * removal by worker thread */
244 int min_pkt_size
; /* = ETH_ZLEN; */
245 int max_pkt_size
; /* = ETH_ZLEN; */
246 int pkt_overhead
; /* overhead for MPLS, VLANs, IPSEC etc */
248 __u32 delay_us
; /* Default delay */
250 __u64 count
; /* Default No packets to send */
251 __u64 sofar
; /* How many pkts we've sent so far */
252 __u64 tx_bytes
; /* How many bytes we've transmitted */
253 __u64 errors
; /* Errors when trying to transmit, pkts will be re-sent */
255 /* runtime counters relating to clone_skb */
256 __u64 next_tx_us
; /* timestamp of when to tx next */
259 __u64 allocated_skbs
;
261 int last_ok
; /* Was last skb sent?
262 * Or a failed transmit of some sort? This will keep
263 * sequence numbers in order, for example.
265 __u64 started_at
; /* micro-seconds */
266 __u64 stopped_at
; /* micro-seconds */
267 __u64 idle_acc
; /* micro-seconds */
270 int clone_skb
; /* Use multiple SKBs during packet gen. If this number
271 * is greater than 1, then that many copies of the same
272 * packet will be sent before a new packet is allocated.
273 * For instance, if you want to send 1024 identical packets
274 * before creating a new packet, set clone_skb to 1024.
277 char dst_min
[IP_NAME_SZ
]; /* IP, ie 1.2.3.4 */
278 char dst_max
[IP_NAME_SZ
]; /* IP, ie 1.2.3.4 */
279 char src_min
[IP_NAME_SZ
]; /* IP, ie 1.2.3.4 */
280 char src_max
[IP_NAME_SZ
]; /* IP, ie 1.2.3.4 */
282 struct in6_addr in6_saddr
;
283 struct in6_addr in6_daddr
;
284 struct in6_addr cur_in6_daddr
;
285 struct in6_addr cur_in6_saddr
;
287 struct in6_addr min_in6_daddr
;
288 struct in6_addr max_in6_daddr
;
289 struct in6_addr min_in6_saddr
;
290 struct in6_addr max_in6_saddr
;
292 /* If we're doing ranges, random or incremental, then this
293 * defines the min/max for those ranges.
295 __be32 saddr_min
; /* inclusive, source IP address */
296 __be32 saddr_max
; /* exclusive, source IP address */
297 __be32 daddr_min
; /* inclusive, dest IP address */
298 __be32 daddr_max
; /* exclusive, dest IP address */
300 __u16 udp_src_min
; /* inclusive, source UDP port */
301 __u16 udp_src_max
; /* exclusive, source UDP port */
302 __u16 udp_dst_min
; /* inclusive, dest UDP port */
303 __u16 udp_dst_max
; /* exclusive, dest UDP port */
306 __u8 tos
; /* six most significant bits of (former) IPv4 TOS are for dscp codepoint */
307 __u8 traffic_class
; /* ditto for the (former) Traffic Class in IPv6 (see RFC 3260, sec. 4) */
310 unsigned nr_labels
; /* Depth of stack, 0 = no MPLS */
311 __be32 labels
[MAX_MPLS_LABELS
];
313 /* VLAN/SVLAN (802.1Q/Q-in-Q) */
316 __u16 vlan_id
; /* 0xffff means no vlan tag */
320 __u16 svlan_id
; /* 0xffff means no svlan tag */
322 __u32 src_mac_count
; /* How many MACs to iterate through */
323 __u32 dst_mac_count
; /* How many MACs to iterate through */
325 unsigned char dst_mac
[ETH_ALEN
];
326 unsigned char src_mac
[ETH_ALEN
];
328 __u32 cur_dst_mac_offset
;
329 __u32 cur_src_mac_offset
;
338 0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
340 We fill in SRC address later
341 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
345 __u16 pad
; /* pad out the hh struct to an even 16 bytes */
347 struct sk_buff
*skb
; /* skb we are to transmit next, mainly used for when we
348 * are transmitting the same one multiple times
350 struct net_device
*odev
; /* The out-going device. Note that the device should
351 * have it's pg_info pointer pointing back to this
352 * device. This will be set when the user specifies
353 * the out-going device name (not when the inject is
354 * started as it used to do.)
356 struct flow_state
*flows
;
357 unsigned cflows
; /* Concurrent flows (config) */
358 unsigned lflow
; /* Flow length (config) */
359 unsigned nflows
; /* accumulated flows (stats) */
360 unsigned curfl
; /* current sequenced flow (state)*/
362 __u8 ipsmode
; /* IPSEC mode (config) */
363 __u8 ipsproto
; /* IPSEC type (config) */
375 struct pktgen_thread
{
377 struct list_head if_list
; /* All device here */
378 struct list_head th_list
;
379 struct task_struct
*tsk
;
381 u32 max_before_softirq
; /* We'll call do_softirq to prevent starvation. */
383 /* Field for thread to receive "posted" events terminate, stop ifs etc. */
388 wait_queue_head_t queue
;
394 /* This code works around the fact that do_div cannot handle two 64-bit
395 numbers, and regular 64-bit division doesn't work on x86 kernels.
401 /* This was emailed to LMKL by: Chris Caputo <ccaputo@alt.net>
402 * Function copied/adapted/optimized from:
404 * nemesis.sourceforge.net/browse/lib/static/intmath/ix86/intmath.c.html
406 * Copyright 1994, University of Cambridge Computer Laboratory
407 * All Rights Reserved.
410 static inline s64
divremdi3(s64 x
, s64 y
, int type
)
412 u64 a
= (x
< 0) ? -x
: x
;
413 u64 b
= (y
< 0) ? -y
: y
;
433 if (PG_DIV
== type
) {
434 return (((x
^ y
) & (1ll << 63)) == 0) ? res
: -(s64
) res
;
436 return ((x
& (1ll << 63)) == 0) ? a
: -(s64
) a
;
440 /* End of hacks to deal with 64-bit math on x86 */
442 /** Convert to milliseconds */
443 static inline __u64
tv_to_ms(const struct timeval
*tv
)
445 __u64 ms
= tv
->tv_usec
/ 1000;
446 ms
+= (__u64
) tv
->tv_sec
* (__u64
) 1000;
450 /** Convert to micro-seconds */
451 static inline __u64
tv_to_us(const struct timeval
*tv
)
453 __u64 us
= tv
->tv_usec
;
454 us
+= (__u64
) tv
->tv_sec
* (__u64
) 1000000;
458 static inline __u64
pg_div(__u64 n
, __u32 base
)
462 /* printk("pktgen: pg_div, n: %llu base: %d rv: %llu\n",
467 static inline __u64
pg_div64(__u64 n
, __u64 base
)
471 * How do we know if the architecture we are running on
472 * supports division with 64 bit base?
475 #if defined(__sparc_v9__) || defined(__powerpc64__) || defined(__alpha__) || defined(__x86_64__) || defined(__ia64__)
479 tmp
= divremdi3(n
, base
, PG_DIV
);
484 static inline __u64
getCurMs(void)
487 do_gettimeofday(&tv
);
488 return tv_to_ms(&tv
);
491 static inline __u64
getCurUs(void)
494 do_gettimeofday(&tv
);
495 return tv_to_us(&tv
);
498 static inline __u64
tv_diff(const struct timeval
*a
, const struct timeval
*b
)
500 return tv_to_us(a
) - tv_to_us(b
);
503 /* old include end */
505 static char version
[] __initdata
= VERSION
;
507 static int pktgen_remove_device(struct pktgen_thread
*t
, struct pktgen_dev
*i
);
508 static int pktgen_add_device(struct pktgen_thread
*t
, const char *ifname
);
509 static struct pktgen_dev
*pktgen_find_dev(struct pktgen_thread
*t
,
511 static int pktgen_device_event(struct notifier_block
*, unsigned long, void *);
512 static void pktgen_run_all_threads(void);
513 static void pktgen_stop_all_threads_ifs(void);
514 static int pktgen_stop_device(struct pktgen_dev
*pkt_dev
);
515 static void pktgen_stop(struct pktgen_thread
*t
);
516 static void pktgen_clear_counters(struct pktgen_dev
*pkt_dev
);
518 static unsigned int scan_ip6(const char *s
, char ip
[16]);
519 static unsigned int fmt_ip6(char *s
, const char ip
[16]);
521 /* Module parameters, defaults. */
522 static int pg_count_d
= 1000; /* 1000 pkts by default */
523 static int pg_delay_d
;
524 static int pg_clone_skb_d
;
527 static DEFINE_MUTEX(pktgen_thread_lock
);
528 static LIST_HEAD(pktgen_threads
);
530 static struct notifier_block pktgen_notifier_block
= {
531 .notifier_call
= pktgen_device_event
,
535 * /proc handling functions
539 static int pgctrl_show(struct seq_file
*seq
, void *v
)
541 seq_puts(seq
, VERSION
);
545 static ssize_t
pgctrl_write(struct file
*file
, const char __user
* buf
,
546 size_t count
, loff_t
* ppos
)
551 if (!capable(CAP_NET_ADMIN
)) {
556 if (count
> sizeof(data
))
557 count
= sizeof(data
);
559 if (copy_from_user(data
, buf
, count
)) {
563 data
[count
- 1] = 0; /* Make string */
565 if (!strcmp(data
, "stop"))
566 pktgen_stop_all_threads_ifs();
568 else if (!strcmp(data
, "start"))
569 pktgen_run_all_threads();
572 printk(KERN_WARNING
"pktgen: Unknown command: %s\n", data
);
580 static int pgctrl_open(struct inode
*inode
, struct file
*file
)
582 return single_open(file
, pgctrl_show
, PDE(inode
)->data
);
585 static const struct file_operations pktgen_fops
= {
586 .owner
= THIS_MODULE
,
590 .write
= pgctrl_write
,
591 .release
= single_release
,
594 static int pktgen_if_show(struct seq_file
*seq
, void *v
)
597 struct pktgen_dev
*pkt_dev
= seq
->private;
600 __u64 now
= getCurUs();
603 "Params: count %llu min_pkt_size: %u max_pkt_size: %u\n",
604 (unsigned long long)pkt_dev
->count
, pkt_dev
->min_pkt_size
,
605 pkt_dev
->max_pkt_size
);
608 " frags: %d delay: %u clone_skb: %d ifname: %s\n",
610 1000 * pkt_dev
->delay_us
+ pkt_dev
->delay_ns
,
611 pkt_dev
->clone_skb
, pkt_dev
->odev
->name
);
613 seq_printf(seq
, " flows: %u flowlen: %u\n", pkt_dev
->cflows
,
616 if (pkt_dev
->flags
& F_IPV6
) {
617 char b1
[128], b2
[128], b3
[128];
618 fmt_ip6(b1
, pkt_dev
->in6_saddr
.s6_addr
);
619 fmt_ip6(b2
, pkt_dev
->min_in6_saddr
.s6_addr
);
620 fmt_ip6(b3
, pkt_dev
->max_in6_saddr
.s6_addr
);
622 " saddr: %s min_saddr: %s max_saddr: %s\n", b1
,
625 fmt_ip6(b1
, pkt_dev
->in6_daddr
.s6_addr
);
626 fmt_ip6(b2
, pkt_dev
->min_in6_daddr
.s6_addr
);
627 fmt_ip6(b3
, pkt_dev
->max_in6_daddr
.s6_addr
);
629 " daddr: %s min_daddr: %s max_daddr: %s\n", b1
,
634 " dst_min: %s dst_max: %s\n src_min: %s src_max: %s\n",
635 pkt_dev
->dst_min
, pkt_dev
->dst_max
, pkt_dev
->src_min
,
638 seq_puts(seq
, " src_mac: ");
640 if (is_zero_ether_addr(pkt_dev
->src_mac
))
641 for (i
= 0; i
< 6; i
++)
642 seq_printf(seq
, "%02X%s", pkt_dev
->odev
->dev_addr
[i
],
645 for (i
= 0; i
< 6; i
++)
646 seq_printf(seq
, "%02X%s", pkt_dev
->src_mac
[i
],
649 seq_printf(seq
, "dst_mac: ");
650 for (i
= 0; i
< 6; i
++)
651 seq_printf(seq
, "%02X%s", pkt_dev
->dst_mac
[i
],
652 i
== 5 ? "\n" : ":");
655 " udp_src_min: %d udp_src_max: %d udp_dst_min: %d udp_dst_max: %d\n",
656 pkt_dev
->udp_src_min
, pkt_dev
->udp_src_max
,
657 pkt_dev
->udp_dst_min
, pkt_dev
->udp_dst_max
);
660 " src_mac_count: %d dst_mac_count: %d\n",
661 pkt_dev
->src_mac_count
, pkt_dev
->dst_mac_count
);
663 if (pkt_dev
->nr_labels
) {
665 seq_printf(seq
, " mpls: ");
666 for (i
= 0; i
< pkt_dev
->nr_labels
; i
++)
667 seq_printf(seq
, "%08x%s", ntohl(pkt_dev
->labels
[i
]),
668 i
== pkt_dev
->nr_labels
-1 ? "\n" : ", ");
671 if (pkt_dev
->vlan_id
!= 0xffff) {
672 seq_printf(seq
, " vlan_id: %u vlan_p: %u vlan_cfi: %u\n",
673 pkt_dev
->vlan_id
, pkt_dev
->vlan_p
, pkt_dev
->vlan_cfi
);
676 if (pkt_dev
->svlan_id
!= 0xffff) {
677 seq_printf(seq
, " svlan_id: %u vlan_p: %u vlan_cfi: %u\n",
678 pkt_dev
->svlan_id
, pkt_dev
->svlan_p
, pkt_dev
->svlan_cfi
);
682 seq_printf(seq
, " tos: 0x%02x\n", pkt_dev
->tos
);
685 if (pkt_dev
->traffic_class
) {
686 seq_printf(seq
, " traffic_class: 0x%02x\n", pkt_dev
->traffic_class
);
689 seq_printf(seq
, " Flags: ");
691 if (pkt_dev
->flags
& F_IPV6
)
692 seq_printf(seq
, "IPV6 ");
694 if (pkt_dev
->flags
& F_IPSRC_RND
)
695 seq_printf(seq
, "IPSRC_RND ");
697 if (pkt_dev
->flags
& F_IPDST_RND
)
698 seq_printf(seq
, "IPDST_RND ");
700 if (pkt_dev
->flags
& F_TXSIZE_RND
)
701 seq_printf(seq
, "TXSIZE_RND ");
703 if (pkt_dev
->flags
& F_UDPSRC_RND
)
704 seq_printf(seq
, "UDPSRC_RND ");
706 if (pkt_dev
->flags
& F_UDPDST_RND
)
707 seq_printf(seq
, "UDPDST_RND ");
709 if (pkt_dev
->flags
& F_MPLS_RND
)
710 seq_printf(seq
, "MPLS_RND ");
712 if (pkt_dev
->cflows
) {
713 if (pkt_dev
->flags
& F_FLOW_SEQ
)
714 seq_printf(seq
, "FLOW_SEQ "); /*in sequence flows*/
716 seq_printf(seq
, "FLOW_RND ");
720 if (pkt_dev
->flags
& F_IPSEC_ON
)
721 seq_printf(seq
, "IPSEC ");
724 if (pkt_dev
->flags
& F_MACSRC_RND
)
725 seq_printf(seq
, "MACSRC_RND ");
727 if (pkt_dev
->flags
& F_MACDST_RND
)
728 seq_printf(seq
, "MACDST_RND ");
730 if (pkt_dev
->flags
& F_VID_RND
)
731 seq_printf(seq
, "VID_RND ");
733 if (pkt_dev
->flags
& F_SVID_RND
)
734 seq_printf(seq
, "SVID_RND ");
738 sa
= pkt_dev
->started_at
;
739 stopped
= pkt_dev
->stopped_at
;
740 if (pkt_dev
->running
)
741 stopped
= now
; /* not really stopped, more like last-running-at */
744 "Current:\n pkts-sofar: %llu errors: %llu\n started: %lluus stopped: %lluus idle: %lluus\n",
745 (unsigned long long)pkt_dev
->sofar
,
746 (unsigned long long)pkt_dev
->errors
, (unsigned long long)sa
,
747 (unsigned long long)stopped
,
748 (unsigned long long)pkt_dev
->idle_acc
);
751 " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n",
752 pkt_dev
->seq_num
, pkt_dev
->cur_dst_mac_offset
,
753 pkt_dev
->cur_src_mac_offset
);
755 if (pkt_dev
->flags
& F_IPV6
) {
756 char b1
[128], b2
[128];
757 fmt_ip6(b1
, pkt_dev
->cur_in6_daddr
.s6_addr
);
758 fmt_ip6(b2
, pkt_dev
->cur_in6_saddr
.s6_addr
);
759 seq_printf(seq
, " cur_saddr: %s cur_daddr: %s\n", b2
, b1
);
761 seq_printf(seq
, " cur_saddr: 0x%x cur_daddr: 0x%x\n",
762 pkt_dev
->cur_saddr
, pkt_dev
->cur_daddr
);
764 seq_printf(seq
, " cur_udp_dst: %d cur_udp_src: %d\n",
765 pkt_dev
->cur_udp_dst
, pkt_dev
->cur_udp_src
);
767 seq_printf(seq
, " flows: %u\n", pkt_dev
->nflows
);
769 if (pkt_dev
->result
[0])
770 seq_printf(seq
, "Result: %s\n", pkt_dev
->result
);
772 seq_printf(seq
, "Result: Idle\n");
778 static int hex32_arg(const char __user
*user_buffer
, unsigned long maxlen
, __u32
*num
)
783 for (; i
< maxlen
; i
++) {
786 if (get_user(c
, &user_buffer
[i
]))
788 if ((c
>= '0') && (c
<= '9'))
790 else if ((c
>= 'a') && (c
<= 'f'))
791 *num
|= c
- 'a' + 10;
792 else if ((c
>= 'A') && (c
<= 'F'))
793 *num
|= c
- 'A' + 10;
800 static int count_trail_chars(const char __user
* user_buffer
,
805 for (i
= 0; i
< maxlen
; i
++) {
807 if (get_user(c
, &user_buffer
[i
]))
825 static unsigned long num_arg(const char __user
* user_buffer
,
826 unsigned long maxlen
, unsigned long *num
)
831 for (; i
< maxlen
; i
++) {
833 if (get_user(c
, &user_buffer
[i
]))
835 if ((c
>= '0') && (c
<= '9')) {
844 static int strn_len(const char __user
* user_buffer
, unsigned int maxlen
)
848 for (; i
< maxlen
; i
++) {
850 if (get_user(c
, &user_buffer
[i
]))
868 static ssize_t
get_labels(const char __user
*buffer
, struct pktgen_dev
*pkt_dev
)
875 pkt_dev
->nr_labels
= 0;
878 len
= hex32_arg(&buffer
[i
], 8, &tmp
);
881 pkt_dev
->labels
[n
] = htonl(tmp
);
882 if (pkt_dev
->labels
[n
] & MPLS_STACK_BOTTOM
)
883 pkt_dev
->flags
|= F_MPLS_RND
;
885 if (get_user(c
, &buffer
[i
]))
889 if (n
>= MAX_MPLS_LABELS
)
893 pkt_dev
->nr_labels
= n
;
897 static ssize_t
pktgen_if_write(struct file
*file
,
898 const char __user
* user_buffer
, size_t count
,
901 struct seq_file
*seq
= (struct seq_file
*)file
->private_data
;
902 struct pktgen_dev
*pkt_dev
= seq
->private;
904 char name
[16], valstr
[32];
905 unsigned long value
= 0;
906 char *pg_result
= NULL
;
910 pg_result
= &(pkt_dev
->result
[0]);
913 printk(KERN_WARNING
"pktgen: wrong command format\n");
918 tmp
= count_trail_chars(&user_buffer
[i
], max
);
920 printk(KERN_WARNING
"pktgen: illegal format\n");
925 /* Read variable name */
927 len
= strn_len(&user_buffer
[i
], sizeof(name
) - 1);
931 memset(name
, 0, sizeof(name
));
932 if (copy_from_user(name
, &user_buffer
[i
], len
))
937 len
= count_trail_chars(&user_buffer
[i
], max
);
945 if (copy_from_user(tb
, user_buffer
, count
))
948 printk(KERN_DEBUG
"pktgen: %s,%lu buffer -:%s:-\n", name
,
949 (unsigned long)count
, tb
);
952 if (!strcmp(name
, "min_pkt_size")) {
953 len
= num_arg(&user_buffer
[i
], 10, &value
);
958 if (value
< 14 + 20 + 8)
960 if (value
!= pkt_dev
->min_pkt_size
) {
961 pkt_dev
->min_pkt_size
= value
;
962 pkt_dev
->cur_pkt_size
= value
;
964 sprintf(pg_result
, "OK: min_pkt_size=%u",
965 pkt_dev
->min_pkt_size
);
969 if (!strcmp(name
, "max_pkt_size")) {
970 len
= num_arg(&user_buffer
[i
], 10, &value
);
975 if (value
< 14 + 20 + 8)
977 if (value
!= pkt_dev
->max_pkt_size
) {
978 pkt_dev
->max_pkt_size
= value
;
979 pkt_dev
->cur_pkt_size
= value
;
981 sprintf(pg_result
, "OK: max_pkt_size=%u",
982 pkt_dev
->max_pkt_size
);
986 /* Shortcut for min = max */
988 if (!strcmp(name
, "pkt_size")) {
989 len
= num_arg(&user_buffer
[i
], 10, &value
);
994 if (value
< 14 + 20 + 8)
996 if (value
!= pkt_dev
->min_pkt_size
) {
997 pkt_dev
->min_pkt_size
= value
;
998 pkt_dev
->max_pkt_size
= value
;
999 pkt_dev
->cur_pkt_size
= value
;
1001 sprintf(pg_result
, "OK: pkt_size=%u", pkt_dev
->min_pkt_size
);
1005 if (!strcmp(name
, "debug")) {
1006 len
= num_arg(&user_buffer
[i
], 10, &value
);
1012 sprintf(pg_result
, "OK: debug=%u", debug
);
1016 if (!strcmp(name
, "frags")) {
1017 len
= num_arg(&user_buffer
[i
], 10, &value
);
1022 pkt_dev
->nfrags
= value
;
1023 sprintf(pg_result
, "OK: frags=%u", pkt_dev
->nfrags
);
1026 if (!strcmp(name
, "delay")) {
1027 len
= num_arg(&user_buffer
[i
], 10, &value
);
1032 if (value
== 0x7FFFFFFF) {
1033 pkt_dev
->delay_us
= 0x7FFFFFFF;
1034 pkt_dev
->delay_ns
= 0;
1036 pkt_dev
->delay_us
= value
/ 1000;
1037 pkt_dev
->delay_ns
= value
% 1000;
1039 sprintf(pg_result
, "OK: delay=%u",
1040 1000 * pkt_dev
->delay_us
+ pkt_dev
->delay_ns
);
1043 if (!strcmp(name
, "udp_src_min")) {
1044 len
= num_arg(&user_buffer
[i
], 10, &value
);
1049 if (value
!= pkt_dev
->udp_src_min
) {
1050 pkt_dev
->udp_src_min
= value
;
1051 pkt_dev
->cur_udp_src
= value
;
1053 sprintf(pg_result
, "OK: udp_src_min=%u", pkt_dev
->udp_src_min
);
1056 if (!strcmp(name
, "udp_dst_min")) {
1057 len
= num_arg(&user_buffer
[i
], 10, &value
);
1062 if (value
!= pkt_dev
->udp_dst_min
) {
1063 pkt_dev
->udp_dst_min
= value
;
1064 pkt_dev
->cur_udp_dst
= value
;
1066 sprintf(pg_result
, "OK: udp_dst_min=%u", pkt_dev
->udp_dst_min
);
1069 if (!strcmp(name
, "udp_src_max")) {
1070 len
= num_arg(&user_buffer
[i
], 10, &value
);
1075 if (value
!= pkt_dev
->udp_src_max
) {
1076 pkt_dev
->udp_src_max
= value
;
1077 pkt_dev
->cur_udp_src
= value
;
1079 sprintf(pg_result
, "OK: udp_src_max=%u", pkt_dev
->udp_src_max
);
1082 if (!strcmp(name
, "udp_dst_max")) {
1083 len
= num_arg(&user_buffer
[i
], 10, &value
);
1088 if (value
!= pkt_dev
->udp_dst_max
) {
1089 pkt_dev
->udp_dst_max
= value
;
1090 pkt_dev
->cur_udp_dst
= value
;
1092 sprintf(pg_result
, "OK: udp_dst_max=%u", pkt_dev
->udp_dst_max
);
1095 if (!strcmp(name
, "clone_skb")) {
1096 len
= num_arg(&user_buffer
[i
], 10, &value
);
1101 pkt_dev
->clone_skb
= value
;
1103 sprintf(pg_result
, "OK: clone_skb=%d", pkt_dev
->clone_skb
);
1106 if (!strcmp(name
, "count")) {
1107 len
= num_arg(&user_buffer
[i
], 10, &value
);
1112 pkt_dev
->count
= value
;
1113 sprintf(pg_result
, "OK: count=%llu",
1114 (unsigned long long)pkt_dev
->count
);
1117 if (!strcmp(name
, "src_mac_count")) {
1118 len
= num_arg(&user_buffer
[i
], 10, &value
);
1123 if (pkt_dev
->src_mac_count
!= value
) {
1124 pkt_dev
->src_mac_count
= value
;
1125 pkt_dev
->cur_src_mac_offset
= 0;
1127 sprintf(pg_result
, "OK: src_mac_count=%d",
1128 pkt_dev
->src_mac_count
);
1131 if (!strcmp(name
, "dst_mac_count")) {
1132 len
= num_arg(&user_buffer
[i
], 10, &value
);
1137 if (pkt_dev
->dst_mac_count
!= value
) {
1138 pkt_dev
->dst_mac_count
= value
;
1139 pkt_dev
->cur_dst_mac_offset
= 0;
1141 sprintf(pg_result
, "OK: dst_mac_count=%d",
1142 pkt_dev
->dst_mac_count
);
1145 if (!strcmp(name
, "flag")) {
1148 len
= strn_len(&user_buffer
[i
], sizeof(f
) - 1);
1152 if (copy_from_user(f
, &user_buffer
[i
], len
))
1155 if (strcmp(f
, "IPSRC_RND") == 0)
1156 pkt_dev
->flags
|= F_IPSRC_RND
;
1158 else if (strcmp(f
, "!IPSRC_RND") == 0)
1159 pkt_dev
->flags
&= ~F_IPSRC_RND
;
1161 else if (strcmp(f
, "TXSIZE_RND") == 0)
1162 pkt_dev
->flags
|= F_TXSIZE_RND
;
1164 else if (strcmp(f
, "!TXSIZE_RND") == 0)
1165 pkt_dev
->flags
&= ~F_TXSIZE_RND
;
1167 else if (strcmp(f
, "IPDST_RND") == 0)
1168 pkt_dev
->flags
|= F_IPDST_RND
;
1170 else if (strcmp(f
, "!IPDST_RND") == 0)
1171 pkt_dev
->flags
&= ~F_IPDST_RND
;
1173 else if (strcmp(f
, "UDPSRC_RND") == 0)
1174 pkt_dev
->flags
|= F_UDPSRC_RND
;
1176 else if (strcmp(f
, "!UDPSRC_RND") == 0)
1177 pkt_dev
->flags
&= ~F_UDPSRC_RND
;
1179 else if (strcmp(f
, "UDPDST_RND") == 0)
1180 pkt_dev
->flags
|= F_UDPDST_RND
;
1182 else if (strcmp(f
, "!UDPDST_RND") == 0)
1183 pkt_dev
->flags
&= ~F_UDPDST_RND
;
1185 else if (strcmp(f
, "MACSRC_RND") == 0)
1186 pkt_dev
->flags
|= F_MACSRC_RND
;
1188 else if (strcmp(f
, "!MACSRC_RND") == 0)
1189 pkt_dev
->flags
&= ~F_MACSRC_RND
;
1191 else if (strcmp(f
, "MACDST_RND") == 0)
1192 pkt_dev
->flags
|= F_MACDST_RND
;
1194 else if (strcmp(f
, "!MACDST_RND") == 0)
1195 pkt_dev
->flags
&= ~F_MACDST_RND
;
1197 else if (strcmp(f
, "MPLS_RND") == 0)
1198 pkt_dev
->flags
|= F_MPLS_RND
;
1200 else if (strcmp(f
, "!MPLS_RND") == 0)
1201 pkt_dev
->flags
&= ~F_MPLS_RND
;
1203 else if (strcmp(f
, "VID_RND") == 0)
1204 pkt_dev
->flags
|= F_VID_RND
;
1206 else if (strcmp(f
, "!VID_RND") == 0)
1207 pkt_dev
->flags
&= ~F_VID_RND
;
1209 else if (strcmp(f
, "SVID_RND") == 0)
1210 pkt_dev
->flags
|= F_SVID_RND
;
1212 else if (strcmp(f
, "!SVID_RND") == 0)
1213 pkt_dev
->flags
&= ~F_SVID_RND
;
1215 else if (strcmp(f
, "FLOW_SEQ") == 0)
1216 pkt_dev
->flags
|= F_FLOW_SEQ
;
1219 else if (strcmp(f
, "IPSEC") == 0)
1220 pkt_dev
->flags
|= F_IPSEC_ON
;
1223 else if (strcmp(f
, "!IPV6") == 0)
1224 pkt_dev
->flags
&= ~F_IPV6
;
1228 "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
1230 "IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
1231 "MACSRC_RND, MACDST_RND, TXSIZE_RND, IPV6, MPLS_RND, VID_RND, SVID_RND, FLOW_SEQ, IPSEC\n");
1234 sprintf(pg_result
, "OK: flags=0x%x", pkt_dev
->flags
);
1237 if (!strcmp(name
, "dst_min") || !strcmp(name
, "dst")) {
1238 len
= strn_len(&user_buffer
[i
], sizeof(pkt_dev
->dst_min
) - 1);
1243 if (copy_from_user(buf
, &user_buffer
[i
], len
))
1246 if (strcmp(buf
, pkt_dev
->dst_min
) != 0) {
1247 memset(pkt_dev
->dst_min
, 0, sizeof(pkt_dev
->dst_min
));
1248 strncpy(pkt_dev
->dst_min
, buf
, len
);
1249 pkt_dev
->daddr_min
= in_aton(pkt_dev
->dst_min
);
1250 pkt_dev
->cur_daddr
= pkt_dev
->daddr_min
;
1253 printk(KERN_DEBUG
"pktgen: dst_min set to: %s\n",
1256 sprintf(pg_result
, "OK: dst_min=%s", pkt_dev
->dst_min
);
1259 if (!strcmp(name
, "dst_max")) {
1260 len
= strn_len(&user_buffer
[i
], sizeof(pkt_dev
->dst_max
) - 1);
1265 if (copy_from_user(buf
, &user_buffer
[i
], len
))
1269 if (strcmp(buf
, pkt_dev
->dst_max
) != 0) {
1270 memset(pkt_dev
->dst_max
, 0, sizeof(pkt_dev
->dst_max
));
1271 strncpy(pkt_dev
->dst_max
, buf
, len
);
1272 pkt_dev
->daddr_max
= in_aton(pkt_dev
->dst_max
);
1273 pkt_dev
->cur_daddr
= pkt_dev
->daddr_max
;
1276 printk(KERN_DEBUG
"pktgen: dst_max set to: %s\n",
1279 sprintf(pg_result
, "OK: dst_max=%s", pkt_dev
->dst_max
);
1282 if (!strcmp(name
, "dst6")) {
1283 len
= strn_len(&user_buffer
[i
], sizeof(buf
) - 1);
1287 pkt_dev
->flags
|= F_IPV6
;
1289 if (copy_from_user(buf
, &user_buffer
[i
], len
))
1293 scan_ip6(buf
, pkt_dev
->in6_daddr
.s6_addr
);
1294 fmt_ip6(buf
, pkt_dev
->in6_daddr
.s6_addr
);
1296 ipv6_addr_copy(&pkt_dev
->cur_in6_daddr
, &pkt_dev
->in6_daddr
);
1299 printk(KERN_DEBUG
"pktgen: dst6 set to: %s\n", buf
);
1302 sprintf(pg_result
, "OK: dst6=%s", buf
);
1305 if (!strcmp(name
, "dst6_min")) {
1306 len
= strn_len(&user_buffer
[i
], sizeof(buf
) - 1);
1310 pkt_dev
->flags
|= F_IPV6
;
1312 if (copy_from_user(buf
, &user_buffer
[i
], len
))
1316 scan_ip6(buf
, pkt_dev
->min_in6_daddr
.s6_addr
);
1317 fmt_ip6(buf
, pkt_dev
->min_in6_daddr
.s6_addr
);
1319 ipv6_addr_copy(&pkt_dev
->cur_in6_daddr
,
1320 &pkt_dev
->min_in6_daddr
);
1322 printk(KERN_DEBUG
"pktgen: dst6_min set to: %s\n", buf
);
1325 sprintf(pg_result
, "OK: dst6_min=%s", buf
);
1328 if (!strcmp(name
, "dst6_max")) {
1329 len
= strn_len(&user_buffer
[i
], sizeof(buf
) - 1);
1333 pkt_dev
->flags
|= F_IPV6
;
1335 if (copy_from_user(buf
, &user_buffer
[i
], len
))
1339 scan_ip6(buf
, pkt_dev
->max_in6_daddr
.s6_addr
);
1340 fmt_ip6(buf
, pkt_dev
->max_in6_daddr
.s6_addr
);
1343 printk(KERN_DEBUG
"pktgen: dst6_max set to: %s\n", buf
);
1346 sprintf(pg_result
, "OK: dst6_max=%s", buf
);
1349 if (!strcmp(name
, "src6")) {
1350 len
= strn_len(&user_buffer
[i
], sizeof(buf
) - 1);
1354 pkt_dev
->flags
|= F_IPV6
;
1356 if (copy_from_user(buf
, &user_buffer
[i
], len
))
1360 scan_ip6(buf
, pkt_dev
->in6_saddr
.s6_addr
);
1361 fmt_ip6(buf
, pkt_dev
->in6_saddr
.s6_addr
);
1363 ipv6_addr_copy(&pkt_dev
->cur_in6_saddr
, &pkt_dev
->in6_saddr
);
1366 printk(KERN_DEBUG
"pktgen: src6 set to: %s\n", buf
);
1369 sprintf(pg_result
, "OK: src6=%s", buf
);
1372 if (!strcmp(name
, "src_min")) {
1373 len
= strn_len(&user_buffer
[i
], sizeof(pkt_dev
->src_min
) - 1);
1377 if (copy_from_user(buf
, &user_buffer
[i
], len
))
1380 if (strcmp(buf
, pkt_dev
->src_min
) != 0) {
1381 memset(pkt_dev
->src_min
, 0, sizeof(pkt_dev
->src_min
));
1382 strncpy(pkt_dev
->src_min
, buf
, len
);
1383 pkt_dev
->saddr_min
= in_aton(pkt_dev
->src_min
);
1384 pkt_dev
->cur_saddr
= pkt_dev
->saddr_min
;
1387 printk(KERN_DEBUG
"pktgen: src_min set to: %s\n",
1390 sprintf(pg_result
, "OK: src_min=%s", pkt_dev
->src_min
);
1393 if (!strcmp(name
, "src_max")) {
1394 len
= strn_len(&user_buffer
[i
], sizeof(pkt_dev
->src_max
) - 1);
1398 if (copy_from_user(buf
, &user_buffer
[i
], len
))
1401 if (strcmp(buf
, pkt_dev
->src_max
) != 0) {
1402 memset(pkt_dev
->src_max
, 0, sizeof(pkt_dev
->src_max
));
1403 strncpy(pkt_dev
->src_max
, buf
, len
);
1404 pkt_dev
->saddr_max
= in_aton(pkt_dev
->src_max
);
1405 pkt_dev
->cur_saddr
= pkt_dev
->saddr_max
;
1408 printk(KERN_DEBUG
"pktgen: src_max set to: %s\n",
1411 sprintf(pg_result
, "OK: src_max=%s", pkt_dev
->src_max
);
1414 if (!strcmp(name
, "dst_mac")) {
1416 unsigned char old_dmac
[ETH_ALEN
];
1417 unsigned char *m
= pkt_dev
->dst_mac
;
1418 memcpy(old_dmac
, pkt_dev
->dst_mac
, ETH_ALEN
);
1420 len
= strn_len(&user_buffer
[i
], sizeof(valstr
) - 1);
1424 memset(valstr
, 0, sizeof(valstr
));
1425 if (copy_from_user(valstr
, &user_buffer
[i
], len
))
1429 for (*m
= 0; *v
&& m
< pkt_dev
->dst_mac
+ 6; v
++) {
1430 if (*v
>= '0' && *v
<= '9') {
1434 if (*v
>= 'A' && *v
<= 'F') {
1436 *m
+= *v
- 'A' + 10;
1438 if (*v
>= 'a' && *v
<= 'f') {
1440 *m
+= *v
- 'a' + 10;
1448 /* Set up Dest MAC */
1449 if (compare_ether_addr(old_dmac
, pkt_dev
->dst_mac
))
1450 memcpy(&(pkt_dev
->hh
[0]), pkt_dev
->dst_mac
, ETH_ALEN
);
1452 sprintf(pg_result
, "OK: dstmac");
1455 if (!strcmp(name
, "src_mac")) {
1457 unsigned char old_smac
[ETH_ALEN
];
1458 unsigned char *m
= pkt_dev
->src_mac
;
1460 memcpy(old_smac
, pkt_dev
->src_mac
, ETH_ALEN
);
1462 len
= strn_len(&user_buffer
[i
], sizeof(valstr
) - 1);
1466 memset(valstr
, 0, sizeof(valstr
));
1467 if (copy_from_user(valstr
, &user_buffer
[i
], len
))
1471 for (*m
= 0; *v
&& m
< pkt_dev
->src_mac
+ 6; v
++) {
1472 if (*v
>= '0' && *v
<= '9') {
1476 if (*v
>= 'A' && *v
<= 'F') {
1478 *m
+= *v
- 'A' + 10;
1480 if (*v
>= 'a' && *v
<= 'f') {
1482 *m
+= *v
- 'a' + 10;
1490 /* Set up Src MAC */
1491 if (compare_ether_addr(old_smac
, pkt_dev
->src_mac
))
1492 memcpy(&(pkt_dev
->hh
[6]), pkt_dev
->src_mac
, ETH_ALEN
);
1494 sprintf(pg_result
, "OK: srcmac");
1498 if (!strcmp(name
, "clear_counters")) {
1499 pktgen_clear_counters(pkt_dev
);
1500 sprintf(pg_result
, "OK: Clearing counters.\n");
1504 if (!strcmp(name
, "flows")) {
1505 len
= num_arg(&user_buffer
[i
], 10, &value
);
1510 if (value
> MAX_CFLOWS
)
1513 pkt_dev
->cflows
= value
;
1514 sprintf(pg_result
, "OK: flows=%u", pkt_dev
->cflows
);
1518 if (!strcmp(name
, "flowlen")) {
1519 len
= num_arg(&user_buffer
[i
], 10, &value
);
1524 pkt_dev
->lflow
= value
;
1525 sprintf(pg_result
, "OK: flowlen=%u", pkt_dev
->lflow
);
1529 if (!strcmp(name
, "mpls")) {
1531 len
= get_labels(&user_buffer
[i
], pkt_dev
);
1532 if (len
< 0) { return len
; }
1534 offset
= sprintf(pg_result
, "OK: mpls=");
1535 for (n
= 0; n
< pkt_dev
->nr_labels
; n
++)
1536 offset
+= sprintf(pg_result
+ offset
,
1537 "%08x%s", ntohl(pkt_dev
->labels
[n
]),
1538 n
== pkt_dev
->nr_labels
-1 ? "" : ",");
1540 if (pkt_dev
->nr_labels
&& pkt_dev
->vlan_id
!= 0xffff) {
1541 pkt_dev
->vlan_id
= 0xffff; /* turn off VLAN/SVLAN */
1542 pkt_dev
->svlan_id
= 0xffff;
1545 printk(KERN_DEBUG
"pktgen: VLAN/SVLAN auto turned off\n");
1550 if (!strcmp(name
, "vlan_id")) {
1551 len
= num_arg(&user_buffer
[i
], 4, &value
);
1556 if (value
<= 4095) {
1557 pkt_dev
->vlan_id
= value
; /* turn on VLAN */
1560 printk(KERN_DEBUG
"pktgen: VLAN turned on\n");
1562 if (debug
&& pkt_dev
->nr_labels
)
1563 printk(KERN_DEBUG
"pktgen: MPLS auto turned off\n");
1565 pkt_dev
->nr_labels
= 0; /* turn off MPLS */
1566 sprintf(pg_result
, "OK: vlan_id=%u", pkt_dev
->vlan_id
);
1568 pkt_dev
->vlan_id
= 0xffff; /* turn off VLAN/SVLAN */
1569 pkt_dev
->svlan_id
= 0xffff;
1572 printk(KERN_DEBUG
"pktgen: VLAN/SVLAN turned off\n");
1577 if (!strcmp(name
, "vlan_p")) {
1578 len
= num_arg(&user_buffer
[i
], 1, &value
);
1583 if ((value
<= 7) && (pkt_dev
->vlan_id
!= 0xffff)) {
1584 pkt_dev
->vlan_p
= value
;
1585 sprintf(pg_result
, "OK: vlan_p=%u", pkt_dev
->vlan_p
);
1587 sprintf(pg_result
, "ERROR: vlan_p must be 0-7");
1592 if (!strcmp(name
, "vlan_cfi")) {
1593 len
= num_arg(&user_buffer
[i
], 1, &value
);
1598 if ((value
<= 1) && (pkt_dev
->vlan_id
!= 0xffff)) {
1599 pkt_dev
->vlan_cfi
= value
;
1600 sprintf(pg_result
, "OK: vlan_cfi=%u", pkt_dev
->vlan_cfi
);
1602 sprintf(pg_result
, "ERROR: vlan_cfi must be 0-1");
1607 if (!strcmp(name
, "svlan_id")) {
1608 len
= num_arg(&user_buffer
[i
], 4, &value
);
1613 if ((value
<= 4095) && ((pkt_dev
->vlan_id
!= 0xffff))) {
1614 pkt_dev
->svlan_id
= value
; /* turn on SVLAN */
1617 printk(KERN_DEBUG
"pktgen: SVLAN turned on\n");
1619 if (debug
&& pkt_dev
->nr_labels
)
1620 printk(KERN_DEBUG
"pktgen: MPLS auto turned off\n");
1622 pkt_dev
->nr_labels
= 0; /* turn off MPLS */
1623 sprintf(pg_result
, "OK: svlan_id=%u", pkt_dev
->svlan_id
);
1625 pkt_dev
->vlan_id
= 0xffff; /* turn off VLAN/SVLAN */
1626 pkt_dev
->svlan_id
= 0xffff;
1629 printk(KERN_DEBUG
"pktgen: VLAN/SVLAN turned off\n");
1634 if (!strcmp(name
, "svlan_p")) {
1635 len
= num_arg(&user_buffer
[i
], 1, &value
);
1640 if ((value
<= 7) && (pkt_dev
->svlan_id
!= 0xffff)) {
1641 pkt_dev
->svlan_p
= value
;
1642 sprintf(pg_result
, "OK: svlan_p=%u", pkt_dev
->svlan_p
);
1644 sprintf(pg_result
, "ERROR: svlan_p must be 0-7");
1649 if (!strcmp(name
, "svlan_cfi")) {
1650 len
= num_arg(&user_buffer
[i
], 1, &value
);
1655 if ((value
<= 1) && (pkt_dev
->svlan_id
!= 0xffff)) {
1656 pkt_dev
->svlan_cfi
= value
;
1657 sprintf(pg_result
, "OK: svlan_cfi=%u", pkt_dev
->svlan_cfi
);
1659 sprintf(pg_result
, "ERROR: svlan_cfi must be 0-1");
1664 if (!strcmp(name
, "tos")) {
1665 __u32 tmp_value
= 0;
1666 len
= hex32_arg(&user_buffer
[i
], 2, &tmp_value
);
1672 pkt_dev
->tos
= tmp_value
;
1673 sprintf(pg_result
, "OK: tos=0x%02x", pkt_dev
->tos
);
1675 sprintf(pg_result
, "ERROR: tos must be 00-ff");
1680 if (!strcmp(name
, "traffic_class")) {
1681 __u32 tmp_value
= 0;
1682 len
= hex32_arg(&user_buffer
[i
], 2, &tmp_value
);
1688 pkt_dev
->traffic_class
= tmp_value
;
1689 sprintf(pg_result
, "OK: traffic_class=0x%02x", pkt_dev
->traffic_class
);
1691 sprintf(pg_result
, "ERROR: traffic_class must be 00-ff");
1696 sprintf(pkt_dev
->result
, "No such parameter \"%s\"", name
);
1700 static int pktgen_if_open(struct inode
*inode
, struct file
*file
)
1702 return single_open(file
, pktgen_if_show
, PDE(inode
)->data
);
1705 static const struct file_operations pktgen_if_fops
= {
1706 .owner
= THIS_MODULE
,
1707 .open
= pktgen_if_open
,
1709 .llseek
= seq_lseek
,
1710 .write
= pktgen_if_write
,
1711 .release
= single_release
,
1714 static int pktgen_thread_show(struct seq_file
*seq
, void *v
)
1716 struct pktgen_thread
*t
= seq
->private;
1717 struct pktgen_dev
*pkt_dev
;
1721 seq_printf(seq
, "Name: %s max_before_softirq: %d\n",
1722 t
->tsk
->comm
, t
->max_before_softirq
);
1724 seq_printf(seq
, "Running: ");
1727 list_for_each_entry(pkt_dev
, &t
->if_list
, list
)
1728 if (pkt_dev
->running
)
1729 seq_printf(seq
, "%s ", pkt_dev
->odev
->name
);
1731 seq_printf(seq
, "\nStopped: ");
1733 list_for_each_entry(pkt_dev
, &t
->if_list
, list
)
1734 if (!pkt_dev
->running
)
1735 seq_printf(seq
, "%s ", pkt_dev
->odev
->name
);
1738 seq_printf(seq
, "\nResult: %s\n", t
->result
);
1740 seq_printf(seq
, "\nResult: NA\n");
1747 static ssize_t
pktgen_thread_write(struct file
*file
,
1748 const char __user
* user_buffer
,
1749 size_t count
, loff_t
* offset
)
1751 struct seq_file
*seq
= (struct seq_file
*)file
->private_data
;
1752 struct pktgen_thread
*t
= seq
->private;
1753 int i
= 0, max
, len
, ret
;
1756 unsigned long value
= 0;
1759 // sprintf(pg_result, "Wrong command format");
1764 len
= count_trail_chars(&user_buffer
[i
], max
);
1770 /* Read variable name */
1772 len
= strn_len(&user_buffer
[i
], sizeof(name
) - 1);
1776 memset(name
, 0, sizeof(name
));
1777 if (copy_from_user(name
, &user_buffer
[i
], len
))
1782 len
= count_trail_chars(&user_buffer
[i
], max
);
1789 printk(KERN_DEBUG
"pktgen: t=%s, count=%lu\n",
1790 name
, (unsigned long)count
);
1793 printk(KERN_ERR
"pktgen: ERROR: No thread\n");
1798 pg_result
= &(t
->result
[0]);
1800 if (!strcmp(name
, "add_device")) {
1803 len
= strn_len(&user_buffer
[i
], sizeof(f
) - 1);
1808 if (copy_from_user(f
, &user_buffer
[i
], len
))
1811 mutex_lock(&pktgen_thread_lock
);
1812 pktgen_add_device(t
, f
);
1813 mutex_unlock(&pktgen_thread_lock
);
1815 sprintf(pg_result
, "OK: add_device=%s", f
);
1819 if (!strcmp(name
, "rem_device_all")) {
1820 mutex_lock(&pktgen_thread_lock
);
1821 t
->control
|= T_REMDEVALL
;
1822 mutex_unlock(&pktgen_thread_lock
);
1823 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
1825 sprintf(pg_result
, "OK: rem_device_all");
1829 if (!strcmp(name
, "max_before_softirq")) {
1830 len
= num_arg(&user_buffer
[i
], 10, &value
);
1831 mutex_lock(&pktgen_thread_lock
);
1832 t
->max_before_softirq
= value
;
1833 mutex_unlock(&pktgen_thread_lock
);
1835 sprintf(pg_result
, "OK: max_before_softirq=%lu", value
);
1844 static int pktgen_thread_open(struct inode
*inode
, struct file
*file
)
1846 return single_open(file
, pktgen_thread_show
, PDE(inode
)->data
);
1849 static const struct file_operations pktgen_thread_fops
= {
1850 .owner
= THIS_MODULE
,
1851 .open
= pktgen_thread_open
,
1853 .llseek
= seq_lseek
,
1854 .write
= pktgen_thread_write
,
1855 .release
= single_release
,
1858 /* Think find or remove for NN */
1859 static struct pktgen_dev
*__pktgen_NN_threads(const char *ifname
, int remove
)
1861 struct pktgen_thread
*t
;
1862 struct pktgen_dev
*pkt_dev
= NULL
;
1864 list_for_each_entry(t
, &pktgen_threads
, th_list
) {
1865 pkt_dev
= pktgen_find_dev(t
, ifname
);
1869 pkt_dev
->removal_mark
= 1;
1870 t
->control
|= T_REMDEV
;
1880 * mark a device for removal
1882 static void pktgen_mark_device(const char *ifname
)
1884 struct pktgen_dev
*pkt_dev
= NULL
;
1885 const int max_tries
= 10, msec_per_try
= 125;
1888 mutex_lock(&pktgen_thread_lock
);
1889 pr_debug("pktgen: pktgen_mark_device marking %s for removal\n", ifname
);
1893 pkt_dev
= __pktgen_NN_threads(ifname
, REMOVE
);
1894 if (pkt_dev
== NULL
)
1895 break; /* success */
1897 mutex_unlock(&pktgen_thread_lock
);
1898 pr_debug("pktgen: pktgen_mark_device waiting for %s "
1899 "to disappear....\n", ifname
);
1900 schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try
));
1901 mutex_lock(&pktgen_thread_lock
);
1903 if (++i
>= max_tries
) {
1904 printk(KERN_ERR
"pktgen_mark_device: timed out after "
1905 "waiting %d msec for device %s to be removed\n",
1906 msec_per_try
* i
, ifname
);
1912 mutex_unlock(&pktgen_thread_lock
);
1915 static void pktgen_change_name(struct net_device
*dev
)
1917 struct pktgen_thread
*t
;
1919 list_for_each_entry(t
, &pktgen_threads
, th_list
) {
1920 struct pktgen_dev
*pkt_dev
;
1922 list_for_each_entry(pkt_dev
, &t
->if_list
, list
) {
1923 if (pkt_dev
->odev
!= dev
)
1926 remove_proc_entry(pkt_dev
->entry
->name
, pg_proc_dir
);
1928 pkt_dev
->entry
= create_proc_entry(dev
->name
, 0600,
1930 if (!pkt_dev
->entry
)
1931 printk(KERN_ERR
"pktgen: can't move proc "
1932 " entry for '%s'\n", dev
->name
);
1938 static int pktgen_device_event(struct notifier_block
*unused
,
1939 unsigned long event
, void *ptr
)
1941 struct net_device
*dev
= ptr
;
1943 /* It is OK that we do not hold the group lock right now,
1944 * as we run under the RTNL lock.
1948 case NETDEV_CHANGENAME
:
1949 pktgen_change_name(dev
);
1952 case NETDEV_UNREGISTER
:
1953 pktgen_mark_device(dev
->name
);
1960 /* Associate pktgen_dev with a device. */
1962 static int pktgen_setup_dev(struct pktgen_dev
*pkt_dev
, const char *ifname
)
1964 struct net_device
*odev
;
1967 /* Clean old setups */
1968 if (pkt_dev
->odev
) {
1969 dev_put(pkt_dev
->odev
);
1970 pkt_dev
->odev
= NULL
;
1973 odev
= dev_get_by_name(ifname
);
1975 printk(KERN_ERR
"pktgen: no such netdevice: \"%s\"\n", ifname
);
1979 if (odev
->type
!= ARPHRD_ETHER
) {
1980 printk(KERN_ERR
"pktgen: not an ethernet device: \"%s\"\n", ifname
);
1982 } else if (!netif_running(odev
)) {
1983 printk(KERN_ERR
"pktgen: device is down: \"%s\"\n", ifname
);
1986 pkt_dev
->odev
= odev
;
1994 /* Read pkt_dev from the interface and set up internal pktgen_dev
1995 * structure to have the right information to create/send packets
1997 static void pktgen_setup_inject(struct pktgen_dev
*pkt_dev
)
1999 if (!pkt_dev
->odev
) {
2000 printk(KERN_ERR
"pktgen: ERROR: pkt_dev->odev == NULL in "
2002 sprintf(pkt_dev
->result
,
2003 "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
2007 /* Default to the interface's mac if not explicitly set. */
2009 if (is_zero_ether_addr(pkt_dev
->src_mac
))
2010 memcpy(&(pkt_dev
->hh
[6]), pkt_dev
->odev
->dev_addr
, ETH_ALEN
);
2012 /* Set up Dest MAC */
2013 memcpy(&(pkt_dev
->hh
[0]), pkt_dev
->dst_mac
, ETH_ALEN
);
2015 /* Set up pkt size */
2016 pkt_dev
->cur_pkt_size
= pkt_dev
->min_pkt_size
;
2018 if (pkt_dev
->flags
& F_IPV6
) {
2020 * Skip this automatic address setting until locks or functions
2025 int i
, set
= 0, err
= 1;
2026 struct inet6_dev
*idev
;
2028 for (i
= 0; i
< IN6_ADDR_HSIZE
; i
++)
2029 if (pkt_dev
->cur_in6_saddr
.s6_addr
[i
]) {
2037 * Use linklevel address if unconfigured.
2039 * use ipv6_get_lladdr if/when it's get exported
2043 if ((idev
= __in6_dev_get(pkt_dev
->odev
)) != NULL
) {
2044 struct inet6_ifaddr
*ifp
;
2046 read_lock_bh(&idev
->lock
);
2047 for (ifp
= idev
->addr_list
; ifp
;
2048 ifp
= ifp
->if_next
) {
2049 if (ifp
->scope
== IFA_LINK
2051 flags
& IFA_F_TENTATIVE
)) {
2052 ipv6_addr_copy(&pkt_dev
->
2059 read_unlock_bh(&idev
->lock
);
2063 printk(KERN_ERR
"pktgen: ERROR: IPv6 link "
2064 "address not availble.\n");
2068 pkt_dev
->saddr_min
= 0;
2069 pkt_dev
->saddr_max
= 0;
2070 if (strlen(pkt_dev
->src_min
) == 0) {
2072 struct in_device
*in_dev
;
2075 in_dev
= __in_dev_get_rcu(pkt_dev
->odev
);
2077 if (in_dev
->ifa_list
) {
2078 pkt_dev
->saddr_min
=
2079 in_dev
->ifa_list
->ifa_address
;
2080 pkt_dev
->saddr_max
= pkt_dev
->saddr_min
;
2085 pkt_dev
->saddr_min
= in_aton(pkt_dev
->src_min
);
2086 pkt_dev
->saddr_max
= in_aton(pkt_dev
->src_max
);
2089 pkt_dev
->daddr_min
= in_aton(pkt_dev
->dst_min
);
2090 pkt_dev
->daddr_max
= in_aton(pkt_dev
->dst_max
);
2092 /* Initialize current values. */
2093 pkt_dev
->cur_dst_mac_offset
= 0;
2094 pkt_dev
->cur_src_mac_offset
= 0;
2095 pkt_dev
->cur_saddr
= pkt_dev
->saddr_min
;
2096 pkt_dev
->cur_daddr
= pkt_dev
->daddr_min
;
2097 pkt_dev
->cur_udp_dst
= pkt_dev
->udp_dst_min
;
2098 pkt_dev
->cur_udp_src
= pkt_dev
->udp_src_min
;
2099 pkt_dev
->nflows
= 0;
2102 static void spin(struct pktgen_dev
*pkt_dev
, __u64 spin_until_us
)
2107 start
= now
= getCurUs();
2108 printk(KERN_INFO
"sleeping for %d\n", (int)(spin_until_us
- now
));
2109 while (now
< spin_until_us
) {
2110 /* TODO: optimize sleeping behavior */
2111 if (spin_until_us
- now
> jiffies_to_usecs(1) + 1)
2112 schedule_timeout_interruptible(1);
2113 else if (spin_until_us
- now
> 100) {
2115 if (!pkt_dev
->running
)
2124 pkt_dev
->idle_acc
+= now
- start
;
2127 static inline void set_pkt_overhead(struct pktgen_dev
*pkt_dev
)
2129 pkt_dev
->pkt_overhead
= 0;
2130 pkt_dev
->pkt_overhead
+= pkt_dev
->nr_labels
*sizeof(u32
);
2131 pkt_dev
->pkt_overhead
+= VLAN_TAG_SIZE(pkt_dev
);
2132 pkt_dev
->pkt_overhead
+= SVLAN_TAG_SIZE(pkt_dev
);
2135 static inline int f_seen(struct pktgen_dev
*pkt_dev
, int flow
)
2138 if (pkt_dev
->flows
[flow
].flags
& F_INIT
)
2144 static inline int f_pick(struct pktgen_dev
*pkt_dev
)
2146 int flow
= pkt_dev
->curfl
;
2148 if (pkt_dev
->flags
& F_FLOW_SEQ
) {
2149 if (pkt_dev
->flows
[flow
].count
>= pkt_dev
->lflow
) {
2151 pkt_dev
->flows
[flow
].count
= 0;
2152 pkt_dev
->curfl
+= 1;
2153 if (pkt_dev
->curfl
>= pkt_dev
->cflows
)
2154 pkt_dev
->curfl
= 0; /*reset */
2157 flow
= random32() % pkt_dev
->cflows
;
2159 if (pkt_dev
->flows
[flow
].count
> pkt_dev
->lflow
)
2160 pkt_dev
->flows
[flow
].count
= 0;
2163 return pkt_dev
->curfl
;
2168 /* If there was already an IPSEC SA, we keep it as is, else
2169 * we go look for it ...
2171 static void get_ipsec_sa(struct pktgen_dev
*pkt_dev
, int flow
)
2173 struct xfrm_state
*x
= pkt_dev
->flows
[flow
].x
;
2175 /*slow path: we dont already have xfrm_state*/
2176 x
= xfrm_stateonly_find((xfrm_address_t
*)&pkt_dev
->cur_daddr
,
2177 (xfrm_address_t
*)&pkt_dev
->cur_saddr
,
2180 pkt_dev
->ipsproto
, 0);
2182 pkt_dev
->flows
[flow
].x
= x
;
2183 set_pkt_overhead(pkt_dev
);
2184 pkt_dev
->pkt_overhead
+=x
->props
.header_len
;
2190 /* Increment/randomize headers according to flags and current values
2191 * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
2193 static void mod_cur_headers(struct pktgen_dev
*pkt_dev
)
2199 if (pkt_dev
->cflows
)
2200 flow
= f_pick(pkt_dev
);
2202 /* Deal with source MAC */
2203 if (pkt_dev
->src_mac_count
> 1) {
2207 if (pkt_dev
->flags
& F_MACSRC_RND
)
2208 mc
= random32() % pkt_dev
->src_mac_count
;
2210 mc
= pkt_dev
->cur_src_mac_offset
++;
2211 if (pkt_dev
->cur_src_mac_offset
>
2212 pkt_dev
->src_mac_count
)
2213 pkt_dev
->cur_src_mac_offset
= 0;
2216 tmp
= pkt_dev
->src_mac
[5] + (mc
& 0xFF);
2217 pkt_dev
->hh
[11] = tmp
;
2218 tmp
= (pkt_dev
->src_mac
[4] + ((mc
>> 8) & 0xFF) + (tmp
>> 8));
2219 pkt_dev
->hh
[10] = tmp
;
2220 tmp
= (pkt_dev
->src_mac
[3] + ((mc
>> 16) & 0xFF) + (tmp
>> 8));
2221 pkt_dev
->hh
[9] = tmp
;
2222 tmp
= (pkt_dev
->src_mac
[2] + ((mc
>> 24) & 0xFF) + (tmp
>> 8));
2223 pkt_dev
->hh
[8] = tmp
;
2224 tmp
= (pkt_dev
->src_mac
[1] + (tmp
>> 8));
2225 pkt_dev
->hh
[7] = tmp
;
2228 /* Deal with Destination MAC */
2229 if (pkt_dev
->dst_mac_count
> 1) {
2233 if (pkt_dev
->flags
& F_MACDST_RND
)
2234 mc
= random32() % pkt_dev
->dst_mac_count
;
2237 mc
= pkt_dev
->cur_dst_mac_offset
++;
2238 if (pkt_dev
->cur_dst_mac_offset
>
2239 pkt_dev
->dst_mac_count
) {
2240 pkt_dev
->cur_dst_mac_offset
= 0;
2244 tmp
= pkt_dev
->dst_mac
[5] + (mc
& 0xFF);
2245 pkt_dev
->hh
[5] = tmp
;
2246 tmp
= (pkt_dev
->dst_mac
[4] + ((mc
>> 8) & 0xFF) + (tmp
>> 8));
2247 pkt_dev
->hh
[4] = tmp
;
2248 tmp
= (pkt_dev
->dst_mac
[3] + ((mc
>> 16) & 0xFF) + (tmp
>> 8));
2249 pkt_dev
->hh
[3] = tmp
;
2250 tmp
= (pkt_dev
->dst_mac
[2] + ((mc
>> 24) & 0xFF) + (tmp
>> 8));
2251 pkt_dev
->hh
[2] = tmp
;
2252 tmp
= (pkt_dev
->dst_mac
[1] + (tmp
>> 8));
2253 pkt_dev
->hh
[1] = tmp
;
2256 if (pkt_dev
->flags
& F_MPLS_RND
) {
2258 for (i
= 0; i
< pkt_dev
->nr_labels
; i
++)
2259 if (pkt_dev
->labels
[i
] & MPLS_STACK_BOTTOM
)
2260 pkt_dev
->labels
[i
] = MPLS_STACK_BOTTOM
|
2261 ((__force __be32
)random32() &
2265 if ((pkt_dev
->flags
& F_VID_RND
) && (pkt_dev
->vlan_id
!= 0xffff)) {
2266 pkt_dev
->vlan_id
= random32() & (4096-1);
2269 if ((pkt_dev
->flags
& F_SVID_RND
) && (pkt_dev
->svlan_id
!= 0xffff)) {
2270 pkt_dev
->svlan_id
= random32() & (4096 - 1);
2273 if (pkt_dev
->udp_src_min
< pkt_dev
->udp_src_max
) {
2274 if (pkt_dev
->flags
& F_UDPSRC_RND
)
2275 pkt_dev
->cur_udp_src
= random32() %
2276 (pkt_dev
->udp_src_max
- pkt_dev
->udp_src_min
)
2277 + pkt_dev
->udp_src_min
;
2280 pkt_dev
->cur_udp_src
++;
2281 if (pkt_dev
->cur_udp_src
>= pkt_dev
->udp_src_max
)
2282 pkt_dev
->cur_udp_src
= pkt_dev
->udp_src_min
;
2286 if (pkt_dev
->udp_dst_min
< pkt_dev
->udp_dst_max
) {
2287 if (pkt_dev
->flags
& F_UDPDST_RND
) {
2288 pkt_dev
->cur_udp_dst
= random32() %
2289 (pkt_dev
->udp_dst_max
- pkt_dev
->udp_dst_min
)
2290 + pkt_dev
->udp_dst_min
;
2292 pkt_dev
->cur_udp_dst
++;
2293 if (pkt_dev
->cur_udp_dst
>= pkt_dev
->udp_dst_max
)
2294 pkt_dev
->cur_udp_dst
= pkt_dev
->udp_dst_min
;
2298 if (!(pkt_dev
->flags
& F_IPV6
)) {
2300 if ((imn
= ntohl(pkt_dev
->saddr_min
)) < (imx
=
2304 if (pkt_dev
->flags
& F_IPSRC_RND
)
2305 t
= random32() % (imx
- imn
) + imn
;
2307 t
= ntohl(pkt_dev
->cur_saddr
);
2313 pkt_dev
->cur_saddr
= htonl(t
);
2316 if (pkt_dev
->cflows
&& f_seen(pkt_dev
, flow
)) {
2317 pkt_dev
->cur_daddr
= pkt_dev
->flows
[flow
].cur_daddr
;
2319 imn
= ntohl(pkt_dev
->daddr_min
);
2320 imx
= ntohl(pkt_dev
->daddr_max
);
2324 if (pkt_dev
->flags
& F_IPDST_RND
) {
2326 t
= random32() % (imx
- imn
) + imn
;
2329 while (LOOPBACK(s
) || MULTICAST(s
)
2330 || BADCLASS(s
) || ZERONET(s
)
2331 || LOCAL_MCAST(s
)) {
2332 t
= random32() % (imx
- imn
) + imn
;
2335 pkt_dev
->cur_daddr
= s
;
2337 t
= ntohl(pkt_dev
->cur_daddr
);
2342 pkt_dev
->cur_daddr
= htonl(t
);
2345 if (pkt_dev
->cflows
) {
2346 pkt_dev
->flows
[flow
].flags
|= F_INIT
;
2347 pkt_dev
->flows
[flow
].cur_daddr
=
2350 if (pkt_dev
->flags
& F_IPSEC_ON
)
2351 get_ipsec_sa(pkt_dev
, flow
);
2356 } else { /* IPV6 * */
2358 if (pkt_dev
->min_in6_daddr
.s6_addr32
[0] == 0 &&
2359 pkt_dev
->min_in6_daddr
.s6_addr32
[1] == 0 &&
2360 pkt_dev
->min_in6_daddr
.s6_addr32
[2] == 0 &&
2361 pkt_dev
->min_in6_daddr
.s6_addr32
[3] == 0) ;
2365 /* Only random destinations yet */
2367 for (i
= 0; i
< 4; i
++) {
2368 pkt_dev
->cur_in6_daddr
.s6_addr32
[i
] =
2369 (((__force __be32
)random32() |
2370 pkt_dev
->min_in6_daddr
.s6_addr32
[i
]) &
2371 pkt_dev
->max_in6_daddr
.s6_addr32
[i
]);
2376 if (pkt_dev
->min_pkt_size
< pkt_dev
->max_pkt_size
) {
2378 if (pkt_dev
->flags
& F_TXSIZE_RND
) {
2380 (pkt_dev
->max_pkt_size
- pkt_dev
->min_pkt_size
)
2381 + pkt_dev
->min_pkt_size
;
2383 t
= pkt_dev
->cur_pkt_size
+ 1;
2384 if (t
> pkt_dev
->max_pkt_size
)
2385 t
= pkt_dev
->min_pkt_size
;
2387 pkt_dev
->cur_pkt_size
= t
;
2390 pkt_dev
->flows
[flow
].count
++;
2395 static int pktgen_output_ipsec(struct sk_buff
*skb
, struct pktgen_dev
*pkt_dev
)
2397 struct xfrm_state
*x
= pkt_dev
->flows
[pkt_dev
->curfl
].x
;
2403 /* XXX: we dont support tunnel mode for now until
2404 * we resolve the dst issue */
2405 if (x
->props
.mode
!= XFRM_MODE_TRANSPORT
)
2408 spin_lock(&x
->lock
);
2411 err
= x
->mode
->output(x
, skb
);
2414 err
= x
->type
->output(x
, skb
);
2418 x
->curlft
.bytes
+=skb
->len
;
2419 x
->curlft
.packets
++;
2420 spin_unlock(&x
->lock
);
2423 spin_unlock(&x
->lock
);
2427 static inline void free_SAs(struct pktgen_dev
*pkt_dev
)
2429 if (pkt_dev
->cflows
) {
2430 /* let go of the SAs if we have them */
2432 for (; i
< pkt_dev
->nflows
; i
++){
2433 struct xfrm_state
*x
= pkt_dev
->flows
[i
].x
;
2436 pkt_dev
->flows
[i
].x
= NULL
;
2442 static inline int process_ipsec(struct pktgen_dev
*pkt_dev
,
2443 struct sk_buff
*skb
, __be16 protocol
)
2445 if (pkt_dev
->flags
& F_IPSEC_ON
) {
2446 struct xfrm_state
*x
= pkt_dev
->flows
[pkt_dev
->curfl
].x
;
2451 nhead
= x
->props
.header_len
- skb_headroom(skb
);
2453 ret
= pskb_expand_head(skb
, nhead
, 0, GFP_ATOMIC
);
2455 printk(KERN_ERR
"Error expanding "
2456 "ipsec packet %d\n",ret
);
2461 /* ipsec is not expecting ll header */
2462 skb_pull(skb
, ETH_HLEN
);
2463 ret
= pktgen_output_ipsec(skb
, pkt_dev
);
2465 printk(KERN_ERR
"Error creating ipsec "
2471 eth
= (__u8
*) skb_push(skb
, ETH_HLEN
);
2472 memcpy(eth
, pkt_dev
->hh
, 12);
2473 *(u16
*) & eth
[12] = protocol
;
2480 static void mpls_push(__be32
*mpls
, struct pktgen_dev
*pkt_dev
)
2483 for (i
= 0; i
< pkt_dev
->nr_labels
; i
++) {
2484 *mpls
++ = pkt_dev
->labels
[i
] & ~MPLS_STACK_BOTTOM
;
2487 *mpls
|= MPLS_STACK_BOTTOM
;
2490 static inline __be16
build_tci(unsigned int id
, unsigned int cfi
,
2493 return htons(id
| (cfi
<< 12) | (prio
<< 13));
2496 static struct sk_buff
*fill_packet_ipv4(struct net_device
*odev
,
2497 struct pktgen_dev
*pkt_dev
)
2499 struct sk_buff
*skb
= NULL
;
2501 struct udphdr
*udph
;
2504 struct pktgen_hdr
*pgh
= NULL
;
2505 __be16 protocol
= htons(ETH_P_IP
);
2507 __be16
*vlan_tci
= NULL
; /* Encapsulates priority and VLAN ID */
2508 __be16
*vlan_encapsulated_proto
= NULL
; /* packet type ID field (or len) for VLAN tag */
2509 __be16
*svlan_tci
= NULL
; /* Encapsulates priority and SVLAN ID */
2510 __be16
*svlan_encapsulated_proto
= NULL
; /* packet type ID field (or len) for SVLAN tag */
2513 if (pkt_dev
->nr_labels
)
2514 protocol
= htons(ETH_P_MPLS_UC
);
2516 if (pkt_dev
->vlan_id
!= 0xffff)
2517 protocol
= htons(ETH_P_8021Q
);
2519 /* Update any of the values, used when we're incrementing various
2522 mod_cur_headers(pkt_dev
);
2524 datalen
= (odev
->hard_header_len
+ 16) & ~0xf;
2525 skb
= alloc_skb(pkt_dev
->cur_pkt_size
+ 64 + datalen
+
2526 pkt_dev
->pkt_overhead
, GFP_ATOMIC
);
2528 sprintf(pkt_dev
->result
, "No memory");
2532 skb_reserve(skb
, datalen
);
2534 /* Reserve for ethernet and IP header */
2535 eth
= (__u8
*) skb_push(skb
, 14);
2536 mpls
= (__be32
*)skb_put(skb
, pkt_dev
->nr_labels
*sizeof(__u32
));
2537 if (pkt_dev
->nr_labels
)
2538 mpls_push(mpls
, pkt_dev
);
2540 if (pkt_dev
->vlan_id
!= 0xffff) {
2541 if (pkt_dev
->svlan_id
!= 0xffff) {
2542 svlan_tci
= (__be16
*)skb_put(skb
, sizeof(__be16
));
2543 *svlan_tci
= build_tci(pkt_dev
->svlan_id
,
2546 svlan_encapsulated_proto
= (__be16
*)skb_put(skb
, sizeof(__be16
));
2547 *svlan_encapsulated_proto
= htons(ETH_P_8021Q
);
2549 vlan_tci
= (__be16
*)skb_put(skb
, sizeof(__be16
));
2550 *vlan_tci
= build_tci(pkt_dev
->vlan_id
,
2553 vlan_encapsulated_proto
= (__be16
*)skb_put(skb
, sizeof(__be16
));
2554 *vlan_encapsulated_proto
= htons(ETH_P_IP
);
2557 skb
->network_header
= skb
->tail
;
2558 skb
->transport_header
= skb
->network_header
+ sizeof(struct iphdr
);
2559 skb_put(skb
, sizeof(struct iphdr
) + sizeof(struct udphdr
));
2562 udph
= udp_hdr(skb
);
2564 memcpy(eth
, pkt_dev
->hh
, 12);
2565 *(__be16
*) & eth
[12] = protocol
;
2567 /* Eth + IPh + UDPh + mpls */
2568 datalen
= pkt_dev
->cur_pkt_size
- 14 - 20 - 8 -
2569 pkt_dev
->pkt_overhead
;
2570 if (datalen
< sizeof(struct pktgen_hdr
))
2571 datalen
= sizeof(struct pktgen_hdr
);
2573 udph
->source
= htons(pkt_dev
->cur_udp_src
);
2574 udph
->dest
= htons(pkt_dev
->cur_udp_dst
);
2575 udph
->len
= htons(datalen
+ 8); /* DATA + udphdr */
2576 udph
->check
= 0; /* No checksum */
2581 iph
->tos
= pkt_dev
->tos
;
2582 iph
->protocol
= IPPROTO_UDP
; /* UDP */
2583 iph
->saddr
= pkt_dev
->cur_saddr
;
2584 iph
->daddr
= pkt_dev
->cur_daddr
;
2586 iplen
= 20 + 8 + datalen
;
2587 iph
->tot_len
= htons(iplen
);
2589 iph
->check
= ip_fast_csum((void *)iph
, iph
->ihl
);
2590 skb
->protocol
= protocol
;
2591 skb
->mac_header
= (skb
->network_header
- ETH_HLEN
-
2592 pkt_dev
->pkt_overhead
);
2594 skb
->pkt_type
= PACKET_HOST
;
2596 if (pkt_dev
->nfrags
<= 0)
2597 pgh
= (struct pktgen_hdr
*)skb_put(skb
, datalen
);
2599 int frags
= pkt_dev
->nfrags
;
2602 pgh
= (struct pktgen_hdr
*)(((char *)(udph
)) + 8);
2604 if (frags
> MAX_SKB_FRAGS
)
2605 frags
= MAX_SKB_FRAGS
;
2606 if (datalen
> frags
* PAGE_SIZE
) {
2607 skb_put(skb
, datalen
- frags
* PAGE_SIZE
);
2608 datalen
= frags
* PAGE_SIZE
;
2612 while (datalen
> 0) {
2613 struct page
*page
= alloc_pages(GFP_KERNEL
, 0);
2614 skb_shinfo(skb
)->frags
[i
].page
= page
;
2615 skb_shinfo(skb
)->frags
[i
].page_offset
= 0;
2616 skb_shinfo(skb
)->frags
[i
].size
=
2617 (datalen
< PAGE_SIZE
? datalen
: PAGE_SIZE
);
2618 datalen
-= skb_shinfo(skb
)->frags
[i
].size
;
2619 skb
->len
+= skb_shinfo(skb
)->frags
[i
].size
;
2620 skb
->data_len
+= skb_shinfo(skb
)->frags
[i
].size
;
2622 skb_shinfo(skb
)->nr_frags
= i
;
2631 rem
= skb_shinfo(skb
)->frags
[i
- 1].size
/ 2;
2635 skb_shinfo(skb
)->frags
[i
- 1].size
-= rem
;
2637 skb_shinfo(skb
)->frags
[i
] =
2638 skb_shinfo(skb
)->frags
[i
- 1];
2639 get_page(skb_shinfo(skb
)->frags
[i
].page
);
2640 skb_shinfo(skb
)->frags
[i
].page
=
2641 skb_shinfo(skb
)->frags
[i
- 1].page
;
2642 skb_shinfo(skb
)->frags
[i
].page_offset
+=
2643 skb_shinfo(skb
)->frags
[i
- 1].size
;
2644 skb_shinfo(skb
)->frags
[i
].size
= rem
;
2646 skb_shinfo(skb
)->nr_frags
= i
;
2650 /* Stamp the time, and sequence number, convert them to network byte order */
2653 struct timeval timestamp
;
2655 pgh
->pgh_magic
= htonl(PKTGEN_MAGIC
);
2656 pgh
->seq_num
= htonl(pkt_dev
->seq_num
);
2658 do_gettimeofday(×tamp
);
2659 pgh
->tv_sec
= htonl(timestamp
.tv_sec
);
2660 pgh
->tv_usec
= htonl(timestamp
.tv_usec
);
2664 if (!process_ipsec(pkt_dev
, skb
, protocol
))
2672 * scan_ip6, fmt_ip taken from dietlibc-0.21
2673 * Author Felix von Leitner <felix-dietlibc@fefe.de>
2675 * Slightly modified for kernel.
2676 * Should be candidate for net/ipv4/utils.c
2680 static unsigned int scan_ip6(const char *s
, char ip
[16])
2683 unsigned int len
= 0;
2686 unsigned int prefixlen
= 0;
2687 unsigned int suffixlen
= 0;
2690 for (i
= 0; i
< 16; i
++)
2696 if (s
[1] == ':') { /* Found "::", skip to part 2 */
2705 u
= simple_strtoul(s
, &tmp
, 16);
2711 if (prefixlen
== 12 && s
[i
] == '.') {
2713 /* the last 4 bytes may be written as IPv4 address */
2716 memcpy((struct in_addr
*)(ip
+ 12), &tmp
, sizeof(tmp
));
2719 ip
[prefixlen
++] = (u
>> 8);
2720 ip
[prefixlen
++] = (u
& 255);
2723 if (prefixlen
== 16)
2727 /* part 2, after "::" */
2734 } else if (suffixlen
!= 0)
2738 u
= simple_strtol(s
, &tmp
, 16);
2746 if (suffixlen
+ prefixlen
<= 12 && s
[i
] == '.') {
2748 memcpy((struct in_addr
*)(suffix
+ suffixlen
), &tmp
,
2754 suffix
[suffixlen
++] = (u
>> 8);
2755 suffix
[suffixlen
++] = (u
& 255);
2758 if (prefixlen
+ suffixlen
== 16)
2761 for (i
= 0; i
< suffixlen
; i
++)
2762 ip
[16 - suffixlen
+ i
] = suffix
[i
];
2766 static char tohex(char hexdigit
)
2768 return hexdigit
> 9 ? hexdigit
+ 'a' - 10 : hexdigit
+ '0';
2771 static int fmt_xlong(char *s
, unsigned int i
)
2774 *s
= tohex((i
>> 12) & 0xf);
2775 if (s
!= bak
|| *s
!= '0')
2777 *s
= tohex((i
>> 8) & 0xf);
2778 if (s
!= bak
|| *s
!= '0')
2780 *s
= tohex((i
>> 4) & 0xf);
2781 if (s
!= bak
|| *s
!= '0')
2783 *s
= tohex(i
& 0xf);
2787 static unsigned int fmt_ip6(char *s
, const char ip
[16])
2792 unsigned int compressing
;
2797 for (j
= 0; j
< 16; j
+= 2) {
2799 #ifdef V4MAPPEDPREFIX
2800 if (j
== 12 && !memcmp(ip
, V4mappedprefix
, 12)) {
2801 inet_ntoa_r(*(struct in_addr
*)(ip
+ 12), s
);
2806 temp
= ((unsigned long)(unsigned char)ip
[j
] << 8) +
2807 (unsigned long)(unsigned char)ip
[j
+ 1];
2822 i
= fmt_xlong(s
, temp
);
2839 static struct sk_buff
*fill_packet_ipv6(struct net_device
*odev
,
2840 struct pktgen_dev
*pkt_dev
)
2842 struct sk_buff
*skb
= NULL
;
2844 struct udphdr
*udph
;
2846 struct ipv6hdr
*iph
;
2847 struct pktgen_hdr
*pgh
= NULL
;
2848 __be16 protocol
= htons(ETH_P_IPV6
);
2850 __be16
*vlan_tci
= NULL
; /* Encapsulates priority and VLAN ID */
2851 __be16
*vlan_encapsulated_proto
= NULL
; /* packet type ID field (or len) for VLAN tag */
2852 __be16
*svlan_tci
= NULL
; /* Encapsulates priority and SVLAN ID */
2853 __be16
*svlan_encapsulated_proto
= NULL
; /* packet type ID field (or len) for SVLAN tag */
2855 if (pkt_dev
->nr_labels
)
2856 protocol
= htons(ETH_P_MPLS_UC
);
2858 if (pkt_dev
->vlan_id
!= 0xffff)
2859 protocol
= htons(ETH_P_8021Q
);
2861 /* Update any of the values, used when we're incrementing various
2864 mod_cur_headers(pkt_dev
);
2866 skb
= alloc_skb(pkt_dev
->cur_pkt_size
+ 64 + 16 +
2867 pkt_dev
->pkt_overhead
, GFP_ATOMIC
);
2869 sprintf(pkt_dev
->result
, "No memory");
2873 skb_reserve(skb
, 16);
2875 /* Reserve for ethernet and IP header */
2876 eth
= (__u8
*) skb_push(skb
, 14);
2877 mpls
= (__be32
*)skb_put(skb
, pkt_dev
->nr_labels
*sizeof(__u32
));
2878 if (pkt_dev
->nr_labels
)
2879 mpls_push(mpls
, pkt_dev
);
2881 if (pkt_dev
->vlan_id
!= 0xffff) {
2882 if (pkt_dev
->svlan_id
!= 0xffff) {
2883 svlan_tci
= (__be16
*)skb_put(skb
, sizeof(__be16
));
2884 *svlan_tci
= build_tci(pkt_dev
->svlan_id
,
2887 svlan_encapsulated_proto
= (__be16
*)skb_put(skb
, sizeof(__be16
));
2888 *svlan_encapsulated_proto
= htons(ETH_P_8021Q
);
2890 vlan_tci
= (__be16
*)skb_put(skb
, sizeof(__be16
));
2891 *vlan_tci
= build_tci(pkt_dev
->vlan_id
,
2894 vlan_encapsulated_proto
= (__be16
*)skb_put(skb
, sizeof(__be16
));
2895 *vlan_encapsulated_proto
= htons(ETH_P_IPV6
);
2898 skb
->network_header
= skb
->tail
;
2899 skb
->transport_header
= skb
->network_header
+ sizeof(struct ipv6hdr
);
2900 skb_put(skb
, sizeof(struct ipv6hdr
) + sizeof(struct udphdr
));
2902 iph
= ipv6_hdr(skb
);
2903 udph
= udp_hdr(skb
);
2905 memcpy(eth
, pkt_dev
->hh
, 12);
2906 *(__be16
*) & eth
[12] = protocol
;
2908 /* Eth + IPh + UDPh + mpls */
2909 datalen
= pkt_dev
->cur_pkt_size
- 14 -
2910 sizeof(struct ipv6hdr
) - sizeof(struct udphdr
) -
2911 pkt_dev
->pkt_overhead
;
2913 if (datalen
< sizeof(struct pktgen_hdr
)) {
2914 datalen
= sizeof(struct pktgen_hdr
);
2915 if (net_ratelimit())
2916 printk(KERN_INFO
"pktgen: increased datalen to %d\n",
2920 udph
->source
= htons(pkt_dev
->cur_udp_src
);
2921 udph
->dest
= htons(pkt_dev
->cur_udp_dst
);
2922 udph
->len
= htons(datalen
+ sizeof(struct udphdr
));
2923 udph
->check
= 0; /* No checksum */
2925 *(__be32
*) iph
= htonl(0x60000000); /* Version + flow */
2927 if (pkt_dev
->traffic_class
) {
2928 /* Version + traffic class + flow (0) */
2929 *(__be32
*)iph
|= htonl(0x60000000 | (pkt_dev
->traffic_class
<< 20));
2932 iph
->hop_limit
= 32;
2934 iph
->payload_len
= htons(sizeof(struct udphdr
) + datalen
);
2935 iph
->nexthdr
= IPPROTO_UDP
;
2937 ipv6_addr_copy(&iph
->daddr
, &pkt_dev
->cur_in6_daddr
);
2938 ipv6_addr_copy(&iph
->saddr
, &pkt_dev
->cur_in6_saddr
);
2940 skb
->mac_header
= (skb
->network_header
- ETH_HLEN
-
2941 pkt_dev
->pkt_overhead
);
2942 skb
->protocol
= protocol
;
2944 skb
->pkt_type
= PACKET_HOST
;
2946 if (pkt_dev
->nfrags
<= 0)
2947 pgh
= (struct pktgen_hdr
*)skb_put(skb
, datalen
);
2949 int frags
= pkt_dev
->nfrags
;
2952 pgh
= (struct pktgen_hdr
*)(((char *)(udph
)) + 8);
2954 if (frags
> MAX_SKB_FRAGS
)
2955 frags
= MAX_SKB_FRAGS
;
2956 if (datalen
> frags
* PAGE_SIZE
) {
2957 skb_put(skb
, datalen
- frags
* PAGE_SIZE
);
2958 datalen
= frags
* PAGE_SIZE
;
2962 while (datalen
> 0) {
2963 struct page
*page
= alloc_pages(GFP_KERNEL
, 0);
2964 skb_shinfo(skb
)->frags
[i
].page
= page
;
2965 skb_shinfo(skb
)->frags
[i
].page_offset
= 0;
2966 skb_shinfo(skb
)->frags
[i
].size
=
2967 (datalen
< PAGE_SIZE
? datalen
: PAGE_SIZE
);
2968 datalen
-= skb_shinfo(skb
)->frags
[i
].size
;
2969 skb
->len
+= skb_shinfo(skb
)->frags
[i
].size
;
2970 skb
->data_len
+= skb_shinfo(skb
)->frags
[i
].size
;
2972 skb_shinfo(skb
)->nr_frags
= i
;
2981 rem
= skb_shinfo(skb
)->frags
[i
- 1].size
/ 2;
2985 skb_shinfo(skb
)->frags
[i
- 1].size
-= rem
;
2987 skb_shinfo(skb
)->frags
[i
] =
2988 skb_shinfo(skb
)->frags
[i
- 1];
2989 get_page(skb_shinfo(skb
)->frags
[i
].page
);
2990 skb_shinfo(skb
)->frags
[i
].page
=
2991 skb_shinfo(skb
)->frags
[i
- 1].page
;
2992 skb_shinfo(skb
)->frags
[i
].page_offset
+=
2993 skb_shinfo(skb
)->frags
[i
- 1].size
;
2994 skb_shinfo(skb
)->frags
[i
].size
= rem
;
2996 skb_shinfo(skb
)->nr_frags
= i
;
3000 /* Stamp the time, and sequence number, convert them to network byte order */
3001 /* should we update cloned packets too ? */
3003 struct timeval timestamp
;
3005 pgh
->pgh_magic
= htonl(PKTGEN_MAGIC
);
3006 pgh
->seq_num
= htonl(pkt_dev
->seq_num
);
3008 do_gettimeofday(×tamp
);
3009 pgh
->tv_sec
= htonl(timestamp
.tv_sec
);
3010 pgh
->tv_usec
= htonl(timestamp
.tv_usec
);
3012 /* pkt_dev->seq_num++; FF: you really mean this? */
3017 static inline struct sk_buff
*fill_packet(struct net_device
*odev
,
3018 struct pktgen_dev
*pkt_dev
)
3020 if (pkt_dev
->flags
& F_IPV6
)
3021 return fill_packet_ipv6(odev
, pkt_dev
);
3023 return fill_packet_ipv4(odev
, pkt_dev
);
3026 static void pktgen_clear_counters(struct pktgen_dev
*pkt_dev
)
3028 pkt_dev
->seq_num
= 1;
3029 pkt_dev
->idle_acc
= 0;
3031 pkt_dev
->tx_bytes
= 0;
3032 pkt_dev
->errors
= 0;
3035 /* Set up structure for sending pkts, clear counters */
3037 static void pktgen_run(struct pktgen_thread
*t
)
3039 struct pktgen_dev
*pkt_dev
;
3042 pr_debug("pktgen: entering pktgen_run. %p\n", t
);
3045 list_for_each_entry(pkt_dev
, &t
->if_list
, list
) {
3048 * setup odev and create initial packet.
3050 pktgen_setup_inject(pkt_dev
);
3052 if (pkt_dev
->odev
) {
3053 pktgen_clear_counters(pkt_dev
);
3054 pkt_dev
->running
= 1; /* Cranke yeself! */
3055 pkt_dev
->skb
= NULL
;
3056 pkt_dev
->started_at
= getCurUs();
3057 pkt_dev
->next_tx_us
= getCurUs(); /* Transmit immediately */
3058 pkt_dev
->next_tx_ns
= 0;
3059 set_pkt_overhead(pkt_dev
);
3061 strcpy(pkt_dev
->result
, "Starting");
3064 strcpy(pkt_dev
->result
, "Error starting");
3068 t
->control
&= ~(T_STOP
);
3071 static void pktgen_stop_all_threads_ifs(void)
3073 struct pktgen_thread
*t
;
3075 pr_debug("pktgen: entering pktgen_stop_all_threads_ifs.\n");
3077 mutex_lock(&pktgen_thread_lock
);
3079 list_for_each_entry(t
, &pktgen_threads
, th_list
)
3080 t
->control
|= T_STOP
;
3082 mutex_unlock(&pktgen_thread_lock
);
3085 static int thread_is_running(struct pktgen_thread
*t
)
3087 struct pktgen_dev
*pkt_dev
;
3090 list_for_each_entry(pkt_dev
, &t
->if_list
, list
)
3091 if (pkt_dev
->running
) {
3098 static int pktgen_wait_thread_run(struct pktgen_thread
*t
)
3102 while (thread_is_running(t
)) {
3106 msleep_interruptible(100);
3108 if (signal_pending(current
))
3118 static int pktgen_wait_all_threads_run(void)
3120 struct pktgen_thread
*t
;
3123 mutex_lock(&pktgen_thread_lock
);
3125 list_for_each_entry(t
, &pktgen_threads
, th_list
) {
3126 sig
= pktgen_wait_thread_run(t
);
3132 list_for_each_entry(t
, &pktgen_threads
, th_list
)
3133 t
->control
|= (T_STOP
);
3135 mutex_unlock(&pktgen_thread_lock
);
3139 static void pktgen_run_all_threads(void)
3141 struct pktgen_thread
*t
;
3143 pr_debug("pktgen: entering pktgen_run_all_threads.\n");
3145 mutex_lock(&pktgen_thread_lock
);
3147 list_for_each_entry(t
, &pktgen_threads
, th_list
)
3148 t
->control
|= (T_RUN
);
3150 mutex_unlock(&pktgen_thread_lock
);
3152 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
3154 pktgen_wait_all_threads_run();
3157 static void show_results(struct pktgen_dev
*pkt_dev
, int nr_frags
)
3159 __u64 total_us
, bps
, mbps
, pps
, idle
;
3160 char *p
= pkt_dev
->result
;
3162 total_us
= pkt_dev
->stopped_at
- pkt_dev
->started_at
;
3164 idle
= pkt_dev
->idle_acc
;
3166 p
+= sprintf(p
, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
3167 (unsigned long long)total_us
,
3168 (unsigned long long)(total_us
- idle
),
3169 (unsigned long long)idle
,
3170 (unsigned long long)pkt_dev
->sofar
,
3171 pkt_dev
->cur_pkt_size
, nr_frags
);
3173 pps
= pkt_dev
->sofar
* USEC_PER_SEC
;
3175 while ((total_us
>> 32) != 0) {
3180 do_div(pps
, total_us
);
3182 bps
= pps
* 8 * pkt_dev
->cur_pkt_size
;
3185 do_div(mbps
, 1000000);
3186 p
+= sprintf(p
, " %llupps %lluMb/sec (%llubps) errors: %llu",
3187 (unsigned long long)pps
,
3188 (unsigned long long)mbps
,
3189 (unsigned long long)bps
,
3190 (unsigned long long)pkt_dev
->errors
);
3193 /* Set stopped-at timer, remove from running list, do counters & statistics */
3195 static int pktgen_stop_device(struct pktgen_dev
*pkt_dev
)
3197 int nr_frags
= pkt_dev
->skb
? skb_shinfo(pkt_dev
->skb
)->nr_frags
: -1;
3199 if (!pkt_dev
->running
) {
3200 printk(KERN_WARNING
"pktgen: interface: %s is already "
3201 "stopped\n", pkt_dev
->odev
->name
);
3205 pkt_dev
->stopped_at
= getCurUs();
3206 pkt_dev
->running
= 0;
3208 show_results(pkt_dev
, nr_frags
);
3213 static struct pktgen_dev
*next_to_run(struct pktgen_thread
*t
)
3215 struct pktgen_dev
*pkt_dev
, *best
= NULL
;
3219 list_for_each_entry(pkt_dev
, &t
->if_list
, list
) {
3220 if (!pkt_dev
->running
)
3224 else if (pkt_dev
->next_tx_us
< best
->next_tx_us
)
3231 static void pktgen_stop(struct pktgen_thread
*t
)
3233 struct pktgen_dev
*pkt_dev
;
3235 pr_debug("pktgen: entering pktgen_stop\n");
3239 list_for_each_entry(pkt_dev
, &t
->if_list
, list
) {
3240 pktgen_stop_device(pkt_dev
);
3242 kfree_skb(pkt_dev
->skb
);
3244 pkt_dev
->skb
= NULL
;
3251 * one of our devices needs to be removed - find it
3254 static void pktgen_rem_one_if(struct pktgen_thread
*t
)
3256 struct list_head
*q
, *n
;
3257 struct pktgen_dev
*cur
;
3259 pr_debug("pktgen: entering pktgen_rem_one_if\n");
3263 list_for_each_safe(q
, n
, &t
->if_list
) {
3264 cur
= list_entry(q
, struct pktgen_dev
, list
);
3266 if (!cur
->removal_mark
)
3270 kfree_skb(cur
->skb
);
3273 pktgen_remove_device(t
, cur
);
3281 static void pktgen_rem_all_ifs(struct pktgen_thread
*t
)
3283 struct list_head
*q
, *n
;
3284 struct pktgen_dev
*cur
;
3286 /* Remove all devices, free mem */
3288 pr_debug("pktgen: entering pktgen_rem_all_ifs\n");
3291 list_for_each_safe(q
, n
, &t
->if_list
) {
3292 cur
= list_entry(q
, struct pktgen_dev
, list
);
3295 kfree_skb(cur
->skb
);
3298 pktgen_remove_device(t
, cur
);
3304 static void pktgen_rem_thread(struct pktgen_thread
*t
)
3306 /* Remove from the thread list */
3308 remove_proc_entry(t
->tsk
->comm
, pg_proc_dir
);
3310 mutex_lock(&pktgen_thread_lock
);
3312 list_del(&t
->th_list
);
3314 mutex_unlock(&pktgen_thread_lock
);
3317 static __inline__
void pktgen_xmit(struct pktgen_dev
*pkt_dev
)
3319 struct net_device
*odev
= NULL
;
3320 __u64 idle_start
= 0;
3323 odev
= pkt_dev
->odev
;
3325 if (pkt_dev
->delay_us
|| pkt_dev
->delay_ns
) {
3329 if (now
< pkt_dev
->next_tx_us
)
3330 spin(pkt_dev
, pkt_dev
->next_tx_us
);
3332 /* This is max DELAY, this has special meaning of
3335 if (pkt_dev
->delay_us
== 0x7FFFFFFF) {
3336 pkt_dev
->next_tx_us
= getCurUs() + pkt_dev
->delay_us
;
3337 pkt_dev
->next_tx_ns
= pkt_dev
->delay_ns
;
3342 if ((netif_queue_stopped(odev
) ||
3344 netif_subqueue_stopped(odev
, pkt_dev
->skb
->queue_mapping
))) ||
3346 idle_start
= getCurUs();
3348 if (!netif_running(odev
)) {
3349 pktgen_stop_device(pkt_dev
);
3351 kfree_skb(pkt_dev
->skb
);
3352 pkt_dev
->skb
= NULL
;
3358 pkt_dev
->idle_acc
+= getCurUs() - idle_start
;
3360 if (netif_queue_stopped(odev
) ||
3361 netif_subqueue_stopped(odev
, pkt_dev
->skb
->queue_mapping
)) {
3362 pkt_dev
->next_tx_us
= getCurUs(); /* TODO */
3363 pkt_dev
->next_tx_ns
= 0;
3364 goto out
; /* Try the next interface */
3368 if (pkt_dev
->last_ok
|| !pkt_dev
->skb
) {
3369 if ((++pkt_dev
->clone_count
>= pkt_dev
->clone_skb
)
3370 || (!pkt_dev
->skb
)) {
3371 /* build a new pkt */
3373 kfree_skb(pkt_dev
->skb
);
3375 pkt_dev
->skb
= fill_packet(odev
, pkt_dev
);
3376 if (pkt_dev
->skb
== NULL
) {
3377 printk(KERN_ERR
"pktgen: ERROR: couldn't "
3378 "allocate skb in fill_packet.\n");
3380 pkt_dev
->clone_count
--; /* back out increment, OOM */
3383 pkt_dev
->allocated_skbs
++;
3384 pkt_dev
->clone_count
= 0; /* reset counter */
3388 netif_tx_lock_bh(odev
);
3389 if (!netif_queue_stopped(odev
) &&
3390 !netif_subqueue_stopped(odev
, pkt_dev
->skb
->queue_mapping
)) {
3392 atomic_inc(&(pkt_dev
->skb
->users
));
3394 ret
= odev
->hard_start_xmit(pkt_dev
->skb
, odev
);
3395 if (likely(ret
== NETDEV_TX_OK
)) {
3396 pkt_dev
->last_ok
= 1;
3399 pkt_dev
->tx_bytes
+= pkt_dev
->cur_pkt_size
;
3401 } else if (ret
== NETDEV_TX_LOCKED
3402 && (odev
->features
& NETIF_F_LLTX
)) {
3405 } else { /* Retry it next time */
3407 atomic_dec(&(pkt_dev
->skb
->users
));
3409 if (debug
&& net_ratelimit())
3410 printk(KERN_INFO
"pktgen: Hard xmit error\n");
3413 pkt_dev
->last_ok
= 0;
3416 pkt_dev
->next_tx_us
= getCurUs();
3417 pkt_dev
->next_tx_ns
= 0;
3419 pkt_dev
->next_tx_us
+= pkt_dev
->delay_us
;
3420 pkt_dev
->next_tx_ns
+= pkt_dev
->delay_ns
;
3422 if (pkt_dev
->next_tx_ns
> 1000) {
3423 pkt_dev
->next_tx_us
++;
3424 pkt_dev
->next_tx_ns
-= 1000;
3428 else { /* Retry it next time */
3429 pkt_dev
->last_ok
= 0;
3430 pkt_dev
->next_tx_us
= getCurUs(); /* TODO */
3431 pkt_dev
->next_tx_ns
= 0;
3434 netif_tx_unlock_bh(odev
);
3436 /* If pkt_dev->count is zero, then run forever */
3437 if ((pkt_dev
->count
!= 0) && (pkt_dev
->sofar
>= pkt_dev
->count
)) {
3438 if (atomic_read(&(pkt_dev
->skb
->users
)) != 1) {
3439 idle_start
= getCurUs();
3440 while (atomic_read(&(pkt_dev
->skb
->users
)) != 1) {
3441 if (signal_pending(current
)) {
3446 pkt_dev
->idle_acc
+= getCurUs() - idle_start
;
3449 /* Done with this */
3450 pktgen_stop_device(pkt_dev
);
3452 kfree_skb(pkt_dev
->skb
);
3453 pkt_dev
->skb
= NULL
;
3459 * Main loop of the thread goes here
3462 static int pktgen_thread_worker(void *arg
)
3465 struct pktgen_thread
*t
= arg
;
3466 struct pktgen_dev
*pkt_dev
= NULL
;
3468 u32 max_before_softirq
;
3469 u32 tx_since_softirq
= 0;
3471 BUG_ON(smp_processor_id() != cpu
);
3473 init_waitqueue_head(&t
->queue
);
3475 pr_debug("pktgen: starting pktgen/%d: pid=%d\n", cpu
, current
->pid
);
3477 max_before_softirq
= t
->max_before_softirq
;
3479 set_current_state(TASK_INTERRUPTIBLE
);
3483 while (!kthread_should_stop()) {
3484 pkt_dev
= next_to_run(t
);
3487 (t
->control
& (T_STOP
| T_RUN
| T_REMDEVALL
| T_REMDEV
))
3489 prepare_to_wait(&(t
->queue
), &wait
,
3490 TASK_INTERRUPTIBLE
);
3491 schedule_timeout(HZ
/ 10);
3492 finish_wait(&(t
->queue
), &wait
);
3495 __set_current_state(TASK_RUNNING
);
3499 pktgen_xmit(pkt_dev
);
3502 * We like to stay RUNNING but must also give
3503 * others fair share.
3506 tx_since_softirq
+= pkt_dev
->last_ok
;
3508 if (tx_since_softirq
> max_before_softirq
) {
3509 if (local_softirq_pending())
3511 tx_since_softirq
= 0;
3515 if (t
->control
& T_STOP
) {
3517 t
->control
&= ~(T_STOP
);
3520 if (t
->control
& T_RUN
) {
3522 t
->control
&= ~(T_RUN
);
3525 if (t
->control
& T_REMDEVALL
) {
3526 pktgen_rem_all_ifs(t
);
3527 t
->control
&= ~(T_REMDEVALL
);
3530 if (t
->control
& T_REMDEV
) {
3531 pktgen_rem_one_if(t
);
3532 t
->control
&= ~(T_REMDEV
);
3537 set_current_state(TASK_INTERRUPTIBLE
);
3540 pr_debug("pktgen: %s stopping all device\n", t
->tsk
->comm
);
3543 pr_debug("pktgen: %s removing all device\n", t
->tsk
->comm
);
3544 pktgen_rem_all_ifs(t
);
3546 pr_debug("pktgen: %s removing thread.\n", t
->tsk
->comm
);
3547 pktgen_rem_thread(t
);
3552 static struct pktgen_dev
*pktgen_find_dev(struct pktgen_thread
*t
,
3555 struct pktgen_dev
*p
, *pkt_dev
= NULL
;
3558 list_for_each_entry(p
, &t
->if_list
, list
)
3559 if (strncmp(p
->odev
->name
, ifname
, IFNAMSIZ
) == 0) {
3565 pr_debug("pktgen: find_dev(%s) returning %p\n", ifname
, pkt_dev
);
3570 * Adds a dev at front of if_list.
3573 static int add_dev_to_thread(struct pktgen_thread
*t
,
3574 struct pktgen_dev
*pkt_dev
)
3580 if (pkt_dev
->pg_thread
) {
3581 printk(KERN_ERR
"pktgen: ERROR: already assigned "
3587 list_add(&pkt_dev
->list
, &t
->if_list
);
3588 pkt_dev
->pg_thread
= t
;
3589 pkt_dev
->running
= 0;
3596 /* Called under thread lock */
3598 static int pktgen_add_device(struct pktgen_thread
*t
, const char *ifname
)
3600 struct pktgen_dev
*pkt_dev
;
3603 /* We don't allow a device to be on several threads */
3605 pkt_dev
= __pktgen_NN_threads(ifname
, FIND
);
3607 printk(KERN_ERR
"pktgen: ERROR: interface already used.\n");
3611 pkt_dev
= kzalloc(sizeof(struct pktgen_dev
), GFP_KERNEL
);
3615 pkt_dev
->flows
= vmalloc(MAX_CFLOWS
* sizeof(struct flow_state
));
3616 if (pkt_dev
->flows
== NULL
) {
3620 memset(pkt_dev
->flows
, 0, MAX_CFLOWS
* sizeof(struct flow_state
));
3622 pkt_dev
->removal_mark
= 0;
3623 pkt_dev
->min_pkt_size
= ETH_ZLEN
;
3624 pkt_dev
->max_pkt_size
= ETH_ZLEN
;
3625 pkt_dev
->nfrags
= 0;
3626 pkt_dev
->clone_skb
= pg_clone_skb_d
;
3627 pkt_dev
->delay_us
= pg_delay_d
/ 1000;
3628 pkt_dev
->delay_ns
= pg_delay_d
% 1000;
3629 pkt_dev
->count
= pg_count_d
;
3631 pkt_dev
->udp_src_min
= 9; /* sink port */
3632 pkt_dev
->udp_src_max
= 9;
3633 pkt_dev
->udp_dst_min
= 9;
3634 pkt_dev
->udp_dst_max
= 9;
3636 pkt_dev
->vlan_p
= 0;
3637 pkt_dev
->vlan_cfi
= 0;
3638 pkt_dev
->vlan_id
= 0xffff;
3639 pkt_dev
->svlan_p
= 0;
3640 pkt_dev
->svlan_cfi
= 0;
3641 pkt_dev
->svlan_id
= 0xffff;
3643 err
= pktgen_setup_dev(pkt_dev
, ifname
);
3647 pkt_dev
->entry
= create_proc_entry(ifname
, 0600, pg_proc_dir
);
3648 if (!pkt_dev
->entry
) {
3649 printk(KERN_ERR
"pktgen: cannot create %s/%s procfs entry.\n",
3650 PG_PROC_DIR
, ifname
);
3654 pkt_dev
->entry
->proc_fops
= &pktgen_if_fops
;
3655 pkt_dev
->entry
->data
= pkt_dev
;
3657 pkt_dev
->ipsmode
= XFRM_MODE_TRANSPORT
;
3658 pkt_dev
->ipsproto
= IPPROTO_ESP
;
3661 return add_dev_to_thread(t
, pkt_dev
);
3663 dev_put(pkt_dev
->odev
);
3669 vfree(pkt_dev
->flows
);
3674 static int __init
pktgen_create_thread(int cpu
)
3676 struct pktgen_thread
*t
;
3677 struct proc_dir_entry
*pe
;
3678 struct task_struct
*p
;
3680 t
= kzalloc(sizeof(struct pktgen_thread
), GFP_KERNEL
);
3682 printk(KERN_ERR
"pktgen: ERROR: out of memory, can't "
3683 "create new thread.\n");
3687 spin_lock_init(&t
->if_lock
);
3690 INIT_LIST_HEAD(&t
->if_list
);
3692 list_add_tail(&t
->th_list
, &pktgen_threads
);
3694 p
= kthread_create(pktgen_thread_worker
, t
, "kpktgend_%d", cpu
);
3696 printk(KERN_ERR
"pktgen: kernel_thread() failed "
3697 "for cpu %d\n", t
->cpu
);
3698 list_del(&t
->th_list
);
3702 kthread_bind(p
, cpu
);
3705 pe
= create_proc_entry(t
->tsk
->comm
, 0600, pg_proc_dir
);
3707 printk(KERN_ERR
"pktgen: cannot create %s/%s procfs entry.\n",
3708 PG_PROC_DIR
, t
->tsk
->comm
);
3710 list_del(&t
->th_list
);
3715 pe
->proc_fops
= &pktgen_thread_fops
;
3724 * Removes a device from the thread if_list.
3726 static void _rem_dev_from_if_list(struct pktgen_thread
*t
,
3727 struct pktgen_dev
*pkt_dev
)
3729 struct list_head
*q
, *n
;
3730 struct pktgen_dev
*p
;
3732 list_for_each_safe(q
, n
, &t
->if_list
) {
3733 p
= list_entry(q
, struct pktgen_dev
, list
);
3739 static int pktgen_remove_device(struct pktgen_thread
*t
,
3740 struct pktgen_dev
*pkt_dev
)
3743 pr_debug("pktgen: remove_device pkt_dev=%p\n", pkt_dev
);
3745 if (pkt_dev
->running
) {
3746 printk(KERN_WARNING
"pktgen: WARNING: trying to remove a "
3747 "running interface, stopping it now.\n");
3748 pktgen_stop_device(pkt_dev
);
3751 /* Dis-associate from the interface */
3753 if (pkt_dev
->odev
) {
3754 dev_put(pkt_dev
->odev
);
3755 pkt_dev
->odev
= NULL
;
3758 /* And update the thread if_list */
3760 _rem_dev_from_if_list(t
, pkt_dev
);
3763 remove_proc_entry(pkt_dev
->entry
->name
, pg_proc_dir
);
3769 vfree(pkt_dev
->flows
);
3774 static int __init
pg_init(void)
3777 struct proc_dir_entry
*pe
;
3779 printk(KERN_INFO
"%s", version
);
3781 pg_proc_dir
= proc_mkdir(PG_PROC_DIR
, proc_net
);
3784 pg_proc_dir
->owner
= THIS_MODULE
;
3786 pe
= create_proc_entry(PGCTRL
, 0600, pg_proc_dir
);
3788 printk(KERN_ERR
"pktgen: ERROR: cannot create %s "
3789 "procfs entry.\n", PGCTRL
);
3790 proc_net_remove(PG_PROC_DIR
);
3794 pe
->proc_fops
= &pktgen_fops
;
3797 /* Register us to receive netdevice events */
3798 register_netdevice_notifier(&pktgen_notifier_block
);
3800 for_each_online_cpu(cpu
) {
3803 err
= pktgen_create_thread(cpu
);
3805 printk(KERN_WARNING
"pktgen: WARNING: Cannot create "
3806 "thread for cpu %d (%d)\n", cpu
, err
);
3809 if (list_empty(&pktgen_threads
)) {
3810 printk(KERN_ERR
"pktgen: ERROR: Initialization failed for "
3812 unregister_netdevice_notifier(&pktgen_notifier_block
);
3813 remove_proc_entry(PGCTRL
, pg_proc_dir
);
3814 proc_net_remove(PG_PROC_DIR
);
3821 static void __exit
pg_cleanup(void)
3823 struct pktgen_thread
*t
;
3824 struct list_head
*q
, *n
;
3825 wait_queue_head_t queue
;
3826 init_waitqueue_head(&queue
);
3828 /* Stop all interfaces & threads */
3830 list_for_each_safe(q
, n
, &pktgen_threads
) {
3831 t
= list_entry(q
, struct pktgen_thread
, th_list
);
3832 kthread_stop(t
->tsk
);
3836 /* Un-register us from receiving netdevice events */
3837 unregister_netdevice_notifier(&pktgen_notifier_block
);
3839 /* Clean up proc file system */
3840 remove_proc_entry(PGCTRL
, pg_proc_dir
);
3841 proc_net_remove(PG_PROC_DIR
);
3844 module_init(pg_init
);
3845 module_exit(pg_cleanup
);
3847 MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se");
3848 MODULE_DESCRIPTION("Packet Generator tool");
3849 MODULE_LICENSE("GPL");
3850 module_param(pg_count_d
, int, 0);
3851 module_param(pg_delay_d
, int, 0);
3852 module_param(pg_clone_skb_d
, int, 0);
3853 module_param(debug
, int, 0);