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