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_service_component.h>
23 #include "eal_hugepages.h"
24 #include "eal_trace.h"
25 #include "eal_windows.h"
27 #define MEMSIZE_IF_NO_HUGE_PAGE (64ULL * 1024ULL * 1024ULL)
29 /* define fd variable here, because file needs to be kept open for the
30 * duration of the program, as we hold a write lock on it in the primary proc
32 static int mem_cfg_fd = -1;
34 /* internal configuration (per-core) */
35 struct lcore_config lcore_config[RTE_MAX_LCORE];
37 /* Detect if we are a primary or a secondary process */
39 eal_proc_type_detect(void)
41 enum rte_proc_type_t ptype = RTE_PROC_PRIMARY;
42 const char *pathname = eal_runtime_config_path();
43 const struct rte_config *config = rte_eal_get_configuration();
45 /* if we can open the file but not get a write-lock we are a secondary
46 * process. NOTE: if we get a file handle back, we keep that open
47 * and don't close it to prevent a race condition between multiple opens
49 errno_t err = _sopen_s(&mem_cfg_fd, pathname,
50 _O_RDWR, _SH_DENYNO, _S_IREAD | _S_IWRITE);
52 OVERLAPPED soverlapped = { 0 };
53 soverlapped.Offset = sizeof(*config->mem_config);
54 soverlapped.OffsetHigh = 0;
56 HANDLE hwinfilehandle = (HANDLE)_get_osfhandle(mem_cfg_fd);
58 if (!LockFileEx(hwinfilehandle,
59 LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY, 0,
60 sizeof(*config->mem_config), 0, &soverlapped))
61 ptype = RTE_PROC_SECONDARY;
64 RTE_LOG(INFO, EAL, "Auto-detected process type: %s\n",
65 ptype == RTE_PROC_PRIMARY ? "PRIMARY" : "SECONDARY");
78 eal_usage(const char *prgname)
80 rte_usage_hook_t hook = eal_get_application_usage_hook();
82 printf("\nUsage: %s ", prgname);
84 /* Allow the application to print its usage message too
88 printf("===== Application Usage =====\n\n");
93 /* Parse the arguments for --log-level only */
95 eal_log_level_parse(int argc, char **argv)
100 struct internal_config *internal_conf =
101 eal_get_internal_configuration();
105 eal_reset_internal_config(internal_conf);
107 while ((opt = getopt_long(argc, argvopt, eal_short_options,
108 eal_long_options, &option_index)) != EOF) {
112 /* getopt is not happy, stop right now */
116 ret = (opt == OPT_LOG_LEVEL_NUM) ?
117 eal_parse_common_option(opt, optarg,
120 /* common parser is not happy */
125 optind = 0; /* reset getopt lib */
128 /* Parse the argument given in the command line of the application */
130 eal_parse_args(int argc, char **argv)
135 char *prgname = argv[0];
136 struct internal_config *internal_conf =
137 eal_get_internal_configuration();
141 while ((opt = getopt_long(argc, argvopt, eal_short_options,
142 eal_long_options, &option_index)) != EOF) {
146 /* getopt is not happy, stop right now */
152 ret = eal_parse_common_option(opt, optarg, internal_conf);
153 /* common parser is not happy */
158 /* common parser handled this option */
167 if (opt < OPT_LONG_MIN_NUM && isprint(opt)) {
168 RTE_LOG(ERR, EAL, "Option %c is not supported "
169 "on Windows\n", opt);
170 } else if (opt >= OPT_LONG_MIN_NUM &&
171 opt < OPT_LONG_MAX_NUM) {
172 RTE_LOG(ERR, EAL, "Option %s is not supported "
174 eal_long_options[option_index].name);
176 RTE_LOG(ERR, EAL, "Option %d is not supported "
177 "on Windows\n", opt);
184 if (eal_adjust_config(internal_conf) != 0)
188 if (eal_check_common_options(internal_conf) != 0) {
194 argv[optind - 1] = prgname;
196 optind = 0; /* reset getopt lib */
201 sync_func(void *arg __rte_unused)
207 rte_eal_init_alert(const char *msg)
209 fprintf(stderr, "EAL: FATAL: %s\n", msg);
210 RTE_LOG(ERR, EAL, "%s\n", msg);
213 /* Stubs to enable EAL trace point compilation
214 * until eal_common_trace.c can be compiled.
217 RTE_DEFINE_PER_LCORE(volatile int, trace_point_sz);
218 RTE_DEFINE_PER_LCORE(void *, trace_mem);
221 __rte_trace_mem_per_thread_alloc(void)
226 trace_mem_per_thread_free(void)
231 __rte_trace_point_emit_field(size_t sz, const char *field,
240 __rte_trace_point_register(rte_trace_point_t *trace, const char *name,
241 void (*register_fn)(void))
245 RTE_SET_USED(register_fn);
250 rte_eal_cleanup(void)
252 struct internal_config *internal_conf =
253 eal_get_internal_configuration();
255 eal_cleanup_config(internal_conf);
259 /* Launch threads, called at application init(). */
261 rte_eal_init(int argc, char **argv)
263 int i, fctret, bscan;
264 const struct rte_config *config = rte_eal_get_configuration();
265 struct internal_config *internal_conf =
266 eal_get_internal_configuration();
268 rte_eal_log_init(NULL, 0);
270 eal_log_level_parse(argc, argv);
272 if (eal_create_cpu_map() < 0) {
273 rte_eal_init_alert("Cannot discover CPU and NUMA.");
274 /* rte_errno is set */
278 if (rte_eal_cpu_init() < 0) {
279 rte_eal_init_alert("Cannot detect lcores.");
284 fctret = eal_parse_args(argc, argv);
288 if (eal_option_device_parse()) {
293 /* Prevent creation of shared memory files. */
294 if (internal_conf->in_memory == 0) {
295 RTE_LOG(WARNING, EAL, "Multi-process support is requested, "
296 "but not available.\n");
297 internal_conf->in_memory = 1;
298 internal_conf->no_shconf = 1;
301 if (!internal_conf->no_hugetlbfs && (eal_hugepage_info_init() < 0)) {
302 rte_eal_init_alert("Cannot get hugepage information");
307 if (internal_conf->memory == 0 && !internal_conf->force_sockets) {
308 if (internal_conf->no_hugetlbfs)
309 internal_conf->memory = MEMSIZE_IF_NO_HUGE_PAGE;
312 if (eal_mem_win32api_init() < 0) {
313 rte_eal_init_alert("Cannot access Win32 memory management");
318 if (eal_mem_virt2iova_init() < 0) {
319 /* Non-fatal error if physical addresses are not required. */
320 RTE_LOG(WARNING, EAL, "Cannot access virt2phys driver, "
321 "PA will not be available\n");
324 if (rte_eal_memzone_init() < 0) {
325 rte_eal_init_alert("Cannot init memzone");
330 if (rte_eal_memory_init() < 0) {
331 rte_eal_init_alert("Cannot init memory");
336 if (rte_eal_malloc_heap_init() < 0) {
337 rte_eal_init_alert("Cannot init malloc heap");
342 if (rte_eal_tailqs_init() < 0) {
343 rte_eal_init_alert("Cannot init tail queues for objects");
348 if (rte_eal_intr_init() < 0) {
349 rte_eal_init_alert("Cannot init interrupt-handling thread");
353 if (rte_eal_timer_init() < 0) {
354 rte_eal_init_alert("Cannot init TSC timer");
359 __rte_thread_init(config->main_lcore,
360 &lcore_config[config->main_lcore].cpuset);
362 bscan = rte_bus_scan();
364 rte_eal_init_alert("Cannot init PCI");
369 RTE_LCORE_FOREACH_WORKER(i) {
372 * create communication pipes between main thread
375 if (_pipe(lcore_config[i].pipe_main2worker,
376 sizeof(char), _O_BINARY) < 0)
377 rte_panic("Cannot create pipe\n");
378 if (_pipe(lcore_config[i].pipe_worker2main,
379 sizeof(char), _O_BINARY) < 0)
380 rte_panic("Cannot create pipe\n");
382 lcore_config[i].state = WAIT;
384 /* create a thread for each lcore */
385 if (eal_thread_create(&lcore_config[i].thread_id) != 0)
386 rte_panic("Cannot create thread\n");
389 /* Initialize services so drivers can register services during probe. */
390 if (rte_service_init()) {
391 rte_eal_init_alert("rte_service_init() failed");
396 if (rte_bus_probe()) {
397 rte_eal_init_alert("Cannot probe devices");
403 * Launch a dummy function on all worker lcores, so that main lcore
404 * knows they are all ready when this function returns.
406 rte_eal_mp_remote_launch(sync_func, NULL, SKIP_MAIN);
407 rte_eal_mp_wait_lcore();
412 rte_vfio_container_dma_map(__rte_unused int container_fd,
413 __rte_unused uint64_t vaddr,
414 __rte_unused uint64_t iova,
415 __rte_unused uint64_t len)
421 rte_vfio_container_dma_unmap(__rte_unused int container_fd,
422 __rte_unused uint64_t vaddr,
423 __rte_unused uint64_t iova,
424 __rte_unused uint64_t len)