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>
115 #include <linux/sys.h>
116 #include <linux/types.h>
117 #include <linux/module.h>
118 #include <linux/moduleparam.h>
119 #include <linux/kernel.h>
120 #include <linux/mutex.h>
121 #include <linux/sched.h>
122 #include <linux/slab.h>
123 #include <linux/vmalloc.h>
124 #include <linux/unistd.h>
125 #include <linux/string.h>
126 #include <linux/ptrace.h>
127 #include <linux/errno.h>
128 #include <linux/ioport.h>
129 #include <linux/interrupt.h>
130 #include <linux/capability.h>
131 #include <linux/freezer.h>
132 #include <linux/delay.h>
133 #include <linux/timer.h>
134 #include <linux/list.h>
135 #include <linux/init.h>
136 #include <linux/skbuff.h>
137 #include <linux/netdevice.h>
138 #include <linux/inet.h>
139 #include <linux/inetdevice.h>
140 #include <linux/rtnetlink.h>
141 #include <linux/if_arp.h>
142 #include <linux/if_vlan.h>
143 #include <linux/in.h>
144 #include <linux/ip.h>
145 #include <linux/ipv6.h>
146 #include <linux/udp.h>
147 #include <linux/proc_fs.h>
148 #include <linux/seq_file.h>
149 #include <linux/wait.h>
150 #include <linux/etherdevice.h>
151 #include <linux/kthread.h>
152 #include <net/checksum.h>
153 #include <net/ipv6.h>
154 #include <net/addrconf.h>
155 #include <asm/byteorder.h>
156 #include <linux/rcupdate.h>
157 #include <asm/bitops.h>
160 #include <asm/uaccess.h>
161 #include <asm/div64.h> /* do_div */
162 #include <asm/timex.h>
164 #define VERSION "pktgen v2.68: Packet Generator for packet performance testing.\n"
166 /* The buckets are exponential in 'width' */
167 #define LAT_BUCKETS_MAX 32
168 #define IP_NAME_SZ 32
169 #define MAX_MPLS_LABELS 16 /* This is the max label stack depth */
170 #define MPLS_STACK_BOTTOM htonl(0x00000100)
172 /* Device flag bits */
173 #define F_IPSRC_RND (1<<0) /* IP-Src Random */
174 #define F_IPDST_RND (1<<1) /* IP-Dst Random */
175 #define F_UDPSRC_RND (1<<2) /* UDP-Src Random */
176 #define F_UDPDST_RND (1<<3) /* UDP-Dst Random */
177 #define F_MACSRC_RND (1<<4) /* MAC-Src Random */
178 #define F_MACDST_RND (1<<5) /* MAC-Dst Random */
179 #define F_TXSIZE_RND (1<<6) /* Transmit size is random */
180 #define F_IPV6 (1<<7) /* Interface in IPV6 Mode */
181 #define F_MPLS_RND (1<<8) /* Random MPLS labels */
182 #define F_VID_RND (1<<9) /* Random VLAN ID */
183 #define F_SVID_RND (1<<10) /* Random SVLAN ID */
185 /* Thread control flag bits */
186 #define T_TERMINATE (1<<0)
187 #define T_STOP (1<<1) /* Stop run */
188 #define T_RUN (1<<2) /* Start run */
189 #define T_REMDEVALL (1<<3) /* Remove all devs */
190 #define T_REMDEV (1<<4) /* Remove one dev */
192 /* If lock -- can be removed after some work */
193 #define if_lock(t) spin_lock(&(t->if_lock));
194 #define if_unlock(t) spin_unlock(&(t->if_lock));
196 /* Used to help with determining the pkts on receive */
197 #define PKTGEN_MAGIC 0xbe9be955
198 #define PG_PROC_DIR "pktgen"
199 #define PGCTRL "pgctrl"
200 static struct proc_dir_entry
*pg_proc_dir
= NULL
;
202 #define MAX_CFLOWS 65536
204 #define VLAN_TAG_SIZE(x) ((x)->vlan_id == 0xffff ? 0 : 4)
205 #define SVLAN_TAG_SIZE(x) ((x)->svlan_id == 0xffff ? 0 : 4)
214 * Try to keep frequent/infrequent used vars. separated.
216 struct proc_dir_entry
*entry
; /* proc file */
217 struct pktgen_thread
*pg_thread
;/* the owner */
218 struct list_head list
; /* Used for chaining in the thread's run-queue */
220 int running
; /* if this changes to false, the test will stop */
222 /* If min != max, then we will either do a linear iteration, or
223 * we will do a random selection from within the range.
226 int removal_mark
; /* non-zero => the device is marked for
227 * removal by worker thread */
229 int min_pkt_size
; /* = ETH_ZLEN; */
230 int max_pkt_size
; /* = ETH_ZLEN; */
232 __u32 delay_us
; /* Default delay */
234 __u64 count
; /* Default No packets to send */
235 __u64 sofar
; /* How many pkts we've sent so far */
236 __u64 tx_bytes
; /* How many bytes we've transmitted */
237 __u64 errors
; /* Errors when trying to transmit, pkts will be re-sent */
239 /* runtime counters relating to clone_skb */
240 __u64 next_tx_us
; /* timestamp of when to tx next */
243 __u64 allocated_skbs
;
245 int last_ok
; /* Was last skb sent?
246 * Or a failed transmit of some sort? This will keep
247 * sequence numbers in order, for example.
249 __u64 started_at
; /* micro-seconds */
250 __u64 stopped_at
; /* micro-seconds */
251 __u64 idle_acc
; /* micro-seconds */
254 int clone_skb
; /* Use multiple SKBs during packet gen. If this number
255 * is greater than 1, then that many copies of the same
256 * packet will be sent before a new packet is allocated.
257 * For instance, if you want to send 1024 identical packets
258 * before creating a new packet, set clone_skb to 1024.
261 char dst_min
[IP_NAME_SZ
]; /* IP, ie 1.2.3.4 */
262 char dst_max
[IP_NAME_SZ
]; /* IP, ie 1.2.3.4 */
263 char src_min
[IP_NAME_SZ
]; /* IP, ie 1.2.3.4 */
264 char src_max
[IP_NAME_SZ
]; /* IP, ie 1.2.3.4 */
266 struct in6_addr in6_saddr
;
267 struct in6_addr in6_daddr
;
268 struct in6_addr cur_in6_daddr
;
269 struct in6_addr cur_in6_saddr
;
271 struct in6_addr min_in6_daddr
;
272 struct in6_addr max_in6_daddr
;
273 struct in6_addr min_in6_saddr
;
274 struct in6_addr max_in6_saddr
;
276 /* If we're doing ranges, random or incremental, then this
277 * defines the min/max for those ranges.
279 __be32 saddr_min
; /* inclusive, source IP address */
280 __be32 saddr_max
; /* exclusive, source IP address */
281 __be32 daddr_min
; /* inclusive, dest IP address */
282 __be32 daddr_max
; /* exclusive, dest IP address */
284 __u16 udp_src_min
; /* inclusive, source UDP port */
285 __u16 udp_src_max
; /* exclusive, source UDP port */
286 __u16 udp_dst_min
; /* inclusive, dest UDP port */
287 __u16 udp_dst_max
; /* exclusive, dest UDP port */
290 __u8 tos
; /* six most significant bits of (former) IPv4 TOS are for dscp codepoint */
291 __u8 traffic_class
; /* ditto for the (former) Traffic Class in IPv6 (see RFC 3260, sec. 4) */
294 unsigned nr_labels
; /* Depth of stack, 0 = no MPLS */
295 __be32 labels
[MAX_MPLS_LABELS
];
297 /* VLAN/SVLAN (802.1Q/Q-in-Q) */
300 __u16 vlan_id
; /* 0xffff means no vlan tag */
304 __u16 svlan_id
; /* 0xffff means no svlan tag */
306 __u32 src_mac_count
; /* How many MACs to iterate through */
307 __u32 dst_mac_count
; /* How many MACs to iterate through */
309 unsigned char dst_mac
[ETH_ALEN
];
310 unsigned char src_mac
[ETH_ALEN
];
312 __u32 cur_dst_mac_offset
;
313 __u32 cur_src_mac_offset
;
322 0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
324 We fill in SRC address later
325 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
329 __u16 pad
; /* pad out the hh struct to an even 16 bytes */
331 struct sk_buff
*skb
; /* skb we are to transmit next, mainly used for when we
332 * are transmitting the same one multiple times
334 struct net_device
*odev
; /* The out-going device. Note that the device should
335 * have it's pg_info pointer pointing back to this
336 * device. This will be set when the user specifies
337 * the out-going device name (not when the inject is
338 * started as it used to do.)
340 struct flow_state
*flows
;
341 unsigned cflows
; /* Concurrent flows (config) */
342 unsigned lflow
; /* Flow length (config) */
343 unsigned nflows
; /* accumulated flows (stats) */
355 struct pktgen_thread
{
357 struct list_head if_list
; /* All device here */
358 struct list_head th_list
;
359 struct task_struct
*tsk
;
361 u32 max_before_softirq
; /* We'll call do_softirq to prevent starvation. */
363 /* Field for thread to receive "posted" events terminate, stop ifs etc. */
369 wait_queue_head_t queue
;
375 /* This code works around the fact that do_div cannot handle two 64-bit
376 numbers, and regular 64-bit division doesn't work on x86 kernels.
382 /* This was emailed to LMKL by: Chris Caputo <ccaputo@alt.net>
383 * Function copied/adapted/optimized from:
385 * nemesis.sourceforge.net/browse/lib/static/intmath/ix86/intmath.c.html
387 * Copyright 1994, University of Cambridge Computer Laboratory
388 * All Rights Reserved.
391 static inline s64
divremdi3(s64 x
, s64 y
, int type
)
393 u64 a
= (x
< 0) ? -x
: x
;
394 u64 b
= (y
< 0) ? -y
: y
;
414 if (PG_DIV
== type
) {
415 return (((x
^ y
) & (1ll << 63)) == 0) ? res
: -(s64
) res
;
417 return ((x
& (1ll << 63)) == 0) ? a
: -(s64
) a
;
421 /* End of hacks to deal with 64-bit math on x86 */
423 /** Convert to milliseconds */
424 static inline __u64
tv_to_ms(const struct timeval
*tv
)
426 __u64 ms
= tv
->tv_usec
/ 1000;
427 ms
+= (__u64
) tv
->tv_sec
* (__u64
) 1000;
431 /** Convert to micro-seconds */
432 static inline __u64
tv_to_us(const struct timeval
*tv
)
434 __u64 us
= tv
->tv_usec
;
435 us
+= (__u64
) tv
->tv_sec
* (__u64
) 1000000;
439 static inline __u64
pg_div(__u64 n
, __u32 base
)
443 /* printk("pktgen: pg_div, n: %llu base: %d rv: %llu\n",
448 static inline __u64
pg_div64(__u64 n
, __u64 base
)
452 * How do we know if the architecture we are running on
453 * supports division with 64 bit base?
456 #if defined(__sparc_v9__) || defined(__powerpc64__) || defined(__alpha__) || defined(__x86_64__) || defined(__ia64__)
460 tmp
= divremdi3(n
, base
, PG_DIV
);
465 static inline __u64
getCurMs(void)
468 do_gettimeofday(&tv
);
469 return tv_to_ms(&tv
);
472 static inline __u64
getCurUs(void)
475 do_gettimeofday(&tv
);
476 return tv_to_us(&tv
);
479 static inline __u64
tv_diff(const struct timeval
*a
, const struct timeval
*b
)
481 return tv_to_us(a
) - tv_to_us(b
);
484 /* old include end */
486 static char version
[] __initdata
= VERSION
;
488 static int pktgen_remove_device(struct pktgen_thread
*t
, struct pktgen_dev
*i
);
489 static int pktgen_add_device(struct pktgen_thread
*t
, const char *ifname
);
490 static struct pktgen_dev
*pktgen_find_dev(struct pktgen_thread
*t
,
492 static int pktgen_device_event(struct notifier_block
*, unsigned long, void *);
493 static void pktgen_run_all_threads(void);
494 static void pktgen_stop_all_threads_ifs(void);
495 static int pktgen_stop_device(struct pktgen_dev
*pkt_dev
);
496 static void pktgen_stop(struct pktgen_thread
*t
);
497 static void pktgen_clear_counters(struct pktgen_dev
*pkt_dev
);
499 static unsigned int scan_ip6(const char *s
, char ip
[16]);
500 static unsigned int fmt_ip6(char *s
, const char ip
[16]);
502 /* Module parameters, defaults. */
503 static int pg_count_d
= 1000; /* 1000 pkts by default */
504 static int pg_delay_d
;
505 static int pg_clone_skb_d
;
508 static DEFINE_MUTEX(pktgen_thread_lock
);
509 static LIST_HEAD(pktgen_threads
);
511 static struct notifier_block pktgen_notifier_block
= {
512 .notifier_call
= pktgen_device_event
,
516 * /proc handling functions
520 static int pgctrl_show(struct seq_file
*seq
, void *v
)
522 seq_puts(seq
, VERSION
);
526 static ssize_t
pgctrl_write(struct file
*file
, const char __user
* buf
,
527 size_t count
, loff_t
* ppos
)
532 if (!capable(CAP_NET_ADMIN
)) {
537 if (count
> sizeof(data
))
538 count
= sizeof(data
);
540 if (copy_from_user(data
, buf
, count
)) {
544 data
[count
- 1] = 0; /* Make string */
546 if (!strcmp(data
, "stop"))
547 pktgen_stop_all_threads_ifs();
549 else if (!strcmp(data
, "start"))
550 pktgen_run_all_threads();
553 printk("pktgen: Unknown command: %s\n", data
);
561 static int pgctrl_open(struct inode
*inode
, struct file
*file
)
563 return single_open(file
, pgctrl_show
, PDE(inode
)->data
);
566 static const struct file_operations pktgen_fops
= {
567 .owner
= THIS_MODULE
,
571 .write
= pgctrl_write
,
572 .release
= single_release
,
575 static int pktgen_if_show(struct seq_file
*seq
, void *v
)
578 struct pktgen_dev
*pkt_dev
= seq
->private;
581 __u64 now
= getCurUs();
584 "Params: count %llu min_pkt_size: %u max_pkt_size: %u\n",
585 (unsigned long long)pkt_dev
->count
, pkt_dev
->min_pkt_size
,
586 pkt_dev
->max_pkt_size
);
589 " frags: %d delay: %u clone_skb: %d ifname: %s\n",
591 1000 * pkt_dev
->delay_us
+ pkt_dev
->delay_ns
,
592 pkt_dev
->clone_skb
, pkt_dev
->odev
->name
);
594 seq_printf(seq
, " flows: %u flowlen: %u\n", pkt_dev
->cflows
,
597 if (pkt_dev
->flags
& F_IPV6
) {
598 char b1
[128], b2
[128], b3
[128];
599 fmt_ip6(b1
, pkt_dev
->in6_saddr
.s6_addr
);
600 fmt_ip6(b2
, pkt_dev
->min_in6_saddr
.s6_addr
);
601 fmt_ip6(b3
, pkt_dev
->max_in6_saddr
.s6_addr
);
603 " saddr: %s min_saddr: %s max_saddr: %s\n", b1
,
606 fmt_ip6(b1
, pkt_dev
->in6_daddr
.s6_addr
);
607 fmt_ip6(b2
, pkt_dev
->min_in6_daddr
.s6_addr
);
608 fmt_ip6(b3
, pkt_dev
->max_in6_daddr
.s6_addr
);
610 " daddr: %s min_daddr: %s max_daddr: %s\n", b1
,
615 " dst_min: %s dst_max: %s\n src_min: %s src_max: %s\n",
616 pkt_dev
->dst_min
, pkt_dev
->dst_max
, pkt_dev
->src_min
,
619 seq_puts(seq
, " src_mac: ");
621 if (is_zero_ether_addr(pkt_dev
->src_mac
))
622 for (i
= 0; i
< 6; i
++)
623 seq_printf(seq
, "%02X%s", pkt_dev
->odev
->dev_addr
[i
],
626 for (i
= 0; i
< 6; i
++)
627 seq_printf(seq
, "%02X%s", pkt_dev
->src_mac
[i
],
630 seq_printf(seq
, "dst_mac: ");
631 for (i
= 0; i
< 6; i
++)
632 seq_printf(seq
, "%02X%s", pkt_dev
->dst_mac
[i
],
633 i
== 5 ? "\n" : ":");
636 " udp_src_min: %d udp_src_max: %d udp_dst_min: %d udp_dst_max: %d\n",
637 pkt_dev
->udp_src_min
, pkt_dev
->udp_src_max
,
638 pkt_dev
->udp_dst_min
, pkt_dev
->udp_dst_max
);
641 " src_mac_count: %d dst_mac_count: %d\n",
642 pkt_dev
->src_mac_count
, pkt_dev
->dst_mac_count
);
644 if (pkt_dev
->nr_labels
) {
646 seq_printf(seq
, " mpls: ");
647 for (i
= 0; i
< pkt_dev
->nr_labels
; i
++)
648 seq_printf(seq
, "%08x%s", ntohl(pkt_dev
->labels
[i
]),
649 i
== pkt_dev
->nr_labels
-1 ? "\n" : ", ");
652 if (pkt_dev
->vlan_id
!= 0xffff) {
653 seq_printf(seq
, " vlan_id: %u vlan_p: %u vlan_cfi: %u\n",
654 pkt_dev
->vlan_id
, pkt_dev
->vlan_p
, pkt_dev
->vlan_cfi
);
657 if (pkt_dev
->svlan_id
!= 0xffff) {
658 seq_printf(seq
, " svlan_id: %u vlan_p: %u vlan_cfi: %u\n",
659 pkt_dev
->svlan_id
, pkt_dev
->svlan_p
, pkt_dev
->svlan_cfi
);
663 seq_printf(seq
, " tos: 0x%02x\n", pkt_dev
->tos
);
666 if (pkt_dev
->traffic_class
) {
667 seq_printf(seq
, " traffic_class: 0x%02x\n", pkt_dev
->traffic_class
);
670 seq_printf(seq
, " Flags: ");
672 if (pkt_dev
->flags
& F_IPV6
)
673 seq_printf(seq
, "IPV6 ");
675 if (pkt_dev
->flags
& F_IPSRC_RND
)
676 seq_printf(seq
, "IPSRC_RND ");
678 if (pkt_dev
->flags
& F_IPDST_RND
)
679 seq_printf(seq
, "IPDST_RND ");
681 if (pkt_dev
->flags
& F_TXSIZE_RND
)
682 seq_printf(seq
, "TXSIZE_RND ");
684 if (pkt_dev
->flags
& F_UDPSRC_RND
)
685 seq_printf(seq
, "UDPSRC_RND ");
687 if (pkt_dev
->flags
& F_UDPDST_RND
)
688 seq_printf(seq
, "UDPDST_RND ");
690 if (pkt_dev
->flags
& F_MPLS_RND
)
691 seq_printf(seq
, "MPLS_RND ");
693 if (pkt_dev
->flags
& F_MACSRC_RND
)
694 seq_printf(seq
, "MACSRC_RND ");
696 if (pkt_dev
->flags
& F_MACDST_RND
)
697 seq_printf(seq
, "MACDST_RND ");
699 if (pkt_dev
->flags
& F_VID_RND
)
700 seq_printf(seq
, "VID_RND ");
702 if (pkt_dev
->flags
& F_SVID_RND
)
703 seq_printf(seq
, "SVID_RND ");
707 sa
= pkt_dev
->started_at
;
708 stopped
= pkt_dev
->stopped_at
;
709 if (pkt_dev
->running
)
710 stopped
= now
; /* not really stopped, more like last-running-at */
713 "Current:\n pkts-sofar: %llu errors: %llu\n started: %lluus stopped: %lluus idle: %lluus\n",
714 (unsigned long long)pkt_dev
->sofar
,
715 (unsigned long long)pkt_dev
->errors
, (unsigned long long)sa
,
716 (unsigned long long)stopped
,
717 (unsigned long long)pkt_dev
->idle_acc
);
720 " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n",
721 pkt_dev
->seq_num
, pkt_dev
->cur_dst_mac_offset
,
722 pkt_dev
->cur_src_mac_offset
);
724 if (pkt_dev
->flags
& F_IPV6
) {
725 char b1
[128], b2
[128];
726 fmt_ip6(b1
, pkt_dev
->cur_in6_daddr
.s6_addr
);
727 fmt_ip6(b2
, pkt_dev
->cur_in6_saddr
.s6_addr
);
728 seq_printf(seq
, " cur_saddr: %s cur_daddr: %s\n", b2
, b1
);
730 seq_printf(seq
, " cur_saddr: 0x%x cur_daddr: 0x%x\n",
731 pkt_dev
->cur_saddr
, pkt_dev
->cur_daddr
);
733 seq_printf(seq
, " cur_udp_dst: %d cur_udp_src: %d\n",
734 pkt_dev
->cur_udp_dst
, pkt_dev
->cur_udp_src
);
736 seq_printf(seq
, " flows: %u\n", pkt_dev
->nflows
);
738 if (pkt_dev
->result
[0])
739 seq_printf(seq
, "Result: %s\n", pkt_dev
->result
);
741 seq_printf(seq
, "Result: Idle\n");
747 static int hex32_arg(const char __user
*user_buffer
, unsigned long maxlen
, __u32
*num
)
752 for (; i
< maxlen
; i
++) {
755 if (get_user(c
, &user_buffer
[i
]))
757 if ((c
>= '0') && (c
<= '9'))
759 else if ((c
>= 'a') && (c
<= 'f'))
760 *num
|= c
- 'a' + 10;
761 else if ((c
>= 'A') && (c
<= 'F'))
762 *num
|= c
- 'A' + 10;
769 static int count_trail_chars(const char __user
* user_buffer
,
774 for (i
= 0; i
< maxlen
; i
++) {
776 if (get_user(c
, &user_buffer
[i
]))
794 static unsigned long num_arg(const char __user
* user_buffer
,
795 unsigned long maxlen
, unsigned long *num
)
800 for (; i
< maxlen
; i
++) {
802 if (get_user(c
, &user_buffer
[i
]))
804 if ((c
>= '0') && (c
<= '9')) {
813 static int strn_len(const char __user
* user_buffer
, unsigned int maxlen
)
817 for (; i
< maxlen
; i
++) {
819 if (get_user(c
, &user_buffer
[i
]))
837 static ssize_t
get_labels(const char __user
*buffer
, struct pktgen_dev
*pkt_dev
)
844 pkt_dev
->nr_labels
= 0;
847 len
= hex32_arg(&buffer
[i
], 8, &tmp
);
850 pkt_dev
->labels
[n
] = htonl(tmp
);
851 if (pkt_dev
->labels
[n
] & MPLS_STACK_BOTTOM
)
852 pkt_dev
->flags
|= F_MPLS_RND
;
854 if (get_user(c
, &buffer
[i
]))
858 if (n
>= MAX_MPLS_LABELS
)
862 pkt_dev
->nr_labels
= n
;
866 static ssize_t
pktgen_if_write(struct file
*file
,
867 const char __user
* user_buffer
, size_t count
,
870 struct seq_file
*seq
= (struct seq_file
*)file
->private_data
;
871 struct pktgen_dev
*pkt_dev
= seq
->private;
873 char name
[16], valstr
[32];
874 unsigned long value
= 0;
875 char *pg_result
= NULL
;
879 pg_result
= &(pkt_dev
->result
[0]);
882 printk("pktgen: wrong command format\n");
887 tmp
= count_trail_chars(&user_buffer
[i
], max
);
889 printk("pktgen: illegal format\n");
894 /* Read variable name */
896 len
= strn_len(&user_buffer
[i
], sizeof(name
) - 1);
900 memset(name
, 0, sizeof(name
));
901 if (copy_from_user(name
, &user_buffer
[i
], len
))
906 len
= count_trail_chars(&user_buffer
[i
], max
);
914 if (copy_from_user(tb
, user_buffer
, count
))
917 printk("pktgen: %s,%lu buffer -:%s:-\n", name
,
918 (unsigned long)count
, tb
);
921 if (!strcmp(name
, "min_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
->cur_pkt_size
= value
;
933 sprintf(pg_result
, "OK: min_pkt_size=%u",
934 pkt_dev
->min_pkt_size
);
938 if (!strcmp(name
, "max_pkt_size")) {
939 len
= num_arg(&user_buffer
[i
], 10, &value
);
944 if (value
< 14 + 20 + 8)
946 if (value
!= pkt_dev
->max_pkt_size
) {
947 pkt_dev
->max_pkt_size
= value
;
948 pkt_dev
->cur_pkt_size
= value
;
950 sprintf(pg_result
, "OK: max_pkt_size=%u",
951 pkt_dev
->max_pkt_size
);
955 /* Shortcut for min = max */
957 if (!strcmp(name
, "pkt_size")) {
958 len
= num_arg(&user_buffer
[i
], 10, &value
);
963 if (value
< 14 + 20 + 8)
965 if (value
!= pkt_dev
->min_pkt_size
) {
966 pkt_dev
->min_pkt_size
= value
;
967 pkt_dev
->max_pkt_size
= value
;
968 pkt_dev
->cur_pkt_size
= value
;
970 sprintf(pg_result
, "OK: pkt_size=%u", pkt_dev
->min_pkt_size
);
974 if (!strcmp(name
, "debug")) {
975 len
= num_arg(&user_buffer
[i
], 10, &value
);
981 sprintf(pg_result
, "OK: debug=%u", debug
);
985 if (!strcmp(name
, "frags")) {
986 len
= num_arg(&user_buffer
[i
], 10, &value
);
991 pkt_dev
->nfrags
= value
;
992 sprintf(pg_result
, "OK: frags=%u", pkt_dev
->nfrags
);
995 if (!strcmp(name
, "delay")) {
996 len
= num_arg(&user_buffer
[i
], 10, &value
);
1001 if (value
== 0x7FFFFFFF) {
1002 pkt_dev
->delay_us
= 0x7FFFFFFF;
1003 pkt_dev
->delay_ns
= 0;
1005 pkt_dev
->delay_us
= value
/ 1000;
1006 pkt_dev
->delay_ns
= value
% 1000;
1008 sprintf(pg_result
, "OK: delay=%u",
1009 1000 * pkt_dev
->delay_us
+ pkt_dev
->delay_ns
);
1012 if (!strcmp(name
, "udp_src_min")) {
1013 len
= num_arg(&user_buffer
[i
], 10, &value
);
1018 if (value
!= pkt_dev
->udp_src_min
) {
1019 pkt_dev
->udp_src_min
= value
;
1020 pkt_dev
->cur_udp_src
= value
;
1022 sprintf(pg_result
, "OK: udp_src_min=%u", pkt_dev
->udp_src_min
);
1025 if (!strcmp(name
, "udp_dst_min")) {
1026 len
= num_arg(&user_buffer
[i
], 10, &value
);
1031 if (value
!= pkt_dev
->udp_dst_min
) {
1032 pkt_dev
->udp_dst_min
= value
;
1033 pkt_dev
->cur_udp_dst
= value
;
1035 sprintf(pg_result
, "OK: udp_dst_min=%u", pkt_dev
->udp_dst_min
);
1038 if (!strcmp(name
, "udp_src_max")) {
1039 len
= num_arg(&user_buffer
[i
], 10, &value
);
1044 if (value
!= pkt_dev
->udp_src_max
) {
1045 pkt_dev
->udp_src_max
= value
;
1046 pkt_dev
->cur_udp_src
= value
;
1048 sprintf(pg_result
, "OK: udp_src_max=%u", pkt_dev
->udp_src_max
);
1051 if (!strcmp(name
, "udp_dst_max")) {
1052 len
= num_arg(&user_buffer
[i
], 10, &value
);
1057 if (value
!= pkt_dev
->udp_dst_max
) {
1058 pkt_dev
->udp_dst_max
= value
;
1059 pkt_dev
->cur_udp_dst
= value
;
1061 sprintf(pg_result
, "OK: udp_dst_max=%u", pkt_dev
->udp_dst_max
);
1064 if (!strcmp(name
, "clone_skb")) {
1065 len
= num_arg(&user_buffer
[i
], 10, &value
);
1070 pkt_dev
->clone_skb
= value
;
1072 sprintf(pg_result
, "OK: clone_skb=%d", pkt_dev
->clone_skb
);
1075 if (!strcmp(name
, "count")) {
1076 len
= num_arg(&user_buffer
[i
], 10, &value
);
1081 pkt_dev
->count
= value
;
1082 sprintf(pg_result
, "OK: count=%llu",
1083 (unsigned long long)pkt_dev
->count
);
1086 if (!strcmp(name
, "src_mac_count")) {
1087 len
= num_arg(&user_buffer
[i
], 10, &value
);
1092 if (pkt_dev
->src_mac_count
!= value
) {
1093 pkt_dev
->src_mac_count
= value
;
1094 pkt_dev
->cur_src_mac_offset
= 0;
1096 sprintf(pg_result
, "OK: src_mac_count=%d",
1097 pkt_dev
->src_mac_count
);
1100 if (!strcmp(name
, "dst_mac_count")) {
1101 len
= num_arg(&user_buffer
[i
], 10, &value
);
1106 if (pkt_dev
->dst_mac_count
!= value
) {
1107 pkt_dev
->dst_mac_count
= value
;
1108 pkt_dev
->cur_dst_mac_offset
= 0;
1110 sprintf(pg_result
, "OK: dst_mac_count=%d",
1111 pkt_dev
->dst_mac_count
);
1114 if (!strcmp(name
, "flag")) {
1117 len
= strn_len(&user_buffer
[i
], sizeof(f
) - 1);
1121 if (copy_from_user(f
, &user_buffer
[i
], len
))
1124 if (strcmp(f
, "IPSRC_RND") == 0)
1125 pkt_dev
->flags
|= F_IPSRC_RND
;
1127 else if (strcmp(f
, "!IPSRC_RND") == 0)
1128 pkt_dev
->flags
&= ~F_IPSRC_RND
;
1130 else if (strcmp(f
, "TXSIZE_RND") == 0)
1131 pkt_dev
->flags
|= F_TXSIZE_RND
;
1133 else if (strcmp(f
, "!TXSIZE_RND") == 0)
1134 pkt_dev
->flags
&= ~F_TXSIZE_RND
;
1136 else if (strcmp(f
, "IPDST_RND") == 0)
1137 pkt_dev
->flags
|= F_IPDST_RND
;
1139 else if (strcmp(f
, "!IPDST_RND") == 0)
1140 pkt_dev
->flags
&= ~F_IPDST_RND
;
1142 else if (strcmp(f
, "UDPSRC_RND") == 0)
1143 pkt_dev
->flags
|= F_UDPSRC_RND
;
1145 else if (strcmp(f
, "!UDPSRC_RND") == 0)
1146 pkt_dev
->flags
&= ~F_UDPSRC_RND
;
1148 else if (strcmp(f
, "UDPDST_RND") == 0)
1149 pkt_dev
->flags
|= F_UDPDST_RND
;
1151 else if (strcmp(f
, "!UDPDST_RND") == 0)
1152 pkt_dev
->flags
&= ~F_UDPDST_RND
;
1154 else if (strcmp(f
, "MACSRC_RND") == 0)
1155 pkt_dev
->flags
|= F_MACSRC_RND
;
1157 else if (strcmp(f
, "!MACSRC_RND") == 0)
1158 pkt_dev
->flags
&= ~F_MACSRC_RND
;
1160 else if (strcmp(f
, "MACDST_RND") == 0)
1161 pkt_dev
->flags
|= F_MACDST_RND
;
1163 else if (strcmp(f
, "!MACDST_RND") == 0)
1164 pkt_dev
->flags
&= ~F_MACDST_RND
;
1166 else if (strcmp(f
, "MPLS_RND") == 0)
1167 pkt_dev
->flags
|= F_MPLS_RND
;
1169 else if (strcmp(f
, "!MPLS_RND") == 0)
1170 pkt_dev
->flags
&= ~F_MPLS_RND
;
1172 else if (strcmp(f
, "VID_RND") == 0)
1173 pkt_dev
->flags
|= F_VID_RND
;
1175 else if (strcmp(f
, "!VID_RND") == 0)
1176 pkt_dev
->flags
&= ~F_VID_RND
;
1178 else if (strcmp(f
, "SVID_RND") == 0)
1179 pkt_dev
->flags
|= F_SVID_RND
;
1181 else if (strcmp(f
, "!SVID_RND") == 0)
1182 pkt_dev
->flags
&= ~F_SVID_RND
;
1184 else if (strcmp(f
, "!IPV6") == 0)
1185 pkt_dev
->flags
&= ~F_IPV6
;
1189 "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
1191 "IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
1192 "MACSRC_RND, MACDST_RND, TXSIZE_RND, IPV6, MPLS_RND, VID_RND, SVID_RND\n");
1195 sprintf(pg_result
, "OK: flags=0x%x", pkt_dev
->flags
);
1198 if (!strcmp(name
, "dst_min") || !strcmp(name
, "dst")) {
1199 len
= strn_len(&user_buffer
[i
], sizeof(pkt_dev
->dst_min
) - 1);
1204 if (copy_from_user(buf
, &user_buffer
[i
], len
))
1207 if (strcmp(buf
, pkt_dev
->dst_min
) != 0) {
1208 memset(pkt_dev
->dst_min
, 0, sizeof(pkt_dev
->dst_min
));
1209 strncpy(pkt_dev
->dst_min
, buf
, len
);
1210 pkt_dev
->daddr_min
= in_aton(pkt_dev
->dst_min
);
1211 pkt_dev
->cur_daddr
= pkt_dev
->daddr_min
;
1214 printk("pktgen: dst_min set to: %s\n",
1217 sprintf(pg_result
, "OK: dst_min=%s", pkt_dev
->dst_min
);
1220 if (!strcmp(name
, "dst_max")) {
1221 len
= strn_len(&user_buffer
[i
], sizeof(pkt_dev
->dst_max
) - 1);
1226 if (copy_from_user(buf
, &user_buffer
[i
], len
))
1230 if (strcmp(buf
, pkt_dev
->dst_max
) != 0) {
1231 memset(pkt_dev
->dst_max
, 0, sizeof(pkt_dev
->dst_max
));
1232 strncpy(pkt_dev
->dst_max
, buf
, len
);
1233 pkt_dev
->daddr_max
= in_aton(pkt_dev
->dst_max
);
1234 pkt_dev
->cur_daddr
= pkt_dev
->daddr_max
;
1237 printk("pktgen: dst_max set to: %s\n",
1240 sprintf(pg_result
, "OK: dst_max=%s", pkt_dev
->dst_max
);
1243 if (!strcmp(name
, "dst6")) {
1244 len
= strn_len(&user_buffer
[i
], sizeof(buf
) - 1);
1248 pkt_dev
->flags
|= F_IPV6
;
1250 if (copy_from_user(buf
, &user_buffer
[i
], len
))
1254 scan_ip6(buf
, pkt_dev
->in6_daddr
.s6_addr
);
1255 fmt_ip6(buf
, pkt_dev
->in6_daddr
.s6_addr
);
1257 ipv6_addr_copy(&pkt_dev
->cur_in6_daddr
, &pkt_dev
->in6_daddr
);
1260 printk("pktgen: dst6 set to: %s\n", buf
);
1263 sprintf(pg_result
, "OK: dst6=%s", buf
);
1266 if (!strcmp(name
, "dst6_min")) {
1267 len
= strn_len(&user_buffer
[i
], sizeof(buf
) - 1);
1271 pkt_dev
->flags
|= F_IPV6
;
1273 if (copy_from_user(buf
, &user_buffer
[i
], len
))
1277 scan_ip6(buf
, pkt_dev
->min_in6_daddr
.s6_addr
);
1278 fmt_ip6(buf
, pkt_dev
->min_in6_daddr
.s6_addr
);
1280 ipv6_addr_copy(&pkt_dev
->cur_in6_daddr
,
1281 &pkt_dev
->min_in6_daddr
);
1283 printk("pktgen: dst6_min set to: %s\n", buf
);
1286 sprintf(pg_result
, "OK: dst6_min=%s", buf
);
1289 if (!strcmp(name
, "dst6_max")) {
1290 len
= strn_len(&user_buffer
[i
], sizeof(buf
) - 1);
1294 pkt_dev
->flags
|= F_IPV6
;
1296 if (copy_from_user(buf
, &user_buffer
[i
], len
))
1300 scan_ip6(buf
, pkt_dev
->max_in6_daddr
.s6_addr
);
1301 fmt_ip6(buf
, pkt_dev
->max_in6_daddr
.s6_addr
);
1304 printk("pktgen: dst6_max set to: %s\n", buf
);
1307 sprintf(pg_result
, "OK: dst6_max=%s", buf
);
1310 if (!strcmp(name
, "src6")) {
1311 len
= strn_len(&user_buffer
[i
], sizeof(buf
) - 1);
1315 pkt_dev
->flags
|= F_IPV6
;
1317 if (copy_from_user(buf
, &user_buffer
[i
], len
))
1321 scan_ip6(buf
, pkt_dev
->in6_saddr
.s6_addr
);
1322 fmt_ip6(buf
, pkt_dev
->in6_saddr
.s6_addr
);
1324 ipv6_addr_copy(&pkt_dev
->cur_in6_saddr
, &pkt_dev
->in6_saddr
);
1327 printk("pktgen: src6 set to: %s\n", buf
);
1330 sprintf(pg_result
, "OK: src6=%s", buf
);
1333 if (!strcmp(name
, "src_min")) {
1334 len
= strn_len(&user_buffer
[i
], sizeof(pkt_dev
->src_min
) - 1);
1338 if (copy_from_user(buf
, &user_buffer
[i
], len
))
1341 if (strcmp(buf
, pkt_dev
->src_min
) != 0) {
1342 memset(pkt_dev
->src_min
, 0, sizeof(pkt_dev
->src_min
));
1343 strncpy(pkt_dev
->src_min
, buf
, len
);
1344 pkt_dev
->saddr_min
= in_aton(pkt_dev
->src_min
);
1345 pkt_dev
->cur_saddr
= pkt_dev
->saddr_min
;
1348 printk("pktgen: src_min set to: %s\n",
1351 sprintf(pg_result
, "OK: src_min=%s", pkt_dev
->src_min
);
1354 if (!strcmp(name
, "src_max")) {
1355 len
= strn_len(&user_buffer
[i
], sizeof(pkt_dev
->src_max
) - 1);
1359 if (copy_from_user(buf
, &user_buffer
[i
], len
))
1362 if (strcmp(buf
, pkt_dev
->src_max
) != 0) {
1363 memset(pkt_dev
->src_max
, 0, sizeof(pkt_dev
->src_max
));
1364 strncpy(pkt_dev
->src_max
, buf
, len
);
1365 pkt_dev
->saddr_max
= in_aton(pkt_dev
->src_max
);
1366 pkt_dev
->cur_saddr
= pkt_dev
->saddr_max
;
1369 printk("pktgen: src_max set to: %s\n",
1372 sprintf(pg_result
, "OK: src_max=%s", pkt_dev
->src_max
);
1375 if (!strcmp(name
, "dst_mac")) {
1377 unsigned char old_dmac
[ETH_ALEN
];
1378 unsigned char *m
= pkt_dev
->dst_mac
;
1379 memcpy(old_dmac
, pkt_dev
->dst_mac
, ETH_ALEN
);
1381 len
= strn_len(&user_buffer
[i
], sizeof(valstr
) - 1);
1385 memset(valstr
, 0, sizeof(valstr
));
1386 if (copy_from_user(valstr
, &user_buffer
[i
], len
))
1390 for (*m
= 0; *v
&& m
< pkt_dev
->dst_mac
+ 6; v
++) {
1391 if (*v
>= '0' && *v
<= '9') {
1395 if (*v
>= 'A' && *v
<= 'F') {
1397 *m
+= *v
- 'A' + 10;
1399 if (*v
>= 'a' && *v
<= 'f') {
1401 *m
+= *v
- 'a' + 10;
1409 /* Set up Dest MAC */
1410 if (compare_ether_addr(old_dmac
, pkt_dev
->dst_mac
))
1411 memcpy(&(pkt_dev
->hh
[0]), pkt_dev
->dst_mac
, ETH_ALEN
);
1413 sprintf(pg_result
, "OK: dstmac");
1416 if (!strcmp(name
, "src_mac")) {
1418 unsigned char *m
= pkt_dev
->src_mac
;
1420 len
= strn_len(&user_buffer
[i
], sizeof(valstr
) - 1);
1424 memset(valstr
, 0, sizeof(valstr
));
1425 if (copy_from_user(valstr
, &user_buffer
[i
], len
))
1429 for (*m
= 0; *v
&& m
< pkt_dev
->src_mac
+ 6; v
++) {
1430 if (*v
>= '0' && *v
<= '9') {
1434 if (*v
>= 'A' && *v
<= 'F') {
1436 *m
+= *v
- 'A' + 10;
1438 if (*v
>= 'a' && *v
<= 'f') {
1440 *m
+= *v
- 'a' + 10;
1448 sprintf(pg_result
, "OK: srcmac");
1452 if (!strcmp(name
, "clear_counters")) {
1453 pktgen_clear_counters(pkt_dev
);
1454 sprintf(pg_result
, "OK: Clearing counters.\n");
1458 if (!strcmp(name
, "flows")) {
1459 len
= num_arg(&user_buffer
[i
], 10, &value
);
1464 if (value
> MAX_CFLOWS
)
1467 pkt_dev
->cflows
= value
;
1468 sprintf(pg_result
, "OK: flows=%u", pkt_dev
->cflows
);
1472 if (!strcmp(name
, "flowlen")) {
1473 len
= num_arg(&user_buffer
[i
], 10, &value
);
1478 pkt_dev
->lflow
= value
;
1479 sprintf(pg_result
, "OK: flowlen=%u", pkt_dev
->lflow
);
1483 if (!strcmp(name
, "mpls")) {
1485 len
= get_labels(&user_buffer
[i
], pkt_dev
);
1486 if (len
< 0) { return len
; }
1488 offset
= sprintf(pg_result
, "OK: mpls=");
1489 for (n
= 0; n
< pkt_dev
->nr_labels
; n
++)
1490 offset
+= sprintf(pg_result
+ offset
,
1491 "%08x%s", ntohl(pkt_dev
->labels
[n
]),
1492 n
== pkt_dev
->nr_labels
-1 ? "" : ",");
1494 if (pkt_dev
->nr_labels
&& pkt_dev
->vlan_id
!= 0xffff) {
1495 pkt_dev
->vlan_id
= 0xffff; /* turn off VLAN/SVLAN */
1496 pkt_dev
->svlan_id
= 0xffff;
1499 printk("pktgen: VLAN/SVLAN auto turned off\n");
1504 if (!strcmp(name
, "vlan_id")) {
1505 len
= num_arg(&user_buffer
[i
], 4, &value
);
1510 if (value
<= 4095) {
1511 pkt_dev
->vlan_id
= value
; /* turn on VLAN */
1514 printk("pktgen: VLAN turned on\n");
1516 if (debug
&& pkt_dev
->nr_labels
)
1517 printk("pktgen: MPLS auto turned off\n");
1519 pkt_dev
->nr_labels
= 0; /* turn off MPLS */
1520 sprintf(pg_result
, "OK: vlan_id=%u", pkt_dev
->vlan_id
);
1522 pkt_dev
->vlan_id
= 0xffff; /* turn off VLAN/SVLAN */
1523 pkt_dev
->svlan_id
= 0xffff;
1526 printk("pktgen: VLAN/SVLAN turned off\n");
1531 if (!strcmp(name
, "vlan_p")) {
1532 len
= num_arg(&user_buffer
[i
], 1, &value
);
1537 if ((value
<= 7) && (pkt_dev
->vlan_id
!= 0xffff)) {
1538 pkt_dev
->vlan_p
= value
;
1539 sprintf(pg_result
, "OK: vlan_p=%u", pkt_dev
->vlan_p
);
1541 sprintf(pg_result
, "ERROR: vlan_p must be 0-7");
1546 if (!strcmp(name
, "vlan_cfi")) {
1547 len
= num_arg(&user_buffer
[i
], 1, &value
);
1552 if ((value
<= 1) && (pkt_dev
->vlan_id
!= 0xffff)) {
1553 pkt_dev
->vlan_cfi
= value
;
1554 sprintf(pg_result
, "OK: vlan_cfi=%u", pkt_dev
->vlan_cfi
);
1556 sprintf(pg_result
, "ERROR: vlan_cfi must be 0-1");
1561 if (!strcmp(name
, "svlan_id")) {
1562 len
= num_arg(&user_buffer
[i
], 4, &value
);
1567 if ((value
<= 4095) && ((pkt_dev
->vlan_id
!= 0xffff))) {
1568 pkt_dev
->svlan_id
= value
; /* turn on SVLAN */
1571 printk("pktgen: SVLAN turned on\n");
1573 if (debug
&& pkt_dev
->nr_labels
)
1574 printk("pktgen: MPLS auto turned off\n");
1576 pkt_dev
->nr_labels
= 0; /* turn off MPLS */
1577 sprintf(pg_result
, "OK: svlan_id=%u", pkt_dev
->svlan_id
);
1579 pkt_dev
->vlan_id
= 0xffff; /* turn off VLAN/SVLAN */
1580 pkt_dev
->svlan_id
= 0xffff;
1583 printk("pktgen: VLAN/SVLAN turned off\n");
1588 if (!strcmp(name
, "svlan_p")) {
1589 len
= num_arg(&user_buffer
[i
], 1, &value
);
1594 if ((value
<= 7) && (pkt_dev
->svlan_id
!= 0xffff)) {
1595 pkt_dev
->svlan_p
= value
;
1596 sprintf(pg_result
, "OK: svlan_p=%u", pkt_dev
->svlan_p
);
1598 sprintf(pg_result
, "ERROR: svlan_p must be 0-7");
1603 if (!strcmp(name
, "svlan_cfi")) {
1604 len
= num_arg(&user_buffer
[i
], 1, &value
);
1609 if ((value
<= 1) && (pkt_dev
->svlan_id
!= 0xffff)) {
1610 pkt_dev
->svlan_cfi
= value
;
1611 sprintf(pg_result
, "OK: svlan_cfi=%u", pkt_dev
->svlan_cfi
);
1613 sprintf(pg_result
, "ERROR: svlan_cfi must be 0-1");
1618 if (!strcmp(name
, "tos")) {
1619 __u32 tmp_value
= 0;
1620 len
= hex32_arg(&user_buffer
[i
], 2, &tmp_value
);
1626 pkt_dev
->tos
= tmp_value
;
1627 sprintf(pg_result
, "OK: tos=0x%02x", pkt_dev
->tos
);
1629 sprintf(pg_result
, "ERROR: tos must be 00-ff");
1634 if (!strcmp(name
, "traffic_class")) {
1635 __u32 tmp_value
= 0;
1636 len
= hex32_arg(&user_buffer
[i
], 2, &tmp_value
);
1642 pkt_dev
->traffic_class
= tmp_value
;
1643 sprintf(pg_result
, "OK: traffic_class=0x%02x", pkt_dev
->traffic_class
);
1645 sprintf(pg_result
, "ERROR: traffic_class must be 00-ff");
1650 sprintf(pkt_dev
->result
, "No such parameter \"%s\"", name
);
1654 static int pktgen_if_open(struct inode
*inode
, struct file
*file
)
1656 return single_open(file
, pktgen_if_show
, PDE(inode
)->data
);
1659 static const struct file_operations pktgen_if_fops
= {
1660 .owner
= THIS_MODULE
,
1661 .open
= pktgen_if_open
,
1663 .llseek
= seq_lseek
,
1664 .write
= pktgen_if_write
,
1665 .release
= single_release
,
1668 static int pktgen_thread_show(struct seq_file
*seq
, void *v
)
1670 struct pktgen_thread
*t
= seq
->private;
1671 struct pktgen_dev
*pkt_dev
;
1675 seq_printf(seq
, "Name: %s max_before_softirq: %d\n",
1676 t
->tsk
->comm
, t
->max_before_softirq
);
1678 seq_printf(seq
, "Running: ");
1681 list_for_each_entry(pkt_dev
, &t
->if_list
, list
)
1682 if (pkt_dev
->running
)
1683 seq_printf(seq
, "%s ", pkt_dev
->odev
->name
);
1685 seq_printf(seq
, "\nStopped: ");
1687 list_for_each_entry(pkt_dev
, &t
->if_list
, list
)
1688 if (!pkt_dev
->running
)
1689 seq_printf(seq
, "%s ", pkt_dev
->odev
->name
);
1692 seq_printf(seq
, "\nResult: %s\n", t
->result
);
1694 seq_printf(seq
, "\nResult: NA\n");
1701 static ssize_t
pktgen_thread_write(struct file
*file
,
1702 const char __user
* user_buffer
,
1703 size_t count
, loff_t
* offset
)
1705 struct seq_file
*seq
= (struct seq_file
*)file
->private_data
;
1706 struct pktgen_thread
*t
= seq
->private;
1707 int i
= 0, max
, len
, ret
;
1710 unsigned long value
= 0;
1713 // sprintf(pg_result, "Wrong command format");
1718 len
= count_trail_chars(&user_buffer
[i
], max
);
1724 /* Read variable name */
1726 len
= strn_len(&user_buffer
[i
], sizeof(name
) - 1);
1730 memset(name
, 0, sizeof(name
));
1731 if (copy_from_user(name
, &user_buffer
[i
], len
))
1736 len
= count_trail_chars(&user_buffer
[i
], max
);
1743 printk("pktgen: t=%s, count=%lu\n", name
, (unsigned long)count
);
1746 printk("pktgen: ERROR: No thread\n");
1751 pg_result
= &(t
->result
[0]);
1753 if (!strcmp(name
, "add_device")) {
1756 len
= strn_len(&user_buffer
[i
], sizeof(f
) - 1);
1761 if (copy_from_user(f
, &user_buffer
[i
], len
))
1764 mutex_lock(&pktgen_thread_lock
);
1765 pktgen_add_device(t
, f
);
1766 mutex_unlock(&pktgen_thread_lock
);
1768 sprintf(pg_result
, "OK: add_device=%s", f
);
1772 if (!strcmp(name
, "rem_device_all")) {
1773 mutex_lock(&pktgen_thread_lock
);
1774 t
->control
|= T_REMDEVALL
;
1775 mutex_unlock(&pktgen_thread_lock
);
1776 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
1778 sprintf(pg_result
, "OK: rem_device_all");
1782 if (!strcmp(name
, "max_before_softirq")) {
1783 len
= num_arg(&user_buffer
[i
], 10, &value
);
1784 mutex_lock(&pktgen_thread_lock
);
1785 t
->max_before_softirq
= value
;
1786 mutex_unlock(&pktgen_thread_lock
);
1788 sprintf(pg_result
, "OK: max_before_softirq=%lu", value
);
1797 static int pktgen_thread_open(struct inode
*inode
, struct file
*file
)
1799 return single_open(file
, pktgen_thread_show
, PDE(inode
)->data
);
1802 static const struct file_operations pktgen_thread_fops
= {
1803 .owner
= THIS_MODULE
,
1804 .open
= pktgen_thread_open
,
1806 .llseek
= seq_lseek
,
1807 .write
= pktgen_thread_write
,
1808 .release
= single_release
,
1811 /* Think find or remove for NN */
1812 static struct pktgen_dev
*__pktgen_NN_threads(const char *ifname
, int remove
)
1814 struct pktgen_thread
*t
;
1815 struct pktgen_dev
*pkt_dev
= NULL
;
1817 list_for_each_entry(t
, &pktgen_threads
, th_list
) {
1818 pkt_dev
= pktgen_find_dev(t
, ifname
);
1822 pkt_dev
->removal_mark
= 1;
1823 t
->control
|= T_REMDEV
;
1833 * mark a device for removal
1835 static void pktgen_mark_device(const char *ifname
)
1837 struct pktgen_dev
*pkt_dev
= NULL
;
1838 const int max_tries
= 10, msec_per_try
= 125;
1841 mutex_lock(&pktgen_thread_lock
);
1842 pr_debug("pktgen: pktgen_mark_device marking %s for removal\n", ifname
);
1846 pkt_dev
= __pktgen_NN_threads(ifname
, REMOVE
);
1847 if (pkt_dev
== NULL
)
1848 break; /* success */
1850 mutex_unlock(&pktgen_thread_lock
);
1851 pr_debug("pktgen: pktgen_mark_device waiting for %s "
1852 "to disappear....\n", ifname
);
1853 schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try
));
1854 mutex_lock(&pktgen_thread_lock
);
1856 if (++i
>= max_tries
) {
1857 printk("pktgen_mark_device: timed out after waiting "
1858 "%d msec for device %s to be removed\n",
1859 msec_per_try
* i
, ifname
);
1865 mutex_unlock(&pktgen_thread_lock
);
1868 static void pktgen_change_name(struct net_device
*dev
)
1870 struct pktgen_thread
*t
;
1872 list_for_each_entry(t
, &pktgen_threads
, th_list
) {
1873 struct pktgen_dev
*pkt_dev
;
1875 list_for_each_entry(pkt_dev
, &t
->if_list
, list
) {
1876 if (pkt_dev
->odev
!= dev
)
1879 remove_proc_entry(pkt_dev
->entry
->name
, pg_proc_dir
);
1881 pkt_dev
->entry
= create_proc_entry(dev
->name
, 0600,
1883 if (!pkt_dev
->entry
)
1884 printk(KERN_ERR
"pktgen: can't move proc "
1885 " entry for '%s'\n", dev
->name
);
1891 static int pktgen_device_event(struct notifier_block
*unused
,
1892 unsigned long event
, void *ptr
)
1894 struct net_device
*dev
= ptr
;
1896 /* It is OK that we do not hold the group lock right now,
1897 * as we run under the RTNL lock.
1901 case NETDEV_CHANGENAME
:
1902 pktgen_change_name(dev
);
1905 case NETDEV_UNREGISTER
:
1906 pktgen_mark_device(dev
->name
);
1913 /* Associate pktgen_dev with a device. */
1915 static int pktgen_setup_dev(struct pktgen_dev
*pkt_dev
, const char *ifname
)
1917 struct net_device
*odev
;
1920 /* Clean old setups */
1921 if (pkt_dev
->odev
) {
1922 dev_put(pkt_dev
->odev
);
1923 pkt_dev
->odev
= NULL
;
1926 odev
= dev_get_by_name(ifname
);
1928 printk("pktgen: no such netdevice: \"%s\"\n", ifname
);
1932 if (odev
->type
!= ARPHRD_ETHER
) {
1933 printk("pktgen: not an ethernet device: \"%s\"\n", ifname
);
1935 } else if (!netif_running(odev
)) {
1936 printk("pktgen: device is down: \"%s\"\n", ifname
);
1939 pkt_dev
->odev
= odev
;
1947 /* Read pkt_dev from the interface and set up internal pktgen_dev
1948 * structure to have the right information to create/send packets
1950 static void pktgen_setup_inject(struct pktgen_dev
*pkt_dev
)
1952 if (!pkt_dev
->odev
) {
1953 printk("pktgen: ERROR: pkt_dev->odev == NULL in setup_inject.\n");
1954 sprintf(pkt_dev
->result
,
1955 "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
1959 /* Default to the interface's mac if not explicitly set. */
1961 if (is_zero_ether_addr(pkt_dev
->src_mac
))
1962 memcpy(&(pkt_dev
->hh
[6]), pkt_dev
->odev
->dev_addr
, ETH_ALEN
);
1964 /* Set up Dest MAC */
1965 memcpy(&(pkt_dev
->hh
[0]), pkt_dev
->dst_mac
, ETH_ALEN
);
1967 /* Set up pkt size */
1968 pkt_dev
->cur_pkt_size
= pkt_dev
->min_pkt_size
;
1970 if (pkt_dev
->flags
& F_IPV6
) {
1972 * Skip this automatic address setting until locks or functions
1977 int i
, set
= 0, err
= 1;
1978 struct inet6_dev
*idev
;
1980 for (i
= 0; i
< IN6_ADDR_HSIZE
; i
++)
1981 if (pkt_dev
->cur_in6_saddr
.s6_addr
[i
]) {
1989 * Use linklevel address if unconfigured.
1991 * use ipv6_get_lladdr if/when it's get exported
1995 if ((idev
= __in6_dev_get(pkt_dev
->odev
)) != NULL
) {
1996 struct inet6_ifaddr
*ifp
;
1998 read_lock_bh(&idev
->lock
);
1999 for (ifp
= idev
->addr_list
; ifp
;
2000 ifp
= ifp
->if_next
) {
2001 if (ifp
->scope
== IFA_LINK
2003 flags
& IFA_F_TENTATIVE
)) {
2004 ipv6_addr_copy(&pkt_dev
->
2011 read_unlock_bh(&idev
->lock
);
2015 printk("pktgen: ERROR: IPv6 link address not availble.\n");
2019 pkt_dev
->saddr_min
= 0;
2020 pkt_dev
->saddr_max
= 0;
2021 if (strlen(pkt_dev
->src_min
) == 0) {
2023 struct in_device
*in_dev
;
2026 in_dev
= __in_dev_get_rcu(pkt_dev
->odev
);
2028 if (in_dev
->ifa_list
) {
2029 pkt_dev
->saddr_min
=
2030 in_dev
->ifa_list
->ifa_address
;
2031 pkt_dev
->saddr_max
= pkt_dev
->saddr_min
;
2036 pkt_dev
->saddr_min
= in_aton(pkt_dev
->src_min
);
2037 pkt_dev
->saddr_max
= in_aton(pkt_dev
->src_max
);
2040 pkt_dev
->daddr_min
= in_aton(pkt_dev
->dst_min
);
2041 pkt_dev
->daddr_max
= in_aton(pkt_dev
->dst_max
);
2043 /* Initialize current values. */
2044 pkt_dev
->cur_dst_mac_offset
= 0;
2045 pkt_dev
->cur_src_mac_offset
= 0;
2046 pkt_dev
->cur_saddr
= pkt_dev
->saddr_min
;
2047 pkt_dev
->cur_daddr
= pkt_dev
->daddr_min
;
2048 pkt_dev
->cur_udp_dst
= pkt_dev
->udp_dst_min
;
2049 pkt_dev
->cur_udp_src
= pkt_dev
->udp_src_min
;
2050 pkt_dev
->nflows
= 0;
2053 static void spin(struct pktgen_dev
*pkt_dev
, __u64 spin_until_us
)
2058 start
= now
= getCurUs();
2059 printk(KERN_INFO
"sleeping for %d\n", (int)(spin_until_us
- now
));
2060 while (now
< spin_until_us
) {
2061 /* TODO: optimize sleeping behavior */
2062 if (spin_until_us
- now
> jiffies_to_usecs(1) + 1)
2063 schedule_timeout_interruptible(1);
2064 else if (spin_until_us
- now
> 100) {
2066 if (!pkt_dev
->running
)
2075 pkt_dev
->idle_acc
+= now
- start
;
2078 /* Increment/randomize headers according to flags and current values
2079 * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
2081 static void mod_cur_headers(struct pktgen_dev
*pkt_dev
)
2087 if (pkt_dev
->cflows
) {
2088 flow
= random32() % pkt_dev
->cflows
;
2090 if (pkt_dev
->flows
[flow
].count
> pkt_dev
->lflow
)
2091 pkt_dev
->flows
[flow
].count
= 0;
2094 /* Deal with source MAC */
2095 if (pkt_dev
->src_mac_count
> 1) {
2099 if (pkt_dev
->flags
& F_MACSRC_RND
)
2100 mc
= random32() % pkt_dev
->src_mac_count
;
2102 mc
= pkt_dev
->cur_src_mac_offset
++;
2103 if (pkt_dev
->cur_src_mac_offset
>
2104 pkt_dev
->src_mac_count
)
2105 pkt_dev
->cur_src_mac_offset
= 0;
2108 tmp
= pkt_dev
->src_mac
[5] + (mc
& 0xFF);
2109 pkt_dev
->hh
[11] = tmp
;
2110 tmp
= (pkt_dev
->src_mac
[4] + ((mc
>> 8) & 0xFF) + (tmp
>> 8));
2111 pkt_dev
->hh
[10] = tmp
;
2112 tmp
= (pkt_dev
->src_mac
[3] + ((mc
>> 16) & 0xFF) + (tmp
>> 8));
2113 pkt_dev
->hh
[9] = tmp
;
2114 tmp
= (pkt_dev
->src_mac
[2] + ((mc
>> 24) & 0xFF) + (tmp
>> 8));
2115 pkt_dev
->hh
[8] = tmp
;
2116 tmp
= (pkt_dev
->src_mac
[1] + (tmp
>> 8));
2117 pkt_dev
->hh
[7] = tmp
;
2120 /* Deal with Destination MAC */
2121 if (pkt_dev
->dst_mac_count
> 1) {
2125 if (pkt_dev
->flags
& F_MACDST_RND
)
2126 mc
= random32() % pkt_dev
->dst_mac_count
;
2129 mc
= pkt_dev
->cur_dst_mac_offset
++;
2130 if (pkt_dev
->cur_dst_mac_offset
>
2131 pkt_dev
->dst_mac_count
) {
2132 pkt_dev
->cur_dst_mac_offset
= 0;
2136 tmp
= pkt_dev
->dst_mac
[5] + (mc
& 0xFF);
2137 pkt_dev
->hh
[5] = tmp
;
2138 tmp
= (pkt_dev
->dst_mac
[4] + ((mc
>> 8) & 0xFF) + (tmp
>> 8));
2139 pkt_dev
->hh
[4] = tmp
;
2140 tmp
= (pkt_dev
->dst_mac
[3] + ((mc
>> 16) & 0xFF) + (tmp
>> 8));
2141 pkt_dev
->hh
[3] = tmp
;
2142 tmp
= (pkt_dev
->dst_mac
[2] + ((mc
>> 24) & 0xFF) + (tmp
>> 8));
2143 pkt_dev
->hh
[2] = tmp
;
2144 tmp
= (pkt_dev
->dst_mac
[1] + (tmp
>> 8));
2145 pkt_dev
->hh
[1] = tmp
;
2148 if (pkt_dev
->flags
& F_MPLS_RND
) {
2150 for (i
= 0; i
< pkt_dev
->nr_labels
; i
++)
2151 if (pkt_dev
->labels
[i
] & MPLS_STACK_BOTTOM
)
2152 pkt_dev
->labels
[i
] = MPLS_STACK_BOTTOM
|
2153 ((__force __be32
)random32() &
2157 if ((pkt_dev
->flags
& F_VID_RND
) && (pkt_dev
->vlan_id
!= 0xffff)) {
2158 pkt_dev
->vlan_id
= random32() & (4096-1);
2161 if ((pkt_dev
->flags
& F_SVID_RND
) && (pkt_dev
->svlan_id
!= 0xffff)) {
2162 pkt_dev
->svlan_id
= random32() & (4096 - 1);
2165 if (pkt_dev
->udp_src_min
< pkt_dev
->udp_src_max
) {
2166 if (pkt_dev
->flags
& F_UDPSRC_RND
)
2167 pkt_dev
->cur_udp_src
= random32() %
2168 (pkt_dev
->udp_src_max
- pkt_dev
->udp_src_min
)
2169 + pkt_dev
->udp_src_min
;
2172 pkt_dev
->cur_udp_src
++;
2173 if (pkt_dev
->cur_udp_src
>= pkt_dev
->udp_src_max
)
2174 pkt_dev
->cur_udp_src
= pkt_dev
->udp_src_min
;
2178 if (pkt_dev
->udp_dst_min
< pkt_dev
->udp_dst_max
) {
2179 if (pkt_dev
->flags
& F_UDPDST_RND
) {
2180 pkt_dev
->cur_udp_dst
= random32() %
2181 (pkt_dev
->udp_dst_max
- pkt_dev
->udp_dst_min
)
2182 + pkt_dev
->udp_dst_min
;
2184 pkt_dev
->cur_udp_dst
++;
2185 if (pkt_dev
->cur_udp_dst
>= pkt_dev
->udp_dst_max
)
2186 pkt_dev
->cur_udp_dst
= pkt_dev
->udp_dst_min
;
2190 if (!(pkt_dev
->flags
& F_IPV6
)) {
2192 if ((imn
= ntohl(pkt_dev
->saddr_min
)) < (imx
=
2196 if (pkt_dev
->flags
& F_IPSRC_RND
)
2197 t
= random32() % (imx
- imn
) + imn
;
2199 t
= ntohl(pkt_dev
->cur_saddr
);
2205 pkt_dev
->cur_saddr
= htonl(t
);
2208 if (pkt_dev
->cflows
&& pkt_dev
->flows
[flow
].count
!= 0) {
2209 pkt_dev
->cur_daddr
= pkt_dev
->flows
[flow
].cur_daddr
;
2211 imn
= ntohl(pkt_dev
->daddr_min
);
2212 imx
= ntohl(pkt_dev
->daddr_max
);
2216 if (pkt_dev
->flags
& F_IPDST_RND
) {
2218 t
= random32() % (imx
- imn
) + imn
;
2221 while (LOOPBACK(s
) || MULTICAST(s
)
2222 || BADCLASS(s
) || ZERONET(s
)
2223 || LOCAL_MCAST(s
)) {
2224 t
= random32() % (imx
- imn
) + imn
;
2227 pkt_dev
->cur_daddr
= s
;
2229 t
= ntohl(pkt_dev
->cur_daddr
);
2234 pkt_dev
->cur_daddr
= htonl(t
);
2237 if (pkt_dev
->cflows
) {
2238 pkt_dev
->flows
[flow
].cur_daddr
=
2243 } else { /* IPV6 * */
2245 if (pkt_dev
->min_in6_daddr
.s6_addr32
[0] == 0 &&
2246 pkt_dev
->min_in6_daddr
.s6_addr32
[1] == 0 &&
2247 pkt_dev
->min_in6_daddr
.s6_addr32
[2] == 0 &&
2248 pkt_dev
->min_in6_daddr
.s6_addr32
[3] == 0) ;
2252 /* Only random destinations yet */
2254 for (i
= 0; i
< 4; i
++) {
2255 pkt_dev
->cur_in6_daddr
.s6_addr32
[i
] =
2256 (((__force __be32
)random32() |
2257 pkt_dev
->min_in6_daddr
.s6_addr32
[i
]) &
2258 pkt_dev
->max_in6_daddr
.s6_addr32
[i
]);
2263 if (pkt_dev
->min_pkt_size
< pkt_dev
->max_pkt_size
) {
2265 if (pkt_dev
->flags
& F_TXSIZE_RND
) {
2267 (pkt_dev
->max_pkt_size
- pkt_dev
->min_pkt_size
)
2268 + pkt_dev
->min_pkt_size
;
2270 t
= pkt_dev
->cur_pkt_size
+ 1;
2271 if (t
> pkt_dev
->max_pkt_size
)
2272 t
= pkt_dev
->min_pkt_size
;
2274 pkt_dev
->cur_pkt_size
= t
;
2277 pkt_dev
->flows
[flow
].count
++;
2280 static void mpls_push(__be32
*mpls
, struct pktgen_dev
*pkt_dev
)
2283 for (i
= 0; i
< pkt_dev
->nr_labels
; i
++) {
2284 *mpls
++ = pkt_dev
->labels
[i
] & ~MPLS_STACK_BOTTOM
;
2287 *mpls
|= MPLS_STACK_BOTTOM
;
2290 static inline __be16
build_tci(unsigned int id
, unsigned int cfi
,
2293 return htons(id
| (cfi
<< 12) | (prio
<< 13));
2296 static struct sk_buff
*fill_packet_ipv4(struct net_device
*odev
,
2297 struct pktgen_dev
*pkt_dev
)
2299 struct sk_buff
*skb
= NULL
;
2301 struct udphdr
*udph
;
2304 struct pktgen_hdr
*pgh
= NULL
;
2305 __be16 protocol
= htons(ETH_P_IP
);
2307 __be16
*vlan_tci
= NULL
; /* Encapsulates priority and VLAN ID */
2308 __be16
*vlan_encapsulated_proto
= NULL
; /* packet type ID field (or len) for VLAN tag */
2309 __be16
*svlan_tci
= NULL
; /* Encapsulates priority and SVLAN ID */
2310 __be16
*svlan_encapsulated_proto
= NULL
; /* packet type ID field (or len) for SVLAN tag */
2313 if (pkt_dev
->nr_labels
)
2314 protocol
= htons(ETH_P_MPLS_UC
);
2316 if (pkt_dev
->vlan_id
!= 0xffff)
2317 protocol
= htons(ETH_P_8021Q
);
2319 /* Update any of the values, used when we're incrementing various
2322 mod_cur_headers(pkt_dev
);
2324 datalen
= (odev
->hard_header_len
+ 16) & ~0xf;
2325 skb
= alloc_skb(pkt_dev
->cur_pkt_size
+ 64 + datalen
+
2326 pkt_dev
->nr_labels
*sizeof(u32
) +
2327 VLAN_TAG_SIZE(pkt_dev
) + SVLAN_TAG_SIZE(pkt_dev
),
2330 sprintf(pkt_dev
->result
, "No memory");
2334 skb_reserve(skb
, datalen
);
2336 /* Reserve for ethernet and IP header */
2337 eth
= (__u8
*) skb_push(skb
, 14);
2338 mpls
= (__be32
*)skb_put(skb
, pkt_dev
->nr_labels
*sizeof(__u32
));
2339 if (pkt_dev
->nr_labels
)
2340 mpls_push(mpls
, pkt_dev
);
2342 if (pkt_dev
->vlan_id
!= 0xffff) {
2343 if (pkt_dev
->svlan_id
!= 0xffff) {
2344 svlan_tci
= (__be16
*)skb_put(skb
, sizeof(__be16
));
2345 *svlan_tci
= build_tci(pkt_dev
->svlan_id
,
2348 svlan_encapsulated_proto
= (__be16
*)skb_put(skb
, sizeof(__be16
));
2349 *svlan_encapsulated_proto
= htons(ETH_P_8021Q
);
2351 vlan_tci
= (__be16
*)skb_put(skb
, sizeof(__be16
));
2352 *vlan_tci
= build_tci(pkt_dev
->vlan_id
,
2355 vlan_encapsulated_proto
= (__be16
*)skb_put(skb
, sizeof(__be16
));
2356 *vlan_encapsulated_proto
= htons(ETH_P_IP
);
2359 skb
->network_header
= skb
->tail
;
2360 skb
->transport_header
= skb
->network_header
+ sizeof(struct iphdr
);
2361 skb_put(skb
, sizeof(struct iphdr
) + sizeof(struct udphdr
));
2364 udph
= udp_hdr(skb
);
2366 memcpy(eth
, pkt_dev
->hh
, 12);
2367 *(__be16
*) & eth
[12] = protocol
;
2369 /* Eth + IPh + UDPh + mpls */
2370 datalen
= pkt_dev
->cur_pkt_size
- 14 - 20 - 8 -
2371 pkt_dev
->nr_labels
*sizeof(u32
) - VLAN_TAG_SIZE(pkt_dev
) - SVLAN_TAG_SIZE(pkt_dev
);
2372 if (datalen
< sizeof(struct pktgen_hdr
))
2373 datalen
= sizeof(struct pktgen_hdr
);
2375 udph
->source
= htons(pkt_dev
->cur_udp_src
);
2376 udph
->dest
= htons(pkt_dev
->cur_udp_dst
);
2377 udph
->len
= htons(datalen
+ 8); /* DATA + udphdr */
2378 udph
->check
= 0; /* No checksum */
2383 iph
->tos
= pkt_dev
->tos
;
2384 iph
->protocol
= IPPROTO_UDP
; /* UDP */
2385 iph
->saddr
= pkt_dev
->cur_saddr
;
2386 iph
->daddr
= pkt_dev
->cur_daddr
;
2388 iplen
= 20 + 8 + datalen
;
2389 iph
->tot_len
= htons(iplen
);
2391 iph
->check
= ip_fast_csum((void *)iph
, iph
->ihl
);
2392 skb
->protocol
= protocol
;
2393 skb
->mac_header
= (skb
->network_header
- ETH_HLEN
-
2394 pkt_dev
->nr_labels
* sizeof(u32
) -
2395 VLAN_TAG_SIZE(pkt_dev
) - SVLAN_TAG_SIZE(pkt_dev
));
2397 skb
->pkt_type
= PACKET_HOST
;
2399 if (pkt_dev
->nfrags
<= 0)
2400 pgh
= (struct pktgen_hdr
*)skb_put(skb
, datalen
);
2402 int frags
= pkt_dev
->nfrags
;
2405 pgh
= (struct pktgen_hdr
*)(((char *)(udph
)) + 8);
2407 if (frags
> MAX_SKB_FRAGS
)
2408 frags
= MAX_SKB_FRAGS
;
2409 if (datalen
> frags
* PAGE_SIZE
) {
2410 skb_put(skb
, datalen
- frags
* PAGE_SIZE
);
2411 datalen
= frags
* PAGE_SIZE
;
2415 while (datalen
> 0) {
2416 struct page
*page
= alloc_pages(GFP_KERNEL
, 0);
2417 skb_shinfo(skb
)->frags
[i
].page
= page
;
2418 skb_shinfo(skb
)->frags
[i
].page_offset
= 0;
2419 skb_shinfo(skb
)->frags
[i
].size
=
2420 (datalen
< PAGE_SIZE
? datalen
: PAGE_SIZE
);
2421 datalen
-= skb_shinfo(skb
)->frags
[i
].size
;
2422 skb
->len
+= skb_shinfo(skb
)->frags
[i
].size
;
2423 skb
->data_len
+= skb_shinfo(skb
)->frags
[i
].size
;
2425 skb_shinfo(skb
)->nr_frags
= i
;
2434 rem
= skb_shinfo(skb
)->frags
[i
- 1].size
/ 2;
2438 skb_shinfo(skb
)->frags
[i
- 1].size
-= rem
;
2440 skb_shinfo(skb
)->frags
[i
] =
2441 skb_shinfo(skb
)->frags
[i
- 1];
2442 get_page(skb_shinfo(skb
)->frags
[i
].page
);
2443 skb_shinfo(skb
)->frags
[i
].page
=
2444 skb_shinfo(skb
)->frags
[i
- 1].page
;
2445 skb_shinfo(skb
)->frags
[i
].page_offset
+=
2446 skb_shinfo(skb
)->frags
[i
- 1].size
;
2447 skb_shinfo(skb
)->frags
[i
].size
= rem
;
2449 skb_shinfo(skb
)->nr_frags
= i
;
2453 /* Stamp the time, and sequence number, convert them to network byte order */
2456 struct timeval timestamp
;
2458 pgh
->pgh_magic
= htonl(PKTGEN_MAGIC
);
2459 pgh
->seq_num
= htonl(pkt_dev
->seq_num
);
2461 do_gettimeofday(×tamp
);
2462 pgh
->tv_sec
= htonl(timestamp
.tv_sec
);
2463 pgh
->tv_usec
= htonl(timestamp
.tv_usec
);
2470 * scan_ip6, fmt_ip taken from dietlibc-0.21
2471 * Author Felix von Leitner <felix-dietlibc@fefe.de>
2473 * Slightly modified for kernel.
2474 * Should be candidate for net/ipv4/utils.c
2478 static unsigned int scan_ip6(const char *s
, char ip
[16])
2481 unsigned int len
= 0;
2484 unsigned int prefixlen
= 0;
2485 unsigned int suffixlen
= 0;
2488 for (i
= 0; i
< 16; i
++)
2494 if (s
[1] == ':') { /* Found "::", skip to part 2 */
2503 u
= simple_strtoul(s
, &tmp
, 16);
2509 if (prefixlen
== 12 && s
[i
] == '.') {
2511 /* the last 4 bytes may be written as IPv4 address */
2514 memcpy((struct in_addr
*)(ip
+ 12), &tmp
, sizeof(tmp
));
2517 ip
[prefixlen
++] = (u
>> 8);
2518 ip
[prefixlen
++] = (u
& 255);
2521 if (prefixlen
== 16)
2525 /* part 2, after "::" */
2532 } else if (suffixlen
!= 0)
2536 u
= simple_strtol(s
, &tmp
, 16);
2544 if (suffixlen
+ prefixlen
<= 12 && s
[i
] == '.') {
2546 memcpy((struct in_addr
*)(suffix
+ suffixlen
), &tmp
,
2552 suffix
[suffixlen
++] = (u
>> 8);
2553 suffix
[suffixlen
++] = (u
& 255);
2556 if (prefixlen
+ suffixlen
== 16)
2559 for (i
= 0; i
< suffixlen
; i
++)
2560 ip
[16 - suffixlen
+ i
] = suffix
[i
];
2564 static char tohex(char hexdigit
)
2566 return hexdigit
> 9 ? hexdigit
+ 'a' - 10 : hexdigit
+ '0';
2569 static int fmt_xlong(char *s
, unsigned int i
)
2572 *s
= tohex((i
>> 12) & 0xf);
2573 if (s
!= bak
|| *s
!= '0')
2575 *s
= tohex((i
>> 8) & 0xf);
2576 if (s
!= bak
|| *s
!= '0')
2578 *s
= tohex((i
>> 4) & 0xf);
2579 if (s
!= bak
|| *s
!= '0')
2581 *s
= tohex(i
& 0xf);
2585 static unsigned int fmt_ip6(char *s
, const char ip
[16])
2590 unsigned int compressing
;
2595 for (j
= 0; j
< 16; j
+= 2) {
2597 #ifdef V4MAPPEDPREFIX
2598 if (j
== 12 && !memcmp(ip
, V4mappedprefix
, 12)) {
2599 inet_ntoa_r(*(struct in_addr
*)(ip
+ 12), s
);
2604 temp
= ((unsigned long)(unsigned char)ip
[j
] << 8) +
2605 (unsigned long)(unsigned char)ip
[j
+ 1];
2620 i
= fmt_xlong(s
, temp
);
2637 static struct sk_buff
*fill_packet_ipv6(struct net_device
*odev
,
2638 struct pktgen_dev
*pkt_dev
)
2640 struct sk_buff
*skb
= NULL
;
2642 struct udphdr
*udph
;
2644 struct ipv6hdr
*iph
;
2645 struct pktgen_hdr
*pgh
= NULL
;
2646 __be16 protocol
= htons(ETH_P_IPV6
);
2648 __be16
*vlan_tci
= NULL
; /* Encapsulates priority and VLAN ID */
2649 __be16
*vlan_encapsulated_proto
= NULL
; /* packet type ID field (or len) for VLAN tag */
2650 __be16
*svlan_tci
= NULL
; /* Encapsulates priority and SVLAN ID */
2651 __be16
*svlan_encapsulated_proto
= NULL
; /* packet type ID field (or len) for SVLAN tag */
2653 if (pkt_dev
->nr_labels
)
2654 protocol
= htons(ETH_P_MPLS_UC
);
2656 if (pkt_dev
->vlan_id
!= 0xffff)
2657 protocol
= htons(ETH_P_8021Q
);
2659 /* Update any of the values, used when we're incrementing various
2662 mod_cur_headers(pkt_dev
);
2664 skb
= alloc_skb(pkt_dev
->cur_pkt_size
+ 64 + 16 +
2665 pkt_dev
->nr_labels
*sizeof(u32
) +
2666 VLAN_TAG_SIZE(pkt_dev
) + SVLAN_TAG_SIZE(pkt_dev
),
2669 sprintf(pkt_dev
->result
, "No memory");
2673 skb_reserve(skb
, 16);
2675 /* Reserve for ethernet and IP header */
2676 eth
= (__u8
*) skb_push(skb
, 14);
2677 mpls
= (__be32
*)skb_put(skb
, pkt_dev
->nr_labels
*sizeof(__u32
));
2678 if (pkt_dev
->nr_labels
)
2679 mpls_push(mpls
, pkt_dev
);
2681 if (pkt_dev
->vlan_id
!= 0xffff) {
2682 if (pkt_dev
->svlan_id
!= 0xffff) {
2683 svlan_tci
= (__be16
*)skb_put(skb
, sizeof(__be16
));
2684 *svlan_tci
= build_tci(pkt_dev
->svlan_id
,
2687 svlan_encapsulated_proto
= (__be16
*)skb_put(skb
, sizeof(__be16
));
2688 *svlan_encapsulated_proto
= htons(ETH_P_8021Q
);
2690 vlan_tci
= (__be16
*)skb_put(skb
, sizeof(__be16
));
2691 *vlan_tci
= build_tci(pkt_dev
->vlan_id
,
2694 vlan_encapsulated_proto
= (__be16
*)skb_put(skb
, sizeof(__be16
));
2695 *vlan_encapsulated_proto
= htons(ETH_P_IPV6
);
2698 skb
->network_header
= skb
->tail
;
2699 skb
->transport_header
= skb
->network_header
+ sizeof(struct ipv6hdr
);
2700 skb_put(skb
, sizeof(struct ipv6hdr
) + sizeof(struct udphdr
));
2702 iph
= ipv6_hdr(skb
);
2703 udph
= udp_hdr(skb
);
2705 memcpy(eth
, pkt_dev
->hh
, 12);
2706 *(__be16
*) & eth
[12] = protocol
;
2708 /* Eth + IPh + UDPh + mpls */
2709 datalen
= pkt_dev
->cur_pkt_size
- 14 -
2710 sizeof(struct ipv6hdr
) - sizeof(struct udphdr
) -
2711 pkt_dev
->nr_labels
*sizeof(u32
) - VLAN_TAG_SIZE(pkt_dev
) - SVLAN_TAG_SIZE(pkt_dev
);
2713 if (datalen
< sizeof(struct pktgen_hdr
)) {
2714 datalen
= sizeof(struct pktgen_hdr
);
2715 if (net_ratelimit())
2716 printk(KERN_INFO
"pktgen: increased datalen to %d\n",
2720 udph
->source
= htons(pkt_dev
->cur_udp_src
);
2721 udph
->dest
= htons(pkt_dev
->cur_udp_dst
);
2722 udph
->len
= htons(datalen
+ sizeof(struct udphdr
));
2723 udph
->check
= 0; /* No checksum */
2725 *(__be32
*) iph
= htonl(0x60000000); /* Version + flow */
2727 if (pkt_dev
->traffic_class
) {
2728 /* Version + traffic class + flow (0) */
2729 *(__be32
*)iph
|= htonl(0x60000000 | (pkt_dev
->traffic_class
<< 20));
2732 iph
->hop_limit
= 32;
2734 iph
->payload_len
= htons(sizeof(struct udphdr
) + datalen
);
2735 iph
->nexthdr
= IPPROTO_UDP
;
2737 ipv6_addr_copy(&iph
->daddr
, &pkt_dev
->cur_in6_daddr
);
2738 ipv6_addr_copy(&iph
->saddr
, &pkt_dev
->cur_in6_saddr
);
2740 skb
->mac_header
= (skb
->network_header
- ETH_HLEN
-
2741 pkt_dev
->nr_labels
* sizeof(u32
) -
2742 VLAN_TAG_SIZE(pkt_dev
) - SVLAN_TAG_SIZE(pkt_dev
));
2743 skb
->protocol
= protocol
;
2745 skb
->pkt_type
= PACKET_HOST
;
2747 if (pkt_dev
->nfrags
<= 0)
2748 pgh
= (struct pktgen_hdr
*)skb_put(skb
, datalen
);
2750 int frags
= pkt_dev
->nfrags
;
2753 pgh
= (struct pktgen_hdr
*)(((char *)(udph
)) + 8);
2755 if (frags
> MAX_SKB_FRAGS
)
2756 frags
= MAX_SKB_FRAGS
;
2757 if (datalen
> frags
* PAGE_SIZE
) {
2758 skb_put(skb
, datalen
- frags
* PAGE_SIZE
);
2759 datalen
= frags
* PAGE_SIZE
;
2763 while (datalen
> 0) {
2764 struct page
*page
= alloc_pages(GFP_KERNEL
, 0);
2765 skb_shinfo(skb
)->frags
[i
].page
= page
;
2766 skb_shinfo(skb
)->frags
[i
].page_offset
= 0;
2767 skb_shinfo(skb
)->frags
[i
].size
=
2768 (datalen
< PAGE_SIZE
? datalen
: PAGE_SIZE
);
2769 datalen
-= skb_shinfo(skb
)->frags
[i
].size
;
2770 skb
->len
+= skb_shinfo(skb
)->frags
[i
].size
;
2771 skb
->data_len
+= skb_shinfo(skb
)->frags
[i
].size
;
2773 skb_shinfo(skb
)->nr_frags
= i
;
2782 rem
= skb_shinfo(skb
)->frags
[i
- 1].size
/ 2;
2786 skb_shinfo(skb
)->frags
[i
- 1].size
-= rem
;
2788 skb_shinfo(skb
)->frags
[i
] =
2789 skb_shinfo(skb
)->frags
[i
- 1];
2790 get_page(skb_shinfo(skb
)->frags
[i
].page
);
2791 skb_shinfo(skb
)->frags
[i
].page
=
2792 skb_shinfo(skb
)->frags
[i
- 1].page
;
2793 skb_shinfo(skb
)->frags
[i
].page_offset
+=
2794 skb_shinfo(skb
)->frags
[i
- 1].size
;
2795 skb_shinfo(skb
)->frags
[i
].size
= rem
;
2797 skb_shinfo(skb
)->nr_frags
= i
;
2801 /* Stamp the time, and sequence number, convert them to network byte order */
2802 /* should we update cloned packets too ? */
2804 struct timeval timestamp
;
2806 pgh
->pgh_magic
= htonl(PKTGEN_MAGIC
);
2807 pgh
->seq_num
= htonl(pkt_dev
->seq_num
);
2809 do_gettimeofday(×tamp
);
2810 pgh
->tv_sec
= htonl(timestamp
.tv_sec
);
2811 pgh
->tv_usec
= htonl(timestamp
.tv_usec
);
2813 /* pkt_dev->seq_num++; FF: you really mean this? */
2818 static inline struct sk_buff
*fill_packet(struct net_device
*odev
,
2819 struct pktgen_dev
*pkt_dev
)
2821 if (pkt_dev
->flags
& F_IPV6
)
2822 return fill_packet_ipv6(odev
, pkt_dev
);
2824 return fill_packet_ipv4(odev
, pkt_dev
);
2827 static void pktgen_clear_counters(struct pktgen_dev
*pkt_dev
)
2829 pkt_dev
->seq_num
= 1;
2830 pkt_dev
->idle_acc
= 0;
2832 pkt_dev
->tx_bytes
= 0;
2833 pkt_dev
->errors
= 0;
2836 /* Set up structure for sending pkts, clear counters */
2838 static void pktgen_run(struct pktgen_thread
*t
)
2840 struct pktgen_dev
*pkt_dev
;
2843 pr_debug("pktgen: entering pktgen_run. %p\n", t
);
2846 list_for_each_entry(pkt_dev
, &t
->if_list
, list
) {
2849 * setup odev and create initial packet.
2851 pktgen_setup_inject(pkt_dev
);
2853 if (pkt_dev
->odev
) {
2854 pktgen_clear_counters(pkt_dev
);
2855 pkt_dev
->running
= 1; /* Cranke yeself! */
2856 pkt_dev
->skb
= NULL
;
2857 pkt_dev
->started_at
= getCurUs();
2858 pkt_dev
->next_tx_us
= getCurUs(); /* Transmit immediately */
2859 pkt_dev
->next_tx_ns
= 0;
2861 strcpy(pkt_dev
->result
, "Starting");
2864 strcpy(pkt_dev
->result
, "Error starting");
2868 t
->control
&= ~(T_STOP
);
2871 static void pktgen_stop_all_threads_ifs(void)
2873 struct pktgen_thread
*t
;
2875 pr_debug("pktgen: entering pktgen_stop_all_threads_ifs.\n");
2877 mutex_lock(&pktgen_thread_lock
);
2879 list_for_each_entry(t
, &pktgen_threads
, th_list
)
2880 t
->control
|= T_STOP
;
2882 mutex_unlock(&pktgen_thread_lock
);
2885 static int thread_is_running(struct pktgen_thread
*t
)
2887 struct pktgen_dev
*pkt_dev
;
2890 list_for_each_entry(pkt_dev
, &t
->if_list
, list
)
2891 if (pkt_dev
->running
) {
2898 static int pktgen_wait_thread_run(struct pktgen_thread
*t
)
2902 while (thread_is_running(t
)) {
2906 msleep_interruptible(100);
2908 if (signal_pending(current
))
2918 static int pktgen_wait_all_threads_run(void)
2920 struct pktgen_thread
*t
;
2923 mutex_lock(&pktgen_thread_lock
);
2925 list_for_each_entry(t
, &pktgen_threads
, th_list
) {
2926 sig
= pktgen_wait_thread_run(t
);
2932 list_for_each_entry(t
, &pktgen_threads
, th_list
)
2933 t
->control
|= (T_STOP
);
2935 mutex_unlock(&pktgen_thread_lock
);
2939 static void pktgen_run_all_threads(void)
2941 struct pktgen_thread
*t
;
2943 pr_debug("pktgen: entering pktgen_run_all_threads.\n");
2945 mutex_lock(&pktgen_thread_lock
);
2947 list_for_each_entry(t
, &pktgen_threads
, th_list
)
2948 t
->control
|= (T_RUN
);
2950 mutex_unlock(&pktgen_thread_lock
);
2952 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
2954 pktgen_wait_all_threads_run();
2957 static void show_results(struct pktgen_dev
*pkt_dev
, int nr_frags
)
2959 __u64 total_us
, bps
, mbps
, pps
, idle
;
2960 char *p
= pkt_dev
->result
;
2962 total_us
= pkt_dev
->stopped_at
- pkt_dev
->started_at
;
2964 idle
= pkt_dev
->idle_acc
;
2966 p
+= sprintf(p
, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
2967 (unsigned long long)total_us
,
2968 (unsigned long long)(total_us
- idle
),
2969 (unsigned long long)idle
,
2970 (unsigned long long)pkt_dev
->sofar
,
2971 pkt_dev
->cur_pkt_size
, nr_frags
);
2973 pps
= pkt_dev
->sofar
* USEC_PER_SEC
;
2975 while ((total_us
>> 32) != 0) {
2980 do_div(pps
, total_us
);
2982 bps
= pps
* 8 * pkt_dev
->cur_pkt_size
;
2985 do_div(mbps
, 1000000);
2986 p
+= sprintf(p
, " %llupps %lluMb/sec (%llubps) errors: %llu",
2987 (unsigned long long)pps
,
2988 (unsigned long long)mbps
,
2989 (unsigned long long)bps
,
2990 (unsigned long long)pkt_dev
->errors
);
2993 /* Set stopped-at timer, remove from running list, do counters & statistics */
2995 static int pktgen_stop_device(struct pktgen_dev
*pkt_dev
)
2997 int nr_frags
= pkt_dev
->skb
? skb_shinfo(pkt_dev
->skb
)->nr_frags
: -1;
2999 if (!pkt_dev
->running
) {
3000 printk("pktgen: interface: %s is already stopped\n",
3001 pkt_dev
->odev
->name
);
3005 pkt_dev
->stopped_at
= getCurUs();
3006 pkt_dev
->running
= 0;
3008 show_results(pkt_dev
, nr_frags
);
3013 static struct pktgen_dev
*next_to_run(struct pktgen_thread
*t
)
3015 struct pktgen_dev
*pkt_dev
, *best
= NULL
;
3019 list_for_each_entry(pkt_dev
, &t
->if_list
, list
) {
3020 if (!pkt_dev
->running
)
3024 else if (pkt_dev
->next_tx_us
< best
->next_tx_us
)
3031 static void pktgen_stop(struct pktgen_thread
*t
)
3033 struct pktgen_dev
*pkt_dev
;
3035 pr_debug("pktgen: entering pktgen_stop\n");
3039 list_for_each_entry(pkt_dev
, &t
->if_list
, list
) {
3040 pktgen_stop_device(pkt_dev
);
3042 kfree_skb(pkt_dev
->skb
);
3044 pkt_dev
->skb
= NULL
;
3051 * one of our devices needs to be removed - find it
3054 static void pktgen_rem_one_if(struct pktgen_thread
*t
)
3056 struct list_head
*q
, *n
;
3057 struct pktgen_dev
*cur
;
3059 pr_debug("pktgen: entering pktgen_rem_one_if\n");
3063 list_for_each_safe(q
, n
, &t
->if_list
) {
3064 cur
= list_entry(q
, struct pktgen_dev
, list
);
3066 if (!cur
->removal_mark
)
3070 kfree_skb(cur
->skb
);
3073 pktgen_remove_device(t
, cur
);
3081 static void pktgen_rem_all_ifs(struct pktgen_thread
*t
)
3083 struct list_head
*q
, *n
;
3084 struct pktgen_dev
*cur
;
3086 /* Remove all devices, free mem */
3088 pr_debug("pktgen: entering pktgen_rem_all_ifs\n");
3091 list_for_each_safe(q
, n
, &t
->if_list
) {
3092 cur
= list_entry(q
, struct pktgen_dev
, list
);
3095 kfree_skb(cur
->skb
);
3098 pktgen_remove_device(t
, cur
);
3104 static void pktgen_rem_thread(struct pktgen_thread
*t
)
3106 /* Remove from the thread list */
3108 remove_proc_entry(t
->tsk
->comm
, pg_proc_dir
);
3110 mutex_lock(&pktgen_thread_lock
);
3112 list_del(&t
->th_list
);
3114 mutex_unlock(&pktgen_thread_lock
);
3117 static __inline__
void pktgen_xmit(struct pktgen_dev
*pkt_dev
)
3119 struct net_device
*odev
= NULL
;
3120 __u64 idle_start
= 0;
3123 odev
= pkt_dev
->odev
;
3125 if (pkt_dev
->delay_us
|| pkt_dev
->delay_ns
) {
3129 if (now
< pkt_dev
->next_tx_us
)
3130 spin(pkt_dev
, pkt_dev
->next_tx_us
);
3132 /* This is max DELAY, this has special meaning of
3135 if (pkt_dev
->delay_us
== 0x7FFFFFFF) {
3136 pkt_dev
->next_tx_us
= getCurUs() + pkt_dev
->delay_us
;
3137 pkt_dev
->next_tx_ns
= pkt_dev
->delay_ns
;
3142 if (netif_queue_stopped(odev
) || need_resched()) {
3143 idle_start
= getCurUs();
3145 if (!netif_running(odev
)) {
3146 pktgen_stop_device(pkt_dev
);
3148 kfree_skb(pkt_dev
->skb
);
3149 pkt_dev
->skb
= NULL
;
3155 pkt_dev
->idle_acc
+= getCurUs() - idle_start
;
3157 if (netif_queue_stopped(odev
)) {
3158 pkt_dev
->next_tx_us
= getCurUs(); /* TODO */
3159 pkt_dev
->next_tx_ns
= 0;
3160 goto out
; /* Try the next interface */
3164 if (pkt_dev
->last_ok
|| !pkt_dev
->skb
) {
3165 if ((++pkt_dev
->clone_count
>= pkt_dev
->clone_skb
)
3166 || (!pkt_dev
->skb
)) {
3167 /* build a new pkt */
3169 kfree_skb(pkt_dev
->skb
);
3171 pkt_dev
->skb
= fill_packet(odev
, pkt_dev
);
3172 if (pkt_dev
->skb
== NULL
) {
3173 printk("pktgen: ERROR: couldn't allocate skb in fill_packet.\n");
3175 pkt_dev
->clone_count
--; /* back out increment, OOM */
3178 pkt_dev
->allocated_skbs
++;
3179 pkt_dev
->clone_count
= 0; /* reset counter */
3183 netif_tx_lock_bh(odev
);
3184 if (!netif_queue_stopped(odev
)) {
3186 atomic_inc(&(pkt_dev
->skb
->users
));
3188 ret
= odev
->hard_start_xmit(pkt_dev
->skb
, odev
);
3189 if (likely(ret
== NETDEV_TX_OK
)) {
3190 pkt_dev
->last_ok
= 1;
3193 pkt_dev
->tx_bytes
+= pkt_dev
->cur_pkt_size
;
3195 } else if (ret
== NETDEV_TX_LOCKED
3196 && (odev
->features
& NETIF_F_LLTX
)) {
3199 } else { /* Retry it next time */
3201 atomic_dec(&(pkt_dev
->skb
->users
));
3203 if (debug
&& net_ratelimit())
3204 printk(KERN_INFO
"pktgen: Hard xmit error\n");
3207 pkt_dev
->last_ok
= 0;
3210 pkt_dev
->next_tx_us
= getCurUs();
3211 pkt_dev
->next_tx_ns
= 0;
3213 pkt_dev
->next_tx_us
+= pkt_dev
->delay_us
;
3214 pkt_dev
->next_tx_ns
+= pkt_dev
->delay_ns
;
3216 if (pkt_dev
->next_tx_ns
> 1000) {
3217 pkt_dev
->next_tx_us
++;
3218 pkt_dev
->next_tx_ns
-= 1000;
3222 else { /* Retry it next time */
3223 pkt_dev
->last_ok
= 0;
3224 pkt_dev
->next_tx_us
= getCurUs(); /* TODO */
3225 pkt_dev
->next_tx_ns
= 0;
3228 netif_tx_unlock_bh(odev
);
3230 /* If pkt_dev->count is zero, then run forever */
3231 if ((pkt_dev
->count
!= 0) && (pkt_dev
->sofar
>= pkt_dev
->count
)) {
3232 if (atomic_read(&(pkt_dev
->skb
->users
)) != 1) {
3233 idle_start
= getCurUs();
3234 while (atomic_read(&(pkt_dev
->skb
->users
)) != 1) {
3235 if (signal_pending(current
)) {
3240 pkt_dev
->idle_acc
+= getCurUs() - idle_start
;
3243 /* Done with this */
3244 pktgen_stop_device(pkt_dev
);
3246 kfree_skb(pkt_dev
->skb
);
3247 pkt_dev
->skb
= NULL
;
3253 * Main loop of the thread goes here
3256 static int pktgen_thread_worker(void *arg
)
3259 struct pktgen_thread
*t
= arg
;
3260 struct pktgen_dev
*pkt_dev
= NULL
;
3262 u32 max_before_softirq
;
3263 u32 tx_since_softirq
= 0;
3265 BUG_ON(smp_processor_id() != cpu
);
3267 init_waitqueue_head(&t
->queue
);
3269 t
->pid
= current
->pid
;
3271 pr_debug("pktgen: starting pktgen/%d: pid=%d\n", cpu
, current
->pid
);
3273 max_before_softirq
= t
->max_before_softirq
;
3275 set_current_state(TASK_INTERRUPTIBLE
);
3277 while (!kthread_should_stop()) {
3278 pkt_dev
= next_to_run(t
);
3281 (t
->control
& (T_STOP
| T_RUN
| T_REMDEVALL
| T_REMDEV
))
3283 prepare_to_wait(&(t
->queue
), &wait
,
3284 TASK_INTERRUPTIBLE
);
3285 schedule_timeout(HZ
/ 10);
3286 finish_wait(&(t
->queue
), &wait
);
3289 __set_current_state(TASK_RUNNING
);
3293 pktgen_xmit(pkt_dev
);
3296 * We like to stay RUNNING but must also give
3297 * others fair share.
3300 tx_since_softirq
+= pkt_dev
->last_ok
;
3302 if (tx_since_softirq
> max_before_softirq
) {
3303 if (local_softirq_pending())
3305 tx_since_softirq
= 0;
3309 if (t
->control
& T_STOP
) {
3311 t
->control
&= ~(T_STOP
);
3314 if (t
->control
& T_RUN
) {
3316 t
->control
&= ~(T_RUN
);
3319 if (t
->control
& T_REMDEVALL
) {
3320 pktgen_rem_all_ifs(t
);
3321 t
->control
&= ~(T_REMDEVALL
);
3324 if (t
->control
& T_REMDEV
) {
3325 pktgen_rem_one_if(t
);
3326 t
->control
&= ~(T_REMDEV
);
3331 set_current_state(TASK_INTERRUPTIBLE
);
3334 pr_debug("pktgen: %s stopping all device\n", t
->tsk
->comm
);
3337 pr_debug("pktgen: %s removing all device\n", t
->tsk
->comm
);
3338 pktgen_rem_all_ifs(t
);
3340 pr_debug("pktgen: %s removing thread.\n", t
->tsk
->comm
);
3341 pktgen_rem_thread(t
);
3346 static struct pktgen_dev
*pktgen_find_dev(struct pktgen_thread
*t
,
3349 struct pktgen_dev
*p
, *pkt_dev
= NULL
;
3352 list_for_each_entry(p
, &t
->if_list
, list
)
3353 if (strncmp(p
->odev
->name
, ifname
, IFNAMSIZ
) == 0) {
3359 pr_debug("pktgen: find_dev(%s) returning %p\n", ifname
, pkt_dev
);
3364 * Adds a dev at front of if_list.
3367 static int add_dev_to_thread(struct pktgen_thread
*t
,
3368 struct pktgen_dev
*pkt_dev
)
3374 if (pkt_dev
->pg_thread
) {
3375 printk("pktgen: ERROR: already assigned to a thread.\n");
3380 list_add(&pkt_dev
->list
, &t
->if_list
);
3381 pkt_dev
->pg_thread
= t
;
3382 pkt_dev
->running
= 0;
3389 /* Called under thread lock */
3391 static int pktgen_add_device(struct pktgen_thread
*t
, const char *ifname
)
3393 struct pktgen_dev
*pkt_dev
;
3396 /* We don't allow a device to be on several threads */
3398 pkt_dev
= __pktgen_NN_threads(ifname
, FIND
);
3400 printk("pktgen: ERROR: interface already used.\n");
3404 pkt_dev
= kzalloc(sizeof(struct pktgen_dev
), GFP_KERNEL
);
3408 pkt_dev
->flows
= vmalloc(MAX_CFLOWS
* sizeof(struct flow_state
));
3409 if (pkt_dev
->flows
== NULL
) {
3413 memset(pkt_dev
->flows
, 0, MAX_CFLOWS
* sizeof(struct flow_state
));
3415 pkt_dev
->removal_mark
= 0;
3416 pkt_dev
->min_pkt_size
= ETH_ZLEN
;
3417 pkt_dev
->max_pkt_size
= ETH_ZLEN
;
3418 pkt_dev
->nfrags
= 0;
3419 pkt_dev
->clone_skb
= pg_clone_skb_d
;
3420 pkt_dev
->delay_us
= pg_delay_d
/ 1000;
3421 pkt_dev
->delay_ns
= pg_delay_d
% 1000;
3422 pkt_dev
->count
= pg_count_d
;
3424 pkt_dev
->udp_src_min
= 9; /* sink port */
3425 pkt_dev
->udp_src_max
= 9;
3426 pkt_dev
->udp_dst_min
= 9;
3427 pkt_dev
->udp_dst_max
= 9;
3429 pkt_dev
->vlan_p
= 0;
3430 pkt_dev
->vlan_cfi
= 0;
3431 pkt_dev
->vlan_id
= 0xffff;
3432 pkt_dev
->svlan_p
= 0;
3433 pkt_dev
->svlan_cfi
= 0;
3434 pkt_dev
->svlan_id
= 0xffff;
3436 err
= pktgen_setup_dev(pkt_dev
, ifname
);
3440 pkt_dev
->entry
= create_proc_entry(ifname
, 0600, pg_proc_dir
);
3441 if (!pkt_dev
->entry
) {
3442 printk("pktgen: cannot create %s/%s procfs entry.\n",
3443 PG_PROC_DIR
, ifname
);
3447 pkt_dev
->entry
->proc_fops
= &pktgen_if_fops
;
3448 pkt_dev
->entry
->data
= pkt_dev
;
3450 return add_dev_to_thread(t
, pkt_dev
);
3452 dev_put(pkt_dev
->odev
);
3455 vfree(pkt_dev
->flows
);
3460 static int __init
pktgen_create_thread(int cpu
)
3462 struct pktgen_thread
*t
;
3463 struct proc_dir_entry
*pe
;
3464 struct task_struct
*p
;
3466 t
= kzalloc(sizeof(struct pktgen_thread
), GFP_KERNEL
);
3468 printk("pktgen: ERROR: out of memory, can't create new thread.\n");
3472 spin_lock_init(&t
->if_lock
);
3475 INIT_LIST_HEAD(&t
->if_list
);
3477 list_add_tail(&t
->th_list
, &pktgen_threads
);
3479 p
= kthread_create(pktgen_thread_worker
, t
, "kpktgend_%d", cpu
);
3481 printk("pktgen: kernel_thread() failed for cpu %d\n", t
->cpu
);
3482 list_del(&t
->th_list
);
3486 kthread_bind(p
, cpu
);
3489 pe
= create_proc_entry(t
->tsk
->comm
, 0600, pg_proc_dir
);
3491 printk("pktgen: cannot create %s/%s procfs entry.\n",
3492 PG_PROC_DIR
, t
->tsk
->comm
);
3494 list_del(&t
->th_list
);
3499 pe
->proc_fops
= &pktgen_thread_fops
;
3508 * Removes a device from the thread if_list.
3510 static void _rem_dev_from_if_list(struct pktgen_thread
*t
,
3511 struct pktgen_dev
*pkt_dev
)
3513 struct list_head
*q
, *n
;
3514 struct pktgen_dev
*p
;
3516 list_for_each_safe(q
, n
, &t
->if_list
) {
3517 p
= list_entry(q
, struct pktgen_dev
, list
);
3523 static int pktgen_remove_device(struct pktgen_thread
*t
,
3524 struct pktgen_dev
*pkt_dev
)
3527 pr_debug("pktgen: remove_device pkt_dev=%p\n", pkt_dev
);
3529 if (pkt_dev
->running
) {
3530 printk("pktgen:WARNING: trying to remove a running interface, stopping it now.\n");
3531 pktgen_stop_device(pkt_dev
);
3534 /* Dis-associate from the interface */
3536 if (pkt_dev
->odev
) {
3537 dev_put(pkt_dev
->odev
);
3538 pkt_dev
->odev
= NULL
;
3541 /* And update the thread if_list */
3543 _rem_dev_from_if_list(t
, pkt_dev
);
3546 remove_proc_entry(pkt_dev
->entry
->name
, pg_proc_dir
);
3549 vfree(pkt_dev
->flows
);
3554 static int __init
pg_init(void)
3557 struct proc_dir_entry
*pe
;
3561 pg_proc_dir
= proc_mkdir(PG_PROC_DIR
, proc_net
);
3564 pg_proc_dir
->owner
= THIS_MODULE
;
3566 pe
= create_proc_entry(PGCTRL
, 0600, pg_proc_dir
);
3568 printk("pktgen: ERROR: cannot create %s procfs entry.\n",
3570 proc_net_remove(PG_PROC_DIR
);
3574 pe
->proc_fops
= &pktgen_fops
;
3577 /* Register us to receive netdevice events */
3578 register_netdevice_notifier(&pktgen_notifier_block
);
3580 for_each_online_cpu(cpu
) {
3583 err
= pktgen_create_thread(cpu
);
3585 printk("pktgen: WARNING: Cannot create thread for cpu %d (%d)\n",
3589 if (list_empty(&pktgen_threads
)) {
3590 printk("pktgen: ERROR: Initialization failed for all threads\n");
3591 unregister_netdevice_notifier(&pktgen_notifier_block
);
3592 remove_proc_entry(PGCTRL
, pg_proc_dir
);
3593 proc_net_remove(PG_PROC_DIR
);
3600 static void __exit
pg_cleanup(void)
3602 struct pktgen_thread
*t
;
3603 struct list_head
*q
, *n
;
3604 wait_queue_head_t queue
;
3605 init_waitqueue_head(&queue
);
3607 /* Stop all interfaces & threads */
3609 list_for_each_safe(q
, n
, &pktgen_threads
) {
3610 t
= list_entry(q
, struct pktgen_thread
, th_list
);
3611 kthread_stop(t
->tsk
);
3615 /* Un-register us from receiving netdevice events */
3616 unregister_netdevice_notifier(&pktgen_notifier_block
);
3618 /* Clean up proc file system */
3619 remove_proc_entry(PGCTRL
, pg_proc_dir
);
3620 proc_net_remove(PG_PROC_DIR
);
3623 module_init(pg_init
);
3624 module_exit(pg_cleanup
);
3626 MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se");
3627 MODULE_DESCRIPTION("Packet Generator tool");
3628 MODULE_LICENSE("GPL");
3629 module_param(pg_count_d
, int, 0);
3630 module_param(pg_delay_d
, int, 0);
3631 module_param(pg_clone_skb_d
, int, 0);
3632 module_param(debug
, int, 0);