xref: /dpdk/app/test-pmd/testpmd.c (revision 4e261f551986d40fa0c8388ed50899b609add04b)
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 #include <sys/mman.h>
13 #include <sys/types.h>
14 #include <errno.h>
15 #include <stdbool.h>
16 
17 #include <sys/queue.h>
18 #include <sys/stat.h>
19 
20 #include <stdint.h>
21 #include <unistd.h>
22 #include <inttypes.h>
23 
24 #include <rte_common.h>
25 #include <rte_errno.h>
26 #include <rte_byteorder.h>
27 #include <rte_log.h>
28 #include <rte_debug.h>
29 #include <rte_cycles.h>
30 #include <rte_malloc_heap.h>
31 #include <rte_memory.h>
32 #include <rte_memcpy.h>
33 #include <rte_launch.h>
34 #include <rte_eal.h>
35 #include <rte_alarm.h>
36 #include <rte_per_lcore.h>
37 #include <rte_lcore.h>
38 #include <rte_atomic.h>
39 #include <rte_branch_prediction.h>
40 #include <rte_mempool.h>
41 #include <rte_malloc.h>
42 #include <rte_mbuf.h>
43 #include <rte_mbuf_pool_ops.h>
44 #include <rte_interrupts.h>
45 #include <rte_pci.h>
46 #include <rte_ether.h>
47 #include <rte_ethdev.h>
48 #include <rte_dev.h>
49 #include <rte_string_fns.h>
50 #ifdef RTE_LIBRTE_IXGBE_PMD
51 #include <rte_pmd_ixgbe.h>
52 #endif
53 #ifdef RTE_LIBRTE_PDUMP
54 #include <rte_pdump.h>
55 #endif
56 #include <rte_flow.h>
57 #include <rte_metrics.h>
58 #ifdef RTE_LIBRTE_BITRATE
59 #include <rte_bitrate.h>
60 #endif
61 #ifdef RTE_LIBRTE_LATENCY_STATS
62 #include <rte_latencystats.h>
63 #endif
64 
65 #include "testpmd.h"
66 
67 #ifndef MAP_HUGETLB
68 /* FreeBSD may not have MAP_HUGETLB (in fact, it probably doesn't) */
69 #define HUGE_FLAG (0x40000)
70 #else
71 #define HUGE_FLAG MAP_HUGETLB
72 #endif
73 
74 #ifndef MAP_HUGE_SHIFT
75 /* older kernels (or FreeBSD) will not have this define */
76 #define HUGE_SHIFT (26)
77 #else
78 #define HUGE_SHIFT MAP_HUGE_SHIFT
79 #endif
80 
81 #define EXTMEM_HEAP_NAME "extmem"
82 
83 uint16_t verbose_level = 0; /**< Silent by default. */
84 int testpmd_logtype; /**< Log type for testpmd logs */
85 
86 /* use master core for command line ? */
87 uint8_t interactive = 0;
88 uint8_t auto_start = 0;
89 uint8_t tx_first;
90 char cmdline_filename[PATH_MAX] = {0};
91 
92 /*
93  * NUMA support configuration.
94  * When set, the NUMA support attempts to dispatch the allocation of the
95  * RX and TX memory rings, and of the DMA memory buffers (mbufs) for the
96  * probed ports among the CPU sockets 0 and 1.
97  * Otherwise, all memory is allocated from CPU socket 0.
98  */
99 uint8_t numa_support = 1; /**< numa enabled by default */
100 
101 /*
102  * In UMA mode,all memory is allocated from socket 0 if --socket-num is
103  * not configured.
104  */
105 uint8_t socket_num = UMA_NO_CONFIG;
106 
107 /*
108  * Select mempool allocation type:
109  * - native: use regular DPDK memory
110  * - anon: use regular DPDK memory to create mempool, but populate using
111  *         anonymous memory (may not be IOVA-contiguous)
112  * - xmem: use externally allocated hugepage memory
113  */
114 uint8_t mp_alloc_type = MP_ALLOC_NATIVE;
115 
116 /*
117  * Store specified sockets on which memory pool to be used by ports
118  * is allocated.
119  */
120 uint8_t port_numa[RTE_MAX_ETHPORTS];
121 
122 /*
123  * Store specified sockets on which RX ring to be used by ports
124  * is allocated.
125  */
126 uint8_t rxring_numa[RTE_MAX_ETHPORTS];
127 
128 /*
129  * Store specified sockets on which TX ring to be used by ports
130  * is allocated.
131  */
132 uint8_t txring_numa[RTE_MAX_ETHPORTS];
133 
134 /*
135  * Record the Ethernet address of peer target ports to which packets are
136  * forwarded.
137  * Must be instantiated with the ethernet addresses of peer traffic generator
138  * ports.
139  */
140 struct ether_addr peer_eth_addrs[RTE_MAX_ETHPORTS];
141 portid_t nb_peer_eth_addrs = 0;
142 
143 /*
144  * Probed Target Environment.
145  */
146 struct rte_port *ports;	       /**< For all probed ethernet ports. */
147 portid_t nb_ports;             /**< Number of probed ethernet ports. */
148 struct fwd_lcore **fwd_lcores; /**< For all probed logical cores. */
149 lcoreid_t nb_lcores;           /**< Number of probed logical cores. */
150 
151 portid_t ports_ids[RTE_MAX_ETHPORTS]; /**< Store all port ids. */
152 
153 /*
154  * Test Forwarding Configuration.
155  *    nb_fwd_lcores <= nb_cfg_lcores <= nb_lcores
156  *    nb_fwd_ports  <= nb_cfg_ports  <= nb_ports
157  */
158 lcoreid_t nb_cfg_lcores; /**< Number of configured logical cores. */
159 lcoreid_t nb_fwd_lcores; /**< Number of forwarding logical cores. */
160 portid_t  nb_cfg_ports;  /**< Number of configured ports. */
161 portid_t  nb_fwd_ports;  /**< Number of forwarding ports. */
162 
163 unsigned int fwd_lcores_cpuids[RTE_MAX_LCORE]; /**< CPU ids configuration. */
164 portid_t fwd_ports_ids[RTE_MAX_ETHPORTS];      /**< Port ids configuration. */
165 
166 struct fwd_stream **fwd_streams; /**< For each RX queue of each port. */
167 streamid_t nb_fwd_streams;       /**< Is equal to (nb_ports * nb_rxq). */
168 
169 /*
170  * Forwarding engines.
171  */
172 struct fwd_engine * fwd_engines[] = {
173 	&io_fwd_engine,
174 	&mac_fwd_engine,
175 	&mac_swap_engine,
176 	&flow_gen_engine,
177 	&rx_only_engine,
178 	&tx_only_engine,
179 	&csum_fwd_engine,
180 	&icmp_echo_engine,
181 	&noisy_vnf_engine,
182 #if defined RTE_LIBRTE_PMD_SOFTNIC
183 	&softnic_fwd_engine,
184 #endif
185 #ifdef RTE_LIBRTE_IEEE1588
186 	&ieee1588_fwd_engine,
187 #endif
188 	NULL,
189 };
190 
191 struct fwd_config cur_fwd_config;
192 struct fwd_engine *cur_fwd_eng = &io_fwd_engine; /**< IO mode by default. */
193 uint32_t retry_enabled;
194 uint32_t burst_tx_delay_time = BURST_TX_WAIT_US;
195 uint32_t burst_tx_retry_num = BURST_TX_RETRIES;
196 
197 uint16_t mbuf_data_size = DEFAULT_MBUF_DATA_SIZE; /**< Mbuf data space size. */
198 uint32_t param_total_num_mbufs = 0;  /**< number of mbufs in all pools - if
199                                       * specified on command-line. */
200 uint16_t stats_period; /**< Period to show statistics (disabled by default) */
201 
202 /*
203  * In container, it cannot terminate the process which running with 'stats-period'
204  * option. Set flag to exit stats period loop after received SIGINT/SIGTERM.
205  */
206 uint8_t f_quit;
207 
208 /*
209  * Configuration of packet segments used by the "txonly" processing engine.
210  */
211 uint16_t tx_pkt_length = TXONLY_DEF_PACKET_LEN; /**< TXONLY packet length. */
212 uint16_t tx_pkt_seg_lengths[RTE_MAX_SEGS_PER_PKT] = {
213 	TXONLY_DEF_PACKET_LEN,
214 };
215 uint8_t  tx_pkt_nb_segs = 1; /**< Number of segments in TXONLY packets */
216 
217 enum tx_pkt_split tx_pkt_split = TX_PKT_SPLIT_OFF;
218 /**< Split policy for packets to TX. */
219 
220 uint16_t nb_pkt_per_burst = DEF_PKT_BURST; /**< Number of packets per burst. */
221 uint16_t mb_mempool_cache = DEF_MBUF_CACHE; /**< Size of mbuf mempool cache. */
222 
223 /* current configuration is in DCB or not,0 means it is not in DCB mode */
224 uint8_t dcb_config = 0;
225 
226 /* Whether the dcb is in testing status */
227 uint8_t dcb_test = 0;
228 
229 /*
230  * Configurable number of RX/TX queues.
231  */
232 queueid_t nb_rxq = 1; /**< Number of RX queues per port. */
233 queueid_t nb_txq = 1; /**< Number of TX queues per port. */
234 
235 /*
236  * Configurable number of RX/TX ring descriptors.
237  * Defaults are supplied by drivers via ethdev.
238  */
239 #define RTE_TEST_RX_DESC_DEFAULT 0
240 #define RTE_TEST_TX_DESC_DEFAULT 0
241 uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT; /**< Number of RX descriptors. */
242 uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT; /**< Number of TX descriptors. */
243 
244 #define RTE_PMD_PARAM_UNSET -1
245 /*
246  * Configurable values of RX and TX ring threshold registers.
247  */
248 
249 int8_t rx_pthresh = RTE_PMD_PARAM_UNSET;
250 int8_t rx_hthresh = RTE_PMD_PARAM_UNSET;
251 int8_t rx_wthresh = RTE_PMD_PARAM_UNSET;
252 
253 int8_t tx_pthresh = RTE_PMD_PARAM_UNSET;
254 int8_t tx_hthresh = RTE_PMD_PARAM_UNSET;
255 int8_t tx_wthresh = RTE_PMD_PARAM_UNSET;
256 
257 /*
258  * Configurable value of RX free threshold.
259  */
260 int16_t rx_free_thresh = RTE_PMD_PARAM_UNSET;
261 
262 /*
263  * Configurable value of RX drop enable.
264  */
265 int8_t rx_drop_en = RTE_PMD_PARAM_UNSET;
266 
267 /*
268  * Configurable value of TX free threshold.
269  */
270 int16_t tx_free_thresh = RTE_PMD_PARAM_UNSET;
271 
272 /*
273  * Configurable value of TX RS bit threshold.
274  */
275 int16_t tx_rs_thresh = RTE_PMD_PARAM_UNSET;
276 
277 /*
278  * Configurable value of buffered packets before sending.
279  */
280 uint16_t noisy_tx_sw_bufsz;
281 
282 /*
283  * Configurable value of packet buffer timeout.
284  */
285 uint16_t noisy_tx_sw_buf_flush_time;
286 
287 /*
288  * Configurable value for size of VNF internal memory area
289  * used for simulating noisy neighbour behaviour
290  */
291 uint64_t noisy_lkup_mem_sz;
292 
293 /*
294  * Configurable value of number of random writes done in
295  * VNF simulation memory area.
296  */
297 uint64_t noisy_lkup_num_writes;
298 
299 /*
300  * Configurable value of number of random reads done in
301  * VNF simulation memory area.
302  */
303 uint64_t noisy_lkup_num_reads;
304 
305 /*
306  * Configurable value of number of random reads/writes done in
307  * VNF simulation memory area.
308  */
309 uint64_t noisy_lkup_num_reads_writes;
310 
311 /*
312  * Receive Side Scaling (RSS) configuration.
313  */
314 uint64_t rss_hf = ETH_RSS_IP; /* RSS IP by default. */
315 
316 /*
317  * Port topology configuration
318  */
319 uint16_t port_topology = PORT_TOPOLOGY_PAIRED; /* Ports are paired by default */
320 
321 /*
322  * Avoids to flush all the RX streams before starts forwarding.
323  */
324 uint8_t no_flush_rx = 0; /* flush by default */
325 
326 /*
327  * Flow API isolated mode.
328  */
329 uint8_t flow_isolate_all;
330 
331 /*
332  * Avoids to check link status when starting/stopping a port.
333  */
334 uint8_t no_link_check = 0; /* check by default */
335 
336 /*
337  * Enable link status change notification
338  */
339 uint8_t lsc_interrupt = 1; /* enabled by default */
340 
341 /*
342  * Enable device removal notification.
343  */
344 uint8_t rmv_interrupt = 1; /* enabled by default */
345 
346 uint8_t hot_plug = 0; /**< hotplug disabled by default. */
347 
348 /* After attach, port setup is called on event or by iterator */
349 bool setup_on_probe_event = true;
350 
351 /* Pretty printing of ethdev events */
352 static const char * const eth_event_desc[] = {
353 	[RTE_ETH_EVENT_UNKNOWN] = "unknown",
354 	[RTE_ETH_EVENT_INTR_LSC] = "link state change",
355 	[RTE_ETH_EVENT_QUEUE_STATE] = "queue state",
356 	[RTE_ETH_EVENT_INTR_RESET] = "reset",
357 	[RTE_ETH_EVENT_VF_MBOX] = "VF mbox",
358 	[RTE_ETH_EVENT_IPSEC] = "IPsec",
359 	[RTE_ETH_EVENT_MACSEC] = "MACsec",
360 	[RTE_ETH_EVENT_INTR_RMV] = "device removal",
361 	[RTE_ETH_EVENT_NEW] = "device probed",
362 	[RTE_ETH_EVENT_DESTROY] = "device released",
363 	[RTE_ETH_EVENT_MAX] = NULL,
364 };
365 
366 /*
367  * Display or mask ether events
368  * Default to all events except VF_MBOX
369  */
370 uint32_t event_print_mask = (UINT32_C(1) << RTE_ETH_EVENT_UNKNOWN) |
371 			    (UINT32_C(1) << RTE_ETH_EVENT_INTR_LSC) |
372 			    (UINT32_C(1) << RTE_ETH_EVENT_QUEUE_STATE) |
373 			    (UINT32_C(1) << RTE_ETH_EVENT_INTR_RESET) |
374 			    (UINT32_C(1) << RTE_ETH_EVENT_IPSEC) |
375 			    (UINT32_C(1) << RTE_ETH_EVENT_MACSEC) |
376 			    (UINT32_C(1) << RTE_ETH_EVENT_INTR_RMV);
377 /*
378  * Decide if all memory are locked for performance.
379  */
380 int do_mlockall = 0;
381 
382 /*
383  * NIC bypass mode configuration options.
384  */
385 
386 #if defined RTE_LIBRTE_IXGBE_PMD && defined RTE_LIBRTE_IXGBE_BYPASS
387 /* The NIC bypass watchdog timeout. */
388 uint32_t bypass_timeout = RTE_PMD_IXGBE_BYPASS_TMT_OFF;
389 #endif
390 
391 
392 #ifdef RTE_LIBRTE_LATENCY_STATS
393 
394 /*
395  * Set when latency stats is enabled in the commandline
396  */
397 uint8_t latencystats_enabled;
398 
399 /*
400  * Lcore ID to serive latency statistics.
401  */
402 lcoreid_t latencystats_lcore_id = -1;
403 
404 #endif
405 
406 /*
407  * Ethernet device configuration.
408  */
409 struct rte_eth_rxmode rx_mode = {
410 	.max_rx_pkt_len = ETHER_MAX_LEN, /**< Default maximum frame length. */
411 };
412 
413 struct rte_eth_txmode tx_mode = {
414 	.offloads = DEV_TX_OFFLOAD_MBUF_FAST_FREE,
415 };
416 
417 struct rte_fdir_conf fdir_conf = {
418 	.mode = RTE_FDIR_MODE_NONE,
419 	.pballoc = RTE_FDIR_PBALLOC_64K,
420 	.status = RTE_FDIR_REPORT_STATUS,
421 	.mask = {
422 		.vlan_tci_mask = 0xFFEF,
423 		.ipv4_mask     = {
424 			.src_ip = 0xFFFFFFFF,
425 			.dst_ip = 0xFFFFFFFF,
426 		},
427 		.ipv6_mask     = {
428 			.src_ip = {0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF},
429 			.dst_ip = {0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF},
430 		},
431 		.src_port_mask = 0xFFFF,
432 		.dst_port_mask = 0xFFFF,
433 		.mac_addr_byte_mask = 0xFF,
434 		.tunnel_type_mask = 1,
435 		.tunnel_id_mask = 0xFFFFFFFF,
436 	},
437 	.drop_queue = 127,
438 };
439 
440 volatile int test_done = 1; /* stop packet forwarding when set to 1. */
441 
442 struct queue_stats_mappings tx_queue_stats_mappings_array[MAX_TX_QUEUE_STATS_MAPPINGS];
443 struct queue_stats_mappings rx_queue_stats_mappings_array[MAX_RX_QUEUE_STATS_MAPPINGS];
444 
445 struct queue_stats_mappings *tx_queue_stats_mappings = tx_queue_stats_mappings_array;
446 struct queue_stats_mappings *rx_queue_stats_mappings = rx_queue_stats_mappings_array;
447 
448 uint16_t nb_tx_queue_stats_mappings = 0;
449 uint16_t nb_rx_queue_stats_mappings = 0;
450 
451 /*
452  * Display zero values by default for xstats
453  */
454 uint8_t xstats_hide_zero;
455 
456 unsigned int num_sockets = 0;
457 unsigned int socket_ids[RTE_MAX_NUMA_NODES];
458 
459 #ifdef RTE_LIBRTE_BITRATE
460 /* Bitrate statistics */
461 struct rte_stats_bitrates *bitrate_data;
462 lcoreid_t bitrate_lcore_id;
463 uint8_t bitrate_enabled;
464 #endif
465 
466 struct gro_status gro_ports[RTE_MAX_ETHPORTS];
467 uint8_t gro_flush_cycles = GRO_DEFAULT_FLUSH_CYCLES;
468 
469 struct vxlan_encap_conf vxlan_encap_conf = {
470 	.select_ipv4 = 1,
471 	.select_vlan = 0,
472 	.vni = "\x00\x00\x00",
473 	.udp_src = 0,
474 	.udp_dst = RTE_BE16(4789),
475 	.ipv4_src = IPv4(127, 0, 0, 1),
476 	.ipv4_dst = IPv4(255, 255, 255, 255),
477 	.ipv6_src = "\x00\x00\x00\x00\x00\x00\x00\x00"
478 		"\x00\x00\x00\x00\x00\x00\x00\x01",
479 	.ipv6_dst = "\x00\x00\x00\x00\x00\x00\x00\x00"
480 		"\x00\x00\x00\x00\x00\x00\x11\x11",
481 	.vlan_tci = 0,
482 	.eth_src = "\x00\x00\x00\x00\x00\x00",
483 	.eth_dst = "\xff\xff\xff\xff\xff\xff",
484 };
485 
486 struct nvgre_encap_conf nvgre_encap_conf = {
487 	.select_ipv4 = 1,
488 	.select_vlan = 0,
489 	.tni = "\x00\x00\x00",
490 	.ipv4_src = IPv4(127, 0, 0, 1),
491 	.ipv4_dst = IPv4(255, 255, 255, 255),
492 	.ipv6_src = "\x00\x00\x00\x00\x00\x00\x00\x00"
493 		"\x00\x00\x00\x00\x00\x00\x00\x01",
494 	.ipv6_dst = "\x00\x00\x00\x00\x00\x00\x00\x00"
495 		"\x00\x00\x00\x00\x00\x00\x11\x11",
496 	.vlan_tci = 0,
497 	.eth_src = "\x00\x00\x00\x00\x00\x00",
498 	.eth_dst = "\xff\xff\xff\xff\xff\xff",
499 };
500 
501 /* Forward function declarations */
502 static void setup_attached_port(portid_t pi);
503 static void map_port_queue_stats_mapping_registers(portid_t pi,
504 						   struct rte_port *port);
505 static void check_all_ports_link_status(uint32_t port_mask);
506 static int eth_event_callback(portid_t port_id,
507 			      enum rte_eth_event_type type,
508 			      void *param, void *ret_param);
509 static void dev_event_callback(const char *device_name,
510 				enum rte_dev_event_type type,
511 				void *param);
512 
513 /*
514  * Check if all the ports are started.
515  * If yes, return positive value. If not, return zero.
516  */
517 static int all_ports_started(void);
518 
519 struct gso_status gso_ports[RTE_MAX_ETHPORTS];
520 uint16_t gso_max_segment_size = ETHER_MAX_LEN - ETHER_CRC_LEN;
521 
522 /*
523  * Helper function to check if socket is already discovered.
524  * If yes, return positive value. If not, return zero.
525  */
526 int
527 new_socket_id(unsigned int socket_id)
528 {
529 	unsigned int i;
530 
531 	for (i = 0; i < num_sockets; i++) {
532 		if (socket_ids[i] == socket_id)
533 			return 0;
534 	}
535 	return 1;
536 }
537 
538 /*
539  * Setup default configuration.
540  */
541 static void
542 set_default_fwd_lcores_config(void)
543 {
544 	unsigned int i;
545 	unsigned int nb_lc;
546 	unsigned int sock_num;
547 
548 	nb_lc = 0;
549 	for (i = 0; i < RTE_MAX_LCORE; i++) {
550 		if (!rte_lcore_is_enabled(i))
551 			continue;
552 		sock_num = rte_lcore_to_socket_id(i);
553 		if (new_socket_id(sock_num)) {
554 			if (num_sockets >= RTE_MAX_NUMA_NODES) {
555 				rte_exit(EXIT_FAILURE,
556 					 "Total sockets greater than %u\n",
557 					 RTE_MAX_NUMA_NODES);
558 			}
559 			socket_ids[num_sockets++] = sock_num;
560 		}
561 		if (i == rte_get_master_lcore())
562 			continue;
563 		fwd_lcores_cpuids[nb_lc++] = i;
564 	}
565 	nb_lcores = (lcoreid_t) nb_lc;
566 	nb_cfg_lcores = nb_lcores;
567 	nb_fwd_lcores = 1;
568 }
569 
570 static void
571 set_def_peer_eth_addrs(void)
572 {
573 	portid_t i;
574 
575 	for (i = 0; i < RTE_MAX_ETHPORTS; i++) {
576 		peer_eth_addrs[i].addr_bytes[0] = ETHER_LOCAL_ADMIN_ADDR;
577 		peer_eth_addrs[i].addr_bytes[5] = i;
578 	}
579 }
580 
581 static void
582 set_default_fwd_ports_config(void)
583 {
584 	portid_t pt_id;
585 	int i = 0;
586 
587 	RTE_ETH_FOREACH_DEV(pt_id) {
588 		fwd_ports_ids[i++] = pt_id;
589 
590 		/* Update sockets info according to the attached device */
591 		int socket_id = rte_eth_dev_socket_id(pt_id);
592 		if (socket_id >= 0 && new_socket_id(socket_id)) {
593 			if (num_sockets >= RTE_MAX_NUMA_NODES) {
594 				rte_exit(EXIT_FAILURE,
595 					 "Total sockets greater than %u\n",
596 					 RTE_MAX_NUMA_NODES);
597 			}
598 			socket_ids[num_sockets++] = socket_id;
599 		}
600 	}
601 
602 	nb_cfg_ports = nb_ports;
603 	nb_fwd_ports = nb_ports;
604 }
605 
606 void
607 set_def_fwd_config(void)
608 {
609 	set_default_fwd_lcores_config();
610 	set_def_peer_eth_addrs();
611 	set_default_fwd_ports_config();
612 }
613 
614 /* extremely pessimistic estimation of memory required to create a mempool */
615 static int
616 calc_mem_size(uint32_t nb_mbufs, uint32_t mbuf_sz, size_t pgsz, size_t *out)
617 {
618 	unsigned int n_pages, mbuf_per_pg, leftover;
619 	uint64_t total_mem, mbuf_mem, obj_sz;
620 
621 	/* there is no good way to predict how much space the mempool will
622 	 * occupy because it will allocate chunks on the fly, and some of those
623 	 * will come from default DPDK memory while some will come from our
624 	 * external memory, so just assume 128MB will be enough for everyone.
625 	 */
626 	uint64_t hdr_mem = 128 << 20;
627 
628 	/* account for possible non-contiguousness */
629 	obj_sz = rte_mempool_calc_obj_size(mbuf_sz, 0, NULL);
630 	if (obj_sz > pgsz) {
631 		TESTPMD_LOG(ERR, "Object size is bigger than page size\n");
632 		return -1;
633 	}
634 
635 	mbuf_per_pg = pgsz / obj_sz;
636 	leftover = (nb_mbufs % mbuf_per_pg) > 0;
637 	n_pages = (nb_mbufs / mbuf_per_pg) + leftover;
638 
639 	mbuf_mem = n_pages * pgsz;
640 
641 	total_mem = RTE_ALIGN(hdr_mem + mbuf_mem, pgsz);
642 
643 	if (total_mem > SIZE_MAX) {
644 		TESTPMD_LOG(ERR, "Memory size too big\n");
645 		return -1;
646 	}
647 	*out = (size_t)total_mem;
648 
649 	return 0;
650 }
651 
652 static inline uint32_t
653 log2_u64(uint64_t v)
654 {
655 	if (v == 0)
656 		return 0;
657 	v = rte_align64pow2(v);
658 	return rte_bsf64(v);
659 }
660 
661 static int
662 pagesz_flags(uint64_t page_sz)
663 {
664 	/* as per mmap() manpage, all page sizes are log2 of page size
665 	 * shifted by MAP_HUGE_SHIFT
666 	 */
667 	int log2 = log2_u64(page_sz);
668 
669 	return (log2 << HUGE_SHIFT);
670 }
671 
672 static void *
673 alloc_mem(size_t memsz, size_t pgsz, bool huge)
674 {
675 	void *addr;
676 	int flags;
677 
678 	/* allocate anonymous hugepages */
679 	flags = MAP_ANONYMOUS | MAP_PRIVATE;
680 	if (huge)
681 		flags |= HUGE_FLAG | pagesz_flags(pgsz);
682 
683 	addr = mmap(NULL, memsz, PROT_READ | PROT_WRITE, flags, -1, 0);
684 	if (addr == MAP_FAILED)
685 		return NULL;
686 
687 	return addr;
688 }
689 
690 struct extmem_param {
691 	void *addr;
692 	size_t len;
693 	size_t pgsz;
694 	rte_iova_t *iova_table;
695 	unsigned int iova_table_len;
696 };
697 
698 static int
699 create_extmem(uint32_t nb_mbufs, uint32_t mbuf_sz, struct extmem_param *param,
700 		bool huge)
701 {
702 	uint64_t pgsizes[] = {RTE_PGSIZE_2M, RTE_PGSIZE_1G, /* x86_64, ARM */
703 			RTE_PGSIZE_16M, RTE_PGSIZE_16G};    /* POWER */
704 	unsigned int cur_page, n_pages, pgsz_idx;
705 	size_t mem_sz, cur_pgsz;
706 	rte_iova_t *iovas = NULL;
707 	void *addr;
708 	int ret;
709 
710 	for (pgsz_idx = 0; pgsz_idx < RTE_DIM(pgsizes); pgsz_idx++) {
711 		/* skip anything that is too big */
712 		if (pgsizes[pgsz_idx] > SIZE_MAX)
713 			continue;
714 
715 		cur_pgsz = pgsizes[pgsz_idx];
716 
717 		/* if we were told not to allocate hugepages, override */
718 		if (!huge)
719 			cur_pgsz = sysconf(_SC_PAGESIZE);
720 
721 		ret = calc_mem_size(nb_mbufs, mbuf_sz, cur_pgsz, &mem_sz);
722 		if (ret < 0) {
723 			TESTPMD_LOG(ERR, "Cannot calculate memory size\n");
724 			return -1;
725 		}
726 
727 		/* allocate our memory */
728 		addr = alloc_mem(mem_sz, cur_pgsz, huge);
729 
730 		/* if we couldn't allocate memory with a specified page size,
731 		 * that doesn't mean we can't do it with other page sizes, so
732 		 * try another one.
733 		 */
734 		if (addr == NULL)
735 			continue;
736 
737 		/* store IOVA addresses for every page in this memory area */
738 		n_pages = mem_sz / cur_pgsz;
739 
740 		iovas = malloc(sizeof(*iovas) * n_pages);
741 
742 		if (iovas == NULL) {
743 			TESTPMD_LOG(ERR, "Cannot allocate memory for iova addresses\n");
744 			goto fail;
745 		}
746 		/* lock memory if it's not huge pages */
747 		if (!huge)
748 			mlock(addr, mem_sz);
749 
750 		/* populate IOVA addresses */
751 		for (cur_page = 0; cur_page < n_pages; cur_page++) {
752 			rte_iova_t iova;
753 			size_t offset;
754 			void *cur;
755 
756 			offset = cur_pgsz * cur_page;
757 			cur = RTE_PTR_ADD(addr, offset);
758 
759 			/* touch the page before getting its IOVA */
760 			*(volatile char *)cur = 0;
761 
762 			iova = rte_mem_virt2iova(cur);
763 
764 			iovas[cur_page] = iova;
765 		}
766 
767 		break;
768 	}
769 	/* if we couldn't allocate anything */
770 	if (iovas == NULL)
771 		return -1;
772 
773 	param->addr = addr;
774 	param->len = mem_sz;
775 	param->pgsz = cur_pgsz;
776 	param->iova_table = iovas;
777 	param->iova_table_len = n_pages;
778 
779 	return 0;
780 fail:
781 	if (iovas)
782 		free(iovas);
783 	if (addr)
784 		munmap(addr, mem_sz);
785 
786 	return -1;
787 }
788 
789 static int
790 setup_extmem(uint32_t nb_mbufs, uint32_t mbuf_sz, bool huge)
791 {
792 	struct extmem_param param;
793 	int socket_id, ret;
794 
795 	memset(&param, 0, sizeof(param));
796 
797 	/* check if our heap exists */
798 	socket_id = rte_malloc_heap_get_socket(EXTMEM_HEAP_NAME);
799 	if (socket_id < 0) {
800 		/* create our heap */
801 		ret = rte_malloc_heap_create(EXTMEM_HEAP_NAME);
802 		if (ret < 0) {
803 			TESTPMD_LOG(ERR, "Cannot create heap\n");
804 			return -1;
805 		}
806 	}
807 
808 	ret = create_extmem(nb_mbufs, mbuf_sz, &param, huge);
809 	if (ret < 0) {
810 		TESTPMD_LOG(ERR, "Cannot create memory area\n");
811 		return -1;
812 	}
813 
814 	/* we now have a valid memory area, so add it to heap */
815 	ret = rte_malloc_heap_memory_add(EXTMEM_HEAP_NAME,
816 			param.addr, param.len, param.iova_table,
817 			param.iova_table_len, param.pgsz);
818 
819 	/* when using VFIO, memory is automatically mapped for DMA by EAL */
820 
821 	/* not needed any more */
822 	free(param.iova_table);
823 
824 	if (ret < 0) {
825 		TESTPMD_LOG(ERR, "Cannot add memory to heap\n");
826 		munmap(param.addr, param.len);
827 		return -1;
828 	}
829 
830 	/* success */
831 
832 	TESTPMD_LOG(DEBUG, "Allocated %zuMB of external memory\n",
833 			param.len >> 20);
834 
835 	return 0;
836 }
837 
838 /*
839  * Configuration initialisation done once at init time.
840  */
841 static void
842 mbuf_pool_create(uint16_t mbuf_seg_size, unsigned nb_mbuf,
843 		 unsigned int socket_id)
844 {
845 	char pool_name[RTE_MEMPOOL_NAMESIZE];
846 	struct rte_mempool *rte_mp = NULL;
847 	uint32_t mb_size;
848 
849 	mb_size = sizeof(struct rte_mbuf) + mbuf_seg_size;
850 	mbuf_poolname_build(socket_id, pool_name, sizeof(pool_name));
851 
852 	TESTPMD_LOG(INFO,
853 		"create a new mbuf pool <%s>: n=%u, size=%u, socket=%u\n",
854 		pool_name, nb_mbuf, mbuf_seg_size, socket_id);
855 
856 	switch (mp_alloc_type) {
857 	case MP_ALLOC_NATIVE:
858 		{
859 			/* wrapper to rte_mempool_create() */
860 			TESTPMD_LOG(INFO, "preferred mempool ops selected: %s\n",
861 					rte_mbuf_best_mempool_ops());
862 			rte_mp = rte_pktmbuf_pool_create(pool_name, nb_mbuf,
863 				mb_mempool_cache, 0, mbuf_seg_size, socket_id);
864 			break;
865 		}
866 	case MP_ALLOC_ANON:
867 		{
868 			rte_mp = rte_mempool_create_empty(pool_name, nb_mbuf,
869 				mb_size, (unsigned int) mb_mempool_cache,
870 				sizeof(struct rte_pktmbuf_pool_private),
871 				socket_id, 0);
872 			if (rte_mp == NULL)
873 				goto err;
874 
875 			if (rte_mempool_populate_anon(rte_mp) == 0) {
876 				rte_mempool_free(rte_mp);
877 				rte_mp = NULL;
878 				goto err;
879 			}
880 			rte_pktmbuf_pool_init(rte_mp, NULL);
881 			rte_mempool_obj_iter(rte_mp, rte_pktmbuf_init, NULL);
882 			break;
883 		}
884 	case MP_ALLOC_XMEM:
885 	case MP_ALLOC_XMEM_HUGE:
886 		{
887 			int heap_socket;
888 			bool huge = mp_alloc_type == MP_ALLOC_XMEM_HUGE;
889 
890 			if (setup_extmem(nb_mbuf, mbuf_seg_size, huge) < 0)
891 				rte_exit(EXIT_FAILURE, "Could not create external memory\n");
892 
893 			heap_socket =
894 				rte_malloc_heap_get_socket(EXTMEM_HEAP_NAME);
895 			if (heap_socket < 0)
896 				rte_exit(EXIT_FAILURE, "Could not get external memory socket ID\n");
897 
898 			TESTPMD_LOG(INFO, "preferred mempool ops selected: %s\n",
899 					rte_mbuf_best_mempool_ops());
900 			rte_mp = rte_pktmbuf_pool_create(pool_name, nb_mbuf,
901 					mb_mempool_cache, 0, mbuf_seg_size,
902 					heap_socket);
903 			break;
904 		}
905 	default:
906 		{
907 			rte_exit(EXIT_FAILURE, "Invalid mempool creation mode\n");
908 		}
909 	}
910 
911 err:
912 	if (rte_mp == NULL) {
913 		rte_exit(EXIT_FAILURE,
914 			"Creation of mbuf pool for socket %u failed: %s\n",
915 			socket_id, rte_strerror(rte_errno));
916 	} else if (verbose_level > 0) {
917 		rte_mempool_dump(stdout, rte_mp);
918 	}
919 }
920 
921 /*
922  * Check given socket id is valid or not with NUMA mode,
923  * if valid, return 0, else return -1
924  */
925 static int
926 check_socket_id(const unsigned int socket_id)
927 {
928 	static int warning_once = 0;
929 
930 	if (new_socket_id(socket_id)) {
931 		if (!warning_once && numa_support)
932 			printf("Warning: NUMA should be configured manually by"
933 			       " using --port-numa-config and"
934 			       " --ring-numa-config parameters along with"
935 			       " --numa.\n");
936 		warning_once = 1;
937 		return -1;
938 	}
939 	return 0;
940 }
941 
942 /*
943  * Get the allowed maximum number of RX queues.
944  * *pid return the port id which has minimal value of
945  * max_rx_queues in all ports.
946  */
947 queueid_t
948 get_allowed_max_nb_rxq(portid_t *pid)
949 {
950 	queueid_t allowed_max_rxq = MAX_QUEUE_ID;
951 	portid_t pi;
952 	struct rte_eth_dev_info dev_info;
953 
954 	RTE_ETH_FOREACH_DEV(pi) {
955 		rte_eth_dev_info_get(pi, &dev_info);
956 		if (dev_info.max_rx_queues < allowed_max_rxq) {
957 			allowed_max_rxq = dev_info.max_rx_queues;
958 			*pid = pi;
959 		}
960 	}
961 	return allowed_max_rxq;
962 }
963 
964 /*
965  * Check input rxq is valid or not.
966  * If input rxq is not greater than any of maximum number
967  * of RX queues of all ports, it is valid.
968  * if valid, return 0, else return -1
969  */
970 int
971 check_nb_rxq(queueid_t rxq)
972 {
973 	queueid_t allowed_max_rxq;
974 	portid_t pid = 0;
975 
976 	allowed_max_rxq = get_allowed_max_nb_rxq(&pid);
977 	if (rxq > allowed_max_rxq) {
978 		printf("Fail: input rxq (%u) can't be greater "
979 		       "than max_rx_queues (%u) of port %u\n",
980 		       rxq,
981 		       allowed_max_rxq,
982 		       pid);
983 		return -1;
984 	}
985 	return 0;
986 }
987 
988 /*
989  * Get the allowed maximum number of TX queues.
990  * *pid return the port id which has minimal value of
991  * max_tx_queues in all ports.
992  */
993 queueid_t
994 get_allowed_max_nb_txq(portid_t *pid)
995 {
996 	queueid_t allowed_max_txq = MAX_QUEUE_ID;
997 	portid_t pi;
998 	struct rte_eth_dev_info dev_info;
999 
1000 	RTE_ETH_FOREACH_DEV(pi) {
1001 		rte_eth_dev_info_get(pi, &dev_info);
1002 		if (dev_info.max_tx_queues < allowed_max_txq) {
1003 			allowed_max_txq = dev_info.max_tx_queues;
1004 			*pid = pi;
1005 		}
1006 	}
1007 	return allowed_max_txq;
1008 }
1009 
1010 /*
1011  * Check input txq is valid or not.
1012  * If input txq is not greater than any of maximum number
1013  * of TX queues of all ports, it is valid.
1014  * if valid, return 0, else return -1
1015  */
1016 int
1017 check_nb_txq(queueid_t txq)
1018 {
1019 	queueid_t allowed_max_txq;
1020 	portid_t pid = 0;
1021 
1022 	allowed_max_txq = get_allowed_max_nb_txq(&pid);
1023 	if (txq > allowed_max_txq) {
1024 		printf("Fail: input txq (%u) can't be greater "
1025 		       "than max_tx_queues (%u) of port %u\n",
1026 		       txq,
1027 		       allowed_max_txq,
1028 		       pid);
1029 		return -1;
1030 	}
1031 	return 0;
1032 }
1033 
1034 static void
1035 init_config(void)
1036 {
1037 	portid_t pid;
1038 	struct rte_port *port;
1039 	struct rte_mempool *mbp;
1040 	unsigned int nb_mbuf_per_pool;
1041 	lcoreid_t  lc_id;
1042 	uint8_t port_per_socket[RTE_MAX_NUMA_NODES];
1043 	struct rte_gro_param gro_param;
1044 	uint32_t gso_types;
1045 	int k;
1046 
1047 	memset(port_per_socket,0,RTE_MAX_NUMA_NODES);
1048 
1049 	/* Configuration of logical cores. */
1050 	fwd_lcores = rte_zmalloc("testpmd: fwd_lcores",
1051 				sizeof(struct fwd_lcore *) * nb_lcores,
1052 				RTE_CACHE_LINE_SIZE);
1053 	if (fwd_lcores == NULL) {
1054 		rte_exit(EXIT_FAILURE, "rte_zmalloc(%d (struct fwd_lcore *)) "
1055 							"failed\n", nb_lcores);
1056 	}
1057 	for (lc_id = 0; lc_id < nb_lcores; lc_id++) {
1058 		fwd_lcores[lc_id] = rte_zmalloc("testpmd: struct fwd_lcore",
1059 					       sizeof(struct fwd_lcore),
1060 					       RTE_CACHE_LINE_SIZE);
1061 		if (fwd_lcores[lc_id] == NULL) {
1062 			rte_exit(EXIT_FAILURE, "rte_zmalloc(struct fwd_lcore) "
1063 								"failed\n");
1064 		}
1065 		fwd_lcores[lc_id]->cpuid_idx = lc_id;
1066 	}
1067 
1068 	RTE_ETH_FOREACH_DEV(pid) {
1069 		port = &ports[pid];
1070 		/* Apply default TxRx configuration for all ports */
1071 		port->dev_conf.txmode = tx_mode;
1072 		port->dev_conf.rxmode = rx_mode;
1073 		rte_eth_dev_info_get(pid, &port->dev_info);
1074 
1075 		if (!(port->dev_info.tx_offload_capa &
1076 		      DEV_TX_OFFLOAD_MBUF_FAST_FREE))
1077 			port->dev_conf.txmode.offloads &=
1078 				~DEV_TX_OFFLOAD_MBUF_FAST_FREE;
1079 		if (!(port->dev_info.tx_offload_capa &
1080 			DEV_TX_OFFLOAD_MATCH_METADATA))
1081 			port->dev_conf.txmode.offloads &=
1082 				~DEV_TX_OFFLOAD_MATCH_METADATA;
1083 		if (numa_support) {
1084 			if (port_numa[pid] != NUMA_NO_CONFIG)
1085 				port_per_socket[port_numa[pid]]++;
1086 			else {
1087 				uint32_t socket_id = rte_eth_dev_socket_id(pid);
1088 
1089 				/*
1090 				 * if socket_id is invalid,
1091 				 * set to the first available socket.
1092 				 */
1093 				if (check_socket_id(socket_id) < 0)
1094 					socket_id = socket_ids[0];
1095 				port_per_socket[socket_id]++;
1096 			}
1097 		}
1098 
1099 		/* Apply Rx offloads configuration */
1100 		for (k = 0; k < port->dev_info.max_rx_queues; k++)
1101 			port->rx_conf[k].offloads =
1102 				port->dev_conf.rxmode.offloads;
1103 		/* Apply Tx offloads configuration */
1104 		for (k = 0; k < port->dev_info.max_tx_queues; k++)
1105 			port->tx_conf[k].offloads =
1106 				port->dev_conf.txmode.offloads;
1107 
1108 		/* set flag to initialize port/queue */
1109 		port->need_reconfig = 1;
1110 		port->need_reconfig_queues = 1;
1111 		port->tx_metadata = 0;
1112 	}
1113 
1114 	/*
1115 	 * Create pools of mbuf.
1116 	 * If NUMA support is disabled, create a single pool of mbuf in
1117 	 * socket 0 memory by default.
1118 	 * Otherwise, create a pool of mbuf in the memory of sockets 0 and 1.
1119 	 *
1120 	 * Use the maximum value of nb_rxd and nb_txd here, then nb_rxd and
1121 	 * nb_txd can be configured at run time.
1122 	 */
1123 	if (param_total_num_mbufs)
1124 		nb_mbuf_per_pool = param_total_num_mbufs;
1125 	else {
1126 		nb_mbuf_per_pool = RTE_TEST_RX_DESC_MAX +
1127 			(nb_lcores * mb_mempool_cache) +
1128 			RTE_TEST_TX_DESC_MAX + MAX_PKT_BURST;
1129 		nb_mbuf_per_pool *= RTE_MAX_ETHPORTS;
1130 	}
1131 
1132 	if (numa_support) {
1133 		uint8_t i;
1134 
1135 		for (i = 0; i < num_sockets; i++)
1136 			mbuf_pool_create(mbuf_data_size, nb_mbuf_per_pool,
1137 					 socket_ids[i]);
1138 	} else {
1139 		if (socket_num == UMA_NO_CONFIG)
1140 			mbuf_pool_create(mbuf_data_size, nb_mbuf_per_pool, 0);
1141 		else
1142 			mbuf_pool_create(mbuf_data_size, nb_mbuf_per_pool,
1143 						 socket_num);
1144 	}
1145 
1146 	init_port_config();
1147 
1148 	gso_types = DEV_TX_OFFLOAD_TCP_TSO | DEV_TX_OFFLOAD_VXLAN_TNL_TSO |
1149 		DEV_TX_OFFLOAD_GRE_TNL_TSO | DEV_TX_OFFLOAD_UDP_TSO;
1150 	/*
1151 	 * Records which Mbuf pool to use by each logical core, if needed.
1152 	 */
1153 	for (lc_id = 0; lc_id < nb_lcores; lc_id++) {
1154 		mbp = mbuf_pool_find(
1155 			rte_lcore_to_socket_id(fwd_lcores_cpuids[lc_id]));
1156 
1157 		if (mbp == NULL)
1158 			mbp = mbuf_pool_find(0);
1159 		fwd_lcores[lc_id]->mbp = mbp;
1160 		/* initialize GSO context */
1161 		fwd_lcores[lc_id]->gso_ctx.direct_pool = mbp;
1162 		fwd_lcores[lc_id]->gso_ctx.indirect_pool = mbp;
1163 		fwd_lcores[lc_id]->gso_ctx.gso_types = gso_types;
1164 		fwd_lcores[lc_id]->gso_ctx.gso_size = ETHER_MAX_LEN -
1165 			ETHER_CRC_LEN;
1166 		fwd_lcores[lc_id]->gso_ctx.flag = 0;
1167 	}
1168 
1169 	/* Configuration of packet forwarding streams. */
1170 	if (init_fwd_streams() < 0)
1171 		rte_exit(EXIT_FAILURE, "FAIL from init_fwd_streams()\n");
1172 
1173 	fwd_config_setup();
1174 
1175 	/* create a gro context for each lcore */
1176 	gro_param.gro_types = RTE_GRO_TCP_IPV4;
1177 	gro_param.max_flow_num = GRO_MAX_FLUSH_CYCLES;
1178 	gro_param.max_item_per_flow = MAX_PKT_BURST;
1179 	for (lc_id = 0; lc_id < nb_lcores; lc_id++) {
1180 		gro_param.socket_id = rte_lcore_to_socket_id(
1181 				fwd_lcores_cpuids[lc_id]);
1182 		fwd_lcores[lc_id]->gro_ctx = rte_gro_ctx_create(&gro_param);
1183 		if (fwd_lcores[lc_id]->gro_ctx == NULL) {
1184 			rte_exit(EXIT_FAILURE,
1185 					"rte_gro_ctx_create() failed\n");
1186 		}
1187 	}
1188 
1189 #if defined RTE_LIBRTE_PMD_SOFTNIC
1190 	if (strcmp(cur_fwd_eng->fwd_mode_name, "softnic") == 0) {
1191 		RTE_ETH_FOREACH_DEV(pid) {
1192 			port = &ports[pid];
1193 			const char *driver = port->dev_info.driver_name;
1194 
1195 			if (strcmp(driver, "net_softnic") == 0)
1196 				port->softport.fwd_lcore_arg = fwd_lcores;
1197 		}
1198 	}
1199 #endif
1200 
1201 }
1202 
1203 
1204 void
1205 reconfig(portid_t new_port_id, unsigned socket_id)
1206 {
1207 	struct rte_port *port;
1208 
1209 	/* Reconfiguration of Ethernet ports. */
1210 	port = &ports[new_port_id];
1211 	rte_eth_dev_info_get(new_port_id, &port->dev_info);
1212 
1213 	/* set flag to initialize port/queue */
1214 	port->need_reconfig = 1;
1215 	port->need_reconfig_queues = 1;
1216 	port->socket_id = socket_id;
1217 
1218 	init_port_config();
1219 }
1220 
1221 
1222 int
1223 init_fwd_streams(void)
1224 {
1225 	portid_t pid;
1226 	struct rte_port *port;
1227 	streamid_t sm_id, nb_fwd_streams_new;
1228 	queueid_t q;
1229 
1230 	/* set socket id according to numa or not */
1231 	RTE_ETH_FOREACH_DEV(pid) {
1232 		port = &ports[pid];
1233 		if (nb_rxq > port->dev_info.max_rx_queues) {
1234 			printf("Fail: nb_rxq(%d) is greater than "
1235 				"max_rx_queues(%d)\n", nb_rxq,
1236 				port->dev_info.max_rx_queues);
1237 			return -1;
1238 		}
1239 		if (nb_txq > port->dev_info.max_tx_queues) {
1240 			printf("Fail: nb_txq(%d) is greater than "
1241 				"max_tx_queues(%d)\n", nb_txq,
1242 				port->dev_info.max_tx_queues);
1243 			return -1;
1244 		}
1245 		if (numa_support) {
1246 			if (port_numa[pid] != NUMA_NO_CONFIG)
1247 				port->socket_id = port_numa[pid];
1248 			else {
1249 				port->socket_id = rte_eth_dev_socket_id(pid);
1250 
1251 				/*
1252 				 * if socket_id is invalid,
1253 				 * set to the first available socket.
1254 				 */
1255 				if (check_socket_id(port->socket_id) < 0)
1256 					port->socket_id = socket_ids[0];
1257 			}
1258 		}
1259 		else {
1260 			if (socket_num == UMA_NO_CONFIG)
1261 				port->socket_id = 0;
1262 			else
1263 				port->socket_id = socket_num;
1264 		}
1265 	}
1266 
1267 	q = RTE_MAX(nb_rxq, nb_txq);
1268 	if (q == 0) {
1269 		printf("Fail: Cannot allocate fwd streams as number of queues is 0\n");
1270 		return -1;
1271 	}
1272 	nb_fwd_streams_new = (streamid_t)(nb_ports * q);
1273 	if (nb_fwd_streams_new == nb_fwd_streams)
1274 		return 0;
1275 	/* clear the old */
1276 	if (fwd_streams != NULL) {
1277 		for (sm_id = 0; sm_id < nb_fwd_streams; sm_id++) {
1278 			if (fwd_streams[sm_id] == NULL)
1279 				continue;
1280 			rte_free(fwd_streams[sm_id]);
1281 			fwd_streams[sm_id] = NULL;
1282 		}
1283 		rte_free(fwd_streams);
1284 		fwd_streams = NULL;
1285 	}
1286 
1287 	/* init new */
1288 	nb_fwd_streams = nb_fwd_streams_new;
1289 	if (nb_fwd_streams) {
1290 		fwd_streams = rte_zmalloc("testpmd: fwd_streams",
1291 			sizeof(struct fwd_stream *) * nb_fwd_streams,
1292 			RTE_CACHE_LINE_SIZE);
1293 		if (fwd_streams == NULL)
1294 			rte_exit(EXIT_FAILURE, "rte_zmalloc(%d"
1295 				 " (struct fwd_stream *)) failed\n",
1296 				 nb_fwd_streams);
1297 
1298 		for (sm_id = 0; sm_id < nb_fwd_streams; sm_id++) {
1299 			fwd_streams[sm_id] = rte_zmalloc("testpmd:"
1300 				" struct fwd_stream", sizeof(struct fwd_stream),
1301 				RTE_CACHE_LINE_SIZE);
1302 			if (fwd_streams[sm_id] == NULL)
1303 				rte_exit(EXIT_FAILURE, "rte_zmalloc"
1304 					 "(struct fwd_stream) failed\n");
1305 		}
1306 	}
1307 
1308 	return 0;
1309 }
1310 
1311 #ifdef RTE_TEST_PMD_RECORD_BURST_STATS
1312 static void
1313 pkt_burst_stats_display(const char *rx_tx, struct pkt_burst_stats *pbs)
1314 {
1315 	unsigned int total_burst;
1316 	unsigned int nb_burst;
1317 	unsigned int burst_stats[3];
1318 	uint16_t pktnb_stats[3];
1319 	uint16_t nb_pkt;
1320 	int burst_percent[3];
1321 
1322 	/*
1323 	 * First compute the total number of packet bursts and the
1324 	 * two highest numbers of bursts of the same number of packets.
1325 	 */
1326 	total_burst = 0;
1327 	burst_stats[0] = burst_stats[1] = burst_stats[2] = 0;
1328 	pktnb_stats[0] = pktnb_stats[1] = pktnb_stats[2] = 0;
1329 	for (nb_pkt = 0; nb_pkt < MAX_PKT_BURST; nb_pkt++) {
1330 		nb_burst = pbs->pkt_burst_spread[nb_pkt];
1331 		if (nb_burst == 0)
1332 			continue;
1333 		total_burst += nb_burst;
1334 		if (nb_burst > burst_stats[0]) {
1335 			burst_stats[1] = burst_stats[0];
1336 			pktnb_stats[1] = pktnb_stats[0];
1337 			burst_stats[0] = nb_burst;
1338 			pktnb_stats[0] = nb_pkt;
1339 		} else if (nb_burst > burst_stats[1]) {
1340 			burst_stats[1] = nb_burst;
1341 			pktnb_stats[1] = nb_pkt;
1342 		}
1343 	}
1344 	if (total_burst == 0)
1345 		return;
1346 	burst_percent[0] = (burst_stats[0] * 100) / total_burst;
1347 	printf("  %s-bursts : %u [%d%% of %d pkts", rx_tx, total_burst,
1348 	       burst_percent[0], (int) pktnb_stats[0]);
1349 	if (burst_stats[0] == total_burst) {
1350 		printf("]\n");
1351 		return;
1352 	}
1353 	if (burst_stats[0] + burst_stats[1] == total_burst) {
1354 		printf(" + %d%% of %d pkts]\n",
1355 		       100 - burst_percent[0], pktnb_stats[1]);
1356 		return;
1357 	}
1358 	burst_percent[1] = (burst_stats[1] * 100) / total_burst;
1359 	burst_percent[2] = 100 - (burst_percent[0] + burst_percent[1]);
1360 	if ((burst_percent[1] == 0) || (burst_percent[2] == 0)) {
1361 		printf(" + %d%% of others]\n", 100 - burst_percent[0]);
1362 		return;
1363 	}
1364 	printf(" + %d%% of %d pkts + %d%% of others]\n",
1365 	       burst_percent[1], (int) pktnb_stats[1], burst_percent[2]);
1366 }
1367 #endif /* RTE_TEST_PMD_RECORD_BURST_STATS */
1368 
1369 static void
1370 fwd_port_stats_display(portid_t port_id, struct rte_eth_stats *stats)
1371 {
1372 	struct rte_port *port;
1373 	uint8_t i;
1374 
1375 	static const char *fwd_stats_border = "----------------------";
1376 
1377 	port = &ports[port_id];
1378 	printf("\n  %s Forward statistics for port %-2d %s\n",
1379 	       fwd_stats_border, port_id, fwd_stats_border);
1380 
1381 	if ((!port->rx_queue_stats_mapping_enabled) && (!port->tx_queue_stats_mapping_enabled)) {
1382 		printf("  RX-packets: %-14"PRIu64" RX-dropped: %-14"PRIu64"RX-total: "
1383 		       "%-"PRIu64"\n",
1384 		       stats->ipackets, stats->imissed,
1385 		       (uint64_t) (stats->ipackets + stats->imissed));
1386 
1387 		if (cur_fwd_eng == &csum_fwd_engine)
1388 			printf("  Bad-ipcsum: %-14"PRIu64" Bad-l4csum: %-14"PRIu64"Bad-outer-l4csum: %-14"PRIu64"\n",
1389 			       port->rx_bad_ip_csum, port->rx_bad_l4_csum,
1390 			       port->rx_bad_outer_l4_csum);
1391 		if ((stats->ierrors + stats->rx_nombuf) > 0) {
1392 			printf("  RX-error: %-"PRIu64"\n",  stats->ierrors);
1393 			printf("  RX-nombufs: %-14"PRIu64"\n", stats->rx_nombuf);
1394 		}
1395 
1396 		printf("  TX-packets: %-14"PRIu64" TX-dropped: %-14"PRIu64"TX-total: "
1397 		       "%-"PRIu64"\n",
1398 		       stats->opackets, port->tx_dropped,
1399 		       (uint64_t) (stats->opackets + port->tx_dropped));
1400 	}
1401 	else {
1402 		printf("  RX-packets:             %14"PRIu64"    RX-dropped:%14"PRIu64"    RX-total:"
1403 		       "%14"PRIu64"\n",
1404 		       stats->ipackets, stats->imissed,
1405 		       (uint64_t) (stats->ipackets + stats->imissed));
1406 
1407 		if (cur_fwd_eng == &csum_fwd_engine)
1408 			printf("  Bad-ipcsum:%14"PRIu64"    Bad-l4csum:%14"PRIu64"    Bad-outer-l4csum: %-14"PRIu64"\n",
1409 			       port->rx_bad_ip_csum, port->rx_bad_l4_csum,
1410 			       port->rx_bad_outer_l4_csum);
1411 		if ((stats->ierrors + stats->rx_nombuf) > 0) {
1412 			printf("  RX-error:%"PRIu64"\n", stats->ierrors);
1413 			printf("  RX-nombufs:             %14"PRIu64"\n",
1414 			       stats->rx_nombuf);
1415 		}
1416 
1417 		printf("  TX-packets:             %14"PRIu64"    TX-dropped:%14"PRIu64"    TX-total:"
1418 		       "%14"PRIu64"\n",
1419 		       stats->opackets, port->tx_dropped,
1420 		       (uint64_t) (stats->opackets + port->tx_dropped));
1421 	}
1422 
1423 #ifdef RTE_TEST_PMD_RECORD_BURST_STATS
1424 	if (port->rx_stream)
1425 		pkt_burst_stats_display("RX",
1426 			&port->rx_stream->rx_burst_stats);
1427 	if (port->tx_stream)
1428 		pkt_burst_stats_display("TX",
1429 			&port->tx_stream->tx_burst_stats);
1430 #endif
1431 
1432 	if (port->rx_queue_stats_mapping_enabled) {
1433 		printf("\n");
1434 		for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS; i++) {
1435 			printf("  Stats reg %2d RX-packets:%14"PRIu64
1436 			       "     RX-errors:%14"PRIu64
1437 			       "    RX-bytes:%14"PRIu64"\n",
1438 			       i, stats->q_ipackets[i], stats->q_errors[i], stats->q_ibytes[i]);
1439 		}
1440 		printf("\n");
1441 	}
1442 	if (port->tx_queue_stats_mapping_enabled) {
1443 		for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS; i++) {
1444 			printf("  Stats reg %2d TX-packets:%14"PRIu64
1445 			       "                                 TX-bytes:%14"PRIu64"\n",
1446 			       i, stats->q_opackets[i], stats->q_obytes[i]);
1447 		}
1448 	}
1449 
1450 	printf("  %s--------------------------------%s\n",
1451 	       fwd_stats_border, fwd_stats_border);
1452 }
1453 
1454 static void
1455 fwd_stream_stats_display(streamid_t stream_id)
1456 {
1457 	struct fwd_stream *fs;
1458 	static const char *fwd_top_stats_border = "-------";
1459 
1460 	fs = fwd_streams[stream_id];
1461 	if ((fs->rx_packets == 0) && (fs->tx_packets == 0) &&
1462 	    (fs->fwd_dropped == 0))
1463 		return;
1464 	printf("\n  %s Forward Stats for RX Port=%2d/Queue=%2d -> "
1465 	       "TX Port=%2d/Queue=%2d %s\n",
1466 	       fwd_top_stats_border, fs->rx_port, fs->rx_queue,
1467 	       fs->tx_port, fs->tx_queue, fwd_top_stats_border);
1468 	printf("  RX-packets: %-14u TX-packets: %-14u TX-dropped: %-14u",
1469 	       fs->rx_packets, fs->tx_packets, fs->fwd_dropped);
1470 
1471 	/* if checksum mode */
1472 	if (cur_fwd_eng == &csum_fwd_engine) {
1473 	       printf("  RX- bad IP checksum: %-14u  Rx- bad L4 checksum: "
1474 			"%-14u Rx- bad outer L4 checksum: %-14u\n",
1475 			fs->rx_bad_ip_csum, fs->rx_bad_l4_csum,
1476 			fs->rx_bad_outer_l4_csum);
1477 	}
1478 
1479 #ifdef RTE_TEST_PMD_RECORD_BURST_STATS
1480 	pkt_burst_stats_display("RX", &fs->rx_burst_stats);
1481 	pkt_burst_stats_display("TX", &fs->tx_burst_stats);
1482 #endif
1483 }
1484 
1485 static void
1486 flush_fwd_rx_queues(void)
1487 {
1488 	struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
1489 	portid_t  rxp;
1490 	portid_t port_id;
1491 	queueid_t rxq;
1492 	uint16_t  nb_rx;
1493 	uint16_t  i;
1494 	uint8_t   j;
1495 	uint64_t prev_tsc = 0, diff_tsc, cur_tsc, timer_tsc = 0;
1496 	uint64_t timer_period;
1497 
1498 	/* convert to number of cycles */
1499 	timer_period = rte_get_timer_hz(); /* 1 second timeout */
1500 
1501 	for (j = 0; j < 2; j++) {
1502 		for (rxp = 0; rxp < cur_fwd_config.nb_fwd_ports; rxp++) {
1503 			for (rxq = 0; rxq < nb_rxq; rxq++) {
1504 				port_id = fwd_ports_ids[rxp];
1505 				/**
1506 				* testpmd can stuck in the below do while loop
1507 				* if rte_eth_rx_burst() always returns nonzero
1508 				* packets. So timer is added to exit this loop
1509 				* after 1sec timer expiry.
1510 				*/
1511 				prev_tsc = rte_rdtsc();
1512 				do {
1513 					nb_rx = rte_eth_rx_burst(port_id, rxq,
1514 						pkts_burst, MAX_PKT_BURST);
1515 					for (i = 0; i < nb_rx; i++)
1516 						rte_pktmbuf_free(pkts_burst[i]);
1517 
1518 					cur_tsc = rte_rdtsc();
1519 					diff_tsc = cur_tsc - prev_tsc;
1520 					timer_tsc += diff_tsc;
1521 				} while ((nb_rx > 0) &&
1522 					(timer_tsc < timer_period));
1523 				timer_tsc = 0;
1524 			}
1525 		}
1526 		rte_delay_ms(10); /* wait 10 milli-seconds before retrying */
1527 	}
1528 }
1529 
1530 static void
1531 run_pkt_fwd_on_lcore(struct fwd_lcore *fc, packet_fwd_t pkt_fwd)
1532 {
1533 	struct fwd_stream **fsm;
1534 	streamid_t nb_fs;
1535 	streamid_t sm_id;
1536 #ifdef RTE_LIBRTE_BITRATE
1537 	uint64_t tics_per_1sec;
1538 	uint64_t tics_datum;
1539 	uint64_t tics_current;
1540 	uint16_t i, cnt_ports;
1541 
1542 	cnt_ports = nb_ports;
1543 	tics_datum = rte_rdtsc();
1544 	tics_per_1sec = rte_get_timer_hz();
1545 #endif
1546 	fsm = &fwd_streams[fc->stream_idx];
1547 	nb_fs = fc->stream_nb;
1548 	do {
1549 		for (sm_id = 0; sm_id < nb_fs; sm_id++)
1550 			(*pkt_fwd)(fsm[sm_id]);
1551 #ifdef RTE_LIBRTE_BITRATE
1552 		if (bitrate_enabled != 0 &&
1553 				bitrate_lcore_id == rte_lcore_id()) {
1554 			tics_current = rte_rdtsc();
1555 			if (tics_current - tics_datum >= tics_per_1sec) {
1556 				/* Periodic bitrate calculation */
1557 				for (i = 0; i < cnt_ports; i++)
1558 					rte_stats_bitrate_calc(bitrate_data,
1559 						ports_ids[i]);
1560 				tics_datum = tics_current;
1561 			}
1562 		}
1563 #endif
1564 #ifdef RTE_LIBRTE_LATENCY_STATS
1565 		if (latencystats_enabled != 0 &&
1566 				latencystats_lcore_id == rte_lcore_id())
1567 			rte_latencystats_update();
1568 #endif
1569 
1570 	} while (! fc->stopped);
1571 }
1572 
1573 static int
1574 start_pkt_forward_on_core(void *fwd_arg)
1575 {
1576 	run_pkt_fwd_on_lcore((struct fwd_lcore *) fwd_arg,
1577 			     cur_fwd_config.fwd_eng->packet_fwd);
1578 	return 0;
1579 }
1580 
1581 /*
1582  * Run the TXONLY packet forwarding engine to send a single burst of packets.
1583  * Used to start communication flows in network loopback test configurations.
1584  */
1585 static int
1586 run_one_txonly_burst_on_core(void *fwd_arg)
1587 {
1588 	struct fwd_lcore *fwd_lc;
1589 	struct fwd_lcore tmp_lcore;
1590 
1591 	fwd_lc = (struct fwd_lcore *) fwd_arg;
1592 	tmp_lcore = *fwd_lc;
1593 	tmp_lcore.stopped = 1;
1594 	run_pkt_fwd_on_lcore(&tmp_lcore, tx_only_engine.packet_fwd);
1595 	return 0;
1596 }
1597 
1598 /*
1599  * Launch packet forwarding:
1600  *     - Setup per-port forwarding context.
1601  *     - launch logical cores with their forwarding configuration.
1602  */
1603 static void
1604 launch_packet_forwarding(lcore_function_t *pkt_fwd_on_lcore)
1605 {
1606 	port_fwd_begin_t port_fwd_begin;
1607 	unsigned int i;
1608 	unsigned int lc_id;
1609 	int diag;
1610 
1611 	port_fwd_begin = cur_fwd_config.fwd_eng->port_fwd_begin;
1612 	if (port_fwd_begin != NULL) {
1613 		for (i = 0; i < cur_fwd_config.nb_fwd_ports; i++)
1614 			(*port_fwd_begin)(fwd_ports_ids[i]);
1615 	}
1616 	for (i = 0; i < cur_fwd_config.nb_fwd_lcores; i++) {
1617 		lc_id = fwd_lcores_cpuids[i];
1618 		if ((interactive == 0) || (lc_id != rte_lcore_id())) {
1619 			fwd_lcores[i]->stopped = 0;
1620 			diag = rte_eal_remote_launch(pkt_fwd_on_lcore,
1621 						     fwd_lcores[i], lc_id);
1622 			if (diag != 0)
1623 				printf("launch lcore %u failed - diag=%d\n",
1624 				       lc_id, diag);
1625 		}
1626 	}
1627 }
1628 
1629 /*
1630  * Launch packet forwarding configuration.
1631  */
1632 void
1633 start_packet_forwarding(int with_tx_first)
1634 {
1635 	port_fwd_begin_t port_fwd_begin;
1636 	port_fwd_end_t  port_fwd_end;
1637 	struct rte_port *port;
1638 	unsigned int i;
1639 	portid_t   pt_id;
1640 	streamid_t sm_id;
1641 
1642 	if (strcmp(cur_fwd_eng->fwd_mode_name, "rxonly") == 0 && !nb_rxq)
1643 		rte_exit(EXIT_FAILURE, "rxq are 0, cannot use rxonly fwd mode\n");
1644 
1645 	if (strcmp(cur_fwd_eng->fwd_mode_name, "txonly") == 0 && !nb_txq)
1646 		rte_exit(EXIT_FAILURE, "txq are 0, cannot use txonly fwd mode\n");
1647 
1648 	if ((strcmp(cur_fwd_eng->fwd_mode_name, "rxonly") != 0 &&
1649 		strcmp(cur_fwd_eng->fwd_mode_name, "txonly") != 0) &&
1650 		(!nb_rxq || !nb_txq))
1651 		rte_exit(EXIT_FAILURE,
1652 			"Either rxq or txq are 0, cannot use %s fwd mode\n",
1653 			cur_fwd_eng->fwd_mode_name);
1654 
1655 	if (all_ports_started() == 0) {
1656 		printf("Not all ports were started\n");
1657 		return;
1658 	}
1659 	if (test_done == 0) {
1660 		printf("Packet forwarding already started\n");
1661 		return;
1662 	}
1663 
1664 
1665 	if(dcb_test) {
1666 		for (i = 0; i < nb_fwd_ports; i++) {
1667 			pt_id = fwd_ports_ids[i];
1668 			port = &ports[pt_id];
1669 			if (!port->dcb_flag) {
1670 				printf("In DCB mode, all forwarding ports must "
1671                                        "be configured in this mode.\n");
1672 				return;
1673 			}
1674 		}
1675 		if (nb_fwd_lcores == 1) {
1676 			printf("In DCB mode,the nb forwarding cores "
1677                                "should be larger than 1.\n");
1678 			return;
1679 		}
1680 	}
1681 	test_done = 0;
1682 
1683 	fwd_config_setup();
1684 
1685 	if(!no_flush_rx)
1686 		flush_fwd_rx_queues();
1687 
1688 	pkt_fwd_config_display(&cur_fwd_config);
1689 	rxtx_config_display();
1690 
1691 	for (i = 0; i < cur_fwd_config.nb_fwd_ports; i++) {
1692 		pt_id = fwd_ports_ids[i];
1693 		port = &ports[pt_id];
1694 		rte_eth_stats_get(pt_id, &port->stats);
1695 		port->tx_dropped = 0;
1696 
1697 		map_port_queue_stats_mapping_registers(pt_id, port);
1698 	}
1699 	for (sm_id = 0; sm_id < cur_fwd_config.nb_fwd_streams; sm_id++) {
1700 		fwd_streams[sm_id]->rx_packets = 0;
1701 		fwd_streams[sm_id]->tx_packets = 0;
1702 		fwd_streams[sm_id]->fwd_dropped = 0;
1703 		fwd_streams[sm_id]->rx_bad_ip_csum = 0;
1704 		fwd_streams[sm_id]->rx_bad_l4_csum = 0;
1705 		fwd_streams[sm_id]->rx_bad_outer_l4_csum = 0;
1706 
1707 #ifdef RTE_TEST_PMD_RECORD_BURST_STATS
1708 		memset(&fwd_streams[sm_id]->rx_burst_stats, 0,
1709 		       sizeof(fwd_streams[sm_id]->rx_burst_stats));
1710 		memset(&fwd_streams[sm_id]->tx_burst_stats, 0,
1711 		       sizeof(fwd_streams[sm_id]->tx_burst_stats));
1712 #endif
1713 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
1714 		fwd_streams[sm_id]->core_cycles = 0;
1715 #endif
1716 	}
1717 	if (with_tx_first) {
1718 		port_fwd_begin = tx_only_engine.port_fwd_begin;
1719 		if (port_fwd_begin != NULL) {
1720 			for (i = 0; i < cur_fwd_config.nb_fwd_ports; i++)
1721 				(*port_fwd_begin)(fwd_ports_ids[i]);
1722 		}
1723 		while (with_tx_first--) {
1724 			launch_packet_forwarding(
1725 					run_one_txonly_burst_on_core);
1726 			rte_eal_mp_wait_lcore();
1727 		}
1728 		port_fwd_end = tx_only_engine.port_fwd_end;
1729 		if (port_fwd_end != NULL) {
1730 			for (i = 0; i < cur_fwd_config.nb_fwd_ports; i++)
1731 				(*port_fwd_end)(fwd_ports_ids[i]);
1732 		}
1733 	}
1734 	launch_packet_forwarding(start_pkt_forward_on_core);
1735 }
1736 
1737 void
1738 stop_packet_forwarding(void)
1739 {
1740 	struct rte_eth_stats stats;
1741 	struct rte_port *port;
1742 	port_fwd_end_t  port_fwd_end;
1743 	int i;
1744 	portid_t   pt_id;
1745 	streamid_t sm_id;
1746 	lcoreid_t  lc_id;
1747 	uint64_t total_recv;
1748 	uint64_t total_xmit;
1749 	uint64_t total_rx_dropped;
1750 	uint64_t total_tx_dropped;
1751 	uint64_t total_rx_nombuf;
1752 	uint64_t tx_dropped;
1753 	uint64_t rx_bad_ip_csum;
1754 	uint64_t rx_bad_l4_csum;
1755 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
1756 	uint64_t fwd_cycles;
1757 #endif
1758 
1759 	static const char *acc_stats_border = "+++++++++++++++";
1760 
1761 	if (test_done) {
1762 		printf("Packet forwarding not started\n");
1763 		return;
1764 	}
1765 	printf("Telling cores to stop...");
1766 	for (lc_id = 0; lc_id < cur_fwd_config.nb_fwd_lcores; lc_id++)
1767 		fwd_lcores[lc_id]->stopped = 1;
1768 	printf("\nWaiting for lcores to finish...\n");
1769 	rte_eal_mp_wait_lcore();
1770 	port_fwd_end = cur_fwd_config.fwd_eng->port_fwd_end;
1771 	if (port_fwd_end != NULL) {
1772 		for (i = 0; i < cur_fwd_config.nb_fwd_ports; i++) {
1773 			pt_id = fwd_ports_ids[i];
1774 			(*port_fwd_end)(pt_id);
1775 		}
1776 	}
1777 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
1778 	fwd_cycles = 0;
1779 #endif
1780 	for (sm_id = 0; sm_id < cur_fwd_config.nb_fwd_streams; sm_id++) {
1781 		if (cur_fwd_config.nb_fwd_streams >
1782 		    cur_fwd_config.nb_fwd_ports) {
1783 			fwd_stream_stats_display(sm_id);
1784 			ports[fwd_streams[sm_id]->tx_port].tx_stream = NULL;
1785 			ports[fwd_streams[sm_id]->rx_port].rx_stream = NULL;
1786 		} else {
1787 			ports[fwd_streams[sm_id]->tx_port].tx_stream =
1788 				fwd_streams[sm_id];
1789 			ports[fwd_streams[sm_id]->rx_port].rx_stream =
1790 				fwd_streams[sm_id];
1791 		}
1792 		tx_dropped = ports[fwd_streams[sm_id]->tx_port].tx_dropped;
1793 		tx_dropped = (uint64_t) (tx_dropped +
1794 					 fwd_streams[sm_id]->fwd_dropped);
1795 		ports[fwd_streams[sm_id]->tx_port].tx_dropped = tx_dropped;
1796 
1797 		rx_bad_ip_csum =
1798 			ports[fwd_streams[sm_id]->rx_port].rx_bad_ip_csum;
1799 		rx_bad_ip_csum = (uint64_t) (rx_bad_ip_csum +
1800 					 fwd_streams[sm_id]->rx_bad_ip_csum);
1801 		ports[fwd_streams[sm_id]->rx_port].rx_bad_ip_csum =
1802 							rx_bad_ip_csum;
1803 
1804 		rx_bad_l4_csum =
1805 			ports[fwd_streams[sm_id]->rx_port].rx_bad_l4_csum;
1806 		rx_bad_l4_csum = (uint64_t) (rx_bad_l4_csum +
1807 					 fwd_streams[sm_id]->rx_bad_l4_csum);
1808 		ports[fwd_streams[sm_id]->rx_port].rx_bad_l4_csum =
1809 							rx_bad_l4_csum;
1810 
1811 		ports[fwd_streams[sm_id]->rx_port].rx_bad_outer_l4_csum +=
1812 				fwd_streams[sm_id]->rx_bad_outer_l4_csum;
1813 
1814 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
1815 		fwd_cycles = (uint64_t) (fwd_cycles +
1816 					 fwd_streams[sm_id]->core_cycles);
1817 #endif
1818 	}
1819 	total_recv = 0;
1820 	total_xmit = 0;
1821 	total_rx_dropped = 0;
1822 	total_tx_dropped = 0;
1823 	total_rx_nombuf  = 0;
1824 	for (i = 0; i < cur_fwd_config.nb_fwd_ports; i++) {
1825 		pt_id = fwd_ports_ids[i];
1826 
1827 		port = &ports[pt_id];
1828 		rte_eth_stats_get(pt_id, &stats);
1829 		stats.ipackets -= port->stats.ipackets;
1830 		port->stats.ipackets = 0;
1831 		stats.opackets -= port->stats.opackets;
1832 		port->stats.opackets = 0;
1833 		stats.ibytes   -= port->stats.ibytes;
1834 		port->stats.ibytes = 0;
1835 		stats.obytes   -= port->stats.obytes;
1836 		port->stats.obytes = 0;
1837 		stats.imissed  -= port->stats.imissed;
1838 		port->stats.imissed = 0;
1839 		stats.oerrors  -= port->stats.oerrors;
1840 		port->stats.oerrors = 0;
1841 		stats.rx_nombuf -= port->stats.rx_nombuf;
1842 		port->stats.rx_nombuf = 0;
1843 
1844 		total_recv += stats.ipackets;
1845 		total_xmit += stats.opackets;
1846 		total_rx_dropped += stats.imissed;
1847 		total_tx_dropped += port->tx_dropped;
1848 		total_rx_nombuf  += stats.rx_nombuf;
1849 
1850 		fwd_port_stats_display(pt_id, &stats);
1851 	}
1852 
1853 	printf("\n  %s Accumulated forward statistics for all ports"
1854 	       "%s\n",
1855 	       acc_stats_border, acc_stats_border);
1856 	printf("  RX-packets: %-14"PRIu64" RX-dropped: %-14"PRIu64"RX-total: "
1857 	       "%-"PRIu64"\n"
1858 	       "  TX-packets: %-14"PRIu64" TX-dropped: %-14"PRIu64"TX-total: "
1859 	       "%-"PRIu64"\n",
1860 	       total_recv, total_rx_dropped, total_recv + total_rx_dropped,
1861 	       total_xmit, total_tx_dropped, total_xmit + total_tx_dropped);
1862 	if (total_rx_nombuf > 0)
1863 		printf("  RX-nombufs: %-14"PRIu64"\n", total_rx_nombuf);
1864 	printf("  %s++++++++++++++++++++++++++++++++++++++++++++++"
1865 	       "%s\n",
1866 	       acc_stats_border, acc_stats_border);
1867 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
1868 	if (total_recv > 0)
1869 		printf("\n  CPU cycles/packet=%u (total cycles="
1870 		       "%"PRIu64" / total RX packets=%"PRIu64")\n",
1871 		       (unsigned int)(fwd_cycles / total_recv),
1872 		       fwd_cycles, total_recv);
1873 #endif
1874 	printf("\nDone.\n");
1875 	test_done = 1;
1876 }
1877 
1878 void
1879 dev_set_link_up(portid_t pid)
1880 {
1881 	if (rte_eth_dev_set_link_up(pid) < 0)
1882 		printf("\nSet link up fail.\n");
1883 }
1884 
1885 void
1886 dev_set_link_down(portid_t pid)
1887 {
1888 	if (rte_eth_dev_set_link_down(pid) < 0)
1889 		printf("\nSet link down fail.\n");
1890 }
1891 
1892 static int
1893 all_ports_started(void)
1894 {
1895 	portid_t pi;
1896 	struct rte_port *port;
1897 
1898 	RTE_ETH_FOREACH_DEV(pi) {
1899 		port = &ports[pi];
1900 		/* Check if there is a port which is not started */
1901 		if ((port->port_status != RTE_PORT_STARTED) &&
1902 			(port->slave_flag == 0))
1903 			return 0;
1904 	}
1905 
1906 	/* No port is not started */
1907 	return 1;
1908 }
1909 
1910 int
1911 port_is_stopped(portid_t port_id)
1912 {
1913 	struct rte_port *port = &ports[port_id];
1914 
1915 	if ((port->port_status != RTE_PORT_STOPPED) &&
1916 	    (port->slave_flag == 0))
1917 		return 0;
1918 	return 1;
1919 }
1920 
1921 int
1922 all_ports_stopped(void)
1923 {
1924 	portid_t pi;
1925 
1926 	RTE_ETH_FOREACH_DEV(pi) {
1927 		if (!port_is_stopped(pi))
1928 			return 0;
1929 	}
1930 
1931 	return 1;
1932 }
1933 
1934 int
1935 port_is_started(portid_t port_id)
1936 {
1937 	if (port_id_is_invalid(port_id, ENABLED_WARN))
1938 		return 0;
1939 
1940 	if (ports[port_id].port_status != RTE_PORT_STARTED)
1941 		return 0;
1942 
1943 	return 1;
1944 }
1945 
1946 int
1947 start_port(portid_t pid)
1948 {
1949 	int diag, need_check_link_status = -1;
1950 	portid_t pi;
1951 	queueid_t qi;
1952 	struct rte_port *port;
1953 	struct ether_addr mac_addr;
1954 
1955 	if (port_id_is_invalid(pid, ENABLED_WARN))
1956 		return 0;
1957 
1958 	if(dcb_config)
1959 		dcb_test = 1;
1960 	RTE_ETH_FOREACH_DEV(pi) {
1961 		if (pid != pi && pid != (portid_t)RTE_PORT_ALL)
1962 			continue;
1963 
1964 		need_check_link_status = 0;
1965 		port = &ports[pi];
1966 		if (rte_atomic16_cmpset(&(port->port_status), RTE_PORT_STOPPED,
1967 						 RTE_PORT_HANDLING) == 0) {
1968 			printf("Port %d is now not stopped\n", pi);
1969 			continue;
1970 		}
1971 
1972 		if (port->need_reconfig > 0) {
1973 			port->need_reconfig = 0;
1974 
1975 			if (flow_isolate_all) {
1976 				int ret = port_flow_isolate(pi, 1);
1977 				if (ret) {
1978 					printf("Failed to apply isolated"
1979 					       " mode on port %d\n", pi);
1980 					return -1;
1981 				}
1982 			}
1983 			configure_rxtx_dump_callbacks(0);
1984 			printf("Configuring Port %d (socket %u)\n", pi,
1985 					port->socket_id);
1986 			/* configure port */
1987 			diag = rte_eth_dev_configure(pi, nb_rxq, nb_txq,
1988 						&(port->dev_conf));
1989 			if (diag != 0) {
1990 				if (rte_atomic16_cmpset(&(port->port_status),
1991 				RTE_PORT_HANDLING, RTE_PORT_STOPPED) == 0)
1992 					printf("Port %d can not be set back "
1993 							"to stopped\n", pi);
1994 				printf("Fail to configure port %d\n", pi);
1995 				/* try to reconfigure port next time */
1996 				port->need_reconfig = 1;
1997 				return -1;
1998 			}
1999 		}
2000 		if (port->need_reconfig_queues > 0) {
2001 			port->need_reconfig_queues = 0;
2002 			/* setup tx queues */
2003 			for (qi = 0; qi < nb_txq; qi++) {
2004 				if ((numa_support) &&
2005 					(txring_numa[pi] != NUMA_NO_CONFIG))
2006 					diag = rte_eth_tx_queue_setup(pi, qi,
2007 						port->nb_tx_desc[qi],
2008 						txring_numa[pi],
2009 						&(port->tx_conf[qi]));
2010 				else
2011 					diag = rte_eth_tx_queue_setup(pi, qi,
2012 						port->nb_tx_desc[qi],
2013 						port->socket_id,
2014 						&(port->tx_conf[qi]));
2015 
2016 				if (diag == 0)
2017 					continue;
2018 
2019 				/* Fail to setup tx queue, return */
2020 				if (rte_atomic16_cmpset(&(port->port_status),
2021 							RTE_PORT_HANDLING,
2022 							RTE_PORT_STOPPED) == 0)
2023 					printf("Port %d can not be set back "
2024 							"to stopped\n", pi);
2025 				printf("Fail to configure port %d tx queues\n",
2026 				       pi);
2027 				/* try to reconfigure queues next time */
2028 				port->need_reconfig_queues = 1;
2029 				return -1;
2030 			}
2031 			for (qi = 0; qi < nb_rxq; qi++) {
2032 				/* setup rx queues */
2033 				if ((numa_support) &&
2034 					(rxring_numa[pi] != NUMA_NO_CONFIG)) {
2035 					struct rte_mempool * mp =
2036 						mbuf_pool_find(rxring_numa[pi]);
2037 					if (mp == NULL) {
2038 						printf("Failed to setup RX queue:"
2039 							"No mempool allocation"
2040 							" on the socket %d\n",
2041 							rxring_numa[pi]);
2042 						return -1;
2043 					}
2044 
2045 					diag = rte_eth_rx_queue_setup(pi, qi,
2046 					     port->nb_rx_desc[qi],
2047 					     rxring_numa[pi],
2048 					     &(port->rx_conf[qi]),
2049 					     mp);
2050 				} else {
2051 					struct rte_mempool *mp =
2052 						mbuf_pool_find(port->socket_id);
2053 					if (mp == NULL) {
2054 						printf("Failed to setup RX queue:"
2055 							"No mempool allocation"
2056 							" on the socket %d\n",
2057 							port->socket_id);
2058 						return -1;
2059 					}
2060 					diag = rte_eth_rx_queue_setup(pi, qi,
2061 					     port->nb_rx_desc[qi],
2062 					     port->socket_id,
2063 					     &(port->rx_conf[qi]),
2064 					     mp);
2065 				}
2066 				if (diag == 0)
2067 					continue;
2068 
2069 				/* Fail to setup rx queue, return */
2070 				if (rte_atomic16_cmpset(&(port->port_status),
2071 							RTE_PORT_HANDLING,
2072 							RTE_PORT_STOPPED) == 0)
2073 					printf("Port %d can not be set back "
2074 							"to stopped\n", pi);
2075 				printf("Fail to configure port %d rx queues\n",
2076 				       pi);
2077 				/* try to reconfigure queues next time */
2078 				port->need_reconfig_queues = 1;
2079 				return -1;
2080 			}
2081 		}
2082 		configure_rxtx_dump_callbacks(verbose_level);
2083 		/* start port */
2084 		if (rte_eth_dev_start(pi) < 0) {
2085 			printf("Fail to start port %d\n", pi);
2086 
2087 			/* Fail to setup rx queue, return */
2088 			if (rte_atomic16_cmpset(&(port->port_status),
2089 				RTE_PORT_HANDLING, RTE_PORT_STOPPED) == 0)
2090 				printf("Port %d can not be set back to "
2091 							"stopped\n", pi);
2092 			continue;
2093 		}
2094 
2095 		if (rte_atomic16_cmpset(&(port->port_status),
2096 			RTE_PORT_HANDLING, RTE_PORT_STARTED) == 0)
2097 			printf("Port %d can not be set into started\n", pi);
2098 
2099 		rte_eth_macaddr_get(pi, &mac_addr);
2100 		printf("Port %d: %02X:%02X:%02X:%02X:%02X:%02X\n", pi,
2101 				mac_addr.addr_bytes[0], mac_addr.addr_bytes[1],
2102 				mac_addr.addr_bytes[2], mac_addr.addr_bytes[3],
2103 				mac_addr.addr_bytes[4], mac_addr.addr_bytes[5]);
2104 
2105 		/* at least one port started, need checking link status */
2106 		need_check_link_status = 1;
2107 	}
2108 
2109 	if (need_check_link_status == 1 && !no_link_check)
2110 		check_all_ports_link_status(RTE_PORT_ALL);
2111 	else if (need_check_link_status == 0)
2112 		printf("Please stop the ports first\n");
2113 
2114 	printf("Done\n");
2115 	return 0;
2116 }
2117 
2118 void
2119 stop_port(portid_t pid)
2120 {
2121 	portid_t pi;
2122 	struct rte_port *port;
2123 	int need_check_link_status = 0;
2124 
2125 	if (dcb_test) {
2126 		dcb_test = 0;
2127 		dcb_config = 0;
2128 	}
2129 
2130 	if (port_id_is_invalid(pid, ENABLED_WARN))
2131 		return;
2132 
2133 	printf("Stopping ports...\n");
2134 
2135 	RTE_ETH_FOREACH_DEV(pi) {
2136 		if (pid != pi && pid != (portid_t)RTE_PORT_ALL)
2137 			continue;
2138 
2139 		if (port_is_forwarding(pi) != 0 && test_done == 0) {
2140 			printf("Please remove port %d from forwarding configuration.\n", pi);
2141 			continue;
2142 		}
2143 
2144 		if (port_is_bonding_slave(pi)) {
2145 			printf("Please remove port %d from bonded device.\n", pi);
2146 			continue;
2147 		}
2148 
2149 		port = &ports[pi];
2150 		if (rte_atomic16_cmpset(&(port->port_status), RTE_PORT_STARTED,
2151 						RTE_PORT_HANDLING) == 0)
2152 			continue;
2153 
2154 		rte_eth_dev_stop(pi);
2155 
2156 		if (rte_atomic16_cmpset(&(port->port_status),
2157 			RTE_PORT_HANDLING, RTE_PORT_STOPPED) == 0)
2158 			printf("Port %d can not be set into stopped\n", pi);
2159 		need_check_link_status = 1;
2160 	}
2161 	if (need_check_link_status && !no_link_check)
2162 		check_all_ports_link_status(RTE_PORT_ALL);
2163 
2164 	printf("Done\n");
2165 }
2166 
2167 static void
2168 remove_invalid_ports_in(portid_t *array, portid_t *total)
2169 {
2170 	portid_t i;
2171 	portid_t new_total = 0;
2172 
2173 	for (i = 0; i < *total; i++)
2174 		if (!port_id_is_invalid(array[i], DISABLED_WARN)) {
2175 			array[new_total] = array[i];
2176 			new_total++;
2177 		}
2178 	*total = new_total;
2179 }
2180 
2181 static void
2182 remove_invalid_ports(void)
2183 {
2184 	remove_invalid_ports_in(ports_ids, &nb_ports);
2185 	remove_invalid_ports_in(fwd_ports_ids, &nb_fwd_ports);
2186 	nb_cfg_ports = nb_fwd_ports;
2187 }
2188 
2189 void
2190 close_port(portid_t pid)
2191 {
2192 	portid_t pi;
2193 	struct rte_port *port;
2194 
2195 	if (port_id_is_invalid(pid, ENABLED_WARN))
2196 		return;
2197 
2198 	printf("Closing ports...\n");
2199 
2200 	RTE_ETH_FOREACH_DEV(pi) {
2201 		if (pid != pi && pid != (portid_t)RTE_PORT_ALL)
2202 			continue;
2203 
2204 		if (port_is_forwarding(pi) != 0 && test_done == 0) {
2205 			printf("Please remove port %d from forwarding configuration.\n", pi);
2206 			continue;
2207 		}
2208 
2209 		if (port_is_bonding_slave(pi)) {
2210 			printf("Please remove port %d from bonded device.\n", pi);
2211 			continue;
2212 		}
2213 
2214 		port = &ports[pi];
2215 		if (rte_atomic16_cmpset(&(port->port_status),
2216 			RTE_PORT_CLOSED, RTE_PORT_CLOSED) == 1) {
2217 			printf("Port %d is already closed\n", pi);
2218 			continue;
2219 		}
2220 
2221 		if (rte_atomic16_cmpset(&(port->port_status),
2222 			RTE_PORT_STOPPED, RTE_PORT_HANDLING) == 0) {
2223 			printf("Port %d is now not stopped\n", pi);
2224 			continue;
2225 		}
2226 
2227 		if (port->flow_list)
2228 			port_flow_flush(pi);
2229 		rte_eth_dev_close(pi);
2230 
2231 		remove_invalid_ports();
2232 
2233 		if (rte_atomic16_cmpset(&(port->port_status),
2234 			RTE_PORT_HANDLING, RTE_PORT_CLOSED) == 0)
2235 			printf("Port %d cannot be set to closed\n", pi);
2236 	}
2237 
2238 	printf("Done\n");
2239 }
2240 
2241 void
2242 reset_port(portid_t pid)
2243 {
2244 	int diag;
2245 	portid_t pi;
2246 	struct rte_port *port;
2247 
2248 	if (port_id_is_invalid(pid, ENABLED_WARN))
2249 		return;
2250 
2251 	printf("Resetting ports...\n");
2252 
2253 	RTE_ETH_FOREACH_DEV(pi) {
2254 		if (pid != pi && pid != (portid_t)RTE_PORT_ALL)
2255 			continue;
2256 
2257 		if (port_is_forwarding(pi) != 0 && test_done == 0) {
2258 			printf("Please remove port %d from forwarding "
2259 			       "configuration.\n", pi);
2260 			continue;
2261 		}
2262 
2263 		if (port_is_bonding_slave(pi)) {
2264 			printf("Please remove port %d from bonded device.\n",
2265 			       pi);
2266 			continue;
2267 		}
2268 
2269 		diag = rte_eth_dev_reset(pi);
2270 		if (diag == 0) {
2271 			port = &ports[pi];
2272 			port->need_reconfig = 1;
2273 			port->need_reconfig_queues = 1;
2274 		} else {
2275 			printf("Failed to reset port %d. diag=%d\n", pi, diag);
2276 		}
2277 	}
2278 
2279 	printf("Done\n");
2280 }
2281 
2282 void
2283 attach_port(char *identifier)
2284 {
2285 	portid_t pi;
2286 	struct rte_dev_iterator iterator;
2287 
2288 	printf("Attaching a new port...\n");
2289 
2290 	if (identifier == NULL) {
2291 		printf("Invalid parameters are specified\n");
2292 		return;
2293 	}
2294 
2295 	if (rte_dev_probe(identifier) != 0) {
2296 		TESTPMD_LOG(ERR, "Failed to attach port %s\n", identifier);
2297 		return;
2298 	}
2299 
2300 	/* first attach mode: event */
2301 	if (setup_on_probe_event) {
2302 		/* new ports are detected on RTE_ETH_EVENT_NEW event */
2303 		for (pi = 0; pi < RTE_MAX_ETHPORTS; pi++)
2304 			if (ports[pi].port_status == RTE_PORT_HANDLING &&
2305 					ports[pi].need_setup != 0)
2306 				setup_attached_port(pi);
2307 		return;
2308 	}
2309 
2310 	/* second attach mode: iterator */
2311 	RTE_ETH_FOREACH_MATCHING_DEV(pi, identifier, &iterator) {
2312 		/* setup ports matching the devargs used for probing */
2313 		if (port_is_forwarding(pi))
2314 			continue; /* port was already attached before */
2315 		setup_attached_port(pi);
2316 	}
2317 }
2318 
2319 static void
2320 setup_attached_port(portid_t pi)
2321 {
2322 	unsigned int socket_id;
2323 
2324 	socket_id = (unsigned)rte_eth_dev_socket_id(pi);
2325 	/* if socket_id is invalid, set to the first available socket. */
2326 	if (check_socket_id(socket_id) < 0)
2327 		socket_id = socket_ids[0];
2328 	reconfig(pi, socket_id);
2329 	rte_eth_promiscuous_enable(pi);
2330 
2331 	ports_ids[nb_ports++] = pi;
2332 	fwd_ports_ids[nb_fwd_ports++] = pi;
2333 	nb_cfg_ports = nb_fwd_ports;
2334 	ports[pi].need_setup = 0;
2335 	ports[pi].port_status = RTE_PORT_STOPPED;
2336 
2337 	printf("Port %d is attached. Now total ports is %d\n", pi, nb_ports);
2338 	printf("Done\n");
2339 }
2340 
2341 void
2342 detach_port_device(portid_t port_id)
2343 {
2344 	struct rte_device *dev;
2345 	portid_t sibling;
2346 
2347 	printf("Removing a device...\n");
2348 
2349 	dev = rte_eth_devices[port_id].device;
2350 	if (dev == NULL) {
2351 		printf("Device already removed\n");
2352 		return;
2353 	}
2354 
2355 	if (ports[port_id].port_status != RTE_PORT_CLOSED) {
2356 		if (ports[port_id].port_status != RTE_PORT_STOPPED) {
2357 			printf("Port not stopped\n");
2358 			return;
2359 		}
2360 		printf("Port was not closed\n");
2361 		if (ports[port_id].flow_list)
2362 			port_flow_flush(port_id);
2363 	}
2364 
2365 	if (rte_dev_remove(dev) != 0) {
2366 		TESTPMD_LOG(ERR, "Failed to detach device %s\n", dev->name);
2367 		return;
2368 	}
2369 
2370 	for (sibling = 0; sibling < RTE_MAX_ETHPORTS; sibling++) {
2371 		if (rte_eth_devices[sibling].device != dev)
2372 			continue;
2373 		/* reset mapping between old ports and removed device */
2374 		rte_eth_devices[sibling].device = NULL;
2375 		if (ports[sibling].port_status != RTE_PORT_CLOSED) {
2376 			/* sibling ports are forced to be closed */
2377 			ports[sibling].port_status = RTE_PORT_CLOSED;
2378 			printf("Port %u is closed\n", sibling);
2379 		}
2380 	}
2381 
2382 	remove_invalid_ports();
2383 
2384 	printf("Device of port %u is detached\n", port_id);
2385 	printf("Now total ports is %d\n", nb_ports);
2386 	printf("Done\n");
2387 	return;
2388 }
2389 
2390 void
2391 pmd_test_exit(void)
2392 {
2393 	struct rte_device *device;
2394 	portid_t pt_id;
2395 	int ret;
2396 
2397 	if (test_done == 0)
2398 		stop_packet_forwarding();
2399 
2400 	if (ports != NULL) {
2401 		no_link_check = 1;
2402 		RTE_ETH_FOREACH_DEV(pt_id) {
2403 			printf("\nShutting down port %d...\n", pt_id);
2404 			fflush(stdout);
2405 			stop_port(pt_id);
2406 			close_port(pt_id);
2407 
2408 			/*
2409 			 * This is a workaround to fix a virtio-user issue that
2410 			 * requires to call clean-up routine to remove existing
2411 			 * socket.
2412 			 * This workaround valid only for testpmd, needs a fix
2413 			 * valid for all applications.
2414 			 * TODO: Implement proper resource cleanup
2415 			 */
2416 			device = rte_eth_devices[pt_id].device;
2417 			if (device && !strcmp(device->driver->name, "net_virtio_user"))
2418 				detach_port_device(pt_id);
2419 		}
2420 	}
2421 
2422 	if (hot_plug) {
2423 		ret = rte_dev_event_monitor_stop();
2424 		if (ret) {
2425 			RTE_LOG(ERR, EAL,
2426 				"fail to stop device event monitor.");
2427 			return;
2428 		}
2429 
2430 		ret = rte_dev_event_callback_unregister(NULL,
2431 			dev_event_callback, NULL);
2432 		if (ret < 0) {
2433 			RTE_LOG(ERR, EAL,
2434 				"fail to unregister device event callback.\n");
2435 			return;
2436 		}
2437 
2438 		ret = rte_dev_hotplug_handle_disable();
2439 		if (ret) {
2440 			RTE_LOG(ERR, EAL,
2441 				"fail to disable hotplug handling.\n");
2442 			return;
2443 		}
2444 	}
2445 
2446 	printf("\nBye...\n");
2447 }
2448 
2449 typedef void (*cmd_func_t)(void);
2450 struct pmd_test_command {
2451 	const char *cmd_name;
2452 	cmd_func_t cmd_func;
2453 };
2454 
2455 #define PMD_TEST_CMD_NB (sizeof(pmd_test_menu) / sizeof(pmd_test_menu[0]))
2456 
2457 /* Check the link status of all ports in up to 9s, and print them finally */
2458 static void
2459 check_all_ports_link_status(uint32_t port_mask)
2460 {
2461 #define CHECK_INTERVAL 100 /* 100ms */
2462 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
2463 	portid_t portid;
2464 	uint8_t count, all_ports_up, print_flag = 0;
2465 	struct rte_eth_link link;
2466 
2467 	printf("Checking link statuses...\n");
2468 	fflush(stdout);
2469 	for (count = 0; count <= MAX_CHECK_TIME; count++) {
2470 		all_ports_up = 1;
2471 		RTE_ETH_FOREACH_DEV(portid) {
2472 			if ((port_mask & (1 << portid)) == 0)
2473 				continue;
2474 			memset(&link, 0, sizeof(link));
2475 			rte_eth_link_get_nowait(portid, &link);
2476 			/* print link status if flag set */
2477 			if (print_flag == 1) {
2478 				if (link.link_status)
2479 					printf(
2480 					"Port%d Link Up. speed %u Mbps- %s\n",
2481 					portid, link.link_speed,
2482 				(link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
2483 					("full-duplex") : ("half-duplex\n"));
2484 				else
2485 					printf("Port %d Link Down\n", portid);
2486 				continue;
2487 			}
2488 			/* clear all_ports_up flag if any link down */
2489 			if (link.link_status == ETH_LINK_DOWN) {
2490 				all_ports_up = 0;
2491 				break;
2492 			}
2493 		}
2494 		/* after finally printing all link status, get out */
2495 		if (print_flag == 1)
2496 			break;
2497 
2498 		if (all_ports_up == 0) {
2499 			fflush(stdout);
2500 			rte_delay_ms(CHECK_INTERVAL);
2501 		}
2502 
2503 		/* set the print_flag if all ports up or timeout */
2504 		if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
2505 			print_flag = 1;
2506 		}
2507 
2508 		if (lsc_interrupt)
2509 			break;
2510 	}
2511 }
2512 
2513 /*
2514  * This callback is for remove a port for a device. It has limitation because
2515  * it is not for multiple port removal for a device.
2516  * TODO: the device detach invoke will plan to be removed from user side to
2517  * eal. And convert all PMDs to free port resources on ether device closing.
2518  */
2519 static void
2520 rmv_port_callback(void *arg)
2521 {
2522 	int need_to_start = 0;
2523 	int org_no_link_check = no_link_check;
2524 	portid_t port_id = (intptr_t)arg;
2525 
2526 	RTE_ETH_VALID_PORTID_OR_RET(port_id);
2527 
2528 	if (!test_done && port_is_forwarding(port_id)) {
2529 		need_to_start = 1;
2530 		stop_packet_forwarding();
2531 	}
2532 	no_link_check = 1;
2533 	stop_port(port_id);
2534 	no_link_check = org_no_link_check;
2535 	close_port(port_id);
2536 	detach_port_device(port_id);
2537 	if (need_to_start)
2538 		start_packet_forwarding(0);
2539 }
2540 
2541 /* This function is used by the interrupt thread */
2542 static int
2543 eth_event_callback(portid_t port_id, enum rte_eth_event_type type, void *param,
2544 		  void *ret_param)
2545 {
2546 	RTE_SET_USED(param);
2547 	RTE_SET_USED(ret_param);
2548 
2549 	if (type >= RTE_ETH_EVENT_MAX) {
2550 		fprintf(stderr, "\nPort %" PRIu16 ": %s called upon invalid event %d\n",
2551 			port_id, __func__, type);
2552 		fflush(stderr);
2553 	} else if (event_print_mask & (UINT32_C(1) << type)) {
2554 		printf("\nPort %" PRIu16 ": %s event\n", port_id,
2555 			eth_event_desc[type]);
2556 		fflush(stdout);
2557 	}
2558 
2559 	switch (type) {
2560 	case RTE_ETH_EVENT_NEW:
2561 		ports[port_id].need_setup = 1;
2562 		ports[port_id].port_status = RTE_PORT_HANDLING;
2563 		break;
2564 	case RTE_ETH_EVENT_INTR_RMV:
2565 		if (port_id_is_invalid(port_id, DISABLED_WARN))
2566 			break;
2567 		if (rte_eal_alarm_set(100000,
2568 				rmv_port_callback, (void *)(intptr_t)port_id))
2569 			fprintf(stderr, "Could not set up deferred device removal\n");
2570 		break;
2571 	default:
2572 		break;
2573 	}
2574 	return 0;
2575 }
2576 
2577 static int
2578 register_eth_event_callback(void)
2579 {
2580 	int ret;
2581 	enum rte_eth_event_type event;
2582 
2583 	for (event = RTE_ETH_EVENT_UNKNOWN;
2584 			event < RTE_ETH_EVENT_MAX; event++) {
2585 		ret = rte_eth_dev_callback_register(RTE_ETH_ALL,
2586 				event,
2587 				eth_event_callback,
2588 				NULL);
2589 		if (ret != 0) {
2590 			TESTPMD_LOG(ERR, "Failed to register callback for "
2591 					"%s event\n", eth_event_desc[event]);
2592 			return -1;
2593 		}
2594 	}
2595 
2596 	return 0;
2597 }
2598 
2599 /* This function is used by the interrupt thread */
2600 static void
2601 dev_event_callback(const char *device_name, enum rte_dev_event_type type,
2602 			     __rte_unused void *arg)
2603 {
2604 	uint16_t port_id;
2605 	int ret;
2606 
2607 	if (type >= RTE_DEV_EVENT_MAX) {
2608 		fprintf(stderr, "%s called upon invalid event %d\n",
2609 			__func__, type);
2610 		fflush(stderr);
2611 	}
2612 
2613 	switch (type) {
2614 	case RTE_DEV_EVENT_REMOVE:
2615 		RTE_LOG(DEBUG, EAL, "The device: %s has been removed!\n",
2616 			device_name);
2617 		ret = rte_eth_dev_get_port_by_name(device_name, &port_id);
2618 		if (ret) {
2619 			RTE_LOG(ERR, EAL, "can not get port by device %s!\n",
2620 				device_name);
2621 			return;
2622 		}
2623 		/*
2624 		 * Because the user's callback is invoked in eal interrupt
2625 		 * callback, the interrupt callback need to be finished before
2626 		 * it can be unregistered when detaching device. So finish
2627 		 * callback soon and use a deferred removal to detach device
2628 		 * is need. It is a workaround, once the device detaching be
2629 		 * moved into the eal in the future, the deferred removal could
2630 		 * be deleted.
2631 		 */
2632 		if (rte_eal_alarm_set(100000,
2633 				rmv_port_callback, (void *)(intptr_t)port_id))
2634 			RTE_LOG(ERR, EAL,
2635 				"Could not set up deferred device removal\n");
2636 		break;
2637 	case RTE_DEV_EVENT_ADD:
2638 		RTE_LOG(ERR, EAL, "The device: %s has been added!\n",
2639 			device_name);
2640 		/* TODO: After finish kernel driver binding,
2641 		 * begin to attach port.
2642 		 */
2643 		break;
2644 	default:
2645 		break;
2646 	}
2647 }
2648 
2649 static int
2650 set_tx_queue_stats_mapping_registers(portid_t port_id, struct rte_port *port)
2651 {
2652 	uint16_t i;
2653 	int diag;
2654 	uint8_t mapping_found = 0;
2655 
2656 	for (i = 0; i < nb_tx_queue_stats_mappings; i++) {
2657 		if ((tx_queue_stats_mappings[i].port_id == port_id) &&
2658 				(tx_queue_stats_mappings[i].queue_id < nb_txq )) {
2659 			diag = rte_eth_dev_set_tx_queue_stats_mapping(port_id,
2660 					tx_queue_stats_mappings[i].queue_id,
2661 					tx_queue_stats_mappings[i].stats_counter_id);
2662 			if (diag != 0)
2663 				return diag;
2664 			mapping_found = 1;
2665 		}
2666 	}
2667 	if (mapping_found)
2668 		port->tx_queue_stats_mapping_enabled = 1;
2669 	return 0;
2670 }
2671 
2672 static int
2673 set_rx_queue_stats_mapping_registers(portid_t port_id, struct rte_port *port)
2674 {
2675 	uint16_t i;
2676 	int diag;
2677 	uint8_t mapping_found = 0;
2678 
2679 	for (i = 0; i < nb_rx_queue_stats_mappings; i++) {
2680 		if ((rx_queue_stats_mappings[i].port_id == port_id) &&
2681 				(rx_queue_stats_mappings[i].queue_id < nb_rxq )) {
2682 			diag = rte_eth_dev_set_rx_queue_stats_mapping(port_id,
2683 					rx_queue_stats_mappings[i].queue_id,
2684 					rx_queue_stats_mappings[i].stats_counter_id);
2685 			if (diag != 0)
2686 				return diag;
2687 			mapping_found = 1;
2688 		}
2689 	}
2690 	if (mapping_found)
2691 		port->rx_queue_stats_mapping_enabled = 1;
2692 	return 0;
2693 }
2694 
2695 static void
2696 map_port_queue_stats_mapping_registers(portid_t pi, struct rte_port *port)
2697 {
2698 	int diag = 0;
2699 
2700 	diag = set_tx_queue_stats_mapping_registers(pi, port);
2701 	if (diag != 0) {
2702 		if (diag == -ENOTSUP) {
2703 			port->tx_queue_stats_mapping_enabled = 0;
2704 			printf("TX queue stats mapping not supported port id=%d\n", pi);
2705 		}
2706 		else
2707 			rte_exit(EXIT_FAILURE,
2708 					"set_tx_queue_stats_mapping_registers "
2709 					"failed for port id=%d diag=%d\n",
2710 					pi, diag);
2711 	}
2712 
2713 	diag = set_rx_queue_stats_mapping_registers(pi, port);
2714 	if (diag != 0) {
2715 		if (diag == -ENOTSUP) {
2716 			port->rx_queue_stats_mapping_enabled = 0;
2717 			printf("RX queue stats mapping not supported port id=%d\n", pi);
2718 		}
2719 		else
2720 			rte_exit(EXIT_FAILURE,
2721 					"set_rx_queue_stats_mapping_registers "
2722 					"failed for port id=%d diag=%d\n",
2723 					pi, diag);
2724 	}
2725 }
2726 
2727 static void
2728 rxtx_port_config(struct rte_port *port)
2729 {
2730 	uint16_t qid;
2731 
2732 	for (qid = 0; qid < nb_rxq; qid++) {
2733 		port->rx_conf[qid] = port->dev_info.default_rxconf;
2734 
2735 		/* Check if any Rx parameters have been passed */
2736 		if (rx_pthresh != RTE_PMD_PARAM_UNSET)
2737 			port->rx_conf[qid].rx_thresh.pthresh = rx_pthresh;
2738 
2739 		if (rx_hthresh != RTE_PMD_PARAM_UNSET)
2740 			port->rx_conf[qid].rx_thresh.hthresh = rx_hthresh;
2741 
2742 		if (rx_wthresh != RTE_PMD_PARAM_UNSET)
2743 			port->rx_conf[qid].rx_thresh.wthresh = rx_wthresh;
2744 
2745 		if (rx_free_thresh != RTE_PMD_PARAM_UNSET)
2746 			port->rx_conf[qid].rx_free_thresh = rx_free_thresh;
2747 
2748 		if (rx_drop_en != RTE_PMD_PARAM_UNSET)
2749 			port->rx_conf[qid].rx_drop_en = rx_drop_en;
2750 
2751 		port->nb_rx_desc[qid] = nb_rxd;
2752 	}
2753 
2754 	for (qid = 0; qid < nb_txq; qid++) {
2755 		port->tx_conf[qid] = port->dev_info.default_txconf;
2756 
2757 		/* Check if any Tx parameters have been passed */
2758 		if (tx_pthresh != RTE_PMD_PARAM_UNSET)
2759 			port->tx_conf[qid].tx_thresh.pthresh = tx_pthresh;
2760 
2761 		if (tx_hthresh != RTE_PMD_PARAM_UNSET)
2762 			port->tx_conf[qid].tx_thresh.hthresh = tx_hthresh;
2763 
2764 		if (tx_wthresh != RTE_PMD_PARAM_UNSET)
2765 			port->tx_conf[qid].tx_thresh.wthresh = tx_wthresh;
2766 
2767 		if (tx_rs_thresh != RTE_PMD_PARAM_UNSET)
2768 			port->tx_conf[qid].tx_rs_thresh = tx_rs_thresh;
2769 
2770 		if (tx_free_thresh != RTE_PMD_PARAM_UNSET)
2771 			port->tx_conf[qid].tx_free_thresh = tx_free_thresh;
2772 
2773 		port->nb_tx_desc[qid] = nb_txd;
2774 	}
2775 }
2776 
2777 void
2778 init_port_config(void)
2779 {
2780 	portid_t pid;
2781 	struct rte_port *port;
2782 
2783 	RTE_ETH_FOREACH_DEV(pid) {
2784 		port = &ports[pid];
2785 		port->dev_conf.fdir_conf = fdir_conf;
2786 		rte_eth_dev_info_get(pid, &port->dev_info);
2787 		if (nb_rxq > 1) {
2788 			port->dev_conf.rx_adv_conf.rss_conf.rss_key = NULL;
2789 			port->dev_conf.rx_adv_conf.rss_conf.rss_hf =
2790 				rss_hf & port->dev_info.flow_type_rss_offloads;
2791 		} else {
2792 			port->dev_conf.rx_adv_conf.rss_conf.rss_key = NULL;
2793 			port->dev_conf.rx_adv_conf.rss_conf.rss_hf = 0;
2794 		}
2795 
2796 		if (port->dcb_flag == 0) {
2797 			if( port->dev_conf.rx_adv_conf.rss_conf.rss_hf != 0)
2798 				port->dev_conf.rxmode.mq_mode = ETH_MQ_RX_RSS;
2799 			else
2800 				port->dev_conf.rxmode.mq_mode = ETH_MQ_RX_NONE;
2801 		}
2802 
2803 		rxtx_port_config(port);
2804 
2805 		rte_eth_macaddr_get(pid, &port->eth_addr);
2806 
2807 		map_port_queue_stats_mapping_registers(pid, port);
2808 #if defined RTE_LIBRTE_IXGBE_PMD && defined RTE_LIBRTE_IXGBE_BYPASS
2809 		rte_pmd_ixgbe_bypass_init(pid);
2810 #endif
2811 
2812 		if (lsc_interrupt &&
2813 		    (rte_eth_devices[pid].data->dev_flags &
2814 		     RTE_ETH_DEV_INTR_LSC))
2815 			port->dev_conf.intr_conf.lsc = 1;
2816 		if (rmv_interrupt &&
2817 		    (rte_eth_devices[pid].data->dev_flags &
2818 		     RTE_ETH_DEV_INTR_RMV))
2819 			port->dev_conf.intr_conf.rmv = 1;
2820 	}
2821 }
2822 
2823 void set_port_slave_flag(portid_t slave_pid)
2824 {
2825 	struct rte_port *port;
2826 
2827 	port = &ports[slave_pid];
2828 	port->slave_flag = 1;
2829 }
2830 
2831 void clear_port_slave_flag(portid_t slave_pid)
2832 {
2833 	struct rte_port *port;
2834 
2835 	port = &ports[slave_pid];
2836 	port->slave_flag = 0;
2837 }
2838 
2839 uint8_t port_is_bonding_slave(portid_t slave_pid)
2840 {
2841 	struct rte_port *port;
2842 
2843 	port = &ports[slave_pid];
2844 	if ((rte_eth_devices[slave_pid].data->dev_flags &
2845 	    RTE_ETH_DEV_BONDED_SLAVE) || (port->slave_flag == 1))
2846 		return 1;
2847 	return 0;
2848 }
2849 
2850 const uint16_t vlan_tags[] = {
2851 		0,  1,  2,  3,  4,  5,  6,  7,
2852 		8,  9, 10, 11,  12, 13, 14, 15,
2853 		16, 17, 18, 19, 20, 21, 22, 23,
2854 		24, 25, 26, 27, 28, 29, 30, 31
2855 };
2856 
2857 static  int
2858 get_eth_dcb_conf(portid_t pid, struct rte_eth_conf *eth_conf,
2859 		 enum dcb_mode_enable dcb_mode,
2860 		 enum rte_eth_nb_tcs num_tcs,
2861 		 uint8_t pfc_en)
2862 {
2863 	uint8_t i;
2864 	int32_t rc;
2865 	struct rte_eth_rss_conf rss_conf;
2866 
2867 	/*
2868 	 * Builds up the correct configuration for dcb+vt based on the vlan tags array
2869 	 * given above, and the number of traffic classes available for use.
2870 	 */
2871 	if (dcb_mode == DCB_VT_ENABLED) {
2872 		struct rte_eth_vmdq_dcb_conf *vmdq_rx_conf =
2873 				&eth_conf->rx_adv_conf.vmdq_dcb_conf;
2874 		struct rte_eth_vmdq_dcb_tx_conf *vmdq_tx_conf =
2875 				&eth_conf->tx_adv_conf.vmdq_dcb_tx_conf;
2876 
2877 		/* VMDQ+DCB RX and TX configurations */
2878 		vmdq_rx_conf->enable_default_pool = 0;
2879 		vmdq_rx_conf->default_pool = 0;
2880 		vmdq_rx_conf->nb_queue_pools =
2881 			(num_tcs ==  ETH_4_TCS ? ETH_32_POOLS : ETH_16_POOLS);
2882 		vmdq_tx_conf->nb_queue_pools =
2883 			(num_tcs ==  ETH_4_TCS ? ETH_32_POOLS : ETH_16_POOLS);
2884 
2885 		vmdq_rx_conf->nb_pool_maps = vmdq_rx_conf->nb_queue_pools;
2886 		for (i = 0; i < vmdq_rx_conf->nb_pool_maps; i++) {
2887 			vmdq_rx_conf->pool_map[i].vlan_id = vlan_tags[i];
2888 			vmdq_rx_conf->pool_map[i].pools =
2889 				1 << (i % vmdq_rx_conf->nb_queue_pools);
2890 		}
2891 		for (i = 0; i < ETH_DCB_NUM_USER_PRIORITIES; i++) {
2892 			vmdq_rx_conf->dcb_tc[i] = i % num_tcs;
2893 			vmdq_tx_conf->dcb_tc[i] = i % num_tcs;
2894 		}
2895 
2896 		/* set DCB mode of RX and TX of multiple queues */
2897 		eth_conf->rxmode.mq_mode = ETH_MQ_RX_VMDQ_DCB;
2898 		eth_conf->txmode.mq_mode = ETH_MQ_TX_VMDQ_DCB;
2899 	} else {
2900 		struct rte_eth_dcb_rx_conf *rx_conf =
2901 				&eth_conf->rx_adv_conf.dcb_rx_conf;
2902 		struct rte_eth_dcb_tx_conf *tx_conf =
2903 				&eth_conf->tx_adv_conf.dcb_tx_conf;
2904 
2905 		rc = rte_eth_dev_rss_hash_conf_get(pid, &rss_conf);
2906 		if (rc != 0)
2907 			return rc;
2908 
2909 		rx_conf->nb_tcs = num_tcs;
2910 		tx_conf->nb_tcs = num_tcs;
2911 
2912 		for (i = 0; i < ETH_DCB_NUM_USER_PRIORITIES; i++) {
2913 			rx_conf->dcb_tc[i] = i % num_tcs;
2914 			tx_conf->dcb_tc[i] = i % num_tcs;
2915 		}
2916 
2917 		eth_conf->rxmode.mq_mode = ETH_MQ_RX_DCB_RSS;
2918 		eth_conf->rx_adv_conf.rss_conf = rss_conf;
2919 		eth_conf->txmode.mq_mode = ETH_MQ_TX_DCB;
2920 	}
2921 
2922 	if (pfc_en)
2923 		eth_conf->dcb_capability_en =
2924 				ETH_DCB_PG_SUPPORT | ETH_DCB_PFC_SUPPORT;
2925 	else
2926 		eth_conf->dcb_capability_en = ETH_DCB_PG_SUPPORT;
2927 
2928 	return 0;
2929 }
2930 
2931 int
2932 init_port_dcb_config(portid_t pid,
2933 		     enum dcb_mode_enable dcb_mode,
2934 		     enum rte_eth_nb_tcs num_tcs,
2935 		     uint8_t pfc_en)
2936 {
2937 	struct rte_eth_conf port_conf;
2938 	struct rte_port *rte_port;
2939 	int retval;
2940 	uint16_t i;
2941 
2942 	rte_port = &ports[pid];
2943 
2944 	memset(&port_conf, 0, sizeof(struct rte_eth_conf));
2945 	/* Enter DCB configuration status */
2946 	dcb_config = 1;
2947 
2948 	port_conf.rxmode = rte_port->dev_conf.rxmode;
2949 	port_conf.txmode = rte_port->dev_conf.txmode;
2950 
2951 	/*set configuration of DCB in vt mode and DCB in non-vt mode*/
2952 	retval = get_eth_dcb_conf(pid, &port_conf, dcb_mode, num_tcs, pfc_en);
2953 	if (retval < 0)
2954 		return retval;
2955 	port_conf.rxmode.offloads |= DEV_RX_OFFLOAD_VLAN_FILTER;
2956 
2957 	/* re-configure the device . */
2958 	rte_eth_dev_configure(pid, nb_rxq, nb_rxq, &port_conf);
2959 
2960 	rte_eth_dev_info_get(pid, &rte_port->dev_info);
2961 
2962 	/* If dev_info.vmdq_pool_base is greater than 0,
2963 	 * the queue id of vmdq pools is started after pf queues.
2964 	 */
2965 	if (dcb_mode == DCB_VT_ENABLED &&
2966 	    rte_port->dev_info.vmdq_pool_base > 0) {
2967 		printf("VMDQ_DCB multi-queue mode is nonsensical"
2968 			" for port %d.", pid);
2969 		return -1;
2970 	}
2971 
2972 	/* Assume the ports in testpmd have the same dcb capability
2973 	 * and has the same number of rxq and txq in dcb mode
2974 	 */
2975 	if (dcb_mode == DCB_VT_ENABLED) {
2976 		if (rte_port->dev_info.max_vfs > 0) {
2977 			nb_rxq = rte_port->dev_info.nb_rx_queues;
2978 			nb_txq = rte_port->dev_info.nb_tx_queues;
2979 		} else {
2980 			nb_rxq = rte_port->dev_info.max_rx_queues;
2981 			nb_txq = rte_port->dev_info.max_tx_queues;
2982 		}
2983 	} else {
2984 		/*if vt is disabled, use all pf queues */
2985 		if (rte_port->dev_info.vmdq_pool_base == 0) {
2986 			nb_rxq = rte_port->dev_info.max_rx_queues;
2987 			nb_txq = rte_port->dev_info.max_tx_queues;
2988 		} else {
2989 			nb_rxq = (queueid_t)num_tcs;
2990 			nb_txq = (queueid_t)num_tcs;
2991 
2992 		}
2993 	}
2994 	rx_free_thresh = 64;
2995 
2996 	memcpy(&rte_port->dev_conf, &port_conf, sizeof(struct rte_eth_conf));
2997 
2998 	rxtx_port_config(rte_port);
2999 	/* VLAN filter */
3000 	rte_port->dev_conf.rxmode.offloads |= DEV_RX_OFFLOAD_VLAN_FILTER;
3001 	for (i = 0; i < RTE_DIM(vlan_tags); i++)
3002 		rx_vft_set(pid, vlan_tags[i], 1);
3003 
3004 	rte_eth_macaddr_get(pid, &rte_port->eth_addr);
3005 	map_port_queue_stats_mapping_registers(pid, rte_port);
3006 
3007 	rte_port->dcb_flag = 1;
3008 
3009 	return 0;
3010 }
3011 
3012 static void
3013 init_port(void)
3014 {
3015 	/* Configuration of Ethernet ports. */
3016 	ports = rte_zmalloc("testpmd: ports",
3017 			    sizeof(struct rte_port) * RTE_MAX_ETHPORTS,
3018 			    RTE_CACHE_LINE_SIZE);
3019 	if (ports == NULL) {
3020 		rte_exit(EXIT_FAILURE,
3021 				"rte_zmalloc(%d struct rte_port) failed\n",
3022 				RTE_MAX_ETHPORTS);
3023 	}
3024 
3025 	/* Initialize ports NUMA structures */
3026 	memset(port_numa, NUMA_NO_CONFIG, RTE_MAX_ETHPORTS);
3027 	memset(rxring_numa, NUMA_NO_CONFIG, RTE_MAX_ETHPORTS);
3028 	memset(txring_numa, NUMA_NO_CONFIG, RTE_MAX_ETHPORTS);
3029 }
3030 
3031 static void
3032 force_quit(void)
3033 {
3034 	pmd_test_exit();
3035 	prompt_exit();
3036 }
3037 
3038 static void
3039 print_stats(void)
3040 {
3041 	uint8_t i;
3042 	const char clr[] = { 27, '[', '2', 'J', '\0' };
3043 	const char top_left[] = { 27, '[', '1', ';', '1', 'H', '\0' };
3044 
3045 	/* Clear screen and move to top left */
3046 	printf("%s%s", clr, top_left);
3047 
3048 	printf("\nPort statistics ====================================");
3049 	for (i = 0; i < cur_fwd_config.nb_fwd_ports; i++)
3050 		nic_stats_display(fwd_ports_ids[i]);
3051 }
3052 
3053 static void
3054 signal_handler(int signum)
3055 {
3056 	if (signum == SIGINT || signum == SIGTERM) {
3057 		printf("\nSignal %d received, preparing to exit...\n",
3058 				signum);
3059 #ifdef RTE_LIBRTE_PDUMP
3060 		/* uninitialize packet capture framework */
3061 		rte_pdump_uninit();
3062 #endif
3063 #ifdef RTE_LIBRTE_LATENCY_STATS
3064 		rte_latencystats_uninit();
3065 #endif
3066 		force_quit();
3067 		/* Set flag to indicate the force termination. */
3068 		f_quit = 1;
3069 		/* exit with the expected status */
3070 		signal(signum, SIG_DFL);
3071 		kill(getpid(), signum);
3072 	}
3073 }
3074 
3075 int
3076 main(int argc, char** argv)
3077 {
3078 	int diag;
3079 	portid_t port_id;
3080 	uint16_t count;
3081 	int ret;
3082 
3083 	signal(SIGINT, signal_handler);
3084 	signal(SIGTERM, signal_handler);
3085 
3086 	diag = rte_eal_init(argc, argv);
3087 	if (diag < 0)
3088 		rte_panic("Cannot init EAL\n");
3089 
3090 	testpmd_logtype = rte_log_register("testpmd");
3091 	if (testpmd_logtype < 0)
3092 		rte_panic("Cannot register log type");
3093 	rte_log_set_level(testpmd_logtype, RTE_LOG_DEBUG);
3094 
3095 	ret = register_eth_event_callback();
3096 	if (ret != 0)
3097 		rte_panic("Cannot register for ethdev events");
3098 
3099 #ifdef RTE_LIBRTE_PDUMP
3100 	/* initialize packet capture framework */
3101 	rte_pdump_init();
3102 #endif
3103 
3104 	count = 0;
3105 	RTE_ETH_FOREACH_DEV(port_id) {
3106 		ports_ids[count] = port_id;
3107 		count++;
3108 	}
3109 	nb_ports = (portid_t) count;
3110 	if (nb_ports == 0)
3111 		TESTPMD_LOG(WARNING, "No probed ethernet devices\n");
3112 
3113 	/* allocate port structures, and init them */
3114 	init_port();
3115 
3116 	set_def_fwd_config();
3117 	if (nb_lcores == 0)
3118 		rte_panic("Empty set of forwarding logical cores - check the "
3119 			  "core mask supplied in the command parameters\n");
3120 
3121 	/* Bitrate/latency stats disabled by default */
3122 #ifdef RTE_LIBRTE_BITRATE
3123 	bitrate_enabled = 0;
3124 #endif
3125 #ifdef RTE_LIBRTE_LATENCY_STATS
3126 	latencystats_enabled = 0;
3127 #endif
3128 
3129 	/* on FreeBSD, mlockall() is disabled by default */
3130 #ifdef RTE_EXEC_ENV_BSDAPP
3131 	do_mlockall = 0;
3132 #else
3133 	do_mlockall = 1;
3134 #endif
3135 
3136 	argc -= diag;
3137 	argv += diag;
3138 	if (argc > 1)
3139 		launch_args_parse(argc, argv);
3140 
3141 	if (do_mlockall && mlockall(MCL_CURRENT | MCL_FUTURE)) {
3142 		TESTPMD_LOG(NOTICE, "mlockall() failed with error \"%s\"\n",
3143 			strerror(errno));
3144 	}
3145 
3146 	if (tx_first && interactive)
3147 		rte_exit(EXIT_FAILURE, "--tx-first cannot be used on "
3148 				"interactive mode.\n");
3149 
3150 	if (tx_first && lsc_interrupt) {
3151 		printf("Warning: lsc_interrupt needs to be off when "
3152 				" using tx_first. Disabling.\n");
3153 		lsc_interrupt = 0;
3154 	}
3155 
3156 	if (!nb_rxq && !nb_txq)
3157 		printf("Warning: Either rx or tx queues should be non-zero\n");
3158 
3159 	if (nb_rxq > 1 && nb_rxq > nb_txq)
3160 		printf("Warning: nb_rxq=%d enables RSS configuration, "
3161 		       "but nb_txq=%d will prevent to fully test it.\n",
3162 		       nb_rxq, nb_txq);
3163 
3164 	init_config();
3165 
3166 	if (hot_plug) {
3167 		ret = rte_dev_hotplug_handle_enable();
3168 		if (ret) {
3169 			RTE_LOG(ERR, EAL,
3170 				"fail to enable hotplug handling.");
3171 			return -1;
3172 		}
3173 
3174 		ret = rte_dev_event_monitor_start();
3175 		if (ret) {
3176 			RTE_LOG(ERR, EAL,
3177 				"fail to start device event monitoring.");
3178 			return -1;
3179 		}
3180 
3181 		ret = rte_dev_event_callback_register(NULL,
3182 			dev_event_callback, NULL);
3183 		if (ret) {
3184 			RTE_LOG(ERR, EAL,
3185 				"fail  to register device event callback\n");
3186 			return -1;
3187 		}
3188 	}
3189 
3190 	if (start_port(RTE_PORT_ALL) != 0)
3191 		rte_exit(EXIT_FAILURE, "Start ports failed\n");
3192 
3193 	/* set all ports to promiscuous mode by default */
3194 	RTE_ETH_FOREACH_DEV(port_id)
3195 		rte_eth_promiscuous_enable(port_id);
3196 
3197 	/* Init metrics library */
3198 	rte_metrics_init(rte_socket_id());
3199 
3200 #ifdef RTE_LIBRTE_LATENCY_STATS
3201 	if (latencystats_enabled != 0) {
3202 		int ret = rte_latencystats_init(1, NULL);
3203 		if (ret)
3204 			printf("Warning: latencystats init()"
3205 				" returned error %d\n",	ret);
3206 		printf("Latencystats running on lcore %d\n",
3207 			latencystats_lcore_id);
3208 	}
3209 #endif
3210 
3211 	/* Setup bitrate stats */
3212 #ifdef RTE_LIBRTE_BITRATE
3213 	if (bitrate_enabled != 0) {
3214 		bitrate_data = rte_stats_bitrate_create();
3215 		if (bitrate_data == NULL)
3216 			rte_exit(EXIT_FAILURE,
3217 				"Could not allocate bitrate data.\n");
3218 		rte_stats_bitrate_reg(bitrate_data);
3219 	}
3220 #endif
3221 
3222 #ifdef RTE_LIBRTE_CMDLINE
3223 	if (strlen(cmdline_filename) != 0)
3224 		cmdline_read_from_file(cmdline_filename);
3225 
3226 	if (interactive == 1) {
3227 		if (auto_start) {
3228 			printf("Start automatic packet forwarding\n");
3229 			start_packet_forwarding(0);
3230 		}
3231 		prompt();
3232 		pmd_test_exit();
3233 	} else
3234 #endif
3235 	{
3236 		char c;
3237 		int rc;
3238 
3239 		f_quit = 0;
3240 
3241 		printf("No commandline core given, start packet forwarding\n");
3242 		start_packet_forwarding(tx_first);
3243 		if (stats_period != 0) {
3244 			uint64_t prev_time = 0, cur_time, diff_time = 0;
3245 			uint64_t timer_period;
3246 
3247 			/* Convert to number of cycles */
3248 			timer_period = stats_period * rte_get_timer_hz();
3249 
3250 			while (f_quit == 0) {
3251 				cur_time = rte_get_timer_cycles();
3252 				diff_time += cur_time - prev_time;
3253 
3254 				if (diff_time >= timer_period) {
3255 					print_stats();
3256 					/* Reset the timer */
3257 					diff_time = 0;
3258 				}
3259 				/* Sleep to avoid unnecessary checks */
3260 				prev_time = cur_time;
3261 				sleep(1);
3262 			}
3263 		}
3264 
3265 		printf("Press enter to exit\n");
3266 		rc = read(0, &c, 1);
3267 		pmd_test_exit();
3268 		if (rc < 0)
3269 			return 1;
3270 	}
3271 
3272 	return 0;
3273 }
3274