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