1*d30ea906Sjfb8856606 /* SPDX-License-Identifier: BSD-3-Clause 2*d30ea906Sjfb8856606 * Copyright(c) 2010-2015 Intel Corporation 3a9643ea8Slogwang */ 4a9643ea8Slogwang 5a9643ea8Slogwang #include <stdint.h> 6a9643ea8Slogwang #include <inttypes.h> 7a9643ea8Slogwang #include <rte_eal.h> 8a9643ea8Slogwang #include <rte_ethdev.h> 9a9643ea8Slogwang #include <rte_cycles.h> 10a9643ea8Slogwang #include <rte_lcore.h> 11a9643ea8Slogwang #include <rte_mbuf.h> 12a9643ea8Slogwang 13*d30ea906Sjfb8856606 #define RX_RING_SIZE 1024 14*d30ea906Sjfb8856606 #define TX_RING_SIZE 1024 15a9643ea8Slogwang 16a9643ea8Slogwang #define NUM_MBUFS 8191 17a9643ea8Slogwang #define MBUF_CACHE_SIZE 250 18a9643ea8Slogwang #define BURST_SIZE 32 19a9643ea8Slogwang 20a9643ea8Slogwang static const struct rte_eth_conf port_conf_default = { 21*d30ea906Sjfb8856606 .rxmode = { 22*d30ea906Sjfb8856606 .max_rx_pkt_len = ETHER_MAX_LEN, 23*d30ea906Sjfb8856606 }, 24a9643ea8Slogwang }; 25a9643ea8Slogwang 26a9643ea8Slogwang /* basicfwd.c: Basic DPDK skeleton forwarding example. */ 27a9643ea8Slogwang 28a9643ea8Slogwang /* 29a9643ea8Slogwang * Initializes a given port using global settings and with the RX buffers 30a9643ea8Slogwang * coming from the mbuf_pool passed as a parameter. 31a9643ea8Slogwang */ 32a9643ea8Slogwang static inline int 332bfe3f2eSlogwang port_init(uint16_t port, struct rte_mempool *mbuf_pool) 34a9643ea8Slogwang { 35a9643ea8Slogwang struct rte_eth_conf port_conf = port_conf_default; 36a9643ea8Slogwang const uint16_t rx_rings = 1, tx_rings = 1; 372bfe3f2eSlogwang uint16_t nb_rxd = RX_RING_SIZE; 382bfe3f2eSlogwang uint16_t nb_txd = TX_RING_SIZE; 39a9643ea8Slogwang int retval; 40a9643ea8Slogwang uint16_t q; 41*d30ea906Sjfb8856606 struct rte_eth_dev_info dev_info; 42*d30ea906Sjfb8856606 struct rte_eth_txconf txconf; 43a9643ea8Slogwang 44*d30ea906Sjfb8856606 if (!rte_eth_dev_is_valid_port(port)) 45a9643ea8Slogwang return -1; 46a9643ea8Slogwang 47*d30ea906Sjfb8856606 rte_eth_dev_info_get(port, &dev_info); 48*d30ea906Sjfb8856606 if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE) 49*d30ea906Sjfb8856606 port_conf.txmode.offloads |= 50*d30ea906Sjfb8856606 DEV_TX_OFFLOAD_MBUF_FAST_FREE; 51*d30ea906Sjfb8856606 52a9643ea8Slogwang /* Configure the Ethernet device. */ 53a9643ea8Slogwang retval = rte_eth_dev_configure(port, rx_rings, tx_rings, &port_conf); 54a9643ea8Slogwang if (retval != 0) 55a9643ea8Slogwang return retval; 56a9643ea8Slogwang 572bfe3f2eSlogwang retval = rte_eth_dev_adjust_nb_rx_tx_desc(port, &nb_rxd, &nb_txd); 582bfe3f2eSlogwang if (retval != 0) 592bfe3f2eSlogwang return retval; 602bfe3f2eSlogwang 61a9643ea8Slogwang /* Allocate and set up 1 RX queue per Ethernet port. */ 62a9643ea8Slogwang for (q = 0; q < rx_rings; q++) { 632bfe3f2eSlogwang retval = rte_eth_rx_queue_setup(port, q, nb_rxd, 64a9643ea8Slogwang rte_eth_dev_socket_id(port), NULL, mbuf_pool); 65a9643ea8Slogwang if (retval < 0) 66a9643ea8Slogwang return retval; 67a9643ea8Slogwang } 68a9643ea8Slogwang 69*d30ea906Sjfb8856606 txconf = dev_info.default_txconf; 70*d30ea906Sjfb8856606 txconf.offloads = port_conf.txmode.offloads; 71a9643ea8Slogwang /* Allocate and set up 1 TX queue per Ethernet port. */ 72a9643ea8Slogwang for (q = 0; q < tx_rings; q++) { 732bfe3f2eSlogwang retval = rte_eth_tx_queue_setup(port, q, nb_txd, 74*d30ea906Sjfb8856606 rte_eth_dev_socket_id(port), &txconf); 75a9643ea8Slogwang if (retval < 0) 76a9643ea8Slogwang return retval; 77a9643ea8Slogwang } 78a9643ea8Slogwang 79a9643ea8Slogwang /* Start the Ethernet port. */ 80a9643ea8Slogwang retval = rte_eth_dev_start(port); 81a9643ea8Slogwang if (retval < 0) 82a9643ea8Slogwang return retval; 83a9643ea8Slogwang 84a9643ea8Slogwang /* Display the port MAC address. */ 85a9643ea8Slogwang struct ether_addr addr; 86a9643ea8Slogwang rte_eth_macaddr_get(port, &addr); 87a9643ea8Slogwang printf("Port %u MAC: %02" PRIx8 " %02" PRIx8 " %02" PRIx8 88a9643ea8Slogwang " %02" PRIx8 " %02" PRIx8 " %02" PRIx8 "\n", 892bfe3f2eSlogwang port, 90a9643ea8Slogwang addr.addr_bytes[0], addr.addr_bytes[1], 91a9643ea8Slogwang addr.addr_bytes[2], addr.addr_bytes[3], 92a9643ea8Slogwang addr.addr_bytes[4], addr.addr_bytes[5]); 93a9643ea8Slogwang 94a9643ea8Slogwang /* Enable RX in promiscuous mode for the Ethernet device. */ 95a9643ea8Slogwang rte_eth_promiscuous_enable(port); 96a9643ea8Slogwang 97a9643ea8Slogwang return 0; 98a9643ea8Slogwang } 99a9643ea8Slogwang 100a9643ea8Slogwang /* 101a9643ea8Slogwang * The lcore main. This is the main thread that does the work, reading from 102a9643ea8Slogwang * an input port and writing to an output port. 103a9643ea8Slogwang */ 104a9643ea8Slogwang static __attribute__((noreturn)) void 105a9643ea8Slogwang lcore_main(void) 106a9643ea8Slogwang { 1072bfe3f2eSlogwang uint16_t port; 108a9643ea8Slogwang 109a9643ea8Slogwang /* 110a9643ea8Slogwang * Check that the port is on the same NUMA node as the polling thread 111a9643ea8Slogwang * for best performance. 112a9643ea8Slogwang */ 113*d30ea906Sjfb8856606 RTE_ETH_FOREACH_DEV(port) 114a9643ea8Slogwang if (rte_eth_dev_socket_id(port) > 0 && 115a9643ea8Slogwang rte_eth_dev_socket_id(port) != 116a9643ea8Slogwang (int)rte_socket_id()) 117a9643ea8Slogwang printf("WARNING, port %u is on remote NUMA node to " 118a9643ea8Slogwang "polling thread.\n\tPerformance will " 119a9643ea8Slogwang "not be optimal.\n", port); 120a9643ea8Slogwang 121a9643ea8Slogwang printf("\nCore %u forwarding packets. [Ctrl+C to quit]\n", 122a9643ea8Slogwang rte_lcore_id()); 123a9643ea8Slogwang 124a9643ea8Slogwang /* Run until the application is quit or killed. */ 125a9643ea8Slogwang for (;;) { 126a9643ea8Slogwang /* 127a9643ea8Slogwang * Receive packets on a port and forward them on the paired 128a9643ea8Slogwang * port. The mapping is 0 -> 1, 1 -> 0, 2 -> 3, 3 -> 2, etc. 129a9643ea8Slogwang */ 130*d30ea906Sjfb8856606 RTE_ETH_FOREACH_DEV(port) { 131a9643ea8Slogwang 132a9643ea8Slogwang /* Get burst of RX packets, from first port of pair. */ 133a9643ea8Slogwang struct rte_mbuf *bufs[BURST_SIZE]; 134a9643ea8Slogwang const uint16_t nb_rx = rte_eth_rx_burst(port, 0, 135a9643ea8Slogwang bufs, BURST_SIZE); 136a9643ea8Slogwang 137a9643ea8Slogwang if (unlikely(nb_rx == 0)) 138a9643ea8Slogwang continue; 139a9643ea8Slogwang 140a9643ea8Slogwang /* Send burst of TX packets, to second port of pair. */ 141a9643ea8Slogwang const uint16_t nb_tx = rte_eth_tx_burst(port ^ 1, 0, 142a9643ea8Slogwang bufs, nb_rx); 143a9643ea8Slogwang 144a9643ea8Slogwang /* Free any unsent packets. */ 145a9643ea8Slogwang if (unlikely(nb_tx < nb_rx)) { 146a9643ea8Slogwang uint16_t buf; 147a9643ea8Slogwang for (buf = nb_tx; buf < nb_rx; buf++) 148a9643ea8Slogwang rte_pktmbuf_free(bufs[buf]); 149a9643ea8Slogwang } 150a9643ea8Slogwang } 151a9643ea8Slogwang } 152a9643ea8Slogwang } 153a9643ea8Slogwang 154a9643ea8Slogwang /* 155a9643ea8Slogwang * The main function, which does initialization and calls the per-lcore 156a9643ea8Slogwang * functions. 157a9643ea8Slogwang */ 158a9643ea8Slogwang int 159a9643ea8Slogwang main(int argc, char *argv[]) 160a9643ea8Slogwang { 161a9643ea8Slogwang struct rte_mempool *mbuf_pool; 162a9643ea8Slogwang unsigned nb_ports; 1632bfe3f2eSlogwang uint16_t portid; 164a9643ea8Slogwang 165a9643ea8Slogwang /* Initialize the Environment Abstraction Layer (EAL). */ 166a9643ea8Slogwang int ret = rte_eal_init(argc, argv); 167a9643ea8Slogwang if (ret < 0) 168a9643ea8Slogwang rte_exit(EXIT_FAILURE, "Error with EAL initialization\n"); 169a9643ea8Slogwang 170a9643ea8Slogwang argc -= ret; 171a9643ea8Slogwang argv += ret; 172a9643ea8Slogwang 173a9643ea8Slogwang /* Check that there is an even number of ports to send/receive on. */ 174*d30ea906Sjfb8856606 nb_ports = rte_eth_dev_count_avail(); 175a9643ea8Slogwang if (nb_ports < 2 || (nb_ports & 1)) 176a9643ea8Slogwang rte_exit(EXIT_FAILURE, "Error: number of ports must be even\n"); 177a9643ea8Slogwang 178a9643ea8Slogwang /* Creates a new mempool in memory to hold the mbufs. */ 179a9643ea8Slogwang mbuf_pool = rte_pktmbuf_pool_create("MBUF_POOL", NUM_MBUFS * nb_ports, 180a9643ea8Slogwang MBUF_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id()); 181a9643ea8Slogwang 182a9643ea8Slogwang if (mbuf_pool == NULL) 183a9643ea8Slogwang rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n"); 184a9643ea8Slogwang 185a9643ea8Slogwang /* Initialize all ports. */ 186*d30ea906Sjfb8856606 RTE_ETH_FOREACH_DEV(portid) 187a9643ea8Slogwang if (port_init(portid, mbuf_pool) != 0) 1882bfe3f2eSlogwang rte_exit(EXIT_FAILURE, "Cannot init port %"PRIu16 "\n", 189a9643ea8Slogwang portid); 190a9643ea8Slogwang 191a9643ea8Slogwang if (rte_lcore_count() > 1) 192a9643ea8Slogwang printf("\nWARNING: Too many lcores enabled. Only 1 used.\n"); 193a9643ea8Slogwang 194a9643ea8Slogwang /* Call lcore_main on the master core only. */ 195a9643ea8Slogwang lcore_main(); 196a9643ea8Slogwang 197a9643ea8Slogwang return 0; 198a9643ea8Slogwang } 199