1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2017 Intel Corporation
12 #ifndef RTE_EXEC_ENV_WINDOWS
15 #include <sys/types.h>
19 #include <sys/queue.h>
26 #include <rte_common.h>
27 #include <rte_errno.h>
28 #include <rte_byteorder.h>
30 #include <rte_debug.h>
31 #include <rte_cycles.h>
32 #include <rte_memory.h>
33 #include <rte_memcpy.h>
34 #include <rte_launch.h>
36 #include <rte_alarm.h>
37 #include <rte_per_lcore.h>
38 #include <rte_lcore.h>
39 #include <rte_atomic.h>
40 #include <rte_branch_prediction.h>
41 #include <rte_mempool.h>
42 #include <rte_malloc.h>
44 #include <rte_mbuf_pool_ops.h>
45 #include <rte_interrupts.h>
47 #include <rte_ether.h>
48 #include <rte_ethdev.h>
50 #include <rte_string_fns.h>
52 #include <rte_pmd_ixgbe.h>
55 #include <rte_pdump.h>
58 #include <rte_metrics.h>
59 #ifdef RTE_LIB_BITRATESTATS
60 #include <rte_bitrate.h>
62 #ifdef RTE_LIB_LATENCYSTATS
63 #include <rte_latencystats.h>
65 #ifdef RTE_EXEC_ENV_WINDOWS
72 /* FreeBSD may not have MAP_HUGETLB (in fact, it probably doesn't) */
73 #define HUGE_FLAG (0x40000)
75 #define HUGE_FLAG MAP_HUGETLB
78 #ifndef MAP_HUGE_SHIFT
79 /* older kernels (or FreeBSD) will not have this define */
80 #define HUGE_SHIFT (26)
82 #define HUGE_SHIFT MAP_HUGE_SHIFT
85 #define EXTMEM_HEAP_NAME "extmem"
86 #define EXTBUF_ZONE_SIZE RTE_PGSIZE_2M
88 uint16_t verbose_level = 0; /**< Silent by default. */
89 int testpmd_logtype; /**< Log type for testpmd logs */
91 /* use main core for command line ? */
92 uint8_t interactive = 0;
93 uint8_t auto_start = 0;
95 char cmdline_filename[PATH_MAX] = {0};
98 * NUMA support configuration.
99 * When set, the NUMA support attempts to dispatch the allocation of the
100 * RX and TX memory rings, and of the DMA memory buffers (mbufs) for the
101 * probed ports among the CPU sockets 0 and 1.
102 * Otherwise, all memory is allocated from CPU socket 0.
104 uint8_t numa_support = 1; /**< numa enabled by default */
107 * In UMA mode,all memory is allocated from socket 0 if --socket-num is
110 uint8_t socket_num = UMA_NO_CONFIG;
113 * Select mempool allocation type:
114 * - native: use regular DPDK memory
115 * - anon: use regular DPDK memory to create mempool, but populate using
116 * anonymous memory (may not be IOVA-contiguous)
117 * - xmem: use externally allocated hugepage memory
119 uint8_t mp_alloc_type = MP_ALLOC_NATIVE;
122 * Store specified sockets on which memory pool to be used by ports
125 uint8_t port_numa[RTE_MAX_ETHPORTS];
128 * Store specified sockets on which RX ring to be used by ports
131 uint8_t rxring_numa[RTE_MAX_ETHPORTS];
134 * Store specified sockets on which TX ring to be used by ports
137 uint8_t txring_numa[RTE_MAX_ETHPORTS];
140 * Record the Ethernet address of peer target ports to which packets are
142 * Must be instantiated with the ethernet addresses of peer traffic generator
145 struct rte_ether_addr peer_eth_addrs[RTE_MAX_ETHPORTS];
146 portid_t nb_peer_eth_addrs = 0;
149 * Probed Target Environment.
151 struct rte_port *ports; /**< For all probed ethernet ports. */
152 portid_t nb_ports; /**< Number of probed ethernet ports. */
153 struct fwd_lcore **fwd_lcores; /**< For all probed logical cores. */
154 lcoreid_t nb_lcores; /**< Number of probed logical cores. */
156 portid_t ports_ids[RTE_MAX_ETHPORTS]; /**< Store all port ids. */
159 * Test Forwarding Configuration.
160 * nb_fwd_lcores <= nb_cfg_lcores <= nb_lcores
161 * nb_fwd_ports <= nb_cfg_ports <= nb_ports
163 lcoreid_t nb_cfg_lcores; /**< Number of configured logical cores. */
164 lcoreid_t nb_fwd_lcores; /**< Number of forwarding logical cores. */
165 portid_t nb_cfg_ports; /**< Number of configured ports. */
166 portid_t nb_fwd_ports; /**< Number of forwarding ports. */
168 unsigned int fwd_lcores_cpuids[RTE_MAX_LCORE]; /**< CPU ids configuration. */
169 portid_t fwd_ports_ids[RTE_MAX_ETHPORTS]; /**< Port ids configuration. */
171 struct fwd_stream **fwd_streams; /**< For each RX queue of each port. */
172 streamid_t nb_fwd_streams; /**< Is equal to (nb_ports * nb_rxq). */
175 * Forwarding engines.
177 struct fwd_engine * fwd_engines[] = {
187 &five_tuple_swap_fwd_engine,
188 #ifdef RTE_LIBRTE_IEEE1588
189 &ieee1588_fwd_engine,
194 struct rte_mempool *mempools[RTE_MAX_NUMA_NODES * MAX_SEGS_BUFFER_SPLIT];
195 uint16_t mempool_flags;
197 struct fwd_config cur_fwd_config;
198 struct fwd_engine *cur_fwd_eng = &io_fwd_engine; /**< IO mode by default. */
199 uint32_t retry_enabled;
200 uint32_t burst_tx_delay_time = BURST_TX_WAIT_US;
201 uint32_t burst_tx_retry_num = BURST_TX_RETRIES;
203 uint32_t mbuf_data_size_n = 1; /* Number of specified mbuf sizes. */
204 uint16_t mbuf_data_size[MAX_SEGS_BUFFER_SPLIT] = {
205 DEFAULT_MBUF_DATA_SIZE
206 }; /**< Mbuf data space size. */
207 uint32_t param_total_num_mbufs = 0; /**< number of mbufs in all pools - if
208 * specified on command-line. */
209 uint16_t stats_period; /**< Period to show statistics (disabled by default) */
212 * In container, it cannot terminate the process which running with 'stats-period'
213 * option. Set flag to exit stats period loop after received SIGINT/SIGTERM.
218 * Configuration of packet segments used to scatter received packets
219 * if some of split features is configured.
221 uint16_t rx_pkt_seg_lengths[MAX_SEGS_BUFFER_SPLIT];
222 uint8_t rx_pkt_nb_segs; /**< Number of segments to split */
223 uint16_t rx_pkt_seg_offsets[MAX_SEGS_BUFFER_SPLIT];
224 uint8_t rx_pkt_nb_offs; /**< Number of specified offsets */
227 * Configuration of packet segments used by the "txonly" processing engine.
229 uint16_t tx_pkt_length = TXONLY_DEF_PACKET_LEN; /**< TXONLY packet length. */
230 uint16_t tx_pkt_seg_lengths[RTE_MAX_SEGS_PER_PKT] = {
231 TXONLY_DEF_PACKET_LEN,
233 uint8_t tx_pkt_nb_segs = 1; /**< Number of segments in TXONLY packets */
235 enum tx_pkt_split tx_pkt_split = TX_PKT_SPLIT_OFF;
236 /**< Split policy for packets to TX. */
238 uint8_t txonly_multi_flow;
239 /**< Whether multiple flows are generated in TXONLY mode. */
241 uint32_t tx_pkt_times_inter;
242 /**< Timings for send scheduling in TXONLY mode, time between bursts. */
244 uint32_t tx_pkt_times_intra;
245 /**< Timings for send scheduling in TXONLY mode, time between packets. */
247 uint16_t nb_pkt_per_burst = DEF_PKT_BURST; /**< Number of packets per burst. */
248 uint16_t nb_pkt_flowgen_clones; /**< Number of Tx packet clones to send in flowgen mode. */
249 int nb_flows_flowgen = 1024; /**< Number of flows in flowgen mode. */
250 uint16_t mb_mempool_cache = DEF_MBUF_CACHE; /**< Size of mbuf mempool cache. */
252 /* current configuration is in DCB or not,0 means it is not in DCB mode */
253 uint8_t dcb_config = 0;
256 * Configurable number of RX/TX queues.
258 queueid_t nb_hairpinq; /**< Number of hairpin queues per port. */
259 queueid_t nb_rxq = 1; /**< Number of RX queues per port. */
260 queueid_t nb_txq = 1; /**< Number of TX queues per port. */
263 * Configurable number of RX/TX ring descriptors.
264 * Defaults are supplied by drivers via ethdev.
266 #define RTE_TEST_RX_DESC_DEFAULT 0
267 #define RTE_TEST_TX_DESC_DEFAULT 0
268 uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT; /**< Number of RX descriptors. */
269 uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT; /**< Number of TX descriptors. */
271 #define RTE_PMD_PARAM_UNSET -1
273 * Configurable values of RX and TX ring threshold registers.
276 int8_t rx_pthresh = RTE_PMD_PARAM_UNSET;
277 int8_t rx_hthresh = RTE_PMD_PARAM_UNSET;
278 int8_t rx_wthresh = RTE_PMD_PARAM_UNSET;
280 int8_t tx_pthresh = RTE_PMD_PARAM_UNSET;
281 int8_t tx_hthresh = RTE_PMD_PARAM_UNSET;
282 int8_t tx_wthresh = RTE_PMD_PARAM_UNSET;
285 * Configurable value of RX free threshold.
287 int16_t rx_free_thresh = RTE_PMD_PARAM_UNSET;
290 * Configurable value of RX drop enable.
292 int8_t rx_drop_en = RTE_PMD_PARAM_UNSET;
295 * Configurable value of TX free threshold.
297 int16_t tx_free_thresh = RTE_PMD_PARAM_UNSET;
300 * Configurable value of TX RS bit threshold.
302 int16_t tx_rs_thresh = RTE_PMD_PARAM_UNSET;
305 * Configurable value of buffered packets before sending.
307 uint16_t noisy_tx_sw_bufsz;
310 * Configurable value of packet buffer timeout.
312 uint16_t noisy_tx_sw_buf_flush_time;
315 * Configurable value for size of VNF internal memory area
316 * used for simulating noisy neighbour behaviour
318 uint64_t noisy_lkup_mem_sz;
321 * Configurable value of number of random writes done in
322 * VNF simulation memory area.
324 uint64_t noisy_lkup_num_writes;
327 * Configurable value of number of random reads done in
328 * VNF simulation memory area.
330 uint64_t noisy_lkup_num_reads;
333 * Configurable value of number of random reads/writes done in
334 * VNF simulation memory area.
336 uint64_t noisy_lkup_num_reads_writes;
339 * Receive Side Scaling (RSS) configuration.
341 uint64_t rss_hf = ETH_RSS_IP; /* RSS IP by default. */
344 * Port topology configuration
346 uint16_t port_topology = PORT_TOPOLOGY_PAIRED; /* Ports are paired by default */
349 * Avoids to flush all the RX streams before starts forwarding.
351 uint8_t no_flush_rx = 0; /* flush by default */
354 * Flow API isolated mode.
356 uint8_t flow_isolate_all;
359 * Avoids to check link status when starting/stopping a port.
361 uint8_t no_link_check = 0; /* check by default */
364 * Don't automatically start all ports in interactive mode.
366 uint8_t no_device_start = 0;
369 * Enable link status change notification
371 uint8_t lsc_interrupt = 1; /* enabled by default */
374 * Enable device removal notification.
376 uint8_t rmv_interrupt = 1; /* enabled by default */
378 uint8_t hot_plug = 0; /**< hotplug disabled by default. */
380 /* After attach, port setup is called on event or by iterator */
381 bool setup_on_probe_event = true;
383 /* Clear ptypes on port initialization. */
384 uint8_t clear_ptypes = true;
386 /* Hairpin ports configuration mode. */
387 uint16_t hairpin_mode;
389 /* Pretty printing of ethdev events */
390 static const char * const eth_event_desc[] = {
391 [RTE_ETH_EVENT_UNKNOWN] = "unknown",
392 [RTE_ETH_EVENT_INTR_LSC] = "link state change",
393 [RTE_ETH_EVENT_QUEUE_STATE] = "queue state",
394 [RTE_ETH_EVENT_INTR_RESET] = "reset",
395 [RTE_ETH_EVENT_VF_MBOX] = "VF mbox",
396 [RTE_ETH_EVENT_IPSEC] = "IPsec",
397 [RTE_ETH_EVENT_MACSEC] = "MACsec",
398 [RTE_ETH_EVENT_INTR_RMV] = "device removal",
399 [RTE_ETH_EVENT_NEW] = "device probed",
400 [RTE_ETH_EVENT_DESTROY] = "device released",
401 [RTE_ETH_EVENT_FLOW_AGED] = "flow aged",
402 [RTE_ETH_EVENT_MAX] = NULL,
406 * Display or mask ether events
407 * Default to all events except VF_MBOX
409 uint32_t event_print_mask = (UINT32_C(1) << RTE_ETH_EVENT_UNKNOWN) |
410 (UINT32_C(1) << RTE_ETH_EVENT_INTR_LSC) |
411 (UINT32_C(1) << RTE_ETH_EVENT_QUEUE_STATE) |
412 (UINT32_C(1) << RTE_ETH_EVENT_INTR_RESET) |
413 (UINT32_C(1) << RTE_ETH_EVENT_IPSEC) |
414 (UINT32_C(1) << RTE_ETH_EVENT_MACSEC) |
415 (UINT32_C(1) << RTE_ETH_EVENT_INTR_RMV) |
416 (UINT32_C(1) << RTE_ETH_EVENT_FLOW_AGED);
418 * Decide if all memory are locked for performance.
423 * NIC bypass mode configuration options.
426 #if defined RTE_NET_IXGBE && defined RTE_LIBRTE_IXGBE_BYPASS
427 /* The NIC bypass watchdog timeout. */
428 uint32_t bypass_timeout = RTE_PMD_IXGBE_BYPASS_TMT_OFF;
432 #ifdef RTE_LIB_LATENCYSTATS
435 * Set when latency stats is enabled in the commandline
437 uint8_t latencystats_enabled;
440 * Lcore ID to serive latency statistics.
442 lcoreid_t latencystats_lcore_id = -1;
447 * Ethernet device configuration.
449 struct rte_eth_rxmode rx_mode = {
450 /* Default maximum frame length.
451 * Zero is converted to "RTE_ETHER_MTU + PMD Ethernet overhead"
457 struct rte_eth_txmode tx_mode = {
458 .offloads = DEV_TX_OFFLOAD_MBUF_FAST_FREE,
461 struct rte_fdir_conf fdir_conf = {
462 .mode = RTE_FDIR_MODE_NONE,
463 .pballoc = RTE_FDIR_PBALLOC_64K,
464 .status = RTE_FDIR_REPORT_STATUS,
466 .vlan_tci_mask = 0xFFEF,
468 .src_ip = 0xFFFFFFFF,
469 .dst_ip = 0xFFFFFFFF,
472 .src_ip = {0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF},
473 .dst_ip = {0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF},
475 .src_port_mask = 0xFFFF,
476 .dst_port_mask = 0xFFFF,
477 .mac_addr_byte_mask = 0xFF,
478 .tunnel_type_mask = 1,
479 .tunnel_id_mask = 0xFFFFFFFF,
484 volatile int test_done = 1; /* stop packet forwarding when set to 1. */
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_LIB_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;
520 * Used to set forced link speed
522 uint32_t eth_link_speed;
525 * ID of the current process in multi-process, used to
526 * configure the queues to be polled.
531 * Number of processes in multi-process, used to
532 * configure the queues to be polled.
534 unsigned int num_procs = 1;
537 eth_dev_configure_mp(uint16_t port_id, uint16_t nb_rx_q, uint16_t nb_tx_q,
538 const struct rte_eth_conf *dev_conf)
540 if (is_proc_primary())
541 return rte_eth_dev_configure(port_id, nb_rx_q, nb_tx_q,
547 eth_dev_start_mp(uint16_t port_id)
549 if (is_proc_primary())
550 return rte_eth_dev_start(port_id);
556 eth_dev_stop_mp(uint16_t port_id)
558 if (is_proc_primary())
559 return rte_eth_dev_stop(port_id);
565 mempool_free_mp(struct rte_mempool *mp)
567 if (is_proc_primary())
568 rte_mempool_free(mp);
572 eth_dev_set_mtu_mp(uint16_t port_id, uint16_t mtu)
574 if (is_proc_primary())
575 return rte_eth_dev_set_mtu(port_id, mtu);
580 /* Forward function declarations */
581 static void setup_attached_port(portid_t pi);
582 static void check_all_ports_link_status(uint32_t port_mask);
583 static int eth_event_callback(portid_t port_id,
584 enum rte_eth_event_type type,
585 void *param, void *ret_param);
586 static void dev_event_callback(const char *device_name,
587 enum rte_dev_event_type type,
591 * Check if all the ports are started.
592 * If yes, return positive value. If not, return zero.
594 static int all_ports_started(void);
596 struct gso_status gso_ports[RTE_MAX_ETHPORTS];
597 uint16_t gso_max_segment_size = RTE_ETHER_MAX_LEN - RTE_ETHER_CRC_LEN;
599 /* Holds the registered mbuf dynamic flags names. */
600 char dynf_names[64][RTE_MBUF_DYN_NAMESIZE];
603 * Helper function to check if socket is already discovered.
604 * If yes, return positive value. If not, return zero.
607 new_socket_id(unsigned int socket_id)
611 for (i = 0; i < num_sockets; i++) {
612 if (socket_ids[i] == socket_id)
619 * Setup default configuration.
622 set_default_fwd_lcores_config(void)
626 unsigned int sock_num;
629 for (i = 0; i < RTE_MAX_LCORE; i++) {
630 if (!rte_lcore_is_enabled(i))
632 sock_num = rte_lcore_to_socket_id(i);
633 if (new_socket_id(sock_num)) {
634 if (num_sockets >= RTE_MAX_NUMA_NODES) {
635 rte_exit(EXIT_FAILURE,
636 "Total sockets greater than %u\n",
639 socket_ids[num_sockets++] = sock_num;
641 if (i == rte_get_main_lcore())
643 fwd_lcores_cpuids[nb_lc++] = i;
645 nb_lcores = (lcoreid_t) nb_lc;
646 nb_cfg_lcores = nb_lcores;
651 set_def_peer_eth_addrs(void)
655 for (i = 0; i < RTE_MAX_ETHPORTS; i++) {
656 peer_eth_addrs[i].addr_bytes[0] = RTE_ETHER_LOCAL_ADMIN_ADDR;
657 peer_eth_addrs[i].addr_bytes[5] = i;
662 set_default_fwd_ports_config(void)
667 RTE_ETH_FOREACH_DEV(pt_id) {
668 fwd_ports_ids[i++] = pt_id;
670 /* Update sockets info according to the attached device */
671 int socket_id = rte_eth_dev_socket_id(pt_id);
672 if (socket_id >= 0 && new_socket_id(socket_id)) {
673 if (num_sockets >= RTE_MAX_NUMA_NODES) {
674 rte_exit(EXIT_FAILURE,
675 "Total sockets greater than %u\n",
678 socket_ids[num_sockets++] = socket_id;
682 nb_cfg_ports = nb_ports;
683 nb_fwd_ports = nb_ports;
687 set_def_fwd_config(void)
689 set_default_fwd_lcores_config();
690 set_def_peer_eth_addrs();
691 set_default_fwd_ports_config();
694 #ifndef RTE_EXEC_ENV_WINDOWS
695 /* extremely pessimistic estimation of memory required to create a mempool */
697 calc_mem_size(uint32_t nb_mbufs, uint32_t mbuf_sz, size_t pgsz, size_t *out)
699 unsigned int n_pages, mbuf_per_pg, leftover;
700 uint64_t total_mem, mbuf_mem, obj_sz;
702 /* there is no good way to predict how much space the mempool will
703 * occupy because it will allocate chunks on the fly, and some of those
704 * will come from default DPDK memory while some will come from our
705 * external memory, so just assume 128MB will be enough for everyone.
707 uint64_t hdr_mem = 128 << 20;
709 /* account for possible non-contiguousness */
710 obj_sz = rte_mempool_calc_obj_size(mbuf_sz, 0, NULL);
712 TESTPMD_LOG(ERR, "Object size is bigger than page size\n");
716 mbuf_per_pg = pgsz / obj_sz;
717 leftover = (nb_mbufs % mbuf_per_pg) > 0;
718 n_pages = (nb_mbufs / mbuf_per_pg) + leftover;
720 mbuf_mem = n_pages * pgsz;
722 total_mem = RTE_ALIGN(hdr_mem + mbuf_mem, pgsz);
724 if (total_mem > SIZE_MAX) {
725 TESTPMD_LOG(ERR, "Memory size too big\n");
728 *out = (size_t)total_mem;
734 pagesz_flags(uint64_t page_sz)
736 /* as per mmap() manpage, all page sizes are log2 of page size
737 * shifted by MAP_HUGE_SHIFT
739 int log2 = rte_log2_u64(page_sz);
741 return (log2 << HUGE_SHIFT);
745 alloc_mem(size_t memsz, size_t pgsz, bool huge)
750 /* allocate anonymous hugepages */
751 flags = MAP_ANONYMOUS | MAP_PRIVATE;
753 flags |= HUGE_FLAG | pagesz_flags(pgsz);
755 addr = mmap(NULL, memsz, PROT_READ | PROT_WRITE, flags, -1, 0);
756 if (addr == MAP_FAILED)
762 struct extmem_param {
766 rte_iova_t *iova_table;
767 unsigned int iova_table_len;
771 create_extmem(uint32_t nb_mbufs, uint32_t mbuf_sz, struct extmem_param *param,
774 uint64_t pgsizes[] = {RTE_PGSIZE_2M, RTE_PGSIZE_1G, /* x86_64, ARM */
775 RTE_PGSIZE_16M, RTE_PGSIZE_16G}; /* POWER */
776 unsigned int cur_page, n_pages, pgsz_idx;
777 size_t mem_sz, cur_pgsz;
778 rte_iova_t *iovas = NULL;
782 for (pgsz_idx = 0; pgsz_idx < RTE_DIM(pgsizes); pgsz_idx++) {
783 /* skip anything that is too big */
784 if (pgsizes[pgsz_idx] > SIZE_MAX)
787 cur_pgsz = pgsizes[pgsz_idx];
789 /* if we were told not to allocate hugepages, override */
791 cur_pgsz = sysconf(_SC_PAGESIZE);
793 ret = calc_mem_size(nb_mbufs, mbuf_sz, cur_pgsz, &mem_sz);
795 TESTPMD_LOG(ERR, "Cannot calculate memory size\n");
799 /* allocate our memory */
800 addr = alloc_mem(mem_sz, cur_pgsz, huge);
802 /* if we couldn't allocate memory with a specified page size,
803 * that doesn't mean we can't do it with other page sizes, so
809 /* store IOVA addresses for every page in this memory area */
810 n_pages = mem_sz / cur_pgsz;
812 iovas = malloc(sizeof(*iovas) * n_pages);
815 TESTPMD_LOG(ERR, "Cannot allocate memory for iova addresses\n");
818 /* lock memory if it's not huge pages */
822 /* populate IOVA addresses */
823 for (cur_page = 0; cur_page < n_pages; cur_page++) {
828 offset = cur_pgsz * cur_page;
829 cur = RTE_PTR_ADD(addr, offset);
831 /* touch the page before getting its IOVA */
832 *(volatile char *)cur = 0;
834 iova = rte_mem_virt2iova(cur);
836 iovas[cur_page] = iova;
841 /* if we couldn't allocate anything */
847 param->pgsz = cur_pgsz;
848 param->iova_table = iovas;
849 param->iova_table_len = n_pages;
856 munmap(addr, mem_sz);
862 setup_extmem(uint32_t nb_mbufs, uint32_t mbuf_sz, bool huge)
864 struct extmem_param param;
867 memset(¶m, 0, sizeof(param));
869 /* check if our heap exists */
870 socket_id = rte_malloc_heap_get_socket(EXTMEM_HEAP_NAME);
872 /* create our heap */
873 ret = rte_malloc_heap_create(EXTMEM_HEAP_NAME);
875 TESTPMD_LOG(ERR, "Cannot create heap\n");
880 ret = create_extmem(nb_mbufs, mbuf_sz, ¶m, huge);
882 TESTPMD_LOG(ERR, "Cannot create memory area\n");
886 /* we now have a valid memory area, so add it to heap */
887 ret = rte_malloc_heap_memory_add(EXTMEM_HEAP_NAME,
888 param.addr, param.len, param.iova_table,
889 param.iova_table_len, param.pgsz);
891 /* when using VFIO, memory is automatically mapped for DMA by EAL */
893 /* not needed any more */
894 free(param.iova_table);
897 TESTPMD_LOG(ERR, "Cannot add memory to heap\n");
898 munmap(param.addr, param.len);
904 TESTPMD_LOG(DEBUG, "Allocated %zuMB of external memory\n",
910 dma_unmap_cb(struct rte_mempool *mp __rte_unused, void *opaque __rte_unused,
911 struct rte_mempool_memhdr *memhdr, unsigned mem_idx __rte_unused)
916 RTE_ETH_FOREACH_DEV(pid) {
917 struct rte_eth_dev_info dev_info;
919 ret = eth_dev_info_get_print_err(pid, &dev_info);
922 "unable to get device info for port %d on addr 0x%p,"
923 "mempool unmapping will not be performed\n",
928 ret = rte_dev_dma_unmap(dev_info.device, memhdr->addr, 0, memhdr->len);
931 "unable to DMA unmap addr 0x%p "
933 memhdr->addr, dev_info.device->name);
936 ret = rte_extmem_unregister(memhdr->addr, memhdr->len);
939 "unable to un-register addr 0x%p\n", memhdr->addr);
944 dma_map_cb(struct rte_mempool *mp __rte_unused, void *opaque __rte_unused,
945 struct rte_mempool_memhdr *memhdr, unsigned mem_idx __rte_unused)
948 size_t page_size = sysconf(_SC_PAGESIZE);
951 ret = rte_extmem_register(memhdr->addr, memhdr->len, NULL, 0,
955 "unable to register addr 0x%p\n", memhdr->addr);
958 RTE_ETH_FOREACH_DEV(pid) {
959 struct rte_eth_dev_info dev_info;
961 ret = eth_dev_info_get_print_err(pid, &dev_info);
964 "unable to get device info for port %d on addr 0x%p,"
965 "mempool mapping will not be performed\n",
969 ret = rte_dev_dma_map(dev_info.device, memhdr->addr, 0, memhdr->len);
972 "unable to DMA map addr 0x%p "
974 memhdr->addr, dev_info.device->name);
981 setup_extbuf(uint32_t nb_mbufs, uint16_t mbuf_sz, unsigned int socket_id,
982 char *pool_name, struct rte_pktmbuf_extmem **ext_mem)
984 struct rte_pktmbuf_extmem *xmem;
985 unsigned int ext_num, zone_num, elt_num;
988 elt_size = RTE_ALIGN_CEIL(mbuf_sz, RTE_CACHE_LINE_SIZE);
989 elt_num = EXTBUF_ZONE_SIZE / elt_size;
990 zone_num = (nb_mbufs + elt_num - 1) / elt_num;
992 xmem = malloc(sizeof(struct rte_pktmbuf_extmem) * zone_num);
994 TESTPMD_LOG(ERR, "Cannot allocate memory for "
995 "external buffer descriptors\n");
999 for (ext_num = 0; ext_num < zone_num; ext_num++) {
1000 struct rte_pktmbuf_extmem *xseg = xmem + ext_num;
1001 const struct rte_memzone *mz;
1002 char mz_name[RTE_MEMZONE_NAMESIZE];
1005 ret = snprintf(mz_name, sizeof(mz_name),
1006 RTE_MEMPOOL_MZ_FORMAT "_xb_%u", pool_name, ext_num);
1007 if (ret < 0 || ret >= (int)sizeof(mz_name)) {
1008 errno = ENAMETOOLONG;
1012 mz = rte_memzone_reserve_aligned(mz_name, EXTBUF_ZONE_SIZE,
1014 RTE_MEMZONE_IOVA_CONTIG |
1016 RTE_MEMZONE_SIZE_HINT_ONLY,
1020 * The caller exits on external buffer creation
1021 * error, so there is no need to free memzones.
1027 xseg->buf_ptr = mz->addr;
1028 xseg->buf_iova = mz->iova;
1029 xseg->buf_len = EXTBUF_ZONE_SIZE;
1030 xseg->elt_size = elt_size;
1032 if (ext_num == 0 && xmem != NULL) {
1041 * Configuration initialisation done once at init time.
1043 static struct rte_mempool *
1044 mbuf_pool_create(uint16_t mbuf_seg_size, unsigned nb_mbuf,
1045 unsigned int socket_id, uint16_t size_idx)
1047 char pool_name[RTE_MEMPOOL_NAMESIZE];
1048 struct rte_mempool *rte_mp = NULL;
1049 #ifndef RTE_EXEC_ENV_WINDOWS
1052 mb_size = sizeof(struct rte_mbuf) + mbuf_seg_size;
1054 mbuf_poolname_build(socket_id, pool_name, sizeof(pool_name), size_idx);
1055 if (!is_proc_primary()) {
1056 rte_mp = rte_mempool_lookup(pool_name);
1058 rte_exit(EXIT_FAILURE,
1059 "Get mbuf pool for socket %u failed: %s\n",
1060 socket_id, rte_strerror(rte_errno));
1065 "create a new mbuf pool <%s>: n=%u, size=%u, socket=%u\n",
1066 pool_name, nb_mbuf, mbuf_seg_size, socket_id);
1068 switch (mp_alloc_type) {
1069 case MP_ALLOC_NATIVE:
1071 /* wrapper to rte_mempool_create() */
1072 TESTPMD_LOG(INFO, "preferred mempool ops selected: %s\n",
1073 rte_mbuf_best_mempool_ops());
1074 rte_mp = rte_pktmbuf_pool_create(pool_name, nb_mbuf,
1075 mb_mempool_cache, 0, mbuf_seg_size, socket_id);
1078 #ifndef RTE_EXEC_ENV_WINDOWS
1081 rte_mp = rte_mempool_create_empty(pool_name, nb_mbuf,
1082 mb_size, (unsigned int) mb_mempool_cache,
1083 sizeof(struct rte_pktmbuf_pool_private),
1084 socket_id, mempool_flags);
1088 if (rte_mempool_populate_anon(rte_mp) == 0) {
1089 rte_mempool_free(rte_mp);
1093 rte_pktmbuf_pool_init(rte_mp, NULL);
1094 rte_mempool_obj_iter(rte_mp, rte_pktmbuf_init, NULL);
1095 rte_mempool_mem_iter(rte_mp, dma_map_cb, NULL);
1099 case MP_ALLOC_XMEM_HUGE:
1102 bool huge = mp_alloc_type == MP_ALLOC_XMEM_HUGE;
1104 if (setup_extmem(nb_mbuf, mbuf_seg_size, huge) < 0)
1105 rte_exit(EXIT_FAILURE, "Could not create external memory\n");
1108 rte_malloc_heap_get_socket(EXTMEM_HEAP_NAME);
1109 if (heap_socket < 0)
1110 rte_exit(EXIT_FAILURE, "Could not get external memory socket ID\n");
1112 TESTPMD_LOG(INFO, "preferred mempool ops selected: %s\n",
1113 rte_mbuf_best_mempool_ops());
1114 rte_mp = rte_pktmbuf_pool_create(pool_name, nb_mbuf,
1115 mb_mempool_cache, 0, mbuf_seg_size,
1122 struct rte_pktmbuf_extmem *ext_mem;
1123 unsigned int ext_num;
1125 ext_num = setup_extbuf(nb_mbuf, mbuf_seg_size,
1126 socket_id, pool_name, &ext_mem);
1128 rte_exit(EXIT_FAILURE,
1129 "Can't create pinned data buffers\n");
1131 TESTPMD_LOG(INFO, "preferred mempool ops selected: %s\n",
1132 rte_mbuf_best_mempool_ops());
1133 rte_mp = rte_pktmbuf_pool_create_extbuf
1134 (pool_name, nb_mbuf, mb_mempool_cache,
1135 0, mbuf_seg_size, socket_id,
1142 rte_exit(EXIT_FAILURE, "Invalid mempool creation mode\n");
1146 #ifndef RTE_EXEC_ENV_WINDOWS
1149 if (rte_mp == NULL) {
1150 rte_exit(EXIT_FAILURE,
1151 "Creation of mbuf pool for socket %u failed: %s\n",
1152 socket_id, rte_strerror(rte_errno));
1153 } else if (verbose_level > 0) {
1154 rte_mempool_dump(stdout, rte_mp);
1160 * Check given socket id is valid or not with NUMA mode,
1161 * if valid, return 0, else return -1
1164 check_socket_id(const unsigned int socket_id)
1166 static int warning_once = 0;
1168 if (new_socket_id(socket_id)) {
1169 if (!warning_once && numa_support)
1171 "Warning: NUMA should be configured manually by using --port-numa-config and --ring-numa-config parameters along with --numa.\n");
1179 * Get the allowed maximum number of RX queues.
1180 * *pid return the port id which has minimal value of
1181 * max_rx_queues in all ports.
1184 get_allowed_max_nb_rxq(portid_t *pid)
1186 queueid_t allowed_max_rxq = RTE_MAX_QUEUES_PER_PORT;
1187 bool max_rxq_valid = false;
1189 struct rte_eth_dev_info dev_info;
1191 RTE_ETH_FOREACH_DEV(pi) {
1192 if (eth_dev_info_get_print_err(pi, &dev_info) != 0)
1195 max_rxq_valid = true;
1196 if (dev_info.max_rx_queues < allowed_max_rxq) {
1197 allowed_max_rxq = dev_info.max_rx_queues;
1201 return max_rxq_valid ? allowed_max_rxq : 0;
1205 * Check input rxq is valid or not.
1206 * If input rxq is not greater than any of maximum number
1207 * of RX queues of all ports, it is valid.
1208 * if valid, return 0, else return -1
1211 check_nb_rxq(queueid_t rxq)
1213 queueid_t allowed_max_rxq;
1216 allowed_max_rxq = get_allowed_max_nb_rxq(&pid);
1217 if (rxq > allowed_max_rxq) {
1219 "Fail: input rxq (%u) can't be greater than max_rx_queues (%u) of port %u\n",
1220 rxq, allowed_max_rxq, pid);
1227 * Get the allowed maximum number of TX queues.
1228 * *pid return the port id which has minimal value of
1229 * max_tx_queues in all ports.
1232 get_allowed_max_nb_txq(portid_t *pid)
1234 queueid_t allowed_max_txq = RTE_MAX_QUEUES_PER_PORT;
1235 bool max_txq_valid = false;
1237 struct rte_eth_dev_info dev_info;
1239 RTE_ETH_FOREACH_DEV(pi) {
1240 if (eth_dev_info_get_print_err(pi, &dev_info) != 0)
1243 max_txq_valid = true;
1244 if (dev_info.max_tx_queues < allowed_max_txq) {
1245 allowed_max_txq = dev_info.max_tx_queues;
1249 return max_txq_valid ? allowed_max_txq : 0;
1253 * Check input txq is valid or not.
1254 * If input txq is not greater than any of maximum number
1255 * of TX queues of all ports, it is valid.
1256 * if valid, return 0, else return -1
1259 check_nb_txq(queueid_t txq)
1261 queueid_t allowed_max_txq;
1264 allowed_max_txq = get_allowed_max_nb_txq(&pid);
1265 if (txq > allowed_max_txq) {
1267 "Fail: input txq (%u) can't be greater than max_tx_queues (%u) of port %u\n",
1268 txq, allowed_max_txq, pid);
1275 * Get the allowed maximum number of RXDs of every rx queue.
1276 * *pid return the port id which has minimal value of
1277 * max_rxd in all queues of all ports.
1280 get_allowed_max_nb_rxd(portid_t *pid)
1282 uint16_t allowed_max_rxd = UINT16_MAX;
1284 struct rte_eth_dev_info dev_info;
1286 RTE_ETH_FOREACH_DEV(pi) {
1287 if (eth_dev_info_get_print_err(pi, &dev_info) != 0)
1290 if (dev_info.rx_desc_lim.nb_max < allowed_max_rxd) {
1291 allowed_max_rxd = dev_info.rx_desc_lim.nb_max;
1295 return allowed_max_rxd;
1299 * Get the allowed minimal number of RXDs of every rx queue.
1300 * *pid return the port id which has minimal value of
1301 * min_rxd in all queues of all ports.
1304 get_allowed_min_nb_rxd(portid_t *pid)
1306 uint16_t allowed_min_rxd = 0;
1308 struct rte_eth_dev_info dev_info;
1310 RTE_ETH_FOREACH_DEV(pi) {
1311 if (eth_dev_info_get_print_err(pi, &dev_info) != 0)
1314 if (dev_info.rx_desc_lim.nb_min > allowed_min_rxd) {
1315 allowed_min_rxd = dev_info.rx_desc_lim.nb_min;
1320 return allowed_min_rxd;
1324 * Check input rxd is valid or not.
1325 * If input rxd is not greater than any of maximum number
1326 * of RXDs of every Rx queues and is not less than any of
1327 * minimal number of RXDs of every Rx queues, it is valid.
1328 * if valid, return 0, else return -1
1331 check_nb_rxd(queueid_t rxd)
1333 uint16_t allowed_max_rxd;
1334 uint16_t allowed_min_rxd;
1337 allowed_max_rxd = get_allowed_max_nb_rxd(&pid);
1338 if (rxd > allowed_max_rxd) {
1340 "Fail: input rxd (%u) can't be greater than max_rxds (%u) of port %u\n",
1341 rxd, allowed_max_rxd, pid);
1345 allowed_min_rxd = get_allowed_min_nb_rxd(&pid);
1346 if (rxd < allowed_min_rxd) {
1348 "Fail: input rxd (%u) can't be less than min_rxds (%u) of port %u\n",
1349 rxd, allowed_min_rxd, pid);
1357 * Get the allowed maximum number of TXDs of every rx queues.
1358 * *pid return the port id which has minimal value of
1359 * max_txd in every tx queue.
1362 get_allowed_max_nb_txd(portid_t *pid)
1364 uint16_t allowed_max_txd = UINT16_MAX;
1366 struct rte_eth_dev_info dev_info;
1368 RTE_ETH_FOREACH_DEV(pi) {
1369 if (eth_dev_info_get_print_err(pi, &dev_info) != 0)
1372 if (dev_info.tx_desc_lim.nb_max < allowed_max_txd) {
1373 allowed_max_txd = dev_info.tx_desc_lim.nb_max;
1377 return allowed_max_txd;
1381 * Get the allowed maximum number of TXDs of every tx queues.
1382 * *pid return the port id which has minimal value of
1383 * min_txd in every tx queue.
1386 get_allowed_min_nb_txd(portid_t *pid)
1388 uint16_t allowed_min_txd = 0;
1390 struct rte_eth_dev_info dev_info;
1392 RTE_ETH_FOREACH_DEV(pi) {
1393 if (eth_dev_info_get_print_err(pi, &dev_info) != 0)
1396 if (dev_info.tx_desc_lim.nb_min > allowed_min_txd) {
1397 allowed_min_txd = dev_info.tx_desc_lim.nb_min;
1402 return allowed_min_txd;
1406 * Check input txd is valid or not.
1407 * If input txd is not greater than any of maximum number
1408 * of TXDs of every Rx queues, it is valid.
1409 * if valid, return 0, else return -1
1412 check_nb_txd(queueid_t txd)
1414 uint16_t allowed_max_txd;
1415 uint16_t allowed_min_txd;
1418 allowed_max_txd = get_allowed_max_nb_txd(&pid);
1419 if (txd > allowed_max_txd) {
1421 "Fail: input txd (%u) can't be greater than max_txds (%u) of port %u\n",
1422 txd, allowed_max_txd, pid);
1426 allowed_min_txd = get_allowed_min_nb_txd(&pid);
1427 if (txd < allowed_min_txd) {
1429 "Fail: input txd (%u) can't be less than min_txds (%u) of port %u\n",
1430 txd, allowed_min_txd, pid);
1438 * Get the allowed maximum number of hairpin queues.
1439 * *pid return the port id which has minimal value of
1440 * max_hairpin_queues in all ports.
1443 get_allowed_max_nb_hairpinq(portid_t *pid)
1445 queueid_t allowed_max_hairpinq = RTE_MAX_QUEUES_PER_PORT;
1447 struct rte_eth_hairpin_cap cap;
1449 RTE_ETH_FOREACH_DEV(pi) {
1450 if (rte_eth_dev_hairpin_capability_get(pi, &cap) != 0) {
1454 if (cap.max_nb_queues < allowed_max_hairpinq) {
1455 allowed_max_hairpinq = cap.max_nb_queues;
1459 return allowed_max_hairpinq;
1463 * Check input hairpin is valid or not.
1464 * If input hairpin is not greater than any of maximum number
1465 * of hairpin queues of all ports, it is valid.
1466 * if valid, return 0, else return -1
1469 check_nb_hairpinq(queueid_t hairpinq)
1471 queueid_t allowed_max_hairpinq;
1474 allowed_max_hairpinq = get_allowed_max_nb_hairpinq(&pid);
1475 if (hairpinq > allowed_max_hairpinq) {
1477 "Fail: input hairpin (%u) can't be greater than max_hairpin_queues (%u) of port %u\n",
1478 hairpinq, allowed_max_hairpinq, pid);
1485 init_config_port_offloads(portid_t pid, uint32_t socket_id)
1487 struct rte_port *port = &ports[pid];
1492 port->dev_conf.txmode = tx_mode;
1493 port->dev_conf.rxmode = rx_mode;
1495 ret = eth_dev_info_get_print_err(pid, &port->dev_info);
1497 rte_exit(EXIT_FAILURE, "rte_eth_dev_info_get() failed\n");
1499 ret = update_jumbo_frame_offload(pid);
1502 "Updating jumbo frame offload failed for port %u\n",
1505 if (!(port->dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE))
1506 port->dev_conf.txmode.offloads &=
1507 ~DEV_TX_OFFLOAD_MBUF_FAST_FREE;
1509 /* Apply Rx offloads configuration */
1510 for (i = 0; i < port->dev_info.max_rx_queues; i++)
1511 port->rx_conf[i].offloads = port->dev_conf.rxmode.offloads;
1512 /* Apply Tx offloads configuration */
1513 for (i = 0; i < port->dev_info.max_tx_queues; i++)
1514 port->tx_conf[i].offloads = port->dev_conf.txmode.offloads;
1517 port->dev_conf.link_speeds = eth_link_speed;
1519 /* set flag to initialize port/queue */
1520 port->need_reconfig = 1;
1521 port->need_reconfig_queues = 1;
1522 port->socket_id = socket_id;
1523 port->tx_metadata = 0;
1526 * Check for maximum number of segments per MTU.
1527 * Accordingly update the mbuf data size.
1529 if (port->dev_info.rx_desc_lim.nb_mtu_seg_max != UINT16_MAX &&
1530 port->dev_info.rx_desc_lim.nb_mtu_seg_max != 0) {
1531 data_size = rx_mode.max_rx_pkt_len /
1532 port->dev_info.rx_desc_lim.nb_mtu_seg_max;
1534 if ((data_size + RTE_PKTMBUF_HEADROOM) > mbuf_data_size[0]) {
1535 mbuf_data_size[0] = data_size + RTE_PKTMBUF_HEADROOM;
1536 TESTPMD_LOG(WARNING,
1537 "Configured mbuf size of the first segment %hu\n",
1547 struct rte_mempool *mbp;
1548 unsigned int nb_mbuf_per_pool;
1550 struct rte_gro_param gro_param;
1553 /* Configuration of logical cores. */
1554 fwd_lcores = rte_zmalloc("testpmd: fwd_lcores",
1555 sizeof(struct fwd_lcore *) * nb_lcores,
1556 RTE_CACHE_LINE_SIZE);
1557 if (fwd_lcores == NULL) {
1558 rte_exit(EXIT_FAILURE, "rte_zmalloc(%d (struct fwd_lcore *)) "
1559 "failed\n", nb_lcores);
1561 for (lc_id = 0; lc_id < nb_lcores; lc_id++) {
1562 fwd_lcores[lc_id] = rte_zmalloc("testpmd: struct fwd_lcore",
1563 sizeof(struct fwd_lcore),
1564 RTE_CACHE_LINE_SIZE);
1565 if (fwd_lcores[lc_id] == NULL) {
1566 rte_exit(EXIT_FAILURE, "rte_zmalloc(struct fwd_lcore) "
1569 fwd_lcores[lc_id]->cpuid_idx = lc_id;
1572 RTE_ETH_FOREACH_DEV(pid) {
1576 socket_id = port_numa[pid];
1577 if (port_numa[pid] == NUMA_NO_CONFIG) {
1578 socket_id = rte_eth_dev_socket_id(pid);
1581 * if socket_id is invalid,
1582 * set to the first available socket.
1584 if (check_socket_id(socket_id) < 0)
1585 socket_id = socket_ids[0];
1588 socket_id = (socket_num == UMA_NO_CONFIG) ?
1591 /* Apply default TxRx configuration for all ports */
1592 init_config_port_offloads(pid, socket_id);
1595 * Create pools of mbuf.
1596 * If NUMA support is disabled, create a single pool of mbuf in
1597 * socket 0 memory by default.
1598 * Otherwise, create a pool of mbuf in the memory of sockets 0 and 1.
1600 * Use the maximum value of nb_rxd and nb_txd here, then nb_rxd and
1601 * nb_txd can be configured at run time.
1603 if (param_total_num_mbufs)
1604 nb_mbuf_per_pool = param_total_num_mbufs;
1606 nb_mbuf_per_pool = RTE_TEST_RX_DESC_MAX +
1607 (nb_lcores * mb_mempool_cache) +
1608 RTE_TEST_TX_DESC_MAX + MAX_PKT_BURST;
1609 nb_mbuf_per_pool *= RTE_MAX_ETHPORTS;
1615 for (i = 0; i < num_sockets; i++)
1616 for (j = 0; j < mbuf_data_size_n; j++)
1617 mempools[i * MAX_SEGS_BUFFER_SPLIT + j] =
1618 mbuf_pool_create(mbuf_data_size[j],
1624 for (i = 0; i < mbuf_data_size_n; i++)
1625 mempools[i] = mbuf_pool_create
1628 socket_num == UMA_NO_CONFIG ?
1634 gso_types = DEV_TX_OFFLOAD_TCP_TSO | DEV_TX_OFFLOAD_VXLAN_TNL_TSO |
1635 DEV_TX_OFFLOAD_GRE_TNL_TSO | DEV_TX_OFFLOAD_UDP_TSO;
1637 * Records which Mbuf pool to use by each logical core, if needed.
1639 for (lc_id = 0; lc_id < nb_lcores; lc_id++) {
1640 mbp = mbuf_pool_find(
1641 rte_lcore_to_socket_id(fwd_lcores_cpuids[lc_id]), 0);
1644 mbp = mbuf_pool_find(0, 0);
1645 fwd_lcores[lc_id]->mbp = mbp;
1646 /* initialize GSO context */
1647 fwd_lcores[lc_id]->gso_ctx.direct_pool = mbp;
1648 fwd_lcores[lc_id]->gso_ctx.indirect_pool = mbp;
1649 fwd_lcores[lc_id]->gso_ctx.gso_types = gso_types;
1650 fwd_lcores[lc_id]->gso_ctx.gso_size = RTE_ETHER_MAX_LEN -
1652 fwd_lcores[lc_id]->gso_ctx.flag = 0;
1657 /* create a gro context for each lcore */
1658 gro_param.gro_types = RTE_GRO_TCP_IPV4;
1659 gro_param.max_flow_num = GRO_MAX_FLUSH_CYCLES;
1660 gro_param.max_item_per_flow = MAX_PKT_BURST;
1661 for (lc_id = 0; lc_id < nb_lcores; lc_id++) {
1662 gro_param.socket_id = rte_lcore_to_socket_id(
1663 fwd_lcores_cpuids[lc_id]);
1664 fwd_lcores[lc_id]->gro_ctx = rte_gro_ctx_create(&gro_param);
1665 if (fwd_lcores[lc_id]->gro_ctx == NULL) {
1666 rte_exit(EXIT_FAILURE,
1667 "rte_gro_ctx_create() failed\n");
1674 reconfig(portid_t new_port_id, unsigned socket_id)
1676 /* Reconfiguration of Ethernet ports. */
1677 init_config_port_offloads(new_port_id, socket_id);
1683 init_fwd_streams(void)
1686 struct rte_port *port;
1687 streamid_t sm_id, nb_fwd_streams_new;
1690 /* set socket id according to numa or not */
1691 RTE_ETH_FOREACH_DEV(pid) {
1693 if (nb_rxq > port->dev_info.max_rx_queues) {
1695 "Fail: nb_rxq(%d) is greater than max_rx_queues(%d)\n",
1696 nb_rxq, port->dev_info.max_rx_queues);
1699 if (nb_txq > port->dev_info.max_tx_queues) {
1701 "Fail: nb_txq(%d) is greater than max_tx_queues(%d)\n",
1702 nb_txq, port->dev_info.max_tx_queues);
1706 if (port_numa[pid] != NUMA_NO_CONFIG)
1707 port->socket_id = port_numa[pid];
1709 port->socket_id = rte_eth_dev_socket_id(pid);
1712 * if socket_id is invalid,
1713 * set to the first available socket.
1715 if (check_socket_id(port->socket_id) < 0)
1716 port->socket_id = socket_ids[0];
1720 if (socket_num == UMA_NO_CONFIG)
1721 port->socket_id = 0;
1723 port->socket_id = socket_num;
1727 q = RTE_MAX(nb_rxq, nb_txq);
1730 "Fail: Cannot allocate fwd streams as number of queues is 0\n");
1733 nb_fwd_streams_new = (streamid_t)(nb_ports * q);
1734 if (nb_fwd_streams_new == nb_fwd_streams)
1737 if (fwd_streams != NULL) {
1738 for (sm_id = 0; sm_id < nb_fwd_streams; sm_id++) {
1739 if (fwd_streams[sm_id] == NULL)
1741 rte_free(fwd_streams[sm_id]);
1742 fwd_streams[sm_id] = NULL;
1744 rte_free(fwd_streams);
1749 nb_fwd_streams = nb_fwd_streams_new;
1750 if (nb_fwd_streams) {
1751 fwd_streams = rte_zmalloc("testpmd: fwd_streams",
1752 sizeof(struct fwd_stream *) * nb_fwd_streams,
1753 RTE_CACHE_LINE_SIZE);
1754 if (fwd_streams == NULL)
1755 rte_exit(EXIT_FAILURE, "rte_zmalloc(%d"
1756 " (struct fwd_stream *)) failed\n",
1759 for (sm_id = 0; sm_id < nb_fwd_streams; sm_id++) {
1760 fwd_streams[sm_id] = rte_zmalloc("testpmd:"
1761 " struct fwd_stream", sizeof(struct fwd_stream),
1762 RTE_CACHE_LINE_SIZE);
1763 if (fwd_streams[sm_id] == NULL)
1764 rte_exit(EXIT_FAILURE, "rte_zmalloc"
1765 "(struct fwd_stream) failed\n");
1773 pkt_burst_stats_display(const char *rx_tx, struct pkt_burst_stats *pbs)
1775 uint64_t total_burst, sburst;
1777 uint64_t burst_stats[4];
1778 uint16_t pktnb_stats[4];
1780 int burst_percent[4], sburstp;
1784 * First compute the total number of packet bursts and the
1785 * two highest numbers of bursts of the same number of packets.
1787 memset(&burst_stats, 0x0, sizeof(burst_stats));
1788 memset(&pktnb_stats, 0x0, sizeof(pktnb_stats));
1790 /* Show stats for 0 burst size always */
1791 total_burst = pbs->pkt_burst_spread[0];
1792 burst_stats[0] = pbs->pkt_burst_spread[0];
1795 /* Find the next 2 burst sizes with highest occurrences. */
1796 for (nb_pkt = 1; nb_pkt < MAX_PKT_BURST; nb_pkt++) {
1797 nb_burst = pbs->pkt_burst_spread[nb_pkt];
1802 total_burst += nb_burst;
1804 if (nb_burst > burst_stats[1]) {
1805 burst_stats[2] = burst_stats[1];
1806 pktnb_stats[2] = pktnb_stats[1];
1807 burst_stats[1] = nb_burst;
1808 pktnb_stats[1] = nb_pkt;
1809 } else if (nb_burst > burst_stats[2]) {
1810 burst_stats[2] = nb_burst;
1811 pktnb_stats[2] = nb_pkt;
1814 if (total_burst == 0)
1817 printf(" %s-bursts : %"PRIu64" [", rx_tx, total_burst);
1818 for (i = 0, sburst = 0, sburstp = 0; i < 4; i++) {
1820 printf("%d%% of other]\n", 100 - sburstp);
1824 sburst += burst_stats[i];
1825 if (sburst == total_burst) {
1826 printf("%d%% of %d pkts]\n",
1827 100 - sburstp, (int) pktnb_stats[i]);
1832 (double)burst_stats[i] / total_burst * 100;
1833 printf("%d%% of %d pkts + ",
1834 burst_percent[i], (int) pktnb_stats[i]);
1835 sburstp += burst_percent[i];
1840 fwd_stream_stats_display(streamid_t stream_id)
1842 struct fwd_stream *fs;
1843 static const char *fwd_top_stats_border = "-------";
1845 fs = fwd_streams[stream_id];
1846 if ((fs->rx_packets == 0) && (fs->tx_packets == 0) &&
1847 (fs->fwd_dropped == 0))
1849 printf("\n %s Forward Stats for RX Port=%2d/Queue=%2d -> "
1850 "TX Port=%2d/Queue=%2d %s\n",
1851 fwd_top_stats_border, fs->rx_port, fs->rx_queue,
1852 fs->tx_port, fs->tx_queue, fwd_top_stats_border);
1853 printf(" RX-packets: %-14"PRIu64" TX-packets: %-14"PRIu64
1854 " TX-dropped: %-14"PRIu64,
1855 fs->rx_packets, fs->tx_packets, fs->fwd_dropped);
1857 /* if checksum mode */
1858 if (cur_fwd_eng == &csum_fwd_engine) {
1859 printf(" RX- bad IP checksum: %-14"PRIu64
1860 " Rx- bad L4 checksum: %-14"PRIu64
1861 " Rx- bad outer L4 checksum: %-14"PRIu64"\n",
1862 fs->rx_bad_ip_csum, fs->rx_bad_l4_csum,
1863 fs->rx_bad_outer_l4_csum);
1864 printf(" RX- bad outer IP checksum: %-14"PRIu64"\n",
1865 fs->rx_bad_outer_ip_csum);
1870 if (record_burst_stats) {
1871 pkt_burst_stats_display("RX", &fs->rx_burst_stats);
1872 pkt_burst_stats_display("TX", &fs->tx_burst_stats);
1877 fwd_stats_display(void)
1879 static const char *fwd_stats_border = "----------------------";
1880 static const char *acc_stats_border = "+++++++++++++++";
1882 struct fwd_stream *rx_stream;
1883 struct fwd_stream *tx_stream;
1884 uint64_t tx_dropped;
1885 uint64_t rx_bad_ip_csum;
1886 uint64_t rx_bad_l4_csum;
1887 uint64_t rx_bad_outer_l4_csum;
1888 uint64_t rx_bad_outer_ip_csum;
1889 } ports_stats[RTE_MAX_ETHPORTS];
1890 uint64_t total_rx_dropped = 0;
1891 uint64_t total_tx_dropped = 0;
1892 uint64_t total_rx_nombuf = 0;
1893 struct rte_eth_stats stats;
1894 uint64_t fwd_cycles = 0;
1895 uint64_t total_recv = 0;
1896 uint64_t total_xmit = 0;
1897 struct rte_port *port;
1902 memset(ports_stats, 0, sizeof(ports_stats));
1904 for (sm_id = 0; sm_id < cur_fwd_config.nb_fwd_streams; sm_id++) {
1905 struct fwd_stream *fs = fwd_streams[sm_id];
1907 if (cur_fwd_config.nb_fwd_streams >
1908 cur_fwd_config.nb_fwd_ports) {
1909 fwd_stream_stats_display(sm_id);
1911 ports_stats[fs->tx_port].tx_stream = fs;
1912 ports_stats[fs->rx_port].rx_stream = fs;
1915 ports_stats[fs->tx_port].tx_dropped += fs->fwd_dropped;
1917 ports_stats[fs->rx_port].rx_bad_ip_csum += fs->rx_bad_ip_csum;
1918 ports_stats[fs->rx_port].rx_bad_l4_csum += fs->rx_bad_l4_csum;
1919 ports_stats[fs->rx_port].rx_bad_outer_l4_csum +=
1920 fs->rx_bad_outer_l4_csum;
1921 ports_stats[fs->rx_port].rx_bad_outer_ip_csum +=
1922 fs->rx_bad_outer_ip_csum;
1924 if (record_core_cycles)
1925 fwd_cycles += fs->core_cycles;
1927 for (i = 0; i < cur_fwd_config.nb_fwd_ports; i++) {
1928 pt_id = fwd_ports_ids[i];
1929 port = &ports[pt_id];
1931 rte_eth_stats_get(pt_id, &stats);
1932 stats.ipackets -= port->stats.ipackets;
1933 stats.opackets -= port->stats.opackets;
1934 stats.ibytes -= port->stats.ibytes;
1935 stats.obytes -= port->stats.obytes;
1936 stats.imissed -= port->stats.imissed;
1937 stats.oerrors -= port->stats.oerrors;
1938 stats.rx_nombuf -= port->stats.rx_nombuf;
1940 total_recv += stats.ipackets;
1941 total_xmit += stats.opackets;
1942 total_rx_dropped += stats.imissed;
1943 total_tx_dropped += ports_stats[pt_id].tx_dropped;
1944 total_tx_dropped += stats.oerrors;
1945 total_rx_nombuf += stats.rx_nombuf;
1947 printf("\n %s Forward statistics for port %-2d %s\n",
1948 fwd_stats_border, pt_id, fwd_stats_border);
1950 printf(" RX-packets: %-14"PRIu64" RX-dropped: %-14"PRIu64
1951 "RX-total: %-"PRIu64"\n", stats.ipackets, stats.imissed,
1952 stats.ipackets + stats.imissed);
1954 if (cur_fwd_eng == &csum_fwd_engine) {
1955 printf(" Bad-ipcsum: %-14"PRIu64
1956 " Bad-l4csum: %-14"PRIu64
1957 "Bad-outer-l4csum: %-14"PRIu64"\n",
1958 ports_stats[pt_id].rx_bad_ip_csum,
1959 ports_stats[pt_id].rx_bad_l4_csum,
1960 ports_stats[pt_id].rx_bad_outer_l4_csum);
1961 printf(" Bad-outer-ipcsum: %-14"PRIu64"\n",
1962 ports_stats[pt_id].rx_bad_outer_ip_csum);
1964 if (stats.ierrors + stats.rx_nombuf > 0) {
1965 printf(" RX-error: %-"PRIu64"\n", stats.ierrors);
1966 printf(" RX-nombufs: %-14"PRIu64"\n", stats.rx_nombuf);
1969 printf(" TX-packets: %-14"PRIu64" TX-dropped: %-14"PRIu64
1970 "TX-total: %-"PRIu64"\n",
1971 stats.opackets, ports_stats[pt_id].tx_dropped,
1972 stats.opackets + ports_stats[pt_id].tx_dropped);
1974 if (record_burst_stats) {
1975 if (ports_stats[pt_id].rx_stream)
1976 pkt_burst_stats_display("RX",
1977 &ports_stats[pt_id].rx_stream->rx_burst_stats);
1978 if (ports_stats[pt_id].tx_stream)
1979 pkt_burst_stats_display("TX",
1980 &ports_stats[pt_id].tx_stream->tx_burst_stats);
1983 printf(" %s--------------------------------%s\n",
1984 fwd_stats_border, fwd_stats_border);
1987 printf("\n %s Accumulated forward statistics for all ports"
1989 acc_stats_border, acc_stats_border);
1990 printf(" RX-packets: %-14"PRIu64" RX-dropped: %-14"PRIu64"RX-total: "
1992 " TX-packets: %-14"PRIu64" TX-dropped: %-14"PRIu64"TX-total: "
1994 total_recv, total_rx_dropped, total_recv + total_rx_dropped,
1995 total_xmit, total_tx_dropped, total_xmit + total_tx_dropped);
1996 if (total_rx_nombuf > 0)
1997 printf(" RX-nombufs: %-14"PRIu64"\n", total_rx_nombuf);
1998 printf(" %s++++++++++++++++++++++++++++++++++++++++++++++"
2000 acc_stats_border, acc_stats_border);
2001 if (record_core_cycles) {
2002 #define CYC_PER_MHZ 1E6
2003 if (total_recv > 0 || total_xmit > 0) {
2004 uint64_t total_pkts = 0;
2005 if (strcmp(cur_fwd_eng->fwd_mode_name, "txonly") == 0 ||
2006 strcmp(cur_fwd_eng->fwd_mode_name, "flowgen") == 0)
2007 total_pkts = total_xmit;
2009 total_pkts = total_recv;
2011 printf("\n CPU cycles/packet=%.2F (total cycles="
2012 "%"PRIu64" / total %s packets=%"PRIu64") at %"PRIu64
2014 (double) fwd_cycles / total_pkts,
2015 fwd_cycles, cur_fwd_eng->fwd_mode_name, total_pkts,
2016 (uint64_t)(rte_get_tsc_hz() / CYC_PER_MHZ));
2022 fwd_stats_reset(void)
2028 for (i = 0; i < cur_fwd_config.nb_fwd_ports; i++) {
2029 pt_id = fwd_ports_ids[i];
2030 rte_eth_stats_get(pt_id, &ports[pt_id].stats);
2032 for (sm_id = 0; sm_id < cur_fwd_config.nb_fwd_streams; sm_id++) {
2033 struct fwd_stream *fs = fwd_streams[sm_id];
2037 fs->fwd_dropped = 0;
2038 fs->rx_bad_ip_csum = 0;
2039 fs->rx_bad_l4_csum = 0;
2040 fs->rx_bad_outer_l4_csum = 0;
2041 fs->rx_bad_outer_ip_csum = 0;
2043 memset(&fs->rx_burst_stats, 0, sizeof(fs->rx_burst_stats));
2044 memset(&fs->tx_burst_stats, 0, sizeof(fs->tx_burst_stats));
2045 fs->core_cycles = 0;
2050 flush_fwd_rx_queues(void)
2052 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
2059 uint64_t prev_tsc = 0, diff_tsc, cur_tsc, timer_tsc = 0;
2060 uint64_t timer_period;
2062 if (num_procs > 1) {
2063 printf("multi-process not support for flushing fwd Rx queues, skip the below lines and return.\n");
2067 /* convert to number of cycles */
2068 timer_period = rte_get_timer_hz(); /* 1 second timeout */
2070 for (j = 0; j < 2; j++) {
2071 for (rxp = 0; rxp < cur_fwd_config.nb_fwd_ports; rxp++) {
2072 for (rxq = 0; rxq < nb_rxq; rxq++) {
2073 port_id = fwd_ports_ids[rxp];
2075 * testpmd can stuck in the below do while loop
2076 * if rte_eth_rx_burst() always returns nonzero
2077 * packets. So timer is added to exit this loop
2078 * after 1sec timer expiry.
2080 prev_tsc = rte_rdtsc();
2082 nb_rx = rte_eth_rx_burst(port_id, rxq,
2083 pkts_burst, MAX_PKT_BURST);
2084 for (i = 0; i < nb_rx; i++)
2085 rte_pktmbuf_free(pkts_burst[i]);
2087 cur_tsc = rte_rdtsc();
2088 diff_tsc = cur_tsc - prev_tsc;
2089 timer_tsc += diff_tsc;
2090 } while ((nb_rx > 0) &&
2091 (timer_tsc < timer_period));
2095 rte_delay_ms(10); /* wait 10 milli-seconds before retrying */
2100 run_pkt_fwd_on_lcore(struct fwd_lcore *fc, packet_fwd_t pkt_fwd)
2102 struct fwd_stream **fsm;
2105 #ifdef RTE_LIB_BITRATESTATS
2106 uint64_t tics_per_1sec;
2107 uint64_t tics_datum;
2108 uint64_t tics_current;
2109 uint16_t i, cnt_ports;
2111 cnt_ports = nb_ports;
2112 tics_datum = rte_rdtsc();
2113 tics_per_1sec = rte_get_timer_hz();
2115 fsm = &fwd_streams[fc->stream_idx];
2116 nb_fs = fc->stream_nb;
2118 for (sm_id = 0; sm_id < nb_fs; sm_id++)
2119 (*pkt_fwd)(fsm[sm_id]);
2120 #ifdef RTE_LIB_BITRATESTATS
2121 if (bitrate_enabled != 0 &&
2122 bitrate_lcore_id == rte_lcore_id()) {
2123 tics_current = rte_rdtsc();
2124 if (tics_current - tics_datum >= tics_per_1sec) {
2125 /* Periodic bitrate calculation */
2126 for (i = 0; i < cnt_ports; i++)
2127 rte_stats_bitrate_calc(bitrate_data,
2129 tics_datum = tics_current;
2133 #ifdef RTE_LIB_LATENCYSTATS
2134 if (latencystats_enabled != 0 &&
2135 latencystats_lcore_id == rte_lcore_id())
2136 rte_latencystats_update();
2139 } while (! fc->stopped);
2143 start_pkt_forward_on_core(void *fwd_arg)
2145 run_pkt_fwd_on_lcore((struct fwd_lcore *) fwd_arg,
2146 cur_fwd_config.fwd_eng->packet_fwd);
2151 * Run the TXONLY packet forwarding engine to send a single burst of packets.
2152 * Used to start communication flows in network loopback test configurations.
2155 run_one_txonly_burst_on_core(void *fwd_arg)
2157 struct fwd_lcore *fwd_lc;
2158 struct fwd_lcore tmp_lcore;
2160 fwd_lc = (struct fwd_lcore *) fwd_arg;
2161 tmp_lcore = *fwd_lc;
2162 tmp_lcore.stopped = 1;
2163 run_pkt_fwd_on_lcore(&tmp_lcore, tx_only_engine.packet_fwd);
2168 * Launch packet forwarding:
2169 * - Setup per-port forwarding context.
2170 * - launch logical cores with their forwarding configuration.
2173 launch_packet_forwarding(lcore_function_t *pkt_fwd_on_lcore)
2175 port_fwd_begin_t port_fwd_begin;
2180 port_fwd_begin = cur_fwd_config.fwd_eng->port_fwd_begin;
2181 if (port_fwd_begin != NULL) {
2182 for (i = 0; i < cur_fwd_config.nb_fwd_ports; i++)
2183 (*port_fwd_begin)(fwd_ports_ids[i]);
2185 for (i = 0; i < cur_fwd_config.nb_fwd_lcores; i++) {
2186 lc_id = fwd_lcores_cpuids[i];
2187 if ((interactive == 0) || (lc_id != rte_lcore_id())) {
2188 fwd_lcores[i]->stopped = 0;
2189 diag = rte_eal_remote_launch(pkt_fwd_on_lcore,
2190 fwd_lcores[i], lc_id);
2193 "launch lcore %u failed - diag=%d\n",
2200 * Launch packet forwarding configuration.
2203 start_packet_forwarding(int with_tx_first)
2205 port_fwd_begin_t port_fwd_begin;
2206 port_fwd_end_t port_fwd_end;
2209 if (strcmp(cur_fwd_eng->fwd_mode_name, "rxonly") == 0 && !nb_rxq)
2210 rte_exit(EXIT_FAILURE, "rxq are 0, cannot use rxonly fwd mode\n");
2212 if (strcmp(cur_fwd_eng->fwd_mode_name, "txonly") == 0 && !nb_txq)
2213 rte_exit(EXIT_FAILURE, "txq are 0, cannot use txonly fwd mode\n");
2215 if ((strcmp(cur_fwd_eng->fwd_mode_name, "rxonly") != 0 &&
2216 strcmp(cur_fwd_eng->fwd_mode_name, "txonly") != 0) &&
2217 (!nb_rxq || !nb_txq))
2218 rte_exit(EXIT_FAILURE,
2219 "Either rxq or txq are 0, cannot use %s fwd mode\n",
2220 cur_fwd_eng->fwd_mode_name);
2222 if (all_ports_started() == 0) {
2223 fprintf(stderr, "Not all ports were started\n");
2226 if (test_done == 0) {
2227 fprintf(stderr, "Packet forwarding already started\n");
2235 flush_fwd_rx_queues();
2237 pkt_fwd_config_display(&cur_fwd_config);
2238 rxtx_config_display();
2241 if (with_tx_first) {
2242 port_fwd_begin = tx_only_engine.port_fwd_begin;
2243 if (port_fwd_begin != NULL) {
2244 for (i = 0; i < cur_fwd_config.nb_fwd_ports; i++)
2245 (*port_fwd_begin)(fwd_ports_ids[i]);
2247 while (with_tx_first--) {
2248 launch_packet_forwarding(
2249 run_one_txonly_burst_on_core);
2250 rte_eal_mp_wait_lcore();
2252 port_fwd_end = tx_only_engine.port_fwd_end;
2253 if (port_fwd_end != NULL) {
2254 for (i = 0; i < cur_fwd_config.nb_fwd_ports; i++)
2255 (*port_fwd_end)(fwd_ports_ids[i]);
2258 launch_packet_forwarding(start_pkt_forward_on_core);
2262 stop_packet_forwarding(void)
2264 port_fwd_end_t port_fwd_end;
2270 fprintf(stderr, "Packet forwarding not started\n");
2273 printf("Telling cores to stop...");
2274 for (lc_id = 0; lc_id < cur_fwd_config.nb_fwd_lcores; lc_id++)
2275 fwd_lcores[lc_id]->stopped = 1;
2276 printf("\nWaiting for lcores to finish...\n");
2277 rte_eal_mp_wait_lcore();
2278 port_fwd_end = cur_fwd_config.fwd_eng->port_fwd_end;
2279 if (port_fwd_end != NULL) {
2280 for (i = 0; i < cur_fwd_config.nb_fwd_ports; i++) {
2281 pt_id = fwd_ports_ids[i];
2282 (*port_fwd_end)(pt_id);
2286 fwd_stats_display();
2288 printf("\nDone.\n");
2293 dev_set_link_up(portid_t pid)
2295 if (rte_eth_dev_set_link_up(pid) < 0)
2296 fprintf(stderr, "\nSet link up fail.\n");
2300 dev_set_link_down(portid_t pid)
2302 if (rte_eth_dev_set_link_down(pid) < 0)
2303 fprintf(stderr, "\nSet link down fail.\n");
2307 all_ports_started(void)
2310 struct rte_port *port;
2312 RTE_ETH_FOREACH_DEV(pi) {
2314 /* Check if there is a port which is not started */
2315 if ((port->port_status != RTE_PORT_STARTED) &&
2316 (port->slave_flag == 0))
2320 /* No port is not started */
2325 port_is_stopped(portid_t port_id)
2327 struct rte_port *port = &ports[port_id];
2329 if ((port->port_status != RTE_PORT_STOPPED) &&
2330 (port->slave_flag == 0))
2336 all_ports_stopped(void)
2340 RTE_ETH_FOREACH_DEV(pi) {
2341 if (!port_is_stopped(pi))
2349 port_is_started(portid_t port_id)
2351 if (port_id_is_invalid(port_id, ENABLED_WARN))
2354 if (ports[port_id].port_status != RTE_PORT_STARTED)
2360 /* Configure the Rx and Tx hairpin queues for the selected port. */
2362 setup_hairpin_queues(portid_t pi, portid_t p_pi, uint16_t cnt_pi)
2365 struct rte_eth_hairpin_conf hairpin_conf = {
2370 struct rte_port *port = &ports[pi];
2371 uint16_t peer_rx_port = pi;
2372 uint16_t peer_tx_port = pi;
2373 uint32_t manual = 1;
2374 uint32_t tx_exp = hairpin_mode & 0x10;
2376 if (!(hairpin_mode & 0xf)) {
2380 } else if (hairpin_mode & 0x1) {
2381 peer_tx_port = rte_eth_find_next_owned_by(pi + 1,
2382 RTE_ETH_DEV_NO_OWNER);
2383 if (peer_tx_port >= RTE_MAX_ETHPORTS)
2384 peer_tx_port = rte_eth_find_next_owned_by(0,
2385 RTE_ETH_DEV_NO_OWNER);
2386 if (p_pi != RTE_MAX_ETHPORTS) {
2387 peer_rx_port = p_pi;
2391 /* Last port will be the peer RX port of the first. */
2392 RTE_ETH_FOREACH_DEV(next_pi)
2393 peer_rx_port = next_pi;
2396 } else if (hairpin_mode & 0x2) {
2398 peer_rx_port = p_pi;
2400 peer_rx_port = rte_eth_find_next_owned_by(pi + 1,
2401 RTE_ETH_DEV_NO_OWNER);
2402 if (peer_rx_port >= RTE_MAX_ETHPORTS)
2405 peer_tx_port = peer_rx_port;
2409 for (qi = nb_txq, i = 0; qi < nb_hairpinq + nb_txq; qi++) {
2410 hairpin_conf.peers[0].port = peer_rx_port;
2411 hairpin_conf.peers[0].queue = i + nb_rxq;
2412 hairpin_conf.manual_bind = !!manual;
2413 hairpin_conf.tx_explicit = !!tx_exp;
2414 diag = rte_eth_tx_hairpin_queue_setup
2415 (pi, qi, nb_txd, &hairpin_conf);
2420 /* Fail to setup rx queue, return */
2421 if (rte_atomic16_cmpset(&(port->port_status),
2423 RTE_PORT_STOPPED) == 0)
2425 "Port %d can not be set back to stopped\n", pi);
2426 fprintf(stderr, "Fail to configure port %d hairpin queues\n",
2428 /* try to reconfigure queues next time */
2429 port->need_reconfig_queues = 1;
2432 for (qi = nb_rxq, i = 0; qi < nb_hairpinq + nb_rxq; qi++) {
2433 hairpin_conf.peers[0].port = peer_tx_port;
2434 hairpin_conf.peers[0].queue = i + nb_txq;
2435 hairpin_conf.manual_bind = !!manual;
2436 hairpin_conf.tx_explicit = !!tx_exp;
2437 diag = rte_eth_rx_hairpin_queue_setup
2438 (pi, qi, nb_rxd, &hairpin_conf);
2443 /* Fail to setup rx queue, return */
2444 if (rte_atomic16_cmpset(&(port->port_status),
2446 RTE_PORT_STOPPED) == 0)
2448 "Port %d can not be set back to stopped\n", pi);
2449 fprintf(stderr, "Fail to configure port %d hairpin queues\n",
2451 /* try to reconfigure queues next time */
2452 port->need_reconfig_queues = 1;
2458 /* Configure the Rx with optional split. */
2460 rx_queue_setup(uint16_t port_id, uint16_t rx_queue_id,
2461 uint16_t nb_rx_desc, unsigned int socket_id,
2462 struct rte_eth_rxconf *rx_conf, struct rte_mempool *mp)
2464 union rte_eth_rxseg rx_useg[MAX_SEGS_BUFFER_SPLIT] = {};
2465 unsigned int i, mp_n;
2468 if (rx_pkt_nb_segs <= 1 ||
2469 (rx_conf->offloads & RTE_ETH_RX_OFFLOAD_BUFFER_SPLIT) == 0) {
2470 rx_conf->rx_seg = NULL;
2471 rx_conf->rx_nseg = 0;
2472 ret = rte_eth_rx_queue_setup(port_id, rx_queue_id,
2473 nb_rx_desc, socket_id,
2477 for (i = 0; i < rx_pkt_nb_segs; i++) {
2478 struct rte_eth_rxseg_split *rx_seg = &rx_useg[i].split;
2479 struct rte_mempool *mpx;
2481 * Use last valid pool for the segments with number
2482 * exceeding the pool index.
2484 mp_n = (i > mbuf_data_size_n) ? mbuf_data_size_n - 1 : i;
2485 mpx = mbuf_pool_find(socket_id, mp_n);
2486 /* Handle zero as mbuf data buffer size. */
2487 rx_seg->length = rx_pkt_seg_lengths[i] ?
2488 rx_pkt_seg_lengths[i] :
2489 mbuf_data_size[mp_n];
2490 rx_seg->offset = i < rx_pkt_nb_offs ?
2491 rx_pkt_seg_offsets[i] : 0;
2492 rx_seg->mp = mpx ? mpx : mp;
2494 rx_conf->rx_nseg = rx_pkt_nb_segs;
2495 rx_conf->rx_seg = rx_useg;
2496 ret = rte_eth_rx_queue_setup(port_id, rx_queue_id, nb_rx_desc,
2497 socket_id, rx_conf, NULL);
2498 rx_conf->rx_seg = NULL;
2499 rx_conf->rx_nseg = 0;
2504 start_port(portid_t pid)
2506 int diag, need_check_link_status = -1;
2508 portid_t p_pi = RTE_MAX_ETHPORTS;
2509 portid_t pl[RTE_MAX_ETHPORTS];
2510 portid_t peer_pl[RTE_MAX_ETHPORTS];
2511 uint16_t cnt_pi = 0;
2512 uint16_t cfg_pi = 0;
2515 struct rte_port *port;
2516 struct rte_eth_hairpin_cap cap;
2518 if (port_id_is_invalid(pid, ENABLED_WARN))
2521 RTE_ETH_FOREACH_DEV(pi) {
2522 if (pid != pi && pid != (portid_t)RTE_PORT_ALL)
2525 need_check_link_status = 0;
2527 if (rte_atomic16_cmpset(&(port->port_status), RTE_PORT_STOPPED,
2528 RTE_PORT_HANDLING) == 0) {
2529 fprintf(stderr, "Port %d is now not stopped\n", pi);
2533 if (port->need_reconfig > 0) {
2534 port->need_reconfig = 0;
2536 if (flow_isolate_all) {
2537 int ret = port_flow_isolate(pi, 1);
2540 "Failed to apply isolated mode on port %d\n",
2545 configure_rxtx_dump_callbacks(0);
2546 printf("Configuring Port %d (socket %u)\n", pi,
2548 if (nb_hairpinq > 0 &&
2549 rte_eth_dev_hairpin_capability_get(pi, &cap)) {
2551 "Port %d doesn't support hairpin queues\n",
2555 /* configure port */
2556 diag = eth_dev_configure_mp(pi, nb_rxq + nb_hairpinq,
2557 nb_txq + nb_hairpinq,
2560 if (rte_atomic16_cmpset(&(port->port_status),
2561 RTE_PORT_HANDLING, RTE_PORT_STOPPED) == 0)
2563 "Port %d can not be set back to stopped\n",
2565 fprintf(stderr, "Fail to configure port %d\n",
2567 /* try to reconfigure port next time */
2568 port->need_reconfig = 1;
2572 if (port->need_reconfig_queues > 0 && is_proc_primary()) {
2573 port->need_reconfig_queues = 0;
2574 /* setup tx queues */
2575 for (qi = 0; qi < nb_txq; qi++) {
2576 if ((numa_support) &&
2577 (txring_numa[pi] != NUMA_NO_CONFIG))
2578 diag = rte_eth_tx_queue_setup(pi, qi,
2579 port->nb_tx_desc[qi],
2581 &(port->tx_conf[qi]));
2583 diag = rte_eth_tx_queue_setup(pi, qi,
2584 port->nb_tx_desc[qi],
2586 &(port->tx_conf[qi]));
2591 /* Fail to setup tx queue, return */
2592 if (rte_atomic16_cmpset(&(port->port_status),
2594 RTE_PORT_STOPPED) == 0)
2596 "Port %d can not be set back to stopped\n",
2599 "Fail to configure port %d tx queues\n",
2601 /* try to reconfigure queues next time */
2602 port->need_reconfig_queues = 1;
2605 for (qi = 0; qi < nb_rxq; qi++) {
2606 /* setup rx queues */
2607 if ((numa_support) &&
2608 (rxring_numa[pi] != NUMA_NO_CONFIG)) {
2609 struct rte_mempool * mp =
2611 (rxring_numa[pi], 0);
2614 "Failed to setup RX queue: No mempool allocation on the socket %d\n",
2619 diag = rx_queue_setup(pi, qi,
2620 port->nb_rx_desc[qi],
2622 &(port->rx_conf[qi]),
2625 struct rte_mempool *mp =
2627 (port->socket_id, 0);
2630 "Failed to setup RX queue: No mempool allocation on the socket %d\n",
2634 diag = rx_queue_setup(pi, qi,
2635 port->nb_rx_desc[qi],
2637 &(port->rx_conf[qi]),
2643 /* Fail to setup rx queue, return */
2644 if (rte_atomic16_cmpset(&(port->port_status),
2646 RTE_PORT_STOPPED) == 0)
2648 "Port %d can not be set back to stopped\n",
2651 "Fail to configure port %d rx queues\n",
2653 /* try to reconfigure queues next time */
2654 port->need_reconfig_queues = 1;
2657 /* setup hairpin queues */
2658 if (setup_hairpin_queues(pi, p_pi, cnt_pi) != 0)
2661 configure_rxtx_dump_callbacks(verbose_level);
2663 diag = rte_eth_dev_set_ptypes(pi, RTE_PTYPE_UNKNOWN,
2667 "Port %d: Failed to disable Ptype parsing\n",
2675 diag = eth_dev_start_mp(pi);
2677 fprintf(stderr, "Fail to start port %d: %s\n",
2678 pi, rte_strerror(-diag));
2680 /* Fail to setup rx queue, return */
2681 if (rte_atomic16_cmpset(&(port->port_status),
2682 RTE_PORT_HANDLING, RTE_PORT_STOPPED) == 0)
2684 "Port %d can not be set back to stopped\n",
2689 if (rte_atomic16_cmpset(&(port->port_status),
2690 RTE_PORT_HANDLING, RTE_PORT_STARTED) == 0)
2691 fprintf(stderr, "Port %d can not be set into started\n",
2694 if (eth_macaddr_get_print_err(pi, &port->eth_addr) == 0)
2695 printf("Port %d: %02X:%02X:%02X:%02X:%02X:%02X\n", pi,
2696 port->eth_addr.addr_bytes[0],
2697 port->eth_addr.addr_bytes[1],
2698 port->eth_addr.addr_bytes[2],
2699 port->eth_addr.addr_bytes[3],
2700 port->eth_addr.addr_bytes[4],
2701 port->eth_addr.addr_bytes[5]);
2703 /* at least one port started, need checking link status */
2704 need_check_link_status = 1;
2709 if (need_check_link_status == 1 && !no_link_check)
2710 check_all_ports_link_status(RTE_PORT_ALL);
2711 else if (need_check_link_status == 0)
2712 fprintf(stderr, "Please stop the ports first\n");
2714 if (hairpin_mode & 0xf) {
2718 /* bind all started hairpin ports */
2719 for (i = 0; i < cfg_pi; i++) {
2721 /* bind current Tx to all peer Rx */
2722 peer_pi = rte_eth_hairpin_get_peer_ports(pi, peer_pl,
2723 RTE_MAX_ETHPORTS, 1);
2726 for (j = 0; j < peer_pi; j++) {
2727 if (!port_is_started(peer_pl[j]))
2729 diag = rte_eth_hairpin_bind(pi, peer_pl[j]);
2732 "Error during binding hairpin Tx port %u to %u: %s\n",
2734 rte_strerror(-diag));
2738 /* bind all peer Tx to current Rx */
2739 peer_pi = rte_eth_hairpin_get_peer_ports(pi, peer_pl,
2740 RTE_MAX_ETHPORTS, 0);
2743 for (j = 0; j < peer_pi; j++) {
2744 if (!port_is_started(peer_pl[j]))
2746 diag = rte_eth_hairpin_bind(peer_pl[j], pi);
2749 "Error during binding hairpin Tx port %u to %u: %s\n",
2751 rte_strerror(-diag));
2763 stop_port(portid_t pid)
2766 struct rte_port *port;
2767 int need_check_link_status = 0;
2768 portid_t peer_pl[RTE_MAX_ETHPORTS];
2771 if (port_id_is_invalid(pid, ENABLED_WARN))
2774 printf("Stopping ports...\n");
2776 RTE_ETH_FOREACH_DEV(pi) {
2777 if (pid != pi && pid != (portid_t)RTE_PORT_ALL)
2780 if (port_is_forwarding(pi) != 0 && test_done == 0) {
2782 "Please remove port %d from forwarding configuration.\n",
2787 if (port_is_bonding_slave(pi)) {
2789 "Please remove port %d from bonded device.\n",
2795 if (rte_atomic16_cmpset(&(port->port_status), RTE_PORT_STARTED,
2796 RTE_PORT_HANDLING) == 0)
2799 if (hairpin_mode & 0xf) {
2802 rte_eth_hairpin_unbind(pi, RTE_MAX_ETHPORTS);
2803 /* unbind all peer Tx from current Rx */
2804 peer_pi = rte_eth_hairpin_get_peer_ports(pi, peer_pl,
2805 RTE_MAX_ETHPORTS, 0);
2808 for (j = 0; j < peer_pi; j++) {
2809 if (!port_is_started(peer_pl[j]))
2811 rte_eth_hairpin_unbind(peer_pl[j], pi);
2815 if (port->flow_list)
2816 port_flow_flush(pi);
2818 if (eth_dev_stop_mp(pi) != 0)
2819 RTE_LOG(ERR, EAL, "rte_eth_dev_stop failed for port %u\n",
2822 if (rte_atomic16_cmpset(&(port->port_status),
2823 RTE_PORT_HANDLING, RTE_PORT_STOPPED) == 0)
2824 fprintf(stderr, "Port %d can not be set into stopped\n",
2826 need_check_link_status = 1;
2828 if (need_check_link_status && !no_link_check)
2829 check_all_ports_link_status(RTE_PORT_ALL);
2835 remove_invalid_ports_in(portid_t *array, portid_t *total)
2838 portid_t new_total = 0;
2840 for (i = 0; i < *total; i++)
2841 if (!port_id_is_invalid(array[i], DISABLED_WARN)) {
2842 array[new_total] = array[i];
2849 remove_invalid_ports(void)
2851 remove_invalid_ports_in(ports_ids, &nb_ports);
2852 remove_invalid_ports_in(fwd_ports_ids, &nb_fwd_ports);
2853 nb_cfg_ports = nb_fwd_ports;
2857 close_port(portid_t pid)
2860 struct rte_port *port;
2862 if (port_id_is_invalid(pid, ENABLED_WARN))
2865 printf("Closing ports...\n");
2867 RTE_ETH_FOREACH_DEV(pi) {
2868 if (pid != pi && pid != (portid_t)RTE_PORT_ALL)
2871 if (port_is_forwarding(pi) != 0 && test_done == 0) {
2873 "Please remove port %d from forwarding configuration.\n",
2878 if (port_is_bonding_slave(pi)) {
2880 "Please remove port %d from bonded device.\n",
2886 if (rte_atomic16_cmpset(&(port->port_status),
2887 RTE_PORT_CLOSED, RTE_PORT_CLOSED) == 1) {
2888 fprintf(stderr, "Port %d is already closed\n", pi);
2892 if (is_proc_primary()) {
2893 port_flow_flush(pi);
2894 rte_eth_dev_close(pi);
2898 remove_invalid_ports();
2903 reset_port(portid_t pid)
2907 struct rte_port *port;
2909 if (port_id_is_invalid(pid, ENABLED_WARN))
2912 if ((pid == (portid_t)RTE_PORT_ALL && !all_ports_stopped()) ||
2913 (pid != (portid_t)RTE_PORT_ALL && !port_is_stopped(pid))) {
2915 "Can not reset port(s), please stop port(s) first.\n");
2919 printf("Resetting ports...\n");
2921 RTE_ETH_FOREACH_DEV(pi) {
2922 if (pid != pi && pid != (portid_t)RTE_PORT_ALL)
2925 if (port_is_forwarding(pi) != 0 && test_done == 0) {
2927 "Please remove port %d from forwarding configuration.\n",
2932 if (port_is_bonding_slave(pi)) {
2934 "Please remove port %d from bonded device.\n",
2939 diag = rte_eth_dev_reset(pi);
2942 port->need_reconfig = 1;
2943 port->need_reconfig_queues = 1;
2945 fprintf(stderr, "Failed to reset port %d. diag=%d\n",
2954 attach_port(char *identifier)
2957 struct rte_dev_iterator iterator;
2959 printf("Attaching a new port...\n");
2961 if (identifier == NULL) {
2962 fprintf(stderr, "Invalid parameters are specified\n");
2966 if (rte_dev_probe(identifier) < 0) {
2967 TESTPMD_LOG(ERR, "Failed to attach port %s\n", identifier);
2971 /* first attach mode: event */
2972 if (setup_on_probe_event) {
2973 /* new ports are detected on RTE_ETH_EVENT_NEW event */
2974 for (pi = 0; pi < RTE_MAX_ETHPORTS; pi++)
2975 if (ports[pi].port_status == RTE_PORT_HANDLING &&
2976 ports[pi].need_setup != 0)
2977 setup_attached_port(pi);
2981 /* second attach mode: iterator */
2982 RTE_ETH_FOREACH_MATCHING_DEV(pi, identifier, &iterator) {
2983 /* setup ports matching the devargs used for probing */
2984 if (port_is_forwarding(pi))
2985 continue; /* port was already attached before */
2986 setup_attached_port(pi);
2991 setup_attached_port(portid_t pi)
2993 unsigned int socket_id;
2996 socket_id = (unsigned)rte_eth_dev_socket_id(pi);
2997 /* if socket_id is invalid, set to the first available socket. */
2998 if (check_socket_id(socket_id) < 0)
2999 socket_id = socket_ids[0];
3000 reconfig(pi, socket_id);
3001 ret = rte_eth_promiscuous_enable(pi);
3004 "Error during enabling promiscuous mode for port %u: %s - ignore\n",
3005 pi, rte_strerror(-ret));
3007 ports_ids[nb_ports++] = pi;
3008 fwd_ports_ids[nb_fwd_ports++] = pi;
3009 nb_cfg_ports = nb_fwd_ports;
3010 ports[pi].need_setup = 0;
3011 ports[pi].port_status = RTE_PORT_STOPPED;
3013 printf("Port %d is attached. Now total ports is %d\n", pi, nb_ports);
3018 detach_device(struct rte_device *dev)
3023 fprintf(stderr, "Device already removed\n");
3027 printf("Removing a device...\n");
3029 RTE_ETH_FOREACH_DEV_OF(sibling, dev) {
3030 if (ports[sibling].port_status != RTE_PORT_CLOSED) {
3031 if (ports[sibling].port_status != RTE_PORT_STOPPED) {
3032 fprintf(stderr, "Port %u not stopped\n",
3036 port_flow_flush(sibling);
3040 if (rte_dev_remove(dev) < 0) {
3041 TESTPMD_LOG(ERR, "Failed to detach device %s\n", dev->name);
3044 remove_invalid_ports();
3046 printf("Device is detached\n");
3047 printf("Now total ports is %d\n", nb_ports);
3053 detach_port_device(portid_t port_id)
3056 struct rte_eth_dev_info dev_info;
3058 if (port_id_is_invalid(port_id, ENABLED_WARN))
3061 if (ports[port_id].port_status != RTE_PORT_CLOSED) {
3062 if (ports[port_id].port_status != RTE_PORT_STOPPED) {
3063 fprintf(stderr, "Port not stopped\n");
3066 fprintf(stderr, "Port was not closed\n");
3069 ret = eth_dev_info_get_print_err(port_id, &dev_info);
3072 "Failed to get device info for port %d, not detaching\n",
3076 detach_device(dev_info.device);
3080 detach_devargs(char *identifier)
3082 struct rte_dev_iterator iterator;
3083 struct rte_devargs da;
3086 printf("Removing a device...\n");
3088 memset(&da, 0, sizeof(da));
3089 if (rte_devargs_parsef(&da, "%s", identifier)) {
3090 fprintf(stderr, "cannot parse identifier\n");
3094 RTE_ETH_FOREACH_MATCHING_DEV(port_id, identifier, &iterator) {
3095 if (ports[port_id].port_status != RTE_PORT_CLOSED) {
3096 if (ports[port_id].port_status != RTE_PORT_STOPPED) {
3097 fprintf(stderr, "Port %u not stopped\n",
3099 rte_eth_iterator_cleanup(&iterator);
3100 rte_devargs_reset(&da);
3103 port_flow_flush(port_id);
3107 if (rte_eal_hotplug_remove(da.bus->name, da.name) != 0) {
3108 TESTPMD_LOG(ERR, "Failed to detach device %s(%s)\n",
3109 da.name, da.bus->name);
3110 rte_devargs_reset(&da);
3114 remove_invalid_ports();
3116 printf("Device %s is detached\n", identifier);
3117 printf("Now total ports is %d\n", nb_ports);
3119 rte_devargs_reset(&da);
3130 stop_packet_forwarding();
3132 #ifndef RTE_EXEC_ENV_WINDOWS
3133 for (i = 0 ; i < RTE_DIM(mempools) ; i++) {
3135 if (mp_alloc_type == MP_ALLOC_ANON)
3136 rte_mempool_mem_iter(mempools[i], dma_unmap_cb,
3141 if (ports != NULL) {
3143 RTE_ETH_FOREACH_DEV(pt_id) {
3144 printf("\nStopping port %d...\n", pt_id);
3148 RTE_ETH_FOREACH_DEV(pt_id) {
3149 printf("\nShutting down port %d...\n", pt_id);
3156 ret = rte_dev_event_monitor_stop();
3159 "fail to stop device event monitor.");
3163 ret = rte_dev_event_callback_unregister(NULL,
3164 dev_event_callback, NULL);
3167 "fail to unregister device event callback.\n");
3171 ret = rte_dev_hotplug_handle_disable();
3174 "fail to disable hotplug handling.\n");
3178 for (i = 0 ; i < RTE_DIM(mempools) ; i++) {
3180 mempool_free_mp(mempools[i]);
3183 printf("\nBye...\n");
3186 typedef void (*cmd_func_t)(void);
3187 struct pmd_test_command {
3188 const char *cmd_name;
3189 cmd_func_t cmd_func;
3192 /* Check the link status of all ports in up to 9s, and print them finally */
3194 check_all_ports_link_status(uint32_t port_mask)
3196 #define CHECK_INTERVAL 100 /* 100ms */
3197 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
3199 uint8_t count, all_ports_up, print_flag = 0;
3200 struct rte_eth_link link;
3202 char link_status[RTE_ETH_LINK_MAX_STR_LEN];
3204 printf("Checking link statuses...\n");
3206 for (count = 0; count <= MAX_CHECK_TIME; count++) {
3208 RTE_ETH_FOREACH_DEV(portid) {
3209 if ((port_mask & (1 << portid)) == 0)
3211 memset(&link, 0, sizeof(link));
3212 ret = rte_eth_link_get_nowait(portid, &link);
3215 if (print_flag == 1)
3217 "Port %u link get failed: %s\n",
3218 portid, rte_strerror(-ret));
3221 /* print link status if flag set */
3222 if (print_flag == 1) {
3223 rte_eth_link_to_str(link_status,
3224 sizeof(link_status), &link);
3225 printf("Port %d %s\n", portid, link_status);
3228 /* clear all_ports_up flag if any link down */
3229 if (link.link_status == ETH_LINK_DOWN) {
3234 /* after finally printing all link status, get out */
3235 if (print_flag == 1)
3238 if (all_ports_up == 0) {
3240 rte_delay_ms(CHECK_INTERVAL);
3243 /* set the print_flag if all ports up or timeout */
3244 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
3254 rmv_port_callback(void *arg)
3256 int need_to_start = 0;
3257 int org_no_link_check = no_link_check;
3258 portid_t port_id = (intptr_t)arg;
3259 struct rte_eth_dev_info dev_info;
3262 RTE_ETH_VALID_PORTID_OR_RET(port_id);
3264 if (!test_done && port_is_forwarding(port_id)) {
3266 stop_packet_forwarding();
3270 no_link_check = org_no_link_check;
3272 ret = eth_dev_info_get_print_err(port_id, &dev_info);
3275 "Failed to get device info for port %d, not detaching\n",
3278 struct rte_device *device = dev_info.device;
3279 close_port(port_id);
3280 detach_device(device); /* might be already removed or have more ports */
3283 start_packet_forwarding(0);
3286 /* This function is used by the interrupt thread */
3288 eth_event_callback(portid_t port_id, enum rte_eth_event_type type, void *param,
3291 RTE_SET_USED(param);
3292 RTE_SET_USED(ret_param);
3294 if (type >= RTE_ETH_EVENT_MAX) {
3296 "\nPort %" PRIu16 ": %s called upon invalid event %d\n",
3297 port_id, __func__, type);
3299 } else if (event_print_mask & (UINT32_C(1) << type)) {
3300 printf("\nPort %" PRIu16 ": %s event\n", port_id,
3301 eth_event_desc[type]);
3306 case RTE_ETH_EVENT_NEW:
3307 ports[port_id].need_setup = 1;
3308 ports[port_id].port_status = RTE_PORT_HANDLING;
3310 case RTE_ETH_EVENT_INTR_RMV:
3311 if (port_id_is_invalid(port_id, DISABLED_WARN))
3313 if (rte_eal_alarm_set(100000,
3314 rmv_port_callback, (void *)(intptr_t)port_id))
3316 "Could not set up deferred device removal\n");
3318 case RTE_ETH_EVENT_DESTROY:
3319 ports[port_id].port_status = RTE_PORT_CLOSED;
3320 printf("Port %u is closed\n", port_id);
3329 register_eth_event_callback(void)
3332 enum rte_eth_event_type event;
3334 for (event = RTE_ETH_EVENT_UNKNOWN;
3335 event < RTE_ETH_EVENT_MAX; event++) {
3336 ret = rte_eth_dev_callback_register(RTE_ETH_ALL,
3341 TESTPMD_LOG(ERR, "Failed to register callback for "
3342 "%s event\n", eth_event_desc[event]);
3350 /* This function is used by the interrupt thread */
3352 dev_event_callback(const char *device_name, enum rte_dev_event_type type,
3353 __rte_unused void *arg)
3358 if (type >= RTE_DEV_EVENT_MAX) {
3359 fprintf(stderr, "%s called upon invalid event %d\n",
3365 case RTE_DEV_EVENT_REMOVE:
3366 RTE_LOG(DEBUG, EAL, "The device: %s has been removed!\n",
3368 ret = rte_eth_dev_get_port_by_name(device_name, &port_id);
3370 RTE_LOG(ERR, EAL, "can not get port by device %s!\n",
3375 * Because the user's callback is invoked in eal interrupt
3376 * callback, the interrupt callback need to be finished before
3377 * it can be unregistered when detaching device. So finish
3378 * callback soon and use a deferred removal to detach device
3379 * is need. It is a workaround, once the device detaching be
3380 * moved into the eal in the future, the deferred removal could
3383 if (rte_eal_alarm_set(100000,
3384 rmv_port_callback, (void *)(intptr_t)port_id))
3386 "Could not set up deferred device removal\n");
3388 case RTE_DEV_EVENT_ADD:
3389 RTE_LOG(ERR, EAL, "The device: %s has been added!\n",
3391 /* TODO: After finish kernel driver binding,
3392 * begin to attach port.
3401 rxtx_port_config(struct rte_port *port)
3406 for (qid = 0; qid < nb_rxq; qid++) {
3407 offloads = port->rx_conf[qid].offloads;
3408 port->rx_conf[qid] = port->dev_info.default_rxconf;
3410 port->rx_conf[qid].offloads = offloads;
3412 /* Check if any Rx parameters have been passed */
3413 if (rx_pthresh != RTE_PMD_PARAM_UNSET)
3414 port->rx_conf[qid].rx_thresh.pthresh = rx_pthresh;
3416 if (rx_hthresh != RTE_PMD_PARAM_UNSET)
3417 port->rx_conf[qid].rx_thresh.hthresh = rx_hthresh;
3419 if (rx_wthresh != RTE_PMD_PARAM_UNSET)
3420 port->rx_conf[qid].rx_thresh.wthresh = rx_wthresh;
3422 if (rx_free_thresh != RTE_PMD_PARAM_UNSET)
3423 port->rx_conf[qid].rx_free_thresh = rx_free_thresh;
3425 if (rx_drop_en != RTE_PMD_PARAM_UNSET)
3426 port->rx_conf[qid].rx_drop_en = rx_drop_en;
3428 port->nb_rx_desc[qid] = nb_rxd;
3431 for (qid = 0; qid < nb_txq; qid++) {
3432 offloads = port->tx_conf[qid].offloads;
3433 port->tx_conf[qid] = port->dev_info.default_txconf;
3435 port->tx_conf[qid].offloads = offloads;
3437 /* Check if any Tx parameters have been passed */
3438 if (tx_pthresh != RTE_PMD_PARAM_UNSET)
3439 port->tx_conf[qid].tx_thresh.pthresh = tx_pthresh;
3441 if (tx_hthresh != RTE_PMD_PARAM_UNSET)
3442 port->tx_conf[qid].tx_thresh.hthresh = tx_hthresh;
3444 if (tx_wthresh != RTE_PMD_PARAM_UNSET)
3445 port->tx_conf[qid].tx_thresh.wthresh = tx_wthresh;
3447 if (tx_rs_thresh != RTE_PMD_PARAM_UNSET)
3448 port->tx_conf[qid].tx_rs_thresh = tx_rs_thresh;
3450 if (tx_free_thresh != RTE_PMD_PARAM_UNSET)
3451 port->tx_conf[qid].tx_free_thresh = tx_free_thresh;
3453 port->nb_tx_desc[qid] = nb_txd;
3458 * Helper function to arrange max_rx_pktlen value and JUMBO_FRAME offload,
3459 * MTU is also aligned if JUMBO_FRAME offload is not set.
3461 * port->dev_info should be set before calling this function.
3463 * return 0 on success, negative on error
3466 update_jumbo_frame_offload(portid_t portid)
3468 struct rte_port *port = &ports[portid];
3469 uint32_t eth_overhead;
3470 uint64_t rx_offloads;
3474 /* Update the max_rx_pkt_len to have MTU as RTE_ETHER_MTU */
3475 if (port->dev_info.max_mtu != UINT16_MAX &&
3476 port->dev_info.max_rx_pktlen > port->dev_info.max_mtu)
3477 eth_overhead = port->dev_info.max_rx_pktlen -
3478 port->dev_info.max_mtu;
3480 eth_overhead = RTE_ETHER_HDR_LEN + RTE_ETHER_CRC_LEN;
3482 rx_offloads = port->dev_conf.rxmode.offloads;
3484 /* Default config value is 0 to use PMD specific overhead */
3485 if (port->dev_conf.rxmode.max_rx_pkt_len == 0)
3486 port->dev_conf.rxmode.max_rx_pkt_len = RTE_ETHER_MTU + eth_overhead;
3488 if (port->dev_conf.rxmode.max_rx_pkt_len <= RTE_ETHER_MTU + eth_overhead) {
3489 rx_offloads &= ~DEV_RX_OFFLOAD_JUMBO_FRAME;
3492 if ((port->dev_info.rx_offload_capa & DEV_RX_OFFLOAD_JUMBO_FRAME) == 0) {
3494 "Frame size (%u) is not supported by port %u\n",
3495 port->dev_conf.rxmode.max_rx_pkt_len,
3499 rx_offloads |= DEV_RX_OFFLOAD_JUMBO_FRAME;
3503 if (rx_offloads != port->dev_conf.rxmode.offloads) {
3506 port->dev_conf.rxmode.offloads = rx_offloads;
3508 /* Apply JUMBO_FRAME offload configuration to Rx queue(s) */
3509 for (qid = 0; qid < port->dev_info.nb_rx_queues; qid++) {
3511 port->rx_conf[qid].offloads |= DEV_RX_OFFLOAD_JUMBO_FRAME;
3513 port->rx_conf[qid].offloads &= ~DEV_RX_OFFLOAD_JUMBO_FRAME;
3517 /* If JUMBO_FRAME is set MTU conversion done by ethdev layer,
3518 * if unset do it here
3520 if ((rx_offloads & DEV_RX_OFFLOAD_JUMBO_FRAME) == 0) {
3521 ret = eth_dev_set_mtu_mp(portid,
3522 port->dev_conf.rxmode.max_rx_pkt_len - eth_overhead);
3525 "Failed to set MTU to %u for port %u\n",
3526 port->dev_conf.rxmode.max_rx_pkt_len - eth_overhead,
3534 init_port_config(void)
3537 struct rte_port *port;
3540 RTE_ETH_FOREACH_DEV(pid) {
3542 port->dev_conf.fdir_conf = fdir_conf;
3544 ret = eth_dev_info_get_print_err(pid, &port->dev_info);
3549 port->dev_conf.rx_adv_conf.rss_conf.rss_key = NULL;
3550 port->dev_conf.rx_adv_conf.rss_conf.rss_hf =
3551 rss_hf & port->dev_info.flow_type_rss_offloads;
3553 port->dev_conf.rx_adv_conf.rss_conf.rss_key = NULL;
3554 port->dev_conf.rx_adv_conf.rss_conf.rss_hf = 0;
3557 if (port->dcb_flag == 0) {
3558 if( port->dev_conf.rx_adv_conf.rss_conf.rss_hf != 0)
3559 port->dev_conf.rxmode.mq_mode =
3560 (enum rte_eth_rx_mq_mode)
3561 (rx_mq_mode & ETH_MQ_RX_RSS);
3563 port->dev_conf.rxmode.mq_mode = ETH_MQ_RX_NONE;
3566 rxtx_port_config(port);
3568 ret = eth_macaddr_get_print_err(pid, &port->eth_addr);
3572 #if defined RTE_NET_IXGBE && defined RTE_LIBRTE_IXGBE_BYPASS
3573 rte_pmd_ixgbe_bypass_init(pid);
3576 if (lsc_interrupt && (*port->dev_info.dev_flags & RTE_ETH_DEV_INTR_LSC))
3577 port->dev_conf.intr_conf.lsc = 1;
3578 if (rmv_interrupt && (*port->dev_info.dev_flags & RTE_ETH_DEV_INTR_RMV))
3579 port->dev_conf.intr_conf.rmv = 1;
3583 void set_port_slave_flag(portid_t slave_pid)
3585 struct rte_port *port;
3587 port = &ports[slave_pid];
3588 port->slave_flag = 1;
3591 void clear_port_slave_flag(portid_t slave_pid)
3593 struct rte_port *port;
3595 port = &ports[slave_pid];
3596 port->slave_flag = 0;
3599 uint8_t port_is_bonding_slave(portid_t slave_pid)
3601 struct rte_port *port;
3602 struct rte_eth_dev_info dev_info;
3605 port = &ports[slave_pid];
3606 ret = eth_dev_info_get_print_err(slave_pid, &dev_info);
3609 "Failed to get device info for port id %d,"
3610 "cannot determine if the port is a bonded slave",
3614 if ((*dev_info.dev_flags & RTE_ETH_DEV_BONDED_SLAVE) || (port->slave_flag == 1))
3619 const uint16_t vlan_tags[] = {
3620 0, 1, 2, 3, 4, 5, 6, 7,
3621 8, 9, 10, 11, 12, 13, 14, 15,
3622 16, 17, 18, 19, 20, 21, 22, 23,
3623 24, 25, 26, 27, 28, 29, 30, 31
3627 get_eth_dcb_conf(portid_t pid, struct rte_eth_conf *eth_conf,
3628 enum dcb_mode_enable dcb_mode,
3629 enum rte_eth_nb_tcs num_tcs,
3634 struct rte_eth_rss_conf rss_conf;
3637 * Builds up the correct configuration for dcb+vt based on the vlan tags array
3638 * given above, and the number of traffic classes available for use.
3640 if (dcb_mode == DCB_VT_ENABLED) {
3641 struct rte_eth_vmdq_dcb_conf *vmdq_rx_conf =
3642 ð_conf->rx_adv_conf.vmdq_dcb_conf;
3643 struct rte_eth_vmdq_dcb_tx_conf *vmdq_tx_conf =
3644 ð_conf->tx_adv_conf.vmdq_dcb_tx_conf;
3646 /* VMDQ+DCB RX and TX configurations */
3647 vmdq_rx_conf->enable_default_pool = 0;
3648 vmdq_rx_conf->default_pool = 0;
3649 vmdq_rx_conf->nb_queue_pools =
3650 (num_tcs == ETH_4_TCS ? ETH_32_POOLS : ETH_16_POOLS);
3651 vmdq_tx_conf->nb_queue_pools =
3652 (num_tcs == ETH_4_TCS ? ETH_32_POOLS : ETH_16_POOLS);
3654 vmdq_rx_conf->nb_pool_maps = vmdq_rx_conf->nb_queue_pools;
3655 for (i = 0; i < vmdq_rx_conf->nb_pool_maps; i++) {
3656 vmdq_rx_conf->pool_map[i].vlan_id = vlan_tags[i];
3657 vmdq_rx_conf->pool_map[i].pools =
3658 1 << (i % vmdq_rx_conf->nb_queue_pools);
3660 for (i = 0; i < ETH_DCB_NUM_USER_PRIORITIES; i++) {
3661 vmdq_rx_conf->dcb_tc[i] = i % num_tcs;
3662 vmdq_tx_conf->dcb_tc[i] = i % num_tcs;
3665 /* set DCB mode of RX and TX of multiple queues */
3666 eth_conf->rxmode.mq_mode =
3667 (enum rte_eth_rx_mq_mode)
3668 (rx_mq_mode & ETH_MQ_RX_VMDQ_DCB);
3669 eth_conf->txmode.mq_mode = ETH_MQ_TX_VMDQ_DCB;
3671 struct rte_eth_dcb_rx_conf *rx_conf =
3672 ð_conf->rx_adv_conf.dcb_rx_conf;
3673 struct rte_eth_dcb_tx_conf *tx_conf =
3674 ð_conf->tx_adv_conf.dcb_tx_conf;
3676 memset(&rss_conf, 0, sizeof(struct rte_eth_rss_conf));
3678 rc = rte_eth_dev_rss_hash_conf_get(pid, &rss_conf);
3682 rx_conf->nb_tcs = num_tcs;
3683 tx_conf->nb_tcs = num_tcs;
3685 for (i = 0; i < ETH_DCB_NUM_USER_PRIORITIES; i++) {
3686 rx_conf->dcb_tc[i] = i % num_tcs;
3687 tx_conf->dcb_tc[i] = i % num_tcs;
3690 eth_conf->rxmode.mq_mode =
3691 (enum rte_eth_rx_mq_mode)
3692 (rx_mq_mode & ETH_MQ_RX_DCB_RSS);
3693 eth_conf->rx_adv_conf.rss_conf = rss_conf;
3694 eth_conf->txmode.mq_mode = ETH_MQ_TX_DCB;
3698 eth_conf->dcb_capability_en =
3699 ETH_DCB_PG_SUPPORT | ETH_DCB_PFC_SUPPORT;
3701 eth_conf->dcb_capability_en = ETH_DCB_PG_SUPPORT;
3707 init_port_dcb_config(portid_t pid,
3708 enum dcb_mode_enable dcb_mode,
3709 enum rte_eth_nb_tcs num_tcs,
3712 struct rte_eth_conf port_conf;
3713 struct rte_port *rte_port;
3717 if (num_procs > 1) {
3718 printf("The multi-process feature doesn't support dcb.\n");
3721 rte_port = &ports[pid];
3723 memset(&port_conf, 0, sizeof(struct rte_eth_conf));
3725 port_conf.rxmode = rte_port->dev_conf.rxmode;
3726 port_conf.txmode = rte_port->dev_conf.txmode;
3728 /*set configuration of DCB in vt mode and DCB in non-vt mode*/
3729 retval = get_eth_dcb_conf(pid, &port_conf, dcb_mode, num_tcs, pfc_en);
3732 port_conf.rxmode.offloads |= DEV_RX_OFFLOAD_VLAN_FILTER;
3734 /* re-configure the device . */
3735 retval = rte_eth_dev_configure(pid, nb_rxq, nb_rxq, &port_conf);
3739 retval = eth_dev_info_get_print_err(pid, &rte_port->dev_info);
3743 /* If dev_info.vmdq_pool_base is greater than 0,
3744 * the queue id of vmdq pools is started after pf queues.
3746 if (dcb_mode == DCB_VT_ENABLED &&
3747 rte_port->dev_info.vmdq_pool_base > 0) {
3749 "VMDQ_DCB multi-queue mode is nonsensical for port %d.\n",
3754 /* Assume the ports in testpmd have the same dcb capability
3755 * and has the same number of rxq and txq in dcb mode
3757 if (dcb_mode == DCB_VT_ENABLED) {
3758 if (rte_port->dev_info.max_vfs > 0) {
3759 nb_rxq = rte_port->dev_info.nb_rx_queues;
3760 nb_txq = rte_port->dev_info.nb_tx_queues;
3762 nb_rxq = rte_port->dev_info.max_rx_queues;
3763 nb_txq = rte_port->dev_info.max_tx_queues;
3766 /*if vt is disabled, use all pf queues */
3767 if (rte_port->dev_info.vmdq_pool_base == 0) {
3768 nb_rxq = rte_port->dev_info.max_rx_queues;
3769 nb_txq = rte_port->dev_info.max_tx_queues;
3771 nb_rxq = (queueid_t)num_tcs;
3772 nb_txq = (queueid_t)num_tcs;
3776 rx_free_thresh = 64;
3778 memcpy(&rte_port->dev_conf, &port_conf, sizeof(struct rte_eth_conf));
3780 rxtx_port_config(rte_port);
3782 rte_port->dev_conf.rxmode.offloads |= DEV_RX_OFFLOAD_VLAN_FILTER;
3783 for (i = 0; i < RTE_DIM(vlan_tags); i++)
3784 rx_vft_set(pid, vlan_tags[i], 1);
3786 retval = eth_macaddr_get_print_err(pid, &rte_port->eth_addr);
3790 rte_port->dcb_flag = 1;
3792 /* Enter DCB configuration status */
3803 /* Configuration of Ethernet ports. */
3804 ports = rte_zmalloc("testpmd: ports",
3805 sizeof(struct rte_port) * RTE_MAX_ETHPORTS,
3806 RTE_CACHE_LINE_SIZE);
3807 if (ports == NULL) {
3808 rte_exit(EXIT_FAILURE,
3809 "rte_zmalloc(%d struct rte_port) failed\n",
3812 for (i = 0; i < RTE_MAX_ETHPORTS; i++)
3813 LIST_INIT(&ports[i].flow_tunnel_list);
3814 /* Initialize ports NUMA structures */
3815 memset(port_numa, NUMA_NO_CONFIG, RTE_MAX_ETHPORTS);
3816 memset(rxring_numa, NUMA_NO_CONFIG, RTE_MAX_ETHPORTS);
3817 memset(txring_numa, NUMA_NO_CONFIG, RTE_MAX_ETHPORTS);
3831 const char clr[] = { 27, '[', '2', 'J', '\0' };
3832 const char top_left[] = { 27, '[', '1', ';', '1', 'H', '\0' };
3834 /* Clear screen and move to top left */
3835 printf("%s%s", clr, top_left);
3837 printf("\nPort statistics ====================================");
3838 for (i = 0; i < cur_fwd_config.nb_fwd_ports; i++)
3839 nic_stats_display(fwd_ports_ids[i]);
3845 signal_handler(int signum)
3847 if (signum == SIGINT || signum == SIGTERM) {
3848 fprintf(stderr, "\nSignal %d received, preparing to exit...\n",
3850 #ifdef RTE_LIB_PDUMP
3851 /* uninitialize packet capture framework */
3854 #ifdef RTE_LIB_LATENCYSTATS
3855 if (latencystats_enabled != 0)
3856 rte_latencystats_uninit();
3859 /* Set flag to indicate the force termination. */
3861 /* exit with the expected status */
3862 #ifndef RTE_EXEC_ENV_WINDOWS
3863 signal(signum, SIG_DFL);
3864 kill(getpid(), signum);
3870 main(int argc, char** argv)
3877 signal(SIGINT, signal_handler);
3878 signal(SIGTERM, signal_handler);
3880 testpmd_logtype = rte_log_register("testpmd");
3881 if (testpmd_logtype < 0)
3882 rte_exit(EXIT_FAILURE, "Cannot register log type");
3883 rte_log_set_level(testpmd_logtype, RTE_LOG_DEBUG);
3885 diag = rte_eal_init(argc, argv);
3887 rte_exit(EXIT_FAILURE, "Cannot init EAL: %s\n",
3888 rte_strerror(rte_errno));
3890 ret = register_eth_event_callback();
3892 rte_exit(EXIT_FAILURE, "Cannot register for ethdev events");
3894 #ifdef RTE_LIB_PDUMP
3895 /* initialize packet capture framework */
3900 RTE_ETH_FOREACH_DEV(port_id) {
3901 ports_ids[count] = port_id;
3904 nb_ports = (portid_t) count;
3906 TESTPMD_LOG(WARNING, "No probed ethernet devices\n");
3908 /* allocate port structures, and init them */
3911 set_def_fwd_config();
3913 rte_exit(EXIT_FAILURE, "No cores defined for forwarding\n"
3914 "Check the core mask argument\n");
3916 /* Bitrate/latency stats disabled by default */
3917 #ifdef RTE_LIB_BITRATESTATS
3918 bitrate_enabled = 0;
3920 #ifdef RTE_LIB_LATENCYSTATS
3921 latencystats_enabled = 0;
3924 /* on FreeBSD, mlockall() is disabled by default */
3925 #ifdef RTE_EXEC_ENV_FREEBSD
3934 launch_args_parse(argc, argv);
3936 #ifndef RTE_EXEC_ENV_WINDOWS
3937 if (do_mlockall && mlockall(MCL_CURRENT | MCL_FUTURE)) {
3938 TESTPMD_LOG(NOTICE, "mlockall() failed with error \"%s\"\n",
3943 if (tx_first && interactive)
3944 rte_exit(EXIT_FAILURE, "--tx-first cannot be used on "
3945 "interactive mode.\n");
3947 if (tx_first && lsc_interrupt) {
3949 "Warning: lsc_interrupt needs to be off when using tx_first. Disabling.\n");
3953 if (!nb_rxq && !nb_txq)
3955 "Warning: Either rx or tx queues should be non-zero\n");
3957 if (nb_rxq > 1 && nb_rxq > nb_txq)
3959 "Warning: nb_rxq=%d enables RSS configuration, but nb_txq=%d will prevent to fully test it.\n",
3965 ret = rte_dev_hotplug_handle_enable();
3968 "fail to enable hotplug handling.");
3972 ret = rte_dev_event_monitor_start();
3975 "fail to start device event monitoring.");
3979 ret = rte_dev_event_callback_register(NULL,
3980 dev_event_callback, NULL);
3983 "fail to register device event callback\n");
3988 if (!no_device_start && start_port(RTE_PORT_ALL) != 0)
3989 rte_exit(EXIT_FAILURE, "Start ports failed\n");
3991 /* set all ports to promiscuous mode by default */
3992 RTE_ETH_FOREACH_DEV(port_id) {
3993 ret = rte_eth_promiscuous_enable(port_id);
3996 "Error during enabling promiscuous mode for port %u: %s - ignore\n",
3997 port_id, rte_strerror(-ret));
4000 /* Init metrics library */
4001 rte_metrics_init(rte_socket_id());
4003 #ifdef RTE_LIB_LATENCYSTATS
4004 if (latencystats_enabled != 0) {
4005 int ret = rte_latencystats_init(1, NULL);
4008 "Warning: latencystats init() returned error %d\n",
4010 fprintf(stderr, "Latencystats running on lcore %d\n",
4011 latencystats_lcore_id);
4015 /* Setup bitrate stats */
4016 #ifdef RTE_LIB_BITRATESTATS
4017 if (bitrate_enabled != 0) {
4018 bitrate_data = rte_stats_bitrate_create();
4019 if (bitrate_data == NULL)
4020 rte_exit(EXIT_FAILURE,
4021 "Could not allocate bitrate data.\n");
4022 rte_stats_bitrate_reg(bitrate_data);
4026 #ifdef RTE_LIB_CMDLINE
4027 if (strlen(cmdline_filename) != 0)
4028 cmdline_read_from_file(cmdline_filename);
4030 if (interactive == 1) {
4032 printf("Start automatic packet forwarding\n");
4033 start_packet_forwarding(0);
4045 printf("No commandline core given, start packet forwarding\n");
4046 start_packet_forwarding(tx_first);
4047 if (stats_period != 0) {
4048 uint64_t prev_time = 0, cur_time, diff_time = 0;
4049 uint64_t timer_period;
4051 /* Convert to number of cycles */
4052 timer_period = stats_period * rte_get_timer_hz();
4054 while (f_quit == 0) {
4055 cur_time = rte_get_timer_cycles();
4056 diff_time += cur_time - prev_time;
4058 if (diff_time >= timer_period) {
4060 /* Reset the timer */
4063 /* Sleep to avoid unnecessary checks */
4064 prev_time = cur_time;
4065 rte_delay_us_sleep(US_PER_S);
4069 printf("Press enter to exit\n");
4070 rc = read(0, &c, 1);
4076 ret = rte_eal_cleanup();
4078 rte_exit(EXIT_FAILURE,
4079 "EAL cleanup failed: %s\n", strerror(-ret));
4081 return EXIT_SUCCESS;