3 * Copyright 2001, 2002 by Robert Olsson <robert.olsson@its.uu.se>
4 * Uppsala University and
5 * Swedish University of Agricultural Sciences
7 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
8 * Ben Greear <greearb@candelatech.com>
9 * Jens Låås <jens.laas@data.slu.se>
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version
14 * 2 of the License, or (at your option) any later version.
17 * A tool for loading the network with preconfigurated packets.
18 * The tool is implemented as a linux module. Parameters are output
19 * device, delay (to hard_xmit), number of packets, and whether
20 * to use multiple SKBs or just the same one.
21 * pktgen uses the installed interface's output routine.
23 * Additional hacking by:
25 * Jens.Laas@data.slu.se
26 * Improved by ANK. 010120.
27 * Improved by ANK even more. 010212.
28 * MAC address typo fixed. 010417 --ro
29 * Integrated. 020301 --DaveM
30 * Added multiskb option 020301 --DaveM
31 * Scaling of results. 020417--sigurdur@linpro.no
32 * Significant re-work of the module:
33 * * Convert to threaded model to more efficiently be able to transmit
34 * and receive on multiple interfaces at once.
35 * * Converted many counters to __u64 to allow longer runs.
36 * * Allow configuration of ranges, like min/max IP address, MACs,
37 * and UDP-ports, for both source and destination, and can
38 * set to use a random distribution or sequentially walk the range.
39 * * Can now change most values after starting.
40 * * Place 12-byte packet in UDP payload with magic number,
41 * sequence number, and timestamp.
42 * * Add receiver code that detects dropped pkts, re-ordered pkts, and
43 * latencies (with micro-second) precision.
44 * * Add IOCTL interface to easily get counters & configuration.
45 * --Ben Greear <greearb@candelatech.com>
47 * Renamed multiskb to clone_skb and cleaned up sending core for two distinct
48 * skb modes. A clone_skb=0 mode for Ben "ranges" work and a clone_skb != 0
49 * as a "fastpath" with a configurable number of clones after alloc's.
50 * clone_skb=0 means all packets are allocated this also means ranges time
51 * stamps etc can be used. clone_skb=100 means 1 malloc is followed by 100
54 * Also moved to /proc/net/pktgen/
57 * Sept 10: Fixed threading/locking. Lots of bone-headed and more clever
58 * mistakes. Also merged in DaveM's patch in the -pre6 patch.
59 * --Ben Greear <greearb@candelatech.com>
61 * Integrated to 2.5.x 021029 --Lucio Maciel (luciomaciel@zipmail.com.br)
64 * 021124 Finished major redesign and rewrite for new functionality.
65 * See Documentation/networking/pktgen.txt for how to use this.
68 * For each CPU one thread/process is created at start. This process checks
69 * for running devices in the if_list and sends packets until count is 0 it
70 * also the thread checks the thread->control which is used for inter-process
71 * communication. controlling process "posts" operations to the threads this
72 * way. The if_lock should be possible to remove when add/rem_device is merged
75 * By design there should only be *one* "controlling" process. In practice
76 * multiple write accesses gives unpredictable result. Understood by "write"
77 * to /proc gives result code thats should be read be the "writer".
78 * For practical use this should be no problem.
80 * Note when adding devices to a specific CPU there good idea to also assign
81 * /proc/irq/XX/smp_affinity so TX-interrupts gets bound to the same CPU.
84 * Fix refcount off by one if first packet fails, potential null deref,
87 * First "ranges" functionality for ipv6 030726 --ro
89 * Included flow support. 030802 ANK.
91 * Fixed unaligned access on IA-64 Grant Grundler <grundler@parisc-linux.org>
93 * Remove if fix from added Harald Welte <laforge@netfilter.org> 040419
94 * ia64 compilation fix from Aron Griffis <aron@hp.com> 040604
96 * New xmit() return, do_div and misc clean up by Stephen Hemminger
97 * <shemminger@osdl.org> 040923
99 * Randy Dunlap fixed u64 printk compiler waring
101 * Remove FCS from BW calculation. Lennert Buytenhek <buytenh@wantstofly.org>
102 * New time handling. Lennert Buytenhek <buytenh@wantstofly.org> 041213
104 * Corrections from Nikolai Malykh (nmalykh@bilim.com)
105 * Removed unused flags F_SET_SRCMAC & F_SET_SRCIP 041230
107 * interruptible_sleep_on_timeout() replaced Nishanth Aravamudan <nacc@us.ibm.com>
110 * MPLS support by Steven Whitehouse <steve@chygwyn.com>
112 * 802.1Q/Q-in-Q support by Francesco Fondelli (FF) <francesco.fondelli@gmail.com>
114 * Fixed src_mac command to set source mac of packet to value specified in
115 * command by Adit Ranadive <adit.262@gmail.com>
118 #include <linux/sys.h>
119 #include <linux/types.h>
120 #include <linux/module.h>
121 #include <linux/moduleparam.h>
122 #include <linux/kernel.h>
123 #include <linux/mutex.h>
124 #include <linux/sched.h>
125 #include <linux/slab.h>
126 #include <linux/vmalloc.h>
127 #include <linux/unistd.h>
128 #include <linux/string.h>
129 #include <linux/ptrace.h>
130 #include <linux/errno.h>
131 #include <linux/ioport.h>
132 #include <linux/interrupt.h>
133 #include <linux/capability.h>
134 #include <linux/hrtimer.h>
135 #include <linux/freezer.h>
136 #include <linux/delay.h>
137 #include <linux/timer.h>
138 #include <linux/list.h>
139 #include <linux/init.h>
140 #include <linux/skbuff.h>
141 #include <linux/netdevice.h>
142 #include <linux/inet.h>
143 #include <linux/inetdevice.h>
144 #include <linux/rtnetlink.h>
145 #include <linux/if_arp.h>
146 #include <linux/if_vlan.h>
147 #include <linux/in.h>
148 #include <linux/ip.h>
149 #include <linux/ipv6.h>
150 #include <linux/udp.h>
151 #include <linux/proc_fs.h>
152 #include <linux/seq_file.h>
153 #include <linux/wait.h>
154 #include <linux/etherdevice.h>
155 #include <linux/kthread.h>
156 #include <net/net_namespace.h>
157 #include <net/checksum.h>
158 #include <net/ipv6.h>
159 #include <net/addrconf.h>
161 #include <net/xfrm.h>
163 #include <asm/byteorder.h>
164 #include <linux/rcupdate.h>
165 #include <linux/bitops.h>
166 #include <linux/io.h>
167 #include <linux/timex.h>
168 #include <linux/uaccess.h>
170 #include <asm/div64.h> /* do_div */
172 #define VERSION "2.73"
173 #define IP_NAME_SZ 32
174 #define MAX_MPLS_LABELS 16 /* This is the max label stack depth */
175 #define MPLS_STACK_BOTTOM htonl(0x00000100)
177 /* Device flag bits */
178 #define F_IPSRC_RND (1<<0) /* IP-Src Random */
179 #define F_IPDST_RND (1<<1) /* IP-Dst Random */
180 #define F_UDPSRC_RND (1<<2) /* UDP-Src Random */
181 #define F_UDPDST_RND (1<<3) /* UDP-Dst Random */
182 #define F_MACSRC_RND (1<<4) /* MAC-Src Random */
183 #define F_MACDST_RND (1<<5) /* MAC-Dst Random */
184 #define F_TXSIZE_RND (1<<6) /* Transmit size is random */
185 #define F_IPV6 (1<<7) /* Interface in IPV6 Mode */
186 #define F_MPLS_RND (1<<8) /* Random MPLS labels */
187 #define F_VID_RND (1<<9) /* Random VLAN ID */
188 #define F_SVID_RND (1<<10) /* Random SVLAN ID */
189 #define F_FLOW_SEQ (1<<11) /* Sequential flows */
190 #define F_IPSEC_ON (1<<12) /* ipsec on for flows */
191 #define F_QUEUE_MAP_RND (1<<13) /* queue map Random */
192 #define F_QUEUE_MAP_CPU (1<<14) /* queue map mirrors smp_processor_id() */
193 #define F_NODE (1<<15) /* Node memory alloc*/
195 /* Thread control flag bits */
196 #define T_STOP (1<<0) /* Stop run */
197 #define T_RUN (1<<1) /* Start run */
198 #define T_REMDEVALL (1<<2) /* Remove all devs */
199 #define T_REMDEV (1<<3) /* Remove one dev */
201 /* If lock -- can be removed after some work */
202 #define if_lock(t) spin_lock(&(t->if_lock));
203 #define if_unlock(t) spin_unlock(&(t->if_lock));
205 /* Used to help with determining the pkts on receive */
206 #define PKTGEN_MAGIC 0xbe9be955
207 #define PG_PROC_DIR "pktgen"
208 #define PGCTRL "pgctrl"
209 static struct proc_dir_entry
*pg_proc_dir
;
211 #define MAX_CFLOWS 65536
213 #define VLAN_TAG_SIZE(x) ((x)->vlan_id == 0xffff ? 0 : 4)
214 #define SVLAN_TAG_SIZE(x) ((x)->svlan_id == 0xffff ? 0 : 4)
220 struct xfrm_state
*x
;
226 #define F_INIT (1<<0) /* flow has been initialized */
230 * Try to keep frequent/infrequent used vars. separated.
232 struct proc_dir_entry
*entry
; /* proc file */
233 struct pktgen_thread
*pg_thread
;/* the owner */
234 struct list_head list
; /* chaining in the thread's run-queue */
236 int running
; /* if false, the test will stop */
238 /* If min != max, then we will either do a linear iteration, or
239 * we will do a random selection from within the range.
242 int removal_mark
; /* non-zero => the device is marked for
243 * removal by worker thread */
245 int min_pkt_size
; /* = ETH_ZLEN; */
246 int max_pkt_size
; /* = ETH_ZLEN; */
247 int pkt_overhead
; /* overhead for MPLS, VLANs, IPSEC etc */
249 u64 delay
; /* nano-seconds */
251 __u64 count
; /* Default No packets to send */
252 __u64 sofar
; /* How many pkts we've sent so far */
253 __u64 tx_bytes
; /* How many bytes we've transmitted */
254 __u64 errors
; /* Errors when trying to transmit, */
256 /* runtime counters relating to clone_skb */
258 __u64 allocated_skbs
;
260 int last_ok
; /* Was last skb sent?
261 * Or a failed transmit of some sort?
262 * This will keep sequence numbers in order
267 u64 idle_acc
; /* nano-seconds */
272 * Use multiple SKBs during packet gen.
273 * If this number is greater than 1, then
274 * that many copies of the same packet will be
275 * sent before a new packet is allocated.
276 * If you want to send 1024 identical packets
277 * before creating a new packet,
278 * set clone_skb to 1024.
281 char dst_min
[IP_NAME_SZ
]; /* IP, ie 1.2.3.4 */
282 char dst_max
[IP_NAME_SZ
]; /* IP, ie 1.2.3.4 */
283 char src_min
[IP_NAME_SZ
]; /* IP, ie 1.2.3.4 */
284 char src_max
[IP_NAME_SZ
]; /* IP, ie 1.2.3.4 */
286 struct in6_addr in6_saddr
;
287 struct in6_addr in6_daddr
;
288 struct in6_addr cur_in6_daddr
;
289 struct in6_addr cur_in6_saddr
;
291 struct in6_addr min_in6_daddr
;
292 struct in6_addr max_in6_daddr
;
293 struct in6_addr min_in6_saddr
;
294 struct in6_addr max_in6_saddr
;
296 /* If we're doing ranges, random or incremental, then this
297 * defines the min/max for those ranges.
299 __be32 saddr_min
; /* inclusive, source IP address */
300 __be32 saddr_max
; /* exclusive, source IP address */
301 __be32 daddr_min
; /* inclusive, dest IP address */
302 __be32 daddr_max
; /* exclusive, dest IP address */
304 __u16 udp_src_min
; /* inclusive, source UDP port */
305 __u16 udp_src_max
; /* exclusive, source UDP port */
306 __u16 udp_dst_min
; /* inclusive, dest UDP port */
307 __u16 udp_dst_max
; /* exclusive, dest UDP port */
310 __u8 tos
; /* six MSB of (former) IPv4 TOS
311 are for dscp codepoint */
312 __u8 traffic_class
; /* ditto for the (former) Traffic Class in IPv6
313 (see RFC 3260, sec. 4) */
316 unsigned nr_labels
; /* Depth of stack, 0 = no MPLS */
317 __be32 labels
[MAX_MPLS_LABELS
];
319 /* VLAN/SVLAN (802.1Q/Q-in-Q) */
322 __u16 vlan_id
; /* 0xffff means no vlan tag */
326 __u16 svlan_id
; /* 0xffff means no svlan tag */
328 __u32 src_mac_count
; /* How many MACs to iterate through */
329 __u32 dst_mac_count
; /* How many MACs to iterate through */
331 unsigned char dst_mac
[ETH_ALEN
];
332 unsigned char src_mac
[ETH_ALEN
];
334 __u32 cur_dst_mac_offset
;
335 __u32 cur_src_mac_offset
;
347 0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
349 We fill in SRC address later
350 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
354 __u16 pad
; /* pad out the hh struct to an even 16 bytes */
356 struct sk_buff
*skb
; /* skb we are to transmit next, used for when we
357 * are transmitting the same one multiple times
359 struct net_device
*odev
; /* The out-going device.
360 * Note that the device should have it's
361 * pg_info pointer pointing back to this
363 * Set when the user specifies the out-going
364 * device name (not when the inject is
365 * started as it used to do.)
368 struct flow_state
*flows
;
369 unsigned cflows
; /* Concurrent flows (config) */
370 unsigned lflow
; /* Flow length (config) */
371 unsigned nflows
; /* accumulated flows (stats) */
372 unsigned curfl
; /* current sequenced flow (state)*/
376 int node
; /* Memory node */
379 __u8 ipsmode
; /* IPSEC mode (config) */
380 __u8 ipsproto
; /* IPSEC type (config) */
392 struct pktgen_thread
{
393 spinlock_t if_lock
; /* for list of devices */
394 struct list_head if_list
; /* All device here */
395 struct list_head th_list
;
396 struct task_struct
*tsk
;
399 /* Field for thread to receive "posted" events terminate,
405 wait_queue_head_t queue
;
406 struct completion start_done
;
412 static inline ktime_t
ktime_now(void)
417 return timespec_to_ktime(ts
);
420 /* This works even if 32 bit because of careful byte order choice */
421 static inline int ktime_lt(const ktime_t cmp1
, const ktime_t cmp2
)
423 return cmp1
.tv64
< cmp2
.tv64
;
426 static const char version
[] =
427 "pktgen " VERSION
": Packet Generator for packet performance testing.\n";
429 static int pktgen_remove_device(struct pktgen_thread
*t
, struct pktgen_dev
*i
);
430 static int pktgen_add_device(struct pktgen_thread
*t
, const char *ifname
);
431 static struct pktgen_dev
*pktgen_find_dev(struct pktgen_thread
*t
,
432 const char *ifname
, bool exact
);
433 static int pktgen_device_event(struct notifier_block
*, unsigned long, void *);
434 static void pktgen_run_all_threads(void);
435 static void pktgen_reset_all_threads(void);
436 static void pktgen_stop_all_threads_ifs(void);
438 static void pktgen_stop(struct pktgen_thread
*t
);
439 static void pktgen_clear_counters(struct pktgen_dev
*pkt_dev
);
441 static unsigned int scan_ip6(const char *s
, char ip
[16]);
442 static unsigned int fmt_ip6(char *s
, const char ip
[16]);
444 /* Module parameters, defaults. */
445 static int pg_count_d __read_mostly
= 1000;
446 static int pg_delay_d __read_mostly
;
447 static int pg_clone_skb_d __read_mostly
;
448 static int debug __read_mostly
;
450 static DEFINE_MUTEX(pktgen_thread_lock
);
451 static LIST_HEAD(pktgen_threads
);
453 static struct notifier_block pktgen_notifier_block
= {
454 .notifier_call
= pktgen_device_event
,
458 * /proc handling functions
462 static int pgctrl_show(struct seq_file
*seq
, void *v
)
464 seq_puts(seq
, version
);
468 static ssize_t
pgctrl_write(struct file
*file
, const char __user
*buf
,
469 size_t count
, loff_t
*ppos
)
474 if (!capable(CAP_NET_ADMIN
)) {
479 if (count
> sizeof(data
))
480 count
= sizeof(data
);
482 if (copy_from_user(data
, buf
, count
)) {
486 data
[count
- 1] = 0; /* Make string */
488 if (!strcmp(data
, "stop"))
489 pktgen_stop_all_threads_ifs();
491 else if (!strcmp(data
, "start"))
492 pktgen_run_all_threads();
494 else if (!strcmp(data
, "reset"))
495 pktgen_reset_all_threads();
498 printk(KERN_WARNING
"pktgen: Unknown command: %s\n", data
);
506 static int pgctrl_open(struct inode
*inode
, struct file
*file
)
508 return single_open(file
, pgctrl_show
, PDE(inode
)->data
);
511 static const struct file_operations pktgen_fops
= {
512 .owner
= THIS_MODULE
,
516 .write
= pgctrl_write
,
517 .release
= single_release
,
520 static int pktgen_if_show(struct seq_file
*seq
, void *v
)
522 const struct pktgen_dev
*pkt_dev
= seq
->private;
527 "Params: count %llu min_pkt_size: %u max_pkt_size: %u\n",
528 (unsigned long long)pkt_dev
->count
, pkt_dev
->min_pkt_size
,
529 pkt_dev
->max_pkt_size
);
532 " frags: %d delay: %llu clone_skb: %d ifname: %s\n",
533 pkt_dev
->nfrags
, (unsigned long long) pkt_dev
->delay
,
534 pkt_dev
->clone_skb
, pkt_dev
->odevname
);
536 seq_printf(seq
, " flows: %u flowlen: %u\n", pkt_dev
->cflows
,
540 " queue_map_min: %u queue_map_max: %u\n",
541 pkt_dev
->queue_map_min
,
542 pkt_dev
->queue_map_max
);
544 if (pkt_dev
->flags
& F_IPV6
) {
545 char b1
[128], b2
[128], b3
[128];
546 fmt_ip6(b1
, pkt_dev
->in6_saddr
.s6_addr
);
547 fmt_ip6(b2
, pkt_dev
->min_in6_saddr
.s6_addr
);
548 fmt_ip6(b3
, pkt_dev
->max_in6_saddr
.s6_addr
);
550 " saddr: %s min_saddr: %s max_saddr: %s\n", b1
,
553 fmt_ip6(b1
, pkt_dev
->in6_daddr
.s6_addr
);
554 fmt_ip6(b2
, pkt_dev
->min_in6_daddr
.s6_addr
);
555 fmt_ip6(b3
, pkt_dev
->max_in6_daddr
.s6_addr
);
557 " daddr: %s min_daddr: %s max_daddr: %s\n", b1
,
562 " dst_min: %s dst_max: %s\n",
563 pkt_dev
->dst_min
, pkt_dev
->dst_max
);
565 " src_min: %s src_max: %s\n",
566 pkt_dev
->src_min
, pkt_dev
->src_max
);
569 seq_puts(seq
, " src_mac: ");
571 seq_printf(seq
, "%pM ",
572 is_zero_ether_addr(pkt_dev
->src_mac
) ?
573 pkt_dev
->odev
->dev_addr
: pkt_dev
->src_mac
);
575 seq_printf(seq
, "dst_mac: ");
576 seq_printf(seq
, "%pM\n", pkt_dev
->dst_mac
);
579 " udp_src_min: %d udp_src_max: %d"
580 " udp_dst_min: %d udp_dst_max: %d\n",
581 pkt_dev
->udp_src_min
, pkt_dev
->udp_src_max
,
582 pkt_dev
->udp_dst_min
, pkt_dev
->udp_dst_max
);
585 " src_mac_count: %d dst_mac_count: %d\n",
586 pkt_dev
->src_mac_count
, pkt_dev
->dst_mac_count
);
588 if (pkt_dev
->nr_labels
) {
590 seq_printf(seq
, " mpls: ");
591 for (i
= 0; i
< pkt_dev
->nr_labels
; i
++)
592 seq_printf(seq
, "%08x%s", ntohl(pkt_dev
->labels
[i
]),
593 i
== pkt_dev
->nr_labels
-1 ? "\n" : ", ");
596 if (pkt_dev
->vlan_id
!= 0xffff)
597 seq_printf(seq
, " vlan_id: %u vlan_p: %u vlan_cfi: %u\n",
598 pkt_dev
->vlan_id
, pkt_dev
->vlan_p
,
601 if (pkt_dev
->svlan_id
!= 0xffff)
602 seq_printf(seq
, " svlan_id: %u vlan_p: %u vlan_cfi: %u\n",
603 pkt_dev
->svlan_id
, pkt_dev
->svlan_p
,
607 seq_printf(seq
, " tos: 0x%02x\n", pkt_dev
->tos
);
609 if (pkt_dev
->traffic_class
)
610 seq_printf(seq
, " traffic_class: 0x%02x\n", pkt_dev
->traffic_class
);
612 if (pkt_dev
->node
>= 0)
613 seq_printf(seq
, " node: %d\n", pkt_dev
->node
);
615 seq_printf(seq
, " Flags: ");
617 if (pkt_dev
->flags
& F_IPV6
)
618 seq_printf(seq
, "IPV6 ");
620 if (pkt_dev
->flags
& F_IPSRC_RND
)
621 seq_printf(seq
, "IPSRC_RND ");
623 if (pkt_dev
->flags
& F_IPDST_RND
)
624 seq_printf(seq
, "IPDST_RND ");
626 if (pkt_dev
->flags
& F_TXSIZE_RND
)
627 seq_printf(seq
, "TXSIZE_RND ");
629 if (pkt_dev
->flags
& F_UDPSRC_RND
)
630 seq_printf(seq
, "UDPSRC_RND ");
632 if (pkt_dev
->flags
& F_UDPDST_RND
)
633 seq_printf(seq
, "UDPDST_RND ");
635 if (pkt_dev
->flags
& F_MPLS_RND
)
636 seq_printf(seq
, "MPLS_RND ");
638 if (pkt_dev
->flags
& F_QUEUE_MAP_RND
)
639 seq_printf(seq
, "QUEUE_MAP_RND ");
641 if (pkt_dev
->flags
& F_QUEUE_MAP_CPU
)
642 seq_printf(seq
, "QUEUE_MAP_CPU ");
644 if (pkt_dev
->cflows
) {
645 if (pkt_dev
->flags
& F_FLOW_SEQ
)
646 seq_printf(seq
, "FLOW_SEQ "); /*in sequence flows*/
648 seq_printf(seq
, "FLOW_RND ");
652 if (pkt_dev
->flags
& F_IPSEC_ON
)
653 seq_printf(seq
, "IPSEC ");
656 if (pkt_dev
->flags
& F_MACSRC_RND
)
657 seq_printf(seq
, "MACSRC_RND ");
659 if (pkt_dev
->flags
& F_MACDST_RND
)
660 seq_printf(seq
, "MACDST_RND ");
662 if (pkt_dev
->flags
& F_VID_RND
)
663 seq_printf(seq
, "VID_RND ");
665 if (pkt_dev
->flags
& F_SVID_RND
)
666 seq_printf(seq
, "SVID_RND ");
668 if (pkt_dev
->flags
& F_NODE
)
669 seq_printf(seq
, "NODE_ALLOC ");
673 /* not really stopped, more like last-running-at */
674 stopped
= pkt_dev
->running
? ktime_now() : pkt_dev
->stopped_at
;
675 idle
= pkt_dev
->idle_acc
;
676 do_div(idle
, NSEC_PER_USEC
);
679 "Current:\n pkts-sofar: %llu errors: %llu\n",
680 (unsigned long long)pkt_dev
->sofar
,
681 (unsigned long long)pkt_dev
->errors
);
684 " started: %lluus stopped: %lluus idle: %lluus\n",
685 (unsigned long long) ktime_to_us(pkt_dev
->started_at
),
686 (unsigned long long) ktime_to_us(stopped
),
687 (unsigned long long) idle
);
690 " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n",
691 pkt_dev
->seq_num
, pkt_dev
->cur_dst_mac_offset
,
692 pkt_dev
->cur_src_mac_offset
);
694 if (pkt_dev
->flags
& F_IPV6
) {
695 char b1
[128], b2
[128];
696 fmt_ip6(b1
, pkt_dev
->cur_in6_daddr
.s6_addr
);
697 fmt_ip6(b2
, pkt_dev
->cur_in6_saddr
.s6_addr
);
698 seq_printf(seq
, " cur_saddr: %s cur_daddr: %s\n", b2
, b1
);
700 seq_printf(seq
, " cur_saddr: 0x%x cur_daddr: 0x%x\n",
701 pkt_dev
->cur_saddr
, pkt_dev
->cur_daddr
);
703 seq_printf(seq
, " cur_udp_dst: %d cur_udp_src: %d\n",
704 pkt_dev
->cur_udp_dst
, pkt_dev
->cur_udp_src
);
706 seq_printf(seq
, " cur_queue_map: %u\n", pkt_dev
->cur_queue_map
);
708 seq_printf(seq
, " flows: %u\n", pkt_dev
->nflows
);
710 if (pkt_dev
->result
[0])
711 seq_printf(seq
, "Result: %s\n", pkt_dev
->result
);
713 seq_printf(seq
, "Result: Idle\n");
719 static int hex32_arg(const char __user
*user_buffer
, unsigned long maxlen
,
725 for (; i
< maxlen
; i
++) {
728 if (get_user(c
, &user_buffer
[i
]))
730 if ((c
>= '0') && (c
<= '9'))
732 else if ((c
>= 'a') && (c
<= 'f'))
733 *num
|= c
- 'a' + 10;
734 else if ((c
>= 'A') && (c
<= 'F'))
735 *num
|= c
- 'A' + 10;
742 static int count_trail_chars(const char __user
* user_buffer
,
747 for (i
= 0; i
< maxlen
; i
++) {
749 if (get_user(c
, &user_buffer
[i
]))
767 static unsigned long num_arg(const char __user
* user_buffer
,
768 unsigned long maxlen
, unsigned long *num
)
773 for (; i
< maxlen
; i
++) {
775 if (get_user(c
, &user_buffer
[i
]))
777 if ((c
>= '0') && (c
<= '9')) {
786 static int strn_len(const char __user
* user_buffer
, unsigned int maxlen
)
790 for (; i
< maxlen
; i
++) {
792 if (get_user(c
, &user_buffer
[i
]))
810 static ssize_t
get_labels(const char __user
*buffer
, struct pktgen_dev
*pkt_dev
)
817 pkt_dev
->nr_labels
= 0;
820 len
= hex32_arg(&buffer
[i
], 8, &tmp
);
823 pkt_dev
->labels
[n
] = htonl(tmp
);
824 if (pkt_dev
->labels
[n
] & MPLS_STACK_BOTTOM
)
825 pkt_dev
->flags
|= F_MPLS_RND
;
827 if (get_user(c
, &buffer
[i
]))
831 if (n
>= MAX_MPLS_LABELS
)
835 pkt_dev
->nr_labels
= n
;
839 static ssize_t
pktgen_if_write(struct file
*file
,
840 const char __user
* user_buffer
, size_t count
,
843 struct seq_file
*seq
= (struct seq_file
*)file
->private_data
;
844 struct pktgen_dev
*pkt_dev
= seq
->private;
846 char name
[16], valstr
[32];
847 unsigned long value
= 0;
848 char *pg_result
= NULL
;
852 pg_result
= &(pkt_dev
->result
[0]);
855 printk(KERN_WARNING
"pktgen: wrong command format\n");
860 tmp
= count_trail_chars(&user_buffer
[i
], max
);
862 printk(KERN_WARNING
"pktgen: illegal format\n");
867 /* Read variable name */
869 len
= strn_len(&user_buffer
[i
], sizeof(name
) - 1);
873 memset(name
, 0, sizeof(name
));
874 if (copy_from_user(name
, &user_buffer
[i
], len
))
879 len
= count_trail_chars(&user_buffer
[i
], max
);
887 if (copy_from_user(tb
, user_buffer
, count
))
890 printk(KERN_DEBUG
"pktgen: %s,%lu buffer -:%s:-\n", name
,
891 (unsigned long)count
, tb
);
894 if (!strcmp(name
, "min_pkt_size")) {
895 len
= num_arg(&user_buffer
[i
], 10, &value
);
900 if (value
< 14 + 20 + 8)
902 if (value
!= pkt_dev
->min_pkt_size
) {
903 pkt_dev
->min_pkt_size
= value
;
904 pkt_dev
->cur_pkt_size
= value
;
906 sprintf(pg_result
, "OK: min_pkt_size=%u",
907 pkt_dev
->min_pkt_size
);
911 if (!strcmp(name
, "max_pkt_size")) {
912 len
= num_arg(&user_buffer
[i
], 10, &value
);
917 if (value
< 14 + 20 + 8)
919 if (value
!= pkt_dev
->max_pkt_size
) {
920 pkt_dev
->max_pkt_size
= value
;
921 pkt_dev
->cur_pkt_size
= value
;
923 sprintf(pg_result
, "OK: max_pkt_size=%u",
924 pkt_dev
->max_pkt_size
);
928 /* Shortcut for min = max */
930 if (!strcmp(name
, "pkt_size")) {
931 len
= num_arg(&user_buffer
[i
], 10, &value
);
936 if (value
< 14 + 20 + 8)
938 if (value
!= pkt_dev
->min_pkt_size
) {
939 pkt_dev
->min_pkt_size
= value
;
940 pkt_dev
->max_pkt_size
= value
;
941 pkt_dev
->cur_pkt_size
= value
;
943 sprintf(pg_result
, "OK: pkt_size=%u", pkt_dev
->min_pkt_size
);
947 if (!strcmp(name
, "debug")) {
948 len
= num_arg(&user_buffer
[i
], 10, &value
);
954 sprintf(pg_result
, "OK: debug=%u", debug
);
958 if (!strcmp(name
, "frags")) {
959 len
= num_arg(&user_buffer
[i
], 10, &value
);
964 pkt_dev
->nfrags
= value
;
965 sprintf(pg_result
, "OK: frags=%u", pkt_dev
->nfrags
);
968 if (!strcmp(name
, "delay")) {
969 len
= num_arg(&user_buffer
[i
], 10, &value
);
974 if (value
== 0x7FFFFFFF)
975 pkt_dev
->delay
= ULLONG_MAX
;
977 pkt_dev
->delay
= (u64
)value
;
979 sprintf(pg_result
, "OK: delay=%llu",
980 (unsigned long long) pkt_dev
->delay
);
983 if (!strcmp(name
, "udp_src_min")) {
984 len
= num_arg(&user_buffer
[i
], 10, &value
);
989 if (value
!= pkt_dev
->udp_src_min
) {
990 pkt_dev
->udp_src_min
= value
;
991 pkt_dev
->cur_udp_src
= value
;
993 sprintf(pg_result
, "OK: udp_src_min=%u", pkt_dev
->udp_src_min
);
996 if (!strcmp(name
, "udp_dst_min")) {
997 len
= num_arg(&user_buffer
[i
], 10, &value
);
1002 if (value
!= pkt_dev
->udp_dst_min
) {
1003 pkt_dev
->udp_dst_min
= value
;
1004 pkt_dev
->cur_udp_dst
= value
;
1006 sprintf(pg_result
, "OK: udp_dst_min=%u", pkt_dev
->udp_dst_min
);
1009 if (!strcmp(name
, "udp_src_max")) {
1010 len
= num_arg(&user_buffer
[i
], 10, &value
);
1015 if (value
!= pkt_dev
->udp_src_max
) {
1016 pkt_dev
->udp_src_max
= value
;
1017 pkt_dev
->cur_udp_src
= value
;
1019 sprintf(pg_result
, "OK: udp_src_max=%u", pkt_dev
->udp_src_max
);
1022 if (!strcmp(name
, "udp_dst_max")) {
1023 len
= num_arg(&user_buffer
[i
], 10, &value
);
1028 if (value
!= pkt_dev
->udp_dst_max
) {
1029 pkt_dev
->udp_dst_max
= value
;
1030 pkt_dev
->cur_udp_dst
= value
;
1032 sprintf(pg_result
, "OK: udp_dst_max=%u", pkt_dev
->udp_dst_max
);
1035 if (!strcmp(name
, "clone_skb")) {
1036 len
= num_arg(&user_buffer
[i
], 10, &value
);
1041 pkt_dev
->clone_skb
= value
;
1043 sprintf(pg_result
, "OK: clone_skb=%d", pkt_dev
->clone_skb
);
1046 if (!strcmp(name
, "count")) {
1047 len
= num_arg(&user_buffer
[i
], 10, &value
);
1052 pkt_dev
->count
= value
;
1053 sprintf(pg_result
, "OK: count=%llu",
1054 (unsigned long long)pkt_dev
->count
);
1057 if (!strcmp(name
, "src_mac_count")) {
1058 len
= num_arg(&user_buffer
[i
], 10, &value
);
1063 if (pkt_dev
->src_mac_count
!= value
) {
1064 pkt_dev
->src_mac_count
= value
;
1065 pkt_dev
->cur_src_mac_offset
= 0;
1067 sprintf(pg_result
, "OK: src_mac_count=%d",
1068 pkt_dev
->src_mac_count
);
1071 if (!strcmp(name
, "dst_mac_count")) {
1072 len
= num_arg(&user_buffer
[i
], 10, &value
);
1077 if (pkt_dev
->dst_mac_count
!= value
) {
1078 pkt_dev
->dst_mac_count
= value
;
1079 pkt_dev
->cur_dst_mac_offset
= 0;
1081 sprintf(pg_result
, "OK: dst_mac_count=%d",
1082 pkt_dev
->dst_mac_count
);
1085 if (!strcmp(name
, "node")) {
1086 len
= num_arg(&user_buffer
[i
], 10, &value
);
1092 if (node_possible(value
)) {
1093 pkt_dev
->node
= value
;
1094 sprintf(pg_result
, "OK: node=%d", pkt_dev
->node
);
1097 sprintf(pg_result
, "ERROR: node not possible");
1100 if (!strcmp(name
, "flag")) {
1103 len
= strn_len(&user_buffer
[i
], sizeof(f
) - 1);
1107 if (copy_from_user(f
, &user_buffer
[i
], len
))
1110 if (strcmp(f
, "IPSRC_RND") == 0)
1111 pkt_dev
->flags
|= F_IPSRC_RND
;
1113 else if (strcmp(f
, "!IPSRC_RND") == 0)
1114 pkt_dev
->flags
&= ~F_IPSRC_RND
;
1116 else if (strcmp(f
, "TXSIZE_RND") == 0)
1117 pkt_dev
->flags
|= F_TXSIZE_RND
;
1119 else if (strcmp(f
, "!TXSIZE_RND") == 0)
1120 pkt_dev
->flags
&= ~F_TXSIZE_RND
;
1122 else if (strcmp(f
, "IPDST_RND") == 0)
1123 pkt_dev
->flags
|= F_IPDST_RND
;
1125 else if (strcmp(f
, "!IPDST_RND") == 0)
1126 pkt_dev
->flags
&= ~F_IPDST_RND
;
1128 else if (strcmp(f
, "UDPSRC_RND") == 0)
1129 pkt_dev
->flags
|= F_UDPSRC_RND
;
1131 else if (strcmp(f
, "!UDPSRC_RND") == 0)
1132 pkt_dev
->flags
&= ~F_UDPSRC_RND
;
1134 else if (strcmp(f
, "UDPDST_RND") == 0)
1135 pkt_dev
->flags
|= F_UDPDST_RND
;
1137 else if (strcmp(f
, "!UDPDST_RND") == 0)
1138 pkt_dev
->flags
&= ~F_UDPDST_RND
;
1140 else if (strcmp(f
, "MACSRC_RND") == 0)
1141 pkt_dev
->flags
|= F_MACSRC_RND
;
1143 else if (strcmp(f
, "!MACSRC_RND") == 0)
1144 pkt_dev
->flags
&= ~F_MACSRC_RND
;
1146 else if (strcmp(f
, "MACDST_RND") == 0)
1147 pkt_dev
->flags
|= F_MACDST_RND
;
1149 else if (strcmp(f
, "!MACDST_RND") == 0)
1150 pkt_dev
->flags
&= ~F_MACDST_RND
;
1152 else if (strcmp(f
, "MPLS_RND") == 0)
1153 pkt_dev
->flags
|= F_MPLS_RND
;
1155 else if (strcmp(f
, "!MPLS_RND") == 0)
1156 pkt_dev
->flags
&= ~F_MPLS_RND
;
1158 else if (strcmp(f
, "VID_RND") == 0)
1159 pkt_dev
->flags
|= F_VID_RND
;
1161 else if (strcmp(f
, "!VID_RND") == 0)
1162 pkt_dev
->flags
&= ~F_VID_RND
;
1164 else if (strcmp(f
, "SVID_RND") == 0)
1165 pkt_dev
->flags
|= F_SVID_RND
;
1167 else if (strcmp(f
, "!SVID_RND") == 0)
1168 pkt_dev
->flags
&= ~F_SVID_RND
;
1170 else if (strcmp(f
, "FLOW_SEQ") == 0)
1171 pkt_dev
->flags
|= F_FLOW_SEQ
;
1173 else if (strcmp(f
, "QUEUE_MAP_RND") == 0)
1174 pkt_dev
->flags
|= F_QUEUE_MAP_RND
;
1176 else if (strcmp(f
, "!QUEUE_MAP_RND") == 0)
1177 pkt_dev
->flags
&= ~F_QUEUE_MAP_RND
;
1179 else if (strcmp(f
, "QUEUE_MAP_CPU") == 0)
1180 pkt_dev
->flags
|= F_QUEUE_MAP_CPU
;
1182 else if (strcmp(f
, "!QUEUE_MAP_CPU") == 0)
1183 pkt_dev
->flags
&= ~F_QUEUE_MAP_CPU
;
1185 else if (strcmp(f
, "IPSEC") == 0)
1186 pkt_dev
->flags
|= F_IPSEC_ON
;
1189 else if (strcmp(f
, "!IPV6") == 0)
1190 pkt_dev
->flags
&= ~F_IPV6
;
1192 else if (strcmp(f
, "NODE_ALLOC") == 0)
1193 pkt_dev
->flags
|= F_NODE
;
1195 else if (strcmp(f
, "!NODE_ALLOC") == 0)
1196 pkt_dev
->flags
&= ~F_NODE
;
1200 "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
1202 "IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
1203 "MACSRC_RND, MACDST_RND, TXSIZE_RND, IPV6, MPLS_RND, VID_RND, SVID_RND, FLOW_SEQ, IPSEC, NODE_ALLOC\n");
1206 sprintf(pg_result
, "OK: flags=0x%x", pkt_dev
->flags
);
1209 if (!strcmp(name
, "dst_min") || !strcmp(name
, "dst")) {
1210 len
= strn_len(&user_buffer
[i
], sizeof(pkt_dev
->dst_min
) - 1);
1214 if (copy_from_user(buf
, &user_buffer
[i
], len
))
1217 if (strcmp(buf
, pkt_dev
->dst_min
) != 0) {
1218 memset(pkt_dev
->dst_min
, 0, sizeof(pkt_dev
->dst_min
));
1219 strncpy(pkt_dev
->dst_min
, buf
, len
);
1220 pkt_dev
->daddr_min
= in_aton(pkt_dev
->dst_min
);
1221 pkt_dev
->cur_daddr
= pkt_dev
->daddr_min
;
1224 printk(KERN_DEBUG
"pktgen: dst_min set to: %s\n",
1227 sprintf(pg_result
, "OK: dst_min=%s", pkt_dev
->dst_min
);
1230 if (!strcmp(name
, "dst_max")) {
1231 len
= strn_len(&user_buffer
[i
], sizeof(pkt_dev
->dst_max
) - 1);
1236 if (copy_from_user(buf
, &user_buffer
[i
], len
))
1240 if (strcmp(buf
, pkt_dev
->dst_max
) != 0) {
1241 memset(pkt_dev
->dst_max
, 0, sizeof(pkt_dev
->dst_max
));
1242 strncpy(pkt_dev
->dst_max
, buf
, len
);
1243 pkt_dev
->daddr_max
= in_aton(pkt_dev
->dst_max
);
1244 pkt_dev
->cur_daddr
= pkt_dev
->daddr_max
;
1247 printk(KERN_DEBUG
"pktgen: dst_max set to: %s\n",
1250 sprintf(pg_result
, "OK: dst_max=%s", pkt_dev
->dst_max
);
1253 if (!strcmp(name
, "dst6")) {
1254 len
= strn_len(&user_buffer
[i
], sizeof(buf
) - 1);
1258 pkt_dev
->flags
|= F_IPV6
;
1260 if (copy_from_user(buf
, &user_buffer
[i
], len
))
1264 scan_ip6(buf
, pkt_dev
->in6_daddr
.s6_addr
);
1265 fmt_ip6(buf
, pkt_dev
->in6_daddr
.s6_addr
);
1267 ipv6_addr_copy(&pkt_dev
->cur_in6_daddr
, &pkt_dev
->in6_daddr
);
1270 printk(KERN_DEBUG
"pktgen: dst6 set to: %s\n", buf
);
1273 sprintf(pg_result
, "OK: dst6=%s", buf
);
1276 if (!strcmp(name
, "dst6_min")) {
1277 len
= strn_len(&user_buffer
[i
], sizeof(buf
) - 1);
1281 pkt_dev
->flags
|= F_IPV6
;
1283 if (copy_from_user(buf
, &user_buffer
[i
], len
))
1287 scan_ip6(buf
, pkt_dev
->min_in6_daddr
.s6_addr
);
1288 fmt_ip6(buf
, pkt_dev
->min_in6_daddr
.s6_addr
);
1290 ipv6_addr_copy(&pkt_dev
->cur_in6_daddr
,
1291 &pkt_dev
->min_in6_daddr
);
1293 printk(KERN_DEBUG
"pktgen: dst6_min set to: %s\n", buf
);
1296 sprintf(pg_result
, "OK: dst6_min=%s", buf
);
1299 if (!strcmp(name
, "dst6_max")) {
1300 len
= strn_len(&user_buffer
[i
], sizeof(buf
) - 1);
1304 pkt_dev
->flags
|= F_IPV6
;
1306 if (copy_from_user(buf
, &user_buffer
[i
], len
))
1310 scan_ip6(buf
, pkt_dev
->max_in6_daddr
.s6_addr
);
1311 fmt_ip6(buf
, pkt_dev
->max_in6_daddr
.s6_addr
);
1314 printk(KERN_DEBUG
"pktgen: dst6_max set to: %s\n", buf
);
1317 sprintf(pg_result
, "OK: dst6_max=%s", buf
);
1320 if (!strcmp(name
, "src6")) {
1321 len
= strn_len(&user_buffer
[i
], sizeof(buf
) - 1);
1325 pkt_dev
->flags
|= F_IPV6
;
1327 if (copy_from_user(buf
, &user_buffer
[i
], len
))
1331 scan_ip6(buf
, pkt_dev
->in6_saddr
.s6_addr
);
1332 fmt_ip6(buf
, pkt_dev
->in6_saddr
.s6_addr
);
1334 ipv6_addr_copy(&pkt_dev
->cur_in6_saddr
, &pkt_dev
->in6_saddr
);
1337 printk(KERN_DEBUG
"pktgen: src6 set to: %s\n", buf
);
1340 sprintf(pg_result
, "OK: src6=%s", buf
);
1343 if (!strcmp(name
, "src_min")) {
1344 len
= strn_len(&user_buffer
[i
], sizeof(pkt_dev
->src_min
) - 1);
1348 if (copy_from_user(buf
, &user_buffer
[i
], len
))
1351 if (strcmp(buf
, pkt_dev
->src_min
) != 0) {
1352 memset(pkt_dev
->src_min
, 0, sizeof(pkt_dev
->src_min
));
1353 strncpy(pkt_dev
->src_min
, buf
, len
);
1354 pkt_dev
->saddr_min
= in_aton(pkt_dev
->src_min
);
1355 pkt_dev
->cur_saddr
= pkt_dev
->saddr_min
;
1358 printk(KERN_DEBUG
"pktgen: src_min set to: %s\n",
1361 sprintf(pg_result
, "OK: src_min=%s", pkt_dev
->src_min
);
1364 if (!strcmp(name
, "src_max")) {
1365 len
= strn_len(&user_buffer
[i
], sizeof(pkt_dev
->src_max
) - 1);
1369 if (copy_from_user(buf
, &user_buffer
[i
], len
))
1372 if (strcmp(buf
, pkt_dev
->src_max
) != 0) {
1373 memset(pkt_dev
->src_max
, 0, sizeof(pkt_dev
->src_max
));
1374 strncpy(pkt_dev
->src_max
, buf
, len
);
1375 pkt_dev
->saddr_max
= in_aton(pkt_dev
->src_max
);
1376 pkt_dev
->cur_saddr
= pkt_dev
->saddr_max
;
1379 printk(KERN_DEBUG
"pktgen: src_max set to: %s\n",
1382 sprintf(pg_result
, "OK: src_max=%s", pkt_dev
->src_max
);
1385 if (!strcmp(name
, "dst_mac")) {
1387 unsigned char old_dmac
[ETH_ALEN
];
1388 unsigned char *m
= pkt_dev
->dst_mac
;
1389 memcpy(old_dmac
, pkt_dev
->dst_mac
, ETH_ALEN
);
1391 len
= strn_len(&user_buffer
[i
], sizeof(valstr
) - 1);
1395 memset(valstr
, 0, sizeof(valstr
));
1396 if (copy_from_user(valstr
, &user_buffer
[i
], len
))
1400 for (*m
= 0; *v
&& m
< pkt_dev
->dst_mac
+ 6; v
++) {
1401 if (*v
>= '0' && *v
<= '9') {
1405 if (*v
>= 'A' && *v
<= 'F') {
1407 *m
+= *v
- 'A' + 10;
1409 if (*v
>= 'a' && *v
<= 'f') {
1411 *m
+= *v
- 'a' + 10;
1419 /* Set up Dest MAC */
1420 if (compare_ether_addr(old_dmac
, pkt_dev
->dst_mac
))
1421 memcpy(&(pkt_dev
->hh
[0]), pkt_dev
->dst_mac
, ETH_ALEN
);
1423 sprintf(pg_result
, "OK: dstmac");
1426 if (!strcmp(name
, "src_mac")) {
1428 unsigned char old_smac
[ETH_ALEN
];
1429 unsigned char *m
= pkt_dev
->src_mac
;
1431 memcpy(old_smac
, pkt_dev
->src_mac
, ETH_ALEN
);
1433 len
= strn_len(&user_buffer
[i
], sizeof(valstr
) - 1);
1437 memset(valstr
, 0, sizeof(valstr
));
1438 if (copy_from_user(valstr
, &user_buffer
[i
], len
))
1442 for (*m
= 0; *v
&& m
< pkt_dev
->src_mac
+ 6; v
++) {
1443 if (*v
>= '0' && *v
<= '9') {
1447 if (*v
>= 'A' && *v
<= 'F') {
1449 *m
+= *v
- 'A' + 10;
1451 if (*v
>= 'a' && *v
<= 'f') {
1453 *m
+= *v
- 'a' + 10;
1461 /* Set up Src MAC */
1462 if (compare_ether_addr(old_smac
, pkt_dev
->src_mac
))
1463 memcpy(&(pkt_dev
->hh
[6]), pkt_dev
->src_mac
, ETH_ALEN
);
1465 sprintf(pg_result
, "OK: srcmac");
1469 if (!strcmp(name
, "clear_counters")) {
1470 pktgen_clear_counters(pkt_dev
);
1471 sprintf(pg_result
, "OK: Clearing counters.\n");
1475 if (!strcmp(name
, "flows")) {
1476 len
= num_arg(&user_buffer
[i
], 10, &value
);
1481 if (value
> MAX_CFLOWS
)
1484 pkt_dev
->cflows
= value
;
1485 sprintf(pg_result
, "OK: flows=%u", pkt_dev
->cflows
);
1489 if (!strcmp(name
, "flowlen")) {
1490 len
= num_arg(&user_buffer
[i
], 10, &value
);
1495 pkt_dev
->lflow
= value
;
1496 sprintf(pg_result
, "OK: flowlen=%u", pkt_dev
->lflow
);
1500 if (!strcmp(name
, "queue_map_min")) {
1501 len
= num_arg(&user_buffer
[i
], 5, &value
);
1506 pkt_dev
->queue_map_min
= value
;
1507 sprintf(pg_result
, "OK: queue_map_min=%u", pkt_dev
->queue_map_min
);
1511 if (!strcmp(name
, "queue_map_max")) {
1512 len
= num_arg(&user_buffer
[i
], 5, &value
);
1517 pkt_dev
->queue_map_max
= value
;
1518 sprintf(pg_result
, "OK: queue_map_max=%u", pkt_dev
->queue_map_max
);
1522 if (!strcmp(name
, "mpls")) {
1525 len
= get_labels(&user_buffer
[i
], pkt_dev
);
1529 cnt
= sprintf(pg_result
, "OK: mpls=");
1530 for (n
= 0; n
< pkt_dev
->nr_labels
; n
++)
1531 cnt
+= sprintf(pg_result
+ cnt
,
1532 "%08x%s", ntohl(pkt_dev
->labels
[n
]),
1533 n
== pkt_dev
->nr_labels
-1 ? "" : ",");
1535 if (pkt_dev
->nr_labels
&& pkt_dev
->vlan_id
!= 0xffff) {
1536 pkt_dev
->vlan_id
= 0xffff; /* turn off VLAN/SVLAN */
1537 pkt_dev
->svlan_id
= 0xffff;
1540 printk(KERN_DEBUG
"pktgen: VLAN/SVLAN auto turned off\n");
1545 if (!strcmp(name
, "vlan_id")) {
1546 len
= num_arg(&user_buffer
[i
], 4, &value
);
1551 if (value
<= 4095) {
1552 pkt_dev
->vlan_id
= value
; /* turn on VLAN */
1555 printk(KERN_DEBUG
"pktgen: VLAN turned on\n");
1557 if (debug
&& pkt_dev
->nr_labels
)
1558 printk(KERN_DEBUG
"pktgen: MPLS auto turned off\n");
1560 pkt_dev
->nr_labels
= 0; /* turn off MPLS */
1561 sprintf(pg_result
, "OK: vlan_id=%u", pkt_dev
->vlan_id
);
1563 pkt_dev
->vlan_id
= 0xffff; /* turn off VLAN/SVLAN */
1564 pkt_dev
->svlan_id
= 0xffff;
1567 printk(KERN_DEBUG
"pktgen: VLAN/SVLAN turned off\n");
1572 if (!strcmp(name
, "vlan_p")) {
1573 len
= num_arg(&user_buffer
[i
], 1, &value
);
1578 if ((value
<= 7) && (pkt_dev
->vlan_id
!= 0xffff)) {
1579 pkt_dev
->vlan_p
= value
;
1580 sprintf(pg_result
, "OK: vlan_p=%u", pkt_dev
->vlan_p
);
1582 sprintf(pg_result
, "ERROR: vlan_p must be 0-7");
1587 if (!strcmp(name
, "vlan_cfi")) {
1588 len
= num_arg(&user_buffer
[i
], 1, &value
);
1593 if ((value
<= 1) && (pkt_dev
->vlan_id
!= 0xffff)) {
1594 pkt_dev
->vlan_cfi
= value
;
1595 sprintf(pg_result
, "OK: vlan_cfi=%u", pkt_dev
->vlan_cfi
);
1597 sprintf(pg_result
, "ERROR: vlan_cfi must be 0-1");
1602 if (!strcmp(name
, "svlan_id")) {
1603 len
= num_arg(&user_buffer
[i
], 4, &value
);
1608 if ((value
<= 4095) && ((pkt_dev
->vlan_id
!= 0xffff))) {
1609 pkt_dev
->svlan_id
= value
; /* turn on SVLAN */
1612 printk(KERN_DEBUG
"pktgen: SVLAN turned on\n");
1614 if (debug
&& pkt_dev
->nr_labels
)
1615 printk(KERN_DEBUG
"pktgen: MPLS auto turned off\n");
1617 pkt_dev
->nr_labels
= 0; /* turn off MPLS */
1618 sprintf(pg_result
, "OK: svlan_id=%u", pkt_dev
->svlan_id
);
1620 pkt_dev
->vlan_id
= 0xffff; /* turn off VLAN/SVLAN */
1621 pkt_dev
->svlan_id
= 0xffff;
1624 printk(KERN_DEBUG
"pktgen: VLAN/SVLAN turned off\n");
1629 if (!strcmp(name
, "svlan_p")) {
1630 len
= num_arg(&user_buffer
[i
], 1, &value
);
1635 if ((value
<= 7) && (pkt_dev
->svlan_id
!= 0xffff)) {
1636 pkt_dev
->svlan_p
= value
;
1637 sprintf(pg_result
, "OK: svlan_p=%u", pkt_dev
->svlan_p
);
1639 sprintf(pg_result
, "ERROR: svlan_p must be 0-7");
1644 if (!strcmp(name
, "svlan_cfi")) {
1645 len
= num_arg(&user_buffer
[i
], 1, &value
);
1650 if ((value
<= 1) && (pkt_dev
->svlan_id
!= 0xffff)) {
1651 pkt_dev
->svlan_cfi
= value
;
1652 sprintf(pg_result
, "OK: svlan_cfi=%u", pkt_dev
->svlan_cfi
);
1654 sprintf(pg_result
, "ERROR: svlan_cfi must be 0-1");
1659 if (!strcmp(name
, "tos")) {
1660 __u32 tmp_value
= 0;
1661 len
= hex32_arg(&user_buffer
[i
], 2, &tmp_value
);
1667 pkt_dev
->tos
= tmp_value
;
1668 sprintf(pg_result
, "OK: tos=0x%02x", pkt_dev
->tos
);
1670 sprintf(pg_result
, "ERROR: tos must be 00-ff");
1675 if (!strcmp(name
, "traffic_class")) {
1676 __u32 tmp_value
= 0;
1677 len
= hex32_arg(&user_buffer
[i
], 2, &tmp_value
);
1683 pkt_dev
->traffic_class
= tmp_value
;
1684 sprintf(pg_result
, "OK: traffic_class=0x%02x", pkt_dev
->traffic_class
);
1686 sprintf(pg_result
, "ERROR: traffic_class must be 00-ff");
1691 sprintf(pkt_dev
->result
, "No such parameter \"%s\"", name
);
1695 static int pktgen_if_open(struct inode
*inode
, struct file
*file
)
1697 return single_open(file
, pktgen_if_show
, PDE(inode
)->data
);
1700 static const struct file_operations pktgen_if_fops
= {
1701 .owner
= THIS_MODULE
,
1702 .open
= pktgen_if_open
,
1704 .llseek
= seq_lseek
,
1705 .write
= pktgen_if_write
,
1706 .release
= single_release
,
1709 static int pktgen_thread_show(struct seq_file
*seq
, void *v
)
1711 struct pktgen_thread
*t
= seq
->private;
1712 const struct pktgen_dev
*pkt_dev
;
1716 seq_printf(seq
, "Running: ");
1719 list_for_each_entry(pkt_dev
, &t
->if_list
, list
)
1720 if (pkt_dev
->running
)
1721 seq_printf(seq
, "%s ", pkt_dev
->odevname
);
1723 seq_printf(seq
, "\nStopped: ");
1725 list_for_each_entry(pkt_dev
, &t
->if_list
, list
)
1726 if (!pkt_dev
->running
)
1727 seq_printf(seq
, "%s ", pkt_dev
->odevname
);
1730 seq_printf(seq
, "\nResult: %s\n", t
->result
);
1732 seq_printf(seq
, "\nResult: NA\n");
1739 static ssize_t
pktgen_thread_write(struct file
*file
,
1740 const char __user
* user_buffer
,
1741 size_t count
, loff_t
* offset
)
1743 struct seq_file
*seq
= (struct seq_file
*)file
->private_data
;
1744 struct pktgen_thread
*t
= seq
->private;
1745 int i
= 0, max
, len
, ret
;
1750 // sprintf(pg_result, "Wrong command format");
1755 len
= count_trail_chars(&user_buffer
[i
], max
);
1761 /* Read variable name */
1763 len
= strn_len(&user_buffer
[i
], sizeof(name
) - 1);
1767 memset(name
, 0, sizeof(name
));
1768 if (copy_from_user(name
, &user_buffer
[i
], len
))
1773 len
= count_trail_chars(&user_buffer
[i
], max
);
1780 printk(KERN_DEBUG
"pktgen: t=%s, count=%lu\n",
1781 name
, (unsigned long)count
);
1784 printk(KERN_ERR
"pktgen: ERROR: No thread\n");
1789 pg_result
= &(t
->result
[0]);
1791 if (!strcmp(name
, "add_device")) {
1794 len
= strn_len(&user_buffer
[i
], sizeof(f
) - 1);
1799 if (copy_from_user(f
, &user_buffer
[i
], len
))
1802 mutex_lock(&pktgen_thread_lock
);
1803 pktgen_add_device(t
, f
);
1804 mutex_unlock(&pktgen_thread_lock
);
1806 sprintf(pg_result
, "OK: add_device=%s", f
);
1810 if (!strcmp(name
, "rem_device_all")) {
1811 mutex_lock(&pktgen_thread_lock
);
1812 t
->control
|= T_REMDEVALL
;
1813 mutex_unlock(&pktgen_thread_lock
);
1814 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
1816 sprintf(pg_result
, "OK: rem_device_all");
1820 if (!strcmp(name
, "max_before_softirq")) {
1821 sprintf(pg_result
, "OK: Note! max_before_softirq is obsoleted -- Do not use");
1831 static int pktgen_thread_open(struct inode
*inode
, struct file
*file
)
1833 return single_open(file
, pktgen_thread_show
, PDE(inode
)->data
);
1836 static const struct file_operations pktgen_thread_fops
= {
1837 .owner
= THIS_MODULE
,
1838 .open
= pktgen_thread_open
,
1840 .llseek
= seq_lseek
,
1841 .write
= pktgen_thread_write
,
1842 .release
= single_release
,
1845 /* Think find or remove for NN */
1846 static struct pktgen_dev
*__pktgen_NN_threads(const char *ifname
, int remove
)
1848 struct pktgen_thread
*t
;
1849 struct pktgen_dev
*pkt_dev
= NULL
;
1850 bool exact
= (remove
== FIND
);
1852 list_for_each_entry(t
, &pktgen_threads
, th_list
) {
1853 pkt_dev
= pktgen_find_dev(t
, ifname
, exact
);
1857 pkt_dev
->removal_mark
= 1;
1858 t
->control
|= T_REMDEV
;
1868 * mark a device for removal
1870 static void pktgen_mark_device(const char *ifname
)
1872 struct pktgen_dev
*pkt_dev
= NULL
;
1873 const int max_tries
= 10, msec_per_try
= 125;
1876 mutex_lock(&pktgen_thread_lock
);
1877 pr_debug("pktgen: pktgen_mark_device marking %s for removal\n", ifname
);
1881 pkt_dev
= __pktgen_NN_threads(ifname
, REMOVE
);
1882 if (pkt_dev
== NULL
)
1883 break; /* success */
1885 mutex_unlock(&pktgen_thread_lock
);
1886 pr_debug("pktgen: pktgen_mark_device waiting for %s "
1887 "to disappear....\n", ifname
);
1888 schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try
));
1889 mutex_lock(&pktgen_thread_lock
);
1891 if (++i
>= max_tries
) {
1892 printk(KERN_ERR
"pktgen_mark_device: timed out after "
1893 "waiting %d msec for device %s to be removed\n",
1894 msec_per_try
* i
, ifname
);
1900 mutex_unlock(&pktgen_thread_lock
);
1903 static void pktgen_change_name(struct net_device
*dev
)
1905 struct pktgen_thread
*t
;
1907 list_for_each_entry(t
, &pktgen_threads
, th_list
) {
1908 struct pktgen_dev
*pkt_dev
;
1910 list_for_each_entry(pkt_dev
, &t
->if_list
, list
) {
1911 if (pkt_dev
->odev
!= dev
)
1914 remove_proc_entry(pkt_dev
->entry
->name
, pg_proc_dir
);
1916 pkt_dev
->entry
= proc_create_data(dev
->name
, 0600,
1920 if (!pkt_dev
->entry
)
1921 printk(KERN_ERR
"pktgen: can't move proc "
1922 " entry for '%s'\n", dev
->name
);
1928 static int pktgen_device_event(struct notifier_block
*unused
,
1929 unsigned long event
, void *ptr
)
1931 struct net_device
*dev
= ptr
;
1933 if (!net_eq(dev_net(dev
), &init_net
))
1936 /* It is OK that we do not hold the group lock right now,
1937 * as we run under the RTNL lock.
1941 case NETDEV_CHANGENAME
:
1942 pktgen_change_name(dev
);
1945 case NETDEV_UNREGISTER
:
1946 pktgen_mark_device(dev
->name
);
1953 static struct net_device
*pktgen_dev_get_by_name(struct pktgen_dev
*pkt_dev
,
1959 for (i
= 0; ifname
[i
] != '@'; i
++) {
1967 return dev_get_by_name(&init_net
, b
);
1971 /* Associate pktgen_dev with a device. */
1973 static int pktgen_setup_dev(struct pktgen_dev
*pkt_dev
, const char *ifname
)
1975 struct net_device
*odev
;
1978 /* Clean old setups */
1979 if (pkt_dev
->odev
) {
1980 dev_put(pkt_dev
->odev
);
1981 pkt_dev
->odev
= NULL
;
1984 odev
= pktgen_dev_get_by_name(pkt_dev
, ifname
);
1986 printk(KERN_ERR
"pktgen: no such netdevice: \"%s\"\n", ifname
);
1990 if (odev
->type
!= ARPHRD_ETHER
) {
1991 printk(KERN_ERR
"pktgen: not an ethernet device: \"%s\"\n", ifname
);
1993 } else if (!netif_running(odev
)) {
1994 printk(KERN_ERR
"pktgen: device is down: \"%s\"\n", ifname
);
1997 pkt_dev
->odev
= odev
;
2005 /* Read pkt_dev from the interface and set up internal pktgen_dev
2006 * structure to have the right information to create/send packets
2008 static void pktgen_setup_inject(struct pktgen_dev
*pkt_dev
)
2012 if (!pkt_dev
->odev
) {
2013 printk(KERN_ERR
"pktgen: ERROR: pkt_dev->odev == NULL in "
2015 sprintf(pkt_dev
->result
,
2016 "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
2020 /* make sure that we don't pick a non-existing transmit queue */
2021 ntxq
= pkt_dev
->odev
->real_num_tx_queues
;
2023 if (ntxq
<= pkt_dev
->queue_map_min
) {
2024 printk(KERN_WARNING
"pktgen: WARNING: Requested "
2025 "queue_map_min (zero-based) (%d) exceeds valid range "
2026 "[0 - %d] for (%d) queues on %s, resetting\n",
2027 pkt_dev
->queue_map_min
, (ntxq
?: 1) - 1, ntxq
,
2029 pkt_dev
->queue_map_min
= ntxq
- 1;
2031 if (pkt_dev
->queue_map_max
>= ntxq
) {
2032 printk(KERN_WARNING
"pktgen: WARNING: Requested "
2033 "queue_map_max (zero-based) (%d) exceeds valid range "
2034 "[0 - %d] for (%d) queues on %s, resetting\n",
2035 pkt_dev
->queue_map_max
, (ntxq
?: 1) - 1, ntxq
,
2037 pkt_dev
->queue_map_max
= ntxq
- 1;
2040 /* Default to the interface's mac if not explicitly set. */
2042 if (is_zero_ether_addr(pkt_dev
->src_mac
))
2043 memcpy(&(pkt_dev
->hh
[6]), pkt_dev
->odev
->dev_addr
, ETH_ALEN
);
2045 /* Set up Dest MAC */
2046 memcpy(&(pkt_dev
->hh
[0]), pkt_dev
->dst_mac
, ETH_ALEN
);
2048 /* Set up pkt size */
2049 pkt_dev
->cur_pkt_size
= pkt_dev
->min_pkt_size
;
2051 if (pkt_dev
->flags
& F_IPV6
) {
2053 * Skip this automatic address setting until locks or functions
2058 int i
, set
= 0, err
= 1;
2059 struct inet6_dev
*idev
;
2061 for (i
= 0; i
< IN6_ADDR_HSIZE
; i
++)
2062 if (pkt_dev
->cur_in6_saddr
.s6_addr
[i
]) {
2070 * Use linklevel address if unconfigured.
2072 * use ipv6_get_lladdr if/when it's get exported
2076 idev
= __in6_dev_get(pkt_dev
->odev
);
2078 struct inet6_ifaddr
*ifp
;
2080 read_lock_bh(&idev
->lock
);
2081 for (ifp
= idev
->addr_list
; ifp
;
2082 ifp
= ifp
->if_next
) {
2083 if (ifp
->scope
== IFA_LINK
&&
2084 !(ifp
->flags
& IFA_F_TENTATIVE
)) {
2085 ipv6_addr_copy(&pkt_dev
->
2092 read_unlock_bh(&idev
->lock
);
2096 printk(KERN_ERR
"pktgen: ERROR: IPv6 link "
2097 "address not availble.\n");
2101 pkt_dev
->saddr_min
= 0;
2102 pkt_dev
->saddr_max
= 0;
2103 if (strlen(pkt_dev
->src_min
) == 0) {
2105 struct in_device
*in_dev
;
2108 in_dev
= __in_dev_get_rcu(pkt_dev
->odev
);
2110 if (in_dev
->ifa_list
) {
2111 pkt_dev
->saddr_min
=
2112 in_dev
->ifa_list
->ifa_address
;
2113 pkt_dev
->saddr_max
= pkt_dev
->saddr_min
;
2118 pkt_dev
->saddr_min
= in_aton(pkt_dev
->src_min
);
2119 pkt_dev
->saddr_max
= in_aton(pkt_dev
->src_max
);
2122 pkt_dev
->daddr_min
= in_aton(pkt_dev
->dst_min
);
2123 pkt_dev
->daddr_max
= in_aton(pkt_dev
->dst_max
);
2125 /* Initialize current values. */
2126 pkt_dev
->cur_dst_mac_offset
= 0;
2127 pkt_dev
->cur_src_mac_offset
= 0;
2128 pkt_dev
->cur_saddr
= pkt_dev
->saddr_min
;
2129 pkt_dev
->cur_daddr
= pkt_dev
->daddr_min
;
2130 pkt_dev
->cur_udp_dst
= pkt_dev
->udp_dst_min
;
2131 pkt_dev
->cur_udp_src
= pkt_dev
->udp_src_min
;
2132 pkt_dev
->nflows
= 0;
2136 static void spin(struct pktgen_dev
*pkt_dev
, ktime_t spin_until
)
2138 ktime_t start_time
, end_time
;
2140 struct hrtimer_sleeper t
;
2142 hrtimer_init_on_stack(&t
.timer
, CLOCK_MONOTONIC
, HRTIMER_MODE_ABS
);
2143 hrtimer_set_expires(&t
.timer
, spin_until
);
2145 remaining
= ktime_to_us(hrtimer_expires_remaining(&t
.timer
));
2146 if (remaining
<= 0) {
2147 pkt_dev
->next_tx
= ktime_add_ns(spin_until
, pkt_dev
->delay
);
2151 start_time
= ktime_now();
2152 if (remaining
< 100)
2153 udelay(remaining
); /* really small just spin */
2155 /* see do_nanosleep */
2156 hrtimer_init_sleeper(&t
, current
);
2158 set_current_state(TASK_INTERRUPTIBLE
);
2159 hrtimer_start_expires(&t
.timer
, HRTIMER_MODE_ABS
);
2160 if (!hrtimer_active(&t
.timer
))
2166 hrtimer_cancel(&t
.timer
);
2167 } while (t
.task
&& pkt_dev
->running
&& !signal_pending(current
));
2168 __set_current_state(TASK_RUNNING
);
2170 end_time
= ktime_now();
2172 pkt_dev
->idle_acc
+= ktime_to_ns(ktime_sub(end_time
, start_time
));
2173 pkt_dev
->next_tx
= ktime_add_ns(spin_until
, pkt_dev
->delay
);
2176 static inline void set_pkt_overhead(struct pktgen_dev
*pkt_dev
)
2178 pkt_dev
->pkt_overhead
= 0;
2179 pkt_dev
->pkt_overhead
+= pkt_dev
->nr_labels
*sizeof(u32
);
2180 pkt_dev
->pkt_overhead
+= VLAN_TAG_SIZE(pkt_dev
);
2181 pkt_dev
->pkt_overhead
+= SVLAN_TAG_SIZE(pkt_dev
);
2184 static inline int f_seen(const struct pktgen_dev
*pkt_dev
, int flow
)
2186 return !!(pkt_dev
->flows
[flow
].flags
& F_INIT
);
2189 static inline int f_pick(struct pktgen_dev
*pkt_dev
)
2191 int flow
= pkt_dev
->curfl
;
2193 if (pkt_dev
->flags
& F_FLOW_SEQ
) {
2194 if (pkt_dev
->flows
[flow
].count
>= pkt_dev
->lflow
) {
2196 pkt_dev
->flows
[flow
].count
= 0;
2197 pkt_dev
->flows
[flow
].flags
= 0;
2198 pkt_dev
->curfl
+= 1;
2199 if (pkt_dev
->curfl
>= pkt_dev
->cflows
)
2200 pkt_dev
->curfl
= 0; /*reset */
2203 flow
= random32() % pkt_dev
->cflows
;
2204 pkt_dev
->curfl
= flow
;
2206 if (pkt_dev
->flows
[flow
].count
> pkt_dev
->lflow
) {
2207 pkt_dev
->flows
[flow
].count
= 0;
2208 pkt_dev
->flows
[flow
].flags
= 0;
2212 return pkt_dev
->curfl
;
2217 /* If there was already an IPSEC SA, we keep it as is, else
2218 * we go look for it ...
2220 #define DUMMY_MARK 0
2221 static void get_ipsec_sa(struct pktgen_dev
*pkt_dev
, int flow
)
2223 struct xfrm_state
*x
= pkt_dev
->flows
[flow
].x
;
2225 /*slow path: we dont already have xfrm_state*/
2226 x
= xfrm_stateonly_find(&init_net
, DUMMY_MARK
,
2227 (xfrm_address_t
*)&pkt_dev
->cur_daddr
,
2228 (xfrm_address_t
*)&pkt_dev
->cur_saddr
,
2231 pkt_dev
->ipsproto
, 0);
2233 pkt_dev
->flows
[flow
].x
= x
;
2234 set_pkt_overhead(pkt_dev
);
2235 pkt_dev
->pkt_overhead
+= x
->props
.header_len
;
2241 static void set_cur_queue_map(struct pktgen_dev
*pkt_dev
)
2244 if (pkt_dev
->flags
& F_QUEUE_MAP_CPU
)
2245 pkt_dev
->cur_queue_map
= smp_processor_id();
2247 else if (pkt_dev
->queue_map_min
<= pkt_dev
->queue_map_max
) {
2249 if (pkt_dev
->flags
& F_QUEUE_MAP_RND
) {
2251 (pkt_dev
->queue_map_max
-
2252 pkt_dev
->queue_map_min
+ 1)
2253 + pkt_dev
->queue_map_min
;
2255 t
= pkt_dev
->cur_queue_map
+ 1;
2256 if (t
> pkt_dev
->queue_map_max
)
2257 t
= pkt_dev
->queue_map_min
;
2259 pkt_dev
->cur_queue_map
= t
;
2261 pkt_dev
->cur_queue_map
= pkt_dev
->cur_queue_map
% pkt_dev
->odev
->real_num_tx_queues
;
2264 /* Increment/randomize headers according to flags and current values
2265 * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
2267 static void mod_cur_headers(struct pktgen_dev
*pkt_dev
)
2273 if (pkt_dev
->cflows
)
2274 flow
= f_pick(pkt_dev
);
2276 /* Deal with source MAC */
2277 if (pkt_dev
->src_mac_count
> 1) {
2281 if (pkt_dev
->flags
& F_MACSRC_RND
)
2282 mc
= random32() % pkt_dev
->src_mac_count
;
2284 mc
= pkt_dev
->cur_src_mac_offset
++;
2285 if (pkt_dev
->cur_src_mac_offset
>=
2286 pkt_dev
->src_mac_count
)
2287 pkt_dev
->cur_src_mac_offset
= 0;
2290 tmp
= pkt_dev
->src_mac
[5] + (mc
& 0xFF);
2291 pkt_dev
->hh
[11] = tmp
;
2292 tmp
= (pkt_dev
->src_mac
[4] + ((mc
>> 8) & 0xFF) + (tmp
>> 8));
2293 pkt_dev
->hh
[10] = tmp
;
2294 tmp
= (pkt_dev
->src_mac
[3] + ((mc
>> 16) & 0xFF) + (tmp
>> 8));
2295 pkt_dev
->hh
[9] = tmp
;
2296 tmp
= (pkt_dev
->src_mac
[2] + ((mc
>> 24) & 0xFF) + (tmp
>> 8));
2297 pkt_dev
->hh
[8] = tmp
;
2298 tmp
= (pkt_dev
->src_mac
[1] + (tmp
>> 8));
2299 pkt_dev
->hh
[7] = tmp
;
2302 /* Deal with Destination MAC */
2303 if (pkt_dev
->dst_mac_count
> 1) {
2307 if (pkt_dev
->flags
& F_MACDST_RND
)
2308 mc
= random32() % pkt_dev
->dst_mac_count
;
2311 mc
= pkt_dev
->cur_dst_mac_offset
++;
2312 if (pkt_dev
->cur_dst_mac_offset
>=
2313 pkt_dev
->dst_mac_count
) {
2314 pkt_dev
->cur_dst_mac_offset
= 0;
2318 tmp
= pkt_dev
->dst_mac
[5] + (mc
& 0xFF);
2319 pkt_dev
->hh
[5] = tmp
;
2320 tmp
= (pkt_dev
->dst_mac
[4] + ((mc
>> 8) & 0xFF) + (tmp
>> 8));
2321 pkt_dev
->hh
[4] = tmp
;
2322 tmp
= (pkt_dev
->dst_mac
[3] + ((mc
>> 16) & 0xFF) + (tmp
>> 8));
2323 pkt_dev
->hh
[3] = tmp
;
2324 tmp
= (pkt_dev
->dst_mac
[2] + ((mc
>> 24) & 0xFF) + (tmp
>> 8));
2325 pkt_dev
->hh
[2] = tmp
;
2326 tmp
= (pkt_dev
->dst_mac
[1] + (tmp
>> 8));
2327 pkt_dev
->hh
[1] = tmp
;
2330 if (pkt_dev
->flags
& F_MPLS_RND
) {
2332 for (i
= 0; i
< pkt_dev
->nr_labels
; i
++)
2333 if (pkt_dev
->labels
[i
] & MPLS_STACK_BOTTOM
)
2334 pkt_dev
->labels
[i
] = MPLS_STACK_BOTTOM
|
2335 ((__force __be32
)random32() &
2339 if ((pkt_dev
->flags
& F_VID_RND
) && (pkt_dev
->vlan_id
!= 0xffff)) {
2340 pkt_dev
->vlan_id
= random32() & (4096-1);
2343 if ((pkt_dev
->flags
& F_SVID_RND
) && (pkt_dev
->svlan_id
!= 0xffff)) {
2344 pkt_dev
->svlan_id
= random32() & (4096 - 1);
2347 if (pkt_dev
->udp_src_min
< pkt_dev
->udp_src_max
) {
2348 if (pkt_dev
->flags
& F_UDPSRC_RND
)
2349 pkt_dev
->cur_udp_src
= random32() %
2350 (pkt_dev
->udp_src_max
- pkt_dev
->udp_src_min
)
2351 + pkt_dev
->udp_src_min
;
2354 pkt_dev
->cur_udp_src
++;
2355 if (pkt_dev
->cur_udp_src
>= pkt_dev
->udp_src_max
)
2356 pkt_dev
->cur_udp_src
= pkt_dev
->udp_src_min
;
2360 if (pkt_dev
->udp_dst_min
< pkt_dev
->udp_dst_max
) {
2361 if (pkt_dev
->flags
& F_UDPDST_RND
) {
2362 pkt_dev
->cur_udp_dst
= random32() %
2363 (pkt_dev
->udp_dst_max
- pkt_dev
->udp_dst_min
)
2364 + pkt_dev
->udp_dst_min
;
2366 pkt_dev
->cur_udp_dst
++;
2367 if (pkt_dev
->cur_udp_dst
>= pkt_dev
->udp_dst_max
)
2368 pkt_dev
->cur_udp_dst
= pkt_dev
->udp_dst_min
;
2372 if (!(pkt_dev
->flags
& F_IPV6
)) {
2374 imn
= ntohl(pkt_dev
->saddr_min
);
2375 imx
= ntohl(pkt_dev
->saddr_max
);
2378 if (pkt_dev
->flags
& F_IPSRC_RND
)
2379 t
= random32() % (imx
- imn
) + imn
;
2381 t
= ntohl(pkt_dev
->cur_saddr
);
2387 pkt_dev
->cur_saddr
= htonl(t
);
2390 if (pkt_dev
->cflows
&& f_seen(pkt_dev
, flow
)) {
2391 pkt_dev
->cur_daddr
= pkt_dev
->flows
[flow
].cur_daddr
;
2393 imn
= ntohl(pkt_dev
->daddr_min
);
2394 imx
= ntohl(pkt_dev
->daddr_max
);
2398 if (pkt_dev
->flags
& F_IPDST_RND
) {
2400 t
= random32() % (imx
- imn
) + imn
;
2403 while (ipv4_is_loopback(s
) ||
2404 ipv4_is_multicast(s
) ||
2405 ipv4_is_lbcast(s
) ||
2406 ipv4_is_zeronet(s
) ||
2407 ipv4_is_local_multicast(s
)) {
2408 t
= random32() % (imx
- imn
) + imn
;
2411 pkt_dev
->cur_daddr
= s
;
2413 t
= ntohl(pkt_dev
->cur_daddr
);
2418 pkt_dev
->cur_daddr
= htonl(t
);
2421 if (pkt_dev
->cflows
) {
2422 pkt_dev
->flows
[flow
].flags
|= F_INIT
;
2423 pkt_dev
->flows
[flow
].cur_daddr
=
2426 if (pkt_dev
->flags
& F_IPSEC_ON
)
2427 get_ipsec_sa(pkt_dev
, flow
);
2432 } else { /* IPV6 * */
2434 if (pkt_dev
->min_in6_daddr
.s6_addr32
[0] == 0 &&
2435 pkt_dev
->min_in6_daddr
.s6_addr32
[1] == 0 &&
2436 pkt_dev
->min_in6_daddr
.s6_addr32
[2] == 0 &&
2437 pkt_dev
->min_in6_daddr
.s6_addr32
[3] == 0) ;
2441 /* Only random destinations yet */
2443 for (i
= 0; i
< 4; i
++) {
2444 pkt_dev
->cur_in6_daddr
.s6_addr32
[i
] =
2445 (((__force __be32
)random32() |
2446 pkt_dev
->min_in6_daddr
.s6_addr32
[i
]) &
2447 pkt_dev
->max_in6_daddr
.s6_addr32
[i
]);
2452 if (pkt_dev
->min_pkt_size
< pkt_dev
->max_pkt_size
) {
2454 if (pkt_dev
->flags
& F_TXSIZE_RND
) {
2456 (pkt_dev
->max_pkt_size
- pkt_dev
->min_pkt_size
)
2457 + pkt_dev
->min_pkt_size
;
2459 t
= pkt_dev
->cur_pkt_size
+ 1;
2460 if (t
> pkt_dev
->max_pkt_size
)
2461 t
= pkt_dev
->min_pkt_size
;
2463 pkt_dev
->cur_pkt_size
= t
;
2466 set_cur_queue_map(pkt_dev
);
2468 pkt_dev
->flows
[flow
].count
++;
2473 static int pktgen_output_ipsec(struct sk_buff
*skb
, struct pktgen_dev
*pkt_dev
)
2475 struct xfrm_state
*x
= pkt_dev
->flows
[pkt_dev
->curfl
].x
;
2481 /* XXX: we dont support tunnel mode for now until
2482 * we resolve the dst issue */
2483 if (x
->props
.mode
!= XFRM_MODE_TRANSPORT
)
2486 spin_lock(&x
->lock
);
2489 err
= x
->outer_mode
->output(x
, skb
);
2492 err
= x
->type
->output(x
, skb
);
2496 x
->curlft
.bytes
+= skb
->len
;
2497 x
->curlft
.packets
++;
2499 spin_unlock(&x
->lock
);
2503 static void free_SAs(struct pktgen_dev
*pkt_dev
)
2505 if (pkt_dev
->cflows
) {
2506 /* let go of the SAs if we have them */
2508 for (; i
< pkt_dev
->cflows
; i
++) {
2509 struct xfrm_state
*x
= pkt_dev
->flows
[i
].x
;
2512 pkt_dev
->flows
[i
].x
= NULL
;
2518 static int process_ipsec(struct pktgen_dev
*pkt_dev
,
2519 struct sk_buff
*skb
, __be16 protocol
)
2521 if (pkt_dev
->flags
& F_IPSEC_ON
) {
2522 struct xfrm_state
*x
= pkt_dev
->flows
[pkt_dev
->curfl
].x
;
2527 nhead
= x
->props
.header_len
- skb_headroom(skb
);
2529 ret
= pskb_expand_head(skb
, nhead
, 0, GFP_ATOMIC
);
2531 printk(KERN_ERR
"Error expanding "
2532 "ipsec packet %d\n", ret
);
2537 /* ipsec is not expecting ll header */
2538 skb_pull(skb
, ETH_HLEN
);
2539 ret
= pktgen_output_ipsec(skb
, pkt_dev
);
2541 printk(KERN_ERR
"Error creating ipsec "
2542 "packet %d\n", ret
);
2546 eth
= (__u8
*) skb_push(skb
, ETH_HLEN
);
2547 memcpy(eth
, pkt_dev
->hh
, 12);
2548 *(u16
*) ð
[12] = protocol
;
2558 static void mpls_push(__be32
*mpls
, struct pktgen_dev
*pkt_dev
)
2561 for (i
= 0; i
< pkt_dev
->nr_labels
; i
++)
2562 *mpls
++ = pkt_dev
->labels
[i
] & ~MPLS_STACK_BOTTOM
;
2565 *mpls
|= MPLS_STACK_BOTTOM
;
2568 static inline __be16
build_tci(unsigned int id
, unsigned int cfi
,
2571 return htons(id
| (cfi
<< 12) | (prio
<< 13));
2574 static struct sk_buff
*fill_packet_ipv4(struct net_device
*odev
,
2575 struct pktgen_dev
*pkt_dev
)
2577 struct sk_buff
*skb
= NULL
;
2579 struct udphdr
*udph
;
2582 struct pktgen_hdr
*pgh
= NULL
;
2583 __be16 protocol
= htons(ETH_P_IP
);
2585 __be16
*vlan_tci
= NULL
; /* Encapsulates priority and VLAN ID */
2586 __be16
*vlan_encapsulated_proto
= NULL
; /* packet type ID field (or len) for VLAN tag */
2587 __be16
*svlan_tci
= NULL
; /* Encapsulates priority and SVLAN ID */
2588 __be16
*svlan_encapsulated_proto
= NULL
; /* packet type ID field (or len) for SVLAN tag */
2591 if (pkt_dev
->nr_labels
)
2592 protocol
= htons(ETH_P_MPLS_UC
);
2594 if (pkt_dev
->vlan_id
!= 0xffff)
2595 protocol
= htons(ETH_P_8021Q
);
2597 /* Update any of the values, used when we're incrementing various
2600 queue_map
= pkt_dev
->cur_queue_map
;
2601 mod_cur_headers(pkt_dev
);
2603 datalen
= (odev
->hard_header_len
+ 16) & ~0xf;
2605 if (pkt_dev
->flags
& F_NODE
) {
2608 if (pkt_dev
->node
>= 0)
2609 node
= pkt_dev
->node
;
2611 node
= numa_node_id();
2613 skb
= __alloc_skb(NET_SKB_PAD
+ pkt_dev
->cur_pkt_size
+ 64
2614 + datalen
+ pkt_dev
->pkt_overhead
, GFP_NOWAIT
, 0, node
);
2616 skb_reserve(skb
, NET_SKB_PAD
);
2621 skb
= __netdev_alloc_skb(odev
,
2622 pkt_dev
->cur_pkt_size
+ 64
2623 + datalen
+ pkt_dev
->pkt_overhead
, GFP_NOWAIT
);
2626 sprintf(pkt_dev
->result
, "No memory");
2630 skb_reserve(skb
, datalen
);
2632 /* Reserve for ethernet and IP header */
2633 eth
= (__u8
*) skb_push(skb
, 14);
2634 mpls
= (__be32
*)skb_put(skb
, pkt_dev
->nr_labels
*sizeof(__u32
));
2635 if (pkt_dev
->nr_labels
)
2636 mpls_push(mpls
, pkt_dev
);
2638 if (pkt_dev
->vlan_id
!= 0xffff) {
2639 if (pkt_dev
->svlan_id
!= 0xffff) {
2640 svlan_tci
= (__be16
*)skb_put(skb
, sizeof(__be16
));
2641 *svlan_tci
= build_tci(pkt_dev
->svlan_id
,
2644 svlan_encapsulated_proto
= (__be16
*)skb_put(skb
, sizeof(__be16
));
2645 *svlan_encapsulated_proto
= htons(ETH_P_8021Q
);
2647 vlan_tci
= (__be16
*)skb_put(skb
, sizeof(__be16
));
2648 *vlan_tci
= build_tci(pkt_dev
->vlan_id
,
2651 vlan_encapsulated_proto
= (__be16
*)skb_put(skb
, sizeof(__be16
));
2652 *vlan_encapsulated_proto
= htons(ETH_P_IP
);
2655 skb
->network_header
= skb
->tail
;
2656 skb
->transport_header
= skb
->network_header
+ sizeof(struct iphdr
);
2657 skb_put(skb
, sizeof(struct iphdr
) + sizeof(struct udphdr
));
2658 skb_set_queue_mapping(skb
, queue_map
);
2660 udph
= udp_hdr(skb
);
2662 memcpy(eth
, pkt_dev
->hh
, 12);
2663 *(__be16
*) & eth
[12] = protocol
;
2665 /* Eth + IPh + UDPh + mpls */
2666 datalen
= pkt_dev
->cur_pkt_size
- 14 - 20 - 8 -
2667 pkt_dev
->pkt_overhead
;
2668 if (datalen
< sizeof(struct pktgen_hdr
))
2669 datalen
= sizeof(struct pktgen_hdr
);
2671 udph
->source
= htons(pkt_dev
->cur_udp_src
);
2672 udph
->dest
= htons(pkt_dev
->cur_udp_dst
);
2673 udph
->len
= htons(datalen
+ 8); /* DATA + udphdr */
2674 udph
->check
= 0; /* No checksum */
2679 iph
->tos
= pkt_dev
->tos
;
2680 iph
->protocol
= IPPROTO_UDP
; /* UDP */
2681 iph
->saddr
= pkt_dev
->cur_saddr
;
2682 iph
->daddr
= pkt_dev
->cur_daddr
;
2683 iph
->id
= htons(pkt_dev
->ip_id
);
2686 iplen
= 20 + 8 + datalen
;
2687 iph
->tot_len
= htons(iplen
);
2689 iph
->check
= ip_fast_csum((void *)iph
, iph
->ihl
);
2690 skb
->protocol
= protocol
;
2691 skb
->mac_header
= (skb
->network_header
- ETH_HLEN
-
2692 pkt_dev
->pkt_overhead
);
2694 skb
->pkt_type
= PACKET_HOST
;
2696 if (pkt_dev
->nfrags
<= 0) {
2697 pgh
= (struct pktgen_hdr
*)skb_put(skb
, datalen
);
2698 memset(pgh
+ 1, 0, datalen
- sizeof(struct pktgen_hdr
));
2700 int frags
= pkt_dev
->nfrags
;
2703 pgh
= (struct pktgen_hdr
*)(((char *)(udph
)) + 8);
2705 if (frags
> MAX_SKB_FRAGS
)
2706 frags
= MAX_SKB_FRAGS
;
2707 if (datalen
> frags
* PAGE_SIZE
) {
2708 len
= datalen
- frags
* PAGE_SIZE
;
2709 memset(skb_put(skb
, len
), 0, len
);
2710 datalen
= frags
* PAGE_SIZE
;
2714 while (datalen
> 0) {
2715 struct page
*page
= alloc_pages(GFP_KERNEL
| __GFP_ZERO
, 0);
2716 skb_shinfo(skb
)->frags
[i
].page
= page
;
2717 skb_shinfo(skb
)->frags
[i
].page_offset
= 0;
2718 skb_shinfo(skb
)->frags
[i
].size
=
2719 (datalen
< PAGE_SIZE
? datalen
: PAGE_SIZE
);
2720 datalen
-= skb_shinfo(skb
)->frags
[i
].size
;
2721 skb
->len
+= skb_shinfo(skb
)->frags
[i
].size
;
2722 skb
->data_len
+= skb_shinfo(skb
)->frags
[i
].size
;
2724 skb_shinfo(skb
)->nr_frags
= i
;
2733 rem
= skb_shinfo(skb
)->frags
[i
- 1].size
/ 2;
2737 skb_shinfo(skb
)->frags
[i
- 1].size
-= rem
;
2739 skb_shinfo(skb
)->frags
[i
] =
2740 skb_shinfo(skb
)->frags
[i
- 1];
2741 get_page(skb_shinfo(skb
)->frags
[i
].page
);
2742 skb_shinfo(skb
)->frags
[i
].page
=
2743 skb_shinfo(skb
)->frags
[i
- 1].page
;
2744 skb_shinfo(skb
)->frags
[i
].page_offset
+=
2745 skb_shinfo(skb
)->frags
[i
- 1].size
;
2746 skb_shinfo(skb
)->frags
[i
].size
= rem
;
2748 skb_shinfo(skb
)->nr_frags
= i
;
2752 /* Stamp the time, and sequence number,
2753 * convert them to network byte order
2756 struct timeval timestamp
;
2758 pgh
->pgh_magic
= htonl(PKTGEN_MAGIC
);
2759 pgh
->seq_num
= htonl(pkt_dev
->seq_num
);
2761 do_gettimeofday(×tamp
);
2762 pgh
->tv_sec
= htonl(timestamp
.tv_sec
);
2763 pgh
->tv_usec
= htonl(timestamp
.tv_usec
);
2767 if (!process_ipsec(pkt_dev
, skb
, protocol
))
2775 * scan_ip6, fmt_ip taken from dietlibc-0.21
2776 * Author Felix von Leitner <felix-dietlibc@fefe.de>
2778 * Slightly modified for kernel.
2779 * Should be candidate for net/ipv4/utils.c
2783 static unsigned int scan_ip6(const char *s
, char ip
[16])
2786 unsigned int len
= 0;
2789 unsigned int prefixlen
= 0;
2790 unsigned int suffixlen
= 0;
2794 for (i
= 0; i
< 16; i
++)
2800 if (s
[1] == ':') { /* Found "::", skip to part 2 */
2808 u
= simple_strtoul(s
, &pos
, 16);
2812 if (prefixlen
== 12 && s
[i
] == '.') {
2814 /* the last 4 bytes may be written as IPv4 address */
2817 memcpy((struct in_addr
*)(ip
+ 12), &tmp
, sizeof(tmp
));
2820 ip
[prefixlen
++] = (u
>> 8);
2821 ip
[prefixlen
++] = (u
& 255);
2824 if (prefixlen
== 16)
2828 /* part 2, after "::" */
2835 } else if (suffixlen
!= 0)
2838 u
= simple_strtol(s
, &pos
, 16);
2845 if (suffixlen
+ prefixlen
<= 12 && s
[i
] == '.') {
2847 memcpy((struct in_addr
*)(suffix
+ suffixlen
), &tmp
,
2853 suffix
[suffixlen
++] = (u
>> 8);
2854 suffix
[suffixlen
++] = (u
& 255);
2857 if (prefixlen
+ suffixlen
== 16)
2860 for (i
= 0; i
< suffixlen
; i
++)
2861 ip
[16 - suffixlen
+ i
] = suffix
[i
];
2865 static char tohex(char hexdigit
)
2867 return hexdigit
> 9 ? hexdigit
+ 'a' - 10 : hexdigit
+ '0';
2870 static int fmt_xlong(char *s
, unsigned int i
)
2873 *s
= tohex((i
>> 12) & 0xf);
2874 if (s
!= bak
|| *s
!= '0')
2876 *s
= tohex((i
>> 8) & 0xf);
2877 if (s
!= bak
|| *s
!= '0')
2879 *s
= tohex((i
>> 4) & 0xf);
2880 if (s
!= bak
|| *s
!= '0')
2882 *s
= tohex(i
& 0xf);
2886 static unsigned int fmt_ip6(char *s
, const char ip
[16])
2891 unsigned int compressing
;
2896 for (j
= 0; j
< 16; j
+= 2) {
2898 #ifdef V4MAPPEDPREFIX
2899 if (j
== 12 && !memcmp(ip
, V4mappedprefix
, 12)) {
2900 inet_ntoa_r(*(struct in_addr
*)(ip
+ 12), s
);
2905 temp
= ((unsigned long)(unsigned char)ip
[j
] << 8) +
2906 (unsigned long)(unsigned char)ip
[j
+ 1];
2921 i
= fmt_xlong(s
, temp
);
2938 static struct sk_buff
*fill_packet_ipv6(struct net_device
*odev
,
2939 struct pktgen_dev
*pkt_dev
)
2941 struct sk_buff
*skb
= NULL
;
2943 struct udphdr
*udph
;
2945 struct ipv6hdr
*iph
;
2946 struct pktgen_hdr
*pgh
= NULL
;
2947 __be16 protocol
= htons(ETH_P_IPV6
);
2949 __be16
*vlan_tci
= NULL
; /* Encapsulates priority and VLAN ID */
2950 __be16
*vlan_encapsulated_proto
= NULL
; /* packet type ID field (or len) for VLAN tag */
2951 __be16
*svlan_tci
= NULL
; /* Encapsulates priority and SVLAN ID */
2952 __be16
*svlan_encapsulated_proto
= NULL
; /* packet type ID field (or len) for SVLAN tag */
2955 if (pkt_dev
->nr_labels
)
2956 protocol
= htons(ETH_P_MPLS_UC
);
2958 if (pkt_dev
->vlan_id
!= 0xffff)
2959 protocol
= htons(ETH_P_8021Q
);
2961 /* Update any of the values, used when we're incrementing various
2964 queue_map
= pkt_dev
->cur_queue_map
;
2965 mod_cur_headers(pkt_dev
);
2967 skb
= __netdev_alloc_skb(odev
,
2968 pkt_dev
->cur_pkt_size
+ 64
2969 + 16 + pkt_dev
->pkt_overhead
, GFP_NOWAIT
);
2971 sprintf(pkt_dev
->result
, "No memory");
2975 skb_reserve(skb
, 16);
2977 /* Reserve for ethernet and IP header */
2978 eth
= (__u8
*) skb_push(skb
, 14);
2979 mpls
= (__be32
*)skb_put(skb
, pkt_dev
->nr_labels
*sizeof(__u32
));
2980 if (pkt_dev
->nr_labels
)
2981 mpls_push(mpls
, pkt_dev
);
2983 if (pkt_dev
->vlan_id
!= 0xffff) {
2984 if (pkt_dev
->svlan_id
!= 0xffff) {
2985 svlan_tci
= (__be16
*)skb_put(skb
, sizeof(__be16
));
2986 *svlan_tci
= build_tci(pkt_dev
->svlan_id
,
2989 svlan_encapsulated_proto
= (__be16
*)skb_put(skb
, sizeof(__be16
));
2990 *svlan_encapsulated_proto
= htons(ETH_P_8021Q
);
2992 vlan_tci
= (__be16
*)skb_put(skb
, sizeof(__be16
));
2993 *vlan_tci
= build_tci(pkt_dev
->vlan_id
,
2996 vlan_encapsulated_proto
= (__be16
*)skb_put(skb
, sizeof(__be16
));
2997 *vlan_encapsulated_proto
= htons(ETH_P_IPV6
);
3000 skb
->network_header
= skb
->tail
;
3001 skb
->transport_header
= skb
->network_header
+ sizeof(struct ipv6hdr
);
3002 skb_put(skb
, sizeof(struct ipv6hdr
) + sizeof(struct udphdr
));
3003 skb_set_queue_mapping(skb
, queue_map
);
3004 iph
= ipv6_hdr(skb
);
3005 udph
= udp_hdr(skb
);
3007 memcpy(eth
, pkt_dev
->hh
, 12);
3008 *(__be16
*) ð
[12] = protocol
;
3010 /* Eth + IPh + UDPh + mpls */
3011 datalen
= pkt_dev
->cur_pkt_size
- 14 -
3012 sizeof(struct ipv6hdr
) - sizeof(struct udphdr
) -
3013 pkt_dev
->pkt_overhead
;
3015 if (datalen
< sizeof(struct pktgen_hdr
)) {
3016 datalen
= sizeof(struct pktgen_hdr
);
3017 if (net_ratelimit())
3018 printk(KERN_INFO
"pktgen: increased datalen to %d\n",
3022 udph
->source
= htons(pkt_dev
->cur_udp_src
);
3023 udph
->dest
= htons(pkt_dev
->cur_udp_dst
);
3024 udph
->len
= htons(datalen
+ sizeof(struct udphdr
));
3025 udph
->check
= 0; /* No checksum */
3027 *(__be32
*) iph
= htonl(0x60000000); /* Version + flow */
3029 if (pkt_dev
->traffic_class
) {
3030 /* Version + traffic class + flow (0) */
3031 *(__be32
*)iph
|= htonl(0x60000000 | (pkt_dev
->traffic_class
<< 20));
3034 iph
->hop_limit
= 32;
3036 iph
->payload_len
= htons(sizeof(struct udphdr
) + datalen
);
3037 iph
->nexthdr
= IPPROTO_UDP
;
3039 ipv6_addr_copy(&iph
->daddr
, &pkt_dev
->cur_in6_daddr
);
3040 ipv6_addr_copy(&iph
->saddr
, &pkt_dev
->cur_in6_saddr
);
3042 skb
->mac_header
= (skb
->network_header
- ETH_HLEN
-
3043 pkt_dev
->pkt_overhead
);
3044 skb
->protocol
= protocol
;
3046 skb
->pkt_type
= PACKET_HOST
;
3048 if (pkt_dev
->nfrags
<= 0)
3049 pgh
= (struct pktgen_hdr
*)skb_put(skb
, datalen
);
3051 int frags
= pkt_dev
->nfrags
;
3054 pgh
= (struct pktgen_hdr
*)(((char *)(udph
)) + 8);
3056 if (frags
> MAX_SKB_FRAGS
)
3057 frags
= MAX_SKB_FRAGS
;
3058 if (datalen
> frags
* PAGE_SIZE
) {
3059 skb_put(skb
, datalen
- frags
* PAGE_SIZE
);
3060 datalen
= frags
* PAGE_SIZE
;
3064 while (datalen
> 0) {
3065 struct page
*page
= alloc_pages(GFP_KERNEL
, 0);
3066 skb_shinfo(skb
)->frags
[i
].page
= page
;
3067 skb_shinfo(skb
)->frags
[i
].page_offset
= 0;
3068 skb_shinfo(skb
)->frags
[i
].size
=
3069 (datalen
< PAGE_SIZE
? datalen
: PAGE_SIZE
);
3070 datalen
-= skb_shinfo(skb
)->frags
[i
].size
;
3071 skb
->len
+= skb_shinfo(skb
)->frags
[i
].size
;
3072 skb
->data_len
+= skb_shinfo(skb
)->frags
[i
].size
;
3074 skb_shinfo(skb
)->nr_frags
= i
;
3083 rem
= skb_shinfo(skb
)->frags
[i
- 1].size
/ 2;
3087 skb_shinfo(skb
)->frags
[i
- 1].size
-= rem
;
3089 skb_shinfo(skb
)->frags
[i
] =
3090 skb_shinfo(skb
)->frags
[i
- 1];
3091 get_page(skb_shinfo(skb
)->frags
[i
].page
);
3092 skb_shinfo(skb
)->frags
[i
].page
=
3093 skb_shinfo(skb
)->frags
[i
- 1].page
;
3094 skb_shinfo(skb
)->frags
[i
].page_offset
+=
3095 skb_shinfo(skb
)->frags
[i
- 1].size
;
3096 skb_shinfo(skb
)->frags
[i
].size
= rem
;
3098 skb_shinfo(skb
)->nr_frags
= i
;
3102 /* Stamp the time, and sequence number,
3103 * convert them to network byte order
3104 * should we update cloned packets too ?
3107 struct timeval timestamp
;
3109 pgh
->pgh_magic
= htonl(PKTGEN_MAGIC
);
3110 pgh
->seq_num
= htonl(pkt_dev
->seq_num
);
3112 do_gettimeofday(×tamp
);
3113 pgh
->tv_sec
= htonl(timestamp
.tv_sec
);
3114 pgh
->tv_usec
= htonl(timestamp
.tv_usec
);
3116 /* pkt_dev->seq_num++; FF: you really mean this? */
3121 static struct sk_buff
*fill_packet(struct net_device
*odev
,
3122 struct pktgen_dev
*pkt_dev
)
3124 if (pkt_dev
->flags
& F_IPV6
)
3125 return fill_packet_ipv6(odev
, pkt_dev
);
3127 return fill_packet_ipv4(odev
, pkt_dev
);
3130 static void pktgen_clear_counters(struct pktgen_dev
*pkt_dev
)
3132 pkt_dev
->seq_num
= 1;
3133 pkt_dev
->idle_acc
= 0;
3135 pkt_dev
->tx_bytes
= 0;
3136 pkt_dev
->errors
= 0;
3139 /* Set up structure for sending pkts, clear counters */
3141 static void pktgen_run(struct pktgen_thread
*t
)
3143 struct pktgen_dev
*pkt_dev
;
3146 pr_debug("pktgen: entering pktgen_run. %p\n", t
);
3149 list_for_each_entry(pkt_dev
, &t
->if_list
, list
) {
3152 * setup odev and create initial packet.
3154 pktgen_setup_inject(pkt_dev
);
3156 if (pkt_dev
->odev
) {
3157 pktgen_clear_counters(pkt_dev
);
3158 pkt_dev
->running
= 1; /* Cranke yeself! */
3159 pkt_dev
->skb
= NULL
;
3160 pkt_dev
->started_at
=
3161 pkt_dev
->next_tx
= ktime_now();
3163 set_pkt_overhead(pkt_dev
);
3165 strcpy(pkt_dev
->result
, "Starting");
3168 strcpy(pkt_dev
->result
, "Error starting");
3172 t
->control
&= ~(T_STOP
);
3175 static void pktgen_stop_all_threads_ifs(void)
3177 struct pktgen_thread
*t
;
3179 pr_debug("pktgen: entering pktgen_stop_all_threads_ifs.\n");
3181 mutex_lock(&pktgen_thread_lock
);
3183 list_for_each_entry(t
, &pktgen_threads
, th_list
)
3184 t
->control
|= T_STOP
;
3186 mutex_unlock(&pktgen_thread_lock
);
3189 static int thread_is_running(const struct pktgen_thread
*t
)
3191 const struct pktgen_dev
*pkt_dev
;
3193 list_for_each_entry(pkt_dev
, &t
->if_list
, list
)
3194 if (pkt_dev
->running
)
3199 static int pktgen_wait_thread_run(struct pktgen_thread
*t
)
3203 while (thread_is_running(t
)) {
3207 msleep_interruptible(100);
3209 if (signal_pending(current
))
3219 static int pktgen_wait_all_threads_run(void)
3221 struct pktgen_thread
*t
;
3224 mutex_lock(&pktgen_thread_lock
);
3226 list_for_each_entry(t
, &pktgen_threads
, th_list
) {
3227 sig
= pktgen_wait_thread_run(t
);
3233 list_for_each_entry(t
, &pktgen_threads
, th_list
)
3234 t
->control
|= (T_STOP
);
3236 mutex_unlock(&pktgen_thread_lock
);
3240 static void pktgen_run_all_threads(void)
3242 struct pktgen_thread
*t
;
3244 pr_debug("pktgen: entering pktgen_run_all_threads.\n");
3246 mutex_lock(&pktgen_thread_lock
);
3248 list_for_each_entry(t
, &pktgen_threads
, th_list
)
3249 t
->control
|= (T_RUN
);
3251 mutex_unlock(&pktgen_thread_lock
);
3253 /* Propagate thread->control */
3254 schedule_timeout_interruptible(msecs_to_jiffies(125));
3256 pktgen_wait_all_threads_run();
3259 static void pktgen_reset_all_threads(void)
3261 struct pktgen_thread
*t
;
3263 pr_debug("pktgen: entering pktgen_reset_all_threads.\n");
3265 mutex_lock(&pktgen_thread_lock
);
3267 list_for_each_entry(t
, &pktgen_threads
, th_list
)
3268 t
->control
|= (T_REMDEVALL
);
3270 mutex_unlock(&pktgen_thread_lock
);
3272 /* Propagate thread->control */
3273 schedule_timeout_interruptible(msecs_to_jiffies(125));
3275 pktgen_wait_all_threads_run();
3278 static void show_results(struct pktgen_dev
*pkt_dev
, int nr_frags
)
3280 __u64 bps
, mbps
, pps
;
3281 char *p
= pkt_dev
->result
;
3282 ktime_t elapsed
= ktime_sub(pkt_dev
->stopped_at
,
3283 pkt_dev
->started_at
);
3284 ktime_t idle
= ns_to_ktime(pkt_dev
->idle_acc
);
3286 p
+= sprintf(p
, "OK: %llu(c%llu+d%llu) nsec, %llu (%dbyte,%dfrags)\n",
3287 (unsigned long long)ktime_to_us(elapsed
),
3288 (unsigned long long)ktime_to_us(ktime_sub(elapsed
, idle
)),
3289 (unsigned long long)ktime_to_us(idle
),
3290 (unsigned long long)pkt_dev
->sofar
,
3291 pkt_dev
->cur_pkt_size
, nr_frags
);
3293 pps
= div64_u64(pkt_dev
->sofar
* NSEC_PER_SEC
,
3294 ktime_to_ns(elapsed
));
3296 bps
= pps
* 8 * pkt_dev
->cur_pkt_size
;
3299 do_div(mbps
, 1000000);
3300 p
+= sprintf(p
, " %llupps %lluMb/sec (%llubps) errors: %llu",
3301 (unsigned long long)pps
,
3302 (unsigned long long)mbps
,
3303 (unsigned long long)bps
,
3304 (unsigned long long)pkt_dev
->errors
);
3307 /* Set stopped-at timer, remove from running list, do counters & statistics */
3308 static int pktgen_stop_device(struct pktgen_dev
*pkt_dev
)
3310 int nr_frags
= pkt_dev
->skb
? skb_shinfo(pkt_dev
->skb
)->nr_frags
: -1;
3312 if (!pkt_dev
->running
) {
3313 printk(KERN_WARNING
"pktgen: interface: %s is already "
3314 "stopped\n", pkt_dev
->odevname
);
3318 kfree_skb(pkt_dev
->skb
);
3319 pkt_dev
->skb
= NULL
;
3320 pkt_dev
->stopped_at
= ktime_now();
3321 pkt_dev
->running
= 0;
3323 show_results(pkt_dev
, nr_frags
);
3328 static struct pktgen_dev
*next_to_run(struct pktgen_thread
*t
)
3330 struct pktgen_dev
*pkt_dev
, *best
= NULL
;
3334 list_for_each_entry(pkt_dev
, &t
->if_list
, list
) {
3335 if (!pkt_dev
->running
)
3339 else if (ktime_lt(pkt_dev
->next_tx
, best
->next_tx
))
3346 static void pktgen_stop(struct pktgen_thread
*t
)
3348 struct pktgen_dev
*pkt_dev
;
3350 pr_debug("pktgen: entering pktgen_stop\n");
3354 list_for_each_entry(pkt_dev
, &t
->if_list
, list
) {
3355 pktgen_stop_device(pkt_dev
);
3362 * one of our devices needs to be removed - find it
3365 static void pktgen_rem_one_if(struct pktgen_thread
*t
)
3367 struct list_head
*q
, *n
;
3368 struct pktgen_dev
*cur
;
3370 pr_debug("pktgen: entering pktgen_rem_one_if\n");
3374 list_for_each_safe(q
, n
, &t
->if_list
) {
3375 cur
= list_entry(q
, struct pktgen_dev
, list
);
3377 if (!cur
->removal_mark
)
3380 kfree_skb(cur
->skb
);
3383 pktgen_remove_device(t
, cur
);
3391 static void pktgen_rem_all_ifs(struct pktgen_thread
*t
)
3393 struct list_head
*q
, *n
;
3394 struct pktgen_dev
*cur
;
3396 /* Remove all devices, free mem */
3398 pr_debug("pktgen: entering pktgen_rem_all_ifs\n");
3401 list_for_each_safe(q
, n
, &t
->if_list
) {
3402 cur
= list_entry(q
, struct pktgen_dev
, list
);
3404 kfree_skb(cur
->skb
);
3407 pktgen_remove_device(t
, cur
);
3413 static void pktgen_rem_thread(struct pktgen_thread
*t
)
3415 /* Remove from the thread list */
3417 remove_proc_entry(t
->tsk
->comm
, pg_proc_dir
);
3419 mutex_lock(&pktgen_thread_lock
);
3421 list_del(&t
->th_list
);
3423 mutex_unlock(&pktgen_thread_lock
);
3426 static void pktgen_resched(struct pktgen_dev
*pkt_dev
)
3428 ktime_t idle_start
= ktime_now();
3430 pkt_dev
->idle_acc
+= ktime_to_ns(ktime_sub(ktime_now(), idle_start
));
3433 static void pktgen_wait_for_skb(struct pktgen_dev
*pkt_dev
)
3435 ktime_t idle_start
= ktime_now();
3437 while (atomic_read(&(pkt_dev
->skb
->users
)) != 1) {
3438 if (signal_pending(current
))
3442 pktgen_resched(pkt_dev
);
3446 pkt_dev
->idle_acc
+= ktime_to_ns(ktime_sub(ktime_now(), idle_start
));
3449 static void pktgen_xmit(struct pktgen_dev
*pkt_dev
)
3451 struct net_device
*odev
= pkt_dev
->odev
;
3452 netdev_tx_t (*xmit
)(struct sk_buff
*, struct net_device
*)
3453 = odev
->netdev_ops
->ndo_start_xmit
;
3454 struct netdev_queue
*txq
;
3458 /* If device is offline, then don't send */
3459 if (unlikely(!netif_running(odev
) || !netif_carrier_ok(odev
))) {
3460 pktgen_stop_device(pkt_dev
);
3464 /* This is max DELAY, this has special meaning of
3467 if (unlikely(pkt_dev
->delay
== ULLONG_MAX
)) {
3468 pkt_dev
->next_tx
= ktime_add_ns(ktime_now(), ULONG_MAX
);
3472 /* If no skb or clone count exhausted then get new one */
3473 if (!pkt_dev
->skb
|| (pkt_dev
->last_ok
&&
3474 ++pkt_dev
->clone_count
>= pkt_dev
->clone_skb
)) {
3475 /* build a new pkt */
3476 kfree_skb(pkt_dev
->skb
);
3478 pkt_dev
->skb
= fill_packet(odev
, pkt_dev
);
3479 if (pkt_dev
->skb
== NULL
) {
3480 printk(KERN_ERR
"pktgen: ERROR: couldn't "
3481 "allocate skb in fill_packet.\n");
3483 pkt_dev
->clone_count
--; /* back out increment, OOM */
3486 pkt_dev
->last_pkt_size
= pkt_dev
->skb
->len
;
3487 pkt_dev
->allocated_skbs
++;
3488 pkt_dev
->clone_count
= 0; /* reset counter */
3491 if (pkt_dev
->delay
&& pkt_dev
->last_ok
)
3492 spin(pkt_dev
, pkt_dev
->next_tx
);
3494 queue_map
= skb_get_queue_mapping(pkt_dev
->skb
);
3495 txq
= netdev_get_tx_queue(odev
, queue_map
);
3497 __netif_tx_lock_bh(txq
);
3499 if (unlikely(netif_tx_queue_stopped(txq
) || netif_tx_queue_frozen(txq
))) {
3500 ret
= NETDEV_TX_BUSY
;
3501 pkt_dev
->last_ok
= 0;
3504 atomic_inc(&(pkt_dev
->skb
->users
));
3505 ret
= (*xmit
)(pkt_dev
->skb
, odev
);
3509 txq_trans_update(txq
);
3510 pkt_dev
->last_ok
= 1;
3513 pkt_dev
->tx_bytes
+= pkt_dev
->last_pkt_size
;
3517 case NET_XMIT_POLICED
:
3518 /* skb has been consumed */
3521 default: /* Drivers are not supposed to return other values! */
3522 if (net_ratelimit())
3523 pr_info("pktgen: %s xmit error: %d\n",
3524 pkt_dev
->odevname
, ret
);
3527 case NETDEV_TX_LOCKED
:
3528 case NETDEV_TX_BUSY
:
3529 /* Retry it next time */
3530 atomic_dec(&(pkt_dev
->skb
->users
));
3531 pkt_dev
->last_ok
= 0;
3534 __netif_tx_unlock_bh(txq
);
3536 /* If pkt_dev->count is zero, then run forever */
3537 if ((pkt_dev
->count
!= 0) && (pkt_dev
->sofar
>= pkt_dev
->count
)) {
3538 pktgen_wait_for_skb(pkt_dev
);
3540 /* Done with this */
3541 pktgen_stop_device(pkt_dev
);
3546 * Main loop of the thread goes here
3549 static int pktgen_thread_worker(void *arg
)
3552 struct pktgen_thread
*t
= arg
;
3553 struct pktgen_dev
*pkt_dev
= NULL
;
3556 BUG_ON(smp_processor_id() != cpu
);
3558 init_waitqueue_head(&t
->queue
);
3559 complete(&t
->start_done
);
3561 pr_debug("pktgen: starting pktgen/%d: pid=%d\n",
3562 cpu
, task_pid_nr(current
));
3564 set_current_state(TASK_INTERRUPTIBLE
);
3568 while (!kthread_should_stop()) {
3569 pkt_dev
= next_to_run(t
);
3571 if (unlikely(!pkt_dev
&& t
->control
== 0)) {
3572 wait_event_interruptible_timeout(t
->queue
,
3579 __set_current_state(TASK_RUNNING
);
3581 if (likely(pkt_dev
)) {
3582 pktgen_xmit(pkt_dev
);
3585 pktgen_resched(pkt_dev
);
3590 if (t
->control
& T_STOP
) {
3592 t
->control
&= ~(T_STOP
);
3595 if (t
->control
& T_RUN
) {
3597 t
->control
&= ~(T_RUN
);
3600 if (t
->control
& T_REMDEVALL
) {
3601 pktgen_rem_all_ifs(t
);
3602 t
->control
&= ~(T_REMDEVALL
);
3605 if (t
->control
& T_REMDEV
) {
3606 pktgen_rem_one_if(t
);
3607 t
->control
&= ~(T_REMDEV
);
3612 set_current_state(TASK_INTERRUPTIBLE
);
3615 pr_debug("pktgen: %s stopping all device\n", t
->tsk
->comm
);
3618 pr_debug("pktgen: %s removing all device\n", t
->tsk
->comm
);
3619 pktgen_rem_all_ifs(t
);
3621 pr_debug("pktgen: %s removing thread.\n", t
->tsk
->comm
);
3622 pktgen_rem_thread(t
);
3627 static struct pktgen_dev
*pktgen_find_dev(struct pktgen_thread
*t
,
3628 const char *ifname
, bool exact
)
3630 struct pktgen_dev
*p
, *pkt_dev
= NULL
;
3631 size_t len
= strlen(ifname
);
3634 list_for_each_entry(p
, &t
->if_list
, list
)
3635 if (strncmp(p
->odevname
, ifname
, len
) == 0) {
3636 if (p
->odevname
[len
]) {
3637 if (exact
|| p
->odevname
[len
] != '@')
3645 pr_debug("pktgen: find_dev(%s) returning %p\n", ifname
, pkt_dev
);
3650 * Adds a dev at front of if_list.
3653 static int add_dev_to_thread(struct pktgen_thread
*t
,
3654 struct pktgen_dev
*pkt_dev
)
3660 if (pkt_dev
->pg_thread
) {
3661 printk(KERN_ERR
"pktgen: ERROR: already assigned "
3667 list_add(&pkt_dev
->list
, &t
->if_list
);
3668 pkt_dev
->pg_thread
= t
;
3669 pkt_dev
->running
= 0;
3676 /* Called under thread lock */
3678 static int pktgen_add_device(struct pktgen_thread
*t
, const char *ifname
)
3680 struct pktgen_dev
*pkt_dev
;
3682 int node
= cpu_to_node(t
->cpu
);
3684 /* We don't allow a device to be on several threads */
3686 pkt_dev
= __pktgen_NN_threads(ifname
, FIND
);
3688 printk(KERN_ERR
"pktgen: ERROR: interface already used.\n");
3692 pkt_dev
= kzalloc_node(sizeof(struct pktgen_dev
), GFP_KERNEL
, node
);
3696 strcpy(pkt_dev
->odevname
, ifname
);
3697 pkt_dev
->flows
= vmalloc_node(MAX_CFLOWS
* sizeof(struct flow_state
),
3699 if (pkt_dev
->flows
== NULL
) {
3703 memset(pkt_dev
->flows
, 0, MAX_CFLOWS
* sizeof(struct flow_state
));
3705 pkt_dev
->removal_mark
= 0;
3706 pkt_dev
->min_pkt_size
= ETH_ZLEN
;
3707 pkt_dev
->max_pkt_size
= ETH_ZLEN
;
3708 pkt_dev
->nfrags
= 0;
3709 pkt_dev
->clone_skb
= pg_clone_skb_d
;
3710 pkt_dev
->delay
= pg_delay_d
;
3711 pkt_dev
->count
= pg_count_d
;
3713 pkt_dev
->udp_src_min
= 9; /* sink port */
3714 pkt_dev
->udp_src_max
= 9;
3715 pkt_dev
->udp_dst_min
= 9;
3716 pkt_dev
->udp_dst_max
= 9;
3718 pkt_dev
->vlan_p
= 0;
3719 pkt_dev
->vlan_cfi
= 0;
3720 pkt_dev
->vlan_id
= 0xffff;
3721 pkt_dev
->svlan_p
= 0;
3722 pkt_dev
->svlan_cfi
= 0;
3723 pkt_dev
->svlan_id
= 0xffff;
3726 err
= pktgen_setup_dev(pkt_dev
, ifname
);
3730 pkt_dev
->entry
= proc_create_data(ifname
, 0600, pg_proc_dir
,
3731 &pktgen_if_fops
, pkt_dev
);
3732 if (!pkt_dev
->entry
) {
3733 printk(KERN_ERR
"pktgen: cannot create %s/%s procfs entry.\n",
3734 PG_PROC_DIR
, ifname
);
3739 pkt_dev
->ipsmode
= XFRM_MODE_TRANSPORT
;
3740 pkt_dev
->ipsproto
= IPPROTO_ESP
;
3743 return add_dev_to_thread(t
, pkt_dev
);
3745 dev_put(pkt_dev
->odev
);
3750 vfree(pkt_dev
->flows
);
3755 static int __init
pktgen_create_thread(int cpu
)
3757 struct pktgen_thread
*t
;
3758 struct proc_dir_entry
*pe
;
3759 struct task_struct
*p
;
3761 t
= kzalloc_node(sizeof(struct pktgen_thread
), GFP_KERNEL
,
3764 printk(KERN_ERR
"pktgen: ERROR: out of memory, can't "
3765 "create new thread.\n");
3769 spin_lock_init(&t
->if_lock
);
3772 INIT_LIST_HEAD(&t
->if_list
);
3774 list_add_tail(&t
->th_list
, &pktgen_threads
);
3775 init_completion(&t
->start_done
);
3777 p
= kthread_create(pktgen_thread_worker
, t
, "kpktgend_%d", cpu
);
3779 printk(KERN_ERR
"pktgen: kernel_thread() failed "
3780 "for cpu %d\n", t
->cpu
);
3781 list_del(&t
->th_list
);
3785 kthread_bind(p
, cpu
);
3788 pe
= proc_create_data(t
->tsk
->comm
, 0600, pg_proc_dir
,
3789 &pktgen_thread_fops
, t
);
3791 printk(KERN_ERR
"pktgen: cannot create %s/%s procfs entry.\n",
3792 PG_PROC_DIR
, t
->tsk
->comm
);
3794 list_del(&t
->th_list
);
3800 wait_for_completion(&t
->start_done
);
3806 * Removes a device from the thread if_list.
3808 static void _rem_dev_from_if_list(struct pktgen_thread
*t
,
3809 struct pktgen_dev
*pkt_dev
)
3811 struct list_head
*q
, *n
;
3812 struct pktgen_dev
*p
;
3814 list_for_each_safe(q
, n
, &t
->if_list
) {
3815 p
= list_entry(q
, struct pktgen_dev
, list
);
3821 static int pktgen_remove_device(struct pktgen_thread
*t
,
3822 struct pktgen_dev
*pkt_dev
)
3825 pr_debug("pktgen: remove_device pkt_dev=%p\n", pkt_dev
);
3827 if (pkt_dev
->running
) {
3828 printk(KERN_WARNING
"pktgen: WARNING: trying to remove a "
3829 "running interface, stopping it now.\n");
3830 pktgen_stop_device(pkt_dev
);
3833 /* Dis-associate from the interface */
3835 if (pkt_dev
->odev
) {
3836 dev_put(pkt_dev
->odev
);
3837 pkt_dev
->odev
= NULL
;
3840 /* And update the thread if_list */
3842 _rem_dev_from_if_list(t
, pkt_dev
);
3845 remove_proc_entry(pkt_dev
->entry
->name
, pg_proc_dir
);
3850 vfree(pkt_dev
->flows
);
3855 static int __init
pg_init(void)
3858 struct proc_dir_entry
*pe
;
3860 printk(KERN_INFO
"%s", version
);
3862 pg_proc_dir
= proc_mkdir(PG_PROC_DIR
, init_net
.proc_net
);
3866 pe
= proc_create(PGCTRL
, 0600, pg_proc_dir
, &pktgen_fops
);
3868 printk(KERN_ERR
"pktgen: ERROR: cannot create %s "
3869 "procfs entry.\n", PGCTRL
);
3870 proc_net_remove(&init_net
, PG_PROC_DIR
);
3874 /* Register us to receive netdevice events */
3875 register_netdevice_notifier(&pktgen_notifier_block
);
3877 for_each_online_cpu(cpu
) {
3880 err
= pktgen_create_thread(cpu
);
3882 printk(KERN_WARNING
"pktgen: WARNING: Cannot create "
3883 "thread for cpu %d (%d)\n", cpu
, err
);
3886 if (list_empty(&pktgen_threads
)) {
3887 printk(KERN_ERR
"pktgen: ERROR: Initialization failed for "
3889 unregister_netdevice_notifier(&pktgen_notifier_block
);
3890 remove_proc_entry(PGCTRL
, pg_proc_dir
);
3891 proc_net_remove(&init_net
, PG_PROC_DIR
);
3898 static void __exit
pg_cleanup(void)
3900 struct pktgen_thread
*t
;
3901 struct list_head
*q
, *n
;
3902 wait_queue_head_t queue
;
3903 init_waitqueue_head(&queue
);
3905 /* Stop all interfaces & threads */
3907 list_for_each_safe(q
, n
, &pktgen_threads
) {
3908 t
= list_entry(q
, struct pktgen_thread
, th_list
);
3909 kthread_stop(t
->tsk
);
3913 /* Un-register us from receiving netdevice events */
3914 unregister_netdevice_notifier(&pktgen_notifier_block
);
3916 /* Clean up proc file system */
3917 remove_proc_entry(PGCTRL
, pg_proc_dir
);
3918 proc_net_remove(&init_net
, PG_PROC_DIR
);
3921 module_init(pg_init
);
3922 module_exit(pg_cleanup
);
3924 MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se>");
3925 MODULE_DESCRIPTION("Packet Generator tool");
3926 MODULE_LICENSE("GPL");
3927 MODULE_VERSION(VERSION
);
3928 module_param(pg_count_d
, int, 0);
3929 MODULE_PARM_DESC(pg_count_d
, "Default number of packets to inject");
3930 module_param(pg_delay_d
, int, 0);
3931 MODULE_PARM_DESC(pg_delay_d
, "Default delay between packets (nanoseconds)");
3932 module_param(pg_clone_skb_d
, int, 0);
3933 MODULE_PARM_DESC(pg_clone_skb_d
, "Default number of copies of the same packet");
3934 module_param(debug
, int, 0);
3935 MODULE_PARM_DESC(debug
, "Enable debugging of pktgen module");