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)
29 #include <rte_compat.h>
30 #include <rte_common.h>
31 #include <rte_debug.h>
32 #include <rte_memory.h>
33 #include <rte_launch.h>
35 #include <rte_errno.h>
36 #include <rte_per_lcore.h>
37 #include <rte_lcore.h>
38 #include <rte_service_component.h>
40 #include <rte_random.h>
41 #include <rte_cycles.h>
42 #include <rte_string_fns.h>
43 #include <rte_cpuflags.h>
44 #include <rte_interrupts.h>
47 #include <rte_devargs.h>
48 #include <rte_version.h>
49 #include <rte_atomic.h>
50 #include <malloc_heap.h>
52 #include <rte_option.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_options.h"
62 #include "hotplug_mp.h"
64 #define MEMSIZE_IF_NO_HUGE_PAGE (64ULL * 1024ULL * 1024ULL)
66 #define SOCKET_MEM_STRLEN (RTE_MAX_NUMA_NODES * 10)
68 #define KERNEL_IOMMU_GROUPS_PATH "/sys/kernel/iommu_groups"
70 /* Allow the application to print its usage message too if set */
71 static rte_usage_hook_t rte_application_usage_hook = NULL;
73 /* early configuration structure, when memory config is not mmapped */
74 static struct rte_mem_config early_mem_config;
76 /* define fd variable here, because file needs to be kept open for the
77 * duration of the program, as we hold a write lock on it in the primary proc */
78 static int mem_cfg_fd = -1;
80 static struct flock wr_lock = {
83 .l_start = offsetof(struct rte_mem_config, memsegs),
84 .l_len = sizeof(early_mem_config.memsegs),
87 /* Address of global and public configuration */
88 static struct rte_config rte_config = {
89 .mem_config = &early_mem_config,
92 /* internal configuration (per-core) */
93 struct lcore_config lcore_config[RTE_MAX_LCORE];
95 /* internal configuration */
96 struct internal_config internal_config;
98 /* used by rte_rdtsc() */
99 int rte_cycles_vmware_tsc_map;
101 /* platform-specific runtime dir */
102 static char runtime_dir[PATH_MAX];
104 static const char *default_runtime_dir = "/var/run";
107 eal_create_runtime_dir(void)
109 const char *directory = default_runtime_dir;
110 const char *xdg_runtime_dir = getenv("XDG_RUNTIME_DIR");
111 const char *fallback = "/tmp";
116 /* try XDG path first, fall back to /tmp */
117 if (xdg_runtime_dir != NULL)
118 directory = xdg_runtime_dir;
120 directory = fallback;
122 /* create DPDK subdirectory under runtime dir */
123 ret = snprintf(tmp, sizeof(tmp), "%s/dpdk", directory);
124 if (ret < 0 || ret == sizeof(tmp)) {
125 RTE_LOG(ERR, EAL, "Error creating DPDK runtime path name\n");
129 /* create prefix-specific subdirectory under DPDK runtime dir */
130 ret = snprintf(runtime_dir, sizeof(runtime_dir), "%s/%s",
131 tmp, eal_get_hugefile_prefix());
132 if (ret < 0 || ret == sizeof(runtime_dir)) {
133 RTE_LOG(ERR, EAL, "Error creating prefix-specific runtime path name\n");
137 /* create the path if it doesn't exist. no "mkdir -p" here, so do it
140 ret = mkdir(tmp, 0700);
141 if (ret < 0 && errno != EEXIST) {
142 RTE_LOG(ERR, EAL, "Error creating '%s': %s\n",
143 tmp, strerror(errno));
147 ret = mkdir(runtime_dir, 0700);
148 if (ret < 0 && errno != EEXIST) {
149 RTE_LOG(ERR, EAL, "Error creating '%s': %s\n",
150 runtime_dir, strerror(errno));
158 eal_clean_runtime_dir(void)
161 struct dirent *dirent;
162 int dir_fd, fd, lck_result;
163 static const char * const filters[] = {
169 dir = opendir(runtime_dir);
171 RTE_LOG(ERR, EAL, "Unable to open runtime directory %s\n",
177 /* lock the directory before doing anything, to avoid races */
178 if (flock(dir_fd, LOCK_EX) < 0) {
179 RTE_LOG(ERR, EAL, "Unable to lock runtime directory %s\n",
184 dirent = readdir(dir);
186 RTE_LOG(ERR, EAL, "Unable to read runtime directory %s\n",
191 while (dirent != NULL) {
195 /* skip files that don't match the patterns */
196 for (f_idx = 0; f_idx < RTE_DIM(filters); f_idx++) {
197 const char *filter = filters[f_idx];
199 if (fnmatch(filter, dirent->d_name, 0) == 0) {
205 dirent = readdir(dir);
209 /* try and lock the file */
210 fd = openat(dir_fd, dirent->d_name, O_RDONLY);
212 /* skip to next file */
214 dirent = readdir(dir);
218 /* non-blocking lock */
219 lck_result = flock(fd, LOCK_EX | LOCK_NB);
221 /* if lock succeeds, remove the file */
222 if (lck_result != -1)
223 unlinkat(dir_fd, dirent->d_name, 0);
225 dirent = readdir(dir);
228 /* closedir closes dir_fd and drops the lock */
236 RTE_LOG(ERR, EAL, "Error while clearing runtime dir: %s\n",
243 rte_eal_get_runtime_dir(void)
248 /* Return user provided mbuf pool ops name */
250 rte_eal_mbuf_user_pool_ops(void)
252 return internal_config.user_mbuf_pool_ops_name;
255 /* Return a pointer to the configuration structure */
257 rte_eal_get_configuration(void)
263 rte_eal_iova_mode(void)
265 return rte_eal_get_configuration()->iova_mode;
268 /* parse a sysfs (or other) file containing one integer value */
270 eal_parse_sysfs_value(const char *filename, unsigned long *val)
276 if ((f = fopen(filename, "r")) == NULL) {
277 RTE_LOG(ERR, EAL, "%s(): cannot open sysfs value %s\n",
282 if (fgets(buf, sizeof(buf), f) == NULL) {
283 RTE_LOG(ERR, EAL, "%s(): cannot read sysfs value %s\n",
288 *val = strtoul(buf, &end, 0);
289 if ((buf[0] == '\0') || (end == NULL) || (*end != '\n')) {
290 RTE_LOG(ERR, EAL, "%s(): cannot parse sysfs value %s\n",
300 /* create memory configuration in shared/mmap memory. Take out
301 * a write lock on the memsegs, so we can auto-detect primary/secondary.
302 * This means we never close the file while running (auto-close on exit).
303 * We also don't lock the whole file, so that in future we can use read-locks
304 * on other parts, e.g. memzones, to detect if there are running secondary
307 rte_eal_config_create(void)
309 void *rte_mem_cfg_addr;
312 const char *pathname = eal_runtime_config_path();
314 if (internal_config.no_shconf)
317 /* map the config before hugepage address so that we don't waste a page */
318 if (internal_config.base_virtaddr != 0)
319 rte_mem_cfg_addr = (void *)
320 RTE_ALIGN_FLOOR(internal_config.base_virtaddr -
321 sizeof(struct rte_mem_config), sysconf(_SC_PAGE_SIZE));
323 rte_mem_cfg_addr = NULL;
326 mem_cfg_fd = open(pathname, O_RDWR | O_CREAT, 0600);
327 if (mem_cfg_fd < 0) {
328 RTE_LOG(ERR, EAL, "Cannot open '%s' for rte_mem_config\n",
334 retval = ftruncate(mem_cfg_fd, sizeof(*rte_config.mem_config));
338 RTE_LOG(ERR, EAL, "Cannot resize '%s' for rte_mem_config\n",
343 retval = fcntl(mem_cfg_fd, F_SETLK, &wr_lock);
347 RTE_LOG(ERR, EAL, "Cannot create lock on '%s'. Is another primary "
348 "process running?\n", pathname);
352 rte_mem_cfg_addr = mmap(rte_mem_cfg_addr, sizeof(*rte_config.mem_config),
353 PROT_READ | PROT_WRITE, MAP_SHARED, mem_cfg_fd, 0);
355 if (rte_mem_cfg_addr == MAP_FAILED){
358 RTE_LOG(ERR, EAL, "Cannot mmap memory for rte_config\n");
362 memcpy(rte_mem_cfg_addr, &early_mem_config, sizeof(early_mem_config));
363 rte_config.mem_config = rte_mem_cfg_addr;
365 /* store address of the config in the config itself so that secondary
366 * processes could later map the config into this exact location */
367 rte_config.mem_config->mem_cfg_addr = (uintptr_t) rte_mem_cfg_addr;
369 rte_config.mem_config->dma_maskbits = 0;
374 /* attach to an existing shared memory config */
376 rte_eal_config_attach(void)
378 struct rte_mem_config *mem_config;
380 const char *pathname = eal_runtime_config_path();
382 if (internal_config.no_shconf)
386 mem_cfg_fd = open(pathname, O_RDWR);
387 if (mem_cfg_fd < 0) {
388 RTE_LOG(ERR, EAL, "Cannot open '%s' for rte_mem_config\n",
394 /* map it as read-only first */
395 mem_config = (struct rte_mem_config *) mmap(NULL, sizeof(*mem_config),
396 PROT_READ, MAP_SHARED, mem_cfg_fd, 0);
397 if (mem_config == MAP_FAILED) {
400 RTE_LOG(ERR, EAL, "Cannot mmap memory for rte_config! error %i (%s)\n",
401 errno, strerror(errno));
405 rte_config.mem_config = mem_config;
410 /* reattach the shared config at exact memory location primary process has it */
412 rte_eal_config_reattach(void)
414 struct rte_mem_config *mem_config;
415 void *rte_mem_cfg_addr;
417 if (internal_config.no_shconf)
420 /* save the address primary process has mapped shared config to */
421 rte_mem_cfg_addr = (void *) (uintptr_t) rte_config.mem_config->mem_cfg_addr;
423 /* unmap original config */
424 munmap(rte_config.mem_config, sizeof(struct rte_mem_config));
426 /* remap the config at proper address */
427 mem_config = (struct rte_mem_config *) mmap(rte_mem_cfg_addr,
428 sizeof(*mem_config), PROT_READ | PROT_WRITE, MAP_SHARED,
434 if (mem_config == MAP_FAILED || mem_config != rte_mem_cfg_addr) {
435 if (mem_config != MAP_FAILED) {
436 /* errno is stale, don't use */
437 RTE_LOG(ERR, EAL, "Cannot mmap memory for rte_config at [%p], got [%p]"
438 " - please use '--base-virtaddr' option\n",
439 rte_mem_cfg_addr, mem_config);
440 munmap(mem_config, sizeof(struct rte_mem_config));
443 RTE_LOG(ERR, EAL, "Cannot mmap memory for rte_config! error %i (%s)\n",
444 errno, strerror(errno));
448 rte_config.mem_config = mem_config;
453 /* Detect if we are a primary or a secondary process */
455 eal_proc_type_detect(void)
457 enum rte_proc_type_t ptype = RTE_PROC_PRIMARY;
458 const char *pathname = eal_runtime_config_path();
460 /* if there no shared config, there can be no secondary processes */
461 if (!internal_config.no_shconf) {
462 /* if we can open the file but not get a write-lock we are a
463 * secondary process. NOTE: if we get a file handle back, we
464 * keep that open and don't close it to prevent a race condition
465 * between multiple opens.
467 if (((mem_cfg_fd = open(pathname, O_RDWR)) >= 0) &&
468 (fcntl(mem_cfg_fd, F_SETLK, &wr_lock) < 0))
469 ptype = RTE_PROC_SECONDARY;
472 RTE_LOG(INFO, EAL, "Auto-detected process type: %s\n",
473 ptype == RTE_PROC_PRIMARY ? "PRIMARY" : "SECONDARY");
478 /* Sets up rte_config structure with the pointer to shared memory config.*/
480 rte_config_init(void)
482 rte_config.process_type = internal_config.process_type;
484 switch (rte_config.process_type){
485 case RTE_PROC_PRIMARY:
486 if (rte_eal_config_create() < 0)
488 eal_mcfg_update_from_internal();
490 case RTE_PROC_SECONDARY:
491 if (rte_eal_config_attach() < 0)
493 eal_mcfg_wait_complete();
494 if (eal_mcfg_check_version() < 0) {
495 RTE_LOG(ERR, EAL, "Primary and secondary process DPDK version mismatch\n");
498 if (rte_eal_config_reattach() < 0)
500 eal_mcfg_update_internal();
503 case RTE_PROC_INVALID:
504 RTE_LOG(ERR, EAL, "Invalid process type %d\n",
505 rte_config.process_type);
512 /* Unlocks hugepage directories that were locked by eal_hugepage_info_init */
514 eal_hugedirs_unlock(void)
518 for (i = 0; i < MAX_HUGEPAGE_SIZES; i++)
520 /* skip uninitialized */
521 if (internal_config.hugepage_info[i].lock_descriptor < 0)
523 /* unlock hugepage file */
524 flock(internal_config.hugepage_info[i].lock_descriptor, LOCK_UN);
525 close(internal_config.hugepage_info[i].lock_descriptor);
526 /* reset the field */
527 internal_config.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)
619 enum rte_intr_mode value;
621 { "legacy", RTE_INTR_MODE_LEGACY },
622 { "msi", RTE_INTR_MODE_MSI },
623 { "msix", RTE_INTR_MODE_MSIX },
626 for (i = 0; i < RTE_DIM(map); i++) {
627 if (!strcmp(mode, map[i].name)) {
628 internal_config.vfio_intr_mode = map[i].value;
635 /* Parse the arguments for --log-level only */
637 eal_log_level_parse(int argc, char **argv)
642 const int old_optind = optind;
643 const int old_optopt = optopt;
644 char * const old_optarg = optarg;
649 while ((opt = getopt_long(argc, argvopt, eal_short_options,
650 eal_long_options, &option_index)) != EOF) {
654 /* getopt is not happy, stop right now */
658 ret = (opt == OPT_LOG_LEVEL_NUM) ?
659 eal_parse_common_option(opt, optarg, &internal_config) : 0;
661 /* common parser is not happy */
666 /* restore getopt lib */
672 /* Parse the argument given in the command line of the application */
674 eal_parse_args(int argc, char **argv)
679 char *prgname = argv[0];
680 const int old_optind = optind;
681 const int old_optopt = optopt;
682 char * const old_optarg = optarg;
688 while ((opt = getopt_long(argc, argvopt, eal_short_options,
689 eal_long_options, &option_index)) != EOF) {
692 * getopt didn't recognise the option, lets parse the
693 * registered options to see if the flag is valid
696 ret = rte_option_parse(argv[optind-1]);
705 ret = eal_parse_common_option(opt, optarg, &internal_config);
706 /* common parser is not happy */
712 /* common parser handled this option */
721 case OPT_HUGE_DIR_NUM:
723 char *hdir = strdup(optarg);
725 RTE_LOG(ERR, EAL, "Could not store hugepage directory\n");
727 /* free old hugepage dir */
728 if (internal_config.hugepage_dir != NULL)
729 free(internal_config.hugepage_dir);
730 internal_config.hugepage_dir = hdir;
734 case OPT_FILE_PREFIX_NUM:
736 char *prefix = strdup(optarg);
738 RTE_LOG(ERR, EAL, "Could not store file prefix\n");
740 /* free old prefix */
741 if (internal_config.hugefile_prefix != NULL)
742 free(internal_config.hugefile_prefix);
743 internal_config.hugefile_prefix = prefix;
747 case OPT_SOCKET_MEM_NUM:
748 if (eal_parse_socket_arg(optarg,
749 internal_config.socket_mem) < 0) {
750 RTE_LOG(ERR, EAL, "invalid parameters for --"
751 OPT_SOCKET_MEM "\n");
756 internal_config.force_sockets = 1;
759 case OPT_SOCKET_LIMIT_NUM:
760 if (eal_parse_socket_arg(optarg,
761 internal_config.socket_limit) < 0) {
762 RTE_LOG(ERR, EAL, "invalid parameters for --"
763 OPT_SOCKET_LIMIT "\n");
768 internal_config.force_socket_limits = 1;
771 case OPT_VFIO_INTR_NUM:
772 if (eal_parse_vfio_intr(optarg) < 0) {
773 RTE_LOG(ERR, EAL, "invalid parameters for --"
781 case OPT_CREATE_UIO_DEV_NUM:
782 internal_config.create_uio_dev = 1;
785 case OPT_MBUF_POOL_OPS_NAME_NUM:
787 char *ops_name = strdup(optarg);
788 if (ops_name == NULL)
789 RTE_LOG(ERR, EAL, "Could not store mbuf pool ops name\n");
791 /* free old ops name */
792 if (internal_config.user_mbuf_pool_ops_name !=
794 free(internal_config.user_mbuf_pool_ops_name);
796 internal_config.user_mbuf_pool_ops_name =
801 case OPT_MATCH_ALLOCATIONS_NUM:
802 internal_config.match_allocations = 1;
806 if (opt < OPT_LONG_MIN_NUM && isprint(opt)) {
807 RTE_LOG(ERR, EAL, "Option %c is not supported "
809 } else if (opt >= OPT_LONG_MIN_NUM &&
810 opt < OPT_LONG_MAX_NUM) {
811 RTE_LOG(ERR, EAL, "Option %s is not supported "
813 eal_long_options[option_index].name);
815 RTE_LOG(ERR, EAL, "Option %d is not supported "
824 /* create runtime data directory */
825 if (internal_config.no_shconf == 0 &&
826 eal_create_runtime_dir() < 0) {
827 RTE_LOG(ERR, EAL, "Cannot create runtime directory\n");
832 if (eal_adjust_config(&internal_config) != 0) {
838 if (eal_check_common_options(&internal_config) != 0) {
845 argv[optind-1] = prgname;
849 /* restore getopt lib */
858 check_socket(const struct rte_memseg_list *msl, void *arg)
860 int *socket_id = arg;
865 return *socket_id == msl->socket_id;
869 eal_check_mem_on_local_socket(void)
873 socket_id = rte_lcore_to_socket_id(rte_config.master_lcore);
875 if (rte_memseg_list_walk(check_socket, &socket_id) == 0)
876 RTE_LOG(WARNING, EAL, "WARNING: Master core has no memory on local socket!\n");
880 sync_func(__attribute__((unused)) void *arg)
886 * Request iopl privilege for all RPL, returns 0 on success
887 * iopl() call is mostly for the i386 architecture. For other architectures,
888 * return -1 to indicate IO privilege can't be changed in this way.
891 rte_eal_iopl_init(void)
893 #if defined(RTE_ARCH_X86)
901 static int rte_eal_vfio_setup(void)
903 if (rte_vfio_enable("vfio"))
910 static void rte_eal_init_alert(const char *msg)
912 fprintf(stderr, "EAL: FATAL: %s\n", msg);
913 RTE_LOG(ERR, EAL, "%s\n", msg);
917 * On Linux 3.6+, even if VFIO is not loaded, whenever IOMMU is enabled in the
918 * BIOS and in the kernel, /sys/kernel/iommu_groups path will contain kernel
919 * IOMMU groups. If IOMMU is not enabled, that path would be empty.
920 * Therefore, checking if the path is empty will tell us if IOMMU is enabled.
923 is_iommu_enabled(void)
925 DIR *dir = opendir(KERNEL_IOMMU_GROUPS_PATH);
929 /* if directory doesn't exist, assume IOMMU is not enabled */
933 while ((d = readdir(dir)) != NULL) {
934 /* skip dot and dot-dot */
943 /* Launch threads, called at application init(). */
945 rte_eal_init(int argc, char **argv)
949 static rte_atomic32_t run_once = RTE_ATOMIC32_INIT(0);
951 static char logid[PATH_MAX];
952 char cpuset[RTE_CPU_AFFINITY_STR_LEN];
953 char thread_name[RTE_MAX_THREAD_NAME_LEN];
956 /* checks if the machine is adequate */
957 if (!rte_cpu_is_supported()) {
958 rte_eal_init_alert("unsupported cpu type.");
963 if (!rte_atomic32_test_and_set(&run_once)) {
964 rte_eal_init_alert("already called initialization.");
965 rte_errno = EALREADY;
969 p = strrchr(argv[0], '/');
970 strlcpy(logid, p ? p + 1 : argv[0], sizeof(logid));
971 thread_id = pthread_self();
973 eal_reset_internal_config(&internal_config);
975 /* set log level as early as possible */
976 eal_log_level_parse(argc, argv);
978 if (rte_eal_cpu_init() < 0) {
979 rte_eal_init_alert("Cannot detect lcores.");
984 fctret = eal_parse_args(argc, argv);
986 rte_eal_init_alert("Invalid 'command line' arguments.");
988 rte_atomic32_clear(&run_once);
992 if (eal_plugins_init() < 0) {
993 rte_eal_init_alert("Cannot init plugins");
995 rte_atomic32_clear(&run_once);
999 if (eal_option_device_parse()) {
1001 rte_atomic32_clear(&run_once);
1005 if (rte_config_init() < 0) {
1006 rte_eal_init_alert("Cannot init config");
1010 if (rte_eal_intr_init() < 0) {
1011 rte_eal_init_alert("Cannot init interrupt-handling thread");
1015 if (rte_eal_alarm_init() < 0) {
1016 rte_eal_init_alert("Cannot init alarm");
1017 /* rte_eal_alarm_init sets rte_errno on failure. */
1021 /* Put mp channel init before bus scan so that we can init the vdev
1022 * bus through mp channel in the secondary process before the bus scan.
1024 if (rte_mp_channel_init() < 0 && rte_errno != ENOTSUP) {
1025 rte_eal_init_alert("failed to init mp channel");
1026 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
1032 /* register multi-process action callbacks for hotplug */
1033 if (eal_mp_dev_hotplug_init() < 0) {
1034 rte_eal_init_alert("failed to register mp callback for hotplug");
1038 if (rte_bus_scan()) {
1039 rte_eal_init_alert("Cannot scan the buses for devices");
1041 rte_atomic32_clear(&run_once);
1045 phys_addrs = rte_eal_using_phys_addrs() != 0;
1047 /* if no EAL option "--iova-mode=<pa|va>", use bus IOVA scheme */
1048 if (internal_config.iova_mode == RTE_IOVA_DC) {
1049 /* autodetect the IOVA mapping mode */
1050 enum rte_iova_mode iova_mode = rte_bus_get_iommu_class();
1052 if (iova_mode == RTE_IOVA_DC) {
1053 RTE_LOG(DEBUG, EAL, "Buses did not request a specific IOVA mode.\n");
1056 /* if we have no access to physical addresses,
1057 * pick IOVA as VA mode.
1059 iova_mode = RTE_IOVA_VA;
1060 RTE_LOG(DEBUG, EAL, "Physical addresses are unavailable, selecting IOVA as VA mode.\n");
1061 } else if (is_iommu_enabled()) {
1062 /* we have an IOMMU, pick IOVA as VA mode */
1063 iova_mode = RTE_IOVA_VA;
1064 RTE_LOG(DEBUG, EAL, "IOMMU is available, selecting IOVA as VA mode.\n");
1066 /* physical addresses available, and no IOMMU
1067 * found, so pick IOVA as PA.
1069 iova_mode = RTE_IOVA_PA;
1070 RTE_LOG(DEBUG, EAL, "IOMMU is not available, selecting IOVA as PA mode.\n");
1073 #ifdef RTE_LIBRTE_KNI
1074 /* Workaround for KNI which requires physical address to work */
1075 if (iova_mode == RTE_IOVA_VA &&
1076 rte_eal_check_module("rte_kni") == 1) {
1078 iova_mode = RTE_IOVA_PA;
1079 RTE_LOG(WARNING, EAL, "Forcing IOVA as 'PA' because KNI module is loaded\n");
1081 RTE_LOG(DEBUG, EAL, "KNI can not work since physical addresses are unavailable\n");
1085 rte_eal_get_configuration()->iova_mode = iova_mode;
1087 rte_eal_get_configuration()->iova_mode =
1088 internal_config.iova_mode;
1091 if (rte_eal_iova_mode() == RTE_IOVA_PA && !phys_addrs) {
1092 rte_eal_init_alert("Cannot use IOVA as 'PA' since physical addresses are not available");
1097 RTE_LOG(INFO, EAL, "Selected IOVA mode '%s'\n",
1098 rte_eal_iova_mode() == RTE_IOVA_PA ? "PA" : "VA");
1100 if (internal_config.no_hugetlbfs == 0) {
1101 /* rte_config isn't initialized yet */
1102 ret = internal_config.process_type == RTE_PROC_PRIMARY ?
1103 eal_hugepage_info_init() :
1104 eal_hugepage_info_read();
1106 rte_eal_init_alert("Cannot get hugepage information.");
1108 rte_atomic32_clear(&run_once);
1113 if (internal_config.memory == 0 && internal_config.force_sockets == 0) {
1114 if (internal_config.no_hugetlbfs)
1115 internal_config.memory = MEMSIZE_IF_NO_HUGE_PAGE;
1118 if (internal_config.vmware_tsc_map == 1) {
1119 #ifdef RTE_LIBRTE_EAL_VMWARE_TSC_MAP_SUPPORT
1120 rte_cycles_vmware_tsc_map = 1;
1121 RTE_LOG (DEBUG, EAL, "Using VMWARE TSC MAP, "
1122 "you must have monitor_control.pseudo_perfctr = TRUE\n");
1124 RTE_LOG (WARNING, EAL, "Ignoring --vmware-tsc-map because "
1125 "RTE_LIBRTE_EAL_VMWARE_TSC_MAP_SUPPORT is not set\n");
1129 if (rte_eal_log_init(logid, internal_config.syslog_facility) < 0) {
1130 rte_eal_init_alert("Cannot init logging.");
1132 rte_atomic32_clear(&run_once);
1137 if (rte_eal_vfio_setup() < 0) {
1138 rte_eal_init_alert("Cannot init VFIO");
1140 rte_atomic32_clear(&run_once);
1144 /* in secondary processes, memory init may allocate additional fbarrays
1145 * not present in primary processes, so to avoid any potential issues,
1146 * initialize memzones first.
1148 if (rte_eal_memzone_init() < 0) {
1149 rte_eal_init_alert("Cannot init memzone");
1154 if (rte_eal_memory_init() < 0) {
1155 rte_eal_init_alert("Cannot init memory");
1160 /* the directories are locked during eal_hugepage_info_init */
1161 eal_hugedirs_unlock();
1163 if (rte_eal_malloc_heap_init() < 0) {
1164 rte_eal_init_alert("Cannot init malloc heap");
1169 if (rte_eal_tailqs_init() < 0) {
1170 rte_eal_init_alert("Cannot init tail queues for objects");
1175 if (rte_eal_timer_init() < 0) {
1176 rte_eal_init_alert("Cannot init HPET or TSC timers");
1177 rte_errno = ENOTSUP;
1181 eal_check_mem_on_local_socket();
1183 eal_thread_init_master(rte_config.master_lcore);
1185 ret = eal_thread_dump_affinity(cpuset, sizeof(cpuset));
1187 RTE_LOG(DEBUG, EAL, "Master lcore %u is ready (tid=%zx;cpuset=[%s%s])\n",
1188 rte_config.master_lcore, (uintptr_t)thread_id, cpuset,
1189 ret == 0 ? "" : "...");
1191 RTE_LCORE_FOREACH_SLAVE(i) {
1194 * create communication pipes between master thread
1197 if (pipe(lcore_config[i].pipe_master2slave) < 0)
1198 rte_panic("Cannot create pipe\n");
1199 if (pipe(lcore_config[i].pipe_slave2master) < 0)
1200 rte_panic("Cannot create pipe\n");
1202 lcore_config[i].state = WAIT;
1204 /* create a thread for each lcore */
1205 ret = pthread_create(&lcore_config[i].thread_id, NULL,
1206 eal_thread_loop, NULL);
1208 rte_panic("Cannot create thread\n");
1210 /* Set thread_name for aid in debugging. */
1211 snprintf(thread_name, sizeof(thread_name),
1212 "lcore-slave-%d", i);
1213 ret = rte_thread_setname(lcore_config[i].thread_id,
1217 "Cannot set name for lcore thread\n");
1221 * Launch a dummy function on all slave lcores, so that master lcore
1222 * knows they are all ready when this function returns.
1224 rte_eal_mp_remote_launch(sync_func, NULL, SKIP_MASTER);
1225 rte_eal_mp_wait_lcore();
1227 /* initialize services so vdevs register service during bus_probe. */
1228 ret = rte_service_init();
1230 rte_eal_init_alert("rte_service_init() failed");
1231 rte_errno = ENOEXEC;
1235 /* Probe all the buses and devices/drivers on them */
1236 if (rte_bus_probe()) {
1237 rte_eal_init_alert("Cannot probe devices");
1238 rte_errno = ENOTSUP;
1243 /* Register mp action after probe() so that we got enough info */
1244 if (rte_vfio_is_enabled("vfio") && vfio_mp_sync_setup() < 0)
1248 /* initialize default service/lcore mappings and start running. Ignore
1249 * -ENOTSUP, as it indicates no service coremask passed to EAL.
1251 ret = rte_service_start_with_defaults();
1252 if (ret < 0 && ret != -ENOTSUP) {
1253 rte_errno = ENOEXEC;
1258 * Clean up unused files in runtime directory. We do this at the end of
1259 * init and not at the beginning because we want to clean stuff up
1260 * whether we are primary or secondary process, but we cannot remove
1261 * primary process' files because secondary should be able to run even
1262 * if primary process is dead.
1264 * In no_shconf mode, no runtime directory is created in the first
1265 * place, so no cleanup needed.
1267 if (!internal_config.no_shconf && eal_clean_runtime_dir() < 0) {
1268 rte_eal_init_alert("Cannot clear runtime directory\n");
1272 eal_mcfg_complete();
1274 /* Call each registered callback, if enabled */
1281 mark_freeable(const struct rte_memseg_list *msl, const struct rte_memseg *ms,
1282 void *arg __rte_unused)
1284 /* ms is const, so find this memseg */
1285 struct rte_memseg *found;
1290 found = rte_mem_virt2memseg(ms->addr, msl);
1292 found->flags &= ~RTE_MEMSEG_FLAG_DO_NOT_FREE;
1298 rte_eal_cleanup(void)
1300 /* if we're in a primary process, we need to mark hugepages as freeable
1301 * so that finalization can release them back to the system.
1303 if (rte_eal_process_type() == RTE_PROC_PRIMARY)
1304 rte_memseg_walk(mark_freeable, NULL);
1305 rte_service_finalize();
1306 rte_mp_channel_cleanup();
1307 eal_cleanup_config(&internal_config);
1312 enum rte_lcore_role_t
1313 rte_eal_lcore_role(unsigned lcore_id)
1315 return rte_config.lcore_role[lcore_id];
1318 enum rte_proc_type_t
1319 rte_eal_process_type(void)
1321 return rte_config.process_type;
1324 int rte_eal_has_hugepages(void)
1326 return ! internal_config.no_hugetlbfs;
1329 int rte_eal_has_pci(void)
1331 return !internal_config.no_pci;
1334 int rte_eal_create_uio_dev(void)
1336 return internal_config.create_uio_dev;
1340 rte_eal_vfio_intr_mode(void)
1342 return internal_config.vfio_intr_mode;
1346 rte_eal_check_module(const char *module_name)
1348 char sysfs_mod_name[PATH_MAX];
1352 if (NULL == module_name)
1355 /* Check if there is sysfs mounted */
1356 if (stat("/sys/module", &st) != 0) {
1357 RTE_LOG(DEBUG, EAL, "sysfs is not mounted! error %i (%s)\n",
1358 errno, strerror(errno));
1362 /* A module might be built-in, therefore try sysfs */
1363 n = snprintf(sysfs_mod_name, PATH_MAX, "/sys/module/%s", module_name);
1364 if (n < 0 || n > PATH_MAX) {
1365 RTE_LOG(DEBUG, EAL, "Could not format module path\n");
1369 if (stat(sysfs_mod_name, &st) != 0) {
1370 RTE_LOG(DEBUG, EAL, "Module %s not found! error %i (%s)\n",
1371 sysfs_mod_name, errno, strerror(errno));
1375 /* Module has been found */