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