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