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>
22 #include "eal_hugepages.h"
23 #include "eal_trace.h"
24 #include "eal_windows.h"
26 #define MEMSIZE_IF_NO_HUGE_PAGE (64ULL * 1024ULL * 1024ULL)
28 /* define fd variable here, because file needs to be kept open for the
29 * duration of the program, as we hold a write lock on it in the primary proc
31 static int mem_cfg_fd = -1;
33 /* internal configuration (per-core) */
34 struct lcore_config lcore_config[RTE_MAX_LCORE];
36 /* Detect if we are a primary or a secondary process */
38 eal_proc_type_detect(void)
40 enum rte_proc_type_t ptype = RTE_PROC_PRIMARY;
41 const char *pathname = eal_runtime_config_path();
42 const struct rte_config *config = rte_eal_get_configuration();
44 /* if we can open the file but not get a write-lock we are a secondary
45 * process. NOTE: if we get a file handle back, we keep that open
46 * and don't close it to prevent a race condition between multiple opens
48 errno_t err = _sopen_s(&mem_cfg_fd, pathname,
49 _O_RDWR, _SH_DENYNO, _S_IREAD | _S_IWRITE);
51 OVERLAPPED soverlapped = { 0 };
52 soverlapped.Offset = sizeof(*config->mem_config);
53 soverlapped.OffsetHigh = 0;
55 HANDLE hwinfilehandle = (HANDLE)_get_osfhandle(mem_cfg_fd);
57 if (!LockFileEx(hwinfilehandle,
58 LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY, 0,
59 sizeof(*config->mem_config), 0, &soverlapped))
60 ptype = RTE_PROC_SECONDARY;
63 RTE_LOG(INFO, EAL, "Auto-detected process type: %s\n",
64 ptype == RTE_PROC_PRIMARY ? "PRIMARY" : "SECONDARY");
71 eal_usage(const char *prgname)
73 rte_usage_hook_t hook = eal_get_application_usage_hook();
75 printf("\nUsage: %s ", prgname);
77 /* Allow the application to print its usage message too
81 printf("===== Application Usage =====\n\n");
86 /* Parse the arguments for --log-level only */
88 eal_log_level_parse(int argc, char **argv)
93 struct internal_config *internal_conf =
94 eal_get_internal_configuration();
98 eal_reset_internal_config(internal_conf);
100 while ((opt = getopt_long(argc, argvopt, eal_short_options,
101 eal_long_options, &option_index)) != EOF) {
105 /* getopt is not happy, stop right now */
109 ret = (opt == OPT_LOG_LEVEL_NUM) ?
110 eal_parse_common_option(opt, optarg,
113 /* common parser is not happy */
118 optind = 0; /* reset getopt lib */
121 /* Parse the argument given in the command line of the application */
123 eal_parse_args(int argc, char **argv)
128 char *prgname = argv[0];
129 struct internal_config *internal_conf =
130 eal_get_internal_configuration();
134 while ((opt = getopt_long(argc, argvopt, eal_short_options,
135 eal_long_options, &option_index)) != EOF) {
139 /* getopt is not happy, stop right now */
145 ret = eal_parse_common_option(opt, optarg, internal_conf);
146 /* common parser is not happy */
151 /* common parser handled this option */
160 if (opt < OPT_LONG_MIN_NUM && isprint(opt)) {
161 RTE_LOG(ERR, EAL, "Option %c is not supported "
162 "on Windows\n", opt);
163 } else if (opt >= OPT_LONG_MIN_NUM &&
164 opt < OPT_LONG_MAX_NUM) {
165 RTE_LOG(ERR, EAL, "Option %s is not supported "
167 eal_long_options[option_index].name);
169 RTE_LOG(ERR, EAL, "Option %d is not supported "
170 "on Windows\n", opt);
177 if (eal_adjust_config(internal_conf) != 0)
181 if (eal_check_common_options(internal_conf) != 0) {
187 argv[optind - 1] = prgname;
189 optind = 0; /* reset getopt lib */
194 sync_func(void *arg __rte_unused)
200 rte_eal_init_alert(const char *msg)
202 fprintf(stderr, "EAL: FATAL: %s\n", msg);
203 RTE_LOG(ERR, EAL, "%s\n", msg);
206 /* Stubs to enable EAL trace point compilation
207 * until eal_common_trace.c can be compiled.
210 RTE_DEFINE_PER_LCORE(volatile int, trace_point_sz);
211 RTE_DEFINE_PER_LCORE(void *, trace_mem);
214 __rte_trace_mem_per_thread_alloc(void)
219 trace_mem_per_thread_free(void)
224 __rte_trace_point_emit_field(size_t sz, const char *field,
233 __rte_trace_point_register(rte_trace_point_t *trace, const char *name,
234 void (*register_fn)(void))
238 RTE_SET_USED(register_fn);
243 rte_eal_cleanup(void)
245 struct internal_config *internal_conf =
246 eal_get_internal_configuration();
248 eal_cleanup_config(internal_conf);
252 /* Launch threads, called at application init(). */
254 rte_eal_init(int argc, char **argv)
256 int i, fctret, bscan;
257 const struct rte_config *config = rte_eal_get_configuration();
258 struct internal_config *internal_conf =
259 eal_get_internal_configuration();
261 rte_eal_log_init(NULL, 0);
263 eal_log_level_parse(argc, argv);
265 if (eal_create_cpu_map() < 0) {
266 rte_eal_init_alert("Cannot discover CPU and NUMA.");
267 /* rte_errno is set */
271 if (rte_eal_cpu_init() < 0) {
272 rte_eal_init_alert("Cannot detect lcores.");
277 fctret = eal_parse_args(argc, argv);
281 /* Prevent creation of shared memory files. */
282 if (internal_conf->in_memory == 0) {
283 RTE_LOG(WARNING, EAL, "Multi-process support is requested, "
284 "but not available.\n");
285 internal_conf->in_memory = 1;
288 if (!internal_conf->no_hugetlbfs && (eal_hugepage_info_init() < 0)) {
289 rte_eal_init_alert("Cannot get hugepage information");
294 if (internal_conf->memory == 0 && !internal_conf->force_sockets) {
295 if (internal_conf->no_hugetlbfs)
296 internal_conf->memory = MEMSIZE_IF_NO_HUGE_PAGE;
299 if (eal_mem_win32api_init() < 0) {
300 rte_eal_init_alert("Cannot access Win32 memory management");
305 if (eal_mem_virt2iova_init() < 0) {
306 /* Non-fatal error if physical addresses are not required. */
307 RTE_LOG(WARNING, EAL, "Cannot access virt2phys driver, "
308 "PA will not be available\n");
311 if (rte_eal_memzone_init() < 0) {
312 rte_eal_init_alert("Cannot init memzone");
317 if (rte_eal_memory_init() < 0) {
318 rte_eal_init_alert("Cannot init memory");
323 if (rte_eal_malloc_heap_init() < 0) {
324 rte_eal_init_alert("Cannot init malloc heap");
329 if (rte_eal_tailqs_init() < 0) {
330 rte_eal_init_alert("Cannot init tail queues for objects");
335 if (rte_eal_timer_init() < 0) {
336 rte_eal_init_alert("Cannot init TSC timer");
341 __rte_thread_init(config->master_lcore,
342 &lcore_config[config->master_lcore].cpuset);
344 bscan = rte_bus_scan();
346 rte_eal_init_alert("Cannot init PCI");
351 RTE_LCORE_FOREACH_SLAVE(i) {
354 * create communication pipes between master thread
357 if (_pipe(lcore_config[i].pipe_master2slave,
358 sizeof(char), _O_BINARY) < 0)
359 rte_panic("Cannot create pipe\n");
360 if (_pipe(lcore_config[i].pipe_slave2master,
361 sizeof(char), _O_BINARY) < 0)
362 rte_panic("Cannot create pipe\n");
364 lcore_config[i].state = WAIT;
366 /* create a thread for each lcore */
367 if (eal_thread_create(&lcore_config[i].thread_id) != 0)
368 rte_panic("Cannot create thread\n");
372 * Launch a dummy function on all slave lcores, so that master lcore
373 * knows they are all ready when this function returns.
375 rte_eal_mp_remote_launch(sync_func, NULL, SKIP_MASTER);
376 rte_eal_mp_wait_lcore();
381 rte_vfio_container_dma_map(__rte_unused int container_fd,
382 __rte_unused uint64_t vaddr,
383 __rte_unused uint64_t iova,
384 __rte_unused uint64_t len)
390 rte_vfio_container_dma_unmap(__rte_unused int container_fd,
391 __rte_unused uint64_t vaddr,
392 __rte_unused uint64_t iova,
393 __rte_unused uint64_t len)