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 #include "spdk/log.h" 41 42 #include <rte_config.h> 43 #include <rte_eal.h> 44 #include <rte_errno.h> 45 #include <rte_vfio.h> 46 47 #define SPDK_ENV_DPDK_DEFAULT_NAME "spdk" 48 #define SPDK_ENV_DPDK_DEFAULT_SHM_ID -1 49 #define SPDK_ENV_DPDK_DEFAULT_MEM_SIZE -1 50 #define SPDK_ENV_DPDK_DEFAULT_MAIN_CORE -1 51 #define SPDK_ENV_DPDK_DEFAULT_MEM_CHANNEL -1 52 #define SPDK_ENV_DPDK_DEFAULT_CORE_MASK "0x1" 53 #define SPDK_ENV_DPDK_DEFAULT_BASE_VIRTADDR 0x200000000000 54 55 #if RTE_VERSION < RTE_VERSION_NUM(20, 11, 0, 0) 56 #define DPDK_ALLOW_PARAM "--pci-whitelist" 57 #define DPDK_BLOCK_PARAM "--pci-blacklist" 58 #define DPDK_MAIN_CORE_PARAM "--master-lcore" 59 #else 60 #define DPDK_ALLOW_PARAM "--allow" 61 #define DPDK_BLOCK_PARAM "--block" 62 #define DPDK_MAIN_CORE_PARAM "--main-lcore" 63 #endif 64 65 static char **g_eal_cmdline; 66 static int g_eal_cmdline_argcount; 67 static bool g_external_init = true; 68 69 static char * 70 _sprintf_alloc(const char *format, ...) 71 { 72 va_list args; 73 va_list args_copy; 74 char *buf; 75 size_t bufsize; 76 int rc; 77 78 va_start(args, format); 79 80 /* Try with a small buffer first. */ 81 bufsize = 32; 82 83 /* Limit maximum buffer size to something reasonable so we don't loop forever. */ 84 while (bufsize <= 1024 * 1024) { 85 buf = malloc(bufsize); 86 if (buf == NULL) { 87 va_end(args); 88 return NULL; 89 } 90 91 va_copy(args_copy, args); 92 rc = vsnprintf(buf, bufsize, format, args_copy); 93 va_end(args_copy); 94 95 /* 96 * If vsnprintf() returned a count within our current buffer size, we are done. 97 * The count does not include the \0 terminator, so rc == bufsize is not OK. 98 */ 99 if (rc >= 0 && (size_t)rc < bufsize) { 100 va_end(args); 101 return buf; 102 } 103 104 /* 105 * vsnprintf() should return the required space, but some libc versions do not 106 * implement this correctly, so just double the buffer size and try again. 107 * 108 * We don't need the data in buf, so rather than realloc(), use free() and malloc() 109 * again to avoid a copy. 110 */ 111 free(buf); 112 bufsize *= 2; 113 } 114 115 va_end(args); 116 return NULL; 117 } 118 119 void 120 spdk_env_opts_init(struct spdk_env_opts *opts) 121 { 122 if (!opts) { 123 return; 124 } 125 126 memset(opts, 0, sizeof(*opts)); 127 128 opts->name = SPDK_ENV_DPDK_DEFAULT_NAME; 129 opts->core_mask = SPDK_ENV_DPDK_DEFAULT_CORE_MASK; 130 opts->shm_id = SPDK_ENV_DPDK_DEFAULT_SHM_ID; 131 opts->mem_size = SPDK_ENV_DPDK_DEFAULT_MEM_SIZE; 132 opts->main_core = SPDK_ENV_DPDK_DEFAULT_MAIN_CORE; 133 opts->mem_channel = SPDK_ENV_DPDK_DEFAULT_MEM_CHANNEL; 134 opts->base_virtaddr = SPDK_ENV_DPDK_DEFAULT_BASE_VIRTADDR; 135 } 136 137 static void 138 free_args(char **args, int argcount) 139 { 140 int i; 141 142 if (args == NULL) { 143 return; 144 } 145 146 for (i = 0; i < argcount; i++) { 147 free(args[i]); 148 } 149 150 if (argcount) { 151 free(args); 152 } 153 } 154 155 static char ** 156 push_arg(char *args[], int *argcount, char *arg) 157 { 158 char **tmp; 159 160 if (arg == NULL) { 161 SPDK_ERRLOG("%s: NULL arg supplied\n", __func__); 162 free_args(args, *argcount); 163 return NULL; 164 } 165 166 tmp = realloc(args, sizeof(char *) * (*argcount + 1)); 167 if (tmp == NULL) { 168 free(arg); 169 free_args(args, *argcount); 170 return NULL; 171 } 172 173 tmp[*argcount] = arg; 174 (*argcount)++; 175 176 return tmp; 177 } 178 179 #if defined(__linux__) && defined(__x86_64__) 180 181 /* TODO: Can likely get this value from rlimits in the future */ 182 #define SPDK_IOMMU_VA_REQUIRED_WIDTH 48 183 #define VTD_CAP_MGAW_SHIFT 16 184 #define VTD_CAP_MGAW_MASK (0x3F << VTD_CAP_MGAW_SHIFT) 185 186 static int 187 get_iommu_width(void) 188 { 189 DIR *dir; 190 FILE *file; 191 struct dirent *entry; 192 char mgaw_path[64]; 193 char buf[64]; 194 char *end; 195 long long int val; 196 int width, tmp; 197 198 dir = opendir("/sys/devices/virtual/iommu/"); 199 if (dir == NULL) { 200 return -EINVAL; 201 } 202 203 width = 0; 204 205 while ((entry = readdir(dir)) != NULL) { 206 /* Find directories named "dmar0", "dmar1", etc */ 207 if (strncmp(entry->d_name, "dmar", sizeof("dmar") - 1) != 0) { 208 continue; 209 } 210 211 tmp = snprintf(mgaw_path, sizeof(mgaw_path), "/sys/devices/virtual/iommu/%s/intel-iommu/cap", 212 entry->d_name); 213 if ((unsigned)tmp >= sizeof(mgaw_path)) { 214 continue; 215 } 216 217 file = fopen(mgaw_path, "r"); 218 if (file == NULL) { 219 continue; 220 } 221 222 if (fgets(buf, sizeof(buf), file) == NULL) { 223 fclose(file); 224 continue; 225 } 226 227 val = strtoll(buf, &end, 16); 228 if (val == LLONG_MIN || val == LLONG_MAX) { 229 fclose(file); 230 continue; 231 } 232 233 tmp = ((val & VTD_CAP_MGAW_MASK) >> VTD_CAP_MGAW_SHIFT) + 1; 234 if (width == 0 || tmp < width) { 235 width = tmp; 236 } 237 238 fclose(file); 239 } 240 241 closedir(dir); 242 243 return width; 244 } 245 246 #endif 247 248 static int 249 build_eal_cmdline(const struct spdk_env_opts *opts) 250 { 251 int argcount = 0; 252 char **args; 253 254 args = NULL; 255 256 /* set the program name */ 257 args = push_arg(args, &argcount, _sprintf_alloc("%s", opts->name)); 258 if (args == NULL) { 259 return -1; 260 } 261 262 /* disable shared configuration files when in single process mode. This allows for cleaner shutdown */ 263 if (opts->shm_id < 0) { 264 args = push_arg(args, &argcount, _sprintf_alloc("%s", "--no-shconf")); 265 if (args == NULL) { 266 return -1; 267 } 268 } 269 270 /* set the coremask */ 271 /* NOTE: If coremask starts with '[' and ends with ']' it is a core list 272 */ 273 if (opts->core_mask[0] == '[') { 274 char *l_arg = _sprintf_alloc("-l %s", opts->core_mask + 1); 275 276 if (l_arg != NULL) { 277 int len = strlen(l_arg); 278 279 if (l_arg[len - 1] == ']') { 280 l_arg[len - 1] = '\0'; 281 } 282 } 283 args = push_arg(args, &argcount, l_arg); 284 } else { 285 args = push_arg(args, &argcount, _sprintf_alloc("-c %s", opts->core_mask)); 286 } 287 288 if (args == NULL) { 289 return -1; 290 } 291 292 /* set the memory channel number */ 293 if (opts->mem_channel > 0) { 294 args = push_arg(args, &argcount, _sprintf_alloc("-n %d", opts->mem_channel)); 295 if (args == NULL) { 296 return -1; 297 } 298 } 299 300 /* set the memory size */ 301 if (opts->mem_size >= 0) { 302 args = push_arg(args, &argcount, _sprintf_alloc("-m %d", opts->mem_size)); 303 if (args == NULL) { 304 return -1; 305 } 306 } 307 308 /* set the main core */ 309 if (opts->main_core > 0) { 310 args = push_arg(args, &argcount, _sprintf_alloc("%s=%d", 311 DPDK_MAIN_CORE_PARAM, opts->main_core)); 312 if (args == NULL) { 313 return -1; 314 } 315 } 316 317 /* set no pci if enabled */ 318 if (opts->no_pci) { 319 args = push_arg(args, &argcount, _sprintf_alloc("--no-pci")); 320 if (args == NULL) { 321 return -1; 322 } 323 } 324 325 /* create just one hugetlbfs file */ 326 if (opts->hugepage_single_segments) { 327 args = push_arg(args, &argcount, _sprintf_alloc("--single-file-segments")); 328 if (args == NULL) { 329 return -1; 330 } 331 } 332 333 /* unlink hugepages after initialization */ 334 if (opts->unlink_hugepage) { 335 args = push_arg(args, &argcount, _sprintf_alloc("--huge-unlink")); 336 if (args == NULL) { 337 return -1; 338 } 339 } 340 341 /* use a specific hugetlbfs mount */ 342 if (opts->hugedir) { 343 args = push_arg(args, &argcount, _sprintf_alloc("--huge-dir=%s", opts->hugedir)); 344 if (args == NULL) { 345 return -1; 346 } 347 } 348 349 if (opts->num_pci_addr) { 350 size_t i; 351 char bdf[32]; 352 struct spdk_pci_addr *pci_addr = 353 opts->pci_blocked ? opts->pci_blocked : opts->pci_allowed; 354 355 for (i = 0; i < opts->num_pci_addr; i++) { 356 spdk_pci_addr_fmt(bdf, 32, &pci_addr[i]); 357 args = push_arg(args, &argcount, _sprintf_alloc("%s=%s", 358 (opts->pci_blocked ? DPDK_BLOCK_PARAM : DPDK_ALLOW_PARAM), 359 bdf)); 360 if (args == NULL) { 361 return -1; 362 } 363 } 364 } 365 366 /* Lower default EAL loglevel to RTE_LOG_NOTICE - normal, but significant messages. 367 * This can be overridden by specifying the same option in opts->env_context 368 */ 369 args = push_arg(args, &argcount, strdup("--log-level=lib.eal:6")); 370 if (args == NULL) { 371 return -1; 372 } 373 374 /* Lower default CRYPTO loglevel to RTE_LOG_ERR to avoid a ton of init msgs. 375 * This can be overridden by specifying the same option in opts->env_context 376 */ 377 args = push_arg(args, &argcount, strdup("--log-level=lib.cryptodev:5")); 378 if (args == NULL) { 379 return -1; 380 } 381 382 /* `user1` log type is used by rte_vhost, which prints an INFO log for each received 383 * vhost user message. We don't want that. The same log type is also used by a couple 384 * of other DPDK libs, but none of which we make use right now. If necessary, this can 385 * be overridden via opts->env_context. 386 */ 387 args = push_arg(args, &argcount, strdup("--log-level=user1:6")); 388 if (args == NULL) { 389 return -1; 390 } 391 392 if (opts->env_context) { 393 args = push_arg(args, &argcount, strdup(opts->env_context)); 394 if (args == NULL) { 395 return -1; 396 } 397 } 398 399 #ifdef __linux__ 400 401 if (opts->iova_mode) { 402 args = push_arg(args, &argcount, _sprintf_alloc("--iova-mode=%s", opts->iova_mode)); 403 if (args == NULL) { 404 return -1; 405 } 406 } else { 407 /* When using vfio with enable_unsafe_noiommu_mode=Y, we need iova-mode=pa, 408 * but DPDK guesses it should be iova-mode=va. Add a check and force 409 * iova-mode=pa here. */ 410 if (rte_vfio_noiommu_is_enabled()) { 411 args = push_arg(args, &argcount, _sprintf_alloc("--iova-mode=pa")); 412 if (args == NULL) { 413 return -1; 414 } 415 } 416 417 #if defined(__x86_64__) 418 /* DPDK by default guesses that it should be using iova-mode=va so that it can 419 * support running as an unprivileged user. However, some systems (especially 420 * virtual machines) don't have an IOMMU capable of handling the full virtual 421 * address space and DPDK doesn't currently catch that. Add a check in SPDK 422 * and force iova-mode=pa here. */ 423 if (get_iommu_width() < SPDK_IOMMU_VA_REQUIRED_WIDTH) { 424 args = push_arg(args, &argcount, _sprintf_alloc("--iova-mode=pa")); 425 if (args == NULL) { 426 return -1; 427 } 428 } 429 #elif defined(__PPC64__) 430 /* On Linux + PowerPC, DPDK doesn't support VA mode at all. Unfortunately, it doesn't correctly 431 * auto-detect at the moment, so we'll just force it here. */ 432 args = push_arg(args, &argcount, _sprintf_alloc("--iova-mode=pa")); 433 if (args == NULL) { 434 return -1; 435 } 436 #endif 437 } 438 439 440 /* Set the base virtual address - it must be an address that is not in the 441 * ASAN shadow region, otherwise ASAN-enabled builds will ignore the 442 * mmap hint. 443 * 444 * Ref: https://github.com/google/sanitizers/wiki/AddressSanitizerAlgorithm 445 */ 446 args = push_arg(args, &argcount, _sprintf_alloc("--base-virtaddr=0x%" PRIx64, opts->base_virtaddr)); 447 if (args == NULL) { 448 return -1; 449 } 450 451 /* --match-allocation prevents DPDK from merging or splitting system memory allocations under the hood. 452 * This is critical for RDMA when attempting to use an rte_mempool based buffer pool. If DPDK merges two 453 * physically or IOVA contiguous memory regions, then when we go to allocate a buffer pool, it can split 454 * the memory for a buffer over two allocations meaning the buffer will be split over a memory region. 455 */ 456 if (!opts->env_context || strstr(opts->env_context, "--legacy-mem") == NULL) { 457 args = push_arg(args, &argcount, _sprintf_alloc("%s", "--match-allocations")); 458 if (args == NULL) { 459 return -1; 460 } 461 } 462 463 if (opts->shm_id < 0) { 464 args = push_arg(args, &argcount, _sprintf_alloc("--file-prefix=spdk_pid%d", 465 getpid())); 466 if (args == NULL) { 467 return -1; 468 } 469 } else { 470 args = push_arg(args, &argcount, _sprintf_alloc("--file-prefix=spdk%d", 471 opts->shm_id)); 472 if (args == NULL) { 473 return -1; 474 } 475 476 /* set the process type */ 477 args = push_arg(args, &argcount, _sprintf_alloc("--proc-type=auto")); 478 if (args == NULL) { 479 return -1; 480 } 481 } 482 #endif 483 484 g_eal_cmdline = args; 485 g_eal_cmdline_argcount = argcount; 486 return argcount; 487 } 488 489 int 490 spdk_env_dpdk_post_init(bool legacy_mem) 491 { 492 int rc; 493 494 pci_env_init(); 495 496 rc = mem_map_init(legacy_mem); 497 if (rc < 0) { 498 SPDK_ERRLOG("Failed to allocate mem_map\n"); 499 return rc; 500 } 501 502 rc = vtophys_init(); 503 if (rc < 0) { 504 SPDK_ERRLOG("Failed to initialize vtophys\n"); 505 return rc; 506 } 507 508 return 0; 509 } 510 511 void 512 spdk_env_dpdk_post_fini(void) 513 { 514 pci_env_fini(); 515 516 free_args(g_eal_cmdline, g_eal_cmdline_argcount); 517 g_eal_cmdline = NULL; 518 g_eal_cmdline_argcount = 0; 519 } 520 521 int 522 spdk_env_init(const struct spdk_env_opts *opts) 523 { 524 char **dpdk_args = NULL; 525 int i, rc; 526 int orig_optind; 527 bool legacy_mem; 528 529 /* If SPDK env has been initialized before, then only pci env requires 530 * reinitialization. 531 */ 532 if (g_external_init == false) { 533 if (opts != NULL) { 534 fprintf(stderr, "Invalid arguments to reinitialize SPDK env\n"); 535 return -EINVAL; 536 } 537 538 printf("Starting %s / %s reinitialization...\n", SPDK_VERSION_STRING, rte_version()); 539 pci_env_reinit(); 540 541 return 0; 542 } 543 544 if (opts == NULL) { 545 fprintf(stderr, "NULL arguments to initialize DPDK\n"); 546 return -EINVAL; 547 } 548 549 rc = build_eal_cmdline(opts); 550 if (rc < 0) { 551 SPDK_ERRLOG("Invalid arguments to initialize DPDK\n"); 552 return -EINVAL; 553 } 554 555 SPDK_PRINTF("Starting %s / %s initialization...\n", SPDK_VERSION_STRING, rte_version()); 556 SPDK_PRINTF("[ DPDK EAL parameters: "); 557 for (i = 0; i < g_eal_cmdline_argcount; i++) { 558 SPDK_PRINTF("%s ", g_eal_cmdline[i]); 559 } 560 SPDK_PRINTF("]\n"); 561 562 /* DPDK rearranges the array we pass to it, so make a copy 563 * before passing so we can still free the individual strings 564 * correctly. 565 */ 566 dpdk_args = calloc(g_eal_cmdline_argcount, sizeof(char *)); 567 if (dpdk_args == NULL) { 568 SPDK_ERRLOG("Failed to allocate dpdk_args\n"); 569 return -ENOMEM; 570 } 571 memcpy(dpdk_args, g_eal_cmdline, sizeof(char *) * g_eal_cmdline_argcount); 572 573 fflush(stdout); 574 orig_optind = optind; 575 optind = 1; 576 rc = rte_eal_init(g_eal_cmdline_argcount, dpdk_args); 577 optind = orig_optind; 578 579 free(dpdk_args); 580 581 if (rc < 0) { 582 if (rte_errno == EALREADY) { 583 SPDK_ERRLOG("DPDK already initialized\n"); 584 } else { 585 SPDK_ERRLOG("Failed to initialize DPDK\n"); 586 } 587 return -rte_errno; 588 } 589 590 legacy_mem = false; 591 if (opts->env_context && strstr(opts->env_context, "--legacy-mem") != NULL) { 592 legacy_mem = true; 593 } 594 595 rc = spdk_env_dpdk_post_init(legacy_mem); 596 if (rc == 0) { 597 g_external_init = false; 598 } 599 600 return rc; 601 } 602 603 void 604 spdk_env_fini(void) 605 { 606 spdk_env_dpdk_post_fini(); 607 } 608 609 bool 610 spdk_env_dpdk_external_init(void) 611 { 612 return g_external_init; 613 } 614