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