1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2019 Intel Corporation
10 #include <rte_debug.h>
12 #include <eal_memcfg.h>
13 #include <rte_errno.h>
14 #include <rte_lcore.h>
15 #include <eal_thread.h>
16 #include <eal_internal_cfg.h>
17 #include <eal_filesystem.h>
18 #include <eal_options.h>
19 #include <eal_private.h>
20 #include <rte_trace_point.h>
22 #include "eal_hugepages.h"
23 #include "eal_windows.h"
25 #define MEMSIZE_IF_NO_HUGE_PAGE (64ULL * 1024ULL * 1024ULL)
27 /* Allow the application to print its usage message too if set */
28 static rte_usage_hook_t rte_application_usage_hook;
30 /* define fd variable here, because file needs to be kept open for the
31 * duration of the program, as we hold a write lock on it in the primary proc
33 static int mem_cfg_fd = -1;
35 /* early configuration structure, when memory config is not mmapped */
36 static struct rte_mem_config early_mem_config;
38 /* Address of global and public configuration */
39 static struct rte_config rte_config = {
40 .mem_config = &early_mem_config,
43 /* internal configuration (per-core) */
44 struct lcore_config lcore_config[RTE_MAX_LCORE];
46 /* internal configuration */
47 struct internal_config internal_config;
49 /* platform-specific runtime dir */
50 static char runtime_dir[PATH_MAX];
53 rte_eal_get_runtime_dir(void)
58 /* Return a pointer to the configuration structure */
60 rte_eal_get_configuration(void)
65 /* Detect if we are a primary or a secondary process */
67 eal_proc_type_detect(void)
69 enum rte_proc_type_t ptype = RTE_PROC_PRIMARY;
70 const char *pathname = eal_runtime_config_path();
72 /* if we can open the file but not get a write-lock we are a secondary
73 * process. NOTE: if we get a file handle back, we keep that open
74 * and don't close it to prevent a race condition between multiple opens
76 errno_t err = _sopen_s(&mem_cfg_fd, pathname,
77 _O_RDWR, _SH_DENYNO, _S_IREAD | _S_IWRITE);
79 OVERLAPPED soverlapped = { 0 };
80 soverlapped.Offset = sizeof(*rte_config.mem_config);
81 soverlapped.OffsetHigh = 0;
83 HANDLE hwinfilehandle = (HANDLE)_get_osfhandle(mem_cfg_fd);
85 if (!LockFileEx(hwinfilehandle,
86 LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY, 0,
87 sizeof(*rte_config.mem_config), 0, &soverlapped))
88 ptype = RTE_PROC_SECONDARY;
91 RTE_LOG(INFO, EAL, "Auto-detected process type: %s\n",
92 ptype == RTE_PROC_PRIMARY ? "PRIMARY" : "SECONDARY");
98 rte_eal_process_type(void)
100 return rte_config.process_type;
104 rte_eal_has_hugepages(void)
106 return !internal_config.no_hugetlbfs;
110 rte_eal_iova_mode(void)
112 return rte_config.iova_mode;
117 eal_usage(const char *prgname)
119 printf("\nUsage: %s ", prgname);
121 /* Allow the application to print its usage message too
124 if (rte_application_usage_hook) {
125 printf("===== Application Usage =====\n\n");
126 rte_application_usage_hook(prgname);
130 /* Parse the arguments for --log-level only */
132 eal_log_level_parse(int argc, char **argv)
140 eal_reset_internal_config(&internal_config);
142 while ((opt = getopt_long(argc, argvopt, eal_short_options,
143 eal_long_options, &option_index)) != EOF) {
147 /* getopt is not happy, stop right now */
151 ret = (opt == OPT_LOG_LEVEL_NUM) ?
152 eal_parse_common_option(opt, optarg,
153 &internal_config) : 0;
155 /* common parser is not happy */
160 optind = 0; /* reset getopt lib */
163 /* Parse the argument given in the command line of the application */
165 eal_parse_args(int argc, char **argv)
170 char *prgname = argv[0];
174 while ((opt = getopt_long(argc, argvopt, eal_short_options,
175 eal_long_options, &option_index)) != EOF) {
179 /* getopt is not happy, stop right now */
185 ret = eal_parse_common_option(opt, optarg, &internal_config);
186 /* common parser is not happy */
191 /* common parser handled this option */
200 if (opt < OPT_LONG_MIN_NUM && isprint(opt)) {
201 RTE_LOG(ERR, EAL, "Option %c is not supported "
202 "on Windows\n", opt);
203 } else if (opt >= OPT_LONG_MIN_NUM &&
204 opt < OPT_LONG_MAX_NUM) {
205 RTE_LOG(ERR, EAL, "Option %s is not supported "
207 eal_long_options[option_index].name);
209 RTE_LOG(ERR, EAL, "Option %d is not supported "
210 "on Windows\n", opt);
217 if (eal_adjust_config(&internal_config) != 0)
221 if (eal_check_common_options(&internal_config) != 0) {
227 argv[optind - 1] = prgname;
229 optind = 0; /* reset getopt lib */
234 sync_func(void *arg __rte_unused)
240 rte_eal_init_alert(const char *msg)
242 fprintf(stderr, "EAL: FATAL: %s\n", msg);
243 RTE_LOG(ERR, EAL, "%s\n", msg);
246 /* Stubs to enable EAL trace point compilation
247 * until eal_common_trace.c can be compiled.
250 RTE_DEFINE_PER_LCORE(volatile int, trace_point_sz);
251 RTE_DEFINE_PER_LCORE(void *, trace_mem);
254 __rte_trace_mem_per_thread_alloc(void)
259 __rte_trace_point_emit_field(size_t sz, const char *field,
268 __rte_trace_point_register(rte_trace_point_t *trace, const char *name,
269 void (*register_fn)(void))
273 RTE_SET_USED(register_fn);
278 rte_eal_cleanup(void)
280 eal_cleanup_config(&internal_config);
284 /* Launch threads, called at application init(). */
286 rte_eal_init(int argc, char **argv)
290 rte_eal_log_init(NULL, 0);
292 eal_log_level_parse(argc, argv);
294 if (eal_create_cpu_map() < 0) {
295 rte_eal_init_alert("Cannot discover CPU and NUMA.");
296 /* rte_errno is set */
300 if (rte_eal_cpu_init() < 0) {
301 rte_eal_init_alert("Cannot detect lcores.");
306 fctret = eal_parse_args(argc, argv);
310 /* Prevent creation of shared memory files. */
311 if (internal_config.in_memory == 0) {
312 RTE_LOG(WARNING, EAL, "Multi-process support is requested, "
313 "but not available.\n");
314 internal_config.in_memory = 1;
317 if (!internal_config.no_hugetlbfs && (eal_hugepage_info_init() < 0)) {
318 rte_eal_init_alert("Cannot get hugepage information");
323 if (internal_config.memory == 0 && !internal_config.force_sockets) {
324 if (internal_config.no_hugetlbfs)
325 internal_config.memory = MEMSIZE_IF_NO_HUGE_PAGE;
328 if (eal_mem_win32api_init() < 0) {
329 rte_eal_init_alert("Cannot access Win32 memory management");
334 if (eal_mem_virt2iova_init() < 0) {
335 /* Non-fatal error if physical addresses are not required. */
336 RTE_LOG(WARNING, EAL, "Cannot access virt2phys driver, "
337 "PA will not be available\n");
340 if (rte_eal_memzone_init() < 0) {
341 rte_eal_init_alert("Cannot init memzone");
346 if (rte_eal_memory_init() < 0) {
347 rte_eal_init_alert("Cannot init memory");
352 if (rte_eal_malloc_heap_init() < 0) {
353 rte_eal_init_alert("Cannot init malloc heap");
358 if (rte_eal_tailqs_init() < 0) {
359 rte_eal_init_alert("Cannot init tail queues for objects");
364 if (rte_eal_timer_init() < 0) {
365 rte_eal_init_alert("Cannot init TSC timer");
370 eal_thread_init_master(rte_config.master_lcore);
372 RTE_LCORE_FOREACH_SLAVE(i) {
375 * create communication pipes between master thread
378 if (_pipe(lcore_config[i].pipe_master2slave,
379 sizeof(char), _O_BINARY) < 0)
380 rte_panic("Cannot create pipe\n");
381 if (_pipe(lcore_config[i].pipe_slave2master,
382 sizeof(char), _O_BINARY) < 0)
383 rte_panic("Cannot create pipe\n");
385 lcore_config[i].state = WAIT;
387 /* create a thread for each lcore */
388 if (eal_thread_create(&lcore_config[i].thread_id) != 0)
389 rte_panic("Cannot create thread\n");
393 * Launch a dummy function on all slave lcores, so that master lcore
394 * knows they are all ready when this function returns.
396 rte_eal_mp_remote_launch(sync_func, NULL, SKIP_MASTER);
397 rte_eal_mp_wait_lcore();