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