xref: /dpdk/lib/eal/linux/eal.c (revision 90b13ab8d4f7dc2564dc9ae30f7b45e47c527e3c)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2018 Intel Corporation.
3  * Copyright(c) 2012-2014 6WIND S.A.
4  */
5 
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <stdint.h>
9 #include <string.h>
10 #include <stdarg.h>
11 #include <unistd.h>
12 #include <pthread.h>
13 #include <syslog.h>
14 #include <getopt.h>
15 #include <sys/file.h>
16 #include <dirent.h>
17 #include <fcntl.h>
18 #include <fnmatch.h>
19 #include <stddef.h>
20 #include <errno.h>
21 #include <limits.h>
22 #include <sys/mman.h>
23 #include <sys/queue.h>
24 #include <sys/stat.h>
25 #if defined(RTE_ARCH_X86)
26 #include <sys/io.h>
27 #endif
28 #include <linux/version.h>
29 
30 #include <rte_compat.h>
31 #include <rte_common.h>
32 #include <rte_debug.h>
33 #include <rte_memory.h>
34 #include <rte_launch.h>
35 #include <rte_eal.h>
36 #include <rte_errno.h>
37 #include <rte_per_lcore.h>
38 #include <rte_lcore.h>
39 #include <rte_service_component.h>
40 #include <rte_log.h>
41 #include <rte_random.h>
42 #include <rte_cycles.h>
43 #include <rte_string_fns.h>
44 #include <rte_cpuflags.h>
45 #include <rte_interrupts.h>
46 #include <rte_bus.h>
47 #include <rte_dev.h>
48 #include <rte_devargs.h>
49 #include <rte_version.h>
50 #include <malloc_heap.h>
51 #include <rte_vfio.h>
52 
53 #include <telemetry_internal.h>
54 #include "eal_private.h"
55 #include "eal_thread.h"
56 #include "eal_internal_cfg.h"
57 #include "eal_filesystem.h"
58 #include "eal_hugepages.h"
59 #include "eal_memcfg.h"
60 #include "eal_trace.h"
61 #include "eal_log.h"
62 #include "eal_options.h"
63 #include "eal_vfio.h"
64 #include "hotplug_mp.h"
65 
66 #define MEMSIZE_IF_NO_HUGE_PAGE (64ULL * 1024ULL * 1024ULL)
67 
68 #define SOCKET_MEM_STRLEN (RTE_MAX_NUMA_NODES * 10)
69 
70 #define KERNEL_IOMMU_GROUPS_PATH "/sys/kernel/iommu_groups"
71 
72 /* define fd variable here, because file needs to be kept open for the
73  * duration of the program, as we hold a write lock on it in the primary proc */
74 static int mem_cfg_fd = -1;
75 
76 static struct flock wr_lock = {
77 		.l_type = F_WRLCK,
78 		.l_whence = SEEK_SET,
79 		.l_start = offsetof(struct rte_mem_config, memsegs),
80 		.l_len = RTE_SIZEOF_FIELD(struct rte_mem_config, memsegs),
81 };
82 
83 /* internal configuration (per-core) */
84 struct lcore_config lcore_config[RTE_MAX_LCORE];
85 
86 /* used by rte_rdtsc() */
87 int rte_cycles_vmware_tsc_map;
88 
89 static const char *default_runtime_dir = "/var/run";
90 
91 int
92 eal_create_runtime_dir(void)
93 {
94 	const char *directory = default_runtime_dir;
95 	const char *xdg_runtime_dir = getenv("XDG_RUNTIME_DIR");
96 	const char *fallback = "/tmp";
97 	char run_dir[PATH_MAX];
98 	char tmp[PATH_MAX];
99 	int ret;
100 
101 	if (getuid() != 0) {
102 		/* try XDG path first, fall back to /tmp */
103 		if (xdg_runtime_dir != NULL)
104 			directory = xdg_runtime_dir;
105 		else
106 			directory = fallback;
107 	}
108 	/* create DPDK subdirectory under runtime dir */
109 	ret = snprintf(tmp, sizeof(tmp), "%s/dpdk", directory);
110 	if (ret < 0 || ret == sizeof(tmp)) {
111 		RTE_LOG(ERR, EAL, "Error creating DPDK runtime path name\n");
112 		return -1;
113 	}
114 
115 	/* create prefix-specific subdirectory under DPDK runtime dir */
116 	ret = snprintf(run_dir, sizeof(run_dir), "%s/%s",
117 			tmp, eal_get_hugefile_prefix());
118 	if (ret < 0 || ret == sizeof(run_dir)) {
119 		RTE_LOG(ERR, EAL, "Error creating prefix-specific runtime path name\n");
120 		return -1;
121 	}
122 
123 	/* create the path if it doesn't exist. no "mkdir -p" here, so do it
124 	 * step by step.
125 	 */
126 	ret = mkdir(tmp, 0700);
127 	if (ret < 0 && errno != EEXIST) {
128 		RTE_LOG(ERR, EAL, "Error creating '%s': %s\n",
129 			tmp, strerror(errno));
130 		return -1;
131 	}
132 
133 	ret = mkdir(run_dir, 0700);
134 	if (ret < 0 && errno != EEXIST) {
135 		RTE_LOG(ERR, EAL, "Error creating '%s': %s\n",
136 			run_dir, strerror(errno));
137 		return -1;
138 	}
139 
140 	if (eal_set_runtime_dir(run_dir, sizeof(run_dir)))
141 		return -1;
142 
143 	return 0;
144 }
145 
146 int
147 eal_clean_runtime_dir(void)
148 {
149 	const char *runtime_dir = rte_eal_get_runtime_dir();
150 	DIR *dir;
151 	struct dirent *dirent;
152 	int dir_fd, fd, lck_result;
153 	static const char * const filters[] = {
154 		"fbarray_*",
155 		"mp_socket_*"
156 	};
157 
158 	/* open directory */
159 	dir = opendir(runtime_dir);
160 	if (!dir) {
161 		RTE_LOG(ERR, EAL, "Unable to open runtime directory %s\n",
162 				runtime_dir);
163 		goto error;
164 	}
165 	dir_fd = dirfd(dir);
166 
167 	/* lock the directory before doing anything, to avoid races */
168 	if (flock(dir_fd, LOCK_EX) < 0) {
169 		RTE_LOG(ERR, EAL, "Unable to lock runtime directory %s\n",
170 			runtime_dir);
171 		goto error;
172 	}
173 
174 	dirent = readdir(dir);
175 	if (!dirent) {
176 		RTE_LOG(ERR, EAL, "Unable to read runtime directory %s\n",
177 				runtime_dir);
178 		goto error;
179 	}
180 
181 	while (dirent != NULL) {
182 		unsigned int f_idx;
183 		bool skip = true;
184 
185 		/* skip files that don't match the patterns */
186 		for (f_idx = 0; f_idx < RTE_DIM(filters); f_idx++) {
187 			const char *filter = filters[f_idx];
188 
189 			if (fnmatch(filter, dirent->d_name, 0) == 0) {
190 				skip = false;
191 				break;
192 			}
193 		}
194 		if (skip) {
195 			dirent = readdir(dir);
196 			continue;
197 		}
198 
199 		/* try and lock the file */
200 		fd = openat(dir_fd, dirent->d_name, O_RDONLY);
201 
202 		/* skip to next file */
203 		if (fd == -1) {
204 			dirent = readdir(dir);
205 			continue;
206 		}
207 
208 		/* non-blocking lock */
209 		lck_result = flock(fd, LOCK_EX | LOCK_NB);
210 
211 		/* if lock succeeds, remove the file */
212 		if (lck_result != -1)
213 			unlinkat(dir_fd, dirent->d_name, 0);
214 		close(fd);
215 		dirent = readdir(dir);
216 	}
217 
218 	/* closedir closes dir_fd and drops the lock */
219 	closedir(dir);
220 	return 0;
221 
222 error:
223 	if (dir)
224 		closedir(dir);
225 
226 	RTE_LOG(ERR, EAL, "Error while clearing runtime dir: %s\n",
227 		strerror(errno));
228 
229 	return -1;
230 }
231 
232 /* parse a sysfs (or other) file containing one integer value */
233 int
234 eal_parse_sysfs_value(const char *filename, unsigned long *val)
235 {
236 	FILE *f;
237 	char buf[BUFSIZ];
238 	char *end = NULL;
239 
240 	if ((f = fopen(filename, "r")) == NULL) {
241 		RTE_LOG(ERR, EAL, "%s(): cannot open sysfs value %s\n",
242 			__func__, filename);
243 		return -1;
244 	}
245 
246 	if (fgets(buf, sizeof(buf), f) == NULL) {
247 		RTE_LOG(ERR, EAL, "%s(): cannot read sysfs value %s\n",
248 			__func__, filename);
249 		fclose(f);
250 		return -1;
251 	}
252 	*val = strtoul(buf, &end, 0);
253 	if ((buf[0] == '\0') || (end == NULL) || (*end != '\n')) {
254 		RTE_LOG(ERR, EAL, "%s(): cannot parse sysfs value %s\n",
255 				__func__, filename);
256 		fclose(f);
257 		return -1;
258 	}
259 	fclose(f);
260 	return 0;
261 }
262 
263 
264 /* create memory configuration in shared/mmap memory. Take out
265  * a write lock on the memsegs, so we can auto-detect primary/secondary.
266  * This means we never close the file while running (auto-close on exit).
267  * We also don't lock the whole file, so that in future we can use read-locks
268  * on other parts, e.g. memzones, to detect if there are running secondary
269  * processes. */
270 static int
271 rte_eal_config_create(void)
272 {
273 	struct rte_config *config = rte_eal_get_configuration();
274 	size_t page_sz = sysconf(_SC_PAGE_SIZE);
275 	size_t cfg_len = sizeof(*config->mem_config);
276 	size_t cfg_len_aligned = RTE_ALIGN(cfg_len, page_sz);
277 	void *rte_mem_cfg_addr, *mapped_mem_cfg_addr;
278 	int retval;
279 	const struct internal_config *internal_conf =
280 		eal_get_internal_configuration();
281 
282 	const char *pathname = eal_runtime_config_path();
283 
284 	if (internal_conf->no_shconf)
285 		return 0;
286 
287 	/* map the config before hugepage address so that we don't waste a page */
288 	if (internal_conf->base_virtaddr != 0)
289 		rte_mem_cfg_addr = (void *)
290 			RTE_ALIGN_FLOOR(internal_conf->base_virtaddr -
291 			sizeof(struct rte_mem_config), page_sz);
292 	else
293 		rte_mem_cfg_addr = NULL;
294 
295 	if (mem_cfg_fd < 0){
296 		mem_cfg_fd = open(pathname, O_RDWR | O_CREAT, 0600);
297 		if (mem_cfg_fd < 0) {
298 			RTE_LOG(ERR, EAL, "Cannot open '%s' for rte_mem_config\n",
299 				pathname);
300 			return -1;
301 		}
302 	}
303 
304 	retval = ftruncate(mem_cfg_fd, cfg_len);
305 	if (retval < 0){
306 		close(mem_cfg_fd);
307 		mem_cfg_fd = -1;
308 		RTE_LOG(ERR, EAL, "Cannot resize '%s' for rte_mem_config\n",
309 			pathname);
310 		return -1;
311 	}
312 
313 	retval = fcntl(mem_cfg_fd, F_SETLK, &wr_lock);
314 	if (retval < 0){
315 		close(mem_cfg_fd);
316 		mem_cfg_fd = -1;
317 		RTE_LOG(ERR, EAL, "Cannot create lock on '%s'. Is another primary "
318 			"process running?\n", pathname);
319 		return -1;
320 	}
321 
322 	/* reserve space for config */
323 	rte_mem_cfg_addr = eal_get_virtual_area(rte_mem_cfg_addr,
324 			&cfg_len_aligned, page_sz, 0, 0);
325 	if (rte_mem_cfg_addr == NULL) {
326 		RTE_LOG(ERR, EAL, "Cannot mmap memory for rte_config\n");
327 		close(mem_cfg_fd);
328 		mem_cfg_fd = -1;
329 		return -1;
330 	}
331 
332 	/* remap the actual file into the space we've just reserved */
333 	mapped_mem_cfg_addr = mmap(rte_mem_cfg_addr,
334 			cfg_len_aligned, PROT_READ | PROT_WRITE,
335 			MAP_SHARED | MAP_FIXED, mem_cfg_fd, 0);
336 	if (mapped_mem_cfg_addr == MAP_FAILED) {
337 		munmap(rte_mem_cfg_addr, cfg_len);
338 		close(mem_cfg_fd);
339 		mem_cfg_fd = -1;
340 		RTE_LOG(ERR, EAL, "Cannot remap memory for rte_config\n");
341 		return -1;
342 	}
343 
344 	memcpy(rte_mem_cfg_addr, config->mem_config, sizeof(struct rte_mem_config));
345 	config->mem_config = rte_mem_cfg_addr;
346 
347 	/* store address of the config in the config itself so that secondary
348 	 * processes could later map the config into this exact location
349 	 */
350 	config->mem_config->mem_cfg_addr = (uintptr_t) rte_mem_cfg_addr;
351 	config->mem_config->dma_maskbits = 0;
352 
353 	return 0;
354 }
355 
356 /* attach to an existing shared memory config */
357 static int
358 rte_eal_config_attach(void)
359 {
360 	struct rte_config *config = rte_eal_get_configuration();
361 	struct rte_mem_config *mem_config;
362 	const struct internal_config *internal_conf =
363 		eal_get_internal_configuration();
364 
365 	const char *pathname = eal_runtime_config_path();
366 
367 	if (internal_conf->no_shconf)
368 		return 0;
369 
370 	if (mem_cfg_fd < 0){
371 		mem_cfg_fd = open(pathname, O_RDWR);
372 		if (mem_cfg_fd < 0) {
373 			RTE_LOG(ERR, EAL, "Cannot open '%s' for rte_mem_config\n",
374 				pathname);
375 			return -1;
376 		}
377 	}
378 
379 	/* map it as read-only first */
380 	mem_config = (struct rte_mem_config *) mmap(NULL, sizeof(*mem_config),
381 			PROT_READ, MAP_SHARED, mem_cfg_fd, 0);
382 	if (mem_config == MAP_FAILED) {
383 		close(mem_cfg_fd);
384 		mem_cfg_fd = -1;
385 		RTE_LOG(ERR, EAL, "Cannot mmap memory for rte_config! error %i (%s)\n",
386 			errno, strerror(errno));
387 		return -1;
388 	}
389 
390 	config->mem_config = mem_config;
391 
392 	return 0;
393 }
394 
395 /* reattach the shared config at exact memory location primary process has it */
396 static int
397 rte_eal_config_reattach(void)
398 {
399 	struct rte_config *config = rte_eal_get_configuration();
400 	struct rte_mem_config *mem_config;
401 	void *rte_mem_cfg_addr;
402 	const struct internal_config *internal_conf =
403 		eal_get_internal_configuration();
404 
405 	if (internal_conf->no_shconf)
406 		return 0;
407 
408 	/* save the address primary process has mapped shared config to */
409 	rte_mem_cfg_addr =
410 		(void *) (uintptr_t) config->mem_config->mem_cfg_addr;
411 
412 	/* unmap original config */
413 	munmap(config->mem_config, sizeof(struct rte_mem_config));
414 
415 	/* remap the config at proper address */
416 	mem_config = (struct rte_mem_config *) mmap(rte_mem_cfg_addr,
417 			sizeof(*mem_config), PROT_READ | PROT_WRITE, MAP_SHARED,
418 			mem_cfg_fd, 0);
419 
420 	close(mem_cfg_fd);
421 	mem_cfg_fd = -1;
422 
423 	if (mem_config == MAP_FAILED || mem_config != rte_mem_cfg_addr) {
424 		if (mem_config != MAP_FAILED) {
425 			/* errno is stale, don't use */
426 			RTE_LOG(ERR, EAL, "Cannot mmap memory for rte_config at [%p], got [%p]"
427 				" - please use '--" OPT_BASE_VIRTADDR
428 				"' option\n", rte_mem_cfg_addr, mem_config);
429 			munmap(mem_config, sizeof(struct rte_mem_config));
430 			return -1;
431 		}
432 		RTE_LOG(ERR, EAL, "Cannot mmap memory for rte_config! error %i (%s)\n",
433 			errno, strerror(errno));
434 		return -1;
435 	}
436 
437 	config->mem_config = mem_config;
438 
439 	return 0;
440 }
441 
442 /* Detect if we are a primary or a secondary process */
443 enum rte_proc_type_t
444 eal_proc_type_detect(void)
445 {
446 	enum rte_proc_type_t ptype = RTE_PROC_PRIMARY;
447 	const char *pathname = eal_runtime_config_path();
448 	const struct internal_config *internal_conf =
449 		eal_get_internal_configuration();
450 
451 	/* if there no shared config, there can be no secondary processes */
452 	if (!internal_conf->no_shconf) {
453 		/* if we can open the file but not get a write-lock we are a
454 		 * secondary process. NOTE: if we get a file handle back, we
455 		 * keep that open and don't close it to prevent a race condition
456 		 * between multiple opens.
457 		 */
458 		if (((mem_cfg_fd = open(pathname, O_RDWR)) >= 0) &&
459 				(fcntl(mem_cfg_fd, F_SETLK, &wr_lock) < 0))
460 			ptype = RTE_PROC_SECONDARY;
461 	}
462 
463 	RTE_LOG(INFO, EAL, "Auto-detected process type: %s\n",
464 			ptype == RTE_PROC_PRIMARY ? "PRIMARY" : "SECONDARY");
465 
466 	return ptype;
467 }
468 
469 /* Sets up rte_config structure with the pointer to shared memory config.*/
470 static int
471 rte_config_init(void)
472 {
473 	struct rte_config *config = rte_eal_get_configuration();
474 	const struct internal_config *internal_conf =
475 		eal_get_internal_configuration();
476 
477 	config->process_type = internal_conf->process_type;
478 
479 	switch (config->process_type) {
480 	case RTE_PROC_PRIMARY:
481 		if (rte_eal_config_create() < 0)
482 			return -1;
483 		eal_mcfg_update_from_internal();
484 		break;
485 	case RTE_PROC_SECONDARY:
486 		if (rte_eal_config_attach() < 0)
487 			return -1;
488 		eal_mcfg_wait_complete();
489 		if (eal_mcfg_check_version() < 0) {
490 			RTE_LOG(ERR, EAL, "Primary and secondary process DPDK version mismatch\n");
491 			return -1;
492 		}
493 		if (rte_eal_config_reattach() < 0)
494 			return -1;
495 		if (!__rte_mp_enable()) {
496 			RTE_LOG(ERR, EAL, "Primary process refused secondary attachment\n");
497 			return -1;
498 		}
499 		eal_mcfg_update_internal();
500 		break;
501 	case RTE_PROC_AUTO:
502 	case RTE_PROC_INVALID:
503 		RTE_LOG(ERR, EAL, "Invalid process type %d\n",
504 			config->process_type);
505 		return -1;
506 	}
507 
508 	return 0;
509 }
510 
511 /* Unlocks hugepage directories that were locked by eal_hugepage_info_init */
512 static void
513 eal_hugedirs_unlock(void)
514 {
515 	int i;
516 	struct internal_config *internal_conf =
517 		eal_get_internal_configuration();
518 
519 	for (i = 0; i < MAX_HUGEPAGE_SIZES; i++)
520 	{
521 		/* skip uninitialized */
522 		if (internal_conf->hugepage_info[i].lock_descriptor < 0)
523 			continue;
524 		/* unlock hugepage file */
525 		flock(internal_conf->hugepage_info[i].lock_descriptor, LOCK_UN);
526 		close(internal_conf->hugepage_info[i].lock_descriptor);
527 		/* reset the field */
528 		internal_conf->hugepage_info[i].lock_descriptor = -1;
529 	}
530 }
531 
532 /* display usage */
533 static void
534 eal_usage(const char *prgname)
535 {
536 	rte_usage_hook_t hook = eal_get_application_usage_hook();
537 
538 	printf("\nUsage: %s ", prgname);
539 	eal_common_usage();
540 	printf("EAL Linux options:\n"
541 	       "  --"OPT_SOCKET_MEM"        Memory to allocate on sockets (comma separated values)\n"
542 	       "  --"OPT_SOCKET_LIMIT"      Limit memory allocation on sockets (comma separated values)\n"
543 	       "  --"OPT_HUGE_DIR"          Directory where hugetlbfs is mounted\n"
544 	       "  --"OPT_FILE_PREFIX"       Prefix for hugepage filenames\n"
545 	       "  --"OPT_CREATE_UIO_DEV"    Create /dev/uioX (usually done by hotplug)\n"
546 	       "  --"OPT_VFIO_INTR"         Interrupt mode for VFIO (legacy|msi|msix)\n"
547 	       "  --"OPT_VFIO_VF_TOKEN"     VF token (UUID) shared between SR-IOV PF and VFs\n"
548 	       "  --"OPT_LEGACY_MEM"        Legacy memory mode (no dynamic allocation, contiguous segments)\n"
549 	       "  --"OPT_SINGLE_FILE_SEGMENTS" Put all hugepage memory in single files\n"
550 	       "  --"OPT_MATCH_ALLOCATIONS" Free hugepages exactly as allocated\n"
551 	       "\n");
552 	/* Allow the application to print its usage message too if hook is set */
553 	if (hook) {
554 		printf("===== Application Usage =====\n\n");
555 		(hook)(prgname);
556 	}
557 }
558 
559 static int
560 eal_parse_socket_arg(char *strval, volatile uint64_t *socket_arg)
561 {
562 	char * arg[RTE_MAX_NUMA_NODES];
563 	char *end;
564 	int arg_num, i, len;
565 	uint64_t total_mem = 0;
566 
567 	len = strnlen(strval, SOCKET_MEM_STRLEN);
568 	if (len == SOCKET_MEM_STRLEN) {
569 		RTE_LOG(ERR, EAL, "--socket-mem is too long\n");
570 		return -1;
571 	}
572 
573 	/* all other error cases will be caught later */
574 	if (!isdigit(strval[len-1]))
575 		return -1;
576 
577 	/* split the optarg into separate socket values */
578 	arg_num = rte_strsplit(strval, len,
579 			arg, RTE_MAX_NUMA_NODES, ',');
580 
581 	/* if split failed, or 0 arguments */
582 	if (arg_num <= 0)
583 		return -1;
584 
585 	/* parse each defined socket option */
586 	errno = 0;
587 	for (i = 0; i < arg_num; i++) {
588 		uint64_t val;
589 		end = NULL;
590 		val = strtoull(arg[i], &end, 10);
591 
592 		/* check for invalid input */
593 		if ((errno != 0)  ||
594 				(arg[i][0] == '\0') || (end == NULL) || (*end != '\0'))
595 			return -1;
596 		val <<= 20;
597 		total_mem += val;
598 		socket_arg[i] = val;
599 	}
600 
601 	return 0;
602 }
603 
604 static int
605 eal_parse_vfio_intr(const char *mode)
606 {
607 	struct internal_config *internal_conf =
608 		eal_get_internal_configuration();
609 	unsigned i;
610 	static struct {
611 		const char *name;
612 		enum rte_intr_mode value;
613 	} map[] = {
614 		{ "legacy", RTE_INTR_MODE_LEGACY },
615 		{ "msi", RTE_INTR_MODE_MSI },
616 		{ "msix", RTE_INTR_MODE_MSIX },
617 	};
618 
619 	for (i = 0; i < RTE_DIM(map); i++) {
620 		if (!strcmp(mode, map[i].name)) {
621 			internal_conf->vfio_intr_mode = map[i].value;
622 			return 0;
623 		}
624 	}
625 	return -1;
626 }
627 
628 static int
629 eal_parse_vfio_vf_token(const char *vf_token)
630 {
631 	struct internal_config *cfg = eal_get_internal_configuration();
632 	rte_uuid_t uuid;
633 
634 	if (!rte_uuid_parse(vf_token, uuid)) {
635 		rte_uuid_copy(cfg->vfio_vf_token, uuid);
636 		return 0;
637 	}
638 
639 	return -1;
640 }
641 
642 /* Parse the arguments for --log-level only */
643 static void
644 eal_log_level_parse(int argc, char **argv)
645 {
646 	int opt;
647 	char **argvopt;
648 	int option_index;
649 	const int old_optind = optind;
650 	const int old_optopt = optopt;
651 	char * const old_optarg = optarg;
652 	struct internal_config *internal_conf =
653 		eal_get_internal_configuration();
654 
655 	argvopt = argv;
656 	optind = 1;
657 
658 	while ((opt = getopt_long(argc, argvopt, eal_short_options,
659 				  eal_long_options, &option_index)) != EOF) {
660 
661 		int ret;
662 
663 		/* getopt is not happy, stop right now */
664 		if (opt == '?')
665 			break;
666 
667 		ret = (opt == OPT_LOG_LEVEL_NUM) ?
668 			eal_parse_common_option(opt, optarg, internal_conf) : 0;
669 
670 		/* common parser is not happy */
671 		if (ret < 0)
672 			break;
673 	}
674 
675 	/* restore getopt lib */
676 	optind = old_optind;
677 	optopt = old_optopt;
678 	optarg = old_optarg;
679 }
680 
681 /* Parse the argument given in the command line of the application */
682 static int
683 eal_parse_args(int argc, char **argv)
684 {
685 	int opt, ret;
686 	char **argvopt;
687 	int option_index;
688 	char *prgname = argv[0];
689 	const int old_optind = optind;
690 	const int old_optopt = optopt;
691 	char * const old_optarg = optarg;
692 	struct internal_config *internal_conf =
693 		eal_get_internal_configuration();
694 
695 	argvopt = argv;
696 	optind = 1;
697 
698 	while ((opt = getopt_long(argc, argvopt, eal_short_options,
699 				  eal_long_options, &option_index)) != EOF) {
700 
701 		/* getopt didn't recognise the option */
702 		if (opt == '?') {
703 			eal_usage(prgname);
704 			ret = -1;
705 			goto out;
706 		}
707 
708 		/* eal_log_level_parse() already handled this option */
709 		if (opt == OPT_LOG_LEVEL_NUM)
710 			continue;
711 
712 		ret = eal_parse_common_option(opt, optarg, internal_conf);
713 		/* common parser is not happy */
714 		if (ret < 0) {
715 			eal_usage(prgname);
716 			ret = -1;
717 			goto out;
718 		}
719 		/* common parser handled this option */
720 		if (ret == 0)
721 			continue;
722 
723 		switch (opt) {
724 		case 'h':
725 			eal_usage(prgname);
726 			exit(EXIT_SUCCESS);
727 
728 		case OPT_HUGE_DIR_NUM:
729 		{
730 			char *hdir = strdup(optarg);
731 			if (hdir == NULL)
732 				RTE_LOG(ERR, EAL, "Could not store hugepage directory\n");
733 			else {
734 				/* free old hugepage dir */
735 				if (internal_conf->hugepage_dir != NULL)
736 					free(internal_conf->hugepage_dir);
737 				internal_conf->hugepage_dir = hdir;
738 			}
739 			break;
740 		}
741 		case OPT_FILE_PREFIX_NUM:
742 		{
743 			char *prefix = strdup(optarg);
744 			if (prefix == NULL)
745 				RTE_LOG(ERR, EAL, "Could not store file prefix\n");
746 			else {
747 				/* free old prefix */
748 				if (internal_conf->hugefile_prefix != NULL)
749 					free(internal_conf->hugefile_prefix);
750 				internal_conf->hugefile_prefix = prefix;
751 			}
752 			break;
753 		}
754 		case OPT_SOCKET_MEM_NUM:
755 			if (eal_parse_socket_arg(optarg,
756 					internal_conf->socket_mem) < 0) {
757 				RTE_LOG(ERR, EAL, "invalid parameters for --"
758 						OPT_SOCKET_MEM "\n");
759 				eal_usage(prgname);
760 				ret = -1;
761 				goto out;
762 			}
763 			internal_conf->force_sockets = 1;
764 			break;
765 
766 		case OPT_SOCKET_LIMIT_NUM:
767 			if (eal_parse_socket_arg(optarg,
768 					internal_conf->socket_limit) < 0) {
769 				RTE_LOG(ERR, EAL, "invalid parameters for --"
770 						OPT_SOCKET_LIMIT "\n");
771 				eal_usage(prgname);
772 				ret = -1;
773 				goto out;
774 			}
775 			internal_conf->force_socket_limits = 1;
776 			break;
777 
778 		case OPT_VFIO_INTR_NUM:
779 			if (eal_parse_vfio_intr(optarg) < 0) {
780 				RTE_LOG(ERR, EAL, "invalid parameters for --"
781 						OPT_VFIO_INTR "\n");
782 				eal_usage(prgname);
783 				ret = -1;
784 				goto out;
785 			}
786 			break;
787 
788 		case OPT_VFIO_VF_TOKEN_NUM:
789 			if (eal_parse_vfio_vf_token(optarg) < 0) {
790 				RTE_LOG(ERR, EAL, "invalid parameters for --"
791 						OPT_VFIO_VF_TOKEN "\n");
792 				eal_usage(prgname);
793 				ret = -1;
794 				goto out;
795 			}
796 			break;
797 
798 		case OPT_CREATE_UIO_DEV_NUM:
799 			internal_conf->create_uio_dev = 1;
800 			break;
801 
802 		case OPT_MBUF_POOL_OPS_NAME_NUM:
803 		{
804 			char *ops_name = strdup(optarg);
805 			if (ops_name == NULL)
806 				RTE_LOG(ERR, EAL, "Could not store mbuf pool ops name\n");
807 			else {
808 				/* free old ops name */
809 				if (internal_conf->user_mbuf_pool_ops_name !=
810 						NULL)
811 					free(internal_conf->user_mbuf_pool_ops_name);
812 
813 				internal_conf->user_mbuf_pool_ops_name =
814 						ops_name;
815 			}
816 			break;
817 		}
818 		case OPT_MATCH_ALLOCATIONS_NUM:
819 			internal_conf->match_allocations = 1;
820 			break;
821 
822 		default:
823 			if (opt < OPT_LONG_MIN_NUM && isprint(opt)) {
824 				RTE_LOG(ERR, EAL, "Option %c is not supported "
825 					"on Linux\n", opt);
826 			} else if (opt >= OPT_LONG_MIN_NUM &&
827 				   opt < OPT_LONG_MAX_NUM) {
828 				RTE_LOG(ERR, EAL, "Option %s is not supported "
829 					"on Linux\n",
830 					eal_long_options[option_index].name);
831 			} else {
832 				RTE_LOG(ERR, EAL, "Option %d is not supported "
833 					"on Linux\n", opt);
834 			}
835 			eal_usage(prgname);
836 			ret = -1;
837 			goto out;
838 		}
839 	}
840 
841 	/* create runtime data directory. In no_shconf mode, skip any errors */
842 	if (eal_create_runtime_dir() < 0) {
843 		if (internal_conf->no_shconf == 0) {
844 			RTE_LOG(ERR, EAL, "Cannot create runtime directory\n");
845 			ret = -1;
846 			goto out;
847 		} else
848 			RTE_LOG(WARNING, EAL, "No DPDK runtime directory created\n");
849 	}
850 
851 	if (eal_adjust_config(internal_conf) != 0) {
852 		ret = -1;
853 		goto out;
854 	}
855 
856 	/* sanity checks */
857 	if (eal_check_common_options(internal_conf) != 0) {
858 		eal_usage(prgname);
859 		ret = -1;
860 		goto out;
861 	}
862 
863 	if (optind >= 0)
864 		argv[optind-1] = prgname;
865 	ret = optind-1;
866 
867 out:
868 	/* restore getopt lib */
869 	optind = old_optind;
870 	optopt = old_optopt;
871 	optarg = old_optarg;
872 
873 	return ret;
874 }
875 
876 static int
877 check_socket(const struct rte_memseg_list *msl, void *arg)
878 {
879 	int *socket_id = arg;
880 
881 	if (msl->external)
882 		return 0;
883 
884 	return *socket_id == msl->socket_id;
885 }
886 
887 static void
888 eal_check_mem_on_local_socket(void)
889 {
890 	int socket_id;
891 	const struct rte_config *config = rte_eal_get_configuration();
892 
893 	socket_id = rte_lcore_to_socket_id(config->main_lcore);
894 
895 	if (rte_memseg_list_walk(check_socket, &socket_id) == 0)
896 		RTE_LOG(WARNING, EAL, "WARNING: Main core has no memory on local socket!\n");
897 }
898 
899 static int
900 sync_func(__rte_unused void *arg)
901 {
902 	return 0;
903 }
904 
905 /*
906  * Request iopl privilege for all RPL, returns 0 on success
907  * iopl() call is mostly for the i386 architecture. For other architectures,
908  * return -1 to indicate IO privilege can't be changed in this way.
909  */
910 int
911 rte_eal_iopl_init(void)
912 {
913 #if defined(RTE_ARCH_X86)
914 	if (iopl(3) != 0)
915 		return -1;
916 #endif
917 	return 0;
918 }
919 
920 #ifdef VFIO_PRESENT
921 static int rte_eal_vfio_setup(void)
922 {
923 	if (rte_vfio_enable("vfio"))
924 		return -1;
925 
926 	return 0;
927 }
928 #endif
929 
930 static void rte_eal_init_alert(const char *msg)
931 {
932 	fprintf(stderr, "EAL: FATAL: %s\n", msg);
933 	RTE_LOG(ERR, EAL, "%s\n", msg);
934 }
935 
936 /*
937  * On Linux 3.6+, even if VFIO is not loaded, whenever IOMMU is enabled in the
938  * BIOS and in the kernel, /sys/kernel/iommu_groups path will contain kernel
939  * IOMMU groups. If IOMMU is not enabled, that path would be empty.
940  * Therefore, checking if the path is empty will tell us if IOMMU is enabled.
941  */
942 static bool
943 is_iommu_enabled(void)
944 {
945 	DIR *dir = opendir(KERNEL_IOMMU_GROUPS_PATH);
946 	struct dirent *d;
947 	int n = 0;
948 
949 	/* if directory doesn't exist, assume IOMMU is not enabled */
950 	if (dir == NULL)
951 		return false;
952 
953 	while ((d = readdir(dir)) != NULL) {
954 		/* skip dot and dot-dot */
955 		if (++n > 2)
956 			break;
957 	}
958 	closedir(dir);
959 
960 	return n > 2;
961 }
962 
963 /* Launch threads, called at application init(). */
964 int
965 rte_eal_init(int argc, char **argv)
966 {
967 	int i, fctret, ret;
968 	pthread_t thread_id;
969 	static uint32_t run_once;
970 	uint32_t has_run = 0;
971 	const char *p;
972 	static char logid[PATH_MAX];
973 	char cpuset[RTE_CPU_AFFINITY_STR_LEN];
974 	char thread_name[RTE_MAX_THREAD_NAME_LEN];
975 	bool phys_addrs;
976 	const struct rte_config *config = rte_eal_get_configuration();
977 	struct internal_config *internal_conf =
978 		eal_get_internal_configuration();
979 
980 	/* checks if the machine is adequate */
981 	if (!rte_cpu_is_supported()) {
982 		rte_eal_init_alert("unsupported cpu type.");
983 		rte_errno = ENOTSUP;
984 		return -1;
985 	}
986 
987 	if (!__atomic_compare_exchange_n(&run_once, &has_run, 1, 0,
988 					__ATOMIC_RELAXED, __ATOMIC_RELAXED)) {
989 		rte_eal_init_alert("already called initialization.");
990 		rte_errno = EALREADY;
991 		return -1;
992 	}
993 
994 	p = strrchr(argv[0], '/');
995 	strlcpy(logid, p ? p + 1 : argv[0], sizeof(logid));
996 	thread_id = pthread_self();
997 
998 	eal_reset_internal_config(internal_conf);
999 
1000 	/* set log level as early as possible */
1001 	eal_log_level_parse(argc, argv);
1002 
1003 	/* clone argv to report out later in telemetry */
1004 	eal_save_args(argc, argv);
1005 
1006 	if (rte_eal_cpu_init() < 0) {
1007 		rte_eal_init_alert("Cannot detect lcores.");
1008 		rte_errno = ENOTSUP;
1009 		return -1;
1010 	}
1011 
1012 	fctret = eal_parse_args(argc, argv);
1013 	if (fctret < 0) {
1014 		rte_eal_init_alert("Invalid 'command line' arguments.");
1015 		rte_errno = EINVAL;
1016 		__atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
1017 		return -1;
1018 	}
1019 
1020 	if (eal_plugins_init() < 0) {
1021 		rte_eal_init_alert("Cannot init plugins");
1022 		rte_errno = EINVAL;
1023 		__atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
1024 		return -1;
1025 	}
1026 
1027 	if (eal_trace_init() < 0) {
1028 		rte_eal_init_alert("Cannot init trace");
1029 		rte_errno = EFAULT;
1030 		return -1;
1031 	}
1032 
1033 	if (eal_option_device_parse()) {
1034 		rte_errno = ENODEV;
1035 		__atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
1036 		return -1;
1037 	}
1038 
1039 	if (rte_config_init() < 0) {
1040 		rte_eal_init_alert("Cannot init config");
1041 		return -1;
1042 	}
1043 
1044 	if (rte_eal_intr_init() < 0) {
1045 		rte_eal_init_alert("Cannot init interrupt-handling thread");
1046 		return -1;
1047 	}
1048 
1049 	if (rte_eal_alarm_init() < 0) {
1050 		rte_eal_init_alert("Cannot init alarm");
1051 		/* rte_eal_alarm_init sets rte_errno on failure. */
1052 		return -1;
1053 	}
1054 
1055 	/* Put mp channel init before bus scan so that we can init the vdev
1056 	 * bus through mp channel in the secondary process before the bus scan.
1057 	 */
1058 	if (rte_mp_channel_init() < 0 && rte_errno != ENOTSUP) {
1059 		rte_eal_init_alert("failed to init mp channel");
1060 		if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
1061 			rte_errno = EFAULT;
1062 			return -1;
1063 		}
1064 	}
1065 
1066 	/* register multi-process action callbacks for hotplug */
1067 	if (eal_mp_dev_hotplug_init() < 0) {
1068 		rte_eal_init_alert("failed to register mp callback for hotplug");
1069 		return -1;
1070 	}
1071 
1072 	if (rte_bus_scan()) {
1073 		rte_eal_init_alert("Cannot scan the buses for devices");
1074 		rte_errno = ENODEV;
1075 		__atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
1076 		return -1;
1077 	}
1078 
1079 	phys_addrs = rte_eal_using_phys_addrs() != 0;
1080 
1081 	/* if no EAL option "--iova-mode=<pa|va>", use bus IOVA scheme */
1082 	if (internal_conf->iova_mode == RTE_IOVA_DC) {
1083 		/* autodetect the IOVA mapping mode */
1084 		enum rte_iova_mode iova_mode = rte_bus_get_iommu_class();
1085 
1086 		if (iova_mode == RTE_IOVA_DC) {
1087 			RTE_LOG(DEBUG, EAL, "Buses did not request a specific IOVA mode.\n");
1088 
1089 			if (!phys_addrs) {
1090 				/* if we have no access to physical addresses,
1091 				 * pick IOVA as VA mode.
1092 				 */
1093 				iova_mode = RTE_IOVA_VA;
1094 				RTE_LOG(DEBUG, EAL, "Physical addresses are unavailable, selecting IOVA as VA mode.\n");
1095 #if defined(RTE_LIB_KNI) && LINUX_VERSION_CODE >= KERNEL_VERSION(4, 10, 0)
1096 			} else if (rte_eal_check_module("rte_kni") == 1) {
1097 				iova_mode = RTE_IOVA_PA;
1098 				RTE_LOG(DEBUG, EAL, "KNI is loaded, selecting IOVA as PA mode for better KNI performance.\n");
1099 #endif
1100 			} else if (is_iommu_enabled()) {
1101 				/* we have an IOMMU, pick IOVA as VA mode */
1102 				iova_mode = RTE_IOVA_VA;
1103 				RTE_LOG(DEBUG, EAL, "IOMMU is available, selecting IOVA as VA mode.\n");
1104 			} else {
1105 				/* physical addresses available, and no IOMMU
1106 				 * found, so pick IOVA as PA.
1107 				 */
1108 				iova_mode = RTE_IOVA_PA;
1109 				RTE_LOG(DEBUG, EAL, "IOMMU is not available, selecting IOVA as PA mode.\n");
1110 			}
1111 		}
1112 #if defined(RTE_LIB_KNI) && LINUX_VERSION_CODE < KERNEL_VERSION(4, 10, 0)
1113 		/* Workaround for KNI which requires physical address to work
1114 		 * in kernels < 4.10
1115 		 */
1116 		if (iova_mode == RTE_IOVA_VA &&
1117 				rte_eal_check_module("rte_kni") == 1) {
1118 			if (phys_addrs) {
1119 				iova_mode = RTE_IOVA_PA;
1120 				RTE_LOG(WARNING, EAL, "Forcing IOVA as 'PA' because KNI module is loaded\n");
1121 			} else {
1122 				RTE_LOG(DEBUG, EAL, "KNI can not work since physical addresses are unavailable\n");
1123 			}
1124 		}
1125 #endif
1126 		rte_eal_get_configuration()->iova_mode = iova_mode;
1127 	} else {
1128 		rte_eal_get_configuration()->iova_mode =
1129 			internal_conf->iova_mode;
1130 	}
1131 
1132 	if (rte_eal_iova_mode() == RTE_IOVA_PA && !phys_addrs) {
1133 		rte_eal_init_alert("Cannot use IOVA as 'PA' since physical addresses are not available");
1134 		rte_errno = EINVAL;
1135 		return -1;
1136 	}
1137 
1138 	RTE_LOG(INFO, EAL, "Selected IOVA mode '%s'\n",
1139 		rte_eal_iova_mode() == RTE_IOVA_PA ? "PA" : "VA");
1140 
1141 	if (internal_conf->no_hugetlbfs == 0) {
1142 		/* rte_config isn't initialized yet */
1143 		ret = internal_conf->process_type == RTE_PROC_PRIMARY ?
1144 				eal_hugepage_info_init() :
1145 				eal_hugepage_info_read();
1146 		if (ret < 0) {
1147 			rte_eal_init_alert("Cannot get hugepage information.");
1148 			rte_errno = EACCES;
1149 			__atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
1150 			return -1;
1151 		}
1152 	}
1153 
1154 	if (internal_conf->memory == 0 && internal_conf->force_sockets == 0) {
1155 		if (internal_conf->no_hugetlbfs)
1156 			internal_conf->memory = MEMSIZE_IF_NO_HUGE_PAGE;
1157 	}
1158 
1159 	if (internal_conf->vmware_tsc_map == 1) {
1160 #ifdef RTE_LIBRTE_EAL_VMWARE_TSC_MAP_SUPPORT
1161 		rte_cycles_vmware_tsc_map = 1;
1162 		RTE_LOG (DEBUG, EAL, "Using VMWARE TSC MAP, "
1163 				"you must have monitor_control.pseudo_perfctr = TRUE\n");
1164 #else
1165 		RTE_LOG (WARNING, EAL, "Ignoring --vmware-tsc-map because "
1166 				"RTE_LIBRTE_EAL_VMWARE_TSC_MAP_SUPPORT is not set\n");
1167 #endif
1168 	}
1169 
1170 	if (eal_log_init(logid, internal_conf->syslog_facility) < 0) {
1171 		rte_eal_init_alert("Cannot init logging.");
1172 		rte_errno = ENOMEM;
1173 		__atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
1174 		return -1;
1175 	}
1176 
1177 #ifdef VFIO_PRESENT
1178 	if (rte_eal_vfio_setup() < 0) {
1179 		rte_eal_init_alert("Cannot init VFIO");
1180 		rte_errno = EAGAIN;
1181 		__atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
1182 		return -1;
1183 	}
1184 #endif
1185 	/* in secondary processes, memory init may allocate additional fbarrays
1186 	 * not present in primary processes, so to avoid any potential issues,
1187 	 * initialize memzones first.
1188 	 */
1189 	if (rte_eal_memzone_init() < 0) {
1190 		rte_eal_init_alert("Cannot init memzone");
1191 		rte_errno = ENODEV;
1192 		return -1;
1193 	}
1194 
1195 	if (rte_eal_memory_init() < 0) {
1196 		rte_eal_init_alert("Cannot init memory");
1197 		rte_errno = ENOMEM;
1198 		return -1;
1199 	}
1200 
1201 	/* the directories are locked during eal_hugepage_info_init */
1202 	eal_hugedirs_unlock();
1203 
1204 	if (rte_eal_malloc_heap_init() < 0) {
1205 		rte_eal_init_alert("Cannot init malloc heap");
1206 		rte_errno = ENODEV;
1207 		return -1;
1208 	}
1209 
1210 	if (rte_eal_tailqs_init() < 0) {
1211 		rte_eal_init_alert("Cannot init tail queues for objects");
1212 		rte_errno = EFAULT;
1213 		return -1;
1214 	}
1215 
1216 	if (rte_eal_timer_init() < 0) {
1217 		rte_eal_init_alert("Cannot init HPET or TSC timers");
1218 		rte_errno = ENOTSUP;
1219 		return -1;
1220 	}
1221 
1222 	eal_check_mem_on_local_socket();
1223 
1224 	if (pthread_setaffinity_np(pthread_self(), sizeof(rte_cpuset_t),
1225 			&lcore_config[config->main_lcore].cpuset) != 0) {
1226 		rte_eal_init_alert("Cannot set affinity");
1227 		rte_errno = EINVAL;
1228 		return -1;
1229 	}
1230 	__rte_thread_init(config->main_lcore,
1231 		&lcore_config[config->main_lcore].cpuset);
1232 
1233 	ret = eal_thread_dump_current_affinity(cpuset, sizeof(cpuset));
1234 	RTE_LOG(DEBUG, EAL, "Main lcore %u is ready (tid=%zx;cpuset=[%s%s])\n",
1235 		config->main_lcore, (uintptr_t)thread_id, cpuset,
1236 		ret == 0 ? "" : "...");
1237 
1238 	RTE_LCORE_FOREACH_WORKER(i) {
1239 
1240 		/*
1241 		 * create communication pipes between main thread
1242 		 * and children
1243 		 */
1244 		if (pipe(lcore_config[i].pipe_main2worker) < 0)
1245 			rte_panic("Cannot create pipe\n");
1246 		if (pipe(lcore_config[i].pipe_worker2main) < 0)
1247 			rte_panic("Cannot create pipe\n");
1248 
1249 		lcore_config[i].state = WAIT;
1250 
1251 		/* create a thread for each lcore */
1252 		ret = pthread_create(&lcore_config[i].thread_id, NULL,
1253 				     eal_thread_loop, NULL);
1254 		if (ret != 0)
1255 			rte_panic("Cannot create thread\n");
1256 
1257 		/* Set thread_name for aid in debugging. */
1258 		snprintf(thread_name, sizeof(thread_name),
1259 			"lcore-worker-%d", i);
1260 		ret = rte_thread_setname(lcore_config[i].thread_id,
1261 						thread_name);
1262 		if (ret != 0)
1263 			RTE_LOG(DEBUG, EAL,
1264 				"Cannot set name for lcore thread\n");
1265 
1266 		ret = pthread_setaffinity_np(lcore_config[i].thread_id,
1267 			sizeof(rte_cpuset_t), &lcore_config[i].cpuset);
1268 		if (ret != 0)
1269 			rte_panic("Cannot set affinity\n");
1270 	}
1271 
1272 	/*
1273 	 * Launch a dummy function on all worker lcores, so that main lcore
1274 	 * knows they are all ready when this function returns.
1275 	 */
1276 	rte_eal_mp_remote_launch(sync_func, NULL, SKIP_MAIN);
1277 	rte_eal_mp_wait_lcore();
1278 
1279 	/* initialize services so vdevs register service during bus_probe. */
1280 	ret = rte_service_init();
1281 	if (ret) {
1282 		rte_eal_init_alert("rte_service_init() failed");
1283 		rte_errno = -ret;
1284 		return -1;
1285 	}
1286 
1287 	/* Probe all the buses and devices/drivers on them */
1288 	if (rte_bus_probe()) {
1289 		rte_eal_init_alert("Cannot probe devices");
1290 		rte_errno = ENOTSUP;
1291 		return -1;
1292 	}
1293 
1294 #ifdef VFIO_PRESENT
1295 	/* Register mp action after probe() so that we got enough info */
1296 	if (rte_vfio_is_enabled("vfio") && vfio_mp_sync_setup() < 0)
1297 		return -1;
1298 #endif
1299 
1300 	/* initialize default service/lcore mappings and start running. Ignore
1301 	 * -ENOTSUP, as it indicates no service coremask passed to EAL.
1302 	 */
1303 	ret = rte_service_start_with_defaults();
1304 	if (ret < 0 && ret != -ENOTSUP) {
1305 		rte_errno = -ret;
1306 		return -1;
1307 	}
1308 
1309 	/*
1310 	 * Clean up unused files in runtime directory. We do this at the end of
1311 	 * init and not at the beginning because we want to clean stuff up
1312 	 * whether we are primary or secondary process, but we cannot remove
1313 	 * primary process' files because secondary should be able to run even
1314 	 * if primary process is dead.
1315 	 *
1316 	 * In no_shconf mode, no runtime directory is created in the first
1317 	 * place, so no cleanup needed.
1318 	 */
1319 	if (!internal_conf->no_shconf && eal_clean_runtime_dir() < 0) {
1320 		rte_eal_init_alert("Cannot clear runtime directory");
1321 		return -1;
1322 	}
1323 	if (rte_eal_process_type() == RTE_PROC_PRIMARY && !internal_conf->no_telemetry) {
1324 		int tlog = rte_log_register_type_and_pick_level(
1325 				"lib.telemetry", RTE_LOG_WARNING);
1326 		if (tlog < 0)
1327 			tlog = RTE_LOGTYPE_EAL;
1328 		if (rte_telemetry_init(rte_eal_get_runtime_dir(),
1329 				rte_version(),
1330 				&internal_conf->ctrl_cpuset, rte_log, tlog) != 0)
1331 			return -1;
1332 	}
1333 
1334 	eal_mcfg_complete();
1335 
1336 	return fctret;
1337 }
1338 
1339 static int
1340 mark_freeable(const struct rte_memseg_list *msl, const struct rte_memseg *ms,
1341 		void *arg __rte_unused)
1342 {
1343 	/* ms is const, so find this memseg */
1344 	struct rte_memseg *found;
1345 
1346 	if (msl->external)
1347 		return 0;
1348 
1349 	found = rte_mem_virt2memseg(ms->addr, msl);
1350 
1351 	found->flags &= ~RTE_MEMSEG_FLAG_DO_NOT_FREE;
1352 
1353 	return 0;
1354 }
1355 
1356 int
1357 rte_eal_cleanup(void)
1358 {
1359 	/* if we're in a primary process, we need to mark hugepages as freeable
1360 	 * so that finalization can release them back to the system.
1361 	 */
1362 	struct internal_config *internal_conf =
1363 		eal_get_internal_configuration();
1364 
1365 	if (rte_eal_process_type() == RTE_PROC_PRIMARY)
1366 		rte_memseg_walk(mark_freeable, NULL);
1367 	rte_service_finalize();
1368 	rte_mp_channel_cleanup();
1369 	/* after this point, any DPDK pointers will become dangling */
1370 	rte_eal_memory_detach();
1371 	rte_eal_alarm_cleanup();
1372 	rte_trace_save();
1373 	eal_trace_fini();
1374 	eal_cleanup_config(internal_conf);
1375 	return 0;
1376 }
1377 
1378 int rte_eal_create_uio_dev(void)
1379 {
1380 	const struct internal_config *internal_conf =
1381 		eal_get_internal_configuration();
1382 
1383 	return internal_conf->create_uio_dev;
1384 }
1385 
1386 enum rte_intr_mode
1387 rte_eal_vfio_intr_mode(void)
1388 {
1389 	const struct internal_config *internal_conf =
1390 		eal_get_internal_configuration();
1391 
1392 	return internal_conf->vfio_intr_mode;
1393 }
1394 
1395 void
1396 rte_eal_vfio_get_vf_token(rte_uuid_t vf_token)
1397 {
1398 	struct internal_config *cfg = eal_get_internal_configuration();
1399 
1400 	rte_uuid_copy(vf_token, cfg->vfio_vf_token);
1401 }
1402 
1403 int
1404 rte_eal_check_module(const char *module_name)
1405 {
1406 	char sysfs_mod_name[PATH_MAX];
1407 	struct stat st;
1408 	int n;
1409 
1410 	if (NULL == module_name)
1411 		return -1;
1412 
1413 	/* Check if there is sysfs mounted */
1414 	if (stat("/sys/module", &st) != 0) {
1415 		RTE_LOG(DEBUG, EAL, "sysfs is not mounted! error %i (%s)\n",
1416 			errno, strerror(errno));
1417 		return -1;
1418 	}
1419 
1420 	/* A module might be built-in, therefore try sysfs */
1421 	n = snprintf(sysfs_mod_name, PATH_MAX, "/sys/module/%s", module_name);
1422 	if (n < 0 || n > PATH_MAX) {
1423 		RTE_LOG(DEBUG, EAL, "Could not format module path\n");
1424 		return -1;
1425 	}
1426 
1427 	if (stat(sysfs_mod_name, &st) != 0) {
1428 		RTE_LOG(DEBUG, EAL, "Module %s not found! error %i (%s)\n",
1429 		        sysfs_mod_name, errno, strerror(errno));
1430 		return 0;
1431 	}
1432 
1433 	/* Module has been found */
1434 	return 1;
1435 }
1436