1 /*- 2 * BSD LICENSE 3 * 4 * Copyright (c) Intel Corporation. 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 "spdk/stdinc.h" 35 36 #include "env_internal.h" 37 38 #include "spdk/version.h" 39 #include "spdk/env_dpdk.h" 40 41 #include <rte_config.h> 42 #include <rte_eal.h> 43 44 #define SPDK_ENV_DPDK_DEFAULT_NAME "spdk" 45 #define SPDK_ENV_DPDK_DEFAULT_SHM_ID -1 46 #define SPDK_ENV_DPDK_DEFAULT_MEM_SIZE -1 47 #define SPDK_ENV_DPDK_DEFAULT_MASTER_CORE -1 48 #define SPDK_ENV_DPDK_DEFAULT_MEM_CHANNEL -1 49 #define SPDK_ENV_DPDK_DEFAULT_CORE_MASK "0x1" 50 51 static char **g_eal_cmdline; 52 static int g_eal_cmdline_argcount; 53 static bool g_external_init = true; 54 55 static char * 56 _sprintf_alloc(const char *format, ...) 57 { 58 va_list args; 59 va_list args_copy; 60 char *buf; 61 size_t bufsize; 62 int rc; 63 64 va_start(args, format); 65 66 /* Try with a small buffer first. */ 67 bufsize = 32; 68 69 /* Limit maximum buffer size to something reasonable so we don't loop forever. */ 70 while (bufsize <= 1024 * 1024) { 71 buf = malloc(bufsize); 72 if (buf == NULL) { 73 va_end(args); 74 return NULL; 75 } 76 77 va_copy(args_copy, args); 78 rc = vsnprintf(buf, bufsize, format, args_copy); 79 va_end(args_copy); 80 81 /* 82 * If vsnprintf() returned a count within our current buffer size, we are done. 83 * The count does not include the \0 terminator, so rc == bufsize is not OK. 84 */ 85 if (rc >= 0 && (size_t)rc < bufsize) { 86 va_end(args); 87 return buf; 88 } 89 90 /* 91 * vsnprintf() should return the required space, but some libc versions do not 92 * implement this correctly, so just double the buffer size and try again. 93 * 94 * We don't need the data in buf, so rather than realloc(), use free() and malloc() 95 * again to avoid a copy. 96 */ 97 free(buf); 98 bufsize *= 2; 99 } 100 101 va_end(args); 102 return NULL; 103 } 104 105 static void 106 spdk_env_unlink_shared_files(void) 107 { 108 /* Starting with DPDK 18.05, there are more files with unpredictable paths 109 * and filenames. The --no-shconf option prevents from creating them, but 110 * only for DPDK 18.08+. For DPDK 18.05 we just leave them be. 111 */ 112 #if RTE_VERSION < RTE_VERSION_NUM(18, 05, 0, 0) 113 char buffer[PATH_MAX]; 114 115 snprintf(buffer, PATH_MAX, "/var/run/.spdk_pid%d_hugepage_info", getpid()); 116 if (unlink(buffer)) { 117 fprintf(stderr, "Unable to unlink shared memory file: %s. Error code: %d\n", buffer, errno); 118 } 119 #endif 120 } 121 122 void 123 spdk_env_opts_init(struct spdk_env_opts *opts) 124 { 125 if (!opts) { 126 return; 127 } 128 129 memset(opts, 0, sizeof(*opts)); 130 131 opts->name = SPDK_ENV_DPDK_DEFAULT_NAME; 132 opts->core_mask = SPDK_ENV_DPDK_DEFAULT_CORE_MASK; 133 opts->shm_id = SPDK_ENV_DPDK_DEFAULT_SHM_ID; 134 opts->mem_size = SPDK_ENV_DPDK_DEFAULT_MEM_SIZE; 135 opts->master_core = SPDK_ENV_DPDK_DEFAULT_MASTER_CORE; 136 opts->mem_channel = SPDK_ENV_DPDK_DEFAULT_MEM_CHANNEL; 137 } 138 139 static void 140 spdk_free_args(char **args, int argcount) 141 { 142 int i; 143 144 for (i = 0; i < argcount; i++) { 145 free(args[i]); 146 } 147 148 if (argcount) { 149 free(args); 150 } 151 } 152 153 static char ** 154 spdk_push_arg(char *args[], int *argcount, char *arg) 155 { 156 char **tmp; 157 158 if (arg == NULL) { 159 fprintf(stderr, "%s: NULL arg supplied\n", __func__); 160 spdk_free_args(args, *argcount); 161 return NULL; 162 } 163 164 tmp = realloc(args, sizeof(char *) * (*argcount + 1)); 165 if (tmp == NULL) { 166 free(arg); 167 spdk_free_args(args, *argcount); 168 return NULL; 169 } 170 171 tmp[*argcount] = arg; 172 (*argcount)++; 173 174 return tmp; 175 } 176 177 static int 178 spdk_build_eal_cmdline(const struct spdk_env_opts *opts) 179 { 180 int argcount = 0; 181 char **args; 182 183 args = NULL; 184 185 /* set the program name */ 186 args = spdk_push_arg(args, &argcount, _sprintf_alloc("%s", opts->name)); 187 if (args == NULL) { 188 return -1; 189 } 190 191 /* disable shared configuration files when in single process mode. This allows for cleaner shutdown */ 192 if (opts->shm_id < 0) { 193 args = spdk_push_arg(args, &argcount, _sprintf_alloc("%s", "--no-shconf")); 194 if (args == NULL) { 195 return -1; 196 } 197 } 198 199 /* set the coremask */ 200 /* NOTE: If coremask starts with '[' and ends with ']' it is a core list 201 */ 202 if (opts->core_mask[0] == '[') { 203 char *l_arg = _sprintf_alloc("-l %s", opts->core_mask + 1); 204 int len = strlen(l_arg); 205 if (l_arg[len - 1] == ']') { 206 l_arg[len - 1] = '\0'; 207 } 208 args = spdk_push_arg(args, &argcount, l_arg); 209 } else { 210 args = spdk_push_arg(args, &argcount, _sprintf_alloc("-c %s", opts->core_mask)); 211 } 212 213 if (args == NULL) { 214 return -1; 215 } 216 217 /* set the memory channel number */ 218 if (opts->mem_channel > 0) { 219 args = spdk_push_arg(args, &argcount, _sprintf_alloc("-n %d", opts->mem_channel)); 220 if (args == NULL) { 221 return -1; 222 } 223 } 224 225 /* set the memory size */ 226 if (opts->mem_size >= 0) { 227 args = spdk_push_arg(args, &argcount, _sprintf_alloc("-m %d", opts->mem_size)); 228 if (args == NULL) { 229 return -1; 230 } 231 } 232 233 /* set the master core */ 234 if (opts->master_core > 0) { 235 args = spdk_push_arg(args, &argcount, _sprintf_alloc("--master-lcore=%d", 236 opts->master_core)); 237 if (args == NULL) { 238 return -1; 239 } 240 } 241 242 /* set no pci if enabled */ 243 if (opts->no_pci) { 244 args = spdk_push_arg(args, &argcount, _sprintf_alloc("--no-pci")); 245 if (args == NULL) { 246 return -1; 247 } 248 } 249 250 /* create just one hugetlbfs file */ 251 if (opts->hugepage_single_segments) { 252 args = spdk_push_arg(args, &argcount, _sprintf_alloc("--single-file-segments")); 253 if (args == NULL) { 254 return -1; 255 } 256 } 257 258 /* unlink hugepages after initialization */ 259 if (opts->unlink_hugepage) { 260 args = spdk_push_arg(args, &argcount, _sprintf_alloc("--huge-unlink")); 261 if (args == NULL) { 262 return -1; 263 } 264 } 265 266 /* use a specific hugetlbfs mount */ 267 if (opts->hugedir) { 268 args = spdk_push_arg(args, &argcount, _sprintf_alloc("--huge-dir=%s", opts->hugedir)); 269 if (args == NULL) { 270 return -1; 271 } 272 } 273 274 #if RTE_VERSION >= RTE_VERSION_NUM(18, 05, 0, 0) && RTE_VERSION < RTE_VERSION_NUM(18, 5, 1, 0) 275 /* Dynamic memory management is buggy in DPDK 18.05.0. Don't use it. */ 276 if (!opts->env_context || strcmp(opts->env_context, "--legacy-mem") != 0) { 277 args = spdk_push_arg(args, &argcount, _sprintf_alloc("--legacy-mem")); 278 if (args == NULL) { 279 return -1; 280 } 281 } 282 #endif 283 284 if (opts->num_pci_addr) { 285 size_t i; 286 char bdf[32]; 287 struct spdk_pci_addr *pci_addr = 288 opts->pci_blacklist ? opts->pci_blacklist : opts->pci_whitelist; 289 290 for (i = 0; i < opts->num_pci_addr; i++) { 291 spdk_pci_addr_fmt(bdf, 32, &pci_addr[i]); 292 args = spdk_push_arg(args, &argcount, _sprintf_alloc("%s=%s", 293 (opts->pci_blacklist ? "--pci-blacklist" : "--pci-whitelist"), 294 bdf)); 295 if (args == NULL) { 296 return -1; 297 } 298 } 299 } 300 301 /* Lower default EAL loglevel to RTE_LOG_NOTICE - normal, but significant messages. 302 * This can be overridden by specifying the same option in opts->env_context 303 */ 304 args = spdk_push_arg(args, &argcount, strdup("--log-level=lib.eal:6")); 305 if (args == NULL) { 306 return -1; 307 } 308 309 if (opts->env_context) { 310 args = spdk_push_arg(args, &argcount, strdup(opts->env_context)); 311 if (args == NULL) { 312 return -1; 313 } 314 } 315 316 #ifdef __linux__ 317 /* Set the base virtual address - it must be an address that is not in the 318 * ASAN shadow region, otherwise ASAN-enabled builds will ignore the 319 * mmap hint. 320 * 321 * Ref: https://github.com/google/sanitizers/wiki/AddressSanitizerAlgorithm 322 */ 323 args = spdk_push_arg(args, &argcount, _sprintf_alloc("--base-virtaddr=0x200000000000")); 324 if (args == NULL) { 325 return -1; 326 } 327 328 /* --match-allocation prevents DPDK from merging or splitting system memory allocations under the hood. 329 * This is critical for RDMA when attempting to use an rte_mempool based buffer pool. If DPDK merges two 330 * physically or IOVA contiguous memory regions, then when we go to allocate a buffer pool, it can split 331 * the memory for a buffer over two allocations meaning the buffer will be split over a memory region. 332 */ 333 #if RTE_VERSION >= RTE_VERSION_NUM(19, 02, 0, 0) 334 if (!opts->env_context || strcmp(opts->env_context, "--legacy-mem") != 0) { 335 args = spdk_push_arg(args, &argcount, _sprintf_alloc("%s", "--match-allocations")); 336 if (args == NULL) { 337 return -1; 338 } 339 } 340 #endif 341 342 if (opts->shm_id < 0) { 343 args = spdk_push_arg(args, &argcount, _sprintf_alloc("--file-prefix=spdk_pid%d", 344 getpid())); 345 if (args == NULL) { 346 return -1; 347 } 348 } else { 349 args = spdk_push_arg(args, &argcount, _sprintf_alloc("--file-prefix=spdk%d", 350 opts->shm_id)); 351 if (args == NULL) { 352 return -1; 353 } 354 355 /* set the process type */ 356 args = spdk_push_arg(args, &argcount, _sprintf_alloc("--proc-type=auto")); 357 if (args == NULL) { 358 return -1; 359 } 360 } 361 #endif 362 363 g_eal_cmdline = args; 364 g_eal_cmdline_argcount = argcount; 365 return argcount; 366 } 367 368 int 369 spdk_env_dpdk_post_init(void) 370 { 371 spdk_pci_init(); 372 373 if (spdk_mem_map_init() < 0) { 374 fprintf(stderr, "Failed to allocate mem_map\n"); 375 return -1; 376 } 377 if (spdk_vtophys_init() < 0) { 378 fprintf(stderr, "Failed to initialize vtophys\n"); 379 return -1; 380 } 381 382 return 0; 383 } 384 385 void 386 spdk_env_dpdk_post_fini(void) 387 { 388 spdk_pci_fini(); 389 390 spdk_free_args(g_eal_cmdline, g_eal_cmdline_argcount); 391 } 392 393 int 394 spdk_env_init(const struct spdk_env_opts *opts) 395 { 396 char **dpdk_args = NULL; 397 int i, rc; 398 int orig_optind; 399 400 g_external_init = false; 401 402 rc = spdk_build_eal_cmdline(opts); 403 if (rc < 0) { 404 fprintf(stderr, "Invalid arguments to initialize DPDK\n"); 405 return -1; 406 } 407 408 printf("Starting %s / %s initialization...\n", SPDK_VERSION_STRING, rte_version()); 409 printf("[ DPDK EAL parameters: "); 410 for (i = 0; i < g_eal_cmdline_argcount; i++) { 411 printf("%s ", g_eal_cmdline[i]); 412 } 413 printf("]\n"); 414 415 /* DPDK rearranges the array we pass to it, so make a copy 416 * before passing so we can still free the individual strings 417 * correctly. 418 */ 419 dpdk_args = calloc(g_eal_cmdline_argcount, sizeof(char *)); 420 if (dpdk_args == NULL) { 421 fprintf(stderr, "Failed to allocate dpdk_args\n"); 422 return -1; 423 } 424 memcpy(dpdk_args, g_eal_cmdline, sizeof(char *) * g_eal_cmdline_argcount); 425 426 fflush(stdout); 427 orig_optind = optind; 428 optind = 1; 429 rc = rte_eal_init(g_eal_cmdline_argcount, dpdk_args); 430 optind = orig_optind; 431 432 free(dpdk_args); 433 434 if (rc < 0) { 435 fprintf(stderr, "Failed to initialize DPDK\n"); 436 return -1; 437 } 438 439 if (opts->shm_id < 0 && !opts->hugepage_single_segments) { 440 /* 441 * Unlink hugepage and config info files after init. This will ensure they get 442 * deleted on app exit, even if the app crashes and does not exit normally. 443 * Only do this when not in multi-process mode, since for multi-process other 444 * apps will need to open these files. These files are not created for 445 * "single file segments". 446 */ 447 spdk_env_unlink_shared_files(); 448 } 449 450 return spdk_env_dpdk_post_init(); 451 } 452 453 void 454 spdk_env_fini(void) 455 { 456 spdk_env_dpdk_post_fini(); 457 } 458 459 bool 460 spdk_env_dpdk_external_init(void) 461 { 462 return g_external_init; 463 } 464