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