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");
99 eal_usage(const char *prgname)
101 printf("\nUsage: %s ", prgname);
103 /* Allow the application to print its usage message too
106 if (rte_application_usage_hook) {
107 printf("===== Application Usage =====\n\n");
108 rte_application_usage_hook(prgname);
112 /* Parse the arguments for --log-level only */
114 eal_log_level_parse(int argc, char **argv)
122 eal_reset_internal_config(&internal_config);
124 while ((opt = getopt_long(argc, argvopt, eal_short_options,
125 eal_long_options, &option_index)) != EOF) {
129 /* getopt is not happy, stop right now */
133 ret = (opt == OPT_LOG_LEVEL_NUM) ?
134 eal_parse_common_option(opt, optarg,
135 &internal_config) : 0;
137 /* common parser is not happy */
142 optind = 0; /* reset getopt lib */
145 /* Parse the argument given in the command line of the application */
147 eal_parse_args(int argc, char **argv)
152 char *prgname = argv[0];
156 while ((opt = getopt_long(argc, argvopt, eal_short_options,
157 eal_long_options, &option_index)) != EOF) {
161 /* getopt is not happy, stop right now */
167 ret = eal_parse_common_option(opt, optarg, &internal_config);
168 /* common parser is not happy */
173 /* common parser handled this option */
182 if (opt < OPT_LONG_MIN_NUM && isprint(opt)) {
183 RTE_LOG(ERR, EAL, "Option %c is not supported "
184 "on Windows\n", opt);
185 } else if (opt >= OPT_LONG_MIN_NUM &&
186 opt < OPT_LONG_MAX_NUM) {
187 RTE_LOG(ERR, EAL, "Option %s is not supported "
189 eal_long_options[option_index].name);
191 RTE_LOG(ERR, EAL, "Option %d is not supported "
192 "on Windows\n", opt);
199 if (eal_adjust_config(&internal_config) != 0)
203 if (eal_check_common_options(&internal_config) != 0) {
209 argv[optind - 1] = prgname;
211 optind = 0; /* reset getopt lib */
216 sync_func(void *arg __rte_unused)
222 rte_eal_init_alert(const char *msg)
224 fprintf(stderr, "EAL: FATAL: %s\n", msg);
225 RTE_LOG(ERR, EAL, "%s\n", msg);
228 /* Stubs to enable EAL trace point compilation
229 * until eal_common_trace.c can be compiled.
232 RTE_DEFINE_PER_LCORE(volatile int, trace_point_sz);
233 RTE_DEFINE_PER_LCORE(void *, trace_mem);
236 __rte_trace_mem_per_thread_alloc(void)
241 __rte_trace_point_emit_field(size_t sz, const char *field,
250 __rte_trace_point_register(rte_trace_point_t *trace, const char *name,
251 void (*register_fn)(void))
255 RTE_SET_USED(register_fn);
259 /* Launch threads, called at application init(). */
261 rte_eal_init(int argc, char **argv)
265 rte_eal_log_init(NULL, 0);
267 eal_log_level_parse(argc, argv);
269 if (eal_create_cpu_map() < 0) {
270 rte_eal_init_alert("Cannot discover CPU and NUMA.");
271 /* rte_errno is set */
275 if (rte_eal_cpu_init() < 0) {
276 rte_eal_init_alert("Cannot detect lcores.");
281 fctret = eal_parse_args(argc, argv);
285 if (!internal_config.no_hugetlbfs && (eal_hugepage_info_init() < 0)) {
286 rte_eal_init_alert("Cannot get hugepage information");
291 if (internal_config.memory == 0 && !internal_config.force_sockets) {
292 if (internal_config.no_hugetlbfs)
293 internal_config.memory = MEMSIZE_IF_NO_HUGE_PAGE;
296 eal_thread_init_master(rte_config.master_lcore);
298 RTE_LCORE_FOREACH_SLAVE(i) {
301 * create communication pipes between master thread
304 if (_pipe(lcore_config[i].pipe_master2slave,
305 sizeof(char), _O_BINARY) < 0)
306 rte_panic("Cannot create pipe\n");
307 if (_pipe(lcore_config[i].pipe_slave2master,
308 sizeof(char), _O_BINARY) < 0)
309 rte_panic("Cannot create pipe\n");
311 lcore_config[i].state = WAIT;
313 /* create a thread for each lcore */
314 if (eal_thread_create(&lcore_config[i].thread_id) != 0)
315 rte_panic("Cannot create thread\n");
319 * Launch a dummy function on all slave lcores, so that master lcore
320 * knows they are all ready when this function returns.
322 rte_eal_mp_remote_launch(sync_func, NULL, SKIP_MASTER);
323 rte_eal_mp_wait_lcore();