3 * Copyright 2001, 2002 by Robert Olsson <robert.olsson@its.uu.se>
4 * Uppsala University and
5 * Swedish University of Agricultural Sciences
7 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
8 * Ben Greear <greearb@candelatech.com>
9 * Jens Låås <jens.laas@data.slu.se>
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version
14 * 2 of the License, or (at your option) any later version.
17 * A tool for loading the network with preconfigurated packets.
18 * The tool is implemented as a linux module. Parameters are output
19 * device, delay (to hard_xmit), number of packets, and whether
20 * to use multiple SKBs or just the same one.
21 * pktgen uses the installed interface's output routine.
23 * Additional hacking by:
25 * Jens.Laas@data.slu.se
26 * Improved by ANK. 010120.
27 * Improved by ANK even more. 010212.
28 * MAC address typo fixed. 010417 --ro
29 * Integrated. 020301 --DaveM
30 * Added multiskb option 020301 --DaveM
31 * Scaling of results. 020417--sigurdur@linpro.no
32 * Significant re-work of the module:
33 * * Convert to threaded model to more efficiently be able to transmit
34 * and receive on multiple interfaces at once.
35 * * Converted many counters to __u64 to allow longer runs.
36 * * Allow configuration of ranges, like min/max IP address, MACs,
37 * and UDP-ports, for both source and destination, and can
38 * set to use a random distribution or sequentially walk the range.
39 * * Can now change most values after starting.
40 * * Place 12-byte packet in UDP payload with magic number,
41 * sequence number, and timestamp.
42 * * Add receiver code that detects dropped pkts, re-ordered pkts, and
43 * latencies (with micro-second) precision.
44 * * Add IOCTL interface to easily get counters & configuration.
45 * --Ben Greear <greearb@candelatech.com>
47 * Renamed multiskb to clone_skb and cleaned up sending core for two distinct
48 * skb modes. A clone_skb=0 mode for Ben "ranges" work and a clone_skb != 0
49 * as a "fastpath" with a configurable number of clones after alloc's.
50 * clone_skb=0 means all packets are allocated this also means ranges time
51 * stamps etc can be used. clone_skb=100 means 1 malloc is followed by 100
54 * Also moved to /proc/net/pktgen/
57 * Sept 10: Fixed threading/locking. Lots of bone-headed and more clever
58 * mistakes. Also merged in DaveM's patch in the -pre6 patch.
59 * --Ben Greear <greearb@candelatech.com>
61 * Integrated to 2.5.x 021029 --Lucio Maciel (luciomaciel@zipmail.com.br)
64 * 021124 Finished major redesign and rewrite for new functionality.
65 * See Documentation/networking/pktgen.txt for how to use this.
68 * For each CPU one thread/process is created at start. This process checks
69 * for running devices in the if_list and sends packets until count is 0 it
70 * also the thread checks the thread->control which is used for inter-process
71 * communication. controlling process "posts" operations to the threads this
72 * way. The if_lock should be possible to remove when add/rem_device is merged
75 * By design there should only be *one* "controlling" process. In practice
76 * multiple write accesses gives unpredictable result. Understood by "write"
77 * to /proc gives result code thats should be read be the "writer".
78 * For practical use this should be no problem.
80 * Note when adding devices to a specific CPU there good idea to also assign
81 * /proc/irq/XX/smp_affinity so TX-interrupts gets bound to the same CPU.
84 * Fix refcount off by one if first packet fails, potential null deref,
87 * First "ranges" functionality for ipv6 030726 --ro
89 * Included flow support. 030802 ANK.
91 * Fixed unaligned access on IA-64 Grant Grundler <grundler@parisc-linux.org>
93 * Remove if fix from added Harald Welte <laforge@netfilter.org> 040419
94 * ia64 compilation fix from Aron Griffis <aron@hp.com> 040604
96 * New xmit() return, do_div and misc clean up by Stephen Hemminger
97 * <shemminger@osdl.org> 040923
99 * Randy Dunlap fixed u64 printk compiler waring
101 * Remove FCS from BW calculation. Lennert Buytenhek <buytenh@wantstofly.org>
102 * New time handling. Lennert Buytenhek <buytenh@wantstofly.org> 041213
104 * Corrections from Nikolai Malykh (nmalykh@bilim.com)
105 * Removed unused flags F_SET_SRCMAC & F_SET_SRCIP 041230
107 * interruptible_sleep_on_timeout() replaced Nishanth Aravamudan <nacc@us.ibm.com>
110 * MPLS support by Steven Whitehouse <steve@chygwyn.com>
112 * 802.1Q/Q-in-Q support by Francesco Fondelli (FF) <francesco.fondelli@gmail.com>
114 * Fixed src_mac command to set source mac of packet to value specified in
115 * command by Adit Ranadive <adit.262@gmail.com>
118 #include <linux/sys.h>
119 #include <linux/types.h>
120 #include <linux/module.h>
121 #include <linux/moduleparam.h>
122 #include <linux/kernel.h>
123 #include <linux/mutex.h>
124 #include <linux/sched.h>
125 #include <linux/slab.h>
126 #include <linux/vmalloc.h>
127 #include <linux/unistd.h>
128 #include <linux/string.h>
129 #include <linux/ptrace.h>
130 #include <linux/errno.h>
131 #include <linux/ioport.h>
132 #include <linux/interrupt.h>
133 #include <linux/capability.h>
134 #include <linux/hrtimer.h>
135 #include <linux/freezer.h>
136 #include <linux/delay.h>
137 #include <linux/timer.h>
138 #include <linux/list.h>
139 #include <linux/init.h>
140 #include <linux/skbuff.h>
141 #include <linux/netdevice.h>
142 #include <linux/inet.h>
143 #include <linux/inetdevice.h>
144 #include <linux/rtnetlink.h>
145 #include <linux/if_arp.h>
146 #include <linux/if_vlan.h>
147 #include <linux/in.h>
148 #include <linux/ip.h>
149 #include <linux/ipv6.h>
150 #include <linux/udp.h>
151 #include <linux/proc_fs.h>
152 #include <linux/seq_file.h>
153 #include <linux/wait.h>
154 #include <linux/etherdevice.h>
155 #include <linux/kthread.h>
156 #include <net/net_namespace.h>
157 #include <net/checksum.h>
158 #include <net/ipv6.h>
159 #include <net/addrconf.h>
161 #include <net/xfrm.h>
163 #include <asm/byteorder.h>
164 #include <linux/rcupdate.h>
165 #include <linux/bitops.h>
166 #include <linux/io.h>
167 #include <linux/timex.h>
168 #include <linux/uaccess.h>
170 #include <asm/div64.h> /* do_div */
172 #define VERSION "2.72"
173 #define IP_NAME_SZ 32
174 #define MAX_MPLS_LABELS 16 /* This is the max label stack depth */
175 #define MPLS_STACK_BOTTOM htonl(0x00000100)
177 /* Device flag bits */
178 #define F_IPSRC_RND (1<<0) /* IP-Src Random */
179 #define F_IPDST_RND (1<<1) /* IP-Dst Random */
180 #define F_UDPSRC_RND (1<<2) /* UDP-Src Random */
181 #define F_UDPDST_RND (1<<3) /* UDP-Dst Random */
182 #define F_MACSRC_RND (1<<4) /* MAC-Src Random */
183 #define F_MACDST_RND (1<<5) /* MAC-Dst Random */
184 #define F_TXSIZE_RND (1<<6) /* Transmit size is random */
185 #define F_IPV6 (1<<7) /* Interface in IPV6 Mode */
186 #define F_MPLS_RND (1<<8) /* Random MPLS labels */
187 #define F_VID_RND (1<<9) /* Random VLAN ID */
188 #define F_SVID_RND (1<<10) /* Random SVLAN ID */
189 #define F_FLOW_SEQ (1<<11) /* Sequential flows */
190 #define F_IPSEC_ON (1<<12) /* ipsec on for flows */
191 #define F_QUEUE_MAP_RND (1<<13) /* queue map Random */
192 #define F_QUEUE_MAP_CPU (1<<14) /* queue map mirrors smp_processor_id() */
194 /* Thread control flag bits */
195 #define T_STOP (1<<0) /* Stop run */
196 #define T_RUN (1<<1) /* Start run */
197 #define T_REMDEVALL (1<<2) /* Remove all devs */
198 #define T_REMDEV (1<<3) /* 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;
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; /* chaining in the thread's run-queue */
235 int running; /* if 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 u64 delay; /* nano-seconds */
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,
254 pkts will be re-sent */
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;
345 0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
347 We fill in SRC address later
348 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
352 __u16 pad; /* pad out the hh struct to an even 16 bytes */
354 struct sk_buff *skb; /* skb we are to transmit next, used for when we
355 * are transmitting the same one multiple times
357 struct net_device *odev; /* The out-going device.
358 * Note that the device should have it's
359 * pg_info pointer pointing back to this
361 * Set when the user specifies the out-going
362 * device name (not when the inject is
363 * started as it used to do.)
365 struct flow_state *flows;
366 unsigned cflows; /* Concurrent flows (config) */
367 unsigned lflow; /* Flow length (config) */
368 unsigned nflows; /* accumulated flows (stats) */
369 unsigned curfl; /* current sequenced flow (state)*/
375 __u8 ipsmode; /* IPSEC mode (config) */
376 __u8 ipsproto; /* IPSEC type (config) */
388 struct pktgen_thread {
389 spinlock_t if_lock; /* for list of devices */
390 struct list_head if_list; /* All device here */
391 struct list_head th_list;
392 struct task_struct *tsk;
395 /* Field for thread to receive "posted" events terminate,
401 wait_queue_head_t queue;
402 struct completion start_done;
408 static inline ktime_t ktime_now(void)
413 return timespec_to_ktime(ts);
416 /* This works even if 32 bit because of careful byte order choice */
417 static inline int ktime_lt(const ktime_t cmp1, const ktime_t cmp2)
419 return cmp1.tv64 < cmp2.tv64;
422 static const char version[] =
423 "pktgen " VERSION ": Packet Generator for packet performance testing.\n";
425 static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *i);
426 static int pktgen_add_device(struct pktgen_thread *t, const char *ifname);
427 static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
429 static int pktgen_device_event(struct notifier_block *, unsigned long, void *);
430 static void pktgen_run_all_threads(void);
431 static void pktgen_reset_all_threads(void);
432 static void pktgen_stop_all_threads_ifs(void);
434 static void pktgen_stop(struct pktgen_thread *t);
435 static void pktgen_clear_counters(struct pktgen_dev *pkt_dev);
437 static unsigned int scan_ip6(const char *s, char ip[16]);
438 static unsigned int fmt_ip6(char *s, const char ip[16]);
440 /* Module parameters, defaults. */
441 static int pg_count_d __read_mostly = 1000;
442 static int pg_delay_d __read_mostly;
443 static int pg_clone_skb_d __read_mostly;
444 static int debug __read_mostly;
446 static DEFINE_MUTEX(pktgen_thread_lock);
447 static LIST_HEAD(pktgen_threads);
449 static struct notifier_block pktgen_notifier_block = {
450 .notifier_call = pktgen_device_event,
454 * /proc handling functions
458 static int pgctrl_show(struct seq_file *seq, void *v)
460 seq_puts(seq, version);
464 static ssize_t pgctrl_write(struct file *file, const char __user *buf,
465 size_t count, loff_t *ppos)
470 if (!capable(CAP_NET_ADMIN)) {
475 if (count > sizeof(data))
476 count = sizeof(data);
478 if (copy_from_user(data, buf, count)) {
482 data[count - 1] = 0; /* Make string */
484 if (!strcmp(data, "stop"))
485 pktgen_stop_all_threads_ifs();
487 else if (!strcmp(data, "start"))
488 pktgen_run_all_threads();
490 else if (!strcmp(data, "reset"))
491 pktgen_reset_all_threads();
494 printk(KERN_WARNING "pktgen: Unknown command: %s\n", data);
502 static int pgctrl_open(struct inode *inode, struct file *file)
504 return single_open(file, pgctrl_show, PDE(inode)->data);
507 static const struct file_operations pktgen_fops = {
508 .owner = THIS_MODULE,
512 .write = pgctrl_write,
513 .release = single_release,
516 static int pktgen_if_show(struct seq_file *seq, void *v)
518 const struct pktgen_dev *pkt_dev = seq->private;
523 "Params: count %llu min_pkt_size: %u max_pkt_size: %u\n",
524 (unsigned long long)pkt_dev->count, pkt_dev->min_pkt_size,
525 pkt_dev->max_pkt_size);
528 " frags: %d delay: %llu clone_skb: %d ifname: %s\n",
529 pkt_dev->nfrags, (unsigned long long) pkt_dev->delay,
530 pkt_dev->clone_skb, pkt_dev->odev->name);
532 seq_printf(seq, " flows: %u flowlen: %u\n", pkt_dev->cflows,
536 " queue_map_min: %u queue_map_max: %u\n",
537 pkt_dev->queue_map_min,
538 pkt_dev->queue_map_max);
540 if (pkt_dev->flags & F_IPV6) {
541 char b1[128], b2[128], b3[128];
542 fmt_ip6(b1, pkt_dev->in6_saddr.s6_addr);
543 fmt_ip6(b2, pkt_dev->min_in6_saddr.s6_addr);
544 fmt_ip6(b3, pkt_dev->max_in6_saddr.s6_addr);
546 " saddr: %s min_saddr: %s max_saddr: %s\n", b1,
549 fmt_ip6(b1, pkt_dev->in6_daddr.s6_addr);
550 fmt_ip6(b2, pkt_dev->min_in6_daddr.s6_addr);
551 fmt_ip6(b3, pkt_dev->max_in6_daddr.s6_addr);
553 " daddr: %s min_daddr: %s max_daddr: %s\n", b1,
558 " dst_min: %s dst_max: %s\n",
559 pkt_dev->dst_min, pkt_dev->dst_max);
561 " src_min: %s src_max: %s\n",
562 pkt_dev->src_min, pkt_dev->src_max);
565 seq_puts(seq, " src_mac: ");
567 seq_printf(seq, "%pM ",
568 is_zero_ether_addr(pkt_dev->src_mac) ?
569 pkt_dev->odev->dev_addr : pkt_dev->src_mac);
571 seq_printf(seq, "dst_mac: ");
572 seq_printf(seq, "%pM\n", pkt_dev->dst_mac);
575 " udp_src_min: %d udp_src_max: %d"
576 " udp_dst_min: %d udp_dst_max: %d\n",
577 pkt_dev->udp_src_min, pkt_dev->udp_src_max,
578 pkt_dev->udp_dst_min, pkt_dev->udp_dst_max);
581 " src_mac_count: %d dst_mac_count: %d\n",
582 pkt_dev->src_mac_count, pkt_dev->dst_mac_count);
584 if (pkt_dev->nr_labels) {
586 seq_printf(seq, " mpls: ");
587 for (i = 0; i < pkt_dev->nr_labels; i++)
588 seq_printf(seq, "%08x%s", ntohl(pkt_dev->labels[i]),
589 i == pkt_dev->nr_labels-1 ? "\n" : ", ");
592 if (pkt_dev->vlan_id != 0xffff)
593 seq_printf(seq, " vlan_id: %u vlan_p: %u vlan_cfi: %u\n",
594 pkt_dev->vlan_id, pkt_dev->vlan_p,
597 if (pkt_dev->svlan_id != 0xffff)
598 seq_printf(seq, " svlan_id: %u vlan_p: %u vlan_cfi: %u\n",
599 pkt_dev->svlan_id, pkt_dev->svlan_p,
603 seq_printf(seq, " tos: 0x%02x\n", pkt_dev->tos);
605 if (pkt_dev->traffic_class)
606 seq_printf(seq, " traffic_class: 0x%02x\n", pkt_dev->traffic_class);
608 seq_printf(seq, " Flags: ");
610 if (pkt_dev->flags & F_IPV6)
611 seq_printf(seq, "IPV6 ");
613 if (pkt_dev->flags & F_IPSRC_RND)
614 seq_printf(seq, "IPSRC_RND ");
616 if (pkt_dev->flags & F_IPDST_RND)
617 seq_printf(seq, "IPDST_RND ");
619 if (pkt_dev->flags & F_TXSIZE_RND)
620 seq_printf(seq, "TXSIZE_RND ");
622 if (pkt_dev->flags & F_UDPSRC_RND)
623 seq_printf(seq, "UDPSRC_RND ");
625 if (pkt_dev->flags & F_UDPDST_RND)
626 seq_printf(seq, "UDPDST_RND ");
628 if (pkt_dev->flags & F_MPLS_RND)
629 seq_printf(seq, "MPLS_RND ");
631 if (pkt_dev->flags & F_QUEUE_MAP_RND)
632 seq_printf(seq, "QUEUE_MAP_RND ");
634 if (pkt_dev->flags & F_QUEUE_MAP_CPU)
635 seq_printf(seq, "QUEUE_MAP_CPU ");
637 if (pkt_dev->cflows) {
638 if (pkt_dev->flags & F_FLOW_SEQ)
639 seq_printf(seq, "FLOW_SEQ "); /*in sequence flows*/
641 seq_printf(seq, "FLOW_RND ");
645 if (pkt_dev->flags & F_IPSEC_ON)
646 seq_printf(seq, "IPSEC ");
649 if (pkt_dev->flags & F_MACSRC_RND)
650 seq_printf(seq, "MACSRC_RND ");
652 if (pkt_dev->flags & F_MACDST_RND)
653 seq_printf(seq, "MACDST_RND ");
655 if (pkt_dev->flags & F_VID_RND)
656 seq_printf(seq, "VID_RND ");
658 if (pkt_dev->flags & F_SVID_RND)
659 seq_printf(seq, "SVID_RND ");
663 /* not really stopped, more like last-running-at */
664 stopped = pkt_dev->running ? ktime_now() : pkt_dev->stopped_at;
665 idle = pkt_dev->idle_acc;
666 do_div(idle, NSEC_PER_USEC);
669 "Current:\n pkts-sofar: %llu errors: %llu\n",
670 (unsigned long long)pkt_dev->sofar,
671 (unsigned long long)pkt_dev->errors);
674 " started: %lluus stopped: %lluus idle: %lluus\n",
675 (unsigned long long) ktime_to_us(pkt_dev->started_at),
676 (unsigned long long) ktime_to_us(stopped),
677 (unsigned long long) idle);
680 " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n",
681 pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset,
682 pkt_dev->cur_src_mac_offset);
684 if (pkt_dev->flags & F_IPV6) {
685 char b1[128], b2[128];
686 fmt_ip6(b1, pkt_dev->cur_in6_daddr.s6_addr);
687 fmt_ip6(b2, pkt_dev->cur_in6_saddr.s6_addr);
688 seq_printf(seq, " cur_saddr: %s cur_daddr: %s\n", b2, b1);
690 seq_printf(seq, " cur_saddr: 0x%x cur_daddr: 0x%x\n",
691 pkt_dev->cur_saddr, pkt_dev->cur_daddr);
693 seq_printf(seq, " cur_udp_dst: %d cur_udp_src: %d\n",
694 pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src);
696 seq_printf(seq, " cur_queue_map: %u\n", pkt_dev->cur_queue_map);
698 seq_printf(seq, " flows: %u\n", pkt_dev->nflows);
700 if (pkt_dev->result[0])
701 seq_printf(seq, "Result: %s\n", pkt_dev->result);
703 seq_printf(seq, "Result: Idle\n");
709 static int hex32_arg(const char __user *user_buffer, unsigned long maxlen,
715 for (; i < maxlen; i++) {
718 if (get_user(c, &user_buffer[i]))
720 if ((c >= '0') && (c <= '9'))
722 else if ((c >= 'a') && (c <= 'f'))
723 *num |= c - 'a' + 10;
724 else if ((c >= 'A') && (c <= 'F'))
725 *num |= c - 'A' + 10;
732 static int count_trail_chars(const char __user * user_buffer,
737 for (i = 0; i < maxlen; i++) {
739 if (get_user(c, &user_buffer[i]))
757 static unsigned long num_arg(const char __user * user_buffer,
758 unsigned long maxlen, unsigned long *num)
763 for (; i < maxlen; i++) {
765 if (get_user(c, &user_buffer[i]))
767 if ((c >= '0') && (c <= '9')) {
776 static int strn_len(const char __user * user_buffer, unsigned int maxlen)
780 for (; i < maxlen; i++) {
782 if (get_user(c, &user_buffer[i]))
800 static ssize_t get_labels(const char __user *buffer, struct pktgen_dev *pkt_dev)
807 pkt_dev->nr_labels = 0;
810 len = hex32_arg(&buffer[i], 8, &tmp);
813 pkt_dev->labels[n] = htonl(tmp);
814 if (pkt_dev->labels[n] & MPLS_STACK_BOTTOM)
815 pkt_dev->flags |= F_MPLS_RND;
817 if (get_user(c, &buffer[i]))
821 if (n >= MAX_MPLS_LABELS)
825 pkt_dev->nr_labels = n;
829 static ssize_t pktgen_if_write(struct file *file,
830 const char __user * user_buffer, size_t count,
833 struct seq_file *seq = (struct seq_file *)file->private_data;
834 struct pktgen_dev *pkt_dev = seq->private;
836 char name[16], valstr[32];
837 unsigned long value = 0;
838 char *pg_result = NULL;
842 pg_result = &(pkt_dev->result[0]);
845 printk(KERN_WARNING "pktgen: wrong command format\n");
850 tmp = count_trail_chars(&user_buffer[i], max);
852 printk(KERN_WARNING "pktgen: illegal format\n");
857 /* Read variable name */
859 len = strn_len(&user_buffer[i], sizeof(name) - 1);
863 memset(name, 0, sizeof(name));
864 if (copy_from_user(name, &user_buffer[i], len))
869 len = count_trail_chars(&user_buffer[i], max);
877 if (copy_from_user(tb, user_buffer, count))
880 printk(KERN_DEBUG "pktgen: %s,%lu buffer -:%s:-\n", name,
881 (unsigned long)count, tb);
884 if (!strcmp(name, "min_pkt_size")) {
885 len = num_arg(&user_buffer[i], 10, &value);
890 if (value < 14 + 20 + 8)
892 if (value != pkt_dev->min_pkt_size) {
893 pkt_dev->min_pkt_size = value;
894 pkt_dev->cur_pkt_size = value;
896 sprintf(pg_result, "OK: min_pkt_size=%u",
897 pkt_dev->min_pkt_size);
901 if (!strcmp(name, "max_pkt_size")) {
902 len = num_arg(&user_buffer[i], 10, &value);
907 if (value < 14 + 20 + 8)
909 if (value != pkt_dev->max_pkt_size) {
910 pkt_dev->max_pkt_size = value;
911 pkt_dev->cur_pkt_size = value;
913 sprintf(pg_result, "OK: max_pkt_size=%u",
914 pkt_dev->max_pkt_size);
918 /* Shortcut for min = max */
920 if (!strcmp(name, "pkt_size")) {
921 len = num_arg(&user_buffer[i], 10, &value);
926 if (value < 14 + 20 + 8)
928 if (value != pkt_dev->min_pkt_size) {
929 pkt_dev->min_pkt_size = value;
930 pkt_dev->max_pkt_size = value;
931 pkt_dev->cur_pkt_size = value;
933 sprintf(pg_result, "OK: pkt_size=%u", pkt_dev->min_pkt_size);
937 if (!strcmp(name, "debug")) {
938 len = num_arg(&user_buffer[i], 10, &value);
944 sprintf(pg_result, "OK: debug=%u", debug);
948 if (!strcmp(name, "frags")) {
949 len = num_arg(&user_buffer[i], 10, &value);
954 pkt_dev->nfrags = value;
955 sprintf(pg_result, "OK: frags=%u", pkt_dev->nfrags);
958 if (!strcmp(name, "delay")) {
959 len = num_arg(&user_buffer[i], 10, &value);
964 if (value == 0x7FFFFFFF)
965 pkt_dev->delay = ULLONG_MAX;
967 pkt_dev->delay = (u64)value;
969 sprintf(pg_result, "OK: delay=%llu",
970 (unsigned long long) pkt_dev->delay);
973 if (!strcmp(name, "udp_src_min")) {
974 len = num_arg(&user_buffer[i], 10, &value);
979 if (value != pkt_dev->udp_src_min) {
980 pkt_dev->udp_src_min = value;
981 pkt_dev->cur_udp_src = value;
983 sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min);
986 if (!strcmp(name, "udp_dst_min")) {
987 len = num_arg(&user_buffer[i], 10, &value);
992 if (value != pkt_dev->udp_dst_min) {
993 pkt_dev->udp_dst_min = value;
994 pkt_dev->cur_udp_dst = value;
996 sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min);
999 if (!strcmp(name, "udp_src_max")) {
1000 len = num_arg(&user_buffer[i], 10, &value);
1005 if (value != pkt_dev->udp_src_max) {
1006 pkt_dev->udp_src_max = value;
1007 pkt_dev->cur_udp_src = value;
1009 sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max);
1012 if (!strcmp(name, "udp_dst_max")) {
1013 len = num_arg(&user_buffer[i], 10, &value);
1018 if (value != pkt_dev->udp_dst_max) {
1019 pkt_dev->udp_dst_max = value;
1020 pkt_dev->cur_udp_dst = value;
1022 sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max);
1025 if (!strcmp(name, "clone_skb")) {
1026 len = num_arg(&user_buffer[i], 10, &value);
1031 pkt_dev->clone_skb = value;
1033 sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb);
1036 if (!strcmp(name, "count")) {
1037 len = num_arg(&user_buffer[i], 10, &value);
1042 pkt_dev->count = value;
1043 sprintf(pg_result, "OK: count=%llu",
1044 (unsigned long long)pkt_dev->count);
1047 if (!strcmp(name, "src_mac_count")) {
1048 len = num_arg(&user_buffer[i], 10, &value);
1053 if (pkt_dev->src_mac_count != value) {
1054 pkt_dev->src_mac_count = value;
1055 pkt_dev->cur_src_mac_offset = 0;
1057 sprintf(pg_result, "OK: src_mac_count=%d",
1058 pkt_dev->src_mac_count);
1061 if (!strcmp(name, "dst_mac_count")) {
1062 len = num_arg(&user_buffer[i], 10, &value);
1067 if (pkt_dev->dst_mac_count != value) {
1068 pkt_dev->dst_mac_count = value;
1069 pkt_dev->cur_dst_mac_offset = 0;
1071 sprintf(pg_result, "OK: dst_mac_count=%d",
1072 pkt_dev->dst_mac_count);
1075 if (!strcmp(name, "flag")) {
1078 len = strn_len(&user_buffer[i], sizeof(f) - 1);
1082 if (copy_from_user(f, &user_buffer[i], len))
1085 if (strcmp(f, "IPSRC_RND") == 0)
1086 pkt_dev->flags |= F_IPSRC_RND;
1088 else if (strcmp(f, "!IPSRC_RND") == 0)
1089 pkt_dev->flags &= ~F_IPSRC_RND;
1091 else if (strcmp(f, "TXSIZE_RND") == 0)
1092 pkt_dev->flags |= F_TXSIZE_RND;
1094 else if (strcmp(f, "!TXSIZE_RND") == 0)
1095 pkt_dev->flags &= ~F_TXSIZE_RND;
1097 else if (strcmp(f, "IPDST_RND") == 0)
1098 pkt_dev->flags |= F_IPDST_RND;
1100 else if (strcmp(f, "!IPDST_RND") == 0)
1101 pkt_dev->flags &= ~F_IPDST_RND;
1103 else if (strcmp(f, "UDPSRC_RND") == 0)
1104 pkt_dev->flags |= F_UDPSRC_RND;
1106 else if (strcmp(f, "!UDPSRC_RND") == 0)
1107 pkt_dev->flags &= ~F_UDPSRC_RND;
1109 else if (strcmp(f, "UDPDST_RND") == 0)
1110 pkt_dev->flags |= F_UDPDST_RND;
1112 else if (strcmp(f, "!UDPDST_RND") == 0)
1113 pkt_dev->flags &= ~F_UDPDST_RND;
1115 else if (strcmp(f, "MACSRC_RND") == 0)
1116 pkt_dev->flags |= F_MACSRC_RND;
1118 else if (strcmp(f, "!MACSRC_RND") == 0)
1119 pkt_dev->flags &= ~F_MACSRC_RND;
1121 else if (strcmp(f, "MACDST_RND") == 0)
1122 pkt_dev->flags |= F_MACDST_RND;
1124 else if (strcmp(f, "!MACDST_RND") == 0)
1125 pkt_dev->flags &= ~F_MACDST_RND;
1127 else if (strcmp(f, "MPLS_RND") == 0)
1128 pkt_dev->flags |= F_MPLS_RND;
1130 else if (strcmp(f, "!MPLS_RND") == 0)
1131 pkt_dev->flags &= ~F_MPLS_RND;
1133 else if (strcmp(f, "VID_RND") == 0)
1134 pkt_dev->flags |= F_VID_RND;
1136 else if (strcmp(f, "!VID_RND") == 0)
1137 pkt_dev->flags &= ~F_VID_RND;
1139 else if (strcmp(f, "SVID_RND") == 0)
1140 pkt_dev->flags |= F_SVID_RND;
1142 else if (strcmp(f, "!SVID_RND") == 0)
1143 pkt_dev->flags &= ~F_SVID_RND;
1145 else if (strcmp(f, "FLOW_SEQ") == 0)
1146 pkt_dev->flags |= F_FLOW_SEQ;
1148 else if (strcmp(f, "QUEUE_MAP_RND") == 0)
1149 pkt_dev->flags |= F_QUEUE_MAP_RND;
1151 else if (strcmp(f, "!QUEUE_MAP_RND") == 0)
1152 pkt_dev->flags &= ~F_QUEUE_MAP_RND;
1154 else if (strcmp(f, "QUEUE_MAP_CPU") == 0)
1155 pkt_dev->flags |= F_QUEUE_MAP_CPU;
1157 else if (strcmp(f, "!QUEUE_MAP_CPU") == 0)
1158 pkt_dev->flags &= ~F_QUEUE_MAP_CPU;
1160 else if (strcmp(f, "IPSEC") == 0)
1161 pkt_dev->flags |= F_IPSEC_ON;
1164 else if (strcmp(f, "!IPV6") == 0)
1165 pkt_dev->flags &= ~F_IPV6;
1169 "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
1171 "IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
1172 "MACSRC_RND, MACDST_RND, TXSIZE_RND, IPV6, MPLS_RND, VID_RND, SVID_RND, FLOW_SEQ, IPSEC\n");
1175 sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
1178 if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
1179 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
1183 if (copy_from_user(buf, &user_buffer[i], len))
1186 if (strcmp(buf, pkt_dev->dst_min) != 0) {
1187 memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
1188 strncpy(pkt_dev->dst_min, buf, len);
1189 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
1190 pkt_dev->cur_daddr = pkt_dev->daddr_min;
1193 printk(KERN_DEBUG "pktgen: dst_min set to: %s\n",
1196 sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min);
1199 if (!strcmp(name, "dst_max")) {
1200 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
1205 if (copy_from_user(buf, &user_buffer[i], len))
1209 if (strcmp(buf, pkt_dev->dst_max) != 0) {
1210 memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
1211 strncpy(pkt_dev->dst_max, buf, len);
1212 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
1213 pkt_dev->cur_daddr = pkt_dev->daddr_max;
1216 printk(KERN_DEBUG "pktgen: dst_max set to: %s\n",
1219 sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max);
1222 if (!strcmp(name, "dst6")) {
1223 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1227 pkt_dev->flags |= F_IPV6;
1229 if (copy_from_user(buf, &user_buffer[i], len))
1233 scan_ip6(buf, pkt_dev->in6_daddr.s6_addr);
1234 fmt_ip6(buf, pkt_dev->in6_daddr.s6_addr);
1236 ipv6_addr_copy(&pkt_dev->cur_in6_daddr, &pkt_dev->in6_daddr);
1239 printk(KERN_DEBUG "pktgen: dst6 set to: %s\n", buf);
1242 sprintf(pg_result, "OK: dst6=%s", buf);
1245 if (!strcmp(name, "dst6_min")) {
1246 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1250 pkt_dev->flags |= F_IPV6;
1252 if (copy_from_user(buf, &user_buffer[i], len))
1256 scan_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
1257 fmt_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
1259 ipv6_addr_copy(&pkt_dev->cur_in6_daddr,
1260 &pkt_dev->min_in6_daddr);
1262 printk(KERN_DEBUG "pktgen: dst6_min set to: %s\n", buf);
1265 sprintf(pg_result, "OK: dst6_min=%s", buf);
1268 if (!strcmp(name, "dst6_max")) {
1269 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1273 pkt_dev->flags |= F_IPV6;
1275 if (copy_from_user(buf, &user_buffer[i], len))
1279 scan_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
1280 fmt_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
1283 printk(KERN_DEBUG "pktgen: dst6_max set to: %s\n", buf);
1286 sprintf(pg_result, "OK: dst6_max=%s", buf);
1289 if (!strcmp(name, "src6")) {
1290 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1294 pkt_dev->flags |= F_IPV6;
1296 if (copy_from_user(buf, &user_buffer[i], len))
1300 scan_ip6(buf, pkt_dev->in6_saddr.s6_addr);
1301 fmt_ip6(buf, pkt_dev->in6_saddr.s6_addr);
1303 ipv6_addr_copy(&pkt_dev->cur_in6_saddr, &pkt_dev->in6_saddr);
1306 printk(KERN_DEBUG "pktgen: src6 set to: %s\n", buf);
1309 sprintf(pg_result, "OK: src6=%s", buf);
1312 if (!strcmp(name, "src_min")) {
1313 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
1317 if (copy_from_user(buf, &user_buffer[i], len))
1320 if (strcmp(buf, pkt_dev->src_min) != 0) {
1321 memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
1322 strncpy(pkt_dev->src_min, buf, len);
1323 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
1324 pkt_dev->cur_saddr = pkt_dev->saddr_min;
1327 printk(KERN_DEBUG "pktgen: src_min set to: %s\n",
1330 sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min);
1333 if (!strcmp(name, "src_max")) {
1334 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
1338 if (copy_from_user(buf, &user_buffer[i], len))
1341 if (strcmp(buf, pkt_dev->src_max) != 0) {
1342 memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
1343 strncpy(pkt_dev->src_max, buf, len);
1344 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
1345 pkt_dev->cur_saddr = pkt_dev->saddr_max;
1348 printk(KERN_DEBUG "pktgen: src_max set to: %s\n",
1351 sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max);
1354 if (!strcmp(name, "dst_mac")) {
1356 unsigned char old_dmac[ETH_ALEN];
1357 unsigned char *m = pkt_dev->dst_mac;
1358 memcpy(old_dmac, pkt_dev->dst_mac, ETH_ALEN);
1360 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
1364 memset(valstr, 0, sizeof(valstr));
1365 if (copy_from_user(valstr, &user_buffer[i], len))
1369 for (*m = 0; *v && m < pkt_dev->dst_mac + 6; v++) {
1370 if (*v >= '0' && *v <= '9') {
1374 if (*v >= 'A' && *v <= 'F') {
1376 *m += *v - 'A' + 10;
1378 if (*v >= 'a' && *v <= 'f') {
1380 *m += *v - 'a' + 10;
1388 /* Set up Dest MAC */
1389 if (compare_ether_addr(old_dmac, pkt_dev->dst_mac))
1390 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
1392 sprintf(pg_result, "OK: dstmac");
1395 if (!strcmp(name, "src_mac")) {
1397 unsigned char old_smac[ETH_ALEN];
1398 unsigned char *m = pkt_dev->src_mac;
1400 memcpy(old_smac, pkt_dev->src_mac, ETH_ALEN);
1402 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
1406 memset(valstr, 0, sizeof(valstr));
1407 if (copy_from_user(valstr, &user_buffer[i], len))
1411 for (*m = 0; *v && m < pkt_dev->src_mac + 6; v++) {
1412 if (*v >= '0' && *v <= '9') {
1416 if (*v >= 'A' && *v <= 'F') {
1418 *m += *v - 'A' + 10;
1420 if (*v >= 'a' && *v <= 'f') {
1422 *m += *v - 'a' + 10;
1430 /* Set up Src MAC */
1431 if (compare_ether_addr(old_smac, pkt_dev->src_mac))
1432 memcpy(&(pkt_dev->hh[6]), pkt_dev->src_mac, ETH_ALEN);
1434 sprintf(pg_result, "OK: srcmac");
1438 if (!strcmp(name, "clear_counters")) {
1439 pktgen_clear_counters(pkt_dev);
1440 sprintf(pg_result, "OK: Clearing counters.\n");
1444 if (!strcmp(name, "flows")) {
1445 len = num_arg(&user_buffer[i], 10, &value);
1450 if (value > MAX_CFLOWS)
1453 pkt_dev->cflows = value;
1454 sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
1458 if (!strcmp(name, "flowlen")) {
1459 len = num_arg(&user_buffer[i], 10, &value);
1464 pkt_dev->lflow = value;
1465 sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
1469 if (!strcmp(name, "queue_map_min")) {
1470 len = num_arg(&user_buffer[i], 5, &value);
1475 pkt_dev->queue_map_min = value;
1476 sprintf(pg_result, "OK: queue_map_min=%u", pkt_dev->queue_map_min);
1480 if (!strcmp(name, "queue_map_max")) {
1481 len = num_arg(&user_buffer[i], 5, &value);
1486 pkt_dev->queue_map_max = value;
1487 sprintf(pg_result, "OK: queue_map_max=%u", pkt_dev->queue_map_max);
1491 if (!strcmp(name, "mpls")) {
1494 len = get_labels(&user_buffer[i], pkt_dev);
1498 cnt = sprintf(pg_result, "OK: mpls=");
1499 for (n = 0; n < pkt_dev->nr_labels; n++)
1500 cnt += sprintf(pg_result + cnt,
1501 "%08x%s", ntohl(pkt_dev->labels[n]),
1502 n == pkt_dev->nr_labels-1 ? "" : ",");
1504 if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) {
1505 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1506 pkt_dev->svlan_id = 0xffff;
1509 printk(KERN_DEBUG "pktgen: VLAN/SVLAN auto turned off\n");
1514 if (!strcmp(name, "vlan_id")) {
1515 len = num_arg(&user_buffer[i], 4, &value);
1520 if (value <= 4095) {
1521 pkt_dev->vlan_id = value; /* turn on VLAN */
1524 printk(KERN_DEBUG "pktgen: VLAN turned on\n");
1526 if (debug && pkt_dev->nr_labels)
1527 printk(KERN_DEBUG "pktgen: MPLS auto turned off\n");
1529 pkt_dev->nr_labels = 0; /* turn off MPLS */
1530 sprintf(pg_result, "OK: vlan_id=%u", pkt_dev->vlan_id);
1532 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1533 pkt_dev->svlan_id = 0xffff;
1536 printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n");
1541 if (!strcmp(name, "vlan_p")) {
1542 len = num_arg(&user_buffer[i], 1, &value);
1547 if ((value <= 7) && (pkt_dev->vlan_id != 0xffff)) {
1548 pkt_dev->vlan_p = value;
1549 sprintf(pg_result, "OK: vlan_p=%u", pkt_dev->vlan_p);
1551 sprintf(pg_result, "ERROR: vlan_p must be 0-7");
1556 if (!strcmp(name, "vlan_cfi")) {
1557 len = num_arg(&user_buffer[i], 1, &value);
1562 if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) {
1563 pkt_dev->vlan_cfi = value;
1564 sprintf(pg_result, "OK: vlan_cfi=%u", pkt_dev->vlan_cfi);
1566 sprintf(pg_result, "ERROR: vlan_cfi must be 0-1");
1571 if (!strcmp(name, "svlan_id")) {
1572 len = num_arg(&user_buffer[i], 4, &value);
1577 if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) {
1578 pkt_dev->svlan_id = value; /* turn on SVLAN */
1581 printk(KERN_DEBUG "pktgen: SVLAN turned on\n");
1583 if (debug && pkt_dev->nr_labels)
1584 printk(KERN_DEBUG "pktgen: MPLS auto turned off\n");
1586 pkt_dev->nr_labels = 0; /* turn off MPLS */
1587 sprintf(pg_result, "OK: svlan_id=%u", pkt_dev->svlan_id);
1589 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1590 pkt_dev->svlan_id = 0xffff;
1593 printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n");
1598 if (!strcmp(name, "svlan_p")) {
1599 len = num_arg(&user_buffer[i], 1, &value);
1604 if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) {
1605 pkt_dev->svlan_p = value;
1606 sprintf(pg_result, "OK: svlan_p=%u", pkt_dev->svlan_p);
1608 sprintf(pg_result, "ERROR: svlan_p must be 0-7");
1613 if (!strcmp(name, "svlan_cfi")) {
1614 len = num_arg(&user_buffer[i], 1, &value);
1619 if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) {
1620 pkt_dev->svlan_cfi = value;
1621 sprintf(pg_result, "OK: svlan_cfi=%u", pkt_dev->svlan_cfi);
1623 sprintf(pg_result, "ERROR: svlan_cfi must be 0-1");
1628 if (!strcmp(name, "tos")) {
1629 __u32 tmp_value = 0;
1630 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1636 pkt_dev->tos = tmp_value;
1637 sprintf(pg_result, "OK: tos=0x%02x", pkt_dev->tos);
1639 sprintf(pg_result, "ERROR: tos must be 00-ff");
1644 if (!strcmp(name, "traffic_class")) {
1645 __u32 tmp_value = 0;
1646 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1652 pkt_dev->traffic_class = tmp_value;
1653 sprintf(pg_result, "OK: traffic_class=0x%02x", pkt_dev->traffic_class);
1655 sprintf(pg_result, "ERROR: traffic_class must be 00-ff");
1660 sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
1664 static int pktgen_if_open(struct inode *inode, struct file *file)
1666 return single_open(file, pktgen_if_show, PDE(inode)->data);
1669 static const struct file_operations pktgen_if_fops = {
1670 .owner = THIS_MODULE,
1671 .open = pktgen_if_open,
1673 .llseek = seq_lseek,
1674 .write = pktgen_if_write,
1675 .release = single_release,
1678 static int pktgen_thread_show(struct seq_file *seq, void *v)
1680 struct pktgen_thread *t = seq->private;
1681 const struct pktgen_dev *pkt_dev;
1685 seq_printf(seq, "Running: ");
1688 list_for_each_entry(pkt_dev, &t->if_list, list)
1689 if (pkt_dev->running)
1690 seq_printf(seq, "%s ", pkt_dev->odev->name);
1692 seq_printf(seq, "\nStopped: ");
1694 list_for_each_entry(pkt_dev, &t->if_list, list)
1695 if (!pkt_dev->running)
1696 seq_printf(seq, "%s ", pkt_dev->odev->name);
1699 seq_printf(seq, "\nResult: %s\n", t->result);
1701 seq_printf(seq, "\nResult: NA\n");
1708 static ssize_t pktgen_thread_write(struct file *file,
1709 const char __user * user_buffer,
1710 size_t count, loff_t * offset)
1712 struct seq_file *seq = (struct seq_file *)file->private_data;
1713 struct pktgen_thread *t = seq->private;
1714 int i = 0, max, len, ret;
1719 // sprintf(pg_result, "Wrong command format");
1724 len = count_trail_chars(&user_buffer[i], max);
1730 /* Read variable name */
1732 len = strn_len(&user_buffer[i], sizeof(name) - 1);
1736 memset(name, 0, sizeof(name));
1737 if (copy_from_user(name, &user_buffer[i], len))
1742 len = count_trail_chars(&user_buffer[i], max);
1749 printk(KERN_DEBUG "pktgen: t=%s, count=%lu\n",
1750 name, (unsigned long)count);
1753 printk(KERN_ERR "pktgen: ERROR: No thread\n");
1758 pg_result = &(t->result[0]);
1760 if (!strcmp(name, "add_device")) {
1763 len = strn_len(&user_buffer[i], sizeof(f) - 1);
1768 if (copy_from_user(f, &user_buffer[i], len))
1771 mutex_lock(&pktgen_thread_lock);
1772 pktgen_add_device(t, f);
1773 mutex_unlock(&pktgen_thread_lock);
1775 sprintf(pg_result, "OK: add_device=%s", f);
1779 if (!strcmp(name, "rem_device_all")) {
1780 mutex_lock(&pktgen_thread_lock);
1781 t->control |= T_REMDEVALL;
1782 mutex_unlock(&pktgen_thread_lock);
1783 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
1785 sprintf(pg_result, "OK: rem_device_all");
1789 if (!strcmp(name, "max_before_softirq")) {
1790 sprintf(pg_result, "OK: Note! max_before_softirq is obsoleted -- Do not use");
1800 static int pktgen_thread_open(struct inode *inode, struct file *file)
1802 return single_open(file, pktgen_thread_show, PDE(inode)->data);
1805 static const struct file_operations pktgen_thread_fops = {
1806 .owner = THIS_MODULE,
1807 .open = pktgen_thread_open,
1809 .llseek = seq_lseek,
1810 .write = pktgen_thread_write,
1811 .release = single_release,
1814 /* Think find or remove for NN */
1815 static struct pktgen_dev *__pktgen_NN_threads(const char *ifname, int remove)
1817 struct pktgen_thread *t;
1818 struct pktgen_dev *pkt_dev = NULL;
1820 list_for_each_entry(t, &pktgen_threads, th_list) {
1821 pkt_dev = pktgen_find_dev(t, ifname);
1825 pkt_dev->removal_mark = 1;
1826 t->control |= T_REMDEV;
1836 * mark a device for removal
1838 static void pktgen_mark_device(const char *ifname)
1840 struct pktgen_dev *pkt_dev = NULL;
1841 const int max_tries = 10, msec_per_try = 125;
1844 mutex_lock(&pktgen_thread_lock);
1845 pr_debug("pktgen: pktgen_mark_device marking %s for removal\n", ifname);
1849 pkt_dev = __pktgen_NN_threads(ifname, REMOVE);
1850 if (pkt_dev == NULL)
1851 break; /* success */
1853 mutex_unlock(&pktgen_thread_lock);
1854 pr_debug("pktgen: pktgen_mark_device waiting for %s "
1855 "to disappear....\n", ifname);
1856 schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
1857 mutex_lock(&pktgen_thread_lock);
1859 if (++i >= max_tries) {
1860 printk(KERN_ERR "pktgen_mark_device: timed out after "
1861 "waiting %d msec for device %s to be removed\n",
1862 msec_per_try * i, ifname);
1868 mutex_unlock(&pktgen_thread_lock);
1871 static void pktgen_change_name(struct net_device *dev)
1873 struct pktgen_thread *t;
1875 list_for_each_entry(t, &pktgen_threads, th_list) {
1876 struct pktgen_dev *pkt_dev;
1878 list_for_each_entry(pkt_dev, &t->if_list, list) {
1879 if (pkt_dev->odev != dev)
1882 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
1884 pkt_dev->entry = proc_create_data(dev->name, 0600,
1888 if (!pkt_dev->entry)
1889 printk(KERN_ERR "pktgen: can't move proc "
1890 " entry for '%s'\n", dev->name);
1896 static int pktgen_device_event(struct notifier_block *unused,
1897 unsigned long event, void *ptr)
1899 struct net_device *dev = ptr;
1901 if (!net_eq(dev_net(dev), &init_net))
1904 /* It is OK that we do not hold the group lock right now,
1905 * as we run under the RTNL lock.
1909 case NETDEV_CHANGENAME:
1910 pktgen_change_name(dev);
1913 case NETDEV_UNREGISTER:
1914 pktgen_mark_device(dev->name);
1921 static struct net_device *pktgen_dev_get_by_name(struct pktgen_dev *pkt_dev,
1927 for (i = 0; ifname[i] != '@'; i++) {
1935 return dev_get_by_name(&init_net, b);
1939 /* Associate pktgen_dev with a device. */
1941 static int pktgen_setup_dev(struct pktgen_dev *pkt_dev, const char *ifname)
1943 struct net_device *odev;
1946 /* Clean old setups */
1947 if (pkt_dev->odev) {
1948 dev_put(pkt_dev->odev);
1949 pkt_dev->odev = NULL;
1952 odev = pktgen_dev_get_by_name(pkt_dev, ifname);
1954 printk(KERN_ERR "pktgen: no such netdevice: \"%s\"\n", ifname);
1958 if (odev->type != ARPHRD_ETHER) {
1959 printk(KERN_ERR "pktgen: not an ethernet device: \"%s\"\n", ifname);
1961 } else if (!netif_running(odev)) {
1962 printk(KERN_ERR "pktgen: device is down: \"%s\"\n", ifname);
1965 pkt_dev->odev = odev;
1973 /* Read pkt_dev from the interface and set up internal pktgen_dev
1974 * structure to have the right information to create/send packets
1976 static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
1980 if (!pkt_dev->odev) {
1981 printk(KERN_ERR "pktgen: ERROR: pkt_dev->odev == NULL in "
1983 sprintf(pkt_dev->result,
1984 "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
1988 /* make sure that we don't pick a non-existing transmit queue */
1989 ntxq = pkt_dev->odev->real_num_tx_queues;
1991 if (ntxq <= pkt_dev->queue_map_min) {
1992 printk(KERN_WARNING "pktgen: WARNING: Requested "
1993 "queue_map_min (zero-based) (%d) exceeds valid range "
1994 "[0 - %d] for (%d) queues on %s, resetting\n",
1995 pkt_dev->queue_map_min, (ntxq ?: 1) - 1, ntxq,
1996 pkt_dev->odev->name);
1997 pkt_dev->queue_map_min = ntxq - 1;
1999 if (pkt_dev->queue_map_max >= ntxq) {
2000 printk(KERN_WARNING "pktgen: WARNING: Requested "
2001 "queue_map_max (zero-based) (%d) exceeds valid range "
2002 "[0 - %d] for (%d) queues on %s, resetting\n",
2003 pkt_dev->queue_map_max, (ntxq ?: 1) - 1, ntxq,
2004 pkt_dev->odev->name);
2005 pkt_dev->queue_map_max = ntxq - 1;
2008 /* Default to the interface's mac if not explicitly set. */
2010 if (is_zero_ether_addr(pkt_dev->src_mac))
2011 memcpy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr, ETH_ALEN);
2013 /* Set up Dest MAC */
2014 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
2016 /* Set up pkt size */
2017 pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
2019 if (pkt_dev->flags & F_IPV6) {
2021 * Skip this automatic address setting until locks or functions
2026 int i, set = 0, err = 1;
2027 struct inet6_dev *idev;
2029 for (i = 0; i < IN6_ADDR_HSIZE; i++)
2030 if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
2038 * Use linklevel address if unconfigured.
2040 * use ipv6_get_lladdr if/when it's get exported
2044 idev = __in6_dev_get(pkt_dev->odev);
2046 struct inet6_ifaddr *ifp;
2048 read_lock_bh(&idev->lock);
2049 for (ifp = idev->addr_list; ifp;
2050 ifp = ifp->if_next) {
2051 if (ifp->scope == IFA_LINK
2053 flags & IFA_F_TENTATIVE)) {
2054 ipv6_addr_copy(&pkt_dev->
2061 read_unlock_bh(&idev->lock);
2065 printk(KERN_ERR "pktgen: ERROR: IPv6 link "
2066 "address not availble.\n");
2070 pkt_dev->saddr_min = 0;
2071 pkt_dev->saddr_max = 0;
2072 if (strlen(pkt_dev->src_min) == 0) {
2074 struct in_device *in_dev;
2077 in_dev = __in_dev_get_rcu(pkt_dev->odev);
2079 if (in_dev->ifa_list) {
2080 pkt_dev->saddr_min =
2081 in_dev->ifa_list->ifa_address;
2082 pkt_dev->saddr_max = pkt_dev->saddr_min;
2087 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
2088 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
2091 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
2092 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
2094 /* Initialize current values. */
2095 pkt_dev->cur_dst_mac_offset = 0;
2096 pkt_dev->cur_src_mac_offset = 0;
2097 pkt_dev->cur_saddr = pkt_dev->saddr_min;
2098 pkt_dev->cur_daddr = pkt_dev->daddr_min;
2099 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2100 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
2101 pkt_dev->nflows = 0;
2105 static void spin(struct pktgen_dev *pkt_dev, ktime_t spin_until)
2107 ktime_t start_time, end_time;
2109 struct hrtimer_sleeper t;
2111 hrtimer_init_on_stack(&t.timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
2112 hrtimer_set_expires(&t.timer, spin_until);
2114 remaining = ktime_to_us(hrtimer_expires_remaining(&t.timer));
2115 if (remaining <= 0) {
2116 pkt_dev->next_tx = ktime_add_ns(spin_until, pkt_dev->delay);
2120 start_time = ktime_now();
2121 if (remaining < 100)
2122 udelay(remaining); /* really small just spin */
2124 /* see do_nanosleep */
2125 hrtimer_init_sleeper(&t, current);
2127 set_current_state(TASK_INTERRUPTIBLE);
2128 hrtimer_start_expires(&t.timer, HRTIMER_MODE_ABS);
2129 if (!hrtimer_active(&t.timer))
2135 hrtimer_cancel(&t.timer);
2136 } while (t.task && pkt_dev->running && !signal_pending(current));
2137 __set_current_state(TASK_RUNNING);
2139 end_time = ktime_now();
2141 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(end_time, start_time));
2142 pkt_dev->next_tx = ktime_add_ns(end_time, pkt_dev->delay);
2145 static inline void set_pkt_overhead(struct pktgen_dev *pkt_dev)
2147 pkt_dev->pkt_overhead = 0;
2148 pkt_dev->pkt_overhead += pkt_dev->nr_labels*sizeof(u32);
2149 pkt_dev->pkt_overhead += VLAN_TAG_SIZE(pkt_dev);
2150 pkt_dev->pkt_overhead += SVLAN_TAG_SIZE(pkt_dev);
2153 static inline int f_seen(const struct pktgen_dev *pkt_dev, int flow)
2155 return !!(pkt_dev->flows[flow].flags & F_INIT);
2158 static inline int f_pick(struct pktgen_dev *pkt_dev)
2160 int flow = pkt_dev->curfl;
2162 if (pkt_dev->flags & F_FLOW_SEQ) {
2163 if (pkt_dev->flows[flow].count >= pkt_dev->lflow) {
2165 pkt_dev->flows[flow].count = 0;
2166 pkt_dev->flows[flow].flags = 0;
2167 pkt_dev->curfl += 1;
2168 if (pkt_dev->curfl >= pkt_dev->cflows)
2169 pkt_dev->curfl = 0; /*reset */
2172 flow = random32() % pkt_dev->cflows;
2173 pkt_dev->curfl = flow;
2175 if (pkt_dev->flows[flow].count > pkt_dev->lflow) {
2176 pkt_dev->flows[flow].count = 0;
2177 pkt_dev->flows[flow].flags = 0;
2181 return pkt_dev->curfl;
2186 /* If there was already an IPSEC SA, we keep it as is, else
2187 * we go look for it ...
2189 static void get_ipsec_sa(struct pktgen_dev *pkt_dev, int flow)
2191 struct xfrm_state *x = pkt_dev->flows[flow].x;
2193 /*slow path: we dont already have xfrm_state*/
2194 x = xfrm_stateonly_find(&init_net,
2195 (xfrm_address_t *)&pkt_dev->cur_daddr,
2196 (xfrm_address_t *)&pkt_dev->cur_saddr,
2199 pkt_dev->ipsproto, 0);
2201 pkt_dev->flows[flow].x = x;
2202 set_pkt_overhead(pkt_dev);
2203 pkt_dev->pkt_overhead += x->props.header_len;
2209 static void set_cur_queue_map(struct pktgen_dev *pkt_dev)
2212 if (pkt_dev->flags & F_QUEUE_MAP_CPU)
2213 pkt_dev->cur_queue_map = smp_processor_id();
2215 else if (pkt_dev->queue_map_min <= pkt_dev->queue_map_max) {
2217 if (pkt_dev->flags & F_QUEUE_MAP_RND) {
2219 (pkt_dev->queue_map_max -
2220 pkt_dev->queue_map_min + 1)
2221 + pkt_dev->queue_map_min;
2223 t = pkt_dev->cur_queue_map + 1;
2224 if (t > pkt_dev->queue_map_max)
2225 t = pkt_dev->queue_map_min;
2227 pkt_dev->cur_queue_map = t;
2229 pkt_dev->cur_queue_map = pkt_dev->cur_queue_map % pkt_dev->odev->real_num_tx_queues;
2232 /* Increment/randomize headers according to flags and current values
2233 * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
2235 static void mod_cur_headers(struct pktgen_dev *pkt_dev)
2241 if (pkt_dev->cflows)
2242 flow = f_pick(pkt_dev);
2244 /* Deal with source MAC */
2245 if (pkt_dev->src_mac_count > 1) {
2249 if (pkt_dev->flags & F_MACSRC_RND)
2250 mc = random32() % pkt_dev->src_mac_count;
2252 mc = pkt_dev->cur_src_mac_offset++;
2253 if (pkt_dev->cur_src_mac_offset >=
2254 pkt_dev->src_mac_count)
2255 pkt_dev->cur_src_mac_offset = 0;
2258 tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
2259 pkt_dev->hh[11] = tmp;
2260 tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2261 pkt_dev->hh[10] = tmp;
2262 tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2263 pkt_dev->hh[9] = tmp;
2264 tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2265 pkt_dev->hh[8] = tmp;
2266 tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
2267 pkt_dev->hh[7] = tmp;
2270 /* Deal with Destination MAC */
2271 if (pkt_dev->dst_mac_count > 1) {
2275 if (pkt_dev->flags & F_MACDST_RND)
2276 mc = random32() % pkt_dev->dst_mac_count;
2279 mc = pkt_dev->cur_dst_mac_offset++;
2280 if (pkt_dev->cur_dst_mac_offset >=
2281 pkt_dev->dst_mac_count) {
2282 pkt_dev->cur_dst_mac_offset = 0;
2286 tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
2287 pkt_dev->hh[5] = tmp;
2288 tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2289 pkt_dev->hh[4] = tmp;
2290 tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2291 pkt_dev->hh[3] = tmp;
2292 tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2293 pkt_dev->hh[2] = tmp;
2294 tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
2295 pkt_dev->hh[1] = tmp;
2298 if (pkt_dev->flags & F_MPLS_RND) {
2300 for (i = 0; i < pkt_dev->nr_labels; i++)
2301 if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
2302 pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
2303 ((__force __be32)random32() &
2307 if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
2308 pkt_dev->vlan_id = random32() & (4096-1);
2311 if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
2312 pkt_dev->svlan_id = random32() & (4096 - 1);
2315 if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
2316 if (pkt_dev->flags & F_UDPSRC_RND)
2317 pkt_dev->cur_udp_src = random32() %
2318 (pkt_dev->udp_src_max - pkt_dev->udp_src_min)
2319 + pkt_dev->udp_src_min;
2322 pkt_dev->cur_udp_src++;
2323 if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
2324 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
2328 if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
2329 if (pkt_dev->flags & F_UDPDST_RND) {
2330 pkt_dev->cur_udp_dst = random32() %
2331 (pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)
2332 + pkt_dev->udp_dst_min;
2334 pkt_dev->cur_udp_dst++;
2335 if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
2336 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2340 if (!(pkt_dev->flags & F_IPV6)) {
2342 imn = ntohl(pkt_dev->saddr_min);
2343 imx = ntohl(pkt_dev->saddr_max);
2346 if (pkt_dev->flags & F_IPSRC_RND)
2347 t = random32() % (imx - imn) + imn;
2349 t = ntohl(pkt_dev->cur_saddr);
2355 pkt_dev->cur_saddr = htonl(t);
2358 if (pkt_dev->cflows && f_seen(pkt_dev, flow)) {
2359 pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
2361 imn = ntohl(pkt_dev->daddr_min);
2362 imx = ntohl(pkt_dev->daddr_max);
2366 if (pkt_dev->flags & F_IPDST_RND) {
2368 t = random32() % (imx - imn) + imn;
2371 while (ipv4_is_loopback(s) ||
2372 ipv4_is_multicast(s) ||
2373 ipv4_is_lbcast(s) ||
2374 ipv4_is_zeronet(s) ||
2375 ipv4_is_local_multicast(s)) {
2376 t = random32() % (imx - imn) + imn;
2379 pkt_dev->cur_daddr = s;
2381 t = ntohl(pkt_dev->cur_daddr);
2386 pkt_dev->cur_daddr = htonl(t);
2389 if (pkt_dev->cflows) {
2390 pkt_dev->flows[flow].flags |= F_INIT;
2391 pkt_dev->flows[flow].cur_daddr =
2394 if (pkt_dev->flags & F_IPSEC_ON)
2395 get_ipsec_sa(pkt_dev, flow);
2400 } else { /* IPV6 * */
2402 if (pkt_dev->min_in6_daddr.s6_addr32[0] == 0 &&
2403 pkt_dev->min_in6_daddr.s6_addr32[1] == 0 &&
2404 pkt_dev->min_in6_daddr.s6_addr32[2] == 0 &&
2405 pkt_dev->min_in6_daddr.s6_addr32[3] == 0) ;
2409 /* Only random destinations yet */
2411 for (i = 0; i < 4; i++) {
2412 pkt_dev->cur_in6_daddr.s6_addr32[i] =
2413 (((__force __be32)random32() |
2414 pkt_dev->min_in6_daddr.s6_addr32[i]) &
2415 pkt_dev->max_in6_daddr.s6_addr32[i]);
2420 if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
2422 if (pkt_dev->flags & F_TXSIZE_RND) {
2424 (pkt_dev->max_pkt_size - pkt_dev->min_pkt_size)
2425 + pkt_dev->min_pkt_size;
2427 t = pkt_dev->cur_pkt_size + 1;
2428 if (t > pkt_dev->max_pkt_size)
2429 t = pkt_dev->min_pkt_size;
2431 pkt_dev->cur_pkt_size = t;
2434 set_cur_queue_map(pkt_dev);
2436 pkt_dev->flows[flow].count++;
2441 static int pktgen_output_ipsec(struct sk_buff *skb, struct pktgen_dev *pkt_dev)
2443 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2449 /* XXX: we dont support tunnel mode for now until
2450 * we resolve the dst issue */
2451 if (x->props.mode != XFRM_MODE_TRANSPORT)
2454 spin_lock(&x->lock);
2457 err = x->outer_mode->output(x, skb);
2460 err = x->type->output(x, skb);
2464 x->curlft.bytes += skb->len;
2465 x->curlft.packets++;
2467 spin_unlock(&x->lock);
2471 static void free_SAs(struct pktgen_dev *pkt_dev)
2473 if (pkt_dev->cflows) {
2474 /* let go of the SAs if we have them */
2476 for (; i < pkt_dev->cflows; i++) {
2477 struct xfrm_state *x = pkt_dev->flows[i].x;
2480 pkt_dev->flows[i].x = NULL;
2486 static int process_ipsec(struct pktgen_dev *pkt_dev,
2487 struct sk_buff *skb, __be16 protocol)
2489 if (pkt_dev->flags & F_IPSEC_ON) {
2490 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2495 nhead = x->props.header_len - skb_headroom(skb);
2497 ret = pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
2499 printk(KERN_ERR "Error expanding "
2500 "ipsec packet %d\n", ret);
2505 /* ipsec is not expecting ll header */
2506 skb_pull(skb, ETH_HLEN);
2507 ret = pktgen_output_ipsec(skb, pkt_dev);
2509 printk(KERN_ERR "Error creating ipsec "
2510 "packet %d\n", ret);
2514 eth = (__u8 *) skb_push(skb, ETH_HLEN);
2515 memcpy(eth, pkt_dev->hh, 12);
2516 *(u16 *) ð[12] = protocol;
2526 static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
2529 for (i = 0; i < pkt_dev->nr_labels; i++)
2530 *mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
2533 *mpls |= MPLS_STACK_BOTTOM;
2536 static inline __be16 build_tci(unsigned int id, unsigned int cfi,
2539 return htons(id | (cfi << 12) | (prio << 13));
2542 static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
2543 struct pktgen_dev *pkt_dev)
2545 struct sk_buff *skb = NULL;
2547 struct udphdr *udph;
2550 struct pktgen_hdr *pgh = NULL;
2551 __be16 protocol = htons(ETH_P_IP);
2553 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2554 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2555 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2556 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2559 if (pkt_dev->nr_labels)
2560 protocol = htons(ETH_P_MPLS_UC);
2562 if (pkt_dev->vlan_id != 0xffff)
2563 protocol = htons(ETH_P_8021Q);
2565 /* Update any of the values, used when we're incrementing various
2568 queue_map = pkt_dev->cur_queue_map;
2569 mod_cur_headers(pkt_dev);
2571 datalen = (odev->hard_header_len + 16) & ~0xf;
2572 skb = __netdev_alloc_skb(odev,
2573 pkt_dev->cur_pkt_size + 64
2574 + datalen + pkt_dev->pkt_overhead, GFP_NOWAIT);
2576 sprintf(pkt_dev->result, "No memory");
2580 skb_reserve(skb, datalen);
2582 /* Reserve for ethernet and IP header */
2583 eth = (__u8 *) skb_push(skb, 14);
2584 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2585 if (pkt_dev->nr_labels)
2586 mpls_push(mpls, pkt_dev);
2588 if (pkt_dev->vlan_id != 0xffff) {
2589 if (pkt_dev->svlan_id != 0xffff) {
2590 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2591 *svlan_tci = build_tci(pkt_dev->svlan_id,
2594 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2595 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
2597 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2598 *vlan_tci = build_tci(pkt_dev->vlan_id,
2601 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2602 *vlan_encapsulated_proto = htons(ETH_P_IP);
2605 skb->network_header = skb->tail;
2606 skb->transport_header = skb->network_header + sizeof(struct iphdr);
2607 skb_put(skb, sizeof(struct iphdr) + sizeof(struct udphdr));
2608 skb_set_queue_mapping(skb, queue_map);
2610 udph = udp_hdr(skb);
2612 memcpy(eth, pkt_dev->hh, 12);
2613 *(__be16 *) & eth[12] = protocol;
2615 /* Eth + IPh + UDPh + mpls */
2616 datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
2617 pkt_dev->pkt_overhead;
2618 if (datalen < sizeof(struct pktgen_hdr))
2619 datalen = sizeof(struct pktgen_hdr);
2621 udph->source = htons(pkt_dev->cur_udp_src);
2622 udph->dest = htons(pkt_dev->cur_udp_dst);
2623 udph->len = htons(datalen + 8); /* DATA + udphdr */
2624 udph->check = 0; /* No checksum */
2629 iph->tos = pkt_dev->tos;
2630 iph->protocol = IPPROTO_UDP; /* UDP */
2631 iph->saddr = pkt_dev->cur_saddr;
2632 iph->daddr = pkt_dev->cur_daddr;
2634 iplen = 20 + 8 + datalen;
2635 iph->tot_len = htons(iplen);
2637 iph->check = ip_fast_csum((void *)iph, iph->ihl);
2638 skb->protocol = protocol;
2639 skb->mac_header = (skb->network_header - ETH_HLEN -
2640 pkt_dev->pkt_overhead);
2642 skb->pkt_type = PACKET_HOST;
2644 if (pkt_dev->nfrags <= 0)
2645 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
2647 int frags = pkt_dev->nfrags;
2650 pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
2652 if (frags > MAX_SKB_FRAGS)
2653 frags = MAX_SKB_FRAGS;
2654 if (datalen > frags * PAGE_SIZE) {
2655 skb_put(skb, datalen - frags * PAGE_SIZE);
2656 datalen = frags * PAGE_SIZE;
2660 while (datalen > 0) {
2661 struct page *page = alloc_pages(GFP_KERNEL, 0);
2662 skb_shinfo(skb)->frags[i].page = page;
2663 skb_shinfo(skb)->frags[i].page_offset = 0;
2664 skb_shinfo(skb)->frags[i].size =
2665 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
2666 datalen -= skb_shinfo(skb)->frags[i].size;
2667 skb->len += skb_shinfo(skb)->frags[i].size;
2668 skb->data_len += skb_shinfo(skb)->frags[i].size;
2670 skb_shinfo(skb)->nr_frags = i;
2679 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
2683 skb_shinfo(skb)->frags[i - 1].size -= rem;
2685 skb_shinfo(skb)->frags[i] =
2686 skb_shinfo(skb)->frags[i - 1];
2687 get_page(skb_shinfo(skb)->frags[i].page);
2688 skb_shinfo(skb)->frags[i].page =
2689 skb_shinfo(skb)->frags[i - 1].page;
2690 skb_shinfo(skb)->frags[i].page_offset +=
2691 skb_shinfo(skb)->frags[i - 1].size;
2692 skb_shinfo(skb)->frags[i].size = rem;
2694 skb_shinfo(skb)->nr_frags = i;
2698 /* Stamp the time, and sequence number,
2699 * convert them to network byte order
2702 struct timeval timestamp;
2704 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2705 pgh->seq_num = htonl(pkt_dev->seq_num);
2707 do_gettimeofday(×tamp);
2708 pgh->tv_sec = htonl(timestamp.tv_sec);
2709 pgh->tv_usec = htonl(timestamp.tv_usec);
2713 if (!process_ipsec(pkt_dev, skb, protocol))
2721 * scan_ip6, fmt_ip taken from dietlibc-0.21
2722 * Author Felix von Leitner <felix-dietlibc@fefe.de>
2724 * Slightly modified for kernel.
2725 * Should be candidate for net/ipv4/utils.c
2729 static unsigned int scan_ip6(const char *s, char ip[16])
2732 unsigned int len = 0;
2735 unsigned int prefixlen = 0;
2736 unsigned int suffixlen = 0;
2740 for (i = 0; i < 16; i++)
2746 if (s[1] == ':') { /* Found "::", skip to part 2 */
2754 u = simple_strtoul(s, &pos, 16);
2758 if (prefixlen == 12 && s[i] == '.') {
2760 /* the last 4 bytes may be written as IPv4 address */
2763 memcpy((struct in_addr *)(ip + 12), &tmp, sizeof(tmp));
2766 ip[prefixlen++] = (u >> 8);
2767 ip[prefixlen++] = (u & 255);
2770 if (prefixlen == 16)
2774 /* part 2, after "::" */
2781 } else if (suffixlen != 0)
2784 u = simple_strtol(s, &pos, 16);
2791 if (suffixlen + prefixlen <= 12 && s[i] == '.') {
2793 memcpy((struct in_addr *)(suffix + suffixlen), &tmp,
2799 suffix[suffixlen++] = (u >> 8);
2800 suffix[suffixlen++] = (u & 255);
2803 if (prefixlen + suffixlen == 16)
2806 for (i = 0; i < suffixlen; i++)
2807 ip[16 - suffixlen + i] = suffix[i];
2811 static char tohex(char hexdigit)
2813 return hexdigit > 9 ? hexdigit + 'a' - 10 : hexdigit + '0';
2816 static int fmt_xlong(char *s, unsigned int i)
2819 *s = tohex((i >> 12) & 0xf);
2820 if (s != bak || *s != '0')
2822 *s = tohex((i >> 8) & 0xf);
2823 if (s != bak || *s != '0')
2825 *s = tohex((i >> 4) & 0xf);
2826 if (s != bak || *s != '0')
2828 *s = tohex(i & 0xf);
2832 static unsigned int fmt_ip6(char *s, const char ip[16])
2837 unsigned int compressing;
2842 for (j = 0; j < 16; j += 2) {
2844 #ifdef V4MAPPEDPREFIX
2845 if (j == 12 && !memcmp(ip, V4mappedprefix, 12)) {
2846 inet_ntoa_r(*(struct in_addr *)(ip + 12), s);
2851 temp = ((unsigned long)(unsigned char)ip[j] << 8) +
2852 (unsigned long)(unsigned char)ip[j + 1];
2867 i = fmt_xlong(s, temp);
2884 static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
2885 struct pktgen_dev *pkt_dev)
2887 struct sk_buff *skb = NULL;
2889 struct udphdr *udph;
2891 struct ipv6hdr *iph;
2892 struct pktgen_hdr *pgh = NULL;
2893 __be16 protocol = htons(ETH_P_IPV6);
2895 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2896 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2897 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2898 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2901 if (pkt_dev->nr_labels)
2902 protocol = htons(ETH_P_MPLS_UC);
2904 if (pkt_dev->vlan_id != 0xffff)
2905 protocol = htons(ETH_P_8021Q);
2907 /* Update any of the values, used when we're incrementing various
2910 queue_map = pkt_dev->cur_queue_map;
2911 mod_cur_headers(pkt_dev);
2913 skb = __netdev_alloc_skb(odev,
2914 pkt_dev->cur_pkt_size + 64
2915 + 16 + pkt_dev->pkt_overhead, GFP_NOWAIT);
2917 sprintf(pkt_dev->result, "No memory");
2921 skb_reserve(skb, 16);
2923 /* Reserve for ethernet and IP header */
2924 eth = (__u8 *) skb_push(skb, 14);
2925 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2926 if (pkt_dev->nr_labels)
2927 mpls_push(mpls, pkt_dev);
2929 if (pkt_dev->vlan_id != 0xffff) {
2930 if (pkt_dev->svlan_id != 0xffff) {
2931 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2932 *svlan_tci = build_tci(pkt_dev->svlan_id,
2935 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2936 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
2938 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2939 *vlan_tci = build_tci(pkt_dev->vlan_id,
2942 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2943 *vlan_encapsulated_proto = htons(ETH_P_IPV6);
2946 skb->network_header = skb->tail;
2947 skb->transport_header = skb->network_header + sizeof(struct ipv6hdr);
2948 skb_put(skb, sizeof(struct ipv6hdr) + sizeof(struct udphdr));
2949 skb_set_queue_mapping(skb, queue_map);
2950 iph = ipv6_hdr(skb);
2951 udph = udp_hdr(skb);
2953 memcpy(eth, pkt_dev->hh, 12);
2954 *(__be16 *) ð[12] = protocol;
2956 /* Eth + IPh + UDPh + mpls */
2957 datalen = pkt_dev->cur_pkt_size - 14 -
2958 sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
2959 pkt_dev->pkt_overhead;
2961 if (datalen < sizeof(struct pktgen_hdr)) {
2962 datalen = sizeof(struct pktgen_hdr);
2963 if (net_ratelimit())
2964 printk(KERN_INFO "pktgen: increased datalen to %d\n",
2968 udph->source = htons(pkt_dev->cur_udp_src);
2969 udph->dest = htons(pkt_dev->cur_udp_dst);
2970 udph->len = htons(datalen + sizeof(struct udphdr));
2971 udph->check = 0; /* No checksum */
2973 *(__be32 *) iph = htonl(0x60000000); /* Version + flow */
2975 if (pkt_dev->traffic_class) {
2976 /* Version + traffic class + flow (0) */
2977 *(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
2980 iph->hop_limit = 32;
2982 iph->payload_len = htons(sizeof(struct udphdr) + datalen);
2983 iph->nexthdr = IPPROTO_UDP;
2985 ipv6_addr_copy(&iph->daddr, &pkt_dev->cur_in6_daddr);
2986 ipv6_addr_copy(&iph->saddr, &pkt_dev->cur_in6_saddr);
2988 skb->mac_header = (skb->network_header - ETH_HLEN -
2989 pkt_dev->pkt_overhead);
2990 skb->protocol = protocol;
2992 skb->pkt_type = PACKET_HOST;
2994 if (pkt_dev->nfrags <= 0)
2995 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
2997 int frags = pkt_dev->nfrags;
3000 pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
3002 if (frags > MAX_SKB_FRAGS)
3003 frags = MAX_SKB_FRAGS;
3004 if (datalen > frags * PAGE_SIZE) {
3005 skb_put(skb, datalen - frags * PAGE_SIZE);
3006 datalen = frags * PAGE_SIZE;
3010 while (datalen > 0) {
3011 struct page *page = alloc_pages(GFP_KERNEL, 0);
3012 skb_shinfo(skb)->frags[i].page = page;
3013 skb_shinfo(skb)->frags[i].page_offset = 0;
3014 skb_shinfo(skb)->frags[i].size =
3015 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
3016 datalen -= skb_shinfo(skb)->frags[i].size;
3017 skb->len += skb_shinfo(skb)->frags[i].size;
3018 skb->data_len += skb_shinfo(skb)->frags[i].size;
3020 skb_shinfo(skb)->nr_frags = i;
3029 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
3033 skb_shinfo(skb)->frags[i - 1].size -= rem;
3035 skb_shinfo(skb)->frags[i] =
3036 skb_shinfo(skb)->frags[i - 1];
3037 get_page(skb_shinfo(skb)->frags[i].page);
3038 skb_shinfo(skb)->frags[i].page =
3039 skb_shinfo(skb)->frags[i - 1].page;
3040 skb_shinfo(skb)->frags[i].page_offset +=
3041 skb_shinfo(skb)->frags[i - 1].size;
3042 skb_shinfo(skb)->frags[i].size = rem;
3044 skb_shinfo(skb)->nr_frags = i;
3048 /* Stamp the time, and sequence number,
3049 * convert them to network byte order
3050 * should we update cloned packets too ?
3053 struct timeval timestamp;
3055 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
3056 pgh->seq_num = htonl(pkt_dev->seq_num);
3058 do_gettimeofday(×tamp);
3059 pgh->tv_sec = htonl(timestamp.tv_sec);
3060 pgh->tv_usec = htonl(timestamp.tv_usec);
3062 /* pkt_dev->seq_num++; FF: you really mean this? */
3067 static struct sk_buff *fill_packet(struct net_device *odev,
3068 struct pktgen_dev *pkt_dev)
3070 if (pkt_dev->flags & F_IPV6)
3071 return fill_packet_ipv6(odev, pkt_dev);
3073 return fill_packet_ipv4(odev, pkt_dev);
3076 static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
3078 pkt_dev->seq_num = 1;
3079 pkt_dev->idle_acc = 0;
3081 pkt_dev->tx_bytes = 0;
3082 pkt_dev->errors = 0;
3085 /* Set up structure for sending pkts, clear counters */
3087 static void pktgen_run(struct pktgen_thread *t)
3089 struct pktgen_dev *pkt_dev;
3092 pr_debug("pktgen: entering pktgen_run. %p\n", t);
3095 list_for_each_entry(pkt_dev, &t->if_list, list) {
3098 * setup odev and create initial packet.
3100 pktgen_setup_inject(pkt_dev);
3102 if (pkt_dev->odev) {
3103 pktgen_clear_counters(pkt_dev);
3104 pkt_dev->running = 1; /* Cranke yeself! */
3105 pkt_dev->skb = NULL;
3106 pkt_dev->started_at =
3107 pkt_dev->next_tx = ktime_now();
3109 set_pkt_overhead(pkt_dev);
3111 strcpy(pkt_dev->result, "Starting");
3114 strcpy(pkt_dev->result, "Error starting");
3118 t->control &= ~(T_STOP);
3121 static void pktgen_stop_all_threads_ifs(void)
3123 struct pktgen_thread *t;
3125 pr_debug("pktgen: entering pktgen_stop_all_threads_ifs.\n");
3127 mutex_lock(&pktgen_thread_lock);
3129 list_for_each_entry(t, &pktgen_threads, th_list)
3130 t->control |= T_STOP;
3132 mutex_unlock(&pktgen_thread_lock);
3135 static int thread_is_running(const struct pktgen_thread *t)
3137 const struct pktgen_dev *pkt_dev;
3139 list_for_each_entry(pkt_dev, &t->if_list, list)
3140 if (pkt_dev->running)
3145 static int pktgen_wait_thread_run(struct pktgen_thread *t)
3149 while (thread_is_running(t)) {
3153 msleep_interruptible(100);
3155 if (signal_pending(current))
3165 static int pktgen_wait_all_threads_run(void)
3167 struct pktgen_thread *t;
3170 mutex_lock(&pktgen_thread_lock);
3172 list_for_each_entry(t, &pktgen_threads, th_list) {
3173 sig = pktgen_wait_thread_run(t);
3179 list_for_each_entry(t, &pktgen_threads, th_list)
3180 t->control |= (T_STOP);
3182 mutex_unlock(&pktgen_thread_lock);
3186 static void pktgen_run_all_threads(void)
3188 struct pktgen_thread *t;
3190 pr_debug("pktgen: entering pktgen_run_all_threads.\n");
3192 mutex_lock(&pktgen_thread_lock);
3194 list_for_each_entry(t, &pktgen_threads, th_list)
3195 t->control |= (T_RUN);
3197 mutex_unlock(&pktgen_thread_lock);
3199 /* Propagate thread->control */
3200 schedule_timeout_interruptible(msecs_to_jiffies(125));
3202 pktgen_wait_all_threads_run();
3205 static void pktgen_reset_all_threads(void)
3207 struct pktgen_thread *t;
3209 pr_debug("pktgen: entering pktgen_reset_all_threads.\n");
3211 mutex_lock(&pktgen_thread_lock);
3213 list_for_each_entry(t, &pktgen_threads, th_list)
3214 t->control |= (T_REMDEVALL);
3216 mutex_unlock(&pktgen_thread_lock);
3218 /* Propagate thread->control */
3219 schedule_timeout_interruptible(msecs_to_jiffies(125));
3221 pktgen_wait_all_threads_run();
3224 static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
3226 __u64 bps, mbps, pps;
3227 char *p = pkt_dev->result;
3228 ktime_t elapsed = ktime_sub(pkt_dev->stopped_at,
3229 pkt_dev->started_at);
3230 ktime_t idle = ns_to_ktime(pkt_dev->idle_acc);
3232 p += sprintf(p, "OK: %llu(c%llu+d%llu) nsec, %llu (%dbyte,%dfrags)\n",
3233 (unsigned long long)ktime_to_us(elapsed),
3234 (unsigned long long)ktime_to_us(ktime_sub(elapsed, idle)),
3235 (unsigned long long)ktime_to_us(idle),
3236 (unsigned long long)pkt_dev->sofar,