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