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_options.h" 54 #include "eal_vfio.h" 55 #include "hotplug_mp.h" 56 #include "log_internal.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 EAL_LOG(ERR, "Unable to open runtime directory %s", 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 EAL_LOG(ERR, "Unable to lock runtime directory %s", 106 runtime_dir); 107 goto error; 108 } 109 110 dirent = readdir(dir); 111 if (!dirent) { 112 EAL_LOG(ERR, "Unable to read runtime directory %s", 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 EAL_LOG(ERR, "Error while clearing runtime dir: %s", 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 EAL_LOG(ERR, "Cannot open '%s' for rte_mem_config", 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 EAL_LOG(ERR, "Cannot resize '%s' for rte_mem_config", 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 EAL_LOG(ERR, "Cannot create lock on '%s'. Is another primary " 223 "process running?", 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 EAL_LOG(ERR, "Cannot mmap memory for rte_config"); 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 EAL_LOG(ERR, "Cannot remap memory for rte_config"); 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 EAL_LOG(ERR, "Cannot open '%s' for rte_mem_config", 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 EAL_LOG(ERR, "Cannot mmap memory for rte_config! error %i (%s)", 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 EAL_LOG(ERR, "Cannot mmap memory for rte_config at [%p], got [%p]" 332 " - please use '--" OPT_BASE_VIRTADDR 333 "' option", rte_mem_cfg_addr, mem_config); 334 munmap(mem_config, sizeof(struct rte_mem_config)); 335 return -1; 336 } 337 EAL_LOG(ERR, "Cannot mmap memory for rte_config! error %i (%s)", 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 EAL_LOG(INFO, "Auto-detected process type: %s", 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 EAL_LOG(ERR, "Primary and secondary process DPDK version mismatch"); 396 return -1; 397 } 398 if (rte_eal_config_reattach() < 0) 399 return -1; 400 if (!__rte_mp_enable()) { 401 EAL_LOG(ERR, "Primary process refused secondary attachment"); 402 return -1; 403 } 404 eal_mcfg_update_internal(); 405 break; 406 case RTE_PROC_AUTO: 407 case RTE_PROC_INVALID: 408 EAL_LOG(ERR, "Invalid process type %d", 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 EAL_LOG(ERR, "--socket-mem is too long"); 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 EAL_LOG(ERR, "Could not retrieve default stack size"); 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 EAL_LOG(ERR, "Could not retrieve default stack size"); 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 EAL_LOG(DEBUG, "Each worker thread will use %zu kB of DPDK memory as stack", 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 OPT_HELP_NUM: 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 EAL_LOG(ERR, "Could not store hugepage directory"); 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 EAL_LOG(ERR, "Could not store file prefix"); 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 EAL_LOG(ERR, "invalid parameters for --" 700 OPT_SOCKET_MEM); 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 EAL_LOG(ERR, "invalid parameters for --" 712 OPT_SOCKET_LIMIT); 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 EAL_LOG(ERR, "invalid parameters for --" 723 OPT_VFIO_INTR); 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 EAL_LOG(ERR, "invalid parameters for --" 733 OPT_VFIO_VF_TOKEN); 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 EAL_LOG(ERR, "Could not store mbuf pool ops name"); 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 EAL_LOG(ERR, "invalid parameter for --" 765 OPT_HUGE_WORKER_STACK); 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 EAL_LOG(ERR, "Option %c is not supported " 775 "on Linux", opt); 776 } else if (opt >= OPT_LONG_MIN_NUM && 777 opt < OPT_LONG_MAX_NUM) { 778 EAL_LOG(ERR, "Option %s is not supported " 779 "on Linux", 780 eal_long_options[option_index].name); 781 } else { 782 EAL_LOG(ERR, "Option %d is not supported " 783 "on Linux", 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 EAL_LOG(ERR, "Cannot create runtime directory"); 795 ret = -1; 796 goto out; 797 } else 798 EAL_LOG(WARNING, "No DPDK runtime directory created"); 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 EAL_LOG(WARNING, "WARNING: Main core has no memory on local socket!"); 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 static void rte_eal_init_alert(const char *msg) 871 { 872 fprintf(stderr, "EAL: FATAL: %s\n", msg); 873 EAL_LOG(ERR, "%s", msg); 874 } 875 876 /* 877 * On Linux 3.6+, even if VFIO is not loaded, whenever IOMMU is enabled in the 878 * BIOS and in the kernel, /sys/kernel/iommu_groups path will contain kernel 879 * IOMMU groups. If IOMMU is not enabled, that path would be empty. 880 * Therefore, checking if the path is empty will tell us if IOMMU is enabled. 881 */ 882 static bool 883 is_iommu_enabled(void) 884 { 885 DIR *dir = opendir(KERNEL_IOMMU_GROUPS_PATH); 886 struct dirent *d; 887 int n = 0; 888 889 /* if directory doesn't exist, assume IOMMU is not enabled */ 890 if (dir == NULL) 891 return false; 892 893 while ((d = readdir(dir)) != NULL) { 894 /* skip dot and dot-dot */ 895 if (++n > 2) 896 break; 897 } 898 closedir(dir); 899 900 return n > 2; 901 } 902 903 static __rte_noreturn void * 904 eal_worker_thread_loop(void *arg) 905 { 906 eal_thread_loop(arg); 907 } 908 909 static int 910 eal_worker_thread_create(unsigned int lcore_id) 911 { 912 pthread_attr_t *attrp = NULL; 913 void *stack_ptr = NULL; 914 pthread_attr_t attr; 915 size_t stack_size; 916 int ret = -1; 917 918 stack_size = eal_get_internal_configuration()->huge_worker_stack_size; 919 if (stack_size != 0) { 920 /* Allocate NUMA aware stack memory and set pthread attributes */ 921 stack_ptr = rte_zmalloc_socket("lcore_stack", stack_size, 922 RTE_CACHE_LINE_SIZE, rte_lcore_to_socket_id(lcore_id)); 923 if (stack_ptr == NULL) { 924 rte_eal_init_alert("Cannot allocate worker lcore stack memory"); 925 rte_errno = ENOMEM; 926 goto out; 927 } 928 929 if (pthread_attr_init(&attr) != 0) { 930 rte_eal_init_alert("Cannot init pthread attributes"); 931 rte_errno = EFAULT; 932 goto out; 933 } 934 attrp = &attr; 935 936 if (pthread_attr_setstack(attrp, stack_ptr, stack_size) != 0) { 937 rte_eal_init_alert("Cannot set pthread stack attributes"); 938 rte_errno = EFAULT; 939 goto out; 940 } 941 } 942 943 if (pthread_create((pthread_t *)&lcore_config[lcore_id].thread_id.opaque_id, 944 attrp, eal_worker_thread_loop, (void *)(uintptr_t)lcore_id) == 0) 945 ret = 0; 946 947 out: 948 if (ret != 0) 949 rte_free(stack_ptr); 950 if (attrp != NULL) 951 pthread_attr_destroy(attrp); 952 return ret; 953 } 954 955 /* Launch threads, called at application init(). */ 956 int 957 rte_eal_init(int argc, char **argv) 958 { 959 int i, fctret, ret; 960 static RTE_ATOMIC(uint32_t) run_once; 961 uint32_t has_run = 0; 962 char cpuset[RTE_CPU_AFFINITY_STR_LEN]; 963 char thread_name[RTE_THREAD_NAME_SIZE]; 964 bool phys_addrs; 965 const struct rte_config *config = rte_eal_get_configuration(); 966 struct internal_config *internal_conf = 967 eal_get_internal_configuration(); 968 969 /* checks if the machine is adequate */ 970 if (!rte_cpu_is_supported()) { 971 rte_eal_init_alert("unsupported cpu type."); 972 rte_errno = ENOTSUP; 973 return -1; 974 } 975 976 /* verify if DPDK supported on architecture MMU */ 977 if (!eal_mmu_supported()) { 978 rte_eal_init_alert("unsupported MMU type."); 979 rte_errno = ENOTSUP; 980 return -1; 981 } 982 983 if (!rte_atomic_compare_exchange_strong_explicit(&run_once, &has_run, 1, 984 rte_memory_order_relaxed, rte_memory_order_relaxed)) { 985 rte_eal_init_alert("already called initialization."); 986 rte_errno = EALREADY; 987 return -1; 988 } 989 990 eal_reset_internal_config(internal_conf); 991 992 /* set log level as early as possible */ 993 eal_log_level_parse(argc, argv); 994 995 /* clone argv to report out later in telemetry */ 996 eal_save_args(argc, argv); 997 998 if (rte_eal_cpu_init() < 0) { 999 rte_eal_init_alert("Cannot detect lcores."); 1000 rte_errno = ENOTSUP; 1001 return -1; 1002 } 1003 1004 fctret = eal_parse_args(argc, argv); 1005 if (fctret < 0) { 1006 rte_eal_init_alert("Invalid 'command line' arguments."); 1007 rte_errno = EINVAL; 1008 rte_atomic_store_explicit(&run_once, 0, rte_memory_order_relaxed); 1009 return -1; 1010 } 1011 1012 if (eal_plugins_init() < 0) { 1013 rte_eal_init_alert("Cannot init plugins"); 1014 rte_errno = EINVAL; 1015 rte_atomic_store_explicit(&run_once, 0, rte_memory_order_relaxed); 1016 return -1; 1017 } 1018 1019 if (eal_trace_init() < 0) { 1020 rte_eal_init_alert("Cannot init trace"); 1021 rte_errno = EFAULT; 1022 return -1; 1023 } 1024 1025 if (eal_option_device_parse()) { 1026 rte_errno = ENODEV; 1027 rte_atomic_store_explicit(&run_once, 0, rte_memory_order_relaxed); 1028 return -1; 1029 } 1030 1031 if (rte_config_init() < 0) { 1032 rte_eal_init_alert("Cannot init config"); 1033 return -1; 1034 } 1035 1036 if (rte_eal_intr_init() < 0) { 1037 rte_eal_init_alert("Cannot init interrupt-handling thread"); 1038 return -1; 1039 } 1040 1041 if (rte_eal_alarm_init() < 0) { 1042 rte_eal_init_alert("Cannot init alarm"); 1043 /* rte_eal_alarm_init sets rte_errno on failure. */ 1044 return -1; 1045 } 1046 1047 /* Put mp channel init before bus scan so that we can init the vdev 1048 * bus through mp channel in the secondary process before the bus scan. 1049 */ 1050 if (rte_mp_channel_init() < 0 && rte_errno != ENOTSUP) { 1051 rte_eal_init_alert("failed to init mp channel"); 1052 if (rte_eal_process_type() == RTE_PROC_PRIMARY) { 1053 rte_errno = EFAULT; 1054 return -1; 1055 } 1056 } 1057 1058 if (rte_bus_scan()) { 1059 rte_eal_init_alert("Cannot scan the buses for devices"); 1060 rte_errno = ENODEV; 1061 rte_atomic_store_explicit(&run_once, 0, rte_memory_order_relaxed); 1062 return -1; 1063 } 1064 1065 phys_addrs = rte_eal_using_phys_addrs() != 0; 1066 1067 /* if no EAL option "--iova-mode=<pa|va>", use bus IOVA scheme */ 1068 if (internal_conf->iova_mode == RTE_IOVA_DC) { 1069 /* autodetect the IOVA mapping mode */ 1070 enum rte_iova_mode iova_mode = rte_bus_get_iommu_class(); 1071 1072 if (iova_mode == RTE_IOVA_DC) { 1073 EAL_LOG(DEBUG, "Buses did not request a specific IOVA mode."); 1074 1075 if (!RTE_IOVA_IN_MBUF) { 1076 iova_mode = RTE_IOVA_VA; 1077 EAL_LOG(DEBUG, "IOVA as VA mode is forced by build option."); 1078 } else if (!phys_addrs) { 1079 /* if we have no access to physical addresses, 1080 * pick IOVA as VA mode. 1081 */ 1082 iova_mode = RTE_IOVA_VA; 1083 EAL_LOG(DEBUG, "Physical addresses are unavailable, selecting IOVA as VA mode."); 1084 } else if (is_iommu_enabled()) { 1085 /* we have an IOMMU, pick IOVA as VA mode */ 1086 iova_mode = RTE_IOVA_VA; 1087 EAL_LOG(DEBUG, "IOMMU is available, selecting IOVA as VA mode."); 1088 } else { 1089 /* physical addresses available, and no IOMMU 1090 * found, so pick IOVA as PA. 1091 */ 1092 iova_mode = RTE_IOVA_PA; 1093 EAL_LOG(DEBUG, "IOMMU is not available, selecting IOVA as PA mode."); 1094 } 1095 } 1096 rte_eal_get_configuration()->iova_mode = iova_mode; 1097 } else { 1098 rte_eal_get_configuration()->iova_mode = 1099 internal_conf->iova_mode; 1100 } 1101 1102 if (rte_eal_iova_mode() == RTE_IOVA_PA && !phys_addrs) { 1103 rte_eal_init_alert("Cannot use IOVA as 'PA' since physical addresses are not available"); 1104 rte_errno = EINVAL; 1105 return -1; 1106 } 1107 1108 if (rte_eal_iova_mode() == RTE_IOVA_PA && !RTE_IOVA_IN_MBUF) { 1109 rte_eal_init_alert("Cannot use IOVA as 'PA' as it is disabled during build"); 1110 rte_errno = EINVAL; 1111 return -1; 1112 } 1113 1114 EAL_LOG(INFO, "Selected IOVA mode '%s'", 1115 rte_eal_iova_mode() == RTE_IOVA_PA ? "PA" : "VA"); 1116 1117 if (internal_conf->no_hugetlbfs == 0) { 1118 /* rte_config isn't initialized yet */ 1119 ret = internal_conf->process_type == RTE_PROC_PRIMARY ? 1120 eal_hugepage_info_init() : 1121 eal_hugepage_info_read(); 1122 if (ret < 0) { 1123 rte_eal_init_alert("Cannot get hugepage information."); 1124 rte_errno = EACCES; 1125 rte_atomic_store_explicit(&run_once, 0, rte_memory_order_relaxed); 1126 return -1; 1127 } 1128 } 1129 1130 if (internal_conf->memory == 0 && internal_conf->force_sockets == 0) { 1131 if (internal_conf->no_hugetlbfs) 1132 internal_conf->memory = MEMSIZE_IF_NO_HUGE_PAGE; 1133 } 1134 1135 if (internal_conf->vmware_tsc_map == 1) { 1136 #ifdef RTE_LIBRTE_EAL_VMWARE_TSC_MAP_SUPPORT 1137 rte_cycles_vmware_tsc_map = 1; 1138 EAL_LOG(DEBUG, "Using VMWARE TSC MAP, " 1139 "you must have monitor_control.pseudo_perfctr = TRUE"); 1140 #else 1141 EAL_LOG(WARNING, "Ignoring --vmware-tsc-map because " 1142 "RTE_LIBRTE_EAL_VMWARE_TSC_MAP_SUPPORT is not set"); 1143 #endif 1144 } 1145 1146 if (eal_log_init(program_invocation_short_name, 1147 internal_conf->syslog_facility) < 0) { 1148 rte_eal_init_alert("Cannot init logging."); 1149 rte_errno = ENOMEM; 1150 rte_atomic_store_explicit(&run_once, 0, rte_memory_order_relaxed); 1151 return -1; 1152 } 1153 1154 #ifdef VFIO_PRESENT 1155 if (rte_vfio_enable("vfio")) { 1156 rte_eal_init_alert("Cannot init VFIO"); 1157 rte_errno = EAGAIN; 1158 rte_atomic_store_explicit(&run_once, 0, rte_memory_order_relaxed); 1159 return -1; 1160 } 1161 #endif 1162 /* in secondary processes, memory init may allocate additional fbarrays 1163 * not present in primary processes, so to avoid any potential issues, 1164 * initialize memzones first. 1165 */ 1166 if (rte_eal_memzone_init() < 0) { 1167 rte_eal_init_alert("Cannot init memzone"); 1168 rte_errno = ENODEV; 1169 return -1; 1170 } 1171 1172 rte_mcfg_mem_read_lock(); 1173 1174 if (rte_eal_memory_init() < 0) { 1175 rte_mcfg_mem_read_unlock(); 1176 rte_eal_init_alert("Cannot init memory"); 1177 rte_errno = ENOMEM; 1178 return -1; 1179 } 1180 1181 /* the directories are locked during eal_hugepage_info_init */ 1182 eal_hugedirs_unlock(); 1183 1184 if (rte_eal_malloc_heap_init() < 0) { 1185 rte_mcfg_mem_read_unlock(); 1186 rte_eal_init_alert("Cannot init malloc heap"); 1187 rte_errno = ENODEV; 1188 return -1; 1189 } 1190 1191 rte_mcfg_mem_read_unlock(); 1192 1193 if (rte_eal_malloc_heap_populate() < 0) { 1194 rte_eal_init_alert("Cannot init malloc heap"); 1195 rte_errno = ENODEV; 1196 return -1; 1197 } 1198 1199 /* register multi-process action callbacks for hotplug after memory init */ 1200 if (eal_mp_dev_hotplug_init() < 0) { 1201 rte_eal_init_alert("failed to register mp callback for hotplug"); 1202 return -1; 1203 } 1204 1205 if (rte_eal_tailqs_init() < 0) { 1206 rte_eal_init_alert("Cannot init tail queues for objects"); 1207 rte_errno = EFAULT; 1208 return -1; 1209 } 1210 1211 if (rte_eal_timer_init() < 0) { 1212 rte_eal_init_alert("Cannot init HPET or TSC timers"); 1213 rte_errno = ENOTSUP; 1214 return -1; 1215 } 1216 1217 eal_check_mem_on_local_socket(); 1218 1219 if (rte_thread_set_affinity_by_id(rte_thread_self(), 1220 &lcore_config[config->main_lcore].cpuset) != 0) { 1221 rte_eal_init_alert("Cannot set affinity"); 1222 rte_errno = EINVAL; 1223 return -1; 1224 } 1225 __rte_thread_init(config->main_lcore, 1226 &lcore_config[config->main_lcore].cpuset); 1227 1228 ret = eal_thread_dump_current_affinity(cpuset, sizeof(cpuset)); 1229 EAL_LOG(DEBUG, "Main lcore %u is ready (tid=%zx;cpuset=[%s%s])", 1230 config->main_lcore, (uintptr_t)pthread_self(), cpuset, 1231 ret == 0 ? "" : "..."); 1232 1233 RTE_LCORE_FOREACH_WORKER(i) { 1234 1235 /* 1236 * create communication pipes between main thread 1237 * and children 1238 */ 1239 if (pipe(lcore_config[i].pipe_main2worker) < 0) 1240 rte_panic("Cannot create pipe\n"); 1241 if (pipe(lcore_config[i].pipe_worker2main) < 0) 1242 rte_panic("Cannot create pipe\n"); 1243 1244 lcore_config[i].state = WAIT; 1245 1246 /* create a thread for each lcore */ 1247 ret = eal_worker_thread_create(i); 1248 if (ret != 0) 1249 rte_panic("Cannot create thread\n"); 1250 1251 /* Set thread_name for aid in debugging. */ 1252 snprintf(thread_name, sizeof(thread_name), 1253 "dpdk-worker%d", i); 1254 rte_thread_set_name(lcore_config[i].thread_id, thread_name); 1255 1256 ret = rte_thread_set_affinity_by_id(lcore_config[i].thread_id, 1257 &lcore_config[i].cpuset); 1258 if (ret != 0) 1259 rte_panic("Cannot set affinity\n"); 1260 } 1261 1262 /* 1263 * Launch a dummy function on all worker lcores, so that main lcore 1264 * knows they are all ready when this function returns. 1265 */ 1266 rte_eal_mp_remote_launch(sync_func, NULL, SKIP_MAIN); 1267 rte_eal_mp_wait_lcore(); 1268 1269 /* initialize services so vdevs register service during bus_probe. */ 1270 ret = rte_service_init(); 1271 if (ret) { 1272 rte_eal_init_alert("rte_service_init() failed"); 1273 rte_errno = -ret; 1274 return -1; 1275 } 1276 1277 /* Probe all the buses and devices/drivers on them */ 1278 if (rte_bus_probe()) { 1279 rte_eal_init_alert("Cannot probe devices"); 1280 rte_errno = ENOTSUP; 1281 return -1; 1282 } 1283 1284 /* initialize default service/lcore mappings and start running. Ignore 1285 * -ENOTSUP, as it indicates no service coremask passed to EAL. 1286 */ 1287 ret = rte_service_start_with_defaults(); 1288 if (ret < 0 && ret != -ENOTSUP) { 1289 rte_errno = -ret; 1290 return -1; 1291 } 1292 1293 /* 1294 * Clean up unused files in runtime directory. We do this at the end of 1295 * init and not at the beginning because we want to clean stuff up 1296 * whether we are primary or secondary process, but we cannot remove 1297 * primary process' files because secondary should be able to run even 1298 * if primary process is dead. 1299 * 1300 * In no_shconf mode, no runtime directory is created in the first 1301 * place, so no cleanup needed. 1302 */ 1303 if (!internal_conf->no_shconf && eal_clean_runtime_dir() < 0) { 1304 rte_eal_init_alert("Cannot clear runtime directory"); 1305 return -1; 1306 } 1307 if (rte_eal_process_type() == RTE_PROC_PRIMARY && !internal_conf->no_telemetry) { 1308 if (rte_telemetry_init(rte_eal_get_runtime_dir(), 1309 rte_version(), 1310 &internal_conf->ctrl_cpuset) != 0) 1311 return -1; 1312 } 1313 1314 eal_mcfg_complete(); 1315 1316 return fctret; 1317 } 1318 1319 static int 1320 mark_freeable(const struct rte_memseg_list *msl, const struct rte_memseg *ms, 1321 void *arg __rte_unused) 1322 { 1323 /* ms is const, so find this memseg */ 1324 struct rte_memseg *found; 1325 1326 if (msl->external) 1327 return 0; 1328 1329 found = rte_mem_virt2memseg(ms->addr, msl); 1330 1331 found->flags &= ~RTE_MEMSEG_FLAG_DO_NOT_FREE; 1332 1333 return 0; 1334 } 1335 1336 int 1337 rte_eal_cleanup(void) 1338 { 1339 static RTE_ATOMIC(uint32_t) run_once; 1340 uint32_t has_run = 0; 1341 1342 if (!rte_atomic_compare_exchange_strong_explicit(&run_once, &has_run, 1, 1343 rte_memory_order_relaxed, rte_memory_order_relaxed)) { 1344 EAL_LOG(WARNING, "Already called cleanup"); 1345 rte_errno = EALREADY; 1346 return -1; 1347 } 1348 1349 /* if we're in a primary process, we need to mark hugepages as freeable 1350 * so that finalization can release them back to the system. 1351 */ 1352 struct internal_config *internal_conf = 1353 eal_get_internal_configuration(); 1354 1355 if (rte_eal_process_type() == RTE_PROC_PRIMARY && 1356 internal_conf->hugepage_file.unlink_existing) 1357 rte_memseg_walk(mark_freeable, NULL); 1358 1359 rte_service_finalize(); 1360 #ifdef VFIO_PRESENT 1361 vfio_mp_sync_cleanup(); 1362 #endif 1363 rte_mp_channel_cleanup(); 1364 eal_bus_cleanup(); 1365 rte_trace_save(); 1366 eal_trace_fini(); 1367 eal_mp_dev_hotplug_cleanup(); 1368 rte_eal_alarm_cleanup(); 1369 /* after this point, any DPDK pointers will become dangling */ 1370 rte_eal_memory_detach(); 1371 rte_eal_malloc_heap_cleanup(); 1372 eal_cleanup_config(internal_conf); 1373 rte_eal_log_cleanup(); 1374 return 0; 1375 } 1376 1377 int rte_eal_create_uio_dev(void) 1378 { 1379 const struct internal_config *internal_conf = 1380 eal_get_internal_configuration(); 1381 1382 return internal_conf->create_uio_dev; 1383 } 1384 1385 enum rte_intr_mode 1386 rte_eal_vfio_intr_mode(void) 1387 { 1388 const struct internal_config *internal_conf = 1389 eal_get_internal_configuration(); 1390 1391 return internal_conf->vfio_intr_mode; 1392 } 1393 1394 void 1395 rte_eal_vfio_get_vf_token(rte_uuid_t vf_token) 1396 { 1397 struct internal_config *cfg = eal_get_internal_configuration(); 1398 1399 rte_uuid_copy(vf_token, cfg->vfio_vf_token); 1400 } 1401 1402 int 1403 rte_eal_check_module(const char *module_name) 1404 { 1405 char sysfs_mod_name[PATH_MAX]; 1406 struct stat st; 1407 int n; 1408 1409 if (NULL == module_name) 1410 return -1; 1411 1412 /* Check if there is sysfs mounted */ 1413 if (stat("/sys/module", &st) != 0) { 1414 EAL_LOG(DEBUG, "sysfs is not mounted! error %i (%s)", 1415 errno, strerror(errno)); 1416 return -1; 1417 } 1418 1419 /* A module might be built-in, therefore try sysfs */ 1420 n = snprintf(sysfs_mod_name, PATH_MAX, "/sys/module/%s", module_name); 1421 if (n < 0 || n > PATH_MAX) { 1422 EAL_LOG(DEBUG, "Could not format module path"); 1423 return -1; 1424 } 1425 1426 if (stat(sysfs_mod_name, &st) != 0) { 1427 EAL_LOG(DEBUG, "Module %s not found! error %i (%s)", 1428 sysfs_mod_name, errno, strerror(errno)); 1429 return 0; 1430 } 1431 1432 /* Module has been found */ 1433 return 1; 1434 } 1435