xref: /dpdk/lib/eal/linux/eal.c (revision 2773d39ffee46f66fd628cebdd401d89fce09f1f)
199a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
299a2dd95SBruce Richardson  * Copyright(c) 2010-2018 Intel Corporation.
399a2dd95SBruce Richardson  * Copyright(c) 2012-2014 6WIND S.A.
499a2dd95SBruce Richardson  */
599a2dd95SBruce Richardson 
672b452c5SDmitry Kozlyuk #include <ctype.h>
799a2dd95SBruce Richardson #include <stdio.h>
899a2dd95SBruce Richardson #include <stdlib.h>
999a2dd95SBruce Richardson #include <stdint.h>
1099a2dd95SBruce Richardson #include <string.h>
1199a2dd95SBruce Richardson #include <unistd.h>
1299a2dd95SBruce Richardson #include <pthread.h>
1399a2dd95SBruce Richardson #include <getopt.h>
1499a2dd95SBruce Richardson #include <sys/file.h>
1599a2dd95SBruce Richardson #include <dirent.h>
1699a2dd95SBruce Richardson #include <fcntl.h>
1799a2dd95SBruce Richardson #include <fnmatch.h>
1899a2dd95SBruce Richardson #include <stddef.h>
1999a2dd95SBruce Richardson #include <errno.h>
2099a2dd95SBruce Richardson #include <limits.h>
2199a2dd95SBruce Richardson #include <sys/mman.h>
2299a2dd95SBruce Richardson #include <sys/stat.h>
2399a2dd95SBruce Richardson #if defined(RTE_ARCH_X86)
2499a2dd95SBruce Richardson #include <sys/io.h>
2599a2dd95SBruce Richardson #endif
2699a2dd95SBruce Richardson #include <linux/version.h>
2799a2dd95SBruce Richardson 
2899a2dd95SBruce Richardson #include <rte_common.h>
2999a2dd95SBruce Richardson #include <rte_debug.h>
3099a2dd95SBruce Richardson #include <rte_memory.h>
3199a2dd95SBruce Richardson #include <rte_launch.h>
3299a2dd95SBruce Richardson #include <rte_eal.h>
332e2f0272SDavid Marchand #include <rte_eal_memconfig.h>
3499a2dd95SBruce Richardson #include <rte_errno.h>
3599a2dd95SBruce Richardson #include <rte_lcore.h>
3699a2dd95SBruce Richardson #include <rte_service_component.h>
3799a2dd95SBruce Richardson #include <rte_log.h>
3899a2dd95SBruce Richardson #include <rte_string_fns.h>
3999a2dd95SBruce Richardson #include <rte_cpuflags.h>
4099a2dd95SBruce Richardson #include <rte_bus.h>
4199a2dd95SBruce Richardson #include <rte_version.h>
4299a2dd95SBruce Richardson #include <malloc_heap.h>
4399a2dd95SBruce Richardson #include <rte_vfio.h>
4499a2dd95SBruce Richardson 
4599a2dd95SBruce Richardson #include <telemetry_internal.h>
4699a2dd95SBruce Richardson #include "eal_private.h"
4799a2dd95SBruce Richardson #include "eal_thread.h"
485bce9bedSMattias Rönnblom #include "eal_lcore_var.h"
4999a2dd95SBruce Richardson #include "eal_internal_cfg.h"
5099a2dd95SBruce Richardson #include "eal_filesystem.h"
5199a2dd95SBruce Richardson #include "eal_hugepages.h"
5299a2dd95SBruce Richardson #include "eal_memcfg.h"
5399a2dd95SBruce Richardson #include "eal_trace.h"
5499a2dd95SBruce Richardson #include "eal_options.h"
5599a2dd95SBruce Richardson #include "eal_vfio.h"
5699a2dd95SBruce Richardson #include "hotplug_mp.h"
5709ce4131SBruce Richardson #include "log_internal.h"
5899a2dd95SBruce Richardson 
5999a2dd95SBruce Richardson #define MEMSIZE_IF_NO_HUGE_PAGE (64ULL * 1024ULL * 1024ULL)
6099a2dd95SBruce Richardson 
6199a2dd95SBruce Richardson #define SOCKET_MEM_STRLEN (RTE_MAX_NUMA_NODES * 10)
6299a2dd95SBruce Richardson 
6399a2dd95SBruce Richardson #define KERNEL_IOMMU_GROUPS_PATH "/sys/kernel/iommu_groups"
6499a2dd95SBruce Richardson 
6599a2dd95SBruce Richardson /* define fd variable here, because file needs to be kept open for the
6699a2dd95SBruce Richardson  * duration of the program, as we hold a write lock on it in the primary proc */
6799a2dd95SBruce Richardson static int mem_cfg_fd = -1;
6899a2dd95SBruce Richardson 
6999a2dd95SBruce Richardson static struct flock wr_lock = {
7099a2dd95SBruce Richardson 		.l_type = F_WRLCK,
7199a2dd95SBruce Richardson 		.l_whence = SEEK_SET,
7299a2dd95SBruce Richardson 		.l_start = offsetof(struct rte_mem_config, memsegs),
7399a2dd95SBruce Richardson 		.l_len = RTE_SIZEOF_FIELD(struct rte_mem_config, memsegs),
7499a2dd95SBruce Richardson };
7599a2dd95SBruce Richardson 
7699a2dd95SBruce Richardson /* internal configuration (per-core) */
7799a2dd95SBruce Richardson struct lcore_config lcore_config[RTE_MAX_LCORE];
7899a2dd95SBruce Richardson 
7999a2dd95SBruce Richardson /* used by rte_rdtsc() */
8099a2dd95SBruce Richardson int rte_cycles_vmware_tsc_map;
8199a2dd95SBruce Richardson 
8299a2dd95SBruce Richardson 
8399a2dd95SBruce Richardson int
8499a2dd95SBruce Richardson eal_clean_runtime_dir(void)
8599a2dd95SBruce Richardson {
8699a2dd95SBruce Richardson 	const char *runtime_dir = rte_eal_get_runtime_dir();
8799a2dd95SBruce Richardson 	DIR *dir;
8899a2dd95SBruce Richardson 	struct dirent *dirent;
8999a2dd95SBruce Richardson 	int dir_fd, fd, lck_result;
9099a2dd95SBruce Richardson 	static const char * const filters[] = {
9199a2dd95SBruce Richardson 		"fbarray_*",
9299a2dd95SBruce Richardson 		"mp_socket_*"
9399a2dd95SBruce Richardson 	};
9499a2dd95SBruce Richardson 
9599a2dd95SBruce Richardson 	/* open directory */
9699a2dd95SBruce Richardson 	dir = opendir(runtime_dir);
9799a2dd95SBruce Richardson 	if (!dir) {
98ae67895bSDavid Marchand 		EAL_LOG(ERR, "Unable to open runtime directory %s",
9999a2dd95SBruce Richardson 				runtime_dir);
10099a2dd95SBruce Richardson 		goto error;
10199a2dd95SBruce Richardson 	}
10299a2dd95SBruce Richardson 	dir_fd = dirfd(dir);
10399a2dd95SBruce Richardson 
10499a2dd95SBruce Richardson 	/* lock the directory before doing anything, to avoid races */
10599a2dd95SBruce Richardson 	if (flock(dir_fd, LOCK_EX) < 0) {
106ae67895bSDavid Marchand 		EAL_LOG(ERR, "Unable to lock runtime directory %s",
10799a2dd95SBruce Richardson 			runtime_dir);
10899a2dd95SBruce Richardson 		goto error;
10999a2dd95SBruce Richardson 	}
11099a2dd95SBruce Richardson 
11199a2dd95SBruce Richardson 	dirent = readdir(dir);
11299a2dd95SBruce Richardson 	if (!dirent) {
113ae67895bSDavid Marchand 		EAL_LOG(ERR, "Unable to read runtime directory %s",
11499a2dd95SBruce Richardson 				runtime_dir);
11599a2dd95SBruce Richardson 		goto error;
11699a2dd95SBruce Richardson 	}
11799a2dd95SBruce Richardson 
11899a2dd95SBruce Richardson 	while (dirent != NULL) {
11999a2dd95SBruce Richardson 		unsigned int f_idx;
12099a2dd95SBruce Richardson 		bool skip = true;
12199a2dd95SBruce Richardson 
12299a2dd95SBruce Richardson 		/* skip files that don't match the patterns */
12399a2dd95SBruce Richardson 		for (f_idx = 0; f_idx < RTE_DIM(filters); f_idx++) {
12499a2dd95SBruce Richardson 			const char *filter = filters[f_idx];
12599a2dd95SBruce Richardson 
12699a2dd95SBruce Richardson 			if (fnmatch(filter, dirent->d_name, 0) == 0) {
12799a2dd95SBruce Richardson 				skip = false;
12899a2dd95SBruce Richardson 				break;
12999a2dd95SBruce Richardson 			}
13099a2dd95SBruce Richardson 		}
13199a2dd95SBruce Richardson 		if (skip) {
13299a2dd95SBruce Richardson 			dirent = readdir(dir);
13399a2dd95SBruce Richardson 			continue;
13499a2dd95SBruce Richardson 		}
13599a2dd95SBruce Richardson 
13699a2dd95SBruce Richardson 		/* try and lock the file */
13799a2dd95SBruce Richardson 		fd = openat(dir_fd, dirent->d_name, O_RDONLY);
13899a2dd95SBruce Richardson 
13999a2dd95SBruce Richardson 		/* skip to next file */
14099a2dd95SBruce Richardson 		if (fd == -1) {
14199a2dd95SBruce Richardson 			dirent = readdir(dir);
14299a2dd95SBruce Richardson 			continue;
14399a2dd95SBruce Richardson 		}
14499a2dd95SBruce Richardson 
14599a2dd95SBruce Richardson 		/* non-blocking lock */
14699a2dd95SBruce Richardson 		lck_result = flock(fd, LOCK_EX | LOCK_NB);
14799a2dd95SBruce Richardson 
14899a2dd95SBruce Richardson 		/* if lock succeeds, remove the file */
14999a2dd95SBruce Richardson 		if (lck_result != -1)
15099a2dd95SBruce Richardson 			unlinkat(dir_fd, dirent->d_name, 0);
15199a2dd95SBruce Richardson 		close(fd);
15299a2dd95SBruce Richardson 		dirent = readdir(dir);
15399a2dd95SBruce Richardson 	}
15499a2dd95SBruce Richardson 
15599a2dd95SBruce Richardson 	/* closedir closes dir_fd and drops the lock */
15699a2dd95SBruce Richardson 	closedir(dir);
15799a2dd95SBruce Richardson 	return 0;
15899a2dd95SBruce Richardson 
15999a2dd95SBruce Richardson error:
16099a2dd95SBruce Richardson 	if (dir)
16199a2dd95SBruce Richardson 		closedir(dir);
16299a2dd95SBruce Richardson 
163ae67895bSDavid Marchand 	EAL_LOG(ERR, "Error while clearing runtime dir: %s",
16499a2dd95SBruce Richardson 		strerror(errno));
16599a2dd95SBruce Richardson 
16699a2dd95SBruce Richardson 	return -1;
16799a2dd95SBruce Richardson }
16899a2dd95SBruce Richardson 
16999a2dd95SBruce Richardson 
17099a2dd95SBruce Richardson /* create memory configuration in shared/mmap memory. Take out
17199a2dd95SBruce Richardson  * a write lock on the memsegs, so we can auto-detect primary/secondary.
17299a2dd95SBruce Richardson  * This means we never close the file while running (auto-close on exit).
17399a2dd95SBruce Richardson  * We also don't lock the whole file, so that in future we can use read-locks
17499a2dd95SBruce Richardson  * on other parts, e.g. memzones, to detect if there are running secondary
17599a2dd95SBruce Richardson  * processes. */
17699a2dd95SBruce Richardson static int
17799a2dd95SBruce Richardson rte_eal_config_create(void)
17899a2dd95SBruce Richardson {
17999a2dd95SBruce Richardson 	struct rte_config *config = rte_eal_get_configuration();
18099a2dd95SBruce Richardson 	size_t page_sz = sysconf(_SC_PAGE_SIZE);
18199a2dd95SBruce Richardson 	size_t cfg_len = sizeof(*config->mem_config);
18299a2dd95SBruce Richardson 	size_t cfg_len_aligned = RTE_ALIGN(cfg_len, page_sz);
18399a2dd95SBruce Richardson 	void *rte_mem_cfg_addr, *mapped_mem_cfg_addr;
18499a2dd95SBruce Richardson 	int retval;
18599a2dd95SBruce Richardson 	const struct internal_config *internal_conf =
18699a2dd95SBruce Richardson 		eal_get_internal_configuration();
18799a2dd95SBruce Richardson 
18899a2dd95SBruce Richardson 	const char *pathname = eal_runtime_config_path();
18999a2dd95SBruce Richardson 
19099a2dd95SBruce Richardson 	if (internal_conf->no_shconf)
19199a2dd95SBruce Richardson 		return 0;
19299a2dd95SBruce Richardson 
19399a2dd95SBruce Richardson 	/* map the config before hugepage address so that we don't waste a page */
19499a2dd95SBruce Richardson 	if (internal_conf->base_virtaddr != 0)
19599a2dd95SBruce Richardson 		rte_mem_cfg_addr = (void *)
19699a2dd95SBruce Richardson 			RTE_ALIGN_FLOOR(internal_conf->base_virtaddr -
19799a2dd95SBruce Richardson 			sizeof(struct rte_mem_config), page_sz);
19899a2dd95SBruce Richardson 	else
19999a2dd95SBruce Richardson 		rte_mem_cfg_addr = NULL;
20099a2dd95SBruce Richardson 
20199a2dd95SBruce Richardson 	if (mem_cfg_fd < 0){
20299a2dd95SBruce Richardson 		mem_cfg_fd = open(pathname, O_RDWR | O_CREAT, 0600);
20399a2dd95SBruce Richardson 		if (mem_cfg_fd < 0) {
204ae67895bSDavid Marchand 			EAL_LOG(ERR, "Cannot open '%s' for rte_mem_config",
20599a2dd95SBruce Richardson 				pathname);
20699a2dd95SBruce Richardson 			return -1;
20799a2dd95SBruce Richardson 		}
20899a2dd95SBruce Richardson 	}
20999a2dd95SBruce Richardson 
21099a2dd95SBruce Richardson 	retval = ftruncate(mem_cfg_fd, cfg_len);
21199a2dd95SBruce Richardson 	if (retval < 0){
21299a2dd95SBruce Richardson 		close(mem_cfg_fd);
21399a2dd95SBruce Richardson 		mem_cfg_fd = -1;
214ae67895bSDavid Marchand 		EAL_LOG(ERR, "Cannot resize '%s' for rte_mem_config",
21599a2dd95SBruce Richardson 			pathname);
21699a2dd95SBruce Richardson 		return -1;
21799a2dd95SBruce Richardson 	}
21899a2dd95SBruce Richardson 
21999a2dd95SBruce Richardson 	retval = fcntl(mem_cfg_fd, F_SETLK, &wr_lock);
22099a2dd95SBruce Richardson 	if (retval < 0){
22199a2dd95SBruce Richardson 		close(mem_cfg_fd);
22299a2dd95SBruce Richardson 		mem_cfg_fd = -1;
223ae67895bSDavid Marchand 		EAL_LOG(ERR, "Cannot create lock on '%s'. Is another primary "
224ae67895bSDavid Marchand 			"process running?", pathname);
22599a2dd95SBruce Richardson 		return -1;
22699a2dd95SBruce Richardson 	}
22799a2dd95SBruce Richardson 
22899a2dd95SBruce Richardson 	/* reserve space for config */
22999a2dd95SBruce Richardson 	rte_mem_cfg_addr = eal_get_virtual_area(rte_mem_cfg_addr,
23099a2dd95SBruce Richardson 			&cfg_len_aligned, page_sz, 0, 0);
23199a2dd95SBruce Richardson 	if (rte_mem_cfg_addr == NULL) {
232ae67895bSDavid Marchand 		EAL_LOG(ERR, "Cannot mmap memory for rte_config");
23399a2dd95SBruce Richardson 		close(mem_cfg_fd);
23499a2dd95SBruce Richardson 		mem_cfg_fd = -1;
23599a2dd95SBruce Richardson 		return -1;
23699a2dd95SBruce Richardson 	}
23799a2dd95SBruce Richardson 
23899a2dd95SBruce Richardson 	/* remap the actual file into the space we've just reserved */
23999a2dd95SBruce Richardson 	mapped_mem_cfg_addr = mmap(rte_mem_cfg_addr,
24099a2dd95SBruce Richardson 			cfg_len_aligned, PROT_READ | PROT_WRITE,
24199a2dd95SBruce Richardson 			MAP_SHARED | MAP_FIXED, mem_cfg_fd, 0);
24299a2dd95SBruce Richardson 	if (mapped_mem_cfg_addr == MAP_FAILED) {
24399a2dd95SBruce Richardson 		munmap(rte_mem_cfg_addr, cfg_len);
24499a2dd95SBruce Richardson 		close(mem_cfg_fd);
24599a2dd95SBruce Richardson 		mem_cfg_fd = -1;
246ae67895bSDavid Marchand 		EAL_LOG(ERR, "Cannot remap memory for rte_config");
24799a2dd95SBruce Richardson 		return -1;
24899a2dd95SBruce Richardson 	}
24999a2dd95SBruce Richardson 
25099a2dd95SBruce Richardson 	memcpy(rte_mem_cfg_addr, config->mem_config, sizeof(struct rte_mem_config));
25199a2dd95SBruce Richardson 	config->mem_config = rte_mem_cfg_addr;
25299a2dd95SBruce Richardson 
25399a2dd95SBruce Richardson 	/* store address of the config in the config itself so that secondary
25499a2dd95SBruce Richardson 	 * processes could later map the config into this exact location
25599a2dd95SBruce Richardson 	 */
25699a2dd95SBruce Richardson 	config->mem_config->mem_cfg_addr = (uintptr_t) rte_mem_cfg_addr;
25799a2dd95SBruce Richardson 	config->mem_config->dma_maskbits = 0;
25899a2dd95SBruce Richardson 
25999a2dd95SBruce Richardson 	return 0;
26099a2dd95SBruce Richardson }
26199a2dd95SBruce Richardson 
26299a2dd95SBruce Richardson /* attach to an existing shared memory config */
26399a2dd95SBruce Richardson static int
26499a2dd95SBruce Richardson rte_eal_config_attach(void)
26599a2dd95SBruce Richardson {
26699a2dd95SBruce Richardson 	struct rte_config *config = rte_eal_get_configuration();
26799a2dd95SBruce Richardson 	struct rte_mem_config *mem_config;
26899a2dd95SBruce Richardson 	const struct internal_config *internal_conf =
26999a2dd95SBruce Richardson 		eal_get_internal_configuration();
27099a2dd95SBruce Richardson 
27199a2dd95SBruce Richardson 	const char *pathname = eal_runtime_config_path();
27299a2dd95SBruce Richardson 
27399a2dd95SBruce Richardson 	if (internal_conf->no_shconf)
27499a2dd95SBruce Richardson 		return 0;
27599a2dd95SBruce Richardson 
27699a2dd95SBruce Richardson 	if (mem_cfg_fd < 0){
27799a2dd95SBruce Richardson 		mem_cfg_fd = open(pathname, O_RDWR);
27899a2dd95SBruce Richardson 		if (mem_cfg_fd < 0) {
279ae67895bSDavid Marchand 			EAL_LOG(ERR, "Cannot open '%s' for rte_mem_config",
28099a2dd95SBruce Richardson 				pathname);
28199a2dd95SBruce Richardson 			return -1;
28299a2dd95SBruce Richardson 		}
28399a2dd95SBruce Richardson 	}
28499a2dd95SBruce Richardson 
28599a2dd95SBruce Richardson 	/* map it as read-only first */
28699a2dd95SBruce Richardson 	mem_config = (struct rte_mem_config *) mmap(NULL, sizeof(*mem_config),
28799a2dd95SBruce Richardson 			PROT_READ, MAP_SHARED, mem_cfg_fd, 0);
28899a2dd95SBruce Richardson 	if (mem_config == MAP_FAILED) {
28999a2dd95SBruce Richardson 		close(mem_cfg_fd);
29099a2dd95SBruce Richardson 		mem_cfg_fd = -1;
291ae67895bSDavid Marchand 		EAL_LOG(ERR, "Cannot mmap memory for rte_config! error %i (%s)",
29299a2dd95SBruce Richardson 			errno, strerror(errno));
29399a2dd95SBruce Richardson 		return -1;
29499a2dd95SBruce Richardson 	}
29599a2dd95SBruce Richardson 
29699a2dd95SBruce Richardson 	config->mem_config = mem_config;
29799a2dd95SBruce Richardson 
29899a2dd95SBruce Richardson 	return 0;
29999a2dd95SBruce Richardson }
30099a2dd95SBruce Richardson 
30199a2dd95SBruce Richardson /* reattach the shared config at exact memory location primary process has it */
30299a2dd95SBruce Richardson static int
30399a2dd95SBruce Richardson rte_eal_config_reattach(void)
30499a2dd95SBruce Richardson {
30599a2dd95SBruce Richardson 	struct rte_config *config = rte_eal_get_configuration();
30699a2dd95SBruce Richardson 	struct rte_mem_config *mem_config;
30799a2dd95SBruce Richardson 	void *rte_mem_cfg_addr;
30899a2dd95SBruce Richardson 	const struct internal_config *internal_conf =
30999a2dd95SBruce Richardson 		eal_get_internal_configuration();
31099a2dd95SBruce Richardson 
31199a2dd95SBruce Richardson 	if (internal_conf->no_shconf)
31299a2dd95SBruce Richardson 		return 0;
31399a2dd95SBruce Richardson 
31499a2dd95SBruce Richardson 	/* save the address primary process has mapped shared config to */
31599a2dd95SBruce Richardson 	rte_mem_cfg_addr =
31699a2dd95SBruce Richardson 		(void *) (uintptr_t) config->mem_config->mem_cfg_addr;
31799a2dd95SBruce Richardson 
31899a2dd95SBruce Richardson 	/* unmap original config */
31999a2dd95SBruce Richardson 	munmap(config->mem_config, sizeof(struct rte_mem_config));
32099a2dd95SBruce Richardson 
32199a2dd95SBruce Richardson 	/* remap the config at proper address */
32299a2dd95SBruce Richardson 	mem_config = (struct rte_mem_config *) mmap(rte_mem_cfg_addr,
32399a2dd95SBruce Richardson 			sizeof(*mem_config), PROT_READ | PROT_WRITE, MAP_SHARED,
32499a2dd95SBruce Richardson 			mem_cfg_fd, 0);
32599a2dd95SBruce Richardson 
32699a2dd95SBruce Richardson 	close(mem_cfg_fd);
32799a2dd95SBruce Richardson 	mem_cfg_fd = -1;
32899a2dd95SBruce Richardson 
32999a2dd95SBruce Richardson 	if (mem_config == MAP_FAILED || mem_config != rte_mem_cfg_addr) {
33099a2dd95SBruce Richardson 		if (mem_config != MAP_FAILED) {
33199a2dd95SBruce Richardson 			/* errno is stale, don't use */
332ae67895bSDavid Marchand 			EAL_LOG(ERR, "Cannot mmap memory for rte_config at [%p], got [%p]"
33399a2dd95SBruce Richardson 				" - please use '--" OPT_BASE_VIRTADDR
334ae67895bSDavid Marchand 				"' option", rte_mem_cfg_addr, mem_config);
33599a2dd95SBruce Richardson 			munmap(mem_config, sizeof(struct rte_mem_config));
33699a2dd95SBruce Richardson 			return -1;
33799a2dd95SBruce Richardson 		}
338ae67895bSDavid Marchand 		EAL_LOG(ERR, "Cannot mmap memory for rte_config! error %i (%s)",
33999a2dd95SBruce Richardson 			errno, strerror(errno));
34099a2dd95SBruce Richardson 		return -1;
34199a2dd95SBruce Richardson 	}
34299a2dd95SBruce Richardson 
34399a2dd95SBruce Richardson 	config->mem_config = mem_config;
34499a2dd95SBruce Richardson 
34599a2dd95SBruce Richardson 	return 0;
34699a2dd95SBruce Richardson }
34799a2dd95SBruce Richardson 
34899a2dd95SBruce Richardson /* Detect if we are a primary or a secondary process */
34999a2dd95SBruce Richardson enum rte_proc_type_t
35099a2dd95SBruce Richardson eal_proc_type_detect(void)
35199a2dd95SBruce Richardson {
35299a2dd95SBruce Richardson 	enum rte_proc_type_t ptype = RTE_PROC_PRIMARY;
35399a2dd95SBruce Richardson 	const char *pathname = eal_runtime_config_path();
35499a2dd95SBruce Richardson 	const struct internal_config *internal_conf =
35599a2dd95SBruce Richardson 		eal_get_internal_configuration();
35699a2dd95SBruce Richardson 
35799a2dd95SBruce Richardson 	/* if there no shared config, there can be no secondary processes */
35899a2dd95SBruce Richardson 	if (!internal_conf->no_shconf) {
35999a2dd95SBruce Richardson 		/* if we can open the file but not get a write-lock we are a
36099a2dd95SBruce Richardson 		 * secondary process. NOTE: if we get a file handle back, we
36199a2dd95SBruce Richardson 		 * keep that open and don't close it to prevent a race condition
36299a2dd95SBruce Richardson 		 * between multiple opens.
36399a2dd95SBruce Richardson 		 */
36499a2dd95SBruce Richardson 		if (((mem_cfg_fd = open(pathname, O_RDWR)) >= 0) &&
36599a2dd95SBruce Richardson 				(fcntl(mem_cfg_fd, F_SETLK, &wr_lock) < 0))
36699a2dd95SBruce Richardson 			ptype = RTE_PROC_SECONDARY;
36799a2dd95SBruce Richardson 	}
36899a2dd95SBruce Richardson 
369ae67895bSDavid Marchand 	EAL_LOG(INFO, "Auto-detected process type: %s",
37099a2dd95SBruce Richardson 			ptype == RTE_PROC_PRIMARY ? "PRIMARY" : "SECONDARY");
37199a2dd95SBruce Richardson 
37299a2dd95SBruce Richardson 	return ptype;
37399a2dd95SBruce Richardson }
37499a2dd95SBruce Richardson 
37599a2dd95SBruce Richardson /* Sets up rte_config structure with the pointer to shared memory config.*/
37699a2dd95SBruce Richardson static int
37799a2dd95SBruce Richardson rte_config_init(void)
37899a2dd95SBruce Richardson {
37999a2dd95SBruce Richardson 	struct rte_config *config = rte_eal_get_configuration();
38099a2dd95SBruce Richardson 	const struct internal_config *internal_conf =
38199a2dd95SBruce Richardson 		eal_get_internal_configuration();
38299a2dd95SBruce Richardson 
38399a2dd95SBruce Richardson 	config->process_type = internal_conf->process_type;
38499a2dd95SBruce Richardson 
38599a2dd95SBruce Richardson 	switch (config->process_type) {
38699a2dd95SBruce Richardson 	case RTE_PROC_PRIMARY:
38799a2dd95SBruce Richardson 		if (rte_eal_config_create() < 0)
38899a2dd95SBruce Richardson 			return -1;
38999a2dd95SBruce Richardson 		eal_mcfg_update_from_internal();
39099a2dd95SBruce Richardson 		break;
39199a2dd95SBruce Richardson 	case RTE_PROC_SECONDARY:
39299a2dd95SBruce Richardson 		if (rte_eal_config_attach() < 0)
39399a2dd95SBruce Richardson 			return -1;
39499a2dd95SBruce Richardson 		eal_mcfg_wait_complete();
39599a2dd95SBruce Richardson 		if (eal_mcfg_check_version() < 0) {
396ae67895bSDavid Marchand 			EAL_LOG(ERR, "Primary and secondary process DPDK version mismatch");
39799a2dd95SBruce Richardson 			return -1;
39899a2dd95SBruce Richardson 		}
39999a2dd95SBruce Richardson 		if (rte_eal_config_reattach() < 0)
40099a2dd95SBruce Richardson 			return -1;
40199a2dd95SBruce Richardson 		if (!__rte_mp_enable()) {
402ae67895bSDavid Marchand 			EAL_LOG(ERR, "Primary process refused secondary attachment");
40399a2dd95SBruce Richardson 			return -1;
40499a2dd95SBruce Richardson 		}
40599a2dd95SBruce Richardson 		eal_mcfg_update_internal();
40699a2dd95SBruce Richardson 		break;
40799a2dd95SBruce Richardson 	case RTE_PROC_AUTO:
40899a2dd95SBruce Richardson 	case RTE_PROC_INVALID:
409ae67895bSDavid Marchand 		EAL_LOG(ERR, "Invalid process type %d",
41099a2dd95SBruce Richardson 			config->process_type);
41199a2dd95SBruce Richardson 		return -1;
41299a2dd95SBruce Richardson 	}
41399a2dd95SBruce Richardson 
41499a2dd95SBruce Richardson 	return 0;
41599a2dd95SBruce Richardson }
41699a2dd95SBruce Richardson 
41799a2dd95SBruce Richardson /* Unlocks hugepage directories that were locked by eal_hugepage_info_init */
41899a2dd95SBruce Richardson static void
41999a2dd95SBruce Richardson eal_hugedirs_unlock(void)
42099a2dd95SBruce Richardson {
42199a2dd95SBruce Richardson 	int i;
42299a2dd95SBruce Richardson 	struct internal_config *internal_conf =
42399a2dd95SBruce Richardson 		eal_get_internal_configuration();
42499a2dd95SBruce Richardson 
42599a2dd95SBruce Richardson 	for (i = 0; i < MAX_HUGEPAGE_SIZES; i++)
42699a2dd95SBruce Richardson 	{
42799a2dd95SBruce Richardson 		/* skip uninitialized */
42899a2dd95SBruce Richardson 		if (internal_conf->hugepage_info[i].lock_descriptor < 0)
42999a2dd95SBruce Richardson 			continue;
43099a2dd95SBruce Richardson 		/* unlock hugepage file */
43199a2dd95SBruce Richardson 		flock(internal_conf->hugepage_info[i].lock_descriptor, LOCK_UN);
43299a2dd95SBruce Richardson 		close(internal_conf->hugepage_info[i].lock_descriptor);
43399a2dd95SBruce Richardson 		/* reset the field */
43499a2dd95SBruce Richardson 		internal_conf->hugepage_info[i].lock_descriptor = -1;
43599a2dd95SBruce Richardson 	}
43699a2dd95SBruce Richardson }
43799a2dd95SBruce Richardson 
43899a2dd95SBruce Richardson /* display usage */
43999a2dd95SBruce Richardson static void
44099a2dd95SBruce Richardson eal_usage(const char *prgname)
44199a2dd95SBruce Richardson {
44299a2dd95SBruce Richardson 	rte_usage_hook_t hook = eal_get_application_usage_hook();
44399a2dd95SBruce Richardson 
44499a2dd95SBruce Richardson 	printf("\nUsage: %s ", prgname);
44599a2dd95SBruce Richardson 	eal_common_usage();
44699a2dd95SBruce Richardson 	printf("EAL Linux options:\n"
44799a2dd95SBruce Richardson 	       "  --"OPT_SOCKET_MEM"        Memory to allocate on sockets (comma separated values)\n"
44899a2dd95SBruce Richardson 	       "  --"OPT_SOCKET_LIMIT"      Limit memory allocation on sockets (comma separated values)\n"
44999a2dd95SBruce Richardson 	       "  --"OPT_HUGE_DIR"          Directory where hugetlbfs is mounted\n"
45099a2dd95SBruce Richardson 	       "  --"OPT_FILE_PREFIX"       Prefix for hugepage filenames\n"
45199a2dd95SBruce Richardson 	       "  --"OPT_CREATE_UIO_DEV"    Create /dev/uioX (usually done by hotplug)\n"
45299a2dd95SBruce Richardson 	       "  --"OPT_VFIO_INTR"         Interrupt mode for VFIO (legacy|msi|msix)\n"
45399a2dd95SBruce Richardson 	       "  --"OPT_VFIO_VF_TOKEN"     VF token (UUID) shared between SR-IOV PF and VFs\n"
45499a2dd95SBruce Richardson 	       "  --"OPT_LEGACY_MEM"        Legacy memory mode (no dynamic allocation, contiguous segments)\n"
45599a2dd95SBruce Richardson 	       "  --"OPT_SINGLE_FILE_SEGMENTS" Put all hugepage memory in single files\n"
45699a2dd95SBruce Richardson 	       "  --"OPT_MATCH_ALLOCATIONS" Free hugepages exactly as allocated\n"
45742fbb8e8SDon Wallwork 	       "  --"OPT_HUGE_WORKER_STACK"[=size]\n"
45842fbb8e8SDon Wallwork 	       "                      Allocate worker thread stacks from hugepage memory.\n"
45942fbb8e8SDon Wallwork 	       "                      Size is in units of kbytes and defaults to system\n"
46042fbb8e8SDon Wallwork 	       "                      thread stack size if not specified.\n"
46199a2dd95SBruce Richardson 	       "\n");
46299a2dd95SBruce Richardson 	/* Allow the application to print its usage message too if hook is set */
46399a2dd95SBruce Richardson 	if (hook) {
46499a2dd95SBruce Richardson 		printf("===== Application Usage =====\n\n");
46599a2dd95SBruce Richardson 		(hook)(prgname);
46699a2dd95SBruce Richardson 	}
46799a2dd95SBruce Richardson }
46899a2dd95SBruce Richardson 
46999a2dd95SBruce Richardson static int
47099a2dd95SBruce Richardson eal_parse_socket_arg(char *strval, volatile uint64_t *socket_arg)
47199a2dd95SBruce Richardson {
47299a2dd95SBruce Richardson 	char * arg[RTE_MAX_NUMA_NODES];
47399a2dd95SBruce Richardson 	char *end;
47499a2dd95SBruce Richardson 	int arg_num, i, len;
47599a2dd95SBruce Richardson 
47699a2dd95SBruce Richardson 	len = strnlen(strval, SOCKET_MEM_STRLEN);
47799a2dd95SBruce Richardson 	if (len == SOCKET_MEM_STRLEN) {
478ae67895bSDavid Marchand 		EAL_LOG(ERR, "--socket-mem is too long");
47999a2dd95SBruce Richardson 		return -1;
48099a2dd95SBruce Richardson 	}
48199a2dd95SBruce Richardson 
48299a2dd95SBruce Richardson 	/* all other error cases will be caught later */
48399a2dd95SBruce Richardson 	if (!isdigit(strval[len-1]))
48499a2dd95SBruce Richardson 		return -1;
48599a2dd95SBruce Richardson 
48699a2dd95SBruce Richardson 	/* split the optarg into separate socket values */
48799a2dd95SBruce Richardson 	arg_num = rte_strsplit(strval, len,
48899a2dd95SBruce Richardson 			arg, RTE_MAX_NUMA_NODES, ',');
48999a2dd95SBruce Richardson 
49099a2dd95SBruce Richardson 	/* if split failed, or 0 arguments */
49199a2dd95SBruce Richardson 	if (arg_num <= 0)
49299a2dd95SBruce Richardson 		return -1;
49399a2dd95SBruce Richardson 
49499a2dd95SBruce Richardson 	/* parse each defined socket option */
49599a2dd95SBruce Richardson 	errno = 0;
49699a2dd95SBruce Richardson 	for (i = 0; i < arg_num; i++) {
49799a2dd95SBruce Richardson 		uint64_t val;
49899a2dd95SBruce Richardson 		end = NULL;
49999a2dd95SBruce Richardson 		val = strtoull(arg[i], &end, 10);
50099a2dd95SBruce Richardson 
50199a2dd95SBruce Richardson 		/* check for invalid input */
50299a2dd95SBruce Richardson 		if ((errno != 0)  ||
50399a2dd95SBruce Richardson 				(arg[i][0] == '\0') || (end == NULL) || (*end != '\0'))
50499a2dd95SBruce Richardson 			return -1;
50599a2dd95SBruce Richardson 		val <<= 20;
50699a2dd95SBruce Richardson 		socket_arg[i] = val;
50799a2dd95SBruce Richardson 	}
50899a2dd95SBruce Richardson 
50999a2dd95SBruce Richardson 	return 0;
51099a2dd95SBruce Richardson }
51199a2dd95SBruce Richardson 
51299a2dd95SBruce Richardson static int
51399a2dd95SBruce Richardson eal_parse_vfio_intr(const char *mode)
51499a2dd95SBruce Richardson {
51599a2dd95SBruce Richardson 	struct internal_config *internal_conf =
51699a2dd95SBruce Richardson 		eal_get_internal_configuration();
51799a2dd95SBruce Richardson 	unsigned i;
51899a2dd95SBruce Richardson 	static struct {
51999a2dd95SBruce Richardson 		const char *name;
52099a2dd95SBruce Richardson 		enum rte_intr_mode value;
52199a2dd95SBruce Richardson 	} map[] = {
52299a2dd95SBruce Richardson 		{ "legacy", RTE_INTR_MODE_LEGACY },
52399a2dd95SBruce Richardson 		{ "msi", RTE_INTR_MODE_MSI },
52499a2dd95SBruce Richardson 		{ "msix", RTE_INTR_MODE_MSIX },
52599a2dd95SBruce Richardson 	};
52699a2dd95SBruce Richardson 
52799a2dd95SBruce Richardson 	for (i = 0; i < RTE_DIM(map); i++) {
52899a2dd95SBruce Richardson 		if (!strcmp(mode, map[i].name)) {
52999a2dd95SBruce Richardson 			internal_conf->vfio_intr_mode = map[i].value;
53099a2dd95SBruce Richardson 			return 0;
53199a2dd95SBruce Richardson 		}
53299a2dd95SBruce Richardson 	}
53399a2dd95SBruce Richardson 	return -1;
53499a2dd95SBruce Richardson }
53599a2dd95SBruce Richardson 
53699a2dd95SBruce Richardson static int
53799a2dd95SBruce Richardson eal_parse_vfio_vf_token(const char *vf_token)
53899a2dd95SBruce Richardson {
53999a2dd95SBruce Richardson 	struct internal_config *cfg = eal_get_internal_configuration();
54099a2dd95SBruce Richardson 	rte_uuid_t uuid;
54199a2dd95SBruce Richardson 
54299a2dd95SBruce Richardson 	if (!rte_uuid_parse(vf_token, uuid)) {
54399a2dd95SBruce Richardson 		rte_uuid_copy(cfg->vfio_vf_token, uuid);
54499a2dd95SBruce Richardson 		return 0;
54599a2dd95SBruce Richardson 	}
54699a2dd95SBruce Richardson 
54799a2dd95SBruce Richardson 	return -1;
54899a2dd95SBruce Richardson }
54999a2dd95SBruce Richardson 
55042fbb8e8SDon Wallwork static int
55142fbb8e8SDon Wallwork eal_parse_huge_worker_stack(const char *arg)
55242fbb8e8SDon Wallwork {
55342fbb8e8SDon Wallwork 	struct internal_config *cfg = eal_get_internal_configuration();
55442fbb8e8SDon Wallwork 
55542fbb8e8SDon Wallwork 	if (arg == NULL || arg[0] == '\0') {
55642fbb8e8SDon Wallwork 		pthread_attr_t attr;
55742fbb8e8SDon Wallwork 		int ret;
55842fbb8e8SDon Wallwork 
55942fbb8e8SDon Wallwork 		if (pthread_attr_init(&attr) != 0) {
560ae67895bSDavid Marchand 			EAL_LOG(ERR, "Could not retrieve default stack size");
56142fbb8e8SDon Wallwork 			return -1;
56242fbb8e8SDon Wallwork 		}
56342fbb8e8SDon Wallwork 		ret = pthread_attr_getstacksize(&attr, &cfg->huge_worker_stack_size);
56442fbb8e8SDon Wallwork 		pthread_attr_destroy(&attr);
56542fbb8e8SDon Wallwork 		if (ret != 0) {
566ae67895bSDavid Marchand 			EAL_LOG(ERR, "Could not retrieve default stack size");
56742fbb8e8SDon Wallwork 			return -1;
56842fbb8e8SDon Wallwork 		}
56942fbb8e8SDon Wallwork 	} else {
57042fbb8e8SDon Wallwork 		unsigned long stack_size;
57142fbb8e8SDon Wallwork 		char *end;
57242fbb8e8SDon Wallwork 
57342fbb8e8SDon Wallwork 		errno = 0;
57442fbb8e8SDon Wallwork 		stack_size = strtoul(arg, &end, 10);
57542fbb8e8SDon Wallwork 		if (errno || end == NULL || stack_size == 0 ||
57642fbb8e8SDon Wallwork 				stack_size >= (size_t)-1 / 1024)
57742fbb8e8SDon Wallwork 			return -1;
57842fbb8e8SDon Wallwork 
57942fbb8e8SDon Wallwork 		cfg->huge_worker_stack_size = stack_size * 1024;
58042fbb8e8SDon Wallwork 	}
58142fbb8e8SDon Wallwork 
582ae67895bSDavid Marchand 	EAL_LOG(DEBUG, "Each worker thread will use %zu kB of DPDK memory as stack",
58342fbb8e8SDon Wallwork 		cfg->huge_worker_stack_size / 1024);
58442fbb8e8SDon Wallwork 	return 0;
58542fbb8e8SDon Wallwork }
58642fbb8e8SDon Wallwork 
58799a2dd95SBruce Richardson /* Parse the argument given in the command line of the application */
58899a2dd95SBruce Richardson static int
58999a2dd95SBruce Richardson eal_parse_args(int argc, char **argv)
59099a2dd95SBruce Richardson {
59199a2dd95SBruce Richardson 	int opt, ret;
59299a2dd95SBruce Richardson 	char **argvopt;
59399a2dd95SBruce Richardson 	int option_index;
59499a2dd95SBruce Richardson 	char *prgname = argv[0];
59599a2dd95SBruce Richardson 	const int old_optind = optind;
59699a2dd95SBruce Richardson 	const int old_optopt = optopt;
59799a2dd95SBruce Richardson 	char * const old_optarg = optarg;
59899a2dd95SBruce Richardson 	struct internal_config *internal_conf =
59999a2dd95SBruce Richardson 		eal_get_internal_configuration();
60099a2dd95SBruce Richardson 
60199a2dd95SBruce Richardson 	argvopt = argv;
60299a2dd95SBruce Richardson 	optind = 1;
60399a2dd95SBruce Richardson 
60499a2dd95SBruce Richardson 	while ((opt = getopt_long(argc, argvopt, eal_short_options,
60599a2dd95SBruce Richardson 				  eal_long_options, &option_index)) != EOF) {
60699a2dd95SBruce Richardson 
60799a2dd95SBruce Richardson 		/* getopt didn't recognise the option */
60899a2dd95SBruce Richardson 		if (opt == '?') {
60999a2dd95SBruce Richardson 			eal_usage(prgname);
61099a2dd95SBruce Richardson 			ret = -1;
61199a2dd95SBruce Richardson 			goto out;
61299a2dd95SBruce Richardson 		}
61399a2dd95SBruce Richardson 
6149a4276f9SDavid Marchand 		/* eal_parse_log_options() already handled this option */
6159a4276f9SDavid Marchand 		if (eal_option_is_log(opt))
61699a2dd95SBruce Richardson 			continue;
61799a2dd95SBruce Richardson 
61899a2dd95SBruce Richardson 		ret = eal_parse_common_option(opt, optarg, internal_conf);
61999a2dd95SBruce Richardson 		/* common parser is not happy */
62099a2dd95SBruce Richardson 		if (ret < 0) {
62199a2dd95SBruce Richardson 			eal_usage(prgname);
62299a2dd95SBruce Richardson 			ret = -1;
62399a2dd95SBruce Richardson 			goto out;
62499a2dd95SBruce Richardson 		}
62599a2dd95SBruce Richardson 		/* common parser handled this option */
62699a2dd95SBruce Richardson 		if (ret == 0)
62799a2dd95SBruce Richardson 			continue;
62899a2dd95SBruce Richardson 
62999a2dd95SBruce Richardson 		switch (opt) {
630df60837cSThomas Monjalon 		case OPT_HELP_NUM:
63199a2dd95SBruce Richardson 			eal_usage(prgname);
63299a2dd95SBruce Richardson 			exit(EXIT_SUCCESS);
63399a2dd95SBruce Richardson 
63499a2dd95SBruce Richardson 		case OPT_HUGE_DIR_NUM:
63599a2dd95SBruce Richardson 		{
63699a2dd95SBruce Richardson 			char *hdir = strdup(optarg);
63799a2dd95SBruce Richardson 			if (hdir == NULL)
638ae67895bSDavid Marchand 				EAL_LOG(ERR, "Could not store hugepage directory");
63999a2dd95SBruce Richardson 			else {
64099a2dd95SBruce Richardson 				/* free old hugepage dir */
64199a2dd95SBruce Richardson 				free(internal_conf->hugepage_dir);
64299a2dd95SBruce Richardson 				internal_conf->hugepage_dir = hdir;
64399a2dd95SBruce Richardson 			}
64499a2dd95SBruce Richardson 			break;
64599a2dd95SBruce Richardson 		}
64699a2dd95SBruce Richardson 		case OPT_FILE_PREFIX_NUM:
64799a2dd95SBruce Richardson 		{
64899a2dd95SBruce Richardson 			char *prefix = strdup(optarg);
64999a2dd95SBruce Richardson 			if (prefix == NULL)
650ae67895bSDavid Marchand 				EAL_LOG(ERR, "Could not store file prefix");
65199a2dd95SBruce Richardson 			else {
65299a2dd95SBruce Richardson 				/* free old prefix */
65399a2dd95SBruce Richardson 				free(internal_conf->hugefile_prefix);
65499a2dd95SBruce Richardson 				internal_conf->hugefile_prefix = prefix;
65599a2dd95SBruce Richardson 			}
65699a2dd95SBruce Richardson 			break;
65799a2dd95SBruce Richardson 		}
65899a2dd95SBruce Richardson 		case OPT_SOCKET_MEM_NUM:
65999a2dd95SBruce Richardson 			if (eal_parse_socket_arg(optarg,
66099a2dd95SBruce Richardson 					internal_conf->socket_mem) < 0) {
661ae67895bSDavid Marchand 				EAL_LOG(ERR, "invalid parameters for --"
662ae67895bSDavid Marchand 						OPT_SOCKET_MEM);
66399a2dd95SBruce Richardson 				eal_usage(prgname);
66499a2dd95SBruce Richardson 				ret = -1;
66599a2dd95SBruce Richardson 				goto out;
66699a2dd95SBruce Richardson 			}
66799a2dd95SBruce Richardson 			internal_conf->force_sockets = 1;
66899a2dd95SBruce Richardson 			break;
66999a2dd95SBruce Richardson 
67099a2dd95SBruce Richardson 		case OPT_SOCKET_LIMIT_NUM:
67199a2dd95SBruce Richardson 			if (eal_parse_socket_arg(optarg,
67299a2dd95SBruce Richardson 					internal_conf->socket_limit) < 0) {
673ae67895bSDavid Marchand 				EAL_LOG(ERR, "invalid parameters for --"
674ae67895bSDavid Marchand 						OPT_SOCKET_LIMIT);
67599a2dd95SBruce Richardson 				eal_usage(prgname);
67699a2dd95SBruce Richardson 				ret = -1;
67799a2dd95SBruce Richardson 				goto out;
67899a2dd95SBruce Richardson 			}
67999a2dd95SBruce Richardson 			internal_conf->force_socket_limits = 1;
68099a2dd95SBruce Richardson 			break;
68199a2dd95SBruce Richardson 
68299a2dd95SBruce Richardson 		case OPT_VFIO_INTR_NUM:
68399a2dd95SBruce Richardson 			if (eal_parse_vfio_intr(optarg) < 0) {
684ae67895bSDavid Marchand 				EAL_LOG(ERR, "invalid parameters for --"
685ae67895bSDavid Marchand 						OPT_VFIO_INTR);
68699a2dd95SBruce Richardson 				eal_usage(prgname);
68799a2dd95SBruce Richardson 				ret = -1;
68899a2dd95SBruce Richardson 				goto out;
68999a2dd95SBruce Richardson 			}
69099a2dd95SBruce Richardson 			break;
69199a2dd95SBruce Richardson 
69299a2dd95SBruce Richardson 		case OPT_VFIO_VF_TOKEN_NUM:
69399a2dd95SBruce Richardson 			if (eal_parse_vfio_vf_token(optarg) < 0) {
694ae67895bSDavid Marchand 				EAL_LOG(ERR, "invalid parameters for --"
695ae67895bSDavid Marchand 						OPT_VFIO_VF_TOKEN);
69699a2dd95SBruce Richardson 				eal_usage(prgname);
69799a2dd95SBruce Richardson 				ret = -1;
69899a2dd95SBruce Richardson 				goto out;
69999a2dd95SBruce Richardson 			}
70099a2dd95SBruce Richardson 			break;
70199a2dd95SBruce Richardson 
70299a2dd95SBruce Richardson 		case OPT_CREATE_UIO_DEV_NUM:
70399a2dd95SBruce Richardson 			internal_conf->create_uio_dev = 1;
70499a2dd95SBruce Richardson 			break;
70599a2dd95SBruce Richardson 
70699a2dd95SBruce Richardson 		case OPT_MBUF_POOL_OPS_NAME_NUM:
70799a2dd95SBruce Richardson 		{
70899a2dd95SBruce Richardson 			char *ops_name = strdup(optarg);
70999a2dd95SBruce Richardson 			if (ops_name == NULL)
710ae67895bSDavid Marchand 				EAL_LOG(ERR, "Could not store mbuf pool ops name");
71199a2dd95SBruce Richardson 			else {
71299a2dd95SBruce Richardson 				/* free old ops name */
71399a2dd95SBruce Richardson 				free(internal_conf->user_mbuf_pool_ops_name);
71499a2dd95SBruce Richardson 
71599a2dd95SBruce Richardson 				internal_conf->user_mbuf_pool_ops_name =
71699a2dd95SBruce Richardson 						ops_name;
71799a2dd95SBruce Richardson 			}
71899a2dd95SBruce Richardson 			break;
71999a2dd95SBruce Richardson 		}
72099a2dd95SBruce Richardson 		case OPT_MATCH_ALLOCATIONS_NUM:
72199a2dd95SBruce Richardson 			internal_conf->match_allocations = 1;
72299a2dd95SBruce Richardson 			break;
72399a2dd95SBruce Richardson 
72442fbb8e8SDon Wallwork 		case OPT_HUGE_WORKER_STACK_NUM:
72542fbb8e8SDon Wallwork 			if (eal_parse_huge_worker_stack(optarg) < 0) {
726ae67895bSDavid Marchand 				EAL_LOG(ERR, "invalid parameter for --"
727ae67895bSDavid Marchand 					OPT_HUGE_WORKER_STACK);
72842fbb8e8SDon Wallwork 				eal_usage(prgname);
72942fbb8e8SDon Wallwork 				ret = -1;
73042fbb8e8SDon Wallwork 				goto out;
73142fbb8e8SDon Wallwork 			}
73242fbb8e8SDon Wallwork 			break;
73342fbb8e8SDon Wallwork 
73499a2dd95SBruce Richardson 		default:
73599a2dd95SBruce Richardson 			if (opt < OPT_LONG_MIN_NUM && isprint(opt)) {
736ae67895bSDavid Marchand 				EAL_LOG(ERR, "Option %c is not supported "
737ae67895bSDavid Marchand 					"on Linux", opt);
73899a2dd95SBruce Richardson 			} else if (opt >= OPT_LONG_MIN_NUM &&
73999a2dd95SBruce Richardson 				   opt < OPT_LONG_MAX_NUM) {
740ae67895bSDavid Marchand 				EAL_LOG(ERR, "Option %s is not supported "
741ae67895bSDavid Marchand 					"on Linux",
74299a2dd95SBruce Richardson 					eal_long_options[option_index].name);
74399a2dd95SBruce Richardson 			} else {
744ae67895bSDavid Marchand 				EAL_LOG(ERR, "Option %d is not supported "
745ae67895bSDavid Marchand 					"on Linux", opt);
74699a2dd95SBruce Richardson 			}
74799a2dd95SBruce Richardson 			eal_usage(prgname);
74899a2dd95SBruce Richardson 			ret = -1;
74999a2dd95SBruce Richardson 			goto out;
75099a2dd95SBruce Richardson 		}
75199a2dd95SBruce Richardson 	}
75299a2dd95SBruce Richardson 
753ce382fddSBruce Richardson 	/* create runtime data directory. In no_shconf mode, skip any errors */
754ce382fddSBruce Richardson 	if (eal_create_runtime_dir() < 0) {
755ce382fddSBruce Richardson 		if (internal_conf->no_shconf == 0) {
756ae67895bSDavid Marchand 			EAL_LOG(ERR, "Cannot create runtime directory");
75799a2dd95SBruce Richardson 			ret = -1;
75899a2dd95SBruce Richardson 			goto out;
759ce382fddSBruce Richardson 		} else
760ae67895bSDavid Marchand 			EAL_LOG(WARNING, "No DPDK runtime directory created");
76199a2dd95SBruce Richardson 	}
76299a2dd95SBruce Richardson 
76399a2dd95SBruce Richardson 	if (eal_adjust_config(internal_conf) != 0) {
76499a2dd95SBruce Richardson 		ret = -1;
76599a2dd95SBruce Richardson 		goto out;
76699a2dd95SBruce Richardson 	}
76799a2dd95SBruce Richardson 
76899a2dd95SBruce Richardson 	/* sanity checks */
76999a2dd95SBruce Richardson 	if (eal_check_common_options(internal_conf) != 0) {
77099a2dd95SBruce Richardson 		eal_usage(prgname);
77199a2dd95SBruce Richardson 		ret = -1;
77299a2dd95SBruce Richardson 		goto out;
77399a2dd95SBruce Richardson 	}
77499a2dd95SBruce Richardson 
77599a2dd95SBruce Richardson 	if (optind >= 0)
77699a2dd95SBruce Richardson 		argv[optind-1] = prgname;
77799a2dd95SBruce Richardson 	ret = optind-1;
77899a2dd95SBruce Richardson 
77999a2dd95SBruce Richardson out:
78099a2dd95SBruce Richardson 	/* restore getopt lib */
78199a2dd95SBruce Richardson 	optind = old_optind;
78299a2dd95SBruce Richardson 	optopt = old_optopt;
78399a2dd95SBruce Richardson 	optarg = old_optarg;
78499a2dd95SBruce Richardson 
78599a2dd95SBruce Richardson 	return ret;
78699a2dd95SBruce Richardson }
78799a2dd95SBruce Richardson 
78899a2dd95SBruce Richardson static int
78999a2dd95SBruce Richardson check_socket(const struct rte_memseg_list *msl, void *arg)
79099a2dd95SBruce Richardson {
79199a2dd95SBruce Richardson 	int *socket_id = arg;
79299a2dd95SBruce Richardson 
79399a2dd95SBruce Richardson 	if (msl->external)
79499a2dd95SBruce Richardson 		return 0;
79599a2dd95SBruce Richardson 
79699a2dd95SBruce Richardson 	return *socket_id == msl->socket_id;
79799a2dd95SBruce Richardson }
79899a2dd95SBruce Richardson 
79999a2dd95SBruce Richardson static void
80099a2dd95SBruce Richardson eal_check_mem_on_local_socket(void)
80199a2dd95SBruce Richardson {
80299a2dd95SBruce Richardson 	int socket_id;
80399a2dd95SBruce Richardson 	const struct rte_config *config = rte_eal_get_configuration();
80499a2dd95SBruce Richardson 
80599a2dd95SBruce Richardson 	socket_id = rte_lcore_to_socket_id(config->main_lcore);
80699a2dd95SBruce Richardson 
80799a2dd95SBruce Richardson 	if (rte_memseg_list_walk(check_socket, &socket_id) == 0)
808ae67895bSDavid Marchand 		EAL_LOG(WARNING, "WARNING: Main core has no memory on local socket!");
80999a2dd95SBruce Richardson }
81099a2dd95SBruce Richardson 
81199a2dd95SBruce Richardson static int
81299a2dd95SBruce Richardson sync_func(__rte_unused void *arg)
81399a2dd95SBruce Richardson {
81499a2dd95SBruce Richardson 	return 0;
81599a2dd95SBruce Richardson }
81699a2dd95SBruce Richardson 
81799a2dd95SBruce Richardson /*
81899a2dd95SBruce Richardson  * Request iopl privilege for all RPL, returns 0 on success
81999a2dd95SBruce Richardson  * iopl() call is mostly for the i386 architecture. For other architectures,
82099a2dd95SBruce Richardson  * return -1 to indicate IO privilege can't be changed in this way.
82199a2dd95SBruce Richardson  */
82299a2dd95SBruce Richardson int
82399a2dd95SBruce Richardson rte_eal_iopl_init(void)
82499a2dd95SBruce Richardson {
82599a2dd95SBruce Richardson #if defined(RTE_ARCH_X86)
82699a2dd95SBruce Richardson 	if (iopl(3) != 0)
82799a2dd95SBruce Richardson 		return -1;
82899a2dd95SBruce Richardson #endif
82999a2dd95SBruce Richardson 	return 0;
83099a2dd95SBruce Richardson }
83199a2dd95SBruce Richardson 
83299a2dd95SBruce Richardson static void rte_eal_init_alert(const char *msg)
83399a2dd95SBruce Richardson {
83472bf6da8SStephen Hemminger 	EAL_LOG(ALERT, "%s", msg);
83599a2dd95SBruce Richardson }
83699a2dd95SBruce Richardson 
83799a2dd95SBruce Richardson /*
83899a2dd95SBruce Richardson  * On Linux 3.6+, even if VFIO is not loaded, whenever IOMMU is enabled in the
83999a2dd95SBruce Richardson  * BIOS and in the kernel, /sys/kernel/iommu_groups path will contain kernel
84099a2dd95SBruce Richardson  * IOMMU groups. If IOMMU is not enabled, that path would be empty.
84199a2dd95SBruce Richardson  * Therefore, checking if the path is empty will tell us if IOMMU is enabled.
84299a2dd95SBruce Richardson  */
84399a2dd95SBruce Richardson static bool
84499a2dd95SBruce Richardson is_iommu_enabled(void)
84599a2dd95SBruce Richardson {
84699a2dd95SBruce Richardson 	DIR *dir = opendir(KERNEL_IOMMU_GROUPS_PATH);
84799a2dd95SBruce Richardson 	struct dirent *d;
84899a2dd95SBruce Richardson 	int n = 0;
84999a2dd95SBruce Richardson 
85099a2dd95SBruce Richardson 	/* if directory doesn't exist, assume IOMMU is not enabled */
85199a2dd95SBruce Richardson 	if (dir == NULL)
85299a2dd95SBruce Richardson 		return false;
85399a2dd95SBruce Richardson 
85499a2dd95SBruce Richardson 	while ((d = readdir(dir)) != NULL) {
85599a2dd95SBruce Richardson 		/* skip dot and dot-dot */
85699a2dd95SBruce Richardson 		if (++n > 2)
85799a2dd95SBruce Richardson 			break;
85899a2dd95SBruce Richardson 	}
85999a2dd95SBruce Richardson 	closedir(dir);
86099a2dd95SBruce Richardson 
86199a2dd95SBruce Richardson 	return n > 2;
86299a2dd95SBruce Richardson }
86399a2dd95SBruce Richardson 
8648b0a1b8cSTyler Retzlaff static __rte_noreturn void *
8658b0a1b8cSTyler Retzlaff eal_worker_thread_loop(void *arg)
8668b0a1b8cSTyler Retzlaff {
8678b0a1b8cSTyler Retzlaff 	eal_thread_loop(arg);
8688b0a1b8cSTyler Retzlaff }
8698b0a1b8cSTyler Retzlaff 
87042fbb8e8SDon Wallwork static int
87142fbb8e8SDon Wallwork eal_worker_thread_create(unsigned int lcore_id)
87242fbb8e8SDon Wallwork {
87342fbb8e8SDon Wallwork 	pthread_attr_t *attrp = NULL;
87442fbb8e8SDon Wallwork 	void *stack_ptr = NULL;
87542fbb8e8SDon Wallwork 	pthread_attr_t attr;
87642fbb8e8SDon Wallwork 	size_t stack_size;
87742fbb8e8SDon Wallwork 	int ret = -1;
87842fbb8e8SDon Wallwork 
87942fbb8e8SDon Wallwork 	stack_size = eal_get_internal_configuration()->huge_worker_stack_size;
88042fbb8e8SDon Wallwork 	if (stack_size != 0) {
88142fbb8e8SDon Wallwork 		/* Allocate NUMA aware stack memory and set pthread attributes */
88242fbb8e8SDon Wallwork 		stack_ptr = rte_zmalloc_socket("lcore_stack", stack_size,
88342fbb8e8SDon Wallwork 			RTE_CACHE_LINE_SIZE, rte_lcore_to_socket_id(lcore_id));
88442fbb8e8SDon Wallwork 		if (stack_ptr == NULL) {
88542fbb8e8SDon Wallwork 			rte_eal_init_alert("Cannot allocate worker lcore stack memory");
88642fbb8e8SDon Wallwork 			rte_errno = ENOMEM;
88742fbb8e8SDon Wallwork 			goto out;
88842fbb8e8SDon Wallwork 		}
88942fbb8e8SDon Wallwork 
89042fbb8e8SDon Wallwork 		if (pthread_attr_init(&attr) != 0) {
89142fbb8e8SDon Wallwork 			rte_eal_init_alert("Cannot init pthread attributes");
89242fbb8e8SDon Wallwork 			rte_errno = EFAULT;
89342fbb8e8SDon Wallwork 			goto out;
89442fbb8e8SDon Wallwork 		}
89542fbb8e8SDon Wallwork 		attrp = &attr;
89642fbb8e8SDon Wallwork 
89742fbb8e8SDon Wallwork 		if (pthread_attr_setstack(attrp, stack_ptr, stack_size) != 0) {
89842fbb8e8SDon Wallwork 			rte_eal_init_alert("Cannot set pthread stack attributes");
89942fbb8e8SDon Wallwork 			rte_errno = EFAULT;
90042fbb8e8SDon Wallwork 			goto out;
90142fbb8e8SDon Wallwork 		}
90242fbb8e8SDon Wallwork 	}
90342fbb8e8SDon Wallwork 
9048b0a1b8cSTyler Retzlaff 	if (pthread_create((pthread_t *)&lcore_config[lcore_id].thread_id.opaque_id,
9058b0a1b8cSTyler Retzlaff 			attrp, eal_worker_thread_loop, (void *)(uintptr_t)lcore_id) == 0)
90642fbb8e8SDon Wallwork 		ret = 0;
90742fbb8e8SDon Wallwork 
90842fbb8e8SDon Wallwork out:
90942fbb8e8SDon Wallwork 	if (ret != 0)
91042fbb8e8SDon Wallwork 		rte_free(stack_ptr);
91142fbb8e8SDon Wallwork 	if (attrp != NULL)
91242fbb8e8SDon Wallwork 		pthread_attr_destroy(attrp);
91342fbb8e8SDon Wallwork 	return ret;
91442fbb8e8SDon Wallwork }
91542fbb8e8SDon Wallwork 
91699a2dd95SBruce Richardson /* Launch threads, called at application init(). */
91799a2dd95SBruce Richardson int
91899a2dd95SBruce Richardson rte_eal_init(int argc, char **argv)
91999a2dd95SBruce Richardson {
92099a2dd95SBruce Richardson 	int i, fctret, ret;
9212a7a42a5STyler Retzlaff 	static RTE_ATOMIC(uint32_t) run_once;
92299a2dd95SBruce Richardson 	uint32_t has_run = 0;
92399a2dd95SBruce Richardson 	char cpuset[RTE_CPU_AFFINITY_STR_LEN];
92493d8a7edSThomas Monjalon 	char thread_name[RTE_THREAD_NAME_SIZE];
92599a2dd95SBruce Richardson 	bool phys_addrs;
92699a2dd95SBruce Richardson 	const struct rte_config *config = rte_eal_get_configuration();
92799a2dd95SBruce Richardson 	struct internal_config *internal_conf =
92899a2dd95SBruce Richardson 		eal_get_internal_configuration();
92999a2dd95SBruce Richardson 
930*2773d39fSStephen Hemminger 	/* setup log as early as possible */
931*2773d39fSStephen Hemminger 	if (eal_parse_log_options(argc, argv) < 0) {
932*2773d39fSStephen Hemminger 		rte_eal_init_alert("invalid log arguments.");
933*2773d39fSStephen Hemminger 		rte_errno = EINVAL;
934*2773d39fSStephen Hemminger 		return -1;
935*2773d39fSStephen Hemminger 	}
936*2773d39fSStephen Hemminger 
937*2773d39fSStephen Hemminger 	eal_log_init(program_invocation_short_name);
938*2773d39fSStephen Hemminger 
93999a2dd95SBruce Richardson 	/* checks if the machine is adequate */
94099a2dd95SBruce Richardson 	if (!rte_cpu_is_supported()) {
94199a2dd95SBruce Richardson 		rte_eal_init_alert("unsupported cpu type.");
94299a2dd95SBruce Richardson 		rte_errno = ENOTSUP;
94399a2dd95SBruce Richardson 		return -1;
94499a2dd95SBruce Richardson 	}
94599a2dd95SBruce Richardson 
946e168b189SDavid Christensen 	/* verify if DPDK supported on architecture MMU */
947e168b189SDavid Christensen 	if (!eal_mmu_supported()) {
948e168b189SDavid Christensen 		rte_eal_init_alert("unsupported MMU type.");
949e168b189SDavid Christensen 		rte_errno = ENOTSUP;
950e168b189SDavid Christensen 		return -1;
951e168b189SDavid Christensen 	}
952e168b189SDavid Christensen 
9532a7a42a5STyler Retzlaff 	if (!rte_atomic_compare_exchange_strong_explicit(&run_once, &has_run, 1,
9542a7a42a5STyler Retzlaff 					rte_memory_order_relaxed, rte_memory_order_relaxed)) {
95599a2dd95SBruce Richardson 		rte_eal_init_alert("already called initialization.");
95699a2dd95SBruce Richardson 		rte_errno = EALREADY;
95799a2dd95SBruce Richardson 		return -1;
95899a2dd95SBruce Richardson 	}
95999a2dd95SBruce Richardson 
96099a2dd95SBruce Richardson 	eal_reset_internal_config(internal_conf);
96199a2dd95SBruce Richardson 
96299a2dd95SBruce Richardson 	/* clone argv to report out later in telemetry */
96399a2dd95SBruce Richardson 	eal_save_args(argc, argv);
96499a2dd95SBruce Richardson 
96599a2dd95SBruce Richardson 	if (rte_eal_cpu_init() < 0) {
96699a2dd95SBruce Richardson 		rte_eal_init_alert("Cannot detect lcores.");
96799a2dd95SBruce Richardson 		rte_errno = ENOTSUP;
96899a2dd95SBruce Richardson 		return -1;
96999a2dd95SBruce Richardson 	}
97099a2dd95SBruce Richardson 
97199a2dd95SBruce Richardson 	fctret = eal_parse_args(argc, argv);
97299a2dd95SBruce Richardson 	if (fctret < 0) {
97399a2dd95SBruce Richardson 		rte_eal_init_alert("Invalid 'command line' arguments.");
97499a2dd95SBruce Richardson 		rte_errno = EINVAL;
9752a7a42a5STyler Retzlaff 		rte_atomic_store_explicit(&run_once, 0, rte_memory_order_relaxed);
97699a2dd95SBruce Richardson 		return -1;
97799a2dd95SBruce Richardson 	}
97899a2dd95SBruce Richardson 
97999a2dd95SBruce Richardson 	if (eal_plugins_init() < 0) {
98099a2dd95SBruce Richardson 		rte_eal_init_alert("Cannot init plugins");
98199a2dd95SBruce Richardson 		rte_errno = EINVAL;
9822a7a42a5STyler Retzlaff 		rte_atomic_store_explicit(&run_once, 0, rte_memory_order_relaxed);
98399a2dd95SBruce Richardson 		return -1;
98499a2dd95SBruce Richardson 	}
98599a2dd95SBruce Richardson 
98699a2dd95SBruce Richardson 	if (eal_trace_init() < 0) {
98799a2dd95SBruce Richardson 		rte_eal_init_alert("Cannot init trace");
98899a2dd95SBruce Richardson 		rte_errno = EFAULT;
98999a2dd95SBruce Richardson 		return -1;
99099a2dd95SBruce Richardson 	}
99199a2dd95SBruce Richardson 
99299a2dd95SBruce Richardson 	if (eal_option_device_parse()) {
99399a2dd95SBruce Richardson 		rte_errno = ENODEV;
9942a7a42a5STyler Retzlaff 		rte_atomic_store_explicit(&run_once, 0, rte_memory_order_relaxed);
99599a2dd95SBruce Richardson 		return -1;
99699a2dd95SBruce Richardson 	}
99799a2dd95SBruce Richardson 
99899a2dd95SBruce Richardson 	if (rte_config_init() < 0) {
99999a2dd95SBruce Richardson 		rte_eal_init_alert("Cannot init config");
100099a2dd95SBruce Richardson 		return -1;
100199a2dd95SBruce Richardson 	}
100299a2dd95SBruce Richardson 
100399a2dd95SBruce Richardson 	if (rte_eal_intr_init() < 0) {
100499a2dd95SBruce Richardson 		rte_eal_init_alert("Cannot init interrupt-handling thread");
100599a2dd95SBruce Richardson 		return -1;
100699a2dd95SBruce Richardson 	}
100799a2dd95SBruce Richardson 
100899a2dd95SBruce Richardson 	if (rte_eal_alarm_init() < 0) {
100999a2dd95SBruce Richardson 		rte_eal_init_alert("Cannot init alarm");
101099a2dd95SBruce Richardson 		/* rte_eal_alarm_init sets rte_errno on failure. */
101199a2dd95SBruce Richardson 		return -1;
101299a2dd95SBruce Richardson 	}
101399a2dd95SBruce Richardson 
101499a2dd95SBruce Richardson 	/* Put mp channel init before bus scan so that we can init the vdev
101599a2dd95SBruce Richardson 	 * bus through mp channel in the secondary process before the bus scan.
101699a2dd95SBruce Richardson 	 */
101799a2dd95SBruce Richardson 	if (rte_mp_channel_init() < 0 && rte_errno != ENOTSUP) {
101899a2dd95SBruce Richardson 		rte_eal_init_alert("failed to init mp channel");
101999a2dd95SBruce Richardson 		if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
102099a2dd95SBruce Richardson 			rte_errno = EFAULT;
102199a2dd95SBruce Richardson 			return -1;
102299a2dd95SBruce Richardson 		}
102399a2dd95SBruce Richardson 	}
102499a2dd95SBruce Richardson 
102599a2dd95SBruce Richardson 	if (rte_bus_scan()) {
102699a2dd95SBruce Richardson 		rte_eal_init_alert("Cannot scan the buses for devices");
102799a2dd95SBruce Richardson 		rte_errno = ENODEV;
10282a7a42a5STyler Retzlaff 		rte_atomic_store_explicit(&run_once, 0, rte_memory_order_relaxed);
102999a2dd95SBruce Richardson 		return -1;
103099a2dd95SBruce Richardson 	}
103199a2dd95SBruce Richardson 
103299a2dd95SBruce Richardson 	phys_addrs = rte_eal_using_phys_addrs() != 0;
103399a2dd95SBruce Richardson 
103499a2dd95SBruce Richardson 	/* if no EAL option "--iova-mode=<pa|va>", use bus IOVA scheme */
103599a2dd95SBruce Richardson 	if (internal_conf->iova_mode == RTE_IOVA_DC) {
103699a2dd95SBruce Richardson 		/* autodetect the IOVA mapping mode */
103799a2dd95SBruce Richardson 		enum rte_iova_mode iova_mode = rte_bus_get_iommu_class();
103899a2dd95SBruce Richardson 
103999a2dd95SBruce Richardson 		if (iova_mode == RTE_IOVA_DC) {
1040ae67895bSDavid Marchand 			EAL_LOG(DEBUG, "Buses did not request a specific IOVA mode.");
104199a2dd95SBruce Richardson 
1042a37335bcSViacheslav Ovsiienko 			if (!RTE_IOVA_IN_MBUF) {
1043a37335bcSViacheslav Ovsiienko 				iova_mode = RTE_IOVA_VA;
1044ae67895bSDavid Marchand 				EAL_LOG(DEBUG, "IOVA as VA mode is forced by build option.");
1045a37335bcSViacheslav Ovsiienko 			} else if (!phys_addrs) {
104699a2dd95SBruce Richardson 				/* if we have no access to physical addresses,
104799a2dd95SBruce Richardson 				 * pick IOVA as VA mode.
104899a2dd95SBruce Richardson 				 */
104999a2dd95SBruce Richardson 				iova_mode = RTE_IOVA_VA;
1050ae67895bSDavid Marchand 				EAL_LOG(DEBUG, "Physical addresses are unavailable, selecting IOVA as VA mode.");
105199a2dd95SBruce Richardson 			} else if (is_iommu_enabled()) {
105299a2dd95SBruce Richardson 				/* we have an IOMMU, pick IOVA as VA mode */
105399a2dd95SBruce Richardson 				iova_mode = RTE_IOVA_VA;
1054ae67895bSDavid Marchand 				EAL_LOG(DEBUG, "IOMMU is available, selecting IOVA as VA mode.");
105599a2dd95SBruce Richardson 			} else {
105699a2dd95SBruce Richardson 				/* physical addresses available, and no IOMMU
105799a2dd95SBruce Richardson 				 * found, so pick IOVA as PA.
105899a2dd95SBruce Richardson 				 */
105999a2dd95SBruce Richardson 				iova_mode = RTE_IOVA_PA;
1060ae67895bSDavid Marchand 				EAL_LOG(DEBUG, "IOMMU is not available, selecting IOVA as PA mode.");
106199a2dd95SBruce Richardson 			}
106299a2dd95SBruce Richardson 		}
106399a2dd95SBruce Richardson 		rte_eal_get_configuration()->iova_mode = iova_mode;
106499a2dd95SBruce Richardson 	} else {
106599a2dd95SBruce Richardson 		rte_eal_get_configuration()->iova_mode =
106699a2dd95SBruce Richardson 			internal_conf->iova_mode;
106799a2dd95SBruce Richardson 	}
106899a2dd95SBruce Richardson 
106999a2dd95SBruce Richardson 	if (rte_eal_iova_mode() == RTE_IOVA_PA && !phys_addrs) {
107099a2dd95SBruce Richardson 		rte_eal_init_alert("Cannot use IOVA as 'PA' since physical addresses are not available");
107199a2dd95SBruce Richardson 		rte_errno = EINVAL;
107299a2dd95SBruce Richardson 		return -1;
107399a2dd95SBruce Richardson 	}
107499a2dd95SBruce Richardson 
1075d5d9e8feSThomas Monjalon 	if (rte_eal_iova_mode() == RTE_IOVA_PA && !RTE_IOVA_IN_MBUF) {
1076a986c2b7SShijith Thotton 		rte_eal_init_alert("Cannot use IOVA as 'PA' as it is disabled during build");
1077a986c2b7SShijith Thotton 		rte_errno = EINVAL;
1078a986c2b7SShijith Thotton 		return -1;
1079a986c2b7SShijith Thotton 	}
1080a986c2b7SShijith Thotton 
1081ae67895bSDavid Marchand 	EAL_LOG(INFO, "Selected IOVA mode '%s'",
108299a2dd95SBruce Richardson 		rte_eal_iova_mode() == RTE_IOVA_PA ? "PA" : "VA");
108399a2dd95SBruce Richardson 
108499a2dd95SBruce Richardson 	if (internal_conf->no_hugetlbfs == 0) {
108599a2dd95SBruce Richardson 		/* rte_config isn't initialized yet */
108699a2dd95SBruce Richardson 		ret = internal_conf->process_type == RTE_PROC_PRIMARY ?
108799a2dd95SBruce Richardson 				eal_hugepage_info_init() :
108899a2dd95SBruce Richardson 				eal_hugepage_info_read();
108999a2dd95SBruce Richardson 		if (ret < 0) {
109099a2dd95SBruce Richardson 			rte_eal_init_alert("Cannot get hugepage information.");
109199a2dd95SBruce Richardson 			rte_errno = EACCES;
10922a7a42a5STyler Retzlaff 			rte_atomic_store_explicit(&run_once, 0, rte_memory_order_relaxed);
109399a2dd95SBruce Richardson 			return -1;
109499a2dd95SBruce Richardson 		}
109599a2dd95SBruce Richardson 	}
109699a2dd95SBruce Richardson 
109799a2dd95SBruce Richardson 	if (internal_conf->memory == 0 && internal_conf->force_sockets == 0) {
109899a2dd95SBruce Richardson 		if (internal_conf->no_hugetlbfs)
109999a2dd95SBruce Richardson 			internal_conf->memory = MEMSIZE_IF_NO_HUGE_PAGE;
110099a2dd95SBruce Richardson 	}
110199a2dd95SBruce Richardson 
110299a2dd95SBruce Richardson 	if (internal_conf->vmware_tsc_map == 1) {
110399a2dd95SBruce Richardson #ifdef RTE_LIBRTE_EAL_VMWARE_TSC_MAP_SUPPORT
110499a2dd95SBruce Richardson 		rte_cycles_vmware_tsc_map = 1;
1105ae67895bSDavid Marchand 		EAL_LOG(DEBUG, "Using VMWARE TSC MAP, "
1106ae67895bSDavid Marchand 				"you must have monitor_control.pseudo_perfctr = TRUE");
110799a2dd95SBruce Richardson #else
1108ae67895bSDavid Marchand 		EAL_LOG(WARNING, "Ignoring --vmware-tsc-map because "
1109ae67895bSDavid Marchand 				"RTE_LIBRTE_EAL_VMWARE_TSC_MAP_SUPPORT is not set");
111099a2dd95SBruce Richardson #endif
111199a2dd95SBruce Richardson 	}
111299a2dd95SBruce Richardson 
111399a2dd95SBruce Richardson #ifdef VFIO_PRESENT
11146e18a2d4SDavid Marchand 	if (rte_vfio_enable("vfio")) {
111599a2dd95SBruce Richardson 		rte_eal_init_alert("Cannot init VFIO");
111699a2dd95SBruce Richardson 		rte_errno = EAGAIN;
11172a7a42a5STyler Retzlaff 		rte_atomic_store_explicit(&run_once, 0, rte_memory_order_relaxed);
111899a2dd95SBruce Richardson 		return -1;
111999a2dd95SBruce Richardson 	}
112099a2dd95SBruce Richardson #endif
112199a2dd95SBruce Richardson 	/* in secondary processes, memory init may allocate additional fbarrays
112299a2dd95SBruce Richardson 	 * not present in primary processes, so to avoid any potential issues,
112399a2dd95SBruce Richardson 	 * initialize memzones first.
112499a2dd95SBruce Richardson 	 */
112599a2dd95SBruce Richardson 	if (rte_eal_memzone_init() < 0) {
112699a2dd95SBruce Richardson 		rte_eal_init_alert("Cannot init memzone");
112799a2dd95SBruce Richardson 		rte_errno = ENODEV;
112899a2dd95SBruce Richardson 		return -1;
112999a2dd95SBruce Richardson 	}
113099a2dd95SBruce Richardson 
11312e2f0272SDavid Marchand 	rte_mcfg_mem_read_lock();
11322e2f0272SDavid Marchand 
113399a2dd95SBruce Richardson 	if (rte_eal_memory_init() < 0) {
11342e2f0272SDavid Marchand 		rte_mcfg_mem_read_unlock();
113599a2dd95SBruce Richardson 		rte_eal_init_alert("Cannot init memory");
113699a2dd95SBruce Richardson 		rte_errno = ENOMEM;
113799a2dd95SBruce Richardson 		return -1;
113899a2dd95SBruce Richardson 	}
113999a2dd95SBruce Richardson 
114099a2dd95SBruce Richardson 	/* the directories are locked during eal_hugepage_info_init */
114199a2dd95SBruce Richardson 	eal_hugedirs_unlock();
114299a2dd95SBruce Richardson 
114399a2dd95SBruce Richardson 	if (rte_eal_malloc_heap_init() < 0) {
11442e2f0272SDavid Marchand 		rte_mcfg_mem_read_unlock();
11452e2f0272SDavid Marchand 		rte_eal_init_alert("Cannot init malloc heap");
11462e2f0272SDavid Marchand 		rte_errno = ENODEV;
11472e2f0272SDavid Marchand 		return -1;
11482e2f0272SDavid Marchand 	}
11492e2f0272SDavid Marchand 
11502e2f0272SDavid Marchand 	rte_mcfg_mem_read_unlock();
11512e2f0272SDavid Marchand 
11522e2f0272SDavid Marchand 	if (rte_eal_malloc_heap_populate() < 0) {
115399a2dd95SBruce Richardson 		rte_eal_init_alert("Cannot init malloc heap");
115499a2dd95SBruce Richardson 		rte_errno = ENODEV;
115599a2dd95SBruce Richardson 		return -1;
115699a2dd95SBruce Richardson 	}
115799a2dd95SBruce Richardson 
115866f7ee3fSZhihong Wang 	/* register multi-process action callbacks for hotplug after memory init */
115966f7ee3fSZhihong Wang 	if (eal_mp_dev_hotplug_init() < 0) {
116066f7ee3fSZhihong Wang 		rte_eal_init_alert("failed to register mp callback for hotplug");
116166f7ee3fSZhihong Wang 		return -1;
116266f7ee3fSZhihong Wang 	}
116366f7ee3fSZhihong Wang 
116499a2dd95SBruce Richardson 	if (rte_eal_tailqs_init() < 0) {
116599a2dd95SBruce Richardson 		rte_eal_init_alert("Cannot init tail queues for objects");
116699a2dd95SBruce Richardson 		rte_errno = EFAULT;
116799a2dd95SBruce Richardson 		return -1;
116899a2dd95SBruce Richardson 	}
116999a2dd95SBruce Richardson 
117099a2dd95SBruce Richardson 	if (rte_eal_timer_init() < 0) {
117199a2dd95SBruce Richardson 		rte_eal_init_alert("Cannot init HPET or TSC timers");
117299a2dd95SBruce Richardson 		rte_errno = ENOTSUP;
117399a2dd95SBruce Richardson 		return -1;
117499a2dd95SBruce Richardson 	}
117599a2dd95SBruce Richardson 
117699a2dd95SBruce Richardson 	eal_check_mem_on_local_socket();
117799a2dd95SBruce Richardson 
11788b0a1b8cSTyler Retzlaff 	if (rte_thread_set_affinity_by_id(rte_thread_self(),
117999a2dd95SBruce Richardson 			&lcore_config[config->main_lcore].cpuset) != 0) {
118099a2dd95SBruce Richardson 		rte_eal_init_alert("Cannot set affinity");
118199a2dd95SBruce Richardson 		rte_errno = EINVAL;
118299a2dd95SBruce Richardson 		return -1;
118399a2dd95SBruce Richardson 	}
118499a2dd95SBruce Richardson 	__rte_thread_init(config->main_lcore,
118599a2dd95SBruce Richardson 		&lcore_config[config->main_lcore].cpuset);
118699a2dd95SBruce Richardson 
118799a2dd95SBruce Richardson 	ret = eal_thread_dump_current_affinity(cpuset, sizeof(cpuset));
1188ae67895bSDavid Marchand 	EAL_LOG(DEBUG, "Main lcore %u is ready (tid=%zx;cpuset=[%s%s])",
1189a95d7054SDavid Marchand 		config->main_lcore, (uintptr_t)pthread_self(), cpuset,
119099a2dd95SBruce Richardson 		ret == 0 ? "" : "...");
119199a2dd95SBruce Richardson 
119299a2dd95SBruce Richardson 	RTE_LCORE_FOREACH_WORKER(i) {
119399a2dd95SBruce Richardson 
119499a2dd95SBruce Richardson 		/*
119599a2dd95SBruce Richardson 		 * create communication pipes between main thread
119699a2dd95SBruce Richardson 		 * and children
119799a2dd95SBruce Richardson 		 */
119899a2dd95SBruce Richardson 		if (pipe(lcore_config[i].pipe_main2worker) < 0)
119999a2dd95SBruce Richardson 			rte_panic("Cannot create pipe\n");
120099a2dd95SBruce Richardson 		if (pipe(lcore_config[i].pipe_worker2main) < 0)
120199a2dd95SBruce Richardson 			rte_panic("Cannot create pipe\n");
120299a2dd95SBruce Richardson 
120399a2dd95SBruce Richardson 		lcore_config[i].state = WAIT;
120499a2dd95SBruce Richardson 
120599a2dd95SBruce Richardson 		/* create a thread for each lcore */
120642fbb8e8SDon Wallwork 		ret = eal_worker_thread_create(i);
120799a2dd95SBruce Richardson 		if (ret != 0)
120899a2dd95SBruce Richardson 			rte_panic("Cannot create thread\n");
120999a2dd95SBruce Richardson 
121099a2dd95SBruce Richardson 		/* Set thread_name for aid in debugging. */
121199a2dd95SBruce Richardson 		snprintf(thread_name, sizeof(thread_name),
121262774b78SThomas Monjalon 			"dpdk-worker%d", i);
12136d87be58STyler Retzlaff 		rte_thread_set_name(lcore_config[i].thread_id, thread_name);
121499a2dd95SBruce Richardson 
12158b0a1b8cSTyler Retzlaff 		ret = rte_thread_set_affinity_by_id(lcore_config[i].thread_id,
12168b0a1b8cSTyler Retzlaff 			&lcore_config[i].cpuset);
121799a2dd95SBruce Richardson 		if (ret != 0)
121899a2dd95SBruce Richardson 			rte_panic("Cannot set affinity\n");
121999a2dd95SBruce Richardson 	}
122099a2dd95SBruce Richardson 
122199a2dd95SBruce Richardson 	/*
122299a2dd95SBruce Richardson 	 * Launch a dummy function on all worker lcores, so that main lcore
122399a2dd95SBruce Richardson 	 * knows they are all ready when this function returns.
122499a2dd95SBruce Richardson 	 */
122599a2dd95SBruce Richardson 	rte_eal_mp_remote_launch(sync_func, NULL, SKIP_MAIN);
122699a2dd95SBruce Richardson 	rte_eal_mp_wait_lcore();
122799a2dd95SBruce Richardson 
122899a2dd95SBruce Richardson 	/* initialize services so vdevs register service during bus_probe. */
122999a2dd95SBruce Richardson 	ret = rte_service_init();
123099a2dd95SBruce Richardson 	if (ret) {
123199a2dd95SBruce Richardson 		rte_eal_init_alert("rte_service_init() failed");
123299a2dd95SBruce Richardson 		rte_errno = -ret;
123399a2dd95SBruce Richardson 		return -1;
123499a2dd95SBruce Richardson 	}
123599a2dd95SBruce Richardson 
123699a2dd95SBruce Richardson 	/* Probe all the buses and devices/drivers on them */
123799a2dd95SBruce Richardson 	if (rte_bus_probe()) {
123899a2dd95SBruce Richardson 		rte_eal_init_alert("Cannot probe devices");
123999a2dd95SBruce Richardson 		rte_errno = ENOTSUP;
124099a2dd95SBruce Richardson 		return -1;
124199a2dd95SBruce Richardson 	}
124299a2dd95SBruce Richardson 
124399a2dd95SBruce Richardson 	/* initialize default service/lcore mappings and start running. Ignore
124499a2dd95SBruce Richardson 	 * -ENOTSUP, as it indicates no service coremask passed to EAL.
124599a2dd95SBruce Richardson 	 */
124699a2dd95SBruce Richardson 	ret = rte_service_start_with_defaults();
124799a2dd95SBruce Richardson 	if (ret < 0 && ret != -ENOTSUP) {
124899a2dd95SBruce Richardson 		rte_errno = -ret;
124999a2dd95SBruce Richardson 		return -1;
125099a2dd95SBruce Richardson 	}
125199a2dd95SBruce Richardson 
125299a2dd95SBruce Richardson 	/*
125399a2dd95SBruce Richardson 	 * Clean up unused files in runtime directory. We do this at the end of
125499a2dd95SBruce Richardson 	 * init and not at the beginning because we want to clean stuff up
125599a2dd95SBruce Richardson 	 * whether we are primary or secondary process, but we cannot remove
125699a2dd95SBruce Richardson 	 * primary process' files because secondary should be able to run even
125799a2dd95SBruce Richardson 	 * if primary process is dead.
125899a2dd95SBruce Richardson 	 *
125999a2dd95SBruce Richardson 	 * In no_shconf mode, no runtime directory is created in the first
126099a2dd95SBruce Richardson 	 * place, so no cleanup needed.
126199a2dd95SBruce Richardson 	 */
126299a2dd95SBruce Richardson 	if (!internal_conf->no_shconf && eal_clean_runtime_dir() < 0) {
126399a2dd95SBruce Richardson 		rte_eal_init_alert("Cannot clear runtime directory");
126499a2dd95SBruce Richardson 		return -1;
126599a2dd95SBruce Richardson 	}
1266e89463a3SBruce Richardson 	if (rte_eal_process_type() == RTE_PROC_PRIMARY && !internal_conf->no_telemetry) {
126799a2dd95SBruce Richardson 		if (rte_telemetry_init(rte_eal_get_runtime_dir(),
126899a2dd95SBruce Richardson 				rte_version(),
126968150b90SBruce Richardson 				&internal_conf->ctrl_cpuset) != 0)
127099a2dd95SBruce Richardson 			return -1;
127199a2dd95SBruce Richardson 	}
127299a2dd95SBruce Richardson 
127399a2dd95SBruce Richardson 	eal_mcfg_complete();
127499a2dd95SBruce Richardson 
127599a2dd95SBruce Richardson 	return fctret;
127699a2dd95SBruce Richardson }
127799a2dd95SBruce Richardson 
127899a2dd95SBruce Richardson static int
127999a2dd95SBruce Richardson mark_freeable(const struct rte_memseg_list *msl, const struct rte_memseg *ms,
128099a2dd95SBruce Richardson 		void *arg __rte_unused)
128199a2dd95SBruce Richardson {
128299a2dd95SBruce Richardson 	/* ms is const, so find this memseg */
128399a2dd95SBruce Richardson 	struct rte_memseg *found;
128499a2dd95SBruce Richardson 
128599a2dd95SBruce Richardson 	if (msl->external)
128699a2dd95SBruce Richardson 		return 0;
128799a2dd95SBruce Richardson 
128899a2dd95SBruce Richardson 	found = rte_mem_virt2memseg(ms->addr, msl);
128999a2dd95SBruce Richardson 
129099a2dd95SBruce Richardson 	found->flags &= ~RTE_MEMSEG_FLAG_DO_NOT_FREE;
129199a2dd95SBruce Richardson 
129299a2dd95SBruce Richardson 	return 0;
129399a2dd95SBruce Richardson }
129499a2dd95SBruce Richardson 
129599a2dd95SBruce Richardson int
129699a2dd95SBruce Richardson rte_eal_cleanup(void)
129799a2dd95SBruce Richardson {
12982a7a42a5STyler Retzlaff 	static RTE_ATOMIC(uint32_t) run_once;
1299a4a2ac98SBruce Richardson 	uint32_t has_run = 0;
1300a4a2ac98SBruce Richardson 
13012a7a42a5STyler Retzlaff 	if (!rte_atomic_compare_exchange_strong_explicit(&run_once, &has_run, 1,
13022a7a42a5STyler Retzlaff 					rte_memory_order_relaxed, rte_memory_order_relaxed)) {
1303ae67895bSDavid Marchand 		EAL_LOG(WARNING, "Already called cleanup");
1304a4a2ac98SBruce Richardson 		rte_errno = EALREADY;
1305a4a2ac98SBruce Richardson 		return -1;
1306a4a2ac98SBruce Richardson 	}
1307a4a2ac98SBruce Richardson 
130899a2dd95SBruce Richardson 	/* if we're in a primary process, we need to mark hugepages as freeable
130999a2dd95SBruce Richardson 	 * so that finalization can release them back to the system.
131099a2dd95SBruce Richardson 	 */
131199a2dd95SBruce Richardson 	struct internal_config *internal_conf =
131299a2dd95SBruce Richardson 		eal_get_internal_configuration();
131399a2dd95SBruce Richardson 
131432b4771cSDmitry Kozlyuk 	if (rte_eal_process_type() == RTE_PROC_PRIMARY &&
131532b4771cSDmitry Kozlyuk 			internal_conf->hugepage_file.unlink_existing)
131699a2dd95SBruce Richardson 		rte_memseg_walk(mark_freeable, NULL);
13176412941aSStephen Hemminger 
131899a2dd95SBruce Richardson 	rte_service_finalize();
13196412941aSStephen Hemminger #ifdef VFIO_PRESENT
13206412941aSStephen Hemminger 	vfio_mp_sync_cleanup();
13216412941aSStephen Hemminger #endif
132299a2dd95SBruce Richardson 	rte_mp_channel_cleanup();
13231cab1a40SKevin Laatz 	eal_bus_cleanup();
1324a8f23b44SChengwen Feng 	rte_trace_save();
1325a8f23b44SChengwen Feng 	eal_trace_fini();
1326cc759682SFengnan Chang 	eal_mp_dev_hotplug_cleanup();
1327cc759682SFengnan Chang 	rte_eal_alarm_cleanup();
132899a2dd95SBruce Richardson 	/* after this point, any DPDK pointers will become dangling */
132999a2dd95SBruce Richardson 	rte_eal_memory_detach();
1330a0cc7be2SStephen Hemminger 	rte_eal_malloc_heap_cleanup();
133199a2dd95SBruce Richardson 	eal_cleanup_config(internal_conf);
13325bce9bedSMattias Rönnblom 	eal_lcore_var_cleanup();
13335f4eb82fSStephen Hemminger 	rte_eal_log_cleanup();
133499a2dd95SBruce Richardson 	return 0;
133599a2dd95SBruce Richardson }
133699a2dd95SBruce Richardson 
133799a2dd95SBruce Richardson int rte_eal_create_uio_dev(void)
133899a2dd95SBruce Richardson {
133999a2dd95SBruce Richardson 	const struct internal_config *internal_conf =
134099a2dd95SBruce Richardson 		eal_get_internal_configuration();
134199a2dd95SBruce Richardson 
134299a2dd95SBruce Richardson 	return internal_conf->create_uio_dev;
134399a2dd95SBruce Richardson }
134499a2dd95SBruce Richardson 
134599a2dd95SBruce Richardson enum rte_intr_mode
134699a2dd95SBruce Richardson rte_eal_vfio_intr_mode(void)
134799a2dd95SBruce Richardson {
134899a2dd95SBruce Richardson 	const struct internal_config *internal_conf =
134999a2dd95SBruce Richardson 		eal_get_internal_configuration();
135099a2dd95SBruce Richardson 
135199a2dd95SBruce Richardson 	return internal_conf->vfio_intr_mode;
135299a2dd95SBruce Richardson }
135399a2dd95SBruce Richardson 
135499a2dd95SBruce Richardson void
135599a2dd95SBruce Richardson rte_eal_vfio_get_vf_token(rte_uuid_t vf_token)
135699a2dd95SBruce Richardson {
135799a2dd95SBruce Richardson 	struct internal_config *cfg = eal_get_internal_configuration();
135899a2dd95SBruce Richardson 
135999a2dd95SBruce Richardson 	rte_uuid_copy(vf_token, cfg->vfio_vf_token);
136099a2dd95SBruce Richardson }
136199a2dd95SBruce Richardson 
136299a2dd95SBruce Richardson int
136399a2dd95SBruce Richardson rte_eal_check_module(const char *module_name)
136499a2dd95SBruce Richardson {
136599a2dd95SBruce Richardson 	char sysfs_mod_name[PATH_MAX];
136699a2dd95SBruce Richardson 	struct stat st;
136799a2dd95SBruce Richardson 	int n;
136899a2dd95SBruce Richardson 
136999a2dd95SBruce Richardson 	if (NULL == module_name)
137099a2dd95SBruce Richardson 		return -1;
137199a2dd95SBruce Richardson 
137299a2dd95SBruce Richardson 	/* Check if there is sysfs mounted */
137399a2dd95SBruce Richardson 	if (stat("/sys/module", &st) != 0) {
1374ae67895bSDavid Marchand 		EAL_LOG(DEBUG, "sysfs is not mounted! error %i (%s)",
137599a2dd95SBruce Richardson 			errno, strerror(errno));
137699a2dd95SBruce Richardson 		return -1;
137799a2dd95SBruce Richardson 	}
137899a2dd95SBruce Richardson 
137999a2dd95SBruce Richardson 	/* A module might be built-in, therefore try sysfs */
138099a2dd95SBruce Richardson 	n = snprintf(sysfs_mod_name, PATH_MAX, "/sys/module/%s", module_name);
138199a2dd95SBruce Richardson 	if (n < 0 || n > PATH_MAX) {
1382ae67895bSDavid Marchand 		EAL_LOG(DEBUG, "Could not format module path");
138399a2dd95SBruce Richardson 		return -1;
138499a2dd95SBruce Richardson 	}
138599a2dd95SBruce Richardson 
138699a2dd95SBruce Richardson 	if (stat(sysfs_mod_name, &st) != 0) {
1387ae67895bSDavid Marchand 		EAL_LOG(DEBUG, "Module %s not found! error %i (%s)",
138899a2dd95SBruce Richardson 		        sysfs_mod_name, errno, strerror(errno));
138999a2dd95SBruce Richardson 		return 0;
139099a2dd95SBruce Richardson 	}
139199a2dd95SBruce Richardson 
139299a2dd95SBruce Richardson 	/* Module has been found */
139399a2dd95SBruce Richardson 	return 1;
139499a2dd95SBruce Richardson }
1395