xref: /dpdk/app/test-pmd/testpmd.c (revision 37b68fa1445cb6fe287178607bac6fa3d90b835f)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2017 Intel Corporation
3  */
4 
5 #include <stdarg.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <signal.h>
9 #include <string.h>
10 #include <time.h>
11 #include <fcntl.h>
12 #ifndef RTE_EXEC_ENV_WINDOWS
13 #include <sys/mman.h>
14 #include <sys/select.h>
15 #endif
16 #include <sys/types.h>
17 #include <errno.h>
18 #include <stdbool.h>
19 
20 #include <sys/queue.h>
21 #include <sys/stat.h>
22 
23 #include <stdint.h>
24 #include <unistd.h>
25 #include <inttypes.h>
26 
27 #include <rte_common.h>
28 #include <rte_errno.h>
29 #include <rte_byteorder.h>
30 #include <rte_log.h>
31 #include <rte_debug.h>
32 #include <rte_cycles.h>
33 #include <rte_memory.h>
34 #include <rte_memcpy.h>
35 #include <rte_launch.h>
36 #include <rte_bus.h>
37 #include <rte_eal.h>
38 #include <rte_alarm.h>
39 #include <rte_per_lcore.h>
40 #include <rte_lcore.h>
41 #include <rte_branch_prediction.h>
42 #include <rte_mempool.h>
43 #include <rte_malloc.h>
44 #include <rte_mbuf.h>
45 #include <rte_mbuf_pool_ops.h>
46 #include <rte_interrupts.h>
47 #include <rte_ether.h>
48 #include <rte_ethdev.h>
49 #include <rte_dev.h>
50 #include <rte_string_fns.h>
51 #ifdef RTE_NET_IXGBE
52 #include <rte_pmd_ixgbe.h>
53 #endif
54 #ifdef RTE_LIB_PDUMP
55 #include <rte_pdump.h>
56 #endif
57 #include <rte_flow.h>
58 #ifdef RTE_LIB_METRICS
59 #include <rte_metrics.h>
60 #endif
61 #ifdef RTE_LIB_BITRATESTATS
62 #include <rte_bitrate.h>
63 #endif
64 #ifdef RTE_LIB_LATENCYSTATS
65 #include <rte_latencystats.h>
66 #endif
67 #ifdef RTE_EXEC_ENV_WINDOWS
68 #include <process.h>
69 #endif
70 #ifdef RTE_NET_BOND
71 #include <rte_eth_bond.h>
72 #endif
73 #ifdef RTE_NET_MLX5
74 #include "mlx5_testpmd.h"
75 #endif
76 
77 #include "testpmd.h"
78 
79 #ifndef MAP_HUGETLB
80 /* FreeBSD may not have MAP_HUGETLB (in fact, it probably doesn't) */
81 #define HUGE_FLAG (0x40000)
82 #else
83 #define HUGE_FLAG MAP_HUGETLB
84 #endif
85 
86 #ifndef MAP_HUGE_SHIFT
87 /* older kernels (or FreeBSD) will not have this define */
88 #define HUGE_SHIFT (26)
89 #else
90 #define HUGE_SHIFT MAP_HUGE_SHIFT
91 #endif
92 
93 #define EXTMEM_HEAP_NAME "extmem"
94 /*
95  * Zone size with the malloc overhead (max of debug and release variants)
96  * must fit into the smallest supported hugepage size (2M),
97  * so that an IOVA-contiguous zone of this size can always be allocated
98  * if there are free 2M hugepages.
99  */
100 #define EXTBUF_ZONE_SIZE (RTE_PGSIZE_2M - 4 * RTE_CACHE_LINE_SIZE)
101 
102 uint16_t verbose_level = 0; /**< Silent by default. */
103 int testpmd_logtype; /**< Log type for testpmd logs */
104 
105 /* use main core for command line ? */
106 uint8_t interactive = 0;
107 uint8_t auto_start = 0;
108 uint8_t tx_first;
109 char cmdline_filename[PATH_MAX] = {0};
110 
111 /*
112  * NUMA support configuration.
113  * When set, the NUMA support attempts to dispatch the allocation of the
114  * RX and TX memory rings, and of the DMA memory buffers (mbufs) for the
115  * probed ports among the CPU sockets 0 and 1.
116  * Otherwise, all memory is allocated from CPU socket 0.
117  */
118 uint8_t numa_support = 1; /**< numa enabled by default */
119 
120 /*
121  * In UMA mode,all memory is allocated from socket 0 if --socket-num is
122  * not configured.
123  */
124 uint8_t socket_num = UMA_NO_CONFIG;
125 
126 /*
127  * Select mempool allocation type:
128  * - native: use regular DPDK memory
129  * - anon: use regular DPDK memory to create mempool, but populate using
130  *         anonymous memory (may not be IOVA-contiguous)
131  * - xmem: use externally allocated hugepage memory
132  */
133 uint8_t mp_alloc_type = MP_ALLOC_NATIVE;
134 
135 /*
136  * Store specified sockets on which memory pool to be used by ports
137  * is allocated.
138  */
139 uint8_t port_numa[RTE_MAX_ETHPORTS];
140 
141 /*
142  * Store specified sockets on which RX ring to be used by ports
143  * is allocated.
144  */
145 uint8_t rxring_numa[RTE_MAX_ETHPORTS];
146 
147 /*
148  * Store specified sockets on which TX ring to be used by ports
149  * is allocated.
150  */
151 uint8_t txring_numa[RTE_MAX_ETHPORTS];
152 
153 /*
154  * Record the Ethernet address of peer target ports to which packets are
155  * forwarded.
156  * Must be instantiated with the ethernet addresses of peer traffic generator
157  * ports.
158  */
159 struct rte_ether_addr peer_eth_addrs[RTE_MAX_ETHPORTS];
160 portid_t nb_peer_eth_addrs = 0;
161 
162 /*
163  * Probed Target Environment.
164  */
165 struct rte_port *ports;	       /**< For all probed ethernet ports. */
166 portid_t nb_ports;             /**< Number of probed ethernet ports. */
167 struct fwd_lcore **fwd_lcores; /**< For all probed logical cores. */
168 lcoreid_t nb_lcores;           /**< Number of probed logical cores. */
169 
170 portid_t ports_ids[RTE_MAX_ETHPORTS]; /**< Store all port ids. */
171 
172 /*
173  * Test Forwarding Configuration.
174  *    nb_fwd_lcores <= nb_cfg_lcores <= nb_lcores
175  *    nb_fwd_ports  <= nb_cfg_ports  <= nb_ports
176  */
177 lcoreid_t nb_cfg_lcores; /**< Number of configured logical cores. */
178 lcoreid_t nb_fwd_lcores; /**< Number of forwarding logical cores. */
179 portid_t  nb_cfg_ports;  /**< Number of configured ports. */
180 portid_t  nb_fwd_ports;  /**< Number of forwarding ports. */
181 
182 unsigned int fwd_lcores_cpuids[RTE_MAX_LCORE]; /**< CPU ids configuration. */
183 portid_t fwd_ports_ids[RTE_MAX_ETHPORTS];      /**< Port ids configuration. */
184 
185 struct fwd_stream **fwd_streams; /**< For each RX queue of each port. */
186 streamid_t nb_fwd_streams;       /**< Is equal to (nb_ports * nb_rxq). */
187 
188 /*
189  * Forwarding engines.
190  */
191 struct fwd_engine * fwd_engines[] = {
192 	&io_fwd_engine,
193 	&mac_fwd_engine,
194 	&mac_swap_engine,
195 	&flow_gen_engine,
196 	&rx_only_engine,
197 	&tx_only_engine,
198 	&csum_fwd_engine,
199 	&icmp_echo_engine,
200 	&noisy_vnf_engine,
201 	&five_tuple_swap_fwd_engine,
202 	&recycle_mbufs_engine,
203 #ifdef RTE_LIBRTE_IEEE1588
204 	&ieee1588_fwd_engine,
205 #endif
206 	&shared_rxq_engine,
207 	NULL,
208 };
209 
210 struct rte_mempool *mempools[RTE_MAX_NUMA_NODES * MAX_SEGS_BUFFER_SPLIT];
211 uint16_t mempool_flags;
212 
213 struct fwd_config cur_fwd_config;
214 struct fwd_engine *cur_fwd_eng = &io_fwd_engine; /**< IO mode by default. */
215 uint32_t retry_enabled;
216 uint32_t burst_tx_delay_time = BURST_TX_WAIT_US;
217 uint32_t burst_tx_retry_num = BURST_TX_RETRIES;
218 
219 uint32_t mbuf_data_size_n = 1; /* Number of specified mbuf sizes. */
220 uint16_t mbuf_data_size[MAX_SEGS_BUFFER_SPLIT] = {
221 	DEFAULT_MBUF_DATA_SIZE
222 }; /**< Mbuf data space size. */
223 uint32_t param_total_num_mbufs = 0;  /**< number of mbufs in all pools - if
224                                       * specified on command-line. */
225 uint16_t stats_period; /**< Period to show statistics (disabled by default) */
226 
227 /** Extended statistics to show. */
228 struct rte_eth_xstat_name *xstats_display;
229 
230 unsigned int xstats_display_num; /**< Size of extended statistics to show */
231 
232 /*
233  * In container, it cannot terminate the process which running with 'stats-period'
234  * option. Set flag to exit stats period loop after received SIGINT/SIGTERM.
235  */
236 volatile uint8_t f_quit;
237 uint8_t cl_quit; /* Quit testpmd from cmdline. */
238 
239 /*
240  * Max Rx frame size, set by '--max-pkt-len' parameter.
241  */
242 uint32_t max_rx_pkt_len;
243 
244 /*
245  * Configuration of packet segments used to scatter received packets
246  * if some of split features is configured.
247  */
248 uint16_t rx_pkt_seg_lengths[MAX_SEGS_BUFFER_SPLIT];
249 uint8_t  rx_pkt_nb_segs; /**< Number of segments to split */
250 uint16_t rx_pkt_seg_offsets[MAX_SEGS_BUFFER_SPLIT];
251 uint8_t  rx_pkt_nb_offs; /**< Number of specified offsets */
252 uint32_t rx_pkt_hdr_protos[MAX_SEGS_BUFFER_SPLIT];
253 
254 uint8_t multi_rx_mempool; /**< Enables multi-rx-mempool feature */
255 
256 /*
257  * Configuration of packet segments used by the "txonly" processing engine.
258  */
259 uint16_t tx_pkt_length = TXONLY_DEF_PACKET_LEN; /**< TXONLY packet length. */
260 uint16_t tx_pkt_seg_lengths[RTE_MAX_SEGS_PER_PKT] = {
261 	TXONLY_DEF_PACKET_LEN,
262 };
263 uint8_t  tx_pkt_nb_segs = 1; /**< Number of segments in TXONLY packets */
264 
265 enum tx_pkt_split tx_pkt_split = TX_PKT_SPLIT_OFF;
266 /**< Split policy for packets to TX. */
267 
268 uint8_t txonly_multi_flow;
269 /**< Whether multiple flows are generated in TXONLY mode. */
270 
271 uint32_t tx_pkt_times_inter;
272 /**< Timings for send scheduling in TXONLY mode, time between bursts. */
273 
274 uint32_t tx_pkt_times_intra;
275 /**< Timings for send scheduling in TXONLY mode, time between packets. */
276 
277 uint16_t nb_pkt_per_burst = DEF_PKT_BURST; /**< Number of packets per burst. */
278 uint16_t nb_pkt_flowgen_clones; /**< Number of Tx packet clones to send in flowgen mode. */
279 int nb_flows_flowgen = 1024; /**< Number of flows in flowgen mode. */
280 uint16_t mb_mempool_cache = DEF_MBUF_CACHE; /**< Size of mbuf mempool cache. */
281 
282 /* current configuration is in DCB or not,0 means it is not in DCB mode */
283 uint8_t dcb_config = 0;
284 
285 /*
286  * Configurable number of RX/TX queues.
287  */
288 queueid_t nb_hairpinq; /**< Number of hairpin queues per port. */
289 queueid_t nb_rxq = 1; /**< Number of RX queues per port. */
290 queueid_t nb_txq = 1; /**< Number of TX queues per port. */
291 
292 /*
293  * Configurable number of RX/TX ring descriptors.
294  * Defaults are supplied by drivers via ethdev.
295  */
296 #define RX_DESC_DEFAULT 0
297 #define TX_DESC_DEFAULT 0
298 uint16_t nb_rxd = RX_DESC_DEFAULT; /**< Number of RX descriptors. */
299 uint16_t nb_txd = TX_DESC_DEFAULT; /**< Number of TX descriptors. */
300 
301 #define RTE_PMD_PARAM_UNSET -1
302 /*
303  * Configurable values of RX and TX ring threshold registers.
304  */
305 
306 int8_t rx_pthresh = RTE_PMD_PARAM_UNSET;
307 int8_t rx_hthresh = RTE_PMD_PARAM_UNSET;
308 int8_t rx_wthresh = RTE_PMD_PARAM_UNSET;
309 
310 int8_t tx_pthresh = RTE_PMD_PARAM_UNSET;
311 int8_t tx_hthresh = RTE_PMD_PARAM_UNSET;
312 int8_t tx_wthresh = RTE_PMD_PARAM_UNSET;
313 
314 /*
315  * Configurable value of RX free threshold.
316  */
317 int16_t rx_free_thresh = RTE_PMD_PARAM_UNSET;
318 
319 /*
320  * Configurable value of RX drop enable.
321  */
322 int8_t rx_drop_en = RTE_PMD_PARAM_UNSET;
323 
324 /*
325  * Configurable value of TX free threshold.
326  */
327 int16_t tx_free_thresh = RTE_PMD_PARAM_UNSET;
328 
329 /*
330  * Configurable value of TX RS bit threshold.
331  */
332 int16_t tx_rs_thresh = RTE_PMD_PARAM_UNSET;
333 
334 /*
335  * Configurable sub-forwarding mode for the noisy_vnf forwarding mode.
336  */
337 enum noisy_fwd_mode noisy_fwd_mode;
338 
339 /* String version of enum noisy_fwd_mode */
340 const char * const noisy_fwd_mode_desc[] = {
341 	[NOISY_FWD_MODE_IO] = "io",
342 	[NOISY_FWD_MODE_MAC] = "mac",
343 	[NOISY_FWD_MODE_MACSWAP] = "macswap",
344 	[NOISY_FWD_MODE_5TSWAP] = "5tswap",
345 	[NOISY_FWD_MODE_MAX] = NULL,
346 };
347 
348 /*
349  * Configurable value of buffered packets before sending.
350  */
351 uint16_t noisy_tx_sw_bufsz;
352 
353 /*
354  * Configurable value of packet buffer timeout.
355  */
356 uint16_t noisy_tx_sw_buf_flush_time;
357 
358 /*
359  * Configurable value for size of VNF internal memory area
360  * used for simulating noisy neighbour behaviour
361  */
362 uint64_t noisy_lkup_mem_sz;
363 
364 /*
365  * Configurable value of number of random writes done in
366  * VNF simulation memory area.
367  */
368 uint64_t noisy_lkup_num_writes;
369 
370 /*
371  * Configurable value of number of random reads done in
372  * VNF simulation memory area.
373  */
374 uint64_t noisy_lkup_num_reads;
375 
376 /*
377  * Configurable value of number of random reads/writes done in
378  * VNF simulation memory area.
379  */
380 uint64_t noisy_lkup_num_reads_writes;
381 
382 /*
383  * Receive Side Scaling (RSS) configuration.
384  */
385 uint64_t rss_hf = RTE_ETH_RSS_IP; /* RSS IP by default. */
386 
387 /*
388  * Port topology configuration
389  */
390 uint16_t port_topology = PORT_TOPOLOGY_PAIRED; /* Ports are paired by default */
391 
392 /*
393  * Avoids to flush all the RX streams before starts forwarding.
394  */
395 uint8_t no_flush_rx = 0; /* flush by default */
396 
397 /*
398  * Flow API isolated mode.
399  */
400 uint8_t flow_isolate_all;
401 
402 /*
403  * Disable port flow flush when stop port.
404  */
405 uint8_t no_flow_flush = 0; /* do flow flush by default */
406 
407 /*
408  * Avoids to check link status when starting/stopping a port.
409  */
410 uint8_t no_link_check = 0; /* check by default */
411 
412 /*
413  * Don't automatically start all ports in interactive mode.
414  */
415 uint8_t no_device_start = 0;
416 
417 /*
418  * Enable link status change notification
419  */
420 uint8_t lsc_interrupt = 1; /* enabled by default */
421 
422 /*
423  * Enable device removal notification.
424  */
425 uint8_t rmv_interrupt = 1; /* enabled by default */
426 
427 uint8_t hot_plug = 0; /**< hotplug disabled by default. */
428 
429 /* After attach, port setup is called on event or by iterator */
430 bool setup_on_probe_event = true;
431 
432 /* Clear ptypes on port initialization. */
433 uint8_t clear_ptypes = true;
434 
435 /* Hairpin ports configuration mode. */
436 uint32_t hairpin_mode;
437 
438 /* Pretty printing of ethdev events */
439 static const char * const eth_event_desc[] = {
440 	[RTE_ETH_EVENT_UNKNOWN] = "unknown",
441 	[RTE_ETH_EVENT_INTR_LSC] = "link state change",
442 	[RTE_ETH_EVENT_QUEUE_STATE] = "queue state",
443 	[RTE_ETH_EVENT_INTR_RESET] = "reset",
444 	[RTE_ETH_EVENT_VF_MBOX] = "VF mbox",
445 	[RTE_ETH_EVENT_IPSEC] = "IPsec",
446 	[RTE_ETH_EVENT_MACSEC] = "MACsec",
447 	[RTE_ETH_EVENT_INTR_RMV] = "device removal",
448 	[RTE_ETH_EVENT_NEW] = "device probed",
449 	[RTE_ETH_EVENT_DESTROY] = "device released",
450 	[RTE_ETH_EVENT_FLOW_AGED] = "flow aged",
451 	[RTE_ETH_EVENT_RX_AVAIL_THRESH] = "RxQ available descriptors threshold reached",
452 	[RTE_ETH_EVENT_ERR_RECOVERING] = "error recovering",
453 	[RTE_ETH_EVENT_RECOVERY_SUCCESS] = "error recovery successful",
454 	[RTE_ETH_EVENT_RECOVERY_FAILED] = "error recovery failed",
455 	[RTE_ETH_EVENT_MAX] = NULL,
456 };
457 
458 /*
459  * Display or mask ether events
460  * Default to all events except VF_MBOX
461  */
462 uint32_t event_print_mask = (UINT32_C(1) << RTE_ETH_EVENT_UNKNOWN) |
463 			    (UINT32_C(1) << RTE_ETH_EVENT_INTR_LSC) |
464 			    (UINT32_C(1) << RTE_ETH_EVENT_QUEUE_STATE) |
465 			    (UINT32_C(1) << RTE_ETH_EVENT_INTR_RESET) |
466 			    (UINT32_C(1) << RTE_ETH_EVENT_IPSEC) |
467 			    (UINT32_C(1) << RTE_ETH_EVENT_MACSEC) |
468 			    (UINT32_C(1) << RTE_ETH_EVENT_INTR_RMV) |
469 			    (UINT32_C(1) << RTE_ETH_EVENT_FLOW_AGED) |
470 			    (UINT32_C(1) << RTE_ETH_EVENT_ERR_RECOVERING) |
471 			    (UINT32_C(1) << RTE_ETH_EVENT_RECOVERY_SUCCESS) |
472 			    (UINT32_C(1) << RTE_ETH_EVENT_RECOVERY_FAILED);
473 /*
474  * Decide if all memory are locked for performance.
475  */
476 int do_mlockall = 0;
477 
478 #ifdef RTE_LIB_LATENCYSTATS
479 
480 /*
481  * Set when latency stats is enabled in the commandline
482  */
483 uint8_t latencystats_enabled;
484 
485 /*
486  * Lcore ID to service latency statistics.
487  */
488 lcoreid_t latencystats_lcore_id = -1;
489 
490 #endif
491 
492 /*
493  * Ethernet device configuration.
494  */
495 struct rte_eth_rxmode rx_mode;
496 
497 struct rte_eth_txmode tx_mode = {
498 	.offloads = RTE_ETH_TX_OFFLOAD_MBUF_FAST_FREE,
499 };
500 
501 volatile int test_done = 1; /* stop packet forwarding when set to 1. */
502 
503 /*
504  * Display zero values by default for xstats
505  */
506 uint8_t xstats_hide_zero;
507 
508 /*
509  * Measure of CPU cycles disabled by default
510  */
511 uint8_t record_core_cycles;
512 
513 /*
514  * Display of RX and TX bursts disabled by default
515  */
516 uint8_t record_burst_stats;
517 
518 /*
519  * Number of ports per shared Rx queue group, 0 disable.
520  */
521 uint32_t rxq_share;
522 
523 unsigned int num_sockets = 0;
524 unsigned int socket_ids[RTE_MAX_NUMA_NODES];
525 
526 #ifdef RTE_LIB_BITRATESTATS
527 /* Bitrate statistics */
528 struct rte_stats_bitrates *bitrate_data;
529 lcoreid_t bitrate_lcore_id;
530 uint8_t bitrate_enabled;
531 #endif
532 
533 #ifdef RTE_LIB_GRO
534 struct gro_status gro_ports[RTE_MAX_ETHPORTS];
535 uint8_t gro_flush_cycles = GRO_DEFAULT_FLUSH_CYCLES;
536 #endif
537 
538 /*
539  * hexadecimal bitmask of RX mq mode can be enabled.
540  */
541 enum rte_eth_rx_mq_mode rx_mq_mode = RTE_ETH_MQ_RX_VMDQ_DCB_RSS;
542 
543 /*
544  * Used to set forced link speed
545  */
546 uint32_t eth_link_speed;
547 
548 /*
549  * ID of the current process in multi-process, used to
550  * configure the queues to be polled.
551  */
552 int proc_id;
553 
554 /*
555  * Number of processes in multi-process, used to
556  * configure the queues to be polled.
557  */
558 unsigned int num_procs = 1;
559 
560 static void
561 eth_rx_metadata_negotiate_mp(uint16_t port_id)
562 {
563 	uint64_t rx_meta_features = 0;
564 	int ret;
565 
566 	if (!is_proc_primary())
567 		return;
568 
569 	rx_meta_features |= RTE_ETH_RX_METADATA_USER_FLAG;
570 	rx_meta_features |= RTE_ETH_RX_METADATA_USER_MARK;
571 	rx_meta_features |= RTE_ETH_RX_METADATA_TUNNEL_ID;
572 
573 	ret = rte_eth_rx_metadata_negotiate(port_id, &rx_meta_features);
574 	if (ret == 0) {
575 		if (!(rx_meta_features & RTE_ETH_RX_METADATA_USER_FLAG)) {
576 			TESTPMD_LOG(DEBUG, "Flow action FLAG will not affect Rx mbufs on port %u\n",
577 				    port_id);
578 		}
579 
580 		if (!(rx_meta_features & RTE_ETH_RX_METADATA_USER_MARK)) {
581 			TESTPMD_LOG(DEBUG, "Flow action MARK will not affect Rx mbufs on port %u\n",
582 				    port_id);
583 		}
584 
585 		if (!(rx_meta_features & RTE_ETH_RX_METADATA_TUNNEL_ID)) {
586 			TESTPMD_LOG(DEBUG, "Flow tunnel offload support might be limited or unavailable on port %u\n",
587 				    port_id);
588 		}
589 	} else if (ret != -ENOTSUP) {
590 		rte_exit(EXIT_FAILURE, "Error when negotiating Rx meta features on port %u: %s\n",
591 			 port_id, rte_strerror(-ret));
592 	}
593 }
594 
595 static int
596 eth_dev_configure_mp(uint16_t port_id, uint16_t nb_rx_q, uint16_t nb_tx_q,
597 		      const struct rte_eth_conf *dev_conf)
598 {
599 	if (is_proc_primary())
600 		return rte_eth_dev_configure(port_id, nb_rx_q, nb_tx_q,
601 					dev_conf);
602 	return 0;
603 }
604 
605 static int
606 change_bonding_member_port_status(portid_t bond_pid, bool is_stop)
607 {
608 #ifdef RTE_NET_BOND
609 
610 	portid_t member_pids[RTE_MAX_ETHPORTS];
611 	struct rte_port *port;
612 	int num_members;
613 	portid_t member_pid;
614 	int i;
615 
616 	num_members = rte_eth_bond_members_get(bond_pid, member_pids,
617 						RTE_MAX_ETHPORTS);
618 	if (num_members < 0) {
619 		fprintf(stderr, "Failed to get member list for port = %u\n",
620 			bond_pid);
621 		return num_members;
622 	}
623 
624 	for (i = 0; i < num_members; i++) {
625 		member_pid = member_pids[i];
626 		port = &ports[member_pid];
627 		port->port_status =
628 			is_stop ? RTE_PORT_STOPPED : RTE_PORT_STARTED;
629 	}
630 #else
631 	RTE_SET_USED(bond_pid);
632 	RTE_SET_USED(is_stop);
633 #endif
634 	return 0;
635 }
636 
637 static int
638 eth_dev_start_mp(uint16_t port_id)
639 {
640 	int ret;
641 
642 	if (is_proc_primary()) {
643 		ret = rte_eth_dev_start(port_id);
644 		if (ret != 0)
645 			return ret;
646 
647 		struct rte_port *port = &ports[port_id];
648 
649 		/*
650 		 * Starting a bonding port also starts all members under the bonding
651 		 * device. So if this port is bond device, we need to modify the
652 		 * port status of these members.
653 		 */
654 		if (port->bond_flag == 1)
655 			return change_bonding_member_port_status(port_id, false);
656 	}
657 
658 	return 0;
659 }
660 
661 static int
662 eth_dev_stop_mp(uint16_t port_id)
663 {
664 	int ret;
665 
666 	if (is_proc_primary()) {
667 		ret = rte_eth_dev_stop(port_id);
668 		if (ret != 0)
669 			return ret;
670 
671 		struct rte_port *port = &ports[port_id];
672 
673 		/*
674 		 * Stopping a bonding port also stops all members under the bonding
675 		 * device. So if this port is bond device, we need to modify the
676 		 * port status of these members.
677 		 */
678 		if (port->bond_flag == 1)
679 			return change_bonding_member_port_status(port_id, true);
680 	}
681 
682 	return 0;
683 }
684 
685 static void
686 mempool_free_mp(struct rte_mempool *mp)
687 {
688 	if (is_proc_primary())
689 		rte_mempool_free(mp);
690 }
691 
692 static int
693 eth_dev_set_mtu_mp(uint16_t port_id, uint16_t mtu)
694 {
695 	if (is_proc_primary())
696 		return rte_eth_dev_set_mtu(port_id, mtu);
697 
698 	return 0;
699 }
700 
701 /* Forward function declarations */
702 static void setup_attached_port(portid_t pi);
703 static void check_all_ports_link_status(uint32_t port_mask);
704 static int eth_event_callback(portid_t port_id,
705 			      enum rte_eth_event_type type,
706 			      void *param, void *ret_param);
707 static void dev_event_callback(const char *device_name,
708 				enum rte_dev_event_type type,
709 				void *param);
710 static void fill_xstats_display_info(void);
711 
712 /*
713  * Check if all the ports are started.
714  * If yes, return positive value. If not, return zero.
715  */
716 static int all_ports_started(void);
717 
718 #ifdef RTE_LIB_GSO
719 struct gso_status gso_ports[RTE_MAX_ETHPORTS];
720 uint16_t gso_max_segment_size = RTE_ETHER_MAX_LEN - RTE_ETHER_CRC_LEN;
721 #endif
722 
723 /* Holds the registered mbuf dynamic flags names. */
724 char dynf_names[64][RTE_MBUF_DYN_NAMESIZE];
725 
726 
727 /*
728  * Helper function to check if socket is already discovered.
729  * If yes, return positive value. If not, return zero.
730  */
731 int
732 new_socket_id(unsigned int socket_id)
733 {
734 	unsigned int i;
735 
736 	for (i = 0; i < num_sockets; i++) {
737 		if (socket_ids[i] == socket_id)
738 			return 0;
739 	}
740 	return 1;
741 }
742 
743 /*
744  * Setup default configuration.
745  */
746 static void
747 set_default_fwd_lcores_config(void)
748 {
749 	unsigned int i;
750 	unsigned int nb_lc;
751 	unsigned int sock_num;
752 
753 	nb_lc = 0;
754 	for (i = 0; i < RTE_MAX_LCORE; i++) {
755 		if (!rte_lcore_is_enabled(i))
756 			continue;
757 		sock_num = rte_lcore_to_socket_id(i);
758 		if (new_socket_id(sock_num)) {
759 			if (num_sockets >= RTE_MAX_NUMA_NODES) {
760 				rte_exit(EXIT_FAILURE,
761 					 "Total sockets greater than %u\n",
762 					 RTE_MAX_NUMA_NODES);
763 			}
764 			socket_ids[num_sockets++] = sock_num;
765 		}
766 		if (i == rte_get_main_lcore())
767 			continue;
768 		fwd_lcores_cpuids[nb_lc++] = i;
769 	}
770 	nb_lcores = (lcoreid_t) nb_lc;
771 	nb_cfg_lcores = nb_lcores;
772 	nb_fwd_lcores = 1;
773 }
774 
775 static void
776 set_def_peer_eth_addrs(void)
777 {
778 	portid_t i;
779 
780 	for (i = 0; i < RTE_MAX_ETHPORTS; i++) {
781 		peer_eth_addrs[i].addr_bytes[0] = RTE_ETHER_LOCAL_ADMIN_ADDR;
782 		peer_eth_addrs[i].addr_bytes[5] = i;
783 	}
784 }
785 
786 static void
787 set_default_fwd_ports_config(void)
788 {
789 	portid_t pt_id;
790 	int i = 0;
791 
792 	RTE_ETH_FOREACH_DEV(pt_id) {
793 		fwd_ports_ids[i++] = pt_id;
794 
795 		/* Update sockets info according to the attached device */
796 		int socket_id = rte_eth_dev_socket_id(pt_id);
797 		if (socket_id >= 0 && new_socket_id(socket_id)) {
798 			if (num_sockets >= RTE_MAX_NUMA_NODES) {
799 				rte_exit(EXIT_FAILURE,
800 					 "Total sockets greater than %u\n",
801 					 RTE_MAX_NUMA_NODES);
802 			}
803 			socket_ids[num_sockets++] = socket_id;
804 		}
805 	}
806 
807 	nb_cfg_ports = nb_ports;
808 	nb_fwd_ports = nb_ports;
809 }
810 
811 void
812 set_def_fwd_config(void)
813 {
814 	set_default_fwd_lcores_config();
815 	set_def_peer_eth_addrs();
816 	set_default_fwd_ports_config();
817 }
818 
819 #ifndef RTE_EXEC_ENV_WINDOWS
820 /* extremely pessimistic estimation of memory required to create a mempool */
821 static int
822 calc_mem_size(uint32_t nb_mbufs, uint32_t mbuf_sz, size_t pgsz, size_t *out)
823 {
824 	unsigned int n_pages, mbuf_per_pg, leftover;
825 	uint64_t total_mem, mbuf_mem, obj_sz;
826 
827 	/* there is no good way to predict how much space the mempool will
828 	 * occupy because it will allocate chunks on the fly, and some of those
829 	 * will come from default DPDK memory while some will come from our
830 	 * external memory, so just assume 128MB will be enough for everyone.
831 	 */
832 	uint64_t hdr_mem = 128 << 20;
833 
834 	/* account for possible non-contiguousness */
835 	obj_sz = rte_mempool_calc_obj_size(mbuf_sz, 0, NULL);
836 	if (obj_sz > pgsz) {
837 		TESTPMD_LOG(ERR, "Object size is bigger than page size\n");
838 		return -1;
839 	}
840 
841 	mbuf_per_pg = pgsz / obj_sz;
842 	leftover = (nb_mbufs % mbuf_per_pg) > 0;
843 	n_pages = (nb_mbufs / mbuf_per_pg) + leftover;
844 
845 	mbuf_mem = n_pages * pgsz;
846 
847 	total_mem = RTE_ALIGN(hdr_mem + mbuf_mem, pgsz);
848 
849 	if (total_mem > SIZE_MAX) {
850 		TESTPMD_LOG(ERR, "Memory size too big\n");
851 		return -1;
852 	}
853 	*out = (size_t)total_mem;
854 
855 	return 0;
856 }
857 
858 static int
859 pagesz_flags(uint64_t page_sz)
860 {
861 	/* as per mmap() manpage, all page sizes are log2 of page size
862 	 * shifted by MAP_HUGE_SHIFT
863 	 */
864 	int log2 = rte_log2_u64(page_sz);
865 
866 	return (log2 << HUGE_SHIFT);
867 }
868 
869 static void *
870 alloc_mem(size_t memsz, size_t pgsz, bool huge)
871 {
872 	void *addr;
873 	int flags;
874 
875 	/* allocate anonymous hugepages */
876 	flags = MAP_ANONYMOUS | MAP_PRIVATE;
877 	if (huge)
878 		flags |= HUGE_FLAG | pagesz_flags(pgsz);
879 
880 	addr = mmap(NULL, memsz, PROT_READ | PROT_WRITE, flags, -1, 0);
881 	if (addr == MAP_FAILED)
882 		return NULL;
883 
884 	return addr;
885 }
886 
887 struct extmem_param {
888 	void *addr;
889 	size_t len;
890 	size_t pgsz;
891 	rte_iova_t *iova_table;
892 	unsigned int iova_table_len;
893 };
894 
895 static int
896 create_extmem(uint32_t nb_mbufs, uint32_t mbuf_sz, struct extmem_param *param,
897 		bool huge)
898 {
899 	uint64_t pgsizes[] = {RTE_PGSIZE_2M, RTE_PGSIZE_1G, /* x86_64, ARM */
900 			RTE_PGSIZE_16M, RTE_PGSIZE_16G};    /* POWER */
901 	unsigned int cur_page, n_pages, pgsz_idx;
902 	size_t mem_sz, cur_pgsz;
903 	rte_iova_t *iovas = NULL;
904 	void *addr;
905 	int ret;
906 
907 	for (pgsz_idx = 0; pgsz_idx < RTE_DIM(pgsizes); pgsz_idx++) {
908 		/* skip anything that is too big */
909 		if (pgsizes[pgsz_idx] > SIZE_MAX)
910 			continue;
911 
912 		cur_pgsz = pgsizes[pgsz_idx];
913 
914 		/* if we were told not to allocate hugepages, override */
915 		if (!huge)
916 			cur_pgsz = sysconf(_SC_PAGESIZE);
917 
918 		ret = calc_mem_size(nb_mbufs, mbuf_sz, cur_pgsz, &mem_sz);
919 		if (ret < 0) {
920 			TESTPMD_LOG(ERR, "Cannot calculate memory size\n");
921 			return -1;
922 		}
923 
924 		/* allocate our memory */
925 		addr = alloc_mem(mem_sz, cur_pgsz, huge);
926 
927 		/* if we couldn't allocate memory with a specified page size,
928 		 * that doesn't mean we can't do it with other page sizes, so
929 		 * try another one.
930 		 */
931 		if (addr == NULL)
932 			continue;
933 
934 		/* store IOVA addresses for every page in this memory area */
935 		n_pages = mem_sz / cur_pgsz;
936 
937 		iovas = malloc(sizeof(*iovas) * n_pages);
938 
939 		if (iovas == NULL) {
940 			TESTPMD_LOG(ERR, "Cannot allocate memory for iova addresses\n");
941 			goto fail;
942 		}
943 		/* lock memory if it's not huge pages */
944 		if (!huge)
945 			mlock(addr, mem_sz);
946 
947 		/* populate IOVA addresses */
948 		for (cur_page = 0; cur_page < n_pages; cur_page++) {
949 			rte_iova_t iova;
950 			size_t offset;
951 			void *cur;
952 
953 			offset = cur_pgsz * cur_page;
954 			cur = RTE_PTR_ADD(addr, offset);
955 
956 			/* touch the page before getting its IOVA */
957 			*(volatile char *)cur = 0;
958 
959 			iova = rte_mem_virt2iova(cur);
960 
961 			iovas[cur_page] = iova;
962 		}
963 
964 		break;
965 	}
966 	/* if we couldn't allocate anything */
967 	if (iovas == NULL)
968 		return -1;
969 
970 	param->addr = addr;
971 	param->len = mem_sz;
972 	param->pgsz = cur_pgsz;
973 	param->iova_table = iovas;
974 	param->iova_table_len = n_pages;
975 
976 	return 0;
977 fail:
978 	free(iovas);
979 	if (addr)
980 		munmap(addr, mem_sz);
981 
982 	return -1;
983 }
984 
985 static int
986 setup_extmem(uint32_t nb_mbufs, uint32_t mbuf_sz, bool huge)
987 {
988 	struct extmem_param param;
989 	int socket_id, ret;
990 
991 	memset(&param, 0, sizeof(param));
992 
993 	/* check if our heap exists */
994 	socket_id = rte_malloc_heap_get_socket(EXTMEM_HEAP_NAME);
995 	if (socket_id < 0) {
996 		/* create our heap */
997 		ret = rte_malloc_heap_create(EXTMEM_HEAP_NAME);
998 		if (ret < 0) {
999 			TESTPMD_LOG(ERR, "Cannot create heap\n");
1000 			return -1;
1001 		}
1002 	}
1003 
1004 	ret = create_extmem(nb_mbufs, mbuf_sz, &param, huge);
1005 	if (ret < 0) {
1006 		TESTPMD_LOG(ERR, "Cannot create memory area\n");
1007 		return -1;
1008 	}
1009 
1010 	/* we now have a valid memory area, so add it to heap */
1011 	ret = rte_malloc_heap_memory_add(EXTMEM_HEAP_NAME,
1012 			param.addr, param.len, param.iova_table,
1013 			param.iova_table_len, param.pgsz);
1014 
1015 	/* when using VFIO, memory is automatically mapped for DMA by EAL */
1016 
1017 	/* not needed any more */
1018 	free(param.iova_table);
1019 
1020 	if (ret < 0) {
1021 		TESTPMD_LOG(ERR, "Cannot add memory to heap\n");
1022 		munmap(param.addr, param.len);
1023 		return -1;
1024 	}
1025 
1026 	/* success */
1027 
1028 	TESTPMD_LOG(DEBUG, "Allocated %zuMB of external memory\n",
1029 			param.len >> 20);
1030 
1031 	return 0;
1032 }
1033 static void
1034 dma_unmap_cb(struct rte_mempool *mp __rte_unused, void *opaque __rte_unused,
1035 	     struct rte_mempool_memhdr *memhdr, unsigned mem_idx __rte_unused)
1036 {
1037 	uint16_t pid = 0;
1038 	int ret;
1039 
1040 	RTE_ETH_FOREACH_DEV(pid) {
1041 		struct rte_eth_dev_info dev_info;
1042 
1043 		ret = eth_dev_info_get_print_err(pid, &dev_info);
1044 		if (ret != 0) {
1045 			TESTPMD_LOG(DEBUG,
1046 				    "unable to get device info for port %d on addr 0x%p,"
1047 				    "mempool unmapping will not be performed\n",
1048 				    pid, memhdr->addr);
1049 			continue;
1050 		}
1051 
1052 		ret = rte_dev_dma_unmap(dev_info.device, memhdr->addr, 0, memhdr->len);
1053 		if (ret) {
1054 			TESTPMD_LOG(DEBUG,
1055 				    "unable to DMA unmap addr 0x%p "
1056 				    "for device %s\n",
1057 				    memhdr->addr, rte_dev_name(dev_info.device));
1058 		}
1059 	}
1060 	ret = rte_extmem_unregister(memhdr->addr, memhdr->len);
1061 	if (ret) {
1062 		TESTPMD_LOG(DEBUG,
1063 			    "unable to un-register addr 0x%p\n", memhdr->addr);
1064 	}
1065 }
1066 
1067 static void
1068 dma_map_cb(struct rte_mempool *mp __rte_unused, void *opaque __rte_unused,
1069 	   struct rte_mempool_memhdr *memhdr, unsigned mem_idx __rte_unused)
1070 {
1071 	uint16_t pid = 0;
1072 	size_t page_size = sysconf(_SC_PAGESIZE);
1073 	int ret;
1074 
1075 	ret = rte_extmem_register(memhdr->addr, memhdr->len, NULL, 0,
1076 				  page_size);
1077 	if (ret) {
1078 		TESTPMD_LOG(DEBUG,
1079 			    "unable to register addr 0x%p\n", memhdr->addr);
1080 		return;
1081 	}
1082 	RTE_ETH_FOREACH_DEV(pid) {
1083 		struct rte_eth_dev_info dev_info;
1084 
1085 		ret = eth_dev_info_get_print_err(pid, &dev_info);
1086 		if (ret != 0) {
1087 			TESTPMD_LOG(DEBUG,
1088 				    "unable to get device info for port %d on addr 0x%p,"
1089 				    "mempool mapping will not be performed\n",
1090 				    pid, memhdr->addr);
1091 			continue;
1092 		}
1093 		ret = rte_dev_dma_map(dev_info.device, memhdr->addr, 0, memhdr->len);
1094 		if (ret) {
1095 			TESTPMD_LOG(DEBUG,
1096 				    "unable to DMA map addr 0x%p "
1097 				    "for device %s\n",
1098 				    memhdr->addr, rte_dev_name(dev_info.device));
1099 		}
1100 	}
1101 }
1102 #endif
1103 
1104 static unsigned int
1105 setup_extbuf(uint32_t nb_mbufs, uint16_t mbuf_sz, unsigned int socket_id,
1106 	    char *pool_name, struct rte_pktmbuf_extmem **ext_mem)
1107 {
1108 	struct rte_pktmbuf_extmem *xmem;
1109 	unsigned int ext_num, zone_num, elt_num;
1110 	uint16_t elt_size;
1111 
1112 	elt_size = RTE_ALIGN_CEIL(mbuf_sz, RTE_CACHE_LINE_SIZE);
1113 	elt_num = EXTBUF_ZONE_SIZE / elt_size;
1114 	zone_num = (nb_mbufs + elt_num - 1) / elt_num;
1115 
1116 	xmem = malloc(sizeof(struct rte_pktmbuf_extmem) * zone_num);
1117 	if (xmem == NULL) {
1118 		TESTPMD_LOG(ERR, "Cannot allocate memory for "
1119 				 "external buffer descriptors\n");
1120 		*ext_mem = NULL;
1121 		return 0;
1122 	}
1123 	for (ext_num = 0; ext_num < zone_num; ext_num++) {
1124 		struct rte_pktmbuf_extmem *xseg = xmem + ext_num;
1125 		const struct rte_memzone *mz;
1126 		char mz_name[RTE_MEMZONE_NAMESIZE];
1127 		int ret;
1128 
1129 		ret = snprintf(mz_name, sizeof(mz_name),
1130 			RTE_MEMPOOL_MZ_FORMAT "_xb_%u", pool_name, ext_num);
1131 		if (ret < 0 || ret >= (int)sizeof(mz_name)) {
1132 			errno = ENAMETOOLONG;
1133 			ext_num = 0;
1134 			break;
1135 		}
1136 		mz = rte_memzone_reserve(mz_name, EXTBUF_ZONE_SIZE,
1137 					 socket_id,
1138 					 RTE_MEMZONE_IOVA_CONTIG |
1139 					 RTE_MEMZONE_1GB |
1140 					 RTE_MEMZONE_SIZE_HINT_ONLY);
1141 		if (mz == NULL) {
1142 			/*
1143 			 * The caller exits on external buffer creation
1144 			 * error, so there is no need to free memzones.
1145 			 */
1146 			errno = ENOMEM;
1147 			ext_num = 0;
1148 			break;
1149 		}
1150 		xseg->buf_ptr = mz->addr;
1151 		xseg->buf_iova = mz->iova;
1152 		xseg->buf_len = EXTBUF_ZONE_SIZE;
1153 		xseg->elt_size = elt_size;
1154 	}
1155 	if (ext_num == 0 && xmem != NULL) {
1156 		free(xmem);
1157 		xmem = NULL;
1158 	}
1159 	*ext_mem = xmem;
1160 	return ext_num;
1161 }
1162 
1163 /*
1164  * Configuration initialisation done once at init time.
1165  */
1166 static struct rte_mempool *
1167 mbuf_pool_create(uint16_t mbuf_seg_size, unsigned nb_mbuf,
1168 		 unsigned int socket_id, uint16_t size_idx)
1169 {
1170 	char pool_name[RTE_MEMPOOL_NAMESIZE];
1171 	struct rte_mempool *rte_mp = NULL;
1172 #ifndef RTE_EXEC_ENV_WINDOWS
1173 	uint32_t mb_size;
1174 
1175 	mb_size = sizeof(struct rte_mbuf) + mbuf_seg_size;
1176 #endif
1177 	mbuf_poolname_build(socket_id, pool_name, sizeof(pool_name), size_idx);
1178 	if (!is_proc_primary()) {
1179 		rte_mp = rte_mempool_lookup(pool_name);
1180 		if (rte_mp == NULL)
1181 			rte_exit(EXIT_FAILURE,
1182 				"Get mbuf pool for socket %u failed: %s\n",
1183 				socket_id, rte_strerror(rte_errno));
1184 		return rte_mp;
1185 	}
1186 
1187 	TESTPMD_LOG(INFO,
1188 		"create a new mbuf pool <%s>: n=%u, size=%u, socket=%u\n",
1189 		pool_name, nb_mbuf, mbuf_seg_size, socket_id);
1190 
1191 	switch (mp_alloc_type) {
1192 	case MP_ALLOC_NATIVE:
1193 		{
1194 			/* wrapper to rte_mempool_create() */
1195 			TESTPMD_LOG(INFO, "preferred mempool ops selected: %s\n",
1196 					rte_mbuf_best_mempool_ops());
1197 			rte_mp = rte_pktmbuf_pool_create(pool_name, nb_mbuf,
1198 				mb_mempool_cache, 0, mbuf_seg_size, socket_id);
1199 			break;
1200 		}
1201 #ifndef RTE_EXEC_ENV_WINDOWS
1202 	case MP_ALLOC_ANON:
1203 		{
1204 			rte_mp = rte_mempool_create_empty(pool_name, nb_mbuf,
1205 				mb_size, (unsigned int) mb_mempool_cache,
1206 				sizeof(struct rte_pktmbuf_pool_private),
1207 				socket_id, mempool_flags);
1208 			if (rte_mp == NULL)
1209 				goto err;
1210 
1211 			if (rte_mempool_populate_anon(rte_mp) == 0) {
1212 				rte_mempool_free(rte_mp);
1213 				rte_mp = NULL;
1214 				goto err;
1215 			}
1216 			rte_pktmbuf_pool_init(rte_mp, NULL);
1217 			rte_mempool_obj_iter(rte_mp, rte_pktmbuf_init, NULL);
1218 			rte_mempool_mem_iter(rte_mp, dma_map_cb, NULL);
1219 			break;
1220 		}
1221 	case MP_ALLOC_XMEM:
1222 	case MP_ALLOC_XMEM_HUGE:
1223 		{
1224 			int heap_socket;
1225 			bool huge = mp_alloc_type == MP_ALLOC_XMEM_HUGE;
1226 
1227 			if (setup_extmem(nb_mbuf, mbuf_seg_size, huge) < 0)
1228 				rte_exit(EXIT_FAILURE, "Could not create external memory\n");
1229 
1230 			heap_socket =
1231 				rte_malloc_heap_get_socket(EXTMEM_HEAP_NAME);
1232 			if (heap_socket < 0)
1233 				rte_exit(EXIT_FAILURE, "Could not get external memory socket ID\n");
1234 
1235 			TESTPMD_LOG(INFO, "preferred mempool ops selected: %s\n",
1236 					rte_mbuf_best_mempool_ops());
1237 			rte_mp = rte_pktmbuf_pool_create(pool_name, nb_mbuf,
1238 					mb_mempool_cache, 0, mbuf_seg_size,
1239 					heap_socket);
1240 			break;
1241 		}
1242 #endif
1243 	case MP_ALLOC_XBUF:
1244 		{
1245 			struct rte_pktmbuf_extmem *ext_mem;
1246 			unsigned int ext_num;
1247 
1248 			ext_num = setup_extbuf(nb_mbuf,	mbuf_seg_size,
1249 					       socket_id, pool_name, &ext_mem);
1250 			if (ext_num == 0)
1251 				rte_exit(EXIT_FAILURE,
1252 					 "Can't create pinned data buffers\n");
1253 
1254 			TESTPMD_LOG(INFO, "preferred mempool ops selected: %s\n",
1255 					rte_mbuf_best_mempool_ops());
1256 			rte_mp = rte_pktmbuf_pool_create_extbuf
1257 					(pool_name, nb_mbuf, mb_mempool_cache,
1258 					 0, mbuf_seg_size, socket_id,
1259 					 ext_mem, ext_num);
1260 			free(ext_mem);
1261 			break;
1262 		}
1263 	default:
1264 		{
1265 			rte_exit(EXIT_FAILURE, "Invalid mempool creation mode\n");
1266 		}
1267 	}
1268 
1269 #ifndef RTE_EXEC_ENV_WINDOWS
1270 err:
1271 #endif
1272 	if (rte_mp == NULL) {
1273 		rte_exit(EXIT_FAILURE,
1274 			"Creation of mbuf pool for socket %u failed: %s\n",
1275 			socket_id, rte_strerror(rte_errno));
1276 	} else if (verbose_level > 0) {
1277 		rte_mempool_dump(stdout, rte_mp);
1278 	}
1279 	return rte_mp;
1280 }
1281 
1282 /*
1283  * Check given socket id is valid or not with NUMA mode,
1284  * if valid, return 0, else return -1
1285  */
1286 static int
1287 check_socket_id(const unsigned int socket_id)
1288 {
1289 	static int warning_once = 0;
1290 
1291 	if (new_socket_id(socket_id)) {
1292 		if (!warning_once && numa_support)
1293 			fprintf(stderr,
1294 				"Warning: NUMA should be configured manually by using --port-numa-config and --ring-numa-config parameters along with --numa.\n");
1295 		warning_once = 1;
1296 		return -1;
1297 	}
1298 	return 0;
1299 }
1300 
1301 /*
1302  * Get the allowed maximum number of RX queues.
1303  * *pid return the port id which has minimal value of
1304  * max_rx_queues in all ports.
1305  */
1306 queueid_t
1307 get_allowed_max_nb_rxq(portid_t *pid)
1308 {
1309 	queueid_t allowed_max_rxq = RTE_MAX_QUEUES_PER_PORT;
1310 	bool max_rxq_valid = false;
1311 	portid_t pi;
1312 	struct rte_eth_dev_info dev_info;
1313 
1314 	RTE_ETH_FOREACH_DEV(pi) {
1315 		if (eth_dev_info_get_print_err(pi, &dev_info) != 0)
1316 			continue;
1317 
1318 		max_rxq_valid = true;
1319 		if (dev_info.max_rx_queues < allowed_max_rxq) {
1320 			allowed_max_rxq = dev_info.max_rx_queues;
1321 			*pid = pi;
1322 		}
1323 	}
1324 	return max_rxq_valid ? allowed_max_rxq : 0;
1325 }
1326 
1327 /*
1328  * Check input rxq is valid or not.
1329  * If input rxq is not greater than any of maximum number
1330  * of RX queues of all ports, it is valid.
1331  * if valid, return 0, else return -1
1332  */
1333 int
1334 check_nb_rxq(queueid_t rxq)
1335 {
1336 	queueid_t allowed_max_rxq;
1337 	portid_t pid = 0;
1338 
1339 	allowed_max_rxq = get_allowed_max_nb_rxq(&pid);
1340 	if (rxq > allowed_max_rxq) {
1341 		fprintf(stderr,
1342 			"Fail: input rxq (%u) can't be greater than max_rx_queues (%u) of port %u\n",
1343 			rxq, allowed_max_rxq, pid);
1344 		return -1;
1345 	}
1346 	return 0;
1347 }
1348 
1349 /*
1350  * Get the allowed maximum number of TX queues.
1351  * *pid return the port id which has minimal value of
1352  * max_tx_queues in all ports.
1353  */
1354 queueid_t
1355 get_allowed_max_nb_txq(portid_t *pid)
1356 {
1357 	queueid_t allowed_max_txq = RTE_MAX_QUEUES_PER_PORT;
1358 	bool max_txq_valid = false;
1359 	portid_t pi;
1360 	struct rte_eth_dev_info dev_info;
1361 
1362 	RTE_ETH_FOREACH_DEV(pi) {
1363 		if (eth_dev_info_get_print_err(pi, &dev_info) != 0)
1364 			continue;
1365 
1366 		max_txq_valid = true;
1367 		if (dev_info.max_tx_queues < allowed_max_txq) {
1368 			allowed_max_txq = dev_info.max_tx_queues;
1369 			*pid = pi;
1370 		}
1371 	}
1372 	return max_txq_valid ? allowed_max_txq : 0;
1373 }
1374 
1375 /*
1376  * Check input txq is valid or not.
1377  * If input txq is not greater than any of maximum number
1378  * of TX queues of all ports, it is valid.
1379  * if valid, return 0, else return -1
1380  */
1381 int
1382 check_nb_txq(queueid_t txq)
1383 {
1384 	queueid_t allowed_max_txq;
1385 	portid_t pid = 0;
1386 
1387 	allowed_max_txq = get_allowed_max_nb_txq(&pid);
1388 	if (txq > allowed_max_txq) {
1389 		fprintf(stderr,
1390 			"Fail: input txq (%u) can't be greater than max_tx_queues (%u) of port %u\n",
1391 			txq, allowed_max_txq, pid);
1392 		return -1;
1393 	}
1394 	return 0;
1395 }
1396 
1397 /*
1398  * Get the allowed maximum number of RXDs of every rx queue.
1399  * *pid return the port id which has minimal value of
1400  * max_rxd in all queues of all ports.
1401  */
1402 static uint16_t
1403 get_allowed_max_nb_rxd(portid_t *pid)
1404 {
1405 	uint16_t allowed_max_rxd = UINT16_MAX;
1406 	portid_t pi;
1407 	struct rte_eth_dev_info dev_info;
1408 
1409 	RTE_ETH_FOREACH_DEV(pi) {
1410 		if (eth_dev_info_get_print_err(pi, &dev_info) != 0)
1411 			continue;
1412 
1413 		if (dev_info.rx_desc_lim.nb_max < allowed_max_rxd) {
1414 			allowed_max_rxd = dev_info.rx_desc_lim.nb_max;
1415 			*pid = pi;
1416 		}
1417 	}
1418 	return allowed_max_rxd;
1419 }
1420 
1421 /*
1422  * Get the allowed minimal number of RXDs of every rx queue.
1423  * *pid return the port id which has minimal value of
1424  * min_rxd in all queues of all ports.
1425  */
1426 static uint16_t
1427 get_allowed_min_nb_rxd(portid_t *pid)
1428 {
1429 	uint16_t allowed_min_rxd = 0;
1430 	portid_t pi;
1431 	struct rte_eth_dev_info dev_info;
1432 
1433 	RTE_ETH_FOREACH_DEV(pi) {
1434 		if (eth_dev_info_get_print_err(pi, &dev_info) != 0)
1435 			continue;
1436 
1437 		if (dev_info.rx_desc_lim.nb_min > allowed_min_rxd) {
1438 			allowed_min_rxd = dev_info.rx_desc_lim.nb_min;
1439 			*pid = pi;
1440 		}
1441 	}
1442 
1443 	return allowed_min_rxd;
1444 }
1445 
1446 /*
1447  * Check input rxd is valid or not.
1448  * If input rxd is not greater than any of maximum number
1449  * of RXDs of every Rx queues and is not less than any of
1450  * minimal number of RXDs of every Rx queues, it is valid.
1451  * if valid, return 0, else return -1
1452  */
1453 int
1454 check_nb_rxd(queueid_t rxd)
1455 {
1456 	uint16_t allowed_max_rxd;
1457 	uint16_t allowed_min_rxd;
1458 	portid_t pid = 0;
1459 
1460 	allowed_max_rxd = get_allowed_max_nb_rxd(&pid);
1461 	if (rxd > allowed_max_rxd) {
1462 		fprintf(stderr,
1463 			"Fail: input rxd (%u) can't be greater than max_rxds (%u) of port %u\n",
1464 			rxd, allowed_max_rxd, pid);
1465 		return -1;
1466 	}
1467 
1468 	allowed_min_rxd = get_allowed_min_nb_rxd(&pid);
1469 	if (rxd < allowed_min_rxd) {
1470 		fprintf(stderr,
1471 			"Fail: input rxd (%u) can't be less than min_rxds (%u) of port %u\n",
1472 			rxd, allowed_min_rxd, pid);
1473 		return -1;
1474 	}
1475 
1476 	return 0;
1477 }
1478 
1479 /*
1480  * Get the allowed maximum number of TXDs of every rx queues.
1481  * *pid return the port id which has minimal value of
1482  * max_txd in every tx queue.
1483  */
1484 static uint16_t
1485 get_allowed_max_nb_txd(portid_t *pid)
1486 {
1487 	uint16_t allowed_max_txd = UINT16_MAX;
1488 	portid_t pi;
1489 	struct rte_eth_dev_info dev_info;
1490 
1491 	RTE_ETH_FOREACH_DEV(pi) {
1492 		if (eth_dev_info_get_print_err(pi, &dev_info) != 0)
1493 			continue;
1494 
1495 		if (dev_info.tx_desc_lim.nb_max < allowed_max_txd) {
1496 			allowed_max_txd = dev_info.tx_desc_lim.nb_max;
1497 			*pid = pi;
1498 		}
1499 	}
1500 	return allowed_max_txd;
1501 }
1502 
1503 /*
1504  * Get the allowed maximum number of TXDs of every tx queues.
1505  * *pid return the port id which has minimal value of
1506  * min_txd in every tx queue.
1507  */
1508 static uint16_t
1509 get_allowed_min_nb_txd(portid_t *pid)
1510 {
1511 	uint16_t allowed_min_txd = 0;
1512 	portid_t pi;
1513 	struct rte_eth_dev_info dev_info;
1514 
1515 	RTE_ETH_FOREACH_DEV(pi) {
1516 		if (eth_dev_info_get_print_err(pi, &dev_info) != 0)
1517 			continue;
1518 
1519 		if (dev_info.tx_desc_lim.nb_min > allowed_min_txd) {
1520 			allowed_min_txd = dev_info.tx_desc_lim.nb_min;
1521 			*pid = pi;
1522 		}
1523 	}
1524 
1525 	return allowed_min_txd;
1526 }
1527 
1528 /*
1529  * Check input txd is valid or not.
1530  * If input txd is not greater than any of maximum number
1531  * of TXDs of every Rx queues, it is valid.
1532  * if valid, return 0, else return -1
1533  */
1534 int
1535 check_nb_txd(queueid_t txd)
1536 {
1537 	uint16_t allowed_max_txd;
1538 	uint16_t allowed_min_txd;
1539 	portid_t pid = 0;
1540 
1541 	allowed_max_txd = get_allowed_max_nb_txd(&pid);
1542 	if (txd > allowed_max_txd) {
1543 		fprintf(stderr,
1544 			"Fail: input txd (%u) can't be greater than max_txds (%u) of port %u\n",
1545 			txd, allowed_max_txd, pid);
1546 		return -1;
1547 	}
1548 
1549 	allowed_min_txd = get_allowed_min_nb_txd(&pid);
1550 	if (txd < allowed_min_txd) {
1551 		fprintf(stderr,
1552 			"Fail: input txd (%u) can't be less than min_txds (%u) of port %u\n",
1553 			txd, allowed_min_txd, pid);
1554 		return -1;
1555 	}
1556 	return 0;
1557 }
1558 
1559 
1560 /*
1561  * Get the allowed maximum number of hairpin queues.
1562  * *pid return the port id which has minimal value of
1563  * max_hairpin_queues in all ports.
1564  */
1565 queueid_t
1566 get_allowed_max_nb_hairpinq(portid_t *pid)
1567 {
1568 	queueid_t allowed_max_hairpinq = RTE_MAX_QUEUES_PER_PORT;
1569 	portid_t pi;
1570 	struct rte_eth_hairpin_cap cap;
1571 
1572 	RTE_ETH_FOREACH_DEV(pi) {
1573 		if (rte_eth_dev_hairpin_capability_get(pi, &cap) != 0) {
1574 			*pid = pi;
1575 			return 0;
1576 		}
1577 		if (cap.max_nb_queues < allowed_max_hairpinq) {
1578 			allowed_max_hairpinq = cap.max_nb_queues;
1579 			*pid = pi;
1580 		}
1581 	}
1582 	return allowed_max_hairpinq;
1583 }
1584 
1585 /*
1586  * Check input hairpin is valid or not.
1587  * If input hairpin is not greater than any of maximum number
1588  * of hairpin queues of all ports, it is valid.
1589  * if valid, return 0, else return -1
1590  */
1591 int
1592 check_nb_hairpinq(queueid_t hairpinq)
1593 {
1594 	queueid_t allowed_max_hairpinq;
1595 	portid_t pid = 0;
1596 
1597 	allowed_max_hairpinq = get_allowed_max_nb_hairpinq(&pid);
1598 	if (hairpinq > allowed_max_hairpinq) {
1599 		fprintf(stderr,
1600 			"Fail: input hairpin (%u) can't be greater than max_hairpin_queues (%u) of port %u\n",
1601 			hairpinq, allowed_max_hairpinq, pid);
1602 		return -1;
1603 	}
1604 	return 0;
1605 }
1606 
1607 static int
1608 get_eth_overhead(struct rte_eth_dev_info *dev_info)
1609 {
1610 	uint32_t eth_overhead;
1611 
1612 	if (dev_info->max_mtu != UINT16_MAX &&
1613 	    dev_info->max_rx_pktlen > dev_info->max_mtu)
1614 		eth_overhead = dev_info->max_rx_pktlen - dev_info->max_mtu;
1615 	else
1616 		eth_overhead = RTE_ETHER_HDR_LEN + RTE_ETHER_CRC_LEN;
1617 
1618 	return eth_overhead;
1619 }
1620 
1621 static void
1622 init_config_port_offloads(portid_t pid, uint32_t socket_id)
1623 {
1624 	struct rte_port *port = &ports[pid];
1625 	int ret;
1626 	int i;
1627 
1628 	eth_rx_metadata_negotiate_mp(pid);
1629 
1630 	port->dev_conf.txmode = tx_mode;
1631 	port->dev_conf.rxmode = rx_mode;
1632 
1633 	ret = eth_dev_info_get_print_err(pid, &port->dev_info);
1634 	if (ret != 0)
1635 		rte_exit(EXIT_FAILURE, "rte_eth_dev_info_get() failed\n");
1636 
1637 	if (!(port->dev_info.tx_offload_capa & RTE_ETH_TX_OFFLOAD_MBUF_FAST_FREE))
1638 		port->dev_conf.txmode.offloads &=
1639 			~RTE_ETH_TX_OFFLOAD_MBUF_FAST_FREE;
1640 
1641 	/* Apply Rx offloads configuration */
1642 	for (i = 0; i < port->dev_info.max_rx_queues; i++)
1643 		port->rxq[i].conf.offloads = port->dev_conf.rxmode.offloads;
1644 	/* Apply Tx offloads configuration */
1645 	for (i = 0; i < port->dev_info.max_tx_queues; i++)
1646 		port->txq[i].conf.offloads = port->dev_conf.txmode.offloads;
1647 
1648 	if (eth_link_speed)
1649 		port->dev_conf.link_speeds = eth_link_speed;
1650 
1651 	if (max_rx_pkt_len)
1652 		port->dev_conf.rxmode.mtu = max_rx_pkt_len -
1653 			get_eth_overhead(&port->dev_info);
1654 
1655 	/* set flag to initialize port/queue */
1656 	port->need_reconfig = 1;
1657 	port->need_reconfig_queues = 1;
1658 	port->socket_id = socket_id;
1659 	port->tx_metadata = 0;
1660 
1661 	/*
1662 	 * Check for maximum number of segments per MTU.
1663 	 * Accordingly update the mbuf data size.
1664 	 */
1665 	if (port->dev_info.rx_desc_lim.nb_mtu_seg_max != UINT16_MAX &&
1666 	    port->dev_info.rx_desc_lim.nb_mtu_seg_max != 0) {
1667 		uint32_t eth_overhead = get_eth_overhead(&port->dev_info);
1668 		uint16_t mtu;
1669 
1670 		if (rte_eth_dev_get_mtu(pid, &mtu) == 0) {
1671 			uint16_t data_size = (mtu + eth_overhead) /
1672 				port->dev_info.rx_desc_lim.nb_mtu_seg_max;
1673 			uint16_t buffer_size = data_size + RTE_PKTMBUF_HEADROOM;
1674 
1675 			if (buffer_size > mbuf_data_size[0]) {
1676 				mbuf_data_size[0] = buffer_size;
1677 				TESTPMD_LOG(WARNING,
1678 					"Configured mbuf size of the first segment %hu\n",
1679 					mbuf_data_size[0]);
1680 			}
1681 		}
1682 	}
1683 }
1684 
1685 static void
1686 init_config(void)
1687 {
1688 	portid_t pid;
1689 	struct rte_mempool *mbp;
1690 	unsigned int nb_mbuf_per_pool;
1691 	lcoreid_t  lc_id;
1692 #ifdef RTE_LIB_GRO
1693 	struct rte_gro_param gro_param;
1694 #endif
1695 #ifdef RTE_LIB_GSO
1696 	uint32_t gso_types;
1697 #endif
1698 
1699 	/* Configuration of logical cores. */
1700 	fwd_lcores = rte_zmalloc("testpmd: fwd_lcores",
1701 				sizeof(struct fwd_lcore *) * nb_lcores,
1702 				RTE_CACHE_LINE_SIZE);
1703 	if (fwd_lcores == NULL) {
1704 		rte_exit(EXIT_FAILURE, "rte_zmalloc(%d (struct fwd_lcore *)) "
1705 							"failed\n", nb_lcores);
1706 	}
1707 	for (lc_id = 0; lc_id < nb_lcores; lc_id++) {
1708 		fwd_lcores[lc_id] = rte_zmalloc("testpmd: struct fwd_lcore",
1709 					       sizeof(struct fwd_lcore),
1710 					       RTE_CACHE_LINE_SIZE);
1711 		if (fwd_lcores[lc_id] == NULL) {
1712 			rte_exit(EXIT_FAILURE, "rte_zmalloc(struct fwd_lcore) "
1713 								"failed\n");
1714 		}
1715 		fwd_lcores[lc_id]->cpuid_idx = lc_id;
1716 	}
1717 
1718 	RTE_ETH_FOREACH_DEV(pid) {
1719 		uint32_t socket_id;
1720 
1721 		if (numa_support) {
1722 			socket_id = port_numa[pid];
1723 			if (port_numa[pid] == NUMA_NO_CONFIG) {
1724 				socket_id = rte_eth_dev_socket_id(pid);
1725 
1726 				/*
1727 				 * if socket_id is invalid,
1728 				 * set to the first available socket.
1729 				 */
1730 				if (check_socket_id(socket_id) < 0)
1731 					socket_id = socket_ids[0];
1732 			}
1733 		} else {
1734 			socket_id = (socket_num == UMA_NO_CONFIG) ?
1735 				    0 : socket_num;
1736 		}
1737 		/* Apply default TxRx configuration for all ports */
1738 		init_config_port_offloads(pid, socket_id);
1739 	}
1740 	/*
1741 	 * Create pools of mbuf.
1742 	 * If NUMA support is disabled, create a single pool of mbuf in
1743 	 * socket 0 memory by default.
1744 	 * Otherwise, create a pool of mbuf in the memory of sockets 0 and 1.
1745 	 *
1746 	 * Use the maximum value of nb_rxd and nb_txd here, then nb_rxd and
1747 	 * nb_txd can be configured at run time.
1748 	 */
1749 	if (param_total_num_mbufs)
1750 		nb_mbuf_per_pool = param_total_num_mbufs;
1751 	else {
1752 		nb_mbuf_per_pool = RX_DESC_MAX +
1753 			(nb_lcores * mb_mempool_cache) +
1754 			TX_DESC_MAX + MAX_PKT_BURST;
1755 		nb_mbuf_per_pool *= RTE_MAX_ETHPORTS;
1756 	}
1757 
1758 	if (numa_support) {
1759 		uint8_t i, j;
1760 
1761 		for (i = 0; i < num_sockets; i++)
1762 			for (j = 0; j < mbuf_data_size_n; j++)
1763 				mempools[i * MAX_SEGS_BUFFER_SPLIT + j] =
1764 					mbuf_pool_create(mbuf_data_size[j],
1765 							  nb_mbuf_per_pool,
1766 							  socket_ids[i], j);
1767 	} else {
1768 		uint8_t i;
1769 
1770 		for (i = 0; i < mbuf_data_size_n; i++)
1771 			mempools[i] = mbuf_pool_create
1772 					(mbuf_data_size[i],
1773 					 nb_mbuf_per_pool,
1774 					 socket_num == UMA_NO_CONFIG ?
1775 					 0 : socket_num, i);
1776 	}
1777 
1778 	init_port_config();
1779 
1780 #ifdef RTE_LIB_GSO
1781 	gso_types = RTE_ETH_TX_OFFLOAD_TCP_TSO | RTE_ETH_TX_OFFLOAD_VXLAN_TNL_TSO |
1782 		RTE_ETH_TX_OFFLOAD_GRE_TNL_TSO | RTE_ETH_TX_OFFLOAD_UDP_TSO;
1783 #endif
1784 	/*
1785 	 * Records which Mbuf pool to use by each logical core, if needed.
1786 	 */
1787 	for (lc_id = 0; lc_id < nb_lcores; lc_id++) {
1788 		mbp = mbuf_pool_find(
1789 			rte_lcore_to_socket_id(fwd_lcores_cpuids[lc_id]), 0);
1790 
1791 		if (mbp == NULL)
1792 			mbp = mbuf_pool_find(0, 0);
1793 		fwd_lcores[lc_id]->mbp = mbp;
1794 #ifdef RTE_LIB_GSO
1795 		/* initialize GSO context */
1796 		fwd_lcores[lc_id]->gso_ctx.direct_pool = mbp;
1797 		fwd_lcores[lc_id]->gso_ctx.indirect_pool = mbp;
1798 		fwd_lcores[lc_id]->gso_ctx.gso_types = gso_types;
1799 		fwd_lcores[lc_id]->gso_ctx.gso_size = RTE_ETHER_MAX_LEN -
1800 			RTE_ETHER_CRC_LEN;
1801 		fwd_lcores[lc_id]->gso_ctx.flag = 0;
1802 #endif
1803 	}
1804 
1805 	fwd_config_setup();
1806 
1807 #ifdef RTE_LIB_GRO
1808 	/* create a gro context for each lcore */
1809 	gro_param.gro_types = RTE_GRO_TCP_IPV4;
1810 	gro_param.max_flow_num = GRO_MAX_FLUSH_CYCLES;
1811 	gro_param.max_item_per_flow = MAX_PKT_BURST;
1812 	for (lc_id = 0; lc_id < nb_lcores; lc_id++) {
1813 		gro_param.socket_id = rte_lcore_to_socket_id(
1814 				fwd_lcores_cpuids[lc_id]);
1815 		fwd_lcores[lc_id]->gro_ctx = rte_gro_ctx_create(&gro_param);
1816 		if (fwd_lcores[lc_id]->gro_ctx == NULL) {
1817 			rte_exit(EXIT_FAILURE,
1818 					"rte_gro_ctx_create() failed\n");
1819 		}
1820 	}
1821 #endif
1822 }
1823 
1824 
1825 void
1826 reconfig(portid_t new_port_id, unsigned socket_id)
1827 {
1828 	/* Reconfiguration of Ethernet ports. */
1829 	init_config_port_offloads(new_port_id, socket_id);
1830 	init_port_config();
1831 }
1832 
1833 int
1834 init_fwd_streams(void)
1835 {
1836 	portid_t pid;
1837 	struct rte_port *port;
1838 	streamid_t sm_id, nb_fwd_streams_new;
1839 	queueid_t q;
1840 
1841 	/* set socket id according to numa or not */
1842 	RTE_ETH_FOREACH_DEV(pid) {
1843 		port = &ports[pid];
1844 		if (nb_rxq > port->dev_info.max_rx_queues) {
1845 			fprintf(stderr,
1846 				"Fail: nb_rxq(%d) is greater than max_rx_queues(%d)\n",
1847 				nb_rxq, port->dev_info.max_rx_queues);
1848 			return -1;
1849 		}
1850 		if (nb_txq > port->dev_info.max_tx_queues) {
1851 			fprintf(stderr,
1852 				"Fail: nb_txq(%d) is greater than max_tx_queues(%d)\n",
1853 				nb_txq, port->dev_info.max_tx_queues);
1854 			return -1;
1855 		}
1856 		if (numa_support) {
1857 			if (port_numa[pid] != NUMA_NO_CONFIG)
1858 				port->socket_id = port_numa[pid];
1859 			else {
1860 				port->socket_id = rte_eth_dev_socket_id(pid);
1861 
1862 				/*
1863 				 * if socket_id is invalid,
1864 				 * set to the first available socket.
1865 				 */
1866 				if (check_socket_id(port->socket_id) < 0)
1867 					port->socket_id = socket_ids[0];
1868 			}
1869 		}
1870 		else {
1871 			if (socket_num == UMA_NO_CONFIG)
1872 				port->socket_id = 0;
1873 			else
1874 				port->socket_id = socket_num;
1875 		}
1876 	}
1877 
1878 	q = RTE_MAX(nb_rxq, nb_txq);
1879 	if (q == 0) {
1880 		fprintf(stderr,
1881 			"Fail: Cannot allocate fwd streams as number of queues is 0\n");
1882 		return -1;
1883 	}
1884 	nb_fwd_streams_new = (streamid_t)(nb_ports * q);
1885 	if (nb_fwd_streams_new == nb_fwd_streams)
1886 		return 0;
1887 	/* clear the old */
1888 	if (fwd_streams != NULL) {
1889 		for (sm_id = 0; sm_id < nb_fwd_streams; sm_id++) {
1890 			if (fwd_streams[sm_id] == NULL)
1891 				continue;
1892 			rte_free(fwd_streams[sm_id]);
1893 			fwd_streams[sm_id] = NULL;
1894 		}
1895 		rte_free(fwd_streams);
1896 		fwd_streams = NULL;
1897 	}
1898 
1899 	/* init new */
1900 	nb_fwd_streams = nb_fwd_streams_new;
1901 	if (nb_fwd_streams) {
1902 		fwd_streams = rte_zmalloc("testpmd: fwd_streams",
1903 			sizeof(struct fwd_stream *) * nb_fwd_streams,
1904 			RTE_CACHE_LINE_SIZE);
1905 		if (fwd_streams == NULL)
1906 			rte_exit(EXIT_FAILURE, "rte_zmalloc(%d"
1907 				 " (struct fwd_stream *)) failed\n",
1908 				 nb_fwd_streams);
1909 
1910 		for (sm_id = 0; sm_id < nb_fwd_streams; sm_id++) {
1911 			fwd_streams[sm_id] = rte_zmalloc("testpmd:"
1912 				" struct fwd_stream", sizeof(struct fwd_stream),
1913 				RTE_CACHE_LINE_SIZE);
1914 			if (fwd_streams[sm_id] == NULL)
1915 				rte_exit(EXIT_FAILURE, "rte_zmalloc"
1916 					 "(struct fwd_stream) failed\n");
1917 		}
1918 	}
1919 
1920 	return 0;
1921 }
1922 
1923 static void
1924 pkt_burst_stats_display(const char *rx_tx, struct pkt_burst_stats *pbs)
1925 {
1926 	uint64_t total_burst, sburst;
1927 	uint64_t nb_burst;
1928 	uint64_t burst_stats[4];
1929 	uint16_t pktnb_stats[4];
1930 	uint16_t nb_pkt;
1931 	int burst_percent[4], sburstp;
1932 	int i;
1933 
1934 	/*
1935 	 * First compute the total number of packet bursts and the
1936 	 * two highest numbers of bursts of the same number of packets.
1937 	 */
1938 	memset(&burst_stats, 0x0, sizeof(burst_stats));
1939 	memset(&pktnb_stats, 0x0, sizeof(pktnb_stats));
1940 
1941 	/* Show stats for 0 burst size always */
1942 	total_burst = pbs->pkt_burst_spread[0];
1943 	burst_stats[0] = pbs->pkt_burst_spread[0];
1944 	pktnb_stats[0] = 0;
1945 
1946 	/* Find the next 2 burst sizes with highest occurrences. */
1947 	for (nb_pkt = 1; nb_pkt < MAX_PKT_BURST + 1; nb_pkt++) {
1948 		nb_burst = pbs->pkt_burst_spread[nb_pkt];
1949 
1950 		if (nb_burst == 0)
1951 			continue;
1952 
1953 		total_burst += nb_burst;
1954 
1955 		if (nb_burst > burst_stats[1]) {
1956 			burst_stats[2] = burst_stats[1];
1957 			pktnb_stats[2] = pktnb_stats[1];
1958 			burst_stats[1] = nb_burst;
1959 			pktnb_stats[1] = nb_pkt;
1960 		} else if (nb_burst > burst_stats[2]) {
1961 			burst_stats[2] = nb_burst;
1962 			pktnb_stats[2] = nb_pkt;
1963 		}
1964 	}
1965 	if (total_burst == 0)
1966 		return;
1967 
1968 	printf("  %s-bursts : %"PRIu64" [", rx_tx, total_burst);
1969 	for (i = 0, sburst = 0, sburstp = 0; i < 4; i++) {
1970 		if (i == 3) {
1971 			printf("%d%% of other]\n", 100 - sburstp);
1972 			return;
1973 		}
1974 
1975 		sburst += burst_stats[i];
1976 		if (sburst == total_burst) {
1977 			printf("%d%% of %d pkts]\n",
1978 				100 - sburstp, (int) pktnb_stats[i]);
1979 			return;
1980 		}
1981 
1982 		burst_percent[i] =
1983 			(double)burst_stats[i] / total_burst * 100;
1984 		printf("%d%% of %d pkts + ",
1985 			burst_percent[i], (int) pktnb_stats[i]);
1986 		sburstp += burst_percent[i];
1987 	}
1988 }
1989 
1990 static void
1991 fwd_stream_stats_display(streamid_t stream_id)
1992 {
1993 	struct fwd_stream *fs;
1994 	static const char *fwd_top_stats_border = "-------";
1995 
1996 	fs = fwd_streams[stream_id];
1997 	if ((fs->rx_packets == 0) && (fs->tx_packets == 0) &&
1998 	    (fs->fwd_dropped == 0))
1999 		return;
2000 	printf("\n  %s Forward Stats for RX Port=%2d/Queue=%2d -> "
2001 	       "TX Port=%2d/Queue=%2d %s\n",
2002 	       fwd_top_stats_border, fs->rx_port, fs->rx_queue,
2003 	       fs->tx_port, fs->tx_queue, fwd_top_stats_border);
2004 	printf("  RX-packets: %-14"PRIu64" TX-packets: %-14"PRIu64
2005 	       " TX-dropped: %-14"PRIu64,
2006 	       fs->rx_packets, fs->tx_packets, fs->fwd_dropped);
2007 
2008 	/* if checksum mode */
2009 	if (cur_fwd_eng == &csum_fwd_engine) {
2010 		printf("  RX- bad IP checksum: %-14"PRIu64
2011 		       "  Rx- bad L4 checksum: %-14"PRIu64
2012 		       " Rx- bad outer L4 checksum: %-14"PRIu64"\n",
2013 			fs->rx_bad_ip_csum, fs->rx_bad_l4_csum,
2014 			fs->rx_bad_outer_l4_csum);
2015 		printf(" RX- bad outer IP checksum: %-14"PRIu64"\n",
2016 			fs->rx_bad_outer_ip_csum);
2017 	} else {
2018 		printf("\n");
2019 	}
2020 
2021 	if (record_burst_stats) {
2022 		pkt_burst_stats_display("RX", &fs->rx_burst_stats);
2023 		pkt_burst_stats_display("TX", &fs->tx_burst_stats);
2024 	}
2025 }
2026 
2027 void
2028 fwd_stats_display(void)
2029 {
2030 	static const char *fwd_stats_border = "----------------------";
2031 	static const char *acc_stats_border = "+++++++++++++++";
2032 	struct {
2033 		struct fwd_stream *rx_stream;
2034 		struct fwd_stream *tx_stream;
2035 		uint64_t tx_dropped;
2036 		uint64_t rx_bad_ip_csum;
2037 		uint64_t rx_bad_l4_csum;
2038 		uint64_t rx_bad_outer_l4_csum;
2039 		uint64_t rx_bad_outer_ip_csum;
2040 	} ports_stats[RTE_MAX_ETHPORTS];
2041 	uint64_t total_rx_dropped = 0;
2042 	uint64_t total_tx_dropped = 0;
2043 	uint64_t total_rx_nombuf = 0;
2044 	struct rte_eth_stats stats;
2045 	uint64_t fwd_cycles = 0;
2046 	uint64_t total_recv = 0;
2047 	uint64_t total_xmit = 0;
2048 	struct rte_port *port;
2049 	streamid_t sm_id;
2050 	portid_t pt_id;
2051 	int ret;
2052 	int i;
2053 
2054 	memset(ports_stats, 0, sizeof(ports_stats));
2055 
2056 	for (sm_id = 0; sm_id < cur_fwd_config.nb_fwd_streams; sm_id++) {
2057 		struct fwd_stream *fs = fwd_streams[sm_id];
2058 
2059 		if (cur_fwd_config.nb_fwd_streams >
2060 		    cur_fwd_config.nb_fwd_ports) {
2061 			fwd_stream_stats_display(sm_id);
2062 		} else {
2063 			ports_stats[fs->tx_port].tx_stream = fs;
2064 			ports_stats[fs->rx_port].rx_stream = fs;
2065 		}
2066 
2067 		ports_stats[fs->tx_port].tx_dropped += fs->fwd_dropped;
2068 
2069 		ports_stats[fs->rx_port].rx_bad_ip_csum += fs->rx_bad_ip_csum;
2070 		ports_stats[fs->rx_port].rx_bad_l4_csum += fs->rx_bad_l4_csum;
2071 		ports_stats[fs->rx_port].rx_bad_outer_l4_csum +=
2072 				fs->rx_bad_outer_l4_csum;
2073 		ports_stats[fs->rx_port].rx_bad_outer_ip_csum +=
2074 				fs->rx_bad_outer_ip_csum;
2075 
2076 		if (record_core_cycles)
2077 			fwd_cycles += fs->busy_cycles;
2078 	}
2079 	for (i = 0; i < cur_fwd_config.nb_fwd_ports; i++) {
2080 		uint64_t tx_dropped = 0;
2081 
2082 		pt_id = fwd_ports_ids[i];
2083 		port = &ports[pt_id];
2084 
2085 		ret = rte_eth_stats_get(pt_id, &stats);
2086 		if (ret != 0) {
2087 			fprintf(stderr,
2088 				"%s: Error: failed to get stats (port %u): %d",
2089 				__func__, pt_id, ret);
2090 			continue;
2091 		}
2092 		stats.ipackets -= port->stats.ipackets;
2093 		stats.opackets -= port->stats.opackets;
2094 		stats.ibytes -= port->stats.ibytes;
2095 		stats.obytes -= port->stats.obytes;
2096 		stats.imissed -= port->stats.imissed;
2097 		stats.oerrors -= port->stats.oerrors;
2098 		stats.rx_nombuf -= port->stats.rx_nombuf;
2099 
2100 		total_recv += stats.ipackets;
2101 		total_xmit += stats.opackets;
2102 		total_rx_dropped += stats.imissed;
2103 		tx_dropped += ports_stats[pt_id].tx_dropped;
2104 		tx_dropped += stats.oerrors;
2105 		total_tx_dropped += tx_dropped;
2106 		total_rx_nombuf  += stats.rx_nombuf;
2107 
2108 		printf("\n  %s Forward statistics for port %-2d %s\n",
2109 		       fwd_stats_border, pt_id, fwd_stats_border);
2110 
2111 		printf("  RX-packets: %-14"PRIu64" RX-dropped: %-14"PRIu64
2112 		       "RX-total: %-"PRIu64"\n", stats.ipackets, stats.imissed,
2113 		       stats.ipackets + stats.imissed);
2114 
2115 		if (cur_fwd_eng == &csum_fwd_engine) {
2116 			printf("  Bad-ipcsum: %-14"PRIu64
2117 			       " Bad-l4csum: %-14"PRIu64
2118 			       "Bad-outer-l4csum: %-14"PRIu64"\n",
2119 			       ports_stats[pt_id].rx_bad_ip_csum,
2120 			       ports_stats[pt_id].rx_bad_l4_csum,
2121 			       ports_stats[pt_id].rx_bad_outer_l4_csum);
2122 			printf("  Bad-outer-ipcsum: %-14"PRIu64"\n",
2123 			       ports_stats[pt_id].rx_bad_outer_ip_csum);
2124 		}
2125 		if (stats.ierrors + stats.rx_nombuf > 0) {
2126 			printf("  RX-error: %-"PRIu64"\n", stats.ierrors);
2127 			printf("  RX-nombufs: %-14"PRIu64"\n", stats.rx_nombuf);
2128 		}
2129 
2130 		printf("  TX-packets: %-14"PRIu64" TX-dropped: %-14"PRIu64
2131 		       "TX-total: %-"PRIu64"\n",
2132 		       stats.opackets, tx_dropped,
2133 		       stats.opackets + tx_dropped);
2134 
2135 		if (record_burst_stats) {
2136 			if (ports_stats[pt_id].rx_stream)
2137 				pkt_burst_stats_display("RX",
2138 					&ports_stats[pt_id].rx_stream->rx_burst_stats);
2139 			if (ports_stats[pt_id].tx_stream)
2140 				pkt_burst_stats_display("TX",
2141 				&ports_stats[pt_id].tx_stream->tx_burst_stats);
2142 		}
2143 
2144 		printf("  %s--------------------------------%s\n",
2145 		       fwd_stats_border, fwd_stats_border);
2146 	}
2147 
2148 	printf("\n  %s Accumulated forward statistics for all ports"
2149 	       "%s\n",
2150 	       acc_stats_border, acc_stats_border);
2151 	printf("  RX-packets: %-14"PRIu64" RX-dropped: %-14"PRIu64"RX-total: "
2152 	       "%-"PRIu64"\n"
2153 	       "  TX-packets: %-14"PRIu64" TX-dropped: %-14"PRIu64"TX-total: "
2154 	       "%-"PRIu64"\n",
2155 	       total_recv, total_rx_dropped, total_recv + total_rx_dropped,
2156 	       total_xmit, total_tx_dropped, total_xmit + total_tx_dropped);
2157 	if (total_rx_nombuf > 0)
2158 		printf("  RX-nombufs: %-14"PRIu64"\n", total_rx_nombuf);
2159 	printf("  %s++++++++++++++++++++++++++++++++++++++++++++++"
2160 	       "%s\n",
2161 	       acc_stats_border, acc_stats_border);
2162 	if (record_core_cycles) {
2163 #define CYC_PER_MHZ 1E6
2164 		if (total_recv > 0 || total_xmit > 0) {
2165 			uint64_t total_pkts = 0;
2166 			if (strcmp(cur_fwd_eng->fwd_mode_name, "txonly") == 0 ||
2167 			    strcmp(cur_fwd_eng->fwd_mode_name, "flowgen") == 0)
2168 				total_pkts = total_xmit;
2169 			else
2170 				total_pkts = total_recv;
2171 
2172 			printf("\n  CPU cycles/packet=%.2F (busy cycles="
2173 			       "%"PRIu64" / total %s packets=%"PRIu64") at %"PRIu64
2174 			       " MHz Clock\n",
2175 			       (double) fwd_cycles / total_pkts,
2176 			       fwd_cycles, cur_fwd_eng->fwd_mode_name, total_pkts,
2177 			       (uint64_t)(rte_get_tsc_hz() / CYC_PER_MHZ));
2178 		}
2179 	}
2180 }
2181 
2182 void
2183 fwd_stats_reset(void)
2184 {
2185 	streamid_t sm_id;
2186 	portid_t pt_id;
2187 	int ret;
2188 	int i;
2189 
2190 	for (i = 0; i < cur_fwd_config.nb_fwd_ports; i++) {
2191 		pt_id = fwd_ports_ids[i];
2192 		ret = rte_eth_stats_get(pt_id, &ports[pt_id].stats);
2193 		if (ret != 0)
2194 			fprintf(stderr,
2195 				"%s: Error: failed to clear stats (port %u):%d",
2196 				__func__, pt_id, ret);
2197 	}
2198 	for (sm_id = 0; sm_id < cur_fwd_config.nb_fwd_streams; sm_id++) {
2199 		struct fwd_stream *fs = fwd_streams[sm_id];
2200 
2201 		fs->rx_packets = 0;
2202 		fs->tx_packets = 0;
2203 		fs->fwd_dropped = 0;
2204 		fs->rx_bad_ip_csum = 0;
2205 		fs->rx_bad_l4_csum = 0;
2206 		fs->rx_bad_outer_l4_csum = 0;
2207 		fs->rx_bad_outer_ip_csum = 0;
2208 
2209 		memset(&fs->rx_burst_stats, 0, sizeof(fs->rx_burst_stats));
2210 		memset(&fs->tx_burst_stats, 0, sizeof(fs->tx_burst_stats));
2211 		fs->busy_cycles = 0;
2212 	}
2213 }
2214 
2215 static void
2216 flush_fwd_rx_queues(void)
2217 {
2218 	struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
2219 	portid_t  rxp;
2220 	portid_t port_id;
2221 	queueid_t rxq;
2222 	uint16_t  nb_rx;
2223 	uint8_t   j;
2224 	uint64_t prev_tsc = 0, diff_tsc, cur_tsc, timer_tsc = 0;
2225 	uint64_t timer_period;
2226 
2227 	if (num_procs > 1) {
2228 		printf("multi-process not support for flushing fwd Rx queues, skip the below lines and return.\n");
2229 		return;
2230 	}
2231 
2232 	/* convert to number of cycles */
2233 	timer_period = rte_get_timer_hz(); /* 1 second timeout */
2234 
2235 	for (j = 0; j < 2; j++) {
2236 		for (rxp = 0; rxp < cur_fwd_config.nb_fwd_ports; rxp++) {
2237 			for (rxq = 0; rxq < nb_rxq; rxq++) {
2238 				port_id = fwd_ports_ids[rxp];
2239 
2240 				/* Polling stopped queues is prohibited. */
2241 				if (ports[port_id].rxq[rxq].state ==
2242 				    RTE_ETH_QUEUE_STATE_STOPPED)
2243 					continue;
2244 
2245 				/**
2246 				* testpmd can stuck in the below do while loop
2247 				* if rte_eth_rx_burst() always returns nonzero
2248 				* packets. So timer is added to exit this loop
2249 				* after 1sec timer expiry.
2250 				*/
2251 				prev_tsc = rte_rdtsc();
2252 				do {
2253 					nb_rx = rte_eth_rx_burst(port_id, rxq,
2254 						pkts_burst, MAX_PKT_BURST);
2255 					rte_pktmbuf_free_bulk(pkts_burst, nb_rx);
2256 
2257 					cur_tsc = rte_rdtsc();
2258 					diff_tsc = cur_tsc - prev_tsc;
2259 					timer_tsc += diff_tsc;
2260 				} while ((nb_rx > 0) &&
2261 					(timer_tsc < timer_period));
2262 				timer_tsc = 0;
2263 			}
2264 		}
2265 		rte_delay_ms(10); /* wait 10 milli-seconds before retrying */
2266 	}
2267 }
2268 
2269 static void
2270 run_pkt_fwd_on_lcore(struct fwd_lcore *fc, packet_fwd_t pkt_fwd)
2271 {
2272 	struct fwd_stream **fsm;
2273 	uint64_t prev_tsc;
2274 	streamid_t nb_fs;
2275 	streamid_t sm_id;
2276 #ifdef RTE_LIB_BITRATESTATS
2277 	uint64_t tics_per_1sec;
2278 	uint64_t tics_datum;
2279 	uint64_t tics_current;
2280 	uint16_t i, cnt_ports;
2281 
2282 	cnt_ports = nb_ports;
2283 	tics_datum = rte_rdtsc();
2284 	tics_per_1sec = rte_get_timer_hz();
2285 #endif
2286 	fsm = &fwd_streams[fc->stream_idx];
2287 	nb_fs = fc->stream_nb;
2288 	prev_tsc = rte_rdtsc();
2289 	do {
2290 		for (sm_id = 0; sm_id < nb_fs; sm_id++) {
2291 			struct fwd_stream *fs = fsm[sm_id];
2292 			uint64_t start_fs_tsc = 0;
2293 			bool busy;
2294 
2295 			if (fs->disabled)
2296 				continue;
2297 			if (record_core_cycles)
2298 				start_fs_tsc = rte_rdtsc();
2299 			busy = (*pkt_fwd)(fs);
2300 			if (record_core_cycles && busy)
2301 				fs->busy_cycles += rte_rdtsc() - start_fs_tsc;
2302 		}
2303 #ifdef RTE_LIB_BITRATESTATS
2304 		if (bitrate_enabled != 0 &&
2305 				bitrate_lcore_id == rte_lcore_id()) {
2306 			tics_current = rte_rdtsc();
2307 			if (tics_current - tics_datum >= tics_per_1sec) {
2308 				/* Periodic bitrate calculation */
2309 				for (i = 0; i < cnt_ports; i++)
2310 					rte_stats_bitrate_calc(bitrate_data,
2311 						ports_ids[i]);
2312 				tics_datum = tics_current;
2313 			}
2314 		}
2315 #endif
2316 #ifdef RTE_LIB_LATENCYSTATS
2317 		if (latencystats_enabled != 0 &&
2318 				latencystats_lcore_id == rte_lcore_id())
2319 			rte_latencystats_update();
2320 #endif
2321 		if (record_core_cycles) {
2322 			uint64_t tsc = rte_rdtsc();
2323 
2324 			fc->total_cycles += tsc - prev_tsc;
2325 			prev_tsc = tsc;
2326 		}
2327 	} while (! fc->stopped);
2328 }
2329 
2330 static int
2331 lcore_usage_callback(unsigned int lcore_id, struct rte_lcore_usage *usage)
2332 {
2333 	struct fwd_stream **fsm;
2334 	struct fwd_lcore *fc;
2335 	streamid_t nb_fs;
2336 	streamid_t sm_id;
2337 
2338 	fc = lcore_to_fwd_lcore(lcore_id);
2339 	if (fc == NULL)
2340 		return -1;
2341 
2342 	fsm = &fwd_streams[fc->stream_idx];
2343 	nb_fs = fc->stream_nb;
2344 	usage->busy_cycles = 0;
2345 	usage->total_cycles = fc->total_cycles;
2346 
2347 	for (sm_id = 0; sm_id < nb_fs; sm_id++) {
2348 		if (!fsm[sm_id]->disabled)
2349 			usage->busy_cycles += fsm[sm_id]->busy_cycles;
2350 	}
2351 
2352 	return 0;
2353 }
2354 
2355 static int
2356 start_pkt_forward_on_core(void *fwd_arg)
2357 {
2358 	run_pkt_fwd_on_lcore((struct fwd_lcore *) fwd_arg,
2359 			     cur_fwd_config.fwd_eng->packet_fwd);
2360 	return 0;
2361 }
2362 
2363 /*
2364  * Run the TXONLY packet forwarding engine to send a single burst of packets.
2365  * Used to start communication flows in network loopback test configurations.
2366  */
2367 static int
2368 run_one_txonly_burst_on_core(void *fwd_arg)
2369 {
2370 	struct fwd_lcore *fwd_lc;
2371 	struct fwd_lcore tmp_lcore;
2372 
2373 	fwd_lc = (struct fwd_lcore *) fwd_arg;
2374 	tmp_lcore = *fwd_lc;
2375 	tmp_lcore.stopped = 1;
2376 	run_pkt_fwd_on_lcore(&tmp_lcore, tx_only_engine.packet_fwd);
2377 	return 0;
2378 }
2379 
2380 /*
2381  * Launch packet forwarding:
2382  *     - Setup per-port forwarding context.
2383  *     - launch logical cores with their forwarding configuration.
2384  */
2385 static void
2386 launch_packet_forwarding(lcore_function_t *pkt_fwd_on_lcore)
2387 {
2388 	unsigned int i;
2389 	unsigned int lc_id;
2390 	int diag;
2391 
2392 	for (i = 0; i < cur_fwd_config.nb_fwd_lcores; i++) {
2393 		lc_id = fwd_lcores_cpuids[i];
2394 		if ((interactive == 0) || (lc_id != rte_lcore_id())) {
2395 			fwd_lcores[i]->stopped = 0;
2396 			diag = rte_eal_remote_launch(pkt_fwd_on_lcore,
2397 						     fwd_lcores[i], lc_id);
2398 			if (diag != 0)
2399 				fprintf(stderr,
2400 					"launch lcore %u failed - diag=%d\n",
2401 					lc_id, diag);
2402 		}
2403 	}
2404 }
2405 
2406 void
2407 common_fwd_stream_init(struct fwd_stream *fs)
2408 {
2409 	bool rx_stopped, tx_stopped;
2410 
2411 	rx_stopped = (ports[fs->rx_port].rxq[fs->rx_queue].state == RTE_ETH_QUEUE_STATE_STOPPED);
2412 	tx_stopped = (ports[fs->tx_port].txq[fs->tx_queue].state == RTE_ETH_QUEUE_STATE_STOPPED);
2413 	fs->disabled = rx_stopped || tx_stopped;
2414 }
2415 
2416 static void
2417 update_rx_queue_state(uint16_t port_id, uint16_t queue_id)
2418 {
2419 	struct rte_eth_rxq_info rx_qinfo;
2420 	int32_t rc;
2421 
2422 	rc = rte_eth_rx_queue_info_get(port_id,
2423 			queue_id, &rx_qinfo);
2424 	if (rc == 0) {
2425 		ports[port_id].rxq[queue_id].state =
2426 			rx_qinfo.queue_state;
2427 	} else if (rc == -ENOTSUP) {
2428 		/*
2429 		 * Do not change the rxq state for primary process
2430 		 * to ensure that the PMDs do not implement
2431 		 * rte_eth_rx_queue_info_get can forward as before.
2432 		 */
2433 		if (rte_eal_process_type() == RTE_PROC_PRIMARY)
2434 			return;
2435 		/*
2436 		 * Set the rxq state to RTE_ETH_QUEUE_STATE_STARTED
2437 		 * to ensure that the PMDs do not implement
2438 		 * rte_eth_rx_queue_info_get can forward.
2439 		 */
2440 		ports[port_id].rxq[queue_id].state =
2441 			RTE_ETH_QUEUE_STATE_STARTED;
2442 	} else {
2443 		TESTPMD_LOG(WARNING,
2444 			"Failed to get rx queue info\n");
2445 	}
2446 }
2447 
2448 static void
2449 update_tx_queue_state(uint16_t port_id, uint16_t queue_id)
2450 {
2451 	struct rte_eth_txq_info tx_qinfo;
2452 	int32_t rc;
2453 
2454 	rc = rte_eth_tx_queue_info_get(port_id,
2455 			queue_id, &tx_qinfo);
2456 	if (rc == 0) {
2457 		ports[port_id].txq[queue_id].state =
2458 			tx_qinfo.queue_state;
2459 	} else if (rc == -ENOTSUP) {
2460 		/*
2461 		 * Do not change the txq state for primary process
2462 		 * to ensure that the PMDs do not implement
2463 		 * rte_eth_tx_queue_info_get can forward as before.
2464 		 */
2465 		if (rte_eal_process_type() == RTE_PROC_PRIMARY)
2466 			return;
2467 		/*
2468 		 * Set the txq state to RTE_ETH_QUEUE_STATE_STARTED
2469 		 * to ensure that the PMDs do not implement
2470 		 * rte_eth_tx_queue_info_get can forward.
2471 		 */
2472 		ports[port_id].txq[queue_id].state =
2473 			RTE_ETH_QUEUE_STATE_STARTED;
2474 	} else {
2475 		TESTPMD_LOG(WARNING,
2476 			"Failed to get tx queue info\n");
2477 	}
2478 }
2479 
2480 static void
2481 update_queue_state(portid_t pid)
2482 {
2483 	portid_t pi;
2484 	queueid_t qi;
2485 
2486 	RTE_ETH_FOREACH_DEV(pi) {
2487 		if (pid != pi && pid != (portid_t)RTE_PORT_ALL)
2488 			continue;
2489 
2490 		for (qi = 0; qi < nb_rxq; qi++)
2491 			update_rx_queue_state(pi, qi);
2492 		for (qi = 0; qi < nb_txq; qi++)
2493 			update_tx_queue_state(pi, qi);
2494 	}
2495 }
2496 
2497 /*
2498  * Launch packet forwarding configuration.
2499  */
2500 void
2501 start_packet_forwarding(int with_tx_first)
2502 {
2503 	port_fwd_begin_t port_fwd_begin;
2504 	port_fwd_end_t  port_fwd_end;
2505 	stream_init_t stream_init = cur_fwd_eng->stream_init;
2506 	unsigned int i;
2507 
2508 	if (strcmp(cur_fwd_eng->fwd_mode_name, "rxonly") == 0 && !nb_rxq)
2509 		rte_exit(EXIT_FAILURE, "rxq are 0, cannot use rxonly fwd mode\n");
2510 
2511 	if (strcmp(cur_fwd_eng->fwd_mode_name, "txonly") == 0 && !nb_txq)
2512 		rte_exit(EXIT_FAILURE, "txq are 0, cannot use txonly fwd mode\n");
2513 
2514 	if ((strcmp(cur_fwd_eng->fwd_mode_name, "rxonly") != 0 &&
2515 		strcmp(cur_fwd_eng->fwd_mode_name, "txonly") != 0) &&
2516 		(!nb_rxq || !nb_txq))
2517 		rte_exit(EXIT_FAILURE,
2518 			"Either rxq or txq are 0, cannot use %s fwd mode\n",
2519 			cur_fwd_eng->fwd_mode_name);
2520 
2521 	if (all_ports_started() == 0) {
2522 		fprintf(stderr, "Not all ports were started\n");
2523 		return;
2524 	}
2525 	if (test_done == 0) {
2526 		fprintf(stderr, "Packet forwarding already started\n");
2527 		return;
2528 	}
2529 
2530 	fwd_config_setup();
2531 
2532 	pkt_fwd_config_display(&cur_fwd_config);
2533 	if (!pkt_fwd_shared_rxq_check())
2534 		return;
2535 
2536 	if (stream_init != NULL) {
2537 		update_queue_state(RTE_PORT_ALL);
2538 		for (i = 0; i < cur_fwd_config.nb_fwd_streams; i++)
2539 			stream_init(fwd_streams[i]);
2540 	}
2541 
2542 	port_fwd_begin = cur_fwd_config.fwd_eng->port_fwd_begin;
2543 	if (port_fwd_begin != NULL) {
2544 		for (i = 0; i < cur_fwd_config.nb_fwd_ports; i++) {
2545 			if (port_fwd_begin(fwd_ports_ids[i])) {
2546 				fprintf(stderr,
2547 					"Packet forwarding is not ready\n");
2548 				return;
2549 			}
2550 		}
2551 	}
2552 
2553 	if (with_tx_first) {
2554 		port_fwd_begin = tx_only_engine.port_fwd_begin;
2555 		if (port_fwd_begin != NULL) {
2556 			for (i = 0; i < cur_fwd_config.nb_fwd_ports; i++) {
2557 				if (port_fwd_begin(fwd_ports_ids[i])) {
2558 					fprintf(stderr,
2559 						"Packet forwarding is not ready\n");
2560 					return;
2561 				}
2562 			}
2563 		}
2564 	}
2565 
2566 	test_done = 0;
2567 
2568 	if(!no_flush_rx)
2569 		flush_fwd_rx_queues();
2570 
2571 	rxtx_config_display();
2572 
2573 	fwd_stats_reset();
2574 	if (with_tx_first) {
2575 		while (with_tx_first--) {
2576 			launch_packet_forwarding(
2577 					run_one_txonly_burst_on_core);
2578 			rte_eal_mp_wait_lcore();
2579 		}
2580 		port_fwd_end = tx_only_engine.port_fwd_end;
2581 		if (port_fwd_end != NULL) {
2582 			for (i = 0; i < cur_fwd_config.nb_fwd_ports; i++)
2583 				(*port_fwd_end)(fwd_ports_ids[i]);
2584 		}
2585 	}
2586 	launch_packet_forwarding(start_pkt_forward_on_core);
2587 }
2588 
2589 void
2590 stop_packet_forwarding(void)
2591 {
2592 	port_fwd_end_t port_fwd_end;
2593 	lcoreid_t lc_id;
2594 	portid_t pt_id;
2595 	int i;
2596 
2597 	if (test_done) {
2598 		fprintf(stderr, "Packet forwarding not started\n");
2599 		return;
2600 	}
2601 	printf("Telling cores to stop...");
2602 	for (lc_id = 0; lc_id < cur_fwd_config.nb_fwd_lcores; lc_id++)
2603 		fwd_lcores[lc_id]->stopped = 1;
2604 	printf("\nWaiting for lcores to finish...\n");
2605 	rte_eal_mp_wait_lcore();
2606 	port_fwd_end = cur_fwd_config.fwd_eng->port_fwd_end;
2607 	if (port_fwd_end != NULL) {
2608 		for (i = 0; i < cur_fwd_config.nb_fwd_ports; i++) {
2609 			pt_id = fwd_ports_ids[i];
2610 			(*port_fwd_end)(pt_id);
2611 		}
2612 	}
2613 
2614 	fwd_stats_display();
2615 
2616 	printf("\nDone.\n");
2617 	test_done = 1;
2618 }
2619 
2620 void
2621 dev_set_link_up(portid_t pid)
2622 {
2623 	if (rte_eth_dev_set_link_up(pid) < 0)
2624 		fprintf(stderr, "\nSet link up fail.\n");
2625 }
2626 
2627 void
2628 dev_set_link_down(portid_t pid)
2629 {
2630 	if (rte_eth_dev_set_link_down(pid) < 0)
2631 		fprintf(stderr, "\nSet link down fail.\n");
2632 }
2633 
2634 static int
2635 all_ports_started(void)
2636 {
2637 	portid_t pi;
2638 	struct rte_port *port;
2639 
2640 	RTE_ETH_FOREACH_DEV(pi) {
2641 		port = &ports[pi];
2642 		/* Check if there is a port which is not started */
2643 		if ((port->port_status != RTE_PORT_STARTED) &&
2644 			(port->member_flag == 0))
2645 			return 0;
2646 	}
2647 
2648 	/* No port is not started */
2649 	return 1;
2650 }
2651 
2652 int
2653 port_is_stopped(portid_t port_id)
2654 {
2655 	struct rte_port *port = &ports[port_id];
2656 
2657 	if ((port->port_status != RTE_PORT_STOPPED) &&
2658 	    (port->member_flag == 0))
2659 		return 0;
2660 	return 1;
2661 }
2662 
2663 int
2664 all_ports_stopped(void)
2665 {
2666 	portid_t pi;
2667 
2668 	RTE_ETH_FOREACH_DEV(pi) {
2669 		if (!port_is_stopped(pi))
2670 			return 0;
2671 	}
2672 
2673 	return 1;
2674 }
2675 
2676 int
2677 port_is_started(portid_t port_id)
2678 {
2679 	if (port_id_is_invalid(port_id, ENABLED_WARN))
2680 		return 0;
2681 
2682 	if (ports[port_id].port_status != RTE_PORT_STARTED)
2683 		return 0;
2684 
2685 	return 1;
2686 }
2687 
2688 #define HAIRPIN_MODE_RX_FORCE_MEMORY RTE_BIT32(8)
2689 #define HAIRPIN_MODE_TX_FORCE_MEMORY RTE_BIT32(9)
2690 
2691 #define HAIRPIN_MODE_RX_LOCKED_MEMORY RTE_BIT32(12)
2692 #define HAIRPIN_MODE_RX_RTE_MEMORY RTE_BIT32(13)
2693 
2694 #define HAIRPIN_MODE_TX_LOCKED_MEMORY RTE_BIT32(16)
2695 #define HAIRPIN_MODE_TX_RTE_MEMORY RTE_BIT32(17)
2696 
2697 
2698 /* Configure the Rx and Tx hairpin queues for the selected port. */
2699 static int
2700 setup_hairpin_queues(portid_t pi, portid_t p_pi, uint16_t cnt_pi)
2701 {
2702 	queueid_t qi;
2703 	struct rte_eth_hairpin_conf hairpin_conf = {
2704 		.peer_count = 1,
2705 	};
2706 	int i;
2707 	int diag;
2708 	struct rte_port *port = &ports[pi];
2709 	uint16_t peer_rx_port = pi;
2710 	uint16_t peer_tx_port = pi;
2711 	uint32_t manual = 1;
2712 	uint32_t tx_exp = hairpin_mode & 0x10;
2713 	uint32_t rx_force_memory = hairpin_mode & HAIRPIN_MODE_RX_FORCE_MEMORY;
2714 	uint32_t rx_locked_memory = hairpin_mode & HAIRPIN_MODE_RX_LOCKED_MEMORY;
2715 	uint32_t rx_rte_memory = hairpin_mode & HAIRPIN_MODE_RX_RTE_MEMORY;
2716 	uint32_t tx_force_memory = hairpin_mode & HAIRPIN_MODE_TX_FORCE_MEMORY;
2717 	uint32_t tx_locked_memory = hairpin_mode & HAIRPIN_MODE_TX_LOCKED_MEMORY;
2718 	uint32_t tx_rte_memory = hairpin_mode & HAIRPIN_MODE_TX_RTE_MEMORY;
2719 
2720 	if (!(hairpin_mode & 0xf)) {
2721 		peer_rx_port = pi;
2722 		peer_tx_port = pi;
2723 		manual = 0;
2724 	} else if (hairpin_mode & 0x1) {
2725 		peer_tx_port = rte_eth_find_next_owned_by(pi + 1,
2726 						       RTE_ETH_DEV_NO_OWNER);
2727 		if (peer_tx_port >= RTE_MAX_ETHPORTS)
2728 			peer_tx_port = rte_eth_find_next_owned_by(0,
2729 						RTE_ETH_DEV_NO_OWNER);
2730 		if (p_pi != RTE_MAX_ETHPORTS) {
2731 			peer_rx_port = p_pi;
2732 		} else {
2733 			uint16_t next_pi;
2734 
2735 			/* Last port will be the peer RX port of the first. */
2736 			RTE_ETH_FOREACH_DEV(next_pi)
2737 				peer_rx_port = next_pi;
2738 		}
2739 		manual = 1;
2740 	} else if (hairpin_mode & 0x2) {
2741 		if (cnt_pi & 0x1) {
2742 			peer_rx_port = p_pi;
2743 		} else {
2744 			peer_rx_port = rte_eth_find_next_owned_by(pi + 1,
2745 						RTE_ETH_DEV_NO_OWNER);
2746 			if (peer_rx_port >= RTE_MAX_ETHPORTS)
2747 				peer_rx_port = pi;
2748 		}
2749 		peer_tx_port = peer_rx_port;
2750 		manual = 1;
2751 	}
2752 
2753 	for (qi = nb_txq, i = 0; qi < nb_hairpinq + nb_txq; qi++) {
2754 		hairpin_conf.peers[0].port = peer_rx_port;
2755 		hairpin_conf.peers[0].queue = i + nb_rxq;
2756 		hairpin_conf.manual_bind = !!manual;
2757 		hairpin_conf.tx_explicit = !!tx_exp;
2758 		hairpin_conf.force_memory = !!tx_force_memory;
2759 		hairpin_conf.use_locked_device_memory = !!tx_locked_memory;
2760 		hairpin_conf.use_rte_memory = !!tx_rte_memory;
2761 		diag = rte_eth_tx_hairpin_queue_setup
2762 			(pi, qi, nb_txd, &hairpin_conf);
2763 		i++;
2764 		if (diag == 0)
2765 			continue;
2766 
2767 		/* Fail to setup rx queue, return */
2768 		if (port->port_status == RTE_PORT_HANDLING)
2769 			port->port_status = RTE_PORT_STOPPED;
2770 		else
2771 			fprintf(stderr,
2772 				"Port %d can not be set back to stopped\n", pi);
2773 		fprintf(stderr, "Fail to configure port %d hairpin queues\n",
2774 			pi);
2775 		/* try to reconfigure queues next time */
2776 		port->need_reconfig_queues = 1;
2777 		return -1;
2778 	}
2779 	for (qi = nb_rxq, i = 0; qi < nb_hairpinq + nb_rxq; qi++) {
2780 		hairpin_conf.peers[0].port = peer_tx_port;
2781 		hairpin_conf.peers[0].queue = i + nb_txq;
2782 		hairpin_conf.manual_bind = !!manual;
2783 		hairpin_conf.tx_explicit = !!tx_exp;
2784 		hairpin_conf.force_memory = !!rx_force_memory;
2785 		hairpin_conf.use_locked_device_memory = !!rx_locked_memory;
2786 		hairpin_conf.use_rte_memory = !!rx_rte_memory;
2787 		diag = rte_eth_rx_hairpin_queue_setup
2788 			(pi, qi, nb_rxd, &hairpin_conf);
2789 		i++;
2790 		if (diag == 0)
2791 			continue;
2792 
2793 		/* Fail to setup rx queue, return */
2794 		if (port->port_status == RTE_PORT_HANDLING)
2795 			port->port_status = RTE_PORT_STOPPED;
2796 		else
2797 			fprintf(stderr,
2798 				"Port %d can not be set back to stopped\n", pi);
2799 		fprintf(stderr, "Fail to configure port %d hairpin queues\n",
2800 			pi);
2801 		/* try to reconfigure queues next time */
2802 		port->need_reconfig_queues = 1;
2803 		return -1;
2804 	}
2805 	return 0;
2806 }
2807 
2808 /* Configure the Rx with optional split. */
2809 int
2810 rx_queue_setup(uint16_t port_id, uint16_t rx_queue_id,
2811 	       uint16_t nb_rx_desc, unsigned int socket_id,
2812 	       struct rte_eth_rxconf *rx_conf, struct rte_mempool *mp)
2813 {
2814 	union rte_eth_rxseg rx_useg[MAX_SEGS_BUFFER_SPLIT] = {};
2815 	struct rte_mempool *rx_mempool[MAX_MEMPOOL] = {};
2816 	struct rte_mempool *mpx;
2817 	unsigned int i, mp_n;
2818 	uint32_t prev_hdrs = 0;
2819 	int ret;
2820 
2821 
2822 	if ((rx_pkt_nb_segs > 1) &&
2823 	    (rx_conf->offloads & RTE_ETH_RX_OFFLOAD_BUFFER_SPLIT)) {
2824 		/* multi-segment configuration */
2825 		for (i = 0; i < rx_pkt_nb_segs; i++) {
2826 			struct rte_eth_rxseg_split *rx_seg = &rx_useg[i].split;
2827 			/*
2828 			 * Use last valid pool for the segments with number
2829 			 * exceeding the pool index.
2830 			 */
2831 			mp_n = (i >= mbuf_data_size_n) ? mbuf_data_size_n - 1 : i;
2832 			mpx = mbuf_pool_find(socket_id, mp_n);
2833 			/* Handle zero as mbuf data buffer size. */
2834 			rx_seg->offset = i < rx_pkt_nb_offs ?
2835 					   rx_pkt_seg_offsets[i] : 0;
2836 			rx_seg->mp = mpx ? mpx : mp;
2837 			if (rx_pkt_hdr_protos[i] != 0 && rx_pkt_seg_lengths[i] == 0) {
2838 				rx_seg->proto_hdr = rx_pkt_hdr_protos[i] & ~prev_hdrs;
2839 				prev_hdrs |= rx_seg->proto_hdr;
2840 			} else {
2841 				rx_seg->length = rx_pkt_seg_lengths[i] ?
2842 						rx_pkt_seg_lengths[i] :
2843 						mbuf_data_size[mp_n];
2844 			}
2845 		}
2846 		rx_conf->rx_nseg = rx_pkt_nb_segs;
2847 		rx_conf->rx_seg = rx_useg;
2848 		rx_conf->rx_mempools = NULL;
2849 		rx_conf->rx_nmempool = 0;
2850 		ret = rte_eth_rx_queue_setup(port_id, rx_queue_id, nb_rx_desc,
2851 				    socket_id, rx_conf, NULL);
2852 		rx_conf->rx_seg = NULL;
2853 		rx_conf->rx_nseg = 0;
2854 	} else if (multi_rx_mempool == 1) {
2855 		/* multi-pool configuration */
2856 		struct rte_eth_dev_info dev_info;
2857 
2858 		if (mbuf_data_size_n <= 1) {
2859 			fprintf(stderr, "Invalid number of mempools %u\n",
2860 				mbuf_data_size_n);
2861 			return -EINVAL;
2862 		}
2863 		ret = rte_eth_dev_info_get(port_id, &dev_info);
2864 		if (ret != 0)
2865 			return ret;
2866 		if (dev_info.max_rx_mempools == 0) {
2867 			fprintf(stderr,
2868 				"Port %u doesn't support requested multi-rx-mempool configuration.\n",
2869 				port_id);
2870 			return -ENOTSUP;
2871 		}
2872 		for (i = 0; i < mbuf_data_size_n; i++) {
2873 			mpx = mbuf_pool_find(socket_id, i);
2874 			rx_mempool[i] = mpx ? mpx : mp;
2875 		}
2876 		rx_conf->rx_mempools = rx_mempool;
2877 		rx_conf->rx_nmempool = mbuf_data_size_n;
2878 		rx_conf->rx_seg = NULL;
2879 		rx_conf->rx_nseg = 0;
2880 		ret = rte_eth_rx_queue_setup(port_id, rx_queue_id, nb_rx_desc,
2881 				    socket_id, rx_conf, NULL);
2882 		rx_conf->rx_mempools = NULL;
2883 		rx_conf->rx_nmempool = 0;
2884 	} else {
2885 		/* Single pool/segment configuration */
2886 		rx_conf->rx_seg = NULL;
2887 		rx_conf->rx_nseg = 0;
2888 		rx_conf->rx_mempools = NULL;
2889 		rx_conf->rx_nmempool = 0;
2890 		ret = rte_eth_rx_queue_setup(port_id, rx_queue_id, nb_rx_desc,
2891 				    socket_id, rx_conf, mp);
2892 	}
2893 
2894 	ports[port_id].rxq[rx_queue_id].state = rx_conf->rx_deferred_start ?
2895 						RTE_ETH_QUEUE_STATE_STOPPED :
2896 						RTE_ETH_QUEUE_STATE_STARTED;
2897 	return ret;
2898 }
2899 
2900 static int
2901 alloc_xstats_display_info(portid_t pi)
2902 {
2903 	uint64_t **ids_supp = &ports[pi].xstats_info.ids_supp;
2904 	uint64_t **prev_values = &ports[pi].xstats_info.prev_values;
2905 	uint64_t **curr_values = &ports[pi].xstats_info.curr_values;
2906 
2907 	if (xstats_display_num == 0)
2908 		return 0;
2909 
2910 	*ids_supp = calloc(xstats_display_num, sizeof(**ids_supp));
2911 	if (*ids_supp == NULL)
2912 		goto fail_ids_supp;
2913 
2914 	*prev_values = calloc(xstats_display_num,
2915 			      sizeof(**prev_values));
2916 	if (*prev_values == NULL)
2917 		goto fail_prev_values;
2918 
2919 	*curr_values = calloc(xstats_display_num,
2920 			      sizeof(**curr_values));
2921 	if (*curr_values == NULL)
2922 		goto fail_curr_values;
2923 
2924 	ports[pi].xstats_info.allocated = true;
2925 
2926 	return 0;
2927 
2928 fail_curr_values:
2929 	free(*prev_values);
2930 fail_prev_values:
2931 	free(*ids_supp);
2932 fail_ids_supp:
2933 	return -ENOMEM;
2934 }
2935 
2936 static void
2937 free_xstats_display_info(portid_t pi)
2938 {
2939 	if (!ports[pi].xstats_info.allocated)
2940 		return;
2941 	free(ports[pi].xstats_info.ids_supp);
2942 	free(ports[pi].xstats_info.prev_values);
2943 	free(ports[pi].xstats_info.curr_values);
2944 	ports[pi].xstats_info.allocated = false;
2945 }
2946 
2947 /** Fill helper structures for specified port to show extended statistics. */
2948 static void
2949 fill_xstats_display_info_for_port(portid_t pi)
2950 {
2951 	unsigned int stat, stat_supp;
2952 	const char *xstat_name;
2953 	struct rte_port *port;
2954 	uint64_t *ids_supp;
2955 	int rc;
2956 
2957 	if (xstats_display_num == 0)
2958 		return;
2959 
2960 	if (pi == (portid_t)RTE_PORT_ALL) {
2961 		fill_xstats_display_info();
2962 		return;
2963 	}
2964 
2965 	port = &ports[pi];
2966 	if (port->port_status != RTE_PORT_STARTED)
2967 		return;
2968 
2969 	if (!port->xstats_info.allocated && alloc_xstats_display_info(pi) != 0)
2970 		rte_exit(EXIT_FAILURE,
2971 			 "Failed to allocate xstats display memory\n");
2972 
2973 	ids_supp = port->xstats_info.ids_supp;
2974 	for (stat = stat_supp = 0; stat < xstats_display_num; stat++) {
2975 		xstat_name = xstats_display[stat].name;
2976 		rc = rte_eth_xstats_get_id_by_name(pi, xstat_name,
2977 						   ids_supp + stat_supp);
2978 		if (rc != 0) {
2979 			fprintf(stderr, "No xstat '%s' on port %u - skip it %u\n",
2980 				xstat_name, pi, stat);
2981 			continue;
2982 		}
2983 		stat_supp++;
2984 	}
2985 
2986 	port->xstats_info.ids_supp_sz = stat_supp;
2987 }
2988 
2989 /** Fill helper structures for all ports to show extended statistics. */
2990 static void
2991 fill_xstats_display_info(void)
2992 {
2993 	portid_t pi;
2994 
2995 	if (xstats_display_num == 0)
2996 		return;
2997 
2998 	RTE_ETH_FOREACH_DEV(pi)
2999 		fill_xstats_display_info_for_port(pi);
3000 }
3001 
3002 /*
3003  * Some capabilities (like, rx_offload_capa and tx_offload_capa) of bonding
3004  * device in dev_info is zero when no member is added. And its capability
3005  * will be updated when add a new member device. So adding a member device need
3006  * to update the port configurations of bonding device.
3007  */
3008 static void
3009 update_bonding_port_dev_conf(portid_t bond_pid)
3010 {
3011 #ifdef RTE_NET_BOND
3012 	struct rte_port *port = &ports[bond_pid];
3013 	uint16_t i;
3014 	int ret;
3015 
3016 	ret = eth_dev_info_get_print_err(bond_pid, &port->dev_info);
3017 	if (ret != 0) {
3018 		fprintf(stderr, "Failed to get dev info for port = %u\n",
3019 			bond_pid);
3020 		return;
3021 	}
3022 
3023 	if (port->dev_info.tx_offload_capa & RTE_ETH_TX_OFFLOAD_MBUF_FAST_FREE)
3024 		port->dev_conf.txmode.offloads |=
3025 				RTE_ETH_TX_OFFLOAD_MBUF_FAST_FREE;
3026 	/* Apply Tx offloads configuration */
3027 	for (i = 0; i < port->dev_info.max_tx_queues; i++)
3028 		port->txq[i].conf.offloads = port->dev_conf.txmode.offloads;
3029 
3030 	port->dev_conf.rx_adv_conf.rss_conf.rss_hf &=
3031 				port->dev_info.flow_type_rss_offloads;
3032 #else
3033 	RTE_SET_USED(bond_pid);
3034 #endif
3035 }
3036 
3037 int
3038 start_port(portid_t pid)
3039 {
3040 	int diag;
3041 	portid_t pi;
3042 	portid_t p_pi = RTE_MAX_ETHPORTS;
3043 	portid_t pl[RTE_MAX_ETHPORTS];
3044 	portid_t peer_pl[RTE_MAX_ETHPORTS];
3045 	uint16_t cnt_pi = 0;
3046 	uint16_t cfg_pi = 0;
3047 	int peer_pi;
3048 	queueid_t qi;
3049 	struct rte_port *port;
3050 	struct rte_eth_hairpin_cap cap;
3051 	bool at_least_one_port_exist = false;
3052 	bool all_ports_already_started = true;
3053 	bool at_least_one_port_successfully_started = false;
3054 
3055 	if (port_id_is_invalid(pid, ENABLED_WARN))
3056 		return 0;
3057 
3058 	RTE_ETH_FOREACH_DEV(pi) {
3059 		if (pid != pi && pid != (portid_t)RTE_PORT_ALL)
3060 			continue;
3061 
3062 		if (port_is_bonding_member(pi)) {
3063 			fprintf(stderr,
3064 				"Please remove port %d from bonding device.\n",
3065 				pi);
3066 			continue;
3067 		}
3068 
3069 		at_least_one_port_exist = true;
3070 
3071 		port = &ports[pi];
3072 		if (port->port_status == RTE_PORT_STOPPED) {
3073 			port->port_status = RTE_PORT_HANDLING;
3074 			all_ports_already_started = false;
3075 		} else {
3076 			fprintf(stderr, "Port %d is now not stopped\n", pi);
3077 			continue;
3078 		}
3079 
3080 		if (port->need_reconfig > 0) {
3081 			struct rte_eth_conf dev_conf;
3082 			int k;
3083 
3084 			port->need_reconfig = 0;
3085 
3086 			if (flow_isolate_all) {
3087 				int ret = port_flow_isolate(pi, 1);
3088 				if (ret) {
3089 					fprintf(stderr,
3090 						"Failed to apply isolated mode on port %d\n",
3091 						pi);
3092 					return -1;
3093 				}
3094 			}
3095 			configure_rxtx_dump_callbacks(0);
3096 			printf("Configuring Port %d (socket %u)\n", pi,
3097 					port->socket_id);
3098 			if (nb_hairpinq > 0 &&
3099 			    rte_eth_dev_hairpin_capability_get(pi, &cap)) {
3100 				fprintf(stderr,
3101 					"Port %d doesn't support hairpin queues\n",
3102 					pi);
3103 				return -1;
3104 			}
3105 
3106 			if (port->bond_flag == 1 && port->update_conf == 1) {
3107 				update_bonding_port_dev_conf(pi);
3108 				port->update_conf = 0;
3109 			}
3110 
3111 			/* configure port */
3112 			diag = eth_dev_configure_mp(pi, nb_rxq + nb_hairpinq,
3113 						     nb_txq + nb_hairpinq,
3114 						     &(port->dev_conf));
3115 			if (diag != 0) {
3116 				if (port->port_status == RTE_PORT_HANDLING)
3117 					port->port_status = RTE_PORT_STOPPED;
3118 				else
3119 					fprintf(stderr,
3120 						"Port %d can not be set back to stopped\n",
3121 						pi);
3122 				fprintf(stderr, "Fail to configure port %d\n",
3123 					pi);
3124 				/* try to reconfigure port next time */
3125 				port->need_reconfig = 1;
3126 				return -1;
3127 			}
3128 			/* get device configuration*/
3129 			if (0 !=
3130 				eth_dev_conf_get_print_err(pi, &dev_conf)) {
3131 				fprintf(stderr,
3132 					"port %d can not get device configuration\n",
3133 					pi);
3134 				return -1;
3135 			}
3136 			/* Apply Rx offloads configuration */
3137 			if (dev_conf.rxmode.offloads !=
3138 			    port->dev_conf.rxmode.offloads) {
3139 				port->dev_conf.rxmode.offloads |=
3140 					dev_conf.rxmode.offloads;
3141 				for (k = 0;
3142 				     k < port->dev_info.max_rx_queues;
3143 				     k++)
3144 					port->rxq[k].conf.offloads |=
3145 						dev_conf.rxmode.offloads;
3146 			}
3147 			/* Apply Tx offloads configuration */
3148 			if (dev_conf.txmode.offloads !=
3149 			    port->dev_conf.txmode.offloads) {
3150 				port->dev_conf.txmode.offloads |=
3151 					dev_conf.txmode.offloads;
3152 				for (k = 0;
3153 				     k < port->dev_info.max_tx_queues;
3154 				     k++)
3155 					port->txq[k].conf.offloads |=
3156 						dev_conf.txmode.offloads;
3157 			}
3158 		}
3159 		if (port->need_reconfig_queues > 0 && is_proc_primary()) {
3160 			port->need_reconfig_queues = 0;
3161 			/* setup tx queues */
3162 			for (qi = 0; qi < nb_txq; qi++) {
3163 				struct rte_eth_txconf *conf =
3164 							&port->txq[qi].conf;
3165 
3166 				if ((numa_support) &&
3167 					(txring_numa[pi] != NUMA_NO_CONFIG))
3168 					diag = rte_eth_tx_queue_setup(pi, qi,
3169 						port->nb_tx_desc[qi],
3170 						txring_numa[pi],
3171 						&(port->txq[qi].conf));
3172 				else
3173 					diag = rte_eth_tx_queue_setup(pi, qi,
3174 						port->nb_tx_desc[qi],
3175 						port->socket_id,
3176 						&(port->txq[qi].conf));
3177 
3178 				if (diag == 0) {
3179 					port->txq[qi].state =
3180 						conf->tx_deferred_start ?
3181 						RTE_ETH_QUEUE_STATE_STOPPED :
3182 						RTE_ETH_QUEUE_STATE_STARTED;
3183 					continue;
3184 				}
3185 
3186 				/* Fail to setup tx queue, return */
3187 				if (port->port_status == RTE_PORT_HANDLING)
3188 					port->port_status = RTE_PORT_STOPPED;
3189 				else
3190 					fprintf(stderr,
3191 						"Port %d can not be set back to stopped\n",
3192 						pi);
3193 				fprintf(stderr,
3194 					"Fail to configure port %d tx queues\n",
3195 					pi);
3196 				/* try to reconfigure queues next time */
3197 				port->need_reconfig_queues = 1;
3198 				return -1;
3199 			}
3200 			for (qi = 0; qi < nb_rxq; qi++) {
3201 				/* setup rx queues */
3202 				if ((numa_support) &&
3203 					(rxring_numa[pi] != NUMA_NO_CONFIG)) {
3204 					struct rte_mempool * mp =
3205 						mbuf_pool_find
3206 							(rxring_numa[pi], 0);
3207 					if (mp == NULL) {
3208 						fprintf(stderr,
3209 							"Failed to setup RX queue: No mempool allocation on the socket %d\n",
3210 							rxring_numa[pi]);
3211 						return -1;
3212 					}
3213 
3214 					diag = rx_queue_setup(pi, qi,
3215 					     port->nb_rx_desc[qi],
3216 					     rxring_numa[pi],
3217 					     &(port->rxq[qi].conf),
3218 					     mp);
3219 				} else {
3220 					struct rte_mempool *mp =
3221 						mbuf_pool_find
3222 							(port->socket_id, 0);
3223 					if (mp == NULL) {
3224 						fprintf(stderr,
3225 							"Failed to setup RX queue: No mempool allocation on the socket %d\n",
3226 							port->socket_id);
3227 						return -1;
3228 					}
3229 					diag = rx_queue_setup(pi, qi,
3230 					     port->nb_rx_desc[qi],
3231 					     port->socket_id,
3232 					     &(port->rxq[qi].conf),
3233 					     mp);
3234 				}
3235 				if (diag == 0)
3236 					continue;
3237 
3238 				/* Fail to setup rx queue, return */
3239 				if (port->port_status == RTE_PORT_HANDLING)
3240 					port->port_status = RTE_PORT_STOPPED;
3241 				else
3242 					fprintf(stderr,
3243 						"Port %d can not be set back to stopped\n",
3244 						pi);
3245 				fprintf(stderr,
3246 					"Fail to configure port %d rx queues\n",
3247 					pi);
3248 				/* try to reconfigure queues next time */
3249 				port->need_reconfig_queues = 1;
3250 				return -1;
3251 			}
3252 			/* setup hairpin queues */
3253 			if (setup_hairpin_queues(pi, p_pi, cnt_pi) != 0)
3254 				return -1;
3255 		}
3256 		configure_rxtx_dump_callbacks(verbose_level);
3257 		if (clear_ptypes) {
3258 			diag = rte_eth_dev_set_ptypes(pi, RTE_PTYPE_UNKNOWN,
3259 					NULL, 0);
3260 			if (diag < 0)
3261 				fprintf(stderr,
3262 					"Port %d: Failed to disable Ptype parsing\n",
3263 					pi);
3264 		}
3265 
3266 		p_pi = pi;
3267 		cnt_pi++;
3268 
3269 		/* start port */
3270 		diag = eth_dev_start_mp(pi);
3271 		if (diag < 0) {
3272 			fprintf(stderr, "Fail to start port %d: %s\n",
3273 				pi, rte_strerror(-diag));
3274 
3275 			/* Fail to setup rx queue, return */
3276 			if (port->port_status == RTE_PORT_HANDLING)
3277 				port->port_status = RTE_PORT_STOPPED;
3278 			else
3279 				fprintf(stderr,
3280 					"Port %d can not be set back to stopped\n",
3281 					pi);
3282 			continue;
3283 		}
3284 
3285 		if (port->port_status == RTE_PORT_HANDLING)
3286 			port->port_status = RTE_PORT_STARTED;
3287 		else
3288 			fprintf(stderr, "Port %d can not be set into started\n",
3289 				pi);
3290 
3291 		if (eth_macaddr_get_print_err(pi, &port->eth_addr) == 0)
3292 			printf("Port %d: " RTE_ETHER_ADDR_PRT_FMT "\n", pi,
3293 					RTE_ETHER_ADDR_BYTES(&port->eth_addr));
3294 
3295 		at_least_one_port_successfully_started = true;
3296 
3297 		pl[cfg_pi++] = pi;
3298 	}
3299 
3300 	update_queue_state(pi);
3301 
3302 	if (at_least_one_port_successfully_started && !no_link_check)
3303 		check_all_ports_link_status(RTE_PORT_ALL);
3304 	else if (at_least_one_port_exist & all_ports_already_started)
3305 		fprintf(stderr, "Please stop the ports first\n");
3306 
3307 	if (hairpin_mode & 0xf) {
3308 		uint16_t i;
3309 		int j;
3310 
3311 		/* bind all started hairpin ports */
3312 		for (i = 0; i < cfg_pi; i++) {
3313 			pi = pl[i];
3314 			/* bind current Tx to all peer Rx */
3315 			peer_pi = rte_eth_hairpin_get_peer_ports(pi, peer_pl,
3316 							RTE_MAX_ETHPORTS, 1);
3317 			if (peer_pi < 0)
3318 				return peer_pi;
3319 			for (j = 0; j < peer_pi; j++) {
3320 				if (!port_is_started(peer_pl[j]))
3321 					continue;
3322 				diag = rte_eth_hairpin_bind(pi, peer_pl[j]);
3323 				if (diag < 0) {
3324 					fprintf(stderr,
3325 						"Error during binding hairpin Tx port %u to %u: %s\n",
3326 						pi, peer_pl[j],
3327 						rte_strerror(-diag));
3328 					return -1;
3329 				}
3330 			}
3331 			/* bind all peer Tx to current Rx */
3332 			peer_pi = rte_eth_hairpin_get_peer_ports(pi, peer_pl,
3333 							RTE_MAX_ETHPORTS, 0);
3334 			if (peer_pi < 0)
3335 				return peer_pi;
3336 			for (j = 0; j < peer_pi; j++) {
3337 				if (!port_is_started(peer_pl[j]))
3338 					continue;
3339 				diag = rte_eth_hairpin_bind(peer_pl[j], pi);
3340 				if (diag < 0) {
3341 					fprintf(stderr,
3342 						"Error during binding hairpin Tx port %u to %u: %s\n",
3343 						peer_pl[j], pi,
3344 						rte_strerror(-diag));
3345 					return -1;
3346 				}
3347 			}
3348 		}
3349 	}
3350 
3351 	fill_xstats_display_info_for_port(pid);
3352 
3353 	printf("Done\n");
3354 	return 0;
3355 }
3356 
3357 void
3358 stop_port(portid_t pid)
3359 {
3360 	portid_t pi;
3361 	struct rte_port *port;
3362 	int need_check_link_status = 0;
3363 	portid_t peer_pl[RTE_MAX_ETHPORTS];
3364 	int peer_pi;
3365 	int ret;
3366 
3367 	if (port_id_is_invalid(pid, ENABLED_WARN))
3368 		return;
3369 
3370 	printf("Stopping ports...\n");
3371 
3372 	RTE_ETH_FOREACH_DEV(pi) {
3373 		if (pid != pi && pid != (portid_t)RTE_PORT_ALL)
3374 			continue;
3375 
3376 		if (port_is_forwarding(pi) != 0 && test_done == 0) {
3377 			fprintf(stderr,
3378 				"Please remove port %d from forwarding configuration.\n",
3379 				pi);
3380 			continue;
3381 		}
3382 
3383 		if (port_is_bonding_member(pi)) {
3384 			fprintf(stderr,
3385 				"Please remove port %d from bonding device.\n",
3386 				pi);
3387 			continue;
3388 		}
3389 
3390 		port = &ports[pi];
3391 		if (port->port_status == RTE_PORT_STARTED)
3392 			port->port_status = RTE_PORT_HANDLING;
3393 		else
3394 			continue;
3395 
3396 		if (hairpin_mode & 0xf) {
3397 			int j;
3398 
3399 			rte_eth_hairpin_unbind(pi, RTE_MAX_ETHPORTS);
3400 			/* unbind all peer Tx from current Rx */
3401 			peer_pi = rte_eth_hairpin_get_peer_ports(pi, peer_pl,
3402 							RTE_MAX_ETHPORTS, 0);
3403 			if (peer_pi < 0)
3404 				continue;
3405 			for (j = 0; j < peer_pi; j++) {
3406 				if (!port_is_started(peer_pl[j]))
3407 					continue;
3408 				rte_eth_hairpin_unbind(peer_pl[j], pi);
3409 			}
3410 		}
3411 
3412 		if (port->flow_list && !no_flow_flush)
3413 			port_flow_flush(pi);
3414 
3415 		ret = eth_dev_stop_mp(pi);
3416 		if (ret != 0) {
3417 			RTE_LOG(ERR, EAL, "rte_eth_dev_stop failed for port %u\n",
3418 				pi);
3419 			/* Allow to retry stopping the port. */
3420 			port->port_status = RTE_PORT_STARTED;
3421 			continue;
3422 		}
3423 
3424 		if (port->port_status == RTE_PORT_HANDLING)
3425 			port->port_status = RTE_PORT_STOPPED;
3426 		else
3427 			fprintf(stderr, "Port %d can not be set into stopped\n",
3428 				pi);
3429 		need_check_link_status = 1;
3430 	}
3431 	if (need_check_link_status && !no_link_check)
3432 		check_all_ports_link_status(RTE_PORT_ALL);
3433 
3434 	printf("Done\n");
3435 }
3436 
3437 static void
3438 remove_invalid_ports_in(portid_t *array, portid_t *total)
3439 {
3440 	portid_t i;
3441 	portid_t new_total = 0;
3442 
3443 	for (i = 0; i < *total; i++)
3444 		if (!port_id_is_invalid(array[i], DISABLED_WARN)) {
3445 			array[new_total] = array[i];
3446 			new_total++;
3447 		}
3448 	*total = new_total;
3449 }
3450 
3451 static void
3452 remove_invalid_ports(void)
3453 {
3454 	remove_invalid_ports_in(ports_ids, &nb_ports);
3455 	remove_invalid_ports_in(fwd_ports_ids, &nb_fwd_ports);
3456 	nb_cfg_ports = nb_fwd_ports;
3457 }
3458 
3459 static void
3460 flush_port_owned_resources(portid_t pi)
3461 {
3462 	mcast_addr_pool_destroy(pi);
3463 	port_flow_flush(pi);
3464 	port_flow_template_table_flush(pi);
3465 	port_flow_pattern_template_flush(pi);
3466 	port_flow_actions_template_flush(pi);
3467 	port_flex_item_flush(pi);
3468 	port_action_handle_flush(pi);
3469 }
3470 
3471 static void
3472 clear_bonding_member_device(portid_t *member_pids, uint16_t num_members)
3473 {
3474 	struct rte_port *port;
3475 	portid_t member_pid;
3476 	uint16_t i;
3477 
3478 	for (i = 0; i < num_members; i++) {
3479 		member_pid = member_pids[i];
3480 		if (port_is_started(member_pid) == 1) {
3481 			if (rte_eth_dev_stop(member_pid) != 0)
3482 				fprintf(stderr, "rte_eth_dev_stop failed for port %u\n",
3483 					member_pid);
3484 
3485 			port = &ports[member_pid];
3486 			port->port_status = RTE_PORT_STOPPED;
3487 		}
3488 
3489 		clear_port_member_flag(member_pid);
3490 
3491 		/* Close member device when testpmd quit or is killed. */
3492 		if (cl_quit == 1 || f_quit == 1)
3493 			rte_eth_dev_close(member_pid);
3494 	}
3495 }
3496 
3497 void
3498 close_port(portid_t pid)
3499 {
3500 	portid_t pi;
3501 	struct rte_port *port;
3502 	portid_t member_pids[RTE_MAX_ETHPORTS];
3503 	int num_members = 0;
3504 
3505 	if (port_id_is_invalid(pid, ENABLED_WARN))
3506 		return;
3507 
3508 	printf("Closing ports...\n");
3509 
3510 	RTE_ETH_FOREACH_DEV(pi) {
3511 		if (pid != pi && pid != (portid_t)RTE_PORT_ALL)
3512 			continue;
3513 
3514 		if (port_is_forwarding(pi) != 0 && test_done == 0) {
3515 			fprintf(stderr,
3516 				"Please remove port %d from forwarding configuration.\n",
3517 				pi);
3518 			continue;
3519 		}
3520 
3521 		if (port_is_bonding_member(pi)) {
3522 			fprintf(stderr,
3523 				"Please remove port %d from bonding device.\n",
3524 				pi);
3525 			continue;
3526 		}
3527 
3528 		port = &ports[pi];
3529 		if (port->port_status == RTE_PORT_CLOSED) {
3530 			fprintf(stderr, "Port %d is already closed\n", pi);
3531 			continue;
3532 		}
3533 
3534 		if (is_proc_primary()) {
3535 			flush_port_owned_resources(pi);
3536 #ifdef RTE_NET_BOND
3537 			if (port->bond_flag == 1)
3538 				num_members = rte_eth_bond_members_get(pi,
3539 						member_pids, RTE_MAX_ETHPORTS);
3540 #endif
3541 			rte_eth_dev_close(pi);
3542 			/*
3543 			 * If this port is bonding device, all members under the
3544 			 * device need to be removed or closed.
3545 			 */
3546 			if (port->bond_flag == 1 && num_members > 0)
3547 				clear_bonding_member_device(member_pids,
3548 							num_members);
3549 		}
3550 
3551 		free_xstats_display_info(pi);
3552 	}
3553 
3554 	remove_invalid_ports();
3555 	printf("Done\n");
3556 }
3557 
3558 void
3559 reset_port(portid_t pid)
3560 {
3561 	int diag;
3562 	portid_t pi;
3563 	struct rte_port *port;
3564 
3565 	if (port_id_is_invalid(pid, ENABLED_WARN))
3566 		return;
3567 
3568 	if ((pid == (portid_t)RTE_PORT_ALL && !all_ports_stopped()) ||
3569 		(pid != (portid_t)RTE_PORT_ALL && !port_is_stopped(pid))) {
3570 		fprintf(stderr,
3571 			"Can not reset port(s), please stop port(s) first.\n");
3572 		return;
3573 	}
3574 
3575 	printf("Resetting ports...\n");
3576 
3577 	RTE_ETH_FOREACH_DEV(pi) {
3578 		if (pid != pi && pid != (portid_t)RTE_PORT_ALL)
3579 			continue;
3580 
3581 		if (port_is_forwarding(pi) != 0 && test_done == 0) {
3582 			fprintf(stderr,
3583 				"Please remove port %d from forwarding configuration.\n",
3584 				pi);
3585 			continue;
3586 		}
3587 
3588 		if (port_is_bonding_member(pi)) {
3589 			fprintf(stderr,
3590 				"Please remove port %d from bonding device.\n",
3591 				pi);
3592 			continue;
3593 		}
3594 
3595 		if (is_proc_primary()) {
3596 			diag = rte_eth_dev_reset(pi);
3597 			if (diag == 0) {
3598 				port = &ports[pi];
3599 				port->need_reconfig = 1;
3600 				port->need_reconfig_queues = 1;
3601 			} else {
3602 				fprintf(stderr, "Failed to reset port %d. diag=%d\n",
3603 					pi, diag);
3604 			}
3605 		}
3606 	}
3607 
3608 	printf("Done\n");
3609 }
3610 
3611 void
3612 attach_port(char *identifier)
3613 {
3614 	portid_t pi;
3615 	struct rte_dev_iterator iterator;
3616 
3617 	printf("Attaching a new port...\n");
3618 
3619 	if (identifier == NULL) {
3620 		fprintf(stderr, "Invalid parameters are specified\n");
3621 		return;
3622 	}
3623 
3624 	if (rte_dev_probe(identifier) < 0) {
3625 		TESTPMD_LOG(ERR, "Failed to attach port %s\n", identifier);
3626 		return;
3627 	}
3628 
3629 	/* first attach mode: event */
3630 	if (setup_on_probe_event) {
3631 		/* new ports are detected on RTE_ETH_EVENT_NEW event */
3632 		for (pi = 0; pi < RTE_MAX_ETHPORTS; pi++)
3633 			if (ports[pi].port_status == RTE_PORT_HANDLING &&
3634 					ports[pi].need_setup != 0)
3635 				setup_attached_port(pi);
3636 		return;
3637 	}
3638 
3639 	/* second attach mode: iterator */
3640 	RTE_ETH_FOREACH_MATCHING_DEV(pi, identifier, &iterator) {
3641 		/* setup ports matching the devargs used for probing */
3642 		if (port_is_forwarding(pi))
3643 			continue; /* port was already attached before */
3644 		setup_attached_port(pi);
3645 	}
3646 }
3647 
3648 static void
3649 setup_attached_port(portid_t pi)
3650 {
3651 	unsigned int socket_id;
3652 	int ret;
3653 
3654 	socket_id = (unsigned)rte_eth_dev_socket_id(pi);
3655 	/* if socket_id is invalid, set to the first available socket. */
3656 	if (check_socket_id(socket_id) < 0)
3657 		socket_id = socket_ids[0];
3658 	reconfig(pi, socket_id);
3659 	ret = rte_eth_promiscuous_enable(pi);
3660 	if (ret != 0)
3661 		fprintf(stderr,
3662 			"Error during enabling promiscuous mode for port %u: %s - ignore\n",
3663 			pi, rte_strerror(-ret));
3664 
3665 	ports_ids[nb_ports++] = pi;
3666 	fwd_ports_ids[nb_fwd_ports++] = pi;
3667 	nb_cfg_ports = nb_fwd_ports;
3668 	ports[pi].need_setup = 0;
3669 	ports[pi].port_status = RTE_PORT_STOPPED;
3670 
3671 	printf("Port %d is attached. Now total ports is %d\n", pi, nb_ports);
3672 	printf("Done\n");
3673 }
3674 
3675 static void
3676 detach_device(struct rte_device *dev)
3677 {
3678 	portid_t sibling;
3679 
3680 	if (dev == NULL) {
3681 		fprintf(stderr, "Device already removed\n");
3682 		return;
3683 	}
3684 
3685 	printf("Removing a device...\n");
3686 
3687 	RTE_ETH_FOREACH_DEV_OF(sibling, dev) {
3688 		if (ports[sibling].port_status != RTE_PORT_CLOSED) {
3689 			if (ports[sibling].port_status != RTE_PORT_STOPPED) {
3690 				fprintf(stderr, "Port %u not stopped\n",
3691 					sibling);
3692 				return;
3693 			}
3694 			flush_port_owned_resources(sibling);
3695 		}
3696 	}
3697 
3698 	if (rte_dev_remove(dev) < 0) {
3699 		TESTPMD_LOG(ERR, "Failed to detach device %s\n", rte_dev_name(dev));
3700 		return;
3701 	}
3702 	remove_invalid_ports();
3703 
3704 	printf("Device is detached\n");
3705 	printf("Now total ports is %d\n", nb_ports);
3706 	printf("Done\n");
3707 	return;
3708 }
3709 
3710 void
3711 detach_port_device(portid_t port_id)
3712 {
3713 	int ret;
3714 	struct rte_eth_dev_info dev_info;
3715 
3716 	if (port_id_is_invalid(port_id, ENABLED_WARN))
3717 		return;
3718 
3719 	if (ports[port_id].port_status != RTE_PORT_CLOSED) {
3720 		if (ports[port_id].port_status != RTE_PORT_STOPPED) {
3721 			fprintf(stderr, "Port not stopped\n");
3722 			return;
3723 		}
3724 		fprintf(stderr, "Port was not closed\n");
3725 	}
3726 
3727 	ret = eth_dev_info_get_print_err(port_id, &dev_info);
3728 	if (ret != 0) {
3729 		TESTPMD_LOG(ERR,
3730 			"Failed to get device info for port %d, not detaching\n",
3731 			port_id);
3732 		return;
3733 	}
3734 	detach_device(dev_info.device);
3735 }
3736 
3737 void
3738 detach_devargs(char *identifier)
3739 {
3740 	struct rte_dev_iterator iterator;
3741 	struct rte_devargs da;
3742 	portid_t port_id;
3743 
3744 	printf("Removing a device...\n");
3745 
3746 	memset(&da, 0, sizeof(da));
3747 	if (rte_devargs_parsef(&da, "%s", identifier)) {
3748 		fprintf(stderr, "cannot parse identifier\n");
3749 		return;
3750 	}
3751 
3752 	RTE_ETH_FOREACH_MATCHING_DEV(port_id, identifier, &iterator) {
3753 		if (ports[port_id].port_status != RTE_PORT_CLOSED) {
3754 			if (ports[port_id].port_status != RTE_PORT_STOPPED) {
3755 				fprintf(stderr, "Port %u not stopped\n",
3756 					port_id);
3757 				rte_eth_iterator_cleanup(&iterator);
3758 				rte_devargs_reset(&da);
3759 				return;
3760 			}
3761 			flush_port_owned_resources(port_id);
3762 		}
3763 	}
3764 
3765 	if (rte_eal_hotplug_remove(rte_bus_name(da.bus), da.name) != 0) {
3766 		TESTPMD_LOG(ERR, "Failed to detach device %s(%s)\n",
3767 			    da.name, rte_bus_name(da.bus));
3768 		rte_devargs_reset(&da);
3769 		return;
3770 	}
3771 
3772 	remove_invalid_ports();
3773 
3774 	printf("Device %s is detached\n", identifier);
3775 	printf("Now total ports is %d\n", nb_ports);
3776 	printf("Done\n");
3777 	rte_devargs_reset(&da);
3778 }
3779 
3780 void
3781 pmd_test_exit(void)
3782 {
3783 	portid_t pt_id;
3784 	unsigned int i;
3785 	int ret;
3786 
3787 	if (test_done == 0)
3788 		stop_packet_forwarding();
3789 
3790 #ifndef RTE_EXEC_ENV_WINDOWS
3791 	for (i = 0 ; i < RTE_DIM(mempools) ; i++) {
3792 		if (mempools[i]) {
3793 			if (mp_alloc_type == MP_ALLOC_ANON)
3794 				rte_mempool_mem_iter(mempools[i], dma_unmap_cb,
3795 						     NULL);
3796 		}
3797 	}
3798 #endif
3799 	if (ports != NULL) {
3800 		no_link_check = 1;
3801 		RTE_ETH_FOREACH_DEV(pt_id) {
3802 			printf("\nStopping port %d...\n", pt_id);
3803 			fflush(stdout);
3804 			stop_port(pt_id);
3805 		}
3806 		RTE_ETH_FOREACH_DEV(pt_id) {
3807 			printf("\nShutting down port %d...\n", pt_id);
3808 			fflush(stdout);
3809 			close_port(pt_id);
3810 		}
3811 	}
3812 
3813 	if (hot_plug) {
3814 		ret = rte_dev_event_monitor_stop();
3815 		if (ret) {
3816 			RTE_LOG(ERR, EAL,
3817 				"fail to stop device event monitor.");
3818 			return;
3819 		}
3820 
3821 		ret = rte_dev_event_callback_unregister(NULL,
3822 			dev_event_callback, NULL);
3823 		if (ret < 0) {
3824 			RTE_LOG(ERR, EAL,
3825 				"fail to unregister device event callback.\n");
3826 			return;
3827 		}
3828 
3829 		ret = rte_dev_hotplug_handle_disable();
3830 		if (ret) {
3831 			RTE_LOG(ERR, EAL,
3832 				"fail to disable hotplug handling.\n");
3833 			return;
3834 		}
3835 	}
3836 	for (i = 0 ; i < RTE_DIM(mempools) ; i++) {
3837 		if (mempools[i])
3838 			mempool_free_mp(mempools[i]);
3839 	}
3840 	free(xstats_display);
3841 
3842 	printf("\nBye...\n");
3843 }
3844 
3845 typedef void (*cmd_func_t)(void);
3846 struct pmd_test_command {
3847 	const char *cmd_name;
3848 	cmd_func_t cmd_func;
3849 };
3850 
3851 /* Check the link status of all ports in up to 9s, and print them finally */
3852 static void
3853 check_all_ports_link_status(uint32_t port_mask)
3854 {
3855 #define CHECK_INTERVAL 100 /* 100ms */
3856 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
3857 	portid_t portid;
3858 	uint8_t count, all_ports_up, print_flag = 0;
3859 	struct rte_eth_link link;
3860 	int ret;
3861 	char link_status[RTE_ETH_LINK_MAX_STR_LEN];
3862 
3863 	printf("Checking link statuses...\n");
3864 	fflush(stdout);
3865 	for (count = 0; count <= MAX_CHECK_TIME; count++) {
3866 		all_ports_up = 1;
3867 		RTE_ETH_FOREACH_DEV(portid) {
3868 			if ((port_mask & (1 << portid)) == 0)
3869 				continue;
3870 			memset(&link, 0, sizeof(link));
3871 			ret = rte_eth_link_get_nowait(portid, &link);
3872 			if (ret < 0) {
3873 				all_ports_up = 0;
3874 				if (print_flag == 1)
3875 					fprintf(stderr,
3876 						"Port %u link get failed: %s\n",
3877 						portid, rte_strerror(-ret));
3878 				continue;
3879 			}
3880 			/* print link status if flag set */
3881 			if (print_flag == 1) {
3882 				rte_eth_link_to_str(link_status,
3883 					sizeof(link_status), &link);
3884 				printf("Port %d %s\n", portid, link_status);
3885 				continue;
3886 			}
3887 			/* clear all_ports_up flag if any link down */
3888 			if (link.link_status == RTE_ETH_LINK_DOWN) {
3889 				all_ports_up = 0;
3890 				break;
3891 			}
3892 		}
3893 		/* after finally printing all link status, get out */
3894 		if (print_flag == 1)
3895 			break;
3896 
3897 		if (all_ports_up == 0) {
3898 			fflush(stdout);
3899 			rte_delay_ms(CHECK_INTERVAL);
3900 		}
3901 
3902 		/* set the print_flag if all ports up or timeout */
3903 		if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
3904 			print_flag = 1;
3905 		}
3906 
3907 		if (lsc_interrupt)
3908 			break;
3909 	}
3910 }
3911 
3912 static void
3913 rmv_port_callback(void *arg)
3914 {
3915 	int need_to_start = 0;
3916 	int org_no_link_check = no_link_check;
3917 	portid_t port_id = (intptr_t)arg;
3918 	struct rte_eth_dev_info dev_info;
3919 	int ret;
3920 
3921 	RTE_ETH_VALID_PORTID_OR_RET(port_id);
3922 
3923 	if (!test_done && port_is_forwarding(port_id)) {
3924 		need_to_start = 1;
3925 		stop_packet_forwarding();
3926 	}
3927 	no_link_check = 1;
3928 	stop_port(port_id);
3929 	no_link_check = org_no_link_check;
3930 
3931 	ret = eth_dev_info_get_print_err(port_id, &dev_info);
3932 	if (ret != 0)
3933 		TESTPMD_LOG(ERR,
3934 			"Failed to get device info for port %d, not detaching\n",
3935 			port_id);
3936 	else {
3937 		struct rte_device *device = dev_info.device;
3938 		close_port(port_id);
3939 		detach_device(device); /* might be already removed or have more ports */
3940 	}
3941 	if (need_to_start)
3942 		start_packet_forwarding(0);
3943 }
3944 
3945 /* This function is used by the interrupt thread */
3946 static int
3947 eth_event_callback(portid_t port_id, enum rte_eth_event_type type, void *param,
3948 		  void *ret_param)
3949 {
3950 	RTE_SET_USED(param);
3951 	RTE_SET_USED(ret_param);
3952 
3953 	if (type >= RTE_ETH_EVENT_MAX) {
3954 		fprintf(stderr,
3955 			"\nPort %" PRIu16 ": %s called upon invalid event %d\n",
3956 			port_id, __func__, type);
3957 		fflush(stderr);
3958 	} else if (event_print_mask & (UINT32_C(1) << type)) {
3959 		printf("\nPort %" PRIu16 ": %s event\n", port_id,
3960 			eth_event_desc[type]);
3961 		fflush(stdout);
3962 	}
3963 
3964 	switch (type) {
3965 	case RTE_ETH_EVENT_NEW:
3966 		ports[port_id].need_setup = 1;
3967 		ports[port_id].port_status = RTE_PORT_HANDLING;
3968 		break;
3969 	case RTE_ETH_EVENT_INTR_RMV:
3970 		if (port_id_is_invalid(port_id, DISABLED_WARN))
3971 			break;
3972 		if (rte_eal_alarm_set(100000,
3973 				rmv_port_callback, (void *)(intptr_t)port_id))
3974 			fprintf(stderr,
3975 				"Could not set up deferred device removal\n");
3976 		break;
3977 	case RTE_ETH_EVENT_DESTROY:
3978 		ports[port_id].port_status = RTE_PORT_CLOSED;
3979 		printf("Port %u is closed\n", port_id);
3980 		break;
3981 	case RTE_ETH_EVENT_RX_AVAIL_THRESH: {
3982 		uint16_t rxq_id;
3983 		int ret;
3984 
3985 		/* avail_thresh query API rewinds rxq_id, no need to check max RxQ num */
3986 		for (rxq_id = 0; ; rxq_id++) {
3987 			ret = rte_eth_rx_avail_thresh_query(port_id, &rxq_id,
3988 							    NULL);
3989 			if (ret <= 0)
3990 				break;
3991 			printf("Received avail_thresh event, port: %u, rxq_id: %u\n",
3992 			       port_id, rxq_id);
3993 
3994 #ifdef RTE_NET_MLX5
3995 			mlx5_test_avail_thresh_event_handler(port_id, rxq_id);
3996 #endif
3997 		}
3998 		break;
3999 	}
4000 	default:
4001 		break;
4002 	}
4003 	return 0;
4004 }
4005 
4006 static int
4007 register_eth_event_callback(void)
4008 {
4009 	int ret;
4010 	enum rte_eth_event_type event;
4011 
4012 	for (event = RTE_ETH_EVENT_UNKNOWN;
4013 			event < RTE_ETH_EVENT_MAX; event++) {
4014 		ret = rte_eth_dev_callback_register(RTE_ETH_ALL,
4015 				event,
4016 				eth_event_callback,
4017 				NULL);
4018 		if (ret != 0) {
4019 			TESTPMD_LOG(ERR, "Failed to register callback for "
4020 					"%s event\n", eth_event_desc[event]);
4021 			return -1;
4022 		}
4023 	}
4024 
4025 	return 0;
4026 }
4027 
4028 static int
4029 unregister_eth_event_callback(void)
4030 {
4031 	int ret;
4032 	enum rte_eth_event_type event;
4033 
4034 	for (event = RTE_ETH_EVENT_UNKNOWN;
4035 			event < RTE_ETH_EVENT_MAX; event++) {
4036 		ret = rte_eth_dev_callback_unregister(RTE_ETH_ALL,
4037 				event,
4038 				eth_event_callback,
4039 				NULL);
4040 		if (ret != 0) {
4041 			TESTPMD_LOG(ERR, "Failed to unregister callback for "
4042 					"%s event\n", eth_event_desc[event]);
4043 			return -1;
4044 		}
4045 	}
4046 
4047 	return 0;
4048 }
4049 
4050 /* This function is used by the interrupt thread */
4051 static void
4052 dev_event_callback(const char *device_name, enum rte_dev_event_type type,
4053 			     __rte_unused void *arg)
4054 {
4055 	uint16_t port_id;
4056 	int ret;
4057 
4058 	if (type >= RTE_DEV_EVENT_MAX) {
4059 		fprintf(stderr, "%s called upon invalid event %d\n",
4060 			__func__, type);
4061 		fflush(stderr);
4062 	}
4063 
4064 	switch (type) {
4065 	case RTE_DEV_EVENT_REMOVE:
4066 		RTE_LOG(DEBUG, EAL, "The device: %s has been removed!\n",
4067 			device_name);
4068 		ret = rte_eth_dev_get_port_by_name(device_name, &port_id);
4069 		if (ret) {
4070 			RTE_LOG(ERR, EAL, "can not get port by device %s!\n",
4071 				device_name);
4072 			return;
4073 		}
4074 		/*
4075 		 * Because the user's callback is invoked in eal interrupt
4076 		 * callback, the interrupt callback need to be finished before
4077 		 * it can be unregistered when detaching device. So finish
4078 		 * callback soon and use a deferred removal to detach device
4079 		 * is need. It is a workaround, once the device detaching be
4080 		 * moved into the eal in the future, the deferred removal could
4081 		 * be deleted.
4082 		 */
4083 		if (rte_eal_alarm_set(100000,
4084 				rmv_port_callback, (void *)(intptr_t)port_id))
4085 			RTE_LOG(ERR, EAL,
4086 				"Could not set up deferred device removal\n");
4087 		break;
4088 	case RTE_DEV_EVENT_ADD:
4089 		RTE_LOG(ERR, EAL, "The device: %s has been added!\n",
4090 			device_name);
4091 		/* TODO: After finish kernel driver binding,
4092 		 * begin to attach port.
4093 		 */
4094 		break;
4095 	default:
4096 		break;
4097 	}
4098 }
4099 
4100 static void
4101 rxtx_port_config(portid_t pid)
4102 {
4103 	uint16_t qid;
4104 	uint64_t offloads;
4105 	struct rte_port *port = &ports[pid];
4106 
4107 	for (qid = 0; qid < nb_rxq; qid++) {
4108 		offloads = port->rxq[qid].conf.offloads;
4109 		port->rxq[qid].conf = port->dev_info.default_rxconf;
4110 
4111 		if (rxq_share > 0 &&
4112 		    (port->dev_info.dev_capa & RTE_ETH_DEV_CAPA_RXQ_SHARE)) {
4113 			/* Non-zero share group to enable RxQ share. */
4114 			port->rxq[qid].conf.share_group = pid / rxq_share + 1;
4115 			port->rxq[qid].conf.share_qid = qid; /* Equal mapping. */
4116 		}
4117 
4118 		if (offloads != 0)
4119 			port->rxq[qid].conf.offloads = offloads;
4120 
4121 		/* Check if any Rx parameters have been passed */
4122 		if (rx_pthresh != RTE_PMD_PARAM_UNSET)
4123 			port->rxq[qid].conf.rx_thresh.pthresh = rx_pthresh;
4124 
4125 		if (rx_hthresh != RTE_PMD_PARAM_UNSET)
4126 			port->rxq[qid].conf.rx_thresh.hthresh = rx_hthresh;
4127 
4128 		if (rx_wthresh != RTE_PMD_PARAM_UNSET)
4129 			port->rxq[qid].conf.rx_thresh.wthresh = rx_wthresh;
4130 
4131 		if (rx_free_thresh != RTE_PMD_PARAM_UNSET)
4132 			port->rxq[qid].conf.rx_free_thresh = rx_free_thresh;
4133 
4134 		if (rx_drop_en != RTE_PMD_PARAM_UNSET)
4135 			port->rxq[qid].conf.rx_drop_en = rx_drop_en;
4136 
4137 		port->nb_rx_desc[qid] = nb_rxd;
4138 	}
4139 
4140 	for (qid = 0; qid < nb_txq; qid++) {
4141 		offloads = port->txq[qid].conf.offloads;
4142 		port->txq[qid].conf = port->dev_info.default_txconf;
4143 		if (offloads != 0)
4144 			port->txq[qid].conf.offloads = offloads;
4145 
4146 		/* Check if any Tx parameters have been passed */
4147 		if (tx_pthresh != RTE_PMD_PARAM_UNSET)
4148 			port->txq[qid].conf.tx_thresh.pthresh = tx_pthresh;
4149 
4150 		if (tx_hthresh != RTE_PMD_PARAM_UNSET)
4151 			port->txq[qid].conf.tx_thresh.hthresh = tx_hthresh;
4152 
4153 		if (tx_wthresh != RTE_PMD_PARAM_UNSET)
4154 			port->txq[qid].conf.tx_thresh.wthresh = tx_wthresh;
4155 
4156 		if (tx_rs_thresh != RTE_PMD_PARAM_UNSET)
4157 			port->txq[qid].conf.tx_rs_thresh = tx_rs_thresh;
4158 
4159 		if (tx_free_thresh != RTE_PMD_PARAM_UNSET)
4160 			port->txq[qid].conf.tx_free_thresh = tx_free_thresh;
4161 
4162 		port->nb_tx_desc[qid] = nb_txd;
4163 	}
4164 }
4165 
4166 /*
4167  * Helper function to set MTU from frame size
4168  *
4169  * port->dev_info should be set before calling this function.
4170  *
4171  * return 0 on success, negative on error
4172  */
4173 int
4174 update_mtu_from_frame_size(portid_t portid, uint32_t max_rx_pktlen)
4175 {
4176 	struct rte_port *port = &ports[portid];
4177 	uint32_t eth_overhead;
4178 	uint16_t mtu, new_mtu;
4179 
4180 	eth_overhead = get_eth_overhead(&port->dev_info);
4181 
4182 	if (rte_eth_dev_get_mtu(portid, &mtu) != 0) {
4183 		printf("Failed to get MTU for port %u\n", portid);
4184 		return -1;
4185 	}
4186 
4187 	new_mtu = max_rx_pktlen - eth_overhead;
4188 
4189 	if (mtu == new_mtu)
4190 		return 0;
4191 
4192 	if (eth_dev_set_mtu_mp(portid, new_mtu) != 0) {
4193 		fprintf(stderr,
4194 			"Failed to set MTU to %u for port %u\n",
4195 			new_mtu, portid);
4196 		return -1;
4197 	}
4198 
4199 	port->dev_conf.rxmode.mtu = new_mtu;
4200 
4201 	return 0;
4202 }
4203 
4204 void
4205 init_port_config(void)
4206 {
4207 	portid_t pid;
4208 	struct rte_port *port;
4209 	int ret, i;
4210 
4211 	RTE_ETH_FOREACH_DEV(pid) {
4212 		port = &ports[pid];
4213 
4214 		ret = eth_dev_info_get_print_err(pid, &port->dev_info);
4215 		if (ret != 0)
4216 			return;
4217 
4218 		if (nb_rxq > 1) {
4219 			port->dev_conf.rx_adv_conf.rss_conf.rss_key = NULL;
4220 			port->dev_conf.rx_adv_conf.rss_conf.rss_hf =
4221 				rss_hf & port->dev_info.flow_type_rss_offloads;
4222 		} else {
4223 			port->dev_conf.rx_adv_conf.rss_conf.rss_key = NULL;
4224 			port->dev_conf.rx_adv_conf.rss_conf.rss_hf = 0;
4225 		}
4226 
4227 		if (port->dcb_flag == 0) {
4228 			if (port->dev_conf.rx_adv_conf.rss_conf.rss_hf != 0) {
4229 				port->dev_conf.rxmode.mq_mode =
4230 					(enum rte_eth_rx_mq_mode)
4231 						(rx_mq_mode & RTE_ETH_MQ_RX_RSS);
4232 			} else {
4233 				port->dev_conf.rxmode.mq_mode = RTE_ETH_MQ_RX_NONE;
4234 				port->dev_conf.rxmode.offloads &=
4235 						~RTE_ETH_RX_OFFLOAD_RSS_HASH;
4236 
4237 				for (i = 0;
4238 				     i < port->dev_info.nb_rx_queues;
4239 				     i++)
4240 					port->rxq[i].conf.offloads &=
4241 						~RTE_ETH_RX_OFFLOAD_RSS_HASH;
4242 			}
4243 		}
4244 
4245 		rxtx_port_config(pid);
4246 
4247 		ret = eth_macaddr_get_print_err(pid, &port->eth_addr);
4248 		if (ret != 0)
4249 			return;
4250 
4251 		if (lsc_interrupt && (*port->dev_info.dev_flags & RTE_ETH_DEV_INTR_LSC))
4252 			port->dev_conf.intr_conf.lsc = 1;
4253 		if (rmv_interrupt && (*port->dev_info.dev_flags & RTE_ETH_DEV_INTR_RMV))
4254 			port->dev_conf.intr_conf.rmv = 1;
4255 	}
4256 }
4257 
4258 void set_port_member_flag(portid_t member_pid)
4259 {
4260 	struct rte_port *port;
4261 
4262 	port = &ports[member_pid];
4263 	port->member_flag = 1;
4264 }
4265 
4266 void clear_port_member_flag(portid_t member_pid)
4267 {
4268 	struct rte_port *port;
4269 
4270 	port = &ports[member_pid];
4271 	port->member_flag = 0;
4272 }
4273 
4274 uint8_t port_is_bonding_member(portid_t member_pid)
4275 {
4276 	struct rte_port *port;
4277 	struct rte_eth_dev_info dev_info;
4278 	int ret;
4279 
4280 	port = &ports[member_pid];
4281 	ret = eth_dev_info_get_print_err(member_pid, &dev_info);
4282 	if (ret != 0) {
4283 		TESTPMD_LOG(ERR,
4284 			"Failed to get device info for port id %d,"
4285 			"cannot determine if the port is a bonding member",
4286 			member_pid);
4287 		return 0;
4288 	}
4289 
4290 	if ((*dev_info.dev_flags & RTE_ETH_DEV_BONDING_MEMBER) || (port->member_flag == 1))
4291 		return 1;
4292 	return 0;
4293 }
4294 
4295 const uint16_t vlan_tags[] = {
4296 		0,  1,  2,  3,  4,  5,  6,  7,
4297 		8,  9, 10, 11,  12, 13, 14, 15,
4298 		16, 17, 18, 19, 20, 21, 22, 23,
4299 		24, 25, 26, 27, 28, 29, 30, 31
4300 };
4301 
4302 static  int
4303 get_eth_dcb_conf(portid_t pid, struct rte_eth_conf *eth_conf,
4304 		 enum dcb_mode_enable dcb_mode,
4305 		 enum rte_eth_nb_tcs num_tcs,
4306 		 uint8_t pfc_en)
4307 {
4308 	uint8_t i;
4309 	int32_t rc;
4310 	struct rte_eth_rss_conf rss_conf;
4311 
4312 	/*
4313 	 * Builds up the correct configuration for dcb+vt based on the vlan tags array
4314 	 * given above, and the number of traffic classes available for use.
4315 	 */
4316 	if (dcb_mode == DCB_VT_ENABLED) {
4317 		struct rte_eth_vmdq_dcb_conf *vmdq_rx_conf =
4318 				&eth_conf->rx_adv_conf.vmdq_dcb_conf;
4319 		struct rte_eth_vmdq_dcb_tx_conf *vmdq_tx_conf =
4320 				&eth_conf->tx_adv_conf.vmdq_dcb_tx_conf;
4321 
4322 		/* VMDQ+DCB RX and TX configurations */
4323 		vmdq_rx_conf->enable_default_pool = 0;
4324 		vmdq_rx_conf->default_pool = 0;
4325 		vmdq_rx_conf->nb_queue_pools =
4326 			(num_tcs ==  RTE_ETH_4_TCS ? RTE_ETH_32_POOLS : RTE_ETH_16_POOLS);
4327 		vmdq_tx_conf->nb_queue_pools =
4328 			(num_tcs ==  RTE_ETH_4_TCS ? RTE_ETH_32_POOLS : RTE_ETH_16_POOLS);
4329 
4330 		vmdq_rx_conf->nb_pool_maps = vmdq_rx_conf->nb_queue_pools;
4331 		for (i = 0; i < vmdq_rx_conf->nb_pool_maps; i++) {
4332 			vmdq_rx_conf->pool_map[i].vlan_id = vlan_tags[i];
4333 			vmdq_rx_conf->pool_map[i].pools =
4334 				1 << (i % vmdq_rx_conf->nb_queue_pools);
4335 		}
4336 		for (i = 0; i < RTE_ETH_DCB_NUM_USER_PRIORITIES; i++) {
4337 			vmdq_rx_conf->dcb_tc[i] = i % num_tcs;
4338 			vmdq_tx_conf->dcb_tc[i] = i % num_tcs;
4339 		}
4340 
4341 		/* set DCB mode of RX and TX of multiple queues */
4342 		eth_conf->rxmode.mq_mode =
4343 				(enum rte_eth_rx_mq_mode)
4344 					(rx_mq_mode & RTE_ETH_MQ_RX_VMDQ_DCB);
4345 		eth_conf->txmode.mq_mode = RTE_ETH_MQ_TX_VMDQ_DCB;
4346 	} else {
4347 		struct rte_eth_dcb_rx_conf *rx_conf =
4348 				&eth_conf->rx_adv_conf.dcb_rx_conf;
4349 		struct rte_eth_dcb_tx_conf *tx_conf =
4350 				&eth_conf->tx_adv_conf.dcb_tx_conf;
4351 
4352 		memset(&rss_conf, 0, sizeof(struct rte_eth_rss_conf));
4353 
4354 		rc = rte_eth_dev_rss_hash_conf_get(pid, &rss_conf);
4355 		if (rc != 0)
4356 			return rc;
4357 
4358 		rx_conf->nb_tcs = num_tcs;
4359 		tx_conf->nb_tcs = num_tcs;
4360 
4361 		for (i = 0; i < RTE_ETH_DCB_NUM_USER_PRIORITIES; i++) {
4362 			rx_conf->dcb_tc[i] = i % num_tcs;
4363 			tx_conf->dcb_tc[i] = i % num_tcs;
4364 		}
4365 
4366 		eth_conf->rxmode.mq_mode =
4367 				(enum rte_eth_rx_mq_mode)
4368 					(rx_mq_mode & RTE_ETH_MQ_RX_DCB_RSS);
4369 		eth_conf->rx_adv_conf.rss_conf = rss_conf;
4370 		eth_conf->txmode.mq_mode = RTE_ETH_MQ_TX_DCB;
4371 	}
4372 
4373 	if (pfc_en)
4374 		eth_conf->dcb_capability_en =
4375 				RTE_ETH_DCB_PG_SUPPORT | RTE_ETH_DCB_PFC_SUPPORT;
4376 	else
4377 		eth_conf->dcb_capability_en = RTE_ETH_DCB_PG_SUPPORT;
4378 
4379 	return 0;
4380 }
4381 
4382 int
4383 init_port_dcb_config(portid_t pid,
4384 		     enum dcb_mode_enable dcb_mode,
4385 		     enum rte_eth_nb_tcs num_tcs,
4386 		     uint8_t pfc_en)
4387 {
4388 	struct rte_eth_conf port_conf;
4389 	struct rte_port *rte_port;
4390 	int retval;
4391 	uint16_t i;
4392 
4393 	if (num_procs > 1) {
4394 		printf("The multi-process feature doesn't support dcb.\n");
4395 		return -ENOTSUP;
4396 	}
4397 	rte_port = &ports[pid];
4398 
4399 	/* retain the original device configuration. */
4400 	memcpy(&port_conf, &rte_port->dev_conf, sizeof(struct rte_eth_conf));
4401 
4402 	/*set configuration of DCB in vt mode and DCB in non-vt mode*/
4403 	retval = get_eth_dcb_conf(pid, &port_conf, dcb_mode, num_tcs, pfc_en);
4404 	if (retval < 0)
4405 		return retval;
4406 	port_conf.rxmode.offloads |= RTE_ETH_RX_OFFLOAD_VLAN_FILTER;
4407 	/* remove RSS HASH offload for DCB in vt mode */
4408 	if (port_conf.rxmode.mq_mode == RTE_ETH_MQ_RX_VMDQ_DCB) {
4409 		port_conf.rxmode.offloads &= ~RTE_ETH_RX_OFFLOAD_RSS_HASH;
4410 		for (i = 0; i < nb_rxq; i++)
4411 			rte_port->rxq[i].conf.offloads &=
4412 				~RTE_ETH_RX_OFFLOAD_RSS_HASH;
4413 	}
4414 
4415 	/* re-configure the device . */
4416 	retval = rte_eth_dev_configure(pid, nb_rxq, nb_rxq, &port_conf);
4417 	if (retval < 0)
4418 		return retval;
4419 
4420 	retval = eth_dev_info_get_print_err(pid, &rte_port->dev_info);
4421 	if (retval != 0)
4422 		return retval;
4423 
4424 	/* If dev_info.vmdq_pool_base is greater than 0,
4425 	 * the queue id of vmdq pools is started after pf queues.
4426 	 */
4427 	if (dcb_mode == DCB_VT_ENABLED &&
4428 	    rte_port->dev_info.vmdq_pool_base > 0) {
4429 		fprintf(stderr,
4430 			"VMDQ_DCB multi-queue mode is nonsensical for port %d.\n",
4431 			pid);
4432 		return -1;
4433 	}
4434 
4435 	/* Assume the ports in testpmd have the same dcb capability
4436 	 * and has the same number of rxq and txq in dcb mode
4437 	 */
4438 	if (dcb_mode == DCB_VT_ENABLED) {
4439 		if (rte_port->dev_info.max_vfs > 0) {
4440 			nb_rxq = rte_port->dev_info.nb_rx_queues;
4441 			nb_txq = rte_port->dev_info.nb_tx_queues;
4442 		} else {
4443 			nb_rxq = rte_port->dev_info.max_rx_queues;
4444 			nb_txq = rte_port->dev_info.max_tx_queues;
4445 		}
4446 	} else {
4447 		/*if vt is disabled, use all pf queues */
4448 		if (rte_port->dev_info.vmdq_pool_base == 0) {
4449 			nb_rxq = rte_port->dev_info.max_rx_queues;
4450 			nb_txq = rte_port->dev_info.max_tx_queues;
4451 		} else {
4452 			nb_rxq = (queueid_t)num_tcs;
4453 			nb_txq = (queueid_t)num_tcs;
4454 
4455 		}
4456 	}
4457 	rx_free_thresh = 64;
4458 
4459 	memcpy(&rte_port->dev_conf, &port_conf, sizeof(struct rte_eth_conf));
4460 
4461 	rxtx_port_config(pid);
4462 	/* VLAN filter */
4463 	rte_port->dev_conf.rxmode.offloads |= RTE_ETH_RX_OFFLOAD_VLAN_FILTER;
4464 	for (i = 0; i < RTE_DIM(vlan_tags); i++)
4465 		rx_vft_set(pid, vlan_tags[i], 1);
4466 
4467 	retval = eth_macaddr_get_print_err(pid, &rte_port->eth_addr);
4468 	if (retval != 0)
4469 		return retval;
4470 
4471 	rte_port->dcb_flag = 1;
4472 
4473 	/* Enter DCB configuration status */
4474 	dcb_config = 1;
4475 
4476 	return 0;
4477 }
4478 
4479 static void
4480 init_port(void)
4481 {
4482 	int i;
4483 
4484 	/* Configuration of Ethernet ports. */
4485 	ports = rte_zmalloc("testpmd: ports",
4486 			    sizeof(struct rte_port) * RTE_MAX_ETHPORTS,
4487 			    RTE_CACHE_LINE_SIZE);
4488 	if (ports == NULL) {
4489 		rte_exit(EXIT_FAILURE,
4490 				"rte_zmalloc(%d struct rte_port) failed\n",
4491 				RTE_MAX_ETHPORTS);
4492 	}
4493 	for (i = 0; i < RTE_MAX_ETHPORTS; i++) {
4494 		ports[i].fwd_mac_swap = 1;
4495 		ports[i].xstats_info.allocated = false;
4496 		LIST_INIT(&ports[i].flow_tunnel_list);
4497 	}
4498 	/* Initialize ports NUMA structures */
4499 	memset(port_numa, NUMA_NO_CONFIG, RTE_MAX_ETHPORTS);
4500 	memset(rxring_numa, NUMA_NO_CONFIG, RTE_MAX_ETHPORTS);
4501 	memset(txring_numa, NUMA_NO_CONFIG, RTE_MAX_ETHPORTS);
4502 }
4503 
4504 static void
4505 print_stats(void)
4506 {
4507 	uint8_t i;
4508 	const char clr[] = { 27, '[', '2', 'J', '\0' };
4509 	const char top_left[] = { 27, '[', '1', ';', '1', 'H', '\0' };
4510 
4511 	/* Clear screen and move to top left */
4512 	printf("%s%s", clr, top_left);
4513 
4514 	printf("\nPort statistics ====================================");
4515 	for (i = 0; i < cur_fwd_config.nb_fwd_ports; i++)
4516 		nic_stats_display(fwd_ports_ids[i]);
4517 
4518 	fflush(stdout);
4519 }
4520 
4521 static void
4522 signal_handler(int signum __rte_unused)
4523 {
4524 	f_quit = 1;
4525 	prompt_exit();
4526 }
4527 
4528 int
4529 main(int argc, char** argv)
4530 {
4531 	int diag;
4532 	portid_t port_id;
4533 	uint16_t count;
4534 	int ret;
4535 
4536 #ifdef RTE_EXEC_ENV_WINDOWS
4537 	signal(SIGINT, signal_handler);
4538 	signal(SIGTERM, signal_handler);
4539 #else
4540 	/* Want read() not to be restarted on signal */
4541 	struct sigaction action = {
4542 		.sa_handler = signal_handler,
4543 	};
4544 
4545 	sigaction(SIGINT, &action, NULL);
4546 	sigaction(SIGTERM, &action, NULL);
4547 #endif
4548 
4549 	testpmd_logtype = rte_log_register("testpmd");
4550 	if (testpmd_logtype < 0)
4551 		rte_exit(EXIT_FAILURE, "Cannot register log type");
4552 	rte_log_set_level(testpmd_logtype, RTE_LOG_DEBUG);
4553 
4554 	diag = rte_eal_init(argc, argv);
4555 	if (diag < 0)
4556 		rte_exit(EXIT_FAILURE, "Cannot init EAL: %s\n",
4557 			 rte_strerror(rte_errno));
4558 
4559 	/* allocate port structures, and init them */
4560 	init_port();
4561 
4562 	ret = register_eth_event_callback();
4563 	if (ret != 0)
4564 		rte_exit(EXIT_FAILURE, "Cannot register for ethdev events");
4565 
4566 #ifdef RTE_LIB_PDUMP
4567 	/* initialize packet capture framework */
4568 	rte_pdump_init();
4569 #endif
4570 
4571 	count = 0;
4572 	RTE_ETH_FOREACH_DEV(port_id) {
4573 		ports_ids[count] = port_id;
4574 		count++;
4575 	}
4576 	nb_ports = (portid_t) count;
4577 	if (nb_ports == 0)
4578 		TESTPMD_LOG(WARNING, "No probed ethernet devices\n");
4579 
4580 	set_def_fwd_config();
4581 	if (nb_lcores == 0)
4582 		rte_exit(EXIT_FAILURE, "No cores defined for forwarding\n"
4583 			 "Check the core mask argument\n");
4584 
4585 	/* Bitrate/latency stats disabled by default */
4586 #ifdef RTE_LIB_BITRATESTATS
4587 	bitrate_enabled = 0;
4588 #endif
4589 #ifdef RTE_LIB_LATENCYSTATS
4590 	latencystats_enabled = 0;
4591 #endif
4592 
4593 	/* on FreeBSD, mlockall() is disabled by default */
4594 #ifdef RTE_EXEC_ENV_FREEBSD
4595 	do_mlockall = 0;
4596 #else
4597 	do_mlockall = 1;
4598 #endif
4599 
4600 	argc -= diag;
4601 	argv += diag;
4602 	if (argc > 1)
4603 		launch_args_parse(argc, argv);
4604 
4605 #ifndef RTE_EXEC_ENV_WINDOWS
4606 	if (do_mlockall && mlockall(MCL_CURRENT | MCL_FUTURE)) {
4607 		TESTPMD_LOG(NOTICE, "mlockall() failed with error \"%s\"\n",
4608 			strerror(errno));
4609 	}
4610 #endif
4611 
4612 	if (tx_first && interactive)
4613 		rte_exit(EXIT_FAILURE, "--tx-first cannot be used on "
4614 				"interactive mode.\n");
4615 
4616 	if (tx_first && lsc_interrupt) {
4617 		fprintf(stderr,
4618 			"Warning: lsc_interrupt needs to be off when using tx_first. Disabling.\n");
4619 		lsc_interrupt = 0;
4620 	}
4621 
4622 	if (!nb_rxq && !nb_txq)
4623 		fprintf(stderr,
4624 			"Warning: Either rx or tx queues should be non-zero\n");
4625 
4626 	if (nb_rxq > 1 && nb_rxq > nb_txq)
4627 		fprintf(stderr,
4628 			"Warning: nb_rxq=%d enables RSS configuration, but nb_txq=%d will prevent to fully test it.\n",
4629 			nb_rxq, nb_txq);
4630 
4631 	init_config();
4632 
4633 	if (hot_plug) {
4634 		ret = rte_dev_hotplug_handle_enable();
4635 		if (ret) {
4636 			RTE_LOG(ERR, EAL,
4637 				"fail to enable hotplug handling.");
4638 			return -1;
4639 		}
4640 
4641 		ret = rte_dev_event_monitor_start();
4642 		if (ret) {
4643 			RTE_LOG(ERR, EAL,
4644 				"fail to start device event monitoring.");
4645 			return -1;
4646 		}
4647 
4648 		ret = rte_dev_event_callback_register(NULL,
4649 			dev_event_callback, NULL);
4650 		if (ret) {
4651 			RTE_LOG(ERR, EAL,
4652 				"fail  to register device event callback\n");
4653 			return -1;
4654 		}
4655 	}
4656 
4657 	if (!no_device_start && start_port(RTE_PORT_ALL) != 0) {
4658 		if (!interactive) {
4659 			rte_eal_cleanup();
4660 			rte_exit(EXIT_FAILURE, "Start ports failed\n");
4661 		}
4662 		fprintf(stderr, "Start ports failed\n");
4663 	}
4664 
4665 	/* set all ports to promiscuous mode by default */
4666 	RTE_ETH_FOREACH_DEV(port_id) {
4667 		ret = rte_eth_promiscuous_enable(port_id);
4668 		if (ret != 0)
4669 			fprintf(stderr,
4670 				"Error during enabling promiscuous mode for port %u: %s - ignore\n",
4671 				port_id, rte_strerror(-ret));
4672 	}
4673 
4674 #ifdef RTE_LIB_METRICS
4675 	/* Init metrics library */
4676 	rte_metrics_init(rte_socket_id());
4677 #endif
4678 
4679 #ifdef RTE_LIB_LATENCYSTATS
4680 	if (latencystats_enabled != 0) {
4681 		int ret = rte_latencystats_init(1, NULL);
4682 		if (ret)
4683 			fprintf(stderr,
4684 				"Warning: latencystats init() returned error %d\n",
4685 				ret);
4686 		fprintf(stderr, "Latencystats running on lcore %d\n",
4687 			latencystats_lcore_id);
4688 	}
4689 #endif
4690 
4691 	/* Setup bitrate stats */
4692 #ifdef RTE_LIB_BITRATESTATS
4693 	if (bitrate_enabled != 0) {
4694 		bitrate_data = rte_stats_bitrate_create();
4695 		if (bitrate_data == NULL)
4696 			rte_exit(EXIT_FAILURE,
4697 				"Could not allocate bitrate data.\n");
4698 		rte_stats_bitrate_reg(bitrate_data);
4699 	}
4700 #endif
4701 
4702 	if (record_core_cycles)
4703 		rte_lcore_register_usage_cb(lcore_usage_callback);
4704 
4705 #ifdef RTE_LIB_CMDLINE
4706 	if (init_cmdline() != 0)
4707 		rte_exit(EXIT_FAILURE,
4708 			"Could not initialise cmdline context.\n");
4709 
4710 	if (strlen(cmdline_filename) != 0)
4711 		cmdline_read_from_file(cmdline_filename);
4712 
4713 	if (interactive == 1) {
4714 		if (auto_start) {
4715 			printf("Start automatic packet forwarding\n");
4716 			start_packet_forwarding(0);
4717 		}
4718 		prompt();
4719 	} else
4720 #endif
4721 	{
4722 		printf("No commandline core given, start packet forwarding\n");
4723 		start_packet_forwarding(tx_first);
4724 		if (stats_period != 0) {
4725 			uint64_t prev_time = 0, cur_time, diff_time = 0;
4726 			uint64_t timer_period;
4727 
4728 			/* Convert to number of cycles */
4729 			timer_period = stats_period * rte_get_timer_hz();
4730 
4731 			while (f_quit == 0) {
4732 				cur_time = rte_get_timer_cycles();
4733 				diff_time += cur_time - prev_time;
4734 
4735 				if (diff_time >= timer_period) {
4736 					print_stats();
4737 					/* Reset the timer */
4738 					diff_time = 0;
4739 				}
4740 				/* Sleep to avoid unnecessary checks */
4741 				prev_time = cur_time;
4742 				rte_delay_us_sleep(US_PER_S);
4743 			}
4744 		} else {
4745 			char c;
4746 			fd_set fds;
4747 
4748 			printf("Press enter to exit\n");
4749 
4750 			FD_ZERO(&fds);
4751 			FD_SET(0, &fds);
4752 
4753 			/* wait for signal or enter */
4754 			ret = select(1, &fds, NULL, NULL, NULL);
4755 			if (ret < 0 && errno != EINTR)
4756 				rte_exit(EXIT_FAILURE,
4757 					 "Select failed: %s\n",
4758 					 strerror(errno));
4759 
4760 			/* if got enter then consume it */
4761 			if (ret == 1 && read(0, &c, 1) < 0)
4762 				rte_exit(EXIT_FAILURE,
4763 					 "Read failed: %s\n",
4764 					 strerror(errno));
4765 		}
4766 	}
4767 
4768 	pmd_test_exit();
4769 
4770 #ifdef RTE_LIB_PDUMP
4771 	/* uninitialize packet capture framework */
4772 	rte_pdump_uninit();
4773 #endif
4774 #ifdef RTE_LIB_LATENCYSTATS
4775 	if (latencystats_enabled != 0)
4776 		rte_latencystats_uninit();
4777 #endif
4778 
4779 	ret = unregister_eth_event_callback();
4780 	if (ret != 0)
4781 		rte_exit(EXIT_FAILURE, "Cannot unregister for ethdev events");
4782 
4783 
4784 	ret = rte_eal_cleanup();
4785 	if (ret != 0)
4786 		rte_exit(EXIT_FAILURE,
4787 			 "EAL cleanup failed: %s\n", strerror(-ret));
4788 
4789 	return EXIT_SUCCESS;
4790 }
4791