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/hrtimer.h>
135 #include <linux/freezer.h>
136 #include <linux/delay.h>
137 #include <linux/timer.h>
138 #include <linux/list.h>
139 #include <linux/init.h>
140 #include <linux/skbuff.h>
141 #include <linux/netdevice.h>
142 #include <linux/inet.h>
143 #include <linux/inetdevice.h>
144 #include <linux/rtnetlink.h>
145 #include <linux/if_arp.h>
146 #include <linux/if_vlan.h>
147 #include <linux/in.h>
148 #include <linux/ip.h>
149 #include <linux/ipv6.h>
150 #include <linux/udp.h>
151 #include <linux/proc_fs.h>
152 #include <linux/seq_file.h>
153 #include <linux/wait.h>
154 #include <linux/etherdevice.h>
155 #include <linux/kthread.h>
156 #include <net/net_namespace.h>
157 #include <net/checksum.h>
158 #include <net/ipv6.h>
159 #include <net/addrconf.h>
161 #include <net/xfrm.h>
163 #include <asm/byteorder.h>
164 #include <linux/rcupdate.h>
165 #include <linux/bitops.h>
166 #include <linux/io.h>
167 #include <linux/timex.h>
168 #include <linux/uaccess.h>
170 #include <asm/div64.h> /* do_div */
172 #define VERSION "2.72"
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
;
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
; /* chaining in the thread's run-queue */
236 int running
; /* if 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 u64 delay
; /* nano-seconds */
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,
255 pkts will be re-sent */
257 /* runtime counters relating to clone_skb */
259 __u64 allocated_skbs
;
261 int last_ok
; /* Was last skb sent?
262 * Or a failed transmit of some sort?
263 * This will keep sequence numbers in order
268 u64 idle_acc
; /* nano-seconds */
273 * Use multiple SKBs during packet gen.
274 * If this number is greater than 1, then
275 * that many copies of the same packet will be
276 * sent before a new packet is allocated.
277 * If you want to send 1024 identical packets
278 * before creating a new packet,
279 * set clone_skb to 1024.
282 char dst_min
[IP_NAME_SZ
]; /* IP, ie 1.2.3.4 */
283 char dst_max
[IP_NAME_SZ
]; /* IP, ie 1.2.3.4 */
284 char src_min
[IP_NAME_SZ
]; /* IP, ie 1.2.3.4 */
285 char src_max
[IP_NAME_SZ
]; /* IP, ie 1.2.3.4 */
287 struct in6_addr in6_saddr
;
288 struct in6_addr in6_daddr
;
289 struct in6_addr cur_in6_daddr
;
290 struct in6_addr cur_in6_saddr
;
292 struct in6_addr min_in6_daddr
;
293 struct in6_addr max_in6_daddr
;
294 struct in6_addr min_in6_saddr
;
295 struct in6_addr max_in6_saddr
;
297 /* If we're doing ranges, random or incremental, then this
298 * defines the min/max for those ranges.
300 __be32 saddr_min
; /* inclusive, source IP address */
301 __be32 saddr_max
; /* exclusive, source IP address */
302 __be32 daddr_min
; /* inclusive, dest IP address */
303 __be32 daddr_max
; /* exclusive, dest IP address */
305 __u16 udp_src_min
; /* inclusive, source UDP port */
306 __u16 udp_src_max
; /* exclusive, source UDP port */
307 __u16 udp_dst_min
; /* inclusive, dest UDP port */
308 __u16 udp_dst_max
; /* exclusive, dest UDP port */
311 __u8 tos
; /* six MSB of (former) IPv4 TOS
312 are for dscp codepoint */
313 __u8 traffic_class
; /* ditto for the (former) Traffic Class in IPv6
314 (see RFC 3260, sec. 4) */
317 unsigned nr_labels
; /* Depth of stack, 0 = no MPLS */
318 __be32 labels
[MAX_MPLS_LABELS
];
320 /* VLAN/SVLAN (802.1Q/Q-in-Q) */
323 __u16 vlan_id
; /* 0xffff means no vlan tag */
327 __u16 svlan_id
; /* 0xffff means no svlan tag */
329 __u32 src_mac_count
; /* How many MACs to iterate through */
330 __u32 dst_mac_count
; /* How many MACs to iterate through */
332 unsigned char dst_mac
[ETH_ALEN
];
333 unsigned char src_mac
[ETH_ALEN
];
335 __u32 cur_dst_mac_offset
;
336 __u32 cur_src_mac_offset
;
346 0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
348 We fill in SRC address later
349 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
353 __u16 pad
; /* pad out the hh struct to an even 16 bytes */
355 struct sk_buff
*skb
; /* skb we are to transmit next, used for when we
356 * are transmitting the same one multiple times
358 struct net_device
*odev
; /* The out-going device.
359 * Note that the device should have it's
360 * pg_info pointer pointing back to this
362 * Set when the user specifies the out-going
363 * device name (not when the inject is
364 * started as it used to do.)
366 struct flow_state
*flows
;
367 unsigned cflows
; /* Concurrent flows (config) */
368 unsigned lflow
; /* Flow length (config) */
369 unsigned nflows
; /* accumulated flows (stats) */
370 unsigned curfl
; /* current sequenced flow (state)*/
376 __u8 ipsmode
; /* IPSEC mode (config) */
377 __u8 ipsproto
; /* IPSEC type (config) */
389 struct pktgen_thread
{
390 spinlock_t if_lock
; /* for list of devices */
391 struct list_head if_list
; /* All device here */
392 struct list_head th_list
;
393 struct task_struct
*tsk
;
396 /* Field for thread to receive "posted" events terminate,
402 wait_queue_head_t queue
;
403 struct completion start_done
;
409 static inline ktime_t
ktime_now(void)
414 return timespec_to_ktime(ts
);
417 /* This works even if 32 bit because of careful byte order choice */
418 static inline int ktime_lt(const ktime_t cmp1
, const ktime_t cmp2
)
420 return cmp1
.tv64
< cmp2
.tv64
;
423 static const char version
[] =
424 "pktgen " VERSION
": Packet Generator for packet performance testing.\n";
426 static int pktgen_remove_device(struct pktgen_thread
*t
, struct pktgen_dev
*i
);
427 static int pktgen_add_device(struct pktgen_thread
*t
, const char *ifname
);
428 static struct pktgen_dev
*pktgen_find_dev(struct pktgen_thread
*t
,
430 static int pktgen_device_event(struct notifier_block
*, unsigned long, void *);
431 static void pktgen_run_all_threads(void);
432 static void pktgen_reset_all_threads(void);
433 static void pktgen_stop_all_threads_ifs(void);
435 static void pktgen_stop(struct pktgen_thread
*t
);
436 static void pktgen_clear_counters(struct pktgen_dev
*pkt_dev
);
438 static unsigned int scan_ip6(const char *s
, char ip
[16]);
439 static unsigned int fmt_ip6(char *s
, const char ip
[16]);
441 /* Module parameters, defaults. */
442 static int pg_count_d __read_mostly
= 1000;
443 static int pg_delay_d __read_mostly
;
444 static int pg_clone_skb_d __read_mostly
;
445 static int debug __read_mostly
;
447 static DEFINE_MUTEX(pktgen_thread_lock
);
448 static LIST_HEAD(pktgen_threads
);
450 static struct notifier_block pktgen_notifier_block
= {
451 .notifier_call
= pktgen_device_event
,
455 * /proc handling functions
459 static int pgctrl_show(struct seq_file
*seq
, void *v
)
461 seq_puts(seq
, version
);
465 static ssize_t
pgctrl_write(struct file
*file
, const char __user
*buf
,
466 size_t count
, loff_t
*ppos
)
471 if (!capable(CAP_NET_ADMIN
)) {
476 if (count
> sizeof(data
))
477 count
= sizeof(data
);
479 if (copy_from_user(data
, buf
, count
)) {
483 data
[count
- 1] = 0; /* Make string */
485 if (!strcmp(data
, "stop"))
486 pktgen_stop_all_threads_ifs();
488 else if (!strcmp(data
, "start"))
489 pktgen_run_all_threads();
491 else if (!strcmp(data
, "reset"))
492 pktgen_reset_all_threads();
495 printk(KERN_WARNING
"pktgen: Unknown command: %s\n", data
);
503 static int pgctrl_open(struct inode
*inode
, struct file
*file
)
505 return single_open(file
, pgctrl_show
, PDE(inode
)->data
);
508 static const struct file_operations pktgen_fops
= {
509 .owner
= THIS_MODULE
,
513 .write
= pgctrl_write
,
514 .release
= single_release
,
517 static int pktgen_if_show(struct seq_file
*seq
, void *v
)
519 const struct pktgen_dev
*pkt_dev
= seq
->private;
524 "Params: count %llu min_pkt_size: %u max_pkt_size: %u\n",
525 (unsigned long long)pkt_dev
->count
, pkt_dev
->min_pkt_size
,
526 pkt_dev
->max_pkt_size
);
529 " frags: %d delay: %llu clone_skb: %d ifname: %s\n",
530 pkt_dev
->nfrags
, (unsigned long long) pkt_dev
->delay
,
531 pkt_dev
->clone_skb
, pkt_dev
->odev
->name
);
533 seq_printf(seq
, " flows: %u flowlen: %u\n", pkt_dev
->cflows
,
537 " queue_map_min: %u queue_map_max: %u\n",
538 pkt_dev
->queue_map_min
,
539 pkt_dev
->queue_map_max
);
541 if (pkt_dev
->flags
& F_IPV6
) {
542 char b1
[128], b2
[128], b3
[128];
543 fmt_ip6(b1
, pkt_dev
->in6_saddr
.s6_addr
);
544 fmt_ip6(b2
, pkt_dev
->min_in6_saddr
.s6_addr
);
545 fmt_ip6(b3
, pkt_dev
->max_in6_saddr
.s6_addr
);
547 " saddr: %s min_saddr: %s max_saddr: %s\n", b1
,
550 fmt_ip6(b1
, pkt_dev
->in6_daddr
.s6_addr
);
551 fmt_ip6(b2
, pkt_dev
->min_in6_daddr
.s6_addr
);
552 fmt_ip6(b3
, pkt_dev
->max_in6_daddr
.s6_addr
);
554 " daddr: %s min_daddr: %s max_daddr: %s\n", b1
,
559 " dst_min: %s dst_max: %s\n",
560 pkt_dev
->dst_min
, pkt_dev
->dst_max
);
562 " src_min: %s src_max: %s\n",
563 pkt_dev
->src_min
, pkt_dev
->src_max
);
566 seq_puts(seq
, " src_mac: ");
568 seq_printf(seq
, "%pM ",
569 is_zero_ether_addr(pkt_dev
->src_mac
) ?
570 pkt_dev
->odev
->dev_addr
: pkt_dev
->src_mac
);
572 seq_printf(seq
, "dst_mac: ");
573 seq_printf(seq
, "%pM\n", pkt_dev
->dst_mac
);
576 " udp_src_min: %d udp_src_max: %d"
577 " udp_dst_min: %d udp_dst_max: %d\n",
578 pkt_dev
->udp_src_min
, pkt_dev
->udp_src_max
,
579 pkt_dev
->udp_dst_min
, pkt_dev
->udp_dst_max
);
582 " src_mac_count: %d dst_mac_count: %d\n",
583 pkt_dev
->src_mac_count
, pkt_dev
->dst_mac_count
);
585 if (pkt_dev
->nr_labels
) {
587 seq_printf(seq
, " mpls: ");
588 for (i
= 0; i
< pkt_dev
->nr_labels
; i
++)
589 seq_printf(seq
, "%08x%s", ntohl(pkt_dev
->labels
[i
]),
590 i
== pkt_dev
->nr_labels
-1 ? "\n" : ", ");
593 if (pkt_dev
->vlan_id
!= 0xffff)
594 seq_printf(seq
, " vlan_id: %u vlan_p: %u vlan_cfi: %u\n",
595 pkt_dev
->vlan_id
, pkt_dev
->vlan_p
,
598 if (pkt_dev
->svlan_id
!= 0xffff)
599 seq_printf(seq
, " svlan_id: %u vlan_p: %u vlan_cfi: %u\n",
600 pkt_dev
->svlan_id
, pkt_dev
->svlan_p
,
604 seq_printf(seq
, " tos: 0x%02x\n", pkt_dev
->tos
);
606 if (pkt_dev
->traffic_class
)
607 seq_printf(seq
, " traffic_class: 0x%02x\n", pkt_dev
->traffic_class
);
609 seq_printf(seq
, " Flags: ");
611 if (pkt_dev
->flags
& F_IPV6
)
612 seq_printf(seq
, "IPV6 ");
614 if (pkt_dev
->flags
& F_IPSRC_RND
)
615 seq_printf(seq
, "IPSRC_RND ");
617 if (pkt_dev
->flags
& F_IPDST_RND
)
618 seq_printf(seq
, "IPDST_RND ");
620 if (pkt_dev
->flags
& F_TXSIZE_RND
)
621 seq_printf(seq
, "TXSIZE_RND ");
623 if (pkt_dev
->flags
& F_UDPSRC_RND
)
624 seq_printf(seq
, "UDPSRC_RND ");
626 if (pkt_dev
->flags
& F_UDPDST_RND
)
627 seq_printf(seq
, "UDPDST_RND ");
629 if (pkt_dev
->flags
& F_MPLS_RND
)
630 seq_printf(seq
, "MPLS_RND ");
632 if (pkt_dev
->flags
& F_QUEUE_MAP_RND
)
633 seq_printf(seq
, "QUEUE_MAP_RND ");
635 if (pkt_dev
->flags
& F_QUEUE_MAP_CPU
)
636 seq_printf(seq
, "QUEUE_MAP_CPU ");
638 if (pkt_dev
->cflows
) {
639 if (pkt_dev
->flags
& F_FLOW_SEQ
)
640 seq_printf(seq
, "FLOW_SEQ "); /*in sequence flows*/
642 seq_printf(seq
, "FLOW_RND ");
646 if (pkt_dev
->flags
& F_IPSEC_ON
)
647 seq_printf(seq
, "IPSEC ");
650 if (pkt_dev
->flags
& F_MACSRC_RND
)
651 seq_printf(seq
, "MACSRC_RND ");
653 if (pkt_dev
->flags
& F_MACDST_RND
)
654 seq_printf(seq
, "MACDST_RND ");
656 if (pkt_dev
->flags
& F_VID_RND
)
657 seq_printf(seq
, "VID_RND ");
659 if (pkt_dev
->flags
& F_SVID_RND
)
660 seq_printf(seq
, "SVID_RND ");
664 /* not really stopped, more like last-running-at */
665 stopped
= pkt_dev
->running
? ktime_now() : pkt_dev
->stopped_at
;
666 idle
= pkt_dev
->idle_acc
;
667 do_div(idle
, NSEC_PER_USEC
);
670 "Current:\n pkts-sofar: %llu errors: %llu\n",
671 (unsigned long long)pkt_dev
->sofar
,
672 (unsigned long long)pkt_dev
->errors
);
675 " started: %lluus stopped: %lluus idle: %lluus\n",
676 (unsigned long long) ktime_to_us(pkt_dev
->started_at
),
677 (unsigned long long) ktime_to_us(stopped
),
678 (unsigned long long) idle
);
681 " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n",
682 pkt_dev
->seq_num
, pkt_dev
->cur_dst_mac_offset
,
683 pkt_dev
->cur_src_mac_offset
);
685 if (pkt_dev
->flags
& F_IPV6
) {
686 char b1
[128], b2
[128];
687 fmt_ip6(b1
, pkt_dev
->cur_in6_daddr
.s6_addr
);
688 fmt_ip6(b2
, pkt_dev
->cur_in6_saddr
.s6_addr
);
689 seq_printf(seq
, " cur_saddr: %s cur_daddr: %s\n", b2
, b1
);
691 seq_printf(seq
, " cur_saddr: 0x%x cur_daddr: 0x%x\n",
692 pkt_dev
->cur_saddr
, pkt_dev
->cur_daddr
);
694 seq_printf(seq
, " cur_udp_dst: %d cur_udp_src: %d\n",
695 pkt_dev
->cur_udp_dst
, pkt_dev
->cur_udp_src
);
697 seq_printf(seq
, " cur_queue_map: %u\n", pkt_dev
->cur_queue_map
);
699 seq_printf(seq
, " flows: %u\n", pkt_dev
->nflows
);
701 if (pkt_dev
->result
[0])
702 seq_printf(seq
, "Result: %s\n", pkt_dev
->result
);
704 seq_printf(seq
, "Result: Idle\n");
710 static int hex32_arg(const char __user
*user_buffer
, unsigned long maxlen
,
716 for (; i
< maxlen
; i
++) {
719 if (get_user(c
, &user_buffer
[i
]))
721 if ((c
>= '0') && (c
<= '9'))
723 else if ((c
>= 'a') && (c
<= 'f'))
724 *num
|= c
- 'a' + 10;
725 else if ((c
>= 'A') && (c
<= 'F'))
726 *num
|= c
- 'A' + 10;
733 static int count_trail_chars(const char __user
* user_buffer
,
738 for (i
= 0; i
< maxlen
; i
++) {
740 if (get_user(c
, &user_buffer
[i
]))
758 static unsigned long num_arg(const char __user
* user_buffer
,
759 unsigned long maxlen
, unsigned long *num
)
764 for (; i
< maxlen
; i
++) {
766 if (get_user(c
, &user_buffer
[i
]))
768 if ((c
>= '0') && (c
<= '9')) {
777 static int strn_len(const char __user
* user_buffer
, unsigned int maxlen
)
781 for (; i
< maxlen
; i
++) {
783 if (get_user(c
, &user_buffer
[i
]))
801 static ssize_t
get_labels(const char __user
*buffer
, struct pktgen_dev
*pkt_dev
)
808 pkt_dev
->nr_labels
= 0;
811 len
= hex32_arg(&buffer
[i
], 8, &tmp
);
814 pkt_dev
->labels
[n
] = htonl(tmp
);
815 if (pkt_dev
->labels
[n
] & MPLS_STACK_BOTTOM
)
816 pkt_dev
->flags
|= F_MPLS_RND
;
818 if (get_user(c
, &buffer
[i
]))
822 if (n
>= MAX_MPLS_LABELS
)
826 pkt_dev
->nr_labels
= n
;
830 static ssize_t
pktgen_if_write(struct file
*file
,
831 const char __user
* user_buffer
, size_t count
,
834 struct seq_file
*seq
= (struct seq_file
*)file
->private_data
;
835 struct pktgen_dev
*pkt_dev
= seq
->private;
837 char name
[16], valstr
[32];
838 unsigned long value
= 0;
839 char *pg_result
= NULL
;
843 pg_result
= &(pkt_dev
->result
[0]);
846 printk(KERN_WARNING
"pktgen: wrong command format\n");
851 tmp
= count_trail_chars(&user_buffer
[i
], max
);
853 printk(KERN_WARNING
"pktgen: illegal format\n");
858 /* Read variable name */
860 len
= strn_len(&user_buffer
[i
], sizeof(name
) - 1);
864 memset(name
, 0, sizeof(name
));
865 if (copy_from_user(name
, &user_buffer
[i
], len
))
870 len
= count_trail_chars(&user_buffer
[i
], max
);
878 if (copy_from_user(tb
, user_buffer
, count
))
881 printk(KERN_DEBUG
"pktgen: %s,%lu buffer -:%s:-\n", name
,
882 (unsigned long)count
, tb
);
885 if (!strcmp(name
, "min_pkt_size")) {
886 len
= num_arg(&user_buffer
[i
], 10, &value
);
891 if (value
< 14 + 20 + 8)
893 if (value
!= pkt_dev
->min_pkt_size
) {
894 pkt_dev
->min_pkt_size
= value
;
895 pkt_dev
->cur_pkt_size
= value
;
897 sprintf(pg_result
, "OK: min_pkt_size=%u",
898 pkt_dev
->min_pkt_size
);
902 if (!strcmp(name
, "max_pkt_size")) {
903 len
= num_arg(&user_buffer
[i
], 10, &value
);
908 if (value
< 14 + 20 + 8)
910 if (value
!= pkt_dev
->max_pkt_size
) {
911 pkt_dev
->max_pkt_size
= value
;
912 pkt_dev
->cur_pkt_size
= value
;
914 sprintf(pg_result
, "OK: max_pkt_size=%u",
915 pkt_dev
->max_pkt_size
);
919 /* Shortcut for min = max */
921 if (!strcmp(name
, "pkt_size")) {
922 len
= num_arg(&user_buffer
[i
], 10, &value
);
927 if (value
< 14 + 20 + 8)
929 if (value
!= pkt_dev
->min_pkt_size
) {
930 pkt_dev
->min_pkt_size
= value
;
931 pkt_dev
->max_pkt_size
= value
;
932 pkt_dev
->cur_pkt_size
= value
;
934 sprintf(pg_result
, "OK: pkt_size=%u", pkt_dev
->min_pkt_size
);
938 if (!strcmp(name
, "debug")) {
939 len
= num_arg(&user_buffer
[i
], 10, &value
);
945 sprintf(pg_result
, "OK: debug=%u", debug
);
949 if (!strcmp(name
, "frags")) {
950 len
= num_arg(&user_buffer
[i
], 10, &value
);
955 pkt_dev
->nfrags
= value
;
956 sprintf(pg_result
, "OK: frags=%u", pkt_dev
->nfrags
);
959 if (!strcmp(name
, "delay")) {
960 len
= num_arg(&user_buffer
[i
], 10, &value
);
965 if (value
== 0x7FFFFFFF)
966 pkt_dev
->delay
= ULLONG_MAX
;
968 pkt_dev
->delay
= (u64
)value
* NSEC_PER_USEC
;
970 sprintf(pg_result
, "OK: delay=%llu",
971 (unsigned long long) pkt_dev
->delay
);
974 if (!strcmp(name
, "udp_src_min")) {
975 len
= num_arg(&user_buffer
[i
], 10, &value
);
980 if (value
!= pkt_dev
->udp_src_min
) {
981 pkt_dev
->udp_src_min
= value
;
982 pkt_dev
->cur_udp_src
= value
;
984 sprintf(pg_result
, "OK: udp_src_min=%u", pkt_dev
->udp_src_min
);
987 if (!strcmp(name
, "udp_dst_min")) {
988 len
= num_arg(&user_buffer
[i
], 10, &value
);
993 if (value
!= pkt_dev
->udp_dst_min
) {
994 pkt_dev
->udp_dst_min
= value
;
995 pkt_dev
->cur_udp_dst
= value
;
997 sprintf(pg_result
, "OK: udp_dst_min=%u", pkt_dev
->udp_dst_min
);
1000 if (!strcmp(name
, "udp_src_max")) {
1001 len
= num_arg(&user_buffer
[i
], 10, &value
);
1006 if (value
!= pkt_dev
->udp_src_max
) {
1007 pkt_dev
->udp_src_max
= value
;
1008 pkt_dev
->cur_udp_src
= value
;
1010 sprintf(pg_result
, "OK: udp_src_max=%u", pkt_dev
->udp_src_max
);
1013 if (!strcmp(name
, "udp_dst_max")) {
1014 len
= num_arg(&user_buffer
[i
], 10, &value
);
1019 if (value
!= pkt_dev
->udp_dst_max
) {
1020 pkt_dev
->udp_dst_max
= value
;
1021 pkt_dev
->cur_udp_dst
= value
;
1023 sprintf(pg_result
, "OK: udp_dst_max=%u", pkt_dev
->udp_dst_max
);
1026 if (!strcmp(name
, "clone_skb")) {
1027 len
= num_arg(&user_buffer
[i
], 10, &value
);
1032 pkt_dev
->clone_skb
= value
;
1034 sprintf(pg_result
, "OK: clone_skb=%d", pkt_dev
->clone_skb
);
1037 if (!strcmp(name
, "count")) {
1038 len
= num_arg(&user_buffer
[i
], 10, &value
);
1043 pkt_dev
->count
= value
;
1044 sprintf(pg_result
, "OK: count=%llu",
1045 (unsigned long long)pkt_dev
->count
);
1048 if (!strcmp(name
, "src_mac_count")) {
1049 len
= num_arg(&user_buffer
[i
], 10, &value
);
1054 if (pkt_dev
->src_mac_count
!= value
) {
1055 pkt_dev
->src_mac_count
= value
;
1056 pkt_dev
->cur_src_mac_offset
= 0;
1058 sprintf(pg_result
, "OK: src_mac_count=%d",
1059 pkt_dev
->src_mac_count
);
1062 if (!strcmp(name
, "dst_mac_count")) {
1063 len
= num_arg(&user_buffer
[i
], 10, &value
);
1068 if (pkt_dev
->dst_mac_count
!= value
) {
1069 pkt_dev
->dst_mac_count
= value
;
1070 pkt_dev
->cur_dst_mac_offset
= 0;
1072 sprintf(pg_result
, "OK: dst_mac_count=%d",
1073 pkt_dev
->dst_mac_count
);
1076 if (!strcmp(name
, "flag")) {
1079 len
= strn_len(&user_buffer
[i
], sizeof(f
) - 1);
1083 if (copy_from_user(f
, &user_buffer
[i
], len
))
1086 if (strcmp(f
, "IPSRC_RND") == 0)
1087 pkt_dev
->flags
|= F_IPSRC_RND
;
1089 else if (strcmp(f
, "!IPSRC_RND") == 0)
1090 pkt_dev
->flags
&= ~F_IPSRC_RND
;
1092 else if (strcmp(f
, "TXSIZE_RND") == 0)
1093 pkt_dev
->flags
|= F_TXSIZE_RND
;
1095 else if (strcmp(f
, "!TXSIZE_RND") == 0)
1096 pkt_dev
->flags
&= ~F_TXSIZE_RND
;
1098 else if (strcmp(f
, "IPDST_RND") == 0)
1099 pkt_dev
->flags
|= F_IPDST_RND
;
1101 else if (strcmp(f
, "!IPDST_RND") == 0)
1102 pkt_dev
->flags
&= ~F_IPDST_RND
;
1104 else if (strcmp(f
, "UDPSRC_RND") == 0)
1105 pkt_dev
->flags
|= F_UDPSRC_RND
;
1107 else if (strcmp(f
, "!UDPSRC_RND") == 0)
1108 pkt_dev
->flags
&= ~F_UDPSRC_RND
;
1110 else if (strcmp(f
, "UDPDST_RND") == 0)
1111 pkt_dev
->flags
|= F_UDPDST_RND
;
1113 else if (strcmp(f
, "!UDPDST_RND") == 0)
1114 pkt_dev
->flags
&= ~F_UDPDST_RND
;
1116 else if (strcmp(f
, "MACSRC_RND") == 0)
1117 pkt_dev
->flags
|= F_MACSRC_RND
;
1119 else if (strcmp(f
, "!MACSRC_RND") == 0)
1120 pkt_dev
->flags
&= ~F_MACSRC_RND
;
1122 else if (strcmp(f
, "MACDST_RND") == 0)
1123 pkt_dev
->flags
|= F_MACDST_RND
;
1125 else if (strcmp(f
, "!MACDST_RND") == 0)
1126 pkt_dev
->flags
&= ~F_MACDST_RND
;
1128 else if (strcmp(f
, "MPLS_RND") == 0)
1129 pkt_dev
->flags
|= F_MPLS_RND
;
1131 else if (strcmp(f
, "!MPLS_RND") == 0)
1132 pkt_dev
->flags
&= ~F_MPLS_RND
;
1134 else if (strcmp(f
, "VID_RND") == 0)
1135 pkt_dev
->flags
|= F_VID_RND
;
1137 else if (strcmp(f
, "!VID_RND") == 0)
1138 pkt_dev
->flags
&= ~F_VID_RND
;
1140 else if (strcmp(f
, "SVID_RND") == 0)
1141 pkt_dev
->flags
|= F_SVID_RND
;
1143 else if (strcmp(f
, "!SVID_RND") == 0)
1144 pkt_dev
->flags
&= ~F_SVID_RND
;
1146 else if (strcmp(f
, "FLOW_SEQ") == 0)
1147 pkt_dev
->flags
|= F_FLOW_SEQ
;
1149 else if (strcmp(f
, "QUEUE_MAP_RND") == 0)
1150 pkt_dev
->flags
|= F_QUEUE_MAP_RND
;
1152 else if (strcmp(f
, "!QUEUE_MAP_RND") == 0)
1153 pkt_dev
->flags
&= ~F_QUEUE_MAP_RND
;
1155 else if (strcmp(f
, "QUEUE_MAP_CPU") == 0)
1156 pkt_dev
->flags
|= F_QUEUE_MAP_CPU
;
1158 else if (strcmp(f
, "!QUEUE_MAP_CPU") == 0)
1159 pkt_dev
->flags
&= ~F_QUEUE_MAP_CPU
;
1161 else if (strcmp(f
, "IPSEC") == 0)
1162 pkt_dev
->flags
|= F_IPSEC_ON
;
1165 else if (strcmp(f
, "!IPV6") == 0)
1166 pkt_dev
->flags
&= ~F_IPV6
;
1170 "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
1172 "IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
1173 "MACSRC_RND, MACDST_RND, TXSIZE_RND, IPV6, MPLS_RND, VID_RND, SVID_RND, FLOW_SEQ, IPSEC\n");
1176 sprintf(pg_result
, "OK: flags=0x%x", pkt_dev
->flags
);
1179 if (!strcmp(name
, "dst_min") || !strcmp(name
, "dst")) {
1180 len
= strn_len(&user_buffer
[i
], sizeof(pkt_dev
->dst_min
) - 1);
1184 if (copy_from_user(buf
, &user_buffer
[i
], len
))
1187 if (strcmp(buf
, pkt_dev
->dst_min
) != 0) {
1188 memset(pkt_dev
->dst_min
, 0, sizeof(pkt_dev
->dst_min
));
1189 strncpy(pkt_dev
->dst_min
, buf
, len
);
1190 pkt_dev
->daddr_min
= in_aton(pkt_dev
->dst_min
);
1191 pkt_dev
->cur_daddr
= pkt_dev
->daddr_min
;
1194 printk(KERN_DEBUG
"pktgen: dst_min set to: %s\n",
1197 sprintf(pg_result
, "OK: dst_min=%s", pkt_dev
->dst_min
);
1200 if (!strcmp(name
, "dst_max")) {
1201 len
= strn_len(&user_buffer
[i
], sizeof(pkt_dev
->dst_max
) - 1);
1206 if (copy_from_user(buf
, &user_buffer
[i
], len
))
1210 if (strcmp(buf
, pkt_dev
->dst_max
) != 0) {
1211 memset(pkt_dev
->dst_max
, 0, sizeof(pkt_dev
->dst_max
));
1212 strncpy(pkt_dev
->dst_max
, buf
, len
);
1213 pkt_dev
->daddr_max
= in_aton(pkt_dev
->dst_max
);
1214 pkt_dev
->cur_daddr
= pkt_dev
->daddr_max
;
1217 printk(KERN_DEBUG
"pktgen: dst_max set to: %s\n",
1220 sprintf(pg_result
, "OK: dst_max=%s", pkt_dev
->dst_max
);
1223 if (!strcmp(name
, "dst6")) {
1224 len
= strn_len(&user_buffer
[i
], sizeof(buf
) - 1);
1228 pkt_dev
->flags
|= F_IPV6
;
1230 if (copy_from_user(buf
, &user_buffer
[i
], len
))
1234 scan_ip6(buf
, pkt_dev
->in6_daddr
.s6_addr
);
1235 fmt_ip6(buf
, pkt_dev
->in6_daddr
.s6_addr
);
1237 ipv6_addr_copy(&pkt_dev
->cur_in6_daddr
, &pkt_dev
->in6_daddr
);
1240 printk(KERN_DEBUG
"pktgen: dst6 set to: %s\n", buf
);
1243 sprintf(pg_result
, "OK: dst6=%s", buf
);
1246 if (!strcmp(name
, "dst6_min")) {
1247 len
= strn_len(&user_buffer
[i
], sizeof(buf
) - 1);
1251 pkt_dev
->flags
|= F_IPV6
;
1253 if (copy_from_user(buf
, &user_buffer
[i
], len
))
1257 scan_ip6(buf
, pkt_dev
->min_in6_daddr
.s6_addr
);
1258 fmt_ip6(buf
, pkt_dev
->min_in6_daddr
.s6_addr
);
1260 ipv6_addr_copy(&pkt_dev
->cur_in6_daddr
,
1261 &pkt_dev
->min_in6_daddr
);
1263 printk(KERN_DEBUG
"pktgen: dst6_min set to: %s\n", buf
);
1266 sprintf(pg_result
, "OK: dst6_min=%s", buf
);
1269 if (!strcmp(name
, "dst6_max")) {
1270 len
= strn_len(&user_buffer
[i
], sizeof(buf
) - 1);
1274 pkt_dev
->flags
|= F_IPV6
;
1276 if (copy_from_user(buf
, &user_buffer
[i
], len
))
1280 scan_ip6(buf
, pkt_dev
->max_in6_daddr
.s6_addr
);
1281 fmt_ip6(buf
, pkt_dev
->max_in6_daddr
.s6_addr
);
1284 printk(KERN_DEBUG
"pktgen: dst6_max set to: %s\n", buf
);
1287 sprintf(pg_result
, "OK: dst6_max=%s", buf
);
1290 if (!strcmp(name
, "src6")) {
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_saddr
.s6_addr
);
1302 fmt_ip6(buf
, pkt_dev
->in6_saddr
.s6_addr
);
1304 ipv6_addr_copy(&pkt_dev
->cur_in6_saddr
, &pkt_dev
->in6_saddr
);
1307 printk(KERN_DEBUG
"pktgen: src6 set to: %s\n", buf
);
1310 sprintf(pg_result
, "OK: src6=%s", buf
);
1313 if (!strcmp(name
, "src_min")) {
1314 len
= strn_len(&user_buffer
[i
], sizeof(pkt_dev
->src_min
) - 1);
1318 if (copy_from_user(buf
, &user_buffer
[i
], len
))
1321 if (strcmp(buf
, pkt_dev
->src_min
) != 0) {
1322 memset(pkt_dev
->src_min
, 0, sizeof(pkt_dev
->src_min
));
1323 strncpy(pkt_dev
->src_min
, buf
, len
);
1324 pkt_dev
->saddr_min
= in_aton(pkt_dev
->src_min
);
1325 pkt_dev
->cur_saddr
= pkt_dev
->saddr_min
;
1328 printk(KERN_DEBUG
"pktgen: src_min set to: %s\n",
1331 sprintf(pg_result
, "OK: src_min=%s", pkt_dev
->src_min
);
1334 if (!strcmp(name
, "src_max")) {
1335 len
= strn_len(&user_buffer
[i
], sizeof(pkt_dev
->src_max
) - 1);
1339 if (copy_from_user(buf
, &user_buffer
[i
], len
))
1342 if (strcmp(buf
, pkt_dev
->src_max
) != 0) {
1343 memset(pkt_dev
->src_max
, 0, sizeof(pkt_dev
->src_max
));
1344 strncpy(pkt_dev
->src_max
, buf
, len
);
1345 pkt_dev
->saddr_max
= in_aton(pkt_dev
->src_max
);
1346 pkt_dev
->cur_saddr
= pkt_dev
->saddr_max
;
1349 printk(KERN_DEBUG
"pktgen: src_max set to: %s\n",
1352 sprintf(pg_result
, "OK: src_max=%s", pkt_dev
->src_max
);
1355 if (!strcmp(name
, "dst_mac")) {
1357 unsigned char old_dmac
[ETH_ALEN
];
1358 unsigned char *m
= pkt_dev
->dst_mac
;
1359 memcpy(old_dmac
, pkt_dev
->dst_mac
, ETH_ALEN
);
1361 len
= strn_len(&user_buffer
[i
], sizeof(valstr
) - 1);
1365 memset(valstr
, 0, sizeof(valstr
));
1366 if (copy_from_user(valstr
, &user_buffer
[i
], len
))
1370 for (*m
= 0; *v
&& m
< pkt_dev
->dst_mac
+ 6; v
++) {
1371 if (*v
>= '0' && *v
<= '9') {
1375 if (*v
>= 'A' && *v
<= 'F') {
1377 *m
+= *v
- 'A' + 10;
1379 if (*v
>= 'a' && *v
<= 'f') {
1381 *m
+= *v
- 'a' + 10;
1389 /* Set up Dest MAC */
1390 if (compare_ether_addr(old_dmac
, pkt_dev
->dst_mac
))
1391 memcpy(&(pkt_dev
->hh
[0]), pkt_dev
->dst_mac
, ETH_ALEN
);
1393 sprintf(pg_result
, "OK: dstmac");
1396 if (!strcmp(name
, "src_mac")) {
1398 unsigned char old_smac
[ETH_ALEN
];
1399 unsigned char *m
= pkt_dev
->src_mac
;
1401 memcpy(old_smac
, pkt_dev
->src_mac
, ETH_ALEN
);
1403 len
= strn_len(&user_buffer
[i
], sizeof(valstr
) - 1);
1407 memset(valstr
, 0, sizeof(valstr
));
1408 if (copy_from_user(valstr
, &user_buffer
[i
], len
))
1412 for (*m
= 0; *v
&& m
< pkt_dev
->src_mac
+ 6; v
++) {
1413 if (*v
>= '0' && *v
<= '9') {
1417 if (*v
>= 'A' && *v
<= 'F') {
1419 *m
+= *v
- 'A' + 10;
1421 if (*v
>= 'a' && *v
<= 'f') {
1423 *m
+= *v
- 'a' + 10;
1431 /* Set up Src MAC */
1432 if (compare_ether_addr(old_smac
, pkt_dev
->src_mac
))
1433 memcpy(&(pkt_dev
->hh
[6]), pkt_dev
->src_mac
, ETH_ALEN
);
1435 sprintf(pg_result
, "OK: srcmac");
1439 if (!strcmp(name
, "clear_counters")) {
1440 pktgen_clear_counters(pkt_dev
);
1441 sprintf(pg_result
, "OK: Clearing counters.\n");
1445 if (!strcmp(name
, "flows")) {
1446 len
= num_arg(&user_buffer
[i
], 10, &value
);
1451 if (value
> MAX_CFLOWS
)
1454 pkt_dev
->cflows
= value
;
1455 sprintf(pg_result
, "OK: flows=%u", pkt_dev
->cflows
);
1459 if (!strcmp(name
, "flowlen")) {
1460 len
= num_arg(&user_buffer
[i
], 10, &value
);
1465 pkt_dev
->lflow
= value
;
1466 sprintf(pg_result
, "OK: flowlen=%u", pkt_dev
->lflow
);
1470 if (!strcmp(name
, "queue_map_min")) {
1471 len
= num_arg(&user_buffer
[i
], 5, &value
);
1476 pkt_dev
->queue_map_min
= value
;
1477 sprintf(pg_result
, "OK: queue_map_min=%u", pkt_dev
->queue_map_min
);
1481 if (!strcmp(name
, "queue_map_max")) {
1482 len
= num_arg(&user_buffer
[i
], 5, &value
);
1487 pkt_dev
->queue_map_max
= value
;
1488 sprintf(pg_result
, "OK: queue_map_max=%u", pkt_dev
->queue_map_max
);
1492 if (!strcmp(name
, "mpls")) {
1495 len
= get_labels(&user_buffer
[i
], pkt_dev
);
1499 cnt
= sprintf(pg_result
, "OK: mpls=");
1500 for (n
= 0; n
< pkt_dev
->nr_labels
; n
++)
1501 cnt
+= sprintf(pg_result
+ cnt
,
1502 "%08x%s", ntohl(pkt_dev
->labels
[n
]),
1503 n
== pkt_dev
->nr_labels
-1 ? "" : ",");
1505 if (pkt_dev
->nr_labels
&& pkt_dev
->vlan_id
!= 0xffff) {
1506 pkt_dev
->vlan_id
= 0xffff; /* turn off VLAN/SVLAN */
1507 pkt_dev
->svlan_id
= 0xffff;
1510 printk(KERN_DEBUG
"pktgen: VLAN/SVLAN auto turned off\n");
1515 if (!strcmp(name
, "vlan_id")) {
1516 len
= num_arg(&user_buffer
[i
], 4, &value
);
1521 if (value
<= 4095) {
1522 pkt_dev
->vlan_id
= value
; /* turn on VLAN */
1525 printk(KERN_DEBUG
"pktgen: VLAN turned on\n");
1527 if (debug
&& pkt_dev
->nr_labels
)
1528 printk(KERN_DEBUG
"pktgen: MPLS auto turned off\n");
1530 pkt_dev
->nr_labels
= 0; /* turn off MPLS */
1531 sprintf(pg_result
, "OK: vlan_id=%u", pkt_dev
->vlan_id
);
1533 pkt_dev
->vlan_id
= 0xffff; /* turn off VLAN/SVLAN */
1534 pkt_dev
->svlan_id
= 0xffff;
1537 printk(KERN_DEBUG
"pktgen: VLAN/SVLAN turned off\n");
1542 if (!strcmp(name
, "vlan_p")) {
1543 len
= num_arg(&user_buffer
[i
], 1, &value
);
1548 if ((value
<= 7) && (pkt_dev
->vlan_id
!= 0xffff)) {
1549 pkt_dev
->vlan_p
= value
;
1550 sprintf(pg_result
, "OK: vlan_p=%u", pkt_dev
->vlan_p
);
1552 sprintf(pg_result
, "ERROR: vlan_p must be 0-7");
1557 if (!strcmp(name
, "vlan_cfi")) {
1558 len
= num_arg(&user_buffer
[i
], 1, &value
);
1563 if ((value
<= 1) && (pkt_dev
->vlan_id
!= 0xffff)) {
1564 pkt_dev
->vlan_cfi
= value
;
1565 sprintf(pg_result
, "OK: vlan_cfi=%u", pkt_dev
->vlan_cfi
);
1567 sprintf(pg_result
, "ERROR: vlan_cfi must be 0-1");
1572 if (!strcmp(name
, "svlan_id")) {
1573 len
= num_arg(&user_buffer
[i
], 4, &value
);
1578 if ((value
<= 4095) && ((pkt_dev
->vlan_id
!= 0xffff))) {
1579 pkt_dev
->svlan_id
= value
; /* turn on SVLAN */
1582 printk(KERN_DEBUG
"pktgen: SVLAN turned on\n");
1584 if (debug
&& pkt_dev
->nr_labels
)
1585 printk(KERN_DEBUG
"pktgen: MPLS auto turned off\n");
1587 pkt_dev
->nr_labels
= 0; /* turn off MPLS */
1588 sprintf(pg_result
, "OK: svlan_id=%u", pkt_dev
->svlan_id
);
1590 pkt_dev
->vlan_id
= 0xffff; /* turn off VLAN/SVLAN */
1591 pkt_dev
->svlan_id
= 0xffff;
1594 printk(KERN_DEBUG
"pktgen: VLAN/SVLAN turned off\n");
1599 if (!strcmp(name
, "svlan_p")) {
1600 len
= num_arg(&user_buffer
[i
], 1, &value
);
1605 if ((value
<= 7) && (pkt_dev
->svlan_id
!= 0xffff)) {
1606 pkt_dev
->svlan_p
= value
;
1607 sprintf(pg_result
, "OK: svlan_p=%u", pkt_dev
->svlan_p
);
1609 sprintf(pg_result
, "ERROR: svlan_p must be 0-7");
1614 if (!strcmp(name
, "svlan_cfi")) {
1615 len
= num_arg(&user_buffer
[i
], 1, &value
);
1620 if ((value
<= 1) && (pkt_dev
->svlan_id
!= 0xffff)) {
1621 pkt_dev
->svlan_cfi
= value
;
1622 sprintf(pg_result
, "OK: svlan_cfi=%u", pkt_dev
->svlan_cfi
);
1624 sprintf(pg_result
, "ERROR: svlan_cfi must be 0-1");
1629 if (!strcmp(name
, "tos")) {
1630 __u32 tmp_value
= 0;
1631 len
= hex32_arg(&user_buffer
[i
], 2, &tmp_value
);
1637 pkt_dev
->tos
= tmp_value
;
1638 sprintf(pg_result
, "OK: tos=0x%02x", pkt_dev
->tos
);
1640 sprintf(pg_result
, "ERROR: tos must be 00-ff");
1645 if (!strcmp(name
, "traffic_class")) {
1646 __u32 tmp_value
= 0;
1647 len
= hex32_arg(&user_buffer
[i
], 2, &tmp_value
);
1653 pkt_dev
->traffic_class
= tmp_value
;
1654 sprintf(pg_result
, "OK: traffic_class=0x%02x", pkt_dev
->traffic_class
);
1656 sprintf(pg_result
, "ERROR: traffic_class must be 00-ff");
1661 sprintf(pkt_dev
->result
, "No such parameter \"%s\"", name
);
1665 static int pktgen_if_open(struct inode
*inode
, struct file
*file
)
1667 return single_open(file
, pktgen_if_show
, PDE(inode
)->data
);
1670 static const struct file_operations pktgen_if_fops
= {
1671 .owner
= THIS_MODULE
,
1672 .open
= pktgen_if_open
,
1674 .llseek
= seq_lseek
,
1675 .write
= pktgen_if_write
,
1676 .release
= single_release
,
1679 static int pktgen_thread_show(struct seq_file
*seq
, void *v
)
1681 struct pktgen_thread
*t
= seq
->private;
1682 const struct pktgen_dev
*pkt_dev
;
1686 seq_printf(seq
, "Running: ");
1689 list_for_each_entry(pkt_dev
, &t
->if_list
, list
)
1690 if (pkt_dev
->running
)
1691 seq_printf(seq
, "%s ", pkt_dev
->odev
->name
);
1693 seq_printf(seq
, "\nStopped: ");
1695 list_for_each_entry(pkt_dev
, &t
->if_list
, list
)
1696 if (!pkt_dev
->running
)
1697 seq_printf(seq
, "%s ", pkt_dev
->odev
->name
);
1700 seq_printf(seq
, "\nResult: %s\n", t
->result
);
1702 seq_printf(seq
, "\nResult: NA\n");
1709 static ssize_t
pktgen_thread_write(struct file
*file
,
1710 const char __user
* user_buffer
,
1711 size_t count
, loff_t
* offset
)
1713 struct seq_file
*seq
= (struct seq_file
*)file
->private_data
;
1714 struct pktgen_thread
*t
= seq
->private;
1715 int i
= 0, max
, len
, ret
;
1720 // sprintf(pg_result, "Wrong command format");
1725 len
= count_trail_chars(&user_buffer
[i
], max
);
1731 /* Read variable name */
1733 len
= strn_len(&user_buffer
[i
], sizeof(name
) - 1);
1737 memset(name
, 0, sizeof(name
));
1738 if (copy_from_user(name
, &user_buffer
[i
], len
))
1743 len
= count_trail_chars(&user_buffer
[i
], max
);
1750 printk(KERN_DEBUG
"pktgen: t=%s, count=%lu\n",
1751 name
, (unsigned long)count
);
1754 printk(KERN_ERR
"pktgen: ERROR: No thread\n");
1759 pg_result
= &(t
->result
[0]);
1761 if (!strcmp(name
, "add_device")) {
1764 len
= strn_len(&user_buffer
[i
], sizeof(f
) - 1);
1769 if (copy_from_user(f
, &user_buffer
[i
], len
))
1772 mutex_lock(&pktgen_thread_lock
);
1773 pktgen_add_device(t
, f
);
1774 mutex_unlock(&pktgen_thread_lock
);
1776 sprintf(pg_result
, "OK: add_device=%s", f
);
1780 if (!strcmp(name
, "rem_device_all")) {
1781 mutex_lock(&pktgen_thread_lock
);
1782 t
->control
|= T_REMDEVALL
;
1783 mutex_unlock(&pktgen_thread_lock
);
1784 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
1786 sprintf(pg_result
, "OK: rem_device_all");
1790 if (!strcmp(name
, "max_before_softirq")) {
1791 sprintf(pg_result
, "OK: Note! max_before_softirq is obsoleted -- Do not use");
1801 static int pktgen_thread_open(struct inode
*inode
, struct file
*file
)
1803 return single_open(file
, pktgen_thread_show
, PDE(inode
)->data
);
1806 static const struct file_operations pktgen_thread_fops
= {
1807 .owner
= THIS_MODULE
,
1808 .open
= pktgen_thread_open
,
1810 .llseek
= seq_lseek
,
1811 .write
= pktgen_thread_write
,
1812 .release
= single_release
,
1815 /* Think find or remove for NN */
1816 static struct pktgen_dev
*__pktgen_NN_threads(const char *ifname
, int remove
)
1818 struct pktgen_thread
*t
;
1819 struct pktgen_dev
*pkt_dev
= NULL
;
1821 list_for_each_entry(t
, &pktgen_threads
, th_list
) {
1822 pkt_dev
= pktgen_find_dev(t
, ifname
);
1826 pkt_dev
->removal_mark
= 1;
1827 t
->control
|= T_REMDEV
;
1837 * mark a device for removal
1839 static void pktgen_mark_device(const char *ifname
)
1841 struct pktgen_dev
*pkt_dev
= NULL
;
1842 const int max_tries
= 10, msec_per_try
= 125;
1845 mutex_lock(&pktgen_thread_lock
);
1846 pr_debug("pktgen: pktgen_mark_device marking %s for removal\n", ifname
);
1850 pkt_dev
= __pktgen_NN_threads(ifname
, REMOVE
);
1851 if (pkt_dev
== NULL
)
1852 break; /* success */
1854 mutex_unlock(&pktgen_thread_lock
);
1855 pr_debug("pktgen: pktgen_mark_device waiting for %s "
1856 "to disappear....\n", ifname
);
1857 schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try
));
1858 mutex_lock(&pktgen_thread_lock
);
1860 if (++i
>= max_tries
) {
1861 printk(KERN_ERR
"pktgen_mark_device: timed out after "
1862 "waiting %d msec for device %s to be removed\n",
1863 msec_per_try
* i
, ifname
);
1869 mutex_unlock(&pktgen_thread_lock
);
1872 static void pktgen_change_name(struct net_device
*dev
)
1874 struct pktgen_thread
*t
;
1876 list_for_each_entry(t
, &pktgen_threads
, th_list
) {
1877 struct pktgen_dev
*pkt_dev
;
1879 list_for_each_entry(pkt_dev
, &t
->if_list
, list
) {
1880 if (pkt_dev
->odev
!= dev
)
1883 remove_proc_entry(pkt_dev
->entry
->name
, pg_proc_dir
);
1885 pkt_dev
->entry
= proc_create_data(dev
->name
, 0600,
1889 if (!pkt_dev
->entry
)
1890 printk(KERN_ERR
"pktgen: can't move proc "
1891 " entry for '%s'\n", dev
->name
);
1897 static int pktgen_device_event(struct notifier_block
*unused
,
1898 unsigned long event
, void *ptr
)
1900 struct net_device
*dev
= ptr
;
1902 if (!net_eq(dev_net(dev
), &init_net
))
1905 /* It is OK that we do not hold the group lock right now,
1906 * as we run under the RTNL lock.
1910 case NETDEV_CHANGENAME
:
1911 pktgen_change_name(dev
);
1914 case NETDEV_UNREGISTER
:
1915 pktgen_mark_device(dev
->name
);
1922 static struct net_device
*pktgen_dev_get_by_name(struct pktgen_dev
*pkt_dev
,
1928 for (i
= 0; ifname
[i
] != '@'; i
++) {
1936 return dev_get_by_name(&init_net
, b
);
1940 /* Associate pktgen_dev with a device. */
1942 static int pktgen_setup_dev(struct pktgen_dev
*pkt_dev
, const char *ifname
)
1944 struct net_device
*odev
;
1947 /* Clean old setups */
1948 if (pkt_dev
->odev
) {
1949 dev_put(pkt_dev
->odev
);
1950 pkt_dev
->odev
= NULL
;
1953 odev
= pktgen_dev_get_by_name(pkt_dev
, ifname
);
1955 printk(KERN_ERR
"pktgen: no such netdevice: \"%s\"\n", ifname
);
1959 if (odev
->type
!= ARPHRD_ETHER
) {
1960 printk(KERN_ERR
"pktgen: not an ethernet device: \"%s\"\n", ifname
);
1962 } else if (!netif_running(odev
)) {
1963 printk(KERN_ERR
"pktgen: device is down: \"%s\"\n", ifname
);
1966 pkt_dev
->odev
= odev
;
1974 /* Read pkt_dev from the interface and set up internal pktgen_dev
1975 * structure to have the right information to create/send packets
1977 static void pktgen_setup_inject(struct pktgen_dev
*pkt_dev
)
1981 if (!pkt_dev
->odev
) {
1982 printk(KERN_ERR
"pktgen: ERROR: pkt_dev->odev == NULL in "
1984 sprintf(pkt_dev
->result
,
1985 "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
1989 /* make sure that we don't pick a non-existing transmit queue */
1990 ntxq
= pkt_dev
->odev
->real_num_tx_queues
;
1992 if (ntxq
<= pkt_dev
->queue_map_min
) {
1993 printk(KERN_WARNING
"pktgen: WARNING: Requested "
1994 "queue_map_min (zero-based) (%d) exceeds valid range "
1995 "[0 - %d] for (%d) queues on %s, resetting\n",
1996 pkt_dev
->queue_map_min
, (ntxq
?: 1) - 1, ntxq
,
1997 pkt_dev
->odev
->name
);
1998 pkt_dev
->queue_map_min
= ntxq
- 1;
2000 if (pkt_dev
->queue_map_max
>= ntxq
) {
2001 printk(KERN_WARNING
"pktgen: WARNING: Requested "
2002 "queue_map_max (zero-based) (%d) exceeds valid range "
2003 "[0 - %d] for (%d) queues on %s, resetting\n",
2004 pkt_dev
->queue_map_max
, (ntxq
?: 1) - 1, ntxq
,
2005 pkt_dev
->odev
->name
);
2006 pkt_dev
->queue_map_max
= ntxq
- 1;
2009 /* Default to the interface's mac if not explicitly set. */
2011 if (is_zero_ether_addr(pkt_dev
->src_mac
))
2012 memcpy(&(pkt_dev
->hh
[6]), pkt_dev
->odev
->dev_addr
, ETH_ALEN
);
2014 /* Set up Dest MAC */
2015 memcpy(&(pkt_dev
->hh
[0]), pkt_dev
->dst_mac
, ETH_ALEN
);
2017 /* Set up pkt size */
2018 pkt_dev
->cur_pkt_size
= pkt_dev
->min_pkt_size
;
2020 if (pkt_dev
->flags
& F_IPV6
) {
2022 * Skip this automatic address setting until locks or functions
2027 int i
, set
= 0, err
= 1;
2028 struct inet6_dev
*idev
;
2030 for (i
= 0; i
< IN6_ADDR_HSIZE
; i
++)
2031 if (pkt_dev
->cur_in6_saddr
.s6_addr
[i
]) {
2039 * Use linklevel address if unconfigured.
2041 * use ipv6_get_lladdr if/when it's get exported
2045 idev
= __in6_dev_get(pkt_dev
->odev
);
2047 struct inet6_ifaddr
*ifp
;
2049 read_lock_bh(&idev
->lock
);
2050 for (ifp
= idev
->addr_list
; ifp
;
2051 ifp
= ifp
->if_next
) {
2052 if (ifp
->scope
== IFA_LINK
2054 flags
& IFA_F_TENTATIVE
)) {
2055 ipv6_addr_copy(&pkt_dev
->
2062 read_unlock_bh(&idev
->lock
);
2066 printk(KERN_ERR
"pktgen: ERROR: IPv6 link "
2067 "address not availble.\n");
2071 pkt_dev
->saddr_min
= 0;
2072 pkt_dev
->saddr_max
= 0;
2073 if (strlen(pkt_dev
->src_min
) == 0) {
2075 struct in_device
*in_dev
;
2078 in_dev
= __in_dev_get_rcu(pkt_dev
->odev
);
2080 if (in_dev
->ifa_list
) {
2081 pkt_dev
->saddr_min
=
2082 in_dev
->ifa_list
->ifa_address
;
2083 pkt_dev
->saddr_max
= pkt_dev
->saddr_min
;
2088 pkt_dev
->saddr_min
= in_aton(pkt_dev
->src_min
);
2089 pkt_dev
->saddr_max
= in_aton(pkt_dev
->src_max
);
2092 pkt_dev
->daddr_min
= in_aton(pkt_dev
->dst_min
);
2093 pkt_dev
->daddr_max
= in_aton(pkt_dev
->dst_max
);
2095 /* Initialize current values. */
2096 pkt_dev
->cur_dst_mac_offset
= 0;
2097 pkt_dev
->cur_src_mac_offset
= 0;
2098 pkt_dev
->cur_saddr
= pkt_dev
->saddr_min
;
2099 pkt_dev
->cur_daddr
= pkt_dev
->daddr_min
;
2100 pkt_dev
->cur_udp_dst
= pkt_dev
->udp_dst_min
;
2101 pkt_dev
->cur_udp_src
= pkt_dev
->udp_src_min
;
2102 pkt_dev
->nflows
= 0;
2106 static void spin(struct pktgen_dev
*pkt_dev
, ktime_t spin_until
)
2110 struct hrtimer_sleeper t
;
2112 hrtimer_init_on_stack(&t
.timer
, CLOCK_MONOTONIC
, HRTIMER_MODE_ABS
);
2113 hrtimer_set_expires(&t
.timer
, spin_until
);
2115 remaining
= ktime_to_us(hrtimer_expires_remaining(&t
.timer
));
2119 start
= ktime_now();
2120 if (remaining
< 100)
2121 udelay(remaining
); /* really small just spin */
2123 /* see do_nanosleep */
2124 hrtimer_init_sleeper(&t
, current
);
2126 set_current_state(TASK_INTERRUPTIBLE
);
2127 hrtimer_start_expires(&t
.timer
, HRTIMER_MODE_ABS
);
2128 if (!hrtimer_active(&t
.timer
))
2134 hrtimer_cancel(&t
.timer
);
2135 } while (t
.task
&& pkt_dev
->running
&& !signal_pending(current
));
2136 __set_current_state(TASK_RUNNING
);
2138 pkt_dev
->idle_acc
+= ktime_to_ns(ktime_sub(ktime_now(), start
));
2141 static inline void set_pkt_overhead(struct pktgen_dev
*pkt_dev
)
2143 pkt_dev
->pkt_overhead
= 0;
2144 pkt_dev
->pkt_overhead
+= pkt_dev
->nr_labels
*sizeof(u32
);
2145 pkt_dev
->pkt_overhead
+= VLAN_TAG_SIZE(pkt_dev
);
2146 pkt_dev
->pkt_overhead
+= SVLAN_TAG_SIZE(pkt_dev
);
2149 static inline int f_seen(const struct pktgen_dev
*pkt_dev
, int flow
)
2151 return !!(pkt_dev
->flows
[flow
].flags
& F_INIT
);
2154 static inline int f_pick(struct pktgen_dev
*pkt_dev
)
2156 int flow
= pkt_dev
->curfl
;
2158 if (pkt_dev
->flags
& F_FLOW_SEQ
) {
2159 if (pkt_dev
->flows
[flow
].count
>= pkt_dev
->lflow
) {
2161 pkt_dev
->flows
[flow
].count
= 0;
2162 pkt_dev
->flows
[flow
].flags
= 0;
2163 pkt_dev
->curfl
+= 1;
2164 if (pkt_dev
->curfl
>= pkt_dev
->cflows
)
2165 pkt_dev
->curfl
= 0; /*reset */
2168 flow
= random32() % pkt_dev
->cflows
;
2169 pkt_dev
->curfl
= flow
;
2171 if (pkt_dev
->flows
[flow
].count
> pkt_dev
->lflow
) {
2172 pkt_dev
->flows
[flow
].count
= 0;
2173 pkt_dev
->flows
[flow
].flags
= 0;
2177 return pkt_dev
->curfl
;
2182 /* If there was already an IPSEC SA, we keep it as is, else
2183 * we go look for it ...
2185 static void get_ipsec_sa(struct pktgen_dev
*pkt_dev
, int flow
)
2187 struct xfrm_state
*x
= pkt_dev
->flows
[flow
].x
;
2189 /*slow path: we dont already have xfrm_state*/
2190 x
= xfrm_stateonly_find(&init_net
,
2191 (xfrm_address_t
*)&pkt_dev
->cur_daddr
,
2192 (xfrm_address_t
*)&pkt_dev
->cur_saddr
,
2195 pkt_dev
->ipsproto
, 0);
2197 pkt_dev
->flows
[flow
].x
= x
;
2198 set_pkt_overhead(pkt_dev
);
2199 pkt_dev
->pkt_overhead
+= x
->props
.header_len
;
2205 static void set_cur_queue_map(struct pktgen_dev
*pkt_dev
)
2208 if (pkt_dev
->flags
& F_QUEUE_MAP_CPU
)
2209 pkt_dev
->cur_queue_map
= smp_processor_id();
2211 else if (pkt_dev
->queue_map_min
< pkt_dev
->queue_map_max
) {
2213 if (pkt_dev
->flags
& F_QUEUE_MAP_RND
) {
2215 (pkt_dev
->queue_map_max
-
2216 pkt_dev
->queue_map_min
+ 1)
2217 + pkt_dev
->queue_map_min
;
2219 t
= pkt_dev
->cur_queue_map
+ 1;
2220 if (t
> pkt_dev
->queue_map_max
)
2221 t
= pkt_dev
->queue_map_min
;
2223 pkt_dev
->cur_queue_map
= t
;
2225 pkt_dev
->cur_queue_map
= pkt_dev
->cur_queue_map
% pkt_dev
->odev
->real_num_tx_queues
;
2228 /* Increment/randomize headers according to flags and current values
2229 * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
2231 static void mod_cur_headers(struct pktgen_dev
*pkt_dev
)
2237 if (pkt_dev
->cflows
)
2238 flow
= f_pick(pkt_dev
);
2240 /* Deal with source MAC */
2241 if (pkt_dev
->src_mac_count
> 1) {
2245 if (pkt_dev
->flags
& F_MACSRC_RND
)
2246 mc
= random32() % pkt_dev
->src_mac_count
;
2248 mc
= pkt_dev
->cur_src_mac_offset
++;
2249 if (pkt_dev
->cur_src_mac_offset
>=
2250 pkt_dev
->src_mac_count
)
2251 pkt_dev
->cur_src_mac_offset
= 0;
2254 tmp
= pkt_dev
->src_mac
[5] + (mc
& 0xFF);
2255 pkt_dev
->hh
[11] = tmp
;
2256 tmp
= (pkt_dev
->src_mac
[4] + ((mc
>> 8) & 0xFF) + (tmp
>> 8));
2257 pkt_dev
->hh
[10] = tmp
;
2258 tmp
= (pkt_dev
->src_mac
[3] + ((mc
>> 16) & 0xFF) + (tmp
>> 8));
2259 pkt_dev
->hh
[9] = tmp
;
2260 tmp
= (pkt_dev
->src_mac
[2] + ((mc
>> 24) & 0xFF) + (tmp
>> 8));
2261 pkt_dev
->hh
[8] = tmp
;
2262 tmp
= (pkt_dev
->src_mac
[1] + (tmp
>> 8));
2263 pkt_dev
->hh
[7] = tmp
;
2266 /* Deal with Destination MAC */
2267 if (pkt_dev
->dst_mac_count
> 1) {
2271 if (pkt_dev
->flags
& F_MACDST_RND
)
2272 mc
= random32() % pkt_dev
->dst_mac_count
;
2275 mc
= pkt_dev
->cur_dst_mac_offset
++;
2276 if (pkt_dev
->cur_dst_mac_offset
>=
2277 pkt_dev
->dst_mac_count
) {
2278 pkt_dev
->cur_dst_mac_offset
= 0;
2282 tmp
= pkt_dev
->dst_mac
[5] + (mc
& 0xFF);
2283 pkt_dev
->hh
[5] = tmp
;
2284 tmp
= (pkt_dev
->dst_mac
[4] + ((mc
>> 8) & 0xFF) + (tmp
>> 8));
2285 pkt_dev
->hh
[4] = tmp
;
2286 tmp
= (pkt_dev
->dst_mac
[3] + ((mc
>> 16) & 0xFF) + (tmp
>> 8));
2287 pkt_dev
->hh
[3] = tmp
;
2288 tmp
= (pkt_dev
->dst_mac
[2] + ((mc
>> 24) & 0xFF) + (tmp
>> 8));
2289 pkt_dev
->hh
[2] = tmp
;
2290 tmp
= (pkt_dev
->dst_mac
[1] + (tmp
>> 8));
2291 pkt_dev
->hh
[1] = tmp
;
2294 if (pkt_dev
->flags
& F_MPLS_RND
) {
2296 for (i
= 0; i
< pkt_dev
->nr_labels
; i
++)
2297 if (pkt_dev
->labels
[i
] & MPLS_STACK_BOTTOM
)
2298 pkt_dev
->labels
[i
] = MPLS_STACK_BOTTOM
|
2299 ((__force __be32
)random32() &
2303 if ((pkt_dev
->flags
& F_VID_RND
) && (pkt_dev
->vlan_id
!= 0xffff)) {
2304 pkt_dev
->vlan_id
= random32() & (4096-1);
2307 if ((pkt_dev
->flags
& F_SVID_RND
) && (pkt_dev
->svlan_id
!= 0xffff)) {
2308 pkt_dev
->svlan_id
= random32() & (4096 - 1);
2311 if (pkt_dev
->udp_src_min
< pkt_dev
->udp_src_max
) {
2312 if (pkt_dev
->flags
& F_UDPSRC_RND
)
2313 pkt_dev
->cur_udp_src
= random32() %
2314 (pkt_dev
->udp_src_max
- pkt_dev
->udp_src_min
)
2315 + pkt_dev
->udp_src_min
;
2318 pkt_dev
->cur_udp_src
++;
2319 if (pkt_dev
->cur_udp_src
>= pkt_dev
->udp_src_max
)
2320 pkt_dev
->cur_udp_src
= pkt_dev
->udp_src_min
;
2324 if (pkt_dev
->udp_dst_min
< pkt_dev
->udp_dst_max
) {
2325 if (pkt_dev
->flags
& F_UDPDST_RND
) {
2326 pkt_dev
->cur_udp_dst
= random32() %
2327 (pkt_dev
->udp_dst_max
- pkt_dev
->udp_dst_min
)
2328 + pkt_dev
->udp_dst_min
;
2330 pkt_dev
->cur_udp_dst
++;
2331 if (pkt_dev
->cur_udp_dst
>= pkt_dev
->udp_dst_max
)
2332 pkt_dev
->cur_udp_dst
= pkt_dev
->udp_dst_min
;
2336 if (!(pkt_dev
->flags
& F_IPV6
)) {
2338 imn
= ntohl(pkt_dev
->saddr_min
);
2339 imx
= ntohl(pkt_dev
->saddr_max
);
2342 if (pkt_dev
->flags
& F_IPSRC_RND
)
2343 t
= random32() % (imx
- imn
) + imn
;
2345 t
= ntohl(pkt_dev
->cur_saddr
);
2351 pkt_dev
->cur_saddr
= htonl(t
);
2354 if (pkt_dev
->cflows
&& f_seen(pkt_dev
, flow
)) {
2355 pkt_dev
->cur_daddr
= pkt_dev
->flows
[flow
].cur_daddr
;
2357 imn
= ntohl(pkt_dev
->daddr_min
);
2358 imx
= ntohl(pkt_dev
->daddr_max
);
2362 if (pkt_dev
->flags
& F_IPDST_RND
) {
2364 t
= random32() % (imx
- imn
) + imn
;
2367 while (ipv4_is_loopback(s
) ||
2368 ipv4_is_multicast(s
) ||
2369 ipv4_is_lbcast(s
) ||
2370 ipv4_is_zeronet(s
) ||
2371 ipv4_is_local_multicast(s
)) {
2372 t
= random32() % (imx
- imn
) + imn
;
2375 pkt_dev
->cur_daddr
= s
;
2377 t
= ntohl(pkt_dev
->cur_daddr
);
2382 pkt_dev
->cur_daddr
= htonl(t
);
2385 if (pkt_dev
->cflows
) {
2386 pkt_dev
->flows
[flow
].flags
|= F_INIT
;
2387 pkt_dev
->flows
[flow
].cur_daddr
=
2390 if (pkt_dev
->flags
& F_IPSEC_ON
)
2391 get_ipsec_sa(pkt_dev
, flow
);
2396 } else { /* IPV6 * */
2398 if (pkt_dev
->min_in6_daddr
.s6_addr32
[0] == 0 &&
2399 pkt_dev
->min_in6_daddr
.s6_addr32
[1] == 0 &&
2400 pkt_dev
->min_in6_daddr
.s6_addr32
[2] == 0 &&
2401 pkt_dev
->min_in6_daddr
.s6_addr32
[3] == 0) ;
2405 /* Only random destinations yet */
2407 for (i
= 0; i
< 4; i
++) {
2408 pkt_dev
->cur_in6_daddr
.s6_addr32
[i
] =
2409 (((__force __be32
)random32() |
2410 pkt_dev
->min_in6_daddr
.s6_addr32
[i
]) &
2411 pkt_dev
->max_in6_daddr
.s6_addr32
[i
]);
2416 if (pkt_dev
->min_pkt_size
< pkt_dev
->max_pkt_size
) {
2418 if (pkt_dev
->flags
& F_TXSIZE_RND
) {
2420 (pkt_dev
->max_pkt_size
- pkt_dev
->min_pkt_size
)
2421 + pkt_dev
->min_pkt_size
;
2423 t
= pkt_dev
->cur_pkt_size
+ 1;
2424 if (t
> pkt_dev
->max_pkt_size
)
2425 t
= pkt_dev
->min_pkt_size
;
2427 pkt_dev
->cur_pkt_size
= t
;
2430 set_cur_queue_map(pkt_dev
);
2432 pkt_dev
->flows
[flow
].count
++;
2437 static int pktgen_output_ipsec(struct sk_buff
*skb
, struct pktgen_dev
*pkt_dev
)
2439 struct xfrm_state
*x
= pkt_dev
->flows
[pkt_dev
->curfl
].x
;
2445 /* XXX: we dont support tunnel mode for now until
2446 * we resolve the dst issue */
2447 if (x
->props
.mode
!= XFRM_MODE_TRANSPORT
)
2450 spin_lock(&x
->lock
);
2453 err
= x
->outer_mode
->output(x
, skb
);
2456 err
= x
->type
->output(x
, skb
);
2460 x
->curlft
.bytes
+= skb
->len
;
2461 x
->curlft
.packets
++;
2463 spin_unlock(&x
->lock
);
2467 static void free_SAs(struct pktgen_dev
*pkt_dev
)
2469 if (pkt_dev
->cflows
) {
2470 /* let go of the SAs if we have them */
2472 for (; i
< pkt_dev
->cflows
; i
++) {
2473 struct xfrm_state
*x
= pkt_dev
->flows
[i
].x
;
2476 pkt_dev
->flows
[i
].x
= NULL
;
2482 static int process_ipsec(struct pktgen_dev
*pkt_dev
,
2483 struct sk_buff
*skb
, __be16 protocol
)
2485 if (pkt_dev
->flags
& F_IPSEC_ON
) {
2486 struct xfrm_state
*x
= pkt_dev
->flows
[pkt_dev
->curfl
].x
;
2491 nhead
= x
->props
.header_len
- skb_headroom(skb
);
2493 ret
= pskb_expand_head(skb
, nhead
, 0, GFP_ATOMIC
);
2495 printk(KERN_ERR
"Error expanding "
2496 "ipsec packet %d\n", ret
);
2501 /* ipsec is not expecting ll header */
2502 skb_pull(skb
, ETH_HLEN
);
2503 ret
= pktgen_output_ipsec(skb
, pkt_dev
);
2505 printk(KERN_ERR
"Error creating ipsec "
2506 "packet %d\n", ret
);
2510 eth
= (__u8
*) skb_push(skb
, ETH_HLEN
);
2511 memcpy(eth
, pkt_dev
->hh
, 12);
2512 *(u16
*) ð
[12] = protocol
;
2522 static void mpls_push(__be32
*mpls
, struct pktgen_dev
*pkt_dev
)
2525 for (i
= 0; i
< pkt_dev
->nr_labels
; i
++)
2526 *mpls
++ = pkt_dev
->labels
[i
] & ~MPLS_STACK_BOTTOM
;
2529 *mpls
|= MPLS_STACK_BOTTOM
;
2532 static inline __be16
build_tci(unsigned int id
, unsigned int cfi
,
2535 return htons(id
| (cfi
<< 12) | (prio
<< 13));
2538 static struct sk_buff
*fill_packet_ipv4(struct net_device
*odev
,
2539 struct pktgen_dev
*pkt_dev
)
2541 struct sk_buff
*skb
= NULL
;
2543 struct udphdr
*udph
;
2546 struct pktgen_hdr
*pgh
= NULL
;
2547 __be16 protocol
= htons(ETH_P_IP
);
2549 __be16
*vlan_tci
= NULL
; /* Encapsulates priority and VLAN ID */
2550 __be16
*vlan_encapsulated_proto
= NULL
; /* packet type ID field (or len) for VLAN tag */
2551 __be16
*svlan_tci
= NULL
; /* Encapsulates priority and SVLAN ID */
2552 __be16
*svlan_encapsulated_proto
= NULL
; /* packet type ID field (or len) for SVLAN tag */
2555 if (pkt_dev
->nr_labels
)
2556 protocol
= htons(ETH_P_MPLS_UC
);
2558 if (pkt_dev
->vlan_id
!= 0xffff)
2559 protocol
= htons(ETH_P_8021Q
);
2561 /* Update any of the values, used when we're incrementing various
2564 queue_map
= pkt_dev
->cur_queue_map
;
2565 mod_cur_headers(pkt_dev
);
2567 datalen
= (odev
->hard_header_len
+ 16) & ~0xf;
2568 skb
= __netdev_alloc_skb(odev
,
2569 pkt_dev
->cur_pkt_size
+ 64
2570 + datalen
+ pkt_dev
->pkt_overhead
, GFP_NOWAIT
);
2572 sprintf(pkt_dev
->result
, "No memory");
2576 skb_reserve(skb
, datalen
);
2578 /* Reserve for ethernet and IP header */
2579 eth
= (__u8
*) skb_push(skb
, 14);
2580 mpls
= (__be32
*)skb_put(skb
, pkt_dev
->nr_labels
*sizeof(__u32
));
2581 if (pkt_dev
->nr_labels
)
2582 mpls_push(mpls
, pkt_dev
);
2584 if (pkt_dev
->vlan_id
!= 0xffff) {
2585 if (pkt_dev
->svlan_id
!= 0xffff) {
2586 svlan_tci
= (__be16
*)skb_put(skb
, sizeof(__be16
));
2587 *svlan_tci
= build_tci(pkt_dev
->svlan_id
,
2590 svlan_encapsulated_proto
= (__be16
*)skb_put(skb
, sizeof(__be16
));
2591 *svlan_encapsulated_proto
= htons(ETH_P_8021Q
);
2593 vlan_tci
= (__be16
*)skb_put(skb
, sizeof(__be16
));
2594 *vlan_tci
= build_tci(pkt_dev
->vlan_id
,
2597 vlan_encapsulated_proto
= (__be16
*)skb_put(skb
, sizeof(__be16
));
2598 *vlan_encapsulated_proto
= htons(ETH_P_IP
);
2601 skb
->network_header
= skb
->tail
;
2602 skb
->transport_header
= skb
->network_header
+ sizeof(struct iphdr
);
2603 skb_put(skb
, sizeof(struct iphdr
) + sizeof(struct udphdr
));
2604 skb_set_queue_mapping(skb
, queue_map
);
2606 udph
= udp_hdr(skb
);
2608 memcpy(eth
, pkt_dev
->hh
, 12);
2609 *(__be16
*) & eth
[12] = protocol
;
2611 /* Eth + IPh + UDPh + mpls */
2612 datalen
= pkt_dev
->cur_pkt_size
- 14 - 20 - 8 -
2613 pkt_dev
->pkt_overhead
;
2614 if (datalen
< sizeof(struct pktgen_hdr
))
2615 datalen
= sizeof(struct pktgen_hdr
);
2617 udph
->source
= htons(pkt_dev
->cur_udp_src
);
2618 udph
->dest
= htons(pkt_dev
->cur_udp_dst
);
2619 udph
->len
= htons(datalen
+ 8); /* DATA + udphdr */
2620 udph
->check
= 0; /* No checksum */
2625 iph
->tos
= pkt_dev
->tos
;
2626 iph
->protocol
= IPPROTO_UDP
; /* UDP */
2627 iph
->saddr
= pkt_dev
->cur_saddr
;
2628 iph
->daddr
= pkt_dev
->cur_daddr
;
2630 iplen
= 20 + 8 + datalen
;
2631 iph
->tot_len
= htons(iplen
);
2633 iph
->check
= ip_fast_csum((void *)iph
, iph
->ihl
);
2634 skb
->protocol
= protocol
;
2635 skb
->mac_header
= (skb
->network_header
- ETH_HLEN
-
2636 pkt_dev
->pkt_overhead
);
2638 skb
->pkt_type
= PACKET_HOST
;
2640 if (pkt_dev
->nfrags
<= 0)
2641 pgh
= (struct pktgen_hdr
*)skb_put(skb
, datalen
);
2643 int frags
= pkt_dev
->nfrags
;
2646 pgh
= (struct pktgen_hdr
*)(((char *)(udph
)) + 8);
2648 if (frags
> MAX_SKB_FRAGS
)
2649 frags
= MAX_SKB_FRAGS
;
2650 if (datalen
> frags
* PAGE_SIZE
) {
2651 skb_put(skb
, datalen
- frags
* PAGE_SIZE
);
2652 datalen
= frags
* PAGE_SIZE
;
2656 while (datalen
> 0) {
2657 struct page
*page
= alloc_pages(GFP_KERNEL
, 0);
2658 skb_shinfo(skb
)->frags
[i
].page
= page
;
2659 skb_shinfo(skb
)->frags
[i
].page_offset
= 0;
2660 skb_shinfo(skb
)->frags
[i
].size
=
2661 (datalen
< PAGE_SIZE
? datalen
: PAGE_SIZE
);
2662 datalen
-= skb_shinfo(skb
)->frags
[i
].size
;
2663 skb
->len
+= skb_shinfo(skb
)->frags
[i
].size
;
2664 skb
->data_len
+= skb_shinfo(skb
)->frags
[i
].size
;
2666 skb_shinfo(skb
)->nr_frags
= i
;
2675 rem
= skb_shinfo(skb
)->frags
[i
- 1].size
/ 2;
2679 skb_shinfo(skb
)->frags
[i
- 1].size
-= rem
;
2681 skb_shinfo(skb
)->frags
[i
] =
2682 skb_shinfo(skb
)->frags
[i
- 1];
2683 get_page(skb_shinfo(skb
)->frags
[i
].page
);
2684 skb_shinfo(skb
)->frags
[i
].page
=
2685 skb_shinfo(skb
)->frags
[i
- 1].page
;
2686 skb_shinfo(skb
)->frags
[i
].page_offset
+=
2687 skb_shinfo(skb
)->frags
[i
- 1].size
;
2688 skb_shinfo(skb
)->frags
[i
].size
= rem
;
2690 skb_shinfo(skb
)->nr_frags
= i
;
2694 /* Stamp the time, and sequence number,
2695 * convert them to network byte order
2698 struct timeval timestamp
;
2700 pgh
->pgh_magic
= htonl(PKTGEN_MAGIC
);
2701 pgh
->seq_num
= htonl(pkt_dev
->seq_num
);
2703 do_gettimeofday(×tamp
);
2704 pgh
->tv_sec
= htonl(timestamp
.tv_sec
);
2705 pgh
->tv_usec
= htonl(timestamp
.tv_usec
);
2709 if (!process_ipsec(pkt_dev
, skb
, protocol
))
2717 * scan_ip6, fmt_ip taken from dietlibc-0.21
2718 * Author Felix von Leitner <felix-dietlibc@fefe.de>
2720 * Slightly modified for kernel.
2721 * Should be candidate for net/ipv4/utils.c
2725 static unsigned int scan_ip6(const char *s
, char ip
[16])
2728 unsigned int len
= 0;
2731 unsigned int prefixlen
= 0;
2732 unsigned int suffixlen
= 0;
2736 for (i
= 0; i
< 16; i
++)
2742 if (s
[1] == ':') { /* Found "::", skip to part 2 */
2750 u
= simple_strtoul(s
, &pos
, 16);
2754 if (prefixlen
== 12 && s
[i
] == '.') {
2756 /* the last 4 bytes may be written as IPv4 address */
2759 memcpy((struct in_addr
*)(ip
+ 12), &tmp
, sizeof(tmp
));
2762 ip
[prefixlen
++] = (u
>> 8);
2763 ip
[prefixlen
++] = (u
& 255);
2766 if (prefixlen
== 16)
2770 /* part 2, after "::" */
2777 } else if (suffixlen
!= 0)
2780 u
= simple_strtol(s
, &pos
, 16);
2787 if (suffixlen
+ prefixlen
<= 12 && s
[i
] == '.') {
2789 memcpy((struct in_addr
*)(suffix
+ suffixlen
), &tmp
,
2795 suffix
[suffixlen
++] = (u
>> 8);
2796 suffix
[suffixlen
++] = (u
& 255);
2799 if (prefixlen
+ suffixlen
== 16)
2802 for (i
= 0; i
< suffixlen
; i
++)
2803 ip
[16 - suffixlen
+ i
] = suffix
[i
];
2807 static char tohex(char hexdigit
)
2809 return hexdigit
> 9 ? hexdigit
+ 'a' - 10 : hexdigit
+ '0';
2812 static int fmt_xlong(char *s
, unsigned int i
)
2815 *s
= tohex((i
>> 12) & 0xf);
2816 if (s
!= bak
|| *s
!= '0')
2818 *s
= tohex((i
>> 8) & 0xf);
2819 if (s
!= bak
|| *s
!= '0')
2821 *s
= tohex((i
>> 4) & 0xf);
2822 if (s
!= bak
|| *s
!= '0')
2824 *s
= tohex(i
& 0xf);
2828 static unsigned int fmt_ip6(char *s
, const char ip
[16])
2833 unsigned int compressing
;
2838 for (j
= 0; j
< 16; j
+= 2) {
2840 #ifdef V4MAPPEDPREFIX
2841 if (j
== 12 && !memcmp(ip
, V4mappedprefix
, 12)) {
2842 inet_ntoa_r(*(struct in_addr
*)(ip
+ 12), s
);
2847 temp
= ((unsigned long)(unsigned char)ip
[j
] << 8) +
2848 (unsigned long)(unsigned char)ip
[j
+ 1];
2863 i
= fmt_xlong(s
, temp
);
2880 static struct sk_buff
*fill_packet_ipv6(struct net_device
*odev
,
2881 struct pktgen_dev
*pkt_dev
)
2883 struct sk_buff
*skb
= NULL
;
2885 struct udphdr
*udph
;
2887 struct ipv6hdr
*iph
;
2888 struct pktgen_hdr
*pgh
= NULL
;
2889 __be16 protocol
= htons(ETH_P_IPV6
);
2891 __be16
*vlan_tci
= NULL
; /* Encapsulates priority and VLAN ID */
2892 __be16
*vlan_encapsulated_proto
= NULL
; /* packet type ID field (or len) for VLAN tag */
2893 __be16
*svlan_tci
= NULL
; /* Encapsulates priority and SVLAN ID */
2894 __be16
*svlan_encapsulated_proto
= NULL
; /* packet type ID field (or len) for SVLAN tag */
2897 if (pkt_dev
->nr_labels
)
2898 protocol
= htons(ETH_P_MPLS_UC
);
2900 if (pkt_dev
->vlan_id
!= 0xffff)
2901 protocol
= htons(ETH_P_8021Q
);
2903 /* Update any of the values, used when we're incrementing various
2906 queue_map
= pkt_dev
->cur_queue_map
;
2907 mod_cur_headers(pkt_dev
);
2909 skb
= __netdev_alloc_skb(odev
,
2910 pkt_dev
->cur_pkt_size
+ 64
2911 + 16 + pkt_dev
->pkt_overhead
, GFP_NOWAIT
);
2913 sprintf(pkt_dev
->result
, "No memory");
2917 skb_reserve(skb
, 16);
2919 /* Reserve for ethernet and IP header */
2920 eth
= (__u8
*) skb_push(skb
, 14);
2921 mpls
= (__be32
*)skb_put(skb
, pkt_dev
->nr_labels
*sizeof(__u32
));
2922 if (pkt_dev
->nr_labels
)
2923 mpls_push(mpls
, pkt_dev
);
2925 if (pkt_dev
->vlan_id
!= 0xffff) {
2926 if (pkt_dev
->svlan_id
!= 0xffff) {
2927 svlan_tci
= (__be16
*)skb_put(skb
, sizeof(__be16
));
2928 *svlan_tci
= build_tci(pkt_dev
->svlan_id
,
2931 svlan_encapsulated_proto
= (__be16
*)skb_put(skb
, sizeof(__be16
));
2932 *svlan_encapsulated_proto
= htons(ETH_P_8021Q
);
2934 vlan_tci
= (__be16
*)skb_put(skb
, sizeof(__be16
));
2935 *vlan_tci
= build_tci(pkt_dev
->vlan_id
,
2938 vlan_encapsulated_proto
= (__be16
*)skb_put(skb
, sizeof(__be16
));
2939 *vlan_encapsulated_proto
= htons(ETH_P_IPV6
);
2942 skb
->network_header
= skb
->tail
;
2943 skb
->transport_header
= skb
->network_header
+ sizeof(struct ipv6hdr
);
2944 skb_put(skb
, sizeof(struct ipv6hdr
) + sizeof(struct udphdr
));
2945 skb_set_queue_mapping(skb
, queue_map
);
2946 iph
= ipv6_hdr(skb
);
2947 udph
= udp_hdr(skb
);
2949 memcpy(eth
, pkt_dev
->hh
, 12);
2950 *(__be16
*) ð
[12] = protocol
;
2952 /* Eth + IPh + UDPh + mpls */
2953 datalen
= pkt_dev
->cur_pkt_size
- 14 -
2954 sizeof(struct ipv6hdr
) - sizeof(struct udphdr
) -
2955 pkt_dev
->pkt_overhead
;
2957 if (datalen
< sizeof(struct pktgen_hdr
)) {
2958 datalen
= sizeof(struct pktgen_hdr
);
2959 if (net_ratelimit())
2960 printk(KERN_INFO
"pktgen: increased datalen to %d\n",
2964 udph
->source
= htons(pkt_dev
->cur_udp_src
);
2965 udph
->dest
= htons(pkt_dev
->cur_udp_dst
);
2966 udph
->len
= htons(datalen
+ sizeof(struct udphdr
));
2967 udph
->check
= 0; /* No checksum */
2969 *(__be32
*) iph
= htonl(0x60000000); /* Version + flow */
2971 if (pkt_dev
->traffic_class
) {
2972 /* Version + traffic class + flow (0) */
2973 *(__be32
*)iph
|= htonl(0x60000000 | (pkt_dev
->traffic_class
<< 20));
2976 iph
->hop_limit
= 32;
2978 iph
->payload_len
= htons(sizeof(struct udphdr
) + datalen
);
2979 iph
->nexthdr
= IPPROTO_UDP
;
2981 ipv6_addr_copy(&iph
->daddr
, &pkt_dev
->cur_in6_daddr
);
2982 ipv6_addr_copy(&iph
->saddr
, &pkt_dev
->cur_in6_saddr
);
2984 skb
->mac_header
= (skb
->network_header
- ETH_HLEN
-
2985 pkt_dev
->pkt_overhead
);
2986 skb
->protocol
= protocol
;
2988 skb
->pkt_type
= PACKET_HOST
;
2990 if (pkt_dev
->nfrags
<= 0)
2991 pgh
= (struct pktgen_hdr
*)skb_put(skb
, datalen
);
2993 int frags
= pkt_dev
->nfrags
;
2996 pgh
= (struct pktgen_hdr
*)(((char *)(udph
)) + 8);
2998 if (frags
> MAX_SKB_FRAGS
)
2999 frags
= MAX_SKB_FRAGS
;
3000 if (datalen
> frags
* PAGE_SIZE
) {
3001 skb_put(skb
, datalen
- frags
* PAGE_SIZE
);
3002 datalen
= frags
* PAGE_SIZE
;
3006 while (datalen
> 0) {
3007 struct page
*page
= alloc_pages(GFP_KERNEL
, 0);
3008 skb_shinfo(skb
)->frags
[i
].page
= page
;
3009 skb_shinfo(skb
)->frags
[i
].page_offset
= 0;
3010 skb_shinfo(skb
)->frags
[i
].size
=
3011 (datalen
< PAGE_SIZE
? datalen
: PAGE_SIZE
);
3012 datalen
-= skb_shinfo(skb
)->frags
[i
].size
;
3013 skb
->len
+= skb_shinfo(skb
)->frags
[i
].size
;
3014 skb
->data_len
+= skb_shinfo(skb
)->frags
[i
].size
;
3016 skb_shinfo(skb
)->nr_frags
= i
;
3025 rem
= skb_shinfo(skb
)->frags
[i
- 1].size
/ 2;
3029 skb_shinfo(skb
)->frags
[i
- 1].size
-= rem
;
3031 skb_shinfo(skb
)->frags
[i
] =
3032 skb_shinfo(skb
)->frags
[i
- 1];
3033 get_page(skb_shinfo(skb
)->frags
[i
].page
);
3034 skb_shinfo(skb
)->frags
[i
].page
=
3035 skb_shinfo(skb
)->frags
[i
- 1].page
;
3036 skb_shinfo(skb
)->frags
[i
].page_offset
+=
3037 skb_shinfo(skb
)->frags
[i
- 1].size
;
3038 skb_shinfo(skb
)->frags
[i
].size
= rem
;
3040 skb_shinfo(skb
)->nr_frags
= i
;
3044 /* Stamp the time, and sequence number,
3045 * convert them to network byte order
3046 * should we update cloned packets too ?
3049 struct timeval timestamp
;
3051 pgh
->pgh_magic
= htonl(PKTGEN_MAGIC
);
3052 pgh
->seq_num
= htonl(pkt_dev
->seq_num
);
3054 do_gettimeofday(×tamp
);
3055 pgh
->tv_sec
= htonl(timestamp
.tv_sec
);
3056 pgh
->tv_usec
= htonl(timestamp
.tv_usec
);
3058 /* pkt_dev->seq_num++; FF: you really mean this? */
3063 static struct sk_buff
*fill_packet(struct net_device
*odev
,
3064 struct pktgen_dev
*pkt_dev
)
3066 if (pkt_dev
->flags
& F_IPV6
)
3067 return fill_packet_ipv6(odev
, pkt_dev
);
3069 return fill_packet_ipv4(odev
, pkt_dev
);
3072 static void pktgen_clear_counters(struct pktgen_dev
*pkt_dev
)
3074 pkt_dev
->seq_num
= 1;
3075 pkt_dev
->idle_acc
= 0;
3077 pkt_dev
->tx_bytes
= 0;
3078 pkt_dev
->errors
= 0;
3081 /* Set up structure for sending pkts, clear counters */
3083 static void pktgen_run(struct pktgen_thread
*t
)
3085 struct pktgen_dev
*pkt_dev
;
3088 pr_debug("pktgen: entering pktgen_run. %p\n", t
);
3091 list_for_each_entry(pkt_dev
, &t
->if_list
, list
) {
3094 * setup odev and create initial packet.
3096 pktgen_setup_inject(pkt_dev
);
3098 if (pkt_dev
->odev
) {
3099 pktgen_clear_counters(pkt_dev
);
3100 pkt_dev
->running
= 1; /* Cranke yeself! */
3101 pkt_dev
->skb
= NULL
;
3102 pkt_dev
->started_at
=
3103 pkt_dev
->next_tx
= ktime_now();
3105 set_pkt_overhead(pkt_dev
);
3107 strcpy(pkt_dev
->result
, "Starting");
3110 strcpy(pkt_dev
->result
, "Error starting");
3114 t
->control
&= ~(T_STOP
);
3117 static void pktgen_stop_all_threads_ifs(void)
3119 struct pktgen_thread
*t
;
3121 pr_debug("pktgen: entering pktgen_stop_all_threads_ifs.\n");
3123 mutex_lock(&pktgen_thread_lock
);
3125 list_for_each_entry(t
, &pktgen_threads
, th_list
)
3126 t
->control
|= T_STOP
;
3128 mutex_unlock(&pktgen_thread_lock
);
3131 static int thread_is_running(const struct pktgen_thread
*t
)
3133 const struct pktgen_dev
*pkt_dev
;
3135 list_for_each_entry(pkt_dev
, &t
->if_list
, list
)
3136 if (pkt_dev
->running
)
3141 static int pktgen_wait_thread_run(struct pktgen_thread
*t
)
3145 while (thread_is_running(t
)) {
3149 msleep_interruptible(100);
3151 if (signal_pending(current
))
3161 static int pktgen_wait_all_threads_run(void)
3163 struct pktgen_thread
*t
;
3166 mutex_lock(&pktgen_thread_lock
);
3168 list_for_each_entry(t
, &pktgen_threads
, th_list
) {
3169 sig
= pktgen_wait_thread_run(t
);
3175 list_for_each_entry(t
, &pktgen_threads
, th_list
)
3176 t
->control
|= (T_STOP
);
3178 mutex_unlock(&pktgen_thread_lock
);
3182 static void pktgen_run_all_threads(void)
3184 struct pktgen_thread
*t
;
3186 pr_debug("pktgen: entering pktgen_run_all_threads.\n");
3188 mutex_lock(&pktgen_thread_lock
);
3190 list_for_each_entry(t
, &pktgen_threads
, th_list
)
3191 t
->control
|= (T_RUN
);
3193 mutex_unlock(&pktgen_thread_lock
);
3195 /* Propagate thread->control */
3196 schedule_timeout_interruptible(msecs_to_jiffies(125));
3198 pktgen_wait_all_threads_run();
3201 static void pktgen_reset_all_threads(void)
3203 struct pktgen_thread
*t
;
3205 pr_debug("pktgen: entering pktgen_reset_all_threads.\n");
3207 mutex_lock(&pktgen_thread_lock
);
3209 list_for_each_entry(t
, &pktgen_threads
, th_list
)
3210 t
->control
|= (T_REMDEVALL
);
3212 mutex_unlock(&pktgen_thread_lock
);
3214 /* Propagate thread->control */
3215 schedule_timeout_interruptible(msecs_to_jiffies(125));
3217 pktgen_wait_all_threads_run();
3220 static void show_results(struct pktgen_dev
*pkt_dev
, int nr_frags
)
3222 __u64 bps
, mbps
, pps
;
3223 char *p
= pkt_dev
->result
;
3224 ktime_t elapsed
= ktime_sub(pkt_dev
->stopped_at
,
3225 pkt_dev
->started_at
);
3226 ktime_t idle
= ns_to_ktime(pkt_dev
->idle_acc
);
3228 p
+= sprintf(p
, "OK: %llu(c%llu+d%llu) nsec, %llu (%dbyte,%dfrags)\n",
3229 (unsigned long long)ktime_to_us(elapsed
),
3230 (unsigned long long)ktime_to_us(ktime_sub(elapsed
, idle
)),
3231 (unsigned long long)ktime_to_us(idle
),
3232 (unsigned long long)pkt_dev
->sofar
,
3233 pkt_dev
->cur_pkt_size
, nr_frags
);
3235 pps
= div64_u64(pkt_dev
->sofar
* NSEC_PER_SEC
,
3236 ktime_to_ns(elapsed
));
3238 bps
= pps
* 8 * pkt_dev
->cur_pkt_size
;
3241 do_div(mbps
, 1000000);
3242 p
+= sprintf(p
, " %llupps %lluMb/sec (%llubps) errors: %llu",
3243 (unsigned long long)pps
,
3244 (unsigned long long)mbps
,
3245 (unsigned long long)bps
,
3246 (unsigned long long)pkt_dev
->errors
);
3249 /* Set stopped-at timer, remove from running list, do counters & statistics */
3250 static int pktgen_stop_device(struct pktgen_dev
*pkt_dev
)
3252 int nr_frags
= pkt_dev
->skb
? skb_shinfo(pkt_dev
->skb
)->nr_frags
: -1;
3254 if (!pkt_dev
->running
) {
3255 printk(KERN_WARNING
"pktgen: interface: %s is already "
3256 "stopped\n", pkt_dev
->odev
->name
);
3260 kfree_skb(pkt_dev
->skb
);
3261 pkt_dev
->skb
= NULL
;
3262 pkt_dev
->stopped_at
= ktime_now();
3263 pkt_dev
->running
= 0;
3265 show_results(pkt_dev
, nr_frags
);
3270 static struct pktgen_dev
*next_to_run(struct pktgen_thread
*t
)
3272 struct pktgen_dev
*pkt_dev
, *best
= NULL
;
3276 list_for_each_entry(pkt_dev
, &t
->if_list
, list
) {
3277 if (!pkt_dev
->running
)
3281 else if (ktime_lt(pkt_dev
->next_tx
, best
->next_tx
))
3288 static void pktgen_stop(struct pktgen_thread
*t
)
3290 struct pktgen_dev
*pkt_dev
;
3292 pr_debug("pktgen: entering pktgen_stop\n");
3296 list_for_each_entry(pkt_dev
, &t
->if_list
, list
) {
3297 pktgen_stop_device(pkt_dev
);
3304 * one of our devices needs to be removed - find it
3307 static void pktgen_rem_one_if(struct pktgen_thread
*t
)
3309 struct list_head
*q
, *n
;
3310 struct pktgen_dev
*cur
;
3312 pr_debug("pktgen: entering pktgen_rem_one_if\n");
3316 list_for_each_safe(q
, n
, &t
->if_list
) {
3317 cur
= list_entry(q
, struct pktgen_dev
, list
);
3319 if (!cur
->removal_mark
)
3322 kfree_skb(cur
->skb
);
3325 pktgen_remove_device(t
, cur
);
3333 static void pktgen_rem_all_ifs(struct pktgen_thread
*t
)
3335 struct list_head
*q
, *n
;
3336 struct pktgen_dev
*cur
;
3338 /* Remove all devices, free mem */
3340 pr_debug("pktgen: entering pktgen_rem_all_ifs\n");
3343 list_for_each_safe(q
, n
, &t
->if_list
) {
3344 cur
= list_entry(q
, struct pktgen_dev
, list
);
3346 kfree_skb(cur
->skb
);
3349 pktgen_remove_device(t
, cur
);
3355 static void pktgen_rem_thread(struct pktgen_thread
*t
)
3357 /* Remove from the thread list */
3359 remove_proc_entry(t
->tsk
->comm
, pg_proc_dir
);
3361 mutex_lock(&pktgen_thread_lock
);
3363 list_del(&t
->th_list
);
3365 mutex_unlock(&pktgen_thread_lock
);
3368 static void idle(struct pktgen_dev
*pkt_dev
)
3370 ktime_t idle_start
= ktime_now();
3377 pkt_dev
->idle_acc
+= ktime_to_ns(ktime_sub(ktime_now(), idle_start
));
3381 static void pktgen_xmit(struct pktgen_dev
*pkt_dev
)
3383 struct net_device
*odev
= pkt_dev
->odev
;
3384 netdev_tx_t (*xmit
)(struct sk_buff
*, struct net_device
*)
3385 = odev
->netdev_ops
->ndo_start_xmit
;
3386 struct netdev_queue
*txq
;
3390 if (pkt_dev
->delay
) {
3391 spin(pkt_dev
, pkt_dev
->next_tx
);
3393 /* This is max DELAY, this has special meaning of
3396 if (pkt_dev
->delay
== ULLONG_MAX
) {
3397 pkt_dev
->next_tx
= ktime_add_ns(ktime_now(), ULONG_MAX
);
3402 if (!pkt_dev
->skb
) {
3403 set_cur_queue_map(pkt_dev
);
3404 queue_map
= pkt_dev
->cur_queue_map
;
3406 queue_map
= skb_get_queue_mapping(pkt_dev
->skb
);
3409 txq
= netdev_get_tx_queue(odev
, queue_map
);
3410 /* Did we saturate the queue already? */
3411 if (netif_tx_queue_stopped(txq
) || netif_tx_queue_frozen(txq
)) {
3412 /* If device is down, then all queues are permnantly frozen */
3413 if (netif_running(odev
))
3416 pktgen_stop_device(pkt_dev
);
3420 if (!pkt_dev
->skb
|| (pkt_dev
->last_ok
&&
3421 ++pkt_dev
->clone_count
>= pkt_dev
->clone_skb
)) {
3422 /* build a new pkt */
3423 kfree_skb(pkt_dev
->skb
);
3425 pkt_dev
->skb
= fill_packet(odev
, pkt_dev
);
3426 if (pkt_dev
->skb
== NULL
) {
3427 printk(KERN_ERR
"pktgen: ERROR: couldn't "
3428 "allocate skb in fill_packet.\n");
3430 pkt_dev
->clone_count
--; /* back out increment, OOM */
3434 pkt_dev
->allocated_skbs
++;
3435 pkt_dev
->clone_count
= 0; /* reset counter */
3438 /* fill_packet() might have changed the queue */
3439 queue_map
= skb_get_queue_mapping(pkt_dev
->skb
);
3440 txq
= netdev_get_tx_queue(odev
, queue_map
);
3442 __netif_tx_lock_bh(txq
);
3443 if (unlikely(netif_tx_queue_stopped(txq
) || netif_tx_queue_frozen(txq
)))
3444 pkt_dev
->last_ok
= 0;
3446 atomic_inc(&(pkt_dev
->skb
->users
));
3449 ret
= (*xmit
)(pkt_dev
->skb
, odev
);
3452 txq_trans_update(txq
);
3453 pkt_dev
->last_ok
= 1;
3456 pkt_dev
->tx_bytes
+= pkt_dev
->cur_pkt_size
;
3458 case NETDEV_TX_LOCKED
:
3461 default: /* Drivers are not supposed to return other values! */
3462 if (net_ratelimit())
3463 pr_info("pktgen: %s xmit error: %d\n",
3467 case NETDEV_TX_BUSY
:
3468 /* Retry it next time */
3469 atomic_dec(&(pkt_dev
->skb
->users
));
3470 pkt_dev
->last_ok
= 0;
3474 pkt_dev
->next_tx
= ktime_add_ns(ktime_now(),
3477 __netif_tx_unlock_bh(txq
);
3479 /* If pkt_dev->count is zero, then run forever */
3480 if ((pkt_dev
->count
!= 0) && (pkt_dev
->sofar
>= pkt_dev
->count
)) {
3481 while (atomic_read(&(pkt_dev
->skb
->users
)) != 1) {
3482 if (signal_pending(current
))
3487 /* Done with this */
3488 pktgen_stop_device(pkt_dev
);
3493 * Main loop of the thread goes here
3496 static int pktgen_thread_worker(void *arg
)
3499 struct pktgen_thread
*t
= arg
;
3500 struct pktgen_dev
*pkt_dev
= NULL
;
3503 BUG_ON(smp_processor_id() != cpu
);
3505 init_waitqueue_head(&t
->queue
);
3506 complete(&t
->start_done
);
3508 pr_debug("pktgen: starting pktgen/%d: pid=%d\n",
3509 cpu
, task_pid_nr(current
));
3511 set_current_state(TASK_INTERRUPTIBLE
);
3515 while (!kthread_should_stop()) {
3516 pkt_dev
= next_to_run(t
);
3519 (t
->control
& (T_STOP
| T_RUN
| T_REMDEVALL
| T_REMDEV
))
3521 prepare_to_wait(&(t
->queue
), &wait
,
3522 TASK_INTERRUPTIBLE
);
3523 schedule_timeout(HZ
/ 10);
3524 finish_wait(&(t
->queue
), &wait
);
3527 __set_current_state(TASK_RUNNING
);
3530 pktgen_xmit(pkt_dev
);
3532 if (t
->control
& T_STOP
) {
3534 t
->control
&= ~(T_STOP
);
3537 if (t
->control
& T_RUN
) {
3539 t
->control
&= ~(T_RUN
);
3542 if (t
->control
& T_REMDEVALL
) {
3543 pktgen_rem_all_ifs(t
);
3544 t
->control
&= ~(T_REMDEVALL
);
3547 if (t
->control
& T_REMDEV
) {
3548 pktgen_rem_one_if(t
);
3549 t
->control
&= ~(T_REMDEV
);
3554 set_current_state(TASK_INTERRUPTIBLE
);
3557 pr_debug("pktgen: %s stopping all device\n", t
->tsk
->comm
);
3560 pr_debug("pktgen: %s removing all device\n", t
->tsk
->comm
);
3561 pktgen_rem_all_ifs(t
);
3563 pr_debug("pktgen: %s removing thread.\n", t
->tsk
->comm
);
3564 pktgen_rem_thread(t
);
3569 static struct pktgen_dev
*pktgen_find_dev(struct pktgen_thread
*t
,
3572 struct pktgen_dev
*p
, *pkt_dev
= NULL
;
3575 list_for_each_entry(p
, &t
->if_list
, list
)
3576 if (strncmp(p
->odev
->name
, ifname
, IFNAMSIZ
) == 0) {
3582 pr_debug("pktgen: find_dev(%s) returning %p\n", ifname
, pkt_dev
);
3587 * Adds a dev at front of if_list.
3590 static int add_dev_to_thread(struct pktgen_thread
*t
,
3591 struct pktgen_dev
*pkt_dev
)
3597 if (pkt_dev
->pg_thread
) {
3598 printk(KERN_ERR
"pktgen: ERROR: already assigned "
3604 list_add(&pkt_dev
->list
, &t
->if_list
);
3605 pkt_dev
->pg_thread
= t
;
3606 pkt_dev
->running
= 0;
3613 /* Called under thread lock */
3615 static int pktgen_add_device(struct pktgen_thread
*t
, const char *ifname
)
3617 struct pktgen_dev
*pkt_dev
;
3620 /* We don't allow a device to be on several threads */
3622 pkt_dev
= __pktgen_NN_threads(ifname
, FIND
);
3624 printk(KERN_ERR
"pktgen: ERROR: interface already used.\n");
3628 pkt_dev
= kzalloc(sizeof(struct pktgen_dev
), GFP_KERNEL
);
3632 pkt_dev
->flows
= vmalloc(MAX_CFLOWS
* sizeof(struct flow_state
));
3633 if (pkt_dev
->flows
== NULL
) {
3637 memset(pkt_dev
->flows
, 0, MAX_CFLOWS
* sizeof(struct flow_state
));
3639 pkt_dev
->removal_mark
= 0;
3640 pkt_dev
->min_pkt_size
= ETH_ZLEN
;
3641 pkt_dev
->max_pkt_size
= ETH_ZLEN
;
3642 pkt_dev
->nfrags
= 0;
3643 pkt_dev
->clone_skb
= pg_clone_skb_d
;
3644 pkt_dev
->delay
= pg_delay_d
;
3645 pkt_dev
->count
= pg_count_d
;
3647 pkt_dev
->udp_src_min
= 9; /* sink port */
3648 pkt_dev
->udp_src_max
= 9;
3649 pkt_dev
->udp_dst_min
= 9;
3650 pkt_dev
->udp_dst_max
= 9;
3652 pkt_dev
->vlan_p
= 0;
3653 pkt_dev
->vlan_cfi
= 0;
3654 pkt_dev
->vlan_id
= 0xffff;
3655 pkt_dev
->svlan_p
= 0;
3656 pkt_dev
->svlan_cfi
= 0;
3657 pkt_dev
->svlan_id
= 0xffff;
3659 err
= pktgen_setup_dev(pkt_dev
, ifname
);
3663 pkt_dev
->entry
= proc_create_data(ifname
, 0600, pg_proc_dir
,
3664 &pktgen_if_fops
, pkt_dev
);
3665 if (!pkt_dev
->entry
) {
3666 printk(KERN_ERR
"pktgen: cannot create %s/%s procfs entry.\n",
3667 PG_PROC_DIR
, ifname
);
3672 pkt_dev
->ipsmode
= XFRM_MODE_TRANSPORT
;
3673 pkt_dev
->ipsproto
= IPPROTO_ESP
;
3676 return add_dev_to_thread(t
, pkt_dev
);
3678 dev_put(pkt_dev
->odev
);
3683 vfree(pkt_dev
->flows
);
3688 static int __init
pktgen_create_thread(int cpu
)
3690 struct pktgen_thread
*t
;
3691 struct proc_dir_entry
*pe
;
3692 struct task_struct
*p
;
3694 t
= kzalloc(sizeof(struct pktgen_thread
), GFP_KERNEL
);
3696 printk(KERN_ERR
"pktgen: ERROR: out of memory, can't "
3697 "create new thread.\n");
3701 spin_lock_init(&t
->if_lock
);
3704 INIT_LIST_HEAD(&t
->if_list
);
3706 list_add_tail(&t
->th_list
, &pktgen_threads
);
3707 init_completion(&t
->start_done
);
3709 p
= kthread_create(pktgen_thread_worker
, t
, "kpktgend_%d", cpu
);
3711 printk(KERN_ERR
"pktgen: kernel_thread() failed "
3712 "for cpu %d\n", t
->cpu
);
3713 list_del(&t
->th_list
);
3717 kthread_bind(p
, cpu
);
3720 pe
= proc_create_data(t
->tsk
->comm
, 0600, pg_proc_dir
,
3721 &pktgen_thread_fops
, t
);
3723 printk(KERN_ERR
"pktgen: cannot create %s/%s procfs entry.\n",
3724 PG_PROC_DIR
, t
->tsk
->comm
);
3726 list_del(&t
->th_list
);
3732 wait_for_completion(&t
->start_done
);
3738 * Removes a device from the thread if_list.
3740 static void _rem_dev_from_if_list(struct pktgen_thread
*t
,
3741 struct pktgen_dev
*pkt_dev
)
3743 struct list_head
*q
, *n
;
3744 struct pktgen_dev
*p
;
3746 list_for_each_safe(q
, n
, &t
->if_list
) {
3747 p
= list_entry(q
, struct pktgen_dev
, list
);
3753 static int pktgen_remove_device(struct pktgen_thread
*t
,
3754 struct pktgen_dev
*pkt_dev
)
3757 pr_debug("pktgen: remove_device pkt_dev=%p\n", pkt_dev
);
3759 if (pkt_dev
->running
) {
3760 printk(KERN_WARNING
"pktgen: WARNING: trying to remove a "
3761 "running interface, stopping it now.\n");
3762 pktgen_stop_device(pkt_dev
);
3765 /* Dis-associate from the interface */
3767 if (pkt_dev
->odev
) {
3768 dev_put(pkt_dev
->odev
);
3769 pkt_dev
->odev
= NULL
;
3772 /* And update the thread if_list */
3774 _rem_dev_from_if_list(t
, pkt_dev
);
3777 remove_proc_entry(pkt_dev
->entry
->name
, pg_proc_dir
);
3782 vfree(pkt_dev
->flows
);
3787 static int __init
pg_init(void)
3790 struct proc_dir_entry
*pe
;
3792 printk(KERN_INFO
"%s", version
);
3794 pg_proc_dir
= proc_mkdir(PG_PROC_DIR
, init_net
.proc_net
);
3798 pe
= proc_create(PGCTRL
, 0600, pg_proc_dir
, &pktgen_fops
);
3800 printk(KERN_ERR
"pktgen: ERROR: cannot create %s "
3801 "procfs entry.\n", PGCTRL
);
3802 proc_net_remove(&init_net
, PG_PROC_DIR
);
3806 /* Register us to receive netdevice events */
3807 register_netdevice_notifier(&pktgen_notifier_block
);
3809 for_each_online_cpu(cpu
) {
3812 err
= pktgen_create_thread(cpu
);
3814 printk(KERN_WARNING
"pktgen: WARNING: Cannot create "
3815 "thread for cpu %d (%d)\n", cpu
, err
);
3818 if (list_empty(&pktgen_threads
)) {
3819 printk(KERN_ERR
"pktgen: ERROR: Initialization failed for "
3821 unregister_netdevice_notifier(&pktgen_notifier_block
);
3822 remove_proc_entry(PGCTRL
, pg_proc_dir
);
3823 proc_net_remove(&init_net
, PG_PROC_DIR
);
3830 static void __exit
pg_cleanup(void)
3832 struct pktgen_thread
*t
;
3833 struct list_head
*q
, *n
;
3834 wait_queue_head_t queue
;
3835 init_waitqueue_head(&queue
);
3837 /* Stop all interfaces & threads */
3839 list_for_each_safe(q
, n
, &pktgen_threads
) {
3840 t
= list_entry(q
, struct pktgen_thread
, th_list
);
3841 kthread_stop(t
->tsk
);
3845 /* Un-register us from receiving netdevice events */
3846 unregister_netdevice_notifier(&pktgen_notifier_block
);
3848 /* Clean up proc file system */
3849 remove_proc_entry(PGCTRL
, pg_proc_dir
);
3850 proc_net_remove(&init_net
, PG_PROC_DIR
);
3853 module_init(pg_init
);
3854 module_exit(pg_cleanup
);
3856 MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se>");
3857 MODULE_DESCRIPTION("Packet Generator tool");
3858 MODULE_LICENSE("GPL");
3859 MODULE_VERSION(VERSION
);
3860 module_param(pg_count_d
, int, 0);
3861 MODULE_PARM_DESC(pg_count_d
, "Default number of packets to inject");
3862 module_param(pg_delay_d
, int, 0);
3863 MODULE_PARM_DESC(pg_delay_d
, "Default delay between packets (nanoseconds)");
3864 module_param(pg_clone_skb_d
, int, 0);
3865 MODULE_PARM_DESC(pg_clone_skb_d
, "Default number of copies of the same packet");
3866 module_param(debug
, int, 0);
3867 MODULE_PARM_DESC(debug
, "Enable debugging of pktgen module");