4 * Copyright(c) 2010-2017 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 #include <sys/types.h>
44 #include <sys/queue.h>
51 #include <rte_common.h>
52 #include <rte_errno.h>
53 #include <rte_byteorder.h>
55 #include <rte_debug.h>
56 #include <rte_cycles.h>
57 #include <rte_memory.h>
58 #include <rte_memcpy.h>
59 #include <rte_memzone.h>
60 #include <rte_launch.h>
62 #include <rte_per_lcore.h>
63 #include <rte_lcore.h>
64 #include <rte_atomic.h>
65 #include <rte_branch_prediction.h>
66 #include <rte_mempool.h>
67 #include <rte_malloc.h>
69 #include <rte_interrupts.h>
71 #include <rte_ether.h>
72 #include <rte_ethdev.h>
74 #include <rte_string_fns.h>
75 #ifdef RTE_LIBRTE_PMD_XENVIRT
76 #include <rte_eth_xenvirt.h>
78 #ifdef RTE_LIBRTE_PDUMP
79 #include <rte_pdump.h>
82 #include <rte_metrics.h>
83 #ifdef RTE_LIBRTE_BITRATE
84 #include <rte_bitrate.h>
86 #include <rte_metrics.h>
87 #ifdef RTE_LIBRTE_LATENCY_STATS
88 #include <rte_latencystats.h>
93 uint16_t verbose_level = 0; /**< Silent by default. */
95 /* use master core for command line ? */
96 uint8_t interactive = 0;
97 uint8_t auto_start = 0;
100 * NUMA support configuration.
101 * When set, the NUMA support attempts to dispatch the allocation of the
102 * RX and TX memory rings, and of the DMA memory buffers (mbufs) for the
103 * probed ports among the CPU sockets 0 and 1.
104 * Otherwise, all memory is allocated from CPU socket 0.
106 uint8_t numa_support = 0; /**< No numa support by default */
109 * In UMA mode,all memory is allocated from socket 0 if --socket-num is
112 uint8_t socket_num = UMA_NO_CONFIG;
115 * Use ANONYMOUS mapped memory (might be not physically continuous) for mbufs.
120 * Record the Ethernet address of peer target ports to which packets are
122 * Must be instantiated with the ethernet addresses of peer traffic generator
125 struct ether_addr peer_eth_addrs[RTE_MAX_ETHPORTS];
126 portid_t nb_peer_eth_addrs = 0;
129 * Probed Target Environment.
131 struct rte_port *ports; /**< For all probed ethernet ports. */
132 portid_t nb_ports; /**< Number of probed ethernet ports. */
133 struct fwd_lcore **fwd_lcores; /**< For all probed logical cores. */
134 lcoreid_t nb_lcores; /**< Number of probed logical cores. */
137 * Test Forwarding Configuration.
138 * nb_fwd_lcores <= nb_cfg_lcores <= nb_lcores
139 * nb_fwd_ports <= nb_cfg_ports <= nb_ports
141 lcoreid_t nb_cfg_lcores; /**< Number of configured logical cores. */
142 lcoreid_t nb_fwd_lcores; /**< Number of forwarding logical cores. */
143 portid_t nb_cfg_ports; /**< Number of configured ports. */
144 portid_t nb_fwd_ports; /**< Number of forwarding ports. */
146 unsigned int fwd_lcores_cpuids[RTE_MAX_LCORE]; /**< CPU ids configuration. */
147 portid_t fwd_ports_ids[RTE_MAX_ETHPORTS]; /**< Port ids configuration. */
149 struct fwd_stream **fwd_streams; /**< For each RX queue of each port. */
150 streamid_t nb_fwd_streams; /**< Is equal to (nb_ports * nb_rxq). */
153 * Forwarding engines.
155 struct fwd_engine * fwd_engines[] = {
164 #ifdef RTE_LIBRTE_IEEE1588
165 &ieee1588_fwd_engine,
170 struct fwd_config cur_fwd_config;
171 struct fwd_engine *cur_fwd_eng = &io_fwd_engine; /**< IO mode by default. */
172 uint32_t retry_enabled;
173 uint32_t burst_tx_delay_time = BURST_TX_WAIT_US;
174 uint32_t burst_tx_retry_num = BURST_TX_RETRIES;
176 uint16_t mbuf_data_size = DEFAULT_MBUF_DATA_SIZE; /**< Mbuf data space size. */
177 uint32_t param_total_num_mbufs = 0; /**< number of mbufs in all pools - if
178 * specified on command-line. */
181 * Configuration of packet segments used by the "txonly" processing engine.
183 uint16_t tx_pkt_length = TXONLY_DEF_PACKET_LEN; /**< TXONLY packet length. */
184 uint16_t tx_pkt_seg_lengths[RTE_MAX_SEGS_PER_PKT] = {
185 TXONLY_DEF_PACKET_LEN,
187 uint8_t tx_pkt_nb_segs = 1; /**< Number of segments in TXONLY packets */
189 enum tx_pkt_split tx_pkt_split = TX_PKT_SPLIT_OFF;
190 /**< Split policy for packets to TX. */
192 uint16_t nb_pkt_per_burst = DEF_PKT_BURST; /**< Number of packets per burst. */
193 uint16_t mb_mempool_cache = DEF_MBUF_CACHE; /**< Size of mbuf mempool cache. */
195 /* current configuration is in DCB or not,0 means it is not in DCB mode */
196 uint8_t dcb_config = 0;
198 /* Whether the dcb is in testing status */
199 uint8_t dcb_test = 0;
202 * Configurable number of RX/TX queues.
204 queueid_t nb_rxq = 1; /**< Number of RX queues per port. */
205 queueid_t nb_txq = 1; /**< Number of TX queues per port. */
208 * Configurable number of RX/TX ring descriptors.
210 #define RTE_TEST_RX_DESC_DEFAULT 128
211 #define RTE_TEST_TX_DESC_DEFAULT 512
212 uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT; /**< Number of RX descriptors. */
213 uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT; /**< Number of TX descriptors. */
215 #define RTE_PMD_PARAM_UNSET -1
217 * Configurable values of RX and TX ring threshold registers.
220 int8_t rx_pthresh = RTE_PMD_PARAM_UNSET;
221 int8_t rx_hthresh = RTE_PMD_PARAM_UNSET;
222 int8_t rx_wthresh = RTE_PMD_PARAM_UNSET;
224 int8_t tx_pthresh = RTE_PMD_PARAM_UNSET;
225 int8_t tx_hthresh = RTE_PMD_PARAM_UNSET;
226 int8_t tx_wthresh = RTE_PMD_PARAM_UNSET;
229 * Configurable value of RX free threshold.
231 int16_t rx_free_thresh = RTE_PMD_PARAM_UNSET;
234 * Configurable value of RX drop enable.
236 int8_t rx_drop_en = RTE_PMD_PARAM_UNSET;
239 * Configurable value of TX free threshold.
241 int16_t tx_free_thresh = RTE_PMD_PARAM_UNSET;
244 * Configurable value of TX RS bit threshold.
246 int16_t tx_rs_thresh = RTE_PMD_PARAM_UNSET;
249 * Configurable value of TX queue flags.
251 int32_t txq_flags = RTE_PMD_PARAM_UNSET;
254 * Receive Side Scaling (RSS) configuration.
256 uint64_t rss_hf = ETH_RSS_IP; /* RSS IP by default. */
259 * Port topology configuration
261 uint16_t port_topology = PORT_TOPOLOGY_PAIRED; /* Ports are paired by default */
264 * Avoids to flush all the RX streams before starts forwarding.
266 uint8_t no_flush_rx = 0; /* flush by default */
269 * Avoids to check link status when starting/stopping a port.
271 uint8_t no_link_check = 0; /* check by default */
274 * NIC bypass mode configuration options.
276 #ifdef RTE_NIC_BYPASS
278 /* The NIC bypass watchdog timeout. */
279 uint32_t bypass_timeout = RTE_BYPASS_TMT_OFF;
283 #ifdef RTE_LIBRTE_LATENCY_STATS
286 * Set when latency stats is enabled in the commandline
288 uint8_t latencystats_enabled;
291 * Lcore ID to serive latency statistics.
293 lcoreid_t latencystats_lcore_id = -1;
298 * Ethernet device configuration.
300 struct rte_eth_rxmode rx_mode = {
301 .max_rx_pkt_len = ETHER_MAX_LEN, /**< Default maximum frame length. */
303 .header_split = 0, /**< Header Split disabled. */
304 .hw_ip_checksum = 0, /**< IP checksum offload disabled. */
305 .hw_vlan_filter = 1, /**< VLAN filtering enabled. */
306 .hw_vlan_strip = 1, /**< VLAN strip enabled. */
307 .hw_vlan_extend = 0, /**< Extended VLAN disabled. */
308 .jumbo_frame = 0, /**< Jumbo Frame Support disabled. */
309 .hw_strip_crc = 0, /**< CRC stripping by hardware disabled. */
312 struct rte_fdir_conf fdir_conf = {
313 .mode = RTE_FDIR_MODE_NONE,
314 .pballoc = RTE_FDIR_PBALLOC_64K,
315 .status = RTE_FDIR_REPORT_STATUS,
317 .vlan_tci_mask = 0x0,
319 .src_ip = 0xFFFFFFFF,
320 .dst_ip = 0xFFFFFFFF,
323 .src_ip = {0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF},
324 .dst_ip = {0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF},
326 .src_port_mask = 0xFFFF,
327 .dst_port_mask = 0xFFFF,
328 .mac_addr_byte_mask = 0xFF,
329 .tunnel_type_mask = 1,
330 .tunnel_id_mask = 0xFFFFFFFF,
335 volatile int test_done = 1; /* stop packet forwarding when set to 1. */
337 struct queue_stats_mappings tx_queue_stats_mappings_array[MAX_TX_QUEUE_STATS_MAPPINGS];
338 struct queue_stats_mappings rx_queue_stats_mappings_array[MAX_RX_QUEUE_STATS_MAPPINGS];
340 struct queue_stats_mappings *tx_queue_stats_mappings = tx_queue_stats_mappings_array;
341 struct queue_stats_mappings *rx_queue_stats_mappings = rx_queue_stats_mappings_array;
343 uint16_t nb_tx_queue_stats_mappings = 0;
344 uint16_t nb_rx_queue_stats_mappings = 0;
346 unsigned max_socket = 0;
348 /* Bitrate statistics */
349 struct rte_stats_bitrates *bitrate_data;
351 /* Forward function declarations */
352 static void map_port_queue_stats_mapping_registers(uint8_t pi, struct rte_port *port);
353 static void check_all_ports_link_status(uint32_t port_mask);
356 * Check if all the ports are started.
357 * If yes, return positive value. If not, return zero.
359 static int all_ports_started(void);
362 * Setup default configuration.
365 set_default_fwd_lcores_config(void)
369 unsigned int sock_num;
372 for (i = 0; i < RTE_MAX_LCORE; i++) {
373 sock_num = rte_lcore_to_socket_id(i) + 1;
374 if (sock_num > max_socket) {
375 if (sock_num > RTE_MAX_NUMA_NODES)
376 rte_exit(EXIT_FAILURE, "Total sockets greater than %u\n", RTE_MAX_NUMA_NODES);
377 max_socket = sock_num;
379 if (!rte_lcore_is_enabled(i))
381 if (i == rte_get_master_lcore())
383 fwd_lcores_cpuids[nb_lc++] = i;
385 nb_lcores = (lcoreid_t) nb_lc;
386 nb_cfg_lcores = nb_lcores;
391 set_def_peer_eth_addrs(void)
395 for (i = 0; i < RTE_MAX_ETHPORTS; i++) {
396 peer_eth_addrs[i].addr_bytes[0] = ETHER_LOCAL_ADMIN_ADDR;
397 peer_eth_addrs[i].addr_bytes[5] = i;
402 set_default_fwd_ports_config(void)
406 for (pt_id = 0; pt_id < nb_ports; pt_id++)
407 fwd_ports_ids[pt_id] = pt_id;
409 nb_cfg_ports = nb_ports;
410 nb_fwd_ports = nb_ports;
414 set_def_fwd_config(void)
416 set_default_fwd_lcores_config();
417 set_def_peer_eth_addrs();
418 set_default_fwd_ports_config();
422 * Configuration initialisation done once at init time.
425 mbuf_pool_create(uint16_t mbuf_seg_size, unsigned nb_mbuf,
426 unsigned int socket_id)
428 char pool_name[RTE_MEMPOOL_NAMESIZE];
429 struct rte_mempool *rte_mp = NULL;
432 mb_size = sizeof(struct rte_mbuf) + mbuf_seg_size;
433 mbuf_poolname_build(socket_id, pool_name, sizeof(pool_name));
436 "create a new mbuf pool <%s>: n=%u, size=%u, socket=%u\n",
437 pool_name, nb_mbuf, mbuf_seg_size, socket_id);
439 #ifdef RTE_LIBRTE_PMD_XENVIRT
440 rte_mp = rte_mempool_gntalloc_create(pool_name, nb_mbuf, mb_size,
441 (unsigned) mb_mempool_cache,
442 sizeof(struct rte_pktmbuf_pool_private),
443 rte_pktmbuf_pool_init, NULL,
444 rte_pktmbuf_init, NULL,
448 /* if the former XEN allocation failed fall back to normal allocation */
449 if (rte_mp == NULL) {
451 rte_mp = rte_mempool_create_empty(pool_name, nb_mbuf,
452 mb_size, (unsigned) mb_mempool_cache,
453 sizeof(struct rte_pktmbuf_pool_private),
458 if (rte_mempool_populate_anon(rte_mp) == 0) {
459 rte_mempool_free(rte_mp);
463 rte_pktmbuf_pool_init(rte_mp, NULL);
464 rte_mempool_obj_iter(rte_mp, rte_pktmbuf_init, NULL);
466 /* wrapper to rte_mempool_create() */
467 rte_mp = rte_pktmbuf_pool_create(pool_name, nb_mbuf,
468 mb_mempool_cache, 0, mbuf_seg_size, socket_id);
473 if (rte_mp == NULL) {
474 rte_exit(EXIT_FAILURE,
475 "Creation of mbuf pool for socket %u failed: %s\n",
476 socket_id, rte_strerror(rte_errno));
477 } else if (verbose_level > 0) {
478 rte_mempool_dump(stdout, rte_mp);
483 * Check given socket id is valid or not with NUMA mode,
484 * if valid, return 0, else return -1
487 check_socket_id(const unsigned int socket_id)
489 static int warning_once = 0;
491 if (socket_id >= max_socket) {
492 if (!warning_once && numa_support)
493 printf("Warning: NUMA should be configured manually by"
494 " using --port-numa-config and"
495 " --ring-numa-config parameters along with"
507 struct rte_port *port;
508 struct rte_mempool *mbp;
509 unsigned int nb_mbuf_per_pool;
511 uint8_t port_per_socket[RTE_MAX_NUMA_NODES];
513 memset(port_per_socket,0,RTE_MAX_NUMA_NODES);
514 /* Configuration of logical cores. */
515 fwd_lcores = rte_zmalloc("testpmd: fwd_lcores",
516 sizeof(struct fwd_lcore *) * nb_lcores,
517 RTE_CACHE_LINE_SIZE);
518 if (fwd_lcores == NULL) {
519 rte_exit(EXIT_FAILURE, "rte_zmalloc(%d (struct fwd_lcore *)) "
520 "failed\n", nb_lcores);
522 for (lc_id = 0; lc_id < nb_lcores; lc_id++) {
523 fwd_lcores[lc_id] = rte_zmalloc("testpmd: struct fwd_lcore",
524 sizeof(struct fwd_lcore),
525 RTE_CACHE_LINE_SIZE);
526 if (fwd_lcores[lc_id] == NULL) {
527 rte_exit(EXIT_FAILURE, "rte_zmalloc(struct fwd_lcore) "
530 fwd_lcores[lc_id]->cpuid_idx = lc_id;
534 * Create pools of mbuf.
535 * If NUMA support is disabled, create a single pool of mbuf in
536 * socket 0 memory by default.
537 * Otherwise, create a pool of mbuf in the memory of sockets 0 and 1.
539 * Use the maximum value of nb_rxd and nb_txd here, then nb_rxd and
540 * nb_txd can be configured at run time.
542 if (param_total_num_mbufs)
543 nb_mbuf_per_pool = param_total_num_mbufs;
545 nb_mbuf_per_pool = RTE_TEST_RX_DESC_MAX + (nb_lcores * mb_mempool_cache)
546 + RTE_TEST_TX_DESC_MAX + MAX_PKT_BURST;
550 (nb_mbuf_per_pool * RTE_MAX_ETHPORTS);
554 if (socket_num == UMA_NO_CONFIG)
555 mbuf_pool_create(mbuf_data_size, nb_mbuf_per_pool, 0);
557 mbuf_pool_create(mbuf_data_size, nb_mbuf_per_pool,
561 RTE_ETH_FOREACH_DEV(pid) {
563 rte_eth_dev_info_get(pid, &port->dev_info);
566 if (port_numa[pid] != NUMA_NO_CONFIG)
567 port_per_socket[port_numa[pid]]++;
569 uint32_t socket_id = rte_eth_dev_socket_id(pid);
571 /* if socket_id is invalid, set to 0 */
572 if (check_socket_id(socket_id) < 0)
574 port_per_socket[socket_id]++;
578 /* set flag to initialize port/queue */
579 port->need_reconfig = 1;
580 port->need_reconfig_queues = 1;
585 unsigned int nb_mbuf;
587 if (param_total_num_mbufs)
588 nb_mbuf_per_pool = nb_mbuf_per_pool/nb_ports;
590 for (i = 0; i < max_socket; i++) {
591 nb_mbuf = (nb_mbuf_per_pool * RTE_MAX_ETHPORTS);
593 mbuf_pool_create(mbuf_data_size,
600 * Records which Mbuf pool to use by each logical core, if needed.
602 for (lc_id = 0; lc_id < nb_lcores; lc_id++) {
603 mbp = mbuf_pool_find(
604 rte_lcore_to_socket_id(fwd_lcores_cpuids[lc_id]));
607 mbp = mbuf_pool_find(0);
608 fwd_lcores[lc_id]->mbp = mbp;
611 /* Configuration of packet forwarding streams. */
612 if (init_fwd_streams() < 0)
613 rte_exit(EXIT_FAILURE, "FAIL from init_fwd_streams()\n");
620 reconfig(portid_t new_port_id, unsigned socket_id)
622 struct rte_port *port;
624 /* Reconfiguration of Ethernet ports. */
625 port = &ports[new_port_id];
626 rte_eth_dev_info_get(new_port_id, &port->dev_info);
628 /* set flag to initialize port/queue */
629 port->need_reconfig = 1;
630 port->need_reconfig_queues = 1;
631 port->socket_id = socket_id;
638 init_fwd_streams(void)
641 struct rte_port *port;
642 streamid_t sm_id, nb_fwd_streams_new;
645 /* set socket id according to numa or not */
646 RTE_ETH_FOREACH_DEV(pid) {
648 if (nb_rxq > port->dev_info.max_rx_queues) {
649 printf("Fail: nb_rxq(%d) is greater than "
650 "max_rx_queues(%d)\n", nb_rxq,
651 port->dev_info.max_rx_queues);
654 if (nb_txq > port->dev_info.max_tx_queues) {
655 printf("Fail: nb_txq(%d) is greater than "
656 "max_tx_queues(%d)\n", nb_txq,
657 port->dev_info.max_tx_queues);
661 if (port_numa[pid] != NUMA_NO_CONFIG)
662 port->socket_id = port_numa[pid];
664 port->socket_id = rte_eth_dev_socket_id(pid);
666 /* if socket_id is invalid, set to 0 */
667 if (check_socket_id(port->socket_id) < 0)
672 if (socket_num == UMA_NO_CONFIG)
675 port->socket_id = socket_num;
679 q = RTE_MAX(nb_rxq, nb_txq);
681 printf("Fail: Cannot allocate fwd streams as number of queues is 0\n");
684 nb_fwd_streams_new = (streamid_t)(nb_ports * q);
685 if (nb_fwd_streams_new == nb_fwd_streams)
688 if (fwd_streams != NULL) {
689 for (sm_id = 0; sm_id < nb_fwd_streams; sm_id++) {
690 if (fwd_streams[sm_id] == NULL)
692 rte_free(fwd_streams[sm_id]);
693 fwd_streams[sm_id] = NULL;
695 rte_free(fwd_streams);
700 nb_fwd_streams = nb_fwd_streams_new;
701 fwd_streams = rte_zmalloc("testpmd: fwd_streams",
702 sizeof(struct fwd_stream *) * nb_fwd_streams, RTE_CACHE_LINE_SIZE);
703 if (fwd_streams == NULL)
704 rte_exit(EXIT_FAILURE, "rte_zmalloc(%d (struct fwd_stream *)) "
705 "failed\n", nb_fwd_streams);
707 for (sm_id = 0; sm_id < nb_fwd_streams; sm_id++) {
708 fwd_streams[sm_id] = rte_zmalloc("testpmd: struct fwd_stream",
709 sizeof(struct fwd_stream), RTE_CACHE_LINE_SIZE);
710 if (fwd_streams[sm_id] == NULL)
711 rte_exit(EXIT_FAILURE, "rte_zmalloc(struct fwd_stream)"
718 #ifdef RTE_TEST_PMD_RECORD_BURST_STATS
720 pkt_burst_stats_display(const char *rx_tx, struct pkt_burst_stats *pbs)
722 unsigned int total_burst;
723 unsigned int nb_burst;
724 unsigned int burst_stats[3];
725 uint16_t pktnb_stats[3];
727 int burst_percent[3];
730 * First compute the total number of packet bursts and the
731 * two highest numbers of bursts of the same number of packets.
734 burst_stats[0] = burst_stats[1] = burst_stats[2] = 0;
735 pktnb_stats[0] = pktnb_stats[1] = pktnb_stats[2] = 0;
736 for (nb_pkt = 0; nb_pkt < MAX_PKT_BURST; nb_pkt++) {
737 nb_burst = pbs->pkt_burst_spread[nb_pkt];
740 total_burst += nb_burst;
741 if (nb_burst > burst_stats[0]) {
742 burst_stats[1] = burst_stats[0];
743 pktnb_stats[1] = pktnb_stats[0];
744 burst_stats[0] = nb_burst;
745 pktnb_stats[0] = nb_pkt;
748 if (total_burst == 0)
750 burst_percent[0] = (burst_stats[0] * 100) / total_burst;
751 printf(" %s-bursts : %u [%d%% of %d pkts", rx_tx, total_burst,
752 burst_percent[0], (int) pktnb_stats[0]);
753 if (burst_stats[0] == total_burst) {
757 if (burst_stats[0] + burst_stats[1] == total_burst) {
758 printf(" + %d%% of %d pkts]\n",
759 100 - burst_percent[0], pktnb_stats[1]);
762 burst_percent[1] = (burst_stats[1] * 100) / total_burst;
763 burst_percent[2] = 100 - (burst_percent[0] + burst_percent[1]);
764 if ((burst_percent[1] == 0) || (burst_percent[2] == 0)) {
765 printf(" + %d%% of others]\n", 100 - burst_percent[0]);
768 printf(" + %d%% of %d pkts + %d%% of others]\n",
769 burst_percent[1], (int) pktnb_stats[1], burst_percent[2]);
771 #endif /* RTE_TEST_PMD_RECORD_BURST_STATS */
774 fwd_port_stats_display(portid_t port_id, struct rte_eth_stats *stats)
776 struct rte_port *port;
779 static const char *fwd_stats_border = "----------------------";
781 port = &ports[port_id];
782 printf("\n %s Forward statistics for port %-2d %s\n",
783 fwd_stats_border, port_id, fwd_stats_border);
785 if ((!port->rx_queue_stats_mapping_enabled) && (!port->tx_queue_stats_mapping_enabled)) {
786 printf(" RX-packets: %-14"PRIu64" RX-dropped: %-14"PRIu64"RX-total: "
788 stats->ipackets, stats->imissed,
789 (uint64_t) (stats->ipackets + stats->imissed));
791 if (cur_fwd_eng == &csum_fwd_engine)
792 printf(" Bad-ipcsum: %-14"PRIu64" Bad-l4csum: %-14"PRIu64" \n",
793 port->rx_bad_ip_csum, port->rx_bad_l4_csum);
794 if ((stats->ierrors + stats->rx_nombuf) > 0) {
795 printf(" RX-error: %-"PRIu64"\n", stats->ierrors);
796 printf(" RX-nombufs: %-14"PRIu64"\n", stats->rx_nombuf);
799 printf(" TX-packets: %-14"PRIu64" TX-dropped: %-14"PRIu64"TX-total: "
801 stats->opackets, port->tx_dropped,
802 (uint64_t) (stats->opackets + port->tx_dropped));
805 printf(" RX-packets: %14"PRIu64" RX-dropped:%14"PRIu64" RX-total:"
807 stats->ipackets, stats->imissed,
808 (uint64_t) (stats->ipackets + stats->imissed));
810 if (cur_fwd_eng == &csum_fwd_engine)
811 printf(" Bad-ipcsum:%14"PRIu64" Bad-l4csum:%14"PRIu64"\n",
812 port->rx_bad_ip_csum, port->rx_bad_l4_csum);
813 if ((stats->ierrors + stats->rx_nombuf) > 0) {
814 printf(" RX-error:%"PRIu64"\n", stats->ierrors);
815 printf(" RX-nombufs: %14"PRIu64"\n",
819 printf(" TX-packets: %14"PRIu64" TX-dropped:%14"PRIu64" TX-total:"
821 stats->opackets, port->tx_dropped,
822 (uint64_t) (stats->opackets + port->tx_dropped));
825 #ifdef RTE_TEST_PMD_RECORD_BURST_STATS
827 pkt_burst_stats_display("RX",
828 &port->rx_stream->rx_burst_stats);
830 pkt_burst_stats_display("TX",
831 &port->tx_stream->tx_burst_stats);
834 if (port->rx_queue_stats_mapping_enabled) {
836 for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS; i++) {
837 printf(" Stats reg %2d RX-packets:%14"PRIu64
838 " RX-errors:%14"PRIu64
839 " RX-bytes:%14"PRIu64"\n",
840 i, stats->q_ipackets[i], stats->q_errors[i], stats->q_ibytes[i]);
844 if (port->tx_queue_stats_mapping_enabled) {
845 for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS; i++) {
846 printf(" Stats reg %2d TX-packets:%14"PRIu64
847 " TX-bytes:%14"PRIu64"\n",
848 i, stats->q_opackets[i], stats->q_obytes[i]);
852 printf(" %s--------------------------------%s\n",
853 fwd_stats_border, fwd_stats_border);
857 fwd_stream_stats_display(streamid_t stream_id)
859 struct fwd_stream *fs;
860 static const char *fwd_top_stats_border = "-------";
862 fs = fwd_streams[stream_id];
863 if ((fs->rx_packets == 0) && (fs->tx_packets == 0) &&
864 (fs->fwd_dropped == 0))
866 printf("\n %s Forward Stats for RX Port=%2d/Queue=%2d -> "
867 "TX Port=%2d/Queue=%2d %s\n",
868 fwd_top_stats_border, fs->rx_port, fs->rx_queue,
869 fs->tx_port, fs->tx_queue, fwd_top_stats_border);
870 printf(" RX-packets: %-14u TX-packets: %-14u TX-dropped: %-14u",
871 fs->rx_packets, fs->tx_packets, fs->fwd_dropped);
873 /* if checksum mode */
874 if (cur_fwd_eng == &csum_fwd_engine) {
875 printf(" RX- bad IP checksum: %-14u Rx- bad L4 checksum: "
876 "%-14u\n", fs->rx_bad_ip_csum, fs->rx_bad_l4_csum);
879 #ifdef RTE_TEST_PMD_RECORD_BURST_STATS
880 pkt_burst_stats_display("RX", &fs->rx_burst_stats);
881 pkt_burst_stats_display("TX", &fs->tx_burst_stats);
886 flush_fwd_rx_queues(void)
888 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
895 uint64_t prev_tsc = 0, diff_tsc, cur_tsc, timer_tsc = 0;
896 uint64_t timer_period;
898 /* convert to number of cycles */
899 timer_period = rte_get_timer_hz(); /* 1 second timeout */
901 for (j = 0; j < 2; j++) {
902 for (rxp = 0; rxp < cur_fwd_config.nb_fwd_ports; rxp++) {
903 for (rxq = 0; rxq < nb_rxq; rxq++) {
904 port_id = fwd_ports_ids[rxp];
906 * testpmd can stuck in the below do while loop
907 * if rte_eth_rx_burst() always returns nonzero
908 * packets. So timer is added to exit this loop
909 * after 1sec timer expiry.
911 prev_tsc = rte_rdtsc();
913 nb_rx = rte_eth_rx_burst(port_id, rxq,
914 pkts_burst, MAX_PKT_BURST);
915 for (i = 0; i < nb_rx; i++)
916 rte_pktmbuf_free(pkts_burst[i]);
918 cur_tsc = rte_rdtsc();
919 diff_tsc = cur_tsc - prev_tsc;
920 timer_tsc += diff_tsc;
921 } while ((nb_rx > 0) &&
922 (timer_tsc < timer_period));
926 rte_delay_ms(10); /* wait 10 milli-seconds before retrying */
931 run_pkt_fwd_on_lcore(struct fwd_lcore *fc, packet_fwd_t pkt_fwd)
933 struct fwd_stream **fsm;
936 #ifdef RTE_LIBRTE_BITRATE
937 uint64_t tics_per_1sec;
939 uint64_t tics_current;
940 uint8_t idx_port, cnt_ports;
942 cnt_ports = rte_eth_dev_count();
943 tics_datum = rte_rdtsc();
944 tics_per_1sec = rte_get_timer_hz();
946 fsm = &fwd_streams[fc->stream_idx];
947 nb_fs = fc->stream_nb;
949 for (sm_id = 0; sm_id < nb_fs; sm_id++)
950 (*pkt_fwd)(fsm[sm_id]);
951 #ifdef RTE_LIBRTE_BITRATE
952 tics_current = rte_rdtsc();
953 if (tics_current - tics_datum >= tics_per_1sec) {
954 /* Periodic bitrate calculation */
955 for (idx_port = 0; idx_port < cnt_ports; idx_port++)
956 rte_stats_bitrate_calc(bitrate_data, idx_port);
957 tics_datum = tics_current;
960 #ifdef RTE_LIBRTE_LATENCY_STATS
961 if (latencystats_lcore_id == rte_lcore_id())
962 rte_latencystats_update();
965 } while (! fc->stopped);
969 start_pkt_forward_on_core(void *fwd_arg)
971 run_pkt_fwd_on_lcore((struct fwd_lcore *) fwd_arg,
972 cur_fwd_config.fwd_eng->packet_fwd);
977 * Run the TXONLY packet forwarding engine to send a single burst of packets.
978 * Used to start communication flows in network loopback test configurations.
981 run_one_txonly_burst_on_core(void *fwd_arg)
983 struct fwd_lcore *fwd_lc;
984 struct fwd_lcore tmp_lcore;
986 fwd_lc = (struct fwd_lcore *) fwd_arg;
988 tmp_lcore.stopped = 1;
989 run_pkt_fwd_on_lcore(&tmp_lcore, tx_only_engine.packet_fwd);
994 * Launch packet forwarding:
995 * - Setup per-port forwarding context.
996 * - launch logical cores with their forwarding configuration.
999 launch_packet_forwarding(lcore_function_t *pkt_fwd_on_lcore)
1001 port_fwd_begin_t port_fwd_begin;
1006 port_fwd_begin = cur_fwd_config.fwd_eng->port_fwd_begin;
1007 if (port_fwd_begin != NULL) {
1008 for (i = 0; i < cur_fwd_config.nb_fwd_ports; i++)
1009 (*port_fwd_begin)(fwd_ports_ids[i]);
1011 for (i = 0; i < cur_fwd_config.nb_fwd_lcores; i++) {
1012 lc_id = fwd_lcores_cpuids[i];
1013 if ((interactive == 0) || (lc_id != rte_lcore_id())) {
1014 fwd_lcores[i]->stopped = 0;
1015 diag = rte_eal_remote_launch(pkt_fwd_on_lcore,
1016 fwd_lcores[i], lc_id);
1018 printf("launch lcore %u failed - diag=%d\n",
1025 * Launch packet forwarding configuration.
1028 start_packet_forwarding(int with_tx_first)
1030 port_fwd_begin_t port_fwd_begin;
1031 port_fwd_end_t port_fwd_end;
1032 struct rte_port *port;
1037 if (strcmp(cur_fwd_eng->fwd_mode_name, "rxonly") == 0 && !nb_rxq)
1038 rte_exit(EXIT_FAILURE, "rxq are 0, cannot use rxonly fwd mode\n");
1040 if (strcmp(cur_fwd_eng->fwd_mode_name, "txonly") == 0 && !nb_txq)
1041 rte_exit(EXIT_FAILURE, "txq are 0, cannot use txonly fwd mode\n");
1043 if ((strcmp(cur_fwd_eng->fwd_mode_name, "rxonly") != 0 &&
1044 strcmp(cur_fwd_eng->fwd_mode_name, "txonly") != 0) &&
1045 (!nb_rxq || !nb_txq))
1046 rte_exit(EXIT_FAILURE,
1047 "Either rxq or txq are 0, cannot use %s fwd mode\n",
1048 cur_fwd_eng->fwd_mode_name);
1050 if (all_ports_started() == 0) {
1051 printf("Not all ports were started\n");
1054 if (test_done == 0) {
1055 printf("Packet forwarding already started\n");
1059 if (init_fwd_streams() < 0) {
1060 printf("Fail from init_fwd_streams()\n");
1065 for (i = 0; i < nb_fwd_ports; i++) {
1066 pt_id = fwd_ports_ids[i];
1067 port = &ports[pt_id];
1068 if (!port->dcb_flag) {
1069 printf("In DCB mode, all forwarding ports must "
1070 "be configured in this mode.\n");
1074 if (nb_fwd_lcores == 1) {
1075 printf("In DCB mode,the nb forwarding cores "
1076 "should be larger than 1.\n");
1083 flush_fwd_rx_queues();
1086 pkt_fwd_config_display(&cur_fwd_config);
1087 rxtx_config_display();
1089 for (i = 0; i < cur_fwd_config.nb_fwd_ports; i++) {
1090 pt_id = fwd_ports_ids[i];
1091 port = &ports[pt_id];
1092 rte_eth_stats_get(pt_id, &port->stats);
1093 port->tx_dropped = 0;
1095 map_port_queue_stats_mapping_registers(pt_id, port);
1097 for (sm_id = 0; sm_id < cur_fwd_config.nb_fwd_streams; sm_id++) {
1098 fwd_streams[sm_id]->rx_packets = 0;
1099 fwd_streams[sm_id]->tx_packets = 0;
1100 fwd_streams[sm_id]->fwd_dropped = 0;
1101 fwd_streams[sm_id]->rx_bad_ip_csum = 0;
1102 fwd_streams[sm_id]->rx_bad_l4_csum = 0;
1104 #ifdef RTE_TEST_PMD_RECORD_BURST_STATS
1105 memset(&fwd_streams[sm_id]->rx_burst_stats, 0,
1106 sizeof(fwd_streams[sm_id]->rx_burst_stats));
1107 memset(&fwd_streams[sm_id]->tx_burst_stats, 0,
1108 sizeof(fwd_streams[sm_id]->tx_burst_stats));
1110 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
1111 fwd_streams[sm_id]->core_cycles = 0;
1114 if (with_tx_first) {
1115 port_fwd_begin = tx_only_engine.port_fwd_begin;
1116 if (port_fwd_begin != NULL) {
1117 for (i = 0; i < cur_fwd_config.nb_fwd_ports; i++)
1118 (*port_fwd_begin)(fwd_ports_ids[i]);
1120 while (with_tx_first--) {
1121 launch_packet_forwarding(
1122 run_one_txonly_burst_on_core);
1123 rte_eal_mp_wait_lcore();
1125 port_fwd_end = tx_only_engine.port_fwd_end;
1126 if (port_fwd_end != NULL) {
1127 for (i = 0; i < cur_fwd_config.nb_fwd_ports; i++)
1128 (*port_fwd_end)(fwd_ports_ids[i]);
1131 launch_packet_forwarding(start_pkt_forward_on_core);
1135 stop_packet_forwarding(void)
1137 struct rte_eth_stats stats;
1138 struct rte_port *port;
1139 port_fwd_end_t port_fwd_end;
1144 uint64_t total_recv;
1145 uint64_t total_xmit;
1146 uint64_t total_rx_dropped;
1147 uint64_t total_tx_dropped;
1148 uint64_t total_rx_nombuf;
1149 uint64_t tx_dropped;
1150 uint64_t rx_bad_ip_csum;
1151 uint64_t rx_bad_l4_csum;
1152 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
1153 uint64_t fwd_cycles;
1155 static const char *acc_stats_border = "+++++++++++++++";
1158 printf("Packet forwarding not started\n");
1161 printf("Telling cores to stop...");
1162 for (lc_id = 0; lc_id < cur_fwd_config.nb_fwd_lcores; lc_id++)
1163 fwd_lcores[lc_id]->stopped = 1;
1164 printf("\nWaiting for lcores to finish...\n");
1165 rte_eal_mp_wait_lcore();
1166 port_fwd_end = cur_fwd_config.fwd_eng->port_fwd_end;
1167 if (port_fwd_end != NULL) {
1168 for (i = 0; i < cur_fwd_config.nb_fwd_ports; i++) {
1169 pt_id = fwd_ports_ids[i];
1170 (*port_fwd_end)(pt_id);
1173 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
1176 for (sm_id = 0; sm_id < cur_fwd_config.nb_fwd_streams; sm_id++) {
1177 if (cur_fwd_config.nb_fwd_streams >
1178 cur_fwd_config.nb_fwd_ports) {
1179 fwd_stream_stats_display(sm_id);
1180 ports[fwd_streams[sm_id]->tx_port].tx_stream = NULL;
1181 ports[fwd_streams[sm_id]->rx_port].rx_stream = NULL;
1183 ports[fwd_streams[sm_id]->tx_port].tx_stream =
1185 ports[fwd_streams[sm_id]->rx_port].rx_stream =
1188 tx_dropped = ports[fwd_streams[sm_id]->tx_port].tx_dropped;
1189 tx_dropped = (uint64_t) (tx_dropped +
1190 fwd_streams[sm_id]->fwd_dropped);
1191 ports[fwd_streams[sm_id]->tx_port].tx_dropped = tx_dropped;
1194 ports[fwd_streams[sm_id]->rx_port].rx_bad_ip_csum;
1195 rx_bad_ip_csum = (uint64_t) (rx_bad_ip_csum +
1196 fwd_streams[sm_id]->rx_bad_ip_csum);
1197 ports[fwd_streams[sm_id]->rx_port].rx_bad_ip_csum =
1201 ports[fwd_streams[sm_id]->rx_port].rx_bad_l4_csum;
1202 rx_bad_l4_csum = (uint64_t) (rx_bad_l4_csum +
1203 fwd_streams[sm_id]->rx_bad_l4_csum);
1204 ports[fwd_streams[sm_id]->rx_port].rx_bad_l4_csum =
1207 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
1208 fwd_cycles = (uint64_t) (fwd_cycles +
1209 fwd_streams[sm_id]->core_cycles);
1214 total_rx_dropped = 0;
1215 total_tx_dropped = 0;
1216 total_rx_nombuf = 0;
1217 for (i = 0; i < cur_fwd_config.nb_fwd_ports; i++) {
1218 pt_id = fwd_ports_ids[i];
1220 port = &ports[pt_id];
1221 rte_eth_stats_get(pt_id, &stats);
1222 stats.ipackets -= port->stats.ipackets;
1223 port->stats.ipackets = 0;
1224 stats.opackets -= port->stats.opackets;
1225 port->stats.opackets = 0;
1226 stats.ibytes -= port->stats.ibytes;
1227 port->stats.ibytes = 0;
1228 stats.obytes -= port->stats.obytes;
1229 port->stats.obytes = 0;
1230 stats.imissed -= port->stats.imissed;
1231 port->stats.imissed = 0;
1232 stats.oerrors -= port->stats.oerrors;
1233 port->stats.oerrors = 0;
1234 stats.rx_nombuf -= port->stats.rx_nombuf;
1235 port->stats.rx_nombuf = 0;
1237 total_recv += stats.ipackets;
1238 total_xmit += stats.opackets;
1239 total_rx_dropped += stats.imissed;
1240 total_tx_dropped += port->tx_dropped;
1241 total_rx_nombuf += stats.rx_nombuf;
1243 fwd_port_stats_display(pt_id, &stats);
1245 printf("\n %s Accumulated forward statistics for all ports"
1247 acc_stats_border, acc_stats_border);
1248 printf(" RX-packets: %-14"PRIu64" RX-dropped: %-14"PRIu64"RX-total: "
1250 " TX-packets: %-14"PRIu64" TX-dropped: %-14"PRIu64"TX-total: "
1252 total_recv, total_rx_dropped, total_recv + total_rx_dropped,
1253 total_xmit, total_tx_dropped, total_xmit + total_tx_dropped);
1254 if (total_rx_nombuf > 0)
1255 printf(" RX-nombufs: %-14"PRIu64"\n", total_rx_nombuf);
1256 printf(" %s++++++++++++++++++++++++++++++++++++++++++++++"
1258 acc_stats_border, acc_stats_border);
1259 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
1261 printf("\n CPU cycles/packet=%u (total cycles="
1262 "%"PRIu64" / total RX packets=%"PRIu64")\n",
1263 (unsigned int)(fwd_cycles / total_recv),
1264 fwd_cycles, total_recv);
1266 printf("\nDone.\n");
1271 dev_set_link_up(portid_t pid)
1273 if (rte_eth_dev_set_link_up((uint8_t)pid) < 0)
1274 printf("\nSet link up fail.\n");
1278 dev_set_link_down(portid_t pid)
1280 if (rte_eth_dev_set_link_down((uint8_t)pid) < 0)
1281 printf("\nSet link down fail.\n");
1285 all_ports_started(void)
1288 struct rte_port *port;
1290 RTE_ETH_FOREACH_DEV(pi) {
1292 /* Check if there is a port which is not started */
1293 if ((port->port_status != RTE_PORT_STARTED) &&
1294 (port->slave_flag == 0))
1298 /* No port is not started */
1303 all_ports_stopped(void)
1306 struct rte_port *port;
1308 RTE_ETH_FOREACH_DEV(pi) {
1310 if ((port->port_status != RTE_PORT_STOPPED) &&
1311 (port->slave_flag == 0))
1319 port_is_started(portid_t port_id)
1321 if (port_id_is_invalid(port_id, ENABLED_WARN))
1324 if (ports[port_id].port_status != RTE_PORT_STARTED)
1331 port_is_closed(portid_t port_id)
1333 if (port_id_is_invalid(port_id, ENABLED_WARN))
1336 if (ports[port_id].port_status != RTE_PORT_CLOSED)
1343 start_port(portid_t pid)
1345 int diag, need_check_link_status = -1;
1348 struct rte_port *port;
1349 struct ether_addr mac_addr;
1351 if (port_id_is_invalid(pid, ENABLED_WARN))
1356 RTE_ETH_FOREACH_DEV(pi) {
1357 if (pid != pi && pid != (portid_t)RTE_PORT_ALL)
1360 need_check_link_status = 0;
1362 if (rte_atomic16_cmpset(&(port->port_status), RTE_PORT_STOPPED,
1363 RTE_PORT_HANDLING) == 0) {
1364 printf("Port %d is now not stopped\n", pi);
1368 if (port->need_reconfig > 0) {
1369 port->need_reconfig = 0;
1371 printf("Configuring Port %d (socket %u)\n", pi,
1373 /* configure port */
1374 diag = rte_eth_dev_configure(pi, nb_rxq, nb_txq,
1377 if (rte_atomic16_cmpset(&(port->port_status),
1378 RTE_PORT_HANDLING, RTE_PORT_STOPPED) == 0)
1379 printf("Port %d can not be set back "
1380 "to stopped\n", pi);
1381 printf("Fail to configure port %d\n", pi);
1382 /* try to reconfigure port next time */
1383 port->need_reconfig = 1;
1387 if (port->need_reconfig_queues > 0) {
1388 port->need_reconfig_queues = 0;
1389 /* setup tx queues */
1390 for (qi = 0; qi < nb_txq; qi++) {
1391 if ((numa_support) &&
1392 (txring_numa[pi] != NUMA_NO_CONFIG))
1393 diag = rte_eth_tx_queue_setup(pi, qi,
1394 nb_txd,txring_numa[pi],
1397 diag = rte_eth_tx_queue_setup(pi, qi,
1398 nb_txd,port->socket_id,
1404 /* Fail to setup tx queue, return */
1405 if (rte_atomic16_cmpset(&(port->port_status),
1407 RTE_PORT_STOPPED) == 0)
1408 printf("Port %d can not be set back "
1409 "to stopped\n", pi);
1410 printf("Fail to configure port %d tx queues\n", pi);
1411 /* try to reconfigure queues next time */
1412 port->need_reconfig_queues = 1;
1415 /* setup rx queues */
1416 for (qi = 0; qi < nb_rxq; qi++) {
1417 if ((numa_support) &&
1418 (rxring_numa[pi] != NUMA_NO_CONFIG)) {
1419 struct rte_mempool * mp =
1420 mbuf_pool_find(rxring_numa[pi]);
1422 printf("Failed to setup RX queue:"
1423 "No mempool allocation"
1424 " on the socket %d\n",
1429 diag = rte_eth_rx_queue_setup(pi, qi,
1430 nb_rxd,rxring_numa[pi],
1431 &(port->rx_conf),mp);
1433 struct rte_mempool *mp =
1434 mbuf_pool_find(port->socket_id);
1436 printf("Failed to setup RX queue:"
1437 "No mempool allocation"
1438 " on the socket %d\n",
1442 diag = rte_eth_rx_queue_setup(pi, qi,
1443 nb_rxd,port->socket_id,
1444 &(port->rx_conf), mp);
1449 /* Fail to setup rx queue, return */
1450 if (rte_atomic16_cmpset(&(port->port_status),
1452 RTE_PORT_STOPPED) == 0)
1453 printf("Port %d can not be set back "
1454 "to stopped\n", pi);
1455 printf("Fail to configure port %d rx queues\n", pi);
1456 /* try to reconfigure queues next time */
1457 port->need_reconfig_queues = 1;
1462 if (rte_eth_dev_start(pi) < 0) {
1463 printf("Fail to start port %d\n", pi);
1465 /* Fail to setup rx queue, return */
1466 if (rte_atomic16_cmpset(&(port->port_status),
1467 RTE_PORT_HANDLING, RTE_PORT_STOPPED) == 0)
1468 printf("Port %d can not be set back to "
1473 if (rte_atomic16_cmpset(&(port->port_status),
1474 RTE_PORT_HANDLING, RTE_PORT_STARTED) == 0)
1475 printf("Port %d can not be set into started\n", pi);
1477 rte_eth_macaddr_get(pi, &mac_addr);
1478 printf("Port %d: %02X:%02X:%02X:%02X:%02X:%02X\n", pi,
1479 mac_addr.addr_bytes[0], mac_addr.addr_bytes[1],
1480 mac_addr.addr_bytes[2], mac_addr.addr_bytes[3],
1481 mac_addr.addr_bytes[4], mac_addr.addr_bytes[5]);
1483 /* at least one port started, need checking link status */
1484 need_check_link_status = 1;
1487 if (need_check_link_status == 1 && !no_link_check)
1488 check_all_ports_link_status(RTE_PORT_ALL);
1489 else if (need_check_link_status == 0)
1490 printf("Please stop the ports first\n");
1497 stop_port(portid_t pid)
1500 struct rte_port *port;
1501 int need_check_link_status = 0;
1508 if (port_id_is_invalid(pid, ENABLED_WARN))
1511 printf("Stopping ports...\n");
1513 RTE_ETH_FOREACH_DEV(pi) {
1514 if (pid != pi && pid != (portid_t)RTE_PORT_ALL)
1517 if (port_is_forwarding(pi) != 0 && test_done == 0) {
1518 printf("Please remove port %d from forwarding configuration.\n", pi);
1522 if (port_is_bonding_slave(pi)) {
1523 printf("Please remove port %d from bonded device.\n", pi);
1528 if (rte_atomic16_cmpset(&(port->port_status), RTE_PORT_STARTED,
1529 RTE_PORT_HANDLING) == 0)
1532 rte_eth_dev_stop(pi);
1534 if (rte_atomic16_cmpset(&(port->port_status),
1535 RTE_PORT_HANDLING, RTE_PORT_STOPPED) == 0)
1536 printf("Port %d can not be set into stopped\n", pi);
1537 need_check_link_status = 1;
1539 if (need_check_link_status && !no_link_check)
1540 check_all_ports_link_status(RTE_PORT_ALL);
1546 close_port(portid_t pid)
1549 struct rte_port *port;
1551 if (port_id_is_invalid(pid, ENABLED_WARN))
1554 printf("Closing ports...\n");
1556 RTE_ETH_FOREACH_DEV(pi) {
1557 if (pid != pi && pid != (portid_t)RTE_PORT_ALL)
1560 if (port_is_forwarding(pi) != 0 && test_done == 0) {
1561 printf("Please remove port %d from forwarding configuration.\n", pi);
1565 if (port_is_bonding_slave(pi)) {
1566 printf("Please remove port %d from bonded device.\n", pi);
1571 if (rte_atomic16_cmpset(&(port->port_status),
1572 RTE_PORT_CLOSED, RTE_PORT_CLOSED) == 1) {
1573 printf("Port %d is already closed\n", pi);
1577 if (rte_atomic16_cmpset(&(port->port_status),
1578 RTE_PORT_STOPPED, RTE_PORT_HANDLING) == 0) {
1579 printf("Port %d is now not stopped\n", pi);
1583 if (port->flow_list)
1584 port_flow_flush(pi);
1585 rte_eth_dev_close(pi);
1587 if (rte_atomic16_cmpset(&(port->port_status),
1588 RTE_PORT_HANDLING, RTE_PORT_CLOSED) == 0)
1589 printf("Port %d cannot be set to closed\n", pi);
1596 attach_port(char *identifier)
1599 unsigned int socket_id;
1601 printf("Attaching a new port...\n");
1603 if (identifier == NULL) {
1604 printf("Invalid parameters are specified\n");
1608 if (rte_eth_dev_attach(identifier, &pi))
1611 socket_id = (unsigned)rte_eth_dev_socket_id(pi);
1612 /* if socket_id is invalid, set to 0 */
1613 if (check_socket_id(socket_id) < 0)
1615 reconfig(pi, socket_id);
1616 rte_eth_promiscuous_enable(pi);
1618 nb_ports = rte_eth_dev_count();
1620 ports[pi].port_status = RTE_PORT_STOPPED;
1622 printf("Port %d is attached. Now total ports is %d\n", pi, nb_ports);
1627 detach_port(uint8_t port_id)
1629 char name[RTE_ETH_NAME_MAX_LEN];
1631 printf("Detaching a port...\n");
1633 if (!port_is_closed(port_id)) {
1634 printf("Please close port first\n");
1638 if (ports[port_id].flow_list)
1639 port_flow_flush(port_id);
1641 if (rte_eth_dev_detach(port_id, name))
1644 nb_ports = rte_eth_dev_count();
1646 printf("Port '%s' is detached. Now total ports is %d\n",
1658 stop_packet_forwarding();
1660 if (ports != NULL) {
1662 RTE_ETH_FOREACH_DEV(pt_id) {
1663 printf("\nShutting down port %d...\n", pt_id);
1669 printf("\nBye...\n");
1672 typedef void (*cmd_func_t)(void);
1673 struct pmd_test_command {
1674 const char *cmd_name;
1675 cmd_func_t cmd_func;
1678 #define PMD_TEST_CMD_NB (sizeof(pmd_test_menu) / sizeof(pmd_test_menu[0]))
1680 /* Check the link status of all ports in up to 9s, and print them finally */
1682 check_all_ports_link_status(uint32_t port_mask)
1684 #define CHECK_INTERVAL 100 /* 100ms */
1685 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
1686 uint8_t portid, count, all_ports_up, print_flag = 0;
1687 struct rte_eth_link link;
1689 printf("Checking link statuses...\n");
1691 for (count = 0; count <= MAX_CHECK_TIME; count++) {
1693 RTE_ETH_FOREACH_DEV(portid) {
1694 if ((port_mask & (1 << portid)) == 0)
1696 memset(&link, 0, sizeof(link));
1697 rte_eth_link_get_nowait(portid, &link);
1698 /* print link status if flag set */
1699 if (print_flag == 1) {
1700 if (link.link_status)
1701 printf("Port %d Link Up - speed %u "
1702 "Mbps - %s\n", (uint8_t)portid,
1703 (unsigned)link.link_speed,
1704 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
1705 ("full-duplex") : ("half-duplex\n"));
1707 printf("Port %d Link Down\n",
1711 /* clear all_ports_up flag if any link down */
1712 if (link.link_status == ETH_LINK_DOWN) {
1717 /* after finally printing all link status, get out */
1718 if (print_flag == 1)
1721 if (all_ports_up == 0) {
1723 rte_delay_ms(CHECK_INTERVAL);
1726 /* set the print_flag if all ports up or timeout */
1727 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
1734 set_tx_queue_stats_mapping_registers(uint8_t port_id, struct rte_port *port)
1738 uint8_t mapping_found = 0;
1740 for (i = 0; i < nb_tx_queue_stats_mappings; i++) {
1741 if ((tx_queue_stats_mappings[i].port_id == port_id) &&
1742 (tx_queue_stats_mappings[i].queue_id < nb_txq )) {
1743 diag = rte_eth_dev_set_tx_queue_stats_mapping(port_id,
1744 tx_queue_stats_mappings[i].queue_id,
1745 tx_queue_stats_mappings[i].stats_counter_id);
1752 port->tx_queue_stats_mapping_enabled = 1;
1757 set_rx_queue_stats_mapping_registers(uint8_t port_id, struct rte_port *port)
1761 uint8_t mapping_found = 0;
1763 for (i = 0; i < nb_rx_queue_stats_mappings; i++) {
1764 if ((rx_queue_stats_mappings[i].port_id == port_id) &&
1765 (rx_queue_stats_mappings[i].queue_id < nb_rxq )) {
1766 diag = rte_eth_dev_set_rx_queue_stats_mapping(port_id,
1767 rx_queue_stats_mappings[i].queue_id,
1768 rx_queue_stats_mappings[i].stats_counter_id);
1775 port->rx_queue_stats_mapping_enabled = 1;
1780 map_port_queue_stats_mapping_registers(uint8_t pi, struct rte_port *port)
1784 diag = set_tx_queue_stats_mapping_registers(pi, port);
1786 if (diag == -ENOTSUP) {
1787 port->tx_queue_stats_mapping_enabled = 0;
1788 printf("TX queue stats mapping not supported port id=%d\n", pi);
1791 rte_exit(EXIT_FAILURE,
1792 "set_tx_queue_stats_mapping_registers "
1793 "failed for port id=%d diag=%d\n",
1797 diag = set_rx_queue_stats_mapping_registers(pi, port);
1799 if (diag == -ENOTSUP) {
1800 port->rx_queue_stats_mapping_enabled = 0;
1801 printf("RX queue stats mapping not supported port id=%d\n", pi);
1804 rte_exit(EXIT_FAILURE,
1805 "set_rx_queue_stats_mapping_registers "
1806 "failed for port id=%d diag=%d\n",
1812 rxtx_port_config(struct rte_port *port)
1814 port->rx_conf = port->dev_info.default_rxconf;
1815 port->tx_conf = port->dev_info.default_txconf;
1817 /* Check if any RX/TX parameters have been passed */
1818 if (rx_pthresh != RTE_PMD_PARAM_UNSET)
1819 port->rx_conf.rx_thresh.pthresh = rx_pthresh;
1821 if (rx_hthresh != RTE_PMD_PARAM_UNSET)
1822 port->rx_conf.rx_thresh.hthresh = rx_hthresh;
1824 if (rx_wthresh != RTE_PMD_PARAM_UNSET)
1825 port->rx_conf.rx_thresh.wthresh = rx_wthresh;
1827 if (rx_free_thresh != RTE_PMD_PARAM_UNSET)
1828 port->rx_conf.rx_free_thresh = rx_free_thresh;
1830 if (rx_drop_en != RTE_PMD_PARAM_UNSET)
1831 port->rx_conf.rx_drop_en = rx_drop_en;
1833 if (tx_pthresh != RTE_PMD_PARAM_UNSET)
1834 port->tx_conf.tx_thresh.pthresh = tx_pthresh;
1836 if (tx_hthresh != RTE_PMD_PARAM_UNSET)
1837 port->tx_conf.tx_thresh.hthresh = tx_hthresh;
1839 if (tx_wthresh != RTE_PMD_PARAM_UNSET)
1840 port->tx_conf.tx_thresh.wthresh = tx_wthresh;
1842 if (tx_rs_thresh != RTE_PMD_PARAM_UNSET)
1843 port->tx_conf.tx_rs_thresh = tx_rs_thresh;
1845 if (tx_free_thresh != RTE_PMD_PARAM_UNSET)
1846 port->tx_conf.tx_free_thresh = tx_free_thresh;
1848 if (txq_flags != RTE_PMD_PARAM_UNSET)
1849 port->tx_conf.txq_flags = txq_flags;
1853 init_port_config(void)
1856 struct rte_port *port;
1858 RTE_ETH_FOREACH_DEV(pid) {
1860 port->dev_conf.rxmode = rx_mode;
1861 port->dev_conf.fdir_conf = fdir_conf;
1863 port->dev_conf.rx_adv_conf.rss_conf.rss_key = NULL;
1864 port->dev_conf.rx_adv_conf.rss_conf.rss_hf = rss_hf;
1866 port->dev_conf.rx_adv_conf.rss_conf.rss_key = NULL;
1867 port->dev_conf.rx_adv_conf.rss_conf.rss_hf = 0;
1870 if (port->dcb_flag == 0) {
1871 if( port->dev_conf.rx_adv_conf.rss_conf.rss_hf != 0)
1872 port->dev_conf.rxmode.mq_mode = ETH_MQ_RX_RSS;
1874 port->dev_conf.rxmode.mq_mode = ETH_MQ_RX_NONE;
1877 rxtx_port_config(port);
1879 rte_eth_macaddr_get(pid, &port->eth_addr);
1881 map_port_queue_stats_mapping_registers(pid, port);
1882 #ifdef RTE_NIC_BYPASS
1883 rte_eth_dev_bypass_init(pid);
1888 void set_port_slave_flag(portid_t slave_pid)
1890 struct rte_port *port;
1892 port = &ports[slave_pid];
1893 port->slave_flag = 1;
1896 void clear_port_slave_flag(portid_t slave_pid)
1898 struct rte_port *port;
1900 port = &ports[slave_pid];
1901 port->slave_flag = 0;
1904 uint8_t port_is_bonding_slave(portid_t slave_pid)
1906 struct rte_port *port;
1908 port = &ports[slave_pid];
1909 return port->slave_flag;
1912 const uint16_t vlan_tags[] = {
1913 0, 1, 2, 3, 4, 5, 6, 7,
1914 8, 9, 10, 11, 12, 13, 14, 15,
1915 16, 17, 18, 19, 20, 21, 22, 23,
1916 24, 25, 26, 27, 28, 29, 30, 31
1920 get_eth_dcb_conf(struct rte_eth_conf *eth_conf,
1921 enum dcb_mode_enable dcb_mode,
1922 enum rte_eth_nb_tcs num_tcs,
1928 * Builds up the correct configuration for dcb+vt based on the vlan tags array
1929 * given above, and the number of traffic classes available for use.
1931 if (dcb_mode == DCB_VT_ENABLED) {
1932 struct rte_eth_vmdq_dcb_conf *vmdq_rx_conf =
1933 ð_conf->rx_adv_conf.vmdq_dcb_conf;
1934 struct rte_eth_vmdq_dcb_tx_conf *vmdq_tx_conf =
1935 ð_conf->tx_adv_conf.vmdq_dcb_tx_conf;
1937 /* VMDQ+DCB RX and TX configurations */
1938 vmdq_rx_conf->enable_default_pool = 0;
1939 vmdq_rx_conf->default_pool = 0;
1940 vmdq_rx_conf->nb_queue_pools =
1941 (num_tcs == ETH_4_TCS ? ETH_32_POOLS : ETH_16_POOLS);
1942 vmdq_tx_conf->nb_queue_pools =
1943 (num_tcs == ETH_4_TCS ? ETH_32_POOLS : ETH_16_POOLS);
1945 vmdq_rx_conf->nb_pool_maps = vmdq_rx_conf->nb_queue_pools;
1946 for (i = 0; i < vmdq_rx_conf->nb_pool_maps; i++) {
1947 vmdq_rx_conf->pool_map[i].vlan_id = vlan_tags[i];
1948 vmdq_rx_conf->pool_map[i].pools =
1949 1 << (i % vmdq_rx_conf->nb_queue_pools);
1951 for (i = 0; i < ETH_DCB_NUM_USER_PRIORITIES; i++) {
1952 vmdq_rx_conf->dcb_tc[i] = i;
1953 vmdq_tx_conf->dcb_tc[i] = i;
1956 /* set DCB mode of RX and TX of multiple queues */
1957 eth_conf->rxmode.mq_mode = ETH_MQ_RX_VMDQ_DCB;
1958 eth_conf->txmode.mq_mode = ETH_MQ_TX_VMDQ_DCB;
1960 struct rte_eth_dcb_rx_conf *rx_conf =
1961 ð_conf->rx_adv_conf.dcb_rx_conf;
1962 struct rte_eth_dcb_tx_conf *tx_conf =
1963 ð_conf->tx_adv_conf.dcb_tx_conf;
1965 rx_conf->nb_tcs = num_tcs;
1966 tx_conf->nb_tcs = num_tcs;
1968 for (i = 0; i < ETH_DCB_NUM_USER_PRIORITIES; i++) {
1969 rx_conf->dcb_tc[i] = i % num_tcs;
1970 tx_conf->dcb_tc[i] = i % num_tcs;
1972 eth_conf->rxmode.mq_mode = ETH_MQ_RX_DCB_RSS;
1973 eth_conf->rx_adv_conf.rss_conf.rss_hf = rss_hf;
1974 eth_conf->txmode.mq_mode = ETH_MQ_TX_DCB;
1978 eth_conf->dcb_capability_en =
1979 ETH_DCB_PG_SUPPORT | ETH_DCB_PFC_SUPPORT;
1981 eth_conf->dcb_capability_en = ETH_DCB_PG_SUPPORT;
1987 init_port_dcb_config(portid_t pid,
1988 enum dcb_mode_enable dcb_mode,
1989 enum rte_eth_nb_tcs num_tcs,
1992 struct rte_eth_conf port_conf;
1993 struct rte_port *rte_port;
1997 rte_port = &ports[pid];
1999 memset(&port_conf, 0, sizeof(struct rte_eth_conf));
2000 /* Enter DCB configuration status */
2003 /*set configuration of DCB in vt mode and DCB in non-vt mode*/
2004 retval = get_eth_dcb_conf(&port_conf, dcb_mode, num_tcs, pfc_en);
2007 port_conf.rxmode.hw_vlan_filter = 1;
2010 * Write the configuration into the device.
2011 * Set the numbers of RX & TX queues to 0, so
2012 * the RX & TX queues will not be setup.
2014 (void)rte_eth_dev_configure(pid, 0, 0, &port_conf);
2016 rte_eth_dev_info_get(pid, &rte_port->dev_info);
2018 /* If dev_info.vmdq_pool_base is greater than 0,
2019 * the queue id of vmdq pools is started after pf queues.
2021 if (dcb_mode == DCB_VT_ENABLED &&
2022 rte_port->dev_info.vmdq_pool_base > 0) {
2023 printf("VMDQ_DCB multi-queue mode is nonsensical"
2024 " for port %d.", pid);
2028 /* Assume the ports in testpmd have the same dcb capability
2029 * and has the same number of rxq and txq in dcb mode
2031 if (dcb_mode == DCB_VT_ENABLED) {
2032 if (rte_port->dev_info.max_vfs > 0) {
2033 nb_rxq = rte_port->dev_info.nb_rx_queues;
2034 nb_txq = rte_port->dev_info.nb_tx_queues;
2036 nb_rxq = rte_port->dev_info.max_rx_queues;
2037 nb_txq = rte_port->dev_info.max_tx_queues;
2040 /*if vt is disabled, use all pf queues */
2041 if (rte_port->dev_info.vmdq_pool_base == 0) {
2042 nb_rxq = rte_port->dev_info.max_rx_queues;
2043 nb_txq = rte_port->dev_info.max_tx_queues;
2045 nb_rxq = (queueid_t)num_tcs;
2046 nb_txq = (queueid_t)num_tcs;
2050 rx_free_thresh = 64;
2052 memcpy(&rte_port->dev_conf, &port_conf, sizeof(struct rte_eth_conf));
2054 rxtx_port_config(rte_port);
2056 rte_port->dev_conf.rxmode.hw_vlan_filter = 1;
2057 for (i = 0; i < RTE_DIM(vlan_tags); i++)
2058 rx_vft_set(pid, vlan_tags[i], 1);
2060 rte_eth_macaddr_get(pid, &rte_port->eth_addr);
2061 map_port_queue_stats_mapping_registers(pid, rte_port);
2063 rte_port->dcb_flag = 1;
2071 /* Configuration of Ethernet ports. */
2072 ports = rte_zmalloc("testpmd: ports",
2073 sizeof(struct rte_port) * RTE_MAX_ETHPORTS,
2074 RTE_CACHE_LINE_SIZE);
2075 if (ports == NULL) {
2076 rte_exit(EXIT_FAILURE,
2077 "rte_zmalloc(%d struct rte_port) failed\n",
2090 signal_handler(int signum)
2092 if (signum == SIGINT || signum == SIGTERM) {
2093 printf("\nSignal %d received, preparing to exit...\n",
2095 #ifdef RTE_LIBRTE_PDUMP
2096 /* uninitialize packet capture framework */
2099 #ifdef RTE_LIBRTE_LATENCY_STATS
2100 rte_latencystats_uninit();
2103 /* exit with the expected status */
2104 signal(signum, SIG_DFL);
2105 kill(getpid(), signum);
2110 main(int argc, char** argv)
2115 signal(SIGINT, signal_handler);
2116 signal(SIGTERM, signal_handler);
2118 diag = rte_eal_init(argc, argv);
2120 rte_panic("Cannot init EAL\n");
2122 #ifdef RTE_LIBRTE_PDUMP
2123 /* initialize packet capture framework */
2124 rte_pdump_init(NULL);
2127 nb_ports = (portid_t) rte_eth_dev_count();
2129 RTE_LOG(WARNING, EAL, "No probed ethernet devices\n");
2131 /* allocate port structures, and init them */
2134 set_def_fwd_config();
2136 rte_panic("Empty set of forwarding logical cores - check the "
2137 "core mask supplied in the command parameters\n");
2142 launch_args_parse(argc, argv);
2144 if (!nb_rxq && !nb_txq)
2145 printf("Warning: Either rx or tx queues should be non-zero\n");
2147 if (nb_rxq > 1 && nb_rxq > nb_txq)
2148 printf("Warning: nb_rxq=%d enables RSS configuration, "
2149 "but nb_txq=%d will prevent to fully test it.\n",
2153 if (start_port(RTE_PORT_ALL) != 0)
2154 rte_exit(EXIT_FAILURE, "Start ports failed\n");
2156 /* set all ports to promiscuous mode by default */
2157 RTE_ETH_FOREACH_DEV(port_id)
2158 rte_eth_promiscuous_enable(port_id);
2160 /* Init metrics library */
2161 rte_metrics_init(rte_socket_id());
2163 #ifdef RTE_LIBRTE_LATENCY_STATS
2164 if (latencystats_enabled != 0) {
2165 int ret = rte_latencystats_init(1, NULL);
2167 printf("Warning: latencystats init()"
2168 " returned error %d\n", ret);
2169 printf("Latencystats running on lcore %d\n",
2170 latencystats_lcore_id);
2174 /* Setup bitrate stats */
2175 #ifdef RTE_LIBRTE_BITRATE
2176 bitrate_data = rte_stats_bitrate_create();
2177 if (bitrate_data == NULL)
2178 rte_exit(EXIT_FAILURE, "Could not allocate bitrate data.\n");
2179 rte_stats_bitrate_reg(bitrate_data);
2183 #ifdef RTE_LIBRTE_CMDLINE
2184 if (interactive == 1) {
2186 printf("Start automatic packet forwarding\n");
2187 start_packet_forwarding(0);
2196 printf("No commandline core given, start packet forwarding\n");
2197 start_packet_forwarding(0);
2198 printf("Press enter to exit\n");
2199 rc = read(0, &c, 1);