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_telemetry.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_trace.h"
62 #include "eal_options.h"
64 #include "hotplug_mp.h"
66 #define MEMSIZE_IF_NO_HUGE_PAGE (64ULL * 1024ULL * 1024ULL)
68 #define SOCKET_MEM_STRLEN (RTE_MAX_NUMA_NODES * 10)
70 #define KERNEL_IOMMU_GROUPS_PATH "/sys/kernel/iommu_groups"
72 /* Allow the application to print its usage message too if set */
73 static rte_usage_hook_t rte_application_usage_hook = NULL;
75 /* define fd variable here, because file needs to be kept open for the
76 * duration of the program, as we hold a write lock on it in the primary proc */
77 static int mem_cfg_fd = -1;
79 static struct flock wr_lock = {
82 .l_start = offsetof(struct rte_mem_config, memsegs),
83 .l_len = RTE_SIZEOF_FIELD(struct rte_mem_config, memsegs),
86 /* internal configuration (per-core) */
87 struct lcore_config lcore_config[RTE_MAX_LCORE];
89 /* used by rte_rdtsc() */
90 int rte_cycles_vmware_tsc_map;
92 static const char *default_runtime_dir = "/var/run";
95 eal_create_runtime_dir(void)
97 const char *directory = default_runtime_dir;
98 const char *xdg_runtime_dir = getenv("XDG_RUNTIME_DIR");
99 const char *fallback = "/tmp";
100 char run_dir[PATH_MAX];
105 /* try XDG path first, fall back to /tmp */
106 if (xdg_runtime_dir != NULL)
107 directory = xdg_runtime_dir;
109 directory = fallback;
111 /* create DPDK subdirectory under runtime dir */
112 ret = snprintf(tmp, sizeof(tmp), "%s/dpdk", directory);
113 if (ret < 0 || ret == sizeof(tmp)) {
114 RTE_LOG(ERR, EAL, "Error creating DPDK runtime path name\n");
118 /* create prefix-specific subdirectory under DPDK runtime dir */
119 ret = snprintf(run_dir, sizeof(run_dir), "%s/%s",
120 tmp, eal_get_hugefile_prefix());
121 if (ret < 0 || ret == sizeof(run_dir)) {
122 RTE_LOG(ERR, EAL, "Error creating prefix-specific runtime path name\n");
126 /* create the path if it doesn't exist. no "mkdir -p" here, so do it
129 ret = mkdir(tmp, 0700);
130 if (ret < 0 && errno != EEXIST) {
131 RTE_LOG(ERR, EAL, "Error creating '%s': %s\n",
132 tmp, strerror(errno));
136 ret = mkdir(run_dir, 0700);
137 if (ret < 0 && errno != EEXIST) {
138 RTE_LOG(ERR, EAL, "Error creating '%s': %s\n",
139 run_dir, strerror(errno));
143 if (eal_set_runtime_dir(run_dir, sizeof(run_dir)))
150 eal_clean_runtime_dir(void)
152 const char *runtime_dir = rte_eal_get_runtime_dir();
154 struct dirent *dirent;
155 int dir_fd, fd, lck_result;
156 static const char * const filters[] = {
162 dir = opendir(runtime_dir);
164 RTE_LOG(ERR, EAL, "Unable to open runtime directory %s\n",
170 /* lock the directory before doing anything, to avoid races */
171 if (flock(dir_fd, LOCK_EX) < 0) {
172 RTE_LOG(ERR, EAL, "Unable to lock runtime directory %s\n",
177 dirent = readdir(dir);
179 RTE_LOG(ERR, EAL, "Unable to read runtime directory %s\n",
184 while (dirent != NULL) {
188 /* skip files that don't match the patterns */
189 for (f_idx = 0; f_idx < RTE_DIM(filters); f_idx++) {
190 const char *filter = filters[f_idx];
192 if (fnmatch(filter, dirent->d_name, 0) == 0) {
198 dirent = readdir(dir);
202 /* try and lock the file */
203 fd = openat(dir_fd, dirent->d_name, O_RDONLY);
205 /* skip to next file */
207 dirent = readdir(dir);
211 /* non-blocking lock */
212 lck_result = flock(fd, LOCK_EX | LOCK_NB);
214 /* if lock succeeds, remove the file */
215 if (lck_result != -1)
216 unlinkat(dir_fd, dirent->d_name, 0);
218 dirent = readdir(dir);
221 /* closedir closes dir_fd and drops the lock */
229 RTE_LOG(ERR, EAL, "Error while clearing runtime dir: %s\n",
235 /* parse a sysfs (or other) file containing one integer value */
237 eal_parse_sysfs_value(const char *filename, unsigned long *val)
243 if ((f = fopen(filename, "r")) == NULL) {
244 RTE_LOG(ERR, EAL, "%s(): cannot open sysfs value %s\n",
249 if (fgets(buf, sizeof(buf), f) == NULL) {
250 RTE_LOG(ERR, EAL, "%s(): cannot read sysfs value %s\n",
255 *val = strtoul(buf, &end, 0);
256 if ((buf[0] == '\0') || (end == NULL) || (*end != '\n')) {
257 RTE_LOG(ERR, EAL, "%s(): cannot parse sysfs value %s\n",
267 /* create memory configuration in shared/mmap memory. Take out
268 * a write lock on the memsegs, so we can auto-detect primary/secondary.
269 * This means we never close the file while running (auto-close on exit).
270 * We also don't lock the whole file, so that in future we can use read-locks
271 * on other parts, e.g. memzones, to detect if there are running secondary
274 rte_eal_config_create(void)
276 struct rte_config *config = rte_eal_get_configuration();
277 size_t page_sz = sysconf(_SC_PAGE_SIZE);
278 size_t cfg_len = sizeof(*config->mem_config);
279 size_t cfg_len_aligned = RTE_ALIGN(cfg_len, page_sz);
280 void *rte_mem_cfg_addr, *mapped_mem_cfg_addr;
282 const struct internal_config *internal_conf =
283 eal_get_internal_configuration();
285 const char *pathname = eal_runtime_config_path();
287 if (internal_conf->no_shconf)
290 /* map the config before hugepage address so that we don't waste a page */
291 if (internal_conf->base_virtaddr != 0)
292 rte_mem_cfg_addr = (void *)
293 RTE_ALIGN_FLOOR(internal_conf->base_virtaddr -
294 sizeof(struct rte_mem_config), page_sz);
296 rte_mem_cfg_addr = NULL;
299 mem_cfg_fd = open(pathname, O_RDWR | O_CREAT, 0600);
300 if (mem_cfg_fd < 0) {
301 RTE_LOG(ERR, EAL, "Cannot open '%s' for rte_mem_config\n",
307 retval = ftruncate(mem_cfg_fd, cfg_len);
311 RTE_LOG(ERR, EAL, "Cannot resize '%s' for rte_mem_config\n",
316 retval = fcntl(mem_cfg_fd, F_SETLK, &wr_lock);
320 RTE_LOG(ERR, EAL, "Cannot create lock on '%s'. Is another primary "
321 "process running?\n", pathname);
325 /* reserve space for config */
326 rte_mem_cfg_addr = eal_get_virtual_area(rte_mem_cfg_addr,
327 &cfg_len_aligned, page_sz, 0, 0);
328 if (rte_mem_cfg_addr == NULL) {
329 RTE_LOG(ERR, EAL, "Cannot mmap memory for rte_config\n");
335 /* remap the actual file into the space we've just reserved */
336 mapped_mem_cfg_addr = mmap(rte_mem_cfg_addr,
337 cfg_len_aligned, PROT_READ | PROT_WRITE,
338 MAP_SHARED | MAP_FIXED, mem_cfg_fd, 0);
339 if (mapped_mem_cfg_addr == MAP_FAILED) {
340 munmap(rte_mem_cfg_addr, cfg_len);
343 RTE_LOG(ERR, EAL, "Cannot remap memory for rte_config\n");
347 memcpy(rte_mem_cfg_addr, config->mem_config, sizeof(struct rte_mem_config));
348 config->mem_config = rte_mem_cfg_addr;
350 /* store address of the config in the config itself so that secondary
351 * processes could later map the config into this exact location
353 config->mem_config->mem_cfg_addr = (uintptr_t) rte_mem_cfg_addr;
354 config->mem_config->dma_maskbits = 0;
359 /* attach to an existing shared memory config */
361 rte_eal_config_attach(void)
363 struct rte_config *config = rte_eal_get_configuration();
364 struct rte_mem_config *mem_config;
365 const struct internal_config *internal_conf =
366 eal_get_internal_configuration();
368 const char *pathname = eal_runtime_config_path();
370 if (internal_conf->no_shconf)
374 mem_cfg_fd = open(pathname, O_RDWR);
375 if (mem_cfg_fd < 0) {
376 RTE_LOG(ERR, EAL, "Cannot open '%s' for rte_mem_config\n",
382 /* map it as read-only first */
383 mem_config = (struct rte_mem_config *) mmap(NULL, sizeof(*mem_config),
384 PROT_READ, MAP_SHARED, mem_cfg_fd, 0);
385 if (mem_config == MAP_FAILED) {
388 RTE_LOG(ERR, EAL, "Cannot mmap memory for rte_config! error %i (%s)\n",
389 errno, strerror(errno));
393 config->mem_config = mem_config;
398 /* reattach the shared config at exact memory location primary process has it */
400 rte_eal_config_reattach(void)
402 struct rte_config *config = rte_eal_get_configuration();
403 struct rte_mem_config *mem_config;
404 void *rte_mem_cfg_addr;
405 const struct internal_config *internal_conf =
406 eal_get_internal_configuration();
408 if (internal_conf->no_shconf)
411 /* save the address primary process has mapped shared config to */
413 (void *) (uintptr_t) config->mem_config->mem_cfg_addr;
415 /* unmap original config */
416 munmap(config->mem_config, sizeof(struct rte_mem_config));
418 /* remap the config at proper address */
419 mem_config = (struct rte_mem_config *) mmap(rte_mem_cfg_addr,
420 sizeof(*mem_config), PROT_READ | PROT_WRITE, MAP_SHARED,
426 if (mem_config == MAP_FAILED || mem_config != rte_mem_cfg_addr) {
427 if (mem_config != MAP_FAILED) {
428 /* errno is stale, don't use */
429 RTE_LOG(ERR, EAL, "Cannot mmap memory for rte_config at [%p], got [%p]"
430 " - please use '--" OPT_BASE_VIRTADDR
431 "' option\n", rte_mem_cfg_addr, mem_config);
432 munmap(mem_config, sizeof(struct rte_mem_config));
435 RTE_LOG(ERR, EAL, "Cannot mmap memory for rte_config! error %i (%s)\n",
436 errno, strerror(errno));
440 config->mem_config = mem_config;
445 /* Detect if we are a primary or a secondary process */
447 eal_proc_type_detect(void)
449 enum rte_proc_type_t ptype = RTE_PROC_PRIMARY;
450 const char *pathname = eal_runtime_config_path();
451 const struct internal_config *internal_conf =
452 eal_get_internal_configuration();
454 /* if there no shared config, there can be no secondary processes */
455 if (!internal_conf->no_shconf) {
456 /* if we can open the file but not get a write-lock we are a
457 * secondary process. NOTE: if we get a file handle back, we
458 * keep that open and don't close it to prevent a race condition
459 * between multiple opens.
461 if (((mem_cfg_fd = open(pathname, O_RDWR)) >= 0) &&
462 (fcntl(mem_cfg_fd, F_SETLK, &wr_lock) < 0))
463 ptype = RTE_PROC_SECONDARY;
466 RTE_LOG(INFO, EAL, "Auto-detected process type: %s\n",
467 ptype == RTE_PROC_PRIMARY ? "PRIMARY" : "SECONDARY");
472 /* Sets up rte_config structure with the pointer to shared memory config.*/
474 rte_config_init(void)
476 struct rte_config *config = rte_eal_get_configuration();
477 const struct internal_config *internal_conf =
478 eal_get_internal_configuration();
480 config->process_type = internal_conf->process_type;
482 switch (config->process_type) {
483 case RTE_PROC_PRIMARY:
484 if (rte_eal_config_create() < 0)
486 eal_mcfg_update_from_internal();
488 case RTE_PROC_SECONDARY:
489 if (rte_eal_config_attach() < 0)
491 eal_mcfg_wait_complete();
492 if (eal_mcfg_check_version() < 0) {
493 RTE_LOG(ERR, EAL, "Primary and secondary process DPDK version mismatch\n");
496 if (rte_eal_config_reattach() < 0)
498 eal_mcfg_update_internal();
501 case RTE_PROC_INVALID:
502 RTE_LOG(ERR, EAL, "Invalid process type %d\n",
503 config->process_type);
510 /* Unlocks hugepage directories that were locked by eal_hugepage_info_init */
512 eal_hugedirs_unlock(void)
515 struct internal_config *internal_conf =
516 eal_get_internal_configuration();
518 for (i = 0; i < MAX_HUGEPAGE_SIZES; i++)
520 /* skip uninitialized */
521 if (internal_conf->hugepage_info[i].lock_descriptor < 0)
523 /* unlock hugepage file */
524 flock(internal_conf->hugepage_info[i].lock_descriptor, LOCK_UN);
525 close(internal_conf->hugepage_info[i].lock_descriptor);
526 /* reset the field */
527 internal_conf->hugepage_info[i].lock_descriptor = -1;
533 eal_usage(const char *prgname)
535 printf("\nUsage: %s ", prgname);
537 printf("EAL Linux options:\n"
538 " --"OPT_SOCKET_MEM" Memory to allocate on sockets (comma separated values)\n"
539 " --"OPT_SOCKET_LIMIT" Limit memory allocation on sockets (comma separated values)\n"
540 " --"OPT_HUGE_DIR" Directory where hugetlbfs is mounted\n"
541 " --"OPT_FILE_PREFIX" Prefix for hugepage filenames\n"
542 " --"OPT_CREATE_UIO_DEV" Create /dev/uioX (usually done by hotplug)\n"
543 " --"OPT_VFIO_INTR" Interrupt mode for VFIO (legacy|msi|msix)\n"
544 " --"OPT_LEGACY_MEM" Legacy memory mode (no dynamic allocation, contiguous segments)\n"
545 " --"OPT_SINGLE_FILE_SEGMENTS" Put all hugepage memory in single files\n"
546 " --"OPT_MATCH_ALLOCATIONS" Free hugepages exactly as allocated\n"
548 /* Allow the application to print its usage message too if hook is set */
549 if ( rte_application_usage_hook ) {
550 printf("===== Application Usage =====\n\n");
551 rte_application_usage_hook(prgname);
555 /* Set a per-application usage message */
557 rte_set_application_usage_hook( rte_usage_hook_t usage_func )
559 rte_usage_hook_t old_func;
561 /* Will be NULL on the first call to denote the last usage routine. */
562 old_func = rte_application_usage_hook;
563 rte_application_usage_hook = usage_func;
569 eal_parse_socket_arg(char *strval, volatile uint64_t *socket_arg)
571 char * arg[RTE_MAX_NUMA_NODES];
574 uint64_t total_mem = 0;
576 len = strnlen(strval, SOCKET_MEM_STRLEN);
577 if (len == SOCKET_MEM_STRLEN) {
578 RTE_LOG(ERR, EAL, "--socket-mem is too long\n");
582 /* all other error cases will be caught later */
583 if (!isdigit(strval[len-1]))
586 /* split the optarg into separate socket values */
587 arg_num = rte_strsplit(strval, len,
588 arg, RTE_MAX_NUMA_NODES, ',');
590 /* if split failed, or 0 arguments */
594 /* parse each defined socket option */
596 for (i = 0; i < arg_num; i++) {
599 val = strtoull(arg[i], &end, 10);
601 /* check for invalid input */
603 (arg[i][0] == '\0') || (end == NULL) || (*end != '\0'))
614 eal_parse_vfio_intr(const char *mode)
616 struct internal_config *internal_conf =
617 eal_get_internal_configuration();
621 enum rte_intr_mode value;
623 { "legacy", RTE_INTR_MODE_LEGACY },
624 { "msi", RTE_INTR_MODE_MSI },
625 { "msix", RTE_INTR_MODE_MSIX },
628 for (i = 0; i < RTE_DIM(map); i++) {
629 if (!strcmp(mode, map[i].name)) {
630 internal_conf->vfio_intr_mode = map[i].value;
637 /* Parse the arguments for --log-level only */
639 eal_log_level_parse(int argc, char **argv)
644 const int old_optind = optind;
645 const int old_optopt = optopt;
646 char * const old_optarg = optarg;
647 struct internal_config *internal_conf =
648 eal_get_internal_configuration();
653 while ((opt = getopt_long(argc, argvopt, eal_short_options,
654 eal_long_options, &option_index)) != EOF) {
658 /* getopt is not happy, stop right now */
662 ret = (opt == OPT_LOG_LEVEL_NUM) ?
663 eal_parse_common_option(opt, optarg, internal_conf) : 0;
665 /* common parser is not happy */
670 /* restore getopt lib */
676 /* Parse the argument given in the command line of the application */
678 eal_parse_args(int argc, char **argv)
683 char *prgname = argv[0];
684 const int old_optind = optind;
685 const int old_optopt = optopt;
686 char * const old_optarg = optarg;
687 struct internal_config *internal_conf =
688 eal_get_internal_configuration();
693 while ((opt = getopt_long(argc, argvopt, eal_short_options,
694 eal_long_options, &option_index)) != EOF) {
696 /* getopt didn't recognise the option */
703 ret = eal_parse_common_option(opt, optarg, internal_conf);
704 /* common parser is not happy */
710 /* common parser handled this option */
719 case OPT_HUGE_DIR_NUM:
721 char *hdir = strdup(optarg);
723 RTE_LOG(ERR, EAL, "Could not store hugepage directory\n");
725 /* free old hugepage dir */
726 if (internal_conf->hugepage_dir != NULL)
727 free(internal_conf->hugepage_dir);
728 internal_conf->hugepage_dir = hdir;
732 case OPT_FILE_PREFIX_NUM:
734 char *prefix = strdup(optarg);
736 RTE_LOG(ERR, EAL, "Could not store file prefix\n");
738 /* free old prefix */
739 if (internal_conf->hugefile_prefix != NULL)
740 free(internal_conf->hugefile_prefix);
741 internal_conf->hugefile_prefix = prefix;
745 case OPT_SOCKET_MEM_NUM:
746 if (eal_parse_socket_arg(optarg,
747 internal_conf->socket_mem) < 0) {
748 RTE_LOG(ERR, EAL, "invalid parameters for --"
749 OPT_SOCKET_MEM "\n");
754 internal_conf->force_sockets = 1;
757 case OPT_SOCKET_LIMIT_NUM:
758 if (eal_parse_socket_arg(optarg,
759 internal_conf->socket_limit) < 0) {
760 RTE_LOG(ERR, EAL, "invalid parameters for --"
761 OPT_SOCKET_LIMIT "\n");
766 internal_conf->force_socket_limits = 1;
769 case OPT_VFIO_INTR_NUM:
770 if (eal_parse_vfio_intr(optarg) < 0) {
771 RTE_LOG(ERR, EAL, "invalid parameters for --"
779 case OPT_CREATE_UIO_DEV_NUM:
780 internal_conf->create_uio_dev = 1;
783 case OPT_MBUF_POOL_OPS_NAME_NUM:
785 char *ops_name = strdup(optarg);
786 if (ops_name == NULL)
787 RTE_LOG(ERR, EAL, "Could not store mbuf pool ops name\n");
789 /* free old ops name */
790 if (internal_conf->user_mbuf_pool_ops_name !=
792 free(internal_conf->user_mbuf_pool_ops_name);
794 internal_conf->user_mbuf_pool_ops_name =
799 case OPT_MATCH_ALLOCATIONS_NUM:
800 internal_conf->match_allocations = 1;
804 if (opt < OPT_LONG_MIN_NUM && isprint(opt)) {
805 RTE_LOG(ERR, EAL, "Option %c is not supported "
807 } else if (opt >= OPT_LONG_MIN_NUM &&
808 opt < OPT_LONG_MAX_NUM) {
809 RTE_LOG(ERR, EAL, "Option %s is not supported "
811 eal_long_options[option_index].name);
813 RTE_LOG(ERR, EAL, "Option %d is not supported "
822 /* create runtime data directory */
823 if (internal_conf->no_shconf == 0 &&
824 eal_create_runtime_dir() < 0) {
825 RTE_LOG(ERR, EAL, "Cannot create runtime directory\n");
830 if (eal_adjust_config(internal_conf) != 0) {
836 if (eal_check_common_options(internal_conf) != 0) {
843 argv[optind-1] = prgname;
847 /* restore getopt lib */
856 check_socket(const struct rte_memseg_list *msl, void *arg)
858 int *socket_id = arg;
863 return *socket_id == msl->socket_id;
867 eal_check_mem_on_local_socket(void)
870 const struct rte_config *config = rte_eal_get_configuration();
872 socket_id = rte_lcore_to_socket_id(config->master_lcore);
874 if (rte_memseg_list_walk(check_socket, &socket_id) == 0)
875 RTE_LOG(WARNING, EAL, "WARNING: Master core has no memory on local socket!\n");
879 sync_func(__rte_unused void *arg)
885 * Request iopl privilege for all RPL, returns 0 on success
886 * iopl() call is mostly for the i386 architecture. For other architectures,
887 * return -1 to indicate IO privilege can't be changed in this way.
890 rte_eal_iopl_init(void)
892 #if defined(RTE_ARCH_X86)
900 static int rte_eal_vfio_setup(void)
902 if (rte_vfio_enable("vfio"))
909 static void rte_eal_init_alert(const char *msg)
911 fprintf(stderr, "EAL: FATAL: %s\n", msg);
912 RTE_LOG(ERR, EAL, "%s\n", msg);
916 * On Linux 3.6+, even if VFIO is not loaded, whenever IOMMU is enabled in the
917 * BIOS and in the kernel, /sys/kernel/iommu_groups path will contain kernel
918 * IOMMU groups. If IOMMU is not enabled, that path would be empty.
919 * Therefore, checking if the path is empty will tell us if IOMMU is enabled.
922 is_iommu_enabled(void)
924 DIR *dir = opendir(KERNEL_IOMMU_GROUPS_PATH);
928 /* if directory doesn't exist, assume IOMMU is not enabled */
932 while ((d = readdir(dir)) != NULL) {
933 /* skip dot and dot-dot */
942 /* Launch threads, called at application init(). */
944 rte_eal_init(int argc, char **argv)
948 static rte_atomic32_t run_once = RTE_ATOMIC32_INIT(0);
950 static char logid[PATH_MAX];
951 char cpuset[RTE_CPU_AFFINITY_STR_LEN];
952 char thread_name[RTE_MAX_THREAD_NAME_LEN];
954 const struct rte_config *config = rte_eal_get_configuration();
955 struct internal_config *internal_conf =
956 eal_get_internal_configuration();
958 /* checks if the machine is adequate */
959 if (!rte_cpu_is_supported()) {
960 rte_eal_init_alert("unsupported cpu type.");
965 if (!rte_atomic32_test_and_set(&run_once)) {
966 rte_eal_init_alert("already called initialization.");
967 rte_errno = EALREADY;
971 p = strrchr(argv[0], '/');
972 strlcpy(logid, p ? p + 1 : argv[0], sizeof(logid));
973 thread_id = pthread_self();
975 eal_reset_internal_config(internal_conf);
977 /* set log level as early as possible */
978 eal_log_level_parse(argc, argv);
980 /* clone argv to report out later in telemetry */
981 eal_save_args(argc, argv);
983 if (rte_eal_cpu_init() < 0) {
984 rte_eal_init_alert("Cannot detect lcores.");
989 fctret = eal_parse_args(argc, argv);
991 rte_eal_init_alert("Invalid 'command line' arguments.");
993 rte_atomic32_clear(&run_once);
997 if (eal_plugins_init() < 0) {
998 rte_eal_init_alert("Cannot init plugins");
1000 rte_atomic32_clear(&run_once);
1004 if (eal_trace_init() < 0) {
1005 rte_eal_init_alert("Cannot init trace");
1010 if (eal_option_device_parse()) {
1012 rte_atomic32_clear(&run_once);
1016 if (rte_config_init() < 0) {
1017 rte_eal_init_alert("Cannot init config");
1021 if (rte_eal_intr_init() < 0) {
1022 rte_eal_init_alert("Cannot init interrupt-handling thread");
1026 if (rte_eal_alarm_init() < 0) {
1027 rte_eal_init_alert("Cannot init alarm");
1028 /* rte_eal_alarm_init sets rte_errno on failure. */
1032 /* Put mp channel init before bus scan so that we can init the vdev
1033 * bus through mp channel in the secondary process before the bus scan.
1035 if (rte_mp_channel_init() < 0 && rte_errno != ENOTSUP) {
1036 rte_eal_init_alert("failed to init mp channel");
1037 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
1043 /* register multi-process action callbacks for hotplug */
1044 if (eal_mp_dev_hotplug_init() < 0) {
1045 rte_eal_init_alert("failed to register mp callback for hotplug");
1049 if (rte_bus_scan()) {
1050 rte_eal_init_alert("Cannot scan the buses for devices");
1052 rte_atomic32_clear(&run_once);
1056 phys_addrs = rte_eal_using_phys_addrs() != 0;
1058 /* if no EAL option "--iova-mode=<pa|va>", use bus IOVA scheme */
1059 if (internal_conf->iova_mode == RTE_IOVA_DC) {
1060 /* autodetect the IOVA mapping mode */
1061 enum rte_iova_mode iova_mode = rte_bus_get_iommu_class();
1063 if (iova_mode == RTE_IOVA_DC) {
1064 RTE_LOG(DEBUG, EAL, "Buses did not request a specific IOVA mode.\n");
1067 /* if we have no access to physical addresses,
1068 * pick IOVA as VA mode.
1070 iova_mode = RTE_IOVA_VA;
1071 RTE_LOG(DEBUG, EAL, "Physical addresses are unavailable, selecting IOVA as VA mode.\n");
1072 #if defined(RTE_LIBRTE_KNI) && LINUX_VERSION_CODE >= KERNEL_VERSION(4, 10, 0)
1073 } else if (rte_eal_check_module("rte_kni") == 1) {
1074 iova_mode = RTE_IOVA_PA;
1075 RTE_LOG(DEBUG, EAL, "KNI is loaded, selecting IOVA as PA mode for better KNI performance.\n");
1077 } else if (is_iommu_enabled()) {
1078 /* we have an IOMMU, pick IOVA as VA mode */
1079 iova_mode = RTE_IOVA_VA;
1080 RTE_LOG(DEBUG, EAL, "IOMMU is available, selecting IOVA as VA mode.\n");
1082 /* physical addresses available, and no IOMMU
1083 * found, so pick IOVA as PA.
1085 iova_mode = RTE_IOVA_PA;
1086 RTE_LOG(DEBUG, EAL, "IOMMU is not available, selecting IOVA as PA mode.\n");
1089 #if defined(RTE_LIBRTE_KNI) && LINUX_VERSION_CODE < KERNEL_VERSION(4, 10, 0)
1090 /* Workaround for KNI which requires physical address to work
1093 if (iova_mode == RTE_IOVA_VA &&
1094 rte_eal_check_module("rte_kni") == 1) {
1096 iova_mode = RTE_IOVA_PA;
1097 RTE_LOG(WARNING, EAL, "Forcing IOVA as 'PA' because KNI module is loaded\n");
1099 RTE_LOG(DEBUG, EAL, "KNI can not work since physical addresses are unavailable\n");
1103 rte_eal_get_configuration()->iova_mode = iova_mode;
1105 rte_eal_get_configuration()->iova_mode =
1106 internal_conf->iova_mode;
1109 if (rte_eal_iova_mode() == RTE_IOVA_PA && !phys_addrs) {
1110 rte_eal_init_alert("Cannot use IOVA as 'PA' since physical addresses are not available");
1115 RTE_LOG(INFO, EAL, "Selected IOVA mode '%s'\n",
1116 rte_eal_iova_mode() == RTE_IOVA_PA ? "PA" : "VA");
1118 if (internal_conf->no_hugetlbfs == 0) {
1119 /* rte_config isn't initialized yet */
1120 ret = internal_conf->process_type == RTE_PROC_PRIMARY ?
1121 eal_hugepage_info_init() :
1122 eal_hugepage_info_read();
1124 rte_eal_init_alert("Cannot get hugepage information.");
1126 rte_atomic32_clear(&run_once);
1131 if (internal_conf->memory == 0 && internal_conf->force_sockets == 0) {
1132 if (internal_conf->no_hugetlbfs)
1133 internal_conf->memory = MEMSIZE_IF_NO_HUGE_PAGE;
1136 if (internal_conf->vmware_tsc_map == 1) {
1137 #ifdef RTE_LIBRTE_EAL_VMWARE_TSC_MAP_SUPPORT
1138 rte_cycles_vmware_tsc_map = 1;
1139 RTE_LOG (DEBUG, EAL, "Using VMWARE TSC MAP, "
1140 "you must have monitor_control.pseudo_perfctr = TRUE\n");
1142 RTE_LOG (WARNING, EAL, "Ignoring --vmware-tsc-map because "
1143 "RTE_LIBRTE_EAL_VMWARE_TSC_MAP_SUPPORT is not set\n");
1147 if (rte_eal_log_init(logid, internal_conf->syslog_facility) < 0) {
1148 rte_eal_init_alert("Cannot init logging.");
1150 rte_atomic32_clear(&run_once);
1155 if (rte_eal_vfio_setup() < 0) {
1156 rte_eal_init_alert("Cannot init VFIO");
1158 rte_atomic32_clear(&run_once);
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.
1166 if (rte_eal_memzone_init() < 0) {
1167 rte_eal_init_alert("Cannot init memzone");
1172 if (rte_eal_memory_init() < 0) {
1173 rte_eal_init_alert("Cannot init memory");
1178 /* the directories are locked during eal_hugepage_info_init */
1179 eal_hugedirs_unlock();
1181 if (rte_eal_malloc_heap_init() < 0) {
1182 rte_eal_init_alert("Cannot init malloc heap");
1187 if (rte_eal_tailqs_init() < 0) {
1188 rte_eal_init_alert("Cannot init tail queues for objects");
1193 if (rte_eal_timer_init() < 0) {
1194 rte_eal_init_alert("Cannot init HPET or TSC timers");
1195 rte_errno = ENOTSUP;
1199 eal_check_mem_on_local_socket();
1201 eal_thread_init_master(config->master_lcore);
1203 ret = eal_thread_dump_affinity(cpuset, sizeof(cpuset));
1205 RTE_LOG(DEBUG, EAL, "Master lcore %u is ready (tid=%zx;cpuset=[%s%s])\n",
1206 config->master_lcore, (uintptr_t)thread_id, cpuset,
1207 ret == 0 ? "" : "...");
1209 RTE_LCORE_FOREACH_SLAVE(i) {
1212 * create communication pipes between master thread
1215 if (pipe(lcore_config[i].pipe_master2slave) < 0)
1216 rte_panic("Cannot create pipe\n");
1217 if (pipe(lcore_config[i].pipe_slave2master) < 0)
1218 rte_panic("Cannot create pipe\n");
1220 lcore_config[i].state = WAIT;
1222 /* create a thread for each lcore */
1223 ret = pthread_create(&lcore_config[i].thread_id, NULL,
1224 eal_thread_loop, NULL);
1226 rte_panic("Cannot create thread\n");
1228 /* Set thread_name for aid in debugging. */
1229 snprintf(thread_name, sizeof(thread_name),
1230 "lcore-slave-%d", i);
1231 ret = rte_thread_setname(lcore_config[i].thread_id,
1235 "Cannot set name for lcore thread\n");
1239 * Launch a dummy function on all slave lcores, so that master lcore
1240 * knows they are all ready when this function returns.
1242 rte_eal_mp_remote_launch(sync_func, NULL, SKIP_MASTER);
1243 rte_eal_mp_wait_lcore();
1245 /* initialize services so vdevs register service during bus_probe. */
1246 ret = rte_service_init();
1248 rte_eal_init_alert("rte_service_init() failed");
1249 rte_errno = ENOEXEC;
1253 /* Probe all the buses and devices/drivers on them */
1254 if (rte_bus_probe()) {
1255 rte_eal_init_alert("Cannot probe devices");
1256 rte_errno = ENOTSUP;
1261 /* Register mp action after probe() so that we got enough info */
1262 if (rte_vfio_is_enabled("vfio") && vfio_mp_sync_setup() < 0)
1266 /* initialize default service/lcore mappings and start running. Ignore
1267 * -ENOTSUP, as it indicates no service coremask passed to EAL.
1269 ret = rte_service_start_with_defaults();
1270 if (ret < 0 && ret != -ENOTSUP) {
1271 rte_errno = ENOEXEC;
1276 * Clean up unused files in runtime directory. We do this at the end of
1277 * init and not at the beginning because we want to clean stuff up
1278 * whether we are primary or secondary process, but we cannot remove
1279 * primary process' files because secondary should be able to run even
1280 * if primary process is dead.
1282 * In no_shconf mode, no runtime directory is created in the first
1283 * place, so no cleanup needed.
1285 if (!internal_conf->no_shconf && eal_clean_runtime_dir() < 0) {
1286 rte_eal_init_alert("Cannot clear runtime directory");
1289 if (!internal_conf->no_telemetry) {
1290 const char *error_str = NULL;
1291 if (rte_telemetry_init(rte_eal_get_runtime_dir(),
1292 &internal_conf->ctrl_cpuset, &error_str)
1294 rte_eal_init_alert(error_str);
1297 if (error_str != NULL)
1298 RTE_LOG(NOTICE, EAL, "%s\n", error_str);
1301 eal_mcfg_complete();
1307 mark_freeable(const struct rte_memseg_list *msl, const struct rte_memseg *ms,
1308 void *arg __rte_unused)
1310 /* ms is const, so find this memseg */
1311 struct rte_memseg *found;
1316 found = rte_mem_virt2memseg(ms->addr, msl);
1318 found->flags &= ~RTE_MEMSEG_FLAG_DO_NOT_FREE;
1324 rte_eal_cleanup(void)
1326 /* if we're in a primary process, we need to mark hugepages as freeable
1327 * so that finalization can release them back to the system.
1329 struct internal_config *internal_conf =
1330 eal_get_internal_configuration();
1332 if (rte_eal_process_type() == RTE_PROC_PRIMARY)
1333 rte_memseg_walk(mark_freeable, NULL);
1334 rte_service_finalize();
1335 rte_mp_channel_cleanup();
1338 eal_cleanup_config(internal_conf);
1342 int rte_eal_create_uio_dev(void)
1344 const struct internal_config *internal_conf =
1345 eal_get_internal_configuration();
1347 return internal_conf->create_uio_dev;
1351 rte_eal_vfio_intr_mode(void)
1353 const struct internal_config *internal_conf =
1354 eal_get_internal_configuration();
1356 return internal_conf->vfio_intr_mode;
1360 rte_eal_check_module(const char *module_name)
1362 char sysfs_mod_name[PATH_MAX];
1366 if (NULL == module_name)
1369 /* Check if there is sysfs mounted */
1370 if (stat("/sys/module", &st) != 0) {
1371 RTE_LOG(DEBUG, EAL, "sysfs is not mounted! error %i (%s)\n",
1372 errno, strerror(errno));
1376 /* A module might be built-in, therefore try sysfs */
1377 n = snprintf(sysfs_mod_name, PATH_MAX, "/sys/module/%s", module_name);
1378 if (n < 0 || n > PATH_MAX) {
1379 RTE_LOG(DEBUG, EAL, "Could not format module path\n");
1383 if (stat(sysfs_mod_name, &st) != 0) {
1384 RTE_LOG(DEBUG, EAL, "Module %s not found! error %i (%s)\n",
1385 sysfs_mod_name, errno, strerror(errno));
1389 /* Module has been found */