1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2018 Intel Corporation.
3 * Copyright(c) 2012-2014 6WIND S.A.
23 #include <sys/queue.h>
25 #if defined(RTE_ARCH_X86)
28 #include <linux/version.h>
30 #include <rte_compat.h>
31 #include <rte_common.h>
32 #include <rte_debug.h>
33 #include <rte_memory.h>
34 #include <rte_launch.h>
36 #include <rte_errno.h>
37 #include <rte_per_lcore.h>
38 #include <rte_lcore.h>
39 #include <rte_service_component.h>
41 #include <rte_random.h>
42 #include <rte_cycles.h>
43 #include <rte_string_fns.h>
44 #include <rte_cpuflags.h>
45 #include <rte_interrupts.h>
48 #include <rte_devargs.h>
49 #include <rte_version.h>
50 #include <rte_atomic.h>
51 #include <malloc_heap.h>
53 #include <rte_option.h>
54 #include <rte_telemetry.h>
56 #include "eal_private.h"
57 #include "eal_thread.h"
58 #include "eal_internal_cfg.h"
59 #include "eal_filesystem.h"
60 #include "eal_hugepages.h"
61 #include "eal_memcfg.h"
62 #include "eal_trace.h"
63 #include "eal_options.h"
65 #include "hotplug_mp.h"
67 #define MEMSIZE_IF_NO_HUGE_PAGE (64ULL * 1024ULL * 1024ULL)
69 #define SOCKET_MEM_STRLEN (RTE_MAX_NUMA_NODES * 10)
71 #define KERNEL_IOMMU_GROUPS_PATH "/sys/kernel/iommu_groups"
73 /* Allow the application to print its usage message too if set */
74 static rte_usage_hook_t rte_application_usage_hook = NULL;
76 /* early configuration structure, when memory config is not mmapped */
77 static struct rte_mem_config early_mem_config;
79 /* define fd variable here, because file needs to be kept open for the
80 * duration of the program, as we hold a write lock on it in the primary proc */
81 static int mem_cfg_fd = -1;
83 static struct flock wr_lock = {
86 .l_start = offsetof(struct rte_mem_config, memsegs),
87 .l_len = sizeof(early_mem_config.memsegs),
90 /* Address of global and public configuration */
91 static struct rte_config rte_config = {
92 .mem_config = &early_mem_config,
95 /* internal configuration (per-core) */
96 struct lcore_config lcore_config[RTE_MAX_LCORE];
98 /* internal configuration */
99 struct internal_config internal_config;
101 /* used by rte_rdtsc() */
102 int rte_cycles_vmware_tsc_map;
104 /* platform-specific runtime dir */
105 static char runtime_dir[PATH_MAX];
107 static const char *default_runtime_dir = "/var/run";
110 eal_create_runtime_dir(void)
112 const char *directory = default_runtime_dir;
113 const char *xdg_runtime_dir = getenv("XDG_RUNTIME_DIR");
114 const char *fallback = "/tmp";
119 /* try XDG path first, fall back to /tmp */
120 if (xdg_runtime_dir != NULL)
121 directory = xdg_runtime_dir;
123 directory = fallback;
125 /* create DPDK subdirectory under runtime dir */
126 ret = snprintf(tmp, sizeof(tmp), "%s/dpdk", directory);
127 if (ret < 0 || ret == sizeof(tmp)) {
128 RTE_LOG(ERR, EAL, "Error creating DPDK runtime path name\n");
132 /* create prefix-specific subdirectory under DPDK runtime dir */
133 ret = snprintf(runtime_dir, sizeof(runtime_dir), "%s/%s",
134 tmp, eal_get_hugefile_prefix());
135 if (ret < 0 || ret == sizeof(runtime_dir)) {
136 RTE_LOG(ERR, EAL, "Error creating prefix-specific runtime path name\n");
140 /* create the path if it doesn't exist. no "mkdir -p" here, so do it
143 ret = mkdir(tmp, 0700);
144 if (ret < 0 && errno != EEXIST) {
145 RTE_LOG(ERR, EAL, "Error creating '%s': %s\n",
146 tmp, strerror(errno));
150 ret = mkdir(runtime_dir, 0700);
151 if (ret < 0 && errno != EEXIST) {
152 RTE_LOG(ERR, EAL, "Error creating '%s': %s\n",
153 runtime_dir, strerror(errno));
161 eal_clean_runtime_dir(void)
164 struct dirent *dirent;
165 int dir_fd, fd, lck_result;
166 static const char * const filters[] = {
172 dir = opendir(runtime_dir);
174 RTE_LOG(ERR, EAL, "Unable to open runtime directory %s\n",
180 /* lock the directory before doing anything, to avoid races */
181 if (flock(dir_fd, LOCK_EX) < 0) {
182 RTE_LOG(ERR, EAL, "Unable to lock runtime directory %s\n",
187 dirent = readdir(dir);
189 RTE_LOG(ERR, EAL, "Unable to read runtime directory %s\n",
194 while (dirent != NULL) {
198 /* skip files that don't match the patterns */
199 for (f_idx = 0; f_idx < RTE_DIM(filters); f_idx++) {
200 const char *filter = filters[f_idx];
202 if (fnmatch(filter, dirent->d_name, 0) == 0) {
208 dirent = readdir(dir);
212 /* try and lock the file */
213 fd = openat(dir_fd, dirent->d_name, O_RDONLY);
215 /* skip to next file */
217 dirent = readdir(dir);
221 /* non-blocking lock */
222 lck_result = flock(fd, LOCK_EX | LOCK_NB);
224 /* if lock succeeds, remove the file */
225 if (lck_result != -1)
226 unlinkat(dir_fd, dirent->d_name, 0);
228 dirent = readdir(dir);
231 /* closedir closes dir_fd and drops the lock */
239 RTE_LOG(ERR, EAL, "Error while clearing runtime dir: %s\n",
246 rte_eal_get_runtime_dir(void)
251 /* Return user provided mbuf pool ops name */
253 rte_eal_mbuf_user_pool_ops(void)
255 return internal_config.user_mbuf_pool_ops_name;
258 /* Return a pointer to the configuration structure */
260 rte_eal_get_configuration(void)
266 rte_eal_iova_mode(void)
268 return rte_eal_get_configuration()->iova_mode;
271 /* parse a sysfs (or other) file containing one integer value */
273 eal_parse_sysfs_value(const char *filename, unsigned long *val)
279 if ((f = fopen(filename, "r")) == NULL) {
280 RTE_LOG(ERR, EAL, "%s(): cannot open sysfs value %s\n",
285 if (fgets(buf, sizeof(buf), f) == NULL) {
286 RTE_LOG(ERR, EAL, "%s(): cannot read sysfs value %s\n",
291 *val = strtoul(buf, &end, 0);
292 if ((buf[0] == '\0') || (end == NULL) || (*end != '\n')) {
293 RTE_LOG(ERR, EAL, "%s(): cannot parse sysfs value %s\n",
303 /* create memory configuration in shared/mmap memory. Take out
304 * a write lock on the memsegs, so we can auto-detect primary/secondary.
305 * This means we never close the file while running (auto-close on exit).
306 * We also don't lock the whole file, so that in future we can use read-locks
307 * on other parts, e.g. memzones, to detect if there are running secondary
310 rte_eal_config_create(void)
312 size_t page_sz = sysconf(_SC_PAGE_SIZE);
313 size_t cfg_len = sizeof(*rte_config.mem_config);
314 size_t cfg_len_aligned = RTE_ALIGN(cfg_len, page_sz);
315 void *rte_mem_cfg_addr, *mapped_mem_cfg_addr;
318 const char *pathname = eal_runtime_config_path();
320 if (internal_config.no_shconf)
323 /* map the config before hugepage address so that we don't waste a page */
324 if (internal_config.base_virtaddr != 0)
325 rte_mem_cfg_addr = (void *)
326 RTE_ALIGN_FLOOR(internal_config.base_virtaddr -
327 sizeof(struct rte_mem_config), page_sz);
329 rte_mem_cfg_addr = NULL;
332 mem_cfg_fd = open(pathname, O_RDWR | O_CREAT, 0600);
333 if (mem_cfg_fd < 0) {
334 RTE_LOG(ERR, EAL, "Cannot open '%s' for rte_mem_config\n",
340 retval = ftruncate(mem_cfg_fd, cfg_len);
344 RTE_LOG(ERR, EAL, "Cannot resize '%s' for rte_mem_config\n",
349 retval = fcntl(mem_cfg_fd, F_SETLK, &wr_lock);
353 RTE_LOG(ERR, EAL, "Cannot create lock on '%s'. Is another primary "
354 "process running?\n", pathname);
358 /* reserve space for config */
359 rte_mem_cfg_addr = eal_get_virtual_area(rte_mem_cfg_addr,
360 &cfg_len_aligned, page_sz, 0, 0);
361 if (rte_mem_cfg_addr == NULL) {
362 RTE_LOG(ERR, EAL, "Cannot mmap memory for rte_config\n");
368 /* remap the actual file into the space we've just reserved */
369 mapped_mem_cfg_addr = mmap(rte_mem_cfg_addr,
370 cfg_len_aligned, PROT_READ | PROT_WRITE,
371 MAP_SHARED | MAP_FIXED, mem_cfg_fd, 0);
372 if (mapped_mem_cfg_addr == MAP_FAILED) {
373 munmap(rte_mem_cfg_addr, cfg_len);
376 RTE_LOG(ERR, EAL, "Cannot remap memory for rte_config\n");
380 memcpy(rte_mem_cfg_addr, &early_mem_config, sizeof(early_mem_config));
381 rte_config.mem_config = rte_mem_cfg_addr;
383 /* store address of the config in the config itself so that secondary
384 * processes could later map the config into this exact location */
385 rte_config.mem_config->mem_cfg_addr = (uintptr_t) rte_mem_cfg_addr;
387 rte_config.mem_config->dma_maskbits = 0;
392 /* attach to an existing shared memory config */
394 rte_eal_config_attach(void)
396 struct rte_mem_config *mem_config;
398 const char *pathname = eal_runtime_config_path();
400 if (internal_config.no_shconf)
404 mem_cfg_fd = open(pathname, O_RDWR);
405 if (mem_cfg_fd < 0) {
406 RTE_LOG(ERR, EAL, "Cannot open '%s' for rte_mem_config\n",
412 /* map it as read-only first */
413 mem_config = (struct rte_mem_config *) mmap(NULL, sizeof(*mem_config),
414 PROT_READ, MAP_SHARED, mem_cfg_fd, 0);
415 if (mem_config == MAP_FAILED) {
418 RTE_LOG(ERR, EAL, "Cannot mmap memory for rte_config! error %i (%s)\n",
419 errno, strerror(errno));
423 rte_config.mem_config = mem_config;
428 /* reattach the shared config at exact memory location primary process has it */
430 rte_eal_config_reattach(void)
432 struct rte_mem_config *mem_config;
433 void *rte_mem_cfg_addr;
435 if (internal_config.no_shconf)
438 /* save the address primary process has mapped shared config to */
439 rte_mem_cfg_addr = (void *) (uintptr_t) rte_config.mem_config->mem_cfg_addr;
441 /* unmap original config */
442 munmap(rte_config.mem_config, sizeof(struct rte_mem_config));
444 /* remap the config at proper address */
445 mem_config = (struct rte_mem_config *) mmap(rte_mem_cfg_addr,
446 sizeof(*mem_config), PROT_READ | PROT_WRITE, MAP_SHARED,
452 if (mem_config == MAP_FAILED || mem_config != rte_mem_cfg_addr) {
453 if (mem_config != MAP_FAILED) {
454 /* errno is stale, don't use */
455 RTE_LOG(ERR, EAL, "Cannot mmap memory for rte_config at [%p], got [%p]"
456 " - please use '--" OPT_BASE_VIRTADDR
457 "' option\n", rte_mem_cfg_addr, mem_config);
458 munmap(mem_config, sizeof(struct rte_mem_config));
461 RTE_LOG(ERR, EAL, "Cannot mmap memory for rte_config! error %i (%s)\n",
462 errno, strerror(errno));
466 rte_config.mem_config = mem_config;
471 /* Detect if we are a primary or a secondary process */
473 eal_proc_type_detect(void)
475 enum rte_proc_type_t ptype = RTE_PROC_PRIMARY;
476 const char *pathname = eal_runtime_config_path();
478 /* if there no shared config, there can be no secondary processes */
479 if (!internal_config.no_shconf) {
480 /* if we can open the file but not get a write-lock we are a
481 * secondary process. NOTE: if we get a file handle back, we
482 * keep that open and don't close it to prevent a race condition
483 * between multiple opens.
485 if (((mem_cfg_fd = open(pathname, O_RDWR)) >= 0) &&
486 (fcntl(mem_cfg_fd, F_SETLK, &wr_lock) < 0))
487 ptype = RTE_PROC_SECONDARY;
490 RTE_LOG(INFO, EAL, "Auto-detected process type: %s\n",
491 ptype == RTE_PROC_PRIMARY ? "PRIMARY" : "SECONDARY");
496 /* Sets up rte_config structure with the pointer to shared memory config.*/
498 rte_config_init(void)
500 rte_config.process_type = internal_config.process_type;
502 switch (rte_config.process_type){
503 case RTE_PROC_PRIMARY:
504 if (rte_eal_config_create() < 0)
506 eal_mcfg_update_from_internal();
508 case RTE_PROC_SECONDARY:
509 if (rte_eal_config_attach() < 0)
511 eal_mcfg_wait_complete();
512 if (eal_mcfg_check_version() < 0) {
513 RTE_LOG(ERR, EAL, "Primary and secondary process DPDK version mismatch\n");
516 if (rte_eal_config_reattach() < 0)
518 eal_mcfg_update_internal();
521 case RTE_PROC_INVALID:
522 RTE_LOG(ERR, EAL, "Invalid process type %d\n",
523 rte_config.process_type);
530 /* Unlocks hugepage directories that were locked by eal_hugepage_info_init */
532 eal_hugedirs_unlock(void)
536 for (i = 0; i < MAX_HUGEPAGE_SIZES; i++)
538 /* skip uninitialized */
539 if (internal_config.hugepage_info[i].lock_descriptor < 0)
541 /* unlock hugepage file */
542 flock(internal_config.hugepage_info[i].lock_descriptor, LOCK_UN);
543 close(internal_config.hugepage_info[i].lock_descriptor);
544 /* reset the field */
545 internal_config.hugepage_info[i].lock_descriptor = -1;
551 eal_usage(const char *prgname)
553 printf("\nUsage: %s ", prgname);
555 printf("EAL Linux options:\n"
556 " --"OPT_SOCKET_MEM" Memory to allocate on sockets (comma separated values)\n"
557 " --"OPT_SOCKET_LIMIT" Limit memory allocation on sockets (comma separated values)\n"
558 " --"OPT_HUGE_DIR" Directory where hugetlbfs is mounted\n"
559 " --"OPT_FILE_PREFIX" Prefix for hugepage filenames\n"
560 " --"OPT_CREATE_UIO_DEV" Create /dev/uioX (usually done by hotplug)\n"
561 " --"OPT_VFIO_INTR" Interrupt mode for VFIO (legacy|msi|msix)\n"
562 " --"OPT_LEGACY_MEM" Legacy memory mode (no dynamic allocation, contiguous segments)\n"
563 " --"OPT_SINGLE_FILE_SEGMENTS" Put all hugepage memory in single files\n"
564 " --"OPT_MATCH_ALLOCATIONS" Free hugepages exactly as allocated\n"
566 /* Allow the application to print its usage message too if hook is set */
567 if ( rte_application_usage_hook ) {
568 printf("===== Application Usage =====\n\n");
569 rte_application_usage_hook(prgname);
573 /* Set a per-application usage message */
575 rte_set_application_usage_hook( rte_usage_hook_t usage_func )
577 rte_usage_hook_t old_func;
579 /* Will be NULL on the first call to denote the last usage routine. */
580 old_func = rte_application_usage_hook;
581 rte_application_usage_hook = usage_func;
587 eal_parse_socket_arg(char *strval, volatile uint64_t *socket_arg)
589 char * arg[RTE_MAX_NUMA_NODES];
592 uint64_t total_mem = 0;
594 len = strnlen(strval, SOCKET_MEM_STRLEN);
595 if (len == SOCKET_MEM_STRLEN) {
596 RTE_LOG(ERR, EAL, "--socket-mem is too long\n");
600 /* all other error cases will be caught later */
601 if (!isdigit(strval[len-1]))
604 /* split the optarg into separate socket values */
605 arg_num = rte_strsplit(strval, len,
606 arg, RTE_MAX_NUMA_NODES, ',');
608 /* if split failed, or 0 arguments */
612 /* parse each defined socket option */
614 for (i = 0; i < arg_num; i++) {
617 val = strtoull(arg[i], &end, 10);
619 /* check for invalid input */
621 (arg[i][0] == '\0') || (end == NULL) || (*end != '\0'))
632 eal_parse_vfio_intr(const char *mode)
637 enum rte_intr_mode value;
639 { "legacy", RTE_INTR_MODE_LEGACY },
640 { "msi", RTE_INTR_MODE_MSI },
641 { "msix", RTE_INTR_MODE_MSIX },
644 for (i = 0; i < RTE_DIM(map); i++) {
645 if (!strcmp(mode, map[i].name)) {
646 internal_config.vfio_intr_mode = map[i].value;
653 /* Parse the arguments for --log-level only */
655 eal_log_level_parse(int argc, char **argv)
660 const int old_optind = optind;
661 const int old_optopt = optopt;
662 char * const old_optarg = optarg;
667 while ((opt = getopt_long(argc, argvopt, eal_short_options,
668 eal_long_options, &option_index)) != EOF) {
672 /* getopt is not happy, stop right now */
676 ret = (opt == OPT_LOG_LEVEL_NUM) ?
677 eal_parse_common_option(opt, optarg, &internal_config) : 0;
679 /* common parser is not happy */
684 /* restore getopt lib */
690 /* Parse the argument given in the command line of the application */
692 eal_parse_args(int argc, char **argv)
697 char *prgname = argv[0];
698 const int old_optind = optind;
699 const int old_optopt = optopt;
700 char * const old_optarg = optarg;
706 while ((opt = getopt_long(argc, argvopt, eal_short_options,
707 eal_long_options, &option_index)) != EOF) {
710 * getopt didn't recognise the option, lets parse the
711 * registered options to see if the flag is valid
714 ret = rte_option_parse(argv[optind-1]);
723 ret = eal_parse_common_option(opt, optarg, &internal_config);
724 /* common parser is not happy */
730 /* common parser handled this option */
739 case OPT_HUGE_DIR_NUM:
741 char *hdir = strdup(optarg);
743 RTE_LOG(ERR, EAL, "Could not store hugepage directory\n");
745 /* free old hugepage dir */
746 if (internal_config.hugepage_dir != NULL)
747 free(internal_config.hugepage_dir);
748 internal_config.hugepage_dir = hdir;
752 case OPT_FILE_PREFIX_NUM:
754 char *prefix = strdup(optarg);
756 RTE_LOG(ERR, EAL, "Could not store file prefix\n");
758 /* free old prefix */
759 if (internal_config.hugefile_prefix != NULL)
760 free(internal_config.hugefile_prefix);
761 internal_config.hugefile_prefix = prefix;
765 case OPT_SOCKET_MEM_NUM:
766 if (eal_parse_socket_arg(optarg,
767 internal_config.socket_mem) < 0) {
768 RTE_LOG(ERR, EAL, "invalid parameters for --"
769 OPT_SOCKET_MEM "\n");
774 internal_config.force_sockets = 1;
777 case OPT_SOCKET_LIMIT_NUM:
778 if (eal_parse_socket_arg(optarg,
779 internal_config.socket_limit) < 0) {
780 RTE_LOG(ERR, EAL, "invalid parameters for --"
781 OPT_SOCKET_LIMIT "\n");
786 internal_config.force_socket_limits = 1;
789 case OPT_VFIO_INTR_NUM:
790 if (eal_parse_vfio_intr(optarg) < 0) {
791 RTE_LOG(ERR, EAL, "invalid parameters for --"
799 case OPT_CREATE_UIO_DEV_NUM:
800 internal_config.create_uio_dev = 1;
803 case OPT_MBUF_POOL_OPS_NAME_NUM:
805 char *ops_name = strdup(optarg);
806 if (ops_name == NULL)
807 RTE_LOG(ERR, EAL, "Could not store mbuf pool ops name\n");
809 /* free old ops name */
810 if (internal_config.user_mbuf_pool_ops_name !=
812 free(internal_config.user_mbuf_pool_ops_name);
814 internal_config.user_mbuf_pool_ops_name =
819 case OPT_MATCH_ALLOCATIONS_NUM:
820 internal_config.match_allocations = 1;
824 if (opt < OPT_LONG_MIN_NUM && isprint(opt)) {
825 RTE_LOG(ERR, EAL, "Option %c is not supported "
827 } else if (opt >= OPT_LONG_MIN_NUM &&
828 opt < OPT_LONG_MAX_NUM) {
829 RTE_LOG(ERR, EAL, "Option %s is not supported "
831 eal_long_options[option_index].name);
833 RTE_LOG(ERR, EAL, "Option %d is not supported "
842 /* create runtime data directory */
843 if (internal_config.no_shconf == 0 &&
844 eal_create_runtime_dir() < 0) {
845 RTE_LOG(ERR, EAL, "Cannot create runtime directory\n");
850 if (eal_adjust_config(&internal_config) != 0) {
856 if (eal_check_common_options(&internal_config) != 0) {
863 argv[optind-1] = prgname;
867 /* restore getopt lib */
876 check_socket(const struct rte_memseg_list *msl, void *arg)
878 int *socket_id = arg;
883 return *socket_id == msl->socket_id;
887 eal_check_mem_on_local_socket(void)
891 socket_id = rte_lcore_to_socket_id(rte_config.master_lcore);
893 if (rte_memseg_list_walk(check_socket, &socket_id) == 0)
894 RTE_LOG(WARNING, EAL, "WARNING: Master core has no memory on local socket!\n");
898 sync_func(__rte_unused void *arg)
904 * Request iopl privilege for all RPL, returns 0 on success
905 * iopl() call is mostly for the i386 architecture. For other architectures,
906 * return -1 to indicate IO privilege can't be changed in this way.
909 rte_eal_iopl_init(void)
911 #if defined(RTE_ARCH_X86)
919 static int rte_eal_vfio_setup(void)
921 if (rte_vfio_enable("vfio"))
928 static void rte_eal_init_alert(const char *msg)
930 fprintf(stderr, "EAL: FATAL: %s\n", msg);
931 RTE_LOG(ERR, EAL, "%s\n", msg);
935 * On Linux 3.6+, even if VFIO is not loaded, whenever IOMMU is enabled in the
936 * BIOS and in the kernel, /sys/kernel/iommu_groups path will contain kernel
937 * IOMMU groups. If IOMMU is not enabled, that path would be empty.
938 * Therefore, checking if the path is empty will tell us if IOMMU is enabled.
941 is_iommu_enabled(void)
943 DIR *dir = opendir(KERNEL_IOMMU_GROUPS_PATH);
947 /* if directory doesn't exist, assume IOMMU is not enabled */
951 while ((d = readdir(dir)) != NULL) {
952 /* skip dot and dot-dot */
961 /* Launch threads, called at application init(). */
963 rte_eal_init(int argc, char **argv)
967 static rte_atomic32_t run_once = RTE_ATOMIC32_INIT(0);
969 static char logid[PATH_MAX];
970 char cpuset[RTE_CPU_AFFINITY_STR_LEN];
971 char thread_name[RTE_MAX_THREAD_NAME_LEN];
974 /* checks if the machine is adequate */
975 if (!rte_cpu_is_supported()) {
976 rte_eal_init_alert("unsupported cpu type.");
981 if (!rte_atomic32_test_and_set(&run_once)) {
982 rte_eal_init_alert("already called initialization.");
983 rte_errno = EALREADY;
987 p = strrchr(argv[0], '/');
988 strlcpy(logid, p ? p + 1 : argv[0], sizeof(logid));
989 thread_id = pthread_self();
991 eal_reset_internal_config(&internal_config);
993 /* set log level as early as possible */
994 eal_log_level_parse(argc, argv);
996 if (rte_eal_cpu_init() < 0) {
997 rte_eal_init_alert("Cannot detect lcores.");
1002 fctret = eal_parse_args(argc, argv);
1004 rte_eal_init_alert("Invalid 'command line' arguments.");
1006 rte_atomic32_clear(&run_once);
1010 if (eal_plugins_init() < 0) {
1011 rte_eal_init_alert("Cannot init plugins");
1013 rte_atomic32_clear(&run_once);
1017 if (eal_trace_init() < 0) {
1018 rte_eal_init_alert("Cannot init trace");
1023 if (eal_option_device_parse()) {
1025 rte_atomic32_clear(&run_once);
1029 if (rte_config_init() < 0) {
1030 rte_eal_init_alert("Cannot init config");
1034 if (rte_eal_intr_init() < 0) {
1035 rte_eal_init_alert("Cannot init interrupt-handling thread");
1039 if (rte_eal_alarm_init() < 0) {
1040 rte_eal_init_alert("Cannot init alarm");
1041 /* rte_eal_alarm_init sets rte_errno on failure. */
1045 /* Put mp channel init before bus scan so that we can init the vdev
1046 * bus through mp channel in the secondary process before the bus scan.
1048 if (rte_mp_channel_init() < 0 && rte_errno != ENOTSUP) {
1049 rte_eal_init_alert("failed to init mp channel");
1050 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
1056 /* register multi-process action callbacks for hotplug */
1057 if (eal_mp_dev_hotplug_init() < 0) {
1058 rte_eal_init_alert("failed to register mp callback for hotplug");
1062 if (rte_bus_scan()) {
1063 rte_eal_init_alert("Cannot scan the buses for devices");
1065 rte_atomic32_clear(&run_once);
1069 phys_addrs = rte_eal_using_phys_addrs() != 0;
1071 /* if no EAL option "--iova-mode=<pa|va>", use bus IOVA scheme */
1072 if (internal_config.iova_mode == RTE_IOVA_DC) {
1073 /* autodetect the IOVA mapping mode */
1074 enum rte_iova_mode iova_mode = rte_bus_get_iommu_class();
1076 if (iova_mode == RTE_IOVA_DC) {
1077 RTE_LOG(DEBUG, EAL, "Buses did not request a specific IOVA mode.\n");
1080 /* if we have no access to physical addresses,
1081 * pick IOVA as VA mode.
1083 iova_mode = RTE_IOVA_VA;
1084 RTE_LOG(DEBUG, EAL, "Physical addresses are unavailable, selecting IOVA as VA mode.\n");
1085 #if defined(RTE_LIBRTE_KNI) && LINUX_VERSION_CODE >= KERNEL_VERSION(4, 10, 0)
1086 } else if (rte_eal_check_module("rte_kni") == 1) {
1087 iova_mode = RTE_IOVA_PA;
1088 RTE_LOG(DEBUG, EAL, "KNI is loaded, selecting IOVA as PA mode for better KNI performance.\n");
1090 } else if (is_iommu_enabled()) {
1091 /* we have an IOMMU, pick IOVA as VA mode */
1092 iova_mode = RTE_IOVA_VA;
1093 RTE_LOG(DEBUG, EAL, "IOMMU is available, selecting IOVA as VA mode.\n");
1095 /* physical addresses available, and no IOMMU
1096 * found, so pick IOVA as PA.
1098 iova_mode = RTE_IOVA_PA;
1099 RTE_LOG(DEBUG, EAL, "IOMMU is not available, selecting IOVA as PA mode.\n");
1102 #if defined(RTE_LIBRTE_KNI) && LINUX_VERSION_CODE < KERNEL_VERSION(4, 10, 0)
1103 /* Workaround for KNI which requires physical address to work
1106 if (iova_mode == RTE_IOVA_VA &&
1107 rte_eal_check_module("rte_kni") == 1) {
1109 iova_mode = RTE_IOVA_PA;
1110 RTE_LOG(WARNING, EAL, "Forcing IOVA as 'PA' because KNI module is loaded\n");
1112 RTE_LOG(DEBUG, EAL, "KNI can not work since physical addresses are unavailable\n");
1116 rte_eal_get_configuration()->iova_mode = iova_mode;
1118 rte_eal_get_configuration()->iova_mode =
1119 internal_config.iova_mode;
1122 if (rte_eal_iova_mode() == RTE_IOVA_PA && !phys_addrs) {
1123 rte_eal_init_alert("Cannot use IOVA as 'PA' since physical addresses are not available");
1128 RTE_LOG(INFO, EAL, "Selected IOVA mode '%s'\n",
1129 rte_eal_iova_mode() == RTE_IOVA_PA ? "PA" : "VA");
1131 if (internal_config.no_hugetlbfs == 0) {
1132 /* rte_config isn't initialized yet */
1133 ret = internal_config.process_type == RTE_PROC_PRIMARY ?
1134 eal_hugepage_info_init() :
1135 eal_hugepage_info_read();
1137 rte_eal_init_alert("Cannot get hugepage information.");
1139 rte_atomic32_clear(&run_once);
1144 if (internal_config.memory == 0 && internal_config.force_sockets == 0) {
1145 if (internal_config.no_hugetlbfs)
1146 internal_config.memory = MEMSIZE_IF_NO_HUGE_PAGE;
1149 if (internal_config.vmware_tsc_map == 1) {
1150 #ifdef RTE_LIBRTE_EAL_VMWARE_TSC_MAP_SUPPORT
1151 rte_cycles_vmware_tsc_map = 1;
1152 RTE_LOG (DEBUG, EAL, "Using VMWARE TSC MAP, "
1153 "you must have monitor_control.pseudo_perfctr = TRUE\n");
1155 RTE_LOG (WARNING, EAL, "Ignoring --vmware-tsc-map because "
1156 "RTE_LIBRTE_EAL_VMWARE_TSC_MAP_SUPPORT is not set\n");
1160 if (rte_eal_log_init(logid, internal_config.syslog_facility) < 0) {
1161 rte_eal_init_alert("Cannot init logging.");
1163 rte_atomic32_clear(&run_once);
1168 if (rte_eal_vfio_setup() < 0) {
1169 rte_eal_init_alert("Cannot init VFIO");
1171 rte_atomic32_clear(&run_once);
1175 /* in secondary processes, memory init may allocate additional fbarrays
1176 * not present in primary processes, so to avoid any potential issues,
1177 * initialize memzones first.
1179 if (rte_eal_memzone_init() < 0) {
1180 rte_eal_init_alert("Cannot init memzone");
1185 if (rte_eal_memory_init() < 0) {
1186 rte_eal_init_alert("Cannot init memory");
1191 /* the directories are locked during eal_hugepage_info_init */
1192 eal_hugedirs_unlock();
1194 if (rte_eal_malloc_heap_init() < 0) {
1195 rte_eal_init_alert("Cannot init malloc heap");
1200 if (rte_eal_tailqs_init() < 0) {
1201 rte_eal_init_alert("Cannot init tail queues for objects");
1206 if (rte_eal_timer_init() < 0) {
1207 rte_eal_init_alert("Cannot init HPET or TSC timers");
1208 rte_errno = ENOTSUP;
1212 eal_check_mem_on_local_socket();
1214 eal_thread_init_master(rte_config.master_lcore);
1216 ret = eal_thread_dump_affinity(cpuset, sizeof(cpuset));
1218 RTE_LOG(DEBUG, EAL, "Master lcore %u is ready (tid=%zx;cpuset=[%s%s])\n",
1219 rte_config.master_lcore, (uintptr_t)thread_id, cpuset,
1220 ret == 0 ? "" : "...");
1222 RTE_LCORE_FOREACH_SLAVE(i) {
1225 * create communication pipes between master thread
1228 if (pipe(lcore_config[i].pipe_master2slave) < 0)
1229 rte_panic("Cannot create pipe\n");
1230 if (pipe(lcore_config[i].pipe_slave2master) < 0)
1231 rte_panic("Cannot create pipe\n");
1233 lcore_config[i].state = WAIT;
1235 /* create a thread for each lcore */
1236 ret = pthread_create(&lcore_config[i].thread_id, NULL,
1237 eal_thread_loop, NULL);
1239 rte_panic("Cannot create thread\n");
1241 /* Set thread_name for aid in debugging. */
1242 snprintf(thread_name, sizeof(thread_name),
1243 "lcore-slave-%d", i);
1244 ret = rte_thread_setname(lcore_config[i].thread_id,
1248 "Cannot set name for lcore thread\n");
1252 * Launch a dummy function on all slave lcores, so that master lcore
1253 * knows they are all ready when this function returns.
1255 rte_eal_mp_remote_launch(sync_func, NULL, SKIP_MASTER);
1256 rte_eal_mp_wait_lcore();
1258 /* initialize services so vdevs register service during bus_probe. */
1259 ret = rte_service_init();
1261 rte_eal_init_alert("rte_service_init() failed");
1262 rte_errno = ENOEXEC;
1266 /* Probe all the buses and devices/drivers on them */
1267 if (rte_bus_probe()) {
1268 rte_eal_init_alert("Cannot probe devices");
1269 rte_errno = ENOTSUP;
1274 /* Register mp action after probe() so that we got enough info */
1275 if (rte_vfio_is_enabled("vfio") && vfio_mp_sync_setup() < 0)
1279 /* initialize default service/lcore mappings and start running. Ignore
1280 * -ENOTSUP, as it indicates no service coremask passed to EAL.
1282 ret = rte_service_start_with_defaults();
1283 if (ret < 0 && ret != -ENOTSUP) {
1284 rte_errno = ENOEXEC;
1289 * Clean up unused files in runtime directory. We do this at the end of
1290 * init and not at the beginning because we want to clean stuff up
1291 * whether we are primary or secondary process, but we cannot remove
1292 * primary process' files because secondary should be able to run even
1293 * if primary process is dead.
1295 * In no_shconf mode, no runtime directory is created in the first
1296 * place, so no cleanup needed.
1298 if (!internal_config.no_shconf && eal_clean_runtime_dir() < 0) {
1299 rte_eal_init_alert("Cannot clear runtime directory\n");
1302 if (!internal_config.no_telemetry) {
1303 const char *error_str;
1304 if (rte_telemetry_init(rte_eal_get_runtime_dir(),
1306 rte_eal_init_alert(error_str);
1311 eal_mcfg_complete();
1313 /* Call each registered callback, if enabled */
1320 mark_freeable(const struct rte_memseg_list *msl, const struct rte_memseg *ms,
1321 void *arg __rte_unused)
1323 /* ms is const, so find this memseg */
1324 struct rte_memseg *found;
1329 found = rte_mem_virt2memseg(ms->addr, msl);
1331 found->flags &= ~RTE_MEMSEG_FLAG_DO_NOT_FREE;
1337 rte_eal_cleanup(void)
1339 /* if we're in a primary process, we need to mark hugepages as freeable
1340 * so that finalization can release them back to the system.
1342 if (rte_eal_process_type() == RTE_PROC_PRIMARY)
1343 rte_memseg_walk(mark_freeable, NULL);
1344 rte_service_finalize();
1345 rte_mp_channel_cleanup();
1348 eal_cleanup_config(&internal_config);
1352 enum rte_proc_type_t
1353 rte_eal_process_type(void)
1355 return rte_config.process_type;
1358 int rte_eal_has_hugepages(void)
1360 return ! internal_config.no_hugetlbfs;
1363 int rte_eal_has_pci(void)
1365 return !internal_config.no_pci;
1368 int rte_eal_create_uio_dev(void)
1370 return internal_config.create_uio_dev;
1374 rte_eal_vfio_intr_mode(void)
1376 return internal_config.vfio_intr_mode;
1380 rte_eal_check_module(const char *module_name)
1382 char sysfs_mod_name[PATH_MAX];
1386 if (NULL == module_name)
1389 /* Check if there is sysfs mounted */
1390 if (stat("/sys/module", &st) != 0) {
1391 RTE_LOG(DEBUG, EAL, "sysfs is not mounted! error %i (%s)\n",
1392 errno, strerror(errno));
1396 /* A module might be built-in, therefore try sysfs */
1397 n = snprintf(sysfs_mod_name, PATH_MAX, "/sys/module/%s", module_name);
1398 if (n < 0 || n > PATH_MAX) {
1399 RTE_LOG(DEBUG, EAL, "Could not format module path\n");
1403 if (stat(sysfs_mod_name, &st) != 0) {
1404 RTE_LOG(DEBUG, EAL, "Module %s not found! error %i (%s)\n",
1405 sysfs_mod_name, errno, strerror(errno));
1409 /* Module has been found */