xref: /dpdk/app/test-pmd/config.c (revision 4b53e9802b6b6040ad5622b1414aaa93d9581d0c)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2016 Intel Corporation.
3  * Copyright 2013-2014 6WIND S.A.
4  */
5 
6 #include <ctype.h>
7 #include <stdarg.h>
8 #include <errno.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <stdint.h>
13 #include <inttypes.h>
14 
15 #include <sys/queue.h>
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 #include <fcntl.h>
19 #include <unistd.h>
20 
21 #include <rte_common.h>
22 #include <rte_byteorder.h>
23 #include <rte_debug.h>
24 #include <rte_log.h>
25 #include <rte_memory.h>
26 #include <rte_memcpy.h>
27 #include <rte_memzone.h>
28 #include <rte_launch.h>
29 #include <rte_bus.h>
30 #include <rte_eal.h>
31 #include <rte_per_lcore.h>
32 #include <rte_lcore.h>
33 #include <rte_branch_prediction.h>
34 #include <rte_mempool.h>
35 #include <rte_mbuf.h>
36 #include <rte_interrupts.h>
37 #include <rte_ether.h>
38 #include <rte_ethdev.h>
39 #include <rte_string_fns.h>
40 #include <rte_cycles.h>
41 #include <rte_flow.h>
42 #include <rte_mtr.h>
43 #include <rte_errno.h>
44 #ifdef RTE_NET_IXGBE
45 #include <rte_pmd_ixgbe.h>
46 #endif
47 #ifdef RTE_NET_I40E
48 #include <rte_pmd_i40e.h>
49 #endif
50 #ifdef RTE_NET_BNXT
51 #include <rte_pmd_bnxt.h>
52 #endif
53 #ifdef RTE_LIB_GRO
54 #include <rte_gro.h>
55 #endif
56 #include <rte_hexdump.h>
57 
58 #include "testpmd.h"
59 #include "cmdline_mtr.h"
60 
61 #define ETHDEV_FWVERS_LEN 32
62 
63 #ifdef CLOCK_MONOTONIC_RAW /* Defined in glibc bits/time.h */
64 #define CLOCK_TYPE_ID CLOCK_MONOTONIC_RAW
65 #else
66 #define CLOCK_TYPE_ID CLOCK_MONOTONIC
67 #endif
68 
69 #define NS_PER_SEC 1E9
70 
71 static const struct {
72 	enum tx_pkt_split split;
73 	const char *name;
74 } tx_split_name[] = {
75 	{
76 		.split = TX_PKT_SPLIT_OFF,
77 		.name = "off",
78 	},
79 	{
80 		.split = TX_PKT_SPLIT_ON,
81 		.name = "on",
82 	},
83 	{
84 		.split = TX_PKT_SPLIT_RND,
85 		.name = "rand",
86 	},
87 };
88 
89 const struct rss_type_info rss_type_table[] = {
90 	/* Group types */
91 	{ "all", RTE_ETH_RSS_ETH | RTE_ETH_RSS_VLAN | RTE_ETH_RSS_IP | RTE_ETH_RSS_TCP |
92 		RTE_ETH_RSS_UDP | RTE_ETH_RSS_SCTP | RTE_ETH_RSS_L2_PAYLOAD |
93 		RTE_ETH_RSS_L2TPV3 | RTE_ETH_RSS_ESP | RTE_ETH_RSS_AH | RTE_ETH_RSS_PFCP |
94 		RTE_ETH_RSS_GTPU | RTE_ETH_RSS_ECPRI | RTE_ETH_RSS_MPLS | RTE_ETH_RSS_L2TPV2},
95 	{ "none", 0 },
96 	{ "ip", RTE_ETH_RSS_IP },
97 	{ "udp", RTE_ETH_RSS_UDP },
98 	{ "tcp", RTE_ETH_RSS_TCP },
99 	{ "sctp", RTE_ETH_RSS_SCTP },
100 	{ "tunnel", RTE_ETH_RSS_TUNNEL },
101 	{ "vlan", RTE_ETH_RSS_VLAN },
102 
103 	/* Individual type */
104 	{ "ipv4", RTE_ETH_RSS_IPV4 },
105 	{ "ipv4-frag", RTE_ETH_RSS_FRAG_IPV4 },
106 	{ "ipv4-tcp", RTE_ETH_RSS_NONFRAG_IPV4_TCP },
107 	{ "ipv4-udp", RTE_ETH_RSS_NONFRAG_IPV4_UDP },
108 	{ "ipv4-sctp", RTE_ETH_RSS_NONFRAG_IPV4_SCTP },
109 	{ "ipv4-other", RTE_ETH_RSS_NONFRAG_IPV4_OTHER },
110 	{ "ipv6", RTE_ETH_RSS_IPV6 },
111 	{ "ipv6-frag", RTE_ETH_RSS_FRAG_IPV6 },
112 	{ "ipv6-tcp", RTE_ETH_RSS_NONFRAG_IPV6_TCP },
113 	{ "ipv6-udp", RTE_ETH_RSS_NONFRAG_IPV6_UDP },
114 	{ "ipv6-sctp", RTE_ETH_RSS_NONFRAG_IPV6_SCTP },
115 	{ "ipv6-other", RTE_ETH_RSS_NONFRAG_IPV6_OTHER },
116 	{ "l2-payload", RTE_ETH_RSS_L2_PAYLOAD },
117 	{ "ipv6-ex", RTE_ETH_RSS_IPV6_EX },
118 	{ "ipv6-tcp-ex", RTE_ETH_RSS_IPV6_TCP_EX },
119 	{ "ipv6-udp-ex", RTE_ETH_RSS_IPV6_UDP_EX },
120 	{ "port", RTE_ETH_RSS_PORT },
121 	{ "vxlan", RTE_ETH_RSS_VXLAN },
122 	{ "geneve", RTE_ETH_RSS_GENEVE },
123 	{ "nvgre", RTE_ETH_RSS_NVGRE },
124 	{ "gtpu", RTE_ETH_RSS_GTPU },
125 	{ "eth", RTE_ETH_RSS_ETH },
126 	{ "s-vlan", RTE_ETH_RSS_S_VLAN },
127 	{ "c-vlan", RTE_ETH_RSS_C_VLAN },
128 	{ "esp", RTE_ETH_RSS_ESP },
129 	{ "ah", RTE_ETH_RSS_AH },
130 	{ "l2tpv3", RTE_ETH_RSS_L2TPV3 },
131 	{ "pfcp", RTE_ETH_RSS_PFCP },
132 	{ "pppoe", RTE_ETH_RSS_PPPOE },
133 	{ "ecpri", RTE_ETH_RSS_ECPRI },
134 	{ "mpls", RTE_ETH_RSS_MPLS },
135 	{ "ipv4-chksum", RTE_ETH_RSS_IPV4_CHKSUM },
136 	{ "l4-chksum", RTE_ETH_RSS_L4_CHKSUM },
137 	{ "l2tpv2", RTE_ETH_RSS_L2TPV2 },
138 	{ "l3-pre96", RTE_ETH_RSS_L3_PRE96 },
139 	{ "l3-pre64", RTE_ETH_RSS_L3_PRE64 },
140 	{ "l3-pre56", RTE_ETH_RSS_L3_PRE56 },
141 	{ "l3-pre48", RTE_ETH_RSS_L3_PRE48 },
142 	{ "l3-pre40", RTE_ETH_RSS_L3_PRE40 },
143 	{ "l3-pre32", RTE_ETH_RSS_L3_PRE32 },
144 	{ "l2-dst-only", RTE_ETH_RSS_L2_DST_ONLY },
145 	{ "l2-src-only", RTE_ETH_RSS_L2_SRC_ONLY },
146 	{ "l4-dst-only", RTE_ETH_RSS_L4_DST_ONLY },
147 	{ "l4-src-only", RTE_ETH_RSS_L4_SRC_ONLY },
148 	{ "l3-dst-only", RTE_ETH_RSS_L3_DST_ONLY },
149 	{ "l3-src-only", RTE_ETH_RSS_L3_SRC_ONLY },
150 	{ NULL, 0},
151 };
152 
153 static const struct {
154 	enum rte_eth_fec_mode mode;
155 	const char *name;
156 } fec_mode_name[] = {
157 	{
158 		.mode = RTE_ETH_FEC_NOFEC,
159 		.name = "off",
160 	},
161 	{
162 		.mode = RTE_ETH_FEC_AUTO,
163 		.name = "auto",
164 	},
165 	{
166 		.mode = RTE_ETH_FEC_BASER,
167 		.name = "baser",
168 	},
169 	{
170 		.mode = RTE_ETH_FEC_RS,
171 		.name = "rs",
172 	},
173 };
174 
175 static const struct {
176 	char str[32];
177 	uint16_t ftype;
178 } flowtype_str_table[] = {
179 	{"raw", RTE_ETH_FLOW_RAW},
180 	{"ipv4", RTE_ETH_FLOW_IPV4},
181 	{"ipv4-frag", RTE_ETH_FLOW_FRAG_IPV4},
182 	{"ipv4-tcp", RTE_ETH_FLOW_NONFRAG_IPV4_TCP},
183 	{"ipv4-udp", RTE_ETH_FLOW_NONFRAG_IPV4_UDP},
184 	{"ipv4-sctp", RTE_ETH_FLOW_NONFRAG_IPV4_SCTP},
185 	{"ipv4-other", RTE_ETH_FLOW_NONFRAG_IPV4_OTHER},
186 	{"ipv6", RTE_ETH_FLOW_IPV6},
187 	{"ipv6-frag", RTE_ETH_FLOW_FRAG_IPV6},
188 	{"ipv6-tcp", RTE_ETH_FLOW_NONFRAG_IPV6_TCP},
189 	{"ipv6-udp", RTE_ETH_FLOW_NONFRAG_IPV6_UDP},
190 	{"ipv6-sctp", RTE_ETH_FLOW_NONFRAG_IPV6_SCTP},
191 	{"ipv6-other", RTE_ETH_FLOW_NONFRAG_IPV6_OTHER},
192 	{"l2_payload", RTE_ETH_FLOW_L2_PAYLOAD},
193 	{"ipv6-ex", RTE_ETH_FLOW_IPV6_EX},
194 	{"ipv6-tcp-ex", RTE_ETH_FLOW_IPV6_TCP_EX},
195 	{"ipv6-udp-ex", RTE_ETH_FLOW_IPV6_UDP_EX},
196 	{"port", RTE_ETH_FLOW_PORT},
197 	{"vxlan", RTE_ETH_FLOW_VXLAN},
198 	{"geneve", RTE_ETH_FLOW_GENEVE},
199 	{"nvgre", RTE_ETH_FLOW_NVGRE},
200 	{"vxlan-gpe", RTE_ETH_FLOW_VXLAN_GPE},
201 	{"gtpu", RTE_ETH_FLOW_GTPU},
202 };
203 
204 static void
205 print_ethaddr(const char *name, struct rte_ether_addr *eth_addr)
206 {
207 	char buf[RTE_ETHER_ADDR_FMT_SIZE];
208 	rte_ether_format_addr(buf, RTE_ETHER_ADDR_FMT_SIZE, eth_addr);
209 	printf("%s%s", name, buf);
210 }
211 
212 static void
213 nic_xstats_display_periodic(portid_t port_id)
214 {
215 	struct xstat_display_info *xstats_info;
216 	uint64_t *prev_values, *curr_values;
217 	uint64_t diff_value, value_rate;
218 	struct timespec cur_time;
219 	uint64_t *ids_supp;
220 	size_t ids_supp_sz;
221 	uint64_t diff_ns;
222 	unsigned int i;
223 	int rc;
224 
225 	xstats_info = &ports[port_id].xstats_info;
226 
227 	ids_supp_sz = xstats_info->ids_supp_sz;
228 	if (ids_supp_sz == 0)
229 		return;
230 
231 	printf("\n");
232 
233 	ids_supp = xstats_info->ids_supp;
234 	prev_values = xstats_info->prev_values;
235 	curr_values = xstats_info->curr_values;
236 
237 	rc = rte_eth_xstats_get_by_id(port_id, ids_supp, curr_values,
238 				      ids_supp_sz);
239 	if (rc != (int)ids_supp_sz) {
240 		fprintf(stderr,
241 			"Failed to get values of %zu xstats for port %u - return code %d\n",
242 			ids_supp_sz, port_id, rc);
243 		return;
244 	}
245 
246 	diff_ns = 0;
247 	if (clock_gettime(CLOCK_TYPE_ID, &cur_time) == 0) {
248 		uint64_t ns;
249 
250 		ns = cur_time.tv_sec * NS_PER_SEC;
251 		ns += cur_time.tv_nsec;
252 
253 		if (xstats_info->prev_ns != 0)
254 			diff_ns = ns - xstats_info->prev_ns;
255 		xstats_info->prev_ns = ns;
256 	}
257 
258 	printf("%-31s%-17s%s\n", " ", "Value", "Rate (since last show)");
259 	for (i = 0; i < ids_supp_sz; i++) {
260 		diff_value = (curr_values[i] > prev_values[i]) ?
261 			     (curr_values[i] - prev_values[i]) : 0;
262 		prev_values[i] = curr_values[i];
263 		value_rate = diff_ns > 0 ?
264 				(double)diff_value / diff_ns * NS_PER_SEC : 0;
265 
266 		printf("  %-25s%12"PRIu64" %15"PRIu64"\n",
267 		       xstats_display[i].name, curr_values[i], value_rate);
268 	}
269 }
270 
271 void
272 nic_stats_display(portid_t port_id)
273 {
274 	static uint64_t prev_pkts_rx[RTE_MAX_ETHPORTS];
275 	static uint64_t prev_pkts_tx[RTE_MAX_ETHPORTS];
276 	static uint64_t prev_bytes_rx[RTE_MAX_ETHPORTS];
277 	static uint64_t prev_bytes_tx[RTE_MAX_ETHPORTS];
278 	static uint64_t prev_ns[RTE_MAX_ETHPORTS];
279 	struct timespec cur_time;
280 	uint64_t diff_pkts_rx, diff_pkts_tx, diff_bytes_rx, diff_bytes_tx,
281 								diff_ns;
282 	uint64_t mpps_rx, mpps_tx, mbps_rx, mbps_tx;
283 	struct rte_eth_stats stats;
284 	static const char *nic_stats_border = "########################";
285 	int ret;
286 
287 	if (port_id_is_invalid(port_id, ENABLED_WARN)) {
288 		print_valid_ports();
289 		return;
290 	}
291 	ret = rte_eth_stats_get(port_id, &stats);
292 	if (ret != 0) {
293 		fprintf(stderr,
294 			"%s: Error: failed to get stats (port %u): %d",
295 			__func__, port_id, ret);
296 		return;
297 	}
298 	printf("\n  %s NIC statistics for port %-2d %s\n",
299 	       nic_stats_border, port_id, nic_stats_border);
300 
301 	printf("  RX-packets: %-10"PRIu64" RX-missed: %-10"PRIu64" RX-bytes:  "
302 	       "%-"PRIu64"\n", stats.ipackets, stats.imissed, stats.ibytes);
303 	printf("  RX-errors: %-"PRIu64"\n", stats.ierrors);
304 	printf("  RX-nombuf:  %-10"PRIu64"\n", stats.rx_nombuf);
305 	printf("  TX-packets: %-10"PRIu64" TX-errors: %-10"PRIu64" TX-bytes:  "
306 	       "%-"PRIu64"\n", stats.opackets, stats.oerrors, stats.obytes);
307 
308 	diff_ns = 0;
309 	if (clock_gettime(CLOCK_TYPE_ID, &cur_time) == 0) {
310 		uint64_t ns;
311 
312 		ns = cur_time.tv_sec * NS_PER_SEC;
313 		ns += cur_time.tv_nsec;
314 
315 		if (prev_ns[port_id] != 0)
316 			diff_ns = ns - prev_ns[port_id];
317 		prev_ns[port_id] = ns;
318 	}
319 
320 	diff_pkts_rx = (stats.ipackets > prev_pkts_rx[port_id]) ?
321 		(stats.ipackets - prev_pkts_rx[port_id]) : 0;
322 	diff_pkts_tx = (stats.opackets > prev_pkts_tx[port_id]) ?
323 		(stats.opackets - prev_pkts_tx[port_id]) : 0;
324 	prev_pkts_rx[port_id] = stats.ipackets;
325 	prev_pkts_tx[port_id] = stats.opackets;
326 	mpps_rx = diff_ns > 0 ?
327 		(double)diff_pkts_rx / diff_ns * NS_PER_SEC : 0;
328 	mpps_tx = diff_ns > 0 ?
329 		(double)diff_pkts_tx / diff_ns * NS_PER_SEC : 0;
330 
331 	diff_bytes_rx = (stats.ibytes > prev_bytes_rx[port_id]) ?
332 		(stats.ibytes - prev_bytes_rx[port_id]) : 0;
333 	diff_bytes_tx = (stats.obytes > prev_bytes_tx[port_id]) ?
334 		(stats.obytes - prev_bytes_tx[port_id]) : 0;
335 	prev_bytes_rx[port_id] = stats.ibytes;
336 	prev_bytes_tx[port_id] = stats.obytes;
337 	mbps_rx = diff_ns > 0 ?
338 		(double)diff_bytes_rx / diff_ns * NS_PER_SEC : 0;
339 	mbps_tx = diff_ns > 0 ?
340 		(double)diff_bytes_tx / diff_ns * NS_PER_SEC : 0;
341 
342 	printf("\n  Throughput (since last show)\n");
343 	printf("  Rx-pps: %12"PRIu64"          Rx-bps: %12"PRIu64"\n  Tx-pps: %12"
344 	       PRIu64"          Tx-bps: %12"PRIu64"\n", mpps_rx, mbps_rx * 8,
345 	       mpps_tx, mbps_tx * 8);
346 
347 	if (xstats_display_num > 0)
348 		nic_xstats_display_periodic(port_id);
349 
350 	printf("  %s############################%s\n",
351 	       nic_stats_border, nic_stats_border);
352 }
353 
354 void
355 nic_stats_clear(portid_t port_id)
356 {
357 	int ret;
358 
359 	if (port_id_is_invalid(port_id, ENABLED_WARN)) {
360 		print_valid_ports();
361 		return;
362 	}
363 
364 	ret = rte_eth_stats_reset(port_id);
365 	if (ret != 0) {
366 		fprintf(stderr,
367 			"%s: Error: failed to reset stats (port %u): %s",
368 			__func__, port_id, strerror(-ret));
369 		return;
370 	}
371 
372 	ret = rte_eth_stats_get(port_id, &ports[port_id].stats);
373 	if (ret != 0) {
374 		if (ret < 0)
375 			ret = -ret;
376 		fprintf(stderr,
377 			"%s: Error: failed to get stats (port %u): %s",
378 			__func__, port_id, strerror(ret));
379 		return;
380 	}
381 	printf("\n  NIC statistics for port %d cleared\n", port_id);
382 }
383 
384 void
385 nic_xstats_display(portid_t port_id)
386 {
387 	struct rte_eth_xstat *xstats;
388 	int cnt_xstats, idx_xstat;
389 	struct rte_eth_xstat_name *xstats_names;
390 
391 	if (port_id_is_invalid(port_id, ENABLED_WARN)) {
392 		print_valid_ports();
393 		return;
394 	}
395 	printf("###### NIC extended statistics for port %-2d\n", port_id);
396 	if (!rte_eth_dev_is_valid_port(port_id)) {
397 		fprintf(stderr, "Error: Invalid port number %i\n", port_id);
398 		return;
399 	}
400 
401 	/* Get count */
402 	cnt_xstats = rte_eth_xstats_get_names(port_id, NULL, 0);
403 	if (cnt_xstats  < 0) {
404 		fprintf(stderr, "Error: Cannot get count of xstats\n");
405 		return;
406 	}
407 
408 	/* Get id-name lookup table */
409 	xstats_names = malloc(sizeof(struct rte_eth_xstat_name) * cnt_xstats);
410 	if (xstats_names == NULL) {
411 		fprintf(stderr, "Cannot allocate memory for xstats lookup\n");
412 		return;
413 	}
414 	if (cnt_xstats != rte_eth_xstats_get_names(
415 			port_id, xstats_names, cnt_xstats)) {
416 		fprintf(stderr, "Error: Cannot get xstats lookup\n");
417 		free(xstats_names);
418 		return;
419 	}
420 
421 	/* Get stats themselves */
422 	xstats = malloc(sizeof(struct rte_eth_xstat) * cnt_xstats);
423 	if (xstats == NULL) {
424 		fprintf(stderr, "Cannot allocate memory for xstats\n");
425 		free(xstats_names);
426 		return;
427 	}
428 	if (cnt_xstats != rte_eth_xstats_get(port_id, xstats, cnt_xstats)) {
429 		fprintf(stderr, "Error: Unable to get xstats\n");
430 		free(xstats_names);
431 		free(xstats);
432 		return;
433 	}
434 
435 	/* Display xstats */
436 	for (idx_xstat = 0; idx_xstat < cnt_xstats; idx_xstat++) {
437 		if (xstats_hide_zero && !xstats[idx_xstat].value)
438 			continue;
439 		printf("%s: %"PRIu64"\n",
440 			xstats_names[idx_xstat].name,
441 			xstats[idx_xstat].value);
442 	}
443 	free(xstats_names);
444 	free(xstats);
445 }
446 
447 void
448 nic_xstats_clear(portid_t port_id)
449 {
450 	int ret;
451 
452 	if (port_id_is_invalid(port_id, ENABLED_WARN)) {
453 		print_valid_ports();
454 		return;
455 	}
456 
457 	ret = rte_eth_xstats_reset(port_id);
458 	if (ret != 0) {
459 		fprintf(stderr,
460 			"%s: Error: failed to reset xstats (port %u): %s\n",
461 			__func__, port_id, strerror(-ret));
462 		return;
463 	}
464 
465 	ret = rte_eth_stats_get(port_id, &ports[port_id].stats);
466 	if (ret != 0) {
467 		if (ret < 0)
468 			ret = -ret;
469 		fprintf(stderr, "%s: Error: failed to get stats (port %u): %s",
470 			__func__, port_id, strerror(ret));
471 		return;
472 	}
473 }
474 
475 static const char *
476 get_queue_state_name(uint8_t queue_state)
477 {
478 	if (queue_state == RTE_ETH_QUEUE_STATE_STOPPED)
479 		return "stopped";
480 	else if (queue_state == RTE_ETH_QUEUE_STATE_STARTED)
481 		return "started";
482 	else if (queue_state == RTE_ETH_QUEUE_STATE_HAIRPIN)
483 		return "hairpin";
484 	else
485 		return "unknown";
486 }
487 
488 void
489 rx_queue_infos_display(portid_t port_id, uint16_t queue_id)
490 {
491 	struct rte_eth_burst_mode mode;
492 	struct rte_eth_rxq_info qinfo;
493 	int32_t rc;
494 	static const char *info_border = "*********************";
495 
496 	rc = rte_eth_rx_queue_info_get(port_id, queue_id, &qinfo);
497 	if (rc != 0) {
498 		fprintf(stderr,
499 			"Failed to retrieve information for port: %u, RX queue: %hu\nerror desc: %s(%d)\n",
500 			port_id, queue_id, strerror(-rc), rc);
501 		return;
502 	}
503 
504 	printf("\n%s Infos for port %-2u, RX queue %-2u %s",
505 	       info_border, port_id, queue_id, info_border);
506 
507 	printf("\nMempool: %s", (qinfo.mp == NULL) ? "NULL" : qinfo.mp->name);
508 	printf("\nRX prefetch threshold: %hhu", qinfo.conf.rx_thresh.pthresh);
509 	printf("\nRX host threshold: %hhu", qinfo.conf.rx_thresh.hthresh);
510 	printf("\nRX writeback threshold: %hhu", qinfo.conf.rx_thresh.wthresh);
511 	printf("\nRX free threshold: %hu", qinfo.conf.rx_free_thresh);
512 	printf("\nRX drop packets: %s",
513 		(qinfo.conf.rx_drop_en != 0) ? "on" : "off");
514 	printf("\nRX deferred start: %s",
515 		(qinfo.conf.rx_deferred_start != 0) ? "on" : "off");
516 	printf("\nRX scattered packets: %s",
517 		(qinfo.scattered_rx != 0) ? "on" : "off");
518 	printf("\nRx queue state: %s", get_queue_state_name(qinfo.queue_state));
519 	if (qinfo.rx_buf_size != 0)
520 		printf("\nRX buffer size: %hu", qinfo.rx_buf_size);
521 	printf("\nNumber of RXDs: %hu", qinfo.nb_desc);
522 
523 	if (rte_eth_rx_burst_mode_get(port_id, queue_id, &mode) == 0)
524 		printf("\nBurst mode: %s%s",
525 		       mode.info,
526 		       mode.flags & RTE_ETH_BURST_FLAG_PER_QUEUE ?
527 				" (per queue)" : "");
528 
529 	printf("\n");
530 }
531 
532 void
533 tx_queue_infos_display(portid_t port_id, uint16_t queue_id)
534 {
535 	struct rte_eth_burst_mode mode;
536 	struct rte_eth_txq_info qinfo;
537 	int32_t rc;
538 	static const char *info_border = "*********************";
539 
540 	rc = rte_eth_tx_queue_info_get(port_id, queue_id, &qinfo);
541 	if (rc != 0) {
542 		fprintf(stderr,
543 			"Failed to retrieve information for port: %u, TX queue: %hu\nerror desc: %s(%d)\n",
544 			port_id, queue_id, strerror(-rc), rc);
545 		return;
546 	}
547 
548 	printf("\n%s Infos for port %-2u, TX queue %-2u %s",
549 	       info_border, port_id, queue_id, info_border);
550 
551 	printf("\nTX prefetch threshold: %hhu", qinfo.conf.tx_thresh.pthresh);
552 	printf("\nTX host threshold: %hhu", qinfo.conf.tx_thresh.hthresh);
553 	printf("\nTX writeback threshold: %hhu", qinfo.conf.tx_thresh.wthresh);
554 	printf("\nTX RS threshold: %hu", qinfo.conf.tx_rs_thresh);
555 	printf("\nTX free threshold: %hu", qinfo.conf.tx_free_thresh);
556 	printf("\nTX deferred start: %s",
557 		(qinfo.conf.tx_deferred_start != 0) ? "on" : "off");
558 	printf("\nNumber of TXDs: %hu", qinfo.nb_desc);
559 	printf("\nTx queue state: %s", get_queue_state_name(qinfo.queue_state));
560 
561 	if (rte_eth_tx_burst_mode_get(port_id, queue_id, &mode) == 0)
562 		printf("\nBurst mode: %s%s",
563 		       mode.info,
564 		       mode.flags & RTE_ETH_BURST_FLAG_PER_QUEUE ?
565 				" (per queue)" : "");
566 
567 	printf("\n");
568 }
569 
570 static int bus_match_all(const struct rte_bus *bus, const void *data)
571 {
572 	RTE_SET_USED(bus);
573 	RTE_SET_USED(data);
574 	return 0;
575 }
576 
577 static void
578 device_infos_display_speeds(uint32_t speed_capa)
579 {
580 	printf("\n\tDevice speed capability:");
581 	if (speed_capa == RTE_ETH_LINK_SPEED_AUTONEG)
582 		printf(" Autonegotiate (all speeds)");
583 	if (speed_capa & RTE_ETH_LINK_SPEED_FIXED)
584 		printf(" Disable autonegotiate (fixed speed)  ");
585 	if (speed_capa & RTE_ETH_LINK_SPEED_10M_HD)
586 		printf(" 10 Mbps half-duplex  ");
587 	if (speed_capa & RTE_ETH_LINK_SPEED_10M)
588 		printf(" 10 Mbps full-duplex  ");
589 	if (speed_capa & RTE_ETH_LINK_SPEED_100M_HD)
590 		printf(" 100 Mbps half-duplex  ");
591 	if (speed_capa & RTE_ETH_LINK_SPEED_100M)
592 		printf(" 100 Mbps full-duplex  ");
593 	if (speed_capa & RTE_ETH_LINK_SPEED_1G)
594 		printf(" 1 Gbps  ");
595 	if (speed_capa & RTE_ETH_LINK_SPEED_2_5G)
596 		printf(" 2.5 Gbps  ");
597 	if (speed_capa & RTE_ETH_LINK_SPEED_5G)
598 		printf(" 5 Gbps  ");
599 	if (speed_capa & RTE_ETH_LINK_SPEED_10G)
600 		printf(" 10 Gbps  ");
601 	if (speed_capa & RTE_ETH_LINK_SPEED_20G)
602 		printf(" 20 Gbps  ");
603 	if (speed_capa & RTE_ETH_LINK_SPEED_25G)
604 		printf(" 25 Gbps  ");
605 	if (speed_capa & RTE_ETH_LINK_SPEED_40G)
606 		printf(" 40 Gbps  ");
607 	if (speed_capa & RTE_ETH_LINK_SPEED_50G)
608 		printf(" 50 Gbps  ");
609 	if (speed_capa & RTE_ETH_LINK_SPEED_56G)
610 		printf(" 56 Gbps  ");
611 	if (speed_capa & RTE_ETH_LINK_SPEED_100G)
612 		printf(" 100 Gbps  ");
613 	if (speed_capa & RTE_ETH_LINK_SPEED_200G)
614 		printf(" 200 Gbps  ");
615 }
616 
617 void
618 device_infos_display(const char *identifier)
619 {
620 	static const char *info_border = "*********************";
621 	struct rte_bus *start = NULL, *next;
622 	struct rte_dev_iterator dev_iter;
623 	char name[RTE_ETH_NAME_MAX_LEN];
624 	struct rte_ether_addr mac_addr;
625 	struct rte_device *dev;
626 	struct rte_devargs da;
627 	portid_t port_id;
628 	struct rte_eth_dev_info dev_info;
629 	char devstr[128];
630 
631 	memset(&da, 0, sizeof(da));
632 	if (!identifier)
633 		goto skip_parse;
634 
635 	if (rte_devargs_parsef(&da, "%s", identifier)) {
636 		fprintf(stderr, "cannot parse identifier\n");
637 		return;
638 	}
639 
640 skip_parse:
641 	while ((next = rte_bus_find(start, bus_match_all, NULL)) != NULL) {
642 
643 		start = next;
644 		if (identifier && da.bus != next)
645 			continue;
646 
647 		snprintf(devstr, sizeof(devstr), "bus=%s", rte_bus_name(next));
648 		RTE_DEV_FOREACH(dev, devstr, &dev_iter) {
649 
650 			if (rte_dev_driver(dev) == NULL)
651 				continue;
652 			/* Check for matching device if identifier is present */
653 			if (identifier &&
654 			    strncmp(da.name, rte_dev_name(dev), strlen(rte_dev_name(dev))))
655 				continue;
656 			printf("\n%s Infos for device %s %s\n",
657 			       info_border, rte_dev_name(dev), info_border);
658 			printf("Bus name: %s", rte_bus_name(rte_dev_bus(dev)));
659 			printf("\nBus information: %s",
660 				rte_dev_bus_info(dev) ? rte_dev_bus_info(dev) : "");
661 			printf("\nDriver name: %s", rte_driver_name(rte_dev_driver(dev)));
662 			printf("\nDevargs: %s",
663 			       rte_dev_devargs(dev) ? rte_dev_devargs(dev)->args : "");
664 			printf("\nConnect to socket: %d", rte_dev_numa_node(dev));
665 			printf("\n");
666 
667 			/* List ports with matching device name */
668 			RTE_ETH_FOREACH_DEV_OF(port_id, dev) {
669 				printf("\n\tPort id: %-2d", port_id);
670 				if (eth_macaddr_get_print_err(port_id,
671 							      &mac_addr) == 0)
672 					print_ethaddr("\n\tMAC address: ",
673 						      &mac_addr);
674 				rte_eth_dev_get_name_by_port(port_id, name);
675 				printf("\n\tDevice name: %s", name);
676 				if (rte_eth_dev_info_get(port_id, &dev_info) == 0)
677 					device_infos_display_speeds(dev_info.speed_capa);
678 				printf("\n");
679 			}
680 		}
681 	};
682 	rte_devargs_reset(&da);
683 }
684 
685 static void
686 print_dev_capabilities(uint64_t capabilities)
687 {
688 	uint64_t single_capa;
689 	int begin;
690 	int end;
691 	int bit;
692 
693 	if (capabilities == 0)
694 		return;
695 
696 	begin = __builtin_ctzll(capabilities);
697 	end = sizeof(capabilities) * CHAR_BIT - __builtin_clzll(capabilities);
698 
699 	single_capa = 1ULL << begin;
700 	for (bit = begin; bit < end; bit++) {
701 		if (capabilities & single_capa)
702 			printf(" %s",
703 			       rte_eth_dev_capability_name(single_capa));
704 		single_capa <<= 1;
705 	}
706 }
707 
708 uint64_t
709 str_to_rsstypes(const char *str)
710 {
711 	uint16_t i;
712 
713 	for (i = 0; rss_type_table[i].str != NULL; i++) {
714 		if (strcmp(rss_type_table[i].str, str) == 0)
715 			return rss_type_table[i].rss_type;
716 	}
717 
718 	return 0;
719 }
720 
721 const char *
722 rsstypes_to_str(uint64_t rss_type)
723 {
724 	uint16_t i;
725 
726 	for (i = 0; rss_type_table[i].str != NULL; i++) {
727 		if (rss_type_table[i].rss_type == rss_type)
728 			return rss_type_table[i].str;
729 	}
730 
731 	return NULL;
732 }
733 
734 static void
735 rss_offload_types_display(uint64_t offload_types, uint16_t char_num_per_line)
736 {
737 	uint16_t user_defined_str_len;
738 	uint16_t total_len = 0;
739 	uint16_t str_len = 0;
740 	uint64_t rss_offload;
741 	uint16_t i;
742 
743 	for (i = 0; i < sizeof(offload_types) * CHAR_BIT; i++) {
744 		rss_offload = RTE_BIT64(i);
745 		if ((offload_types & rss_offload) != 0) {
746 			const char *p = rsstypes_to_str(rss_offload);
747 
748 			user_defined_str_len =
749 				strlen("user-defined-") + (i / 10 + 1);
750 			str_len = p ? strlen(p) : user_defined_str_len;
751 			str_len += 2; /* add two spaces */
752 			if (total_len + str_len >= char_num_per_line) {
753 				total_len = 0;
754 				printf("\n");
755 			}
756 
757 			if (p)
758 				printf("  %s", p);
759 			else
760 				printf("  user-defined-%u", i);
761 			total_len += str_len;
762 		}
763 	}
764 	printf("\n");
765 }
766 
767 void
768 port_infos_display(portid_t port_id)
769 {
770 	struct rte_port *port;
771 	struct rte_ether_addr mac_addr;
772 	struct rte_eth_link link;
773 	struct rte_eth_dev_info dev_info;
774 	int vlan_offload;
775 	struct rte_mempool * mp;
776 	static const char *info_border = "*********************";
777 	uint16_t mtu;
778 	char name[RTE_ETH_NAME_MAX_LEN];
779 	int ret;
780 	char fw_version[ETHDEV_FWVERS_LEN];
781 
782 	if (port_id_is_invalid(port_id, ENABLED_WARN)) {
783 		print_valid_ports();
784 		return;
785 	}
786 	port = &ports[port_id];
787 	ret = eth_link_get_nowait_print_err(port_id, &link);
788 	if (ret < 0)
789 		return;
790 
791 	ret = eth_dev_info_get_print_err(port_id, &dev_info);
792 	if (ret != 0)
793 		return;
794 
795 	printf("\n%s Infos for port %-2d %s\n",
796 	       info_border, port_id, info_border);
797 	if (eth_macaddr_get_print_err(port_id, &mac_addr) == 0)
798 		print_ethaddr("MAC address: ", &mac_addr);
799 	rte_eth_dev_get_name_by_port(port_id, name);
800 	printf("\nDevice name: %s", name);
801 	printf("\nDriver name: %s", dev_info.driver_name);
802 
803 	if (rte_eth_dev_fw_version_get(port_id, fw_version,
804 						ETHDEV_FWVERS_LEN) == 0)
805 		printf("\nFirmware-version: %s", fw_version);
806 	else
807 		printf("\nFirmware-version: %s", "not available");
808 
809 	if (rte_dev_devargs(dev_info.device) && rte_dev_devargs(dev_info.device)->args)
810 		printf("\nDevargs: %s", rte_dev_devargs(dev_info.device)->args);
811 	printf("\nConnect to socket: %u", port->socket_id);
812 
813 	if (port_numa[port_id] != NUMA_NO_CONFIG) {
814 		mp = mbuf_pool_find(port_numa[port_id], 0);
815 		if (mp)
816 			printf("\nmemory allocation on the socket: %d",
817 							port_numa[port_id]);
818 	} else
819 		printf("\nmemory allocation on the socket: %u",port->socket_id);
820 
821 	printf("\nLink status: %s\n", (link.link_status) ? ("up") : ("down"));
822 	printf("Link speed: %s\n", rte_eth_link_speed_to_str(link.link_speed));
823 	printf("Link duplex: %s\n", (link.link_duplex == RTE_ETH_LINK_FULL_DUPLEX) ?
824 	       ("full-duplex") : ("half-duplex"));
825 	printf("Autoneg status: %s\n", (link.link_autoneg == RTE_ETH_LINK_AUTONEG) ?
826 	       ("On") : ("Off"));
827 
828 	if (!rte_eth_dev_get_mtu(port_id, &mtu))
829 		printf("MTU: %u\n", mtu);
830 
831 	printf("Promiscuous mode: %s\n",
832 	       rte_eth_promiscuous_get(port_id) ? "enabled" : "disabled");
833 	printf("Allmulticast mode: %s\n",
834 	       rte_eth_allmulticast_get(port_id) ? "enabled" : "disabled");
835 	printf("Maximum number of MAC addresses: %u\n",
836 	       (unsigned int)(port->dev_info.max_mac_addrs));
837 	printf("Maximum number of MAC addresses of hash filtering: %u\n",
838 	       (unsigned int)(port->dev_info.max_hash_mac_addrs));
839 
840 	vlan_offload = rte_eth_dev_get_vlan_offload(port_id);
841 	if (vlan_offload >= 0){
842 		printf("VLAN offload: \n");
843 		if (vlan_offload & RTE_ETH_VLAN_STRIP_OFFLOAD)
844 			printf("  strip on, ");
845 		else
846 			printf("  strip off, ");
847 
848 		if (vlan_offload & RTE_ETH_VLAN_FILTER_OFFLOAD)
849 			printf("filter on, ");
850 		else
851 			printf("filter off, ");
852 
853 		if (vlan_offload & RTE_ETH_VLAN_EXTEND_OFFLOAD)
854 			printf("extend on, ");
855 		else
856 			printf("extend off, ");
857 
858 		if (vlan_offload & RTE_ETH_QINQ_STRIP_OFFLOAD)
859 			printf("qinq strip on\n");
860 		else
861 			printf("qinq strip off\n");
862 	}
863 
864 	if (dev_info.hash_key_size > 0)
865 		printf("Hash key size in bytes: %u\n", dev_info.hash_key_size);
866 	if (dev_info.reta_size > 0)
867 		printf("Redirection table size: %u\n", dev_info.reta_size);
868 	if (!dev_info.flow_type_rss_offloads)
869 		printf("No RSS offload flow type is supported.\n");
870 	else {
871 		printf("Supported RSS offload flow types:\n");
872 		rss_offload_types_display(dev_info.flow_type_rss_offloads,
873 				TESTPMD_RSS_TYPES_CHAR_NUM_PER_LINE);
874 	}
875 
876 	printf("Minimum size of RX buffer: %u\n", dev_info.min_rx_bufsize);
877 	printf("Maximum configurable length of RX packet: %u\n",
878 		dev_info.max_rx_pktlen);
879 	printf("Maximum configurable size of LRO aggregated packet: %u\n",
880 		dev_info.max_lro_pkt_size);
881 	if (dev_info.max_vfs)
882 		printf("Maximum number of VFs: %u\n", dev_info.max_vfs);
883 	if (dev_info.max_vmdq_pools)
884 		printf("Maximum number of VMDq pools: %u\n",
885 			dev_info.max_vmdq_pools);
886 
887 	printf("Current number of RX queues: %u\n", dev_info.nb_rx_queues);
888 	printf("Max possible RX queues: %u\n", dev_info.max_rx_queues);
889 	printf("Max possible number of RXDs per queue: %hu\n",
890 		dev_info.rx_desc_lim.nb_max);
891 	printf("Min possible number of RXDs per queue: %hu\n",
892 		dev_info.rx_desc_lim.nb_min);
893 	printf("RXDs number alignment: %hu\n", dev_info.rx_desc_lim.nb_align);
894 
895 	printf("Current number of TX queues: %u\n", dev_info.nb_tx_queues);
896 	printf("Max possible TX queues: %u\n", dev_info.max_tx_queues);
897 	printf("Max possible number of TXDs per queue: %hu\n",
898 		dev_info.tx_desc_lim.nb_max);
899 	printf("Min possible number of TXDs per queue: %hu\n",
900 		dev_info.tx_desc_lim.nb_min);
901 	printf("TXDs number alignment: %hu\n", dev_info.tx_desc_lim.nb_align);
902 	printf("Max segment number per packet: %hu\n",
903 		dev_info.tx_desc_lim.nb_seg_max);
904 	printf("Max segment number per MTU/TSO: %hu\n",
905 		dev_info.tx_desc_lim.nb_mtu_seg_max);
906 
907 	printf("Device capabilities: 0x%"PRIx64"(", dev_info.dev_capa);
908 	print_dev_capabilities(dev_info.dev_capa);
909 	printf(" )\n");
910 	/* Show switch info only if valid switch domain and port id is set */
911 	if (dev_info.switch_info.domain_id !=
912 		RTE_ETH_DEV_SWITCH_DOMAIN_ID_INVALID) {
913 		if (dev_info.switch_info.name)
914 			printf("Switch name: %s\n", dev_info.switch_info.name);
915 
916 		printf("Switch domain Id: %u\n",
917 			dev_info.switch_info.domain_id);
918 		printf("Switch Port Id: %u\n",
919 			dev_info.switch_info.port_id);
920 		if ((dev_info.dev_capa & RTE_ETH_DEV_CAPA_RXQ_SHARE) != 0)
921 			printf("Switch Rx domain: %u\n",
922 			       dev_info.switch_info.rx_domain);
923 	}
924 	printf("Device error handling mode: ");
925 	switch (dev_info.err_handle_mode) {
926 	case RTE_ETH_ERROR_HANDLE_MODE_NONE:
927 		printf("none\n");
928 		break;
929 	case RTE_ETH_ERROR_HANDLE_MODE_PASSIVE:
930 		printf("passive\n");
931 		break;
932 	case RTE_ETH_ERROR_HANDLE_MODE_PROACTIVE:
933 		printf("proactive\n");
934 		break;
935 	default:
936 		printf("unknown\n");
937 		break;
938 	}
939 }
940 
941 void
942 port_summary_header_display(void)
943 {
944 	uint16_t port_number;
945 
946 	port_number = rte_eth_dev_count_avail();
947 	printf("Number of available ports: %i\n", port_number);
948 	printf("%-4s %-17s %-12s %-14s %-8s %s\n", "Port", "MAC Address", "Name",
949 			"Driver", "Status", "Link");
950 }
951 
952 void
953 port_summary_display(portid_t port_id)
954 {
955 	struct rte_ether_addr mac_addr;
956 	struct rte_eth_link link;
957 	struct rte_eth_dev_info dev_info;
958 	char name[RTE_ETH_NAME_MAX_LEN];
959 	int ret;
960 
961 	if (port_id_is_invalid(port_id, ENABLED_WARN)) {
962 		print_valid_ports();
963 		return;
964 	}
965 
966 	ret = eth_link_get_nowait_print_err(port_id, &link);
967 	if (ret < 0)
968 		return;
969 
970 	ret = eth_dev_info_get_print_err(port_id, &dev_info);
971 	if (ret != 0)
972 		return;
973 
974 	rte_eth_dev_get_name_by_port(port_id, name);
975 	ret = eth_macaddr_get_print_err(port_id, &mac_addr);
976 	if (ret != 0)
977 		return;
978 
979 	printf("%-4d " RTE_ETHER_ADDR_PRT_FMT " %-12s %-14s %-8s %s\n",
980 		port_id, RTE_ETHER_ADDR_BYTES(&mac_addr), name,
981 		dev_info.driver_name, (link.link_status) ? ("up") : ("down"),
982 		rte_eth_link_speed_to_str(link.link_speed));
983 }
984 
985 void
986 port_eeprom_display(portid_t port_id)
987 {
988 	struct rte_dev_eeprom_info einfo;
989 	int ret;
990 	if (port_id_is_invalid(port_id, ENABLED_WARN)) {
991 		print_valid_ports();
992 		return;
993 	}
994 
995 	int len_eeprom = rte_eth_dev_get_eeprom_length(port_id);
996 	if (len_eeprom < 0) {
997 		switch (len_eeprom) {
998 		case -ENODEV:
999 			fprintf(stderr, "port index %d invalid\n", port_id);
1000 			break;
1001 		case -ENOTSUP:
1002 			fprintf(stderr, "operation not supported by device\n");
1003 			break;
1004 		case -EIO:
1005 			fprintf(stderr, "device is removed\n");
1006 			break;
1007 		default:
1008 			fprintf(stderr, "Unable to get EEPROM: %d\n",
1009 				len_eeprom);
1010 			break;
1011 		}
1012 		return;
1013 	}
1014 
1015 	einfo.offset = 0;
1016 	einfo.length = len_eeprom;
1017 	einfo.data = calloc(1, len_eeprom);
1018 	if (!einfo.data) {
1019 		fprintf(stderr,
1020 			"Allocation of port %u eeprom data failed\n",
1021 			port_id);
1022 		return;
1023 	}
1024 
1025 	ret = rte_eth_dev_get_eeprom(port_id, &einfo);
1026 	if (ret != 0) {
1027 		switch (ret) {
1028 		case -ENODEV:
1029 			fprintf(stderr, "port index %d invalid\n", port_id);
1030 			break;
1031 		case -ENOTSUP:
1032 			fprintf(stderr, "operation not supported by device\n");
1033 			break;
1034 		case -EIO:
1035 			fprintf(stderr, "device is removed\n");
1036 			break;
1037 		default:
1038 			fprintf(stderr, "Unable to get EEPROM: %d\n", ret);
1039 			break;
1040 		}
1041 		free(einfo.data);
1042 		return;
1043 	}
1044 	rte_hexdump(stdout, "hexdump", einfo.data, einfo.length);
1045 	printf("Finish -- Port: %d EEPROM length: %d bytes\n", port_id, len_eeprom);
1046 	free(einfo.data);
1047 }
1048 
1049 void
1050 port_module_eeprom_display(portid_t port_id)
1051 {
1052 	struct rte_eth_dev_module_info minfo;
1053 	struct rte_dev_eeprom_info einfo;
1054 	int ret;
1055 
1056 	if (port_id_is_invalid(port_id, ENABLED_WARN)) {
1057 		print_valid_ports();
1058 		return;
1059 	}
1060 
1061 
1062 	ret = rte_eth_dev_get_module_info(port_id, &minfo);
1063 	if (ret != 0) {
1064 		switch (ret) {
1065 		case -ENODEV:
1066 			fprintf(stderr, "port index %d invalid\n", port_id);
1067 			break;
1068 		case -ENOTSUP:
1069 			fprintf(stderr, "operation not supported by device\n");
1070 			break;
1071 		case -EIO:
1072 			fprintf(stderr, "device is removed\n");
1073 			break;
1074 		default:
1075 			fprintf(stderr, "Unable to get module EEPROM: %d\n",
1076 				ret);
1077 			break;
1078 		}
1079 		return;
1080 	}
1081 
1082 	einfo.offset = 0;
1083 	einfo.length = minfo.eeprom_len;
1084 	einfo.data = calloc(1, minfo.eeprom_len);
1085 	if (!einfo.data) {
1086 		fprintf(stderr,
1087 			"Allocation of port %u eeprom data failed\n",
1088 			port_id);
1089 		return;
1090 	}
1091 
1092 	ret = rte_eth_dev_get_module_eeprom(port_id, &einfo);
1093 	if (ret != 0) {
1094 		switch (ret) {
1095 		case -ENODEV:
1096 			fprintf(stderr, "port index %d invalid\n", port_id);
1097 			break;
1098 		case -ENOTSUP:
1099 			fprintf(stderr, "operation not supported by device\n");
1100 			break;
1101 		case -EIO:
1102 			fprintf(stderr, "device is removed\n");
1103 			break;
1104 		default:
1105 			fprintf(stderr, "Unable to get module EEPROM: %d\n",
1106 				ret);
1107 			break;
1108 		}
1109 		free(einfo.data);
1110 		return;
1111 	}
1112 
1113 	rte_hexdump(stdout, "hexdump", einfo.data, einfo.length);
1114 	printf("Finish -- Port: %d MODULE EEPROM length: %d bytes\n", port_id, einfo.length);
1115 	free(einfo.data);
1116 }
1117 
1118 int
1119 port_id_is_invalid(portid_t port_id, enum print_warning warning)
1120 {
1121 	uint16_t pid;
1122 
1123 	if (port_id == (portid_t)RTE_PORT_ALL)
1124 		return 0;
1125 
1126 	RTE_ETH_FOREACH_DEV(pid)
1127 		if (port_id == pid)
1128 			return 0;
1129 
1130 	if (warning == ENABLED_WARN)
1131 		fprintf(stderr, "Invalid port %d\n", port_id);
1132 
1133 	return 1;
1134 }
1135 
1136 void print_valid_ports(void)
1137 {
1138 	portid_t pid;
1139 
1140 	printf("The valid ports array is [");
1141 	RTE_ETH_FOREACH_DEV(pid) {
1142 		printf(" %d", pid);
1143 	}
1144 	printf(" ]\n");
1145 }
1146 
1147 static int
1148 vlan_id_is_invalid(uint16_t vlan_id)
1149 {
1150 	if (vlan_id < 4096)
1151 		return 0;
1152 	fprintf(stderr, "Invalid vlan_id %d (must be < 4096)\n", vlan_id);
1153 	return 1;
1154 }
1155 
1156 static uint32_t
1157 eth_dev_get_overhead_len(uint32_t max_rx_pktlen, uint16_t max_mtu)
1158 {
1159 	uint32_t overhead_len;
1160 
1161 	if (max_mtu != UINT16_MAX && max_rx_pktlen > max_mtu)
1162 		overhead_len = max_rx_pktlen - max_mtu;
1163 	else
1164 		overhead_len = RTE_ETHER_HDR_LEN + RTE_ETHER_CRC_LEN;
1165 
1166 	return overhead_len;
1167 }
1168 
1169 static int
1170 eth_dev_validate_mtu(uint16_t port_id, uint16_t mtu)
1171 {
1172 	struct rte_eth_dev_info dev_info;
1173 	uint32_t overhead_len;
1174 	uint32_t frame_size;
1175 	int ret;
1176 
1177 	ret = rte_eth_dev_info_get(port_id, &dev_info);
1178 	if (ret != 0)
1179 		return ret;
1180 
1181 	if (mtu < dev_info.min_mtu) {
1182 		fprintf(stderr,
1183 			"MTU (%u) < device min MTU (%u) for port_id %u\n",
1184 			mtu, dev_info.min_mtu, port_id);
1185 		return -EINVAL;
1186 	}
1187 	if (mtu > dev_info.max_mtu) {
1188 		fprintf(stderr,
1189 			"MTU (%u) > device max MTU (%u) for port_id %u\n",
1190 			mtu, dev_info.max_mtu, port_id);
1191 		return -EINVAL;
1192 	}
1193 
1194 	overhead_len = eth_dev_get_overhead_len(dev_info.max_rx_pktlen,
1195 			dev_info.max_mtu);
1196 	frame_size = mtu + overhead_len;
1197 	if (frame_size > dev_info.max_rx_pktlen) {
1198 		fprintf(stderr,
1199 			"Frame size (%u) > device max frame size (%u) for port_id %u\n",
1200 			frame_size, dev_info.max_rx_pktlen, port_id);
1201 		return -EINVAL;
1202 	}
1203 
1204 	return 0;
1205 }
1206 
1207 void
1208 port_mtu_set(portid_t port_id, uint16_t mtu)
1209 {
1210 	struct rte_port *port = &ports[port_id];
1211 	int diag;
1212 
1213 	if (port_id_is_invalid(port_id, ENABLED_WARN))
1214 		return;
1215 
1216 	diag = eth_dev_validate_mtu(port_id, mtu);
1217 	if (diag != 0)
1218 		return;
1219 
1220 	if (port->need_reconfig == 0) {
1221 		diag = rte_eth_dev_set_mtu(port_id, mtu);
1222 		if (diag != 0) {
1223 			fprintf(stderr, "Set MTU failed. diag=%d\n", diag);
1224 			return;
1225 		}
1226 	}
1227 
1228 	port->dev_conf.rxmode.mtu = mtu;
1229 }
1230 
1231 /* Generic flow management functions. */
1232 
1233 static struct port_flow_tunnel *
1234 port_flow_locate_tunnel_id(struct rte_port *port, uint32_t port_tunnel_id)
1235 {
1236 	struct port_flow_tunnel *flow_tunnel;
1237 
1238 	LIST_FOREACH(flow_tunnel, &port->flow_tunnel_list, chain) {
1239 		if (flow_tunnel->id == port_tunnel_id)
1240 			goto out;
1241 	}
1242 	flow_tunnel = NULL;
1243 
1244 out:
1245 	return flow_tunnel;
1246 }
1247 
1248 const char *
1249 port_flow_tunnel_type(struct rte_flow_tunnel *tunnel)
1250 {
1251 	const char *type;
1252 	switch (tunnel->type) {
1253 	default:
1254 		type = "unknown";
1255 		break;
1256 	case RTE_FLOW_ITEM_TYPE_VXLAN:
1257 		type = "vxlan";
1258 		break;
1259 	case RTE_FLOW_ITEM_TYPE_GRE:
1260 		type = "gre";
1261 		break;
1262 	case RTE_FLOW_ITEM_TYPE_NVGRE:
1263 		type = "nvgre";
1264 		break;
1265 	case RTE_FLOW_ITEM_TYPE_GENEVE:
1266 		type = "geneve";
1267 		break;
1268 	}
1269 
1270 	return type;
1271 }
1272 
1273 struct port_flow_tunnel *
1274 port_flow_locate_tunnel(uint16_t port_id, struct rte_flow_tunnel *tun)
1275 {
1276 	struct rte_port *port = &ports[port_id];
1277 	struct port_flow_tunnel *flow_tunnel;
1278 
1279 	LIST_FOREACH(flow_tunnel, &port->flow_tunnel_list, chain) {
1280 		if (!memcmp(&flow_tunnel->tunnel, tun, sizeof(*tun)))
1281 			goto out;
1282 	}
1283 	flow_tunnel = NULL;
1284 
1285 out:
1286 	return flow_tunnel;
1287 }
1288 
1289 void port_flow_tunnel_list(portid_t port_id)
1290 {
1291 	struct rte_port *port = &ports[port_id];
1292 	struct port_flow_tunnel *flt;
1293 
1294 	LIST_FOREACH(flt, &port->flow_tunnel_list, chain) {
1295 		printf("port %u tunnel #%u type=%s",
1296 			port_id, flt->id, port_flow_tunnel_type(&flt->tunnel));
1297 		if (flt->tunnel.tun_id)
1298 			printf(" id=%" PRIu64, flt->tunnel.tun_id);
1299 		printf("\n");
1300 	}
1301 }
1302 
1303 void port_flow_tunnel_destroy(portid_t port_id, uint32_t tunnel_id)
1304 {
1305 	struct rte_port *port = &ports[port_id];
1306 	struct port_flow_tunnel *flt;
1307 
1308 	LIST_FOREACH(flt, &port->flow_tunnel_list, chain) {
1309 		if (flt->id == tunnel_id)
1310 			break;
1311 	}
1312 	if (flt) {
1313 		LIST_REMOVE(flt, chain);
1314 		free(flt);
1315 		printf("port %u: flow tunnel #%u destroyed\n",
1316 			port_id, tunnel_id);
1317 	}
1318 }
1319 
1320 void port_flow_tunnel_create(portid_t port_id, const struct tunnel_ops *ops)
1321 {
1322 	struct rte_port *port = &ports[port_id];
1323 	enum rte_flow_item_type	type;
1324 	struct port_flow_tunnel *flt;
1325 
1326 	if (!strcmp(ops->type, "vxlan"))
1327 		type = RTE_FLOW_ITEM_TYPE_VXLAN;
1328 	else if (!strcmp(ops->type, "gre"))
1329 		type = RTE_FLOW_ITEM_TYPE_GRE;
1330 	else if (!strcmp(ops->type, "nvgre"))
1331 		type = RTE_FLOW_ITEM_TYPE_NVGRE;
1332 	else if (!strcmp(ops->type, "geneve"))
1333 		type = RTE_FLOW_ITEM_TYPE_GENEVE;
1334 	else {
1335 		fprintf(stderr, "cannot offload \"%s\" tunnel type\n",
1336 			ops->type);
1337 		return;
1338 	}
1339 	LIST_FOREACH(flt, &port->flow_tunnel_list, chain) {
1340 		if (flt->tunnel.type == type)
1341 			break;
1342 	}
1343 	if (!flt) {
1344 		flt = calloc(1, sizeof(*flt));
1345 		if (!flt) {
1346 			fprintf(stderr, "failed to allocate port flt object\n");
1347 			return;
1348 		}
1349 		flt->tunnel.type = type;
1350 		flt->id = LIST_EMPTY(&port->flow_tunnel_list) ? 1 :
1351 				  LIST_FIRST(&port->flow_tunnel_list)->id + 1;
1352 		LIST_INSERT_HEAD(&port->flow_tunnel_list, flt, chain);
1353 	}
1354 	printf("port %d: flow tunnel #%u type %s\n",
1355 		port_id, flt->id, ops->type);
1356 }
1357 
1358 /** Generate a port_flow entry from attributes/pattern/actions. */
1359 static struct port_flow *
1360 port_flow_new(const struct rte_flow_attr *attr,
1361 	      const struct rte_flow_item *pattern,
1362 	      const struct rte_flow_action *actions,
1363 	      struct rte_flow_error *error)
1364 {
1365 	const struct rte_flow_conv_rule rule = {
1366 		.attr_ro = attr,
1367 		.pattern_ro = pattern,
1368 		.actions_ro = actions,
1369 	};
1370 	struct port_flow *pf;
1371 	int ret;
1372 
1373 	ret = rte_flow_conv(RTE_FLOW_CONV_OP_RULE, NULL, 0, &rule, error);
1374 	if (ret < 0)
1375 		return NULL;
1376 	pf = calloc(1, offsetof(struct port_flow, rule) + ret);
1377 	if (!pf) {
1378 		rte_flow_error_set
1379 			(error, errno, RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1380 			 "calloc() failed");
1381 		return NULL;
1382 	}
1383 	if (rte_flow_conv(RTE_FLOW_CONV_OP_RULE, &pf->rule, ret, &rule,
1384 			  error) >= 0)
1385 		return pf;
1386 	free(pf);
1387 	return NULL;
1388 }
1389 
1390 /** Print a message out of a flow error. */
1391 static int
1392 port_flow_complain(struct rte_flow_error *error)
1393 {
1394 	static const char *const errstrlist[] = {
1395 		[RTE_FLOW_ERROR_TYPE_NONE] = "no error",
1396 		[RTE_FLOW_ERROR_TYPE_UNSPECIFIED] = "cause unspecified",
1397 		[RTE_FLOW_ERROR_TYPE_HANDLE] = "flow rule (handle)",
1398 		[RTE_FLOW_ERROR_TYPE_ATTR_GROUP] = "group field",
1399 		[RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY] = "priority field",
1400 		[RTE_FLOW_ERROR_TYPE_ATTR_INGRESS] = "ingress field",
1401 		[RTE_FLOW_ERROR_TYPE_ATTR_EGRESS] = "egress field",
1402 		[RTE_FLOW_ERROR_TYPE_ATTR_TRANSFER] = "transfer field",
1403 		[RTE_FLOW_ERROR_TYPE_ATTR] = "attributes structure",
1404 		[RTE_FLOW_ERROR_TYPE_ITEM_NUM] = "pattern length",
1405 		[RTE_FLOW_ERROR_TYPE_ITEM_SPEC] = "item specification",
1406 		[RTE_FLOW_ERROR_TYPE_ITEM_LAST] = "item specification range",
1407 		[RTE_FLOW_ERROR_TYPE_ITEM_MASK] = "item specification mask",
1408 		[RTE_FLOW_ERROR_TYPE_ITEM] = "specific pattern item",
1409 		[RTE_FLOW_ERROR_TYPE_ACTION_NUM] = "number of actions",
1410 		[RTE_FLOW_ERROR_TYPE_ACTION_CONF] = "action configuration",
1411 		[RTE_FLOW_ERROR_TYPE_ACTION] = "specific action",
1412 	};
1413 	const char *errstr;
1414 	char buf[32];
1415 	int err = rte_errno;
1416 
1417 	if ((unsigned int)error->type >= RTE_DIM(errstrlist) ||
1418 	    !errstrlist[error->type])
1419 		errstr = "unknown type";
1420 	else
1421 		errstr = errstrlist[error->type];
1422 	fprintf(stderr, "%s(): Caught PMD error type %d (%s): %s%s: %s\n",
1423 		__func__, error->type, errstr,
1424 		error->cause ? (snprintf(buf, sizeof(buf), "cause: %p, ",
1425 					 error->cause), buf) : "",
1426 		error->message ? error->message : "(no stated reason)",
1427 		rte_strerror(err));
1428 
1429 	switch (error->type) {
1430 	case RTE_FLOW_ERROR_TYPE_ATTR_TRANSFER:
1431 		fprintf(stderr, "The status suggests the use of \"transfer\" "
1432 				"as the possible cause of the failure. Make "
1433 				"sure that the flow in question and its "
1434 				"indirect components (if any) are managed "
1435 				"via \"transfer\" proxy port. Use command "
1436 				"\"show port (port_id) flow transfer proxy\" "
1437 				"to figure out the proxy port ID\n");
1438 		break;
1439 	default:
1440 		break;
1441 	}
1442 
1443 	return -err;
1444 }
1445 
1446 static void
1447 rss_types_display(uint64_t rss_types, uint16_t char_num_per_line)
1448 {
1449 	uint16_t total_len = 0;
1450 	uint16_t str_len;
1451 	uint16_t i;
1452 
1453 	if (rss_types == 0)
1454 		return;
1455 
1456 	for (i = 0; rss_type_table[i].str; i++) {
1457 		if (rss_type_table[i].rss_type == 0)
1458 			continue;
1459 
1460 		if ((rss_types & rss_type_table[i].rss_type) ==
1461 					rss_type_table[i].rss_type) {
1462 			/* Contain two spaces */
1463 			str_len = strlen(rss_type_table[i].str) + 2;
1464 			if (total_len + str_len > char_num_per_line) {
1465 				printf("\n");
1466 				total_len = 0;
1467 			}
1468 			printf("  %s", rss_type_table[i].str);
1469 			total_len += str_len;
1470 		}
1471 	}
1472 	printf("\n");
1473 }
1474 
1475 static void
1476 rss_config_display(struct rte_flow_action_rss *rss_conf)
1477 {
1478 	uint8_t i;
1479 
1480 	if (rss_conf == NULL) {
1481 		fprintf(stderr, "Invalid rule\n");
1482 		return;
1483 	}
1484 
1485 	printf("RSS:\n"
1486 	       " queues:");
1487 	if (rss_conf->queue_num == 0)
1488 		printf(" none");
1489 	for (i = 0; i < rss_conf->queue_num; i++)
1490 		printf(" %d", rss_conf->queue[i]);
1491 	printf("\n");
1492 
1493 	printf(" function: ");
1494 	switch (rss_conf->func) {
1495 	case RTE_ETH_HASH_FUNCTION_DEFAULT:
1496 		printf("default\n");
1497 		break;
1498 	case RTE_ETH_HASH_FUNCTION_TOEPLITZ:
1499 		printf("toeplitz\n");
1500 		break;
1501 	case RTE_ETH_HASH_FUNCTION_SIMPLE_XOR:
1502 		printf("simple_xor\n");
1503 		break;
1504 	case RTE_ETH_HASH_FUNCTION_SYMMETRIC_TOEPLITZ:
1505 		printf("symmetric_toeplitz\n");
1506 		break;
1507 	default:
1508 		printf("Unknown function\n");
1509 		return;
1510 	}
1511 
1512 	printf(" types:\n");
1513 	if (rss_conf->types == 0) {
1514 		printf("  none\n");
1515 		return;
1516 	}
1517 	rss_types_display(rss_conf->types, TESTPMD_RSS_TYPES_CHAR_NUM_PER_LINE);
1518 }
1519 
1520 static struct port_indirect_action *
1521 action_get_by_id(portid_t port_id, uint32_t id)
1522 {
1523 	struct rte_port *port;
1524 	struct port_indirect_action **ppia;
1525 	struct port_indirect_action *pia = NULL;
1526 
1527 	if (port_id_is_invalid(port_id, ENABLED_WARN) ||
1528 	    port_id == (portid_t)RTE_PORT_ALL)
1529 		return NULL;
1530 	port = &ports[port_id];
1531 	ppia = &port->actions_list;
1532 	while (*ppia) {
1533 		if ((*ppia)->id == id) {
1534 			pia = *ppia;
1535 			break;
1536 		}
1537 		ppia = &(*ppia)->next;
1538 	}
1539 	if (!pia)
1540 		fprintf(stderr,
1541 			"Failed to find indirect action #%u on port %u\n",
1542 			id, port_id);
1543 	return pia;
1544 }
1545 
1546 static int
1547 action_alloc(portid_t port_id, uint32_t id,
1548 	     struct port_indirect_action **action)
1549 {
1550 	struct rte_port *port;
1551 	struct port_indirect_action **ppia;
1552 	struct port_indirect_action *pia = NULL;
1553 
1554 	*action = NULL;
1555 	if (port_id_is_invalid(port_id, ENABLED_WARN) ||
1556 	    port_id == (portid_t)RTE_PORT_ALL)
1557 		return -EINVAL;
1558 	port = &ports[port_id];
1559 	if (id == UINT32_MAX) {
1560 		/* taking first available ID */
1561 		if (port->actions_list) {
1562 			if (port->actions_list->id == UINT32_MAX - 1) {
1563 				fprintf(stderr,
1564 					"Highest indirect action ID is already assigned, delete it first\n");
1565 				return -ENOMEM;
1566 			}
1567 			id = port->actions_list->id + 1;
1568 		} else {
1569 			id = 0;
1570 		}
1571 	}
1572 	pia = calloc(1, sizeof(*pia));
1573 	if (!pia) {
1574 		fprintf(stderr,
1575 			"Allocation of port %u indirect action failed\n",
1576 			port_id);
1577 		return -ENOMEM;
1578 	}
1579 	ppia = &port->actions_list;
1580 	while (*ppia && (*ppia)->id > id)
1581 		ppia = &(*ppia)->next;
1582 	if (*ppia && (*ppia)->id == id) {
1583 		fprintf(stderr,
1584 			"Indirect action #%u is already assigned, delete it first\n",
1585 			id);
1586 		free(pia);
1587 		return -EINVAL;
1588 	}
1589 	pia->next = *ppia;
1590 	pia->id = id;
1591 	*ppia = pia;
1592 	*action = pia;
1593 	return 0;
1594 }
1595 
1596 static int
1597 template_alloc(uint32_t id, struct port_template **template,
1598 	       struct port_template **list)
1599 {
1600 	struct port_template *lst = *list;
1601 	struct port_template **ppt;
1602 	struct port_template *pt = NULL;
1603 
1604 	*template = NULL;
1605 	if (id == UINT32_MAX) {
1606 		/* taking first available ID */
1607 		if (lst) {
1608 			if (lst->id == UINT32_MAX - 1) {
1609 				printf("Highest template ID is already"
1610 				" assigned, delete it first\n");
1611 				return -ENOMEM;
1612 			}
1613 			id = lst->id + 1;
1614 		} else {
1615 			id = 0;
1616 		}
1617 	}
1618 	pt = calloc(1, sizeof(*pt));
1619 	if (!pt) {
1620 		printf("Allocation of port template failed\n");
1621 		return -ENOMEM;
1622 	}
1623 	ppt = list;
1624 	while (*ppt && (*ppt)->id > id)
1625 		ppt = &(*ppt)->next;
1626 	if (*ppt && (*ppt)->id == id) {
1627 		printf("Template #%u is already assigned,"
1628 			" delete it first\n", id);
1629 		free(pt);
1630 		return -EINVAL;
1631 	}
1632 	pt->next = *ppt;
1633 	pt->id = id;
1634 	*ppt = pt;
1635 	*template = pt;
1636 	return 0;
1637 }
1638 
1639 static int
1640 table_alloc(uint32_t id, struct port_table **table,
1641 	    struct port_table **list)
1642 {
1643 	struct port_table *lst = *list;
1644 	struct port_table **ppt;
1645 	struct port_table *pt = NULL;
1646 
1647 	*table = NULL;
1648 	if (id == UINT32_MAX) {
1649 		/* taking first available ID */
1650 		if (lst) {
1651 			if (lst->id == UINT32_MAX - 1) {
1652 				printf("Highest table ID is already"
1653 				" assigned, delete it first\n");
1654 				return -ENOMEM;
1655 			}
1656 			id = lst->id + 1;
1657 		} else {
1658 			id = 0;
1659 		}
1660 	}
1661 	pt = calloc(1, sizeof(*pt));
1662 	if (!pt) {
1663 		printf("Allocation of table failed\n");
1664 		return -ENOMEM;
1665 	}
1666 	ppt = list;
1667 	while (*ppt && (*ppt)->id > id)
1668 		ppt = &(*ppt)->next;
1669 	if (*ppt && (*ppt)->id == id) {
1670 		printf("Table #%u is already assigned,"
1671 			" delete it first\n", id);
1672 		free(pt);
1673 		return -EINVAL;
1674 	}
1675 	pt->next = *ppt;
1676 	pt->id = id;
1677 	*ppt = pt;
1678 	*table = pt;
1679 	return 0;
1680 }
1681 
1682 /** Get info about flow management resources. */
1683 int
1684 port_flow_get_info(portid_t port_id)
1685 {
1686 	struct rte_flow_port_info port_info;
1687 	struct rte_flow_queue_info queue_info;
1688 	struct rte_flow_error error;
1689 
1690 	if (port_id_is_invalid(port_id, ENABLED_WARN) ||
1691 	    port_id == (portid_t)RTE_PORT_ALL)
1692 		return -EINVAL;
1693 	/* Poisoning to make sure PMDs update it in case of error. */
1694 	memset(&error, 0x99, sizeof(error));
1695 	memset(&port_info, 0, sizeof(port_info));
1696 	memset(&queue_info, 0, sizeof(queue_info));
1697 	if (rte_flow_info_get(port_id, &port_info, &queue_info, &error))
1698 		return port_flow_complain(&error);
1699 	printf("Flow engine resources on port %u:\n"
1700 	       "Number of queues: %d\n"
1701 		   "Size of queues: %d\n"
1702 	       "Number of counters: %d\n"
1703 	       "Number of aging objects: %d\n"
1704 	       "Number of meter actions: %d\n",
1705 	       port_id, port_info.max_nb_queues,
1706 		   queue_info.max_size,
1707 	       port_info.max_nb_counters,
1708 	       port_info.max_nb_aging_objects,
1709 	       port_info.max_nb_meters);
1710 	return 0;
1711 }
1712 
1713 /** Configure flow management resources. */
1714 int
1715 port_flow_configure(portid_t port_id,
1716 	const struct rte_flow_port_attr *port_attr,
1717 	uint16_t nb_queue,
1718 	const struct rte_flow_queue_attr *queue_attr)
1719 {
1720 	struct rte_port *port;
1721 	struct rte_flow_error error;
1722 	const struct rte_flow_queue_attr *attr_list[nb_queue];
1723 	int std_queue;
1724 
1725 	if (port_id_is_invalid(port_id, ENABLED_WARN) ||
1726 	    port_id == (portid_t)RTE_PORT_ALL)
1727 		return -EINVAL;
1728 	port = &ports[port_id];
1729 	port->queue_nb = nb_queue;
1730 	port->queue_sz = queue_attr->size;
1731 	for (std_queue = 0; std_queue < nb_queue; std_queue++)
1732 		attr_list[std_queue] = queue_attr;
1733 	/* Poisoning to make sure PMDs update it in case of error. */
1734 	memset(&error, 0x66, sizeof(error));
1735 	if (rte_flow_configure(port_id, port_attr, nb_queue, attr_list, &error))
1736 		return port_flow_complain(&error);
1737 	printf("Configure flows on port %u: "
1738 	       "number of queues %d with %d elements\n",
1739 	       port_id, nb_queue, queue_attr->size);
1740 	return 0;
1741 }
1742 
1743 /** Create indirect action */
1744 int
1745 port_action_handle_create(portid_t port_id, uint32_t id,
1746 			  const struct rte_flow_indir_action_conf *conf,
1747 			  const struct rte_flow_action *action)
1748 {
1749 	struct port_indirect_action *pia;
1750 	int ret;
1751 	struct rte_flow_error error;
1752 
1753 	ret = action_alloc(port_id, id, &pia);
1754 	if (ret)
1755 		return ret;
1756 	if (action->type == RTE_FLOW_ACTION_TYPE_AGE) {
1757 		struct rte_flow_action_age *age =
1758 			(struct rte_flow_action_age *)(uintptr_t)(action->conf);
1759 
1760 		pia->age_type = ACTION_AGE_CONTEXT_TYPE_INDIRECT_ACTION;
1761 		age->context = &pia->age_type;
1762 	} else if (action->type == RTE_FLOW_ACTION_TYPE_CONNTRACK) {
1763 		struct rte_flow_action_conntrack *ct =
1764 		(struct rte_flow_action_conntrack *)(uintptr_t)(action->conf);
1765 
1766 		memcpy(ct, &conntrack_context, sizeof(*ct));
1767 	}
1768 	/* Poisoning to make sure PMDs update it in case of error. */
1769 	memset(&error, 0x22, sizeof(error));
1770 	pia->handle = rte_flow_action_handle_create(port_id, conf, action,
1771 						    &error);
1772 	if (!pia->handle) {
1773 		uint32_t destroy_id = pia->id;
1774 		port_action_handle_destroy(port_id, 1, &destroy_id);
1775 		return port_flow_complain(&error);
1776 	}
1777 	pia->type = action->type;
1778 	printf("Indirect action #%u created\n", pia->id);
1779 	return 0;
1780 }
1781 
1782 /** Destroy indirect action */
1783 int
1784 port_action_handle_destroy(portid_t port_id,
1785 			   uint32_t n,
1786 			   const uint32_t *actions)
1787 {
1788 	struct rte_port *port;
1789 	struct port_indirect_action **tmp;
1790 	uint32_t c = 0;
1791 	int ret = 0;
1792 
1793 	if (port_id_is_invalid(port_id, ENABLED_WARN) ||
1794 	    port_id == (portid_t)RTE_PORT_ALL)
1795 		return -EINVAL;
1796 	port = &ports[port_id];
1797 	tmp = &port->actions_list;
1798 	while (*tmp) {
1799 		uint32_t i;
1800 
1801 		for (i = 0; i != n; ++i) {
1802 			struct rte_flow_error error;
1803 			struct port_indirect_action *pia = *tmp;
1804 
1805 			if (actions[i] != pia->id)
1806 				continue;
1807 			/*
1808 			 * Poisoning to make sure PMDs update it in case
1809 			 * of error.
1810 			 */
1811 			memset(&error, 0x33, sizeof(error));
1812 
1813 			if (pia->handle && rte_flow_action_handle_destroy(
1814 					port_id, pia->handle, &error)) {
1815 				ret = port_flow_complain(&error);
1816 				continue;
1817 			}
1818 			*tmp = pia->next;
1819 			printf("Indirect action #%u destroyed\n", pia->id);
1820 			free(pia);
1821 			break;
1822 		}
1823 		if (i == n)
1824 			tmp = &(*tmp)->next;
1825 		++c;
1826 	}
1827 	return ret;
1828 }
1829 
1830 int
1831 port_action_handle_flush(portid_t port_id)
1832 {
1833 	struct rte_port *port;
1834 	struct port_indirect_action **tmp;
1835 	int ret = 0;
1836 
1837 	if (port_id_is_invalid(port_id, ENABLED_WARN) ||
1838 	    port_id == (portid_t)RTE_PORT_ALL)
1839 		return -EINVAL;
1840 	port = &ports[port_id];
1841 	tmp = &port->actions_list;
1842 	while (*tmp != NULL) {
1843 		struct rte_flow_error error;
1844 		struct port_indirect_action *pia = *tmp;
1845 
1846 		/* Poisoning to make sure PMDs update it in case of error. */
1847 		memset(&error, 0x44, sizeof(error));
1848 		if (pia->handle != NULL &&
1849 		    rte_flow_action_handle_destroy
1850 					(port_id, pia->handle, &error) != 0) {
1851 			printf("Indirect action #%u not destroyed\n", pia->id);
1852 			ret = port_flow_complain(&error);
1853 			tmp = &pia->next;
1854 		} else {
1855 			*tmp = pia->next;
1856 			free(pia);
1857 		}
1858 	}
1859 	return ret;
1860 }
1861 
1862 /** Get indirect action by port + id */
1863 struct rte_flow_action_handle *
1864 port_action_handle_get_by_id(portid_t port_id, uint32_t id)
1865 {
1866 
1867 	struct port_indirect_action *pia = action_get_by_id(port_id, id);
1868 
1869 	return (pia) ? pia->handle : NULL;
1870 }
1871 
1872 /** Update indirect action */
1873 int
1874 port_action_handle_update(portid_t port_id, uint32_t id,
1875 			  const struct rte_flow_action *action)
1876 {
1877 	struct rte_flow_error error;
1878 	struct rte_flow_action_handle *action_handle;
1879 	struct port_indirect_action *pia;
1880 	const void *update;
1881 
1882 	action_handle = port_action_handle_get_by_id(port_id, id);
1883 	if (!action_handle)
1884 		return -EINVAL;
1885 	pia = action_get_by_id(port_id, id);
1886 	if (!pia)
1887 		return -EINVAL;
1888 	switch (pia->type) {
1889 	case RTE_FLOW_ACTION_TYPE_AGE:
1890 	case RTE_FLOW_ACTION_TYPE_CONNTRACK:
1891 		update = action->conf;
1892 		break;
1893 	default:
1894 		update = action;
1895 		break;
1896 	}
1897 	if (rte_flow_action_handle_update(port_id, action_handle, update,
1898 					  &error)) {
1899 		return port_flow_complain(&error);
1900 	}
1901 	printf("Indirect action #%u updated\n", id);
1902 	return 0;
1903 }
1904 
1905 static void
1906 port_action_handle_query_dump(uint32_t type, union port_action_query *query)
1907 {
1908 	switch (type) {
1909 	case RTE_FLOW_ACTION_TYPE_AGE:
1910 		printf("Indirect AGE action:\n"
1911 		       " aged: %u\n"
1912 		       " sec_since_last_hit_valid: %u\n"
1913 		       " sec_since_last_hit: %" PRIu32 "\n",
1914 		       query->age.aged,
1915 		       query->age.sec_since_last_hit_valid,
1916 		       query->age.sec_since_last_hit);
1917 		break;
1918 	case RTE_FLOW_ACTION_TYPE_COUNT:
1919 		printf("Indirect COUNT action:\n"
1920 		       " hits_set: %u\n"
1921 		       " bytes_set: %u\n"
1922 		       " hits: %" PRIu64 "\n"
1923 		       " bytes: %" PRIu64 "\n",
1924 		       query->count.hits_set,
1925 		       query->count.bytes_set,
1926 		       query->count.hits,
1927 		       query->count.bytes);
1928 		break;
1929 	case RTE_FLOW_ACTION_TYPE_CONNTRACK:
1930 		printf("Conntrack Context:\n"
1931 		       "  Peer: %u, Flow dir: %s, Enable: %u\n"
1932 		       "  Live: %u, SACK: %u, CACK: %u\n"
1933 		       "  Packet dir: %s, Liberal: %u, State: %u\n"
1934 		       "  Factor: %u, Retrans: %u, TCP flags: %u\n"
1935 		       "  Last Seq: %u, Last ACK: %u\n"
1936 		       "  Last Win: %u, Last End: %u\n",
1937 		       query->ct.peer_port,
1938 		       query->ct.is_original_dir ? "Original" : "Reply",
1939 		       query->ct.enable, query->ct.live_connection,
1940 		       query->ct.selective_ack, query->ct.challenge_ack_passed,
1941 		       query->ct.last_direction ? "Original" : "Reply",
1942 		       query->ct.liberal_mode, query->ct.state,
1943 		       query->ct.max_ack_window, query->ct.retransmission_limit,
1944 		       query->ct.last_index, query->ct.last_seq,
1945 		       query->ct.last_ack, query->ct.last_window,
1946 		       query->ct.last_end);
1947 		printf("  Original Dir:\n"
1948 		       "    scale: %u, fin: %u, ack seen: %u\n"
1949 		       " unacked data: %u\n    Sent end: %u,"
1950 		       "    Reply end: %u, Max win: %u, Max ACK: %u\n",
1951 		       query->ct.original_dir.scale,
1952 		       query->ct.original_dir.close_initiated,
1953 		       query->ct.original_dir.last_ack_seen,
1954 		       query->ct.original_dir.data_unacked,
1955 		       query->ct.original_dir.sent_end,
1956 		       query->ct.original_dir.reply_end,
1957 		       query->ct.original_dir.max_win,
1958 		       query->ct.original_dir.max_ack);
1959 		printf("  Reply Dir:\n"
1960 		       "    scale: %u, fin: %u, ack seen: %u\n"
1961 		       " unacked data: %u\n    Sent end: %u,"
1962 		       "    Reply end: %u, Max win: %u, Max ACK: %u\n",
1963 		       query->ct.reply_dir.scale,
1964 		       query->ct.reply_dir.close_initiated,
1965 		       query->ct.reply_dir.last_ack_seen,
1966 		       query->ct.reply_dir.data_unacked,
1967 		       query->ct.reply_dir.sent_end,
1968 		       query->ct.reply_dir.reply_end,
1969 		       query->ct.reply_dir.max_win,
1970 		       query->ct.reply_dir.max_ack);
1971 		break;
1972 	default:
1973 		fprintf(stderr,
1974 			"Indirect action (type: %d) doesn't support query\n",
1975 			type);
1976 		break;
1977 	}
1978 
1979 }
1980 
1981 int
1982 port_action_handle_query(portid_t port_id, uint32_t id)
1983 {
1984 	struct rte_flow_error error;
1985 	struct port_indirect_action *pia;
1986 	union port_action_query query;
1987 
1988 	pia = action_get_by_id(port_id, id);
1989 	if (!pia)
1990 		return -EINVAL;
1991 	switch (pia->type) {
1992 	case RTE_FLOW_ACTION_TYPE_AGE:
1993 	case RTE_FLOW_ACTION_TYPE_COUNT:
1994 		break;
1995 	default:
1996 		fprintf(stderr,
1997 			"Indirect action %u (type: %d) on port %u doesn't support query\n",
1998 			id, pia->type, port_id);
1999 		return -ENOTSUP;
2000 	}
2001 	/* Poisoning to make sure PMDs update it in case of error. */
2002 	memset(&error, 0x55, sizeof(error));
2003 	memset(&query, 0, sizeof(query));
2004 	if (rte_flow_action_handle_query(port_id, pia->handle, &query, &error))
2005 		return port_flow_complain(&error);
2006 	port_action_handle_query_dump(pia->type, &query);
2007 	return 0;
2008 }
2009 
2010 static struct port_flow_tunnel *
2011 port_flow_tunnel_offload_cmd_prep(portid_t port_id,
2012 				  const struct rte_flow_item *pattern,
2013 				  const struct rte_flow_action *actions,
2014 				  const struct tunnel_ops *tunnel_ops)
2015 {
2016 	int ret;
2017 	struct rte_port *port;
2018 	struct port_flow_tunnel *pft;
2019 	struct rte_flow_error error;
2020 
2021 	port = &ports[port_id];
2022 	pft = port_flow_locate_tunnel_id(port, tunnel_ops->id);
2023 	if (!pft) {
2024 		fprintf(stderr, "failed to locate port flow tunnel #%u\n",
2025 			tunnel_ops->id);
2026 		return NULL;
2027 	}
2028 	if (tunnel_ops->actions) {
2029 		uint32_t num_actions;
2030 		const struct rte_flow_action *aptr;
2031 
2032 		ret = rte_flow_tunnel_decap_set(port_id, &pft->tunnel,
2033 						&pft->pmd_actions,
2034 						&pft->num_pmd_actions,
2035 						&error);
2036 		if (ret) {
2037 			port_flow_complain(&error);
2038 			return NULL;
2039 		}
2040 		for (aptr = actions, num_actions = 1;
2041 		     aptr->type != RTE_FLOW_ACTION_TYPE_END;
2042 		     aptr++, num_actions++);
2043 		pft->actions = malloc(
2044 				(num_actions +  pft->num_pmd_actions) *
2045 				sizeof(actions[0]));
2046 		if (!pft->actions) {
2047 			rte_flow_tunnel_action_decap_release(
2048 					port_id, pft->actions,
2049 					pft->num_pmd_actions, &error);
2050 			return NULL;
2051 		}
2052 		rte_memcpy(pft->actions, pft->pmd_actions,
2053 			   pft->num_pmd_actions * sizeof(actions[0]));
2054 		rte_memcpy(pft->actions + pft->num_pmd_actions, actions,
2055 			   num_actions * sizeof(actions[0]));
2056 	}
2057 	if (tunnel_ops->items) {
2058 		uint32_t num_items;
2059 		const struct rte_flow_item *iptr;
2060 
2061 		ret = rte_flow_tunnel_match(port_id, &pft->tunnel,
2062 					    &pft->pmd_items,
2063 					    &pft->num_pmd_items,
2064 					    &error);
2065 		if (ret) {
2066 			port_flow_complain(&error);
2067 			return NULL;
2068 		}
2069 		for (iptr = pattern, num_items = 1;
2070 		     iptr->type != RTE_FLOW_ITEM_TYPE_END;
2071 		     iptr++, num_items++);
2072 		pft->items = malloc((num_items + pft->num_pmd_items) *
2073 				    sizeof(pattern[0]));
2074 		if (!pft->items) {
2075 			rte_flow_tunnel_item_release(
2076 					port_id, pft->pmd_items,
2077 					pft->num_pmd_items, &error);
2078 			return NULL;
2079 		}
2080 		rte_memcpy(pft->items, pft->pmd_items,
2081 			   pft->num_pmd_items * sizeof(pattern[0]));
2082 		rte_memcpy(pft->items + pft->num_pmd_items, pattern,
2083 			   num_items * sizeof(pattern[0]));
2084 	}
2085 
2086 	return pft;
2087 }
2088 
2089 static void
2090 port_flow_tunnel_offload_cmd_release(portid_t port_id,
2091 				     const struct tunnel_ops *tunnel_ops,
2092 				     struct port_flow_tunnel *pft)
2093 {
2094 	struct rte_flow_error error;
2095 
2096 	if (tunnel_ops->actions) {
2097 		free(pft->actions);
2098 		rte_flow_tunnel_action_decap_release(
2099 			port_id, pft->pmd_actions,
2100 			pft->num_pmd_actions, &error);
2101 		pft->actions = NULL;
2102 		pft->pmd_actions = NULL;
2103 	}
2104 	if (tunnel_ops->items) {
2105 		free(pft->items);
2106 		rte_flow_tunnel_item_release(port_id, pft->pmd_items,
2107 					     pft->num_pmd_items,
2108 					     &error);
2109 		pft->items = NULL;
2110 		pft->pmd_items = NULL;
2111 	}
2112 }
2113 
2114 /** Add port meter policy */
2115 int
2116 port_meter_policy_add(portid_t port_id, uint32_t policy_id,
2117 			const struct rte_flow_action *actions)
2118 {
2119 	struct rte_mtr_error error;
2120 	const struct rte_flow_action *act = actions;
2121 	const struct rte_flow_action *start;
2122 	struct rte_mtr_meter_policy_params policy;
2123 	uint32_t i = 0, act_n;
2124 	int ret;
2125 
2126 	for (i = 0; i < RTE_COLORS; i++) {
2127 		for (act_n = 0, start = act;
2128 			act->type != RTE_FLOW_ACTION_TYPE_END; act++)
2129 			act_n++;
2130 		if (act_n && act->type == RTE_FLOW_ACTION_TYPE_END)
2131 			policy.actions[i] = start;
2132 		else
2133 			policy.actions[i] = NULL;
2134 		act++;
2135 	}
2136 	ret = rte_mtr_meter_policy_add(port_id,
2137 			policy_id,
2138 			&policy, &error);
2139 	if (ret)
2140 		print_mtr_err_msg(&error);
2141 	return ret;
2142 }
2143 
2144 struct rte_flow_meter_profile *
2145 port_meter_profile_get_by_id(portid_t port_id, uint32_t id)
2146 {
2147 	struct rte_mtr_error error;
2148 	struct rte_flow_meter_profile *profile;
2149 
2150 	profile = rte_mtr_meter_profile_get(port_id, id, &error);
2151 	if (!profile)
2152 		print_mtr_err_msg(&error);
2153 	return profile;
2154 }
2155 struct rte_flow_meter_policy *
2156 port_meter_policy_get_by_id(portid_t port_id, uint32_t id)
2157 {
2158 	struct rte_mtr_error error;
2159 	struct rte_flow_meter_policy *policy;
2160 
2161 	policy = rte_mtr_meter_policy_get(port_id, id, &error);
2162 	if (!policy)
2163 		print_mtr_err_msg(&error);
2164 	return policy;
2165 }
2166 
2167 /** Validate flow rule. */
2168 int
2169 port_flow_validate(portid_t port_id,
2170 		   const struct rte_flow_attr *attr,
2171 		   const struct rte_flow_item *pattern,
2172 		   const struct rte_flow_action *actions,
2173 		   const struct tunnel_ops *tunnel_ops)
2174 {
2175 	struct rte_flow_error error;
2176 	struct port_flow_tunnel *pft = NULL;
2177 	int ret;
2178 
2179 	/* Poisoning to make sure PMDs update it in case of error. */
2180 	memset(&error, 0x11, sizeof(error));
2181 	if (tunnel_ops->enabled) {
2182 		pft = port_flow_tunnel_offload_cmd_prep(port_id, pattern,
2183 							actions, tunnel_ops);
2184 		if (!pft)
2185 			return -ENOENT;
2186 		if (pft->items)
2187 			pattern = pft->items;
2188 		if (pft->actions)
2189 			actions = pft->actions;
2190 	}
2191 	ret = rte_flow_validate(port_id, attr, pattern, actions, &error);
2192 	if (tunnel_ops->enabled)
2193 		port_flow_tunnel_offload_cmd_release(port_id, tunnel_ops, pft);
2194 	if (ret)
2195 		return port_flow_complain(&error);
2196 	printf("Flow rule validated\n");
2197 	return 0;
2198 }
2199 
2200 /** Return age action structure if exists, otherwise NULL. */
2201 static struct rte_flow_action_age *
2202 age_action_get(const struct rte_flow_action *actions)
2203 {
2204 	for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
2205 		switch (actions->type) {
2206 		case RTE_FLOW_ACTION_TYPE_AGE:
2207 			return (struct rte_flow_action_age *)
2208 				(uintptr_t)actions->conf;
2209 		default:
2210 			break;
2211 		}
2212 	}
2213 	return NULL;
2214 }
2215 
2216 /** Create pattern template */
2217 int
2218 port_flow_pattern_template_create(portid_t port_id, uint32_t id,
2219 				  const struct rte_flow_pattern_template_attr *attr,
2220 				  const struct rte_flow_item *pattern)
2221 {
2222 	struct rte_port *port;
2223 	struct port_template *pit;
2224 	int ret;
2225 	struct rte_flow_error error;
2226 
2227 	if (port_id_is_invalid(port_id, ENABLED_WARN) ||
2228 	    port_id == (portid_t)RTE_PORT_ALL)
2229 		return -EINVAL;
2230 	port = &ports[port_id];
2231 	ret = template_alloc(id, &pit, &port->pattern_templ_list);
2232 	if (ret)
2233 		return ret;
2234 	/* Poisoning to make sure PMDs update it in case of error. */
2235 	memset(&error, 0x22, sizeof(error));
2236 	pit->template.pattern_template = rte_flow_pattern_template_create(port_id,
2237 						attr, pattern, &error);
2238 	if (!pit->template.pattern_template) {
2239 		uint32_t destroy_id = pit->id;
2240 		port_flow_pattern_template_destroy(port_id, 1, &destroy_id);
2241 		return port_flow_complain(&error);
2242 	}
2243 	printf("Pattern template #%u created\n", pit->id);
2244 	return 0;
2245 }
2246 
2247 /** Destroy pattern template */
2248 int
2249 port_flow_pattern_template_destroy(portid_t port_id, uint32_t n,
2250 				   const uint32_t *template)
2251 {
2252 	struct rte_port *port;
2253 	struct port_template **tmp;
2254 	uint32_t c = 0;
2255 	int ret = 0;
2256 
2257 	if (port_id_is_invalid(port_id, ENABLED_WARN) ||
2258 	    port_id == (portid_t)RTE_PORT_ALL)
2259 		return -EINVAL;
2260 	port = &ports[port_id];
2261 	tmp = &port->pattern_templ_list;
2262 	while (*tmp) {
2263 		uint32_t i;
2264 
2265 		for (i = 0; i != n; ++i) {
2266 			struct rte_flow_error error;
2267 			struct port_template *pit = *tmp;
2268 
2269 			if (template[i] != pit->id)
2270 				continue;
2271 			/*
2272 			 * Poisoning to make sure PMDs update it in case
2273 			 * of error.
2274 			 */
2275 			memset(&error, 0x33, sizeof(error));
2276 
2277 			if (pit->template.pattern_template &&
2278 			    rte_flow_pattern_template_destroy(port_id,
2279 							   pit->template.pattern_template,
2280 							   &error)) {
2281 				ret = port_flow_complain(&error);
2282 				continue;
2283 			}
2284 			*tmp = pit->next;
2285 			printf("Pattern template #%u destroyed\n", pit->id);
2286 			free(pit);
2287 			break;
2288 		}
2289 		if (i == n)
2290 			tmp = &(*tmp)->next;
2291 		++c;
2292 	}
2293 	return ret;
2294 }
2295 
2296 /** Create actions template */
2297 int
2298 port_flow_actions_template_create(portid_t port_id, uint32_t id,
2299 				  const struct rte_flow_actions_template_attr *attr,
2300 				  const struct rte_flow_action *actions,
2301 				  const struct rte_flow_action *masks)
2302 {
2303 	struct rte_port *port;
2304 	struct port_template *pat;
2305 	int ret;
2306 	struct rte_flow_error error;
2307 
2308 	if (port_id_is_invalid(port_id, ENABLED_WARN) ||
2309 	    port_id == (portid_t)RTE_PORT_ALL)
2310 		return -EINVAL;
2311 	port = &ports[port_id];
2312 	ret = template_alloc(id, &pat, &port->actions_templ_list);
2313 	if (ret)
2314 		return ret;
2315 	/* Poisoning to make sure PMDs update it in case of error. */
2316 	memset(&error, 0x22, sizeof(error));
2317 	pat->template.actions_template = rte_flow_actions_template_create(port_id,
2318 						attr, actions, masks, &error);
2319 	if (!pat->template.actions_template) {
2320 		uint32_t destroy_id = pat->id;
2321 		port_flow_actions_template_destroy(port_id, 1, &destroy_id);
2322 		return port_flow_complain(&error);
2323 	}
2324 	printf("Actions template #%u created\n", pat->id);
2325 	return 0;
2326 }
2327 
2328 /** Destroy actions template */
2329 int
2330 port_flow_actions_template_destroy(portid_t port_id, uint32_t n,
2331 				   const uint32_t *template)
2332 {
2333 	struct rte_port *port;
2334 	struct port_template **tmp;
2335 	uint32_t c = 0;
2336 	int ret = 0;
2337 
2338 	if (port_id_is_invalid(port_id, ENABLED_WARN) ||
2339 	    port_id == (portid_t)RTE_PORT_ALL)
2340 		return -EINVAL;
2341 	port = &ports[port_id];
2342 	tmp = &port->actions_templ_list;
2343 	while (*tmp) {
2344 		uint32_t i;
2345 
2346 		for (i = 0; i != n; ++i) {
2347 			struct rte_flow_error error;
2348 			struct port_template *pat = *tmp;
2349 
2350 			if (template[i] != pat->id)
2351 				continue;
2352 			/*
2353 			 * Poisoning to make sure PMDs update it in case
2354 			 * of error.
2355 			 */
2356 			memset(&error, 0x33, sizeof(error));
2357 
2358 			if (pat->template.actions_template &&
2359 			    rte_flow_actions_template_destroy(port_id,
2360 					pat->template.actions_template, &error)) {
2361 				ret = port_flow_complain(&error);
2362 				continue;
2363 			}
2364 			*tmp = pat->next;
2365 			printf("Actions template #%u destroyed\n", pat->id);
2366 			free(pat);
2367 			break;
2368 		}
2369 		if (i == n)
2370 			tmp = &(*tmp)->next;
2371 		++c;
2372 	}
2373 	return ret;
2374 }
2375 
2376 /** Create table */
2377 int
2378 port_flow_template_table_create(portid_t port_id, uint32_t id,
2379 		const struct rte_flow_template_table_attr *table_attr,
2380 		uint32_t nb_pattern_templates, uint32_t *pattern_templates,
2381 		uint32_t nb_actions_templates, uint32_t *actions_templates)
2382 {
2383 	struct rte_port *port;
2384 	struct port_table *pt;
2385 	struct port_template *temp = NULL;
2386 	int ret;
2387 	uint32_t i;
2388 	struct rte_flow_error error;
2389 	struct rte_flow_pattern_template
2390 			*flow_pattern_templates[nb_pattern_templates];
2391 	struct rte_flow_actions_template
2392 			*flow_actions_templates[nb_actions_templates];
2393 
2394 	if (port_id_is_invalid(port_id, ENABLED_WARN) ||
2395 	    port_id == (portid_t)RTE_PORT_ALL)
2396 		return -EINVAL;
2397 	port = &ports[port_id];
2398 	for (i = 0; i < nb_pattern_templates; ++i) {
2399 		bool found = false;
2400 		temp = port->pattern_templ_list;
2401 		while (temp) {
2402 			if (pattern_templates[i] == temp->id) {
2403 				flow_pattern_templates[i] =
2404 					temp->template.pattern_template;
2405 				found = true;
2406 				break;
2407 			}
2408 			temp = temp->next;
2409 		}
2410 		if (!found) {
2411 			printf("Pattern template #%u is invalid\n",
2412 			       pattern_templates[i]);
2413 			return -EINVAL;
2414 		}
2415 	}
2416 	for (i = 0; i < nb_actions_templates; ++i) {
2417 		bool found = false;
2418 		temp = port->actions_templ_list;
2419 		while (temp) {
2420 			if (actions_templates[i] == temp->id) {
2421 				flow_actions_templates[i] =
2422 					temp->template.actions_template;
2423 				found = true;
2424 				break;
2425 			}
2426 			temp = temp->next;
2427 		}
2428 		if (!found) {
2429 			printf("Actions template #%u is invalid\n",
2430 			       actions_templates[i]);
2431 			return -EINVAL;
2432 		}
2433 	}
2434 	ret = table_alloc(id, &pt, &port->table_list);
2435 	if (ret)
2436 		return ret;
2437 	/* Poisoning to make sure PMDs update it in case of error. */
2438 	memset(&error, 0x22, sizeof(error));
2439 	pt->table = rte_flow_template_table_create(port_id, table_attr,
2440 		      flow_pattern_templates, nb_pattern_templates,
2441 		      flow_actions_templates, nb_actions_templates,
2442 		      &error);
2443 
2444 	if (!pt->table) {
2445 		uint32_t destroy_id = pt->id;
2446 		port_flow_template_table_destroy(port_id, 1, &destroy_id);
2447 		return port_flow_complain(&error);
2448 	}
2449 	pt->nb_pattern_templates = nb_pattern_templates;
2450 	pt->nb_actions_templates = nb_actions_templates;
2451 	printf("Template table #%u created\n", pt->id);
2452 	return 0;
2453 }
2454 
2455 /** Destroy table */
2456 int
2457 port_flow_template_table_destroy(portid_t port_id,
2458 				 uint32_t n, const uint32_t *table)
2459 {
2460 	struct rte_port *port;
2461 	struct port_table **tmp;
2462 	uint32_t c = 0;
2463 	int ret = 0;
2464 
2465 	if (port_id_is_invalid(port_id, ENABLED_WARN) ||
2466 	    port_id == (portid_t)RTE_PORT_ALL)
2467 		return -EINVAL;
2468 	port = &ports[port_id];
2469 	tmp = &port->table_list;
2470 	while (*tmp) {
2471 		uint32_t i;
2472 
2473 		for (i = 0; i != n; ++i) {
2474 			struct rte_flow_error error;
2475 			struct port_table *pt = *tmp;
2476 
2477 			if (table[i] != pt->id)
2478 				continue;
2479 			/*
2480 			 * Poisoning to make sure PMDs update it in case
2481 			 * of error.
2482 			 */
2483 			memset(&error, 0x33, sizeof(error));
2484 
2485 			if (pt->table &&
2486 			    rte_flow_template_table_destroy(port_id,
2487 							    pt->table,
2488 							    &error)) {
2489 				ret = port_flow_complain(&error);
2490 				continue;
2491 			}
2492 			*tmp = pt->next;
2493 			printf("Template table #%u destroyed\n", pt->id);
2494 			free(pt);
2495 			break;
2496 		}
2497 		if (i == n)
2498 			tmp = &(*tmp)->next;
2499 		++c;
2500 	}
2501 	return ret;
2502 }
2503 
2504 /** Enqueue create flow rule operation. */
2505 int
2506 port_queue_flow_create(portid_t port_id, queueid_t queue_id,
2507 		       bool postpone, uint32_t table_id,
2508 		       uint32_t pattern_idx, uint32_t actions_idx,
2509 		       const struct rte_flow_item *pattern,
2510 		       const struct rte_flow_action *actions)
2511 {
2512 	struct rte_flow_op_attr op_attr = { .postpone = postpone };
2513 	struct rte_flow_attr flow_attr = { 0 };
2514 	struct rte_flow *flow;
2515 	struct rte_port *port;
2516 	struct port_flow *pf;
2517 	struct port_table *pt;
2518 	uint32_t id = 0;
2519 	bool found;
2520 	struct rte_flow_error error = { RTE_FLOW_ERROR_TYPE_NONE, NULL, NULL };
2521 	struct rte_flow_action_age *age = age_action_get(actions);
2522 	struct queue_job *job;
2523 
2524 	port = &ports[port_id];
2525 	if (port->flow_list) {
2526 		if (port->flow_list->id == UINT32_MAX) {
2527 			printf("Highest rule ID is already assigned,"
2528 			       " delete it first");
2529 			return -ENOMEM;
2530 		}
2531 		id = port->flow_list->id + 1;
2532 	}
2533 
2534 	if (queue_id >= port->queue_nb) {
2535 		printf("Queue #%u is invalid\n", queue_id);
2536 		return -EINVAL;
2537 	}
2538 
2539 	found = false;
2540 	pt = port->table_list;
2541 	while (pt) {
2542 		if (table_id == pt->id) {
2543 			found = true;
2544 			break;
2545 		}
2546 		pt = pt->next;
2547 	}
2548 	if (!found) {
2549 		printf("Table #%u is invalid\n", table_id);
2550 		return -EINVAL;
2551 	}
2552 
2553 	if (pattern_idx >= pt->nb_pattern_templates) {
2554 		printf("Pattern template index #%u is invalid,"
2555 		       " %u templates present in the table\n",
2556 		       pattern_idx, pt->nb_pattern_templates);
2557 		return -EINVAL;
2558 	}
2559 	if (actions_idx >= pt->nb_actions_templates) {
2560 		printf("Actions template index #%u is invalid,"
2561 		       " %u templates present in the table\n",
2562 		       actions_idx, pt->nb_actions_templates);
2563 		return -EINVAL;
2564 	}
2565 
2566 	job = calloc(1, sizeof(*job));
2567 	if (!job) {
2568 		printf("Queue flow create job allocate failed\n");
2569 		return -ENOMEM;
2570 	}
2571 	job->type = QUEUE_JOB_TYPE_FLOW_CREATE;
2572 
2573 	pf = port_flow_new(&flow_attr, pattern, actions, &error);
2574 	if (!pf) {
2575 		free(job);
2576 		return port_flow_complain(&error);
2577 	}
2578 	if (age) {
2579 		pf->age_type = ACTION_AGE_CONTEXT_TYPE_FLOW;
2580 		age->context = &pf->age_type;
2581 	}
2582 	/* Poisoning to make sure PMDs update it in case of error. */
2583 	memset(&error, 0x11, sizeof(error));
2584 	flow = rte_flow_async_create(port_id, queue_id, &op_attr, pt->table,
2585 		pattern, pattern_idx, actions, actions_idx, job, &error);
2586 	if (!flow) {
2587 		uint32_t flow_id = pf->id;
2588 		port_queue_flow_destroy(port_id, queue_id, true, 1, &flow_id);
2589 		free(job);
2590 		return port_flow_complain(&error);
2591 	}
2592 
2593 	pf->next = port->flow_list;
2594 	pf->id = id;
2595 	pf->flow = flow;
2596 	job->pf = pf;
2597 	port->flow_list = pf;
2598 	printf("Flow rule #%u creation enqueued\n", pf->id);
2599 	return 0;
2600 }
2601 
2602 /** Enqueue number of destroy flow rules operations. */
2603 int
2604 port_queue_flow_destroy(portid_t port_id, queueid_t queue_id,
2605 			bool postpone, uint32_t n, const uint32_t *rule)
2606 {
2607 	struct rte_flow_op_attr op_attr = { .postpone = postpone };
2608 	struct rte_port *port;
2609 	struct port_flow **tmp;
2610 	uint32_t c = 0;
2611 	int ret = 0;
2612 	struct queue_job *job;
2613 
2614 	if (port_id_is_invalid(port_id, ENABLED_WARN) ||
2615 	    port_id == (portid_t)RTE_PORT_ALL)
2616 		return -EINVAL;
2617 	port = &ports[port_id];
2618 
2619 	if (queue_id >= port->queue_nb) {
2620 		printf("Queue #%u is invalid\n", queue_id);
2621 		return -EINVAL;
2622 	}
2623 
2624 	tmp = &port->flow_list;
2625 	while (*tmp) {
2626 		uint32_t i;
2627 
2628 		for (i = 0; i != n; ++i) {
2629 			struct rte_flow_error error;
2630 			struct port_flow *pf = *tmp;
2631 
2632 			if (rule[i] != pf->id)
2633 				continue;
2634 			/*
2635 			 * Poisoning to make sure PMD
2636 			 * update it in case of error.
2637 			 */
2638 			memset(&error, 0x33, sizeof(error));
2639 			job = calloc(1, sizeof(*job));
2640 			if (!job) {
2641 				printf("Queue flow destroy job allocate failed\n");
2642 				return -ENOMEM;
2643 			}
2644 			job->type = QUEUE_JOB_TYPE_FLOW_DESTROY;
2645 			job->pf = pf;
2646 
2647 			if (rte_flow_async_destroy(port_id, queue_id, &op_attr,
2648 						   pf->flow, job, &error)) {
2649 				free(job);
2650 				ret = port_flow_complain(&error);
2651 				continue;
2652 			}
2653 			printf("Flow rule #%u destruction enqueued\n", pf->id);
2654 			*tmp = pf->next;
2655 			break;
2656 		}
2657 		if (i == n)
2658 			tmp = &(*tmp)->next;
2659 		++c;
2660 	}
2661 	return ret;
2662 }
2663 
2664 /** Enqueue indirect action create operation. */
2665 int
2666 port_queue_action_handle_create(portid_t port_id, uint32_t queue_id,
2667 				bool postpone, uint32_t id,
2668 				const struct rte_flow_indir_action_conf *conf,
2669 				const struct rte_flow_action *action)
2670 {
2671 	const struct rte_flow_op_attr attr = { .postpone = postpone};
2672 	struct rte_port *port;
2673 	struct port_indirect_action *pia;
2674 	int ret;
2675 	struct rte_flow_error error;
2676 	struct queue_job *job;
2677 
2678 	ret = action_alloc(port_id, id, &pia);
2679 	if (ret)
2680 		return ret;
2681 
2682 	port = &ports[port_id];
2683 	if (queue_id >= port->queue_nb) {
2684 		printf("Queue #%u is invalid\n", queue_id);
2685 		return -EINVAL;
2686 	}
2687 	job = calloc(1, sizeof(*job));
2688 	if (!job) {
2689 		printf("Queue action create job allocate failed\n");
2690 		return -ENOMEM;
2691 	}
2692 	job->type = QUEUE_JOB_TYPE_ACTION_CREATE;
2693 	job->pia = pia;
2694 
2695 	if (action->type == RTE_FLOW_ACTION_TYPE_AGE) {
2696 		struct rte_flow_action_age *age =
2697 			(struct rte_flow_action_age *)(uintptr_t)(action->conf);
2698 
2699 		pia->age_type = ACTION_AGE_CONTEXT_TYPE_INDIRECT_ACTION;
2700 		age->context = &pia->age_type;
2701 	}
2702 	/* Poisoning to make sure PMDs update it in case of error. */
2703 	memset(&error, 0x88, sizeof(error));
2704 	pia->handle = rte_flow_async_action_handle_create(port_id, queue_id,
2705 					&attr, conf, action, job, &error);
2706 	if (!pia->handle) {
2707 		uint32_t destroy_id = pia->id;
2708 		port_queue_action_handle_destroy(port_id, queue_id,
2709 						 postpone, 1, &destroy_id);
2710 		free(job);
2711 		return port_flow_complain(&error);
2712 	}
2713 	pia->type = action->type;
2714 	printf("Indirect action #%u creation queued\n", pia->id);
2715 	return 0;
2716 }
2717 
2718 /** Enqueue indirect action destroy operation. */
2719 int
2720 port_queue_action_handle_destroy(portid_t port_id,
2721 				 uint32_t queue_id, bool postpone,
2722 				 uint32_t n, const uint32_t *actions)
2723 {
2724 	const struct rte_flow_op_attr attr = { .postpone = postpone};
2725 	struct rte_port *port;
2726 	struct port_indirect_action **tmp;
2727 	uint32_t c = 0;
2728 	int ret = 0;
2729 	struct queue_job *job;
2730 
2731 	if (port_id_is_invalid(port_id, ENABLED_WARN) ||
2732 	    port_id == (portid_t)RTE_PORT_ALL)
2733 		return -EINVAL;
2734 	port = &ports[port_id];
2735 
2736 	if (queue_id >= port->queue_nb) {
2737 		printf("Queue #%u is invalid\n", queue_id);
2738 		return -EINVAL;
2739 	}
2740 
2741 	tmp = &port->actions_list;
2742 	while (*tmp) {
2743 		uint32_t i;
2744 
2745 		for (i = 0; i != n; ++i) {
2746 			struct rte_flow_error error;
2747 			struct port_indirect_action *pia = *tmp;
2748 
2749 			if (actions[i] != pia->id)
2750 				continue;
2751 			/*
2752 			 * Poisoning to make sure PMDs update it in case
2753 			 * of error.
2754 			 */
2755 			memset(&error, 0x99, sizeof(error));
2756 			job = calloc(1, sizeof(*job));
2757 			if (!job) {
2758 				printf("Queue action destroy job allocate failed\n");
2759 				return -ENOMEM;
2760 			}
2761 			job->type = QUEUE_JOB_TYPE_ACTION_DESTROY;
2762 			job->pia = pia;
2763 
2764 			if (pia->handle &&
2765 			    rte_flow_async_action_handle_destroy(port_id,
2766 				queue_id, &attr, pia->handle, job, &error)) {
2767 				ret = port_flow_complain(&error);
2768 				continue;
2769 			}
2770 			*tmp = pia->next;
2771 			printf("Indirect action #%u destruction queued\n",
2772 			       pia->id);
2773 			break;
2774 		}
2775 		if (i == n)
2776 			tmp = &(*tmp)->next;
2777 		++c;
2778 	}
2779 	return ret;
2780 }
2781 
2782 /** Enqueue indirect action update operation. */
2783 int
2784 port_queue_action_handle_update(portid_t port_id,
2785 				uint32_t queue_id, bool postpone, uint32_t id,
2786 				const struct rte_flow_action *action)
2787 {
2788 	const struct rte_flow_op_attr attr = { .postpone = postpone};
2789 	struct rte_port *port;
2790 	struct rte_flow_error error;
2791 	struct rte_flow_action_handle *action_handle;
2792 	struct queue_job *job;
2793 	struct port_indirect_action *pia;
2794 	struct rte_flow_update_meter_mark mtr_update;
2795 	const void *update;
2796 
2797 	action_handle = port_action_handle_get_by_id(port_id, id);
2798 	if (!action_handle)
2799 		return -EINVAL;
2800 
2801 	port = &ports[port_id];
2802 	if (queue_id >= port->queue_nb) {
2803 		printf("Queue #%u is invalid\n", queue_id);
2804 		return -EINVAL;
2805 	}
2806 
2807 	job = calloc(1, sizeof(*job));
2808 	if (!job) {
2809 		printf("Queue action update job allocate failed\n");
2810 		return -ENOMEM;
2811 	}
2812 	job->type = QUEUE_JOB_TYPE_ACTION_UPDATE;
2813 
2814 	pia = action_get_by_id(port_id, id);
2815 	if (!pia) {
2816 		free(job);
2817 		return -EINVAL;
2818 	}
2819 
2820 	switch (pia->type) {
2821 	case RTE_FLOW_ACTION_TYPE_AGE:
2822 		update = action->conf;
2823 		break;
2824 	case RTE_FLOW_ACTION_TYPE_METER_MARK:
2825 		rte_memcpy(&mtr_update.meter_mark, action->conf,
2826 			sizeof(struct rte_flow_action_meter_mark));
2827 		mtr_update.profile_valid = 1;
2828 		mtr_update.policy_valid = 1;
2829 		mtr_update.color_mode_valid = 1;
2830 		mtr_update.init_color_valid = 1;
2831 		mtr_update.state_valid = 1;
2832 		update = &mtr_update;
2833 		break;
2834 	default:
2835 		update = action;
2836 		break;
2837 	}
2838 
2839 	if (rte_flow_async_action_handle_update(port_id, queue_id, &attr,
2840 				    action_handle, update, job, &error)) {
2841 		free(job);
2842 		return port_flow_complain(&error);
2843 	}
2844 	printf("Indirect action #%u update queued\n", id);
2845 	return 0;
2846 }
2847 
2848 /** Enqueue indirect action query operation. */
2849 int
2850 port_queue_action_handle_query(portid_t port_id,
2851 			       uint32_t queue_id, bool postpone, uint32_t id)
2852 {
2853 	const struct rte_flow_op_attr attr = { .postpone = postpone};
2854 	struct rte_port *port;
2855 	struct rte_flow_error error;
2856 	struct rte_flow_action_handle *action_handle;
2857 	struct port_indirect_action *pia;
2858 	struct queue_job *job;
2859 
2860 	pia = action_get_by_id(port_id, id);
2861 	action_handle = pia ? pia->handle : NULL;
2862 	if (!action_handle)
2863 		return -EINVAL;
2864 
2865 	port = &ports[port_id];
2866 	if (queue_id >= port->queue_nb) {
2867 		printf("Queue #%u is invalid\n", queue_id);
2868 		return -EINVAL;
2869 	}
2870 
2871 	job = calloc(1, sizeof(*job));
2872 	if (!job) {
2873 		printf("Queue action update job allocate failed\n");
2874 		return -ENOMEM;
2875 	}
2876 	job->type = QUEUE_JOB_TYPE_ACTION_QUERY;
2877 	job->pia = pia;
2878 
2879 	if (rte_flow_async_action_handle_query(port_id, queue_id, &attr,
2880 				    action_handle, &job->query, job, &error)) {
2881 		free(job);
2882 		return port_flow_complain(&error);
2883 	}
2884 	printf("Indirect action #%u update queued\n", id);
2885 	return 0;
2886 }
2887 
2888 /** Push all the queue operations in the queue to the NIC. */
2889 int
2890 port_queue_flow_push(portid_t port_id, queueid_t queue_id)
2891 {
2892 	struct rte_port *port;
2893 	struct rte_flow_error error;
2894 	int ret = 0;
2895 
2896 	if (port_id_is_invalid(port_id, ENABLED_WARN) ||
2897 	    port_id == (portid_t)RTE_PORT_ALL)
2898 		return -EINVAL;
2899 	port = &ports[port_id];
2900 
2901 	if (queue_id >= port->queue_nb) {
2902 		printf("Queue #%u is invalid\n", queue_id);
2903 		return -EINVAL;
2904 	}
2905 
2906 	memset(&error, 0x55, sizeof(error));
2907 	ret = rte_flow_push(port_id, queue_id, &error);
2908 	if (ret < 0) {
2909 		printf("Failed to push operations in the queue\n");
2910 		return -EINVAL;
2911 	}
2912 	printf("Queue #%u operations pushed\n", queue_id);
2913 	return ret;
2914 }
2915 
2916 /** Pull queue operation results from the queue. */
2917 static int
2918 port_queue_aged_flow_destroy(portid_t port_id, queueid_t queue_id,
2919 			     const uint32_t *rule, int nb_flows)
2920 {
2921 	struct rte_port *port = &ports[port_id];
2922 	struct rte_flow_op_result *res;
2923 	struct rte_flow_error error;
2924 	uint32_t n = nb_flows;
2925 	int ret = 0;
2926 	int i;
2927 
2928 	res = calloc(port->queue_sz, sizeof(struct rte_flow_op_result));
2929 	if (!res) {
2930 		printf("Failed to allocate memory for pulled results\n");
2931 		return -ENOMEM;
2932 	}
2933 
2934 	memset(&error, 0x66, sizeof(error));
2935 	while (nb_flows > 0) {
2936 		int success = 0;
2937 
2938 		if (n > port->queue_sz)
2939 			n = port->queue_sz;
2940 		ret = port_queue_flow_destroy(port_id, queue_id, true, n, rule);
2941 		if (ret < 0) {
2942 			free(res);
2943 			return ret;
2944 		}
2945 		ret = rte_flow_push(port_id, queue_id, &error);
2946 		if (ret < 0) {
2947 			printf("Failed to push operations in the queue: %s\n",
2948 			       strerror(-ret));
2949 			free(res);
2950 			return ret;
2951 		}
2952 		while (success < nb_flows) {
2953 			ret = rte_flow_pull(port_id, queue_id, res,
2954 					    port->queue_sz, &error);
2955 			if (ret < 0) {
2956 				printf("Failed to pull a operation results: %s\n",
2957 				       strerror(-ret));
2958 				free(res);
2959 				return ret;
2960 			}
2961 
2962 			for (i = 0; i < ret; i++) {
2963 				if (res[i].status == RTE_FLOW_OP_SUCCESS)
2964 					success++;
2965 			}
2966 		}
2967 		rule += n;
2968 		nb_flows -= n;
2969 		n = nb_flows;
2970 	}
2971 
2972 	free(res);
2973 	return ret;
2974 }
2975 
2976 /** List simply and destroy all aged flows per queue. */
2977 void
2978 port_queue_flow_aged(portid_t port_id, uint32_t queue_id, uint8_t destroy)
2979 {
2980 	void **contexts;
2981 	int nb_context, total = 0, idx;
2982 	uint32_t *rules = NULL;
2983 	struct rte_port *port;
2984 	struct rte_flow_error error;
2985 	enum age_action_context_type *type;
2986 	union {
2987 		struct port_flow *pf;
2988 		struct port_indirect_action *pia;
2989 	} ctx;
2990 
2991 	if (port_id_is_invalid(port_id, ENABLED_WARN) ||
2992 	    port_id == (portid_t)RTE_PORT_ALL)
2993 		return;
2994 	port = &ports[port_id];
2995 	if (queue_id >= port->queue_nb) {
2996 		printf("Error: queue #%u is invalid\n", queue_id);
2997 		return;
2998 	}
2999 	total = rte_flow_get_q_aged_flows(port_id, queue_id, NULL, 0, &error);
3000 	if (total < 0) {
3001 		port_flow_complain(&error);
3002 		return;
3003 	}
3004 	printf("Port %u queue %u total aged flows: %d\n",
3005 	       port_id, queue_id, total);
3006 	if (total == 0)
3007 		return;
3008 	contexts = calloc(total, sizeof(void *));
3009 	if (contexts == NULL) {
3010 		printf("Cannot allocate contexts for aged flow\n");
3011 		return;
3012 	}
3013 	printf("%-20s\tID\tGroup\tPrio\tAttr\n", "Type");
3014 	nb_context = rte_flow_get_q_aged_flows(port_id, queue_id, contexts,
3015 					       total, &error);
3016 	if (nb_context > total) {
3017 		printf("Port %u queue %u get aged flows count(%d) > total(%d)\n",
3018 		       port_id, queue_id, nb_context, total);
3019 		free(contexts);
3020 		return;
3021 	}
3022 	if (destroy) {
3023 		rules = malloc(sizeof(uint32_t) * nb_context);
3024 		if (rules == NULL)
3025 			printf("Cannot allocate memory for destroy aged flow\n");
3026 	}
3027 	total = 0;
3028 	for (idx = 0; idx < nb_context; idx++) {
3029 		if (!contexts[idx]) {
3030 			printf("Error: get Null context in port %u queue %u\n",
3031 			       port_id, queue_id);
3032 			continue;
3033 		}
3034 		type = (enum age_action_context_type *)contexts[idx];
3035 		switch (*type) {
3036 		case ACTION_AGE_CONTEXT_TYPE_FLOW:
3037 			ctx.pf = container_of(type, struct port_flow, age_type);
3038 			printf("%-20s\t%" PRIu32 "\t%" PRIu32 "\t%" PRIu32
3039 								 "\t%c%c%c\t\n",
3040 			       "Flow",
3041 			       ctx.pf->id,
3042 			       ctx.pf->rule.attr->group,
3043 			       ctx.pf->rule.attr->priority,
3044 			       ctx.pf->rule.attr->ingress ? 'i' : '-',
3045 			       ctx.pf->rule.attr->egress ? 'e' : '-',
3046 			       ctx.pf->rule.attr->transfer ? 't' : '-');
3047 			if (rules != NULL) {
3048 				rules[total] = ctx.pf->id;
3049 				total++;
3050 			}
3051 			break;
3052 		case ACTION_AGE_CONTEXT_TYPE_INDIRECT_ACTION:
3053 			ctx.pia = container_of(type,
3054 					       struct port_indirect_action,
3055 					       age_type);
3056 			printf("%-20s\t%" PRIu32 "\n", "Indirect action",
3057 			       ctx.pia->id);
3058 			break;
3059 		default:
3060 			printf("Error: invalid context type %u\n", port_id);
3061 			break;
3062 		}
3063 	}
3064 	if (rules != NULL) {
3065 		port_queue_aged_flow_destroy(port_id, queue_id, rules, total);
3066 		free(rules);
3067 	}
3068 	printf("\n%d flows destroyed\n", total);
3069 	free(contexts);
3070 }
3071 
3072 /** Pull queue operation results from the queue. */
3073 int
3074 port_queue_flow_pull(portid_t port_id, queueid_t queue_id)
3075 {
3076 	struct rte_port *port;
3077 	struct rte_flow_op_result *res;
3078 	struct rte_flow_error error;
3079 	int ret = 0;
3080 	int success = 0;
3081 	int i;
3082 	struct queue_job *job;
3083 
3084 	if (port_id_is_invalid(port_id, ENABLED_WARN) ||
3085 	    port_id == (portid_t)RTE_PORT_ALL)
3086 		return -EINVAL;
3087 	port = &ports[port_id];
3088 
3089 	if (queue_id >= port->queue_nb) {
3090 		printf("Queue #%u is invalid\n", queue_id);
3091 		return -EINVAL;
3092 	}
3093 
3094 	res = calloc(port->queue_sz, sizeof(struct rte_flow_op_result));
3095 	if (!res) {
3096 		printf("Failed to allocate memory for pulled results\n");
3097 		return -ENOMEM;
3098 	}
3099 
3100 	memset(&error, 0x66, sizeof(error));
3101 	ret = rte_flow_pull(port_id, queue_id, res,
3102 				 port->queue_sz, &error);
3103 	if (ret < 0) {
3104 		printf("Failed to pull a operation results\n");
3105 		free(res);
3106 		return -EINVAL;
3107 	}
3108 
3109 	for (i = 0; i < ret; i++) {
3110 		if (res[i].status == RTE_FLOW_OP_SUCCESS)
3111 			success++;
3112 		job = (struct queue_job *)res[i].user_data;
3113 		if (job->type == QUEUE_JOB_TYPE_FLOW_DESTROY)
3114 			free(job->pf);
3115 		else if (job->type == QUEUE_JOB_TYPE_ACTION_DESTROY)
3116 			free(job->pia);
3117 		else if (job->type == QUEUE_JOB_TYPE_ACTION_QUERY)
3118 			port_action_handle_query_dump(job->pia->type, &job->query);
3119 		free(job);
3120 	}
3121 	printf("Queue #%u pulled %u operations (%u failed, %u succeeded)\n",
3122 	       queue_id, ret, ret - success, success);
3123 	free(res);
3124 	return ret;
3125 }
3126 
3127 /** Create flow rule. */
3128 int
3129 port_flow_create(portid_t port_id,
3130 		 const struct rte_flow_attr *attr,
3131 		 const struct rte_flow_item *pattern,
3132 		 const struct rte_flow_action *actions,
3133 		 const struct tunnel_ops *tunnel_ops)
3134 {
3135 	struct rte_flow *flow;
3136 	struct rte_port *port;
3137 	struct port_flow *pf;
3138 	uint32_t id = 0;
3139 	struct rte_flow_error error;
3140 	struct port_flow_tunnel *pft = NULL;
3141 	struct rte_flow_action_age *age = age_action_get(actions);
3142 
3143 	port = &ports[port_id];
3144 	if (port->flow_list) {
3145 		if (port->flow_list->id == UINT32_MAX) {
3146 			fprintf(stderr,
3147 				"Highest rule ID is already assigned, delete it first");
3148 			return -ENOMEM;
3149 		}
3150 		id = port->flow_list->id + 1;
3151 	}
3152 	if (tunnel_ops->enabled) {
3153 		pft = port_flow_tunnel_offload_cmd_prep(port_id, pattern,
3154 							actions, tunnel_ops);
3155 		if (!pft)
3156 			return -ENOENT;
3157 		if (pft->items)
3158 			pattern = pft->items;
3159 		if (pft->actions)
3160 			actions = pft->actions;
3161 	}
3162 	pf = port_flow_new(attr, pattern, actions, &error);
3163 	if (!pf)
3164 		return port_flow_complain(&error);
3165 	if (age) {
3166 		pf->age_type = ACTION_AGE_CONTEXT_TYPE_FLOW;
3167 		age->context = &pf->age_type;
3168 	}
3169 	/* Poisoning to make sure PMDs update it in case of error. */
3170 	memset(&error, 0x22, sizeof(error));
3171 	flow = rte_flow_create(port_id, attr, pattern, actions, &error);
3172 	if (!flow) {
3173 		if (tunnel_ops->enabled)
3174 			port_flow_tunnel_offload_cmd_release(port_id,
3175 							     tunnel_ops, pft);
3176 		free(pf);
3177 		return port_flow_complain(&error);
3178 	}
3179 	pf->next = port->flow_list;
3180 	pf->id = id;
3181 	pf->flow = flow;
3182 	port->flow_list = pf;
3183 	if (tunnel_ops->enabled)
3184 		port_flow_tunnel_offload_cmd_release(port_id, tunnel_ops, pft);
3185 	printf("Flow rule #%u created\n", pf->id);
3186 	return 0;
3187 }
3188 
3189 /** Destroy a number of flow rules. */
3190 int
3191 port_flow_destroy(portid_t port_id, uint32_t n, const uint32_t *rule)
3192 {
3193 	struct rte_port *port;
3194 	struct port_flow **tmp;
3195 	uint32_t c = 0;
3196 	int ret = 0;
3197 
3198 	if (port_id_is_invalid(port_id, ENABLED_WARN) ||
3199 	    port_id == (portid_t)RTE_PORT_ALL)
3200 		return -EINVAL;
3201 	port = &ports[port_id];
3202 	tmp = &port->flow_list;
3203 	while (*tmp) {
3204 		uint32_t i;
3205 
3206 		for (i = 0; i != n; ++i) {
3207 			struct rte_flow_error error;
3208 			struct port_flow *pf = *tmp;
3209 
3210 			if (rule[i] != pf->id)
3211 				continue;
3212 			/*
3213 			 * Poisoning to make sure PMDs update it in case
3214 			 * of error.
3215 			 */
3216 			memset(&error, 0x33, sizeof(error));
3217 			if (rte_flow_destroy(port_id, pf->flow, &error)) {
3218 				ret = port_flow_complain(&error);
3219 				continue;
3220 			}
3221 			printf("Flow rule #%u destroyed\n", pf->id);
3222 			*tmp = pf->next;
3223 			free(pf);
3224 			break;
3225 		}
3226 		if (i == n)
3227 			tmp = &(*tmp)->next;
3228 		++c;
3229 	}
3230 	return ret;
3231 }
3232 
3233 /** Remove all flow rules. */
3234 int
3235 port_flow_flush(portid_t port_id)
3236 {
3237 	struct rte_flow_error error;
3238 	struct rte_port *port;
3239 	int ret = 0;
3240 
3241 	if (port_id_is_invalid(port_id, ENABLED_WARN) ||
3242 		port_id == (portid_t)RTE_PORT_ALL)
3243 		return -EINVAL;
3244 
3245 	port = &ports[port_id];
3246 
3247 	if (port->flow_list == NULL)
3248 		return ret;
3249 
3250 	/* Poisoning to make sure PMDs update it in case of error. */
3251 	memset(&error, 0x44, sizeof(error));
3252 	if (rte_flow_flush(port_id, &error)) {
3253 		port_flow_complain(&error);
3254 	}
3255 
3256 	while (port->flow_list) {
3257 		struct port_flow *pf = port->flow_list->next;
3258 
3259 		free(port->flow_list);
3260 		port->flow_list = pf;
3261 	}
3262 	return ret;
3263 }
3264 
3265 /** Dump flow rules. */
3266 int
3267 port_flow_dump(portid_t port_id, bool dump_all, uint32_t rule_id,
3268 		const char *file_name)
3269 {
3270 	int ret = 0;
3271 	FILE *file = stdout;
3272 	struct rte_flow_error error;
3273 	struct rte_port *port;
3274 	struct port_flow *pflow;
3275 	struct rte_flow *tmpFlow = NULL;
3276 	bool found = false;
3277 
3278 	if (port_id_is_invalid(port_id, ENABLED_WARN) ||
3279 		port_id == (portid_t)RTE_PORT_ALL)
3280 		return -EINVAL;
3281 
3282 	if (!dump_all) {
3283 		port = &ports[port_id];
3284 		pflow = port->flow_list;
3285 		while (pflow) {
3286 			if (rule_id != pflow->id) {
3287 				pflow = pflow->next;
3288 			} else {
3289 				tmpFlow = pflow->flow;
3290 				if (tmpFlow)
3291 					found = true;
3292 				break;
3293 			}
3294 		}
3295 		if (found == false) {
3296 			fprintf(stderr, "Failed to dump to flow %d\n", rule_id);
3297 			return -EINVAL;
3298 		}
3299 	}
3300 
3301 	if (file_name && strlen(file_name)) {
3302 		file = fopen(file_name, "w");
3303 		if (!file) {
3304 			fprintf(stderr, "Failed to create file %s: %s\n",
3305 				file_name, strerror(errno));
3306 			return -errno;
3307 		}
3308 	}
3309 
3310 	if (!dump_all)
3311 		ret = rte_flow_dev_dump(port_id, tmpFlow, file, &error);
3312 	else
3313 		ret = rte_flow_dev_dump(port_id, NULL, file, &error);
3314 	if (ret) {
3315 		port_flow_complain(&error);
3316 		fprintf(stderr, "Failed to dump flow: %s\n", strerror(-ret));
3317 	} else
3318 		printf("Flow dump finished\n");
3319 	if (file_name && strlen(file_name))
3320 		fclose(file);
3321 	return ret;
3322 }
3323 
3324 /** Query a flow rule. */
3325 int
3326 port_flow_query(portid_t port_id, uint32_t rule,
3327 		const struct rte_flow_action *action)
3328 {
3329 	struct rte_flow_error error;
3330 	struct rte_port *port;
3331 	struct port_flow *pf;
3332 	const char *name;
3333 	union {
3334 		struct rte_flow_query_count count;
3335 		struct rte_flow_action_rss rss_conf;
3336 		struct rte_flow_query_age age;
3337 	} query;
3338 	int ret;
3339 
3340 	if (port_id_is_invalid(port_id, ENABLED_WARN) ||
3341 	    port_id == (portid_t)RTE_PORT_ALL)
3342 		return -EINVAL;
3343 	port = &ports[port_id];
3344 	for (pf = port->flow_list; pf; pf = pf->next)
3345 		if (pf->id == rule)
3346 			break;
3347 	if (!pf) {
3348 		fprintf(stderr, "Flow rule #%u not found\n", rule);
3349 		return -ENOENT;
3350 	}
3351 	ret = rte_flow_conv(RTE_FLOW_CONV_OP_ACTION_NAME_PTR,
3352 			    &name, sizeof(name),
3353 			    (void *)(uintptr_t)action->type, &error);
3354 	if (ret < 0)
3355 		return port_flow_complain(&error);
3356 	switch (action->type) {
3357 	case RTE_FLOW_ACTION_TYPE_COUNT:
3358 	case RTE_FLOW_ACTION_TYPE_RSS:
3359 	case RTE_FLOW_ACTION_TYPE_AGE:
3360 		break;
3361 	default:
3362 		fprintf(stderr, "Cannot query action type %d (%s)\n",
3363 			action->type, name);
3364 		return -ENOTSUP;
3365 	}
3366 	/* Poisoning to make sure PMDs update it in case of error. */
3367 	memset(&error, 0x55, sizeof(error));
3368 	memset(&query, 0, sizeof(query));
3369 	if (rte_flow_query(port_id, pf->flow, action, &query, &error))
3370 		return port_flow_complain(&error);
3371 	switch (action->type) {
3372 	case RTE_FLOW_ACTION_TYPE_COUNT:
3373 		printf("%s:\n"
3374 		       " hits_set: %u\n"
3375 		       " bytes_set: %u\n"
3376 		       " hits: %" PRIu64 "\n"
3377 		       " bytes: %" PRIu64 "\n",
3378 		       name,
3379 		       query.count.hits_set,
3380 		       query.count.bytes_set,
3381 		       query.count.hits,
3382 		       query.count.bytes);
3383 		break;
3384 	case RTE_FLOW_ACTION_TYPE_RSS:
3385 		rss_config_display(&query.rss_conf);
3386 		break;
3387 	case RTE_FLOW_ACTION_TYPE_AGE:
3388 		printf("%s:\n"
3389 		       " aged: %u\n"
3390 		       " sec_since_last_hit_valid: %u\n"
3391 		       " sec_since_last_hit: %" PRIu32 "\n",
3392 		       name,
3393 		       query.age.aged,
3394 		       query.age.sec_since_last_hit_valid,
3395 		       query.age.sec_since_last_hit);
3396 		break;
3397 	default:
3398 		fprintf(stderr,
3399 			"Cannot display result for action type %d (%s)\n",
3400 			action->type, name);
3401 		break;
3402 	}
3403 	return 0;
3404 }
3405 
3406 /** List simply and destroy all aged flows. */
3407 void
3408 port_flow_aged(portid_t port_id, uint8_t destroy)
3409 {
3410 	void **contexts;
3411 	int nb_context, total = 0, idx;
3412 	struct rte_flow_error error;
3413 	enum age_action_context_type *type;
3414 	union {
3415 		struct port_flow *pf;
3416 		struct port_indirect_action *pia;
3417 	} ctx;
3418 
3419 	if (port_id_is_invalid(port_id, ENABLED_WARN) ||
3420 	    port_id == (portid_t)RTE_PORT_ALL)
3421 		return;
3422 	total = rte_flow_get_aged_flows(port_id, NULL, 0, &error);
3423 	printf("Port %u total aged flows: %d\n", port_id, total);
3424 	if (total < 0) {
3425 		port_flow_complain(&error);
3426 		return;
3427 	}
3428 	if (total == 0)
3429 		return;
3430 	contexts = malloc(sizeof(void *) * total);
3431 	if (contexts == NULL) {
3432 		fprintf(stderr, "Cannot allocate contexts for aged flow\n");
3433 		return;
3434 	}
3435 	printf("%-20s\tID\tGroup\tPrio\tAttr\n", "Type");
3436 	nb_context = rte_flow_get_aged_flows(port_id, contexts, total, &error);
3437 	if (nb_context != total) {
3438 		fprintf(stderr,
3439 			"Port:%d get aged flows count(%d) != total(%d)\n",
3440 			port_id, nb_context, total);
3441 		free(contexts);
3442 		return;
3443 	}
3444 	total = 0;
3445 	for (idx = 0; idx < nb_context; idx++) {
3446 		if (!contexts[idx]) {
3447 			fprintf(stderr, "Error: get Null context in port %u\n",
3448 				port_id);
3449 			continue;
3450 		}
3451 		type = (enum age_action_context_type *)contexts[idx];
3452 		switch (*type) {
3453 		case ACTION_AGE_CONTEXT_TYPE_FLOW:
3454 			ctx.pf = container_of(type, struct port_flow, age_type);
3455 			printf("%-20s\t%" PRIu32 "\t%" PRIu32 "\t%" PRIu32
3456 								 "\t%c%c%c\t\n",
3457 			       "Flow",
3458 			       ctx.pf->id,
3459 			       ctx.pf->rule.attr->group,
3460 			       ctx.pf->rule.attr->priority,
3461 			       ctx.pf->rule.attr->ingress ? 'i' : '-',
3462 			       ctx.pf->rule.attr->egress ? 'e' : '-',
3463 			       ctx.pf->rule.attr->transfer ? 't' : '-');
3464 			if (destroy && !port_flow_destroy(port_id, 1,
3465 							  &ctx.pf->id))
3466 				total++;
3467 			break;
3468 		case ACTION_AGE_CONTEXT_TYPE_INDIRECT_ACTION:
3469 			ctx.pia = container_of(type,
3470 					struct port_indirect_action, age_type);
3471 			printf("%-20s\t%" PRIu32 "\n", "Indirect action",
3472 			       ctx.pia->id);
3473 			break;
3474 		default:
3475 			fprintf(stderr, "Error: invalid context type %u\n",
3476 				port_id);
3477 			break;
3478 		}
3479 	}
3480 	printf("\n%d flows destroyed\n", total);
3481 	free(contexts);
3482 }
3483 
3484 /** List flow rules. */
3485 void
3486 port_flow_list(portid_t port_id, uint32_t n, const uint32_t *group)
3487 {
3488 	struct rte_port *port;
3489 	struct port_flow *pf;
3490 	struct port_flow *list = NULL;
3491 	uint32_t i;
3492 
3493 	if (port_id_is_invalid(port_id, ENABLED_WARN) ||
3494 	    port_id == (portid_t)RTE_PORT_ALL)
3495 		return;
3496 	port = &ports[port_id];
3497 	if (!port->flow_list)
3498 		return;
3499 	/* Sort flows by group, priority and ID. */
3500 	for (pf = port->flow_list; pf != NULL; pf = pf->next) {
3501 		struct port_flow **tmp;
3502 		const struct rte_flow_attr *curr = pf->rule.attr;
3503 
3504 		if (n) {
3505 			/* Filter out unwanted groups. */
3506 			for (i = 0; i != n; ++i)
3507 				if (curr->group == group[i])
3508 					break;
3509 			if (i == n)
3510 				continue;
3511 		}
3512 		for (tmp = &list; *tmp; tmp = &(*tmp)->tmp) {
3513 			const struct rte_flow_attr *comp = (*tmp)->rule.attr;
3514 
3515 			if (curr->group > comp->group ||
3516 			    (curr->group == comp->group &&
3517 			     curr->priority > comp->priority) ||
3518 			    (curr->group == comp->group &&
3519 			     curr->priority == comp->priority &&
3520 			     pf->id > (*tmp)->id))
3521 				continue;
3522 			break;
3523 		}
3524 		pf->tmp = *tmp;
3525 		*tmp = pf;
3526 	}
3527 	printf("ID\tGroup\tPrio\tAttr\tRule\n");
3528 	for (pf = list; pf != NULL; pf = pf->tmp) {
3529 		const struct rte_flow_item *item = pf->rule.pattern;
3530 		const struct rte_flow_action *action = pf->rule.actions;
3531 		const char *name;
3532 
3533 		printf("%" PRIu32 "\t%" PRIu32 "\t%" PRIu32 "\t%c%c%c\t",
3534 		       pf->id,
3535 		       pf->rule.attr->group,
3536 		       pf->rule.attr->priority,
3537 		       pf->rule.attr->ingress ? 'i' : '-',
3538 		       pf->rule.attr->egress ? 'e' : '-',
3539 		       pf->rule.attr->transfer ? 't' : '-');
3540 		while (item->type != RTE_FLOW_ITEM_TYPE_END) {
3541 			if ((uint32_t)item->type > INT_MAX)
3542 				name = "PMD_INTERNAL";
3543 			else if (rte_flow_conv(RTE_FLOW_CONV_OP_ITEM_NAME_PTR,
3544 					  &name, sizeof(name),
3545 					  (void *)(uintptr_t)item->type,
3546 					  NULL) <= 0)
3547 				name = "[UNKNOWN]";
3548 			if (item->type != RTE_FLOW_ITEM_TYPE_VOID)
3549 				printf("%s ", name);
3550 			++item;
3551 		}
3552 		printf("=>");
3553 		while (action->type != RTE_FLOW_ACTION_TYPE_END) {
3554 			if ((uint32_t)action->type > INT_MAX)
3555 				name = "PMD_INTERNAL";
3556 			else if (rte_flow_conv(RTE_FLOW_CONV_OP_ACTION_NAME_PTR,
3557 					  &name, sizeof(name),
3558 					  (void *)(uintptr_t)action->type,
3559 					  NULL) <= 0)
3560 				name = "[UNKNOWN]";
3561 			if (action->type != RTE_FLOW_ACTION_TYPE_VOID)
3562 				printf(" %s", name);
3563 			++action;
3564 		}
3565 		printf("\n");
3566 	}
3567 }
3568 
3569 /** Restrict ingress traffic to the defined flow rules. */
3570 int
3571 port_flow_isolate(portid_t port_id, int set)
3572 {
3573 	struct rte_flow_error error;
3574 
3575 	/* Poisoning to make sure PMDs update it in case of error. */
3576 	memset(&error, 0x66, sizeof(error));
3577 	if (rte_flow_isolate(port_id, set, &error))
3578 		return port_flow_complain(&error);
3579 	printf("Ingress traffic on port %u is %s to the defined flow rules\n",
3580 	       port_id,
3581 	       set ? "now restricted" : "not restricted anymore");
3582 	return 0;
3583 }
3584 
3585 /*
3586  * RX/TX ring descriptors display functions.
3587  */
3588 int
3589 rx_queue_id_is_invalid(queueid_t rxq_id)
3590 {
3591 	if (rxq_id < nb_rxq)
3592 		return 0;
3593 	fprintf(stderr, "Invalid RX queue %d (must be < nb_rxq=%d)\n",
3594 		rxq_id, nb_rxq);
3595 	return 1;
3596 }
3597 
3598 int
3599 tx_queue_id_is_invalid(queueid_t txq_id)
3600 {
3601 	if (txq_id < nb_txq)
3602 		return 0;
3603 	fprintf(stderr, "Invalid TX queue %d (must be < nb_txq=%d)\n",
3604 		txq_id, nb_txq);
3605 	return 1;
3606 }
3607 
3608 static int
3609 get_rx_ring_size(portid_t port_id, queueid_t rxq_id, uint16_t *ring_size)
3610 {
3611 	struct rte_port *port = &ports[port_id];
3612 	struct rte_eth_rxq_info rx_qinfo;
3613 	int ret;
3614 
3615 	ret = rte_eth_rx_queue_info_get(port_id, rxq_id, &rx_qinfo);
3616 	if (ret == 0) {
3617 		*ring_size = rx_qinfo.nb_desc;
3618 		return ret;
3619 	}
3620 
3621 	if (ret != -ENOTSUP)
3622 		return ret;
3623 	/*
3624 	 * If the rte_eth_rx_queue_info_get is not support for this PMD,
3625 	 * ring_size stored in testpmd will be used for validity verification.
3626 	 * When configure the rxq by rte_eth_rx_queue_setup with nb_rx_desc
3627 	 * being 0, it will use a default value provided by PMDs to setup this
3628 	 * rxq. If the default value is 0, it will use the
3629 	 * RTE_ETH_DEV_FALLBACK_RX_RINGSIZE to setup this rxq.
3630 	 */
3631 	if (port->nb_rx_desc[rxq_id])
3632 		*ring_size = port->nb_rx_desc[rxq_id];
3633 	else if (port->dev_info.default_rxportconf.ring_size)
3634 		*ring_size = port->dev_info.default_rxportconf.ring_size;
3635 	else
3636 		*ring_size = RTE_ETH_DEV_FALLBACK_RX_RINGSIZE;
3637 	return 0;
3638 }
3639 
3640 static int
3641 get_tx_ring_size(portid_t port_id, queueid_t txq_id, uint16_t *ring_size)
3642 {
3643 	struct rte_port *port = &ports[port_id];
3644 	struct rte_eth_txq_info tx_qinfo;
3645 	int ret;
3646 
3647 	ret = rte_eth_tx_queue_info_get(port_id, txq_id, &tx_qinfo);
3648 	if (ret == 0) {
3649 		*ring_size = tx_qinfo.nb_desc;
3650 		return ret;
3651 	}
3652 
3653 	if (ret != -ENOTSUP)
3654 		return ret;
3655 	/*
3656 	 * If the rte_eth_tx_queue_info_get is not support for this PMD,
3657 	 * ring_size stored in testpmd will be used for validity verification.
3658 	 * When configure the txq by rte_eth_tx_queue_setup with nb_tx_desc
3659 	 * being 0, it will use a default value provided by PMDs to setup this
3660 	 * txq. If the default value is 0, it will use the
3661 	 * RTE_ETH_DEV_FALLBACK_TX_RINGSIZE to setup this txq.
3662 	 */
3663 	if (port->nb_tx_desc[txq_id])
3664 		*ring_size = port->nb_tx_desc[txq_id];
3665 	else if (port->dev_info.default_txportconf.ring_size)
3666 		*ring_size = port->dev_info.default_txportconf.ring_size;
3667 	else
3668 		*ring_size = RTE_ETH_DEV_FALLBACK_TX_RINGSIZE;
3669 	return 0;
3670 }
3671 
3672 static int
3673 rx_desc_id_is_invalid(portid_t port_id, queueid_t rxq_id, uint16_t rxdesc_id)
3674 {
3675 	uint16_t ring_size;
3676 	int ret;
3677 
3678 	ret = get_rx_ring_size(port_id, rxq_id, &ring_size);
3679 	if (ret)
3680 		return 1;
3681 
3682 	if (rxdesc_id < ring_size)
3683 		return 0;
3684 
3685 	fprintf(stderr, "Invalid RX descriptor %u (must be < ring_size=%u)\n",
3686 		rxdesc_id, ring_size);
3687 	return 1;
3688 }
3689 
3690 static int
3691 tx_desc_id_is_invalid(portid_t port_id, queueid_t txq_id, uint16_t txdesc_id)
3692 {
3693 	uint16_t ring_size;
3694 	int ret;
3695 
3696 	ret = get_tx_ring_size(port_id, txq_id, &ring_size);
3697 	if (ret)
3698 		return 1;
3699 
3700 	if (txdesc_id < ring_size)
3701 		return 0;
3702 
3703 	fprintf(stderr, "Invalid TX descriptor %u (must be < ring_size=%u)\n",
3704 		txdesc_id, ring_size);
3705 	return 1;
3706 }
3707 
3708 static const struct rte_memzone *
3709 ring_dma_zone_lookup(const char *ring_name, portid_t port_id, uint16_t q_id)
3710 {
3711 	char mz_name[RTE_MEMZONE_NAMESIZE];
3712 	const struct rte_memzone *mz;
3713 
3714 	snprintf(mz_name, sizeof(mz_name), "eth_p%d_q%d_%s",
3715 			port_id, q_id, ring_name);
3716 	mz = rte_memzone_lookup(mz_name);
3717 	if (mz == NULL)
3718 		fprintf(stderr,
3719 			"%s ring memory zoneof (port %d, queue %d) not found (zone name = %s\n",
3720 			ring_name, port_id, q_id, mz_name);
3721 	return mz;
3722 }
3723 
3724 union igb_ring_dword {
3725 	uint64_t dword;
3726 	struct {
3727 #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
3728 		uint32_t lo;
3729 		uint32_t hi;
3730 #else
3731 		uint32_t hi;
3732 		uint32_t lo;
3733 #endif
3734 	} words;
3735 };
3736 
3737 struct igb_ring_desc_32_bytes {
3738 	union igb_ring_dword lo_dword;
3739 	union igb_ring_dword hi_dword;
3740 	union igb_ring_dword resv1;
3741 	union igb_ring_dword resv2;
3742 };
3743 
3744 struct igb_ring_desc_16_bytes {
3745 	union igb_ring_dword lo_dword;
3746 	union igb_ring_dword hi_dword;
3747 };
3748 
3749 static void
3750 ring_rxd_display_dword(union igb_ring_dword dword)
3751 {
3752 	printf("    0x%08X - 0x%08X\n", (unsigned)dword.words.lo,
3753 					(unsigned)dword.words.hi);
3754 }
3755 
3756 static void
3757 ring_rx_descriptor_display(const struct rte_memzone *ring_mz,
3758 #ifndef RTE_LIBRTE_I40E_16BYTE_RX_DESC
3759 			   portid_t port_id,
3760 #else
3761 			   __rte_unused portid_t port_id,
3762 #endif
3763 			   uint16_t desc_id)
3764 {
3765 	struct igb_ring_desc_16_bytes *ring =
3766 		(struct igb_ring_desc_16_bytes *)ring_mz->addr;
3767 #ifndef RTE_LIBRTE_I40E_16BYTE_RX_DESC
3768 	int ret;
3769 	struct rte_eth_dev_info dev_info;
3770 
3771 	ret = eth_dev_info_get_print_err(port_id, &dev_info);
3772 	if (ret != 0)
3773 		return;
3774 
3775 	if (strstr(dev_info.driver_name, "i40e") != NULL) {
3776 		/* 32 bytes RX descriptor, i40e only */
3777 		struct igb_ring_desc_32_bytes *ring =
3778 			(struct igb_ring_desc_32_bytes *)ring_mz->addr;
3779 		ring[desc_id].lo_dword.dword =
3780 			rte_le_to_cpu_64(ring[desc_id].lo_dword.dword);
3781 		ring_rxd_display_dword(ring[desc_id].lo_dword);
3782 		ring[desc_id].hi_dword.dword =
3783 			rte_le_to_cpu_64(ring[desc_id].hi_dword.dword);
3784 		ring_rxd_display_dword(ring[desc_id].hi_dword);
3785 		ring[desc_id].resv1.dword =
3786 			rte_le_to_cpu_64(ring[desc_id].resv1.dword);
3787 		ring_rxd_display_dword(ring[desc_id].resv1);
3788 		ring[desc_id].resv2.dword =
3789 			rte_le_to_cpu_64(ring[desc_id].resv2.dword);
3790 		ring_rxd_display_dword(ring[desc_id].resv2);
3791 
3792 		return;
3793 	}
3794 #endif
3795 	/* 16 bytes RX descriptor */
3796 	ring[desc_id].lo_dword.dword =
3797 		rte_le_to_cpu_64(ring[desc_id].lo_dword.dword);
3798 	ring_rxd_display_dword(ring[desc_id].lo_dword);
3799 	ring[desc_id].hi_dword.dword =
3800 		rte_le_to_cpu_64(ring[desc_id].hi_dword.dword);
3801 	ring_rxd_display_dword(ring[desc_id].hi_dword);
3802 }
3803 
3804 static void
3805 ring_tx_descriptor_display(const struct rte_memzone *ring_mz, uint16_t desc_id)
3806 {
3807 	struct igb_ring_desc_16_bytes *ring;
3808 	struct igb_ring_desc_16_bytes txd;
3809 
3810 	ring = (struct igb_ring_desc_16_bytes *)ring_mz->addr;
3811 	txd.lo_dword.dword = rte_le_to_cpu_64(ring[desc_id].lo_dword.dword);
3812 	txd.hi_dword.dword = rte_le_to_cpu_64(ring[desc_id].hi_dword.dword);
3813 	printf("    0x%08X - 0x%08X / 0x%08X - 0x%08X\n",
3814 			(unsigned)txd.lo_dword.words.lo,
3815 			(unsigned)txd.lo_dword.words.hi,
3816 			(unsigned)txd.hi_dword.words.lo,
3817 			(unsigned)txd.hi_dword.words.hi);
3818 }
3819 
3820 void
3821 rx_ring_desc_display(portid_t port_id, queueid_t rxq_id, uint16_t rxd_id)
3822 {
3823 	const struct rte_memzone *rx_mz;
3824 
3825 	if (rx_desc_id_is_invalid(port_id, rxq_id, rxd_id))
3826 		return;
3827 	rx_mz = ring_dma_zone_lookup("rx_ring", port_id, rxq_id);
3828 	if (rx_mz == NULL)
3829 		return;
3830 	ring_rx_descriptor_display(rx_mz, port_id, rxd_id);
3831 }
3832 
3833 void
3834 tx_ring_desc_display(portid_t port_id, queueid_t txq_id, uint16_t txd_id)
3835 {
3836 	const struct rte_memzone *tx_mz;
3837 
3838 	if (tx_desc_id_is_invalid(port_id, txq_id, txd_id))
3839 		return;
3840 	tx_mz = ring_dma_zone_lookup("tx_ring", port_id, txq_id);
3841 	if (tx_mz == NULL)
3842 		return;
3843 	ring_tx_descriptor_display(tx_mz, txd_id);
3844 }
3845 
3846 void
3847 fwd_lcores_config_display(void)
3848 {
3849 	lcoreid_t lc_id;
3850 
3851 	printf("List of forwarding lcores:");
3852 	for (lc_id = 0; lc_id < nb_cfg_lcores; lc_id++)
3853 		printf(" %2u", fwd_lcores_cpuids[lc_id]);
3854 	printf("\n");
3855 }
3856 void
3857 rxtx_config_display(void)
3858 {
3859 	portid_t pid;
3860 	queueid_t qid;
3861 
3862 	printf("  %s packet forwarding%s packets/burst=%d\n",
3863 	       cur_fwd_eng->fwd_mode_name,
3864 	       retry_enabled == 0 ? "" : " with retry",
3865 	       nb_pkt_per_burst);
3866 
3867 	if (cur_fwd_eng == &tx_only_engine || cur_fwd_eng == &flow_gen_engine)
3868 		printf("  packet len=%u - nb packet segments=%d\n",
3869 				(unsigned)tx_pkt_length, (int) tx_pkt_nb_segs);
3870 
3871 	printf("  nb forwarding cores=%d - nb forwarding ports=%d\n",
3872 	       nb_fwd_lcores, nb_fwd_ports);
3873 
3874 	RTE_ETH_FOREACH_DEV(pid) {
3875 		struct rte_eth_rxconf *rx_conf = &ports[pid].rxq[0].conf;
3876 		struct rte_eth_txconf *tx_conf = &ports[pid].txq[0].conf;
3877 		uint16_t *nb_rx_desc = &ports[pid].nb_rx_desc[0];
3878 		uint16_t *nb_tx_desc = &ports[pid].nb_tx_desc[0];
3879 		struct rte_eth_rxq_info rx_qinfo;
3880 		struct rte_eth_txq_info tx_qinfo;
3881 		uint16_t rx_free_thresh_tmp;
3882 		uint16_t tx_free_thresh_tmp;
3883 		uint16_t tx_rs_thresh_tmp;
3884 		uint16_t nb_rx_desc_tmp;
3885 		uint16_t nb_tx_desc_tmp;
3886 		uint64_t offloads_tmp;
3887 		uint8_t pthresh_tmp;
3888 		uint8_t hthresh_tmp;
3889 		uint8_t wthresh_tmp;
3890 		int32_t rc;
3891 
3892 		/* per port config */
3893 		printf("  port %d: RX queue number: %d Tx queue number: %d\n",
3894 				(unsigned int)pid, nb_rxq, nb_txq);
3895 
3896 		printf("    Rx offloads=0x%"PRIx64" Tx offloads=0x%"PRIx64"\n",
3897 				ports[pid].dev_conf.rxmode.offloads,
3898 				ports[pid].dev_conf.txmode.offloads);
3899 
3900 		/* per rx queue config only for first queue to be less verbose */
3901 		for (qid = 0; qid < 1; qid++) {
3902 			rc = rte_eth_rx_queue_info_get(pid, qid, &rx_qinfo);
3903 			if (rc) {
3904 				nb_rx_desc_tmp = nb_rx_desc[qid];
3905 				rx_free_thresh_tmp =
3906 					rx_conf[qid].rx_free_thresh;
3907 				pthresh_tmp = rx_conf[qid].rx_thresh.pthresh;
3908 				hthresh_tmp = rx_conf[qid].rx_thresh.hthresh;
3909 				wthresh_tmp = rx_conf[qid].rx_thresh.wthresh;
3910 				offloads_tmp = rx_conf[qid].offloads;
3911 			} else {
3912 				nb_rx_desc_tmp = rx_qinfo.nb_desc;
3913 				rx_free_thresh_tmp =
3914 						rx_qinfo.conf.rx_free_thresh;
3915 				pthresh_tmp = rx_qinfo.conf.rx_thresh.pthresh;
3916 				hthresh_tmp = rx_qinfo.conf.rx_thresh.hthresh;
3917 				wthresh_tmp = rx_qinfo.conf.rx_thresh.wthresh;
3918 				offloads_tmp = rx_qinfo.conf.offloads;
3919 			}
3920 
3921 			printf("    RX queue: %d\n", qid);
3922 			printf("      RX desc=%d - RX free threshold=%d\n",
3923 				nb_rx_desc_tmp, rx_free_thresh_tmp);
3924 			printf("      RX threshold registers: pthresh=%d hthresh=%d "
3925 				" wthresh=%d\n",
3926 				pthresh_tmp, hthresh_tmp, wthresh_tmp);
3927 			printf("      RX Offloads=0x%"PRIx64, offloads_tmp);
3928 			if (rx_conf->share_group > 0)
3929 				printf(" share_group=%u share_qid=%u",
3930 				       rx_conf->share_group,
3931 				       rx_conf->share_qid);
3932 			printf("\n");
3933 		}
3934 
3935 		/* per tx queue config only for first queue to be less verbose */
3936 		for (qid = 0; qid < 1; qid++) {
3937 			rc = rte_eth_tx_queue_info_get(pid, qid, &tx_qinfo);
3938 			if (rc) {
3939 				nb_tx_desc_tmp = nb_tx_desc[qid];
3940 				tx_free_thresh_tmp =
3941 					tx_conf[qid].tx_free_thresh;
3942 				pthresh_tmp = tx_conf[qid].tx_thresh.pthresh;
3943 				hthresh_tmp = tx_conf[qid].tx_thresh.hthresh;
3944 				wthresh_tmp = tx_conf[qid].tx_thresh.wthresh;
3945 				offloads_tmp = tx_conf[qid].offloads;
3946 				tx_rs_thresh_tmp = tx_conf[qid].tx_rs_thresh;
3947 			} else {
3948 				nb_tx_desc_tmp = tx_qinfo.nb_desc;
3949 				tx_free_thresh_tmp =
3950 						tx_qinfo.conf.tx_free_thresh;
3951 				pthresh_tmp = tx_qinfo.conf.tx_thresh.pthresh;
3952 				hthresh_tmp = tx_qinfo.conf.tx_thresh.hthresh;
3953 				wthresh_tmp = tx_qinfo.conf.tx_thresh.wthresh;
3954 				offloads_tmp = tx_qinfo.conf.offloads;
3955 				tx_rs_thresh_tmp = tx_qinfo.conf.tx_rs_thresh;
3956 			}
3957 
3958 			printf("    TX queue: %d\n", qid);
3959 			printf("      TX desc=%d - TX free threshold=%d\n",
3960 				nb_tx_desc_tmp, tx_free_thresh_tmp);
3961 			printf("      TX threshold registers: pthresh=%d hthresh=%d "
3962 				" wthresh=%d\n",
3963 				pthresh_tmp, hthresh_tmp, wthresh_tmp);
3964 			printf("      TX offloads=0x%"PRIx64" - TX RS bit threshold=%d\n",
3965 				offloads_tmp, tx_rs_thresh_tmp);
3966 		}
3967 	}
3968 }
3969 
3970 void
3971 port_rss_reta_info(portid_t port_id,
3972 		   struct rte_eth_rss_reta_entry64 *reta_conf,
3973 		   uint16_t nb_entries)
3974 {
3975 	uint16_t i, idx, shift;
3976 	int ret;
3977 
3978 	if (port_id_is_invalid(port_id, ENABLED_WARN))
3979 		return;
3980 
3981 	ret = rte_eth_dev_rss_reta_query(port_id, reta_conf, nb_entries);
3982 	if (ret != 0) {
3983 		fprintf(stderr,
3984 			"Failed to get RSS RETA info, return code = %d\n",
3985 			ret);
3986 		return;
3987 	}
3988 
3989 	for (i = 0; i < nb_entries; i++) {
3990 		idx = i / RTE_ETH_RETA_GROUP_SIZE;
3991 		shift = i % RTE_ETH_RETA_GROUP_SIZE;
3992 		if (!(reta_conf[idx].mask & (1ULL << shift)))
3993 			continue;
3994 		printf("RSS RETA configuration: hash index=%u, queue=%u\n",
3995 					i, reta_conf[idx].reta[shift]);
3996 	}
3997 }
3998 
3999 /*
4000  * Displays the RSS hash functions of a port, and, optionally, the RSS hash
4001  * key of the port.
4002  */
4003 void
4004 port_rss_hash_conf_show(portid_t port_id, int show_rss_key)
4005 {
4006 	struct rte_eth_rss_conf rss_conf = {0};
4007 	uint8_t rss_key[RSS_HASH_KEY_LENGTH];
4008 	uint64_t rss_hf;
4009 	uint8_t i;
4010 	int diag;
4011 	struct rte_eth_dev_info dev_info;
4012 	uint8_t hash_key_size;
4013 	int ret;
4014 
4015 	if (port_id_is_invalid(port_id, ENABLED_WARN))
4016 		return;
4017 
4018 	ret = eth_dev_info_get_print_err(port_id, &dev_info);
4019 	if (ret != 0)
4020 		return;
4021 
4022 	if (dev_info.hash_key_size > 0 &&
4023 			dev_info.hash_key_size <= sizeof(rss_key))
4024 		hash_key_size = dev_info.hash_key_size;
4025 	else {
4026 		fprintf(stderr,
4027 			"dev_info did not provide a valid hash key size\n");
4028 		return;
4029 	}
4030 
4031 	/* Get RSS hash key if asked to display it */
4032 	rss_conf.rss_key = (show_rss_key) ? rss_key : NULL;
4033 	rss_conf.rss_key_len = hash_key_size;
4034 	diag = rte_eth_dev_rss_hash_conf_get(port_id, &rss_conf);
4035 	if (diag != 0) {
4036 		switch (diag) {
4037 		case -ENODEV:
4038 			fprintf(stderr, "port index %d invalid\n", port_id);
4039 			break;
4040 		case -ENOTSUP:
4041 			fprintf(stderr, "operation not supported by device\n");
4042 			break;
4043 		default:
4044 			fprintf(stderr, "operation failed - diag=%d\n", diag);
4045 			break;
4046 		}
4047 		return;
4048 	}
4049 	rss_hf = rss_conf.rss_hf;
4050 	if (rss_hf == 0) {
4051 		printf("RSS disabled\n");
4052 		return;
4053 	}
4054 	printf("RSS functions:\n");
4055 	rss_types_display(rss_hf, TESTPMD_RSS_TYPES_CHAR_NUM_PER_LINE);
4056 	if (!show_rss_key)
4057 		return;
4058 	printf("RSS key:\n");
4059 	for (i = 0; i < hash_key_size; i++)
4060 		printf("%02X", rss_key[i]);
4061 	printf("\n");
4062 }
4063 
4064 void
4065 port_rss_hash_key_update(portid_t port_id, char rss_type[], uint8_t *hash_key,
4066 			 uint8_t hash_key_len)
4067 {
4068 	struct rte_eth_rss_conf rss_conf;
4069 	int diag;
4070 
4071 	rss_conf.rss_key = NULL;
4072 	rss_conf.rss_key_len = 0;
4073 	rss_conf.rss_hf = str_to_rsstypes(rss_type);
4074 	diag = rte_eth_dev_rss_hash_conf_get(port_id, &rss_conf);
4075 	if (diag == 0) {
4076 		rss_conf.rss_key = hash_key;
4077 		rss_conf.rss_key_len = hash_key_len;
4078 		diag = rte_eth_dev_rss_hash_update(port_id, &rss_conf);
4079 	}
4080 	if (diag == 0)
4081 		return;
4082 
4083 	switch (diag) {
4084 	case -ENODEV:
4085 		fprintf(stderr, "port index %d invalid\n", port_id);
4086 		break;
4087 	case -ENOTSUP:
4088 		fprintf(stderr, "operation not supported by device\n");
4089 		break;
4090 	default:
4091 		fprintf(stderr, "operation failed - diag=%d\n", diag);
4092 		break;
4093 	}
4094 }
4095 
4096 /*
4097  * Check whether a shared rxq scheduled on other lcores.
4098  */
4099 static bool
4100 fwd_stream_on_other_lcores(uint16_t domain_id, lcoreid_t src_lc,
4101 			   portid_t src_port, queueid_t src_rxq,
4102 			   uint32_t share_group, queueid_t share_rxq)
4103 {
4104 	streamid_t sm_id;
4105 	streamid_t nb_fs_per_lcore;
4106 	lcoreid_t  nb_fc;
4107 	lcoreid_t  lc_id;
4108 	struct fwd_stream *fs;
4109 	struct rte_port *port;
4110 	struct rte_eth_dev_info *dev_info;
4111 	struct rte_eth_rxconf *rxq_conf;
4112 
4113 	nb_fc = cur_fwd_config.nb_fwd_lcores;
4114 	/* Check remaining cores. */
4115 	for (lc_id = src_lc + 1; lc_id < nb_fc; lc_id++) {
4116 		sm_id = fwd_lcores[lc_id]->stream_idx;
4117 		nb_fs_per_lcore = fwd_lcores[lc_id]->stream_nb;
4118 		for (; sm_id < fwd_lcores[lc_id]->stream_idx + nb_fs_per_lcore;
4119 		     sm_id++) {
4120 			fs = fwd_streams[sm_id];
4121 			port = &ports[fs->rx_port];
4122 			dev_info = &port->dev_info;
4123 			rxq_conf = &port->rxq[fs->rx_queue].conf;
4124 			if ((dev_info->dev_capa & RTE_ETH_DEV_CAPA_RXQ_SHARE)
4125 			    == 0 || rxq_conf->share_group == 0)
4126 				/* Not shared rxq. */
4127 				continue;
4128 			if (domain_id != port->dev_info.switch_info.domain_id)
4129 				continue;
4130 			if (rxq_conf->share_group != share_group)
4131 				continue;
4132 			if (rxq_conf->share_qid != share_rxq)
4133 				continue;
4134 			printf("Shared Rx queue group %u queue %hu can't be scheduled on different cores:\n",
4135 			       share_group, share_rxq);
4136 			printf("  lcore %hhu Port %hu queue %hu\n",
4137 			       src_lc, src_port, src_rxq);
4138 			printf("  lcore %hhu Port %hu queue %hu\n",
4139 			       lc_id, fs->rx_port, fs->rx_queue);
4140 			printf("Please use --nb-cores=%hu to limit number of forwarding cores\n",
4141 			       nb_rxq);
4142 			return true;
4143 		}
4144 	}
4145 	return false;
4146 }
4147 
4148 /*
4149  * Check shared rxq configuration.
4150  *
4151  * Shared group must not being scheduled on different core.
4152  */
4153 bool
4154 pkt_fwd_shared_rxq_check(void)
4155 {
4156 	streamid_t sm_id;
4157 	streamid_t nb_fs_per_lcore;
4158 	lcoreid_t  nb_fc;
4159 	lcoreid_t  lc_id;
4160 	struct fwd_stream *fs;
4161 	uint16_t domain_id;
4162 	struct rte_port *port;
4163 	struct rte_eth_dev_info *dev_info;
4164 	struct rte_eth_rxconf *rxq_conf;
4165 
4166 	if (rxq_share == 0)
4167 		return true;
4168 	nb_fc = cur_fwd_config.nb_fwd_lcores;
4169 	/*
4170 	 * Check streams on each core, make sure the same switch domain +
4171 	 * group + queue doesn't get scheduled on other cores.
4172 	 */
4173 	for (lc_id = 0; lc_id < nb_fc; lc_id++) {
4174 		sm_id = fwd_lcores[lc_id]->stream_idx;
4175 		nb_fs_per_lcore = fwd_lcores[lc_id]->stream_nb;
4176 		for (; sm_id < fwd_lcores[lc_id]->stream_idx + nb_fs_per_lcore;
4177 		     sm_id++) {
4178 			fs = fwd_streams[sm_id];
4179 			/* Update lcore info stream being scheduled. */
4180 			fs->lcore = fwd_lcores[lc_id];
4181 			port = &ports[fs->rx_port];
4182 			dev_info = &port->dev_info;
4183 			rxq_conf = &port->rxq[fs->rx_queue].conf;
4184 			if ((dev_info->dev_capa & RTE_ETH_DEV_CAPA_RXQ_SHARE)
4185 			    == 0 || rxq_conf->share_group == 0)
4186 				/* Not shared rxq. */
4187 				continue;
4188 			/* Check shared rxq not scheduled on remaining cores. */
4189 			domain_id = port->dev_info.switch_info.domain_id;
4190 			if (fwd_stream_on_other_lcores(domain_id, lc_id,
4191 						       fs->rx_port,
4192 						       fs->rx_queue,
4193 						       rxq_conf->share_group,
4194 						       rxq_conf->share_qid))
4195 				return false;
4196 		}
4197 	}
4198 	return true;
4199 }
4200 
4201 /*
4202  * Setup forwarding configuration for each logical core.
4203  */
4204 static void
4205 setup_fwd_config_of_each_lcore(struct fwd_config *cfg)
4206 {
4207 	streamid_t nb_fs_per_lcore;
4208 	streamid_t nb_fs;
4209 	streamid_t sm_id;
4210 	lcoreid_t  nb_extra;
4211 	lcoreid_t  nb_fc;
4212 	lcoreid_t  nb_lc;
4213 	lcoreid_t  lc_id;
4214 
4215 	nb_fs = cfg->nb_fwd_streams;
4216 	nb_fc = cfg->nb_fwd_lcores;
4217 	if (nb_fs <= nb_fc) {
4218 		nb_fs_per_lcore = 1;
4219 		nb_extra = 0;
4220 	} else {
4221 		nb_fs_per_lcore = (streamid_t) (nb_fs / nb_fc);
4222 		nb_extra = (lcoreid_t) (nb_fs % nb_fc);
4223 	}
4224 
4225 	nb_lc = (lcoreid_t) (nb_fc - nb_extra);
4226 	sm_id = 0;
4227 	for (lc_id = 0; lc_id < nb_lc; lc_id++) {
4228 		fwd_lcores[lc_id]->stream_idx = sm_id;
4229 		fwd_lcores[lc_id]->stream_nb = nb_fs_per_lcore;
4230 		sm_id = (streamid_t) (sm_id + nb_fs_per_lcore);
4231 	}
4232 
4233 	/*
4234 	 * Assign extra remaining streams, if any.
4235 	 */
4236 	nb_fs_per_lcore = (streamid_t) (nb_fs_per_lcore + 1);
4237 	for (lc_id = 0; lc_id < nb_extra; lc_id++) {
4238 		fwd_lcores[nb_lc + lc_id]->stream_idx = sm_id;
4239 		fwd_lcores[nb_lc + lc_id]->stream_nb = nb_fs_per_lcore;
4240 		sm_id = (streamid_t) (sm_id + nb_fs_per_lcore);
4241 	}
4242 }
4243 
4244 static portid_t
4245 fwd_topology_tx_port_get(portid_t rxp)
4246 {
4247 	static int warning_once = 1;
4248 
4249 	RTE_ASSERT(rxp < cur_fwd_config.nb_fwd_ports);
4250 
4251 	switch (port_topology) {
4252 	default:
4253 	case PORT_TOPOLOGY_PAIRED:
4254 		if ((rxp & 0x1) == 0) {
4255 			if (rxp + 1 < cur_fwd_config.nb_fwd_ports)
4256 				return rxp + 1;
4257 			if (warning_once) {
4258 				fprintf(stderr,
4259 					"\nWarning! port-topology=paired and odd forward ports number, the last port will pair with itself.\n\n");
4260 				warning_once = 0;
4261 			}
4262 			return rxp;
4263 		}
4264 		return rxp - 1;
4265 	case PORT_TOPOLOGY_CHAINED:
4266 		return (rxp + 1) % cur_fwd_config.nb_fwd_ports;
4267 	case PORT_TOPOLOGY_LOOP:
4268 		return rxp;
4269 	}
4270 }
4271 
4272 static void
4273 simple_fwd_config_setup(void)
4274 {
4275 	portid_t i;
4276 
4277 	cur_fwd_config.nb_fwd_ports = (portid_t) nb_fwd_ports;
4278 	cur_fwd_config.nb_fwd_streams =
4279 		(streamid_t) cur_fwd_config.nb_fwd_ports;
4280 
4281 	/* reinitialize forwarding streams */
4282 	init_fwd_streams();
4283 
4284 	/*
4285 	 * In the simple forwarding test, the number of forwarding cores
4286 	 * must be lower or equal to the number of forwarding ports.
4287 	 */
4288 	cur_fwd_config.nb_fwd_lcores = (lcoreid_t) nb_fwd_lcores;
4289 	if (cur_fwd_config.nb_fwd_lcores > cur_fwd_config.nb_fwd_ports)
4290 		cur_fwd_config.nb_fwd_lcores =
4291 			(lcoreid_t) cur_fwd_config.nb_fwd_ports;
4292 	setup_fwd_config_of_each_lcore(&cur_fwd_config);
4293 
4294 	for (i = 0; i < cur_fwd_config.nb_fwd_ports; i++) {
4295 		fwd_streams[i]->rx_port   = fwd_ports_ids[i];
4296 		fwd_streams[i]->rx_queue  = 0;
4297 		fwd_streams[i]->tx_port   =
4298 				fwd_ports_ids[fwd_topology_tx_port_get(i)];
4299 		fwd_streams[i]->tx_queue  = 0;
4300 		fwd_streams[i]->peer_addr = fwd_streams[i]->tx_port;
4301 		fwd_streams[i]->retry_enabled = retry_enabled;
4302 	}
4303 }
4304 
4305 /**
4306  * For the RSS forwarding test all streams distributed over lcores. Each stream
4307  * being composed of a RX queue to poll on a RX port for input messages,
4308  * associated with a TX queue of a TX port where to send forwarded packets.
4309  */
4310 static void
4311 rss_fwd_config_setup(void)
4312 {
4313 	portid_t   rxp;
4314 	portid_t   txp;
4315 	queueid_t  rxq;
4316 	queueid_t  nb_q;
4317 	streamid_t  sm_id;
4318 	int start;
4319 	int end;
4320 
4321 	nb_q = nb_rxq;
4322 	if (nb_q > nb_txq)
4323 		nb_q = nb_txq;
4324 	cur_fwd_config.nb_fwd_lcores = (lcoreid_t) nb_fwd_lcores;
4325 	cur_fwd_config.nb_fwd_ports = nb_fwd_ports;
4326 	cur_fwd_config.nb_fwd_streams =
4327 		(streamid_t) (nb_q * cur_fwd_config.nb_fwd_ports);
4328 
4329 	if (cur_fwd_config.nb_fwd_streams < cur_fwd_config.nb_fwd_lcores)
4330 		cur_fwd_config.nb_fwd_lcores =
4331 			(lcoreid_t)cur_fwd_config.nb_fwd_streams;
4332 
4333 	/* reinitialize forwarding streams */
4334 	init_fwd_streams();
4335 
4336 	setup_fwd_config_of_each_lcore(&cur_fwd_config);
4337 
4338 	if (proc_id > 0 && nb_q % num_procs != 0)
4339 		printf("Warning! queue numbers should be multiple of processes, or packet loss will happen.\n");
4340 
4341 	/**
4342 	 * In multi-process, All queues are allocated to different
4343 	 * processes based on num_procs and proc_id. For example:
4344 	 * if supports 4 queues(nb_q), 2 processes(num_procs),
4345 	 * the 0~1 queue for primary process.
4346 	 * the 2~3 queue for secondary process.
4347 	 */
4348 	start = proc_id * nb_q / num_procs;
4349 	end = start + nb_q / num_procs;
4350 	rxp = 0;
4351 	rxq = start;
4352 	for (sm_id = 0; sm_id < cur_fwd_config.nb_fwd_streams; sm_id++) {
4353 		struct fwd_stream *fs;
4354 
4355 		fs = fwd_streams[sm_id];
4356 		txp = fwd_topology_tx_port_get(rxp);
4357 		fs->rx_port = fwd_ports_ids[rxp];
4358 		fs->rx_queue = rxq;
4359 		fs->tx_port = fwd_ports_ids[txp];
4360 		fs->tx_queue = rxq;
4361 		fs->peer_addr = fs->tx_port;
4362 		fs->retry_enabled = retry_enabled;
4363 		rxp++;
4364 		if (rxp < nb_fwd_ports)
4365 			continue;
4366 		rxp = 0;
4367 		rxq++;
4368 		if (rxq >= end)
4369 			rxq = start;
4370 	}
4371 }
4372 
4373 static uint16_t
4374 get_fwd_port_total_tc_num(void)
4375 {
4376 	struct rte_eth_dcb_info dcb_info;
4377 	uint16_t total_tc_num = 0;
4378 	unsigned int i;
4379 
4380 	for (i = 0; i < nb_fwd_ports; i++) {
4381 		(void)rte_eth_dev_get_dcb_info(fwd_ports_ids[i], &dcb_info);
4382 		total_tc_num += dcb_info.nb_tcs;
4383 	}
4384 
4385 	return total_tc_num;
4386 }
4387 
4388 /**
4389  * For the DCB forwarding test, each core is assigned on each traffic class.
4390  *
4391  * Each core is assigned a multi-stream, each stream being composed of
4392  * a RX queue to poll on a RX port for input messages, associated with
4393  * a TX queue of a TX port where to send forwarded packets. All RX and
4394  * TX queues are mapping to the same traffic class.
4395  * If VMDQ and DCB co-exist, each traffic class on different POOLs share
4396  * the same core
4397  */
4398 static void
4399 dcb_fwd_config_setup(void)
4400 {
4401 	struct rte_eth_dcb_info rxp_dcb_info, txp_dcb_info;
4402 	portid_t txp, rxp = 0;
4403 	queueid_t txq, rxq = 0;
4404 	lcoreid_t  lc_id;
4405 	uint16_t nb_rx_queue, nb_tx_queue;
4406 	uint16_t i, j, k, sm_id = 0;
4407 	uint16_t total_tc_num;
4408 	struct rte_port *port;
4409 	uint8_t tc = 0;
4410 	portid_t pid;
4411 	int ret;
4412 
4413 	/*
4414 	 * The fwd_config_setup() is called when the port is RTE_PORT_STARTED
4415 	 * or RTE_PORT_STOPPED.
4416 	 *
4417 	 * Re-configure ports to get updated mapping between tc and queue in
4418 	 * case the queue number of the port is changed. Skip for started ports
4419 	 * since modifying queue number and calling dev_configure need to stop
4420 	 * ports first.
4421 	 */
4422 	for (pid = 0; pid < nb_fwd_ports; pid++) {
4423 		if (port_is_started(pid) == 1)
4424 			continue;
4425 
4426 		port = &ports[pid];
4427 		ret = rte_eth_dev_configure(pid, nb_rxq, nb_txq,
4428 					    &port->dev_conf);
4429 		if (ret < 0) {
4430 			fprintf(stderr,
4431 				"Failed to re-configure port %d, ret = %d.\n",
4432 				pid, ret);
4433 			return;
4434 		}
4435 	}
4436 
4437 	cur_fwd_config.nb_fwd_lcores = (lcoreid_t) nb_fwd_lcores;
4438 	cur_fwd_config.nb_fwd_ports = nb_fwd_ports;
4439 	cur_fwd_config.nb_fwd_streams =
4440 		(streamid_t) (nb_rxq * cur_fwd_config.nb_fwd_ports);
4441 	total_tc_num = get_fwd_port_total_tc_num();
4442 	if (cur_fwd_config.nb_fwd_lcores > total_tc_num)
4443 		cur_fwd_config.nb_fwd_lcores = total_tc_num;
4444 
4445 	/* reinitialize forwarding streams */
4446 	init_fwd_streams();
4447 	sm_id = 0;
4448 	txp = 1;
4449 	/* get the dcb info on the first RX and TX ports */
4450 	(void)rte_eth_dev_get_dcb_info(fwd_ports_ids[rxp], &rxp_dcb_info);
4451 	(void)rte_eth_dev_get_dcb_info(fwd_ports_ids[txp], &txp_dcb_info);
4452 
4453 	for (lc_id = 0; lc_id < cur_fwd_config.nb_fwd_lcores; lc_id++) {
4454 		fwd_lcores[lc_id]->stream_nb = 0;
4455 		fwd_lcores[lc_id]->stream_idx = sm_id;
4456 		for (i = 0; i < RTE_ETH_MAX_VMDQ_POOL; i++) {
4457 			/* if the nb_queue is zero, means this tc is
4458 			 * not enabled on the POOL
4459 			 */
4460 			if (rxp_dcb_info.tc_queue.tc_rxq[i][tc].nb_queue == 0)
4461 				break;
4462 			k = fwd_lcores[lc_id]->stream_nb +
4463 				fwd_lcores[lc_id]->stream_idx;
4464 			rxq = rxp_dcb_info.tc_queue.tc_rxq[i][tc].base;
4465 			txq = txp_dcb_info.tc_queue.tc_txq[i][tc].base;
4466 			nb_rx_queue = txp_dcb_info.tc_queue.tc_rxq[i][tc].nb_queue;
4467 			nb_tx_queue = txp_dcb_info.tc_queue.tc_txq[i][tc].nb_queue;
4468 			for (j = 0; j < nb_rx_queue; j++) {
4469 				struct fwd_stream *fs;
4470 
4471 				fs = fwd_streams[k + j];
4472 				fs->rx_port = fwd_ports_ids[rxp];
4473 				fs->rx_queue = rxq + j;
4474 				fs->tx_port = fwd_ports_ids[txp];
4475 				fs->tx_queue = txq + j % nb_tx_queue;
4476 				fs->peer_addr = fs->tx_port;
4477 				fs->retry_enabled = retry_enabled;
4478 			}
4479 			fwd_lcores[lc_id]->stream_nb +=
4480 				rxp_dcb_info.tc_queue.tc_rxq[i][tc].nb_queue;
4481 		}
4482 		sm_id = (streamid_t) (sm_id + fwd_lcores[lc_id]->stream_nb);
4483 
4484 		tc++;
4485 		if (tc < rxp_dcb_info.nb_tcs)
4486 			continue;
4487 		/* Restart from TC 0 on next RX port */
4488 		tc = 0;
4489 		if (numa_support && (nb_fwd_ports <= (nb_ports >> 1)))
4490 			rxp = (portid_t)
4491 				(rxp + ((nb_ports >> 1) / nb_fwd_ports));
4492 		else
4493 			rxp++;
4494 		if (rxp >= nb_fwd_ports)
4495 			return;
4496 		/* get the dcb information on next RX and TX ports */
4497 		if ((rxp & 0x1) == 0)
4498 			txp = (portid_t) (rxp + 1);
4499 		else
4500 			txp = (portid_t) (rxp - 1);
4501 		rte_eth_dev_get_dcb_info(fwd_ports_ids[rxp], &rxp_dcb_info);
4502 		rte_eth_dev_get_dcb_info(fwd_ports_ids[txp], &txp_dcb_info);
4503 	}
4504 }
4505 
4506 static void
4507 icmp_echo_config_setup(void)
4508 {
4509 	portid_t  rxp;
4510 	queueid_t rxq;
4511 	lcoreid_t lc_id;
4512 	uint16_t  sm_id;
4513 
4514 	if ((nb_txq * nb_fwd_ports) < nb_fwd_lcores)
4515 		cur_fwd_config.nb_fwd_lcores = (lcoreid_t)
4516 			(nb_txq * nb_fwd_ports);
4517 	else
4518 		cur_fwd_config.nb_fwd_lcores = (lcoreid_t) nb_fwd_lcores;
4519 	cur_fwd_config.nb_fwd_ports = nb_fwd_ports;
4520 	cur_fwd_config.nb_fwd_streams =
4521 		(streamid_t) (nb_rxq * cur_fwd_config.nb_fwd_ports);
4522 	if (cur_fwd_config.nb_fwd_streams < cur_fwd_config.nb_fwd_lcores)
4523 		cur_fwd_config.nb_fwd_lcores =
4524 			(lcoreid_t)cur_fwd_config.nb_fwd_streams;
4525 	if (verbose_level > 0) {
4526 		printf("%s fwd_cores=%d fwd_ports=%d fwd_streams=%d\n",
4527 		       __FUNCTION__,
4528 		       cur_fwd_config.nb_fwd_lcores,
4529 		       cur_fwd_config.nb_fwd_ports,
4530 		       cur_fwd_config.nb_fwd_streams);
4531 	}
4532 
4533 	/* reinitialize forwarding streams */
4534 	init_fwd_streams();
4535 	setup_fwd_config_of_each_lcore(&cur_fwd_config);
4536 	rxp = 0; rxq = 0;
4537 	for (lc_id = 0; lc_id < cur_fwd_config.nb_fwd_lcores; lc_id++) {
4538 		if (verbose_level > 0)
4539 			printf("  core=%d: \n", lc_id);
4540 		for (sm_id = 0; sm_id < fwd_lcores[lc_id]->stream_nb; sm_id++) {
4541 			struct fwd_stream *fs;
4542 			fs = fwd_streams[fwd_lcores[lc_id]->stream_idx + sm_id];
4543 			fs->rx_port = fwd_ports_ids[rxp];
4544 			fs->rx_queue = rxq;
4545 			fs->tx_port = fs->rx_port;
4546 			fs->tx_queue = rxq;
4547 			fs->peer_addr = fs->tx_port;
4548 			fs->retry_enabled = retry_enabled;
4549 			if (verbose_level > 0)
4550 				printf("  stream=%d port=%d rxq=%d txq=%d\n",
4551 				       sm_id, fs->rx_port, fs->rx_queue,
4552 				       fs->tx_queue);
4553 			rxq = (queueid_t) (rxq + 1);
4554 			if (rxq == nb_rxq) {
4555 				rxq = 0;
4556 				rxp = (portid_t) (rxp + 1);
4557 			}
4558 		}
4559 	}
4560 }
4561 
4562 void
4563 fwd_config_setup(void)
4564 {
4565 	struct rte_port *port;
4566 	portid_t pt_id;
4567 	unsigned int i;
4568 
4569 	cur_fwd_config.fwd_eng = cur_fwd_eng;
4570 	if (strcmp(cur_fwd_eng->fwd_mode_name, "icmpecho") == 0) {
4571 		icmp_echo_config_setup();
4572 		return;
4573 	}
4574 
4575 	if ((nb_rxq > 1) && (nb_txq > 1)){
4576 		if (dcb_config) {
4577 			for (i = 0; i < nb_fwd_ports; i++) {
4578 				pt_id = fwd_ports_ids[i];
4579 				port = &ports[pt_id];
4580 				if (!port->dcb_flag) {
4581 					fprintf(stderr,
4582 						"In DCB mode, all forwarding ports must be configured in this mode.\n");
4583 					return;
4584 				}
4585 			}
4586 			if (nb_fwd_lcores == 1) {
4587 				fprintf(stderr,
4588 					"In DCB mode,the nb forwarding cores should be larger than 1.\n");
4589 				return;
4590 			}
4591 
4592 			dcb_fwd_config_setup();
4593 		} else
4594 			rss_fwd_config_setup();
4595 	}
4596 	else
4597 		simple_fwd_config_setup();
4598 }
4599 
4600 static const char *
4601 mp_alloc_to_str(uint8_t mode)
4602 {
4603 	switch (mode) {
4604 	case MP_ALLOC_NATIVE:
4605 		return "native";
4606 	case MP_ALLOC_ANON:
4607 		return "anon";
4608 	case MP_ALLOC_XMEM:
4609 		return "xmem";
4610 	case MP_ALLOC_XMEM_HUGE:
4611 		return "xmemhuge";
4612 	case MP_ALLOC_XBUF:
4613 		return "xbuf";
4614 	default:
4615 		return "invalid";
4616 	}
4617 }
4618 
4619 void
4620 pkt_fwd_config_display(struct fwd_config *cfg)
4621 {
4622 	struct fwd_stream *fs;
4623 	lcoreid_t  lc_id;
4624 	streamid_t sm_id;
4625 
4626 	printf("%s packet forwarding%s - ports=%d - cores=%d - streams=%d - "
4627 		"NUMA support %s, MP allocation mode: %s\n",
4628 		cfg->fwd_eng->fwd_mode_name,
4629 		retry_enabled == 0 ? "" : " with retry",
4630 		cfg->nb_fwd_ports, cfg->nb_fwd_lcores, cfg->nb_fwd_streams,
4631 		numa_support == 1 ? "enabled" : "disabled",
4632 		mp_alloc_to_str(mp_alloc_type));
4633 
4634 	if (retry_enabled)
4635 		printf("TX retry num: %u, delay between TX retries: %uus\n",
4636 			burst_tx_retry_num, burst_tx_delay_time);
4637 	for (lc_id = 0; lc_id < cfg->nb_fwd_lcores; lc_id++) {
4638 		printf("Logical Core %u (socket %u) forwards packets on "
4639 		       "%d streams:",
4640 		       fwd_lcores_cpuids[lc_id],
4641 		       rte_lcore_to_socket_id(fwd_lcores_cpuids[lc_id]),
4642 		       fwd_lcores[lc_id]->stream_nb);
4643 		for (sm_id = 0; sm_id < fwd_lcores[lc_id]->stream_nb; sm_id++) {
4644 			fs = fwd_streams[fwd_lcores[lc_id]->stream_idx + sm_id];
4645 			printf("\n  RX P=%d/Q=%d (socket %u) -> TX "
4646 			       "P=%d/Q=%d (socket %u) ",
4647 			       fs->rx_port, fs->rx_queue,
4648 			       ports[fs->rx_port].socket_id,
4649 			       fs->tx_port, fs->tx_queue,
4650 			       ports[fs->tx_port].socket_id);
4651 			print_ethaddr("peer=",
4652 				      &peer_eth_addrs[fs->peer_addr]);
4653 		}
4654 		printf("\n");
4655 	}
4656 	printf("\n");
4657 }
4658 
4659 void
4660 set_fwd_eth_peer(portid_t port_id, char *peer_addr)
4661 {
4662 	struct rte_ether_addr new_peer_addr;
4663 	if (!rte_eth_dev_is_valid_port(port_id)) {
4664 		fprintf(stderr, "Error: Invalid port number %i\n", port_id);
4665 		return;
4666 	}
4667 	if (rte_ether_unformat_addr(peer_addr, &new_peer_addr) < 0) {
4668 		fprintf(stderr, "Error: Invalid ethernet address: %s\n",
4669 			peer_addr);
4670 		return;
4671 	}
4672 	peer_eth_addrs[port_id] = new_peer_addr;
4673 }
4674 
4675 int
4676 set_fwd_lcores_list(unsigned int *lcorelist, unsigned int nb_lc)
4677 {
4678 	unsigned int i;
4679 	unsigned int lcore_cpuid;
4680 	int record_now;
4681 
4682 	record_now = 0;
4683  again:
4684 	for (i = 0; i < nb_lc; i++) {
4685 		lcore_cpuid = lcorelist[i];
4686 		if (! rte_lcore_is_enabled(lcore_cpuid)) {
4687 			fprintf(stderr, "lcore %u not enabled\n", lcore_cpuid);
4688 			return -1;
4689 		}
4690 		if (lcore_cpuid == rte_get_main_lcore()) {
4691 			fprintf(stderr,
4692 				"lcore %u cannot be masked on for running packet forwarding, which is the main lcore and reserved for command line parsing only\n",
4693 				lcore_cpuid);
4694 			return -1;
4695 		}
4696 		if (record_now)
4697 			fwd_lcores_cpuids[i] = lcore_cpuid;
4698 	}
4699 	if (record_now == 0) {
4700 		record_now = 1;
4701 		goto again;
4702 	}
4703 	nb_cfg_lcores = (lcoreid_t) nb_lc;
4704 	if (nb_fwd_lcores != (lcoreid_t) nb_lc) {
4705 		printf("previous number of forwarding cores %u - changed to "
4706 		       "number of configured cores %u\n",
4707 		       (unsigned int) nb_fwd_lcores, nb_lc);
4708 		nb_fwd_lcores = (lcoreid_t) nb_lc;
4709 	}
4710 
4711 	return 0;
4712 }
4713 
4714 int
4715 set_fwd_lcores_mask(uint64_t lcoremask)
4716 {
4717 	unsigned int lcorelist[64];
4718 	unsigned int nb_lc;
4719 	unsigned int i;
4720 
4721 	if (lcoremask == 0) {
4722 		fprintf(stderr, "Invalid NULL mask of cores\n");
4723 		return -1;
4724 	}
4725 	nb_lc = 0;
4726 	for (i = 0; i < 64; i++) {
4727 		if (! ((uint64_t)(1ULL << i) & lcoremask))
4728 			continue;
4729 		lcorelist[nb_lc++] = i;
4730 	}
4731 	return set_fwd_lcores_list(lcorelist, nb_lc);
4732 }
4733 
4734 void
4735 set_fwd_lcores_number(uint16_t nb_lc)
4736 {
4737 	if (test_done == 0) {
4738 		fprintf(stderr, "Please stop forwarding first\n");
4739 		return;
4740 	}
4741 	if (nb_lc > nb_cfg_lcores) {
4742 		fprintf(stderr,
4743 			"nb fwd cores %u > %u (max. number of configured lcores) - ignored\n",
4744 			(unsigned int) nb_lc, (unsigned int) nb_cfg_lcores);
4745 		return;
4746 	}
4747 	nb_fwd_lcores = (lcoreid_t) nb_lc;
4748 	printf("Number of forwarding cores set to %u\n",
4749 	       (unsigned int) nb_fwd_lcores);
4750 }
4751 
4752 void
4753 set_fwd_ports_list(unsigned int *portlist, unsigned int nb_pt)
4754 {
4755 	unsigned int i;
4756 	portid_t port_id;
4757 	int record_now;
4758 
4759 	record_now = 0;
4760  again:
4761 	for (i = 0; i < nb_pt; i++) {
4762 		port_id = (portid_t) portlist[i];
4763 		if (port_id_is_invalid(port_id, ENABLED_WARN))
4764 			return;
4765 		if (record_now)
4766 			fwd_ports_ids[i] = port_id;
4767 	}
4768 	if (record_now == 0) {
4769 		record_now = 1;
4770 		goto again;
4771 	}
4772 	nb_cfg_ports = (portid_t) nb_pt;
4773 	if (nb_fwd_ports != (portid_t) nb_pt) {
4774 		printf("previous number of forwarding ports %u - changed to "
4775 		       "number of configured ports %u\n",
4776 		       (unsigned int) nb_fwd_ports, nb_pt);
4777 		nb_fwd_ports = (portid_t) nb_pt;
4778 	}
4779 }
4780 
4781 /**
4782  * Parse the user input and obtain the list of forwarding ports
4783  *
4784  * @param[in] list
4785  *   String containing the user input. User can specify
4786  *   in these formats 1,3,5 or 1-3 or 1-2,5 or 3,5-6.
4787  *   For example, if the user wants to use all the available
4788  *   4 ports in his system, then the input can be 0-3 or 0,1,2,3.
4789  *   If the user wants to use only the ports 1,2 then the input
4790  *   is 1,2.
4791  *   valid characters are '-' and ','
4792  * @param[out] values
4793  *   This array will be filled with a list of port IDs
4794  *   based on the user input
4795  *   Note that duplicate entries are discarded and only the first
4796  *   count entries in this array are port IDs and all the rest
4797  *   will contain default values
4798  * @param[in] maxsize
4799  *   This parameter denotes 2 things
4800  *   1) Number of elements in the values array
4801  *   2) Maximum value of each element in the values array
4802  * @return
4803  *   On success, returns total count of parsed port IDs
4804  *   On failure, returns 0
4805  */
4806 static unsigned int
4807 parse_port_list(const char *list, unsigned int *values, unsigned int maxsize)
4808 {
4809 	unsigned int count = 0;
4810 	char *end = NULL;
4811 	int min, max;
4812 	int value, i;
4813 	unsigned int marked[maxsize];
4814 
4815 	if (list == NULL || values == NULL)
4816 		return 0;
4817 
4818 	for (i = 0; i < (int)maxsize; i++)
4819 		marked[i] = 0;
4820 
4821 	min = INT_MAX;
4822 
4823 	do {
4824 		/*Remove the blank spaces if any*/
4825 		while (isblank(*list))
4826 			list++;
4827 		if (*list == '\0')
4828 			break;
4829 		errno = 0;
4830 		value = strtol(list, &end, 10);
4831 		if (errno || end == NULL)
4832 			return 0;
4833 		if (value < 0 || value >= (int)maxsize)
4834 			return 0;
4835 		while (isblank(*end))
4836 			end++;
4837 		if (*end == '-' && min == INT_MAX) {
4838 			min = value;
4839 		} else if ((*end == ',') || (*end == '\0')) {
4840 			max = value;
4841 			if (min == INT_MAX)
4842 				min = value;
4843 			for (i = min; i <= max; i++) {
4844 				if (count < maxsize) {
4845 					if (marked[i])
4846 						continue;
4847 					values[count] = i;
4848 					marked[i] = 1;
4849 					count++;
4850 				}
4851 			}
4852 			min = INT_MAX;
4853 		} else
4854 			return 0;
4855 		list = end + 1;
4856 	} while (*end != '\0');
4857 
4858 	return count;
4859 }
4860 
4861 void
4862 parse_fwd_portlist(const char *portlist)
4863 {
4864 	unsigned int portcount;
4865 	unsigned int portindex[RTE_MAX_ETHPORTS];
4866 	unsigned int i, valid_port_count = 0;
4867 
4868 	portcount = parse_port_list(portlist, portindex, RTE_MAX_ETHPORTS);
4869 	if (!portcount)
4870 		rte_exit(EXIT_FAILURE, "Invalid fwd port list\n");
4871 
4872 	/*
4873 	 * Here we verify the validity of the ports
4874 	 * and thereby calculate the total number of
4875 	 * valid ports
4876 	 */
4877 	for (i = 0; i < portcount && i < RTE_DIM(portindex); i++) {
4878 		if (rte_eth_dev_is_valid_port(portindex[i])) {
4879 			portindex[valid_port_count] = portindex[i];
4880 			valid_port_count++;
4881 		}
4882 	}
4883 
4884 	set_fwd_ports_list(portindex, valid_port_count);
4885 }
4886 
4887 void
4888 set_fwd_ports_mask(uint64_t portmask)
4889 {
4890 	unsigned int portlist[64];
4891 	unsigned int nb_pt;
4892 	unsigned int i;
4893 
4894 	if (portmask == 0) {
4895 		fprintf(stderr, "Invalid NULL mask of ports\n");
4896 		return;
4897 	}
4898 	nb_pt = 0;
4899 	RTE_ETH_FOREACH_DEV(i) {
4900 		if (! ((uint64_t)(1ULL << i) & portmask))
4901 			continue;
4902 		portlist[nb_pt++] = i;
4903 	}
4904 	set_fwd_ports_list(portlist, nb_pt);
4905 }
4906 
4907 void
4908 set_fwd_ports_number(uint16_t nb_pt)
4909 {
4910 	if (nb_pt > nb_cfg_ports) {
4911 		fprintf(stderr,
4912 			"nb fwd ports %u > %u (number of configured ports) - ignored\n",
4913 			(unsigned int) nb_pt, (unsigned int) nb_cfg_ports);
4914 		return;
4915 	}
4916 	nb_fwd_ports = (portid_t) nb_pt;
4917 	printf("Number of forwarding ports set to %u\n",
4918 	       (unsigned int) nb_fwd_ports);
4919 }
4920 
4921 int
4922 port_is_forwarding(portid_t port_id)
4923 {
4924 	unsigned int i;
4925 
4926 	if (port_id_is_invalid(port_id, ENABLED_WARN))
4927 		return -1;
4928 
4929 	for (i = 0; i < nb_fwd_ports; i++) {
4930 		if (fwd_ports_ids[i] == port_id)
4931 			return 1;
4932 	}
4933 
4934 	return 0;
4935 }
4936 
4937 void
4938 set_nb_pkt_per_burst(uint16_t nb)
4939 {
4940 	if (nb > MAX_PKT_BURST) {
4941 		fprintf(stderr,
4942 			"nb pkt per burst: %u > %u (maximum packet per burst)  ignored\n",
4943 			(unsigned int) nb, (unsigned int) MAX_PKT_BURST);
4944 		return;
4945 	}
4946 	nb_pkt_per_burst = nb;
4947 	printf("Number of packets per burst set to %u\n",
4948 	       (unsigned int) nb_pkt_per_burst);
4949 }
4950 
4951 static const char *
4952 tx_split_get_name(enum tx_pkt_split split)
4953 {
4954 	uint32_t i;
4955 
4956 	for (i = 0; i != RTE_DIM(tx_split_name); i++) {
4957 		if (tx_split_name[i].split == split)
4958 			return tx_split_name[i].name;
4959 	}
4960 	return NULL;
4961 }
4962 
4963 void
4964 set_tx_pkt_split(const char *name)
4965 {
4966 	uint32_t i;
4967 
4968 	for (i = 0; i != RTE_DIM(tx_split_name); i++) {
4969 		if (strcmp(tx_split_name[i].name, name) == 0) {
4970 			tx_pkt_split = tx_split_name[i].split;
4971 			return;
4972 		}
4973 	}
4974 	fprintf(stderr, "unknown value: \"%s\"\n", name);
4975 }
4976 
4977 int
4978 parse_fec_mode(const char *name, uint32_t *fec_capa)
4979 {
4980 	uint8_t i;
4981 
4982 	for (i = 0; i < RTE_DIM(fec_mode_name); i++) {
4983 		if (strcmp(fec_mode_name[i].name, name) == 0) {
4984 			*fec_capa =
4985 				RTE_ETH_FEC_MODE_TO_CAPA(fec_mode_name[i].mode);
4986 			return 0;
4987 		}
4988 	}
4989 	return -1;
4990 }
4991 
4992 void
4993 show_fec_capability(unsigned int num, struct rte_eth_fec_capa *speed_fec_capa)
4994 {
4995 	unsigned int i, j;
4996 
4997 	printf("FEC capabilities:\n");
4998 
4999 	for (i = 0; i < num; i++) {
5000 		printf("%s : ",
5001 			rte_eth_link_speed_to_str(speed_fec_capa[i].speed));
5002 
5003 		for (j = 0; j < RTE_DIM(fec_mode_name); j++) {
5004 			if (RTE_ETH_FEC_MODE_TO_CAPA(j) &
5005 						speed_fec_capa[i].capa)
5006 				printf("%s ", fec_mode_name[j].name);
5007 		}
5008 		printf("\n");
5009 	}
5010 }
5011 
5012 void
5013 show_rx_pkt_offsets(void)
5014 {
5015 	uint32_t i, n;
5016 
5017 	n = rx_pkt_nb_offs;
5018 	printf("Number of offsets: %u\n", n);
5019 	if (n) {
5020 		printf("Segment offsets: ");
5021 		for (i = 0; i != n - 1; i++)
5022 			printf("%hu,", rx_pkt_seg_offsets[i]);
5023 		printf("%hu\n", rx_pkt_seg_lengths[i]);
5024 	}
5025 }
5026 
5027 void
5028 set_rx_pkt_offsets(unsigned int *seg_offsets, unsigned int nb_offs)
5029 {
5030 	unsigned int i;
5031 
5032 	if (nb_offs >= MAX_SEGS_BUFFER_SPLIT) {
5033 		printf("nb segments per RX packets=%u >= "
5034 		       "MAX_SEGS_BUFFER_SPLIT - ignored\n", nb_offs);
5035 		return;
5036 	}
5037 
5038 	/*
5039 	 * No extra check here, the segment length will be checked by PMD
5040 	 * in the extended queue setup.
5041 	 */
5042 	for (i = 0; i < nb_offs; i++) {
5043 		if (seg_offsets[i] >= UINT16_MAX) {
5044 			printf("offset[%u]=%u > UINT16_MAX - give up\n",
5045 			       i, seg_offsets[i]);
5046 			return;
5047 		}
5048 	}
5049 
5050 	for (i = 0; i < nb_offs; i++)
5051 		rx_pkt_seg_offsets[i] = (uint16_t) seg_offsets[i];
5052 
5053 	rx_pkt_nb_offs = (uint8_t) nb_offs;
5054 }
5055 
5056 void
5057 show_rx_pkt_segments(void)
5058 {
5059 	uint32_t i, n;
5060 
5061 	n = rx_pkt_nb_segs;
5062 	printf("Number of segments: %u\n", n);
5063 	if (n) {
5064 		printf("Segment sizes: ");
5065 		for (i = 0; i != n - 1; i++)
5066 			printf("%hu,", rx_pkt_seg_lengths[i]);
5067 		printf("%hu\n", rx_pkt_seg_lengths[i]);
5068 	}
5069 }
5070 
5071 static const char *get_ptype_str(uint32_t ptype)
5072 {
5073 	if ((ptype & (RTE_PTYPE_L3_IPV4_EXT_UNKNOWN | RTE_PTYPE_L4_TCP)) ==
5074 		(RTE_PTYPE_L3_IPV4_EXT_UNKNOWN | RTE_PTYPE_L4_TCP))
5075 		return "ipv4-tcp";
5076 	else if ((ptype & (RTE_PTYPE_L3_IPV4_EXT_UNKNOWN | RTE_PTYPE_L4_UDP)) ==
5077 		(RTE_PTYPE_L3_IPV4_EXT_UNKNOWN | RTE_PTYPE_L4_UDP))
5078 		return "ipv4-udp";
5079 	else if ((ptype & (RTE_PTYPE_L3_IPV4_EXT_UNKNOWN | RTE_PTYPE_L4_SCTP)) ==
5080 		(RTE_PTYPE_L3_IPV4_EXT_UNKNOWN | RTE_PTYPE_L4_SCTP))
5081 		return "ipv4-sctp";
5082 	else if ((ptype & (RTE_PTYPE_L3_IPV6_EXT_UNKNOWN | RTE_PTYPE_L4_TCP)) ==
5083 		(RTE_PTYPE_L3_IPV6_EXT_UNKNOWN | RTE_PTYPE_L4_TCP))
5084 		return "ipv6-tcp";
5085 	else if ((ptype & (RTE_PTYPE_L3_IPV6_EXT_UNKNOWN | RTE_PTYPE_L4_UDP)) ==
5086 		(RTE_PTYPE_L3_IPV6_EXT_UNKNOWN | RTE_PTYPE_L4_UDP))
5087 		return "ipv6-udp";
5088 	else if ((ptype & (RTE_PTYPE_L3_IPV6_EXT_UNKNOWN | RTE_PTYPE_L4_SCTP)) ==
5089 		(RTE_PTYPE_L3_IPV6_EXT_UNKNOWN | RTE_PTYPE_L4_SCTP))
5090 		return "ipv6-sctp";
5091 	else if ((ptype & RTE_PTYPE_L4_TCP) == RTE_PTYPE_L4_TCP)
5092 		return "tcp";
5093 	else if ((ptype & RTE_PTYPE_L4_UDP) == RTE_PTYPE_L4_UDP)
5094 		return "udp";
5095 	else if ((ptype & RTE_PTYPE_L4_SCTP) == RTE_PTYPE_L4_SCTP)
5096 		return "sctp";
5097 	else if ((ptype & RTE_PTYPE_L3_IPV4_EXT_UNKNOWN) == RTE_PTYPE_L3_IPV4_EXT_UNKNOWN)
5098 		return "ipv4";
5099 	else if ((ptype & RTE_PTYPE_L3_IPV6_EXT_UNKNOWN) == RTE_PTYPE_L3_IPV6_EXT_UNKNOWN)
5100 		return "ipv6";
5101 	else if ((ptype & RTE_PTYPE_L2_ETHER) == RTE_PTYPE_L2_ETHER)
5102 		return "eth";
5103 
5104 	else if ((ptype & (RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN | RTE_PTYPE_INNER_L4_TCP)) ==
5105 		(RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN | RTE_PTYPE_INNER_L4_TCP))
5106 		return "inner-ipv4-tcp";
5107 	else if ((ptype & (RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN | RTE_PTYPE_INNER_L4_UDP)) ==
5108 		(RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN | RTE_PTYPE_INNER_L4_UDP))
5109 		return "inner-ipv4-udp";
5110 	else if ((ptype & (RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN | RTE_PTYPE_INNER_L4_SCTP)) ==
5111 		(RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN | RTE_PTYPE_INNER_L4_SCTP))
5112 		return "inner-ipv4-sctp";
5113 	else if ((ptype & (RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN | RTE_PTYPE_INNER_L4_TCP)) ==
5114 		(RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN | RTE_PTYPE_INNER_L4_TCP))
5115 		return "inner-ipv6-tcp";
5116 	else if ((ptype & (RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN | RTE_PTYPE_INNER_L4_UDP)) ==
5117 		(RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN | RTE_PTYPE_INNER_L4_UDP))
5118 		return "inner-ipv6-udp";
5119 	else if ((ptype & (RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN | RTE_PTYPE_INNER_L4_SCTP)) ==
5120 		(RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN | RTE_PTYPE_INNER_L4_SCTP))
5121 		return "inner-ipv6-sctp";
5122 	else if ((ptype & RTE_PTYPE_INNER_L4_TCP) == RTE_PTYPE_INNER_L4_TCP)
5123 		return "inner-tcp";
5124 	else if ((ptype & RTE_PTYPE_INNER_L4_UDP) == RTE_PTYPE_INNER_L4_UDP)
5125 		return "inner-udp";
5126 	else if ((ptype & RTE_PTYPE_INNER_L4_SCTP) == RTE_PTYPE_INNER_L4_SCTP)
5127 		return "inner-sctp";
5128 	else if ((ptype & RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN) ==
5129 		RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN)
5130 		return "inner-ipv4";
5131 	else if ((ptype & RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN) ==
5132 		RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN)
5133 		return "inner-ipv6";
5134 	else if ((ptype & RTE_PTYPE_INNER_L2_ETHER) == RTE_PTYPE_INNER_L2_ETHER)
5135 		return "inner-eth";
5136 	else if ((ptype & RTE_PTYPE_TUNNEL_GRENAT) == RTE_PTYPE_TUNNEL_GRENAT)
5137 		return "grenat";
5138 	else
5139 		return "unsupported";
5140 }
5141 
5142 void
5143 show_rx_pkt_hdrs(void)
5144 {
5145 	uint32_t i, n;
5146 
5147 	n = rx_pkt_nb_segs;
5148 	printf("Number of segments: %u\n", n);
5149 	if (n) {
5150 		printf("Packet segs: ");
5151 		for (i = 0; i < n - 1; i++)
5152 			printf("%s, ", get_ptype_str(rx_pkt_hdr_protos[i]));
5153 		printf("payload\n");
5154 	}
5155 }
5156 
5157 void
5158 set_rx_pkt_hdrs(unsigned int *seg_hdrs, unsigned int nb_segs)
5159 {
5160 	unsigned int i;
5161 
5162 	if (nb_segs + 1 > MAX_SEGS_BUFFER_SPLIT) {
5163 		printf("nb segments per RX packets=%u > "
5164 		       "MAX_SEGS_BUFFER_SPLIT - ignored\n", nb_segs + 1);
5165 		return;
5166 	}
5167 
5168 	memset(rx_pkt_hdr_protos, 0, sizeof(rx_pkt_hdr_protos));
5169 
5170 	for (i = 0; i < nb_segs; i++)
5171 		rx_pkt_hdr_protos[i] = (uint32_t)seg_hdrs[i];
5172 	/*
5173 	 * We calculate the number of hdrs, but payload is not included,
5174 	 * so rx_pkt_nb_segs would increase 1.
5175 	 */
5176 	rx_pkt_nb_segs = nb_segs + 1;
5177 }
5178 
5179 void
5180 set_rx_pkt_segments(unsigned int *seg_lengths, unsigned int nb_segs)
5181 {
5182 	unsigned int i;
5183 
5184 	if (nb_segs >= MAX_SEGS_BUFFER_SPLIT) {
5185 		printf("nb segments per RX packets=%u >= "
5186 		       "MAX_SEGS_BUFFER_SPLIT - ignored\n", nb_segs);
5187 		return;
5188 	}
5189 
5190 	/*
5191 	 * No extra check here, the segment length will be checked by PMD
5192 	 * in the extended queue setup.
5193 	 */
5194 	for (i = 0; i < nb_segs; i++) {
5195 		if (seg_lengths[i] >= UINT16_MAX) {
5196 			printf("length[%u]=%u > UINT16_MAX - give up\n",
5197 			       i, seg_lengths[i]);
5198 			return;
5199 		}
5200 	}
5201 
5202 	for (i = 0; i < nb_segs; i++)
5203 		rx_pkt_seg_lengths[i] = (uint16_t) seg_lengths[i];
5204 
5205 	rx_pkt_nb_segs = (uint8_t) nb_segs;
5206 }
5207 
5208 void
5209 show_tx_pkt_segments(void)
5210 {
5211 	uint32_t i, n;
5212 	const char *split;
5213 
5214 	n = tx_pkt_nb_segs;
5215 	split = tx_split_get_name(tx_pkt_split);
5216 
5217 	printf("Number of segments: %u\n", n);
5218 	printf("Segment sizes: ");
5219 	for (i = 0; i != n - 1; i++)
5220 		printf("%hu,", tx_pkt_seg_lengths[i]);
5221 	printf("%hu\n", tx_pkt_seg_lengths[i]);
5222 	printf("Split packet: %s\n", split);
5223 }
5224 
5225 static bool
5226 nb_segs_is_invalid(unsigned int nb_segs)
5227 {
5228 	uint16_t ring_size;
5229 	uint16_t queue_id;
5230 	uint16_t port_id;
5231 	int ret;
5232 
5233 	RTE_ETH_FOREACH_DEV(port_id) {
5234 		for (queue_id = 0; queue_id < nb_txq; queue_id++) {
5235 			ret = get_tx_ring_size(port_id, queue_id, &ring_size);
5236 			if (ret) {
5237 				/* Port may not be initialized yet, can't say
5238 				 * the port is invalid in this stage.
5239 				 */
5240 				continue;
5241 			}
5242 			if (ring_size < nb_segs) {
5243 				printf("nb segments per TX packets=%u >= TX "
5244 				       "queue(%u) ring_size=%u - txpkts ignored\n",
5245 				       nb_segs, queue_id, ring_size);
5246 				return true;
5247 			}
5248 		}
5249 	}
5250 
5251 	return false;
5252 }
5253 
5254 void
5255 set_tx_pkt_segments(unsigned int *seg_lengths, unsigned int nb_segs)
5256 {
5257 	uint16_t tx_pkt_len;
5258 	unsigned int i;
5259 
5260 	/*
5261 	 * For single segment settings failed check is ignored.
5262 	 * It is a very basic capability to send the single segment
5263 	 * packets, suppose it is always supported.
5264 	 */
5265 	if (nb_segs > 1 && nb_segs_is_invalid(nb_segs)) {
5266 		fprintf(stderr,
5267 			"Tx segment size(%u) is not supported - txpkts ignored\n",
5268 			nb_segs);
5269 		return;
5270 	}
5271 
5272 	if (nb_segs > RTE_MAX_SEGS_PER_PKT) {
5273 		fprintf(stderr,
5274 			"Tx segment size(%u) is bigger than max number of segment(%u)\n",
5275 			nb_segs, RTE_MAX_SEGS_PER_PKT);
5276 		return;
5277 	}
5278 
5279 	/*
5280 	 * Check that each segment length is greater or equal than
5281 	 * the mbuf data size.
5282 	 * Check also that the total packet length is greater or equal than the
5283 	 * size of an empty UDP/IP packet (sizeof(struct rte_ether_hdr) +
5284 	 * 20 + 8).
5285 	 */
5286 	tx_pkt_len = 0;
5287 	for (i = 0; i < nb_segs; i++) {
5288 		if (seg_lengths[i] > mbuf_data_size[0]) {
5289 			fprintf(stderr,
5290 				"length[%u]=%u > mbuf_data_size=%u - give up\n",
5291 				i, seg_lengths[i], mbuf_data_size[0]);
5292 			return;
5293 		}
5294 		tx_pkt_len = (uint16_t)(tx_pkt_len + seg_lengths[i]);
5295 	}
5296 	if (tx_pkt_len < (sizeof(struct rte_ether_hdr) + 20 + 8)) {
5297 		fprintf(stderr, "total packet length=%u < %d - give up\n",
5298 				(unsigned) tx_pkt_len,
5299 				(int)(sizeof(struct rte_ether_hdr) + 20 + 8));
5300 		return;
5301 	}
5302 
5303 	for (i = 0; i < nb_segs; i++)
5304 		tx_pkt_seg_lengths[i] = (uint16_t) seg_lengths[i];
5305 
5306 	tx_pkt_length  = tx_pkt_len;
5307 	tx_pkt_nb_segs = (uint8_t) nb_segs;
5308 }
5309 
5310 void
5311 show_tx_pkt_times(void)
5312 {
5313 	printf("Interburst gap: %u\n", tx_pkt_times_inter);
5314 	printf("Intraburst gap: %u\n", tx_pkt_times_intra);
5315 }
5316 
5317 void
5318 set_tx_pkt_times(unsigned int *tx_times)
5319 {
5320 	tx_pkt_times_inter = tx_times[0];
5321 	tx_pkt_times_intra = tx_times[1];
5322 }
5323 
5324 #ifdef RTE_LIB_GRO
5325 void
5326 setup_gro(const char *onoff, portid_t port_id)
5327 {
5328 	if (!rte_eth_dev_is_valid_port(port_id)) {
5329 		fprintf(stderr, "invalid port id %u\n", port_id);
5330 		return;
5331 	}
5332 	if (test_done == 0) {
5333 		fprintf(stderr,
5334 			"Before enable/disable GRO, please stop forwarding first\n");
5335 		return;
5336 	}
5337 	if (strcmp(onoff, "on") == 0) {
5338 		if (gro_ports[port_id].enable != 0) {
5339 			fprintf(stderr,
5340 				"Port %u has enabled GRO. Please disable GRO first\n",
5341 				port_id);
5342 			return;
5343 		}
5344 		if (gro_flush_cycles == GRO_DEFAULT_FLUSH_CYCLES) {
5345 			gro_ports[port_id].param.gro_types = RTE_GRO_TCP_IPV4;
5346 			gro_ports[port_id].param.max_flow_num =
5347 				GRO_DEFAULT_FLOW_NUM;
5348 			gro_ports[port_id].param.max_item_per_flow =
5349 				GRO_DEFAULT_ITEM_NUM_PER_FLOW;
5350 		}
5351 		gro_ports[port_id].enable = 1;
5352 	} else {
5353 		if (gro_ports[port_id].enable == 0) {
5354 			fprintf(stderr, "Port %u has disabled GRO\n", port_id);
5355 			return;
5356 		}
5357 		gro_ports[port_id].enable = 0;
5358 	}
5359 }
5360 
5361 void
5362 setup_gro_flush_cycles(uint8_t cycles)
5363 {
5364 	if (test_done == 0) {
5365 		fprintf(stderr,
5366 			"Before change flush interval for GRO, please stop forwarding first.\n");
5367 		return;
5368 	}
5369 
5370 	if (cycles > GRO_MAX_FLUSH_CYCLES || cycles <
5371 			GRO_DEFAULT_FLUSH_CYCLES) {
5372 		fprintf(stderr,
5373 			"The flushing cycle be in the range of 1 to %u. Revert to the default value %u.\n",
5374 			GRO_MAX_FLUSH_CYCLES, GRO_DEFAULT_FLUSH_CYCLES);
5375 		cycles = GRO_DEFAULT_FLUSH_CYCLES;
5376 	}
5377 
5378 	gro_flush_cycles = cycles;
5379 }
5380 
5381 void
5382 show_gro(portid_t port_id)
5383 {
5384 	struct rte_gro_param *param;
5385 	uint32_t max_pkts_num;
5386 
5387 	param = &gro_ports[port_id].param;
5388 
5389 	if (!rte_eth_dev_is_valid_port(port_id)) {
5390 		fprintf(stderr, "Invalid port id %u.\n", port_id);
5391 		return;
5392 	}
5393 	if (gro_ports[port_id].enable) {
5394 		printf("GRO type: TCP/IPv4\n");
5395 		if (gro_flush_cycles == GRO_DEFAULT_FLUSH_CYCLES) {
5396 			max_pkts_num = param->max_flow_num *
5397 				param->max_item_per_flow;
5398 		} else
5399 			max_pkts_num = MAX_PKT_BURST * GRO_MAX_FLUSH_CYCLES;
5400 		printf("Max number of packets to perform GRO: %u\n",
5401 				max_pkts_num);
5402 		printf("Flushing cycles: %u\n", gro_flush_cycles);
5403 	} else
5404 		printf("Port %u doesn't enable GRO.\n", port_id);
5405 }
5406 #endif /* RTE_LIB_GRO */
5407 
5408 #ifdef RTE_LIB_GSO
5409 void
5410 setup_gso(const char *mode, portid_t port_id)
5411 {
5412 	if (!rte_eth_dev_is_valid_port(port_id)) {
5413 		fprintf(stderr, "invalid port id %u\n", port_id);
5414 		return;
5415 	}
5416 	if (strcmp(mode, "on") == 0) {
5417 		if (test_done == 0) {
5418 			fprintf(stderr,
5419 				"before enabling GSO, please stop forwarding first\n");
5420 			return;
5421 		}
5422 		gso_ports[port_id].enable = 1;
5423 	} else if (strcmp(mode, "off") == 0) {
5424 		if (test_done == 0) {
5425 			fprintf(stderr,
5426 				"before disabling GSO, please stop forwarding first\n");
5427 			return;
5428 		}
5429 		gso_ports[port_id].enable = 0;
5430 	}
5431 }
5432 #endif /* RTE_LIB_GSO */
5433 
5434 char*
5435 list_pkt_forwarding_modes(void)
5436 {
5437 	static char fwd_modes[128] = "";
5438 	const char *separator = "|";
5439 	struct fwd_engine *fwd_eng;
5440 	unsigned i = 0;
5441 
5442 	if (strlen (fwd_modes) == 0) {
5443 		while ((fwd_eng = fwd_engines[i++]) != NULL) {
5444 			strncat(fwd_modes, fwd_eng->fwd_mode_name,
5445 					sizeof(fwd_modes) - strlen(fwd_modes) - 1);
5446 			strncat(fwd_modes, separator,
5447 					sizeof(fwd_modes) - strlen(fwd_modes) - 1);
5448 		}
5449 		fwd_modes[strlen(fwd_modes) - strlen(separator)] = '\0';
5450 	}
5451 
5452 	return fwd_modes;
5453 }
5454 
5455 char*
5456 list_pkt_forwarding_retry_modes(void)
5457 {
5458 	static char fwd_modes[128] = "";
5459 	const char *separator = "|";
5460 	struct fwd_engine *fwd_eng;
5461 	unsigned i = 0;
5462 
5463 	if (strlen(fwd_modes) == 0) {
5464 		while ((fwd_eng = fwd_engines[i++]) != NULL) {
5465 			if (fwd_eng == &rx_only_engine)
5466 				continue;
5467 			strncat(fwd_modes, fwd_eng->fwd_mode_name,
5468 					sizeof(fwd_modes) -
5469 					strlen(fwd_modes) - 1);
5470 			strncat(fwd_modes, separator,
5471 					sizeof(fwd_modes) -
5472 					strlen(fwd_modes) - 1);
5473 		}
5474 		fwd_modes[strlen(fwd_modes) - strlen(separator)] = '\0';
5475 	}
5476 
5477 	return fwd_modes;
5478 }
5479 
5480 void
5481 set_pkt_forwarding_mode(const char *fwd_mode_name)
5482 {
5483 	struct fwd_engine *fwd_eng;
5484 	unsigned i;
5485 
5486 	i = 0;
5487 	while ((fwd_eng = fwd_engines[i]) != NULL) {
5488 		if (! strcmp(fwd_eng->fwd_mode_name, fwd_mode_name)) {
5489 			printf("Set %s packet forwarding mode%s\n",
5490 			       fwd_mode_name,
5491 			       retry_enabled == 0 ? "" : " with retry");
5492 			cur_fwd_eng = fwd_eng;
5493 			return;
5494 		}
5495 		i++;
5496 	}
5497 	fprintf(stderr, "Invalid %s packet forwarding mode\n", fwd_mode_name);
5498 }
5499 
5500 void
5501 add_rx_dump_callbacks(portid_t portid)
5502 {
5503 	struct rte_eth_dev_info dev_info;
5504 	uint16_t queue;
5505 	int ret;
5506 
5507 	if (port_id_is_invalid(portid, ENABLED_WARN))
5508 		return;
5509 
5510 	ret = eth_dev_info_get_print_err(portid, &dev_info);
5511 	if (ret != 0)
5512 		return;
5513 
5514 	for (queue = 0; queue < dev_info.nb_rx_queues; queue++)
5515 		if (!ports[portid].rx_dump_cb[queue])
5516 			ports[portid].rx_dump_cb[queue] =
5517 				rte_eth_add_rx_callback(portid, queue,
5518 					dump_rx_pkts, NULL);
5519 }
5520 
5521 void
5522 add_tx_dump_callbacks(portid_t portid)
5523 {
5524 	struct rte_eth_dev_info dev_info;
5525 	uint16_t queue;
5526 	int ret;
5527 
5528 	if (port_id_is_invalid(portid, ENABLED_WARN))
5529 		return;
5530 
5531 	ret = eth_dev_info_get_print_err(portid, &dev_info);
5532 	if (ret != 0)
5533 		return;
5534 
5535 	for (queue = 0; queue < dev_info.nb_tx_queues; queue++)
5536 		if (!ports[portid].tx_dump_cb[queue])
5537 			ports[portid].tx_dump_cb[queue] =
5538 				rte_eth_add_tx_callback(portid, queue,
5539 							dump_tx_pkts, NULL);
5540 }
5541 
5542 void
5543 remove_rx_dump_callbacks(portid_t portid)
5544 {
5545 	struct rte_eth_dev_info dev_info;
5546 	uint16_t queue;
5547 	int ret;
5548 
5549 	if (port_id_is_invalid(portid, ENABLED_WARN))
5550 		return;
5551 
5552 	ret = eth_dev_info_get_print_err(portid, &dev_info);
5553 	if (ret != 0)
5554 		return;
5555 
5556 	for (queue = 0; queue < dev_info.nb_rx_queues; queue++)
5557 		if (ports[portid].rx_dump_cb[queue]) {
5558 			rte_eth_remove_rx_callback(portid, queue,
5559 				ports[portid].rx_dump_cb[queue]);
5560 			ports[portid].rx_dump_cb[queue] = NULL;
5561 		}
5562 }
5563 
5564 void
5565 remove_tx_dump_callbacks(portid_t portid)
5566 {
5567 	struct rte_eth_dev_info dev_info;
5568 	uint16_t queue;
5569 	int ret;
5570 
5571 	if (port_id_is_invalid(portid, ENABLED_WARN))
5572 		return;
5573 
5574 	ret = eth_dev_info_get_print_err(portid, &dev_info);
5575 	if (ret != 0)
5576 		return;
5577 
5578 	for (queue = 0; queue < dev_info.nb_tx_queues; queue++)
5579 		if (ports[portid].tx_dump_cb[queue]) {
5580 			rte_eth_remove_tx_callback(portid, queue,
5581 				ports[portid].tx_dump_cb[queue]);
5582 			ports[portid].tx_dump_cb[queue] = NULL;
5583 		}
5584 }
5585 
5586 void
5587 configure_rxtx_dump_callbacks(uint16_t verbose)
5588 {
5589 	portid_t portid;
5590 
5591 #ifndef RTE_ETHDEV_RXTX_CALLBACKS
5592 		TESTPMD_LOG(ERR, "setting rxtx callbacks is not enabled\n");
5593 		return;
5594 #endif
5595 
5596 	RTE_ETH_FOREACH_DEV(portid)
5597 	{
5598 		if (verbose == 1 || verbose > 2)
5599 			add_rx_dump_callbacks(portid);
5600 		else
5601 			remove_rx_dump_callbacks(portid);
5602 		if (verbose >= 2)
5603 			add_tx_dump_callbacks(portid);
5604 		else
5605 			remove_tx_dump_callbacks(portid);
5606 	}
5607 }
5608 
5609 void
5610 set_verbose_level(uint16_t vb_level)
5611 {
5612 	printf("Change verbose level from %u to %u\n",
5613 	       (unsigned int) verbose_level, (unsigned int) vb_level);
5614 	verbose_level = vb_level;
5615 	configure_rxtx_dump_callbacks(verbose_level);
5616 }
5617 
5618 void
5619 vlan_extend_set(portid_t port_id, int on)
5620 {
5621 	int diag;
5622 	int vlan_offload;
5623 	uint64_t port_rx_offloads = ports[port_id].dev_conf.rxmode.offloads;
5624 
5625 	if (port_id_is_invalid(port_id, ENABLED_WARN))
5626 		return;
5627 
5628 	vlan_offload = rte_eth_dev_get_vlan_offload(port_id);
5629 
5630 	if (on) {
5631 		vlan_offload |= RTE_ETH_VLAN_EXTEND_OFFLOAD;
5632 		port_rx_offloads |= RTE_ETH_RX_OFFLOAD_VLAN_EXTEND;
5633 	} else {
5634 		vlan_offload &= ~RTE_ETH_VLAN_EXTEND_OFFLOAD;
5635 		port_rx_offloads &= ~RTE_ETH_RX_OFFLOAD_VLAN_EXTEND;
5636 	}
5637 
5638 	diag = rte_eth_dev_set_vlan_offload(port_id, vlan_offload);
5639 	if (diag < 0) {
5640 		fprintf(stderr,
5641 			"rx_vlan_extend_set(port_pi=%d, on=%d) failed diag=%d\n",
5642 			port_id, on, diag);
5643 		return;
5644 	}
5645 	ports[port_id].dev_conf.rxmode.offloads = port_rx_offloads;
5646 }
5647 
5648 void
5649 rx_vlan_strip_set(portid_t port_id, int on)
5650 {
5651 	int diag;
5652 	int vlan_offload;
5653 	uint64_t port_rx_offloads = ports[port_id].dev_conf.rxmode.offloads;
5654 
5655 	if (port_id_is_invalid(port_id, ENABLED_WARN))
5656 		return;
5657 
5658 	vlan_offload = rte_eth_dev_get_vlan_offload(port_id);
5659 
5660 	if (on) {
5661 		vlan_offload |= RTE_ETH_VLAN_STRIP_OFFLOAD;
5662 		port_rx_offloads |= RTE_ETH_RX_OFFLOAD_VLAN_STRIP;
5663 	} else {
5664 		vlan_offload &= ~RTE_ETH_VLAN_STRIP_OFFLOAD;
5665 		port_rx_offloads &= ~RTE_ETH_RX_OFFLOAD_VLAN_STRIP;
5666 	}
5667 
5668 	diag = rte_eth_dev_set_vlan_offload(port_id, vlan_offload);
5669 	if (diag < 0) {
5670 		fprintf(stderr,
5671 			"%s(port_pi=%d, on=%d) failed diag=%d\n",
5672 			__func__, port_id, on, diag);
5673 		return;
5674 	}
5675 	ports[port_id].dev_conf.rxmode.offloads = port_rx_offloads;
5676 }
5677 
5678 void
5679 rx_vlan_strip_set_on_queue(portid_t port_id, uint16_t queue_id, int on)
5680 {
5681 	int diag;
5682 
5683 	if (port_id_is_invalid(port_id, ENABLED_WARN))
5684 		return;
5685 
5686 	diag = rte_eth_dev_set_vlan_strip_on_queue(port_id, queue_id, on);
5687 	if (diag < 0)
5688 		fprintf(stderr,
5689 			"%s(port_pi=%d, queue_id=%d, on=%d) failed diag=%d\n",
5690 			__func__, port_id, queue_id, on, diag);
5691 }
5692 
5693 void
5694 rx_vlan_filter_set(portid_t port_id, int on)
5695 {
5696 	int diag;
5697 	int vlan_offload;
5698 	uint64_t port_rx_offloads = ports[port_id].dev_conf.rxmode.offloads;
5699 
5700 	if (port_id_is_invalid(port_id, ENABLED_WARN))
5701 		return;
5702 
5703 	vlan_offload = rte_eth_dev_get_vlan_offload(port_id);
5704 
5705 	if (on) {
5706 		vlan_offload |= RTE_ETH_VLAN_FILTER_OFFLOAD;
5707 		port_rx_offloads |= RTE_ETH_RX_OFFLOAD_VLAN_FILTER;
5708 	} else {
5709 		vlan_offload &= ~RTE_ETH_VLAN_FILTER_OFFLOAD;
5710 		port_rx_offloads &= ~RTE_ETH_RX_OFFLOAD_VLAN_FILTER;
5711 	}
5712 
5713 	diag = rte_eth_dev_set_vlan_offload(port_id, vlan_offload);
5714 	if (diag < 0) {
5715 		fprintf(stderr,
5716 			"%s(port_pi=%d, on=%d) failed diag=%d\n",
5717 			__func__, port_id, on, diag);
5718 		return;
5719 	}
5720 	ports[port_id].dev_conf.rxmode.offloads = port_rx_offloads;
5721 }
5722 
5723 void
5724 rx_vlan_qinq_strip_set(portid_t port_id, int on)
5725 {
5726 	int diag;
5727 	int vlan_offload;
5728 	uint64_t port_rx_offloads = ports[port_id].dev_conf.rxmode.offloads;
5729 
5730 	if (port_id_is_invalid(port_id, ENABLED_WARN))
5731 		return;
5732 
5733 	vlan_offload = rte_eth_dev_get_vlan_offload(port_id);
5734 
5735 	if (on) {
5736 		vlan_offload |= RTE_ETH_QINQ_STRIP_OFFLOAD;
5737 		port_rx_offloads |= RTE_ETH_RX_OFFLOAD_QINQ_STRIP;
5738 	} else {
5739 		vlan_offload &= ~RTE_ETH_QINQ_STRIP_OFFLOAD;
5740 		port_rx_offloads &= ~RTE_ETH_RX_OFFLOAD_QINQ_STRIP;
5741 	}
5742 
5743 	diag = rte_eth_dev_set_vlan_offload(port_id, vlan_offload);
5744 	if (diag < 0) {
5745 		fprintf(stderr, "%s(port_pi=%d, on=%d) failed diag=%d\n",
5746 			__func__, port_id, on, diag);
5747 		return;
5748 	}
5749 	ports[port_id].dev_conf.rxmode.offloads = port_rx_offloads;
5750 }
5751 
5752 int
5753 rx_vft_set(portid_t port_id, uint16_t vlan_id, int on)
5754 {
5755 	int diag;
5756 
5757 	if (port_id_is_invalid(port_id, ENABLED_WARN))
5758 		return 1;
5759 	if (vlan_id_is_invalid(vlan_id))
5760 		return 1;
5761 	diag = rte_eth_dev_vlan_filter(port_id, vlan_id, on);
5762 	if (diag == 0)
5763 		return 0;
5764 	fprintf(stderr,
5765 		"rte_eth_dev_vlan_filter(port_pi=%d, vlan_id=%d, on=%d) failed diag=%d\n",
5766 		port_id, vlan_id, on, diag);
5767 	return -1;
5768 }
5769 
5770 void
5771 rx_vlan_all_filter_set(portid_t port_id, int on)
5772 {
5773 	uint16_t vlan_id;
5774 
5775 	if (port_id_is_invalid(port_id, ENABLED_WARN))
5776 		return;
5777 	for (vlan_id = 0; vlan_id < 4096; vlan_id++) {
5778 		if (rx_vft_set(port_id, vlan_id, on))
5779 			break;
5780 	}
5781 }
5782 
5783 void
5784 vlan_tpid_set(portid_t port_id, enum rte_vlan_type vlan_type, uint16_t tp_id)
5785 {
5786 	int diag;
5787 
5788 	if (port_id_is_invalid(port_id, ENABLED_WARN))
5789 		return;
5790 
5791 	diag = rte_eth_dev_set_vlan_ether_type(port_id, vlan_type, tp_id);
5792 	if (diag == 0)
5793 		return;
5794 
5795 	fprintf(stderr,
5796 		"tx_vlan_tpid_set(port_pi=%d, vlan_type=%d, tpid=%d) failed diag=%d\n",
5797 		port_id, vlan_type, tp_id, diag);
5798 }
5799 
5800 void
5801 tx_vlan_set(portid_t port_id, uint16_t vlan_id)
5802 {
5803 	struct rte_eth_dev_info dev_info;
5804 	int ret;
5805 
5806 	if (vlan_id_is_invalid(vlan_id))
5807 		return;
5808 
5809 	if (ports[port_id].dev_conf.txmode.offloads &
5810 	    RTE_ETH_TX_OFFLOAD_QINQ_INSERT) {
5811 		fprintf(stderr, "Error, as QinQ has been enabled.\n");
5812 		return;
5813 	}
5814 
5815 	ret = eth_dev_info_get_print_err(port_id, &dev_info);
5816 	if (ret != 0)
5817 		return;
5818 
5819 	if ((dev_info.tx_offload_capa & RTE_ETH_TX_OFFLOAD_VLAN_INSERT) == 0) {
5820 		fprintf(stderr,
5821 			"Error: vlan insert is not supported by port %d\n",
5822 			port_id);
5823 		return;
5824 	}
5825 
5826 	tx_vlan_reset(port_id);
5827 	ports[port_id].dev_conf.txmode.offloads |= RTE_ETH_TX_OFFLOAD_VLAN_INSERT;
5828 	ports[port_id].tx_vlan_id = vlan_id;
5829 }
5830 
5831 void
5832 tx_qinq_set(portid_t port_id, uint16_t vlan_id, uint16_t vlan_id_outer)
5833 {
5834 	struct rte_eth_dev_info dev_info;
5835 	int ret;
5836 
5837 	if (vlan_id_is_invalid(vlan_id))
5838 		return;
5839 	if (vlan_id_is_invalid(vlan_id_outer))
5840 		return;
5841 
5842 	ret = eth_dev_info_get_print_err(port_id, &dev_info);
5843 	if (ret != 0)
5844 		return;
5845 
5846 	if ((dev_info.tx_offload_capa & RTE_ETH_TX_OFFLOAD_QINQ_INSERT) == 0) {
5847 		fprintf(stderr,
5848 			"Error: qinq insert not supported by port %d\n",
5849 			port_id);
5850 		return;
5851 	}
5852 
5853 	tx_vlan_reset(port_id);
5854 	ports[port_id].dev_conf.txmode.offloads |= (RTE_ETH_TX_OFFLOAD_VLAN_INSERT |
5855 						    RTE_ETH_TX_OFFLOAD_QINQ_INSERT);
5856 	ports[port_id].tx_vlan_id = vlan_id;
5857 	ports[port_id].tx_vlan_id_outer = vlan_id_outer;
5858 }
5859 
5860 void
5861 tx_vlan_reset(portid_t port_id)
5862 {
5863 	ports[port_id].dev_conf.txmode.offloads &=
5864 				~(RTE_ETH_TX_OFFLOAD_VLAN_INSERT |
5865 				  RTE_ETH_TX_OFFLOAD_QINQ_INSERT);
5866 	ports[port_id].tx_vlan_id = 0;
5867 	ports[port_id].tx_vlan_id_outer = 0;
5868 }
5869 
5870 void
5871 tx_vlan_pvid_set(portid_t port_id, uint16_t vlan_id, int on)
5872 {
5873 	if (port_id_is_invalid(port_id, ENABLED_WARN))
5874 		return;
5875 
5876 	rte_eth_dev_set_vlan_pvid(port_id, vlan_id, on);
5877 }
5878 
5879 void
5880 set_qmap(portid_t port_id, uint8_t is_rx, uint16_t queue_id, uint8_t map_value)
5881 {
5882 	int ret;
5883 
5884 	if (port_id_is_invalid(port_id, ENABLED_WARN))
5885 		return;
5886 
5887 	if (is_rx ? (rx_queue_id_is_invalid(queue_id)) : (tx_queue_id_is_invalid(queue_id)))
5888 		return;
5889 
5890 	if (map_value >= RTE_ETHDEV_QUEUE_STAT_CNTRS) {
5891 		fprintf(stderr, "map_value not in required range 0..%d\n",
5892 			RTE_ETHDEV_QUEUE_STAT_CNTRS - 1);
5893 		return;
5894 	}
5895 
5896 	if (!is_rx) { /* tx */
5897 		ret = rte_eth_dev_set_tx_queue_stats_mapping(port_id, queue_id,
5898 							     map_value);
5899 		if (ret) {
5900 			fprintf(stderr,
5901 				"failed to set tx queue stats mapping.\n");
5902 			return;
5903 		}
5904 	} else { /* rx */
5905 		ret = rte_eth_dev_set_rx_queue_stats_mapping(port_id, queue_id,
5906 							     map_value);
5907 		if (ret) {
5908 			fprintf(stderr,
5909 				"failed to set rx queue stats mapping.\n");
5910 			return;
5911 		}
5912 	}
5913 }
5914 
5915 void
5916 set_xstats_hide_zero(uint8_t on_off)
5917 {
5918 	xstats_hide_zero = on_off;
5919 }
5920 
5921 void
5922 set_record_core_cycles(uint8_t on_off)
5923 {
5924 	record_core_cycles = on_off;
5925 }
5926 
5927 void
5928 set_record_burst_stats(uint8_t on_off)
5929 {
5930 	record_burst_stats = on_off;
5931 }
5932 
5933 uint16_t
5934 str_to_flowtype(const char *string)
5935 {
5936 	uint8_t i;
5937 
5938 	for (i = 0; i < RTE_DIM(flowtype_str_table); i++) {
5939 		if (!strcmp(flowtype_str_table[i].str, string))
5940 			return flowtype_str_table[i].ftype;
5941 	}
5942 
5943 	if (isdigit(string[0])) {
5944 		int val = atoi(string);
5945 		if (val > 0 && val < 64)
5946 			return (uint16_t)val;
5947 	}
5948 
5949 	return RTE_ETH_FLOW_UNKNOWN;
5950 }
5951 
5952 const char*
5953 flowtype_to_str(uint16_t flow_type)
5954 {
5955 	uint8_t i;
5956 
5957 	for (i = 0; i < RTE_DIM(flowtype_str_table); i++) {
5958 		if (flowtype_str_table[i].ftype == flow_type)
5959 			return flowtype_str_table[i].str;
5960 	}
5961 
5962 	return NULL;
5963 }
5964 
5965 #if defined(RTE_NET_I40E) || defined(RTE_NET_IXGBE)
5966 
5967 static inline void
5968 print_fdir_flex_payload(struct rte_eth_fdir_flex_conf *flex_conf, uint32_t num)
5969 {
5970 	struct rte_eth_flex_payload_cfg *cfg;
5971 	uint32_t i, j;
5972 
5973 	for (i = 0; i < flex_conf->nb_payloads; i++) {
5974 		cfg = &flex_conf->flex_set[i];
5975 		if (cfg->type == RTE_ETH_RAW_PAYLOAD)
5976 			printf("\n    RAW:  ");
5977 		else if (cfg->type == RTE_ETH_L2_PAYLOAD)
5978 			printf("\n    L2_PAYLOAD:  ");
5979 		else if (cfg->type == RTE_ETH_L3_PAYLOAD)
5980 			printf("\n    L3_PAYLOAD:  ");
5981 		else if (cfg->type == RTE_ETH_L4_PAYLOAD)
5982 			printf("\n    L4_PAYLOAD:  ");
5983 		else
5984 			printf("\n    UNKNOWN PAYLOAD(%u):  ", cfg->type);
5985 		for (j = 0; j < num; j++)
5986 			printf("  %-5u", cfg->src_offset[j]);
5987 	}
5988 	printf("\n");
5989 }
5990 
5991 static inline void
5992 print_fdir_flex_mask(struct rte_eth_fdir_flex_conf *flex_conf, uint32_t num)
5993 {
5994 	struct rte_eth_fdir_flex_mask *mask;
5995 	uint32_t i, j;
5996 	const char *p;
5997 
5998 	for (i = 0; i < flex_conf->nb_flexmasks; i++) {
5999 		mask = &flex_conf->flex_mask[i];
6000 		p = flowtype_to_str(mask->flow_type);
6001 		printf("\n    %s:\t", p ? p : "unknown");
6002 		for (j = 0; j < num; j++)
6003 			printf(" %02x", mask->mask[j]);
6004 	}
6005 	printf("\n");
6006 }
6007 
6008 static inline void
6009 print_fdir_flow_type(uint32_t flow_types_mask)
6010 {
6011 	int i;
6012 	const char *p;
6013 
6014 	for (i = RTE_ETH_FLOW_UNKNOWN; i < RTE_ETH_FLOW_MAX; i++) {
6015 		if (!(flow_types_mask & (1 << i)))
6016 			continue;
6017 		p = flowtype_to_str(i);
6018 		if (p)
6019 			printf(" %s", p);
6020 		else
6021 			printf(" unknown");
6022 	}
6023 	printf("\n");
6024 }
6025 
6026 static int
6027 get_fdir_info(portid_t port_id, struct rte_eth_fdir_info *fdir_info,
6028 		    struct rte_eth_fdir_stats *fdir_stat)
6029 {
6030 	int ret = -ENOTSUP;
6031 
6032 #ifdef RTE_NET_I40E
6033 	if (ret == -ENOTSUP) {
6034 		ret = rte_pmd_i40e_get_fdir_info(port_id, fdir_info);
6035 		if (!ret)
6036 			ret = rte_pmd_i40e_get_fdir_stats(port_id, fdir_stat);
6037 	}
6038 #endif
6039 #ifdef RTE_NET_IXGBE
6040 	if (ret == -ENOTSUP) {
6041 		ret = rte_pmd_ixgbe_get_fdir_info(port_id, fdir_info);
6042 		if (!ret)
6043 			ret = rte_pmd_ixgbe_get_fdir_stats(port_id, fdir_stat);
6044 	}
6045 #endif
6046 	switch (ret) {
6047 	case 0:
6048 		break;
6049 	case -ENOTSUP:
6050 		fprintf(stderr, "\n FDIR is not supported on port %-2d\n",
6051 			port_id);
6052 		break;
6053 	default:
6054 		fprintf(stderr, "programming error: (%s)\n", strerror(-ret));
6055 		break;
6056 	}
6057 	return ret;
6058 }
6059 
6060 void
6061 fdir_get_infos(portid_t port_id)
6062 {
6063 	struct rte_eth_fdir_stats fdir_stat;
6064 	struct rte_eth_fdir_info fdir_info;
6065 
6066 	static const char *fdir_stats_border = "########################";
6067 
6068 	if (port_id_is_invalid(port_id, ENABLED_WARN))
6069 		return;
6070 
6071 	memset(&fdir_info, 0, sizeof(fdir_info));
6072 	memset(&fdir_stat, 0, sizeof(fdir_stat));
6073 	if (get_fdir_info(port_id, &fdir_info, &fdir_stat))
6074 		return;
6075 
6076 	printf("\n  %s FDIR infos for port %-2d     %s\n",
6077 	       fdir_stats_border, port_id, fdir_stats_border);
6078 	printf("  MODE: ");
6079 	if (fdir_info.mode == RTE_FDIR_MODE_PERFECT)
6080 		printf("  PERFECT\n");
6081 	else if (fdir_info.mode == RTE_FDIR_MODE_PERFECT_MAC_VLAN)
6082 		printf("  PERFECT-MAC-VLAN\n");
6083 	else if (fdir_info.mode == RTE_FDIR_MODE_PERFECT_TUNNEL)
6084 		printf("  PERFECT-TUNNEL\n");
6085 	else if (fdir_info.mode == RTE_FDIR_MODE_SIGNATURE)
6086 		printf("  SIGNATURE\n");
6087 	else
6088 		printf("  DISABLE\n");
6089 	if (fdir_info.mode != RTE_FDIR_MODE_PERFECT_MAC_VLAN
6090 		&& fdir_info.mode != RTE_FDIR_MODE_PERFECT_TUNNEL) {
6091 		printf("  SUPPORTED FLOW TYPE: ");
6092 		print_fdir_flow_type(fdir_info.flow_types_mask[0]);
6093 	}
6094 	printf("  FLEX PAYLOAD INFO:\n");
6095 	printf("  max_len:       %-10"PRIu32"  payload_limit: %-10"PRIu32"\n"
6096 	       "  payload_unit:  %-10"PRIu32"  payload_seg:   %-10"PRIu32"\n"
6097 	       "  bitmask_unit:  %-10"PRIu32"  bitmask_num:   %-10"PRIu32"\n",
6098 		fdir_info.max_flexpayload, fdir_info.flex_payload_limit,
6099 		fdir_info.flex_payload_unit,
6100 		fdir_info.max_flex_payload_segment_num,
6101 		fdir_info.flex_bitmask_unit, fdir_info.max_flex_bitmask_num);
6102 	if (fdir_info.flex_conf.nb_payloads > 0) {
6103 		printf("  FLEX PAYLOAD SRC OFFSET:");
6104 		print_fdir_flex_payload(&fdir_info.flex_conf, fdir_info.max_flexpayload);
6105 	}
6106 	if (fdir_info.flex_conf.nb_flexmasks > 0) {
6107 		printf("  FLEX MASK CFG:");
6108 		print_fdir_flex_mask(&fdir_info.flex_conf, fdir_info.max_flexpayload);
6109 	}
6110 	printf("  guarant_count: %-10"PRIu32"  best_count:    %"PRIu32"\n",
6111 	       fdir_stat.guarant_cnt, fdir_stat.best_cnt);
6112 	printf("  guarant_space: %-10"PRIu32"  best_space:    %"PRIu32"\n",
6113 	       fdir_info.guarant_spc, fdir_info.best_spc);
6114 	printf("  collision:     %-10"PRIu32"  free:          %"PRIu32"\n"
6115 	       "  maxhash:       %-10"PRIu32"  maxlen:        %"PRIu32"\n"
6116 	       "  add:	         %-10"PRIu64"  remove:        %"PRIu64"\n"
6117 	       "  f_add:         %-10"PRIu64"  f_remove:      %"PRIu64"\n",
6118 	       fdir_stat.collision, fdir_stat.free,
6119 	       fdir_stat.maxhash, fdir_stat.maxlen,
6120 	       fdir_stat.add, fdir_stat.remove,
6121 	       fdir_stat.f_add, fdir_stat.f_remove);
6122 	printf("  %s############################%s\n",
6123 	       fdir_stats_border, fdir_stats_border);
6124 }
6125 
6126 #endif /* RTE_NET_I40E || RTE_NET_IXGBE */
6127 
6128 void
6129 set_vf_traffic(portid_t port_id, uint8_t is_rx, uint16_t vf, uint8_t on)
6130 {
6131 #ifdef RTE_NET_IXGBE
6132 	int diag;
6133 
6134 	if (is_rx)
6135 		diag = rte_pmd_ixgbe_set_vf_rx(port_id, vf, on);
6136 	else
6137 		diag = rte_pmd_ixgbe_set_vf_tx(port_id, vf, on);
6138 
6139 	if (diag == 0)
6140 		return;
6141 	fprintf(stderr,
6142 		"rte_pmd_ixgbe_set_vf_%s for port_id=%d failed diag=%d\n",
6143 		is_rx ? "rx" : "tx", port_id, diag);
6144 	return;
6145 #endif
6146 	fprintf(stderr, "VF %s setting not supported for port %d\n",
6147 		is_rx ? "Rx" : "Tx", port_id);
6148 	RTE_SET_USED(vf);
6149 	RTE_SET_USED(on);
6150 }
6151 
6152 int
6153 set_queue_rate_limit(portid_t port_id, uint16_t queue_idx, uint32_t rate)
6154 {
6155 	int diag;
6156 	struct rte_eth_link link;
6157 	int ret;
6158 
6159 	if (port_id_is_invalid(port_id, ENABLED_WARN))
6160 		return 1;
6161 	ret = eth_link_get_nowait_print_err(port_id, &link);
6162 	if (ret < 0)
6163 		return 1;
6164 	if (link.link_speed != RTE_ETH_SPEED_NUM_UNKNOWN &&
6165 	    rate > link.link_speed) {
6166 		fprintf(stderr,
6167 			"Invalid rate value:%u bigger than link speed: %u\n",
6168 			rate, link.link_speed);
6169 		return 1;
6170 	}
6171 	diag = rte_eth_set_queue_rate_limit(port_id, queue_idx, rate);
6172 	if (diag == 0)
6173 		return diag;
6174 	fprintf(stderr,
6175 		"rte_eth_set_queue_rate_limit for port_id=%d failed diag=%d\n",
6176 		port_id, diag);
6177 	return diag;
6178 }
6179 
6180 int
6181 set_vf_rate_limit(portid_t port_id, uint16_t vf, uint32_t rate, uint64_t q_msk)
6182 {
6183 	int diag = -ENOTSUP;
6184 
6185 	RTE_SET_USED(vf);
6186 	RTE_SET_USED(rate);
6187 	RTE_SET_USED(q_msk);
6188 
6189 #ifdef RTE_NET_IXGBE
6190 	if (diag == -ENOTSUP)
6191 		diag = rte_pmd_ixgbe_set_vf_rate_limit(port_id, vf, rate,
6192 						       q_msk);
6193 #endif
6194 #ifdef RTE_NET_BNXT
6195 	if (diag == -ENOTSUP)
6196 		diag = rte_pmd_bnxt_set_vf_rate_limit(port_id, vf, rate, q_msk);
6197 #endif
6198 	if (diag == 0)
6199 		return diag;
6200 
6201 	fprintf(stderr,
6202 		"%s for port_id=%d failed diag=%d\n",
6203 		__func__, port_id, diag);
6204 	return diag;
6205 }
6206 
6207 int
6208 set_rxq_avail_thresh(portid_t port_id, uint16_t queue_id, uint8_t avail_thresh)
6209 {
6210 	if (port_id_is_invalid(port_id, ENABLED_WARN))
6211 		return -EINVAL;
6212 
6213 	return rte_eth_rx_avail_thresh_set(port_id, queue_id, avail_thresh);
6214 }
6215 
6216 /*
6217  * Functions to manage the set of filtered Multicast MAC addresses.
6218  *
6219  * A pool of filtered multicast MAC addresses is associated with each port.
6220  * The pool is allocated in chunks of MCAST_POOL_INC multicast addresses.
6221  * The address of the pool and the number of valid multicast MAC addresses
6222  * recorded in the pool are stored in the fields "mc_addr_pool" and
6223  * "mc_addr_nb" of the "rte_port" data structure.
6224  *
6225  * The function "rte_eth_dev_set_mc_addr_list" of the PMDs API imposes
6226  * to be supplied a contiguous array of multicast MAC addresses.
6227  * To comply with this constraint, the set of multicast addresses recorded
6228  * into the pool are systematically compacted at the beginning of the pool.
6229  * Hence, when a multicast address is removed from the pool, all following
6230  * addresses, if any, are copied back to keep the set contiguous.
6231  */
6232 #define MCAST_POOL_INC 32
6233 
6234 static int
6235 mcast_addr_pool_extend(struct rte_port *port)
6236 {
6237 	struct rte_ether_addr *mc_pool;
6238 	size_t mc_pool_size;
6239 
6240 	/*
6241 	 * If a free entry is available at the end of the pool, just
6242 	 * increment the number of recorded multicast addresses.
6243 	 */
6244 	if ((port->mc_addr_nb % MCAST_POOL_INC) != 0) {
6245 		port->mc_addr_nb++;
6246 		return 0;
6247 	}
6248 
6249 	/*
6250 	 * [re]allocate a pool with MCAST_POOL_INC more entries.
6251 	 * The previous test guarantees that port->mc_addr_nb is a multiple
6252 	 * of MCAST_POOL_INC.
6253 	 */
6254 	mc_pool_size = sizeof(struct rte_ether_addr) * (port->mc_addr_nb +
6255 						    MCAST_POOL_INC);
6256 	mc_pool = (struct rte_ether_addr *) realloc(port->mc_addr_pool,
6257 						mc_pool_size);
6258 	if (mc_pool == NULL) {
6259 		fprintf(stderr,
6260 			"allocation of pool of %u multicast addresses failed\n",
6261 			port->mc_addr_nb + MCAST_POOL_INC);
6262 		return -ENOMEM;
6263 	}
6264 
6265 	port->mc_addr_pool = mc_pool;
6266 	port->mc_addr_nb++;
6267 	return 0;
6268 
6269 }
6270 
6271 static void
6272 mcast_addr_pool_append(struct rte_port *port, struct rte_ether_addr *mc_addr)
6273 {
6274 	if (mcast_addr_pool_extend(port) != 0)
6275 		return;
6276 	rte_ether_addr_copy(mc_addr, &port->mc_addr_pool[port->mc_addr_nb - 1]);
6277 }
6278 
6279 static void
6280 mcast_addr_pool_remove(struct rte_port *port, uint32_t addr_idx)
6281 {
6282 	port->mc_addr_nb--;
6283 	if (addr_idx == port->mc_addr_nb) {
6284 		/* No need to recompact the set of multicast addresses. */
6285 		if (port->mc_addr_nb == 0) {
6286 			/* free the pool of multicast addresses. */
6287 			free(port->mc_addr_pool);
6288 			port->mc_addr_pool = NULL;
6289 		}
6290 		return;
6291 	}
6292 	memmove(&port->mc_addr_pool[addr_idx],
6293 		&port->mc_addr_pool[addr_idx + 1],
6294 		sizeof(struct rte_ether_addr) * (port->mc_addr_nb - addr_idx));
6295 }
6296 
6297 int
6298 mcast_addr_pool_destroy(portid_t port_id)
6299 {
6300 	struct rte_port *port;
6301 
6302 	if (port_id_is_invalid(port_id, ENABLED_WARN) ||
6303 	    port_id == (portid_t)RTE_PORT_ALL)
6304 		return -EINVAL;
6305 	port = &ports[port_id];
6306 
6307 	if (port->mc_addr_nb != 0) {
6308 		/* free the pool of multicast addresses. */
6309 		free(port->mc_addr_pool);
6310 		port->mc_addr_pool = NULL;
6311 		port->mc_addr_nb = 0;
6312 	}
6313 	return 0;
6314 }
6315 
6316 static int
6317 eth_port_multicast_addr_list_set(portid_t port_id)
6318 {
6319 	struct rte_port *port;
6320 	int diag;
6321 
6322 	port = &ports[port_id];
6323 	diag = rte_eth_dev_set_mc_addr_list(port_id, port->mc_addr_pool,
6324 					    port->mc_addr_nb);
6325 	if (diag < 0)
6326 		fprintf(stderr,
6327 			"rte_eth_dev_set_mc_addr_list(port=%d, nb=%u) failed. diag=%d\n",
6328 			port_id, port->mc_addr_nb, diag);
6329 
6330 	return diag;
6331 }
6332 
6333 void
6334 mcast_addr_add(portid_t port_id, struct rte_ether_addr *mc_addr)
6335 {
6336 	struct rte_port *port;
6337 	uint32_t i;
6338 
6339 	if (port_id_is_invalid(port_id, ENABLED_WARN))
6340 		return;
6341 
6342 	port = &ports[port_id];
6343 
6344 	/*
6345 	 * Check that the added multicast MAC address is not already recorded
6346 	 * in the pool of multicast addresses.
6347 	 */
6348 	for (i = 0; i < port->mc_addr_nb; i++) {
6349 		if (rte_is_same_ether_addr(mc_addr, &port->mc_addr_pool[i])) {
6350 			fprintf(stderr,
6351 				"multicast address already filtered by port\n");
6352 			return;
6353 		}
6354 	}
6355 
6356 	mcast_addr_pool_append(port, mc_addr);
6357 	if (eth_port_multicast_addr_list_set(port_id) < 0)
6358 		/* Rollback on failure, remove the address from the pool */
6359 		mcast_addr_pool_remove(port, i);
6360 }
6361 
6362 void
6363 mcast_addr_remove(portid_t port_id, struct rte_ether_addr *mc_addr)
6364 {
6365 	struct rte_port *port;
6366 	uint32_t i;
6367 
6368 	if (port_id_is_invalid(port_id, ENABLED_WARN))
6369 		return;
6370 
6371 	port = &ports[port_id];
6372 
6373 	/*
6374 	 * Search the pool of multicast MAC addresses for the removed address.
6375 	 */
6376 	for (i = 0; i < port->mc_addr_nb; i++) {
6377 		if (rte_is_same_ether_addr(mc_addr, &port->mc_addr_pool[i]))
6378 			break;
6379 	}
6380 	if (i == port->mc_addr_nb) {
6381 		fprintf(stderr, "multicast address not filtered by port %d\n",
6382 			port_id);
6383 		return;
6384 	}
6385 
6386 	mcast_addr_pool_remove(port, i);
6387 	if (eth_port_multicast_addr_list_set(port_id) < 0)
6388 		/* Rollback on failure, add the address back into the pool */
6389 		mcast_addr_pool_append(port, mc_addr);
6390 }
6391 
6392 void
6393 port_dcb_info_display(portid_t port_id)
6394 {
6395 	struct rte_eth_dcb_info dcb_info;
6396 	uint16_t i;
6397 	int ret;
6398 	static const char *border = "================";
6399 
6400 	if (port_id_is_invalid(port_id, ENABLED_WARN))
6401 		return;
6402 
6403 	ret = rte_eth_dev_get_dcb_info(port_id, &dcb_info);
6404 	if (ret) {
6405 		fprintf(stderr, "\n Failed to get dcb infos on port %-2d\n",
6406 			port_id);
6407 		return;
6408 	}
6409 	printf("\n  %s DCB infos for port %-2d  %s\n", border, port_id, border);
6410 	printf("  TC NUMBER: %d\n", dcb_info.nb_tcs);
6411 	printf("\n  TC :        ");
6412 	for (i = 0; i < dcb_info.nb_tcs; i++)
6413 		printf("\t%4d", i);
6414 	printf("\n  Priority :  ");
6415 	for (i = 0; i < dcb_info.nb_tcs; i++)
6416 		printf("\t%4d", dcb_info.prio_tc[i]);
6417 	printf("\n  BW percent :");
6418 	for (i = 0; i < dcb_info.nb_tcs; i++)
6419 		printf("\t%4d%%", dcb_info.tc_bws[i]);
6420 	printf("\n  RXQ base :  ");
6421 	for (i = 0; i < dcb_info.nb_tcs; i++)
6422 		printf("\t%4d", dcb_info.tc_queue.tc_rxq[0][i].base);
6423 	printf("\n  RXQ number :");
6424 	for (i = 0; i < dcb_info.nb_tcs; i++)
6425 		printf("\t%4d", dcb_info.tc_queue.tc_rxq[0][i].nb_queue);
6426 	printf("\n  TXQ base :  ");
6427 	for (i = 0; i < dcb_info.nb_tcs; i++)
6428 		printf("\t%4d", dcb_info.tc_queue.tc_txq[0][i].base);
6429 	printf("\n  TXQ number :");
6430 	for (i = 0; i < dcb_info.nb_tcs; i++)
6431 		printf("\t%4d", dcb_info.tc_queue.tc_txq[0][i].nb_queue);
6432 	printf("\n");
6433 }
6434 
6435 uint8_t *
6436 open_file(const char *file_path, uint32_t *size)
6437 {
6438 	int fd = open(file_path, O_RDONLY);
6439 	off_t pkg_size;
6440 	uint8_t *buf = NULL;
6441 	int ret = 0;
6442 	struct stat st_buf;
6443 
6444 	if (size)
6445 		*size = 0;
6446 
6447 	if (fd == -1) {
6448 		fprintf(stderr, "%s: Failed to open %s\n", __func__, file_path);
6449 		return buf;
6450 	}
6451 
6452 	if ((fstat(fd, &st_buf) != 0) || (!S_ISREG(st_buf.st_mode))) {
6453 		close(fd);
6454 		fprintf(stderr, "%s: File operations failed\n", __func__);
6455 		return buf;
6456 	}
6457 
6458 	pkg_size = st_buf.st_size;
6459 	if (pkg_size < 0) {
6460 		close(fd);
6461 		fprintf(stderr, "%s: File operations failed\n", __func__);
6462 		return buf;
6463 	}
6464 
6465 	buf = (uint8_t *)malloc(pkg_size);
6466 	if (!buf) {
6467 		close(fd);
6468 		fprintf(stderr, "%s: Failed to malloc memory\n", __func__);
6469 		return buf;
6470 	}
6471 
6472 	ret = read(fd, buf, pkg_size);
6473 	if (ret < 0) {
6474 		close(fd);
6475 		fprintf(stderr, "%s: File read operation failed\n", __func__);
6476 		close_file(buf);
6477 		return NULL;
6478 	}
6479 
6480 	if (size)
6481 		*size = pkg_size;
6482 
6483 	close(fd);
6484 
6485 	return buf;
6486 }
6487 
6488 int
6489 save_file(const char *file_path, uint8_t *buf, uint32_t size)
6490 {
6491 	FILE *fh = fopen(file_path, "wb");
6492 
6493 	if (fh == NULL) {
6494 		fprintf(stderr, "%s: Failed to open %s\n", __func__, file_path);
6495 		return -1;
6496 	}
6497 
6498 	if (fwrite(buf, 1, size, fh) != size) {
6499 		fclose(fh);
6500 		fprintf(stderr, "%s: File write operation failed\n", __func__);
6501 		return -1;
6502 	}
6503 
6504 	fclose(fh);
6505 
6506 	return 0;
6507 }
6508 
6509 int
6510 close_file(uint8_t *buf)
6511 {
6512 	if (buf) {
6513 		free((void *)buf);
6514 		return 0;
6515 	}
6516 
6517 	return -1;
6518 }
6519 
6520 void
6521 show_macs(portid_t port_id)
6522 {
6523 	char buf[RTE_ETHER_ADDR_FMT_SIZE];
6524 	struct rte_eth_dev_info dev_info;
6525 	int32_t i, rc, num_macs = 0;
6526 
6527 	if (eth_dev_info_get_print_err(port_id, &dev_info))
6528 		return;
6529 
6530 	struct rte_ether_addr addr[dev_info.max_mac_addrs];
6531 	rc = rte_eth_macaddrs_get(port_id, addr, dev_info.max_mac_addrs);
6532 	if (rc < 0)
6533 		return;
6534 
6535 	for (i = 0; i < rc; i++) {
6536 
6537 		/* skip zero address */
6538 		if (rte_is_zero_ether_addr(&addr[i]))
6539 			continue;
6540 
6541 		num_macs++;
6542 	}
6543 
6544 	printf("Number of MAC address added: %d\n", num_macs);
6545 
6546 	for (i = 0; i < rc; i++) {
6547 
6548 		/* skip zero address */
6549 		if (rte_is_zero_ether_addr(&addr[i]))
6550 			continue;
6551 
6552 		rte_ether_format_addr(buf, RTE_ETHER_ADDR_FMT_SIZE, &addr[i]);
6553 		printf("  %s\n", buf);
6554 	}
6555 }
6556 
6557 void
6558 show_mcast_macs(portid_t port_id)
6559 {
6560 	char buf[RTE_ETHER_ADDR_FMT_SIZE];
6561 	struct rte_ether_addr *addr;
6562 	struct rte_port *port;
6563 	uint32_t i;
6564 
6565 	port = &ports[port_id];
6566 
6567 	printf("Number of Multicast MAC address added: %d\n", port->mc_addr_nb);
6568 
6569 	for (i = 0; i < port->mc_addr_nb; i++) {
6570 		addr = &port->mc_addr_pool[i];
6571 
6572 		rte_ether_format_addr(buf, RTE_ETHER_ADDR_FMT_SIZE, addr);
6573 		printf("  %s\n", buf);
6574 	}
6575 }
6576