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