Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / net / core / pktgen.c
blobb8ab5c8295113a8d30b64d8a260d2d7582ead0f3
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.
73 * The if_list is RCU protected, and the if_lock remains to protect updating
74 * of if_list, from "add_device" as it invoked from userspace (via proc write).
76 * By design there should only be *one* "controlling" process. In practice
77 * multiple write accesses gives unpredictable result. Understood by "write"
78 * to /proc gives result code thats should be read be the "writer".
79 * For practical use this should be no problem.
81 * Note when adding devices to a specific CPU there good idea to also assign
82 * /proc/irq/XX/smp_affinity so TX-interrupts gets bound to the same CPU.
83 * --ro
85 * Fix refcount off by one if first packet fails, potential null deref,
86 * memleak 030710- KJP
88 * First "ranges" functionality for ipv6 030726 --ro
90 * Included flow support. 030802 ANK.
92 * Fixed unaligned access on IA-64 Grant Grundler <grundler@parisc-linux.org>
94 * Remove if fix from added Harald Welte <laforge@netfilter.org> 040419
95 * ia64 compilation fix from Aron Griffis <aron@hp.com> 040604
97 * New xmit() return, do_div and misc clean up by Stephen Hemminger
98 * <shemminger@osdl.org> 040923
100 * Randy Dunlap fixed u64 printk compiler warning
102 * Remove FCS from BW calculation. Lennert Buytenhek <buytenh@wantstofly.org>
103 * New time handling. Lennert Buytenhek <buytenh@wantstofly.org> 041213
105 * Corrections from Nikolai Malykh (nmalykh@bilim.com)
106 * Removed unused flags F_SET_SRCMAC & F_SET_SRCIP 041230
108 * interruptible_sleep_on_timeout() replaced Nishanth Aravamudan <nacc@us.ibm.com>
109 * 050103
111 * MPLS support by Steven Whitehouse <steve@chygwyn.com>
113 * 802.1Q/Q-in-Q support by Francesco Fondelli (FF) <francesco.fondelli@gmail.com>
115 * Fixed src_mac command to set source mac of packet to value specified in
116 * command by Adit Ranadive <adit.262@gmail.com>
120 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
122 #include <linux/sys.h>
123 #include <linux/types.h>
124 #include <linux/module.h>
125 #include <linux/moduleparam.h>
126 #include <linux/kernel.h>
127 #include <linux/mutex.h>
128 #include <linux/sched.h>
129 #include <linux/slab.h>
130 #include <linux/vmalloc.h>
131 #include <linux/unistd.h>
132 #include <linux/string.h>
133 #include <linux/ptrace.h>
134 #include <linux/errno.h>
135 #include <linux/ioport.h>
136 #include <linux/interrupt.h>
137 #include <linux/capability.h>
138 #include <linux/hrtimer.h>
139 #include <linux/freezer.h>
140 #include <linux/delay.h>
141 #include <linux/timer.h>
142 #include <linux/list.h>
143 #include <linux/init.h>
144 #include <linux/skbuff.h>
145 #include <linux/netdevice.h>
146 #include <linux/inet.h>
147 #include <linux/inetdevice.h>
148 #include <linux/rtnetlink.h>
149 #include <linux/if_arp.h>
150 #include <linux/if_vlan.h>
151 #include <linux/in.h>
152 #include <linux/ip.h>
153 #include <linux/ipv6.h>
154 #include <linux/udp.h>
155 #include <linux/proc_fs.h>
156 #include <linux/seq_file.h>
157 #include <linux/wait.h>
158 #include <linux/etherdevice.h>
159 #include <linux/kthread.h>
160 #include <linux/prefetch.h>
161 #include <net/net_namespace.h>
162 #include <net/checksum.h>
163 #include <net/ipv6.h>
164 #include <net/udp.h>
165 #include <net/ip6_checksum.h>
166 #include <net/addrconf.h>
167 #ifdef CONFIG_XFRM
168 #include <net/xfrm.h>
169 #endif
170 #include <net/netns/generic.h>
171 #include <asm/byteorder.h>
172 #include <linux/rcupdate.h>
173 #include <linux/bitops.h>
174 #include <linux/io.h>
175 #include <linux/timex.h>
176 #include <linux/uaccess.h>
177 #include <asm/dma.h>
178 #include <asm/div64.h> /* do_div */
180 #define VERSION "2.75"
181 #define IP_NAME_SZ 32
182 #define MAX_MPLS_LABELS 16 /* This is the max label stack depth */
183 #define MPLS_STACK_BOTTOM htonl(0x00000100)
185 #define func_enter() pr_debug("entering %s\n", __func__);
187 #define PKT_FLAGS \
188 pf(IPV6) /* Interface in IPV6 Mode */ \
189 pf(IPSRC_RND) /* IP-Src Random */ \
190 pf(IPDST_RND) /* IP-Dst Random */ \
191 pf(TXSIZE_RND) /* Transmit size is random */ \
192 pf(UDPSRC_RND) /* UDP-Src Random */ \
193 pf(UDPDST_RND) /* UDP-Dst Random */ \
194 pf(UDPCSUM) /* Include UDP checksum */ \
195 pf(NO_TIMESTAMP) /* Don't timestamp packets (default TS) */ \
196 pf(MPLS_RND) /* Random MPLS labels */ \
197 pf(QUEUE_MAP_RND) /* queue map Random */ \
198 pf(QUEUE_MAP_CPU) /* queue map mirrors smp_processor_id() */ \
199 pf(FLOW_SEQ) /* Sequential flows */ \
200 pf(IPSEC) /* ipsec on for flows */ \
201 pf(MACSRC_RND) /* MAC-Src Random */ \
202 pf(MACDST_RND) /* MAC-Dst Random */ \
203 pf(VID_RND) /* Random VLAN ID */ \
204 pf(SVID_RND) /* Random SVLAN ID */ \
205 pf(NODE) /* Node memory alloc*/ \
207 #define pf(flag) flag##_SHIFT,
208 enum pkt_flags {
209 PKT_FLAGS
211 #undef pf
213 /* Device flag bits */
214 #define pf(flag) static const __u32 F_##flag = (1<<flag##_SHIFT);
215 PKT_FLAGS
216 #undef pf
218 #define pf(flag) __stringify(flag),
219 static char *pkt_flag_names[] = {
220 PKT_FLAGS
222 #undef pf
224 #define NR_PKT_FLAGS ARRAY_SIZE(pkt_flag_names)
226 /* Thread control flag bits */
227 #define T_STOP (1<<0) /* Stop run */
228 #define T_RUN (1<<1) /* Start run */
229 #define T_REMDEVALL (1<<2) /* Remove all devs */
230 #define T_REMDEV (1<<3) /* Remove one dev */
232 /* Xmit modes */
233 #define M_START_XMIT 0 /* Default normal TX */
234 #define M_NETIF_RECEIVE 1 /* Inject packets into stack */
235 #define M_QUEUE_XMIT 2 /* Inject packet into qdisc */
237 /* If lock -- protects updating of if_list */
238 #define if_lock(t) mutex_lock(&(t->if_lock));
239 #define if_unlock(t) mutex_unlock(&(t->if_lock));
241 /* Used to help with determining the pkts on receive */
242 #define PKTGEN_MAGIC 0xbe9be955
243 #define PG_PROC_DIR "pktgen"
244 #define PGCTRL "pgctrl"
246 #define MAX_CFLOWS 65536
248 #define VLAN_TAG_SIZE(x) ((x)->vlan_id == 0xffff ? 0 : 4)
249 #define SVLAN_TAG_SIZE(x) ((x)->svlan_id == 0xffff ? 0 : 4)
251 struct flow_state {
252 __be32 cur_daddr;
253 int count;
254 #ifdef CONFIG_XFRM
255 struct xfrm_state *x;
256 #endif
257 __u32 flags;
260 /* flow flag bits */
261 #define F_INIT (1<<0) /* flow has been initialized */
263 struct pktgen_dev {
265 * Try to keep frequent/infrequent used vars. separated.
267 struct proc_dir_entry *entry; /* proc file */
268 struct pktgen_thread *pg_thread;/* the owner */
269 struct list_head list; /* chaining in the thread's run-queue */
270 struct rcu_head rcu; /* freed by RCU */
272 int running; /* if false, the test will stop */
274 /* If min != max, then we will either do a linear iteration, or
275 * we will do a random selection from within the range.
277 __u32 flags;
278 int xmit_mode;
279 int min_pkt_size;
280 int max_pkt_size;
281 int pkt_overhead; /* overhead for MPLS, VLANs, IPSEC etc */
282 int nfrags;
283 int removal_mark; /* non-zero => the device is marked for
284 * removal by worker thread */
286 struct page *page;
287 u64 delay; /* nano-seconds */
289 __u64 count; /* Default No packets to send */
290 __u64 sofar; /* How many pkts we've sent so far */
291 __u64 tx_bytes; /* How many bytes we've transmitted */
292 __u64 errors; /* Errors when trying to transmit, */
294 /* runtime counters relating to clone_skb */
296 __u32 clone_count;
297 int last_ok; /* Was last skb sent?
298 * Or a failed transmit of some sort?
299 * This will keep sequence numbers in order
301 ktime_t next_tx;
302 ktime_t started_at;
303 ktime_t stopped_at;
304 u64 idle_acc; /* nano-seconds */
306 __u32 seq_num;
308 int clone_skb; /*
309 * Use multiple SKBs during packet gen.
310 * If this number is greater than 1, then
311 * that many copies of the same packet will be
312 * sent before a new packet is allocated.
313 * If you want to send 1024 identical packets
314 * before creating a new packet,
315 * set clone_skb to 1024.
318 char dst_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
319 char dst_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
320 char src_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
321 char src_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
323 struct in6_addr in6_saddr;
324 struct in6_addr in6_daddr;
325 struct in6_addr cur_in6_daddr;
326 struct in6_addr cur_in6_saddr;
327 /* For ranges */
328 struct in6_addr min_in6_daddr;
329 struct in6_addr max_in6_daddr;
330 struct in6_addr min_in6_saddr;
331 struct in6_addr max_in6_saddr;
333 /* If we're doing ranges, random or incremental, then this
334 * defines the min/max for those ranges.
336 __be32 saddr_min; /* inclusive, source IP address */
337 __be32 saddr_max; /* exclusive, source IP address */
338 __be32 daddr_min; /* inclusive, dest IP address */
339 __be32 daddr_max; /* exclusive, dest IP address */
341 __u16 udp_src_min; /* inclusive, source UDP port */
342 __u16 udp_src_max; /* exclusive, source UDP port */
343 __u16 udp_dst_min; /* inclusive, dest UDP port */
344 __u16 udp_dst_max; /* exclusive, dest UDP port */
346 /* DSCP + ECN */
347 __u8 tos; /* six MSB of (former) IPv4 TOS
348 are for dscp codepoint */
349 __u8 traffic_class; /* ditto for the (former) Traffic Class in IPv6
350 (see RFC 3260, sec. 4) */
352 /* MPLS */
353 unsigned int nr_labels; /* Depth of stack, 0 = no MPLS */
354 __be32 labels[MAX_MPLS_LABELS];
356 /* VLAN/SVLAN (802.1Q/Q-in-Q) */
357 __u8 vlan_p;
358 __u8 vlan_cfi;
359 __u16 vlan_id; /* 0xffff means no vlan tag */
361 __u8 svlan_p;
362 __u8 svlan_cfi;
363 __u16 svlan_id; /* 0xffff means no svlan tag */
365 __u32 src_mac_count; /* How many MACs to iterate through */
366 __u32 dst_mac_count; /* How many MACs to iterate through */
368 unsigned char dst_mac[ETH_ALEN];
369 unsigned char src_mac[ETH_ALEN];
371 __u32 cur_dst_mac_offset;
372 __u32 cur_src_mac_offset;
373 __be32 cur_saddr;
374 __be32 cur_daddr;
375 __u16 ip_id;
376 __u16 cur_udp_dst;
377 __u16 cur_udp_src;
378 __u16 cur_queue_map;
379 __u32 cur_pkt_size;
380 __u32 last_pkt_size;
382 __u8 hh[14];
383 /* = {
384 0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
386 We fill in SRC address later
387 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
388 0x08, 0x00
391 __u16 pad; /* pad out the hh struct to an even 16 bytes */
393 struct sk_buff *skb; /* skb we are to transmit next, used for when we
394 * are transmitting the same one multiple times
396 struct net_device *odev; /* The out-going device.
397 * Note that the device should have it's
398 * pg_info pointer pointing back to this
399 * device.
400 * Set when the user specifies the out-going
401 * device name (not when the inject is
402 * started as it used to do.)
404 char odevname[32];
405 struct flow_state *flows;
406 unsigned int cflows; /* Concurrent flows (config) */
407 unsigned int lflow; /* Flow length (config) */
408 unsigned int nflows; /* accumulated flows (stats) */
409 unsigned int curfl; /* current sequenced flow (state)*/
411 u16 queue_map_min;
412 u16 queue_map_max;
413 __u32 skb_priority; /* skb priority field */
414 unsigned int burst; /* number of duplicated packets to burst */
415 int node; /* Memory node */
417 #ifdef CONFIG_XFRM
418 __u8 ipsmode; /* IPSEC mode (config) */
419 __u8 ipsproto; /* IPSEC type (config) */
420 __u32 spi;
421 struct xfrm_dst xdst;
422 struct dst_ops dstops;
423 #endif
424 char result[512];
427 struct pktgen_hdr {
428 __be32 pgh_magic;
429 __be32 seq_num;
430 __be32 tv_sec;
431 __be32 tv_usec;
435 static unsigned int pg_net_id __read_mostly;
437 struct pktgen_net {
438 struct net *net;
439 struct proc_dir_entry *proc_dir;
440 struct list_head pktgen_threads;
441 bool pktgen_exiting;
444 struct pktgen_thread {
445 struct mutex if_lock; /* for list of devices */
446 struct list_head if_list; /* All device here */
447 struct list_head th_list;
448 struct task_struct *tsk;
449 char result[512];
451 /* Field for thread to receive "posted" events terminate,
452 stop ifs etc. */
454 u32 control;
455 int cpu;
457 wait_queue_head_t queue;
458 struct completion start_done;
459 struct pktgen_net *net;
462 #define REMOVE 1
463 #define FIND 0
465 static const char version[] =
466 "Packet Generator for packet performance testing. "
467 "Version: " VERSION "\n";
469 static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *i);
470 static int pktgen_add_device(struct pktgen_thread *t, const char *ifname);
471 static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
472 const char *ifname, bool exact);
473 static int pktgen_device_event(struct notifier_block *, unsigned long, void *);
474 static void pktgen_run_all_threads(struct pktgen_net *pn);
475 static void pktgen_reset_all_threads(struct pktgen_net *pn);
476 static void pktgen_stop_all_threads_ifs(struct pktgen_net *pn);
478 static void pktgen_stop(struct pktgen_thread *t);
479 static void pktgen_clear_counters(struct pktgen_dev *pkt_dev);
481 /* Module parameters, defaults. */
482 static int pg_count_d __read_mostly = 1000;
483 static int pg_delay_d __read_mostly;
484 static int pg_clone_skb_d __read_mostly;
485 static int debug __read_mostly;
487 static DEFINE_MUTEX(pktgen_thread_lock);
489 static struct notifier_block pktgen_notifier_block = {
490 .notifier_call = pktgen_device_event,
494 * /proc handling functions
498 static int pgctrl_show(struct seq_file *seq, void *v)
500 seq_puts(seq, version);
501 return 0;
504 static ssize_t pgctrl_write(struct file *file, const char __user *buf,
505 size_t count, loff_t *ppos)
507 char data[128];
508 struct pktgen_net *pn = net_generic(current->nsproxy->net_ns, pg_net_id);
510 if (!capable(CAP_NET_ADMIN))
511 return -EPERM;
513 if (count == 0)
514 return -EINVAL;
516 if (count > sizeof(data))
517 count = sizeof(data);
519 if (copy_from_user(data, buf, count))
520 return -EFAULT;
522 data[count - 1] = 0; /* Strip trailing '\n' and terminate string */
524 if (!strcmp(data, "stop"))
525 pktgen_stop_all_threads_ifs(pn);
527 else if (!strcmp(data, "start"))
528 pktgen_run_all_threads(pn);
530 else if (!strcmp(data, "reset"))
531 pktgen_reset_all_threads(pn);
533 else
534 return -EINVAL;
536 return count;
539 static int pgctrl_open(struct inode *inode, struct file *file)
541 return single_open(file, pgctrl_show, PDE_DATA(inode));
544 static const struct file_operations pktgen_fops = {
545 .open = pgctrl_open,
546 .read = seq_read,
547 .llseek = seq_lseek,
548 .write = pgctrl_write,
549 .release = single_release,
552 static int pktgen_if_show(struct seq_file *seq, void *v)
554 const struct pktgen_dev *pkt_dev = seq->private;
555 ktime_t stopped;
556 unsigned int i;
557 u64 idle;
559 seq_printf(seq,
560 "Params: count %llu min_pkt_size: %u max_pkt_size: %u\n",
561 (unsigned long long)pkt_dev->count, pkt_dev->min_pkt_size,
562 pkt_dev->max_pkt_size);
564 seq_printf(seq,
565 " frags: %d delay: %llu clone_skb: %d ifname: %s\n",
566 pkt_dev->nfrags, (unsigned long long) pkt_dev->delay,
567 pkt_dev->clone_skb, pkt_dev->odevname);
569 seq_printf(seq, " flows: %u flowlen: %u\n", pkt_dev->cflows,
570 pkt_dev->lflow);
572 seq_printf(seq,
573 " queue_map_min: %u queue_map_max: %u\n",
574 pkt_dev->queue_map_min,
575 pkt_dev->queue_map_max);
577 if (pkt_dev->skb_priority)
578 seq_printf(seq, " skb_priority: %u\n",
579 pkt_dev->skb_priority);
581 if (pkt_dev->flags & F_IPV6) {
582 seq_printf(seq,
583 " saddr: %pI6c min_saddr: %pI6c max_saddr: %pI6c\n"
584 " daddr: %pI6c min_daddr: %pI6c max_daddr: %pI6c\n",
585 &pkt_dev->in6_saddr,
586 &pkt_dev->min_in6_saddr, &pkt_dev->max_in6_saddr,
587 &pkt_dev->in6_daddr,
588 &pkt_dev->min_in6_daddr, &pkt_dev->max_in6_daddr);
589 } else {
590 seq_printf(seq,
591 " dst_min: %s dst_max: %s\n",
592 pkt_dev->dst_min, pkt_dev->dst_max);
593 seq_printf(seq,
594 " src_min: %s src_max: %s\n",
595 pkt_dev->src_min, pkt_dev->src_max);
598 seq_puts(seq, " src_mac: ");
600 seq_printf(seq, "%pM ",
601 is_zero_ether_addr(pkt_dev->src_mac) ?
602 pkt_dev->odev->dev_addr : pkt_dev->src_mac);
604 seq_puts(seq, "dst_mac: ");
605 seq_printf(seq, "%pM\n", pkt_dev->dst_mac);
607 seq_printf(seq,
608 " udp_src_min: %d udp_src_max: %d"
609 " udp_dst_min: %d udp_dst_max: %d\n",
610 pkt_dev->udp_src_min, pkt_dev->udp_src_max,
611 pkt_dev->udp_dst_min, pkt_dev->udp_dst_max);
613 seq_printf(seq,
614 " src_mac_count: %d dst_mac_count: %d\n",
615 pkt_dev->src_mac_count, pkt_dev->dst_mac_count);
617 if (pkt_dev->nr_labels) {
618 seq_puts(seq, " mpls: ");
619 for (i = 0; i < pkt_dev->nr_labels; i++)
620 seq_printf(seq, "%08x%s", ntohl(pkt_dev->labels[i]),
621 i == pkt_dev->nr_labels-1 ? "\n" : ", ");
624 if (pkt_dev->vlan_id != 0xffff)
625 seq_printf(seq, " vlan_id: %u vlan_p: %u vlan_cfi: %u\n",
626 pkt_dev->vlan_id, pkt_dev->vlan_p,
627 pkt_dev->vlan_cfi);
629 if (pkt_dev->svlan_id != 0xffff)
630 seq_printf(seq, " svlan_id: %u vlan_p: %u vlan_cfi: %u\n",
631 pkt_dev->svlan_id, pkt_dev->svlan_p,
632 pkt_dev->svlan_cfi);
634 if (pkt_dev->tos)
635 seq_printf(seq, " tos: 0x%02x\n", pkt_dev->tos);
637 if (pkt_dev->traffic_class)
638 seq_printf(seq, " traffic_class: 0x%02x\n", pkt_dev->traffic_class);
640 if (pkt_dev->burst > 1)
641 seq_printf(seq, " burst: %d\n", pkt_dev->burst);
643 if (pkt_dev->node >= 0)
644 seq_printf(seq, " node: %d\n", pkt_dev->node);
646 if (pkt_dev->xmit_mode == M_NETIF_RECEIVE)
647 seq_puts(seq, " xmit_mode: netif_receive\n");
648 else if (pkt_dev->xmit_mode == M_QUEUE_XMIT)
649 seq_puts(seq, " xmit_mode: xmit_queue\n");
651 seq_puts(seq, " Flags: ");
653 for (i = 0; i < NR_PKT_FLAGS; i++) {
654 if (i == F_FLOW_SEQ)
655 if (!pkt_dev->cflows)
656 continue;
658 if (pkt_dev->flags & (1 << i))
659 seq_printf(seq, "%s ", pkt_flag_names[i]);
660 else if (i == F_FLOW_SEQ)
661 seq_puts(seq, "FLOW_RND ");
663 #ifdef CONFIG_XFRM
664 if (i == F_IPSEC && pkt_dev->spi)
665 seq_printf(seq, "spi:%u", pkt_dev->spi);
666 #endif
669 seq_puts(seq, "\n");
671 /* not really stopped, more like last-running-at */
672 stopped = pkt_dev->running ? ktime_get() : pkt_dev->stopped_at;
673 idle = pkt_dev->idle_acc;
674 do_div(idle, NSEC_PER_USEC);
676 seq_printf(seq,
677 "Current:\n pkts-sofar: %llu errors: %llu\n",
678 (unsigned long long)pkt_dev->sofar,
679 (unsigned long long)pkt_dev->errors);
681 seq_printf(seq,
682 " started: %lluus stopped: %lluus idle: %lluus\n",
683 (unsigned long long) ktime_to_us(pkt_dev->started_at),
684 (unsigned long long) ktime_to_us(stopped),
685 (unsigned long long) idle);
687 seq_printf(seq,
688 " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n",
689 pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset,
690 pkt_dev->cur_src_mac_offset);
692 if (pkt_dev->flags & F_IPV6) {
693 seq_printf(seq, " cur_saddr: %pI6c cur_daddr: %pI6c\n",
694 &pkt_dev->cur_in6_saddr,
695 &pkt_dev->cur_in6_daddr);
696 } else
697 seq_printf(seq, " cur_saddr: %pI4 cur_daddr: %pI4\n",
698 &pkt_dev->cur_saddr, &pkt_dev->cur_daddr);
700 seq_printf(seq, " cur_udp_dst: %d cur_udp_src: %d\n",
701 pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src);
703 seq_printf(seq, " cur_queue_map: %u\n", pkt_dev->cur_queue_map);
705 seq_printf(seq, " flows: %u\n", pkt_dev->nflows);
707 if (pkt_dev->result[0])
708 seq_printf(seq, "Result: %s\n", pkt_dev->result);
709 else
710 seq_puts(seq, "Result: Idle\n");
712 return 0;
716 static int hex32_arg(const char __user *user_buffer, unsigned long maxlen,
717 __u32 *num)
719 int i = 0;
720 *num = 0;
722 for (; i < maxlen; i++) {
723 int value;
724 char c;
725 *num <<= 4;
726 if (get_user(c, &user_buffer[i]))
727 return -EFAULT;
728 value = hex_to_bin(c);
729 if (value >= 0)
730 *num |= value;
731 else
732 break;
734 return i;
737 static int count_trail_chars(const char __user * user_buffer,
738 unsigned int maxlen)
740 int i;
742 for (i = 0; i < maxlen; i++) {
743 char c;
744 if (get_user(c, &user_buffer[i]))
745 return -EFAULT;
746 switch (c) {
747 case '\"':
748 case '\n':
749 case '\r':
750 case '\t':
751 case ' ':
752 case '=':
753 break;
754 default:
755 goto done;
758 done:
759 return i;
762 static long num_arg(const char __user *user_buffer, unsigned long maxlen,
763 unsigned long *num)
765 int i;
766 *num = 0;
768 for (i = 0; i < maxlen; i++) {
769 char c;
770 if (get_user(c, &user_buffer[i]))
771 return -EFAULT;
772 if ((c >= '0') && (c <= '9')) {
773 *num *= 10;
774 *num += c - '0';
775 } else
776 break;
778 return i;
781 static int strn_len(const char __user * user_buffer, unsigned int maxlen)
783 int i;
785 for (i = 0; i < maxlen; i++) {
786 char c;
787 if (get_user(c, &user_buffer[i]))
788 return -EFAULT;
789 switch (c) {
790 case '\"':
791 case '\n':
792 case '\r':
793 case '\t':
794 case ' ':
795 goto done_str;
796 default:
797 break;
800 done_str:
801 return i;
804 static ssize_t get_labels(const char __user *buffer, struct pktgen_dev *pkt_dev)
806 unsigned int n = 0;
807 char c;
808 ssize_t i = 0;
809 int len;
811 pkt_dev->nr_labels = 0;
812 do {
813 __u32 tmp;
814 len = hex32_arg(&buffer[i], 8, &tmp);
815 if (len <= 0)
816 return len;
817 pkt_dev->labels[n] = htonl(tmp);
818 if (pkt_dev->labels[n] & MPLS_STACK_BOTTOM)
819 pkt_dev->flags |= F_MPLS_RND;
820 i += len;
821 if (get_user(c, &buffer[i]))
822 return -EFAULT;
823 i++;
824 n++;
825 if (n >= MAX_MPLS_LABELS)
826 return -E2BIG;
827 } while (c == ',');
829 pkt_dev->nr_labels = n;
830 return i;
833 static __u32 pktgen_read_flag(const char *f, bool *disable)
835 __u32 i;
837 if (f[0] == '!') {
838 *disable = true;
839 f++;
842 for (i = 0; i < NR_PKT_FLAGS; i++) {
843 if (!IS_ENABLED(CONFIG_XFRM) && i == IPSEC_SHIFT)
844 continue;
846 /* allow only disabling ipv6 flag */
847 if (!*disable && i == IPV6_SHIFT)
848 continue;
850 if (strcmp(f, pkt_flag_names[i]) == 0)
851 return 1 << i;
854 if (strcmp(f, "FLOW_RND") == 0) {
855 *disable = !*disable;
856 return F_FLOW_SEQ;
859 return 0;
862 static ssize_t pktgen_if_write(struct file *file,
863 const char __user * user_buffer, size_t count,
864 loff_t * offset)
866 struct seq_file *seq = file->private_data;
867 struct pktgen_dev *pkt_dev = seq->private;
868 int i, max, len;
869 char name[16], valstr[32];
870 unsigned long value = 0;
871 char *pg_result = NULL;
872 int tmp = 0;
873 char buf[128];
875 pg_result = &(pkt_dev->result[0]);
877 if (count < 1) {
878 pr_warn("wrong command format\n");
879 return -EINVAL;
882 max = count;
883 tmp = count_trail_chars(user_buffer, max);
884 if (tmp < 0) {
885 pr_warn("illegal format\n");
886 return tmp;
888 i = tmp;
890 /* Read variable name */
892 len = strn_len(&user_buffer[i], sizeof(name) - 1);
893 if (len < 0)
894 return len;
896 memset(name, 0, sizeof(name));
897 if (copy_from_user(name, &user_buffer[i], len))
898 return -EFAULT;
899 i += len;
901 max = count - i;
902 len = count_trail_chars(&user_buffer[i], max);
903 if (len < 0)
904 return len;
906 i += len;
908 if (debug) {
909 size_t copy = min_t(size_t, count, 1023);
910 char tb[copy + 1];
911 if (copy_from_user(tb, user_buffer, copy))
912 return -EFAULT;
913 tb[copy] = 0;
914 pr_debug("%s,%lu buffer -:%s:-\n",
915 name, (unsigned long)count, tb);
918 if (!strcmp(name, "min_pkt_size")) {
919 len = num_arg(&user_buffer[i], 10, &value);
920 if (len < 0)
921 return len;
923 i += len;
924 if (value < 14 + 20 + 8)
925 value = 14 + 20 + 8;
926 if (value != pkt_dev->min_pkt_size) {
927 pkt_dev->min_pkt_size = value;
928 pkt_dev->cur_pkt_size = value;
930 sprintf(pg_result, "OK: min_pkt_size=%u",
931 pkt_dev->min_pkt_size);
932 return count;
935 if (!strcmp(name, "max_pkt_size")) {
936 len = num_arg(&user_buffer[i], 10, &value);
937 if (len < 0)
938 return len;
940 i += len;
941 if (value < 14 + 20 + 8)
942 value = 14 + 20 + 8;
943 if (value != pkt_dev->max_pkt_size) {
944 pkt_dev->max_pkt_size = value;
945 pkt_dev->cur_pkt_size = value;
947 sprintf(pg_result, "OK: max_pkt_size=%u",
948 pkt_dev->max_pkt_size);
949 return count;
952 /* Shortcut for min = max */
954 if (!strcmp(name, "pkt_size")) {
955 len = num_arg(&user_buffer[i], 10, &value);
956 if (len < 0)
957 return len;
959 i += len;
960 if (value < 14 + 20 + 8)
961 value = 14 + 20 + 8;
962 if (value != pkt_dev->min_pkt_size) {
963 pkt_dev->min_pkt_size = value;
964 pkt_dev->max_pkt_size = value;
965 pkt_dev->cur_pkt_size = value;
967 sprintf(pg_result, "OK: pkt_size=%u", pkt_dev->min_pkt_size);
968 return count;
971 if (!strcmp(name, "debug")) {
972 len = num_arg(&user_buffer[i], 10, &value);
973 if (len < 0)
974 return len;
976 i += len;
977 debug = value;
978 sprintf(pg_result, "OK: debug=%u", debug);
979 return count;
982 if (!strcmp(name, "frags")) {
983 len = num_arg(&user_buffer[i], 10, &value);
984 if (len < 0)
985 return len;
987 i += len;
988 pkt_dev->nfrags = value;
989 sprintf(pg_result, "OK: frags=%u", pkt_dev->nfrags);
990 return count;
992 if (!strcmp(name, "delay")) {
993 len = num_arg(&user_buffer[i], 10, &value);
994 if (len < 0)
995 return len;
997 i += len;
998 if (value == 0x7FFFFFFF)
999 pkt_dev->delay = ULLONG_MAX;
1000 else
1001 pkt_dev->delay = (u64)value;
1003 sprintf(pg_result, "OK: delay=%llu",
1004 (unsigned long long) pkt_dev->delay);
1005 return count;
1007 if (!strcmp(name, "rate")) {
1008 len = num_arg(&user_buffer[i], 10, &value);
1009 if (len < 0)
1010 return len;
1012 i += len;
1013 if (!value)
1014 return len;
1015 pkt_dev->delay = pkt_dev->min_pkt_size*8*NSEC_PER_USEC/value;
1016 if (debug)
1017 pr_info("Delay set at: %llu ns\n", pkt_dev->delay);
1019 sprintf(pg_result, "OK: rate=%lu", value);
1020 return count;
1022 if (!strcmp(name, "ratep")) {
1023 len = num_arg(&user_buffer[i], 10, &value);
1024 if (len < 0)
1025 return len;
1027 i += len;
1028 if (!value)
1029 return len;
1030 pkt_dev->delay = NSEC_PER_SEC/value;
1031 if (debug)
1032 pr_info("Delay set at: %llu ns\n", pkt_dev->delay);
1034 sprintf(pg_result, "OK: rate=%lu", value);
1035 return count;
1037 if (!strcmp(name, "udp_src_min")) {
1038 len = num_arg(&user_buffer[i], 10, &value);
1039 if (len < 0)
1040 return len;
1042 i += len;
1043 if (value != pkt_dev->udp_src_min) {
1044 pkt_dev->udp_src_min = value;
1045 pkt_dev->cur_udp_src = value;
1047 sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min);
1048 return count;
1050 if (!strcmp(name, "udp_dst_min")) {
1051 len = num_arg(&user_buffer[i], 10, &value);
1052 if (len < 0)
1053 return len;
1055 i += len;
1056 if (value != pkt_dev->udp_dst_min) {
1057 pkt_dev->udp_dst_min = value;
1058 pkt_dev->cur_udp_dst = value;
1060 sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min);
1061 return count;
1063 if (!strcmp(name, "udp_src_max")) {
1064 len = num_arg(&user_buffer[i], 10, &value);
1065 if (len < 0)
1066 return len;
1068 i += len;
1069 if (value != pkt_dev->udp_src_max) {
1070 pkt_dev->udp_src_max = value;
1071 pkt_dev->cur_udp_src = value;
1073 sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max);
1074 return count;
1076 if (!strcmp(name, "udp_dst_max")) {
1077 len = num_arg(&user_buffer[i], 10, &value);
1078 if (len < 0)
1079 return len;
1081 i += len;
1082 if (value != pkt_dev->udp_dst_max) {
1083 pkt_dev->udp_dst_max = value;
1084 pkt_dev->cur_udp_dst = value;
1086 sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max);
1087 return count;
1089 if (!strcmp(name, "clone_skb")) {
1090 len = num_arg(&user_buffer[i], 10, &value);
1091 if (len < 0)
1092 return len;
1093 if ((value > 0) &&
1094 ((pkt_dev->xmit_mode == M_NETIF_RECEIVE) ||
1095 !(pkt_dev->odev->priv_flags & IFF_TX_SKB_SHARING)))
1096 return -ENOTSUPP;
1097 i += len;
1098 pkt_dev->clone_skb = value;
1100 sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb);
1101 return count;
1103 if (!strcmp(name, "count")) {
1104 len = num_arg(&user_buffer[i], 10, &value);
1105 if (len < 0)
1106 return len;
1108 i += len;
1109 pkt_dev->count = value;
1110 sprintf(pg_result, "OK: count=%llu",
1111 (unsigned long long)pkt_dev->count);
1112 return count;
1114 if (!strcmp(name, "src_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->src_mac_count != value) {
1121 pkt_dev->src_mac_count = value;
1122 pkt_dev->cur_src_mac_offset = 0;
1124 sprintf(pg_result, "OK: src_mac_count=%d",
1125 pkt_dev->src_mac_count);
1126 return count;
1128 if (!strcmp(name, "dst_mac_count")) {
1129 len = num_arg(&user_buffer[i], 10, &value);
1130 if (len < 0)
1131 return len;
1133 i += len;
1134 if (pkt_dev->dst_mac_count != value) {
1135 pkt_dev->dst_mac_count = value;
1136 pkt_dev->cur_dst_mac_offset = 0;
1138 sprintf(pg_result, "OK: dst_mac_count=%d",
1139 pkt_dev->dst_mac_count);
1140 return count;
1142 if (!strcmp(name, "burst")) {
1143 len = num_arg(&user_buffer[i], 10, &value);
1144 if (len < 0)
1145 return len;
1147 i += len;
1148 if ((value > 1) &&
1149 ((pkt_dev->xmit_mode == M_QUEUE_XMIT) ||
1150 ((pkt_dev->xmit_mode == M_START_XMIT) &&
1151 (!(pkt_dev->odev->priv_flags & IFF_TX_SKB_SHARING)))))
1152 return -ENOTSUPP;
1153 pkt_dev->burst = value < 1 ? 1 : value;
1154 sprintf(pg_result, "OK: burst=%d", pkt_dev->burst);
1155 return count;
1157 if (!strcmp(name, "node")) {
1158 len = num_arg(&user_buffer[i], 10, &value);
1159 if (len < 0)
1160 return len;
1162 i += len;
1164 if (node_possible(value)) {
1165 pkt_dev->node = value;
1166 sprintf(pg_result, "OK: node=%d", pkt_dev->node);
1167 if (pkt_dev->page) {
1168 put_page(pkt_dev->page);
1169 pkt_dev->page = NULL;
1172 else
1173 sprintf(pg_result, "ERROR: node not possible");
1174 return count;
1176 if (!strcmp(name, "xmit_mode")) {
1177 char f[32];
1179 memset(f, 0, 32);
1180 len = strn_len(&user_buffer[i], sizeof(f) - 1);
1181 if (len < 0)
1182 return len;
1184 if (copy_from_user(f, &user_buffer[i], len))
1185 return -EFAULT;
1186 i += len;
1188 if (strcmp(f, "start_xmit") == 0) {
1189 pkt_dev->xmit_mode = M_START_XMIT;
1190 } else if (strcmp(f, "netif_receive") == 0) {
1191 /* clone_skb set earlier, not supported in this mode */
1192 if (pkt_dev->clone_skb > 0)
1193 return -ENOTSUPP;
1195 pkt_dev->xmit_mode = M_NETIF_RECEIVE;
1197 /* make sure new packet is allocated every time
1198 * pktgen_xmit() is called
1200 pkt_dev->last_ok = 1;
1202 /* override clone_skb if user passed default value
1203 * at module loading time
1205 pkt_dev->clone_skb = 0;
1206 } else if (strcmp(f, "queue_xmit") == 0) {
1207 pkt_dev->xmit_mode = M_QUEUE_XMIT;
1208 pkt_dev->last_ok = 1;
1209 } else {
1210 sprintf(pg_result,
1211 "xmit_mode -:%s:- unknown\nAvailable modes: %s",
1212 f, "start_xmit, netif_receive\n");
1213 return count;
1215 sprintf(pg_result, "OK: xmit_mode=%s", f);
1216 return count;
1218 if (!strcmp(name, "flag")) {
1219 __u32 flag;
1220 char f[32];
1221 bool disable = false;
1223 memset(f, 0, 32);
1224 len = strn_len(&user_buffer[i], sizeof(f) - 1);
1225 if (len < 0)
1226 return len;
1228 if (copy_from_user(f, &user_buffer[i], len))
1229 return -EFAULT;
1230 i += len;
1232 flag = pktgen_read_flag(f, &disable);
1234 if (flag) {
1235 if (disable)
1236 pkt_dev->flags &= ~flag;
1237 else
1238 pkt_dev->flags |= flag;
1239 } else {
1240 sprintf(pg_result,
1241 "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
1243 "IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
1244 "MACSRC_RND, MACDST_RND, TXSIZE_RND, IPV6, "
1245 "MPLS_RND, VID_RND, SVID_RND, FLOW_SEQ, "
1246 "QUEUE_MAP_RND, QUEUE_MAP_CPU, UDPCSUM, "
1247 "NO_TIMESTAMP, "
1248 #ifdef CONFIG_XFRM
1249 "IPSEC, "
1250 #endif
1251 "NODE_ALLOC\n");
1252 return count;
1254 sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
1255 return count;
1257 if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
1258 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
1259 if (len < 0)
1260 return len;
1262 if (copy_from_user(buf, &user_buffer[i], len))
1263 return -EFAULT;
1264 buf[len] = 0;
1265 if (strcmp(buf, pkt_dev->dst_min) != 0) {
1266 memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
1267 strncpy(pkt_dev->dst_min, buf, len);
1268 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
1269 pkt_dev->cur_daddr = pkt_dev->daddr_min;
1271 if (debug)
1272 pr_debug("dst_min set to: %s\n", 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 pr_debug("dst_max set to: %s\n", pkt_dev->dst_max);
1295 i += len;
1296 sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max);
1297 return count;
1299 if (!strcmp(name, "dst6")) {
1300 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1301 if (len < 0)
1302 return len;
1304 pkt_dev->flags |= F_IPV6;
1306 if (copy_from_user(buf, &user_buffer[i], len))
1307 return -EFAULT;
1308 buf[len] = 0;
1310 in6_pton(buf, -1, pkt_dev->in6_daddr.s6_addr, -1, NULL);
1311 snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->in6_daddr);
1313 pkt_dev->cur_in6_daddr = pkt_dev->in6_daddr;
1315 if (debug)
1316 pr_debug("dst6 set to: %s\n", buf);
1318 i += len;
1319 sprintf(pg_result, "OK: dst6=%s", buf);
1320 return count;
1322 if (!strcmp(name, "dst6_min")) {
1323 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1324 if (len < 0)
1325 return len;
1327 pkt_dev->flags |= F_IPV6;
1329 if (copy_from_user(buf, &user_buffer[i], len))
1330 return -EFAULT;
1331 buf[len] = 0;
1333 in6_pton(buf, -1, pkt_dev->min_in6_daddr.s6_addr, -1, NULL);
1334 snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->min_in6_daddr);
1336 pkt_dev->cur_in6_daddr = pkt_dev->min_in6_daddr;
1337 if (debug)
1338 pr_debug("dst6_min set to: %s\n", buf);
1340 i += len;
1341 sprintf(pg_result, "OK: dst6_min=%s", buf);
1342 return count;
1344 if (!strcmp(name, "dst6_max")) {
1345 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1346 if (len < 0)
1347 return len;
1349 pkt_dev->flags |= F_IPV6;
1351 if (copy_from_user(buf, &user_buffer[i], len))
1352 return -EFAULT;
1353 buf[len] = 0;
1355 in6_pton(buf, -1, pkt_dev->max_in6_daddr.s6_addr, -1, NULL);
1356 snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->max_in6_daddr);
1358 if (debug)
1359 pr_debug("dst6_max set to: %s\n", buf);
1361 i += len;
1362 sprintf(pg_result, "OK: dst6_max=%s", buf);
1363 return count;
1365 if (!strcmp(name, "src6")) {
1366 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1367 if (len < 0)
1368 return len;
1370 pkt_dev->flags |= F_IPV6;
1372 if (copy_from_user(buf, &user_buffer[i], len))
1373 return -EFAULT;
1374 buf[len] = 0;
1376 in6_pton(buf, -1, pkt_dev->in6_saddr.s6_addr, -1, NULL);
1377 snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->in6_saddr);
1379 pkt_dev->cur_in6_saddr = pkt_dev->in6_saddr;
1381 if (debug)
1382 pr_debug("src6 set to: %s\n", buf);
1384 i += len;
1385 sprintf(pg_result, "OK: src6=%s", buf);
1386 return count;
1388 if (!strcmp(name, "src_min")) {
1389 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
1390 if (len < 0)
1391 return len;
1393 if (copy_from_user(buf, &user_buffer[i], len))
1394 return -EFAULT;
1395 buf[len] = 0;
1396 if (strcmp(buf, pkt_dev->src_min) != 0) {
1397 memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
1398 strncpy(pkt_dev->src_min, buf, len);
1399 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
1400 pkt_dev->cur_saddr = pkt_dev->saddr_min;
1402 if (debug)
1403 pr_debug("src_min set to: %s\n", pkt_dev->src_min);
1404 i += len;
1405 sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min);
1406 return count;
1408 if (!strcmp(name, "src_max")) {
1409 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
1410 if (len < 0)
1411 return len;
1413 if (copy_from_user(buf, &user_buffer[i], len))
1414 return -EFAULT;
1415 buf[len] = 0;
1416 if (strcmp(buf, pkt_dev->src_max) != 0) {
1417 memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
1418 strncpy(pkt_dev->src_max, buf, len);
1419 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
1420 pkt_dev->cur_saddr = pkt_dev->saddr_max;
1422 if (debug)
1423 pr_debug("src_max set to: %s\n", pkt_dev->src_max);
1424 i += len;
1425 sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max);
1426 return count;
1428 if (!strcmp(name, "dst_mac")) {
1429 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
1430 if (len < 0)
1431 return len;
1433 memset(valstr, 0, sizeof(valstr));
1434 if (copy_from_user(valstr, &user_buffer[i], len))
1435 return -EFAULT;
1437 if (!mac_pton(valstr, pkt_dev->dst_mac))
1438 return -EINVAL;
1439 /* Set up Dest MAC */
1440 ether_addr_copy(&pkt_dev->hh[0], pkt_dev->dst_mac);
1442 sprintf(pg_result, "OK: dstmac %pM", pkt_dev->dst_mac);
1443 return count;
1445 if (!strcmp(name, "src_mac")) {
1446 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
1447 if (len < 0)
1448 return len;
1450 memset(valstr, 0, sizeof(valstr));
1451 if (copy_from_user(valstr, &user_buffer[i], len))
1452 return -EFAULT;
1454 if (!mac_pton(valstr, pkt_dev->src_mac))
1455 return -EINVAL;
1456 /* Set up Src MAC */
1457 ether_addr_copy(&pkt_dev->hh[6], pkt_dev->src_mac);
1459 sprintf(pg_result, "OK: srcmac %pM", pkt_dev->src_mac);
1460 return count;
1463 if (!strcmp(name, "clear_counters")) {
1464 pktgen_clear_counters(pkt_dev);
1465 sprintf(pg_result, "OK: Clearing counters.\n");
1466 return count;
1469 if (!strcmp(name, "flows")) {
1470 len = num_arg(&user_buffer[i], 10, &value);
1471 if (len < 0)
1472 return len;
1474 i += len;
1475 if (value > MAX_CFLOWS)
1476 value = MAX_CFLOWS;
1478 pkt_dev->cflows = value;
1479 sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
1480 return count;
1482 #ifdef CONFIG_XFRM
1483 if (!strcmp(name, "spi")) {
1484 len = num_arg(&user_buffer[i], 10, &value);
1485 if (len < 0)
1486 return len;
1488 i += len;
1489 pkt_dev->spi = value;
1490 sprintf(pg_result, "OK: spi=%u", pkt_dev->spi);
1491 return count;
1493 #endif
1494 if (!strcmp(name, "flowlen")) {
1495 len = num_arg(&user_buffer[i], 10, &value);
1496 if (len < 0)
1497 return len;
1499 i += len;
1500 pkt_dev->lflow = value;
1501 sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
1502 return count;
1505 if (!strcmp(name, "queue_map_min")) {
1506 len = num_arg(&user_buffer[i], 5, &value);
1507 if (len < 0)
1508 return len;
1510 i += len;
1511 pkt_dev->queue_map_min = value;
1512 sprintf(pg_result, "OK: queue_map_min=%u", pkt_dev->queue_map_min);
1513 return count;
1516 if (!strcmp(name, "queue_map_max")) {
1517 len = num_arg(&user_buffer[i], 5, &value);
1518 if (len < 0)
1519 return len;
1521 i += len;
1522 pkt_dev->queue_map_max = value;
1523 sprintf(pg_result, "OK: queue_map_max=%u", pkt_dev->queue_map_max);
1524 return count;
1527 if (!strcmp(name, "mpls")) {
1528 unsigned int n, cnt;
1530 len = get_labels(&user_buffer[i], pkt_dev);
1531 if (len < 0)
1532 return len;
1533 i += len;
1534 cnt = sprintf(pg_result, "OK: mpls=");
1535 for (n = 0; n < pkt_dev->nr_labels; n++)
1536 cnt += sprintf(pg_result + cnt,
1537 "%08x%s", ntohl(pkt_dev->labels[n]),
1538 n == pkt_dev->nr_labels-1 ? "" : ",");
1540 if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) {
1541 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1542 pkt_dev->svlan_id = 0xffff;
1544 if (debug)
1545 pr_debug("VLAN/SVLAN auto turned off\n");
1547 return count;
1550 if (!strcmp(name, "vlan_id")) {
1551 len = num_arg(&user_buffer[i], 4, &value);
1552 if (len < 0)
1553 return len;
1555 i += len;
1556 if (value <= 4095) {
1557 pkt_dev->vlan_id = value; /* turn on VLAN */
1559 if (debug)
1560 pr_debug("VLAN turned on\n");
1562 if (debug && pkt_dev->nr_labels)
1563 pr_debug("MPLS auto turned off\n");
1565 pkt_dev->nr_labels = 0; /* turn off MPLS */
1566 sprintf(pg_result, "OK: vlan_id=%u", pkt_dev->vlan_id);
1567 } else {
1568 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1569 pkt_dev->svlan_id = 0xffff;
1571 if (debug)
1572 pr_debug("VLAN/SVLAN turned off\n");
1574 return count;
1577 if (!strcmp(name, "vlan_p")) {
1578 len = num_arg(&user_buffer[i], 1, &value);
1579 if (len < 0)
1580 return len;
1582 i += len;
1583 if ((value <= 7) && (pkt_dev->vlan_id != 0xffff)) {
1584 pkt_dev->vlan_p = value;
1585 sprintf(pg_result, "OK: vlan_p=%u", pkt_dev->vlan_p);
1586 } else {
1587 sprintf(pg_result, "ERROR: vlan_p must be 0-7");
1589 return count;
1592 if (!strcmp(name, "vlan_cfi")) {
1593 len = num_arg(&user_buffer[i], 1, &value);
1594 if (len < 0)
1595 return len;
1597 i += len;
1598 if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) {
1599 pkt_dev->vlan_cfi = value;
1600 sprintf(pg_result, "OK: vlan_cfi=%u", pkt_dev->vlan_cfi);
1601 } else {
1602 sprintf(pg_result, "ERROR: vlan_cfi must be 0-1");
1604 return count;
1607 if (!strcmp(name, "svlan_id")) {
1608 len = num_arg(&user_buffer[i], 4, &value);
1609 if (len < 0)
1610 return len;
1612 i += len;
1613 if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) {
1614 pkt_dev->svlan_id = value; /* turn on SVLAN */
1616 if (debug)
1617 pr_debug("SVLAN turned on\n");
1619 if (debug && pkt_dev->nr_labels)
1620 pr_debug("MPLS auto turned off\n");
1622 pkt_dev->nr_labels = 0; /* turn off MPLS */
1623 sprintf(pg_result, "OK: svlan_id=%u", pkt_dev->svlan_id);
1624 } else {
1625 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1626 pkt_dev->svlan_id = 0xffff;
1628 if (debug)
1629 pr_debug("VLAN/SVLAN turned off\n");
1631 return count;
1634 if (!strcmp(name, "svlan_p")) {
1635 len = num_arg(&user_buffer[i], 1, &value);
1636 if (len < 0)
1637 return len;
1639 i += len;
1640 if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) {
1641 pkt_dev->svlan_p = value;
1642 sprintf(pg_result, "OK: svlan_p=%u", pkt_dev->svlan_p);
1643 } else {
1644 sprintf(pg_result, "ERROR: svlan_p must be 0-7");
1646 return count;
1649 if (!strcmp(name, "svlan_cfi")) {
1650 len = num_arg(&user_buffer[i], 1, &value);
1651 if (len < 0)
1652 return len;
1654 i += len;
1655 if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) {
1656 pkt_dev->svlan_cfi = value;
1657 sprintf(pg_result, "OK: svlan_cfi=%u", pkt_dev->svlan_cfi);
1658 } else {
1659 sprintf(pg_result, "ERROR: svlan_cfi must be 0-1");
1661 return count;
1664 if (!strcmp(name, "tos")) {
1665 __u32 tmp_value = 0;
1666 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1667 if (len < 0)
1668 return len;
1670 i += len;
1671 if (len == 2) {
1672 pkt_dev->tos = tmp_value;
1673 sprintf(pg_result, "OK: tos=0x%02x", pkt_dev->tos);
1674 } else {
1675 sprintf(pg_result, "ERROR: tos must be 00-ff");
1677 return count;
1680 if (!strcmp(name, "traffic_class")) {
1681 __u32 tmp_value = 0;
1682 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1683 if (len < 0)
1684 return len;
1686 i += len;
1687 if (len == 2) {
1688 pkt_dev->traffic_class = tmp_value;
1689 sprintf(pg_result, "OK: traffic_class=0x%02x", pkt_dev->traffic_class);
1690 } else {
1691 sprintf(pg_result, "ERROR: traffic_class must be 00-ff");
1693 return count;
1696 if (!strcmp(name, "skb_priority")) {
1697 len = num_arg(&user_buffer[i], 9, &value);
1698 if (len < 0)
1699 return len;
1701 i += len;
1702 pkt_dev->skb_priority = value;
1703 sprintf(pg_result, "OK: skb_priority=%i",
1704 pkt_dev->skb_priority);
1705 return count;
1708 sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
1709 return -EINVAL;
1712 static int pktgen_if_open(struct inode *inode, struct file *file)
1714 return single_open(file, pktgen_if_show, PDE_DATA(inode));
1717 static const struct file_operations pktgen_if_fops = {
1718 .open = pktgen_if_open,
1719 .read = seq_read,
1720 .llseek = seq_lseek,
1721 .write = pktgen_if_write,
1722 .release = single_release,
1725 static int pktgen_thread_show(struct seq_file *seq, void *v)
1727 struct pktgen_thread *t = seq->private;
1728 const struct pktgen_dev *pkt_dev;
1730 BUG_ON(!t);
1732 seq_puts(seq, "Running: ");
1734 rcu_read_lock();
1735 list_for_each_entry_rcu(pkt_dev, &t->if_list, list)
1736 if (pkt_dev->running)
1737 seq_printf(seq, "%s ", pkt_dev->odevname);
1739 seq_puts(seq, "\nStopped: ");
1741 list_for_each_entry_rcu(pkt_dev, &t->if_list, list)
1742 if (!pkt_dev->running)
1743 seq_printf(seq, "%s ", pkt_dev->odevname);
1745 if (t->result[0])
1746 seq_printf(seq, "\nResult: %s\n", t->result);
1747 else
1748 seq_puts(seq, "\nResult: NA\n");
1750 rcu_read_unlock();
1752 return 0;
1755 static ssize_t pktgen_thread_write(struct file *file,
1756 const char __user * user_buffer,
1757 size_t count, loff_t * offset)
1759 struct seq_file *seq = file->private_data;
1760 struct pktgen_thread *t = seq->private;
1761 int i, max, len, ret;
1762 char name[40];
1763 char *pg_result;
1765 if (count < 1) {
1766 // sprintf(pg_result, "Wrong command format");
1767 return -EINVAL;
1770 max = count;
1771 len = count_trail_chars(user_buffer, max);
1772 if (len < 0)
1773 return len;
1775 i = len;
1777 /* Read variable name */
1779 len = strn_len(&user_buffer[i], sizeof(name) - 1);
1780 if (len < 0)
1781 return len;
1783 memset(name, 0, sizeof(name));
1784 if (copy_from_user(name, &user_buffer[i], len))
1785 return -EFAULT;
1786 i += len;
1788 max = count - i;
1789 len = count_trail_chars(&user_buffer[i], max);
1790 if (len < 0)
1791 return len;
1793 i += len;
1795 if (debug)
1796 pr_debug("t=%s, count=%lu\n", name, (unsigned long)count);
1798 if (!t) {
1799 pr_err("ERROR: No thread\n");
1800 ret = -EINVAL;
1801 goto out;
1804 pg_result = &(t->result[0]);
1806 if (!strcmp(name, "add_device")) {
1807 char f[32];
1808 memset(f, 0, 32);
1809 len = strn_len(&user_buffer[i], sizeof(f) - 1);
1810 if (len < 0) {
1811 ret = len;
1812 goto out;
1814 if (copy_from_user(f, &user_buffer[i], len))
1815 return -EFAULT;
1816 i += len;
1817 mutex_lock(&pktgen_thread_lock);
1818 ret = pktgen_add_device(t, f);
1819 mutex_unlock(&pktgen_thread_lock);
1820 if (!ret) {
1821 ret = count;
1822 sprintf(pg_result, "OK: add_device=%s", f);
1823 } else
1824 sprintf(pg_result, "ERROR: can not add device %s", f);
1825 goto out;
1828 if (!strcmp(name, "rem_device_all")) {
1829 mutex_lock(&pktgen_thread_lock);
1830 t->control |= T_REMDEVALL;
1831 mutex_unlock(&pktgen_thread_lock);
1832 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
1833 ret = count;
1834 sprintf(pg_result, "OK: rem_device_all");
1835 goto out;
1838 if (!strcmp(name, "max_before_softirq")) {
1839 sprintf(pg_result, "OK: Note! max_before_softirq is obsoleted -- Do not use");
1840 ret = count;
1841 goto out;
1844 ret = -EINVAL;
1845 out:
1846 return ret;
1849 static int pktgen_thread_open(struct inode *inode, struct file *file)
1851 return single_open(file, pktgen_thread_show, PDE_DATA(inode));
1854 static const struct file_operations pktgen_thread_fops = {
1855 .open = pktgen_thread_open,
1856 .read = seq_read,
1857 .llseek = seq_lseek,
1858 .write = pktgen_thread_write,
1859 .release = single_release,
1862 /* Think find or remove for NN */
1863 static struct pktgen_dev *__pktgen_NN_threads(const struct pktgen_net *pn,
1864 const char *ifname, int remove)
1866 struct pktgen_thread *t;
1867 struct pktgen_dev *pkt_dev = NULL;
1868 bool exact = (remove == FIND);
1870 list_for_each_entry(t, &pn->pktgen_threads, th_list) {
1871 pkt_dev = pktgen_find_dev(t, ifname, exact);
1872 if (pkt_dev) {
1873 if (remove) {
1874 pkt_dev->removal_mark = 1;
1875 t->control |= T_REMDEV;
1877 break;
1880 return pkt_dev;
1884 * mark a device for removal
1886 static void pktgen_mark_device(const struct pktgen_net *pn, const char *ifname)
1888 struct pktgen_dev *pkt_dev = NULL;
1889 const int max_tries = 10, msec_per_try = 125;
1890 int i = 0;
1892 mutex_lock(&pktgen_thread_lock);
1893 pr_debug("%s: marking %s for removal\n", __func__, ifname);
1895 while (1) {
1897 pkt_dev = __pktgen_NN_threads(pn, ifname, REMOVE);
1898 if (pkt_dev == NULL)
1899 break; /* success */
1901 mutex_unlock(&pktgen_thread_lock);
1902 pr_debug("%s: waiting for %s to disappear....\n",
1903 __func__, ifname);
1904 schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
1905 mutex_lock(&pktgen_thread_lock);
1907 if (++i >= max_tries) {
1908 pr_err("%s: timed out after waiting %d msec for device %s to be removed\n",
1909 __func__, msec_per_try * i, ifname);
1910 break;
1915 mutex_unlock(&pktgen_thread_lock);
1918 static void pktgen_change_name(const struct pktgen_net *pn, struct net_device *dev)
1920 struct pktgen_thread *t;
1922 mutex_lock(&pktgen_thread_lock);
1924 list_for_each_entry(t, &pn->pktgen_threads, th_list) {
1925 struct pktgen_dev *pkt_dev;
1927 if_lock(t);
1928 list_for_each_entry(pkt_dev, &t->if_list, list) {
1929 if (pkt_dev->odev != dev)
1930 continue;
1932 proc_remove(pkt_dev->entry);
1934 pkt_dev->entry = proc_create_data(dev->name, 0600,
1935 pn->proc_dir,
1936 &pktgen_if_fops,
1937 pkt_dev);
1938 if (!pkt_dev->entry)
1939 pr_err("can't move proc entry for '%s'\n",
1940 dev->name);
1941 break;
1943 if_unlock(t);
1945 mutex_unlock(&pktgen_thread_lock);
1948 static int pktgen_device_event(struct notifier_block *unused,
1949 unsigned long event, void *ptr)
1951 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1952 struct pktgen_net *pn = net_generic(dev_net(dev), pg_net_id);
1954 if (pn->pktgen_exiting)
1955 return NOTIFY_DONE;
1957 /* It is OK that we do not hold the group lock right now,
1958 * as we run under the RTNL lock.
1961 switch (event) {
1962 case NETDEV_CHANGENAME:
1963 pktgen_change_name(pn, dev);
1964 break;
1966 case NETDEV_UNREGISTER:
1967 pktgen_mark_device(pn, dev->name);
1968 break;
1971 return NOTIFY_DONE;
1974 static struct net_device *pktgen_dev_get_by_name(const struct pktgen_net *pn,
1975 struct pktgen_dev *pkt_dev,
1976 const char *ifname)
1978 char b[IFNAMSIZ+5];
1979 int i;
1981 for (i = 0; ifname[i] != '@'; i++) {
1982 if (i == IFNAMSIZ)
1983 break;
1985 b[i] = ifname[i];
1987 b[i] = 0;
1989 return dev_get_by_name(pn->net, b);
1993 /* Associate pktgen_dev with a device. */
1995 static int pktgen_setup_dev(const struct pktgen_net *pn,
1996 struct pktgen_dev *pkt_dev, const char *ifname)
1998 struct net_device *odev;
1999 int err;
2001 /* Clean old setups */
2002 if (pkt_dev->odev) {
2003 dev_put(pkt_dev->odev);
2004 pkt_dev->odev = NULL;
2007 odev = pktgen_dev_get_by_name(pn, pkt_dev, ifname);
2008 if (!odev) {
2009 pr_err("no such netdevice: \"%s\"\n", ifname);
2010 return -ENODEV;
2013 if (odev->type != ARPHRD_ETHER) {
2014 pr_err("not an ethernet device: \"%s\"\n", ifname);
2015 err = -EINVAL;
2016 } else if (!netif_running(odev)) {
2017 pr_err("device is down: \"%s\"\n", ifname);
2018 err = -ENETDOWN;
2019 } else {
2020 pkt_dev->odev = odev;
2021 return 0;
2024 dev_put(odev);
2025 return err;
2028 /* Read pkt_dev from the interface and set up internal pktgen_dev
2029 * structure to have the right information to create/send packets
2031 static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
2033 int ntxq;
2035 if (!pkt_dev->odev) {
2036 pr_err("ERROR: pkt_dev->odev == NULL in setup_inject\n");
2037 sprintf(pkt_dev->result,
2038 "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
2039 return;
2042 /* make sure that we don't pick a non-existing transmit queue */
2043 ntxq = pkt_dev->odev->real_num_tx_queues;
2045 if (ntxq <= pkt_dev->queue_map_min) {
2046 pr_warn("WARNING: Requested queue_map_min (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
2047 pkt_dev->queue_map_min, (ntxq ?: 1) - 1, ntxq,
2048 pkt_dev->odevname);
2049 pkt_dev->queue_map_min = (ntxq ?: 1) - 1;
2051 if (pkt_dev->queue_map_max >= ntxq) {
2052 pr_warn("WARNING: Requested queue_map_max (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
2053 pkt_dev->queue_map_max, (ntxq ?: 1) - 1, ntxq,
2054 pkt_dev->odevname);
2055 pkt_dev->queue_map_max = (ntxq ?: 1) - 1;
2058 /* Default to the interface's mac if not explicitly set. */
2060 if (is_zero_ether_addr(pkt_dev->src_mac))
2061 ether_addr_copy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr);
2063 /* Set up Dest MAC */
2064 ether_addr_copy(&(pkt_dev->hh[0]), pkt_dev->dst_mac);
2066 if (pkt_dev->flags & F_IPV6) {
2067 int i, set = 0, err = 1;
2068 struct inet6_dev *idev;
2070 if (pkt_dev->min_pkt_size == 0) {
2071 pkt_dev->min_pkt_size = 14 + sizeof(struct ipv6hdr)
2072 + sizeof(struct udphdr)
2073 + sizeof(struct pktgen_hdr)
2074 + pkt_dev->pkt_overhead;
2077 for (i = 0; i < sizeof(struct in6_addr); i++)
2078 if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
2079 set = 1;
2080 break;
2083 if (!set) {
2086 * Use linklevel address if unconfigured.
2088 * use ipv6_get_lladdr if/when it's get exported
2091 rcu_read_lock();
2092 idev = __in6_dev_get(pkt_dev->odev);
2093 if (idev) {
2094 struct inet6_ifaddr *ifp;
2096 read_lock_bh(&idev->lock);
2097 list_for_each_entry(ifp, &idev->addr_list, if_list) {
2098 if ((ifp->scope & IFA_LINK) &&
2099 !(ifp->flags & IFA_F_TENTATIVE)) {
2100 pkt_dev->cur_in6_saddr = ifp->addr;
2101 err = 0;
2102 break;
2105 read_unlock_bh(&idev->lock);
2107 rcu_read_unlock();
2108 if (err)
2109 pr_err("ERROR: IPv6 link address not available\n");
2111 } else {
2112 if (pkt_dev->min_pkt_size == 0) {
2113 pkt_dev->min_pkt_size = 14 + sizeof(struct iphdr)
2114 + sizeof(struct udphdr)
2115 + sizeof(struct pktgen_hdr)
2116 + pkt_dev->pkt_overhead;
2119 pkt_dev->saddr_min = 0;
2120 pkt_dev->saddr_max = 0;
2121 if (strlen(pkt_dev->src_min) == 0) {
2123 struct in_device *in_dev;
2125 rcu_read_lock();
2126 in_dev = __in_dev_get_rcu(pkt_dev->odev);
2127 if (in_dev) {
2128 if (in_dev->ifa_list) {
2129 pkt_dev->saddr_min =
2130 in_dev->ifa_list->ifa_address;
2131 pkt_dev->saddr_max = pkt_dev->saddr_min;
2134 rcu_read_unlock();
2135 } else {
2136 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
2137 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
2140 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
2141 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
2143 /* Initialize current values. */
2144 pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
2145 if (pkt_dev->min_pkt_size > pkt_dev->max_pkt_size)
2146 pkt_dev->max_pkt_size = pkt_dev->min_pkt_size;
2148 pkt_dev->cur_dst_mac_offset = 0;
2149 pkt_dev->cur_src_mac_offset = 0;
2150 pkt_dev->cur_saddr = pkt_dev->saddr_min;
2151 pkt_dev->cur_daddr = pkt_dev->daddr_min;
2152 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2153 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
2154 pkt_dev->nflows = 0;
2158 static void spin(struct pktgen_dev *pkt_dev, ktime_t spin_until)
2160 ktime_t start_time, end_time;
2161 s64 remaining;
2162 struct hrtimer_sleeper t;
2164 hrtimer_init_on_stack(&t.timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
2165 hrtimer_set_expires(&t.timer, spin_until);
2167 remaining = ktime_to_ns(hrtimer_expires_remaining(&t.timer));
2168 if (remaining <= 0)
2169 goto out;
2171 start_time = ktime_get();
2172 if (remaining < 100000) {
2173 /* for small delays (<100us), just loop until limit is reached */
2174 do {
2175 end_time = ktime_get();
2176 } while (ktime_compare(end_time, spin_until) < 0);
2177 } else {
2178 /* see do_nanosleep */
2179 hrtimer_init_sleeper(&t, current);
2180 do {
2181 set_current_state(TASK_INTERRUPTIBLE);
2182 hrtimer_start_expires(&t.timer, HRTIMER_MODE_ABS);
2184 if (likely(t.task))
2185 schedule();
2187 hrtimer_cancel(&t.timer);
2188 } while (t.task && pkt_dev->running && !signal_pending(current));
2189 __set_current_state(TASK_RUNNING);
2190 end_time = ktime_get();
2193 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(end_time, start_time));
2194 out:
2195 pkt_dev->next_tx = ktime_add_ns(spin_until, pkt_dev->delay);
2196 destroy_hrtimer_on_stack(&t.timer);
2199 static inline void set_pkt_overhead(struct pktgen_dev *pkt_dev)
2201 pkt_dev->pkt_overhead = 0;
2202 pkt_dev->pkt_overhead += pkt_dev->nr_labels*sizeof(u32);
2203 pkt_dev->pkt_overhead += VLAN_TAG_SIZE(pkt_dev);
2204 pkt_dev->pkt_overhead += SVLAN_TAG_SIZE(pkt_dev);
2207 static inline int f_seen(const struct pktgen_dev *pkt_dev, int flow)
2209 return !!(pkt_dev->flows[flow].flags & F_INIT);
2212 static inline int f_pick(struct pktgen_dev *pkt_dev)
2214 int flow = pkt_dev->curfl;
2216 if (pkt_dev->flags & F_FLOW_SEQ) {
2217 if (pkt_dev->flows[flow].count >= pkt_dev->lflow) {
2218 /* reset time */
2219 pkt_dev->flows[flow].count = 0;
2220 pkt_dev->flows[flow].flags = 0;
2221 pkt_dev->curfl += 1;
2222 if (pkt_dev->curfl >= pkt_dev->cflows)
2223 pkt_dev->curfl = 0; /*reset */
2225 } else {
2226 flow = prandom_u32() % pkt_dev->cflows;
2227 pkt_dev->curfl = flow;
2229 if (pkt_dev->flows[flow].count > pkt_dev->lflow) {
2230 pkt_dev->flows[flow].count = 0;
2231 pkt_dev->flows[flow].flags = 0;
2235 return pkt_dev->curfl;
2239 #ifdef CONFIG_XFRM
2240 /* If there was already an IPSEC SA, we keep it as is, else
2241 * we go look for it ...
2243 #define DUMMY_MARK 0
2244 static void get_ipsec_sa(struct pktgen_dev *pkt_dev, int flow)
2246 struct xfrm_state *x = pkt_dev->flows[flow].x;
2247 struct pktgen_net *pn = net_generic(dev_net(pkt_dev->odev), pg_net_id);
2248 if (!x) {
2250 if (pkt_dev->spi) {
2251 /* We need as quick as possible to find the right SA
2252 * Searching with minimum criteria to archieve this.
2254 x = xfrm_state_lookup_byspi(pn->net, htonl(pkt_dev->spi), AF_INET);
2255 } else {
2256 /* slow path: we dont already have xfrm_state */
2257 x = xfrm_stateonly_find(pn->net, DUMMY_MARK,
2258 (xfrm_address_t *)&pkt_dev->cur_daddr,
2259 (xfrm_address_t *)&pkt_dev->cur_saddr,
2260 AF_INET,
2261 pkt_dev->ipsmode,
2262 pkt_dev->ipsproto, 0);
2264 if (x) {
2265 pkt_dev->flows[flow].x = x;
2266 set_pkt_overhead(pkt_dev);
2267 pkt_dev->pkt_overhead += x->props.header_len;
2272 #endif
2273 static void set_cur_queue_map(struct pktgen_dev *pkt_dev)
2276 if (pkt_dev->flags & F_QUEUE_MAP_CPU)
2277 pkt_dev->cur_queue_map = smp_processor_id();
2279 else if (pkt_dev->queue_map_min <= pkt_dev->queue_map_max) {
2280 __u16 t;
2281 if (pkt_dev->flags & F_QUEUE_MAP_RND) {
2282 t = prandom_u32() %
2283 (pkt_dev->queue_map_max -
2284 pkt_dev->queue_map_min + 1)
2285 + pkt_dev->queue_map_min;
2286 } else {
2287 t = pkt_dev->cur_queue_map + 1;
2288 if (t > pkt_dev->queue_map_max)
2289 t = pkt_dev->queue_map_min;
2291 pkt_dev->cur_queue_map = t;
2293 pkt_dev->cur_queue_map = pkt_dev->cur_queue_map % pkt_dev->odev->real_num_tx_queues;
2296 /* Increment/randomize headers according to flags and current values
2297 * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
2299 static void mod_cur_headers(struct pktgen_dev *pkt_dev)
2301 __u32 imn;
2302 __u32 imx;
2303 int flow = 0;
2305 if (pkt_dev->cflows)
2306 flow = f_pick(pkt_dev);
2308 /* Deal with source MAC */
2309 if (pkt_dev->src_mac_count > 1) {
2310 __u32 mc;
2311 __u32 tmp;
2313 if (pkt_dev->flags & F_MACSRC_RND)
2314 mc = prandom_u32() % pkt_dev->src_mac_count;
2315 else {
2316 mc = pkt_dev->cur_src_mac_offset++;
2317 if (pkt_dev->cur_src_mac_offset >=
2318 pkt_dev->src_mac_count)
2319 pkt_dev->cur_src_mac_offset = 0;
2322 tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
2323 pkt_dev->hh[11] = tmp;
2324 tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2325 pkt_dev->hh[10] = tmp;
2326 tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2327 pkt_dev->hh[9] = tmp;
2328 tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2329 pkt_dev->hh[8] = tmp;
2330 tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
2331 pkt_dev->hh[7] = tmp;
2334 /* Deal with Destination MAC */
2335 if (pkt_dev->dst_mac_count > 1) {
2336 __u32 mc;
2337 __u32 tmp;
2339 if (pkt_dev->flags & F_MACDST_RND)
2340 mc = prandom_u32() % pkt_dev->dst_mac_count;
2342 else {
2343 mc = pkt_dev->cur_dst_mac_offset++;
2344 if (pkt_dev->cur_dst_mac_offset >=
2345 pkt_dev->dst_mac_count) {
2346 pkt_dev->cur_dst_mac_offset = 0;
2350 tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
2351 pkt_dev->hh[5] = tmp;
2352 tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2353 pkt_dev->hh[4] = tmp;
2354 tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2355 pkt_dev->hh[3] = tmp;
2356 tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2357 pkt_dev->hh[2] = tmp;
2358 tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
2359 pkt_dev->hh[1] = tmp;
2362 if (pkt_dev->flags & F_MPLS_RND) {
2363 unsigned int i;
2364 for (i = 0; i < pkt_dev->nr_labels; i++)
2365 if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
2366 pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
2367 ((__force __be32)prandom_u32() &
2368 htonl(0x000fffff));
2371 if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
2372 pkt_dev->vlan_id = prandom_u32() & (4096 - 1);
2375 if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
2376 pkt_dev->svlan_id = prandom_u32() & (4096 - 1);
2379 if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
2380 if (pkt_dev->flags & F_UDPSRC_RND)
2381 pkt_dev->cur_udp_src = prandom_u32() %
2382 (pkt_dev->udp_src_max - pkt_dev->udp_src_min)
2383 + pkt_dev->udp_src_min;
2385 else {
2386 pkt_dev->cur_udp_src++;
2387 if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
2388 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
2392 if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
2393 if (pkt_dev->flags & F_UDPDST_RND) {
2394 pkt_dev->cur_udp_dst = prandom_u32() %
2395 (pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)
2396 + pkt_dev->udp_dst_min;
2397 } else {
2398 pkt_dev->cur_udp_dst++;
2399 if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
2400 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2404 if (!(pkt_dev->flags & F_IPV6)) {
2406 imn = ntohl(pkt_dev->saddr_min);
2407 imx = ntohl(pkt_dev->saddr_max);
2408 if (imn < imx) {
2409 __u32 t;
2410 if (pkt_dev->flags & F_IPSRC_RND)
2411 t = prandom_u32() % (imx - imn) + imn;
2412 else {
2413 t = ntohl(pkt_dev->cur_saddr);
2414 t++;
2415 if (t > imx)
2416 t = imn;
2419 pkt_dev->cur_saddr = htonl(t);
2422 if (pkt_dev->cflows && f_seen(pkt_dev, flow)) {
2423 pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
2424 } else {
2425 imn = ntohl(pkt_dev->daddr_min);
2426 imx = ntohl(pkt_dev->daddr_max);
2427 if (imn < imx) {
2428 __u32 t;
2429 __be32 s;
2430 if (pkt_dev->flags & F_IPDST_RND) {
2432 do {
2433 t = prandom_u32() %
2434 (imx - imn) + imn;
2435 s = htonl(t);
2436 } while (ipv4_is_loopback(s) ||
2437 ipv4_is_multicast(s) ||
2438 ipv4_is_lbcast(s) ||
2439 ipv4_is_zeronet(s) ||
2440 ipv4_is_local_multicast(s));
2441 pkt_dev->cur_daddr = s;
2442 } else {
2443 t = ntohl(pkt_dev->cur_daddr);
2444 t++;
2445 if (t > imx) {
2446 t = imn;
2448 pkt_dev->cur_daddr = htonl(t);
2451 if (pkt_dev->cflows) {
2452 pkt_dev->flows[flow].flags |= F_INIT;
2453 pkt_dev->flows[flow].cur_daddr =
2454 pkt_dev->cur_daddr;
2455 #ifdef CONFIG_XFRM
2456 if (pkt_dev->flags & F_IPSEC)
2457 get_ipsec_sa(pkt_dev, flow);
2458 #endif
2459 pkt_dev->nflows++;
2462 } else { /* IPV6 * */
2464 if (!ipv6_addr_any(&pkt_dev->min_in6_daddr)) {
2465 int i;
2467 /* Only random destinations yet */
2469 for (i = 0; i < 4; i++) {
2470 pkt_dev->cur_in6_daddr.s6_addr32[i] =
2471 (((__force __be32)prandom_u32() |
2472 pkt_dev->min_in6_daddr.s6_addr32[i]) &
2473 pkt_dev->max_in6_daddr.s6_addr32[i]);
2478 if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
2479 __u32 t;
2480 if (pkt_dev->flags & F_TXSIZE_RND) {
2481 t = prandom_u32() %
2482 (pkt_dev->max_pkt_size - pkt_dev->min_pkt_size)
2483 + pkt_dev->min_pkt_size;
2484 } else {
2485 t = pkt_dev->cur_pkt_size + 1;
2486 if (t > pkt_dev->max_pkt_size)
2487 t = pkt_dev->min_pkt_size;
2489 pkt_dev->cur_pkt_size = t;
2492 set_cur_queue_map(pkt_dev);
2494 pkt_dev->flows[flow].count++;
2498 #ifdef CONFIG_XFRM
2499 static u32 pktgen_dst_metrics[RTAX_MAX + 1] = {
2501 [RTAX_HOPLIMIT] = 0x5, /* Set a static hoplimit */
2504 static int pktgen_output_ipsec(struct sk_buff *skb, struct pktgen_dev *pkt_dev)
2506 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2507 int err = 0;
2508 struct net *net = dev_net(pkt_dev->odev);
2510 if (!x)
2511 return 0;
2512 /* XXX: we dont support tunnel mode for now until
2513 * we resolve the dst issue */
2514 if ((x->props.mode != XFRM_MODE_TRANSPORT) && (pkt_dev->spi == 0))
2515 return 0;
2517 /* But when user specify an valid SPI, transformation
2518 * supports both transport/tunnel mode + ESP/AH type.
2520 if ((x->props.mode == XFRM_MODE_TUNNEL) && (pkt_dev->spi != 0))
2521 skb->_skb_refdst = (unsigned long)&pkt_dev->xdst.u.dst | SKB_DST_NOREF;
2523 rcu_read_lock_bh();
2524 err = x->outer_mode->output(x, skb);
2525 rcu_read_unlock_bh();
2526 if (err) {
2527 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEMODEERROR);
2528 goto error;
2530 err = x->type->output(x, skb);
2531 if (err) {
2532 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEPROTOERROR);
2533 goto error;
2535 spin_lock_bh(&x->lock);
2536 x->curlft.bytes += skb->len;
2537 x->curlft.packets++;
2538 spin_unlock_bh(&x->lock);
2539 error:
2540 return err;
2543 static void free_SAs(struct pktgen_dev *pkt_dev)
2545 if (pkt_dev->cflows) {
2546 /* let go of the SAs if we have them */
2547 int i;
2548 for (i = 0; i < pkt_dev->cflows; i++) {
2549 struct xfrm_state *x = pkt_dev->flows[i].x;
2550 if (x) {
2551 xfrm_state_put(x);
2552 pkt_dev->flows[i].x = NULL;
2558 static int process_ipsec(struct pktgen_dev *pkt_dev,
2559 struct sk_buff *skb, __be16 protocol)
2561 if (pkt_dev->flags & F_IPSEC) {
2562 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2563 int nhead = 0;
2564 if (x) {
2565 struct ethhdr *eth;
2566 struct iphdr *iph;
2567 int ret;
2569 nhead = x->props.header_len - skb_headroom(skb);
2570 if (nhead > 0) {
2571 ret = pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
2572 if (ret < 0) {
2573 pr_err("Error expanding ipsec packet %d\n",
2574 ret);
2575 goto err;
2579 /* ipsec is not expecting ll header */
2580 skb_pull(skb, ETH_HLEN);
2581 ret = pktgen_output_ipsec(skb, pkt_dev);
2582 if (ret) {
2583 pr_err("Error creating ipsec packet %d\n", ret);
2584 goto err;
2586 /* restore ll */
2587 eth = skb_push(skb, ETH_HLEN);
2588 memcpy(eth, pkt_dev->hh, 2 * ETH_ALEN);
2589 eth->h_proto = protocol;
2591 /* Update IPv4 header len as well as checksum value */
2592 iph = ip_hdr(skb);
2593 iph->tot_len = htons(skb->len - ETH_HLEN);
2594 ip_send_check(iph);
2597 return 1;
2598 err:
2599 kfree_skb(skb);
2600 return 0;
2602 #endif
2604 static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
2606 unsigned int i;
2607 for (i = 0; i < pkt_dev->nr_labels; i++)
2608 *mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
2610 mpls--;
2611 *mpls |= MPLS_STACK_BOTTOM;
2614 static inline __be16 build_tci(unsigned int id, unsigned int cfi,
2615 unsigned int prio)
2617 return htons(id | (cfi << 12) | (prio << 13));
2620 static void pktgen_finalize_skb(struct pktgen_dev *pkt_dev, struct sk_buff *skb,
2621 int datalen)
2623 struct timespec64 timestamp;
2624 struct pktgen_hdr *pgh;
2626 pgh = skb_put(skb, sizeof(*pgh));
2627 datalen -= sizeof(*pgh);
2629 if (pkt_dev->nfrags <= 0) {
2630 skb_put_zero(skb, datalen);
2631 } else {
2632 int frags = pkt_dev->nfrags;
2633 int i, len;
2634 int frag_len;
2637 if (frags > MAX_SKB_FRAGS)
2638 frags = MAX_SKB_FRAGS;
2639 len = datalen - frags * PAGE_SIZE;
2640 if (len > 0) {
2641 skb_put_zero(skb, len);
2642 datalen = frags * PAGE_SIZE;
2645 i = 0;
2646 frag_len = (datalen/frags) < PAGE_SIZE ?
2647 (datalen/frags) : PAGE_SIZE;
2648 while (datalen > 0) {
2649 if (unlikely(!pkt_dev->page)) {
2650 int node = numa_node_id();
2652 if (pkt_dev->node >= 0 && (pkt_dev->flags & F_NODE))
2653 node = pkt_dev->node;
2654 pkt_dev->page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
2655 if (!pkt_dev->page)
2656 break;
2658 get_page(pkt_dev->page);
2659 skb_frag_set_page(skb, i, pkt_dev->page);
2660 skb_shinfo(skb)->frags[i].page_offset = 0;
2661 /*last fragment, fill rest of data*/
2662 if (i == (frags - 1))
2663 skb_frag_size_set(&skb_shinfo(skb)->frags[i],
2664 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE));
2665 else
2666 skb_frag_size_set(&skb_shinfo(skb)->frags[i], frag_len);
2667 datalen -= skb_frag_size(&skb_shinfo(skb)->frags[i]);
2668 skb->len += skb_frag_size(&skb_shinfo(skb)->frags[i]);
2669 skb->data_len += skb_frag_size(&skb_shinfo(skb)->frags[i]);
2670 i++;
2671 skb_shinfo(skb)->nr_frags = i;
2675 /* Stamp the time, and sequence number,
2676 * convert them to network byte order
2678 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2679 pgh->seq_num = htonl(pkt_dev->seq_num);
2681 if (pkt_dev->flags & F_NO_TIMESTAMP) {
2682 pgh->tv_sec = 0;
2683 pgh->tv_usec = 0;
2684 } else {
2686 * pgh->tv_sec wraps in y2106 when interpreted as unsigned
2687 * as done by wireshark, or y2038 when interpreted as signed.
2688 * This is probably harmless, but if anyone wants to improve
2689 * it, we could introduce a variant that puts 64-bit nanoseconds
2690 * into the respective header bytes.
2691 * This would also be slightly faster to read.
2693 ktime_get_real_ts64(&timestamp);
2694 pgh->tv_sec = htonl(timestamp.tv_sec);
2695 pgh->tv_usec = htonl(timestamp.tv_nsec / NSEC_PER_USEC);
2699 static struct sk_buff *pktgen_alloc_skb(struct net_device *dev,
2700 struct pktgen_dev *pkt_dev)
2702 unsigned int extralen = LL_RESERVED_SPACE(dev);
2703 struct sk_buff *skb = NULL;
2704 unsigned int size;
2706 size = pkt_dev->cur_pkt_size + 64 + extralen + pkt_dev->pkt_overhead;
2707 if (pkt_dev->flags & F_NODE) {
2708 int node = pkt_dev->node >= 0 ? pkt_dev->node : numa_node_id();
2710 skb = __alloc_skb(NET_SKB_PAD + size, GFP_NOWAIT, 0, node);
2711 if (likely(skb)) {
2712 skb_reserve(skb, NET_SKB_PAD);
2713 skb->dev = dev;
2715 } else {
2716 skb = __netdev_alloc_skb(dev, size, GFP_NOWAIT);
2719 /* the caller pre-fetches from skb->data and reserves for the mac hdr */
2720 if (likely(skb))
2721 skb_reserve(skb, extralen - 16);
2723 return skb;
2726 static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
2727 struct pktgen_dev *pkt_dev)
2729 struct sk_buff *skb = NULL;
2730 __u8 *eth;
2731 struct udphdr *udph;
2732 int datalen, iplen;
2733 struct iphdr *iph;
2734 __be16 protocol = htons(ETH_P_IP);
2735 __be32 *mpls;
2736 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2737 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2738 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2739 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2740 u16 queue_map;
2742 if (pkt_dev->nr_labels)
2743 protocol = htons(ETH_P_MPLS_UC);
2745 if (pkt_dev->vlan_id != 0xffff)
2746 protocol = htons(ETH_P_8021Q);
2748 /* Update any of the values, used when we're incrementing various
2749 * fields.
2751 mod_cur_headers(pkt_dev);
2752 queue_map = pkt_dev->cur_queue_map;
2754 skb = pktgen_alloc_skb(odev, pkt_dev);
2755 if (!skb) {
2756 sprintf(pkt_dev->result, "No memory");
2757 return NULL;
2760 prefetchw(skb->data);
2761 skb_reserve(skb, 16);
2763 /* Reserve for ethernet and IP header */
2764 eth = skb_push(skb, 14);
2765 mpls = skb_put(skb, pkt_dev->nr_labels * sizeof(__u32));
2766 if (pkt_dev->nr_labels)
2767 mpls_push(mpls, pkt_dev);
2769 if (pkt_dev->vlan_id != 0xffff) {
2770 if (pkt_dev->svlan_id != 0xffff) {
2771 svlan_tci = skb_put(skb, sizeof(__be16));
2772 *svlan_tci = build_tci(pkt_dev->svlan_id,
2773 pkt_dev->svlan_cfi,
2774 pkt_dev->svlan_p);
2775 svlan_encapsulated_proto = skb_put(skb,
2776 sizeof(__be16));
2777 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
2779 vlan_tci = skb_put(skb, sizeof(__be16));
2780 *vlan_tci = build_tci(pkt_dev->vlan_id,
2781 pkt_dev->vlan_cfi,
2782 pkt_dev->vlan_p);
2783 vlan_encapsulated_proto = skb_put(skb, sizeof(__be16));
2784 *vlan_encapsulated_proto = htons(ETH_P_IP);
2787 skb_reset_mac_header(skb);
2788 skb_set_network_header(skb, skb->len);
2789 iph = skb_put(skb, sizeof(struct iphdr));
2791 skb_set_transport_header(skb, skb->len);
2792 udph = skb_put(skb, sizeof(struct udphdr));
2793 skb_set_queue_mapping(skb, queue_map);
2794 skb->priority = pkt_dev->skb_priority;
2796 memcpy(eth, pkt_dev->hh, 12);
2797 *(__be16 *) & eth[12] = protocol;
2799 /* Eth + IPh + UDPh + mpls */
2800 datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
2801 pkt_dev->pkt_overhead;
2802 if (datalen < 0 || datalen < sizeof(struct pktgen_hdr))
2803 datalen = sizeof(struct pktgen_hdr);
2805 udph->source = htons(pkt_dev->cur_udp_src);
2806 udph->dest = htons(pkt_dev->cur_udp_dst);
2807 udph->len = htons(datalen + 8); /* DATA + udphdr */
2808 udph->check = 0;
2810 iph->ihl = 5;
2811 iph->version = 4;
2812 iph->ttl = 32;
2813 iph->tos = pkt_dev->tos;
2814 iph->protocol = IPPROTO_UDP; /* UDP */
2815 iph->saddr = pkt_dev->cur_saddr;
2816 iph->daddr = pkt_dev->cur_daddr;
2817 iph->id = htons(pkt_dev->ip_id);
2818 pkt_dev->ip_id++;
2819 iph->frag_off = 0;
2820 iplen = 20 + 8 + datalen;
2821 iph->tot_len = htons(iplen);
2822 ip_send_check(iph);
2823 skb->protocol = protocol;
2824 skb->dev = odev;
2825 skb->pkt_type = PACKET_HOST;
2827 pktgen_finalize_skb(pkt_dev, skb, datalen);
2829 if (!(pkt_dev->flags & F_UDPCSUM)) {
2830 skb->ip_summed = CHECKSUM_NONE;
2831 } else if (odev->features & (NETIF_F_HW_CSUM | NETIF_F_IP_CSUM)) {
2832 skb->ip_summed = CHECKSUM_PARTIAL;
2833 skb->csum = 0;
2834 udp4_hwcsum(skb, iph->saddr, iph->daddr);
2835 } else {
2836 __wsum csum = skb_checksum(skb, skb_transport_offset(skb), datalen + 8, 0);
2838 /* add protocol-dependent pseudo-header */
2839 udph->check = csum_tcpudp_magic(iph->saddr, iph->daddr,
2840 datalen + 8, IPPROTO_UDP, csum);
2842 if (udph->check == 0)
2843 udph->check = CSUM_MANGLED_0;
2846 #ifdef CONFIG_XFRM
2847 if (!process_ipsec(pkt_dev, skb, protocol))
2848 return NULL;
2849 #endif
2851 return skb;
2854 static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
2855 struct pktgen_dev *pkt_dev)
2857 struct sk_buff *skb = NULL;
2858 __u8 *eth;
2859 struct udphdr *udph;
2860 int datalen, udplen;
2861 struct ipv6hdr *iph;
2862 __be16 protocol = htons(ETH_P_IPV6);
2863 __be32 *mpls;
2864 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2865 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2866 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2867 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2868 u16 queue_map;
2870 if (pkt_dev->nr_labels)
2871 protocol = htons(ETH_P_MPLS_UC);
2873 if (pkt_dev->vlan_id != 0xffff)
2874 protocol = htons(ETH_P_8021Q);
2876 /* Update any of the values, used when we're incrementing various
2877 * fields.
2879 mod_cur_headers(pkt_dev);
2880 queue_map = pkt_dev->cur_queue_map;
2882 skb = pktgen_alloc_skb(odev, pkt_dev);
2883 if (!skb) {
2884 sprintf(pkt_dev->result, "No memory");
2885 return NULL;
2888 prefetchw(skb->data);
2889 skb_reserve(skb, 16);
2891 /* Reserve for ethernet and IP header */
2892 eth = skb_push(skb, 14);
2893 mpls = skb_put(skb, pkt_dev->nr_labels * sizeof(__u32));
2894 if (pkt_dev->nr_labels)
2895 mpls_push(mpls, pkt_dev);
2897 if (pkt_dev->vlan_id != 0xffff) {
2898 if (pkt_dev->svlan_id != 0xffff) {
2899 svlan_tci = skb_put(skb, sizeof(__be16));
2900 *svlan_tci = build_tci(pkt_dev->svlan_id,
2901 pkt_dev->svlan_cfi,
2902 pkt_dev->svlan_p);
2903 svlan_encapsulated_proto = skb_put(skb,
2904 sizeof(__be16));
2905 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
2907 vlan_tci = skb_put(skb, sizeof(__be16));
2908 *vlan_tci = build_tci(pkt_dev->vlan_id,
2909 pkt_dev->vlan_cfi,
2910 pkt_dev->vlan_p);
2911 vlan_encapsulated_proto = skb_put(skb, sizeof(__be16));
2912 *vlan_encapsulated_proto = htons(ETH_P_IPV6);
2915 skb_reset_mac_header(skb);
2916 skb_set_network_header(skb, skb->len);
2917 iph = skb_put(skb, sizeof(struct ipv6hdr));
2919 skb_set_transport_header(skb, skb->len);
2920 udph = skb_put(skb, sizeof(struct udphdr));
2921 skb_set_queue_mapping(skb, queue_map);
2922 skb->priority = pkt_dev->skb_priority;
2924 memcpy(eth, pkt_dev->hh, 12);
2925 *(__be16 *) &eth[12] = protocol;
2927 /* Eth + IPh + UDPh + mpls */
2928 datalen = pkt_dev->cur_pkt_size - 14 -
2929 sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
2930 pkt_dev->pkt_overhead;
2932 if (datalen < 0 || datalen < sizeof(struct pktgen_hdr)) {
2933 datalen = sizeof(struct pktgen_hdr);
2934 net_info_ratelimited("increased datalen to %d\n", datalen);
2937 udplen = datalen + sizeof(struct udphdr);
2938 udph->source = htons(pkt_dev->cur_udp_src);
2939 udph->dest = htons(pkt_dev->cur_udp_dst);
2940 udph->len = htons(udplen);
2941 udph->check = 0;
2943 *(__be32 *) iph = htonl(0x60000000); /* Version + flow */
2945 if (pkt_dev->traffic_class) {
2946 /* Version + traffic class + flow (0) */
2947 *(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
2950 iph->hop_limit = 32;
2952 iph->payload_len = htons(udplen);
2953 iph->nexthdr = IPPROTO_UDP;
2955 iph->daddr = pkt_dev->cur_in6_daddr;
2956 iph->saddr = pkt_dev->cur_in6_saddr;
2958 skb->protocol = protocol;
2959 skb->dev = odev;
2960 skb->pkt_type = PACKET_HOST;
2962 pktgen_finalize_skb(pkt_dev, skb, datalen);
2964 if (!(pkt_dev->flags & F_UDPCSUM)) {
2965 skb->ip_summed = CHECKSUM_NONE;
2966 } else if (odev->features & (NETIF_F_HW_CSUM | NETIF_F_IPV6_CSUM)) {
2967 skb->ip_summed = CHECKSUM_PARTIAL;
2968 skb->csum_start = skb_transport_header(skb) - skb->head;
2969 skb->csum_offset = offsetof(struct udphdr, check);
2970 udph->check = ~csum_ipv6_magic(&iph->saddr, &iph->daddr, udplen, IPPROTO_UDP, 0);
2971 } else {
2972 __wsum csum = skb_checksum(skb, skb_transport_offset(skb), udplen, 0);
2974 /* add protocol-dependent pseudo-header */
2975 udph->check = csum_ipv6_magic(&iph->saddr, &iph->daddr, udplen, IPPROTO_UDP, csum);
2977 if (udph->check == 0)
2978 udph->check = CSUM_MANGLED_0;
2981 return skb;
2984 static struct sk_buff *fill_packet(struct net_device *odev,
2985 struct pktgen_dev *pkt_dev)
2987 if (pkt_dev->flags & F_IPV6)
2988 return fill_packet_ipv6(odev, pkt_dev);
2989 else
2990 return fill_packet_ipv4(odev, pkt_dev);
2993 static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
2995 pkt_dev->seq_num = 1;
2996 pkt_dev->idle_acc = 0;
2997 pkt_dev->sofar = 0;
2998 pkt_dev->tx_bytes = 0;
2999 pkt_dev->errors = 0;
3002 /* Set up structure for sending pkts, clear counters */
3004 static void pktgen_run(struct pktgen_thread *t)
3006 struct pktgen_dev *pkt_dev;
3007 int started = 0;
3009 func_enter();
3011 rcu_read_lock();
3012 list_for_each_entry_rcu(pkt_dev, &t->if_list, list) {
3015 * setup odev and create initial packet.
3017 pktgen_setup_inject(pkt_dev);
3019 if (pkt_dev->odev) {
3020 pktgen_clear_counters(pkt_dev);
3021 pkt_dev->skb = NULL;
3022 pkt_dev->started_at = pkt_dev->next_tx = ktime_get();
3024 set_pkt_overhead(pkt_dev);
3026 strcpy(pkt_dev->result, "Starting");
3027 pkt_dev->running = 1; /* Cranke yeself! */
3028 started++;
3029 } else
3030 strcpy(pkt_dev->result, "Error starting");
3032 rcu_read_unlock();
3033 if (started)
3034 t->control &= ~(T_STOP);
3037 static void pktgen_stop_all_threads_ifs(struct pktgen_net *pn)
3039 struct pktgen_thread *t;
3041 func_enter();
3043 mutex_lock(&pktgen_thread_lock);
3045 list_for_each_entry(t, &pn->pktgen_threads, th_list)
3046 t->control |= T_STOP;
3048 mutex_unlock(&pktgen_thread_lock);
3051 static int thread_is_running(const struct pktgen_thread *t)
3053 const struct pktgen_dev *pkt_dev;
3055 rcu_read_lock();
3056 list_for_each_entry_rcu(pkt_dev, &t->if_list, list)
3057 if (pkt_dev->running) {
3058 rcu_read_unlock();
3059 return 1;
3061 rcu_read_unlock();
3062 return 0;
3065 static int pktgen_wait_thread_run(struct pktgen_thread *t)
3067 while (thread_is_running(t)) {
3069 msleep_interruptible(100);
3071 if (signal_pending(current))
3072 goto signal;
3074 return 1;
3075 signal:
3076 return 0;
3079 static int pktgen_wait_all_threads_run(struct pktgen_net *pn)
3081 struct pktgen_thread *t;
3082 int sig = 1;
3084 mutex_lock(&pktgen_thread_lock);
3086 list_for_each_entry(t, &pn->pktgen_threads, th_list) {
3087 sig = pktgen_wait_thread_run(t);
3088 if (sig == 0)
3089 break;
3092 if (sig == 0)
3093 list_for_each_entry(t, &pn->pktgen_threads, th_list)
3094 t->control |= (T_STOP);
3096 mutex_unlock(&pktgen_thread_lock);
3097 return sig;
3100 static void pktgen_run_all_threads(struct pktgen_net *pn)
3102 struct pktgen_thread *t;
3104 func_enter();
3106 mutex_lock(&pktgen_thread_lock);
3108 list_for_each_entry(t, &pn->pktgen_threads, th_list)
3109 t->control |= (T_RUN);
3111 mutex_unlock(&pktgen_thread_lock);
3113 /* Propagate thread->control */
3114 schedule_timeout_interruptible(msecs_to_jiffies(125));
3116 pktgen_wait_all_threads_run(pn);
3119 static void pktgen_reset_all_threads(struct pktgen_net *pn)
3121 struct pktgen_thread *t;
3123 func_enter();
3125 mutex_lock(&pktgen_thread_lock);
3127 list_for_each_entry(t, &pn->pktgen_threads, th_list)
3128 t->control |= (T_REMDEVALL);
3130 mutex_unlock(&pktgen_thread_lock);
3132 /* Propagate thread->control */
3133 schedule_timeout_interruptible(msecs_to_jiffies(125));
3135 pktgen_wait_all_threads_run(pn);
3138 static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
3140 __u64 bps, mbps, pps;
3141 char *p = pkt_dev->result;
3142 ktime_t elapsed = ktime_sub(pkt_dev->stopped_at,
3143 pkt_dev->started_at);
3144 ktime_t idle = ns_to_ktime(pkt_dev->idle_acc);
3146 p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
3147 (unsigned long long)ktime_to_us(elapsed),
3148 (unsigned long long)ktime_to_us(ktime_sub(elapsed, idle)),
3149 (unsigned long long)ktime_to_us(idle),
3150 (unsigned long long)pkt_dev->sofar,
3151 pkt_dev->cur_pkt_size, nr_frags);
3153 pps = div64_u64(pkt_dev->sofar * NSEC_PER_SEC,
3154 ktime_to_ns(elapsed));
3156 bps = pps * 8 * pkt_dev->cur_pkt_size;
3158 mbps = bps;
3159 do_div(mbps, 1000000);
3160 p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu",
3161 (unsigned long long)pps,
3162 (unsigned long long)mbps,
3163 (unsigned long long)bps,
3164 (unsigned long long)pkt_dev->errors);
3167 /* Set stopped-at timer, remove from running list, do counters & statistics */
3168 static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
3170 int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
3172 if (!pkt_dev->running) {
3173 pr_warn("interface: %s is already stopped\n",
3174 pkt_dev->odevname);
3175 return -EINVAL;
3178 pkt_dev->running = 0;
3179 kfree_skb(pkt_dev->skb);
3180 pkt_dev->skb = NULL;
3181 pkt_dev->stopped_at = ktime_get();
3183 show_results(pkt_dev, nr_frags);
3185 return 0;
3188 static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
3190 struct pktgen_dev *pkt_dev, *best = NULL;
3192 rcu_read_lock();
3193 list_for_each_entry_rcu(pkt_dev, &t->if_list, list) {
3194 if (!pkt_dev->running)
3195 continue;
3196 if (best == NULL)
3197 best = pkt_dev;
3198 else if (ktime_compare(pkt_dev->next_tx, best->next_tx) < 0)
3199 best = pkt_dev;
3201 rcu_read_unlock();
3203 return best;
3206 static void pktgen_stop(struct pktgen_thread *t)
3208 struct pktgen_dev *pkt_dev;
3210 func_enter();
3212 rcu_read_lock();
3214 list_for_each_entry_rcu(pkt_dev, &t->if_list, list) {
3215 pktgen_stop_device(pkt_dev);
3218 rcu_read_unlock();
3222 * one of our devices needs to be removed - find it
3223 * and remove it
3225 static void pktgen_rem_one_if(struct pktgen_thread *t)
3227 struct list_head *q, *n;
3228 struct pktgen_dev *cur;
3230 func_enter();
3232 list_for_each_safe(q, n, &t->if_list) {
3233 cur = list_entry(q, struct pktgen_dev, list);
3235 if (!cur->removal_mark)
3236 continue;
3238 kfree_skb(cur->skb);
3239 cur->skb = NULL;
3241 pktgen_remove_device(t, cur);
3243 break;
3247 static void pktgen_rem_all_ifs(struct pktgen_thread *t)
3249 struct list_head *q, *n;
3250 struct pktgen_dev *cur;
3252 func_enter();
3254 /* Remove all devices, free mem */
3256 list_for_each_safe(q, n, &t->if_list) {
3257 cur = list_entry(q, struct pktgen_dev, list);
3259 kfree_skb(cur->skb);
3260 cur->skb = NULL;
3262 pktgen_remove_device(t, cur);
3266 static void pktgen_rem_thread(struct pktgen_thread *t)
3268 /* Remove from the thread list */
3269 remove_proc_entry(t->tsk->comm, t->net->proc_dir);
3272 static void pktgen_resched(struct pktgen_dev *pkt_dev)
3274 ktime_t idle_start = ktime_get();
3275 schedule();
3276 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_get(), idle_start));
3279 static void pktgen_wait_for_skb(struct pktgen_dev *pkt_dev)
3281 ktime_t idle_start = ktime_get();
3283 while (refcount_read(&(pkt_dev->skb->users)) != 1) {
3284 if (signal_pending(current))
3285 break;
3287 if (need_resched())
3288 pktgen_resched(pkt_dev);
3289 else
3290 cpu_relax();
3292 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_get(), idle_start));
3295 static void pktgen_xmit(struct pktgen_dev *pkt_dev)
3297 unsigned int burst = READ_ONCE(pkt_dev->burst);
3298 struct net_device *odev = pkt_dev->odev;
3299 struct netdev_queue *txq;
3300 struct sk_buff *skb;
3301 int ret;
3303 /* If device is offline, then don't send */
3304 if (unlikely(!netif_running(odev) || !netif_carrier_ok(odev))) {
3305 pktgen_stop_device(pkt_dev);
3306 return;
3309 /* This is max DELAY, this has special meaning of
3310 * "never transmit"
3312 if (unlikely(pkt_dev->delay == ULLONG_MAX)) {
3313 pkt_dev->next_tx = ktime_add_ns(ktime_get(), ULONG_MAX);
3314 return;
3317 /* If no skb or clone count exhausted then get new one */
3318 if (!pkt_dev->skb || (pkt_dev->last_ok &&
3319 ++pkt_dev->clone_count >= pkt_dev->clone_skb)) {
3320 /* build a new pkt */
3321 kfree_skb(pkt_dev->skb);
3323 pkt_dev->skb = fill_packet(odev, pkt_dev);
3324 if (pkt_dev->skb == NULL) {
3325 pr_err("ERROR: couldn't allocate skb in fill_packet\n");
3326 schedule();
3327 pkt_dev->clone_count--; /* back out increment, OOM */
3328 return;
3330 pkt_dev->last_pkt_size = pkt_dev->skb->len;
3331 pkt_dev->clone_count = 0; /* reset counter */
3334 if (pkt_dev->delay && pkt_dev->last_ok)
3335 spin(pkt_dev, pkt_dev->next_tx);
3337 if (pkt_dev->xmit_mode == M_NETIF_RECEIVE) {
3338 skb = pkt_dev->skb;
3339 skb->protocol = eth_type_trans(skb, skb->dev);
3340 refcount_add(burst, &skb->users);
3341 local_bh_disable();
3342 do {
3343 ret = netif_receive_skb(skb);
3344 if (ret == NET_RX_DROP)
3345 pkt_dev->errors++;
3346 pkt_dev->sofar++;
3347 pkt_dev->seq_num++;
3348 if (refcount_read(&skb->users) != burst) {
3349 /* skb was queued by rps/rfs or taps,
3350 * so cannot reuse this skb
3352 WARN_ON(refcount_sub_and_test(burst - 1, &skb->users));
3353 /* get out of the loop and wait
3354 * until skb is consumed
3356 break;
3358 /* skb was 'freed' by stack, so clean few
3359 * bits and reuse it
3361 skb_reset_tc(skb);
3362 } while (--burst > 0);
3363 goto out; /* Skips xmit_mode M_START_XMIT */
3364 } else if (pkt_dev->xmit_mode == M_QUEUE_XMIT) {
3365 local_bh_disable();
3366 refcount_inc(&pkt_dev->skb->users);
3368 ret = dev_queue_xmit(pkt_dev->skb);
3369 switch (ret) {
3370 case NET_XMIT_SUCCESS:
3371 pkt_dev->sofar++;
3372 pkt_dev->seq_num++;
3373 pkt_dev->tx_bytes += pkt_dev->last_pkt_size;
3374 break;
3375 case NET_XMIT_DROP:
3376 case NET_XMIT_CN:
3377 /* These are all valid return codes for a qdisc but
3378 * indicate packets are being dropped or will likely
3379 * be dropped soon.
3381 case NETDEV_TX_BUSY:
3382 /* qdisc may call dev_hard_start_xmit directly in cases
3383 * where no queues exist e.g. loopback device, virtual
3384 * devices, etc. In this case we need to handle
3385 * NETDEV_TX_ codes.
3387 default:
3388 pkt_dev->errors++;
3389 net_info_ratelimited("%s xmit error: %d\n",
3390 pkt_dev->odevname, ret);
3391 break;
3393 goto out;
3396 txq = skb_get_tx_queue(odev, pkt_dev->skb);
3398 local_bh_disable();
3400 HARD_TX_LOCK(odev, txq, smp_processor_id());
3402 if (unlikely(netif_xmit_frozen_or_drv_stopped(txq))) {
3403 ret = NETDEV_TX_BUSY;
3404 pkt_dev->last_ok = 0;
3405 goto unlock;
3407 refcount_add(burst, &pkt_dev->skb->users);
3409 xmit_more:
3410 ret = netdev_start_xmit(pkt_dev->skb, odev, txq, --burst > 0);
3412 switch (ret) {
3413 case NETDEV_TX_OK:
3414 pkt_dev->last_ok = 1;
3415 pkt_dev->sofar++;
3416 pkt_dev->seq_num++;
3417 pkt_dev->tx_bytes += pkt_dev->last_pkt_size;
3418 if (burst > 0 && !netif_xmit_frozen_or_drv_stopped(txq))
3419 goto xmit_more;
3420 break;
3421 case NET_XMIT_DROP:
3422 case NET_XMIT_CN:
3423 /* skb has been consumed */
3424 pkt_dev->errors++;
3425 break;
3426 default: /* Drivers are not supposed to return other values! */
3427 net_info_ratelimited("%s xmit error: %d\n",
3428 pkt_dev->odevname, ret);
3429 pkt_dev->errors++;
3430 /* fallthru */
3431 case NETDEV_TX_BUSY:
3432 /* Retry it next time */
3433 refcount_dec(&(pkt_dev->skb->users));
3434 pkt_dev->last_ok = 0;
3436 if (unlikely(burst))
3437 WARN_ON(refcount_sub_and_test(burst, &pkt_dev->skb->users));
3438 unlock:
3439 HARD_TX_UNLOCK(odev, txq);
3441 out:
3442 local_bh_enable();
3444 /* If pkt_dev->count is zero, then run forever */
3445 if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
3446 pktgen_wait_for_skb(pkt_dev);
3448 /* Done with this */
3449 pktgen_stop_device(pkt_dev);
3454 * Main loop of the thread goes here
3457 static int pktgen_thread_worker(void *arg)
3459 DEFINE_WAIT(wait);
3460 struct pktgen_thread *t = arg;
3461 struct pktgen_dev *pkt_dev = NULL;
3462 int cpu = t->cpu;
3464 BUG_ON(smp_processor_id() != cpu);
3466 init_waitqueue_head(&t->queue);
3467 complete(&t->start_done);
3469 pr_debug("starting pktgen/%d: pid=%d\n", cpu, task_pid_nr(current));
3471 set_freezable();
3473 while (!kthread_should_stop()) {
3474 pkt_dev = next_to_run(t);
3476 if (unlikely(!pkt_dev && t->control == 0)) {
3477 if (t->net->pktgen_exiting)
3478 break;
3479 wait_event_interruptible_timeout(t->queue,
3480 t->control != 0,
3481 HZ/10);
3482 try_to_freeze();
3483 continue;
3486 if (likely(pkt_dev)) {
3487 pktgen_xmit(pkt_dev);
3489 if (need_resched())
3490 pktgen_resched(pkt_dev);
3491 else
3492 cpu_relax();
3495 if (t->control & T_STOP) {
3496 pktgen_stop(t);
3497 t->control &= ~(T_STOP);
3500 if (t->control & T_RUN) {
3501 pktgen_run(t);
3502 t->control &= ~(T_RUN);
3505 if (t->control & T_REMDEVALL) {
3506 pktgen_rem_all_ifs(t);
3507 t->control &= ~(T_REMDEVALL);
3510 if (t->control & T_REMDEV) {
3511 pktgen_rem_one_if(t);
3512 t->control &= ~(T_REMDEV);
3515 try_to_freeze();
3518 pr_debug("%s stopping all device\n", t->tsk->comm);
3519 pktgen_stop(t);
3521 pr_debug("%s removing all device\n", t->tsk->comm);
3522 pktgen_rem_all_ifs(t);
3524 pr_debug("%s removing thread\n", t->tsk->comm);
3525 pktgen_rem_thread(t);
3527 return 0;
3530 static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
3531 const char *ifname, bool exact)
3533 struct pktgen_dev *p, *pkt_dev = NULL;
3534 size_t len = strlen(ifname);
3536 rcu_read_lock();
3537 list_for_each_entry_rcu(p, &t->if_list, list)
3538 if (strncmp(p->odevname, ifname, len) == 0) {
3539 if (p->odevname[len]) {
3540 if (exact || p->odevname[len] != '@')
3541 continue;
3543 pkt_dev = p;
3544 break;
3547 rcu_read_unlock();
3548 pr_debug("find_dev(%s) returning %p\n", ifname, pkt_dev);
3549 return pkt_dev;
3553 * Adds a dev at front of if_list.
3556 static int add_dev_to_thread(struct pktgen_thread *t,
3557 struct pktgen_dev *pkt_dev)
3559 int rv = 0;
3561 /* This function cannot be called concurrently, as its called
3562 * under pktgen_thread_lock mutex, but it can run from
3563 * userspace on another CPU than the kthread. The if_lock()
3564 * is used here to sync with concurrent instances of
3565 * _rem_dev_from_if_list() invoked via kthread, which is also
3566 * updating the if_list */
3567 if_lock(t);
3569 if (pkt_dev->pg_thread) {
3570 pr_err("ERROR: already assigned to a thread\n");
3571 rv = -EBUSY;
3572 goto out;
3575 pkt_dev->running = 0;
3576 pkt_dev->pg_thread = t;
3577 list_add_rcu(&pkt_dev->list, &t->if_list);
3579 out:
3580 if_unlock(t);
3581 return rv;
3584 /* Called under thread lock */
3586 static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
3588 struct pktgen_dev *pkt_dev;
3589 int err;
3590 int node = cpu_to_node(t->cpu);
3592 /* We don't allow a device to be on several threads */
3594 pkt_dev = __pktgen_NN_threads(t->net, ifname, FIND);
3595 if (pkt_dev) {
3596 pr_err("ERROR: interface already used\n");
3597 return -EBUSY;
3600 pkt_dev = kzalloc_node(sizeof(struct pktgen_dev), GFP_KERNEL, node);
3601 if (!pkt_dev)
3602 return -ENOMEM;
3604 strcpy(pkt_dev->odevname, ifname);
3605 pkt_dev->flows = vzalloc_node(MAX_CFLOWS * sizeof(struct flow_state),
3606 node);
3607 if (pkt_dev->flows == NULL) {
3608 kfree(pkt_dev);
3609 return -ENOMEM;
3612 pkt_dev->removal_mark = 0;
3613 pkt_dev->nfrags = 0;
3614 pkt_dev->delay = pg_delay_d;
3615 pkt_dev->count = pg_count_d;
3616 pkt_dev->sofar = 0;
3617 pkt_dev->udp_src_min = 9; /* sink port */
3618 pkt_dev->udp_src_max = 9;
3619 pkt_dev->udp_dst_min = 9;
3620 pkt_dev->udp_dst_max = 9;
3621 pkt_dev->vlan_p = 0;
3622 pkt_dev->vlan_cfi = 0;
3623 pkt_dev->vlan_id = 0xffff;
3624 pkt_dev->svlan_p = 0;
3625 pkt_dev->svlan_cfi = 0;
3626 pkt_dev->svlan_id = 0xffff;
3627 pkt_dev->burst = 1;
3628 pkt_dev->node = -1;
3630 err = pktgen_setup_dev(t->net, pkt_dev, ifname);
3631 if (err)
3632 goto out1;
3633 if (pkt_dev->odev->priv_flags & IFF_TX_SKB_SHARING)
3634 pkt_dev->clone_skb = pg_clone_skb_d;
3636 pkt_dev->entry = proc_create_data(ifname, 0600, t->net->proc_dir,
3637 &pktgen_if_fops, pkt_dev);
3638 if (!pkt_dev->entry) {
3639 pr_err("cannot create %s/%s procfs entry\n",
3640 PG_PROC_DIR, ifname);
3641 err = -EINVAL;
3642 goto out2;
3644 #ifdef CONFIG_XFRM
3645 pkt_dev->ipsmode = XFRM_MODE_TRANSPORT;
3646 pkt_dev->ipsproto = IPPROTO_ESP;
3648 /* xfrm tunnel mode needs additional dst to extract outter
3649 * ip header protocol/ttl/id field, here creat a phony one.
3650 * instead of looking for a valid rt, which definitely hurting
3651 * performance under such circumstance.
3653 pkt_dev->dstops.family = AF_INET;
3654 pkt_dev->xdst.u.dst.dev = pkt_dev->odev;
3655 dst_init_metrics(&pkt_dev->xdst.u.dst, pktgen_dst_metrics, false);
3656 pkt_dev->xdst.child = &pkt_dev->xdst.u.dst;
3657 pkt_dev->xdst.u.dst.ops = &pkt_dev->dstops;
3658 #endif
3660 return add_dev_to_thread(t, pkt_dev);
3661 out2:
3662 dev_put(pkt_dev->odev);
3663 out1:
3664 #ifdef CONFIG_XFRM
3665 free_SAs(pkt_dev);
3666 #endif
3667 vfree(pkt_dev->flows);
3668 kfree(pkt_dev);
3669 return err;
3672 static int __net_init pktgen_create_thread(int cpu, struct pktgen_net *pn)
3674 struct pktgen_thread *t;
3675 struct proc_dir_entry *pe;
3676 struct task_struct *p;
3678 t = kzalloc_node(sizeof(struct pktgen_thread), GFP_KERNEL,
3679 cpu_to_node(cpu));
3680 if (!t) {
3681 pr_err("ERROR: out of memory, can't create new thread\n");
3682 return -ENOMEM;
3685 mutex_init(&t->if_lock);
3686 t->cpu = cpu;
3688 INIT_LIST_HEAD(&t->if_list);
3690 list_add_tail(&t->th_list, &pn->pktgen_threads);
3691 init_completion(&t->start_done);
3693 p = kthread_create_on_node(pktgen_thread_worker,
3695 cpu_to_node(cpu),
3696 "kpktgend_%d", cpu);
3697 if (IS_ERR(p)) {
3698 pr_err("kernel_thread() failed for cpu %d\n", t->cpu);
3699 list_del(&t->th_list);
3700 kfree(t);
3701 return PTR_ERR(p);
3703 kthread_bind(p, cpu);
3704 t->tsk = p;
3706 pe = proc_create_data(t->tsk->comm, 0600, pn->proc_dir,
3707 &pktgen_thread_fops, t);
3708 if (!pe) {
3709 pr_err("cannot create %s/%s procfs entry\n",
3710 PG_PROC_DIR, t->tsk->comm);
3711 kthread_stop(p);
3712 list_del(&t->th_list);
3713 kfree(t);
3714 return -EINVAL;
3717 t->net = pn;
3718 get_task_struct(p);
3719 wake_up_process(p);
3720 wait_for_completion(&t->start_done);
3722 return 0;
3726 * Removes a device from the thread if_list.
3728 static void _rem_dev_from_if_list(struct pktgen_thread *t,
3729 struct pktgen_dev *pkt_dev)
3731 struct list_head *q, *n;
3732 struct pktgen_dev *p;
3734 if_lock(t);
3735 list_for_each_safe(q, n, &t->if_list) {
3736 p = list_entry(q, struct pktgen_dev, list);
3737 if (p == pkt_dev)
3738 list_del_rcu(&p->list);
3740 if_unlock(t);
3743 static int pktgen_remove_device(struct pktgen_thread *t,
3744 struct pktgen_dev *pkt_dev)
3746 pr_debug("remove_device pkt_dev=%p\n", pkt_dev);
3748 if (pkt_dev->running) {
3749 pr_warn("WARNING: trying to remove a running interface, stopping it now\n");
3750 pktgen_stop_device(pkt_dev);
3753 /* Dis-associate from the interface */
3755 if (pkt_dev->odev) {
3756 dev_put(pkt_dev->odev);
3757 pkt_dev->odev = NULL;
3760 /* Remove proc before if_list entry, because add_device uses
3761 * list to determine if interface already exist, avoid race
3762 * with proc_create_data() */
3763 proc_remove(pkt_dev->entry);
3765 /* And update the thread if_list */
3766 _rem_dev_from_if_list(t, pkt_dev);
3768 #ifdef CONFIG_XFRM
3769 free_SAs(pkt_dev);
3770 #endif
3771 vfree(pkt_dev->flows);
3772 if (pkt_dev->page)
3773 put_page(pkt_dev->page);
3774 kfree_rcu(pkt_dev, rcu);
3775 return 0;
3778 static int __net_init pg_net_init(struct net *net)
3780 struct pktgen_net *pn = net_generic(net, pg_net_id);
3781 struct proc_dir_entry *pe;
3782 int cpu, ret = 0;
3784 pn->net = net;
3785 INIT_LIST_HEAD(&pn->pktgen_threads);
3786 pn->pktgen_exiting = false;
3787 pn->proc_dir = proc_mkdir(PG_PROC_DIR, pn->net->proc_net);
3788 if (!pn->proc_dir) {
3789 pr_warn("cannot create /proc/net/%s\n", PG_PROC_DIR);
3790 return -ENODEV;
3792 pe = proc_create(PGCTRL, 0600, pn->proc_dir, &pktgen_fops);
3793 if (pe == NULL) {
3794 pr_err("cannot create %s procfs entry\n", PGCTRL);
3795 ret = -EINVAL;
3796 goto remove;
3799 for_each_online_cpu(cpu) {
3800 int err;
3802 err = pktgen_create_thread(cpu, pn);
3803 if (err)
3804 pr_warn("Cannot create thread for cpu %d (%d)\n",
3805 cpu, err);
3808 if (list_empty(&pn->pktgen_threads)) {
3809 pr_err("Initialization failed for all threads\n");
3810 ret = -ENODEV;
3811 goto remove_entry;
3814 return 0;
3816 remove_entry:
3817 remove_proc_entry(PGCTRL, pn->proc_dir);
3818 remove:
3819 remove_proc_entry(PG_PROC_DIR, pn->net->proc_net);
3820 return ret;
3823 static void __net_exit pg_net_exit(struct net *net)
3825 struct pktgen_net *pn = net_generic(net, pg_net_id);
3826 struct pktgen_thread *t;
3827 struct list_head *q, *n;
3828 LIST_HEAD(list);
3830 /* Stop all interfaces & threads */
3831 pn->pktgen_exiting = true;
3833 mutex_lock(&pktgen_thread_lock);
3834 list_splice_init(&pn->pktgen_threads, &list);
3835 mutex_unlock(&pktgen_thread_lock);
3837 list_for_each_safe(q, n, &list) {
3838 t = list_entry(q, struct pktgen_thread, th_list);
3839 list_del(&t->th_list);
3840 kthread_stop(t->tsk);
3841 put_task_struct(t->tsk);
3842 kfree(t);
3845 remove_proc_entry(PGCTRL, pn->proc_dir);
3846 remove_proc_entry(PG_PROC_DIR, pn->net->proc_net);
3849 static struct pernet_operations pg_net_ops = {
3850 .init = pg_net_init,
3851 .exit = pg_net_exit,
3852 .id = &pg_net_id,
3853 .size = sizeof(struct pktgen_net),
3856 static int __init pg_init(void)
3858 int ret = 0;
3860 pr_info("%s", version);
3861 ret = register_pernet_subsys(&pg_net_ops);
3862 if (ret)
3863 return ret;
3864 ret = register_netdevice_notifier(&pktgen_notifier_block);
3865 if (ret)
3866 unregister_pernet_subsys(&pg_net_ops);
3868 return ret;
3871 static void __exit pg_cleanup(void)
3873 unregister_netdevice_notifier(&pktgen_notifier_block);
3874 unregister_pernet_subsys(&pg_net_ops);
3875 /* Don't need rcu_barrier() due to use of kfree_rcu() */
3878 module_init(pg_init);
3879 module_exit(pg_cleanup);
3881 MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se>");
3882 MODULE_DESCRIPTION("Packet Generator tool");
3883 MODULE_LICENSE("GPL");
3884 MODULE_VERSION(VERSION);
3885 module_param(pg_count_d, int, 0);
3886 MODULE_PARM_DESC(pg_count_d, "Default number of packets to inject");
3887 module_param(pg_delay_d, int, 0);
3888 MODULE_PARM_DESC(pg_delay_d, "Default delay between packets (nanoseconds)");
3889 module_param(pg_clone_skb_d, int, 0);
3890 MODULE_PARM_DESC(pg_clone_skb_d, "Default number of copies of the same packet");
3891 module_param(debug, int, 0);
3892 MODULE_PARM_DESC(debug, "Enable debugging of pktgen module");