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/net_namespace.h>
156 #include <net/checksum.h>
157 #include <net/ipv6.h>
158 #include <net/addrconf.h>
160 #include <net/xfrm.h>
162 #include <asm/byteorder.h>
163 #include <linux/rcupdate.h>
164 #include <linux/bitops.h>
167 #include <asm/uaccess.h>
168 #include <asm/div64.h> /* do_div */
169 #include <asm/timex.h>
171 #define VERSION "pktgen v2.69: Packet Generator for packet performance testing.\n"
173 /* The buckets are exponential in 'width' */
174 #define LAT_BUCKETS_MAX 32
175 #define IP_NAME_SZ 32
176 #define MAX_MPLS_LABELS 16 /* This is the max label stack depth */
177 #define MPLS_STACK_BOTTOM htonl(0x00000100)
179 /* Device flag bits */
180 #define F_IPSRC_RND (1<<0) /* IP-Src Random */
181 #define F_IPDST_RND (1<<1) /* IP-Dst Random */
182 #define F_UDPSRC_RND (1<<2) /* UDP-Src Random */
183 #define F_UDPDST_RND (1<<3) /* UDP-Dst Random */
184 #define F_MACSRC_RND (1<<4) /* MAC-Src Random */
185 #define F_MACDST_RND (1<<5) /* MAC-Dst Random */
186 #define F_TXSIZE_RND (1<<6) /* Transmit size is random */
187 #define F_IPV6 (1<<7) /* Interface in IPV6 Mode */
188 #define F_MPLS_RND (1<<8) /* Random MPLS labels */
189 #define F_VID_RND (1<<9) /* Random VLAN ID */
190 #define F_SVID_RND (1<<10) /* Random SVLAN ID */
191 #define F_FLOW_SEQ (1<<11) /* Sequential flows */
192 #define F_IPSEC_ON (1<<12) /* ipsec on for flows */
193 #define F_QUEUE_MAP_RND (1<<13) /* queue map Random */
195 /* Thread control flag bits */
196 #define T_TERMINATE (1<<0)
197 #define T_STOP (1<<1) /* Stop run */
198 #define T_RUN (1<<2) /* Start run */
199 #define T_REMDEVALL (1<<3) /* Remove all devs */
200 #define T_REMDEV (1<<4) /* Remove one dev */
202 /* If lock -- can be removed after some work */
203 #define if_lock(t) spin_lock(&(t->if_lock));
204 #define if_unlock(t) spin_unlock(&(t->if_lock));
206 /* Used to help with determining the pkts on receive */
207 #define PKTGEN_MAGIC 0xbe9be955
208 #define PG_PROC_DIR "pktgen"
209 #define PGCTRL "pgctrl"
210 static struct proc_dir_entry
*pg_proc_dir
= NULL
;
212 #define MAX_CFLOWS 65536
214 #define VLAN_TAG_SIZE(x) ((x)->vlan_id == 0xffff ? 0 : 4)
215 #define SVLAN_TAG_SIZE(x) ((x)->svlan_id == 0xffff ? 0 : 4)
221 struct xfrm_state
*x
;
227 #define F_INIT (1<<0) /* flow has been initialized */
231 * Try to keep frequent/infrequent used vars. separated.
233 struct proc_dir_entry
*entry
; /* proc file */
234 struct pktgen_thread
*pg_thread
;/* the owner */
235 struct list_head list
; /* Used for chaining in the thread's run-queue */
237 int running
; /* if this changes to false, the test will stop */
239 /* If min != max, then we will either do a linear iteration, or
240 * we will do a random selection from within the range.
243 int removal_mark
; /* non-zero => the device is marked for
244 * removal by worker thread */
246 int min_pkt_size
; /* = ETH_ZLEN; */
247 int max_pkt_size
; /* = ETH_ZLEN; */
248 int pkt_overhead
; /* overhead for MPLS, VLANs, IPSEC etc */
250 __u32 delay_us
; /* Default delay */
252 __u64 count
; /* Default No packets to send */
253 __u64 sofar
; /* How many pkts we've sent so far */
254 __u64 tx_bytes
; /* How many bytes we've transmitted */
255 __u64 errors
; /* Errors when trying to transmit, pkts will be re-sent */
257 /* runtime counters relating to clone_skb */
258 __u64 next_tx_us
; /* timestamp of when to tx next */
261 __u64 allocated_skbs
;
263 int last_ok
; /* Was last skb sent?
264 * Or a failed transmit of some sort? This will keep
265 * sequence numbers in order, for example.
267 __u64 started_at
; /* micro-seconds */
268 __u64 stopped_at
; /* micro-seconds */
269 __u64 idle_acc
; /* micro-seconds */
272 int clone_skb
; /* Use multiple SKBs during packet gen. If this number
273 * is greater than 1, then that many copies of the same
274 * packet will be sent before a new packet is allocated.
275 * For instance, if you want to send 1024 identical packets
276 * before creating a new packet, set clone_skb to 1024.
279 char dst_min
[IP_NAME_SZ
]; /* IP, ie 1.2.3.4 */
280 char dst_max
[IP_NAME_SZ
]; /* IP, ie 1.2.3.4 */
281 char src_min
[IP_NAME_SZ
]; /* IP, ie 1.2.3.4 */
282 char src_max
[IP_NAME_SZ
]; /* IP, ie 1.2.3.4 */
284 struct in6_addr in6_saddr
;
285 struct in6_addr in6_daddr
;
286 struct in6_addr cur_in6_daddr
;
287 struct in6_addr cur_in6_saddr
;
289 struct in6_addr min_in6_daddr
;
290 struct in6_addr max_in6_daddr
;
291 struct in6_addr min_in6_saddr
;
292 struct in6_addr max_in6_saddr
;
294 /* If we're doing ranges, random or incremental, then this
295 * defines the min/max for those ranges.
297 __be32 saddr_min
; /* inclusive, source IP address */
298 __be32 saddr_max
; /* exclusive, source IP address */
299 __be32 daddr_min
; /* inclusive, dest IP address */
300 __be32 daddr_max
; /* exclusive, dest IP address */
302 __u16 udp_src_min
; /* inclusive, source UDP port */
303 __u16 udp_src_max
; /* exclusive, source UDP port */
304 __u16 udp_dst_min
; /* inclusive, dest UDP port */
305 __u16 udp_dst_max
; /* exclusive, dest UDP port */
308 __u8 tos
; /* six most significant bits of (former) IPv4 TOS are for dscp codepoint */
309 __u8 traffic_class
; /* ditto for the (former) Traffic Class in IPv6 (see RFC 3260, sec. 4) */
312 unsigned nr_labels
; /* Depth of stack, 0 = no MPLS */
313 __be32 labels
[MAX_MPLS_LABELS
];
315 /* VLAN/SVLAN (802.1Q/Q-in-Q) */
318 __u16 vlan_id
; /* 0xffff means no vlan tag */
322 __u16 svlan_id
; /* 0xffff means no svlan tag */
324 __u32 src_mac_count
; /* How many MACs to iterate through */
325 __u32 dst_mac_count
; /* How many MACs to iterate through */
327 unsigned char dst_mac
[ETH_ALEN
];
328 unsigned char src_mac
[ETH_ALEN
];
330 __u32 cur_dst_mac_offset
;
331 __u32 cur_src_mac_offset
;
341 0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
343 We fill in SRC address later
344 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
348 __u16 pad
; /* pad out the hh struct to an even 16 bytes */
350 struct sk_buff
*skb
; /* skb we are to transmit next, mainly used for when we
351 * are transmitting the same one multiple times
353 struct net_device
*odev
; /* The out-going device. Note that the device should
354 * have it's pg_info pointer pointing back to this
355 * device. This will be set when the user specifies
356 * the out-going device name (not when the inject is
357 * started as it used to do.)
359 struct flow_state
*flows
;
360 unsigned cflows
; /* Concurrent flows (config) */
361 unsigned lflow
; /* Flow length (config) */
362 unsigned nflows
; /* accumulated flows (stats) */
363 unsigned curfl
; /* current sequenced flow (state)*/
369 __u8 ipsmode
; /* IPSEC mode (config) */
370 __u8 ipsproto
; /* IPSEC type (config) */
382 struct pktgen_thread
{
384 struct list_head if_list
; /* All device here */
385 struct list_head th_list
;
386 struct task_struct
*tsk
;
389 /* Field for thread to receive "posted" events terminate, stop ifs etc. */
394 wait_queue_head_t queue
;
400 /** Convert to micro-seconds */
401 static inline __u64
tv_to_us(const struct timeval
*tv
)
403 __u64 us
= tv
->tv_usec
;
404 us
+= (__u64
) tv
->tv_sec
* (__u64
) 1000000;
408 static __u64
getCurUs(void)
411 do_gettimeofday(&tv
);
412 return tv_to_us(&tv
);
415 /* old include end */
417 static char version
[] __initdata
= VERSION
;
419 static int pktgen_remove_device(struct pktgen_thread
*t
, struct pktgen_dev
*i
);
420 static int pktgen_add_device(struct pktgen_thread
*t
, const char *ifname
);
421 static struct pktgen_dev
*pktgen_find_dev(struct pktgen_thread
*t
,
423 static int pktgen_device_event(struct notifier_block
*, unsigned long, void *);
424 static void pktgen_run_all_threads(void);
425 static void pktgen_stop_all_threads_ifs(void);
426 static int pktgen_stop_device(struct pktgen_dev
*pkt_dev
);
427 static void pktgen_stop(struct pktgen_thread
*t
);
428 static void pktgen_clear_counters(struct pktgen_dev
*pkt_dev
);
430 static unsigned int scan_ip6(const char *s
, char ip
[16]);
431 static unsigned int fmt_ip6(char *s
, const char ip
[16]);
433 /* Module parameters, defaults. */
434 static int pg_count_d
= 1000; /* 1000 pkts by default */
435 static int pg_delay_d
;
436 static int pg_clone_skb_d
;
439 static DEFINE_MUTEX(pktgen_thread_lock
);
440 static LIST_HEAD(pktgen_threads
);
442 static struct notifier_block pktgen_notifier_block
= {
443 .notifier_call
= pktgen_device_event
,
447 * /proc handling functions
451 static int pgctrl_show(struct seq_file
*seq
, void *v
)
453 seq_puts(seq
, VERSION
);
457 static ssize_t
pgctrl_write(struct file
*file
, const char __user
* buf
,
458 size_t count
, loff_t
* ppos
)
463 if (!capable(CAP_NET_ADMIN
)) {
468 if (count
> sizeof(data
))
469 count
= sizeof(data
);
471 if (copy_from_user(data
, buf
, count
)) {
475 data
[count
- 1] = 0; /* Make string */
477 if (!strcmp(data
, "stop"))
478 pktgen_stop_all_threads_ifs();
480 else if (!strcmp(data
, "start"))
481 pktgen_run_all_threads();
484 printk(KERN_WARNING
"pktgen: Unknown command: %s\n", data
);
492 static int pgctrl_open(struct inode
*inode
, struct file
*file
)
494 return single_open(file
, pgctrl_show
, PDE(inode
)->data
);
497 static const struct file_operations pktgen_fops
= {
498 .owner
= THIS_MODULE
,
502 .write
= pgctrl_write
,
503 .release
= single_release
,
506 static int pktgen_if_show(struct seq_file
*seq
, void *v
)
508 struct pktgen_dev
*pkt_dev
= seq
->private;
511 __u64 now
= getCurUs();
512 DECLARE_MAC_BUF(mac
);
515 "Params: count %llu min_pkt_size: %u max_pkt_size: %u\n",
516 (unsigned long long)pkt_dev
->count
, pkt_dev
->min_pkt_size
,
517 pkt_dev
->max_pkt_size
);
520 " frags: %d delay: %u clone_skb: %d ifname: %s\n",
522 1000 * pkt_dev
->delay_us
+ pkt_dev
->delay_ns
,
523 pkt_dev
->clone_skb
, pkt_dev
->odev
->name
);
525 seq_printf(seq
, " flows: %u flowlen: %u\n", pkt_dev
->cflows
,
529 " queue_map_min: %u queue_map_max: %u\n",
530 pkt_dev
->queue_map_min
,
531 pkt_dev
->queue_map_max
);
533 if (pkt_dev
->flags
& F_IPV6
) {
534 char b1
[128], b2
[128], b3
[128];
535 fmt_ip6(b1
, pkt_dev
->in6_saddr
.s6_addr
);
536 fmt_ip6(b2
, pkt_dev
->min_in6_saddr
.s6_addr
);
537 fmt_ip6(b3
, pkt_dev
->max_in6_saddr
.s6_addr
);
539 " saddr: %s min_saddr: %s max_saddr: %s\n", b1
,
542 fmt_ip6(b1
, pkt_dev
->in6_daddr
.s6_addr
);
543 fmt_ip6(b2
, pkt_dev
->min_in6_daddr
.s6_addr
);
544 fmt_ip6(b3
, pkt_dev
->max_in6_daddr
.s6_addr
);
546 " daddr: %s min_daddr: %s max_daddr: %s\n", b1
,
551 " dst_min: %s dst_max: %s\n src_min: %s src_max: %s\n",
552 pkt_dev
->dst_min
, pkt_dev
->dst_max
, pkt_dev
->src_min
,
555 seq_puts(seq
, " src_mac: ");
557 seq_printf(seq
, "%s ",
558 print_mac(mac
, is_zero_ether_addr(pkt_dev
->src_mac
) ?
559 pkt_dev
->odev
->dev_addr
: pkt_dev
->src_mac
));
561 seq_printf(seq
, "dst_mac: ");
562 seq_printf(seq
, "%s\n", print_mac(mac
, pkt_dev
->dst_mac
));
565 " udp_src_min: %d udp_src_max: %d udp_dst_min: %d udp_dst_max: %d\n",
566 pkt_dev
->udp_src_min
, pkt_dev
->udp_src_max
,
567 pkt_dev
->udp_dst_min
, pkt_dev
->udp_dst_max
);
570 " src_mac_count: %d dst_mac_count: %d\n",
571 pkt_dev
->src_mac_count
, pkt_dev
->dst_mac_count
);
573 if (pkt_dev
->nr_labels
) {
575 seq_printf(seq
, " mpls: ");
576 for (i
= 0; i
< pkt_dev
->nr_labels
; i
++)
577 seq_printf(seq
, "%08x%s", ntohl(pkt_dev
->labels
[i
]),
578 i
== pkt_dev
->nr_labels
-1 ? "\n" : ", ");
581 if (pkt_dev
->vlan_id
!= 0xffff) {
582 seq_printf(seq
, " vlan_id: %u vlan_p: %u vlan_cfi: %u\n",
583 pkt_dev
->vlan_id
, pkt_dev
->vlan_p
, pkt_dev
->vlan_cfi
);
586 if (pkt_dev
->svlan_id
!= 0xffff) {
587 seq_printf(seq
, " svlan_id: %u vlan_p: %u vlan_cfi: %u\n",
588 pkt_dev
->svlan_id
, pkt_dev
->svlan_p
, pkt_dev
->svlan_cfi
);
592 seq_printf(seq
, " tos: 0x%02x\n", pkt_dev
->tos
);
595 if (pkt_dev
->traffic_class
) {
596 seq_printf(seq
, " traffic_class: 0x%02x\n", pkt_dev
->traffic_class
);
599 seq_printf(seq
, " Flags: ");
601 if (pkt_dev
->flags
& F_IPV6
)
602 seq_printf(seq
, "IPV6 ");
604 if (pkt_dev
->flags
& F_IPSRC_RND
)
605 seq_printf(seq
, "IPSRC_RND ");
607 if (pkt_dev
->flags
& F_IPDST_RND
)
608 seq_printf(seq
, "IPDST_RND ");
610 if (pkt_dev
->flags
& F_TXSIZE_RND
)
611 seq_printf(seq
, "TXSIZE_RND ");
613 if (pkt_dev
->flags
& F_UDPSRC_RND
)
614 seq_printf(seq
, "UDPSRC_RND ");
616 if (pkt_dev
->flags
& F_UDPDST_RND
)
617 seq_printf(seq
, "UDPDST_RND ");
619 if (pkt_dev
->flags
& F_MPLS_RND
)
620 seq_printf(seq
, "MPLS_RND ");
622 if (pkt_dev
->flags
& F_QUEUE_MAP_RND
)
623 seq_printf(seq
, "QUEUE_MAP_RND ");
625 if (pkt_dev
->cflows
) {
626 if (pkt_dev
->flags
& F_FLOW_SEQ
)
627 seq_printf(seq
, "FLOW_SEQ "); /*in sequence flows*/
629 seq_printf(seq
, "FLOW_RND ");
633 if (pkt_dev
->flags
& F_IPSEC_ON
)
634 seq_printf(seq
, "IPSEC ");
637 if (pkt_dev
->flags
& F_MACSRC_RND
)
638 seq_printf(seq
, "MACSRC_RND ");
640 if (pkt_dev
->flags
& F_MACDST_RND
)
641 seq_printf(seq
, "MACDST_RND ");
643 if (pkt_dev
->flags
& F_VID_RND
)
644 seq_printf(seq
, "VID_RND ");
646 if (pkt_dev
->flags
& F_SVID_RND
)
647 seq_printf(seq
, "SVID_RND ");
651 sa
= pkt_dev
->started_at
;
652 stopped
= pkt_dev
->stopped_at
;
653 if (pkt_dev
->running
)
654 stopped
= now
; /* not really stopped, more like last-running-at */
657 "Current:\n pkts-sofar: %llu errors: %llu\n started: %lluus stopped: %lluus idle: %lluus\n",
658 (unsigned long long)pkt_dev
->sofar
,
659 (unsigned long long)pkt_dev
->errors
, (unsigned long long)sa
,
660 (unsigned long long)stopped
,
661 (unsigned long long)pkt_dev
->idle_acc
);
664 " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n",
665 pkt_dev
->seq_num
, pkt_dev
->cur_dst_mac_offset
,
666 pkt_dev
->cur_src_mac_offset
);
668 if (pkt_dev
->flags
& F_IPV6
) {
669 char b1
[128], b2
[128];
670 fmt_ip6(b1
, pkt_dev
->cur_in6_daddr
.s6_addr
);
671 fmt_ip6(b2
, pkt_dev
->cur_in6_saddr
.s6_addr
);
672 seq_printf(seq
, " cur_saddr: %s cur_daddr: %s\n", b2
, b1
);
674 seq_printf(seq
, " cur_saddr: 0x%x cur_daddr: 0x%x\n",
675 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
, " cur_queue_map: %u\n", pkt_dev
->cur_queue_map
);
682 seq_printf(seq
, " flows: %u\n", pkt_dev
->nflows
);
684 if (pkt_dev
->result
[0])
685 seq_printf(seq
, "Result: %s\n", pkt_dev
->result
);
687 seq_printf(seq
, "Result: Idle\n");
693 static int hex32_arg(const char __user
*user_buffer
, unsigned long maxlen
, __u32
*num
)
698 for (; i
< maxlen
; i
++) {
701 if (get_user(c
, &user_buffer
[i
]))
703 if ((c
>= '0') && (c
<= '9'))
705 else if ((c
>= 'a') && (c
<= 'f'))
706 *num
|= c
- 'a' + 10;
707 else if ((c
>= 'A') && (c
<= 'F'))
708 *num
|= c
- 'A' + 10;
715 static int count_trail_chars(const char __user
* user_buffer
,
720 for (i
= 0; i
< maxlen
; i
++) {
722 if (get_user(c
, &user_buffer
[i
]))
740 static unsigned long num_arg(const char __user
* user_buffer
,
741 unsigned long maxlen
, unsigned long *num
)
746 for (; i
< maxlen
; i
++) {
748 if (get_user(c
, &user_buffer
[i
]))
750 if ((c
>= '0') && (c
<= '9')) {
759 static int strn_len(const char __user
* user_buffer
, unsigned int maxlen
)
763 for (; i
< maxlen
; i
++) {
765 if (get_user(c
, &user_buffer
[i
]))
783 static ssize_t
get_labels(const char __user
*buffer
, struct pktgen_dev
*pkt_dev
)
790 pkt_dev
->nr_labels
= 0;
793 len
= hex32_arg(&buffer
[i
], 8, &tmp
);
796 pkt_dev
->labels
[n
] = htonl(tmp
);
797 if (pkt_dev
->labels
[n
] & MPLS_STACK_BOTTOM
)
798 pkt_dev
->flags
|= F_MPLS_RND
;
800 if (get_user(c
, &buffer
[i
]))
804 if (n
>= MAX_MPLS_LABELS
)
808 pkt_dev
->nr_labels
= n
;
812 static ssize_t
pktgen_if_write(struct file
*file
,
813 const char __user
* user_buffer
, size_t count
,
816 struct seq_file
*seq
= (struct seq_file
*)file
->private_data
;
817 struct pktgen_dev
*pkt_dev
= seq
->private;
819 char name
[16], valstr
[32];
820 unsigned long value
= 0;
821 char *pg_result
= NULL
;
825 pg_result
= &(pkt_dev
->result
[0]);
828 printk(KERN_WARNING
"pktgen: wrong command format\n");
833 tmp
= count_trail_chars(&user_buffer
[i
], max
);
835 printk(KERN_WARNING
"pktgen: illegal format\n");
840 /* Read variable name */
842 len
= strn_len(&user_buffer
[i
], sizeof(name
) - 1);
846 memset(name
, 0, sizeof(name
));
847 if (copy_from_user(name
, &user_buffer
[i
], len
))
852 len
= count_trail_chars(&user_buffer
[i
], max
);
860 if (copy_from_user(tb
, user_buffer
, count
))
863 printk(KERN_DEBUG
"pktgen: %s,%lu buffer -:%s:-\n", name
,
864 (unsigned long)count
, tb
);
867 if (!strcmp(name
, "min_pkt_size")) {
868 len
= num_arg(&user_buffer
[i
], 10, &value
);
873 if (value
< 14 + 20 + 8)
875 if (value
!= pkt_dev
->min_pkt_size
) {
876 pkt_dev
->min_pkt_size
= value
;
877 pkt_dev
->cur_pkt_size
= value
;
879 sprintf(pg_result
, "OK: min_pkt_size=%u",
880 pkt_dev
->min_pkt_size
);
884 if (!strcmp(name
, "max_pkt_size")) {
885 len
= num_arg(&user_buffer
[i
], 10, &value
);
890 if (value
< 14 + 20 + 8)
892 if (value
!= pkt_dev
->max_pkt_size
) {
893 pkt_dev
->max_pkt_size
= value
;
894 pkt_dev
->cur_pkt_size
= value
;
896 sprintf(pg_result
, "OK: max_pkt_size=%u",
897 pkt_dev
->max_pkt_size
);
901 /* Shortcut for min = max */
903 if (!strcmp(name
, "pkt_size")) {
904 len
= num_arg(&user_buffer
[i
], 10, &value
);
909 if (value
< 14 + 20 + 8)
911 if (value
!= pkt_dev
->min_pkt_size
) {
912 pkt_dev
->min_pkt_size
= value
;
913 pkt_dev
->max_pkt_size
= value
;
914 pkt_dev
->cur_pkt_size
= value
;
916 sprintf(pg_result
, "OK: pkt_size=%u", pkt_dev
->min_pkt_size
);
920 if (!strcmp(name
, "debug")) {
921 len
= num_arg(&user_buffer
[i
], 10, &value
);
927 sprintf(pg_result
, "OK: debug=%u", debug
);
931 if (!strcmp(name
, "frags")) {
932 len
= num_arg(&user_buffer
[i
], 10, &value
);
937 pkt_dev
->nfrags
= value
;
938 sprintf(pg_result
, "OK: frags=%u", pkt_dev
->nfrags
);
941 if (!strcmp(name
, "delay")) {
942 len
= num_arg(&user_buffer
[i
], 10, &value
);
947 if (value
== 0x7FFFFFFF) {
948 pkt_dev
->delay_us
= 0x7FFFFFFF;
949 pkt_dev
->delay_ns
= 0;
951 pkt_dev
->delay_us
= value
/ 1000;
952 pkt_dev
->delay_ns
= value
% 1000;
954 sprintf(pg_result
, "OK: delay=%u",
955 1000 * pkt_dev
->delay_us
+ pkt_dev
->delay_ns
);
958 if (!strcmp(name
, "udp_src_min")) {
959 len
= num_arg(&user_buffer
[i
], 10, &value
);
964 if (value
!= pkt_dev
->udp_src_min
) {
965 pkt_dev
->udp_src_min
= value
;
966 pkt_dev
->cur_udp_src
= value
;
968 sprintf(pg_result
, "OK: udp_src_min=%u", pkt_dev
->udp_src_min
);
971 if (!strcmp(name
, "udp_dst_min")) {
972 len
= num_arg(&user_buffer
[i
], 10, &value
);
977 if (value
!= pkt_dev
->udp_dst_min
) {
978 pkt_dev
->udp_dst_min
= value
;
979 pkt_dev
->cur_udp_dst
= value
;
981 sprintf(pg_result
, "OK: udp_dst_min=%u", pkt_dev
->udp_dst_min
);
984 if (!strcmp(name
, "udp_src_max")) {
985 len
= num_arg(&user_buffer
[i
], 10, &value
);
990 if (value
!= pkt_dev
->udp_src_max
) {
991 pkt_dev
->udp_src_max
= value
;
992 pkt_dev
->cur_udp_src
= value
;
994 sprintf(pg_result
, "OK: udp_src_max=%u", pkt_dev
->udp_src_max
);
997 if (!strcmp(name
, "udp_dst_max")) {
998 len
= num_arg(&user_buffer
[i
], 10, &value
);
1003 if (value
!= pkt_dev
->udp_dst_max
) {
1004 pkt_dev
->udp_dst_max
= value
;
1005 pkt_dev
->cur_udp_dst
= value
;
1007 sprintf(pg_result
, "OK: udp_dst_max=%u", pkt_dev
->udp_dst_max
);
1010 if (!strcmp(name
, "clone_skb")) {
1011 len
= num_arg(&user_buffer
[i
], 10, &value
);
1016 pkt_dev
->clone_skb
= value
;
1018 sprintf(pg_result
, "OK: clone_skb=%d", pkt_dev
->clone_skb
);
1021 if (!strcmp(name
, "count")) {
1022 len
= num_arg(&user_buffer
[i
], 10, &value
);
1027 pkt_dev
->count
= value
;
1028 sprintf(pg_result
, "OK: count=%llu",
1029 (unsigned long long)pkt_dev
->count
);
1032 if (!strcmp(name
, "src_mac_count")) {
1033 len
= num_arg(&user_buffer
[i
], 10, &value
);
1038 if (pkt_dev
->src_mac_count
!= value
) {
1039 pkt_dev
->src_mac_count
= value
;
1040 pkt_dev
->cur_src_mac_offset
= 0;
1042 sprintf(pg_result
, "OK: src_mac_count=%d",
1043 pkt_dev
->src_mac_count
);
1046 if (!strcmp(name
, "dst_mac_count")) {
1047 len
= num_arg(&user_buffer
[i
], 10, &value
);
1052 if (pkt_dev
->dst_mac_count
!= value
) {
1053 pkt_dev
->dst_mac_count
= value
;
1054 pkt_dev
->cur_dst_mac_offset
= 0;
1056 sprintf(pg_result
, "OK: dst_mac_count=%d",
1057 pkt_dev
->dst_mac_count
);
1060 if (!strcmp(name
, "flag")) {
1063 len
= strn_len(&user_buffer
[i
], sizeof(f
) - 1);
1067 if (copy_from_user(f
, &user_buffer
[i
], len
))
1070 if (strcmp(f
, "IPSRC_RND") == 0)
1071 pkt_dev
->flags
|= F_IPSRC_RND
;
1073 else if (strcmp(f
, "!IPSRC_RND") == 0)
1074 pkt_dev
->flags
&= ~F_IPSRC_RND
;
1076 else if (strcmp(f
, "TXSIZE_RND") == 0)
1077 pkt_dev
->flags
|= F_TXSIZE_RND
;
1079 else if (strcmp(f
, "!TXSIZE_RND") == 0)
1080 pkt_dev
->flags
&= ~F_TXSIZE_RND
;
1082 else if (strcmp(f
, "IPDST_RND") == 0)
1083 pkt_dev
->flags
|= F_IPDST_RND
;
1085 else if (strcmp(f
, "!IPDST_RND") == 0)
1086 pkt_dev
->flags
&= ~F_IPDST_RND
;
1088 else if (strcmp(f
, "UDPSRC_RND") == 0)
1089 pkt_dev
->flags
|= F_UDPSRC_RND
;
1091 else if (strcmp(f
, "!UDPSRC_RND") == 0)
1092 pkt_dev
->flags
&= ~F_UDPSRC_RND
;
1094 else if (strcmp(f
, "UDPDST_RND") == 0)
1095 pkt_dev
->flags
|= F_UDPDST_RND
;
1097 else if (strcmp(f
, "!UDPDST_RND") == 0)
1098 pkt_dev
->flags
&= ~F_UDPDST_RND
;
1100 else if (strcmp(f
, "MACSRC_RND") == 0)
1101 pkt_dev
->flags
|= F_MACSRC_RND
;
1103 else if (strcmp(f
, "!MACSRC_RND") == 0)
1104 pkt_dev
->flags
&= ~F_MACSRC_RND
;
1106 else if (strcmp(f
, "MACDST_RND") == 0)
1107 pkt_dev
->flags
|= F_MACDST_RND
;
1109 else if (strcmp(f
, "!MACDST_RND") == 0)
1110 pkt_dev
->flags
&= ~F_MACDST_RND
;
1112 else if (strcmp(f
, "MPLS_RND") == 0)
1113 pkt_dev
->flags
|= F_MPLS_RND
;
1115 else if (strcmp(f
, "!MPLS_RND") == 0)
1116 pkt_dev
->flags
&= ~F_MPLS_RND
;
1118 else if (strcmp(f
, "VID_RND") == 0)
1119 pkt_dev
->flags
|= F_VID_RND
;
1121 else if (strcmp(f
, "!VID_RND") == 0)
1122 pkt_dev
->flags
&= ~F_VID_RND
;
1124 else if (strcmp(f
, "SVID_RND") == 0)
1125 pkt_dev
->flags
|= F_SVID_RND
;
1127 else if (strcmp(f
, "!SVID_RND") == 0)
1128 pkt_dev
->flags
&= ~F_SVID_RND
;
1130 else if (strcmp(f
, "FLOW_SEQ") == 0)
1131 pkt_dev
->flags
|= F_FLOW_SEQ
;
1133 else if (strcmp(f
, "QUEUE_MAP_RND") == 0)
1134 pkt_dev
->flags
|= F_QUEUE_MAP_RND
;
1136 else if (strcmp(f
, "!QUEUE_MAP_RND") == 0)
1137 pkt_dev
->flags
&= ~F_QUEUE_MAP_RND
;
1139 else if (strcmp(f
, "IPSEC") == 0)
1140 pkt_dev
->flags
|= F_IPSEC_ON
;
1143 else if (strcmp(f
, "!IPV6") == 0)
1144 pkt_dev
->flags
&= ~F_IPV6
;
1148 "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
1150 "IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
1151 "MACSRC_RND, MACDST_RND, TXSIZE_RND, IPV6, MPLS_RND, VID_RND, SVID_RND, FLOW_SEQ, IPSEC\n");
1154 sprintf(pg_result
, "OK: flags=0x%x", pkt_dev
->flags
);
1157 if (!strcmp(name
, "dst_min") || !strcmp(name
, "dst")) {
1158 len
= strn_len(&user_buffer
[i
], sizeof(pkt_dev
->dst_min
) - 1);
1163 if (copy_from_user(buf
, &user_buffer
[i
], len
))
1166 if (strcmp(buf
, pkt_dev
->dst_min
) != 0) {
1167 memset(pkt_dev
->dst_min
, 0, sizeof(pkt_dev
->dst_min
));
1168 strncpy(pkt_dev
->dst_min
, buf
, len
);
1169 pkt_dev
->daddr_min
= in_aton(pkt_dev
->dst_min
);
1170 pkt_dev
->cur_daddr
= pkt_dev
->daddr_min
;
1173 printk(KERN_DEBUG
"pktgen: dst_min set to: %s\n",
1176 sprintf(pg_result
, "OK: dst_min=%s", pkt_dev
->dst_min
);
1179 if (!strcmp(name
, "dst_max")) {
1180 len
= strn_len(&user_buffer
[i
], sizeof(pkt_dev
->dst_max
) - 1);
1185 if (copy_from_user(buf
, &user_buffer
[i
], len
))
1189 if (strcmp(buf
, pkt_dev
->dst_max
) != 0) {
1190 memset(pkt_dev
->dst_max
, 0, sizeof(pkt_dev
->dst_max
));
1191 strncpy(pkt_dev
->dst_max
, buf
, len
);
1192 pkt_dev
->daddr_max
= in_aton(pkt_dev
->dst_max
);
1193 pkt_dev
->cur_daddr
= pkt_dev
->daddr_max
;
1196 printk(KERN_DEBUG
"pktgen: dst_max set to: %s\n",
1199 sprintf(pg_result
, "OK: dst_max=%s", pkt_dev
->dst_max
);
1202 if (!strcmp(name
, "dst6")) {
1203 len
= strn_len(&user_buffer
[i
], sizeof(buf
) - 1);
1207 pkt_dev
->flags
|= F_IPV6
;
1209 if (copy_from_user(buf
, &user_buffer
[i
], len
))
1213 scan_ip6(buf
, pkt_dev
->in6_daddr
.s6_addr
);
1214 fmt_ip6(buf
, pkt_dev
->in6_daddr
.s6_addr
);
1216 ipv6_addr_copy(&pkt_dev
->cur_in6_daddr
, &pkt_dev
->in6_daddr
);
1219 printk(KERN_DEBUG
"pktgen: dst6 set to: %s\n", buf
);
1222 sprintf(pg_result
, "OK: dst6=%s", buf
);
1225 if (!strcmp(name
, "dst6_min")) {
1226 len
= strn_len(&user_buffer
[i
], sizeof(buf
) - 1);
1230 pkt_dev
->flags
|= F_IPV6
;
1232 if (copy_from_user(buf
, &user_buffer
[i
], len
))
1236 scan_ip6(buf
, pkt_dev
->min_in6_daddr
.s6_addr
);
1237 fmt_ip6(buf
, pkt_dev
->min_in6_daddr
.s6_addr
);
1239 ipv6_addr_copy(&pkt_dev
->cur_in6_daddr
,
1240 &pkt_dev
->min_in6_daddr
);
1242 printk(KERN_DEBUG
"pktgen: dst6_min set to: %s\n", buf
);
1245 sprintf(pg_result
, "OK: dst6_min=%s", buf
);
1248 if (!strcmp(name
, "dst6_max")) {
1249 len
= strn_len(&user_buffer
[i
], sizeof(buf
) - 1);
1253 pkt_dev
->flags
|= F_IPV6
;
1255 if (copy_from_user(buf
, &user_buffer
[i
], len
))
1259 scan_ip6(buf
, pkt_dev
->max_in6_daddr
.s6_addr
);
1260 fmt_ip6(buf
, pkt_dev
->max_in6_daddr
.s6_addr
);
1263 printk(KERN_DEBUG
"pktgen: dst6_max set to: %s\n", buf
);
1266 sprintf(pg_result
, "OK: dst6_max=%s", buf
);
1269 if (!strcmp(name
, "src6")) {
1270 len
= strn_len(&user_buffer
[i
], sizeof(buf
) - 1);
1274 pkt_dev
->flags
|= F_IPV6
;
1276 if (copy_from_user(buf
, &user_buffer
[i
], len
))
1280 scan_ip6(buf
, pkt_dev
->in6_saddr
.s6_addr
);
1281 fmt_ip6(buf
, pkt_dev
->in6_saddr
.s6_addr
);
1283 ipv6_addr_copy(&pkt_dev
->cur_in6_saddr
, &pkt_dev
->in6_saddr
);
1286 printk(KERN_DEBUG
"pktgen: src6 set to: %s\n", buf
);
1289 sprintf(pg_result
, "OK: src6=%s", buf
);
1292 if (!strcmp(name
, "src_min")) {
1293 len
= strn_len(&user_buffer
[i
], sizeof(pkt_dev
->src_min
) - 1);
1297 if (copy_from_user(buf
, &user_buffer
[i
], len
))
1300 if (strcmp(buf
, pkt_dev
->src_min
) != 0) {
1301 memset(pkt_dev
->src_min
, 0, sizeof(pkt_dev
->src_min
));
1302 strncpy(pkt_dev
->src_min
, buf
, len
);
1303 pkt_dev
->saddr_min
= in_aton(pkt_dev
->src_min
);
1304 pkt_dev
->cur_saddr
= pkt_dev
->saddr_min
;
1307 printk(KERN_DEBUG
"pktgen: src_min set to: %s\n",
1310 sprintf(pg_result
, "OK: src_min=%s", pkt_dev
->src_min
);
1313 if (!strcmp(name
, "src_max")) {
1314 len
= strn_len(&user_buffer
[i
], sizeof(pkt_dev
->src_max
) - 1);
1318 if (copy_from_user(buf
, &user_buffer
[i
], len
))
1321 if (strcmp(buf
, pkt_dev
->src_max
) != 0) {
1322 memset(pkt_dev
->src_max
, 0, sizeof(pkt_dev
->src_max
));
1323 strncpy(pkt_dev
->src_max
, buf
, len
);
1324 pkt_dev
->saddr_max
= in_aton(pkt_dev
->src_max
);
1325 pkt_dev
->cur_saddr
= pkt_dev
->saddr_max
;
1328 printk(KERN_DEBUG
"pktgen: src_max set to: %s\n",
1331 sprintf(pg_result
, "OK: src_max=%s", pkt_dev
->src_max
);
1334 if (!strcmp(name
, "dst_mac")) {
1336 unsigned char old_dmac
[ETH_ALEN
];
1337 unsigned char *m
= pkt_dev
->dst_mac
;
1338 memcpy(old_dmac
, pkt_dev
->dst_mac
, ETH_ALEN
);
1340 len
= strn_len(&user_buffer
[i
], sizeof(valstr
) - 1);
1344 memset(valstr
, 0, sizeof(valstr
));
1345 if (copy_from_user(valstr
, &user_buffer
[i
], len
))
1349 for (*m
= 0; *v
&& m
< pkt_dev
->dst_mac
+ 6; v
++) {
1350 if (*v
>= '0' && *v
<= '9') {
1354 if (*v
>= 'A' && *v
<= 'F') {
1356 *m
+= *v
- 'A' + 10;
1358 if (*v
>= 'a' && *v
<= 'f') {
1360 *m
+= *v
- 'a' + 10;
1368 /* Set up Dest MAC */
1369 if (compare_ether_addr(old_dmac
, pkt_dev
->dst_mac
))
1370 memcpy(&(pkt_dev
->hh
[0]), pkt_dev
->dst_mac
, ETH_ALEN
);
1372 sprintf(pg_result
, "OK: dstmac");
1375 if (!strcmp(name
, "src_mac")) {
1377 unsigned char old_smac
[ETH_ALEN
];
1378 unsigned char *m
= pkt_dev
->src_mac
;
1380 memcpy(old_smac
, pkt_dev
->src_mac
, ETH_ALEN
);
1382 len
= strn_len(&user_buffer
[i
], sizeof(valstr
) - 1);
1386 memset(valstr
, 0, sizeof(valstr
));
1387 if (copy_from_user(valstr
, &user_buffer
[i
], len
))
1391 for (*m
= 0; *v
&& m
< pkt_dev
->src_mac
+ 6; v
++) {
1392 if (*v
>= '0' && *v
<= '9') {
1396 if (*v
>= 'A' && *v
<= 'F') {
1398 *m
+= *v
- 'A' + 10;
1400 if (*v
>= 'a' && *v
<= 'f') {
1402 *m
+= *v
- 'a' + 10;
1410 /* Set up Src MAC */
1411 if (compare_ether_addr(old_smac
, pkt_dev
->src_mac
))
1412 memcpy(&(pkt_dev
->hh
[6]), pkt_dev
->src_mac
, ETH_ALEN
);
1414 sprintf(pg_result
, "OK: srcmac");
1418 if (!strcmp(name
, "clear_counters")) {
1419 pktgen_clear_counters(pkt_dev
);
1420 sprintf(pg_result
, "OK: Clearing counters.\n");
1424 if (!strcmp(name
, "flows")) {
1425 len
= num_arg(&user_buffer
[i
], 10, &value
);
1430 if (value
> MAX_CFLOWS
)
1433 pkt_dev
->cflows
= value
;
1434 sprintf(pg_result
, "OK: flows=%u", pkt_dev
->cflows
);
1438 if (!strcmp(name
, "flowlen")) {
1439 len
= num_arg(&user_buffer
[i
], 10, &value
);
1444 pkt_dev
->lflow
= value
;
1445 sprintf(pg_result
, "OK: flowlen=%u", pkt_dev
->lflow
);
1449 if (!strcmp(name
, "queue_map_min")) {
1450 len
= num_arg(&user_buffer
[i
], 5, &value
);
1455 pkt_dev
->queue_map_min
= value
;
1456 sprintf(pg_result
, "OK: queue_map_min=%u", pkt_dev
->queue_map_min
);
1460 if (!strcmp(name
, "queue_map_max")) {
1461 len
= num_arg(&user_buffer
[i
], 5, &value
);
1466 pkt_dev
->queue_map_max
= value
;
1467 sprintf(pg_result
, "OK: queue_map_max=%u", pkt_dev
->queue_map_max
);
1471 if (!strcmp(name
, "mpls")) {
1474 len
= get_labels(&user_buffer
[i
], pkt_dev
);
1478 cnt
= sprintf(pg_result
, "OK: mpls=");
1479 for (n
= 0; n
< pkt_dev
->nr_labels
; n
++)
1480 cnt
+= sprintf(pg_result
+ cnt
,
1481 "%08x%s", ntohl(pkt_dev
->labels
[n
]),
1482 n
== pkt_dev
->nr_labels
-1 ? "" : ",");
1484 if (pkt_dev
->nr_labels
&& pkt_dev
->vlan_id
!= 0xffff) {
1485 pkt_dev
->vlan_id
= 0xffff; /* turn off VLAN/SVLAN */
1486 pkt_dev
->svlan_id
= 0xffff;
1489 printk(KERN_DEBUG
"pktgen: VLAN/SVLAN auto turned off\n");
1494 if (!strcmp(name
, "vlan_id")) {
1495 len
= num_arg(&user_buffer
[i
], 4, &value
);
1500 if (value
<= 4095) {
1501 pkt_dev
->vlan_id
= value
; /* turn on VLAN */
1504 printk(KERN_DEBUG
"pktgen: VLAN turned on\n");
1506 if (debug
&& pkt_dev
->nr_labels
)
1507 printk(KERN_DEBUG
"pktgen: MPLS auto turned off\n");
1509 pkt_dev
->nr_labels
= 0; /* turn off MPLS */
1510 sprintf(pg_result
, "OK: vlan_id=%u", pkt_dev
->vlan_id
);
1512 pkt_dev
->vlan_id
= 0xffff; /* turn off VLAN/SVLAN */
1513 pkt_dev
->svlan_id
= 0xffff;
1516 printk(KERN_DEBUG
"pktgen: VLAN/SVLAN turned off\n");
1521 if (!strcmp(name
, "vlan_p")) {
1522 len
= num_arg(&user_buffer
[i
], 1, &value
);
1527 if ((value
<= 7) && (pkt_dev
->vlan_id
!= 0xffff)) {
1528 pkt_dev
->vlan_p
= value
;
1529 sprintf(pg_result
, "OK: vlan_p=%u", pkt_dev
->vlan_p
);
1531 sprintf(pg_result
, "ERROR: vlan_p must be 0-7");
1536 if (!strcmp(name
, "vlan_cfi")) {
1537 len
= num_arg(&user_buffer
[i
], 1, &value
);
1542 if ((value
<= 1) && (pkt_dev
->vlan_id
!= 0xffff)) {
1543 pkt_dev
->vlan_cfi
= value
;
1544 sprintf(pg_result
, "OK: vlan_cfi=%u", pkt_dev
->vlan_cfi
);
1546 sprintf(pg_result
, "ERROR: vlan_cfi must be 0-1");
1551 if (!strcmp(name
, "svlan_id")) {
1552 len
= num_arg(&user_buffer
[i
], 4, &value
);
1557 if ((value
<= 4095) && ((pkt_dev
->vlan_id
!= 0xffff))) {
1558 pkt_dev
->svlan_id
= value
; /* turn on SVLAN */
1561 printk(KERN_DEBUG
"pktgen: SVLAN turned on\n");
1563 if (debug
&& pkt_dev
->nr_labels
)
1564 printk(KERN_DEBUG
"pktgen: MPLS auto turned off\n");
1566 pkt_dev
->nr_labels
= 0; /* turn off MPLS */
1567 sprintf(pg_result
, "OK: svlan_id=%u", pkt_dev
->svlan_id
);
1569 pkt_dev
->vlan_id
= 0xffff; /* turn off VLAN/SVLAN */
1570 pkt_dev
->svlan_id
= 0xffff;
1573 printk(KERN_DEBUG
"pktgen: VLAN/SVLAN turned off\n");
1578 if (!strcmp(name
, "svlan_p")) {
1579 len
= num_arg(&user_buffer
[i
], 1, &value
);
1584 if ((value
<= 7) && (pkt_dev
->svlan_id
!= 0xffff)) {
1585 pkt_dev
->svlan_p
= value
;
1586 sprintf(pg_result
, "OK: svlan_p=%u", pkt_dev
->svlan_p
);
1588 sprintf(pg_result
, "ERROR: svlan_p must be 0-7");
1593 if (!strcmp(name
, "svlan_cfi")) {
1594 len
= num_arg(&user_buffer
[i
], 1, &value
);
1599 if ((value
<= 1) && (pkt_dev
->svlan_id
!= 0xffff)) {
1600 pkt_dev
->svlan_cfi
= value
;
1601 sprintf(pg_result
, "OK: svlan_cfi=%u", pkt_dev
->svlan_cfi
);
1603 sprintf(pg_result
, "ERROR: svlan_cfi must be 0-1");
1608 if (!strcmp(name
, "tos")) {
1609 __u32 tmp_value
= 0;
1610 len
= hex32_arg(&user_buffer
[i
], 2, &tmp_value
);
1616 pkt_dev
->tos
= tmp_value
;
1617 sprintf(pg_result
, "OK: tos=0x%02x", pkt_dev
->tos
);
1619 sprintf(pg_result
, "ERROR: tos must be 00-ff");
1624 if (!strcmp(name
, "traffic_class")) {
1625 __u32 tmp_value
= 0;
1626 len
= hex32_arg(&user_buffer
[i
], 2, &tmp_value
);
1632 pkt_dev
->traffic_class
= tmp_value
;
1633 sprintf(pg_result
, "OK: traffic_class=0x%02x", pkt_dev
->traffic_class
);
1635 sprintf(pg_result
, "ERROR: traffic_class must be 00-ff");
1640 sprintf(pkt_dev
->result
, "No such parameter \"%s\"", name
);
1644 static int pktgen_if_open(struct inode
*inode
, struct file
*file
)
1646 return single_open(file
, pktgen_if_show
, PDE(inode
)->data
);
1649 static const struct file_operations pktgen_if_fops
= {
1650 .owner
= THIS_MODULE
,
1651 .open
= pktgen_if_open
,
1653 .llseek
= seq_lseek
,
1654 .write
= pktgen_if_write
,
1655 .release
= single_release
,
1658 static int pktgen_thread_show(struct seq_file
*seq
, void *v
)
1660 struct pktgen_thread
*t
= seq
->private;
1661 struct pktgen_dev
*pkt_dev
;
1665 seq_printf(seq
, "Running: ");
1668 list_for_each_entry(pkt_dev
, &t
->if_list
, list
)
1669 if (pkt_dev
->running
)
1670 seq_printf(seq
, "%s ", pkt_dev
->odev
->name
);
1672 seq_printf(seq
, "\nStopped: ");
1674 list_for_each_entry(pkt_dev
, &t
->if_list
, list
)
1675 if (!pkt_dev
->running
)
1676 seq_printf(seq
, "%s ", pkt_dev
->odev
->name
);
1679 seq_printf(seq
, "\nResult: %s\n", t
->result
);
1681 seq_printf(seq
, "\nResult: NA\n");
1688 static ssize_t
pktgen_thread_write(struct file
*file
,
1689 const char __user
* user_buffer
,
1690 size_t count
, loff_t
* offset
)
1692 struct seq_file
*seq
= (struct seq_file
*)file
->private_data
;
1693 struct pktgen_thread
*t
= seq
->private;
1694 int i
= 0, max
, len
, ret
;
1699 // sprintf(pg_result, "Wrong command format");
1704 len
= count_trail_chars(&user_buffer
[i
], max
);
1710 /* Read variable name */
1712 len
= strn_len(&user_buffer
[i
], sizeof(name
) - 1);
1716 memset(name
, 0, sizeof(name
));
1717 if (copy_from_user(name
, &user_buffer
[i
], len
))
1722 len
= count_trail_chars(&user_buffer
[i
], max
);
1729 printk(KERN_DEBUG
"pktgen: t=%s, count=%lu\n",
1730 name
, (unsigned long)count
);
1733 printk(KERN_ERR
"pktgen: ERROR: No thread\n");
1738 pg_result
= &(t
->result
[0]);
1740 if (!strcmp(name
, "add_device")) {
1743 len
= strn_len(&user_buffer
[i
], sizeof(f
) - 1);
1748 if (copy_from_user(f
, &user_buffer
[i
], len
))
1751 mutex_lock(&pktgen_thread_lock
);
1752 pktgen_add_device(t
, f
);
1753 mutex_unlock(&pktgen_thread_lock
);
1755 sprintf(pg_result
, "OK: add_device=%s", f
);
1759 if (!strcmp(name
, "rem_device_all")) {
1760 mutex_lock(&pktgen_thread_lock
);
1761 t
->control
|= T_REMDEVALL
;
1762 mutex_unlock(&pktgen_thread_lock
);
1763 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
1765 sprintf(pg_result
, "OK: rem_device_all");
1769 if (!strcmp(name
, "max_before_softirq")) {
1770 sprintf(pg_result
, "OK: Note! max_before_softirq is obsoleted -- Do not use");
1780 static int pktgen_thread_open(struct inode
*inode
, struct file
*file
)
1782 return single_open(file
, pktgen_thread_show
, PDE(inode
)->data
);
1785 static const struct file_operations pktgen_thread_fops
= {
1786 .owner
= THIS_MODULE
,
1787 .open
= pktgen_thread_open
,
1789 .llseek
= seq_lseek
,
1790 .write
= pktgen_thread_write
,
1791 .release
= single_release
,
1794 /* Think find or remove for NN */
1795 static struct pktgen_dev
*__pktgen_NN_threads(const char *ifname
, int remove
)
1797 struct pktgen_thread
*t
;
1798 struct pktgen_dev
*pkt_dev
= NULL
;
1800 list_for_each_entry(t
, &pktgen_threads
, th_list
) {
1801 pkt_dev
= pktgen_find_dev(t
, ifname
);
1805 pkt_dev
->removal_mark
= 1;
1806 t
->control
|= T_REMDEV
;
1816 * mark a device for removal
1818 static void pktgen_mark_device(const char *ifname
)
1820 struct pktgen_dev
*pkt_dev
= NULL
;
1821 const int max_tries
= 10, msec_per_try
= 125;
1824 mutex_lock(&pktgen_thread_lock
);
1825 pr_debug("pktgen: pktgen_mark_device marking %s for removal\n", ifname
);
1829 pkt_dev
= __pktgen_NN_threads(ifname
, REMOVE
);
1830 if (pkt_dev
== NULL
)
1831 break; /* success */
1833 mutex_unlock(&pktgen_thread_lock
);
1834 pr_debug("pktgen: pktgen_mark_device waiting for %s "
1835 "to disappear....\n", ifname
);
1836 schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try
));
1837 mutex_lock(&pktgen_thread_lock
);
1839 if (++i
>= max_tries
) {
1840 printk(KERN_ERR
"pktgen_mark_device: timed out after "
1841 "waiting %d msec for device %s to be removed\n",
1842 msec_per_try
* i
, ifname
);
1848 mutex_unlock(&pktgen_thread_lock
);
1851 static void pktgen_change_name(struct net_device
*dev
)
1853 struct pktgen_thread
*t
;
1855 list_for_each_entry(t
, &pktgen_threads
, th_list
) {
1856 struct pktgen_dev
*pkt_dev
;
1858 list_for_each_entry(pkt_dev
, &t
->if_list
, list
) {
1859 if (pkt_dev
->odev
!= dev
)
1862 remove_proc_entry(pkt_dev
->entry
->name
, pg_proc_dir
);
1864 pkt_dev
->entry
= create_proc_entry(dev
->name
, 0600,
1866 if (!pkt_dev
->entry
)
1867 printk(KERN_ERR
"pktgen: can't move proc "
1868 " entry for '%s'\n", dev
->name
);
1874 static int pktgen_device_event(struct notifier_block
*unused
,
1875 unsigned long event
, void *ptr
)
1877 struct net_device
*dev
= ptr
;
1879 if (dev
->nd_net
!= &init_net
)
1882 /* It is OK that we do not hold the group lock right now,
1883 * as we run under the RTNL lock.
1887 case NETDEV_CHANGENAME
:
1888 pktgen_change_name(dev
);
1891 case NETDEV_UNREGISTER
:
1892 pktgen_mark_device(dev
->name
);
1899 /* Associate pktgen_dev with a device. */
1901 static int pktgen_setup_dev(struct pktgen_dev
*pkt_dev
, const char *ifname
)
1903 struct net_device
*odev
;
1906 /* Clean old setups */
1907 if (pkt_dev
->odev
) {
1908 dev_put(pkt_dev
->odev
);
1909 pkt_dev
->odev
= NULL
;
1912 odev
= dev_get_by_name(&init_net
, ifname
);
1914 printk(KERN_ERR
"pktgen: no such netdevice: \"%s\"\n", ifname
);
1918 if (odev
->type
!= ARPHRD_ETHER
) {
1919 printk(KERN_ERR
"pktgen: not an ethernet device: \"%s\"\n", ifname
);
1921 } else if (!netif_running(odev
)) {
1922 printk(KERN_ERR
"pktgen: device is down: \"%s\"\n", ifname
);
1925 pkt_dev
->odev
= odev
;
1933 /* Read pkt_dev from the interface and set up internal pktgen_dev
1934 * structure to have the right information to create/send packets
1936 static void pktgen_setup_inject(struct pktgen_dev
*pkt_dev
)
1938 if (!pkt_dev
->odev
) {
1939 printk(KERN_ERR
"pktgen: ERROR: pkt_dev->odev == NULL in "
1941 sprintf(pkt_dev
->result
,
1942 "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
1946 /* Default to the interface's mac if not explicitly set. */
1948 if (is_zero_ether_addr(pkt_dev
->src_mac
))
1949 memcpy(&(pkt_dev
->hh
[6]), pkt_dev
->odev
->dev_addr
, ETH_ALEN
);
1951 /* Set up Dest MAC */
1952 memcpy(&(pkt_dev
->hh
[0]), pkt_dev
->dst_mac
, ETH_ALEN
);
1954 /* Set up pkt size */
1955 pkt_dev
->cur_pkt_size
= pkt_dev
->min_pkt_size
;
1957 if (pkt_dev
->flags
& F_IPV6
) {
1959 * Skip this automatic address setting until locks or functions
1964 int i
, set
= 0, err
= 1;
1965 struct inet6_dev
*idev
;
1967 for (i
= 0; i
< IN6_ADDR_HSIZE
; i
++)
1968 if (pkt_dev
->cur_in6_saddr
.s6_addr
[i
]) {
1976 * Use linklevel address if unconfigured.
1978 * use ipv6_get_lladdr if/when it's get exported
1982 if ((idev
= __in6_dev_get(pkt_dev
->odev
)) != NULL
) {
1983 struct inet6_ifaddr
*ifp
;
1985 read_lock_bh(&idev
->lock
);
1986 for (ifp
= idev
->addr_list
; ifp
;
1987 ifp
= ifp
->if_next
) {
1988 if (ifp
->scope
== IFA_LINK
1990 flags
& IFA_F_TENTATIVE
)) {
1991 ipv6_addr_copy(&pkt_dev
->
1998 read_unlock_bh(&idev
->lock
);
2002 printk(KERN_ERR
"pktgen: ERROR: IPv6 link "
2003 "address not availble.\n");
2007 pkt_dev
->saddr_min
= 0;
2008 pkt_dev
->saddr_max
= 0;
2009 if (strlen(pkt_dev
->src_min
) == 0) {
2011 struct in_device
*in_dev
;
2014 in_dev
= __in_dev_get_rcu(pkt_dev
->odev
);
2016 if (in_dev
->ifa_list
) {
2017 pkt_dev
->saddr_min
=
2018 in_dev
->ifa_list
->ifa_address
;
2019 pkt_dev
->saddr_max
= pkt_dev
->saddr_min
;
2024 pkt_dev
->saddr_min
= in_aton(pkt_dev
->src_min
);
2025 pkt_dev
->saddr_max
= in_aton(pkt_dev
->src_max
);
2028 pkt_dev
->daddr_min
= in_aton(pkt_dev
->dst_min
);
2029 pkt_dev
->daddr_max
= in_aton(pkt_dev
->dst_max
);
2031 /* Initialize current values. */
2032 pkt_dev
->cur_dst_mac_offset
= 0;
2033 pkt_dev
->cur_src_mac_offset
= 0;
2034 pkt_dev
->cur_saddr
= pkt_dev
->saddr_min
;
2035 pkt_dev
->cur_daddr
= pkt_dev
->daddr_min
;
2036 pkt_dev
->cur_udp_dst
= pkt_dev
->udp_dst_min
;
2037 pkt_dev
->cur_udp_src
= pkt_dev
->udp_src_min
;
2038 pkt_dev
->nflows
= 0;
2041 static void spin(struct pktgen_dev
*pkt_dev
, __u64 spin_until_us
)
2046 start
= now
= getCurUs();
2047 printk(KERN_INFO
"sleeping for %d\n", (int)(spin_until_us
- now
));
2048 while (now
< spin_until_us
) {
2049 /* TODO: optimize sleeping behavior */
2050 if (spin_until_us
- now
> jiffies_to_usecs(1) + 1)
2051 schedule_timeout_interruptible(1);
2052 else if (spin_until_us
- now
> 100) {
2053 if (!pkt_dev
->running
)
2062 pkt_dev
->idle_acc
+= now
- start
;
2065 static inline void set_pkt_overhead(struct pktgen_dev
*pkt_dev
)
2067 pkt_dev
->pkt_overhead
= 0;
2068 pkt_dev
->pkt_overhead
+= pkt_dev
->nr_labels
*sizeof(u32
);
2069 pkt_dev
->pkt_overhead
+= VLAN_TAG_SIZE(pkt_dev
);
2070 pkt_dev
->pkt_overhead
+= SVLAN_TAG_SIZE(pkt_dev
);
2073 static inline int f_seen(struct pktgen_dev
*pkt_dev
, int flow
)
2076 if (pkt_dev
->flows
[flow
].flags
& F_INIT
)
2082 static inline int f_pick(struct pktgen_dev
*pkt_dev
)
2084 int flow
= pkt_dev
->curfl
;
2086 if (pkt_dev
->flags
& F_FLOW_SEQ
) {
2087 if (pkt_dev
->flows
[flow
].count
>= pkt_dev
->lflow
) {
2089 pkt_dev
->flows
[flow
].count
= 0;
2090 pkt_dev
->curfl
+= 1;
2091 if (pkt_dev
->curfl
>= pkt_dev
->cflows
)
2092 pkt_dev
->curfl
= 0; /*reset */
2095 flow
= random32() % pkt_dev
->cflows
;
2097 if (pkt_dev
->flows
[flow
].count
> pkt_dev
->lflow
)
2098 pkt_dev
->flows
[flow
].count
= 0;
2101 return pkt_dev
->curfl
;
2106 /* If there was already an IPSEC SA, we keep it as is, else
2107 * we go look for it ...
2109 static void get_ipsec_sa(struct pktgen_dev
*pkt_dev
, int flow
)
2111 struct xfrm_state
*x
= pkt_dev
->flows
[flow
].x
;
2113 /*slow path: we dont already have xfrm_state*/
2114 x
= xfrm_stateonly_find((xfrm_address_t
*)&pkt_dev
->cur_daddr
,
2115 (xfrm_address_t
*)&pkt_dev
->cur_saddr
,
2118 pkt_dev
->ipsproto
, 0);
2120 pkt_dev
->flows
[flow
].x
= x
;
2121 set_pkt_overhead(pkt_dev
);
2122 pkt_dev
->pkt_overhead
+=x
->props
.header_len
;
2128 /* Increment/randomize headers according to flags and current values
2129 * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
2131 static void mod_cur_headers(struct pktgen_dev
*pkt_dev
)
2137 if (pkt_dev
->cflows
)
2138 flow
= f_pick(pkt_dev
);
2140 /* Deal with source MAC */
2141 if (pkt_dev
->src_mac_count
> 1) {
2145 if (pkt_dev
->flags
& F_MACSRC_RND
)
2146 mc
= random32() % pkt_dev
->src_mac_count
;
2148 mc
= pkt_dev
->cur_src_mac_offset
++;
2149 if (pkt_dev
->cur_src_mac_offset
>
2150 pkt_dev
->src_mac_count
)
2151 pkt_dev
->cur_src_mac_offset
= 0;
2154 tmp
= pkt_dev
->src_mac
[5] + (mc
& 0xFF);
2155 pkt_dev
->hh
[11] = tmp
;
2156 tmp
= (pkt_dev
->src_mac
[4] + ((mc
>> 8) & 0xFF) + (tmp
>> 8));
2157 pkt_dev
->hh
[10] = tmp
;
2158 tmp
= (pkt_dev
->src_mac
[3] + ((mc
>> 16) & 0xFF) + (tmp
>> 8));
2159 pkt_dev
->hh
[9] = tmp
;
2160 tmp
= (pkt_dev
->src_mac
[2] + ((mc
>> 24) & 0xFF) + (tmp
>> 8));
2161 pkt_dev
->hh
[8] = tmp
;
2162 tmp
= (pkt_dev
->src_mac
[1] + (tmp
>> 8));
2163 pkt_dev
->hh
[7] = tmp
;
2166 /* Deal with Destination MAC */
2167 if (pkt_dev
->dst_mac_count
> 1) {
2171 if (pkt_dev
->flags
& F_MACDST_RND
)
2172 mc
= random32() % pkt_dev
->dst_mac_count
;
2175 mc
= pkt_dev
->cur_dst_mac_offset
++;
2176 if (pkt_dev
->cur_dst_mac_offset
>
2177 pkt_dev
->dst_mac_count
) {
2178 pkt_dev
->cur_dst_mac_offset
= 0;
2182 tmp
= pkt_dev
->dst_mac
[5] + (mc
& 0xFF);
2183 pkt_dev
->hh
[5] = tmp
;
2184 tmp
= (pkt_dev
->dst_mac
[4] + ((mc
>> 8) & 0xFF) + (tmp
>> 8));
2185 pkt_dev
->hh
[4] = tmp
;
2186 tmp
= (pkt_dev
->dst_mac
[3] + ((mc
>> 16) & 0xFF) + (tmp
>> 8));
2187 pkt_dev
->hh
[3] = tmp
;
2188 tmp
= (pkt_dev
->dst_mac
[2] + ((mc
>> 24) & 0xFF) + (tmp
>> 8));
2189 pkt_dev
->hh
[2] = tmp
;
2190 tmp
= (pkt_dev
->dst_mac
[1] + (tmp
>> 8));
2191 pkt_dev
->hh
[1] = tmp
;
2194 if (pkt_dev
->flags
& F_MPLS_RND
) {
2196 for (i
= 0; i
< pkt_dev
->nr_labels
; i
++)
2197 if (pkt_dev
->labels
[i
] & MPLS_STACK_BOTTOM
)
2198 pkt_dev
->labels
[i
] = MPLS_STACK_BOTTOM
|
2199 ((__force __be32
)random32() &
2203 if ((pkt_dev
->flags
& F_VID_RND
) && (pkt_dev
->vlan_id
!= 0xffff)) {
2204 pkt_dev
->vlan_id
= random32() & (4096-1);
2207 if ((pkt_dev
->flags
& F_SVID_RND
) && (pkt_dev
->svlan_id
!= 0xffff)) {
2208 pkt_dev
->svlan_id
= random32() & (4096 - 1);
2211 if (pkt_dev
->udp_src_min
< pkt_dev
->udp_src_max
) {
2212 if (pkt_dev
->flags
& F_UDPSRC_RND
)
2213 pkt_dev
->cur_udp_src
= random32() %
2214 (pkt_dev
->udp_src_max
- pkt_dev
->udp_src_min
)
2215 + pkt_dev
->udp_src_min
;
2218 pkt_dev
->cur_udp_src
++;
2219 if (pkt_dev
->cur_udp_src
>= pkt_dev
->udp_src_max
)
2220 pkt_dev
->cur_udp_src
= pkt_dev
->udp_src_min
;
2224 if (pkt_dev
->udp_dst_min
< pkt_dev
->udp_dst_max
) {
2225 if (pkt_dev
->flags
& F_UDPDST_RND
) {
2226 pkt_dev
->cur_udp_dst
= random32() %
2227 (pkt_dev
->udp_dst_max
- pkt_dev
->udp_dst_min
)
2228 + pkt_dev
->udp_dst_min
;
2230 pkt_dev
->cur_udp_dst
++;
2231 if (pkt_dev
->cur_udp_dst
>= pkt_dev
->udp_dst_max
)
2232 pkt_dev
->cur_udp_dst
= pkt_dev
->udp_dst_min
;
2236 if (!(pkt_dev
->flags
& F_IPV6
)) {
2238 if ((imn
= ntohl(pkt_dev
->saddr_min
)) < (imx
=
2242 if (pkt_dev
->flags
& F_IPSRC_RND
)
2243 t
= random32() % (imx
- imn
) + imn
;
2245 t
= ntohl(pkt_dev
->cur_saddr
);
2251 pkt_dev
->cur_saddr
= htonl(t
);
2254 if (pkt_dev
->cflows
&& f_seen(pkt_dev
, flow
)) {
2255 pkt_dev
->cur_daddr
= pkt_dev
->flows
[flow
].cur_daddr
;
2257 imn
= ntohl(pkt_dev
->daddr_min
);
2258 imx
= ntohl(pkt_dev
->daddr_max
);
2262 if (pkt_dev
->flags
& F_IPDST_RND
) {
2264 t
= random32() % (imx
- imn
) + imn
;
2267 while (ipv4_is_loopback(s
) ||
2268 ipv4_is_multicast(s
) ||
2269 ipv4_is_lbcast(s
) ||
2270 ipv4_is_zeronet(s
) ||
2271 ipv4_is_local_multicast(s
)) {
2272 t
= random32() % (imx
- imn
) + imn
;
2275 pkt_dev
->cur_daddr
= s
;
2277 t
= ntohl(pkt_dev
->cur_daddr
);
2282 pkt_dev
->cur_daddr
= htonl(t
);
2285 if (pkt_dev
->cflows
) {
2286 pkt_dev
->flows
[flow
].flags
|= F_INIT
;
2287 pkt_dev
->flows
[flow
].cur_daddr
=
2290 if (pkt_dev
->flags
& F_IPSEC_ON
)
2291 get_ipsec_sa(pkt_dev
, flow
);
2296 } else { /* IPV6 * */
2298 if (pkt_dev
->min_in6_daddr
.s6_addr32
[0] == 0 &&
2299 pkt_dev
->min_in6_daddr
.s6_addr32
[1] == 0 &&
2300 pkt_dev
->min_in6_daddr
.s6_addr32
[2] == 0 &&
2301 pkt_dev
->min_in6_daddr
.s6_addr32
[3] == 0) ;
2305 /* Only random destinations yet */
2307 for (i
= 0; i
< 4; i
++) {
2308 pkt_dev
->cur_in6_daddr
.s6_addr32
[i
] =
2309 (((__force __be32
)random32() |
2310 pkt_dev
->min_in6_daddr
.s6_addr32
[i
]) &
2311 pkt_dev
->max_in6_daddr
.s6_addr32
[i
]);
2316 if (pkt_dev
->min_pkt_size
< pkt_dev
->max_pkt_size
) {
2318 if (pkt_dev
->flags
& F_TXSIZE_RND
) {
2320 (pkt_dev
->max_pkt_size
- pkt_dev
->min_pkt_size
)
2321 + pkt_dev
->min_pkt_size
;
2323 t
= pkt_dev
->cur_pkt_size
+ 1;
2324 if (t
> pkt_dev
->max_pkt_size
)
2325 t
= pkt_dev
->min_pkt_size
;
2327 pkt_dev
->cur_pkt_size
= t
;
2330 if (pkt_dev
->queue_map_min
< pkt_dev
->queue_map_max
) {
2332 if (pkt_dev
->flags
& F_QUEUE_MAP_RND
) {
2334 (pkt_dev
->queue_map_max
- pkt_dev
->queue_map_min
+ 1)
2335 + pkt_dev
->queue_map_min
;
2337 t
= pkt_dev
->cur_queue_map
+ 1;
2338 if (t
> pkt_dev
->queue_map_max
)
2339 t
= pkt_dev
->queue_map_min
;
2341 pkt_dev
->cur_queue_map
= t
;
2344 pkt_dev
->flows
[flow
].count
++;
2349 static int pktgen_output_ipsec(struct sk_buff
*skb
, struct pktgen_dev
*pkt_dev
)
2351 struct xfrm_state
*x
= pkt_dev
->flows
[pkt_dev
->curfl
].x
;
2357 /* XXX: we dont support tunnel mode for now until
2358 * we resolve the dst issue */
2359 if (x
->props
.mode
!= XFRM_MODE_TRANSPORT
)
2362 spin_lock(&x
->lock
);
2365 err
= x
->outer_mode
->output(x
, skb
);
2368 err
= x
->type
->output(x
, skb
);
2372 x
->curlft
.bytes
+=skb
->len
;
2373 x
->curlft
.packets
++;
2375 spin_unlock(&x
->lock
);
2379 static inline void free_SAs(struct pktgen_dev
*pkt_dev
)
2381 if (pkt_dev
->cflows
) {
2382 /* let go of the SAs if we have them */
2384 for (; i
< pkt_dev
->nflows
; i
++){
2385 struct xfrm_state
*x
= pkt_dev
->flows
[i
].x
;
2388 pkt_dev
->flows
[i
].x
= NULL
;
2394 static inline int process_ipsec(struct pktgen_dev
*pkt_dev
,
2395 struct sk_buff
*skb
, __be16 protocol
)
2397 if (pkt_dev
->flags
& F_IPSEC_ON
) {
2398 struct xfrm_state
*x
= pkt_dev
->flows
[pkt_dev
->curfl
].x
;
2403 nhead
= x
->props
.header_len
- skb_headroom(skb
);
2405 ret
= pskb_expand_head(skb
, nhead
, 0, GFP_ATOMIC
);
2407 printk(KERN_ERR
"Error expanding "
2408 "ipsec packet %d\n",ret
);
2413 /* ipsec is not expecting ll header */
2414 skb_pull(skb
, ETH_HLEN
);
2415 ret
= pktgen_output_ipsec(skb
, pkt_dev
);
2417 printk(KERN_ERR
"Error creating ipsec "
2423 eth
= (__u8
*) skb_push(skb
, ETH_HLEN
);
2424 memcpy(eth
, pkt_dev
->hh
, 12);
2425 *(u16
*) & eth
[12] = protocol
;
2432 static void mpls_push(__be32
*mpls
, struct pktgen_dev
*pkt_dev
)
2435 for (i
= 0; i
< pkt_dev
->nr_labels
; i
++) {
2436 *mpls
++ = pkt_dev
->labels
[i
] & ~MPLS_STACK_BOTTOM
;
2439 *mpls
|= MPLS_STACK_BOTTOM
;
2442 static inline __be16
build_tci(unsigned int id
, unsigned int cfi
,
2445 return htons(id
| (cfi
<< 12) | (prio
<< 13));
2448 static struct sk_buff
*fill_packet_ipv4(struct net_device
*odev
,
2449 struct pktgen_dev
*pkt_dev
)
2451 struct sk_buff
*skb
= NULL
;
2453 struct udphdr
*udph
;
2456 struct pktgen_hdr
*pgh
= NULL
;
2457 __be16 protocol
= htons(ETH_P_IP
);
2459 __be16
*vlan_tci
= NULL
; /* Encapsulates priority and VLAN ID */
2460 __be16
*vlan_encapsulated_proto
= NULL
; /* packet type ID field (or len) for VLAN tag */
2461 __be16
*svlan_tci
= NULL
; /* Encapsulates priority and SVLAN ID */
2462 __be16
*svlan_encapsulated_proto
= NULL
; /* packet type ID field (or len) for SVLAN tag */
2465 if (pkt_dev
->nr_labels
)
2466 protocol
= htons(ETH_P_MPLS_UC
);
2468 if (pkt_dev
->vlan_id
!= 0xffff)
2469 protocol
= htons(ETH_P_8021Q
);
2471 /* Update any of the values, used when we're incrementing various
2474 mod_cur_headers(pkt_dev
);
2476 datalen
= (odev
->hard_header_len
+ 16) & ~0xf;
2477 skb
= alloc_skb(pkt_dev
->cur_pkt_size
+ 64 + datalen
+
2478 pkt_dev
->pkt_overhead
, GFP_ATOMIC
);
2480 sprintf(pkt_dev
->result
, "No memory");
2484 skb_reserve(skb
, datalen
);
2486 /* Reserve for ethernet and IP header */
2487 eth
= (__u8
*) skb_push(skb
, 14);
2488 mpls
= (__be32
*)skb_put(skb
, pkt_dev
->nr_labels
*sizeof(__u32
));
2489 if (pkt_dev
->nr_labels
)
2490 mpls_push(mpls
, pkt_dev
);
2492 if (pkt_dev
->vlan_id
!= 0xffff) {
2493 if (pkt_dev
->svlan_id
!= 0xffff) {
2494 svlan_tci
= (__be16
*)skb_put(skb
, sizeof(__be16
));
2495 *svlan_tci
= build_tci(pkt_dev
->svlan_id
,
2498 svlan_encapsulated_proto
= (__be16
*)skb_put(skb
, sizeof(__be16
));
2499 *svlan_encapsulated_proto
= htons(ETH_P_8021Q
);
2501 vlan_tci
= (__be16
*)skb_put(skb
, sizeof(__be16
));
2502 *vlan_tci
= build_tci(pkt_dev
->vlan_id
,
2505 vlan_encapsulated_proto
= (__be16
*)skb_put(skb
, sizeof(__be16
));
2506 *vlan_encapsulated_proto
= htons(ETH_P_IP
);
2509 skb
->network_header
= skb
->tail
;
2510 skb
->transport_header
= skb
->network_header
+ sizeof(struct iphdr
);
2511 skb_put(skb
, sizeof(struct iphdr
) + sizeof(struct udphdr
));
2512 skb_set_queue_mapping(skb
, pkt_dev
->cur_queue_map
);
2514 udph
= udp_hdr(skb
);
2516 memcpy(eth
, pkt_dev
->hh
, 12);
2517 *(__be16
*) & eth
[12] = protocol
;
2519 /* Eth + IPh + UDPh + mpls */
2520 datalen
= pkt_dev
->cur_pkt_size
- 14 - 20 - 8 -
2521 pkt_dev
->pkt_overhead
;
2522 if (datalen
< sizeof(struct pktgen_hdr
))
2523 datalen
= sizeof(struct pktgen_hdr
);
2525 udph
->source
= htons(pkt_dev
->cur_udp_src
);
2526 udph
->dest
= htons(pkt_dev
->cur_udp_dst
);
2527 udph
->len
= htons(datalen
+ 8); /* DATA + udphdr */
2528 udph
->check
= 0; /* No checksum */
2533 iph
->tos
= pkt_dev
->tos
;
2534 iph
->protocol
= IPPROTO_UDP
; /* UDP */
2535 iph
->saddr
= pkt_dev
->cur_saddr
;
2536 iph
->daddr
= pkt_dev
->cur_daddr
;
2538 iplen
= 20 + 8 + datalen
;
2539 iph
->tot_len
= htons(iplen
);
2541 iph
->check
= ip_fast_csum((void *)iph
, iph
->ihl
);
2542 skb
->protocol
= protocol
;
2543 skb
->mac_header
= (skb
->network_header
- ETH_HLEN
-
2544 pkt_dev
->pkt_overhead
);
2546 skb
->pkt_type
= PACKET_HOST
;
2548 if (pkt_dev
->nfrags
<= 0)
2549 pgh
= (struct pktgen_hdr
*)skb_put(skb
, datalen
);
2551 int frags
= pkt_dev
->nfrags
;
2554 pgh
= (struct pktgen_hdr
*)(((char *)(udph
)) + 8);
2556 if (frags
> MAX_SKB_FRAGS
)
2557 frags
= MAX_SKB_FRAGS
;
2558 if (datalen
> frags
* PAGE_SIZE
) {
2559 skb_put(skb
, datalen
- frags
* PAGE_SIZE
);
2560 datalen
= frags
* PAGE_SIZE
;
2564 while (datalen
> 0) {
2565 struct page
*page
= alloc_pages(GFP_KERNEL
, 0);
2566 skb_shinfo(skb
)->frags
[i
].page
= page
;
2567 skb_shinfo(skb
)->frags
[i
].page_offset
= 0;
2568 skb_shinfo(skb
)->frags
[i
].size
=
2569 (datalen
< PAGE_SIZE
? datalen
: PAGE_SIZE
);
2570 datalen
-= skb_shinfo(skb
)->frags
[i
].size
;
2571 skb
->len
+= skb_shinfo(skb
)->frags
[i
].size
;
2572 skb
->data_len
+= skb_shinfo(skb
)->frags
[i
].size
;
2574 skb_shinfo(skb
)->nr_frags
= i
;
2583 rem
= skb_shinfo(skb
)->frags
[i
- 1].size
/ 2;
2587 skb_shinfo(skb
)->frags
[i
- 1].size
-= rem
;
2589 skb_shinfo(skb
)->frags
[i
] =
2590 skb_shinfo(skb
)->frags
[i
- 1];
2591 get_page(skb_shinfo(skb
)->frags
[i
].page
);
2592 skb_shinfo(skb
)->frags
[i
].page
=
2593 skb_shinfo(skb
)->frags
[i
- 1].page
;
2594 skb_shinfo(skb
)->frags
[i
].page_offset
+=
2595 skb_shinfo(skb
)->frags
[i
- 1].size
;
2596 skb_shinfo(skb
)->frags
[i
].size
= rem
;
2598 skb_shinfo(skb
)->nr_frags
= i
;
2602 /* Stamp the time, and sequence number, convert them to network byte order */
2605 struct timeval timestamp
;
2607 pgh
->pgh_magic
= htonl(PKTGEN_MAGIC
);
2608 pgh
->seq_num
= htonl(pkt_dev
->seq_num
);
2610 do_gettimeofday(×tamp
);
2611 pgh
->tv_sec
= htonl(timestamp
.tv_sec
);
2612 pgh
->tv_usec
= htonl(timestamp
.tv_usec
);
2616 if (!process_ipsec(pkt_dev
, skb
, protocol
))
2624 * scan_ip6, fmt_ip taken from dietlibc-0.21
2625 * Author Felix von Leitner <felix-dietlibc@fefe.de>
2627 * Slightly modified for kernel.
2628 * Should be candidate for net/ipv4/utils.c
2632 static unsigned int scan_ip6(const char *s
, char ip
[16])
2635 unsigned int len
= 0;
2638 unsigned int prefixlen
= 0;
2639 unsigned int suffixlen
= 0;
2643 for (i
= 0; i
< 16; i
++)
2649 if (s
[1] == ':') { /* Found "::", skip to part 2 */
2657 u
= simple_strtoul(s
, &pos
, 16);
2661 if (prefixlen
== 12 && s
[i
] == '.') {
2663 /* the last 4 bytes may be written as IPv4 address */
2666 memcpy((struct in_addr
*)(ip
+ 12), &tmp
, sizeof(tmp
));
2669 ip
[prefixlen
++] = (u
>> 8);
2670 ip
[prefixlen
++] = (u
& 255);
2673 if (prefixlen
== 16)
2677 /* part 2, after "::" */
2684 } else if (suffixlen
!= 0)
2687 u
= simple_strtol(s
, &pos
, 16);
2694 if (suffixlen
+ prefixlen
<= 12 && s
[i
] == '.') {
2696 memcpy((struct in_addr
*)(suffix
+ suffixlen
), &tmp
,
2702 suffix
[suffixlen
++] = (u
>> 8);
2703 suffix
[suffixlen
++] = (u
& 255);
2706 if (prefixlen
+ suffixlen
== 16)
2709 for (i
= 0; i
< suffixlen
; i
++)
2710 ip
[16 - suffixlen
+ i
] = suffix
[i
];
2714 static char tohex(char hexdigit
)
2716 return hexdigit
> 9 ? hexdigit
+ 'a' - 10 : hexdigit
+ '0';
2719 static int fmt_xlong(char *s
, unsigned int i
)
2722 *s
= tohex((i
>> 12) & 0xf);
2723 if (s
!= bak
|| *s
!= '0')
2725 *s
= tohex((i
>> 8) & 0xf);
2726 if (s
!= bak
|| *s
!= '0')
2728 *s
= tohex((i
>> 4) & 0xf);
2729 if (s
!= bak
|| *s
!= '0')
2731 *s
= tohex(i
& 0xf);
2735 static unsigned int fmt_ip6(char *s
, const char ip
[16])
2740 unsigned int compressing
;
2745 for (j
= 0; j
< 16; j
+= 2) {
2747 #ifdef V4MAPPEDPREFIX
2748 if (j
== 12 && !memcmp(ip
, V4mappedprefix
, 12)) {
2749 inet_ntoa_r(*(struct in_addr
*)(ip
+ 12), s
);
2754 temp
= ((unsigned long)(unsigned char)ip
[j
] << 8) +
2755 (unsigned long)(unsigned char)ip
[j
+ 1];
2770 i
= fmt_xlong(s
, temp
);
2787 static struct sk_buff
*fill_packet_ipv6(struct net_device
*odev
,
2788 struct pktgen_dev
*pkt_dev
)
2790 struct sk_buff
*skb
= NULL
;
2792 struct udphdr
*udph
;
2794 struct ipv6hdr
*iph
;
2795 struct pktgen_hdr
*pgh
= NULL
;
2796 __be16 protocol
= htons(ETH_P_IPV6
);
2798 __be16
*vlan_tci
= NULL
; /* Encapsulates priority and VLAN ID */
2799 __be16
*vlan_encapsulated_proto
= NULL
; /* packet type ID field (or len) for VLAN tag */
2800 __be16
*svlan_tci
= NULL
; /* Encapsulates priority and SVLAN ID */
2801 __be16
*svlan_encapsulated_proto
= NULL
; /* packet type ID field (or len) for SVLAN tag */
2803 if (pkt_dev
->nr_labels
)
2804 protocol
= htons(ETH_P_MPLS_UC
);
2806 if (pkt_dev
->vlan_id
!= 0xffff)
2807 protocol
= htons(ETH_P_8021Q
);
2809 /* Update any of the values, used when we're incrementing various
2812 mod_cur_headers(pkt_dev
);
2814 skb
= alloc_skb(pkt_dev
->cur_pkt_size
+ 64 + 16 +
2815 pkt_dev
->pkt_overhead
, GFP_ATOMIC
);
2817 sprintf(pkt_dev
->result
, "No memory");
2821 skb_reserve(skb
, 16);
2823 /* Reserve for ethernet and IP header */
2824 eth
= (__u8
*) skb_push(skb
, 14);
2825 mpls
= (__be32
*)skb_put(skb
, pkt_dev
->nr_labels
*sizeof(__u32
));
2826 if (pkt_dev
->nr_labels
)
2827 mpls_push(mpls
, pkt_dev
);
2829 if (pkt_dev
->vlan_id
!= 0xffff) {
2830 if (pkt_dev
->svlan_id
!= 0xffff) {
2831 svlan_tci
= (__be16
*)skb_put(skb
, sizeof(__be16
));
2832 *svlan_tci
= build_tci(pkt_dev
->svlan_id
,
2835 svlan_encapsulated_proto
= (__be16
*)skb_put(skb
, sizeof(__be16
));
2836 *svlan_encapsulated_proto
= htons(ETH_P_8021Q
);
2838 vlan_tci
= (__be16
*)skb_put(skb
, sizeof(__be16
));
2839 *vlan_tci
= build_tci(pkt_dev
->vlan_id
,
2842 vlan_encapsulated_proto
= (__be16
*)skb_put(skb
, sizeof(__be16
));
2843 *vlan_encapsulated_proto
= htons(ETH_P_IPV6
);
2846 skb
->network_header
= skb
->tail
;
2847 skb
->transport_header
= skb
->network_header
+ sizeof(struct ipv6hdr
);
2848 skb_put(skb
, sizeof(struct ipv6hdr
) + sizeof(struct udphdr
));
2849 skb_set_queue_mapping(skb
, pkt_dev
->cur_queue_map
);
2850 iph
= ipv6_hdr(skb
);
2851 udph
= udp_hdr(skb
);
2853 memcpy(eth
, pkt_dev
->hh
, 12);
2854 *(__be16
*) & eth
[12] = protocol
;
2856 /* Eth + IPh + UDPh + mpls */
2857 datalen
= pkt_dev
->cur_pkt_size
- 14 -
2858 sizeof(struct ipv6hdr
) - sizeof(struct udphdr
) -
2859 pkt_dev
->pkt_overhead
;
2861 if (datalen
< sizeof(struct pktgen_hdr
)) {
2862 datalen
= sizeof(struct pktgen_hdr
);
2863 if (net_ratelimit())
2864 printk(KERN_INFO
"pktgen: increased datalen to %d\n",
2868 udph
->source
= htons(pkt_dev
->cur_udp_src
);
2869 udph
->dest
= htons(pkt_dev
->cur_udp_dst
);
2870 udph
->len
= htons(datalen
+ sizeof(struct udphdr
));
2871 udph
->check
= 0; /* No checksum */
2873 *(__be32
*) iph
= htonl(0x60000000); /* Version + flow */
2875 if (pkt_dev
->traffic_class
) {
2876 /* Version + traffic class + flow (0) */
2877 *(__be32
*)iph
|= htonl(0x60000000 | (pkt_dev
->traffic_class
<< 20));
2880 iph
->hop_limit
= 32;
2882 iph
->payload_len
= htons(sizeof(struct udphdr
) + datalen
);
2883 iph
->nexthdr
= IPPROTO_UDP
;
2885 ipv6_addr_copy(&iph
->daddr
, &pkt_dev
->cur_in6_daddr
);
2886 ipv6_addr_copy(&iph
->saddr
, &pkt_dev
->cur_in6_saddr
);
2888 skb
->mac_header
= (skb
->network_header
- ETH_HLEN
-
2889 pkt_dev
->pkt_overhead
);
2890 skb
->protocol
= protocol
;
2892 skb
->pkt_type
= PACKET_HOST
;
2894 if (pkt_dev
->nfrags
<= 0)
2895 pgh
= (struct pktgen_hdr
*)skb_put(skb
, datalen
);
2897 int frags
= pkt_dev
->nfrags
;
2900 pgh
= (struct pktgen_hdr
*)(((char *)(udph
)) + 8);
2902 if (frags
> MAX_SKB_FRAGS
)
2903 frags
= MAX_SKB_FRAGS
;
2904 if (datalen
> frags
* PAGE_SIZE
) {
2905 skb_put(skb
, datalen
- frags
* PAGE_SIZE
);
2906 datalen
= frags
* PAGE_SIZE
;
2910 while (datalen
> 0) {
2911 struct page
*page
= alloc_pages(GFP_KERNEL
, 0);
2912 skb_shinfo(skb
)->frags
[i
].page
= page
;
2913 skb_shinfo(skb
)->frags
[i
].page_offset
= 0;
2914 skb_shinfo(skb
)->frags
[i
].size
=
2915 (datalen
< PAGE_SIZE
? datalen
: PAGE_SIZE
);
2916 datalen
-= skb_shinfo(skb
)->frags
[i
].size
;
2917 skb
->len
+= skb_shinfo(skb
)->frags
[i
].size
;
2918 skb
->data_len
+= skb_shinfo(skb
)->frags
[i
].size
;
2920 skb_shinfo(skb
)->nr_frags
= i
;
2929 rem
= skb_shinfo(skb
)->frags
[i
- 1].size
/ 2;
2933 skb_shinfo(skb
)->frags
[i
- 1].size
-= rem
;
2935 skb_shinfo(skb
)->frags
[i
] =
2936 skb_shinfo(skb
)->frags
[i
- 1];
2937 get_page(skb_shinfo(skb
)->frags
[i
].page
);
2938 skb_shinfo(skb
)->frags
[i
].page
=
2939 skb_shinfo(skb
)->frags
[i
- 1].page
;
2940 skb_shinfo(skb
)->frags
[i
].page_offset
+=
2941 skb_shinfo(skb
)->frags
[i
- 1].size
;
2942 skb_shinfo(skb
)->frags
[i
].size
= rem
;
2944 skb_shinfo(skb
)->nr_frags
= i
;
2948 /* Stamp the time, and sequence number, convert them to network byte order */
2949 /* should we update cloned packets too ? */
2951 struct timeval timestamp
;
2953 pgh
->pgh_magic
= htonl(PKTGEN_MAGIC
);
2954 pgh
->seq_num
= htonl(pkt_dev
->seq_num
);
2956 do_gettimeofday(×tamp
);
2957 pgh
->tv_sec
= htonl(timestamp
.tv_sec
);
2958 pgh
->tv_usec
= htonl(timestamp
.tv_usec
);
2960 /* pkt_dev->seq_num++; FF: you really mean this? */
2965 static inline struct sk_buff
*fill_packet(struct net_device
*odev
,
2966 struct pktgen_dev
*pkt_dev
)
2968 if (pkt_dev
->flags
& F_IPV6
)
2969 return fill_packet_ipv6(odev
, pkt_dev
);
2971 return fill_packet_ipv4(odev
, pkt_dev
);
2974 static void pktgen_clear_counters(struct pktgen_dev
*pkt_dev
)
2976 pkt_dev
->seq_num
= 1;
2977 pkt_dev
->idle_acc
= 0;
2979 pkt_dev
->tx_bytes
= 0;
2980 pkt_dev
->errors
= 0;
2983 /* Set up structure for sending pkts, clear counters */
2985 static void pktgen_run(struct pktgen_thread
*t
)
2987 struct pktgen_dev
*pkt_dev
;
2990 pr_debug("pktgen: entering pktgen_run. %p\n", t
);
2993 list_for_each_entry(pkt_dev
, &t
->if_list
, list
) {
2996 * setup odev and create initial packet.
2998 pktgen_setup_inject(pkt_dev
);
3000 if (pkt_dev
->odev
) {
3001 pktgen_clear_counters(pkt_dev
);
3002 pkt_dev
->running
= 1; /* Cranke yeself! */
3003 pkt_dev
->skb
= NULL
;
3004 pkt_dev
->started_at
= getCurUs();
3005 pkt_dev
->next_tx_us
= getCurUs(); /* Transmit immediately */
3006 pkt_dev
->next_tx_ns
= 0;
3007 set_pkt_overhead(pkt_dev
);
3009 strcpy(pkt_dev
->result
, "Starting");
3012 strcpy(pkt_dev
->result
, "Error starting");
3016 t
->control
&= ~(T_STOP
);
3019 static void pktgen_stop_all_threads_ifs(void)
3021 struct pktgen_thread
*t
;
3023 pr_debug("pktgen: entering pktgen_stop_all_threads_ifs.\n");
3025 mutex_lock(&pktgen_thread_lock
);
3027 list_for_each_entry(t
, &pktgen_threads
, th_list
)
3028 t
->control
|= T_STOP
;
3030 mutex_unlock(&pktgen_thread_lock
);
3033 static int thread_is_running(struct pktgen_thread
*t
)
3035 struct pktgen_dev
*pkt_dev
;
3038 list_for_each_entry(pkt_dev
, &t
->if_list
, list
)
3039 if (pkt_dev
->running
) {
3046 static int pktgen_wait_thread_run(struct pktgen_thread
*t
)
3050 while (thread_is_running(t
)) {
3054 msleep_interruptible(100);
3056 if (signal_pending(current
))
3066 static int pktgen_wait_all_threads_run(void)
3068 struct pktgen_thread
*t
;
3071 mutex_lock(&pktgen_thread_lock
);
3073 list_for_each_entry(t
, &pktgen_threads
, th_list
) {
3074 sig
= pktgen_wait_thread_run(t
);
3080 list_for_each_entry(t
, &pktgen_threads
, th_list
)
3081 t
->control
|= (T_STOP
);
3083 mutex_unlock(&pktgen_thread_lock
);
3087 static void pktgen_run_all_threads(void)
3089 struct pktgen_thread
*t
;
3091 pr_debug("pktgen: entering pktgen_run_all_threads.\n");
3093 mutex_lock(&pktgen_thread_lock
);
3095 list_for_each_entry(t
, &pktgen_threads
, th_list
)
3096 t
->control
|= (T_RUN
);
3098 mutex_unlock(&pktgen_thread_lock
);
3100 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
3102 pktgen_wait_all_threads_run();
3105 static void show_results(struct pktgen_dev
*pkt_dev
, int nr_frags
)
3107 __u64 total_us
, bps
, mbps
, pps
, idle
;
3108 char *p
= pkt_dev
->result
;
3110 total_us
= pkt_dev
->stopped_at
- pkt_dev
->started_at
;
3112 idle
= pkt_dev
->idle_acc
;
3114 p
+= sprintf(p
, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
3115 (unsigned long long)total_us
,
3116 (unsigned long long)(total_us
- idle
),
3117 (unsigned long long)idle
,
3118 (unsigned long long)pkt_dev
->sofar
,
3119 pkt_dev
->cur_pkt_size
, nr_frags
);
3121 pps
= pkt_dev
->sofar
* USEC_PER_SEC
;
3123 while ((total_us
>> 32) != 0) {
3128 do_div(pps
, total_us
);
3130 bps
= pps
* 8 * pkt_dev
->cur_pkt_size
;
3133 do_div(mbps
, 1000000);
3134 p
+= sprintf(p
, " %llupps %lluMb/sec (%llubps) errors: %llu",
3135 (unsigned long long)pps
,
3136 (unsigned long long)mbps
,
3137 (unsigned long long)bps
,
3138 (unsigned long long)pkt_dev
->errors
);
3141 /* Set stopped-at timer, remove from running list, do counters & statistics */
3143 static int pktgen_stop_device(struct pktgen_dev
*pkt_dev
)
3145 int nr_frags
= pkt_dev
->skb
? skb_shinfo(pkt_dev
->skb
)->nr_frags
: -1;
3147 if (!pkt_dev
->running
) {
3148 printk(KERN_WARNING
"pktgen: interface: %s is already "
3149 "stopped\n", pkt_dev
->odev
->name
);
3153 pkt_dev
->stopped_at
= getCurUs();
3154 pkt_dev
->running
= 0;
3156 show_results(pkt_dev
, nr_frags
);
3161 static struct pktgen_dev
*next_to_run(struct pktgen_thread
*t
)
3163 struct pktgen_dev
*pkt_dev
, *best
= NULL
;
3167 list_for_each_entry(pkt_dev
, &t
->if_list
, list
) {
3168 if (!pkt_dev
->running
)
3172 else if (pkt_dev
->next_tx_us
< best
->next_tx_us
)
3179 static void pktgen_stop(struct pktgen_thread
*t
)
3181 struct pktgen_dev
*pkt_dev
;
3183 pr_debug("pktgen: entering pktgen_stop\n");
3187 list_for_each_entry(pkt_dev
, &t
->if_list
, list
) {
3188 pktgen_stop_device(pkt_dev
);
3190 kfree_skb(pkt_dev
->skb
);
3192 pkt_dev
->skb
= NULL
;
3199 * one of our devices needs to be removed - find it
3202 static void pktgen_rem_one_if(struct pktgen_thread
*t
)
3204 struct list_head
*q
, *n
;
3205 struct pktgen_dev
*cur
;
3207 pr_debug("pktgen: entering pktgen_rem_one_if\n");
3211 list_for_each_safe(q
, n
, &t
->if_list
) {
3212 cur
= list_entry(q
, struct pktgen_dev
, list
);
3214 if (!cur
->removal_mark
)
3218 kfree_skb(cur
->skb
);
3221 pktgen_remove_device(t
, cur
);
3229 static void pktgen_rem_all_ifs(struct pktgen_thread
*t
)
3231 struct list_head
*q
, *n
;
3232 struct pktgen_dev
*cur
;
3234 /* Remove all devices, free mem */
3236 pr_debug("pktgen: entering pktgen_rem_all_ifs\n");
3239 list_for_each_safe(q
, n
, &t
->if_list
) {
3240 cur
= list_entry(q
, struct pktgen_dev
, list
);
3243 kfree_skb(cur
->skb
);
3246 pktgen_remove_device(t
, cur
);
3252 static void pktgen_rem_thread(struct pktgen_thread
*t
)
3254 /* Remove from the thread list */
3256 remove_proc_entry(t
->tsk
->comm
, pg_proc_dir
);
3258 mutex_lock(&pktgen_thread_lock
);
3260 list_del(&t
->th_list
);
3262 mutex_unlock(&pktgen_thread_lock
);
3265 static __inline__
void pktgen_xmit(struct pktgen_dev
*pkt_dev
)
3267 struct net_device
*odev
= NULL
;
3268 __u64 idle_start
= 0;
3271 odev
= pkt_dev
->odev
;
3273 if (pkt_dev
->delay_us
|| pkt_dev
->delay_ns
) {
3277 if (now
< pkt_dev
->next_tx_us
)
3278 spin(pkt_dev
, pkt_dev
->next_tx_us
);
3280 /* This is max DELAY, this has special meaning of
3283 if (pkt_dev
->delay_us
== 0x7FFFFFFF) {
3284 pkt_dev
->next_tx_us
= getCurUs() + pkt_dev
->delay_us
;
3285 pkt_dev
->next_tx_ns
= pkt_dev
->delay_ns
;
3290 if ((netif_queue_stopped(odev
) ||
3292 netif_subqueue_stopped(odev
, pkt_dev
->skb
))) ||
3294 idle_start
= getCurUs();
3296 if (!netif_running(odev
)) {
3297 pktgen_stop_device(pkt_dev
);
3299 kfree_skb(pkt_dev
->skb
);
3300 pkt_dev
->skb
= NULL
;
3306 pkt_dev
->idle_acc
+= getCurUs() - idle_start
;
3308 if (netif_queue_stopped(odev
) ||
3309 netif_subqueue_stopped(odev
, pkt_dev
->skb
)) {
3310 pkt_dev
->next_tx_us
= getCurUs(); /* TODO */
3311 pkt_dev
->next_tx_ns
= 0;
3312 goto out
; /* Try the next interface */
3316 if (pkt_dev
->last_ok
|| !pkt_dev
->skb
) {
3317 if ((++pkt_dev
->clone_count
>= pkt_dev
->clone_skb
)
3318 || (!pkt_dev
->skb
)) {
3319 /* build a new pkt */
3321 kfree_skb(pkt_dev
->skb
);
3323 pkt_dev
->skb
= fill_packet(odev
, pkt_dev
);
3324 if (pkt_dev
->skb
== NULL
) {
3325 printk(KERN_ERR
"pktgen: ERROR: couldn't "
3326 "allocate skb in fill_packet.\n");
3328 pkt_dev
->clone_count
--; /* back out increment, OOM */
3331 pkt_dev
->allocated_skbs
++;
3332 pkt_dev
->clone_count
= 0; /* reset counter */
3336 netif_tx_lock_bh(odev
);
3337 if (!netif_queue_stopped(odev
) &&
3338 !netif_subqueue_stopped(odev
, pkt_dev
->skb
)) {
3340 atomic_inc(&(pkt_dev
->skb
->users
));
3342 ret
= odev
->hard_start_xmit(pkt_dev
->skb
, odev
);
3343 if (likely(ret
== NETDEV_TX_OK
)) {
3344 pkt_dev
->last_ok
= 1;
3347 pkt_dev
->tx_bytes
+= pkt_dev
->cur_pkt_size
;
3349 } else if (ret
== NETDEV_TX_LOCKED
3350 && (odev
->features
& NETIF_F_LLTX
)) {
3353 } else { /* Retry it next time */
3355 atomic_dec(&(pkt_dev
->skb
->users
));
3357 if (debug
&& net_ratelimit())
3358 printk(KERN_INFO
"pktgen: Hard xmit error\n");
3361 pkt_dev
->last_ok
= 0;
3364 pkt_dev
->next_tx_us
= getCurUs();
3365 pkt_dev
->next_tx_ns
= 0;
3367 pkt_dev
->next_tx_us
+= pkt_dev
->delay_us
;
3368 pkt_dev
->next_tx_ns
+= pkt_dev
->delay_ns
;
3370 if (pkt_dev
->next_tx_ns
> 1000) {
3371 pkt_dev
->next_tx_us
++;
3372 pkt_dev
->next_tx_ns
-= 1000;
3376 else { /* Retry it next time */
3377 pkt_dev
->last_ok
= 0;
3378 pkt_dev
->next_tx_us
= getCurUs(); /* TODO */
3379 pkt_dev
->next_tx_ns
= 0;
3382 netif_tx_unlock_bh(odev
);
3384 /* If pkt_dev->count is zero, then run forever */
3385 if ((pkt_dev
->count
!= 0) && (pkt_dev
->sofar
>= pkt_dev
->count
)) {
3386 if (atomic_read(&(pkt_dev
->skb
->users
)) != 1) {
3387 idle_start
= getCurUs();
3388 while (atomic_read(&(pkt_dev
->skb
->users
)) != 1) {
3389 if (signal_pending(current
)) {
3394 pkt_dev
->idle_acc
+= getCurUs() - idle_start
;
3397 /* Done with this */
3398 pktgen_stop_device(pkt_dev
);
3400 kfree_skb(pkt_dev
->skb
);
3401 pkt_dev
->skb
= NULL
;
3407 * Main loop of the thread goes here
3410 static int pktgen_thread_worker(void *arg
)
3413 struct pktgen_thread
*t
= arg
;
3414 struct pktgen_dev
*pkt_dev
= NULL
;
3417 BUG_ON(smp_processor_id() != cpu
);
3419 init_waitqueue_head(&t
->queue
);
3421 pr_debug("pktgen: starting pktgen/%d: pid=%d\n", cpu
, task_pid_nr(current
));
3423 set_current_state(TASK_INTERRUPTIBLE
);
3427 while (!kthread_should_stop()) {
3428 pkt_dev
= next_to_run(t
);
3431 (t
->control
& (T_STOP
| T_RUN
| T_REMDEVALL
| T_REMDEV
))
3433 prepare_to_wait(&(t
->queue
), &wait
,
3434 TASK_INTERRUPTIBLE
);
3435 schedule_timeout(HZ
/ 10);
3436 finish_wait(&(t
->queue
), &wait
);
3439 __set_current_state(TASK_RUNNING
);
3442 pktgen_xmit(pkt_dev
);
3444 if (t
->control
& T_STOP
) {
3446 t
->control
&= ~(T_STOP
);
3449 if (t
->control
& T_RUN
) {
3451 t
->control
&= ~(T_RUN
);
3454 if (t
->control
& T_REMDEVALL
) {
3455 pktgen_rem_all_ifs(t
);
3456 t
->control
&= ~(T_REMDEVALL
);
3459 if (t
->control
& T_REMDEV
) {
3460 pktgen_rem_one_if(t
);
3461 t
->control
&= ~(T_REMDEV
);
3466 set_current_state(TASK_INTERRUPTIBLE
);
3469 pr_debug("pktgen: %s stopping all device\n", t
->tsk
->comm
);
3472 pr_debug("pktgen: %s removing all device\n", t
->tsk
->comm
);
3473 pktgen_rem_all_ifs(t
);
3475 pr_debug("pktgen: %s removing thread.\n", t
->tsk
->comm
);
3476 pktgen_rem_thread(t
);
3481 static struct pktgen_dev
*pktgen_find_dev(struct pktgen_thread
*t
,
3484 struct pktgen_dev
*p
, *pkt_dev
= NULL
;
3487 list_for_each_entry(p
, &t
->if_list
, list
)
3488 if (strncmp(p
->odev
->name
, ifname
, IFNAMSIZ
) == 0) {
3494 pr_debug("pktgen: find_dev(%s) returning %p\n", ifname
, pkt_dev
);
3499 * Adds a dev at front of if_list.
3502 static int add_dev_to_thread(struct pktgen_thread
*t
,
3503 struct pktgen_dev
*pkt_dev
)
3509 if (pkt_dev
->pg_thread
) {
3510 printk(KERN_ERR
"pktgen: ERROR: already assigned "
3516 list_add(&pkt_dev
->list
, &t
->if_list
);
3517 pkt_dev
->pg_thread
= t
;
3518 pkt_dev
->running
= 0;
3525 /* Called under thread lock */
3527 static int pktgen_add_device(struct pktgen_thread
*t
, const char *ifname
)
3529 struct pktgen_dev
*pkt_dev
;
3532 /* We don't allow a device to be on several threads */
3534 pkt_dev
= __pktgen_NN_threads(ifname
, FIND
);
3536 printk(KERN_ERR
"pktgen: ERROR: interface already used.\n");
3540 pkt_dev
= kzalloc(sizeof(struct pktgen_dev
), GFP_KERNEL
);
3544 pkt_dev
->flows
= vmalloc(MAX_CFLOWS
* sizeof(struct flow_state
));
3545 if (pkt_dev
->flows
== NULL
) {
3549 memset(pkt_dev
->flows
, 0, MAX_CFLOWS
* sizeof(struct flow_state
));
3551 pkt_dev
->removal_mark
= 0;
3552 pkt_dev
->min_pkt_size
= ETH_ZLEN
;
3553 pkt_dev
->max_pkt_size
= ETH_ZLEN
;
3554 pkt_dev
->nfrags
= 0;
3555 pkt_dev
->clone_skb
= pg_clone_skb_d
;
3556 pkt_dev
->delay_us
= pg_delay_d
/ 1000;
3557 pkt_dev
->delay_ns
= pg_delay_d
% 1000;
3558 pkt_dev
->count
= pg_count_d
;
3560 pkt_dev
->udp_src_min
= 9; /* sink port */
3561 pkt_dev
->udp_src_max
= 9;
3562 pkt_dev
->udp_dst_min
= 9;
3563 pkt_dev
->udp_dst_max
= 9;
3565 pkt_dev
->vlan_p
= 0;
3566 pkt_dev
->vlan_cfi
= 0;
3567 pkt_dev
->vlan_id
= 0xffff;
3568 pkt_dev
->svlan_p
= 0;
3569 pkt_dev
->svlan_cfi
= 0;
3570 pkt_dev
->svlan_id
= 0xffff;
3572 err
= pktgen_setup_dev(pkt_dev
, ifname
);
3576 pkt_dev
->entry
= create_proc_entry(ifname
, 0600, pg_proc_dir
);
3577 if (!pkt_dev
->entry
) {
3578 printk(KERN_ERR
"pktgen: cannot create %s/%s procfs entry.\n",
3579 PG_PROC_DIR
, ifname
);
3583 pkt_dev
->entry
->proc_fops
= &pktgen_if_fops
;
3584 pkt_dev
->entry
->data
= pkt_dev
;
3586 pkt_dev
->ipsmode
= XFRM_MODE_TRANSPORT
;
3587 pkt_dev
->ipsproto
= IPPROTO_ESP
;
3590 return add_dev_to_thread(t
, pkt_dev
);
3592 dev_put(pkt_dev
->odev
);
3598 vfree(pkt_dev
->flows
);
3603 static int __init
pktgen_create_thread(int cpu
)
3605 struct pktgen_thread
*t
;
3606 struct proc_dir_entry
*pe
;
3607 struct task_struct
*p
;
3609 t
= kzalloc(sizeof(struct pktgen_thread
), GFP_KERNEL
);
3611 printk(KERN_ERR
"pktgen: ERROR: out of memory, can't "
3612 "create new thread.\n");
3616 spin_lock_init(&t
->if_lock
);
3619 INIT_LIST_HEAD(&t
->if_list
);
3621 list_add_tail(&t
->th_list
, &pktgen_threads
);
3623 p
= kthread_create(pktgen_thread_worker
, t
, "kpktgend_%d", cpu
);
3625 printk(KERN_ERR
"pktgen: kernel_thread() failed "
3626 "for cpu %d\n", t
->cpu
);
3627 list_del(&t
->th_list
);
3631 kthread_bind(p
, cpu
);
3634 pe
= create_proc_entry(t
->tsk
->comm
, 0600, pg_proc_dir
);
3636 printk(KERN_ERR
"pktgen: cannot create %s/%s procfs entry.\n",
3637 PG_PROC_DIR
, t
->tsk
->comm
);
3639 list_del(&t
->th_list
);
3644 pe
->proc_fops
= &pktgen_thread_fops
;
3653 * Removes a device from the thread if_list.
3655 static void _rem_dev_from_if_list(struct pktgen_thread
*t
,
3656 struct pktgen_dev
*pkt_dev
)
3658 struct list_head
*q
, *n
;
3659 struct pktgen_dev
*p
;
3661 list_for_each_safe(q
, n
, &t
->if_list
) {
3662 p
= list_entry(q
, struct pktgen_dev
, list
);
3668 static int pktgen_remove_device(struct pktgen_thread
*t
,
3669 struct pktgen_dev
*pkt_dev
)
3672 pr_debug("pktgen: remove_device pkt_dev=%p\n", pkt_dev
);
3674 if (pkt_dev
->running
) {
3675 printk(KERN_WARNING
"pktgen: WARNING: trying to remove a "
3676 "running interface, stopping it now.\n");
3677 pktgen_stop_device(pkt_dev
);
3680 /* Dis-associate from the interface */
3682 if (pkt_dev
->odev
) {
3683 dev_put(pkt_dev
->odev
);
3684 pkt_dev
->odev
= NULL
;
3687 /* And update the thread if_list */
3689 _rem_dev_from_if_list(t
, pkt_dev
);
3692 remove_proc_entry(pkt_dev
->entry
->name
, pg_proc_dir
);
3698 vfree(pkt_dev
->flows
);
3703 static int __init
pg_init(void)
3706 struct proc_dir_entry
*pe
;
3708 printk(KERN_INFO
"%s", version
);
3710 pg_proc_dir
= proc_mkdir(PG_PROC_DIR
, init_net
.proc_net
);
3713 pg_proc_dir
->owner
= THIS_MODULE
;
3715 pe
= create_proc_entry(PGCTRL
, 0600, pg_proc_dir
);
3717 printk(KERN_ERR
"pktgen: ERROR: cannot create %s "
3718 "procfs entry.\n", PGCTRL
);
3719 proc_net_remove(&init_net
, PG_PROC_DIR
);
3723 pe
->proc_fops
= &pktgen_fops
;
3726 /* Register us to receive netdevice events */
3727 register_netdevice_notifier(&pktgen_notifier_block
);
3729 for_each_online_cpu(cpu
) {
3732 err
= pktgen_create_thread(cpu
);
3734 printk(KERN_WARNING
"pktgen: WARNING: Cannot create "
3735 "thread for cpu %d (%d)\n", cpu
, err
);
3738 if (list_empty(&pktgen_threads
)) {
3739 printk(KERN_ERR
"pktgen: ERROR: Initialization failed for "
3741 unregister_netdevice_notifier(&pktgen_notifier_block
);
3742 remove_proc_entry(PGCTRL
, pg_proc_dir
);
3743 proc_net_remove(&init_net
, PG_PROC_DIR
);
3750 static void __exit
pg_cleanup(void)
3752 struct pktgen_thread
*t
;
3753 struct list_head
*q
, *n
;
3754 wait_queue_head_t queue
;
3755 init_waitqueue_head(&queue
);
3757 /* Stop all interfaces & threads */
3759 list_for_each_safe(q
, n
, &pktgen_threads
) {
3760 t
= list_entry(q
, struct pktgen_thread
, th_list
);
3761 kthread_stop(t
->tsk
);
3765 /* Un-register us from receiving netdevice events */
3766 unregister_netdevice_notifier(&pktgen_notifier_block
);
3768 /* Clean up proc file system */
3769 remove_proc_entry(PGCTRL
, pg_proc_dir
);
3770 proc_net_remove(&init_net
, PG_PROC_DIR
);
3773 module_init(pg_init
);
3774 module_exit(pg_cleanup
);
3776 MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se");
3777 MODULE_DESCRIPTION("Packet Generator tool");
3778 MODULE_LICENSE("GPL");
3779 module_param(pg_count_d
, int, 0);
3780 module_param(pg_delay_d
, int, 0);
3781 module_param(pg_clone_skb_d
, int, 0);
3782 module_param(debug
, int, 0);