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_firmware.h"
26 #include "eal_hugepages.h"
27 #include "eal_trace.h"
29 #include "eal_windows.h"
31 #define MEMSIZE_IF_NO_HUGE_PAGE (64ULL * 1024ULL * 1024ULL)
33 /* define fd variable here, because file needs to be kept open for the
34 * duration of the program, as we hold a write lock on it in the primary proc
36 static int mem_cfg_fd = -1;
38 /* internal configuration (per-core) */
39 struct lcore_config lcore_config[RTE_MAX_LCORE];
41 /* Detect if we are a primary or a secondary process */
43 eal_proc_type_detect(void)
45 enum rte_proc_type_t ptype = RTE_PROC_PRIMARY;
46 const char *pathname = eal_runtime_config_path();
47 const struct rte_config *config = rte_eal_get_configuration();
49 /* if we can open the file but not get a write-lock we are a secondary
50 * process. NOTE: if we get a file handle back, we keep that open
51 * and don't close it to prevent a race condition between multiple opens
53 errno_t err = _sopen_s(&mem_cfg_fd, pathname,
54 _O_RDWR, _SH_DENYNO, _S_IREAD | _S_IWRITE);
56 OVERLAPPED soverlapped = { 0 };
57 soverlapped.Offset = sizeof(*config->mem_config);
58 soverlapped.OffsetHigh = 0;
60 HANDLE hwinfilehandle = (HANDLE)_get_osfhandle(mem_cfg_fd);
62 if (!LockFileEx(hwinfilehandle,
63 LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY, 0,
64 sizeof(*config->mem_config), 0, &soverlapped))
65 ptype = RTE_PROC_SECONDARY;
68 RTE_LOG(INFO, EAL, "Auto-detected process type: %s\n",
69 ptype == RTE_PROC_PRIMARY ? "PRIMARY" : "SECONDARY");
82 eal_usage(const char *prgname)
84 rte_usage_hook_t hook = eal_get_application_usage_hook();
86 printf("\nUsage: %s ", prgname);
88 /* Allow the application to print its usage message too
92 printf("===== Application Usage =====\n\n");
97 /* Parse the arguments for --log-level only */
99 eal_log_level_parse(int argc, char **argv)
104 struct internal_config *internal_conf =
105 eal_get_internal_configuration();
109 eal_reset_internal_config(internal_conf);
111 while ((opt = getopt_long(argc, argvopt, eal_short_options,
112 eal_long_options, &option_index)) != EOF) {
116 /* getopt is not happy, stop right now */
120 ret = (opt == OPT_LOG_LEVEL_NUM) ?
121 eal_parse_common_option(opt, optarg,
124 /* common parser is not happy */
129 optind = 0; /* reset getopt lib */
132 /* Parse the argument given in the command line of the application */
134 eal_parse_args(int argc, char **argv)
139 char *prgname = argv[0];
140 struct internal_config *internal_conf =
141 eal_get_internal_configuration();
145 while ((opt = getopt_long(argc, argvopt, eal_short_options,
146 eal_long_options, &option_index)) != EOF) {
150 /* getopt is not happy, stop right now */
156 /* eal_log_level_parse() already handled this option */
157 if (opt == OPT_LOG_LEVEL_NUM)
160 ret = eal_parse_common_option(opt, optarg, internal_conf);
161 /* common parser is not happy */
166 /* common parser handled this option */
175 if (opt < OPT_LONG_MIN_NUM && isprint(opt)) {
176 RTE_LOG(ERR, EAL, "Option %c is not supported "
177 "on Windows\n", opt);
178 } else if (opt >= OPT_LONG_MIN_NUM &&
179 opt < OPT_LONG_MAX_NUM) {
180 RTE_LOG(ERR, EAL, "Option %s is not supported "
182 eal_long_options[option_index].name);
184 RTE_LOG(ERR, EAL, "Option %d is not supported "
185 "on Windows\n", opt);
192 if (eal_adjust_config(internal_conf) != 0)
196 if (eal_check_common_options(internal_conf) != 0) {
202 argv[optind - 1] = prgname;
204 optind = 0; /* reset getopt lib */
209 sync_func(void *arg __rte_unused)
215 rte_eal_init_alert(const char *msg)
217 fprintf(stderr, "EAL: FATAL: %s\n", msg);
218 RTE_LOG(ERR, EAL, "%s\n", msg);
221 /* Stubs to enable EAL trace point compilation
222 * until eal_common_trace.c can be compiled.
225 RTE_DEFINE_PER_LCORE(volatile int, trace_point_sz);
226 RTE_DEFINE_PER_LCORE(void *, trace_mem);
229 __rte_trace_mem_per_thread_alloc(void)
234 trace_mem_per_thread_free(void)
239 __rte_trace_point_emit_field(size_t sz, const char *field,
248 __rte_trace_point_register(rte_trace_point_t *trace, const char *name,
249 void (*register_fn)(void))
253 RTE_SET_USED(register_fn);
258 rte_eal_cleanup(void)
260 struct internal_config *internal_conf =
261 eal_get_internal_configuration();
263 eal_intr_thread_cancel();
264 eal_mem_virt2iova_cleanup();
265 /* after this point, any DPDK pointers will become dangling */
266 rte_eal_memory_detach();
267 eal_cleanup_config(internal_conf);
271 /* Launch threads, called at application init(). */
273 rte_eal_init(int argc, char **argv)
275 int i, fctret, bscan;
276 const struct rte_config *config = rte_eal_get_configuration();
277 struct internal_config *internal_conf =
278 eal_get_internal_configuration();
280 enum rte_iova_mode iova_mode;
283 eal_log_init(NULL, 0);
285 eal_log_level_parse(argc, argv);
287 if (eal_create_cpu_map() < 0) {
288 rte_eal_init_alert("Cannot discover CPU and NUMA.");
289 /* rte_errno is set */
293 if (rte_eal_cpu_init() < 0) {
294 rte_eal_init_alert("Cannot detect lcores.");
299 fctret = eal_parse_args(argc, argv);
303 if (eal_option_device_parse()) {
308 /* Prevent creation of shared memory files. */
309 if (internal_conf->in_memory == 0) {
310 RTE_LOG(WARNING, EAL, "Multi-process support is requested, "
311 "but not available.\n");
312 internal_conf->in_memory = 1;
313 internal_conf->no_shconf = 1;
316 if (!internal_conf->no_hugetlbfs && (eal_hugepage_info_init() < 0)) {
317 rte_eal_init_alert("Cannot get hugepage information");
322 if (internal_conf->memory == 0 && !internal_conf->force_sockets) {
323 if (internal_conf->no_hugetlbfs)
324 internal_conf->memory = MEMSIZE_IF_NO_HUGE_PAGE;
327 if (rte_eal_intr_init() < 0) {
328 rte_eal_init_alert("Cannot init interrupt-handling thread");
332 if (rte_eal_timer_init() < 0) {
333 rte_eal_init_alert("Cannot init TSC timer");
338 bscan = rte_bus_scan();
340 rte_eal_init_alert("Cannot scan the buses");
345 if (eal_mem_win32api_init() < 0) {
346 rte_eal_init_alert("Cannot access Win32 memory management");
351 has_phys_addr = true;
352 if (eal_mem_virt2iova_init() < 0) {
353 /* Non-fatal error if physical addresses are not required. */
354 RTE_LOG(DEBUG, EAL, "Cannot access virt2phys driver, "
355 "PA will not be available\n");
356 has_phys_addr = false;
359 iova_mode = internal_conf->iova_mode;
360 if (iova_mode == RTE_IOVA_PA && !has_phys_addr) {
361 rte_eal_init_alert("Cannot use IOVA as 'PA' since physical addresses are not available");
365 if (iova_mode == RTE_IOVA_DC) {
366 RTE_LOG(DEBUG, EAL, "Specific IOVA mode is not requested, autodetecting\n");
368 RTE_LOG(DEBUG, EAL, "Selecting IOVA mode according to bus requests\n");
369 iova_mode = rte_bus_get_iommu_class();
370 if (iova_mode == RTE_IOVA_DC)
371 iova_mode = RTE_IOVA_PA;
373 iova_mode = RTE_IOVA_VA;
376 RTE_LOG(DEBUG, EAL, "Selected IOVA mode '%s'\n",
377 iova_mode == RTE_IOVA_PA ? "PA" : "VA");
378 rte_eal_get_configuration()->iova_mode = iova_mode;
380 if (rte_eal_memzone_init() < 0) {
381 rte_eal_init_alert("Cannot init memzone");
386 if (rte_eal_memory_init() < 0) {
387 rte_eal_init_alert("Cannot init memory");
392 if (rte_eal_malloc_heap_init() < 0) {
393 rte_eal_init_alert("Cannot init malloc heap");
398 if (rte_eal_tailqs_init() < 0) {
399 rte_eal_init_alert("Cannot init tail queues for objects");
404 __rte_thread_init(config->main_lcore,
405 &lcore_config[config->main_lcore].cpuset);
407 RTE_LCORE_FOREACH_WORKER(i) {
410 * create communication pipes between main thread
413 if (_pipe(lcore_config[i].pipe_main2worker,
414 sizeof(char), _O_BINARY) < 0)
415 rte_panic("Cannot create pipe\n");
416 if (_pipe(lcore_config[i].pipe_worker2main,
417 sizeof(char), _O_BINARY) < 0)
418 rte_panic("Cannot create pipe\n");
420 lcore_config[i].state = WAIT;
422 /* create a thread for each lcore */
423 if (eal_thread_create(&lcore_config[i].thread_id) != 0)
424 rte_panic("Cannot create thread\n");
427 /* Initialize services so drivers can register services during probe. */
428 ret = rte_service_init();
430 rte_eal_init_alert("rte_service_init() failed");
435 if (rte_bus_probe()) {
436 rte_eal_init_alert("Cannot probe devices");
442 * Launch a dummy function on all worker lcores, so that main lcore
443 * knows they are all ready when this function returns.
445 rte_eal_mp_remote_launch(sync_func, NULL, SKIP_MAIN);
446 rte_eal_mp_wait_lcore();
450 /* Don't use MinGW asprintf() to have identical code with all toolchains. */
452 eal_asprintf(char **buffer, const char *format, ...)
457 va_start(arg, format);
458 size = vsnprintf(NULL, 0, format, arg);
464 *buffer = malloc(size);
468 va_start(arg, format);
469 ret = vsnprintf(*buffer, size, format, arg);
471 if (ret != size - 1) {
479 rte_vfio_container_dma_map(__rte_unused int container_fd,
480 __rte_unused uint64_t vaddr,
481 __rte_unused uint64_t iova,
482 __rte_unused uint64_t len)
488 rte_vfio_container_dma_unmap(__rte_unused int container_fd,
489 __rte_unused uint64_t vaddr,
490 __rte_unused uint64_t iova,
491 __rte_unused uint64_t len)
497 rte_firmware_read(__rte_unused const char *name,
498 __rte_unused void **buf,
499 __rte_unused size_t *bufsz)