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>
55 #include "eal_private.h"
56 #include "eal_thread.h"
57 #include "eal_internal_cfg.h"
58 #include "eal_filesystem.h"
59 #include "eal_hugepages.h"
60 #include "eal_memcfg.h"
61 #include "eal_options.h"
63 #include "hotplug_mp.h"
65 #define MEMSIZE_IF_NO_HUGE_PAGE (64ULL * 1024ULL * 1024ULL)
67 #define SOCKET_MEM_STRLEN (RTE_MAX_NUMA_NODES * 10)
69 #define KERNEL_IOMMU_GROUPS_PATH "/sys/kernel/iommu_groups"
71 /* Allow the application to print its usage message too if set */
72 static rte_usage_hook_t rte_application_usage_hook = NULL;
74 /* early configuration structure, when memory config is not mmapped */
75 static struct rte_mem_config early_mem_config;
77 /* define fd variable here, because file needs to be kept open for the
78 * duration of the program, as we hold a write lock on it in the primary proc */
79 static int mem_cfg_fd = -1;
81 static struct flock wr_lock = {
84 .l_start = offsetof(struct rte_mem_config, memsegs),
85 .l_len = sizeof(early_mem_config.memsegs),
88 /* Address of global and public configuration */
89 static struct rte_config rte_config = {
90 .mem_config = &early_mem_config,
93 /* internal configuration (per-core) */
94 struct lcore_config lcore_config[RTE_MAX_LCORE];
96 /* internal configuration */
97 struct internal_config internal_config;
99 /* used by rte_rdtsc() */
100 int rte_cycles_vmware_tsc_map;
102 /* platform-specific runtime dir */
103 static char runtime_dir[PATH_MAX];
105 static const char *default_runtime_dir = "/var/run";
108 eal_create_runtime_dir(void)
110 const char *directory = default_runtime_dir;
111 const char *xdg_runtime_dir = getenv("XDG_RUNTIME_DIR");
112 const char *fallback = "/tmp";
117 /* try XDG path first, fall back to /tmp */
118 if (xdg_runtime_dir != NULL)
119 directory = xdg_runtime_dir;
121 directory = fallback;
123 /* create DPDK subdirectory under runtime dir */
124 ret = snprintf(tmp, sizeof(tmp), "%s/dpdk", directory);
125 if (ret < 0 || ret == sizeof(tmp)) {
126 RTE_LOG(ERR, EAL, "Error creating DPDK runtime path name\n");
130 /* create prefix-specific subdirectory under DPDK runtime dir */
131 ret = snprintf(runtime_dir, sizeof(runtime_dir), "%s/%s",
132 tmp, eal_get_hugefile_prefix());
133 if (ret < 0 || ret == sizeof(runtime_dir)) {
134 RTE_LOG(ERR, EAL, "Error creating prefix-specific runtime path name\n");
138 /* create the path if it doesn't exist. no "mkdir -p" here, so do it
141 ret = mkdir(tmp, 0700);
142 if (ret < 0 && errno != EEXIST) {
143 RTE_LOG(ERR, EAL, "Error creating '%s': %s\n",
144 tmp, strerror(errno));
148 ret = mkdir(runtime_dir, 0700);
149 if (ret < 0 && errno != EEXIST) {
150 RTE_LOG(ERR, EAL, "Error creating '%s': %s\n",
151 runtime_dir, strerror(errno));
159 eal_clean_runtime_dir(void)
162 struct dirent *dirent;
163 int dir_fd, fd, lck_result;
164 static const char * const filters[] = {
170 dir = opendir(runtime_dir);
172 RTE_LOG(ERR, EAL, "Unable to open runtime directory %s\n",
178 /* lock the directory before doing anything, to avoid races */
179 if (flock(dir_fd, LOCK_EX) < 0) {
180 RTE_LOG(ERR, EAL, "Unable to lock runtime directory %s\n",
185 dirent = readdir(dir);
187 RTE_LOG(ERR, EAL, "Unable to read runtime directory %s\n",
192 while (dirent != NULL) {
196 /* skip files that don't match the patterns */
197 for (f_idx = 0; f_idx < RTE_DIM(filters); f_idx++) {
198 const char *filter = filters[f_idx];
200 if (fnmatch(filter, dirent->d_name, 0) == 0) {
206 dirent = readdir(dir);
210 /* try and lock the file */
211 fd = openat(dir_fd, dirent->d_name, O_RDONLY);
213 /* skip to next file */
215 dirent = readdir(dir);
219 /* non-blocking lock */
220 lck_result = flock(fd, LOCK_EX | LOCK_NB);
222 /* if lock succeeds, remove the file */
223 if (lck_result != -1)
224 unlinkat(dir_fd, dirent->d_name, 0);
226 dirent = readdir(dir);
229 /* closedir closes dir_fd and drops the lock */
237 RTE_LOG(ERR, EAL, "Error while clearing runtime dir: %s\n",
244 rte_eal_get_runtime_dir(void)
249 /* Return user provided mbuf pool ops name */
251 rte_eal_mbuf_user_pool_ops(void)
253 return internal_config.user_mbuf_pool_ops_name;
256 /* Return a pointer to the configuration structure */
258 rte_eal_get_configuration(void)
264 rte_eal_iova_mode(void)
266 return rte_eal_get_configuration()->iova_mode;
269 /* parse a sysfs (or other) file containing one integer value */
271 eal_parse_sysfs_value(const char *filename, unsigned long *val)
277 if ((f = fopen(filename, "r")) == NULL) {
278 RTE_LOG(ERR, EAL, "%s(): cannot open sysfs value %s\n",
283 if (fgets(buf, sizeof(buf), f) == NULL) {
284 RTE_LOG(ERR, EAL, "%s(): cannot read sysfs value %s\n",
289 *val = strtoul(buf, &end, 0);
290 if ((buf[0] == '\0') || (end == NULL) || (*end != '\n')) {
291 RTE_LOG(ERR, EAL, "%s(): cannot parse sysfs value %s\n",
301 /* create memory configuration in shared/mmap memory. Take out
302 * a write lock on the memsegs, so we can auto-detect primary/secondary.
303 * This means we never close the file while running (auto-close on exit).
304 * We also don't lock the whole file, so that in future we can use read-locks
305 * on other parts, e.g. memzones, to detect if there are running secondary
308 rte_eal_config_create(void)
310 size_t page_sz = sysconf(_SC_PAGE_SIZE);
311 size_t cfg_len = sizeof(*rte_config.mem_config);
312 size_t cfg_len_aligned = RTE_ALIGN(cfg_len, page_sz);
313 void *rte_mem_cfg_addr, *mapped_mem_cfg_addr;
316 const char *pathname = eal_runtime_config_path();
318 if (internal_config.no_shconf)
321 /* map the config before hugepage address so that we don't waste a page */
322 if (internal_config.base_virtaddr != 0)
323 rte_mem_cfg_addr = (void *)
324 RTE_ALIGN_FLOOR(internal_config.base_virtaddr -
325 sizeof(struct rte_mem_config), page_sz);
327 rte_mem_cfg_addr = NULL;
330 mem_cfg_fd = open(pathname, O_RDWR | O_CREAT, 0600);
331 if (mem_cfg_fd < 0) {
332 RTE_LOG(ERR, EAL, "Cannot open '%s' for rte_mem_config\n",
338 retval = ftruncate(mem_cfg_fd, cfg_len);
342 RTE_LOG(ERR, EAL, "Cannot resize '%s' for rte_mem_config\n",
347 retval = fcntl(mem_cfg_fd, F_SETLK, &wr_lock);
351 RTE_LOG(ERR, EAL, "Cannot create lock on '%s'. Is another primary "
352 "process running?\n", pathname);
356 /* reserve space for config */
357 rte_mem_cfg_addr = eal_get_virtual_area(rte_mem_cfg_addr,
358 &cfg_len_aligned, page_sz, 0, 0);
359 if (rte_mem_cfg_addr == NULL) {
360 RTE_LOG(ERR, EAL, "Cannot mmap memory for rte_config\n");
366 /* remap the actual file into the space we've just reserved */
367 mapped_mem_cfg_addr = mmap(rte_mem_cfg_addr,
368 cfg_len_aligned, PROT_READ | PROT_WRITE,
369 MAP_SHARED | MAP_FIXED, mem_cfg_fd, 0);
370 if (mapped_mem_cfg_addr == MAP_FAILED) {
371 munmap(rte_mem_cfg_addr, cfg_len);
374 RTE_LOG(ERR, EAL, "Cannot remap memory for rte_config\n");
378 memcpy(rte_mem_cfg_addr, &early_mem_config, sizeof(early_mem_config));
379 rte_config.mem_config = rte_mem_cfg_addr;
381 /* store address of the config in the config itself so that secondary
382 * processes could later map the config into this exact location */
383 rte_config.mem_config->mem_cfg_addr = (uintptr_t) rte_mem_cfg_addr;
385 rte_config.mem_config->dma_maskbits = 0;
390 /* attach to an existing shared memory config */
392 rte_eal_config_attach(void)
394 struct rte_mem_config *mem_config;
396 const char *pathname = eal_runtime_config_path();
398 if (internal_config.no_shconf)
402 mem_cfg_fd = open(pathname, O_RDWR);
403 if (mem_cfg_fd < 0) {
404 RTE_LOG(ERR, EAL, "Cannot open '%s' for rte_mem_config\n",
410 /* map it as read-only first */
411 mem_config = (struct rte_mem_config *) mmap(NULL, sizeof(*mem_config),
412 PROT_READ, MAP_SHARED, mem_cfg_fd, 0);
413 if (mem_config == MAP_FAILED) {
416 RTE_LOG(ERR, EAL, "Cannot mmap memory for rte_config! error %i (%s)\n",
417 errno, strerror(errno));
421 rte_config.mem_config = mem_config;
426 /* reattach the shared config at exact memory location primary process has it */
428 rte_eal_config_reattach(void)
430 struct rte_mem_config *mem_config;
431 void *rte_mem_cfg_addr;
433 if (internal_config.no_shconf)
436 /* save the address primary process has mapped shared config to */
437 rte_mem_cfg_addr = (void *) (uintptr_t) rte_config.mem_config->mem_cfg_addr;
439 /* unmap original config */
440 munmap(rte_config.mem_config, sizeof(struct rte_mem_config));
442 /* remap the config at proper address */
443 mem_config = (struct rte_mem_config *) mmap(rte_mem_cfg_addr,
444 sizeof(*mem_config), PROT_READ | PROT_WRITE, MAP_SHARED,
450 if (mem_config == MAP_FAILED || mem_config != rte_mem_cfg_addr) {
451 if (mem_config != MAP_FAILED) {
452 /* errno is stale, don't use */
453 RTE_LOG(ERR, EAL, "Cannot mmap memory for rte_config at [%p], got [%p]"
454 " - please use '--" OPT_BASE_VIRTADDR
455 "' option\n", rte_mem_cfg_addr, mem_config);
456 munmap(mem_config, sizeof(struct rte_mem_config));
459 RTE_LOG(ERR, EAL, "Cannot mmap memory for rte_config! error %i (%s)\n",
460 errno, strerror(errno));
464 rte_config.mem_config = mem_config;
469 /* Detect if we are a primary or a secondary process */
471 eal_proc_type_detect(void)
473 enum rte_proc_type_t ptype = RTE_PROC_PRIMARY;
474 const char *pathname = eal_runtime_config_path();
476 /* if there no shared config, there can be no secondary processes */
477 if (!internal_config.no_shconf) {
478 /* if we can open the file but not get a write-lock we are a
479 * secondary process. NOTE: if we get a file handle back, we
480 * keep that open and don't close it to prevent a race condition
481 * between multiple opens.
483 if (((mem_cfg_fd = open(pathname, O_RDWR)) >= 0) &&
484 (fcntl(mem_cfg_fd, F_SETLK, &wr_lock) < 0))
485 ptype = RTE_PROC_SECONDARY;
488 RTE_LOG(INFO, EAL, "Auto-detected process type: %s\n",
489 ptype == RTE_PROC_PRIMARY ? "PRIMARY" : "SECONDARY");
494 /* Sets up rte_config structure with the pointer to shared memory config.*/
496 rte_config_init(void)
498 rte_config.process_type = internal_config.process_type;
500 switch (rte_config.process_type){
501 case RTE_PROC_PRIMARY:
502 if (rte_eal_config_create() < 0)
504 eal_mcfg_update_from_internal();
506 case RTE_PROC_SECONDARY:
507 if (rte_eal_config_attach() < 0)
509 eal_mcfg_wait_complete();
510 if (eal_mcfg_check_version() < 0) {
511 RTE_LOG(ERR, EAL, "Primary and secondary process DPDK version mismatch\n");
514 if (rte_eal_config_reattach() < 0)
516 eal_mcfg_update_internal();
519 case RTE_PROC_INVALID:
520 RTE_LOG(ERR, EAL, "Invalid process type %d\n",
521 rte_config.process_type);
528 /* Unlocks hugepage directories that were locked by eal_hugepage_info_init */
530 eal_hugedirs_unlock(void)
534 for (i = 0; i < MAX_HUGEPAGE_SIZES; i++)
536 /* skip uninitialized */
537 if (internal_config.hugepage_info[i].lock_descriptor < 0)
539 /* unlock hugepage file */
540 flock(internal_config.hugepage_info[i].lock_descriptor, LOCK_UN);
541 close(internal_config.hugepage_info[i].lock_descriptor);
542 /* reset the field */
543 internal_config.hugepage_info[i].lock_descriptor = -1;
549 eal_usage(const char *prgname)
551 printf("\nUsage: %s ", prgname);
553 printf("EAL Linux options:\n"
554 " --"OPT_SOCKET_MEM" Memory to allocate on sockets (comma separated values)\n"
555 " --"OPT_SOCKET_LIMIT" Limit memory allocation on sockets (comma separated values)\n"
556 " --"OPT_HUGE_DIR" Directory where hugetlbfs is mounted\n"
557 " --"OPT_FILE_PREFIX" Prefix for hugepage filenames\n"
558 " --"OPT_CREATE_UIO_DEV" Create /dev/uioX (usually done by hotplug)\n"
559 " --"OPT_VFIO_INTR" Interrupt mode for VFIO (legacy|msi|msix)\n"
560 " --"OPT_LEGACY_MEM" Legacy memory mode (no dynamic allocation, contiguous segments)\n"
561 " --"OPT_SINGLE_FILE_SEGMENTS" Put all hugepage memory in single files\n"
562 " --"OPT_MATCH_ALLOCATIONS" Free hugepages exactly as allocated\n"
564 /* Allow the application to print its usage message too if hook is set */
565 if ( rte_application_usage_hook ) {
566 printf("===== Application Usage =====\n\n");
567 rte_application_usage_hook(prgname);
571 /* Set a per-application usage message */
573 rte_set_application_usage_hook( rte_usage_hook_t usage_func )
575 rte_usage_hook_t old_func;
577 /* Will be NULL on the first call to denote the last usage routine. */
578 old_func = rte_application_usage_hook;
579 rte_application_usage_hook = usage_func;
585 eal_parse_socket_arg(char *strval, volatile uint64_t *socket_arg)
587 char * arg[RTE_MAX_NUMA_NODES];
590 uint64_t total_mem = 0;
592 len = strnlen(strval, SOCKET_MEM_STRLEN);
593 if (len == SOCKET_MEM_STRLEN) {
594 RTE_LOG(ERR, EAL, "--socket-mem is too long\n");
598 /* all other error cases will be caught later */
599 if (!isdigit(strval[len-1]))
602 /* split the optarg into separate socket values */
603 arg_num = rte_strsplit(strval, len,
604 arg, RTE_MAX_NUMA_NODES, ',');
606 /* if split failed, or 0 arguments */
610 /* parse each defined socket option */
612 for (i = 0; i < arg_num; i++) {
615 val = strtoull(arg[i], &end, 10);
617 /* check for invalid input */
619 (arg[i][0] == '\0') || (end == NULL) || (*end != '\0'))
630 eal_parse_vfio_intr(const char *mode)
635 enum rte_intr_mode value;
637 { "legacy", RTE_INTR_MODE_LEGACY },
638 { "msi", RTE_INTR_MODE_MSI },
639 { "msix", RTE_INTR_MODE_MSIX },
642 for (i = 0; i < RTE_DIM(map); i++) {
643 if (!strcmp(mode, map[i].name)) {
644 internal_config.vfio_intr_mode = map[i].value;
651 /* Parse the arguments for --log-level only */
653 eal_log_level_parse(int argc, char **argv)
658 const int old_optind = optind;
659 const int old_optopt = optopt;
660 char * const old_optarg = optarg;
665 while ((opt = getopt_long(argc, argvopt, eal_short_options,
666 eal_long_options, &option_index)) != EOF) {
670 /* getopt is not happy, stop right now */
674 ret = (opt == OPT_LOG_LEVEL_NUM) ?
675 eal_parse_common_option(opt, optarg, &internal_config) : 0;
677 /* common parser is not happy */
682 /* restore getopt lib */
688 /* Parse the argument given in the command line of the application */
690 eal_parse_args(int argc, char **argv)
695 char *prgname = argv[0];
696 const int old_optind = optind;
697 const int old_optopt = optopt;
698 char * const old_optarg = optarg;
704 while ((opt = getopt_long(argc, argvopt, eal_short_options,
705 eal_long_options, &option_index)) != EOF) {
708 * getopt didn't recognise the option, lets parse the
709 * registered options to see if the flag is valid
712 ret = rte_option_parse(argv[optind-1]);
721 ret = eal_parse_common_option(opt, optarg, &internal_config);
722 /* common parser is not happy */
728 /* common parser handled this option */
737 case OPT_HUGE_DIR_NUM:
739 char *hdir = strdup(optarg);
741 RTE_LOG(ERR, EAL, "Could not store hugepage directory\n");
743 /* free old hugepage dir */
744 if (internal_config.hugepage_dir != NULL)
745 free(internal_config.hugepage_dir);
746 internal_config.hugepage_dir = hdir;
750 case OPT_FILE_PREFIX_NUM:
752 char *prefix = strdup(optarg);
754 RTE_LOG(ERR, EAL, "Could not store file prefix\n");
756 /* free old prefix */
757 if (internal_config.hugefile_prefix != NULL)
758 free(internal_config.hugefile_prefix);
759 internal_config.hugefile_prefix = prefix;
763 case OPT_SOCKET_MEM_NUM:
764 if (eal_parse_socket_arg(optarg,
765 internal_config.socket_mem) < 0) {
766 RTE_LOG(ERR, EAL, "invalid parameters for --"
767 OPT_SOCKET_MEM "\n");
772 internal_config.force_sockets = 1;
775 case OPT_SOCKET_LIMIT_NUM:
776 if (eal_parse_socket_arg(optarg,
777 internal_config.socket_limit) < 0) {
778 RTE_LOG(ERR, EAL, "invalid parameters for --"
779 OPT_SOCKET_LIMIT "\n");
784 internal_config.force_socket_limits = 1;
787 case OPT_VFIO_INTR_NUM:
788 if (eal_parse_vfio_intr(optarg) < 0) {
789 RTE_LOG(ERR, EAL, "invalid parameters for --"
797 case OPT_CREATE_UIO_DEV_NUM:
798 internal_config.create_uio_dev = 1;
801 case OPT_MBUF_POOL_OPS_NAME_NUM:
803 char *ops_name = strdup(optarg);
804 if (ops_name == NULL)
805 RTE_LOG(ERR, EAL, "Could not store mbuf pool ops name\n");
807 /* free old ops name */
808 if (internal_config.user_mbuf_pool_ops_name !=
810 free(internal_config.user_mbuf_pool_ops_name);
812 internal_config.user_mbuf_pool_ops_name =
817 case OPT_MATCH_ALLOCATIONS_NUM:
818 internal_config.match_allocations = 1;
822 if (opt < OPT_LONG_MIN_NUM && isprint(opt)) {
823 RTE_LOG(ERR, EAL, "Option %c is not supported "
825 } else if (opt >= OPT_LONG_MIN_NUM &&
826 opt < OPT_LONG_MAX_NUM) {
827 RTE_LOG(ERR, EAL, "Option %s is not supported "
829 eal_long_options[option_index].name);
831 RTE_LOG(ERR, EAL, "Option %d is not supported "
840 /* create runtime data directory */
841 if (internal_config.no_shconf == 0 &&
842 eal_create_runtime_dir() < 0) {
843 RTE_LOG(ERR, EAL, "Cannot create runtime directory\n");
848 if (eal_adjust_config(&internal_config) != 0) {
854 if (eal_check_common_options(&internal_config) != 0) {
861 argv[optind-1] = prgname;
865 /* restore getopt lib */
874 check_socket(const struct rte_memseg_list *msl, void *arg)
876 int *socket_id = arg;
881 return *socket_id == msl->socket_id;
885 eal_check_mem_on_local_socket(void)
889 socket_id = rte_lcore_to_socket_id(rte_config.master_lcore);
891 if (rte_memseg_list_walk(check_socket, &socket_id) == 0)
892 RTE_LOG(WARNING, EAL, "WARNING: Master core has no memory on local socket!\n");
896 sync_func(__attribute__((unused)) void *arg)
902 * Request iopl privilege for all RPL, returns 0 on success
903 * iopl() call is mostly for the i386 architecture. For other architectures,
904 * return -1 to indicate IO privilege can't be changed in this way.
907 rte_eal_iopl_init(void)
909 #if defined(RTE_ARCH_X86)
917 static int rte_eal_vfio_setup(void)
919 if (rte_vfio_enable("vfio"))
926 static void rte_eal_init_alert(const char *msg)
928 fprintf(stderr, "EAL: FATAL: %s\n", msg);
929 RTE_LOG(ERR, EAL, "%s\n", msg);
933 * On Linux 3.6+, even if VFIO is not loaded, whenever IOMMU is enabled in the
934 * BIOS and in the kernel, /sys/kernel/iommu_groups path will contain kernel
935 * IOMMU groups. If IOMMU is not enabled, that path would be empty.
936 * Therefore, checking if the path is empty will tell us if IOMMU is enabled.
939 is_iommu_enabled(void)
941 DIR *dir = opendir(KERNEL_IOMMU_GROUPS_PATH);
945 /* if directory doesn't exist, assume IOMMU is not enabled */
949 while ((d = readdir(dir)) != NULL) {
950 /* skip dot and dot-dot */
959 /* Launch threads, called at application init(). */
961 rte_eal_init(int argc, char **argv)
965 static rte_atomic32_t run_once = RTE_ATOMIC32_INIT(0);
967 static char logid[PATH_MAX];
968 char cpuset[RTE_CPU_AFFINITY_STR_LEN];
969 char thread_name[RTE_MAX_THREAD_NAME_LEN];
972 /* checks if the machine is adequate */
973 if (!rte_cpu_is_supported()) {
974 rte_eal_init_alert("unsupported cpu type.");
979 if (!rte_atomic32_test_and_set(&run_once)) {
980 rte_eal_init_alert("already called initialization.");
981 rte_errno = EALREADY;
985 p = strrchr(argv[0], '/');
986 strlcpy(logid, p ? p + 1 : argv[0], sizeof(logid));
987 thread_id = pthread_self();
989 eal_reset_internal_config(&internal_config);
991 /* set log level as early as possible */
992 eal_log_level_parse(argc, argv);
994 if (rte_eal_cpu_init() < 0) {
995 rte_eal_init_alert("Cannot detect lcores.");
1000 fctret = eal_parse_args(argc, argv);
1002 rte_eal_init_alert("Invalid 'command line' arguments.");
1004 rte_atomic32_clear(&run_once);
1008 if (eal_plugins_init() < 0) {
1009 rte_eal_init_alert("Cannot init plugins");
1011 rte_atomic32_clear(&run_once);
1015 if (eal_option_device_parse()) {
1017 rte_atomic32_clear(&run_once);
1021 if (rte_config_init() < 0) {
1022 rte_eal_init_alert("Cannot init config");
1026 if (rte_eal_intr_init() < 0) {
1027 rte_eal_init_alert("Cannot init interrupt-handling thread");
1031 if (rte_eal_alarm_init() < 0) {
1032 rte_eal_init_alert("Cannot init alarm");
1033 /* rte_eal_alarm_init sets rte_errno on failure. */
1037 /* Put mp channel init before bus scan so that we can init the vdev
1038 * bus through mp channel in the secondary process before the bus scan.
1040 if (rte_mp_channel_init() < 0 && rte_errno != ENOTSUP) {
1041 rte_eal_init_alert("failed to init mp channel");
1042 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
1048 /* register multi-process action callbacks for hotplug */
1049 if (eal_mp_dev_hotplug_init() < 0) {
1050 rte_eal_init_alert("failed to register mp callback for hotplug");
1054 if (rte_bus_scan()) {
1055 rte_eal_init_alert("Cannot scan the buses for devices");
1057 rte_atomic32_clear(&run_once);
1061 phys_addrs = rte_eal_using_phys_addrs() != 0;
1063 /* if no EAL option "--iova-mode=<pa|va>", use bus IOVA scheme */
1064 if (internal_config.iova_mode == RTE_IOVA_DC) {
1065 /* autodetect the IOVA mapping mode */
1066 enum rte_iova_mode iova_mode = rte_bus_get_iommu_class();
1068 if (iova_mode == RTE_IOVA_DC) {
1069 RTE_LOG(DEBUG, EAL, "Buses did not request a specific IOVA mode.\n");
1072 /* if we have no access to physical addresses,
1073 * pick IOVA as VA mode.
1075 iova_mode = RTE_IOVA_VA;
1076 RTE_LOG(DEBUG, EAL, "Physical addresses are unavailable, selecting IOVA as VA mode.\n");
1077 #if defined(RTE_LIBRTE_KNI) && LINUX_VERSION_CODE >= KERNEL_VERSION(4, 10, 0)
1078 } else if (rte_eal_check_module("rte_kni") == 1) {
1079 iova_mode = RTE_IOVA_PA;
1080 RTE_LOG(DEBUG, EAL, "KNI is loaded, selecting IOVA as PA mode for better KNI perfomance.\n");
1082 } else if (is_iommu_enabled()) {
1083 /* we have an IOMMU, pick IOVA as VA mode */
1084 iova_mode = RTE_IOVA_VA;
1085 RTE_LOG(DEBUG, EAL, "IOMMU is available, selecting IOVA as VA mode.\n");
1087 /* physical addresses available, and no IOMMU
1088 * found, so pick IOVA as PA.
1090 iova_mode = RTE_IOVA_PA;
1091 RTE_LOG(DEBUG, EAL, "IOMMU is not available, selecting IOVA as PA mode.\n");
1094 #if defined(RTE_LIBRTE_KNI) && LINUX_VERSION_CODE < KERNEL_VERSION(4, 10, 0)
1095 /* Workaround for KNI which requires physical address to work
1098 if (iova_mode == RTE_IOVA_VA &&
1099 rte_eal_check_module("rte_kni") == 1) {
1101 iova_mode = RTE_IOVA_PA;
1102 RTE_LOG(WARNING, EAL, "Forcing IOVA as 'PA' because KNI module is loaded\n");
1104 RTE_LOG(DEBUG, EAL, "KNI can not work since physical addresses are unavailable\n");
1108 rte_eal_get_configuration()->iova_mode = iova_mode;
1110 rte_eal_get_configuration()->iova_mode =
1111 internal_config.iova_mode;
1114 if (rte_eal_iova_mode() == RTE_IOVA_PA && !phys_addrs) {
1115 rte_eal_init_alert("Cannot use IOVA as 'PA' since physical addresses are not available");
1120 RTE_LOG(INFO, EAL, "Selected IOVA mode '%s'\n",
1121 rte_eal_iova_mode() == RTE_IOVA_PA ? "PA" : "VA");
1123 if (internal_config.no_hugetlbfs == 0) {
1124 /* rte_config isn't initialized yet */
1125 ret = internal_config.process_type == RTE_PROC_PRIMARY ?
1126 eal_hugepage_info_init() :
1127 eal_hugepage_info_read();
1129 rte_eal_init_alert("Cannot get hugepage information.");
1131 rte_atomic32_clear(&run_once);
1136 if (internal_config.memory == 0 && internal_config.force_sockets == 0) {
1137 if (internal_config.no_hugetlbfs)
1138 internal_config.memory = MEMSIZE_IF_NO_HUGE_PAGE;
1141 if (internal_config.vmware_tsc_map == 1) {
1142 #ifdef RTE_LIBRTE_EAL_VMWARE_TSC_MAP_SUPPORT
1143 rte_cycles_vmware_tsc_map = 1;
1144 RTE_LOG (DEBUG, EAL, "Using VMWARE TSC MAP, "
1145 "you must have monitor_control.pseudo_perfctr = TRUE\n");
1147 RTE_LOG (WARNING, EAL, "Ignoring --vmware-tsc-map because "
1148 "RTE_LIBRTE_EAL_VMWARE_TSC_MAP_SUPPORT is not set\n");
1152 if (rte_eal_log_init(logid, internal_config.syslog_facility) < 0) {
1153 rte_eal_init_alert("Cannot init logging.");
1155 rte_atomic32_clear(&run_once);
1160 if (rte_eal_vfio_setup() < 0) {
1161 rte_eal_init_alert("Cannot init VFIO");
1163 rte_atomic32_clear(&run_once);
1167 /* in secondary processes, memory init may allocate additional fbarrays
1168 * not present in primary processes, so to avoid any potential issues,
1169 * initialize memzones first.
1171 if (rte_eal_memzone_init() < 0) {
1172 rte_eal_init_alert("Cannot init memzone");
1177 if (rte_eal_memory_init() < 0) {
1178 rte_eal_init_alert("Cannot init memory");
1183 /* the directories are locked during eal_hugepage_info_init */
1184 eal_hugedirs_unlock();
1186 if (rte_eal_malloc_heap_init() < 0) {
1187 rte_eal_init_alert("Cannot init malloc heap");
1192 if (rte_eal_tailqs_init() < 0) {
1193 rte_eal_init_alert("Cannot init tail queues for objects");
1198 if (rte_eal_timer_init() < 0) {
1199 rte_eal_init_alert("Cannot init HPET or TSC timers");
1200 rte_errno = ENOTSUP;
1204 eal_check_mem_on_local_socket();
1206 eal_thread_init_master(rte_config.master_lcore);
1208 ret = eal_thread_dump_affinity(cpuset, sizeof(cpuset));
1210 RTE_LOG(DEBUG, EAL, "Master lcore %u is ready (tid=%zx;cpuset=[%s%s])\n",
1211 rte_config.master_lcore, (uintptr_t)thread_id, cpuset,
1212 ret == 0 ? "" : "...");
1214 RTE_LCORE_FOREACH_SLAVE(i) {
1217 * create communication pipes between master thread
1220 if (pipe(lcore_config[i].pipe_master2slave) < 0)
1221 rte_panic("Cannot create pipe\n");
1222 if (pipe(lcore_config[i].pipe_slave2master) < 0)
1223 rte_panic("Cannot create pipe\n");
1225 lcore_config[i].state = WAIT;
1227 /* create a thread for each lcore */
1228 ret = pthread_create(&lcore_config[i].thread_id, NULL,
1229 eal_thread_loop, NULL);
1231 rte_panic("Cannot create thread\n");
1233 /* Set thread_name for aid in debugging. */
1234 snprintf(thread_name, sizeof(thread_name),
1235 "lcore-slave-%d", i);
1236 ret = rte_thread_setname(lcore_config[i].thread_id,
1240 "Cannot set name for lcore thread\n");
1244 * Launch a dummy function on all slave lcores, so that master lcore
1245 * knows they are all ready when this function returns.
1247 rte_eal_mp_remote_launch(sync_func, NULL, SKIP_MASTER);
1248 rte_eal_mp_wait_lcore();
1250 /* initialize services so vdevs register service during bus_probe. */
1251 ret = rte_service_init();
1253 rte_eal_init_alert("rte_service_init() failed");
1254 rte_errno = ENOEXEC;
1258 /* Probe all the buses and devices/drivers on them */
1259 if (rte_bus_probe()) {
1260 rte_eal_init_alert("Cannot probe devices");
1261 rte_errno = ENOTSUP;
1266 /* Register mp action after probe() so that we got enough info */
1267 if (rte_vfio_is_enabled("vfio") && vfio_mp_sync_setup() < 0)
1271 /* initialize default service/lcore mappings and start running. Ignore
1272 * -ENOTSUP, as it indicates no service coremask passed to EAL.
1274 ret = rte_service_start_with_defaults();
1275 if (ret < 0 && ret != -ENOTSUP) {
1276 rte_errno = ENOEXEC;
1281 * Clean up unused files in runtime directory. We do this at the end of
1282 * init and not at the beginning because we want to clean stuff up
1283 * whether we are primary or secondary process, but we cannot remove
1284 * primary process' files because secondary should be able to run even
1285 * if primary process is dead.
1287 * In no_shconf mode, no runtime directory is created in the first
1288 * place, so no cleanup needed.
1290 if (!internal_config.no_shconf && eal_clean_runtime_dir() < 0) {
1291 rte_eal_init_alert("Cannot clear runtime directory\n");
1295 eal_mcfg_complete();
1297 /* Call each registered callback, if enabled */
1304 mark_freeable(const struct rte_memseg_list *msl, const struct rte_memseg *ms,
1305 void *arg __rte_unused)
1307 /* ms is const, so find this memseg */
1308 struct rte_memseg *found;
1313 found = rte_mem_virt2memseg(ms->addr, msl);
1315 found->flags &= ~RTE_MEMSEG_FLAG_DO_NOT_FREE;
1321 rte_eal_cleanup(void)
1323 /* if we're in a primary process, we need to mark hugepages as freeable
1324 * so that finalization can release them back to the system.
1326 if (rte_eal_process_type() == RTE_PROC_PRIMARY)
1327 rte_memseg_walk(mark_freeable, NULL);
1328 rte_service_finalize();
1329 rte_mp_channel_cleanup();
1330 eal_cleanup_config(&internal_config);
1334 enum rte_proc_type_t
1335 rte_eal_process_type(void)
1337 return rte_config.process_type;
1340 int rte_eal_has_hugepages(void)
1342 return ! internal_config.no_hugetlbfs;
1345 int rte_eal_has_pci(void)
1347 return !internal_config.no_pci;
1350 int rte_eal_create_uio_dev(void)
1352 return internal_config.create_uio_dev;
1356 rte_eal_vfio_intr_mode(void)
1358 return internal_config.vfio_intr_mode;
1362 rte_eal_check_module(const char *module_name)
1364 char sysfs_mod_name[PATH_MAX];
1368 if (NULL == module_name)
1371 /* Check if there is sysfs mounted */
1372 if (stat("/sys/module", &st) != 0) {
1373 RTE_LOG(DEBUG, EAL, "sysfs is not mounted! error %i (%s)\n",
1374 errno, strerror(errno));
1378 /* A module might be built-in, therefore try sysfs */
1379 n = snprintf(sysfs_mod_name, PATH_MAX, "/sys/module/%s", module_name);
1380 if (n < 0 || n > PATH_MAX) {
1381 RTE_LOG(DEBUG, EAL, "Could not format module path\n");
1385 if (stat(sysfs_mod_name, &st) != 0) {
1386 RTE_LOG(DEBUG, EAL, "Module %s not found! error %i (%s)\n",
1387 sysfs_mod_name, errno, strerror(errno));
1391 /* Module has been found */