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.70: Packet Generator for packet performance testing.\n"
173 #define IP_NAME_SZ 32
174 #define MAX_MPLS_LABELS 16 /* This is the max label stack depth */
175 #define MPLS_STACK_BOTTOM htonl(0x00000100)
177 /* Device flag bits */
178 #define F_IPSRC_RND (1<<0) /* IP-Src Random */
179 #define F_IPDST_RND (1<<1) /* IP-Dst Random */
180 #define F_UDPSRC_RND (1<<2) /* UDP-Src Random */
181 #define F_UDPDST_RND (1<<3) /* UDP-Dst Random */
182 #define F_MACSRC_RND (1<<4) /* MAC-Src Random */
183 #define F_MACDST_RND (1<<5) /* MAC-Dst Random */
184 #define F_TXSIZE_RND (1<<6) /* Transmit size is random */
185 #define F_IPV6 (1<<7) /* Interface in IPV6 Mode */
186 #define F_MPLS_RND (1<<8) /* Random MPLS labels */
187 #define F_VID_RND (1<<9) /* Random VLAN ID */
188 #define F_SVID_RND (1<<10) /* Random SVLAN ID */
189 #define F_FLOW_SEQ (1<<11) /* Sequential flows */
190 #define F_IPSEC_ON (1<<12) /* ipsec on for flows */
191 #define F_QUEUE_MAP_RND (1<<13) /* queue map Random */
192 #define F_QUEUE_MAP_CPU (1<<14) /* queue map mirrors smp_processor_id() */
194 /* Thread control flag bits */
195 #define T_TERMINATE (1<<0)
196 #define T_STOP (1<<1) /* Stop run */
197 #define T_RUN (1<<2) /* Start run */
198 #define T_REMDEVALL (1<<3) /* Remove all devs */
199 #define T_REMDEV (1<<4) /* Remove one dev */
201 /* If lock -- can be removed after some work */
202 #define if_lock(t) spin_lock(&(t->if_lock));
203 #define if_unlock(t) spin_unlock(&(t->if_lock));
205 /* Used to help with determining the pkts on receive */
206 #define PKTGEN_MAGIC 0xbe9be955
207 #define PG_PROC_DIR "pktgen"
208 #define PGCTRL "pgctrl"
209 static struct proc_dir_entry
*pg_proc_dir
= NULL
;
211 #define MAX_CFLOWS 65536
213 #define VLAN_TAG_SIZE(x) ((x)->vlan_id == 0xffff ? 0 : 4)
214 #define SVLAN_TAG_SIZE(x) ((x)->svlan_id == 0xffff ? 0 : 4)
220 struct xfrm_state
*x
;
226 #define F_INIT (1<<0) /* flow has been initialized */
230 * Try to keep frequent/infrequent used vars. separated.
232 struct proc_dir_entry
*entry
; /* proc file */
233 struct pktgen_thread
*pg_thread
;/* the owner */
234 struct list_head list
; /* Used for chaining in the thread's run-queue */
236 int running
; /* if this changes to false, the test will stop */
238 /* If min != max, then we will either do a linear iteration, or
239 * we will do a random selection from within the range.
242 int removal_mark
; /* non-zero => the device is marked for
243 * removal by worker thread */
245 int min_pkt_size
; /* = ETH_ZLEN; */
246 int max_pkt_size
; /* = ETH_ZLEN; */
247 int pkt_overhead
; /* overhead for MPLS, VLANs, IPSEC etc */
249 __u32 delay_us
; /* Default delay */
251 __u64 count
; /* Default No packets to send */
252 __u64 sofar
; /* How many pkts we've sent so far */
253 __u64 tx_bytes
; /* How many bytes we've transmitted */
254 __u64 errors
; /* Errors when trying to transmit, pkts will be re-sent */
256 /* runtime counters relating to clone_skb */
257 __u64 next_tx_us
; /* timestamp of when to tx next */
260 __u64 allocated_skbs
;
262 int last_ok
; /* Was last skb sent?
263 * Or a failed transmit of some sort? This will keep
264 * sequence numbers in order, for example.
266 __u64 started_at
; /* micro-seconds */
267 __u64 stopped_at
; /* micro-seconds */
268 __u64 idle_acc
; /* micro-seconds */
271 int clone_skb
; /* Use multiple SKBs during packet gen. If this number
272 * is greater than 1, then that many copies of the same
273 * packet will be sent before a new packet is allocated.
274 * For instance, if you want to send 1024 identical packets
275 * before creating a new packet, set clone_skb to 1024.
278 char dst_min
[IP_NAME_SZ
]; /* IP, ie 1.2.3.4 */
279 char dst_max
[IP_NAME_SZ
]; /* IP, ie 1.2.3.4 */
280 char src_min
[IP_NAME_SZ
]; /* IP, ie 1.2.3.4 */
281 char src_max
[IP_NAME_SZ
]; /* IP, ie 1.2.3.4 */
283 struct in6_addr in6_saddr
;
284 struct in6_addr in6_daddr
;
285 struct in6_addr cur_in6_daddr
;
286 struct in6_addr cur_in6_saddr
;
288 struct in6_addr min_in6_daddr
;
289 struct in6_addr max_in6_daddr
;
290 struct in6_addr min_in6_saddr
;
291 struct in6_addr max_in6_saddr
;
293 /* If we're doing ranges, random or incremental, then this
294 * defines the min/max for those ranges.
296 __be32 saddr_min
; /* inclusive, source IP address */
297 __be32 saddr_max
; /* exclusive, source IP address */
298 __be32 daddr_min
; /* inclusive, dest IP address */
299 __be32 daddr_max
; /* exclusive, dest IP address */
301 __u16 udp_src_min
; /* inclusive, source UDP port */
302 __u16 udp_src_max
; /* exclusive, source UDP port */
303 __u16 udp_dst_min
; /* inclusive, dest UDP port */
304 __u16 udp_dst_max
; /* exclusive, dest UDP port */
307 __u8 tos
; /* six most significant bits of (former) IPv4 TOS are for dscp codepoint */
308 __u8 traffic_class
; /* ditto for the (former) Traffic Class in IPv6 (see RFC 3260, sec. 4) */
311 unsigned nr_labels
; /* Depth of stack, 0 = no MPLS */
312 __be32 labels
[MAX_MPLS_LABELS
];
314 /* VLAN/SVLAN (802.1Q/Q-in-Q) */
317 __u16 vlan_id
; /* 0xffff means no vlan tag */
321 __u16 svlan_id
; /* 0xffff means no svlan tag */
323 __u32 src_mac_count
; /* How many MACs to iterate through */
324 __u32 dst_mac_count
; /* How many MACs to iterate through */
326 unsigned char dst_mac
[ETH_ALEN
];
327 unsigned char src_mac
[ETH_ALEN
];
329 __u32 cur_dst_mac_offset
;
330 __u32 cur_src_mac_offset
;
340 0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
342 We fill in SRC address later
343 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
347 __u16 pad
; /* pad out the hh struct to an even 16 bytes */
349 struct sk_buff
*skb
; /* skb we are to transmit next, mainly used for when we
350 * are transmitting the same one multiple times
352 struct net_device
*odev
; /* The out-going device. Note that the device should
353 * have it's pg_info pointer pointing back to this
354 * device. This will be set when the user specifies
355 * the out-going device name (not when the inject is
356 * started as it used to do.)
358 struct flow_state
*flows
;
359 unsigned cflows
; /* Concurrent flows (config) */
360 unsigned lflow
; /* Flow length (config) */
361 unsigned nflows
; /* accumulated flows (stats) */
362 unsigned curfl
; /* current sequenced flow (state)*/
368 __u8 ipsmode
; /* IPSEC mode (config) */
369 __u8 ipsproto
; /* IPSEC type (config) */
381 struct pktgen_thread
{
383 struct list_head if_list
; /* All device here */
384 struct list_head th_list
;
385 struct task_struct
*tsk
;
388 /* Field for thread to receive "posted" events terminate, stop ifs etc. */
393 wait_queue_head_t queue
;
394 struct completion start_done
;
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
->flags
& F_QUEUE_MAP_CPU
)
626 seq_printf(seq
, "QUEUE_MAP_CPU ");
628 if (pkt_dev
->cflows
) {
629 if (pkt_dev
->flags
& F_FLOW_SEQ
)
630 seq_printf(seq
, "FLOW_SEQ "); /*in sequence flows*/
632 seq_printf(seq
, "FLOW_RND ");
636 if (pkt_dev
->flags
& F_IPSEC_ON
)
637 seq_printf(seq
, "IPSEC ");
640 if (pkt_dev
->flags
& F_MACSRC_RND
)
641 seq_printf(seq
, "MACSRC_RND ");
643 if (pkt_dev
->flags
& F_MACDST_RND
)
644 seq_printf(seq
, "MACDST_RND ");
646 if (pkt_dev
->flags
& F_VID_RND
)
647 seq_printf(seq
, "VID_RND ");
649 if (pkt_dev
->flags
& F_SVID_RND
)
650 seq_printf(seq
, "SVID_RND ");
654 sa
= pkt_dev
->started_at
;
655 stopped
= pkt_dev
->stopped_at
;
656 if (pkt_dev
->running
)
657 stopped
= now
; /* not really stopped, more like last-running-at */
660 "Current:\n pkts-sofar: %llu errors: %llu\n started: %lluus stopped: %lluus idle: %lluus\n",
661 (unsigned long long)pkt_dev
->sofar
,
662 (unsigned long long)pkt_dev
->errors
, (unsigned long long)sa
,
663 (unsigned long long)stopped
,
664 (unsigned long long)pkt_dev
->idle_acc
);
667 " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n",
668 pkt_dev
->seq_num
, pkt_dev
->cur_dst_mac_offset
,
669 pkt_dev
->cur_src_mac_offset
);
671 if (pkt_dev
->flags
& F_IPV6
) {
672 char b1
[128], b2
[128];
673 fmt_ip6(b1
, pkt_dev
->cur_in6_daddr
.s6_addr
);
674 fmt_ip6(b2
, pkt_dev
->cur_in6_saddr
.s6_addr
);
675 seq_printf(seq
, " cur_saddr: %s cur_daddr: %s\n", b2
, b1
);
677 seq_printf(seq
, " cur_saddr: 0x%x cur_daddr: 0x%x\n",
678 pkt_dev
->cur_saddr
, pkt_dev
->cur_daddr
);
680 seq_printf(seq
, " cur_udp_dst: %d cur_udp_src: %d\n",
681 pkt_dev
->cur_udp_dst
, pkt_dev
->cur_udp_src
);
683 seq_printf(seq
, " cur_queue_map: %u\n", pkt_dev
->cur_queue_map
);
685 seq_printf(seq
, " flows: %u\n", pkt_dev
->nflows
);
687 if (pkt_dev
->result
[0])
688 seq_printf(seq
, "Result: %s\n", pkt_dev
->result
);
690 seq_printf(seq
, "Result: Idle\n");
696 static int hex32_arg(const char __user
*user_buffer
, unsigned long maxlen
, __u32
*num
)
701 for (; i
< maxlen
; i
++) {
704 if (get_user(c
, &user_buffer
[i
]))
706 if ((c
>= '0') && (c
<= '9'))
708 else if ((c
>= 'a') && (c
<= 'f'))
709 *num
|= c
- 'a' + 10;
710 else if ((c
>= 'A') && (c
<= 'F'))
711 *num
|= c
- 'A' + 10;
718 static int count_trail_chars(const char __user
* user_buffer
,
723 for (i
= 0; i
< maxlen
; i
++) {
725 if (get_user(c
, &user_buffer
[i
]))
743 static unsigned long num_arg(const char __user
* user_buffer
,
744 unsigned long maxlen
, unsigned long *num
)
749 for (; i
< maxlen
; i
++) {
751 if (get_user(c
, &user_buffer
[i
]))
753 if ((c
>= '0') && (c
<= '9')) {
762 static int strn_len(const char __user
* user_buffer
, unsigned int maxlen
)
766 for (; i
< maxlen
; i
++) {
768 if (get_user(c
, &user_buffer
[i
]))
786 static ssize_t
get_labels(const char __user
*buffer
, struct pktgen_dev
*pkt_dev
)
793 pkt_dev
->nr_labels
= 0;
796 len
= hex32_arg(&buffer
[i
], 8, &tmp
);
799 pkt_dev
->labels
[n
] = htonl(tmp
);
800 if (pkt_dev
->labels
[n
] & MPLS_STACK_BOTTOM
)
801 pkt_dev
->flags
|= F_MPLS_RND
;
803 if (get_user(c
, &buffer
[i
]))
807 if (n
>= MAX_MPLS_LABELS
)
811 pkt_dev
->nr_labels
= n
;
815 static ssize_t
pktgen_if_write(struct file
*file
,
816 const char __user
* user_buffer
, size_t count
,
819 struct seq_file
*seq
= (struct seq_file
*)file
->private_data
;
820 struct pktgen_dev
*pkt_dev
= seq
->private;
822 char name
[16], valstr
[32];
823 unsigned long value
= 0;
824 char *pg_result
= NULL
;
828 pg_result
= &(pkt_dev
->result
[0]);
831 printk(KERN_WARNING
"pktgen: wrong command format\n");
836 tmp
= count_trail_chars(&user_buffer
[i
], max
);
838 printk(KERN_WARNING
"pktgen: illegal format\n");
843 /* Read variable name */
845 len
= strn_len(&user_buffer
[i
], sizeof(name
) - 1);
849 memset(name
, 0, sizeof(name
));
850 if (copy_from_user(name
, &user_buffer
[i
], len
))
855 len
= count_trail_chars(&user_buffer
[i
], max
);
863 if (copy_from_user(tb
, user_buffer
, count
))
866 printk(KERN_DEBUG
"pktgen: %s,%lu buffer -:%s:-\n", name
,
867 (unsigned long)count
, tb
);
870 if (!strcmp(name
, "min_pkt_size")) {
871 len
= num_arg(&user_buffer
[i
], 10, &value
);
876 if (value
< 14 + 20 + 8)
878 if (value
!= pkt_dev
->min_pkt_size
) {
879 pkt_dev
->min_pkt_size
= value
;
880 pkt_dev
->cur_pkt_size
= value
;
882 sprintf(pg_result
, "OK: min_pkt_size=%u",
883 pkt_dev
->min_pkt_size
);
887 if (!strcmp(name
, "max_pkt_size")) {
888 len
= num_arg(&user_buffer
[i
], 10, &value
);
893 if (value
< 14 + 20 + 8)
895 if (value
!= pkt_dev
->max_pkt_size
) {
896 pkt_dev
->max_pkt_size
= value
;
897 pkt_dev
->cur_pkt_size
= value
;
899 sprintf(pg_result
, "OK: max_pkt_size=%u",
900 pkt_dev
->max_pkt_size
);
904 /* Shortcut for min = max */
906 if (!strcmp(name
, "pkt_size")) {
907 len
= num_arg(&user_buffer
[i
], 10, &value
);
912 if (value
< 14 + 20 + 8)
914 if (value
!= pkt_dev
->min_pkt_size
) {
915 pkt_dev
->min_pkt_size
= value
;
916 pkt_dev
->max_pkt_size
= value
;
917 pkt_dev
->cur_pkt_size
= value
;
919 sprintf(pg_result
, "OK: pkt_size=%u", pkt_dev
->min_pkt_size
);
923 if (!strcmp(name
, "debug")) {
924 len
= num_arg(&user_buffer
[i
], 10, &value
);
930 sprintf(pg_result
, "OK: debug=%u", debug
);
934 if (!strcmp(name
, "frags")) {
935 len
= num_arg(&user_buffer
[i
], 10, &value
);
940 pkt_dev
->nfrags
= value
;
941 sprintf(pg_result
, "OK: frags=%u", pkt_dev
->nfrags
);
944 if (!strcmp(name
, "delay")) {
945 len
= num_arg(&user_buffer
[i
], 10, &value
);
950 if (value
== 0x7FFFFFFF) {
951 pkt_dev
->delay_us
= 0x7FFFFFFF;
952 pkt_dev
->delay_ns
= 0;
954 pkt_dev
->delay_us
= value
/ 1000;
955 pkt_dev
->delay_ns
= value
% 1000;
957 sprintf(pg_result
, "OK: delay=%u",
958 1000 * pkt_dev
->delay_us
+ pkt_dev
->delay_ns
);
961 if (!strcmp(name
, "udp_src_min")) {
962 len
= num_arg(&user_buffer
[i
], 10, &value
);
967 if (value
!= pkt_dev
->udp_src_min
) {
968 pkt_dev
->udp_src_min
= value
;
969 pkt_dev
->cur_udp_src
= value
;
971 sprintf(pg_result
, "OK: udp_src_min=%u", pkt_dev
->udp_src_min
);
974 if (!strcmp(name
, "udp_dst_min")) {
975 len
= num_arg(&user_buffer
[i
], 10, &value
);
980 if (value
!= pkt_dev
->udp_dst_min
) {
981 pkt_dev
->udp_dst_min
= value
;
982 pkt_dev
->cur_udp_dst
= value
;
984 sprintf(pg_result
, "OK: udp_dst_min=%u", pkt_dev
->udp_dst_min
);
987 if (!strcmp(name
, "udp_src_max")) {
988 len
= num_arg(&user_buffer
[i
], 10, &value
);
993 if (value
!= pkt_dev
->udp_src_max
) {
994 pkt_dev
->udp_src_max
= value
;
995 pkt_dev
->cur_udp_src
= value
;
997 sprintf(pg_result
, "OK: udp_src_max=%u", pkt_dev
->udp_src_max
);
1000 if (!strcmp(name
, "udp_dst_max")) {
1001 len
= num_arg(&user_buffer
[i
], 10, &value
);
1006 if (value
!= pkt_dev
->udp_dst_max
) {
1007 pkt_dev
->udp_dst_max
= value
;
1008 pkt_dev
->cur_udp_dst
= value
;
1010 sprintf(pg_result
, "OK: udp_dst_max=%u", pkt_dev
->udp_dst_max
);
1013 if (!strcmp(name
, "clone_skb")) {
1014 len
= num_arg(&user_buffer
[i
], 10, &value
);
1019 pkt_dev
->clone_skb
= value
;
1021 sprintf(pg_result
, "OK: clone_skb=%d", pkt_dev
->clone_skb
);
1024 if (!strcmp(name
, "count")) {
1025 len
= num_arg(&user_buffer
[i
], 10, &value
);
1030 pkt_dev
->count
= value
;
1031 sprintf(pg_result
, "OK: count=%llu",
1032 (unsigned long long)pkt_dev
->count
);
1035 if (!strcmp(name
, "src_mac_count")) {
1036 len
= num_arg(&user_buffer
[i
], 10, &value
);
1041 if (pkt_dev
->src_mac_count
!= value
) {
1042 pkt_dev
->src_mac_count
= value
;
1043 pkt_dev
->cur_src_mac_offset
= 0;
1045 sprintf(pg_result
, "OK: src_mac_count=%d",
1046 pkt_dev
->src_mac_count
);
1049 if (!strcmp(name
, "dst_mac_count")) {
1050 len
= num_arg(&user_buffer
[i
], 10, &value
);
1055 if (pkt_dev
->dst_mac_count
!= value
) {
1056 pkt_dev
->dst_mac_count
= value
;
1057 pkt_dev
->cur_dst_mac_offset
= 0;
1059 sprintf(pg_result
, "OK: dst_mac_count=%d",
1060 pkt_dev
->dst_mac_count
);
1063 if (!strcmp(name
, "flag")) {
1066 len
= strn_len(&user_buffer
[i
], sizeof(f
) - 1);
1070 if (copy_from_user(f
, &user_buffer
[i
], len
))
1073 if (strcmp(f
, "IPSRC_RND") == 0)
1074 pkt_dev
->flags
|= F_IPSRC_RND
;
1076 else if (strcmp(f
, "!IPSRC_RND") == 0)
1077 pkt_dev
->flags
&= ~F_IPSRC_RND
;
1079 else if (strcmp(f
, "TXSIZE_RND") == 0)
1080 pkt_dev
->flags
|= F_TXSIZE_RND
;
1082 else if (strcmp(f
, "!TXSIZE_RND") == 0)
1083 pkt_dev
->flags
&= ~F_TXSIZE_RND
;
1085 else if (strcmp(f
, "IPDST_RND") == 0)
1086 pkt_dev
->flags
|= F_IPDST_RND
;
1088 else if (strcmp(f
, "!IPDST_RND") == 0)
1089 pkt_dev
->flags
&= ~F_IPDST_RND
;
1091 else if (strcmp(f
, "UDPSRC_RND") == 0)
1092 pkt_dev
->flags
|= F_UDPSRC_RND
;
1094 else if (strcmp(f
, "!UDPSRC_RND") == 0)
1095 pkt_dev
->flags
&= ~F_UDPSRC_RND
;
1097 else if (strcmp(f
, "UDPDST_RND") == 0)
1098 pkt_dev
->flags
|= F_UDPDST_RND
;
1100 else if (strcmp(f
, "!UDPDST_RND") == 0)
1101 pkt_dev
->flags
&= ~F_UDPDST_RND
;
1103 else if (strcmp(f
, "MACSRC_RND") == 0)
1104 pkt_dev
->flags
|= F_MACSRC_RND
;
1106 else if (strcmp(f
, "!MACSRC_RND") == 0)
1107 pkt_dev
->flags
&= ~F_MACSRC_RND
;
1109 else if (strcmp(f
, "MACDST_RND") == 0)
1110 pkt_dev
->flags
|= F_MACDST_RND
;
1112 else if (strcmp(f
, "!MACDST_RND") == 0)
1113 pkt_dev
->flags
&= ~F_MACDST_RND
;
1115 else if (strcmp(f
, "MPLS_RND") == 0)
1116 pkt_dev
->flags
|= F_MPLS_RND
;
1118 else if (strcmp(f
, "!MPLS_RND") == 0)
1119 pkt_dev
->flags
&= ~F_MPLS_RND
;
1121 else if (strcmp(f
, "VID_RND") == 0)
1122 pkt_dev
->flags
|= F_VID_RND
;
1124 else if (strcmp(f
, "!VID_RND") == 0)
1125 pkt_dev
->flags
&= ~F_VID_RND
;
1127 else if (strcmp(f
, "SVID_RND") == 0)
1128 pkt_dev
->flags
|= F_SVID_RND
;
1130 else if (strcmp(f
, "!SVID_RND") == 0)
1131 pkt_dev
->flags
&= ~F_SVID_RND
;
1133 else if (strcmp(f
, "FLOW_SEQ") == 0)
1134 pkt_dev
->flags
|= F_FLOW_SEQ
;
1136 else if (strcmp(f
, "QUEUE_MAP_RND") == 0)
1137 pkt_dev
->flags
|= F_QUEUE_MAP_RND
;
1139 else if (strcmp(f
, "!QUEUE_MAP_RND") == 0)
1140 pkt_dev
->flags
&= ~F_QUEUE_MAP_RND
;
1142 else if (strcmp(f
, "QUEUE_MAP_CPU") == 0)
1143 pkt_dev
->flags
|= F_QUEUE_MAP_CPU
;
1145 else if (strcmp(f
, "!QUEUE_MAP_CPU") == 0)
1146 pkt_dev
->flags
&= ~F_QUEUE_MAP_CPU
;
1148 else if (strcmp(f
, "IPSEC") == 0)
1149 pkt_dev
->flags
|= F_IPSEC_ON
;
1152 else if (strcmp(f
, "!IPV6") == 0)
1153 pkt_dev
->flags
&= ~F_IPV6
;
1157 "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
1159 "IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
1160 "MACSRC_RND, MACDST_RND, TXSIZE_RND, IPV6, MPLS_RND, VID_RND, SVID_RND, FLOW_SEQ, IPSEC\n");
1163 sprintf(pg_result
, "OK: flags=0x%x", pkt_dev
->flags
);
1166 if (!strcmp(name
, "dst_min") || !strcmp(name
, "dst")) {
1167 len
= strn_len(&user_buffer
[i
], sizeof(pkt_dev
->dst_min
) - 1);
1172 if (copy_from_user(buf
, &user_buffer
[i
], len
))
1175 if (strcmp(buf
, pkt_dev
->dst_min
) != 0) {
1176 memset(pkt_dev
->dst_min
, 0, sizeof(pkt_dev
->dst_min
));
1177 strncpy(pkt_dev
->dst_min
, buf
, len
);
1178 pkt_dev
->daddr_min
= in_aton(pkt_dev
->dst_min
);
1179 pkt_dev
->cur_daddr
= pkt_dev
->daddr_min
;
1182 printk(KERN_DEBUG
"pktgen: dst_min set to: %s\n",
1185 sprintf(pg_result
, "OK: dst_min=%s", pkt_dev
->dst_min
);
1188 if (!strcmp(name
, "dst_max")) {
1189 len
= strn_len(&user_buffer
[i
], sizeof(pkt_dev
->dst_max
) - 1);
1194 if (copy_from_user(buf
, &user_buffer
[i
], len
))
1198 if (strcmp(buf
, pkt_dev
->dst_max
) != 0) {
1199 memset(pkt_dev
->dst_max
, 0, sizeof(pkt_dev
->dst_max
));
1200 strncpy(pkt_dev
->dst_max
, buf
, len
);
1201 pkt_dev
->daddr_max
= in_aton(pkt_dev
->dst_max
);
1202 pkt_dev
->cur_daddr
= pkt_dev
->daddr_max
;
1205 printk(KERN_DEBUG
"pktgen: dst_max set to: %s\n",
1208 sprintf(pg_result
, "OK: dst_max=%s", pkt_dev
->dst_max
);
1211 if (!strcmp(name
, "dst6")) {
1212 len
= strn_len(&user_buffer
[i
], sizeof(buf
) - 1);
1216 pkt_dev
->flags
|= F_IPV6
;
1218 if (copy_from_user(buf
, &user_buffer
[i
], len
))
1222 scan_ip6(buf
, pkt_dev
->in6_daddr
.s6_addr
);
1223 fmt_ip6(buf
, pkt_dev
->in6_daddr
.s6_addr
);
1225 ipv6_addr_copy(&pkt_dev
->cur_in6_daddr
, &pkt_dev
->in6_daddr
);
1228 printk(KERN_DEBUG
"pktgen: dst6 set to: %s\n", buf
);
1231 sprintf(pg_result
, "OK: dst6=%s", buf
);
1234 if (!strcmp(name
, "dst6_min")) {
1235 len
= strn_len(&user_buffer
[i
], sizeof(buf
) - 1);
1239 pkt_dev
->flags
|= F_IPV6
;
1241 if (copy_from_user(buf
, &user_buffer
[i
], len
))
1245 scan_ip6(buf
, pkt_dev
->min_in6_daddr
.s6_addr
);
1246 fmt_ip6(buf
, pkt_dev
->min_in6_daddr
.s6_addr
);
1248 ipv6_addr_copy(&pkt_dev
->cur_in6_daddr
,
1249 &pkt_dev
->min_in6_daddr
);
1251 printk(KERN_DEBUG
"pktgen: dst6_min set to: %s\n", buf
);
1254 sprintf(pg_result
, "OK: dst6_min=%s", buf
);
1257 if (!strcmp(name
, "dst6_max")) {
1258 len
= strn_len(&user_buffer
[i
], sizeof(buf
) - 1);
1262 pkt_dev
->flags
|= F_IPV6
;
1264 if (copy_from_user(buf
, &user_buffer
[i
], len
))
1268 scan_ip6(buf
, pkt_dev
->max_in6_daddr
.s6_addr
);
1269 fmt_ip6(buf
, pkt_dev
->max_in6_daddr
.s6_addr
);
1272 printk(KERN_DEBUG
"pktgen: dst6_max set to: %s\n", buf
);
1275 sprintf(pg_result
, "OK: dst6_max=%s", buf
);
1278 if (!strcmp(name
, "src6")) {
1279 len
= strn_len(&user_buffer
[i
], sizeof(buf
) - 1);
1283 pkt_dev
->flags
|= F_IPV6
;
1285 if (copy_from_user(buf
, &user_buffer
[i
], len
))
1289 scan_ip6(buf
, pkt_dev
->in6_saddr
.s6_addr
);
1290 fmt_ip6(buf
, pkt_dev
->in6_saddr
.s6_addr
);
1292 ipv6_addr_copy(&pkt_dev
->cur_in6_saddr
, &pkt_dev
->in6_saddr
);
1295 printk(KERN_DEBUG
"pktgen: src6 set to: %s\n", buf
);
1298 sprintf(pg_result
, "OK: src6=%s", buf
);
1301 if (!strcmp(name
, "src_min")) {
1302 len
= strn_len(&user_buffer
[i
], sizeof(pkt_dev
->src_min
) - 1);
1306 if (copy_from_user(buf
, &user_buffer
[i
], len
))
1309 if (strcmp(buf
, pkt_dev
->src_min
) != 0) {
1310 memset(pkt_dev
->src_min
, 0, sizeof(pkt_dev
->src_min
));
1311 strncpy(pkt_dev
->src_min
, buf
, len
);
1312 pkt_dev
->saddr_min
= in_aton(pkt_dev
->src_min
);
1313 pkt_dev
->cur_saddr
= pkt_dev
->saddr_min
;
1316 printk(KERN_DEBUG
"pktgen: src_min set to: %s\n",
1319 sprintf(pg_result
, "OK: src_min=%s", pkt_dev
->src_min
);
1322 if (!strcmp(name
, "src_max")) {
1323 len
= strn_len(&user_buffer
[i
], sizeof(pkt_dev
->src_max
) - 1);
1327 if (copy_from_user(buf
, &user_buffer
[i
], len
))
1330 if (strcmp(buf
, pkt_dev
->src_max
) != 0) {
1331 memset(pkt_dev
->src_max
, 0, sizeof(pkt_dev
->src_max
));
1332 strncpy(pkt_dev
->src_max
, buf
, len
);
1333 pkt_dev
->saddr_max
= in_aton(pkt_dev
->src_max
);
1334 pkt_dev
->cur_saddr
= pkt_dev
->saddr_max
;
1337 printk(KERN_DEBUG
"pktgen: src_max set to: %s\n",
1340 sprintf(pg_result
, "OK: src_max=%s", pkt_dev
->src_max
);
1343 if (!strcmp(name
, "dst_mac")) {
1345 unsigned char old_dmac
[ETH_ALEN
];
1346 unsigned char *m
= pkt_dev
->dst_mac
;
1347 memcpy(old_dmac
, pkt_dev
->dst_mac
, ETH_ALEN
);
1349 len
= strn_len(&user_buffer
[i
], sizeof(valstr
) - 1);
1353 memset(valstr
, 0, sizeof(valstr
));
1354 if (copy_from_user(valstr
, &user_buffer
[i
], len
))
1358 for (*m
= 0; *v
&& m
< pkt_dev
->dst_mac
+ 6; v
++) {
1359 if (*v
>= '0' && *v
<= '9') {
1363 if (*v
>= 'A' && *v
<= 'F') {
1365 *m
+= *v
- 'A' + 10;
1367 if (*v
>= 'a' && *v
<= 'f') {
1369 *m
+= *v
- 'a' + 10;
1377 /* Set up Dest MAC */
1378 if (compare_ether_addr(old_dmac
, pkt_dev
->dst_mac
))
1379 memcpy(&(pkt_dev
->hh
[0]), pkt_dev
->dst_mac
, ETH_ALEN
);
1381 sprintf(pg_result
, "OK: dstmac");
1384 if (!strcmp(name
, "src_mac")) {
1386 unsigned char old_smac
[ETH_ALEN
];
1387 unsigned char *m
= pkt_dev
->src_mac
;
1389 memcpy(old_smac
, pkt_dev
->src_mac
, ETH_ALEN
);
1391 len
= strn_len(&user_buffer
[i
], sizeof(valstr
) - 1);
1395 memset(valstr
, 0, sizeof(valstr
));
1396 if (copy_from_user(valstr
, &user_buffer
[i
], len
))
1400 for (*m
= 0; *v
&& m
< pkt_dev
->src_mac
+ 6; v
++) {
1401 if (*v
>= '0' && *v
<= '9') {
1405 if (*v
>= 'A' && *v
<= 'F') {
1407 *m
+= *v
- 'A' + 10;
1409 if (*v
>= 'a' && *v
<= 'f') {
1411 *m
+= *v
- 'a' + 10;
1419 /* Set up Src MAC */
1420 if (compare_ether_addr(old_smac
, pkt_dev
->src_mac
))
1421 memcpy(&(pkt_dev
->hh
[6]), pkt_dev
->src_mac
, ETH_ALEN
);
1423 sprintf(pg_result
, "OK: srcmac");
1427 if (!strcmp(name
, "clear_counters")) {
1428 pktgen_clear_counters(pkt_dev
);
1429 sprintf(pg_result
, "OK: Clearing counters.\n");
1433 if (!strcmp(name
, "flows")) {
1434 len
= num_arg(&user_buffer
[i
], 10, &value
);
1439 if (value
> MAX_CFLOWS
)
1442 pkt_dev
->cflows
= value
;
1443 sprintf(pg_result
, "OK: flows=%u", pkt_dev
->cflows
);
1447 if (!strcmp(name
, "flowlen")) {
1448 len
= num_arg(&user_buffer
[i
], 10, &value
);
1453 pkt_dev
->lflow
= value
;
1454 sprintf(pg_result
, "OK: flowlen=%u", pkt_dev
->lflow
);
1458 if (!strcmp(name
, "queue_map_min")) {
1459 len
= num_arg(&user_buffer
[i
], 5, &value
);
1464 pkt_dev
->queue_map_min
= value
;
1465 sprintf(pg_result
, "OK: queue_map_min=%u", pkt_dev
->queue_map_min
);
1469 if (!strcmp(name
, "queue_map_max")) {
1470 len
= num_arg(&user_buffer
[i
], 5, &value
);
1475 pkt_dev
->queue_map_max
= value
;
1476 sprintf(pg_result
, "OK: queue_map_max=%u", pkt_dev
->queue_map_max
);
1480 if (!strcmp(name
, "mpls")) {
1483 len
= get_labels(&user_buffer
[i
], pkt_dev
);
1487 cnt
= sprintf(pg_result
, "OK: mpls=");
1488 for (n
= 0; n
< pkt_dev
->nr_labels
; n
++)
1489 cnt
+= sprintf(pg_result
+ cnt
,
1490 "%08x%s", ntohl(pkt_dev
->labels
[n
]),
1491 n
== pkt_dev
->nr_labels
-1 ? "" : ",");
1493 if (pkt_dev
->nr_labels
&& pkt_dev
->vlan_id
!= 0xffff) {
1494 pkt_dev
->vlan_id
= 0xffff; /* turn off VLAN/SVLAN */
1495 pkt_dev
->svlan_id
= 0xffff;
1498 printk(KERN_DEBUG
"pktgen: VLAN/SVLAN auto turned off\n");
1503 if (!strcmp(name
, "vlan_id")) {
1504 len
= num_arg(&user_buffer
[i
], 4, &value
);
1509 if (value
<= 4095) {
1510 pkt_dev
->vlan_id
= value
; /* turn on VLAN */
1513 printk(KERN_DEBUG
"pktgen: VLAN turned on\n");
1515 if (debug
&& pkt_dev
->nr_labels
)
1516 printk(KERN_DEBUG
"pktgen: MPLS auto turned off\n");
1518 pkt_dev
->nr_labels
= 0; /* turn off MPLS */
1519 sprintf(pg_result
, "OK: vlan_id=%u", pkt_dev
->vlan_id
);
1521 pkt_dev
->vlan_id
= 0xffff; /* turn off VLAN/SVLAN */
1522 pkt_dev
->svlan_id
= 0xffff;
1525 printk(KERN_DEBUG
"pktgen: VLAN/SVLAN turned off\n");
1530 if (!strcmp(name
, "vlan_p")) {
1531 len
= num_arg(&user_buffer
[i
], 1, &value
);
1536 if ((value
<= 7) && (pkt_dev
->vlan_id
!= 0xffff)) {
1537 pkt_dev
->vlan_p
= value
;
1538 sprintf(pg_result
, "OK: vlan_p=%u", pkt_dev
->vlan_p
);
1540 sprintf(pg_result
, "ERROR: vlan_p must be 0-7");
1545 if (!strcmp(name
, "vlan_cfi")) {
1546 len
= num_arg(&user_buffer
[i
], 1, &value
);
1551 if ((value
<= 1) && (pkt_dev
->vlan_id
!= 0xffff)) {
1552 pkt_dev
->vlan_cfi
= value
;
1553 sprintf(pg_result
, "OK: vlan_cfi=%u", pkt_dev
->vlan_cfi
);
1555 sprintf(pg_result
, "ERROR: vlan_cfi must be 0-1");
1560 if (!strcmp(name
, "svlan_id")) {
1561 len
= num_arg(&user_buffer
[i
], 4, &value
);
1566 if ((value
<= 4095) && ((pkt_dev
->vlan_id
!= 0xffff))) {
1567 pkt_dev
->svlan_id
= value
; /* turn on SVLAN */
1570 printk(KERN_DEBUG
"pktgen: SVLAN turned on\n");
1572 if (debug
&& pkt_dev
->nr_labels
)
1573 printk(KERN_DEBUG
"pktgen: MPLS auto turned off\n");
1575 pkt_dev
->nr_labels
= 0; /* turn off MPLS */
1576 sprintf(pg_result
, "OK: svlan_id=%u", pkt_dev
->svlan_id
);
1578 pkt_dev
->vlan_id
= 0xffff; /* turn off VLAN/SVLAN */
1579 pkt_dev
->svlan_id
= 0xffff;
1582 printk(KERN_DEBUG
"pktgen: VLAN/SVLAN turned off\n");
1587 if (!strcmp(name
, "svlan_p")) {
1588 len
= num_arg(&user_buffer
[i
], 1, &value
);
1593 if ((value
<= 7) && (pkt_dev
->svlan_id
!= 0xffff)) {
1594 pkt_dev
->svlan_p
= value
;
1595 sprintf(pg_result
, "OK: svlan_p=%u", pkt_dev
->svlan_p
);
1597 sprintf(pg_result
, "ERROR: svlan_p must be 0-7");
1602 if (!strcmp(name
, "svlan_cfi")) {
1603 len
= num_arg(&user_buffer
[i
], 1, &value
);
1608 if ((value
<= 1) && (pkt_dev
->svlan_id
!= 0xffff)) {
1609 pkt_dev
->svlan_cfi
= value
;
1610 sprintf(pg_result
, "OK: svlan_cfi=%u", pkt_dev
->svlan_cfi
);
1612 sprintf(pg_result
, "ERROR: svlan_cfi must be 0-1");
1617 if (!strcmp(name
, "tos")) {
1618 __u32 tmp_value
= 0;
1619 len
= hex32_arg(&user_buffer
[i
], 2, &tmp_value
);
1625 pkt_dev
->tos
= tmp_value
;
1626 sprintf(pg_result
, "OK: tos=0x%02x", pkt_dev
->tos
);
1628 sprintf(pg_result
, "ERROR: tos must be 00-ff");
1633 if (!strcmp(name
, "traffic_class")) {
1634 __u32 tmp_value
= 0;
1635 len
= hex32_arg(&user_buffer
[i
], 2, &tmp_value
);
1641 pkt_dev
->traffic_class
= tmp_value
;
1642 sprintf(pg_result
, "OK: traffic_class=0x%02x", pkt_dev
->traffic_class
);
1644 sprintf(pg_result
, "ERROR: traffic_class must be 00-ff");
1649 sprintf(pkt_dev
->result
, "No such parameter \"%s\"", name
);
1653 static int pktgen_if_open(struct inode
*inode
, struct file
*file
)
1655 return single_open(file
, pktgen_if_show
, PDE(inode
)->data
);
1658 static const struct file_operations pktgen_if_fops
= {
1659 .owner
= THIS_MODULE
,
1660 .open
= pktgen_if_open
,
1662 .llseek
= seq_lseek
,
1663 .write
= pktgen_if_write
,
1664 .release
= single_release
,
1667 static int pktgen_thread_show(struct seq_file
*seq
, void *v
)
1669 struct pktgen_thread
*t
= seq
->private;
1670 struct pktgen_dev
*pkt_dev
;
1674 seq_printf(seq
, "Running: ");
1677 list_for_each_entry(pkt_dev
, &t
->if_list
, list
)
1678 if (pkt_dev
->running
)
1679 seq_printf(seq
, "%s ", pkt_dev
->odev
->name
);
1681 seq_printf(seq
, "\nStopped: ");
1683 list_for_each_entry(pkt_dev
, &t
->if_list
, list
)
1684 if (!pkt_dev
->running
)
1685 seq_printf(seq
, "%s ", pkt_dev
->odev
->name
);
1688 seq_printf(seq
, "\nResult: %s\n", t
->result
);
1690 seq_printf(seq
, "\nResult: NA\n");
1697 static ssize_t
pktgen_thread_write(struct file
*file
,
1698 const char __user
* user_buffer
,
1699 size_t count
, loff_t
* offset
)
1701 struct seq_file
*seq
= (struct seq_file
*)file
->private_data
;
1702 struct pktgen_thread
*t
= seq
->private;
1703 int i
= 0, max
, len
, ret
;
1708 // sprintf(pg_result, "Wrong command format");
1713 len
= count_trail_chars(&user_buffer
[i
], max
);
1719 /* Read variable name */
1721 len
= strn_len(&user_buffer
[i
], sizeof(name
) - 1);
1725 memset(name
, 0, sizeof(name
));
1726 if (copy_from_user(name
, &user_buffer
[i
], len
))
1731 len
= count_trail_chars(&user_buffer
[i
], max
);
1738 printk(KERN_DEBUG
"pktgen: t=%s, count=%lu\n",
1739 name
, (unsigned long)count
);
1742 printk(KERN_ERR
"pktgen: ERROR: No thread\n");
1747 pg_result
= &(t
->result
[0]);
1749 if (!strcmp(name
, "add_device")) {
1752 len
= strn_len(&user_buffer
[i
], sizeof(f
) - 1);
1757 if (copy_from_user(f
, &user_buffer
[i
], len
))
1760 mutex_lock(&pktgen_thread_lock
);
1761 pktgen_add_device(t
, f
);
1762 mutex_unlock(&pktgen_thread_lock
);
1764 sprintf(pg_result
, "OK: add_device=%s", f
);
1768 if (!strcmp(name
, "rem_device_all")) {
1769 mutex_lock(&pktgen_thread_lock
);
1770 t
->control
|= T_REMDEVALL
;
1771 mutex_unlock(&pktgen_thread_lock
);
1772 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
1774 sprintf(pg_result
, "OK: rem_device_all");
1778 if (!strcmp(name
, "max_before_softirq")) {
1779 sprintf(pg_result
, "OK: Note! max_before_softirq is obsoleted -- Do not use");
1789 static int pktgen_thread_open(struct inode
*inode
, struct file
*file
)
1791 return single_open(file
, pktgen_thread_show
, PDE(inode
)->data
);
1794 static const struct file_operations pktgen_thread_fops
= {
1795 .owner
= THIS_MODULE
,
1796 .open
= pktgen_thread_open
,
1798 .llseek
= seq_lseek
,
1799 .write
= pktgen_thread_write
,
1800 .release
= single_release
,
1803 /* Think find or remove for NN */
1804 static struct pktgen_dev
*__pktgen_NN_threads(const char *ifname
, int remove
)
1806 struct pktgen_thread
*t
;
1807 struct pktgen_dev
*pkt_dev
= NULL
;
1809 list_for_each_entry(t
, &pktgen_threads
, th_list
) {
1810 pkt_dev
= pktgen_find_dev(t
, ifname
);
1814 pkt_dev
->removal_mark
= 1;
1815 t
->control
|= T_REMDEV
;
1825 * mark a device for removal
1827 static void pktgen_mark_device(const char *ifname
)
1829 struct pktgen_dev
*pkt_dev
= NULL
;
1830 const int max_tries
= 10, msec_per_try
= 125;
1833 mutex_lock(&pktgen_thread_lock
);
1834 pr_debug("pktgen: pktgen_mark_device marking %s for removal\n", ifname
);
1838 pkt_dev
= __pktgen_NN_threads(ifname
, REMOVE
);
1839 if (pkt_dev
== NULL
)
1840 break; /* success */
1842 mutex_unlock(&pktgen_thread_lock
);
1843 pr_debug("pktgen: pktgen_mark_device waiting for %s "
1844 "to disappear....\n", ifname
);
1845 schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try
));
1846 mutex_lock(&pktgen_thread_lock
);
1848 if (++i
>= max_tries
) {
1849 printk(KERN_ERR
"pktgen_mark_device: timed out after "
1850 "waiting %d msec for device %s to be removed\n",
1851 msec_per_try
* i
, ifname
);
1857 mutex_unlock(&pktgen_thread_lock
);
1860 static void pktgen_change_name(struct net_device
*dev
)
1862 struct pktgen_thread
*t
;
1864 list_for_each_entry(t
, &pktgen_threads
, th_list
) {
1865 struct pktgen_dev
*pkt_dev
;
1867 list_for_each_entry(pkt_dev
, &t
->if_list
, list
) {
1868 if (pkt_dev
->odev
!= dev
)
1871 remove_proc_entry(pkt_dev
->entry
->name
, pg_proc_dir
);
1873 pkt_dev
->entry
= create_proc_entry(dev
->name
, 0600,
1875 if (!pkt_dev
->entry
)
1876 printk(KERN_ERR
"pktgen: can't move proc "
1877 " entry for '%s'\n", dev
->name
);
1883 static int pktgen_device_event(struct notifier_block
*unused
,
1884 unsigned long event
, void *ptr
)
1886 struct net_device
*dev
= ptr
;
1888 if (!net_eq(dev_net(dev
), &init_net
))
1891 /* It is OK that we do not hold the group lock right now,
1892 * as we run under the RTNL lock.
1896 case NETDEV_CHANGENAME
:
1897 pktgen_change_name(dev
);
1900 case NETDEV_UNREGISTER
:
1901 pktgen_mark_device(dev
->name
);
1908 static struct net_device
*pktgen_dev_get_by_name(struct pktgen_dev
*pkt_dev
, const char *ifname
)
1913 for(i
=0; ifname
[i
] != '@'; i
++) {
1921 return dev_get_by_name(&init_net
, b
);
1925 /* Associate pktgen_dev with a device. */
1927 static int pktgen_setup_dev(struct pktgen_dev
*pkt_dev
, const char *ifname
)
1929 struct net_device
*odev
;
1932 /* Clean old setups */
1933 if (pkt_dev
->odev
) {
1934 dev_put(pkt_dev
->odev
);
1935 pkt_dev
->odev
= NULL
;
1938 odev
= pktgen_dev_get_by_name(pkt_dev
, ifname
);
1940 printk(KERN_ERR
"pktgen: no such netdevice: \"%s\"\n", ifname
);
1944 if (odev
->type
!= ARPHRD_ETHER
) {
1945 printk(KERN_ERR
"pktgen: not an ethernet device: \"%s\"\n", ifname
);
1947 } else if (!netif_running(odev
)) {
1948 printk(KERN_ERR
"pktgen: device is down: \"%s\"\n", ifname
);
1951 pkt_dev
->odev
= odev
;
1959 /* Read pkt_dev from the interface and set up internal pktgen_dev
1960 * structure to have the right information to create/send packets
1962 static void pktgen_setup_inject(struct pktgen_dev
*pkt_dev
)
1966 if (!pkt_dev
->odev
) {
1967 printk(KERN_ERR
"pktgen: ERROR: pkt_dev->odev == NULL in "
1969 sprintf(pkt_dev
->result
,
1970 "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
1974 /* make sure that we don't pick a non-existing transmit queue */
1975 ntxq
= pkt_dev
->odev
->real_num_tx_queues
;
1977 if (ntxq
<= pkt_dev
->queue_map_min
) {
1978 printk(KERN_WARNING
"pktgen: WARNING: Requested "
1979 "queue_map_min (zero-based) (%d) exceeds valid range "
1980 "[0 - %d] for (%d) queues on %s, resetting\n",
1981 pkt_dev
->queue_map_min
, (ntxq
?: 1)- 1, ntxq
,
1982 pkt_dev
->odev
->name
);
1983 pkt_dev
->queue_map_min
= ntxq
- 1;
1985 if (pkt_dev
->queue_map_max
>= ntxq
) {
1986 printk(KERN_WARNING
"pktgen: WARNING: Requested "
1987 "queue_map_max (zero-based) (%d) exceeds valid range "
1988 "[0 - %d] for (%d) queues on %s, resetting\n",
1989 pkt_dev
->queue_map_max
, (ntxq
?: 1)- 1, ntxq
,
1990 pkt_dev
->odev
->name
);
1991 pkt_dev
->queue_map_max
= ntxq
- 1;
1994 /* Default to the interface's mac if not explicitly set. */
1996 if (is_zero_ether_addr(pkt_dev
->src_mac
))
1997 memcpy(&(pkt_dev
->hh
[6]), pkt_dev
->odev
->dev_addr
, ETH_ALEN
);
1999 /* Set up Dest MAC */
2000 memcpy(&(pkt_dev
->hh
[0]), pkt_dev
->dst_mac
, ETH_ALEN
);
2002 /* Set up pkt size */
2003 pkt_dev
->cur_pkt_size
= pkt_dev
->min_pkt_size
;
2005 if (pkt_dev
->flags
& F_IPV6
) {
2007 * Skip this automatic address setting until locks or functions
2012 int i
, set
= 0, err
= 1;
2013 struct inet6_dev
*idev
;
2015 for (i
= 0; i
< IN6_ADDR_HSIZE
; i
++)
2016 if (pkt_dev
->cur_in6_saddr
.s6_addr
[i
]) {
2024 * Use linklevel address if unconfigured.
2026 * use ipv6_get_lladdr if/when it's get exported
2030 if ((idev
= __in6_dev_get(pkt_dev
->odev
)) != NULL
) {
2031 struct inet6_ifaddr
*ifp
;
2033 read_lock_bh(&idev
->lock
);
2034 for (ifp
= idev
->addr_list
; ifp
;
2035 ifp
= ifp
->if_next
) {
2036 if (ifp
->scope
== IFA_LINK
2038 flags
& IFA_F_TENTATIVE
)) {
2039 ipv6_addr_copy(&pkt_dev
->
2046 read_unlock_bh(&idev
->lock
);
2050 printk(KERN_ERR
"pktgen: ERROR: IPv6 link "
2051 "address not availble.\n");
2055 pkt_dev
->saddr_min
= 0;
2056 pkt_dev
->saddr_max
= 0;
2057 if (strlen(pkt_dev
->src_min
) == 0) {
2059 struct in_device
*in_dev
;
2062 in_dev
= __in_dev_get_rcu(pkt_dev
->odev
);
2064 if (in_dev
->ifa_list
) {
2065 pkt_dev
->saddr_min
=
2066 in_dev
->ifa_list
->ifa_address
;
2067 pkt_dev
->saddr_max
= pkt_dev
->saddr_min
;
2072 pkt_dev
->saddr_min
= in_aton(pkt_dev
->src_min
);
2073 pkt_dev
->saddr_max
= in_aton(pkt_dev
->src_max
);
2076 pkt_dev
->daddr_min
= in_aton(pkt_dev
->dst_min
);
2077 pkt_dev
->daddr_max
= in_aton(pkt_dev
->dst_max
);
2079 /* Initialize current values. */
2080 pkt_dev
->cur_dst_mac_offset
= 0;
2081 pkt_dev
->cur_src_mac_offset
= 0;
2082 pkt_dev
->cur_saddr
= pkt_dev
->saddr_min
;
2083 pkt_dev
->cur_daddr
= pkt_dev
->daddr_min
;
2084 pkt_dev
->cur_udp_dst
= pkt_dev
->udp_dst_min
;
2085 pkt_dev
->cur_udp_src
= pkt_dev
->udp_src_min
;
2086 pkt_dev
->nflows
= 0;
2089 static void spin(struct pktgen_dev
*pkt_dev
, __u64 spin_until_us
)
2094 start
= now
= getCurUs();
2095 while (now
< spin_until_us
) {
2096 /* TODO: optimize sleeping behavior */
2097 if (spin_until_us
- now
> jiffies_to_usecs(1) + 1)
2098 schedule_timeout_interruptible(1);
2099 else if (spin_until_us
- now
> 100) {
2100 if (!pkt_dev
->running
)
2109 pkt_dev
->idle_acc
+= now
- start
;
2112 static inline void set_pkt_overhead(struct pktgen_dev
*pkt_dev
)
2114 pkt_dev
->pkt_overhead
= 0;
2115 pkt_dev
->pkt_overhead
+= pkt_dev
->nr_labels
*sizeof(u32
);
2116 pkt_dev
->pkt_overhead
+= VLAN_TAG_SIZE(pkt_dev
);
2117 pkt_dev
->pkt_overhead
+= SVLAN_TAG_SIZE(pkt_dev
);
2120 static inline int f_seen(struct pktgen_dev
*pkt_dev
, int flow
)
2123 if (pkt_dev
->flows
[flow
].flags
& F_INIT
)
2129 static inline int f_pick(struct pktgen_dev
*pkt_dev
)
2131 int flow
= pkt_dev
->curfl
;
2133 if (pkt_dev
->flags
& F_FLOW_SEQ
) {
2134 if (pkt_dev
->flows
[flow
].count
>= pkt_dev
->lflow
) {
2136 pkt_dev
->flows
[flow
].count
= 0;
2137 pkt_dev
->flows
[flow
].flags
= 0;
2138 pkt_dev
->curfl
+= 1;
2139 if (pkt_dev
->curfl
>= pkt_dev
->cflows
)
2140 pkt_dev
->curfl
= 0; /*reset */
2143 flow
= random32() % pkt_dev
->cflows
;
2144 pkt_dev
->curfl
= flow
;
2146 if (pkt_dev
->flows
[flow
].count
> pkt_dev
->lflow
) {
2147 pkt_dev
->flows
[flow
].count
= 0;
2148 pkt_dev
->flows
[flow
].flags
= 0;
2152 return pkt_dev
->curfl
;
2157 /* If there was already an IPSEC SA, we keep it as is, else
2158 * we go look for it ...
2160 static void get_ipsec_sa(struct pktgen_dev
*pkt_dev
, int flow
)
2162 struct xfrm_state
*x
= pkt_dev
->flows
[flow
].x
;
2164 /*slow path: we dont already have xfrm_state*/
2165 x
= xfrm_stateonly_find((xfrm_address_t
*)&pkt_dev
->cur_daddr
,
2166 (xfrm_address_t
*)&pkt_dev
->cur_saddr
,
2169 pkt_dev
->ipsproto
, 0);
2171 pkt_dev
->flows
[flow
].x
= x
;
2172 set_pkt_overhead(pkt_dev
);
2173 pkt_dev
->pkt_overhead
+=x
->props
.header_len
;
2179 static void set_cur_queue_map(struct pktgen_dev
*pkt_dev
)
2182 if (pkt_dev
->flags
& F_QUEUE_MAP_CPU
)
2183 pkt_dev
->cur_queue_map
= smp_processor_id();
2185 else if (pkt_dev
->queue_map_min
< pkt_dev
->queue_map_max
) {
2187 if (pkt_dev
->flags
& F_QUEUE_MAP_RND
) {
2189 (pkt_dev
->queue_map_max
-
2190 pkt_dev
->queue_map_min
+ 1)
2191 + pkt_dev
->queue_map_min
;
2193 t
= pkt_dev
->cur_queue_map
+ 1;
2194 if (t
> pkt_dev
->queue_map_max
)
2195 t
= pkt_dev
->queue_map_min
;
2197 pkt_dev
->cur_queue_map
= t
;
2199 pkt_dev
->cur_queue_map
= pkt_dev
->cur_queue_map
% pkt_dev
->odev
->real_num_tx_queues
;
2202 /* Increment/randomize headers according to flags and current values
2203 * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
2205 static void mod_cur_headers(struct pktgen_dev
*pkt_dev
)
2211 if (pkt_dev
->cflows
)
2212 flow
= f_pick(pkt_dev
);
2214 /* Deal with source MAC */
2215 if (pkt_dev
->src_mac_count
> 1) {
2219 if (pkt_dev
->flags
& F_MACSRC_RND
)
2220 mc
= random32() % pkt_dev
->src_mac_count
;
2222 mc
= pkt_dev
->cur_src_mac_offset
++;
2223 if (pkt_dev
->cur_src_mac_offset
>=
2224 pkt_dev
->src_mac_count
)
2225 pkt_dev
->cur_src_mac_offset
= 0;
2228 tmp
= pkt_dev
->src_mac
[5] + (mc
& 0xFF);
2229 pkt_dev
->hh
[11] = tmp
;
2230 tmp
= (pkt_dev
->src_mac
[4] + ((mc
>> 8) & 0xFF) + (tmp
>> 8));
2231 pkt_dev
->hh
[10] = tmp
;
2232 tmp
= (pkt_dev
->src_mac
[3] + ((mc
>> 16) & 0xFF) + (tmp
>> 8));
2233 pkt_dev
->hh
[9] = tmp
;
2234 tmp
= (pkt_dev
->src_mac
[2] + ((mc
>> 24) & 0xFF) + (tmp
>> 8));
2235 pkt_dev
->hh
[8] = tmp
;
2236 tmp
= (pkt_dev
->src_mac
[1] + (tmp
>> 8));
2237 pkt_dev
->hh
[7] = tmp
;
2240 /* Deal with Destination MAC */
2241 if (pkt_dev
->dst_mac_count
> 1) {
2245 if (pkt_dev
->flags
& F_MACDST_RND
)
2246 mc
= random32() % pkt_dev
->dst_mac_count
;
2249 mc
= pkt_dev
->cur_dst_mac_offset
++;
2250 if (pkt_dev
->cur_dst_mac_offset
>=
2251 pkt_dev
->dst_mac_count
) {
2252 pkt_dev
->cur_dst_mac_offset
= 0;
2256 tmp
= pkt_dev
->dst_mac
[5] + (mc
& 0xFF);
2257 pkt_dev
->hh
[5] = tmp
;
2258 tmp
= (pkt_dev
->dst_mac
[4] + ((mc
>> 8) & 0xFF) + (tmp
>> 8));
2259 pkt_dev
->hh
[4] = tmp
;
2260 tmp
= (pkt_dev
->dst_mac
[3] + ((mc
>> 16) & 0xFF) + (tmp
>> 8));
2261 pkt_dev
->hh
[3] = tmp
;
2262 tmp
= (pkt_dev
->dst_mac
[2] + ((mc
>> 24) & 0xFF) + (tmp
>> 8));
2263 pkt_dev
->hh
[2] = tmp
;
2264 tmp
= (pkt_dev
->dst_mac
[1] + (tmp
>> 8));
2265 pkt_dev
->hh
[1] = tmp
;
2268 if (pkt_dev
->flags
& F_MPLS_RND
) {
2270 for (i
= 0; i
< pkt_dev
->nr_labels
; i
++)
2271 if (pkt_dev
->labels
[i
] & MPLS_STACK_BOTTOM
)
2272 pkt_dev
->labels
[i
] = MPLS_STACK_BOTTOM
|
2273 ((__force __be32
)random32() &
2277 if ((pkt_dev
->flags
& F_VID_RND
) && (pkt_dev
->vlan_id
!= 0xffff)) {
2278 pkt_dev
->vlan_id
= random32() & (4096-1);
2281 if ((pkt_dev
->flags
& F_SVID_RND
) && (pkt_dev
->svlan_id
!= 0xffff)) {
2282 pkt_dev
->svlan_id
= random32() & (4096 - 1);
2285 if (pkt_dev
->udp_src_min
< pkt_dev
->udp_src_max
) {
2286 if (pkt_dev
->flags
& F_UDPSRC_RND
)
2287 pkt_dev
->cur_udp_src
= random32() %
2288 (pkt_dev
->udp_src_max
- pkt_dev
->udp_src_min
)
2289 + pkt_dev
->udp_src_min
;
2292 pkt_dev
->cur_udp_src
++;
2293 if (pkt_dev
->cur_udp_src
>= pkt_dev
->udp_src_max
)
2294 pkt_dev
->cur_udp_src
= pkt_dev
->udp_src_min
;
2298 if (pkt_dev
->udp_dst_min
< pkt_dev
->udp_dst_max
) {
2299 if (pkt_dev
->flags
& F_UDPDST_RND
) {
2300 pkt_dev
->cur_udp_dst
= random32() %
2301 (pkt_dev
->udp_dst_max
- pkt_dev
->udp_dst_min
)
2302 + pkt_dev
->udp_dst_min
;
2304 pkt_dev
->cur_udp_dst
++;
2305 if (pkt_dev
->cur_udp_dst
>= pkt_dev
->udp_dst_max
)
2306 pkt_dev
->cur_udp_dst
= pkt_dev
->udp_dst_min
;
2310 if (!(pkt_dev
->flags
& F_IPV6
)) {
2312 if ((imn
= ntohl(pkt_dev
->saddr_min
)) < (imx
=
2316 if (pkt_dev
->flags
& F_IPSRC_RND
)
2317 t
= random32() % (imx
- imn
) + imn
;
2319 t
= ntohl(pkt_dev
->cur_saddr
);
2325 pkt_dev
->cur_saddr
= htonl(t
);
2328 if (pkt_dev
->cflows
&& f_seen(pkt_dev
, flow
)) {
2329 pkt_dev
->cur_daddr
= pkt_dev
->flows
[flow
].cur_daddr
;
2331 imn
= ntohl(pkt_dev
->daddr_min
);
2332 imx
= ntohl(pkt_dev
->daddr_max
);
2336 if (pkt_dev
->flags
& F_IPDST_RND
) {
2338 t
= random32() % (imx
- imn
) + imn
;
2341 while (ipv4_is_loopback(s
) ||
2342 ipv4_is_multicast(s
) ||
2343 ipv4_is_lbcast(s
) ||
2344 ipv4_is_zeronet(s
) ||
2345 ipv4_is_local_multicast(s
)) {
2346 t
= random32() % (imx
- imn
) + imn
;
2349 pkt_dev
->cur_daddr
= s
;
2351 t
= ntohl(pkt_dev
->cur_daddr
);
2356 pkt_dev
->cur_daddr
= htonl(t
);
2359 if (pkt_dev
->cflows
) {
2360 pkt_dev
->flows
[flow
].flags
|= F_INIT
;
2361 pkt_dev
->flows
[flow
].cur_daddr
=
2364 if (pkt_dev
->flags
& F_IPSEC_ON
)
2365 get_ipsec_sa(pkt_dev
, flow
);
2370 } else { /* IPV6 * */
2372 if (pkt_dev
->min_in6_daddr
.s6_addr32
[0] == 0 &&
2373 pkt_dev
->min_in6_daddr
.s6_addr32
[1] == 0 &&
2374 pkt_dev
->min_in6_daddr
.s6_addr32
[2] == 0 &&
2375 pkt_dev
->min_in6_daddr
.s6_addr32
[3] == 0) ;
2379 /* Only random destinations yet */
2381 for (i
= 0; i
< 4; i
++) {
2382 pkt_dev
->cur_in6_daddr
.s6_addr32
[i
] =
2383 (((__force __be32
)random32() |
2384 pkt_dev
->min_in6_daddr
.s6_addr32
[i
]) &
2385 pkt_dev
->max_in6_daddr
.s6_addr32
[i
]);
2390 if (pkt_dev
->min_pkt_size
< pkt_dev
->max_pkt_size
) {
2392 if (pkt_dev
->flags
& F_TXSIZE_RND
) {
2394 (pkt_dev
->max_pkt_size
- pkt_dev
->min_pkt_size
)
2395 + pkt_dev
->min_pkt_size
;
2397 t
= pkt_dev
->cur_pkt_size
+ 1;
2398 if (t
> pkt_dev
->max_pkt_size
)
2399 t
= pkt_dev
->min_pkt_size
;
2401 pkt_dev
->cur_pkt_size
= t
;
2404 set_cur_queue_map(pkt_dev
);
2406 pkt_dev
->flows
[flow
].count
++;
2411 static int pktgen_output_ipsec(struct sk_buff
*skb
, struct pktgen_dev
*pkt_dev
)
2413 struct xfrm_state
*x
= pkt_dev
->flows
[pkt_dev
->curfl
].x
;
2419 /* XXX: we dont support tunnel mode for now until
2420 * we resolve the dst issue */
2421 if (x
->props
.mode
!= XFRM_MODE_TRANSPORT
)
2424 spin_lock(&x
->lock
);
2427 err
= x
->outer_mode
->output(x
, skb
);
2430 err
= x
->type
->output(x
, skb
);
2434 x
->curlft
.bytes
+=skb
->len
;
2435 x
->curlft
.packets
++;
2437 spin_unlock(&x
->lock
);
2441 static inline void free_SAs(struct pktgen_dev
*pkt_dev
)
2443 if (pkt_dev
->cflows
) {
2444 /* let go of the SAs if we have them */
2446 for (; i
< pkt_dev
->nflows
; i
++){
2447 struct xfrm_state
*x
= pkt_dev
->flows
[i
].x
;
2450 pkt_dev
->flows
[i
].x
= NULL
;
2456 static inline int process_ipsec(struct pktgen_dev
*pkt_dev
,
2457 struct sk_buff
*skb
, __be16 protocol
)
2459 if (pkt_dev
->flags
& F_IPSEC_ON
) {
2460 struct xfrm_state
*x
= pkt_dev
->flows
[pkt_dev
->curfl
].x
;
2465 nhead
= x
->props
.header_len
- skb_headroom(skb
);
2467 ret
= pskb_expand_head(skb
, nhead
, 0, GFP_ATOMIC
);
2469 printk(KERN_ERR
"Error expanding "
2470 "ipsec packet %d\n",ret
);
2475 /* ipsec is not expecting ll header */
2476 skb_pull(skb
, ETH_HLEN
);
2477 ret
= pktgen_output_ipsec(skb
, pkt_dev
);
2479 printk(KERN_ERR
"Error creating ipsec "
2484 eth
= (__u8
*) skb_push(skb
, ETH_HLEN
);
2485 memcpy(eth
, pkt_dev
->hh
, 12);
2486 *(u16
*) & eth
[12] = protocol
;
2496 static void mpls_push(__be32
*mpls
, struct pktgen_dev
*pkt_dev
)
2499 for (i
= 0; i
< pkt_dev
->nr_labels
; i
++) {
2500 *mpls
++ = pkt_dev
->labels
[i
] & ~MPLS_STACK_BOTTOM
;
2503 *mpls
|= MPLS_STACK_BOTTOM
;
2506 static inline __be16
build_tci(unsigned int id
, unsigned int cfi
,
2509 return htons(id
| (cfi
<< 12) | (prio
<< 13));
2512 static struct sk_buff
*fill_packet_ipv4(struct net_device
*odev
,
2513 struct pktgen_dev
*pkt_dev
)
2515 struct sk_buff
*skb
= NULL
;
2517 struct udphdr
*udph
;
2520 struct pktgen_hdr
*pgh
= NULL
;
2521 __be16 protocol
= htons(ETH_P_IP
);
2523 __be16
*vlan_tci
= NULL
; /* Encapsulates priority and VLAN ID */
2524 __be16
*vlan_encapsulated_proto
= NULL
; /* packet type ID field (or len) for VLAN tag */
2525 __be16
*svlan_tci
= NULL
; /* Encapsulates priority and SVLAN ID */
2526 __be16
*svlan_encapsulated_proto
= NULL
; /* packet type ID field (or len) for SVLAN tag */
2529 if (pkt_dev
->nr_labels
)
2530 protocol
= htons(ETH_P_MPLS_UC
);
2532 if (pkt_dev
->vlan_id
!= 0xffff)
2533 protocol
= htons(ETH_P_8021Q
);
2535 /* Update any of the values, used when we're incrementing various
2538 queue_map
= pkt_dev
->cur_queue_map
;
2539 mod_cur_headers(pkt_dev
);
2541 datalen
= (odev
->hard_header_len
+ 16) & ~0xf;
2542 skb
= alloc_skb(pkt_dev
->cur_pkt_size
+ 64 + datalen
+
2543 pkt_dev
->pkt_overhead
, GFP_ATOMIC
);
2545 sprintf(pkt_dev
->result
, "No memory");
2549 skb_reserve(skb
, datalen
);
2551 /* Reserve for ethernet and IP header */
2552 eth
= (__u8
*) skb_push(skb
, 14);
2553 mpls
= (__be32
*)skb_put(skb
, pkt_dev
->nr_labels
*sizeof(__u32
));
2554 if (pkt_dev
->nr_labels
)
2555 mpls_push(mpls
, pkt_dev
);
2557 if (pkt_dev
->vlan_id
!= 0xffff) {
2558 if (pkt_dev
->svlan_id
!= 0xffff) {
2559 svlan_tci
= (__be16
*)skb_put(skb
, sizeof(__be16
));
2560 *svlan_tci
= build_tci(pkt_dev
->svlan_id
,
2563 svlan_encapsulated_proto
= (__be16
*)skb_put(skb
, sizeof(__be16
));
2564 *svlan_encapsulated_proto
= htons(ETH_P_8021Q
);
2566 vlan_tci
= (__be16
*)skb_put(skb
, sizeof(__be16
));
2567 *vlan_tci
= build_tci(pkt_dev
->vlan_id
,
2570 vlan_encapsulated_proto
= (__be16
*)skb_put(skb
, sizeof(__be16
));
2571 *vlan_encapsulated_proto
= htons(ETH_P_IP
);
2574 skb
->network_header
= skb
->tail
;
2575 skb
->transport_header
= skb
->network_header
+ sizeof(struct iphdr
);
2576 skb_put(skb
, sizeof(struct iphdr
) + sizeof(struct udphdr
));
2577 skb_set_queue_mapping(skb
, queue_map
);
2579 udph
= udp_hdr(skb
);
2581 memcpy(eth
, pkt_dev
->hh
, 12);
2582 *(__be16
*) & eth
[12] = protocol
;
2584 /* Eth + IPh + UDPh + mpls */
2585 datalen
= pkt_dev
->cur_pkt_size
- 14 - 20 - 8 -
2586 pkt_dev
->pkt_overhead
;
2587 if (datalen
< sizeof(struct pktgen_hdr
))
2588 datalen
= sizeof(struct pktgen_hdr
);
2590 udph
->source
= htons(pkt_dev
->cur_udp_src
);
2591 udph
->dest
= htons(pkt_dev
->cur_udp_dst
);
2592 udph
->len
= htons(datalen
+ 8); /* DATA + udphdr */
2593 udph
->check
= 0; /* No checksum */
2598 iph
->tos
= pkt_dev
->tos
;
2599 iph
->protocol
= IPPROTO_UDP
; /* UDP */
2600 iph
->saddr
= pkt_dev
->cur_saddr
;
2601 iph
->daddr
= pkt_dev
->cur_daddr
;
2603 iplen
= 20 + 8 + datalen
;
2604 iph
->tot_len
= htons(iplen
);
2606 iph
->check
= ip_fast_csum((void *)iph
, iph
->ihl
);
2607 skb
->protocol
= protocol
;
2608 skb
->mac_header
= (skb
->network_header
- ETH_HLEN
-
2609 pkt_dev
->pkt_overhead
);
2611 skb
->pkt_type
= PACKET_HOST
;
2613 if (pkt_dev
->nfrags
<= 0)
2614 pgh
= (struct pktgen_hdr
*)skb_put(skb
, datalen
);
2616 int frags
= pkt_dev
->nfrags
;
2619 pgh
= (struct pktgen_hdr
*)(((char *)(udph
)) + 8);
2621 if (frags
> MAX_SKB_FRAGS
)
2622 frags
= MAX_SKB_FRAGS
;
2623 if (datalen
> frags
* PAGE_SIZE
) {
2624 skb_put(skb
, datalen
- frags
* PAGE_SIZE
);
2625 datalen
= frags
* PAGE_SIZE
;
2629 while (datalen
> 0) {
2630 struct page
*page
= alloc_pages(GFP_KERNEL
, 0);
2631 skb_shinfo(skb
)->frags
[i
].page
= page
;
2632 skb_shinfo(skb
)->frags
[i
].page_offset
= 0;
2633 skb_shinfo(skb
)->frags
[i
].size
=
2634 (datalen
< PAGE_SIZE
? datalen
: PAGE_SIZE
);
2635 datalen
-= skb_shinfo(skb
)->frags
[i
].size
;
2636 skb
->len
+= skb_shinfo(skb
)->frags
[i
].size
;
2637 skb
->data_len
+= skb_shinfo(skb
)->frags
[i
].size
;
2639 skb_shinfo(skb
)->nr_frags
= i
;
2648 rem
= skb_shinfo(skb
)->frags
[i
- 1].size
/ 2;
2652 skb_shinfo(skb
)->frags
[i
- 1].size
-= rem
;
2654 skb_shinfo(skb
)->frags
[i
] =
2655 skb_shinfo(skb
)->frags
[i
- 1];
2656 get_page(skb_shinfo(skb
)->frags
[i
].page
);
2657 skb_shinfo(skb
)->frags
[i
].page
=
2658 skb_shinfo(skb
)->frags
[i
- 1].page
;
2659 skb_shinfo(skb
)->frags
[i
].page_offset
+=
2660 skb_shinfo(skb
)->frags
[i
- 1].size
;
2661 skb_shinfo(skb
)->frags
[i
].size
= rem
;
2663 skb_shinfo(skb
)->nr_frags
= i
;
2667 /* Stamp the time, and sequence number, convert them to network byte order */
2670 struct timeval timestamp
;
2672 pgh
->pgh_magic
= htonl(PKTGEN_MAGIC
);
2673 pgh
->seq_num
= htonl(pkt_dev
->seq_num
);
2675 do_gettimeofday(×tamp
);
2676 pgh
->tv_sec
= htonl(timestamp
.tv_sec
);
2677 pgh
->tv_usec
= htonl(timestamp
.tv_usec
);
2681 if (!process_ipsec(pkt_dev
, skb
, protocol
))
2689 * scan_ip6, fmt_ip taken from dietlibc-0.21
2690 * Author Felix von Leitner <felix-dietlibc@fefe.de>
2692 * Slightly modified for kernel.
2693 * Should be candidate for net/ipv4/utils.c
2697 static unsigned int scan_ip6(const char *s
, char ip
[16])
2700 unsigned int len
= 0;
2703 unsigned int prefixlen
= 0;
2704 unsigned int suffixlen
= 0;
2708 for (i
= 0; i
< 16; i
++)
2714 if (s
[1] == ':') { /* Found "::", skip to part 2 */
2722 u
= simple_strtoul(s
, &pos
, 16);
2726 if (prefixlen
== 12 && s
[i
] == '.') {
2728 /* the last 4 bytes may be written as IPv4 address */
2731 memcpy((struct in_addr
*)(ip
+ 12), &tmp
, sizeof(tmp
));
2734 ip
[prefixlen
++] = (u
>> 8);
2735 ip
[prefixlen
++] = (u
& 255);
2738 if (prefixlen
== 16)
2742 /* part 2, after "::" */
2749 } else if (suffixlen
!= 0)
2752 u
= simple_strtol(s
, &pos
, 16);
2759 if (suffixlen
+ prefixlen
<= 12 && s
[i
] == '.') {
2761 memcpy((struct in_addr
*)(suffix
+ suffixlen
), &tmp
,
2767 suffix
[suffixlen
++] = (u
>> 8);
2768 suffix
[suffixlen
++] = (u
& 255);
2771 if (prefixlen
+ suffixlen
== 16)
2774 for (i
= 0; i
< suffixlen
; i
++)
2775 ip
[16 - suffixlen
+ i
] = suffix
[i
];
2779 static char tohex(char hexdigit
)
2781 return hexdigit
> 9 ? hexdigit
+ 'a' - 10 : hexdigit
+ '0';
2784 static int fmt_xlong(char *s
, unsigned int i
)
2787 *s
= tohex((i
>> 12) & 0xf);
2788 if (s
!= bak
|| *s
!= '0')
2790 *s
= tohex((i
>> 8) & 0xf);
2791 if (s
!= bak
|| *s
!= '0')
2793 *s
= tohex((i
>> 4) & 0xf);
2794 if (s
!= bak
|| *s
!= '0')
2796 *s
= tohex(i
& 0xf);
2800 static unsigned int fmt_ip6(char *s
, const char ip
[16])
2805 unsigned int compressing
;
2810 for (j
= 0; j
< 16; j
+= 2) {
2812 #ifdef V4MAPPEDPREFIX
2813 if (j
== 12 && !memcmp(ip
, V4mappedprefix
, 12)) {
2814 inet_ntoa_r(*(struct in_addr
*)(ip
+ 12), s
);
2819 temp
= ((unsigned long)(unsigned char)ip
[j
] << 8) +
2820 (unsigned long)(unsigned char)ip
[j
+ 1];
2835 i
= fmt_xlong(s
, temp
);
2852 static struct sk_buff
*fill_packet_ipv6(struct net_device
*odev
,
2853 struct pktgen_dev
*pkt_dev
)
2855 struct sk_buff
*skb
= NULL
;
2857 struct udphdr
*udph
;
2859 struct ipv6hdr
*iph
;
2860 struct pktgen_hdr
*pgh
= NULL
;
2861 __be16 protocol
= htons(ETH_P_IPV6
);
2863 __be16
*vlan_tci
= NULL
; /* Encapsulates priority and VLAN ID */
2864 __be16
*vlan_encapsulated_proto
= NULL
; /* packet type ID field (or len) for VLAN tag */
2865 __be16
*svlan_tci
= NULL
; /* Encapsulates priority and SVLAN ID */
2866 __be16
*svlan_encapsulated_proto
= NULL
; /* packet type ID field (or len) for SVLAN tag */
2869 if (pkt_dev
->nr_labels
)
2870 protocol
= htons(ETH_P_MPLS_UC
);
2872 if (pkt_dev
->vlan_id
!= 0xffff)
2873 protocol
= htons(ETH_P_8021Q
);
2875 /* Update any of the values, used when we're incrementing various
2878 queue_map
= pkt_dev
->cur_queue_map
;
2879 mod_cur_headers(pkt_dev
);
2881 skb
= alloc_skb(pkt_dev
->cur_pkt_size
+ 64 + 16 +
2882 pkt_dev
->pkt_overhead
, GFP_ATOMIC
);
2884 sprintf(pkt_dev
->result
, "No memory");
2888 skb_reserve(skb
, 16);
2890 /* Reserve for ethernet and IP header */
2891 eth
= (__u8
*) skb_push(skb
, 14);
2892 mpls
= (__be32
*)skb_put(skb
, pkt_dev
->nr_labels
*sizeof(__u32
));
2893 if (pkt_dev
->nr_labels
)
2894 mpls_push(mpls
, pkt_dev
);
2896 if (pkt_dev
->vlan_id
!= 0xffff) {
2897 if (pkt_dev
->svlan_id
!= 0xffff) {
2898 svlan_tci
= (__be16
*)skb_put(skb
, sizeof(__be16
));
2899 *svlan_tci
= build_tci(pkt_dev
->svlan_id
,
2902 svlan_encapsulated_proto
= (__be16
*)skb_put(skb
, sizeof(__be16
));
2903 *svlan_encapsulated_proto
= htons(ETH_P_8021Q
);
2905 vlan_tci
= (__be16
*)skb_put(skb
, sizeof(__be16
));
2906 *vlan_tci
= build_tci(pkt_dev
->vlan_id
,
2909 vlan_encapsulated_proto
= (__be16
*)skb_put(skb
, sizeof(__be16
));
2910 *vlan_encapsulated_proto
= htons(ETH_P_IPV6
);
2913 skb
->network_header
= skb
->tail
;
2914 skb
->transport_header
= skb
->network_header
+ sizeof(struct ipv6hdr
);
2915 skb_put(skb
, sizeof(struct ipv6hdr
) + sizeof(struct udphdr
));
2916 skb_set_queue_mapping(skb
, queue_map
);
2917 iph
= ipv6_hdr(skb
);
2918 udph
= udp_hdr(skb
);
2920 memcpy(eth
, pkt_dev
->hh
, 12);
2921 *(__be16
*) & eth
[12] = protocol
;
2923 /* Eth + IPh + UDPh + mpls */
2924 datalen
= pkt_dev
->cur_pkt_size
- 14 -
2925 sizeof(struct ipv6hdr
) - sizeof(struct udphdr
) -
2926 pkt_dev
->pkt_overhead
;
2928 if (datalen
< sizeof(struct pktgen_hdr
)) {
2929 datalen
= sizeof(struct pktgen_hdr
);
2930 if (net_ratelimit())
2931 printk(KERN_INFO
"pktgen: increased datalen to %d\n",
2935 udph
->source
= htons(pkt_dev
->cur_udp_src
);
2936 udph
->dest
= htons(pkt_dev
->cur_udp_dst
);
2937 udph
->len
= htons(datalen
+ sizeof(struct udphdr
));
2938 udph
->check
= 0; /* No checksum */
2940 *(__be32
*) iph
= htonl(0x60000000); /* Version + flow */
2942 if (pkt_dev
->traffic_class
) {
2943 /* Version + traffic class + flow (0) */
2944 *(__be32
*)iph
|= htonl(0x60000000 | (pkt_dev
->traffic_class
<< 20));
2947 iph
->hop_limit
= 32;
2949 iph
->payload_len
= htons(sizeof(struct udphdr
) + datalen
);
2950 iph
->nexthdr
= IPPROTO_UDP
;
2952 ipv6_addr_copy(&iph
->daddr
, &pkt_dev
->cur_in6_daddr
);
2953 ipv6_addr_copy(&iph
->saddr
, &pkt_dev
->cur_in6_saddr
);
2955 skb
->mac_header
= (skb
->network_header
- ETH_HLEN
-
2956 pkt_dev
->pkt_overhead
);
2957 skb
->protocol
= protocol
;
2959 skb
->pkt_type
= PACKET_HOST
;
2961 if (pkt_dev
->nfrags
<= 0)
2962 pgh
= (struct pktgen_hdr
*)skb_put(skb
, datalen
);
2964 int frags
= pkt_dev
->nfrags
;
2967 pgh
= (struct pktgen_hdr
*)(((char *)(udph
)) + 8);
2969 if (frags
> MAX_SKB_FRAGS
)
2970 frags
= MAX_SKB_FRAGS
;
2971 if (datalen
> frags
* PAGE_SIZE
) {
2972 skb_put(skb
, datalen
- frags
* PAGE_SIZE
);
2973 datalen
= frags
* PAGE_SIZE
;
2977 while (datalen
> 0) {
2978 struct page
*page
= alloc_pages(GFP_KERNEL
, 0);
2979 skb_shinfo(skb
)->frags
[i
].page
= page
;
2980 skb_shinfo(skb
)->frags
[i
].page_offset
= 0;
2981 skb_shinfo(skb
)->frags
[i
].size
=
2982 (datalen
< PAGE_SIZE
? datalen
: PAGE_SIZE
);
2983 datalen
-= skb_shinfo(skb
)->frags
[i
].size
;
2984 skb
->len
+= skb_shinfo(skb
)->frags
[i
].size
;
2985 skb
->data_len
+= skb_shinfo(skb
)->frags
[i
].size
;
2987 skb_shinfo(skb
)->nr_frags
= i
;
2996 rem
= skb_shinfo(skb
)->frags
[i
- 1].size
/ 2;
3000 skb_shinfo(skb
)->frags
[i
- 1].size
-= rem
;
3002 skb_shinfo(skb
)->frags
[i
] =
3003 skb_shinfo(skb
)->frags
[i
- 1];
3004 get_page(skb_shinfo(skb
)->frags
[i
].page
);
3005 skb_shinfo(skb
)->frags
[i
].page
=
3006 skb_shinfo(skb
)->frags
[i
- 1].page
;
3007 skb_shinfo(skb
)->frags
[i
].page_offset
+=
3008 skb_shinfo(skb
)->frags
[i
- 1].size
;
3009 skb_shinfo(skb
)->frags
[i
].size
= rem
;
3011 skb_shinfo(skb
)->nr_frags
= i
;
3015 /* Stamp the time, and sequence number, convert them to network byte order */
3016 /* should we update cloned packets too ? */
3018 struct timeval timestamp
;
3020 pgh
->pgh_magic
= htonl(PKTGEN_MAGIC
);
3021 pgh
->seq_num
= htonl(pkt_dev
->seq_num
);
3023 do_gettimeofday(×tamp
);
3024 pgh
->tv_sec
= htonl(timestamp
.tv_sec
);
3025 pgh
->tv_usec
= htonl(timestamp
.tv_usec
);
3027 /* pkt_dev->seq_num++; FF: you really mean this? */
3032 static inline struct sk_buff
*fill_packet(struct net_device
*odev
,
3033 struct pktgen_dev
*pkt_dev
)
3035 if (pkt_dev
->flags
& F_IPV6
)
3036 return fill_packet_ipv6(odev
, pkt_dev
);
3038 return fill_packet_ipv4(odev
, pkt_dev
);
3041 static void pktgen_clear_counters(struct pktgen_dev
*pkt_dev
)
3043 pkt_dev
->seq_num
= 1;
3044 pkt_dev
->idle_acc
= 0;
3046 pkt_dev
->tx_bytes
= 0;
3047 pkt_dev
->errors
= 0;
3050 /* Set up structure for sending pkts, clear counters */
3052 static void pktgen_run(struct pktgen_thread
*t
)
3054 struct pktgen_dev
*pkt_dev
;
3057 pr_debug("pktgen: entering pktgen_run. %p\n", t
);
3060 list_for_each_entry(pkt_dev
, &t
->if_list
, list
) {
3063 * setup odev and create initial packet.
3065 pktgen_setup_inject(pkt_dev
);
3067 if (pkt_dev
->odev
) {
3068 pktgen_clear_counters(pkt_dev
);
3069 pkt_dev
->running
= 1; /* Cranke yeself! */
3070 pkt_dev
->skb
= NULL
;
3071 pkt_dev
->started_at
= getCurUs();
3072 pkt_dev
->next_tx_us
= getCurUs(); /* Transmit immediately */
3073 pkt_dev
->next_tx_ns
= 0;
3074 set_pkt_overhead(pkt_dev
);
3076 strcpy(pkt_dev
->result
, "Starting");
3079 strcpy(pkt_dev
->result
, "Error starting");
3083 t
->control
&= ~(T_STOP
);
3086 static void pktgen_stop_all_threads_ifs(void)
3088 struct pktgen_thread
*t
;
3090 pr_debug("pktgen: entering pktgen_stop_all_threads_ifs.\n");
3092 mutex_lock(&pktgen_thread_lock
);
3094 list_for_each_entry(t
, &pktgen_threads
, th_list
)
3095 t
->control
|= T_STOP
;
3097 mutex_unlock(&pktgen_thread_lock
);
3100 static int thread_is_running(struct pktgen_thread
*t
)
3102 struct pktgen_dev
*pkt_dev
;
3105 list_for_each_entry(pkt_dev
, &t
->if_list
, list
)
3106 if (pkt_dev
->running
) {
3113 static int pktgen_wait_thread_run(struct pktgen_thread
*t
)
3117 while (thread_is_running(t
)) {
3121 msleep_interruptible(100);
3123 if (signal_pending(current
))
3133 static int pktgen_wait_all_threads_run(void)
3135 struct pktgen_thread
*t
;
3138 mutex_lock(&pktgen_thread_lock
);
3140 list_for_each_entry(t
, &pktgen_threads
, th_list
) {
3141 sig
= pktgen_wait_thread_run(t
);
3147 list_for_each_entry(t
, &pktgen_threads
, th_list
)
3148 t
->control
|= (T_STOP
);
3150 mutex_unlock(&pktgen_thread_lock
);
3154 static void pktgen_run_all_threads(void)
3156 struct pktgen_thread
*t
;
3158 pr_debug("pktgen: entering pktgen_run_all_threads.\n");
3160 mutex_lock(&pktgen_thread_lock
);
3162 list_for_each_entry(t
, &pktgen_threads
, th_list
)
3163 t
->control
|= (T_RUN
);
3165 mutex_unlock(&pktgen_thread_lock
);
3167 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
3169 pktgen_wait_all_threads_run();
3172 static void show_results(struct pktgen_dev
*pkt_dev
, int nr_frags
)
3174 __u64 total_us
, bps
, mbps
, pps
, idle
;
3175 char *p
= pkt_dev
->result
;
3177 total_us
= pkt_dev
->stopped_at
- pkt_dev
->started_at
;
3179 idle
= pkt_dev
->idle_acc
;
3181 p
+= sprintf(p
, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
3182 (unsigned long long)total_us
,
3183 (unsigned long long)(total_us
- idle
),
3184 (unsigned long long)idle
,
3185 (unsigned long long)pkt_dev
->sofar
,
3186 pkt_dev
->cur_pkt_size
, nr_frags
);
3188 pps
= pkt_dev
->sofar
* USEC_PER_SEC
;
3190 while ((total_us
>> 32) != 0) {
3195 do_div(pps
, total_us
);
3197 bps
= pps
* 8 * pkt_dev
->cur_pkt_size
;
3200 do_div(mbps
, 1000000);
3201 p
+= sprintf(p
, " %llupps %lluMb/sec (%llubps) errors: %llu",
3202 (unsigned long long)pps
,
3203 (unsigned long long)mbps
,
3204 (unsigned long long)bps
,
3205 (unsigned long long)pkt_dev
->errors
);
3208 /* Set stopped-at timer, remove from running list, do counters & statistics */
3210 static int pktgen_stop_device(struct pktgen_dev
*pkt_dev
)
3212 int nr_frags
= pkt_dev
->skb
? skb_shinfo(pkt_dev
->skb
)->nr_frags
: -1;
3214 if (!pkt_dev
->running
) {
3215 printk(KERN_WARNING
"pktgen: interface: %s is already "
3216 "stopped\n", pkt_dev
->odev
->name
);
3220 pkt_dev
->stopped_at
= getCurUs();
3221 pkt_dev
->running
= 0;
3223 show_results(pkt_dev
, nr_frags
);
3228 static struct pktgen_dev
*next_to_run(struct pktgen_thread
*t
)
3230 struct pktgen_dev
*pkt_dev
, *best
= NULL
;
3234 list_for_each_entry(pkt_dev
, &t
->if_list
, list
) {
3235 if (!pkt_dev
->running
)
3239 else if (pkt_dev
->next_tx_us
< best
->next_tx_us
)
3246 static void pktgen_stop(struct pktgen_thread
*t
)
3248 struct pktgen_dev
*pkt_dev
;
3250 pr_debug("pktgen: entering pktgen_stop\n");
3254 list_for_each_entry(pkt_dev
, &t
->if_list
, list
) {
3255 pktgen_stop_device(pkt_dev
);
3257 kfree_skb(pkt_dev
->skb
);
3259 pkt_dev
->skb
= NULL
;
3266 * one of our devices needs to be removed - find it
3269 static void pktgen_rem_one_if(struct pktgen_thread
*t
)
3271 struct list_head
*q
, *n
;
3272 struct pktgen_dev
*cur
;
3274 pr_debug("pktgen: entering pktgen_rem_one_if\n");
3278 list_for_each_safe(q
, n
, &t
->if_list
) {
3279 cur
= list_entry(q
, struct pktgen_dev
, list
);
3281 if (!cur
->removal_mark
)
3285 kfree_skb(cur
->skb
);
3288 pktgen_remove_device(t
, cur
);
3296 static void pktgen_rem_all_ifs(struct pktgen_thread
*t
)
3298 struct list_head
*q
, *n
;
3299 struct pktgen_dev
*cur
;
3301 /* Remove all devices, free mem */
3303 pr_debug("pktgen: entering pktgen_rem_all_ifs\n");
3306 list_for_each_safe(q
, n
, &t
->if_list
) {
3307 cur
= list_entry(q
, struct pktgen_dev
, list
);
3310 kfree_skb(cur
->skb
);
3313 pktgen_remove_device(t
, cur
);
3319 static void pktgen_rem_thread(struct pktgen_thread
*t
)
3321 /* Remove from the thread list */
3323 remove_proc_entry(t
->tsk
->comm
, pg_proc_dir
);
3325 mutex_lock(&pktgen_thread_lock
);
3327 list_del(&t
->th_list
);
3329 mutex_unlock(&pktgen_thread_lock
);
3332 static __inline__
void pktgen_xmit(struct pktgen_dev
*pkt_dev
)
3334 struct net_device
*odev
= NULL
;
3335 struct netdev_queue
*txq
;
3336 __u64 idle_start
= 0;
3340 odev
= pkt_dev
->odev
;
3342 if (pkt_dev
->delay_us
|| pkt_dev
->delay_ns
) {
3346 if (now
< pkt_dev
->next_tx_us
)
3347 spin(pkt_dev
, pkt_dev
->next_tx_us
);
3349 /* This is max DELAY, this has special meaning of
3352 if (pkt_dev
->delay_us
== 0x7FFFFFFF) {
3353 pkt_dev
->next_tx_us
= getCurUs() + pkt_dev
->delay_us
;
3354 pkt_dev
->next_tx_ns
= pkt_dev
->delay_ns
;
3359 if (!pkt_dev
->skb
) {
3360 set_cur_queue_map(pkt_dev
);
3361 queue_map
= pkt_dev
->cur_queue_map
;
3363 queue_map
= skb_get_queue_mapping(pkt_dev
->skb
);
3366 txq
= netdev_get_tx_queue(odev
, queue_map
);
3367 if (netif_tx_queue_stopped(txq
) ||
3368 netif_tx_queue_frozen(txq
) ||
3370 idle_start
= getCurUs();
3372 if (!netif_running(odev
)) {
3373 pktgen_stop_device(pkt_dev
);
3375 kfree_skb(pkt_dev
->skb
);
3376 pkt_dev
->skb
= NULL
;
3382 pkt_dev
->idle_acc
+= getCurUs() - idle_start
;
3384 if (netif_tx_queue_stopped(txq
) ||
3385 netif_tx_queue_frozen(txq
)) {
3386 pkt_dev
->next_tx_us
= getCurUs(); /* TODO */
3387 pkt_dev
->next_tx_ns
= 0;
3388 goto out
; /* Try the next interface */
3392 if (pkt_dev
->last_ok
|| !pkt_dev
->skb
) {
3393 if ((++pkt_dev
->clone_count
>= pkt_dev
->clone_skb
)
3394 || (!pkt_dev
->skb
)) {
3395 /* build a new pkt */
3397 kfree_skb(pkt_dev
->skb
);
3399 pkt_dev
->skb
= fill_packet(odev
, pkt_dev
);
3400 if (pkt_dev
->skb
== NULL
) {
3401 printk(KERN_ERR
"pktgen: ERROR: couldn't "
3402 "allocate skb in fill_packet.\n");
3404 pkt_dev
->clone_count
--; /* back out increment, OOM */
3407 pkt_dev
->allocated_skbs
++;
3408 pkt_dev
->clone_count
= 0; /* reset counter */
3412 /* fill_packet() might have changed the queue */
3413 queue_map
= skb_get_queue_mapping(pkt_dev
->skb
);
3414 txq
= netdev_get_tx_queue(odev
, queue_map
);
3416 __netif_tx_lock_bh(txq
);
3417 if (!netif_tx_queue_stopped(txq
) &&
3418 !netif_tx_queue_frozen(txq
)) {
3420 atomic_inc(&(pkt_dev
->skb
->users
));
3422 ret
= odev
->hard_start_xmit(pkt_dev
->skb
, odev
);
3423 if (likely(ret
== NETDEV_TX_OK
)) {
3424 pkt_dev
->last_ok
= 1;
3427 pkt_dev
->tx_bytes
+= pkt_dev
->cur_pkt_size
;
3429 } else if (ret
== NETDEV_TX_LOCKED
3430 && (odev
->features
& NETIF_F_LLTX
)) {
3433 } else { /* Retry it next time */
3435 atomic_dec(&(pkt_dev
->skb
->users
));
3437 if (debug
&& net_ratelimit())
3438 printk(KERN_INFO
"pktgen: Hard xmit error\n");
3441 pkt_dev
->last_ok
= 0;
3444 pkt_dev
->next_tx_us
= getCurUs();
3445 pkt_dev
->next_tx_ns
= 0;
3447 pkt_dev
->next_tx_us
+= pkt_dev
->delay_us
;
3448 pkt_dev
->next_tx_ns
+= pkt_dev
->delay_ns
;
3450 if (pkt_dev
->next_tx_ns
> 1000) {
3451 pkt_dev
->next_tx_us
++;
3452 pkt_dev
->next_tx_ns
-= 1000;
3456 else { /* Retry it next time */
3457 pkt_dev
->last_ok
= 0;
3458 pkt_dev
->next_tx_us
= getCurUs(); /* TODO */
3459 pkt_dev
->next_tx_ns
= 0;
3462 __netif_tx_unlock_bh(txq
);
3464 /* If pkt_dev->count is zero, then run forever */
3465 if ((pkt_dev
->count
!= 0) && (pkt_dev
->sofar
>= pkt_dev
->count
)) {
3466 if (atomic_read(&(pkt_dev
->skb
->users
)) != 1) {
3467 idle_start
= getCurUs();
3468 while (atomic_read(&(pkt_dev
->skb
->users
)) != 1) {
3469 if (signal_pending(current
)) {
3474 pkt_dev
->idle_acc
+= getCurUs() - idle_start
;
3477 /* Done with this */
3478 pktgen_stop_device(pkt_dev
);
3480 kfree_skb(pkt_dev
->skb
);
3481 pkt_dev
->skb
= NULL
;
3487 * Main loop of the thread goes here
3490 static int pktgen_thread_worker(void *arg
)
3493 struct pktgen_thread
*t
= arg
;
3494 struct pktgen_dev
*pkt_dev
= NULL
;
3497 BUG_ON(smp_processor_id() != cpu
);
3499 init_waitqueue_head(&t
->queue
);
3500 complete(&t
->start_done
);
3502 pr_debug("pktgen: starting pktgen/%d: pid=%d\n", cpu
, task_pid_nr(current
));
3504 set_current_state(TASK_INTERRUPTIBLE
);
3508 while (!kthread_should_stop()) {
3509 pkt_dev
= next_to_run(t
);
3512 (t
->control
& (T_STOP
| T_RUN
| T_REMDEVALL
| T_REMDEV
))
3514 prepare_to_wait(&(t
->queue
), &wait
,
3515 TASK_INTERRUPTIBLE
);
3516 schedule_timeout(HZ
/ 10);
3517 finish_wait(&(t
->queue
), &wait
);
3520 __set_current_state(TASK_RUNNING
);
3523 pktgen_xmit(pkt_dev
);
3525 if (t
->control
& T_STOP
) {
3527 t
->control
&= ~(T_STOP
);
3530 if (t
->control
& T_RUN
) {
3532 t
->control
&= ~(T_RUN
);
3535 if (t
->control
& T_REMDEVALL
) {
3536 pktgen_rem_all_ifs(t
);
3537 t
->control
&= ~(T_REMDEVALL
);
3540 if (t
->control
& T_REMDEV
) {
3541 pktgen_rem_one_if(t
);
3542 t
->control
&= ~(T_REMDEV
);
3547 set_current_state(TASK_INTERRUPTIBLE
);
3550 pr_debug("pktgen: %s stopping all device\n", t
->tsk
->comm
);
3553 pr_debug("pktgen: %s removing all device\n", t
->tsk
->comm
);
3554 pktgen_rem_all_ifs(t
);
3556 pr_debug("pktgen: %s removing thread.\n", t
->tsk
->comm
);
3557 pktgen_rem_thread(t
);
3562 static struct pktgen_dev
*pktgen_find_dev(struct pktgen_thread
*t
,
3565 struct pktgen_dev
*p
, *pkt_dev
= NULL
;
3568 list_for_each_entry(p
, &t
->if_list
, list
)
3569 if (strncmp(p
->odev
->name
, ifname
, IFNAMSIZ
) == 0) {
3575 pr_debug("pktgen: find_dev(%s) returning %p\n", ifname
, pkt_dev
);
3580 * Adds a dev at front of if_list.
3583 static int add_dev_to_thread(struct pktgen_thread
*t
,
3584 struct pktgen_dev
*pkt_dev
)
3590 if (pkt_dev
->pg_thread
) {
3591 printk(KERN_ERR
"pktgen: ERROR: already assigned "
3597 list_add(&pkt_dev
->list
, &t
->if_list
);
3598 pkt_dev
->pg_thread
= t
;
3599 pkt_dev
->running
= 0;
3606 /* Called under thread lock */
3608 static int pktgen_add_device(struct pktgen_thread
*t
, const char *ifname
)
3610 struct pktgen_dev
*pkt_dev
;
3613 /* We don't allow a device to be on several threads */
3615 pkt_dev
= __pktgen_NN_threads(ifname
, FIND
);
3617 printk(KERN_ERR
"pktgen: ERROR: interface already used.\n");
3621 pkt_dev
= kzalloc(sizeof(struct pktgen_dev
), GFP_KERNEL
);
3625 pkt_dev
->flows
= vmalloc(MAX_CFLOWS
* sizeof(struct flow_state
));
3626 if (pkt_dev
->flows
== NULL
) {
3630 memset(pkt_dev
->flows
, 0, MAX_CFLOWS
* sizeof(struct flow_state
));
3632 pkt_dev
->removal_mark
= 0;
3633 pkt_dev
->min_pkt_size
= ETH_ZLEN
;
3634 pkt_dev
->max_pkt_size
= ETH_ZLEN
;
3635 pkt_dev
->nfrags
= 0;
3636 pkt_dev
->clone_skb
= pg_clone_skb_d
;
3637 pkt_dev
->delay_us
= pg_delay_d
/ 1000;
3638 pkt_dev
->delay_ns
= pg_delay_d
% 1000;
3639 pkt_dev
->count
= pg_count_d
;
3641 pkt_dev
->udp_src_min
= 9; /* sink port */
3642 pkt_dev
->udp_src_max
= 9;
3643 pkt_dev
->udp_dst_min
= 9;
3644 pkt_dev
->udp_dst_max
= 9;
3646 pkt_dev
->vlan_p
= 0;
3647 pkt_dev
->vlan_cfi
= 0;
3648 pkt_dev
->vlan_id
= 0xffff;
3649 pkt_dev
->svlan_p
= 0;
3650 pkt_dev
->svlan_cfi
= 0;
3651 pkt_dev
->svlan_id
= 0xffff;
3653 err
= pktgen_setup_dev(pkt_dev
, ifname
);
3657 pkt_dev
->entry
= proc_create_data(ifname
, 0600, pg_proc_dir
,
3658 &pktgen_if_fops
, pkt_dev
);
3659 if (!pkt_dev
->entry
) {
3660 printk(KERN_ERR
"pktgen: cannot create %s/%s procfs entry.\n",
3661 PG_PROC_DIR
, ifname
);
3666 pkt_dev
->ipsmode
= XFRM_MODE_TRANSPORT
;
3667 pkt_dev
->ipsproto
= IPPROTO_ESP
;
3670 return add_dev_to_thread(t
, pkt_dev
);
3672 dev_put(pkt_dev
->odev
);
3678 vfree(pkt_dev
->flows
);
3683 static int __init
pktgen_create_thread(int cpu
)
3685 struct pktgen_thread
*t
;
3686 struct proc_dir_entry
*pe
;
3687 struct task_struct
*p
;
3689 t
= kzalloc(sizeof(struct pktgen_thread
), GFP_KERNEL
);
3691 printk(KERN_ERR
"pktgen: ERROR: out of memory, can't "
3692 "create new thread.\n");
3696 spin_lock_init(&t
->if_lock
);
3699 INIT_LIST_HEAD(&t
->if_list
);
3701 list_add_tail(&t
->th_list
, &pktgen_threads
);
3702 init_completion(&t
->start_done
);
3704 p
= kthread_create(pktgen_thread_worker
, t
, "kpktgend_%d", cpu
);
3706 printk(KERN_ERR
"pktgen: kernel_thread() failed "
3707 "for cpu %d\n", t
->cpu
);
3708 list_del(&t
->th_list
);
3712 kthread_bind(p
, cpu
);
3715 pe
= proc_create_data(t
->tsk
->comm
, 0600, pg_proc_dir
,
3716 &pktgen_thread_fops
, t
);
3718 printk(KERN_ERR
"pktgen: cannot create %s/%s procfs entry.\n",
3719 PG_PROC_DIR
, t
->tsk
->comm
);
3721 list_del(&t
->th_list
);
3727 wait_for_completion(&t
->start_done
);
3733 * Removes a device from the thread if_list.
3735 static void _rem_dev_from_if_list(struct pktgen_thread
*t
,
3736 struct pktgen_dev
*pkt_dev
)
3738 struct list_head
*q
, *n
;
3739 struct pktgen_dev
*p
;
3741 list_for_each_safe(q
, n
, &t
->if_list
) {
3742 p
= list_entry(q
, struct pktgen_dev
, list
);
3748 static int pktgen_remove_device(struct pktgen_thread
*t
,
3749 struct pktgen_dev
*pkt_dev
)
3752 pr_debug("pktgen: remove_device pkt_dev=%p\n", pkt_dev
);
3754 if (pkt_dev
->running
) {
3755 printk(KERN_WARNING
"pktgen: WARNING: trying to remove a "
3756 "running interface, stopping it now.\n");
3757 pktgen_stop_device(pkt_dev
);
3760 /* Dis-associate from the interface */
3762 if (pkt_dev
->odev
) {
3763 dev_put(pkt_dev
->odev
);
3764 pkt_dev
->odev
= NULL
;
3767 /* And update the thread if_list */
3769 _rem_dev_from_if_list(t
, pkt_dev
);
3772 remove_proc_entry(pkt_dev
->entry
->name
, pg_proc_dir
);
3778 vfree(pkt_dev
->flows
);
3783 static int __init
pg_init(void)
3786 struct proc_dir_entry
*pe
;
3788 printk(KERN_INFO
"%s", version
);
3790 pg_proc_dir
= proc_mkdir(PG_PROC_DIR
, init_net
.proc_net
);
3793 pg_proc_dir
->owner
= THIS_MODULE
;
3795 pe
= proc_create(PGCTRL
, 0600, pg_proc_dir
, &pktgen_fops
);
3797 printk(KERN_ERR
"pktgen: ERROR: cannot create %s "
3798 "procfs entry.\n", PGCTRL
);
3799 proc_net_remove(&init_net
, PG_PROC_DIR
);
3803 /* Register us to receive netdevice events */
3804 register_netdevice_notifier(&pktgen_notifier_block
);
3806 for_each_online_cpu(cpu
) {
3809 err
= pktgen_create_thread(cpu
);
3811 printk(KERN_WARNING
"pktgen: WARNING: Cannot create "
3812 "thread for cpu %d (%d)\n", cpu
, err
);
3815 if (list_empty(&pktgen_threads
)) {
3816 printk(KERN_ERR
"pktgen: ERROR: Initialization failed for "
3818 unregister_netdevice_notifier(&pktgen_notifier_block
);
3819 remove_proc_entry(PGCTRL
, pg_proc_dir
);
3820 proc_net_remove(&init_net
, PG_PROC_DIR
);
3827 static void __exit
pg_cleanup(void)
3829 struct pktgen_thread
*t
;
3830 struct list_head
*q
, *n
;
3831 wait_queue_head_t queue
;
3832 init_waitqueue_head(&queue
);
3834 /* Stop all interfaces & threads */
3836 list_for_each_safe(q
, n
, &pktgen_threads
) {
3837 t
= list_entry(q
, struct pktgen_thread
, th_list
);
3838 kthread_stop(t
->tsk
);
3842 /* Un-register us from receiving netdevice events */
3843 unregister_netdevice_notifier(&pktgen_notifier_block
);
3845 /* Clean up proc file system */
3846 remove_proc_entry(PGCTRL
, pg_proc_dir
);
3847 proc_net_remove(&init_net
, PG_PROC_DIR
);
3850 module_init(pg_init
);
3851 module_exit(pg_cleanup
);
3853 MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se");
3854 MODULE_DESCRIPTION("Packet Generator tool");
3855 MODULE_LICENSE("GPL");
3856 module_param(pg_count_d
, int, 0);
3857 module_param(pg_delay_d
, int, 0);
3858 module_param(pg_clone_skb_d
, int, 0);
3859 module_param(debug
, int, 0);