Add linux-next specific files for 20110426
[linux-2.6/next.git] / net / core / pktgen.c
blob2fa6fee1b46f428b21a753d320033babe3e9e943
1 /*
2 * Authors:
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
52 * clones.
54 * Also moved to /proc/net/pktgen/
55 * --ro
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.
67 * The new operation:
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
73 * into this too.
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.
82 * --ro
84 * Fix refcount off by one if first packet fails, potential null deref,
85 * memleak 030710- KJP
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>
108 * 050103
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>
119 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
121 #include <linux/sys.h>
122 #include <linux/types.h>
123 #include <linux/module.h>
124 #include <linux/moduleparam.h>
125 #include <linux/kernel.h>
126 #include <linux/mutex.h>
127 #include <linux/sched.h>
128 #include <linux/slab.h>
129 #include <linux/vmalloc.h>
130 #include <linux/unistd.h>
131 #include <linux/string.h>
132 #include <linux/ptrace.h>
133 #include <linux/errno.h>
134 #include <linux/ioport.h>
135 #include <linux/interrupt.h>
136 #include <linux/capability.h>
137 #include <linux/hrtimer.h>
138 #include <linux/freezer.h>
139 #include <linux/delay.h>
140 #include <linux/timer.h>
141 #include <linux/list.h>
142 #include <linux/init.h>
143 #include <linux/skbuff.h>
144 #include <linux/netdevice.h>
145 #include <linux/inet.h>
146 #include <linux/inetdevice.h>
147 #include <linux/rtnetlink.h>
148 #include <linux/if_arp.h>
149 #include <linux/if_vlan.h>
150 #include <linux/in.h>
151 #include <linux/ip.h>
152 #include <linux/ipv6.h>
153 #include <linux/udp.h>
154 #include <linux/proc_fs.h>
155 #include <linux/seq_file.h>
156 #include <linux/wait.h>
157 #include <linux/etherdevice.h>
158 #include <linux/kthread.h>
159 #include <net/net_namespace.h>
160 #include <net/checksum.h>
161 #include <net/ipv6.h>
162 #include <net/addrconf.h>
163 #ifdef CONFIG_XFRM
164 #include <net/xfrm.h>
165 #endif
166 #include <asm/byteorder.h>
167 #include <linux/rcupdate.h>
168 #include <linux/bitops.h>
169 #include <linux/io.h>
170 #include <linux/timex.h>
171 #include <linux/uaccess.h>
172 #include <asm/dma.h>
173 #include <asm/div64.h> /* do_div */
175 #define VERSION "2.74"
176 #define IP_NAME_SZ 32
177 #define MAX_MPLS_LABELS 16 /* This is the max label stack depth */
178 #define MPLS_STACK_BOTTOM htonl(0x00000100)
180 #define func_enter() pr_debug("entering %s\n", __func__);
182 /* Device flag bits */
183 #define F_IPSRC_RND (1<<0) /* IP-Src Random */
184 #define F_IPDST_RND (1<<1) /* IP-Dst Random */
185 #define F_UDPSRC_RND (1<<2) /* UDP-Src Random */
186 #define F_UDPDST_RND (1<<3) /* UDP-Dst Random */
187 #define F_MACSRC_RND (1<<4) /* MAC-Src Random */
188 #define F_MACDST_RND (1<<5) /* MAC-Dst Random */
189 #define F_TXSIZE_RND (1<<6) /* Transmit size is random */
190 #define F_IPV6 (1<<7) /* Interface in IPV6 Mode */
191 #define F_MPLS_RND (1<<8) /* Random MPLS labels */
192 #define F_VID_RND (1<<9) /* Random VLAN ID */
193 #define F_SVID_RND (1<<10) /* Random SVLAN ID */
194 #define F_FLOW_SEQ (1<<11) /* Sequential flows */
195 #define F_IPSEC_ON (1<<12) /* ipsec on for flows */
196 #define F_QUEUE_MAP_RND (1<<13) /* queue map Random */
197 #define F_QUEUE_MAP_CPU (1<<14) /* queue map mirrors smp_processor_id() */
198 #define F_NODE (1<<15) /* Node memory alloc*/
200 /* Thread control flag bits */
201 #define T_STOP (1<<0) /* Stop run */
202 #define T_RUN (1<<1) /* Start run */
203 #define T_REMDEVALL (1<<2) /* Remove all devs */
204 #define T_REMDEV (1<<3) /* Remove one dev */
206 /* If lock -- can be removed after some work */
207 #define if_lock(t) spin_lock(&(t->if_lock));
208 #define if_unlock(t) spin_unlock(&(t->if_lock));
210 /* Used to help with determining the pkts on receive */
211 #define PKTGEN_MAGIC 0xbe9be955
212 #define PG_PROC_DIR "pktgen"
213 #define PGCTRL "pgctrl"
214 static struct proc_dir_entry *pg_proc_dir;
216 #define MAX_CFLOWS 65536
218 #define VLAN_TAG_SIZE(x) ((x)->vlan_id == 0xffff ? 0 : 4)
219 #define SVLAN_TAG_SIZE(x) ((x)->svlan_id == 0xffff ? 0 : 4)
221 struct flow_state {
222 __be32 cur_daddr;
223 int count;
224 #ifdef CONFIG_XFRM
225 struct xfrm_state *x;
226 #endif
227 __u32 flags;
230 /* flow flag bits */
231 #define F_INIT (1<<0) /* flow has been initialized */
233 struct pktgen_dev {
235 * Try to keep frequent/infrequent used vars. separated.
237 struct proc_dir_entry *entry; /* proc file */
238 struct pktgen_thread *pg_thread;/* the owner */
239 struct list_head list; /* chaining in the thread's run-queue */
241 int running; /* if false, the test will stop */
243 /* If min != max, then we will either do a linear iteration, or
244 * we will do a random selection from within the range.
246 __u32 flags;
247 int removal_mark; /* non-zero => the device is marked for
248 * removal by worker thread */
250 int min_pkt_size; /* = ETH_ZLEN; */
251 int max_pkt_size; /* = ETH_ZLEN; */
252 int pkt_overhead; /* overhead for MPLS, VLANs, IPSEC etc */
253 int nfrags;
254 struct page *page;
255 u64 delay; /* nano-seconds */
257 __u64 count; /* Default No packets to send */
258 __u64 sofar; /* How many pkts we've sent so far */
259 __u64 tx_bytes; /* How many bytes we've transmitted */
260 __u64 errors; /* Errors when trying to transmit, */
262 /* runtime counters relating to clone_skb */
264 __u64 allocated_skbs;
265 __u32 clone_count;
266 int last_ok; /* Was last skb sent?
267 * Or a failed transmit of some sort?
268 * This will keep sequence numbers in order
270 ktime_t next_tx;
271 ktime_t started_at;
272 ktime_t stopped_at;
273 u64 idle_acc; /* nano-seconds */
275 __u32 seq_num;
277 int clone_skb; /*
278 * Use multiple SKBs during packet gen.
279 * If this number is greater than 1, then
280 * that many copies of the same packet will be
281 * sent before a new packet is allocated.
282 * If you want to send 1024 identical packets
283 * before creating a new packet,
284 * set clone_skb to 1024.
287 char dst_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
288 char dst_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
289 char src_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
290 char src_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
292 struct in6_addr in6_saddr;
293 struct in6_addr in6_daddr;
294 struct in6_addr cur_in6_daddr;
295 struct in6_addr cur_in6_saddr;
296 /* For ranges */
297 struct in6_addr min_in6_daddr;
298 struct in6_addr max_in6_daddr;
299 struct in6_addr min_in6_saddr;
300 struct in6_addr max_in6_saddr;
302 /* If we're doing ranges, random or incremental, then this
303 * defines the min/max for those ranges.
305 __be32 saddr_min; /* inclusive, source IP address */
306 __be32 saddr_max; /* exclusive, source IP address */
307 __be32 daddr_min; /* inclusive, dest IP address */
308 __be32 daddr_max; /* exclusive, dest IP address */
310 __u16 udp_src_min; /* inclusive, source UDP port */
311 __u16 udp_src_max; /* exclusive, source UDP port */
312 __u16 udp_dst_min; /* inclusive, dest UDP port */
313 __u16 udp_dst_max; /* exclusive, dest UDP port */
315 /* DSCP + ECN */
316 __u8 tos; /* six MSB of (former) IPv4 TOS
317 are for dscp codepoint */
318 __u8 traffic_class; /* ditto for the (former) Traffic Class in IPv6
319 (see RFC 3260, sec. 4) */
321 /* MPLS */
322 unsigned nr_labels; /* Depth of stack, 0 = no MPLS */
323 __be32 labels[MAX_MPLS_LABELS];
325 /* VLAN/SVLAN (802.1Q/Q-in-Q) */
326 __u8 vlan_p;
327 __u8 vlan_cfi;
328 __u16 vlan_id; /* 0xffff means no vlan tag */
330 __u8 svlan_p;
331 __u8 svlan_cfi;
332 __u16 svlan_id; /* 0xffff means no svlan tag */
334 __u32 src_mac_count; /* How many MACs to iterate through */
335 __u32 dst_mac_count; /* How many MACs to iterate through */
337 unsigned char dst_mac[ETH_ALEN];
338 unsigned char src_mac[ETH_ALEN];
340 __u32 cur_dst_mac_offset;
341 __u32 cur_src_mac_offset;
342 __be32 cur_saddr;
343 __be32 cur_daddr;
344 __u16 ip_id;
345 __u16 cur_udp_dst;
346 __u16 cur_udp_src;
347 __u16 cur_queue_map;
348 __u32 cur_pkt_size;
349 __u32 last_pkt_size;
351 __u8 hh[14];
352 /* = {
353 0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
355 We fill in SRC address later
356 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
357 0x08, 0x00
360 __u16 pad; /* pad out the hh struct to an even 16 bytes */
362 struct sk_buff *skb; /* skb we are to transmit next, used for when we
363 * are transmitting the same one multiple times
365 struct net_device *odev; /* The out-going device.
366 * Note that the device should have it's
367 * pg_info pointer pointing back to this
368 * device.
369 * Set when the user specifies the out-going
370 * device name (not when the inject is
371 * started as it used to do.)
373 char odevname[32];
374 struct flow_state *flows;
375 unsigned cflows; /* Concurrent flows (config) */
376 unsigned lflow; /* Flow length (config) */
377 unsigned nflows; /* accumulated flows (stats) */
378 unsigned curfl; /* current sequenced flow (state)*/
380 u16 queue_map_min;
381 u16 queue_map_max;
382 __u32 skb_priority; /* skb priority field */
383 int node; /* Memory node */
385 #ifdef CONFIG_XFRM
386 __u8 ipsmode; /* IPSEC mode (config) */
387 __u8 ipsproto; /* IPSEC type (config) */
388 #endif
389 char result[512];
392 struct pktgen_hdr {
393 __be32 pgh_magic;
394 __be32 seq_num;
395 __be32 tv_sec;
396 __be32 tv_usec;
399 static bool pktgen_exiting __read_mostly;
401 struct pktgen_thread {
402 spinlock_t if_lock; /* for list of devices */
403 struct list_head if_list; /* All device here */
404 struct list_head th_list;
405 struct task_struct *tsk;
406 char result[512];
408 /* Field for thread to receive "posted" events terminate,
409 stop ifs etc. */
411 u32 control;
412 int cpu;
414 wait_queue_head_t queue;
415 struct completion start_done;
418 #define REMOVE 1
419 #define FIND 0
421 static inline ktime_t ktime_now(void)
423 struct timespec ts;
424 ktime_get_ts(&ts);
426 return timespec_to_ktime(ts);
429 /* This works even if 32 bit because of careful byte order choice */
430 static inline int ktime_lt(const ktime_t cmp1, const ktime_t cmp2)
432 return cmp1.tv64 < cmp2.tv64;
435 static const char version[] =
436 "Packet Generator for packet performance testing. "
437 "Version: " VERSION "\n";
439 static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *i);
440 static int pktgen_add_device(struct pktgen_thread *t, const char *ifname);
441 static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
442 const char *ifname, bool exact);
443 static int pktgen_device_event(struct notifier_block *, unsigned long, void *);
444 static void pktgen_run_all_threads(void);
445 static void pktgen_reset_all_threads(void);
446 static void pktgen_stop_all_threads_ifs(void);
448 static void pktgen_stop(struct pktgen_thread *t);
449 static void pktgen_clear_counters(struct pktgen_dev *pkt_dev);
451 static unsigned int scan_ip6(const char *s, char ip[16]);
452 static unsigned int fmt_ip6(char *s, const char ip[16]);
454 /* Module parameters, defaults. */
455 static int pg_count_d __read_mostly = 1000;
456 static int pg_delay_d __read_mostly;
457 static int pg_clone_skb_d __read_mostly;
458 static int debug __read_mostly;
460 static DEFINE_MUTEX(pktgen_thread_lock);
461 static LIST_HEAD(pktgen_threads);
463 static struct notifier_block pktgen_notifier_block = {
464 .notifier_call = pktgen_device_event,
468 * /proc handling functions
472 static int pgctrl_show(struct seq_file *seq, void *v)
474 seq_puts(seq, version);
475 return 0;
478 static ssize_t pgctrl_write(struct file *file, const char __user *buf,
479 size_t count, loff_t *ppos)
481 int err = 0;
482 char data[128];
484 if (!capable(CAP_NET_ADMIN)) {
485 err = -EPERM;
486 goto out;
489 if (count > sizeof(data))
490 count = sizeof(data);
492 if (copy_from_user(data, buf, count)) {
493 err = -EFAULT;
494 goto out;
496 data[count - 1] = 0; /* Make string */
498 if (!strcmp(data, "stop"))
499 pktgen_stop_all_threads_ifs();
501 else if (!strcmp(data, "start"))
502 pktgen_run_all_threads();
504 else if (!strcmp(data, "reset"))
505 pktgen_reset_all_threads();
507 else
508 pr_warning("Unknown command: %s\n", data);
510 err = count;
512 out:
513 return err;
516 static int pgctrl_open(struct inode *inode, struct file *file)
518 return single_open(file, pgctrl_show, PDE(inode)->data);
521 static const struct file_operations pktgen_fops = {
522 .owner = THIS_MODULE,
523 .open = pgctrl_open,
524 .read = seq_read,
525 .llseek = seq_lseek,
526 .write = pgctrl_write,
527 .release = single_release,
530 static int pktgen_if_show(struct seq_file *seq, void *v)
532 const struct pktgen_dev *pkt_dev = seq->private;
533 ktime_t stopped;
534 u64 idle;
536 seq_printf(seq,
537 "Params: count %llu min_pkt_size: %u max_pkt_size: %u\n",
538 (unsigned long long)pkt_dev->count, pkt_dev->min_pkt_size,
539 pkt_dev->max_pkt_size);
541 seq_printf(seq,
542 " frags: %d delay: %llu clone_skb: %d ifname: %s\n",
543 pkt_dev->nfrags, (unsigned long long) pkt_dev->delay,
544 pkt_dev->clone_skb, pkt_dev->odevname);
546 seq_printf(seq, " flows: %u flowlen: %u\n", pkt_dev->cflows,
547 pkt_dev->lflow);
549 seq_printf(seq,
550 " queue_map_min: %u queue_map_max: %u\n",
551 pkt_dev->queue_map_min,
552 pkt_dev->queue_map_max);
554 if (pkt_dev->skb_priority)
555 seq_printf(seq, " skb_priority: %u\n",
556 pkt_dev->skb_priority);
558 if (pkt_dev->flags & F_IPV6) {
559 char b1[128], b2[128], b3[128];
560 fmt_ip6(b1, pkt_dev->in6_saddr.s6_addr);
561 fmt_ip6(b2, pkt_dev->min_in6_saddr.s6_addr);
562 fmt_ip6(b3, pkt_dev->max_in6_saddr.s6_addr);
563 seq_printf(seq,
564 " saddr: %s min_saddr: %s max_saddr: %s\n", b1,
565 b2, b3);
567 fmt_ip6(b1, pkt_dev->in6_daddr.s6_addr);
568 fmt_ip6(b2, pkt_dev->min_in6_daddr.s6_addr);
569 fmt_ip6(b3, pkt_dev->max_in6_daddr.s6_addr);
570 seq_printf(seq,
571 " daddr: %s min_daddr: %s max_daddr: %s\n", b1,
572 b2, b3);
574 } else {
575 seq_printf(seq,
576 " dst_min: %s dst_max: %s\n",
577 pkt_dev->dst_min, pkt_dev->dst_max);
578 seq_printf(seq,
579 " src_min: %s src_max: %s\n",
580 pkt_dev->src_min, pkt_dev->src_max);
583 seq_puts(seq, " src_mac: ");
585 seq_printf(seq, "%pM ",
586 is_zero_ether_addr(pkt_dev->src_mac) ?
587 pkt_dev->odev->dev_addr : pkt_dev->src_mac);
589 seq_printf(seq, "dst_mac: ");
590 seq_printf(seq, "%pM\n", pkt_dev->dst_mac);
592 seq_printf(seq,
593 " udp_src_min: %d udp_src_max: %d"
594 " udp_dst_min: %d udp_dst_max: %d\n",
595 pkt_dev->udp_src_min, pkt_dev->udp_src_max,
596 pkt_dev->udp_dst_min, pkt_dev->udp_dst_max);
598 seq_printf(seq,
599 " src_mac_count: %d dst_mac_count: %d\n",
600 pkt_dev->src_mac_count, pkt_dev->dst_mac_count);
602 if (pkt_dev->nr_labels) {
603 unsigned i;
604 seq_printf(seq, " mpls: ");
605 for (i = 0; i < pkt_dev->nr_labels; i++)
606 seq_printf(seq, "%08x%s", ntohl(pkt_dev->labels[i]),
607 i == pkt_dev->nr_labels-1 ? "\n" : ", ");
610 if (pkt_dev->vlan_id != 0xffff)
611 seq_printf(seq, " vlan_id: %u vlan_p: %u vlan_cfi: %u\n",
612 pkt_dev->vlan_id, pkt_dev->vlan_p,
613 pkt_dev->vlan_cfi);
615 if (pkt_dev->svlan_id != 0xffff)
616 seq_printf(seq, " svlan_id: %u vlan_p: %u vlan_cfi: %u\n",
617 pkt_dev->svlan_id, pkt_dev->svlan_p,
618 pkt_dev->svlan_cfi);
620 if (pkt_dev->tos)
621 seq_printf(seq, " tos: 0x%02x\n", pkt_dev->tos);
623 if (pkt_dev->traffic_class)
624 seq_printf(seq, " traffic_class: 0x%02x\n", pkt_dev->traffic_class);
626 if (pkt_dev->node >= 0)
627 seq_printf(seq, " node: %d\n", pkt_dev->node);
629 seq_printf(seq, " Flags: ");
631 if (pkt_dev->flags & F_IPV6)
632 seq_printf(seq, "IPV6 ");
634 if (pkt_dev->flags & F_IPSRC_RND)
635 seq_printf(seq, "IPSRC_RND ");
637 if (pkt_dev->flags & F_IPDST_RND)
638 seq_printf(seq, "IPDST_RND ");
640 if (pkt_dev->flags & F_TXSIZE_RND)
641 seq_printf(seq, "TXSIZE_RND ");
643 if (pkt_dev->flags & F_UDPSRC_RND)
644 seq_printf(seq, "UDPSRC_RND ");
646 if (pkt_dev->flags & F_UDPDST_RND)
647 seq_printf(seq, "UDPDST_RND ");
649 if (pkt_dev->flags & F_MPLS_RND)
650 seq_printf(seq, "MPLS_RND ");
652 if (pkt_dev->flags & F_QUEUE_MAP_RND)
653 seq_printf(seq, "QUEUE_MAP_RND ");
655 if (pkt_dev->flags & F_QUEUE_MAP_CPU)
656 seq_printf(seq, "QUEUE_MAP_CPU ");
658 if (pkt_dev->cflows) {
659 if (pkt_dev->flags & F_FLOW_SEQ)
660 seq_printf(seq, "FLOW_SEQ "); /*in sequence flows*/
661 else
662 seq_printf(seq, "FLOW_RND ");
665 #ifdef CONFIG_XFRM
666 if (pkt_dev->flags & F_IPSEC_ON)
667 seq_printf(seq, "IPSEC ");
668 #endif
670 if (pkt_dev->flags & F_MACSRC_RND)
671 seq_printf(seq, "MACSRC_RND ");
673 if (pkt_dev->flags & F_MACDST_RND)
674 seq_printf(seq, "MACDST_RND ");
676 if (pkt_dev->flags & F_VID_RND)
677 seq_printf(seq, "VID_RND ");
679 if (pkt_dev->flags & F_SVID_RND)
680 seq_printf(seq, "SVID_RND ");
682 if (pkt_dev->flags & F_NODE)
683 seq_printf(seq, "NODE_ALLOC ");
685 seq_puts(seq, "\n");
687 /* not really stopped, more like last-running-at */
688 stopped = pkt_dev->running ? ktime_now() : pkt_dev->stopped_at;
689 idle = pkt_dev->idle_acc;
690 do_div(idle, NSEC_PER_USEC);
692 seq_printf(seq,
693 "Current:\n pkts-sofar: %llu errors: %llu\n",
694 (unsigned long long)pkt_dev->sofar,
695 (unsigned long long)pkt_dev->errors);
697 seq_printf(seq,
698 " started: %lluus stopped: %lluus idle: %lluus\n",
699 (unsigned long long) ktime_to_us(pkt_dev->started_at),
700 (unsigned long long) ktime_to_us(stopped),
701 (unsigned long long) idle);
703 seq_printf(seq,
704 " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n",
705 pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset,
706 pkt_dev->cur_src_mac_offset);
708 if (pkt_dev->flags & F_IPV6) {
709 char b1[128], b2[128];
710 fmt_ip6(b1, pkt_dev->cur_in6_daddr.s6_addr);
711 fmt_ip6(b2, pkt_dev->cur_in6_saddr.s6_addr);
712 seq_printf(seq, " cur_saddr: %s cur_daddr: %s\n", b2, b1);
713 } else
714 seq_printf(seq, " cur_saddr: 0x%x cur_daddr: 0x%x\n",
715 pkt_dev->cur_saddr, pkt_dev->cur_daddr);
717 seq_printf(seq, " cur_udp_dst: %d cur_udp_src: %d\n",
718 pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src);
720 seq_printf(seq, " cur_queue_map: %u\n", pkt_dev->cur_queue_map);
722 seq_printf(seq, " flows: %u\n", pkt_dev->nflows);
724 if (pkt_dev->result[0])
725 seq_printf(seq, "Result: %s\n", pkt_dev->result);
726 else
727 seq_printf(seq, "Result: Idle\n");
729 return 0;
733 static int hex32_arg(const char __user *user_buffer, unsigned long maxlen,
734 __u32 *num)
736 int i = 0;
737 *num = 0;
739 for (; i < maxlen; i++) {
740 int value;
741 char c;
742 *num <<= 4;
743 if (get_user(c, &user_buffer[i]))
744 return -EFAULT;
745 value = hex_to_bin(c);
746 if (value >= 0)
747 *num |= value;
748 else
749 break;
751 return i;
754 static int count_trail_chars(const char __user * user_buffer,
755 unsigned int maxlen)
757 int i;
759 for (i = 0; i < maxlen; i++) {
760 char c;
761 if (get_user(c, &user_buffer[i]))
762 return -EFAULT;
763 switch (c) {
764 case '\"':
765 case '\n':
766 case '\r':
767 case '\t':
768 case ' ':
769 case '=':
770 break;
771 default:
772 goto done;
775 done:
776 return i;
779 static unsigned long num_arg(const char __user * user_buffer,
780 unsigned long maxlen, unsigned long *num)
782 int i;
783 *num = 0;
785 for (i = 0; i < maxlen; i++) {
786 char c;
787 if (get_user(c, &user_buffer[i]))
788 return -EFAULT;
789 if ((c >= '0') && (c <= '9')) {
790 *num *= 10;
791 *num += c - '0';
792 } else
793 break;
795 return i;
798 static int strn_len(const char __user * user_buffer, unsigned int maxlen)
800 int i;
802 for (i = 0; i < maxlen; i++) {
803 char c;
804 if (get_user(c, &user_buffer[i]))
805 return -EFAULT;
806 switch (c) {
807 case '\"':
808 case '\n':
809 case '\r':
810 case '\t':
811 case ' ':
812 goto done_str;
813 break;
814 default:
815 break;
818 done_str:
819 return i;
822 static ssize_t get_labels(const char __user *buffer, struct pktgen_dev *pkt_dev)
824 unsigned n = 0;
825 char c;
826 ssize_t i = 0;
827 int len;
829 pkt_dev->nr_labels = 0;
830 do {
831 __u32 tmp;
832 len = hex32_arg(&buffer[i], 8, &tmp);
833 if (len <= 0)
834 return len;
835 pkt_dev->labels[n] = htonl(tmp);
836 if (pkt_dev->labels[n] & MPLS_STACK_BOTTOM)
837 pkt_dev->flags |= F_MPLS_RND;
838 i += len;
839 if (get_user(c, &buffer[i]))
840 return -EFAULT;
841 i++;
842 n++;
843 if (n >= MAX_MPLS_LABELS)
844 return -E2BIG;
845 } while (c == ',');
847 pkt_dev->nr_labels = n;
848 return i;
851 static ssize_t pktgen_if_write(struct file *file,
852 const char __user * user_buffer, size_t count,
853 loff_t * offset)
855 struct seq_file *seq = file->private_data;
856 struct pktgen_dev *pkt_dev = seq->private;
857 int i, max, len;
858 char name[16], valstr[32];
859 unsigned long value = 0;
860 char *pg_result = NULL;
861 int tmp = 0;
862 char buf[128];
864 pg_result = &(pkt_dev->result[0]);
866 if (count < 1) {
867 pr_warning("wrong command format\n");
868 return -EINVAL;
871 max = count;
872 tmp = count_trail_chars(user_buffer, max);
873 if (tmp < 0) {
874 pr_warning("illegal format\n");
875 return tmp;
877 i = tmp;
879 /* Read variable name */
881 len = strn_len(&user_buffer[i], sizeof(name) - 1);
882 if (len < 0)
883 return len;
885 memset(name, 0, sizeof(name));
886 if (copy_from_user(name, &user_buffer[i], len))
887 return -EFAULT;
888 i += len;
890 max = count - i;
891 len = count_trail_chars(&user_buffer[i], max);
892 if (len < 0)
893 return len;
895 i += len;
897 if (debug) {
898 size_t copy = min_t(size_t, count, 1023);
899 char tb[copy + 1];
900 if (copy_from_user(tb, user_buffer, copy))
901 return -EFAULT;
902 tb[copy] = 0;
903 printk(KERN_DEBUG "pktgen: %s,%lu buffer -:%s:-\n", name,
904 (unsigned long)count, tb);
907 if (!strcmp(name, "min_pkt_size")) {
908 len = num_arg(&user_buffer[i], 10, &value);
909 if (len < 0)
910 return len;
912 i += len;
913 if (value < 14 + 20 + 8)
914 value = 14 + 20 + 8;
915 if (value != pkt_dev->min_pkt_size) {
916 pkt_dev->min_pkt_size = value;
917 pkt_dev->cur_pkt_size = value;
919 sprintf(pg_result, "OK: min_pkt_size=%u",
920 pkt_dev->min_pkt_size);
921 return count;
924 if (!strcmp(name, "max_pkt_size")) {
925 len = num_arg(&user_buffer[i], 10, &value);
926 if (len < 0)
927 return len;
929 i += len;
930 if (value < 14 + 20 + 8)
931 value = 14 + 20 + 8;
932 if (value != pkt_dev->max_pkt_size) {
933 pkt_dev->max_pkt_size = value;
934 pkt_dev->cur_pkt_size = value;
936 sprintf(pg_result, "OK: max_pkt_size=%u",
937 pkt_dev->max_pkt_size);
938 return count;
941 /* Shortcut for min = max */
943 if (!strcmp(name, "pkt_size")) {
944 len = num_arg(&user_buffer[i], 10, &value);
945 if (len < 0)
946 return len;
948 i += len;
949 if (value < 14 + 20 + 8)
950 value = 14 + 20 + 8;
951 if (value != pkt_dev->min_pkt_size) {
952 pkt_dev->min_pkt_size = value;
953 pkt_dev->max_pkt_size = value;
954 pkt_dev->cur_pkt_size = value;
956 sprintf(pg_result, "OK: pkt_size=%u", pkt_dev->min_pkt_size);
957 return count;
960 if (!strcmp(name, "debug")) {
961 len = num_arg(&user_buffer[i], 10, &value);
962 if (len < 0)
963 return len;
965 i += len;
966 debug = value;
967 sprintf(pg_result, "OK: debug=%u", debug);
968 return count;
971 if (!strcmp(name, "frags")) {
972 len = num_arg(&user_buffer[i], 10, &value);
973 if (len < 0)
974 return len;
976 i += len;
977 pkt_dev->nfrags = value;
978 sprintf(pg_result, "OK: frags=%u", pkt_dev->nfrags);
979 return count;
981 if (!strcmp(name, "delay")) {
982 len = num_arg(&user_buffer[i], 10, &value);
983 if (len < 0)
984 return len;
986 i += len;
987 if (value == 0x7FFFFFFF)
988 pkt_dev->delay = ULLONG_MAX;
989 else
990 pkt_dev->delay = (u64)value;
992 sprintf(pg_result, "OK: delay=%llu",
993 (unsigned long long) pkt_dev->delay);
994 return count;
996 if (!strcmp(name, "rate")) {
997 len = num_arg(&user_buffer[i], 10, &value);
998 if (len < 0)
999 return len;
1001 i += len;
1002 if (!value)
1003 return len;
1004 pkt_dev->delay = pkt_dev->min_pkt_size*8*NSEC_PER_USEC/value;
1005 if (debug)
1006 pr_info("Delay set at: %llu ns\n", pkt_dev->delay);
1008 sprintf(pg_result, "OK: rate=%lu", value);
1009 return count;
1011 if (!strcmp(name, "ratep")) {
1012 len = num_arg(&user_buffer[i], 10, &value);
1013 if (len < 0)
1014 return len;
1016 i += len;
1017 if (!value)
1018 return len;
1019 pkt_dev->delay = NSEC_PER_SEC/value;
1020 if (debug)
1021 pr_info("Delay set at: %llu ns\n", pkt_dev->delay);
1023 sprintf(pg_result, "OK: rate=%lu", value);
1024 return count;
1026 if (!strcmp(name, "udp_src_min")) {
1027 len = num_arg(&user_buffer[i], 10, &value);
1028 if (len < 0)
1029 return len;
1031 i += len;
1032 if (value != pkt_dev->udp_src_min) {
1033 pkt_dev->udp_src_min = value;
1034 pkt_dev->cur_udp_src = value;
1036 sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min);
1037 return count;
1039 if (!strcmp(name, "udp_dst_min")) {
1040 len = num_arg(&user_buffer[i], 10, &value);
1041 if (len < 0)
1042 return len;
1044 i += len;
1045 if (value != pkt_dev->udp_dst_min) {
1046 pkt_dev->udp_dst_min = value;
1047 pkt_dev->cur_udp_dst = value;
1049 sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min);
1050 return count;
1052 if (!strcmp(name, "udp_src_max")) {
1053 len = num_arg(&user_buffer[i], 10, &value);
1054 if (len < 0)
1055 return len;
1057 i += len;
1058 if (value != pkt_dev->udp_src_max) {
1059 pkt_dev->udp_src_max = value;
1060 pkt_dev->cur_udp_src = value;
1062 sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max);
1063 return count;
1065 if (!strcmp(name, "udp_dst_max")) {
1066 len = num_arg(&user_buffer[i], 10, &value);
1067 if (len < 0)
1068 return len;
1070 i += len;
1071 if (value != pkt_dev->udp_dst_max) {
1072 pkt_dev->udp_dst_max = value;
1073 pkt_dev->cur_udp_dst = value;
1075 sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max);
1076 return count;
1078 if (!strcmp(name, "clone_skb")) {
1079 len = num_arg(&user_buffer[i], 10, &value);
1080 if (len < 0)
1081 return len;
1083 i += len;
1084 pkt_dev->clone_skb = value;
1086 sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb);
1087 return count;
1089 if (!strcmp(name, "count")) {
1090 len = num_arg(&user_buffer[i], 10, &value);
1091 if (len < 0)
1092 return len;
1094 i += len;
1095 pkt_dev->count = value;
1096 sprintf(pg_result, "OK: count=%llu",
1097 (unsigned long long)pkt_dev->count);
1098 return count;
1100 if (!strcmp(name, "src_mac_count")) {
1101 len = num_arg(&user_buffer[i], 10, &value);
1102 if (len < 0)
1103 return len;
1105 i += len;
1106 if (pkt_dev->src_mac_count != value) {
1107 pkt_dev->src_mac_count = value;
1108 pkt_dev->cur_src_mac_offset = 0;
1110 sprintf(pg_result, "OK: src_mac_count=%d",
1111 pkt_dev->src_mac_count);
1112 return count;
1114 if (!strcmp(name, "dst_mac_count")) {
1115 len = num_arg(&user_buffer[i], 10, &value);
1116 if (len < 0)
1117 return len;
1119 i += len;
1120 if (pkt_dev->dst_mac_count != value) {
1121 pkt_dev->dst_mac_count = value;
1122 pkt_dev->cur_dst_mac_offset = 0;
1124 sprintf(pg_result, "OK: dst_mac_count=%d",
1125 pkt_dev->dst_mac_count);
1126 return count;
1128 if (!strcmp(name, "node")) {
1129 len = num_arg(&user_buffer[i], 10, &value);
1130 if (len < 0)
1131 return len;
1133 i += len;
1135 if (node_possible(value)) {
1136 pkt_dev->node = value;
1137 sprintf(pg_result, "OK: node=%d", pkt_dev->node);
1138 if (pkt_dev->page) {
1139 put_page(pkt_dev->page);
1140 pkt_dev->page = NULL;
1143 else
1144 sprintf(pg_result, "ERROR: node not possible");
1145 return count;
1147 if (!strcmp(name, "flag")) {
1148 char f[32];
1149 memset(f, 0, 32);
1150 len = strn_len(&user_buffer[i], sizeof(f) - 1);
1151 if (len < 0)
1152 return len;
1154 if (copy_from_user(f, &user_buffer[i], len))
1155 return -EFAULT;
1156 i += len;
1157 if (strcmp(f, "IPSRC_RND") == 0)
1158 pkt_dev->flags |= F_IPSRC_RND;
1160 else if (strcmp(f, "!IPSRC_RND") == 0)
1161 pkt_dev->flags &= ~F_IPSRC_RND;
1163 else if (strcmp(f, "TXSIZE_RND") == 0)
1164 pkt_dev->flags |= F_TXSIZE_RND;
1166 else if (strcmp(f, "!TXSIZE_RND") == 0)
1167 pkt_dev->flags &= ~F_TXSIZE_RND;
1169 else if (strcmp(f, "IPDST_RND") == 0)
1170 pkt_dev->flags |= F_IPDST_RND;
1172 else if (strcmp(f, "!IPDST_RND") == 0)
1173 pkt_dev->flags &= ~F_IPDST_RND;
1175 else if (strcmp(f, "UDPSRC_RND") == 0)
1176 pkt_dev->flags |= F_UDPSRC_RND;
1178 else if (strcmp(f, "!UDPSRC_RND") == 0)
1179 pkt_dev->flags &= ~F_UDPSRC_RND;
1181 else if (strcmp(f, "UDPDST_RND") == 0)
1182 pkt_dev->flags |= F_UDPDST_RND;
1184 else if (strcmp(f, "!UDPDST_RND") == 0)
1185 pkt_dev->flags &= ~F_UDPDST_RND;
1187 else if (strcmp(f, "MACSRC_RND") == 0)
1188 pkt_dev->flags |= F_MACSRC_RND;
1190 else if (strcmp(f, "!MACSRC_RND") == 0)
1191 pkt_dev->flags &= ~F_MACSRC_RND;
1193 else if (strcmp(f, "MACDST_RND") == 0)
1194 pkt_dev->flags |= F_MACDST_RND;
1196 else if (strcmp(f, "!MACDST_RND") == 0)
1197 pkt_dev->flags &= ~F_MACDST_RND;
1199 else if (strcmp(f, "MPLS_RND") == 0)
1200 pkt_dev->flags |= F_MPLS_RND;
1202 else if (strcmp(f, "!MPLS_RND") == 0)
1203 pkt_dev->flags &= ~F_MPLS_RND;
1205 else if (strcmp(f, "VID_RND") == 0)
1206 pkt_dev->flags |= F_VID_RND;
1208 else if (strcmp(f, "!VID_RND") == 0)
1209 pkt_dev->flags &= ~F_VID_RND;
1211 else if (strcmp(f, "SVID_RND") == 0)
1212 pkt_dev->flags |= F_SVID_RND;
1214 else if (strcmp(f, "!SVID_RND") == 0)
1215 pkt_dev->flags &= ~F_SVID_RND;
1217 else if (strcmp(f, "FLOW_SEQ") == 0)
1218 pkt_dev->flags |= F_FLOW_SEQ;
1220 else if (strcmp(f, "QUEUE_MAP_RND") == 0)
1221 pkt_dev->flags |= F_QUEUE_MAP_RND;
1223 else if (strcmp(f, "!QUEUE_MAP_RND") == 0)
1224 pkt_dev->flags &= ~F_QUEUE_MAP_RND;
1226 else if (strcmp(f, "QUEUE_MAP_CPU") == 0)
1227 pkt_dev->flags |= F_QUEUE_MAP_CPU;
1229 else if (strcmp(f, "!QUEUE_MAP_CPU") == 0)
1230 pkt_dev->flags &= ~F_QUEUE_MAP_CPU;
1231 #ifdef CONFIG_XFRM
1232 else if (strcmp(f, "IPSEC") == 0)
1233 pkt_dev->flags |= F_IPSEC_ON;
1234 #endif
1236 else if (strcmp(f, "!IPV6") == 0)
1237 pkt_dev->flags &= ~F_IPV6;
1239 else if (strcmp(f, "NODE_ALLOC") == 0)
1240 pkt_dev->flags |= F_NODE;
1242 else if (strcmp(f, "!NODE_ALLOC") == 0)
1243 pkt_dev->flags &= ~F_NODE;
1245 else {
1246 sprintf(pg_result,
1247 "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
1249 "IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
1250 "MACSRC_RND, MACDST_RND, TXSIZE_RND, IPV6, MPLS_RND, VID_RND, SVID_RND, FLOW_SEQ, IPSEC, NODE_ALLOC\n");
1251 return count;
1253 sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
1254 return count;
1256 if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
1257 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
1258 if (len < 0)
1259 return len;
1261 if (copy_from_user(buf, &user_buffer[i], len))
1262 return -EFAULT;
1263 buf[len] = 0;
1264 if (strcmp(buf, pkt_dev->dst_min) != 0) {
1265 memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
1266 strncpy(pkt_dev->dst_min, buf, len);
1267 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
1268 pkt_dev->cur_daddr = pkt_dev->daddr_min;
1270 if (debug)
1271 printk(KERN_DEBUG "pktgen: dst_min set to: %s\n",
1272 pkt_dev->dst_min);
1273 i += len;
1274 sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min);
1275 return count;
1277 if (!strcmp(name, "dst_max")) {
1278 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
1279 if (len < 0)
1280 return len;
1283 if (copy_from_user(buf, &user_buffer[i], len))
1284 return -EFAULT;
1286 buf[len] = 0;
1287 if (strcmp(buf, pkt_dev->dst_max) != 0) {
1288 memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
1289 strncpy(pkt_dev->dst_max, buf, len);
1290 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
1291 pkt_dev->cur_daddr = pkt_dev->daddr_max;
1293 if (debug)
1294 printk(KERN_DEBUG "pktgen: dst_max set to: %s\n",
1295 pkt_dev->dst_max);
1296 i += len;
1297 sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max);
1298 return count;
1300 if (!strcmp(name, "dst6")) {
1301 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1302 if (len < 0)
1303 return len;
1305 pkt_dev->flags |= F_IPV6;
1307 if (copy_from_user(buf, &user_buffer[i], len))
1308 return -EFAULT;
1309 buf[len] = 0;
1311 scan_ip6(buf, pkt_dev->in6_daddr.s6_addr);
1312 fmt_ip6(buf, pkt_dev->in6_daddr.s6_addr);
1314 ipv6_addr_copy(&pkt_dev->cur_in6_daddr, &pkt_dev->in6_daddr);
1316 if (debug)
1317 printk(KERN_DEBUG "pktgen: dst6 set to: %s\n", buf);
1319 i += len;
1320 sprintf(pg_result, "OK: dst6=%s", buf);
1321 return count;
1323 if (!strcmp(name, "dst6_min")) {
1324 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1325 if (len < 0)
1326 return len;
1328 pkt_dev->flags |= F_IPV6;
1330 if (copy_from_user(buf, &user_buffer[i], len))
1331 return -EFAULT;
1332 buf[len] = 0;
1334 scan_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
1335 fmt_ip6(buf, pkt_dev->min_in6_daddr.s6_addr);
1337 ipv6_addr_copy(&pkt_dev->cur_in6_daddr,
1338 &pkt_dev->min_in6_daddr);
1339 if (debug)
1340 printk(KERN_DEBUG "pktgen: dst6_min set to: %s\n", buf);
1342 i += len;
1343 sprintf(pg_result, "OK: dst6_min=%s", buf);
1344 return count;
1346 if (!strcmp(name, "dst6_max")) {
1347 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1348 if (len < 0)
1349 return len;
1351 pkt_dev->flags |= F_IPV6;
1353 if (copy_from_user(buf, &user_buffer[i], len))
1354 return -EFAULT;
1355 buf[len] = 0;
1357 scan_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
1358 fmt_ip6(buf, pkt_dev->max_in6_daddr.s6_addr);
1360 if (debug)
1361 printk(KERN_DEBUG "pktgen: dst6_max set to: %s\n", buf);
1363 i += len;
1364 sprintf(pg_result, "OK: dst6_max=%s", buf);
1365 return count;
1367 if (!strcmp(name, "src6")) {
1368 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1369 if (len < 0)
1370 return len;
1372 pkt_dev->flags |= F_IPV6;
1374 if (copy_from_user(buf, &user_buffer[i], len))
1375 return -EFAULT;
1376 buf[len] = 0;
1378 scan_ip6(buf, pkt_dev->in6_saddr.s6_addr);
1379 fmt_ip6(buf, pkt_dev->in6_saddr.s6_addr);
1381 ipv6_addr_copy(&pkt_dev->cur_in6_saddr, &pkt_dev->in6_saddr);
1383 if (debug)
1384 printk(KERN_DEBUG "pktgen: src6 set to: %s\n", buf);
1386 i += len;
1387 sprintf(pg_result, "OK: src6=%s", buf);
1388 return count;
1390 if (!strcmp(name, "src_min")) {
1391 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
1392 if (len < 0)
1393 return len;
1395 if (copy_from_user(buf, &user_buffer[i], len))
1396 return -EFAULT;
1397 buf[len] = 0;
1398 if (strcmp(buf, pkt_dev->src_min) != 0) {
1399 memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
1400 strncpy(pkt_dev->src_min, buf, len);
1401 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
1402 pkt_dev->cur_saddr = pkt_dev->saddr_min;
1404 if (debug)
1405 printk(KERN_DEBUG "pktgen: src_min set to: %s\n",
1406 pkt_dev->src_min);
1407 i += len;
1408 sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min);
1409 return count;
1411 if (!strcmp(name, "src_max")) {
1412 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
1413 if (len < 0)
1414 return len;
1416 if (copy_from_user(buf, &user_buffer[i], len))
1417 return -EFAULT;
1418 buf[len] = 0;
1419 if (strcmp(buf, pkt_dev->src_max) != 0) {
1420 memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
1421 strncpy(pkt_dev->src_max, buf, len);
1422 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
1423 pkt_dev->cur_saddr = pkt_dev->saddr_max;
1425 if (debug)
1426 printk(KERN_DEBUG "pktgen: src_max set to: %s\n",
1427 pkt_dev->src_max);
1428 i += len;
1429 sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max);
1430 return count;
1432 if (!strcmp(name, "dst_mac")) {
1433 char *v = valstr;
1434 unsigned char old_dmac[ETH_ALEN];
1435 unsigned char *m = pkt_dev->dst_mac;
1436 memcpy(old_dmac, pkt_dev->dst_mac, ETH_ALEN);
1438 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
1439 if (len < 0)
1440 return len;
1442 memset(valstr, 0, sizeof(valstr));
1443 if (copy_from_user(valstr, &user_buffer[i], len))
1444 return -EFAULT;
1445 i += len;
1447 for (*m = 0; *v && m < pkt_dev->dst_mac + 6; v++) {
1448 int value;
1450 value = hex_to_bin(*v);
1451 if (value >= 0)
1452 *m = *m * 16 + value;
1454 if (*v == ':') {
1455 m++;
1456 *m = 0;
1460 /* Set up Dest MAC */
1461 if (compare_ether_addr(old_dmac, pkt_dev->dst_mac))
1462 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
1464 sprintf(pg_result, "OK: dstmac");
1465 return count;
1467 if (!strcmp(name, "src_mac")) {
1468 char *v = valstr;
1469 unsigned char old_smac[ETH_ALEN];
1470 unsigned char *m = pkt_dev->src_mac;
1472 memcpy(old_smac, pkt_dev->src_mac, ETH_ALEN);
1474 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
1475 if (len < 0)
1476 return len;
1478 memset(valstr, 0, sizeof(valstr));
1479 if (copy_from_user(valstr, &user_buffer[i], len))
1480 return -EFAULT;
1481 i += len;
1483 for (*m = 0; *v && m < pkt_dev->src_mac + 6; v++) {
1484 int value;
1486 value = hex_to_bin(*v);
1487 if (value >= 0)
1488 *m = *m * 16 + value;
1490 if (*v == ':') {
1491 m++;
1492 *m = 0;
1496 /* Set up Src MAC */
1497 if (compare_ether_addr(old_smac, pkt_dev->src_mac))
1498 memcpy(&(pkt_dev->hh[6]), pkt_dev->src_mac, ETH_ALEN);
1500 sprintf(pg_result, "OK: srcmac");
1501 return count;
1504 if (!strcmp(name, "clear_counters")) {
1505 pktgen_clear_counters(pkt_dev);
1506 sprintf(pg_result, "OK: Clearing counters.\n");
1507 return count;
1510 if (!strcmp(name, "flows")) {
1511 len = num_arg(&user_buffer[i], 10, &value);
1512 if (len < 0)
1513 return len;
1515 i += len;
1516 if (value > MAX_CFLOWS)
1517 value = MAX_CFLOWS;
1519 pkt_dev->cflows = value;
1520 sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
1521 return count;
1524 if (!strcmp(name, "flowlen")) {
1525 len = num_arg(&user_buffer[i], 10, &value);
1526 if (len < 0)
1527 return len;
1529 i += len;
1530 pkt_dev->lflow = value;
1531 sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
1532 return count;
1535 if (!strcmp(name, "queue_map_min")) {
1536 len = num_arg(&user_buffer[i], 5, &value);
1537 if (len < 0)
1538 return len;
1540 i += len;
1541 pkt_dev->queue_map_min = value;
1542 sprintf(pg_result, "OK: queue_map_min=%u", pkt_dev->queue_map_min);
1543 return count;
1546 if (!strcmp(name, "queue_map_max")) {
1547 len = num_arg(&user_buffer[i], 5, &value);
1548 if (len < 0)
1549 return len;
1551 i += len;
1552 pkt_dev->queue_map_max = value;
1553 sprintf(pg_result, "OK: queue_map_max=%u", pkt_dev->queue_map_max);
1554 return count;
1557 if (!strcmp(name, "mpls")) {
1558 unsigned n, cnt;
1560 len = get_labels(&user_buffer[i], pkt_dev);
1561 if (len < 0)
1562 return len;
1563 i += len;
1564 cnt = sprintf(pg_result, "OK: mpls=");
1565 for (n = 0; n < pkt_dev->nr_labels; n++)
1566 cnt += sprintf(pg_result + cnt,
1567 "%08x%s", ntohl(pkt_dev->labels[n]),
1568 n == pkt_dev->nr_labels-1 ? "" : ",");
1570 if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) {
1571 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1572 pkt_dev->svlan_id = 0xffff;
1574 if (debug)
1575 printk(KERN_DEBUG "pktgen: VLAN/SVLAN auto turned off\n");
1577 return count;
1580 if (!strcmp(name, "vlan_id")) {
1581 len = num_arg(&user_buffer[i], 4, &value);
1582 if (len < 0)
1583 return len;
1585 i += len;
1586 if (value <= 4095) {
1587 pkt_dev->vlan_id = value; /* turn on VLAN */
1589 if (debug)
1590 printk(KERN_DEBUG "pktgen: VLAN turned on\n");
1592 if (debug && pkt_dev->nr_labels)
1593 printk(KERN_DEBUG "pktgen: MPLS auto turned off\n");
1595 pkt_dev->nr_labels = 0; /* turn off MPLS */
1596 sprintf(pg_result, "OK: vlan_id=%u", pkt_dev->vlan_id);
1597 } else {
1598 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1599 pkt_dev->svlan_id = 0xffff;
1601 if (debug)
1602 printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n");
1604 return count;
1607 if (!strcmp(name, "vlan_p")) {
1608 len = num_arg(&user_buffer[i], 1, &value);
1609 if (len < 0)
1610 return len;
1612 i += len;
1613 if ((value <= 7) && (pkt_dev->vlan_id != 0xffff)) {
1614 pkt_dev->vlan_p = value;
1615 sprintf(pg_result, "OK: vlan_p=%u", pkt_dev->vlan_p);
1616 } else {
1617 sprintf(pg_result, "ERROR: vlan_p must be 0-7");
1619 return count;
1622 if (!strcmp(name, "vlan_cfi")) {
1623 len = num_arg(&user_buffer[i], 1, &value);
1624 if (len < 0)
1625 return len;
1627 i += len;
1628 if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) {
1629 pkt_dev->vlan_cfi = value;
1630 sprintf(pg_result, "OK: vlan_cfi=%u", pkt_dev->vlan_cfi);
1631 } else {
1632 sprintf(pg_result, "ERROR: vlan_cfi must be 0-1");
1634 return count;
1637 if (!strcmp(name, "svlan_id")) {
1638 len = num_arg(&user_buffer[i], 4, &value);
1639 if (len < 0)
1640 return len;
1642 i += len;
1643 if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) {
1644 pkt_dev->svlan_id = value; /* turn on SVLAN */
1646 if (debug)
1647 printk(KERN_DEBUG "pktgen: SVLAN turned on\n");
1649 if (debug && pkt_dev->nr_labels)
1650 printk(KERN_DEBUG "pktgen: MPLS auto turned off\n");
1652 pkt_dev->nr_labels = 0; /* turn off MPLS */
1653 sprintf(pg_result, "OK: svlan_id=%u", pkt_dev->svlan_id);
1654 } else {
1655 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1656 pkt_dev->svlan_id = 0xffff;
1658 if (debug)
1659 printk(KERN_DEBUG "pktgen: VLAN/SVLAN turned off\n");
1661 return count;
1664 if (!strcmp(name, "svlan_p")) {
1665 len = num_arg(&user_buffer[i], 1, &value);
1666 if (len < 0)
1667 return len;
1669 i += len;
1670 if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) {
1671 pkt_dev->svlan_p = value;
1672 sprintf(pg_result, "OK: svlan_p=%u", pkt_dev->svlan_p);
1673 } else {
1674 sprintf(pg_result, "ERROR: svlan_p must be 0-7");
1676 return count;
1679 if (!strcmp(name, "svlan_cfi")) {
1680 len = num_arg(&user_buffer[i], 1, &value);
1681 if (len < 0)
1682 return len;
1684 i += len;
1685 if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) {
1686 pkt_dev->svlan_cfi = value;
1687 sprintf(pg_result, "OK: svlan_cfi=%u", pkt_dev->svlan_cfi);
1688 } else {
1689 sprintf(pg_result, "ERROR: svlan_cfi must be 0-1");
1691 return count;
1694 if (!strcmp(name, "tos")) {
1695 __u32 tmp_value = 0;
1696 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1697 if (len < 0)
1698 return len;
1700 i += len;
1701 if (len == 2) {
1702 pkt_dev->tos = tmp_value;
1703 sprintf(pg_result, "OK: tos=0x%02x", pkt_dev->tos);
1704 } else {
1705 sprintf(pg_result, "ERROR: tos must be 00-ff");
1707 return count;
1710 if (!strcmp(name, "traffic_class")) {
1711 __u32 tmp_value = 0;
1712 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1713 if (len < 0)
1714 return len;
1716 i += len;
1717 if (len == 2) {
1718 pkt_dev->traffic_class = tmp_value;
1719 sprintf(pg_result, "OK: traffic_class=0x%02x", pkt_dev->traffic_class);
1720 } else {
1721 sprintf(pg_result, "ERROR: traffic_class must be 00-ff");
1723 return count;
1726 if (!strcmp(name, "skb_priority")) {
1727 len = num_arg(&user_buffer[i], 9, &value);
1728 if (len < 0)
1729 return len;
1731 i += len;
1732 pkt_dev->skb_priority = value;
1733 sprintf(pg_result, "OK: skb_priority=%i",
1734 pkt_dev->skb_priority);
1735 return count;
1738 sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
1739 return -EINVAL;
1742 static int pktgen_if_open(struct inode *inode, struct file *file)
1744 return single_open(file, pktgen_if_show, PDE(inode)->data);
1747 static const struct file_operations pktgen_if_fops = {
1748 .owner = THIS_MODULE,
1749 .open = pktgen_if_open,
1750 .read = seq_read,
1751 .llseek = seq_lseek,
1752 .write = pktgen_if_write,
1753 .release = single_release,
1756 static int pktgen_thread_show(struct seq_file *seq, void *v)
1758 struct pktgen_thread *t = seq->private;
1759 const struct pktgen_dev *pkt_dev;
1761 BUG_ON(!t);
1763 seq_printf(seq, "Running: ");
1765 if_lock(t);
1766 list_for_each_entry(pkt_dev, &t->if_list, list)
1767 if (pkt_dev->running)
1768 seq_printf(seq, "%s ", pkt_dev->odevname);
1770 seq_printf(seq, "\nStopped: ");
1772 list_for_each_entry(pkt_dev, &t->if_list, list)
1773 if (!pkt_dev->running)
1774 seq_printf(seq, "%s ", pkt_dev->odevname);
1776 if (t->result[0])
1777 seq_printf(seq, "\nResult: %s\n", t->result);
1778 else
1779 seq_printf(seq, "\nResult: NA\n");
1781 if_unlock(t);
1783 return 0;
1786 static ssize_t pktgen_thread_write(struct file *file,
1787 const char __user * user_buffer,
1788 size_t count, loff_t * offset)
1790 struct seq_file *seq = file->private_data;
1791 struct pktgen_thread *t = seq->private;
1792 int i, max, len, ret;
1793 char name[40];
1794 char *pg_result;
1796 if (count < 1) {
1797 // sprintf(pg_result, "Wrong command format");
1798 return -EINVAL;
1801 max = count;
1802 len = count_trail_chars(user_buffer, max);
1803 if (len < 0)
1804 return len;
1806 i = len;
1808 /* Read variable name */
1810 len = strn_len(&user_buffer[i], sizeof(name) - 1);
1811 if (len < 0)
1812 return len;
1814 memset(name, 0, sizeof(name));
1815 if (copy_from_user(name, &user_buffer[i], len))
1816 return -EFAULT;
1817 i += len;
1819 max = count - i;
1820 len = count_trail_chars(&user_buffer[i], max);
1821 if (len < 0)
1822 return len;
1824 i += len;
1826 if (debug)
1827 printk(KERN_DEBUG "pktgen: t=%s, count=%lu\n",
1828 name, (unsigned long)count);
1830 if (!t) {
1831 pr_err("ERROR: No thread\n");
1832 ret = -EINVAL;
1833 goto out;
1836 pg_result = &(t->result[0]);
1838 if (!strcmp(name, "add_device")) {
1839 char f[32];
1840 memset(f, 0, 32);
1841 len = strn_len(&user_buffer[i], sizeof(f) - 1);
1842 if (len < 0) {
1843 ret = len;
1844 goto out;
1846 if (copy_from_user(f, &user_buffer[i], len))
1847 return -EFAULT;
1848 i += len;
1849 mutex_lock(&pktgen_thread_lock);
1850 pktgen_add_device(t, f);
1851 mutex_unlock(&pktgen_thread_lock);
1852 ret = count;
1853 sprintf(pg_result, "OK: add_device=%s", f);
1854 goto out;
1857 if (!strcmp(name, "rem_device_all")) {
1858 mutex_lock(&pktgen_thread_lock);
1859 t->control |= T_REMDEVALL;
1860 mutex_unlock(&pktgen_thread_lock);
1861 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
1862 ret = count;
1863 sprintf(pg_result, "OK: rem_device_all");
1864 goto out;
1867 if (!strcmp(name, "max_before_softirq")) {
1868 sprintf(pg_result, "OK: Note! max_before_softirq is obsoleted -- Do not use");
1869 ret = count;
1870 goto out;
1873 ret = -EINVAL;
1874 out:
1875 return ret;
1878 static int pktgen_thread_open(struct inode *inode, struct file *file)
1880 return single_open(file, pktgen_thread_show, PDE(inode)->data);
1883 static const struct file_operations pktgen_thread_fops = {
1884 .owner = THIS_MODULE,
1885 .open = pktgen_thread_open,
1886 .read = seq_read,
1887 .llseek = seq_lseek,
1888 .write = pktgen_thread_write,
1889 .release = single_release,
1892 /* Think find or remove for NN */
1893 static struct pktgen_dev *__pktgen_NN_threads(const char *ifname, int remove)
1895 struct pktgen_thread *t;
1896 struct pktgen_dev *pkt_dev = NULL;
1897 bool exact = (remove == FIND);
1899 list_for_each_entry(t, &pktgen_threads, th_list) {
1900 pkt_dev = pktgen_find_dev(t, ifname, exact);
1901 if (pkt_dev) {
1902 if (remove) {
1903 if_lock(t);
1904 pkt_dev->removal_mark = 1;
1905 t->control |= T_REMDEV;
1906 if_unlock(t);
1908 break;
1911 return pkt_dev;
1915 * mark a device for removal
1917 static void pktgen_mark_device(const char *ifname)
1919 struct pktgen_dev *pkt_dev = NULL;
1920 const int max_tries = 10, msec_per_try = 125;
1921 int i = 0;
1923 mutex_lock(&pktgen_thread_lock);
1924 pr_debug("%s: marking %s for removal\n", __func__, ifname);
1926 while (1) {
1928 pkt_dev = __pktgen_NN_threads(ifname, REMOVE);
1929 if (pkt_dev == NULL)
1930 break; /* success */
1932 mutex_unlock(&pktgen_thread_lock);
1933 pr_debug("%s: waiting for %s to disappear....\n",
1934 __func__, ifname);
1935 schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
1936 mutex_lock(&pktgen_thread_lock);
1938 if (++i >= max_tries) {
1939 pr_err("%s: timed out after waiting %d msec for device %s to be removed\n",
1940 __func__, msec_per_try * i, ifname);
1941 break;
1946 mutex_unlock(&pktgen_thread_lock);
1949 static void pktgen_change_name(struct net_device *dev)
1951 struct pktgen_thread *t;
1953 list_for_each_entry(t, &pktgen_threads, th_list) {
1954 struct pktgen_dev *pkt_dev;
1956 list_for_each_entry(pkt_dev, &t->if_list, list) {
1957 if (pkt_dev->odev != dev)
1958 continue;
1960 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
1962 pkt_dev->entry = proc_create_data(dev->name, 0600,
1963 pg_proc_dir,
1964 &pktgen_if_fops,
1965 pkt_dev);
1966 if (!pkt_dev->entry)
1967 pr_err("can't move proc entry for '%s'\n",
1968 dev->name);
1969 break;
1974 static int pktgen_device_event(struct notifier_block *unused,
1975 unsigned long event, void *ptr)
1977 struct net_device *dev = ptr;
1979 if (!net_eq(dev_net(dev), &init_net))
1980 return NOTIFY_DONE;
1982 /* It is OK that we do not hold the group lock right now,
1983 * as we run under the RTNL lock.
1986 switch (event) {
1987 case NETDEV_CHANGENAME:
1988 pktgen_change_name(dev);
1989 break;
1991 case NETDEV_UNREGISTER:
1992 pktgen_mark_device(dev->name);
1993 break;
1996 return NOTIFY_DONE;
1999 static struct net_device *pktgen_dev_get_by_name(struct pktgen_dev *pkt_dev,
2000 const char *ifname)
2002 char b[IFNAMSIZ+5];
2003 int i;
2005 for (i = 0; ifname[i] != '@'; i++) {
2006 if (i == IFNAMSIZ)
2007 break;
2009 b[i] = ifname[i];
2011 b[i] = 0;
2013 return dev_get_by_name(&init_net, b);
2017 /* Associate pktgen_dev with a device. */
2019 static int pktgen_setup_dev(struct pktgen_dev *pkt_dev, const char *ifname)
2021 struct net_device *odev;
2022 int err;
2024 /* Clean old setups */
2025 if (pkt_dev->odev) {
2026 dev_put(pkt_dev->odev);
2027 pkt_dev->odev = NULL;
2030 odev = pktgen_dev_get_by_name(pkt_dev, ifname);
2031 if (!odev) {
2032 pr_err("no such netdevice: \"%s\"\n", ifname);
2033 return -ENODEV;
2036 if (odev->type != ARPHRD_ETHER) {
2037 pr_err("not an ethernet device: \"%s\"\n", ifname);
2038 err = -EINVAL;
2039 } else if (!netif_running(odev)) {
2040 pr_err("device is down: \"%s\"\n", ifname);
2041 err = -ENETDOWN;
2042 } else {
2043 pkt_dev->odev = odev;
2044 return 0;
2047 dev_put(odev);
2048 return err;
2051 /* Read pkt_dev from the interface and set up internal pktgen_dev
2052 * structure to have the right information to create/send packets
2054 static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
2056 int ntxq;
2058 if (!pkt_dev->odev) {
2059 pr_err("ERROR: pkt_dev->odev == NULL in setup_inject\n");
2060 sprintf(pkt_dev->result,
2061 "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
2062 return;
2065 /* make sure that we don't pick a non-existing transmit queue */
2066 ntxq = pkt_dev->odev->real_num_tx_queues;
2068 if (ntxq <= pkt_dev->queue_map_min) {
2069 pr_warning("WARNING: Requested queue_map_min (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
2070 pkt_dev->queue_map_min, (ntxq ?: 1) - 1, ntxq,
2071 pkt_dev->odevname);
2072 pkt_dev->queue_map_min = ntxq - 1;
2074 if (pkt_dev->queue_map_max >= ntxq) {
2075 pr_warning("WARNING: Requested queue_map_max (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
2076 pkt_dev->queue_map_max, (ntxq ?: 1) - 1, ntxq,
2077 pkt_dev->odevname);
2078 pkt_dev->queue_map_max = ntxq - 1;
2081 /* Default to the interface's mac if not explicitly set. */
2083 if (is_zero_ether_addr(pkt_dev->src_mac))
2084 memcpy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr, ETH_ALEN);
2086 /* Set up Dest MAC */
2087 memcpy(&(pkt_dev->hh[0]), pkt_dev->dst_mac, ETH_ALEN);
2089 /* Set up pkt size */
2090 pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
2092 if (pkt_dev->flags & F_IPV6) {
2094 * Skip this automatic address setting until locks or functions
2095 * gets exported
2098 #ifdef NOTNOW
2099 int i, set = 0, err = 1;
2100 struct inet6_dev *idev;
2102 for (i = 0; i < IN6_ADDR_HSIZE; i++)
2103 if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
2104 set = 1;
2105 break;
2108 if (!set) {
2111 * Use linklevel address if unconfigured.
2113 * use ipv6_get_lladdr if/when it's get exported
2116 rcu_read_lock();
2117 idev = __in6_dev_get(pkt_dev->odev);
2118 if (idev) {
2119 struct inet6_ifaddr *ifp;
2121 read_lock_bh(&idev->lock);
2122 for (ifp = idev->addr_list; ifp;
2123 ifp = ifp->if_next) {
2124 if (ifp->scope == IFA_LINK &&
2125 !(ifp->flags & IFA_F_TENTATIVE)) {
2126 ipv6_addr_copy(&pkt_dev->
2127 cur_in6_saddr,
2128 &ifp->addr);
2129 err = 0;
2130 break;
2133 read_unlock_bh(&idev->lock);
2135 rcu_read_unlock();
2136 if (err)
2137 pr_err("ERROR: IPv6 link address not available\n");
2139 #endif
2140 } else {
2141 pkt_dev->saddr_min = 0;
2142 pkt_dev->saddr_max = 0;
2143 if (strlen(pkt_dev->src_min) == 0) {
2145 struct in_device *in_dev;
2147 rcu_read_lock();
2148 in_dev = __in_dev_get_rcu(pkt_dev->odev);
2149 if (in_dev) {
2150 if (in_dev->ifa_list) {
2151 pkt_dev->saddr_min =
2152 in_dev->ifa_list->ifa_address;
2153 pkt_dev->saddr_max = pkt_dev->saddr_min;
2156 rcu_read_unlock();
2157 } else {
2158 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
2159 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
2162 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
2163 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
2165 /* Initialize current values. */
2166 pkt_dev->cur_dst_mac_offset = 0;
2167 pkt_dev->cur_src_mac_offset = 0;
2168 pkt_dev->cur_saddr = pkt_dev->saddr_min;
2169 pkt_dev->cur_daddr = pkt_dev->daddr_min;
2170 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2171 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
2172 pkt_dev->nflows = 0;
2176 static void spin(struct pktgen_dev *pkt_dev, ktime_t spin_until)
2178 ktime_t start_time, end_time;
2179 s64 remaining;
2180 struct hrtimer_sleeper t;
2182 hrtimer_init_on_stack(&t.timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
2183 hrtimer_set_expires(&t.timer, spin_until);
2185 remaining = ktime_to_ns(hrtimer_expires_remaining(&t.timer));
2186 if (remaining <= 0) {
2187 pkt_dev->next_tx = ktime_add_ns(spin_until, pkt_dev->delay);
2188 return;
2191 start_time = ktime_now();
2192 if (remaining < 100000)
2193 ndelay(remaining); /* really small just spin */
2194 else {
2195 /* see do_nanosleep */
2196 hrtimer_init_sleeper(&t, current);
2197 do {
2198 set_current_state(TASK_INTERRUPTIBLE);
2199 hrtimer_start_expires(&t.timer, HRTIMER_MODE_ABS);
2200 if (!hrtimer_active(&t.timer))
2201 t.task = NULL;
2203 if (likely(t.task))
2204 schedule();
2206 hrtimer_cancel(&t.timer);
2207 } while (t.task && pkt_dev->running && !signal_pending(current));
2208 __set_current_state(TASK_RUNNING);
2210 end_time = ktime_now();
2212 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(end_time, start_time));
2213 pkt_dev->next_tx = ktime_add_ns(spin_until, pkt_dev->delay);
2216 static inline void set_pkt_overhead(struct pktgen_dev *pkt_dev)
2218 pkt_dev->pkt_overhead = 0;
2219 pkt_dev->pkt_overhead += pkt_dev->nr_labels*sizeof(u32);
2220 pkt_dev->pkt_overhead += VLAN_TAG_SIZE(pkt_dev);
2221 pkt_dev->pkt_overhead += SVLAN_TAG_SIZE(pkt_dev);
2224 static inline int f_seen(const struct pktgen_dev *pkt_dev, int flow)
2226 return !!(pkt_dev->flows[flow].flags & F_INIT);
2229 static inline int f_pick(struct pktgen_dev *pkt_dev)
2231 int flow = pkt_dev->curfl;
2233 if (pkt_dev->flags & F_FLOW_SEQ) {
2234 if (pkt_dev->flows[flow].count >= pkt_dev->lflow) {
2235 /* reset time */
2236 pkt_dev->flows[flow].count = 0;
2237 pkt_dev->flows[flow].flags = 0;
2238 pkt_dev->curfl += 1;
2239 if (pkt_dev->curfl >= pkt_dev->cflows)
2240 pkt_dev->curfl = 0; /*reset */
2242 } else {
2243 flow = random32() % pkt_dev->cflows;
2244 pkt_dev->curfl = flow;
2246 if (pkt_dev->flows[flow].count > pkt_dev->lflow) {
2247 pkt_dev->flows[flow].count = 0;
2248 pkt_dev->flows[flow].flags = 0;
2252 return pkt_dev->curfl;
2256 #ifdef CONFIG_XFRM
2257 /* If there was already an IPSEC SA, we keep it as is, else
2258 * we go look for it ...
2260 #define DUMMY_MARK 0
2261 static void get_ipsec_sa(struct pktgen_dev *pkt_dev, int flow)
2263 struct xfrm_state *x = pkt_dev->flows[flow].x;
2264 if (!x) {
2265 /*slow path: we dont already have xfrm_state*/
2266 x = xfrm_stateonly_find(&init_net, DUMMY_MARK,
2267 (xfrm_address_t *)&pkt_dev->cur_daddr,
2268 (xfrm_address_t *)&pkt_dev->cur_saddr,
2269 AF_INET,
2270 pkt_dev->ipsmode,
2271 pkt_dev->ipsproto, 0);
2272 if (x) {
2273 pkt_dev->flows[flow].x = x;
2274 set_pkt_overhead(pkt_dev);
2275 pkt_dev->pkt_overhead += x->props.header_len;
2280 #endif
2281 static void set_cur_queue_map(struct pktgen_dev *pkt_dev)
2284 if (pkt_dev->flags & F_QUEUE_MAP_CPU)
2285 pkt_dev->cur_queue_map = smp_processor_id();
2287 else if (pkt_dev->queue_map_min <= pkt_dev->queue_map_max) {
2288 __u16 t;
2289 if (pkt_dev->flags & F_QUEUE_MAP_RND) {
2290 t = random32() %
2291 (pkt_dev->queue_map_max -
2292 pkt_dev->queue_map_min + 1)
2293 + pkt_dev->queue_map_min;
2294 } else {
2295 t = pkt_dev->cur_queue_map + 1;
2296 if (t > pkt_dev->queue_map_max)
2297 t = pkt_dev->queue_map_min;
2299 pkt_dev->cur_queue_map = t;
2301 pkt_dev->cur_queue_map = pkt_dev->cur_queue_map % pkt_dev->odev->real_num_tx_queues;
2304 /* Increment/randomize headers according to flags and current values
2305 * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
2307 static void mod_cur_headers(struct pktgen_dev *pkt_dev)
2309 __u32 imn;
2310 __u32 imx;
2311 int flow = 0;
2313 if (pkt_dev->cflows)
2314 flow = f_pick(pkt_dev);
2316 /* Deal with source MAC */
2317 if (pkt_dev->src_mac_count > 1) {
2318 __u32 mc;
2319 __u32 tmp;
2321 if (pkt_dev->flags & F_MACSRC_RND)
2322 mc = random32() % pkt_dev->src_mac_count;
2323 else {
2324 mc = pkt_dev->cur_src_mac_offset++;
2325 if (pkt_dev->cur_src_mac_offset >=
2326 pkt_dev->src_mac_count)
2327 pkt_dev->cur_src_mac_offset = 0;
2330 tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
2331 pkt_dev->hh[11] = tmp;
2332 tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2333 pkt_dev->hh[10] = tmp;
2334 tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2335 pkt_dev->hh[9] = tmp;
2336 tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2337 pkt_dev->hh[8] = tmp;
2338 tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
2339 pkt_dev->hh[7] = tmp;
2342 /* Deal with Destination MAC */
2343 if (pkt_dev->dst_mac_count > 1) {
2344 __u32 mc;
2345 __u32 tmp;
2347 if (pkt_dev->flags & F_MACDST_RND)
2348 mc = random32() % pkt_dev->dst_mac_count;
2350 else {
2351 mc = pkt_dev->cur_dst_mac_offset++;
2352 if (pkt_dev->cur_dst_mac_offset >=
2353 pkt_dev->dst_mac_count) {
2354 pkt_dev->cur_dst_mac_offset = 0;
2358 tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
2359 pkt_dev->hh[5] = tmp;
2360 tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2361 pkt_dev->hh[4] = tmp;
2362 tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2363 pkt_dev->hh[3] = tmp;
2364 tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2365 pkt_dev->hh[2] = tmp;
2366 tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
2367 pkt_dev->hh[1] = tmp;
2370 if (pkt_dev->flags & F_MPLS_RND) {
2371 unsigned i;
2372 for (i = 0; i < pkt_dev->nr_labels; i++)
2373 if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
2374 pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
2375 ((__force __be32)random32() &
2376 htonl(0x000fffff));
2379 if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
2380 pkt_dev->vlan_id = random32() & (4096-1);
2383 if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
2384 pkt_dev->svlan_id = random32() & (4096 - 1);
2387 if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
2388 if (pkt_dev->flags & F_UDPSRC_RND)
2389 pkt_dev->cur_udp_src = random32() %
2390 (pkt_dev->udp_src_max - pkt_dev->udp_src_min)
2391 + pkt_dev->udp_src_min;
2393 else {
2394 pkt_dev->cur_udp_src++;
2395 if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
2396 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
2400 if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
2401 if (pkt_dev->flags & F_UDPDST_RND) {
2402 pkt_dev->cur_udp_dst = random32() %
2403 (pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)
2404 + pkt_dev->udp_dst_min;
2405 } else {
2406 pkt_dev->cur_udp_dst++;
2407 if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
2408 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2412 if (!(pkt_dev->flags & F_IPV6)) {
2414 imn = ntohl(pkt_dev->saddr_min);
2415 imx = ntohl(pkt_dev->saddr_max);
2416 if (imn < imx) {
2417 __u32 t;
2418 if (pkt_dev->flags & F_IPSRC_RND)
2419 t = random32() % (imx - imn) + imn;
2420 else {
2421 t = ntohl(pkt_dev->cur_saddr);
2422 t++;
2423 if (t > imx)
2424 t = imn;
2427 pkt_dev->cur_saddr = htonl(t);
2430 if (pkt_dev->cflows && f_seen(pkt_dev, flow)) {
2431 pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
2432 } else {
2433 imn = ntohl(pkt_dev->daddr_min);
2434 imx = ntohl(pkt_dev->daddr_max);
2435 if (imn < imx) {
2436 __u32 t;
2437 __be32 s;
2438 if (pkt_dev->flags & F_IPDST_RND) {
2440 t = random32() % (imx - imn) + imn;
2441 s = htonl(t);
2443 while (ipv4_is_loopback(s) ||
2444 ipv4_is_multicast(s) ||
2445 ipv4_is_lbcast(s) ||
2446 ipv4_is_zeronet(s) ||
2447 ipv4_is_local_multicast(s)) {
2448 t = random32() % (imx - imn) + imn;
2449 s = htonl(t);
2451 pkt_dev->cur_daddr = s;
2452 } else {
2453 t = ntohl(pkt_dev->cur_daddr);
2454 t++;
2455 if (t > imx) {
2456 t = imn;
2458 pkt_dev->cur_daddr = htonl(t);
2461 if (pkt_dev->cflows) {
2462 pkt_dev->flows[flow].flags |= F_INIT;
2463 pkt_dev->flows[flow].cur_daddr =
2464 pkt_dev->cur_daddr;
2465 #ifdef CONFIG_XFRM
2466 if (pkt_dev->flags & F_IPSEC_ON)
2467 get_ipsec_sa(pkt_dev, flow);
2468 #endif
2469 pkt_dev->nflows++;
2472 } else { /* IPV6 * */
2474 if (pkt_dev->min_in6_daddr.s6_addr32[0] == 0 &&
2475 pkt_dev->min_in6_daddr.s6_addr32[1] == 0 &&
2476 pkt_dev->min_in6_daddr.s6_addr32[2] == 0 &&
2477 pkt_dev->min_in6_daddr.s6_addr32[3] == 0) ;
2478 else {
2479 int i;
2481 /* Only random destinations yet */
2483 for (i = 0; i < 4; i++) {
2484 pkt_dev->cur_in6_daddr.s6_addr32[i] =
2485 (((__force __be32)random32() |
2486 pkt_dev->min_in6_daddr.s6_addr32[i]) &
2487 pkt_dev->max_in6_daddr.s6_addr32[i]);
2492 if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
2493 __u32 t;
2494 if (pkt_dev->flags & F_TXSIZE_RND) {
2495 t = random32() %
2496 (pkt_dev->max_pkt_size - pkt_dev->min_pkt_size)
2497 + pkt_dev->min_pkt_size;
2498 } else {
2499 t = pkt_dev->cur_pkt_size + 1;
2500 if (t > pkt_dev->max_pkt_size)
2501 t = pkt_dev->min_pkt_size;
2503 pkt_dev->cur_pkt_size = t;
2506 set_cur_queue_map(pkt_dev);
2508 pkt_dev->flows[flow].count++;
2512 #ifdef CONFIG_XFRM
2513 static int pktgen_output_ipsec(struct sk_buff *skb, struct pktgen_dev *pkt_dev)
2515 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2516 int err = 0;
2518 if (!x)
2519 return 0;
2520 /* XXX: we dont support tunnel mode for now until
2521 * we resolve the dst issue */
2522 if (x->props.mode != XFRM_MODE_TRANSPORT)
2523 return 0;
2525 spin_lock(&x->lock);
2527 err = x->outer_mode->output(x, skb);
2528 if (err)
2529 goto error;
2530 err = x->type->output(x, skb);
2531 if (err)
2532 goto error;
2534 x->curlft.bytes += skb->len;
2535 x->curlft.packets++;
2536 error:
2537 spin_unlock(&x->lock);
2538 return err;
2541 static void free_SAs(struct pktgen_dev *pkt_dev)
2543 if (pkt_dev->cflows) {
2544 /* let go of the SAs if we have them */
2545 int i;
2546 for (i = 0; i < pkt_dev->cflows; i++) {
2547 struct xfrm_state *x = pkt_dev->flows[i].x;
2548 if (x) {
2549 xfrm_state_put(x);
2550 pkt_dev->flows[i].x = NULL;
2556 static int process_ipsec(struct pktgen_dev *pkt_dev,
2557 struct sk_buff *skb, __be16 protocol)
2559 if (pkt_dev->flags & F_IPSEC_ON) {
2560 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2561 int nhead = 0;
2562 if (x) {
2563 int ret;
2564 __u8 *eth;
2565 nhead = x->props.header_len - skb_headroom(skb);
2566 if (nhead > 0) {
2567 ret = pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
2568 if (ret < 0) {
2569 pr_err("Error expanding ipsec packet %d\n",
2570 ret);
2571 goto err;
2575 /* ipsec is not expecting ll header */
2576 skb_pull(skb, ETH_HLEN);
2577 ret = pktgen_output_ipsec(skb, pkt_dev);
2578 if (ret) {
2579 pr_err("Error creating ipsec packet %d\n", ret);
2580 goto err;
2582 /* restore ll */
2583 eth = (__u8 *) skb_push(skb, ETH_HLEN);
2584 memcpy(eth, pkt_dev->hh, 12);
2585 *(u16 *) &eth[12] = protocol;
2588 return 1;
2589 err:
2590 kfree_skb(skb);
2591 return 0;
2593 #endif
2595 static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
2597 unsigned i;
2598 for (i = 0; i < pkt_dev->nr_labels; i++)
2599 *mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
2601 mpls--;
2602 *mpls |= MPLS_STACK_BOTTOM;
2605 static inline __be16 build_tci(unsigned int id, unsigned int cfi,
2606 unsigned int prio)
2608 return htons(id | (cfi << 12) | (prio << 13));
2611 static void pktgen_finalize_skb(struct pktgen_dev *pkt_dev, struct sk_buff *skb,
2612 int datalen)
2614 struct timeval timestamp;
2615 struct pktgen_hdr *pgh;
2617 pgh = (struct pktgen_hdr *)skb_put(skb, sizeof(*pgh));
2618 datalen -= sizeof(*pgh);
2620 if (pkt_dev->nfrags <= 0) {
2621 memset(skb_put(skb, datalen), 0, datalen);
2622 } else {
2623 int frags = pkt_dev->nfrags;
2624 int i, len;
2627 if (frags > MAX_SKB_FRAGS)
2628 frags = MAX_SKB_FRAGS;
2629 len = datalen - frags * PAGE_SIZE;
2630 if (len > 0) {
2631 memset(skb_put(skb, len), 0, len);
2632 datalen = frags * PAGE_SIZE;
2635 i = 0;
2636 while (datalen > 0) {
2637 if (unlikely(!pkt_dev->page)) {
2638 int node = numa_node_id();
2640 if (pkt_dev->node >= 0 && (pkt_dev->flags & F_NODE))
2641 node = pkt_dev->node;
2642 pkt_dev->page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
2643 if (!pkt_dev->page)
2644 break;
2646 skb_shinfo(skb)->frags[i].page = pkt_dev->page;
2647 get_page(pkt_dev->page);
2648 skb_shinfo(skb)->frags[i].page_offset = 0;
2649 skb_shinfo(skb)->frags[i].size =
2650 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE);
2651 datalen -= skb_shinfo(skb)->frags[i].size;
2652 skb->len += skb_shinfo(skb)->frags[i].size;
2653 skb->data_len += skb_shinfo(skb)->frags[i].size;
2654 i++;
2655 skb_shinfo(skb)->nr_frags = i;
2658 while (i < frags) {
2659 int rem;
2661 if (i == 0)
2662 break;
2664 rem = skb_shinfo(skb)->frags[i - 1].size / 2;
2665 if (rem == 0)
2666 break;
2668 skb_shinfo(skb)->frags[i - 1].size -= rem;
2670 skb_shinfo(skb)->frags[i] =
2671 skb_shinfo(skb)->frags[i - 1];
2672 get_page(skb_shinfo(skb)->frags[i].page);
2673 skb_shinfo(skb)->frags[i].page =
2674 skb_shinfo(skb)->frags[i - 1].page;
2675 skb_shinfo(skb)->frags[i].page_offset +=
2676 skb_shinfo(skb)->frags[i - 1].size;
2677 skb_shinfo(skb)->frags[i].size = rem;
2678 i++;
2679 skb_shinfo(skb)->nr_frags = i;
2683 /* Stamp the time, and sequence number,
2684 * convert them to network byte order
2686 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2687 pgh->seq_num = htonl(pkt_dev->seq_num);
2689 do_gettimeofday(&timestamp);
2690 pgh->tv_sec = htonl(timestamp.tv_sec);
2691 pgh->tv_usec = htonl(timestamp.tv_usec);
2694 static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
2695 struct pktgen_dev *pkt_dev)
2697 struct sk_buff *skb = NULL;
2698 __u8 *eth;
2699 struct udphdr *udph;
2700 int datalen, iplen;
2701 struct iphdr *iph;
2702 __be16 protocol = htons(ETH_P_IP);
2703 __be32 *mpls;
2704 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2705 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2706 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2707 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2708 u16 queue_map;
2710 if (pkt_dev->nr_labels)
2711 protocol = htons(ETH_P_MPLS_UC);
2713 if (pkt_dev->vlan_id != 0xffff)
2714 protocol = htons(ETH_P_8021Q);
2716 /* Update any of the values, used when we're incrementing various
2717 * fields.
2719 mod_cur_headers(pkt_dev);
2720 queue_map = pkt_dev->cur_queue_map;
2722 datalen = (odev->hard_header_len + 16) & ~0xf;
2724 if (pkt_dev->flags & F_NODE) {
2725 int node;
2727 if (pkt_dev->node >= 0)
2728 node = pkt_dev->node;
2729 else
2730 node = numa_node_id();
2732 skb = __alloc_skb(NET_SKB_PAD + pkt_dev->cur_pkt_size + 64
2733 + datalen + pkt_dev->pkt_overhead, GFP_NOWAIT, 0, node);
2734 if (likely(skb)) {
2735 skb_reserve(skb, NET_SKB_PAD);
2736 skb->dev = odev;
2739 else
2740 skb = __netdev_alloc_skb(odev,
2741 pkt_dev->cur_pkt_size + 64
2742 + datalen + pkt_dev->pkt_overhead, GFP_NOWAIT);
2744 if (!skb) {
2745 sprintf(pkt_dev->result, "No memory");
2746 return NULL;
2748 prefetchw(skb->data);
2750 skb_reserve(skb, datalen);
2752 /* Reserve for ethernet and IP header */
2753 eth = (__u8 *) skb_push(skb, 14);
2754 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
2755 if (pkt_dev->nr_labels)
2756 mpls_push(mpls, pkt_dev);
2758 if (pkt_dev->vlan_id != 0xffff) {
2759 if (pkt_dev->svlan_id != 0xffff) {
2760 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2761 *svlan_tci = build_tci(pkt_dev->svlan_id,
2762 pkt_dev->svlan_cfi,
2763 pkt_dev->svlan_p);
2764 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2765 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
2767 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
2768 *vlan_tci = build_tci(pkt_dev->vlan_id,
2769 pkt_dev->vlan_cfi,
2770 pkt_dev->vlan_p);
2771 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
2772 *vlan_encapsulated_proto = htons(ETH_P_IP);
2775 skb->network_header = skb->tail;
2776 skb->transport_header = skb->network_header + sizeof(struct iphdr);
2777 skb_put(skb, sizeof(struct iphdr) + sizeof(struct udphdr));
2778 skb_set_queue_mapping(skb, queue_map);
2779 skb->priority = pkt_dev->skb_priority;
2781 iph = ip_hdr(skb);
2782 udph = udp_hdr(skb);
2784 memcpy(eth, pkt_dev->hh, 12);
2785 *(__be16 *) & eth[12] = protocol;
2787 /* Eth + IPh + UDPh + mpls */
2788 datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
2789 pkt_dev->pkt_overhead;
2790 if (datalen < sizeof(struct pktgen_hdr))
2791 datalen = sizeof(struct pktgen_hdr);
2793 udph->source = htons(pkt_dev->cur_udp_src);
2794 udph->dest = htons(pkt_dev->cur_udp_dst);
2795 udph->len = htons(datalen + 8); /* DATA + udphdr */
2796 udph->check = 0; /* No checksum */
2798 iph->ihl = 5;
2799 iph->version = 4;
2800 iph->ttl = 32;
2801 iph->tos = pkt_dev->tos;
2802 iph->protocol = IPPROTO_UDP; /* UDP */
2803 iph->saddr = pkt_dev->cur_saddr;
2804 iph->daddr = pkt_dev->cur_daddr;
2805 iph->id = htons(pkt_dev->ip_id);
2806 pkt_dev->ip_id++;
2807 iph->frag_off = 0;
2808 iplen = 20 + 8 + datalen;
2809 iph->tot_len = htons(iplen);
2810 iph->check = 0;
2811 iph->check = ip_fast_csum((void *)iph, iph->ihl);
2812 skb->protocol = protocol;
2813 skb->mac_header = (skb->network_header - ETH_HLEN -
2814 pkt_dev->pkt_overhead);
2815 skb->dev = odev;
2816 skb->pkt_type = PACKET_HOST;
2817 pktgen_finalize_skb(pkt_dev, skb, datalen);
2819 #ifdef CONFIG_XFRM
2820 if (!process_ipsec(pkt_dev, skb, protocol))
2821 return NULL;
2822 #endif
2824 return skb;
2828 * scan_ip6, fmt_ip taken from dietlibc-0.21
2829 * Author Felix von Leitner <felix-dietlibc@fefe.de>
2831 * Slightly modified for kernel.
2832 * Should be candidate for net/ipv4/utils.c
2833 * --ro
2836 static unsigned int scan_ip6(const char *s, char ip[16])
2838 unsigned int i;
2839 unsigned int len = 0;
2840 unsigned long u;
2841 char suffix[16];
2842 unsigned int prefixlen = 0;
2843 unsigned int suffixlen = 0;
2844 __be32 tmp;
2845 char *pos;
2847 for (i = 0; i < 16; i++)
2848 ip[i] = 0;
2850 for (;;) {
2851 if (*s == ':') {
2852 len++;
2853 if (s[1] == ':') { /* Found "::", skip to part 2 */
2854 s += 2;
2855 len++;
2856 break;
2858 s++;
2861 u = simple_strtoul(s, &pos, 16);
2862 i = pos - s;
2863 if (!i)
2864 return 0;
2865 if (prefixlen == 12 && s[i] == '.') {
2867 /* the last 4 bytes may be written as IPv4 address */
2869 tmp = in_aton(s);
2870 memcpy((struct in_addr *)(ip + 12), &tmp, sizeof(tmp));
2871 return i + len;
2873 ip[prefixlen++] = (u >> 8);
2874 ip[prefixlen++] = (u & 255);
2875 s += i;
2876 len += i;
2877 if (prefixlen == 16)
2878 return len;
2881 /* part 2, after "::" */
2882 for (;;) {
2883 if (*s == ':') {
2884 if (suffixlen == 0)
2885 break;
2886 s++;
2887 len++;
2888 } else if (suffixlen != 0)
2889 break;
2891 u = simple_strtol(s, &pos, 16);
2892 i = pos - s;
2893 if (!i) {
2894 if (*s)
2895 len--;
2896 break;
2898 if (suffixlen + prefixlen <= 12 && s[i] == '.') {
2899 tmp = in_aton(s);
2900 memcpy((struct in_addr *)(suffix + suffixlen), &tmp,
2901 sizeof(tmp));
2902 suffixlen += 4;
2903 len += strlen(s);
2904 break;
2906 suffix[suffixlen++] = (u >> 8);
2907 suffix[suffixlen++] = (u & 255);
2908 s += i;
2909 len += i;
2910 if (prefixlen + suffixlen == 16)
2911 break;
2913 for (i = 0; i < suffixlen; i++)
2914 ip[16 - suffixlen + i] = suffix[i];
2915 return len;
2918 static char tohex(char hexdigit)
2920 return hexdigit > 9 ? hexdigit + 'a' - 10 : hexdigit + '0';
2923 static int fmt_xlong(char *s, unsigned int i)
2925 char *bak = s;
2926 *s = tohex((i >> 12) & 0xf);
2927 if (s != bak || *s != '0')
2928 ++s;
2929 *s = tohex((i >> 8) & 0xf);
2930 if (s != bak || *s != '0')
2931 ++s;
2932 *s = tohex((i >> 4) & 0xf);
2933 if (s != bak || *s != '0')
2934 ++s;
2935 *s = tohex(i & 0xf);
2936 return s - bak + 1;
2939 static unsigned int fmt_ip6(char *s, const char ip[16])
2941 unsigned int len;
2942 unsigned int i;
2943 unsigned int temp;
2944 unsigned int compressing;
2945 int j;
2947 len = 0;
2948 compressing = 0;
2949 for (j = 0; j < 16; j += 2) {
2951 #ifdef V4MAPPEDPREFIX
2952 if (j == 12 && !memcmp(ip, V4mappedprefix, 12)) {
2953 inet_ntoa_r(*(struct in_addr *)(ip + 12), s);
2954 temp = strlen(s);
2955 return len + temp;
2957 #endif
2958 temp = ((unsigned long)(unsigned char)ip[j] << 8) +
2959 (unsigned long)(unsigned char)ip[j + 1];
2960 if (temp == 0) {
2961 if (!compressing) {
2962 compressing = 1;
2963 if (j == 0) {
2964 *s++ = ':';
2965 ++len;
2968 } else {
2969 if (compressing) {
2970 compressing = 0;
2971 *s++ = ':';
2972 ++len;
2974 i = fmt_xlong(s, temp);
2975 len += i;
2976 s += i;
2977 if (j < 14) {
2978 *s++ = ':';
2979 ++len;
2983 if (compressing) {
2984 *s++ = ':';
2985 ++len;
2987 *s = 0;
2988 return len;
2991 static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
2992 struct pktgen_dev *pkt_dev)
2994 struct sk_buff *skb = NULL;
2995 __u8 *eth;
2996 struct udphdr *udph;
2997 int datalen;
2998 struct ipv6hdr *iph;
2999 __be16 protocol = htons(ETH_P_IPV6);
3000 __be32 *mpls;
3001 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
3002 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
3003 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
3004 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
3005 u16 queue_map;
3007 if (pkt_dev->nr_labels)
3008 protocol = htons(ETH_P_MPLS_UC);
3010 if (pkt_dev->vlan_id != 0xffff)
3011 protocol = htons(ETH_P_8021Q);
3013 /* Update any of the values, used when we're incrementing various
3014 * fields.
3016 mod_cur_headers(pkt_dev);
3017 queue_map = pkt_dev->cur_queue_map;
3019 skb = __netdev_alloc_skb(odev,
3020 pkt_dev->cur_pkt_size + 64
3021 + 16 + pkt_dev->pkt_overhead, GFP_NOWAIT);
3022 if (!skb) {
3023 sprintf(pkt_dev->result, "No memory");
3024 return NULL;
3026 prefetchw(skb->data);
3028 skb_reserve(skb, 16);
3030 /* Reserve for ethernet and IP header */
3031 eth = (__u8 *) skb_push(skb, 14);
3032 mpls = (__be32 *)skb_put(skb, pkt_dev->nr_labels*sizeof(__u32));
3033 if (pkt_dev->nr_labels)
3034 mpls_push(mpls, pkt_dev);
3036 if (pkt_dev->vlan_id != 0xffff) {
3037 if (pkt_dev->svlan_id != 0xffff) {
3038 svlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
3039 *svlan_tci = build_tci(pkt_dev->svlan_id,
3040 pkt_dev->svlan_cfi,
3041 pkt_dev->svlan_p);
3042 svlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
3043 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
3045 vlan_tci = (__be16 *)skb_put(skb, sizeof(__be16));
3046 *vlan_tci = build_tci(pkt_dev->vlan_id,
3047 pkt_dev->vlan_cfi,
3048 pkt_dev->vlan_p);
3049 vlan_encapsulated_proto = (__be16 *)skb_put(skb, sizeof(__be16));
3050 *vlan_encapsulated_proto = htons(ETH_P_IPV6);
3053 skb->network_header = skb->tail;
3054 skb->transport_header = skb->network_header + sizeof(struct ipv6hdr);
3055 skb_put(skb, sizeof(struct ipv6hdr) + sizeof(struct udphdr));
3056 skb_set_queue_mapping(skb, queue_map);
3057 skb->priority = pkt_dev->skb_priority;
3058 iph = ipv6_hdr(skb);
3059 udph = udp_hdr(skb);
3061 memcpy(eth, pkt_dev->hh, 12);
3062 *(__be16 *) &eth[12] = protocol;
3064 /* Eth + IPh + UDPh + mpls */
3065 datalen = pkt_dev->cur_pkt_size - 14 -
3066 sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
3067 pkt_dev->pkt_overhead;
3069 if (datalen < sizeof(struct pktgen_hdr)) {
3070 datalen = sizeof(struct pktgen_hdr);
3071 if (net_ratelimit())
3072 pr_info("increased datalen to %d\n", datalen);
3075 udph->source = htons(pkt_dev->cur_udp_src);
3076 udph->dest = htons(pkt_dev->cur_udp_dst);
3077 udph->len = htons(datalen + sizeof(struct udphdr));
3078 udph->check = 0; /* No checksum */
3080 *(__be32 *) iph = htonl(0x60000000); /* Version + flow */
3082 if (pkt_dev->traffic_class) {
3083 /* Version + traffic class + flow (0) */
3084 *(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
3087 iph->hop_limit = 32;
3089 iph->payload_len = htons(sizeof(struct udphdr) + datalen);
3090 iph->nexthdr = IPPROTO_UDP;
3092 ipv6_addr_copy(&iph->daddr, &pkt_dev->cur_in6_daddr);
3093 ipv6_addr_copy(&iph->saddr, &pkt_dev->cur_in6_saddr);
3095 skb->mac_header = (skb->network_header - ETH_HLEN -
3096 pkt_dev->pkt_overhead);
3097 skb->protocol = protocol;
3098 skb->dev = odev;
3099 skb->pkt_type = PACKET_HOST;
3101 pktgen_finalize_skb(pkt_dev, skb, datalen);
3103 return skb;
3106 static struct sk_buff *fill_packet(struct net_device *odev,
3107 struct pktgen_dev *pkt_dev)
3109 if (pkt_dev->flags & F_IPV6)
3110 return fill_packet_ipv6(odev, pkt_dev);
3111 else
3112 return fill_packet_ipv4(odev, pkt_dev);
3115 static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
3117 pkt_dev->seq_num = 1;
3118 pkt_dev->idle_acc = 0;
3119 pkt_dev->sofar = 0;
3120 pkt_dev->tx_bytes = 0;
3121 pkt_dev->errors = 0;
3124 /* Set up structure for sending pkts, clear counters */
3126 static void pktgen_run(struct pktgen_thread *t)
3128 struct pktgen_dev *pkt_dev;
3129 int started = 0;
3131 func_enter();
3133 if_lock(t);
3134 list_for_each_entry(pkt_dev, &t->if_list, list) {
3137 * setup odev and create initial packet.
3139 pktgen_setup_inject(pkt_dev);
3141 if (pkt_dev->odev) {
3142 pktgen_clear_counters(pkt_dev);
3143 pkt_dev->running = 1; /* Cranke yeself! */
3144 pkt_dev->skb = NULL;
3145 pkt_dev->started_at =
3146 pkt_dev->next_tx = ktime_now();
3148 set_pkt_overhead(pkt_dev);
3150 strcpy(pkt_dev->result, "Starting");
3151 started++;
3152 } else
3153 strcpy(pkt_dev->result, "Error starting");
3155 if_unlock(t);
3156 if (started)
3157 t->control &= ~(T_STOP);
3160 static void pktgen_stop_all_threads_ifs(void)
3162 struct pktgen_thread *t;
3164 func_enter();
3166 mutex_lock(&pktgen_thread_lock);
3168 list_for_each_entry(t, &pktgen_threads, th_list)
3169 t->control |= T_STOP;
3171 mutex_unlock(&pktgen_thread_lock);
3174 static int thread_is_running(const struct pktgen_thread *t)
3176 const struct pktgen_dev *pkt_dev;
3178 list_for_each_entry(pkt_dev, &t->if_list, list)
3179 if (pkt_dev->running)
3180 return 1;
3181 return 0;
3184 static int pktgen_wait_thread_run(struct pktgen_thread *t)
3186 if_lock(t);
3188 while (thread_is_running(t)) {
3190 if_unlock(t);
3192 msleep_interruptible(100);
3194 if (signal_pending(current))
3195 goto signal;
3196 if_lock(t);
3198 if_unlock(t);
3199 return 1;
3200 signal:
3201 return 0;
3204 static int pktgen_wait_all_threads_run(void)
3206 struct pktgen_thread *t;
3207 int sig = 1;
3209 mutex_lock(&pktgen_thread_lock);
3211 list_for_each_entry(t, &pktgen_threads, th_list) {
3212 sig = pktgen_wait_thread_run(t);
3213 if (sig == 0)
3214 break;
3217 if (sig == 0)
3218 list_for_each_entry(t, &pktgen_threads, th_list)
3219 t->control |= (T_STOP);
3221 mutex_unlock(&pktgen_thread_lock);
3222 return sig;
3225 static void pktgen_run_all_threads(void)
3227 struct pktgen_thread *t;
3229 func_enter();
3231 mutex_lock(&pktgen_thread_lock);
3233 list_for_each_entry(t, &pktgen_threads, th_list)
3234 t->control |= (T_RUN);
3236 mutex_unlock(&pktgen_thread_lock);
3238 /* Propagate thread->control */
3239 schedule_timeout_interruptible(msecs_to_jiffies(125));
3241 pktgen_wait_all_threads_run();
3244 static void pktgen_reset_all_threads(void)
3246 struct pktgen_thread *t;
3248 func_enter();
3250 mutex_lock(&pktgen_thread_lock);
3252 list_for_each_entry(t, &pktgen_threads, th_list)
3253 t->control |= (T_REMDEVALL);
3255 mutex_unlock(&pktgen_thread_lock);
3257 /* Propagate thread->control */
3258 schedule_timeout_interruptible(msecs_to_jiffies(125));
3260 pktgen_wait_all_threads_run();
3263 static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
3265 __u64 bps, mbps, pps;
3266 char *p = pkt_dev->result;
3267 ktime_t elapsed = ktime_sub(pkt_dev->stopped_at,
3268 pkt_dev->started_at);
3269 ktime_t idle = ns_to_ktime(pkt_dev->idle_acc);
3271 p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
3272 (unsigned long long)ktime_to_us(elapsed),
3273 (unsigned long long)ktime_to_us(ktime_sub(elapsed, idle)),
3274 (unsigned long long)ktime_to_us(idle),
3275 (unsigned long long)pkt_dev->sofar,
3276 pkt_dev->cur_pkt_size, nr_frags);
3278 pps = div64_u64(pkt_dev->sofar * NSEC_PER_SEC,
3279 ktime_to_ns(elapsed));
3281 bps = pps * 8 * pkt_dev->cur_pkt_size;
3283 mbps = bps;
3284 do_div(mbps, 1000000);
3285 p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu",
3286 (unsigned long long)pps,
3287 (unsigned long long)mbps,
3288 (unsigned long long)bps,
3289 (unsigned long long)pkt_dev->errors);
3292 /* Set stopped-at timer, remove from running list, do counters & statistics */
3293 static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
3295 int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
3297 if (!pkt_dev->running) {
3298 pr_warning("interface: %s is already stopped\n",
3299 pkt_dev->odevname);
3300 return -EINVAL;
3303 kfree_skb(pkt_dev->skb);
3304 pkt_dev->skb = NULL;
3305 pkt_dev->stopped_at = ktime_now();
3306 pkt_dev->running = 0;
3308 show_results(pkt_dev, nr_frags);
3310 return 0;
3313 static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
3315 struct pktgen_dev *pkt_dev, *best = NULL;
3317 if_lock(t);
3319 list_for_each_entry(pkt_dev, &t->if_list, list) {
3320 if (!pkt_dev->running)
3321 continue;
3322 if (best == NULL)
3323 best = pkt_dev;
3324 else if (ktime_lt(pkt_dev->next_tx, best->next_tx))
3325 best = pkt_dev;
3327 if_unlock(t);
3328 return best;
3331 static void pktgen_stop(struct pktgen_thread *t)
3333 struct pktgen_dev *pkt_dev;
3335 func_enter();
3337 if_lock(t);
3339 list_for_each_entry(pkt_dev, &t->if_list, list) {
3340 pktgen_stop_device(pkt_dev);
3343 if_unlock(t);
3347 * one of our devices needs to be removed - find it
3348 * and remove it
3350 static void pktgen_rem_one_if(struct pktgen_thread *t)
3352 struct list_head *q, *n;
3353 struct pktgen_dev *cur;
3355 func_enter();
3357 if_lock(t);
3359 list_for_each_safe(q, n, &t->if_list) {
3360 cur = list_entry(q, struct pktgen_dev, list);
3362 if (!cur->removal_mark)
3363 continue;
3365 kfree_skb(cur->skb);
3366 cur->skb = NULL;
3368 pktgen_remove_device(t, cur);
3370 break;
3373 if_unlock(t);
3376 static void pktgen_rem_all_ifs(struct pktgen_thread *t)
3378 struct list_head *q, *n;
3379 struct pktgen_dev *cur;
3381 func_enter();
3383 /* Remove all devices, free mem */
3385 if_lock(t);
3387 list_for_each_safe(q, n, &t->if_list) {
3388 cur = list_entry(q, struct pktgen_dev, list);
3390 kfree_skb(cur->skb);
3391 cur->skb = NULL;
3393 pktgen_remove_device(t, cur);
3396 if_unlock(t);
3399 static void pktgen_rem_thread(struct pktgen_thread *t)
3401 /* Remove from the thread list */
3403 remove_proc_entry(t->tsk->comm, pg_proc_dir);
3407 static void pktgen_resched(struct pktgen_dev *pkt_dev)
3409 ktime_t idle_start = ktime_now();
3410 schedule();
3411 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_now(), idle_start));
3414 static void pktgen_wait_for_skb(struct pktgen_dev *pkt_dev)
3416 ktime_t idle_start = ktime_now();
3418 while (atomic_read(&(pkt_dev->skb->users)) != 1) {
3419 if (signal_pending(current))
3420 break;
3422 if (need_resched())
3423 pktgen_resched(pkt_dev);
3424 else
3425 cpu_relax();
3427 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_now(), idle_start));
3430 static void pktgen_xmit(struct pktgen_dev *pkt_dev)
3432 struct net_device *odev = pkt_dev->odev;
3433 netdev_tx_t (*xmit)(struct sk_buff *, struct net_device *)
3434 = odev->netdev_ops->ndo_start_xmit;
3435 struct netdev_queue *txq;
3436 u16 queue_map;
3437 int ret;
3439 /* If device is offline, then don't send */
3440 if (unlikely(!netif_running(odev) || !netif_carrier_ok(odev))) {
3441 pktgen_stop_device(pkt_dev);
3442 return;
3445 /* This is max DELAY, this has special meaning of
3446 * "never transmit"
3448 if (unlikely(pkt_dev->delay == ULLONG_MAX)) {
3449 pkt_dev->next_tx = ktime_add_ns(ktime_now(), ULONG_MAX);
3450 return;
3453 /* If no skb or clone count exhausted then get new one */
3454 if (!pkt_dev->skb || (pkt_dev->last_ok &&
3455 ++pkt_dev->clone_count >= pkt_dev->clone_skb)) {
3456 /* build a new pkt */
3457 kfree_skb(pkt_dev->skb);
3459 pkt_dev->skb = fill_packet(odev, pkt_dev);
3460 if (pkt_dev->skb == NULL) {
3461 pr_err("ERROR: couldn't allocate skb in fill_packet\n");
3462 schedule();
3463 pkt_dev->clone_count--; /* back out increment, OOM */
3464 return;
3466 pkt_dev->last_pkt_size = pkt_dev->skb->len;
3467 pkt_dev->allocated_skbs++;
3468 pkt_dev->clone_count = 0; /* reset counter */
3471 if (pkt_dev->delay && pkt_dev->last_ok)
3472 spin(pkt_dev, pkt_dev->next_tx);
3474 queue_map = skb_get_queue_mapping(pkt_dev->skb);
3475 txq = netdev_get_tx_queue(odev, queue_map);
3477 __netif_tx_lock_bh(txq);
3479 if (unlikely(netif_tx_queue_frozen_or_stopped(txq))) {
3480 ret = NETDEV_TX_BUSY;
3481 pkt_dev->last_ok = 0;
3482 goto unlock;
3484 atomic_inc(&(pkt_dev->skb->users));
3485 ret = (*xmit)(pkt_dev->skb, odev);
3487 switch (ret) {
3488 case NETDEV_TX_OK:
3489 txq_trans_update(txq);
3490 pkt_dev->last_ok = 1;
3491 pkt_dev->sofar++;
3492 pkt_dev->seq_num++;
3493 pkt_dev->tx_bytes += pkt_dev->last_pkt_size;
3494 break;
3495 case NET_XMIT_DROP:
3496 case NET_XMIT_CN:
3497 case NET_XMIT_POLICED:
3498 /* skb has been consumed */
3499 pkt_dev->errors++;
3500 break;
3501 default: /* Drivers are not supposed to return other values! */
3502 if (net_ratelimit())
3503 pr_info("%s xmit error: %d\n", pkt_dev->odevname, ret);
3504 pkt_dev->errors++;
3505 /* fallthru */
3506 case NETDEV_TX_LOCKED:
3507 case NETDEV_TX_BUSY:
3508 /* Retry it next time */
3509 atomic_dec(&(pkt_dev->skb->users));
3510 pkt_dev->last_ok = 0;
3512 unlock:
3513 __netif_tx_unlock_bh(txq);
3515 /* If pkt_dev->count is zero, then run forever */
3516 if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
3517 pktgen_wait_for_skb(pkt_dev);
3519 /* Done with this */
3520 pktgen_stop_device(pkt_dev);
3525 * Main loop of the thread goes here
3528 static int pktgen_thread_worker(void *arg)
3530 DEFINE_WAIT(wait);
3531 struct pktgen_thread *t = arg;
3532 struct pktgen_dev *pkt_dev = NULL;
3533 int cpu = t->cpu;
3535 BUG_ON(smp_processor_id() != cpu);
3537 init_waitqueue_head(&t->queue);
3538 complete(&t->start_done);
3540 pr_debug("starting pktgen/%d: pid=%d\n", cpu, task_pid_nr(current));
3542 set_current_state(TASK_INTERRUPTIBLE);
3544 set_freezable();
3546 while (!kthread_should_stop()) {
3547 pkt_dev = next_to_run(t);
3549 if (unlikely(!pkt_dev && t->control == 0)) {
3550 if (pktgen_exiting)
3551 break;
3552 wait_event_interruptible_timeout(t->queue,
3553 t->control != 0,
3554 HZ/10);
3555 try_to_freeze();
3556 continue;
3559 __set_current_state(TASK_RUNNING);
3561 if (likely(pkt_dev)) {
3562 pktgen_xmit(pkt_dev);
3564 if (need_resched())
3565 pktgen_resched(pkt_dev);
3566 else
3567 cpu_relax();
3570 if (t->control & T_STOP) {
3571 pktgen_stop(t);
3572 t->control &= ~(T_STOP);
3575 if (t->control & T_RUN) {
3576 pktgen_run(t);
3577 t->control &= ~(T_RUN);
3580 if (t->control & T_REMDEVALL) {
3581 pktgen_rem_all_ifs(t);
3582 t->control &= ~(T_REMDEVALL);
3585 if (t->control & T_REMDEV) {
3586 pktgen_rem_one_if(t);
3587 t->control &= ~(T_REMDEV);
3590 try_to_freeze();
3592 set_current_state(TASK_INTERRUPTIBLE);
3595 pr_debug("%s stopping all device\n", t->tsk->comm);
3596 pktgen_stop(t);
3598 pr_debug("%s removing all device\n", t->tsk->comm);
3599 pktgen_rem_all_ifs(t);
3601 pr_debug("%s removing thread\n", t->tsk->comm);
3602 pktgen_rem_thread(t);
3604 /* Wait for kthread_stop */
3605 while (!kthread_should_stop()) {
3606 set_current_state(TASK_INTERRUPTIBLE);
3607 schedule();
3609 __set_current_state(TASK_RUNNING);
3611 return 0;
3614 static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
3615 const char *ifname, bool exact)
3617 struct pktgen_dev *p, *pkt_dev = NULL;
3618 size_t len = strlen(ifname);
3620 if_lock(t);
3621 list_for_each_entry(p, &t->if_list, list)
3622 if (strncmp(p->odevname, ifname, len) == 0) {
3623 if (p->odevname[len]) {
3624 if (exact || p->odevname[len] != '@')
3625 continue;
3627 pkt_dev = p;
3628 break;
3631 if_unlock(t);
3632 pr_debug("find_dev(%s) returning %p\n", ifname, pkt_dev);
3633 return pkt_dev;
3637 * Adds a dev at front of if_list.
3640 static int add_dev_to_thread(struct pktgen_thread *t,
3641 struct pktgen_dev *pkt_dev)
3643 int rv = 0;
3645 if_lock(t);
3647 if (pkt_dev->pg_thread) {
3648 pr_err("ERROR: already assigned to a thread\n");
3649 rv = -EBUSY;
3650 goto out;
3653 list_add(&pkt_dev->list, &t->if_list);
3654 pkt_dev->pg_thread = t;
3655 pkt_dev->running = 0;
3657 out:
3658 if_unlock(t);
3659 return rv;
3662 /* Called under thread lock */
3664 static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
3666 struct pktgen_dev *pkt_dev;
3667 int err;
3668 int node = cpu_to_node(t->cpu);
3670 /* We don't allow a device to be on several threads */
3672 pkt_dev = __pktgen_NN_threads(ifname, FIND);
3673 if (pkt_dev) {
3674 pr_err("ERROR: interface already used\n");
3675 return -EBUSY;
3678 pkt_dev = kzalloc_node(sizeof(struct pktgen_dev), GFP_KERNEL, node);
3679 if (!pkt_dev)
3680 return -ENOMEM;
3682 strcpy(pkt_dev->odevname, ifname);
3683 pkt_dev->flows = vmalloc_node(MAX_CFLOWS * sizeof(struct flow_state),
3684 node);
3685 if (pkt_dev->flows == NULL) {
3686 kfree(pkt_dev);
3687 return -ENOMEM;
3689 memset(pkt_dev->flows, 0, MAX_CFLOWS * sizeof(struct flow_state));
3691 pkt_dev->removal_mark = 0;
3692 pkt_dev->min_pkt_size = ETH_ZLEN;
3693 pkt_dev->max_pkt_size = ETH_ZLEN;
3694 pkt_dev->nfrags = 0;
3695 pkt_dev->clone_skb = pg_clone_skb_d;
3696 pkt_dev->delay = pg_delay_d;
3697 pkt_dev->count = pg_count_d;
3698 pkt_dev->sofar = 0;
3699 pkt_dev->udp_src_min = 9; /* sink port */
3700 pkt_dev->udp_src_max = 9;
3701 pkt_dev->udp_dst_min = 9;
3702 pkt_dev->udp_dst_max = 9;
3704 pkt_dev->vlan_p = 0;
3705 pkt_dev->vlan_cfi = 0;
3706 pkt_dev->vlan_id = 0xffff;
3707 pkt_dev->svlan_p = 0;
3708 pkt_dev->svlan_cfi = 0;
3709 pkt_dev->svlan_id = 0xffff;
3710 pkt_dev->node = -1;
3712 err = pktgen_setup_dev(pkt_dev, ifname);
3713 if (err)
3714 goto out1;
3716 pkt_dev->entry = proc_create_data(ifname, 0600, pg_proc_dir,
3717 &pktgen_if_fops, pkt_dev);
3718 if (!pkt_dev->entry) {
3719 pr_err("cannot create %s/%s procfs entry\n",
3720 PG_PROC_DIR, ifname);
3721 err = -EINVAL;
3722 goto out2;
3724 #ifdef CONFIG_XFRM
3725 pkt_dev->ipsmode = XFRM_MODE_TRANSPORT;
3726 pkt_dev->ipsproto = IPPROTO_ESP;
3727 #endif
3729 return add_dev_to_thread(t, pkt_dev);
3730 out2:
3731 dev_put(pkt_dev->odev);
3732 out1:
3733 #ifdef CONFIG_XFRM
3734 free_SAs(pkt_dev);
3735 #endif
3736 vfree(pkt_dev->flows);
3737 kfree(pkt_dev);
3738 return err;
3741 static int __init pktgen_create_thread(int cpu)
3743 struct pktgen_thread *t;
3744 struct proc_dir_entry *pe;
3745 struct task_struct *p;
3747 t = kzalloc_node(sizeof(struct pktgen_thread), GFP_KERNEL,
3748 cpu_to_node(cpu));
3749 if (!t) {
3750 pr_err("ERROR: out of memory, can't create new thread\n");
3751 return -ENOMEM;
3754 spin_lock_init(&t->if_lock);
3755 t->cpu = cpu;
3757 INIT_LIST_HEAD(&t->if_list);
3759 list_add_tail(&t->th_list, &pktgen_threads);
3760 init_completion(&t->start_done);
3762 p = kthread_create_on_node(pktgen_thread_worker,
3764 cpu_to_node(cpu),
3765 "kpktgend_%d", cpu);
3766 if (IS_ERR(p)) {
3767 pr_err("kernel_thread() failed for cpu %d\n", t->cpu);
3768 list_del(&t->th_list);
3769 kfree(t);
3770 return PTR_ERR(p);
3772 kthread_bind(p, cpu);
3773 t->tsk = p;
3775 pe = proc_create_data(t->tsk->comm, 0600, pg_proc_dir,
3776 &pktgen_thread_fops, t);
3777 if (!pe) {
3778 pr_err("cannot create %s/%s procfs entry\n",
3779 PG_PROC_DIR, t->tsk->comm);
3780 kthread_stop(p);
3781 list_del(&t->th_list);
3782 kfree(t);
3783 return -EINVAL;
3786 wake_up_process(p);
3787 wait_for_completion(&t->start_done);
3789 return 0;
3793 * Removes a device from the thread if_list.
3795 static void _rem_dev_from_if_list(struct pktgen_thread *t,
3796 struct pktgen_dev *pkt_dev)
3798 struct list_head *q, *n;
3799 struct pktgen_dev *p;
3801 list_for_each_safe(q, n, &t->if_list) {
3802 p = list_entry(q, struct pktgen_dev, list);
3803 if (p == pkt_dev)
3804 list_del(&p->list);
3808 static int pktgen_remove_device(struct pktgen_thread *t,
3809 struct pktgen_dev *pkt_dev)
3812 pr_debug("remove_device pkt_dev=%p\n", pkt_dev);
3814 if (pkt_dev->running) {
3815 pr_warning("WARNING: trying to remove a running interface, stopping it now\n");
3816 pktgen_stop_device(pkt_dev);
3819 /* Dis-associate from the interface */
3821 if (pkt_dev->odev) {
3822 dev_put(pkt_dev->odev);
3823 pkt_dev->odev = NULL;
3826 /* And update the thread if_list */
3828 _rem_dev_from_if_list(t, pkt_dev);
3830 if (pkt_dev->entry)
3831 remove_proc_entry(pkt_dev->entry->name, pg_proc_dir);
3833 #ifdef CONFIG_XFRM
3834 free_SAs(pkt_dev);
3835 #endif
3836 vfree(pkt_dev->flows);
3837 if (pkt_dev->page)
3838 put_page(pkt_dev->page);
3839 kfree(pkt_dev);
3840 return 0;
3843 static int __init pg_init(void)
3845 int cpu;
3846 struct proc_dir_entry *pe;
3848 pr_info("%s", version);
3850 pg_proc_dir = proc_mkdir(PG_PROC_DIR, init_net.proc_net);
3851 if (!pg_proc_dir)
3852 return -ENODEV;
3854 pe = proc_create(PGCTRL, 0600, pg_proc_dir, &pktgen_fops);
3855 if (pe == NULL) {
3856 pr_err("ERROR: cannot create %s procfs entry\n", PGCTRL);
3857 proc_net_remove(&init_net, PG_PROC_DIR);
3858 return -EINVAL;
3861 /* Register us to receive netdevice events */
3862 register_netdevice_notifier(&pktgen_notifier_block);
3864 for_each_online_cpu(cpu) {
3865 int err;
3867 err = pktgen_create_thread(cpu);
3868 if (err)
3869 pr_warning("WARNING: Cannot create thread for cpu %d (%d)\n",
3870 cpu, err);
3873 if (list_empty(&pktgen_threads)) {
3874 pr_err("ERROR: Initialization failed for all threads\n");
3875 unregister_netdevice_notifier(&pktgen_notifier_block);
3876 remove_proc_entry(PGCTRL, pg_proc_dir);
3877 proc_net_remove(&init_net, PG_PROC_DIR);
3878 return -ENODEV;
3881 return 0;
3884 static void __exit pg_cleanup(void)
3886 struct pktgen_thread *t;
3887 struct list_head *q, *n;
3889 /* Stop all interfaces & threads */
3890 pktgen_exiting = true;
3892 list_for_each_safe(q, n, &pktgen_threads) {
3893 t = list_entry(q, struct pktgen_thread, th_list);
3894 kthread_stop(t->tsk);
3895 kfree(t);
3898 /* Un-register us from receiving netdevice events */
3899 unregister_netdevice_notifier(&pktgen_notifier_block);
3901 /* Clean up proc file system */
3902 remove_proc_entry(PGCTRL, pg_proc_dir);
3903 proc_net_remove(&init_net, PG_PROC_DIR);
3906 module_init(pg_init);
3907 module_exit(pg_cleanup);
3909 MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se>");
3910 MODULE_DESCRIPTION("Packet Generator tool");
3911 MODULE_LICENSE("GPL");
3912 MODULE_VERSION(VERSION);
3913 module_param(pg_count_d, int, 0);
3914 MODULE_PARM_DESC(pg_count_d, "Default number of packets to inject");
3915 module_param(pg_delay_d, int, 0);
3916 MODULE_PARM_DESC(pg_delay_d, "Default delay between packets (nanoseconds)");
3917 module_param(pg_clone_skb_d, int, 0);
3918 MODULE_PARM_DESC(pg_clone_skb_d, "Default number of copies of the same packet");
3919 module_param(debug, int, 0);
3920 MODULE_PARM_DESC(debug, "Enable debugging of pktgen module");