3 * Copyright 2001, 2002 by Robert Olsson <robert.olsson@its.uu.se>
4 * Uppsala University and
5 * Swedish University of Agricultural Sciences
7 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
8 * Ben Greear <greearb@candelatech.com>
9 * Jens Låås <jens.laas@data.slu.se>
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version
14 * 2 of the License, or (at your option) any later version.
17 * A tool for loading the network with preconfigurated packets.
18 * The tool is implemented as a linux module. Parameters are output
19 * device, delay (to hard_xmit), number of packets, and whether
20 * to use multiple SKBs or just the same one.
21 * pktgen uses the installed interface's output routine.
23 * Additional hacking by:
25 * Jens.Laas@data.slu.se
26 * Improved by ANK. 010120.
27 * Improved by ANK even more. 010212.
28 * MAC address typo fixed. 010417 --ro
29 * Integrated. 020301 --DaveM
30 * Added multiskb option 020301 --DaveM
31 * Scaling of results. 020417--sigurdur@linpro.no
32 * Significant re-work of the module:
33 * * Convert to threaded model to more efficiently be able to transmit
34 * and receive on multiple interfaces at once.
35 * * Converted many counters to __u64 to allow longer runs.
36 * * Allow configuration of ranges, like min/max IP address, MACs,
37 * and UDP-ports, for both source and destination, and can
38 * set to use a random distribution or sequentially walk the range.
39 * * Can now change most values after starting.
40 * * Place 12-byte packet in UDP payload with magic number,
41 * sequence number, and timestamp.
42 * * Add receiver code that detects dropped pkts, re-ordered pkts, and
43 * latencies (with micro-second) precision.
44 * * Add IOCTL interface to easily get counters & configuration.
45 * --Ben Greear <greearb@candelatech.com>
47 * Renamed multiskb to clone_skb and cleaned up sending core for two distinct
48 * skb modes. A clone_skb=0 mode for Ben "ranges" work and a clone_skb != 0
49 * as a "fastpath" with a configurable number of clones after alloc's.
50 * clone_skb=0 means all packets are allocated this also means ranges time
51 * stamps etc can be used. clone_skb=100 means 1 malloc is followed by 100
54 * Also moved to /proc/net/pktgen/
57 * Sept 10: Fixed threading/locking. Lots of bone-headed and more clever
58 * mistakes. Also merged in DaveM's patch in the -pre6 patch.
59 * --Ben Greear <greearb@candelatech.com>
61 * Integrated to 2.5.x 021029 --Lucio Maciel (luciomaciel@zipmail.com.br)
64 * 021124 Finished major redesign and rewrite for new functionality.
65 * See Documentation/networking/pktgen.txt for how to use this.
68 * For each CPU one thread/process is created at start. This process checks
69 * for running devices in the if_list and sends packets until count is 0 it
70 * also the thread checks the thread->control which is used for inter-process
71 * communication. controlling process "posts" operations to the threads this
72 * way. The if_lock should be possible to remove when add/rem_device is merged
75 * By design there should only be *one* "controlling" process. In practice
76 * multiple write accesses gives unpredictable result. Understood by "write"
77 * to /proc gives result code thats should be read be the "writer".
78 * For practical use this should be no problem.
80 * Note when adding devices to a specific CPU there good idea to also assign
81 * /proc/irq/XX/smp_affinity so TX-interrupts gets bound to the same CPU.
84 * Fix refcount off by one if first packet fails, potential null deref,
87 * First "ranges" functionality for ipv6 030726 --ro
89 * Included flow support. 030802 ANK.
91 * Fixed unaligned access on IA-64 Grant Grundler <grundler@parisc-linux.org>
93 * Remove if fix from added Harald Welte <laforge@netfilter.org> 040419
94 * ia64 compilation fix from Aron Griffis <aron@hp.com> 040604
96 * New xmit() return, do_div and misc clean up by Stephen Hemminger
97 * <shemminger@osdl.org> 040923
99 * Randy Dunlap fixed u64 printk compiler waring
101 * Remove FCS from BW calculation. Lennert Buytenhek <buytenh@wantstofly.org>
102 * New time handling. Lennert Buytenhek <buytenh@wantstofly.org> 041213
104 * Corrections from Nikolai Malykh (nmalykh@bilim.com)
105 * Removed unused flags F_SET_SRCMAC & F_SET_SRCIP 041230
107 * interruptible_sleep_on_timeout() replaced Nishanth Aravamudan <nacc@us.ibm.com>
110 * MPLS support by Steven Whitehouse <steve@chygwyn.com>
112 * 802.1Q/Q-in-Q support by Francesco Fondelli (FF) <francesco.fondelli@gmail.com>
114 * Fixed src_mac command to set source mac of packet to value specified in
115 * command by Adit Ranadive <adit.262@gmail.com>
118 #include <linux/sys.h>
119 #include <linux/types.h>
120 #include <linux/module.h>
121 #include <linux/moduleparam.h>
122 #include <linux/kernel.h>
123 #include <linux/mutex.h>
124 #include <linux/sched.h>
125 #include <linux/slab.h>
126 #include <linux/vmalloc.h>
127 #include <linux/unistd.h>
128 #include <linux/string.h>
129 #include <linux/ptrace.h>
130 #include <linux/errno.h>
131 #include <linux/ioport.h>
132 #include <linux/interrupt.h>
133 #include <linux/capability.h>
134 #include <linux/freezer.h>
135 #include <linux/delay.h>
136 #include <linux/timer.h>
137 #include <linux/list.h>
138 #include <linux/init.h>
139 #include <linux/skbuff.h>
140 #include <linux/netdevice.h>
141 #include <linux/inet.h>
142 #include <linux/inetdevice.h>
143 #include <linux/rtnetlink.h>
144 #include <linux/if_arp.h>
145 #include <linux/if_vlan.h>
146 #include <linux/in.h>
147 #include <linux/ip.h>
148 #include <linux/ipv6.h>
149 #include <linux/udp.h>
150 #include <linux/proc_fs.h>
151 #include <linux/seq_file.h>
152 #include <linux/wait.h>
153 #include <linux/etherdevice.h>
154 #include <linux/kthread.h>
155 #include <net/net_namespace.h>
156 #include <net/checksum.h>
157 #include <net/ipv6.h>
158 #include <net/addrconf.h>
160 #include <net/xfrm.h>
162 #include <asm/byteorder.h>
163 #include <linux/rcupdate.h>
164 #include <linux/bitops.h>
167 #include <asm/uaccess.h>
168 #include <asm/div64.h> /* do_div */
169 #include <asm/timex.h>
171 #define VERSION "pktgen v2.69: Packet Generator for packet performance testing.\n"
173 #define IP_NAME_SZ 32
174 #define MAX_MPLS_LABELS 16 /* This is the max label stack depth */
175 #define MPLS_STACK_BOTTOM htonl(0x00000100)
177 /* Device flag bits */
178 #define F_IPSRC_RND (1<<0) /* IP-Src Random */
179 #define F_IPDST_RND (1<<1) /* IP-Dst Random */
180 #define F_UDPSRC_RND (1<<2) /* UDP-Src Random */
181 #define F_UDPDST_RND (1<<3) /* UDP-Dst Random */
182 #define F_MACSRC_RND (1<<4) /* MAC-Src Random */
183 #define F_MACDST_RND (1<<5) /* MAC-Dst Random */
184 #define F_TXSIZE_RND (1<<6) /* Transmit size is random */
185 #define F_IPV6 (1<<7) /* Interface in IPV6 Mode */
186 #define F_MPLS_RND (1<<8) /* Random MPLS labels */
187 #define F_VID_RND (1<<9) /* Random VLAN ID */
188 #define F_SVID_RND (1<<10) /* Random SVLAN ID */
189 #define F_FLOW_SEQ (1<<11) /* Sequential flows */
190 #define F_IPSEC_ON (1<<12) /* ipsec on for flows */
191 #define F_QUEUE_MAP_RND (1<<13) /* queue map Random */
193 /* Thread control flag bits */
194 #define T_TERMINATE (1<<0)
195 #define T_STOP (1<<1) /* Stop run */
196 #define T_RUN (1<<2) /* Start run */
197 #define T_REMDEVALL (1<<3) /* Remove all devs */
198 #define T_REMDEV (1<<4) /* Remove one dev */
200 /* If lock -- can be removed after some work */
201 #define if_lock(t) spin_lock(&(t->if_lock));
202 #define if_unlock(t) spin_unlock(&(t->if_lock));
204 /* Used to help with determining the pkts on receive */
205 #define PKTGEN_MAGIC 0xbe9be955
206 #define PG_PROC_DIR "pktgen"
207 #define PGCTRL "pgctrl"
208 static struct proc_dir_entry *pg_proc_dir = NULL;
210 #define MAX_CFLOWS 65536
212 #define VLAN_TAG_SIZE(x) ((x)->vlan_id == 0xffff ? 0 : 4)
213 #define SVLAN_TAG_SIZE(x) ((x)->svlan_id == 0xffff ? 0 : 4)
219 struct xfrm_state *x;
225 #define F_INIT (1<<0) /* flow has been initialized */
229 * Try to keep frequent/infrequent used vars. separated.
231 struct proc_dir_entry *entry; /* proc file */
232 struct pktgen_thread *pg_thread;/* the owner */
233 struct list_head list; /* Used for chaining in the thread's run-queue */
235 int running; /* if this changes to false, the test will stop */
237 /* If min != max, then we will either do a linear iteration, or
238 * we will do a random selection from within the range.
241 int removal_mark; /* non-zero => the device is marked for
242 * removal by worker thread */
244 int min_pkt_size; /* = ETH_ZLEN; */
245 int max_pkt_size; /* = ETH_ZLEN; */
246 int pkt_overhead; /* overhead for MPLS, VLANs, IPSEC etc */
248 __u32 delay_us; /* Default delay */
250 __u64 count; /* Default No packets to send */
251 __u64 sofar; /* How many pkts we've sent so far */
252 __u64 tx_bytes; /* How many bytes we've transmitted */
253 __u64 errors; /* Errors when trying to transmit, pkts will be re-sent */
255 /* runtime counters relating to clone_skb */
256 __u64 next_tx_us; /* timestamp of when to tx next */
259 __u64 allocated_skbs;
261 int last_ok; /* Was last skb sent?
262 * Or a failed transmit of some sort? This will keep
263 * sequence numbers in order, for example.
265 __u64 started_at; /* micro-seconds */
266 __u64 stopped_at; /* micro-seconds */
267 __u64 idle_acc; /* micro-seconds */
270 int clone_skb; /* Use multiple SKBs during packet gen. If this number
271 * is greater than 1, then that many copies of the same
272 * packet will be sent before a new packet is allocated.
273 * For instance, if you want to send 1024 identical packets
274 * before creating a new packet, set clone_skb to 1024.
277 char dst_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
278 char dst_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
279 char src_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
280 char src_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
282 struct in6_addr in6_saddr;
283 struct in6_addr in6_daddr;
284 struct in6_addr cur_in6_daddr;
285 struct in6_addr cur_in6_saddr;
287 struct in6_addr min_in6_daddr;
288 struct in6_addr max_in6_daddr;
289 struct in6_addr min_in6_saddr;
290 struct in6_addr max_in6_saddr;
292 /* If we're doing ranges, random or incremental, then this
293 * defines the min/max for those ranges.
295 __be32 saddr_min; /* inclusive, source IP address */
296 __be32 saddr_max; /* exclusive, source IP address */
297 __be32 daddr_min; /* inclusive, dest IP address */
298 __be32 daddr_max; /* exclusive, dest IP address */
300 __u16 udp_src_min; /* inclusive, source UDP port */
301 __u16 udp_src_max; /* exclusive, source UDP port */
302 __u16 udp_dst_min; /* inclusive, dest UDP port */
303 __u16 udp_dst_max; /* exclusive, dest UDP port */
306 __u8 tos; /* six most significant bits of (former) IPv4 TOS are for dscp codepoint */
307 __u8 traffic_class; /* ditto for the (former) Traffic Class in IPv6 (see RFC 3260, sec. 4) */
310 unsigned nr_labels; /* Depth of stack, 0 = no MPLS */
311 __be32 labels[MAX_MPLS_LABELS];
313 /* VLAN/SVLAN (802.1Q/Q-in-Q) */
316 __u16 vlan_id; /* 0xffff means no vlan tag */
320 __u16 svlan_id; /* 0xffff means no svlan tag */
322 __u32 src_mac_count; /* How many MACs to iterate through */
323 __u32 dst_mac_count; /* How many MACs to iterate through */
325 unsigned char dst_mac[ETH_ALEN];
326 unsigned char src_mac[ETH_ALEN];
328 __u32 cur_dst_mac_offset;
329 __u32 cur_src_mac_offset;
339 0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
341 We fill in SRC address later
342 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
346 __u16 pad; /* pad out the hh struct to an even 16 bytes */
348 struct sk_buff *skb; /* skb we are to transmit next, mainly used for when we
349 * are transmitting the same one multiple times
351 struct net_device *odev; /* The out-going device. Note that the device should
352 * have it's pg_info pointer pointing back to this
353 * device. This will be set when the user specifies
354 * the out-going device name (not when the inject is
355 * started as it used to do.)
357 struct flow_state *flows;
358 unsigned cflows; /* Concurrent flows (config) */
359 unsigned lflow; /* Flow length (config) */
360 unsigned nflows; /* accumulated flows (stats) */
361 unsigned curfl; /* current sequenced flow (state)*/
367 __u8 ipsmode; /* IPSEC mode (config) */
368 __u8 ipsproto; /* IPSEC type (config) */
380 struct pktgen_thread {
382 struct list_head if_list; /* All device here */
383 struct list_head th_list;
384 struct task_struct *tsk;
387 /* Field for thread to receive "posted" events terminate, stop ifs etc. */
392 wait_queue_head_t queue;
398 /** Convert to micro-seconds */
399 static inline __u64 tv_to_us(const struct timeval *tv)
401 __u64 us = tv->tv_usec;
402 us += (__u64) tv->tv_sec * (__u64) 1000000;
406 static __u64 getCurUs(void)
409 do_gettimeofday(&tv);
410 return tv_to_us(&tv);
413 /* old include end */
415 static char version[] __initdata = VERSION;
417 static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *i);
418 static int pktgen_add_device(struct pktgen_thread *t, const char *ifname);
419 static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
421 static int pktgen_device_event(struct notifier_block *, unsigned long, void *);
422 static void pktgen_run_all_threads(void);
423 static void pktgen_stop_all_threads_ifs(void);
424 static int pktgen_stop_device(struct pktgen_dev *pkt_dev);
425 static void pktgen_stop(struct pktgen_thread *t);
426 static void pktgen_clear_counters(struct pktgen_dev *pkt_dev);
428 static unsigned int scan_ip6(const char *s, char ip[16]);
429 static unsigned int fmt_ip6(char *s, const char ip[16]);
431 /* Module parameters, defaults. */
432 static int pg_count_d = 1000; /* 1000 pkts by default */
433 static int pg_delay_d;
434 static int pg_clone_skb_d;
437 static DEFINE_MUTEX(pktgen_thread_lock);
438 static LIST_HEAD(pktgen_threads);
440 static struct notifier_block pktgen_notifier_block = {
441 .notifier_call = pktgen_device_event,
445 * /proc handling functions
449 static int pgctrl_show(struct seq_file *seq, void *v)
451 seq_puts(seq, VERSION);
455 static ssize_t pgctrl_write(struct file *file, const char __user * buf,
456 size_t count, loff_t * ppos)
461 if (!capable(CAP_NET_ADMIN)) {
466 if (count > sizeof(data))
467 count = sizeof(data);
469 if (copy_from_user(data, buf, count)) {
473 data[count - 1] = 0; /* Make string */
475 if (!strcmp(data, "stop"))
476 pktgen_stop_all_threads_ifs();
478 else if (!strcmp(data, "start"))
479 pktgen_run_all_threads();
482 printk(KERN_WARNING "pktgen: Unknown command: %s\n", data);
490 static int pgctrl_open(struct inode *inode, struct file *file)
492 return single_open(file, pgctrl_show, PDE(inode)->data);
495 static const struct file_operations pktgen_fops = {
496 .owner = THIS_MODULE,
500 .write = pgctrl_write,
501 .release = single_release,
504 static int pktgen_if_show(struct seq_file *seq, void *v)
506 struct pktgen_dev *pkt_dev = seq->private;
509 __u64 now = getCurUs();
510 DECLARE_MAC_BUF(mac);
513 "Params: count %llu min_pkt_size: %u max_pkt_size: %u\n",
514 (unsigned long long)pkt_dev->count, pkt_dev->min_pkt_size,
515 pkt_dev->max_pkt_size);
518 " frags: %d delay: %u clone_skb: %d ifname: %s\n",
520 1000 * pkt_dev->delay_us + pkt_dev->delay_ns,
521 pkt_dev->clone_skb, pkt_dev->odev->name);
523 seq_printf(seq, " flows: %u flowlen: %u\n", pkt_dev->cflows,
527 " queue_map_min: %u queue_map_max: %u\n",
528 pkt_dev->queue_map_min,
529 pkt_dev->queue_map_max);
531 if (pkt_dev->flags & F_IPV6) {
532 char b1[128], b2[128], b3[128];
533 fmt_ip6(b1, pkt_dev->in6_saddr.s6_addr);
534 fmt_ip6(b2, pkt_dev->min_in6_saddr.s6_addr);
535 fmt_ip6(b3, pkt_dev->max_in6_saddr.s6_addr);
537 " saddr: %s min_saddr: %s max_saddr: %s\n", b1,
540 fmt_ip6(b1, pkt_dev->in6_daddr.s6_addr);
541 fmt_ip6(b2, pkt_dev->min_in6_daddr.s6_addr);
542 fmt_ip6(b3, pkt_dev->max_in6_daddr.s6_addr);
544 " daddr: %s min_daddr: %s max_daddr: %s\n", b1,
549 " dst_min: %s dst_max: %s\n src_min: %s src_max: %s\n",
550 pkt_dev->dst_min, pkt_dev->dst_max, pkt_dev->src_min,
553 seq_puts(seq, " src_mac: ");
555 seq_printf(seq, "%s ",
556 print_mac(mac, is_zero_ether_addr(pkt_dev->src_mac) ?
557 pkt_dev->odev->dev_addr : pkt_dev->src_mac));
559 seq_printf(seq, "dst_mac: ");
560 seq_printf(seq, "%s\n", print_mac(mac, pkt_dev->dst_mac));
563 " udp_src_min: %d udp_src_max: %d udp_dst_min: %d udp_dst_max: %d\n",
564 pkt_dev->udp_src_min, pkt_dev->udp_src_max,
565 pkt_dev->udp_dst_min, pkt_dev->udp_dst_max);
568 " src_mac_count: %d dst_mac_count: %d\n",
569 pkt_dev->src_mac_count, pkt_dev->dst_mac_count);
571 if (pkt_dev->nr_labels) {
573 seq_printf(seq, " mpls: ");
574 for (i = 0; i < pkt_dev->nr_labels; i++)
575 seq_printf(seq, "%08x%s", ntohl(pkt_dev->labels[i]),
576 i == pkt_dev->nr_labels-1 ? "\n" : ", ");
579 if (pkt_dev->vlan_id != 0xffff) {
580 seq_printf(seq, " vlan_id: %u vlan_p: %u vlan_cfi: %u\n",
581 pkt_dev->vlan_id, pkt_dev->vlan_p, pkt_dev->vlan_cfi);
584 if (pkt_dev->svlan_id != 0xffff) {
585 seq_printf(seq, " svlan_id: %u vlan_p: %u vlan_cfi: %u\n",
586 pkt_dev->svlan_id, pkt_dev->svlan_p, pkt_dev->svlan_cfi);
590 seq_printf(seq, " tos: 0x%02x\n", pkt_dev->tos);
593 if (pkt_dev->traffic_class) {
594 seq_printf(seq, " traffic_class: 0x%02x\n", pkt_dev->traffic_class);
597 seq_printf(seq, " Flags: ");
599 if (pkt_dev->flags & F_IPV6)
600 seq_printf(seq, "IPV6 ");
602 if (pkt_dev->flags & F_IPSRC_RND)
603 seq_printf(seq, "IPSRC_RND ");
605 if (pkt_dev->flags & F_IPDST_RND)
606 seq_printf(seq, "IPDST_RND ");
608 if (pkt_dev->flags & F_TXSIZE_RND)
609 seq_printf(seq, "TXSIZE_RND ");
611 if (pkt_dev->flags & F_UDPSRC_RND)
612 seq_printf(seq, "UDPSRC_RND ");
614 if (pkt_dev->flags & F_UDPDST_RND)
615 seq_printf(seq, "UDPDST_RND ");
617 if (pkt_dev->flags & F_MPLS_RND)
618 seq_printf(seq, "MPLS_RND ");
620 if (pkt_dev->flags & F_QUEUE_MAP_RND)
621 seq_printf(seq, "QUEUE_MAP_RND ");
623 if (pkt_dev->cflows) {
624 if (pkt_dev->flags & F_FLOW_SEQ)
625 seq_printf(seq, "FLOW_SEQ "); /*in sequence flows*/
627 seq_printf(seq, "FLOW_RND ");
631 if (pkt_dev->flags & F_IPSEC_ON)
632 seq_printf(seq, "IPSEC ");
635 if (pkt_dev->flags & F_MACSRC_RND)
636 seq_printf(seq, "MACSRC_RND ");
638 if (pkt_dev->flags & F_MACDST_RND)
639 seq_printf(seq, "MACDST_RND ");
641 if (pkt_dev->flags & F_VID_RND)
642 seq_printf(seq, "VID_RND ");
644 if (pkt_dev->flags & F_SVID_RND)
645 seq_printf(seq, "SVID_RND ");
649 sa = pkt_dev->started_at;
650 stopped = pkt_dev->stopped_at;
651 if (pkt_dev->running)
652 stopped = now; /* not really stopped, more like last-running-at */
655 "Current:\n pkts-sofar: %llu errors: %llu\n started: %lluus stopped: %lluus idle: %lluus\n",
656 (unsigned long long)pkt_dev->sofar,
657 (unsigned long long)pkt_dev->errors, (unsigned long long)sa,
658 (unsigned long long)stopped,
659 (unsigned long long)pkt_dev->idle_acc);
662 " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n",
663 pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset,
664 pkt_dev->cur_src_mac_offset);
666 if (pkt_dev->flags & F_IPV6) {
667 char b1[128], b2[128];
668 fmt_ip6(b1, pkt_dev->cur_in6_daddr.s6_addr);
669 fmt_ip6(b2, pkt_dev->cur_in6_saddr.s6_addr);
670 seq_printf(seq, " cur_saddr: %s cur_daddr: %s\n", b2, b1);
672 seq_printf(seq, " cur_saddr: 0x%x cur_daddr: 0x%x\n",
673 pkt_dev->cur_saddr, pkt_dev->cur_daddr);
675 seq_printf(seq, " cur_udp_dst: %d cur_udp_src: %d\n",
676 pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src);
678 seq_printf(seq, " cur_queue_map: %u\n", pkt_dev->cur_queue_map);
680 seq_printf(seq, " flows: %u\n", pkt_dev->nflows);
682 if (pkt_dev->result[0])
683 seq_printf(seq, "Result: %s\n", pkt_dev->result);
685 seq_printf(seq, "Result: Idle\n");
691 static int hex32_arg(const char __user *user_buffer, unsigned long maxlen, __u32 *num)
696 for (; i < maxlen; i++) {
699 if (get_user(c, &user_buffer[i]))
701 if ((c >= '0') && (c <= '9'))
703 else if ((c >= 'a') && (c <= 'f'))
704 *num |= c - 'a' + 10;
705 else if ((c >= 'A') && (c <= 'F'))
706 *num |= c - 'A' + 10;
713 static int count_trail_chars(const char __user * user_buffer,
718 for (i = 0; i < maxlen; i++) {
720 if (get_user(c, &user_buffer[i]))
738 static unsigned long num_arg(const char __user * user_buffer,
739 unsigned long maxlen, unsigned long *num)
744 for (; i < maxlen; i++) {
746 if (get_user(c, &user_buffer[i]))
748 if ((c >= '0') && (c <= '9')) {
757 static int strn_len(const char __user * user_buffer, unsigned int maxlen)
761 for (; i < maxlen; i++) {
763 if (get_user(c, &user_buffer[i]))
781 static ssize_t get_labels(const char __user *buffer, struct pktgen_dev *pkt_dev)
788 pkt_dev->nr_labels = 0;
791 len = hex32_arg(&buffer[i], 8, &tmp);
794 pkt_dev->labels[n] = htonl(tmp);
795 if (pkt_dev->labels[n] & MPLS_STACK_BOTTOM)
796 pkt_dev->flags |= F_MPLS_RND;
798 if (get_user(c, &buffer[i]))
802 if (n >= MAX_MPLS_LABELS)
806 pkt_dev->nr_labels = n;
810 static ssize_t pktgen_if_write(struct file *file,
811 const char __user * user_buffer, size_t count,
814 struct seq_file *seq = (struct seq_file *)file->private_data;
815 struct pktgen_dev *pkt_dev = seq->private;
817 char name[16], valstr[32];
818 unsigned long value = 0;
819 char *pg_result = NULL;
823 pg_result = &(pkt_dev->result[0]);
826 printk(KERN_WARNING "pktgen: wrong command format\n");
831 tmp = count_trail_chars(&user_buffer[i], max);
833 printk(KERN_WARNING "pktgen: illegal format\n");
838 /* Read variable name */
840 len = strn_len(&user_buffer[i], sizeof(name) - 1);
844 memset(name, 0, sizeof(name));
845 if (copy_from_user(name, &user_buffer[i], len))
850 len = count_trail_chars(&user_buffer[i], max);
858 if (copy_from_user(tb, user_buffer, count))
861 printk(KERN_DEBUG "pktgen: %s,%lu buffer -:%s:-\n", name,
862 (unsigned long)count, tb);
865 if (!strcmp(name, "min_pkt_size")) {
866 len = num_arg(&user_buffer[i], 10, &value);
871 if (value < 14 + 20 + 8)
873 if (value != pkt_dev->min_pkt_size) {
874 pkt_dev->min_pkt_size = value;
875 pkt_dev->cur_pkt_size = value;
877 sprintf(pg_result, "OK: min_pkt_size=%u",
878 pkt_dev->min_pkt_size);
882 if (!strcmp(name, "max_pkt_size")) {
883 len = num_arg(&user_buffer[i], 10, &value);
888 if (value < 14 + 20 + 8)
890 if (value != pkt_dev->max_pkt_size) {
891 pkt_dev->max_pkt_size = value;
892 pkt_dev->cur_pkt_size = value;
894 sprintf(pg_result, "OK: max_pkt_size=%u",
895 pkt_dev->max_pkt_size);
899 /* Shortcut for min = max */
901 if (!strcmp(name, "pkt_size")) {
902 len = num_arg(&user_buffer[i], 10, &value);
907 if (value < 14 + 20 + 8)
909 if (value != pkt_dev->min_pkt_size) {
910 pkt_dev->min_pkt_size = value;
911 pkt_dev->max_pkt_size = value;
912 pkt_dev->cur_pkt_size = value;
914 sprintf(pg_result, "OK: pkt_size=%u", pkt_dev->min_pkt_size);
918 if (!strcmp(name, "debug")) {
919 len = num_arg(&user_buffer[i], 10, &value);
925 sprintf(pg_result, "OK: debug=%u", debug);
929 if (!strcmp(name, "frags")) {
930 len = num_arg(&user_buffer[i], 10, &value);
935 pkt_dev->nfrags = value;
936 sprintf(pg_result, "OK: frags=%u", pkt_dev->nfrags);
939 if (!strcmp(name, "delay")) {
940 len = num_arg(&user_buffer[i], 10, &value);
945 if (value == 0x7FFFFFFF) {
946 pkt_dev->delay_us = 0x7FFFFFFF;
947 pkt_dev->delay_ns = 0;
949 pkt_dev->delay_us = value / 1000;
950 pkt_dev->delay_ns = value % 1000;
952 sprintf(pg_result, "OK: delay=%u",
953 1000 * pkt_dev->delay_us + pkt_dev->delay_ns);
956 if (!strcmp(name, "udp_src_min")) {
957 len = num_arg(&user_buffer[i], 10, &value);
962 if (value != pkt_dev->udp_src_min) {
963 pkt_dev->udp_src_min = value;
964 pkt_dev->cur_udp_src = value;
966 sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min);
969 if (!strcmp(name, "udp_dst_min")) {
970 len = num_arg(&user_buffer[i], 10, &value);
975 if (value != pkt_dev->udp_dst_min) {
976 pkt_dev->udp_dst_min = value;
977 pkt_dev->cur_udp_dst = value;
979 sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min);
982 if (!strcmp(name, "udp_src_max")) {
983 len = num_arg(&user_buffer[i], 10, &value);
988 if (value != pkt_dev->udp_src_max) {
989 pkt_dev->udp_src_max = value;
990 pkt_dev->cur_udp_src = value;
992 sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max);
995 if (!strcmp(name, "udp_dst_max")) {
996 len = num_arg(&user_buffer[i], 10, &value);
1001 if (value != pkt_dev->udp_dst_max) {
1002 pkt_dev->udp_dst_max = value;
1003 pkt_dev->cur_udp_dst = value;
1005 sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max);
1008 if (!strcmp(name, "clone_skb")) {
1009 len = num_arg(&user_buffer[i], 10, &value);
1014 pkt_dev->clone_skb = value;
1016 sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb);
1019 if (!strcmp(name, "count")) {
1020 len = num_arg(&user_buffer[i], 10, &value);
1025 pkt_dev->count = value;
1026 sprintf(pg_result, "OK: count=%llu",
1027 (unsigned long long)pkt_dev->count);
1030 if (!strcmp(name, "src_mac_count")) {
1031 len = num_arg(&user_buffer[i], 10, &value);
1036 if (pkt_dev->src_mac_count != value) {
1037 pkt_dev->src_mac_count = value;
1038 pkt_dev->cur_src_mac_offset = 0;
1040 sprintf(pg_result, "OK: src_mac_count=%d",
1041 pkt_dev->src_mac_count);
1044 if (!strcmp(name, "dst_mac_count")) {
1045 len = num_arg(&user_buffer[i], 10, &value);
1050 if (pkt_dev->dst_mac_count != value) {
1051 pkt_dev->dst_mac_count = value;
1052 pkt_dev->cur_dst_mac_offset = 0;
1054 sprintf(pg_result, "OK: dst_mac_count=%d",
1055 pkt_dev->dst_mac_count);
1058 if (!strcmp(name, "flag")) {
1061 len = strn_len(&user_buffer[i], sizeof(f) - 1);
1065 if (copy_from_user(f, &user_buffer[i], len))
1068 if (strcmp(f, "IPSRC_RND") == 0)
1069 pkt_dev->flags |= F_IPSRC_RND;
1071 else if (strcmp(f, "!IPSRC_RND") == 0)
1072 pkt_dev->flags &= ~F_IPSRC_RND;
1074 else if (strcmp(f, "TXSIZE_RND") == 0)
1075 pkt_dev->flags |= F_TXSIZE_RND;
1077 else if (strcmp(f, "!TXSIZE_RND") == 0)
1078 pkt_dev->flags &= ~F_TXSIZE_RND;
1080 else if (strcmp(f, "IPDST_RND") == 0)
1081 pkt_dev->flags |= F_IPDST_RND;
1083 else if (strcmp(f, "!IPDST_RND") == 0)
1084 pkt_dev->flags &= ~F_IPDST_RND;
1086 else if (strcmp(f, "UDPSRC_RND") == 0)
1087 pkt_dev->flags |= F_UDPSRC_RND;
1089 else if (strcmp(f, "!UDPSRC_RND") == 0)
1090 pkt_dev->flags &= ~F_UDPSRC_RND;
1092 else if (strcmp(f, "UDPDST_RND") == 0)
1093 pkt_dev->flags |= F_UDPDST_RND;
1095 else if (strcmp(f, "!UDPDST_RND") == 0)
1096 pkt_dev->flags &= ~F_UDPDST_RND;
1098 else if (strcmp(f, "MACSRC_RND") == 0)
1099 pkt_dev->flags |= F_MACSRC_RND;
1101 else if (strcmp(f, "!MACSRC_RND") == 0)
1102 pkt_dev->flags &= ~F_MACSRC_RND;
1104 else if (strcmp(f, "MACDST_RND") == 0)
1105 pkt_dev->flags |= F_MACDST_RND;
1107 else if (strcmp(f, "!MACDST_RND") == 0)
1108 pkt_dev->flags &= ~F_MACDST_RND;
1110 else if (strcmp(f, "MPLS_RND") == 0)
1111 pkt_dev->flags |= F_MPLS_RND;
1113 else if (strcmp(f, "!MPLS_RND") == 0)
1114 pkt_dev->flags &= ~F_MPLS_RND;
1116 else if (strcmp(f, "VID_RND") == 0)
1117 pkt_dev->flags |= F_VID_RND;
1119 else if (strcmp(f, "!VID_RND") == 0)
1120 pkt_dev->flags &= ~F_VID_RND;
1122 else if (strcmp(f, "SVID_RND") == 0)
1123 pkt_dev->flags |= F_SVID_RND;
1125 else if (strcmp(f, "!SVID_RND") == 0)
1126 pkt_dev->flags &= ~F_SVID_RND;
1128 else if (strcmp(f, "FLOW_SEQ") == 0)
1129 pkt_dev->flags |= F_FLOW_SEQ;
1131 else if (strcmp(f, "QUEUE_MAP_RND") == 0)
1132 pkt_dev->flags |= F_QUEUE_MAP_RND;
1134 else if (strcmp(f, "!QUEUE_MAP_RND") == 0)
1135 pkt_dev->flags &= ~F_QUEUE_MAP_RND;
1137 else if (strcmp(f, "IPSEC") == 0)
1138 pkt_dev->flags |= F_IPSEC_ON;
1141 else if (strcmp(f, "!IPV6") == 0)
1142 pkt_dev->flags &= ~F_IPV6;
1146 "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
1148 "IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
1149 "MACSRC_RND, MACDST_RND, TXSIZE_RND, IPV6, MPLS_RND, VID_RND, SVID_RND, FLOW_SEQ, IPSEC\n");
1152 sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
1155 if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
1156 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
1161 if (copy_from_user(buf, &user_buffer[i], len))
1164 if (strcmp(buf, pkt_dev->dst_min) != 0) {
1165 memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
1166 strncpy(pkt_dev->dst_min, buf, len);
1167 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
1168 pkt_dev->cur_daddr = pkt_dev->daddr_min;
1171 printk(KERN_DEBUG "pktgen: dst_min set to: %s\n",
1174 sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min);
1177 if (!strcmp(name, "dst_max")) {
1178 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
1183 if (copy_from_user(buf, &user_buffer[i], len))
1187 if (strcmp(buf, pkt_dev->dst_max) != 0) {
1188 memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
1189 strncpy(pkt_dev->dst_max, buf, len);
1190 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
1191 pkt_dev->cur_daddr = pkt_dev->daddr_max;
1194 printk(KERN_DEBUG "pktgen: dst_max set to: %s\n",
1197 sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max);
1200 if (!strcmp(name, "dst6")) {
1201 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1205 pkt_dev->flags |= F_IPV6;
1207 if (copy_from_user(buf, &user_buffer[i], len))
1211 scan_ip6(buf, pkt_dev->in6_daddr.s6_addr);
1212 fmt_ip6(buf, pkt_dev->in6_daddr.s6_addr);
1214 ipv6_addr_copy(&pkt_dev->cur_in6_daddr, &pkt_dev->in6_daddr);
1217 printk(KERN_DEBUG "pktgen: dst6 set to: %s\n", buf);
1220 sprintf(pg_result, "OK: dst6=%s", buf);
1223 if (!strcmp(name, "dst6_min")) {
1224 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1228 pkt_dev->flags |= F_IPV6;
1230 if (copy_from_user(buf, &user_buffer[i], len))
1234 scan_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
1235 fmt_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
1237 ipv6_addr_copy(&pkt_dev->cur_in6_daddr,
1238 &pkt_dev->min_in6_daddr);
1240 printk(KERN_DEBUG "pktgen: dst6_min set to: %s\n", buf);
1243 sprintf(pg_result, "OK: dst6_min=%s", buf);
1246 if (!strcmp(name, "dst6_max")) {
1247 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1251 pkt_dev->flags |= F_IPV6;
1253 if (copy_from_user(buf, &user_buffer[i], len))
1257 scan_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
1258 fmt_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
1261 printk(KERN_DEBUG "pktgen: dst6_max set to: %s\n", buf);
1264 sprintf(pg_result, "OK: dst6_max=%s", buf);
1267 if (!strcmp(name, "src6")) {
1268 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1272 pkt_dev->flags |= F_IPV6;
1274 if (copy_from_user(buf, &user_buffer[i], len))
1278 scan_ip6(buf, pkt_dev->in6_saddr.s6_addr);
1279 fmt_ip6(buf, pkt_dev->in6_saddr.s6_addr);
1281 ipv6_addr_copy(&pkt_dev->cur_in6_saddr, &pkt_dev->in6_saddr);
1284 printk(KERN_DEBUG "pktgen: src6 set to: %s\n", buf);
1287 sprintf(pg_result, "OK: src6=%s", buf);
1290 if (!strcmp(name, "src_min")) {
1291 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
1295 if (copy_from_user(buf, &user_buffer[i], len))
1298 if (strcmp(buf, pkt_dev->src_min) != 0) {
1299 memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
1300 strncpy(pkt_dev->src_min, buf, len);
1301 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
1302 pkt_dev->cur_saddr = pkt_dev->saddr_min;
1305 printk(KERN_DEBUG "pktgen: src_min set to: %s\n",
1308 sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min);
1311 if (!strcmp(name, "src_max")) {
1312 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
1316 if (copy_from_user(buf, &user_buffer[i], len))
1319 if (strcmp(buf, pkt_dev->src_max) != 0) {
1320 memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
1321 strncpy(pkt_dev->src_max, buf, len);
1322 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
1323 pkt_dev->cur_saddr = pkt_dev->saddr_max;
1326 printk(KERN_DEBUG "pktgen: src_max set to: %s\n",
1329 sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max);
1332 if (!strcmp(name, "dst_mac")) {
1334 unsigned char old_dmac[ETH_ALEN];
1335 unsigned char *m = pkt_dev->dst_mac;
1336 memcpy(old_dmac, pkt_dev->dst_mac, ETH_ALEN);
1338 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
1342 memset(valstr, 0, sizeof(valstr));
1343 if (copy_from_user(valstr, &user_buffer[i], len))
1347 for (*m = 0; *v && m < pkt_dev->dst_mac + 6; v++) {
1348 if (*v >= '0' && *v <= '9') {
1352 if (*v >= 'A' && *v <= 'F') {
1354 *m += *v - 'A' + 10;
1356 if (*v >= 'a' && *v <= 'f') {
1358 *m += *v - 'a' + 10;
1366 /* Set up Dest MAC */
1367 if (compare_ether_addr(old_dmac, pkt_dev->dst_mac))
1368 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
1370 sprintf(pg_result, "OK: dstmac");
1373 if (!strcmp(name, "src_mac")) {
1375 unsigned char old_smac[ETH_ALEN];
1376 unsigned char *m = pkt_dev->src_mac;
1378 memcpy(old_smac, pkt_dev->src_mac, ETH_ALEN);
1380 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
1384 memset(valstr, 0, sizeof(valstr));
1385 if (copy_from_user(valstr, &user_buffer[i], len))
1389 for (*m = 0; *v && m < pkt_dev->src_mac + 6; v++) {
1390 if (*v >= '0' && *v <= '9') {
1394 if (*v >= 'A' && *v <= 'F') {
1396 *m += *v - 'A' + 10;
1398 if (*v >= 'a' && *v <= 'f') {
1400 *m += *v - 'a' + 10;
1408 /* Set up Src MAC */
1409 if (compare_ether_addr(old_smac, pkt_dev->src_mac))
1410 memcpy(&(pkt_dev->hh[6]), pkt_dev->src_mac, ETH_ALEN);
1412 sprintf(pg_result, "OK: srcmac");
1416 if (!strcmp(name, "clear_counters")) {
1417 pktgen_clear_counters(pkt_dev);
1418 sprintf(pg_result, "OK: Clearing counters.\n");
1422 if (!strcmp(name, "flows")) {
1423 len = num_arg(&user_buffer[i], 10, &value);
1428 if (value > MAX_CFLOWS)
1431 pkt_dev->cflows = value;
1432 sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
1436 if (!strcmp(name, "flowlen")) {
1437 len = num_arg(&user_buffer[i], 10, &value);
1442 pkt_dev->lflow = value;
1443 sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
1447 if (!strcmp(name, "queue_map_min")) {
1448 len = num_arg(&user_buffer[i], 5, &value);
1453 pkt_dev->queue_map_min = value;
1454 sprintf(pg_result, "OK: queue_map_min=%u", pkt_dev->queue_map_min);
1458 if (!strcmp(name, "queue_map_max")) {
1459 len = num_arg(&user_buffer[i], 5, &value);
1464 pkt_dev->queue_map_max = value;
1465 sprintf(pg_result, "OK: queue_map_max=%u", pkt_dev->queue_map_max);
1469 if (!strcmp(name, "mpls")) {
1472 len = get_labels(&user_buffer[i], pkt_dev);
1476 cnt = sprintf(pg_result, "OK: mpls=");
1477 for (n = 0; n < pkt_dev->nr_labels; n++)
1478 cnt += sprintf(pg_result + cnt,
1479 "%08x%s", ntohl(pkt_dev->labels[n]),
1480 n == pkt_dev->nr_labels-1 ? "" : ",");
1482 if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) {
1483 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1484 pkt_dev->svlan_id = 0xffff;
1487 printk(KERN_DEBUG "pktgen: VLAN/SVLAN auto turned off\n");
1492 if (!strcmp(name, "vlan_id")) {
1493 len = num_arg(&user_buffer[i], 4, &value);
1498 if (value <= 4095) {
1499 pkt_dev->vlan_id = value; /* turn on VLAN */
1502 printk(KERN_DEBUG "pktgen: VLAN turned on\n");
1504 if (debug && pkt_dev->nr_labels)
1505 printk(KERN_DEBUG "pktgen: MPLS auto turned off\n");
1507 pkt_dev->nr_labels = 0; /* turn off MPLS */
1508 sprintf(pg_result, "OK: vlan_id=%u", pkt_dev->vlan_id);
1510 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1511 pkt_dev->svlan_id = 0xffff;
1514 printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n");
1519 if (!strcmp(name, "vlan_p")) {
1520 len = num_arg(&user_buffer[i], 1, &value);
1525 if ((value <= 7) && (pkt_dev->vlan_id != 0xffff)) {
1526 pkt_dev->vlan_p = value;
1527 sprintf(pg_result, "OK: vlan_p=%u", pkt_dev->vlan_p);
1529 sprintf(pg_result, "ERROR: vlan_p must be 0-7");
1534 if (!strcmp(name, "vlan_cfi")) {
1535 len = num_arg(&user_buffer[i], 1, &value);
1540 if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) {
1541 pkt_dev->vlan_cfi = value;
1542 sprintf(pg_result, "OK: vlan_cfi=%u", pkt_dev->vlan_cfi);
1544 sprintf(pg_result, "ERROR: vlan_cfi must be 0-1");
1549 if (!strcmp(name, "svlan_id")) {
1550 len = num_arg(&user_buffer[i], 4, &value);
1555 if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) {
1556 pkt_dev->svlan_id = value; /* turn on SVLAN */
1559 printk(KERN_DEBUG "pktgen: SVLAN turned on\n");
1561 if (debug && pkt_dev->nr_labels)
1562 printk(KERN_DEBUG "pktgen: MPLS auto turned off\n");
1564 pkt_dev->nr_labels = 0; /* turn off MPLS */
1565 sprintf(pg_result, "OK: svlan_id=%u", pkt_dev->svlan_id);
1567 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1568 pkt_dev->svlan_id = 0xffff;
1571 printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n");
1576 if (!strcmp(name, "svlan_p")) {
1577 len = num_arg(&user_buffer[i], 1, &value);
1582 if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) {
1583 pkt_dev->svlan_p = value;
1584 sprintf(pg_result, "OK: svlan_p=%u", pkt_dev->svlan_p);
1586 sprintf(pg_result, "ERROR: svlan_p must be 0-7");
1591 if (!strcmp(name, "svlan_cfi")) {
1592 len = num_arg(&user_buffer[i], 1, &value);
1597 if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) {
1598 pkt_dev->svlan_cfi = value;
1599 sprintf(pg_result, "OK: svlan_cfi=%u", pkt_dev->svlan_cfi);
1601 sprintf(pg_result, "ERROR: svlan_cfi must be 0-1");
1606 if (!strcmp(name, "tos")) {
1607 __u32 tmp_value = 0;
1608 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1614 pkt_dev->tos = tmp_value;
1615 sprintf(pg_result, "OK: tos=0x%02x", pkt_dev->tos);
1617 sprintf(pg_result, "ERROR: tos must be 00-ff");
1622 if (!strcmp(name, "traffic_class")) {
1623 __u32 tmp_value = 0;
1624 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1630 pkt_dev->traffic_class = tmp_value;
1631 sprintf(pg_result, "OK: traffic_class=0x%02x", pkt_dev->traffic_class);
1633 sprintf(pg_result, "ERROR: traffic_class must be 00-ff");
1638 sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
1642 static int pktgen_if_open(struct inode *inode, struct file *file)
1644 return single_open(file, pktgen_if_show, PDE(inode)->data);
1647 static const struct file_operations pktgen_if_fops = {
1648 .owner = THIS_MODULE,
1649 .open = pktgen_if_open,
1651 .llseek = seq_lseek,
1652 .write = pktgen_if_write,
1653 .release = single_release,
1656 static int pktgen_thread_show(struct seq_file *seq, void *v)
1658 struct pktgen_thread *t = seq->private;
1659 struct pktgen_dev *pkt_dev;
1663 seq_printf(seq, "Running: ");
1666 list_for_each_entry(pkt_dev, &t->if_list, list)
1667 if (pkt_dev->running)
1668 seq_printf(seq, "%s ", pkt_dev->odev->name);
1670 seq_printf(seq, "\nStopped: ");
1672 list_for_each_entry(pkt_dev, &t->if_list, list)
1673 if (!pkt_dev->running)
1674 seq_printf(seq, "%s ", pkt_dev->odev->name);
1677 seq_printf(seq, "\nResult: %s\n", t->result);
1679 seq_printf(seq, "\nResult: NA\n");
1686 static ssize_t pktgen_thread_write(struct file *file,
1687 const char __user * user_buffer,
1688 size_t count, loff_t * offset)
1690 struct seq_file *seq = (struct seq_file *)file->private_data;
1691 struct pktgen_thread *t = seq->private;
1692 int i = 0, max, len, ret;
1697 // sprintf(pg_result, "Wrong command format");
1702 len = count_trail_chars(&user_buffer[i], max);
1708 /* Read variable name */
1710 len = strn_len(&user_buffer[i], sizeof(name) - 1);
1714 memset(name, 0, sizeof(name));
1715 if (copy_from_user(name, &user_buffer[i], len))
1720 len = count_trail_chars(&user_buffer[i], max);
1727 printk(KERN_DEBUG "pktgen: t=%s, count=%lu\n",
1728 name, (unsigned long)count);
1731 printk(KERN_ERR "pktgen: ERROR: No thread\n");
1736 pg_result = &(t->result[0]);
1738 if (!strcmp(name, "add_device")) {
1741 len = strn_len(&user_buffer[i], sizeof(f) - 1);
1746 if (copy_from_user(f, &user_buffer[i], len))
1749 mutex_lock(&pktgen_thread_lock);
1750 pktgen_add_device(t, f);
1751 mutex_unlock(&pktgen_thread_lock);
1753 sprintf(pg_result, "OK: add_device=%s", f);
1757 if (!strcmp(name, "rem_device_all")) {
1758 mutex_lock(&pktgen_thread_lock);
1759 t->control |= T_REMDEVALL;
1760 mutex_unlock(&pktgen_thread_lock);
1761 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
1763 sprintf(pg_result, "OK: rem_device_all");
1767 if (!strcmp(name, "max_before_softirq")) {
1768 sprintf(pg_result, "OK: Note! max_before_softirq is obsoleted -- Do not use");
1778 static int pktgen_thread_open(struct inode *inode, struct file *file)
1780 return single_open(file, pktgen_thread_show, PDE(inode)->data);
1783 static const struct file_operations pktgen_thread_fops = {
1784 .owner = THIS_MODULE,
1785 .open = pktgen_thread_open,
1787 .llseek = seq_lseek,
1788 .write = pktgen_thread_write,
1789 .release = single_release,
1792 /* Think find or remove for NN */
1793 static struct pktgen_dev *__pktgen_NN_threads(const char *ifname, int remove)
1795 struct pktgen_thread *t;
1796 struct pktgen_dev *pkt_dev = NULL;
1798 list_for_each_entry(t, &pktgen_threads, th_list) {
1799 pkt_dev = pktgen_find_dev(t, ifname);
1803 pkt_dev->removal_mark = 1;
1804 t->control |= T_REMDEV;
1814 * mark a device for removal
1816 static void pktgen_mark_device(const char *ifname)
1818 struct pktgen_dev *pkt_dev = NULL;
1819 const int max_tries = 10, msec_per_try = 125;
1822 mutex_lock(&pktgen_thread_lock);
1823 pr_debug("pktgen: pktgen_mark_device marking %s for removal\n", ifname);
1827 pkt_dev = __pktgen_NN_threads(ifname, REMOVE);
1828 if (pkt_dev == NULL)
1829 break; /* success */
1831 mutex_unlock(&pktgen_thread_lock);
1832 pr_debug("pktgen: pktgen_mark_device waiting for %s "
1833 "to disappear....\n", ifname);
1834 schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
1835 mutex_lock(&pktgen_thread_lock);
1837 if (++i >= max_tries) {
1838 printk(KERN_ERR "pktgen_mark_device: timed out after "
1839 "waiting %d msec for device %s to be removed\n",
1840 msec_per_try * i, ifname);
1846 mutex_unlock(&pktgen_thread_lock);
1849 static void pktgen_change_name(struct net_device *dev)
1851 struct pktgen_thread *t;
1853 list_for_each_entry(t, &pktgen_threads, th_list) {
1854 struct pktgen_dev *pkt_dev;
1856 list_for_each_entry(pkt_dev, &t->if_list, list) {
1857 if (pkt_dev->odev != dev)
1860 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
1862 pkt_dev->entry = create_proc_entry(dev->name, 0600,
1864 if (!pkt_dev->entry)
1865 printk(KERN_ERR "pktgen: can't move proc "
1866 " entry for '%s'\n", dev->name);
1872 static int pktgen_device_event(struct notifier_block *unused,
1873 unsigned long event, void *ptr)
1875 struct net_device *dev = ptr;
1877 if (dev->nd_net != &init_net)
1880 /* It is OK that we do not hold the group lock right now,
1881 * as we run under the RTNL lock.
1885 case NETDEV_CHANGENAME:
1886 pktgen_change_name(dev);
1889 case NETDEV_UNREGISTER:
1890 pktgen_mark_device(dev->name);
1897 /* Associate pktgen_dev with a device. */
1899 static int pktgen_setup_dev(struct pktgen_dev *pkt_dev, const char *ifname)
1901 struct net_device *odev;
1904 /* Clean old setups */
1905 if (pkt_dev->odev) {
1906 dev_put(pkt_dev->odev);
1907 pkt_dev->odev = NULL;
1910 odev = dev_get_by_name(&init_net, ifname);
1912 printk(KERN_ERR "pktgen: no such netdevice: \"%s\"\n", ifname);
1916 if (odev->type != ARPHRD_ETHER) {
1917 printk(KERN_ERR "pktgen: not an ethernet device: \"%s\"\n", ifname);
1919 } else if (!netif_running(odev)) {
1920 printk(KERN_ERR "pktgen: device is down: \"%s\"\n", ifname);
1923 pkt_dev->odev = odev;
1931 /* Read pkt_dev from the interface and set up internal pktgen_dev
1932 * structure to have the right information to create/send packets
1934 static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
1936 if (!pkt_dev->odev) {
1937 printk(KERN_ERR "pktgen: ERROR: pkt_dev->odev == NULL in "
1939 sprintf(pkt_dev->result,
1940 "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
1944 /* Default to the interface's mac if not explicitly set. */
1946 if (is_zero_ether_addr(pkt_dev->src_mac))
1947 memcpy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr, ETH_ALEN);
1949 /* Set up Dest MAC */
1950 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
1952 /* Set up pkt size */
1953 pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
1955 if (pkt_dev->flags & F_IPV6) {
1957 * Skip this automatic address setting until locks or functions
1962 int i, set = 0, err = 1;
1963 struct inet6_dev *idev;
1965 for (i = 0; i < IN6_ADDR_HSIZE; i++)
1966 if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
1974 * Use linklevel address if unconfigured.
1976 * use ipv6_get_lladdr if/when it's get exported
1980 if ((idev = __in6_dev_get(pkt_dev->odev)) != NULL) {
1981 struct inet6_ifaddr *ifp;
1983 read_lock_bh(&idev->lock);
1984 for (ifp = idev->addr_list; ifp;
1985 ifp = ifp->if_next) {
1986 if (ifp->scope == IFA_LINK
1988 flags & IFA_F_TENTATIVE)) {
1989 ipv6_addr_copy(&pkt_dev->
1996 read_unlock_bh(&idev->lock);
2000 printk(KERN_ERR "pktgen: ERROR: IPv6 link "
2001 "address not availble.\n");
2005 pkt_dev->saddr_min = 0;
2006 pkt_dev->saddr_max = 0;
2007 if (strlen(pkt_dev->src_min) == 0) {
2009 struct in_device *in_dev;
2012 in_dev = __in_dev_get_rcu(pkt_dev->odev);
2014 if (in_dev->ifa_list) {
2015 pkt_dev->saddr_min =
2016 in_dev->ifa_list->ifa_address;
2017 pkt_dev->saddr_max = pkt_dev->saddr_min;
2022 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
2023 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
2026 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
2027 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
2029 /* Initialize current values. */
2030 pkt_dev->cur_dst_mac_offset = 0;
2031 pkt_dev->cur_src_mac_offset = 0;
2032 pkt_dev->cur_saddr = pkt_dev->saddr_min;
2033 pkt_dev->cur_daddr = pkt_dev->daddr_min;
2034 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2035 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
2036 pkt_dev->nflows = 0;
2039 static void spin(struct pktgen_dev *pkt_dev, __u64 spin_until_us)
2044 start = now = getCurUs();
2045 while (now < spin_until_us) {
2046 /* TODO: optimize sleeping behavior */
2047 if (spin_until_us - now > jiffies_to_usecs(1) + 1)
2048 schedule_timeout_interruptible(1);
2049 else if (spin_until_us - now > 100) {
2050 if (!pkt_dev->running)
2059 pkt_dev->idle_acc += now - start;
2062 static inline void set_pkt_overhead(struct pktgen_dev *pkt_dev)
2064 pkt_dev->pkt_overhead = 0;
2065 pkt_dev->pkt_overhead += pkt_dev->nr_labels*sizeof(u32);
2066 pkt_dev->pkt_overhead += VLAN_TAG_SIZE(pkt_dev);
2067 pkt_dev->pkt_overhead += SVLAN_TAG_SIZE(pkt_dev);
2070 static inline int f_seen(struct pktgen_dev *pkt_dev, int flow)
2073 if (pkt_dev->flows[flow].flags & F_INIT)
2079 static inline int f_pick(struct pktgen_dev *pkt_dev)
2081 int flow = pkt_dev->curfl;
2083 if (pkt_dev->flags & F_FLOW_SEQ) {
2084 if (pkt_dev->flows[flow].count >= pkt_dev->lflow) {
2086 pkt_dev->flows[flow].count = 0;
2087 pkt_dev->curfl += 1;
2088 if (pkt_dev->curfl >= pkt_dev->cflows)
2089 pkt_dev->curfl = 0; /*reset */
2092 flow = random32() % pkt_dev->cflows;
2094 if (pkt_dev->flows[flow].count > pkt_dev->lflow)
2095 pkt_dev->flows[flow].count = 0;
2098 return pkt_dev->curfl;
2103 /* If there was already an IPSEC SA, we keep it as is, else
2104 * we go look for it ...
2106 static void get_ipsec_sa(struct pktgen_dev *pkt_dev, int flow)
2108 struct xfrm_state *x = pkt_dev->flows[flow].x;
2110 /*slow path: we dont already have xfrm_state*/
2111 x = xfrm_stateonly_find((xfrm_address_t *)&pkt_dev->cur_daddr,
2112 (xfrm_address_t *)&pkt_dev->cur_saddr,
2115 pkt_dev->ipsproto, 0);
2117 pkt_dev->flows[flow].x = x;
2118 set_pkt_overhead(pkt_dev);
2119 pkt_dev->pkt_overhead+=x->props.header_len;
2125 /* Increment/randomize headers according to flags and current values
2126 * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
2128 static void mod_cur_headers(struct pktgen_dev *pkt_dev)
2134 if (pkt_dev->cflows)
2135 flow = f_pick(pkt_dev);
2137 /* Deal with source MAC */
2138 if (pkt_dev->src_mac_count > 1) {
2142 if (pkt_dev->flags & F_MACSRC_RND)
2143 mc = random32() % pkt_dev->src_mac_count;
2145 mc = pkt_dev->cur_src_mac_offset++;
2146 if (pkt_dev->cur_src_mac_offset >
2147 pkt_dev->src_mac_count)
2148 pkt_dev->cur_src_mac_offset = 0;
2151 tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
2152 pkt_dev->hh[11] = tmp;
2153 tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2154 pkt_dev->hh[10] = tmp;
2155 tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2156 pkt_dev->hh[9] = tmp;
2157 tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2158 pkt_dev->hh[8] = tmp;
2159 tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
2160 pkt_dev->hh[7] = tmp;
2163 /* Deal with Destination MAC */
2164 if (pkt_dev->dst_mac_count > 1) {
2168 if (pkt_dev->flags & F_MACDST_RND)
2169 mc = random32() % pkt_dev->dst_mac_count;
2172 mc = pkt_dev->cur_dst_mac_offset++;
2173 if (pkt_dev->cur_dst_mac_offset >
2174 pkt_dev->dst_mac_count) {
2175 pkt_dev->cur_dst_mac_offset = 0;
2179 tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
2180 pkt_dev->hh[5] = tmp;
2181 tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2182 pkt_dev->hh[4] = tmp;
2183 tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2184 pkt_dev->hh[3] = tmp;
2185 tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2186 pkt_dev->hh[2] = tmp;
2187 tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
2188 pkt_dev->hh[1] = tmp;
2191 if (pkt_dev->flags & F_MPLS_RND) {
2193 for (i = 0; i < pkt_dev->nr_labels; i++)
2194 if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
2195 pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
2196 ((__force __be32)random32() &
2200 if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
2201 pkt_dev->vlan_id = random32() & (4096-1);
2204 if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
2205 pkt_dev->svlan_id = random32() & (4096 - 1);
2208 if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
2209 if (pkt_dev->flags & F_UDPSRC_RND)
2210 pkt_dev->cur_udp_src = random32() %
2211 (pkt_dev->udp_src_max - pkt_dev->udp_src_min)
2212 + pkt_dev->udp_src_min;
2215 pkt_dev->cur_udp_src++;
2216 if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
2217 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
2221 if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
2222 if (pkt_dev->flags & F_UDPDST_RND) {
2223 pkt_dev->cur_udp_dst = random32() %
2224 (pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)
2225 + pkt_dev->udp_dst_min;
2227 pkt_dev->cur_udp_dst++;
2228 if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
2229 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2233 if (!(pkt_dev->flags & F_IPV6)) {
2235 if ((imn = ntohl(pkt_dev->saddr_min)) < (imx =
2239 if (pkt_dev->flags & F_IPSRC_RND)
2240 t = random32() % (imx - imn) + imn;
2242 t = ntohl(pkt_dev->cur_saddr);
2248 pkt_dev->cur_saddr = htonl(t);
2251 if (pkt_dev->cflows && f_seen(pkt_dev, flow)) {
2252 pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
2254 imn = ntohl(pkt_dev->daddr_min);
2255 imx = ntohl(pkt_dev->daddr_max);
2259 if (pkt_dev->flags & F_IPDST_RND) {
2261 t = random32() % (imx - imn) + imn;
2264 while (ipv4_is_loopback(s) ||
2265 ipv4_is_multicast(s) ||
2266 ipv4_is_lbcast(s) ||
2267 ipv4_is_zeronet(s) ||
2268 ipv4_is_local_multicast(s)) {
2269 t = random32() % (imx - imn) + imn;
2272 pkt_dev->cur_daddr = s;
2274 t = ntohl(pkt_dev->cur_daddr);
2279 pkt_dev->cur_daddr = htonl(t);
2282 if (pkt_dev->cflows) {
2283 pkt_dev->flows[flow].flags |= F_INIT;
2284 pkt_dev->flows[flow].cur_daddr =
2287 if (pkt_dev->flags & F_IPSEC_ON)
2288 get_ipsec_sa(pkt_dev, flow);
2293 } else { /* IPV6 * */
2295 if (pkt_dev->min_in6_daddr.s6_addr32[0] == 0 &&
2296 pkt_dev->min_in6_daddr.s6_addr32[1] == 0 &&
2297 pkt_dev->min_in6_daddr.s6_addr32[2] == 0 &&
2298 pkt_dev->min_in6_daddr.s6_addr32[3] == 0) ;
2302 /* Only random destinations yet */
2304 for (i = 0; i < 4; i++) {
2305 pkt_dev->cur_in6_daddr.s6_addr32[i] =
2306 (((__force __be32)random32() |
2307 pkt_dev->min_in6_daddr.s6_addr32[i]) &
2308 pkt_dev->max_in6_daddr.s6_addr32[i]);
2313 if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
2315 if (pkt_dev->flags & F_TXSIZE_RND) {
2317 (pkt_dev->max_pkt_size - pkt_dev->min_pkt_size)
2318 + pkt_dev->min_pkt_size;
2320 t = pkt_dev->cur_pkt_size + 1;
2321 if (t > pkt_dev->max_pkt_size)
2322 t = pkt_dev->min_pkt_size;
2324 pkt_dev->cur_pkt_size = t;
2327 if (pkt_dev->queue_map_min < pkt_dev->queue_map_max) {
2329 if (pkt_dev->flags & F_QUEUE_MAP_RND) {
2331 (pkt_dev->queue_map_max - pkt_dev->queue_map_min + 1)
2332 + pkt_dev->queue_map_min;
2334 t = pkt_dev->cur_queue_map + 1;
2335 if (t > pkt_dev->queue_map_max)
2336 t = pkt_dev->queue_map_min;
2338 pkt_dev->cur_queue_map = t;
2341 pkt_dev->flows[flow].count++;
2346 static int pktgen_output_ipsec(struct sk_buff *skb, struct pktgen_dev *pkt_dev)
2348 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2354 /* XXX: we dont support tunnel mode for now until
2355 * we resolve the dst issue */
2356 if (x->props.mode != XFRM_MODE_TRANSPORT)
2359 spin_lock(&x->lock);
2362 err = x->outer_mode->output(x, skb);
2365 err = x->type->output(x, skb);
2369 x->curlft.bytes +=skb->len;
2370 x->curlft.packets++;
2372 spin_unlock(&x->lock);
2376 static inline void free_SAs(struct pktgen_dev *pkt_dev)
2378 if (pkt_dev->cflows) {
2379 /* let go of the SAs if we have them */
2381 for (; i < pkt_dev->nflows; i++){
2382 struct xfrm_state *x = pkt_dev->flows[i].x;
2385 pkt_dev->flows[i].x = NULL;
2391 static inline int process_ipsec(struct pktgen_dev *pkt_dev,
2392 struct sk_buff *skb, __be16 protocol)
2394 if (pkt_dev->flags & F_IPSEC_ON) {
2395 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2400 nhead = x->props.header_len - skb_headroom(skb);
2402 ret = pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
2404 printk(KERN_ERR "Error expanding "
2405 "ipsec packet %d\n",ret);
2410 /* ipsec is not expecting ll header */
2411 skb_pull(skb, ETH_HLEN);
2412 ret = pktgen_output_ipsec(skb, pkt_dev);
2414 printk(KERN_ERR "Error creating ipsec "
2420 eth = (__u8 *) skb_push(skb, ETH_HLEN);
2421 memcpy(eth, pkt_dev->hh, 12);
2422 *(u16 *) & eth[12] = protocol;
2429 static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
2432 for (i = 0; i < pkt_dev->nr_labels; i++) {
2433 *mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
2436 *mpls |= MPLS_STACK_BOTTOM;
2439 static inline __be16 build_tci(unsigned int id, unsigned int cfi,
2442 return htons(id | (cfi << 12) | (prio << 13));
2445 static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
2446 struct pktgen_dev *pkt_dev)
2448 struct sk_buff *skb = NULL;
2450 struct udphdr *udph;
2453 struct pktgen_hdr *pgh = NULL;
2454 __be16 protocol = htons(ETH_P_IP);
2456 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2457 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2458 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2459 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2462 if (pkt_dev->nr_labels)
2463 protocol = htons(ETH_P_MPLS_UC);
2465 if (pkt_dev->vlan_id != 0xffff)
2466 protocol = htons(ETH_P_8021Q);
2468 /* Update any of the values, used when we're incrementing various
2471 mod_cur_headers(pkt_dev);
2473 datalen = (odev->hard_header_len + 16) & ~0xf;
2474 skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + datalen +
2475 pkt_dev->pkt_overhead, GFP_ATOMIC);
2477 sprintf(pkt_dev->result, "No memory");
2481 skb_reserve(skb, datalen);
2483 /* Reserve for ethernet and IP header */
2484 eth = (__u8 *) skb_push(skb, 14);
2485 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2486 if (pkt_dev->nr_labels)
2487 mpls_push(mpls, pkt_dev);
2489 if (pkt_dev->vlan_id != 0xffff) {
2490 if (pkt_dev->svlan_id != 0xffff) {
2491 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2492 *svlan_tci = build_tci(pkt_dev->svlan_id,
2495 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2496 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
2498 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2499 *vlan_tci = build_tci(pkt_dev->vlan_id,
2502 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2503 *vlan_encapsulated_proto = htons(ETH_P_IP);
2506 skb->network_header = skb->tail;
2507 skb->transport_header = skb->network_header + sizeof(struct iphdr);
2508 skb_put(skb, sizeof(struct iphdr) + sizeof(struct udphdr));
2509 skb_set_queue_mapping(skb, pkt_dev->cur_queue_map);
2511 udph = udp_hdr(skb);
2513 memcpy(eth, pkt_dev->hh, 12);
2514 *(__be16 *) & eth[12] = protocol;
2516 /* Eth + IPh + UDPh + mpls */
2517 datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
2518 pkt_dev->pkt_overhead;
2519 if (datalen < sizeof(struct pktgen_hdr))
2520 datalen = sizeof(struct pktgen_hdr);
2522 udph->source = htons(pkt_dev->cur_udp_src);
2523 udph->dest = htons(pkt_dev->cur_udp_dst);
2524 udph->len = htons(datalen + 8); /* DATA + udphdr */
2525 udph->check = 0; /* No checksum */
2530 iph->tos = pkt_dev->tos;
2531 iph->protocol = IPPROTO_UDP; /* UDP */
2532 iph->saddr = pkt_dev->cur_saddr;
2533 iph->daddr = pkt_dev->cur_daddr;
2535 iplen = 20 + 8 + datalen;
2536 iph->tot_len = htons(iplen);
2538 iph->check = ip_fast_csum((void *)iph, iph->ihl);
2539 skb->protocol = protocol;
2540 skb->mac_header = (skb->network_header - ETH_HLEN -
2541 pkt_dev->pkt_overhead);
2543 skb->pkt_type = PACKET_HOST;
2545 if (pkt_dev->nfrags <= 0)
2546 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
2548 int frags = pkt_dev->nfrags;
2551 pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
2553 if (frags > MAX_SKB_FRAGS)
2554 frags = MAX_SKB_FRAGS;
2555 if (datalen > frags * PAGE_SIZE) {
2556 skb_put(skb, datalen - frags * PAGE_SIZE);
2557 datalen = frags * PAGE_SIZE;
2561 while (datalen > 0) {
2562 struct page *page = alloc_pages(GFP_KERNEL, 0);
2563 skb_shinfo(skb)->frags[i].page = page;
2564 skb_shinfo(skb)->frags[i].page_offset = 0;
2565 skb_shinfo(skb)->frags[i].size =
2566 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
2567 datalen -= skb_shinfo(skb)->frags[i].size;
2568 skb->len += skb_shinfo(skb)->frags[i].size;
2569 skb->data_len += skb_shinfo(skb)->frags[i].size;
2571 skb_shinfo(skb)->nr_frags = i;
2580 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
2584 skb_shinfo(skb)->frags[i - 1].size -= rem;
2586 skb_shinfo(skb)->frags[i] =
2587 skb_shinfo(skb)->frags[i - 1];
2588 get_page(skb_shinfo(skb)->frags[i].page);
2589 skb_shinfo(skb)->frags[i].page =
2590 skb_shinfo(skb)->frags[i - 1].page;
2591 skb_shinfo(skb)->frags[i].page_offset +=
2592 skb_shinfo(skb)->frags[i - 1].size;
2593 skb_shinfo(skb)->frags[i].size = rem;
2595 skb_shinfo(skb)->nr_frags = i;
2599 /* Stamp the time, and sequence number, convert them to network byte order */
2602 struct timeval timestamp;
2604 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2605 pgh->seq_num = htonl(pkt_dev->seq_num);
2607 do_gettimeofday(×tamp);
2608 pgh->tv_sec = htonl(timestamp.tv_sec);
2609 pgh->tv_usec = htonl(timestamp.tv_usec);
2613 if (!process_ipsec(pkt_dev, skb, protocol))
2621 * scan_ip6, fmt_ip taken from dietlibc-0.21
2622 * Author Felix von Leitner <felix-dietlibc@fefe.de>
2624 * Slightly modified for kernel.
2625 * Should be candidate for net/ipv4/utils.c
2629 static unsigned int scan_ip6(const char *s, char ip[16])
2632 unsigned int len = 0;
2635 unsigned int prefixlen = 0;
2636 unsigned int suffixlen = 0;
2640 for (i = 0; i < 16; i++)
2646 if (s[1] == ':') { /* Found "::", skip to part 2 */
2654 u = simple_strtoul(s, &pos, 16);
2658 if (prefixlen == 12 && s[i] == '.') {
2660 /* the last 4 bytes may be written as IPv4 address */
2663 memcpy((struct in_addr *)(ip + 12), &tmp, sizeof(tmp));
2666 ip[prefixlen++] = (u >> 8);
2667 ip[prefixlen++] = (u & 255);
2670 if (prefixlen == 16)
2674 /* part 2, after "::" */
2681 } else if (suffixlen != 0)
2684 u = simple_strtol(s, &pos, 16);
2691 if (suffixlen + prefixlen <= 12 && s[i] == '.') {
2693 memcpy((struct in_addr *)(suffix + suffixlen), &tmp,
2699 suffix[suffixlen++] = (u >> 8);
2700 suffix[suffixlen++] = (u & 255);
2703 if (prefixlen + suffixlen == 16)
2706 for (i = 0; i < suffixlen; i++)
2707 ip[16 - suffixlen + i] = suffix[i];
2711 static char tohex(char hexdigit)
2713 return hexdigit > 9 ? hexdigit + 'a' - 10 : hexdigit + '0';
2716 static int fmt_xlong(char *s, unsigned int i)
2719 *s = tohex((i >> 12) & 0xf);
2720 if (s != bak || *s != '0')
2722 *s = tohex((i >> 8) & 0xf);
2723 if (s != bak || *s != '0')
2725 *s = tohex((i >> 4) & 0xf);
2726 if (s != bak || *s != '0')
2728 *s = tohex(i & 0xf);
2732 static unsigned int fmt_ip6(char *s, const char ip[16])
2737 unsigned int compressing;
2742 for (j = 0; j < 16; j += 2) {
2744 #ifdef V4MAPPEDPREFIX
2745 if (j == 12 && !memcmp(ip, V4mappedprefix, 12)) {
2746 inet_ntoa_r(*(struct in_addr *)(ip + 12), s);
2751 temp = ((unsigned long)(unsigned char)ip[j] << 8) +
2752 (unsigned long)(unsigned char)ip[j + 1];
2767 i = fmt_xlong(s, temp);
2784 static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
2785 struct pktgen_dev *pkt_dev)
2787 struct sk_buff *skb = NULL;
2789 struct udphdr *udph;
2791 struct ipv6hdr *iph;
2792 struct pktgen_hdr *pgh = NULL;
2793 __be16 protocol = htons(ETH_P_IPV6);
2795 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2796 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2797 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2798 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2800 if (pkt_dev->nr_labels)
2801 protocol = htons(ETH_P_MPLS_UC);
2803 if (pkt_dev->vlan_id != 0xffff)
2804 protocol = htons(ETH_P_8021Q);
2806 /* Update any of the values, used when we're incrementing various
2809 mod_cur_headers(pkt_dev);
2811 skb = alloc_skb(pkt_dev->cur_pkt_size + 64 + 16 +
2812 pkt_dev->pkt_overhead, GFP_ATOMIC);
2814 sprintf(pkt_dev->result, "No memory");
2818 skb_reserve(skb, 16);
2820 /* Reserve for ethernet and IP header */
2821 eth = (__u8 *) skb_push(skb, 14);
2822 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2823 if (pkt_dev->nr_labels)
2824 mpls_push(mpls, pkt_dev);
2826 if (pkt_dev->vlan_id != 0xffff) {
2827 if (pkt_dev->svlan_id != 0xffff) {
2828 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2829 *svlan_tci = build_tci(pkt_dev->svlan_id,
2832 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2833 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
2835 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2836 *vlan_tci = build_tci(pkt_dev->vlan_id,
2839 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2840 *vlan_encapsulated_proto = htons(ETH_P_IPV6);
2843 skb->network_header = skb->tail;
2844 skb->transport_header = skb->network_header + sizeof(struct ipv6hdr);
2845 skb_put(skb, sizeof(struct ipv6hdr) + sizeof(struct udphdr));
2846 skb_set_queue_mapping(skb, pkt_dev->cur_queue_map);
2847 iph = ipv6_hdr(skb);
2848 udph = udp_hdr(skb);
2850 memcpy(eth, pkt_dev->hh, 12);
2851 *(__be16 *) & eth[12] = protocol;
2853 /* Eth + IPh + UDPh + mpls */
2854 datalen = pkt_dev->cur_pkt_size - 14 -
2855 sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
2856 pkt_dev->pkt_overhead;
2858 if (datalen < sizeof(struct pktgen_hdr)) {
2859 datalen = sizeof(struct pktgen_hdr);
2860 if (net_ratelimit())
2861 printk(KERN_INFO "pktgen: increased datalen to %d\n",
2865 udph->source = htons(pkt_dev->cur_udp_src);
2866 udph->dest = htons(pkt_dev->cur_udp_dst);
2867 udph->len = htons(datalen + sizeof(struct udphdr));
2868 udph->check = 0; /* No checksum */
2870 *(__be32 *) iph = htonl(0x60000000); /* Version + flow */
2872 if (pkt_dev->traffic_class) {
2873 /* Version + traffic class + flow (0) */
2874 *(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
2877 iph->hop_limit = 32;
2879 iph->payload_len = htons(sizeof(struct udphdr) + datalen);
2880 iph->nexthdr = IPPROTO_UDP;
2882 ipv6_addr_copy(&iph->daddr, &pkt_dev->cur_in6_daddr);
2883 ipv6_addr_copy(&iph->saddr, &pkt_dev->cur_in6_saddr);
2885 skb->mac_header = (skb->network_header - ETH_HLEN -
2886 pkt_dev->pkt_overhead);
2887 skb->protocol = protocol;
2889 skb->pkt_type = PACKET_HOST;
2891 if (pkt_dev->nfrags <= 0)
2892 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
2894 int frags = pkt_dev->nfrags;
2897 pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
2899 if (frags > MAX_SKB_FRAGS)
2900 frags = MAX_SKB_FRAGS;
2901 if (datalen > frags * PAGE_SIZE) {
2902 skb_put(skb, datalen - frags * PAGE_SIZE);
2903 datalen = frags * PAGE_SIZE;
2907 while (datalen > 0) {
2908 struct page *page = alloc_pages(GFP_KERNEL, 0);
2909 skb_shinfo(skb)->frags[i].page = page;
2910 skb_shinfo(skb)->frags[i].page_offset = 0;
2911 skb_shinfo(skb)->frags[i].size =
2912 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
2913 datalen -= skb_shinfo(skb)->frags[i].size;
2914 skb->len += skb_shinfo(skb)->frags[i].size;
2915 skb->data_len += skb_shinfo(skb)->frags[i].size;
2917 skb_shinfo(skb)->nr_frags = i;
2926 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
2930 skb_shinfo(skb)->frags[i - 1].size -= rem;
2932 skb_shinfo(skb)->frags[i] =
2933 skb_shinfo(skb)->frags[i - 1];
2934 get_page(skb_shinfo(skb)->frags[i].page);
2935 skb_shinfo(skb)->frags[i].page =
2936 skb_shinfo(skb)->frags[i - 1].page;
2937 skb_shinfo(skb)->frags[i].page_offset +=
2938 skb_shinfo(skb)->frags[i - 1].size;
2939 skb_shinfo(skb)->frags[i].size = rem;
2941 skb_shinfo(skb)->nr_frags = i;
2945 /* Stamp the time, and sequence number, convert them to network byte order */
2946 /* should we update cloned packets too ? */
2948 struct timeval timestamp;
2950 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2951 pgh->seq_num = htonl(pkt_dev->seq_num);
2953 do_gettimeofday(×tamp);
2954 pgh->tv_sec = htonl(timestamp.tv_sec);
2955 pgh->tv_usec = htonl(timestamp.tv_usec);
2957 /* pkt_dev->seq_num++; FF: you really mean this? */
2962 static inline struct sk_buff *fill_packet(struct net_device *odev,
2963 struct pktgen_dev *pkt_dev)
2965 if (pkt_dev->flags & F_IPV6)
2966 return fill_packet_ipv6(odev, pkt_dev);
2968 return fill_packet_ipv4(odev, pkt_dev);
2971 static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
2973 pkt_dev->seq_num = 1;
2974 pkt_dev->idle_acc = 0;
2976 pkt_dev->tx_bytes = 0;
2977 pkt_dev->errors = 0;
2980 /* Set up structure for sending pkts, clear counters */
2982 static void pktgen_run(struct pktgen_thread *t)
2984 struct pktgen_dev *pkt_dev;
2987 pr_debug("pktgen: entering pktgen_run. %p\n", t);
2990 list_for_each_entry(pkt_dev, &t->if_list, list) {
2993 * setup odev and create initial packet.
2995 pktgen_setup_inject(pkt_dev);
2997 if (pkt_dev->odev) {
2998 pktgen_clear_counters(pkt_dev);
2999 pkt_dev->running = 1; /* Cranke yeself! */
3000 pkt_dev->skb = NULL;
3001 pkt_dev->started_at = getCurUs();
3002 pkt_dev->next_tx_us = getCurUs(); /* Transmit immediately */
3003 pkt_dev->next_tx_ns = 0;
3004 set_pkt_overhead(pkt_dev);
3006 strcpy(pkt_dev->result, "Starting");
3009 strcpy(pkt_dev->result, "Error starting");
3013 t->control &= ~(T_STOP);
3016 static void pktgen_stop_all_threads_ifs(void)
3018 struct pktgen_thread *t;
3020 pr_debug("pktgen: entering pktgen_stop_all_threads_ifs.\n");
3022 mutex_lock(&pktgen_thread_lock);
3024 list_for_each_entry(t, &pktgen_threads, th_list)
3025 t->control |= T_STOP;
3027 mutex_unlock(&pktgen_thread_lock);
3030 static int thread_is_running(struct pktgen_thread *t)
3032 struct pktgen_dev *pkt_dev;
3035 list_for_each_entry(pkt_dev, &t->if_list, list)
3036 if (pkt_dev->running) {
3043 static int pktgen_wait_thread_run(struct pktgen_thread *t)
3047 while (thread_is_running(t)) {
3051 msleep_interruptible(100);
3053 if (signal_pending(current))
3063 static int pktgen_wait_all_threads_run(void)
3065 struct pktgen_thread *t;
3068 mutex_lock(&pktgen_thread_lock);
3070 list_for_each_entry(t, &pktgen_threads, th_list) {
3071 sig = pktgen_wait_thread_run(t);
3077 list_for_each_entry(t, &pktgen_threads, th_list)
3078 t->control |= (T_STOP);
3080 mutex_unlock(&pktgen_thread_lock);
3084 static void pktgen_run_all_threads(void)
3086 struct pktgen_thread *t;
3088 pr_debug("pktgen: entering pktgen_run_all_threads.\n");
3090 mutex_lock(&pktgen_thread_lock);
3092 list_for_each_entry(t, &pktgen_threads, th_list)
3093 t->control |= (T_RUN);
3095 mutex_unlock(&pktgen_thread_lock);
3097 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
3099 pktgen_wait_all_threads_run();
3102 static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
3104 __u64 total_us, bps, mbps, pps, idle;
3105 char *p = pkt_dev->result;
3107 total_us = pkt_dev->stopped_at - pkt_dev->started_at;
3109 idle = pkt_dev->idle_acc;
3111 p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
3112 (unsigned long long)total_us,
3113 (unsigned long long)(total_us - idle),
3114 (unsigned long long)idle,
3115 (unsigned long long)pkt_dev->sofar,
3116 pkt_dev->cur_pkt_size, nr_frags);
3118 pps = pkt_dev->sofar * USEC_PER_SEC;
3120 while ((total_us >> 32) != 0) {
3125 do_div(pps, total_us);
3127 bps = pps * 8 * pkt_dev->cur_pkt_size;
3130 do_div(mbps, 1000000);
3131 p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu",
3132 (unsigned long long)pps,
3133 (unsigned long long)mbps,
3134 (unsigned long long)bps,
3135 (unsigned long long)pkt_dev->errors);
3138 /* Set stopped-at timer, remove from running list, do counters & statistics */
3140 static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
3142 int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
3144 if (!pkt_dev->running) {
3145 printk(KERN_WARNING "pktgen: interface: %s is already "
3146 "stopped\n", pkt_dev->odev->name);
3150 pkt_dev->stopped_at = getCurUs();
3151 pkt_dev->running = 0;
3153 show_results(pkt_dev, nr_frags);
3158 static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
3160 struct pktgen_dev *pkt_dev, *best = NULL;
3164 list_for_each_entry(pkt_dev, &t->if_list, list) {
3165 if (!pkt_dev->running)
3169 else if (pkt_dev->next_tx_us < best->next_tx_us)
3176 static void pktgen_stop(struct pktgen_thread *t)
3178 struct pktgen_dev *pkt_dev;
3180 pr_debug("pktgen: entering pktgen_stop\n");
3184 list_for_each_entry(pkt_dev, &t->if_list, list) {
3185 pktgen_stop_device(pkt_dev);
3187 kfree_skb(pkt_dev->skb);
3189 pkt_dev->skb = NULL;
3196 * one of our devices needs to be removed - find it
3199 static void pktgen_rem_one_if(struct pktgen_thread *t)
3201 struct list_head *q, *n;
3202 struct pktgen_dev *cur;
3204 pr_debug("pktgen: entering pktgen_rem_one_if\n");
3208 list_for_each_safe(q, n, &t->if_list) {
3209 cur = list_entry(q, struct pktgen_dev, list);
3211 if (!cur->removal_mark)
3215 kfree_skb(cur->skb);
3218 pktgen_remove_device(t, cur);
3226 static void pktgen_rem_all_ifs(struct pktgen_thread *t)
3228 struct list_head *q, *n;
3229 struct pktgen_dev *cur;
3231 /* Remove all devices, free mem */
3233 pr_debug("pktgen: entering pktgen_rem_all_ifs\n");
3236 list_for_each_safe(q, n, &t->if_list) {
3237 cur = list_entry(q, struct pktgen_dev, list);
3240 kfree_skb(cur->skb);
3243 pktgen_remove_device(t, cur);
3249 static void pktgen_rem_thread(struct pktgen_thread *t)
3251 /* Remove from the thread list */
3253 remove_proc_entry(t->tsk->comm, pg_proc_dir);
3255 mutex_lock(&pktgen_thread_lock);
3257 list_del(&t->th_list);
3259 mutex_unlock(&pktgen_thread_lock);
3262 static __inline__ void pktgen_xmit(struct pktgen_dev *pkt_dev)
3264 struct net_device *odev = NULL;
3265 __u64 idle_start = 0;
3268 odev = pkt_dev->odev;
3270 if (pkt_dev->delay_us || pkt_dev->delay_ns) {
3274 if (now < pkt_dev->next_tx_us)
3275 spin(pkt_dev, pkt_dev->next_tx_us);
3277 /* This is max DELAY, this has special meaning of
3280 if (pkt_dev->delay_us == 0x7FFFFFFF) {
3281 pkt_dev->next_tx_us = getCurUs() + pkt_dev->delay_us;
3282 pkt_dev->next_tx_ns = pkt_dev->delay_ns;
3287 if ((netif_queue_stopped(odev) ||
3289 netif_subqueue_stopped(odev, pkt_dev->skb))) ||
3291 idle_start = getCurUs();
3293 if (!netif_running(odev)) {
3294 pktgen_stop_device(pkt_dev);
3296 kfree_skb(pkt_dev->skb);
3297 pkt_dev->skb = NULL;
3303 pkt_dev->idle_acc += getCurUs() - idle_start;
3305 if (netif_queue_stopped(odev) ||
3306 netif_subqueue_stopped(odev, pkt_dev->skb)) {
3307 pkt_dev->next_tx_us = getCurUs(); /* TODO */
3308 pkt_dev->next_tx_ns = 0;
3309 goto out; /* Try the next interface */
3313 if (pkt_dev->last_ok || !pkt_dev->skb) {
3314 if ((++pkt_dev->clone_count >= pkt_dev->clone_skb)
3315 || (!pkt_dev->skb)) {
3316 /* build a new pkt */
3318 kfree_skb(pkt_dev->skb);
3320 pkt_dev->skb = fill_packet(odev, pkt_dev);
3321 if (pkt_dev->skb == NULL) {
3322 printk(KERN_ERR "pktgen: ERROR: couldn't "
3323 "allocate skb in fill_packet.\n");
3325 pkt_dev->clone_count--; /* back out increment, OOM */
3328 pkt_dev->allocated_skbs++;
3329 pkt_dev->clone_count = 0; /* reset counter */
3333 netif_tx_lock_bh(odev);
3334 if (!netif_queue_stopped(odev) &&
3335 !netif_subqueue_stopped(odev, pkt_dev->skb)) {
3337 atomic_inc(&(pkt_dev->skb->users));
3339 ret = odev->hard_start_xmit(pkt_dev->skb, odev);
3340 if (likely(ret == NETDEV_TX_OK)) {
3341 pkt_dev->last_ok = 1;
3344 pkt_dev->tx_bytes += pkt_dev->cur_pkt_size;
3346 } else if (ret == NETDEV_TX_LOCKED
3347 && (odev->features & NETIF_F_LLTX)) {
3350 } else { /* Retry it next time */
3352 atomic_dec(&(pkt_dev->skb->users));
3354 if (debug && net_ratelimit())
3355 printk(KERN_INFO "pktgen: Hard xmit error\n");
3358 pkt_dev->last_ok = 0;
3361 pkt_dev->next_tx_us = getCurUs();
3362 pkt_dev->next_tx_ns = 0;
3364 pkt_dev->next_tx_us += pkt_dev->delay_us;
3365 pkt_dev->next_tx_ns += pkt_dev->delay_ns;
3367 if (pkt_dev->next_tx_ns > 1000) {
3368 pkt_dev->next_tx_us++;
3369 pkt_dev->next_tx_ns -= 1000;
3373 else { /* Retry it next time */
3374 pkt_dev->last_ok = 0;
3375 pkt_dev->next_tx_us = getCurUs(); /* TODO */
3376 pkt_dev->next_tx_ns = 0;
3379 netif_tx_unlock_bh(odev);
3381 /* If pkt_dev->count is zero, then run forever */
3382 if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
3383 if (atomic_read(&(pkt_dev->skb->users)) != 1) {
3384 idle_start = getCurUs();
3385 while (atomic_read(&(pkt_dev->skb->users)) != 1) {
3386 if (signal_pending(current)) {
3391 pkt_dev->idle_acc += getCurUs() - idle_start;
3394 /* Done with this */
3395 pktgen_stop_device(pkt_dev);
3397 kfree_skb(pkt_dev->skb);
3398 pkt_dev->skb = NULL;
3404 * Main loop of the thread goes here
3407 static int pktgen_thread_worker(void *arg)
3410 struct pktgen_thread *t = arg;
3411 struct pktgen_dev *pkt_dev = NULL;
3414 BUG_ON(smp_processor_id() != cpu);
3416 init_waitqueue_head(&t->queue);
3418 pr_debug("pktgen: starting pktgen/%d: pid=%d\n", cpu, task_pid_nr(current));
3420 set_current_state(TASK_INTERRUPTIBLE);
3424 while (!kthread_should_stop()) {
3425 pkt_dev = next_to_run(t);
3428 (t->control & (T_STOP | T_RUN | T_REMDEVALL | T_REMDEV))
3430 prepare_to_wait(&(t->queue), &wait,
3431 TASK_INTERRUPTIBLE);
3432 schedule_timeout(HZ / 10);
3433 finish_wait(&(t->queue), &wait);
3436 __set_current_state(TASK_RUNNING);
3439 pktgen_xmit(pkt_dev);
3441 if (t->control & T_STOP) {
3443 t->control &= ~(T_STOP);
3446 if (t->control & T_RUN) {
3448 t->control &= ~(T_RUN);
3451 if (t->control & T_REMDEVALL) {
3452 pktgen_rem_all_ifs(t);
3453 t->control &= ~(T_REMDEVALL);
3456 if (t->control & T_REMDEV) {
3457 pktgen_rem_one_if(t);
3458 t->control &= ~(T_REMDEV);
3463 set_current_state(TASK_INTERRUPTIBLE);
3466 pr_debug("pktgen: %s stopping all device\n", t->tsk->comm);
3469 pr_debug("pktgen: %s removing all device\n", t->tsk->comm);
3470 pktgen_rem_all_ifs(t);
3472 pr_debug("pktgen: %s removing thread.\n", t->tsk->comm);
3473 pktgen_rem_thread(t);
3478 static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
3481 struct pktgen_dev *p, *pkt_dev = NULL;
3484 list_for_each_entry(p, &t->if_list, list)
3485 if (strncmp(p->odev->name, ifname, IFNAMSIZ) == 0) {
3491 pr_debug("pktgen: find_dev(%s) returning %p\n", ifname, pkt_dev);
3496 * Adds a dev at front of if_list.
3499 static int add_dev_to_thread(struct pktgen_thread *t,
3500 struct pktgen_dev *pkt_dev)
3506 if (pkt_dev->pg_thread) {
3507 printk(KERN_ERR "pktgen: ERROR: already assigned "
3513 list_add(&pkt_dev->list, &t->if_list);
3514 pkt_dev->pg_thread = t;
3515 pkt_dev->running = 0;
3522 /* Called under thread lock */
3524 static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
3526 struct pktgen_dev *pkt_dev;
3529 /* We don't allow a device to be on several threads */
3531 pkt_dev = __pktgen_NN_threads(ifname, FIND);
3533 printk(KERN_ERR "pktgen: ERROR: interface already used.\n");
3537 pkt_dev = kzalloc(sizeof(struct pktgen_dev), GFP_KERNEL);
3541 pkt_dev->flows = vmalloc(MAX_CFLOWS * sizeof(struct flow_state));
3542 if (pkt_dev->flows == NULL) {
3546 memset(pkt_dev->flows, 0, MAX_CFLOWS * sizeof(struct flow_state));
3548 pkt_dev->removal_mark = 0;
3549 pkt_dev->min_pkt_size = ETH_ZLEN;
3550 pkt_dev->max_pkt_size = ETH_ZLEN;
3551 pkt_dev->nfrags = 0;
3552 pkt_dev->clone_skb = pg_clone_skb_d;
3553 pkt_dev->delay_us = pg_delay_d / 1000;
3554 pkt_dev->delay_ns = pg_delay_d % 1000;
3555 pkt_dev->count = pg_count_d;
3557 pkt_dev->udp_src_min = 9; /* sink port */
3558 pkt_dev->udp_src_max = 9;
3559 pkt_dev->udp_dst_min = 9;
3560 pkt_dev->udp_dst_max = 9;
3562 pkt_dev->vlan_p = 0;
3563 pkt_dev->vlan_cfi = 0;
3564 pkt_dev->vlan_id = 0xffff;
3565 pkt_dev->svlan_p = 0;
3566 pkt_dev->svlan_cfi = 0;
3567 pkt_dev->svlan_id = 0xffff;
3569 err = pktgen_setup_dev(pkt_dev, ifname);
3573 pkt_dev->entry = create_proc_entry(ifname, 0600, pg_proc_dir);
3574 if (!pkt_dev->entry) {
3575 printk(KERN_ERR "pktgen: cannot create %s/%s procfs entry.\n",
3576 PG_PROC_DIR, ifname);
3580 pkt_dev->entry->proc_fops = &pktgen_if_fops;
3581 pkt_dev->entry->data = pkt_dev;
3583 pkt_dev->ipsmode = XFRM_MODE_TRANSPORT;
3584 pkt_dev->ipsproto = IPPROTO_ESP;
3587 return add_dev_to_thread(t, pkt_dev);
3589 dev_put(pkt_dev->odev);
3595 vfree(pkt_dev->flows);
3600 static int __init pktgen_create_thread(int cpu)
3602 struct pktgen_thread *t;
3603 struct proc_dir_entry *pe;
3604 struct task_struct *p;
3606 t = kzalloc(sizeof(struct pktgen_thread), GFP_KERNEL);
3608 printk(KERN_ERR "pktgen: ERROR: out of memory, can't "
3609 "create new thread.\n");
3613 spin_lock_init(&t->if_lock);
3616 INIT_LIST_HEAD(&t->if_list);
3618 list_add_tail(&t->th_list, &pktgen_threads);
3620 p = kthread_create(pktgen_thread_worker, t, "kpktgend_%d", cpu);
3622 printk(KERN_ERR "pktgen: kernel_thread() failed "
3623 "for cpu %d\n", t->cpu);
3624 list_del(&t->th_list);
3628 kthread_bind(p, cpu);
3631 pe = create_proc_entry(t->tsk->comm, 0600, pg_proc_dir);
3633 printk(KERN_ERR "pktgen: cannot create %s/%s procfs entry.\n",
3634 PG_PROC_DIR, t->tsk->comm);
3636 list_del(&t->th_list);
3641 pe->proc_fops = &pktgen_thread_fops;
3650 * Removes a device from the thread if_list.
3652 static void _rem_dev_from_if_list(struct pktgen_thread *t,
3653 struct pktgen_dev *pkt_dev)
3655 struct list_head *q, *n;
3656 struct pktgen_dev *p;
3658 list_for_each_safe(q, n, &t->if_list) {
3659 p = list_entry(q, struct pktgen_dev, list);
3665 static int pktgen_remove_device(struct pktgen_thread *t,
3666 struct pktgen_dev *pkt_dev)
3669 pr_debug("pktgen: remove_device pkt_dev=%p\n", pkt_dev);
3671 if (pkt_dev->running) {
3672 printk(KERN_WARNING "pktgen: WARNING: trying to remove a "
3673 "running interface, stopping it now.\n");
3674 pktgen_stop_device(pkt_dev);
3677 /* Dis-associate from the interface */
3679 if (pkt_dev->odev) {
3680 dev_put(pkt_dev->odev);
3681 pkt_dev->odev = NULL;
3684 /* And update the thread if_list */
3686 _rem_dev_from_if_list(t, pkt_dev);
3689 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
3695 vfree(pkt_dev->flows);
3700 static int __init pg_init(void)
3703 struct proc_dir_entry *pe;
3705 printk(KERN_INFO "%s", version);
3707 pg_proc_dir = proc_mkdir(PG_PROC_DIR, init_net.proc_net);
3710 pg_proc_dir->owner = THIS_MODULE;
3712 pe = create_proc_entry(PGCTRL, 0600, pg_proc_dir);
3714 printk(KERN_ERR "pktgen: ERROR: cannot create %s "
3715 "procfs entry.\n", PGCTRL);
3716 proc_net_remove(&init_net, PG_PROC_DIR);
3720 pe->proc_fops = &pktgen_fops;
3723 /* Register us to receive netdevice events */
3724 register_netdevice_notifier(&pktgen_notifier_block);
3726 for_each_online_cpu(cpu) {
3729 err = pktgen_create_thread(cpu);
3731 printk(KERN_WARNING "pktgen: WARNING: Cannot create "
3732 "thread for cpu %d (%d)\n", cpu, err);
3735 if (list_empty(&pktgen_threads)) {
3736 printk(KERN_ERR "pktgen: ERROR: Initialization failed for "
3738 unregister_netdevice_notifier(&pktgen_notifier_block);
3739 remove_proc_entry(PGCTRL, pg_proc_dir);
3740 proc_net_remove(&init_net, PG_PROC_DIR);
3747 static void __exit pg_cleanup(void)
3749 struct pktgen_thread *t;
3750 struct list_head *q, *n;
3751 wait_queue_head_t queue;
3752 init_waitqueue_head(&queue);
3754 /* Stop all interfaces & threads */
3756 list_for_each_safe(q, n, &pktgen_threads) {
3757 t = list_entry(q, struct pktgen_thread, th_list);
3758 kthread_stop(t->tsk);
3762 /* Un-register us from receiving netdevice events */
3763 unregister_netdevice_notifier(&pktgen_notifier_block);
3765 /* Clean up proc file system */
3766 remove_proc_entry(PGCTRL, pg_proc_dir);
3767 proc_net_remove(&init_net, PG_PROC_DIR);
3770 module_init(pg_init);
3771 module_exit(pg_cleanup);
3773 MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se");
3774 MODULE_DESCRIPTION("Packet Generator tool");
3775 MODULE_LICENSE("GPL");
3776 module_param(pg_count_d, int, 0);
3777 module_param(pg_delay_d, int, 0);
3778 module_param(pg_clone_skb_d, int, 0);
3779 module_param(debug, int, 0);