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_CACHE_SIZE 250 47 #define BURST_SIZE 32 48 49 static const struct rte_eth_conf port_conf_default = { 50 .rxmode = { .max_rx_pkt_len = ETHER_MAX_LEN, }, 51 }; 52 53 static unsigned nb_ports; 54 55 static struct { 56 uint64_t total_cycles; 57 uint64_t total_pkts; 58 } latency_numbers; 59 60 61 static uint16_t 62 add_timestamps(uint16_t port __rte_unused, uint16_t qidx __rte_unused, 63 struct rte_mbuf **pkts, uint16_t nb_pkts, 64 uint16_t max_pkts __rte_unused, void *_ __rte_unused) 65 { 66 unsigned i; 67 uint64_t now = rte_rdtsc(); 68 69 for (i = 0; i < nb_pkts; i++) 70 pkts[i]->udata64 = now; 71 return nb_pkts; 72 } 73 74 static uint16_t 75 calc_latency(uint16_t port __rte_unused, uint16_t qidx __rte_unused, 76 struct rte_mbuf **pkts, uint16_t nb_pkts, void *_ __rte_unused) 77 { 78 uint64_t cycles = 0; 79 uint64_t now = rte_rdtsc(); 80 unsigned i; 81 82 for (i = 0; i < nb_pkts; i++) 83 cycles += now - pkts[i]->udata64; 84 latency_numbers.total_cycles += cycles; 85 latency_numbers.total_pkts += nb_pkts; 86 87 if (latency_numbers.total_pkts > (100 * 1000 * 1000ULL)) { 88 printf("Latency = %"PRIu64" cycles\n", 89 latency_numbers.total_cycles / latency_numbers.total_pkts); 90 latency_numbers.total_cycles = latency_numbers.total_pkts = 0; 91 } 92 return nb_pkts; 93 } 94 95 /* 96 * Initialises a given port using global settings and with the rx buffers 97 * coming from the mbuf_pool passed as parameter 98 */ 99 static inline int 100 port_init(uint16_t port, struct rte_mempool *mbuf_pool) 101 { 102 struct rte_eth_conf port_conf = port_conf_default; 103 const uint16_t rx_rings = 1, tx_rings = 1; 104 uint16_t nb_rxd = RX_RING_SIZE; 105 uint16_t nb_txd = TX_RING_SIZE; 106 int retval; 107 uint16_t q; 108 109 if (port >= rte_eth_dev_count()) 110 return -1; 111 112 retval = rte_eth_dev_configure(port, rx_rings, tx_rings, &port_conf); 113 if (retval != 0) 114 return retval; 115 116 retval = rte_eth_dev_adjust_nb_rx_tx_desc(port, &nb_rxd, &nb_txd); 117 if (retval != 0) 118 return retval; 119 120 for (q = 0; q < rx_rings; q++) { 121 retval = rte_eth_rx_queue_setup(port, q, nb_rxd, 122 rte_eth_dev_socket_id(port), NULL, mbuf_pool); 123 if (retval < 0) 124 return retval; 125 } 126 127 for (q = 0; q < tx_rings; q++) { 128 retval = rte_eth_tx_queue_setup(port, q, nb_txd, 129 rte_eth_dev_socket_id(port), NULL); 130 if (retval < 0) 131 return retval; 132 } 133 134 retval = rte_eth_dev_start(port); 135 if (retval < 0) 136 return retval; 137 138 struct ether_addr addr; 139 140 rte_eth_macaddr_get(port, &addr); 141 printf("Port %u MAC: %02"PRIx8" %02"PRIx8" %02"PRIx8 142 " %02"PRIx8" %02"PRIx8" %02"PRIx8"\n", 143 (unsigned)port, 144 addr.addr_bytes[0], addr.addr_bytes[1], 145 addr.addr_bytes[2], addr.addr_bytes[3], 146 addr.addr_bytes[4], addr.addr_bytes[5]); 147 148 rte_eth_promiscuous_enable(port); 149 rte_eth_add_rx_callback(port, 0, add_timestamps, NULL); 150 rte_eth_add_tx_callback(port, 0, calc_latency, NULL); 151 152 return 0; 153 } 154 155 /* 156 * Main thread that does the work, reading from INPUT_PORT 157 * and writing to OUTPUT_PORT 158 */ 159 static __attribute__((noreturn)) void 160 lcore_main(void) 161 { 162 uint16_t port; 163 164 for (port = 0; port < nb_ports; port++) 165 if (rte_eth_dev_socket_id(port) > 0 && 166 rte_eth_dev_socket_id(port) != 167 (int)rte_socket_id()) 168 printf("WARNING, port %u is on remote NUMA node to " 169 "polling thread.\n\tPerformance will " 170 "not be optimal.\n", port); 171 172 printf("\nCore %u forwarding packets. [Ctrl+C to quit]\n", 173 rte_lcore_id()); 174 for (;;) { 175 for (port = 0; port < nb_ports; port++) { 176 struct rte_mbuf *bufs[BURST_SIZE]; 177 const uint16_t nb_rx = rte_eth_rx_burst(port, 0, 178 bufs, BURST_SIZE); 179 if (unlikely(nb_rx == 0)) 180 continue; 181 const uint16_t nb_tx = rte_eth_tx_burst(port ^ 1, 0, 182 bufs, nb_rx); 183 if (unlikely(nb_tx < nb_rx)) { 184 uint16_t buf; 185 186 for (buf = nb_tx; buf < nb_rx; buf++) 187 rte_pktmbuf_free(bufs[buf]); 188 } 189 } 190 } 191 } 192 193 /* Main function, does initialisation and calls the per-lcore functions */ 194 int 195 main(int argc, char *argv[]) 196 { 197 struct rte_mempool *mbuf_pool; 198 uint16_t portid; 199 200 /* init EAL */ 201 int ret = rte_eal_init(argc, argv); 202 203 if (ret < 0) 204 rte_exit(EXIT_FAILURE, "Error with EAL initialization\n"); 205 argc -= ret; 206 argv += ret; 207 208 nb_ports = rte_eth_dev_count(); 209 if (nb_ports < 2 || (nb_ports & 1)) 210 rte_exit(EXIT_FAILURE, "Error: number of ports must be even\n"); 211 212 mbuf_pool = rte_pktmbuf_pool_create("MBUF_POOL", 213 NUM_MBUFS * nb_ports, MBUF_CACHE_SIZE, 0, 214 RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id()); 215 if (mbuf_pool == NULL) 216 rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n"); 217 218 /* initialize all ports */ 219 for (portid = 0; portid < nb_ports; portid++) 220 if (port_init(portid, mbuf_pool) != 0) 221 rte_exit(EXIT_FAILURE, "Cannot init port %"PRIu8"\n", 222 portid); 223 224 if (rte_lcore_count() > 1) 225 printf("\nWARNING: Too much enabled lcores - " 226 "App uses only 1 lcore\n"); 227 228 /* call lcore_main on master core only */ 229 lcore_main(); 230 return 0; 231 } 232