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