1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2017 Intel Corporation
13 #include <sys/types.h>
17 #include <sys/queue.h>
24 #include <rte_common.h>
25 #include <rte_errno.h>
26 #include <rte_byteorder.h>
28 #include <rte_debug.h>
29 #include <rte_cycles.h>
30 #include <rte_memory.h>
31 #include <rte_memcpy.h>
32 #include <rte_launch.h>
34 #include <rte_alarm.h>
35 #include <rte_per_lcore.h>
36 #include <rte_lcore.h>
37 #include <rte_atomic.h>
38 #include <rte_branch_prediction.h>
39 #include <rte_mempool.h>
40 #include <rte_malloc.h>
42 #include <rte_mbuf_pool_ops.h>
43 #include <rte_interrupts.h>
45 #include <rte_ether.h>
46 #include <rte_ethdev.h>
48 #include <rte_string_fns.h>
49 #ifdef RTE_LIBRTE_IXGBE_PMD
50 #include <rte_pmd_ixgbe.h>
52 #ifdef RTE_LIBRTE_PDUMP
53 #include <rte_pdump.h>
56 #include <rte_metrics.h>
57 #ifdef RTE_LIBRTE_BITRATESTATS
58 #include <rte_bitrate.h>
60 #ifdef RTE_LIBRTE_LATENCY_STATS
61 #include <rte_latencystats.h>
67 /* FreeBSD may not have MAP_HUGETLB (in fact, it probably doesn't) */
68 #define HUGE_FLAG (0x40000)
70 #define HUGE_FLAG MAP_HUGETLB
73 #ifndef MAP_HUGE_SHIFT
74 /* older kernels (or FreeBSD) will not have this define */
75 #define HUGE_SHIFT (26)
77 #define HUGE_SHIFT MAP_HUGE_SHIFT
80 #define EXTMEM_HEAP_NAME "extmem"
81 #define EXTBUF_ZONE_SIZE RTE_PGSIZE_2M
83 uint16_t verbose_level = 0; /**< Silent by default. */
84 int testpmd_logtype; /**< Log type for testpmd logs */
86 /* use master core for command line ? */
87 uint8_t interactive = 0;
88 uint8_t auto_start = 0;
90 char cmdline_filename[PATH_MAX] = {0};
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.
99 uint8_t numa_support = 1; /**< numa enabled by default */
102 * In UMA mode,all memory is allocated from socket 0 if --socket-num is
105 uint8_t socket_num = UMA_NO_CONFIG;
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
114 uint8_t mp_alloc_type = MP_ALLOC_NATIVE;
117 * Store specified sockets on which memory pool to be used by ports
120 uint8_t port_numa[RTE_MAX_ETHPORTS];
123 * Store specified sockets on which RX ring to be used by ports
126 uint8_t rxring_numa[RTE_MAX_ETHPORTS];
129 * Store specified sockets on which TX ring to be used by ports
132 uint8_t txring_numa[RTE_MAX_ETHPORTS];
135 * Record the Ethernet address of peer target ports to which packets are
137 * Must be instantiated with the ethernet addresses of peer traffic generator
140 struct rte_ether_addr peer_eth_addrs[RTE_MAX_ETHPORTS];
141 portid_t nb_peer_eth_addrs = 0;
144 * Probed Target Environment.
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. */
151 portid_t ports_ids[RTE_MAX_ETHPORTS]; /**< Store all port ids. */
154 * Test Forwarding Configuration.
155 * nb_fwd_lcores <= nb_cfg_lcores <= nb_lcores
156 * nb_fwd_ports <= nb_cfg_ports <= nb_ports
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. */
163 unsigned int fwd_lcores_cpuids[RTE_MAX_LCORE]; /**< CPU ids configuration. */
164 portid_t fwd_ports_ids[RTE_MAX_ETHPORTS]; /**< Port ids configuration. */
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). */
170 * Forwarding engines.
172 struct fwd_engine * fwd_engines[] = {
182 &five_tuple_swap_fwd_engine,
183 #ifdef RTE_LIBRTE_IEEE1588
184 &ieee1588_fwd_engine,
189 struct rte_mempool *mempools[RTE_MAX_NUMA_NODES * MAX_SEGS_BUFFER_SPLIT];
190 uint16_t mempool_flags;
192 struct fwd_config cur_fwd_config;
193 struct fwd_engine *cur_fwd_eng = &io_fwd_engine; /**< IO mode by default. */
194 uint32_t retry_enabled;
195 uint32_t burst_tx_delay_time = BURST_TX_WAIT_US;
196 uint32_t burst_tx_retry_num = BURST_TX_RETRIES;
198 uint32_t mbuf_data_size_n = 1; /* Number of specified mbuf sizes. */
199 uint16_t mbuf_data_size[MAX_SEGS_BUFFER_SPLIT] = {
200 DEFAULT_MBUF_DATA_SIZE
201 }; /**< Mbuf data space size. */
202 uint32_t param_total_num_mbufs = 0; /**< number of mbufs in all pools - if
203 * specified on command-line. */
204 uint16_t stats_period; /**< Period to show statistics (disabled by default) */
207 * In container, it cannot terminate the process which running with 'stats-period'
208 * option. Set flag to exit stats period loop after received SIGINT/SIGTERM.
213 * Configuration of packet segments used to scatter received packets
214 * if some of split features is configured.
216 uint16_t rx_pkt_seg_lengths[MAX_SEGS_BUFFER_SPLIT];
217 uint8_t rx_pkt_nb_segs; /**< Number of segments to split */
220 * Configuration of packet segments used by the "txonly" processing engine.
222 uint16_t tx_pkt_length = TXONLY_DEF_PACKET_LEN; /**< TXONLY packet length. */
223 uint16_t tx_pkt_seg_lengths[RTE_MAX_SEGS_PER_PKT] = {
224 TXONLY_DEF_PACKET_LEN,
226 uint8_t tx_pkt_nb_segs = 1; /**< Number of segments in TXONLY packets */
228 enum tx_pkt_split tx_pkt_split = TX_PKT_SPLIT_OFF;
229 /**< Split policy for packets to TX. */
231 uint8_t txonly_multi_flow;
232 /**< Whether multiple flows are generated in TXONLY mode. */
234 uint32_t tx_pkt_times_inter;
235 /**< Timings for send scheduling in TXONLY mode, time between bursts. */
237 uint32_t tx_pkt_times_intra;
238 /**< Timings for send scheduling in TXONLY mode, time between packets. */
240 uint16_t nb_pkt_per_burst = DEF_PKT_BURST; /**< Number of packets per burst. */
241 uint16_t mb_mempool_cache = DEF_MBUF_CACHE; /**< Size of mbuf mempool cache. */
243 /* current configuration is in DCB or not,0 means it is not in DCB mode */
244 uint8_t dcb_config = 0;
246 /* Whether the dcb is in testing status */
247 uint8_t dcb_test = 0;
250 * Configurable number of RX/TX queues.
252 queueid_t nb_hairpinq; /**< Number of hairpin queues per port. */
253 queueid_t nb_rxq = 1; /**< Number of RX queues per port. */
254 queueid_t nb_txq = 1; /**< Number of TX queues per port. */
257 * Configurable number of RX/TX ring descriptors.
258 * Defaults are supplied by drivers via ethdev.
260 #define RTE_TEST_RX_DESC_DEFAULT 0
261 #define RTE_TEST_TX_DESC_DEFAULT 0
262 uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT; /**< Number of RX descriptors. */
263 uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT; /**< Number of TX descriptors. */
265 #define RTE_PMD_PARAM_UNSET -1
267 * Configurable values of RX and TX ring threshold registers.
270 int8_t rx_pthresh = RTE_PMD_PARAM_UNSET;
271 int8_t rx_hthresh = RTE_PMD_PARAM_UNSET;
272 int8_t rx_wthresh = RTE_PMD_PARAM_UNSET;
274 int8_t tx_pthresh = RTE_PMD_PARAM_UNSET;
275 int8_t tx_hthresh = RTE_PMD_PARAM_UNSET;
276 int8_t tx_wthresh = RTE_PMD_PARAM_UNSET;
279 * Configurable value of RX free threshold.
281 int16_t rx_free_thresh = RTE_PMD_PARAM_UNSET;
284 * Configurable value of RX drop enable.
286 int8_t rx_drop_en = RTE_PMD_PARAM_UNSET;
289 * Configurable value of TX free threshold.
291 int16_t tx_free_thresh = RTE_PMD_PARAM_UNSET;
294 * Configurable value of TX RS bit threshold.
296 int16_t tx_rs_thresh = RTE_PMD_PARAM_UNSET;
299 * Configurable value of buffered packets before sending.
301 uint16_t noisy_tx_sw_bufsz;
304 * Configurable value of packet buffer timeout.
306 uint16_t noisy_tx_sw_buf_flush_time;
309 * Configurable value for size of VNF internal memory area
310 * used for simulating noisy neighbour behaviour
312 uint64_t noisy_lkup_mem_sz;
315 * Configurable value of number of random writes done in
316 * VNF simulation memory area.
318 uint64_t noisy_lkup_num_writes;
321 * Configurable value of number of random reads done in
322 * VNF simulation memory area.
324 uint64_t noisy_lkup_num_reads;
327 * Configurable value of number of random reads/writes done in
328 * VNF simulation memory area.
330 uint64_t noisy_lkup_num_reads_writes;
333 * Receive Side Scaling (RSS) configuration.
335 uint64_t rss_hf = ETH_RSS_IP; /* RSS IP by default. */
338 * Port topology configuration
340 uint16_t port_topology = PORT_TOPOLOGY_PAIRED; /* Ports are paired by default */
343 * Avoids to flush all the RX streams before starts forwarding.
345 uint8_t no_flush_rx = 0; /* flush by default */
348 * Flow API isolated mode.
350 uint8_t flow_isolate_all;
353 * Avoids to check link status when starting/stopping a port.
355 uint8_t no_link_check = 0; /* check by default */
358 * Don't automatically start all ports in interactive mode.
360 uint8_t no_device_start = 0;
363 * Enable link status change notification
365 uint8_t lsc_interrupt = 1; /* enabled by default */
368 * Enable device removal notification.
370 uint8_t rmv_interrupt = 1; /* enabled by default */
372 uint8_t hot_plug = 0; /**< hotplug disabled by default. */
374 /* After attach, port setup is called on event or by iterator */
375 bool setup_on_probe_event = true;
377 /* Clear ptypes on port initialization. */
378 uint8_t clear_ptypes = true;
380 /* Hairpin ports configuration mode. */
381 uint16_t hairpin_mode;
383 /* Pretty printing of ethdev events */
384 static const char * const eth_event_desc[] = {
385 [RTE_ETH_EVENT_UNKNOWN] = "unknown",
386 [RTE_ETH_EVENT_INTR_LSC] = "link state change",
387 [RTE_ETH_EVENT_QUEUE_STATE] = "queue state",
388 [RTE_ETH_EVENT_INTR_RESET] = "reset",
389 [RTE_ETH_EVENT_VF_MBOX] = "VF mbox",
390 [RTE_ETH_EVENT_IPSEC] = "IPsec",
391 [RTE_ETH_EVENT_MACSEC] = "MACsec",
392 [RTE_ETH_EVENT_INTR_RMV] = "device removal",
393 [RTE_ETH_EVENT_NEW] = "device probed",
394 [RTE_ETH_EVENT_DESTROY] = "device released",
395 [RTE_ETH_EVENT_FLOW_AGED] = "flow aged",
396 [RTE_ETH_EVENT_MAX] = NULL,
400 * Display or mask ether events
401 * Default to all events except VF_MBOX
403 uint32_t event_print_mask = (UINT32_C(1) << RTE_ETH_EVENT_UNKNOWN) |
404 (UINT32_C(1) << RTE_ETH_EVENT_INTR_LSC) |
405 (UINT32_C(1) << RTE_ETH_EVENT_QUEUE_STATE) |
406 (UINT32_C(1) << RTE_ETH_EVENT_INTR_RESET) |
407 (UINT32_C(1) << RTE_ETH_EVENT_IPSEC) |
408 (UINT32_C(1) << RTE_ETH_EVENT_MACSEC) |
409 (UINT32_C(1) << RTE_ETH_EVENT_INTR_RMV) |
410 (UINT32_C(1) << RTE_ETH_EVENT_FLOW_AGED);
412 * Decide if all memory are locked for performance.
417 * NIC bypass mode configuration options.
420 #if defined RTE_LIBRTE_IXGBE_PMD && defined RTE_LIBRTE_IXGBE_BYPASS
421 /* The NIC bypass watchdog timeout. */
422 uint32_t bypass_timeout = RTE_PMD_IXGBE_BYPASS_TMT_OFF;
426 #ifdef RTE_LIBRTE_LATENCY_STATS
429 * Set when latency stats is enabled in the commandline
431 uint8_t latencystats_enabled;
434 * Lcore ID to serive latency statistics.
436 lcoreid_t latencystats_lcore_id = -1;
441 * Ethernet device configuration.
443 struct rte_eth_rxmode rx_mode = {
444 .max_rx_pkt_len = RTE_ETHER_MAX_LEN,
445 /**< Default maximum frame length. */
448 struct rte_eth_txmode tx_mode = {
449 .offloads = DEV_TX_OFFLOAD_MBUF_FAST_FREE,
452 struct rte_fdir_conf fdir_conf = {
453 .mode = RTE_FDIR_MODE_NONE,
454 .pballoc = RTE_FDIR_PBALLOC_64K,
455 .status = RTE_FDIR_REPORT_STATUS,
457 .vlan_tci_mask = 0xFFEF,
459 .src_ip = 0xFFFFFFFF,
460 .dst_ip = 0xFFFFFFFF,
463 .src_ip = {0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF},
464 .dst_ip = {0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF},
466 .src_port_mask = 0xFFFF,
467 .dst_port_mask = 0xFFFF,
468 .mac_addr_byte_mask = 0xFF,
469 .tunnel_type_mask = 1,
470 .tunnel_id_mask = 0xFFFFFFFF,
475 volatile int test_done = 1; /* stop packet forwarding when set to 1. */
477 struct queue_stats_mappings tx_queue_stats_mappings_array[MAX_TX_QUEUE_STATS_MAPPINGS];
478 struct queue_stats_mappings rx_queue_stats_mappings_array[MAX_RX_QUEUE_STATS_MAPPINGS];
480 struct queue_stats_mappings *tx_queue_stats_mappings = tx_queue_stats_mappings_array;
481 struct queue_stats_mappings *rx_queue_stats_mappings = rx_queue_stats_mappings_array;
483 uint16_t nb_tx_queue_stats_mappings = 0;
484 uint16_t nb_rx_queue_stats_mappings = 0;
487 * Display zero values by default for xstats
489 uint8_t xstats_hide_zero;
492 * Measure of CPU cycles disabled by default
494 uint8_t record_core_cycles;
497 * Display of RX and TX bursts disabled by default
499 uint8_t record_burst_stats;
501 unsigned int num_sockets = 0;
502 unsigned int socket_ids[RTE_MAX_NUMA_NODES];
504 #ifdef RTE_LIBRTE_BITRATESTATS
505 /* Bitrate statistics */
506 struct rte_stats_bitrates *bitrate_data;
507 lcoreid_t bitrate_lcore_id;
508 uint8_t bitrate_enabled;
511 struct gro_status gro_ports[RTE_MAX_ETHPORTS];
512 uint8_t gro_flush_cycles = GRO_DEFAULT_FLUSH_CYCLES;
515 * hexadecimal bitmask of RX mq mode can be enabled.
517 enum rte_eth_rx_mq_mode rx_mq_mode = ETH_MQ_RX_VMDQ_DCB_RSS;
519 /* Forward function declarations */
520 static void setup_attached_port(portid_t pi);
521 static void map_port_queue_stats_mapping_registers(portid_t pi,
522 struct rte_port *port);
523 static void check_all_ports_link_status(uint32_t port_mask);
524 static int eth_event_callback(portid_t port_id,
525 enum rte_eth_event_type type,
526 void *param, void *ret_param);
527 static void dev_event_callback(const char *device_name,
528 enum rte_dev_event_type type,
532 * Check if all the ports are started.
533 * If yes, return positive value. If not, return zero.
535 static int all_ports_started(void);
537 struct gso_status gso_ports[RTE_MAX_ETHPORTS];
538 uint16_t gso_max_segment_size = RTE_ETHER_MAX_LEN - RTE_ETHER_CRC_LEN;
540 /* Holds the registered mbuf dynamic flags names. */
541 char dynf_names[64][RTE_MBUF_DYN_NAMESIZE];
544 * Helper function to check if socket is already discovered.
545 * If yes, return positive value. If not, return zero.
548 new_socket_id(unsigned int socket_id)
552 for (i = 0; i < num_sockets; i++) {
553 if (socket_ids[i] == socket_id)
560 * Setup default configuration.
563 set_default_fwd_lcores_config(void)
567 unsigned int sock_num;
570 for (i = 0; i < RTE_MAX_LCORE; i++) {
571 if (!rte_lcore_is_enabled(i))
573 sock_num = rte_lcore_to_socket_id(i);
574 if (new_socket_id(sock_num)) {
575 if (num_sockets >= RTE_MAX_NUMA_NODES) {
576 rte_exit(EXIT_FAILURE,
577 "Total sockets greater than %u\n",
580 socket_ids[num_sockets++] = sock_num;
582 if (i == rte_get_master_lcore())
584 fwd_lcores_cpuids[nb_lc++] = i;
586 nb_lcores = (lcoreid_t) nb_lc;
587 nb_cfg_lcores = nb_lcores;
592 set_def_peer_eth_addrs(void)
596 for (i = 0; i < RTE_MAX_ETHPORTS; i++) {
597 peer_eth_addrs[i].addr_bytes[0] = RTE_ETHER_LOCAL_ADMIN_ADDR;
598 peer_eth_addrs[i].addr_bytes[5] = i;
603 set_default_fwd_ports_config(void)
608 RTE_ETH_FOREACH_DEV(pt_id) {
609 fwd_ports_ids[i++] = pt_id;
611 /* Update sockets info according to the attached device */
612 int socket_id = rte_eth_dev_socket_id(pt_id);
613 if (socket_id >= 0 && new_socket_id(socket_id)) {
614 if (num_sockets >= RTE_MAX_NUMA_NODES) {
615 rte_exit(EXIT_FAILURE,
616 "Total sockets greater than %u\n",
619 socket_ids[num_sockets++] = socket_id;
623 nb_cfg_ports = nb_ports;
624 nb_fwd_ports = nb_ports;
628 set_def_fwd_config(void)
630 set_default_fwd_lcores_config();
631 set_def_peer_eth_addrs();
632 set_default_fwd_ports_config();
635 /* extremely pessimistic estimation of memory required to create a mempool */
637 calc_mem_size(uint32_t nb_mbufs, uint32_t mbuf_sz, size_t pgsz, size_t *out)
639 unsigned int n_pages, mbuf_per_pg, leftover;
640 uint64_t total_mem, mbuf_mem, obj_sz;
642 /* there is no good way to predict how much space the mempool will
643 * occupy because it will allocate chunks on the fly, and some of those
644 * will come from default DPDK memory while some will come from our
645 * external memory, so just assume 128MB will be enough for everyone.
647 uint64_t hdr_mem = 128 << 20;
649 /* account for possible non-contiguousness */
650 obj_sz = rte_mempool_calc_obj_size(mbuf_sz, 0, NULL);
652 TESTPMD_LOG(ERR, "Object size is bigger than page size\n");
656 mbuf_per_pg = pgsz / obj_sz;
657 leftover = (nb_mbufs % mbuf_per_pg) > 0;
658 n_pages = (nb_mbufs / mbuf_per_pg) + leftover;
660 mbuf_mem = n_pages * pgsz;
662 total_mem = RTE_ALIGN(hdr_mem + mbuf_mem, pgsz);
664 if (total_mem > SIZE_MAX) {
665 TESTPMD_LOG(ERR, "Memory size too big\n");
668 *out = (size_t)total_mem;
674 pagesz_flags(uint64_t page_sz)
676 /* as per mmap() manpage, all page sizes are log2 of page size
677 * shifted by MAP_HUGE_SHIFT
679 int log2 = rte_log2_u64(page_sz);
681 return (log2 << HUGE_SHIFT);
685 alloc_mem(size_t memsz, size_t pgsz, bool huge)
690 /* allocate anonymous hugepages */
691 flags = MAP_ANONYMOUS | MAP_PRIVATE;
693 flags |= HUGE_FLAG | pagesz_flags(pgsz);
695 addr = mmap(NULL, memsz, PROT_READ | PROT_WRITE, flags, -1, 0);
696 if (addr == MAP_FAILED)
702 struct extmem_param {
706 rte_iova_t *iova_table;
707 unsigned int iova_table_len;
711 create_extmem(uint32_t nb_mbufs, uint32_t mbuf_sz, struct extmem_param *param,
714 uint64_t pgsizes[] = {RTE_PGSIZE_2M, RTE_PGSIZE_1G, /* x86_64, ARM */
715 RTE_PGSIZE_16M, RTE_PGSIZE_16G}; /* POWER */
716 unsigned int cur_page, n_pages, pgsz_idx;
717 size_t mem_sz, cur_pgsz;
718 rte_iova_t *iovas = NULL;
722 for (pgsz_idx = 0; pgsz_idx < RTE_DIM(pgsizes); pgsz_idx++) {
723 /* skip anything that is too big */
724 if (pgsizes[pgsz_idx] > SIZE_MAX)
727 cur_pgsz = pgsizes[pgsz_idx];
729 /* if we were told not to allocate hugepages, override */
731 cur_pgsz = sysconf(_SC_PAGESIZE);
733 ret = calc_mem_size(nb_mbufs, mbuf_sz, cur_pgsz, &mem_sz);
735 TESTPMD_LOG(ERR, "Cannot calculate memory size\n");
739 /* allocate our memory */
740 addr = alloc_mem(mem_sz, cur_pgsz, huge);
742 /* if we couldn't allocate memory with a specified page size,
743 * that doesn't mean we can't do it with other page sizes, so
749 /* store IOVA addresses for every page in this memory area */
750 n_pages = mem_sz / cur_pgsz;
752 iovas = malloc(sizeof(*iovas) * n_pages);
755 TESTPMD_LOG(ERR, "Cannot allocate memory for iova addresses\n");
758 /* lock memory if it's not huge pages */
762 /* populate IOVA addresses */
763 for (cur_page = 0; cur_page < n_pages; cur_page++) {
768 offset = cur_pgsz * cur_page;
769 cur = RTE_PTR_ADD(addr, offset);
771 /* touch the page before getting its IOVA */
772 *(volatile char *)cur = 0;
774 iova = rte_mem_virt2iova(cur);
776 iovas[cur_page] = iova;
781 /* if we couldn't allocate anything */
787 param->pgsz = cur_pgsz;
788 param->iova_table = iovas;
789 param->iova_table_len = n_pages;
796 munmap(addr, mem_sz);
802 setup_extmem(uint32_t nb_mbufs, uint32_t mbuf_sz, bool huge)
804 struct extmem_param param;
807 memset(¶m, 0, sizeof(param));
809 /* check if our heap exists */
810 socket_id = rte_malloc_heap_get_socket(EXTMEM_HEAP_NAME);
812 /* create our heap */
813 ret = rte_malloc_heap_create(EXTMEM_HEAP_NAME);
815 TESTPMD_LOG(ERR, "Cannot create heap\n");
820 ret = create_extmem(nb_mbufs, mbuf_sz, ¶m, huge);
822 TESTPMD_LOG(ERR, "Cannot create memory area\n");
826 /* we now have a valid memory area, so add it to heap */
827 ret = rte_malloc_heap_memory_add(EXTMEM_HEAP_NAME,
828 param.addr, param.len, param.iova_table,
829 param.iova_table_len, param.pgsz);
831 /* when using VFIO, memory is automatically mapped for DMA by EAL */
833 /* not needed any more */
834 free(param.iova_table);
837 TESTPMD_LOG(ERR, "Cannot add memory to heap\n");
838 munmap(param.addr, param.len);
844 TESTPMD_LOG(DEBUG, "Allocated %zuMB of external memory\n",
850 dma_unmap_cb(struct rte_mempool *mp __rte_unused, void *opaque __rte_unused,
851 struct rte_mempool_memhdr *memhdr, unsigned mem_idx __rte_unused)
856 RTE_ETH_FOREACH_DEV(pid) {
857 struct rte_eth_dev *dev =
858 &rte_eth_devices[pid];
860 ret = rte_dev_dma_unmap(dev->device, memhdr->addr, 0,
864 "unable to DMA unmap addr 0x%p "
866 memhdr->addr, dev->data->name);
869 ret = rte_extmem_unregister(memhdr->addr, memhdr->len);
872 "unable to un-register addr 0x%p\n", memhdr->addr);
877 dma_map_cb(struct rte_mempool *mp __rte_unused, void *opaque __rte_unused,
878 struct rte_mempool_memhdr *memhdr, unsigned mem_idx __rte_unused)
881 size_t page_size = sysconf(_SC_PAGESIZE);
884 ret = rte_extmem_register(memhdr->addr, memhdr->len, NULL, 0,
888 "unable to register addr 0x%p\n", memhdr->addr);
891 RTE_ETH_FOREACH_DEV(pid) {
892 struct rte_eth_dev *dev =
893 &rte_eth_devices[pid];
895 ret = rte_dev_dma_map(dev->device, memhdr->addr, 0,
899 "unable to DMA map addr 0x%p "
901 memhdr->addr, dev->data->name);
907 setup_extbuf(uint32_t nb_mbufs, uint16_t mbuf_sz, unsigned int socket_id,
908 char *pool_name, struct rte_pktmbuf_extmem **ext_mem)
910 struct rte_pktmbuf_extmem *xmem;
911 unsigned int ext_num, zone_num, elt_num;
914 elt_size = RTE_ALIGN_CEIL(mbuf_sz, RTE_CACHE_LINE_SIZE);
915 elt_num = EXTBUF_ZONE_SIZE / elt_size;
916 zone_num = (nb_mbufs + elt_num - 1) / elt_num;
918 xmem = malloc(sizeof(struct rte_pktmbuf_extmem) * zone_num);
920 TESTPMD_LOG(ERR, "Cannot allocate memory for "
921 "external buffer descriptors\n");
925 for (ext_num = 0; ext_num < zone_num; ext_num++) {
926 struct rte_pktmbuf_extmem *xseg = xmem + ext_num;
927 const struct rte_memzone *mz;
928 char mz_name[RTE_MEMZONE_NAMESIZE];
931 ret = snprintf(mz_name, sizeof(mz_name),
932 RTE_MEMPOOL_MZ_FORMAT "_xb_%u", pool_name, ext_num);
933 if (ret < 0 || ret >= (int)sizeof(mz_name)) {
934 errno = ENAMETOOLONG;
938 mz = rte_memzone_reserve_aligned(mz_name, EXTBUF_ZONE_SIZE,
940 RTE_MEMZONE_IOVA_CONTIG |
942 RTE_MEMZONE_SIZE_HINT_ONLY,
946 * The caller exits on external buffer creation
947 * error, so there is no need to free memzones.
953 xseg->buf_ptr = mz->addr;
954 xseg->buf_iova = mz->iova;
955 xseg->buf_len = EXTBUF_ZONE_SIZE;
956 xseg->elt_size = elt_size;
958 if (ext_num == 0 && xmem != NULL) {
967 * Configuration initialisation done once at init time.
969 static struct rte_mempool *
970 mbuf_pool_create(uint16_t mbuf_seg_size, unsigned nb_mbuf,
971 unsigned int socket_id, uint16_t size_idx)
973 char pool_name[RTE_MEMPOOL_NAMESIZE];
974 struct rte_mempool *rte_mp = NULL;
977 mb_size = sizeof(struct rte_mbuf) + mbuf_seg_size;
978 mbuf_poolname_build(socket_id, pool_name, sizeof(pool_name), size_idx);
981 "create a new mbuf pool <%s>: n=%u, size=%u, socket=%u\n",
982 pool_name, nb_mbuf, mbuf_seg_size, socket_id);
984 switch (mp_alloc_type) {
985 case MP_ALLOC_NATIVE:
987 /* wrapper to rte_mempool_create() */
988 TESTPMD_LOG(INFO, "preferred mempool ops selected: %s\n",
989 rte_mbuf_best_mempool_ops());
990 rte_mp = rte_pktmbuf_pool_create(pool_name, nb_mbuf,
991 mb_mempool_cache, 0, mbuf_seg_size, socket_id);
996 rte_mp = rte_mempool_create_empty(pool_name, nb_mbuf,
997 mb_size, (unsigned int) mb_mempool_cache,
998 sizeof(struct rte_pktmbuf_pool_private),
999 socket_id, mempool_flags);
1003 if (rte_mempool_populate_anon(rte_mp) == 0) {
1004 rte_mempool_free(rte_mp);
1008 rte_pktmbuf_pool_init(rte_mp, NULL);
1009 rte_mempool_obj_iter(rte_mp, rte_pktmbuf_init, NULL);
1010 rte_mempool_mem_iter(rte_mp, dma_map_cb, NULL);
1014 case MP_ALLOC_XMEM_HUGE:
1017 bool huge = mp_alloc_type == MP_ALLOC_XMEM_HUGE;
1019 if (setup_extmem(nb_mbuf, mbuf_seg_size, huge) < 0)
1020 rte_exit(EXIT_FAILURE, "Could not create external memory\n");
1023 rte_malloc_heap_get_socket(EXTMEM_HEAP_NAME);
1024 if (heap_socket < 0)
1025 rte_exit(EXIT_FAILURE, "Could not get external memory socket ID\n");
1027 TESTPMD_LOG(INFO, "preferred mempool ops selected: %s\n",
1028 rte_mbuf_best_mempool_ops());
1029 rte_mp = rte_pktmbuf_pool_create(pool_name, nb_mbuf,
1030 mb_mempool_cache, 0, mbuf_seg_size,
1036 struct rte_pktmbuf_extmem *ext_mem;
1037 unsigned int ext_num;
1039 ext_num = setup_extbuf(nb_mbuf, mbuf_seg_size,
1040 socket_id, pool_name, &ext_mem);
1042 rte_exit(EXIT_FAILURE,
1043 "Can't create pinned data buffers\n");
1045 TESTPMD_LOG(INFO, "preferred mempool ops selected: %s\n",
1046 rte_mbuf_best_mempool_ops());
1047 rte_mp = rte_pktmbuf_pool_create_extbuf
1048 (pool_name, nb_mbuf, mb_mempool_cache,
1049 0, mbuf_seg_size, socket_id,
1056 rte_exit(EXIT_FAILURE, "Invalid mempool creation mode\n");
1061 if (rte_mp == NULL) {
1062 rte_exit(EXIT_FAILURE,
1063 "Creation of mbuf pool for socket %u failed: %s\n",
1064 socket_id, rte_strerror(rte_errno));
1065 } else if (verbose_level > 0) {
1066 rte_mempool_dump(stdout, rte_mp);
1072 * Check given socket id is valid or not with NUMA mode,
1073 * if valid, return 0, else return -1
1076 check_socket_id(const unsigned int socket_id)
1078 static int warning_once = 0;
1080 if (new_socket_id(socket_id)) {
1081 if (!warning_once && numa_support)
1082 printf("Warning: NUMA should be configured manually by"
1083 " using --port-numa-config and"
1084 " --ring-numa-config parameters along with"
1093 * Get the allowed maximum number of RX queues.
1094 * *pid return the port id which has minimal value of
1095 * max_rx_queues in all ports.
1098 get_allowed_max_nb_rxq(portid_t *pid)
1100 queueid_t allowed_max_rxq = RTE_MAX_QUEUES_PER_PORT;
1101 bool max_rxq_valid = false;
1103 struct rte_eth_dev_info dev_info;
1105 RTE_ETH_FOREACH_DEV(pi) {
1106 if (eth_dev_info_get_print_err(pi, &dev_info) != 0)
1109 max_rxq_valid = true;
1110 if (dev_info.max_rx_queues < allowed_max_rxq) {
1111 allowed_max_rxq = dev_info.max_rx_queues;
1115 return max_rxq_valid ? allowed_max_rxq : 0;
1119 * Check input rxq is valid or not.
1120 * If input rxq is not greater than any of maximum number
1121 * of RX queues of all ports, it is valid.
1122 * if valid, return 0, else return -1
1125 check_nb_rxq(queueid_t rxq)
1127 queueid_t allowed_max_rxq;
1130 allowed_max_rxq = get_allowed_max_nb_rxq(&pid);
1131 if (rxq > allowed_max_rxq) {
1132 printf("Fail: input rxq (%u) can't be greater "
1133 "than max_rx_queues (%u) of port %u\n",
1143 * Get the allowed maximum number of TX queues.
1144 * *pid return the port id which has minimal value of
1145 * max_tx_queues in all ports.
1148 get_allowed_max_nb_txq(portid_t *pid)
1150 queueid_t allowed_max_txq = RTE_MAX_QUEUES_PER_PORT;
1151 bool max_txq_valid = false;
1153 struct rte_eth_dev_info dev_info;
1155 RTE_ETH_FOREACH_DEV(pi) {
1156 if (eth_dev_info_get_print_err(pi, &dev_info) != 0)
1159 max_txq_valid = true;
1160 if (dev_info.max_tx_queues < allowed_max_txq) {
1161 allowed_max_txq = dev_info.max_tx_queues;
1165 return max_txq_valid ? allowed_max_txq : 0;
1169 * Check input txq is valid or not.
1170 * If input txq is not greater than any of maximum number
1171 * of TX queues of all ports, it is valid.
1172 * if valid, return 0, else return -1
1175 check_nb_txq(queueid_t txq)
1177 queueid_t allowed_max_txq;
1180 allowed_max_txq = get_allowed_max_nb_txq(&pid);
1181 if (txq > allowed_max_txq) {
1182 printf("Fail: input txq (%u) can't be greater "
1183 "than max_tx_queues (%u) of port %u\n",
1193 * Get the allowed maximum number of RXDs of every rx queue.
1194 * *pid return the port id which has minimal value of
1195 * max_rxd in all queues of all ports.
1198 get_allowed_max_nb_rxd(portid_t *pid)
1200 uint16_t allowed_max_rxd = UINT16_MAX;
1202 struct rte_eth_dev_info dev_info;
1204 RTE_ETH_FOREACH_DEV(pi) {
1205 if (eth_dev_info_get_print_err(pi, &dev_info) != 0)
1208 if (dev_info.rx_desc_lim.nb_max < allowed_max_rxd) {
1209 allowed_max_rxd = dev_info.rx_desc_lim.nb_max;
1213 return allowed_max_rxd;
1217 * Get the allowed minimal number of RXDs of every rx queue.
1218 * *pid return the port id which has minimal value of
1219 * min_rxd in all queues of all ports.
1222 get_allowed_min_nb_rxd(portid_t *pid)
1224 uint16_t allowed_min_rxd = 0;
1226 struct rte_eth_dev_info dev_info;
1228 RTE_ETH_FOREACH_DEV(pi) {
1229 if (eth_dev_info_get_print_err(pi, &dev_info) != 0)
1232 if (dev_info.rx_desc_lim.nb_min > allowed_min_rxd) {
1233 allowed_min_rxd = dev_info.rx_desc_lim.nb_min;
1238 return allowed_min_rxd;
1242 * Check input rxd is valid or not.
1243 * If input rxd is not greater than any of maximum number
1244 * of RXDs of every Rx queues and is not less than any of
1245 * minimal number of RXDs of every Rx queues, it is valid.
1246 * if valid, return 0, else return -1
1249 check_nb_rxd(queueid_t rxd)
1251 uint16_t allowed_max_rxd;
1252 uint16_t allowed_min_rxd;
1255 allowed_max_rxd = get_allowed_max_nb_rxd(&pid);
1256 if (rxd > allowed_max_rxd) {
1257 printf("Fail: input rxd (%u) can't be greater "
1258 "than max_rxds (%u) of port %u\n",
1265 allowed_min_rxd = get_allowed_min_nb_rxd(&pid);
1266 if (rxd < allowed_min_rxd) {
1267 printf("Fail: input rxd (%u) can't be less "
1268 "than min_rxds (%u) of port %u\n",
1279 * Get the allowed maximum number of TXDs of every rx queues.
1280 * *pid return the port id which has minimal value of
1281 * max_txd in every tx queue.
1284 get_allowed_max_nb_txd(portid_t *pid)
1286 uint16_t allowed_max_txd = UINT16_MAX;
1288 struct rte_eth_dev_info dev_info;
1290 RTE_ETH_FOREACH_DEV(pi) {
1291 if (eth_dev_info_get_print_err(pi, &dev_info) != 0)
1294 if (dev_info.tx_desc_lim.nb_max < allowed_max_txd) {
1295 allowed_max_txd = dev_info.tx_desc_lim.nb_max;
1299 return allowed_max_txd;
1303 * Get the allowed maximum number of TXDs of every tx queues.
1304 * *pid return the port id which has minimal value of
1305 * min_txd in every tx queue.
1308 get_allowed_min_nb_txd(portid_t *pid)
1310 uint16_t allowed_min_txd = 0;
1312 struct rte_eth_dev_info dev_info;
1314 RTE_ETH_FOREACH_DEV(pi) {
1315 if (eth_dev_info_get_print_err(pi, &dev_info) != 0)
1318 if (dev_info.tx_desc_lim.nb_min > allowed_min_txd) {
1319 allowed_min_txd = dev_info.tx_desc_lim.nb_min;
1324 return allowed_min_txd;
1328 * Check input txd is valid or not.
1329 * If input txd is not greater than any of maximum number
1330 * of TXDs of every Rx queues, it is valid.
1331 * if valid, return 0, else return -1
1334 check_nb_txd(queueid_t txd)
1336 uint16_t allowed_max_txd;
1337 uint16_t allowed_min_txd;
1340 allowed_max_txd = get_allowed_max_nb_txd(&pid);
1341 if (txd > allowed_max_txd) {
1342 printf("Fail: input txd (%u) can't be greater "
1343 "than max_txds (%u) of port %u\n",
1350 allowed_min_txd = get_allowed_min_nb_txd(&pid);
1351 if (txd < allowed_min_txd) {
1352 printf("Fail: input txd (%u) can't be less "
1353 "than min_txds (%u) of port %u\n",
1364 * Get the allowed maximum number of hairpin queues.
1365 * *pid return the port id which has minimal value of
1366 * max_hairpin_queues in all ports.
1369 get_allowed_max_nb_hairpinq(portid_t *pid)
1371 queueid_t allowed_max_hairpinq = RTE_MAX_QUEUES_PER_PORT;
1373 struct rte_eth_hairpin_cap cap;
1375 RTE_ETH_FOREACH_DEV(pi) {
1376 if (rte_eth_dev_hairpin_capability_get(pi, &cap) != 0) {
1380 if (cap.max_nb_queues < allowed_max_hairpinq) {
1381 allowed_max_hairpinq = cap.max_nb_queues;
1385 return allowed_max_hairpinq;
1389 * Check input hairpin is valid or not.
1390 * If input hairpin is not greater than any of maximum number
1391 * of hairpin queues of all ports, it is valid.
1392 * if valid, return 0, else return -1
1395 check_nb_hairpinq(queueid_t hairpinq)
1397 queueid_t allowed_max_hairpinq;
1400 allowed_max_hairpinq = get_allowed_max_nb_hairpinq(&pid);
1401 if (hairpinq > allowed_max_hairpinq) {
1402 printf("Fail: input hairpin (%u) can't be greater "
1403 "than max_hairpin_queues (%u) of port %u\n",
1404 hairpinq, allowed_max_hairpinq, pid);
1414 struct rte_port *port;
1415 struct rte_mempool *mbp;
1416 unsigned int nb_mbuf_per_pool;
1418 uint8_t port_per_socket[RTE_MAX_NUMA_NODES];
1419 struct rte_gro_param gro_param;
1426 memset(port_per_socket,0,RTE_MAX_NUMA_NODES);
1428 /* Configuration of logical cores. */
1429 fwd_lcores = rte_zmalloc("testpmd: fwd_lcores",
1430 sizeof(struct fwd_lcore *) * nb_lcores,
1431 RTE_CACHE_LINE_SIZE);
1432 if (fwd_lcores == NULL) {
1433 rte_exit(EXIT_FAILURE, "rte_zmalloc(%d (struct fwd_lcore *)) "
1434 "failed\n", nb_lcores);
1436 for (lc_id = 0; lc_id < nb_lcores; lc_id++) {
1437 fwd_lcores[lc_id] = rte_zmalloc("testpmd: struct fwd_lcore",
1438 sizeof(struct fwd_lcore),
1439 RTE_CACHE_LINE_SIZE);
1440 if (fwd_lcores[lc_id] == NULL) {
1441 rte_exit(EXIT_FAILURE, "rte_zmalloc(struct fwd_lcore) "
1444 fwd_lcores[lc_id]->cpuid_idx = lc_id;
1447 RTE_ETH_FOREACH_DEV(pid) {
1449 /* Apply default TxRx configuration for all ports */
1450 port->dev_conf.txmode = tx_mode;
1451 port->dev_conf.rxmode = rx_mode;
1453 ret = eth_dev_info_get_print_err(pid, &port->dev_info);
1455 rte_exit(EXIT_FAILURE,
1456 "rte_eth_dev_info_get() failed\n");
1458 if (!(port->dev_info.tx_offload_capa &
1459 DEV_TX_OFFLOAD_MBUF_FAST_FREE))
1460 port->dev_conf.txmode.offloads &=
1461 ~DEV_TX_OFFLOAD_MBUF_FAST_FREE;
1463 if (port_numa[pid] != NUMA_NO_CONFIG)
1464 port_per_socket[port_numa[pid]]++;
1466 uint32_t socket_id = rte_eth_dev_socket_id(pid);
1469 * if socket_id is invalid,
1470 * set to the first available socket.
1472 if (check_socket_id(socket_id) < 0)
1473 socket_id = socket_ids[0];
1474 port_per_socket[socket_id]++;
1478 /* Apply Rx offloads configuration */
1479 for (k = 0; k < port->dev_info.max_rx_queues; k++)
1480 port->rx_conf[k].offloads =
1481 port->dev_conf.rxmode.offloads;
1482 /* Apply Tx offloads configuration */
1483 for (k = 0; k < port->dev_info.max_tx_queues; k++)
1484 port->tx_conf[k].offloads =
1485 port->dev_conf.txmode.offloads;
1487 /* set flag to initialize port/queue */
1488 port->need_reconfig = 1;
1489 port->need_reconfig_queues = 1;
1490 port->tx_metadata = 0;
1492 /* Check for maximum number of segments per MTU. Accordingly
1493 * update the mbuf data size.
1495 if (port->dev_info.rx_desc_lim.nb_mtu_seg_max != UINT16_MAX &&
1496 port->dev_info.rx_desc_lim.nb_mtu_seg_max != 0) {
1497 data_size = rx_mode.max_rx_pkt_len /
1498 port->dev_info.rx_desc_lim.nb_mtu_seg_max;
1500 if ((data_size + RTE_PKTMBUF_HEADROOM) >
1501 mbuf_data_size[0]) {
1502 mbuf_data_size[0] = data_size +
1503 RTE_PKTMBUF_HEADROOM;
1510 TESTPMD_LOG(WARNING,
1511 "Configured mbuf size of the first segment %hu\n",
1514 * Create pools of mbuf.
1515 * If NUMA support is disabled, create a single pool of mbuf in
1516 * socket 0 memory by default.
1517 * Otherwise, create a pool of mbuf in the memory of sockets 0 and 1.
1519 * Use the maximum value of nb_rxd and nb_txd here, then nb_rxd and
1520 * nb_txd can be configured at run time.
1522 if (param_total_num_mbufs)
1523 nb_mbuf_per_pool = param_total_num_mbufs;
1525 nb_mbuf_per_pool = RTE_TEST_RX_DESC_MAX +
1526 (nb_lcores * mb_mempool_cache) +
1527 RTE_TEST_TX_DESC_MAX + MAX_PKT_BURST;
1528 nb_mbuf_per_pool *= RTE_MAX_ETHPORTS;
1534 for (i = 0; i < num_sockets; i++)
1535 for (j = 0; j < mbuf_data_size_n; j++)
1536 mempools[i * MAX_SEGS_BUFFER_SPLIT + j] =
1537 mbuf_pool_create(mbuf_data_size[j],
1543 for (i = 0; i < mbuf_data_size_n; i++)
1544 mempools[i] = mbuf_pool_create
1547 socket_num == UMA_NO_CONFIG ?
1553 gso_types = DEV_TX_OFFLOAD_TCP_TSO | DEV_TX_OFFLOAD_VXLAN_TNL_TSO |
1554 DEV_TX_OFFLOAD_GRE_TNL_TSO | DEV_TX_OFFLOAD_UDP_TSO;
1556 * Records which Mbuf pool to use by each logical core, if needed.
1558 for (lc_id = 0; lc_id < nb_lcores; lc_id++) {
1559 mbp = mbuf_pool_find(
1560 rte_lcore_to_socket_id(fwd_lcores_cpuids[lc_id]), 0);
1563 mbp = mbuf_pool_find(0, 0);
1564 fwd_lcores[lc_id]->mbp = mbp;
1565 /* initialize GSO context */
1566 fwd_lcores[lc_id]->gso_ctx.direct_pool = mbp;
1567 fwd_lcores[lc_id]->gso_ctx.indirect_pool = mbp;
1568 fwd_lcores[lc_id]->gso_ctx.gso_types = gso_types;
1569 fwd_lcores[lc_id]->gso_ctx.gso_size = RTE_ETHER_MAX_LEN -
1571 fwd_lcores[lc_id]->gso_ctx.flag = 0;
1574 /* Configuration of packet forwarding streams. */
1575 if (init_fwd_streams() < 0)
1576 rte_exit(EXIT_FAILURE, "FAIL from init_fwd_streams()\n");
1580 /* create a gro context for each lcore */
1581 gro_param.gro_types = RTE_GRO_TCP_IPV4;
1582 gro_param.max_flow_num = GRO_MAX_FLUSH_CYCLES;
1583 gro_param.max_item_per_flow = MAX_PKT_BURST;
1584 for (lc_id = 0; lc_id < nb_lcores; lc_id++) {
1585 gro_param.socket_id = rte_lcore_to_socket_id(
1586 fwd_lcores_cpuids[lc_id]);
1587 fwd_lcores[lc_id]->gro_ctx = rte_gro_ctx_create(&gro_param);
1588 if (fwd_lcores[lc_id]->gro_ctx == NULL) {
1589 rte_exit(EXIT_FAILURE,
1590 "rte_gro_ctx_create() failed\n");
1597 reconfig(portid_t new_port_id, unsigned socket_id)
1599 struct rte_port *port;
1602 /* Reconfiguration of Ethernet ports. */
1603 port = &ports[new_port_id];
1605 ret = eth_dev_info_get_print_err(new_port_id, &port->dev_info);
1609 /* set flag to initialize port/queue */
1610 port->need_reconfig = 1;
1611 port->need_reconfig_queues = 1;
1612 port->socket_id = socket_id;
1619 init_fwd_streams(void)
1622 struct rte_port *port;
1623 streamid_t sm_id, nb_fwd_streams_new;
1626 /* set socket id according to numa or not */
1627 RTE_ETH_FOREACH_DEV(pid) {
1629 if (nb_rxq > port->dev_info.max_rx_queues) {
1630 printf("Fail: nb_rxq(%d) is greater than "
1631 "max_rx_queues(%d)\n", nb_rxq,
1632 port->dev_info.max_rx_queues);
1635 if (nb_txq > port->dev_info.max_tx_queues) {
1636 printf("Fail: nb_txq(%d) is greater than "
1637 "max_tx_queues(%d)\n", nb_txq,
1638 port->dev_info.max_tx_queues);
1642 if (port_numa[pid] != NUMA_NO_CONFIG)
1643 port->socket_id = port_numa[pid];
1645 port->socket_id = rte_eth_dev_socket_id(pid);
1648 * if socket_id is invalid,
1649 * set to the first available socket.
1651 if (check_socket_id(port->socket_id) < 0)
1652 port->socket_id = socket_ids[0];
1656 if (socket_num == UMA_NO_CONFIG)
1657 port->socket_id = 0;
1659 port->socket_id = socket_num;
1663 q = RTE_MAX(nb_rxq, nb_txq);
1665 printf("Fail: Cannot allocate fwd streams as number of queues is 0\n");
1668 nb_fwd_streams_new = (streamid_t)(nb_ports * q);
1669 if (nb_fwd_streams_new == nb_fwd_streams)
1672 if (fwd_streams != NULL) {
1673 for (sm_id = 0; sm_id < nb_fwd_streams; sm_id++) {
1674 if (fwd_streams[sm_id] == NULL)
1676 rte_free(fwd_streams[sm_id]);
1677 fwd_streams[sm_id] = NULL;
1679 rte_free(fwd_streams);
1684 nb_fwd_streams = nb_fwd_streams_new;
1685 if (nb_fwd_streams) {
1686 fwd_streams = rte_zmalloc("testpmd: fwd_streams",
1687 sizeof(struct fwd_stream *) * nb_fwd_streams,
1688 RTE_CACHE_LINE_SIZE);
1689 if (fwd_streams == NULL)
1690 rte_exit(EXIT_FAILURE, "rte_zmalloc(%d"
1691 " (struct fwd_stream *)) failed\n",
1694 for (sm_id = 0; sm_id < nb_fwd_streams; sm_id++) {
1695 fwd_streams[sm_id] = rte_zmalloc("testpmd:"
1696 " struct fwd_stream", sizeof(struct fwd_stream),
1697 RTE_CACHE_LINE_SIZE);
1698 if (fwd_streams[sm_id] == NULL)
1699 rte_exit(EXIT_FAILURE, "rte_zmalloc"
1700 "(struct fwd_stream) failed\n");
1708 pkt_burst_stats_display(const char *rx_tx, struct pkt_burst_stats *pbs)
1710 uint64_t total_burst, sburst;
1712 uint64_t burst_stats[4];
1713 uint16_t pktnb_stats[4];
1715 int burst_percent[4], sburstp;
1719 * First compute the total number of packet bursts and the
1720 * two highest numbers of bursts of the same number of packets.
1722 memset(&burst_stats, 0x0, sizeof(burst_stats));
1723 memset(&pktnb_stats, 0x0, sizeof(pktnb_stats));
1725 /* Show stats for 0 burst size always */
1726 total_burst = pbs->pkt_burst_spread[0];
1727 burst_stats[0] = pbs->pkt_burst_spread[0];
1730 /* Find the next 2 burst sizes with highest occurrences. */
1731 for (nb_pkt = 1; nb_pkt < MAX_PKT_BURST; nb_pkt++) {
1732 nb_burst = pbs->pkt_burst_spread[nb_pkt];
1737 total_burst += nb_burst;
1739 if (nb_burst > burst_stats[1]) {
1740 burst_stats[2] = burst_stats[1];
1741 pktnb_stats[2] = pktnb_stats[1];
1742 burst_stats[1] = nb_burst;
1743 pktnb_stats[1] = nb_pkt;
1744 } else if (nb_burst > burst_stats[2]) {
1745 burst_stats[2] = nb_burst;
1746 pktnb_stats[2] = nb_pkt;
1749 if (total_burst == 0)
1752 printf(" %s-bursts : %"PRIu64" [", rx_tx, total_burst);
1753 for (i = 0, sburst = 0, sburstp = 0; i < 4; i++) {
1755 printf("%d%% of other]\n", 100 - sburstp);
1759 sburst += burst_stats[i];
1760 if (sburst == total_burst) {
1761 printf("%d%% of %d pkts]\n",
1762 100 - sburstp, (int) pktnb_stats[i]);
1767 (double)burst_stats[i] / total_burst * 100;
1768 printf("%d%% of %d pkts + ",
1769 burst_percent[i], (int) pktnb_stats[i]);
1770 sburstp += burst_percent[i];
1775 fwd_stream_stats_display(streamid_t stream_id)
1777 struct fwd_stream *fs;
1778 static const char *fwd_top_stats_border = "-------";
1780 fs = fwd_streams[stream_id];
1781 if ((fs->rx_packets == 0) && (fs->tx_packets == 0) &&
1782 (fs->fwd_dropped == 0))
1784 printf("\n %s Forward Stats for RX Port=%2d/Queue=%2d -> "
1785 "TX Port=%2d/Queue=%2d %s\n",
1786 fwd_top_stats_border, fs->rx_port, fs->rx_queue,
1787 fs->tx_port, fs->tx_queue, fwd_top_stats_border);
1788 printf(" RX-packets: %-14"PRIu64" TX-packets: %-14"PRIu64
1789 " TX-dropped: %-14"PRIu64,
1790 fs->rx_packets, fs->tx_packets, fs->fwd_dropped);
1792 /* if checksum mode */
1793 if (cur_fwd_eng == &csum_fwd_engine) {
1794 printf(" RX- bad IP checksum: %-14"PRIu64
1795 " Rx- bad L4 checksum: %-14"PRIu64
1796 " Rx- bad outer L4 checksum: %-14"PRIu64"\n",
1797 fs->rx_bad_ip_csum, fs->rx_bad_l4_csum,
1798 fs->rx_bad_outer_l4_csum);
1803 if (record_burst_stats) {
1804 pkt_burst_stats_display("RX", &fs->rx_burst_stats);
1805 pkt_burst_stats_display("TX", &fs->tx_burst_stats);
1810 fwd_stats_display(void)
1812 static const char *fwd_stats_border = "----------------------";
1813 static const char *acc_stats_border = "+++++++++++++++";
1815 struct fwd_stream *rx_stream;
1816 struct fwd_stream *tx_stream;
1817 uint64_t tx_dropped;
1818 uint64_t rx_bad_ip_csum;
1819 uint64_t rx_bad_l4_csum;
1820 uint64_t rx_bad_outer_l4_csum;
1821 } ports_stats[RTE_MAX_ETHPORTS];
1822 uint64_t total_rx_dropped = 0;
1823 uint64_t total_tx_dropped = 0;
1824 uint64_t total_rx_nombuf = 0;
1825 struct rte_eth_stats stats;
1826 uint64_t fwd_cycles = 0;
1827 uint64_t total_recv = 0;
1828 uint64_t total_xmit = 0;
1829 struct rte_port *port;
1834 memset(ports_stats, 0, sizeof(ports_stats));
1836 for (sm_id = 0; sm_id < cur_fwd_config.nb_fwd_streams; sm_id++) {
1837 struct fwd_stream *fs = fwd_streams[sm_id];
1839 if (cur_fwd_config.nb_fwd_streams >
1840 cur_fwd_config.nb_fwd_ports) {
1841 fwd_stream_stats_display(sm_id);
1843 ports_stats[fs->tx_port].tx_stream = fs;
1844 ports_stats[fs->rx_port].rx_stream = fs;
1847 ports_stats[fs->tx_port].tx_dropped += fs->fwd_dropped;
1849 ports_stats[fs->rx_port].rx_bad_ip_csum += fs->rx_bad_ip_csum;
1850 ports_stats[fs->rx_port].rx_bad_l4_csum += fs->rx_bad_l4_csum;
1851 ports_stats[fs->rx_port].rx_bad_outer_l4_csum +=
1852 fs->rx_bad_outer_l4_csum;
1854 if (record_core_cycles)
1855 fwd_cycles += fs->core_cycles;
1857 for (i = 0; i < cur_fwd_config.nb_fwd_ports; i++) {
1860 pt_id = fwd_ports_ids[i];
1861 port = &ports[pt_id];
1863 rte_eth_stats_get(pt_id, &stats);
1864 stats.ipackets -= port->stats.ipackets;
1865 stats.opackets -= port->stats.opackets;
1866 stats.ibytes -= port->stats.ibytes;
1867 stats.obytes -= port->stats.obytes;
1868 stats.imissed -= port->stats.imissed;
1869 stats.oerrors -= port->stats.oerrors;
1870 stats.rx_nombuf -= port->stats.rx_nombuf;
1872 total_recv += stats.ipackets;
1873 total_xmit += stats.opackets;
1874 total_rx_dropped += stats.imissed;
1875 total_tx_dropped += ports_stats[pt_id].tx_dropped;
1876 total_tx_dropped += stats.oerrors;
1877 total_rx_nombuf += stats.rx_nombuf;
1879 printf("\n %s Forward statistics for port %-2d %s\n",
1880 fwd_stats_border, pt_id, fwd_stats_border);
1882 if (!port->rx_queue_stats_mapping_enabled &&
1883 !port->tx_queue_stats_mapping_enabled) {
1884 printf(" RX-packets: %-14"PRIu64
1885 " RX-dropped: %-14"PRIu64
1886 "RX-total: %-"PRIu64"\n",
1887 stats.ipackets, stats.imissed,
1888 stats.ipackets + stats.imissed);
1890 if (cur_fwd_eng == &csum_fwd_engine)
1891 printf(" Bad-ipcsum: %-14"PRIu64
1892 " Bad-l4csum: %-14"PRIu64
1893 "Bad-outer-l4csum: %-14"PRIu64"\n",
1894 ports_stats[pt_id].rx_bad_ip_csum,
1895 ports_stats[pt_id].rx_bad_l4_csum,
1896 ports_stats[pt_id].rx_bad_outer_l4_csum);
1897 if (stats.ierrors + stats.rx_nombuf > 0) {
1898 printf(" RX-error: %-"PRIu64"\n",
1900 printf(" RX-nombufs: %-14"PRIu64"\n",
1904 printf(" TX-packets: %-14"PRIu64
1905 " TX-dropped: %-14"PRIu64
1906 "TX-total: %-"PRIu64"\n",
1907 stats.opackets, ports_stats[pt_id].tx_dropped,
1908 stats.opackets + ports_stats[pt_id].tx_dropped);
1910 printf(" RX-packets: %14"PRIu64
1911 " RX-dropped:%14"PRIu64
1912 " RX-total:%14"PRIu64"\n",
1913 stats.ipackets, stats.imissed,
1914 stats.ipackets + stats.imissed);
1916 if (cur_fwd_eng == &csum_fwd_engine)
1917 printf(" Bad-ipcsum:%14"PRIu64
1918 " Bad-l4csum:%14"PRIu64
1919 " Bad-outer-l4csum: %-14"PRIu64"\n",
1920 ports_stats[pt_id].rx_bad_ip_csum,
1921 ports_stats[pt_id].rx_bad_l4_csum,
1922 ports_stats[pt_id].rx_bad_outer_l4_csum);
1923 if ((stats.ierrors + stats.rx_nombuf) > 0) {
1924 printf(" RX-error:%"PRIu64"\n", stats.ierrors);
1925 printf(" RX-nombufs: %14"PRIu64"\n",
1929 printf(" TX-packets: %14"PRIu64
1930 " TX-dropped:%14"PRIu64
1931 " TX-total:%14"PRIu64"\n",
1932 stats.opackets, ports_stats[pt_id].tx_dropped,
1933 stats.opackets + ports_stats[pt_id].tx_dropped);
1936 if (record_burst_stats) {
1937 if (ports_stats[pt_id].rx_stream)
1938 pkt_burst_stats_display("RX",
1939 &ports_stats[pt_id].rx_stream->rx_burst_stats);
1940 if (ports_stats[pt_id].tx_stream)
1941 pkt_burst_stats_display("TX",
1942 &ports_stats[pt_id].tx_stream->tx_burst_stats);
1945 if (port->rx_queue_stats_mapping_enabled) {
1947 for (j = 0; j < RTE_ETHDEV_QUEUE_STAT_CNTRS; j++) {
1948 printf(" Stats reg %2d RX-packets:%14"PRIu64
1949 " RX-errors:%14"PRIu64
1950 " RX-bytes:%14"PRIu64"\n",
1951 j, stats.q_ipackets[j],
1952 stats.q_errors[j], stats.q_ibytes[j]);
1956 if (port->tx_queue_stats_mapping_enabled) {
1957 for (j = 0; j < RTE_ETHDEV_QUEUE_STAT_CNTRS; j++) {
1958 printf(" Stats reg %2d TX-packets:%14"PRIu64
1961 j, stats.q_opackets[j],
1966 printf(" %s--------------------------------%s\n",
1967 fwd_stats_border, fwd_stats_border);
1970 printf("\n %s Accumulated forward statistics for all ports"
1972 acc_stats_border, acc_stats_border);
1973 printf(" RX-packets: %-14"PRIu64" RX-dropped: %-14"PRIu64"RX-total: "
1975 " TX-packets: %-14"PRIu64" TX-dropped: %-14"PRIu64"TX-total: "
1977 total_recv, total_rx_dropped, total_recv + total_rx_dropped,
1978 total_xmit, total_tx_dropped, total_xmit + total_tx_dropped);
1979 if (total_rx_nombuf > 0)
1980 printf(" RX-nombufs: %-14"PRIu64"\n", total_rx_nombuf);
1981 printf(" %s++++++++++++++++++++++++++++++++++++++++++++++"
1983 acc_stats_border, acc_stats_border);
1984 if (record_core_cycles) {
1985 #define CYC_PER_MHZ 1E6
1986 if (total_recv > 0 || total_xmit > 0) {
1987 uint64_t total_pkts = 0;
1988 if (strcmp(cur_fwd_eng->fwd_mode_name, "txonly") == 0 ||
1989 strcmp(cur_fwd_eng->fwd_mode_name, "flowgen") == 0)
1990 total_pkts = total_xmit;
1992 total_pkts = total_recv;
1994 printf("\n CPU cycles/packet=%.2F (total cycles="
1995 "%"PRIu64" / total %s packets=%"PRIu64") at %"PRIu64
1997 (double) fwd_cycles / total_pkts,
1998 fwd_cycles, cur_fwd_eng->fwd_mode_name, total_pkts,
1999 (uint64_t)(rte_get_tsc_hz() / CYC_PER_MHZ));
2005 fwd_stats_reset(void)
2011 for (i = 0; i < cur_fwd_config.nb_fwd_ports; i++) {
2012 pt_id = fwd_ports_ids[i];
2013 rte_eth_stats_get(pt_id, &ports[pt_id].stats);
2015 for (sm_id = 0; sm_id < cur_fwd_config.nb_fwd_streams; sm_id++) {
2016 struct fwd_stream *fs = fwd_streams[sm_id];
2020 fs->fwd_dropped = 0;
2021 fs->rx_bad_ip_csum = 0;
2022 fs->rx_bad_l4_csum = 0;
2023 fs->rx_bad_outer_l4_csum = 0;
2025 memset(&fs->rx_burst_stats, 0, sizeof(fs->rx_burst_stats));
2026 memset(&fs->tx_burst_stats, 0, sizeof(fs->tx_burst_stats));
2027 fs->core_cycles = 0;
2032 flush_fwd_rx_queues(void)
2034 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
2041 uint64_t prev_tsc = 0, diff_tsc, cur_tsc, timer_tsc = 0;
2042 uint64_t timer_period;
2044 /* convert to number of cycles */
2045 timer_period = rte_get_timer_hz(); /* 1 second timeout */
2047 for (j = 0; j < 2; j++) {
2048 for (rxp = 0; rxp < cur_fwd_config.nb_fwd_ports; rxp++) {
2049 for (rxq = 0; rxq < nb_rxq; rxq++) {
2050 port_id = fwd_ports_ids[rxp];
2052 * testpmd can stuck in the below do while loop
2053 * if rte_eth_rx_burst() always returns nonzero
2054 * packets. So timer is added to exit this loop
2055 * after 1sec timer expiry.
2057 prev_tsc = rte_rdtsc();
2059 nb_rx = rte_eth_rx_burst(port_id, rxq,
2060 pkts_burst, MAX_PKT_BURST);
2061 for (i = 0; i < nb_rx; i++)
2062 rte_pktmbuf_free(pkts_burst[i]);
2064 cur_tsc = rte_rdtsc();
2065 diff_tsc = cur_tsc - prev_tsc;
2066 timer_tsc += diff_tsc;
2067 } while ((nb_rx > 0) &&
2068 (timer_tsc < timer_period));
2072 rte_delay_ms(10); /* wait 10 milli-seconds before retrying */
2077 run_pkt_fwd_on_lcore(struct fwd_lcore *fc, packet_fwd_t pkt_fwd)
2079 struct fwd_stream **fsm;
2082 #ifdef RTE_LIBRTE_BITRATESTATS
2083 uint64_t tics_per_1sec;
2084 uint64_t tics_datum;
2085 uint64_t tics_current;
2086 uint16_t i, cnt_ports;
2088 cnt_ports = nb_ports;
2089 tics_datum = rte_rdtsc();
2090 tics_per_1sec = rte_get_timer_hz();
2092 fsm = &fwd_streams[fc->stream_idx];
2093 nb_fs = fc->stream_nb;
2095 for (sm_id = 0; sm_id < nb_fs; sm_id++)
2096 (*pkt_fwd)(fsm[sm_id]);
2097 #ifdef RTE_LIBRTE_BITRATESTATS
2098 if (bitrate_enabled != 0 &&
2099 bitrate_lcore_id == rte_lcore_id()) {
2100 tics_current = rte_rdtsc();
2101 if (tics_current - tics_datum >= tics_per_1sec) {
2102 /* Periodic bitrate calculation */
2103 for (i = 0; i < cnt_ports; i++)
2104 rte_stats_bitrate_calc(bitrate_data,
2106 tics_datum = tics_current;
2110 #ifdef RTE_LIBRTE_LATENCY_STATS
2111 if (latencystats_enabled != 0 &&
2112 latencystats_lcore_id == rte_lcore_id())
2113 rte_latencystats_update();
2116 } while (! fc->stopped);
2120 start_pkt_forward_on_core(void *fwd_arg)
2122 run_pkt_fwd_on_lcore((struct fwd_lcore *) fwd_arg,
2123 cur_fwd_config.fwd_eng->packet_fwd);
2128 * Run the TXONLY packet forwarding engine to send a single burst of packets.
2129 * Used to start communication flows in network loopback test configurations.
2132 run_one_txonly_burst_on_core(void *fwd_arg)
2134 struct fwd_lcore *fwd_lc;
2135 struct fwd_lcore tmp_lcore;
2137 fwd_lc = (struct fwd_lcore *) fwd_arg;
2138 tmp_lcore = *fwd_lc;
2139 tmp_lcore.stopped = 1;
2140 run_pkt_fwd_on_lcore(&tmp_lcore, tx_only_engine.packet_fwd);
2145 * Launch packet forwarding:
2146 * - Setup per-port forwarding context.
2147 * - launch logical cores with their forwarding configuration.
2150 launch_packet_forwarding(lcore_function_t *pkt_fwd_on_lcore)
2152 port_fwd_begin_t port_fwd_begin;
2157 port_fwd_begin = cur_fwd_config.fwd_eng->port_fwd_begin;
2158 if (port_fwd_begin != NULL) {
2159 for (i = 0; i < cur_fwd_config.nb_fwd_ports; i++)
2160 (*port_fwd_begin)(fwd_ports_ids[i]);
2162 for (i = 0; i < cur_fwd_config.nb_fwd_lcores; i++) {
2163 lc_id = fwd_lcores_cpuids[i];
2164 if ((interactive == 0) || (lc_id != rte_lcore_id())) {
2165 fwd_lcores[i]->stopped = 0;
2166 diag = rte_eal_remote_launch(pkt_fwd_on_lcore,
2167 fwd_lcores[i], lc_id);
2169 printf("launch lcore %u failed - diag=%d\n",
2176 * Launch packet forwarding configuration.
2179 start_packet_forwarding(int with_tx_first)
2181 port_fwd_begin_t port_fwd_begin;
2182 port_fwd_end_t port_fwd_end;
2183 struct rte_port *port;
2187 if (strcmp(cur_fwd_eng->fwd_mode_name, "rxonly") == 0 && !nb_rxq)
2188 rte_exit(EXIT_FAILURE, "rxq are 0, cannot use rxonly fwd mode\n");
2190 if (strcmp(cur_fwd_eng->fwd_mode_name, "txonly") == 0 && !nb_txq)
2191 rte_exit(EXIT_FAILURE, "txq are 0, cannot use txonly fwd mode\n");
2193 if ((strcmp(cur_fwd_eng->fwd_mode_name, "rxonly") != 0 &&
2194 strcmp(cur_fwd_eng->fwd_mode_name, "txonly") != 0) &&
2195 (!nb_rxq || !nb_txq))
2196 rte_exit(EXIT_FAILURE,
2197 "Either rxq or txq are 0, cannot use %s fwd mode\n",
2198 cur_fwd_eng->fwd_mode_name);
2200 if (all_ports_started() == 0) {
2201 printf("Not all ports were started\n");
2204 if (test_done == 0) {
2205 printf("Packet forwarding already started\n");
2211 for (i = 0; i < nb_fwd_ports; i++) {
2212 pt_id = fwd_ports_ids[i];
2213 port = &ports[pt_id];
2214 if (!port->dcb_flag) {
2215 printf("In DCB mode, all forwarding ports must "
2216 "be configured in this mode.\n");
2220 if (nb_fwd_lcores == 1) {
2221 printf("In DCB mode,the nb forwarding cores "
2222 "should be larger than 1.\n");
2231 flush_fwd_rx_queues();
2233 pkt_fwd_config_display(&cur_fwd_config);
2234 rxtx_config_display();
2237 for (i = 0; i < cur_fwd_config.nb_fwd_ports; i++) {
2238 pt_id = fwd_ports_ids[i];
2239 port = &ports[pt_id];
2240 map_port_queue_stats_mapping_registers(pt_id, port);
2242 if (with_tx_first) {
2243 port_fwd_begin = tx_only_engine.port_fwd_begin;
2244 if (port_fwd_begin != NULL) {
2245 for (i = 0; i < cur_fwd_config.nb_fwd_ports; i++)
2246 (*port_fwd_begin)(fwd_ports_ids[i]);
2248 while (with_tx_first--) {
2249 launch_packet_forwarding(
2250 run_one_txonly_burst_on_core);
2251 rte_eal_mp_wait_lcore();
2253 port_fwd_end = tx_only_engine.port_fwd_end;
2254 if (port_fwd_end != NULL) {
2255 for (i = 0; i < cur_fwd_config.nb_fwd_ports; i++)
2256 (*port_fwd_end)(fwd_ports_ids[i]);
2259 launch_packet_forwarding(start_pkt_forward_on_core);
2263 stop_packet_forwarding(void)
2265 port_fwd_end_t port_fwd_end;
2271 printf("Packet forwarding not started\n");
2274 printf("Telling cores to stop...");
2275 for (lc_id = 0; lc_id < cur_fwd_config.nb_fwd_lcores; lc_id++)
2276 fwd_lcores[lc_id]->stopped = 1;
2277 printf("\nWaiting for lcores to finish...\n");
2278 rte_eal_mp_wait_lcore();
2279 port_fwd_end = cur_fwd_config.fwd_eng->port_fwd_end;
2280 if (port_fwd_end != NULL) {
2281 for (i = 0; i < cur_fwd_config.nb_fwd_ports; i++) {
2282 pt_id = fwd_ports_ids[i];
2283 (*port_fwd_end)(pt_id);
2287 fwd_stats_display();
2289 printf("\nDone.\n");
2294 dev_set_link_up(portid_t pid)
2296 if (rte_eth_dev_set_link_up(pid) < 0)
2297 printf("\nSet link up fail.\n");
2301 dev_set_link_down(portid_t pid)
2303 if (rte_eth_dev_set_link_down(pid) < 0)
2304 printf("\nSet link down fail.\n");
2308 all_ports_started(void)
2311 struct rte_port *port;
2313 RTE_ETH_FOREACH_DEV(pi) {
2315 /* Check if there is a port which is not started */
2316 if ((port->port_status != RTE_PORT_STARTED) &&
2317 (port->slave_flag == 0))
2321 /* No port is not started */
2326 port_is_stopped(portid_t port_id)
2328 struct rte_port *port = &ports[port_id];
2330 if ((port->port_status != RTE_PORT_STOPPED) &&
2331 (port->slave_flag == 0))
2337 all_ports_stopped(void)
2341 RTE_ETH_FOREACH_DEV(pi) {
2342 if (!port_is_stopped(pi))
2350 port_is_started(portid_t port_id)
2352 if (port_id_is_invalid(port_id, ENABLED_WARN))
2355 if (ports[port_id].port_status != RTE_PORT_STARTED)
2361 /* Configure the Rx and Tx hairpin queues for the selected port. */
2363 setup_hairpin_queues(portid_t pi, portid_t p_pi, uint16_t cnt_pi)
2366 struct rte_eth_hairpin_conf hairpin_conf = {
2371 struct rte_port *port = &ports[pi];
2372 uint16_t peer_rx_port = pi;
2373 uint16_t peer_tx_port = pi;
2374 uint32_t manual = 1;
2375 uint32_t tx_exp = hairpin_mode & 0x10;
2377 if (!(hairpin_mode & 0xf)) {
2381 } else if (hairpin_mode & 0x1) {
2382 peer_tx_port = rte_eth_find_next_owned_by(pi + 1,
2383 RTE_ETH_DEV_NO_OWNER);
2384 if (peer_tx_port >= RTE_MAX_ETHPORTS)
2385 peer_tx_port = rte_eth_find_next_owned_by(0,
2386 RTE_ETH_DEV_NO_OWNER);
2387 if (p_pi != RTE_MAX_ETHPORTS) {
2388 peer_rx_port = p_pi;
2392 /* Last port will be the peer RX port of the first. */
2393 RTE_ETH_FOREACH_DEV(next_pi)
2394 peer_rx_port = next_pi;
2397 } else if (hairpin_mode & 0x2) {
2399 peer_rx_port = p_pi;
2401 peer_rx_port = rte_eth_find_next_owned_by(pi + 1,
2402 RTE_ETH_DEV_NO_OWNER);
2403 if (peer_rx_port >= RTE_MAX_ETHPORTS)
2406 peer_tx_port = peer_rx_port;
2410 for (qi = nb_txq, i = 0; qi < nb_hairpinq + nb_txq; qi++) {
2411 hairpin_conf.peers[0].port = peer_rx_port;
2412 hairpin_conf.peers[0].queue = i + nb_rxq;
2413 hairpin_conf.manual_bind = !!manual;
2414 hairpin_conf.tx_explicit = !!tx_exp;
2415 diag = rte_eth_tx_hairpin_queue_setup
2416 (pi, qi, nb_txd, &hairpin_conf);
2421 /* Fail to setup rx queue, return */
2422 if (rte_atomic16_cmpset(&(port->port_status),
2424 RTE_PORT_STOPPED) == 0)
2425 printf("Port %d can not be set back "
2426 "to stopped\n", pi);
2427 printf("Fail to configure port %d hairpin "
2429 /* try to reconfigure queues next time */
2430 port->need_reconfig_queues = 1;
2433 for (qi = nb_rxq, i = 0; qi < nb_hairpinq + nb_rxq; qi++) {
2434 hairpin_conf.peers[0].port = peer_tx_port;
2435 hairpin_conf.peers[0].queue = i + nb_txq;
2436 hairpin_conf.manual_bind = !!manual;
2437 hairpin_conf.tx_explicit = !!tx_exp;
2438 diag = rte_eth_rx_hairpin_queue_setup
2439 (pi, qi, nb_rxd, &hairpin_conf);
2444 /* Fail to setup rx queue, return */
2445 if (rte_atomic16_cmpset(&(port->port_status),
2447 RTE_PORT_STOPPED) == 0)
2448 printf("Port %d can not be set back "
2449 "to stopped\n", pi);
2450 printf("Fail to configure port %d hairpin "
2452 /* try to reconfigure queues next time */
2453 port->need_reconfig_queues = 1;
2460 start_port(portid_t pid)
2462 int diag, need_check_link_status = -1;
2464 portid_t p_pi = RTE_MAX_ETHPORTS;
2465 portid_t pl[RTE_MAX_ETHPORTS];
2466 portid_t peer_pl[RTE_MAX_ETHPORTS];
2467 uint16_t cnt_pi = 0;
2468 uint16_t cfg_pi = 0;
2471 struct rte_port *port;
2472 struct rte_ether_addr mac_addr;
2473 struct rte_eth_hairpin_cap cap;
2475 if (port_id_is_invalid(pid, ENABLED_WARN))
2480 RTE_ETH_FOREACH_DEV(pi) {
2481 if (pid != pi && pid != (portid_t)RTE_PORT_ALL)
2484 need_check_link_status = 0;
2486 if (rte_atomic16_cmpset(&(port->port_status), RTE_PORT_STOPPED,
2487 RTE_PORT_HANDLING) == 0) {
2488 printf("Port %d is now not stopped\n", pi);
2492 if (port->need_reconfig > 0) {
2493 port->need_reconfig = 0;
2495 if (flow_isolate_all) {
2496 int ret = port_flow_isolate(pi, 1);
2498 printf("Failed to apply isolated"
2499 " mode on port %d\n", pi);
2503 configure_rxtx_dump_callbacks(0);
2504 printf("Configuring Port %d (socket %u)\n", pi,
2506 if (nb_hairpinq > 0 &&
2507 rte_eth_dev_hairpin_capability_get(pi, &cap)) {
2508 printf("Port %d doesn't support hairpin "
2512 /* configure port */
2513 diag = rte_eth_dev_configure(pi, nb_rxq + nb_hairpinq,
2514 nb_txq + nb_hairpinq,
2517 if (rte_atomic16_cmpset(&(port->port_status),
2518 RTE_PORT_HANDLING, RTE_PORT_STOPPED) == 0)
2519 printf("Port %d can not be set back "
2520 "to stopped\n", pi);
2521 printf("Fail to configure port %d\n", pi);
2522 /* try to reconfigure port next time */
2523 port->need_reconfig = 1;
2527 if (port->need_reconfig_queues > 0) {
2528 port->need_reconfig_queues = 0;
2529 /* setup tx queues */
2530 for (qi = 0; qi < nb_txq; qi++) {
2531 if ((numa_support) &&
2532 (txring_numa[pi] != NUMA_NO_CONFIG))
2533 diag = rte_eth_tx_queue_setup(pi, qi,
2534 port->nb_tx_desc[qi],
2536 &(port->tx_conf[qi]));
2538 diag = rte_eth_tx_queue_setup(pi, qi,
2539 port->nb_tx_desc[qi],
2541 &(port->tx_conf[qi]));
2546 /* Fail to setup tx queue, return */
2547 if (rte_atomic16_cmpset(&(port->port_status),
2549 RTE_PORT_STOPPED) == 0)
2550 printf("Port %d can not be set back "
2551 "to stopped\n", pi);
2552 printf("Fail to configure port %d tx queues\n",
2554 /* try to reconfigure queues next time */
2555 port->need_reconfig_queues = 1;
2558 for (qi = 0; qi < nb_rxq; qi++) {
2559 /* setup rx queues */
2560 if ((numa_support) &&
2561 (rxring_numa[pi] != NUMA_NO_CONFIG)) {
2562 struct rte_mempool * mp =
2564 (rxring_numa[pi], 0);
2566 printf("Failed to setup RX queue:"
2567 "No mempool allocation"
2568 " on the socket %d\n",
2573 diag = rte_eth_rx_queue_setup(pi, qi,
2574 port->nb_rx_desc[qi],
2576 &(port->rx_conf[qi]),
2579 struct rte_mempool *mp =
2581 (port->socket_id, 0);
2583 printf("Failed to setup RX queue:"
2584 "No mempool allocation"
2585 " on the socket %d\n",
2589 diag = rte_eth_rx_queue_setup(pi, qi,
2590 port->nb_rx_desc[qi],
2592 &(port->rx_conf[qi]),
2598 /* Fail to setup rx queue, return */
2599 if (rte_atomic16_cmpset(&(port->port_status),
2601 RTE_PORT_STOPPED) == 0)
2602 printf("Port %d can not be set back "
2603 "to stopped\n", pi);
2604 printf("Fail to configure port %d rx queues\n",
2606 /* try to reconfigure queues next time */
2607 port->need_reconfig_queues = 1;
2610 /* setup hairpin queues */
2611 if (setup_hairpin_queues(pi, p_pi, cnt_pi) != 0)
2614 configure_rxtx_dump_callbacks(verbose_level);
2616 diag = rte_eth_dev_set_ptypes(pi, RTE_PTYPE_UNKNOWN,
2620 "Port %d: Failed to disable Ptype parsing\n",
2628 if (rte_eth_dev_start(pi) < 0) {
2629 printf("Fail to start port %d\n", pi);
2631 /* Fail to setup rx queue, return */
2632 if (rte_atomic16_cmpset(&(port->port_status),
2633 RTE_PORT_HANDLING, RTE_PORT_STOPPED) == 0)
2634 printf("Port %d can not be set back to "
2639 if (rte_atomic16_cmpset(&(port->port_status),
2640 RTE_PORT_HANDLING, RTE_PORT_STARTED) == 0)
2641 printf("Port %d can not be set into started\n", pi);
2643 if (eth_macaddr_get_print_err(pi, &mac_addr) == 0)
2644 printf("Port %d: %02X:%02X:%02X:%02X:%02X:%02X\n", pi,
2645 mac_addr.addr_bytes[0], mac_addr.addr_bytes[1],
2646 mac_addr.addr_bytes[2], mac_addr.addr_bytes[3],
2647 mac_addr.addr_bytes[4], mac_addr.addr_bytes[5]);
2649 /* at least one port started, need checking link status */
2650 need_check_link_status = 1;
2655 if (need_check_link_status == 1 && !no_link_check)
2656 check_all_ports_link_status(RTE_PORT_ALL);
2657 else if (need_check_link_status == 0)
2658 printf("Please stop the ports first\n");
2660 if (hairpin_mode & 0xf) {
2664 /* bind all started hairpin ports */
2665 for (i = 0; i < cfg_pi; i++) {
2667 /* bind current Tx to all peer Rx */
2668 peer_pi = rte_eth_hairpin_get_peer_ports(pi, peer_pl,
2669 RTE_MAX_ETHPORTS, 1);
2672 for (j = 0; j < peer_pi; j++) {
2673 if (!port_is_started(peer_pl[j]))
2675 diag = rte_eth_hairpin_bind(pi, peer_pl[j]);
2677 printf("Error during binding hairpin"
2678 " Tx port %u to %u: %s\n",
2680 rte_strerror(-diag));
2684 /* bind all peer Tx to current Rx */
2685 peer_pi = rte_eth_hairpin_get_peer_ports(pi, peer_pl,
2686 RTE_MAX_ETHPORTS, 0);
2689 for (j = 0; j < peer_pi; j++) {
2690 if (!port_is_started(peer_pl[j]))
2692 diag = rte_eth_hairpin_bind(peer_pl[j], pi);
2694 printf("Error during binding hairpin"
2695 " Tx port %u to %u: %s\n",
2697 rte_strerror(-diag));
2709 stop_port(portid_t pid)
2712 struct rte_port *port;
2713 int need_check_link_status = 0;
2714 portid_t peer_pl[RTE_MAX_ETHPORTS];
2722 if (port_id_is_invalid(pid, ENABLED_WARN))
2725 printf("Stopping ports...\n");
2727 RTE_ETH_FOREACH_DEV(pi) {
2728 if (pid != pi && pid != (portid_t)RTE_PORT_ALL)
2731 if (port_is_forwarding(pi) != 0 && test_done == 0) {
2732 printf("Please remove port %d from forwarding configuration.\n", pi);
2736 if (port_is_bonding_slave(pi)) {
2737 printf("Please remove port %d from bonded device.\n", pi);
2742 if (rte_atomic16_cmpset(&(port->port_status), RTE_PORT_STARTED,
2743 RTE_PORT_HANDLING) == 0)
2746 if (hairpin_mode & 0xf) {
2749 rte_eth_hairpin_unbind(pi, RTE_MAX_ETHPORTS);
2750 /* unbind all peer Tx from current Rx */
2751 peer_pi = rte_eth_hairpin_get_peer_ports(pi, peer_pl,
2752 RTE_MAX_ETHPORTS, 0);
2755 for (j = 0; j < peer_pi; j++) {
2756 if (!port_is_started(peer_pl[j]))
2758 rte_eth_hairpin_unbind(peer_pl[j], pi);
2762 rte_eth_dev_stop(pi);
2764 if (rte_atomic16_cmpset(&(port->port_status),
2765 RTE_PORT_HANDLING, RTE_PORT_STOPPED) == 0)
2766 printf("Port %d can not be set into stopped\n", pi);
2767 need_check_link_status = 1;
2769 if (need_check_link_status && !no_link_check)
2770 check_all_ports_link_status(RTE_PORT_ALL);
2776 remove_invalid_ports_in(portid_t *array, portid_t *total)
2779 portid_t new_total = 0;
2781 for (i = 0; i < *total; i++)
2782 if (!port_id_is_invalid(array[i], DISABLED_WARN)) {
2783 array[new_total] = array[i];
2790 remove_invalid_ports(void)
2792 remove_invalid_ports_in(ports_ids, &nb_ports);
2793 remove_invalid_ports_in(fwd_ports_ids, &nb_fwd_ports);
2794 nb_cfg_ports = nb_fwd_ports;
2798 close_port(portid_t pid)
2801 struct rte_port *port;
2803 if (port_id_is_invalid(pid, ENABLED_WARN))
2806 printf("Closing ports...\n");
2808 RTE_ETH_FOREACH_DEV(pi) {
2809 if (pid != pi && pid != (portid_t)RTE_PORT_ALL)
2812 if (port_is_forwarding(pi) != 0 && test_done == 0) {
2813 printf("Please remove port %d from forwarding configuration.\n", pi);
2817 if (port_is_bonding_slave(pi)) {
2818 printf("Please remove port %d from bonded device.\n", pi);
2823 if (rte_atomic16_cmpset(&(port->port_status),
2824 RTE_PORT_CLOSED, RTE_PORT_CLOSED) == 1) {
2825 printf("Port %d is already closed\n", pi);
2829 port_flow_flush(pi);
2830 rte_eth_dev_close(pi);
2833 remove_invalid_ports();
2838 reset_port(portid_t pid)
2842 struct rte_port *port;
2844 if (port_id_is_invalid(pid, ENABLED_WARN))
2847 if ((pid == (portid_t)RTE_PORT_ALL && !all_ports_stopped()) ||
2848 (pid != (portid_t)RTE_PORT_ALL && !port_is_stopped(pid))) {
2849 printf("Can not reset port(s), please stop port(s) first.\n");
2853 printf("Resetting ports...\n");
2855 RTE_ETH_FOREACH_DEV(pi) {
2856 if (pid != pi && pid != (portid_t)RTE_PORT_ALL)
2859 if (port_is_forwarding(pi) != 0 && test_done == 0) {
2860 printf("Please remove port %d from forwarding "
2861 "configuration.\n", pi);
2865 if (port_is_bonding_slave(pi)) {
2866 printf("Please remove port %d from bonded device.\n",
2871 diag = rte_eth_dev_reset(pi);
2874 port->need_reconfig = 1;
2875 port->need_reconfig_queues = 1;
2877 printf("Failed to reset port %d. diag=%d\n", pi, diag);
2885 attach_port(char *identifier)
2888 struct rte_dev_iterator iterator;
2890 printf("Attaching a new port...\n");
2892 if (identifier == NULL) {
2893 printf("Invalid parameters are specified\n");
2897 if (rte_dev_probe(identifier) < 0) {
2898 TESTPMD_LOG(ERR, "Failed to attach port %s\n", identifier);
2902 /* first attach mode: event */
2903 if (setup_on_probe_event) {
2904 /* new ports are detected on RTE_ETH_EVENT_NEW event */
2905 for (pi = 0; pi < RTE_MAX_ETHPORTS; pi++)
2906 if (ports[pi].port_status == RTE_PORT_HANDLING &&
2907 ports[pi].need_setup != 0)
2908 setup_attached_port(pi);
2912 /* second attach mode: iterator */
2913 RTE_ETH_FOREACH_MATCHING_DEV(pi, identifier, &iterator) {
2914 /* setup ports matching the devargs used for probing */
2915 if (port_is_forwarding(pi))
2916 continue; /* port was already attached before */
2917 setup_attached_port(pi);
2922 setup_attached_port(portid_t pi)
2924 unsigned int socket_id;
2927 socket_id = (unsigned)rte_eth_dev_socket_id(pi);
2928 /* if socket_id is invalid, set to the first available socket. */
2929 if (check_socket_id(socket_id) < 0)
2930 socket_id = socket_ids[0];
2931 reconfig(pi, socket_id);
2932 ret = rte_eth_promiscuous_enable(pi);
2934 printf("Error during enabling promiscuous mode for port %u: %s - ignore\n",
2935 pi, rte_strerror(-ret));
2937 ports_ids[nb_ports++] = pi;
2938 fwd_ports_ids[nb_fwd_ports++] = pi;
2939 nb_cfg_ports = nb_fwd_ports;
2940 ports[pi].need_setup = 0;
2941 ports[pi].port_status = RTE_PORT_STOPPED;
2943 printf("Port %d is attached. Now total ports is %d\n", pi, nb_ports);
2948 detach_device(struct rte_device *dev)
2953 printf("Device already removed\n");
2957 printf("Removing a device...\n");
2959 RTE_ETH_FOREACH_DEV_OF(sibling, dev) {
2960 if (ports[sibling].port_status != RTE_PORT_CLOSED) {
2961 if (ports[sibling].port_status != RTE_PORT_STOPPED) {
2962 printf("Port %u not stopped\n", sibling);
2965 port_flow_flush(sibling);
2969 if (rte_dev_remove(dev) < 0) {
2970 TESTPMD_LOG(ERR, "Failed to detach device %s\n", dev->name);
2973 remove_invalid_ports();
2975 printf("Device is detached\n");
2976 printf("Now total ports is %d\n", nb_ports);
2982 detach_port_device(portid_t port_id)
2984 if (port_id_is_invalid(port_id, ENABLED_WARN))
2987 if (ports[port_id].port_status != RTE_PORT_CLOSED) {
2988 if (ports[port_id].port_status != RTE_PORT_STOPPED) {
2989 printf("Port not stopped\n");
2992 printf("Port was not closed\n");
2995 detach_device(rte_eth_devices[port_id].device);
2999 detach_devargs(char *identifier)
3001 struct rte_dev_iterator iterator;
3002 struct rte_devargs da;
3005 printf("Removing a device...\n");
3007 memset(&da, 0, sizeof(da));
3008 if (rte_devargs_parsef(&da, "%s", identifier)) {
3009 printf("cannot parse identifier\n");
3015 RTE_ETH_FOREACH_MATCHING_DEV(port_id, identifier, &iterator) {
3016 if (ports[port_id].port_status != RTE_PORT_CLOSED) {
3017 if (ports[port_id].port_status != RTE_PORT_STOPPED) {
3018 printf("Port %u not stopped\n", port_id);
3019 rte_eth_iterator_cleanup(&iterator);
3022 port_flow_flush(port_id);
3026 if (rte_eal_hotplug_remove(da.bus->name, da.name) != 0) {
3027 TESTPMD_LOG(ERR, "Failed to detach device %s(%s)\n",
3028 da.name, da.bus->name);
3032 remove_invalid_ports();
3034 printf("Device %s is detached\n", identifier);
3035 printf("Now total ports is %d\n", nb_ports);
3047 stop_packet_forwarding();
3049 for (i = 0 ; i < RTE_DIM(mempools) ; i++) {
3051 if (mp_alloc_type == MP_ALLOC_ANON)
3052 rte_mempool_mem_iter(mempools[i], dma_unmap_cb,
3056 if (ports != NULL) {
3058 RTE_ETH_FOREACH_DEV(pt_id) {
3059 printf("\nStopping port %d...\n", pt_id);
3063 RTE_ETH_FOREACH_DEV(pt_id) {
3064 printf("\nShutting down port %d...\n", pt_id);
3071 ret = rte_dev_event_monitor_stop();
3074 "fail to stop device event monitor.");
3078 ret = rte_dev_event_callback_unregister(NULL,
3079 dev_event_callback, NULL);
3082 "fail to unregister device event callback.\n");
3086 ret = rte_dev_hotplug_handle_disable();
3089 "fail to disable hotplug handling.\n");
3093 for (i = 0 ; i < RTE_DIM(mempools) ; i++) {
3095 rte_mempool_free(mempools[i]);
3098 printf("\nBye...\n");
3101 typedef void (*cmd_func_t)(void);
3102 struct pmd_test_command {
3103 const char *cmd_name;
3104 cmd_func_t cmd_func;
3107 /* Check the link status of all ports in up to 9s, and print them finally */
3109 check_all_ports_link_status(uint32_t port_mask)
3111 #define CHECK_INTERVAL 100 /* 100ms */
3112 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
3114 uint8_t count, all_ports_up, print_flag = 0;
3115 struct rte_eth_link link;
3117 char link_status[RTE_ETH_LINK_MAX_STR_LEN];
3119 printf("Checking link statuses...\n");
3121 for (count = 0; count <= MAX_CHECK_TIME; count++) {
3123 RTE_ETH_FOREACH_DEV(portid) {
3124 if ((port_mask & (1 << portid)) == 0)
3126 memset(&link, 0, sizeof(link));
3127 ret = rte_eth_link_get_nowait(portid, &link);
3130 if (print_flag == 1)
3131 printf("Port %u link get failed: %s\n",
3132 portid, rte_strerror(-ret));
3135 /* print link status if flag set */
3136 if (print_flag == 1) {
3137 rte_eth_link_to_str(link_status,
3138 sizeof(link_status), &link);
3139 printf("Port %d %s\n", portid, link_status);
3142 /* clear all_ports_up flag if any link down */
3143 if (link.link_status == ETH_LINK_DOWN) {
3148 /* after finally printing all link status, get out */
3149 if (print_flag == 1)
3152 if (all_ports_up == 0) {
3154 rte_delay_ms(CHECK_INTERVAL);
3157 /* set the print_flag if all ports up or timeout */
3158 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
3168 rmv_port_callback(void *arg)
3170 int need_to_start = 0;
3171 int org_no_link_check = no_link_check;
3172 portid_t port_id = (intptr_t)arg;
3173 struct rte_device *dev;
3175 RTE_ETH_VALID_PORTID_OR_RET(port_id);
3177 if (!test_done && port_is_forwarding(port_id)) {
3179 stop_packet_forwarding();
3183 no_link_check = org_no_link_check;
3185 /* Save rte_device pointer before closing ethdev port */
3186 dev = rte_eth_devices[port_id].device;
3187 close_port(port_id);
3188 detach_device(dev); /* might be already removed or have more ports */
3191 start_packet_forwarding(0);
3194 /* This function is used by the interrupt thread */
3196 eth_event_callback(portid_t port_id, enum rte_eth_event_type type, void *param,
3199 RTE_SET_USED(param);
3200 RTE_SET_USED(ret_param);
3202 if (type >= RTE_ETH_EVENT_MAX) {
3203 fprintf(stderr, "\nPort %" PRIu16 ": %s called upon invalid event %d\n",
3204 port_id, __func__, type);
3206 } else if (event_print_mask & (UINT32_C(1) << type)) {
3207 printf("\nPort %" PRIu16 ": %s event\n", port_id,
3208 eth_event_desc[type]);
3213 case RTE_ETH_EVENT_NEW:
3214 ports[port_id].need_setup = 1;
3215 ports[port_id].port_status = RTE_PORT_HANDLING;
3217 case RTE_ETH_EVENT_INTR_RMV:
3218 if (port_id_is_invalid(port_id, DISABLED_WARN))
3220 if (rte_eal_alarm_set(100000,
3221 rmv_port_callback, (void *)(intptr_t)port_id))
3222 fprintf(stderr, "Could not set up deferred device removal\n");
3224 case RTE_ETH_EVENT_DESTROY:
3225 ports[port_id].port_status = RTE_PORT_CLOSED;
3226 printf("Port %u is closed\n", port_id);
3235 register_eth_event_callback(void)
3238 enum rte_eth_event_type event;
3240 for (event = RTE_ETH_EVENT_UNKNOWN;
3241 event < RTE_ETH_EVENT_MAX; event++) {
3242 ret = rte_eth_dev_callback_register(RTE_ETH_ALL,
3247 TESTPMD_LOG(ERR, "Failed to register callback for "
3248 "%s event\n", eth_event_desc[event]);
3256 /* This function is used by the interrupt thread */
3258 dev_event_callback(const char *device_name, enum rte_dev_event_type type,
3259 __rte_unused void *arg)
3264 if (type >= RTE_DEV_EVENT_MAX) {
3265 fprintf(stderr, "%s called upon invalid event %d\n",
3271 case RTE_DEV_EVENT_REMOVE:
3272 RTE_LOG(DEBUG, EAL, "The device: %s has been removed!\n",
3274 ret = rte_eth_dev_get_port_by_name(device_name, &port_id);
3276 RTE_LOG(ERR, EAL, "can not get port by device %s!\n",
3281 * Because the user's callback is invoked in eal interrupt
3282 * callback, the interrupt callback need to be finished before
3283 * it can be unregistered when detaching device. So finish
3284 * callback soon and use a deferred removal to detach device
3285 * is need. It is a workaround, once the device detaching be
3286 * moved into the eal in the future, the deferred removal could
3289 if (rte_eal_alarm_set(100000,
3290 rmv_port_callback, (void *)(intptr_t)port_id))
3292 "Could not set up deferred device removal\n");
3294 case RTE_DEV_EVENT_ADD:
3295 RTE_LOG(ERR, EAL, "The device: %s has been added!\n",
3297 /* TODO: After finish kernel driver binding,
3298 * begin to attach port.
3307 set_tx_queue_stats_mapping_registers(portid_t port_id, struct rte_port *port)
3311 uint8_t mapping_found = 0;
3313 for (i = 0; i < nb_tx_queue_stats_mappings; i++) {
3314 if ((tx_queue_stats_mappings[i].port_id == port_id) &&
3315 (tx_queue_stats_mappings[i].queue_id < nb_txq )) {
3316 diag = rte_eth_dev_set_tx_queue_stats_mapping(port_id,
3317 tx_queue_stats_mappings[i].queue_id,
3318 tx_queue_stats_mappings[i].stats_counter_id);
3325 port->tx_queue_stats_mapping_enabled = 1;
3330 set_rx_queue_stats_mapping_registers(portid_t port_id, struct rte_port *port)
3334 uint8_t mapping_found = 0;
3336 for (i = 0; i < nb_rx_queue_stats_mappings; i++) {
3337 if ((rx_queue_stats_mappings[i].port_id == port_id) &&
3338 (rx_queue_stats_mappings[i].queue_id < nb_rxq )) {
3339 diag = rte_eth_dev_set_rx_queue_stats_mapping(port_id,
3340 rx_queue_stats_mappings[i].queue_id,
3341 rx_queue_stats_mappings[i].stats_counter_id);
3348 port->rx_queue_stats_mapping_enabled = 1;
3353 map_port_queue_stats_mapping_registers(portid_t pi, struct rte_port *port)
3357 diag = set_tx_queue_stats_mapping_registers(pi, port);
3359 if (diag == -ENOTSUP) {
3360 port->tx_queue_stats_mapping_enabled = 0;
3361 printf("TX queue stats mapping not supported port id=%d\n", pi);
3364 rte_exit(EXIT_FAILURE,
3365 "set_tx_queue_stats_mapping_registers "
3366 "failed for port id=%d diag=%d\n",
3370 diag = set_rx_queue_stats_mapping_registers(pi, port);
3372 if (diag == -ENOTSUP) {
3373 port->rx_queue_stats_mapping_enabled = 0;
3374 printf("RX queue stats mapping not supported port id=%d\n", pi);
3377 rte_exit(EXIT_FAILURE,
3378 "set_rx_queue_stats_mapping_registers "
3379 "failed for port id=%d diag=%d\n",
3385 rxtx_port_config(struct rte_port *port)
3390 for (qid = 0; qid < nb_rxq; qid++) {
3391 offloads = port->rx_conf[qid].offloads;
3392 port->rx_conf[qid] = port->dev_info.default_rxconf;
3394 port->rx_conf[qid].offloads = offloads;
3396 /* Check if any Rx parameters have been passed */
3397 if (rx_pthresh != RTE_PMD_PARAM_UNSET)
3398 port->rx_conf[qid].rx_thresh.pthresh = rx_pthresh;
3400 if (rx_hthresh != RTE_PMD_PARAM_UNSET)
3401 port->rx_conf[qid].rx_thresh.hthresh = rx_hthresh;
3403 if (rx_wthresh != RTE_PMD_PARAM_UNSET)
3404 port->rx_conf[qid].rx_thresh.wthresh = rx_wthresh;
3406 if (rx_free_thresh != RTE_PMD_PARAM_UNSET)
3407 port->rx_conf[qid].rx_free_thresh = rx_free_thresh;
3409 if (rx_drop_en != RTE_PMD_PARAM_UNSET)
3410 port->rx_conf[qid].rx_drop_en = rx_drop_en;
3412 port->nb_rx_desc[qid] = nb_rxd;
3415 for (qid = 0; qid < nb_txq; qid++) {
3416 offloads = port->tx_conf[qid].offloads;
3417 port->tx_conf[qid] = port->dev_info.default_txconf;
3419 port->tx_conf[qid].offloads = offloads;
3421 /* Check if any Tx parameters have been passed */
3422 if (tx_pthresh != RTE_PMD_PARAM_UNSET)
3423 port->tx_conf[qid].tx_thresh.pthresh = tx_pthresh;
3425 if (tx_hthresh != RTE_PMD_PARAM_UNSET)
3426 port->tx_conf[qid].tx_thresh.hthresh = tx_hthresh;
3428 if (tx_wthresh != RTE_PMD_PARAM_UNSET)
3429 port->tx_conf[qid].tx_thresh.wthresh = tx_wthresh;
3431 if (tx_rs_thresh != RTE_PMD_PARAM_UNSET)
3432 port->tx_conf[qid].tx_rs_thresh = tx_rs_thresh;
3434 if (tx_free_thresh != RTE_PMD_PARAM_UNSET)
3435 port->tx_conf[qid].tx_free_thresh = tx_free_thresh;
3437 port->nb_tx_desc[qid] = nb_txd;
3442 init_port_config(void)
3445 struct rte_port *port;
3448 RTE_ETH_FOREACH_DEV(pid) {
3450 port->dev_conf.fdir_conf = fdir_conf;
3452 ret = eth_dev_info_get_print_err(pid, &port->dev_info);
3457 port->dev_conf.rx_adv_conf.rss_conf.rss_key = NULL;
3458 port->dev_conf.rx_adv_conf.rss_conf.rss_hf =
3459 rss_hf & port->dev_info.flow_type_rss_offloads;
3461 port->dev_conf.rx_adv_conf.rss_conf.rss_key = NULL;
3462 port->dev_conf.rx_adv_conf.rss_conf.rss_hf = 0;
3465 if (port->dcb_flag == 0) {
3466 if( port->dev_conf.rx_adv_conf.rss_conf.rss_hf != 0)
3467 port->dev_conf.rxmode.mq_mode =
3468 (enum rte_eth_rx_mq_mode)
3469 (rx_mq_mode & ETH_MQ_RX_RSS);
3471 port->dev_conf.rxmode.mq_mode = ETH_MQ_RX_NONE;
3474 rxtx_port_config(port);
3476 ret = eth_macaddr_get_print_err(pid, &port->eth_addr);
3480 map_port_queue_stats_mapping_registers(pid, port);
3481 #if defined RTE_LIBRTE_IXGBE_PMD && defined RTE_LIBRTE_IXGBE_BYPASS
3482 rte_pmd_ixgbe_bypass_init(pid);
3485 if (lsc_interrupt &&
3486 (rte_eth_devices[pid].data->dev_flags &
3487 RTE_ETH_DEV_INTR_LSC))
3488 port->dev_conf.intr_conf.lsc = 1;
3489 if (rmv_interrupt &&
3490 (rte_eth_devices[pid].data->dev_flags &
3491 RTE_ETH_DEV_INTR_RMV))
3492 port->dev_conf.intr_conf.rmv = 1;
3496 void set_port_slave_flag(portid_t slave_pid)
3498 struct rte_port *port;
3500 port = &ports[slave_pid];
3501 port->slave_flag = 1;
3504 void clear_port_slave_flag(portid_t slave_pid)
3506 struct rte_port *port;
3508 port = &ports[slave_pid];
3509 port->slave_flag = 0;
3512 uint8_t port_is_bonding_slave(portid_t slave_pid)
3514 struct rte_port *port;
3516 port = &ports[slave_pid];
3517 if ((rte_eth_devices[slave_pid].data->dev_flags &
3518 RTE_ETH_DEV_BONDED_SLAVE) || (port->slave_flag == 1))
3523 const uint16_t vlan_tags[] = {
3524 0, 1, 2, 3, 4, 5, 6, 7,
3525 8, 9, 10, 11, 12, 13, 14, 15,
3526 16, 17, 18, 19, 20, 21, 22, 23,
3527 24, 25, 26, 27, 28, 29, 30, 31
3531 get_eth_dcb_conf(portid_t pid, struct rte_eth_conf *eth_conf,
3532 enum dcb_mode_enable dcb_mode,
3533 enum rte_eth_nb_tcs num_tcs,
3538 struct rte_eth_rss_conf rss_conf;
3541 * Builds up the correct configuration for dcb+vt based on the vlan tags array
3542 * given above, and the number of traffic classes available for use.
3544 if (dcb_mode == DCB_VT_ENABLED) {
3545 struct rte_eth_vmdq_dcb_conf *vmdq_rx_conf =
3546 ð_conf->rx_adv_conf.vmdq_dcb_conf;
3547 struct rte_eth_vmdq_dcb_tx_conf *vmdq_tx_conf =
3548 ð_conf->tx_adv_conf.vmdq_dcb_tx_conf;
3550 /* VMDQ+DCB RX and TX configurations */
3551 vmdq_rx_conf->enable_default_pool = 0;
3552 vmdq_rx_conf->default_pool = 0;
3553 vmdq_rx_conf->nb_queue_pools =
3554 (num_tcs == ETH_4_TCS ? ETH_32_POOLS : ETH_16_POOLS);
3555 vmdq_tx_conf->nb_queue_pools =
3556 (num_tcs == ETH_4_TCS ? ETH_32_POOLS : ETH_16_POOLS);
3558 vmdq_rx_conf->nb_pool_maps = vmdq_rx_conf->nb_queue_pools;
3559 for (i = 0; i < vmdq_rx_conf->nb_pool_maps; i++) {
3560 vmdq_rx_conf->pool_map[i].vlan_id = vlan_tags[i];
3561 vmdq_rx_conf->pool_map[i].pools =
3562 1 << (i % vmdq_rx_conf->nb_queue_pools);
3564 for (i = 0; i < ETH_DCB_NUM_USER_PRIORITIES; i++) {
3565 vmdq_rx_conf->dcb_tc[i] = i % num_tcs;
3566 vmdq_tx_conf->dcb_tc[i] = i % num_tcs;
3569 /* set DCB mode of RX and TX of multiple queues */
3570 eth_conf->rxmode.mq_mode =
3571 (enum rte_eth_rx_mq_mode)
3572 (rx_mq_mode & ETH_MQ_RX_VMDQ_DCB);
3573 eth_conf->txmode.mq_mode = ETH_MQ_TX_VMDQ_DCB;
3575 struct rte_eth_dcb_rx_conf *rx_conf =
3576 ð_conf->rx_adv_conf.dcb_rx_conf;
3577 struct rte_eth_dcb_tx_conf *tx_conf =
3578 ð_conf->tx_adv_conf.dcb_tx_conf;
3580 memset(&rss_conf, 0, sizeof(struct rte_eth_rss_conf));
3582 rc = rte_eth_dev_rss_hash_conf_get(pid, &rss_conf);
3586 rx_conf->nb_tcs = num_tcs;
3587 tx_conf->nb_tcs = num_tcs;
3589 for (i = 0; i < ETH_DCB_NUM_USER_PRIORITIES; i++) {
3590 rx_conf->dcb_tc[i] = i % num_tcs;
3591 tx_conf->dcb_tc[i] = i % num_tcs;
3594 eth_conf->rxmode.mq_mode =
3595 (enum rte_eth_rx_mq_mode)
3596 (rx_mq_mode & ETH_MQ_RX_DCB_RSS);
3597 eth_conf->rx_adv_conf.rss_conf = rss_conf;
3598 eth_conf->txmode.mq_mode = ETH_MQ_TX_DCB;
3602 eth_conf->dcb_capability_en =
3603 ETH_DCB_PG_SUPPORT | ETH_DCB_PFC_SUPPORT;
3605 eth_conf->dcb_capability_en = ETH_DCB_PG_SUPPORT;
3611 init_port_dcb_config(portid_t pid,
3612 enum dcb_mode_enable dcb_mode,
3613 enum rte_eth_nb_tcs num_tcs,
3616 struct rte_eth_conf port_conf;
3617 struct rte_port *rte_port;
3621 rte_port = &ports[pid];
3623 memset(&port_conf, 0, sizeof(struct rte_eth_conf));
3624 /* Enter DCB configuration status */
3627 port_conf.rxmode = rte_port->dev_conf.rxmode;
3628 port_conf.txmode = rte_port->dev_conf.txmode;
3630 /*set configuration of DCB in vt mode and DCB in non-vt mode*/
3631 retval = get_eth_dcb_conf(pid, &port_conf, dcb_mode, num_tcs, pfc_en);
3634 port_conf.rxmode.offloads |= DEV_RX_OFFLOAD_VLAN_FILTER;
3636 /* re-configure the device . */
3637 retval = rte_eth_dev_configure(pid, nb_rxq, nb_rxq, &port_conf);
3641 retval = eth_dev_info_get_print_err(pid, &rte_port->dev_info);
3645 /* If dev_info.vmdq_pool_base is greater than 0,
3646 * the queue id of vmdq pools is started after pf queues.
3648 if (dcb_mode == DCB_VT_ENABLED &&
3649 rte_port->dev_info.vmdq_pool_base > 0) {
3650 printf("VMDQ_DCB multi-queue mode is nonsensical"
3651 " for port %d.", pid);
3655 /* Assume the ports in testpmd have the same dcb capability
3656 * and has the same number of rxq and txq in dcb mode
3658 if (dcb_mode == DCB_VT_ENABLED) {
3659 if (rte_port->dev_info.max_vfs > 0) {
3660 nb_rxq = rte_port->dev_info.nb_rx_queues;
3661 nb_txq = rte_port->dev_info.nb_tx_queues;
3663 nb_rxq = rte_port->dev_info.max_rx_queues;
3664 nb_txq = rte_port->dev_info.max_tx_queues;
3667 /*if vt is disabled, use all pf queues */
3668 if (rte_port->dev_info.vmdq_pool_base == 0) {
3669 nb_rxq = rte_port->dev_info.max_rx_queues;
3670 nb_txq = rte_port->dev_info.max_tx_queues;
3672 nb_rxq = (queueid_t)num_tcs;
3673 nb_txq = (queueid_t)num_tcs;
3677 rx_free_thresh = 64;
3679 memcpy(&rte_port->dev_conf, &port_conf, sizeof(struct rte_eth_conf));
3681 rxtx_port_config(rte_port);
3683 rte_port->dev_conf.rxmode.offloads |= DEV_RX_OFFLOAD_VLAN_FILTER;
3684 for (i = 0; i < RTE_DIM(vlan_tags); i++)
3685 rx_vft_set(pid, vlan_tags[i], 1);
3687 retval = eth_macaddr_get_print_err(pid, &rte_port->eth_addr);
3691 map_port_queue_stats_mapping_registers(pid, rte_port);
3693 rte_port->dcb_flag = 1;
3703 /* Configuration of Ethernet ports. */
3704 ports = rte_zmalloc("testpmd: ports",
3705 sizeof(struct rte_port) * RTE_MAX_ETHPORTS,
3706 RTE_CACHE_LINE_SIZE);
3707 if (ports == NULL) {
3708 rte_exit(EXIT_FAILURE,
3709 "rte_zmalloc(%d struct rte_port) failed\n",
3712 for (i = 0; i < RTE_MAX_ETHPORTS; i++)
3713 LIST_INIT(&ports[i].flow_tunnel_list);
3714 /* Initialize ports NUMA structures */
3715 memset(port_numa, NUMA_NO_CONFIG, RTE_MAX_ETHPORTS);
3716 memset(rxring_numa, NUMA_NO_CONFIG, RTE_MAX_ETHPORTS);
3717 memset(txring_numa, NUMA_NO_CONFIG, RTE_MAX_ETHPORTS);
3731 const char clr[] = { 27, '[', '2', 'J', '\0' };
3732 const char top_left[] = { 27, '[', '1', ';', '1', 'H', '\0' };
3734 /* Clear screen and move to top left */
3735 printf("%s%s", clr, top_left);
3737 printf("\nPort statistics ====================================");
3738 for (i = 0; i < cur_fwd_config.nb_fwd_ports; i++)
3739 nic_stats_display(fwd_ports_ids[i]);
3745 signal_handler(int signum)
3747 if (signum == SIGINT || signum == SIGTERM) {
3748 printf("\nSignal %d received, preparing to exit...\n",
3750 #ifdef RTE_LIBRTE_PDUMP
3751 /* uninitialize packet capture framework */
3754 #ifdef RTE_LIBRTE_LATENCY_STATS
3755 if (latencystats_enabled != 0)
3756 rte_latencystats_uninit();
3759 /* Set flag to indicate the force termination. */
3761 /* exit with the expected status */
3762 signal(signum, SIG_DFL);
3763 kill(getpid(), signum);
3768 main(int argc, char** argv)
3775 signal(SIGINT, signal_handler);
3776 signal(SIGTERM, signal_handler);
3778 testpmd_logtype = rte_log_register("testpmd");
3779 if (testpmd_logtype < 0)
3780 rte_exit(EXIT_FAILURE, "Cannot register log type");
3781 rte_log_set_level(testpmd_logtype, RTE_LOG_DEBUG);
3783 diag = rte_eal_init(argc, argv);
3785 rte_exit(EXIT_FAILURE, "Cannot init EAL: %s\n",
3786 rte_strerror(rte_errno));
3788 if (rte_eal_process_type() == RTE_PROC_SECONDARY)
3789 rte_exit(EXIT_FAILURE,
3790 "Secondary process type not supported.\n");
3792 ret = register_eth_event_callback();
3794 rte_exit(EXIT_FAILURE, "Cannot register for ethdev events");
3796 #ifdef RTE_LIBRTE_PDUMP
3797 /* initialize packet capture framework */
3802 RTE_ETH_FOREACH_DEV(port_id) {
3803 ports_ids[count] = port_id;
3806 nb_ports = (portid_t) count;
3808 TESTPMD_LOG(WARNING, "No probed ethernet devices\n");
3810 /* allocate port structures, and init them */
3813 set_def_fwd_config();
3815 rte_exit(EXIT_FAILURE, "No cores defined for forwarding\n"
3816 "Check the core mask argument\n");
3818 /* Bitrate/latency stats disabled by default */
3819 #ifdef RTE_LIBRTE_BITRATESTATS
3820 bitrate_enabled = 0;
3822 #ifdef RTE_LIBRTE_LATENCY_STATS
3823 latencystats_enabled = 0;
3826 /* on FreeBSD, mlockall() is disabled by default */
3827 #ifdef RTE_EXEC_ENV_FREEBSD
3836 launch_args_parse(argc, argv);
3838 if (do_mlockall && mlockall(MCL_CURRENT | MCL_FUTURE)) {
3839 TESTPMD_LOG(NOTICE, "mlockall() failed with error \"%s\"\n",
3843 if (tx_first && interactive)
3844 rte_exit(EXIT_FAILURE, "--tx-first cannot be used on "
3845 "interactive mode.\n");
3847 if (tx_first && lsc_interrupt) {
3848 printf("Warning: lsc_interrupt needs to be off when "
3849 " using tx_first. Disabling.\n");
3853 if (!nb_rxq && !nb_txq)
3854 printf("Warning: Either rx or tx queues should be non-zero\n");
3856 if (nb_rxq > 1 && nb_rxq > nb_txq)
3857 printf("Warning: nb_rxq=%d enables RSS configuration, "
3858 "but nb_txq=%d will prevent to fully test it.\n",
3864 ret = rte_dev_hotplug_handle_enable();
3867 "fail to enable hotplug handling.");
3871 ret = rte_dev_event_monitor_start();
3874 "fail to start device event monitoring.");
3878 ret = rte_dev_event_callback_register(NULL,
3879 dev_event_callback, NULL);
3882 "fail to register device event callback\n");
3887 if (!no_device_start && start_port(RTE_PORT_ALL) != 0)
3888 rte_exit(EXIT_FAILURE, "Start ports failed\n");
3890 /* set all ports to promiscuous mode by default */
3891 RTE_ETH_FOREACH_DEV(port_id) {
3892 ret = rte_eth_promiscuous_enable(port_id);
3894 printf("Error during enabling promiscuous mode for port %u: %s - ignore\n",
3895 port_id, rte_strerror(-ret));
3898 /* Init metrics library */
3899 rte_metrics_init(rte_socket_id());
3901 #ifdef RTE_LIBRTE_LATENCY_STATS
3902 if (latencystats_enabled != 0) {
3903 int ret = rte_latencystats_init(1, NULL);
3905 printf("Warning: latencystats init()"
3906 " returned error %d\n", ret);
3907 printf("Latencystats running on lcore %d\n",
3908 latencystats_lcore_id);
3912 /* Setup bitrate stats */
3913 #ifdef RTE_LIBRTE_BITRATESTATS
3914 if (bitrate_enabled != 0) {
3915 bitrate_data = rte_stats_bitrate_create();
3916 if (bitrate_data == NULL)
3917 rte_exit(EXIT_FAILURE,
3918 "Could not allocate bitrate data.\n");
3919 rte_stats_bitrate_reg(bitrate_data);
3923 #ifdef RTE_LIBRTE_CMDLINE
3924 if (strlen(cmdline_filename) != 0)
3925 cmdline_read_from_file(cmdline_filename);
3927 if (interactive == 1) {
3929 printf("Start automatic packet forwarding\n");
3930 start_packet_forwarding(0);
3942 printf("No commandline core given, start packet forwarding\n");
3943 start_packet_forwarding(tx_first);
3944 if (stats_period != 0) {
3945 uint64_t prev_time = 0, cur_time, diff_time = 0;
3946 uint64_t timer_period;
3948 /* Convert to number of cycles */
3949 timer_period = stats_period * rte_get_timer_hz();
3951 while (f_quit == 0) {
3952 cur_time = rte_get_timer_cycles();
3953 diff_time += cur_time - prev_time;
3955 if (diff_time >= timer_period) {
3957 /* Reset the timer */
3960 /* Sleep to avoid unnecessary checks */
3961 prev_time = cur_time;
3966 printf("Press enter to exit\n");
3967 rc = read(0, &c, 1);
3973 ret = rte_eal_cleanup();
3975 rte_exit(EXIT_FAILURE,
3976 "EAL cleanup failed: %s\n", strerror(-ret));
3978 return EXIT_SUCCESS;