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