1 /*- 2 * BSD LICENSE 3 * 4 * Copyright(c) 2010-2014 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 <stdio.h> 35 #include <stdlib.h> 36 #include <stdint.h> 37 #include <inttypes.h> 38 #include <sys/types.h> 39 #include <string.h> 40 #include <sys/queue.h> 41 #include <stdarg.h> 42 #include <errno.h> 43 #include <getopt.h> 44 45 #include <rte_common.h> 46 #include <rte_byteorder.h> 47 #include <rte_log.h> 48 #include <rte_memory.h> 49 #include <rte_memcpy.h> 50 #include <rte_memzone.h> 51 #include <rte_eal.h> 52 #include <rte_per_lcore.h> 53 #include <rte_launch.h> 54 #include <rte_atomic.h> 55 #include <rte_cycles.h> 56 #include <rte_prefetch.h> 57 #include <rte_lcore.h> 58 #include <rte_per_lcore.h> 59 #include <rte_branch_prediction.h> 60 #include <rte_interrupts.h> 61 #include <rte_pci.h> 62 #include <rte_random.h> 63 #include <rte_debug.h> 64 #include <rte_ether.h> 65 #include <rte_ethdev.h> 66 #include <rte_ring.h> 67 #include <rte_mempool.h> 68 #include <rte_mbuf.h> 69 #include <rte_string_fns.h> 70 #include <rte_ip.h> 71 #include <rte_tcp.h> 72 #include <rte_lpm.h> 73 #include <rte_lpm6.h> 74 75 #include "main.h" 76 77 struct app_params app = { 78 /* Ports*/ 79 .n_ports = APP_MAX_PORTS, 80 .port_rx_ring_size = 128, 81 .port_tx_ring_size = 512, 82 83 /* Rings */ 84 .ring_rx_size = 128, 85 .ring_tx_size = 128, 86 87 /* Buffer pool */ 88 .pool_buffer_size = 2048 + sizeof(struct rte_mbuf) + 89 RTE_PKTMBUF_HEADROOM, 90 .pool_size = 32 * 1024, 91 .pool_cache_size = 256, 92 93 /* Burst sizes */ 94 .burst_size_rx_read = 64, 95 .burst_size_rx_write = 64, 96 .burst_size_worker_read = 64, 97 .burst_size_worker_write = 64, 98 .burst_size_tx_read = 64, 99 .burst_size_tx_write = 64, 100 }; 101 102 static struct rte_eth_conf port_conf = { 103 .rxmode = { 104 .split_hdr_size = 0, 105 .header_split = 0, /* Header Split disabled */ 106 .hw_ip_checksum = 1, /* IP checksum offload enabled */ 107 .hw_vlan_filter = 0, /* VLAN filtering disabled */ 108 .jumbo_frame = 0, /* Jumbo Frame Support disabled */ 109 .hw_strip_crc = 0, /* CRC stripped by hardware */ 110 }, 111 .rx_adv_conf = { 112 .rss_conf = { 113 .rss_key = NULL, 114 .rss_hf = ETH_RSS_IP, 115 }, 116 }, 117 .txmode = { 118 .mq_mode = ETH_MQ_TX_NONE, 119 }, 120 }; 121 122 static struct rte_eth_rxconf rx_conf = { 123 .rx_thresh = { 124 .pthresh = 8, 125 .hthresh = 8, 126 .wthresh = 4, 127 }, 128 .rx_free_thresh = 64, 129 .rx_drop_en = 0, 130 }; 131 132 static struct rte_eth_txconf tx_conf = { 133 .tx_thresh = { 134 .pthresh = 36, 135 .hthresh = 0, 136 .wthresh = 0, 137 }, 138 .tx_free_thresh = 0, 139 .tx_rs_thresh = 0, 140 }; 141 142 static void 143 app_init_mbuf_pools(void) 144 { 145 /* Init the buffer pool */ 146 RTE_LOG(INFO, USER1, "Creating the mbuf pool ...\n"); 147 app.pool = rte_mempool_create( 148 "mempool", 149 app.pool_size, 150 app.pool_buffer_size, 151 app.pool_cache_size, 152 sizeof(struct rte_pktmbuf_pool_private), 153 rte_pktmbuf_pool_init, NULL, 154 rte_pktmbuf_init, NULL, 155 rte_socket_id(), 156 0); 157 if (app.pool == NULL) 158 rte_panic("Cannot create mbuf pool\n"); 159 } 160 161 static void 162 app_init_rings(void) 163 { 164 uint32_t i; 165 166 for (i = 0; i < app.n_ports; i++) { 167 char name[32]; 168 169 snprintf(name, sizeof(name), "app_ring_rx_%u", i); 170 171 app.rings_rx[i] = rte_ring_create( 172 name, 173 app.ring_rx_size, 174 rte_socket_id(), 175 RING_F_SP_ENQ | RING_F_SC_DEQ); 176 177 if (app.rings_rx[i] == NULL) 178 rte_panic("Cannot create RX ring %u\n", i); 179 } 180 181 for (i = 0; i < app.n_ports; i++) { 182 char name[32]; 183 184 snprintf(name, sizeof(name), "app_ring_tx_%u", i); 185 186 app.rings_tx[i] = rte_ring_create( 187 name, 188 app.ring_tx_size, 189 rte_socket_id(), 190 RING_F_SP_ENQ | RING_F_SC_DEQ); 191 192 if (app.rings_tx[i] == NULL) 193 rte_panic("Cannot create TX ring %u\n", i); 194 } 195 196 } 197 198 static void 199 app_ports_check_link(void) 200 { 201 uint32_t all_ports_up, i; 202 203 all_ports_up = 1; 204 205 for (i = 0; i < app.n_ports; i++) { 206 struct rte_eth_link link; 207 uint8_t port; 208 209 port = (uint8_t) app.ports[i]; 210 memset(&link, 0, sizeof(link)); 211 rte_eth_link_get_nowait(port, &link); 212 RTE_LOG(INFO, USER1, "Port %u (%u Gbps) %s\n", 213 port, 214 link.link_speed / 1000, 215 link.link_status ? "UP" : "DOWN"); 216 217 if (link.link_status == 0) 218 all_ports_up = 0; 219 } 220 221 if (all_ports_up == 0) 222 rte_panic("Some NIC ports are DOWN\n"); 223 } 224 225 static void 226 app_init_ports(void) 227 { 228 uint32_t i; 229 230 /* Init NIC ports, then start the ports */ 231 for (i = 0; i < app.n_ports; i++) { 232 uint8_t port; 233 int ret; 234 235 port = (uint8_t) app.ports[i]; 236 RTE_LOG(INFO, USER1, "Initializing NIC port %u ...\n", port); 237 238 /* Init port */ 239 ret = rte_eth_dev_configure( 240 port, 241 1, 242 1, 243 &port_conf); 244 if (ret < 0) 245 rte_panic("Cannot init NIC port %u (%d)\n", port, ret); 246 247 rte_eth_promiscuous_enable(port); 248 249 /* Init RX queues */ 250 ret = rte_eth_rx_queue_setup( 251 port, 252 0, 253 app.port_rx_ring_size, 254 rte_eth_dev_socket_id(port), 255 &rx_conf, 256 app.pool); 257 if (ret < 0) 258 rte_panic("Cannot init RX for port %u (%d)\n", 259 (uint32_t) port, ret); 260 261 /* Init TX queues */ 262 ret = rte_eth_tx_queue_setup( 263 port, 264 0, 265 app.port_tx_ring_size, 266 rte_eth_dev_socket_id(port), 267 &tx_conf); 268 if (ret < 0) 269 rte_panic("Cannot init TX for port %u (%d)\n", 270 (uint32_t) port, ret); 271 272 /* Start port */ 273 ret = rte_eth_dev_start(port); 274 if (ret < 0) 275 rte_panic("Cannot start port %u (%d)\n", port, ret); 276 } 277 278 app_ports_check_link(); 279 } 280 281 void 282 app_init(void) 283 { 284 app_init_mbuf_pools(); 285 app_init_rings(); 286 app_init_ports(); 287 288 RTE_LOG(INFO, USER1, "Initialization completed\n"); 289 } 290