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