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