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