3 * Copyright 2001, 2002 by Robert Olsson <robert.olsson@its.uu.se>
4 * Uppsala University and
5 * Swedish University of Agricultural Sciences
7 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
8 * Ben Greear <greearb@candelatech.com>
9 * Jens Låås <jens.laas@data.slu.se>
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version
14 * 2 of the License, or (at your option) any later version.
17 * A tool for loading the network with preconfigurated packets.
18 * The tool is implemented as a linux module. Parameters are output
19 * device, delay (to hard_xmit), number of packets, and whether
20 * to use multiple SKBs or just the same one.
21 * pktgen uses the installed interface's output routine.
23 * Additional hacking by:
25 * Jens.Laas@data.slu.se
26 * Improved by ANK. 010120.
27 * Improved by ANK even more. 010212.
28 * MAC address typo fixed. 010417 --ro
29 * Integrated. 020301 --DaveM
30 * Added multiskb option 020301 --DaveM
31 * Scaling of results. 020417--sigurdur@linpro.no
32 * Significant re-work of the module:
33 * * Convert to threaded model to more efficiently be able to transmit
34 * and receive on multiple interfaces at once.
35 * * Converted many counters to __u64 to allow longer runs.
36 * * Allow configuration of ranges, like min/max IP address, MACs,
37 * and UDP-ports, for both source and destination, and can
38 * set to use a random distribution or sequentially walk the range.
39 * * Can now change most values after starting.
40 * * Place 12-byte packet in UDP payload with magic number,
41 * sequence number, and timestamp.
42 * * Add receiver code that detects dropped pkts, re-ordered pkts, and
43 * latencies (with micro-second) precision.
44 * * Add IOCTL interface to easily get counters & configuration.
45 * --Ben Greear <greearb@candelatech.com>
47 * Renamed multiskb to clone_skb and cleaned up sending core for two distinct
48 * skb modes. A clone_skb=0 mode for Ben "ranges" work and a clone_skb != 0
49 * as a "fastpath" with a configurable number of clones after alloc's.
50 * clone_skb=0 means all packets are allocated this also means ranges time
51 * stamps etc can be used. clone_skb=100 means 1 malloc is followed by 100
54 * Also moved to /proc/net/pktgen/
57 * Sept 10: Fixed threading/locking. Lots of bone-headed and more clever
58 * mistakes. Also merged in DaveM's patch in the -pre6 patch.
59 * --Ben Greear <greearb@candelatech.com>
61 * Integrated to 2.5.x 021029 --Lucio Maciel (luciomaciel@zipmail.com.br)
64 * 021124 Finished major redesign and rewrite for new functionality.
65 * See Documentation/networking/pktgen.txt for how to use this.
68 * For each CPU one thread/process is created at start. This process checks
69 * for running devices in the if_list and sends packets until count is 0 it
70 * also the thread checks the thread->control which is used for inter-process
71 * communication. controlling process "posts" operations to the threads this
72 * way. The if_lock should be possible to remove when add/rem_device is merged
75 * By design there should only be *one* "controlling" process. In practice
76 * multiple write accesses gives unpredictable result. Understood by "write"
77 * to /proc gives result code thats should be read be the "writer".
78 * For practical use this should be no problem.
80 * Note when adding devices to a specific CPU there good idea to also assign
81 * /proc/irq/XX/smp_affinity so TX-interrupts gets bound to the same CPU.
84 * Fix refcount off by one if first packet fails, potential null deref,
87 * First "ranges" functionality for ipv6 030726 --ro
89 * Included flow support. 030802 ANK.
91 * Fixed unaligned access on IA-64 Grant Grundler <grundler@parisc-linux.org>
93 * Remove if fix from added Harald Welte <laforge@netfilter.org> 040419
94 * ia64 compilation fix from Aron Griffis <aron@hp.com> 040604
96 * New xmit() return, do_div and misc clean up by Stephen Hemminger
97 * <shemminger@osdl.org> 040923
99 * Randy Dunlap fixed u64 printk compiler waring
101 * Remove FCS from BW calculation. Lennert Buytenhek <buytenh@wantstofly.org>
102 * New time handling. Lennert Buytenhek <buytenh@wantstofly.org> 041213
104 * Corrections from Nikolai Malykh (nmalykh@bilim.com)
105 * Removed unused flags F_SET_SRCMAC & F_SET_SRCIP 041230
107 * interruptible_sleep_on_timeout() replaced Nishanth Aravamudan <nacc@us.ibm.com>
110 * MPLS support by Steven Whitehouse <steve@chygwyn.com>
112 * 802.1Q/Q-in-Q support by Francesco Fondelli (FF) <francesco.fondelli@gmail.com>
114 * Fixed src_mac command to set source mac of packet to value specified in
115 * command by Adit Ranadive <adit.262@gmail.com>
118 #include <linux/sys.h>
119 #include <linux/types.h>
120 #include <linux/module.h>
121 #include <linux/moduleparam.h>
122 #include <linux/kernel.h>
123 #include <linux/mutex.h>
124 #include <linux/sched.h>
125 #include <linux/slab.h>
126 #include <linux/vmalloc.h>
127 #include <linux/unistd.h>
128 #include <linux/string.h>
129 #include <linux/ptrace.h>
130 #include <linux/errno.h>
131 #include <linux/ioport.h>
132 #include <linux/interrupt.h>
133 #include <linux/capability.h>
134 #include <linux/freezer.h>
135 #include <linux/delay.h>
136 #include <linux/timer.h>
137 #include <linux/list.h>
138 #include <linux/init.h>
139 #include <linux/skbuff.h>
140 #include <linux/netdevice.h>
141 #include <linux/inet.h>
142 #include <linux/inetdevice.h>
143 #include <linux/rtnetlink.h>
144 #include <linux/if_arp.h>
145 #include <linux/if_vlan.h>
146 #include <linux/in.h>
147 #include <linux/ip.h>
148 #include <linux/ipv6.h>
149 #include <linux/udp.h>
150 #include <linux/proc_fs.h>
151 #include <linux/seq_file.h>
152 #include <linux/wait.h>
153 #include <linux/etherdevice.h>
154 #include <linux/kthread.h>
155 #include <net/checksum.h>
156 #include <net/ipv6.h>
157 #include <net/addrconf.h>
158 #include <asm/byteorder.h>
159 #include <linux/rcupdate.h>
160 #include <asm/bitops.h>
163 #include <asm/uaccess.h>
164 #include <asm/div64.h> /* do_div */
165 #include <asm/timex.h>
167 #define VERSION "pktgen v2.68: Packet Generator for packet performance testing.\n"
169 /* The buckets are exponential in 'width' */
170 #define LAT_BUCKETS_MAX 32
171 #define IP_NAME_SZ 32
172 #define MAX_MPLS_LABELS 16 /* This is the max label stack depth */
173 #define MPLS_STACK_BOTTOM htonl(0x00000100)
175 /* Device flag bits */
176 #define F_IPSRC_RND (1<<0) /* IP-Src Random */
177 #define F_IPDST_RND (1<<1) /* IP-Dst Random */
178 #define F_UDPSRC_RND (1<<2) /* UDP-Src Random */
179 #define F_UDPDST_RND (1<<3) /* UDP-Dst Random */
180 #define F_MACSRC_RND (1<<4) /* MAC-Src Random */
181 #define F_MACDST_RND (1<<5) /* MAC-Dst Random */
182 #define F_TXSIZE_RND (1<<6) /* Transmit size is random */
183 #define F_IPV6 (1<<7) /* Interface in IPV6 Mode */
184 #define F_MPLS_RND (1<<8) /* Random MPLS labels */
185 #define F_VID_RND (1<<9) /* Random VLAN ID */
186 #define F_SVID_RND (1<<10) /* Random SVLAN ID */
188 /* Thread control flag bits */
189 #define T_TERMINATE (1<<0)
190 #define T_STOP (1<<1) /* Stop run */
191 #define T_RUN (1<<2) /* Start run */
192 #define T_REMDEVALL (1<<3) /* Remove all devs */
193 #define T_REMDEV (1<<4) /* Remove one dev */
195 /* If lock -- can be removed after some work */
196 #define if_lock(t) spin_lock(&(t->if_lock));
197 #define if_unlock(t) spin_unlock(&(t->if_lock));
199 /* Used to help with determining the pkts on receive */
200 #define PKTGEN_MAGIC 0xbe9be955
201 #define PG_PROC_DIR "pktgen"
202 #define PGCTRL "pgctrl"
203 static struct proc_dir_entry
*pg_proc_dir
= NULL
;
205 #define MAX_CFLOWS 65536
207 #define VLAN_TAG_SIZE(x) ((x)->vlan_id == 0xffff ? 0 : 4)
208 #define SVLAN_TAG_SIZE(x) ((x)->svlan_id == 0xffff ? 0 : 4)
217 * Try to keep frequent/infrequent used vars. separated.
219 struct proc_dir_entry
*entry
; /* proc file */
220 struct pktgen_thread
*pg_thread
;/* the owner */
221 struct list_head list
; /* Used for chaining in the thread's run-queue */
223 int running
; /* if this changes to false, the test will stop */
225 /* If min != max, then we will either do a linear iteration, or
226 * we will do a random selection from within the range.
229 int removal_mark
; /* non-zero => the device is marked for
230 * removal by worker thread */
232 int min_pkt_size
; /* = ETH_ZLEN; */
233 int max_pkt_size
; /* = ETH_ZLEN; */
235 __u32 delay_us
; /* Default delay */
237 __u64 count
; /* Default No packets to send */
238 __u64 sofar
; /* How many pkts we've sent so far */
239 __u64 tx_bytes
; /* How many bytes we've transmitted */
240 __u64 errors
; /* Errors when trying to transmit, pkts will be re-sent */
242 /* runtime counters relating to clone_skb */
243 __u64 next_tx_us
; /* timestamp of when to tx next */
246 __u64 allocated_skbs
;
248 int last_ok
; /* Was last skb sent?
249 * Or a failed transmit of some sort? This will keep
250 * sequence numbers in order, for example.
252 __u64 started_at
; /* micro-seconds */
253 __u64 stopped_at
; /* micro-seconds */
254 __u64 idle_acc
; /* micro-seconds */
257 int clone_skb
; /* Use multiple SKBs during packet gen. If this number
258 * is greater than 1, then that many copies of the same
259 * packet will be sent before a new packet is allocated.
260 * For instance, if you want to send 1024 identical packets
261 * before creating a new packet, set clone_skb to 1024.
264 char dst_min
[IP_NAME_SZ
]; /* IP, ie 1.2.3.4 */
265 char dst_max
[IP_NAME_SZ
]; /* IP, ie 1.2.3.4 */
266 char src_min
[IP_NAME_SZ
]; /* IP, ie 1.2.3.4 */
267 char src_max
[IP_NAME_SZ
]; /* IP, ie 1.2.3.4 */
269 struct in6_addr in6_saddr
;
270 struct in6_addr in6_daddr
;
271 struct in6_addr cur_in6_daddr
;
272 struct in6_addr cur_in6_saddr
;
274 struct in6_addr min_in6_daddr
;
275 struct in6_addr max_in6_daddr
;
276 struct in6_addr min_in6_saddr
;
277 struct in6_addr max_in6_saddr
;
279 /* If we're doing ranges, random or incremental, then this
280 * defines the min/max for those ranges.
282 __be32 saddr_min
; /* inclusive, source IP address */
283 __be32 saddr_max
; /* exclusive, source IP address */
284 __be32 daddr_min
; /* inclusive, dest IP address */
285 __be32 daddr_max
; /* exclusive, dest IP address */
287 __u16 udp_src_min
; /* inclusive, source UDP port */
288 __u16 udp_src_max
; /* exclusive, source UDP port */
289 __u16 udp_dst_min
; /* inclusive, dest UDP port */
290 __u16 udp_dst_max
; /* exclusive, dest UDP port */
293 __u8 tos
; /* six most significant bits of (former) IPv4 TOS are for dscp codepoint */
294 __u8 traffic_class
; /* ditto for the (former) Traffic Class in IPv6 (see RFC 3260, sec. 4) */
297 unsigned nr_labels
; /* Depth of stack, 0 = no MPLS */
298 __be32 labels
[MAX_MPLS_LABELS
];
300 /* VLAN/SVLAN (802.1Q/Q-in-Q) */
303 __u16 vlan_id
; /* 0xffff means no vlan tag */
307 __u16 svlan_id
; /* 0xffff means no svlan tag */
309 __u32 src_mac_count
; /* How many MACs to iterate through */
310 __u32 dst_mac_count
; /* How many MACs to iterate through */
312 unsigned char dst_mac
[ETH_ALEN
];
313 unsigned char src_mac
[ETH_ALEN
];
315 __u32 cur_dst_mac_offset
;
316 __u32 cur_src_mac_offset
;
325 0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
327 We fill in SRC address later
328 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
332 __u16 pad
; /* pad out the hh struct to an even 16 bytes */
334 struct sk_buff
*skb
; /* skb we are to transmit next, mainly used for when we
335 * are transmitting the same one multiple times
337 struct net_device
*odev
; /* The out-going device. Note that the device should
338 * have it's pg_info pointer pointing back to this
339 * device. This will be set when the user specifies
340 * the out-going device name (not when the inject is
341 * started as it used to do.)
343 struct flow_state
*flows
;
344 unsigned cflows
; /* Concurrent flows (config) */
345 unsigned lflow
; /* Flow length (config) */
346 unsigned nflows
; /* accumulated flows (stats) */
358 struct pktgen_thread
{
360 struct list_head if_list
; /* All device here */
361 struct list_head th_list
;
362 struct task_struct
*tsk
;
364 u32 max_before_softirq
; /* We'll call do_softirq to prevent starvation. */
366 /* Field for thread to receive "posted" events terminate, stop ifs etc. */
372 wait_queue_head_t queue
;
378 /* This code works around the fact that do_div cannot handle two 64-bit
379 numbers, and regular 64-bit division doesn't work on x86 kernels.
385 /* This was emailed to LMKL by: Chris Caputo <ccaputo@alt.net>
386 * Function copied/adapted/optimized from:
388 * nemesis.sourceforge.net/browse/lib/static/intmath/ix86/intmath.c.html
390 * Copyright 1994, University of Cambridge Computer Laboratory
391 * All Rights Reserved.
394 static inline s64
divremdi3(s64 x
, s64 y
, int type
)
396 u64 a
= (x
< 0) ? -x
: x
;
397 u64 b
= (y
< 0) ? -y
: y
;
417 if (PG_DIV
== type
) {
418 return (((x
^ y
) & (1ll << 63)) == 0) ? res
: -(s64
) res
;
420 return ((x
& (1ll << 63)) == 0) ? a
: -(s64
) a
;
424 /* End of hacks to deal with 64-bit math on x86 */
426 /** Convert to milliseconds */
427 static inline __u64
tv_to_ms(const struct timeval
*tv
)
429 __u64 ms
= tv
->tv_usec
/ 1000;
430 ms
+= (__u64
) tv
->tv_sec
* (__u64
) 1000;
434 /** Convert to micro-seconds */
435 static inline __u64
tv_to_us(const struct timeval
*tv
)
437 __u64 us
= tv
->tv_usec
;
438 us
+= (__u64
) tv
->tv_sec
* (__u64
) 1000000;
442 static inline __u64
pg_div(__u64 n
, __u32 base
)
446 /* printk("pktgen: pg_div, n: %llu base: %d rv: %llu\n",
451 static inline __u64
pg_div64(__u64 n
, __u64 base
)
455 * How do we know if the architecture we are running on
456 * supports division with 64 bit base?
459 #if defined(__sparc_v9__) || defined(__powerpc64__) || defined(__alpha__) || defined(__x86_64__) || defined(__ia64__)
463 tmp
= divremdi3(n
, base
, PG_DIV
);
468 static inline __u64
getCurMs(void)
471 do_gettimeofday(&tv
);
472 return tv_to_ms(&tv
);
475 static inline __u64
getCurUs(void)
478 do_gettimeofday(&tv
);
479 return tv_to_us(&tv
);
482 static inline __u64
tv_diff(const struct timeval
*a
, const struct timeval
*b
)
484 return tv_to_us(a
) - tv_to_us(b
);
487 /* old include end */
489 static char version
[] __initdata
= VERSION
;
491 static int pktgen_remove_device(struct pktgen_thread
*t
, struct pktgen_dev
*i
);
492 static int pktgen_add_device(struct pktgen_thread
*t
, const char *ifname
);
493 static struct pktgen_dev
*pktgen_find_dev(struct pktgen_thread
*t
,
495 static int pktgen_device_event(struct notifier_block
*, unsigned long, void *);
496 static void pktgen_run_all_threads(void);
497 static void pktgen_stop_all_threads_ifs(void);
498 static int pktgen_stop_device(struct pktgen_dev
*pkt_dev
);
499 static void pktgen_stop(struct pktgen_thread
*t
);
500 static void pktgen_clear_counters(struct pktgen_dev
*pkt_dev
);
502 static unsigned int scan_ip6(const char *s
, char ip
[16]);
503 static unsigned int fmt_ip6(char *s
, const char ip
[16]);
505 /* Module parameters, defaults. */
506 static int pg_count_d
= 1000; /* 1000 pkts by default */
507 static int pg_delay_d
;
508 static int pg_clone_skb_d
;
511 static DEFINE_MUTEX(pktgen_thread_lock
);
512 static LIST_HEAD(pktgen_threads
);
514 static struct notifier_block pktgen_notifier_block
= {
515 .notifier_call
= pktgen_device_event
,
519 * /proc handling functions
523 static int pgctrl_show(struct seq_file
*seq
, void *v
)
525 seq_puts(seq
, VERSION
);
529 static ssize_t
pgctrl_write(struct file
*file
, const char __user
* buf
,
530 size_t count
, loff_t
* ppos
)
535 if (!capable(CAP_NET_ADMIN
)) {
540 if (count
> sizeof(data
))
541 count
= sizeof(data
);
543 if (copy_from_user(data
, buf
, count
)) {
547 data
[count
- 1] = 0; /* Make string */
549 if (!strcmp(data
, "stop"))
550 pktgen_stop_all_threads_ifs();
552 else if (!strcmp(data
, "start"))
553 pktgen_run_all_threads();
556 printk("pktgen: Unknown command: %s\n", data
);
564 static int pgctrl_open(struct inode
*inode
, struct file
*file
)
566 return single_open(file
, pgctrl_show
, PDE(inode
)->data
);
569 static const struct file_operations pktgen_fops
= {
570 .owner
= THIS_MODULE
,
574 .write
= pgctrl_write
,
575 .release
= single_release
,
578 static int pktgen_if_show(struct seq_file
*seq
, void *v
)
581 struct pktgen_dev
*pkt_dev
= seq
->private;
584 __u64 now
= getCurUs();
587 "Params: count %llu min_pkt_size: %u max_pkt_size: %u\n",
588 (unsigned long long)pkt_dev
->count
, pkt_dev
->min_pkt_size
,
589 pkt_dev
->max_pkt_size
);
592 " frags: %d delay: %u clone_skb: %d ifname: %s\n",
594 1000 * pkt_dev
->delay_us
+ pkt_dev
->delay_ns
,
595 pkt_dev
->clone_skb
, pkt_dev
->odev
->name
);
597 seq_printf(seq
, " flows: %u flowlen: %u\n", pkt_dev
->cflows
,
600 if (pkt_dev
->flags
& F_IPV6
) {
601 char b1
[128], b2
[128], b3
[128];
602 fmt_ip6(b1
, pkt_dev
->in6_saddr
.s6_addr
);
603 fmt_ip6(b2
, pkt_dev
->min_in6_saddr
.s6_addr
);
604 fmt_ip6(b3
, pkt_dev
->max_in6_saddr
.s6_addr
);
606 " saddr: %s min_saddr: %s max_saddr: %s\n", b1
,
609 fmt_ip6(b1
, pkt_dev
->in6_daddr
.s6_addr
);
610 fmt_ip6(b2
, pkt_dev
->min_in6_daddr
.s6_addr
);
611 fmt_ip6(b3
, pkt_dev
->max_in6_daddr
.s6_addr
);
613 " daddr: %s min_daddr: %s max_daddr: %s\n", b1
,
618 " dst_min: %s dst_max: %s\n src_min: %s src_max: %s\n",
619 pkt_dev
->dst_min
, pkt_dev
->dst_max
, pkt_dev
->src_min
,
622 seq_puts(seq
, " src_mac: ");
624 if (is_zero_ether_addr(pkt_dev
->src_mac
))
625 for (i
= 0; i
< 6; i
++)
626 seq_printf(seq
, "%02X%s", pkt_dev
->odev
->dev_addr
[i
],
629 for (i
= 0; i
< 6; i
++)
630 seq_printf(seq
, "%02X%s", pkt_dev
->src_mac
[i
],
633 seq_printf(seq
, "dst_mac: ");
634 for (i
= 0; i
< 6; i
++)
635 seq_printf(seq
, "%02X%s", pkt_dev
->dst_mac
[i
],
636 i
== 5 ? "\n" : ":");
639 " udp_src_min: %d udp_src_max: %d udp_dst_min: %d udp_dst_max: %d\n",
640 pkt_dev
->udp_src_min
, pkt_dev
->udp_src_max
,
641 pkt_dev
->udp_dst_min
, pkt_dev
->udp_dst_max
);
644 " src_mac_count: %d dst_mac_count: %d\n",
645 pkt_dev
->src_mac_count
, pkt_dev
->dst_mac_count
);
647 if (pkt_dev
->nr_labels
) {
649 seq_printf(seq
, " mpls: ");
650 for (i
= 0; i
< pkt_dev
->nr_labels
; i
++)
651 seq_printf(seq
, "%08x%s", ntohl(pkt_dev
->labels
[i
]),
652 i
== pkt_dev
->nr_labels
-1 ? "\n" : ", ");
655 if (pkt_dev
->vlan_id
!= 0xffff) {
656 seq_printf(seq
, " vlan_id: %u vlan_p: %u vlan_cfi: %u\n",
657 pkt_dev
->vlan_id
, pkt_dev
->vlan_p
, pkt_dev
->vlan_cfi
);
660 if (pkt_dev
->svlan_id
!= 0xffff) {
661 seq_printf(seq
, " svlan_id: %u vlan_p: %u vlan_cfi: %u\n",
662 pkt_dev
->svlan_id
, pkt_dev
->svlan_p
, pkt_dev
->svlan_cfi
);
666 seq_printf(seq
, " tos: 0x%02x\n", pkt_dev
->tos
);
669 if (pkt_dev
->traffic_class
) {
670 seq_printf(seq
, " traffic_class: 0x%02x\n", pkt_dev
->traffic_class
);
673 seq_printf(seq
, " Flags: ");
675 if (pkt_dev
->flags
& F_IPV6
)
676 seq_printf(seq
, "IPV6 ");
678 if (pkt_dev
->flags
& F_IPSRC_RND
)
679 seq_printf(seq
, "IPSRC_RND ");
681 if (pkt_dev
->flags
& F_IPDST_RND
)
682 seq_printf(seq
, "IPDST_RND ");
684 if (pkt_dev
->flags
& F_TXSIZE_RND
)
685 seq_printf(seq
, "TXSIZE_RND ");
687 if (pkt_dev
->flags
& F_UDPSRC_RND
)
688 seq_printf(seq
, "UDPSRC_RND ");
690 if (pkt_dev
->flags
& F_UDPDST_RND
)
691 seq_printf(seq
, "UDPDST_RND ");
693 if (pkt_dev
->flags
& F_MPLS_RND
)
694 seq_printf(seq
, "MPLS_RND ");
696 if (pkt_dev
->flags
& F_MACSRC_RND
)
697 seq_printf(seq
, "MACSRC_RND ");
699 if (pkt_dev
->flags
& F_MACDST_RND
)
700 seq_printf(seq
, "MACDST_RND ");
702 if (pkt_dev
->flags
& F_VID_RND
)
703 seq_printf(seq
, "VID_RND ");
705 if (pkt_dev
->flags
& F_SVID_RND
)
706 seq_printf(seq
, "SVID_RND ");
710 sa
= pkt_dev
->started_at
;
711 stopped
= pkt_dev
->stopped_at
;
712 if (pkt_dev
->running
)
713 stopped
= now
; /* not really stopped, more like last-running-at */
716 "Current:\n pkts-sofar: %llu errors: %llu\n started: %lluus stopped: %lluus idle: %lluus\n",
717 (unsigned long long)pkt_dev
->sofar
,
718 (unsigned long long)pkt_dev
->errors
, (unsigned long long)sa
,
719 (unsigned long long)stopped
,
720 (unsigned long long)pkt_dev
->idle_acc
);
723 " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n",
724 pkt_dev
->seq_num
, pkt_dev
->cur_dst_mac_offset
,
725 pkt_dev
->cur_src_mac_offset
);
727 if (pkt_dev
->flags
& F_IPV6
) {
728 char b1
[128], b2
[128];
729 fmt_ip6(b1
, pkt_dev
->cur_in6_daddr
.s6_addr
);
730 fmt_ip6(b2
, pkt_dev
->cur_in6_saddr
.s6_addr
);
731 seq_printf(seq
, " cur_saddr: %s cur_daddr: %s\n", b2
, b1
);
733 seq_printf(seq
, " cur_saddr: 0x%x cur_daddr: 0x%x\n",
734 pkt_dev
->cur_saddr
, pkt_dev
->cur_daddr
);
736 seq_printf(seq
, " cur_udp_dst: %d cur_udp_src: %d\n",
737 pkt_dev
->cur_udp_dst
, pkt_dev
->cur_udp_src
);
739 seq_printf(seq
, " flows: %u\n", pkt_dev
->nflows
);
741 if (pkt_dev
->result
[0])
742 seq_printf(seq
, "Result: %s\n", pkt_dev
->result
);
744 seq_printf(seq
, "Result: Idle\n");
750 static int hex32_arg(const char __user
*user_buffer
, unsigned long maxlen
, __u32
*num
)
755 for (; i
< maxlen
; i
++) {
758 if (get_user(c
, &user_buffer
[i
]))
760 if ((c
>= '0') && (c
<= '9'))
762 else if ((c
>= 'a') && (c
<= 'f'))
763 *num
|= c
- 'a' + 10;
764 else if ((c
>= 'A') && (c
<= 'F'))
765 *num
|= c
- 'A' + 10;
772 static int count_trail_chars(const char __user
* user_buffer
,
777 for (i
= 0; i
< maxlen
; i
++) {
779 if (get_user(c
, &user_buffer
[i
]))
797 static unsigned long num_arg(const char __user
* user_buffer
,
798 unsigned long maxlen
, unsigned long *num
)
803 for (; i
< maxlen
; i
++) {
805 if (get_user(c
, &user_buffer
[i
]))
807 if ((c
>= '0') && (c
<= '9')) {
816 static int strn_len(const char __user
* user_buffer
, unsigned int maxlen
)
820 for (; i
< maxlen
; i
++) {
822 if (get_user(c
, &user_buffer
[i
]))
840 static ssize_t
get_labels(const char __user
*buffer
, struct pktgen_dev
*pkt_dev
)
847 pkt_dev
->nr_labels
= 0;
850 len
= hex32_arg(&buffer
[i
], 8, &tmp
);
853 pkt_dev
->labels
[n
] = htonl(tmp
);
854 if (pkt_dev
->labels
[n
] & MPLS_STACK_BOTTOM
)
855 pkt_dev
->flags
|= F_MPLS_RND
;
857 if (get_user(c
, &buffer
[i
]))
861 if (n
>= MAX_MPLS_LABELS
)
865 pkt_dev
->nr_labels
= n
;
869 static ssize_t
pktgen_if_write(struct file
*file
,
870 const char __user
* user_buffer
, size_t count
,
873 struct seq_file
*seq
= (struct seq_file
*)file
->private_data
;
874 struct pktgen_dev
*pkt_dev
= seq
->private;
876 char name
[16], valstr
[32];
877 unsigned long value
= 0;
878 char *pg_result
= NULL
;
882 pg_result
= &(pkt_dev
->result
[0]);
885 printk("pktgen: wrong command format\n");
890 tmp
= count_trail_chars(&user_buffer
[i
], max
);
892 printk("pktgen: illegal format\n");
897 /* Read variable name */
899 len
= strn_len(&user_buffer
[i
], sizeof(name
) - 1);
903 memset(name
, 0, sizeof(name
));
904 if (copy_from_user(name
, &user_buffer
[i
], len
))
909 len
= count_trail_chars(&user_buffer
[i
], max
);
917 if (copy_from_user(tb
, user_buffer
, count
))
920 printk("pktgen: %s,%lu buffer -:%s:-\n", name
,
921 (unsigned long)count
, tb
);
924 if (!strcmp(name
, "min_pkt_size")) {
925 len
= num_arg(&user_buffer
[i
], 10, &value
);
930 if (value
< 14 + 20 + 8)
932 if (value
!= pkt_dev
->min_pkt_size
) {
933 pkt_dev
->min_pkt_size
= value
;
934 pkt_dev
->cur_pkt_size
= value
;
936 sprintf(pg_result
, "OK: min_pkt_size=%u",
937 pkt_dev
->min_pkt_size
);
941 if (!strcmp(name
, "max_pkt_size")) {
942 len
= num_arg(&user_buffer
[i
], 10, &value
);
947 if (value
< 14 + 20 + 8)
949 if (value
!= pkt_dev
->max_pkt_size
) {
950 pkt_dev
->max_pkt_size
= value
;
951 pkt_dev
->cur_pkt_size
= value
;
953 sprintf(pg_result
, "OK: max_pkt_size=%u",
954 pkt_dev
->max_pkt_size
);
958 /* Shortcut for min = max */
960 if (!strcmp(name
, "pkt_size")) {
961 len
= num_arg(&user_buffer
[i
], 10, &value
);
966 if (value
< 14 + 20 + 8)
968 if (value
!= pkt_dev
->min_pkt_size
) {
969 pkt_dev
->min_pkt_size
= value
;
970 pkt_dev
->max_pkt_size
= value
;
971 pkt_dev
->cur_pkt_size
= value
;
973 sprintf(pg_result
, "OK: pkt_size=%u", pkt_dev
->min_pkt_size
);
977 if (!strcmp(name
, "debug")) {
978 len
= num_arg(&user_buffer
[i
], 10, &value
);
984 sprintf(pg_result
, "OK: debug=%u", debug
);
988 if (!strcmp(name
, "frags")) {
989 len
= num_arg(&user_buffer
[i
], 10, &value
);
994 pkt_dev
->nfrags
= value
;
995 sprintf(pg_result
, "OK: frags=%u", pkt_dev
->nfrags
);
998 if (!strcmp(name
, "delay")) {
999 len
= num_arg(&user_buffer
[i
], 10, &value
);
1004 if (value
== 0x7FFFFFFF) {
1005 pkt_dev
->delay_us
= 0x7FFFFFFF;
1006 pkt_dev
->delay_ns
= 0;
1008 pkt_dev
->delay_us
= value
/ 1000;
1009 pkt_dev
->delay_ns
= value
% 1000;
1011 sprintf(pg_result
, "OK: delay=%u",
1012 1000 * pkt_dev
->delay_us
+ pkt_dev
->delay_ns
);
1015 if (!strcmp(name
, "udp_src_min")) {
1016 len
= num_arg(&user_buffer
[i
], 10, &value
);
1021 if (value
!= pkt_dev
->udp_src_min
) {
1022 pkt_dev
->udp_src_min
= value
;
1023 pkt_dev
->cur_udp_src
= value
;
1025 sprintf(pg_result
, "OK: udp_src_min=%u", pkt_dev
->udp_src_min
);
1028 if (!strcmp(name
, "udp_dst_min")) {
1029 len
= num_arg(&user_buffer
[i
], 10, &value
);
1034 if (value
!= pkt_dev
->udp_dst_min
) {
1035 pkt_dev
->udp_dst_min
= value
;
1036 pkt_dev
->cur_udp_dst
= value
;
1038 sprintf(pg_result
, "OK: udp_dst_min=%u", pkt_dev
->udp_dst_min
);
1041 if (!strcmp(name
, "udp_src_max")) {
1042 len
= num_arg(&user_buffer
[i
], 10, &value
);
1047 if (value
!= pkt_dev
->udp_src_max
) {
1048 pkt_dev
->udp_src_max
= value
;
1049 pkt_dev
->cur_udp_src
= value
;
1051 sprintf(pg_result
, "OK: udp_src_max=%u", pkt_dev
->udp_src_max
);
1054 if (!strcmp(name
, "udp_dst_max")) {
1055 len
= num_arg(&user_buffer
[i
], 10, &value
);
1060 if (value
!= pkt_dev
->udp_dst_max
) {
1061 pkt_dev
->udp_dst_max
= value
;
1062 pkt_dev
->cur_udp_dst
= value
;
1064 sprintf(pg_result
, "OK: udp_dst_max=%u", pkt_dev
->udp_dst_max
);
1067 if (!strcmp(name
, "clone_skb")) {
1068 len
= num_arg(&user_buffer
[i
], 10, &value
);
1073 pkt_dev
->clone_skb
= value
;
1075 sprintf(pg_result
, "OK: clone_skb=%d", pkt_dev
->clone_skb
);
1078 if (!strcmp(name
, "count")) {
1079 len
= num_arg(&user_buffer
[i
], 10, &value
);
1084 pkt_dev
->count
= value
;
1085 sprintf(pg_result
, "OK: count=%llu",
1086 (unsigned long long)pkt_dev
->count
);
1089 if (!strcmp(name
, "src_mac_count")) {
1090 len
= num_arg(&user_buffer
[i
], 10, &value
);
1095 if (pkt_dev
->src_mac_count
!= value
) {
1096 pkt_dev
->src_mac_count
= value
;
1097 pkt_dev
->cur_src_mac_offset
= 0;
1099 sprintf(pg_result
, "OK: src_mac_count=%d",
1100 pkt_dev
->src_mac_count
);
1103 if (!strcmp(name
, "dst_mac_count")) {
1104 len
= num_arg(&user_buffer
[i
], 10, &value
);
1109 if (pkt_dev
->dst_mac_count
!= value
) {
1110 pkt_dev
->dst_mac_count
= value
;
1111 pkt_dev
->cur_dst_mac_offset
= 0;
1113 sprintf(pg_result
, "OK: dst_mac_count=%d",
1114 pkt_dev
->dst_mac_count
);
1117 if (!strcmp(name
, "flag")) {
1120 len
= strn_len(&user_buffer
[i
], sizeof(f
) - 1);
1124 if (copy_from_user(f
, &user_buffer
[i
], len
))
1127 if (strcmp(f
, "IPSRC_RND") == 0)
1128 pkt_dev
->flags
|= F_IPSRC_RND
;
1130 else if (strcmp(f
, "!IPSRC_RND") == 0)
1131 pkt_dev
->flags
&= ~F_IPSRC_RND
;
1133 else if (strcmp(f
, "TXSIZE_RND") == 0)
1134 pkt_dev
->flags
|= F_TXSIZE_RND
;
1136 else if (strcmp(f
, "!TXSIZE_RND") == 0)
1137 pkt_dev
->flags
&= ~F_TXSIZE_RND
;
1139 else if (strcmp(f
, "IPDST_RND") == 0)
1140 pkt_dev
->flags
|= F_IPDST_RND
;
1142 else if (strcmp(f
, "!IPDST_RND") == 0)
1143 pkt_dev
->flags
&= ~F_IPDST_RND
;
1145 else if (strcmp(f
, "UDPSRC_RND") == 0)
1146 pkt_dev
->flags
|= F_UDPSRC_RND
;
1148 else if (strcmp(f
, "!UDPSRC_RND") == 0)
1149 pkt_dev
->flags
&= ~F_UDPSRC_RND
;
1151 else if (strcmp(f
, "UDPDST_RND") == 0)
1152 pkt_dev
->flags
|= F_UDPDST_RND
;
1154 else if (strcmp(f
, "!UDPDST_RND") == 0)
1155 pkt_dev
->flags
&= ~F_UDPDST_RND
;
1157 else if (strcmp(f
, "MACSRC_RND") == 0)
1158 pkt_dev
->flags
|= F_MACSRC_RND
;
1160 else if (strcmp(f
, "!MACSRC_RND") == 0)
1161 pkt_dev
->flags
&= ~F_MACSRC_RND
;
1163 else if (strcmp(f
, "MACDST_RND") == 0)
1164 pkt_dev
->flags
|= F_MACDST_RND
;
1166 else if (strcmp(f
, "!MACDST_RND") == 0)
1167 pkt_dev
->flags
&= ~F_MACDST_RND
;
1169 else if (strcmp(f
, "MPLS_RND") == 0)
1170 pkt_dev
->flags
|= F_MPLS_RND
;
1172 else if (strcmp(f
, "!MPLS_RND") == 0)
1173 pkt_dev
->flags
&= ~F_MPLS_RND
;
1175 else if (strcmp(f
, "VID_RND") == 0)
1176 pkt_dev
->flags
|= F_VID_RND
;
1178 else if (strcmp(f
, "!VID_RND") == 0)
1179 pkt_dev
->flags
&= ~F_VID_RND
;
1181 else if (strcmp(f
, "SVID_RND") == 0)
1182 pkt_dev
->flags
|= F_SVID_RND
;
1184 else if (strcmp(f
, "!SVID_RND") == 0)
1185 pkt_dev
->flags
&= ~F_SVID_RND
;
1187 else if (strcmp(f
, "!IPV6") == 0)
1188 pkt_dev
->flags
&= ~F_IPV6
;
1192 "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
1194 "IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
1195 "MACSRC_RND, MACDST_RND, TXSIZE_RND, IPV6, MPLS_RND, VID_RND, SVID_RND\n");
1198 sprintf(pg_result
, "OK: flags=0x%x", pkt_dev
->flags
);
1201 if (!strcmp(name
, "dst_min") || !strcmp(name
, "dst")) {
1202 len
= strn_len(&user_buffer
[i
], sizeof(pkt_dev
->dst_min
) - 1);
1207 if (copy_from_user(buf
, &user_buffer
[i
], len
))
1210 if (strcmp(buf
, pkt_dev
->dst_min
) != 0) {
1211 memset(pkt_dev
->dst_min
, 0, sizeof(pkt_dev
->dst_min
));
1212 strncpy(pkt_dev
->dst_min
, buf
, len
);
1213 pkt_dev
->daddr_min
= in_aton(pkt_dev
->dst_min
);
1214 pkt_dev
->cur_daddr
= pkt_dev
->daddr_min
;
1217 printk("pktgen: dst_min set to: %s\n",
1220 sprintf(pg_result
, "OK: dst_min=%s", pkt_dev
->dst_min
);
1223 if (!strcmp(name
, "dst_max")) {
1224 len
= strn_len(&user_buffer
[i
], sizeof(pkt_dev
->dst_max
) - 1);
1229 if (copy_from_user(buf
, &user_buffer
[i
], len
))
1233 if (strcmp(buf
, pkt_dev
->dst_max
) != 0) {
1234 memset(pkt_dev
->dst_max
, 0, sizeof(pkt_dev
->dst_max
));
1235 strncpy(pkt_dev
->dst_max
, buf
, len
);
1236 pkt_dev
->daddr_max
= in_aton(pkt_dev
->dst_max
);
1237 pkt_dev
->cur_daddr
= pkt_dev
->daddr_max
;
1240 printk("pktgen: dst_max set to: %s\n",
1243 sprintf(pg_result
, "OK: dst_max=%s", pkt_dev
->dst_max
);
1246 if (!strcmp(name
, "dst6")) {
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
->in6_daddr
.s6_addr
);
1258 fmt_ip6(buf
, pkt_dev
->in6_daddr
.s6_addr
);
1260 ipv6_addr_copy(&pkt_dev
->cur_in6_daddr
, &pkt_dev
->in6_daddr
);
1263 printk("pktgen: dst6 set to: %s\n", buf
);
1266 sprintf(pg_result
, "OK: dst6=%s", buf
);
1269 if (!strcmp(name
, "dst6_min")) {
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
->min_in6_daddr
.s6_addr
);
1281 fmt_ip6(buf
, pkt_dev
->min_in6_daddr
.s6_addr
);
1283 ipv6_addr_copy(&pkt_dev
->cur_in6_daddr
,
1284 &pkt_dev
->min_in6_daddr
);
1286 printk("pktgen: dst6_min set to: %s\n", buf
);
1289 sprintf(pg_result
, "OK: dst6_min=%s", buf
);
1292 if (!strcmp(name
, "dst6_max")) {
1293 len
= strn_len(&user_buffer
[i
], sizeof(buf
) - 1);
1297 pkt_dev
->flags
|= F_IPV6
;
1299 if (copy_from_user(buf
, &user_buffer
[i
], len
))
1303 scan_ip6(buf
, pkt_dev
->max_in6_daddr
.s6_addr
);
1304 fmt_ip6(buf
, pkt_dev
->max_in6_daddr
.s6_addr
);
1307 printk("pktgen: dst6_max set to: %s\n", buf
);
1310 sprintf(pg_result
, "OK: dst6_max=%s", buf
);
1313 if (!strcmp(name
, "src6")) {
1314 len
= strn_len(&user_buffer
[i
], sizeof(buf
) - 1);
1318 pkt_dev
->flags
|= F_IPV6
;
1320 if (copy_from_user(buf
, &user_buffer
[i
], len
))
1324 scan_ip6(buf
, pkt_dev
->in6_saddr
.s6_addr
);
1325 fmt_ip6(buf
, pkt_dev
->in6_saddr
.s6_addr
);
1327 ipv6_addr_copy(&pkt_dev
->cur_in6_saddr
, &pkt_dev
->in6_saddr
);
1330 printk("pktgen: src6 set to: %s\n", buf
);
1333 sprintf(pg_result
, "OK: src6=%s", buf
);
1336 if (!strcmp(name
, "src_min")) {
1337 len
= strn_len(&user_buffer
[i
], sizeof(pkt_dev
->src_min
) - 1);
1341 if (copy_from_user(buf
, &user_buffer
[i
], len
))
1344 if (strcmp(buf
, pkt_dev
->src_min
) != 0) {
1345 memset(pkt_dev
->src_min
, 0, sizeof(pkt_dev
->src_min
));
1346 strncpy(pkt_dev
->src_min
, buf
, len
);
1347 pkt_dev
->saddr_min
= in_aton(pkt_dev
->src_min
);
1348 pkt_dev
->cur_saddr
= pkt_dev
->saddr_min
;
1351 printk("pktgen: src_min set to: %s\n",
1354 sprintf(pg_result
, "OK: src_min=%s", pkt_dev
->src_min
);
1357 if (!strcmp(name
, "src_max")) {
1358 len
= strn_len(&user_buffer
[i
], sizeof(pkt_dev
->src_max
) - 1);
1362 if (copy_from_user(buf
, &user_buffer
[i
], len
))
1365 if (strcmp(buf
, pkt_dev
->src_max
) != 0) {
1366 memset(pkt_dev
->src_max
, 0, sizeof(pkt_dev
->src_max
));
1367 strncpy(pkt_dev
->src_max
, buf
, len
);
1368 pkt_dev
->saddr_max
= in_aton(pkt_dev
->src_max
);
1369 pkt_dev
->cur_saddr
= pkt_dev
->saddr_max
;
1372 printk("pktgen: src_max set to: %s\n",
1375 sprintf(pg_result
, "OK: src_max=%s", pkt_dev
->src_max
);
1378 if (!strcmp(name
, "dst_mac")) {
1380 unsigned char old_dmac
[ETH_ALEN
];
1381 unsigned char *m
= pkt_dev
->dst_mac
;
1382 memcpy(old_dmac
, pkt_dev
->dst_mac
, ETH_ALEN
);
1384 len
= strn_len(&user_buffer
[i
], sizeof(valstr
) - 1);
1388 memset(valstr
, 0, sizeof(valstr
));
1389 if (copy_from_user(valstr
, &user_buffer
[i
], len
))
1393 for (*m
= 0; *v
&& m
< pkt_dev
->dst_mac
+ 6; v
++) {
1394 if (*v
>= '0' && *v
<= '9') {
1398 if (*v
>= 'A' && *v
<= 'F') {
1400 *m
+= *v
- 'A' + 10;
1402 if (*v
>= 'a' && *v
<= 'f') {
1404 *m
+= *v
- 'a' + 10;
1412 /* Set up Dest MAC */
1413 if (compare_ether_addr(old_dmac
, pkt_dev
->dst_mac
))
1414 memcpy(&(pkt_dev
->hh
[0]), pkt_dev
->dst_mac
, ETH_ALEN
);
1416 sprintf(pg_result
, "OK: dstmac");
1419 if (!strcmp(name
, "src_mac")) {
1421 unsigned char old_smac
[ETH_ALEN
];
1422 unsigned char *m
= pkt_dev
->src_mac
;
1424 memcpy(old_smac
, pkt_dev
->src_mac
, ETH_ALEN
);
1426 len
= strn_len(&user_buffer
[i
], sizeof(valstr
) - 1);
1430 memset(valstr
, 0, sizeof(valstr
));
1431 if (copy_from_user(valstr
, &user_buffer
[i
], len
))
1435 for (*m
= 0; *v
&& m
< pkt_dev
->src_mac
+ 6; v
++) {
1436 if (*v
>= '0' && *v
<= '9') {
1440 if (*v
>= 'A' && *v
<= 'F') {
1442 *m
+= *v
- 'A' + 10;
1444 if (*v
>= 'a' && *v
<= 'f') {
1446 *m
+= *v
- 'a' + 10;
1454 /* Set up Src MAC */
1455 if (compare_ether_addr(old_smac
, pkt_dev
->src_mac
))
1456 memcpy(&(pkt_dev
->hh
[6]), pkt_dev
->src_mac
, ETH_ALEN
);
1458 sprintf(pg_result
, "OK: srcmac");
1462 if (!strcmp(name
, "clear_counters")) {
1463 pktgen_clear_counters(pkt_dev
);
1464 sprintf(pg_result
, "OK: Clearing counters.\n");
1468 if (!strcmp(name
, "flows")) {
1469 len
= num_arg(&user_buffer
[i
], 10, &value
);
1474 if (value
> MAX_CFLOWS
)
1477 pkt_dev
->cflows
= value
;
1478 sprintf(pg_result
, "OK: flows=%u", pkt_dev
->cflows
);
1482 if (!strcmp(name
, "flowlen")) {
1483 len
= num_arg(&user_buffer
[i
], 10, &value
);
1488 pkt_dev
->lflow
= value
;
1489 sprintf(pg_result
, "OK: flowlen=%u", pkt_dev
->lflow
);
1493 if (!strcmp(name
, "mpls")) {
1495 len
= get_labels(&user_buffer
[i
], pkt_dev
);
1496 if (len
< 0) { return len
; }
1498 offset
= sprintf(pg_result
, "OK: mpls=");
1499 for (n
= 0; n
< pkt_dev
->nr_labels
; n
++)
1500 offset
+= sprintf(pg_result
+ offset
,
1501 "%08x%s", ntohl(pkt_dev
->labels
[n
]),
1502 n
== pkt_dev
->nr_labels
-1 ? "" : ",");
1504 if (pkt_dev
->nr_labels
&& pkt_dev
->vlan_id
!= 0xffff) {
1505 pkt_dev
->vlan_id
= 0xffff; /* turn off VLAN/SVLAN */
1506 pkt_dev
->svlan_id
= 0xffff;
1509 printk("pktgen: VLAN/SVLAN auto turned off\n");
1514 if (!strcmp(name
, "vlan_id")) {
1515 len
= num_arg(&user_buffer
[i
], 4, &value
);
1520 if (value
<= 4095) {
1521 pkt_dev
->vlan_id
= value
; /* turn on VLAN */
1524 printk("pktgen: VLAN turned on\n");
1526 if (debug
&& pkt_dev
->nr_labels
)
1527 printk("pktgen: MPLS auto turned off\n");
1529 pkt_dev
->nr_labels
= 0; /* turn off MPLS */
1530 sprintf(pg_result
, "OK: vlan_id=%u", pkt_dev
->vlan_id
);
1532 pkt_dev
->vlan_id
= 0xffff; /* turn off VLAN/SVLAN */
1533 pkt_dev
->svlan_id
= 0xffff;
1536 printk("pktgen: VLAN/SVLAN turned off\n");
1541 if (!strcmp(name
, "vlan_p")) {
1542 len
= num_arg(&user_buffer
[i
], 1, &value
);
1547 if ((value
<= 7) && (pkt_dev
->vlan_id
!= 0xffff)) {
1548 pkt_dev
->vlan_p
= value
;
1549 sprintf(pg_result
, "OK: vlan_p=%u", pkt_dev
->vlan_p
);
1551 sprintf(pg_result
, "ERROR: vlan_p must be 0-7");
1556 if (!strcmp(name
, "vlan_cfi")) {
1557 len
= num_arg(&user_buffer
[i
], 1, &value
);
1562 if ((value
<= 1) && (pkt_dev
->vlan_id
!= 0xffff)) {
1563 pkt_dev
->vlan_cfi
= value
;
1564 sprintf(pg_result
, "OK: vlan_cfi=%u", pkt_dev
->vlan_cfi
);
1566 sprintf(pg_result
, "ERROR: vlan_cfi must be 0-1");
1571 if (!strcmp(name
, "svlan_id")) {
1572 len
= num_arg(&user_buffer
[i
], 4, &value
);
1577 if ((value
<= 4095) && ((pkt_dev
->vlan_id
!= 0xffff))) {
1578 pkt_dev
->svlan_id
= value
; /* turn on SVLAN */
1581 printk("pktgen: SVLAN turned on\n");
1583 if (debug
&& pkt_dev
->nr_labels
)
1584 printk("pktgen: MPLS auto turned off\n");
1586 pkt_dev
->nr_labels
= 0; /* turn off MPLS */
1587 sprintf(pg_result
, "OK: svlan_id=%u", pkt_dev
->svlan_id
);
1589 pkt_dev
->vlan_id
= 0xffff; /* turn off VLAN/SVLAN */
1590 pkt_dev
->svlan_id
= 0xffff;
1593 printk("pktgen: VLAN/SVLAN turned off\n");
1598 if (!strcmp(name
, "svlan_p")) {
1599 len
= num_arg(&user_buffer
[i
], 1, &value
);
1604 if ((value
<= 7) && (pkt_dev
->svlan_id
!= 0xffff)) {
1605 pkt_dev
->svlan_p
= value
;
1606 sprintf(pg_result
, "OK: svlan_p=%u", pkt_dev
->svlan_p
);
1608 sprintf(pg_result
, "ERROR: svlan_p must be 0-7");
1613 if (!strcmp(name
, "svlan_cfi")) {
1614 len
= num_arg(&user_buffer
[i
], 1, &value
);
1619 if ((value
<= 1) && (pkt_dev
->svlan_id
!= 0xffff)) {
1620 pkt_dev
->svlan_cfi
= value
;
1621 sprintf(pg_result
, "OK: svlan_cfi=%u", pkt_dev
->svlan_cfi
);
1623 sprintf(pg_result
, "ERROR: svlan_cfi must be 0-1");
1628 if (!strcmp(name
, "tos")) {
1629 __u32 tmp_value
= 0;
1630 len
= hex32_arg(&user_buffer
[i
], 2, &tmp_value
);
1636 pkt_dev
->tos
= tmp_value
;
1637 sprintf(pg_result
, "OK: tos=0x%02x", pkt_dev
->tos
);
1639 sprintf(pg_result
, "ERROR: tos must be 00-ff");
1644 if (!strcmp(name
, "traffic_class")) {
1645 __u32 tmp_value
= 0;
1646 len
= hex32_arg(&user_buffer
[i
], 2, &tmp_value
);
1652 pkt_dev
->traffic_class
= tmp_value
;
1653 sprintf(pg_result
, "OK: traffic_class=0x%02x", pkt_dev
->traffic_class
);
1655 sprintf(pg_result
, "ERROR: traffic_class must be 00-ff");
1660 sprintf(pkt_dev
->result
, "No such parameter \"%s\"", name
);
1664 static int pktgen_if_open(struct inode
*inode
, struct file
*file
)
1666 return single_open(file
, pktgen_if_show
, PDE(inode
)->data
);
1669 static const struct file_operations pktgen_if_fops
= {
1670 .owner
= THIS_MODULE
,
1671 .open
= pktgen_if_open
,
1673 .llseek
= seq_lseek
,
1674 .write
= pktgen_if_write
,
1675 .release
= single_release
,
1678 static int pktgen_thread_show(struct seq_file
*seq
, void *v
)
1680 struct pktgen_thread
*t
= seq
->private;
1681 struct pktgen_dev
*pkt_dev
;
1685 seq_printf(seq
, "Name: %s max_before_softirq: %d\n",
1686 t
->tsk
->comm
, t
->max_before_softirq
);
1688 seq_printf(seq
, "Running: ");
1691 list_for_each_entry(pkt_dev
, &t
->if_list
, list
)
1692 if (pkt_dev
->running
)
1693 seq_printf(seq
, "%s ", pkt_dev
->odev
->name
);
1695 seq_printf(seq
, "\nStopped: ");
1697 list_for_each_entry(pkt_dev
, &t
->if_list
, list
)
1698 if (!pkt_dev
->running
)
1699 seq_printf(seq
, "%s ", pkt_dev
->odev
->name
);
1702 seq_printf(seq
, "\nResult: %s\n", t
->result
);
1704 seq_printf(seq
, "\nResult: NA\n");
1711 static ssize_t
pktgen_thread_write(struct file
*file
,
1712 const char __user
* user_buffer
,
1713 size_t count
, loff_t
* offset
)
1715 struct seq_file
*seq
= (struct seq_file
*)file
->private_data
;
1716 struct pktgen_thread
*t
= seq
->private;
1717 int i
= 0, max
, len
, ret
;
1720 unsigned long value
= 0;
1723 // sprintf(pg_result, "Wrong command format");
1728 len
= count_trail_chars(&user_buffer
[i
], max
);
1734 /* Read variable name */
1736 len
= strn_len(&user_buffer
[i
], sizeof(name
) - 1);
1740 memset(name
, 0, sizeof(name
));
1741 if (copy_from_user(name
, &user_buffer
[i
], len
))
1746 len
= count_trail_chars(&user_buffer
[i
], max
);
1753 printk("pktgen: t=%s, count=%lu\n", name
, (unsigned long)count
);
1756 printk("pktgen: ERROR: No thread\n");
1761 pg_result
= &(t
->result
[0]);
1763 if (!strcmp(name
, "add_device")) {
1766 len
= strn_len(&user_buffer
[i
], sizeof(f
) - 1);
1771 if (copy_from_user(f
, &user_buffer
[i
], len
))
1774 mutex_lock(&pktgen_thread_lock
);
1775 pktgen_add_device(t
, f
);
1776 mutex_unlock(&pktgen_thread_lock
);
1778 sprintf(pg_result
, "OK: add_device=%s", f
);
1782 if (!strcmp(name
, "rem_device_all")) {
1783 mutex_lock(&pktgen_thread_lock
);
1784 t
->control
|= T_REMDEVALL
;
1785 mutex_unlock(&pktgen_thread_lock
);
1786 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
1788 sprintf(pg_result
, "OK: rem_device_all");
1792 if (!strcmp(name
, "max_before_softirq")) {
1793 len
= num_arg(&user_buffer
[i
], 10, &value
);
1794 mutex_lock(&pktgen_thread_lock
);
1795 t
->max_before_softirq
= value
;
1796 mutex_unlock(&pktgen_thread_lock
);
1798 sprintf(pg_result
, "OK: max_before_softirq=%lu", value
);
1807 static int pktgen_thread_open(struct inode
*inode
, struct file
*file
)
1809 return single_open(file
, pktgen_thread_show
, PDE(inode
)->data
);
1812 static const struct file_operations pktgen_thread_fops
= {
1813 .owner
= THIS_MODULE
,
1814 .open
= pktgen_thread_open
,
1816 .llseek
= seq_lseek
,
1817 .write
= pktgen_thread_write
,
1818 .release
= single_release
,
1821 /* Think find or remove for NN */
1822 static struct pktgen_dev
*__pktgen_NN_threads(const char *ifname
, int remove
)
1824 struct pktgen_thread
*t
;
1825 struct pktgen_dev
*pkt_dev
= NULL
;
1827 list_for_each_entry(t
, &pktgen_threads
, th_list
) {
1828 pkt_dev
= pktgen_find_dev(t
, ifname
);
1832 pkt_dev
->removal_mark
= 1;
1833 t
->control
|= T_REMDEV
;
1843 * mark a device for removal
1845 static void pktgen_mark_device(const char *ifname
)
1847 struct pktgen_dev
*pkt_dev
= NULL
;
1848 const int max_tries
= 10, msec_per_try
= 125;
1851 mutex_lock(&pktgen_thread_lock
);
1852 pr_debug("pktgen: pktgen_mark_device marking %s for removal\n", ifname
);
1856 pkt_dev
= __pktgen_NN_threads(ifname
, REMOVE
);
1857 if (pkt_dev
== NULL
)
1858 break; /* success */
1860 mutex_unlock(&pktgen_thread_lock
);
1861 pr_debug("pktgen: pktgen_mark_device waiting for %s "
1862 "to disappear....\n", ifname
);
1863 schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try
));
1864 mutex_lock(&pktgen_thread_lock
);
1866 if (++i
>= max_tries
) {
1867 printk("pktgen_mark_device: timed out after waiting "
1868 "%d msec for device %s to be removed\n",
1869 msec_per_try
* i
, ifname
);
1875 mutex_unlock(&pktgen_thread_lock
);
1878 static void pktgen_change_name(struct net_device
*dev
)
1880 struct pktgen_thread
*t
;
1882 list_for_each_entry(t
, &pktgen_threads
, th_list
) {
1883 struct pktgen_dev
*pkt_dev
;
1885 list_for_each_entry(pkt_dev
, &t
->if_list
, list
) {
1886 if (pkt_dev
->odev
!= dev
)
1889 remove_proc_entry(pkt_dev
->entry
->name
, pg_proc_dir
);
1891 pkt_dev
->entry
= create_proc_entry(dev
->name
, 0600,
1893 if (!pkt_dev
->entry
)
1894 printk(KERN_ERR
"pktgen: can't move proc "
1895 " entry for '%s'\n", dev
->name
);
1901 static int pktgen_device_event(struct notifier_block
*unused
,
1902 unsigned long event
, void *ptr
)
1904 struct net_device
*dev
= ptr
;
1906 /* It is OK that we do not hold the group lock right now,
1907 * as we run under the RTNL lock.
1911 case NETDEV_CHANGENAME
:
1912 pktgen_change_name(dev
);
1915 case NETDEV_UNREGISTER
:
1916 pktgen_mark_device(dev
->name
);
1923 /* Associate pktgen_dev with a device. */
1925 static int pktgen_setup_dev(struct pktgen_dev
*pkt_dev
, const char *ifname
)
1927 struct net_device
*odev
;
1930 /* Clean old setups */
1931 if (pkt_dev
->odev
) {
1932 dev_put(pkt_dev
->odev
);
1933 pkt_dev
->odev
= NULL
;
1936 odev
= dev_get_by_name(ifname
);
1938 printk("pktgen: no such netdevice: \"%s\"\n", ifname
);
1942 if (odev
->type
!= ARPHRD_ETHER
) {
1943 printk("pktgen: not an ethernet device: \"%s\"\n", ifname
);
1945 } else if (!netif_running(odev
)) {
1946 printk("pktgen: device is down: \"%s\"\n", ifname
);
1949 pkt_dev
->odev
= odev
;
1957 /* Read pkt_dev from the interface and set up internal pktgen_dev
1958 * structure to have the right information to create/send packets
1960 static void pktgen_setup_inject(struct pktgen_dev
*pkt_dev
)
1962 if (!pkt_dev
->odev
) {
1963 printk("pktgen: ERROR: pkt_dev->odev == NULL in setup_inject.\n");
1964 sprintf(pkt_dev
->result
,
1965 "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
1969 /* Default to the interface's mac if not explicitly set. */
1971 if (is_zero_ether_addr(pkt_dev
->src_mac
))
1972 memcpy(&(pkt_dev
->hh
[6]), pkt_dev
->odev
->dev_addr
, ETH_ALEN
);
1974 /* Set up Dest MAC */
1975 memcpy(&(pkt_dev
->hh
[0]), pkt_dev
->dst_mac
, ETH_ALEN
);
1977 /* Set up pkt size */
1978 pkt_dev
->cur_pkt_size
= pkt_dev
->min_pkt_size
;
1980 if (pkt_dev
->flags
& F_IPV6
) {
1982 * Skip this automatic address setting until locks or functions
1987 int i
, set
= 0, err
= 1;
1988 struct inet6_dev
*idev
;
1990 for (i
= 0; i
< IN6_ADDR_HSIZE
; i
++)
1991 if (pkt_dev
->cur_in6_saddr
.s6_addr
[i
]) {
1999 * Use linklevel address if unconfigured.
2001 * use ipv6_get_lladdr if/when it's get exported
2005 if ((idev
= __in6_dev_get(pkt_dev
->odev
)) != NULL
) {
2006 struct inet6_ifaddr
*ifp
;
2008 read_lock_bh(&idev
->lock
);
2009 for (ifp
= idev
->addr_list
; ifp
;
2010 ifp
= ifp
->if_next
) {
2011 if (ifp
->scope
== IFA_LINK
2013 flags
& IFA_F_TENTATIVE
)) {
2014 ipv6_addr_copy(&pkt_dev
->
2021 read_unlock_bh(&idev
->lock
);
2025 printk("pktgen: ERROR: IPv6 link address not availble.\n");
2029 pkt_dev
->saddr_min
= 0;
2030 pkt_dev
->saddr_max
= 0;
2031 if (strlen(pkt_dev
->src_min
) == 0) {
2033 struct in_device
*in_dev
;
2036 in_dev
= __in_dev_get_rcu(pkt_dev
->odev
);
2038 if (in_dev
->ifa_list
) {
2039 pkt_dev
->saddr_min
=
2040 in_dev
->ifa_list
->ifa_address
;
2041 pkt_dev
->saddr_max
= pkt_dev
->saddr_min
;
2046 pkt_dev
->saddr_min
= in_aton(pkt_dev
->src_min
);
2047 pkt_dev
->saddr_max
= in_aton(pkt_dev
->src_max
);
2050 pkt_dev
->daddr_min
= in_aton(pkt_dev
->dst_min
);
2051 pkt_dev
->daddr_max
= in_aton(pkt_dev
->dst_max
);
2053 /* Initialize current values. */
2054 pkt_dev
->cur_dst_mac_offset
= 0;
2055 pkt_dev
->cur_src_mac_offset
= 0;
2056 pkt_dev
->cur_saddr
= pkt_dev
->saddr_min
;
2057 pkt_dev
->cur_daddr
= pkt_dev
->daddr_min
;
2058 pkt_dev
->cur_udp_dst
= pkt_dev
->udp_dst_min
;
2059 pkt_dev
->cur_udp_src
= pkt_dev
->udp_src_min
;
2060 pkt_dev
->nflows
= 0;
2063 static void spin(struct pktgen_dev
*pkt_dev
, __u64 spin_until_us
)
2068 start
= now
= getCurUs();
2069 printk(KERN_INFO
"sleeping for %d\n", (int)(spin_until_us
- now
));
2070 while (now
< spin_until_us
) {
2071 /* TODO: optimize sleeping behavior */
2072 if (spin_until_us
- now
> jiffies_to_usecs(1) + 1)
2073 schedule_timeout_interruptible(1);
2074 else if (spin_until_us
- now
> 100) {
2076 if (!pkt_dev
->running
)
2085 pkt_dev
->idle_acc
+= now
- start
;
2088 /* Increment/randomize headers according to flags and current values
2089 * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
2091 static void mod_cur_headers(struct pktgen_dev
*pkt_dev
)
2097 if (pkt_dev
->cflows
) {
2098 flow
= random32() % pkt_dev
->cflows
;
2100 if (pkt_dev
->flows
[flow
].count
> pkt_dev
->lflow
)
2101 pkt_dev
->flows
[flow
].count
= 0;
2104 /* Deal with source MAC */
2105 if (pkt_dev
->src_mac_count
> 1) {
2109 if (pkt_dev
->flags
& F_MACSRC_RND
)
2110 mc
= random32() % pkt_dev
->src_mac_count
;
2112 mc
= pkt_dev
->cur_src_mac_offset
++;
2113 if (pkt_dev
->cur_src_mac_offset
>
2114 pkt_dev
->src_mac_count
)
2115 pkt_dev
->cur_src_mac_offset
= 0;
2118 tmp
= pkt_dev
->src_mac
[5] + (mc
& 0xFF);
2119 pkt_dev
->hh
[11] = tmp
;
2120 tmp
= (pkt_dev
->src_mac
[4] + ((mc
>> 8) & 0xFF) + (tmp
>> 8));
2121 pkt_dev
->hh
[10] = tmp
;
2122 tmp
= (pkt_dev
->src_mac
[3] + ((mc
>> 16) & 0xFF) + (tmp
>> 8));
2123 pkt_dev
->hh
[9] = tmp
;
2124 tmp
= (pkt_dev
->src_mac
[2] + ((mc
>> 24) & 0xFF) + (tmp
>> 8));
2125 pkt_dev
->hh
[8] = tmp
;
2126 tmp
= (pkt_dev
->src_mac
[1] + (tmp
>> 8));
2127 pkt_dev
->hh
[7] = tmp
;
2130 /* Deal with Destination MAC */
2131 if (pkt_dev
->dst_mac_count
> 1) {
2135 if (pkt_dev
->flags
& F_MACDST_RND
)
2136 mc
= random32() % pkt_dev
->dst_mac_count
;
2139 mc
= pkt_dev
->cur_dst_mac_offset
++;
2140 if (pkt_dev
->cur_dst_mac_offset
>
2141 pkt_dev
->dst_mac_count
) {
2142 pkt_dev
->cur_dst_mac_offset
= 0;
2146 tmp
= pkt_dev
->dst_mac
[5] + (mc
& 0xFF);
2147 pkt_dev
->hh
[5] = tmp
;
2148 tmp
= (pkt_dev
->dst_mac
[4] + ((mc
>> 8) & 0xFF) + (tmp
>> 8));
2149 pkt_dev
->hh
[4] = tmp
;
2150 tmp
= (pkt_dev
->dst_mac
[3] + ((mc
>> 16) & 0xFF) + (tmp
>> 8));
2151 pkt_dev
->hh
[3] = tmp
;
2152 tmp
= (pkt_dev
->dst_mac
[2] + ((mc
>> 24) & 0xFF) + (tmp
>> 8));
2153 pkt_dev
->hh
[2] = tmp
;
2154 tmp
= (pkt_dev
->dst_mac
[1] + (tmp
>> 8));
2155 pkt_dev
->hh
[1] = tmp
;
2158 if (pkt_dev
->flags
& F_MPLS_RND
) {
2160 for (i
= 0; i
< pkt_dev
->nr_labels
; i
++)
2161 if (pkt_dev
->labels
[i
] & MPLS_STACK_BOTTOM
)
2162 pkt_dev
->labels
[i
] = MPLS_STACK_BOTTOM
|
2163 ((__force __be32
)random32() &
2167 if ((pkt_dev
->flags
& F_VID_RND
) && (pkt_dev
->vlan_id
!= 0xffff)) {
2168 pkt_dev
->vlan_id
= random32() & (4096-1);
2171 if ((pkt_dev
->flags
& F_SVID_RND
) && (pkt_dev
->svlan_id
!= 0xffff)) {
2172 pkt_dev
->svlan_id
= random32() & (4096 - 1);
2175 if (pkt_dev
->udp_src_min
< pkt_dev
->udp_src_max
) {
2176 if (pkt_dev
->flags
& F_UDPSRC_RND
)
2177 pkt_dev
->cur_udp_src
= random32() %
2178 (pkt_dev
->udp_src_max
- pkt_dev
->udp_src_min
)
2179 + pkt_dev
->udp_src_min
;
2182 pkt_dev
->cur_udp_src
++;
2183 if (pkt_dev
->cur_udp_src
>= pkt_dev
->udp_src_max
)
2184 pkt_dev
->cur_udp_src
= pkt_dev
->udp_src_min
;
2188 if (pkt_dev
->udp_dst_min
< pkt_dev
->udp_dst_max
) {
2189 if (pkt_dev
->flags
& F_UDPDST_RND
) {
2190 pkt_dev
->cur_udp_dst
= random32() %
2191 (pkt_dev
->udp_dst_max
- pkt_dev
->udp_dst_min
)
2192 + pkt_dev
->udp_dst_min
;
2194 pkt_dev
->cur_udp_dst
++;
2195 if (pkt_dev
->cur_udp_dst
>= pkt_dev
->udp_dst_max
)
2196 pkt_dev
->cur_udp_dst
= pkt_dev
->udp_dst_min
;
2200 if (!(pkt_dev
->flags
& F_IPV6
)) {
2202 if ((imn
= ntohl(pkt_dev
->saddr_min
)) < (imx
=
2206 if (pkt_dev
->flags
& F_IPSRC_RND
)
2207 t
= random32() % (imx
- imn
) + imn
;
2209 t
= ntohl(pkt_dev
->cur_saddr
);
2215 pkt_dev
->cur_saddr
= htonl(t
);
2218 if (pkt_dev
->cflows
&& pkt_dev
->flows
[flow
].count
!= 0) {
2219 pkt_dev
->cur_daddr
= pkt_dev
->flows
[flow
].cur_daddr
;
2221 imn
= ntohl(pkt_dev
->daddr_min
);
2222 imx
= ntohl(pkt_dev
->daddr_max
);
2226 if (pkt_dev
->flags
& F_IPDST_RND
) {
2228 t
= random32() % (imx
- imn
) + imn
;
2231 while (LOOPBACK(s
) || MULTICAST(s
)
2232 || BADCLASS(s
) || ZERONET(s
)
2233 || LOCAL_MCAST(s
)) {
2234 t
= random32() % (imx
- imn
) + imn
;
2237 pkt_dev
->cur_daddr
= s
;
2239 t
= ntohl(pkt_dev
->cur_daddr
);
2244 pkt_dev
->cur_daddr
= htonl(t
);
2247 if (pkt_dev
->cflows
) {
2248 pkt_dev
->flows
[flow
].cur_daddr
=
2253 } else { /* IPV6 * */
2255 if (pkt_dev
->min_in6_daddr
.s6_addr32
[0] == 0 &&
2256 pkt_dev
->min_in6_daddr
.s6_addr32
[1] == 0 &&
2257 pkt_dev
->min_in6_daddr
.s6_addr32
[2] == 0 &&
2258 pkt_dev
->min_in6_daddr
.s6_addr32
[3] == 0) ;
2262 /* Only random destinations yet */
2264 for (i
= 0; i
< 4; i
++) {
2265 pkt_dev
->cur_in6_daddr
.s6_addr32
[i
] =
2266 (((__force __be32
)random32() |
2267 pkt_dev
->min_in6_daddr
.s6_addr32
[i
]) &
2268 pkt_dev
->max_in6_daddr
.s6_addr32
[i
]);
2273 if (pkt_dev
->min_pkt_size
< pkt_dev
->max_pkt_size
) {
2275 if (pkt_dev
->flags
& F_TXSIZE_RND
) {
2277 (pkt_dev
->max_pkt_size
- pkt_dev
->min_pkt_size
)
2278 + pkt_dev
->min_pkt_size
;
2280 t
= pkt_dev
->cur_pkt_size
+ 1;
2281 if (t
> pkt_dev
->max_pkt_size
)
2282 t
= pkt_dev
->min_pkt_size
;
2284 pkt_dev
->cur_pkt_size
= t
;
2287 pkt_dev
->flows
[flow
].count
++;
2290 static void mpls_push(__be32
*mpls
, struct pktgen_dev
*pkt_dev
)
2293 for (i
= 0; i
< pkt_dev
->nr_labels
; i
++) {
2294 *mpls
++ = pkt_dev
->labels
[i
] & ~MPLS_STACK_BOTTOM
;
2297 *mpls
|= MPLS_STACK_BOTTOM
;
2300 static inline __be16
build_tci(unsigned int id
, unsigned int cfi
,
2303 return htons(id
| (cfi
<< 12) | (prio
<< 13));
2306 static struct sk_buff
*fill_packet_ipv4(struct net_device
*odev
,
2307 struct pktgen_dev
*pkt_dev
)
2309 struct sk_buff
*skb
= NULL
;
2311 struct udphdr
*udph
;
2314 struct pktgen_hdr
*pgh
= NULL
;
2315 __be16 protocol
= htons(ETH_P_IP
);
2317 __be16
*vlan_tci
= NULL
; /* Encapsulates priority and VLAN ID */
2318 __be16
*vlan_encapsulated_proto
= NULL
; /* packet type ID field (or len) for VLAN tag */
2319 __be16
*svlan_tci
= NULL
; /* Encapsulates priority and SVLAN ID */
2320 __be16
*svlan_encapsulated_proto
= NULL
; /* packet type ID field (or len) for SVLAN tag */
2323 if (pkt_dev
->nr_labels
)
2324 protocol
= htons(ETH_P_MPLS_UC
);
2326 if (pkt_dev
->vlan_id
!= 0xffff)
2327 protocol
= htons(ETH_P_8021Q
);
2329 /* Update any of the values, used when we're incrementing various
2332 mod_cur_headers(pkt_dev
);
2334 datalen
= (odev
->hard_header_len
+ 16) & ~0xf;
2335 skb
= alloc_skb(pkt_dev
->cur_pkt_size
+ 64 + datalen
+
2336 pkt_dev
->nr_labels
*sizeof(u32
) +
2337 VLAN_TAG_SIZE(pkt_dev
) + SVLAN_TAG_SIZE(pkt_dev
),
2340 sprintf(pkt_dev
->result
, "No memory");
2344 skb_reserve(skb
, datalen
);
2346 /* Reserve for ethernet and IP header */
2347 eth
= (__u8
*) skb_push(skb
, 14);
2348 mpls
= (__be32
*)skb_put(skb
, pkt_dev
->nr_labels
*sizeof(__u32
));
2349 if (pkt_dev
->nr_labels
)
2350 mpls_push(mpls
, pkt_dev
);
2352 if (pkt_dev
->vlan_id
!= 0xffff) {
2353 if (pkt_dev
->svlan_id
!= 0xffff) {
2354 svlan_tci
= (__be16
*)skb_put(skb
, sizeof(__be16
));
2355 *svlan_tci
= build_tci(pkt_dev
->svlan_id
,
2358 svlan_encapsulated_proto
= (__be16
*)skb_put(skb
, sizeof(__be16
));
2359 *svlan_encapsulated_proto
= htons(ETH_P_8021Q
);
2361 vlan_tci
= (__be16
*)skb_put(skb
, sizeof(__be16
));
2362 *vlan_tci
= build_tci(pkt_dev
->vlan_id
,
2365 vlan_encapsulated_proto
= (__be16
*)skb_put(skb
, sizeof(__be16
));
2366 *vlan_encapsulated_proto
= htons(ETH_P_IP
);
2369 skb
->network_header
= skb
->tail
;
2370 skb
->transport_header
= skb
->network_header
+ sizeof(struct iphdr
);
2371 skb_put(skb
, sizeof(struct iphdr
) + sizeof(struct udphdr
));
2374 udph
= udp_hdr(skb
);
2376 memcpy(eth
, pkt_dev
->hh
, 12);
2377 *(__be16
*) & eth
[12] = protocol
;
2379 /* Eth + IPh + UDPh + mpls */
2380 datalen
= pkt_dev
->cur_pkt_size
- 14 - 20 - 8 -
2381 pkt_dev
->nr_labels
*sizeof(u32
) - VLAN_TAG_SIZE(pkt_dev
) - SVLAN_TAG_SIZE(pkt_dev
);
2382 if (datalen
< sizeof(struct pktgen_hdr
))
2383 datalen
= sizeof(struct pktgen_hdr
);
2385 udph
->source
= htons(pkt_dev
->cur_udp_src
);
2386 udph
->dest
= htons(pkt_dev
->cur_udp_dst
);
2387 udph
->len
= htons(datalen
+ 8); /* DATA + udphdr */
2388 udph
->check
= 0; /* No checksum */
2393 iph
->tos
= pkt_dev
->tos
;
2394 iph
->protocol
= IPPROTO_UDP
; /* UDP */
2395 iph
->saddr
= pkt_dev
->cur_saddr
;
2396 iph
->daddr
= pkt_dev
->cur_daddr
;
2398 iplen
= 20 + 8 + datalen
;
2399 iph
->tot_len
= htons(iplen
);
2401 iph
->check
= ip_fast_csum((void *)iph
, iph
->ihl
);
2402 skb
->protocol
= protocol
;
2403 skb
->mac_header
= (skb
->network_header
- ETH_HLEN
-
2404 pkt_dev
->nr_labels
* sizeof(u32
) -
2405 VLAN_TAG_SIZE(pkt_dev
) - SVLAN_TAG_SIZE(pkt_dev
));
2407 skb
->pkt_type
= PACKET_HOST
;
2409 if (pkt_dev
->nfrags
<= 0)
2410 pgh
= (struct pktgen_hdr
*)skb_put(skb
, datalen
);
2412 int frags
= pkt_dev
->nfrags
;
2415 pgh
= (struct pktgen_hdr
*)(((char *)(udph
)) + 8);
2417 if (frags
> MAX_SKB_FRAGS
)
2418 frags
= MAX_SKB_FRAGS
;
2419 if (datalen
> frags
* PAGE_SIZE
) {
2420 skb_put(skb
, datalen
- frags
* PAGE_SIZE
);
2421 datalen
= frags
* PAGE_SIZE
;
2425 while (datalen
> 0) {
2426 struct page
*page
= alloc_pages(GFP_KERNEL
, 0);
2427 skb_shinfo(skb
)->frags
[i
].page
= page
;
2428 skb_shinfo(skb
)->frags
[i
].page_offset
= 0;
2429 skb_shinfo(skb
)->frags
[i
].size
=
2430 (datalen
< PAGE_SIZE
? datalen
: PAGE_SIZE
);
2431 datalen
-= skb_shinfo(skb
)->frags
[i
].size
;
2432 skb
->len
+= skb_shinfo(skb
)->frags
[i
].size
;
2433 skb
->data_len
+= skb_shinfo(skb
)->frags
[i
].size
;
2435 skb_shinfo(skb
)->nr_frags
= i
;
2444 rem
= skb_shinfo(skb
)->frags
[i
- 1].size
/ 2;
2448 skb_shinfo(skb
)->frags
[i
- 1].size
-= rem
;
2450 skb_shinfo(skb
)->frags
[i
] =
2451 skb_shinfo(skb
)->frags
[i
- 1];
2452 get_page(skb_shinfo(skb
)->frags
[i
].page
);
2453 skb_shinfo(skb
)->frags
[i
].page
=
2454 skb_shinfo(skb
)->frags
[i
- 1].page
;
2455 skb_shinfo(skb
)->frags
[i
].page_offset
+=
2456 skb_shinfo(skb
)->frags
[i
- 1].size
;
2457 skb_shinfo(skb
)->frags
[i
].size
= rem
;
2459 skb_shinfo(skb
)->nr_frags
= i
;
2463 /* Stamp the time, and sequence number, convert them to network byte order */
2466 struct timeval timestamp
;
2468 pgh
->pgh_magic
= htonl(PKTGEN_MAGIC
);
2469 pgh
->seq_num
= htonl(pkt_dev
->seq_num
);
2471 do_gettimeofday(×tamp
);
2472 pgh
->tv_sec
= htonl(timestamp
.tv_sec
);
2473 pgh
->tv_usec
= htonl(timestamp
.tv_usec
);
2480 * scan_ip6, fmt_ip taken from dietlibc-0.21
2481 * Author Felix von Leitner <felix-dietlibc@fefe.de>
2483 * Slightly modified for kernel.
2484 * Should be candidate for net/ipv4/utils.c
2488 static unsigned int scan_ip6(const char *s
, char ip
[16])
2491 unsigned int len
= 0;
2494 unsigned int prefixlen
= 0;
2495 unsigned int suffixlen
= 0;
2498 for (i
= 0; i
< 16; i
++)
2504 if (s
[1] == ':') { /* Found "::", skip to part 2 */
2513 u
= simple_strtoul(s
, &tmp
, 16);
2519 if (prefixlen
== 12 && s
[i
] == '.') {
2521 /* the last 4 bytes may be written as IPv4 address */
2524 memcpy((struct in_addr
*)(ip
+ 12), &tmp
, sizeof(tmp
));
2527 ip
[prefixlen
++] = (u
>> 8);
2528 ip
[prefixlen
++] = (u
& 255);
2531 if (prefixlen
== 16)
2535 /* part 2, after "::" */
2542 } else if (suffixlen
!= 0)
2546 u
= simple_strtol(s
, &tmp
, 16);
2554 if (suffixlen
+ prefixlen
<= 12 && s
[i
] == '.') {
2556 memcpy((struct in_addr
*)(suffix
+ suffixlen
), &tmp
,
2562 suffix
[suffixlen
++] = (u
>> 8);
2563 suffix
[suffixlen
++] = (u
& 255);
2566 if (prefixlen
+ suffixlen
== 16)
2569 for (i
= 0; i
< suffixlen
; i
++)
2570 ip
[16 - suffixlen
+ i
] = suffix
[i
];
2574 static char tohex(char hexdigit
)
2576 return hexdigit
> 9 ? hexdigit
+ 'a' - 10 : hexdigit
+ '0';
2579 static int fmt_xlong(char *s
, unsigned int i
)
2582 *s
= tohex((i
>> 12) & 0xf);
2583 if (s
!= bak
|| *s
!= '0')
2585 *s
= tohex((i
>> 8) & 0xf);
2586 if (s
!= bak
|| *s
!= '0')
2588 *s
= tohex((i
>> 4) & 0xf);
2589 if (s
!= bak
|| *s
!= '0')
2591 *s
= tohex(i
& 0xf);
2595 static unsigned int fmt_ip6(char *s
, const char ip
[16])
2600 unsigned int compressing
;
2605 for (j
= 0; j
< 16; j
+= 2) {
2607 #ifdef V4MAPPEDPREFIX
2608 if (j
== 12 && !memcmp(ip
, V4mappedprefix
, 12)) {
2609 inet_ntoa_r(*(struct in_addr
*)(ip
+ 12), s
);
2614 temp
= ((unsigned long)(unsigned char)ip
[j
] << 8) +
2615 (unsigned long)(unsigned char)ip
[j
+ 1];
2630 i
= fmt_xlong(s
, temp
);
2647 static struct sk_buff
*fill_packet_ipv6(struct net_device
*odev
,
2648 struct pktgen_dev
*pkt_dev
)
2650 struct sk_buff
*skb
= NULL
;
2652 struct udphdr
*udph
;
2654 struct ipv6hdr
*iph
;
2655 struct pktgen_hdr
*pgh
= NULL
;
2656 __be16 protocol
= htons(ETH_P_IPV6
);
2658 __be16
*vlan_tci
= NULL
; /* Encapsulates priority and VLAN ID */
2659 __be16
*vlan_encapsulated_proto
= NULL
; /* packet type ID field (or len) for VLAN tag */
2660 __be16
*svlan_tci
= NULL
; /* Encapsulates priority and SVLAN ID */
2661 __be16
*svlan_encapsulated_proto
= NULL
; /* packet type ID field (or len) for SVLAN tag */
2663 if (pkt_dev
->nr_labels
)
2664 protocol
= htons(ETH_P_MPLS_UC
);
2666 if (pkt_dev
->vlan_id
!= 0xffff)
2667 protocol
= htons(ETH_P_8021Q
);
2669 /* Update any of the values, used when we're incrementing various
2672 mod_cur_headers(pkt_dev
);
2674 skb
= alloc_skb(pkt_dev
->cur_pkt_size
+ 64 + 16 +
2675 pkt_dev
->nr_labels
*sizeof(u32
) +
2676 VLAN_TAG_SIZE(pkt_dev
) + SVLAN_TAG_SIZE(pkt_dev
),
2679 sprintf(pkt_dev
->result
, "No memory");
2683 skb_reserve(skb
, 16);
2685 /* Reserve for ethernet and IP header */
2686 eth
= (__u8
*) skb_push(skb
, 14);
2687 mpls
= (__be32
*)skb_put(skb
, pkt_dev
->nr_labels
*sizeof(__u32
));
2688 if (pkt_dev
->nr_labels
)
2689 mpls_push(mpls
, pkt_dev
);
2691 if (pkt_dev
->vlan_id
!= 0xffff) {
2692 if (pkt_dev
->svlan_id
!= 0xffff) {
2693 svlan_tci
= (__be16
*)skb_put(skb
, sizeof(__be16
));
2694 *svlan_tci
= build_tci(pkt_dev
->svlan_id
,
2697 svlan_encapsulated_proto
= (__be16
*)skb_put(skb
, sizeof(__be16
));
2698 *svlan_encapsulated_proto
= htons(ETH_P_8021Q
);
2700 vlan_tci
= (__be16
*)skb_put(skb
, sizeof(__be16
));
2701 *vlan_tci
= build_tci(pkt_dev
->vlan_id
,
2704 vlan_encapsulated_proto
= (__be16
*)skb_put(skb
, sizeof(__be16
));
2705 *vlan_encapsulated_proto
= htons(ETH_P_IPV6
);
2708 skb
->network_header
= skb
->tail
;
2709 skb
->transport_header
= skb
->network_header
+ sizeof(struct ipv6hdr
);
2710 skb_put(skb
, sizeof(struct ipv6hdr
) + sizeof(struct udphdr
));
2712 iph
= ipv6_hdr(skb
);
2713 udph
= udp_hdr(skb
);
2715 memcpy(eth
, pkt_dev
->hh
, 12);
2716 *(__be16
*) & eth
[12] = protocol
;
2718 /* Eth + IPh + UDPh + mpls */
2719 datalen
= pkt_dev
->cur_pkt_size
- 14 -
2720 sizeof(struct ipv6hdr
) - sizeof(struct udphdr
) -
2721 pkt_dev
->nr_labels
*sizeof(u32
) - VLAN_TAG_SIZE(pkt_dev
) - SVLAN_TAG_SIZE(pkt_dev
);
2723 if (datalen
< sizeof(struct pktgen_hdr
)) {
2724 datalen
= sizeof(struct pktgen_hdr
);
2725 if (net_ratelimit())
2726 printk(KERN_INFO
"pktgen: increased datalen to %d\n",
2730 udph
->source
= htons(pkt_dev
->cur_udp_src
);
2731 udph
->dest
= htons(pkt_dev
->cur_udp_dst
);
2732 udph
->len
= htons(datalen
+ sizeof(struct udphdr
));
2733 udph
->check
= 0; /* No checksum */
2735 *(__be32
*) iph
= htonl(0x60000000); /* Version + flow */
2737 if (pkt_dev
->traffic_class
) {
2738 /* Version + traffic class + flow (0) */
2739 *(__be32
*)iph
|= htonl(0x60000000 | (pkt_dev
->traffic_class
<< 20));
2742 iph
->hop_limit
= 32;
2744 iph
->payload_len
= htons(sizeof(struct udphdr
) + datalen
);
2745 iph
->nexthdr
= IPPROTO_UDP
;
2747 ipv6_addr_copy(&iph
->daddr
, &pkt_dev
->cur_in6_daddr
);
2748 ipv6_addr_copy(&iph
->saddr
, &pkt_dev
->cur_in6_saddr
);
2750 skb
->mac_header
= (skb
->network_header
- ETH_HLEN
-
2751 pkt_dev
->nr_labels
* sizeof(u32
) -
2752 VLAN_TAG_SIZE(pkt_dev
) - SVLAN_TAG_SIZE(pkt_dev
));
2753 skb
->protocol
= protocol
;
2755 skb
->pkt_type
= PACKET_HOST
;
2757 if (pkt_dev
->nfrags
<= 0)
2758 pgh
= (struct pktgen_hdr
*)skb_put(skb
, datalen
);
2760 int frags
= pkt_dev
->nfrags
;
2763 pgh
= (struct pktgen_hdr
*)(((char *)(udph
)) + 8);
2765 if (frags
> MAX_SKB_FRAGS
)
2766 frags
= MAX_SKB_FRAGS
;
2767 if (datalen
> frags
* PAGE_SIZE
) {
2768 skb_put(skb
, datalen
- frags
* PAGE_SIZE
);
2769 datalen
= frags
* PAGE_SIZE
;
2773 while (datalen
> 0) {
2774 struct page
*page
= alloc_pages(GFP_KERNEL
, 0);
2775 skb_shinfo(skb
)->frags
[i
].page
= page
;
2776 skb_shinfo(skb
)->frags
[i
].page_offset
= 0;
2777 skb_shinfo(skb
)->frags
[i
].size
=
2778 (datalen
< PAGE_SIZE
? datalen
: PAGE_SIZE
);
2779 datalen
-= skb_shinfo(skb
)->frags
[i
].size
;
2780 skb
->len
+= skb_shinfo(skb
)->frags
[i
].size
;
2781 skb
->data_len
+= skb_shinfo(skb
)->frags
[i
].size
;
2783 skb_shinfo(skb
)->nr_frags
= i
;
2792 rem
= skb_shinfo(skb
)->frags
[i
- 1].size
/ 2;
2796 skb_shinfo(skb
)->frags
[i
- 1].size
-= rem
;
2798 skb_shinfo(skb
)->frags
[i
] =
2799 skb_shinfo(skb
)->frags
[i
- 1];
2800 get_page(skb_shinfo(skb
)->frags
[i
].page
);
2801 skb_shinfo(skb
)->frags
[i
].page
=
2802 skb_shinfo(skb
)->frags
[i
- 1].page
;
2803 skb_shinfo(skb
)->frags
[i
].page_offset
+=
2804 skb_shinfo(skb
)->frags
[i
- 1].size
;
2805 skb_shinfo(skb
)->frags
[i
].size
= rem
;
2807 skb_shinfo(skb
)->nr_frags
= i
;
2811 /* Stamp the time, and sequence number, convert them to network byte order */
2812 /* should we update cloned packets too ? */
2814 struct timeval timestamp
;
2816 pgh
->pgh_magic
= htonl(PKTGEN_MAGIC
);
2817 pgh
->seq_num
= htonl(pkt_dev
->seq_num
);
2819 do_gettimeofday(×tamp
);
2820 pgh
->tv_sec
= htonl(timestamp
.tv_sec
);
2821 pgh
->tv_usec
= htonl(timestamp
.tv_usec
);
2823 /* pkt_dev->seq_num++; FF: you really mean this? */
2828 static inline struct sk_buff
*fill_packet(struct net_device
*odev
,
2829 struct pktgen_dev
*pkt_dev
)
2831 if (pkt_dev
->flags
& F_IPV6
)
2832 return fill_packet_ipv6(odev
, pkt_dev
);
2834 return fill_packet_ipv4(odev
, pkt_dev
);
2837 static void pktgen_clear_counters(struct pktgen_dev
*pkt_dev
)
2839 pkt_dev
->seq_num
= 1;
2840 pkt_dev
->idle_acc
= 0;
2842 pkt_dev
->tx_bytes
= 0;
2843 pkt_dev
->errors
= 0;
2846 /* Set up structure for sending pkts, clear counters */
2848 static void pktgen_run(struct pktgen_thread
*t
)
2850 struct pktgen_dev
*pkt_dev
;
2853 pr_debug("pktgen: entering pktgen_run. %p\n", t
);
2856 list_for_each_entry(pkt_dev
, &t
->if_list
, list
) {
2859 * setup odev and create initial packet.
2861 pktgen_setup_inject(pkt_dev
);
2863 if (pkt_dev
->odev
) {
2864 pktgen_clear_counters(pkt_dev
);
2865 pkt_dev
->running
= 1; /* Cranke yeself! */
2866 pkt_dev
->skb
= NULL
;
2867 pkt_dev
->started_at
= getCurUs();
2868 pkt_dev
->next_tx_us
= getCurUs(); /* Transmit immediately */
2869 pkt_dev
->next_tx_ns
= 0;
2871 strcpy(pkt_dev
->result
, "Starting");
2874 strcpy(pkt_dev
->result
, "Error starting");
2878 t
->control
&= ~(T_STOP
);
2881 static void pktgen_stop_all_threads_ifs(void)
2883 struct pktgen_thread
*t
;
2885 pr_debug("pktgen: entering pktgen_stop_all_threads_ifs.\n");
2887 mutex_lock(&pktgen_thread_lock
);
2889 list_for_each_entry(t
, &pktgen_threads
, th_list
)
2890 t
->control
|= T_STOP
;
2892 mutex_unlock(&pktgen_thread_lock
);
2895 static int thread_is_running(struct pktgen_thread
*t
)
2897 struct pktgen_dev
*pkt_dev
;
2900 list_for_each_entry(pkt_dev
, &t
->if_list
, list
)
2901 if (pkt_dev
->running
) {
2908 static int pktgen_wait_thread_run(struct pktgen_thread
*t
)
2912 while (thread_is_running(t
)) {
2916 msleep_interruptible(100);
2918 if (signal_pending(current
))
2928 static int pktgen_wait_all_threads_run(void)
2930 struct pktgen_thread
*t
;
2933 mutex_lock(&pktgen_thread_lock
);
2935 list_for_each_entry(t
, &pktgen_threads
, th_list
) {
2936 sig
= pktgen_wait_thread_run(t
);
2942 list_for_each_entry(t
, &pktgen_threads
, th_list
)
2943 t
->control
|= (T_STOP
);
2945 mutex_unlock(&pktgen_thread_lock
);
2949 static void pktgen_run_all_threads(void)
2951 struct pktgen_thread
*t
;
2953 pr_debug("pktgen: entering pktgen_run_all_threads.\n");
2955 mutex_lock(&pktgen_thread_lock
);
2957 list_for_each_entry(t
, &pktgen_threads
, th_list
)
2958 t
->control
|= (T_RUN
);
2960 mutex_unlock(&pktgen_thread_lock
);
2962 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
2964 pktgen_wait_all_threads_run();
2967 static void show_results(struct pktgen_dev
*pkt_dev
, int nr_frags
)
2969 __u64 total_us
, bps
, mbps
, pps
, idle
;
2970 char *p
= pkt_dev
->result
;
2972 total_us
= pkt_dev
->stopped_at
- pkt_dev
->started_at
;
2974 idle
= pkt_dev
->idle_acc
;
2976 p
+= sprintf(p
, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
2977 (unsigned long long)total_us
,
2978 (unsigned long long)(total_us
- idle
),
2979 (unsigned long long)idle
,
2980 (unsigned long long)pkt_dev
->sofar
,
2981 pkt_dev
->cur_pkt_size
, nr_frags
);
2983 pps
= pkt_dev
->sofar
* USEC_PER_SEC
;
2985 while ((total_us
>> 32) != 0) {
2990 do_div(pps
, total_us
);
2992 bps
= pps
* 8 * pkt_dev
->cur_pkt_size
;
2995 do_div(mbps
, 1000000);
2996 p
+= sprintf(p
, " %llupps %lluMb/sec (%llubps) errors: %llu",
2997 (unsigned long long)pps
,
2998 (unsigned long long)mbps
,
2999 (unsigned long long)bps
,
3000 (unsigned long long)pkt_dev
->errors
);
3003 /* Set stopped-at timer, remove from running list, do counters & statistics */
3005 static int pktgen_stop_device(struct pktgen_dev
*pkt_dev
)
3007 int nr_frags
= pkt_dev
->skb
? skb_shinfo(pkt_dev
->skb
)->nr_frags
: -1;
3009 if (!pkt_dev
->running
) {
3010 printk("pktgen: interface: %s is already stopped\n",
3011 pkt_dev
->odev
->name
);
3015 pkt_dev
->stopped_at
= getCurUs();
3016 pkt_dev
->running
= 0;
3018 show_results(pkt_dev
, nr_frags
);
3023 static struct pktgen_dev
*next_to_run(struct pktgen_thread
*t
)
3025 struct pktgen_dev
*pkt_dev
, *best
= NULL
;
3029 list_for_each_entry(pkt_dev
, &t
->if_list
, list
) {
3030 if (!pkt_dev
->running
)
3034 else if (pkt_dev
->next_tx_us
< best
->next_tx_us
)
3041 static void pktgen_stop(struct pktgen_thread
*t
)
3043 struct pktgen_dev
*pkt_dev
;
3045 pr_debug("pktgen: entering pktgen_stop\n");
3049 list_for_each_entry(pkt_dev
, &t
->if_list
, list
) {
3050 pktgen_stop_device(pkt_dev
);
3052 kfree_skb(pkt_dev
->skb
);
3054 pkt_dev
->skb
= NULL
;
3061 * one of our devices needs to be removed - find it
3064 static void pktgen_rem_one_if(struct pktgen_thread
*t
)
3066 struct list_head
*q
, *n
;
3067 struct pktgen_dev
*cur
;
3069 pr_debug("pktgen: entering pktgen_rem_one_if\n");
3073 list_for_each_safe(q
, n
, &t
->if_list
) {
3074 cur
= list_entry(q
, struct pktgen_dev
, list
);
3076 if (!cur
->removal_mark
)
3080 kfree_skb(cur
->skb
);
3083 pktgen_remove_device(t
, cur
);
3091 static void pktgen_rem_all_ifs(struct pktgen_thread
*t
)
3093 struct list_head
*q
, *n
;
3094 struct pktgen_dev
*cur
;
3096 /* Remove all devices, free mem */
3098 pr_debug("pktgen: entering pktgen_rem_all_ifs\n");
3101 list_for_each_safe(q
, n
, &t
->if_list
) {
3102 cur
= list_entry(q
, struct pktgen_dev
, list
);
3105 kfree_skb(cur
->skb
);
3108 pktgen_remove_device(t
, cur
);
3114 static void pktgen_rem_thread(struct pktgen_thread
*t
)
3116 /* Remove from the thread list */
3118 remove_proc_entry(t
->tsk
->comm
, pg_proc_dir
);
3120 mutex_lock(&pktgen_thread_lock
);
3122 list_del(&t
->th_list
);
3124 mutex_unlock(&pktgen_thread_lock
);
3127 static __inline__
void pktgen_xmit(struct pktgen_dev
*pkt_dev
)
3129 struct net_device
*odev
= NULL
;
3130 __u64 idle_start
= 0;
3133 odev
= pkt_dev
->odev
;
3135 if (pkt_dev
->delay_us
|| pkt_dev
->delay_ns
) {
3139 if (now
< pkt_dev
->next_tx_us
)
3140 spin(pkt_dev
, pkt_dev
->next_tx_us
);
3142 /* This is max DELAY, this has special meaning of
3145 if (pkt_dev
->delay_us
== 0x7FFFFFFF) {
3146 pkt_dev
->next_tx_us
= getCurUs() + pkt_dev
->delay_us
;
3147 pkt_dev
->next_tx_ns
= pkt_dev
->delay_ns
;
3152 if (netif_queue_stopped(odev
) || need_resched()) {
3153 idle_start
= getCurUs();
3155 if (!netif_running(odev
)) {
3156 pktgen_stop_device(pkt_dev
);
3158 kfree_skb(pkt_dev
->skb
);
3159 pkt_dev
->skb
= NULL
;
3165 pkt_dev
->idle_acc
+= getCurUs() - idle_start
;
3167 if (netif_queue_stopped(odev
)) {
3168 pkt_dev
->next_tx_us
= getCurUs(); /* TODO */
3169 pkt_dev
->next_tx_ns
= 0;
3170 goto out
; /* Try the next interface */
3174 if (pkt_dev
->last_ok
|| !pkt_dev
->skb
) {
3175 if ((++pkt_dev
->clone_count
>= pkt_dev
->clone_skb
)
3176 || (!pkt_dev
->skb
)) {
3177 /* build a new pkt */
3179 kfree_skb(pkt_dev
->skb
);
3181 pkt_dev
->skb
= fill_packet(odev
, pkt_dev
);
3182 if (pkt_dev
->skb
== NULL
) {
3183 printk("pktgen: ERROR: couldn't allocate skb in fill_packet.\n");
3185 pkt_dev
->clone_count
--; /* back out increment, OOM */
3188 pkt_dev
->allocated_skbs
++;
3189 pkt_dev
->clone_count
= 0; /* reset counter */
3193 netif_tx_lock_bh(odev
);
3194 if (!netif_queue_stopped(odev
)) {
3196 atomic_inc(&(pkt_dev
->skb
->users
));
3198 ret
= odev
->hard_start_xmit(pkt_dev
->skb
, odev
);
3199 if (likely(ret
== NETDEV_TX_OK
)) {
3200 pkt_dev
->last_ok
= 1;
3203 pkt_dev
->tx_bytes
+= pkt_dev
->cur_pkt_size
;
3205 } else if (ret
== NETDEV_TX_LOCKED
3206 && (odev
->features
& NETIF_F_LLTX
)) {
3209 } else { /* Retry it next time */
3211 atomic_dec(&(pkt_dev
->skb
->users
));
3213 if (debug
&& net_ratelimit())
3214 printk(KERN_INFO
"pktgen: Hard xmit error\n");
3217 pkt_dev
->last_ok
= 0;
3220 pkt_dev
->next_tx_us
= getCurUs();
3221 pkt_dev
->next_tx_ns
= 0;
3223 pkt_dev
->next_tx_us
+= pkt_dev
->delay_us
;
3224 pkt_dev
->next_tx_ns
+= pkt_dev
->delay_ns
;
3226 if (pkt_dev
->next_tx_ns
> 1000) {
3227 pkt_dev
->next_tx_us
++;
3228 pkt_dev
->next_tx_ns
-= 1000;
3232 else { /* Retry it next time */
3233 pkt_dev
->last_ok
= 0;
3234 pkt_dev
->next_tx_us
= getCurUs(); /* TODO */
3235 pkt_dev
->next_tx_ns
= 0;
3238 netif_tx_unlock_bh(odev
);
3240 /* If pkt_dev->count is zero, then run forever */
3241 if ((pkt_dev
->count
!= 0) && (pkt_dev
->sofar
>= pkt_dev
->count
)) {
3242 if (atomic_read(&(pkt_dev
->skb
->users
)) != 1) {
3243 idle_start
= getCurUs();
3244 while (atomic_read(&(pkt_dev
->skb
->users
)) != 1) {
3245 if (signal_pending(current
)) {
3250 pkt_dev
->idle_acc
+= getCurUs() - idle_start
;
3253 /* Done with this */
3254 pktgen_stop_device(pkt_dev
);
3256 kfree_skb(pkt_dev
->skb
);
3257 pkt_dev
->skb
= NULL
;
3263 * Main loop of the thread goes here
3266 static int pktgen_thread_worker(void *arg
)
3269 struct pktgen_thread
*t
= arg
;
3270 struct pktgen_dev
*pkt_dev
= NULL
;
3272 u32 max_before_softirq
;
3273 u32 tx_since_softirq
= 0;
3275 BUG_ON(smp_processor_id() != cpu
);
3277 init_waitqueue_head(&t
->queue
);
3279 t
->pid
= current
->pid
;
3281 pr_debug("pktgen: starting pktgen/%d: pid=%d\n", cpu
, current
->pid
);
3283 max_before_softirq
= t
->max_before_softirq
;
3285 set_current_state(TASK_INTERRUPTIBLE
);
3287 while (!kthread_should_stop()) {
3288 pkt_dev
= next_to_run(t
);
3291 (t
->control
& (T_STOP
| T_RUN
| T_REMDEVALL
| T_REMDEV
))
3293 prepare_to_wait(&(t
->queue
), &wait
,
3294 TASK_INTERRUPTIBLE
);
3295 schedule_timeout(HZ
/ 10);
3296 finish_wait(&(t
->queue
), &wait
);
3299 __set_current_state(TASK_RUNNING
);
3303 pktgen_xmit(pkt_dev
);
3306 * We like to stay RUNNING but must also give
3307 * others fair share.
3310 tx_since_softirq
+= pkt_dev
->last_ok
;
3312 if (tx_since_softirq
> max_before_softirq
) {
3313 if (local_softirq_pending())
3315 tx_since_softirq
= 0;
3319 if (t
->control
& T_STOP
) {
3321 t
->control
&= ~(T_STOP
);
3324 if (t
->control
& T_RUN
) {
3326 t
->control
&= ~(T_RUN
);
3329 if (t
->control
& T_REMDEVALL
) {
3330 pktgen_rem_all_ifs(t
);
3331 t
->control
&= ~(T_REMDEVALL
);
3334 if (t
->control
& T_REMDEV
) {
3335 pktgen_rem_one_if(t
);
3336 t
->control
&= ~(T_REMDEV
);
3341 set_current_state(TASK_INTERRUPTIBLE
);
3344 pr_debug("pktgen: %s stopping all device\n", t
->tsk
->comm
);
3347 pr_debug("pktgen: %s removing all device\n", t
->tsk
->comm
);
3348 pktgen_rem_all_ifs(t
);
3350 pr_debug("pktgen: %s removing thread.\n", t
->tsk
->comm
);
3351 pktgen_rem_thread(t
);
3356 static struct pktgen_dev
*pktgen_find_dev(struct pktgen_thread
*t
,
3359 struct pktgen_dev
*p
, *pkt_dev
= NULL
;
3362 list_for_each_entry(p
, &t
->if_list
, list
)
3363 if (strncmp(p
->odev
->name
, ifname
, IFNAMSIZ
) == 0) {
3369 pr_debug("pktgen: find_dev(%s) returning %p\n", ifname
, pkt_dev
);
3374 * Adds a dev at front of if_list.
3377 static int add_dev_to_thread(struct pktgen_thread
*t
,
3378 struct pktgen_dev
*pkt_dev
)
3384 if (pkt_dev
->pg_thread
) {
3385 printk("pktgen: ERROR: already assigned to a thread.\n");
3390 list_add(&pkt_dev
->list
, &t
->if_list
);
3391 pkt_dev
->pg_thread
= t
;
3392 pkt_dev
->running
= 0;
3399 /* Called under thread lock */
3401 static int pktgen_add_device(struct pktgen_thread
*t
, const char *ifname
)
3403 struct pktgen_dev
*pkt_dev
;
3406 /* We don't allow a device to be on several threads */
3408 pkt_dev
= __pktgen_NN_threads(ifname
, FIND
);
3410 printk("pktgen: ERROR: interface already used.\n");
3414 pkt_dev
= kzalloc(sizeof(struct pktgen_dev
), GFP_KERNEL
);
3418 pkt_dev
->flows
= vmalloc(MAX_CFLOWS
* sizeof(struct flow_state
));
3419 if (pkt_dev
->flows
== NULL
) {
3423 memset(pkt_dev
->flows
, 0, MAX_CFLOWS
* sizeof(struct flow_state
));
3425 pkt_dev
->removal_mark
= 0;
3426 pkt_dev
->min_pkt_size
= ETH_ZLEN
;
3427 pkt_dev
->max_pkt_size
= ETH_ZLEN
;
3428 pkt_dev
->nfrags
= 0;
3429 pkt_dev
->clone_skb
= pg_clone_skb_d
;
3430 pkt_dev
->delay_us
= pg_delay_d
/ 1000;
3431 pkt_dev
->delay_ns
= pg_delay_d
% 1000;
3432 pkt_dev
->count
= pg_count_d
;
3434 pkt_dev
->udp_src_min
= 9; /* sink port */
3435 pkt_dev
->udp_src_max
= 9;
3436 pkt_dev
->udp_dst_min
= 9;
3437 pkt_dev
->udp_dst_max
= 9;
3439 pkt_dev
->vlan_p
= 0;
3440 pkt_dev
->vlan_cfi
= 0;
3441 pkt_dev
->vlan_id
= 0xffff;
3442 pkt_dev
->svlan_p
= 0;
3443 pkt_dev
->svlan_cfi
= 0;
3444 pkt_dev
->svlan_id
= 0xffff;
3446 err
= pktgen_setup_dev(pkt_dev
, ifname
);
3450 pkt_dev
->entry
= create_proc_entry(ifname
, 0600, pg_proc_dir
);
3451 if (!pkt_dev
->entry
) {
3452 printk("pktgen: cannot create %s/%s procfs entry.\n",
3453 PG_PROC_DIR
, ifname
);
3457 pkt_dev
->entry
->proc_fops
= &pktgen_if_fops
;
3458 pkt_dev
->entry
->data
= pkt_dev
;
3460 return add_dev_to_thread(t
, pkt_dev
);
3462 dev_put(pkt_dev
->odev
);
3465 vfree(pkt_dev
->flows
);
3470 static int __init
pktgen_create_thread(int cpu
)
3472 struct pktgen_thread
*t
;
3473 struct proc_dir_entry
*pe
;
3474 struct task_struct
*p
;
3476 t
= kzalloc(sizeof(struct pktgen_thread
), GFP_KERNEL
);
3478 printk("pktgen: ERROR: out of memory, can't create new thread.\n");
3482 spin_lock_init(&t
->if_lock
);
3485 INIT_LIST_HEAD(&t
->if_list
);
3487 list_add_tail(&t
->th_list
, &pktgen_threads
);
3489 p
= kthread_create(pktgen_thread_worker
, t
, "kpktgend_%d", cpu
);
3491 printk("pktgen: kernel_thread() failed for cpu %d\n", t
->cpu
);
3492 list_del(&t
->th_list
);
3496 kthread_bind(p
, cpu
);
3499 pe
= create_proc_entry(t
->tsk
->comm
, 0600, pg_proc_dir
);
3501 printk("pktgen: cannot create %s/%s procfs entry.\n",
3502 PG_PROC_DIR
, t
->tsk
->comm
);
3504 list_del(&t
->th_list
);
3509 pe
->proc_fops
= &pktgen_thread_fops
;
3518 * Removes a device from the thread if_list.
3520 static void _rem_dev_from_if_list(struct pktgen_thread
*t
,
3521 struct pktgen_dev
*pkt_dev
)
3523 struct list_head
*q
, *n
;
3524 struct pktgen_dev
*p
;
3526 list_for_each_safe(q
, n
, &t
->if_list
) {
3527 p
= list_entry(q
, struct pktgen_dev
, list
);
3533 static int pktgen_remove_device(struct pktgen_thread
*t
,
3534 struct pktgen_dev
*pkt_dev
)
3537 pr_debug("pktgen: remove_device pkt_dev=%p\n", pkt_dev
);
3539 if (pkt_dev
->running
) {
3540 printk("pktgen:WARNING: trying to remove a running interface, stopping it now.\n");
3541 pktgen_stop_device(pkt_dev
);
3544 /* Dis-associate from the interface */
3546 if (pkt_dev
->odev
) {
3547 dev_put(pkt_dev
->odev
);
3548 pkt_dev
->odev
= NULL
;
3551 /* And update the thread if_list */
3553 _rem_dev_from_if_list(t
, pkt_dev
);
3556 remove_proc_entry(pkt_dev
->entry
->name
, pg_proc_dir
);
3559 vfree(pkt_dev
->flows
);
3564 static int __init
pg_init(void)
3567 struct proc_dir_entry
*pe
;
3571 pg_proc_dir
= proc_mkdir(PG_PROC_DIR
, proc_net
);
3574 pg_proc_dir
->owner
= THIS_MODULE
;
3576 pe
= create_proc_entry(PGCTRL
, 0600, pg_proc_dir
);
3578 printk("pktgen: ERROR: cannot create %s procfs entry.\n",
3580 proc_net_remove(PG_PROC_DIR
);
3584 pe
->proc_fops
= &pktgen_fops
;
3587 /* Register us to receive netdevice events */
3588 register_netdevice_notifier(&pktgen_notifier_block
);
3590 for_each_online_cpu(cpu
) {
3593 err
= pktgen_create_thread(cpu
);
3595 printk("pktgen: WARNING: Cannot create thread for cpu %d (%d)\n",
3599 if (list_empty(&pktgen_threads
)) {
3600 printk("pktgen: ERROR: Initialization failed for all threads\n");
3601 unregister_netdevice_notifier(&pktgen_notifier_block
);
3602 remove_proc_entry(PGCTRL
, pg_proc_dir
);
3603 proc_net_remove(PG_PROC_DIR
);
3610 static void __exit
pg_cleanup(void)
3612 struct pktgen_thread
*t
;
3613 struct list_head
*q
, *n
;
3614 wait_queue_head_t queue
;
3615 init_waitqueue_head(&queue
);
3617 /* Stop all interfaces & threads */
3619 list_for_each_safe(q
, n
, &pktgen_threads
) {
3620 t
= list_entry(q
, struct pktgen_thread
, th_list
);
3621 kthread_stop(t
->tsk
);
3625 /* Un-register us from receiving netdevice events */
3626 unregister_netdevice_notifier(&pktgen_notifier_block
);
3628 /* Clean up proc file system */
3629 remove_proc_entry(PGCTRL
, pg_proc_dir
);
3630 proc_net_remove(PG_PROC_DIR
);
3633 module_init(pg_init
);
3634 module_exit(pg_cleanup
);
3636 MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se");
3637 MODULE_DESCRIPTION("Packet Generator tool");
3638 MODULE_LICENSE("GPL");
3639 module_param(pg_count_d
, int, 0);
3640 module_param(pg_delay_d
, int, 0);
3641 module_param(pg_clone_skb_d
, int, 0);
3642 module_param(debug
, int, 0);