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