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_eal_memconfig.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 /* Allow the application to print its usage message too if set */
70 static rte_usage_hook_t rte_application_usage_hook = NULL;
72 /* early configuration structure, when memory config is not mmapped */
73 static struct rte_mem_config early_mem_config;
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 = sizeof(early_mem_config.memsegs),
86 /* Address of global and public configuration */
87 static struct rte_config rte_config = {
88 .mem_config = &early_mem_config,
91 /* internal configuration (per-core) */
92 struct lcore_config lcore_config[RTE_MAX_LCORE];
94 /* internal configuration */
95 struct internal_config internal_config;
97 /* used by rte_rdtsc() */
98 int rte_cycles_vmware_tsc_map;
100 /* platform-specific runtime dir */
101 static char runtime_dir[PATH_MAX];
103 static const char *default_runtime_dir = "/var/run";
106 eal_create_runtime_dir(void)
108 const char *directory = default_runtime_dir;
109 const char *xdg_runtime_dir = getenv("XDG_RUNTIME_DIR");
110 const char *fallback = "/tmp";
115 /* try XDG path first, fall back to /tmp */
116 if (xdg_runtime_dir != NULL)
117 directory = xdg_runtime_dir;
119 directory = fallback;
121 /* create DPDK subdirectory under runtime dir */
122 ret = snprintf(tmp, sizeof(tmp), "%s/dpdk", directory);
123 if (ret < 0 || ret == sizeof(tmp)) {
124 RTE_LOG(ERR, EAL, "Error creating DPDK runtime path name\n");
128 /* create prefix-specific subdirectory under DPDK runtime dir */
129 ret = snprintf(runtime_dir, sizeof(runtime_dir), "%s/%s",
130 tmp, eal_get_hugefile_prefix());
131 if (ret < 0 || ret == sizeof(runtime_dir)) {
132 RTE_LOG(ERR, EAL, "Error creating prefix-specific runtime path name\n");
136 /* create the path if it doesn't exist. no "mkdir -p" here, so do it
139 ret = mkdir(tmp, 0700);
140 if (ret < 0 && errno != EEXIST) {
141 RTE_LOG(ERR, EAL, "Error creating '%s': %s\n",
142 tmp, strerror(errno));
146 ret = mkdir(runtime_dir, 0700);
147 if (ret < 0 && errno != EEXIST) {
148 RTE_LOG(ERR, EAL, "Error creating '%s': %s\n",
149 runtime_dir, strerror(errno));
157 eal_clean_runtime_dir(void)
160 struct dirent *dirent;
161 int dir_fd, fd, lck_result;
162 static const char * const filters[] = {
168 dir = opendir(runtime_dir);
170 RTE_LOG(ERR, EAL, "Unable to open runtime directory %s\n",
176 /* lock the directory before doing anything, to avoid races */
177 if (flock(dir_fd, LOCK_EX) < 0) {
178 RTE_LOG(ERR, EAL, "Unable to lock runtime directory %s\n",
183 dirent = readdir(dir);
185 RTE_LOG(ERR, EAL, "Unable to read runtime directory %s\n",
190 while (dirent != NULL) {
194 /* skip files that don't match the patterns */
195 for (f_idx = 0; f_idx < RTE_DIM(filters); f_idx++) {
196 const char *filter = filters[f_idx];
198 if (fnmatch(filter, dirent->d_name, 0) == 0) {
204 dirent = readdir(dir);
208 /* try and lock the file */
209 fd = openat(dir_fd, dirent->d_name, O_RDONLY);
211 /* skip to next file */
213 dirent = readdir(dir);
217 /* non-blocking lock */
218 lck_result = flock(fd, LOCK_EX | LOCK_NB);
220 /* if lock succeeds, remove the file */
221 if (lck_result != -1)
222 unlinkat(dir_fd, dirent->d_name, 0);
224 dirent = readdir(dir);
227 /* closedir closes dir_fd and drops the lock */
235 RTE_LOG(ERR, EAL, "Error while clearing runtime dir: %s\n",
242 rte_eal_get_runtime_dir(void)
247 /* Return user provided mbuf pool ops name */
249 rte_eal_mbuf_user_pool_ops(void)
251 return internal_config.user_mbuf_pool_ops_name;
254 /* Return a pointer to the configuration structure */
256 rte_eal_get_configuration(void)
262 rte_eal_iova_mode(void)
264 return rte_eal_get_configuration()->iova_mode;
267 /* parse a sysfs (or other) file containing one integer value */
269 eal_parse_sysfs_value(const char *filename, unsigned long *val)
275 if ((f = fopen(filename, "r")) == NULL) {
276 RTE_LOG(ERR, EAL, "%s(): cannot open sysfs value %s\n",
281 if (fgets(buf, sizeof(buf), f) == NULL) {
282 RTE_LOG(ERR, EAL, "%s(): cannot read sysfs value %s\n",
287 *val = strtoul(buf, &end, 0);
288 if ((buf[0] == '\0') || (end == NULL) || (*end != '\n')) {
289 RTE_LOG(ERR, EAL, "%s(): cannot parse sysfs value %s\n",
299 /* create memory configuration in shared/mmap memory. Take out
300 * a write lock on the memsegs, so we can auto-detect primary/secondary.
301 * This means we never close the file while running (auto-close on exit).
302 * We also don't lock the whole file, so that in future we can use read-locks
303 * on other parts, e.g. memzones, to detect if there are running secondary
306 rte_eal_config_create(void)
308 void *rte_mem_cfg_addr;
311 const char *pathname = eal_runtime_config_path();
313 if (internal_config.no_shconf)
316 /* map the config before hugepage address so that we don't waste a page */
317 if (internal_config.base_virtaddr != 0)
318 rte_mem_cfg_addr = (void *)
319 RTE_ALIGN_FLOOR(internal_config.base_virtaddr -
320 sizeof(struct rte_mem_config), sysconf(_SC_PAGE_SIZE));
322 rte_mem_cfg_addr = NULL;
325 mem_cfg_fd = open(pathname, O_RDWR | O_CREAT, 0600);
326 if (mem_cfg_fd < 0) {
327 RTE_LOG(ERR, EAL, "Cannot open '%s' for rte_mem_config\n",
333 retval = ftruncate(mem_cfg_fd, sizeof(*rte_config.mem_config));
337 RTE_LOG(ERR, EAL, "Cannot resize '%s' for rte_mem_config\n",
342 retval = fcntl(mem_cfg_fd, F_SETLK, &wr_lock);
346 RTE_LOG(ERR, EAL, "Cannot create lock on '%s'. Is another primary "
347 "process running?\n", pathname);
351 rte_mem_cfg_addr = mmap(rte_mem_cfg_addr, sizeof(*rte_config.mem_config),
352 PROT_READ | PROT_WRITE, MAP_SHARED, mem_cfg_fd, 0);
354 if (rte_mem_cfg_addr == MAP_FAILED){
357 RTE_LOG(ERR, EAL, "Cannot mmap memory for rte_config\n");
361 memcpy(rte_mem_cfg_addr, &early_mem_config, sizeof(early_mem_config));
362 rte_config.mem_config = rte_mem_cfg_addr;
364 /* store address of the config in the config itself so that secondary
365 * processes could later map the config into this exact location */
366 rte_config.mem_config->mem_cfg_addr = (uintptr_t) rte_mem_cfg_addr;
368 rte_config.mem_config->dma_maskbits = 0;
373 /* attach to an existing shared memory config */
375 rte_eal_config_attach(void)
377 struct rte_mem_config *mem_config;
379 const char *pathname = eal_runtime_config_path();
381 if (internal_config.no_shconf)
385 mem_cfg_fd = open(pathname, O_RDWR);
386 if (mem_cfg_fd < 0) {
387 RTE_LOG(ERR, EAL, "Cannot open '%s' for rte_mem_config\n",
393 /* map it as read-only first */
394 mem_config = (struct rte_mem_config *) mmap(NULL, sizeof(*mem_config),
395 PROT_READ, MAP_SHARED, mem_cfg_fd, 0);
396 if (mem_config == MAP_FAILED) {
399 RTE_LOG(ERR, EAL, "Cannot mmap memory for rte_config! error %i (%s)\n",
400 errno, strerror(errno));
404 rte_config.mem_config = mem_config;
409 /* reattach the shared config at exact memory location primary process has it */
411 rte_eal_config_reattach(void)
413 struct rte_mem_config *mem_config;
414 void *rte_mem_cfg_addr;
416 if (internal_config.no_shconf)
419 /* save the address primary process has mapped shared config to */
420 rte_mem_cfg_addr = (void *) (uintptr_t) rte_config.mem_config->mem_cfg_addr;
422 /* unmap original config */
423 munmap(rte_config.mem_config, sizeof(struct rte_mem_config));
425 /* remap the config at proper address */
426 mem_config = (struct rte_mem_config *) mmap(rte_mem_cfg_addr,
427 sizeof(*mem_config), PROT_READ | PROT_WRITE, MAP_SHARED,
433 if (mem_config == MAP_FAILED || mem_config != rte_mem_cfg_addr) {
434 if (mem_config != MAP_FAILED) {
435 /* errno is stale, don't use */
436 RTE_LOG(ERR, EAL, "Cannot mmap memory for rte_config at [%p], got [%p]"
437 " - please use '--base-virtaddr' option\n",
438 rte_mem_cfg_addr, mem_config);
441 RTE_LOG(ERR, EAL, "Cannot mmap memory for rte_config! error %i (%s)\n",
442 errno, strerror(errno));
446 rte_config.mem_config = mem_config;
451 /* Detect if we are a primary or a secondary process */
453 eal_proc_type_detect(void)
455 enum rte_proc_type_t ptype = RTE_PROC_PRIMARY;
456 const char *pathname = eal_runtime_config_path();
458 /* if there no shared config, there can be no secondary processes */
459 if (!internal_config.no_shconf) {
460 /* if we can open the file but not get a write-lock we are a
461 * secondary process. NOTE: if we get a file handle back, we
462 * keep that open and don't close it to prevent a race condition
463 * between multiple opens.
465 if (((mem_cfg_fd = open(pathname, O_RDWR)) >= 0) &&
466 (fcntl(mem_cfg_fd, F_SETLK, &wr_lock) < 0))
467 ptype = RTE_PROC_SECONDARY;
470 RTE_LOG(INFO, EAL, "Auto-detected process type: %s\n",
471 ptype == RTE_PROC_PRIMARY ? "PRIMARY" : "SECONDARY");
476 /* Sets up rte_config structure with the pointer to shared memory config.*/
478 rte_config_init(void)
480 rte_config.process_type = internal_config.process_type;
482 switch (rte_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 rte_config.process_type);
510 /* Unlocks hugepage directories that were locked by eal_hugepage_info_init */
512 eal_hugedirs_unlock(void)
516 for (i = 0; i < MAX_HUGEPAGE_SIZES; i++)
518 /* skip uninitialized */
519 if (internal_config.hugepage_info[i].lock_descriptor < 0)
521 /* unlock hugepage file */
522 flock(internal_config.hugepage_info[i].lock_descriptor, LOCK_UN);
523 close(internal_config.hugepage_info[i].lock_descriptor);
524 /* reset the field */
525 internal_config.hugepage_info[i].lock_descriptor = -1;
531 eal_usage(const char *prgname)
533 printf("\nUsage: %s ", prgname);
535 printf("EAL Linux options:\n"
536 " --"OPT_SOCKET_MEM" Memory to allocate on sockets (comma separated values)\n"
537 " --"OPT_SOCKET_LIMIT" Limit memory allocation on sockets (comma separated values)\n"
538 " --"OPT_HUGE_DIR" Directory where hugetlbfs is mounted\n"
539 " --"OPT_FILE_PREFIX" Prefix for hugepage filenames\n"
540 " --"OPT_BASE_VIRTADDR" Base virtual address\n"
541 " --"OPT_CREATE_UIO_DEV" Create /dev/uioX (usually done by hotplug)\n"
542 " --"OPT_VFIO_INTR" Interrupt mode for VFIO (legacy|msi|msix)\n"
543 " --"OPT_LEGACY_MEM" Legacy memory mode (no dynamic allocation, contiguous segments)\n"
544 " --"OPT_SINGLE_FILE_SEGMENTS" Put all hugepage memory in single files\n"
545 " --"OPT_MATCH_ALLOCATIONS" Free hugepages exactly as allocated\n"
547 /* Allow the application to print its usage message too if hook is set */
548 if ( rte_application_usage_hook ) {
549 printf("===== Application Usage =====\n\n");
550 rte_application_usage_hook(prgname);
554 /* Set a per-application usage message */
556 rte_set_application_usage_hook( rte_usage_hook_t usage_func )
558 rte_usage_hook_t old_func;
560 /* Will be NULL on the first call to denote the last usage routine. */
561 old_func = rte_application_usage_hook;
562 rte_application_usage_hook = usage_func;
568 eal_parse_socket_arg(char *strval, volatile uint64_t *socket_arg)
570 char * arg[RTE_MAX_NUMA_NODES];
573 uint64_t total_mem = 0;
575 len = strnlen(strval, SOCKET_MEM_STRLEN);
576 if (len == SOCKET_MEM_STRLEN) {
577 RTE_LOG(ERR, EAL, "--socket-mem is too long\n");
581 /* all other error cases will be caught later */
582 if (!isdigit(strval[len-1]))
585 /* split the optarg into separate socket values */
586 arg_num = rte_strsplit(strval, len,
587 arg, RTE_MAX_NUMA_NODES, ',');
589 /* if split failed, or 0 arguments */
593 /* parse each defined socket option */
595 for (i = 0; i < arg_num; i++) {
598 val = strtoull(arg[i], &end, 10);
600 /* check for invalid input */
602 (arg[i][0] == '\0') || (end == NULL) || (*end != '\0'))
613 eal_parse_base_virtaddr(const char *arg)
619 addr = strtoull(arg, &end, 16);
621 /* check for errors */
622 if ((errno != 0) || (arg[0] == '\0') || end == NULL || (*end != '\0'))
625 /* make sure we don't exceed 32-bit boundary on 32-bit target */
627 if (addr >= UINTPTR_MAX)
631 /* align the addr on 16M boundary, 16MB is the minimum huge page
632 * size on IBM Power architecture. If the addr is aligned to 16MB,
633 * it can align to 2MB for x86. So this alignment can also be used
635 internal_config.base_virtaddr =
636 RTE_PTR_ALIGN_CEIL((uintptr_t)addr, (size_t)RTE_PGSIZE_16M);
642 eal_parse_vfio_intr(const char *mode)
647 enum rte_intr_mode value;
649 { "legacy", RTE_INTR_MODE_LEGACY },
650 { "msi", RTE_INTR_MODE_MSI },
651 { "msix", RTE_INTR_MODE_MSIX },
654 for (i = 0; i < RTE_DIM(map); i++) {
655 if (!strcmp(mode, map[i].name)) {
656 internal_config.vfio_intr_mode = map[i].value;
663 /* Parse the arguments for --log-level only */
665 eal_log_level_parse(int argc, char **argv)
670 const int old_optind = optind;
671 const int old_optopt = optopt;
672 char * const old_optarg = optarg;
677 while ((opt = getopt_long(argc, argvopt, eal_short_options,
678 eal_long_options, &option_index)) != EOF) {
682 /* getopt is not happy, stop right now */
686 ret = (opt == OPT_LOG_LEVEL_NUM) ?
687 eal_parse_common_option(opt, optarg, &internal_config) : 0;
689 /* common parser is not happy */
694 /* restore getopt lib */
700 /* Parse the argument given in the command line of the application */
702 eal_parse_args(int argc, char **argv)
707 char *prgname = argv[0];
708 const int old_optind = optind;
709 const int old_optopt = optopt;
710 char * const old_optarg = optarg;
716 while ((opt = getopt_long(argc, argvopt, eal_short_options,
717 eal_long_options, &option_index)) != EOF) {
720 * getopt didn't recognise the option, lets parse the
721 * registered options to see if the flag is valid
724 ret = rte_option_parse(argv[optind-1]);
733 ret = eal_parse_common_option(opt, optarg, &internal_config);
734 /* common parser is not happy */
740 /* common parser handled this option */
749 case OPT_HUGE_DIR_NUM:
751 char *hdir = strdup(optarg);
753 RTE_LOG(ERR, EAL, "Could not store hugepage directory\n");
755 /* free old hugepage dir */
756 if (internal_config.hugepage_dir != NULL)
757 free(internal_config.hugepage_dir);
758 internal_config.hugepage_dir = hdir;
762 case OPT_FILE_PREFIX_NUM:
764 char *prefix = strdup(optarg);
766 RTE_LOG(ERR, EAL, "Could not store file prefix\n");
768 /* free old prefix */
769 if (internal_config.hugefile_prefix != NULL)
770 free(internal_config.hugefile_prefix);
771 internal_config.hugefile_prefix = prefix;
775 case OPT_SOCKET_MEM_NUM:
776 if (eal_parse_socket_arg(optarg,
777 internal_config.socket_mem) < 0) {
778 RTE_LOG(ERR, EAL, "invalid parameters for --"
779 OPT_SOCKET_MEM "\n");
784 internal_config.force_sockets = 1;
787 case OPT_SOCKET_LIMIT_NUM:
788 if (eal_parse_socket_arg(optarg,
789 internal_config.socket_limit) < 0) {
790 RTE_LOG(ERR, EAL, "invalid parameters for --"
791 OPT_SOCKET_LIMIT "\n");
796 internal_config.force_socket_limits = 1;
799 case OPT_BASE_VIRTADDR_NUM:
800 if (eal_parse_base_virtaddr(optarg) < 0) {
801 RTE_LOG(ERR, EAL, "invalid parameter for --"
802 OPT_BASE_VIRTADDR "\n");
809 case OPT_VFIO_INTR_NUM:
810 if (eal_parse_vfio_intr(optarg) < 0) {
811 RTE_LOG(ERR, EAL, "invalid parameters for --"
819 case OPT_CREATE_UIO_DEV_NUM:
820 internal_config.create_uio_dev = 1;
823 case OPT_MBUF_POOL_OPS_NAME_NUM:
825 char *ops_name = strdup(optarg);
826 if (ops_name == NULL)
827 RTE_LOG(ERR, EAL, "Could not store mbuf pool ops name\n");
829 /* free old ops name */
830 if (internal_config.user_mbuf_pool_ops_name !=
832 free(internal_config.user_mbuf_pool_ops_name);
834 internal_config.user_mbuf_pool_ops_name =
839 case OPT_MATCH_ALLOCATIONS_NUM:
840 internal_config.match_allocations = 1;
844 if (opt < OPT_LONG_MIN_NUM && isprint(opt)) {
845 RTE_LOG(ERR, EAL, "Option %c is not supported "
847 } else if (opt >= OPT_LONG_MIN_NUM &&
848 opt < OPT_LONG_MAX_NUM) {
849 RTE_LOG(ERR, EAL, "Option %s is not supported "
851 eal_long_options[option_index].name);
853 RTE_LOG(ERR, EAL, "Option %d is not supported "
862 /* create runtime data directory */
863 if (internal_config.no_shconf == 0 &&
864 eal_create_runtime_dir() < 0) {
865 RTE_LOG(ERR, EAL, "Cannot create runtime directory\n");
870 if (eal_adjust_config(&internal_config) != 0) {
876 if (eal_check_common_options(&internal_config) != 0) {
883 argv[optind-1] = prgname;
887 /* restore getopt lib */
896 check_socket(const struct rte_memseg_list *msl, void *arg)
898 int *socket_id = arg;
903 return *socket_id == msl->socket_id;
907 eal_check_mem_on_local_socket(void)
911 socket_id = rte_lcore_to_socket_id(rte_config.master_lcore);
913 if (rte_memseg_list_walk(check_socket, &socket_id) == 0)
914 RTE_LOG(WARNING, EAL, "WARNING: Master core has no memory on local socket!\n");
918 sync_func(__attribute__((unused)) void *arg)
924 * Request iopl privilege for all RPL, returns 0 on success
925 * iopl() call is mostly for the i386 architecture. For other architectures,
926 * return -1 to indicate IO privilege can't be changed in this way.
929 rte_eal_iopl_init(void)
931 #if defined(RTE_ARCH_X86)
939 static int rte_eal_vfio_setup(void)
941 if (rte_vfio_enable("vfio"))
948 static void rte_eal_init_alert(const char *msg)
950 fprintf(stderr, "EAL: FATAL: %s\n", msg);
951 RTE_LOG(ERR, EAL, "%s\n", msg);
954 /* Launch threads, called at application init(). */
956 rte_eal_init(int argc, char **argv)
960 static rte_atomic32_t run_once = RTE_ATOMIC32_INIT(0);
962 static char logid[PATH_MAX];
963 char cpuset[RTE_CPU_AFFINITY_STR_LEN];
964 char thread_name[RTE_MAX_THREAD_NAME_LEN];
967 /* checks if the machine is adequate */
968 if (!rte_cpu_is_supported()) {
969 rte_eal_init_alert("unsupported cpu type.");
974 if (!rte_atomic32_test_and_set(&run_once)) {
975 rte_eal_init_alert("already called initialization.");
976 rte_errno = EALREADY;
980 p = strrchr(argv[0], '/');
981 strlcpy(logid, p ? p + 1 : argv[0], sizeof(logid));
982 thread_id = pthread_self();
984 eal_reset_internal_config(&internal_config);
986 /* set log level as early as possible */
987 eal_log_level_parse(argc, argv);
989 if (rte_eal_cpu_init() < 0) {
990 rte_eal_init_alert("Cannot detect lcores.");
995 fctret = eal_parse_args(argc, argv);
997 rte_eal_init_alert("Invalid 'command line' arguments.");
999 rte_atomic32_clear(&run_once);
1003 if (eal_plugins_init() < 0) {
1004 rte_eal_init_alert("Cannot init plugins");
1006 rte_atomic32_clear(&run_once);
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_config.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 iova_mode = phys_addrs ? RTE_IOVA_PA : RTE_IOVA_VA;
1066 "Buses did not request a specific IOVA mode, using '%s' based on physical addresses availability.\n",
1067 phys_addrs ? "PA" : "VA");
1069 #ifdef RTE_LIBRTE_KNI
1070 /* Workaround for KNI which requires physical address to work */
1071 if (iova_mode == RTE_IOVA_VA &&
1072 rte_eal_check_module("rte_kni") == 1) {
1074 iova_mode = RTE_IOVA_PA;
1075 RTE_LOG(WARNING, EAL, "Forcing IOVA as 'PA' because KNI module is loaded\n");
1077 RTE_LOG(DEBUG, EAL, "KNI can not work since physical addresses are unavailable\n");
1081 rte_eal_get_configuration()->iova_mode = iova_mode;
1083 rte_eal_get_configuration()->iova_mode =
1084 internal_config.iova_mode;
1087 if (rte_eal_iova_mode() == RTE_IOVA_PA && !phys_addrs) {
1088 rte_eal_init_alert("Cannot use IOVA as 'PA' since physical addresses are not available");
1093 RTE_LOG(INFO, EAL, "Selected IOVA mode '%s'\n",
1094 rte_eal_iova_mode() == RTE_IOVA_PA ? "PA" : "VA");
1096 if (internal_config.no_hugetlbfs == 0) {
1097 /* rte_config isn't initialized yet */
1098 ret = internal_config.process_type == RTE_PROC_PRIMARY ?
1099 eal_hugepage_info_init() :
1100 eal_hugepage_info_read();
1102 rte_eal_init_alert("Cannot get hugepage information.");
1104 rte_atomic32_clear(&run_once);
1109 if (internal_config.memory == 0 && internal_config.force_sockets == 0) {
1110 if (internal_config.no_hugetlbfs)
1111 internal_config.memory = MEMSIZE_IF_NO_HUGE_PAGE;
1114 if (internal_config.vmware_tsc_map == 1) {
1115 #ifdef RTE_LIBRTE_EAL_VMWARE_TSC_MAP_SUPPORT
1116 rte_cycles_vmware_tsc_map = 1;
1117 RTE_LOG (DEBUG, EAL, "Using VMWARE TSC MAP, "
1118 "you must have monitor_control.pseudo_perfctr = TRUE\n");
1120 RTE_LOG (WARNING, EAL, "Ignoring --vmware-tsc-map because "
1121 "RTE_LIBRTE_EAL_VMWARE_TSC_MAP_SUPPORT is not set\n");
1125 if (rte_eal_log_init(logid, internal_config.syslog_facility) < 0) {
1126 rte_eal_init_alert("Cannot init logging.");
1128 rte_atomic32_clear(&run_once);
1133 if (rte_eal_vfio_setup() < 0) {
1134 rte_eal_init_alert("Cannot init VFIO");
1136 rte_atomic32_clear(&run_once);
1140 /* in secondary processes, memory init may allocate additional fbarrays
1141 * not present in primary processes, so to avoid any potential issues,
1142 * initialize memzones first.
1144 if (rte_eal_memzone_init() < 0) {
1145 rte_eal_init_alert("Cannot init memzone");
1150 if (rte_eal_memory_init() < 0) {
1151 rte_eal_init_alert("Cannot init memory");
1156 /* the directories are locked during eal_hugepage_info_init */
1157 eal_hugedirs_unlock();
1159 if (rte_eal_malloc_heap_init() < 0) {
1160 rte_eal_init_alert("Cannot init malloc heap");
1165 if (rte_eal_tailqs_init() < 0) {
1166 rte_eal_init_alert("Cannot init tail queues for objects");
1171 if (rte_eal_timer_init() < 0) {
1172 rte_eal_init_alert("Cannot init HPET or TSC timers");
1173 rte_errno = ENOTSUP;
1177 eal_check_mem_on_local_socket();
1179 eal_thread_init_master(rte_config.master_lcore);
1181 ret = eal_thread_dump_affinity(cpuset, sizeof(cpuset));
1183 RTE_LOG(DEBUG, EAL, "Master lcore %u is ready (tid=%zx;cpuset=[%s%s])\n",
1184 rte_config.master_lcore, (uintptr_t)thread_id, cpuset,
1185 ret == 0 ? "" : "...");
1187 RTE_LCORE_FOREACH_SLAVE(i) {
1190 * create communication pipes between master thread
1193 if (pipe(lcore_config[i].pipe_master2slave) < 0)
1194 rte_panic("Cannot create pipe\n");
1195 if (pipe(lcore_config[i].pipe_slave2master) < 0)
1196 rte_panic("Cannot create pipe\n");
1198 lcore_config[i].state = WAIT;
1200 /* create a thread for each lcore */
1201 ret = pthread_create(&lcore_config[i].thread_id, NULL,
1202 eal_thread_loop, NULL);
1204 rte_panic("Cannot create thread\n");
1206 /* Set thread_name for aid in debugging. */
1207 snprintf(thread_name, sizeof(thread_name),
1208 "lcore-slave-%d", i);
1209 ret = rte_thread_setname(lcore_config[i].thread_id,
1213 "Cannot set name for lcore thread\n");
1217 * Launch a dummy function on all slave lcores, so that master lcore
1218 * knows they are all ready when this function returns.
1220 rte_eal_mp_remote_launch(sync_func, NULL, SKIP_MASTER);
1221 rte_eal_mp_wait_lcore();
1223 /* initialize services so vdevs register service during bus_probe. */
1224 ret = rte_service_init();
1226 rte_eal_init_alert("rte_service_init() failed");
1227 rte_errno = ENOEXEC;
1231 /* Probe all the buses and devices/drivers on them */
1232 if (rte_bus_probe()) {
1233 rte_eal_init_alert("Cannot probe devices");
1234 rte_errno = ENOTSUP;
1239 /* Register mp action after probe() so that we got enough info */
1240 if (rte_vfio_is_enabled("vfio") && vfio_mp_sync_setup() < 0)
1244 /* initialize default service/lcore mappings and start running. Ignore
1245 * -ENOTSUP, as it indicates no service coremask passed to EAL.
1247 ret = rte_service_start_with_defaults();
1248 if (ret < 0 && ret != -ENOTSUP) {
1249 rte_errno = ENOEXEC;
1254 * Clean up unused files in runtime directory. We do this at the end of
1255 * init and not at the beginning because we want to clean stuff up
1256 * whether we are primary or secondary process, but we cannot remove
1257 * primary process' files because secondary should be able to run even
1258 * if primary process is dead.
1260 * In no_shconf mode, no runtime directory is created in the first
1261 * place, so no cleanup needed.
1263 if (!internal_config.no_shconf && eal_clean_runtime_dir() < 0) {
1264 rte_eal_init_alert("Cannot clear runtime directory\n");
1268 eal_mcfg_complete();
1270 /* Call each registered callback, if enabled */
1277 mark_freeable(const struct rte_memseg_list *msl, const struct rte_memseg *ms,
1278 void *arg __rte_unused)
1280 /* ms is const, so find this memseg */
1281 struct rte_memseg *found;
1286 found = rte_mem_virt2memseg(ms->addr, msl);
1288 found->flags &= ~RTE_MEMSEG_FLAG_DO_NOT_FREE;
1294 rte_eal_cleanup(void)
1296 /* if we're in a primary process, we need to mark hugepages as freeable
1297 * so that finalization can release them back to the system.
1299 if (rte_eal_process_type() == RTE_PROC_PRIMARY)
1300 rte_memseg_walk(mark_freeable, NULL);
1301 rte_service_finalize();
1302 rte_mp_channel_cleanup();
1303 eal_cleanup_config(&internal_config);
1308 enum rte_lcore_role_t
1309 rte_eal_lcore_role(unsigned lcore_id)
1311 return rte_config.lcore_role[lcore_id];
1314 enum rte_proc_type_t
1315 rte_eal_process_type(void)
1317 return rte_config.process_type;
1320 int rte_eal_has_hugepages(void)
1322 return ! internal_config.no_hugetlbfs;
1325 int rte_eal_has_pci(void)
1327 return !internal_config.no_pci;
1330 int rte_eal_create_uio_dev(void)
1332 return internal_config.create_uio_dev;
1336 rte_eal_vfio_intr_mode(void)
1338 return internal_config.vfio_intr_mode;
1342 rte_eal_check_module(const char *module_name)
1344 char sysfs_mod_name[PATH_MAX];
1348 if (NULL == module_name)
1351 /* Check if there is sysfs mounted */
1352 if (stat("/sys/module", &st) != 0) {
1353 RTE_LOG(DEBUG, EAL, "sysfs is not mounted! error %i (%s)\n",
1354 errno, strerror(errno));
1358 /* A module might be built-in, therefore try sysfs */
1359 n = snprintf(sysfs_mod_name, PATH_MAX, "/sys/module/%s", module_name);
1360 if (n < 0 || n > PATH_MAX) {
1361 RTE_LOG(DEBUG, EAL, "Could not format module path\n");
1365 if (stat(sysfs_mod_name, &st) != 0) {
1366 RTE_LOG(DEBUG, EAL, "Module %s not found! error %i (%s)\n",
1367 sysfs_mod_name, errno, strerror(errno));
1371 /* Module has been found */