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