1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2019 Intel Corporation
12 #include <rte_debug.h>
14 #include <eal_memcfg.h>
15 #include <rte_errno.h>
16 #include <rte_lcore.h>
17 #include <eal_thread.h>
18 #include <eal_internal_cfg.h>
19 #include <eal_filesystem.h>
20 #include <eal_options.h>
21 #include <eal_private.h>
22 #include <rte_service_component.h>
25 #include "eal_hugepages.h"
26 #include "eal_trace.h"
28 #include "eal_windows.h"
30 #define MEMSIZE_IF_NO_HUGE_PAGE (64ULL * 1024ULL * 1024ULL)
32 /* define fd variable here, because file needs to be kept open for the
33 * duration of the program, as we hold a write lock on it in the primary proc
35 static int mem_cfg_fd = -1;
37 /* internal configuration (per-core) */
38 struct lcore_config lcore_config[RTE_MAX_LCORE];
40 /* Detect if we are a primary or a secondary process */
42 eal_proc_type_detect(void)
44 enum rte_proc_type_t ptype = RTE_PROC_PRIMARY;
45 const char *pathname = eal_runtime_config_path();
46 const struct rte_config *config = rte_eal_get_configuration();
48 /* if we can open the file but not get a write-lock we are a secondary
49 * process. NOTE: if we get a file handle back, we keep that open
50 * and don't close it to prevent a race condition between multiple opens
52 errno_t err = _sopen_s(&mem_cfg_fd, pathname,
53 _O_RDWR, _SH_DENYNO, _S_IREAD | _S_IWRITE);
55 OVERLAPPED soverlapped = { 0 };
56 soverlapped.Offset = sizeof(*config->mem_config);
57 soverlapped.OffsetHigh = 0;
59 HANDLE hwinfilehandle = (HANDLE)_get_osfhandle(mem_cfg_fd);
61 if (!LockFileEx(hwinfilehandle,
62 LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY, 0,
63 sizeof(*config->mem_config), 0, &soverlapped))
64 ptype = RTE_PROC_SECONDARY;
67 RTE_LOG(INFO, EAL, "Auto-detected process type: %s\n",
68 ptype == RTE_PROC_PRIMARY ? "PRIMARY" : "SECONDARY");
81 eal_usage(const char *prgname)
83 rte_usage_hook_t hook = eal_get_application_usage_hook();
85 printf("\nUsage: %s ", prgname);
87 /* Allow the application to print its usage message too
91 printf("===== Application Usage =====\n\n");
96 /* Parse the arguments for --log-level only */
98 eal_log_level_parse(int argc, char **argv)
103 struct internal_config *internal_conf =
104 eal_get_internal_configuration();
108 eal_reset_internal_config(internal_conf);
110 while ((opt = getopt_long(argc, argvopt, eal_short_options,
111 eal_long_options, &option_index)) != EOF) {
115 /* getopt is not happy, stop right now */
119 ret = (opt == OPT_LOG_LEVEL_NUM) ?
120 eal_parse_common_option(opt, optarg,
123 /* common parser is not happy */
128 optind = 0; /* reset getopt lib */
131 /* Parse the argument given in the command line of the application */
133 eal_parse_args(int argc, char **argv)
138 char *prgname = argv[0];
139 struct internal_config *internal_conf =
140 eal_get_internal_configuration();
144 while ((opt = getopt_long(argc, argvopt, eal_short_options,
145 eal_long_options, &option_index)) != EOF) {
149 /* getopt is not happy, stop right now */
155 /* eal_log_level_parse() already handled this option */
156 if (opt == OPT_LOG_LEVEL_NUM)
159 ret = eal_parse_common_option(opt, optarg, internal_conf);
160 /* common parser is not happy */
165 /* common parser handled this option */
174 if (opt < OPT_LONG_MIN_NUM && isprint(opt)) {
175 RTE_LOG(ERR, EAL, "Option %c is not supported "
176 "on Windows\n", opt);
177 } else if (opt >= OPT_LONG_MIN_NUM &&
178 opt < OPT_LONG_MAX_NUM) {
179 RTE_LOG(ERR, EAL, "Option %s is not supported "
181 eal_long_options[option_index].name);
183 RTE_LOG(ERR, EAL, "Option %d is not supported "
184 "on Windows\n", opt);
191 if (eal_adjust_config(internal_conf) != 0)
195 if (eal_check_common_options(internal_conf) != 0) {
201 argv[optind - 1] = prgname;
203 optind = 0; /* reset getopt lib */
208 sync_func(void *arg __rte_unused)
214 rte_eal_init_alert(const char *msg)
216 fprintf(stderr, "EAL: FATAL: %s\n", msg);
217 RTE_LOG(ERR, EAL, "%s\n", msg);
220 /* Stubs to enable EAL trace point compilation
221 * until eal_common_trace.c can be compiled.
224 RTE_DEFINE_PER_LCORE(volatile int, trace_point_sz);
225 RTE_DEFINE_PER_LCORE(void *, trace_mem);
228 __rte_trace_mem_per_thread_alloc(void)
233 trace_mem_per_thread_free(void)
238 __rte_trace_point_emit_field(size_t sz, const char *field,
247 __rte_trace_point_register(rte_trace_point_t *trace, const char *name,
248 void (*register_fn)(void))
252 RTE_SET_USED(register_fn);
257 rte_eal_cleanup(void)
259 struct internal_config *internal_conf =
260 eal_get_internal_configuration();
261 /* after this point, any DPDK pointers will become dangling */
262 rte_eal_memory_detach();
263 eal_cleanup_config(internal_conf);
267 /* Launch threads, called at application init(). */
269 rte_eal_init(int argc, char **argv)
271 int i, fctret, bscan;
272 const struct rte_config *config = rte_eal_get_configuration();
273 struct internal_config *internal_conf =
274 eal_get_internal_configuration();
277 eal_log_init(NULL, 0);
279 eal_log_level_parse(argc, argv);
281 if (eal_create_cpu_map() < 0) {
282 rte_eal_init_alert("Cannot discover CPU and NUMA.");
283 /* rte_errno is set */
287 if (rte_eal_cpu_init() < 0) {
288 rte_eal_init_alert("Cannot detect lcores.");
293 fctret = eal_parse_args(argc, argv);
297 if (eal_option_device_parse()) {
302 /* Prevent creation of shared memory files. */
303 if (internal_conf->in_memory == 0) {
304 RTE_LOG(WARNING, EAL, "Multi-process support is requested, "
305 "but not available.\n");
306 internal_conf->in_memory = 1;
307 internal_conf->no_shconf = 1;
310 if (!internal_conf->no_hugetlbfs && (eal_hugepage_info_init() < 0)) {
311 rte_eal_init_alert("Cannot get hugepage information");
316 if (internal_conf->memory == 0 && !internal_conf->force_sockets) {
317 if (internal_conf->no_hugetlbfs)
318 internal_conf->memory = MEMSIZE_IF_NO_HUGE_PAGE;
321 if (eal_mem_win32api_init() < 0) {
322 rte_eal_init_alert("Cannot access Win32 memory management");
327 if (eal_mem_virt2iova_init() < 0) {
328 /* Non-fatal error if physical addresses are not required. */
329 RTE_LOG(WARNING, EAL, "Cannot access virt2phys driver, "
330 "PA will not be available\n");
333 if (rte_eal_memzone_init() < 0) {
334 rte_eal_init_alert("Cannot init memzone");
339 if (rte_eal_memory_init() < 0) {
340 rte_eal_init_alert("Cannot init memory");
345 if (rte_eal_malloc_heap_init() < 0) {
346 rte_eal_init_alert("Cannot init malloc heap");
351 if (rte_eal_tailqs_init() < 0) {
352 rte_eal_init_alert("Cannot init tail queues for objects");
357 if (rte_eal_intr_init() < 0) {
358 rte_eal_init_alert("Cannot init interrupt-handling thread");
362 if (rte_eal_timer_init() < 0) {
363 rte_eal_init_alert("Cannot init TSC timer");
368 __rte_thread_init(config->main_lcore,
369 &lcore_config[config->main_lcore].cpuset);
371 bscan = rte_bus_scan();
373 rte_eal_init_alert("Cannot init PCI");
378 RTE_LCORE_FOREACH_WORKER(i) {
381 * create communication pipes between main thread
384 if (_pipe(lcore_config[i].pipe_main2worker,
385 sizeof(char), _O_BINARY) < 0)
386 rte_panic("Cannot create pipe\n");
387 if (_pipe(lcore_config[i].pipe_worker2main,
388 sizeof(char), _O_BINARY) < 0)
389 rte_panic("Cannot create pipe\n");
391 lcore_config[i].state = WAIT;
393 /* create a thread for each lcore */
394 if (eal_thread_create(&lcore_config[i].thread_id) != 0)
395 rte_panic("Cannot create thread\n");
398 /* Initialize services so drivers can register services during probe. */
399 ret = rte_service_init();
401 rte_eal_init_alert("rte_service_init() failed");
406 if (rte_bus_probe()) {
407 rte_eal_init_alert("Cannot probe devices");
413 * Launch a dummy function on all worker lcores, so that main lcore
414 * knows they are all ready when this function returns.
416 rte_eal_mp_remote_launch(sync_func, NULL, SKIP_MAIN);
417 rte_eal_mp_wait_lcore();
421 /* Don't use MinGW asprintf() to have identical code with all toolchains. */
423 eal_asprintf(char **buffer, const char *format, ...)
428 va_start(arg, format);
429 size = vsnprintf(NULL, 0, format, arg);
435 *buffer = malloc(size);
439 va_start(arg, format);
440 ret = vsnprintf(*buffer, size, format, arg);
442 if (ret != size - 1) {
450 rte_vfio_container_dma_map(__rte_unused int container_fd,
451 __rte_unused uint64_t vaddr,
452 __rte_unused uint64_t iova,
453 __rte_unused uint64_t len)
459 rte_vfio_container_dma_unmap(__rte_unused int container_fd,
460 __rte_unused uint64_t vaddr,
461 __rte_unused uint64_t iova,
462 __rte_unused uint64_t len)