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>
89 uint16_t verbose_level = 0; /**< Silent by default. */
91 /* use master core for command line ? */
92 uint8_t interactive = 0;
93 uint8_t auto_start = 0;
96 * NUMA support configuration.
97 * When set, the NUMA support attempts to dispatch the allocation of the
98 * RX and TX memory rings, and of the DMA memory buffers (mbufs) for the
99 * probed ports among the CPU sockets 0 and 1.
100 * Otherwise, all memory is allocated from CPU socket 0.
102 uint8_t numa_support = 0; /**< No numa support by default */
105 * In UMA mode,all memory is allocated from socket 0 if --socket-num is
108 uint8_t socket_num = UMA_NO_CONFIG;
111 * Use ANONYMOUS mapped memory (might be not physically continuous) for mbufs.
116 * Record the Ethernet address of peer target ports to which packets are
118 * Must be instantiated with the ethernet addresses of peer traffic generator
121 struct ether_addr peer_eth_addrs[RTE_MAX_ETHPORTS];
122 portid_t nb_peer_eth_addrs = 0;
125 * Probed Target Environment.
127 struct rte_port *ports; /**< For all probed ethernet ports. */
128 portid_t nb_ports; /**< Number of probed ethernet ports. */
129 struct fwd_lcore **fwd_lcores; /**< For all probed logical cores. */
130 lcoreid_t nb_lcores; /**< Number of probed logical cores. */
133 * Test Forwarding Configuration.
134 * nb_fwd_lcores <= nb_cfg_lcores <= nb_lcores
135 * nb_fwd_ports <= nb_cfg_ports <= nb_ports
137 lcoreid_t nb_cfg_lcores; /**< Number of configured logical cores. */
138 lcoreid_t nb_fwd_lcores; /**< Number of forwarding logical cores. */
139 portid_t nb_cfg_ports; /**< Number of configured ports. */
140 portid_t nb_fwd_ports; /**< Number of forwarding ports. */
142 unsigned int fwd_lcores_cpuids[RTE_MAX_LCORE]; /**< CPU ids configuration. */
143 portid_t fwd_ports_ids[RTE_MAX_ETHPORTS]; /**< Port ids configuration. */
145 struct fwd_stream **fwd_streams; /**< For each RX queue of each port. */
146 streamid_t nb_fwd_streams; /**< Is equal to (nb_ports * nb_rxq). */
149 * Forwarding engines.
151 struct fwd_engine * fwd_engines[] = {
160 #ifdef RTE_LIBRTE_IEEE1588
161 &ieee1588_fwd_engine,
166 struct fwd_config cur_fwd_config;
167 struct fwd_engine *cur_fwd_eng = &io_fwd_engine; /**< IO mode by default. */
168 uint32_t retry_enabled;
169 uint32_t burst_tx_delay_time = BURST_TX_WAIT_US;
170 uint32_t burst_tx_retry_num = BURST_TX_RETRIES;
172 uint16_t mbuf_data_size = DEFAULT_MBUF_DATA_SIZE; /**< Mbuf data space size. */
173 uint32_t param_total_num_mbufs = 0; /**< number of mbufs in all pools - if
174 * specified on command-line. */
177 * Configuration of packet segments used by the "txonly" processing engine.
179 uint16_t tx_pkt_length = TXONLY_DEF_PACKET_LEN; /**< TXONLY packet length. */
180 uint16_t tx_pkt_seg_lengths[RTE_MAX_SEGS_PER_PKT] = {
181 TXONLY_DEF_PACKET_LEN,
183 uint8_t tx_pkt_nb_segs = 1; /**< Number of segments in TXONLY packets */
185 enum tx_pkt_split tx_pkt_split = TX_PKT_SPLIT_OFF;
186 /**< Split policy for packets to TX. */
188 uint16_t nb_pkt_per_burst = DEF_PKT_BURST; /**< Number of packets per burst. */
189 uint16_t mb_mempool_cache = DEF_MBUF_CACHE; /**< Size of mbuf mempool cache. */
191 /* current configuration is in DCB or not,0 means it is not in DCB mode */
192 uint8_t dcb_config = 0;
194 /* Whether the dcb is in testing status */
195 uint8_t dcb_test = 0;
198 * Configurable number of RX/TX queues.
200 queueid_t nb_rxq = 1; /**< Number of RX queues per port. */
201 queueid_t nb_txq = 1; /**< Number of TX queues per port. */
204 * Configurable number of RX/TX ring descriptors.
206 #define RTE_TEST_RX_DESC_DEFAULT 128
207 #define RTE_TEST_TX_DESC_DEFAULT 512
208 uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT; /**< Number of RX descriptors. */
209 uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT; /**< Number of TX descriptors. */
211 #define RTE_PMD_PARAM_UNSET -1
213 * Configurable values of RX and TX ring threshold registers.
216 int8_t rx_pthresh = RTE_PMD_PARAM_UNSET;
217 int8_t rx_hthresh = RTE_PMD_PARAM_UNSET;
218 int8_t rx_wthresh = RTE_PMD_PARAM_UNSET;
220 int8_t tx_pthresh = RTE_PMD_PARAM_UNSET;
221 int8_t tx_hthresh = RTE_PMD_PARAM_UNSET;
222 int8_t tx_wthresh = RTE_PMD_PARAM_UNSET;
225 * Configurable value of RX free threshold.
227 int16_t rx_free_thresh = RTE_PMD_PARAM_UNSET;
230 * Configurable value of RX drop enable.
232 int8_t rx_drop_en = RTE_PMD_PARAM_UNSET;
235 * Configurable value of TX free threshold.
237 int16_t tx_free_thresh = RTE_PMD_PARAM_UNSET;
240 * Configurable value of TX RS bit threshold.
242 int16_t tx_rs_thresh = RTE_PMD_PARAM_UNSET;
245 * Configurable value of TX queue flags.
247 int32_t txq_flags = RTE_PMD_PARAM_UNSET;
250 * Receive Side Scaling (RSS) configuration.
252 uint64_t rss_hf = ETH_RSS_IP; /* RSS IP by default. */
255 * Port topology configuration
257 uint16_t port_topology = PORT_TOPOLOGY_PAIRED; /* Ports are paired by default */
260 * Avoids to flush all the RX streams before starts forwarding.
262 uint8_t no_flush_rx = 0; /* flush by default */
265 * Avoids to check link status when starting/stopping a port.
267 uint8_t no_link_check = 0; /* check by default */
270 * NIC bypass mode configuration options.
272 #ifdef RTE_NIC_BYPASS
274 /* The NIC bypass watchdog timeout. */
275 uint32_t bypass_timeout = RTE_BYPASS_TMT_OFF;
280 * Ethernet device configuration.
282 struct rte_eth_rxmode rx_mode = {
283 .max_rx_pkt_len = ETHER_MAX_LEN, /**< Default maximum frame length. */
285 .header_split = 0, /**< Header Split disabled. */
286 .hw_ip_checksum = 0, /**< IP checksum offload disabled. */
287 .hw_vlan_filter = 1, /**< VLAN filtering enabled. */
288 .hw_vlan_strip = 1, /**< VLAN strip enabled. */
289 .hw_vlan_extend = 0, /**< Extended VLAN disabled. */
290 .jumbo_frame = 0, /**< Jumbo Frame Support disabled. */
291 .hw_strip_crc = 0, /**< CRC stripping by hardware disabled. */
294 struct rte_fdir_conf fdir_conf = {
295 .mode = RTE_FDIR_MODE_NONE,
296 .pballoc = RTE_FDIR_PBALLOC_64K,
297 .status = RTE_FDIR_REPORT_STATUS,
299 .vlan_tci_mask = 0x0,
301 .src_ip = 0xFFFFFFFF,
302 .dst_ip = 0xFFFFFFFF,
305 .src_ip = {0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF},
306 .dst_ip = {0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF},
308 .src_port_mask = 0xFFFF,
309 .dst_port_mask = 0xFFFF,
310 .mac_addr_byte_mask = 0xFF,
311 .tunnel_type_mask = 1,
312 .tunnel_id_mask = 0xFFFFFFFF,
317 volatile int test_done = 1; /* stop packet forwarding when set to 1. */
319 struct queue_stats_mappings tx_queue_stats_mappings_array[MAX_TX_QUEUE_STATS_MAPPINGS];
320 struct queue_stats_mappings rx_queue_stats_mappings_array[MAX_RX_QUEUE_STATS_MAPPINGS];
322 struct queue_stats_mappings *tx_queue_stats_mappings = tx_queue_stats_mappings_array;
323 struct queue_stats_mappings *rx_queue_stats_mappings = rx_queue_stats_mappings_array;
325 uint16_t nb_tx_queue_stats_mappings = 0;
326 uint16_t nb_rx_queue_stats_mappings = 0;
328 unsigned max_socket = 0;
330 /* Bitrate statistics */
331 struct rte_stats_bitrates *bitrate_data;
333 /* Forward function declarations */
334 static void map_port_queue_stats_mapping_registers(uint8_t pi, struct rte_port *port);
335 static void check_all_ports_link_status(uint32_t port_mask);
338 * Check if all the ports are started.
339 * If yes, return positive value. If not, return zero.
341 static int all_ports_started(void);
344 * Find next enabled port
347 find_next_port(portid_t p, struct rte_port *ports, int size)
350 rte_exit(-EINVAL, "failed to find a next port id\n");
352 while ((p < size) && (ports[p].enabled == 0))
358 * Setup default configuration.
361 set_default_fwd_lcores_config(void)
365 unsigned int sock_num;
368 for (i = 0; i < RTE_MAX_LCORE; i++) {
369 sock_num = rte_lcore_to_socket_id(i) + 1;
370 if (sock_num > max_socket) {
371 if (sock_num > RTE_MAX_NUMA_NODES)
372 rte_exit(EXIT_FAILURE, "Total sockets greater than %u\n", RTE_MAX_NUMA_NODES);
373 max_socket = sock_num;
375 if (!rte_lcore_is_enabled(i))
377 if (i == rte_get_master_lcore())
379 fwd_lcores_cpuids[nb_lc++] = i;
381 nb_lcores = (lcoreid_t) nb_lc;
382 nb_cfg_lcores = nb_lcores;
387 set_def_peer_eth_addrs(void)
391 for (i = 0; i < RTE_MAX_ETHPORTS; i++) {
392 peer_eth_addrs[i].addr_bytes[0] = ETHER_LOCAL_ADMIN_ADDR;
393 peer_eth_addrs[i].addr_bytes[5] = i;
398 set_default_fwd_ports_config(void)
402 for (pt_id = 0; pt_id < nb_ports; pt_id++)
403 fwd_ports_ids[pt_id] = pt_id;
405 nb_cfg_ports = nb_ports;
406 nb_fwd_ports = nb_ports;
410 set_def_fwd_config(void)
412 set_default_fwd_lcores_config();
413 set_def_peer_eth_addrs();
414 set_default_fwd_ports_config();
418 * Configuration initialisation done once at init time.
421 mbuf_pool_create(uint16_t mbuf_seg_size, unsigned nb_mbuf,
422 unsigned int socket_id)
424 char pool_name[RTE_MEMPOOL_NAMESIZE];
425 struct rte_mempool *rte_mp = NULL;
428 mb_size = sizeof(struct rte_mbuf) + mbuf_seg_size;
429 mbuf_poolname_build(socket_id, pool_name, sizeof(pool_name));
432 "create a new mbuf pool <%s>: n=%u, size=%u, socket=%u\n",
433 pool_name, nb_mbuf, mbuf_seg_size, socket_id);
435 #ifdef RTE_LIBRTE_PMD_XENVIRT
436 rte_mp = rte_mempool_gntalloc_create(pool_name, nb_mbuf, mb_size,
437 (unsigned) mb_mempool_cache,
438 sizeof(struct rte_pktmbuf_pool_private),
439 rte_pktmbuf_pool_init, NULL,
440 rte_pktmbuf_init, NULL,
444 /* if the former XEN allocation failed fall back to normal allocation */
445 if (rte_mp == NULL) {
447 rte_mp = rte_mempool_create_empty(pool_name, nb_mbuf,
448 mb_size, (unsigned) mb_mempool_cache,
449 sizeof(struct rte_pktmbuf_pool_private),
454 if (rte_mempool_populate_anon(rte_mp) == 0) {
455 rte_mempool_free(rte_mp);
459 rte_pktmbuf_pool_init(rte_mp, NULL);
460 rte_mempool_obj_iter(rte_mp, rte_pktmbuf_init, NULL);
462 /* wrapper to rte_mempool_create() */
463 rte_mp = rte_pktmbuf_pool_create(pool_name, nb_mbuf,
464 mb_mempool_cache, 0, mbuf_seg_size, socket_id);
469 if (rte_mp == NULL) {
470 rte_exit(EXIT_FAILURE,
471 "Creation of mbuf pool for socket %u failed: %s\n",
472 socket_id, rte_strerror(rte_errno));
473 } else if (verbose_level > 0) {
474 rte_mempool_dump(stdout, rte_mp);
479 * Check given socket id is valid or not with NUMA mode,
480 * if valid, return 0, else return -1
483 check_socket_id(const unsigned int socket_id)
485 static int warning_once = 0;
487 if (socket_id >= max_socket) {
488 if (!warning_once && numa_support)
489 printf("Warning: NUMA should be configured manually by"
490 " using --port-numa-config and"
491 " --ring-numa-config parameters along with"
503 struct rte_port *port;
504 struct rte_mempool *mbp;
505 unsigned int nb_mbuf_per_pool;
507 uint8_t port_per_socket[RTE_MAX_NUMA_NODES];
509 memset(port_per_socket,0,RTE_MAX_NUMA_NODES);
510 /* Configuration of logical cores. */
511 fwd_lcores = rte_zmalloc("testpmd: fwd_lcores",
512 sizeof(struct fwd_lcore *) * nb_lcores,
513 RTE_CACHE_LINE_SIZE);
514 if (fwd_lcores == NULL) {
515 rte_exit(EXIT_FAILURE, "rte_zmalloc(%d (struct fwd_lcore *)) "
516 "failed\n", nb_lcores);
518 for (lc_id = 0; lc_id < nb_lcores; lc_id++) {
519 fwd_lcores[lc_id] = rte_zmalloc("testpmd: struct fwd_lcore",
520 sizeof(struct fwd_lcore),
521 RTE_CACHE_LINE_SIZE);
522 if (fwd_lcores[lc_id] == NULL) {
523 rte_exit(EXIT_FAILURE, "rte_zmalloc(struct fwd_lcore) "
526 fwd_lcores[lc_id]->cpuid_idx = lc_id;
530 * Create pools of mbuf.
531 * If NUMA support is disabled, create a single pool of mbuf in
532 * socket 0 memory by default.
533 * Otherwise, create a pool of mbuf in the memory of sockets 0 and 1.
535 * Use the maximum value of nb_rxd and nb_txd here, then nb_rxd and
536 * nb_txd can be configured at run time.
538 if (param_total_num_mbufs)
539 nb_mbuf_per_pool = param_total_num_mbufs;
541 nb_mbuf_per_pool = RTE_TEST_RX_DESC_MAX + (nb_lcores * mb_mempool_cache)
542 + RTE_TEST_TX_DESC_MAX + MAX_PKT_BURST;
546 (nb_mbuf_per_pool * RTE_MAX_ETHPORTS);
550 if (socket_num == UMA_NO_CONFIG)
551 mbuf_pool_create(mbuf_data_size, nb_mbuf_per_pool, 0);
553 mbuf_pool_create(mbuf_data_size, nb_mbuf_per_pool,
557 FOREACH_PORT(pid, ports) {
559 rte_eth_dev_info_get(pid, &port->dev_info);
562 if (port_numa[pid] != NUMA_NO_CONFIG)
563 port_per_socket[port_numa[pid]]++;
565 uint32_t socket_id = rte_eth_dev_socket_id(pid);
567 /* if socket_id is invalid, set to 0 */
568 if (check_socket_id(socket_id) < 0)
570 port_per_socket[socket_id]++;
574 /* set flag to initialize port/queue */
575 port->need_reconfig = 1;
576 port->need_reconfig_queues = 1;
581 unsigned int nb_mbuf;
583 if (param_total_num_mbufs)
584 nb_mbuf_per_pool = nb_mbuf_per_pool/nb_ports;
586 for (i = 0; i < max_socket; i++) {
587 nb_mbuf = (nb_mbuf_per_pool * RTE_MAX_ETHPORTS);
589 mbuf_pool_create(mbuf_data_size,
596 * Records which Mbuf pool to use by each logical core, if needed.
598 for (lc_id = 0; lc_id < nb_lcores; lc_id++) {
599 mbp = mbuf_pool_find(
600 rte_lcore_to_socket_id(fwd_lcores_cpuids[lc_id]));
603 mbp = mbuf_pool_find(0);
604 fwd_lcores[lc_id]->mbp = mbp;
607 /* Configuration of packet forwarding streams. */
608 if (init_fwd_streams() < 0)
609 rte_exit(EXIT_FAILURE, "FAIL from init_fwd_streams()\n");
616 reconfig(portid_t new_port_id, unsigned socket_id)
618 struct rte_port *port;
620 /* Reconfiguration of Ethernet ports. */
621 port = &ports[new_port_id];
622 rte_eth_dev_info_get(new_port_id, &port->dev_info);
624 /* set flag to initialize port/queue */
625 port->need_reconfig = 1;
626 port->need_reconfig_queues = 1;
627 port->socket_id = socket_id;
634 init_fwd_streams(void)
637 struct rte_port *port;
638 streamid_t sm_id, nb_fwd_streams_new;
641 /* set socket id according to numa or not */
642 FOREACH_PORT(pid, ports) {
644 if (nb_rxq > port->dev_info.max_rx_queues) {
645 printf("Fail: nb_rxq(%d) is greater than "
646 "max_rx_queues(%d)\n", nb_rxq,
647 port->dev_info.max_rx_queues);
650 if (nb_txq > port->dev_info.max_tx_queues) {
651 printf("Fail: nb_txq(%d) is greater than "
652 "max_tx_queues(%d)\n", nb_txq,
653 port->dev_info.max_tx_queues);
657 if (port_numa[pid] != NUMA_NO_CONFIG)
658 port->socket_id = port_numa[pid];
660 port->socket_id = rte_eth_dev_socket_id(pid);
662 /* if socket_id is invalid, set to 0 */
663 if (check_socket_id(port->socket_id) < 0)
668 if (socket_num == UMA_NO_CONFIG)
671 port->socket_id = socket_num;
675 q = RTE_MAX(nb_rxq, nb_txq);
677 printf("Fail: Cannot allocate fwd streams as number of queues is 0\n");
680 nb_fwd_streams_new = (streamid_t)(nb_ports * q);
681 if (nb_fwd_streams_new == nb_fwd_streams)
684 if (fwd_streams != NULL) {
685 for (sm_id = 0; sm_id < nb_fwd_streams; sm_id++) {
686 if (fwd_streams[sm_id] == NULL)
688 rte_free(fwd_streams[sm_id]);
689 fwd_streams[sm_id] = NULL;
691 rte_free(fwd_streams);
696 nb_fwd_streams = nb_fwd_streams_new;
697 fwd_streams = rte_zmalloc("testpmd: fwd_streams",
698 sizeof(struct fwd_stream *) * nb_fwd_streams, RTE_CACHE_LINE_SIZE);
699 if (fwd_streams == NULL)
700 rte_exit(EXIT_FAILURE, "rte_zmalloc(%d (struct fwd_stream *)) "
701 "failed\n", nb_fwd_streams);
703 for (sm_id = 0; sm_id < nb_fwd_streams; sm_id++) {
704 fwd_streams[sm_id] = rte_zmalloc("testpmd: struct fwd_stream",
705 sizeof(struct fwd_stream), RTE_CACHE_LINE_SIZE);
706 if (fwd_streams[sm_id] == NULL)
707 rte_exit(EXIT_FAILURE, "rte_zmalloc(struct fwd_stream)"
714 #ifdef RTE_TEST_PMD_RECORD_BURST_STATS
716 pkt_burst_stats_display(const char *rx_tx, struct pkt_burst_stats *pbs)
718 unsigned int total_burst;
719 unsigned int nb_burst;
720 unsigned int burst_stats[3];
721 uint16_t pktnb_stats[3];
723 int burst_percent[3];
726 * First compute the total number of packet bursts and the
727 * two highest numbers of bursts of the same number of packets.
730 burst_stats[0] = burst_stats[1] = burst_stats[2] = 0;
731 pktnb_stats[0] = pktnb_stats[1] = pktnb_stats[2] = 0;
732 for (nb_pkt = 0; nb_pkt < MAX_PKT_BURST; nb_pkt++) {
733 nb_burst = pbs->pkt_burst_spread[nb_pkt];
736 total_burst += nb_burst;
737 if (nb_burst > burst_stats[0]) {
738 burst_stats[1] = burst_stats[0];
739 pktnb_stats[1] = pktnb_stats[0];
740 burst_stats[0] = nb_burst;
741 pktnb_stats[0] = nb_pkt;
744 if (total_burst == 0)
746 burst_percent[0] = (burst_stats[0] * 100) / total_burst;
747 printf(" %s-bursts : %u [%d%% of %d pkts", rx_tx, total_burst,
748 burst_percent[0], (int) pktnb_stats[0]);
749 if (burst_stats[0] == total_burst) {
753 if (burst_stats[0] + burst_stats[1] == total_burst) {
754 printf(" + %d%% of %d pkts]\n",
755 100 - burst_percent[0], pktnb_stats[1]);
758 burst_percent[1] = (burst_stats[1] * 100) / total_burst;
759 burst_percent[2] = 100 - (burst_percent[0] + burst_percent[1]);
760 if ((burst_percent[1] == 0) || (burst_percent[2] == 0)) {
761 printf(" + %d%% of others]\n", 100 - burst_percent[0]);
764 printf(" + %d%% of %d pkts + %d%% of others]\n",
765 burst_percent[1], (int) pktnb_stats[1], burst_percent[2]);
767 #endif /* RTE_TEST_PMD_RECORD_BURST_STATS */
770 fwd_port_stats_display(portid_t port_id, struct rte_eth_stats *stats)
772 struct rte_port *port;
775 static const char *fwd_stats_border = "----------------------";
777 port = &ports[port_id];
778 printf("\n %s Forward statistics for port %-2d %s\n",
779 fwd_stats_border, port_id, fwd_stats_border);
781 if ((!port->rx_queue_stats_mapping_enabled) && (!port->tx_queue_stats_mapping_enabled)) {
782 printf(" RX-packets: %-14"PRIu64" RX-dropped: %-14"PRIu64"RX-total: "
784 stats->ipackets, stats->imissed,
785 (uint64_t) (stats->ipackets + stats->imissed));
787 if (cur_fwd_eng == &csum_fwd_engine)
788 printf(" Bad-ipcsum: %-14"PRIu64" Bad-l4csum: %-14"PRIu64" \n",
789 port->rx_bad_ip_csum, port->rx_bad_l4_csum);
790 if ((stats->ierrors + stats->rx_nombuf) > 0) {
791 printf(" RX-error: %-"PRIu64"\n", stats->ierrors);
792 printf(" RX-nombufs: %-14"PRIu64"\n", stats->rx_nombuf);
795 printf(" TX-packets: %-14"PRIu64" TX-dropped: %-14"PRIu64"TX-total: "
797 stats->opackets, port->tx_dropped,
798 (uint64_t) (stats->opackets + port->tx_dropped));
801 printf(" RX-packets: %14"PRIu64" RX-dropped:%14"PRIu64" RX-total:"
803 stats->ipackets, stats->imissed,
804 (uint64_t) (stats->ipackets + stats->imissed));
806 if (cur_fwd_eng == &csum_fwd_engine)
807 printf(" Bad-ipcsum:%14"PRIu64" Bad-l4csum:%14"PRIu64"\n",
808 port->rx_bad_ip_csum, port->rx_bad_l4_csum);
809 if ((stats->ierrors + stats->rx_nombuf) > 0) {
810 printf(" RX-error:%"PRIu64"\n", stats->ierrors);
811 printf(" RX-nombufs: %14"PRIu64"\n",
815 printf(" TX-packets: %14"PRIu64" TX-dropped:%14"PRIu64" TX-total:"
817 stats->opackets, port->tx_dropped,
818 (uint64_t) (stats->opackets + port->tx_dropped));
821 #ifdef RTE_TEST_PMD_RECORD_BURST_STATS
823 pkt_burst_stats_display("RX",
824 &port->rx_stream->rx_burst_stats);
826 pkt_burst_stats_display("TX",
827 &port->tx_stream->tx_burst_stats);
830 if (port->rx_queue_stats_mapping_enabled) {
832 for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS; i++) {
833 printf(" Stats reg %2d RX-packets:%14"PRIu64
834 " RX-errors:%14"PRIu64
835 " RX-bytes:%14"PRIu64"\n",
836 i, stats->q_ipackets[i], stats->q_errors[i], stats->q_ibytes[i]);
840 if (port->tx_queue_stats_mapping_enabled) {
841 for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS; i++) {
842 printf(" Stats reg %2d TX-packets:%14"PRIu64
843 " TX-bytes:%14"PRIu64"\n",
844 i, stats->q_opackets[i], stats->q_obytes[i]);
848 printf(" %s--------------------------------%s\n",
849 fwd_stats_border, fwd_stats_border);
853 fwd_stream_stats_display(streamid_t stream_id)
855 struct fwd_stream *fs;
856 static const char *fwd_top_stats_border = "-------";
858 fs = fwd_streams[stream_id];
859 if ((fs->rx_packets == 0) && (fs->tx_packets == 0) &&
860 (fs->fwd_dropped == 0))
862 printf("\n %s Forward Stats for RX Port=%2d/Queue=%2d -> "
863 "TX Port=%2d/Queue=%2d %s\n",
864 fwd_top_stats_border, fs->rx_port, fs->rx_queue,
865 fs->tx_port, fs->tx_queue, fwd_top_stats_border);
866 printf(" RX-packets: %-14u TX-packets: %-14u TX-dropped: %-14u",
867 fs->rx_packets, fs->tx_packets, fs->fwd_dropped);
869 /* if checksum mode */
870 if (cur_fwd_eng == &csum_fwd_engine) {
871 printf(" RX- bad IP checksum: %-14u Rx- bad L4 checksum: "
872 "%-14u\n", fs->rx_bad_ip_csum, fs->rx_bad_l4_csum);
875 #ifdef RTE_TEST_PMD_RECORD_BURST_STATS
876 pkt_burst_stats_display("RX", &fs->rx_burst_stats);
877 pkt_burst_stats_display("TX", &fs->tx_burst_stats);
882 flush_fwd_rx_queues(void)
884 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
891 uint64_t prev_tsc = 0, diff_tsc, cur_tsc, timer_tsc = 0;
892 uint64_t timer_period;
894 /* convert to number of cycles */
895 timer_period = rte_get_timer_hz(); /* 1 second timeout */
897 for (j = 0; j < 2; j++) {
898 for (rxp = 0; rxp < cur_fwd_config.nb_fwd_ports; rxp++) {
899 for (rxq = 0; rxq < nb_rxq; rxq++) {
900 port_id = fwd_ports_ids[rxp];
902 * testpmd can stuck in the below do while loop
903 * if rte_eth_rx_burst() always returns nonzero
904 * packets. So timer is added to exit this loop
905 * after 1sec timer expiry.
907 prev_tsc = rte_rdtsc();
909 nb_rx = rte_eth_rx_burst(port_id, rxq,
910 pkts_burst, MAX_PKT_BURST);
911 for (i = 0; i < nb_rx; i++)
912 rte_pktmbuf_free(pkts_burst[i]);
914 cur_tsc = rte_rdtsc();
915 diff_tsc = cur_tsc - prev_tsc;
916 timer_tsc += diff_tsc;
917 } while ((nb_rx > 0) &&
918 (timer_tsc < timer_period));
922 rte_delay_ms(10); /* wait 10 milli-seconds before retrying */
927 run_pkt_fwd_on_lcore(struct fwd_lcore *fc, packet_fwd_t pkt_fwd)
929 struct fwd_stream **fsm;
932 #ifdef RTE_LIBRTE_BITRATE
933 uint64_t tics_per_1sec;
935 uint64_t tics_current;
936 uint8_t idx_port, cnt_ports;
938 cnt_ports = rte_eth_dev_count();
939 tics_datum = rte_rdtsc();
940 tics_per_1sec = rte_get_timer_hz();
942 fsm = &fwd_streams[fc->stream_idx];
943 nb_fs = fc->stream_nb;
945 for (sm_id = 0; sm_id < nb_fs; sm_id++)
946 (*pkt_fwd)(fsm[sm_id]);
947 #ifdef RTE_LIBRTE_BITRATE
948 tics_current = rte_rdtsc();
949 if (tics_current - tics_datum >= tics_per_1sec) {
950 /* Periodic bitrate calculation */
951 for (idx_port = 0; idx_port < cnt_ports; idx_port++)
952 rte_stats_bitrate_calc(bitrate_data, idx_port);
953 tics_datum = tics_current;
956 } while (! fc->stopped);
960 start_pkt_forward_on_core(void *fwd_arg)
962 run_pkt_fwd_on_lcore((struct fwd_lcore *) fwd_arg,
963 cur_fwd_config.fwd_eng->packet_fwd);
968 * Run the TXONLY packet forwarding engine to send a single burst of packets.
969 * Used to start communication flows in network loopback test configurations.
972 run_one_txonly_burst_on_core(void *fwd_arg)
974 struct fwd_lcore *fwd_lc;
975 struct fwd_lcore tmp_lcore;
977 fwd_lc = (struct fwd_lcore *) fwd_arg;
979 tmp_lcore.stopped = 1;
980 run_pkt_fwd_on_lcore(&tmp_lcore, tx_only_engine.packet_fwd);
985 * Launch packet forwarding:
986 * - Setup per-port forwarding context.
987 * - launch logical cores with their forwarding configuration.
990 launch_packet_forwarding(lcore_function_t *pkt_fwd_on_lcore)
992 port_fwd_begin_t port_fwd_begin;
997 port_fwd_begin = cur_fwd_config.fwd_eng->port_fwd_begin;
998 if (port_fwd_begin != NULL) {
999 for (i = 0; i < cur_fwd_config.nb_fwd_ports; i++)
1000 (*port_fwd_begin)(fwd_ports_ids[i]);
1002 for (i = 0; i < cur_fwd_config.nb_fwd_lcores; i++) {
1003 lc_id = fwd_lcores_cpuids[i];
1004 if ((interactive == 0) || (lc_id != rte_lcore_id())) {
1005 fwd_lcores[i]->stopped = 0;
1006 diag = rte_eal_remote_launch(pkt_fwd_on_lcore,
1007 fwd_lcores[i], lc_id);
1009 printf("launch lcore %u failed - diag=%d\n",
1016 * Launch packet forwarding configuration.
1019 start_packet_forwarding(int with_tx_first)
1021 port_fwd_begin_t port_fwd_begin;
1022 port_fwd_end_t port_fwd_end;
1023 struct rte_port *port;
1028 if (strcmp(cur_fwd_eng->fwd_mode_name, "rxonly") == 0 && !nb_rxq)
1029 rte_exit(EXIT_FAILURE, "rxq are 0, cannot use rxonly fwd mode\n");
1031 if (strcmp(cur_fwd_eng->fwd_mode_name, "txonly") == 0 && !nb_txq)
1032 rte_exit(EXIT_FAILURE, "txq are 0, cannot use txonly fwd mode\n");
1034 if ((strcmp(cur_fwd_eng->fwd_mode_name, "rxonly") != 0 &&
1035 strcmp(cur_fwd_eng->fwd_mode_name, "txonly") != 0) &&
1036 (!nb_rxq || !nb_txq))
1037 rte_exit(EXIT_FAILURE,
1038 "Either rxq or txq are 0, cannot use %s fwd mode\n",
1039 cur_fwd_eng->fwd_mode_name);
1041 if (all_ports_started() == 0) {
1042 printf("Not all ports were started\n");
1045 if (test_done == 0) {
1046 printf("Packet forwarding already started\n");
1050 if (init_fwd_streams() < 0) {
1051 printf("Fail from init_fwd_streams()\n");
1056 for (i = 0; i < nb_fwd_ports; i++) {
1057 pt_id = fwd_ports_ids[i];
1058 port = &ports[pt_id];
1059 if (!port->dcb_flag) {
1060 printf("In DCB mode, all forwarding ports must "
1061 "be configured in this mode.\n");
1065 if (nb_fwd_lcores == 1) {
1066 printf("In DCB mode,the nb forwarding cores "
1067 "should be larger than 1.\n");
1074 flush_fwd_rx_queues();
1077 pkt_fwd_config_display(&cur_fwd_config);
1078 rxtx_config_display();
1080 for (i = 0; i < cur_fwd_config.nb_fwd_ports; i++) {
1081 pt_id = fwd_ports_ids[i];
1082 port = &ports[pt_id];
1083 rte_eth_stats_get(pt_id, &port->stats);
1084 port->tx_dropped = 0;
1086 map_port_queue_stats_mapping_registers(pt_id, port);
1088 for (sm_id = 0; sm_id < cur_fwd_config.nb_fwd_streams; sm_id++) {
1089 fwd_streams[sm_id]->rx_packets = 0;
1090 fwd_streams[sm_id]->tx_packets = 0;
1091 fwd_streams[sm_id]->fwd_dropped = 0;
1092 fwd_streams[sm_id]->rx_bad_ip_csum = 0;
1093 fwd_streams[sm_id]->rx_bad_l4_csum = 0;
1095 #ifdef RTE_TEST_PMD_RECORD_BURST_STATS
1096 memset(&fwd_streams[sm_id]->rx_burst_stats, 0,
1097 sizeof(fwd_streams[sm_id]->rx_burst_stats));
1098 memset(&fwd_streams[sm_id]->tx_burst_stats, 0,
1099 sizeof(fwd_streams[sm_id]->tx_burst_stats));
1101 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
1102 fwd_streams[sm_id]->core_cycles = 0;
1105 if (with_tx_first) {
1106 port_fwd_begin = tx_only_engine.port_fwd_begin;
1107 if (port_fwd_begin != NULL) {
1108 for (i = 0; i < cur_fwd_config.nb_fwd_ports; i++)
1109 (*port_fwd_begin)(fwd_ports_ids[i]);
1111 while (with_tx_first--) {
1112 launch_packet_forwarding(
1113 run_one_txonly_burst_on_core);
1114 rte_eal_mp_wait_lcore();
1116 port_fwd_end = tx_only_engine.port_fwd_end;
1117 if (port_fwd_end != NULL) {
1118 for (i = 0; i < cur_fwd_config.nb_fwd_ports; i++)
1119 (*port_fwd_end)(fwd_ports_ids[i]);
1122 launch_packet_forwarding(start_pkt_forward_on_core);
1126 stop_packet_forwarding(void)
1128 struct rte_eth_stats stats;
1129 struct rte_port *port;
1130 port_fwd_end_t port_fwd_end;
1135 uint64_t total_recv;
1136 uint64_t total_xmit;
1137 uint64_t total_rx_dropped;
1138 uint64_t total_tx_dropped;
1139 uint64_t total_rx_nombuf;
1140 uint64_t tx_dropped;
1141 uint64_t rx_bad_ip_csum;
1142 uint64_t rx_bad_l4_csum;
1143 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
1144 uint64_t fwd_cycles;
1146 static const char *acc_stats_border = "+++++++++++++++";
1149 printf("Packet forwarding not started\n");
1152 printf("Telling cores to stop...");
1153 for (lc_id = 0; lc_id < cur_fwd_config.nb_fwd_lcores; lc_id++)
1154 fwd_lcores[lc_id]->stopped = 1;
1155 printf("\nWaiting for lcores to finish...\n");
1156 rte_eal_mp_wait_lcore();
1157 port_fwd_end = cur_fwd_config.fwd_eng->port_fwd_end;
1158 if (port_fwd_end != NULL) {
1159 for (i = 0; i < cur_fwd_config.nb_fwd_ports; i++) {
1160 pt_id = fwd_ports_ids[i];
1161 (*port_fwd_end)(pt_id);
1164 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
1167 for (sm_id = 0; sm_id < cur_fwd_config.nb_fwd_streams; sm_id++) {
1168 if (cur_fwd_config.nb_fwd_streams >
1169 cur_fwd_config.nb_fwd_ports) {
1170 fwd_stream_stats_display(sm_id);
1171 ports[fwd_streams[sm_id]->tx_port].tx_stream = NULL;
1172 ports[fwd_streams[sm_id]->rx_port].rx_stream = NULL;
1174 ports[fwd_streams[sm_id]->tx_port].tx_stream =
1176 ports[fwd_streams[sm_id]->rx_port].rx_stream =
1179 tx_dropped = ports[fwd_streams[sm_id]->tx_port].tx_dropped;
1180 tx_dropped = (uint64_t) (tx_dropped +
1181 fwd_streams[sm_id]->fwd_dropped);
1182 ports[fwd_streams[sm_id]->tx_port].tx_dropped = tx_dropped;
1185 ports[fwd_streams[sm_id]->rx_port].rx_bad_ip_csum;
1186 rx_bad_ip_csum = (uint64_t) (rx_bad_ip_csum +
1187 fwd_streams[sm_id]->rx_bad_ip_csum);
1188 ports[fwd_streams[sm_id]->rx_port].rx_bad_ip_csum =
1192 ports[fwd_streams[sm_id]->rx_port].rx_bad_l4_csum;
1193 rx_bad_l4_csum = (uint64_t) (rx_bad_l4_csum +
1194 fwd_streams[sm_id]->rx_bad_l4_csum);
1195 ports[fwd_streams[sm_id]->rx_port].rx_bad_l4_csum =
1198 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
1199 fwd_cycles = (uint64_t) (fwd_cycles +
1200 fwd_streams[sm_id]->core_cycles);
1205 total_rx_dropped = 0;
1206 total_tx_dropped = 0;
1207 total_rx_nombuf = 0;
1208 for (i = 0; i < cur_fwd_config.nb_fwd_ports; i++) {
1209 pt_id = fwd_ports_ids[i];
1211 port = &ports[pt_id];
1212 rte_eth_stats_get(pt_id, &stats);
1213 stats.ipackets -= port->stats.ipackets;
1214 port->stats.ipackets = 0;
1215 stats.opackets -= port->stats.opackets;
1216 port->stats.opackets = 0;
1217 stats.ibytes -= port->stats.ibytes;
1218 port->stats.ibytes = 0;
1219 stats.obytes -= port->stats.obytes;
1220 port->stats.obytes = 0;
1221 stats.imissed -= port->stats.imissed;
1222 port->stats.imissed = 0;
1223 stats.oerrors -= port->stats.oerrors;
1224 port->stats.oerrors = 0;
1225 stats.rx_nombuf -= port->stats.rx_nombuf;
1226 port->stats.rx_nombuf = 0;
1228 total_recv += stats.ipackets;
1229 total_xmit += stats.opackets;
1230 total_rx_dropped += stats.imissed;
1231 total_tx_dropped += port->tx_dropped;
1232 total_rx_nombuf += stats.rx_nombuf;
1234 fwd_port_stats_display(pt_id, &stats);
1236 printf("\n %s Accumulated forward statistics for all ports"
1238 acc_stats_border, acc_stats_border);
1239 printf(" RX-packets: %-14"PRIu64" RX-dropped: %-14"PRIu64"RX-total: "
1241 " TX-packets: %-14"PRIu64" TX-dropped: %-14"PRIu64"TX-total: "
1243 total_recv, total_rx_dropped, total_recv + total_rx_dropped,
1244 total_xmit, total_tx_dropped, total_xmit + total_tx_dropped);
1245 if (total_rx_nombuf > 0)
1246 printf(" RX-nombufs: %-14"PRIu64"\n", total_rx_nombuf);
1247 printf(" %s++++++++++++++++++++++++++++++++++++++++++++++"
1249 acc_stats_border, acc_stats_border);
1250 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
1252 printf("\n CPU cycles/packet=%u (total cycles="
1253 "%"PRIu64" / total RX packets=%"PRIu64")\n",
1254 (unsigned int)(fwd_cycles / total_recv),
1255 fwd_cycles, total_recv);
1257 printf("\nDone.\n");
1262 dev_set_link_up(portid_t pid)
1264 if (rte_eth_dev_set_link_up((uint8_t)pid) < 0)
1265 printf("\nSet link up fail.\n");
1269 dev_set_link_down(portid_t pid)
1271 if (rte_eth_dev_set_link_down((uint8_t)pid) < 0)
1272 printf("\nSet link down fail.\n");
1276 all_ports_started(void)
1279 struct rte_port *port;
1281 FOREACH_PORT(pi, ports) {
1283 /* Check if there is a port which is not started */
1284 if ((port->port_status != RTE_PORT_STARTED) &&
1285 (port->slave_flag == 0))
1289 /* No port is not started */
1294 all_ports_stopped(void)
1297 struct rte_port *port;
1299 FOREACH_PORT(pi, ports) {
1301 if ((port->port_status != RTE_PORT_STOPPED) &&
1302 (port->slave_flag == 0))
1310 port_is_started(portid_t port_id)
1312 if (port_id_is_invalid(port_id, ENABLED_WARN))
1315 if (ports[port_id].port_status != RTE_PORT_STARTED)
1322 port_is_closed(portid_t port_id)
1324 if (port_id_is_invalid(port_id, ENABLED_WARN))
1327 if (ports[port_id].port_status != RTE_PORT_CLOSED)
1334 start_port(portid_t pid)
1336 int diag, need_check_link_status = -1;
1339 struct rte_port *port;
1340 struct ether_addr mac_addr;
1342 if (port_id_is_invalid(pid, ENABLED_WARN))
1347 FOREACH_PORT(pi, ports) {
1348 if (pid != pi && pid != (portid_t)RTE_PORT_ALL)
1351 need_check_link_status = 0;
1353 if (rte_atomic16_cmpset(&(port->port_status), RTE_PORT_STOPPED,
1354 RTE_PORT_HANDLING) == 0) {
1355 printf("Port %d is now not stopped\n", pi);
1359 if (port->need_reconfig > 0) {
1360 port->need_reconfig = 0;
1362 printf("Configuring Port %d (socket %u)\n", pi,
1364 /* configure port */
1365 diag = rte_eth_dev_configure(pi, nb_rxq, nb_txq,
1368 if (rte_atomic16_cmpset(&(port->port_status),
1369 RTE_PORT_HANDLING, RTE_PORT_STOPPED) == 0)
1370 printf("Port %d can not be set back "
1371 "to stopped\n", pi);
1372 printf("Fail to configure port %d\n", pi);
1373 /* try to reconfigure port next time */
1374 port->need_reconfig = 1;
1378 if (port->need_reconfig_queues > 0) {
1379 port->need_reconfig_queues = 0;
1380 /* setup tx queues */
1381 for (qi = 0; qi < nb_txq; qi++) {
1382 if ((numa_support) &&
1383 (txring_numa[pi] != NUMA_NO_CONFIG))
1384 diag = rte_eth_tx_queue_setup(pi, qi,
1385 nb_txd,txring_numa[pi],
1388 diag = rte_eth_tx_queue_setup(pi, qi,
1389 nb_txd,port->socket_id,
1395 /* Fail to setup tx queue, return */
1396 if (rte_atomic16_cmpset(&(port->port_status),
1398 RTE_PORT_STOPPED) == 0)
1399 printf("Port %d can not be set back "
1400 "to stopped\n", pi);
1401 printf("Fail to configure port %d tx queues\n", pi);
1402 /* try to reconfigure queues next time */
1403 port->need_reconfig_queues = 1;
1406 /* setup rx queues */
1407 for (qi = 0; qi < nb_rxq; qi++) {
1408 if ((numa_support) &&
1409 (rxring_numa[pi] != NUMA_NO_CONFIG)) {
1410 struct rte_mempool * mp =
1411 mbuf_pool_find(rxring_numa[pi]);
1413 printf("Failed to setup RX queue:"
1414 "No mempool allocation"
1415 " on the socket %d\n",
1420 diag = rte_eth_rx_queue_setup(pi, qi,
1421 nb_rxd,rxring_numa[pi],
1422 &(port->rx_conf),mp);
1424 struct rte_mempool *mp =
1425 mbuf_pool_find(port->socket_id);
1427 printf("Failed to setup RX queue:"
1428 "No mempool allocation"
1429 " on the socket %d\n",
1433 diag = rte_eth_rx_queue_setup(pi, qi,
1434 nb_rxd,port->socket_id,
1435 &(port->rx_conf), mp);
1440 /* Fail to setup rx queue, return */
1441 if (rte_atomic16_cmpset(&(port->port_status),
1443 RTE_PORT_STOPPED) == 0)
1444 printf("Port %d can not be set back "
1445 "to stopped\n", pi);
1446 printf("Fail to configure port %d rx queues\n", pi);
1447 /* try to reconfigure queues next time */
1448 port->need_reconfig_queues = 1;
1453 if (rte_eth_dev_start(pi) < 0) {
1454 printf("Fail to start port %d\n", pi);
1456 /* Fail to setup rx queue, return */
1457 if (rte_atomic16_cmpset(&(port->port_status),
1458 RTE_PORT_HANDLING, RTE_PORT_STOPPED) == 0)
1459 printf("Port %d can not be set back to "
1464 if (rte_atomic16_cmpset(&(port->port_status),
1465 RTE_PORT_HANDLING, RTE_PORT_STARTED) == 0)
1466 printf("Port %d can not be set into started\n", pi);
1468 rte_eth_macaddr_get(pi, &mac_addr);
1469 printf("Port %d: %02X:%02X:%02X:%02X:%02X:%02X\n", pi,
1470 mac_addr.addr_bytes[0], mac_addr.addr_bytes[1],
1471 mac_addr.addr_bytes[2], mac_addr.addr_bytes[3],
1472 mac_addr.addr_bytes[4], mac_addr.addr_bytes[5]);
1474 /* at least one port started, need checking link status */
1475 need_check_link_status = 1;
1478 if (need_check_link_status == 1 && !no_link_check)
1479 check_all_ports_link_status(RTE_PORT_ALL);
1480 else if (need_check_link_status == 0)
1481 printf("Please stop the ports first\n");
1488 stop_port(portid_t pid)
1491 struct rte_port *port;
1492 int need_check_link_status = 0;
1499 if (port_id_is_invalid(pid, ENABLED_WARN))
1502 printf("Stopping ports...\n");
1504 FOREACH_PORT(pi, ports) {
1505 if (pid != pi && pid != (portid_t)RTE_PORT_ALL)
1508 if (port_is_forwarding(pi) != 0 && test_done == 0) {
1509 printf("Please remove port %d from forwarding configuration.\n", pi);
1513 if (port_is_bonding_slave(pi)) {
1514 printf("Please remove port %d from bonded device.\n", pi);
1519 if (rte_atomic16_cmpset(&(port->port_status), RTE_PORT_STARTED,
1520 RTE_PORT_HANDLING) == 0)
1523 rte_eth_dev_stop(pi);
1525 if (rte_atomic16_cmpset(&(port->port_status),
1526 RTE_PORT_HANDLING, RTE_PORT_STOPPED) == 0)
1527 printf("Port %d can not be set into stopped\n", pi);
1528 need_check_link_status = 1;
1530 if (need_check_link_status && !no_link_check)
1531 check_all_ports_link_status(RTE_PORT_ALL);
1537 close_port(portid_t pid)
1540 struct rte_port *port;
1542 if (port_id_is_invalid(pid, ENABLED_WARN))
1545 printf("Closing ports...\n");
1547 FOREACH_PORT(pi, ports) {
1548 if (pid != pi && pid != (portid_t)RTE_PORT_ALL)
1551 if (port_is_forwarding(pi) != 0 && test_done == 0) {
1552 printf("Please remove port %d from forwarding configuration.\n", pi);
1556 if (port_is_bonding_slave(pi)) {
1557 printf("Please remove port %d from bonded device.\n", pi);
1562 if (rte_atomic16_cmpset(&(port->port_status),
1563 RTE_PORT_CLOSED, RTE_PORT_CLOSED) == 1) {
1564 printf("Port %d is already closed\n", pi);
1568 if (rte_atomic16_cmpset(&(port->port_status),
1569 RTE_PORT_STOPPED, RTE_PORT_HANDLING) == 0) {
1570 printf("Port %d is now not stopped\n", pi);
1574 if (port->flow_list)
1575 port_flow_flush(pi);
1576 rte_eth_dev_close(pi);
1578 if (rte_atomic16_cmpset(&(port->port_status),
1579 RTE_PORT_HANDLING, RTE_PORT_CLOSED) == 0)
1580 printf("Port %d cannot be set to closed\n", pi);
1587 attach_port(char *identifier)
1590 unsigned int socket_id;
1592 printf("Attaching a new port...\n");
1594 if (identifier == NULL) {
1595 printf("Invalid parameters are specified\n");
1599 if (rte_eth_dev_attach(identifier, &pi))
1602 ports[pi].enabled = 1;
1603 socket_id = (unsigned)rte_eth_dev_socket_id(pi);
1604 /* if socket_id is invalid, set to 0 */
1605 if (check_socket_id(socket_id) < 0)
1607 reconfig(pi, socket_id);
1608 rte_eth_promiscuous_enable(pi);
1610 nb_ports = rte_eth_dev_count();
1612 ports[pi].port_status = RTE_PORT_STOPPED;
1614 printf("Port %d is attached. Now total ports is %d\n", pi, nb_ports);
1619 detach_port(uint8_t port_id)
1621 char name[RTE_ETH_NAME_MAX_LEN];
1623 printf("Detaching a port...\n");
1625 if (!port_is_closed(port_id)) {
1626 printf("Please close port first\n");
1630 if (ports[port_id].flow_list)
1631 port_flow_flush(port_id);
1633 if (rte_eth_dev_detach(port_id, name))
1636 ports[port_id].enabled = 0;
1637 nb_ports = rte_eth_dev_count();
1639 printf("Port '%s' is detached. Now total ports is %d\n",
1651 stop_packet_forwarding();
1653 if (ports != NULL) {
1655 FOREACH_PORT(pt_id, ports) {
1656 printf("\nShutting down port %d...\n", pt_id);
1662 printf("\nBye...\n");
1665 typedef void (*cmd_func_t)(void);
1666 struct pmd_test_command {
1667 const char *cmd_name;
1668 cmd_func_t cmd_func;
1671 #define PMD_TEST_CMD_NB (sizeof(pmd_test_menu) / sizeof(pmd_test_menu[0]))
1673 /* Check the link status of all ports in up to 9s, and print them finally */
1675 check_all_ports_link_status(uint32_t port_mask)
1677 #define CHECK_INTERVAL 100 /* 100ms */
1678 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
1679 uint8_t portid, count, all_ports_up, print_flag = 0;
1680 struct rte_eth_link link;
1682 printf("Checking link statuses...\n");
1684 for (count = 0; count <= MAX_CHECK_TIME; count++) {
1686 FOREACH_PORT(portid, ports) {
1687 if ((port_mask & (1 << portid)) == 0)
1689 memset(&link, 0, sizeof(link));
1690 rte_eth_link_get_nowait(portid, &link);
1691 /* print link status if flag set */
1692 if (print_flag == 1) {
1693 if (link.link_status)
1694 printf("Port %d Link Up - speed %u "
1695 "Mbps - %s\n", (uint8_t)portid,
1696 (unsigned)link.link_speed,
1697 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
1698 ("full-duplex") : ("half-duplex\n"));
1700 printf("Port %d Link Down\n",
1704 /* clear all_ports_up flag if any link down */
1705 if (link.link_status == ETH_LINK_DOWN) {
1710 /* after finally printing all link status, get out */
1711 if (print_flag == 1)
1714 if (all_ports_up == 0) {
1716 rte_delay_ms(CHECK_INTERVAL);
1719 /* set the print_flag if all ports up or timeout */
1720 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
1727 set_tx_queue_stats_mapping_registers(uint8_t port_id, struct rte_port *port)
1731 uint8_t mapping_found = 0;
1733 for (i = 0; i < nb_tx_queue_stats_mappings; i++) {
1734 if ((tx_queue_stats_mappings[i].port_id == port_id) &&
1735 (tx_queue_stats_mappings[i].queue_id < nb_txq )) {
1736 diag = rte_eth_dev_set_tx_queue_stats_mapping(port_id,
1737 tx_queue_stats_mappings[i].queue_id,
1738 tx_queue_stats_mappings[i].stats_counter_id);
1745 port->tx_queue_stats_mapping_enabled = 1;
1750 set_rx_queue_stats_mapping_registers(uint8_t port_id, struct rte_port *port)
1754 uint8_t mapping_found = 0;
1756 for (i = 0; i < nb_rx_queue_stats_mappings; i++) {
1757 if ((rx_queue_stats_mappings[i].port_id == port_id) &&
1758 (rx_queue_stats_mappings[i].queue_id < nb_rxq )) {
1759 diag = rte_eth_dev_set_rx_queue_stats_mapping(port_id,
1760 rx_queue_stats_mappings[i].queue_id,
1761 rx_queue_stats_mappings[i].stats_counter_id);
1768 port->rx_queue_stats_mapping_enabled = 1;
1773 map_port_queue_stats_mapping_registers(uint8_t pi, struct rte_port *port)
1777 diag = set_tx_queue_stats_mapping_registers(pi, port);
1779 if (diag == -ENOTSUP) {
1780 port->tx_queue_stats_mapping_enabled = 0;
1781 printf("TX queue stats mapping not supported port id=%d\n", pi);
1784 rte_exit(EXIT_FAILURE,
1785 "set_tx_queue_stats_mapping_registers "
1786 "failed for port id=%d diag=%d\n",
1790 diag = set_rx_queue_stats_mapping_registers(pi, port);
1792 if (diag == -ENOTSUP) {
1793 port->rx_queue_stats_mapping_enabled = 0;
1794 printf("RX queue stats mapping not supported port id=%d\n", pi);
1797 rte_exit(EXIT_FAILURE,
1798 "set_rx_queue_stats_mapping_registers "
1799 "failed for port id=%d diag=%d\n",
1805 rxtx_port_config(struct rte_port *port)
1807 port->rx_conf = port->dev_info.default_rxconf;
1808 port->tx_conf = port->dev_info.default_txconf;
1810 /* Check if any RX/TX parameters have been passed */
1811 if (rx_pthresh != RTE_PMD_PARAM_UNSET)
1812 port->rx_conf.rx_thresh.pthresh = rx_pthresh;
1814 if (rx_hthresh != RTE_PMD_PARAM_UNSET)
1815 port->rx_conf.rx_thresh.hthresh = rx_hthresh;
1817 if (rx_wthresh != RTE_PMD_PARAM_UNSET)
1818 port->rx_conf.rx_thresh.wthresh = rx_wthresh;
1820 if (rx_free_thresh != RTE_PMD_PARAM_UNSET)
1821 port->rx_conf.rx_free_thresh = rx_free_thresh;
1823 if (rx_drop_en != RTE_PMD_PARAM_UNSET)
1824 port->rx_conf.rx_drop_en = rx_drop_en;
1826 if (tx_pthresh != RTE_PMD_PARAM_UNSET)
1827 port->tx_conf.tx_thresh.pthresh = tx_pthresh;
1829 if (tx_hthresh != RTE_PMD_PARAM_UNSET)
1830 port->tx_conf.tx_thresh.hthresh = tx_hthresh;
1832 if (tx_wthresh != RTE_PMD_PARAM_UNSET)
1833 port->tx_conf.tx_thresh.wthresh = tx_wthresh;
1835 if (tx_rs_thresh != RTE_PMD_PARAM_UNSET)
1836 port->tx_conf.tx_rs_thresh = tx_rs_thresh;
1838 if (tx_free_thresh != RTE_PMD_PARAM_UNSET)
1839 port->tx_conf.tx_free_thresh = tx_free_thresh;
1841 if (txq_flags != RTE_PMD_PARAM_UNSET)
1842 port->tx_conf.txq_flags = txq_flags;
1846 init_port_config(void)
1849 struct rte_port *port;
1851 FOREACH_PORT(pid, ports) {
1853 port->dev_conf.rxmode = rx_mode;
1854 port->dev_conf.fdir_conf = fdir_conf;
1856 port->dev_conf.rx_adv_conf.rss_conf.rss_key = NULL;
1857 port->dev_conf.rx_adv_conf.rss_conf.rss_hf = rss_hf;
1859 port->dev_conf.rx_adv_conf.rss_conf.rss_key = NULL;
1860 port->dev_conf.rx_adv_conf.rss_conf.rss_hf = 0;
1863 if (port->dcb_flag == 0) {
1864 if( port->dev_conf.rx_adv_conf.rss_conf.rss_hf != 0)
1865 port->dev_conf.rxmode.mq_mode = ETH_MQ_RX_RSS;
1867 port->dev_conf.rxmode.mq_mode = ETH_MQ_RX_NONE;
1870 rxtx_port_config(port);
1872 rte_eth_macaddr_get(pid, &port->eth_addr);
1874 map_port_queue_stats_mapping_registers(pid, port);
1875 #ifdef RTE_NIC_BYPASS
1876 rte_eth_dev_bypass_init(pid);
1881 void set_port_slave_flag(portid_t slave_pid)
1883 struct rte_port *port;
1885 port = &ports[slave_pid];
1886 port->slave_flag = 1;
1889 void clear_port_slave_flag(portid_t slave_pid)
1891 struct rte_port *port;
1893 port = &ports[slave_pid];
1894 port->slave_flag = 0;
1897 uint8_t port_is_bonding_slave(portid_t slave_pid)
1899 struct rte_port *port;
1901 port = &ports[slave_pid];
1902 return port->slave_flag;
1905 const uint16_t vlan_tags[] = {
1906 0, 1, 2, 3, 4, 5, 6, 7,
1907 8, 9, 10, 11, 12, 13, 14, 15,
1908 16, 17, 18, 19, 20, 21, 22, 23,
1909 24, 25, 26, 27, 28, 29, 30, 31
1913 get_eth_dcb_conf(struct rte_eth_conf *eth_conf,
1914 enum dcb_mode_enable dcb_mode,
1915 enum rte_eth_nb_tcs num_tcs,
1921 * Builds up the correct configuration for dcb+vt based on the vlan tags array
1922 * given above, and the number of traffic classes available for use.
1924 if (dcb_mode == DCB_VT_ENABLED) {
1925 struct rte_eth_vmdq_dcb_conf *vmdq_rx_conf =
1926 ð_conf->rx_adv_conf.vmdq_dcb_conf;
1927 struct rte_eth_vmdq_dcb_tx_conf *vmdq_tx_conf =
1928 ð_conf->tx_adv_conf.vmdq_dcb_tx_conf;
1930 /* VMDQ+DCB RX and TX configurations */
1931 vmdq_rx_conf->enable_default_pool = 0;
1932 vmdq_rx_conf->default_pool = 0;
1933 vmdq_rx_conf->nb_queue_pools =
1934 (num_tcs == ETH_4_TCS ? ETH_32_POOLS : ETH_16_POOLS);
1935 vmdq_tx_conf->nb_queue_pools =
1936 (num_tcs == ETH_4_TCS ? ETH_32_POOLS : ETH_16_POOLS);
1938 vmdq_rx_conf->nb_pool_maps = vmdq_rx_conf->nb_queue_pools;
1939 for (i = 0; i < vmdq_rx_conf->nb_pool_maps; i++) {
1940 vmdq_rx_conf->pool_map[i].vlan_id = vlan_tags[i];
1941 vmdq_rx_conf->pool_map[i].pools =
1942 1 << (i % vmdq_rx_conf->nb_queue_pools);
1944 for (i = 0; i < ETH_DCB_NUM_USER_PRIORITIES; i++) {
1945 vmdq_rx_conf->dcb_tc[i] = i;
1946 vmdq_tx_conf->dcb_tc[i] = i;
1949 /* set DCB mode of RX and TX of multiple queues */
1950 eth_conf->rxmode.mq_mode = ETH_MQ_RX_VMDQ_DCB;
1951 eth_conf->txmode.mq_mode = ETH_MQ_TX_VMDQ_DCB;
1953 struct rte_eth_dcb_rx_conf *rx_conf =
1954 ð_conf->rx_adv_conf.dcb_rx_conf;
1955 struct rte_eth_dcb_tx_conf *tx_conf =
1956 ð_conf->tx_adv_conf.dcb_tx_conf;
1958 rx_conf->nb_tcs = num_tcs;
1959 tx_conf->nb_tcs = num_tcs;
1961 for (i = 0; i < ETH_DCB_NUM_USER_PRIORITIES; i++) {
1962 rx_conf->dcb_tc[i] = i % num_tcs;
1963 tx_conf->dcb_tc[i] = i % num_tcs;
1965 eth_conf->rxmode.mq_mode = ETH_MQ_RX_DCB_RSS;
1966 eth_conf->rx_adv_conf.rss_conf.rss_hf = rss_hf;
1967 eth_conf->txmode.mq_mode = ETH_MQ_TX_DCB;
1971 eth_conf->dcb_capability_en =
1972 ETH_DCB_PG_SUPPORT | ETH_DCB_PFC_SUPPORT;
1974 eth_conf->dcb_capability_en = ETH_DCB_PG_SUPPORT;
1980 init_port_dcb_config(portid_t pid,
1981 enum dcb_mode_enable dcb_mode,
1982 enum rte_eth_nb_tcs num_tcs,
1985 struct rte_eth_conf port_conf;
1986 struct rte_port *rte_port;
1990 rte_port = &ports[pid];
1992 memset(&port_conf, 0, sizeof(struct rte_eth_conf));
1993 /* Enter DCB configuration status */
1996 /*set configuration of DCB in vt mode and DCB in non-vt mode*/
1997 retval = get_eth_dcb_conf(&port_conf, dcb_mode, num_tcs, pfc_en);
2000 port_conf.rxmode.hw_vlan_filter = 1;
2003 * Write the configuration into the device.
2004 * Set the numbers of RX & TX queues to 0, so
2005 * the RX & TX queues will not be setup.
2007 (void)rte_eth_dev_configure(pid, 0, 0, &port_conf);
2009 rte_eth_dev_info_get(pid, &rte_port->dev_info);
2011 /* If dev_info.vmdq_pool_base is greater than 0,
2012 * the queue id of vmdq pools is started after pf queues.
2014 if (dcb_mode == DCB_VT_ENABLED &&
2015 rte_port->dev_info.vmdq_pool_base > 0) {
2016 printf("VMDQ_DCB multi-queue mode is nonsensical"
2017 " for port %d.", pid);
2021 /* Assume the ports in testpmd have the same dcb capability
2022 * and has the same number of rxq and txq in dcb mode
2024 if (dcb_mode == DCB_VT_ENABLED) {
2025 if (rte_port->dev_info.max_vfs > 0) {
2026 nb_rxq = rte_port->dev_info.nb_rx_queues;
2027 nb_txq = rte_port->dev_info.nb_tx_queues;
2029 nb_rxq = rte_port->dev_info.max_rx_queues;
2030 nb_txq = rte_port->dev_info.max_tx_queues;
2033 /*if vt is disabled, use all pf queues */
2034 if (rte_port->dev_info.vmdq_pool_base == 0) {
2035 nb_rxq = rte_port->dev_info.max_rx_queues;
2036 nb_txq = rte_port->dev_info.max_tx_queues;
2038 nb_rxq = (queueid_t)num_tcs;
2039 nb_txq = (queueid_t)num_tcs;
2043 rx_free_thresh = 64;
2045 memcpy(&rte_port->dev_conf, &port_conf, sizeof(struct rte_eth_conf));
2047 rxtx_port_config(rte_port);
2049 rte_port->dev_conf.rxmode.hw_vlan_filter = 1;
2050 for (i = 0; i < RTE_DIM(vlan_tags); i++)
2051 rx_vft_set(pid, vlan_tags[i], 1);
2053 rte_eth_macaddr_get(pid, &rte_port->eth_addr);
2054 map_port_queue_stats_mapping_registers(pid, rte_port);
2056 rte_port->dcb_flag = 1;
2066 /* Configuration of Ethernet ports. */
2067 ports = rte_zmalloc("testpmd: ports",
2068 sizeof(struct rte_port) * RTE_MAX_ETHPORTS,
2069 RTE_CACHE_LINE_SIZE);
2070 if (ports == NULL) {
2071 rte_exit(EXIT_FAILURE,
2072 "rte_zmalloc(%d struct rte_port) failed\n",
2076 /* enabled allocated ports */
2077 for (pid = 0; pid < nb_ports; pid++)
2078 ports[pid].enabled = 1;
2089 signal_handler(int signum)
2091 if (signum == SIGINT || signum == SIGTERM) {
2092 printf("\nSignal %d received, preparing to exit...\n",
2094 #ifdef RTE_LIBRTE_PDUMP
2095 /* uninitialize packet capture framework */
2099 /* exit with the expected status */
2100 signal(signum, SIG_DFL);
2101 kill(getpid(), signum);
2106 main(int argc, char** argv)
2111 signal(SIGINT, signal_handler);
2112 signal(SIGTERM, signal_handler);
2114 diag = rte_eal_init(argc, argv);
2116 rte_panic("Cannot init EAL\n");
2118 #ifdef RTE_LIBRTE_PDUMP
2119 /* initialize packet capture framework */
2120 rte_pdump_init(NULL);
2123 nb_ports = (portid_t) rte_eth_dev_count();
2125 RTE_LOG(WARNING, EAL, "No probed ethernet devices\n");
2127 /* allocate port structures, and init them */
2130 set_def_fwd_config();
2132 rte_panic("Empty set of forwarding logical cores - check the "
2133 "core mask supplied in the command parameters\n");
2138 launch_args_parse(argc, argv);
2140 if (!nb_rxq && !nb_txq)
2141 printf("Warning: Either rx or tx queues should be non-zero\n");
2143 if (nb_rxq > 1 && nb_rxq > nb_txq)
2144 printf("Warning: nb_rxq=%d enables RSS configuration, "
2145 "but nb_txq=%d will prevent to fully test it.\n",
2149 if (start_port(RTE_PORT_ALL) != 0)
2150 rte_exit(EXIT_FAILURE, "Start ports failed\n");
2152 /* set all ports to promiscuous mode by default */
2153 FOREACH_PORT(port_id, ports)
2154 rte_eth_promiscuous_enable(port_id);
2156 /* Init metrics library */
2157 rte_metrics_init(rte_socket_id());
2159 /* Setup bitrate stats */
2160 #ifdef RTE_LIBRTE_BITRATE
2161 bitrate_data = rte_stats_bitrate_create();
2162 if (bitrate_data == NULL)
2163 rte_exit(EXIT_FAILURE, "Could not allocate bitrate data.\n");
2164 rte_stats_bitrate_reg(bitrate_data);
2168 #ifdef RTE_LIBRTE_CMDLINE
2169 if (interactive == 1) {
2171 printf("Start automatic packet forwarding\n");
2172 start_packet_forwarding(0);
2181 printf("No commandline core given, start packet forwarding\n");
2182 start_packet_forwarding(0);
2183 printf("Press enter to exit\n");
2184 rc = read(0, &c, 1);