Merge tag 'for-linus-20190706' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / net / core / pktgen.c
blobf975c5e2a3697b555344dbb9f1e009c7eb1c7711
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Authors:
4 * Copyright 2001, 2002 by Robert Olsson <robert.olsson@its.uu.se>
5 * Uppsala University and
6 * Swedish University of Agricultural Sciences
8 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
9 * Ben Greear <greearb@candelatech.com>
10 * Jens Låås <jens.laas@data.slu.se>
12 * A tool for loading the network with preconfigurated packets.
13 * The tool is implemented as a linux module. Parameters are output
14 * device, delay (to hard_xmit), number of packets, and whether
15 * to use multiple SKBs or just the same one.
16 * pktgen uses the installed interface's output routine.
18 * Additional hacking by:
20 * Jens.Laas@data.slu.se
21 * Improved by ANK. 010120.
22 * Improved by ANK even more. 010212.
23 * MAC address typo fixed. 010417 --ro
24 * Integrated. 020301 --DaveM
25 * Added multiskb option 020301 --DaveM
26 * Scaling of results. 020417--sigurdur@linpro.no
27 * Significant re-work of the module:
28 * * Convert to threaded model to more efficiently be able to transmit
29 * and receive on multiple interfaces at once.
30 * * Converted many counters to __u64 to allow longer runs.
31 * * Allow configuration of ranges, like min/max IP address, MACs,
32 * and UDP-ports, for both source and destination, and can
33 * set to use a random distribution or sequentially walk the range.
34 * * Can now change most values after starting.
35 * * Place 12-byte packet in UDP payload with magic number,
36 * sequence number, and timestamp.
37 * * Add receiver code that detects dropped pkts, re-ordered pkts, and
38 * latencies (with micro-second) precision.
39 * * Add IOCTL interface to easily get counters & configuration.
40 * --Ben Greear <greearb@candelatech.com>
42 * Renamed multiskb to clone_skb and cleaned up sending core for two distinct
43 * skb modes. A clone_skb=0 mode for Ben "ranges" work and a clone_skb != 0
44 * as a "fastpath" with a configurable number of clones after alloc's.
45 * clone_skb=0 means all packets are allocated this also means ranges time
46 * stamps etc can be used. clone_skb=100 means 1 malloc is followed by 100
47 * clones.
49 * Also moved to /proc/net/pktgen/
50 * --ro
52 * Sept 10: Fixed threading/locking. Lots of bone-headed and more clever
53 * mistakes. Also merged in DaveM's patch in the -pre6 patch.
54 * --Ben Greear <greearb@candelatech.com>
56 * Integrated to 2.5.x 021029 --Lucio Maciel (luciomaciel@zipmail.com.br)
58 * 021124 Finished major redesign and rewrite for new functionality.
59 * See Documentation/networking/pktgen.txt for how to use this.
61 * The new operation:
62 * For each CPU one thread/process is created at start. This process checks
63 * for running devices in the if_list and sends packets until count is 0 it
64 * also the thread checks the thread->control which is used for inter-process
65 * communication. controlling process "posts" operations to the threads this
66 * way.
67 * The if_list is RCU protected, and the if_lock remains to protect updating
68 * of if_list, from "add_device" as it invoked from userspace (via proc write).
70 * By design there should only be *one* "controlling" process. In practice
71 * multiple write accesses gives unpredictable result. Understood by "write"
72 * to /proc gives result code thats should be read be the "writer".
73 * For practical use this should be no problem.
75 * Note when adding devices to a specific CPU there good idea to also assign
76 * /proc/irq/XX/smp_affinity so TX-interrupts gets bound to the same CPU.
77 * --ro
79 * Fix refcount off by one if first packet fails, potential null deref,
80 * memleak 030710- KJP
82 * First "ranges" functionality for ipv6 030726 --ro
84 * Included flow support. 030802 ANK.
86 * Fixed unaligned access on IA-64 Grant Grundler <grundler@parisc-linux.org>
88 * Remove if fix from added Harald Welte <laforge@netfilter.org> 040419
89 * ia64 compilation fix from Aron Griffis <aron@hp.com> 040604
91 * New xmit() return, do_div and misc clean up by Stephen Hemminger
92 * <shemminger@osdl.org> 040923
94 * Randy Dunlap fixed u64 printk compiler warning
96 * Remove FCS from BW calculation. Lennert Buytenhek <buytenh@wantstofly.org>
97 * New time handling. Lennert Buytenhek <buytenh@wantstofly.org> 041213
99 * Corrections from Nikolai Malykh (nmalykh@bilim.com)
100 * Removed unused flags F_SET_SRCMAC & F_SET_SRCIP 041230
102 * interruptible_sleep_on_timeout() replaced Nishanth Aravamudan <nacc@us.ibm.com>
103 * 050103
105 * MPLS support by Steven Whitehouse <steve@chygwyn.com>
107 * 802.1Q/Q-in-Q support by Francesco Fondelli (FF) <francesco.fondelli@gmail.com>
109 * Fixed src_mac command to set source mac of packet to value specified in
110 * command by Adit Ranadive <adit.262@gmail.com>
113 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
115 #include <linux/sys.h>
116 #include <linux/types.h>
117 #include <linux/module.h>
118 #include <linux/moduleparam.h>
119 #include <linux/kernel.h>
120 #include <linux/mutex.h>
121 #include <linux/sched.h>
122 #include <linux/slab.h>
123 #include <linux/vmalloc.h>
124 #include <linux/unistd.h>
125 #include <linux/string.h>
126 #include <linux/ptrace.h>
127 #include <linux/errno.h>
128 #include <linux/ioport.h>
129 #include <linux/interrupt.h>
130 #include <linux/capability.h>
131 #include <linux/hrtimer.h>
132 #include <linux/freezer.h>
133 #include <linux/delay.h>
134 #include <linux/timer.h>
135 #include <linux/list.h>
136 #include <linux/init.h>
137 #include <linux/skbuff.h>
138 #include <linux/netdevice.h>
139 #include <linux/inet.h>
140 #include <linux/inetdevice.h>
141 #include <linux/rtnetlink.h>
142 #include <linux/if_arp.h>
143 #include <linux/if_vlan.h>
144 #include <linux/in.h>
145 #include <linux/ip.h>
146 #include <linux/ipv6.h>
147 #include <linux/udp.h>
148 #include <linux/proc_fs.h>
149 #include <linux/seq_file.h>
150 #include <linux/wait.h>
151 #include <linux/etherdevice.h>
152 #include <linux/kthread.h>
153 #include <linux/prefetch.h>
154 #include <linux/mmzone.h>
155 #include <net/net_namespace.h>
156 #include <net/checksum.h>
157 #include <net/ipv6.h>
158 #include <net/udp.h>
159 #include <net/ip6_checksum.h>
160 #include <net/addrconf.h>
161 #ifdef CONFIG_XFRM
162 #include <net/xfrm.h>
163 #endif
164 #include <net/netns/generic.h>
165 #include <asm/byteorder.h>
166 #include <linux/rcupdate.h>
167 #include <linux/bitops.h>
168 #include <linux/io.h>
169 #include <linux/timex.h>
170 #include <linux/uaccess.h>
171 #include <asm/dma.h>
172 #include <asm/div64.h> /* do_div */
174 #define VERSION "2.75"
175 #define IP_NAME_SZ 32
176 #define MAX_MPLS_LABELS 16 /* This is the max label stack depth */
177 #define MPLS_STACK_BOTTOM htonl(0x00000100)
179 #define func_enter() pr_debug("entering %s\n", __func__);
181 #define PKT_FLAGS \
182 pf(IPV6) /* Interface in IPV6 Mode */ \
183 pf(IPSRC_RND) /* IP-Src Random */ \
184 pf(IPDST_RND) /* IP-Dst Random */ \
185 pf(TXSIZE_RND) /* Transmit size is random */ \
186 pf(UDPSRC_RND) /* UDP-Src Random */ \
187 pf(UDPDST_RND) /* UDP-Dst Random */ \
188 pf(UDPCSUM) /* Include UDP checksum */ \
189 pf(NO_TIMESTAMP) /* Don't timestamp packets (default TS) */ \
190 pf(MPLS_RND) /* Random MPLS labels */ \
191 pf(QUEUE_MAP_RND) /* queue map Random */ \
192 pf(QUEUE_MAP_CPU) /* queue map mirrors smp_processor_id() */ \
193 pf(FLOW_SEQ) /* Sequential flows */ \
194 pf(IPSEC) /* ipsec on for flows */ \
195 pf(MACSRC_RND) /* MAC-Src Random */ \
196 pf(MACDST_RND) /* MAC-Dst Random */ \
197 pf(VID_RND) /* Random VLAN ID */ \
198 pf(SVID_RND) /* Random SVLAN ID */ \
199 pf(NODE) /* Node memory alloc*/ \
201 #define pf(flag) flag##_SHIFT,
202 enum pkt_flags {
203 PKT_FLAGS
205 #undef pf
207 /* Device flag bits */
208 #define pf(flag) static const __u32 F_##flag = (1<<flag##_SHIFT);
209 PKT_FLAGS
210 #undef pf
212 #define pf(flag) __stringify(flag),
213 static char *pkt_flag_names[] = {
214 PKT_FLAGS
216 #undef pf
218 #define NR_PKT_FLAGS ARRAY_SIZE(pkt_flag_names)
220 /* Thread control flag bits */
221 #define T_STOP (1<<0) /* Stop run */
222 #define T_RUN (1<<1) /* Start run */
223 #define T_REMDEVALL (1<<2) /* Remove all devs */
224 #define T_REMDEV (1<<3) /* Remove one dev */
226 /* Xmit modes */
227 #define M_START_XMIT 0 /* Default normal TX */
228 #define M_NETIF_RECEIVE 1 /* Inject packets into stack */
229 #define M_QUEUE_XMIT 2 /* Inject packet into qdisc */
231 /* If lock -- protects updating of if_list */
232 #define if_lock(t) mutex_lock(&(t->if_lock));
233 #define if_unlock(t) mutex_unlock(&(t->if_lock));
235 /* Used to help with determining the pkts on receive */
236 #define PKTGEN_MAGIC 0xbe9be955
237 #define PG_PROC_DIR "pktgen"
238 #define PGCTRL "pgctrl"
240 #define MAX_CFLOWS 65536
242 #define VLAN_TAG_SIZE(x) ((x)->vlan_id == 0xffff ? 0 : 4)
243 #define SVLAN_TAG_SIZE(x) ((x)->svlan_id == 0xffff ? 0 : 4)
245 struct flow_state {
246 __be32 cur_daddr;
247 int count;
248 #ifdef CONFIG_XFRM
249 struct xfrm_state *x;
250 #endif
251 __u32 flags;
254 /* flow flag bits */
255 #define F_INIT (1<<0) /* flow has been initialized */
257 struct pktgen_dev {
259 * Try to keep frequent/infrequent used vars. separated.
261 struct proc_dir_entry *entry; /* proc file */
262 struct pktgen_thread *pg_thread;/* the owner */
263 struct list_head list; /* chaining in the thread's run-queue */
264 struct rcu_head rcu; /* freed by RCU */
266 int running; /* if false, the test will stop */
268 /* If min != max, then we will either do a linear iteration, or
269 * we will do a random selection from within the range.
271 __u32 flags;
272 int xmit_mode;
273 int min_pkt_size;
274 int max_pkt_size;
275 int pkt_overhead; /* overhead for MPLS, VLANs, IPSEC etc */
276 int nfrags;
277 int removal_mark; /* non-zero => the device is marked for
278 * removal by worker thread */
280 struct page *page;
281 u64 delay; /* nano-seconds */
283 __u64 count; /* Default No packets to send */
284 __u64 sofar; /* How many pkts we've sent so far */
285 __u64 tx_bytes; /* How many bytes we've transmitted */
286 __u64 errors; /* Errors when trying to transmit, */
288 /* runtime counters relating to clone_skb */
290 __u32 clone_count;
291 int last_ok; /* Was last skb sent?
292 * Or a failed transmit of some sort?
293 * This will keep sequence numbers in order
295 ktime_t next_tx;
296 ktime_t started_at;
297 ktime_t stopped_at;
298 u64 idle_acc; /* nano-seconds */
300 __u32 seq_num;
302 int clone_skb; /*
303 * Use multiple SKBs during packet gen.
304 * If this number is greater than 1, then
305 * that many copies of the same packet will be
306 * sent before a new packet is allocated.
307 * If you want to send 1024 identical packets
308 * before creating a new packet,
309 * set clone_skb to 1024.
312 char dst_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
313 char dst_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
314 char src_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
315 char src_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
317 struct in6_addr in6_saddr;
318 struct in6_addr in6_daddr;
319 struct in6_addr cur_in6_daddr;
320 struct in6_addr cur_in6_saddr;
321 /* For ranges */
322 struct in6_addr min_in6_daddr;
323 struct in6_addr max_in6_daddr;
324 struct in6_addr min_in6_saddr;
325 struct in6_addr max_in6_saddr;
327 /* If we're doing ranges, random or incremental, then this
328 * defines the min/max for those ranges.
330 __be32 saddr_min; /* inclusive, source IP address */
331 __be32 saddr_max; /* exclusive, source IP address */
332 __be32 daddr_min; /* inclusive, dest IP address */
333 __be32 daddr_max; /* exclusive, dest IP address */
335 __u16 udp_src_min; /* inclusive, source UDP port */
336 __u16 udp_src_max; /* exclusive, source UDP port */
337 __u16 udp_dst_min; /* inclusive, dest UDP port */
338 __u16 udp_dst_max; /* exclusive, dest UDP port */
340 /* DSCP + ECN */
341 __u8 tos; /* six MSB of (former) IPv4 TOS
342 are for dscp codepoint */
343 __u8 traffic_class; /* ditto for the (former) Traffic Class in IPv6
344 (see RFC 3260, sec. 4) */
346 /* MPLS */
347 unsigned int nr_labels; /* Depth of stack, 0 = no MPLS */
348 __be32 labels[MAX_MPLS_LABELS];
350 /* VLAN/SVLAN (802.1Q/Q-in-Q) */
351 __u8 vlan_p;
352 __u8 vlan_cfi;
353 __u16 vlan_id; /* 0xffff means no vlan tag */
355 __u8 svlan_p;
356 __u8 svlan_cfi;
357 __u16 svlan_id; /* 0xffff means no svlan tag */
359 __u32 src_mac_count; /* How many MACs to iterate through */
360 __u32 dst_mac_count; /* How many MACs to iterate through */
362 unsigned char dst_mac[ETH_ALEN];
363 unsigned char src_mac[ETH_ALEN];
365 __u32 cur_dst_mac_offset;
366 __u32 cur_src_mac_offset;
367 __be32 cur_saddr;
368 __be32 cur_daddr;
369 __u16 ip_id;
370 __u16 cur_udp_dst;
371 __u16 cur_udp_src;
372 __u16 cur_queue_map;
373 __u32 cur_pkt_size;
374 __u32 last_pkt_size;
376 __u8 hh[14];
377 /* = {
378 0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
380 We fill in SRC address later
381 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
382 0x08, 0x00
385 __u16 pad; /* pad out the hh struct to an even 16 bytes */
387 struct sk_buff *skb; /* skb we are to transmit next, used for when we
388 * are transmitting the same one multiple times
390 struct net_device *odev; /* The out-going device.
391 * Note that the device should have it's
392 * pg_info pointer pointing back to this
393 * device.
394 * Set when the user specifies the out-going
395 * device name (not when the inject is
396 * started as it used to do.)
398 char odevname[32];
399 struct flow_state *flows;
400 unsigned int cflows; /* Concurrent flows (config) */
401 unsigned int lflow; /* Flow length (config) */
402 unsigned int nflows; /* accumulated flows (stats) */
403 unsigned int curfl; /* current sequenced flow (state)*/
405 u16 queue_map_min;
406 u16 queue_map_max;
407 __u32 skb_priority; /* skb priority field */
408 unsigned int burst; /* number of duplicated packets to burst */
409 int node; /* Memory node */
411 #ifdef CONFIG_XFRM
412 __u8 ipsmode; /* IPSEC mode (config) */
413 __u8 ipsproto; /* IPSEC type (config) */
414 __u32 spi;
415 struct xfrm_dst xdst;
416 struct dst_ops dstops;
417 #endif
418 char result[512];
421 struct pktgen_hdr {
422 __be32 pgh_magic;
423 __be32 seq_num;
424 __be32 tv_sec;
425 __be32 tv_usec;
429 static unsigned int pg_net_id __read_mostly;
431 struct pktgen_net {
432 struct net *net;
433 struct proc_dir_entry *proc_dir;
434 struct list_head pktgen_threads;
435 bool pktgen_exiting;
438 struct pktgen_thread {
439 struct mutex if_lock; /* for list of devices */
440 struct list_head if_list; /* All device here */
441 struct list_head th_list;
442 struct task_struct *tsk;
443 char result[512];
445 /* Field for thread to receive "posted" events terminate,
446 stop ifs etc. */
448 u32 control;
449 int cpu;
451 wait_queue_head_t queue;
452 struct completion start_done;
453 struct pktgen_net *net;
456 #define REMOVE 1
457 #define FIND 0
459 static const char version[] =
460 "Packet Generator for packet performance testing. "
461 "Version: " VERSION "\n";
463 static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *i);
464 static int pktgen_add_device(struct pktgen_thread *t, const char *ifname);
465 static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
466 const char *ifname, bool exact);
467 static int pktgen_device_event(struct notifier_block *, unsigned long, void *);
468 static void pktgen_run_all_threads(struct pktgen_net *pn);
469 static void pktgen_reset_all_threads(struct pktgen_net *pn);
470 static void pktgen_stop_all_threads_ifs(struct pktgen_net *pn);
472 static void pktgen_stop(struct pktgen_thread *t);
473 static void pktgen_clear_counters(struct pktgen_dev *pkt_dev);
475 /* Module parameters, defaults. */
476 static int pg_count_d __read_mostly = 1000;
477 static int pg_delay_d __read_mostly;
478 static int pg_clone_skb_d __read_mostly;
479 static int debug __read_mostly;
481 static DEFINE_MUTEX(pktgen_thread_lock);
483 static struct notifier_block pktgen_notifier_block = {
484 .notifier_call = pktgen_device_event,
488 * /proc handling functions
492 static int pgctrl_show(struct seq_file *seq, void *v)
494 seq_puts(seq, version);
495 return 0;
498 static ssize_t pgctrl_write(struct file *file, const char __user *buf,
499 size_t count, loff_t *ppos)
501 char data[128];
502 struct pktgen_net *pn = net_generic(current->nsproxy->net_ns, pg_net_id);
504 if (!capable(CAP_NET_ADMIN))
505 return -EPERM;
507 if (count == 0)
508 return -EINVAL;
510 if (count > sizeof(data))
511 count = sizeof(data);
513 if (copy_from_user(data, buf, count))
514 return -EFAULT;
516 data[count - 1] = 0; /* Strip trailing '\n' and terminate string */
518 if (!strcmp(data, "stop"))
519 pktgen_stop_all_threads_ifs(pn);
521 else if (!strcmp(data, "start"))
522 pktgen_run_all_threads(pn);
524 else if (!strcmp(data, "reset"))
525 pktgen_reset_all_threads(pn);
527 else
528 return -EINVAL;
530 return count;
533 static int pgctrl_open(struct inode *inode, struct file *file)
535 return single_open(file, pgctrl_show, PDE_DATA(inode));
538 static const struct file_operations pktgen_fops = {
539 .open = pgctrl_open,
540 .read = seq_read,
541 .llseek = seq_lseek,
542 .write = pgctrl_write,
543 .release = single_release,
546 static int pktgen_if_show(struct seq_file *seq, void *v)
548 const struct pktgen_dev *pkt_dev = seq->private;
549 ktime_t stopped;
550 unsigned int i;
551 u64 idle;
553 seq_printf(seq,
554 "Params: count %llu min_pkt_size: %u max_pkt_size: %u\n",
555 (unsigned long long)pkt_dev->count, pkt_dev->min_pkt_size,
556 pkt_dev->max_pkt_size);
558 seq_printf(seq,
559 " frags: %d delay: %llu clone_skb: %d ifname: %s\n",
560 pkt_dev->nfrags, (unsigned long long) pkt_dev->delay,
561 pkt_dev->clone_skb, pkt_dev->odevname);
563 seq_printf(seq, " flows: %u flowlen: %u\n", pkt_dev->cflows,
564 pkt_dev->lflow);
566 seq_printf(seq,
567 " queue_map_min: %u queue_map_max: %u\n",
568 pkt_dev->queue_map_min,
569 pkt_dev->queue_map_max);
571 if (pkt_dev->skb_priority)
572 seq_printf(seq, " skb_priority: %u\n",
573 pkt_dev->skb_priority);
575 if (pkt_dev->flags & F_IPV6) {
576 seq_printf(seq,
577 " saddr: %pI6c min_saddr: %pI6c max_saddr: %pI6c\n"
578 " daddr: %pI6c min_daddr: %pI6c max_daddr: %pI6c\n",
579 &pkt_dev->in6_saddr,
580 &pkt_dev->min_in6_saddr, &pkt_dev->max_in6_saddr,
581 &pkt_dev->in6_daddr,
582 &pkt_dev->min_in6_daddr, &pkt_dev->max_in6_daddr);
583 } else {
584 seq_printf(seq,
585 " dst_min: %s dst_max: %s\n",
586 pkt_dev->dst_min, pkt_dev->dst_max);
587 seq_printf(seq,
588 " src_min: %s src_max: %s\n",
589 pkt_dev->src_min, pkt_dev->src_max);
592 seq_puts(seq, " src_mac: ");
594 seq_printf(seq, "%pM ",
595 is_zero_ether_addr(pkt_dev->src_mac) ?
596 pkt_dev->odev->dev_addr : pkt_dev->src_mac);
598 seq_puts(seq, "dst_mac: ");
599 seq_printf(seq, "%pM\n", pkt_dev->dst_mac);
601 seq_printf(seq,
602 " udp_src_min: %d udp_src_max: %d"
603 " udp_dst_min: %d udp_dst_max: %d\n",
604 pkt_dev->udp_src_min, pkt_dev->udp_src_max,
605 pkt_dev->udp_dst_min, pkt_dev->udp_dst_max);
607 seq_printf(seq,
608 " src_mac_count: %d dst_mac_count: %d\n",
609 pkt_dev->src_mac_count, pkt_dev->dst_mac_count);
611 if (pkt_dev->nr_labels) {
612 seq_puts(seq, " mpls: ");
613 for (i = 0; i < pkt_dev->nr_labels; i++)
614 seq_printf(seq, "%08x%s", ntohl(pkt_dev->labels[i]),
615 i == pkt_dev->nr_labels-1 ? "\n" : ", ");
618 if (pkt_dev->vlan_id != 0xffff)
619 seq_printf(seq, " vlan_id: %u vlan_p: %u vlan_cfi: %u\n",
620 pkt_dev->vlan_id, pkt_dev->vlan_p,
621 pkt_dev->vlan_cfi);
623 if (pkt_dev->svlan_id != 0xffff)
624 seq_printf(seq, " svlan_id: %u vlan_p: %u vlan_cfi: %u\n",
625 pkt_dev->svlan_id, pkt_dev->svlan_p,
626 pkt_dev->svlan_cfi);
628 if (pkt_dev->tos)
629 seq_printf(seq, " tos: 0x%02x\n", pkt_dev->tos);
631 if (pkt_dev->traffic_class)
632 seq_printf(seq, " traffic_class: 0x%02x\n", pkt_dev->traffic_class);
634 if (pkt_dev->burst > 1)
635 seq_printf(seq, " burst: %d\n", pkt_dev->burst);
637 if (pkt_dev->node >= 0)
638 seq_printf(seq, " node: %d\n", pkt_dev->node);
640 if (pkt_dev->xmit_mode == M_NETIF_RECEIVE)
641 seq_puts(seq, " xmit_mode: netif_receive\n");
642 else if (pkt_dev->xmit_mode == M_QUEUE_XMIT)
643 seq_puts(seq, " xmit_mode: xmit_queue\n");
645 seq_puts(seq, " Flags: ");
647 for (i = 0; i < NR_PKT_FLAGS; i++) {
648 if (i == F_FLOW_SEQ)
649 if (!pkt_dev->cflows)
650 continue;
652 if (pkt_dev->flags & (1 << i))
653 seq_printf(seq, "%s ", pkt_flag_names[i]);
654 else if (i == F_FLOW_SEQ)
655 seq_puts(seq, "FLOW_RND ");
657 #ifdef CONFIG_XFRM
658 if (i == F_IPSEC && pkt_dev->spi)
659 seq_printf(seq, "spi:%u", pkt_dev->spi);
660 #endif
663 seq_puts(seq, "\n");
665 /* not really stopped, more like last-running-at */
666 stopped = pkt_dev->running ? ktime_get() : pkt_dev->stopped_at;
667 idle = pkt_dev->idle_acc;
668 do_div(idle, NSEC_PER_USEC);
670 seq_printf(seq,
671 "Current:\n pkts-sofar: %llu errors: %llu\n",
672 (unsigned long long)pkt_dev->sofar,
673 (unsigned long long)pkt_dev->errors);
675 seq_printf(seq,
676 " started: %lluus stopped: %lluus idle: %lluus\n",
677 (unsigned long long) ktime_to_us(pkt_dev->started_at),
678 (unsigned long long) ktime_to_us(stopped),
679 (unsigned long long) idle);
681 seq_printf(seq,
682 " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n",
683 pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset,
684 pkt_dev->cur_src_mac_offset);
686 if (pkt_dev->flags & F_IPV6) {
687 seq_printf(seq, " cur_saddr: %pI6c cur_daddr: %pI6c\n",
688 &pkt_dev->cur_in6_saddr,
689 &pkt_dev->cur_in6_daddr);
690 } else
691 seq_printf(seq, " cur_saddr: %pI4 cur_daddr: %pI4\n",
692 &pkt_dev->cur_saddr, &pkt_dev->cur_daddr);
694 seq_printf(seq, " cur_udp_dst: %d cur_udp_src: %d\n",
695 pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src);
697 seq_printf(seq, " cur_queue_map: %u\n", pkt_dev->cur_queue_map);
699 seq_printf(seq, " flows: %u\n", pkt_dev->nflows);
701 if (pkt_dev->result[0])
702 seq_printf(seq, "Result: %s\n", pkt_dev->result);
703 else
704 seq_puts(seq, "Result: Idle\n");
706 return 0;
710 static int hex32_arg(const char __user *user_buffer, unsigned long maxlen,
711 __u32 *num)
713 int i = 0;
714 *num = 0;
716 for (; i < maxlen; i++) {
717 int value;
718 char c;
719 *num <<= 4;
720 if (get_user(c, &user_buffer[i]))
721 return -EFAULT;
722 value = hex_to_bin(c);
723 if (value >= 0)
724 *num |= value;
725 else
726 break;
728 return i;
731 static int count_trail_chars(const char __user * user_buffer,
732 unsigned int maxlen)
734 int i;
736 for (i = 0; i < maxlen; i++) {
737 char c;
738 if (get_user(c, &user_buffer[i]))
739 return -EFAULT;
740 switch (c) {
741 case '\"':
742 case '\n':
743 case '\r':
744 case '\t':
745 case ' ':
746 case '=':
747 break;
748 default:
749 goto done;
752 done:
753 return i;
756 static long num_arg(const char __user *user_buffer, unsigned long maxlen,
757 unsigned long *num)
759 int i;
760 *num = 0;
762 for (i = 0; i < maxlen; i++) {
763 char c;
764 if (get_user(c, &user_buffer[i]))
765 return -EFAULT;
766 if ((c >= '0') && (c <= '9')) {
767 *num *= 10;
768 *num += c - '0';
769 } else
770 break;
772 return i;
775 static int strn_len(const char __user * user_buffer, unsigned int maxlen)
777 int i;
779 for (i = 0; i < maxlen; i++) {
780 char c;
781 if (get_user(c, &user_buffer[i]))
782 return -EFAULT;
783 switch (c) {
784 case '\"':
785 case '\n':
786 case '\r':
787 case '\t':
788 case ' ':
789 goto done_str;
790 default:
791 break;
794 done_str:
795 return i;
798 static ssize_t get_labels(const char __user *buffer, struct pktgen_dev *pkt_dev)
800 unsigned int n = 0;
801 char c;
802 ssize_t i = 0;
803 int len;
805 pkt_dev->nr_labels = 0;
806 do {
807 __u32 tmp;
808 len = hex32_arg(&buffer[i], 8, &tmp);
809 if (len <= 0)
810 return len;
811 pkt_dev->labels[n] = htonl(tmp);
812 if (pkt_dev->labels[n] & MPLS_STACK_BOTTOM)
813 pkt_dev->flags |= F_MPLS_RND;
814 i += len;
815 if (get_user(c, &buffer[i]))
816 return -EFAULT;
817 i++;
818 n++;
819 if (n >= MAX_MPLS_LABELS)
820 return -E2BIG;
821 } while (c == ',');
823 pkt_dev->nr_labels = n;
824 return i;
827 static __u32 pktgen_read_flag(const char *f, bool *disable)
829 __u32 i;
831 if (f[0] == '!') {
832 *disable = true;
833 f++;
836 for (i = 0; i < NR_PKT_FLAGS; i++) {
837 if (!IS_ENABLED(CONFIG_XFRM) && i == IPSEC_SHIFT)
838 continue;
840 /* allow only disabling ipv6 flag */
841 if (!*disable && i == IPV6_SHIFT)
842 continue;
844 if (strcmp(f, pkt_flag_names[i]) == 0)
845 return 1 << i;
848 if (strcmp(f, "FLOW_RND") == 0) {
849 *disable = !*disable;
850 return F_FLOW_SEQ;
853 return 0;
856 static ssize_t pktgen_if_write(struct file *file,
857 const char __user * user_buffer, size_t count,
858 loff_t * offset)
860 struct seq_file *seq = file->private_data;
861 struct pktgen_dev *pkt_dev = seq->private;
862 int i, max, len;
863 char name[16], valstr[32];
864 unsigned long value = 0;
865 char *pg_result = NULL;
866 int tmp = 0;
867 char buf[128];
869 pg_result = &(pkt_dev->result[0]);
871 if (count < 1) {
872 pr_warn("wrong command format\n");
873 return -EINVAL;
876 max = count;
877 tmp = count_trail_chars(user_buffer, max);
878 if (tmp < 0) {
879 pr_warn("illegal format\n");
880 return tmp;
882 i = tmp;
884 /* Read variable name */
886 len = strn_len(&user_buffer[i], sizeof(name) - 1);
887 if (len < 0)
888 return len;
890 memset(name, 0, sizeof(name));
891 if (copy_from_user(name, &user_buffer[i], len))
892 return -EFAULT;
893 i += len;
895 max = count - i;
896 len = count_trail_chars(&user_buffer[i], max);
897 if (len < 0)
898 return len;
900 i += len;
902 if (debug) {
903 size_t copy = min_t(size_t, count + 1, 1024);
904 char *tp = strndup_user(user_buffer, copy);
906 if (IS_ERR(tp))
907 return PTR_ERR(tp);
909 pr_debug("%s,%zu buffer -:%s:-\n", name, count, tp);
910 kfree(tp);
913 if (!strcmp(name, "min_pkt_size")) {
914 len = num_arg(&user_buffer[i], 10, &value);
915 if (len < 0)
916 return len;
918 i += len;
919 if (value < 14 + 20 + 8)
920 value = 14 + 20 + 8;
921 if (value != pkt_dev->min_pkt_size) {
922 pkt_dev->min_pkt_size = value;
923 pkt_dev->cur_pkt_size = value;
925 sprintf(pg_result, "OK: min_pkt_size=%u",
926 pkt_dev->min_pkt_size);
927 return count;
930 if (!strcmp(name, "max_pkt_size")) {
931 len = num_arg(&user_buffer[i], 10, &value);
932 if (len < 0)
933 return len;
935 i += len;
936 if (value < 14 + 20 + 8)
937 value = 14 + 20 + 8;
938 if (value != pkt_dev->max_pkt_size) {
939 pkt_dev->max_pkt_size = value;
940 pkt_dev->cur_pkt_size = value;
942 sprintf(pg_result, "OK: max_pkt_size=%u",
943 pkt_dev->max_pkt_size);
944 return count;
947 /* Shortcut for min = max */
949 if (!strcmp(name, "pkt_size")) {
950 len = num_arg(&user_buffer[i], 10, &value);
951 if (len < 0)
952 return len;
954 i += len;
955 if (value < 14 + 20 + 8)
956 value = 14 + 20 + 8;
957 if (value != pkt_dev->min_pkt_size) {
958 pkt_dev->min_pkt_size = value;
959 pkt_dev->max_pkt_size = value;
960 pkt_dev->cur_pkt_size = value;
962 sprintf(pg_result, "OK: pkt_size=%u", pkt_dev->min_pkt_size);
963 return count;
966 if (!strcmp(name, "debug")) {
967 len = num_arg(&user_buffer[i], 10, &value);
968 if (len < 0)
969 return len;
971 i += len;
972 debug = value;
973 sprintf(pg_result, "OK: debug=%u", debug);
974 return count;
977 if (!strcmp(name, "frags")) {
978 len = num_arg(&user_buffer[i], 10, &value);
979 if (len < 0)
980 return len;
982 i += len;
983 pkt_dev->nfrags = value;
984 sprintf(pg_result, "OK: frags=%u", pkt_dev->nfrags);
985 return count;
987 if (!strcmp(name, "delay")) {
988 len = num_arg(&user_buffer[i], 10, &value);
989 if (len < 0)
990 return len;
992 i += len;
993 if (value == 0x7FFFFFFF)
994 pkt_dev->delay = ULLONG_MAX;
995 else
996 pkt_dev->delay = (u64)value;
998 sprintf(pg_result, "OK: delay=%llu",
999 (unsigned long long) pkt_dev->delay);
1000 return count;
1002 if (!strcmp(name, "rate")) {
1003 len = num_arg(&user_buffer[i], 10, &value);
1004 if (len < 0)
1005 return len;
1007 i += len;
1008 if (!value)
1009 return len;
1010 pkt_dev->delay = pkt_dev->min_pkt_size*8*NSEC_PER_USEC/value;
1011 if (debug)
1012 pr_info("Delay set at: %llu ns\n", pkt_dev->delay);
1014 sprintf(pg_result, "OK: rate=%lu", value);
1015 return count;
1017 if (!strcmp(name, "ratep")) {
1018 len = num_arg(&user_buffer[i], 10, &value);
1019 if (len < 0)
1020 return len;
1022 i += len;
1023 if (!value)
1024 return len;
1025 pkt_dev->delay = NSEC_PER_SEC/value;
1026 if (debug)
1027 pr_info("Delay set at: %llu ns\n", pkt_dev->delay);
1029 sprintf(pg_result, "OK: rate=%lu", value);
1030 return count;
1032 if (!strcmp(name, "udp_src_min")) {
1033 len = num_arg(&user_buffer[i], 10, &value);
1034 if (len < 0)
1035 return len;
1037 i += len;
1038 if (value != pkt_dev->udp_src_min) {
1039 pkt_dev->udp_src_min = value;
1040 pkt_dev->cur_udp_src = value;
1042 sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min);
1043 return count;
1045 if (!strcmp(name, "udp_dst_min")) {
1046 len = num_arg(&user_buffer[i], 10, &value);
1047 if (len < 0)
1048 return len;
1050 i += len;
1051 if (value != pkt_dev->udp_dst_min) {
1052 pkt_dev->udp_dst_min = value;
1053 pkt_dev->cur_udp_dst = value;
1055 sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min);
1056 return count;
1058 if (!strcmp(name, "udp_src_max")) {
1059 len = num_arg(&user_buffer[i], 10, &value);
1060 if (len < 0)
1061 return len;
1063 i += len;
1064 if (value != pkt_dev->udp_src_max) {
1065 pkt_dev->udp_src_max = value;
1066 pkt_dev->cur_udp_src = value;
1068 sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max);
1069 return count;
1071 if (!strcmp(name, "udp_dst_max")) {
1072 len = num_arg(&user_buffer[i], 10, &value);
1073 if (len < 0)
1074 return len;
1076 i += len;
1077 if (value != pkt_dev->udp_dst_max) {
1078 pkt_dev->udp_dst_max = value;
1079 pkt_dev->cur_udp_dst = value;
1081 sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max);
1082 return count;
1084 if (!strcmp(name, "clone_skb")) {
1085 len = num_arg(&user_buffer[i], 10, &value);
1086 if (len < 0)
1087 return len;
1088 if ((value > 0) &&
1089 ((pkt_dev->xmit_mode == M_NETIF_RECEIVE) ||
1090 !(pkt_dev->odev->priv_flags & IFF_TX_SKB_SHARING)))
1091 return -ENOTSUPP;
1092 i += len;
1093 pkt_dev->clone_skb = value;
1095 sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb);
1096 return count;
1098 if (!strcmp(name, "count")) {
1099 len = num_arg(&user_buffer[i], 10, &value);
1100 if (len < 0)
1101 return len;
1103 i += len;
1104 pkt_dev->count = value;
1105 sprintf(pg_result, "OK: count=%llu",
1106 (unsigned long long)pkt_dev->count);
1107 return count;
1109 if (!strcmp(name, "src_mac_count")) {
1110 len = num_arg(&user_buffer[i], 10, &value);
1111 if (len < 0)
1112 return len;
1114 i += len;
1115 if (pkt_dev->src_mac_count != value) {
1116 pkt_dev->src_mac_count = value;
1117 pkt_dev->cur_src_mac_offset = 0;
1119 sprintf(pg_result, "OK: src_mac_count=%d",
1120 pkt_dev->src_mac_count);
1121 return count;
1123 if (!strcmp(name, "dst_mac_count")) {
1124 len = num_arg(&user_buffer[i], 10, &value);
1125 if (len < 0)
1126 return len;
1128 i += len;
1129 if (pkt_dev->dst_mac_count != value) {
1130 pkt_dev->dst_mac_count = value;
1131 pkt_dev->cur_dst_mac_offset = 0;
1133 sprintf(pg_result, "OK: dst_mac_count=%d",
1134 pkt_dev->dst_mac_count);
1135 return count;
1137 if (!strcmp(name, "burst")) {
1138 len = num_arg(&user_buffer[i], 10, &value);
1139 if (len < 0)
1140 return len;
1142 i += len;
1143 if ((value > 1) &&
1144 ((pkt_dev->xmit_mode == M_QUEUE_XMIT) ||
1145 ((pkt_dev->xmit_mode == M_START_XMIT) &&
1146 (!(pkt_dev->odev->priv_flags & IFF_TX_SKB_SHARING)))))
1147 return -ENOTSUPP;
1148 pkt_dev->burst = value < 1 ? 1 : value;
1149 sprintf(pg_result, "OK: burst=%d", pkt_dev->burst);
1150 return count;
1152 if (!strcmp(name, "node")) {
1153 len = num_arg(&user_buffer[i], 10, &value);
1154 if (len < 0)
1155 return len;
1157 i += len;
1159 if (node_possible(value)) {
1160 pkt_dev->node = value;
1161 sprintf(pg_result, "OK: node=%d", pkt_dev->node);
1162 if (pkt_dev->page) {
1163 put_page(pkt_dev->page);
1164 pkt_dev->page = NULL;
1167 else
1168 sprintf(pg_result, "ERROR: node not possible");
1169 return count;
1171 if (!strcmp(name, "xmit_mode")) {
1172 char f[32];
1174 memset(f, 0, 32);
1175 len = strn_len(&user_buffer[i], sizeof(f) - 1);
1176 if (len < 0)
1177 return len;
1179 if (copy_from_user(f, &user_buffer[i], len))
1180 return -EFAULT;
1181 i += len;
1183 if (strcmp(f, "start_xmit") == 0) {
1184 pkt_dev->xmit_mode = M_START_XMIT;
1185 } else if (strcmp(f, "netif_receive") == 0) {
1186 /* clone_skb set earlier, not supported in this mode */
1187 if (pkt_dev->clone_skb > 0)
1188 return -ENOTSUPP;
1190 pkt_dev->xmit_mode = M_NETIF_RECEIVE;
1192 /* make sure new packet is allocated every time
1193 * pktgen_xmit() is called
1195 pkt_dev->last_ok = 1;
1197 /* override clone_skb if user passed default value
1198 * at module loading time
1200 pkt_dev->clone_skb = 0;
1201 } else if (strcmp(f, "queue_xmit") == 0) {
1202 pkt_dev->xmit_mode = M_QUEUE_XMIT;
1203 pkt_dev->last_ok = 1;
1204 } else {
1205 sprintf(pg_result,
1206 "xmit_mode -:%s:- unknown\nAvailable modes: %s",
1207 f, "start_xmit, netif_receive\n");
1208 return count;
1210 sprintf(pg_result, "OK: xmit_mode=%s", f);
1211 return count;
1213 if (!strcmp(name, "flag")) {
1214 __u32 flag;
1215 char f[32];
1216 bool disable = false;
1218 memset(f, 0, 32);
1219 len = strn_len(&user_buffer[i], sizeof(f) - 1);
1220 if (len < 0)
1221 return len;
1223 if (copy_from_user(f, &user_buffer[i], len))
1224 return -EFAULT;
1225 i += len;
1227 flag = pktgen_read_flag(f, &disable);
1229 if (flag) {
1230 if (disable)
1231 pkt_dev->flags &= ~flag;
1232 else
1233 pkt_dev->flags |= flag;
1234 } else {
1235 sprintf(pg_result,
1236 "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
1238 "IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
1239 "MACSRC_RND, MACDST_RND, TXSIZE_RND, IPV6, "
1240 "MPLS_RND, VID_RND, SVID_RND, FLOW_SEQ, "
1241 "QUEUE_MAP_RND, QUEUE_MAP_CPU, UDPCSUM, "
1242 "NO_TIMESTAMP, "
1243 #ifdef CONFIG_XFRM
1244 "IPSEC, "
1245 #endif
1246 "NODE_ALLOC\n");
1247 return count;
1249 sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
1250 return count;
1252 if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
1253 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
1254 if (len < 0)
1255 return len;
1257 if (copy_from_user(buf, &user_buffer[i], len))
1258 return -EFAULT;
1259 buf[len] = 0;
1260 if (strcmp(buf, pkt_dev->dst_min) != 0) {
1261 memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
1262 strcpy(pkt_dev->dst_min, buf);
1263 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
1264 pkt_dev->cur_daddr = pkt_dev->daddr_min;
1266 if (debug)
1267 pr_debug("dst_min set to: %s\n", pkt_dev->dst_min);
1268 i += len;
1269 sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min);
1270 return count;
1272 if (!strcmp(name, "dst_max")) {
1273 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
1274 if (len < 0)
1275 return len;
1277 if (copy_from_user(buf, &user_buffer[i], len))
1278 return -EFAULT;
1279 buf[len] = 0;
1280 if (strcmp(buf, pkt_dev->dst_max) != 0) {
1281 memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
1282 strcpy(pkt_dev->dst_max, buf);
1283 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
1284 pkt_dev->cur_daddr = pkt_dev->daddr_max;
1286 if (debug)
1287 pr_debug("dst_max set to: %s\n", pkt_dev->dst_max);
1288 i += len;
1289 sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max);
1290 return count;
1292 if (!strcmp(name, "dst6")) {
1293 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1294 if (len < 0)
1295 return len;
1297 pkt_dev->flags |= F_IPV6;
1299 if (copy_from_user(buf, &user_buffer[i], len))
1300 return -EFAULT;
1301 buf[len] = 0;
1303 in6_pton(buf, -1, pkt_dev->in6_daddr.s6_addr, -1, NULL);
1304 snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->in6_daddr);
1306 pkt_dev->cur_in6_daddr = pkt_dev->in6_daddr;
1308 if (debug)
1309 pr_debug("dst6 set to: %s\n", buf);
1311 i += len;
1312 sprintf(pg_result, "OK: dst6=%s", buf);
1313 return count;
1315 if (!strcmp(name, "dst6_min")) {
1316 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1317 if (len < 0)
1318 return len;
1320 pkt_dev->flags |= F_IPV6;
1322 if (copy_from_user(buf, &user_buffer[i], len))
1323 return -EFAULT;
1324 buf[len] = 0;
1326 in6_pton(buf, -1, pkt_dev->min_in6_daddr.s6_addr, -1, NULL);
1327 snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->min_in6_daddr);
1329 pkt_dev->cur_in6_daddr = pkt_dev->min_in6_daddr;
1330 if (debug)
1331 pr_debug("dst6_min set to: %s\n", buf);
1333 i += len;
1334 sprintf(pg_result, "OK: dst6_min=%s", buf);
1335 return count;
1337 if (!strcmp(name, "dst6_max")) {
1338 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1339 if (len < 0)
1340 return len;
1342 pkt_dev->flags |= F_IPV6;
1344 if (copy_from_user(buf, &user_buffer[i], len))
1345 return -EFAULT;
1346 buf[len] = 0;
1348 in6_pton(buf, -1, pkt_dev->max_in6_daddr.s6_addr, -1, NULL);
1349 snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->max_in6_daddr);
1351 if (debug)
1352 pr_debug("dst6_max set to: %s\n", buf);
1354 i += len;
1355 sprintf(pg_result, "OK: dst6_max=%s", buf);
1356 return count;
1358 if (!strcmp(name, "src6")) {
1359 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1360 if (len < 0)
1361 return len;
1363 pkt_dev->flags |= F_IPV6;
1365 if (copy_from_user(buf, &user_buffer[i], len))
1366 return -EFAULT;
1367 buf[len] = 0;
1369 in6_pton(buf, -1, pkt_dev->in6_saddr.s6_addr, -1, NULL);
1370 snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->in6_saddr);
1372 pkt_dev->cur_in6_saddr = pkt_dev->in6_saddr;
1374 if (debug)
1375 pr_debug("src6 set to: %s\n", buf);
1377 i += len;
1378 sprintf(pg_result, "OK: src6=%s", buf);
1379 return count;
1381 if (!strcmp(name, "src_min")) {
1382 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
1383 if (len < 0)
1384 return len;
1386 if (copy_from_user(buf, &user_buffer[i], len))
1387 return -EFAULT;
1388 buf[len] = 0;
1389 if (strcmp(buf, pkt_dev->src_min) != 0) {
1390 memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
1391 strcpy(pkt_dev->src_min, buf);
1392 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
1393 pkt_dev->cur_saddr = pkt_dev->saddr_min;
1395 if (debug)
1396 pr_debug("src_min set to: %s\n", pkt_dev->src_min);
1397 i += len;
1398 sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min);
1399 return count;
1401 if (!strcmp(name, "src_max")) {
1402 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
1403 if (len < 0)
1404 return len;
1406 if (copy_from_user(buf, &user_buffer[i], len))
1407 return -EFAULT;
1408 buf[len] = 0;
1409 if (strcmp(buf, pkt_dev->src_max) != 0) {
1410 memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
1411 strcpy(pkt_dev->src_max, buf);
1412 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
1413 pkt_dev->cur_saddr = pkt_dev->saddr_max;
1415 if (debug)
1416 pr_debug("src_max set to: %s\n", pkt_dev->src_max);
1417 i += len;
1418 sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max);
1419 return count;
1421 if (!strcmp(name, "dst_mac")) {
1422 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
1423 if (len < 0)
1424 return len;
1426 memset(valstr, 0, sizeof(valstr));
1427 if (copy_from_user(valstr, &user_buffer[i], len))
1428 return -EFAULT;
1430 if (!mac_pton(valstr, pkt_dev->dst_mac))
1431 return -EINVAL;
1432 /* Set up Dest MAC */
1433 ether_addr_copy(&pkt_dev->hh[0], pkt_dev->dst_mac);
1435 sprintf(pg_result, "OK: dstmac %pM", pkt_dev->dst_mac);
1436 return count;
1438 if (!strcmp(name, "src_mac")) {
1439 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
1440 if (len < 0)
1441 return len;
1443 memset(valstr, 0, sizeof(valstr));
1444 if (copy_from_user(valstr, &user_buffer[i], len))
1445 return -EFAULT;
1447 if (!mac_pton(valstr, pkt_dev->src_mac))
1448 return -EINVAL;
1449 /* Set up Src MAC */
1450 ether_addr_copy(&pkt_dev->hh[6], pkt_dev->src_mac);
1452 sprintf(pg_result, "OK: srcmac %pM", pkt_dev->src_mac);
1453 return count;
1456 if (!strcmp(name, "clear_counters")) {
1457 pktgen_clear_counters(pkt_dev);
1458 sprintf(pg_result, "OK: Clearing counters.\n");
1459 return count;
1462 if (!strcmp(name, "flows")) {
1463 len = num_arg(&user_buffer[i], 10, &value);
1464 if (len < 0)
1465 return len;
1467 i += len;
1468 if (value > MAX_CFLOWS)
1469 value = MAX_CFLOWS;
1471 pkt_dev->cflows = value;
1472 sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
1473 return count;
1475 #ifdef CONFIG_XFRM
1476 if (!strcmp(name, "spi")) {
1477 len = num_arg(&user_buffer[i], 10, &value);
1478 if (len < 0)
1479 return len;
1481 i += len;
1482 pkt_dev->spi = value;
1483 sprintf(pg_result, "OK: spi=%u", pkt_dev->spi);
1484 return count;
1486 #endif
1487 if (!strcmp(name, "flowlen")) {
1488 len = num_arg(&user_buffer[i], 10, &value);
1489 if (len < 0)
1490 return len;
1492 i += len;
1493 pkt_dev->lflow = value;
1494 sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
1495 return count;
1498 if (!strcmp(name, "queue_map_min")) {
1499 len = num_arg(&user_buffer[i], 5, &value);
1500 if (len < 0)
1501 return len;
1503 i += len;
1504 pkt_dev->queue_map_min = value;
1505 sprintf(pg_result, "OK: queue_map_min=%u", pkt_dev->queue_map_min);
1506 return count;
1509 if (!strcmp(name, "queue_map_max")) {
1510 len = num_arg(&user_buffer[i], 5, &value);
1511 if (len < 0)
1512 return len;
1514 i += len;
1515 pkt_dev->queue_map_max = value;
1516 sprintf(pg_result, "OK: queue_map_max=%u", pkt_dev->queue_map_max);
1517 return count;
1520 if (!strcmp(name, "mpls")) {
1521 unsigned int n, cnt;
1523 len = get_labels(&user_buffer[i], pkt_dev);
1524 if (len < 0)
1525 return len;
1526 i += len;
1527 cnt = sprintf(pg_result, "OK: mpls=");
1528 for (n = 0; n < pkt_dev->nr_labels; n++)
1529 cnt += sprintf(pg_result + cnt,
1530 "%08x%s", ntohl(pkt_dev->labels[n]),
1531 n == pkt_dev->nr_labels-1 ? "" : ",");
1533 if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) {
1534 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1535 pkt_dev->svlan_id = 0xffff;
1537 if (debug)
1538 pr_debug("VLAN/SVLAN auto turned off\n");
1540 return count;
1543 if (!strcmp(name, "vlan_id")) {
1544 len = num_arg(&user_buffer[i], 4, &value);
1545 if (len < 0)
1546 return len;
1548 i += len;
1549 if (value <= 4095) {
1550 pkt_dev->vlan_id = value; /* turn on VLAN */
1552 if (debug)
1553 pr_debug("VLAN turned on\n");
1555 if (debug && pkt_dev->nr_labels)
1556 pr_debug("MPLS auto turned off\n");
1558 pkt_dev->nr_labels = 0; /* turn off MPLS */
1559 sprintf(pg_result, "OK: vlan_id=%u", pkt_dev->vlan_id);
1560 } else {
1561 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1562 pkt_dev->svlan_id = 0xffff;
1564 if (debug)
1565 pr_debug("VLAN/SVLAN turned off\n");
1567 return count;
1570 if (!strcmp(name, "vlan_p")) {
1571 len = num_arg(&user_buffer[i], 1, &value);
1572 if (len < 0)
1573 return len;
1575 i += len;
1576 if ((value <= 7) && (pkt_dev->vlan_id != 0xffff)) {
1577 pkt_dev->vlan_p = value;
1578 sprintf(pg_result, "OK: vlan_p=%u", pkt_dev->vlan_p);
1579 } else {
1580 sprintf(pg_result, "ERROR: vlan_p must be 0-7");
1582 return count;
1585 if (!strcmp(name, "vlan_cfi")) {
1586 len = num_arg(&user_buffer[i], 1, &value);
1587 if (len < 0)
1588 return len;
1590 i += len;
1591 if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) {
1592 pkt_dev->vlan_cfi = value;
1593 sprintf(pg_result, "OK: vlan_cfi=%u", pkt_dev->vlan_cfi);
1594 } else {
1595 sprintf(pg_result, "ERROR: vlan_cfi must be 0-1");
1597 return count;
1600 if (!strcmp(name, "svlan_id")) {
1601 len = num_arg(&user_buffer[i], 4, &value);
1602 if (len < 0)
1603 return len;
1605 i += len;
1606 if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) {
1607 pkt_dev->svlan_id = value; /* turn on SVLAN */
1609 if (debug)
1610 pr_debug("SVLAN turned on\n");
1612 if (debug && pkt_dev->nr_labels)
1613 pr_debug("MPLS auto turned off\n");
1615 pkt_dev->nr_labels = 0; /* turn off MPLS */
1616 sprintf(pg_result, "OK: svlan_id=%u", pkt_dev->svlan_id);
1617 } else {
1618 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1619 pkt_dev->svlan_id = 0xffff;
1621 if (debug)
1622 pr_debug("VLAN/SVLAN turned off\n");
1624 return count;
1627 if (!strcmp(name, "svlan_p")) {
1628 len = num_arg(&user_buffer[i], 1, &value);
1629 if (len < 0)
1630 return len;
1632 i += len;
1633 if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) {
1634 pkt_dev->svlan_p = value;
1635 sprintf(pg_result, "OK: svlan_p=%u", pkt_dev->svlan_p);
1636 } else {
1637 sprintf(pg_result, "ERROR: svlan_p must be 0-7");
1639 return count;
1642 if (!strcmp(name, "svlan_cfi")) {
1643 len = num_arg(&user_buffer[i], 1, &value);
1644 if (len < 0)
1645 return len;
1647 i += len;
1648 if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) {
1649 pkt_dev->svlan_cfi = value;
1650 sprintf(pg_result, "OK: svlan_cfi=%u", pkt_dev->svlan_cfi);
1651 } else {
1652 sprintf(pg_result, "ERROR: svlan_cfi must be 0-1");
1654 return count;
1657 if (!strcmp(name, "tos")) {
1658 __u32 tmp_value = 0;
1659 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1660 if (len < 0)
1661 return len;
1663 i += len;
1664 if (len == 2) {
1665 pkt_dev->tos = tmp_value;
1666 sprintf(pg_result, "OK: tos=0x%02x", pkt_dev->tos);
1667 } else {
1668 sprintf(pg_result, "ERROR: tos must be 00-ff");
1670 return count;
1673 if (!strcmp(name, "traffic_class")) {
1674 __u32 tmp_value = 0;
1675 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1676 if (len < 0)
1677 return len;
1679 i += len;
1680 if (len == 2) {
1681 pkt_dev->traffic_class = tmp_value;
1682 sprintf(pg_result, "OK: traffic_class=0x%02x", pkt_dev->traffic_class);
1683 } else {
1684 sprintf(pg_result, "ERROR: traffic_class must be 00-ff");
1686 return count;
1689 if (!strcmp(name, "skb_priority")) {
1690 len = num_arg(&user_buffer[i], 9, &value);
1691 if (len < 0)
1692 return len;
1694 i += len;
1695 pkt_dev->skb_priority = value;
1696 sprintf(pg_result, "OK: skb_priority=%i",
1697 pkt_dev->skb_priority);
1698 return count;
1701 sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
1702 return -EINVAL;
1705 static int pktgen_if_open(struct inode *inode, struct file *file)
1707 return single_open(file, pktgen_if_show, PDE_DATA(inode));
1710 static const struct file_operations pktgen_if_fops = {
1711 .open = pktgen_if_open,
1712 .read = seq_read,
1713 .llseek = seq_lseek,
1714 .write = pktgen_if_write,
1715 .release = single_release,
1718 static int pktgen_thread_show(struct seq_file *seq, void *v)
1720 struct pktgen_thread *t = seq->private;
1721 const struct pktgen_dev *pkt_dev;
1723 BUG_ON(!t);
1725 seq_puts(seq, "Running: ");
1727 rcu_read_lock();
1728 list_for_each_entry_rcu(pkt_dev, &t->if_list, list)
1729 if (pkt_dev->running)
1730 seq_printf(seq, "%s ", pkt_dev->odevname);
1732 seq_puts(seq, "\nStopped: ");
1734 list_for_each_entry_rcu(pkt_dev, &t->if_list, list)
1735 if (!pkt_dev->running)
1736 seq_printf(seq, "%s ", pkt_dev->odevname);
1738 if (t->result[0])
1739 seq_printf(seq, "\nResult: %s\n", t->result);
1740 else
1741 seq_puts(seq, "\nResult: NA\n");
1743 rcu_read_unlock();
1745 return 0;
1748 static ssize_t pktgen_thread_write(struct file *file,
1749 const char __user * user_buffer,
1750 size_t count, loff_t * offset)
1752 struct seq_file *seq = file->private_data;
1753 struct pktgen_thread *t = seq->private;
1754 int i, max, len, ret;
1755 char name[40];
1756 char *pg_result;
1758 if (count < 1) {
1759 // sprintf(pg_result, "Wrong command format");
1760 return -EINVAL;
1763 max = count;
1764 len = count_trail_chars(user_buffer, max);
1765 if (len < 0)
1766 return len;
1768 i = len;
1770 /* Read variable name */
1772 len = strn_len(&user_buffer[i], sizeof(name) - 1);
1773 if (len < 0)
1774 return len;
1776 memset(name, 0, sizeof(name));
1777 if (copy_from_user(name, &user_buffer[i], len))
1778 return -EFAULT;
1779 i += len;
1781 max = count - i;
1782 len = count_trail_chars(&user_buffer[i], max);
1783 if (len < 0)
1784 return len;
1786 i += len;
1788 if (debug)
1789 pr_debug("t=%s, count=%lu\n", name, (unsigned long)count);
1791 if (!t) {
1792 pr_err("ERROR: No thread\n");
1793 ret = -EINVAL;
1794 goto out;
1797 pg_result = &(t->result[0]);
1799 if (!strcmp(name, "add_device")) {
1800 char f[32];
1801 memset(f, 0, 32);
1802 len = strn_len(&user_buffer[i], sizeof(f) - 1);
1803 if (len < 0) {
1804 ret = len;
1805 goto out;
1807 if (copy_from_user(f, &user_buffer[i], len))
1808 return -EFAULT;
1809 i += len;
1810 mutex_lock(&pktgen_thread_lock);
1811 ret = pktgen_add_device(t, f);
1812 mutex_unlock(&pktgen_thread_lock);
1813 if (!ret) {
1814 ret = count;
1815 sprintf(pg_result, "OK: add_device=%s", f);
1816 } else
1817 sprintf(pg_result, "ERROR: can not add device %s", f);
1818 goto out;
1821 if (!strcmp(name, "rem_device_all")) {
1822 mutex_lock(&pktgen_thread_lock);
1823 t->control |= T_REMDEVALL;
1824 mutex_unlock(&pktgen_thread_lock);
1825 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
1826 ret = count;
1827 sprintf(pg_result, "OK: rem_device_all");
1828 goto out;
1831 if (!strcmp(name, "max_before_softirq")) {
1832 sprintf(pg_result, "OK: Note! max_before_softirq is obsoleted -- Do not use");
1833 ret = count;
1834 goto out;
1837 ret = -EINVAL;
1838 out:
1839 return ret;
1842 static int pktgen_thread_open(struct inode *inode, struct file *file)
1844 return single_open(file, pktgen_thread_show, PDE_DATA(inode));
1847 static const struct file_operations pktgen_thread_fops = {
1848 .open = pktgen_thread_open,
1849 .read = seq_read,
1850 .llseek = seq_lseek,
1851 .write = pktgen_thread_write,
1852 .release = single_release,
1855 /* Think find or remove for NN */
1856 static struct pktgen_dev *__pktgen_NN_threads(const struct pktgen_net *pn,
1857 const char *ifname, int remove)
1859 struct pktgen_thread *t;
1860 struct pktgen_dev *pkt_dev = NULL;
1861 bool exact = (remove == FIND);
1863 list_for_each_entry(t, &pn->pktgen_threads, th_list) {
1864 pkt_dev = pktgen_find_dev(t, ifname, exact);
1865 if (pkt_dev) {
1866 if (remove) {
1867 pkt_dev->removal_mark = 1;
1868 t->control |= T_REMDEV;
1870 break;
1873 return pkt_dev;
1877 * mark a device for removal
1879 static void pktgen_mark_device(const struct pktgen_net *pn, const char *ifname)
1881 struct pktgen_dev *pkt_dev = NULL;
1882 const int max_tries = 10, msec_per_try = 125;
1883 int i = 0;
1885 mutex_lock(&pktgen_thread_lock);
1886 pr_debug("%s: marking %s for removal\n", __func__, ifname);
1888 while (1) {
1890 pkt_dev = __pktgen_NN_threads(pn, ifname, REMOVE);
1891 if (pkt_dev == NULL)
1892 break; /* success */
1894 mutex_unlock(&pktgen_thread_lock);
1895 pr_debug("%s: waiting for %s to disappear....\n",
1896 __func__, ifname);
1897 schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
1898 mutex_lock(&pktgen_thread_lock);
1900 if (++i >= max_tries) {
1901 pr_err("%s: timed out after waiting %d msec for device %s to be removed\n",
1902 __func__, msec_per_try * i, ifname);
1903 break;
1908 mutex_unlock(&pktgen_thread_lock);
1911 static void pktgen_change_name(const struct pktgen_net *pn, struct net_device *dev)
1913 struct pktgen_thread *t;
1915 mutex_lock(&pktgen_thread_lock);
1917 list_for_each_entry(t, &pn->pktgen_threads, th_list) {
1918 struct pktgen_dev *pkt_dev;
1920 if_lock(t);
1921 list_for_each_entry(pkt_dev, &t->if_list, list) {
1922 if (pkt_dev->odev != dev)
1923 continue;
1925 proc_remove(pkt_dev->entry);
1927 pkt_dev->entry = proc_create_data(dev->name, 0600,
1928 pn->proc_dir,
1929 &pktgen_if_fops,
1930 pkt_dev);
1931 if (!pkt_dev->entry)
1932 pr_err("can't move proc entry for '%s'\n",
1933 dev->name);
1934 break;
1936 if_unlock(t);
1938 mutex_unlock(&pktgen_thread_lock);
1941 static int pktgen_device_event(struct notifier_block *unused,
1942 unsigned long event, void *ptr)
1944 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1945 struct pktgen_net *pn = net_generic(dev_net(dev), pg_net_id);
1947 if (pn->pktgen_exiting)
1948 return NOTIFY_DONE;
1950 /* It is OK that we do not hold the group lock right now,
1951 * as we run under the RTNL lock.
1954 switch (event) {
1955 case NETDEV_CHANGENAME:
1956 pktgen_change_name(pn, dev);
1957 break;
1959 case NETDEV_UNREGISTER:
1960 pktgen_mark_device(pn, dev->name);
1961 break;
1964 return NOTIFY_DONE;
1967 static struct net_device *pktgen_dev_get_by_name(const struct pktgen_net *pn,
1968 struct pktgen_dev *pkt_dev,
1969 const char *ifname)
1971 char b[IFNAMSIZ+5];
1972 int i;
1974 for (i = 0; ifname[i] != '@'; i++) {
1975 if (i == IFNAMSIZ)
1976 break;
1978 b[i] = ifname[i];
1980 b[i] = 0;
1982 return dev_get_by_name(pn->net, b);
1986 /* Associate pktgen_dev with a device. */
1988 static int pktgen_setup_dev(const struct pktgen_net *pn,
1989 struct pktgen_dev *pkt_dev, const char *ifname)
1991 struct net_device *odev;
1992 int err;
1994 /* Clean old setups */
1995 if (pkt_dev->odev) {
1996 dev_put(pkt_dev->odev);
1997 pkt_dev->odev = NULL;
2000 odev = pktgen_dev_get_by_name(pn, pkt_dev, ifname);
2001 if (!odev) {
2002 pr_err("no such netdevice: \"%s\"\n", ifname);
2003 return -ENODEV;
2006 if (odev->type != ARPHRD_ETHER) {
2007 pr_err("not an ethernet device: \"%s\"\n", ifname);
2008 err = -EINVAL;
2009 } else if (!netif_running(odev)) {
2010 pr_err("device is down: \"%s\"\n", ifname);
2011 err = -ENETDOWN;
2012 } else {
2013 pkt_dev->odev = odev;
2014 return 0;
2017 dev_put(odev);
2018 return err;
2021 /* Read pkt_dev from the interface and set up internal pktgen_dev
2022 * structure to have the right information to create/send packets
2024 static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
2026 int ntxq;
2028 if (!pkt_dev->odev) {
2029 pr_err("ERROR: pkt_dev->odev == NULL in setup_inject\n");
2030 sprintf(pkt_dev->result,
2031 "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
2032 return;
2035 /* make sure that we don't pick a non-existing transmit queue */
2036 ntxq = pkt_dev->odev->real_num_tx_queues;
2038 if (ntxq <= pkt_dev->queue_map_min) {
2039 pr_warn("WARNING: Requested queue_map_min (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
2040 pkt_dev->queue_map_min, (ntxq ?: 1) - 1, ntxq,
2041 pkt_dev->odevname);
2042 pkt_dev->queue_map_min = (ntxq ?: 1) - 1;
2044 if (pkt_dev->queue_map_max >= ntxq) {
2045 pr_warn("WARNING: Requested queue_map_max (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
2046 pkt_dev->queue_map_max, (ntxq ?: 1) - 1, ntxq,
2047 pkt_dev->odevname);
2048 pkt_dev->queue_map_max = (ntxq ?: 1) - 1;
2051 /* Default to the interface's mac if not explicitly set. */
2053 if (is_zero_ether_addr(pkt_dev->src_mac))
2054 ether_addr_copy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr);
2056 /* Set up Dest MAC */
2057 ether_addr_copy(&(pkt_dev->hh[0]), pkt_dev->dst_mac);
2059 if (pkt_dev->flags & F_IPV6) {
2060 int i, set = 0, err = 1;
2061 struct inet6_dev *idev;
2063 if (pkt_dev->min_pkt_size == 0) {
2064 pkt_dev->min_pkt_size = 14 + sizeof(struct ipv6hdr)
2065 + sizeof(struct udphdr)
2066 + sizeof(struct pktgen_hdr)
2067 + pkt_dev->pkt_overhead;
2070 for (i = 0; i < sizeof(struct in6_addr); i++)
2071 if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
2072 set = 1;
2073 break;
2076 if (!set) {
2079 * Use linklevel address if unconfigured.
2081 * use ipv6_get_lladdr if/when it's get exported
2084 rcu_read_lock();
2085 idev = __in6_dev_get(pkt_dev->odev);
2086 if (idev) {
2087 struct inet6_ifaddr *ifp;
2089 read_lock_bh(&idev->lock);
2090 list_for_each_entry(ifp, &idev->addr_list, if_list) {
2091 if ((ifp->scope & IFA_LINK) &&
2092 !(ifp->flags & IFA_F_TENTATIVE)) {
2093 pkt_dev->cur_in6_saddr = ifp->addr;
2094 err = 0;
2095 break;
2098 read_unlock_bh(&idev->lock);
2100 rcu_read_unlock();
2101 if (err)
2102 pr_err("ERROR: IPv6 link address not available\n");
2104 } else {
2105 if (pkt_dev->min_pkt_size == 0) {
2106 pkt_dev->min_pkt_size = 14 + sizeof(struct iphdr)
2107 + sizeof(struct udphdr)
2108 + sizeof(struct pktgen_hdr)
2109 + pkt_dev->pkt_overhead;
2112 pkt_dev->saddr_min = 0;
2113 pkt_dev->saddr_max = 0;
2114 if (strlen(pkt_dev->src_min) == 0) {
2116 struct in_device *in_dev;
2118 rcu_read_lock();
2119 in_dev = __in_dev_get_rcu(pkt_dev->odev);
2120 if (in_dev) {
2121 if (in_dev->ifa_list) {
2122 pkt_dev->saddr_min =
2123 in_dev->ifa_list->ifa_address;
2124 pkt_dev->saddr_max = pkt_dev->saddr_min;
2127 rcu_read_unlock();
2128 } else {
2129 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
2130 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
2133 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
2134 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
2136 /* Initialize current values. */
2137 pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
2138 if (pkt_dev->min_pkt_size > pkt_dev->max_pkt_size)
2139 pkt_dev->max_pkt_size = pkt_dev->min_pkt_size;
2141 pkt_dev->cur_dst_mac_offset = 0;
2142 pkt_dev->cur_src_mac_offset = 0;
2143 pkt_dev->cur_saddr = pkt_dev->saddr_min;
2144 pkt_dev->cur_daddr = pkt_dev->daddr_min;
2145 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2146 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
2147 pkt_dev->nflows = 0;
2151 static void spin(struct pktgen_dev *pkt_dev, ktime_t spin_until)
2153 ktime_t start_time, end_time;
2154 s64 remaining;
2155 struct hrtimer_sleeper t;
2157 hrtimer_init_on_stack(&t.timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
2158 hrtimer_set_expires(&t.timer, spin_until);
2160 remaining = ktime_to_ns(hrtimer_expires_remaining(&t.timer));
2161 if (remaining <= 0)
2162 goto out;
2164 start_time = ktime_get();
2165 if (remaining < 100000) {
2166 /* for small delays (<100us), just loop until limit is reached */
2167 do {
2168 end_time = ktime_get();
2169 } while (ktime_compare(end_time, spin_until) < 0);
2170 } else {
2171 /* see do_nanosleep */
2172 hrtimer_init_sleeper(&t, current);
2173 do {
2174 set_current_state(TASK_INTERRUPTIBLE);
2175 hrtimer_start_expires(&t.timer, HRTIMER_MODE_ABS);
2177 if (likely(t.task))
2178 schedule();
2180 hrtimer_cancel(&t.timer);
2181 } while (t.task && pkt_dev->running && !signal_pending(current));
2182 __set_current_state(TASK_RUNNING);
2183 end_time = ktime_get();
2186 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(end_time, start_time));
2187 out:
2188 pkt_dev->next_tx = ktime_add_ns(spin_until, pkt_dev->delay);
2189 destroy_hrtimer_on_stack(&t.timer);
2192 static inline void set_pkt_overhead(struct pktgen_dev *pkt_dev)
2194 pkt_dev->pkt_overhead = 0;
2195 pkt_dev->pkt_overhead += pkt_dev->nr_labels*sizeof(u32);
2196 pkt_dev->pkt_overhead += VLAN_TAG_SIZE(pkt_dev);
2197 pkt_dev->pkt_overhead += SVLAN_TAG_SIZE(pkt_dev);
2200 static inline int f_seen(const struct pktgen_dev *pkt_dev, int flow)
2202 return !!(pkt_dev->flows[flow].flags & F_INIT);
2205 static inline int f_pick(struct pktgen_dev *pkt_dev)
2207 int flow = pkt_dev->curfl;
2209 if (pkt_dev->flags & F_FLOW_SEQ) {
2210 if (pkt_dev->flows[flow].count >= pkt_dev->lflow) {
2211 /* reset time */
2212 pkt_dev->flows[flow].count = 0;
2213 pkt_dev->flows[flow].flags = 0;
2214 pkt_dev->curfl += 1;
2215 if (pkt_dev->curfl >= pkt_dev->cflows)
2216 pkt_dev->curfl = 0; /*reset */
2218 } else {
2219 flow = prandom_u32() % pkt_dev->cflows;
2220 pkt_dev->curfl = flow;
2222 if (pkt_dev->flows[flow].count > pkt_dev->lflow) {
2223 pkt_dev->flows[flow].count = 0;
2224 pkt_dev->flows[flow].flags = 0;
2228 return pkt_dev->curfl;
2232 #ifdef CONFIG_XFRM
2233 /* If there was already an IPSEC SA, we keep it as is, else
2234 * we go look for it ...
2236 #define DUMMY_MARK 0
2237 static void get_ipsec_sa(struct pktgen_dev *pkt_dev, int flow)
2239 struct xfrm_state *x = pkt_dev->flows[flow].x;
2240 struct pktgen_net *pn = net_generic(dev_net(pkt_dev->odev), pg_net_id);
2241 if (!x) {
2243 if (pkt_dev->spi) {
2244 /* We need as quick as possible to find the right SA
2245 * Searching with minimum criteria to archieve this.
2247 x = xfrm_state_lookup_byspi(pn->net, htonl(pkt_dev->spi), AF_INET);
2248 } else {
2249 /* slow path: we dont already have xfrm_state */
2250 x = xfrm_stateonly_find(pn->net, DUMMY_MARK, 0,
2251 (xfrm_address_t *)&pkt_dev->cur_daddr,
2252 (xfrm_address_t *)&pkt_dev->cur_saddr,
2253 AF_INET,
2254 pkt_dev->ipsmode,
2255 pkt_dev->ipsproto, 0);
2257 if (x) {
2258 pkt_dev->flows[flow].x = x;
2259 set_pkt_overhead(pkt_dev);
2260 pkt_dev->pkt_overhead += x->props.header_len;
2265 #endif
2266 static void set_cur_queue_map(struct pktgen_dev *pkt_dev)
2269 if (pkt_dev->flags & F_QUEUE_MAP_CPU)
2270 pkt_dev->cur_queue_map = smp_processor_id();
2272 else if (pkt_dev->queue_map_min <= pkt_dev->queue_map_max) {
2273 __u16 t;
2274 if (pkt_dev->flags & F_QUEUE_MAP_RND) {
2275 t = prandom_u32() %
2276 (pkt_dev->queue_map_max -
2277 pkt_dev->queue_map_min + 1)
2278 + pkt_dev->queue_map_min;
2279 } else {
2280 t = pkt_dev->cur_queue_map + 1;
2281 if (t > pkt_dev->queue_map_max)
2282 t = pkt_dev->queue_map_min;
2284 pkt_dev->cur_queue_map = t;
2286 pkt_dev->cur_queue_map = pkt_dev->cur_queue_map % pkt_dev->odev->real_num_tx_queues;
2289 /* Increment/randomize headers according to flags and current values
2290 * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
2292 static void mod_cur_headers(struct pktgen_dev *pkt_dev)
2294 __u32 imn;
2295 __u32 imx;
2296 int flow = 0;
2298 if (pkt_dev->cflows)
2299 flow = f_pick(pkt_dev);
2301 /* Deal with source MAC */
2302 if (pkt_dev->src_mac_count > 1) {
2303 __u32 mc;
2304 __u32 tmp;
2306 if (pkt_dev->flags & F_MACSRC_RND)
2307 mc = prandom_u32() % pkt_dev->src_mac_count;
2308 else {
2309 mc = pkt_dev->cur_src_mac_offset++;
2310 if (pkt_dev->cur_src_mac_offset >=
2311 pkt_dev->src_mac_count)
2312 pkt_dev->cur_src_mac_offset = 0;
2315 tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
2316 pkt_dev->hh[11] = tmp;
2317 tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2318 pkt_dev->hh[10] = tmp;
2319 tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2320 pkt_dev->hh[9] = tmp;
2321 tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2322 pkt_dev->hh[8] = tmp;
2323 tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
2324 pkt_dev->hh[7] = tmp;
2327 /* Deal with Destination MAC */
2328 if (pkt_dev->dst_mac_count > 1) {
2329 __u32 mc;
2330 __u32 tmp;
2332 if (pkt_dev->flags & F_MACDST_RND)
2333 mc = prandom_u32() % pkt_dev->dst_mac_count;
2335 else {
2336 mc = pkt_dev->cur_dst_mac_offset++;
2337 if (pkt_dev->cur_dst_mac_offset >=
2338 pkt_dev->dst_mac_count) {
2339 pkt_dev->cur_dst_mac_offset = 0;
2343 tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
2344 pkt_dev->hh[5] = tmp;
2345 tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2346 pkt_dev->hh[4] = tmp;
2347 tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2348 pkt_dev->hh[3] = tmp;
2349 tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2350 pkt_dev->hh[2] = tmp;
2351 tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
2352 pkt_dev->hh[1] = tmp;
2355 if (pkt_dev->flags & F_MPLS_RND) {
2356 unsigned int i;
2357 for (i = 0; i < pkt_dev->nr_labels; i++)
2358 if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
2359 pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
2360 ((__force __be32)prandom_u32() &
2361 htonl(0x000fffff));
2364 if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
2365 pkt_dev->vlan_id = prandom_u32() & (4096 - 1);
2368 if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
2369 pkt_dev->svlan_id = prandom_u32() & (4096 - 1);
2372 if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
2373 if (pkt_dev->flags & F_UDPSRC_RND)
2374 pkt_dev->cur_udp_src = prandom_u32() %
2375 (pkt_dev->udp_src_max - pkt_dev->udp_src_min)
2376 + pkt_dev->udp_src_min;
2378 else {
2379 pkt_dev->cur_udp_src++;
2380 if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
2381 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
2385 if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
2386 if (pkt_dev->flags & F_UDPDST_RND) {
2387 pkt_dev->cur_udp_dst = prandom_u32() %
2388 (pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)
2389 + pkt_dev->udp_dst_min;
2390 } else {
2391 pkt_dev->cur_udp_dst++;
2392 if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
2393 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2397 if (!(pkt_dev->flags & F_IPV6)) {
2399 imn = ntohl(pkt_dev->saddr_min);
2400 imx = ntohl(pkt_dev->saddr_max);
2401 if (imn < imx) {
2402 __u32 t;
2403 if (pkt_dev->flags & F_IPSRC_RND)
2404 t = prandom_u32() % (imx - imn) + imn;
2405 else {
2406 t = ntohl(pkt_dev->cur_saddr);
2407 t++;
2408 if (t > imx)
2409 t = imn;
2412 pkt_dev->cur_saddr = htonl(t);
2415 if (pkt_dev->cflows && f_seen(pkt_dev, flow)) {
2416 pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
2417 } else {
2418 imn = ntohl(pkt_dev->daddr_min);
2419 imx = ntohl(pkt_dev->daddr_max);
2420 if (imn < imx) {
2421 __u32 t;
2422 __be32 s;
2423 if (pkt_dev->flags & F_IPDST_RND) {
2425 do {
2426 t = prandom_u32() %
2427 (imx - imn) + imn;
2428 s = htonl(t);
2429 } while (ipv4_is_loopback(s) ||
2430 ipv4_is_multicast(s) ||
2431 ipv4_is_lbcast(s) ||
2432 ipv4_is_zeronet(s) ||
2433 ipv4_is_local_multicast(s));
2434 pkt_dev->cur_daddr = s;
2435 } else {
2436 t = ntohl(pkt_dev->cur_daddr);
2437 t++;
2438 if (t > imx) {
2439 t = imn;
2441 pkt_dev->cur_daddr = htonl(t);
2444 if (pkt_dev->cflows) {
2445 pkt_dev->flows[flow].flags |= F_INIT;
2446 pkt_dev->flows[flow].cur_daddr =
2447 pkt_dev->cur_daddr;
2448 #ifdef CONFIG_XFRM
2449 if (pkt_dev->flags & F_IPSEC)
2450 get_ipsec_sa(pkt_dev, flow);
2451 #endif
2452 pkt_dev->nflows++;
2455 } else { /* IPV6 * */
2457 if (!ipv6_addr_any(&pkt_dev->min_in6_daddr)) {
2458 int i;
2460 /* Only random destinations yet */
2462 for (i = 0; i < 4; i++) {
2463 pkt_dev->cur_in6_daddr.s6_addr32[i] =
2464 (((__force __be32)prandom_u32() |
2465 pkt_dev->min_in6_daddr.s6_addr32[i]) &
2466 pkt_dev->max_in6_daddr.s6_addr32[i]);
2471 if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
2472 __u32 t;
2473 if (pkt_dev->flags & F_TXSIZE_RND) {
2474 t = prandom_u32() %
2475 (pkt_dev->max_pkt_size - pkt_dev->min_pkt_size)
2476 + pkt_dev->min_pkt_size;
2477 } else {
2478 t = pkt_dev->cur_pkt_size + 1;
2479 if (t > pkt_dev->max_pkt_size)
2480 t = pkt_dev->min_pkt_size;
2482 pkt_dev->cur_pkt_size = t;
2485 set_cur_queue_map(pkt_dev);
2487 pkt_dev->flows[flow].count++;
2491 #ifdef CONFIG_XFRM
2492 static u32 pktgen_dst_metrics[RTAX_MAX + 1] = {
2494 [RTAX_HOPLIMIT] = 0x5, /* Set a static hoplimit */
2497 static int pktgen_output_ipsec(struct sk_buff *skb, struct pktgen_dev *pkt_dev)
2499 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2500 int err = 0;
2501 struct net *net = dev_net(pkt_dev->odev);
2503 if (!x)
2504 return 0;
2505 /* XXX: we dont support tunnel mode for now until
2506 * we resolve the dst issue */
2507 if ((x->props.mode != XFRM_MODE_TRANSPORT) && (pkt_dev->spi == 0))
2508 return 0;
2510 /* But when user specify an valid SPI, transformation
2511 * supports both transport/tunnel mode + ESP/AH type.
2513 if ((x->props.mode == XFRM_MODE_TUNNEL) && (pkt_dev->spi != 0))
2514 skb->_skb_refdst = (unsigned long)&pkt_dev->xdst.u.dst | SKB_DST_NOREF;
2516 rcu_read_lock_bh();
2517 err = pktgen_xfrm_outer_mode_output(x, skb);
2518 rcu_read_unlock_bh();
2519 if (err) {
2520 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEMODEERROR);
2521 goto error;
2523 err = x->type->output(x, skb);
2524 if (err) {
2525 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEPROTOERROR);
2526 goto error;
2528 spin_lock_bh(&x->lock);
2529 x->curlft.bytes += skb->len;
2530 x->curlft.packets++;
2531 spin_unlock_bh(&x->lock);
2532 error:
2533 return err;
2536 static void free_SAs(struct pktgen_dev *pkt_dev)
2538 if (pkt_dev->cflows) {
2539 /* let go of the SAs if we have them */
2540 int i;
2541 for (i = 0; i < pkt_dev->cflows; i++) {
2542 struct xfrm_state *x = pkt_dev->flows[i].x;
2543 if (x) {
2544 xfrm_state_put(x);
2545 pkt_dev->flows[i].x = NULL;
2551 static int process_ipsec(struct pktgen_dev *pkt_dev,
2552 struct sk_buff *skb, __be16 protocol)
2554 if (pkt_dev->flags & F_IPSEC) {
2555 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2556 int nhead = 0;
2557 if (x) {
2558 struct ethhdr *eth;
2559 struct iphdr *iph;
2560 int ret;
2562 nhead = x->props.header_len - skb_headroom(skb);
2563 if (nhead > 0) {
2564 ret = pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
2565 if (ret < 0) {
2566 pr_err("Error expanding ipsec packet %d\n",
2567 ret);
2568 goto err;
2572 /* ipsec is not expecting ll header */
2573 skb_pull(skb, ETH_HLEN);
2574 ret = pktgen_output_ipsec(skb, pkt_dev);
2575 if (ret) {
2576 pr_err("Error creating ipsec packet %d\n", ret);
2577 goto err;
2579 /* restore ll */
2580 eth = skb_push(skb, ETH_HLEN);
2581 memcpy(eth, pkt_dev->hh, 2 * ETH_ALEN);
2582 eth->h_proto = protocol;
2584 /* Update IPv4 header len as well as checksum value */
2585 iph = ip_hdr(skb);
2586 iph->tot_len = htons(skb->len - ETH_HLEN);
2587 ip_send_check(iph);
2590 return 1;
2591 err:
2592 kfree_skb(skb);
2593 return 0;
2595 #endif
2597 static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
2599 unsigned int i;
2600 for (i = 0; i < pkt_dev->nr_labels; i++)
2601 *mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
2603 mpls--;
2604 *mpls |= MPLS_STACK_BOTTOM;
2607 static inline __be16 build_tci(unsigned int id, unsigned int cfi,
2608 unsigned int prio)
2610 return htons(id | (cfi << 12) | (prio << 13));
2613 static void pktgen_finalize_skb(struct pktgen_dev *pkt_dev, struct sk_buff *skb,
2614 int datalen)
2616 struct timespec64 timestamp;
2617 struct pktgen_hdr *pgh;
2619 pgh = skb_put(skb, sizeof(*pgh));
2620 datalen -= sizeof(*pgh);
2622 if (pkt_dev->nfrags <= 0) {
2623 skb_put_zero(skb, datalen);
2624 } else {
2625 int frags = pkt_dev->nfrags;
2626 int i, len;
2627 int frag_len;
2630 if (frags > MAX_SKB_FRAGS)
2631 frags = MAX_SKB_FRAGS;
2632 len = datalen - frags * PAGE_SIZE;
2633 if (len > 0) {
2634 skb_put_zero(skb, len);
2635 datalen = frags * PAGE_SIZE;
2638 i = 0;
2639 frag_len = (datalen/frags) < PAGE_SIZE ?
2640 (datalen/frags) : PAGE_SIZE;
2641 while (datalen > 0) {
2642 if (unlikely(!pkt_dev->page)) {
2643 int node = numa_node_id();
2645 if (pkt_dev->node >= 0 && (pkt_dev->flags & F_NODE))
2646 node = pkt_dev->node;
2647 pkt_dev->page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
2648 if (!pkt_dev->page)
2649 break;
2651 get_page(pkt_dev->page);
2652 skb_frag_set_page(skb, i, pkt_dev->page);
2653 skb_shinfo(skb)->frags[i].page_offset = 0;
2654 /*last fragment, fill rest of data*/
2655 if (i == (frags - 1))
2656 skb_frag_size_set(&skb_shinfo(skb)->frags[i],
2657 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE));
2658 else
2659 skb_frag_size_set(&skb_shinfo(skb)->frags[i], frag_len);
2660 datalen -= skb_frag_size(&skb_shinfo(skb)->frags[i]);
2661 skb->len += skb_frag_size(&skb_shinfo(skb)->frags[i]);
2662 skb->data_len += skb_frag_size(&skb_shinfo(skb)->frags[i]);
2663 i++;
2664 skb_shinfo(skb)->nr_frags = i;
2668 /* Stamp the time, and sequence number,
2669 * convert them to network byte order
2671 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2672 pgh->seq_num = htonl(pkt_dev->seq_num);
2674 if (pkt_dev->flags & F_NO_TIMESTAMP) {
2675 pgh->tv_sec = 0;
2676 pgh->tv_usec = 0;
2677 } else {
2679 * pgh->tv_sec wraps in y2106 when interpreted as unsigned
2680 * as done by wireshark, or y2038 when interpreted as signed.
2681 * This is probably harmless, but if anyone wants to improve
2682 * it, we could introduce a variant that puts 64-bit nanoseconds
2683 * into the respective header bytes.
2684 * This would also be slightly faster to read.
2686 ktime_get_real_ts64(&timestamp);
2687 pgh->tv_sec = htonl(timestamp.tv_sec);
2688 pgh->tv_usec = htonl(timestamp.tv_nsec / NSEC_PER_USEC);
2692 static struct sk_buff *pktgen_alloc_skb(struct net_device *dev,
2693 struct pktgen_dev *pkt_dev)
2695 unsigned int extralen = LL_RESERVED_SPACE(dev);
2696 struct sk_buff *skb = NULL;
2697 unsigned int size;
2699 size = pkt_dev->cur_pkt_size + 64 + extralen + pkt_dev->pkt_overhead;
2700 if (pkt_dev->flags & F_NODE) {
2701 int node = pkt_dev->node >= 0 ? pkt_dev->node : numa_node_id();
2703 skb = __alloc_skb(NET_SKB_PAD + size, GFP_NOWAIT, 0, node);
2704 if (likely(skb)) {
2705 skb_reserve(skb, NET_SKB_PAD);
2706 skb->dev = dev;
2708 } else {
2709 skb = __netdev_alloc_skb(dev, size, GFP_NOWAIT);
2712 /* the caller pre-fetches from skb->data and reserves for the mac hdr */
2713 if (likely(skb))
2714 skb_reserve(skb, extralen - 16);
2716 return skb;
2719 static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
2720 struct pktgen_dev *pkt_dev)
2722 struct sk_buff *skb = NULL;
2723 __u8 *eth;
2724 struct udphdr *udph;
2725 int datalen, iplen;
2726 struct iphdr *iph;
2727 __be16 protocol = htons(ETH_P_IP);
2728 __be32 *mpls;
2729 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2730 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2731 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2732 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2733 u16 queue_map;
2735 if (pkt_dev->nr_labels)
2736 protocol = htons(ETH_P_MPLS_UC);
2738 if (pkt_dev->vlan_id != 0xffff)
2739 protocol = htons(ETH_P_8021Q);
2741 /* Update any of the values, used when we're incrementing various
2742 * fields.
2744 mod_cur_headers(pkt_dev);
2745 queue_map = pkt_dev->cur_queue_map;
2747 skb = pktgen_alloc_skb(odev, pkt_dev);
2748 if (!skb) {
2749 sprintf(pkt_dev->result, "No memory");
2750 return NULL;
2753 prefetchw(skb->data);
2754 skb_reserve(skb, 16);
2756 /* Reserve for ethernet and IP header */
2757 eth = skb_push(skb, 14);
2758 mpls = skb_put(skb, pkt_dev->nr_labels * sizeof(__u32));
2759 if (pkt_dev->nr_labels)
2760 mpls_push(mpls, pkt_dev);
2762 if (pkt_dev->vlan_id != 0xffff) {
2763 if (pkt_dev->svlan_id != 0xffff) {
2764 svlan_tci = skb_put(skb, sizeof(__be16));
2765 *svlan_tci = build_tci(pkt_dev->svlan_id,
2766 pkt_dev->svlan_cfi,
2767 pkt_dev->svlan_p);
2768 svlan_encapsulated_proto = skb_put(skb,
2769 sizeof(__be16));
2770 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
2772 vlan_tci = skb_put(skb, sizeof(__be16));
2773 *vlan_tci = build_tci(pkt_dev->vlan_id,
2774 pkt_dev->vlan_cfi,
2775 pkt_dev->vlan_p);
2776 vlan_encapsulated_proto = skb_put(skb, sizeof(__be16));
2777 *vlan_encapsulated_proto = htons(ETH_P_IP);
2780 skb_reset_mac_header(skb);
2781 skb_set_network_header(skb, skb->len);
2782 iph = skb_put(skb, sizeof(struct iphdr));
2784 skb_set_transport_header(skb, skb->len);
2785 udph = skb_put(skb, sizeof(struct udphdr));
2786 skb_set_queue_mapping(skb, queue_map);
2787 skb->priority = pkt_dev->skb_priority;
2789 memcpy(eth, pkt_dev->hh, 12);
2790 *(__be16 *) & eth[12] = protocol;
2792 /* Eth + IPh + UDPh + mpls */
2793 datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
2794 pkt_dev->pkt_overhead;
2795 if (datalen < 0 || datalen < sizeof(struct pktgen_hdr))
2796 datalen = sizeof(struct pktgen_hdr);
2798 udph->source = htons(pkt_dev->cur_udp_src);
2799 udph->dest = htons(pkt_dev->cur_udp_dst);
2800 udph->len = htons(datalen + 8); /* DATA + udphdr */
2801 udph->check = 0;
2803 iph->ihl = 5;
2804 iph->version = 4;
2805 iph->ttl = 32;
2806 iph->tos = pkt_dev->tos;
2807 iph->protocol = IPPROTO_UDP; /* UDP */
2808 iph->saddr = pkt_dev->cur_saddr;
2809 iph->daddr = pkt_dev->cur_daddr;
2810 iph->id = htons(pkt_dev->ip_id);
2811 pkt_dev->ip_id++;
2812 iph->frag_off = 0;
2813 iplen = 20 + 8 + datalen;
2814 iph->tot_len = htons(iplen);
2815 ip_send_check(iph);
2816 skb->protocol = protocol;
2817 skb->dev = odev;
2818 skb->pkt_type = PACKET_HOST;
2820 pktgen_finalize_skb(pkt_dev, skb, datalen);
2822 if (!(pkt_dev->flags & F_UDPCSUM)) {
2823 skb->ip_summed = CHECKSUM_NONE;
2824 } else if (odev->features & (NETIF_F_HW_CSUM | NETIF_F_IP_CSUM)) {
2825 skb->ip_summed = CHECKSUM_PARTIAL;
2826 skb->csum = 0;
2827 udp4_hwcsum(skb, iph->saddr, iph->daddr);
2828 } else {
2829 __wsum csum = skb_checksum(skb, skb_transport_offset(skb), datalen + 8, 0);
2831 /* add protocol-dependent pseudo-header */
2832 udph->check = csum_tcpudp_magic(iph->saddr, iph->daddr,
2833 datalen + 8, IPPROTO_UDP, csum);
2835 if (udph->check == 0)
2836 udph->check = CSUM_MANGLED_0;
2839 #ifdef CONFIG_XFRM
2840 if (!process_ipsec(pkt_dev, skb, protocol))
2841 return NULL;
2842 #endif
2844 return skb;
2847 static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
2848 struct pktgen_dev *pkt_dev)
2850 struct sk_buff *skb = NULL;
2851 __u8 *eth;
2852 struct udphdr *udph;
2853 int datalen, udplen;
2854 struct ipv6hdr *iph;
2855 __be16 protocol = htons(ETH_P_IPV6);
2856 __be32 *mpls;
2857 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2858 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2859 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2860 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2861 u16 queue_map;
2863 if (pkt_dev->nr_labels)
2864 protocol = htons(ETH_P_MPLS_UC);
2866 if (pkt_dev->vlan_id != 0xffff)
2867 protocol = htons(ETH_P_8021Q);
2869 /* Update any of the values, used when we're incrementing various
2870 * fields.
2872 mod_cur_headers(pkt_dev);
2873 queue_map = pkt_dev->cur_queue_map;
2875 skb = pktgen_alloc_skb(odev, pkt_dev);
2876 if (!skb) {
2877 sprintf(pkt_dev->result, "No memory");
2878 return NULL;
2881 prefetchw(skb->data);
2882 skb_reserve(skb, 16);
2884 /* Reserve for ethernet and IP header */
2885 eth = skb_push(skb, 14);
2886 mpls = skb_put(skb, pkt_dev->nr_labels * sizeof(__u32));
2887 if (pkt_dev->nr_labels)
2888 mpls_push(mpls, pkt_dev);
2890 if (pkt_dev->vlan_id != 0xffff) {
2891 if (pkt_dev->svlan_id != 0xffff) {
2892 svlan_tci = skb_put(skb, sizeof(__be16));
2893 *svlan_tci = build_tci(pkt_dev->svlan_id,
2894 pkt_dev->svlan_cfi,
2895 pkt_dev->svlan_p);
2896 svlan_encapsulated_proto = skb_put(skb,
2897 sizeof(__be16));
2898 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
2900 vlan_tci = skb_put(skb, sizeof(__be16));
2901 *vlan_tci = build_tci(pkt_dev->vlan_id,
2902 pkt_dev->vlan_cfi,
2903 pkt_dev->vlan_p);
2904 vlan_encapsulated_proto = skb_put(skb, sizeof(__be16));
2905 *vlan_encapsulated_proto = htons(ETH_P_IPV6);
2908 skb_reset_mac_header(skb);
2909 skb_set_network_header(skb, skb->len);
2910 iph = skb_put(skb, sizeof(struct ipv6hdr));
2912 skb_set_transport_header(skb, skb->len);
2913 udph = skb_put(skb, sizeof(struct udphdr));
2914 skb_set_queue_mapping(skb, queue_map);
2915 skb->priority = pkt_dev->skb_priority;
2917 memcpy(eth, pkt_dev->hh, 12);
2918 *(__be16 *) &eth[12] = protocol;
2920 /* Eth + IPh + UDPh + mpls */
2921 datalen = pkt_dev->cur_pkt_size - 14 -
2922 sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
2923 pkt_dev->pkt_overhead;
2925 if (datalen < 0 || datalen < sizeof(struct pktgen_hdr)) {
2926 datalen = sizeof(struct pktgen_hdr);
2927 net_info_ratelimited("increased datalen to %d\n", datalen);
2930 udplen = datalen + sizeof(struct udphdr);
2931 udph->source = htons(pkt_dev->cur_udp_src);
2932 udph->dest = htons(pkt_dev->cur_udp_dst);
2933 udph->len = htons(udplen);
2934 udph->check = 0;
2936 *(__be32 *) iph = htonl(0x60000000); /* Version + flow */
2938 if (pkt_dev->traffic_class) {
2939 /* Version + traffic class + flow (0) */
2940 *(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
2943 iph->hop_limit = 32;
2945 iph->payload_len = htons(udplen);
2946 iph->nexthdr = IPPROTO_UDP;
2948 iph->daddr = pkt_dev->cur_in6_daddr;
2949 iph->saddr = pkt_dev->cur_in6_saddr;
2951 skb->protocol = protocol;
2952 skb->dev = odev;
2953 skb->pkt_type = PACKET_HOST;
2955 pktgen_finalize_skb(pkt_dev, skb, datalen);
2957 if (!(pkt_dev->flags & F_UDPCSUM)) {
2958 skb->ip_summed = CHECKSUM_NONE;
2959 } else if (odev->features & (NETIF_F_HW_CSUM | NETIF_F_IPV6_CSUM)) {
2960 skb->ip_summed = CHECKSUM_PARTIAL;
2961 skb->csum_start = skb_transport_header(skb) - skb->head;
2962 skb->csum_offset = offsetof(struct udphdr, check);
2963 udph->check = ~csum_ipv6_magic(&iph->saddr, &iph->daddr, udplen, IPPROTO_UDP, 0);
2964 } else {
2965 __wsum csum = skb_checksum(skb, skb_transport_offset(skb), udplen, 0);
2967 /* add protocol-dependent pseudo-header */
2968 udph->check = csum_ipv6_magic(&iph->saddr, &iph->daddr, udplen, IPPROTO_UDP, csum);
2970 if (udph->check == 0)
2971 udph->check = CSUM_MANGLED_0;
2974 return skb;
2977 static struct sk_buff *fill_packet(struct net_device *odev,
2978 struct pktgen_dev *pkt_dev)
2980 if (pkt_dev->flags & F_IPV6)
2981 return fill_packet_ipv6(odev, pkt_dev);
2982 else
2983 return fill_packet_ipv4(odev, pkt_dev);
2986 static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
2988 pkt_dev->seq_num = 1;
2989 pkt_dev->idle_acc = 0;
2990 pkt_dev->sofar = 0;
2991 pkt_dev->tx_bytes = 0;
2992 pkt_dev->errors = 0;
2995 /* Set up structure for sending pkts, clear counters */
2997 static void pktgen_run(struct pktgen_thread *t)
2999 struct pktgen_dev *pkt_dev;
3000 int started = 0;
3002 func_enter();
3004 rcu_read_lock();
3005 list_for_each_entry_rcu(pkt_dev, &t->if_list, list) {
3008 * setup odev and create initial packet.
3010 pktgen_setup_inject(pkt_dev);
3012 if (pkt_dev->odev) {
3013 pktgen_clear_counters(pkt_dev);
3014 pkt_dev->skb = NULL;
3015 pkt_dev->started_at = pkt_dev->next_tx = ktime_get();
3017 set_pkt_overhead(pkt_dev);
3019 strcpy(pkt_dev->result, "Starting");
3020 pkt_dev->running = 1; /* Cranke yeself! */
3021 started++;
3022 } else
3023 strcpy(pkt_dev->result, "Error starting");
3025 rcu_read_unlock();
3026 if (started)
3027 t->control &= ~(T_STOP);
3030 static void pktgen_stop_all_threads_ifs(struct pktgen_net *pn)
3032 struct pktgen_thread *t;
3034 func_enter();
3036 mutex_lock(&pktgen_thread_lock);
3038 list_for_each_entry(t, &pn->pktgen_threads, th_list)
3039 t->control |= T_STOP;
3041 mutex_unlock(&pktgen_thread_lock);
3044 static int thread_is_running(const struct pktgen_thread *t)
3046 const struct pktgen_dev *pkt_dev;
3048 rcu_read_lock();
3049 list_for_each_entry_rcu(pkt_dev, &t->if_list, list)
3050 if (pkt_dev->running) {
3051 rcu_read_unlock();
3052 return 1;
3054 rcu_read_unlock();
3055 return 0;
3058 static int pktgen_wait_thread_run(struct pktgen_thread *t)
3060 while (thread_is_running(t)) {
3062 /* note: 't' will still be around even after the unlock/lock
3063 * cycle because pktgen_thread threads are only cleared at
3064 * net exit
3066 mutex_unlock(&pktgen_thread_lock);
3067 msleep_interruptible(100);
3068 mutex_lock(&pktgen_thread_lock);
3070 if (signal_pending(current))
3071 goto signal;
3073 return 1;
3074 signal:
3075 return 0;
3078 static int pktgen_wait_all_threads_run(struct pktgen_net *pn)
3080 struct pktgen_thread *t;
3081 int sig = 1;
3083 /* prevent from racing with rmmod */
3084 if (!try_module_get(THIS_MODULE))
3085 return sig;
3087 mutex_lock(&pktgen_thread_lock);
3089 list_for_each_entry(t, &pn->pktgen_threads, th_list) {
3090 sig = pktgen_wait_thread_run(t);
3091 if (sig == 0)
3092 break;
3095 if (sig == 0)
3096 list_for_each_entry(t, &pn->pktgen_threads, th_list)
3097 t->control |= (T_STOP);
3099 mutex_unlock(&pktgen_thread_lock);
3100 module_put(THIS_MODULE);
3101 return sig;
3104 static void pktgen_run_all_threads(struct pktgen_net *pn)
3106 struct pktgen_thread *t;
3108 func_enter();
3110 mutex_lock(&pktgen_thread_lock);
3112 list_for_each_entry(t, &pn->pktgen_threads, th_list)
3113 t->control |= (T_RUN);
3115 mutex_unlock(&pktgen_thread_lock);
3117 /* Propagate thread->control */
3118 schedule_timeout_interruptible(msecs_to_jiffies(125));
3120 pktgen_wait_all_threads_run(pn);
3123 static void pktgen_reset_all_threads(struct pktgen_net *pn)
3125 struct pktgen_thread *t;
3127 func_enter();
3129 mutex_lock(&pktgen_thread_lock);
3131 list_for_each_entry(t, &pn->pktgen_threads, th_list)
3132 t->control |= (T_REMDEVALL);
3134 mutex_unlock(&pktgen_thread_lock);
3136 /* Propagate thread->control */
3137 schedule_timeout_interruptible(msecs_to_jiffies(125));
3139 pktgen_wait_all_threads_run(pn);
3142 static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
3144 __u64 bps, mbps, pps;
3145 char *p = pkt_dev->result;
3146 ktime_t elapsed = ktime_sub(pkt_dev->stopped_at,
3147 pkt_dev->started_at);
3148 ktime_t idle = ns_to_ktime(pkt_dev->idle_acc);
3150 p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
3151 (unsigned long long)ktime_to_us(elapsed),
3152 (unsigned long long)ktime_to_us(ktime_sub(elapsed, idle)),
3153 (unsigned long long)ktime_to_us(idle),
3154 (unsigned long long)pkt_dev->sofar,
3155 pkt_dev->cur_pkt_size, nr_frags);
3157 pps = div64_u64(pkt_dev->sofar * NSEC_PER_SEC,
3158 ktime_to_ns(elapsed));
3160 bps = pps * 8 * pkt_dev->cur_pkt_size;
3162 mbps = bps;
3163 do_div(mbps, 1000000);
3164 p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu",
3165 (unsigned long long)pps,
3166 (unsigned long long)mbps,
3167 (unsigned long long)bps,
3168 (unsigned long long)pkt_dev->errors);
3171 /* Set stopped-at timer, remove from running list, do counters & statistics */
3172 static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
3174 int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
3176 if (!pkt_dev->running) {
3177 pr_warn("interface: %s is already stopped\n",
3178 pkt_dev->odevname);
3179 return -EINVAL;
3182 pkt_dev->running = 0;
3183 kfree_skb(pkt_dev->skb);
3184 pkt_dev->skb = NULL;
3185 pkt_dev->stopped_at = ktime_get();
3187 show_results(pkt_dev, nr_frags);
3189 return 0;
3192 static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
3194 struct pktgen_dev *pkt_dev, *best = NULL;
3196 rcu_read_lock();
3197 list_for_each_entry_rcu(pkt_dev, &t->if_list, list) {
3198 if (!pkt_dev->running)
3199 continue;
3200 if (best == NULL)
3201 best = pkt_dev;
3202 else if (ktime_compare(pkt_dev->next_tx, best->next_tx) < 0)
3203 best = pkt_dev;
3205 rcu_read_unlock();
3207 return best;
3210 static void pktgen_stop(struct pktgen_thread *t)
3212 struct pktgen_dev *pkt_dev;
3214 func_enter();
3216 rcu_read_lock();
3218 list_for_each_entry_rcu(pkt_dev, &t->if_list, list) {
3219 pktgen_stop_device(pkt_dev);
3222 rcu_read_unlock();
3226 * one of our devices needs to be removed - find it
3227 * and remove it
3229 static void pktgen_rem_one_if(struct pktgen_thread *t)
3231 struct list_head *q, *n;
3232 struct pktgen_dev *cur;
3234 func_enter();
3236 list_for_each_safe(q, n, &t->if_list) {
3237 cur = list_entry(q, struct pktgen_dev, list);
3239 if (!cur->removal_mark)
3240 continue;
3242 kfree_skb(cur->skb);
3243 cur->skb = NULL;
3245 pktgen_remove_device(t, cur);
3247 break;
3251 static void pktgen_rem_all_ifs(struct pktgen_thread *t)
3253 struct list_head *q, *n;
3254 struct pktgen_dev *cur;
3256 func_enter();
3258 /* Remove all devices, free mem */
3260 list_for_each_safe(q, n, &t->if_list) {
3261 cur = list_entry(q, struct pktgen_dev, list);
3263 kfree_skb(cur->skb);
3264 cur->skb = NULL;
3266 pktgen_remove_device(t, cur);
3270 static void pktgen_rem_thread(struct pktgen_thread *t)
3272 /* Remove from the thread list */
3273 remove_proc_entry(t->tsk->comm, t->net->proc_dir);
3276 static void pktgen_resched(struct pktgen_dev *pkt_dev)
3278 ktime_t idle_start = ktime_get();
3279 schedule();
3280 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_get(), idle_start));
3283 static void pktgen_wait_for_skb(struct pktgen_dev *pkt_dev)
3285 ktime_t idle_start = ktime_get();
3287 while (refcount_read(&(pkt_dev->skb->users)) != 1) {
3288 if (signal_pending(current))
3289 break;
3291 if (need_resched())
3292 pktgen_resched(pkt_dev);
3293 else
3294 cpu_relax();
3296 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_get(), idle_start));
3299 static void pktgen_xmit(struct pktgen_dev *pkt_dev)
3301 unsigned int burst = READ_ONCE(pkt_dev->burst);
3302 struct net_device *odev = pkt_dev->odev;
3303 struct netdev_queue *txq;
3304 struct sk_buff *skb;
3305 int ret;
3307 /* If device is offline, then don't send */
3308 if (unlikely(!netif_running(odev) || !netif_carrier_ok(odev))) {
3309 pktgen_stop_device(pkt_dev);
3310 return;
3313 /* This is max DELAY, this has special meaning of
3314 * "never transmit"
3316 if (unlikely(pkt_dev->delay == ULLONG_MAX)) {
3317 pkt_dev->next_tx = ktime_add_ns(ktime_get(), ULONG_MAX);
3318 return;
3321 /* If no skb or clone count exhausted then get new one */
3322 if (!pkt_dev->skb || (pkt_dev->last_ok &&
3323 ++pkt_dev->clone_count >= pkt_dev->clone_skb)) {
3324 /* build a new pkt */
3325 kfree_skb(pkt_dev->skb);
3327 pkt_dev->skb = fill_packet(odev, pkt_dev);
3328 if (pkt_dev->skb == NULL) {
3329 pr_err("ERROR: couldn't allocate skb in fill_packet\n");
3330 schedule();
3331 pkt_dev->clone_count--; /* back out increment, OOM */
3332 return;
3334 pkt_dev->last_pkt_size = pkt_dev->skb->len;
3335 pkt_dev->clone_count = 0; /* reset counter */
3338 if (pkt_dev->delay && pkt_dev->last_ok)
3339 spin(pkt_dev, pkt_dev->next_tx);
3341 if (pkt_dev->xmit_mode == M_NETIF_RECEIVE) {
3342 skb = pkt_dev->skb;
3343 skb->protocol = eth_type_trans(skb, skb->dev);
3344 refcount_add(burst, &skb->users);
3345 local_bh_disable();
3346 do {
3347 ret = netif_receive_skb(skb);
3348 if (ret == NET_RX_DROP)
3349 pkt_dev->errors++;
3350 pkt_dev->sofar++;
3351 pkt_dev->seq_num++;
3352 if (refcount_read(&skb->users) != burst) {
3353 /* skb was queued by rps/rfs or taps,
3354 * so cannot reuse this skb
3356 WARN_ON(refcount_sub_and_test(burst - 1, &skb->users));
3357 /* get out of the loop and wait
3358 * until skb is consumed
3360 break;
3362 /* skb was 'freed' by stack, so clean few
3363 * bits and reuse it
3365 skb_reset_tc(skb);
3366 } while (--burst > 0);
3367 goto out; /* Skips xmit_mode M_START_XMIT */
3368 } else if (pkt_dev->xmit_mode == M_QUEUE_XMIT) {
3369 local_bh_disable();
3370 refcount_inc(&pkt_dev->skb->users);
3372 ret = dev_queue_xmit(pkt_dev->skb);
3373 switch (ret) {
3374 case NET_XMIT_SUCCESS:
3375 pkt_dev->sofar++;
3376 pkt_dev->seq_num++;
3377 pkt_dev->tx_bytes += pkt_dev->last_pkt_size;
3378 break;
3379 case NET_XMIT_DROP:
3380 case NET_XMIT_CN:
3381 /* These are all valid return codes for a qdisc but
3382 * indicate packets are being dropped or will likely
3383 * be dropped soon.
3385 case NETDEV_TX_BUSY:
3386 /* qdisc may call dev_hard_start_xmit directly in cases
3387 * where no queues exist e.g. loopback device, virtual
3388 * devices, etc. In this case we need to handle
3389 * NETDEV_TX_ codes.
3391 default:
3392 pkt_dev->errors++;
3393 net_info_ratelimited("%s xmit error: %d\n",
3394 pkt_dev->odevname, ret);
3395 break;
3397 goto out;
3400 txq = skb_get_tx_queue(odev, pkt_dev->skb);
3402 local_bh_disable();
3404 HARD_TX_LOCK(odev, txq, smp_processor_id());
3406 if (unlikely(netif_xmit_frozen_or_drv_stopped(txq))) {
3407 ret = NETDEV_TX_BUSY;
3408 pkt_dev->last_ok = 0;
3409 goto unlock;
3411 refcount_add(burst, &pkt_dev->skb->users);
3413 xmit_more:
3414 ret = netdev_start_xmit(pkt_dev->skb, odev, txq, --burst > 0);
3416 switch (ret) {
3417 case NETDEV_TX_OK:
3418 pkt_dev->last_ok = 1;
3419 pkt_dev->sofar++;
3420 pkt_dev->seq_num++;
3421 pkt_dev->tx_bytes += pkt_dev->last_pkt_size;
3422 if (burst > 0 && !netif_xmit_frozen_or_drv_stopped(txq))
3423 goto xmit_more;
3424 break;
3425 case NET_XMIT_DROP:
3426 case NET_XMIT_CN:
3427 /* skb has been consumed */
3428 pkt_dev->errors++;
3429 break;
3430 default: /* Drivers are not supposed to return other values! */
3431 net_info_ratelimited("%s xmit error: %d\n",
3432 pkt_dev->odevname, ret);
3433 pkt_dev->errors++;
3434 /* fall through */
3435 case NETDEV_TX_BUSY:
3436 /* Retry it next time */
3437 refcount_dec(&(pkt_dev->skb->users));
3438 pkt_dev->last_ok = 0;
3440 if (unlikely(burst))
3441 WARN_ON(refcount_sub_and_test(burst, &pkt_dev->skb->users));
3442 unlock:
3443 HARD_TX_UNLOCK(odev, txq);
3445 out:
3446 local_bh_enable();
3448 /* If pkt_dev->count is zero, then run forever */
3449 if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
3450 pktgen_wait_for_skb(pkt_dev);
3452 /* Done with this */
3453 pktgen_stop_device(pkt_dev);
3458 * Main loop of the thread goes here
3461 static int pktgen_thread_worker(void *arg)
3463 DEFINE_WAIT(wait);
3464 struct pktgen_thread *t = arg;
3465 struct pktgen_dev *pkt_dev = NULL;
3466 int cpu = t->cpu;
3468 BUG_ON(smp_processor_id() != cpu);
3470 init_waitqueue_head(&t->queue);
3471 complete(&t->start_done);
3473 pr_debug("starting pktgen/%d: pid=%d\n", cpu, task_pid_nr(current));
3475 set_freezable();
3477 while (!kthread_should_stop()) {
3478 pkt_dev = next_to_run(t);
3480 if (unlikely(!pkt_dev && t->control == 0)) {
3481 if (t->net->pktgen_exiting)
3482 break;
3483 wait_event_interruptible_timeout(t->queue,
3484 t->control != 0,
3485 HZ/10);
3486 try_to_freeze();
3487 continue;
3490 if (likely(pkt_dev)) {
3491 pktgen_xmit(pkt_dev);
3493 if (need_resched())
3494 pktgen_resched(pkt_dev);
3495 else
3496 cpu_relax();
3499 if (t->control & T_STOP) {
3500 pktgen_stop(t);
3501 t->control &= ~(T_STOP);
3504 if (t->control & T_RUN) {
3505 pktgen_run(t);
3506 t->control &= ~(T_RUN);
3509 if (t->control & T_REMDEVALL) {
3510 pktgen_rem_all_ifs(t);
3511 t->control &= ~(T_REMDEVALL);
3514 if (t->control & T_REMDEV) {
3515 pktgen_rem_one_if(t);
3516 t->control &= ~(T_REMDEV);
3519 try_to_freeze();
3522 pr_debug("%s stopping all device\n", t->tsk->comm);
3523 pktgen_stop(t);
3525 pr_debug("%s removing all device\n", t->tsk->comm);
3526 pktgen_rem_all_ifs(t);
3528 pr_debug("%s removing thread\n", t->tsk->comm);
3529 pktgen_rem_thread(t);
3531 return 0;
3534 static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
3535 const char *ifname, bool exact)
3537 struct pktgen_dev *p, *pkt_dev = NULL;
3538 size_t len = strlen(ifname);
3540 rcu_read_lock();
3541 list_for_each_entry_rcu(p, &t->if_list, list)
3542 if (strncmp(p->odevname, ifname, len) == 0) {
3543 if (p->odevname[len]) {
3544 if (exact || p->odevname[len] != '@')
3545 continue;
3547 pkt_dev = p;
3548 break;
3551 rcu_read_unlock();
3552 pr_debug("find_dev(%s) returning %p\n", ifname, pkt_dev);
3553 return pkt_dev;
3557 * Adds a dev at front of if_list.
3560 static int add_dev_to_thread(struct pktgen_thread *t,
3561 struct pktgen_dev *pkt_dev)
3563 int rv = 0;
3565 /* This function cannot be called concurrently, as its called
3566 * under pktgen_thread_lock mutex, but it can run from
3567 * userspace on another CPU than the kthread. The if_lock()
3568 * is used here to sync with concurrent instances of
3569 * _rem_dev_from_if_list() invoked via kthread, which is also
3570 * updating the if_list */
3571 if_lock(t);
3573 if (pkt_dev->pg_thread) {
3574 pr_err("ERROR: already assigned to a thread\n");
3575 rv = -EBUSY;
3576 goto out;
3579 pkt_dev->running = 0;
3580 pkt_dev->pg_thread = t;
3581 list_add_rcu(&pkt_dev->list, &t->if_list);
3583 out:
3584 if_unlock(t);
3585 return rv;
3588 /* Called under thread lock */
3590 static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
3592 struct pktgen_dev *pkt_dev;
3593 int err;
3594 int node = cpu_to_node(t->cpu);
3596 /* We don't allow a device to be on several threads */
3598 pkt_dev = __pktgen_NN_threads(t->net, ifname, FIND);
3599 if (pkt_dev) {
3600 pr_err("ERROR: interface already used\n");
3601 return -EBUSY;
3604 pkt_dev = kzalloc_node(sizeof(struct pktgen_dev), GFP_KERNEL, node);
3605 if (!pkt_dev)
3606 return -ENOMEM;
3608 strcpy(pkt_dev->odevname, ifname);
3609 pkt_dev->flows = vzalloc_node(array_size(MAX_CFLOWS,
3610 sizeof(struct flow_state)),
3611 node);
3612 if (pkt_dev->flows == NULL) {
3613 kfree(pkt_dev);
3614 return -ENOMEM;
3617 pkt_dev->removal_mark = 0;
3618 pkt_dev->nfrags = 0;
3619 pkt_dev->delay = pg_delay_d;
3620 pkt_dev->count = pg_count_d;
3621 pkt_dev->sofar = 0;
3622 pkt_dev->udp_src_min = 9; /* sink port */
3623 pkt_dev->udp_src_max = 9;
3624 pkt_dev->udp_dst_min = 9;
3625 pkt_dev->udp_dst_max = 9;
3626 pkt_dev->vlan_p = 0;
3627 pkt_dev->vlan_cfi = 0;
3628 pkt_dev->vlan_id = 0xffff;
3629 pkt_dev->svlan_p = 0;
3630 pkt_dev->svlan_cfi = 0;
3631 pkt_dev->svlan_id = 0xffff;
3632 pkt_dev->burst = 1;
3633 pkt_dev->node = NUMA_NO_NODE;
3635 err = pktgen_setup_dev(t->net, pkt_dev, ifname);
3636 if (err)
3637 goto out1;
3638 if (pkt_dev->odev->priv_flags & IFF_TX_SKB_SHARING)
3639 pkt_dev->clone_skb = pg_clone_skb_d;
3641 pkt_dev->entry = proc_create_data(ifname, 0600, t->net->proc_dir,
3642 &pktgen_if_fops, pkt_dev);
3643 if (!pkt_dev->entry) {
3644 pr_err("cannot create %s/%s procfs entry\n",
3645 PG_PROC_DIR, ifname);
3646 err = -EINVAL;
3647 goto out2;
3649 #ifdef CONFIG_XFRM
3650 pkt_dev->ipsmode = XFRM_MODE_TRANSPORT;
3651 pkt_dev->ipsproto = IPPROTO_ESP;
3653 /* xfrm tunnel mode needs additional dst to extract outter
3654 * ip header protocol/ttl/id field, here creat a phony one.
3655 * instead of looking for a valid rt, which definitely hurting
3656 * performance under such circumstance.
3658 pkt_dev->dstops.family = AF_INET;
3659 pkt_dev->xdst.u.dst.dev = pkt_dev->odev;
3660 dst_init_metrics(&pkt_dev->xdst.u.dst, pktgen_dst_metrics, false);
3661 pkt_dev->xdst.child = &pkt_dev->xdst.u.dst;
3662 pkt_dev->xdst.u.dst.ops = &pkt_dev->dstops;
3663 #endif
3665 return add_dev_to_thread(t, pkt_dev);
3666 out2:
3667 dev_put(pkt_dev->odev);
3668 out1:
3669 #ifdef CONFIG_XFRM
3670 free_SAs(pkt_dev);
3671 #endif
3672 vfree(pkt_dev->flows);
3673 kfree(pkt_dev);
3674 return err;
3677 static int __net_init pktgen_create_thread(int cpu, struct pktgen_net *pn)
3679 struct pktgen_thread *t;
3680 struct proc_dir_entry *pe;
3681 struct task_struct *p;
3683 t = kzalloc_node(sizeof(struct pktgen_thread), GFP_KERNEL,
3684 cpu_to_node(cpu));
3685 if (!t) {
3686 pr_err("ERROR: out of memory, can't create new thread\n");
3687 return -ENOMEM;
3690 mutex_init(&t->if_lock);
3691 t->cpu = cpu;
3693 INIT_LIST_HEAD(&t->if_list);
3695 list_add_tail(&t->th_list, &pn->pktgen_threads);
3696 init_completion(&t->start_done);
3698 p = kthread_create_on_node(pktgen_thread_worker,
3700 cpu_to_node(cpu),
3701 "kpktgend_%d", cpu);
3702 if (IS_ERR(p)) {
3703 pr_err("kernel_thread() failed for cpu %d\n", t->cpu);
3704 list_del(&t->th_list);
3705 kfree(t);
3706 return PTR_ERR(p);
3708 kthread_bind(p, cpu);
3709 t->tsk = p;
3711 pe = proc_create_data(t->tsk->comm, 0600, pn->proc_dir,
3712 &pktgen_thread_fops, t);
3713 if (!pe) {
3714 pr_err("cannot create %s/%s procfs entry\n",
3715 PG_PROC_DIR, t->tsk->comm);
3716 kthread_stop(p);
3717 list_del(&t->th_list);
3718 kfree(t);
3719 return -EINVAL;
3722 t->net = pn;
3723 get_task_struct(p);
3724 wake_up_process(p);
3725 wait_for_completion(&t->start_done);
3727 return 0;
3731 * Removes a device from the thread if_list.
3733 static void _rem_dev_from_if_list(struct pktgen_thread *t,
3734 struct pktgen_dev *pkt_dev)
3736 struct list_head *q, *n;
3737 struct pktgen_dev *p;
3739 if_lock(t);
3740 list_for_each_safe(q, n, &t->if_list) {
3741 p = list_entry(q, struct pktgen_dev, list);
3742 if (p == pkt_dev)
3743 list_del_rcu(&p->list);
3745 if_unlock(t);
3748 static int pktgen_remove_device(struct pktgen_thread *t,
3749 struct pktgen_dev *pkt_dev)
3751 pr_debug("remove_device pkt_dev=%p\n", pkt_dev);
3753 if (pkt_dev->running) {
3754 pr_warn("WARNING: trying to remove a running interface, stopping it now\n");
3755 pktgen_stop_device(pkt_dev);
3758 /* Dis-associate from the interface */
3760 if (pkt_dev->odev) {
3761 dev_put(pkt_dev->odev);
3762 pkt_dev->odev = NULL;
3765 /* Remove proc before if_list entry, because add_device uses
3766 * list to determine if interface already exist, avoid race
3767 * with proc_create_data() */
3768 proc_remove(pkt_dev->entry);
3770 /* And update the thread if_list */
3771 _rem_dev_from_if_list(t, pkt_dev);
3773 #ifdef CONFIG_XFRM
3774 free_SAs(pkt_dev);
3775 #endif
3776 vfree(pkt_dev->flows);
3777 if (pkt_dev->page)
3778 put_page(pkt_dev->page);
3779 kfree_rcu(pkt_dev, rcu);
3780 return 0;
3783 static int __net_init pg_net_init(struct net *net)
3785 struct pktgen_net *pn = net_generic(net, pg_net_id);
3786 struct proc_dir_entry *pe;
3787 int cpu, ret = 0;
3789 pn->net = net;
3790 INIT_LIST_HEAD(&pn->pktgen_threads);
3791 pn->pktgen_exiting = false;
3792 pn->proc_dir = proc_mkdir(PG_PROC_DIR, pn->net->proc_net);
3793 if (!pn->proc_dir) {
3794 pr_warn("cannot create /proc/net/%s\n", PG_PROC_DIR);
3795 return -ENODEV;
3797 pe = proc_create(PGCTRL, 0600, pn->proc_dir, &pktgen_fops);
3798 if (pe == NULL) {
3799 pr_err("cannot create %s procfs entry\n", PGCTRL);
3800 ret = -EINVAL;
3801 goto remove;
3804 for_each_online_cpu(cpu) {
3805 int err;
3807 err = pktgen_create_thread(cpu, pn);
3808 if (err)
3809 pr_warn("Cannot create thread for cpu %d (%d)\n",
3810 cpu, err);
3813 if (list_empty(&pn->pktgen_threads)) {
3814 pr_err("Initialization failed for all threads\n");
3815 ret = -ENODEV;
3816 goto remove_entry;
3819 return 0;
3821 remove_entry:
3822 remove_proc_entry(PGCTRL, pn->proc_dir);
3823 remove:
3824 remove_proc_entry(PG_PROC_DIR, pn->net->proc_net);
3825 return ret;
3828 static void __net_exit pg_net_exit(struct net *net)
3830 struct pktgen_net *pn = net_generic(net, pg_net_id);
3831 struct pktgen_thread *t;
3832 struct list_head *q, *n;
3833 LIST_HEAD(list);
3835 /* Stop all interfaces & threads */
3836 pn->pktgen_exiting = true;
3838 mutex_lock(&pktgen_thread_lock);
3839 list_splice_init(&pn->pktgen_threads, &list);
3840 mutex_unlock(&pktgen_thread_lock);
3842 list_for_each_safe(q, n, &list) {
3843 t = list_entry(q, struct pktgen_thread, th_list);
3844 list_del(&t->th_list);
3845 kthread_stop(t->tsk);
3846 put_task_struct(t->tsk);
3847 kfree(t);
3850 remove_proc_entry(PGCTRL, pn->proc_dir);
3851 remove_proc_entry(PG_PROC_DIR, pn->net->proc_net);
3854 static struct pernet_operations pg_net_ops = {
3855 .init = pg_net_init,
3856 .exit = pg_net_exit,
3857 .id = &pg_net_id,
3858 .size = sizeof(struct pktgen_net),
3861 static int __init pg_init(void)
3863 int ret = 0;
3865 pr_info("%s", version);
3866 ret = register_pernet_subsys(&pg_net_ops);
3867 if (ret)
3868 return ret;
3869 ret = register_netdevice_notifier(&pktgen_notifier_block);
3870 if (ret)
3871 unregister_pernet_subsys(&pg_net_ops);
3873 return ret;
3876 static void __exit pg_cleanup(void)
3878 unregister_netdevice_notifier(&pktgen_notifier_block);
3879 unregister_pernet_subsys(&pg_net_ops);
3880 /* Don't need rcu_barrier() due to use of kfree_rcu() */
3883 module_init(pg_init);
3884 module_exit(pg_cleanup);
3886 MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se>");
3887 MODULE_DESCRIPTION("Packet Generator tool");
3888 MODULE_LICENSE("GPL");
3889 MODULE_VERSION(VERSION);
3890 module_param(pg_count_d, int, 0);
3891 MODULE_PARM_DESC(pg_count_d, "Default number of packets to inject");
3892 module_param(pg_delay_d, int, 0);
3893 MODULE_PARM_DESC(pg_delay_d, "Default delay between packets (nanoseconds)");
3894 module_param(pg_clone_skb_d, int, 0);
3895 MODULE_PARM_DESC(pg_clone_skb_d, "Default number of copies of the same packet");
3896 module_param(debug, int, 0);
3897 MODULE_PARM_DESC(debug, "Enable debugging of pktgen module");