1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Authors:
4 * Copyright 2001, 2002 by Robert Olsson <robert.olsson@its.uu.se>
5 * Uppsala University and
6 * Swedish University of Agricultural Sciences
7 *
8 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
9 * Ben Greear <greearb@candelatech.com>
10 * Jens Låås <jens.laas@data.slu.se>
11 *
12 * A tool for loading the network with preconfigurated packets.
13 * The tool is implemented as a linux module. Parameters are output
14 * device, delay (to hard_xmit), number of packets, and whether
15 * to use multiple SKBs or just the same one.
16 * pktgen uses the installed interface's output routine.
17 *
18 * Additional hacking by:
19 *
20 * Jens.Laas@data.slu.se
21 * Improved by ANK. 010120.
22 * Improved by ANK even more. 010212.
23 * MAC address typo fixed. 010417 --ro
24 * Integrated. 020301 --DaveM
25 * Added multiskb option 020301 --DaveM
26 * Scaling of results. 020417--sigurdur@linpro.no
27 * Significant re-work of the module:
28 * * Convert to threaded model to more efficiently be able to transmit
29 * and receive on multiple interfaces at once.
30 * * Converted many counters to __u64 to allow longer runs.
31 * * Allow configuration of ranges, like min/max IP address, MACs,
32 * and UDP-ports, for both source and destination, and can
33 * set to use a random distribution or sequentially walk the range.
34 * * Can now change most values after starting.
35 * * Place 12-byte packet in UDP payload with magic number,
36 * sequence number, and timestamp.
37 * * Add receiver code that detects dropped pkts, re-ordered pkts, and
38 * latencies (with micro-second) precision.
39 * * Add IOCTL interface to easily get counters & configuration.
40 * --Ben Greear <greearb@candelatech.com>
41 *
42 * Renamed multiskb to clone_skb and cleaned up sending core for two distinct
43 * skb modes. A clone_skb=0 mode for Ben "ranges" work and a clone_skb != 0
44 * as a "fastpath" with a configurable number of clones after alloc's.
45 * clone_skb=0 means all packets are allocated this also means ranges time
46 * stamps etc can be used. clone_skb=100 means 1 malloc is followed by 100
47 * clones.
48 *
49 * Also moved to /proc/net/pktgen/
50 * --ro
51 *
52 * Sept 10: Fixed threading/locking. Lots of bone-headed and more clever
53 * mistakes. Also merged in DaveM's patch in the -pre6 patch.
54 * --Ben Greear <greearb@candelatech.com>
55 *
56 * Integrated to 2.5.x 021029 --Lucio Maciel (luciomaciel@zipmail.com.br)
57 *
58 * 021124 Finished major redesign and rewrite for new functionality.
59 * See Documentation/networking/pktgen.rst for how to use this.
60 *
61 * The new operation:
62 * For each CPU one thread/process is created at start. This process checks
63 * for running devices in the if_list and sends packets until count is 0 it
64 * also the thread checks the thread->control which is used for inter-process
65 * communication. controlling process "posts" operations to the threads this
66 * way.
67 * The if_list is RCU protected, and the if_lock remains to protect updating
68 * of if_list, from "add_device" as it invoked from userspace (via proc write).
69 *
70 * By design there should only be *one* "controlling" process. In practice
71 * multiple write accesses gives unpredictable result. Understood by "write"
72 * to /proc gives result code thats should be read be the "writer".
73 * For practical use this should be no problem.
74 *
75 * Note when adding devices to a specific CPU there good idea to also assign
76 * /proc/irq/XX/smp_affinity so TX-interrupts gets bound to the same CPU.
77 * --ro
78 *
79 * Fix refcount off by one if first packet fails, potential null deref,
80 * memleak 030710- KJP
81 *
82 * First "ranges" functionality for ipv6 030726 --ro
83 *
84 * Included flow support. 030802 ANK.
85 *
86 * Fixed unaligned access on IA-64 Grant Grundler <grundler@parisc-linux.org>
87 *
88 * Remove if fix from added Harald Welte <laforge@netfilter.org> 040419
89 * ia64 compilation fix from Aron Griffis <aron@hp.com> 040604
90 *
91 * New xmit() return, do_div and misc clean up by Stephen Hemminger
92 * <shemminger@osdl.org> 040923
93 *
94 * Randy Dunlap fixed u64 printk compiler warning
95 *
96 * Remove FCS from BW calculation. Lennert Buytenhek <buytenh@wantstofly.org>
97 * New time handling. Lennert Buytenhek <buytenh@wantstofly.org> 041213
98 *
99 * Corrections from Nikolai Malykh (nmalykh@bilim.com)
100 * Removed unused flags F_SET_SRCMAC & F_SET_SRCIP 041230
101 *
102 * interruptible_sleep_on_timeout() replaced Nishanth Aravamudan <nacc@us.ibm.com>
103 * 050103
104 *
105 * MPLS support by Steven Whitehouse <steve@chygwyn.com>
106 *
107 * 802.1Q/Q-in-Q support by Francesco Fondelli (FF) <francesco.fondelli@gmail.com>
108 *
109 * Fixed src_mac command to set source mac of packet to value specified in
110 * command by Adit Ranadive <adit.262@gmail.com>
111 */
112
113 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
114
115 #include <linux/sys.h>
116 #include <linux/types.h>
117 #include <linux/module.h>
118 #include <linux/moduleparam.h>
119 #include <linux/kernel.h>
120 #include <linux/mutex.h>
121 #include <linux/sched.h>
122 #include <linux/slab.h>
123 #include <linux/vmalloc.h>
124 #include <linux/unistd.h>
125 #include <linux/string.h>
126 #include <linux/ptrace.h>
127 #include <linux/errno.h>
128 #include <linux/ioport.h>
129 #include <linux/interrupt.h>
130 #include <linux/capability.h>
131 #include <linux/hrtimer.h>
132 #include <linux/freezer.h>
133 #include <linux/delay.h>
134 #include <linux/timer.h>
135 #include <linux/list.h>
136 #include <linux/init.h>
137 #include <linux/skbuff.h>
138 #include <linux/netdevice.h>
139 #include <linux/inet.h>
140 #include <linux/inetdevice.h>
141 #include <linux/rtnetlink.h>
142 #include <linux/if_arp.h>
143 #include <linux/if_vlan.h>
144 #include <linux/in.h>
145 #include <linux/ip.h>
146 #include <linux/ipv6.h>
147 #include <linux/udp.h>
148 #include <linux/proc_fs.h>
149 #include <linux/seq_file.h>
150 #include <linux/wait.h>
151 #include <linux/etherdevice.h>
152 #include <linux/kthread.h>
153 #include <linux/prefetch.h>
154 #include <linux/mmzone.h>
155 #include <net/net_namespace.h>
156 #include <net/checksum.h>
157 #include <net/ipv6.h>
158 #include <net/udp.h>
159 #include <net/ip6_checksum.h>
160 #include <net/addrconf.h>
161 #ifdef CONFIG_XFRM
162 #include <net/xfrm.h>
163 #endif
164 #include <net/netns/generic.h>
165 #include <asm/byteorder.h>
166 #include <linux/rcupdate.h>
167 #include <linux/bitops.h>
168 #include <linux/io.h>
169 #include <linux/timex.h>
170 #include <linux/uaccess.h>
171 #include <asm/dma.h>
172 #include <asm/div64.h> /* do_div */
173
174 #define VERSION "2.75"
175 #define IP_NAME_SZ 32
176 #define MAX_MPLS_LABELS 16 /* This is the max label stack depth */
177 #define MPLS_STACK_BOTTOM htonl(0x00000100)
178 /* Max number of internet mix entries that can be specified in imix_weights. */
179 #define MAX_IMIX_ENTRIES 20
180 #define IMIX_PRECISION 100 /* Precision of IMIX distribution */
181
182 #define func_enter() pr_debug("entering %s\n", __func__);
183
184 #define PKT_FLAGS \
185 pf(IPV6) /* Interface in IPV6 Mode */ \
186 pf(IPSRC_RND) /* IP-Src Random */ \
187 pf(IPDST_RND) /* IP-Dst Random */ \
188 pf(TXSIZE_RND) /* Transmit size is random */ \
189 pf(UDPSRC_RND) /* UDP-Src Random */ \
190 pf(UDPDST_RND) /* UDP-Dst Random */ \
191 pf(UDPCSUM) /* Include UDP checksum */ \
192 pf(NO_TIMESTAMP) /* Don't timestamp packets (default TS) */ \
193 pf(MPLS_RND) /* Random MPLS labels */ \
194 pf(QUEUE_MAP_RND) /* queue map Random */ \
195 pf(QUEUE_MAP_CPU) /* queue map mirrors smp_processor_id() */ \
196 pf(FLOW_SEQ) /* Sequential flows */ \
197 pf(IPSEC) /* ipsec on for flows */ \
198 pf(MACSRC_RND) /* MAC-Src Random */ \
199 pf(MACDST_RND) /* MAC-Dst Random */ \
200 pf(VID_RND) /* Random VLAN ID */ \
201 pf(SVID_RND) /* Random SVLAN ID */ \
202 pf(NODE) /* Node memory alloc*/ \
203
204 #define pf(flag) flag##_SHIFT,
205 enum pkt_flags {
206 PKT_FLAGS
207 };
208 #undef pf
209
210 /* Device flag bits */
211 #define pf(flag) static const __u32 F_##flag = (1<<flag##_SHIFT);
212 PKT_FLAGS
213 #undef pf
214
215 #define pf(flag) __stringify(flag),
216 static char *pkt_flag_names[] = {
217 PKT_FLAGS
218 };
219 #undef pf
220
221 #define NR_PKT_FLAGS ARRAY_SIZE(pkt_flag_names)
222
223 /* Thread control flag bits */
224 #define T_STOP (1<<0) /* Stop run */
225 #define T_RUN (1<<1) /* Start run */
226 #define T_REMDEVALL (1<<2) /* Remove all devs */
227 #define T_REMDEV (1<<3) /* Remove one dev */
228
229 /* Xmit modes */
230 #define M_START_XMIT 0 /* Default normal TX */
231 #define M_NETIF_RECEIVE 1 /* Inject packets into stack */
232 #define M_QUEUE_XMIT 2 /* Inject packet into qdisc */
233
234 /* If lock -- protects updating of if_list */
235 #define if_lock(t) mutex_lock(&(t->if_lock));
236 #define if_unlock(t) mutex_unlock(&(t->if_lock));
237
238 /* Used to help with determining the pkts on receive */
239 #define PKTGEN_MAGIC 0xbe9be955
240 #define PG_PROC_DIR "pktgen"
241 #define PGCTRL "pgctrl"
242
243 #define MAX_CFLOWS 65536
244
245 #define VLAN_TAG_SIZE(x) ((x)->vlan_id == 0xffff ? 0 : 4)
246 #define SVLAN_TAG_SIZE(x) ((x)->svlan_id == 0xffff ? 0 : 4)
247
248 struct imix_pkt {
249 u64 size;
250 u64 weight;
251 u64 count_so_far;
252 };
253
254 struct flow_state {
255 __be32 cur_daddr;
256 int count;
257 #ifdef CONFIG_XFRM
258 struct xfrm_state *x;
259 #endif
260 __u32 flags;
261 };
262
263 /* flow flag bits */
264 #define F_INIT (1<<0) /* flow has been initialized */
265
266 struct pktgen_dev {
267 /*
268 * Try to keep frequent/infrequent used vars. separated.
269 */
270 struct proc_dir_entry *entry; /* proc file */
271 struct pktgen_thread *pg_thread;/* the owner */
272 struct list_head list; /* chaining in the thread's run-queue */
273 struct rcu_head rcu; /* freed by RCU */
274
275 int running; /* if false, the test will stop */
276
277 /* If min != max, then we will either do a linear iteration, or
278 * we will do a random selection from within the range.
279 */
280 __u32 flags;
281 int xmit_mode;
282 int min_pkt_size;
283 int max_pkt_size;
284 int pkt_overhead; /* overhead for MPLS, VLANs, IPSEC etc */
285 int nfrags;
286 int removal_mark; /* non-zero => the device is marked for
287 * removal by worker thread */
288
289 struct page *page;
290 u64 delay; /* nano-seconds */
291
292 __u64 count; /* Default No packets to send */
293 __u64 sofar; /* How many pkts we've sent so far */
294 __u64 tx_bytes; /* How many bytes we've transmitted */
295 __u64 errors; /* Errors when trying to transmit, */
296
297 /* runtime counters relating to clone_skb */
298
299 __u32 clone_count;
300 int last_ok; /* Was last skb sent?
301 * Or a failed transmit of some sort?
302 * This will keep sequence numbers in order
303 */
304 ktime_t next_tx;
305 ktime_t started_at;
306 ktime_t stopped_at;
307 u64 idle_acc; /* nano-seconds */
308
309 __u32 seq_num;
310
311 int clone_skb; /*
312 * Use multiple SKBs during packet gen.
313 * If this number is greater than 1, then
314 * that many copies of the same packet will be
315 * sent before a new packet is allocated.
316 * If you want to send 1024 identical packets
317 * before creating a new packet,
318 * set clone_skb to 1024.
319 */
320
321 char dst_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
322 char dst_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
323 char src_min[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
324 char src_max[IP_NAME_SZ]; /* IP, ie 1.2.3.4 */
325
326 struct in6_addr in6_saddr;
327 struct in6_addr in6_daddr;
328 struct in6_addr cur_in6_daddr;
329 struct in6_addr cur_in6_saddr;
330 /* For ranges */
331 struct in6_addr min_in6_daddr;
332 struct in6_addr max_in6_daddr;
333 struct in6_addr min_in6_saddr;
334 struct in6_addr max_in6_saddr;
335
336 /* If we're doing ranges, random or incremental, then this
337 * defines the min/max for those ranges.
338 */
339 __be32 saddr_min; /* inclusive, source IP address */
340 __be32 saddr_max; /* exclusive, source IP address */
341 __be32 daddr_min; /* inclusive, dest IP address */
342 __be32 daddr_max; /* exclusive, dest IP address */
343
344 __u16 udp_src_min; /* inclusive, source UDP port */
345 __u16 udp_src_max; /* exclusive, source UDP port */
346 __u16 udp_dst_min; /* inclusive, dest UDP port */
347 __u16 udp_dst_max; /* exclusive, dest UDP port */
348
349 /* DSCP + ECN */
350 __u8 tos; /* six MSB of (former) IPv4 TOS
351 are for dscp codepoint */
352 __u8 traffic_class; /* ditto for the (former) Traffic Class in IPv6
353 (see RFC 3260, sec. 4) */
354
355 /* IMIX */
356 unsigned int n_imix_entries;
357 struct imix_pkt imix_entries[MAX_IMIX_ENTRIES];
358 /* Maps 0-IMIX_PRECISION range to imix_entry based on probability*/
359 __u8 imix_distribution[IMIX_PRECISION];
360
361 /* MPLS */
362 unsigned int nr_labels; /* Depth of stack, 0 = no MPLS */
363 __be32 labels[MAX_MPLS_LABELS];
364
365 /* VLAN/SVLAN (802.1Q/Q-in-Q) */
366 __u8 vlan_p;
367 __u8 vlan_cfi;
368 __u16 vlan_id; /* 0xffff means no vlan tag */
369
370 __u8 svlan_p;
371 __u8 svlan_cfi;
372 __u16 svlan_id; /* 0xffff means no svlan tag */
373
374 __u32 src_mac_count; /* How many MACs to iterate through */
375 __u32 dst_mac_count; /* How many MACs to iterate through */
376
377 unsigned char dst_mac[ETH_ALEN];
378 unsigned char src_mac[ETH_ALEN];
379
380 __u32 cur_dst_mac_offset;
381 __u32 cur_src_mac_offset;
382 __be32 cur_saddr;
383 __be32 cur_daddr;
384 __u16 ip_id;
385 __u16 cur_udp_dst;
386 __u16 cur_udp_src;
387 __u16 cur_queue_map;
388 __u32 cur_pkt_size;
389 __u32 last_pkt_size;
390
391 __u8 hh[14];
392 /* = {
393 0x00, 0x80, 0xC8, 0x79, 0xB3, 0xCB,
394
395 We fill in SRC address later
396 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
397 0x08, 0x00
398 };
399 */
400 __u16 pad; /* pad out the hh struct to an even 16 bytes */
401
402 struct sk_buff *skb; /* skb we are to transmit next, used for when we
403 * are transmitting the same one multiple times
404 */
405 struct net_device *odev; /* The out-going device.
406 * Note that the device should have it's
407 * pg_info pointer pointing back to this
408 * device.
409 * Set when the user specifies the out-going
410 * device name (not when the inject is
411 * started as it used to do.)
412 */
413 netdevice_tracker dev_tracker;
414 char odevname[32];
415 struct flow_state *flows;
416 unsigned int cflows; /* Concurrent flows (config) */
417 unsigned int lflow; /* Flow length (config) */
418 unsigned int nflows; /* accumulated flows (stats) */
419 unsigned int curfl; /* current sequenced flow (state)*/
420
421 u16 queue_map_min;
422 u16 queue_map_max;
423 __u32 skb_priority; /* skb priority field */
424 unsigned int burst; /* number of duplicated packets to burst */
425 int node; /* Memory node */
426
427 #ifdef CONFIG_XFRM
428 __u8 ipsmode; /* IPSEC mode (config) */
429 __u8 ipsproto; /* IPSEC type (config) */
430 __u32 spi;
431 struct xfrm_dst xdst;
432 struct dst_ops dstops;
433 #endif
434 char result[512];
435 };
436
437 struct pktgen_hdr {
438 __be32 pgh_magic;
439 __be32 seq_num;
440 __be32 tv_sec;
441 __be32 tv_usec;
442 };
443
444
445 static unsigned int pg_net_id __read_mostly;
446
447 struct pktgen_net {
448 struct net *net;
449 struct proc_dir_entry *proc_dir;
450 struct list_head pktgen_threads;
451 bool pktgen_exiting;
452 };
453
454 struct pktgen_thread {
455 struct mutex if_lock; /* for list of devices */
456 struct list_head if_list; /* All device here */
457 struct list_head th_list;
458 struct task_struct *tsk;
459 char result[512];
460
461 /* Field for thread to receive "posted" events terminate,
462 stop ifs etc. */
463
464 u32 control;
465 int cpu;
466
467 wait_queue_head_t queue;
468 struct completion start_done;
469 struct pktgen_net *net;
470 };
471
472 #define REMOVE 1
473 #define FIND 0
474
475 static const char version[] =
476 "Packet Generator for packet performance testing. "
477 "Version: " VERSION "\n";
478
479 static int pktgen_remove_device(struct pktgen_thread *t, struct pktgen_dev *i);
480 static int pktgen_add_device(struct pktgen_thread *t, const char *ifname);
481 static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
482 const char *ifname, bool exact);
483 static int pktgen_device_event(struct notifier_block *, unsigned long, void *);
484 static void pktgen_run_all_threads(struct pktgen_net *pn);
485 static void pktgen_reset_all_threads(struct pktgen_net *pn);
486 static void pktgen_stop_all_threads(struct pktgen_net *pn);
487
488 static void pktgen_stop(struct pktgen_thread *t);
489 static void pktgen_clear_counters(struct pktgen_dev *pkt_dev);
490 static void fill_imix_distribution(struct pktgen_dev *pkt_dev);
491
492 /* Module parameters, defaults. */
493 static int pg_count_d __read_mostly = 1000;
494 static int pg_delay_d __read_mostly;
495 static int pg_clone_skb_d __read_mostly;
496 static int debug __read_mostly;
497
498 static DEFINE_MUTEX(pktgen_thread_lock);
499
500 static struct notifier_block pktgen_notifier_block = {
501 .notifier_call = pktgen_device_event,
502 };
503
504 /*
505 * /proc handling functions
506 *
507 */
508
pgctrl_show(struct seq_file * seq,void * v)509 static int pgctrl_show(struct seq_file *seq, void *v)
510 {
511 seq_puts(seq, version);
512 return 0;
513 }
514
pgctrl_write(struct file * file,const char __user * buf,size_t count,loff_t * ppos)515 static ssize_t pgctrl_write(struct file *file, const char __user *buf,
516 size_t count, loff_t *ppos)
517 {
518 char data[128];
519 struct pktgen_net *pn = net_generic(current->nsproxy->net_ns, pg_net_id);
520
521 if (!capable(CAP_NET_ADMIN))
522 return -EPERM;
523
524 if (count == 0)
525 return -EINVAL;
526
527 if (count > sizeof(data))
528 count = sizeof(data);
529
530 if (copy_from_user(data, buf, count))
531 return -EFAULT;
532
533 data[count - 1] = 0; /* Strip trailing '\n' and terminate string */
534
535 if (!strcmp(data, "stop"))
536 pktgen_stop_all_threads(pn);
537 else if (!strcmp(data, "start"))
538 pktgen_run_all_threads(pn);
539 else if (!strcmp(data, "reset"))
540 pktgen_reset_all_threads(pn);
541 else
542 return -EINVAL;
543
544 return count;
545 }
546
pgctrl_open(struct inode * inode,struct file * file)547 static int pgctrl_open(struct inode *inode, struct file *file)
548 {
549 return single_open(file, pgctrl_show, pde_data(inode));
550 }
551
552 static const struct proc_ops pktgen_proc_ops = {
553 .proc_open = pgctrl_open,
554 .proc_read = seq_read,
555 .proc_lseek = seq_lseek,
556 .proc_write = pgctrl_write,
557 .proc_release = single_release,
558 };
559
pktgen_if_show(struct seq_file * seq,void * v)560 static int pktgen_if_show(struct seq_file *seq, void *v)
561 {
562 const struct pktgen_dev *pkt_dev = seq->private;
563 ktime_t stopped;
564 unsigned int i;
565 u64 idle;
566
567 seq_printf(seq,
568 "Params: count %llu min_pkt_size: %u max_pkt_size: %u\n",
569 (unsigned long long)pkt_dev->count, pkt_dev->min_pkt_size,
570 pkt_dev->max_pkt_size);
571
572 if (pkt_dev->n_imix_entries > 0) {
573 seq_puts(seq, " imix_weights: ");
574 for (i = 0; i < pkt_dev->n_imix_entries; i++) {
575 seq_printf(seq, "%llu,%llu ",
576 pkt_dev->imix_entries[i].size,
577 pkt_dev->imix_entries[i].weight);
578 }
579 seq_puts(seq, "\n");
580 }
581
582 seq_printf(seq,
583 " frags: %d delay: %llu clone_skb: %d ifname: %s\n",
584 pkt_dev->nfrags, (unsigned long long) pkt_dev->delay,
585 pkt_dev->clone_skb, pkt_dev->odevname);
586
587 seq_printf(seq, " flows: %u flowlen: %u\n", pkt_dev->cflows,
588 pkt_dev->lflow);
589
590 seq_printf(seq,
591 " queue_map_min: %u queue_map_max: %u\n",
592 pkt_dev->queue_map_min,
593 pkt_dev->queue_map_max);
594
595 if (pkt_dev->skb_priority)
596 seq_printf(seq, " skb_priority: %u\n",
597 pkt_dev->skb_priority);
598
599 if (pkt_dev->flags & F_IPV6) {
600 seq_printf(seq,
601 " saddr: %pI6c min_saddr: %pI6c max_saddr: %pI6c\n"
602 " daddr: %pI6c min_daddr: %pI6c max_daddr: %pI6c\n",
603 &pkt_dev->in6_saddr,
604 &pkt_dev->min_in6_saddr, &pkt_dev->max_in6_saddr,
605 &pkt_dev->in6_daddr,
606 &pkt_dev->min_in6_daddr, &pkt_dev->max_in6_daddr);
607 } else {
608 seq_printf(seq,
609 " dst_min: %s dst_max: %s\n",
610 pkt_dev->dst_min, pkt_dev->dst_max);
611 seq_printf(seq,
612 " src_min: %s src_max: %s\n",
613 pkt_dev->src_min, pkt_dev->src_max);
614 }
615
616 seq_puts(seq, " src_mac: ");
617
618 seq_printf(seq, "%pM ",
619 is_zero_ether_addr(pkt_dev->src_mac) ?
620 pkt_dev->odev->dev_addr : pkt_dev->src_mac);
621
622 seq_puts(seq, "dst_mac: ");
623 seq_printf(seq, "%pM\n", pkt_dev->dst_mac);
624
625 seq_printf(seq,
626 " udp_src_min: %d udp_src_max: %d"
627 " udp_dst_min: %d udp_dst_max: %d\n",
628 pkt_dev->udp_src_min, pkt_dev->udp_src_max,
629 pkt_dev->udp_dst_min, pkt_dev->udp_dst_max);
630
631 seq_printf(seq,
632 " src_mac_count: %d dst_mac_count: %d\n",
633 pkt_dev->src_mac_count, pkt_dev->dst_mac_count);
634
635 if (pkt_dev->nr_labels) {
636 seq_puts(seq, " mpls: ");
637 for (i = 0; i < pkt_dev->nr_labels; i++)
638 seq_printf(seq, "%08x%s", ntohl(pkt_dev->labels[i]),
639 i == pkt_dev->nr_labels-1 ? "\n" : ", ");
640 }
641
642 if (pkt_dev->vlan_id != 0xffff)
643 seq_printf(seq, " vlan_id: %u vlan_p: %u vlan_cfi: %u\n",
644 pkt_dev->vlan_id, pkt_dev->vlan_p,
645 pkt_dev->vlan_cfi);
646
647 if (pkt_dev->svlan_id != 0xffff)
648 seq_printf(seq, " svlan_id: %u vlan_p: %u vlan_cfi: %u\n",
649 pkt_dev->svlan_id, pkt_dev->svlan_p,
650 pkt_dev->svlan_cfi);
651
652 if (pkt_dev->tos)
653 seq_printf(seq, " tos: 0x%02x\n", pkt_dev->tos);
654
655 if (pkt_dev->traffic_class)
656 seq_printf(seq, " traffic_class: 0x%02x\n", pkt_dev->traffic_class);
657
658 if (pkt_dev->burst > 1)
659 seq_printf(seq, " burst: %d\n", pkt_dev->burst);
660
661 if (pkt_dev->node >= 0)
662 seq_printf(seq, " node: %d\n", pkt_dev->node);
663
664 if (pkt_dev->xmit_mode == M_NETIF_RECEIVE)
665 seq_puts(seq, " xmit_mode: netif_receive\n");
666 else if (pkt_dev->xmit_mode == M_QUEUE_XMIT)
667 seq_puts(seq, " xmit_mode: xmit_queue\n");
668
669 seq_puts(seq, " Flags: ");
670
671 for (i = 0; i < NR_PKT_FLAGS; i++) {
672 if (i == FLOW_SEQ_SHIFT)
673 if (!pkt_dev->cflows)
674 continue;
675
676 if (pkt_dev->flags & (1 << i)) {
677 seq_printf(seq, "%s ", pkt_flag_names[i]);
678 #ifdef CONFIG_XFRM
679 if (i == IPSEC_SHIFT && pkt_dev->spi)
680 seq_printf(seq, "spi:%u ", pkt_dev->spi);
681 #endif
682 } else if (i == FLOW_SEQ_SHIFT) {
683 seq_puts(seq, "FLOW_RND ");
684 }
685 }
686
687 seq_puts(seq, "\n");
688
689 /* not really stopped, more like last-running-at */
690 stopped = pkt_dev->running ? ktime_get() : pkt_dev->stopped_at;
691 idle = pkt_dev->idle_acc;
692 do_div(idle, NSEC_PER_USEC);
693
694 seq_printf(seq,
695 "Current:\n pkts-sofar: %llu errors: %llu\n",
696 (unsigned long long)pkt_dev->sofar,
697 (unsigned long long)pkt_dev->errors);
698
699 if (pkt_dev->n_imix_entries > 0) {
700 int i;
701
702 seq_puts(seq, " imix_size_counts: ");
703 for (i = 0; i < pkt_dev->n_imix_entries; i++) {
704 seq_printf(seq, "%llu,%llu ",
705 pkt_dev->imix_entries[i].size,
706 pkt_dev->imix_entries[i].count_so_far);
707 }
708 seq_puts(seq, "\n");
709 }
710
711 seq_printf(seq,
712 " started: %lluus stopped: %lluus idle: %lluus\n",
713 (unsigned long long) ktime_to_us(pkt_dev->started_at),
714 (unsigned long long) ktime_to_us(stopped),
715 (unsigned long long) idle);
716
717 seq_printf(seq,
718 " seq_num: %d cur_dst_mac_offset: %d cur_src_mac_offset: %d\n",
719 pkt_dev->seq_num, pkt_dev->cur_dst_mac_offset,
720 pkt_dev->cur_src_mac_offset);
721
722 if (pkt_dev->flags & F_IPV6) {
723 seq_printf(seq, " cur_saddr: %pI6c cur_daddr: %pI6c\n",
724 &pkt_dev->cur_in6_saddr,
725 &pkt_dev->cur_in6_daddr);
726 } else
727 seq_printf(seq, " cur_saddr: %pI4 cur_daddr: %pI4\n",
728 &pkt_dev->cur_saddr, &pkt_dev->cur_daddr);
729
730 seq_printf(seq, " cur_udp_dst: %d cur_udp_src: %d\n",
731 pkt_dev->cur_udp_dst, pkt_dev->cur_udp_src);
732
733 seq_printf(seq, " cur_queue_map: %u\n", pkt_dev->cur_queue_map);
734
735 seq_printf(seq, " flows: %u\n", pkt_dev->nflows);
736
737 if (pkt_dev->result[0])
738 seq_printf(seq, "Result: %s\n", pkt_dev->result);
739 else
740 seq_puts(seq, "Result: Idle\n");
741
742 return 0;
743 }
744
745
hex32_arg(const char __user * user_buffer,unsigned long maxlen,__u32 * num)746 static int hex32_arg(const char __user *user_buffer, unsigned long maxlen,
747 __u32 *num)
748 {
749 int i = 0;
750 *num = 0;
751
752 for (; i < maxlen; i++) {
753 int value;
754 char c;
755 *num <<= 4;
756 if (get_user(c, &user_buffer[i]))
757 return -EFAULT;
758 value = hex_to_bin(c);
759 if (value >= 0)
760 *num |= value;
761 else
762 break;
763 }
764 return i;
765 }
766
count_trail_chars(const char __user * user_buffer,unsigned int maxlen)767 static int count_trail_chars(const char __user * user_buffer,
768 unsigned int maxlen)
769 {
770 int i;
771
772 for (i = 0; i < maxlen; i++) {
773 char c;
774 if (get_user(c, &user_buffer[i]))
775 return -EFAULT;
776 switch (c) {
777 case '\"':
778 case '\n':
779 case '\r':
780 case '\t':
781 case ' ':
782 case '=':
783 break;
784 default:
785 goto done;
786 }
787 }
788 done:
789 return i;
790 }
791
num_arg(const char __user * user_buffer,unsigned long maxlen,unsigned long * num)792 static long num_arg(const char __user *user_buffer, unsigned long maxlen,
793 unsigned long *num)
794 {
795 int i;
796 *num = 0;
797
798 for (i = 0; i < maxlen; i++) {
799 char c;
800 if (get_user(c, &user_buffer[i]))
801 return -EFAULT;
802 if ((c >= '0') && (c <= '9')) {
803 *num *= 10;
804 *num += c - '0';
805 } else
806 break;
807 }
808 return i;
809 }
810
strn_len(const char __user * user_buffer,unsigned int maxlen)811 static int strn_len(const char __user * user_buffer, unsigned int maxlen)
812 {
813 int i;
814
815 for (i = 0; i < maxlen; i++) {
816 char c;
817 if (get_user(c, &user_buffer[i]))
818 return -EFAULT;
819 switch (c) {
820 case '\"':
821 case '\n':
822 case '\r':
823 case '\t':
824 case ' ':
825 goto done_str;
826 default:
827 break;
828 }
829 }
830 done_str:
831 return i;
832 }
833
834 /* Parses imix entries from user buffer.
835 * The user buffer should consist of imix entries separated by spaces
836 * where each entry consists of size and weight delimited by commas.
837 * "size1,weight_1 size2,weight_2 ... size_n,weight_n" for example.
838 */
get_imix_entries(const char __user * buffer,struct pktgen_dev * pkt_dev)839 static ssize_t get_imix_entries(const char __user *buffer,
840 struct pktgen_dev *pkt_dev)
841 {
842 const int max_digits = 10;
843 int i = 0;
844 long len;
845 char c;
846
847 pkt_dev->n_imix_entries = 0;
848
849 do {
850 unsigned long weight;
851 unsigned long size;
852
853 if (pkt_dev->n_imix_entries >= MAX_IMIX_ENTRIES)
854 return -E2BIG;
855
856 len = num_arg(&buffer[i], max_digits, &size);
857 if (len < 0)
858 return len;
859 i += len;
860 if (get_user(c, &buffer[i]))
861 return -EFAULT;
862 /* Check for comma between size_i and weight_i */
863 if (c != ',')
864 return -EINVAL;
865 i++;
866
867 if (size < 14 + 20 + 8)
868 size = 14 + 20 + 8;
869
870 len = num_arg(&buffer[i], max_digits, &weight);
871 if (len < 0)
872 return len;
873 if (weight <= 0)
874 return -EINVAL;
875
876 pkt_dev->imix_entries[pkt_dev->n_imix_entries].size = size;
877 pkt_dev->imix_entries[pkt_dev->n_imix_entries].weight = weight;
878
879 i += len;
880 if (get_user(c, &buffer[i]))
881 return -EFAULT;
882
883 i++;
884 pkt_dev->n_imix_entries++;
885 } while (c == ' ');
886
887 return i;
888 }
889
get_labels(const char __user * buffer,struct pktgen_dev * pkt_dev)890 static ssize_t get_labels(const char __user *buffer, struct pktgen_dev *pkt_dev)
891 {
892 unsigned int n = 0;
893 char c;
894 ssize_t i = 0;
895 int len;
896
897 pkt_dev->nr_labels = 0;
898 do {
899 __u32 tmp;
900
901 if (n >= MAX_MPLS_LABELS)
902 return -E2BIG;
903
904 len = hex32_arg(&buffer[i], 8, &tmp);
905 if (len <= 0)
906 return len;
907 pkt_dev->labels[n] = htonl(tmp);
908 if (pkt_dev->labels[n] & MPLS_STACK_BOTTOM)
909 pkt_dev->flags |= F_MPLS_RND;
910 i += len;
911 if (get_user(c, &buffer[i]))
912 return -EFAULT;
913 i++;
914 n++;
915 } while (c == ',');
916
917 pkt_dev->nr_labels = n;
918 return i;
919 }
920
pktgen_read_flag(const char * f,bool * disable)921 static __u32 pktgen_read_flag(const char *f, bool *disable)
922 {
923 __u32 i;
924
925 if (f[0] == '!') {
926 *disable = true;
927 f++;
928 }
929
930 for (i = 0; i < NR_PKT_FLAGS; i++) {
931 if (!IS_ENABLED(CONFIG_XFRM) && i == IPSEC_SHIFT)
932 continue;
933
934 /* allow only disabling ipv6 flag */
935 if (!*disable && i == IPV6_SHIFT)
936 continue;
937
938 if (strcmp(f, pkt_flag_names[i]) == 0)
939 return 1 << i;
940 }
941
942 if (strcmp(f, "FLOW_RND") == 0) {
943 *disable = !*disable;
944 return F_FLOW_SEQ;
945 }
946
947 return 0;
948 }
949
pktgen_if_write(struct file * file,const char __user * user_buffer,size_t count,loff_t * offset)950 static ssize_t pktgen_if_write(struct file *file,
951 const char __user * user_buffer, size_t count,
952 loff_t * offset)
953 {
954 struct seq_file *seq = file->private_data;
955 struct pktgen_dev *pkt_dev = seq->private;
956 int i, max, len;
957 char name[16], valstr[32];
958 unsigned long value = 0;
959 char *pg_result = NULL;
960 int tmp = 0;
961 char buf[128];
962
963 pg_result = &(pkt_dev->result[0]);
964
965 if (count < 1) {
966 pr_warn("wrong command format\n");
967 return -EINVAL;
968 }
969
970 max = count;
971 tmp = count_trail_chars(user_buffer, max);
972 if (tmp < 0) {
973 pr_warn("illegal format\n");
974 return tmp;
975 }
976 i = tmp;
977
978 /* Read variable name */
979
980 len = strn_len(&user_buffer[i], sizeof(name) - 1);
981 if (len < 0)
982 return len;
983
984 memset(name, 0, sizeof(name));
985 if (copy_from_user(name, &user_buffer[i], len))
986 return -EFAULT;
987 i += len;
988
989 max = count - i;
990 len = count_trail_chars(&user_buffer[i], max);
991 if (len < 0)
992 return len;
993
994 i += len;
995
996 if (debug) {
997 size_t copy = min_t(size_t, count + 1, 1024);
998 char *tp = strndup_user(user_buffer, copy);
999
1000 if (IS_ERR(tp))
1001 return PTR_ERR(tp);
1002
1003 pr_debug("%s,%zu buffer -:%s:-\n", name, count, tp);
1004 kfree(tp);
1005 }
1006
1007 if (!strcmp(name, "min_pkt_size")) {
1008 len = num_arg(&user_buffer[i], 10, &value);
1009 if (len < 0)
1010 return len;
1011
1012 i += len;
1013 if (value < 14 + 20 + 8)
1014 value = 14 + 20 + 8;
1015 if (value != pkt_dev->min_pkt_size) {
1016 pkt_dev->min_pkt_size = value;
1017 pkt_dev->cur_pkt_size = value;
1018 }
1019 sprintf(pg_result, "OK: min_pkt_size=%d",
1020 pkt_dev->min_pkt_size);
1021 return count;
1022 }
1023
1024 if (!strcmp(name, "max_pkt_size")) {
1025 len = num_arg(&user_buffer[i], 10, &value);
1026 if (len < 0)
1027 return len;
1028
1029 i += len;
1030 if (value < 14 + 20 + 8)
1031 value = 14 + 20 + 8;
1032 if (value != pkt_dev->max_pkt_size) {
1033 pkt_dev->max_pkt_size = value;
1034 pkt_dev->cur_pkt_size = value;
1035 }
1036 sprintf(pg_result, "OK: max_pkt_size=%d",
1037 pkt_dev->max_pkt_size);
1038 return count;
1039 }
1040
1041 /* Shortcut for min = max */
1042
1043 if (!strcmp(name, "pkt_size")) {
1044 len = num_arg(&user_buffer[i], 10, &value);
1045 if (len < 0)
1046 return len;
1047
1048 i += len;
1049 if (value < 14 + 20 + 8)
1050 value = 14 + 20 + 8;
1051 if (value != pkt_dev->min_pkt_size) {
1052 pkt_dev->min_pkt_size = value;
1053 pkt_dev->max_pkt_size = value;
1054 pkt_dev->cur_pkt_size = value;
1055 }
1056 sprintf(pg_result, "OK: pkt_size=%d", pkt_dev->min_pkt_size);
1057 return count;
1058 }
1059
1060 if (!strcmp(name, "imix_weights")) {
1061 if (pkt_dev->clone_skb > 0)
1062 return -EINVAL;
1063
1064 len = get_imix_entries(&user_buffer[i], pkt_dev);
1065 if (len < 0)
1066 return len;
1067
1068 fill_imix_distribution(pkt_dev);
1069
1070 i += len;
1071 return count;
1072 }
1073
1074 if (!strcmp(name, "debug")) {
1075 len = num_arg(&user_buffer[i], 10, &value);
1076 if (len < 0)
1077 return len;
1078
1079 i += len;
1080 debug = value;
1081 sprintf(pg_result, "OK: debug=%u", debug);
1082 return count;
1083 }
1084
1085 if (!strcmp(name, "frags")) {
1086 len = num_arg(&user_buffer[i], 10, &value);
1087 if (len < 0)
1088 return len;
1089
1090 i += len;
1091 pkt_dev->nfrags = value;
1092 sprintf(pg_result, "OK: frags=%d", pkt_dev->nfrags);
1093 return count;
1094 }
1095 if (!strcmp(name, "delay")) {
1096 len = num_arg(&user_buffer[i], 10, &value);
1097 if (len < 0)
1098 return len;
1099
1100 i += len;
1101 if (value == 0x7FFFFFFF)
1102 pkt_dev->delay = ULLONG_MAX;
1103 else
1104 pkt_dev->delay = (u64)value;
1105
1106 sprintf(pg_result, "OK: delay=%llu",
1107 (unsigned long long) pkt_dev->delay);
1108 return count;
1109 }
1110 if (!strcmp(name, "rate")) {
1111 len = num_arg(&user_buffer[i], 10, &value);
1112 if (len < 0)
1113 return len;
1114
1115 i += len;
1116 if (!value)
1117 return len;
1118 pkt_dev->delay = pkt_dev->min_pkt_size*8*NSEC_PER_USEC/value;
1119 if (debug)
1120 pr_info("Delay set at: %llu ns\n", pkt_dev->delay);
1121
1122 sprintf(pg_result, "OK: rate=%lu", value);
1123 return count;
1124 }
1125 if (!strcmp(name, "ratep")) {
1126 len = num_arg(&user_buffer[i], 10, &value);
1127 if (len < 0)
1128 return len;
1129
1130 i += len;
1131 if (!value)
1132 return len;
1133 pkt_dev->delay = NSEC_PER_SEC/value;
1134 if (debug)
1135 pr_info("Delay set at: %llu ns\n", pkt_dev->delay);
1136
1137 sprintf(pg_result, "OK: rate=%lu", value);
1138 return count;
1139 }
1140 if (!strcmp(name, "udp_src_min")) {
1141 len = num_arg(&user_buffer[i], 10, &value);
1142 if (len < 0)
1143 return len;
1144
1145 i += len;
1146 if (value != pkt_dev->udp_src_min) {
1147 pkt_dev->udp_src_min = value;
1148 pkt_dev->cur_udp_src = value;
1149 }
1150 sprintf(pg_result, "OK: udp_src_min=%u", pkt_dev->udp_src_min);
1151 return count;
1152 }
1153 if (!strcmp(name, "udp_dst_min")) {
1154 len = num_arg(&user_buffer[i], 10, &value);
1155 if (len < 0)
1156 return len;
1157
1158 i += len;
1159 if (value != pkt_dev->udp_dst_min) {
1160 pkt_dev->udp_dst_min = value;
1161 pkt_dev->cur_udp_dst = value;
1162 }
1163 sprintf(pg_result, "OK: udp_dst_min=%u", pkt_dev->udp_dst_min);
1164 return count;
1165 }
1166 if (!strcmp(name, "udp_src_max")) {
1167 len = num_arg(&user_buffer[i], 10, &value);
1168 if (len < 0)
1169 return len;
1170
1171 i += len;
1172 if (value != pkt_dev->udp_src_max) {
1173 pkt_dev->udp_src_max = value;
1174 pkt_dev->cur_udp_src = value;
1175 }
1176 sprintf(pg_result, "OK: udp_src_max=%u", pkt_dev->udp_src_max);
1177 return count;
1178 }
1179 if (!strcmp(name, "udp_dst_max")) {
1180 len = num_arg(&user_buffer[i], 10, &value);
1181 if (len < 0)
1182 return len;
1183
1184 i += len;
1185 if (value != pkt_dev->udp_dst_max) {
1186 pkt_dev->udp_dst_max = value;
1187 pkt_dev->cur_udp_dst = value;
1188 }
1189 sprintf(pg_result, "OK: udp_dst_max=%u", pkt_dev->udp_dst_max);
1190 return count;
1191 }
1192 if (!strcmp(name, "clone_skb")) {
1193 len = num_arg(&user_buffer[i], 10, &value);
1194 if (len < 0)
1195 return len;
1196 /* clone_skb is not supported for netif_receive xmit_mode and
1197 * IMIX mode.
1198 */
1199 if ((value > 0) &&
1200 ((pkt_dev->xmit_mode == M_NETIF_RECEIVE) ||
1201 !(pkt_dev->odev->priv_flags & IFF_TX_SKB_SHARING)))
1202 return -ENOTSUPP;
1203 if (value > 0 && pkt_dev->n_imix_entries > 0)
1204 return -EINVAL;
1205
1206 i += len;
1207 pkt_dev->clone_skb = value;
1208
1209 sprintf(pg_result, "OK: clone_skb=%d", pkt_dev->clone_skb);
1210 return count;
1211 }
1212 if (!strcmp(name, "count")) {
1213 len = num_arg(&user_buffer[i], 10, &value);
1214 if (len < 0)
1215 return len;
1216
1217 i += len;
1218 pkt_dev->count = value;
1219 sprintf(pg_result, "OK: count=%llu",
1220 (unsigned long long)pkt_dev->count);
1221 return count;
1222 }
1223 if (!strcmp(name, "src_mac_count")) {
1224 len = num_arg(&user_buffer[i], 10, &value);
1225 if (len < 0)
1226 return len;
1227
1228 i += len;
1229 if (pkt_dev->src_mac_count != value) {
1230 pkt_dev->src_mac_count = value;
1231 pkt_dev->cur_src_mac_offset = 0;
1232 }
1233 sprintf(pg_result, "OK: src_mac_count=%d",
1234 pkt_dev->src_mac_count);
1235 return count;
1236 }
1237 if (!strcmp(name, "dst_mac_count")) {
1238 len = num_arg(&user_buffer[i], 10, &value);
1239 if (len < 0)
1240 return len;
1241
1242 i += len;
1243 if (pkt_dev->dst_mac_count != value) {
1244 pkt_dev->dst_mac_count = value;
1245 pkt_dev->cur_dst_mac_offset = 0;
1246 }
1247 sprintf(pg_result, "OK: dst_mac_count=%d",
1248 pkt_dev->dst_mac_count);
1249 return count;
1250 }
1251 if (!strcmp(name, "burst")) {
1252 len = num_arg(&user_buffer[i], 10, &value);
1253 if (len < 0)
1254 return len;
1255
1256 i += len;
1257 if ((value > 1) &&
1258 ((pkt_dev->xmit_mode == M_QUEUE_XMIT) ||
1259 ((pkt_dev->xmit_mode == M_START_XMIT) &&
1260 (!(pkt_dev->odev->priv_flags & IFF_TX_SKB_SHARING)))))
1261 return -ENOTSUPP;
1262 pkt_dev->burst = value < 1 ? 1 : value;
1263 sprintf(pg_result, "OK: burst=%u", pkt_dev->burst);
1264 return count;
1265 }
1266 if (!strcmp(name, "node")) {
1267 len = num_arg(&user_buffer[i], 10, &value);
1268 if (len < 0)
1269 return len;
1270
1271 i += len;
1272
1273 if (node_possible(value)) {
1274 pkt_dev->node = value;
1275 sprintf(pg_result, "OK: node=%d", pkt_dev->node);
1276 if (pkt_dev->page) {
1277 put_page(pkt_dev->page);
1278 pkt_dev->page = NULL;
1279 }
1280 }
1281 else
1282 sprintf(pg_result, "ERROR: node not possible");
1283 return count;
1284 }
1285 if (!strcmp(name, "xmit_mode")) {
1286 char f[32];
1287
1288 memset(f, 0, 32);
1289 len = strn_len(&user_buffer[i], sizeof(f) - 1);
1290 if (len < 0)
1291 return len;
1292
1293 if (copy_from_user(f, &user_buffer[i], len))
1294 return -EFAULT;
1295 i += len;
1296
1297 if (strcmp(f, "start_xmit") == 0) {
1298 pkt_dev->xmit_mode = M_START_XMIT;
1299 } else if (strcmp(f, "netif_receive") == 0) {
1300 /* clone_skb set earlier, not supported in this mode */
1301 if (pkt_dev->clone_skb > 0)
1302 return -ENOTSUPP;
1303
1304 pkt_dev->xmit_mode = M_NETIF_RECEIVE;
1305
1306 /* make sure new packet is allocated every time
1307 * pktgen_xmit() is called
1308 */
1309 pkt_dev->last_ok = 1;
1310 } else if (strcmp(f, "queue_xmit") == 0) {
1311 pkt_dev->xmit_mode = M_QUEUE_XMIT;
1312 pkt_dev->last_ok = 1;
1313 } else {
1314 sprintf(pg_result,
1315 "xmit_mode -:%s:- unknown\nAvailable modes: %s",
1316 f, "start_xmit, netif_receive\n");
1317 return count;
1318 }
1319 sprintf(pg_result, "OK: xmit_mode=%s", f);
1320 return count;
1321 }
1322 if (!strcmp(name, "flag")) {
1323 __u32 flag;
1324 char f[32];
1325 bool disable = false;
1326
1327 memset(f, 0, 32);
1328 len = strn_len(&user_buffer[i], sizeof(f) - 1);
1329 if (len < 0)
1330 return len;
1331
1332 if (copy_from_user(f, &user_buffer[i], len))
1333 return -EFAULT;
1334 i += len;
1335
1336 flag = pktgen_read_flag(f, &disable);
1337
1338 if (flag) {
1339 if (disable)
1340 pkt_dev->flags &= ~flag;
1341 else
1342 pkt_dev->flags |= flag;
1343 } else {
1344 sprintf(pg_result,
1345 "Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
1346 f,
1347 "IPSRC_RND, IPDST_RND, UDPSRC_RND, UDPDST_RND, "
1348 "MACSRC_RND, MACDST_RND, TXSIZE_RND, IPV6, "
1349 "MPLS_RND, VID_RND, SVID_RND, FLOW_SEQ, "
1350 "QUEUE_MAP_RND, QUEUE_MAP_CPU, UDPCSUM, "
1351 "NO_TIMESTAMP, "
1352 #ifdef CONFIG_XFRM
1353 "IPSEC, "
1354 #endif
1355 "NODE_ALLOC\n");
1356 return count;
1357 }
1358 sprintf(pg_result, "OK: flags=0x%x", pkt_dev->flags);
1359 return count;
1360 }
1361 if (!strcmp(name, "dst_min") || !strcmp(name, "dst")) {
1362 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_min) - 1);
1363 if (len < 0)
1364 return len;
1365
1366 if (copy_from_user(buf, &user_buffer[i], len))
1367 return -EFAULT;
1368 buf[len] = 0;
1369 if (strcmp(buf, pkt_dev->dst_min) != 0) {
1370 memset(pkt_dev->dst_min, 0, sizeof(pkt_dev->dst_min));
1371 strcpy(pkt_dev->dst_min, buf);
1372 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
1373 pkt_dev->cur_daddr = pkt_dev->daddr_min;
1374 }
1375 if (debug)
1376 pr_debug("dst_min set to: %s\n", pkt_dev->dst_min);
1377 i += len;
1378 sprintf(pg_result, "OK: dst_min=%s", pkt_dev->dst_min);
1379 return count;
1380 }
1381 if (!strcmp(name, "dst_max")) {
1382 len = strn_len(&user_buffer[i], sizeof(pkt_dev->dst_max) - 1);
1383 if (len < 0)
1384 return len;
1385
1386 if (copy_from_user(buf, &user_buffer[i], len))
1387 return -EFAULT;
1388 buf[len] = 0;
1389 if (strcmp(buf, pkt_dev->dst_max) != 0) {
1390 memset(pkt_dev->dst_max, 0, sizeof(pkt_dev->dst_max));
1391 strcpy(pkt_dev->dst_max, buf);
1392 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
1393 pkt_dev->cur_daddr = pkt_dev->daddr_max;
1394 }
1395 if (debug)
1396 pr_debug("dst_max set to: %s\n", pkt_dev->dst_max);
1397 i += len;
1398 sprintf(pg_result, "OK: dst_max=%s", pkt_dev->dst_max);
1399 return count;
1400 }
1401 if (!strcmp(name, "dst6")) {
1402 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1403 if (len < 0)
1404 return len;
1405
1406 pkt_dev->flags |= F_IPV6;
1407
1408 if (copy_from_user(buf, &user_buffer[i], len))
1409 return -EFAULT;
1410 buf[len] = 0;
1411
1412 in6_pton(buf, -1, pkt_dev->in6_daddr.s6_addr, -1, NULL);
1413 snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->in6_daddr);
1414
1415 pkt_dev->cur_in6_daddr = pkt_dev->in6_daddr;
1416
1417 if (debug)
1418 pr_debug("dst6 set to: %s\n", buf);
1419
1420 i += len;
1421 sprintf(pg_result, "OK: dst6=%s", buf);
1422 return count;
1423 }
1424 if (!strcmp(name, "dst6_min")) {
1425 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1426 if (len < 0)
1427 return len;
1428
1429 pkt_dev->flags |= F_IPV6;
1430
1431 if (copy_from_user(buf, &user_buffer[i], len))
1432 return -EFAULT;
1433 buf[len] = 0;
1434
1435 in6_pton(buf, -1, pkt_dev->min_in6_daddr.s6_addr, -1, NULL);
1436 snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->min_in6_daddr);
1437
1438 pkt_dev->cur_in6_daddr = pkt_dev->min_in6_daddr;
1439 if (debug)
1440 pr_debug("dst6_min set to: %s\n", buf);
1441
1442 i += len;
1443 sprintf(pg_result, "OK: dst6_min=%s", buf);
1444 return count;
1445 }
1446 if (!strcmp(name, "dst6_max")) {
1447 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1448 if (len < 0)
1449 return len;
1450
1451 pkt_dev->flags |= F_IPV6;
1452
1453 if (copy_from_user(buf, &user_buffer[i], len))
1454 return -EFAULT;
1455 buf[len] = 0;
1456
1457 in6_pton(buf, -1, pkt_dev->max_in6_daddr.s6_addr, -1, NULL);
1458 snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->max_in6_daddr);
1459
1460 if (debug)
1461 pr_debug("dst6_max set to: %s\n", buf);
1462
1463 i += len;
1464 sprintf(pg_result, "OK: dst6_max=%s", buf);
1465 return count;
1466 }
1467 if (!strcmp(name, "src6")) {
1468 len = strn_len(&user_buffer[i], sizeof(buf) - 1);
1469 if (len < 0)
1470 return len;
1471
1472 pkt_dev->flags |= F_IPV6;
1473
1474 if (copy_from_user(buf, &user_buffer[i], len))
1475 return -EFAULT;
1476 buf[len] = 0;
1477
1478 in6_pton(buf, -1, pkt_dev->in6_saddr.s6_addr, -1, NULL);
1479 snprintf(buf, sizeof(buf), "%pI6c", &pkt_dev->in6_saddr);
1480
1481 pkt_dev->cur_in6_saddr = pkt_dev->in6_saddr;
1482
1483 if (debug)
1484 pr_debug("src6 set to: %s\n", buf);
1485
1486 i += len;
1487 sprintf(pg_result, "OK: src6=%s", buf);
1488 return count;
1489 }
1490 if (!strcmp(name, "src_min")) {
1491 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_min) - 1);
1492 if (len < 0)
1493 return len;
1494
1495 if (copy_from_user(buf, &user_buffer[i], len))
1496 return -EFAULT;
1497 buf[len] = 0;
1498 if (strcmp(buf, pkt_dev->src_min) != 0) {
1499 memset(pkt_dev->src_min, 0, sizeof(pkt_dev->src_min));
1500 strcpy(pkt_dev->src_min, buf);
1501 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
1502 pkt_dev->cur_saddr = pkt_dev->saddr_min;
1503 }
1504 if (debug)
1505 pr_debug("src_min set to: %s\n", pkt_dev->src_min);
1506 i += len;
1507 sprintf(pg_result, "OK: src_min=%s", pkt_dev->src_min);
1508 return count;
1509 }
1510 if (!strcmp(name, "src_max")) {
1511 len = strn_len(&user_buffer[i], sizeof(pkt_dev->src_max) - 1);
1512 if (len < 0)
1513 return len;
1514
1515 if (copy_from_user(buf, &user_buffer[i], len))
1516 return -EFAULT;
1517 buf[len] = 0;
1518 if (strcmp(buf, pkt_dev->src_max) != 0) {
1519 memset(pkt_dev->src_max, 0, sizeof(pkt_dev->src_max));
1520 strcpy(pkt_dev->src_max, buf);
1521 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
1522 pkt_dev->cur_saddr = pkt_dev->saddr_max;
1523 }
1524 if (debug)
1525 pr_debug("src_max set to: %s\n", pkt_dev->src_max);
1526 i += len;
1527 sprintf(pg_result, "OK: src_max=%s", pkt_dev->src_max);
1528 return count;
1529 }
1530 if (!strcmp(name, "dst_mac")) {
1531 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
1532 if (len < 0)
1533 return len;
1534
1535 memset(valstr, 0, sizeof(valstr));
1536 if (copy_from_user(valstr, &user_buffer[i], len))
1537 return -EFAULT;
1538
1539 if (!mac_pton(valstr, pkt_dev->dst_mac))
1540 return -EINVAL;
1541 /* Set up Dest MAC */
1542 ether_addr_copy(&pkt_dev->hh[0], pkt_dev->dst_mac);
1543
1544 sprintf(pg_result, "OK: dstmac %pM", pkt_dev->dst_mac);
1545 return count;
1546 }
1547 if (!strcmp(name, "src_mac")) {
1548 len = strn_len(&user_buffer[i], sizeof(valstr) - 1);
1549 if (len < 0)
1550 return len;
1551
1552 memset(valstr, 0, sizeof(valstr));
1553 if (copy_from_user(valstr, &user_buffer[i], len))
1554 return -EFAULT;
1555
1556 if (!mac_pton(valstr, pkt_dev->src_mac))
1557 return -EINVAL;
1558 /* Set up Src MAC */
1559 ether_addr_copy(&pkt_dev->hh[6], pkt_dev->src_mac);
1560
1561 sprintf(pg_result, "OK: srcmac %pM", pkt_dev->src_mac);
1562 return count;
1563 }
1564
1565 if (!strcmp(name, "clear_counters")) {
1566 pktgen_clear_counters(pkt_dev);
1567 sprintf(pg_result, "OK: Clearing counters.\n");
1568 return count;
1569 }
1570
1571 if (!strcmp(name, "flows")) {
1572 len = num_arg(&user_buffer[i], 10, &value);
1573 if (len < 0)
1574 return len;
1575
1576 i += len;
1577 if (value > MAX_CFLOWS)
1578 value = MAX_CFLOWS;
1579
1580 pkt_dev->cflows = value;
1581 sprintf(pg_result, "OK: flows=%u", pkt_dev->cflows);
1582 return count;
1583 }
1584 #ifdef CONFIG_XFRM
1585 if (!strcmp(name, "spi")) {
1586 len = num_arg(&user_buffer[i], 10, &value);
1587 if (len < 0)
1588 return len;
1589
1590 i += len;
1591 pkt_dev->spi = value;
1592 sprintf(pg_result, "OK: spi=%u", pkt_dev->spi);
1593 return count;
1594 }
1595 #endif
1596 if (!strcmp(name, "flowlen")) {
1597 len = num_arg(&user_buffer[i], 10, &value);
1598 if (len < 0)
1599 return len;
1600
1601 i += len;
1602 pkt_dev->lflow = value;
1603 sprintf(pg_result, "OK: flowlen=%u", pkt_dev->lflow);
1604 return count;
1605 }
1606
1607 if (!strcmp(name, "queue_map_min")) {
1608 len = num_arg(&user_buffer[i], 5, &value);
1609 if (len < 0)
1610 return len;
1611
1612 i += len;
1613 pkt_dev->queue_map_min = value;
1614 sprintf(pg_result, "OK: queue_map_min=%u", pkt_dev->queue_map_min);
1615 return count;
1616 }
1617
1618 if (!strcmp(name, "queue_map_max")) {
1619 len = num_arg(&user_buffer[i], 5, &value);
1620 if (len < 0)
1621 return len;
1622
1623 i += len;
1624 pkt_dev->queue_map_max = value;
1625 sprintf(pg_result, "OK: queue_map_max=%u", pkt_dev->queue_map_max);
1626 return count;
1627 }
1628
1629 if (!strcmp(name, "mpls")) {
1630 unsigned int n, cnt;
1631
1632 len = get_labels(&user_buffer[i], pkt_dev);
1633 if (len < 0)
1634 return len;
1635 i += len;
1636 cnt = sprintf(pg_result, "OK: mpls=");
1637 for (n = 0; n < pkt_dev->nr_labels; n++)
1638 cnt += sprintf(pg_result + cnt,
1639 "%08x%s", ntohl(pkt_dev->labels[n]),
1640 n == pkt_dev->nr_labels-1 ? "" : ",");
1641
1642 if (pkt_dev->nr_labels && pkt_dev->vlan_id != 0xffff) {
1643 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1644 pkt_dev->svlan_id = 0xffff;
1645
1646 if (debug)
1647 pr_debug("VLAN/SVLAN auto turned off\n");
1648 }
1649 return count;
1650 }
1651
1652 if (!strcmp(name, "vlan_id")) {
1653 len = num_arg(&user_buffer[i], 4, &value);
1654 if (len < 0)
1655 return len;
1656
1657 i += len;
1658 if (value <= 4095) {
1659 pkt_dev->vlan_id = value; /* turn on VLAN */
1660
1661 if (debug)
1662 pr_debug("VLAN turned on\n");
1663
1664 if (debug && pkt_dev->nr_labels)
1665 pr_debug("MPLS auto turned off\n");
1666
1667 pkt_dev->nr_labels = 0; /* turn off MPLS */
1668 sprintf(pg_result, "OK: vlan_id=%u", pkt_dev->vlan_id);
1669 } else {
1670 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1671 pkt_dev->svlan_id = 0xffff;
1672
1673 if (debug)
1674 pr_debug("VLAN/SVLAN turned off\n");
1675 }
1676 return count;
1677 }
1678
1679 if (!strcmp(name, "vlan_p")) {
1680 len = num_arg(&user_buffer[i], 1, &value);
1681 if (len < 0)
1682 return len;
1683
1684 i += len;
1685 if ((value <= 7) && (pkt_dev->vlan_id != 0xffff)) {
1686 pkt_dev->vlan_p = value;
1687 sprintf(pg_result, "OK: vlan_p=%u", pkt_dev->vlan_p);
1688 } else {
1689 sprintf(pg_result, "ERROR: vlan_p must be 0-7");
1690 }
1691 return count;
1692 }
1693
1694 if (!strcmp(name, "vlan_cfi")) {
1695 len = num_arg(&user_buffer[i], 1, &value);
1696 if (len < 0)
1697 return len;
1698
1699 i += len;
1700 if ((value <= 1) && (pkt_dev->vlan_id != 0xffff)) {
1701 pkt_dev->vlan_cfi = value;
1702 sprintf(pg_result, "OK: vlan_cfi=%u", pkt_dev->vlan_cfi);
1703 } else {
1704 sprintf(pg_result, "ERROR: vlan_cfi must be 0-1");
1705 }
1706 return count;
1707 }
1708
1709 if (!strcmp(name, "svlan_id")) {
1710 len = num_arg(&user_buffer[i], 4, &value);
1711 if (len < 0)
1712 return len;
1713
1714 i += len;
1715 if ((value <= 4095) && ((pkt_dev->vlan_id != 0xffff))) {
1716 pkt_dev->svlan_id = value; /* turn on SVLAN */
1717
1718 if (debug)
1719 pr_debug("SVLAN turned on\n");
1720
1721 if (debug && pkt_dev->nr_labels)
1722 pr_debug("MPLS auto turned off\n");
1723
1724 pkt_dev->nr_labels = 0; /* turn off MPLS */
1725 sprintf(pg_result, "OK: svlan_id=%u", pkt_dev->svlan_id);
1726 } else {
1727 pkt_dev->vlan_id = 0xffff; /* turn off VLAN/SVLAN */
1728 pkt_dev->svlan_id = 0xffff;
1729
1730 if (debug)
1731 pr_debug("VLAN/SVLAN turned off\n");
1732 }
1733 return count;
1734 }
1735
1736 if (!strcmp(name, "svlan_p")) {
1737 len = num_arg(&user_buffer[i], 1, &value);
1738 if (len < 0)
1739 return len;
1740
1741 i += len;
1742 if ((value <= 7) && (pkt_dev->svlan_id != 0xffff)) {
1743 pkt_dev->svlan_p = value;
1744 sprintf(pg_result, "OK: svlan_p=%u", pkt_dev->svlan_p);
1745 } else {
1746 sprintf(pg_result, "ERROR: svlan_p must be 0-7");
1747 }
1748 return count;
1749 }
1750
1751 if (!strcmp(name, "svlan_cfi")) {
1752 len = num_arg(&user_buffer[i], 1, &value);
1753 if (len < 0)
1754 return len;
1755
1756 i += len;
1757 if ((value <= 1) && (pkt_dev->svlan_id != 0xffff)) {
1758 pkt_dev->svlan_cfi = value;
1759 sprintf(pg_result, "OK: svlan_cfi=%u", pkt_dev->svlan_cfi);
1760 } else {
1761 sprintf(pg_result, "ERROR: svlan_cfi must be 0-1");
1762 }
1763 return count;
1764 }
1765
1766 if (!strcmp(name, "tos")) {
1767 __u32 tmp_value = 0;
1768 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1769 if (len < 0)
1770 return len;
1771
1772 i += len;
1773 if (len == 2) {
1774 pkt_dev->tos = tmp_value;
1775 sprintf(pg_result, "OK: tos=0x%02x", pkt_dev->tos);
1776 } else {
1777 sprintf(pg_result, "ERROR: tos must be 00-ff");
1778 }
1779 return count;
1780 }
1781
1782 if (!strcmp(name, "traffic_class")) {
1783 __u32 tmp_value = 0;
1784 len = hex32_arg(&user_buffer[i], 2, &tmp_value);
1785 if (len < 0)
1786 return len;
1787
1788 i += len;
1789 if (len == 2) {
1790 pkt_dev->traffic_class = tmp_value;
1791 sprintf(pg_result, "OK: traffic_class=0x%02x", pkt_dev->traffic_class);
1792 } else {
1793 sprintf(pg_result, "ERROR: traffic_class must be 00-ff");
1794 }
1795 return count;
1796 }
1797
1798 if (!strcmp(name, "skb_priority")) {
1799 len = num_arg(&user_buffer[i], 9, &value);
1800 if (len < 0)
1801 return len;
1802
1803 i += len;
1804 pkt_dev->skb_priority = value;
1805 sprintf(pg_result, "OK: skb_priority=%i",
1806 pkt_dev->skb_priority);
1807 return count;
1808 }
1809
1810 sprintf(pkt_dev->result, "No such parameter \"%s\"", name);
1811 return -EINVAL;
1812 }
1813
pktgen_if_open(struct inode * inode,struct file * file)1814 static int pktgen_if_open(struct inode *inode, struct file *file)
1815 {
1816 return single_open(file, pktgen_if_show, pde_data(inode));
1817 }
1818
1819 static const struct proc_ops pktgen_if_proc_ops = {
1820 .proc_open = pktgen_if_open,
1821 .proc_read = seq_read,
1822 .proc_lseek = seq_lseek,
1823 .proc_write = pktgen_if_write,
1824 .proc_release = single_release,
1825 };
1826
pktgen_thread_show(struct seq_file * seq,void * v)1827 static int pktgen_thread_show(struct seq_file *seq, void *v)
1828 {
1829 struct pktgen_thread *t = seq->private;
1830 const struct pktgen_dev *pkt_dev;
1831
1832 BUG_ON(!t);
1833
1834 seq_puts(seq, "Running: ");
1835
1836 rcu_read_lock();
1837 list_for_each_entry_rcu(pkt_dev, &t->if_list, list)
1838 if (pkt_dev->running)
1839 seq_printf(seq, "%s ", pkt_dev->odevname);
1840
1841 seq_puts(seq, "\nStopped: ");
1842
1843 list_for_each_entry_rcu(pkt_dev, &t->if_list, list)
1844 if (!pkt_dev->running)
1845 seq_printf(seq, "%s ", pkt_dev->odevname);
1846
1847 if (t->result[0])
1848 seq_printf(seq, "\nResult: %s\n", t->result);
1849 else
1850 seq_puts(seq, "\nResult: NA\n");
1851
1852 rcu_read_unlock();
1853
1854 return 0;
1855 }
1856
pktgen_thread_write(struct file * file,const char __user * user_buffer,size_t count,loff_t * offset)1857 static ssize_t pktgen_thread_write(struct file *file,
1858 const char __user * user_buffer,
1859 size_t count, loff_t * offset)
1860 {
1861 struct seq_file *seq = file->private_data;
1862 struct pktgen_thread *t = seq->private;
1863 int i, max, len, ret;
1864 char name[40];
1865 char *pg_result;
1866
1867 if (count < 1) {
1868 // sprintf(pg_result, "Wrong command format");
1869 return -EINVAL;
1870 }
1871
1872 max = count;
1873 len = count_trail_chars(user_buffer, max);
1874 if (len < 0)
1875 return len;
1876
1877 i = len;
1878
1879 /* Read variable name */
1880 max = min(sizeof(name) - 1, count - i);
1881 len = strn_len(&user_buffer[i], max);
1882 if (len < 0)
1883 return len;
1884
1885 memset(name, 0, sizeof(name));
1886 if (copy_from_user(name, &user_buffer[i], len))
1887 return -EFAULT;
1888 i += len;
1889
1890 max = count - i;
1891 len = count_trail_chars(&user_buffer[i], max);
1892 if (len < 0)
1893 return len;
1894
1895 i += len;
1896
1897 if (debug)
1898 pr_debug("t=%s, count=%lu\n", name, (unsigned long)count);
1899
1900 if (!t) {
1901 pr_err("ERROR: No thread\n");
1902 ret = -EINVAL;
1903 goto out;
1904 }
1905
1906 pg_result = &(t->result[0]);
1907
1908 if (!strcmp(name, "add_device")) {
1909 char f[32];
1910 memset(f, 0, 32);
1911 max = min(sizeof(f) - 1, count - i);
1912 len = strn_len(&user_buffer[i], max);
1913 if (len < 0) {
1914 ret = len;
1915 goto out;
1916 }
1917 if (copy_from_user(f, &user_buffer[i], len))
1918 return -EFAULT;
1919 i += len;
1920 mutex_lock(&pktgen_thread_lock);
1921 ret = pktgen_add_device(t, f);
1922 mutex_unlock(&pktgen_thread_lock);
1923 if (!ret) {
1924 ret = count;
1925 sprintf(pg_result, "OK: add_device=%s", f);
1926 } else
1927 sprintf(pg_result, "ERROR: can not add device %s", f);
1928 goto out;
1929 }
1930
1931 if (!strcmp(name, "rem_device_all")) {
1932 mutex_lock(&pktgen_thread_lock);
1933 t->control |= T_REMDEVALL;
1934 mutex_unlock(&pktgen_thread_lock);
1935 schedule_timeout_interruptible(msecs_to_jiffies(125)); /* Propagate thread->control */
1936 ret = count;
1937 sprintf(pg_result, "OK: rem_device_all");
1938 goto out;
1939 }
1940
1941 if (!strcmp(name, "max_before_softirq")) {
1942 sprintf(pg_result, "OK: Note! max_before_softirq is obsoleted -- Do not use");
1943 ret = count;
1944 goto out;
1945 }
1946
1947 ret = -EINVAL;
1948 out:
1949 return ret;
1950 }
1951
pktgen_thread_open(struct inode * inode,struct file * file)1952 static int pktgen_thread_open(struct inode *inode, struct file *file)
1953 {
1954 return single_open(file, pktgen_thread_show, pde_data(inode));
1955 }
1956
1957 static const struct proc_ops pktgen_thread_proc_ops = {
1958 .proc_open = pktgen_thread_open,
1959 .proc_read = seq_read,
1960 .proc_lseek = seq_lseek,
1961 .proc_write = pktgen_thread_write,
1962 .proc_release = single_release,
1963 };
1964
1965 /* Think find or remove for NN */
__pktgen_NN_threads(const struct pktgen_net * pn,const char * ifname,int remove)1966 static struct pktgen_dev *__pktgen_NN_threads(const struct pktgen_net *pn,
1967 const char *ifname, int remove)
1968 {
1969 struct pktgen_thread *t;
1970 struct pktgen_dev *pkt_dev = NULL;
1971 bool exact = (remove == FIND);
1972
1973 list_for_each_entry(t, &pn->pktgen_threads, th_list) {
1974 pkt_dev = pktgen_find_dev(t, ifname, exact);
1975 if (pkt_dev) {
1976 if (remove) {
1977 pkt_dev->removal_mark = 1;
1978 t->control |= T_REMDEV;
1979 }
1980 break;
1981 }
1982 }
1983 return pkt_dev;
1984 }
1985
1986 /*
1987 * mark a device for removal
1988 */
pktgen_mark_device(const struct pktgen_net * pn,const char * ifname)1989 static void pktgen_mark_device(const struct pktgen_net *pn, const char *ifname)
1990 {
1991 struct pktgen_dev *pkt_dev = NULL;
1992 const int max_tries = 10, msec_per_try = 125;
1993 int i = 0;
1994
1995 mutex_lock(&pktgen_thread_lock);
1996 pr_debug("%s: marking %s for removal\n", __func__, ifname);
1997
1998 while (1) {
1999
2000 pkt_dev = __pktgen_NN_threads(pn, ifname, REMOVE);
2001 if (pkt_dev == NULL)
2002 break; /* success */
2003
2004 mutex_unlock(&pktgen_thread_lock);
2005 pr_debug("%s: waiting for %s to disappear....\n",
2006 __func__, ifname);
2007 schedule_timeout_interruptible(msecs_to_jiffies(msec_per_try));
2008 mutex_lock(&pktgen_thread_lock);
2009
2010 if (++i >= max_tries) {
2011 pr_err("%s: timed out after waiting %d msec for device %s to be removed\n",
2012 __func__, msec_per_try * i, ifname);
2013 break;
2014 }
2015
2016 }
2017
2018 mutex_unlock(&pktgen_thread_lock);
2019 }
2020
pktgen_change_name(const struct pktgen_net * pn,struct net_device * dev)2021 static void pktgen_change_name(const struct pktgen_net *pn, struct net_device *dev)
2022 {
2023 struct pktgen_thread *t;
2024
2025 mutex_lock(&pktgen_thread_lock);
2026
2027 list_for_each_entry(t, &pn->pktgen_threads, th_list) {
2028 struct pktgen_dev *pkt_dev;
2029
2030 if_lock(t);
2031 list_for_each_entry(pkt_dev, &t->if_list, list) {
2032 if (pkt_dev->odev != dev)
2033 continue;
2034
2035 proc_remove(pkt_dev->entry);
2036
2037 pkt_dev->entry = proc_create_data(dev->name, 0600,
2038 pn->proc_dir,
2039 &pktgen_if_proc_ops,
2040 pkt_dev);
2041 if (!pkt_dev->entry)
2042 pr_err("can't move proc entry for '%s'\n",
2043 dev->name);
2044 break;
2045 }
2046 if_unlock(t);
2047 }
2048 mutex_unlock(&pktgen_thread_lock);
2049 }
2050
pktgen_device_event(struct notifier_block * unused,unsigned long event,void * ptr)2051 static int pktgen_device_event(struct notifier_block *unused,
2052 unsigned long event, void *ptr)
2053 {
2054 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2055 struct pktgen_net *pn = net_generic(dev_net(dev), pg_net_id);
2056
2057 if (pn->pktgen_exiting)
2058 return NOTIFY_DONE;
2059
2060 /* It is OK that we do not hold the group lock right now,
2061 * as we run under the RTNL lock.
2062 */
2063
2064 switch (event) {
2065 case NETDEV_CHANGENAME:
2066 pktgen_change_name(pn, dev);
2067 break;
2068
2069 case NETDEV_UNREGISTER:
2070 pktgen_mark_device(pn, dev->name);
2071 break;
2072 }
2073
2074 return NOTIFY_DONE;
2075 }
2076
pktgen_dev_get_by_name(const struct pktgen_net * pn,struct pktgen_dev * pkt_dev,const char * ifname)2077 static struct net_device *pktgen_dev_get_by_name(const struct pktgen_net *pn,
2078 struct pktgen_dev *pkt_dev,
2079 const char *ifname)
2080 {
2081 char b[IFNAMSIZ+5];
2082 int i;
2083
2084 for (i = 0; ifname[i] != '@'; i++) {
2085 if (i == IFNAMSIZ)
2086 break;
2087
2088 b[i] = ifname[i];
2089 }
2090 b[i] = 0;
2091
2092 return dev_get_by_name(pn->net, b);
2093 }
2094
2095
2096 /* Associate pktgen_dev with a device. */
2097
pktgen_setup_dev(const struct pktgen_net * pn,struct pktgen_dev * pkt_dev,const char * ifname)2098 static int pktgen_setup_dev(const struct pktgen_net *pn,
2099 struct pktgen_dev *pkt_dev, const char *ifname)
2100 {
2101 struct net_device *odev;
2102 int err;
2103
2104 /* Clean old setups */
2105 if (pkt_dev->odev) {
2106 netdev_put(pkt_dev->odev, &pkt_dev->dev_tracker);
2107 pkt_dev->odev = NULL;
2108 }
2109
2110 odev = pktgen_dev_get_by_name(pn, pkt_dev, ifname);
2111 if (!odev) {
2112 pr_err("no such netdevice: \"%s\"\n", ifname);
2113 return -ENODEV;
2114 }
2115
2116 if (odev->type != ARPHRD_ETHER && odev->type != ARPHRD_LOOPBACK) {
2117 pr_err("not an ethernet or loopback device: \"%s\"\n", ifname);
2118 err = -EINVAL;
2119 } else if (!netif_running(odev)) {
2120 pr_err("device is down: \"%s\"\n", ifname);
2121 err = -ENETDOWN;
2122 } else {
2123 pkt_dev->odev = odev;
2124 netdev_tracker_alloc(odev, &pkt_dev->dev_tracker, GFP_KERNEL);
2125 return 0;
2126 }
2127
2128 dev_put(odev);
2129 return err;
2130 }
2131
2132 /* Read pkt_dev from the interface and set up internal pktgen_dev
2133 * structure to have the right information to create/send packets
2134 */
pktgen_setup_inject(struct pktgen_dev * pkt_dev)2135 static void pktgen_setup_inject(struct pktgen_dev *pkt_dev)
2136 {
2137 int ntxq;
2138
2139 if (!pkt_dev->odev) {
2140 pr_err("ERROR: pkt_dev->odev == NULL in setup_inject\n");
2141 sprintf(pkt_dev->result,
2142 "ERROR: pkt_dev->odev == NULL in setup_inject.\n");
2143 return;
2144 }
2145
2146 /* make sure that we don't pick a non-existing transmit queue */
2147 ntxq = pkt_dev->odev->real_num_tx_queues;
2148
2149 if (ntxq <= pkt_dev->queue_map_min) {
2150 pr_warn("WARNING: Requested queue_map_min (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
2151 pkt_dev->queue_map_min, (ntxq ?: 1) - 1, ntxq,
2152 pkt_dev->odevname);
2153 pkt_dev->queue_map_min = (ntxq ?: 1) - 1;
2154 }
2155 if (pkt_dev->queue_map_max >= ntxq) {
2156 pr_warn("WARNING: Requested queue_map_max (zero-based) (%d) exceeds valid range [0 - %d] for (%d) queues on %s, resetting\n",
2157 pkt_dev->queue_map_max, (ntxq ?: 1) - 1, ntxq,
2158 pkt_dev->odevname);
2159 pkt_dev->queue_map_max = (ntxq ?: 1) - 1;
2160 }
2161
2162 /* Default to the interface's mac if not explicitly set. */
2163
2164 if (is_zero_ether_addr(pkt_dev->src_mac))
2165 ether_addr_copy(&(pkt_dev->hh[6]), pkt_dev->odev->dev_addr);
2166
2167 /* Set up Dest MAC */
2168 ether_addr_copy(&(pkt_dev->hh[0]), pkt_dev->dst_mac);
2169
2170 if (pkt_dev->flags & F_IPV6) {
2171 int i, set = 0, err = 1;
2172 struct inet6_dev *idev;
2173
2174 if (pkt_dev->min_pkt_size == 0) {
2175 pkt_dev->min_pkt_size = 14 + sizeof(struct ipv6hdr)
2176 + sizeof(struct udphdr)
2177 + sizeof(struct pktgen_hdr)
2178 + pkt_dev->pkt_overhead;
2179 }
2180
2181 for (i = 0; i < sizeof(struct in6_addr); i++)
2182 if (pkt_dev->cur_in6_saddr.s6_addr[i]) {
2183 set = 1;
2184 break;
2185 }
2186
2187 if (!set) {
2188
2189 /*
2190 * Use linklevel address if unconfigured.
2191 *
2192 * use ipv6_get_lladdr if/when it's get exported
2193 */
2194
2195 rcu_read_lock();
2196 idev = __in6_dev_get(pkt_dev->odev);
2197 if (idev) {
2198 struct inet6_ifaddr *ifp;
2199
2200 read_lock_bh(&idev->lock);
2201 list_for_each_entry(ifp, &idev->addr_list, if_list) {
2202 if ((ifp->scope & IFA_LINK) &&
2203 !(ifp->flags & IFA_F_TENTATIVE)) {
2204 pkt_dev->cur_in6_saddr = ifp->addr;
2205 err = 0;
2206 break;
2207 }
2208 }
2209 read_unlock_bh(&idev->lock);
2210 }
2211 rcu_read_unlock();
2212 if (err)
2213 pr_err("ERROR: IPv6 link address not available\n");
2214 }
2215 } else {
2216 if (pkt_dev->min_pkt_size == 0) {
2217 pkt_dev->min_pkt_size = 14 + sizeof(struct iphdr)
2218 + sizeof(struct udphdr)
2219 + sizeof(struct pktgen_hdr)
2220 + pkt_dev->pkt_overhead;
2221 }
2222
2223 pkt_dev->saddr_min = 0;
2224 pkt_dev->saddr_max = 0;
2225 if (strlen(pkt_dev->src_min) == 0) {
2226
2227 struct in_device *in_dev;
2228
2229 rcu_read_lock();
2230 in_dev = __in_dev_get_rcu(pkt_dev->odev);
2231 if (in_dev) {
2232 const struct in_ifaddr *ifa;
2233
2234 ifa = rcu_dereference(in_dev->ifa_list);
2235 if (ifa) {
2236 pkt_dev->saddr_min = ifa->ifa_address;
2237 pkt_dev->saddr_max = pkt_dev->saddr_min;
2238 }
2239 }
2240 rcu_read_unlock();
2241 } else {
2242 pkt_dev->saddr_min = in_aton(pkt_dev->src_min);
2243 pkt_dev->saddr_max = in_aton(pkt_dev->src_max);
2244 }
2245
2246 pkt_dev->daddr_min = in_aton(pkt_dev->dst_min);
2247 pkt_dev->daddr_max = in_aton(pkt_dev->dst_max);
2248 }
2249 /* Initialize current values. */
2250 pkt_dev->cur_pkt_size = pkt_dev->min_pkt_size;
2251 if (pkt_dev->min_pkt_size > pkt_dev->max_pkt_size)
2252 pkt_dev->max_pkt_size = pkt_dev->min_pkt_size;
2253
2254 pkt_dev->cur_dst_mac_offset = 0;
2255 pkt_dev->cur_src_mac_offset = 0;
2256 pkt_dev->cur_saddr = pkt_dev->saddr_min;
2257 pkt_dev->cur_daddr = pkt_dev->daddr_min;
2258 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2259 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
2260 pkt_dev->nflows = 0;
2261 }
2262
2263
spin(struct pktgen_dev * pkt_dev,ktime_t spin_until)2264 static void spin(struct pktgen_dev *pkt_dev, ktime_t spin_until)
2265 {
2266 ktime_t start_time, end_time;
2267 s64 remaining;
2268 struct hrtimer_sleeper t;
2269
2270 hrtimer_init_sleeper_on_stack(&t, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
2271 hrtimer_set_expires(&t.timer, spin_until);
2272
2273 remaining = ktime_to_ns(hrtimer_expires_remaining(&t.timer));
2274 if (remaining <= 0)
2275 goto out;
2276
2277 start_time = ktime_get();
2278 if (remaining < 100000) {
2279 /* for small delays (<100us), just loop until limit is reached */
2280 do {
2281 end_time = ktime_get();
2282 } while (ktime_compare(end_time, spin_until) < 0);
2283 } else {
2284 do {
2285 set_current_state(TASK_INTERRUPTIBLE);
2286 hrtimer_sleeper_start_expires(&t, HRTIMER_MODE_ABS);
2287
2288 if (likely(t.task))
2289 schedule();
2290
2291 hrtimer_cancel(&t.timer);
2292 } while (t.task && pkt_dev->running && !signal_pending(current));
2293 __set_current_state(TASK_RUNNING);
2294 end_time = ktime_get();
2295 }
2296
2297 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(end_time, start_time));
2298 out:
2299 pkt_dev->next_tx = ktime_add_ns(spin_until, pkt_dev->delay);
2300 destroy_hrtimer_on_stack(&t.timer);
2301 }
2302
set_pkt_overhead(struct pktgen_dev * pkt_dev)2303 static inline void set_pkt_overhead(struct pktgen_dev *pkt_dev)
2304 {
2305 pkt_dev->pkt_overhead = 0;
2306 pkt_dev->pkt_overhead += pkt_dev->nr_labels*sizeof(u32);
2307 pkt_dev->pkt_overhead += VLAN_TAG_SIZE(pkt_dev);
2308 pkt_dev->pkt_overhead += SVLAN_TAG_SIZE(pkt_dev);
2309 }
2310
f_seen(const struct pktgen_dev * pkt_dev,int flow)2311 static inline int f_seen(const struct pktgen_dev *pkt_dev, int flow)
2312 {
2313 return !!(pkt_dev->flows[flow].flags & F_INIT);
2314 }
2315
f_pick(struct pktgen_dev * pkt_dev)2316 static inline int f_pick(struct pktgen_dev *pkt_dev)
2317 {
2318 int flow = pkt_dev->curfl;
2319
2320 if (pkt_dev->flags & F_FLOW_SEQ) {
2321 if (pkt_dev->flows[flow].count >= pkt_dev->lflow) {
2322 /* reset time */
2323 pkt_dev->flows[flow].count = 0;
2324 pkt_dev->flows[flow].flags = 0;
2325 pkt_dev->curfl += 1;
2326 if (pkt_dev->curfl >= pkt_dev->cflows)
2327 pkt_dev->curfl = 0; /*reset */
2328 }
2329 } else {
2330 flow = get_random_u32_below(pkt_dev->cflows);
2331 pkt_dev->curfl = flow;
2332
2333 if (pkt_dev->flows[flow].count > pkt_dev->lflow) {
2334 pkt_dev->flows[flow].count = 0;
2335 pkt_dev->flows[flow].flags = 0;
2336 }
2337 }
2338
2339 return pkt_dev->curfl;
2340 }
2341
2342
2343 #ifdef CONFIG_XFRM
2344 /* If there was already an IPSEC SA, we keep it as is, else
2345 * we go look for it ...
2346 */
2347 #define DUMMY_MARK 0
get_ipsec_sa(struct pktgen_dev * pkt_dev,int flow)2348 static void get_ipsec_sa(struct pktgen_dev *pkt_dev, int flow)
2349 {
2350 struct xfrm_state *x = pkt_dev->flows[flow].x;
2351 struct pktgen_net *pn = net_generic(dev_net(pkt_dev->odev), pg_net_id);
2352 if (!x) {
2353
2354 if (pkt_dev->spi) {
2355 /* We need as quick as possible to find the right SA
2356 * Searching with minimum criteria to archieve this.
2357 */
2358 x = xfrm_state_lookup_byspi(pn->net, htonl(pkt_dev->spi), AF_INET);
2359 } else {
2360 /* slow path: we dont already have xfrm_state */
2361 x = xfrm_stateonly_find(pn->net, DUMMY_MARK, 0,
2362 (xfrm_address_t *)&pkt_dev->cur_daddr,
2363 (xfrm_address_t *)&pkt_dev->cur_saddr,
2364 AF_INET,
2365 pkt_dev->ipsmode,
2366 pkt_dev->ipsproto, 0);
2367 }
2368 if (x) {
2369 pkt_dev->flows[flow].x = x;
2370 set_pkt_overhead(pkt_dev);
2371 pkt_dev->pkt_overhead += x->props.header_len;
2372 }
2373
2374 }
2375 }
2376 #endif
set_cur_queue_map(struct pktgen_dev * pkt_dev)2377 static void set_cur_queue_map(struct pktgen_dev *pkt_dev)
2378 {
2379
2380 if (pkt_dev->flags & F_QUEUE_MAP_CPU)
2381 pkt_dev->cur_queue_map = smp_processor_id();
2382
2383 else if (pkt_dev->queue_map_min <= pkt_dev->queue_map_max) {
2384 __u16 t;
2385 if (pkt_dev->flags & F_QUEUE_MAP_RND) {
2386 t = get_random_u32_inclusive(pkt_dev->queue_map_min,
2387 pkt_dev->queue_map_max);
2388 } else {
2389 t = pkt_dev->cur_queue_map + 1;
2390 if (t > pkt_dev->queue_map_max)
2391 t = pkt_dev->queue_map_min;
2392 }
2393 pkt_dev->cur_queue_map = t;
2394 }
2395 pkt_dev->cur_queue_map = pkt_dev->cur_queue_map % pkt_dev->odev->real_num_tx_queues;
2396 }
2397
2398 /* Increment/randomize headers according to flags and current values
2399 * for IP src/dest, UDP src/dst port, MAC-Addr src/dst
2400 */
mod_cur_headers(struct pktgen_dev * pkt_dev)2401 static void mod_cur_headers(struct pktgen_dev *pkt_dev)
2402 {
2403 __u32 imn;
2404 __u32 imx;
2405 int flow = 0;
2406
2407 if (pkt_dev->cflows)
2408 flow = f_pick(pkt_dev);
2409
2410 /* Deal with source MAC */
2411 if (pkt_dev->src_mac_count > 1) {
2412 __u32 mc;
2413 __u32 tmp;
2414
2415 if (pkt_dev->flags & F_MACSRC_RND)
2416 mc = get_random_u32_below(pkt_dev->src_mac_count);
2417 else {
2418 mc = pkt_dev->cur_src_mac_offset++;
2419 if (pkt_dev->cur_src_mac_offset >=
2420 pkt_dev->src_mac_count)
2421 pkt_dev->cur_src_mac_offset = 0;
2422 }
2423
2424 tmp = pkt_dev->src_mac[5] + (mc & 0xFF);
2425 pkt_dev->hh[11] = tmp;
2426 tmp = (pkt_dev->src_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2427 pkt_dev->hh[10] = tmp;
2428 tmp = (pkt_dev->src_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2429 pkt_dev->hh[9] = tmp;
2430 tmp = (pkt_dev->src_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2431 pkt_dev->hh[8] = tmp;
2432 tmp = (pkt_dev->src_mac[1] + (tmp >> 8));
2433 pkt_dev->hh[7] = tmp;
2434 }
2435
2436 /* Deal with Destination MAC */
2437 if (pkt_dev->dst_mac_count > 1) {
2438 __u32 mc;
2439 __u32 tmp;
2440
2441 if (pkt_dev->flags & F_MACDST_RND)
2442 mc = get_random_u32_below(pkt_dev->dst_mac_count);
2443
2444 else {
2445 mc = pkt_dev->cur_dst_mac_offset++;
2446 if (pkt_dev->cur_dst_mac_offset >=
2447 pkt_dev->dst_mac_count) {
2448 pkt_dev->cur_dst_mac_offset = 0;
2449 }
2450 }
2451
2452 tmp = pkt_dev->dst_mac[5] + (mc & 0xFF);
2453 pkt_dev->hh[5] = tmp;
2454 tmp = (pkt_dev->dst_mac[4] + ((mc >> 8) & 0xFF) + (tmp >> 8));
2455 pkt_dev->hh[4] = tmp;
2456 tmp = (pkt_dev->dst_mac[3] + ((mc >> 16) & 0xFF) + (tmp >> 8));
2457 pkt_dev->hh[3] = tmp;
2458 tmp = (pkt_dev->dst_mac[2] + ((mc >> 24) & 0xFF) + (tmp >> 8));
2459 pkt_dev->hh[2] = tmp;
2460 tmp = (pkt_dev->dst_mac[1] + (tmp >> 8));
2461 pkt_dev->hh[1] = tmp;
2462 }
2463
2464 if (pkt_dev->flags & F_MPLS_RND) {
2465 unsigned int i;
2466 for (i = 0; i < pkt_dev->nr_labels; i++)
2467 if (pkt_dev->labels[i] & MPLS_STACK_BOTTOM)
2468 pkt_dev->labels[i] = MPLS_STACK_BOTTOM |
2469 ((__force __be32)get_random_u32() &
2470 htonl(0x000fffff));
2471 }
2472
2473 if ((pkt_dev->flags & F_VID_RND) && (pkt_dev->vlan_id != 0xffff)) {
2474 pkt_dev->vlan_id = get_random_u32_below(4096);
2475 }
2476
2477 if ((pkt_dev->flags & F_SVID_RND) && (pkt_dev->svlan_id != 0xffff)) {
2478 pkt_dev->svlan_id = get_random_u32_below(4096);
2479 }
2480
2481 if (pkt_dev->udp_src_min < pkt_dev->udp_src_max) {
2482 if (pkt_dev->flags & F_UDPSRC_RND)
2483 pkt_dev->cur_udp_src = get_random_u32_inclusive(pkt_dev->udp_src_min,
2484 pkt_dev->udp_src_max - 1);
2485
2486 else {
2487 pkt_dev->cur_udp_src++;
2488 if (pkt_dev->cur_udp_src >= pkt_dev->udp_src_max)
2489 pkt_dev->cur_udp_src = pkt_dev->udp_src_min;
2490 }
2491 }
2492
2493 if (pkt_dev->udp_dst_min < pkt_dev->udp_dst_max) {
2494 if (pkt_dev->flags & F_UDPDST_RND) {
2495 pkt_dev->cur_udp_dst = get_random_u32_inclusive(pkt_dev->udp_dst_min,
2496 pkt_dev->udp_dst_max - 1);
2497 } else {
2498 pkt_dev->cur_udp_dst++;
2499 if (pkt_dev->cur_udp_dst >= pkt_dev->udp_dst_max)
2500 pkt_dev->cur_udp_dst = pkt_dev->udp_dst_min;
2501 }
2502 }
2503
2504 if (!(pkt_dev->flags & F_IPV6)) {
2505
2506 imn = ntohl(pkt_dev->saddr_min);
2507 imx = ntohl(pkt_dev->saddr_max);
2508 if (imn < imx) {
2509 __u32 t;
2510 if (pkt_dev->flags & F_IPSRC_RND)
2511 t = get_random_u32_inclusive(imn, imx - 1);
2512 else {
2513 t = ntohl(pkt_dev->cur_saddr);
2514 t++;
2515 if (t > imx)
2516 t = imn;
2517
2518 }
2519 pkt_dev->cur_saddr = htonl(t);
2520 }
2521
2522 if (pkt_dev->cflows && f_seen(pkt_dev, flow)) {
2523 pkt_dev->cur_daddr = pkt_dev->flows[flow].cur_daddr;
2524 } else {
2525 imn = ntohl(pkt_dev->daddr_min);
2526 imx = ntohl(pkt_dev->daddr_max);
2527 if (imn < imx) {
2528 __u32 t;
2529 __be32 s;
2530 if (pkt_dev->flags & F_IPDST_RND) {
2531
2532 do {
2533 t = get_random_u32_inclusive(imn, imx - 1);
2534 s = htonl(t);
2535 } while (ipv4_is_loopback(s) ||
2536 ipv4_is_multicast(s) ||
2537 ipv4_is_lbcast(s) ||
2538 ipv4_is_zeronet(s) ||
2539 ipv4_is_local_multicast(s));
2540 pkt_dev->cur_daddr = s;
2541 } else {
2542 t = ntohl(pkt_dev->cur_daddr);
2543 t++;
2544 if (t > imx) {
2545 t = imn;
2546 }
2547 pkt_dev->cur_daddr = htonl(t);
2548 }
2549 }
2550 if (pkt_dev->cflows) {
2551 pkt_dev->flows[flow].flags |= F_INIT;
2552 pkt_dev->flows[flow].cur_daddr =
2553 pkt_dev->cur_daddr;
2554 #ifdef CONFIG_XFRM
2555 if (pkt_dev->flags & F_IPSEC)
2556 get_ipsec_sa(pkt_dev, flow);
2557 #endif
2558 pkt_dev->nflows++;
2559 }
2560 }
2561 } else { /* IPV6 * */
2562
2563 if (!ipv6_addr_any(&pkt_dev->min_in6_daddr)) {
2564 int i;
2565
2566 /* Only random destinations yet */
2567
2568 for (i = 0; i < 4; i++) {
2569 pkt_dev->cur_in6_daddr.s6_addr32[i] =
2570 (((__force __be32)get_random_u32() |
2571 pkt_dev->min_in6_daddr.s6_addr32[i]) &
2572 pkt_dev->max_in6_daddr.s6_addr32[i]);
2573 }
2574 }
2575 }
2576
2577 if (pkt_dev->min_pkt_size < pkt_dev->max_pkt_size) {
2578 __u32 t;
2579 if (pkt_dev->flags & F_TXSIZE_RND) {
2580 t = get_random_u32_inclusive(pkt_dev->min_pkt_size,
2581 pkt_dev->max_pkt_size - 1);
2582 } else {
2583 t = pkt_dev->cur_pkt_size + 1;
2584 if (t > pkt_dev->max_pkt_size)
2585 t = pkt_dev->min_pkt_size;
2586 }
2587 pkt_dev->cur_pkt_size = t;
2588 } else if (pkt_dev->n_imix_entries > 0) {
2589 struct imix_pkt *entry;
2590 __u32 t = get_random_u32_below(IMIX_PRECISION);
2591 __u8 entry_index = pkt_dev->imix_distribution[t];
2592
2593 entry = &pkt_dev->imix_entries[entry_index];
2594 entry->count_so_far++;
2595 pkt_dev->cur_pkt_size = entry->size;
2596 }
2597
2598 set_cur_queue_map(pkt_dev);
2599
2600 pkt_dev->flows[flow].count++;
2601 }
2602
fill_imix_distribution(struct pktgen_dev * pkt_dev)2603 static void fill_imix_distribution(struct pktgen_dev *pkt_dev)
2604 {
2605 int cumulative_probabilites[MAX_IMIX_ENTRIES];
2606 int j = 0;
2607 __u64 cumulative_prob = 0;
2608 __u64 total_weight = 0;
2609 int i = 0;
2610
2611 for (i = 0; i < pkt_dev->n_imix_entries; i++)
2612 total_weight += pkt_dev->imix_entries[i].weight;
2613
2614 /* Fill cumulative_probabilites with sum of normalized probabilities */
2615 for (i = 0; i < pkt_dev->n_imix_entries - 1; i++) {
2616 cumulative_prob += div64_u64(pkt_dev->imix_entries[i].weight *
2617 IMIX_PRECISION,
2618 total_weight);
2619 cumulative_probabilites[i] = cumulative_prob;
2620 }
2621 cumulative_probabilites[pkt_dev->n_imix_entries - 1] = 100;
2622
2623 for (i = 0; i < IMIX_PRECISION; i++) {
2624 if (i == cumulative_probabilites[j])
2625 j++;
2626 pkt_dev->imix_distribution[i] = j;
2627 }
2628 }
2629
2630 #ifdef CONFIG_XFRM
2631 static u32 pktgen_dst_metrics[RTAX_MAX + 1] = {
2632
2633 [RTAX_HOPLIMIT] = 0x5, /* Set a static hoplimit */
2634 };
2635
pktgen_output_ipsec(struct sk_buff * skb,struct pktgen_dev * pkt_dev)2636 static int pktgen_output_ipsec(struct sk_buff *skb, struct pktgen_dev *pkt_dev)
2637 {
2638 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2639 int err = 0;
2640 struct net *net = dev_net(pkt_dev->odev);
2641
2642 if (!x)
2643 return 0;
2644 /* XXX: we dont support tunnel mode for now until
2645 * we resolve the dst issue */
2646 if ((x->props.mode != XFRM_MODE_TRANSPORT) && (pkt_dev->spi == 0))
2647 return 0;
2648
2649 /* But when user specify an valid SPI, transformation
2650 * supports both transport/tunnel mode + ESP/AH type.
2651 */
2652 if ((x->props.mode == XFRM_MODE_TUNNEL) && (pkt_dev->spi != 0))
2653 skb->_skb_refdst = (unsigned long)&pkt_dev->xdst.u.dst | SKB_DST_NOREF;
2654
2655 rcu_read_lock_bh();
2656 err = pktgen_xfrm_outer_mode_output(x, skb);
2657 rcu_read_unlock_bh();
2658 if (err) {
2659 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEMODEERROR);
2660 goto error;
2661 }
2662 err = x->type->output(x, skb);
2663 if (err) {
2664 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEPROTOERROR);
2665 goto error;
2666 }
2667 spin_lock_bh(&x->lock);
2668 x->curlft.bytes += skb->len;
2669 x->curlft.packets++;
2670 spin_unlock_bh(&x->lock);
2671 error:
2672 return err;
2673 }
2674
free_SAs(struct pktgen_dev * pkt_dev)2675 static void free_SAs(struct pktgen_dev *pkt_dev)
2676 {
2677 if (pkt_dev->cflows) {
2678 /* let go of the SAs if we have them */
2679 int i;
2680 for (i = 0; i < pkt_dev->cflows; i++) {
2681 struct xfrm_state *x = pkt_dev->flows[i].x;
2682 if (x) {
2683 xfrm_state_put(x);
2684 pkt_dev->flows[i].x = NULL;
2685 }
2686 }
2687 }
2688 }
2689
process_ipsec(struct pktgen_dev * pkt_dev,struct sk_buff * skb,__be16 protocol)2690 static int process_ipsec(struct pktgen_dev *pkt_dev,
2691 struct sk_buff *skb, __be16 protocol)
2692 {
2693 if (pkt_dev->flags & F_IPSEC) {
2694 struct xfrm_state *x = pkt_dev->flows[pkt_dev->curfl].x;
2695 int nhead = 0;
2696 if (x) {
2697 struct ethhdr *eth;
2698 struct iphdr *iph;
2699 int ret;
2700
2701 nhead = x->props.header_len - skb_headroom(skb);
2702 if (nhead > 0) {
2703 ret = pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
2704 if (ret < 0) {
2705 pr_err("Error expanding ipsec packet %d\n",
2706 ret);
2707 goto err;
2708 }
2709 }
2710
2711 /* ipsec is not expecting ll header */
2712 skb_pull(skb, ETH_HLEN);
2713 ret = pktgen_output_ipsec(skb, pkt_dev);
2714 if (ret) {
2715 pr_err("Error creating ipsec packet %d\n", ret);
2716 goto err;
2717 }
2718 /* restore ll */
2719 eth = skb_push(skb, ETH_HLEN);
2720 memcpy(eth, pkt_dev->hh, 2 * ETH_ALEN);
2721 eth->h_proto = protocol;
2722
2723 /* Update IPv4 header len as well as checksum value */
2724 iph = ip_hdr(skb);
2725 iph->tot_len = htons(skb->len - ETH_HLEN);
2726 ip_send_check(iph);
2727 }
2728 }
2729 return 1;
2730 err:
2731 kfree_skb(skb);
2732 return 0;
2733 }
2734 #endif
2735
mpls_push(__be32 * mpls,struct pktgen_dev * pkt_dev)2736 static void mpls_push(__be32 *mpls, struct pktgen_dev *pkt_dev)
2737 {
2738 unsigned int i;
2739 for (i = 0; i < pkt_dev->nr_labels; i++)
2740 *mpls++ = pkt_dev->labels[i] & ~MPLS_STACK_BOTTOM;
2741
2742 mpls--;
2743 *mpls |= MPLS_STACK_BOTTOM;
2744 }
2745
build_tci(unsigned int id,unsigned int cfi,unsigned int prio)2746 static inline __be16 build_tci(unsigned int id, unsigned int cfi,
2747 unsigned int prio)
2748 {
2749 return htons(id | (cfi << 12) | (prio << 13));
2750 }
2751
pktgen_finalize_skb(struct pktgen_dev * pkt_dev,struct sk_buff * skb,int datalen)2752 static void pktgen_finalize_skb(struct pktgen_dev *pkt_dev, struct sk_buff *skb,
2753 int datalen)
2754 {
2755 struct timespec64 timestamp;
2756 struct pktgen_hdr *pgh;
2757
2758 pgh = skb_put(skb, sizeof(*pgh));
2759 datalen -= sizeof(*pgh);
2760
2761 if (pkt_dev->nfrags <= 0) {
2762 skb_put_zero(skb, datalen);
2763 } else {
2764 int frags = pkt_dev->nfrags;
2765 int i, len;
2766 int frag_len;
2767
2768
2769 if (frags > MAX_SKB_FRAGS)
2770 frags = MAX_SKB_FRAGS;
2771 len = datalen - frags * PAGE_SIZE;
2772 if (len > 0) {
2773 skb_put_zero(skb, len);
2774 datalen = frags * PAGE_SIZE;
2775 }
2776
2777 i = 0;
2778 frag_len = (datalen/frags) < PAGE_SIZE ?
2779 (datalen/frags) : PAGE_SIZE;
2780 while (datalen > 0) {
2781 if (unlikely(!pkt_dev->page)) {
2782 int node = numa_node_id();
2783
2784 if (pkt_dev->node >= 0 && (pkt_dev->flags & F_NODE))
2785 node = pkt_dev->node;
2786 pkt_dev->page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
2787 if (!pkt_dev->page)
2788 break;
2789 }
2790 get_page(pkt_dev->page);
2791
2792 /*last fragment, fill rest of data*/
2793 if (i == (frags - 1))
2794 skb_frag_fill_page_desc(&skb_shinfo(skb)->frags[i],
2795 pkt_dev->page, 0,
2796 (datalen < PAGE_SIZE ?
2797 datalen : PAGE_SIZE));
2798 else
2799 skb_frag_fill_page_desc(&skb_shinfo(skb)->frags[i],
2800 pkt_dev->page, 0, frag_len);
2801
2802 datalen -= skb_frag_size(&skb_shinfo(skb)->frags[i]);
2803 skb->len += skb_frag_size(&skb_shinfo(skb)->frags[i]);
2804 skb->data_len += skb_frag_size(&skb_shinfo(skb)->frags[i]);
2805 i++;
2806 skb_shinfo(skb)->nr_frags = i;
2807 }
2808 }
2809
2810 /* Stamp the time, and sequence number,
2811 * convert them to network byte order
2812 */
2813 pgh->pgh_magic = htonl(PKTGEN_MAGIC);
2814 pgh->seq_num = htonl(pkt_dev->seq_num);
2815
2816 if (pkt_dev->flags & F_NO_TIMESTAMP) {
2817 pgh->tv_sec = 0;
2818 pgh->tv_usec = 0;
2819 } else {
2820 /*
2821 * pgh->tv_sec wraps in y2106 when interpreted as unsigned
2822 * as done by wireshark, or y2038 when interpreted as signed.
2823 * This is probably harmless, but if anyone wants to improve
2824 * it, we could introduce a variant that puts 64-bit nanoseconds
2825 * into the respective header bytes.
2826 * This would also be slightly faster to read.
2827 */
2828 ktime_get_real_ts64(×tamp);
2829 pgh->tv_sec = htonl(timestamp.tv_sec);
2830 pgh->tv_usec = htonl(timestamp.tv_nsec / NSEC_PER_USEC);
2831 }
2832 }
2833
pktgen_alloc_skb(struct net_device * dev,struct pktgen_dev * pkt_dev)2834 static struct sk_buff *pktgen_alloc_skb(struct net_device *dev,
2835 struct pktgen_dev *pkt_dev)
2836 {
2837 unsigned int extralen = LL_RESERVED_SPACE(dev);
2838 struct sk_buff *skb = NULL;
2839 unsigned int size;
2840
2841 size = pkt_dev->cur_pkt_size + 64 + extralen + pkt_dev->pkt_overhead;
2842 if (pkt_dev->flags & F_NODE) {
2843 int node = pkt_dev->node >= 0 ? pkt_dev->node : numa_node_id();
2844
2845 skb = __alloc_skb(NET_SKB_PAD + size, GFP_NOWAIT, 0, node);
2846 if (likely(skb)) {
2847 skb_reserve(skb, NET_SKB_PAD);
2848 skb->dev = dev;
2849 }
2850 } else {
2851 skb = __netdev_alloc_skb(dev, size, GFP_NOWAIT);
2852 }
2853
2854 /* the caller pre-fetches from skb->data and reserves for the mac hdr */
2855 if (likely(skb))
2856 skb_reserve(skb, extralen - 16);
2857
2858 return skb;
2859 }
2860
fill_packet_ipv4(struct net_device * odev,struct pktgen_dev * pkt_dev)2861 static struct sk_buff *fill_packet_ipv4(struct net_device *odev,
2862 struct pktgen_dev *pkt_dev)
2863 {
2864 struct sk_buff *skb = NULL;
2865 __u8 *eth;
2866 struct udphdr *udph;
2867 int datalen, iplen;
2868 struct iphdr *iph;
2869 __be16 protocol = htons(ETH_P_IP);
2870 __be32 *mpls;
2871 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
2872 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
2873 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
2874 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
2875 u16 queue_map;
2876
2877 if (pkt_dev->nr_labels)
2878 protocol = htons(ETH_P_MPLS_UC);
2879
2880 if (pkt_dev->vlan_id != 0xffff)
2881 protocol = htons(ETH_P_8021Q);
2882
2883 /* Update any of the values, used when we're incrementing various
2884 * fields.
2885 */
2886 mod_cur_headers(pkt_dev);
2887 queue_map = pkt_dev->cur_queue_map;
2888
2889 skb = pktgen_alloc_skb(odev, pkt_dev);
2890 if (!skb) {
2891 sprintf(pkt_dev->result, "No memory");
2892 return NULL;
2893 }
2894
2895 prefetchw(skb->data);
2896 skb_reserve(skb, 16);
2897
2898 /* Reserve for ethernet and IP header */
2899 eth = skb_push(skb, 14);
2900 mpls = skb_put(skb, pkt_dev->nr_labels * sizeof(__u32));
2901 if (pkt_dev->nr_labels)
2902 mpls_push(mpls, pkt_dev);
2903
2904 if (pkt_dev->vlan_id != 0xffff) {
2905 if (pkt_dev->svlan_id != 0xffff) {
2906 svlan_tci = skb_put(skb, sizeof(__be16));
2907 *svlan_tci = build_tci(pkt_dev->svlan_id,
2908 pkt_dev->svlan_cfi,
2909 pkt_dev->svlan_p);
2910 svlan_encapsulated_proto = skb_put(skb,
2911 sizeof(__be16));
2912 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
2913 }
2914 vlan_tci = skb_put(skb, sizeof(__be16));
2915 *vlan_tci = build_tci(pkt_dev->vlan_id,
2916 pkt_dev->vlan_cfi,
2917 pkt_dev->vlan_p);
2918 vlan_encapsulated_proto = skb_put(skb, sizeof(__be16));
2919 *vlan_encapsulated_proto = htons(ETH_P_IP);
2920 }
2921
2922 skb_reset_mac_header(skb);
2923 skb_set_network_header(skb, skb->len);
2924 iph = skb_put(skb, sizeof(struct iphdr));
2925
2926 skb_set_transport_header(skb, skb->len);
2927 udph = skb_put(skb, sizeof(struct udphdr));
2928 skb_set_queue_mapping(skb, queue_map);
2929 skb->priority = pkt_dev->skb_priority;
2930
2931 memcpy(eth, pkt_dev->hh, 12);
2932 *(__be16 *) & eth[12] = protocol;
2933
2934 /* Eth + IPh + UDPh + mpls */
2935 datalen = pkt_dev->cur_pkt_size - 14 - 20 - 8 -
2936 pkt_dev->pkt_overhead;
2937 if (datalen < 0 || datalen < sizeof(struct pktgen_hdr))
2938 datalen = sizeof(struct pktgen_hdr);
2939
2940 udph->source = htons(pkt_dev->cur_udp_src);
2941 udph->dest = htons(pkt_dev->cur_udp_dst);
2942 udph->len = htons(datalen + 8); /* DATA + udphdr */
2943 udph->check = 0;
2944
2945 iph->ihl = 5;
2946 iph->version = 4;
2947 iph->ttl = 32;
2948 iph->tos = pkt_dev->tos;
2949 iph->protocol = IPPROTO_UDP; /* UDP */
2950 iph->saddr = pkt_dev->cur_saddr;
2951 iph->daddr = pkt_dev->cur_daddr;
2952 iph->id = htons(pkt_dev->ip_id);
2953 pkt_dev->ip_id++;
2954 iph->frag_off = 0;
2955 iplen = 20 + 8 + datalen;
2956 iph->tot_len = htons(iplen);
2957 ip_send_check(iph);
2958 skb->protocol = protocol;
2959 skb->dev = odev;
2960 skb->pkt_type = PACKET_HOST;
2961
2962 pktgen_finalize_skb(pkt_dev, skb, datalen);
2963
2964 if (!(pkt_dev->flags & F_UDPCSUM)) {
2965 skb->ip_summed = CHECKSUM_NONE;
2966 } else if (odev->features & (NETIF_F_HW_CSUM | NETIF_F_IP_CSUM)) {
2967 skb->ip_summed = CHECKSUM_PARTIAL;
2968 skb->csum = 0;
2969 udp4_hwcsum(skb, iph->saddr, iph->daddr);
2970 } else {
2971 __wsum csum = skb_checksum(skb, skb_transport_offset(skb), datalen + 8, 0);
2972
2973 /* add protocol-dependent pseudo-header */
2974 udph->check = csum_tcpudp_magic(iph->saddr, iph->daddr,
2975 datalen + 8, IPPROTO_UDP, csum);
2976
2977 if (udph->check == 0)
2978 udph->check = CSUM_MANGLED_0;
2979 }
2980
2981 #ifdef CONFIG_XFRM
2982 if (!process_ipsec(pkt_dev, skb, protocol))
2983 return NULL;
2984 #endif
2985
2986 return skb;
2987 }
2988
fill_packet_ipv6(struct net_device * odev,struct pktgen_dev * pkt_dev)2989 static struct sk_buff *fill_packet_ipv6(struct net_device *odev,
2990 struct pktgen_dev *pkt_dev)
2991 {
2992 struct sk_buff *skb = NULL;
2993 __u8 *eth;
2994 struct udphdr *udph;
2995 int datalen, udplen;
2996 struct ipv6hdr *iph;
2997 __be16 protocol = htons(ETH_P_IPV6);
2998 __be32 *mpls;
2999 __be16 *vlan_tci = NULL; /* Encapsulates priority and VLAN ID */
3000 __be16 *vlan_encapsulated_proto = NULL; /* packet type ID field (or len) for VLAN tag */
3001 __be16 *svlan_tci = NULL; /* Encapsulates priority and SVLAN ID */
3002 __be16 *svlan_encapsulated_proto = NULL; /* packet type ID field (or len) for SVLAN tag */
3003 u16 queue_map;
3004
3005 if (pkt_dev->nr_labels)
3006 protocol = htons(ETH_P_MPLS_UC);
3007
3008 if (pkt_dev->vlan_id != 0xffff)
3009 protocol = htons(ETH_P_8021Q);
3010
3011 /* Update any of the values, used when we're incrementing various
3012 * fields.
3013 */
3014 mod_cur_headers(pkt_dev);
3015 queue_map = pkt_dev->cur_queue_map;
3016
3017 skb = pktgen_alloc_skb(odev, pkt_dev);
3018 if (!skb) {
3019 sprintf(pkt_dev->result, "No memory");
3020 return NULL;
3021 }
3022
3023 prefetchw(skb->data);
3024 skb_reserve(skb, 16);
3025
3026 /* Reserve for ethernet and IP header */
3027 eth = skb_push(skb, 14);
3028 mpls = skb_put(skb, pkt_dev->nr_labels * sizeof(__u32));
3029 if (pkt_dev->nr_labels)
3030 mpls_push(mpls, pkt_dev);
3031
3032 if (pkt_dev->vlan_id != 0xffff) {
3033 if (pkt_dev->svlan_id != 0xffff) {
3034 svlan_tci = skb_put(skb, sizeof(__be16));
3035 *svlan_tci = build_tci(pkt_dev->svlan_id,
3036 pkt_dev->svlan_cfi,
3037 pkt_dev->svlan_p);
3038 svlan_encapsulated_proto = skb_put(skb,
3039 sizeof(__be16));
3040 *svlan_encapsulated_proto = htons(ETH_P_8021Q);
3041 }
3042 vlan_tci = skb_put(skb, sizeof(__be16));
3043 *vlan_tci = build_tci(pkt_dev->vlan_id,
3044 pkt_dev->vlan_cfi,
3045 pkt_dev->vlan_p);
3046 vlan_encapsulated_proto = skb_put(skb, sizeof(__be16));
3047 *vlan_encapsulated_proto = htons(ETH_P_IPV6);
3048 }
3049
3050 skb_reset_mac_header(skb);
3051 skb_set_network_header(skb, skb->len);
3052 iph = skb_put(skb, sizeof(struct ipv6hdr));
3053
3054 skb_set_transport_header(skb, skb->len);
3055 udph = skb_put(skb, sizeof(struct udphdr));
3056 skb_set_queue_mapping(skb, queue_map);
3057 skb->priority = pkt_dev->skb_priority;
3058
3059 memcpy(eth, pkt_dev->hh, 12);
3060 *(__be16 *) ð[12] = protocol;
3061
3062 /* Eth + IPh + UDPh + mpls */
3063 datalen = pkt_dev->cur_pkt_size - 14 -
3064 sizeof(struct ipv6hdr) - sizeof(struct udphdr) -
3065 pkt_dev->pkt_overhead;
3066
3067 if (datalen < 0 || datalen < sizeof(struct pktgen_hdr)) {
3068 datalen = sizeof(struct pktgen_hdr);
3069 net_info_ratelimited("increased datalen to %d\n", datalen);
3070 }
3071
3072 udplen = datalen + sizeof(struct udphdr);
3073 udph->source = htons(pkt_dev->cur_udp_src);
3074 udph->dest = htons(pkt_dev->cur_udp_dst);
3075 udph->len = htons(udplen);
3076 udph->check = 0;
3077
3078 *(__be32 *) iph = htonl(0x60000000); /* Version + flow */
3079
3080 if (pkt_dev->traffic_class) {
3081 /* Version + traffic class + flow (0) */
3082 *(__be32 *)iph |= htonl(0x60000000 | (pkt_dev->traffic_class << 20));
3083 }
3084
3085 iph->hop_limit = 32;
3086
3087 iph->payload_len = htons(udplen);
3088 iph->nexthdr = IPPROTO_UDP;
3089
3090 iph->daddr = pkt_dev->cur_in6_daddr;
3091 iph->saddr = pkt_dev->cur_in6_saddr;
3092
3093 skb->protocol = protocol;
3094 skb->dev = odev;
3095 skb->pkt_type = PACKET_HOST;
3096
3097 pktgen_finalize_skb(pkt_dev, skb, datalen);
3098
3099 if (!(pkt_dev->flags & F_UDPCSUM)) {
3100 skb->ip_summed = CHECKSUM_NONE;
3101 } else if (odev->features & (NETIF_F_HW_CSUM | NETIF_F_IPV6_CSUM)) {
3102 skb->ip_summed = CHECKSUM_PARTIAL;
3103 skb->csum_start = skb_transport_header(skb) - skb->head;
3104 skb->csum_offset = offsetof(struct udphdr, check);
3105 udph->check = ~csum_ipv6_magic(&iph->saddr, &iph->daddr, udplen, IPPROTO_UDP, 0);
3106 } else {
3107 __wsum csum = skb_checksum(skb, skb_transport_offset(skb), udplen, 0);
3108
3109 /* add protocol-dependent pseudo-header */
3110 udph->check = csum_ipv6_magic(&iph->saddr, &iph->daddr, udplen, IPPROTO_UDP, csum);
3111
3112 if (udph->check == 0)
3113 udph->check = CSUM_MANGLED_0;
3114 }
3115
3116 return skb;
3117 }
3118
fill_packet(struct net_device * odev,struct pktgen_dev * pkt_dev)3119 static struct sk_buff *fill_packet(struct net_device *odev,
3120 struct pktgen_dev *pkt_dev)
3121 {
3122 if (pkt_dev->flags & F_IPV6)
3123 return fill_packet_ipv6(odev, pkt_dev);
3124 else
3125 return fill_packet_ipv4(odev, pkt_dev);
3126 }
3127
pktgen_clear_counters(struct pktgen_dev * pkt_dev)3128 static void pktgen_clear_counters(struct pktgen_dev *pkt_dev)
3129 {
3130 pkt_dev->seq_num = 1;
3131 pkt_dev->idle_acc = 0;
3132 pkt_dev->sofar = 0;
3133 pkt_dev->tx_bytes = 0;
3134 pkt_dev->errors = 0;
3135 }
3136
3137 /* Set up structure for sending pkts, clear counters */
3138
pktgen_run(struct pktgen_thread * t)3139 static void pktgen_run(struct pktgen_thread *t)
3140 {
3141 struct pktgen_dev *pkt_dev;
3142 int started = 0;
3143
3144 func_enter();
3145
3146 rcu_read_lock();
3147 list_for_each_entry_rcu(pkt_dev, &t->if_list, list) {
3148
3149 /*
3150 * setup odev and create initial packet.
3151 */
3152 pktgen_setup_inject(pkt_dev);
3153
3154 if (pkt_dev->odev) {
3155 pktgen_clear_counters(pkt_dev);
3156 pkt_dev->skb = NULL;
3157 pkt_dev->started_at = pkt_dev->next_tx = ktime_get();
3158
3159 set_pkt_overhead(pkt_dev);
3160
3161 strcpy(pkt_dev->result, "Starting");
3162 pkt_dev->running = 1; /* Cranke yeself! */
3163 started++;
3164 } else
3165 strcpy(pkt_dev->result, "Error starting");
3166 }
3167 rcu_read_unlock();
3168 if (started)
3169 t->control &= ~(T_STOP);
3170 }
3171
pktgen_handle_all_threads(struct pktgen_net * pn,u32 flags)3172 static void pktgen_handle_all_threads(struct pktgen_net *pn, u32 flags)
3173 {
3174 struct pktgen_thread *t;
3175
3176 mutex_lock(&pktgen_thread_lock);
3177
3178 list_for_each_entry(t, &pn->pktgen_threads, th_list)
3179 t->control |= (flags);
3180
3181 mutex_unlock(&pktgen_thread_lock);
3182 }
3183
pktgen_stop_all_threads(struct pktgen_net * pn)3184 static void pktgen_stop_all_threads(struct pktgen_net *pn)
3185 {
3186 func_enter();
3187
3188 pktgen_handle_all_threads(pn, T_STOP);
3189 }
3190
thread_is_running(const struct pktgen_thread * t)3191 static int thread_is_running(const struct pktgen_thread *t)
3192 {
3193 const struct pktgen_dev *pkt_dev;
3194
3195 rcu_read_lock();
3196 list_for_each_entry_rcu(pkt_dev, &t->if_list, list)
3197 if (pkt_dev->running) {
3198 rcu_read_unlock();
3199 return 1;
3200 }
3201 rcu_read_unlock();
3202 return 0;
3203 }
3204
pktgen_wait_thread_run(struct pktgen_thread * t)3205 static int pktgen_wait_thread_run(struct pktgen_thread *t)
3206 {
3207 while (thread_is_running(t)) {
3208
3209 /* note: 't' will still be around even after the unlock/lock
3210 * cycle because pktgen_thread threads are only cleared at
3211 * net exit
3212 */
3213 mutex_unlock(&pktgen_thread_lock);
3214 msleep_interruptible(100);
3215 mutex_lock(&pktgen_thread_lock);
3216
3217 if (signal_pending(current))
3218 goto signal;
3219 }
3220 return 1;
3221 signal:
3222 return 0;
3223 }
3224
pktgen_wait_all_threads_run(struct pktgen_net * pn)3225 static int pktgen_wait_all_threads_run(struct pktgen_net *pn)
3226 {
3227 struct pktgen_thread *t;
3228 int sig = 1;
3229
3230 /* prevent from racing with rmmod */
3231 if (!try_module_get(THIS_MODULE))
3232 return sig;
3233
3234 mutex_lock(&pktgen_thread_lock);
3235
3236 list_for_each_entry(t, &pn->pktgen_threads, th_list) {
3237 sig = pktgen_wait_thread_run(t);
3238 if (sig == 0)
3239 break;
3240 }
3241
3242 if (sig == 0)
3243 list_for_each_entry(t, &pn->pktgen_threads, th_list)
3244 t->control |= (T_STOP);
3245
3246 mutex_unlock(&pktgen_thread_lock);
3247 module_put(THIS_MODULE);
3248 return sig;
3249 }
3250
pktgen_run_all_threads(struct pktgen_net * pn)3251 static void pktgen_run_all_threads(struct pktgen_net *pn)
3252 {
3253 func_enter();
3254
3255 pktgen_handle_all_threads(pn, T_RUN);
3256
3257 /* Propagate thread->control */
3258 schedule_timeout_interruptible(msecs_to_jiffies(125));
3259
3260 pktgen_wait_all_threads_run(pn);
3261 }
3262
pktgen_reset_all_threads(struct pktgen_net * pn)3263 static void pktgen_reset_all_threads(struct pktgen_net *pn)
3264 {
3265 func_enter();
3266
3267 pktgen_handle_all_threads(pn, T_REMDEVALL);
3268
3269 /* Propagate thread->control */
3270 schedule_timeout_interruptible(msecs_to_jiffies(125));
3271
3272 pktgen_wait_all_threads_run(pn);
3273 }
3274
show_results(struct pktgen_dev * pkt_dev,int nr_frags)3275 static void show_results(struct pktgen_dev *pkt_dev, int nr_frags)
3276 {
3277 __u64 bps, mbps, pps;
3278 char *p = pkt_dev->result;
3279 ktime_t elapsed = ktime_sub(pkt_dev->stopped_at,
3280 pkt_dev->started_at);
3281 ktime_t idle = ns_to_ktime(pkt_dev->idle_acc);
3282
3283 p += sprintf(p, "OK: %llu(c%llu+d%llu) usec, %llu (%dbyte,%dfrags)\n",
3284 (unsigned long long)ktime_to_us(elapsed),
3285 (unsigned long long)ktime_to_us(ktime_sub(elapsed, idle)),
3286 (unsigned long long)ktime_to_us(idle),
3287 (unsigned long long)pkt_dev->sofar,
3288 pkt_dev->cur_pkt_size, nr_frags);
3289
3290 pps = div64_u64(pkt_dev->sofar * NSEC_PER_SEC,
3291 ktime_to_ns(elapsed));
3292
3293 if (pkt_dev->n_imix_entries > 0) {
3294 int i;
3295 struct imix_pkt *entry;
3296
3297 bps = 0;
3298 for (i = 0; i < pkt_dev->n_imix_entries; i++) {
3299 entry = &pkt_dev->imix_entries[i];
3300 bps += entry->size * entry->count_so_far;
3301 }
3302 bps = div64_u64(bps * 8 * NSEC_PER_SEC, ktime_to_ns(elapsed));
3303 } else {
3304 bps = pps * 8 * pkt_dev->cur_pkt_size;
3305 }
3306
3307 mbps = bps;
3308 do_div(mbps, 1000000);
3309 p += sprintf(p, " %llupps %lluMb/sec (%llubps) errors: %llu",
3310 (unsigned long long)pps,
3311 (unsigned long long)mbps,
3312 (unsigned long long)bps,
3313 (unsigned long long)pkt_dev->errors);
3314 }
3315
3316 /* Set stopped-at timer, remove from running list, do counters & statistics */
pktgen_stop_device(struct pktgen_dev * pkt_dev)3317 static int pktgen_stop_device(struct pktgen_dev *pkt_dev)
3318 {
3319 int nr_frags = pkt_dev->skb ? skb_shinfo(pkt_dev->skb)->nr_frags : -1;
3320
3321 if (!pkt_dev->running) {
3322 pr_warn("interface: %s is already stopped\n",
3323 pkt_dev->odevname);
3324 return -EINVAL;
3325 }
3326
3327 pkt_dev->running = 0;
3328 kfree_skb(pkt_dev->skb);
3329 pkt_dev->skb = NULL;
3330 pkt_dev->stopped_at = ktime_get();
3331
3332 show_results(pkt_dev, nr_frags);
3333
3334 return 0;
3335 }
3336
next_to_run(struct pktgen_thread * t)3337 static struct pktgen_dev *next_to_run(struct pktgen_thread *t)
3338 {
3339 struct pktgen_dev *pkt_dev, *best = NULL;
3340
3341 rcu_read_lock();
3342 list_for_each_entry_rcu(pkt_dev, &t->if_list, list) {
3343 if (!pkt_dev->running)
3344 continue;
3345 if (best == NULL)
3346 best = pkt_dev;
3347 else if (ktime_compare(pkt_dev->next_tx, best->next_tx) < 0)
3348 best = pkt_dev;
3349 }
3350 rcu_read_unlock();
3351
3352 return best;
3353 }
3354
pktgen_stop(struct pktgen_thread * t)3355 static void pktgen_stop(struct pktgen_thread *t)
3356 {
3357 struct pktgen_dev *pkt_dev;
3358
3359 func_enter();
3360
3361 rcu_read_lock();
3362
3363 list_for_each_entry_rcu(pkt_dev, &t->if_list, list) {
3364 pktgen_stop_device(pkt_dev);
3365 }
3366
3367 rcu_read_unlock();
3368 }
3369
3370 /*
3371 * one of our devices needs to be removed - find it
3372 * and remove it
3373 */
pktgen_rem_one_if(struct pktgen_thread * t)3374 static void pktgen_rem_one_if(struct pktgen_thread *t)
3375 {
3376 struct list_head *q, *n;
3377 struct pktgen_dev *cur;
3378
3379 func_enter();
3380
3381 list_for_each_safe(q, n, &t->if_list) {
3382 cur = list_entry(q, struct pktgen_dev, list);
3383
3384 if (!cur->removal_mark)
3385 continue;
3386
3387 kfree_skb(cur->skb);
3388 cur->skb = NULL;
3389
3390 pktgen_remove_device(t, cur);
3391
3392 break;
3393 }
3394 }
3395
pktgen_rem_all_ifs(struct pktgen_thread * t)3396 static void pktgen_rem_all_ifs(struct pktgen_thread *t)
3397 {
3398 struct list_head *q, *n;
3399 struct pktgen_dev *cur;
3400
3401 func_enter();
3402
3403 /* Remove all devices, free mem */
3404
3405 list_for_each_safe(q, n, &t->if_list) {
3406 cur = list_entry(q, struct pktgen_dev, list);
3407
3408 kfree_skb(cur->skb);
3409 cur->skb = NULL;
3410
3411 pktgen_remove_device(t, cur);
3412 }
3413 }
3414
pktgen_rem_thread(struct pktgen_thread * t)3415 static void pktgen_rem_thread(struct pktgen_thread *t)
3416 {
3417 /* Remove from the thread list */
3418 remove_proc_entry(t->tsk->comm, t->net->proc_dir);
3419 }
3420
pktgen_resched(struct pktgen_dev * pkt_dev)3421 static void pktgen_resched(struct pktgen_dev *pkt_dev)
3422 {
3423 ktime_t idle_start = ktime_get();
3424 schedule();
3425 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_get(), idle_start));
3426 }
3427
pktgen_wait_for_skb(struct pktgen_dev * pkt_dev)3428 static void pktgen_wait_for_skb(struct pktgen_dev *pkt_dev)
3429 {
3430 ktime_t idle_start = ktime_get();
3431
3432 while (refcount_read(&(pkt_dev->skb->users)) != 1) {
3433 if (signal_pending(current))
3434 break;
3435
3436 if (need_resched())
3437 pktgen_resched(pkt_dev);
3438 else
3439 cpu_relax();
3440 }
3441 pkt_dev->idle_acc += ktime_to_ns(ktime_sub(ktime_get(), idle_start));
3442 }
3443
pktgen_xmit(struct pktgen_dev * pkt_dev)3444 static void pktgen_xmit(struct pktgen_dev *pkt_dev)
3445 {
3446 unsigned int burst = READ_ONCE(pkt_dev->burst);
3447 struct net_device *odev = pkt_dev->odev;
3448 struct netdev_queue *txq;
3449 struct sk_buff *skb;
3450 int ret;
3451
3452 /* If device is offline, then don't send */
3453 if (unlikely(!netif_running(odev) || !netif_carrier_ok(odev))) {
3454 pktgen_stop_device(pkt_dev);
3455 return;
3456 }
3457
3458 /* This is max DELAY, this has special meaning of
3459 * "never transmit"
3460 */
3461 if (unlikely(pkt_dev->delay == ULLONG_MAX)) {
3462 pkt_dev->next_tx = ktime_add_ns(ktime_get(), ULONG_MAX);
3463 return;
3464 }
3465
3466 /* If no skb or clone count exhausted then get new one */
3467 if (!pkt_dev->skb || (pkt_dev->last_ok &&
3468 ++pkt_dev->clone_count >= pkt_dev->clone_skb)) {
3469 /* build a new pkt */
3470 kfree_skb(pkt_dev->skb);
3471
3472 pkt_dev->skb = fill_packet(odev, pkt_dev);
3473 if (pkt_dev->skb == NULL) {
3474 pr_err("ERROR: couldn't allocate skb in fill_packet\n");
3475 schedule();
3476 pkt_dev->clone_count--; /* back out increment, OOM */
3477 return;
3478 }
3479 pkt_dev->last_pkt_size = pkt_dev->skb->len;
3480 pkt_dev->clone_count = 0; /* reset counter */
3481 }
3482
3483 if (pkt_dev->delay && pkt_dev->last_ok)
3484 spin(pkt_dev, pkt_dev->next_tx);
3485
3486 if (pkt_dev->xmit_mode == M_NETIF_RECEIVE) {
3487 skb = pkt_dev->skb;
3488 skb->protocol = eth_type_trans(skb, skb->dev);
3489 refcount_add(burst, &skb->users);
3490 local_bh_disable();
3491 do {
3492 ret = netif_receive_skb(skb);
3493 if (ret == NET_RX_DROP)
3494 pkt_dev->errors++;
3495 pkt_dev->sofar++;
3496 pkt_dev->seq_num++;
3497 if (refcount_read(&skb->users) != burst) {
3498 /* skb was queued by rps/rfs or taps,
3499 * so cannot reuse this skb
3500 */
3501 WARN_ON(refcount_sub_and_test(burst - 1, &skb->users));
3502 /* get out of the loop and wait
3503 * until skb is consumed
3504 */
3505 break;
3506 }
3507 /* skb was 'freed' by stack, so clean few
3508 * bits and reuse it
3509 */
3510 skb_reset_redirect(skb);
3511 } while (--burst > 0);
3512 goto out; /* Skips xmit_mode M_START_XMIT */
3513 } else if (pkt_dev->xmit_mode == M_QUEUE_XMIT) {
3514 local_bh_disable();
3515 refcount_inc(&pkt_dev->skb->users);
3516
3517 ret = dev_queue_xmit(pkt_dev->skb);
3518 switch (ret) {
3519 case NET_XMIT_SUCCESS:
3520 pkt_dev->sofar++;
3521 pkt_dev->seq_num++;
3522 pkt_dev->tx_bytes += pkt_dev->last_pkt_size;
3523 break;
3524 case NET_XMIT_DROP:
3525 case NET_XMIT_CN:
3526 /* These are all valid return codes for a qdisc but
3527 * indicate packets are being dropped or will likely
3528 * be dropped soon.
3529 */
3530 case NETDEV_TX_BUSY:
3531 /* qdisc may call dev_hard_start_xmit directly in cases
3532 * where no queues exist e.g. loopback device, virtual
3533 * devices, etc. In this case we need to handle
3534 * NETDEV_TX_ codes.
3535 */
3536 default:
3537 pkt_dev->errors++;
3538 net_info_ratelimited("%s xmit error: %d\n",
3539 pkt_dev->odevname, ret);
3540 break;
3541 }
3542 goto out;
3543 }
3544
3545 txq = skb_get_tx_queue(odev, pkt_dev->skb);
3546
3547 local_bh_disable();
3548
3549 HARD_TX_LOCK(odev, txq, smp_processor_id());
3550
3551 if (unlikely(netif_xmit_frozen_or_drv_stopped(txq))) {
3552 pkt_dev->last_ok = 0;
3553 goto unlock;
3554 }
3555 refcount_add(burst, &pkt_dev->skb->users);
3556
3557 xmit_more:
3558 ret = netdev_start_xmit(pkt_dev->skb, odev, txq, --burst > 0);
3559
3560 switch (ret) {
3561 case NETDEV_TX_OK:
3562 pkt_dev->last_ok = 1;
3563 pkt_dev->sofar++;
3564 pkt_dev->seq_num++;
3565 pkt_dev->tx_bytes += pkt_dev->last_pkt_size;
3566 if (burst > 0 && !netif_xmit_frozen_or_drv_stopped(txq))
3567 goto xmit_more;
3568 break;
3569 case NET_XMIT_DROP:
3570 case NET_XMIT_CN:
3571 /* skb has been consumed */
3572 pkt_dev->errors++;
3573 break;
3574 default: /* Drivers are not supposed to return other values! */
3575 net_info_ratelimited("%s xmit error: %d\n",
3576 pkt_dev->odevname, ret);
3577 pkt_dev->errors++;
3578 fallthrough;
3579 case NETDEV_TX_BUSY:
3580 /* Retry it next time */
3581 refcount_dec(&(pkt_dev->skb->users));
3582 pkt_dev->last_ok = 0;
3583 }
3584 if (unlikely(burst))
3585 WARN_ON(refcount_sub_and_test(burst, &pkt_dev->skb->users));
3586 unlock:
3587 HARD_TX_UNLOCK(odev, txq);
3588
3589 out:
3590 local_bh_enable();
3591
3592 /* If pkt_dev->count is zero, then run forever */
3593 if ((pkt_dev->count != 0) && (pkt_dev->sofar >= pkt_dev->count)) {
3594 pktgen_wait_for_skb(pkt_dev);
3595
3596 /* Done with this */
3597 pktgen_stop_device(pkt_dev);
3598 }
3599 }
3600
3601 /*
3602 * Main loop of the thread goes here
3603 */
3604
pktgen_thread_worker(void * arg)3605 static int pktgen_thread_worker(void *arg)
3606 {
3607 struct pktgen_thread *t = arg;
3608 struct pktgen_dev *pkt_dev = NULL;
3609 int cpu = t->cpu;
3610
3611 WARN_ON(smp_processor_id() != cpu);
3612
3613 init_waitqueue_head(&t->queue);
3614 complete(&t->start_done);
3615
3616 pr_debug("starting pktgen/%d: pid=%d\n", cpu, task_pid_nr(current));
3617
3618 set_freezable();
3619
3620 while (!kthread_should_stop()) {
3621 pkt_dev = next_to_run(t);
3622
3623 if (unlikely(!pkt_dev && t->control == 0)) {
3624 if (t->net->pktgen_exiting)
3625 break;
3626 wait_event_interruptible_timeout(t->queue,
3627 t->control != 0,
3628 HZ/10);
3629 try_to_freeze();
3630 continue;
3631 }
3632
3633 if (likely(pkt_dev)) {
3634 pktgen_xmit(pkt_dev);
3635
3636 if (need_resched())
3637 pktgen_resched(pkt_dev);
3638 else
3639 cpu_relax();
3640 }
3641
3642 if (t->control & T_STOP) {
3643 pktgen_stop(t);
3644 t->control &= ~(T_STOP);
3645 }
3646
3647 if (t->control & T_RUN) {
3648 pktgen_run(t);
3649 t->control &= ~(T_RUN);
3650 }
3651
3652 if (t->control & T_REMDEVALL) {
3653 pktgen_rem_all_ifs(t);
3654 t->control &= ~(T_REMDEVALL);
3655 }
3656
3657 if (t->control & T_REMDEV) {
3658 pktgen_rem_one_if(t);
3659 t->control &= ~(T_REMDEV);
3660 }
3661
3662 try_to_freeze();
3663 }
3664
3665 pr_debug("%s stopping all device\n", t->tsk->comm);
3666 pktgen_stop(t);
3667
3668 pr_debug("%s removing all device\n", t->tsk->comm);
3669 pktgen_rem_all_ifs(t);
3670
3671 pr_debug("%s removing thread\n", t->tsk->comm);
3672 pktgen_rem_thread(t);
3673
3674 return 0;
3675 }
3676
pktgen_find_dev(struct pktgen_thread * t,const char * ifname,bool exact)3677 static struct pktgen_dev *pktgen_find_dev(struct pktgen_thread *t,
3678 const char *ifname, bool exact)
3679 {
3680 struct pktgen_dev *p, *pkt_dev = NULL;
3681 size_t len = strlen(ifname);
3682
3683 rcu_read_lock();
3684 list_for_each_entry_rcu(p, &t->if_list, list)
3685 if (strncmp(p->odevname, ifname, len) == 0) {
3686 if (p->odevname[len]) {
3687 if (exact || p->odevname[len] != '@')
3688 continue;
3689 }
3690 pkt_dev = p;
3691 break;
3692 }
3693
3694 rcu_read_unlock();
3695 pr_debug("find_dev(%s) returning %p\n", ifname, pkt_dev);
3696 return pkt_dev;
3697 }
3698
3699 /*
3700 * Adds a dev at front of if_list.
3701 */
3702
add_dev_to_thread(struct pktgen_thread * t,struct pktgen_dev * pkt_dev)3703 static int add_dev_to_thread(struct pktgen_thread *t,
3704 struct pktgen_dev *pkt_dev)
3705 {
3706 int rv = 0;
3707
3708 /* This function cannot be called concurrently, as its called
3709 * under pktgen_thread_lock mutex, but it can run from
3710 * userspace on another CPU than the kthread. The if_lock()
3711 * is used here to sync with concurrent instances of
3712 * _rem_dev_from_if_list() invoked via kthread, which is also
3713 * updating the if_list */
3714 if_lock(t);
3715
3716 if (pkt_dev->pg_thread) {
3717 pr_err("ERROR: already assigned to a thread\n");
3718 rv = -EBUSY;
3719 goto out;
3720 }
3721
3722 pkt_dev->running = 0;
3723 pkt_dev->pg_thread = t;
3724 list_add_rcu(&pkt_dev->list, &t->if_list);
3725
3726 out:
3727 if_unlock(t);
3728 return rv;
3729 }
3730
3731 /* Called under thread lock */
3732
pktgen_add_device(struct pktgen_thread * t,const char * ifname)3733 static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
3734 {
3735 struct pktgen_dev *pkt_dev;
3736 int err;
3737 int node = cpu_to_node(t->cpu);
3738
3739 /* We don't allow a device to be on several threads */
3740
3741 pkt_dev = __pktgen_NN_threads(t->net, ifname, FIND);
3742 if (pkt_dev) {
3743 pr_err("ERROR: interface already used\n");
3744 return -EBUSY;
3745 }
3746
3747 pkt_dev = kzalloc_node(sizeof(struct pktgen_dev), GFP_KERNEL, node);
3748 if (!pkt_dev)
3749 return -ENOMEM;
3750
3751 strcpy(pkt_dev->odevname, ifname);
3752 pkt_dev->flows = vzalloc_node(array_size(MAX_CFLOWS,
3753 sizeof(struct flow_state)),
3754 node);
3755 if (pkt_dev->flows == NULL) {
3756 kfree(pkt_dev);
3757 return -ENOMEM;
3758 }
3759
3760 pkt_dev->removal_mark = 0;
3761 pkt_dev->nfrags = 0;
3762 pkt_dev->delay = pg_delay_d;
3763 pkt_dev->count = pg_count_d;
3764 pkt_dev->sofar = 0;
3765 pkt_dev->udp_src_min = 9; /* sink port */
3766 pkt_dev->udp_src_max = 9;
3767 pkt_dev->udp_dst_min = 9;
3768 pkt_dev->udp_dst_max = 9;
3769 pkt_dev->vlan_p = 0;
3770 pkt_dev->vlan_cfi = 0;
3771 pkt_dev->vlan_id = 0xffff;
3772 pkt_dev->svlan_p = 0;
3773 pkt_dev->svlan_cfi = 0;
3774 pkt_dev->svlan_id = 0xffff;
3775 pkt_dev->burst = 1;
3776 pkt_dev->node = NUMA_NO_NODE;
3777
3778 err = pktgen_setup_dev(t->net, pkt_dev, ifname);
3779 if (err)
3780 goto out1;
3781 if (pkt_dev->odev->priv_flags & IFF_TX_SKB_SHARING)
3782 pkt_dev->clone_skb = pg_clone_skb_d;
3783
3784 pkt_dev->entry = proc_create_data(ifname, 0600, t->net->proc_dir,
3785 &pktgen_if_proc_ops, pkt_dev);
3786 if (!pkt_dev->entry) {
3787 pr_err("cannot create %s/%s procfs entry\n",
3788 PG_PROC_DIR, ifname);
3789 err = -EINVAL;
3790 goto out2;
3791 }
3792 #ifdef CONFIG_XFRM
3793 pkt_dev->ipsmode = XFRM_MODE_TRANSPORT;
3794 pkt_dev->ipsproto = IPPROTO_ESP;
3795
3796 /* xfrm tunnel mode needs additional dst to extract outter
3797 * ip header protocol/ttl/id field, here creat a phony one.
3798 * instead of looking for a valid rt, which definitely hurting
3799 * performance under such circumstance.
3800 */
3801 pkt_dev->dstops.family = AF_INET;
3802 pkt_dev->xdst.u.dst.dev = pkt_dev->odev;
3803 dst_init_metrics(&pkt_dev->xdst.u.dst, pktgen_dst_metrics, false);
3804 pkt_dev->xdst.child = &pkt_dev->xdst.u.dst;
3805 pkt_dev->xdst.u.dst.ops = &pkt_dev->dstops;
3806 #endif
3807
3808 return add_dev_to_thread(t, pkt_dev);
3809 out2:
3810 netdev_put(pkt_dev->odev, &pkt_dev->dev_tracker);
3811 out1:
3812 #ifdef CONFIG_XFRM
3813 free_SAs(pkt_dev);
3814 #endif
3815 vfree(pkt_dev->flows);
3816 kfree(pkt_dev);
3817 return err;
3818 }
3819
pktgen_create_thread(int cpu,struct pktgen_net * pn)3820 static int __net_init pktgen_create_thread(int cpu, struct pktgen_net *pn)
3821 {
3822 struct pktgen_thread *t;
3823 struct proc_dir_entry *pe;
3824 struct task_struct *p;
3825
3826 t = kzalloc_node(sizeof(struct pktgen_thread), GFP_KERNEL,
3827 cpu_to_node(cpu));
3828 if (!t) {
3829 pr_err("ERROR: out of memory, can't create new thread\n");
3830 return -ENOMEM;
3831 }
3832
3833 mutex_init(&t->if_lock);
3834 t->cpu = cpu;
3835
3836 INIT_LIST_HEAD(&t->if_list);
3837
3838 list_add_tail(&t->th_list, &pn->pktgen_threads);
3839 init_completion(&t->start_done);
3840
3841 p = kthread_create_on_node(pktgen_thread_worker,
3842 t,
3843 cpu_to_node(cpu),
3844 "kpktgend_%d", cpu);
3845 if (IS_ERR(p)) {
3846 pr_err("kthread_create_on_node() failed for cpu %d\n", t->cpu);
3847 list_del(&t->th_list);
3848 kfree(t);
3849 return PTR_ERR(p);
3850 }
3851 kthread_bind(p, cpu);
3852 t->tsk = p;
3853
3854 pe = proc_create_data(t->tsk->comm, 0600, pn->proc_dir,
3855 &pktgen_thread_proc_ops, t);
3856 if (!pe) {
3857 pr_err("cannot create %s/%s procfs entry\n",
3858 PG_PROC_DIR, t->tsk->comm);
3859 kthread_stop(p);
3860 list_del(&t->th_list);
3861 kfree(t);
3862 return -EINVAL;
3863 }
3864
3865 t->net = pn;
3866 get_task_struct(p);
3867 wake_up_process(p);
3868 wait_for_completion(&t->start_done);
3869
3870 return 0;
3871 }
3872
3873 /*
3874 * Removes a device from the thread if_list.
3875 */
_rem_dev_from_if_list(struct pktgen_thread * t,struct pktgen_dev * pkt_dev)3876 static void _rem_dev_from_if_list(struct pktgen_thread *t,
3877 struct pktgen_dev *pkt_dev)
3878 {
3879 struct list_head *q, *n;
3880 struct pktgen_dev *p;
3881
3882 if_lock(t);
3883 list_for_each_safe(q, n, &t->if_list) {
3884 p = list_entry(q, struct pktgen_dev, list);
3885 if (p == pkt_dev)
3886 list_del_rcu(&p->list);
3887 }
3888 if_unlock(t);
3889 }
3890
pktgen_remove_device(struct pktgen_thread * t,struct pktgen_dev * pkt_dev)3891 static int pktgen_remove_device(struct pktgen_thread *t,
3892 struct pktgen_dev *pkt_dev)
3893 {
3894 pr_debug("remove_device pkt_dev=%p\n", pkt_dev);
3895
3896 if (pkt_dev->running) {
3897 pr_warn("WARNING: trying to remove a running interface, stopping it now\n");
3898 pktgen_stop_device(pkt_dev);
3899 }
3900
3901 /* Dis-associate from the interface */
3902
3903 if (pkt_dev->odev) {
3904 netdev_put(pkt_dev->odev, &pkt_dev->dev_tracker);
3905 pkt_dev->odev = NULL;
3906 }
3907
3908 /* Remove proc before if_list entry, because add_device uses
3909 * list to determine if interface already exist, avoid race
3910 * with proc_create_data() */
3911 proc_remove(pkt_dev->entry);
3912
3913 /* And update the thread if_list */
3914 _rem_dev_from_if_list(t, pkt_dev);
3915
3916 #ifdef CONFIG_XFRM
3917 free_SAs(pkt_dev);
3918 #endif
3919 vfree(pkt_dev->flows);
3920 if (pkt_dev->page)
3921 put_page(pkt_dev->page);
3922 kfree_rcu(pkt_dev, rcu);
3923 return 0;
3924 }
3925
pg_net_init(struct net * net)3926 static int __net_init pg_net_init(struct net *net)
3927 {
3928 struct pktgen_net *pn = net_generic(net, pg_net_id);
3929 struct proc_dir_entry *pe;
3930 int cpu, ret = 0;
3931
3932 pn->net = net;
3933 INIT_LIST_HEAD(&pn->pktgen_threads);
3934 pn->pktgen_exiting = false;
3935 pn->proc_dir = proc_mkdir(PG_PROC_DIR, pn->net->proc_net);
3936 if (!pn->proc_dir) {
3937 pr_warn("cannot create /proc/net/%s\n", PG_PROC_DIR);
3938 return -ENODEV;
3939 }
3940 pe = proc_create(PGCTRL, 0600, pn->proc_dir, &pktgen_proc_ops);
3941 if (pe == NULL) {
3942 pr_err("cannot create %s procfs entry\n", PGCTRL);
3943 ret = -EINVAL;
3944 goto remove;
3945 }
3946
3947 for_each_online_cpu(cpu) {
3948 int err;
3949
3950 err = pktgen_create_thread(cpu, pn);
3951 if (err)
3952 pr_warn("Cannot create thread for cpu %d (%d)\n",
3953 cpu, err);
3954 }
3955
3956 if (list_empty(&pn->pktgen_threads)) {
3957 pr_err("Initialization failed for all threads\n");
3958 ret = -ENODEV;
3959 goto remove_entry;
3960 }
3961
3962 return 0;
3963
3964 remove_entry:
3965 remove_proc_entry(PGCTRL, pn->proc_dir);
3966 remove:
3967 remove_proc_entry(PG_PROC_DIR, pn->net->proc_net);
3968 return ret;
3969 }
3970
pg_net_exit(struct net * net)3971 static void __net_exit pg_net_exit(struct net *net)
3972 {
3973 struct pktgen_net *pn = net_generic(net, pg_net_id);
3974 struct pktgen_thread *t;
3975 struct list_head *q, *n;
3976 LIST_HEAD(list);
3977
3978 /* Stop all interfaces & threads */
3979 pn->pktgen_exiting = true;
3980
3981 mutex_lock(&pktgen_thread_lock);
3982 list_splice_init(&pn->pktgen_threads, &list);
3983 mutex_unlock(&pktgen_thread_lock);
3984
3985 list_for_each_safe(q, n, &list) {
3986 t = list_entry(q, struct pktgen_thread, th_list);
3987 list_del(&t->th_list);
3988 kthread_stop_put(t->tsk);
3989 kfree(t);
3990 }
3991
3992 remove_proc_entry(PGCTRL, pn->proc_dir);
3993 remove_proc_entry(PG_PROC_DIR, pn->net->proc_net);
3994 }
3995
3996 static struct pernet_operations pg_net_ops = {
3997 .init = pg_net_init,
3998 .exit = pg_net_exit,
3999 .id = &pg_net_id,
4000 .size = sizeof(struct pktgen_net),
4001 };
4002
pg_init(void)4003 static int __init pg_init(void)
4004 {
4005 int ret = 0;
4006
4007 pr_info("%s", version);
4008 ret = register_pernet_subsys(&pg_net_ops);
4009 if (ret)
4010 return ret;
4011 ret = register_netdevice_notifier(&pktgen_notifier_block);
4012 if (ret)
4013 unregister_pernet_subsys(&pg_net_ops);
4014
4015 return ret;
4016 }
4017
pg_cleanup(void)4018 static void __exit pg_cleanup(void)
4019 {
4020 unregister_netdevice_notifier(&pktgen_notifier_block);
4021 unregister_pernet_subsys(&pg_net_ops);
4022 /* Don't need rcu_barrier() due to use of kfree_rcu() */
4023 }
4024
4025 module_init(pg_init);
4026 module_exit(pg_cleanup);
4027
4028 MODULE_AUTHOR("Robert Olsson <robert.olsson@its.uu.se>");
4029 MODULE_DESCRIPTION("Packet Generator tool");
4030 MODULE_LICENSE("GPL");
4031 MODULE_VERSION(VERSION);
4032 module_param(pg_count_d, int, 0);
4033 MODULE_PARM_DESC(pg_count_d, "Default number of packets to inject");
4034 module_param(pg_delay_d, int, 0);
4035 MODULE_PARM_DESC(pg_delay_d, "Default delay between packets (nanoseconds)");
4036 module_param(pg_clone_skb_d, int, 0);
4037 MODULE_PARM_DESC(pg_clone_skb_d, "Default number of copies of the same packet");
4038 module_param(debug, int, 0);
4039 MODULE_PARM_DESC(debug, "Enable debugging of pktgen module");
4040