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>
168 #include <asm/uaccess.h>
169 #include <asm/div64.h> /* do_div */
170 #include <asm/timex.h>
172 #define VERSION "pktgen v2.70: Packet Generator for packet performance testing.\n"
174 #define IP_NAME_SZ 32
175 #define MAX_MPLS_LABELS 16 /* This is the max label stack depth */
176 #define MPLS_STACK_BOTTOM htonl(0x00000100)
178 /* Device flag bits */
179 #define F_IPSRC_RND (1<<0) /* IP-Src Random */
180 #define F_IPDST_RND (1<<1) /* IP-Dst Random */
181 #define F_UDPSRC_RND (1<<2) /* UDP-Src Random */
182 #define F_UDPDST_RND (1<<3) /* UDP-Dst Random */
183 #define F_MACSRC_RND (1<<4) /* MAC-Src Random */
184 #define F_MACDST_RND (1<<5) /* MAC-Dst Random */
185 #define F_TXSIZE_RND (1<<6) /* Transmit size is random */
186 #define F_IPV6 (1<<7) /* Interface in IPV6 Mode */
187 #define F_MPLS_RND (1<<8) /* Random MPLS labels */
188 #define F_VID_RND (1<<9) /* Random VLAN ID */
189 #define F_SVID_RND (1<<10) /* Random SVLAN ID */
190 #define F_FLOW_SEQ (1<<11) /* Sequential flows */
191 #define F_IPSEC_ON (1<<12) /* ipsec on for flows */
192 #define F_QUEUE_MAP_RND (1<<13) /* queue map Random */
193 #define F_QUEUE_MAP_CPU (1<<14) /* queue map mirrors smp_processor_id() */
195 /* Thread control flag bits */
196 #define T_TERMINATE (1<<0)
197 #define T_STOP (1<<1) /* Stop run */
198 #define T_RUN (1<<2) /* Start run */
199 #define T_REMDEVALL (1<<3) /* Remove all devs */
200 #define T_REMDEV (1<<4) /* Remove one dev */
202 /* If lock -- can be removed after some work */
203 #define if_lock(t) spin_lock(&(t->if_lock));
204 #define if_unlock(t) spin_unlock(&(t->if_lock));
206 /* Used to help with determining the pkts on receive */
207 #define PKTGEN_MAGIC 0xbe9be955
208 #define PG_PROC_DIR "pktgen"
209 #define PGCTRL "pgctrl"
210 static struct proc_dir_entry *pg_proc_dir = NULL;
212 #define MAX_CFLOWS 65536
214 #define VLAN_TAG_SIZE(x) ((x)->vlan_id == 0xffff ? 0 : 4)
215 #define SVLAN_TAG_SIZE(x) ((x)->svlan_id == 0xffff ? 0 : 4)
221 struct xfrm_state *x;
227 #define F_INIT (1<<0) /* flow has been initialized */
231 * Try to keep frequent/infrequent used vars. separated.
233 struct proc_dir_entry *entry; /* proc file */
234 struct pktgen_thread *pg_thread;/* the owner */
235 struct list_head list; /* Used for chaining in the thread's run-queue */
237 int running; /* if this changes to false, the test will stop */
239 /* If min != max, then we will either do a linear iteration, or
240 * we will do a random selection from within the range.
243 int removal_mark; /* non-zero => the device is marked for
244 * removal by worker thread */
246 int min_pkt_size; /* = ETH_ZLEN; */
247 int max_pkt_size; /* = ETH_ZLEN; */
248 int pkt_overhead; /* overhead for MPLS, VLANs, IPSEC etc */
250 u64 delay; /* nano-seconds */
252 __u64 count; /* Default No packets to send */
253 __u64 sofar; /* How many pkts we've sent so far */
254 __u64 tx_bytes; /* How many bytes we've transmitted */
255 __u64 errors; /* Errors when trying to transmit, pkts will be re-sent */
257 /* runtime counters relating to clone_skb */
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.
268 u64 idle_acc; /* nano-seconds */
272 int clone_skb; /* Use multiple SKBs during packet gen. If this number
273 * is greater than 1, then that many copies of the same
274 * packet will be sent before a new packet is allocated.
275 * For instance, if you want to send 1024 identical packets
276 * before creating a new packet, set clone_skb to 1024.
279 char dst_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
280 char dst_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
281 char src_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
282 char src_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
284 struct in6_addr in6_saddr;
285 struct in6_addr in6_daddr;
286 struct in6_addr cur_in6_daddr;
287 struct in6_addr cur_in6_saddr;
289 struct in6_addr min_in6_daddr;
290 struct in6_addr max_in6_daddr;
291 struct in6_addr min_in6_saddr;
292 struct in6_addr max_in6_saddr;
294 /* If we're doing ranges, random or incremental, then this
295 * defines the min/max for those ranges.
297 __be32 saddr_min; /* inclusive, source IP address */
298 __be32 saddr_max; /* exclusive, source IP address */
299 __be32 daddr_min; /* inclusive, dest IP address */
300 __be32 daddr_max; /* exclusive, dest IP address */
302 __u16 udp_src_min; /* inclusive, source UDP port */
303 __u16 udp_src_max; /* exclusive, source UDP port */
304 __u16 udp_dst_min; /* inclusive, dest UDP port */
305 __u16 udp_dst_max; /* exclusive, dest UDP port */
308 __u8 tos; /* six most significant bits of (former) IPv4 TOS are for dscp codepoint */
309 __u8 traffic_class; /* ditto for the (former) Traffic Class in IPv6 (see RFC 3260, sec. 4) */
312 unsigned nr_labels; /* Depth of stack, 0 = no MPLS */
313 __be32 labels[MAX_MPLS_LABELS];
315 /* VLAN/SVLAN (802.1Q/Q-in-Q) */
318 __u16 vlan_id; /* 0xffff means no vlan tag */
322 __u16 svlan_id; /* 0xffff means no svlan tag */
324 __u32 src_mac_count; /* How many MACs to iterate through */
325 __u32 dst_mac_count; /* How many MACs to iterate through */
327 unsigned char dst_mac[ETH_ALEN];
328 unsigned char src_mac[ETH_ALEN];
330 __u32 cur_dst_mac_offset;
331 __u32 cur_src_mac_offset;
341 0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
343 We fill in SRC address later
344 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
348 __u16 pad; /* pad out the hh struct to an even 16 bytes */
350 struct sk_buff *skb; /* skb we are to transmit next, mainly used for when we
351 * are transmitting the same one multiple times
353 struct net_device *odev; /* The out-going device. Note that the device should
354 * have it's pg_info pointer pointing back to this
355 * device. This will be set when the user specifies
356 * the out-going device name (not when the inject is
357 * started as it used to do.)
359 struct flow_state *flows;
360 unsigned cflows; /* Concurrent flows (config) */
361 unsigned lflow; /* Flow length (config) */
362 unsigned nflows; /* accumulated flows (stats) */
363 unsigned curfl; /* current sequenced flow (state)*/
369 __u8 ipsmode; /* IPSEC mode (config) */
370 __u8 ipsproto; /* IPSEC type (config) */
382 struct pktgen_thread {
384 struct list_head if_list; /* All device here */
385 struct list_head th_list;
386 struct task_struct *tsk;
389 /* Field for thread to receive "posted" events terminate, stop ifs etc. */
394 wait_queue_head_t queue;
395 struct completion start_done;
401 static inline ktime_t ktime_now(void)
406 return timespec_to_ktime(ts);
409 /* This works even if 32 bit because of careful byte order choice */
410 static inline int ktime_lt(const ktime_t cmp1, const ktime_t cmp2)
412 return cmp1.tv64 < cmp2.tv64;
415 static const char version[] __initconst = 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_reset_all_threads(void);
424 static void pktgen_stop_all_threads_ifs(void);
426 static void pktgen_stop(struct pktgen_thread *t);
427 static void pktgen_clear_counters(struct pktgen_dev *pkt_dev);
429 static unsigned int scan_ip6(const char *s, char ip[16]);
430 static unsigned int fmt_ip6(char *s, const char ip[16]);
432 /* Module parameters, defaults. */
433 static int pg_count_d __read_mostly = 1000;
434 static int pg_delay_d __read_mostly;
435 static int pg_clone_skb_d __read_mostly;
436 static int debug __read_mostly;
438 static DEFINE_MUTEX(pktgen_thread_lock);
439 static LIST_HEAD(pktgen_threads);
441 static struct notifier_block pktgen_notifier_block = {
442 .notifier_call = pktgen_device_event,
446 * /proc handling functions
450 static int pgctrl_show(struct seq_file *seq, void *v)
452 seq_puts(seq, VERSION);
456 static ssize_t pgctrl_write(struct file *file, const char __user * buf,
457 size_t count, loff_t * ppos)
462 if (!capable(CAP_NET_ADMIN)) {
467 if (count > sizeof(data))
468 count = sizeof(data);
470 if (copy_from_user(data, buf, count)) {
474 data[count - 1] = 0; /* Make string */
476 if (!strcmp(data, "stop"))
477 pktgen_stop_all_threads_ifs();
479 else if (!strcmp(data, "start"))
480 pktgen_run_all_threads();
482 else if (!strcmp(data, "reset"))
483 pktgen_reset_all_threads();
486 printk(KERN_WARNING "pktgen: Unknown command: %s\n", data);
494 static int pgctrl_open(struct inode *inode, struct file *file)
496 return single_open(file, pgctrl_show, PDE(inode)->data);
499 static const struct file_operations pktgen_fops = {
500 .owner = THIS_MODULE,
504 .write = pgctrl_write,
505 .release = single_release,
508 static int pktgen_if_show(struct seq_file *seq, void *v)
510 const struct pktgen_dev *pkt_dev = seq->private;
515 "Params: count %llu min_pkt_size: %u max_pkt_size: %u\n",
516 (unsigned long long)pkt_dev->count, pkt_dev->min_pkt_size,
517 pkt_dev->max_pkt_size);
520 " frags: %d delay: %llu clone_skb: %d ifname: %s\n",
521 pkt_dev->nfrags, (unsigned long long) pkt_dev->delay,
522 pkt_dev->clone_skb, pkt_dev->odev->name);
524 seq_printf(seq, " flows: %u flowlen: %u\n", pkt_dev->cflows,
528 " queue_map_min: %u queue_map_max: %u\n",
529 pkt_dev->queue_map_min,
530 pkt_dev->queue_map_max);
532 if (pkt_dev->flags & F_IPV6) {
533 char b1[128], b2[128], b3[128];
534 fmt_ip6(b1, pkt_dev->in6_saddr.s6_addr);
535 fmt_ip6(b2, pkt_dev->min_in6_saddr.s6_addr);
536 fmt_ip6(b3, pkt_dev->max_in6_saddr.s6_addr);
538 " saddr: %s min_saddr: %s max_saddr: %s\n", b1,
541 fmt_ip6(b1, pkt_dev->in6_daddr.s6_addr);
542 fmt_ip6(b2, pkt_dev->min_in6_daddr.s6_addr);
543 fmt_ip6(b3, pkt_dev->max_in6_daddr.s6_addr);
545 " daddr: %s min_daddr: %s max_daddr: %s\n", b1,
550 " dst_min: %s dst_max: %s\n src_min: %s src_max: %s\n",
551 pkt_dev->dst_min, pkt_dev->dst_max, pkt_dev->src_min,
554 seq_puts(seq, " src_mac: ");
556 seq_printf(seq, "%pM ",
557 is_zero_ether_addr(pkt_dev->src_mac) ?
558 pkt_dev->odev->dev_addr : pkt_dev->src_mac);
560 seq_printf(seq, "dst_mac: ");
561 seq_printf(seq, "%pM\n", pkt_dev->dst_mac);
564 " udp_src_min: %d udp_src_max: %d udp_dst_min: %d udp_dst_max: %d\n",
565 pkt_dev->udp_src_min, pkt_dev->udp_src_max,
566 pkt_dev->udp_dst_min, pkt_dev->udp_dst_max);
569 " src_mac_count: %d dst_mac_count: %d\n",
570 pkt_dev->src_mac_count, pkt_dev->dst_mac_count);
572 if (pkt_dev->nr_labels) {
574 seq_printf(seq, " mpls: ");
575 for (i = 0; i < pkt_dev->nr_labels; i++)
576 seq_printf(seq, "%08x%s", ntohl(pkt_dev->labels[i]),
577 i == pkt_dev->nr_labels-1 ? "\n" : ", ");
580 if (pkt_dev->vlan_id != 0xffff) {
581 seq_printf(seq, " vlan_id: %u vlan_p: %u vlan_cfi: %u\n",
582 pkt_dev->vlan_id, pkt_dev->vlan_p, pkt_dev->vlan_cfi);
585 if (pkt_dev->svlan_id != 0xffff) {
586 seq_printf(seq, " svlan_id: %u vlan_p: %u vlan_cfi: %u\n",
587 pkt_dev->svlan_id, pkt_dev->svlan_p, pkt_dev->svlan_cfi);
591 seq_printf(seq, " tos: 0x%02x\n", pkt_dev->tos);
594 if (pkt_dev->traffic_class) {
595 seq_printf(seq, " traffic_class: 0x%02x\n", pkt_dev->traffic_class);
598 seq_printf(seq, " Flags: ");
600 if (pkt_dev->flags & F_IPV6)
601 seq_printf(seq, "IPV6 ");
603 if (pkt_dev->flags & F_IPSRC_RND)
604 seq_printf(seq, "IPSRC_RND ");
606 if (pkt_dev->flags & F_IPDST_RND)
607 seq_printf(seq, "IPDST_RND ");
609 if (pkt_dev->flags & F_TXSIZE_RND)
610 seq_printf(seq, "TXSIZE_RND ");
612 if (pkt_dev->flags & F_UDPSRC_RND)
613 seq_printf(seq, "UDPSRC_RND ");
615 if (pkt_dev->flags & F_UDPDST_RND)
616 seq_printf(seq, "UDPDST_RND ");
618 if (pkt_dev->flags & F_MPLS_RND)
619 seq_printf(seq, "MPLS_RND ");
621 if (pkt_dev->flags & F_QUEUE_MAP_RND)
622 seq_printf(seq, "QUEUE_MAP_RND ");
624 if (pkt_dev->flags & F_QUEUE_MAP_CPU)
625 seq_printf(seq, "QUEUE_MAP_CPU ");
627 if (pkt_dev->cflows) {
628 if (pkt_dev->flags & F_FLOW_SEQ)
629 seq_printf(seq, "FLOW_SEQ "); /*in sequence flows*/
631 seq_printf(seq, "FLOW_RND ");
635 if (pkt_dev->flags & F_IPSEC_ON)
636 seq_printf(seq, "IPSEC ");
639 if (pkt_dev->flags & F_MACSRC_RND)
640 seq_printf(seq, "MACSRC_RND ");
642 if (pkt_dev->flags & F_MACDST_RND)
643 seq_printf(seq, "MACDST_RND ");
645 if (pkt_dev->flags & F_VID_RND)
646 seq_printf(seq, "VID_RND ");
648 if (pkt_dev->flags & F_SVID_RND)
649 seq_printf(seq, "SVID_RND ");
653 /* not really stopped, more like last-running-at */
654 stopped = pkt_dev->running ? ktime_now() : pkt_dev->stopped_at;
655 idle = pkt_dev->idle_acc;
656 do_div(idle, NSEC_PER_USEC);
659 "Current:\n pkts-sofar: %llu errors: %llu\n",
660 (unsigned long long)pkt_dev->sofar,
661 (unsigned long long)pkt_dev->errors);
664 " started: %lluus stopped: %lluus idle: %lluus\n",
665 (unsigned long long) ktime_to_us(pkt_dev->started_at),
666 (unsigned long long) ktime_to_us(stopped),
667 (unsigned long long) idle);
670 " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n",
671 pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset,
672 pkt_dev->cur_src_mac_offset);
674 if (pkt_dev->flags & F_IPV6) {
675 char b1[128], b2[128];
676 fmt_ip6(b1, pkt_dev->cur_in6_daddr.s6_addr);
677 fmt_ip6(b2, pkt_dev->cur_in6_saddr.s6_addr);
678 seq_printf(seq, " cur_saddr: %s cur_daddr: %s\n", b2, b1);
680 seq_printf(seq, " cur_saddr: 0x%x cur_daddr: 0x%x\n",
681 pkt_dev->cur_saddr, pkt_dev->cur_daddr);
683 seq_printf(seq, " cur_udp_dst: %d cur_udp_src: %d\n",
684 pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src);
686 seq_printf(seq, " cur_queue_map: %u\n", pkt_dev->cur_queue_map);
688 seq_printf(seq, " flows: %u\n", pkt_dev->nflows);
690 if (pkt_dev->result[0])
691 seq_printf(seq, "Result: %s\n", pkt_dev->result);
693 seq_printf(seq, "Result: Idle\n");
699 static int hex32_arg(const char __user *user_buffer, unsigned long maxlen, __u32 *num)
704 for (; i < maxlen; i++) {
707 if (get_user(c, &user_buffer[i]))
709 if ((c >= '0') && (c <= '9'))
711 else if ((c >= 'a') && (c <= 'f'))
712 *num |= c - 'a' + 10;
713 else if ((c >= 'A') && (c <= 'F'))
714 *num |= c - 'A' + 10;
721 static int count_trail_chars(const char __user * user_buffer,
726 for (i = 0; i < maxlen; i++) {
728 if (get_user(c, &user_buffer[i]))
746 static unsigned long num_arg(const char __user * user_buffer,
747 unsigned long maxlen, unsigned long *num)
752 for (; i < maxlen; i++) {
754 if (get_user(c, &user_buffer[i]))
756 if ((c >= '0') && (c <= '9')) {
765 static int strn_len(const char __user * user_buffer, unsigned int maxlen)
769 for (; i < maxlen; i++) {
771 if (get_user(c, &user_buffer[i]))
789 static ssize_t get_labels(const char __user *buffer, struct pktgen_dev *pkt_dev)
796 pkt_dev->nr_labels = 0;
799 len = hex32_arg(&buffer[i], 8, &tmp);
802 pkt_dev->labels[n] = htonl(tmp);
803 if (pkt_dev->labels[n] & MPLS_STACK_BOTTOM)
804 pkt_dev->flags |= F_MPLS_RND;
806 if (get_user(c, &buffer[i]))
810 if (n >= MAX_MPLS_LABELS)
814 pkt_dev->nr_labels = n;
818 static ssize_t pktgen_if_write(struct file *file,
819 const char __user * user_buffer, size_t count,
822 struct seq_file *seq = (struct seq_file *)file->private_data;
823 struct pktgen_dev *pkt_dev = seq->private;
825 char name[16], valstr[32];
826 unsigned long value = 0;
827 char *pg_result = NULL;
831 pg_result = &(pkt_dev->result[0]);
834 printk(KERN_WARNING "pktgen: wrong command format\n");
839 tmp = count_trail_chars(&user_buffer[i], max);
841 printk(KERN_WARNING "pktgen: illegal format\n");
846 /* Read variable name */
848 len = strn_len(&user_buffer[i], sizeof(name) - 1);
852 memset(name, 0, sizeof(name));
853 if (copy_from_user(name, &user_buffer[i], len))
858 len = count_trail_chars(&user_buffer[i], max);
866 if (copy_from_user(tb, user_buffer, count))
869 printk(KERN_DEBUG "pktgen: %s,%lu buffer -:%s:-\n", name,
870 (unsigned long)count, tb);
873 if (!strcmp(name, "min_pkt_size")) {
874 len = num_arg(&user_buffer[i], 10, &value);
879 if (value < 14 + 20 + 8)
881 if (value != pkt_dev->min_pkt_size) {
882 pkt_dev->min_pkt_size = value;
883 pkt_dev->cur_pkt_size = value;
885 sprintf(pg_result, "OK: min_pkt_size=%u",
886 pkt_dev->min_pkt_size);
890 if (!strcmp(name, "max_pkt_size")) {
891 len = num_arg(&user_buffer[i], 10, &value);
896 if (value < 14 + 20 + 8)
898 if (value != pkt_dev->max_pkt_size) {
899 pkt_dev->max_pkt_size = value;
900 pkt_dev->cur_pkt_size = value;
902 sprintf(pg_result, "OK: max_pkt_size=%u",
903 pkt_dev->max_pkt_size);
907 /* Shortcut for min = max */
909 if (!strcmp(name, "pkt_size")) {
910 len = num_arg(&user_buffer[i], 10, &value);
915 if (value < 14 + 20 + 8)
917 if (value != pkt_dev->min_pkt_size) {
918 pkt_dev->min_pkt_size = value;
919 pkt_dev->max_pkt_size = value;
920 pkt_dev->cur_pkt_size = value;
922 sprintf(pg_result, "OK: pkt_size=%u", pkt_dev->min_pkt_size);
926 if (!strcmp(name, "debug")) {
927 len = num_arg(&user_buffer[i], 10, &value);
933 sprintf(pg_result, "OK: debug=%u", debug);
937 if (!strcmp(name, "frags")) {
938 len = num_arg(&user_buffer[i], 10, &value);
943 pkt_dev->nfrags = value;
944 sprintf(pg_result, "OK: frags=%u", pkt_dev->nfrags);
947 if (!strcmp(name, "delay")) {
948 len = num_arg(&user_buffer[i], 10, &value);
953 if (value == 0x7FFFFFFF)
954 pkt_dev->delay = ULLONG_MAX;
956 pkt_dev->delay = (u64)value * NSEC_PER_USEC;
958 sprintf(pg_result, "OK: delay=%llu",
959 (unsigned long long) pkt_dev->delay);
962 if (!strcmp(name, "udp_src_min")) {
963 len = num_arg(&user_buffer[i], 10, &value);
968 if (value != pkt_dev->udp_src_min) {
969 pkt_dev->udp_src_min = value;
970 pkt_dev->cur_udp_src = value;
972 sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min);
975 if (!strcmp(name, "udp_dst_min")) {
976 len = num_arg(&user_buffer[i], 10, &value);
981 if (value != pkt_dev->udp_dst_min) {
982 pkt_dev->udp_dst_min = value;
983 pkt_dev->cur_udp_dst = value;
985 sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min);
988 if (!strcmp(name, "udp_src_max")) {
989 len = num_arg(&user_buffer[i], 10, &value);
994 if (value != pkt_dev->udp_src_max) {
995 pkt_dev->udp_src_max = value;
996 pkt_dev->cur_udp_src = value;
998 sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max);
1001 if (!strcmp(name, "udp_dst_max")) {
1002 len = num_arg(&user_buffer[i], 10, &value);
1007 if (value != pkt_dev->udp_dst_max) {
1008 pkt_dev->udp_dst_max = value;
1009 pkt_dev->cur_udp_dst = value;
1011 sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max);
1014 if (!strcmp(name, "clone_skb")) {
1015 len = num_arg(&user_buffer[i], 10, &value);
1020 pkt_dev->clone_skb = value;
1022 sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb);
1025 if (!strcmp(name, "count")) {
1026 len = num_arg(&user_buffer[i], 10, &value);
1031 pkt_dev->count = value;
1032 sprintf(pg_result, "OK: count=%llu",
1033 (unsigned long long)pkt_dev->count);
1036 if (!strcmp(name, "src_mac_count")) {
1037 len = num_arg(&user_buffer[i], 10, &value);
1042 if (pkt_dev->src_mac_count != value) {
1043 pkt_dev->src_mac_count = value;
1044 pkt_dev->cur_src_mac_offset = 0;
1046 sprintf(pg_result, "OK: src_mac_count=%d",
1047 pkt_dev->src_mac_count);
1050 if (!strcmp(name, "dst_mac_count")) {
1051 len = num_arg(&user_buffer[i], 10, &value);
1056 if (pkt_dev->dst_mac_count != value) {
1057 pkt_dev->dst_mac_count = value;
1058 pkt_dev->cur_dst_mac_offset = 0;
1060 sprintf(pg_result, "OK: dst_mac_count=%d",
1061 pkt_dev->dst_mac_count);
1064 if (!strcmp(name, "flag")) {
1067 len = strn_len(&user_buffer[i], sizeof(f) - 1);
1071 if (copy_from_user(f, &user_buffer[i], len))
1074 if (strcmp(f, "IPSRC_RND") == 0)
1075 pkt_dev->flags |= F_IPSRC_RND;
1077 else if (strcmp(f, "!IPSRC_RND") == 0)
1078 pkt_dev->flags &= ~F_IPSRC_RND;
1080 else if (strcmp(f, "TXSIZE_RND") == 0)
1081 pkt_dev->flags |= F_TXSIZE_RND;
1083 else if (strcmp(f, "!TXSIZE_RND") == 0)
1084 pkt_dev->flags &= ~F_TXSIZE_RND;
1086 else if (strcmp(f, "IPDST_RND") == 0)
1087 pkt_dev->flags |= F_IPDST_RND;
1089 else if (strcmp(f, "!IPDST_RND") == 0)
1090 pkt_dev->flags &= ~F_IPDST_RND;
1092 else if (strcmp(f, "UDPSRC_RND") == 0)
1093 pkt_dev->flags |= F_UDPSRC_RND;
1095 else if (strcmp(f, "!UDPSRC_RND") == 0)
1096 pkt_dev->flags &= ~F_UDPSRC_RND;
1098 else if (strcmp(f, "UDPDST_RND") == 0)
1099 pkt_dev->flags |= F_UDPDST_RND;
1101 else if (strcmp(f, "!UDPDST_RND") == 0)
1102 pkt_dev->flags &= ~F_UDPDST_RND;
1104 else if (strcmp(f, "MACSRC_RND") == 0)
1105 pkt_dev->flags |= F_MACSRC_RND;
1107 else if (strcmp(f, "!MACSRC_RND") == 0)
1108 pkt_dev->flags &= ~F_MACSRC_RND;
1110 else if (strcmp(f, "MACDST_RND") == 0)
1111 pkt_dev->flags |= F_MACDST_RND;
1113 else if (strcmp(f, "!MACDST_RND") == 0)
1114 pkt_dev->flags &= ~F_MACDST_RND;
1116 else if (strcmp(f, "MPLS_RND") == 0)
1117 pkt_dev->flags |= F_MPLS_RND;
1119 else if (strcmp(f, "!MPLS_RND") == 0)
1120 pkt_dev->flags &= ~F_MPLS_RND;
1122 else if (strcmp(f, "VID_RND") == 0)
1123 pkt_dev->flags |= F_VID_RND;
1125 else if (strcmp(f, "!VID_RND") == 0)
1126 pkt_dev->flags &= ~F_VID_RND;
1128 else if (strcmp(f, "SVID_RND") == 0)
1129 pkt_dev->flags |= F_SVID_RND;
1131 else if (strcmp(f, "!SVID_RND") == 0)
1132 pkt_dev->flags &= ~F_SVID_RND;
1134 else if (strcmp(f, "FLOW_SEQ") == 0)
1135 pkt_dev->flags |= F_FLOW_SEQ;
1137 else if (strcmp(f, "QUEUE_MAP_RND") == 0)
1138 pkt_dev->flags |= F_QUEUE_MAP_RND;
1140 else if (strcmp(f, "!QUEUE_MAP_RND") == 0)
1141 pkt_dev->flags &= ~F_QUEUE_MAP_RND;
1143 else if (strcmp(f, "QUEUE_MAP_CPU") == 0)
1144 pkt_dev->flags |= F_QUEUE_MAP_CPU;
1146 else if (strcmp(f, "!QUEUE_MAP_CPU") == 0)
1147 pkt_dev->flags &= ~F_QUEUE_MAP_CPU;
1149 else if (strcmp(f, "IPSEC") == 0)
1150 pkt_dev->flags |= F_IPSEC_ON;
1153 else if (strcmp(f, "!IPV6") == 0)
1154 pkt_dev->flags &= ~F_IPV6;
1158 "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
1160 "IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
1161 "MACSRC_RND, MACDST_RND, TXSIZE_RND, IPV6, MPLS_RND, VID_RND, SVID_RND, FLOW_SEQ, IPSEC\n");
1164 sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
1167 if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
1168 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
1173 if (copy_from_user(buf, &user_buffer[i], len))
1176 if (strcmp(buf, pkt_dev->dst_min) != 0) {
1177 memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
1178 strncpy(pkt_dev->dst_min, buf, len);
1179 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
1180 pkt_dev->cur_daddr = pkt_dev->daddr_min;
1183 printk(KERN_DEBUG "pktgen: dst_min set to: %s\n",
1186 sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min);
1189 if (!strcmp(name, "dst_max")) {
1190 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
1195 if (copy_from_user(buf, &user_buffer[i], len))
1199 if (strcmp(buf, pkt_dev->dst_max) != 0) {
1200 memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
1201 strncpy(pkt_dev->dst_max, buf, len);
1202 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
1203 pkt_dev->cur_daddr = pkt_dev->daddr_max;
1206 printk(KERN_DEBUG "pktgen: dst_max set to: %s\n",
1209 sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max);
1212 if (!strcmp(name, "dst6")) {
1213 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1217 pkt_dev->flags |= F_IPV6;
1219 if (copy_from_user(buf, &user_buffer[i], len))
1223 scan_ip6(buf, pkt_dev->in6_daddr.s6_addr);
1224 fmt_ip6(buf, pkt_dev->in6_daddr.s6_addr);
1226 ipv6_addr_copy(&pkt_dev->cur_in6_daddr, &pkt_dev->in6_daddr);
1229 printk(KERN_DEBUG "pktgen: dst6 set to: %s\n", buf);
1232 sprintf(pg_result, "OK: dst6=%s", buf);
1235 if (!strcmp(name, "dst6_min")) {
1236 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1240 pkt_dev->flags |= F_IPV6;
1242 if (copy_from_user(buf, &user_buffer[i], len))
1246 scan_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
1247 fmt_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
1249 ipv6_addr_copy(&pkt_dev->cur_in6_daddr,
1250 &pkt_dev->min_in6_daddr);
1252 printk(KERN_DEBUG "pktgen: dst6_min set to: %s\n", buf);
1255 sprintf(pg_result, "OK: dst6_min=%s", buf);
1258 if (!strcmp(name, "dst6_max")) {
1259 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1263 pkt_dev->flags |= F_IPV6;
1265 if (copy_from_user(buf, &user_buffer[i], len))
1269 scan_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
1270 fmt_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
1273 printk(KERN_DEBUG "pktgen: dst6_max set to: %s\n", buf);
1276 sprintf(pg_result, "OK: dst6_max=%s", buf);
1279 if (!strcmp(name, "src6")) {
1280 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1284 pkt_dev->flags |= F_IPV6;
1286 if (copy_from_user(buf, &user_buffer[i], len))
1290 scan_ip6(buf, pkt_dev->in6_saddr.s6_addr);
1291 fmt_ip6(buf, pkt_dev->in6_saddr.s6_addr);
1293 ipv6_addr_copy(&pkt_dev->cur_in6_saddr, &pkt_dev->in6_saddr);
1296 printk(KERN_DEBUG "pktgen: src6 set to: %s\n", buf);
1299 sprintf(pg_result, "OK: src6=%s", buf);
1302 if (!strcmp(name, "src_min")) {
1303 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
1307 if (copy_from_user(buf, &user_buffer[i], len))
1310 if (strcmp(buf, pkt_dev->src_min) != 0) {
1311 memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
1312 strncpy(pkt_dev->src_min, buf, len);
1313 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
1314 pkt_dev->cur_saddr = pkt_dev->saddr_min;
1317 printk(KERN_DEBUG "pktgen: src_min set to: %s\n",
1320 sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min);
1323 if (!strcmp(name, "src_max")) {
1324 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
1328 if (copy_from_user(buf, &user_buffer[i], len))
1331 if (strcmp(buf, pkt_dev->src_max) != 0) {
1332 memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
1333 strncpy(pkt_dev->src_max, buf, len);
1334 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
1335 pkt_dev->cur_saddr = pkt_dev->saddr_max;
1338 printk(KERN_DEBUG "pktgen: src_max set to: %s\n",
1341 sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max);
1344 if (!strcmp(name, "dst_mac")) {
1346 unsigned char old_dmac[ETH_ALEN];
1347 unsigned char *m = pkt_dev->dst_mac;
1348 memcpy(old_dmac, pkt_dev->dst_mac, ETH_ALEN);
1350 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
1354 memset(valstr, 0, sizeof(valstr));
1355 if (copy_from_user(valstr, &user_buffer[i], len))
1359 for (*m = 0; *v && m < pkt_dev->dst_mac + 6; v++) {
1360 if (*v >= '0' && *v <= '9') {
1364 if (*v >= 'A' && *v <= 'F') {
1366 *m += *v - 'A' + 10;
1368 if (*v >= 'a' && *v <= 'f') {
1370 *m += *v - 'a' + 10;
1378 /* Set up Dest MAC */
1379 if (compare_ether_addr(old_dmac, pkt_dev->dst_mac))
1380 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
1382 sprintf(pg_result, "OK: dstmac");
1385 if (!strcmp(name, "src_mac")) {
1387 unsigned char old_smac[ETH_ALEN];
1388 unsigned char *m = pkt_dev->src_mac;
1390 memcpy(old_smac, pkt_dev->src_mac, ETH_ALEN);
1392 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
1396 memset(valstr, 0, sizeof(valstr));
1397 if (copy_from_user(valstr, &user_buffer[i], len))
1401 for (*m = 0; *v && m < pkt_dev->src_mac + 6; v++) {
1402 if (*v >= '0' && *v <= '9') {
1406 if (*v >= 'A' && *v <= 'F') {
1408 *m += *v - 'A' + 10;
1410 if (*v >= 'a' && *v <= 'f') {
1412 *m += *v - 'a' + 10;
1420 /* Set up Src MAC */
1421 if (compare_ether_addr(old_smac, pkt_dev->src_mac))
1422 memcpy(&(pkt_dev->hh[6]), pkt_dev->src_mac, ETH_ALEN);
1424 sprintf(pg_result, "OK: srcmac");
1428 if (!strcmp(name, "clear_counters")) {
1429 pktgen_clear_counters(pkt_dev);
1430 sprintf(pg_result, "OK: Clearing counters.\n");
1434 if (!strcmp(name, "flows")) {
1435 len = num_arg(&user_buffer[i], 10, &value);
1440 if (value > MAX_CFLOWS)
1443 pkt_dev->cflows = value;
1444 sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
1448 if (!strcmp(name, "flowlen")) {
1449 len = num_arg(&user_buffer[i], 10, &value);
1454 pkt_dev->lflow = value;
1455 sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
1459 if (!strcmp(name, "queue_map_min")) {
1460 len = num_arg(&user_buffer[i], 5, &value);
1465 pkt_dev->queue_map_min = value;
1466 sprintf(pg_result, "OK: queue_map_min=%u", pkt_dev->queue_map_min);
1470 if (!strcmp(name, "queue_map_max")) {
1471 len = num_arg(&user_buffer[i], 5, &value);
1476 pkt_dev->queue_map_max = value;
1477 sprintf(pg_result, "OK: queue_map_max=%u", pkt_dev->queue_map_max);
1481 if (!strcmp(name, "mpls")) {
1484 len = get_labels(&user_buffer[i], pkt_dev);
1488 cnt = sprintf(pg_result, "OK: mpls=");
1489 for (n = 0; n < pkt_dev->nr_labels; n++)
1490 cnt += sprintf(pg_result + cnt,
1491 "%08x%s", ntohl(pkt_dev->labels[n]),
1492 n == pkt_dev->nr_labels-1 ? "" : ",");
1494 if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) {
1495 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1496 pkt_dev->svlan_id = 0xffff;
1499 printk(KERN_DEBUG "pktgen: VLAN/SVLAN auto turned off\n");
1504 if (!strcmp(name, "vlan_id")) {
1505 len = num_arg(&user_buffer[i], 4, &value);
1510 if (value <= 4095) {
1511 pkt_dev->vlan_id = value; /* turn on VLAN */
1514 printk(KERN_DEBUG "pktgen: VLAN turned on\n");
1516 if (debug && pkt_dev->nr_labels)
1517 printk(KERN_DEBUG "pktgen: MPLS auto turned off\n");
1519 pkt_dev->nr_labels = 0; /* turn off MPLS */
1520 sprintf(pg_result, "OK: vlan_id=%u", pkt_dev->vlan_id);
1522 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1523 pkt_dev->svlan_id = 0xffff;
1526 printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n");
1531 if (!strcmp(name, "vlan_p")) {
1532 len = num_arg(&user_buffer[i], 1, &value);
1537 if ((value <= 7) && (pkt_dev->vlan_id != 0xffff)) {
1538 pkt_dev->vlan_p = value;
1539 sprintf(pg_result, "OK: vlan_p=%u", pkt_dev->vlan_p);
1541 sprintf(pg_result, "ERROR: vlan_p must be 0-7");
1546 if (!strcmp(name, "vlan_cfi")) {
1547 len = num_arg(&user_buffer[i], 1, &value);
1552 if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) {
1553 pkt_dev->vlan_cfi = value;
1554 sprintf(pg_result, "OK: vlan_cfi=%u", pkt_dev->vlan_cfi);
1556 sprintf(pg_result, "ERROR: vlan_cfi must be 0-1");
1561 if (!strcmp(name, "svlan_id")) {
1562 len = num_arg(&user_buffer[i], 4, &value);
1567 if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) {
1568 pkt_dev->svlan_id = value; /* turn on SVLAN */
1571 printk(KERN_DEBUG "pktgen: SVLAN turned on\n");
1573 if (debug && pkt_dev->nr_labels)
1574 printk(KERN_DEBUG "pktgen: MPLS auto turned off\n");
1576 pkt_dev->nr_labels = 0; /* turn off MPLS */
1577 sprintf(pg_result, "OK: svlan_id=%u", pkt_dev->svlan_id);
1579 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1580 pkt_dev->svlan_id = 0xffff;
1583 printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n");
1588 if (!strcmp(name, "svlan_p")) {
1589 len = num_arg(&user_buffer[i], 1, &value);
1594 if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) {
1595 pkt_dev->svlan_p = value;
1596 sprintf(pg_result, "OK: svlan_p=%u", pkt_dev->svlan_p);
1598 sprintf(pg_result, "ERROR: svlan_p must be 0-7");
1603 if (!strcmp(name, "svlan_cfi")) {
1604 len = num_arg(&user_buffer[i], 1, &value);
1609 if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) {
1610 pkt_dev->svlan_cfi = value;
1611 sprintf(pg_result, "OK: svlan_cfi=%u", pkt_dev->svlan_cfi);
1613 sprintf(pg_result, "ERROR: svlan_cfi must be 0-1");
1618 if (!strcmp(name, "tos")) {
1619 __u32 tmp_value = 0;
1620 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1626 pkt_dev->tos = tmp_value;
1627 sprintf(pg_result, "OK: tos=0x%02x", pkt_dev->tos);
1629 sprintf(pg_result, "ERROR: tos must be 00-ff");
1634 if (!strcmp(name, "traffic_class")) {
1635 __u32 tmp_value = 0;
1636 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1642 pkt_dev->traffic_class = tmp_value;
1643 sprintf(pg_result, "OK: traffic_class=0x%02x", pkt_dev->traffic_class);
1645 sprintf(pg_result, "ERROR: traffic_class must be 00-ff");
1650 sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
1654 static int pktgen_if_open(struct inode *inode, struct file *file)
1656 return single_open(file, pktgen_if_show, PDE(inode)->data);
1659 static const struct file_operations pktgen_if_fops = {
1660 .owner = THIS_MODULE,
1661 .open = pktgen_if_open,
1663 .llseek = seq_lseek,
1664 .write = pktgen_if_write,
1665 .release = single_release,
1668 static int pktgen_thread_show(struct seq_file *seq, void *v)
1670 struct pktgen_thread *t = seq->private;
1671 const struct pktgen_dev *pkt_dev;
1675 seq_printf(seq, "Running: ");
1678 list_for_each_entry(pkt_dev, &t->if_list, list)
1679 if (pkt_dev->running)
1680 seq_printf(seq, "%s ", pkt_dev->odev->name);
1682 seq_printf(seq, "\nStopped: ");
1684 list_for_each_entry(pkt_dev, &t->if_list, list)
1685 if (!pkt_dev->running)
1686 seq_printf(seq, "%s ", pkt_dev->odev->name);
1689 seq_printf(seq, "\nResult: %s\n", t->result);
1691 seq_printf(seq, "\nResult: NA\n");
1698 static ssize_t pktgen_thread_write(struct file *file,
1699 const char __user * user_buffer,
1700 size_t count, loff_t * offset)
1702 struct seq_file *seq = (struct seq_file *)file->private_data;
1703 struct pktgen_thread *t = seq->private;
1704 int i = 0, max, len, ret;
1709 // sprintf(pg_result, "Wrong command format");
1714 len = count_trail_chars(&user_buffer[i], max);
1720 /* Read variable name */
1722 len = strn_len(&user_buffer[i], sizeof(name) - 1);
1726 memset(name, 0, sizeof(name));
1727 if (copy_from_user(name, &user_buffer[i], len))
1732 len = count_trail_chars(&user_buffer[i], max);
1739 printk(KERN_DEBUG "pktgen: t=%s, count=%lu\n",
1740 name, (unsigned long)count);
1743 printk(KERN_ERR "pktgen: ERROR: No thread\n");
1748 pg_result = &(t->result[0]);
1750 if (!strcmp(name, "add_device")) {
1753 len = strn_len(&user_buffer[i], sizeof(f) - 1);
1758 if (copy_from_user(f, &user_buffer[i], len))
1761 mutex_lock(&pktgen_thread_lock);
1762 pktgen_add_device(t, f);
1763 mutex_unlock(&pktgen_thread_lock);
1765 sprintf(pg_result, "OK: add_device=%s", f);
1769 if (!strcmp(name, "rem_device_all")) {
1770 mutex_lock(&pktgen_thread_lock);
1771 t->control |= T_REMDEVALL;
1772 mutex_unlock(&pktgen_thread_lock);
1773 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
1775 sprintf(pg_result, "OK: rem_device_all");
1779 if (!strcmp(name, "max_before_softirq")) {
1780 sprintf(pg_result, "OK: Note! max_before_softirq is obsoleted -- Do not use");
1790 static int pktgen_thread_open(struct inode *inode, struct file *file)
1792 return single_open(file, pktgen_thread_show, PDE(inode)->data);
1795 static const struct file_operations pktgen_thread_fops = {
1796 .owner = THIS_MODULE,
1797 .open = pktgen_thread_open,
1799 .llseek = seq_lseek,
1800 .write = pktgen_thread_write,
1801 .release = single_release,
1804 /* Think find or remove for NN */
1805 static struct pktgen_dev *__pktgen_NN_threads(const char *ifname, int remove)
1807 struct pktgen_thread *t;
1808 struct pktgen_dev *pkt_dev = NULL;
1810 list_for_each_entry(t, &pktgen_threads, th_list) {
1811 pkt_dev = pktgen_find_dev(t, ifname);
1815 pkt_dev->removal_mark = 1;
1816 t->control |= T_REMDEV;
1826 * mark a device for removal
1828 static void pktgen_mark_device(const char *ifname)
1830 struct pktgen_dev *pkt_dev = NULL;
1831 const int max_tries = 10, msec_per_try = 125;
1834 mutex_lock(&pktgen_thread_lock);
1835 pr_debug("pktgen: pktgen_mark_device marking %s for removal\n", ifname);
1839 pkt_dev = __pktgen_NN_threads(ifname, REMOVE);
1840 if (pkt_dev == NULL)
1841 break; /* success */
1843 mutex_unlock(&pktgen_thread_lock);
1844 pr_debug("pktgen: pktgen_mark_device waiting for %s "
1845 "to disappear....\n", ifname);
1846 schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
1847 mutex_lock(&pktgen_thread_lock);
1849 if (++i >= max_tries) {
1850 printk(KERN_ERR "pktgen_mark_device: timed out after "
1851 "waiting %d msec for device %s to be removed\n",
1852 msec_per_try * i, ifname);
1858 mutex_unlock(&pktgen_thread_lock);
1861 static void pktgen_change_name(struct net_device *dev)
1863 struct pktgen_thread *t;
1865 list_for_each_entry(t, &pktgen_threads, th_list) {
1866 struct pktgen_dev *pkt_dev;
1868 list_for_each_entry(pkt_dev, &t->if_list, list) {
1869 if (pkt_dev->odev != dev)
1872 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
1874 pkt_dev->entry = create_proc_entry(dev->name, 0600,
1876 if (!pkt_dev->entry)
1877 printk(KERN_ERR "pktgen: can't move proc "
1878 " entry for '%s'\n", dev->name);
1884 static int pktgen_device_event(struct notifier_block *unused,
1885 unsigned long event, void *ptr)
1887 struct net_device *dev = ptr;
1889 if (!net_eq(dev_net(dev), &init_net))
1892 /* It is OK that we do not hold the group lock right now,
1893 * as we run under the RTNL lock.
1897 case NETDEV_CHANGENAME:
1898 pktgen_change_name(dev);
1901 case NETDEV_UNREGISTER:
1902 pktgen_mark_device(dev->name);
1909 static struct net_device *pktgen_dev_get_by_name(struct pktgen_dev *pkt_dev, const char *ifname)
1914 for(i=0; ifname[i] != '@'; i++) {
1922 return dev_get_by_name(&init_net, b);
1926 /* Associate pktgen_dev with a device. */
1928 static int pktgen_setup_dev(struct pktgen_dev *pkt_dev, const char *ifname)
1930 struct net_device *odev;
1933 /* Clean old setups */
1934 if (pkt_dev->odev) {
1935 dev_put(pkt_dev->odev);
1936 pkt_dev->odev = NULL;
1939 odev = pktgen_dev_get_by_name(pkt_dev, ifname);
1941 printk(KERN_ERR "pktgen: no such netdevice: \"%s\"\n", ifname);
1945 if (odev->type != ARPHRD_ETHER) {
1946 printk(KERN_ERR "pktgen: not an ethernet device: \"%s\"\n", ifname);
1948 } else if (!netif_running(odev)) {
1949 printk(KERN_ERR "pktgen: device is down: \"%s\"\n", ifname);
1952 pkt_dev->odev = odev;
1960 /* Read pkt_dev from the interface and set up internal pktgen_dev
1961 * structure to have the right information to create/send packets
1963 static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
1967 if (!pkt_dev->odev) {
1968 printk(KERN_ERR "pktgen: ERROR: pkt_dev->odev == NULL in "
1970 sprintf(pkt_dev->result,
1971 "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
1975 /* make sure that we don't pick a non-existing transmit queue */
1976 ntxq = pkt_dev->odev->real_num_tx_queues;
1978 if (ntxq <= pkt_dev->queue_map_min) {
1979 printk(KERN_WARNING "pktgen: WARNING: Requested "
1980 "queue_map_min (zero-based) (%d) exceeds valid range "
1981 "[0 - %d] for (%d) queues on %s, resetting\n",
1982 pkt_dev->queue_map_min, (ntxq ?: 1)- 1, ntxq,
1983 pkt_dev->odev->name);
1984 pkt_dev->queue_map_min = ntxq - 1;
1986 if (pkt_dev->queue_map_max >= ntxq) {
1987 printk(KERN_WARNING "pktgen: WARNING: Requested "
1988 "queue_map_max (zero-based) (%d) exceeds valid range "
1989 "[0 - %d] for (%d) queues on %s, resetting\n",
1990 pkt_dev->queue_map_max, (ntxq ?: 1)- 1, ntxq,
1991 pkt_dev->odev->name);
1992 pkt_dev->queue_map_max = ntxq - 1;
1995 /* Default to the interface's mac if not explicitly set. */
1997 if (is_zero_ether_addr(pkt_dev->src_mac))
1998 memcpy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr, ETH_ALEN);
2000 /* Set up Dest MAC */
2001 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
2003 /* Set up pkt size */
2004 pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
2006 if (pkt_dev->flags & F_IPV6) {
2008 * Skip this automatic address setting until locks or functions
2013 int i, set = 0, err = 1;
2014 struct inet6_dev *idev;
2016 for (i = 0; i < IN6_ADDR_HSIZE; i++)
2017 if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
2025 * Use linklevel address if unconfigured.
2027 * use ipv6_get_lladdr if/when it's get exported
2031 if ((idev = __in6_dev_get(pkt_dev->odev)) != NULL) {
2032 struct inet6_ifaddr *ifp;
2034 read_lock_bh(&idev->lock);
2035 for (ifp = idev->addr_list; ifp;
2036 ifp = ifp->if_next) {
2037 if (ifp->scope == IFA_LINK
2039 flags & IFA_F_TENTATIVE)) {
2040 ipv6_addr_copy(&pkt_dev->
2047 read_unlock_bh(&idev->lock);
2051 printk(KERN_ERR "pktgen: ERROR: IPv6 link "
2052 "address not availble.\n");
2056 pkt_dev->saddr_min = 0;
2057 pkt_dev->saddr_max = 0;
2058 if (strlen(pkt_dev->src_min) == 0) {
2060 struct in_device *in_dev;
2063 in_dev = __in_dev_get_rcu(pkt_dev->odev);
2065 if (in_dev->ifa_list) {
2066 pkt_dev->saddr_min =
2067 in_dev->ifa_list->ifa_address;
2068 pkt_dev->saddr_max = pkt_dev->saddr_min;
2073 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
2074 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
2077 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
2078 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
2080 /* Initialize current values. */
2081 pkt_dev->cur_dst_mac_offset = 0;
2082 pkt_dev->cur_src_mac_offset = 0;
2083 pkt_dev->cur_saddr = pkt_dev->saddr_min;
2084 pkt_dev->cur_daddr = pkt_dev->daddr_min;
2085 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2086 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
2087 pkt_dev->nflows = 0;
2091 static void spin(struct pktgen_dev *pkt_dev, ktime_t spin_until)
2095 struct hrtimer_sleeper t;
2097 hrtimer_init_on_stack(&t.timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
2098 hrtimer_set_expires(&t.timer, spin_until);
2100 remaining = ktime_to_us(hrtimer_expires_remaining(&t.timer));
2104 start = ktime_now();
2105 if (remaining < 100)
2106 udelay(remaining); /* really small just spin */
2108 /* see do_nanosleep */
2109 hrtimer_init_sleeper(&t, current);
2111 set_current_state(TASK_INTERRUPTIBLE);
2112 hrtimer_start_expires(&t.timer, HRTIMER_MODE_ABS);
2113 if (!hrtimer_active(&t.timer))
2119 hrtimer_cancel(&t.timer);
2120 } while (t.task && pkt_dev->running && !signal_pending(current));
2121 __set_current_state(TASK_RUNNING);
2123 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_now(), start));
2126 static inline void set_pkt_overhead(struct pktgen_dev *pkt_dev)
2128 pkt_dev->pkt_overhead = 0;
2129 pkt_dev->pkt_overhead += pkt_dev->nr_labels*sizeof(u32);
2130 pkt_dev->pkt_overhead += VLAN_TAG_SIZE(pkt_dev);
2131 pkt_dev->pkt_overhead += SVLAN_TAG_SIZE(pkt_dev);
2134 static inline int f_seen(const struct pktgen_dev *pkt_dev, int flow)
2136 return !!(pkt_dev->flows[flow].flags & F_INIT);
2139 static inline int f_pick(struct pktgen_dev *pkt_dev)
2141 int flow = pkt_dev->curfl;
2143 if (pkt_dev->flags & F_FLOW_SEQ) {
2144 if (pkt_dev->flows[flow].count >= pkt_dev->lflow) {
2146 pkt_dev->flows[flow].count = 0;
2147 pkt_dev->flows[flow].flags = 0;
2148 pkt_dev->curfl += 1;
2149 if (pkt_dev->curfl >= pkt_dev->cflows)
2150 pkt_dev->curfl = 0; /*reset */
2153 flow = random32() % pkt_dev->cflows;
2154 pkt_dev->curfl = flow;
2156 if (pkt_dev->flows[flow].count > pkt_dev->lflow) {
2157 pkt_dev->flows[flow].count = 0;
2158 pkt_dev->flows[flow].flags = 0;
2162 return pkt_dev->curfl;
2167 /* If there was already an IPSEC SA, we keep it as is, else
2168 * we go look for it ...
2170 static void get_ipsec_sa(struct pktgen_dev *pkt_dev, int flow)
2172 struct xfrm_state *x = pkt_dev->flows[flow].x;
2174 /*slow path: we dont already have xfrm_state*/
2175 x = xfrm_stateonly_find(&init_net,
2176 (xfrm_address_t *)&pkt_dev->cur_daddr,
2177 (xfrm_address_t *)&pkt_dev->cur_saddr,
2180 pkt_dev->ipsproto, 0);
2182 pkt_dev->flows[flow].x = x;
2183 set_pkt_overhead(pkt_dev);
2184 pkt_dev->pkt_overhead+=x->props.header_len;
2190 static void set_cur_queue_map(struct pktgen_dev *pkt_dev)
2193 if (pkt_dev->flags & F_QUEUE_MAP_CPU)
2194 pkt_dev->cur_queue_map = smp_processor_id();
2196 else if (pkt_dev->queue_map_min < pkt_dev->queue_map_max) {
2198 if (pkt_dev->flags & F_QUEUE_MAP_RND) {
2200 (pkt_dev->queue_map_max -
2201 pkt_dev->queue_map_min + 1)
2202 + pkt_dev->queue_map_min;
2204 t = pkt_dev->cur_queue_map + 1;
2205 if (t > pkt_dev->queue_map_max)
2206 t = pkt_dev->queue_map_min;
2208 pkt_dev->cur_queue_map = t;
2210 pkt_dev->cur_queue_map = pkt_dev->cur_queue_map % pkt_dev->odev->real_num_tx_queues;
2213 /* Increment/randomize headers according to flags and current values
2214 * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
2216 static void mod_cur_headers(struct pktgen_dev *pkt_dev)
2222 if (pkt_dev->cflows)
2223 flow = f_pick(pkt_dev);
2225 /* Deal with source MAC */
2226 if (pkt_dev->src_mac_count > 1) {
2230 if (pkt_dev->flags & F_MACSRC_RND)
2231 mc = random32() % pkt_dev->src_mac_count;
2233 mc = pkt_dev->cur_src_mac_offset++;
2234 if (pkt_dev->cur_src_mac_offset >=
2235 pkt_dev->src_mac_count)
2236 pkt_dev->cur_src_mac_offset = 0;
2239 tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
2240 pkt_dev->hh[11] = tmp;
2241 tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2242 pkt_dev->hh[10] = tmp;
2243 tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2244 pkt_dev->hh[9] = tmp;
2245 tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2246 pkt_dev->hh[8] = tmp;
2247 tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
2248 pkt_dev->hh[7] = tmp;
2251 /* Deal with Destination MAC */
2252 if (pkt_dev->dst_mac_count > 1) {
2256 if (pkt_dev->flags & F_MACDST_RND)
2257 mc = random32() % pkt_dev->dst_mac_count;
2260 mc = pkt_dev->cur_dst_mac_offset++;
2261 if (pkt_dev->cur_dst_mac_offset >=
2262 pkt_dev->dst_mac_count) {
2263 pkt_dev->cur_dst_mac_offset = 0;
2267 tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
2268 pkt_dev->hh[5] = tmp;
2269 tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2270 pkt_dev->hh[4] = tmp;
2271 tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2272 pkt_dev->hh[3] = tmp;
2273 tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2274 pkt_dev->hh[2] = tmp;
2275 tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
2276 pkt_dev->hh[1] = tmp;
2279 if (pkt_dev->flags & F_MPLS_RND) {
2281 for (i = 0; i < pkt_dev->nr_labels; i++)
2282 if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
2283 pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
2284 ((__force __be32)random32() &
2288 if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
2289 pkt_dev->vlan_id = random32() & (4096-1);
2292 if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
2293 pkt_dev->svlan_id = random32() & (4096 - 1);
2296 if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
2297 if (pkt_dev->flags & F_UDPSRC_RND)
2298 pkt_dev->cur_udp_src = random32() %
2299 (pkt_dev->udp_src_max - pkt_dev->udp_src_min)
2300 + pkt_dev->udp_src_min;
2303 pkt_dev->cur_udp_src++;
2304 if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
2305 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
2309 if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
2310 if (pkt_dev->flags & F_UDPDST_RND) {
2311 pkt_dev->cur_udp_dst = random32() %
2312 (pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)
2313 + pkt_dev->udp_dst_min;
2315 pkt_dev->cur_udp_dst++;
2316 if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
2317 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2321 if (!(pkt_dev->flags & F_IPV6)) {
2323 if ((imn = ntohl(pkt_dev->saddr_min)) < (imx =
2327 if (pkt_dev->flags & F_IPSRC_RND)
2328 t = random32() % (imx - imn) + imn;
2330 t = ntohl(pkt_dev->cur_saddr);
2336 pkt_dev->cur_saddr = htonl(t);
2339 if (pkt_dev->cflows && f_seen(pkt_dev, flow)) {
2340 pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
2342 imn = ntohl(pkt_dev->daddr_min);
2343 imx = ntohl(pkt_dev->daddr_max);
2347 if (pkt_dev->flags & F_IPDST_RND) {
2349 t = random32() % (imx - imn) + imn;
2352 while (ipv4_is_loopback(s) ||
2353 ipv4_is_multicast(s) ||
2354 ipv4_is_lbcast(s) ||
2355 ipv4_is_zeronet(s) ||
2356 ipv4_is_local_multicast(s)) {
2357 t = random32() % (imx - imn) + imn;
2360 pkt_dev->cur_daddr = s;
2362 t = ntohl(pkt_dev->cur_daddr);
2367 pkt_dev->cur_daddr = htonl(t);
2370 if (pkt_dev->cflows) {
2371 pkt_dev->flows[flow].flags |= F_INIT;
2372 pkt_dev->flows[flow].cur_daddr =
2375 if (pkt_dev->flags & F_IPSEC_ON)
2376 get_ipsec_sa(pkt_dev, flow);
2381 } else { /* IPV6 * */
2383 if (pkt_dev->min_in6_daddr.s6_addr32[0] == 0 &&
2384 pkt_dev->min_in6_daddr.s6_addr32[1] == 0 &&
2385 pkt_dev->min_in6_daddr.s6_addr32[2] == 0 &&
2386 pkt_dev->min_in6_daddr.s6_addr32[3] == 0) ;
2390 /* Only random destinations yet */
2392 for (i = 0; i < 4; i++) {
2393 pkt_dev->cur_in6_daddr.s6_addr32[i] =
2394 (((__force __be32)random32() |
2395 pkt_dev->min_in6_daddr.s6_addr32[i]) &
2396 pkt_dev->max_in6_daddr.s6_addr32[i]);
2401 if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
2403 if (pkt_dev->flags & F_TXSIZE_RND) {
2405 (pkt_dev->max_pkt_size - pkt_dev->min_pkt_size)
2406 + pkt_dev->min_pkt_size;
2408 t = pkt_dev->cur_pkt_size + 1;
2409 if (t > pkt_dev->max_pkt_size)
2410 t = pkt_dev->min_pkt_size;
2412 pkt_dev->cur_pkt_size = t;
2415 set_cur_queue_map(pkt_dev);
2417 pkt_dev->flows[flow].count++;
2422 static int pktgen_output_ipsec(struct sk_buff *skb, struct pktgen_dev *pkt_dev)
2424 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2430 /* XXX: we dont support tunnel mode for now until
2431 * we resolve the dst issue */
2432 if (x->props.mode != XFRM_MODE_TRANSPORT)
2435 spin_lock(&x->lock);
2438 err = x->outer_mode->output(x, skb);
2441 err = x->type->output(x, skb);
2445 x->curlft.bytes +=skb->len;
2446 x->curlft.packets++;
2448 spin_unlock(&x->lock);
2452 static void free_SAs(struct pktgen_dev *pkt_dev)
2454 if (pkt_dev->cflows) {
2455 /* let go of the SAs if we have them */
2457 for (; i < pkt_dev->cflows; i++) {
2458 struct xfrm_state *x = pkt_dev->flows[i].x;
2461 pkt_dev->flows[i].x = NULL;
2467 static int process_ipsec(struct pktgen_dev *pkt_dev,
2468 struct sk_buff *skb, __be16 protocol)
2470 if (pkt_dev->flags & F_IPSEC_ON) {
2471 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2476 nhead = x->props.header_len - skb_headroom(skb);
2478 ret = pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
2480 printk(KERN_ERR "Error expanding "
2481 "ipsec packet %d\n",ret);
2486 /* ipsec is not expecting ll header */
2487 skb_pull(skb, ETH_HLEN);
2488 ret = pktgen_output_ipsec(skb, pkt_dev);
2490 printk(KERN_ERR "Error creating ipsec "
2495 eth = (__u8 *) skb_push(skb, ETH_HLEN);
2496 memcpy(eth, pkt_dev->hh, 12);
2497 *(u16 *) & eth[12] = protocol;
2507 static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
2510 for (i = 0; i < pkt_dev->nr_labels; i++) {
2511 *mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
2514 *mpls |= MPLS_STACK_BOTTOM;
2517 static inline __be16 build_tci(unsigned int id, unsigned int cfi,
2520 return htons(id | (cfi << 12) | (prio << 13));
2523 static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
2524 struct pktgen_dev *pkt_dev)
2526 struct sk_buff *skb = NULL;
2528 struct udphdr *udph;
2531 struct pktgen_hdr *pgh = NULL;
2532 __be16 protocol = htons(ETH_P_IP);
2534 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2535 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2536 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2537 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2540 if (pkt_dev->nr_labels)
2541 protocol = htons(ETH_P_MPLS_UC);
2543 if (pkt_dev->vlan_id != 0xffff)
2544 protocol = htons(ETH_P_8021Q);
2546 /* Update any of the values, used when we're incrementing various
2549 queue_map = pkt_dev->cur_queue_map;
2550 mod_cur_headers(pkt_dev);
2552 datalen = (odev->hard_header_len + 16) & ~0xf;
2553 skb = __netdev_alloc_skb(odev,
2554 pkt_dev->cur_pkt_size + 64
2555 + datalen + pkt_dev->pkt_overhead, GFP_NOWAIT);
2557 sprintf(pkt_dev->result, "No memory");
2561 skb_reserve(skb, datalen);
2563 /* Reserve for ethernet and IP header */
2564 eth = (__u8 *) skb_push(skb, 14);
2565 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2566 if (pkt_dev->nr_labels)
2567 mpls_push(mpls, pkt_dev);
2569 if (pkt_dev->vlan_id != 0xffff) {
2570 if (pkt_dev->svlan_id != 0xffff) {
2571 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2572 *svlan_tci = build_tci(pkt_dev->svlan_id,
2575 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2576 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
2578 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2579 *vlan_tci = build_tci(pkt_dev->vlan_id,
2582 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2583 *vlan_encapsulated_proto = htons(ETH_P_IP);
2586 skb->network_header = skb->tail;
2587 skb->transport_header = skb->network_header + sizeof(struct iphdr);
2588 skb_put(skb, sizeof(struct iphdr) + sizeof(struct udphdr));
2589 skb_set_queue_mapping(skb, queue_map);
2591 udph = udp_hdr(skb);
2593 memcpy(eth, pkt_dev->hh, 12);
2594 *(__be16 *) & eth[12] = protocol;
2596 /* Eth + IPh + UDPh + mpls */
2597 datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
2598 pkt_dev->pkt_overhead;
2599 if (datalen < sizeof(struct pktgen_hdr))
2600 datalen = sizeof(struct pktgen_hdr);
2602 udph->source = htons(pkt_dev->cur_udp_src);
2603 udph->dest = htons(pkt_dev->cur_udp_dst);
2604 udph->len = htons(datalen + 8); /* DATA + udphdr */
2605 udph->check = 0; /* No checksum */
2610 iph->tos = pkt_dev->tos;
2611 iph->protocol = IPPROTO_UDP; /* UDP */
2612 iph->saddr = pkt_dev->cur_saddr;
2613 iph->daddr = pkt_dev->cur_daddr;
2615 iplen = 20 + 8 + datalen;
2616 iph->tot_len = htons(iplen);
2618 iph->check = ip_fast_csum((void *)iph, iph->ihl);
2619 skb->protocol = protocol;
2620 skb->mac_header = (skb->network_header - ETH_HLEN -
2621 pkt_dev->pkt_overhead);
2623 skb->pkt_type = PACKET_HOST;
2625 if (pkt_dev->nfrags <= 0)
2626 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
2628 int frags = pkt_dev->nfrags;
2631 pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
2633 if (frags > MAX_SKB_FRAGS)
2634 frags = MAX_SKB_FRAGS;
2635 if (datalen > frags * PAGE_SIZE) {
2636 skb_put(skb, datalen - frags * PAGE_SIZE);
2637 datalen = frags * PAGE_SIZE;
2641 while (datalen > 0) {
2642 struct page *page = alloc_pages(GFP_KERNEL, 0);
2643 skb_shinfo(skb)->frags[i].page = page;
2644 skb_shinfo(skb)->frags[i].page_offset = 0;
2645 skb_shinfo(skb)->frags[i].size =
2646 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
2647 datalen -= skb_shinfo(skb)->frags[i].size;
2648 skb->len += skb_shinfo(skb)->frags[i].size;
2649 skb->data_len += skb_shinfo(skb)->frags[i].size;
2651 skb_shinfo(skb)->nr_frags = i;
2660 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
2664 skb_shinfo(skb)->frags[i - 1].size -= rem;
2666 skb_shinfo(skb)->frags[i] =
2667 skb_shinfo(skb)->frags[i - 1];
2668 get_page(skb_shinfo(skb)->frags[i].page);
2669 skb_shinfo(skb)->frags[i].page =
2670 skb_shinfo(skb)->frags[i - 1].page;
2671 skb_shinfo(skb)->frags[i].page_offset +=
2672 skb_shinfo(skb)->frags[i - 1].size;
2673 skb_shinfo(skb)->frags[i].size = rem;
2675 skb_shinfo(skb)->nr_frags = i;
2679 /* Stamp the time, and sequence number, convert them to network byte order */
2682 struct timeval timestamp;
2684 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2685 pgh->seq_num = htonl(pkt_dev->seq_num);
2687 do_gettimeofday(×tamp);
2688 pgh->tv_sec = htonl(timestamp.tv_sec);
2689 pgh->tv_usec = htonl(timestamp.tv_usec);
2693 if (!process_ipsec(pkt_dev, skb, protocol))
2701 * scan_ip6, fmt_ip taken from dietlibc-0.21
2702 * Author Felix von Leitner <felix-dietlibc@fefe.de>
2704 * Slightly modified for kernel.
2705 * Should be candidate for net/ipv4/utils.c
2709 static unsigned int scan_ip6(const char *s, char ip[16])
2712 unsigned int len = 0;
2715 unsigned int prefixlen = 0;
2716 unsigned int suffixlen = 0;
2720 for (i = 0; i < 16; i++)
2726 if (s[1] == ':') { /* Found "::", skip to part 2 */
2734 u = simple_strtoul(s, &pos, 16);
2738 if (prefixlen == 12 && s[i] == '.') {
2740 /* the last 4 bytes may be written as IPv4 address */
2743 memcpy((struct in_addr *)(ip + 12), &tmp, sizeof(tmp));
2746 ip[prefixlen++] = (u >> 8);
2747 ip[prefixlen++] = (u & 255);
2750 if (prefixlen == 16)
2754 /* part 2, after "::" */
2761 } else if (suffixlen != 0)
2764 u = simple_strtol(s, &pos, 16);
2771 if (suffixlen + prefixlen <= 12 && s[i] == '.') {
2773 memcpy((struct in_addr *)(suffix + suffixlen), &tmp,
2779 suffix[suffixlen++] = (u >> 8);
2780 suffix[suffixlen++] = (u & 255);
2783 if (prefixlen + suffixlen == 16)
2786 for (i = 0; i < suffixlen; i++)
2787 ip[16 - suffixlen + i] = suffix[i];
2791 static char tohex(char hexdigit)
2793 return hexdigit > 9 ? hexdigit + 'a' - 10 : hexdigit + '0';
2796 static int fmt_xlong(char *s, unsigned int i)
2799 *s = tohex((i >> 12) & 0xf);
2800 if (s != bak || *s != '0')
2802 *s = tohex((i >> 8) & 0xf);
2803 if (s != bak || *s != '0')
2805 *s = tohex((i >> 4) & 0xf);
2806 if (s != bak || *s != '0')
2808 *s = tohex(i & 0xf);
2812 static unsigned int fmt_ip6(char *s, const char ip[16])
2817 unsigned int compressing;
2822 for (j = 0; j < 16; j += 2) {
2824 #ifdef V4MAPPEDPREFIX
2825 if (j == 12 && !memcmp(ip, V4mappedprefix, 12)) {
2826 inet_ntoa_r(*(struct in_addr *)(ip + 12), s);
2831 temp = ((unsigned long)(unsigned char)ip[j] << 8) +
2832 (unsigned long)(unsigned char)ip[j + 1];
2847 i = fmt_xlong(s, temp);
2864 static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
2865 struct pktgen_dev *pkt_dev)
2867 struct sk_buff *skb = NULL;
2869 struct udphdr *udph;
2871 struct ipv6hdr *iph;
2872 struct pktgen_hdr *pgh = NULL;
2873 __be16 protocol = htons(ETH_P_IPV6);
2875 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2876 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2877 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2878 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2881 if (pkt_dev->nr_labels)
2882 protocol = htons(ETH_P_MPLS_UC);
2884 if (pkt_dev->vlan_id != 0xffff)
2885 protocol = htons(ETH_P_8021Q);
2887 /* Update any of the values, used when we're incrementing various
2890 queue_map = pkt_dev->cur_queue_map;
2891 mod_cur_headers(pkt_dev);
2893 skb = __netdev_alloc_skb(odev,
2894 pkt_dev->cur_pkt_size + 64
2895 + 16 + pkt_dev->pkt_overhead, GFP_NOWAIT);
2897 sprintf(pkt_dev->result, "No memory");
2901 skb_reserve(skb, 16);
2903 /* Reserve for ethernet and IP header */
2904 eth = (__u8 *) skb_push(skb, 14);
2905 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2906 if (pkt_dev->nr_labels)
2907 mpls_push(mpls, pkt_dev);
2909 if (pkt_dev->vlan_id != 0xffff) {
2910 if (pkt_dev->svlan_id != 0xffff) {
2911 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2912 *svlan_tci = build_tci(pkt_dev->svlan_id,
2915 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2916 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
2918 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2919 *vlan_tci = build_tci(pkt_dev->vlan_id,
2922 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2923 *vlan_encapsulated_proto = htons(ETH_P_IPV6);
2926 skb->network_header = skb->tail;
2927 skb->transport_header = skb->network_header + sizeof(struct ipv6hdr);
2928 skb_put(skb, sizeof(struct ipv6hdr) + sizeof(struct udphdr));
2929 skb_set_queue_mapping(skb, queue_map);
2930 iph = ipv6_hdr(skb);
2931 udph = udp_hdr(skb);
2933 memcpy(eth, pkt_dev->hh, 12);
2934 *(__be16 *) & eth[12] = protocol;
2936 /* Eth + IPh + UDPh + mpls */
2937 datalen = pkt_dev->cur_pkt_size - 14 -
2938 sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
2939 pkt_dev->pkt_overhead;
2941 if (datalen < sizeof(struct pktgen_hdr)) {
2942 datalen = sizeof(struct pktgen_hdr);
2943 if (net_ratelimit())
2944 printk(KERN_INFO "pktgen: increased datalen to %d\n",
2948 udph->source = htons(pkt_dev->cur_udp_src);
2949 udph->dest = htons(pkt_dev->cur_udp_dst);
2950 udph->len = htons(datalen + sizeof(struct udphdr));
2951 udph->check = 0; /* No checksum */
2953 *(__be32 *) iph = htonl(0x60000000); /* Version + flow */
2955 if (pkt_dev->traffic_class) {
2956 /* Version + traffic class + flow (0) */
2957 *(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
2960 iph->hop_limit = 32;
2962 iph->payload_len = htons(sizeof(struct udphdr) + datalen);
2963 iph->nexthdr = IPPROTO_UDP;
2965 ipv6_addr_copy(&iph->daddr, &pkt_dev->cur_in6_daddr);
2966 ipv6_addr_copy(&iph->saddr, &pkt_dev->cur_in6_saddr);
2968 skb->mac_header = (skb->network_header - ETH_HLEN -
2969 pkt_dev->pkt_overhead);
2970 skb->protocol = protocol;
2972 skb->pkt_type = PACKET_HOST;
2974 if (pkt_dev->nfrags <= 0)
2975 pgh = (struct pktgen_hdr *)skb_put(skb, datalen);
2977 int frags = pkt_dev->nfrags;
2980 pgh = (struct pktgen_hdr *)(((char *)(udph)) + 8);
2982 if (frags > MAX_SKB_FRAGS)
2983 frags = MAX_SKB_FRAGS;
2984 if (datalen > frags * PAGE_SIZE) {
2985 skb_put(skb, datalen - frags * PAGE_SIZE);
2986 datalen = frags * PAGE_SIZE;
2990 while (datalen > 0) {
2991 struct page *page = alloc_pages(GFP_KERNEL, 0);
2992 skb_shinfo(skb)->frags[i].page = page;
2993 skb_shinfo(skb)->frags[i].page_offset = 0;
2994 skb_shinfo(skb)->frags[i].size =
2995 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
2996 datalen -= skb_shinfo(skb)->frags[i].size;
2997 skb->len += skb_shinfo(skb)->frags[i].size;
2998 skb->data_len += skb_shinfo(skb)->frags[i].size;
3000 skb_shinfo(skb)->nr_frags = i;
3009 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
3013 skb_shinfo(skb)->frags[i - 1].size -= rem;
3015 skb_shinfo(skb)->frags[i] =
3016 skb_shinfo(skb)->frags[i - 1];
3017 get_page(skb_shinfo(skb)->frags[i].page);
3018 skb_shinfo(skb)->frags[i].page =
3019 skb_shinfo(skb)->frags[i - 1].page;
3020 skb_shinfo(skb)->frags[i].page_offset +=
3021 skb_shinfo(skb)->frags[i - 1].size;
3022 skb_shinfo(skb)->frags[i].size = rem;
3024 skb_shinfo(skb)->nr_frags = i;
3028 /* Stamp the time, and sequence number, convert them to network byte order */
3029 /* should we update cloned packets too ? */
3031 struct timeval timestamp;
3033 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
3034 pgh->seq_num = htonl(pkt_dev->seq_num);
3036 do_gettimeofday(×tamp);
3037 pgh->tv_sec = htonl(timestamp.tv_sec);
3038 pgh->tv_usec = htonl(timestamp.tv_usec);
3040 /* pkt_dev->seq_num++; FF: you really mean this? */
3045 static struct sk_buff *fill_packet(struct net_device *odev,
3046 struct pktgen_dev *pkt_dev)
3048 if (pkt_dev->flags & F_IPV6)
3049 return fill_packet_ipv6(odev, pkt_dev);
3051 return fill_packet_ipv4(odev, pkt_dev);
3054 static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
3056 pkt_dev->seq_num = 1;
3057 pkt_dev->idle_acc = 0;
3059 pkt_dev->tx_bytes = 0;
3060 pkt_dev->errors = 0;
3063 /* Set up structure for sending pkts, clear counters */
3065 static void pktgen_run(struct pktgen_thread *t)
3067 struct pktgen_dev *pkt_dev;
3070 pr_debug("pktgen: entering pktgen_run. %p\n", t);
3073 list_for_each_entry(pkt_dev, &t->if_list, list) {
3076 * setup odev and create initial packet.
3078 pktgen_setup_inject(pkt_dev);
3080 if (pkt_dev->odev) {
3081 pktgen_clear_counters(pkt_dev);
3082 pkt_dev->running = 1; /* Cranke yeself! */
3083 pkt_dev->skb = NULL;
3084 pkt_dev->started_at =
3085 pkt_dev->next_tx = ktime_now();
3087 set_pkt_overhead(pkt_dev);
3089 strcpy(pkt_dev->result, "Starting");
3092 strcpy(pkt_dev->result, "Error starting");
3096 t->control &= ~(T_STOP);
3099 static void pktgen_stop_all_threads_ifs(void)
3101 struct pktgen_thread *t;
3103 pr_debug("pktgen: entering pktgen_stop_all_threads_ifs.\n");
3105 mutex_lock(&pktgen_thread_lock);
3107 list_for_each_entry(t, &pktgen_threads, th_list)
3108 t->control |= T_STOP;
3110 mutex_unlock(&pktgen_thread_lock);
3113 static int thread_is_running(const struct pktgen_thread *t)
3115 const struct pktgen_dev *pkt_dev;
3117 list_for_each_entry(pkt_dev, &t->if_list, list)
3118 if (pkt_dev->running)
3123 static int pktgen_wait_thread_run(struct pktgen_thread *t)
3127 while (thread_is_running(t)) {
3131 msleep_interruptible(100);
3133 if (signal_pending(current))
3143 static int pktgen_wait_all_threads_run(void)
3145 struct pktgen_thread *t;
3148 mutex_lock(&pktgen_thread_lock);
3150 list_for_each_entry(t, &pktgen_threads, th_list) {
3151 sig = pktgen_wait_thread_run(t);
3157 list_for_each_entry(t, &pktgen_threads, th_list)
3158 t->control |= (T_STOP);
3160 mutex_unlock(&pktgen_thread_lock);
3164 static void pktgen_run_all_threads(void)
3166 struct pktgen_thread *t;
3168 pr_debug("pktgen: entering pktgen_run_all_threads.\n");
3170 mutex_lock(&pktgen_thread_lock);
3172 list_for_each_entry(t, &pktgen_threads, th_list)
3173 t->control |= (T_RUN);
3175 mutex_unlock(&pktgen_thread_lock);
3177 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
3179 pktgen_wait_all_threads_run();
3182 static void pktgen_reset_all_threads(void)
3184 struct pktgen_thread *t;
3186 pr_debug("pktgen: entering pktgen_reset_all_threads.\n");
3188 mutex_lock(&pktgen_thread_lock);
3190 list_for_each_entry(t, &pktgen_threads, th_list)
3191 t->control |= (T_REMDEVALL);
3193 mutex_unlock(&pktgen_thread_lock);
3195 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
3197 pktgen_wait_all_threads_run();
3200 static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
3202 __u64 bps, mbps, pps;
3203 char *p = pkt_dev->result;
3204 ktime_t elapsed = ktime_sub(pkt_dev->stopped_at,
3205 pkt_dev->started_at);
3206 ktime_t idle = ns_to_ktime(pkt_dev->idle_acc);
3208 p += sprintf(p, "OK: %llu(c%llu+d%llu) nsec, %llu (%dbyte,%dfrags)\n",
3209 (unsigned long long)ktime_to_us(elapsed),
3210 (unsigned long long)ktime_to_us(ktime_sub(elapsed, idle)),
3211 (unsigned long long)ktime_to_us(idle),
3212 (unsigned long long)pkt_dev->sofar,
3213 pkt_dev->cur_pkt_size, nr_frags);
3215 pps = div64_u64(pkt_dev->sofar * NSEC_PER_SEC,
3216 ktime_to_ns(elapsed));
3218 bps = pps * 8 * pkt_dev->cur_pkt_size;
3221 do_div(mbps, 1000000);
3222 p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu",
3223 (unsigned long long)pps,
3224 (unsigned long long)mbps,
3225 (unsigned long long)bps,
3226 (unsigned long long)pkt_dev->errors);
3229 /* Set stopped-at timer, remove from running list, do counters & statistics */
3230 static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
3232 int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
3234 if (!pkt_dev->running) {
3235 printk(KERN_WARNING "pktgen: interface: %s is already "
3236 "stopped\n", pkt_dev->odev->name);
3240 kfree_skb(pkt_dev->skb);
3241 pkt_dev->skb = NULL;
3242 pkt_dev->stopped_at = ktime_now();
3243 pkt_dev->running = 0;
3245 show_results(pkt_dev, nr_frags);
3250 static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
3252 struct pktgen_dev *pkt_dev, *best = NULL;
3256 list_for_each_entry(pkt_dev, &t->if_list, list) {
3257 if (!pkt_dev->running)
3261 else if (ktime_lt(pkt_dev->next_tx, best->next_tx))
3268 static void pktgen_stop(struct pktgen_thread *t)
3270 struct pktgen_dev *pkt_dev;
3272 pr_debug("pktgen: entering pktgen_stop\n");
3276 list_for_each_entry(pkt_dev, &t->if_list, list) {
3277 pktgen_stop_device(pkt_dev);
3284 * one of our devices needs to be removed - find it
3287 static void pktgen_rem_one_if(struct pktgen_thread *t)
3289 struct list_head *q, *n;
3290 struct pktgen_dev *cur;
3292 pr_debug("pktgen: entering pktgen_rem_one_if\n");
3296 list_for_each_safe(q, n, &t->if_list) {
3297 cur = list_entry(q, struct pktgen_dev, list);
3299 if (!cur->removal_mark)
3302 kfree_skb(cur->skb);
3305 pktgen_remove_device(t, cur);
3313 static void pktgen_rem_all_ifs(struct pktgen_thread *t)
3315 struct list_head *q, *n;
3316 struct pktgen_dev *cur;
3318 /* Remove all devices, free mem */
3320 pr_debug("pktgen: entering pktgen_rem_all_ifs\n");
3323 list_for_each_safe(q, n, &t->if_list) {
3324 cur = list_entry(q, struct pktgen_dev, list);
3326 kfree_skb(cur->skb);
3329 pktgen_remove_device(t, cur);
3335 static void pktgen_rem_thread(struct pktgen_thread *t)
3337 /* Remove from the thread list */
3339 remove_proc_entry(t->tsk->comm, pg_proc_dir);
3341 mutex_lock(&pktgen_thread_lock);
3343 list_del(&t->th_list);
3345 mutex_unlock(&pktgen_thread_lock);
3348 static void idle(struct pktgen_dev *pkt_dev)
3350 ktime_t idle_start = ktime_now();
3357 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_now(), idle_start));
3361 static void pktgen_xmit(struct pktgen_dev *pkt_dev)
3363 struct net_device *odev = pkt_dev->odev;
3364 int (*xmit)(struct sk_buff *, struct net_device *)
3365 = odev->netdev_ops->ndo_start_xmit;
3366 struct netdev_queue *txq;
3370 if (pkt_dev->delay) {
3371 spin(pkt_dev, pkt_dev->next_tx);
3373 /* This is max DELAY, this has special meaning of
3376 if (pkt_dev->delay == ULLONG_MAX) {
3377 pkt_dev->next_tx = ktime_add_ns(ktime_now(), ULONG_MAX);
3382 if (!pkt_dev->skb) {
3383 set_cur_queue_map(pkt_dev);
3384 queue_map = pkt_dev->cur_queue_map;
3386 queue_map = skb_get_queue_mapping(pkt_dev->skb);
3389 txq = netdev_get_tx_queue(odev, queue_map);
3390 /* Did we saturate the queue already? */
3391 if (netif_tx_queue_stopped(txq) || netif_tx_queue_frozen(txq)) {
3392 /* If device is down, then all queues are permnantly frozen */
3393 if (netif_running(odev))
3396 pktgen_stop_device(pkt_dev);
3400 if (!pkt_dev->skb || (pkt_dev->last_ok &&
3401 ++pkt_dev->clone_count >= pkt_dev->clone_skb)) {
3402 /* build a new pkt */
3403 kfree_skb(pkt_dev->skb);
3405 pkt_dev->skb = fill_packet(odev, pkt_dev);
3406 if (pkt_dev->skb == NULL) {
3407 printk(KERN_ERR "pktgen: ERROR: couldn't "
3408 "allocate skb in fill_packet.\n");
3410 pkt_dev->clone_count--; /* back out increment, OOM */
3414 pkt_dev->allocated_skbs++;
3415 pkt_dev->clone_count = 0; /* reset counter */
3418 /* fill_packet() might have changed the queue */
3419 queue_map = skb_get_queue_mapping(pkt_dev->skb);
3420 txq = netdev_get_tx_queue(odev, queue_map);
3422 __netif_tx_lock_bh(txq);
3423 if (unlikely(netif_tx_queue_stopped(txq) || netif_tx_queue_frozen(txq)))
3424 pkt_dev->last_ok = 0;
3426 atomic_inc(&(pkt_dev->skb->users));
3429 ret = (*xmit)(pkt_dev->skb, odev);
3432 txq_trans_update(txq);
3433 pkt_dev->last_ok = 1;
3436 pkt_dev->tx_bytes += pkt_dev->cur_pkt_size;
3438 case NETDEV_TX_LOCKED:
3441 default: /* Drivers are not supposed to return other values! */
3442 if (net_ratelimit())
3443 pr_info("pktgen: %s xmit error: %d\n",
3447 case NETDEV_TX_BUSY:
3448 /* Retry it next time */
3449 atomic_dec(&(pkt_dev->skb->users));
3450 pkt_dev->last_ok = 0;
3454 pkt_dev->next_tx = ktime_add_ns(ktime_now(),
3457 __netif_tx_unlock_bh(txq);
3459 /* If pkt_dev->count is zero, then run forever */
3460 if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
3461 if (atomic_read(&(pkt_dev->skb->users)) != 1) {
3462 ktime_t idle_start = ktime_now();
3463 while (atomic_read(&(pkt_dev->skb->users)) != 1) {
3464 if (signal_pending(current)) {
3469 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_now(),
3473 /* Done with this */
3474 pktgen_stop_device(pkt_dev);
3479 * Main loop of the thread goes here
3482 static int pktgen_thread_worker(void *arg)
3485 struct pktgen_thread *t = arg;
3486 struct pktgen_dev *pkt_dev = NULL;
3489 BUG_ON(smp_processor_id() != cpu);
3491 init_waitqueue_head(&t->queue);
3492 complete(&t->start_done);
3494 pr_debug("pktgen: starting pktgen/%d: pid=%d\n", cpu, task_pid_nr(current));
3496 set_current_state(TASK_INTERRUPTIBLE);
3500 while (!kthread_should_stop()) {
3501 pkt_dev = next_to_run(t);
3504 (t->control & (T_STOP | T_RUN | T_REMDEVALL | T_REMDEV))
3506 prepare_to_wait(&(t->queue), &wait,
3507 TASK_INTERRUPTIBLE);
3508 schedule_timeout(HZ / 10);
3509 finish_wait(&(t->queue), &wait);
3512 __set_current_state(TASK_RUNNING);
3515 pktgen_xmit(pkt_dev);
3517 if (t->control & T_STOP) {
3519 t->control &= ~(T_STOP);
3522 if (t->control & T_RUN) {
3524 t->control &= ~(T_RUN);
3527 if (t->control & T_REMDEVALL) {
3528 pktgen_rem_all_ifs(t);
3529 t->control &= ~(T_REMDEVALL);
3532 if (t->control & T_REMDEV) {
3533 pktgen_rem_one_if(t);
3534 t->control &= ~(T_REMDEV);
3539 set_current_state(TASK_INTERRUPTIBLE);
3542 pr_debug("pktgen: %s stopping all device\n", t->tsk->comm);
3545 pr_debug("pktgen: %s removing all device\n", t->tsk->comm);
3546 pktgen_rem_all_ifs(t);
3548 pr_debug("pktgen: %s removing thread.\n", t->tsk->comm);
3549 pktgen_rem_thread(t);
3554 static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
3557 struct pktgen_dev *p, *pkt_dev = NULL;
3560 list_for_each_entry(p, &t->if_list, list)
3561 if (strncmp(p->odev->name, ifname, IFNAMSIZ) == 0) {
3567 pr_debug("pktgen: find_dev(%s) returning %p\n", ifname, pkt_dev);
3572 * Adds a dev at front of if_list.
3575 static int add_dev_to_thread(struct pktgen_thread *t,
3576 struct pktgen_dev *pkt_dev)
3582 if (pkt_dev->pg_thread) {
3583 printk(KERN_ERR "pktgen: ERROR: already assigned "
3589 list_add(&pkt_dev->list, &t->if_list);
3590 pkt_dev->pg_thread = t;
3591 pkt_dev->running = 0;
3598 /* Called under thread lock */
3600 static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
3602 struct pktgen_dev *pkt_dev;
3605 /* We don't allow a device to be on several threads */
3607 pkt_dev = __pktgen_NN_threads(ifname, FIND);
3609 printk(KERN_ERR "pktgen: ERROR: interface already used.\n");
3613 pkt_dev = kzalloc(sizeof(struct pktgen_dev), GFP_KERNEL);
3617 pkt_dev->flows = vmalloc(MAX_CFLOWS * sizeof(struct flow_state));
3618 if (pkt_dev->flows == NULL) {
3622 memset(pkt_dev->flows, 0, MAX_CFLOWS * sizeof(struct flow_state));
3624 pkt_dev->removal_mark = 0;
3625 pkt_dev->min_pkt_size = ETH_ZLEN;
3626 pkt_dev->max_pkt_size = ETH_ZLEN;
3627 pkt_dev->nfrags = 0;
3628 pkt_dev->clone_skb = pg_clone_skb_d;
3629 pkt_dev->delay = pg_delay_d;
3630 pkt_dev->count = pg_count_d;
3632 pkt_dev->udp_src_min = 9; /* sink port */
3633 pkt_dev->udp_src_max = 9;
3634 pkt_dev->udp_dst_min = 9;
3635 pkt_dev->udp_dst_max = 9;
3637 pkt_dev->vlan_p = 0;
3638 pkt_dev->vlan_cfi = 0;
3639 pkt_dev->vlan_id = 0xffff;
3640 pkt_dev->svlan_p = 0;
3641 pkt_dev->svlan_cfi = 0;
3642 pkt_dev->svlan_id = 0xffff;
3644 err = pktgen_setup_dev(pkt_dev, ifname);
3648 pkt_dev->entry = proc_create_data(ifname, 0600, pg_proc_dir,
3649 &pktgen_if_fops, pkt_dev);
3650 if (!pkt_dev->entry) {
3651 printk(KERN_ERR "pktgen: cannot create %s/%s procfs entry.\n",
3652 PG_PROC_DIR, ifname);
3657 pkt_dev->ipsmode = XFRM_MODE_TRANSPORT;
3658 pkt_dev->ipsproto = IPPROTO_ESP;
3661 return add_dev_to_thread(t, pkt_dev);
3663 dev_put(pkt_dev->odev);
3668 vfree(pkt_dev->flows);
3673 static int __init pktgen_create_thread(int cpu)
3675 struct pktgen_thread *t;
3676 struct proc_dir_entry *pe;
3677 struct task_struct *p;
3679 t = kzalloc(sizeof(struct pktgen_thread), GFP_KERNEL);
3681 printk(KERN_ERR "pktgen: ERROR: out of memory, can't "
3682 "create new thread.\n");
3686 spin_lock_init(&t->if_lock);
3689 INIT_LIST_HEAD(&t->if_list);
3691 list_add_tail(&t->th_list, &pktgen_threads);
3692 init_completion(&t->start_done);
3694 p = kthread_create(pktgen_thread_worker, t, "kpktgend_%d", cpu);
3696 printk(KERN_ERR "pktgen: kernel_thread() failed "
3697 "for cpu %d\n", t->cpu);
3698 list_del(&t->th_list);
3702 kthread_bind(p, cpu);
3705 pe = proc_create_data(t->tsk->comm, 0600, pg_proc_dir,
3706 &pktgen_thread_fops, t);
3708 printk(KERN_ERR "pktgen: cannot create %s/%s procfs entry.\n",
3709 PG_PROC_DIR, t->tsk->comm);
3711 list_del(&t->th_list);
3717 wait_for_completion(&t->start_done);
3723 * Removes a device from the thread if_list.
3725 static void _rem_dev_from_if_list(struct pktgen_thread *t,
3726 struct pktgen_dev *pkt_dev)
3728 struct list_head *q, *n;
3729 struct pktgen_dev *p;
3731 list_for_each_safe(q, n, &t->if_list) {
3732 p = list_entry(q, struct pktgen_dev, list);
3738 static int pktgen_remove_device(struct pktgen_thread *t,
3739 struct pktgen_dev *pkt_dev)
3742 pr_debug("pktgen: remove_device pkt_dev=%p\n", pkt_dev);
3744 if (pkt_dev->running) {
3745 printk(KERN_WARNING "pktgen: WARNING: trying to remove a "
3746 "running interface, stopping it now.\n");
3747 pktgen_stop_device(pkt_dev);
3750 /* Dis-associate from the interface */
3752 if (pkt_dev->odev) {
3753 dev_put(pkt_dev->odev);
3754 pkt_dev->odev = NULL;
3757 /* And update the thread if_list */
3759 _rem_dev_from_if_list(t, pkt_dev);
3762 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
3767 vfree(pkt_dev->flows);
3772 static int __init pg_init(void)
3775 struct proc_dir_entry *pe;
3777 printk(KERN_INFO "%s", version);
3779 pg_proc_dir = proc_mkdir(PG_PROC_DIR, init_net.proc_net);
3783 pe = proc_create(PGCTRL, 0600, pg_proc_dir, &pktgen_fops);
3785 printk(KERN_ERR "pktgen: ERROR: cannot create %s "
3786 "procfs entry.\n", PGCTRL);
3787 proc_net_remove(&init_net, PG_PROC_DIR);
3791 /* Register us to receive netdevice events */
3792 register_netdevice_notifier(&pktgen_notifier_block);
3794 for_each_online_cpu(cpu) {
3797 err = pktgen_create_thread(cpu);
3799 printk(KERN_WARNING "pktgen: WARNING: Cannot create "
3800 "thread for cpu %d (%d)\n", cpu, err);
3803 if (list_empty(&pktgen_threads)) {
3804 printk(KERN_ERR "pktgen: ERROR: Initialization failed for "
3806 unregister_netdevice_notifier(&pktgen_notifier_block);
3807 remove_proc_entry(PGCTRL, pg_proc_dir);
3808 proc_net_remove(&init_net, PG_PROC_DIR);
3815 static void __exit pg_cleanup(void)
3817 struct pktgen_thread *t;
3818 struct list_head *q, *n;
3819 wait_queue_head_t queue;
3820 init_waitqueue_head(&queue);
3822 /* Stop all interfaces & threads */
3824 list_for_each_safe(q, n, &pktgen_threads) {
3825 t = list_entry(q, struct pktgen_thread, th_list);
3826 kthread_stop(t->tsk);
3830 /* Un-register us from receiving netdevice events */
3831 unregister_netdevice_notifier(&pktgen_notifier_block);
3833 /* Clean up proc file system */
3834 remove_proc_entry(PGCTRL, pg_proc_dir);
3835 proc_net_remove(&init_net, PG_PROC_DIR);
3838 module_init(pg_init);
3839 module_exit(pg_cleanup);
3841 MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se");
3842 MODULE_DESCRIPTION("Packet Generator tool");
3843 MODULE_LICENSE("GPL");
3844 module_param(pg_count_d, int, 0);
3845 module_param(pg_delay_d, int, 0);
3846 module_param(pg_clone_skb_d, int, 0);
3847 module_param(debug, int, 0);