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 /* Allow the application to print its usage message too if set */
20 static rte_usage_hook_t rte_application_usage_hook;
22 /* define fd variable here, because file needs to be kept open for the
23 * duration of the program, as we hold a write lock on it in the primary proc
25 static int mem_cfg_fd = -1;
27 /* early configuration structure, when memory config is not mmapped */
28 static struct rte_mem_config early_mem_config;
30 /* Address of global and public configuration */
31 static struct rte_config rte_config = {
32 .mem_config = &early_mem_config,
35 /* internal configuration (per-core) */
36 struct lcore_config lcore_config[RTE_MAX_LCORE];
38 /* internal configuration */
39 struct internal_config internal_config;
41 /* platform-specific runtime dir */
42 static char runtime_dir[PATH_MAX];
45 rte_eal_get_runtime_dir(void)
50 /* Return a pointer to the configuration structure */
52 rte_eal_get_configuration(void)
57 /* Detect if we are a primary or a secondary process */
59 eal_proc_type_detect(void)
61 enum rte_proc_type_t ptype = RTE_PROC_PRIMARY;
62 const char *pathname = eal_runtime_config_path();
64 /* if we can open the file but not get a write-lock we are a secondary
65 * process. NOTE: if we get a file handle back, we keep that open
66 * and don't close it to prevent a race condition between multiple opens
68 errno_t err = _sopen_s(&mem_cfg_fd, pathname,
69 _O_RDWR, _SH_DENYNO, _S_IREAD | _S_IWRITE);
71 OVERLAPPED soverlapped = { 0 };
72 soverlapped.Offset = sizeof(*rte_config.mem_config);
73 soverlapped.OffsetHigh = 0;
75 HANDLE hwinfilehandle = (HANDLE)_get_osfhandle(mem_cfg_fd);
77 if (!LockFileEx(hwinfilehandle,
78 LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY, 0,
79 sizeof(*rte_config.mem_config), 0, &soverlapped))
80 ptype = RTE_PROC_SECONDARY;
83 RTE_LOG(INFO, EAL, "Auto-detected process type: %s\n",
84 ptype == RTE_PROC_PRIMARY ? "PRIMARY" : "SECONDARY");
91 eal_usage(const char *prgname)
93 printf("\nUsage: %s ", prgname);
95 /* Allow the application to print its usage message too
98 if (rte_application_usage_hook) {
99 printf("===== Application Usage =====\n\n");
100 rte_application_usage_hook(prgname);
104 /* Parse the arguments for --log-level only */
106 eal_log_level_parse(int argc, char **argv)
114 eal_reset_internal_config(&internal_config);
116 while ((opt = getopt_long(argc, argvopt, eal_short_options,
117 eal_long_options, &option_index)) != EOF) {
121 /* getopt is not happy, stop right now */
125 ret = (opt == OPT_LOG_LEVEL_NUM) ?
126 eal_parse_common_option(opt, optarg,
127 &internal_config) : 0;
129 /* common parser is not happy */
134 optind = 0; /* reset getopt lib */
137 /* Parse the argument given in the command line of the application */
138 __attribute__((optnone)) static int
139 eal_parse_args(int argc, char **argv)
144 char *prgname = argv[0];
148 while ((opt = getopt_long(argc, argvopt, eal_short_options,
149 eal_long_options, &option_index)) != EOF) {
153 /* getopt is not happy, stop right now */
159 ret = eal_parse_common_option(opt, optarg, &internal_config);
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_config) != 0)
195 if (eal_check_common_options(&internal_config) != 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 /* Launch threads, called at application init(). */
222 rte_eal_init(int argc, char **argv)
226 eal_log_level_parse(argc, argv);
228 /* create a map of all processors in the system */
229 eal_create_cpu_map();
231 if (rte_eal_cpu_init() < 0) {
232 rte_eal_init_alert("Cannot detect lcores.");
237 fctret = eal_parse_args(argc, argv);
241 eal_thread_init_master(rte_config.master_lcore);
243 RTE_LCORE_FOREACH_SLAVE(i) {
246 * create communication pipes between master thread
249 if (_pipe(lcore_config[i].pipe_master2slave,
250 sizeof(char), _O_BINARY) < 0)
251 rte_panic("Cannot create pipe\n");
252 if (_pipe(lcore_config[i].pipe_slave2master,
253 sizeof(char), _O_BINARY) < 0)
254 rte_panic("Cannot create pipe\n");
256 lcore_config[i].state = WAIT;
258 /* create a thread for each lcore */
259 if (eal_thread_create(&lcore_config[i].thread_id) != 0)
260 rte_panic("Cannot create thread\n");
264 * Launch a dummy function on all slave lcores, so that master lcore
265 * knows they are all ready when this function returns.
267 rte_eal_mp_remote_launch(sync_func, NULL, SKIP_MASTER);
268 rte_eal_mp_wait_lcore();