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