eal: move OS common config objects
[dpdk.git] / lib / librte_eal / windows / eal.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2019 Intel Corporation
3  */
4
5 #include <fcntl.h>
6 #include <io.h>
7 #include <share.h>
8 #include <sys/stat.h>
9
10 #include <rte_debug.h>
11 #include <rte_eal.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_trace_point.h>
21
22 #include "eal_hugepages.h"
23 #include "eal_windows.h"
24
25 #define MEMSIZE_IF_NO_HUGE_PAGE (64ULL * 1024ULL * 1024ULL)
26
27  /* Allow the application to print its usage message too if set */
28 static rte_usage_hook_t rte_application_usage_hook;
29
30 /* define fd variable here, because file needs to be kept open for the
31  * duration of the program, as we hold a write lock on it in the primary proc
32  */
33 static int mem_cfg_fd = -1;
34
35 /* internal configuration (per-core) */
36 struct lcore_config lcore_config[RTE_MAX_LCORE];
37
38 /* Detect if we are a primary or a secondary process */
39 enum rte_proc_type_t
40 eal_proc_type_detect(void)
41 {
42         enum rte_proc_type_t ptype = RTE_PROC_PRIMARY;
43         const char *pathname = eal_runtime_config_path();
44         const struct rte_config *config = rte_eal_get_configuration();
45
46         /* if we can open the file but not get a write-lock we are a secondary
47          * process. NOTE: if we get a file handle back, we keep that open
48          * and don't close it to prevent a race condition between multiple opens
49          */
50         errno_t err = _sopen_s(&mem_cfg_fd, pathname,
51                 _O_RDWR, _SH_DENYNO, _S_IREAD | _S_IWRITE);
52         if (err == 0) {
53                 OVERLAPPED soverlapped = { 0 };
54                 soverlapped.Offset = sizeof(*config->mem_config);
55                 soverlapped.OffsetHigh = 0;
56
57                 HANDLE hwinfilehandle = (HANDLE)_get_osfhandle(mem_cfg_fd);
58
59                 if (!LockFileEx(hwinfilehandle,
60                         LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY, 0,
61                         sizeof(*config->mem_config), 0, &soverlapped))
62                         ptype = RTE_PROC_SECONDARY;
63         }
64
65         RTE_LOG(INFO, EAL, "Auto-detected process type: %s\n",
66                 ptype == RTE_PROC_PRIMARY ? "PRIMARY" : "SECONDARY");
67
68         return ptype;
69 }
70
71 /* display usage */
72 static void
73 eal_usage(const char *prgname)
74 {
75         printf("\nUsage: %s ", prgname);
76         eal_common_usage();
77         /* Allow the application to print its usage message too
78          * if hook is set
79          */
80         if (rte_application_usage_hook) {
81                 printf("===== Application Usage =====\n\n");
82                 rte_application_usage_hook(prgname);
83         }
84 }
85
86 /* Parse the arguments for --log-level only */
87 static void
88 eal_log_level_parse(int argc, char **argv)
89 {
90         int opt;
91         char **argvopt;
92         int option_index;
93         struct internal_config *internal_conf =
94                 eal_get_internal_configuration();
95
96         argvopt = argv;
97
98         eal_reset_internal_config(internal_conf);
99
100         while ((opt = getopt_long(argc, argvopt, eal_short_options,
101                 eal_long_options, &option_index)) != EOF) {
102
103                 int ret;
104
105                 /* getopt is not happy, stop right now */
106                 if (opt == '?')
107                         break;
108
109                 ret = (opt == OPT_LOG_LEVEL_NUM) ?
110                         eal_parse_common_option(opt, optarg,
111                                 internal_conf) : 0;
112
113                 /* common parser is not happy */
114                 if (ret < 0)
115                         break;
116         }
117
118         optind = 0; /* reset getopt lib */
119 }
120
121 /* Parse the argument given in the command line of the application */
122 static int
123 eal_parse_args(int argc, char **argv)
124 {
125         int opt, ret;
126         char **argvopt;
127         int option_index;
128         char *prgname = argv[0];
129         struct internal_config *internal_conf =
130                 eal_get_internal_configuration();
131
132         argvopt = argv;
133
134         while ((opt = getopt_long(argc, argvopt, eal_short_options,
135                 eal_long_options, &option_index)) != EOF) {
136
137                 int ret;
138
139                 /* getopt is not happy, stop right now */
140                 if (opt == '?') {
141                         eal_usage(prgname);
142                         return -1;
143                 }
144
145                 ret = eal_parse_common_option(opt, optarg, internal_conf);
146                 /* common parser is not happy */
147                 if (ret < 0) {
148                         eal_usage(prgname);
149                         return -1;
150                 }
151                 /* common parser handled this option */
152                 if (ret == 0)
153                         continue;
154
155                 switch (opt) {
156                 case 'h':
157                         eal_usage(prgname);
158                         exit(EXIT_SUCCESS);
159                 default:
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 "
166                                         "on Windows\n",
167                                         eal_long_options[option_index].name);
168                         } else {
169                                 RTE_LOG(ERR, EAL, "Option %d is not supported "
170                                         "on Windows\n", opt);
171                         }
172                         eal_usage(prgname);
173                         return -1;
174                 }
175         }
176
177         if (eal_adjust_config(internal_conf) != 0)
178                 return -1;
179
180         /* sanity checks */
181         if (eal_check_common_options(internal_conf) != 0) {
182                 eal_usage(prgname);
183                 return -1;
184         }
185
186         if (optind >= 0)
187                 argv[optind - 1] = prgname;
188         ret = optind - 1;
189         optind = 0; /* reset getopt lib */
190         return ret;
191 }
192
193 static int
194 sync_func(void *arg __rte_unused)
195 {
196         return 0;
197 }
198
199 static void
200 rte_eal_init_alert(const char *msg)
201 {
202         fprintf(stderr, "EAL: FATAL: %s\n", msg);
203         RTE_LOG(ERR, EAL, "%s\n", msg);
204 }
205
206 /* Stubs to enable EAL trace point compilation
207  * until eal_common_trace.c can be compiled.
208  */
209
210 RTE_DEFINE_PER_LCORE(volatile int, trace_point_sz);
211 RTE_DEFINE_PER_LCORE(void *, trace_mem);
212
213 void
214 __rte_trace_mem_per_thread_alloc(void)
215 {
216 }
217
218 void
219 __rte_trace_point_emit_field(size_t sz, const char *field,
220         const char *type)
221 {
222         RTE_SET_USED(sz);
223         RTE_SET_USED(field);
224         RTE_SET_USED(type);
225 }
226
227 int
228 __rte_trace_point_register(rte_trace_point_t *trace, const char *name,
229         void (*register_fn)(void))
230 {
231         RTE_SET_USED(trace);
232         RTE_SET_USED(name);
233         RTE_SET_USED(register_fn);
234         return -ENOTSUP;
235 }
236
237 int
238 rte_eal_cleanup(void)
239 {
240         struct internal_config *internal_conf =
241                 eal_get_internal_configuration();
242
243         eal_cleanup_config(internal_conf);
244         return 0;
245 }
246
247 /* Launch threads, called at application init(). */
248 int
249 rte_eal_init(int argc, char **argv)
250 {
251         int i, fctret;
252         const struct rte_config *config = rte_eal_get_configuration();
253         struct internal_config *internal_conf =
254                 eal_get_internal_configuration();
255
256         rte_eal_log_init(NULL, 0);
257
258         eal_log_level_parse(argc, argv);
259
260         if (eal_create_cpu_map() < 0) {
261                 rte_eal_init_alert("Cannot discover CPU and NUMA.");
262                 /* rte_errno is set */
263                 return -1;
264         }
265
266         if (rte_eal_cpu_init() < 0) {
267                 rte_eal_init_alert("Cannot detect lcores.");
268                 rte_errno = ENOTSUP;
269                 return -1;
270         }
271
272         fctret = eal_parse_args(argc, argv);
273         if (fctret < 0)
274                 exit(1);
275
276         /* Prevent creation of shared memory files. */
277         if (internal_conf->in_memory == 0) {
278                 RTE_LOG(WARNING, EAL, "Multi-process support is requested, "
279                         "but not available.\n");
280                 internal_conf->in_memory = 1;
281         }
282
283         if (!internal_conf->no_hugetlbfs && (eal_hugepage_info_init() < 0)) {
284                 rte_eal_init_alert("Cannot get hugepage information");
285                 rte_errno = EACCES;
286                 return -1;
287         }
288
289         if (internal_conf->memory == 0 && !internal_conf->force_sockets) {
290                 if (internal_conf->no_hugetlbfs)
291                         internal_conf->memory = MEMSIZE_IF_NO_HUGE_PAGE;
292         }
293
294         if (eal_mem_win32api_init() < 0) {
295                 rte_eal_init_alert("Cannot access Win32 memory management");
296                 rte_errno = ENOTSUP;
297                 return -1;
298         }
299
300         if (eal_mem_virt2iova_init() < 0) {
301                 /* Non-fatal error if physical addresses are not required. */
302                 RTE_LOG(WARNING, EAL, "Cannot access virt2phys driver, "
303                         "PA will not be available\n");
304         }
305
306         if (rte_eal_memzone_init() < 0) {
307                 rte_eal_init_alert("Cannot init memzone");
308                 rte_errno = ENODEV;
309                 return -1;
310         }
311
312         if (rte_eal_memory_init() < 0) {
313                 rte_eal_init_alert("Cannot init memory");
314                 rte_errno = ENOMEM;
315                 return -1;
316         }
317
318         if (rte_eal_malloc_heap_init() < 0) {
319                 rte_eal_init_alert("Cannot init malloc heap");
320                 rte_errno = ENODEV;
321                 return -1;
322         }
323
324         if (rte_eal_tailqs_init() < 0) {
325                 rte_eal_init_alert("Cannot init tail queues for objects");
326                 rte_errno = EFAULT;
327                 return -1;
328         }
329
330         if (rte_eal_timer_init() < 0) {
331                 rte_eal_init_alert("Cannot init TSC timer");
332                 rte_errno = EFAULT;
333                 return -1;
334         }
335
336         eal_thread_init_master(config->master_lcore);
337
338         RTE_LCORE_FOREACH_SLAVE(i) {
339
340                 /*
341                  * create communication pipes between master thread
342                  * and children
343                  */
344                 if (_pipe(lcore_config[i].pipe_master2slave,
345                         sizeof(char), _O_BINARY) < 0)
346                         rte_panic("Cannot create pipe\n");
347                 if (_pipe(lcore_config[i].pipe_slave2master,
348                         sizeof(char), _O_BINARY) < 0)
349                         rte_panic("Cannot create pipe\n");
350
351                 lcore_config[i].state = WAIT;
352
353                 /* create a thread for each lcore */
354                 if (eal_thread_create(&lcore_config[i].thread_id) != 0)
355                         rte_panic("Cannot create thread\n");
356         }
357
358         /*
359          * Launch a dummy function on all slave lcores, so that master lcore
360          * knows they are all ready when this function returns.
361          */
362         rte_eal_mp_remote_launch(sync_func, NULL, SKIP_MASTER);
363         rte_eal_mp_wait_lcore();
364         return fctret;
365 }