1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2010-2018 Intel Corporation. 3 * Copyright(c) 2012-2014 6WIND S.A. 4 */ 5 6 #include <ctype.h> 7 #include <stdio.h> 8 #include <stdlib.h> 9 #include <stdint.h> 10 #include <string.h> 11 #include <unistd.h> 12 #include <pthread.h> 13 #include <getopt.h> 14 #include <sys/file.h> 15 #include <dirent.h> 16 #include <fcntl.h> 17 #include <fnmatch.h> 18 #include <stddef.h> 19 #include <errno.h> 20 #include <limits.h> 21 #include <sys/mman.h> 22 #include <sys/stat.h> 23 #if defined(RTE_ARCH_X86) 24 #include <sys/io.h> 25 #endif 26 #include <linux/version.h> 27 28 #include <rte_common.h> 29 #include <rte_debug.h> 30 #include <rte_memory.h> 31 #include <rte_launch.h> 32 #include <rte_eal.h> 33 #include <rte_eal_memconfig.h> 34 #include <rte_errno.h> 35 #include <rte_lcore.h> 36 #include <rte_service_component.h> 37 #include <rte_log.h> 38 #include <rte_string_fns.h> 39 #include <rte_cpuflags.h> 40 #include <rte_bus.h> 41 #include <rte_version.h> 42 #include <malloc_heap.h> 43 #include <rte_vfio.h> 44 45 #include <telemetry_internal.h> 46 #include "eal_private.h" 47 #include "eal_thread.h" 48 #include "eal_internal_cfg.h" 49 #include "eal_filesystem.h" 50 #include "eal_hugepages.h" 51 #include "eal_memcfg.h" 52 #include "eal_trace.h" 53 #include "eal_log.h" 54 #include "eal_options.h" 55 #include "eal_vfio.h" 56 #include "hotplug_mp.h" 57 58 #define MEMSIZE_IF_NO_HUGE_PAGE (64ULL * 1024ULL * 1024ULL) 59 60 #define SOCKET_MEM_STRLEN (RTE_MAX_NUMA_NODES * 10) 61 62 #define KERNEL_IOMMU_GROUPS_PATH "/sys/kernel/iommu_groups" 63 64 /* define fd variable here, because file needs to be kept open for the 65 * duration of the program, as we hold a write lock on it in the primary proc */ 66 static int mem_cfg_fd = -1; 67 68 static struct flock wr_lock = { 69 .l_type = F_WRLCK, 70 .l_whence = SEEK_SET, 71 .l_start = offsetof(struct rte_mem_config, memsegs), 72 .l_len = RTE_SIZEOF_FIELD(struct rte_mem_config, memsegs), 73 }; 74 75 /* internal configuration (per-core) */ 76 struct lcore_config lcore_config[RTE_MAX_LCORE]; 77 78 /* used by rte_rdtsc() */ 79 int rte_cycles_vmware_tsc_map; 80 81 82 int 83 eal_clean_runtime_dir(void) 84 { 85 const char *runtime_dir = rte_eal_get_runtime_dir(); 86 DIR *dir; 87 struct dirent *dirent; 88 int dir_fd, fd, lck_result; 89 static const char * const filters[] = { 90 "fbarray_*", 91 "mp_socket_*" 92 }; 93 94 /* open directory */ 95 dir = opendir(runtime_dir); 96 if (!dir) { 97 RTE_LOG(ERR, EAL, "Unable to open runtime directory %s\n", 98 runtime_dir); 99 goto error; 100 } 101 dir_fd = dirfd(dir); 102 103 /* lock the directory before doing anything, to avoid races */ 104 if (flock(dir_fd, LOCK_EX) < 0) { 105 RTE_LOG(ERR, EAL, "Unable to lock runtime directory %s\n", 106 runtime_dir); 107 goto error; 108 } 109 110 dirent = readdir(dir); 111 if (!dirent) { 112 RTE_LOG(ERR, EAL, "Unable to read runtime directory %s\n", 113 runtime_dir); 114 goto error; 115 } 116 117 while (dirent != NULL) { 118 unsigned int f_idx; 119 bool skip = true; 120 121 /* skip files that don't match the patterns */ 122 for (f_idx = 0; f_idx < RTE_DIM(filters); f_idx++) { 123 const char *filter = filters[f_idx]; 124 125 if (fnmatch(filter, dirent->d_name, 0) == 0) { 126 skip = false; 127 break; 128 } 129 } 130 if (skip) { 131 dirent = readdir(dir); 132 continue; 133 } 134 135 /* try and lock the file */ 136 fd = openat(dir_fd, dirent->d_name, O_RDONLY); 137 138 /* skip to next file */ 139 if (fd == -1) { 140 dirent = readdir(dir); 141 continue; 142 } 143 144 /* non-blocking lock */ 145 lck_result = flock(fd, LOCK_EX | LOCK_NB); 146 147 /* if lock succeeds, remove the file */ 148 if (lck_result != -1) 149 unlinkat(dir_fd, dirent->d_name, 0); 150 close(fd); 151 dirent = readdir(dir); 152 } 153 154 /* closedir closes dir_fd and drops the lock */ 155 closedir(dir); 156 return 0; 157 158 error: 159 if (dir) 160 closedir(dir); 161 162 RTE_LOG(ERR, EAL, "Error while clearing runtime dir: %s\n", 163 strerror(errno)); 164 165 return -1; 166 } 167 168 169 /* create memory configuration in shared/mmap memory. Take out 170 * a write lock on the memsegs, so we can auto-detect primary/secondary. 171 * This means we never close the file while running (auto-close on exit). 172 * We also don't lock the whole file, so that in future we can use read-locks 173 * on other parts, e.g. memzones, to detect if there are running secondary 174 * processes. */ 175 static int 176 rte_eal_config_create(void) 177 { 178 struct rte_config *config = rte_eal_get_configuration(); 179 size_t page_sz = sysconf(_SC_PAGE_SIZE); 180 size_t cfg_len = sizeof(*config->mem_config); 181 size_t cfg_len_aligned = RTE_ALIGN(cfg_len, page_sz); 182 void *rte_mem_cfg_addr, *mapped_mem_cfg_addr; 183 int retval; 184 const struct internal_config *internal_conf = 185 eal_get_internal_configuration(); 186 187 const char *pathname = eal_runtime_config_path(); 188 189 if (internal_conf->no_shconf) 190 return 0; 191 192 /* map the config before hugepage address so that we don't waste a page */ 193 if (internal_conf->base_virtaddr != 0) 194 rte_mem_cfg_addr = (void *) 195 RTE_ALIGN_FLOOR(internal_conf->base_virtaddr - 196 sizeof(struct rte_mem_config), page_sz); 197 else 198 rte_mem_cfg_addr = NULL; 199 200 if (mem_cfg_fd < 0){ 201 mem_cfg_fd = open(pathname, O_RDWR | O_CREAT, 0600); 202 if (mem_cfg_fd < 0) { 203 RTE_LOG(ERR, EAL, "Cannot open '%s' for rte_mem_config\n", 204 pathname); 205 return -1; 206 } 207 } 208 209 retval = ftruncate(mem_cfg_fd, cfg_len); 210 if (retval < 0){ 211 close(mem_cfg_fd); 212 mem_cfg_fd = -1; 213 RTE_LOG(ERR, EAL, "Cannot resize '%s' for rte_mem_config\n", 214 pathname); 215 return -1; 216 } 217 218 retval = fcntl(mem_cfg_fd, F_SETLK, &wr_lock); 219 if (retval < 0){ 220 close(mem_cfg_fd); 221 mem_cfg_fd = -1; 222 RTE_LOG(ERR, EAL, "Cannot create lock on '%s'. Is another primary " 223 "process running?\n", pathname); 224 return -1; 225 } 226 227 /* reserve space for config */ 228 rte_mem_cfg_addr = eal_get_virtual_area(rte_mem_cfg_addr, 229 &cfg_len_aligned, page_sz, 0, 0); 230 if (rte_mem_cfg_addr == NULL) { 231 RTE_LOG(ERR, EAL, "Cannot mmap memory for rte_config\n"); 232 close(mem_cfg_fd); 233 mem_cfg_fd = -1; 234 return -1; 235 } 236 237 /* remap the actual file into the space we've just reserved */ 238 mapped_mem_cfg_addr = mmap(rte_mem_cfg_addr, 239 cfg_len_aligned, PROT_READ | PROT_WRITE, 240 MAP_SHARED | MAP_FIXED, mem_cfg_fd, 0); 241 if (mapped_mem_cfg_addr == MAP_FAILED) { 242 munmap(rte_mem_cfg_addr, cfg_len); 243 close(mem_cfg_fd); 244 mem_cfg_fd = -1; 245 RTE_LOG(ERR, EAL, "Cannot remap memory for rte_config\n"); 246 return -1; 247 } 248 249 memcpy(rte_mem_cfg_addr, config->mem_config, sizeof(struct rte_mem_config)); 250 config->mem_config = rte_mem_cfg_addr; 251 252 /* store address of the config in the config itself so that secondary 253 * processes could later map the config into this exact location 254 */ 255 config->mem_config->mem_cfg_addr = (uintptr_t) rte_mem_cfg_addr; 256 config->mem_config->dma_maskbits = 0; 257 258 return 0; 259 } 260 261 /* attach to an existing shared memory config */ 262 static int 263 rte_eal_config_attach(void) 264 { 265 struct rte_config *config = rte_eal_get_configuration(); 266 struct rte_mem_config *mem_config; 267 const struct internal_config *internal_conf = 268 eal_get_internal_configuration(); 269 270 const char *pathname = eal_runtime_config_path(); 271 272 if (internal_conf->no_shconf) 273 return 0; 274 275 if (mem_cfg_fd < 0){ 276 mem_cfg_fd = open(pathname, O_RDWR); 277 if (mem_cfg_fd < 0) { 278 RTE_LOG(ERR, EAL, "Cannot open '%s' for rte_mem_config\n", 279 pathname); 280 return -1; 281 } 282 } 283 284 /* map it as read-only first */ 285 mem_config = (struct rte_mem_config *) mmap(NULL, sizeof(*mem_config), 286 PROT_READ, MAP_SHARED, mem_cfg_fd, 0); 287 if (mem_config == MAP_FAILED) { 288 close(mem_cfg_fd); 289 mem_cfg_fd = -1; 290 RTE_LOG(ERR, EAL, "Cannot mmap memory for rte_config! error %i (%s)\n", 291 errno, strerror(errno)); 292 return -1; 293 } 294 295 config->mem_config = mem_config; 296 297 return 0; 298 } 299 300 /* reattach the shared config at exact memory location primary process has it */ 301 static int 302 rte_eal_config_reattach(void) 303 { 304 struct rte_config *config = rte_eal_get_configuration(); 305 struct rte_mem_config *mem_config; 306 void *rte_mem_cfg_addr; 307 const struct internal_config *internal_conf = 308 eal_get_internal_configuration(); 309 310 if (internal_conf->no_shconf) 311 return 0; 312 313 /* save the address primary process has mapped shared config to */ 314 rte_mem_cfg_addr = 315 (void *) (uintptr_t) config->mem_config->mem_cfg_addr; 316 317 /* unmap original config */ 318 munmap(config->mem_config, sizeof(struct rte_mem_config)); 319 320 /* remap the config at proper address */ 321 mem_config = (struct rte_mem_config *) mmap(rte_mem_cfg_addr, 322 sizeof(*mem_config), PROT_READ | PROT_WRITE, MAP_SHARED, 323 mem_cfg_fd, 0); 324 325 close(mem_cfg_fd); 326 mem_cfg_fd = -1; 327 328 if (mem_config == MAP_FAILED || mem_config != rte_mem_cfg_addr) { 329 if (mem_config != MAP_FAILED) { 330 /* errno is stale, don't use */ 331 RTE_LOG(ERR, EAL, "Cannot mmap memory for rte_config at [%p], got [%p]" 332 " - please use '--" OPT_BASE_VIRTADDR 333 "' option\n", rte_mem_cfg_addr, mem_config); 334 munmap(mem_config, sizeof(struct rte_mem_config)); 335 return -1; 336 } 337 RTE_LOG(ERR, EAL, "Cannot mmap memory for rte_config! error %i (%s)\n", 338 errno, strerror(errno)); 339 return -1; 340 } 341 342 config->mem_config = mem_config; 343 344 return 0; 345 } 346 347 /* Detect if we are a primary or a secondary process */ 348 enum rte_proc_type_t 349 eal_proc_type_detect(void) 350 { 351 enum rte_proc_type_t ptype = RTE_PROC_PRIMARY; 352 const char *pathname = eal_runtime_config_path(); 353 const struct internal_config *internal_conf = 354 eal_get_internal_configuration(); 355 356 /* if there no shared config, there can be no secondary processes */ 357 if (!internal_conf->no_shconf) { 358 /* if we can open the file but not get a write-lock we are a 359 * secondary process. NOTE: if we get a file handle back, we 360 * keep that open and don't close it to prevent a race condition 361 * between multiple opens. 362 */ 363 if (((mem_cfg_fd = open(pathname, O_RDWR)) >= 0) && 364 (fcntl(mem_cfg_fd, F_SETLK, &wr_lock) < 0)) 365 ptype = RTE_PROC_SECONDARY; 366 } 367 368 RTE_LOG(INFO, EAL, "Auto-detected process type: %s\n", 369 ptype == RTE_PROC_PRIMARY ? "PRIMARY" : "SECONDARY"); 370 371 return ptype; 372 } 373 374 /* Sets up rte_config structure with the pointer to shared memory config.*/ 375 static int 376 rte_config_init(void) 377 { 378 struct rte_config *config = rte_eal_get_configuration(); 379 const struct internal_config *internal_conf = 380 eal_get_internal_configuration(); 381 382 config->process_type = internal_conf->process_type; 383 384 switch (config->process_type) { 385 case RTE_PROC_PRIMARY: 386 if (rte_eal_config_create() < 0) 387 return -1; 388 eal_mcfg_update_from_internal(); 389 break; 390 case RTE_PROC_SECONDARY: 391 if (rte_eal_config_attach() < 0) 392 return -1; 393 eal_mcfg_wait_complete(); 394 if (eal_mcfg_check_version() < 0) { 395 RTE_LOG(ERR, EAL, "Primary and secondary process DPDK version mismatch\n"); 396 return -1; 397 } 398 if (rte_eal_config_reattach() < 0) 399 return -1; 400 if (!__rte_mp_enable()) { 401 RTE_LOG(ERR, EAL, "Primary process refused secondary attachment\n"); 402 return -1; 403 } 404 eal_mcfg_update_internal(); 405 break; 406 case RTE_PROC_AUTO: 407 case RTE_PROC_INVALID: 408 RTE_LOG(ERR, EAL, "Invalid process type %d\n", 409 config->process_type); 410 return -1; 411 } 412 413 return 0; 414 } 415 416 /* Unlocks hugepage directories that were locked by eal_hugepage_info_init */ 417 static void 418 eal_hugedirs_unlock(void) 419 { 420 int i; 421 struct internal_config *internal_conf = 422 eal_get_internal_configuration(); 423 424 for (i = 0; i < MAX_HUGEPAGE_SIZES; i++) 425 { 426 /* skip uninitialized */ 427 if (internal_conf->hugepage_info[i].lock_descriptor < 0) 428 continue; 429 /* unlock hugepage file */ 430 flock(internal_conf->hugepage_info[i].lock_descriptor, LOCK_UN); 431 close(internal_conf->hugepage_info[i].lock_descriptor); 432 /* reset the field */ 433 internal_conf->hugepage_info[i].lock_descriptor = -1; 434 } 435 } 436 437 /* display usage */ 438 static void 439 eal_usage(const char *prgname) 440 { 441 rte_usage_hook_t hook = eal_get_application_usage_hook(); 442 443 printf("\nUsage: %s ", prgname); 444 eal_common_usage(); 445 printf("EAL Linux options:\n" 446 " --"OPT_SOCKET_MEM" Memory to allocate on sockets (comma separated values)\n" 447 " --"OPT_SOCKET_LIMIT" Limit memory allocation on sockets (comma separated values)\n" 448 " --"OPT_HUGE_DIR" Directory where hugetlbfs is mounted\n" 449 " --"OPT_FILE_PREFIX" Prefix for hugepage filenames\n" 450 " --"OPT_CREATE_UIO_DEV" Create /dev/uioX (usually done by hotplug)\n" 451 " --"OPT_VFIO_INTR" Interrupt mode for VFIO (legacy|msi|msix)\n" 452 " --"OPT_VFIO_VF_TOKEN" VF token (UUID) shared between SR-IOV PF and VFs\n" 453 " --"OPT_LEGACY_MEM" Legacy memory mode (no dynamic allocation, contiguous segments)\n" 454 " --"OPT_SINGLE_FILE_SEGMENTS" Put all hugepage memory in single files\n" 455 " --"OPT_MATCH_ALLOCATIONS" Free hugepages exactly as allocated\n" 456 " --"OPT_HUGE_WORKER_STACK"[=size]\n" 457 " Allocate worker thread stacks from hugepage memory.\n" 458 " Size is in units of kbytes and defaults to system\n" 459 " thread stack size if not specified.\n" 460 "\n"); 461 /* Allow the application to print its usage message too if hook is set */ 462 if (hook) { 463 printf("===== Application Usage =====\n\n"); 464 (hook)(prgname); 465 } 466 } 467 468 static int 469 eal_parse_socket_arg(char *strval, volatile uint64_t *socket_arg) 470 { 471 char * arg[RTE_MAX_NUMA_NODES]; 472 char *end; 473 int arg_num, i, len; 474 475 len = strnlen(strval, SOCKET_MEM_STRLEN); 476 if (len == SOCKET_MEM_STRLEN) { 477 RTE_LOG(ERR, EAL, "--socket-mem is too long\n"); 478 return -1; 479 } 480 481 /* all other error cases will be caught later */ 482 if (!isdigit(strval[len-1])) 483 return -1; 484 485 /* split the optarg into separate socket values */ 486 arg_num = rte_strsplit(strval, len, 487 arg, RTE_MAX_NUMA_NODES, ','); 488 489 /* if split failed, or 0 arguments */ 490 if (arg_num <= 0) 491 return -1; 492 493 /* parse each defined socket option */ 494 errno = 0; 495 for (i = 0; i < arg_num; i++) { 496 uint64_t val; 497 end = NULL; 498 val = strtoull(arg[i], &end, 10); 499 500 /* check for invalid input */ 501 if ((errno != 0) || 502 (arg[i][0] == '\0') || (end == NULL) || (*end != '\0')) 503 return -1; 504 val <<= 20; 505 socket_arg[i] = val; 506 } 507 508 return 0; 509 } 510 511 static int 512 eal_parse_vfio_intr(const char *mode) 513 { 514 struct internal_config *internal_conf = 515 eal_get_internal_configuration(); 516 unsigned i; 517 static struct { 518 const char *name; 519 enum rte_intr_mode value; 520 } map[] = { 521 { "legacy", RTE_INTR_MODE_LEGACY }, 522 { "msi", RTE_INTR_MODE_MSI }, 523 { "msix", RTE_INTR_MODE_MSIX }, 524 }; 525 526 for (i = 0; i < RTE_DIM(map); i++) { 527 if (!strcmp(mode, map[i].name)) { 528 internal_conf->vfio_intr_mode = map[i].value; 529 return 0; 530 } 531 } 532 return -1; 533 } 534 535 static int 536 eal_parse_vfio_vf_token(const char *vf_token) 537 { 538 struct internal_config *cfg = eal_get_internal_configuration(); 539 rte_uuid_t uuid; 540 541 if (!rte_uuid_parse(vf_token, uuid)) { 542 rte_uuid_copy(cfg->vfio_vf_token, uuid); 543 return 0; 544 } 545 546 return -1; 547 } 548 549 /* Parse the arguments for --log-level only */ 550 static void 551 eal_log_level_parse(int argc, char **argv) 552 { 553 int opt; 554 char **argvopt; 555 int option_index; 556 const int old_optind = optind; 557 const int old_optopt = optopt; 558 char * const old_optarg = optarg; 559 struct internal_config *internal_conf = 560 eal_get_internal_configuration(); 561 562 argvopt = argv; 563 optind = 1; 564 565 while ((opt = getopt_long(argc, argvopt, eal_short_options, 566 eal_long_options, &option_index)) != EOF) { 567 568 int ret; 569 570 /* getopt is not happy, stop right now */ 571 if (opt == '?') 572 break; 573 574 ret = (opt == OPT_LOG_LEVEL_NUM) ? 575 eal_parse_common_option(opt, optarg, internal_conf) : 0; 576 577 /* common parser is not happy */ 578 if (ret < 0) 579 break; 580 } 581 582 /* restore getopt lib */ 583 optind = old_optind; 584 optopt = old_optopt; 585 optarg = old_optarg; 586 } 587 588 static int 589 eal_parse_huge_worker_stack(const char *arg) 590 { 591 struct internal_config *cfg = eal_get_internal_configuration(); 592 593 if (arg == NULL || arg[0] == '\0') { 594 pthread_attr_t attr; 595 int ret; 596 597 if (pthread_attr_init(&attr) != 0) { 598 RTE_LOG(ERR, EAL, "Could not retrieve default stack size\n"); 599 return -1; 600 } 601 ret = pthread_attr_getstacksize(&attr, &cfg->huge_worker_stack_size); 602 pthread_attr_destroy(&attr); 603 if (ret != 0) { 604 RTE_LOG(ERR, EAL, "Could not retrieve default stack size\n"); 605 return -1; 606 } 607 } else { 608 unsigned long stack_size; 609 char *end; 610 611 errno = 0; 612 stack_size = strtoul(arg, &end, 10); 613 if (errno || end == NULL || stack_size == 0 || 614 stack_size >= (size_t)-1 / 1024) 615 return -1; 616 617 cfg->huge_worker_stack_size = stack_size * 1024; 618 } 619 620 RTE_LOG(DEBUG, EAL, "Each worker thread will use %zu kB of DPDK memory as stack\n", 621 cfg->huge_worker_stack_size / 1024); 622 return 0; 623 } 624 625 /* Parse the argument given in the command line of the application */ 626 static int 627 eal_parse_args(int argc, char **argv) 628 { 629 int opt, ret; 630 char **argvopt; 631 int option_index; 632 char *prgname = argv[0]; 633 const int old_optind = optind; 634 const int old_optopt = optopt; 635 char * const old_optarg = optarg; 636 struct internal_config *internal_conf = 637 eal_get_internal_configuration(); 638 639 argvopt = argv; 640 optind = 1; 641 642 while ((opt = getopt_long(argc, argvopt, eal_short_options, 643 eal_long_options, &option_index)) != EOF) { 644 645 /* getopt didn't recognise the option */ 646 if (opt == '?') { 647 eal_usage(prgname); 648 ret = -1; 649 goto out; 650 } 651 652 /* eal_log_level_parse() already handled this option */ 653 if (opt == OPT_LOG_LEVEL_NUM) 654 continue; 655 656 ret = eal_parse_common_option(opt, optarg, internal_conf); 657 /* common parser is not happy */ 658 if (ret < 0) { 659 eal_usage(prgname); 660 ret = -1; 661 goto out; 662 } 663 /* common parser handled this option */ 664 if (ret == 0) 665 continue; 666 667 switch (opt) { 668 case 'h': 669 eal_usage(prgname); 670 exit(EXIT_SUCCESS); 671 672 case OPT_HUGE_DIR_NUM: 673 { 674 char *hdir = strdup(optarg); 675 if (hdir == NULL) 676 RTE_LOG(ERR, EAL, "Could not store hugepage directory\n"); 677 else { 678 /* free old hugepage dir */ 679 free(internal_conf->hugepage_dir); 680 internal_conf->hugepage_dir = hdir; 681 } 682 break; 683 } 684 case OPT_FILE_PREFIX_NUM: 685 { 686 char *prefix = strdup(optarg); 687 if (prefix == NULL) 688 RTE_LOG(ERR, EAL, "Could not store file prefix\n"); 689 else { 690 /* free old prefix */ 691 free(internal_conf->hugefile_prefix); 692 internal_conf->hugefile_prefix = prefix; 693 } 694 break; 695 } 696 case OPT_SOCKET_MEM_NUM: 697 if (eal_parse_socket_arg(optarg, 698 internal_conf->socket_mem) < 0) { 699 RTE_LOG(ERR, EAL, "invalid parameters for --" 700 OPT_SOCKET_MEM "\n"); 701 eal_usage(prgname); 702 ret = -1; 703 goto out; 704 } 705 internal_conf->force_sockets = 1; 706 break; 707 708 case OPT_SOCKET_LIMIT_NUM: 709 if (eal_parse_socket_arg(optarg, 710 internal_conf->socket_limit) < 0) { 711 RTE_LOG(ERR, EAL, "invalid parameters for --" 712 OPT_SOCKET_LIMIT "\n"); 713 eal_usage(prgname); 714 ret = -1; 715 goto out; 716 } 717 internal_conf->force_socket_limits = 1; 718 break; 719 720 case OPT_VFIO_INTR_NUM: 721 if (eal_parse_vfio_intr(optarg) < 0) { 722 RTE_LOG(ERR, EAL, "invalid parameters for --" 723 OPT_VFIO_INTR "\n"); 724 eal_usage(prgname); 725 ret = -1; 726 goto out; 727 } 728 break; 729 730 case OPT_VFIO_VF_TOKEN_NUM: 731 if (eal_parse_vfio_vf_token(optarg) < 0) { 732 RTE_LOG(ERR, EAL, "invalid parameters for --" 733 OPT_VFIO_VF_TOKEN "\n"); 734 eal_usage(prgname); 735 ret = -1; 736 goto out; 737 } 738 break; 739 740 case OPT_CREATE_UIO_DEV_NUM: 741 internal_conf->create_uio_dev = 1; 742 break; 743 744 case OPT_MBUF_POOL_OPS_NAME_NUM: 745 { 746 char *ops_name = strdup(optarg); 747 if (ops_name == NULL) 748 RTE_LOG(ERR, EAL, "Could not store mbuf pool ops name\n"); 749 else { 750 /* free old ops name */ 751 free(internal_conf->user_mbuf_pool_ops_name); 752 753 internal_conf->user_mbuf_pool_ops_name = 754 ops_name; 755 } 756 break; 757 } 758 case OPT_MATCH_ALLOCATIONS_NUM: 759 internal_conf->match_allocations = 1; 760 break; 761 762 case OPT_HUGE_WORKER_STACK_NUM: 763 if (eal_parse_huge_worker_stack(optarg) < 0) { 764 RTE_LOG(ERR, EAL, "invalid parameter for --" 765 OPT_HUGE_WORKER_STACK"\n"); 766 eal_usage(prgname); 767 ret = -1; 768 goto out; 769 } 770 break; 771 772 default: 773 if (opt < OPT_LONG_MIN_NUM && isprint(opt)) { 774 RTE_LOG(ERR, EAL, "Option %c is not supported " 775 "on Linux\n", opt); 776 } else if (opt >= OPT_LONG_MIN_NUM && 777 opt < OPT_LONG_MAX_NUM) { 778 RTE_LOG(ERR, EAL, "Option %s is not supported " 779 "on Linux\n", 780 eal_long_options[option_index].name); 781 } else { 782 RTE_LOG(ERR, EAL, "Option %d is not supported " 783 "on Linux\n", opt); 784 } 785 eal_usage(prgname); 786 ret = -1; 787 goto out; 788 } 789 } 790 791 /* create runtime data directory. In no_shconf mode, skip any errors */ 792 if (eal_create_runtime_dir() < 0) { 793 if (internal_conf->no_shconf == 0) { 794 RTE_LOG(ERR, EAL, "Cannot create runtime directory\n"); 795 ret = -1; 796 goto out; 797 } else 798 RTE_LOG(WARNING, EAL, "No DPDK runtime directory created\n"); 799 } 800 801 if (eal_adjust_config(internal_conf) != 0) { 802 ret = -1; 803 goto out; 804 } 805 806 /* sanity checks */ 807 if (eal_check_common_options(internal_conf) != 0) { 808 eal_usage(prgname); 809 ret = -1; 810 goto out; 811 } 812 813 if (optind >= 0) 814 argv[optind-1] = prgname; 815 ret = optind-1; 816 817 out: 818 /* restore getopt lib */ 819 optind = old_optind; 820 optopt = old_optopt; 821 optarg = old_optarg; 822 823 return ret; 824 } 825 826 static int 827 check_socket(const struct rte_memseg_list *msl, void *arg) 828 { 829 int *socket_id = arg; 830 831 if (msl->external) 832 return 0; 833 834 return *socket_id == msl->socket_id; 835 } 836 837 static void 838 eal_check_mem_on_local_socket(void) 839 { 840 int socket_id; 841 const struct rte_config *config = rte_eal_get_configuration(); 842 843 socket_id = rte_lcore_to_socket_id(config->main_lcore); 844 845 if (rte_memseg_list_walk(check_socket, &socket_id) == 0) 846 RTE_LOG(WARNING, EAL, "WARNING: Main core has no memory on local socket!\n"); 847 } 848 849 static int 850 sync_func(__rte_unused void *arg) 851 { 852 return 0; 853 } 854 855 /* 856 * Request iopl privilege for all RPL, returns 0 on success 857 * iopl() call is mostly for the i386 architecture. For other architectures, 858 * return -1 to indicate IO privilege can't be changed in this way. 859 */ 860 int 861 rte_eal_iopl_init(void) 862 { 863 #if defined(RTE_ARCH_X86) 864 if (iopl(3) != 0) 865 return -1; 866 #endif 867 return 0; 868 } 869 870 #ifdef VFIO_PRESENT 871 static int rte_eal_vfio_setup(void) 872 { 873 if (rte_vfio_enable("vfio")) 874 return -1; 875 876 return 0; 877 } 878 #endif 879 880 static void rte_eal_init_alert(const char *msg) 881 { 882 fprintf(stderr, "EAL: FATAL: %s\n", msg); 883 RTE_LOG(ERR, EAL, "%s\n", msg); 884 } 885 886 /* 887 * On Linux 3.6+, even if VFIO is not loaded, whenever IOMMU is enabled in the 888 * BIOS and in the kernel, /sys/kernel/iommu_groups path will contain kernel 889 * IOMMU groups. If IOMMU is not enabled, that path would be empty. 890 * Therefore, checking if the path is empty will tell us if IOMMU is enabled. 891 */ 892 static bool 893 is_iommu_enabled(void) 894 { 895 DIR *dir = opendir(KERNEL_IOMMU_GROUPS_PATH); 896 struct dirent *d; 897 int n = 0; 898 899 /* if directory doesn't exist, assume IOMMU is not enabled */ 900 if (dir == NULL) 901 return false; 902 903 while ((d = readdir(dir)) != NULL) { 904 /* skip dot and dot-dot */ 905 if (++n > 2) 906 break; 907 } 908 closedir(dir); 909 910 return n > 2; 911 } 912 913 static __rte_noreturn void * 914 eal_worker_thread_loop(void *arg) 915 { 916 eal_thread_loop(arg); 917 } 918 919 static int 920 eal_worker_thread_create(unsigned int lcore_id) 921 { 922 pthread_attr_t *attrp = NULL; 923 void *stack_ptr = NULL; 924 pthread_attr_t attr; 925 size_t stack_size; 926 int ret = -1; 927 928 stack_size = eal_get_internal_configuration()->huge_worker_stack_size; 929 if (stack_size != 0) { 930 /* Allocate NUMA aware stack memory and set pthread attributes */ 931 stack_ptr = rte_zmalloc_socket("lcore_stack", stack_size, 932 RTE_CACHE_LINE_SIZE, rte_lcore_to_socket_id(lcore_id)); 933 if (stack_ptr == NULL) { 934 rte_eal_init_alert("Cannot allocate worker lcore stack memory"); 935 rte_errno = ENOMEM; 936 goto out; 937 } 938 939 if (pthread_attr_init(&attr) != 0) { 940 rte_eal_init_alert("Cannot init pthread attributes"); 941 rte_errno = EFAULT; 942 goto out; 943 } 944 attrp = &attr; 945 946 if (pthread_attr_setstack(attrp, stack_ptr, stack_size) != 0) { 947 rte_eal_init_alert("Cannot set pthread stack attributes"); 948 rte_errno = EFAULT; 949 goto out; 950 } 951 } 952 953 if (pthread_create((pthread_t *)&lcore_config[lcore_id].thread_id.opaque_id, 954 attrp, eal_worker_thread_loop, (void *)(uintptr_t)lcore_id) == 0) 955 ret = 0; 956 957 out: 958 if (ret != 0) 959 rte_free(stack_ptr); 960 if (attrp != NULL) 961 pthread_attr_destroy(attrp); 962 return ret; 963 } 964 965 /* Launch threads, called at application init(). */ 966 int 967 rte_eal_init(int argc, char **argv) 968 { 969 int i, fctret, ret; 970 static uint32_t run_once; 971 uint32_t has_run = 0; 972 char cpuset[RTE_CPU_AFFINITY_STR_LEN]; 973 char thread_name[RTE_MAX_THREAD_NAME_LEN]; 974 bool phys_addrs; 975 const struct rte_config *config = rte_eal_get_configuration(); 976 struct internal_config *internal_conf = 977 eal_get_internal_configuration(); 978 979 /* checks if the machine is adequate */ 980 if (!rte_cpu_is_supported()) { 981 rte_eal_init_alert("unsupported cpu type."); 982 rte_errno = ENOTSUP; 983 return -1; 984 } 985 986 if (!__atomic_compare_exchange_n(&run_once, &has_run, 1, 0, 987 __ATOMIC_RELAXED, __ATOMIC_RELAXED)) { 988 rte_eal_init_alert("already called initialization."); 989 rte_errno = EALREADY; 990 return -1; 991 } 992 993 eal_reset_internal_config(internal_conf); 994 995 /* set log level as early as possible */ 996 eal_log_level_parse(argc, argv); 997 998 /* clone argv to report out later in telemetry */ 999 eal_save_args(argc, argv); 1000 1001 if (rte_eal_cpu_init() < 0) { 1002 rte_eal_init_alert("Cannot detect lcores."); 1003 rte_errno = ENOTSUP; 1004 return -1; 1005 } 1006 1007 fctret = eal_parse_args(argc, argv); 1008 if (fctret < 0) { 1009 rte_eal_init_alert("Invalid 'command line' arguments."); 1010 rte_errno = EINVAL; 1011 __atomic_store_n(&run_once, 0, __ATOMIC_RELAXED); 1012 return -1; 1013 } 1014 1015 if (eal_plugins_init() < 0) { 1016 rte_eal_init_alert("Cannot init plugins"); 1017 rte_errno = EINVAL; 1018 __atomic_store_n(&run_once, 0, __ATOMIC_RELAXED); 1019 return -1; 1020 } 1021 1022 if (eal_trace_init() < 0) { 1023 rte_eal_init_alert("Cannot init trace"); 1024 rte_errno = EFAULT; 1025 return -1; 1026 } 1027 1028 if (eal_option_device_parse()) { 1029 rte_errno = ENODEV; 1030 __atomic_store_n(&run_once, 0, __ATOMIC_RELAXED); 1031 return -1; 1032 } 1033 1034 if (rte_config_init() < 0) { 1035 rte_eal_init_alert("Cannot init config"); 1036 return -1; 1037 } 1038 1039 if (rte_eal_intr_init() < 0) { 1040 rte_eal_init_alert("Cannot init interrupt-handling thread"); 1041 return -1; 1042 } 1043 1044 if (rte_eal_alarm_init() < 0) { 1045 rte_eal_init_alert("Cannot init alarm"); 1046 /* rte_eal_alarm_init sets rte_errno on failure. */ 1047 return -1; 1048 } 1049 1050 /* Put mp channel init before bus scan so that we can init the vdev 1051 * bus through mp channel in the secondary process before the bus scan. 1052 */ 1053 if (rte_mp_channel_init() < 0 && rte_errno != ENOTSUP) { 1054 rte_eal_init_alert("failed to init mp channel"); 1055 if (rte_eal_process_type() == RTE_PROC_PRIMARY) { 1056 rte_errno = EFAULT; 1057 return -1; 1058 } 1059 } 1060 1061 if (rte_bus_scan()) { 1062 rte_eal_init_alert("Cannot scan the buses for devices"); 1063 rte_errno = ENODEV; 1064 __atomic_store_n(&run_once, 0, __ATOMIC_RELAXED); 1065 return -1; 1066 } 1067 1068 phys_addrs = rte_eal_using_phys_addrs() != 0; 1069 1070 /* if no EAL option "--iova-mode=<pa|va>", use bus IOVA scheme */ 1071 if (internal_conf->iova_mode == RTE_IOVA_DC) { 1072 /* autodetect the IOVA mapping mode */ 1073 enum rte_iova_mode iova_mode = rte_bus_get_iommu_class(); 1074 1075 if (iova_mode == RTE_IOVA_DC) { 1076 RTE_LOG(DEBUG, EAL, "Buses did not request a specific IOVA mode.\n"); 1077 1078 if (!RTE_IOVA_IN_MBUF) { 1079 iova_mode = RTE_IOVA_VA; 1080 RTE_LOG(DEBUG, EAL, "IOVA as VA mode is forced by build option.\n"); 1081 } else if (!phys_addrs) { 1082 /* if we have no access to physical addresses, 1083 * pick IOVA as VA mode. 1084 */ 1085 iova_mode = RTE_IOVA_VA; 1086 RTE_LOG(DEBUG, EAL, "Physical addresses are unavailable, selecting IOVA as VA mode.\n"); 1087 #if defined(RTE_LIB_KNI) && LINUX_VERSION_CODE >= KERNEL_VERSION(4, 10, 0) 1088 } else if (rte_eal_check_module("rte_kni") == 1) { 1089 iova_mode = RTE_IOVA_PA; 1090 RTE_LOG(DEBUG, EAL, "KNI is loaded, selecting IOVA as PA mode for better KNI performance.\n"); 1091 #endif 1092 } else if (is_iommu_enabled()) { 1093 /* we have an IOMMU, pick IOVA as VA mode */ 1094 iova_mode = RTE_IOVA_VA; 1095 RTE_LOG(DEBUG, EAL, "IOMMU is available, selecting IOVA as VA mode.\n"); 1096 } else { 1097 /* physical addresses available, and no IOMMU 1098 * found, so pick IOVA as PA. 1099 */ 1100 iova_mode = RTE_IOVA_PA; 1101 RTE_LOG(DEBUG, EAL, "IOMMU is not available, selecting IOVA as PA mode.\n"); 1102 } 1103 } 1104 #if defined(RTE_LIB_KNI) && LINUX_VERSION_CODE < KERNEL_VERSION(4, 10, 0) 1105 /* Workaround for KNI which requires physical address to work 1106 * in kernels < 4.10 1107 */ 1108 if (iova_mode == RTE_IOVA_VA && 1109 rte_eal_check_module("rte_kni") == 1) { 1110 if (phys_addrs) { 1111 iova_mode = RTE_IOVA_PA; 1112 RTE_LOG(WARNING, EAL, "Forcing IOVA as 'PA' because KNI module is loaded\n"); 1113 } else { 1114 RTE_LOG(DEBUG, EAL, "KNI can not work since physical addresses are unavailable\n"); 1115 } 1116 } 1117 #endif 1118 rte_eal_get_configuration()->iova_mode = iova_mode; 1119 } else { 1120 rte_eal_get_configuration()->iova_mode = 1121 internal_conf->iova_mode; 1122 } 1123 1124 if (rte_eal_iova_mode() == RTE_IOVA_PA && !phys_addrs) { 1125 rte_eal_init_alert("Cannot use IOVA as 'PA' since physical addresses are not available"); 1126 rte_errno = EINVAL; 1127 return -1; 1128 } 1129 1130 if (rte_eal_iova_mode() == RTE_IOVA_PA && !RTE_IOVA_IN_MBUF) { 1131 rte_eal_init_alert("Cannot use IOVA as 'PA' as it is disabled during build"); 1132 rte_errno = EINVAL; 1133 return -1; 1134 } 1135 1136 RTE_LOG(INFO, EAL, "Selected IOVA mode '%s'\n", 1137 rte_eal_iova_mode() == RTE_IOVA_PA ? "PA" : "VA"); 1138 1139 if (internal_conf->no_hugetlbfs == 0) { 1140 /* rte_config isn't initialized yet */ 1141 ret = internal_conf->process_type == RTE_PROC_PRIMARY ? 1142 eal_hugepage_info_init() : 1143 eal_hugepage_info_read(); 1144 if (ret < 0) { 1145 rte_eal_init_alert("Cannot get hugepage information."); 1146 rte_errno = EACCES; 1147 __atomic_store_n(&run_once, 0, __ATOMIC_RELAXED); 1148 return -1; 1149 } 1150 } 1151 1152 if (internal_conf->memory == 0 && internal_conf->force_sockets == 0) { 1153 if (internal_conf->no_hugetlbfs) 1154 internal_conf->memory = MEMSIZE_IF_NO_HUGE_PAGE; 1155 } 1156 1157 if (internal_conf->vmware_tsc_map == 1) { 1158 #ifdef RTE_LIBRTE_EAL_VMWARE_TSC_MAP_SUPPORT 1159 rte_cycles_vmware_tsc_map = 1; 1160 RTE_LOG (DEBUG, EAL, "Using VMWARE TSC MAP, " 1161 "you must have monitor_control.pseudo_perfctr = TRUE\n"); 1162 #else 1163 RTE_LOG (WARNING, EAL, "Ignoring --vmware-tsc-map because " 1164 "RTE_LIBRTE_EAL_VMWARE_TSC_MAP_SUPPORT is not set\n"); 1165 #endif 1166 } 1167 1168 if (eal_log_init(program_invocation_short_name, 1169 internal_conf->syslog_facility) < 0) { 1170 rte_eal_init_alert("Cannot init logging."); 1171 rte_errno = ENOMEM; 1172 __atomic_store_n(&run_once, 0, __ATOMIC_RELAXED); 1173 return -1; 1174 } 1175 1176 #ifdef VFIO_PRESENT 1177 if (rte_eal_vfio_setup() < 0) { 1178 rte_eal_init_alert("Cannot init VFIO"); 1179 rte_errno = EAGAIN; 1180 __atomic_store_n(&run_once, 0, __ATOMIC_RELAXED); 1181 return -1; 1182 } 1183 #endif 1184 /* in secondary processes, memory init may allocate additional fbarrays 1185 * not present in primary processes, so to avoid any potential issues, 1186 * initialize memzones first. 1187 */ 1188 if (rte_eal_memzone_init() < 0) { 1189 rte_eal_init_alert("Cannot init memzone"); 1190 rte_errno = ENODEV; 1191 return -1; 1192 } 1193 1194 rte_mcfg_mem_read_lock(); 1195 1196 if (rte_eal_memory_init() < 0) { 1197 rte_mcfg_mem_read_unlock(); 1198 rte_eal_init_alert("Cannot init memory"); 1199 rte_errno = ENOMEM; 1200 return -1; 1201 } 1202 1203 /* the directories are locked during eal_hugepage_info_init */ 1204 eal_hugedirs_unlock(); 1205 1206 if (rte_eal_malloc_heap_init() < 0) { 1207 rte_mcfg_mem_read_unlock(); 1208 rte_eal_init_alert("Cannot init malloc heap"); 1209 rte_errno = ENODEV; 1210 return -1; 1211 } 1212 1213 rte_mcfg_mem_read_unlock(); 1214 1215 if (rte_eal_malloc_heap_populate() < 0) { 1216 rte_eal_init_alert("Cannot init malloc heap"); 1217 rte_errno = ENODEV; 1218 return -1; 1219 } 1220 1221 /* register multi-process action callbacks for hotplug after memory init */ 1222 if (eal_mp_dev_hotplug_init() < 0) { 1223 rte_eal_init_alert("failed to register mp callback for hotplug"); 1224 return -1; 1225 } 1226 1227 if (rte_eal_tailqs_init() < 0) { 1228 rte_eal_init_alert("Cannot init tail queues for objects"); 1229 rte_errno = EFAULT; 1230 return -1; 1231 } 1232 1233 if (rte_eal_timer_init() < 0) { 1234 rte_eal_init_alert("Cannot init HPET or TSC timers"); 1235 rte_errno = ENOTSUP; 1236 return -1; 1237 } 1238 1239 eal_check_mem_on_local_socket(); 1240 1241 if (rte_thread_set_affinity_by_id(rte_thread_self(), 1242 &lcore_config[config->main_lcore].cpuset) != 0) { 1243 rte_eal_init_alert("Cannot set affinity"); 1244 rte_errno = EINVAL; 1245 return -1; 1246 } 1247 __rte_thread_init(config->main_lcore, 1248 &lcore_config[config->main_lcore].cpuset); 1249 1250 ret = eal_thread_dump_current_affinity(cpuset, sizeof(cpuset)); 1251 RTE_LOG(DEBUG, EAL, "Main lcore %u is ready (tid=%zx;cpuset=[%s%s])\n", 1252 config->main_lcore, (uintptr_t)pthread_self(), cpuset, 1253 ret == 0 ? "" : "..."); 1254 1255 RTE_LCORE_FOREACH_WORKER(i) { 1256 1257 /* 1258 * create communication pipes between main thread 1259 * and children 1260 */ 1261 if (pipe(lcore_config[i].pipe_main2worker) < 0) 1262 rte_panic("Cannot create pipe\n"); 1263 if (pipe(lcore_config[i].pipe_worker2main) < 0) 1264 rte_panic("Cannot create pipe\n"); 1265 1266 lcore_config[i].state = WAIT; 1267 1268 /* create a thread for each lcore */ 1269 ret = eal_worker_thread_create(i); 1270 if (ret != 0) 1271 rte_panic("Cannot create thread\n"); 1272 1273 /* Set thread_name for aid in debugging. */ 1274 snprintf(thread_name, sizeof(thread_name), 1275 "rte-worker-%d", i); 1276 rte_thread_set_name(lcore_config[i].thread_id, thread_name); 1277 1278 ret = rte_thread_set_affinity_by_id(lcore_config[i].thread_id, 1279 &lcore_config[i].cpuset); 1280 if (ret != 0) 1281 rte_panic("Cannot set affinity\n"); 1282 } 1283 1284 /* 1285 * Launch a dummy function on all worker lcores, so that main lcore 1286 * knows they are all ready when this function returns. 1287 */ 1288 rte_eal_mp_remote_launch(sync_func, NULL, SKIP_MAIN); 1289 rte_eal_mp_wait_lcore(); 1290 1291 /* initialize services so vdevs register service during bus_probe. */ 1292 ret = rte_service_init(); 1293 if (ret) { 1294 rte_eal_init_alert("rte_service_init() failed"); 1295 rte_errno = -ret; 1296 return -1; 1297 } 1298 1299 /* Probe all the buses and devices/drivers on them */ 1300 if (rte_bus_probe()) { 1301 rte_eal_init_alert("Cannot probe devices"); 1302 rte_errno = ENOTSUP; 1303 return -1; 1304 } 1305 1306 #ifdef VFIO_PRESENT 1307 /* Register mp action after probe() so that we got enough info */ 1308 if (rte_vfio_is_enabled("vfio") && vfio_mp_sync_setup() < 0) 1309 return -1; 1310 #endif 1311 1312 /* initialize default service/lcore mappings and start running. Ignore 1313 * -ENOTSUP, as it indicates no service coremask passed to EAL. 1314 */ 1315 ret = rte_service_start_with_defaults(); 1316 if (ret < 0 && ret != -ENOTSUP) { 1317 rte_errno = -ret; 1318 return -1; 1319 } 1320 1321 /* 1322 * Clean up unused files in runtime directory. We do this at the end of 1323 * init and not at the beginning because we want to clean stuff up 1324 * whether we are primary or secondary process, but we cannot remove 1325 * primary process' files because secondary should be able to run even 1326 * if primary process is dead. 1327 * 1328 * In no_shconf mode, no runtime directory is created in the first 1329 * place, so no cleanup needed. 1330 */ 1331 if (!internal_conf->no_shconf && eal_clean_runtime_dir() < 0) { 1332 rte_eal_init_alert("Cannot clear runtime directory"); 1333 return -1; 1334 } 1335 if (rte_eal_process_type() == RTE_PROC_PRIMARY && !internal_conf->no_telemetry) { 1336 int tlog = rte_log_register_type_and_pick_level( 1337 "lib.telemetry", RTE_LOG_WARNING); 1338 if (tlog < 0) 1339 tlog = RTE_LOGTYPE_EAL; 1340 if (rte_telemetry_init(rte_eal_get_runtime_dir(), 1341 rte_version(), 1342 &internal_conf->ctrl_cpuset, rte_log, tlog) != 0) 1343 return -1; 1344 } 1345 1346 eal_mcfg_complete(); 1347 1348 return fctret; 1349 } 1350 1351 static int 1352 mark_freeable(const struct rte_memseg_list *msl, const struct rte_memseg *ms, 1353 void *arg __rte_unused) 1354 { 1355 /* ms is const, so find this memseg */ 1356 struct rte_memseg *found; 1357 1358 if (msl->external) 1359 return 0; 1360 1361 found = rte_mem_virt2memseg(ms->addr, msl); 1362 1363 found->flags &= ~RTE_MEMSEG_FLAG_DO_NOT_FREE; 1364 1365 return 0; 1366 } 1367 1368 int 1369 rte_eal_cleanup(void) 1370 { 1371 static uint32_t run_once; 1372 uint32_t has_run = 0; 1373 1374 if (!__atomic_compare_exchange_n(&run_once, &has_run, 1, 0, 1375 __ATOMIC_RELAXED, __ATOMIC_RELAXED)) { 1376 RTE_LOG(WARNING, EAL, "Already called cleanup\n"); 1377 rte_errno = EALREADY; 1378 return -1; 1379 } 1380 1381 /* if we're in a primary process, we need to mark hugepages as freeable 1382 * so that finalization can release them back to the system. 1383 */ 1384 struct internal_config *internal_conf = 1385 eal_get_internal_configuration(); 1386 1387 if (rte_eal_process_type() == RTE_PROC_PRIMARY && 1388 internal_conf->hugepage_file.unlink_existing) 1389 rte_memseg_walk(mark_freeable, NULL); 1390 1391 rte_service_finalize(); 1392 #ifdef VFIO_PRESENT 1393 vfio_mp_sync_cleanup(); 1394 #endif 1395 rte_mp_channel_cleanup(); 1396 eal_bus_cleanup(); 1397 rte_trace_save(); 1398 eal_trace_fini(); 1399 eal_mp_dev_hotplug_cleanup(); 1400 rte_eal_alarm_cleanup(); 1401 /* after this point, any DPDK pointers will become dangling */ 1402 rte_eal_memory_detach(); 1403 rte_eal_malloc_heap_cleanup(); 1404 eal_cleanup_config(internal_conf); 1405 rte_eal_log_cleanup(); 1406 return 0; 1407 } 1408 1409 int rte_eal_create_uio_dev(void) 1410 { 1411 const struct internal_config *internal_conf = 1412 eal_get_internal_configuration(); 1413 1414 return internal_conf->create_uio_dev; 1415 } 1416 1417 enum rte_intr_mode 1418 rte_eal_vfio_intr_mode(void) 1419 { 1420 const struct internal_config *internal_conf = 1421 eal_get_internal_configuration(); 1422 1423 return internal_conf->vfio_intr_mode; 1424 } 1425 1426 void 1427 rte_eal_vfio_get_vf_token(rte_uuid_t vf_token) 1428 { 1429 struct internal_config *cfg = eal_get_internal_configuration(); 1430 1431 rte_uuid_copy(vf_token, cfg->vfio_vf_token); 1432 } 1433 1434 int 1435 rte_eal_check_module(const char *module_name) 1436 { 1437 char sysfs_mod_name[PATH_MAX]; 1438 struct stat st; 1439 int n; 1440 1441 if (NULL == module_name) 1442 return -1; 1443 1444 /* Check if there is sysfs mounted */ 1445 if (stat("/sys/module", &st) != 0) { 1446 RTE_LOG(DEBUG, EAL, "sysfs is not mounted! error %i (%s)\n", 1447 errno, strerror(errno)); 1448 return -1; 1449 } 1450 1451 /* A module might be built-in, therefore try sysfs */ 1452 n = snprintf(sysfs_mod_name, PATH_MAX, "/sys/module/%s", module_name); 1453 if (n < 0 || n > PATH_MAX) { 1454 RTE_LOG(DEBUG, EAL, "Could not format module path\n"); 1455 return -1; 1456 } 1457 1458 if (stat(sysfs_mod_name, &st) != 0) { 1459 RTE_LOG(DEBUG, EAL, "Module %s not found! error %i (%s)\n", 1460 sysfs_mod_name, errno, strerror(errno)); 1461 return 0; 1462 } 1463 1464 /* Module has been found */ 1465 return 1; 1466 } 1467