1 /*- 2 * BSD LICENSE 3 * 4 * Copyright(c) 2016-2017 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 <stdio.h> 36 #include <string.h> 37 #include <sys/queue.h> 38 #include <errno.h> 39 #include <stdarg.h> 40 #include <inttypes.h> 41 42 #include <rte_common.h> 43 #include <rte_memory.h> 44 #include <rte_memzone.h> 45 #include <rte_eal.h> 46 #include <rte_byteorder.h> 47 #include <rte_atomic.h> 48 #include <rte_launch.h> 49 #include <rte_per_lcore.h> 50 #include <rte_lcore.h> 51 #include <rte_branch_prediction.h> 52 #include <rte_debug.h> 53 #include <rte_ring.h> 54 #include <rte_log.h> 55 #include <rte_mempool.h> 56 #include <rte_memcpy.h> 57 #include <rte_mbuf.h> 58 #include <rte_interrupts.h> 59 #include <rte_pci.h> 60 #include <rte_ether.h> 61 #include <rte_ethdev.h> 62 #include <rte_malloc.h> 63 #include <rte_string_fns.h> 64 #include <rte_cycles.h> 65 #include <rte_efd.h> 66 #include <rte_hash.h> 67 68 #include "common.h" 69 #include "args.h" 70 #include "init.h" 71 72 #define MBUFS_PER_NODE 1536 73 #define MBUFS_PER_PORT 1536 74 #define MBUF_CACHE_SIZE 512 75 76 #define RTE_MP_RX_DESC_DEFAULT 512 77 #define RTE_MP_TX_DESC_DEFAULT 512 78 #define NODE_QUEUE_RINGSIZE 128 79 80 #define NO_FLAGS 0 81 82 /* The mbuf pool for packet rx */ 83 struct rte_mempool *pktmbuf_pool; 84 85 /* array of info/queues for nodes */ 86 struct node *nodes; 87 88 /* EFD table */ 89 struct rte_efd_table *efd_table; 90 91 /* Shared info between server and nodes */ 92 struct shared_info *info; 93 94 /** 95 * Initialise the mbuf pool for packet reception for the NIC, and any other 96 * buffer pools needed by the app - currently none. 97 */ 98 static int 99 init_mbuf_pools(void) 100 { 101 const unsigned int num_mbufs = (num_nodes * MBUFS_PER_NODE) + 102 (info->num_ports * MBUFS_PER_PORT); 103 104 /* 105 * Don't pass single-producer/single-consumer flags to mbuf create as it 106 * seems faster to use a cache instead 107 */ 108 printf("Creating mbuf pool '%s' [%u mbufs] ...\n", 109 PKTMBUF_POOL_NAME, num_mbufs); 110 pktmbuf_pool = rte_pktmbuf_pool_create(PKTMBUF_POOL_NAME, num_mbufs, 111 MBUF_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id()); 112 113 return pktmbuf_pool == NULL; /* 0 on success */ 114 } 115 116 /** 117 * Initialise an individual port: 118 * - configure number of rx and tx rings 119 * - set up each rx ring, to pull from the main mbuf pool 120 * - set up each tx ring 121 * - start the port and report its status to stdout 122 */ 123 static int 124 init_port(uint8_t port_num) 125 { 126 /* for port configuration all features are off by default */ 127 const struct rte_eth_conf port_conf = { 128 .rxmode = { 129 .mq_mode = ETH_MQ_RX_RSS 130 } 131 }; 132 const uint16_t rx_rings = 1, tx_rings = num_nodes; 133 uint16_t rx_ring_size = RTE_MP_RX_DESC_DEFAULT; 134 uint16_t tx_ring_size = RTE_MP_TX_DESC_DEFAULT; 135 136 uint16_t q; 137 int retval; 138 139 printf("Port %u init ... ", (unsigned int)port_num); 140 fflush(stdout); 141 142 /* 143 * Standard DPDK port initialisation - config port, then set up 144 * rx and tx rings. 145 */ 146 retval = rte_eth_dev_configure(port_num, rx_rings, tx_rings, &port_conf); 147 if (retval != 0) 148 return retval; 149 150 retval = rte_eth_dev_adjust_nb_rx_tx_desc(port_num, &rx_ring_size, 151 &tx_ring_size); 152 if (retval != 0) 153 return retval; 154 155 for (q = 0; q < rx_rings; q++) { 156 retval = rte_eth_rx_queue_setup(port_num, q, rx_ring_size, 157 rte_eth_dev_socket_id(port_num), 158 NULL, pktmbuf_pool); 159 if (retval < 0) 160 return retval; 161 } 162 163 for (q = 0; q < tx_rings; q++) { 164 retval = rte_eth_tx_queue_setup(port_num, q, tx_ring_size, 165 rte_eth_dev_socket_id(port_num), 166 NULL); 167 if (retval < 0) 168 return retval; 169 } 170 171 rte_eth_promiscuous_enable(port_num); 172 173 retval = rte_eth_dev_start(port_num); 174 if (retval < 0) 175 return retval; 176 177 printf("done:\n"); 178 179 return 0; 180 } 181 182 /** 183 * Set up the DPDK rings which will be used to pass packets, via 184 * pointers, between the multi-process server and node processes. 185 * Each node needs one RX queue. 186 */ 187 static int 188 init_shm_rings(void) 189 { 190 unsigned int i; 191 unsigned int socket_id; 192 const char *q_name; 193 const unsigned int ringsize = NODE_QUEUE_RINGSIZE; 194 195 nodes = rte_malloc("node details", 196 sizeof(*nodes) * num_nodes, 0); 197 if (nodes == NULL) 198 rte_exit(EXIT_FAILURE, "Cannot allocate memory for " 199 "node program details\n"); 200 201 for (i = 0; i < num_nodes; i++) { 202 /* Create an RX queue for each node */ 203 socket_id = rte_socket_id(); 204 q_name = get_rx_queue_name(i); 205 nodes[i].rx_q = rte_ring_create(q_name, 206 ringsize, socket_id, 207 RING_F_SP_ENQ | RING_F_SC_DEQ); 208 if (nodes[i].rx_q == NULL) 209 rte_exit(EXIT_FAILURE, "Cannot create rx ring queue " 210 "for node %u\n", i); 211 } 212 return 0; 213 } 214 215 /* 216 * Create EFD table which will contain all the flows 217 * that will be distributed among the nodes 218 */ 219 static void 220 create_efd_table(void) 221 { 222 uint8_t socket_id = rte_socket_id(); 223 224 /* create table */ 225 efd_table = rte_efd_create("flow table", num_flows * 2, sizeof(uint32_t), 226 1 << socket_id, socket_id); 227 228 if (efd_table == NULL) 229 rte_exit(EXIT_FAILURE, "Problem creating the flow table\n"); 230 } 231 232 static void 233 populate_efd_table(void) 234 { 235 unsigned int i; 236 int32_t ret; 237 uint32_t ip_dst; 238 uint8_t socket_id = rte_socket_id(); 239 uint64_t node_id; 240 241 /* Add flows in table */ 242 for (i = 0; i < num_flows; i++) { 243 node_id = i % num_nodes; 244 245 ip_dst = rte_cpu_to_be_32(i); 246 ret = rte_efd_update(efd_table, socket_id, 247 (void *)&ip_dst, (efd_value_t)node_id); 248 if (ret < 0) 249 rte_exit(EXIT_FAILURE, "Unable to add entry %u in " 250 "EFD table\n", i); 251 } 252 253 printf("EFD table: Adding 0x%x keys\n", num_flows); 254 } 255 256 /* Check the link status of all ports in up to 9s, and print them finally */ 257 static void 258 check_all_ports_link_status(uint8_t port_num, uint32_t port_mask) 259 { 260 #define CHECK_INTERVAL 100 /* 100ms */ 261 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */ 262 uint8_t portid, count, all_ports_up, print_flag = 0; 263 struct rte_eth_link link; 264 265 printf("\nChecking link status"); 266 fflush(stdout); 267 for (count = 0; count <= MAX_CHECK_TIME; count++) { 268 all_ports_up = 1; 269 for (portid = 0; portid < port_num; portid++) { 270 if ((port_mask & (1 << info->id[portid])) == 0) 271 continue; 272 memset(&link, 0, sizeof(link)); 273 rte_eth_link_get_nowait(info->id[portid], &link); 274 /* print link status if flag set */ 275 if (print_flag == 1) { 276 if (link.link_status) 277 printf("Port %d Link Up - speed %u " 278 "Mbps - %s\n", info->id[portid], 279 (unsigned int)link.link_speed, 280 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ? 281 ("full-duplex") : ("half-duplex\n")); 282 else 283 printf("Port %d Link Down\n", 284 (uint8_t)info->id[portid]); 285 continue; 286 } 287 /* clear all_ports_up flag if any link down */ 288 if (link.link_status == ETH_LINK_DOWN) { 289 all_ports_up = 0; 290 break; 291 } 292 } 293 /* after finally printing all link status, get out */ 294 if (print_flag == 1) 295 break; 296 297 if (all_ports_up == 0) { 298 printf("."); 299 fflush(stdout); 300 rte_delay_ms(CHECK_INTERVAL); 301 } 302 303 /* set the print_flag if all ports up or timeout */ 304 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) { 305 print_flag = 1; 306 printf("done\n"); 307 } 308 } 309 } 310 311 /** 312 * Main init function for the multi-process server app, 313 * calls subfunctions to do each stage of the initialisation. 314 */ 315 int 316 init(int argc, char *argv[]) 317 { 318 int retval; 319 const struct rte_memzone *mz; 320 uint8_t i, total_ports; 321 322 /* init EAL, parsing EAL args */ 323 retval = rte_eal_init(argc, argv); 324 if (retval < 0) 325 return -1; 326 argc -= retval; 327 argv += retval; 328 329 /* get total number of ports */ 330 total_ports = rte_eth_dev_count(); 331 332 /* set up array for port data */ 333 mz = rte_memzone_reserve(MZ_SHARED_INFO, sizeof(*info), 334 rte_socket_id(), NO_FLAGS); 335 if (mz == NULL) 336 rte_exit(EXIT_FAILURE, "Cannot reserve memory zone " 337 "for port information\n"); 338 memset(mz->addr, 0, sizeof(*info)); 339 info = mz->addr; 340 341 /* parse additional, application arguments */ 342 retval = parse_app_args(total_ports, argc, argv); 343 if (retval != 0) 344 return -1; 345 346 /* initialise mbuf pools */ 347 retval = init_mbuf_pools(); 348 if (retval != 0) 349 rte_exit(EXIT_FAILURE, "Cannot create needed mbuf pools\n"); 350 351 /* now initialise the ports we will use */ 352 for (i = 0; i < info->num_ports; i++) { 353 retval = init_port(info->id[i]); 354 if (retval != 0) 355 rte_exit(EXIT_FAILURE, "Cannot initialise port %u\n", 356 (unsigned int) i); 357 } 358 359 check_all_ports_link_status(info->num_ports, (~0x0)); 360 361 /* initialise the node queues/rings for inter-eu comms */ 362 init_shm_rings(); 363 364 /* Create the EFD table */ 365 create_efd_table(); 366 367 /* Populate the EFD table */ 368 populate_efd_table(); 369 370 /* Share the total number of nodes */ 371 info->num_nodes = num_nodes; 372 373 /* Share the total number of flows */ 374 info->num_flows = num_flows; 375 return 0; 376 } 377