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