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