1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2019 Intel Corporation
10 #include <eal_memcfg.h>
11 #include <rte_errno.h>
12 #include <rte_lcore.h>
13 #include <eal_thread.h>
14 #include <eal_internal_cfg.h>
15 #include <eal_filesystem.h>
16 #include <eal_options.h>
17 #include <eal_private.h>
19 /* define fd variable here, because file needs to be kept open for the
20 * duration of the program, as we hold a write lock on it in the primary proc
22 static int mem_cfg_fd = -1;
24 /* early configuration structure, when memory config is not mmapped */
25 static struct rte_mem_config early_mem_config;
27 /* Address of global and public configuration */
28 static struct rte_config rte_config = {
29 .mem_config = &early_mem_config,
32 /* internal configuration (per-core) */
33 struct lcore_config lcore_config[RTE_MAX_LCORE];
35 /* internal configuration */
36 struct internal_config internal_config;
38 /* platform-specific runtime dir */
39 static char runtime_dir[PATH_MAX];
42 rte_eal_get_runtime_dir(void)
47 /* Return a pointer to the configuration structure */
49 rte_eal_get_configuration(void)
54 /* Detect if we are a primary or a secondary process */
56 eal_proc_type_detect(void)
58 enum rte_proc_type_t ptype = RTE_PROC_PRIMARY;
59 const char *pathname = eal_runtime_config_path();
61 /* if we can open the file but not get a write-lock we are a secondary
62 * process. NOTE: if we get a file handle back, we keep that open
63 * and don't close it to prevent a race condition between multiple opens
65 errno_t err = _sopen_s(&mem_cfg_fd, pathname,
66 _O_RDWR, _SH_DENYNO, _S_IREAD | _S_IWRITE);
68 OVERLAPPED soverlapped = { 0 };
69 soverlapped.Offset = sizeof(*rte_config.mem_config);
70 soverlapped.OffsetHigh = 0;
72 HANDLE hwinfilehandle = (HANDLE)_get_osfhandle(mem_cfg_fd);
74 if (!LockFileEx(hwinfilehandle,
75 LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY, 0,
76 sizeof(*rte_config.mem_config), 0, &soverlapped))
77 ptype = RTE_PROC_SECONDARY;
80 RTE_LOG(INFO, EAL, "Auto-detected process type: %s\n",
81 ptype == RTE_PROC_PRIMARY ? "PRIMARY" : "SECONDARY");
87 sync_func(void *arg __rte_unused)
93 rte_eal_init_alert(const char *msg)
95 fprintf(stderr, "EAL: FATAL: %s\n", msg);
96 RTE_LOG(ERR, EAL, "%s\n", msg);
99 /* Launch threads, called at application init(). */
101 rte_eal_init(int argc __rte_unused, char **argv __rte_unused)
105 /* create a map of all processors in the system */
106 eal_create_cpu_map();
108 if (rte_eal_cpu_init() < 0) {
109 rte_eal_init_alert("Cannot detect lcores.");
114 eal_thread_init_master(rte_config.master_lcore);
116 RTE_LCORE_FOREACH_SLAVE(i) {
119 * create communication pipes between master thread
122 if (_pipe(lcore_config[i].pipe_master2slave,
123 sizeof(char), _O_BINARY) < 0)
124 rte_panic("Cannot create pipe\n");
125 if (_pipe(lcore_config[i].pipe_slave2master,
126 sizeof(char), _O_BINARY) < 0)
127 rte_panic("Cannot create pipe\n");
129 lcore_config[i].state = WAIT;
131 /* create a thread for each lcore */
132 if (eal_thread_create(&lcore_config[i].thread_id) != 0)
133 rte_panic("Cannot create thread\n");
137 * Launch a dummy function on all slave lcores, so that master lcore
138 * knows they are all ready when this function returns.
140 rte_eal_mp_remote_launch(sync_func, NULL, SKIP_MASTER);
141 rte_eal_mp_wait_lcore();