1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2010-2014 Intel Corporation 3 */ 4 5 #include <stdio.h> 6 #include <stdlib.h> 7 #include <stdint.h> 8 #include <inttypes.h> 9 #include <sys/types.h> 10 #include <string.h> 11 #include <sys/queue.h> 12 #include <stdarg.h> 13 #include <errno.h> 14 #include <getopt.h> 15 16 #include <rte_common.h> 17 #include <rte_byteorder.h> 18 #include <rte_log.h> 19 #include <rte_memory.h> 20 #include <rte_memcpy.h> 21 #include <rte_eal.h> 22 #include <rte_per_lcore.h> 23 #include <rte_launch.h> 24 #include <rte_cycles.h> 25 #include <rte_prefetch.h> 26 #include <rte_lcore.h> 27 #include <rte_branch_prediction.h> 28 #include <rte_interrupts.h> 29 #include <rte_pci.h> 30 #include <rte_random.h> 31 #include <rte_debug.h> 32 #include <rte_ether.h> 33 #include <rte_ethdev.h> 34 #include <rte_ring.h> 35 #include <rte_mempool.h> 36 #include <rte_mbuf.h> 37 #include <rte_string_fns.h> 38 #include <rte_ip.h> 39 #include <rte_tcp.h> 40 #include <rte_lpm.h> 41 #include <rte_lpm6.h> 42 43 #include "main.h" 44 45 struct app_params app = { 46 /* Ports*/ 47 .n_ports = APP_MAX_PORTS, 48 .port_rx_ring_size = 128, 49 .port_tx_ring_size = 512, 50 51 /* Rings */ 52 .ring_rx_size = 128, 53 .ring_tx_size = 128, 54 55 /* Buffer pool */ 56 .pool_buffer_size = 2048 + RTE_PKTMBUF_HEADROOM, 57 .pool_size = 32 * 1024, 58 .pool_cache_size = 256, 59 60 /* Burst sizes */ 61 .burst_size_rx_read = 64, 62 .burst_size_rx_write = 64, 63 .burst_size_worker_read = 64, 64 .burst_size_worker_write = 64, 65 .burst_size_tx_read = 64, 66 .burst_size_tx_write = 64, 67 }; 68 69 static struct rte_eth_conf port_conf = { 70 .rxmode = { 71 .split_hdr_size = 0, 72 .offloads = RTE_ETH_RX_OFFLOAD_CHECKSUM, 73 }, 74 .rx_adv_conf = { 75 .rss_conf = { 76 .rss_key = NULL, 77 .rss_hf = RTE_ETH_RSS_IP, 78 }, 79 }, 80 .txmode = { 81 .mq_mode = RTE_ETH_MQ_TX_NONE, 82 }, 83 }; 84 85 static struct rte_eth_rxconf rx_conf = { 86 .rx_thresh = { 87 .pthresh = 8, 88 .hthresh = 8, 89 .wthresh = 4, 90 }, 91 .rx_free_thresh = 64, 92 .rx_drop_en = 0, 93 }; 94 95 static struct rte_eth_txconf tx_conf = { 96 .tx_thresh = { 97 .pthresh = 36, 98 .hthresh = 0, 99 .wthresh = 0, 100 }, 101 .tx_free_thresh = 0, 102 .tx_rs_thresh = 0, 103 }; 104 105 static void 106 app_init_mbuf_pools(void) 107 { 108 /* Init the buffer pool */ 109 RTE_LOG(INFO, USER1, "Creating the mbuf pool ...\n"); 110 app.pool = rte_pktmbuf_pool_create("mempool", app.pool_size, 111 app.pool_cache_size, 0, app.pool_buffer_size, rte_socket_id()); 112 if (app.pool == NULL) 113 rte_panic("Cannot create mbuf pool\n"); 114 } 115 116 static void 117 app_init_rings(void) 118 { 119 uint32_t i; 120 121 for (i = 0; i < app.n_ports; i++) { 122 char name[32]; 123 124 snprintf(name, sizeof(name), "app_ring_rx_%u", i); 125 126 app.rings_rx[i] = rte_ring_create( 127 name, 128 app.ring_rx_size, 129 rte_socket_id(), 130 RING_F_SP_ENQ | RING_F_SC_DEQ); 131 132 if (app.rings_rx[i] == NULL) 133 rte_panic("Cannot create RX ring %u\n", i); 134 } 135 136 for (i = 0; i < app.n_ports; i++) { 137 char name[32]; 138 139 snprintf(name, sizeof(name), "app_ring_tx_%u", i); 140 141 app.rings_tx[i] = rte_ring_create( 142 name, 143 app.ring_tx_size, 144 rte_socket_id(), 145 RING_F_SP_ENQ | RING_F_SC_DEQ); 146 147 if (app.rings_tx[i] == NULL) 148 rte_panic("Cannot create TX ring %u\n", i); 149 } 150 151 } 152 153 static void 154 app_ports_check_link(void) 155 { 156 uint32_t all_ports_up, i; 157 char link_status_text[RTE_ETH_LINK_MAX_STR_LEN]; 158 all_ports_up = 1; 159 160 for (i = 0; i < app.n_ports; i++) { 161 struct rte_eth_link link; 162 uint16_t port; 163 int ret; 164 165 port = app.ports[i]; 166 memset(&link, 0, sizeof(link)); 167 ret = rte_eth_link_get_nowait(port, &link); 168 if (ret < 0) { 169 RTE_LOG(INFO, USER1, 170 "Failed to get port %u link status: %s\n", 171 port, rte_strerror(-ret)); 172 all_ports_up = 0; 173 continue; 174 } 175 rte_eth_link_to_str(link_status_text, sizeof(link_status_text), 176 &link); 177 RTE_LOG(INFO, USER1, "Port %u %s\n", 178 port, 179 link_status_text); 180 if (link.link_status == RTE_ETH_LINK_DOWN) 181 all_ports_up = 0; 182 } 183 184 if (all_ports_up == 0) 185 rte_panic("Some NIC ports are DOWN\n"); 186 } 187 188 static void 189 app_init_ports(void) 190 { 191 uint32_t i; 192 193 /* Init NIC ports, then start the ports */ 194 for (i = 0; i < app.n_ports; i++) { 195 uint16_t port; 196 int ret; 197 198 port = app.ports[i]; 199 RTE_LOG(INFO, USER1, "Initializing NIC port %u ...\n", port); 200 201 /* Init port */ 202 ret = rte_eth_dev_configure( 203 port, 204 1, 205 1, 206 &port_conf); 207 if (ret < 0) 208 rte_panic("Cannot init NIC port %u (%d)\n", port, ret); 209 210 ret = rte_eth_promiscuous_enable(port); 211 if (ret != 0) 212 rte_panic("Cannot enable promiscuous mode for port %u: %s\n", 213 port, rte_strerror(-ret)); 214 215 /* Init RX queues */ 216 ret = rte_eth_rx_queue_setup( 217 port, 218 0, 219 app.port_rx_ring_size, 220 rte_eth_dev_socket_id(port), 221 &rx_conf, 222 app.pool); 223 if (ret < 0) 224 rte_panic("Cannot init RX for port %u (%d)\n", 225 (uint32_t) port, ret); 226 227 /* Init TX queues */ 228 ret = rte_eth_tx_queue_setup( 229 port, 230 0, 231 app.port_tx_ring_size, 232 rte_eth_dev_socket_id(port), 233 &tx_conf); 234 if (ret < 0) 235 rte_panic("Cannot init TX for port %u (%d)\n", 236 (uint32_t) port, ret); 237 238 /* Start port */ 239 ret = rte_eth_dev_start(port); 240 if (ret < 0) 241 rte_panic("Cannot start port %u (%d)\n", port, ret); 242 } 243 244 app_ports_check_link(); 245 } 246 247 void 248 app_init(void) 249 { 250 app_init_mbuf_pools(); 251 app_init_rings(); 252 app_init_ports(); 253 254 RTE_LOG(INFO, USER1, "Initialization completed\n"); 255 } 256