1 /*- 2 * BSD LICENSE 3 * 4 * Copyright(c) 2010-2015 Intel Corporation. All rights reserved. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * * Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * * Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the 16 * distribution. 17 * * Neither the name of Intel Corporation nor the names of its 18 * contributors may be used to endorse or promote products derived 19 * from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 #include <stdint.h> 35 #include <inttypes.h> 36 #include <rte_eal.h> 37 #include <rte_ethdev.h> 38 #include <rte_cycles.h> 39 #include <rte_lcore.h> 40 #include <rte_mbuf.h> 41 42 #define RX_RING_SIZE 128 43 #define TX_RING_SIZE 512 44 45 #define NUM_MBUFS 8191 46 #define MBUF_DATA_SIZE (1600 + RTE_PKTMBUF_HEADROOM) 47 #define MBUF_CACHE_SIZE 250 48 #define BURST_SIZE 32 49 50 static const struct rte_eth_conf port_conf_default = { 51 .rxmode = { .max_rx_pkt_len = ETHER_MAX_LEN } 52 }; 53 54 /* basicfwd.c: Basic DPDK skeleton forwarding example. */ 55 56 /* 57 * Initializes a given port using global settings and with the RX buffers 58 * coming from the mbuf_pool passed as a parameter. 59 */ 60 static inline int 61 port_init(uint8_t port, struct rte_mempool *mbuf_pool) 62 { 63 struct rte_eth_conf port_conf = port_conf_default; 64 const uint16_t rx_rings = 1, tx_rings = 1; 65 int retval; 66 uint16_t q; 67 68 if (port >= rte_eth_dev_count()) 69 return -1; 70 71 /* Configure the Ethernet device. */ 72 retval = rte_eth_dev_configure(port, rx_rings, tx_rings, &port_conf); 73 if (retval != 0) 74 return retval; 75 76 /* Allocate and set up 1 RX queue per Ethernet port. */ 77 for (q = 0; q < rx_rings; q++) { 78 retval = rte_eth_rx_queue_setup(port, q, RX_RING_SIZE, 79 rte_eth_dev_socket_id(port), NULL, mbuf_pool); 80 if (retval < 0) 81 return retval; 82 } 83 84 /* Allocate and set up 1 TX queue per Ethernet port. */ 85 for (q = 0; q < tx_rings; q++) { 86 retval = rte_eth_tx_queue_setup(port, q, TX_RING_SIZE, 87 rte_eth_dev_socket_id(port), NULL); 88 if (retval < 0) 89 return retval; 90 } 91 92 /* Start the Ethernet port. */ 93 retval = rte_eth_dev_start(port); 94 if (retval < 0) 95 return retval; 96 97 /* Display the port MAC address. */ 98 struct ether_addr addr; 99 rte_eth_macaddr_get(port, &addr); 100 printf("Port %u MAC: %02" PRIx8 " %02" PRIx8 " %02" PRIx8 101 " %02" PRIx8 " %02" PRIx8 " %02" PRIx8 "\n", 102 (unsigned)port, 103 addr.addr_bytes[0], addr.addr_bytes[1], 104 addr.addr_bytes[2], addr.addr_bytes[3], 105 addr.addr_bytes[4], addr.addr_bytes[5]); 106 107 /* Enable RX in promiscuous mode for the Ethernet device. */ 108 rte_eth_promiscuous_enable(port); 109 110 return 0; 111 } 112 113 /* 114 * The lcore main. This is the main thread that does the work, reading from 115 * an input port and writing to an output port. 116 */ 117 static __attribute__((noreturn)) void 118 lcore_main(void) 119 { 120 const uint8_t nb_ports = rte_eth_dev_count(); 121 uint8_t port; 122 123 /* 124 * Check that the port is on the same NUMA node as the polling thread 125 * for best performance. 126 */ 127 for (port = 0; port < nb_ports; port++) 128 if (rte_eth_dev_socket_id(port) > 0 && 129 rte_eth_dev_socket_id(port) != 130 (int)rte_socket_id()) 131 printf("WARNING, port %u is on remote NUMA node to " 132 "polling thread.\n\tPerformance will " 133 "not be optimal.\n", port); 134 135 printf("\nCore %u forwarding packets. [Ctrl+C to quit]\n", 136 rte_lcore_id()); 137 138 /* Run until the application is quit or killed. */ 139 for (;;) { 140 /* 141 * Receive packets on a port and forward them on the paired 142 * port. The mapping is 0 -> 1, 1 -> 0, 2 -> 3, 3 -> 2, etc. 143 */ 144 for (port = 0; port < nb_ports; port++) { 145 146 /* Get burst of RX packets, from first port of pair. */ 147 struct rte_mbuf *bufs[BURST_SIZE]; 148 const uint16_t nb_rx = rte_eth_rx_burst(port, 0, 149 bufs, BURST_SIZE); 150 151 if (unlikely(nb_rx == 0)) 152 continue; 153 154 /* Send burst of TX packets, to second port of pair. */ 155 const uint16_t nb_tx = rte_eth_tx_burst(port ^ 1, 0, 156 bufs, nb_rx); 157 158 /* Free any unsent packets. */ 159 if (unlikely(nb_tx < nb_rx)) { 160 uint16_t buf; 161 for (buf = nb_tx; buf < nb_rx; buf++) 162 rte_pktmbuf_free(bufs[buf]); 163 } 164 } 165 } 166 } 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 uint8_t portid; 178 179 /* Initialize the Environment Abstraction Layer (EAL). */ 180 int ret = rte_eal_init(argc, argv); 181 if (ret < 0) 182 rte_exit(EXIT_FAILURE, "Error with EAL initialization\n"); 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(); 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 mbuf_pool = rte_pktmbuf_pool_create("MBUF_POOL", NUM_MBUFS * nb_ports, 194 MBUF_CACHE_SIZE, 0, MBUF_DATA_SIZE, rte_socket_id()); 195 196 if (mbuf_pool == NULL) 197 rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n"); 198 199 /* Initialize all ports. */ 200 for (portid = 0; portid < nb_ports; portid++) 201 if (port_init(portid, mbuf_pool) != 0) 202 rte_exit(EXIT_FAILURE, "Cannot init port %"PRIu8 "\n", 203 portid); 204 205 if (rte_lcore_count() > 1) 206 printf("\nWARNING: Too many lcores enabled. Only 1 used.\n"); 207 208 /* Call lcore_main on the master core only. */ 209 lcore_main(); 210 211 return 0; 212 } 213