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