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