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_SIZE (1600 + sizeof(struct rte_mbuf) + 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, 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(uint8_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(uint8_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 int retval; 105 uint16_t q; 106 107 if (port >= rte_eth_dev_count()) 108 return -1; 109 110 retval = rte_eth_dev_configure(port, rx_rings, tx_rings, &port_conf); 111 if (retval != 0) 112 return retval; 113 114 for (q = 0; q < rx_rings; q++) { 115 retval = rte_eth_rx_queue_setup(port, q, RX_RING_SIZE, 116 rte_eth_dev_socket_id(port), NULL, mbuf_pool); 117 if (retval < 0) 118 return retval; 119 } 120 121 for (q = 0; q < tx_rings; q++) { 122 retval = rte_eth_tx_queue_setup(port, q, TX_RING_SIZE, 123 rte_eth_dev_socket_id(port), NULL); 124 if (retval < 0) 125 return retval; 126 } 127 128 retval = rte_eth_dev_start(port); 129 if (retval < 0) 130 return retval; 131 132 struct ether_addr addr; 133 134 rte_eth_macaddr_get(port, &addr); 135 printf("Port %u MAC: %02"PRIx8" %02"PRIx8" %02"PRIx8 136 " %02"PRIx8" %02"PRIx8" %02"PRIx8"\n", 137 (unsigned)port, 138 addr.addr_bytes[0], addr.addr_bytes[1], 139 addr.addr_bytes[2], addr.addr_bytes[3], 140 addr.addr_bytes[4], addr.addr_bytes[5]); 141 142 rte_eth_promiscuous_enable(port); 143 rte_eth_add_rx_callback(port, 0, add_timestamps, NULL); 144 rte_eth_add_tx_callback(port, 0, calc_latency, NULL); 145 146 return 0; 147 } 148 149 /* 150 * Main thread that does the work, reading from INPUT_PORT 151 * and writing to OUTPUT_PORT 152 */ 153 static __attribute__((noreturn)) void 154 lcore_main(void) 155 { 156 uint8_t port; 157 158 for (port = 0; port < nb_ports; port++) 159 if (rte_eth_dev_socket_id(port) > 0 && 160 rte_eth_dev_socket_id(port) != 161 (int)rte_socket_id()) 162 printf("WARNING, port %u is on remote NUMA node to " 163 "polling thread.\n\tPerformance will " 164 "not be optimal.\n", port); 165 166 printf("\nCore %u forwarding packets. [Ctrl+C to quit]\n", 167 rte_lcore_id()); 168 for (;;) { 169 for (port = 0; port < nb_ports; port++) { 170 struct rte_mbuf *bufs[BURST_SIZE]; 171 const uint16_t nb_rx = rte_eth_rx_burst(port, 0, 172 bufs, BURST_SIZE); 173 if (unlikely(nb_rx == 0)) 174 continue; 175 const uint16_t nb_tx = rte_eth_tx_burst(port ^ 1, 0, 176 bufs, nb_rx); 177 if (unlikely(nb_tx < nb_rx)) { 178 uint16_t buf; 179 180 for (buf = nb_tx; buf < nb_rx; buf++) 181 rte_pktmbuf_free(bufs[buf]); 182 } 183 } 184 } 185 } 186 187 /* Main function, does initialisation and calls the per-lcore functions */ 188 int 189 main(int argc, char *argv[]) 190 { 191 struct rte_mempool *mbuf_pool; 192 uint8_t portid; 193 194 /* init EAL */ 195 int ret = rte_eal_init(argc, argv); 196 197 if (ret < 0) 198 rte_exit(EXIT_FAILURE, "Error with EAL initialization\n"); 199 argc -= ret; 200 argv += ret; 201 202 nb_ports = rte_eth_dev_count(); 203 if (nb_ports < 2 || (nb_ports & 1)) 204 rte_exit(EXIT_FAILURE, "Error: number of ports must be even\n"); 205 206 mbuf_pool = rte_mempool_create("MBUF_POOL", NUM_MBUFS * nb_ports, 207 MBUF_SIZE, MBUF_CACHE_SIZE, 208 sizeof(struct rte_pktmbuf_pool_private), 209 rte_pktmbuf_pool_init, NULL, 210 rte_pktmbuf_init, NULL, 211 rte_socket_id(), 0); 212 if (mbuf_pool == NULL) 213 rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n"); 214 215 /* initialize all ports */ 216 for (portid = 0; portid < nb_ports; portid++) 217 if (port_init(portid, mbuf_pool) != 0) 218 rte_exit(EXIT_FAILURE, "Cannot init port %"PRIu8"\n", 219 portid); 220 221 if (rte_lcore_count() > 1) 222 printf("\nWARNING: Too much enabled lcores - " 223 "App uses only 1 lcore\n"); 224 225 /* call lcore_main on master core only */ 226 lcore_main(); 227 return 0; 228 } 229