i2c: designware-master: use core to detect 'no zero length' quirk
[linux/fpc-iii.git] / net / core / pktgen.c
blob49368e21d228c3f0bd6684c8831fc1e4398d56b1
1 /*
2 * Authors:
3 * Copyright 2001, 2002 by Robert Olsson <robert.olsson@its.uu.se>
4 * Uppsala University and
5 * Swedish University of Agricultural Sciences
7 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
8 * Ben Greear <greearb@candelatech.com>
9 * Jens Låås <jens.laas@data.slu.se>
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version
14 * 2 of the License, or (at your option) any later version.
17 * A tool for loading the network with preconfigurated packets.
18 * The tool is implemented as a linux module. Parameters are output
19 * device, delay (to hard_xmit), number of packets, and whether
20 * to use multiple SKBs or just the same one.
21 * pktgen uses the installed interface's output routine.
23 * Additional hacking by:
25 * Jens.Laas@data.slu.se
26 * Improved by ANK. 010120.
27 * Improved by ANK even more. 010212.
28 * MAC address typo fixed. 010417 --ro
29 * Integrated. 020301 --DaveM
30 * Added multiskb option 020301 --DaveM
31 * Scaling of results. 020417--sigurdur@linpro.no
32 * Significant re-work of the module:
33 * * Convert to threaded model to more efficiently be able to transmit
34 * and receive on multiple interfaces at once.
35 * * Converted many counters to __u64 to allow longer runs.
36 * * Allow configuration of ranges, like min/max IP address, MACs,
37 * and UDP-ports, for both source and destination, and can
38 * set to use a random distribution or sequentially walk the range.
39 * * Can now change most values after starting.
40 * * Place 12-byte packet in UDP payload with magic number,
41 * sequence number, and timestamp.
42 * * Add receiver code that detects dropped pkts, re-ordered pkts, and
43 * latencies (with micro-second) precision.
44 * * Add IOCTL interface to easily get counters & configuration.
45 * --Ben Greear <greearb@candelatech.com>
47 * Renamed multiskb to clone_skb and cleaned up sending core for two distinct
48 * skb modes. A clone_skb=0 mode for Ben "ranges" work and a clone_skb != 0
49 * as a "fastpath" with a configurable number of clones after alloc's.
50 * clone_skb=0 means all packets are allocated this also means ranges time
51 * stamps etc can be used. clone_skb=100 means 1 malloc is followed by 100
52 * clones.
54 * Also moved to /proc/net/pktgen/
55 * --ro
57 * Sept 10: Fixed threading/locking. Lots of bone-headed and more clever
58 * mistakes. Also merged in DaveM's patch in the -pre6 patch.
59 * --Ben Greear <greearb@candelatech.com>
61 * Integrated to 2.5.x 021029 --Lucio Maciel (luciomaciel@zipmail.com.br)
64 * 021124 Finished major redesign and rewrite for new functionality.
65 * See Documentation/networking/pktgen.txt for how to use this.
67 * The new operation:
68 * For each CPU one thread/process is created at start. This process checks
69 * for running devices in the if_list and sends packets until count is 0 it
70 * also the thread checks the thread->control which is used for inter-process
71 * communication. controlling process "posts" operations to the threads this
72 * way.
73 * The if_list is RCU protected, and the if_lock remains to protect updating
74 * of if_list, from "add_device" as it invoked from userspace (via proc write).
76 * By design there should only be *one* "controlling" process. In practice
77 * multiple write accesses gives unpredictable result. Understood by "write"
78 * to /proc gives result code thats should be read be the "writer".
79 * For practical use this should be no problem.
81 * Note when adding devices to a specific CPU there good idea to also assign
82 * /proc/irq/XX/smp_affinity so TX-interrupts gets bound to the same CPU.
83 * --ro
85 * Fix refcount off by one if first packet fails, potential null deref,
86 * memleak 030710- KJP
88 * First "ranges" functionality for ipv6 030726 --ro
90 * Included flow support. 030802 ANK.
92 * Fixed unaligned access on IA-64 Grant Grundler <grundler@parisc-linux.org>
94 * Remove if fix from added Harald Welte <laforge@netfilter.org> 040419
95 * ia64 compilation fix from Aron Griffis <aron@hp.com> 040604
97 * New xmit() return, do_div and misc clean up by Stephen Hemminger
98 * <shemminger@osdl.org> 040923
100 * Randy Dunlap fixed u64 printk compiler warning
102 * Remove FCS from BW calculation. Lennert Buytenhek <buytenh@wantstofly.org>
103 * New time handling. Lennert Buytenhek <buytenh@wantstofly.org> 041213
105 * Corrections from Nikolai Malykh (nmalykh@bilim.com)
106 * Removed unused flags F_SET_SRCMAC & F_SET_SRCIP 041230
108 * interruptible_sleep_on_timeout() replaced Nishanth Aravamudan <nacc@us.ibm.com>
109 * 050103
111 * MPLS support by Steven Whitehouse <steve@chygwyn.com>
113 * 802.1Q/Q-in-Q support by Francesco Fondelli (FF) <francesco.fondelli@gmail.com>
115 * Fixed src_mac command to set source mac of packet to value specified in
116 * command by Adit Ranadive <adit.262@gmail.com>
120 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
122 #include <linux/sys.h>
123 #include <linux/types.h>
124 #include <linux/module.h>
125 #include <linux/moduleparam.h>
126 #include <linux/kernel.h>
127 #include <linux/mutex.h>
128 #include <linux/sched.h>
129 #include <linux/slab.h>
130 #include <linux/vmalloc.h>
131 #include <linux/unistd.h>
132 #include <linux/string.h>
133 #include <linux/ptrace.h>
134 #include <linux/errno.h>
135 #include <linux/ioport.h>
136 #include <linux/interrupt.h>
137 #include <linux/capability.h>
138 #include <linux/hrtimer.h>
139 #include <linux/freezer.h>
140 #include <linux/delay.h>
141 #include <linux/timer.h>
142 #include <linux/list.h>
143 #include <linux/init.h>
144 #include <linux/skbuff.h>
145 #include <linux/netdevice.h>
146 #include <linux/inet.h>
147 #include <linux/inetdevice.h>
148 #include <linux/rtnetlink.h>
149 #include <linux/if_arp.h>
150 #include <linux/if_vlan.h>
151 #include <linux/in.h>
152 #include <linux/ip.h>
153 #include <linux/ipv6.h>
154 #include <linux/udp.h>
155 #include <linux/proc_fs.h>
156 #include <linux/seq_file.h>
157 #include <linux/wait.h>
158 #include <linux/etherdevice.h>
159 #include <linux/kthread.h>
160 #include <linux/prefetch.h>
161 #include <net/net_namespace.h>
162 #include <net/checksum.h>
163 #include <net/ipv6.h>
164 #include <net/udp.h>
165 #include <net/ip6_checksum.h>
166 #include <net/addrconf.h>
167 #ifdef CONFIG_XFRM
168 #include <net/xfrm.h>
169 #endif
170 #include <net/netns/generic.h>
171 #include <asm/byteorder.h>
172 #include <linux/rcupdate.h>
173 #include <linux/bitops.h>
174 #include <linux/io.h>
175 #include <linux/timex.h>
176 #include <linux/uaccess.h>
177 #include <asm/dma.h>
178 #include <asm/div64.h> /* do_div */
180 #define VERSION "2.75"
181 #define IP_NAME_SZ 32
182 #define MAX_MPLS_LABELS 16 /* This is the max label stack depth */
183 #define MPLS_STACK_BOTTOM htonl(0x00000100)
185 #define func_enter() pr_debug("entering %s\n", __func__);
187 #define PKT_FLAGS \
188 pf(IPV6) /* Interface in IPV6 Mode */ \
189 pf(IPSRC_RND) /* IP-Src Random */ \
190 pf(IPDST_RND) /* IP-Dst Random */ \
191 pf(TXSIZE_RND) /* Transmit size is random */ \
192 pf(UDPSRC_RND) /* UDP-Src Random */ \
193 pf(UDPDST_RND) /* UDP-Dst Random */ \
194 pf(UDPCSUM) /* Include UDP checksum */ \
195 pf(NO_TIMESTAMP) /* Don't timestamp packets (default TS) */ \
196 pf(MPLS_RND) /* Random MPLS labels */ \
197 pf(QUEUE_MAP_RND) /* queue map Random */ \
198 pf(QUEUE_MAP_CPU) /* queue map mirrors smp_processor_id() */ \
199 pf(FLOW_SEQ) /* Sequential flows */ \
200 pf(IPSEC) /* ipsec on for flows */ \
201 pf(MACSRC_RND) /* MAC-Src Random */ \
202 pf(MACDST_RND) /* MAC-Dst Random */ \
203 pf(VID_RND) /* Random VLAN ID */ \
204 pf(SVID_RND) /* Random SVLAN ID */ \
205 pf(NODE) /* Node memory alloc*/ \
207 #define pf(flag) flag##_SHIFT,
208 enum pkt_flags {
209 PKT_FLAGS
211 #undef pf
213 /* Device flag bits */
214 #define pf(flag) static const __u32 F_##flag = (1<<flag##_SHIFT);
215 PKT_FLAGS
216 #undef pf
218 #define pf(flag) __stringify(flag),
219 static char *pkt_flag_names[] = {
220 PKT_FLAGS
222 #undef pf
224 #define NR_PKT_FLAGS ARRAY_SIZE(pkt_flag_names)
226 /* Thread control flag bits */
227 #define T_STOP (1<<0) /* Stop run */
228 #define T_RUN (1<<1) /* Start run */
229 #define T_REMDEVALL (1<<2) /* Remove all devs */
230 #define T_REMDEV (1<<3) /* Remove one dev */
232 /* Xmit modes */
233 #define M_START_XMIT 0 /* Default normal TX */
234 #define M_NETIF_RECEIVE 1 /* Inject packets into stack */
235 #define M_QUEUE_XMIT 2 /* Inject packet into qdisc */
237 /* If lock -- protects updating of if_list */
238 #define if_lock(t) mutex_lock(&(t->if_lock));
239 #define if_unlock(t) mutex_unlock(&(t->if_lock));
241 /* Used to help with determining the pkts on receive */
242 #define PKTGEN_MAGIC 0xbe9be955
243 #define PG_PROC_DIR "pktgen"
244 #define PGCTRL "pgctrl"
246 #define MAX_CFLOWS 65536
248 #define VLAN_TAG_SIZE(x) ((x)->vlan_id == 0xffff ? 0 : 4)
249 #define SVLAN_TAG_SIZE(x) ((x)->svlan_id == 0xffff ? 0 : 4)
251 struct flow_state {
252 __be32 cur_daddr;
253 int count;
254 #ifdef CONFIG_XFRM
255 struct xfrm_state *x;
256 #endif
257 __u32 flags;
260 /* flow flag bits */
261 #define F_INIT (1<<0) /* flow has been initialized */
263 struct pktgen_dev {
265 * Try to keep frequent/infrequent used vars. separated.
267 struct proc_dir_entry *entry; /* proc file */
268 struct pktgen_thread *pg_thread;/* the owner */
269 struct list_head list; /* chaining in the thread's run-queue */
270 struct rcu_head rcu; /* freed by RCU */
272 int running; /* if false, the test will stop */
274 /* If min != max, then we will either do a linear iteration, or
275 * we will do a random selection from within the range.
277 __u32 flags;
278 int xmit_mode;
279 int min_pkt_size;
280 int max_pkt_size;
281 int pkt_overhead; /* overhead for MPLS, VLANs, IPSEC etc */
282 int nfrags;
283 int removal_mark; /* non-zero => the device is marked for
284 * removal by worker thread */
286 struct page *page;
287 u64 delay; /* nano-seconds */
289 __u64 count; /* Default No packets to send */
290 __u64 sofar; /* How many pkts we've sent so far */
291 __u64 tx_bytes; /* How many bytes we've transmitted */
292 __u64 errors; /* Errors when trying to transmit, */
294 /* runtime counters relating to clone_skb */
296 __u32 clone_count;
297 int last_ok; /* Was last skb sent?
298 * Or a failed transmit of some sort?
299 * This will keep sequence numbers in order
301 ktime_t next_tx;
302 ktime_t started_at;
303 ktime_t stopped_at;
304 u64 idle_acc; /* nano-seconds */
306 __u32 seq_num;
308 int clone_skb; /*
309 * Use multiple SKBs during packet gen.
310 * If this number is greater than 1, then
311 * that many copies of the same packet will be
312 * sent before a new packet is allocated.
313 * If you want to send 1024 identical packets
314 * before creating a new packet,
315 * set clone_skb to 1024.
318 char dst_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
319 char dst_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
320 char src_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
321 char src_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
323 struct in6_addr in6_saddr;
324 struct in6_addr in6_daddr;
325 struct in6_addr cur_in6_daddr;
326 struct in6_addr cur_in6_saddr;
327 /* For ranges */
328 struct in6_addr min_in6_daddr;
329 struct in6_addr max_in6_daddr;
330 struct in6_addr min_in6_saddr;
331 struct in6_addr max_in6_saddr;
333 /* If we're doing ranges, random or incremental, then this
334 * defines the min/max for those ranges.
336 __be32 saddr_min; /* inclusive, source IP address */
337 __be32 saddr_max; /* exclusive, source IP address */
338 __be32 daddr_min; /* inclusive, dest IP address */
339 __be32 daddr_max; /* exclusive, dest IP address */
341 __u16 udp_src_min; /* inclusive, source UDP port */
342 __u16 udp_src_max; /* exclusive, source UDP port */
343 __u16 udp_dst_min; /* inclusive, dest UDP port */
344 __u16 udp_dst_max; /* exclusive, dest UDP port */
346 /* DSCP + ECN */
347 __u8 tos; /* six MSB of (former) IPv4 TOS
348 are for dscp codepoint */
349 __u8 traffic_class; /* ditto for the (former) Traffic Class in IPv6
350 (see RFC 3260, sec. 4) */
352 /* MPLS */
353 unsigned int nr_labels; /* Depth of stack, 0 = no MPLS */
354 __be32 labels[MAX_MPLS_LABELS];
356 /* VLAN/SVLAN (802.1Q/Q-in-Q) */
357 __u8 vlan_p;
358 __u8 vlan_cfi;
359 __u16 vlan_id; /* 0xffff means no vlan tag */
361 __u8 svlan_p;
362 __u8 svlan_cfi;
363 __u16 svlan_id; /* 0xffff means no svlan tag */
365 __u32 src_mac_count; /* How many MACs to iterate through */
366 __u32 dst_mac_count; /* How many MACs to iterate through */
368 unsigned char dst_mac[ETH_ALEN];
369 unsigned char src_mac[ETH_ALEN];
371 __u32 cur_dst_mac_offset;
372 __u32 cur_src_mac_offset;
373 __be32 cur_saddr;
374 __be32 cur_daddr;
375 __u16 ip_id;
376 __u16 cur_udp_dst;
377 __u16 cur_udp_src;
378 __u16 cur_queue_map;
379 __u32 cur_pkt_size;
380 __u32 last_pkt_size;
382 __u8 hh[14];
383 /* = {
384 0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
386 We fill in SRC address later
387 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
388 0x08, 0x00
391 __u16 pad; /* pad out the hh struct to an even 16 bytes */
393 struct sk_buff *skb; /* skb we are to transmit next, used for when we
394 * are transmitting the same one multiple times
396 struct net_device *odev; /* The out-going device.
397 * Note that the device should have it's
398 * pg_info pointer pointing back to this
399 * device.
400 * Set when the user specifies the out-going
401 * device name (not when the inject is
402 * started as it used to do.)
404 char odevname[32];
405 struct flow_state *flows;
406 unsigned int cflows; /* Concurrent flows (config) */
407 unsigned int lflow; /* Flow length (config) */
408 unsigned int nflows; /* accumulated flows (stats) */
409 unsigned int curfl; /* current sequenced flow (state)*/
411 u16 queue_map_min;
412 u16 queue_map_max;
413 __u32 skb_priority; /* skb priority field */
414 unsigned int burst; /* number of duplicated packets to burst */
415 int node; /* Memory node */
417 #ifdef CONFIG_XFRM
418 __u8 ipsmode; /* IPSEC mode (config) */
419 __u8 ipsproto; /* IPSEC type (config) */
420 __u32 spi;
421 struct xfrm_dst xdst;
422 struct dst_ops dstops;
423 #endif
424 char result[512];
427 struct pktgen_hdr {
428 __be32 pgh_magic;
429 __be32 seq_num;
430 __be32 tv_sec;
431 __be32 tv_usec;
435 static unsigned int pg_net_id __read_mostly;
437 struct pktgen_net {
438 struct net *net;
439 struct proc_dir_entry *proc_dir;
440 struct list_head pktgen_threads;
441 bool pktgen_exiting;
444 struct pktgen_thread {
445 struct mutex if_lock; /* for list of devices */
446 struct list_head if_list; /* All device here */
447 struct list_head th_list;
448 struct task_struct *tsk;
449 char result[512];
451 /* Field for thread to receive "posted" events terminate,
452 stop ifs etc. */
454 u32 control;
455 int cpu;
457 wait_queue_head_t queue;
458 struct completion start_done;
459 struct pktgen_net *net;
462 #define REMOVE 1
463 #define FIND 0
465 static const char version[] =
466 "Packet Generator for packet performance testing. "
467 "Version: " VERSION "\n";
469 static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *i);
470 static int pktgen_add_device(struct pktgen_thread *t, const char *ifname);
471 static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
472 const char *ifname, bool exact);
473 static int pktgen_device_event(struct notifier_block *, unsigned long, void *);
474 static void pktgen_run_all_threads(struct pktgen_net *pn);
475 static void pktgen_reset_all_threads(struct pktgen_net *pn);
476 static void pktgen_stop_all_threads_ifs(struct pktgen_net *pn);
478 static void pktgen_stop(struct pktgen_thread *t);
479 static void pktgen_clear_counters(struct pktgen_dev *pkt_dev);
481 /* Module parameters, defaults. */
482 static int pg_count_d __read_mostly = 1000;
483 static int pg_delay_d __read_mostly;
484 static int pg_clone_skb_d __read_mostly;
485 static int debug __read_mostly;
487 static DEFINE_MUTEX(pktgen_thread_lock);
489 static struct notifier_block pktgen_notifier_block = {
490 .notifier_call = pktgen_device_event,
494 * /proc handling functions
498 static int pgctrl_show(struct seq_file *seq, void *v)
500 seq_puts(seq, version);
501 return 0;
504 static ssize_t pgctrl_write(struct file *file, const char __user *buf,
505 size_t count, loff_t *ppos)
507 char data[128];
508 struct pktgen_net *pn = net_generic(current->nsproxy->net_ns, pg_net_id);
510 if (!capable(CAP_NET_ADMIN))
511 return -EPERM;
513 if (count == 0)
514 return -EINVAL;
516 if (count > sizeof(data))
517 count = sizeof(data);
519 if (copy_from_user(data, buf, count))
520 return -EFAULT;
522 data[count - 1] = 0; /* Strip trailing '\n' and terminate string */
524 if (!strcmp(data, "stop"))
525 pktgen_stop_all_threads_ifs(pn);
527 else if (!strcmp(data, "start"))
528 pktgen_run_all_threads(pn);
530 else if (!strcmp(data, "reset"))
531 pktgen_reset_all_threads(pn);
533 else
534 return -EINVAL;
536 return count;
539 static int pgctrl_open(struct inode *inode, struct file *file)
541 return single_open(file, pgctrl_show, PDE_DATA(inode));
544 static const struct file_operations pktgen_fops = {
545 .open = pgctrl_open,
546 .read = seq_read,
547 .llseek = seq_lseek,
548 .write = pgctrl_write,
549 .release = single_release,
552 static int pktgen_if_show(struct seq_file *seq, void *v)
554 const struct pktgen_dev *pkt_dev = seq->private;
555 ktime_t stopped;
556 unsigned int i;
557 u64 idle;
559 seq_printf(seq,
560 "Params: count %llu min_pkt_size: %u max_pkt_size: %u\n",
561 (unsigned long long)pkt_dev->count, pkt_dev->min_pkt_size,
562 pkt_dev->max_pkt_size);
564 seq_printf(seq,
565 " frags: %d delay: %llu clone_skb: %d ifname: %s\n",
566 pkt_dev->nfrags, (unsigned long long) pkt_dev->delay,
567 pkt_dev->clone_skb, pkt_dev->odevname);
569 seq_printf(seq, " flows: %u flowlen: %u\n", pkt_dev->cflows,
570 pkt_dev->lflow);
572 seq_printf(seq,
573 " queue_map_min: %u queue_map_max: %u\n",
574 pkt_dev->queue_map_min,
575 pkt_dev->queue_map_max);
577 if (pkt_dev->skb_priority)
578 seq_printf(seq, " skb_priority: %u\n",
579 pkt_dev->skb_priority);
581 if (pkt_dev->flags & F_IPV6) {
582 seq_printf(seq,
583 " saddr: %pI6c min_saddr: %pI6c max_saddr: %pI6c\n"
584 " daddr: %pI6c min_daddr: %pI6c max_daddr: %pI6c\n",
585 &pkt_dev->in6_saddr,
586 &pkt_dev->min_in6_saddr, &pkt_dev->max_in6_saddr,
587 &pkt_dev->in6_daddr,
588 &pkt_dev->min_in6_daddr, &pkt_dev->max_in6_daddr);
589 } else {
590 seq_printf(seq,
591 " dst_min: %s dst_max: %s\n",
592 pkt_dev->dst_min, pkt_dev->dst_max);
593 seq_printf(seq,
594 " src_min: %s src_max: %s\n",
595 pkt_dev->src_min, pkt_dev->src_max);
598 seq_puts(seq, " src_mac: ");
600 seq_printf(seq, "%pM ",
601 is_zero_ether_addr(pkt_dev->src_mac) ?
602 pkt_dev->odev->dev_addr : pkt_dev->src_mac);
604 seq_puts(seq, "dst_mac: ");
605 seq_printf(seq, "%pM\n", pkt_dev->dst_mac);
607 seq_printf(seq,
608 " udp_src_min: %d udp_src_max: %d"
609 " udp_dst_min: %d udp_dst_max: %d\n",
610 pkt_dev->udp_src_min, pkt_dev->udp_src_max,
611 pkt_dev->udp_dst_min, pkt_dev->udp_dst_max);
613 seq_printf(seq,
614 " src_mac_count: %d dst_mac_count: %d\n",
615 pkt_dev->src_mac_count, pkt_dev->dst_mac_count);
617 if (pkt_dev->nr_labels) {
618 seq_puts(seq, " mpls: ");
619 for (i = 0; i < pkt_dev->nr_labels; i++)
620 seq_printf(seq, "%08x%s", ntohl(pkt_dev->labels[i]),
621 i == pkt_dev->nr_labels-1 ? "\n" : ", ");
624 if (pkt_dev->vlan_id != 0xffff)
625 seq_printf(seq, " vlan_id: %u vlan_p: %u vlan_cfi: %u\n",
626 pkt_dev->vlan_id, pkt_dev->vlan_p,
627 pkt_dev->vlan_cfi);
629 if (pkt_dev->svlan_id != 0xffff)
630 seq_printf(seq, " svlan_id: %u vlan_p: %u vlan_cfi: %u\n",
631 pkt_dev->svlan_id, pkt_dev->svlan_p,
632 pkt_dev->svlan_cfi);
634 if (pkt_dev->tos)
635 seq_printf(seq, " tos: 0x%02x\n", pkt_dev->tos);
637 if (pkt_dev->traffic_class)
638 seq_printf(seq, " traffic_class: 0x%02x\n", pkt_dev->traffic_class);
640 if (pkt_dev->burst > 1)
641 seq_printf(seq, " burst: %d\n", pkt_dev->burst);
643 if (pkt_dev->node >= 0)
644 seq_printf(seq, " node: %d\n", pkt_dev->node);
646 if (pkt_dev->xmit_mode == M_NETIF_RECEIVE)
647 seq_puts(seq, " xmit_mode: netif_receive\n");
648 else if (pkt_dev->xmit_mode == M_QUEUE_XMIT)
649 seq_puts(seq, " xmit_mode: xmit_queue\n");
651 seq_puts(seq, " Flags: ");
653 for (i = 0; i < NR_PKT_FLAGS; i++) {
654 if (i == F_FLOW_SEQ)
655 if (!pkt_dev->cflows)
656 continue;
658 if (pkt_dev->flags & (1 << i))
659 seq_printf(seq, "%s ", pkt_flag_names[i]);
660 else if (i == F_FLOW_SEQ)
661 seq_puts(seq, "FLOW_RND ");
663 #ifdef CONFIG_XFRM
664 if (i == F_IPSEC && pkt_dev->spi)
665 seq_printf(seq, "spi:%u", pkt_dev->spi);
666 #endif
669 seq_puts(seq, "\n");
671 /* not really stopped, more like last-running-at */
672 stopped = pkt_dev->running ? ktime_get() : pkt_dev->stopped_at;
673 idle = pkt_dev->idle_acc;
674 do_div(idle, NSEC_PER_USEC);
676 seq_printf(seq,
677 "Current:\n pkts-sofar: %llu errors: %llu\n",
678 (unsigned long long)pkt_dev->sofar,
679 (unsigned long long)pkt_dev->errors);
681 seq_printf(seq,
682 " started: %lluus stopped: %lluus idle: %lluus\n",
683 (unsigned long long) ktime_to_us(pkt_dev->started_at),
684 (unsigned long long) ktime_to_us(stopped),
685 (unsigned long long) idle);
687 seq_printf(seq,
688 " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n",
689 pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset,
690 pkt_dev->cur_src_mac_offset);
692 if (pkt_dev->flags & F_IPV6) {
693 seq_printf(seq, " cur_saddr: %pI6c cur_daddr: %pI6c\n",
694 &pkt_dev->cur_in6_saddr,
695 &pkt_dev->cur_in6_daddr);
696 } else
697 seq_printf(seq, " cur_saddr: %pI4 cur_daddr: %pI4\n",
698 &pkt_dev->cur_saddr, &pkt_dev->cur_daddr);
700 seq_printf(seq, " cur_udp_dst: %d cur_udp_src: %d\n",
701 pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src);
703 seq_printf(seq, " cur_queue_map: %u\n", pkt_dev->cur_queue_map);
705 seq_printf(seq, " flows: %u\n", pkt_dev->nflows);
707 if (pkt_dev->result[0])
708 seq_printf(seq, "Result: %s\n", pkt_dev->result);
709 else
710 seq_puts(seq, "Result: Idle\n");
712 return 0;
716 static int hex32_arg(const char __user *user_buffer, unsigned long maxlen,
717 __u32 *num)
719 int i = 0;
720 *num = 0;
722 for (; i < maxlen; i++) {
723 int value;
724 char c;
725 *num <<= 4;
726 if (get_user(c, &user_buffer[i]))
727 return -EFAULT;
728 value = hex_to_bin(c);
729 if (value >= 0)
730 *num |= value;
731 else
732 break;
734 return i;
737 static int count_trail_chars(const char __user * user_buffer,
738 unsigned int maxlen)
740 int i;
742 for (i = 0; i < maxlen; i++) {
743 char c;
744 if (get_user(c, &user_buffer[i]))
745 return -EFAULT;
746 switch (c) {
747 case '\"':
748 case '\n':
749 case '\r':
750 case '\t':
751 case ' ':
752 case '=':
753 break;
754 default:
755 goto done;
758 done:
759 return i;
762 static long num_arg(const char __user *user_buffer, unsigned long maxlen,
763 unsigned long *num)
765 int i;
766 *num = 0;
768 for (i = 0; i < maxlen; i++) {
769 char c;
770 if (get_user(c, &user_buffer[i]))
771 return -EFAULT;
772 if ((c >= '0') && (c <= '9')) {
773 *num *= 10;
774 *num += c - '0';
775 } else
776 break;
778 return i;
781 static int strn_len(const char __user * user_buffer, unsigned int maxlen)
783 int i;
785 for (i = 0; i < maxlen; i++) {
786 char c;
787 if (get_user(c, &user_buffer[i]))
788 return -EFAULT;
789 switch (c) {
790 case '\"':
791 case '\n':
792 case '\r':
793 case '\t':
794 case ' ':
795 goto done_str;
796 default:
797 break;
800 done_str:
801 return i;
804 static ssize_t get_labels(const char __user *buffer, struct pktgen_dev *pkt_dev)
806 unsigned int n = 0;
807 char c;
808 ssize_t i = 0;
809 int len;
811 pkt_dev->nr_labels = 0;
812 do {
813 __u32 tmp;
814 len = hex32_arg(&buffer[i], 8, &tmp);
815 if (len <= 0)
816 return len;
817 pkt_dev->labels[n] = htonl(tmp);
818 if (pkt_dev->labels[n] & MPLS_STACK_BOTTOM)
819 pkt_dev->flags |= F_MPLS_RND;
820 i += len;
821 if (get_user(c, &buffer[i]))
822 return -EFAULT;
823 i++;
824 n++;
825 if (n >= MAX_MPLS_LABELS)
826 return -E2BIG;
827 } while (c == ',');
829 pkt_dev->nr_labels = n;
830 return i;
833 static __u32 pktgen_read_flag(const char *f, bool *disable)
835 __u32 i;
837 if (f[0] == '!') {
838 *disable = true;
839 f++;
842 for (i = 0; i < NR_PKT_FLAGS; i++) {
843 if (!IS_ENABLED(CONFIG_XFRM) && i == IPSEC_SHIFT)
844 continue;
846 /* allow only disabling ipv6 flag */
847 if (!*disable && i == IPV6_SHIFT)
848 continue;
850 if (strcmp(f, pkt_flag_names[i]) == 0)
851 return 1 << i;
854 if (strcmp(f, "FLOW_RND") == 0) {
855 *disable = !*disable;
856 return F_FLOW_SEQ;
859 return 0;
862 static ssize_t pktgen_if_write(struct file *file,
863 const char __user * user_buffer, size_t count,
864 loff_t * offset)
866 struct seq_file *seq = file->private_data;
867 struct pktgen_dev *pkt_dev = seq->private;
868 int i, max, len;
869 char name[16], valstr[32];
870 unsigned long value = 0;
871 char *pg_result = NULL;
872 int tmp = 0;
873 char buf[128];
875 pg_result = &(pkt_dev->result[0]);
877 if (count < 1) {
878 pr_warn("wrong command format\n");
879 return -EINVAL;
882 max = count;
883 tmp = count_trail_chars(user_buffer, max);
884 if (tmp < 0) {
885 pr_warn("illegal format\n");
886 return tmp;
888 i = tmp;
890 /* Read variable name */
892 len = strn_len(&user_buffer[i], sizeof(name) - 1);
893 if (len < 0)
894 return len;
896 memset(name, 0, sizeof(name));
897 if (copy_from_user(name, &user_buffer[i], len))
898 return -EFAULT;
899 i += len;
901 max = count - i;
902 len = count_trail_chars(&user_buffer[i], max);
903 if (len < 0)
904 return len;
906 i += len;
908 if (debug) {
909 size_t copy = min_t(size_t, count + 1, 1024);
910 char *tp = strndup_user(user_buffer, copy);
912 if (IS_ERR(tp))
913 return PTR_ERR(tp);
915 pr_debug("%s,%zu buffer -:%s:-\n", name, count, tp);
916 kfree(tp);
919 if (!strcmp(name, "min_pkt_size")) {
920 len = num_arg(&user_buffer[i], 10, &value);
921 if (len < 0)
922 return len;
924 i += len;
925 if (value < 14 + 20 + 8)
926 value = 14 + 20 + 8;
927 if (value != pkt_dev->min_pkt_size) {
928 pkt_dev->min_pkt_size = value;
929 pkt_dev->cur_pkt_size = value;
931 sprintf(pg_result, "OK: min_pkt_size=%u",
932 pkt_dev->min_pkt_size);
933 return count;
936 if (!strcmp(name, "max_pkt_size")) {
937 len = num_arg(&user_buffer[i], 10, &value);
938 if (len < 0)
939 return len;
941 i += len;
942 if (value < 14 + 20 + 8)
943 value = 14 + 20 + 8;
944 if (value != pkt_dev->max_pkt_size) {
945 pkt_dev->max_pkt_size = value;
946 pkt_dev->cur_pkt_size = value;
948 sprintf(pg_result, "OK: max_pkt_size=%u",
949 pkt_dev->max_pkt_size);
950 return count;
953 /* Shortcut for min = max */
955 if (!strcmp(name, "pkt_size")) {
956 len = num_arg(&user_buffer[i], 10, &value);
957 if (len < 0)
958 return len;
960 i += len;
961 if (value < 14 + 20 + 8)
962 value = 14 + 20 + 8;
963 if (value != pkt_dev->min_pkt_size) {
964 pkt_dev->min_pkt_size = value;
965 pkt_dev->max_pkt_size = value;
966 pkt_dev->cur_pkt_size = value;
968 sprintf(pg_result, "OK: pkt_size=%u", pkt_dev->min_pkt_size);
969 return count;
972 if (!strcmp(name, "debug")) {
973 len = num_arg(&user_buffer[i], 10, &value);
974 if (len < 0)
975 return len;
977 i += len;
978 debug = value;
979 sprintf(pg_result, "OK: debug=%u", debug);
980 return count;
983 if (!strcmp(name, "frags")) {
984 len = num_arg(&user_buffer[i], 10, &value);
985 if (len < 0)
986 return len;
988 i += len;
989 pkt_dev->nfrags = value;
990 sprintf(pg_result, "OK: frags=%u", pkt_dev->nfrags);
991 return count;
993 if (!strcmp(name, "delay")) {
994 len = num_arg(&user_buffer[i], 10, &value);
995 if (len < 0)
996 return len;
998 i += len;
999 if (value == 0x7FFFFFFF)
1000 pkt_dev->delay = ULLONG_MAX;
1001 else
1002 pkt_dev->delay = (u64)value;
1004 sprintf(pg_result, "OK: delay=%llu",
1005 (unsigned long long) pkt_dev->delay);
1006 return count;
1008 if (!strcmp(name, "rate")) {
1009 len = num_arg(&user_buffer[i], 10, &value);
1010 if (len < 0)
1011 return len;
1013 i += len;
1014 if (!value)
1015 return len;
1016 pkt_dev->delay = pkt_dev->min_pkt_size*8*NSEC_PER_USEC/value;
1017 if (debug)
1018 pr_info("Delay set at: %llu ns\n", pkt_dev->delay);
1020 sprintf(pg_result, "OK: rate=%lu", value);
1021 return count;
1023 if (!strcmp(name, "ratep")) {
1024 len = num_arg(&user_buffer[i], 10, &value);
1025 if (len < 0)
1026 return len;
1028 i += len;
1029 if (!value)
1030 return len;
1031 pkt_dev->delay = NSEC_PER_SEC/value;
1032 if (debug)
1033 pr_info("Delay set at: %llu ns\n", pkt_dev->delay);
1035 sprintf(pg_result, "OK: rate=%lu", value);
1036 return count;
1038 if (!strcmp(name, "udp_src_min")) {
1039 len = num_arg(&user_buffer[i], 10, &value);
1040 if (len < 0)
1041 return len;
1043 i += len;
1044 if (value != pkt_dev->udp_src_min) {
1045 pkt_dev->udp_src_min = value;
1046 pkt_dev->cur_udp_src = value;
1048 sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min);
1049 return count;
1051 if (!strcmp(name, "udp_dst_min")) {
1052 len = num_arg(&user_buffer[i], 10, &value);
1053 if (len < 0)
1054 return len;
1056 i += len;
1057 if (value != pkt_dev->udp_dst_min) {
1058 pkt_dev->udp_dst_min = value;
1059 pkt_dev->cur_udp_dst = value;
1061 sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min);
1062 return count;
1064 if (!strcmp(name, "udp_src_max")) {
1065 len = num_arg(&user_buffer[i], 10, &value);
1066 if (len < 0)
1067 return len;
1069 i += len;
1070 if (value != pkt_dev->udp_src_max) {
1071 pkt_dev->udp_src_max = value;
1072 pkt_dev->cur_udp_src = value;
1074 sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max);
1075 return count;
1077 if (!strcmp(name, "udp_dst_max")) {
1078 len = num_arg(&user_buffer[i], 10, &value);
1079 if (len < 0)
1080 return len;
1082 i += len;
1083 if (value != pkt_dev->udp_dst_max) {
1084 pkt_dev->udp_dst_max = value;
1085 pkt_dev->cur_udp_dst = value;
1087 sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max);
1088 return count;
1090 if (!strcmp(name, "clone_skb")) {
1091 len = num_arg(&user_buffer[i], 10, &value);
1092 if (len < 0)
1093 return len;
1094 if ((value > 0) &&
1095 ((pkt_dev->xmit_mode == M_NETIF_RECEIVE) ||
1096 !(pkt_dev->odev->priv_flags & IFF_TX_SKB_SHARING)))
1097 return -ENOTSUPP;
1098 i += len;
1099 pkt_dev->clone_skb = value;
1101 sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb);
1102 return count;
1104 if (!strcmp(name, "count")) {
1105 len = num_arg(&user_buffer[i], 10, &value);
1106 if (len < 0)
1107 return len;
1109 i += len;
1110 pkt_dev->count = value;
1111 sprintf(pg_result, "OK: count=%llu",
1112 (unsigned long long)pkt_dev->count);
1113 return count;
1115 if (!strcmp(name, "src_mac_count")) {
1116 len = num_arg(&user_buffer[i], 10, &value);
1117 if (len < 0)
1118 return len;
1120 i += len;
1121 if (pkt_dev->src_mac_count != value) {
1122 pkt_dev->src_mac_count = value;
1123 pkt_dev->cur_src_mac_offset = 0;
1125 sprintf(pg_result, "OK: src_mac_count=%d",
1126 pkt_dev->src_mac_count);
1127 return count;
1129 if (!strcmp(name, "dst_mac_count")) {
1130 len = num_arg(&user_buffer[i], 10, &value);
1131 if (len < 0)
1132 return len;
1134 i += len;
1135 if (pkt_dev->dst_mac_count != value) {
1136 pkt_dev->dst_mac_count = value;
1137 pkt_dev->cur_dst_mac_offset = 0;
1139 sprintf(pg_result, "OK: dst_mac_count=%d",
1140 pkt_dev->dst_mac_count);
1141 return count;
1143 if (!strcmp(name, "burst")) {
1144 len = num_arg(&user_buffer[i], 10, &value);
1145 if (len < 0)
1146 return len;
1148 i += len;
1149 if ((value > 1) &&
1150 ((pkt_dev->xmit_mode == M_QUEUE_XMIT) ||
1151 ((pkt_dev->xmit_mode == M_START_XMIT) &&
1152 (!(pkt_dev->odev->priv_flags & IFF_TX_SKB_SHARING)))))
1153 return -ENOTSUPP;
1154 pkt_dev->burst = value < 1 ? 1 : value;
1155 sprintf(pg_result, "OK: burst=%d", pkt_dev->burst);
1156 return count;
1158 if (!strcmp(name, "node")) {
1159 len = num_arg(&user_buffer[i], 10, &value);
1160 if (len < 0)
1161 return len;
1163 i += len;
1165 if (node_possible(value)) {
1166 pkt_dev->node = value;
1167 sprintf(pg_result, "OK: node=%d", pkt_dev->node);
1168 if (pkt_dev->page) {
1169 put_page(pkt_dev->page);
1170 pkt_dev->page = NULL;
1173 else
1174 sprintf(pg_result, "ERROR: node not possible");
1175 return count;
1177 if (!strcmp(name, "xmit_mode")) {
1178 char f[32];
1180 memset(f, 0, 32);
1181 len = strn_len(&user_buffer[i], sizeof(f) - 1);
1182 if (len < 0)
1183 return len;
1185 if (copy_from_user(f, &user_buffer[i], len))
1186 return -EFAULT;
1187 i += len;
1189 if (strcmp(f, "start_xmit") == 0) {
1190 pkt_dev->xmit_mode = M_START_XMIT;
1191 } else if (strcmp(f, "netif_receive") == 0) {
1192 /* clone_skb set earlier, not supported in this mode */
1193 if (pkt_dev->clone_skb > 0)
1194 return -ENOTSUPP;
1196 pkt_dev->xmit_mode = M_NETIF_RECEIVE;
1198 /* make sure new packet is allocated every time
1199 * pktgen_xmit() is called
1201 pkt_dev->last_ok = 1;
1203 /* override clone_skb if user passed default value
1204 * at module loading time
1206 pkt_dev->clone_skb = 0;
1207 } else if (strcmp(f, "queue_xmit") == 0) {
1208 pkt_dev->xmit_mode = M_QUEUE_XMIT;
1209 pkt_dev->last_ok = 1;
1210 } else {
1211 sprintf(pg_result,
1212 "xmit_mode -:%s:- unknown\nAvailable modes: %s",
1213 f, "start_xmit, netif_receive\n");
1214 return count;
1216 sprintf(pg_result, "OK: xmit_mode=%s", f);
1217 return count;
1219 if (!strcmp(name, "flag")) {
1220 __u32 flag;
1221 char f[32];
1222 bool disable = false;
1224 memset(f, 0, 32);
1225 len = strn_len(&user_buffer[i], sizeof(f) - 1);
1226 if (len < 0)
1227 return len;
1229 if (copy_from_user(f, &user_buffer[i], len))
1230 return -EFAULT;
1231 i += len;
1233 flag = pktgen_read_flag(f, &disable);
1235 if (flag) {
1236 if (disable)
1237 pkt_dev->flags &= ~flag;
1238 else
1239 pkt_dev->flags |= flag;
1240 } else {
1241 sprintf(pg_result,
1242 "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
1244 "IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
1245 "MACSRC_RND, MACDST_RND, TXSIZE_RND, IPV6, "
1246 "MPLS_RND, VID_RND, SVID_RND, FLOW_SEQ, "
1247 "QUEUE_MAP_RND, QUEUE_MAP_CPU, UDPCSUM, "
1248 "NO_TIMESTAMP, "
1249 #ifdef CONFIG_XFRM
1250 "IPSEC, "
1251 #endif
1252 "NODE_ALLOC\n");
1253 return count;
1255 sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
1256 return count;
1258 if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
1259 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
1260 if (len < 0)
1261 return len;
1263 if (copy_from_user(buf, &user_buffer[i], len))
1264 return -EFAULT;
1265 buf[len] = 0;
1266 if (strcmp(buf, pkt_dev->dst_min) != 0) {
1267 memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
1268 strncpy(pkt_dev->dst_min, buf, len);
1269 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
1270 pkt_dev->cur_daddr = pkt_dev->daddr_min;
1272 if (debug)
1273 pr_debug("dst_min set to: %s\n", pkt_dev->dst_min);
1274 i += len;
1275 sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min);
1276 return count;
1278 if (!strcmp(name, "dst_max")) {
1279 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
1280 if (len < 0)
1281 return len;
1284 if (copy_from_user(buf, &user_buffer[i], len))
1285 return -EFAULT;
1287 buf[len] = 0;
1288 if (strcmp(buf, pkt_dev->dst_max) != 0) {
1289 memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
1290 strncpy(pkt_dev->dst_max, buf, len);
1291 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
1292 pkt_dev->cur_daddr = pkt_dev->daddr_max;
1294 if (debug)
1295 pr_debug("dst_max set to: %s\n", pkt_dev->dst_max);
1296 i += len;
1297 sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max);
1298 return count;
1300 if (!strcmp(name, "dst6")) {
1301 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1302 if (len < 0)
1303 return len;
1305 pkt_dev->flags |= F_IPV6;
1307 if (copy_from_user(buf, &user_buffer[i], len))
1308 return -EFAULT;
1309 buf[len] = 0;
1311 in6_pton(buf, -1, pkt_dev->in6_daddr.s6_addr, -1, NULL);
1312 snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->in6_daddr);
1314 pkt_dev->cur_in6_daddr = pkt_dev->in6_daddr;
1316 if (debug)
1317 pr_debug("dst6 set to: %s\n", buf);
1319 i += len;
1320 sprintf(pg_result, "OK: dst6=%s", buf);
1321 return count;
1323 if (!strcmp(name, "dst6_min")) {
1324 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1325 if (len < 0)
1326 return len;
1328 pkt_dev->flags |= F_IPV6;
1330 if (copy_from_user(buf, &user_buffer[i], len))
1331 return -EFAULT;
1332 buf[len] = 0;
1334 in6_pton(buf, -1, pkt_dev->min_in6_daddr.s6_addr, -1, NULL);
1335 snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->min_in6_daddr);
1337 pkt_dev->cur_in6_daddr = pkt_dev->min_in6_daddr;
1338 if (debug)
1339 pr_debug("dst6_min set to: %s\n", buf);
1341 i += len;
1342 sprintf(pg_result, "OK: dst6_min=%s", buf);
1343 return count;
1345 if (!strcmp(name, "dst6_max")) {
1346 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1347 if (len < 0)
1348 return len;
1350 pkt_dev->flags |= F_IPV6;
1352 if (copy_from_user(buf, &user_buffer[i], len))
1353 return -EFAULT;
1354 buf[len] = 0;
1356 in6_pton(buf, -1, pkt_dev->max_in6_daddr.s6_addr, -1, NULL);
1357 snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->max_in6_daddr);
1359 if (debug)
1360 pr_debug("dst6_max set to: %s\n", buf);
1362 i += len;
1363 sprintf(pg_result, "OK: dst6_max=%s", buf);
1364 return count;
1366 if (!strcmp(name, "src6")) {
1367 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1368 if (len < 0)
1369 return len;
1371 pkt_dev->flags |= F_IPV6;
1373 if (copy_from_user(buf, &user_buffer[i], len))
1374 return -EFAULT;
1375 buf[len] = 0;
1377 in6_pton(buf, -1, pkt_dev->in6_saddr.s6_addr, -1, NULL);
1378 snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->in6_saddr);
1380 pkt_dev->cur_in6_saddr = pkt_dev->in6_saddr;
1382 if (debug)
1383 pr_debug("src6 set to: %s\n", buf);
1385 i += len;
1386 sprintf(pg_result, "OK: src6=%s", buf);
1387 return count;
1389 if (!strcmp(name, "src_min")) {
1390 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
1391 if (len < 0)
1392 return len;
1394 if (copy_from_user(buf, &user_buffer[i], len))
1395 return -EFAULT;
1396 buf[len] = 0;
1397 if (strcmp(buf, pkt_dev->src_min) != 0) {
1398 memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
1399 strncpy(pkt_dev->src_min, buf, len);
1400 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
1401 pkt_dev->cur_saddr = pkt_dev->saddr_min;
1403 if (debug)
1404 pr_debug("src_min set to: %s\n", pkt_dev->src_min);
1405 i += len;
1406 sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min);
1407 return count;
1409 if (!strcmp(name, "src_max")) {
1410 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
1411 if (len < 0)
1412 return len;
1414 if (copy_from_user(buf, &user_buffer[i], len))
1415 return -EFAULT;
1416 buf[len] = 0;
1417 if (strcmp(buf, pkt_dev->src_max) != 0) {
1418 memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
1419 strncpy(pkt_dev->src_max, buf, len);
1420 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
1421 pkt_dev->cur_saddr = pkt_dev->saddr_max;
1423 if (debug)
1424 pr_debug("src_max set to: %s\n", pkt_dev->src_max);
1425 i += len;
1426 sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max);
1427 return count;
1429 if (!strcmp(name, "dst_mac")) {
1430 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
1431 if (len < 0)
1432 return len;
1434 memset(valstr, 0, sizeof(valstr));
1435 if (copy_from_user(valstr, &user_buffer[i], len))
1436 return -EFAULT;
1438 if (!mac_pton(valstr, pkt_dev->dst_mac))
1439 return -EINVAL;
1440 /* Set up Dest MAC */
1441 ether_addr_copy(&pkt_dev->hh[0], pkt_dev->dst_mac);
1443 sprintf(pg_result, "OK: dstmac %pM", pkt_dev->dst_mac);
1444 return count;
1446 if (!strcmp(name, "src_mac")) {
1447 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
1448 if (len < 0)
1449 return len;
1451 memset(valstr, 0, sizeof(valstr));
1452 if (copy_from_user(valstr, &user_buffer[i], len))
1453 return -EFAULT;
1455 if (!mac_pton(valstr, pkt_dev->src_mac))
1456 return -EINVAL;
1457 /* Set up Src MAC */
1458 ether_addr_copy(&pkt_dev->hh[6], pkt_dev->src_mac);
1460 sprintf(pg_result, "OK: srcmac %pM", pkt_dev->src_mac);
1461 return count;
1464 if (!strcmp(name, "clear_counters")) {
1465 pktgen_clear_counters(pkt_dev);
1466 sprintf(pg_result, "OK: Clearing counters.\n");
1467 return count;
1470 if (!strcmp(name, "flows")) {
1471 len = num_arg(&user_buffer[i], 10, &value);
1472 if (len < 0)
1473 return len;
1475 i += len;
1476 if (value > MAX_CFLOWS)
1477 value = MAX_CFLOWS;
1479 pkt_dev->cflows = value;
1480 sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
1481 return count;
1483 #ifdef CONFIG_XFRM
1484 if (!strcmp(name, "spi")) {
1485 len = num_arg(&user_buffer[i], 10, &value);
1486 if (len < 0)
1487 return len;
1489 i += len;
1490 pkt_dev->spi = value;
1491 sprintf(pg_result, "OK: spi=%u", pkt_dev->spi);
1492 return count;
1494 #endif
1495 if (!strcmp(name, "flowlen")) {
1496 len = num_arg(&user_buffer[i], 10, &value);
1497 if (len < 0)
1498 return len;
1500 i += len;
1501 pkt_dev->lflow = value;
1502 sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
1503 return count;
1506 if (!strcmp(name, "queue_map_min")) {
1507 len = num_arg(&user_buffer[i], 5, &value);
1508 if (len < 0)
1509 return len;
1511 i += len;
1512 pkt_dev->queue_map_min = value;
1513 sprintf(pg_result, "OK: queue_map_min=%u", pkt_dev->queue_map_min);
1514 return count;
1517 if (!strcmp(name, "queue_map_max")) {
1518 len = num_arg(&user_buffer[i], 5, &value);
1519 if (len < 0)
1520 return len;
1522 i += len;
1523 pkt_dev->queue_map_max = value;
1524 sprintf(pg_result, "OK: queue_map_max=%u", pkt_dev->queue_map_max);
1525 return count;
1528 if (!strcmp(name, "mpls")) {
1529 unsigned int n, cnt;
1531 len = get_labels(&user_buffer[i], pkt_dev);
1532 if (len < 0)
1533 return len;
1534 i += len;
1535 cnt = sprintf(pg_result, "OK: mpls=");
1536 for (n = 0; n < pkt_dev->nr_labels; n++)
1537 cnt += sprintf(pg_result + cnt,
1538 "%08x%s", ntohl(pkt_dev->labels[n]),
1539 n == pkt_dev->nr_labels-1 ? "" : ",");
1541 if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) {
1542 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1543 pkt_dev->svlan_id = 0xffff;
1545 if (debug)
1546 pr_debug("VLAN/SVLAN auto turned off\n");
1548 return count;
1551 if (!strcmp(name, "vlan_id")) {
1552 len = num_arg(&user_buffer[i], 4, &value);
1553 if (len < 0)
1554 return len;
1556 i += len;
1557 if (value <= 4095) {
1558 pkt_dev->vlan_id = value; /* turn on VLAN */
1560 if (debug)
1561 pr_debug("VLAN turned on\n");
1563 if (debug && pkt_dev->nr_labels)
1564 pr_debug("MPLS auto turned off\n");
1566 pkt_dev->nr_labels = 0; /* turn off MPLS */
1567 sprintf(pg_result, "OK: vlan_id=%u", pkt_dev->vlan_id);
1568 } else {
1569 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1570 pkt_dev->svlan_id = 0xffff;
1572 if (debug)
1573 pr_debug("VLAN/SVLAN turned off\n");
1575 return count;
1578 if (!strcmp(name, "vlan_p")) {
1579 len = num_arg(&user_buffer[i], 1, &value);
1580 if (len < 0)
1581 return len;
1583 i += len;
1584 if ((value <= 7) && (pkt_dev->vlan_id != 0xffff)) {
1585 pkt_dev->vlan_p = value;
1586 sprintf(pg_result, "OK: vlan_p=%u", pkt_dev->vlan_p);
1587 } else {
1588 sprintf(pg_result, "ERROR: vlan_p must be 0-7");
1590 return count;
1593 if (!strcmp(name, "vlan_cfi")) {
1594 len = num_arg(&user_buffer[i], 1, &value);
1595 if (len < 0)
1596 return len;
1598 i += len;
1599 if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) {
1600 pkt_dev->vlan_cfi = value;
1601 sprintf(pg_result, "OK: vlan_cfi=%u", pkt_dev->vlan_cfi);
1602 } else {
1603 sprintf(pg_result, "ERROR: vlan_cfi must be 0-1");
1605 return count;
1608 if (!strcmp(name, "svlan_id")) {
1609 len = num_arg(&user_buffer[i], 4, &value);
1610 if (len < 0)
1611 return len;
1613 i += len;
1614 if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) {
1615 pkt_dev->svlan_id = value; /* turn on SVLAN */
1617 if (debug)
1618 pr_debug("SVLAN turned on\n");
1620 if (debug && pkt_dev->nr_labels)
1621 pr_debug("MPLS auto turned off\n");
1623 pkt_dev->nr_labels = 0; /* turn off MPLS */
1624 sprintf(pg_result, "OK: svlan_id=%u", pkt_dev->svlan_id);
1625 } else {
1626 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1627 pkt_dev->svlan_id = 0xffff;
1629 if (debug)
1630 pr_debug("VLAN/SVLAN turned off\n");
1632 return count;
1635 if (!strcmp(name, "svlan_p")) {
1636 len = num_arg(&user_buffer[i], 1, &value);
1637 if (len < 0)
1638 return len;
1640 i += len;
1641 if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) {
1642 pkt_dev->svlan_p = value;
1643 sprintf(pg_result, "OK: svlan_p=%u", pkt_dev->svlan_p);
1644 } else {
1645 sprintf(pg_result, "ERROR: svlan_p must be 0-7");
1647 return count;
1650 if (!strcmp(name, "svlan_cfi")) {
1651 len = num_arg(&user_buffer[i], 1, &value);
1652 if (len < 0)
1653 return len;
1655 i += len;
1656 if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) {
1657 pkt_dev->svlan_cfi = value;
1658 sprintf(pg_result, "OK: svlan_cfi=%u", pkt_dev->svlan_cfi);
1659 } else {
1660 sprintf(pg_result, "ERROR: svlan_cfi must be 0-1");
1662 return count;
1665 if (!strcmp(name, "tos")) {
1666 __u32 tmp_value = 0;
1667 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1668 if (len < 0)
1669 return len;
1671 i += len;
1672 if (len == 2) {
1673 pkt_dev->tos = tmp_value;
1674 sprintf(pg_result, "OK: tos=0x%02x", pkt_dev->tos);
1675 } else {
1676 sprintf(pg_result, "ERROR: tos must be 00-ff");
1678 return count;
1681 if (!strcmp(name, "traffic_class")) {
1682 __u32 tmp_value = 0;
1683 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1684 if (len < 0)
1685 return len;
1687 i += len;
1688 if (len == 2) {
1689 pkt_dev->traffic_class = tmp_value;
1690 sprintf(pg_result, "OK: traffic_class=0x%02x", pkt_dev->traffic_class);
1691 } else {
1692 sprintf(pg_result, "ERROR: traffic_class must be 00-ff");
1694 return count;
1697 if (!strcmp(name, "skb_priority")) {
1698 len = num_arg(&user_buffer[i], 9, &value);
1699 if (len < 0)
1700 return len;
1702 i += len;
1703 pkt_dev->skb_priority = value;
1704 sprintf(pg_result, "OK: skb_priority=%i",
1705 pkt_dev->skb_priority);
1706 return count;
1709 sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
1710 return -EINVAL;
1713 static int pktgen_if_open(struct inode *inode, struct file *file)
1715 return single_open(file, pktgen_if_show, PDE_DATA(inode));
1718 static const struct file_operations pktgen_if_fops = {
1719 .open = pktgen_if_open,
1720 .read = seq_read,
1721 .llseek = seq_lseek,
1722 .write = pktgen_if_write,
1723 .release = single_release,
1726 static int pktgen_thread_show(struct seq_file *seq, void *v)
1728 struct pktgen_thread *t = seq->private;
1729 const struct pktgen_dev *pkt_dev;
1731 BUG_ON(!t);
1733 seq_puts(seq, "Running: ");
1735 rcu_read_lock();
1736 list_for_each_entry_rcu(pkt_dev, &t->if_list, list)
1737 if (pkt_dev->running)
1738 seq_printf(seq, "%s ", pkt_dev->odevname);
1740 seq_puts(seq, "\nStopped: ");
1742 list_for_each_entry_rcu(pkt_dev, &t->if_list, list)
1743 if (!pkt_dev->running)
1744 seq_printf(seq, "%s ", pkt_dev->odevname);
1746 if (t->result[0])
1747 seq_printf(seq, "\nResult: %s\n", t->result);
1748 else
1749 seq_puts(seq, "\nResult: NA\n");
1751 rcu_read_unlock();
1753 return 0;
1756 static ssize_t pktgen_thread_write(struct file *file,
1757 const char __user * user_buffer,
1758 size_t count, loff_t * offset)
1760 struct seq_file *seq = file->private_data;
1761 struct pktgen_thread *t = seq->private;
1762 int i, max, len, ret;
1763 char name[40];
1764 char *pg_result;
1766 if (count < 1) {
1767 // sprintf(pg_result, "Wrong command format");
1768 return -EINVAL;
1771 max = count;
1772 len = count_trail_chars(user_buffer, max);
1773 if (len < 0)
1774 return len;
1776 i = len;
1778 /* Read variable name */
1780 len = strn_len(&user_buffer[i], sizeof(name) - 1);
1781 if (len < 0)
1782 return len;
1784 memset(name, 0, sizeof(name));
1785 if (copy_from_user(name, &user_buffer[i], len))
1786 return -EFAULT;
1787 i += len;
1789 max = count - i;
1790 len = count_trail_chars(&user_buffer[i], max);
1791 if (len < 0)
1792 return len;
1794 i += len;
1796 if (debug)
1797 pr_debug("t=%s, count=%lu\n", name, (unsigned long)count);
1799 if (!t) {
1800 pr_err("ERROR: No thread\n");
1801 ret = -EINVAL;
1802 goto out;
1805 pg_result = &(t->result[0]);
1807 if (!strcmp(name, "add_device")) {
1808 char f[32];
1809 memset(f, 0, 32);
1810 len = strn_len(&user_buffer[i], sizeof(f) - 1);
1811 if (len < 0) {
1812 ret = len;
1813 goto out;
1815 if (copy_from_user(f, &user_buffer[i], len))
1816 return -EFAULT;
1817 i += len;
1818 mutex_lock(&pktgen_thread_lock);
1819 ret = pktgen_add_device(t, f);
1820 mutex_unlock(&pktgen_thread_lock);
1821 if (!ret) {
1822 ret = count;
1823 sprintf(pg_result, "OK: add_device=%s", f);
1824 } else
1825 sprintf(pg_result, "ERROR: can not add device %s", f);
1826 goto out;
1829 if (!strcmp(name, "rem_device_all")) {
1830 mutex_lock(&pktgen_thread_lock);
1831 t->control |= T_REMDEVALL;
1832 mutex_unlock(&pktgen_thread_lock);
1833 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
1834 ret = count;
1835 sprintf(pg_result, "OK: rem_device_all");
1836 goto out;
1839 if (!strcmp(name, "max_before_softirq")) {
1840 sprintf(pg_result, "OK: Note! max_before_softirq is obsoleted -- Do not use");
1841 ret = count;
1842 goto out;
1845 ret = -EINVAL;
1846 out:
1847 return ret;
1850 static int pktgen_thread_open(struct inode *inode, struct file *file)
1852 return single_open(file, pktgen_thread_show, PDE_DATA(inode));
1855 static const struct file_operations pktgen_thread_fops = {
1856 .open = pktgen_thread_open,
1857 .read = seq_read,
1858 .llseek = seq_lseek,
1859 .write = pktgen_thread_write,
1860 .release = single_release,
1863 /* Think find or remove for NN */
1864 static struct pktgen_dev *__pktgen_NN_threads(const struct pktgen_net *pn,
1865 const char *ifname, int remove)
1867 struct pktgen_thread *t;
1868 struct pktgen_dev *pkt_dev = NULL;
1869 bool exact = (remove == FIND);
1871 list_for_each_entry(t, &pn->pktgen_threads, th_list) {
1872 pkt_dev = pktgen_find_dev(t, ifname, exact);
1873 if (pkt_dev) {
1874 if (remove) {
1875 pkt_dev->removal_mark = 1;
1876 t->control |= T_REMDEV;
1878 break;
1881 return pkt_dev;
1885 * mark a device for removal
1887 static void pktgen_mark_device(const struct pktgen_net *pn, const char *ifname)
1889 struct pktgen_dev *pkt_dev = NULL;
1890 const int max_tries = 10, msec_per_try = 125;
1891 int i = 0;
1893 mutex_lock(&pktgen_thread_lock);
1894 pr_debug("%s: marking %s for removal\n", __func__, ifname);
1896 while (1) {
1898 pkt_dev = __pktgen_NN_threads(pn, ifname, REMOVE);
1899 if (pkt_dev == NULL)
1900 break; /* success */
1902 mutex_unlock(&pktgen_thread_lock);
1903 pr_debug("%s: waiting for %s to disappear....\n",
1904 __func__, ifname);
1905 schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
1906 mutex_lock(&pktgen_thread_lock);
1908 if (++i >= max_tries) {
1909 pr_err("%s: timed out after waiting %d msec for device %s to be removed\n",
1910 __func__, msec_per_try * i, ifname);
1911 break;
1916 mutex_unlock(&pktgen_thread_lock);
1919 static void pktgen_change_name(const struct pktgen_net *pn, struct net_device *dev)
1921 struct pktgen_thread *t;
1923 mutex_lock(&pktgen_thread_lock);
1925 list_for_each_entry(t, &pn->pktgen_threads, th_list) {
1926 struct pktgen_dev *pkt_dev;
1928 if_lock(t);
1929 list_for_each_entry(pkt_dev, &t->if_list, list) {
1930 if (pkt_dev->odev != dev)
1931 continue;
1933 proc_remove(pkt_dev->entry);
1935 pkt_dev->entry = proc_create_data(dev->name, 0600,
1936 pn->proc_dir,
1937 &pktgen_if_fops,
1938 pkt_dev);
1939 if (!pkt_dev->entry)
1940 pr_err("can't move proc entry for '%s'\n",
1941 dev->name);
1942 break;
1944 if_unlock(t);
1946 mutex_unlock(&pktgen_thread_lock);
1949 static int pktgen_device_event(struct notifier_block *unused,
1950 unsigned long event, void *ptr)
1952 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1953 struct pktgen_net *pn = net_generic(dev_net(dev), pg_net_id);
1955 if (pn->pktgen_exiting)
1956 return NOTIFY_DONE;
1958 /* It is OK that we do not hold the group lock right now,
1959 * as we run under the RTNL lock.
1962 switch (event) {
1963 case NETDEV_CHANGENAME:
1964 pktgen_change_name(pn, dev);
1965 break;
1967 case NETDEV_UNREGISTER:
1968 pktgen_mark_device(pn, dev->name);
1969 break;
1972 return NOTIFY_DONE;
1975 static struct net_device *pktgen_dev_get_by_name(const struct pktgen_net *pn,
1976 struct pktgen_dev *pkt_dev,
1977 const char *ifname)
1979 char b[IFNAMSIZ+5];
1980 int i;
1982 for (i = 0; ifname[i] != '@'; i++) {
1983 if (i == IFNAMSIZ)
1984 break;
1986 b[i] = ifname[i];
1988 b[i] = 0;
1990 return dev_get_by_name(pn->net, b);
1994 /* Associate pktgen_dev with a device. */
1996 static int pktgen_setup_dev(const struct pktgen_net *pn,
1997 struct pktgen_dev *pkt_dev, const char *ifname)
1999 struct net_device *odev;
2000 int err;
2002 /* Clean old setups */
2003 if (pkt_dev->odev) {
2004 dev_put(pkt_dev->odev);
2005 pkt_dev->odev = NULL;
2008 odev = pktgen_dev_get_by_name(pn, pkt_dev, ifname);
2009 if (!odev) {
2010 pr_err("no such netdevice: \"%s\"\n", ifname);
2011 return -ENODEV;
2014 if (odev->type != ARPHRD_ETHER) {
2015 pr_err("not an ethernet device: \"%s\"\n", ifname);
2016 err = -EINVAL;
2017 } else if (!netif_running(odev)) {
2018 pr_err("device is down: \"%s\"\n", ifname);
2019 err = -ENETDOWN;
2020 } else {
2021 pkt_dev->odev = odev;
2022 return 0;
2025 dev_put(odev);
2026 return err;
2029 /* Read pkt_dev from the interface and set up internal pktgen_dev
2030 * structure to have the right information to create/send packets
2032 static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
2034 int ntxq;
2036 if (!pkt_dev->odev) {
2037 pr_err("ERROR: pkt_dev->odev == NULL in setup_inject\n");
2038 sprintf(pkt_dev->result,
2039 "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
2040 return;
2043 /* make sure that we don't pick a non-existing transmit queue */
2044 ntxq = pkt_dev->odev->real_num_tx_queues;
2046 if (ntxq <= pkt_dev->queue_map_min) {
2047 pr_warn("WARNING: Requested queue_map_min (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
2048 pkt_dev->queue_map_min, (ntxq ?: 1) - 1, ntxq,
2049 pkt_dev->odevname);
2050 pkt_dev->queue_map_min = (ntxq ?: 1) - 1;
2052 if (pkt_dev->queue_map_max >= ntxq) {
2053 pr_warn("WARNING: Requested queue_map_max (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
2054 pkt_dev->queue_map_max, (ntxq ?: 1) - 1, ntxq,
2055 pkt_dev->odevname);
2056 pkt_dev->queue_map_max = (ntxq ?: 1) - 1;
2059 /* Default to the interface's mac if not explicitly set. */
2061 if (is_zero_ether_addr(pkt_dev->src_mac))
2062 ether_addr_copy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr);
2064 /* Set up Dest MAC */
2065 ether_addr_copy(&(pkt_dev->hh[0]), pkt_dev->dst_mac);
2067 if (pkt_dev->flags & F_IPV6) {
2068 int i, set = 0, err = 1;
2069 struct inet6_dev *idev;
2071 if (pkt_dev->min_pkt_size == 0) {
2072 pkt_dev->min_pkt_size = 14 + sizeof(struct ipv6hdr)
2073 + sizeof(struct udphdr)
2074 + sizeof(struct pktgen_hdr)
2075 + pkt_dev->pkt_overhead;
2078 for (i = 0; i < sizeof(struct in6_addr); i++)
2079 if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
2080 set = 1;
2081 break;
2084 if (!set) {
2087 * Use linklevel address if unconfigured.
2089 * use ipv6_get_lladdr if/when it's get exported
2092 rcu_read_lock();
2093 idev = __in6_dev_get(pkt_dev->odev);
2094 if (idev) {
2095 struct inet6_ifaddr *ifp;
2097 read_lock_bh(&idev->lock);
2098 list_for_each_entry(ifp, &idev->addr_list, if_list) {
2099 if ((ifp->scope & IFA_LINK) &&
2100 !(ifp->flags & IFA_F_TENTATIVE)) {
2101 pkt_dev->cur_in6_saddr = ifp->addr;
2102 err = 0;
2103 break;
2106 read_unlock_bh(&idev->lock);
2108 rcu_read_unlock();
2109 if (err)
2110 pr_err("ERROR: IPv6 link address not available\n");
2112 } else {
2113 if (pkt_dev->min_pkt_size == 0) {
2114 pkt_dev->min_pkt_size = 14 + sizeof(struct iphdr)
2115 + sizeof(struct udphdr)
2116 + sizeof(struct pktgen_hdr)
2117 + pkt_dev->pkt_overhead;
2120 pkt_dev->saddr_min = 0;
2121 pkt_dev->saddr_max = 0;
2122 if (strlen(pkt_dev->src_min) == 0) {
2124 struct in_device *in_dev;
2126 rcu_read_lock();
2127 in_dev = __in_dev_get_rcu(pkt_dev->odev);
2128 if (in_dev) {
2129 if (in_dev->ifa_list) {
2130 pkt_dev->saddr_min =
2131 in_dev->ifa_list->ifa_address;
2132 pkt_dev->saddr_max = pkt_dev->saddr_min;
2135 rcu_read_unlock();
2136 } else {
2137 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
2138 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
2141 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
2142 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
2144 /* Initialize current values. */
2145 pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
2146 if (pkt_dev->min_pkt_size > pkt_dev->max_pkt_size)
2147 pkt_dev->max_pkt_size = pkt_dev->min_pkt_size;
2149 pkt_dev->cur_dst_mac_offset = 0;
2150 pkt_dev->cur_src_mac_offset = 0;
2151 pkt_dev->cur_saddr = pkt_dev->saddr_min;
2152 pkt_dev->cur_daddr = pkt_dev->daddr_min;
2153 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2154 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
2155 pkt_dev->nflows = 0;
2159 static void spin(struct pktgen_dev *pkt_dev, ktime_t spin_until)
2161 ktime_t start_time, end_time;
2162 s64 remaining;
2163 struct hrtimer_sleeper t;
2165 hrtimer_init_on_stack(&t.timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
2166 hrtimer_set_expires(&t.timer, spin_until);
2168 remaining = ktime_to_ns(hrtimer_expires_remaining(&t.timer));
2169 if (remaining <= 0)
2170 goto out;
2172 start_time = ktime_get();
2173 if (remaining < 100000) {
2174 /* for small delays (<100us), just loop until limit is reached */
2175 do {
2176 end_time = ktime_get();
2177 } while (ktime_compare(end_time, spin_until) < 0);
2178 } else {
2179 /* see do_nanosleep */
2180 hrtimer_init_sleeper(&t, current);
2181 do {
2182 set_current_state(TASK_INTERRUPTIBLE);
2183 hrtimer_start_expires(&t.timer, HRTIMER_MODE_ABS);
2185 if (likely(t.task))
2186 schedule();
2188 hrtimer_cancel(&t.timer);
2189 } while (t.task && pkt_dev->running && !signal_pending(current));
2190 __set_current_state(TASK_RUNNING);
2191 end_time = ktime_get();
2194 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(end_time, start_time));
2195 out:
2196 pkt_dev->next_tx = ktime_add_ns(spin_until, pkt_dev->delay);
2197 destroy_hrtimer_on_stack(&t.timer);
2200 static inline void set_pkt_overhead(struct pktgen_dev *pkt_dev)
2202 pkt_dev->pkt_overhead = 0;
2203 pkt_dev->pkt_overhead += pkt_dev->nr_labels*sizeof(u32);
2204 pkt_dev->pkt_overhead += VLAN_TAG_SIZE(pkt_dev);
2205 pkt_dev->pkt_overhead += SVLAN_TAG_SIZE(pkt_dev);
2208 static inline int f_seen(const struct pktgen_dev *pkt_dev, int flow)
2210 return !!(pkt_dev->flows[flow].flags & F_INIT);
2213 static inline int f_pick(struct pktgen_dev *pkt_dev)
2215 int flow = pkt_dev->curfl;
2217 if (pkt_dev->flags & F_FLOW_SEQ) {
2218 if (pkt_dev->flows[flow].count >= pkt_dev->lflow) {
2219 /* reset time */
2220 pkt_dev->flows[flow].count = 0;
2221 pkt_dev->flows[flow].flags = 0;
2222 pkt_dev->curfl += 1;
2223 if (pkt_dev->curfl >= pkt_dev->cflows)
2224 pkt_dev->curfl = 0; /*reset */
2226 } else {
2227 flow = prandom_u32() % pkt_dev->cflows;
2228 pkt_dev->curfl = flow;
2230 if (pkt_dev->flows[flow].count > pkt_dev->lflow) {
2231 pkt_dev->flows[flow].count = 0;
2232 pkt_dev->flows[flow].flags = 0;
2236 return pkt_dev->curfl;
2240 #ifdef CONFIG_XFRM
2241 /* If there was already an IPSEC SA, we keep it as is, else
2242 * we go look for it ...
2244 #define DUMMY_MARK 0
2245 static void get_ipsec_sa(struct pktgen_dev *pkt_dev, int flow)
2247 struct xfrm_state *x = pkt_dev->flows[flow].x;
2248 struct pktgen_net *pn = net_generic(dev_net(pkt_dev->odev), pg_net_id);
2249 if (!x) {
2251 if (pkt_dev->spi) {
2252 /* We need as quick as possible to find the right SA
2253 * Searching with minimum criteria to archieve this.
2255 x = xfrm_state_lookup_byspi(pn->net, htonl(pkt_dev->spi), AF_INET);
2256 } else {
2257 /* slow path: we dont already have xfrm_state */
2258 x = xfrm_stateonly_find(pn->net, DUMMY_MARK,
2259 (xfrm_address_t *)&pkt_dev->cur_daddr,
2260 (xfrm_address_t *)&pkt_dev->cur_saddr,
2261 AF_INET,
2262 pkt_dev->ipsmode,
2263 pkt_dev->ipsproto, 0);
2265 if (x) {
2266 pkt_dev->flows[flow].x = x;
2267 set_pkt_overhead(pkt_dev);
2268 pkt_dev->pkt_overhead += x->props.header_len;
2273 #endif
2274 static void set_cur_queue_map(struct pktgen_dev *pkt_dev)
2277 if (pkt_dev->flags & F_QUEUE_MAP_CPU)
2278 pkt_dev->cur_queue_map = smp_processor_id();
2280 else if (pkt_dev->queue_map_min <= pkt_dev->queue_map_max) {
2281 __u16 t;
2282 if (pkt_dev->flags & F_QUEUE_MAP_RND) {
2283 t = prandom_u32() %
2284 (pkt_dev->queue_map_max -
2285 pkt_dev->queue_map_min + 1)
2286 + pkt_dev->queue_map_min;
2287 } else {
2288 t = pkt_dev->cur_queue_map + 1;
2289 if (t > pkt_dev->queue_map_max)
2290 t = pkt_dev->queue_map_min;
2292 pkt_dev->cur_queue_map = t;
2294 pkt_dev->cur_queue_map = pkt_dev->cur_queue_map % pkt_dev->odev->real_num_tx_queues;
2297 /* Increment/randomize headers according to flags and current values
2298 * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
2300 static void mod_cur_headers(struct pktgen_dev *pkt_dev)
2302 __u32 imn;
2303 __u32 imx;
2304 int flow = 0;
2306 if (pkt_dev->cflows)
2307 flow = f_pick(pkt_dev);
2309 /* Deal with source MAC */
2310 if (pkt_dev->src_mac_count > 1) {
2311 __u32 mc;
2312 __u32 tmp;
2314 if (pkt_dev->flags & F_MACSRC_RND)
2315 mc = prandom_u32() % pkt_dev->src_mac_count;
2316 else {
2317 mc = pkt_dev->cur_src_mac_offset++;
2318 if (pkt_dev->cur_src_mac_offset >=
2319 pkt_dev->src_mac_count)
2320 pkt_dev->cur_src_mac_offset = 0;
2323 tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
2324 pkt_dev->hh[11] = tmp;
2325 tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2326 pkt_dev->hh[10] = tmp;
2327 tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2328 pkt_dev->hh[9] = tmp;
2329 tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2330 pkt_dev->hh[8] = tmp;
2331 tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
2332 pkt_dev->hh[7] = tmp;
2335 /* Deal with Destination MAC */
2336 if (pkt_dev->dst_mac_count > 1) {
2337 __u32 mc;
2338 __u32 tmp;
2340 if (pkt_dev->flags & F_MACDST_RND)
2341 mc = prandom_u32() % pkt_dev->dst_mac_count;
2343 else {
2344 mc = pkt_dev->cur_dst_mac_offset++;
2345 if (pkt_dev->cur_dst_mac_offset >=
2346 pkt_dev->dst_mac_count) {
2347 pkt_dev->cur_dst_mac_offset = 0;
2351 tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
2352 pkt_dev->hh[5] = tmp;
2353 tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2354 pkt_dev->hh[4] = tmp;
2355 tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2356 pkt_dev->hh[3] = tmp;
2357 tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2358 pkt_dev->hh[2] = tmp;
2359 tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
2360 pkt_dev->hh[1] = tmp;
2363 if (pkt_dev->flags & F_MPLS_RND) {
2364 unsigned int i;
2365 for (i = 0; i < pkt_dev->nr_labels; i++)
2366 if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
2367 pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
2368 ((__force __be32)prandom_u32() &
2369 htonl(0x000fffff));
2372 if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
2373 pkt_dev->vlan_id = prandom_u32() & (4096 - 1);
2376 if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
2377 pkt_dev->svlan_id = prandom_u32() & (4096 - 1);
2380 if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
2381 if (pkt_dev->flags & F_UDPSRC_RND)
2382 pkt_dev->cur_udp_src = prandom_u32() %
2383 (pkt_dev->udp_src_max - pkt_dev->udp_src_min)
2384 + pkt_dev->udp_src_min;
2386 else {
2387 pkt_dev->cur_udp_src++;
2388 if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
2389 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
2393 if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
2394 if (pkt_dev->flags & F_UDPDST_RND) {
2395 pkt_dev->cur_udp_dst = prandom_u32() %
2396 (pkt_dev->udp_dst_max - pkt_dev->udp_dst_min)
2397 + pkt_dev->udp_dst_min;
2398 } else {
2399 pkt_dev->cur_udp_dst++;
2400 if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
2401 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2405 if (!(pkt_dev->flags & F_IPV6)) {
2407 imn = ntohl(pkt_dev->saddr_min);
2408 imx = ntohl(pkt_dev->saddr_max);
2409 if (imn < imx) {
2410 __u32 t;
2411 if (pkt_dev->flags & F_IPSRC_RND)
2412 t = prandom_u32() % (imx - imn) + imn;
2413 else {
2414 t = ntohl(pkt_dev->cur_saddr);
2415 t++;
2416 if (t > imx)
2417 t = imn;
2420 pkt_dev->cur_saddr = htonl(t);
2423 if (pkt_dev->cflows && f_seen(pkt_dev, flow)) {
2424 pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
2425 } else {
2426 imn = ntohl(pkt_dev->daddr_min);
2427 imx = ntohl(pkt_dev->daddr_max);
2428 if (imn < imx) {
2429 __u32 t;
2430 __be32 s;
2431 if (pkt_dev->flags & F_IPDST_RND) {
2433 do {
2434 t = prandom_u32() %
2435 (imx - imn) + imn;
2436 s = htonl(t);
2437 } while (ipv4_is_loopback(s) ||
2438 ipv4_is_multicast(s) ||
2439 ipv4_is_lbcast(s) ||
2440 ipv4_is_zeronet(s) ||
2441 ipv4_is_local_multicast(s));
2442 pkt_dev->cur_daddr = s;
2443 } else {
2444 t = ntohl(pkt_dev->cur_daddr);
2445 t++;
2446 if (t > imx) {
2447 t = imn;
2449 pkt_dev->cur_daddr = htonl(t);
2452 if (pkt_dev->cflows) {
2453 pkt_dev->flows[flow].flags |= F_INIT;
2454 pkt_dev->flows[flow].cur_daddr =
2455 pkt_dev->cur_daddr;
2456 #ifdef CONFIG_XFRM
2457 if (pkt_dev->flags & F_IPSEC)
2458 get_ipsec_sa(pkt_dev, flow);
2459 #endif
2460 pkt_dev->nflows++;
2463 } else { /* IPV6 * */
2465 if (!ipv6_addr_any(&pkt_dev->min_in6_daddr)) {
2466 int i;
2468 /* Only random destinations yet */
2470 for (i = 0; i < 4; i++) {
2471 pkt_dev->cur_in6_daddr.s6_addr32[i] =
2472 (((__force __be32)prandom_u32() |
2473 pkt_dev->min_in6_daddr.s6_addr32[i]) &
2474 pkt_dev->max_in6_daddr.s6_addr32[i]);
2479 if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
2480 __u32 t;
2481 if (pkt_dev->flags & F_TXSIZE_RND) {
2482 t = prandom_u32() %
2483 (pkt_dev->max_pkt_size - pkt_dev->min_pkt_size)
2484 + pkt_dev->min_pkt_size;
2485 } else {
2486 t = pkt_dev->cur_pkt_size + 1;
2487 if (t > pkt_dev->max_pkt_size)
2488 t = pkt_dev->min_pkt_size;
2490 pkt_dev->cur_pkt_size = t;
2493 set_cur_queue_map(pkt_dev);
2495 pkt_dev->flows[flow].count++;
2499 #ifdef CONFIG_XFRM
2500 static u32 pktgen_dst_metrics[RTAX_MAX + 1] = {
2502 [RTAX_HOPLIMIT] = 0x5, /* Set a static hoplimit */
2505 static int pktgen_output_ipsec(struct sk_buff *skb, struct pktgen_dev *pkt_dev)
2507 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2508 int err = 0;
2509 struct net *net = dev_net(pkt_dev->odev);
2511 if (!x)
2512 return 0;
2513 /* XXX: we dont support tunnel mode for now until
2514 * we resolve the dst issue */
2515 if ((x->props.mode != XFRM_MODE_TRANSPORT) && (pkt_dev->spi == 0))
2516 return 0;
2518 /* But when user specify an valid SPI, transformation
2519 * supports both transport/tunnel mode + ESP/AH type.
2521 if ((x->props.mode == XFRM_MODE_TUNNEL) && (pkt_dev->spi != 0))
2522 skb->_skb_refdst = (unsigned long)&pkt_dev->xdst.u.dst | SKB_DST_NOREF;
2524 rcu_read_lock_bh();
2525 err = x->outer_mode->output(x, skb);
2526 rcu_read_unlock_bh();
2527 if (err) {
2528 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEMODEERROR);
2529 goto error;
2531 err = x->type->output(x, skb);
2532 if (err) {
2533 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEPROTOERROR);
2534 goto error;
2536 spin_lock_bh(&x->lock);
2537 x->curlft.bytes += skb->len;
2538 x->curlft.packets++;
2539 spin_unlock_bh(&x->lock);
2540 error:
2541 return err;
2544 static void free_SAs(struct pktgen_dev *pkt_dev)
2546 if (pkt_dev->cflows) {
2547 /* let go of the SAs if we have them */
2548 int i;
2549 for (i = 0; i < pkt_dev->cflows; i++) {
2550 struct xfrm_state *x = pkt_dev->flows[i].x;
2551 if (x) {
2552 xfrm_state_put(x);
2553 pkt_dev->flows[i].x = NULL;
2559 static int process_ipsec(struct pktgen_dev *pkt_dev,
2560 struct sk_buff *skb, __be16 protocol)
2562 if (pkt_dev->flags & F_IPSEC) {
2563 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2564 int nhead = 0;
2565 if (x) {
2566 struct ethhdr *eth;
2567 struct iphdr *iph;
2568 int ret;
2570 nhead = x->props.header_len - skb_headroom(skb);
2571 if (nhead > 0) {
2572 ret = pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
2573 if (ret < 0) {
2574 pr_err("Error expanding ipsec packet %d\n",
2575 ret);
2576 goto err;
2580 /* ipsec is not expecting ll header */
2581 skb_pull(skb, ETH_HLEN);
2582 ret = pktgen_output_ipsec(skb, pkt_dev);
2583 if (ret) {
2584 pr_err("Error creating ipsec packet %d\n", ret);
2585 goto err;
2587 /* restore ll */
2588 eth = skb_push(skb, ETH_HLEN);
2589 memcpy(eth, pkt_dev->hh, 2 * ETH_ALEN);
2590 eth->h_proto = protocol;
2592 /* Update IPv4 header len as well as checksum value */
2593 iph = ip_hdr(skb);
2594 iph->tot_len = htons(skb->len - ETH_HLEN);
2595 ip_send_check(iph);
2598 return 1;
2599 err:
2600 kfree_skb(skb);
2601 return 0;
2603 #endif
2605 static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
2607 unsigned int i;
2608 for (i = 0; i < pkt_dev->nr_labels; i++)
2609 *mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
2611 mpls--;
2612 *mpls |= MPLS_STACK_BOTTOM;
2615 static inline __be16 build_tci(unsigned int id, unsigned int cfi,
2616 unsigned int prio)
2618 return htons(id | (cfi << 12) | (prio << 13));
2621 static void pktgen_finalize_skb(struct pktgen_dev *pkt_dev, struct sk_buff *skb,
2622 int datalen)
2624 struct timespec64 timestamp;
2625 struct pktgen_hdr *pgh;
2627 pgh = skb_put(skb, sizeof(*pgh));
2628 datalen -= sizeof(*pgh);
2630 if (pkt_dev->nfrags <= 0) {
2631 skb_put_zero(skb, datalen);
2632 } else {
2633 int frags = pkt_dev->nfrags;
2634 int i, len;
2635 int frag_len;
2638 if (frags > MAX_SKB_FRAGS)
2639 frags = MAX_SKB_FRAGS;
2640 len = datalen - frags * PAGE_SIZE;
2641 if (len > 0) {
2642 skb_put_zero(skb, len);
2643 datalen = frags * PAGE_SIZE;
2646 i = 0;
2647 frag_len = (datalen/frags) < PAGE_SIZE ?
2648 (datalen/frags) : PAGE_SIZE;
2649 while (datalen > 0) {
2650 if (unlikely(!pkt_dev->page)) {
2651 int node = numa_node_id();
2653 if (pkt_dev->node >= 0 && (pkt_dev->flags & F_NODE))
2654 node = pkt_dev->node;
2655 pkt_dev->page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
2656 if (!pkt_dev->page)
2657 break;
2659 get_page(pkt_dev->page);
2660 skb_frag_set_page(skb, i, pkt_dev->page);
2661 skb_shinfo(skb)->frags[i].page_offset = 0;
2662 /*last fragment, fill rest of data*/
2663 if (i == (frags - 1))
2664 skb_frag_size_set(&skb_shinfo(skb)->frags[i],
2665 (datalen < PAGE_SIZE ? datalen : PAGE_SIZE));
2666 else
2667 skb_frag_size_set(&skb_shinfo(skb)->frags[i], frag_len);
2668 datalen -= skb_frag_size(&skb_shinfo(skb)->frags[i]);
2669 skb->len += skb_frag_size(&skb_shinfo(skb)->frags[i]);
2670 skb->data_len += skb_frag_size(&skb_shinfo(skb)->frags[i]);
2671 i++;
2672 skb_shinfo(skb)->nr_frags = i;
2676 /* Stamp the time, and sequence number,
2677 * convert them to network byte order
2679 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2680 pgh->seq_num = htonl(pkt_dev->seq_num);
2682 if (pkt_dev->flags & F_NO_TIMESTAMP) {
2683 pgh->tv_sec = 0;
2684 pgh->tv_usec = 0;
2685 } else {
2687 * pgh->tv_sec wraps in y2106 when interpreted as unsigned
2688 * as done by wireshark, or y2038 when interpreted as signed.
2689 * This is probably harmless, but if anyone wants to improve
2690 * it, we could introduce a variant that puts 64-bit nanoseconds
2691 * into the respective header bytes.
2692 * This would also be slightly faster to read.
2694 ktime_get_real_ts64(&timestamp);
2695 pgh->tv_sec = htonl(timestamp.tv_sec);
2696 pgh->tv_usec = htonl(timestamp.tv_nsec / NSEC_PER_USEC);
2700 static struct sk_buff *pktgen_alloc_skb(struct net_device *dev,
2701 struct pktgen_dev *pkt_dev)
2703 unsigned int extralen = LL_RESERVED_SPACE(dev);
2704 struct sk_buff *skb = NULL;
2705 unsigned int size;
2707 size = pkt_dev->cur_pkt_size + 64 + extralen + pkt_dev->pkt_overhead;
2708 if (pkt_dev->flags & F_NODE) {
2709 int node = pkt_dev->node >= 0 ? pkt_dev->node : numa_node_id();
2711 skb = __alloc_skb(NET_SKB_PAD + size, GFP_NOWAIT, 0, node);
2712 if (likely(skb)) {
2713 skb_reserve(skb, NET_SKB_PAD);
2714 skb->dev = dev;
2716 } else {
2717 skb = __netdev_alloc_skb(dev, size, GFP_NOWAIT);
2720 /* the caller pre-fetches from skb->data and reserves for the mac hdr */
2721 if (likely(skb))
2722 skb_reserve(skb, extralen - 16);
2724 return skb;
2727 static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
2728 struct pktgen_dev *pkt_dev)
2730 struct sk_buff *skb = NULL;
2731 __u8 *eth;
2732 struct udphdr *udph;
2733 int datalen, iplen;
2734 struct iphdr *iph;
2735 __be16 protocol = htons(ETH_P_IP);
2736 __be32 *mpls;
2737 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2738 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2739 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2740 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2741 u16 queue_map;
2743 if (pkt_dev->nr_labels)
2744 protocol = htons(ETH_P_MPLS_UC);
2746 if (pkt_dev->vlan_id != 0xffff)
2747 protocol = htons(ETH_P_8021Q);
2749 /* Update any of the values, used when we're incrementing various
2750 * fields.
2752 mod_cur_headers(pkt_dev);
2753 queue_map = pkt_dev->cur_queue_map;
2755 skb = pktgen_alloc_skb(odev, pkt_dev);
2756 if (!skb) {
2757 sprintf(pkt_dev->result, "No memory");
2758 return NULL;
2761 prefetchw(skb->data);
2762 skb_reserve(skb, 16);
2764 /* Reserve for ethernet and IP header */
2765 eth = skb_push(skb, 14);
2766 mpls = skb_put(skb, pkt_dev->nr_labels * sizeof(__u32));
2767 if (pkt_dev->nr_labels)
2768 mpls_push(mpls, pkt_dev);
2770 if (pkt_dev->vlan_id != 0xffff) {
2771 if (pkt_dev->svlan_id != 0xffff) {
2772 svlan_tci = skb_put(skb, sizeof(__be16));
2773 *svlan_tci = build_tci(pkt_dev->svlan_id,
2774 pkt_dev->svlan_cfi,
2775 pkt_dev->svlan_p);
2776 svlan_encapsulated_proto = skb_put(skb,
2777 sizeof(__be16));
2778 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
2780 vlan_tci = skb_put(skb, sizeof(__be16));
2781 *vlan_tci = build_tci(pkt_dev->vlan_id,
2782 pkt_dev->vlan_cfi,
2783 pkt_dev->vlan_p);
2784 vlan_encapsulated_proto = skb_put(skb, sizeof(__be16));
2785 *vlan_encapsulated_proto = htons(ETH_P_IP);
2788 skb_reset_mac_header(skb);
2789 skb_set_network_header(skb, skb->len);
2790 iph = skb_put(skb, sizeof(struct iphdr));
2792 skb_set_transport_header(skb, skb->len);
2793 udph = skb_put(skb, sizeof(struct udphdr));
2794 skb_set_queue_mapping(skb, queue_map);
2795 skb->priority = pkt_dev->skb_priority;
2797 memcpy(eth, pkt_dev->hh, 12);
2798 *(__be16 *) & eth[12] = protocol;
2800 /* Eth + IPh + UDPh + mpls */
2801 datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
2802 pkt_dev->pkt_overhead;
2803 if (datalen < 0 || datalen < sizeof(struct pktgen_hdr))
2804 datalen = sizeof(struct pktgen_hdr);
2806 udph->source = htons(pkt_dev->cur_udp_src);
2807 udph->dest = htons(pkt_dev->cur_udp_dst);
2808 udph->len = htons(datalen + 8); /* DATA + udphdr */
2809 udph->check = 0;
2811 iph->ihl = 5;
2812 iph->version = 4;
2813 iph->ttl = 32;
2814 iph->tos = pkt_dev->tos;
2815 iph->protocol = IPPROTO_UDP; /* UDP */
2816 iph->saddr = pkt_dev->cur_saddr;
2817 iph->daddr = pkt_dev->cur_daddr;
2818 iph->id = htons(pkt_dev->ip_id);
2819 pkt_dev->ip_id++;
2820 iph->frag_off = 0;
2821 iplen = 20 + 8 + datalen;
2822 iph->tot_len = htons(iplen);
2823 ip_send_check(iph);
2824 skb->protocol = protocol;
2825 skb->dev = odev;
2826 skb->pkt_type = PACKET_HOST;
2828 pktgen_finalize_skb(pkt_dev, skb, datalen);
2830 if (!(pkt_dev->flags & F_UDPCSUM)) {
2831 skb->ip_summed = CHECKSUM_NONE;
2832 } else if (odev->features & (NETIF_F_HW_CSUM | NETIF_F_IP_CSUM)) {
2833 skb->ip_summed = CHECKSUM_PARTIAL;
2834 skb->csum = 0;
2835 udp4_hwcsum(skb, iph->saddr, iph->daddr);
2836 } else {
2837 __wsum csum = skb_checksum(skb, skb_transport_offset(skb), datalen + 8, 0);
2839 /* add protocol-dependent pseudo-header */
2840 udph->check = csum_tcpudp_magic(iph->saddr, iph->daddr,
2841 datalen + 8, IPPROTO_UDP, csum);
2843 if (udph->check == 0)
2844 udph->check = CSUM_MANGLED_0;
2847 #ifdef CONFIG_XFRM
2848 if (!process_ipsec(pkt_dev, skb, protocol))
2849 return NULL;
2850 #endif
2852 return skb;
2855 static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
2856 struct pktgen_dev *pkt_dev)
2858 struct sk_buff *skb = NULL;
2859 __u8 *eth;
2860 struct udphdr *udph;
2861 int datalen, udplen;
2862 struct ipv6hdr *iph;
2863 __be16 protocol = htons(ETH_P_IPV6);
2864 __be32 *mpls;
2865 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2866 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2867 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2868 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2869 u16 queue_map;
2871 if (pkt_dev->nr_labels)
2872 protocol = htons(ETH_P_MPLS_UC);
2874 if (pkt_dev->vlan_id != 0xffff)
2875 protocol = htons(ETH_P_8021Q);
2877 /* Update any of the values, used when we're incrementing various
2878 * fields.
2880 mod_cur_headers(pkt_dev);
2881 queue_map = pkt_dev->cur_queue_map;
2883 skb = pktgen_alloc_skb(odev, pkt_dev);
2884 if (!skb) {
2885 sprintf(pkt_dev->result, "No memory");
2886 return NULL;
2889 prefetchw(skb->data);
2890 skb_reserve(skb, 16);
2892 /* Reserve for ethernet and IP header */
2893 eth = skb_push(skb, 14);
2894 mpls = skb_put(skb, pkt_dev->nr_labels * sizeof(__u32));
2895 if (pkt_dev->nr_labels)
2896 mpls_push(mpls, pkt_dev);
2898 if (pkt_dev->vlan_id != 0xffff) {
2899 if (pkt_dev->svlan_id != 0xffff) {
2900 svlan_tci = skb_put(skb, sizeof(__be16));
2901 *svlan_tci = build_tci(pkt_dev->svlan_id,
2902 pkt_dev->svlan_cfi,
2903 pkt_dev->svlan_p);
2904 svlan_encapsulated_proto = skb_put(skb,
2905 sizeof(__be16));
2906 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
2908 vlan_tci = skb_put(skb, sizeof(__be16));
2909 *vlan_tci = build_tci(pkt_dev->vlan_id,
2910 pkt_dev->vlan_cfi,
2911 pkt_dev->vlan_p);
2912 vlan_encapsulated_proto = skb_put(skb, sizeof(__be16));
2913 *vlan_encapsulated_proto = htons(ETH_P_IPV6);
2916 skb_reset_mac_header(skb);
2917 skb_set_network_header(skb, skb->len);
2918 iph = skb_put(skb, sizeof(struct ipv6hdr));
2920 skb_set_transport_header(skb, skb->len);
2921 udph = skb_put(skb, sizeof(struct udphdr));
2922 skb_set_queue_mapping(skb, queue_map);
2923 skb->priority = pkt_dev->skb_priority;
2925 memcpy(eth, pkt_dev->hh, 12);
2926 *(__be16 *) &eth[12] = protocol;
2928 /* Eth + IPh + UDPh + mpls */
2929 datalen = pkt_dev->cur_pkt_size - 14 -
2930 sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
2931 pkt_dev->pkt_overhead;
2933 if (datalen < 0 || datalen < sizeof(struct pktgen_hdr)) {
2934 datalen = sizeof(struct pktgen_hdr);
2935 net_info_ratelimited("increased datalen to %d\n", datalen);
2938 udplen = datalen + sizeof(struct udphdr);
2939 udph->source = htons(pkt_dev->cur_udp_src);
2940 udph->dest = htons(pkt_dev->cur_udp_dst);
2941 udph->len = htons(udplen);
2942 udph->check = 0;
2944 *(__be32 *) iph = htonl(0x60000000); /* Version + flow */
2946 if (pkt_dev->traffic_class) {
2947 /* Version + traffic class + flow (0) */
2948 *(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
2951 iph->hop_limit = 32;
2953 iph->payload_len = htons(udplen);
2954 iph->nexthdr = IPPROTO_UDP;
2956 iph->daddr = pkt_dev->cur_in6_daddr;
2957 iph->saddr = pkt_dev->cur_in6_saddr;
2959 skb->protocol = protocol;
2960 skb->dev = odev;
2961 skb->pkt_type = PACKET_HOST;
2963 pktgen_finalize_skb(pkt_dev, skb, datalen);
2965 if (!(pkt_dev->flags & F_UDPCSUM)) {
2966 skb->ip_summed = CHECKSUM_NONE;
2967 } else if (odev->features & (NETIF_F_HW_CSUM | NETIF_F_IPV6_CSUM)) {
2968 skb->ip_summed = CHECKSUM_PARTIAL;
2969 skb->csum_start = skb_transport_header(skb) - skb->head;
2970 skb->csum_offset = offsetof(struct udphdr, check);
2971 udph->check = ~csum_ipv6_magic(&iph->saddr, &iph->daddr, udplen, IPPROTO_UDP, 0);
2972 } else {
2973 __wsum csum = skb_checksum(skb, skb_transport_offset(skb), udplen, 0);
2975 /* add protocol-dependent pseudo-header */
2976 udph->check = csum_ipv6_magic(&iph->saddr, &iph->daddr, udplen, IPPROTO_UDP, csum);
2978 if (udph->check == 0)
2979 udph->check = CSUM_MANGLED_0;
2982 return skb;
2985 static struct sk_buff *fill_packet(struct net_device *odev,
2986 struct pktgen_dev *pkt_dev)
2988 if (pkt_dev->flags & F_IPV6)
2989 return fill_packet_ipv6(odev, pkt_dev);
2990 else
2991 return fill_packet_ipv4(odev, pkt_dev);
2994 static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
2996 pkt_dev->seq_num = 1;
2997 pkt_dev->idle_acc = 0;
2998 pkt_dev->sofar = 0;
2999 pkt_dev->tx_bytes = 0;
3000 pkt_dev->errors = 0;
3003 /* Set up structure for sending pkts, clear counters */
3005 static void pktgen_run(struct pktgen_thread *t)
3007 struct pktgen_dev *pkt_dev;
3008 int started = 0;
3010 func_enter();
3012 rcu_read_lock();
3013 list_for_each_entry_rcu(pkt_dev, &t->if_list, list) {
3016 * setup odev and create initial packet.
3018 pktgen_setup_inject(pkt_dev);
3020 if (pkt_dev->odev) {
3021 pktgen_clear_counters(pkt_dev);
3022 pkt_dev->skb = NULL;
3023 pkt_dev->started_at = pkt_dev->next_tx = ktime_get();
3025 set_pkt_overhead(pkt_dev);
3027 strcpy(pkt_dev->result, "Starting");
3028 pkt_dev->running = 1; /* Cranke yeself! */
3029 started++;
3030 } else
3031 strcpy(pkt_dev->result, "Error starting");
3033 rcu_read_unlock();
3034 if (started)
3035 t->control &= ~(T_STOP);
3038 static void pktgen_stop_all_threads_ifs(struct pktgen_net *pn)
3040 struct pktgen_thread *t;
3042 func_enter();
3044 mutex_lock(&pktgen_thread_lock);
3046 list_for_each_entry(t, &pn->pktgen_threads, th_list)
3047 t->control |= T_STOP;
3049 mutex_unlock(&pktgen_thread_lock);
3052 static int thread_is_running(const struct pktgen_thread *t)
3054 const struct pktgen_dev *pkt_dev;
3056 rcu_read_lock();
3057 list_for_each_entry_rcu(pkt_dev, &t->if_list, list)
3058 if (pkt_dev->running) {
3059 rcu_read_unlock();
3060 return 1;
3062 rcu_read_unlock();
3063 return 0;
3066 static int pktgen_wait_thread_run(struct pktgen_thread *t)
3068 while (thread_is_running(t)) {
3070 msleep_interruptible(100);
3072 if (signal_pending(current))
3073 goto signal;
3075 return 1;
3076 signal:
3077 return 0;
3080 static int pktgen_wait_all_threads_run(struct pktgen_net *pn)
3082 struct pktgen_thread *t;
3083 int sig = 1;
3085 mutex_lock(&pktgen_thread_lock);
3087 list_for_each_entry(t, &pn->pktgen_threads, th_list) {
3088 sig = pktgen_wait_thread_run(t);
3089 if (sig == 0)
3090 break;
3093 if (sig == 0)
3094 list_for_each_entry(t, &pn->pktgen_threads, th_list)
3095 t->control |= (T_STOP);
3097 mutex_unlock(&pktgen_thread_lock);
3098 return sig;
3101 static void pktgen_run_all_threads(struct pktgen_net *pn)
3103 struct pktgen_thread *t;
3105 func_enter();
3107 mutex_lock(&pktgen_thread_lock);
3109 list_for_each_entry(t, &pn->pktgen_threads, th_list)
3110 t->control |= (T_RUN);
3112 mutex_unlock(&pktgen_thread_lock);
3114 /* Propagate thread->control */
3115 schedule_timeout_interruptible(msecs_to_jiffies(125));
3117 pktgen_wait_all_threads_run(pn);
3120 static void pktgen_reset_all_threads(struct pktgen_net *pn)
3122 struct pktgen_thread *t;
3124 func_enter();
3126 mutex_lock(&pktgen_thread_lock);
3128 list_for_each_entry(t, &pn->pktgen_threads, th_list)
3129 t->control |= (T_REMDEVALL);
3131 mutex_unlock(&pktgen_thread_lock);
3133 /* Propagate thread->control */
3134 schedule_timeout_interruptible(msecs_to_jiffies(125));
3136 pktgen_wait_all_threads_run(pn);
3139 static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
3141 __u64 bps, mbps, pps;
3142 char *p = pkt_dev->result;
3143 ktime_t elapsed = ktime_sub(pkt_dev->stopped_at,
3144 pkt_dev->started_at);
3145 ktime_t idle = ns_to_ktime(pkt_dev->idle_acc);
3147 p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
3148 (unsigned long long)ktime_to_us(elapsed),
3149 (unsigned long long)ktime_to_us(ktime_sub(elapsed, idle)),
3150 (unsigned long long)ktime_to_us(idle),
3151 (unsigned long long)pkt_dev->sofar,
3152 pkt_dev->cur_pkt_size, nr_frags);
3154 pps = div64_u64(pkt_dev->sofar * NSEC_PER_SEC,
3155 ktime_to_ns(elapsed));
3157 bps = pps * 8 * pkt_dev->cur_pkt_size;
3159 mbps = bps;
3160 do_div(mbps, 1000000);
3161 p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu",
3162 (unsigned long long)pps,
3163 (unsigned long long)mbps,
3164 (unsigned long long)bps,
3165 (unsigned long long)pkt_dev->errors);
3168 /* Set stopped-at timer, remove from running list, do counters & statistics */
3169 static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
3171 int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
3173 if (!pkt_dev->running) {
3174 pr_warn("interface: %s is already stopped\n",
3175 pkt_dev->odevname);
3176 return -EINVAL;
3179 pkt_dev->running = 0;
3180 kfree_skb(pkt_dev->skb);
3181 pkt_dev->skb = NULL;
3182 pkt_dev->stopped_at = ktime_get();
3184 show_results(pkt_dev, nr_frags);
3186 return 0;
3189 static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
3191 struct pktgen_dev *pkt_dev, *best = NULL;
3193 rcu_read_lock();
3194 list_for_each_entry_rcu(pkt_dev, &t->if_list, list) {
3195 if (!pkt_dev->running)
3196 continue;
3197 if (best == NULL)
3198 best = pkt_dev;
3199 else if (ktime_compare(pkt_dev->next_tx, best->next_tx) < 0)
3200 best = pkt_dev;
3202 rcu_read_unlock();
3204 return best;
3207 static void pktgen_stop(struct pktgen_thread *t)
3209 struct pktgen_dev *pkt_dev;
3211 func_enter();
3213 rcu_read_lock();
3215 list_for_each_entry_rcu(pkt_dev, &t->if_list, list) {
3216 pktgen_stop_device(pkt_dev);
3219 rcu_read_unlock();
3223 * one of our devices needs to be removed - find it
3224 * and remove it
3226 static void pktgen_rem_one_if(struct pktgen_thread *t)
3228 struct list_head *q, *n;
3229 struct pktgen_dev *cur;
3231 func_enter();
3233 list_for_each_safe(q, n, &t->if_list) {
3234 cur = list_entry(q, struct pktgen_dev, list);
3236 if (!cur->removal_mark)
3237 continue;
3239 kfree_skb(cur->skb);
3240 cur->skb = NULL;
3242 pktgen_remove_device(t, cur);
3244 break;
3248 static void pktgen_rem_all_ifs(struct pktgen_thread *t)
3250 struct list_head *q, *n;
3251 struct pktgen_dev *cur;
3253 func_enter();
3255 /* Remove all devices, free mem */
3257 list_for_each_safe(q, n, &t->if_list) {
3258 cur = list_entry(q, struct pktgen_dev, list);
3260 kfree_skb(cur->skb);
3261 cur->skb = NULL;
3263 pktgen_remove_device(t, cur);
3267 static void pktgen_rem_thread(struct pktgen_thread *t)
3269 /* Remove from the thread list */
3270 remove_proc_entry(t->tsk->comm, t->net->proc_dir);
3273 static void pktgen_resched(struct pktgen_dev *pkt_dev)
3275 ktime_t idle_start = ktime_get();
3276 schedule();
3277 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_get(), idle_start));
3280 static void pktgen_wait_for_skb(struct pktgen_dev *pkt_dev)
3282 ktime_t idle_start = ktime_get();
3284 while (refcount_read(&(pkt_dev->skb->users)) != 1) {
3285 if (signal_pending(current))
3286 break;
3288 if (need_resched())
3289 pktgen_resched(pkt_dev);
3290 else
3291 cpu_relax();
3293 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_get(), idle_start));
3296 static void pktgen_xmit(struct pktgen_dev *pkt_dev)
3298 unsigned int burst = READ_ONCE(pkt_dev->burst);
3299 struct net_device *odev = pkt_dev->odev;
3300 struct netdev_queue *txq;
3301 struct sk_buff *skb;
3302 int ret;
3304 /* If device is offline, then don't send */
3305 if (unlikely(!netif_running(odev) || !netif_carrier_ok(odev))) {
3306 pktgen_stop_device(pkt_dev);
3307 return;
3310 /* This is max DELAY, this has special meaning of
3311 * "never transmit"
3313 if (unlikely(pkt_dev->delay == ULLONG_MAX)) {
3314 pkt_dev->next_tx = ktime_add_ns(ktime_get(), ULONG_MAX);
3315 return;
3318 /* If no skb or clone count exhausted then get new one */
3319 if (!pkt_dev->skb || (pkt_dev->last_ok &&
3320 ++pkt_dev->clone_count >= pkt_dev->clone_skb)) {
3321 /* build a new pkt */
3322 kfree_skb(pkt_dev->skb);
3324 pkt_dev->skb = fill_packet(odev, pkt_dev);
3325 if (pkt_dev->skb == NULL) {
3326 pr_err("ERROR: couldn't allocate skb in fill_packet\n");
3327 schedule();
3328 pkt_dev->clone_count--; /* back out increment, OOM */
3329 return;
3331 pkt_dev->last_pkt_size = pkt_dev->skb->len;
3332 pkt_dev->clone_count = 0; /* reset counter */
3335 if (pkt_dev->delay && pkt_dev->last_ok)
3336 spin(pkt_dev, pkt_dev->next_tx);
3338 if (pkt_dev->xmit_mode == M_NETIF_RECEIVE) {
3339 skb = pkt_dev->skb;
3340 skb->protocol = eth_type_trans(skb, skb->dev);
3341 refcount_add(burst, &skb->users);
3342 local_bh_disable();
3343 do {
3344 ret = netif_receive_skb(skb);
3345 if (ret == NET_RX_DROP)
3346 pkt_dev->errors++;
3347 pkt_dev->sofar++;
3348 pkt_dev->seq_num++;
3349 if (refcount_read(&skb->users) != burst) {
3350 /* skb was queued by rps/rfs or taps,
3351 * so cannot reuse this skb
3353 WARN_ON(refcount_sub_and_test(burst - 1, &skb->users));
3354 /* get out of the loop and wait
3355 * until skb is consumed
3357 break;
3359 /* skb was 'freed' by stack, so clean few
3360 * bits and reuse it
3362 skb_reset_tc(skb);
3363 } while (--burst > 0);
3364 goto out; /* Skips xmit_mode M_START_XMIT */
3365 } else if (pkt_dev->xmit_mode == M_QUEUE_XMIT) {
3366 local_bh_disable();
3367 refcount_inc(&pkt_dev->skb->users);
3369 ret = dev_queue_xmit(pkt_dev->skb);
3370 switch (ret) {
3371 case NET_XMIT_SUCCESS:
3372 pkt_dev->sofar++;
3373 pkt_dev->seq_num++;
3374 pkt_dev->tx_bytes += pkt_dev->last_pkt_size;
3375 break;
3376 case NET_XMIT_DROP:
3377 case NET_XMIT_CN:
3378 /* These are all valid return codes for a qdisc but
3379 * indicate packets are being dropped or will likely
3380 * be dropped soon.
3382 case NETDEV_TX_BUSY:
3383 /* qdisc may call dev_hard_start_xmit directly in cases
3384 * where no queues exist e.g. loopback device, virtual
3385 * devices, etc. In this case we need to handle
3386 * NETDEV_TX_ codes.
3388 default:
3389 pkt_dev->errors++;
3390 net_info_ratelimited("%s xmit error: %d\n",
3391 pkt_dev->odevname, ret);
3392 break;
3394 goto out;
3397 txq = skb_get_tx_queue(odev, pkt_dev->skb);
3399 local_bh_disable();
3401 HARD_TX_LOCK(odev, txq, smp_processor_id());
3403 if (unlikely(netif_xmit_frozen_or_drv_stopped(txq))) {
3404 ret = NETDEV_TX_BUSY;
3405 pkt_dev->last_ok = 0;
3406 goto unlock;
3408 refcount_add(burst, &pkt_dev->skb->users);
3410 xmit_more:
3411 ret = netdev_start_xmit(pkt_dev->skb, odev, txq, --burst > 0);
3413 switch (ret) {
3414 case NETDEV_TX_OK:
3415 pkt_dev->last_ok = 1;
3416 pkt_dev->sofar++;
3417 pkt_dev->seq_num++;
3418 pkt_dev->tx_bytes += pkt_dev->last_pkt_size;
3419 if (burst > 0 && !netif_xmit_frozen_or_drv_stopped(txq))
3420 goto xmit_more;
3421 break;
3422 case NET_XMIT_DROP:
3423 case NET_XMIT_CN:
3424 /* skb has been consumed */
3425 pkt_dev->errors++;
3426 break;
3427 default: /* Drivers are not supposed to return other values! */
3428 net_info_ratelimited("%s xmit error: %d\n",
3429 pkt_dev->odevname, ret);
3430 pkt_dev->errors++;
3431 /* fallthru */
3432 case NETDEV_TX_BUSY:
3433 /* Retry it next time */
3434 refcount_dec(&(pkt_dev->skb->users));
3435 pkt_dev->last_ok = 0;
3437 if (unlikely(burst))
3438 WARN_ON(refcount_sub_and_test(burst, &pkt_dev->skb->users));
3439 unlock:
3440 HARD_TX_UNLOCK(odev, txq);
3442 out:
3443 local_bh_enable();
3445 /* If pkt_dev->count is zero, then run forever */
3446 if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
3447 pktgen_wait_for_skb(pkt_dev);
3449 /* Done with this */
3450 pktgen_stop_device(pkt_dev);
3455 * Main loop of the thread goes here
3458 static int pktgen_thread_worker(void *arg)
3460 DEFINE_WAIT(wait);
3461 struct pktgen_thread *t = arg;
3462 struct pktgen_dev *pkt_dev = NULL;
3463 int cpu = t->cpu;
3465 BUG_ON(smp_processor_id() != cpu);
3467 init_waitqueue_head(&t->queue);
3468 complete(&t->start_done);
3470 pr_debug("starting pktgen/%d: pid=%d\n", cpu, task_pid_nr(current));
3472 set_freezable();
3474 while (!kthread_should_stop()) {
3475 pkt_dev = next_to_run(t);
3477 if (unlikely(!pkt_dev && t->control == 0)) {
3478 if (t->net->pktgen_exiting)
3479 break;
3480 wait_event_interruptible_timeout(t->queue,
3481 t->control != 0,
3482 HZ/10);
3483 try_to_freeze();
3484 continue;
3487 if (likely(pkt_dev)) {
3488 pktgen_xmit(pkt_dev);
3490 if (need_resched())
3491 pktgen_resched(pkt_dev);
3492 else
3493 cpu_relax();
3496 if (t->control & T_STOP) {
3497 pktgen_stop(t);
3498 t->control &= ~(T_STOP);
3501 if (t->control & T_RUN) {
3502 pktgen_run(t);
3503 t->control &= ~(T_RUN);
3506 if (t->control & T_REMDEVALL) {
3507 pktgen_rem_all_ifs(t);
3508 t->control &= ~(T_REMDEVALL);
3511 if (t->control & T_REMDEV) {
3512 pktgen_rem_one_if(t);
3513 t->control &= ~(T_REMDEV);
3516 try_to_freeze();
3519 pr_debug("%s stopping all device\n", t->tsk->comm);
3520 pktgen_stop(t);
3522 pr_debug("%s removing all device\n", t->tsk->comm);
3523 pktgen_rem_all_ifs(t);
3525 pr_debug("%s removing thread\n", t->tsk->comm);
3526 pktgen_rem_thread(t);
3528 return 0;
3531 static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
3532 const char *ifname, bool exact)
3534 struct pktgen_dev *p, *pkt_dev = NULL;
3535 size_t len = strlen(ifname);
3537 rcu_read_lock();
3538 list_for_each_entry_rcu(p, &t->if_list, list)
3539 if (strncmp(p->odevname, ifname, len) == 0) {
3540 if (p->odevname[len]) {
3541 if (exact || p->odevname[len] != '@')
3542 continue;
3544 pkt_dev = p;
3545 break;
3548 rcu_read_unlock();
3549 pr_debug("find_dev(%s) returning %p\n", ifname, pkt_dev);
3550 return pkt_dev;
3554 * Adds a dev at front of if_list.
3557 static int add_dev_to_thread(struct pktgen_thread *t,
3558 struct pktgen_dev *pkt_dev)
3560 int rv = 0;
3562 /* This function cannot be called concurrently, as its called
3563 * under pktgen_thread_lock mutex, but it can run from
3564 * userspace on another CPU than the kthread. The if_lock()
3565 * is used here to sync with concurrent instances of
3566 * _rem_dev_from_if_list() invoked via kthread, which is also
3567 * updating the if_list */
3568 if_lock(t);
3570 if (pkt_dev->pg_thread) {
3571 pr_err("ERROR: already assigned to a thread\n");
3572 rv = -EBUSY;
3573 goto out;
3576 pkt_dev->running = 0;
3577 pkt_dev->pg_thread = t;
3578 list_add_rcu(&pkt_dev->list, &t->if_list);
3580 out:
3581 if_unlock(t);
3582 return rv;
3585 /* Called under thread lock */
3587 static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
3589 struct pktgen_dev *pkt_dev;
3590 int err;
3591 int node = cpu_to_node(t->cpu);
3593 /* We don't allow a device to be on several threads */
3595 pkt_dev = __pktgen_NN_threads(t->net, ifname, FIND);
3596 if (pkt_dev) {
3597 pr_err("ERROR: interface already used\n");
3598 return -EBUSY;
3601 pkt_dev = kzalloc_node(sizeof(struct pktgen_dev), GFP_KERNEL, node);
3602 if (!pkt_dev)
3603 return -ENOMEM;
3605 strcpy(pkt_dev->odevname, ifname);
3606 pkt_dev->flows = vzalloc_node(array_size(MAX_CFLOWS,
3607 sizeof(struct flow_state)),
3608 node);
3609 if (pkt_dev->flows == NULL) {
3610 kfree(pkt_dev);
3611 return -ENOMEM;
3614 pkt_dev->removal_mark = 0;
3615 pkt_dev->nfrags = 0;
3616 pkt_dev->delay = pg_delay_d;
3617 pkt_dev->count = pg_count_d;
3618 pkt_dev->sofar = 0;
3619 pkt_dev->udp_src_min = 9; /* sink port */
3620 pkt_dev->udp_src_max = 9;
3621 pkt_dev->udp_dst_min = 9;
3622 pkt_dev->udp_dst_max = 9;
3623 pkt_dev->vlan_p = 0;
3624 pkt_dev->vlan_cfi = 0;
3625 pkt_dev->vlan_id = 0xffff;
3626 pkt_dev->svlan_p = 0;
3627 pkt_dev->svlan_cfi = 0;
3628 pkt_dev->svlan_id = 0xffff;
3629 pkt_dev->burst = 1;
3630 pkt_dev->node = -1;
3632 err = pktgen_setup_dev(t->net, pkt_dev, ifname);
3633 if (err)
3634 goto out1;
3635 if (pkt_dev->odev->priv_flags & IFF_TX_SKB_SHARING)
3636 pkt_dev->clone_skb = pg_clone_skb_d;
3638 pkt_dev->entry = proc_create_data(ifname, 0600, t->net->proc_dir,
3639 &pktgen_if_fops, pkt_dev);
3640 if (!pkt_dev->entry) {
3641 pr_err("cannot create %s/%s procfs entry\n",
3642 PG_PROC_DIR, ifname);
3643 err = -EINVAL;
3644 goto out2;
3646 #ifdef CONFIG_XFRM
3647 pkt_dev->ipsmode = XFRM_MODE_TRANSPORT;
3648 pkt_dev->ipsproto = IPPROTO_ESP;
3650 /* xfrm tunnel mode needs additional dst to extract outter
3651 * ip header protocol/ttl/id field, here creat a phony one.
3652 * instead of looking for a valid rt, which definitely hurting
3653 * performance under such circumstance.
3655 pkt_dev->dstops.family = AF_INET;
3656 pkt_dev->xdst.u.dst.dev = pkt_dev->odev;
3657 dst_init_metrics(&pkt_dev->xdst.u.dst, pktgen_dst_metrics, false);
3658 pkt_dev->xdst.child = &pkt_dev->xdst.u.dst;
3659 pkt_dev->xdst.u.dst.ops = &pkt_dev->dstops;
3660 #endif
3662 return add_dev_to_thread(t, pkt_dev);
3663 out2:
3664 dev_put(pkt_dev->odev);
3665 out1:
3666 #ifdef CONFIG_XFRM
3667 free_SAs(pkt_dev);
3668 #endif
3669 vfree(pkt_dev->flows);
3670 kfree(pkt_dev);
3671 return err;
3674 static int __net_init pktgen_create_thread(int cpu, struct pktgen_net *pn)
3676 struct pktgen_thread *t;
3677 struct proc_dir_entry *pe;
3678 struct task_struct *p;
3680 t = kzalloc_node(sizeof(struct pktgen_thread), GFP_KERNEL,
3681 cpu_to_node(cpu));
3682 if (!t) {
3683 pr_err("ERROR: out of memory, can't create new thread\n");
3684 return -ENOMEM;
3687 mutex_init(&t->if_lock);
3688 t->cpu = cpu;
3690 INIT_LIST_HEAD(&t->if_list);
3692 list_add_tail(&t->th_list, &pn->pktgen_threads);
3693 init_completion(&t->start_done);
3695 p = kthread_create_on_node(pktgen_thread_worker,
3697 cpu_to_node(cpu),
3698 "kpktgend_%d", cpu);
3699 if (IS_ERR(p)) {
3700 pr_err("kernel_thread() failed for cpu %d\n", t->cpu);
3701 list_del(&t->th_list);
3702 kfree(t);
3703 return PTR_ERR(p);
3705 kthread_bind(p, cpu);
3706 t->tsk = p;
3708 pe = proc_create_data(t->tsk->comm, 0600, pn->proc_dir,
3709 &pktgen_thread_fops, t);
3710 if (!pe) {
3711 pr_err("cannot create %s/%s procfs entry\n",
3712 PG_PROC_DIR, t->tsk->comm);
3713 kthread_stop(p);
3714 list_del(&t->th_list);
3715 kfree(t);
3716 return -EINVAL;
3719 t->net = pn;
3720 get_task_struct(p);
3721 wake_up_process(p);
3722 wait_for_completion(&t->start_done);
3724 return 0;
3728 * Removes a device from the thread if_list.
3730 static void _rem_dev_from_if_list(struct pktgen_thread *t,
3731 struct pktgen_dev *pkt_dev)
3733 struct list_head *q, *n;
3734 struct pktgen_dev *p;
3736 if_lock(t);
3737 list_for_each_safe(q, n, &t->if_list) {
3738 p = list_entry(q, struct pktgen_dev, list);
3739 if (p == pkt_dev)
3740 list_del_rcu(&p->list);
3742 if_unlock(t);
3745 static int pktgen_remove_device(struct pktgen_thread *t,
3746 struct pktgen_dev *pkt_dev)
3748 pr_debug("remove_device pkt_dev=%p\n", pkt_dev);
3750 if (pkt_dev->running) {
3751 pr_warn("WARNING: trying to remove a running interface, stopping it now\n");
3752 pktgen_stop_device(pkt_dev);
3755 /* Dis-associate from the interface */
3757 if (pkt_dev->odev) {
3758 dev_put(pkt_dev->odev);
3759 pkt_dev->odev = NULL;
3762 /* Remove proc before if_list entry, because add_device uses
3763 * list to determine if interface already exist, avoid race
3764 * with proc_create_data() */
3765 proc_remove(pkt_dev->entry);
3767 /* And update the thread if_list */
3768 _rem_dev_from_if_list(t, pkt_dev);
3770 #ifdef CONFIG_XFRM
3771 free_SAs(pkt_dev);
3772 #endif
3773 vfree(pkt_dev->flows);
3774 if (pkt_dev->page)
3775 put_page(pkt_dev->page);
3776 kfree_rcu(pkt_dev, rcu);
3777 return 0;
3780 static int __net_init pg_net_init(struct net *net)
3782 struct pktgen_net *pn = net_generic(net, pg_net_id);
3783 struct proc_dir_entry *pe;
3784 int cpu, ret = 0;
3786 pn->net = net;
3787 INIT_LIST_HEAD(&pn->pktgen_threads);
3788 pn->pktgen_exiting = false;
3789 pn->proc_dir = proc_mkdir(PG_PROC_DIR, pn->net->proc_net);
3790 if (!pn->proc_dir) {
3791 pr_warn("cannot create /proc/net/%s\n", PG_PROC_DIR);
3792 return -ENODEV;
3794 pe = proc_create(PGCTRL, 0600, pn->proc_dir, &pktgen_fops);
3795 if (pe == NULL) {
3796 pr_err("cannot create %s procfs entry\n", PGCTRL);
3797 ret = -EINVAL;
3798 goto remove;
3801 for_each_online_cpu(cpu) {
3802 int err;
3804 err = pktgen_create_thread(cpu, pn);
3805 if (err)
3806 pr_warn("Cannot create thread for cpu %d (%d)\n",
3807 cpu, err);
3810 if (list_empty(&pn->pktgen_threads)) {
3811 pr_err("Initialization failed for all threads\n");
3812 ret = -ENODEV;
3813 goto remove_entry;
3816 return 0;
3818 remove_entry:
3819 remove_proc_entry(PGCTRL, pn->proc_dir);
3820 remove:
3821 remove_proc_entry(PG_PROC_DIR, pn->net->proc_net);
3822 return ret;
3825 static void __net_exit pg_net_exit(struct net *net)
3827 struct pktgen_net *pn = net_generic(net, pg_net_id);
3828 struct pktgen_thread *t;
3829 struct list_head *q, *n;
3830 LIST_HEAD(list);
3832 /* Stop all interfaces & threads */
3833 pn->pktgen_exiting = true;
3835 mutex_lock(&pktgen_thread_lock);
3836 list_splice_init(&pn->pktgen_threads, &list);
3837 mutex_unlock(&pktgen_thread_lock);
3839 list_for_each_safe(q, n, &list) {
3840 t = list_entry(q, struct pktgen_thread, th_list);
3841 list_del(&t->th_list);
3842 kthread_stop(t->tsk);
3843 put_task_struct(t->tsk);
3844 kfree(t);
3847 remove_proc_entry(PGCTRL, pn->proc_dir);
3848 remove_proc_entry(PG_PROC_DIR, pn->net->proc_net);
3851 static struct pernet_operations pg_net_ops = {
3852 .init = pg_net_init,
3853 .exit = pg_net_exit,
3854 .id = &pg_net_id,
3855 .size = sizeof(struct pktgen_net),
3858 static int __init pg_init(void)
3860 int ret = 0;
3862 pr_info("%s", version);
3863 ret = register_pernet_subsys(&pg_net_ops);
3864 if (ret)
3865 return ret;
3866 ret = register_netdevice_notifier(&pktgen_notifier_block);
3867 if (ret)
3868 unregister_pernet_subsys(&pg_net_ops);
3870 return ret;
3873 static void __exit pg_cleanup(void)
3875 unregister_netdevice_notifier(&pktgen_notifier_block);
3876 unregister_pernet_subsys(&pg_net_ops);
3877 /* Don't need rcu_barrier() due to use of kfree_rcu() */
3880 module_init(pg_init);
3881 module_exit(pg_cleanup);
3883 MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se>");
3884 MODULE_DESCRIPTION("Packet Generator tool");
3885 MODULE_LICENSE("GPL");
3886 MODULE_VERSION(VERSION);
3887 module_param(pg_count_d, int, 0);
3888 MODULE_PARM_DESC(pg_count_d, "Default number of packets to inject");
3889 module_param(pg_delay_d, int, 0);
3890 MODULE_PARM_DESC(pg_delay_d, "Default delay between packets (nanoseconds)");
3891 module_param(pg_clone_skb_d, int, 0);
3892 MODULE_PARM_DESC(pg_clone_skb_d, "Default number of copies of the same packet");
3893 module_param(debug, int, 0);
3894 MODULE_PARM_DESC(debug, "Enable debugging of pktgen module");