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>
119 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
121 #include <linux/sys.h>
122 #include <linux/types.h>
123 #include <linux/module.h>
124 #include <linux/moduleparam.h>
125 #include <linux/kernel.h>
126 #include <linux/mutex.h>
127 #include <linux/sched.h>
128 #include <linux/slab.h>
129 #include <linux/vmalloc.h>
130 #include <linux/unistd.h>
131 #include <linux/string.h>
132 #include <linux/ptrace.h>
133 #include <linux/errno.h>
134 #include <linux/ioport.h>
135 #include <linux/interrupt.h>
136 #include <linux/capability.h>
137 #include <linux/hrtimer.h>
138 #include <linux/freezer.h>
139 #include <linux/delay.h>
140 #include <linux/timer.h>
141 #include <linux/list.h>
142 #include <linux/init.h>
143 #include <linux/skbuff.h>
144 #include <linux/netdevice.h>
145 #include <linux/inet.h>
146 #include <linux/inetdevice.h>
147 #include <linux/rtnetlink.h>
148 #include <linux/if_arp.h>
149 #include <linux/if_vlan.h>
150 #include <linux/in.h>
151 #include <linux/ip.h>
152 #include <linux/ipv6.h>
153 #include <linux/udp.h>
154 #include <linux/proc_fs.h>
155 #include <linux/seq_file.h>
156 #include <linux/wait.h>
157 #include <linux/etherdevice.h>
158 #include <linux/kthread.h>
159 #include <net/net_namespace.h>
160 #include <net/checksum.h>
161 #include <net/ipv6.h>
162 #include <net/addrconf.h>
164 #include <net/xfrm.h>
166 #include <asm/byteorder.h>
167 #include <linux/rcupdate.h>
168 #include <linux/bitops.h>
169 #include <linux/io.h>
170 #include <linux/timex.h>
171 #include <linux/uaccess.h>
173 #include <asm/div64.h> /* do_div */
175 #define VERSION "2.74"
176 #define IP_NAME_SZ 32
177 #define MAX_MPLS_LABELS 16 /* This is the max label stack depth */
178 #define MPLS_STACK_BOTTOM htonl(0x00000100)
180 #define func_enter() pr_debug("entering %s\n", __func__);
182 /* Device flag bits */
183 #define F_IPSRC_RND (1<<0) /* IP-Src Random */
184 #define F_IPDST_RND (1<<1) /* IP-Dst Random */
185 #define F_UDPSRC_RND (1<<2) /* UDP-Src Random */
186 #define F_UDPDST_RND (1<<3) /* UDP-Dst Random */
187 #define F_MACSRC_RND (1<<4) /* MAC-Src Random */
188 #define F_MACDST_RND (1<<5) /* MAC-Dst Random */
189 #define F_TXSIZE_RND (1<<6) /* Transmit size is random */
190 #define F_IPV6 (1<<7) /* Interface in IPV6 Mode */
191 #define F_MPLS_RND (1<<8) /* Random MPLS labels */
192 #define F_VID_RND (1<<9) /* Random VLAN ID */
193 #define F_SVID_RND (1<<10) /* Random SVLAN ID */
194 #define F_FLOW_SEQ (1<<11) /* Sequential flows */
195 #define F_IPSEC_ON (1<<12) /* ipsec on for flows */
196 #define F_QUEUE_MAP_RND (1<<13) /* queue map Random */
197 #define F_QUEUE_MAP_CPU (1<<14) /* queue map mirrors smp_processor_id() */
198 #define F_NODE (1<<15) /* Node memory alloc*/
200 /* Thread control flag bits */
201 #define T_STOP (1<<0) /* Stop run */
202 #define T_RUN (1<<1) /* Start run */
203 #define T_REMDEVALL (1<<2) /* Remove all devs */
204 #define T_REMDEV (1<<3) /* Remove one dev */
206 /* If lock -- can be removed after some work */
207 #define if_lock(t) spin_lock(&(t->if_lock));
208 #define if_unlock(t) spin_unlock(&(t->if_lock));
210 /* Used to help with determining the pkts on receive */
211 #define PKTGEN_MAGIC 0xbe9be955
212 #define PG_PROC_DIR "pktgen"
213 #define PGCTRL "pgctrl"
214 static struct proc_dir_entry
*pg_proc_dir
;
216 #define MAX_CFLOWS 65536
218 #define VLAN_TAG_SIZE(x) ((x)->vlan_id == 0xffff ? 0 : 4)
219 #define SVLAN_TAG_SIZE(x) ((x)->svlan_id == 0xffff ? 0 : 4)
225 struct xfrm_state
*x
;
231 #define F_INIT (1<<0) /* flow has been initialized */
235 * Try to keep frequent/infrequent used vars. separated.
237 struct proc_dir_entry
*entry
; /* proc file */
238 struct pktgen_thread
*pg_thread
;/* the owner */
239 struct list_head list
; /* chaining in the thread's run-queue */
241 int running
; /* if false, the test will stop */
243 /* If min != max, then we will either do a linear iteration, or
244 * we will do a random selection from within the range.
247 int removal_mark
; /* non-zero => the device is marked for
248 * removal by worker thread */
250 int min_pkt_size
; /* = ETH_ZLEN; */
251 int max_pkt_size
; /* = ETH_ZLEN; */
252 int pkt_overhead
; /* overhead for MPLS, VLANs, IPSEC etc */
255 u64 delay
; /* nano-seconds */
257 __u64 count
; /* Default No packets to send */
258 __u64 sofar
; /* How many pkts we've sent so far */
259 __u64 tx_bytes
; /* How many bytes we've transmitted */
260 __u64 errors
; /* Errors when trying to transmit, */
262 /* runtime counters relating to clone_skb */
264 __u64 allocated_skbs
;
266 int last_ok
; /* Was last skb sent?
267 * Or a failed transmit of some sort?
268 * This will keep sequence numbers in order
273 u64 idle_acc
; /* nano-seconds */
278 * Use multiple SKBs during packet gen.
279 * If this number is greater than 1, then
280 * that many copies of the same packet will be
281 * sent before a new packet is allocated.
282 * If you want to send 1024 identical packets
283 * before creating a new packet,
284 * set clone_skb to 1024.
287 char dst_min
[IP_NAME_SZ
]; /* IP, ie 1.2.3.4 */
288 char dst_max
[IP_NAME_SZ
]; /* IP, ie 1.2.3.4 */
289 char src_min
[IP_NAME_SZ
]; /* IP, ie 1.2.3.4 */
290 char src_max
[IP_NAME_SZ
]; /* IP, ie 1.2.3.4 */
292 struct in6_addr in6_saddr
;
293 struct in6_addr in6_daddr
;
294 struct in6_addr cur_in6_daddr
;
295 struct in6_addr cur_in6_saddr
;
297 struct in6_addr min_in6_daddr
;
298 struct in6_addr max_in6_daddr
;
299 struct in6_addr min_in6_saddr
;
300 struct in6_addr max_in6_saddr
;
302 /* If we're doing ranges, random or incremental, then this
303 * defines the min/max for those ranges.
305 __be32 saddr_min
; /* inclusive, source IP address */
306 __be32 saddr_max
; /* exclusive, source IP address */
307 __be32 daddr_min
; /* inclusive, dest IP address */
308 __be32 daddr_max
; /* exclusive, dest IP address */
310 __u16 udp_src_min
; /* inclusive, source UDP port */
311 __u16 udp_src_max
; /* exclusive, source UDP port */
312 __u16 udp_dst_min
; /* inclusive, dest UDP port */
313 __u16 udp_dst_max
; /* exclusive, dest UDP port */
316 __u8 tos
; /* six MSB of (former) IPv4 TOS
317 are for dscp codepoint */
318 __u8 traffic_class
; /* ditto for the (former) Traffic Class in IPv6
319 (see RFC 3260, sec. 4) */
322 unsigned nr_labels
; /* Depth of stack, 0 = no MPLS */
323 __be32 labels
[MAX_MPLS_LABELS
];
325 /* VLAN/SVLAN (802.1Q/Q-in-Q) */
328 __u16 vlan_id
; /* 0xffff means no vlan tag */
332 __u16 svlan_id
; /* 0xffff means no svlan tag */
334 __u32 src_mac_count
; /* How many MACs to iterate through */
335 __u32 dst_mac_count
; /* How many MACs to iterate through */
337 unsigned char dst_mac
[ETH_ALEN
];
338 unsigned char src_mac
[ETH_ALEN
];
340 __u32 cur_dst_mac_offset
;
341 __u32 cur_src_mac_offset
;
353 0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
355 We fill in SRC address later
356 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
360 __u16 pad
; /* pad out the hh struct to an even 16 bytes */
362 struct sk_buff
*skb
; /* skb we are to transmit next, used for when we
363 * are transmitting the same one multiple times
365 struct net_device
*odev
; /* The out-going device.
366 * Note that the device should have it's
367 * pg_info pointer pointing back to this
369 * Set when the user specifies the out-going
370 * device name (not when the inject is
371 * started as it used to do.)
374 struct flow_state
*flows
;
375 unsigned cflows
; /* Concurrent flows (config) */
376 unsigned lflow
; /* Flow length (config) */
377 unsigned nflows
; /* accumulated flows (stats) */
378 unsigned curfl
; /* current sequenced flow (state)*/
382 __u32 skb_priority
; /* skb priority field */
383 int node
; /* Memory node */
386 __u8 ipsmode
; /* IPSEC mode (config) */
387 __u8 ipsproto
; /* IPSEC type (config) */
399 static bool pktgen_exiting __read_mostly
;
401 struct pktgen_thread
{
402 spinlock_t if_lock
; /* for list of devices */
403 struct list_head if_list
; /* All device here */
404 struct list_head th_list
;
405 struct task_struct
*tsk
;
408 /* Field for thread to receive "posted" events terminate,
414 wait_queue_head_t queue
;
415 struct completion start_done
;
421 static inline ktime_t
ktime_now(void)
426 return timespec_to_ktime(ts
);
429 /* This works even if 32 bit because of careful byte order choice */
430 static inline int ktime_lt(const ktime_t cmp1
, const ktime_t cmp2
)
432 return cmp1
.tv64
< cmp2
.tv64
;
435 static const char version
[] =
436 "Packet Generator for packet performance testing. "
437 "Version: " VERSION
"\n";
439 static int pktgen_remove_device(struct pktgen_thread
*t
, struct pktgen_dev
*i
);
440 static int pktgen_add_device(struct pktgen_thread
*t
, const char *ifname
);
441 static struct pktgen_dev
*pktgen_find_dev(struct pktgen_thread
*t
,
442 const char *ifname
, bool exact
);
443 static int pktgen_device_event(struct notifier_block
*, unsigned long, void *);
444 static void pktgen_run_all_threads(void);
445 static void pktgen_reset_all_threads(void);
446 static void pktgen_stop_all_threads_ifs(void);
448 static void pktgen_stop(struct pktgen_thread
*t
);
449 static void pktgen_clear_counters(struct pktgen_dev
*pkt_dev
);
451 static unsigned int scan_ip6(const char *s
, char ip
[16]);
453 /* Module parameters, defaults. */
454 static int pg_count_d __read_mostly
= 1000;
455 static int pg_delay_d __read_mostly
;
456 static int pg_clone_skb_d __read_mostly
;
457 static int debug __read_mostly
;
459 static DEFINE_MUTEX(pktgen_thread_lock
);
460 static LIST_HEAD(pktgen_threads
);
462 static struct notifier_block pktgen_notifier_block
= {
463 .notifier_call
= pktgen_device_event
,
467 * /proc handling functions
471 static int pgctrl_show(struct seq_file
*seq
, void *v
)
473 seq_puts(seq
, version
);
477 static ssize_t
pgctrl_write(struct file
*file
, const char __user
*buf
,
478 size_t count
, loff_t
*ppos
)
483 if (!capable(CAP_NET_ADMIN
)) {
488 if (count
> sizeof(data
))
489 count
= sizeof(data
);
491 if (copy_from_user(data
, buf
, count
)) {
495 data
[count
- 1] = 0; /* Make string */
497 if (!strcmp(data
, "stop"))
498 pktgen_stop_all_threads_ifs();
500 else if (!strcmp(data
, "start"))
501 pktgen_run_all_threads();
503 else if (!strcmp(data
, "reset"))
504 pktgen_reset_all_threads();
507 pr_warning("Unknown command: %s\n", data
);
515 static int pgctrl_open(struct inode
*inode
, struct file
*file
)
517 return single_open(file
, pgctrl_show
, PDE(inode
)->data
);
520 static const struct file_operations pktgen_fops
= {
521 .owner
= THIS_MODULE
,
525 .write
= pgctrl_write
,
526 .release
= single_release
,
529 static int pktgen_if_show(struct seq_file
*seq
, void *v
)
531 const struct pktgen_dev
*pkt_dev
= seq
->private;
536 "Params: count %llu min_pkt_size: %u max_pkt_size: %u\n",
537 (unsigned long long)pkt_dev
->count
, pkt_dev
->min_pkt_size
,
538 pkt_dev
->max_pkt_size
);
541 " frags: %d delay: %llu clone_skb: %d ifname: %s\n",
542 pkt_dev
->nfrags
, (unsigned long long) pkt_dev
->delay
,
543 pkt_dev
->clone_skb
, pkt_dev
->odevname
);
545 seq_printf(seq
, " flows: %u flowlen: %u\n", pkt_dev
->cflows
,
549 " queue_map_min: %u queue_map_max: %u\n",
550 pkt_dev
->queue_map_min
,
551 pkt_dev
->queue_map_max
);
553 if (pkt_dev
->skb_priority
)
554 seq_printf(seq
, " skb_priority: %u\n",
555 pkt_dev
->skb_priority
);
557 if (pkt_dev
->flags
& F_IPV6
) {
559 " saddr: %pI6c min_saddr: %pI6c max_saddr: %pI6c\n"
560 " daddr: %pI6c min_daddr: %pI6c max_daddr: %pI6c\n",
562 &pkt_dev
->min_in6_saddr
, &pkt_dev
->max_in6_saddr
,
564 &pkt_dev
->min_in6_daddr
, &pkt_dev
->max_in6_daddr
);
567 " dst_min: %s dst_max: %s\n",
568 pkt_dev
->dst_min
, pkt_dev
->dst_max
);
570 " src_min: %s src_max: %s\n",
571 pkt_dev
->src_min
, pkt_dev
->src_max
);
574 seq_puts(seq
, " src_mac: ");
576 seq_printf(seq
, "%pM ",
577 is_zero_ether_addr(pkt_dev
->src_mac
) ?
578 pkt_dev
->odev
->dev_addr
: pkt_dev
->src_mac
);
580 seq_printf(seq
, "dst_mac: ");
581 seq_printf(seq
, "%pM\n", pkt_dev
->dst_mac
);
584 " udp_src_min: %d udp_src_max: %d"
585 " udp_dst_min: %d udp_dst_max: %d\n",
586 pkt_dev
->udp_src_min
, pkt_dev
->udp_src_max
,
587 pkt_dev
->udp_dst_min
, pkt_dev
->udp_dst_max
);
590 " src_mac_count: %d dst_mac_count: %d\n",
591 pkt_dev
->src_mac_count
, pkt_dev
->dst_mac_count
);
593 if (pkt_dev
->nr_labels
) {
595 seq_printf(seq
, " mpls: ");
596 for (i
= 0; i
< pkt_dev
->nr_labels
; i
++)
597 seq_printf(seq
, "%08x%s", ntohl(pkt_dev
->labels
[i
]),
598 i
== pkt_dev
->nr_labels
-1 ? "\n" : ", ");
601 if (pkt_dev
->vlan_id
!= 0xffff)
602 seq_printf(seq
, " vlan_id: %u vlan_p: %u vlan_cfi: %u\n",
603 pkt_dev
->vlan_id
, pkt_dev
->vlan_p
,
606 if (pkt_dev
->svlan_id
!= 0xffff)
607 seq_printf(seq
, " svlan_id: %u vlan_p: %u vlan_cfi: %u\n",
608 pkt_dev
->svlan_id
, pkt_dev
->svlan_p
,
612 seq_printf(seq
, " tos: 0x%02x\n", pkt_dev
->tos
);
614 if (pkt_dev
->traffic_class
)
615 seq_printf(seq
, " traffic_class: 0x%02x\n", pkt_dev
->traffic_class
);
617 if (pkt_dev
->node
>= 0)
618 seq_printf(seq
, " node: %d\n", pkt_dev
->node
);
620 seq_printf(seq
, " Flags: ");
622 if (pkt_dev
->flags
& F_IPV6
)
623 seq_printf(seq
, "IPV6 ");
625 if (pkt_dev
->flags
& F_IPSRC_RND
)
626 seq_printf(seq
, "IPSRC_RND ");
628 if (pkt_dev
->flags
& F_IPDST_RND
)
629 seq_printf(seq
, "IPDST_RND ");
631 if (pkt_dev
->flags
& F_TXSIZE_RND
)
632 seq_printf(seq
, "TXSIZE_RND ");
634 if (pkt_dev
->flags
& F_UDPSRC_RND
)
635 seq_printf(seq
, "UDPSRC_RND ");
637 if (pkt_dev
->flags
& F_UDPDST_RND
)
638 seq_printf(seq
, "UDPDST_RND ");
640 if (pkt_dev
->flags
& F_MPLS_RND
)
641 seq_printf(seq
, "MPLS_RND ");
643 if (pkt_dev
->flags
& F_QUEUE_MAP_RND
)
644 seq_printf(seq
, "QUEUE_MAP_RND ");
646 if (pkt_dev
->flags
& F_QUEUE_MAP_CPU
)
647 seq_printf(seq
, "QUEUE_MAP_CPU ");
649 if (pkt_dev
->cflows
) {
650 if (pkt_dev
->flags
& F_FLOW_SEQ
)
651 seq_printf(seq
, "FLOW_SEQ "); /*in sequence flows*/
653 seq_printf(seq
, "FLOW_RND ");
657 if (pkt_dev
->flags
& F_IPSEC_ON
)
658 seq_printf(seq
, "IPSEC ");
661 if (pkt_dev
->flags
& F_MACSRC_RND
)
662 seq_printf(seq
, "MACSRC_RND ");
664 if (pkt_dev
->flags
& F_MACDST_RND
)
665 seq_printf(seq
, "MACDST_RND ");
667 if (pkt_dev
->flags
& F_VID_RND
)
668 seq_printf(seq
, "VID_RND ");
670 if (pkt_dev
->flags
& F_SVID_RND
)
671 seq_printf(seq
, "SVID_RND ");
673 if (pkt_dev
->flags
& F_NODE
)
674 seq_printf(seq
, "NODE_ALLOC ");
678 /* not really stopped, more like last-running-at */
679 stopped
= pkt_dev
->running
? ktime_now() : pkt_dev
->stopped_at
;
680 idle
= pkt_dev
->idle_acc
;
681 do_div(idle
, NSEC_PER_USEC
);
684 "Current:\n pkts-sofar: %llu errors: %llu\n",
685 (unsigned long long)pkt_dev
->sofar
,
686 (unsigned long long)pkt_dev
->errors
);
689 " started: %lluus stopped: %lluus idle: %lluus\n",
690 (unsigned long long) ktime_to_us(pkt_dev
->started_at
),
691 (unsigned long long) ktime_to_us(stopped
),
692 (unsigned long long) idle
);
695 " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n",
696 pkt_dev
->seq_num
, pkt_dev
->cur_dst_mac_offset
,
697 pkt_dev
->cur_src_mac_offset
);
699 if (pkt_dev
->flags
& F_IPV6
) {
700 seq_printf(seq
, " cur_saddr: %pI6c cur_daddr: %pI6c\n",
701 &pkt_dev
->cur_in6_saddr
,
702 &pkt_dev
->cur_in6_daddr
);
704 seq_printf(seq
, " cur_saddr: 0x%x cur_daddr: 0x%x\n",
705 pkt_dev
->cur_saddr
, pkt_dev
->cur_daddr
);
707 seq_printf(seq
, " cur_udp_dst: %d cur_udp_src: %d\n",
708 pkt_dev
->cur_udp_dst
, pkt_dev
->cur_udp_src
);
710 seq_printf(seq
, " cur_queue_map: %u\n", pkt_dev
->cur_queue_map
);
712 seq_printf(seq
, " flows: %u\n", pkt_dev
->nflows
);
714 if (pkt_dev
->result
[0])
715 seq_printf(seq
, "Result: %s\n", pkt_dev
->result
);
717 seq_printf(seq
, "Result: Idle\n");
723 static int hex32_arg(const char __user
*user_buffer
, unsigned long maxlen
,
729 for (; i
< maxlen
; i
++) {
733 if (get_user(c
, &user_buffer
[i
]))
735 value
= hex_to_bin(c
);
744 static int count_trail_chars(const char __user
* user_buffer
,
749 for (i
= 0; i
< maxlen
; i
++) {
751 if (get_user(c
, &user_buffer
[i
]))
769 static unsigned long num_arg(const char __user
* user_buffer
,
770 unsigned long maxlen
, unsigned long *num
)
775 for (i
= 0; i
< maxlen
; i
++) {
777 if (get_user(c
, &user_buffer
[i
]))
779 if ((c
>= '0') && (c
<= '9')) {
788 static int strn_len(const char __user
* user_buffer
, unsigned int maxlen
)
792 for (i
= 0; i
< maxlen
; i
++) {
794 if (get_user(c
, &user_buffer
[i
]))
812 static ssize_t
get_labels(const char __user
*buffer
, struct pktgen_dev
*pkt_dev
)
819 pkt_dev
->nr_labels
= 0;
822 len
= hex32_arg(&buffer
[i
], 8, &tmp
);
825 pkt_dev
->labels
[n
] = htonl(tmp
);
826 if (pkt_dev
->labels
[n
] & MPLS_STACK_BOTTOM
)
827 pkt_dev
->flags
|= F_MPLS_RND
;
829 if (get_user(c
, &buffer
[i
]))
833 if (n
>= MAX_MPLS_LABELS
)
837 pkt_dev
->nr_labels
= n
;
841 static ssize_t
pktgen_if_write(struct file
*file
,
842 const char __user
* user_buffer
, size_t count
,
845 struct seq_file
*seq
= file
->private_data
;
846 struct pktgen_dev
*pkt_dev
= seq
->private;
848 char name
[16], valstr
[32];
849 unsigned long value
= 0;
850 char *pg_result
= NULL
;
854 pg_result
= &(pkt_dev
->result
[0]);
857 pr_warning("wrong command format\n");
862 tmp
= count_trail_chars(user_buffer
, max
);
864 pr_warning("illegal format\n");
869 /* Read variable name */
871 len
= strn_len(&user_buffer
[i
], sizeof(name
) - 1);
875 memset(name
, 0, sizeof(name
));
876 if (copy_from_user(name
, &user_buffer
[i
], len
))
881 len
= count_trail_chars(&user_buffer
[i
], max
);
888 size_t copy
= min_t(size_t, count
, 1023);
890 if (copy_from_user(tb
, user_buffer
, copy
))
893 printk(KERN_DEBUG
"pktgen: %s,%lu buffer -:%s:-\n", name
,
894 (unsigned long)count
, tb
);
897 if (!strcmp(name
, "min_pkt_size")) {
898 len
= num_arg(&user_buffer
[i
], 10, &value
);
903 if (value
< 14 + 20 + 8)
905 if (value
!= pkt_dev
->min_pkt_size
) {
906 pkt_dev
->min_pkt_size
= value
;
907 pkt_dev
->cur_pkt_size
= value
;
909 sprintf(pg_result
, "OK: min_pkt_size=%u",
910 pkt_dev
->min_pkt_size
);
914 if (!strcmp(name
, "max_pkt_size")) {
915 len
= num_arg(&user_buffer
[i
], 10, &value
);
920 if (value
< 14 + 20 + 8)
922 if (value
!= pkt_dev
->max_pkt_size
) {
923 pkt_dev
->max_pkt_size
= value
;
924 pkt_dev
->cur_pkt_size
= value
;
926 sprintf(pg_result
, "OK: max_pkt_size=%u",
927 pkt_dev
->max_pkt_size
);
931 /* Shortcut for min = max */
933 if (!strcmp(name
, "pkt_size")) {
934 len
= num_arg(&user_buffer
[i
], 10, &value
);
939 if (value
< 14 + 20 + 8)
941 if (value
!= pkt_dev
->min_pkt_size
) {
942 pkt_dev
->min_pkt_size
= value
;
943 pkt_dev
->max_pkt_size
= value
;
944 pkt_dev
->cur_pkt_size
= value
;
946 sprintf(pg_result
, "OK: pkt_size=%u", pkt_dev
->min_pkt_size
);
950 if (!strcmp(name
, "debug")) {
951 len
= num_arg(&user_buffer
[i
], 10, &value
);
957 sprintf(pg_result
, "OK: debug=%u", debug
);
961 if (!strcmp(name
, "frags")) {
962 len
= num_arg(&user_buffer
[i
], 10, &value
);
967 pkt_dev
->nfrags
= value
;
968 sprintf(pg_result
, "OK: frags=%u", pkt_dev
->nfrags
);
971 if (!strcmp(name
, "delay")) {
972 len
= num_arg(&user_buffer
[i
], 10, &value
);
977 if (value
== 0x7FFFFFFF)
978 pkt_dev
->delay
= ULLONG_MAX
;
980 pkt_dev
->delay
= (u64
)value
;
982 sprintf(pg_result
, "OK: delay=%llu",
983 (unsigned long long) pkt_dev
->delay
);
986 if (!strcmp(name
, "rate")) {
987 len
= num_arg(&user_buffer
[i
], 10, &value
);
994 pkt_dev
->delay
= pkt_dev
->min_pkt_size
*8*NSEC_PER_USEC
/value
;
996 pr_info("Delay set at: %llu ns\n", pkt_dev
->delay
);
998 sprintf(pg_result
, "OK: rate=%lu", value
);
1001 if (!strcmp(name
, "ratep")) {
1002 len
= num_arg(&user_buffer
[i
], 10, &value
);
1009 pkt_dev
->delay
= NSEC_PER_SEC
/value
;
1011 pr_info("Delay set at: %llu ns\n", pkt_dev
->delay
);
1013 sprintf(pg_result
, "OK: rate=%lu", value
);
1016 if (!strcmp(name
, "udp_src_min")) {
1017 len
= num_arg(&user_buffer
[i
], 10, &value
);
1022 if (value
!= pkt_dev
->udp_src_min
) {
1023 pkt_dev
->udp_src_min
= value
;
1024 pkt_dev
->cur_udp_src
= value
;
1026 sprintf(pg_result
, "OK: udp_src_min=%u", pkt_dev
->udp_src_min
);
1029 if (!strcmp(name
, "udp_dst_min")) {
1030 len
= num_arg(&user_buffer
[i
], 10, &value
);
1035 if (value
!= pkt_dev
->udp_dst_min
) {
1036 pkt_dev
->udp_dst_min
= value
;
1037 pkt_dev
->cur_udp_dst
= value
;
1039 sprintf(pg_result
, "OK: udp_dst_min=%u", pkt_dev
->udp_dst_min
);
1042 if (!strcmp(name
, "udp_src_max")) {
1043 len
= num_arg(&user_buffer
[i
], 10, &value
);
1048 if (value
!= pkt_dev
->udp_src_max
) {
1049 pkt_dev
->udp_src_max
= value
;
1050 pkt_dev
->cur_udp_src
= value
;
1052 sprintf(pg_result
, "OK: udp_src_max=%u", pkt_dev
->udp_src_max
);
1055 if (!strcmp(name
, "udp_dst_max")) {
1056 len
= num_arg(&user_buffer
[i
], 10, &value
);
1061 if (value
!= pkt_dev
->udp_dst_max
) {
1062 pkt_dev
->udp_dst_max
= value
;
1063 pkt_dev
->cur_udp_dst
= value
;
1065 sprintf(pg_result
, "OK: udp_dst_max=%u", pkt_dev
->udp_dst_max
);
1068 if (!strcmp(name
, "clone_skb")) {
1069 len
= num_arg(&user_buffer
[i
], 10, &value
);
1074 pkt_dev
->clone_skb
= value
;
1076 sprintf(pg_result
, "OK: clone_skb=%d", pkt_dev
->clone_skb
);
1079 if (!strcmp(name
, "count")) {
1080 len
= num_arg(&user_buffer
[i
], 10, &value
);
1085 pkt_dev
->count
= value
;
1086 sprintf(pg_result
, "OK: count=%llu",
1087 (unsigned long long)pkt_dev
->count
);
1090 if (!strcmp(name
, "src_mac_count")) {
1091 len
= num_arg(&user_buffer
[i
], 10, &value
);
1096 if (pkt_dev
->src_mac_count
!= value
) {
1097 pkt_dev
->src_mac_count
= value
;
1098 pkt_dev
->cur_src_mac_offset
= 0;
1100 sprintf(pg_result
, "OK: src_mac_count=%d",
1101 pkt_dev
->src_mac_count
);
1104 if (!strcmp(name
, "dst_mac_count")) {
1105 len
= num_arg(&user_buffer
[i
], 10, &value
);
1110 if (pkt_dev
->dst_mac_count
!= value
) {
1111 pkt_dev
->dst_mac_count
= value
;
1112 pkt_dev
->cur_dst_mac_offset
= 0;
1114 sprintf(pg_result
, "OK: dst_mac_count=%d",
1115 pkt_dev
->dst_mac_count
);
1118 if (!strcmp(name
, "node")) {
1119 len
= num_arg(&user_buffer
[i
], 10, &value
);
1125 if (node_possible(value
)) {
1126 pkt_dev
->node
= value
;
1127 sprintf(pg_result
, "OK: node=%d", pkt_dev
->node
);
1128 if (pkt_dev
->page
) {
1129 put_page(pkt_dev
->page
);
1130 pkt_dev
->page
= NULL
;
1134 sprintf(pg_result
, "ERROR: node not possible");
1137 if (!strcmp(name
, "flag")) {
1140 len
= strn_len(&user_buffer
[i
], sizeof(f
) - 1);
1144 if (copy_from_user(f
, &user_buffer
[i
], len
))
1147 if (strcmp(f
, "IPSRC_RND") == 0)
1148 pkt_dev
->flags
|= F_IPSRC_RND
;
1150 else if (strcmp(f
, "!IPSRC_RND") == 0)
1151 pkt_dev
->flags
&= ~F_IPSRC_RND
;
1153 else if (strcmp(f
, "TXSIZE_RND") == 0)
1154 pkt_dev
->flags
|= F_TXSIZE_RND
;
1156 else if (strcmp(f
, "!TXSIZE_RND") == 0)
1157 pkt_dev
->flags
&= ~F_TXSIZE_RND
;
1159 else if (strcmp(f
, "IPDST_RND") == 0)
1160 pkt_dev
->flags
|= F_IPDST_RND
;
1162 else if (strcmp(f
, "!IPDST_RND") == 0)
1163 pkt_dev
->flags
&= ~F_IPDST_RND
;
1165 else if (strcmp(f
, "UDPSRC_RND") == 0)
1166 pkt_dev
->flags
|= F_UDPSRC_RND
;
1168 else if (strcmp(f
, "!UDPSRC_RND") == 0)
1169 pkt_dev
->flags
&= ~F_UDPSRC_RND
;
1171 else if (strcmp(f
, "UDPDST_RND") == 0)
1172 pkt_dev
->flags
|= F_UDPDST_RND
;
1174 else if (strcmp(f
, "!UDPDST_RND") == 0)
1175 pkt_dev
->flags
&= ~F_UDPDST_RND
;
1177 else if (strcmp(f
, "MACSRC_RND") == 0)
1178 pkt_dev
->flags
|= F_MACSRC_RND
;
1180 else if (strcmp(f
, "!MACSRC_RND") == 0)
1181 pkt_dev
->flags
&= ~F_MACSRC_RND
;
1183 else if (strcmp(f
, "MACDST_RND") == 0)
1184 pkt_dev
->flags
|= F_MACDST_RND
;
1186 else if (strcmp(f
, "!MACDST_RND") == 0)
1187 pkt_dev
->flags
&= ~F_MACDST_RND
;
1189 else if (strcmp(f
, "MPLS_RND") == 0)
1190 pkt_dev
->flags
|= F_MPLS_RND
;
1192 else if (strcmp(f
, "!MPLS_RND") == 0)
1193 pkt_dev
->flags
&= ~F_MPLS_RND
;
1195 else if (strcmp(f
, "VID_RND") == 0)
1196 pkt_dev
->flags
|= F_VID_RND
;
1198 else if (strcmp(f
, "!VID_RND") == 0)
1199 pkt_dev
->flags
&= ~F_VID_RND
;
1201 else if (strcmp(f
, "SVID_RND") == 0)
1202 pkt_dev
->flags
|= F_SVID_RND
;
1204 else if (strcmp(f
, "!SVID_RND") == 0)
1205 pkt_dev
->flags
&= ~F_SVID_RND
;
1207 else if (strcmp(f
, "FLOW_SEQ") == 0)
1208 pkt_dev
->flags
|= F_FLOW_SEQ
;
1210 else if (strcmp(f
, "QUEUE_MAP_RND") == 0)
1211 pkt_dev
->flags
|= F_QUEUE_MAP_RND
;
1213 else if (strcmp(f
, "!QUEUE_MAP_RND") == 0)
1214 pkt_dev
->flags
&= ~F_QUEUE_MAP_RND
;
1216 else if (strcmp(f
, "QUEUE_MAP_CPU") == 0)
1217 pkt_dev
->flags
|= F_QUEUE_MAP_CPU
;
1219 else if (strcmp(f
, "!QUEUE_MAP_CPU") == 0)
1220 pkt_dev
->flags
&= ~F_QUEUE_MAP_CPU
;
1222 else if (strcmp(f
, "IPSEC") == 0)
1223 pkt_dev
->flags
|= F_IPSEC_ON
;
1226 else if (strcmp(f
, "!IPV6") == 0)
1227 pkt_dev
->flags
&= ~F_IPV6
;
1229 else if (strcmp(f
, "NODE_ALLOC") == 0)
1230 pkt_dev
->flags
|= F_NODE
;
1232 else if (strcmp(f
, "!NODE_ALLOC") == 0)
1233 pkt_dev
->flags
&= ~F_NODE
;
1237 "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
1239 "IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
1240 "MACSRC_RND, MACDST_RND, TXSIZE_RND, IPV6, MPLS_RND, VID_RND, SVID_RND, FLOW_SEQ, IPSEC, NODE_ALLOC\n");
1243 sprintf(pg_result
, "OK: flags=0x%x", pkt_dev
->flags
);
1246 if (!strcmp(name
, "dst_min") || !strcmp(name
, "dst")) {
1247 len
= strn_len(&user_buffer
[i
], sizeof(pkt_dev
->dst_min
) - 1);
1251 if (copy_from_user(buf
, &user_buffer
[i
], len
))
1254 if (strcmp(buf
, pkt_dev
->dst_min
) != 0) {
1255 memset(pkt_dev
->dst_min
, 0, sizeof(pkt_dev
->dst_min
));
1256 strncpy(pkt_dev
->dst_min
, buf
, len
);
1257 pkt_dev
->daddr_min
= in_aton(pkt_dev
->dst_min
);
1258 pkt_dev
->cur_daddr
= pkt_dev
->daddr_min
;
1261 printk(KERN_DEBUG
"pktgen: dst_min set to: %s\n",
1264 sprintf(pg_result
, "OK: dst_min=%s", pkt_dev
->dst_min
);
1267 if (!strcmp(name
, "dst_max")) {
1268 len
= strn_len(&user_buffer
[i
], sizeof(pkt_dev
->dst_max
) - 1);
1273 if (copy_from_user(buf
, &user_buffer
[i
], len
))
1277 if (strcmp(buf
, pkt_dev
->dst_max
) != 0) {
1278 memset(pkt_dev
->dst_max
, 0, sizeof(pkt_dev
->dst_max
));
1279 strncpy(pkt_dev
->dst_max
, buf
, len
);
1280 pkt_dev
->daddr_max
= in_aton(pkt_dev
->dst_max
);
1281 pkt_dev
->cur_daddr
= pkt_dev
->daddr_max
;
1284 printk(KERN_DEBUG
"pktgen: dst_max set to: %s\n",
1287 sprintf(pg_result
, "OK: dst_max=%s", pkt_dev
->dst_max
);
1290 if (!strcmp(name
, "dst6")) {
1291 len
= strn_len(&user_buffer
[i
], sizeof(buf
) - 1);
1295 pkt_dev
->flags
|= F_IPV6
;
1297 if (copy_from_user(buf
, &user_buffer
[i
], len
))
1301 scan_ip6(buf
, pkt_dev
->in6_daddr
.s6_addr
);
1302 snprintf(buf
, sizeof(buf
), "%pI6c", &pkt_dev
->in6_daddr
);
1304 ipv6_addr_copy(&pkt_dev
->cur_in6_daddr
, &pkt_dev
->in6_daddr
);
1307 printk(KERN_DEBUG
"pktgen: dst6 set to: %s\n", buf
);
1310 sprintf(pg_result
, "OK: dst6=%s", buf
);
1313 if (!strcmp(name
, "dst6_min")) {
1314 len
= strn_len(&user_buffer
[i
], sizeof(buf
) - 1);
1318 pkt_dev
->flags
|= F_IPV6
;
1320 if (copy_from_user(buf
, &user_buffer
[i
], len
))
1324 scan_ip6(buf
, pkt_dev
->min_in6_daddr
.s6_addr
);
1325 snprintf(buf
, sizeof(buf
), "%pI6c", &pkt_dev
->min_in6_daddr
);
1327 ipv6_addr_copy(&pkt_dev
->cur_in6_daddr
,
1328 &pkt_dev
->min_in6_daddr
);
1330 printk(KERN_DEBUG
"pktgen: dst6_min set to: %s\n", buf
);
1333 sprintf(pg_result
, "OK: dst6_min=%s", buf
);
1336 if (!strcmp(name
, "dst6_max")) {
1337 len
= strn_len(&user_buffer
[i
], sizeof(buf
) - 1);
1341 pkt_dev
->flags
|= F_IPV6
;
1343 if (copy_from_user(buf
, &user_buffer
[i
], len
))
1347 scan_ip6(buf
, pkt_dev
->max_in6_daddr
.s6_addr
);
1348 snprintf(buf
, sizeof(buf
), "%pI6c", &pkt_dev
->max_in6_daddr
);
1351 printk(KERN_DEBUG
"pktgen: dst6_max set to: %s\n", buf
);
1354 sprintf(pg_result
, "OK: dst6_max=%s", buf
);
1357 if (!strcmp(name
, "src6")) {
1358 len
= strn_len(&user_buffer
[i
], sizeof(buf
) - 1);
1362 pkt_dev
->flags
|= F_IPV6
;
1364 if (copy_from_user(buf
, &user_buffer
[i
], len
))
1368 scan_ip6(buf
, pkt_dev
->in6_saddr
.s6_addr
);
1369 snprintf(buf
, sizeof(buf
), "%pI6c", &pkt_dev
->in6_saddr
);
1371 ipv6_addr_copy(&pkt_dev
->cur_in6_saddr
, &pkt_dev
->in6_saddr
);
1374 printk(KERN_DEBUG
"pktgen: src6 set to: %s\n", buf
);
1377 sprintf(pg_result
, "OK: src6=%s", buf
);
1380 if (!strcmp(name
, "src_min")) {
1381 len
= strn_len(&user_buffer
[i
], sizeof(pkt_dev
->src_min
) - 1);
1385 if (copy_from_user(buf
, &user_buffer
[i
], len
))
1388 if (strcmp(buf
, pkt_dev
->src_min
) != 0) {
1389 memset(pkt_dev
->src_min
, 0, sizeof(pkt_dev
->src_min
));
1390 strncpy(pkt_dev
->src_min
, buf
, len
);
1391 pkt_dev
->saddr_min
= in_aton(pkt_dev
->src_min
);
1392 pkt_dev
->cur_saddr
= pkt_dev
->saddr_min
;
1395 printk(KERN_DEBUG
"pktgen: src_min set to: %s\n",
1398 sprintf(pg_result
, "OK: src_min=%s", pkt_dev
->src_min
);
1401 if (!strcmp(name
, "src_max")) {
1402 len
= strn_len(&user_buffer
[i
], sizeof(pkt_dev
->src_max
) - 1);
1406 if (copy_from_user(buf
, &user_buffer
[i
], len
))
1409 if (strcmp(buf
, pkt_dev
->src_max
) != 0) {
1410 memset(pkt_dev
->src_max
, 0, sizeof(pkt_dev
->src_max
));
1411 strncpy(pkt_dev
->src_max
, buf
, len
);
1412 pkt_dev
->saddr_max
= in_aton(pkt_dev
->src_max
);
1413 pkt_dev
->cur_saddr
= pkt_dev
->saddr_max
;
1416 printk(KERN_DEBUG
"pktgen: src_max set to: %s\n",
1419 sprintf(pg_result
, "OK: src_max=%s", pkt_dev
->src_max
);
1422 if (!strcmp(name
, "dst_mac")) {
1423 len
= strn_len(&user_buffer
[i
], sizeof(valstr
) - 1);
1427 memset(valstr
, 0, sizeof(valstr
));
1428 if (copy_from_user(valstr
, &user_buffer
[i
], len
))
1431 if (!mac_pton(valstr
, pkt_dev
->dst_mac
))
1433 /* Set up Dest MAC */
1434 memcpy(&pkt_dev
->hh
[0], pkt_dev
->dst_mac
, ETH_ALEN
);
1436 sprintf(pg_result
, "OK: dstmac %pM", pkt_dev
->dst_mac
);
1439 if (!strcmp(name
, "src_mac")) {
1440 len
= strn_len(&user_buffer
[i
], sizeof(valstr
) - 1);
1444 memset(valstr
, 0, sizeof(valstr
));
1445 if (copy_from_user(valstr
, &user_buffer
[i
], len
))
1448 if (!mac_pton(valstr
, pkt_dev
->src_mac
))
1450 /* Set up Src MAC */
1451 memcpy(&pkt_dev
->hh
[6], pkt_dev
->src_mac
, ETH_ALEN
);
1453 sprintf(pg_result
, "OK: srcmac %pM", pkt_dev
->src_mac
);
1457 if (!strcmp(name
, "clear_counters")) {
1458 pktgen_clear_counters(pkt_dev
);
1459 sprintf(pg_result
, "OK: Clearing counters.\n");
1463 if (!strcmp(name
, "flows")) {
1464 len
= num_arg(&user_buffer
[i
], 10, &value
);
1469 if (value
> MAX_CFLOWS
)
1472 pkt_dev
->cflows
= value
;
1473 sprintf(pg_result
, "OK: flows=%u", pkt_dev
->cflows
);
1477 if (!strcmp(name
, "flowlen")) {
1478 len
= num_arg(&user_buffer
[i
], 10, &value
);
1483 pkt_dev
->lflow
= value
;
1484 sprintf(pg_result
, "OK: flowlen=%u", pkt_dev
->lflow
);
1488 if (!strcmp(name
, "queue_map_min")) {
1489 len
= num_arg(&user_buffer
[i
], 5, &value
);
1494 pkt_dev
->queue_map_min
= value
;
1495 sprintf(pg_result
, "OK: queue_map_min=%u", pkt_dev
->queue_map_min
);
1499 if (!strcmp(name
, "queue_map_max")) {
1500 len
= num_arg(&user_buffer
[i
], 5, &value
);
1505 pkt_dev
->queue_map_max
= value
;
1506 sprintf(pg_result
, "OK: queue_map_max=%u", pkt_dev
->queue_map_max
);
1510 if (!strcmp(name
, "mpls")) {
1513 len
= get_labels(&user_buffer
[i
], pkt_dev
);
1517 cnt
= sprintf(pg_result
, "OK: mpls=");
1518 for (n
= 0; n
< pkt_dev
->nr_labels
; n
++)
1519 cnt
+= sprintf(pg_result
+ cnt
,
1520 "%08x%s", ntohl(pkt_dev
->labels
[n
]),
1521 n
== pkt_dev
->nr_labels
-1 ? "" : ",");
1523 if (pkt_dev
->nr_labels
&& pkt_dev
->vlan_id
!= 0xffff) {
1524 pkt_dev
->vlan_id
= 0xffff; /* turn off VLAN/SVLAN */
1525 pkt_dev
->svlan_id
= 0xffff;
1528 printk(KERN_DEBUG
"pktgen: VLAN/SVLAN auto turned off\n");
1533 if (!strcmp(name
, "vlan_id")) {
1534 len
= num_arg(&user_buffer
[i
], 4, &value
);
1539 if (value
<= 4095) {
1540 pkt_dev
->vlan_id
= value
; /* turn on VLAN */
1543 printk(KERN_DEBUG
"pktgen: VLAN turned on\n");
1545 if (debug
&& pkt_dev
->nr_labels
)
1546 printk(KERN_DEBUG
"pktgen: MPLS auto turned off\n");
1548 pkt_dev
->nr_labels
= 0; /* turn off MPLS */
1549 sprintf(pg_result
, "OK: vlan_id=%u", pkt_dev
->vlan_id
);
1551 pkt_dev
->vlan_id
= 0xffff; /* turn off VLAN/SVLAN */
1552 pkt_dev
->svlan_id
= 0xffff;
1555 printk(KERN_DEBUG
"pktgen: VLAN/SVLAN turned off\n");
1560 if (!strcmp(name
, "vlan_p")) {
1561 len
= num_arg(&user_buffer
[i
], 1, &value
);
1566 if ((value
<= 7) && (pkt_dev
->vlan_id
!= 0xffff)) {
1567 pkt_dev
->vlan_p
= value
;
1568 sprintf(pg_result
, "OK: vlan_p=%u", pkt_dev
->vlan_p
);
1570 sprintf(pg_result
, "ERROR: vlan_p must be 0-7");
1575 if (!strcmp(name
, "vlan_cfi")) {
1576 len
= num_arg(&user_buffer
[i
], 1, &value
);
1581 if ((value
<= 1) && (pkt_dev
->vlan_id
!= 0xffff)) {
1582 pkt_dev
->vlan_cfi
= value
;
1583 sprintf(pg_result
, "OK: vlan_cfi=%u", pkt_dev
->vlan_cfi
);
1585 sprintf(pg_result
, "ERROR: vlan_cfi must be 0-1");
1590 if (!strcmp(name
, "svlan_id")) {
1591 len
= num_arg(&user_buffer
[i
], 4, &value
);
1596 if ((value
<= 4095) && ((pkt_dev
->vlan_id
!= 0xffff))) {
1597 pkt_dev
->svlan_id
= value
; /* turn on SVLAN */
1600 printk(KERN_DEBUG
"pktgen: SVLAN turned on\n");
1602 if (debug
&& pkt_dev
->nr_labels
)
1603 printk(KERN_DEBUG
"pktgen: MPLS auto turned off\n");
1605 pkt_dev
->nr_labels
= 0; /* turn off MPLS */
1606 sprintf(pg_result
, "OK: svlan_id=%u", pkt_dev
->svlan_id
);
1608 pkt_dev
->vlan_id
= 0xffff; /* turn off VLAN/SVLAN */
1609 pkt_dev
->svlan_id
= 0xffff;
1612 printk(KERN_DEBUG
"pktgen: VLAN/SVLAN turned off\n");
1617 if (!strcmp(name
, "svlan_p")) {
1618 len
= num_arg(&user_buffer
[i
], 1, &value
);
1623 if ((value
<= 7) && (pkt_dev
->svlan_id
!= 0xffff)) {
1624 pkt_dev
->svlan_p
= value
;
1625 sprintf(pg_result
, "OK: svlan_p=%u", pkt_dev
->svlan_p
);
1627 sprintf(pg_result
, "ERROR: svlan_p must be 0-7");
1632 if (!strcmp(name
, "svlan_cfi")) {
1633 len
= num_arg(&user_buffer
[i
], 1, &value
);
1638 if ((value
<= 1) && (pkt_dev
->svlan_id
!= 0xffff)) {
1639 pkt_dev
->svlan_cfi
= value
;
1640 sprintf(pg_result
, "OK: svlan_cfi=%u", pkt_dev
->svlan_cfi
);
1642 sprintf(pg_result
, "ERROR: svlan_cfi must be 0-1");
1647 if (!strcmp(name
, "tos")) {
1648 __u32 tmp_value
= 0;
1649 len
= hex32_arg(&user_buffer
[i
], 2, &tmp_value
);
1655 pkt_dev
->tos
= tmp_value
;
1656 sprintf(pg_result
, "OK: tos=0x%02x", pkt_dev
->tos
);
1658 sprintf(pg_result
, "ERROR: tos must be 00-ff");
1663 if (!strcmp(name
, "traffic_class")) {
1664 __u32 tmp_value
= 0;
1665 len
= hex32_arg(&user_buffer
[i
], 2, &tmp_value
);
1671 pkt_dev
->traffic_class
= tmp_value
;
1672 sprintf(pg_result
, "OK: traffic_class=0x%02x", pkt_dev
->traffic_class
);
1674 sprintf(pg_result
, "ERROR: traffic_class must be 00-ff");
1679 if (!strcmp(name
, "skb_priority")) {
1680 len
= num_arg(&user_buffer
[i
], 9, &value
);
1685 pkt_dev
->skb_priority
= value
;
1686 sprintf(pg_result
, "OK: skb_priority=%i",
1687 pkt_dev
->skb_priority
);
1691 sprintf(pkt_dev
->result
, "No such parameter \"%s\"", name
);
1695 static int pktgen_if_open(struct inode
*inode
, struct file
*file
)
1697 return single_open(file
, pktgen_if_show
, PDE(inode
)->data
);
1700 static const struct file_operations pktgen_if_fops
= {
1701 .owner
= THIS_MODULE
,
1702 .open
= pktgen_if_open
,
1704 .llseek
= seq_lseek
,
1705 .write
= pktgen_if_write
,
1706 .release
= single_release
,
1709 static int pktgen_thread_show(struct seq_file
*seq
, void *v
)
1711 struct pktgen_thread
*t
= seq
->private;
1712 const struct pktgen_dev
*pkt_dev
;
1716 seq_printf(seq
, "Running: ");
1719 list_for_each_entry(pkt_dev
, &t
->if_list
, list
)
1720 if (pkt_dev
->running
)
1721 seq_printf(seq
, "%s ", pkt_dev
->odevname
);
1723 seq_printf(seq
, "\nStopped: ");
1725 list_for_each_entry(pkt_dev
, &t
->if_list
, list
)
1726 if (!pkt_dev
->running
)
1727 seq_printf(seq
, "%s ", pkt_dev
->odevname
);
1730 seq_printf(seq
, "\nResult: %s\n", t
->result
);
1732 seq_printf(seq
, "\nResult: NA\n");
1739 static ssize_t
pktgen_thread_write(struct file
*file
,
1740 const char __user
* user_buffer
,
1741 size_t count
, loff_t
* offset
)
1743 struct seq_file
*seq
= file
->private_data
;
1744 struct pktgen_thread
*t
= seq
->private;
1745 int i
, max
, len
, ret
;
1750 // sprintf(pg_result, "Wrong command format");
1755 len
= count_trail_chars(user_buffer
, max
);
1761 /* Read variable name */
1763 len
= strn_len(&user_buffer
[i
], sizeof(name
) - 1);
1767 memset(name
, 0, sizeof(name
));
1768 if (copy_from_user(name
, &user_buffer
[i
], len
))
1773 len
= count_trail_chars(&user_buffer
[i
], max
);
1780 printk(KERN_DEBUG
"pktgen: t=%s, count=%lu\n",
1781 name
, (unsigned long)count
);
1784 pr_err("ERROR: No thread\n");
1789 pg_result
= &(t
->result
[0]);
1791 if (!strcmp(name
, "add_device")) {
1794 len
= strn_len(&user_buffer
[i
], sizeof(f
) - 1);
1799 if (copy_from_user(f
, &user_buffer
[i
], len
))
1802 mutex_lock(&pktgen_thread_lock
);
1803 pktgen_add_device(t
, f
);
1804 mutex_unlock(&pktgen_thread_lock
);
1806 sprintf(pg_result
, "OK: add_device=%s", f
);
1810 if (!strcmp(name
, "rem_device_all")) {
1811 mutex_lock(&pktgen_thread_lock
);
1812 t
->control
|= T_REMDEVALL
;
1813 mutex_unlock(&pktgen_thread_lock
);
1814 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
1816 sprintf(pg_result
, "OK: rem_device_all");
1820 if (!strcmp(name
, "max_before_softirq")) {
1821 sprintf(pg_result
, "OK: Note! max_before_softirq is obsoleted -- Do not use");
1831 static int pktgen_thread_open(struct inode
*inode
, struct file
*file
)
1833 return single_open(file
, pktgen_thread_show
, PDE(inode
)->data
);
1836 static const struct file_operations pktgen_thread_fops
= {
1837 .owner
= THIS_MODULE
,
1838 .open
= pktgen_thread_open
,
1840 .llseek
= seq_lseek
,
1841 .write
= pktgen_thread_write
,
1842 .release
= single_release
,
1845 /* Think find or remove for NN */
1846 static struct pktgen_dev
*__pktgen_NN_threads(const char *ifname
, int remove
)
1848 struct pktgen_thread
*t
;
1849 struct pktgen_dev
*pkt_dev
= NULL
;
1850 bool exact
= (remove
== FIND
);
1852 list_for_each_entry(t
, &pktgen_threads
, th_list
) {
1853 pkt_dev
= pktgen_find_dev(t
, ifname
, exact
);
1857 pkt_dev
->removal_mark
= 1;
1858 t
->control
|= T_REMDEV
;
1868 * mark a device for removal
1870 static void pktgen_mark_device(const char *ifname
)
1872 struct pktgen_dev
*pkt_dev
= NULL
;
1873 const int max_tries
= 10, msec_per_try
= 125;
1876 mutex_lock(&pktgen_thread_lock
);
1877 pr_debug("%s: marking %s for removal\n", __func__
, ifname
);
1881 pkt_dev
= __pktgen_NN_threads(ifname
, REMOVE
);
1882 if (pkt_dev
== NULL
)
1883 break; /* success */
1885 mutex_unlock(&pktgen_thread_lock
);
1886 pr_debug("%s: waiting for %s to disappear....\n",
1888 schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try
));
1889 mutex_lock(&pktgen_thread_lock
);
1891 if (++i
>= max_tries
) {
1892 pr_err("%s: timed out after waiting %d msec for device %s to be removed\n",
1893 __func__
, msec_per_try
* i
, ifname
);
1899 mutex_unlock(&pktgen_thread_lock
);
1902 static void pktgen_change_name(struct net_device
*dev
)
1904 struct pktgen_thread
*t
;
1906 list_for_each_entry(t
, &pktgen_threads
, th_list
) {
1907 struct pktgen_dev
*pkt_dev
;
1909 list_for_each_entry(pkt_dev
, &t
->if_list
, list
) {
1910 if (pkt_dev
->odev
!= dev
)
1913 remove_proc_entry(pkt_dev
->entry
->name
, pg_proc_dir
);
1915 pkt_dev
->entry
= proc_create_data(dev
->name
, 0600,
1919 if (!pkt_dev
->entry
)
1920 pr_err("can't move proc entry for '%s'\n",
1927 static int pktgen_device_event(struct notifier_block
*unused
,
1928 unsigned long event
, void *ptr
)
1930 struct net_device
*dev
= ptr
;
1932 if (!net_eq(dev_net(dev
), &init_net
))
1935 /* It is OK that we do not hold the group lock right now,
1936 * as we run under the RTNL lock.
1940 case NETDEV_CHANGENAME
:
1941 pktgen_change_name(dev
);
1944 case NETDEV_UNREGISTER
:
1945 pktgen_mark_device(dev
->name
);
1952 static struct net_device
*pktgen_dev_get_by_name(struct pktgen_dev
*pkt_dev
,
1958 for (i
= 0; ifname
[i
] != '@'; i
++) {
1966 return dev_get_by_name(&init_net
, b
);
1970 /* Associate pktgen_dev with a device. */
1972 static int pktgen_setup_dev(struct pktgen_dev
*pkt_dev
, const char *ifname
)
1974 struct net_device
*odev
;
1977 /* Clean old setups */
1978 if (pkt_dev
->odev
) {
1979 dev_put(pkt_dev
->odev
);
1980 pkt_dev
->odev
= NULL
;
1983 odev
= pktgen_dev_get_by_name(pkt_dev
, ifname
);
1985 pr_err("no such netdevice: \"%s\"\n", ifname
);
1989 if (odev
->type
!= ARPHRD_ETHER
) {
1990 pr_err("not an ethernet device: \"%s\"\n", ifname
);
1992 } else if (!netif_running(odev
)) {
1993 pr_err("device is down: \"%s\"\n", ifname
);
1996 pkt_dev
->odev
= odev
;
2004 /* Read pkt_dev from the interface and set up internal pktgen_dev
2005 * structure to have the right information to create/send packets
2007 static void pktgen_setup_inject(struct pktgen_dev
*pkt_dev
)
2011 if (!pkt_dev
->odev
) {
2012 pr_err("ERROR: pkt_dev->odev == NULL in setup_inject\n");
2013 sprintf(pkt_dev
->result
,
2014 "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
2018 /* make sure that we don't pick a non-existing transmit queue */
2019 ntxq
= pkt_dev
->odev
->real_num_tx_queues
;
2021 if (ntxq
<= pkt_dev
->queue_map_min
) {
2022 pr_warning("WARNING: Requested queue_map_min (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
2023 pkt_dev
->queue_map_min
, (ntxq
?: 1) - 1, ntxq
,
2025 pkt_dev
->queue_map_min
= ntxq
- 1;
2027 if (pkt_dev
->queue_map_max
>= ntxq
) {
2028 pr_warning("WARNING: Requested queue_map_max (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
2029 pkt_dev
->queue_map_max
, (ntxq
?: 1) - 1, ntxq
,
2031 pkt_dev
->queue_map_max
= ntxq
- 1;
2034 /* Default to the interface's mac if not explicitly set. */
2036 if (is_zero_ether_addr(pkt_dev
->src_mac
))
2037 memcpy(&(pkt_dev
->hh
[6]), pkt_dev
->odev
->dev_addr
, ETH_ALEN
);
2039 /* Set up Dest MAC */
2040 memcpy(&(pkt_dev
->hh
[0]), pkt_dev
->dst_mac
, ETH_ALEN
);
2042 /* Set up pkt size */
2043 pkt_dev
->cur_pkt_size
= pkt_dev
->min_pkt_size
;
2045 if (pkt_dev
->flags
& F_IPV6
) {
2047 * Skip this automatic address setting until locks or functions
2052 int i
, set
= 0, err
= 1;
2053 struct inet6_dev
*idev
;
2055 for (i
= 0; i
< IN6_ADDR_HSIZE
; i
++)
2056 if (pkt_dev
->cur_in6_saddr
.s6_addr
[i
]) {
2064 * Use linklevel address if unconfigured.
2066 * use ipv6_get_lladdr if/when it's get exported
2070 idev
= __in6_dev_get(pkt_dev
->odev
);
2072 struct inet6_ifaddr
*ifp
;
2074 read_lock_bh(&idev
->lock
);
2075 for (ifp
= idev
->addr_list
; ifp
;
2076 ifp
= ifp
->if_next
) {
2077 if (ifp
->scope
== IFA_LINK
&&
2078 !(ifp
->flags
& IFA_F_TENTATIVE
)) {
2079 ipv6_addr_copy(&pkt_dev
->
2086 read_unlock_bh(&idev
->lock
);
2090 pr_err("ERROR: IPv6 link address not available\n");
2094 pkt_dev
->saddr_min
= 0;
2095 pkt_dev
->saddr_max
= 0;
2096 if (strlen(pkt_dev
->src_min
) == 0) {
2098 struct in_device
*in_dev
;
2101 in_dev
= __in_dev_get_rcu(pkt_dev
->odev
);
2103 if (in_dev
->ifa_list
) {
2104 pkt_dev
->saddr_min
=
2105 in_dev
->ifa_list
->ifa_address
;
2106 pkt_dev
->saddr_max
= pkt_dev
->saddr_min
;
2111 pkt_dev
->saddr_min
= in_aton(pkt_dev
->src_min
);
2112 pkt_dev
->saddr_max
= in_aton(pkt_dev
->src_max
);
2115 pkt_dev
->daddr_min
= in_aton(pkt_dev
->dst_min
);
2116 pkt_dev
->daddr_max
= in_aton(pkt_dev
->dst_max
);
2118 /* Initialize current values. */
2119 pkt_dev
->cur_dst_mac_offset
= 0;
2120 pkt_dev
->cur_src_mac_offset
= 0;
2121 pkt_dev
->cur_saddr
= pkt_dev
->saddr_min
;
2122 pkt_dev
->cur_daddr
= pkt_dev
->daddr_min
;
2123 pkt_dev
->cur_udp_dst
= pkt_dev
->udp_dst_min
;
2124 pkt_dev
->cur_udp_src
= pkt_dev
->udp_src_min
;
2125 pkt_dev
->nflows
= 0;
2129 static void spin(struct pktgen_dev
*pkt_dev
, ktime_t spin_until
)
2131 ktime_t start_time
, end_time
;
2133 struct hrtimer_sleeper t
;
2135 hrtimer_init_on_stack(&t
.timer
, CLOCK_MONOTONIC
, HRTIMER_MODE_ABS
);
2136 hrtimer_set_expires(&t
.timer
, spin_until
);
2138 remaining
= ktime_to_ns(hrtimer_expires_remaining(&t
.timer
));
2139 if (remaining
<= 0) {
2140 pkt_dev
->next_tx
= ktime_add_ns(spin_until
, pkt_dev
->delay
);
2144 start_time
= ktime_now();
2145 if (remaining
< 100000)
2146 ndelay(remaining
); /* really small just spin */
2148 /* see do_nanosleep */
2149 hrtimer_init_sleeper(&t
, current
);
2151 set_current_state(TASK_INTERRUPTIBLE
);
2152 hrtimer_start_expires(&t
.timer
, HRTIMER_MODE_ABS
);
2153 if (!hrtimer_active(&t
.timer
))
2159 hrtimer_cancel(&t
.timer
);
2160 } while (t
.task
&& pkt_dev
->running
&& !signal_pending(current
));
2161 __set_current_state(TASK_RUNNING
);
2163 end_time
= ktime_now();
2165 pkt_dev
->idle_acc
+= ktime_to_ns(ktime_sub(end_time
, start_time
));
2166 pkt_dev
->next_tx
= ktime_add_ns(spin_until
, pkt_dev
->delay
);
2169 static inline void set_pkt_overhead(struct pktgen_dev
*pkt_dev
)
2171 pkt_dev
->pkt_overhead
= 0;
2172 pkt_dev
->pkt_overhead
+= pkt_dev
->nr_labels
*sizeof(u32
);
2173 pkt_dev
->pkt_overhead
+= VLAN_TAG_SIZE(pkt_dev
);
2174 pkt_dev
->pkt_overhead
+= SVLAN_TAG_SIZE(pkt_dev
);
2177 static inline int f_seen(const struct pktgen_dev
*pkt_dev
, int flow
)
2179 return !!(pkt_dev
->flows
[flow
].flags
& F_INIT
);
2182 static inline int f_pick(struct pktgen_dev
*pkt_dev
)
2184 int flow
= pkt_dev
->curfl
;
2186 if (pkt_dev
->flags
& F_FLOW_SEQ
) {
2187 if (pkt_dev
->flows
[flow
].count
>= pkt_dev
->lflow
) {
2189 pkt_dev
->flows
[flow
].count
= 0;
2190 pkt_dev
->flows
[flow
].flags
= 0;
2191 pkt_dev
->curfl
+= 1;
2192 if (pkt_dev
->curfl
>= pkt_dev
->cflows
)
2193 pkt_dev
->curfl
= 0; /*reset */
2196 flow
= random32() % pkt_dev
->cflows
;
2197 pkt_dev
->curfl
= flow
;
2199 if (pkt_dev
->flows
[flow
].count
> pkt_dev
->lflow
) {
2200 pkt_dev
->flows
[flow
].count
= 0;
2201 pkt_dev
->flows
[flow
].flags
= 0;
2205 return pkt_dev
->curfl
;
2210 /* If there was already an IPSEC SA, we keep it as is, else
2211 * we go look for it ...
2213 #define DUMMY_MARK 0
2214 static void get_ipsec_sa(struct pktgen_dev
*pkt_dev
, int flow
)
2216 struct xfrm_state
*x
= pkt_dev
->flows
[flow
].x
;
2218 /*slow path: we dont already have xfrm_state*/
2219 x
= xfrm_stateonly_find(&init_net
, DUMMY_MARK
,
2220 (xfrm_address_t
*)&pkt_dev
->cur_daddr
,
2221 (xfrm_address_t
*)&pkt_dev
->cur_saddr
,
2224 pkt_dev
->ipsproto
, 0);
2226 pkt_dev
->flows
[flow
].x
= x
;
2227 set_pkt_overhead(pkt_dev
);
2228 pkt_dev
->pkt_overhead
+= x
->props
.header_len
;
2234 static void set_cur_queue_map(struct pktgen_dev
*pkt_dev
)
2237 if (pkt_dev
->flags
& F_QUEUE_MAP_CPU
)
2238 pkt_dev
->cur_queue_map
= smp_processor_id();
2240 else if (pkt_dev
->queue_map_min
<= pkt_dev
->queue_map_max
) {
2242 if (pkt_dev
->flags
& F_QUEUE_MAP_RND
) {
2244 (pkt_dev
->queue_map_max
-
2245 pkt_dev
->queue_map_min
+ 1)
2246 + pkt_dev
->queue_map_min
;
2248 t
= pkt_dev
->cur_queue_map
+ 1;
2249 if (t
> pkt_dev
->queue_map_max
)
2250 t
= pkt_dev
->queue_map_min
;
2252 pkt_dev
->cur_queue_map
= t
;
2254 pkt_dev
->cur_queue_map
= pkt_dev
->cur_queue_map
% pkt_dev
->odev
->real_num_tx_queues
;
2257 /* Increment/randomize headers according to flags and current values
2258 * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
2260 static void mod_cur_headers(struct pktgen_dev
*pkt_dev
)
2266 if (pkt_dev
->cflows
)
2267 flow
= f_pick(pkt_dev
);
2269 /* Deal with source MAC */
2270 if (pkt_dev
->src_mac_count
> 1) {
2274 if (pkt_dev
->flags
& F_MACSRC_RND
)
2275 mc
= random32() % pkt_dev
->src_mac_count
;
2277 mc
= pkt_dev
->cur_src_mac_offset
++;
2278 if (pkt_dev
->cur_src_mac_offset
>=
2279 pkt_dev
->src_mac_count
)
2280 pkt_dev
->cur_src_mac_offset
= 0;
2283 tmp
= pkt_dev
->src_mac
[5] + (mc
& 0xFF);
2284 pkt_dev
->hh
[11] = tmp
;
2285 tmp
= (pkt_dev
->src_mac
[4] + ((mc
>> 8) & 0xFF) + (tmp
>> 8));
2286 pkt_dev
->hh
[10] = tmp
;
2287 tmp
= (pkt_dev
->src_mac
[3] + ((mc
>> 16) & 0xFF) + (tmp
>> 8));
2288 pkt_dev
->hh
[9] = tmp
;
2289 tmp
= (pkt_dev
->src_mac
[2] + ((mc
>> 24) & 0xFF) + (tmp
>> 8));
2290 pkt_dev
->hh
[8] = tmp
;
2291 tmp
= (pkt_dev
->src_mac
[1] + (tmp
>> 8));
2292 pkt_dev
->hh
[7] = tmp
;
2295 /* Deal with Destination MAC */
2296 if (pkt_dev
->dst_mac_count
> 1) {
2300 if (pkt_dev
->flags
& F_MACDST_RND
)
2301 mc
= random32() % pkt_dev
->dst_mac_count
;
2304 mc
= pkt_dev
->cur_dst_mac_offset
++;
2305 if (pkt_dev
->cur_dst_mac_offset
>=
2306 pkt_dev
->dst_mac_count
) {
2307 pkt_dev
->cur_dst_mac_offset
= 0;
2311 tmp
= pkt_dev
->dst_mac
[5] + (mc
& 0xFF);
2312 pkt_dev
->hh
[5] = tmp
;
2313 tmp
= (pkt_dev
->dst_mac
[4] + ((mc
>> 8) & 0xFF) + (tmp
>> 8));
2314 pkt_dev
->hh
[4] = tmp
;
2315 tmp
= (pkt_dev
->dst_mac
[3] + ((mc
>> 16) & 0xFF) + (tmp
>> 8));
2316 pkt_dev
->hh
[3] = tmp
;
2317 tmp
= (pkt_dev
->dst_mac
[2] + ((mc
>> 24) & 0xFF) + (tmp
>> 8));
2318 pkt_dev
->hh
[2] = tmp
;
2319 tmp
= (pkt_dev
->dst_mac
[1] + (tmp
>> 8));
2320 pkt_dev
->hh
[1] = tmp
;
2323 if (pkt_dev
->flags
& F_MPLS_RND
) {
2325 for (i
= 0; i
< pkt_dev
->nr_labels
; i
++)
2326 if (pkt_dev
->labels
[i
] & MPLS_STACK_BOTTOM
)
2327 pkt_dev
->labels
[i
] = MPLS_STACK_BOTTOM
|
2328 ((__force __be32
)random32() &
2332 if ((pkt_dev
->flags
& F_VID_RND
) && (pkt_dev
->vlan_id
!= 0xffff)) {
2333 pkt_dev
->vlan_id
= random32() & (4096-1);
2336 if ((pkt_dev
->flags
& F_SVID_RND
) && (pkt_dev
->svlan_id
!= 0xffff)) {
2337 pkt_dev
->svlan_id
= random32() & (4096 - 1);
2340 if (pkt_dev
->udp_src_min
< pkt_dev
->udp_src_max
) {
2341 if (pkt_dev
->flags
& F_UDPSRC_RND
)
2342 pkt_dev
->cur_udp_src
= random32() %
2343 (pkt_dev
->udp_src_max
- pkt_dev
->udp_src_min
)
2344 + pkt_dev
->udp_src_min
;
2347 pkt_dev
->cur_udp_src
++;
2348 if (pkt_dev
->cur_udp_src
>= pkt_dev
->udp_src_max
)
2349 pkt_dev
->cur_udp_src
= pkt_dev
->udp_src_min
;
2353 if (pkt_dev
->udp_dst_min
< pkt_dev
->udp_dst_max
) {
2354 if (pkt_dev
->flags
& F_UDPDST_RND
) {
2355 pkt_dev
->cur_udp_dst
= random32() %
2356 (pkt_dev
->udp_dst_max
- pkt_dev
->udp_dst_min
)
2357 + pkt_dev
->udp_dst_min
;
2359 pkt_dev
->cur_udp_dst
++;
2360 if (pkt_dev
->cur_udp_dst
>= pkt_dev
->udp_dst_max
)
2361 pkt_dev
->cur_udp_dst
= pkt_dev
->udp_dst_min
;
2365 if (!(pkt_dev
->flags
& F_IPV6
)) {
2367 imn
= ntohl(pkt_dev
->saddr_min
);
2368 imx
= ntohl(pkt_dev
->saddr_max
);
2371 if (pkt_dev
->flags
& F_IPSRC_RND
)
2372 t
= random32() % (imx
- imn
) + imn
;
2374 t
= ntohl(pkt_dev
->cur_saddr
);
2380 pkt_dev
->cur_saddr
= htonl(t
);
2383 if (pkt_dev
->cflows
&& f_seen(pkt_dev
, flow
)) {
2384 pkt_dev
->cur_daddr
= pkt_dev
->flows
[flow
].cur_daddr
;
2386 imn
= ntohl(pkt_dev
->daddr_min
);
2387 imx
= ntohl(pkt_dev
->daddr_max
);
2391 if (pkt_dev
->flags
& F_IPDST_RND
) {
2393 t
= random32() % (imx
- imn
) + imn
;
2396 while (ipv4_is_loopback(s
) ||
2397 ipv4_is_multicast(s
) ||
2398 ipv4_is_lbcast(s
) ||
2399 ipv4_is_zeronet(s
) ||
2400 ipv4_is_local_multicast(s
)) {
2401 t
= random32() % (imx
- imn
) + imn
;
2404 pkt_dev
->cur_daddr
= s
;
2406 t
= ntohl(pkt_dev
->cur_daddr
);
2411 pkt_dev
->cur_daddr
= htonl(t
);
2414 if (pkt_dev
->cflows
) {
2415 pkt_dev
->flows
[flow
].flags
|= F_INIT
;
2416 pkt_dev
->flows
[flow
].cur_daddr
=
2419 if (pkt_dev
->flags
& F_IPSEC_ON
)
2420 get_ipsec_sa(pkt_dev
, flow
);
2425 } else { /* IPV6 * */
2427 if (pkt_dev
->min_in6_daddr
.s6_addr32
[0] == 0 &&
2428 pkt_dev
->min_in6_daddr
.s6_addr32
[1] == 0 &&
2429 pkt_dev
->min_in6_daddr
.s6_addr32
[2] == 0 &&
2430 pkt_dev
->min_in6_daddr
.s6_addr32
[3] == 0) ;
2434 /* Only random destinations yet */
2436 for (i
= 0; i
< 4; i
++) {
2437 pkt_dev
->cur_in6_daddr
.s6_addr32
[i
] =
2438 (((__force __be32
)random32() |
2439 pkt_dev
->min_in6_daddr
.s6_addr32
[i
]) &
2440 pkt_dev
->max_in6_daddr
.s6_addr32
[i
]);
2445 if (pkt_dev
->min_pkt_size
< pkt_dev
->max_pkt_size
) {
2447 if (pkt_dev
->flags
& F_TXSIZE_RND
) {
2449 (pkt_dev
->max_pkt_size
- pkt_dev
->min_pkt_size
)
2450 + pkt_dev
->min_pkt_size
;
2452 t
= pkt_dev
->cur_pkt_size
+ 1;
2453 if (t
> pkt_dev
->max_pkt_size
)
2454 t
= pkt_dev
->min_pkt_size
;
2456 pkt_dev
->cur_pkt_size
= t
;
2459 set_cur_queue_map(pkt_dev
);
2461 pkt_dev
->flows
[flow
].count
++;
2466 static int pktgen_output_ipsec(struct sk_buff
*skb
, struct pktgen_dev
*pkt_dev
)
2468 struct xfrm_state
*x
= pkt_dev
->flows
[pkt_dev
->curfl
].x
;
2473 /* XXX: we dont support tunnel mode for now until
2474 * we resolve the dst issue */
2475 if (x
->props
.mode
!= XFRM_MODE_TRANSPORT
)
2478 spin_lock(&x
->lock
);
2480 err
= x
->outer_mode
->output(x
, skb
);
2483 err
= x
->type
->output(x
, skb
);
2487 x
->curlft
.bytes
+= skb
->len
;
2488 x
->curlft
.packets
++;
2490 spin_unlock(&x
->lock
);
2494 static void free_SAs(struct pktgen_dev
*pkt_dev
)
2496 if (pkt_dev
->cflows
) {
2497 /* let go of the SAs if we have them */
2499 for (i
= 0; i
< pkt_dev
->cflows
; i
++) {
2500 struct xfrm_state
*x
= pkt_dev
->flows
[i
].x
;
2503 pkt_dev
->flows
[i
].x
= NULL
;
2509 static int process_ipsec(struct pktgen_dev
*pkt_dev
,
2510 struct sk_buff
*skb
, __be16 protocol
)
2512 if (pkt_dev
->flags
& F_IPSEC_ON
) {
2513 struct xfrm_state
*x
= pkt_dev
->flows
[pkt_dev
->curfl
].x
;
2518 nhead
= x
->props
.header_len
- skb_headroom(skb
);
2520 ret
= pskb_expand_head(skb
, nhead
, 0, GFP_ATOMIC
);
2522 pr_err("Error expanding ipsec packet %d\n",
2528 /* ipsec is not expecting ll header */
2529 skb_pull(skb
, ETH_HLEN
);
2530 ret
= pktgen_output_ipsec(skb
, pkt_dev
);
2532 pr_err("Error creating ipsec packet %d\n", ret
);
2536 eth
= (__u8
*) skb_push(skb
, ETH_HLEN
);
2537 memcpy(eth
, pkt_dev
->hh
, 12);
2538 *(u16
*) ð
[12] = protocol
;
2548 static void mpls_push(__be32
*mpls
, struct pktgen_dev
*pkt_dev
)
2551 for (i
= 0; i
< pkt_dev
->nr_labels
; i
++)
2552 *mpls
++ = pkt_dev
->labels
[i
] & ~MPLS_STACK_BOTTOM
;
2555 *mpls
|= MPLS_STACK_BOTTOM
;
2558 static inline __be16
build_tci(unsigned int id
, unsigned int cfi
,
2561 return htons(id
| (cfi
<< 12) | (prio
<< 13));
2564 static void pktgen_finalize_skb(struct pktgen_dev
*pkt_dev
, struct sk_buff
*skb
,
2567 struct timeval timestamp
;
2568 struct pktgen_hdr
*pgh
;
2570 pgh
= (struct pktgen_hdr
*)skb_put(skb
, sizeof(*pgh
));
2571 datalen
-= sizeof(*pgh
);
2573 if (pkt_dev
->nfrags
<= 0) {
2574 memset(skb_put(skb
, datalen
), 0, datalen
);
2576 int frags
= pkt_dev
->nfrags
;
2581 if (frags
> MAX_SKB_FRAGS
)
2582 frags
= MAX_SKB_FRAGS
;
2583 len
= datalen
- frags
* PAGE_SIZE
;
2585 memset(skb_put(skb
, len
), 0, len
);
2586 datalen
= frags
* PAGE_SIZE
;
2590 frag_len
= (datalen
/frags
) < PAGE_SIZE
?
2591 (datalen
/frags
) : PAGE_SIZE
;
2592 while (datalen
> 0) {
2593 if (unlikely(!pkt_dev
->page
)) {
2594 int node
= numa_node_id();
2596 if (pkt_dev
->node
>= 0 && (pkt_dev
->flags
& F_NODE
))
2597 node
= pkt_dev
->node
;
2598 pkt_dev
->page
= alloc_pages_node(node
, GFP_KERNEL
| __GFP_ZERO
, 0);
2602 skb_shinfo(skb
)->frags
[i
].page
= pkt_dev
->page
;
2603 get_page(pkt_dev
->page
);
2604 skb_shinfo(skb
)->frags
[i
].page_offset
= 0;
2605 /*last fragment, fill rest of data*/
2606 if (i
== (frags
- 1))
2607 skb_shinfo(skb
)->frags
[i
].size
=
2608 (datalen
< PAGE_SIZE
? datalen
: PAGE_SIZE
);
2610 skb_shinfo(skb
)->frags
[i
].size
= frag_len
;
2611 datalen
-= skb_shinfo(skb
)->frags
[i
].size
;
2612 skb
->len
+= skb_shinfo(skb
)->frags
[i
].size
;
2613 skb
->data_len
+= skb_shinfo(skb
)->frags
[i
].size
;
2615 skb_shinfo(skb
)->nr_frags
= i
;
2619 /* Stamp the time, and sequence number,
2620 * convert them to network byte order
2622 pgh
->pgh_magic
= htonl(PKTGEN_MAGIC
);
2623 pgh
->seq_num
= htonl(pkt_dev
->seq_num
);
2625 do_gettimeofday(×tamp
);
2626 pgh
->tv_sec
= htonl(timestamp
.tv_sec
);
2627 pgh
->tv_usec
= htonl(timestamp
.tv_usec
);
2630 static struct sk_buff
*fill_packet_ipv4(struct net_device
*odev
,
2631 struct pktgen_dev
*pkt_dev
)
2633 struct sk_buff
*skb
= NULL
;
2635 struct udphdr
*udph
;
2638 __be16 protocol
= htons(ETH_P_IP
);
2640 __be16
*vlan_tci
= NULL
; /* Encapsulates priority and VLAN ID */
2641 __be16
*vlan_encapsulated_proto
= NULL
; /* packet type ID field (or len) for VLAN tag */
2642 __be16
*svlan_tci
= NULL
; /* Encapsulates priority and SVLAN ID */
2643 __be16
*svlan_encapsulated_proto
= NULL
; /* packet type ID field (or len) for SVLAN tag */
2646 if (pkt_dev
->nr_labels
)
2647 protocol
= htons(ETH_P_MPLS_UC
);
2649 if (pkt_dev
->vlan_id
!= 0xffff)
2650 protocol
= htons(ETH_P_8021Q
);
2652 /* Update any of the values, used when we're incrementing various
2655 mod_cur_headers(pkt_dev
);
2656 queue_map
= pkt_dev
->cur_queue_map
;
2658 datalen
= (odev
->hard_header_len
+ 16) & ~0xf;
2660 if (pkt_dev
->flags
& F_NODE
) {
2663 if (pkt_dev
->node
>= 0)
2664 node
= pkt_dev
->node
;
2666 node
= numa_node_id();
2668 skb
= __alloc_skb(NET_SKB_PAD
+ pkt_dev
->cur_pkt_size
+ 64
2669 + datalen
+ pkt_dev
->pkt_overhead
, GFP_NOWAIT
, 0, node
);
2671 skb_reserve(skb
, NET_SKB_PAD
);
2676 skb
= __netdev_alloc_skb(odev
,
2677 pkt_dev
->cur_pkt_size
+ 64
2678 + datalen
+ pkt_dev
->pkt_overhead
, GFP_NOWAIT
);
2681 sprintf(pkt_dev
->result
, "No memory");
2684 prefetchw(skb
->data
);
2686 skb_reserve(skb
, datalen
);
2688 /* Reserve for ethernet and IP header */
2689 eth
= (__u8
*) skb_push(skb
, 14);
2690 mpls
= (__be32
*)skb_put(skb
, pkt_dev
->nr_labels
*sizeof(__u32
));
2691 if (pkt_dev
->nr_labels
)
2692 mpls_push(mpls
, pkt_dev
);
2694 if (pkt_dev
->vlan_id
!= 0xffff) {
2695 if (pkt_dev
->svlan_id
!= 0xffff) {
2696 svlan_tci
= (__be16
*)skb_put(skb
, sizeof(__be16
));
2697 *svlan_tci
= build_tci(pkt_dev
->svlan_id
,
2700 svlan_encapsulated_proto
= (__be16
*)skb_put(skb
, sizeof(__be16
));
2701 *svlan_encapsulated_proto
= htons(ETH_P_8021Q
);
2703 vlan_tci
= (__be16
*)skb_put(skb
, sizeof(__be16
));
2704 *vlan_tci
= build_tci(pkt_dev
->vlan_id
,
2707 vlan_encapsulated_proto
= (__be16
*)skb_put(skb
, sizeof(__be16
));
2708 *vlan_encapsulated_proto
= htons(ETH_P_IP
);
2711 skb
->network_header
= skb
->tail
;
2712 skb
->transport_header
= skb
->network_header
+ sizeof(struct iphdr
);
2713 skb_put(skb
, sizeof(struct iphdr
) + sizeof(struct udphdr
));
2714 skb_set_queue_mapping(skb
, queue_map
);
2715 skb
->priority
= pkt_dev
->skb_priority
;
2718 udph
= udp_hdr(skb
);
2720 memcpy(eth
, pkt_dev
->hh
, 12);
2721 *(__be16
*) & eth
[12] = protocol
;
2723 /* Eth + IPh + UDPh + mpls */
2724 datalen
= pkt_dev
->cur_pkt_size
- 14 - 20 - 8 -
2725 pkt_dev
->pkt_overhead
;
2726 if (datalen
< sizeof(struct pktgen_hdr
))
2727 datalen
= sizeof(struct pktgen_hdr
);
2729 udph
->source
= htons(pkt_dev
->cur_udp_src
);
2730 udph
->dest
= htons(pkt_dev
->cur_udp_dst
);
2731 udph
->len
= htons(datalen
+ 8); /* DATA + udphdr */
2732 udph
->check
= 0; /* No checksum */
2737 iph
->tos
= pkt_dev
->tos
;
2738 iph
->protocol
= IPPROTO_UDP
; /* UDP */
2739 iph
->saddr
= pkt_dev
->cur_saddr
;
2740 iph
->daddr
= pkt_dev
->cur_daddr
;
2741 iph
->id
= htons(pkt_dev
->ip_id
);
2744 iplen
= 20 + 8 + datalen
;
2745 iph
->tot_len
= htons(iplen
);
2747 iph
->check
= ip_fast_csum((void *)iph
, iph
->ihl
);
2748 skb
->protocol
= protocol
;
2749 skb
->mac_header
= (skb
->network_header
- ETH_HLEN
-
2750 pkt_dev
->pkt_overhead
);
2752 skb
->pkt_type
= PACKET_HOST
;
2753 pktgen_finalize_skb(pkt_dev
, skb
, datalen
);
2756 if (!process_ipsec(pkt_dev
, skb
, protocol
))
2764 * scan_ip6, fmt_ip taken from dietlibc-0.21
2765 * Author Felix von Leitner <felix-dietlibc@fefe.de>
2767 * Slightly modified for kernel.
2768 * Should be candidate for net/ipv4/utils.c
2772 static unsigned int scan_ip6(const char *s
, char ip
[16])
2775 unsigned int len
= 0;
2778 unsigned int prefixlen
= 0;
2779 unsigned int suffixlen
= 0;
2783 for (i
= 0; i
< 16; i
++)
2789 if (s
[1] == ':') { /* Found "::", skip to part 2 */
2797 u
= simple_strtoul(s
, &pos
, 16);
2801 if (prefixlen
== 12 && s
[i
] == '.') {
2803 /* the last 4 bytes may be written as IPv4 address */
2806 memcpy((struct in_addr
*)(ip
+ 12), &tmp
, sizeof(tmp
));
2809 ip
[prefixlen
++] = (u
>> 8);
2810 ip
[prefixlen
++] = (u
& 255);
2813 if (prefixlen
== 16)
2817 /* part 2, after "::" */
2824 } else if (suffixlen
!= 0)
2827 u
= simple_strtol(s
, &pos
, 16);
2834 if (suffixlen
+ prefixlen
<= 12 && s
[i
] == '.') {
2836 memcpy((struct in_addr
*)(suffix
+ suffixlen
), &tmp
,
2842 suffix
[suffixlen
++] = (u
>> 8);
2843 suffix
[suffixlen
++] = (u
& 255);
2846 if (prefixlen
+ suffixlen
== 16)
2849 for (i
= 0; i
< suffixlen
; i
++)
2850 ip
[16 - suffixlen
+ i
] = suffix
[i
];
2854 static struct sk_buff
*fill_packet_ipv6(struct net_device
*odev
,
2855 struct pktgen_dev
*pkt_dev
)
2857 struct sk_buff
*skb
= NULL
;
2859 struct udphdr
*udph
;
2861 struct ipv6hdr
*iph
;
2862 __be16 protocol
= htons(ETH_P_IPV6
);
2864 __be16
*vlan_tci
= NULL
; /* Encapsulates priority and VLAN ID */
2865 __be16
*vlan_encapsulated_proto
= NULL
; /* packet type ID field (or len) for VLAN tag */
2866 __be16
*svlan_tci
= NULL
; /* Encapsulates priority and SVLAN ID */
2867 __be16
*svlan_encapsulated_proto
= NULL
; /* packet type ID field (or len) for SVLAN tag */
2870 if (pkt_dev
->nr_labels
)
2871 protocol
= htons(ETH_P_MPLS_UC
);
2873 if (pkt_dev
->vlan_id
!= 0xffff)
2874 protocol
= htons(ETH_P_8021Q
);
2876 /* Update any of the values, used when we're incrementing various
2879 mod_cur_headers(pkt_dev
);
2880 queue_map
= pkt_dev
->cur_queue_map
;
2882 skb
= __netdev_alloc_skb(odev
,
2883 pkt_dev
->cur_pkt_size
+ 64
2884 + 16 + pkt_dev
->pkt_overhead
, GFP_NOWAIT
);
2886 sprintf(pkt_dev
->result
, "No memory");
2889 prefetchw(skb
->data
);
2891 skb_reserve(skb
, 16);
2893 /* Reserve for ethernet and IP header */
2894 eth
= (__u8
*) skb_push(skb
, 14);
2895 mpls
= (__be32
*)skb_put(skb
, pkt_dev
->nr_labels
*sizeof(__u32
));
2896 if (pkt_dev
->nr_labels
)
2897 mpls_push(mpls
, pkt_dev
);
2899 if (pkt_dev
->vlan_id
!= 0xffff) {
2900 if (pkt_dev
->svlan_id
!= 0xffff) {
2901 svlan_tci
= (__be16
*)skb_put(skb
, sizeof(__be16
));
2902 *svlan_tci
= build_tci(pkt_dev
->svlan_id
,
2905 svlan_encapsulated_proto
= (__be16
*)skb_put(skb
, sizeof(__be16
));
2906 *svlan_encapsulated_proto
= htons(ETH_P_8021Q
);
2908 vlan_tci
= (__be16
*)skb_put(skb
, sizeof(__be16
));
2909 *vlan_tci
= build_tci(pkt_dev
->vlan_id
,
2912 vlan_encapsulated_proto
= (__be16
*)skb_put(skb
, sizeof(__be16
));
2913 *vlan_encapsulated_proto
= htons(ETH_P_IPV6
);
2916 skb
->network_header
= skb
->tail
;
2917 skb
->transport_header
= skb
->network_header
+ sizeof(struct ipv6hdr
);
2918 skb_put(skb
, sizeof(struct ipv6hdr
) + sizeof(struct udphdr
));
2919 skb_set_queue_mapping(skb
, queue_map
);
2920 skb
->priority
= pkt_dev
->skb_priority
;
2921 iph
= ipv6_hdr(skb
);
2922 udph
= udp_hdr(skb
);
2924 memcpy(eth
, pkt_dev
->hh
, 12);
2925 *(__be16
*) ð
[12] = protocol
;
2927 /* Eth + IPh + UDPh + mpls */
2928 datalen
= pkt_dev
->cur_pkt_size
- 14 -
2929 sizeof(struct ipv6hdr
) - sizeof(struct udphdr
) -
2930 pkt_dev
->pkt_overhead
;
2932 if (datalen
< sizeof(struct pktgen_hdr
)) {
2933 datalen
= sizeof(struct pktgen_hdr
);
2934 if (net_ratelimit())
2935 pr_info("increased datalen to %d\n", datalen
);
2938 udph
->source
= htons(pkt_dev
->cur_udp_src
);
2939 udph
->dest
= htons(pkt_dev
->cur_udp_dst
);
2940 udph
->len
= htons(datalen
+ sizeof(struct udphdr
));
2941 udph
->check
= 0; /* No checksum */
2943 *(__be32
*) iph
= htonl(0x60000000); /* Version + flow */
2945 if (pkt_dev
->traffic_class
) {
2946 /* Version + traffic class + flow (0) */
2947 *(__be32
*)iph
|= htonl(0x60000000 | (pkt_dev
->traffic_class
<< 20));
2950 iph
->hop_limit
= 32;
2952 iph
->payload_len
= htons(sizeof(struct udphdr
) + datalen
);
2953 iph
->nexthdr
= IPPROTO_UDP
;
2955 ipv6_addr_copy(&iph
->daddr
, &pkt_dev
->cur_in6_daddr
);
2956 ipv6_addr_copy(&iph
->saddr
, &pkt_dev
->cur_in6_saddr
);
2958 skb
->mac_header
= (skb
->network_header
- ETH_HLEN
-
2959 pkt_dev
->pkt_overhead
);
2960 skb
->protocol
= protocol
;
2962 skb
->pkt_type
= PACKET_HOST
;
2964 pktgen_finalize_skb(pkt_dev
, skb
, datalen
);
2969 static struct sk_buff
*fill_packet(struct net_device
*odev
,
2970 struct pktgen_dev
*pkt_dev
)
2972 if (pkt_dev
->flags
& F_IPV6
)
2973 return fill_packet_ipv6(odev
, pkt_dev
);
2975 return fill_packet_ipv4(odev
, pkt_dev
);
2978 static void pktgen_clear_counters(struct pktgen_dev
*pkt_dev
)
2980 pkt_dev
->seq_num
= 1;
2981 pkt_dev
->idle_acc
= 0;
2983 pkt_dev
->tx_bytes
= 0;
2984 pkt_dev
->errors
= 0;
2987 /* Set up structure for sending pkts, clear counters */
2989 static void pktgen_run(struct pktgen_thread
*t
)
2991 struct pktgen_dev
*pkt_dev
;
2997 list_for_each_entry(pkt_dev
, &t
->if_list
, list
) {
3000 * setup odev and create initial packet.
3002 pktgen_setup_inject(pkt_dev
);
3004 if (pkt_dev
->odev
) {
3005 pktgen_clear_counters(pkt_dev
);
3006 pkt_dev
->running
= 1; /* Cranke yeself! */
3007 pkt_dev
->skb
= NULL
;
3008 pkt_dev
->started_at
=
3009 pkt_dev
->next_tx
= ktime_now();
3011 set_pkt_overhead(pkt_dev
);
3013 strcpy(pkt_dev
->result
, "Starting");
3016 strcpy(pkt_dev
->result
, "Error starting");
3020 t
->control
&= ~(T_STOP
);
3023 static void pktgen_stop_all_threads_ifs(void)
3025 struct pktgen_thread
*t
;
3029 mutex_lock(&pktgen_thread_lock
);
3031 list_for_each_entry(t
, &pktgen_threads
, th_list
)
3032 t
->control
|= T_STOP
;
3034 mutex_unlock(&pktgen_thread_lock
);
3037 static int thread_is_running(const struct pktgen_thread
*t
)
3039 const struct pktgen_dev
*pkt_dev
;
3041 list_for_each_entry(pkt_dev
, &t
->if_list
, list
)
3042 if (pkt_dev
->running
)
3047 static int pktgen_wait_thread_run(struct pktgen_thread
*t
)
3051 while (thread_is_running(t
)) {
3055 msleep_interruptible(100);
3057 if (signal_pending(current
))
3067 static int pktgen_wait_all_threads_run(void)
3069 struct pktgen_thread
*t
;
3072 mutex_lock(&pktgen_thread_lock
);
3074 list_for_each_entry(t
, &pktgen_threads
, th_list
) {
3075 sig
= pktgen_wait_thread_run(t
);
3081 list_for_each_entry(t
, &pktgen_threads
, th_list
)
3082 t
->control
|= (T_STOP
);
3084 mutex_unlock(&pktgen_thread_lock
);
3088 static void pktgen_run_all_threads(void)
3090 struct pktgen_thread
*t
;
3094 mutex_lock(&pktgen_thread_lock
);
3096 list_for_each_entry(t
, &pktgen_threads
, th_list
)
3097 t
->control
|= (T_RUN
);
3099 mutex_unlock(&pktgen_thread_lock
);
3101 /* Propagate thread->control */
3102 schedule_timeout_interruptible(msecs_to_jiffies(125));
3104 pktgen_wait_all_threads_run();
3107 static void pktgen_reset_all_threads(void)
3109 struct pktgen_thread
*t
;
3113 mutex_lock(&pktgen_thread_lock
);
3115 list_for_each_entry(t
, &pktgen_threads
, th_list
)
3116 t
->control
|= (T_REMDEVALL
);
3118 mutex_unlock(&pktgen_thread_lock
);
3120 /* Propagate thread->control */
3121 schedule_timeout_interruptible(msecs_to_jiffies(125));
3123 pktgen_wait_all_threads_run();
3126 static void show_results(struct pktgen_dev
*pkt_dev
, int nr_frags
)
3128 __u64 bps
, mbps
, pps
;
3129 char *p
= pkt_dev
->result
;
3130 ktime_t elapsed
= ktime_sub(pkt_dev
->stopped_at
,
3131 pkt_dev
->started_at
);
3132 ktime_t idle
= ns_to_ktime(pkt_dev
->idle_acc
);
3134 p
+= sprintf(p
, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
3135 (unsigned long long)ktime_to_us(elapsed
),
3136 (unsigned long long)ktime_to_us(ktime_sub(elapsed
, idle
)),
3137 (unsigned long long)ktime_to_us(idle
),
3138 (unsigned long long)pkt_dev
->sofar
,
3139 pkt_dev
->cur_pkt_size
, nr_frags
);
3141 pps
= div64_u64(pkt_dev
->sofar
* NSEC_PER_SEC
,
3142 ktime_to_ns(elapsed
));
3144 bps
= pps
* 8 * pkt_dev
->cur_pkt_size
;
3147 do_div(mbps
, 1000000);
3148 p
+= sprintf(p
, " %llupps %lluMb/sec (%llubps) errors: %llu",
3149 (unsigned long long)pps
,
3150 (unsigned long long)mbps
,
3151 (unsigned long long)bps
,
3152 (unsigned long long)pkt_dev
->errors
);
3155 /* Set stopped-at timer, remove from running list, do counters & statistics */
3156 static int pktgen_stop_device(struct pktgen_dev
*pkt_dev
)
3158 int nr_frags
= pkt_dev
->skb
? skb_shinfo(pkt_dev
->skb
)->nr_frags
: -1;
3160 if (!pkt_dev
->running
) {
3161 pr_warning("interface: %s is already stopped\n",
3166 kfree_skb(pkt_dev
->skb
);
3167 pkt_dev
->skb
= NULL
;
3168 pkt_dev
->stopped_at
= ktime_now();
3169 pkt_dev
->running
= 0;
3171 show_results(pkt_dev
, nr_frags
);
3176 static struct pktgen_dev
*next_to_run(struct pktgen_thread
*t
)
3178 struct pktgen_dev
*pkt_dev
, *best
= NULL
;
3182 list_for_each_entry(pkt_dev
, &t
->if_list
, list
) {
3183 if (!pkt_dev
->running
)
3187 else if (ktime_lt(pkt_dev
->next_tx
, best
->next_tx
))
3194 static void pktgen_stop(struct pktgen_thread
*t
)
3196 struct pktgen_dev
*pkt_dev
;
3202 list_for_each_entry(pkt_dev
, &t
->if_list
, list
) {
3203 pktgen_stop_device(pkt_dev
);
3210 * one of our devices needs to be removed - find it
3213 static void pktgen_rem_one_if(struct pktgen_thread
*t
)
3215 struct list_head
*q
, *n
;
3216 struct pktgen_dev
*cur
;
3222 list_for_each_safe(q
, n
, &t
->if_list
) {
3223 cur
= list_entry(q
, struct pktgen_dev
, list
);
3225 if (!cur
->removal_mark
)
3228 kfree_skb(cur
->skb
);
3231 pktgen_remove_device(t
, cur
);
3239 static void pktgen_rem_all_ifs(struct pktgen_thread
*t
)
3241 struct list_head
*q
, *n
;
3242 struct pktgen_dev
*cur
;
3246 /* Remove all devices, free mem */
3250 list_for_each_safe(q
, n
, &t
->if_list
) {
3251 cur
= list_entry(q
, struct pktgen_dev
, list
);
3253 kfree_skb(cur
->skb
);
3256 pktgen_remove_device(t
, cur
);
3262 static void pktgen_rem_thread(struct pktgen_thread
*t
)
3264 /* Remove from the thread list */
3266 remove_proc_entry(t
->tsk
->comm
, pg_proc_dir
);
3270 static void pktgen_resched(struct pktgen_dev
*pkt_dev
)
3272 ktime_t idle_start
= ktime_now();
3274 pkt_dev
->idle_acc
+= ktime_to_ns(ktime_sub(ktime_now(), idle_start
));
3277 static void pktgen_wait_for_skb(struct pktgen_dev
*pkt_dev
)
3279 ktime_t idle_start
= ktime_now();
3281 while (atomic_read(&(pkt_dev
->skb
->users
)) != 1) {
3282 if (signal_pending(current
))
3286 pktgen_resched(pkt_dev
);
3290 pkt_dev
->idle_acc
+= ktime_to_ns(ktime_sub(ktime_now(), idle_start
));
3293 static void pktgen_xmit(struct pktgen_dev
*pkt_dev
)
3295 struct net_device
*odev
= pkt_dev
->odev
;
3296 netdev_tx_t (*xmit
)(struct sk_buff
*, struct net_device
*)
3297 = odev
->netdev_ops
->ndo_start_xmit
;
3298 struct netdev_queue
*txq
;
3302 /* If device is offline, then don't send */
3303 if (unlikely(!netif_running(odev
) || !netif_carrier_ok(odev
))) {
3304 pktgen_stop_device(pkt_dev
);
3308 /* This is max DELAY, this has special meaning of
3311 if (unlikely(pkt_dev
->delay
== ULLONG_MAX
)) {
3312 pkt_dev
->next_tx
= ktime_add_ns(ktime_now(), ULONG_MAX
);
3316 /* If no skb or clone count exhausted then get new one */
3317 if (!pkt_dev
->skb
|| (pkt_dev
->last_ok
&&
3318 ++pkt_dev
->clone_count
>= pkt_dev
->clone_skb
)) {
3319 /* build a new pkt */
3320 kfree_skb(pkt_dev
->skb
);
3322 pkt_dev
->skb
= fill_packet(odev
, pkt_dev
);
3323 if (pkt_dev
->skb
== NULL
) {
3324 pr_err("ERROR: couldn't allocate skb in fill_packet\n");
3326 pkt_dev
->clone_count
--; /* back out increment, OOM */
3329 pkt_dev
->last_pkt_size
= pkt_dev
->skb
->len
;
3330 pkt_dev
->allocated_skbs
++;
3331 pkt_dev
->clone_count
= 0; /* reset counter */
3334 if (pkt_dev
->delay
&& pkt_dev
->last_ok
)
3335 spin(pkt_dev
, pkt_dev
->next_tx
);
3337 queue_map
= skb_get_queue_mapping(pkt_dev
->skb
);
3338 txq
= netdev_get_tx_queue(odev
, queue_map
);
3340 __netif_tx_lock_bh(txq
);
3342 if (unlikely(netif_tx_queue_frozen_or_stopped(txq
))) {
3343 ret
= NETDEV_TX_BUSY
;
3344 pkt_dev
->last_ok
= 0;
3347 atomic_inc(&(pkt_dev
->skb
->users
));
3348 ret
= (*xmit
)(pkt_dev
->skb
, odev
);
3352 txq_trans_update(txq
);
3353 pkt_dev
->last_ok
= 1;
3356 pkt_dev
->tx_bytes
+= pkt_dev
->last_pkt_size
;
3360 case NET_XMIT_POLICED
:
3361 /* skb has been consumed */
3364 default: /* Drivers are not supposed to return other values! */
3365 if (net_ratelimit())
3366 pr_info("%s xmit error: %d\n", pkt_dev
->odevname
, ret
);
3369 case NETDEV_TX_LOCKED
:
3370 case NETDEV_TX_BUSY
:
3371 /* Retry it next time */
3372 atomic_dec(&(pkt_dev
->skb
->users
));
3373 pkt_dev
->last_ok
= 0;
3376 __netif_tx_unlock_bh(txq
);
3378 /* If pkt_dev->count is zero, then run forever */
3379 if ((pkt_dev
->count
!= 0) && (pkt_dev
->sofar
>= pkt_dev
->count
)) {
3380 pktgen_wait_for_skb(pkt_dev
);
3382 /* Done with this */
3383 pktgen_stop_device(pkt_dev
);
3388 * Main loop of the thread goes here
3391 static int pktgen_thread_worker(void *arg
)
3394 struct pktgen_thread
*t
= arg
;
3395 struct pktgen_dev
*pkt_dev
= NULL
;
3398 BUG_ON(smp_processor_id() != cpu
);
3400 init_waitqueue_head(&t
->queue
);
3401 complete(&t
->start_done
);
3403 pr_debug("starting pktgen/%d: pid=%d\n", cpu
, task_pid_nr(current
));
3405 set_current_state(TASK_INTERRUPTIBLE
);
3409 while (!kthread_should_stop()) {
3410 pkt_dev
= next_to_run(t
);
3412 if (unlikely(!pkt_dev
&& t
->control
== 0)) {
3415 wait_event_interruptible_timeout(t
->queue
,
3422 __set_current_state(TASK_RUNNING
);
3424 if (likely(pkt_dev
)) {
3425 pktgen_xmit(pkt_dev
);
3428 pktgen_resched(pkt_dev
);
3433 if (t
->control
& T_STOP
) {
3435 t
->control
&= ~(T_STOP
);
3438 if (t
->control
& T_RUN
) {
3440 t
->control
&= ~(T_RUN
);
3443 if (t
->control
& T_REMDEVALL
) {
3444 pktgen_rem_all_ifs(t
);
3445 t
->control
&= ~(T_REMDEVALL
);
3448 if (t
->control
& T_REMDEV
) {
3449 pktgen_rem_one_if(t
);
3450 t
->control
&= ~(T_REMDEV
);
3455 set_current_state(TASK_INTERRUPTIBLE
);
3458 pr_debug("%s stopping all device\n", t
->tsk
->comm
);
3461 pr_debug("%s removing all device\n", t
->tsk
->comm
);
3462 pktgen_rem_all_ifs(t
);
3464 pr_debug("%s removing thread\n", t
->tsk
->comm
);
3465 pktgen_rem_thread(t
);
3467 /* Wait for kthread_stop */
3468 while (!kthread_should_stop()) {
3469 set_current_state(TASK_INTERRUPTIBLE
);
3472 __set_current_state(TASK_RUNNING
);
3477 static struct pktgen_dev
*pktgen_find_dev(struct pktgen_thread
*t
,
3478 const char *ifname
, bool exact
)
3480 struct pktgen_dev
*p
, *pkt_dev
= NULL
;
3481 size_t len
= strlen(ifname
);
3484 list_for_each_entry(p
, &t
->if_list
, list
)
3485 if (strncmp(p
->odevname
, ifname
, len
) == 0) {
3486 if (p
->odevname
[len
]) {
3487 if (exact
|| p
->odevname
[len
] != '@')
3495 pr_debug("find_dev(%s) returning %p\n", ifname
, pkt_dev
);
3500 * Adds a dev at front of if_list.
3503 static int add_dev_to_thread(struct pktgen_thread
*t
,
3504 struct pktgen_dev
*pkt_dev
)
3510 if (pkt_dev
->pg_thread
) {
3511 pr_err("ERROR: already assigned to a thread\n");
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
;
3531 int node
= cpu_to_node(t
->cpu
);
3533 /* We don't allow a device to be on several threads */
3535 pkt_dev
= __pktgen_NN_threads(ifname
, FIND
);
3537 pr_err("ERROR: interface already used\n");
3541 pkt_dev
= kzalloc_node(sizeof(struct pktgen_dev
), GFP_KERNEL
, node
);
3545 strcpy(pkt_dev
->odevname
, ifname
);
3546 pkt_dev
->flows
= vmalloc_node(MAX_CFLOWS
* sizeof(struct flow_state
),
3548 if (pkt_dev
->flows
== NULL
) {
3552 memset(pkt_dev
->flows
, 0, MAX_CFLOWS
* sizeof(struct flow_state
));
3554 pkt_dev
->removal_mark
= 0;
3555 pkt_dev
->min_pkt_size
= ETH_ZLEN
;
3556 pkt_dev
->max_pkt_size
= ETH_ZLEN
;
3557 pkt_dev
->nfrags
= 0;
3558 pkt_dev
->clone_skb
= pg_clone_skb_d
;
3559 pkt_dev
->delay
= pg_delay_d
;
3560 pkt_dev
->count
= pg_count_d
;
3562 pkt_dev
->udp_src_min
= 9; /* sink port */
3563 pkt_dev
->udp_src_max
= 9;
3564 pkt_dev
->udp_dst_min
= 9;
3565 pkt_dev
->udp_dst_max
= 9;
3567 pkt_dev
->vlan_p
= 0;
3568 pkt_dev
->vlan_cfi
= 0;
3569 pkt_dev
->vlan_id
= 0xffff;
3570 pkt_dev
->svlan_p
= 0;
3571 pkt_dev
->svlan_cfi
= 0;
3572 pkt_dev
->svlan_id
= 0xffff;
3575 err
= pktgen_setup_dev(pkt_dev
, ifname
);
3579 pkt_dev
->entry
= proc_create_data(ifname
, 0600, pg_proc_dir
,
3580 &pktgen_if_fops
, pkt_dev
);
3581 if (!pkt_dev
->entry
) {
3582 pr_err("cannot create %s/%s procfs entry\n",
3583 PG_PROC_DIR
, ifname
);
3588 pkt_dev
->ipsmode
= XFRM_MODE_TRANSPORT
;
3589 pkt_dev
->ipsproto
= IPPROTO_ESP
;
3592 return add_dev_to_thread(t
, pkt_dev
);
3594 dev_put(pkt_dev
->odev
);
3599 vfree(pkt_dev
->flows
);
3604 static int __init
pktgen_create_thread(int cpu
)
3606 struct pktgen_thread
*t
;
3607 struct proc_dir_entry
*pe
;
3608 struct task_struct
*p
;
3610 t
= kzalloc_node(sizeof(struct pktgen_thread
), GFP_KERNEL
,
3613 pr_err("ERROR: out of memory, can't create new thread\n");
3617 spin_lock_init(&t
->if_lock
);
3620 INIT_LIST_HEAD(&t
->if_list
);
3622 list_add_tail(&t
->th_list
, &pktgen_threads
);
3623 init_completion(&t
->start_done
);
3625 p
= kthread_create_on_node(pktgen_thread_worker
,
3628 "kpktgend_%d", cpu
);
3630 pr_err("kernel_thread() failed for cpu %d\n", t
->cpu
);
3631 list_del(&t
->th_list
);
3635 kthread_bind(p
, cpu
);
3638 pe
= proc_create_data(t
->tsk
->comm
, 0600, pg_proc_dir
,
3639 &pktgen_thread_fops
, t
);
3641 pr_err("cannot create %s/%s procfs entry\n",
3642 PG_PROC_DIR
, t
->tsk
->comm
);
3644 list_del(&t
->th_list
);
3650 wait_for_completion(&t
->start_done
);
3656 * Removes a device from the thread if_list.
3658 static void _rem_dev_from_if_list(struct pktgen_thread
*t
,
3659 struct pktgen_dev
*pkt_dev
)
3661 struct list_head
*q
, *n
;
3662 struct pktgen_dev
*p
;
3664 list_for_each_safe(q
, n
, &t
->if_list
) {
3665 p
= list_entry(q
, struct pktgen_dev
, list
);
3671 static int pktgen_remove_device(struct pktgen_thread
*t
,
3672 struct pktgen_dev
*pkt_dev
)
3675 pr_debug("remove_device pkt_dev=%p\n", pkt_dev
);
3677 if (pkt_dev
->running
) {
3678 pr_warning("WARNING: trying to remove a running interface, stopping it now\n");
3679 pktgen_stop_device(pkt_dev
);
3682 /* Dis-associate from the interface */
3684 if (pkt_dev
->odev
) {
3685 dev_put(pkt_dev
->odev
);
3686 pkt_dev
->odev
= NULL
;
3689 /* And update the thread if_list */
3691 _rem_dev_from_if_list(t
, pkt_dev
);
3694 remove_proc_entry(pkt_dev
->entry
->name
, pg_proc_dir
);
3699 vfree(pkt_dev
->flows
);
3701 put_page(pkt_dev
->page
);
3706 static int __init
pg_init(void)
3709 struct proc_dir_entry
*pe
;
3711 pr_info("%s", version
);
3713 pg_proc_dir
= proc_mkdir(PG_PROC_DIR
, init_net
.proc_net
);
3717 pe
= proc_create(PGCTRL
, 0600, pg_proc_dir
, &pktgen_fops
);
3719 pr_err("ERROR: cannot create %s procfs entry\n", PGCTRL
);
3720 proc_net_remove(&init_net
, PG_PROC_DIR
);
3724 /* Register us to receive netdevice events */
3725 register_netdevice_notifier(&pktgen_notifier_block
);
3727 for_each_online_cpu(cpu
) {
3730 err
= pktgen_create_thread(cpu
);
3732 pr_warning("WARNING: Cannot create thread for cpu %d (%d)\n",
3736 if (list_empty(&pktgen_threads
)) {
3737 pr_err("ERROR: Initialization failed for all threads\n");
3738 unregister_netdevice_notifier(&pktgen_notifier_block
);
3739 remove_proc_entry(PGCTRL
, pg_proc_dir
);
3740 proc_net_remove(&init_net
, PG_PROC_DIR
);
3747 static void __exit
pg_cleanup(void)
3749 struct pktgen_thread
*t
;
3750 struct list_head
*q
, *n
;
3752 /* Stop all interfaces & threads */
3753 pktgen_exiting
= true;
3755 list_for_each_safe(q
, n
, &pktgen_threads
) {
3756 t
= list_entry(q
, struct pktgen_thread
, th_list
);
3757 kthread_stop(t
->tsk
);
3761 /* Un-register us from receiving netdevice events */
3762 unregister_netdevice_notifier(&pktgen_notifier_block
);
3764 /* Clean up proc file system */
3765 remove_proc_entry(PGCTRL
, pg_proc_dir
);
3766 proc_net_remove(&init_net
, PG_PROC_DIR
);
3769 module_init(pg_init
);
3770 module_exit(pg_cleanup
);
3772 MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se>");
3773 MODULE_DESCRIPTION("Packet Generator tool");
3774 MODULE_LICENSE("GPL");
3775 MODULE_VERSION(VERSION
);
3776 module_param(pg_count_d
, int, 0);
3777 MODULE_PARM_DESC(pg_count_d
, "Default number of packets to inject");
3778 module_param(pg_delay_d
, int, 0);
3779 MODULE_PARM_DESC(pg_delay_d
, "Default delay between packets (nanoseconds)");
3780 module_param(pg_clone_skb_d
, int, 0);
3781 MODULE_PARM_DESC(pg_clone_skb_d
, "Default number of copies of the same packet");
3782 module_param(debug
, int, 0);
3783 MODULE_PARM_DESC(debug
, "Enable debugging of pktgen module");