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 <malloc_heap.h>
53 #include <telemetry_internal.h>
54 #include "eal_private.h"
55 #include "eal_thread.h"
56 #include "eal_internal_cfg.h"
57 #include "eal_filesystem.h"
58 #include "eal_hugepages.h"
59 #include "eal_memcfg.h"
60 #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 /* define fd variable here, because file needs to be kept open for the
73 * duration of the program, as we hold a write lock on it in the primary proc */
74 static int mem_cfg_fd = -1;
76 static struct flock wr_lock = {
79 .l_start = offsetof(struct rte_mem_config, memsegs),
80 .l_len = RTE_SIZEOF_FIELD(struct rte_mem_config, memsegs),
83 /* internal configuration (per-core) */
84 struct lcore_config lcore_config[RTE_MAX_LCORE];
86 /* used by rte_rdtsc() */
87 int rte_cycles_vmware_tsc_map;
89 static const char *default_runtime_dir = "/var/run";
92 eal_create_runtime_dir(void)
94 const char *directory = default_runtime_dir;
95 const char *xdg_runtime_dir = getenv("XDG_RUNTIME_DIR");
96 const char *fallback = "/tmp";
97 char run_dir[PATH_MAX];
102 /* try XDG path first, fall back to /tmp */
103 if (xdg_runtime_dir != NULL)
104 directory = xdg_runtime_dir;
106 directory = fallback;
108 /* create DPDK subdirectory under runtime dir */
109 ret = snprintf(tmp, sizeof(tmp), "%s/dpdk", directory);
110 if (ret < 0 || ret == sizeof(tmp)) {
111 RTE_LOG(ERR, EAL, "Error creating DPDK runtime path name\n");
115 /* create prefix-specific subdirectory under DPDK runtime dir */
116 ret = snprintf(run_dir, sizeof(run_dir), "%s/%s",
117 tmp, eal_get_hugefile_prefix());
118 if (ret < 0 || ret == sizeof(run_dir)) {
119 RTE_LOG(ERR, EAL, "Error creating prefix-specific runtime path name\n");
123 /* create the path if it doesn't exist. no "mkdir -p" here, so do it
126 ret = mkdir(tmp, 0700);
127 if (ret < 0 && errno != EEXIST) {
128 RTE_LOG(ERR, EAL, "Error creating '%s': %s\n",
129 tmp, strerror(errno));
133 ret = mkdir(run_dir, 0700);
134 if (ret < 0 && errno != EEXIST) {
135 RTE_LOG(ERR, EAL, "Error creating '%s': %s\n",
136 run_dir, strerror(errno));
140 if (eal_set_runtime_dir(run_dir, sizeof(run_dir)))
147 eal_clean_runtime_dir(void)
149 const char *runtime_dir = rte_eal_get_runtime_dir();
151 struct dirent *dirent;
152 int dir_fd, fd, lck_result;
153 static const char * const filters[] = {
159 dir = opendir(runtime_dir);
161 RTE_LOG(ERR, EAL, "Unable to open runtime directory %s\n",
167 /* lock the directory before doing anything, to avoid races */
168 if (flock(dir_fd, LOCK_EX) < 0) {
169 RTE_LOG(ERR, EAL, "Unable to lock runtime directory %s\n",
174 dirent = readdir(dir);
176 RTE_LOG(ERR, EAL, "Unable to read runtime directory %s\n",
181 while (dirent != NULL) {
185 /* skip files that don't match the patterns */
186 for (f_idx = 0; f_idx < RTE_DIM(filters); f_idx++) {
187 const char *filter = filters[f_idx];
189 if (fnmatch(filter, dirent->d_name, 0) == 0) {
195 dirent = readdir(dir);
199 /* try and lock the file */
200 fd = openat(dir_fd, dirent->d_name, O_RDONLY);
202 /* skip to next file */
204 dirent = readdir(dir);
208 /* non-blocking lock */
209 lck_result = flock(fd, LOCK_EX | LOCK_NB);
211 /* if lock succeeds, remove the file */
212 if (lck_result != -1)
213 unlinkat(dir_fd, dirent->d_name, 0);
215 dirent = readdir(dir);
218 /* closedir closes dir_fd and drops the lock */
226 RTE_LOG(ERR, EAL, "Error while clearing runtime dir: %s\n",
232 /* parse a sysfs (or other) file containing one integer value */
234 eal_parse_sysfs_value(const char *filename, unsigned long *val)
240 if ((f = fopen(filename, "r")) == NULL) {
241 RTE_LOG(ERR, EAL, "%s(): cannot open sysfs value %s\n",
246 if (fgets(buf, sizeof(buf), f) == NULL) {
247 RTE_LOG(ERR, EAL, "%s(): cannot read sysfs value %s\n",
252 *val = strtoul(buf, &end, 0);
253 if ((buf[0] == '\0') || (end == NULL) || (*end != '\n')) {
254 RTE_LOG(ERR, EAL, "%s(): cannot parse sysfs value %s\n",
264 /* create memory configuration in shared/mmap memory. Take out
265 * a write lock on the memsegs, so we can auto-detect primary/secondary.
266 * This means we never close the file while running (auto-close on exit).
267 * We also don't lock the whole file, so that in future we can use read-locks
268 * on other parts, e.g. memzones, to detect if there are running secondary
271 rte_eal_config_create(void)
273 struct rte_config *config = rte_eal_get_configuration();
274 size_t page_sz = sysconf(_SC_PAGE_SIZE);
275 size_t cfg_len = sizeof(*config->mem_config);
276 size_t cfg_len_aligned = RTE_ALIGN(cfg_len, page_sz);
277 void *rte_mem_cfg_addr, *mapped_mem_cfg_addr;
279 const struct internal_config *internal_conf =
280 eal_get_internal_configuration();
282 const char *pathname = eal_runtime_config_path();
284 if (internal_conf->no_shconf)
287 /* map the config before hugepage address so that we don't waste a page */
288 if (internal_conf->base_virtaddr != 0)
289 rte_mem_cfg_addr = (void *)
290 RTE_ALIGN_FLOOR(internal_conf->base_virtaddr -
291 sizeof(struct rte_mem_config), page_sz);
293 rte_mem_cfg_addr = NULL;
296 mem_cfg_fd = open(pathname, O_RDWR | O_CREAT, 0600);
297 if (mem_cfg_fd < 0) {
298 RTE_LOG(ERR, EAL, "Cannot open '%s' for rte_mem_config\n",
304 retval = ftruncate(mem_cfg_fd, cfg_len);
308 RTE_LOG(ERR, EAL, "Cannot resize '%s' for rte_mem_config\n",
313 retval = fcntl(mem_cfg_fd, F_SETLK, &wr_lock);
317 RTE_LOG(ERR, EAL, "Cannot create lock on '%s'. Is another primary "
318 "process running?\n", pathname);
322 /* reserve space for config */
323 rte_mem_cfg_addr = eal_get_virtual_area(rte_mem_cfg_addr,
324 &cfg_len_aligned, page_sz, 0, 0);
325 if (rte_mem_cfg_addr == NULL) {
326 RTE_LOG(ERR, EAL, "Cannot mmap memory for rte_config\n");
332 /* remap the actual file into the space we've just reserved */
333 mapped_mem_cfg_addr = mmap(rte_mem_cfg_addr,
334 cfg_len_aligned, PROT_READ | PROT_WRITE,
335 MAP_SHARED | MAP_FIXED, mem_cfg_fd, 0);
336 if (mapped_mem_cfg_addr == MAP_FAILED) {
337 munmap(rte_mem_cfg_addr, cfg_len);
340 RTE_LOG(ERR, EAL, "Cannot remap memory for rte_config\n");
344 memcpy(rte_mem_cfg_addr, config->mem_config, sizeof(struct rte_mem_config));
345 config->mem_config = rte_mem_cfg_addr;
347 /* store address of the config in the config itself so that secondary
348 * processes could later map the config into this exact location
350 config->mem_config->mem_cfg_addr = (uintptr_t) rte_mem_cfg_addr;
351 config->mem_config->dma_maskbits = 0;
356 /* attach to an existing shared memory config */
358 rte_eal_config_attach(void)
360 struct rte_config *config = rte_eal_get_configuration();
361 struct rte_mem_config *mem_config;
362 const struct internal_config *internal_conf =
363 eal_get_internal_configuration();
365 const char *pathname = eal_runtime_config_path();
367 if (internal_conf->no_shconf)
371 mem_cfg_fd = open(pathname, O_RDWR);
372 if (mem_cfg_fd < 0) {
373 RTE_LOG(ERR, EAL, "Cannot open '%s' for rte_mem_config\n",
379 /* map it as read-only first */
380 mem_config = (struct rte_mem_config *) mmap(NULL, sizeof(*mem_config),
381 PROT_READ, MAP_SHARED, mem_cfg_fd, 0);
382 if (mem_config == MAP_FAILED) {
385 RTE_LOG(ERR, EAL, "Cannot mmap memory for rte_config! error %i (%s)\n",
386 errno, strerror(errno));
390 config->mem_config = mem_config;
395 /* reattach the shared config at exact memory location primary process has it */
397 rte_eal_config_reattach(void)
399 struct rte_config *config = rte_eal_get_configuration();
400 struct rte_mem_config *mem_config;
401 void *rte_mem_cfg_addr;
402 const struct internal_config *internal_conf =
403 eal_get_internal_configuration();
405 if (internal_conf->no_shconf)
408 /* save the address primary process has mapped shared config to */
410 (void *) (uintptr_t) config->mem_config->mem_cfg_addr;
412 /* unmap original config */
413 munmap(config->mem_config, sizeof(struct rte_mem_config));
415 /* remap the config at proper address */
416 mem_config = (struct rte_mem_config *) mmap(rte_mem_cfg_addr,
417 sizeof(*mem_config), PROT_READ | PROT_WRITE, MAP_SHARED,
423 if (mem_config == MAP_FAILED || mem_config != rte_mem_cfg_addr) {
424 if (mem_config != MAP_FAILED) {
425 /* errno is stale, don't use */
426 RTE_LOG(ERR, EAL, "Cannot mmap memory for rte_config at [%p], got [%p]"
427 " - please use '--" OPT_BASE_VIRTADDR
428 "' option\n", rte_mem_cfg_addr, mem_config);
429 munmap(mem_config, sizeof(struct rte_mem_config));
432 RTE_LOG(ERR, EAL, "Cannot mmap memory for rte_config! error %i (%s)\n",
433 errno, strerror(errno));
437 config->mem_config = mem_config;
442 /* Detect if we are a primary or a secondary process */
444 eal_proc_type_detect(void)
446 enum rte_proc_type_t ptype = RTE_PROC_PRIMARY;
447 const char *pathname = eal_runtime_config_path();
448 const struct internal_config *internal_conf =
449 eal_get_internal_configuration();
451 /* if there no shared config, there can be no secondary processes */
452 if (!internal_conf->no_shconf) {
453 /* if we can open the file but not get a write-lock we are a
454 * secondary process. NOTE: if we get a file handle back, we
455 * keep that open and don't close it to prevent a race condition
456 * between multiple opens.
458 if (((mem_cfg_fd = open(pathname, O_RDWR)) >= 0) &&
459 (fcntl(mem_cfg_fd, F_SETLK, &wr_lock) < 0))
460 ptype = RTE_PROC_SECONDARY;
463 RTE_LOG(INFO, EAL, "Auto-detected process type: %s\n",
464 ptype == RTE_PROC_PRIMARY ? "PRIMARY" : "SECONDARY");
469 /* Sets up rte_config structure with the pointer to shared memory config.*/
471 rte_config_init(void)
473 struct rte_config *config = rte_eal_get_configuration();
474 const struct internal_config *internal_conf =
475 eal_get_internal_configuration();
477 config->process_type = internal_conf->process_type;
479 switch (config->process_type) {
480 case RTE_PROC_PRIMARY:
481 if (rte_eal_config_create() < 0)
483 eal_mcfg_update_from_internal();
485 case RTE_PROC_SECONDARY:
486 if (rte_eal_config_attach() < 0)
488 eal_mcfg_wait_complete();
489 if (eal_mcfg_check_version() < 0) {
490 RTE_LOG(ERR, EAL, "Primary and secondary process DPDK version mismatch\n");
493 if (rte_eal_config_reattach() < 0)
495 if (!__rte_mp_enable()) {
496 RTE_LOG(ERR, EAL, "Primary process refused secondary attachment\n");
499 eal_mcfg_update_internal();
502 case RTE_PROC_INVALID:
503 RTE_LOG(ERR, EAL, "Invalid process type %d\n",
504 config->process_type);
511 /* Unlocks hugepage directories that were locked by eal_hugepage_info_init */
513 eal_hugedirs_unlock(void)
516 struct internal_config *internal_conf =
517 eal_get_internal_configuration();
519 for (i = 0; i < MAX_HUGEPAGE_SIZES; i++)
521 /* skip uninitialized */
522 if (internal_conf->hugepage_info[i].lock_descriptor < 0)
524 /* unlock hugepage file */
525 flock(internal_conf->hugepage_info[i].lock_descriptor, LOCK_UN);
526 close(internal_conf->hugepage_info[i].lock_descriptor);
527 /* reset the field */
528 internal_conf->hugepage_info[i].lock_descriptor = -1;
534 eal_usage(const char *prgname)
536 rte_usage_hook_t hook = eal_get_application_usage_hook();
538 printf("\nUsage: %s ", prgname);
540 printf("EAL Linux options:\n"
541 " --"OPT_SOCKET_MEM" Memory to allocate on sockets (comma separated values)\n"
542 " --"OPT_SOCKET_LIMIT" Limit memory allocation on sockets (comma separated values)\n"
543 " --"OPT_HUGE_DIR" Directory where hugetlbfs is mounted\n"
544 " --"OPT_FILE_PREFIX" Prefix for hugepage filenames\n"
545 " --"OPT_CREATE_UIO_DEV" Create /dev/uioX (usually done by hotplug)\n"
546 " --"OPT_VFIO_INTR" Interrupt mode for VFIO (legacy|msi|msix)\n"
547 " --"OPT_VFIO_VF_TOKEN" VF token (UUID) shared between SR-IOV PF and VFs\n"
548 " --"OPT_LEGACY_MEM" Legacy memory mode (no dynamic allocation, contiguous segments)\n"
549 " --"OPT_SINGLE_FILE_SEGMENTS" Put all hugepage memory in single files\n"
550 " --"OPT_MATCH_ALLOCATIONS" Free hugepages exactly as allocated\n"
552 /* Allow the application to print its usage message too if hook is set */
554 printf("===== Application Usage =====\n\n");
560 eal_parse_socket_arg(char *strval, volatile uint64_t *socket_arg)
562 char * arg[RTE_MAX_NUMA_NODES];
566 len = strnlen(strval, SOCKET_MEM_STRLEN);
567 if (len == SOCKET_MEM_STRLEN) {
568 RTE_LOG(ERR, EAL, "--socket-mem is too long\n");
572 /* all other error cases will be caught later */
573 if (!isdigit(strval[len-1]))
576 /* split the optarg into separate socket values */
577 arg_num = rte_strsplit(strval, len,
578 arg, RTE_MAX_NUMA_NODES, ',');
580 /* if split failed, or 0 arguments */
584 /* parse each defined socket option */
586 for (i = 0; i < arg_num; i++) {
589 val = strtoull(arg[i], &end, 10);
591 /* check for invalid input */
593 (arg[i][0] == '\0') || (end == NULL) || (*end != '\0'))
603 eal_parse_vfio_intr(const char *mode)
605 struct internal_config *internal_conf =
606 eal_get_internal_configuration();
610 enum rte_intr_mode value;
612 { "legacy", RTE_INTR_MODE_LEGACY },
613 { "msi", RTE_INTR_MODE_MSI },
614 { "msix", RTE_INTR_MODE_MSIX },
617 for (i = 0; i < RTE_DIM(map); i++) {
618 if (!strcmp(mode, map[i].name)) {
619 internal_conf->vfio_intr_mode = map[i].value;
627 eal_parse_vfio_vf_token(const char *vf_token)
629 struct internal_config *cfg = eal_get_internal_configuration();
632 if (!rte_uuid_parse(vf_token, uuid)) {
633 rte_uuid_copy(cfg->vfio_vf_token, uuid);
640 /* Parse the arguments for --log-level only */
642 eal_log_level_parse(int argc, char **argv)
647 const int old_optind = optind;
648 const int old_optopt = optopt;
649 char * const old_optarg = optarg;
650 struct internal_config *internal_conf =
651 eal_get_internal_configuration();
656 while ((opt = getopt_long(argc, argvopt, eal_short_options,
657 eal_long_options, &option_index)) != EOF) {
661 /* getopt is not happy, stop right now */
665 ret = (opt == OPT_LOG_LEVEL_NUM) ?
666 eal_parse_common_option(opt, optarg, internal_conf) : 0;
668 /* common parser is not happy */
673 /* restore getopt lib */
679 /* Parse the argument given in the command line of the application */
681 eal_parse_args(int argc, char **argv)
686 char *prgname = argv[0];
687 const int old_optind = optind;
688 const int old_optopt = optopt;
689 char * const old_optarg = optarg;
690 struct internal_config *internal_conf =
691 eal_get_internal_configuration();
696 while ((opt = getopt_long(argc, argvopt, eal_short_options,
697 eal_long_options, &option_index)) != EOF) {
699 /* getopt didn't recognise the option */
706 /* eal_log_level_parse() already handled this option */
707 if (opt == OPT_LOG_LEVEL_NUM)
710 ret = eal_parse_common_option(opt, optarg, internal_conf);
711 /* common parser is not happy */
717 /* common parser handled this option */
726 case OPT_HUGE_DIR_NUM:
728 char *hdir = strdup(optarg);
730 RTE_LOG(ERR, EAL, "Could not store hugepage directory\n");
732 /* free old hugepage dir */
733 if (internal_conf->hugepage_dir != NULL)
734 free(internal_conf->hugepage_dir);
735 internal_conf->hugepage_dir = hdir;
739 case OPT_FILE_PREFIX_NUM:
741 char *prefix = strdup(optarg);
743 RTE_LOG(ERR, EAL, "Could not store file prefix\n");
745 /* free old prefix */
746 if (internal_conf->hugefile_prefix != NULL)
747 free(internal_conf->hugefile_prefix);
748 internal_conf->hugefile_prefix = prefix;
752 case OPT_SOCKET_MEM_NUM:
753 if (eal_parse_socket_arg(optarg,
754 internal_conf->socket_mem) < 0) {
755 RTE_LOG(ERR, EAL, "invalid parameters for --"
756 OPT_SOCKET_MEM "\n");
761 internal_conf->force_sockets = 1;
764 case OPT_SOCKET_LIMIT_NUM:
765 if (eal_parse_socket_arg(optarg,
766 internal_conf->socket_limit) < 0) {
767 RTE_LOG(ERR, EAL, "invalid parameters for --"
768 OPT_SOCKET_LIMIT "\n");
773 internal_conf->force_socket_limits = 1;
776 case OPT_VFIO_INTR_NUM:
777 if (eal_parse_vfio_intr(optarg) < 0) {
778 RTE_LOG(ERR, EAL, "invalid parameters for --"
786 case OPT_VFIO_VF_TOKEN_NUM:
787 if (eal_parse_vfio_vf_token(optarg) < 0) {
788 RTE_LOG(ERR, EAL, "invalid parameters for --"
789 OPT_VFIO_VF_TOKEN "\n");
796 case OPT_CREATE_UIO_DEV_NUM:
797 internal_conf->create_uio_dev = 1;
800 case OPT_MBUF_POOL_OPS_NAME_NUM:
802 char *ops_name = strdup(optarg);
803 if (ops_name == NULL)
804 RTE_LOG(ERR, EAL, "Could not store mbuf pool ops name\n");
806 /* free old ops name */
807 if (internal_conf->user_mbuf_pool_ops_name !=
809 free(internal_conf->user_mbuf_pool_ops_name);
811 internal_conf->user_mbuf_pool_ops_name =
816 case OPT_MATCH_ALLOCATIONS_NUM:
817 internal_conf->match_allocations = 1;
821 if (opt < OPT_LONG_MIN_NUM && isprint(opt)) {
822 RTE_LOG(ERR, EAL, "Option %c is not supported "
824 } else if (opt >= OPT_LONG_MIN_NUM &&
825 opt < OPT_LONG_MAX_NUM) {
826 RTE_LOG(ERR, EAL, "Option %s is not supported "
828 eal_long_options[option_index].name);
830 RTE_LOG(ERR, EAL, "Option %d is not supported "
839 /* create runtime data directory. In no_shconf mode, skip any errors */
840 if (eal_create_runtime_dir() < 0) {
841 if (internal_conf->no_shconf == 0) {
842 RTE_LOG(ERR, EAL, "Cannot create runtime directory\n");
846 RTE_LOG(WARNING, EAL, "No DPDK runtime directory created\n");
849 if (eal_adjust_config(internal_conf) != 0) {
855 if (eal_check_common_options(internal_conf) != 0) {
862 argv[optind-1] = prgname;
866 /* restore getopt lib */
875 check_socket(const struct rte_memseg_list *msl, void *arg)
877 int *socket_id = arg;
882 return *socket_id == msl->socket_id;
886 eal_check_mem_on_local_socket(void)
889 const struct rte_config *config = rte_eal_get_configuration();
891 socket_id = rte_lcore_to_socket_id(config->main_lcore);
893 if (rte_memseg_list_walk(check_socket, &socket_id) == 0)
894 RTE_LOG(WARNING, EAL, "WARNING: Main 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 uint32_t run_once;
968 uint32_t has_run = 0;
970 static char logid[PATH_MAX];
971 char cpuset[RTE_CPU_AFFINITY_STR_LEN];
972 char thread_name[RTE_MAX_THREAD_NAME_LEN];
974 const struct rte_config *config = rte_eal_get_configuration();
975 struct internal_config *internal_conf =
976 eal_get_internal_configuration();
978 /* checks if the machine is adequate */
979 if (!rte_cpu_is_supported()) {
980 rte_eal_init_alert("unsupported cpu type.");
985 if (!__atomic_compare_exchange_n(&run_once, &has_run, 1, 0,
986 __ATOMIC_RELAXED, __ATOMIC_RELAXED)) {
987 rte_eal_init_alert("already called initialization.");
988 rte_errno = EALREADY;
992 p = strrchr(argv[0], '/');
993 strlcpy(logid, p ? p + 1 : argv[0], sizeof(logid));
994 thread_id = pthread_self();
996 eal_reset_internal_config(internal_conf);
998 /* set log level as early as possible */
999 eal_log_level_parse(argc, argv);
1001 /* clone argv to report out later in telemetry */
1002 eal_save_args(argc, argv);
1004 if (rte_eal_cpu_init() < 0) {
1005 rte_eal_init_alert("Cannot detect lcores.");
1006 rte_errno = ENOTSUP;
1010 fctret = eal_parse_args(argc, argv);
1012 rte_eal_init_alert("Invalid 'command line' arguments.");
1014 __atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
1018 if (eal_plugins_init() < 0) {
1019 rte_eal_init_alert("Cannot init plugins");
1021 __atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
1025 if (eal_trace_init() < 0) {
1026 rte_eal_init_alert("Cannot init trace");
1031 if (eal_option_device_parse()) {
1033 __atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
1037 if (rte_config_init() < 0) {
1038 rte_eal_init_alert("Cannot init config");
1042 if (rte_eal_intr_init() < 0) {
1043 rte_eal_init_alert("Cannot init interrupt-handling thread");
1047 if (rte_eal_alarm_init() < 0) {
1048 rte_eal_init_alert("Cannot init alarm");
1049 /* rte_eal_alarm_init sets rte_errno on failure. */
1053 /* Put mp channel init before bus scan so that we can init the vdev
1054 * bus through mp channel in the secondary process before the bus scan.
1056 if (rte_mp_channel_init() < 0 && rte_errno != ENOTSUP) {
1057 rte_eal_init_alert("failed to init mp channel");
1058 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
1064 /* register multi-process action callbacks for hotplug */
1065 if (eal_mp_dev_hotplug_init() < 0) {
1066 rte_eal_init_alert("failed to register mp callback for hotplug");
1070 if (rte_bus_scan()) {
1071 rte_eal_init_alert("Cannot scan the buses for devices");
1073 __atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
1077 phys_addrs = rte_eal_using_phys_addrs() != 0;
1079 /* if no EAL option "--iova-mode=<pa|va>", use bus IOVA scheme */
1080 if (internal_conf->iova_mode == RTE_IOVA_DC) {
1081 /* autodetect the IOVA mapping mode */
1082 enum rte_iova_mode iova_mode = rte_bus_get_iommu_class();
1084 if (iova_mode == RTE_IOVA_DC) {
1085 RTE_LOG(DEBUG, EAL, "Buses did not request a specific IOVA mode.\n");
1088 /* if we have no access to physical addresses,
1089 * pick IOVA as VA mode.
1091 iova_mode = RTE_IOVA_VA;
1092 RTE_LOG(DEBUG, EAL, "Physical addresses are unavailable, selecting IOVA as VA mode.\n");
1093 #if defined(RTE_LIB_KNI) && LINUX_VERSION_CODE >= KERNEL_VERSION(4, 10, 0)
1094 } else if (rte_eal_check_module("rte_kni") == 1) {
1095 iova_mode = RTE_IOVA_PA;
1096 RTE_LOG(DEBUG, EAL, "KNI is loaded, selecting IOVA as PA mode for better KNI performance.\n");
1098 } else if (is_iommu_enabled()) {
1099 /* we have an IOMMU, pick IOVA as VA mode */
1100 iova_mode = RTE_IOVA_VA;
1101 RTE_LOG(DEBUG, EAL, "IOMMU is available, selecting IOVA as VA mode.\n");
1103 /* physical addresses available, and no IOMMU
1104 * found, so pick IOVA as PA.
1106 iova_mode = RTE_IOVA_PA;
1107 RTE_LOG(DEBUG, EAL, "IOMMU is not available, selecting IOVA as PA mode.\n");
1110 #if defined(RTE_LIB_KNI) && LINUX_VERSION_CODE < KERNEL_VERSION(4, 10, 0)
1111 /* Workaround for KNI which requires physical address to work
1114 if (iova_mode == RTE_IOVA_VA &&
1115 rte_eal_check_module("rte_kni") == 1) {
1117 iova_mode = RTE_IOVA_PA;
1118 RTE_LOG(WARNING, EAL, "Forcing IOVA as 'PA' because KNI module is loaded\n");
1120 RTE_LOG(DEBUG, EAL, "KNI can not work since physical addresses are unavailable\n");
1124 rte_eal_get_configuration()->iova_mode = iova_mode;
1126 rte_eal_get_configuration()->iova_mode =
1127 internal_conf->iova_mode;
1130 if (rte_eal_iova_mode() == RTE_IOVA_PA && !phys_addrs) {
1131 rte_eal_init_alert("Cannot use IOVA as 'PA' since physical addresses are not available");
1136 RTE_LOG(INFO, EAL, "Selected IOVA mode '%s'\n",
1137 rte_eal_iova_mode() == RTE_IOVA_PA ? "PA" : "VA");
1139 if (internal_conf->no_hugetlbfs == 0) {
1140 /* rte_config isn't initialized yet */
1141 ret = internal_conf->process_type == RTE_PROC_PRIMARY ?
1142 eal_hugepage_info_init() :
1143 eal_hugepage_info_read();
1145 rte_eal_init_alert("Cannot get hugepage information.");
1147 __atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
1152 if (internal_conf->memory == 0 && internal_conf->force_sockets == 0) {
1153 if (internal_conf->no_hugetlbfs)
1154 internal_conf->memory = MEMSIZE_IF_NO_HUGE_PAGE;
1157 if (internal_conf->vmware_tsc_map == 1) {
1158 #ifdef RTE_LIBRTE_EAL_VMWARE_TSC_MAP_SUPPORT
1159 rte_cycles_vmware_tsc_map = 1;
1160 RTE_LOG (DEBUG, EAL, "Using VMWARE TSC MAP, "
1161 "you must have monitor_control.pseudo_perfctr = TRUE\n");
1163 RTE_LOG (WARNING, EAL, "Ignoring --vmware-tsc-map because "
1164 "RTE_LIBRTE_EAL_VMWARE_TSC_MAP_SUPPORT is not set\n");
1168 if (eal_log_init(logid, internal_conf->syslog_facility) < 0) {
1169 rte_eal_init_alert("Cannot init logging.");
1171 __atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
1176 if (rte_eal_vfio_setup() < 0) {
1177 rte_eal_init_alert("Cannot init VFIO");
1179 __atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
1183 /* in secondary processes, memory init may allocate additional fbarrays
1184 * not present in primary processes, so to avoid any potential issues,
1185 * initialize memzones first.
1187 if (rte_eal_memzone_init() < 0) {
1188 rte_eal_init_alert("Cannot init memzone");
1193 if (rte_eal_memory_init() < 0) {
1194 rte_eal_init_alert("Cannot init memory");
1199 /* the directories are locked during eal_hugepage_info_init */
1200 eal_hugedirs_unlock();
1202 if (rte_eal_malloc_heap_init() < 0) {
1203 rte_eal_init_alert("Cannot init malloc heap");
1208 if (rte_eal_tailqs_init() < 0) {
1209 rte_eal_init_alert("Cannot init tail queues for objects");
1214 if (rte_eal_timer_init() < 0) {
1215 rte_eal_init_alert("Cannot init HPET or TSC timers");
1216 rte_errno = ENOTSUP;
1220 eal_check_mem_on_local_socket();
1222 if (pthread_setaffinity_np(pthread_self(), sizeof(rte_cpuset_t),
1223 &lcore_config[config->main_lcore].cpuset) != 0) {
1224 rte_eal_init_alert("Cannot set affinity");
1228 __rte_thread_init(config->main_lcore,
1229 &lcore_config[config->main_lcore].cpuset);
1231 ret = eal_thread_dump_current_affinity(cpuset, sizeof(cpuset));
1232 RTE_LOG(DEBUG, EAL, "Main lcore %u is ready (tid=%zx;cpuset=[%s%s])\n",
1233 config->main_lcore, (uintptr_t)thread_id, cpuset,
1234 ret == 0 ? "" : "...");
1236 RTE_LCORE_FOREACH_WORKER(i) {
1239 * create communication pipes between main thread
1242 if (pipe(lcore_config[i].pipe_main2worker) < 0)
1243 rte_panic("Cannot create pipe\n");
1244 if (pipe(lcore_config[i].pipe_worker2main) < 0)
1245 rte_panic("Cannot create pipe\n");
1247 lcore_config[i].state = WAIT;
1249 /* create a thread for each lcore */
1250 ret = pthread_create(&lcore_config[i].thread_id, NULL,
1251 eal_thread_loop, NULL);
1253 rte_panic("Cannot create thread\n");
1255 /* Set thread_name for aid in debugging. */
1256 snprintf(thread_name, sizeof(thread_name),
1257 "lcore-worker-%d", i);
1258 ret = rte_thread_setname(lcore_config[i].thread_id,
1262 "Cannot set name for lcore thread\n");
1264 ret = pthread_setaffinity_np(lcore_config[i].thread_id,
1265 sizeof(rte_cpuset_t), &lcore_config[i].cpuset);
1267 rte_panic("Cannot set affinity\n");
1271 * Launch a dummy function on all worker lcores, so that main lcore
1272 * knows they are all ready when this function returns.
1274 rte_eal_mp_remote_launch(sync_func, NULL, SKIP_MAIN);
1275 rte_eal_mp_wait_lcore();
1277 /* initialize services so vdevs register service during bus_probe. */
1278 ret = rte_service_init();
1280 rte_eal_init_alert("rte_service_init() failed");
1285 /* Probe all the buses and devices/drivers on them */
1286 if (rte_bus_probe()) {
1287 rte_eal_init_alert("Cannot probe devices");
1288 rte_errno = ENOTSUP;
1293 /* Register mp action after probe() so that we got enough info */
1294 if (rte_vfio_is_enabled("vfio") && vfio_mp_sync_setup() < 0)
1298 /* initialize default service/lcore mappings and start running. Ignore
1299 * -ENOTSUP, as it indicates no service coremask passed to EAL.
1301 ret = rte_service_start_with_defaults();
1302 if (ret < 0 && ret != -ENOTSUP) {
1308 * Clean up unused files in runtime directory. We do this at the end of
1309 * init and not at the beginning because we want to clean stuff up
1310 * whether we are primary or secondary process, but we cannot remove
1311 * primary process' files because secondary should be able to run even
1312 * if primary process is dead.
1314 * In no_shconf mode, no runtime directory is created in the first
1315 * place, so no cleanup needed.
1317 if (!internal_conf->no_shconf && eal_clean_runtime_dir() < 0) {
1318 rte_eal_init_alert("Cannot clear runtime directory");
1321 if (rte_eal_process_type() == RTE_PROC_PRIMARY && !internal_conf->no_telemetry) {
1322 int tlog = rte_log_register_type_and_pick_level(
1323 "lib.telemetry", RTE_LOG_WARNING);
1325 tlog = RTE_LOGTYPE_EAL;
1326 if (rte_telemetry_init(rte_eal_get_runtime_dir(),
1328 &internal_conf->ctrl_cpuset, rte_log, tlog) != 0)
1332 eal_mcfg_complete();
1338 mark_freeable(const struct rte_memseg_list *msl, const struct rte_memseg *ms,
1339 void *arg __rte_unused)
1341 /* ms is const, so find this memseg */
1342 struct rte_memseg *found;
1347 found = rte_mem_virt2memseg(ms->addr, msl);
1349 found->flags &= ~RTE_MEMSEG_FLAG_DO_NOT_FREE;
1355 rte_eal_cleanup(void)
1357 /* if we're in a primary process, we need to mark hugepages as freeable
1358 * so that finalization can release them back to the system.
1360 struct internal_config *internal_conf =
1361 eal_get_internal_configuration();
1363 if (rte_eal_process_type() == RTE_PROC_PRIMARY &&
1364 internal_conf->hugepage_file.unlink_existing)
1365 rte_memseg_walk(mark_freeable, NULL);
1366 rte_service_finalize();
1367 rte_mp_channel_cleanup();
1368 /* after this point, any DPDK pointers will become dangling */
1369 rte_eal_memory_detach();
1370 rte_eal_alarm_cleanup();
1373 eal_cleanup_config(internal_conf);
1377 int rte_eal_create_uio_dev(void)
1379 const struct internal_config *internal_conf =
1380 eal_get_internal_configuration();
1382 return internal_conf->create_uio_dev;
1386 rte_eal_vfio_intr_mode(void)
1388 const struct internal_config *internal_conf =
1389 eal_get_internal_configuration();
1391 return internal_conf->vfio_intr_mode;
1395 rte_eal_vfio_get_vf_token(rte_uuid_t vf_token)
1397 struct internal_config *cfg = eal_get_internal_configuration();
1399 rte_uuid_copy(vf_token, cfg->vfio_vf_token);
1403 rte_eal_check_module(const char *module_name)
1405 char sysfs_mod_name[PATH_MAX];
1409 if (NULL == module_name)
1412 /* Check if there is sysfs mounted */
1413 if (stat("/sys/module", &st) != 0) {
1414 RTE_LOG(DEBUG, EAL, "sysfs is not mounted! error %i (%s)\n",
1415 errno, strerror(errno));
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 RTE_LOG(DEBUG, EAL, "Could not format module path\n");
1426 if (stat(sysfs_mod_name, &st) != 0) {
1427 RTE_LOG(DEBUG, EAL, "Module %s not found! error %i (%s)\n",
1428 sysfs_mod_name, errno, strerror(errno));
1432 /* Module has been found */