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 <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_fbk_hash.h> 64 #include <rte_string_fns.h> 65 #include <rte_cycles.h> 66 67 #include "common.h" 68 #include "args.h" 69 #include "init.h" 70 71 #define MBUFS_PER_CLIENT 1536 72 #define MBUFS_PER_PORT 1536 73 #define MBUF_CACHE_SIZE 512 74 75 #define RTE_MP_RX_DESC_DEFAULT 512 76 #define RTE_MP_TX_DESC_DEFAULT 512 77 #define CLIENT_QUEUE_RINGSIZE 128 78 79 #define NO_FLAGS 0 80 81 /* The mbuf pool for packet rx */ 82 struct rte_mempool *pktmbuf_pool; 83 84 /* array of info/queues for clients */ 85 struct client *clients = NULL; 86 87 /* the port details */ 88 struct port_info *ports; 89 90 /** 91 * Initialise the mbuf pool for packet reception for the NIC, and any other 92 * buffer pools needed by the app - currently none. 93 */ 94 static int 95 init_mbuf_pools(void) 96 { 97 const unsigned num_mbufs = (num_clients * MBUFS_PER_CLIENT) \ 98 + (ports->num_ports * MBUFS_PER_PORT); 99 100 /* don't pass single-producer/single-consumer flags to mbuf create as it 101 * seems faster to use a cache instead */ 102 printf("Creating mbuf pool '%s' [%u mbufs] ...\n", 103 PKTMBUF_POOL_NAME, num_mbufs); 104 pktmbuf_pool = rte_pktmbuf_pool_create(PKTMBUF_POOL_NAME, num_mbufs, 105 MBUF_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id()); 106 107 return pktmbuf_pool == NULL; /* 0 on success */ 108 } 109 110 /** 111 * Initialise an individual port: 112 * - configure number of rx and tx rings 113 * - set up each rx ring, to pull from the main mbuf pool 114 * - set up each tx ring 115 * - start the port and report its status to stdout 116 */ 117 static int 118 init_port(uint8_t port_num) 119 { 120 /* for port configuration all features are off by default */ 121 const struct rte_eth_conf port_conf = { 122 .rxmode = { 123 .mq_mode = ETH_MQ_RX_RSS 124 } 125 }; 126 const uint16_t rx_rings = 1, tx_rings = num_clients; 127 const uint16_t rx_ring_size = RTE_MP_RX_DESC_DEFAULT; 128 const uint16_t tx_ring_size = RTE_MP_TX_DESC_DEFAULT; 129 130 uint16_t q; 131 int retval; 132 133 printf("Port %u init ... ", (unsigned)port_num); 134 fflush(stdout); 135 136 /* Standard DPDK port initialisation - config port, then set up 137 * rx and tx rings */ 138 if ((retval = rte_eth_dev_configure(port_num, rx_rings, tx_rings, 139 &port_conf)) != 0) 140 return retval; 141 142 for (q = 0; q < rx_rings; q++) { 143 retval = rte_eth_rx_queue_setup(port_num, q, rx_ring_size, 144 rte_eth_dev_socket_id(port_num), 145 NULL, pktmbuf_pool); 146 if (retval < 0) return retval; 147 } 148 149 for ( q = 0; q < tx_rings; q ++ ) { 150 retval = rte_eth_tx_queue_setup(port_num, q, tx_ring_size, 151 rte_eth_dev_socket_id(port_num), 152 NULL); 153 if (retval < 0) return retval; 154 } 155 156 rte_eth_promiscuous_enable(port_num); 157 158 retval = rte_eth_dev_start(port_num); 159 if (retval < 0) return retval; 160 161 printf( "done: \n"); 162 163 return 0; 164 } 165 166 /** 167 * Set up the DPDK rings which will be used to pass packets, via 168 * pointers, between the multi-process server and client processes. 169 * Each client needs one RX queue. 170 */ 171 static int 172 init_shm_rings(void) 173 { 174 unsigned i; 175 unsigned socket_id; 176 const char * q_name; 177 const unsigned ringsize = CLIENT_QUEUE_RINGSIZE; 178 179 clients = rte_malloc("client details", 180 sizeof(*clients) * num_clients, 0); 181 if (clients == NULL) 182 rte_exit(EXIT_FAILURE, "Cannot allocate memory for client program details\n"); 183 184 for (i = 0; i < num_clients; i++) { 185 /* Create an RX queue for each client */ 186 socket_id = rte_socket_id(); 187 q_name = get_rx_queue_name(i); 188 clients[i].rx_q = rte_ring_create(q_name, 189 ringsize, socket_id, 190 RING_F_SP_ENQ | RING_F_SC_DEQ ); /* single prod, single cons */ 191 if (clients[i].rx_q == NULL) 192 rte_exit(EXIT_FAILURE, "Cannot create rx ring queue for client %u\n", i); 193 } 194 return 0; 195 } 196 197 /* Check the link status of all ports in up to 9s, and print them finally */ 198 static void 199 check_all_ports_link_status(uint8_t port_num, uint32_t port_mask) 200 { 201 #define CHECK_INTERVAL 100 /* 100ms */ 202 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */ 203 uint8_t portid, count, all_ports_up, print_flag = 0; 204 struct rte_eth_link link; 205 206 printf("\nChecking link status"); 207 fflush(stdout); 208 for (count = 0; count <= MAX_CHECK_TIME; count++) { 209 all_ports_up = 1; 210 for (portid = 0; portid < port_num; portid++) { 211 if ((port_mask & (1 << ports->id[portid])) == 0) 212 continue; 213 memset(&link, 0, sizeof(link)); 214 rte_eth_link_get_nowait(ports->id[portid], &link); 215 /* print link status if flag set */ 216 if (print_flag == 1) { 217 if (link.link_status) 218 printf("Port %d Link Up - speed %u " 219 "Mbps - %s\n", ports->id[portid], 220 (unsigned)link.link_speed, 221 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ? 222 ("full-duplex") : ("half-duplex\n")); 223 else 224 printf("Port %d Link Down\n", 225 (uint8_t)ports->id[portid]); 226 continue; 227 } 228 /* clear all_ports_up flag if any link down */ 229 if (link.link_status == ETH_LINK_DOWN) { 230 all_ports_up = 0; 231 break; 232 } 233 } 234 /* after finally printing all link status, get out */ 235 if (print_flag == 1) 236 break; 237 238 if (all_ports_up == 0) { 239 printf("."); 240 fflush(stdout); 241 rte_delay_ms(CHECK_INTERVAL); 242 } 243 244 /* set the print_flag if all ports up or timeout */ 245 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) { 246 print_flag = 1; 247 printf("done\n"); 248 } 249 } 250 } 251 252 /** 253 * Main init function for the multi-process server app, 254 * calls subfunctions to do each stage of the initialisation. 255 */ 256 int 257 init(int argc, char *argv[]) 258 { 259 int retval; 260 const struct rte_memzone *mz; 261 uint8_t i, total_ports; 262 263 /* init EAL, parsing EAL args */ 264 retval = rte_eal_init(argc, argv); 265 if (retval < 0) 266 return -1; 267 argc -= retval; 268 argv += retval; 269 270 /* get total number of ports */ 271 total_ports = rte_eth_dev_count(); 272 273 /* set up array for port data */ 274 mz = rte_memzone_reserve(MZ_PORT_INFO, sizeof(*ports), 275 rte_socket_id(), NO_FLAGS); 276 if (mz == NULL) 277 rte_exit(EXIT_FAILURE, "Cannot reserve memory zone for port information\n"); 278 memset(mz->addr, 0, sizeof(*ports)); 279 ports = mz->addr; 280 281 /* parse additional, application arguments */ 282 retval = parse_app_args(total_ports, argc, argv); 283 if (retval != 0) 284 return -1; 285 286 /* initialise mbuf pools */ 287 retval = init_mbuf_pools(); 288 if (retval != 0) 289 rte_exit(EXIT_FAILURE, "Cannot create needed mbuf pools\n"); 290 291 /* now initialise the ports we will use */ 292 for (i = 0; i < ports->num_ports; i++) { 293 retval = init_port(ports->id[i]); 294 if (retval != 0) 295 rte_exit(EXIT_FAILURE, "Cannot initialise port %u\n", 296 (unsigned)i); 297 } 298 299 check_all_ports_link_status(ports->num_ports, (~0x0)); 300 301 /* initialise the client queues/rings for inter-eu comms */ 302 init_shm_rings(); 303 304 return 0; 305 } 306