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_options.h"
63 #define MEMSIZE_IF_NO_HUGE_PAGE (64ULL * 1024ULL * 1024ULL)
65 #define SOCKET_MEM_STRLEN (RTE_MAX_NUMA_NODES * 10)
67 /* Allow the application to print its usage message too if set */
68 static rte_usage_hook_t rte_application_usage_hook
= NULL
;
70 /* early configuration structure, when memory config is not mmapped */
71 static struct rte_mem_config early_mem_config
;
73 /* define fd variable here, because file needs to be kept open for the
74 * duration of the program, as we hold a write lock on it in the primary proc */
75 static int mem_cfg_fd
= -1;
77 static struct flock wr_lock
= {
80 .l_start
= offsetof(struct rte_mem_config
, memsegs
),
81 .l_len
= sizeof(early_mem_config
.memsegs
),
84 /* Address of global and public configuration */
85 static struct rte_config rte_config
= {
86 .mem_config
= &early_mem_config
,
89 /* internal configuration (per-core) */
90 struct lcore_config lcore_config
[RTE_MAX_LCORE
];
92 /* internal configuration */
93 struct internal_config internal_config
;
95 /* used by rte_rdtsc() */
96 int rte_cycles_vmware_tsc_map
;
98 /* platform-specific runtime dir */
99 static char runtime_dir
[PATH_MAX
];
101 static const char *default_runtime_dir
= "/var/run";
104 eal_create_runtime_dir(void)
106 const char *directory
= default_runtime_dir
;
107 const char *xdg_runtime_dir
= getenv("XDG_RUNTIME_DIR");
108 const char *fallback
= "/tmp";
113 /* try XDG path first, fall back to /tmp */
114 if (xdg_runtime_dir
!= NULL
)
115 directory
= xdg_runtime_dir
;
117 directory
= fallback
;
119 /* create DPDK subdirectory under runtime dir */
120 ret
= snprintf(tmp
, sizeof(tmp
), "%s/dpdk", directory
);
121 if (ret
< 0 || ret
== sizeof(tmp
)) {
122 RTE_LOG(ERR
, EAL
, "Error creating DPDK runtime path name\n");
126 /* create prefix-specific subdirectory under DPDK runtime dir */
127 ret
= snprintf(runtime_dir
, sizeof(runtime_dir
), "%s/%s",
128 tmp
, eal_get_hugefile_prefix());
129 if (ret
< 0 || ret
== sizeof(runtime_dir
)) {
130 RTE_LOG(ERR
, EAL
, "Error creating prefix-specific runtime path name\n");
134 /* create the path if it doesn't exist. no "mkdir -p" here, so do it
137 ret
= mkdir(tmp
, 0700);
138 if (ret
< 0 && errno
!= EEXIST
) {
139 RTE_LOG(ERR
, EAL
, "Error creating '%s': %s\n",
140 tmp
, strerror(errno
));
144 ret
= mkdir(runtime_dir
, 0700);
145 if (ret
< 0 && errno
!= EEXIST
) {
146 RTE_LOG(ERR
, EAL
, "Error creating '%s': %s\n",
147 runtime_dir
, strerror(errno
));
155 eal_clean_runtime_dir(void)
158 struct dirent
*dirent
;
159 int dir_fd
, fd
, lck_result
;
160 static const char * const filters
[] = {
166 dir
= opendir(runtime_dir
);
168 RTE_LOG(ERR
, EAL
, "Unable to open runtime directory %s\n",
174 /* lock the directory before doing anything, to avoid races */
175 if (flock(dir_fd
, LOCK_EX
) < 0) {
176 RTE_LOG(ERR
, EAL
, "Unable to lock runtime directory %s\n",
181 dirent
= readdir(dir
);
183 RTE_LOG(ERR
, EAL
, "Unable to read runtime directory %s\n",
188 while (dirent
!= NULL
) {
192 /* skip files that don't match the patterns */
193 for (f_idx
= 0; f_idx
< RTE_DIM(filters
); f_idx
++) {
194 const char *filter
= filters
[f_idx
];
196 if (fnmatch(filter
, dirent
->d_name
, 0) == 0) {
202 dirent
= readdir(dir
);
206 /* try and lock the file */
207 fd
= openat(dir_fd
, dirent
->d_name
, O_RDONLY
);
209 /* skip to next file */
211 dirent
= readdir(dir
);
215 /* non-blocking lock */
216 lck_result
= flock(fd
, LOCK_EX
| LOCK_NB
);
218 /* if lock succeeds, remove the file */
219 if (lck_result
!= -1)
220 unlinkat(dir_fd
, dirent
->d_name
, 0);
222 dirent
= readdir(dir
);
225 /* closedir closes dir_fd and drops the lock */
233 RTE_LOG(ERR
, EAL
, "Error while clearing runtime dir: %s\n",
240 rte_eal_get_runtime_dir(void)
245 /* Return user provided mbuf pool ops name */
247 rte_eal_mbuf_user_pool_ops(void)
249 return internal_config
.user_mbuf_pool_ops_name
;
252 /* Return a pointer to the configuration structure */
254 rte_eal_get_configuration(void)
260 rte_eal_iova_mode(void)
262 return rte_eal_get_configuration()->iova_mode
;
265 /* parse a sysfs (or other) file containing one integer value */
267 eal_parse_sysfs_value(const char *filename
, unsigned long *val
)
273 if ((f
= fopen(filename
, "r")) == NULL
) {
274 RTE_LOG(ERR
, EAL
, "%s(): cannot open sysfs value %s\n",
279 if (fgets(buf
, sizeof(buf
), f
) == NULL
) {
280 RTE_LOG(ERR
, EAL
, "%s(): cannot read sysfs value %s\n",
285 *val
= strtoul(buf
, &end
, 0);
286 if ((buf
[0] == '\0') || (end
== NULL
) || (*end
!= '\n')) {
287 RTE_LOG(ERR
, EAL
, "%s(): cannot parse sysfs value %s\n",
297 /* create memory configuration in shared/mmap memory. Take out
298 * a write lock on the memsegs, so we can auto-detect primary/secondary.
299 * This means we never close the file while running (auto-close on exit).
300 * We also don't lock the whole file, so that in future we can use read-locks
301 * on other parts, e.g. memzones, to detect if there are running secondary
304 rte_eal_config_create(void)
306 void *rte_mem_cfg_addr
;
309 const char *pathname
= eal_runtime_config_path();
311 if (internal_config
.no_shconf
)
314 /* map the config before hugepage address so that we don't waste a page */
315 if (internal_config
.base_virtaddr
!= 0)
316 rte_mem_cfg_addr
= (void *)
317 RTE_ALIGN_FLOOR(internal_config
.base_virtaddr
-
318 sizeof(struct rte_mem_config
), sysconf(_SC_PAGE_SIZE
));
320 rte_mem_cfg_addr
= NULL
;
323 mem_cfg_fd
= open(pathname
, O_RDWR
| O_CREAT
, 0600);
325 rte_panic("Cannot open '%s' for rte_mem_config\n", pathname
);
328 retval
= ftruncate(mem_cfg_fd
, sizeof(*rte_config
.mem_config
));
331 rte_panic("Cannot resize '%s' for rte_mem_config\n", pathname
);
334 retval
= fcntl(mem_cfg_fd
, F_SETLK
, &wr_lock
);
337 rte_exit(EXIT_FAILURE
, "Cannot create lock on '%s'. Is another primary "
338 "process running?\n", pathname
);
341 rte_mem_cfg_addr
= mmap(rte_mem_cfg_addr
, sizeof(*rte_config
.mem_config
),
342 PROT_READ
| PROT_WRITE
, MAP_SHARED
, mem_cfg_fd
, 0);
344 if (rte_mem_cfg_addr
== MAP_FAILED
){
345 rte_panic("Cannot mmap memory for rte_config\n");
347 memcpy(rte_mem_cfg_addr
, &early_mem_config
, sizeof(early_mem_config
));
348 rte_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 */
352 rte_config
.mem_config
->mem_cfg_addr
= (uintptr_t) rte_mem_cfg_addr
;
354 rte_config
.mem_config
->dma_maskbits
= 0;
358 /* attach to an existing shared memory config */
360 rte_eal_config_attach(void)
362 struct rte_mem_config
*mem_config
;
364 const char *pathname
= eal_runtime_config_path();
366 if (internal_config
.no_shconf
)
370 mem_cfg_fd
= open(pathname
, O_RDWR
);
372 rte_panic("Cannot open '%s' for rte_mem_config\n", pathname
);
375 /* map it as read-only first */
376 mem_config
= (struct rte_mem_config
*) mmap(NULL
, sizeof(*mem_config
),
377 PROT_READ
, MAP_SHARED
, mem_cfg_fd
, 0);
378 if (mem_config
== MAP_FAILED
)
379 rte_panic("Cannot mmap memory for rte_config! error %i (%s)\n",
380 errno
, strerror(errno
));
382 rte_config
.mem_config
= mem_config
;
385 /* reattach the shared config at exact memory location primary process has it */
387 rte_eal_config_reattach(void)
389 struct rte_mem_config
*mem_config
;
390 void *rte_mem_cfg_addr
;
392 if (internal_config
.no_shconf
)
395 /* save the address primary process has mapped shared config to */
396 rte_mem_cfg_addr
= (void *) (uintptr_t) rte_config
.mem_config
->mem_cfg_addr
;
398 /* unmap original config */
399 munmap(rte_config
.mem_config
, sizeof(struct rte_mem_config
));
401 /* remap the config at proper address */
402 mem_config
= (struct rte_mem_config
*) mmap(rte_mem_cfg_addr
,
403 sizeof(*mem_config
), PROT_READ
| PROT_WRITE
, MAP_SHARED
,
405 if (mem_config
== MAP_FAILED
|| mem_config
!= rte_mem_cfg_addr
) {
406 if (mem_config
!= MAP_FAILED
)
407 /* errno is stale, don't use */
408 rte_panic("Cannot mmap memory for rte_config at [%p], got [%p]"
409 " - please use '--base-virtaddr' option\n",
410 rte_mem_cfg_addr
, mem_config
);
412 rte_panic("Cannot mmap memory for rte_config! error %i (%s)\n",
413 errno
, strerror(errno
));
417 rte_config
.mem_config
= mem_config
;
420 /* Detect if we are a primary or a secondary process */
422 eal_proc_type_detect(void)
424 enum rte_proc_type_t ptype
= RTE_PROC_PRIMARY
;
425 const char *pathname
= eal_runtime_config_path();
427 /* if there no shared config, there can be no secondary processes */
428 if (!internal_config
.no_shconf
) {
429 /* if we can open the file but not get a write-lock we are a
430 * secondary process. NOTE: if we get a file handle back, we
431 * keep that open and don't close it to prevent a race condition
432 * between multiple opens.
434 if (((mem_cfg_fd
= open(pathname
, O_RDWR
)) >= 0) &&
435 (fcntl(mem_cfg_fd
, F_SETLK
, &wr_lock
) < 0))
436 ptype
= RTE_PROC_SECONDARY
;
439 RTE_LOG(INFO
, EAL
, "Auto-detected process type: %s\n",
440 ptype
== RTE_PROC_PRIMARY
? "PRIMARY" : "SECONDARY");
445 /* copies data from internal config to shared config */
447 eal_update_mem_config(void)
449 struct rte_mem_config
*mcfg
= rte_eal_get_configuration()->mem_config
;
450 mcfg
->legacy_mem
= internal_config
.legacy_mem
;
451 mcfg
->single_file_segments
= internal_config
.single_file_segments
;
454 /* copies data from shared config to internal config */
456 eal_update_internal_config(void)
458 struct rte_mem_config
*mcfg
= rte_eal_get_configuration()->mem_config
;
459 internal_config
.legacy_mem
= mcfg
->legacy_mem
;
460 internal_config
.single_file_segments
= mcfg
->single_file_segments
;
463 /* Sets up rte_config structure with the pointer to shared memory config.*/
465 rte_config_init(void)
467 rte_config
.process_type
= internal_config
.process_type
;
469 switch (rte_config
.process_type
){
470 case RTE_PROC_PRIMARY
:
471 rte_eal_config_create();
472 eal_update_mem_config();
474 case RTE_PROC_SECONDARY
:
475 rte_eal_config_attach();
476 rte_eal_mcfg_wait_complete(rte_config
.mem_config
);
477 rte_eal_config_reattach();
478 eal_update_internal_config();
481 case RTE_PROC_INVALID
:
482 rte_panic("Invalid process type\n");
486 /* Unlocks hugepage directories that were locked by eal_hugepage_info_init */
488 eal_hugedirs_unlock(void)
492 for (i
= 0; i
< MAX_HUGEPAGE_SIZES
; i
++)
494 /* skip uninitialized */
495 if (internal_config
.hugepage_info
[i
].lock_descriptor
< 0)
497 /* unlock hugepage file */
498 flock(internal_config
.hugepage_info
[i
].lock_descriptor
, LOCK_UN
);
499 close(internal_config
.hugepage_info
[i
].lock_descriptor
);
500 /* reset the field */
501 internal_config
.hugepage_info
[i
].lock_descriptor
= -1;
507 eal_usage(const char *prgname
)
509 printf("\nUsage: %s ", prgname
);
511 printf("EAL Linux options:\n"
512 " --"OPT_SOCKET_MEM
" Memory to allocate on sockets (comma separated values)\n"
513 " --"OPT_SOCKET_LIMIT
" Limit memory allocation on sockets (comma separated values)\n"
514 " --"OPT_HUGE_DIR
" Directory where hugetlbfs is mounted\n"
515 " --"OPT_FILE_PREFIX
" Prefix for hugepage filenames\n"
516 " --"OPT_BASE_VIRTADDR
" Base virtual address\n"
517 " --"OPT_CREATE_UIO_DEV
" Create /dev/uioX (usually done by hotplug)\n"
518 " --"OPT_VFIO_INTR
" Interrupt mode for VFIO (legacy|msi|msix)\n"
519 " --"OPT_LEGACY_MEM
" Legacy memory mode (no dynamic allocation, contiguous segments)\n"
520 " --"OPT_SINGLE_FILE_SEGMENTS
" Put all hugepage memory in single files\n"
521 " --"OPT_MATCH_ALLOCATIONS
" Free hugepages exactly as allocated\n"
523 /* Allow the application to print its usage message too if hook is set */
524 if ( rte_application_usage_hook
) {
525 printf("===== Application Usage =====\n\n");
526 rte_application_usage_hook(prgname
);
530 /* Set a per-application usage message */
532 rte_set_application_usage_hook( rte_usage_hook_t usage_func
)
534 rte_usage_hook_t old_func
;
536 /* Will be NULL on the first call to denote the last usage routine. */
537 old_func
= rte_application_usage_hook
;
538 rte_application_usage_hook
= usage_func
;
544 eal_parse_socket_arg(char *strval
, volatile uint64_t *socket_arg
)
546 char * arg
[RTE_MAX_NUMA_NODES
];
549 uint64_t total_mem
= 0;
551 len
= strnlen(strval
, SOCKET_MEM_STRLEN
);
552 if (len
== SOCKET_MEM_STRLEN
) {
553 RTE_LOG(ERR
, EAL
, "--socket-mem is too long\n");
557 /* all other error cases will be caught later */
558 if (!isdigit(strval
[len
-1]))
561 /* split the optarg into separate socket values */
562 arg_num
= rte_strsplit(strval
, len
,
563 arg
, RTE_MAX_NUMA_NODES
, ',');
565 /* if split failed, or 0 arguments */
569 /* parse each defined socket option */
571 for (i
= 0; i
< arg_num
; i
++) {
574 val
= strtoull(arg
[i
], &end
, 10);
576 /* check for invalid input */
578 (arg
[i
][0] == '\0') || (end
== NULL
) || (*end
!= '\0'))
589 eal_parse_base_virtaddr(const char *arg
)
595 addr
= strtoull(arg
, &end
, 16);
597 /* check for errors */
598 if ((errno
!= 0) || (arg
[0] == '\0') || end
== NULL
|| (*end
!= '\0'))
601 /* make sure we don't exceed 32-bit boundary on 32-bit target */
603 if (addr
>= UINTPTR_MAX
)
607 /* align the addr on 16M boundary, 16MB is the minimum huge page
608 * size on IBM Power architecture. If the addr is aligned to 16MB,
609 * it can align to 2MB for x86. So this alignment can also be used
611 internal_config
.base_virtaddr
=
612 RTE_PTR_ALIGN_CEIL((uintptr_t)addr
, (size_t)RTE_PGSIZE_16M
);
618 eal_parse_vfio_intr(const char *mode
)
623 enum rte_intr_mode value
;
625 { "legacy", RTE_INTR_MODE_LEGACY
},
626 { "msi", RTE_INTR_MODE_MSI
},
627 { "msix", RTE_INTR_MODE_MSIX
},
630 for (i
= 0; i
< RTE_DIM(map
); i
++) {
631 if (!strcmp(mode
, map
[i
].name
)) {
632 internal_config
.vfio_intr_mode
= map
[i
].value
;
639 /* Parse the arguments for --log-level only */
641 eal_log_level_parse(int argc
, char **argv
)
646 const int old_optind
= optind
;
647 const int old_optopt
= optopt
;
648 char * const old_optarg
= optarg
;
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_config
) : 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
;
692 while ((opt
= getopt_long(argc
, argvopt
, eal_short_options
,
693 eal_long_options
, &option_index
)) != EOF
) {
696 * getopt didn't recognise the option, lets parse the
697 * registered options to see if the flag is valid
700 ret
= rte_option_parse(argv
[optind
-1]);
709 ret
= eal_parse_common_option(opt
, optarg
, &internal_config
);
710 /* common parser is not happy */
716 /* common parser handled this option */
725 case OPT_HUGE_DIR_NUM
:
727 char *hdir
= strdup(optarg
);
729 RTE_LOG(ERR
, EAL
, "Could not store hugepage directory\n");
731 /* free old hugepage dir */
732 if (internal_config
.hugepage_dir
!= NULL
)
733 free(internal_config
.hugepage_dir
);
734 internal_config
.hugepage_dir
= hdir
;
738 case OPT_FILE_PREFIX_NUM
:
740 char *prefix
= strdup(optarg
);
742 RTE_LOG(ERR
, EAL
, "Could not store file prefix\n");
744 /* free old prefix */
745 if (internal_config
.hugefile_prefix
!= NULL
)
746 free(internal_config
.hugefile_prefix
);
747 internal_config
.hugefile_prefix
= prefix
;
751 case OPT_SOCKET_MEM_NUM
:
752 if (eal_parse_socket_arg(optarg
,
753 internal_config
.socket_mem
) < 0) {
754 RTE_LOG(ERR
, EAL
, "invalid parameters for --"
755 OPT_SOCKET_MEM
"\n");
760 internal_config
.force_sockets
= 1;
763 case OPT_SOCKET_LIMIT_NUM
:
764 if (eal_parse_socket_arg(optarg
,
765 internal_config
.socket_limit
) < 0) {
766 RTE_LOG(ERR
, EAL
, "invalid parameters for --"
767 OPT_SOCKET_LIMIT
"\n");
772 internal_config
.force_socket_limits
= 1;
775 case OPT_BASE_VIRTADDR_NUM
:
776 if (eal_parse_base_virtaddr(optarg
) < 0) {
777 RTE_LOG(ERR
, EAL
, "invalid parameter for --"
778 OPT_BASE_VIRTADDR
"\n");
785 case OPT_VFIO_INTR_NUM
:
786 if (eal_parse_vfio_intr(optarg
) < 0) {
787 RTE_LOG(ERR
, EAL
, "invalid parameters for --"
795 case OPT_CREATE_UIO_DEV_NUM
:
796 internal_config
.create_uio_dev
= 1;
799 case OPT_MBUF_POOL_OPS_NAME_NUM
:
801 char *ops_name
= strdup(optarg
);
802 if (ops_name
== NULL
)
803 RTE_LOG(ERR
, EAL
, "Could not store mbuf pool ops name\n");
805 /* free old ops name */
806 if (internal_config
.user_mbuf_pool_ops_name
!=
808 free(internal_config
.user_mbuf_pool_ops_name
);
810 internal_config
.user_mbuf_pool_ops_name
=
815 case OPT_MATCH_ALLOCATIONS_NUM
:
816 internal_config
.match_allocations
= 1;
820 if (opt
< OPT_LONG_MIN_NUM
&& isprint(opt
)) {
821 RTE_LOG(ERR
, EAL
, "Option %c is not supported "
823 } else if (opt
>= OPT_LONG_MIN_NUM
&&
824 opt
< OPT_LONG_MAX_NUM
) {
825 RTE_LOG(ERR
, EAL
, "Option %s is not supported "
827 eal_long_options
[option_index
].name
);
829 RTE_LOG(ERR
, EAL
, "Option %d is not supported "
838 /* create runtime data directory */
839 if (internal_config
.no_shconf
== 0 &&
840 eal_create_runtime_dir() < 0) {
841 RTE_LOG(ERR
, EAL
, "Cannot create runtime directory\n");
846 if (eal_adjust_config(&internal_config
) != 0) {
852 if (eal_check_common_options(&internal_config
) != 0) {
859 argv
[optind
-1] = prgname
;
863 /* restore getopt lib */
872 check_socket(const struct rte_memseg_list
*msl
, void *arg
)
874 int *socket_id
= arg
;
879 return *socket_id
== msl
->socket_id
;
883 eal_check_mem_on_local_socket(void)
887 socket_id
= rte_lcore_to_socket_id(rte_config
.master_lcore
);
889 if (rte_memseg_list_walk(check_socket
, &socket_id
) == 0)
890 RTE_LOG(WARNING
, EAL
, "WARNING: Master core has no memory on local socket!\n");
894 sync_func(__attribute__((unused
)) void *arg
)
900 rte_eal_mcfg_complete(void)
902 /* ALL shared mem_config related INIT DONE */
903 if (rte_config
.process_type
== RTE_PROC_PRIMARY
)
904 rte_config
.mem_config
->magic
= RTE_MAGIC
;
906 internal_config
.init_complete
= 1;
910 * Request iopl privilege for all RPL, returns 0 on success
911 * iopl() call is mostly for the i386 architecture. For other architectures,
912 * return -1 to indicate IO privilege can't be changed in this way.
915 rte_eal_iopl_init(void)
917 #if defined(RTE_ARCH_X86)
925 static int rte_eal_vfio_setup(void)
927 if (rte_vfio_enable("vfio"))
934 static void rte_eal_init_alert(const char *msg
)
936 fprintf(stderr
, "EAL: FATAL: %s\n", msg
);
937 RTE_LOG(ERR
, EAL
, "%s\n", msg
);
940 /* Launch threads, called at application init(). */
942 rte_eal_init(int argc
, char **argv
)
946 static rte_atomic32_t run_once
= RTE_ATOMIC32_INIT(0);
948 static char logid
[PATH_MAX
];
949 char cpuset
[RTE_CPU_AFFINITY_STR_LEN
];
950 char thread_name
[RTE_MAX_THREAD_NAME_LEN
];
952 /* checks if the machine is adequate */
953 if (!rte_cpu_is_supported()) {
954 rte_eal_init_alert("unsupported cpu type.");
959 if (!rte_atomic32_test_and_set(&run_once
)) {
960 rte_eal_init_alert("already called initialization.");
961 rte_errno
= EALREADY
;
965 p
= strrchr(argv
[0], '/');
966 strlcpy(logid
, p
? p
+ 1 : argv
[0], sizeof(logid
));
967 thread_id
= pthread_self();
969 eal_reset_internal_config(&internal_config
);
971 /* set log level as early as possible */
972 eal_log_level_parse(argc
, argv
);
974 if (rte_eal_cpu_init() < 0) {
975 rte_eal_init_alert("Cannot detect lcores.");
980 fctret
= eal_parse_args(argc
, argv
);
982 rte_eal_init_alert("Invalid 'command line' arguments.");
984 rte_atomic32_clear(&run_once
);
988 if (eal_plugins_init() < 0) {
989 rte_eal_init_alert("Cannot init plugins");
991 rte_atomic32_clear(&run_once
);
995 if (eal_option_device_parse()) {
997 rte_atomic32_clear(&run_once
);
1003 if (rte_eal_intr_init() < 0) {
1004 rte_eal_init_alert("Cannot init interrupt-handling thread");
1008 if (rte_eal_alarm_init() < 0) {
1009 rte_eal_init_alert("Cannot init interrupt-handling thread");
1010 /* rte_eal_alarm_init sets rte_errno on failure. */
1014 /* Put mp channel init before bus scan so that we can init the vdev
1015 * bus through mp channel in the secondary process before the bus scan.
1017 if (rte_mp_channel_init() < 0) {
1018 rte_eal_init_alert("failed to init mp channel");
1019 if (rte_eal_process_type() == RTE_PROC_PRIMARY
) {
1025 /* register multi-process action callbacks for hotplug */
1026 if (rte_mp_dev_hotplug_init() < 0) {
1027 rte_eal_init_alert("failed to register mp callback for hotplug");
1031 if (rte_bus_scan()) {
1032 rte_eal_init_alert("Cannot scan the buses for devices");
1034 rte_atomic32_clear(&run_once
);
1038 /* if no EAL option "--iova-mode=<pa|va>", use bus IOVA scheme */
1039 if (internal_config
.iova_mode
== RTE_IOVA_DC
) {
1040 /* autodetect the IOVA mapping mode (default is RTE_IOVA_PA) */
1041 rte_eal_get_configuration()->iova_mode
=
1042 rte_bus_get_iommu_class();
1044 /* Workaround for KNI which requires physical address to work */
1045 if (rte_eal_get_configuration()->iova_mode
== RTE_IOVA_VA
&&
1046 rte_eal_check_module("rte_kni") == 1) {
1047 rte_eal_get_configuration()->iova_mode
= RTE_IOVA_PA
;
1048 RTE_LOG(WARNING
, EAL
,
1049 "Some devices want IOVA as VA but PA will be used because.. "
1050 "KNI module inserted\n");
1053 rte_eal_get_configuration()->iova_mode
=
1054 internal_config
.iova_mode
;
1057 if (internal_config
.no_hugetlbfs
== 0) {
1058 /* rte_config isn't initialized yet */
1059 ret
= internal_config
.process_type
== RTE_PROC_PRIMARY
?
1060 eal_hugepage_info_init() :
1061 eal_hugepage_info_read();
1063 rte_eal_init_alert("Cannot get hugepage information.");
1065 rte_atomic32_clear(&run_once
);
1070 if (internal_config
.memory
== 0 && internal_config
.force_sockets
== 0) {
1071 if (internal_config
.no_hugetlbfs
)
1072 internal_config
.memory
= MEMSIZE_IF_NO_HUGE_PAGE
;
1075 if (internal_config
.vmware_tsc_map
== 1) {
1076 #ifdef RTE_LIBRTE_EAL_VMWARE_TSC_MAP_SUPPORT
1077 rte_cycles_vmware_tsc_map
= 1;
1078 RTE_LOG (DEBUG
, EAL
, "Using VMWARE TSC MAP, "
1079 "you must have monitor_control.pseudo_perfctr = TRUE\n");
1081 RTE_LOG (WARNING
, EAL
, "Ignoring --vmware-tsc-map because "
1082 "RTE_LIBRTE_EAL_VMWARE_TSC_MAP_SUPPORT is not set\n");
1086 rte_srand(rte_rdtsc());
1088 if (rte_eal_log_init(logid
, internal_config
.syslog_facility
) < 0) {
1089 rte_eal_init_alert("Cannot init logging.");
1091 rte_atomic32_clear(&run_once
);
1096 if (rte_eal_vfio_setup() < 0) {
1097 rte_eal_init_alert("Cannot init VFIO");
1099 rte_atomic32_clear(&run_once
);
1103 /* in secondary processes, memory init may allocate additional fbarrays
1104 * not present in primary processes, so to avoid any potential issues,
1105 * initialize memzones first.
1107 if (rte_eal_memzone_init() < 0) {
1108 rte_eal_init_alert("Cannot init memzone");
1113 if (rte_eal_memory_init() < 0) {
1114 rte_eal_init_alert("Cannot init memory");
1119 /* the directories are locked during eal_hugepage_info_init */
1120 eal_hugedirs_unlock();
1122 if (rte_eal_malloc_heap_init() < 0) {
1123 rte_eal_init_alert("Cannot init malloc heap");
1128 if (rte_eal_tailqs_init() < 0) {
1129 rte_eal_init_alert("Cannot init tail queues for objects");
1134 if (rte_eal_timer_init() < 0) {
1135 rte_eal_init_alert("Cannot init HPET or TSC timers");
1136 rte_errno
= ENOTSUP
;
1140 eal_check_mem_on_local_socket();
1142 eal_thread_init_master(rte_config
.master_lcore
);
1144 ret
= eal_thread_dump_affinity(cpuset
, sizeof(cpuset
));
1146 RTE_LOG(DEBUG
, EAL
, "Master lcore %u is ready (tid=%zx;cpuset=[%s%s])\n",
1147 rte_config
.master_lcore
, (uintptr_t)thread_id
, cpuset
,
1148 ret
== 0 ? "" : "...");
1150 RTE_LCORE_FOREACH_SLAVE(i
) {
1153 * create communication pipes between master thread
1156 if (pipe(lcore_config
[i
].pipe_master2slave
) < 0)
1157 rte_panic("Cannot create pipe\n");
1158 if (pipe(lcore_config
[i
].pipe_slave2master
) < 0)
1159 rte_panic("Cannot create pipe\n");
1161 lcore_config
[i
].state
= WAIT
;
1163 /* create a thread for each lcore */
1164 ret
= pthread_create(&lcore_config
[i
].thread_id
, NULL
,
1165 eal_thread_loop
, NULL
);
1167 rte_panic("Cannot create thread\n");
1169 /* Set thread_name for aid in debugging. */
1170 snprintf(thread_name
, sizeof(thread_name
),
1171 "lcore-slave-%d", i
);
1172 ret
= rte_thread_setname(lcore_config
[i
].thread_id
,
1176 "Cannot set name for lcore thread\n");
1180 * Launch a dummy function on all slave lcores, so that master lcore
1181 * knows they are all ready when this function returns.
1183 rte_eal_mp_remote_launch(sync_func
, NULL
, SKIP_MASTER
);
1184 rte_eal_mp_wait_lcore();
1186 /* initialize services so vdevs register service during bus_probe. */
1187 ret
= rte_service_init();
1189 rte_eal_init_alert("rte_service_init() failed");
1190 rte_errno
= ENOEXEC
;
1194 /* Probe all the buses and devices/drivers on them */
1195 if (rte_bus_probe()) {
1196 rte_eal_init_alert("Cannot probe devices");
1197 rte_errno
= ENOTSUP
;
1202 /* Register mp action after probe() so that we got enough info */
1203 if (rte_vfio_is_enabled("vfio") && vfio_mp_sync_setup() < 0)
1207 /* initialize default service/lcore mappings and start running. Ignore
1208 * -ENOTSUP, as it indicates no service coremask passed to EAL.
1210 ret
= rte_service_start_with_defaults();
1211 if (ret
< 0 && ret
!= -ENOTSUP
) {
1212 rte_errno
= ENOEXEC
;
1217 * Clean up unused files in runtime directory. We do this at the end of
1218 * init and not at the beginning because we want to clean stuff up
1219 * whether we are primary or secondary process, but we cannot remove
1220 * primary process' files because secondary should be able to run even
1221 * if primary process is dead.
1223 * In no_shconf mode, no runtime directory is created in the first
1224 * place, so no cleanup needed.
1226 if (!internal_config
.no_shconf
&& eal_clean_runtime_dir() < 0) {
1227 rte_eal_init_alert("Cannot clear runtime directory\n");
1231 rte_eal_mcfg_complete();
1233 /* Call each registered callback, if enabled */
1240 mark_freeable(const struct rte_memseg_list
*msl
, const struct rte_memseg
*ms
,
1241 void *arg __rte_unused
)
1243 /* ms is const, so find this memseg */
1244 struct rte_memseg
*found
;
1249 found
= rte_mem_virt2memseg(ms
->addr
, msl
);
1251 found
->flags
&= ~RTE_MEMSEG_FLAG_DO_NOT_FREE
;
1257 rte_eal_cleanup(void)
1259 /* if we're in a primary process, we need to mark hugepages as freeable
1260 * so that finalization can release them back to the system.
1262 if (rte_eal_process_type() == RTE_PROC_PRIMARY
)
1263 rte_memseg_walk(mark_freeable
, NULL
);
1264 rte_service_finalize();
1265 rte_mp_channel_cleanup();
1266 eal_cleanup_config(&internal_config
);
1271 enum rte_lcore_role_t
1272 rte_eal_lcore_role(unsigned lcore_id
)
1274 return rte_config
.lcore_role
[lcore_id
];
1277 enum rte_proc_type_t
1278 rte_eal_process_type(void)
1280 return rte_config
.process_type
;
1283 int rte_eal_has_hugepages(void)
1285 return ! internal_config
.no_hugetlbfs
;
1288 int rte_eal_has_pci(void)
1290 return !internal_config
.no_pci
;
1293 int rte_eal_create_uio_dev(void)
1295 return internal_config
.create_uio_dev
;
1299 rte_eal_vfio_intr_mode(void)
1301 return internal_config
.vfio_intr_mode
;
1305 rte_eal_check_module(const char *module_name
)
1307 char sysfs_mod_name
[PATH_MAX
];
1311 if (NULL
== module_name
)
1314 /* Check if there is sysfs mounted */
1315 if (stat("/sys/module", &st
) != 0) {
1316 RTE_LOG(DEBUG
, EAL
, "sysfs is not mounted! error %i (%s)\n",
1317 errno
, strerror(errno
));
1321 /* A module might be built-in, therefore try sysfs */
1322 n
= snprintf(sysfs_mod_name
, PATH_MAX
, "/sys/module/%s", module_name
);
1323 if (n
< 0 || n
> PATH_MAX
) {
1324 RTE_LOG(DEBUG
, EAL
, "Could not format module path\n");
1328 if (stat(sysfs_mod_name
, &st
) != 0) {
1329 RTE_LOG(DEBUG
, EAL
, "Module %s not found! error %i (%s)\n",
1330 sysfs_mod_name
, errno
, strerror(errno
));
1334 /* Module has been found */