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