1a551c94aSIdo Barnea/*-
2a551c94aSIdo Barnea *   BSD LICENSE
3a551c94aSIdo Barnea *
4a551c94aSIdo Barnea *   Copyright(c) 2010-2016 Intel Corporation. All rights reserved.
5a551c94aSIdo Barnea *   Copyright(c) 2012-2014 6WIND S.A.
6a551c94aSIdo Barnea *   All rights reserved.
7a551c94aSIdo Barnea *
8a551c94aSIdo Barnea *   Redistribution and use in source and binary forms, with or without
9a551c94aSIdo Barnea *   modification, are permitted provided that the following conditions
10a551c94aSIdo Barnea *   are met:
11a551c94aSIdo Barnea *
12a551c94aSIdo Barnea *     * Redistributions of source code must retain the above copyright
13a551c94aSIdo Barnea *       notice, this list of conditions and the following disclaimer.
14a551c94aSIdo Barnea *     * Redistributions in binary form must reproduce the above copyright
15a551c94aSIdo Barnea *       notice, this list of conditions and the following disclaimer in
16a551c94aSIdo Barnea *       the documentation and/or other materials provided with the
17a551c94aSIdo Barnea *       distribution.
18a551c94aSIdo Barnea *     * Neither the name of Intel Corporation nor the names of its
19a551c94aSIdo Barnea *       contributors may be used to endorse or promote products derived
20a551c94aSIdo Barnea *       from this software without specific prior written permission.
21a551c94aSIdo Barnea *
22a551c94aSIdo Barnea *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23a551c94aSIdo Barnea *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24a551c94aSIdo Barnea *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25a551c94aSIdo Barnea *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26a551c94aSIdo Barnea *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27a551c94aSIdo Barnea *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28a551c94aSIdo Barnea *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29a551c94aSIdo Barnea *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30a551c94aSIdo Barnea *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31a551c94aSIdo Barnea *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32a551c94aSIdo Barnea *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33a551c94aSIdo Barnea */
34a551c94aSIdo Barnea
35a551c94aSIdo Barnea#include <stdio.h>
36a551c94aSIdo Barnea#include <stdlib.h>
37a551c94aSIdo Barnea#include <stdint.h>
38a551c94aSIdo Barnea#include <string.h>
39a551c94aSIdo Barnea#include <stdarg.h>
40a551c94aSIdo Barnea#include <unistd.h>
41a551c94aSIdo Barnea#include <pthread.h>
42a551c94aSIdo Barnea#include <syslog.h>
43a551c94aSIdo Barnea#include <getopt.h>
44a551c94aSIdo Barnea#include <sys/file.h>
45a551c94aSIdo Barnea#include <fcntl.h>
46a551c94aSIdo Barnea#include <stddef.h>
47a551c94aSIdo Barnea#include <errno.h>
48a551c94aSIdo Barnea#include <limits.h>
49a551c94aSIdo Barnea#include <errno.h>
50a551c94aSIdo Barnea#include <sys/mman.h>
51a551c94aSIdo Barnea#include <sys/queue.h>
52a551c94aSIdo Barnea#include <sys/stat.h>
53a551c94aSIdo Barnea#if defined(RTE_ARCH_X86)
54a551c94aSIdo Barnea#include <sys/io.h>
55a551c94aSIdo Barnea#endif
56a551c94aSIdo Barnea
57a551c94aSIdo Barnea#include <rte_common.h>
58a551c94aSIdo Barnea#include <rte_debug.h>
59a551c94aSIdo Barnea#include <rte_memory.h>
60a551c94aSIdo Barnea#include <rte_memzone.h>
61a551c94aSIdo Barnea#include <rte_launch.h>
62a551c94aSIdo Barnea#include <rte_eal.h>
63a551c94aSIdo Barnea#include <rte_eal_memconfig.h>
64a551c94aSIdo Barnea#include <rte_per_lcore.h>
65a551c94aSIdo Barnea#include <rte_lcore.h>
66a551c94aSIdo Barnea#include <rte_log.h>
67a551c94aSIdo Barnea#include <rte_random.h>
68a551c94aSIdo Barnea#include <rte_cycles.h>
69a551c94aSIdo Barnea#include <rte_string_fns.h>
70a551c94aSIdo Barnea#include <rte_cpuflags.h>
71a551c94aSIdo Barnea#include <rte_interrupts.h>
729ca4a157SIdo Barnea#include <rte_bus.h>
73a551c94aSIdo Barnea#include <rte_pci.h>
749ca4a157SIdo Barnea#include <rte_dev.h>
75a551c94aSIdo Barnea#include <rte_devargs.h>
76a551c94aSIdo Barnea#include <rte_common.h>
77a551c94aSIdo Barnea#include <rte_version.h>
78a551c94aSIdo Barnea#include <rte_atomic.h>
79a551c94aSIdo Barnea#include <malloc_heap.h>
80a551c94aSIdo Barnea
81a551c94aSIdo Barnea#include "eal_private.h"
82a551c94aSIdo Barnea#include "eal_thread.h"
83a551c94aSIdo Barnea#include "eal_internal_cfg.h"
84a551c94aSIdo Barnea#include "eal_filesystem.h"
85a551c94aSIdo Barnea#include "eal_hugepages.h"
86a551c94aSIdo Barnea#include "eal_options.h"
87a551c94aSIdo Barnea#include "eal_vfio.h"
88a551c94aSIdo Barnea
89a551c94aSIdo Barnea#define MEMSIZE_IF_NO_HUGE_PAGE (64ULL * 1024ULL * 1024ULL)
90a551c94aSIdo Barnea
91a551c94aSIdo Barnea#define SOCKET_MEM_STRLEN (RTE_MAX_NUMA_NODES * 10)
92a551c94aSIdo Barnea
93a551c94aSIdo Barnea/* Allow the application to print its usage message too if set */
94a551c94aSIdo Barneastatic rte_usage_hook_t	rte_application_usage_hook = NULL;
95a551c94aSIdo Barnea
96a551c94aSIdo Barnea/* early configuration structure, when memory config is not mmapped */
97a551c94aSIdo Barneastatic struct rte_mem_config early_mem_config;
98a551c94aSIdo Barnea
99a551c94aSIdo Barnea/* define fd variable here, because file needs to be kept open for the
100a551c94aSIdo Barnea * duration of the program, as we hold a write lock on it in the primary proc */
101a551c94aSIdo Barneastatic int mem_cfg_fd = -1;
102a551c94aSIdo Barnea
103a551c94aSIdo Barneastatic struct flock wr_lock = {
104a551c94aSIdo Barnea		.l_type = F_WRLCK,
105a551c94aSIdo Barnea		.l_whence = SEEK_SET,
106a551c94aSIdo Barnea		.l_start = offsetof(struct rte_mem_config, memseg),
107a551c94aSIdo Barnea		.l_len = sizeof(early_mem_config.memseg),
108a551c94aSIdo Barnea};
109a551c94aSIdo Barnea
110a551c94aSIdo Barnea/* Address of global and public configuration */
111a551c94aSIdo Barneastatic struct rte_config rte_config = {
112a551c94aSIdo Barnea		.mem_config = &early_mem_config,
113a551c94aSIdo Barnea};
114a551c94aSIdo Barnea
115a551c94aSIdo Barnea/* internal configuration (per-core) */
116a551c94aSIdo Barneastruct lcore_config lcore_config[RTE_MAX_LCORE];
117a551c94aSIdo Barnea
118a551c94aSIdo Barnea/* internal configuration */
119a551c94aSIdo Barneastruct internal_config internal_config;
120a551c94aSIdo Barnea
121a551c94aSIdo Barnea/* used by rte_rdtsc() */
122a551c94aSIdo Barneaint rte_cycles_vmware_tsc_map;
123a551c94aSIdo Barnea
124a551c94aSIdo Barnea/* Return a pointer to the configuration structure */
125a551c94aSIdo Barneastruct rte_config *
126a551c94aSIdo Barnearte_eal_get_configuration(void)
127a551c94aSIdo Barnea{
128a551c94aSIdo Barnea	return &rte_config;
129a551c94aSIdo Barnea}
130a551c94aSIdo Barnea
131a551c94aSIdo Barnea/* parse a sysfs (or other) file containing one integer value */
132a551c94aSIdo Barneaint
133a551c94aSIdo Barneaeal_parse_sysfs_value(const char *filename, unsigned long *val)
134a551c94aSIdo Barnea{
135a551c94aSIdo Barnea	FILE *f;
136a551c94aSIdo Barnea	char buf[BUFSIZ];
137a551c94aSIdo Barnea	char *end = NULL;
138a551c94aSIdo Barnea
139a551c94aSIdo Barnea	if ((f = fopen(filename, "r")) == NULL) {
140a551c94aSIdo Barnea		RTE_LOG(ERR, EAL, "%s(): cannot open sysfs value %s\n",
141a551c94aSIdo Barnea			__func__, filename);
142a551c94aSIdo Barnea		return -1;
143a551c94aSIdo Barnea	}
144a551c94aSIdo Barnea
145a551c94aSIdo Barnea	if (fgets(buf, sizeof(buf), f) == NULL) {
146a551c94aSIdo Barnea		RTE_LOG(ERR, EAL, "%s(): cannot read sysfs value %s\n",
147a551c94aSIdo Barnea			__func__, filename);
148a551c94aSIdo Barnea		fclose(f);
149a551c94aSIdo Barnea		return -1;
150a551c94aSIdo Barnea	}
151a551c94aSIdo Barnea	*val = strtoul(buf, &end, 0);
152a551c94aSIdo Barnea	if ((buf[0] == '\0') || (end == NULL) || (*end != '\n')) {
153a551c94aSIdo Barnea		RTE_LOG(ERR, EAL, "%s(): cannot parse sysfs value %s\n",
154a551c94aSIdo Barnea				__func__, filename);
155a551c94aSIdo Barnea		fclose(f);
156a551c94aSIdo Barnea		return -1;
157a551c94aSIdo Barnea	}
158a551c94aSIdo Barnea	fclose(f);
159a551c94aSIdo Barnea	return 0;
160a551c94aSIdo Barnea}
161a551c94aSIdo Barnea
162a551c94aSIdo Barnea
163a551c94aSIdo Barnea/* create memory configuration in shared/mmap memory. Take out
164a551c94aSIdo Barnea * a write lock on the memsegs, so we can auto-detect primary/secondary.
165a551c94aSIdo Barnea * This means we never close the file while running (auto-close on exit).
166a551c94aSIdo Barnea * We also don't lock the whole file, so that in future we can use read-locks
167a551c94aSIdo Barnea * on other parts, e.g. memzones, to detect if there are running secondary
168a551c94aSIdo Barnea * processes. */
169a551c94aSIdo Barneastatic void
170a551c94aSIdo Barnearte_eal_config_create(void)
171a551c94aSIdo Barnea{
172a551c94aSIdo Barnea	void *rte_mem_cfg_addr;
173a551c94aSIdo Barnea	int retval;
174a551c94aSIdo Barnea
175a551c94aSIdo Barnea	const char *pathname = eal_runtime_config_path();
176a551c94aSIdo Barnea
177a551c94aSIdo Barnea	if (internal_config.no_shconf)
178a551c94aSIdo Barnea		return;
179a551c94aSIdo Barnea
180a551c94aSIdo Barnea	/* map the config before hugepage address so that we don't waste a page */
181a551c94aSIdo Barnea	if (internal_config.base_virtaddr != 0)
182a551c94aSIdo Barnea		rte_mem_cfg_addr = (void *)
183a551c94aSIdo Barnea			RTE_ALIGN_FLOOR(internal_config.base_virtaddr -
184a551c94aSIdo Barnea			sizeof(struct rte_mem_config), sysconf(_SC_PAGE_SIZE));
185a551c94aSIdo Barnea	else
186a551c94aSIdo Barnea		rte_mem_cfg_addr = NULL;
187a551c94aSIdo Barnea
188a551c94aSIdo Barnea	if (mem_cfg_fd < 0){
189a551c94aSIdo Barnea		mem_cfg_fd = open(pathname, O_RDWR | O_CREAT, 0660);
190a551c94aSIdo Barnea		if (mem_cfg_fd < 0)
191a551c94aSIdo Barnea			rte_panic("Cannot open '%s' for rte_mem_config\n", pathname);
192a551c94aSIdo Barnea	}
193a551c94aSIdo Barnea
194a551c94aSIdo Barnea	retval = ftruncate(mem_cfg_fd, sizeof(*rte_config.mem_config));
195a551c94aSIdo Barnea	if (retval < 0){
196a551c94aSIdo Barnea		close(mem_cfg_fd);
197a551c94aSIdo Barnea		rte_panic("Cannot resize '%s' for rte_mem_config\n", pathname);
198a551c94aSIdo Barnea	}
199a551c94aSIdo Barnea
200a551c94aSIdo Barnea	retval = fcntl(mem_cfg_fd, F_SETLK, &wr_lock);
201a551c94aSIdo Barnea	if (retval < 0){
202a551c94aSIdo Barnea		close(mem_cfg_fd);
203a551c94aSIdo Barnea		rte_exit(EXIT_FAILURE, "Cannot create lock on '%s'. Is another primary "
204a551c94aSIdo Barnea				"process running?\n", pathname);
205a551c94aSIdo Barnea	}
206a551c94aSIdo Barnea
207a551c94aSIdo Barnea	rte_mem_cfg_addr = mmap(rte_mem_cfg_addr, sizeof(*rte_config.mem_config),
208a551c94aSIdo Barnea				PROT_READ | PROT_WRITE, MAP_SHARED, mem_cfg_fd, 0);
209a551c94aSIdo Barnea
210a551c94aSIdo Barnea	if (rte_mem_cfg_addr == MAP_FAILED){
211a551c94aSIdo Barnea		rte_panic("Cannot mmap memory for rte_config\n");
212a551c94aSIdo Barnea	}
213a551c94aSIdo Barnea	memcpy(rte_mem_cfg_addr, &early_mem_config, sizeof(early_mem_config));
214a551c94aSIdo Barnea	rte_config.mem_config = (struct rte_mem_config *) rte_mem_cfg_addr;
215a551c94aSIdo Barnea
216a551c94aSIdo Barnea	/* store address of the config in the config itself so that secondary
217a551c94aSIdo Barnea	 * processes could later map the config into this exact location */
218a551c94aSIdo Barnea	rte_config.mem_config->mem_cfg_addr = (uintptr_t) rte_mem_cfg_addr;
219a551c94aSIdo Barnea
220a551c94aSIdo Barnea}
221a551c94aSIdo Barnea
222a551c94aSIdo Barnea/* attach to an existing shared memory config */
223a551c94aSIdo Barneastatic void
224a551c94aSIdo Barnearte_eal_config_attach(void)
225a551c94aSIdo Barnea{
226a551c94aSIdo Barnea	struct rte_mem_config *mem_config;
227a551c94aSIdo Barnea
228a551c94aSIdo Barnea	const char *pathname = eal_runtime_config_path();
229a551c94aSIdo Barnea
230a551c94aSIdo Barnea	if (internal_config.no_shconf)
231a551c94aSIdo Barnea		return;
232a551c94aSIdo Barnea
233a551c94aSIdo Barnea	if (mem_cfg_fd < 0){
234a551c94aSIdo Barnea		mem_cfg_fd = open(pathname, O_RDWR);
235a551c94aSIdo Barnea		if (mem_cfg_fd < 0)
236a551c94aSIdo Barnea			rte_panic("Cannot open '%s' for rte_mem_config\n", pathname);
237a551c94aSIdo Barnea	}
238a551c94aSIdo Barnea
239a551c94aSIdo Barnea	/* map it as read-only first */
240a551c94aSIdo Barnea	mem_config = (struct rte_mem_config *) mmap(NULL, sizeof(*mem_config),
241a551c94aSIdo Barnea			PROT_READ, MAP_SHARED, mem_cfg_fd, 0);
242a551c94aSIdo Barnea	if (mem_config == MAP_FAILED)
2439ca4a157SIdo Barnea		rte_panic("Cannot mmap memory for rte_config! error %i (%s)\n",
2449ca4a157SIdo Barnea			  errno, strerror(errno));
245a551c94aSIdo Barnea
246a551c94aSIdo Barnea	rte_config.mem_config = mem_config;
247a551c94aSIdo Barnea}
248a551c94aSIdo Barnea
249a551c94aSIdo Barnea/* reattach the shared config at exact memory location primary process has it */
250a551c94aSIdo Barneastatic void
251a551c94aSIdo Barnearte_eal_config_reattach(void)
252a551c94aSIdo Barnea{
253a551c94aSIdo Barnea	struct rte_mem_config *mem_config;
254a551c94aSIdo Barnea	void *rte_mem_cfg_addr;
255a551c94aSIdo Barnea
256a551c94aSIdo Barnea	if (internal_config.no_shconf)
257a551c94aSIdo Barnea		return;
258a551c94aSIdo Barnea
259a551c94aSIdo Barnea	/* save the address primary process has mapped shared config to */
260a551c94aSIdo Barnea	rte_mem_cfg_addr = (void *) (uintptr_t) rte_config.mem_config->mem_cfg_addr;
261a551c94aSIdo Barnea
262a551c94aSIdo Barnea	/* unmap original config */
263a551c94aSIdo Barnea	munmap(rte_config.mem_config, sizeof(struct rte_mem_config));
264a551c94aSIdo Barnea
265a551c94aSIdo Barnea	/* remap the config at proper address */
266a551c94aSIdo Barnea	mem_config = (struct rte_mem_config *) mmap(rte_mem_cfg_addr,
267a551c94aSIdo Barnea			sizeof(*mem_config), PROT_READ | PROT_WRITE, MAP_SHARED,
268a551c94aSIdo Barnea			mem_cfg_fd, 0);
2699ca4a157SIdo Barnea	if (mem_config == MAP_FAILED || mem_config != rte_mem_cfg_addr) {
2709ca4a157SIdo Barnea		if (mem_config != MAP_FAILED)
2719ca4a157SIdo Barnea			/* errno is stale, don't use */
2729ca4a157SIdo Barnea			rte_panic("Cannot mmap memory for rte_config at [%p], got [%p]"
2739ca4a157SIdo Barnea				  " - please use '--base-virtaddr' option\n",
2749ca4a157SIdo Barnea				  rte_mem_cfg_addr, mem_config);
2759ca4a157SIdo Barnea		else
2769ca4a157SIdo Barnea			rte_panic("Cannot mmap memory for rte_config! error %i (%s)\n",
2779ca4a157SIdo Barnea				  errno, strerror(errno));
2789ca4a157SIdo Barnea	}
279a551c94aSIdo Barnea	close(mem_cfg_fd);
280a551c94aSIdo Barnea
281a551c94aSIdo Barnea	rte_config.mem_config = mem_config;
282a551c94aSIdo Barnea}
283a551c94aSIdo Barnea
284a551c94aSIdo Barnea/* Detect if we are a primary or a secondary process */
285a551c94aSIdo Barneaenum rte_proc_type_t
286a551c94aSIdo Barneaeal_proc_type_detect(void)
287a551c94aSIdo Barnea{
288a551c94aSIdo Barnea	enum rte_proc_type_t ptype = RTE_PROC_PRIMARY;
289a551c94aSIdo Barnea	const char *pathname = eal_runtime_config_path();
290a551c94aSIdo Barnea
291a551c94aSIdo Barnea	/* if we can open the file but not get a write-lock we are a secondary
292a551c94aSIdo Barnea	 * process. NOTE: if we get a file handle back, we keep that open
293a551c94aSIdo Barnea	 * and don't close it to prevent a race condition between multiple opens */
294a551c94aSIdo Barnea	if (((mem_cfg_fd = open(pathname, O_RDWR)) >= 0) &&
295a551c94aSIdo Barnea			(fcntl(mem_cfg_fd, F_SETLK, &wr_lock) < 0))
296a551c94aSIdo Barnea		ptype = RTE_PROC_SECONDARY;
297a551c94aSIdo Barnea
298a551c94aSIdo Barnea	RTE_LOG(INFO, EAL, "Auto-detected process type: %s\n",
299a551c94aSIdo Barnea			ptype == RTE_PROC_PRIMARY ? "PRIMARY" : "SECONDARY");
300a551c94aSIdo Barnea
301a551c94aSIdo Barnea	return ptype;
302a551c94aSIdo Barnea}
303a551c94aSIdo Barnea
304a551c94aSIdo Barnea/* Sets up rte_config structure with the pointer to shared memory config.*/
305a551c94aSIdo Barneastatic void
306a551c94aSIdo Barnearte_config_init(void)
307a551c94aSIdo Barnea{
308a551c94aSIdo Barnea	rte_config.process_type = internal_config.process_type;
309a551c94aSIdo Barnea
310a551c94aSIdo Barnea	switch (rte_config.process_type){
311a551c94aSIdo Barnea	case RTE_PROC_PRIMARY:
312a551c94aSIdo Barnea		rte_eal_config_create();
313a551c94aSIdo Barnea		break;
314a551c94aSIdo Barnea	case RTE_PROC_SECONDARY:
315a551c94aSIdo Barnea		rte_eal_config_attach();
316a551c94aSIdo Barnea		rte_eal_mcfg_wait_complete(rte_config.mem_config);
317a551c94aSIdo Barnea		rte_eal_config_reattach();
318a551c94aSIdo Barnea		break;
319a551c94aSIdo Barnea	case RTE_PROC_AUTO:
320a551c94aSIdo Barnea	case RTE_PROC_INVALID:
321a551c94aSIdo Barnea		rte_panic("Invalid process type\n");
322a551c94aSIdo Barnea	}
323a551c94aSIdo Barnea}
324a551c94aSIdo Barnea
325a551c94aSIdo Barnea/* Unlocks hugepage directories that were locked by eal_hugepage_info_init */
326a551c94aSIdo Barneastatic void
327a551c94aSIdo Barneaeal_hugedirs_unlock(void)
328a551c94aSIdo Barnea{
329a551c94aSIdo Barnea	int i;
330a551c94aSIdo Barnea
331a551c94aSIdo Barnea	for (i = 0; i < MAX_HUGEPAGE_SIZES; i++)
332a551c94aSIdo Barnea	{
333a551c94aSIdo Barnea		/* skip uninitialized */
334a551c94aSIdo Barnea		if (internal_config.hugepage_info[i].lock_descriptor < 0)
335a551c94aSIdo Barnea			continue;
336a551c94aSIdo Barnea		/* unlock hugepage file */
337a551c94aSIdo Barnea		flock(internal_config.hugepage_info[i].lock_descriptor, LOCK_UN);
338a551c94aSIdo Barnea		close(internal_config.hugepage_info[i].lock_descriptor);
339a551c94aSIdo Barnea		/* reset the field */
340a551c94aSIdo Barnea		internal_config.hugepage_info[i].lock_descriptor = -1;
341a551c94aSIdo Barnea	}
342a551c94aSIdo Barnea}
343a551c94aSIdo Barnea
344a551c94aSIdo Barnea/* display usage */
345a551c94aSIdo Barneastatic void
346a551c94aSIdo Barneaeal_usage(const char *prgname)
347a551c94aSIdo Barnea{
348a551c94aSIdo Barnea	printf("\nUsage: %s ", prgname);
349a551c94aSIdo Barnea	eal_common_usage();
350a551c94aSIdo Barnea	printf("EAL Linux options:\n"
351a551c94aSIdo Barnea	       "  --"OPT_SOCKET_MEM"        Memory to allocate on sockets (comma separated values)\n"
352a551c94aSIdo Barnea	       "  --"OPT_HUGE_DIR"          Directory where hugetlbfs is mounted\n"
353a551c94aSIdo Barnea	       "  --"OPT_FILE_PREFIX"       Prefix for hugepage filenames\n"
354a551c94aSIdo Barnea	       "  --"OPT_BASE_VIRTADDR"     Base virtual address\n"
355a551c94aSIdo Barnea	       "  --"OPT_CREATE_UIO_DEV"    Create /dev/uioX (usually done by hotplug)\n"
356a551c94aSIdo Barnea	       "  --"OPT_VFIO_INTR"         Interrupt mode for VFIO (legacy|msi|msix)\n"
357a551c94aSIdo Barnea	       "  --"OPT_XEN_DOM0"          Support running on Xen dom0 without hugetlbfs\n"
358a551c94aSIdo Barnea	       "\n");
359a551c94aSIdo Barnea	/* Allow the application to print its usage message too if hook is set */
360a551c94aSIdo Barnea	if ( rte_application_usage_hook ) {
361a551c94aSIdo Barnea		printf("===== Application Usage =====\n\n");
362a551c94aSIdo Barnea		rte_application_usage_hook(prgname);
363a551c94aSIdo Barnea	}
364a551c94aSIdo Barnea}
365a551c94aSIdo Barnea
366a551c94aSIdo Barnea/* Set a per-application usage message */
367a551c94aSIdo Barnearte_usage_hook_t
368a551c94aSIdo Barnearte_set_application_usage_hook( rte_usage_hook_t usage_func )
369a551c94aSIdo Barnea{
370a551c94aSIdo Barnea	rte_usage_hook_t	old_func;
371a551c94aSIdo Barnea
372a551c94aSIdo Barnea	/* Will be NULL on the first call to denote the last usage routine. */
373a551c94aSIdo Barnea	old_func					= rte_application_usage_hook;
374a551c94aSIdo Barnea	rte_application_usage_hook	= usage_func;
375a551c94aSIdo Barnea
376a551c94aSIdo Barnea	return old_func;
377a551c94aSIdo Barnea}
378a551c94aSIdo Barnea
379a551c94aSIdo Barneastatic int
380a551c94aSIdo Barneaeal_parse_socket_mem(char *socket_mem)
381a551c94aSIdo Barnea{
382a551c94aSIdo Barnea	char * arg[RTE_MAX_NUMA_NODES];
383a551c94aSIdo Barnea	char *end;
384a551c94aSIdo Barnea	int arg_num, i, len;
385a551c94aSIdo Barnea	uint64_t total_mem = 0;
386a551c94aSIdo Barnea
387a551c94aSIdo Barnea	len = strnlen(socket_mem, SOCKET_MEM_STRLEN);
388a551c94aSIdo Barnea	if (len == SOCKET_MEM_STRLEN) {
389a551c94aSIdo Barnea		RTE_LOG(ERR, EAL, "--socket-mem is too long\n");
390a551c94aSIdo Barnea		return -1;
391a551c94aSIdo Barnea	}
392a551c94aSIdo Barnea
393a551c94aSIdo Barnea	/* all other error cases will be caught later */
394a551c94aSIdo Barnea	if (!isdigit(socket_mem[len-1]))
395a551c94aSIdo Barnea		return -1;
396a551c94aSIdo Barnea
397a551c94aSIdo Barnea	/* split the optarg into separate socket values */
398a551c94aSIdo Barnea	arg_num = rte_strsplit(socket_mem, len,
399a551c94aSIdo Barnea			arg, RTE_MAX_NUMA_NODES, ',');
400a551c94aSIdo Barnea
401a551c94aSIdo Barnea	/* if split failed, or 0 arguments */
402a551c94aSIdo Barnea	if (arg_num <= 0)
403a551c94aSIdo Barnea		return -1;
404a551c94aSIdo Barnea
405a551c94aSIdo Barnea	internal_config.force_sockets = 1;
406a551c94aSIdo Barnea
407a551c94aSIdo Barnea	/* parse each defined socket option */
408a551c94aSIdo Barnea	errno = 0;
409a551c94aSIdo Barnea	for (i = 0; i < arg_num; i++) {
410a551c94aSIdo Barnea		end = NULL;
411a551c94aSIdo Barnea		internal_config.socket_mem[i] = strtoull(arg[i], &end, 10);
412a551c94aSIdo Barnea
413a551c94aSIdo Barnea		/* check for invalid input */
414a551c94aSIdo Barnea		if ((errno != 0)  ||
415a551c94aSIdo Barnea				(arg[i][0] == '\0') || (end == NULL) || (*end != '\0'))
416a551c94aSIdo Barnea			return -1;
417a551c94aSIdo Barnea		internal_config.socket_mem[i] *= 1024ULL;
418a551c94aSIdo Barnea		internal_config.socket_mem[i] *= 1024ULL;
419a551c94aSIdo Barnea		total_mem += internal_config.socket_mem[i];
420a551c94aSIdo Barnea	}
421a551c94aSIdo Barnea
422a551c94aSIdo Barnea	/* check if we have a positive amount of total memory */
423a551c94aSIdo Barnea	if (total_mem == 0)
424a551c94aSIdo Barnea		return -1;
425a551c94aSIdo Barnea
426a551c94aSIdo Barnea	return 0;
427a551c94aSIdo Barnea}
428a551c94aSIdo Barnea
429a551c94aSIdo Barneastatic int
430a551c94aSIdo Barneaeal_parse_base_virtaddr(const char *arg)
431a551c94aSIdo Barnea{
432a551c94aSIdo Barnea	char *end;
433a551c94aSIdo Barnea	uint64_t addr;
434a551c94aSIdo Barnea
435a551c94aSIdo Barnea	errno = 0;
436a551c94aSIdo Barnea	addr = strtoull(arg, &end, 16);
437a551c94aSIdo Barnea
438a551c94aSIdo Barnea	/* check for errors */
439a551c94aSIdo Barnea	if ((errno != 0) || (arg[0] == '\0') || end == NULL || (*end != '\0'))
440a551c94aSIdo Barnea		return -1;
441a551c94aSIdo Barnea
442a551c94aSIdo Barnea	/* make sure we don't exceed 32-bit boundary on 32-bit target */
443a551c94aSIdo Barnea#ifndef RTE_ARCH_64
444a551c94aSIdo Barnea	if (addr >= UINTPTR_MAX)
445a551c94aSIdo Barnea		return -1;
446a551c94aSIdo Barnea#endif
447a551c94aSIdo Barnea
448a551c94aSIdo Barnea	/* align the addr on 16M boundary, 16MB is the minimum huge page
449a551c94aSIdo Barnea	 * size on IBM Power architecture. If the addr is aligned to 16MB,
450a551c94aSIdo Barnea	 * it can align to 2MB for x86. So this alignment can also be used
451a551c94aSIdo Barnea	 * on x86 */
452a551c94aSIdo Barnea	internal_config.base_virtaddr =
453a551c94aSIdo Barnea		RTE_PTR_ALIGN_CEIL((uintptr_t)addr, (size_t)RTE_PGSIZE_16M);
454a551c94aSIdo Barnea
455a551c94aSIdo Barnea	return 0;
456a551c94aSIdo Barnea}
457a551c94aSIdo Barnea
458a551c94aSIdo Barneastatic int
459a551c94aSIdo Barneaeal_parse_vfio_intr(const char *mode)
460a551c94aSIdo Barnea{
461a551c94aSIdo Barnea	unsigned i;
462a551c94aSIdo Barnea	static struct {
463a551c94aSIdo Barnea		const char *name;
464a551c94aSIdo Barnea		enum rte_intr_mode value;
465a551c94aSIdo Barnea	} map[] = {
466a551c94aSIdo Barnea		{ "legacy", RTE_INTR_MODE_LEGACY },
467a551c94aSIdo Barnea		{ "msi", RTE_INTR_MODE_MSI },
468a551c94aSIdo Barnea		{ "msix", RTE_INTR_MODE_MSIX },
469a551c94aSIdo Barnea	};
470a551c94aSIdo Barnea
471a551c94aSIdo Barnea	for (i = 0; i < RTE_DIM(map); i++) {
472a551c94aSIdo Barnea		if (!strcmp(mode, map[i].name)) {
473a551c94aSIdo Barnea			internal_config.vfio_intr_mode = map[i].value;
474a551c94aSIdo Barnea			return 0;
475a551c94aSIdo Barnea		}
476a551c94aSIdo Barnea	}
477a551c94aSIdo Barnea	return -1;
478a551c94aSIdo Barnea}
479a551c94aSIdo Barnea
480a551c94aSIdo Barnea/* Parse the arguments for --log-level only */
481a551c94aSIdo Barneastatic void
482a551c94aSIdo Barneaeal_log_level_parse(int argc, char **argv)
483a551c94aSIdo Barnea{
484a551c94aSIdo Barnea	int opt;
485a551c94aSIdo Barnea	char **argvopt;
486a551c94aSIdo Barnea	int option_index;
487a551c94aSIdo Barnea	const int old_optind = optind;
488a551c94aSIdo Barnea	const int old_optopt = optopt;
489a551c94aSIdo Barnea	char * const old_optarg = optarg;
490a551c94aSIdo Barnea
491a551c94aSIdo Barnea	argvopt = argv;
492a551c94aSIdo Barnea	optind = 1;
493a551c94aSIdo Barnea
494a551c94aSIdo Barnea	eal_reset_internal_config(&internal_config);
495a551c94aSIdo Barnea
496a551c94aSIdo Barnea	while ((opt = getopt_long(argc, argvopt, eal_short_options,
497a551c94aSIdo Barnea				  eal_long_options, &option_index)) != EOF) {
498a551c94aSIdo Barnea
499a551c94aSIdo Barnea		int ret;
500a551c94aSIdo Barnea
501a551c94aSIdo Barnea		/* getopt is not happy, stop right now */
502a551c94aSIdo Barnea		if (opt == '?')
503a551c94aSIdo Barnea			break;
504a551c94aSIdo Barnea
505a551c94aSIdo Barnea		ret = (opt == OPT_LOG_LEVEL_NUM) ?
506a551c94aSIdo Barnea			eal_parse_common_option(opt, optarg, &internal_config) : 0;
507a551c94aSIdo Barnea
508a551c94aSIdo Barnea		/* common parser is not happy */
509a551c94aSIdo Barnea		if (ret < 0)
510a551c94aSIdo Barnea			break;
511a551c94aSIdo Barnea	}
512a551c94aSIdo Barnea
513a551c94aSIdo Barnea	/* restore getopt lib */
514a551c94aSIdo Barnea	optind = old_optind;
515a551c94aSIdo Barnea	optopt = old_optopt;
516a551c94aSIdo Barnea	optarg = old_optarg;
517a551c94aSIdo Barnea}
518a551c94aSIdo Barnea
519a551c94aSIdo Barnea/* Parse the argument given in the command line of the application */
520a551c94aSIdo Barneastatic int
521a551c94aSIdo Barneaeal_parse_args(int argc, char **argv)
522a551c94aSIdo Barnea{
523a551c94aSIdo Barnea	int opt, ret;
524a551c94aSIdo Barnea	char **argvopt;
525a551c94aSIdo Barnea	int option_index;
526a551c94aSIdo Barnea	char *prgname = argv[0];
527a551c94aSIdo Barnea	const int old_optind = optind;
528a551c94aSIdo Barnea	const int old_optopt = optopt;
529a551c94aSIdo Barnea	char * const old_optarg = optarg;
530a551c94aSIdo Barnea
531a551c94aSIdo Barnea	argvopt = argv;
532a551c94aSIdo Barnea	optind = 1;
533a551c94aSIdo Barnea
534a551c94aSIdo Barnea	while ((opt = getopt_long(argc, argvopt, eal_short_options,
535a551c94aSIdo Barnea				  eal_long_options, &option_index)) != EOF) {
536a551c94aSIdo Barnea
537a551c94aSIdo Barnea		/* getopt is not happy, stop right now */
538a551c94aSIdo Barnea		if (opt == '?') {
539a551c94aSIdo Barnea			eal_usage(prgname);
540a551c94aSIdo Barnea			ret = -1;
541a551c94aSIdo Barnea			goto out;
542a551c94aSIdo Barnea		}
543a551c94aSIdo Barnea
544a551c94aSIdo Barnea		ret = eal_parse_common_option(opt, optarg, &internal_config);
545a551c94aSIdo Barnea		/* common parser is not happy */
546a551c94aSIdo Barnea		if (ret < 0) {
547a551c94aSIdo Barnea			eal_usage(prgname);
548a551c94aSIdo Barnea			ret = -1;
549a551c94aSIdo Barnea			goto out;
550a551c94aSIdo Barnea		}
551a551c94aSIdo Barnea		/* common parser handled this option */
552a551c94aSIdo Barnea		if (ret == 0)
553a551c94aSIdo Barnea			continue;
554a551c94aSIdo Barnea
555a551c94aSIdo Barnea		switch (opt) {
556a551c94aSIdo Barnea		case 'h':
557a551c94aSIdo Barnea			eal_usage(prgname);
558a551c94aSIdo Barnea			exit(EXIT_SUCCESS);
559a551c94aSIdo Barnea
560a551c94aSIdo Barnea		/* long options */
561a551c94aSIdo Barnea		case OPT_XEN_DOM0_NUM:
562a551c94aSIdo Barnea#ifdef RTE_LIBRTE_XEN_DOM0
563a551c94aSIdo Barnea			internal_config.xen_dom0_support = 1;
564a551c94aSIdo Barnea#else
565a551c94aSIdo Barnea			RTE_LOG(ERR, EAL, "Can't support DPDK app "
566a551c94aSIdo Barnea				"running on Dom0, please configure"
567a551c94aSIdo Barnea				" RTE_LIBRTE_XEN_DOM0=y\n");
568a551c94aSIdo Barnea			ret = -1;
569a551c94aSIdo Barnea			goto out;
570a551c94aSIdo Barnea#endif
571a551c94aSIdo Barnea			break;
572a551c94aSIdo Barnea
573a551c94aSIdo Barnea		case OPT_HUGE_DIR_NUM:
574a551c94aSIdo Barnea			internal_config.hugepage_dir = optarg;
575a551c94aSIdo Barnea			break;
576a551c94aSIdo Barnea
577a551c94aSIdo Barnea		case OPT_FILE_PREFIX_NUM:
578a551c94aSIdo Barnea			internal_config.hugefile_prefix = optarg;
579a551c94aSIdo Barnea			break;
580a551c94aSIdo Barnea
581a551c94aSIdo Barnea		case OPT_SOCKET_MEM_NUM:
582a551c94aSIdo Barnea			if (eal_parse_socket_mem(optarg) < 0) {
583a551c94aSIdo Barnea				RTE_LOG(ERR, EAL, "invalid parameters for --"
584a551c94aSIdo Barnea						OPT_SOCKET_MEM "\n");
585a551c94aSIdo Barnea				eal_usage(prgname);
586a551c94aSIdo Barnea				ret = -1;
587a551c94aSIdo Barnea				goto out;
588a551c94aSIdo Barnea			}
589a551c94aSIdo Barnea			break;
590a551c94aSIdo Barnea
591a551c94aSIdo Barnea		case OPT_BASE_VIRTADDR_NUM:
592a551c94aSIdo Barnea			if (eal_parse_base_virtaddr(optarg) < 0) {
593a551c94aSIdo Barnea				RTE_LOG(ERR, EAL, "invalid parameter for --"
594a551c94aSIdo Barnea						OPT_BASE_VIRTADDR "\n");
595a551c94aSIdo Barnea				eal_usage(prgname);
596a551c94aSIdo Barnea				ret = -1;
597a551c94aSIdo Barnea				goto out;
598a551c94aSIdo Barnea			}
599a551c94aSIdo Barnea			break;
600a551c94aSIdo Barnea
601a551c94aSIdo Barnea		case OPT_VFIO_INTR_NUM:
602a551c94aSIdo Barnea			if (eal_parse_vfio_intr(optarg) < 0) {
603a551c94aSIdo Barnea				RTE_LOG(ERR, EAL, "invalid parameters for --"
604a551c94aSIdo Barnea						OPT_VFIO_INTR "\n");
605a551c94aSIdo Barnea				eal_usage(prgname);
606a551c94aSIdo Barnea				ret = -1;
607a551c94aSIdo Barnea				goto out;
608a551c94aSIdo Barnea			}
609a551c94aSIdo Barnea			break;
610a551c94aSIdo Barnea
611a551c94aSIdo Barnea		case OPT_CREATE_UIO_DEV_NUM:
612a551c94aSIdo Barnea			internal_config.create_uio_dev = 1;
613a551c94aSIdo Barnea			break;
614a551c94aSIdo Barnea
615a551c94aSIdo Barnea		default:
616a551c94aSIdo Barnea			if (opt < OPT_LONG_MIN_NUM && isprint(opt)) {
617a551c94aSIdo Barnea				RTE_LOG(ERR, EAL, "Option %c is not supported "
618a551c94aSIdo Barnea					"on Linux\n", opt);
619a551c94aSIdo Barnea			} else if (opt >= OPT_LONG_MIN_NUM &&
620a551c94aSIdo Barnea				   opt < OPT_LONG_MAX_NUM) {
621a551c94aSIdo Barnea				RTE_LOG(ERR, EAL, "Option %s is not supported "
622a551c94aSIdo Barnea					"on Linux\n",
623a551c94aSIdo Barnea					eal_long_options[option_index].name);
624a551c94aSIdo Barnea			} else {
625a551c94aSIdo Barnea				RTE_LOG(ERR, EAL, "Option %d is not supported "
626a551c94aSIdo Barnea					"on Linux\n", opt);
627a551c94aSIdo Barnea			}
628a551c94aSIdo Barnea			eal_usage(prgname);
629a551c94aSIdo Barnea			ret = -1;
630a551c94aSIdo Barnea			goto out;
631a551c94aSIdo Barnea		}
632a551c94aSIdo Barnea	}
633a551c94aSIdo Barnea
634a551c94aSIdo Barnea	if (eal_adjust_config(&internal_config) != 0) {
635a551c94aSIdo Barnea		ret = -1;
636a551c94aSIdo Barnea		goto out;
637a551c94aSIdo Barnea	}
638a551c94aSIdo Barnea
639a551c94aSIdo Barnea	/* sanity checks */
640a551c94aSIdo Barnea	if (eal_check_common_options(&internal_config) != 0) {
641a551c94aSIdo Barnea		eal_usage(prgname);
642a551c94aSIdo Barnea		ret = -1;
643a551c94aSIdo Barnea		goto out;
644a551c94aSIdo Barnea	}
645a551c94aSIdo Barnea
646a551c94aSIdo Barnea	/* --xen-dom0 doesn't make sense with --socket-mem */
647a551c94aSIdo Barnea	if (internal_config.xen_dom0_support && internal_config.force_sockets == 1) {
648a551c94aSIdo Barnea		RTE_LOG(ERR, EAL, "Options --"OPT_SOCKET_MEM" cannot be specified "
649a551c94aSIdo Barnea			"together with --"OPT_XEN_DOM0"\n");
650a551c94aSIdo Barnea		eal_usage(prgname);
651a551c94aSIdo Barnea		ret = -1;
652a551c94aSIdo Barnea		goto out;
653a551c94aSIdo Barnea	}
654a551c94aSIdo Barnea
655a551c94aSIdo Barnea	if (optind >= 0)
656a551c94aSIdo Barnea		argv[optind-1] = prgname;
657a551c94aSIdo Barnea	ret = optind-1;
658a551c94aSIdo Barnea
659a551c94aSIdo Barneaout:
660a551c94aSIdo Barnea	/* restore getopt lib */
661a551c94aSIdo Barnea	optind = old_optind;
662a551c94aSIdo Barnea	optopt = old_optopt;
663a551c94aSIdo Barnea	optarg = old_optarg;
664a551c94aSIdo Barnea
665a551c94aSIdo Barnea	return ret;
666a551c94aSIdo Barnea}
667a551c94aSIdo Barnea
668a551c94aSIdo Barneastatic void
669a551c94aSIdo Barneaeal_check_mem_on_local_socket(void)
670a551c94aSIdo Barnea{
671a551c94aSIdo Barnea	const struct rte_memseg *ms;
672a551c94aSIdo Barnea	int i, socket_id;
673a551c94aSIdo Barnea
674a551c94aSIdo Barnea	socket_id = rte_lcore_to_socket_id(rte_config.master_lcore);
675a551c94aSIdo Barnea
676a551c94aSIdo Barnea	ms = rte_eal_get_physmem_layout();
677a551c94aSIdo Barnea
678a551c94aSIdo Barnea	for (i = 0; i < RTE_MAX_MEMSEG; i++)
679a551c94aSIdo Barnea		if (ms[i].socket_id == socket_id &&
680a551c94aSIdo Barnea				ms[i].len > 0)
681a551c94aSIdo Barnea			return;
682a551c94aSIdo Barnea
683a551c94aSIdo Barnea	RTE_LOG(WARNING, EAL, "WARNING: Master core has no "
684a551c94aSIdo Barnea			"memory on local socket!\n");
685a551c94aSIdo Barnea}
686a551c94aSIdo Barnea
687a551c94aSIdo Barneastatic int
688a551c94aSIdo Barneasync_func(__attribute__((unused)) void *arg)
689a551c94aSIdo Barnea{
690a551c94aSIdo Barnea	return 0;
691a551c94aSIdo Barnea}
692a551c94aSIdo Barnea
693a551c94aSIdo Barneainline static void
694a551c94aSIdo Barnearte_eal_mcfg_complete(void)
695a551c94aSIdo Barnea{
696a551c94aSIdo Barnea	/* ALL shared mem_config related INIT DONE */
697a551c94aSIdo Barnea	if (rte_config.process_type == RTE_PROC_PRIMARY)
698a551c94aSIdo Barnea		rte_config.mem_config->magic = RTE_MAGIC;
699a551c94aSIdo Barnea}
700a551c94aSIdo Barnea
701a551c94aSIdo Barnea/*
702a551c94aSIdo Barnea * Request iopl privilege for all RPL, returns 0 on success
703a551c94aSIdo Barnea * iopl() call is mostly for the i386 architecture. For other architectures,
704a551c94aSIdo Barnea * return -1 to indicate IO privilege can't be changed in this way.
705a551c94aSIdo Barnea */
706a551c94aSIdo Barneaint
707a551c94aSIdo Barnearte_eal_iopl_init(void)
708a551c94aSIdo Barnea{
709a551c94aSIdo Barnea#if defined(RTE_ARCH_X86)
710a551c94aSIdo Barnea	if (iopl(3) != 0)
711a551c94aSIdo Barnea		return -1;
712a551c94aSIdo Barnea#endif
713a551c94aSIdo Barnea	return 0;
714a551c94aSIdo Barnea}
715a551c94aSIdo Barnea
716a551c94aSIdo Barnea#ifdef VFIO_PRESENT
717a551c94aSIdo Barneastatic int rte_eal_vfio_setup(void)
718a551c94aSIdo Barnea{
719a551c94aSIdo Barnea	int vfio_enabled = 0;
720a551c94aSIdo Barnea
721a551c94aSIdo Barnea	if (!internal_config.no_pci) {
722a551c94aSIdo Barnea		pci_vfio_enable();
723a551c94aSIdo Barnea		vfio_enabled |= pci_vfio_is_enabled();
724a551c94aSIdo Barnea	}
725a551c94aSIdo Barnea
726a551c94aSIdo Barnea	if (vfio_enabled) {
727a551c94aSIdo Barnea
728a551c94aSIdo Barnea		/* if we are primary process, create a thread to communicate with
729a551c94aSIdo Barnea		 * secondary processes. the thread will use a socket to wait for
730a551c94aSIdo Barnea		 * requests from secondary process to send open file descriptors,
731a551c94aSIdo Barnea		 * because VFIO does not allow multiple open descriptors on a group or
732a551c94aSIdo Barnea		 * VFIO container.
733a551c94aSIdo Barnea		 */
734a551c94aSIdo Barnea		if (internal_config.process_type == RTE_PROC_PRIMARY &&
735a551c94aSIdo Barnea				vfio_mp_sync_setup() < 0)
736a551c94aSIdo Barnea			return -1;
737a551c94aSIdo Barnea	}
738a551c94aSIdo Barnea
739a551c94aSIdo Barnea	return 0;
740a551c94aSIdo Barnea}
741a551c94aSIdo Barnea#endif
742a551c94aSIdo Barnea
743a551c94aSIdo Barnea/* Launch threads, called at application init(). */
744a551c94aSIdo Barneaint
745a551c94aSIdo Barnearte_eal_init(int argc, char **argv)
746a551c94aSIdo Barnea{
747a551c94aSIdo Barnea	int i, fctret, ret;
748a551c94aSIdo Barnea	pthread_t thread_id;
749a551c94aSIdo Barnea	static rte_atomic32_t run_once = RTE_ATOMIC32_INIT(0);
750a551c94aSIdo Barnea	const char *logid;
751a551c94aSIdo Barnea	char cpuset[RTE_CPU_AFFINITY_STR_LEN];
752a551c94aSIdo Barnea	char thread_name[RTE_MAX_THREAD_NAME_LEN];
753a551c94aSIdo Barnea
7549ca4a157SIdo Barnea	/* checks if the machine is adequate */
7559ca4a157SIdo Barnea	rte_cpu_check_supported();
7569ca4a157SIdo Barnea
757a551c94aSIdo Barnea	if (!rte_atomic32_test_and_set(&run_once))
758a551c94aSIdo Barnea		return -1;
759a551c94aSIdo Barnea
760a551c94aSIdo Barnea	logid = strrchr(argv[0], '/');
761a551c94aSIdo Barnea	logid = strdup(logid ? logid + 1: argv[0]);
762a551c94aSIdo Barnea
763a551c94aSIdo Barnea	thread_id = pthread_self();
764a551c94aSIdo Barnea
765a551c94aSIdo Barnea	eal_log_level_parse(argc, argv);
766a551c94aSIdo Barnea
767a551c94aSIdo Barnea	/* set log level as early as possible */
768a551c94aSIdo Barnea	rte_set_log_level(internal_config.log_level);
769a551c94aSIdo Barnea
770a551c94aSIdo Barnea	if (rte_eal_cpu_init() < 0)
771a551c94aSIdo Barnea		rte_panic("Cannot detect lcores\n");
772a551c94aSIdo Barnea
773a551c94aSIdo Barnea	fctret = eal_parse_args(argc, argv);
774a551c94aSIdo Barnea	if (fctret < 0)
775a551c94aSIdo Barnea		exit(1);
776a551c94aSIdo Barnea
777a551c94aSIdo Barnea	if (internal_config.no_hugetlbfs == 0 &&
778a551c94aSIdo Barnea			internal_config.process_type != RTE_PROC_SECONDARY &&
779a551c94aSIdo Barnea			internal_config.xen_dom0_support == 0 &&
780a551c94aSIdo Barnea			eal_hugepage_info_init() < 0)
781a551c94aSIdo Barnea		rte_panic("Cannot get hugepage information\n");
782a551c94aSIdo Barnea
783a551c94aSIdo Barnea	if (internal_config.memory == 0 && internal_config.force_sockets == 0) {
784a551c94aSIdo Barnea		if (internal_config.no_hugetlbfs)
785a551c94aSIdo Barnea			internal_config.memory = MEMSIZE_IF_NO_HUGE_PAGE;
786a551c94aSIdo Barnea	}
787a551c94aSIdo Barnea
788a551c94aSIdo Barnea	if (internal_config.vmware_tsc_map == 1) {
789a551c94aSIdo Barnea#ifdef RTE_LIBRTE_EAL_VMWARE_TSC_MAP_SUPPORT
790a551c94aSIdo Barnea		rte_cycles_vmware_tsc_map = 1;
791a551c94aSIdo Barnea		RTE_LOG (DEBUG, EAL, "Using VMWARE TSC MAP, "
792a551c94aSIdo Barnea				"you must have monitor_control.pseudo_perfctr = TRUE\n");
793a551c94aSIdo Barnea#else
794a551c94aSIdo Barnea		RTE_LOG (WARNING, EAL, "Ignoring --vmware-tsc-map because "
795a551c94aSIdo Barnea				"RTE_LIBRTE_EAL_VMWARE_TSC_MAP_SUPPORT is not set\n");
796a551c94aSIdo Barnea#endif
797a551c94aSIdo Barnea	}
798a551c94aSIdo Barnea
799a551c94aSIdo Barnea	rte_srand(rte_rdtsc());
800a551c94aSIdo Barnea
801a551c94aSIdo Barnea	rte_config_init();
802a551c94aSIdo Barnea
8039ca4a157SIdo Barnea	if (rte_eal_log_init(logid, internal_config.syslog_facility) < 0)
8049ca4a157SIdo Barnea		rte_panic("Cannot init logs\n");
8059ca4a157SIdo Barnea
806a551c94aSIdo Barnea	if (rte_eal_pci_init() < 0)
807a551c94aSIdo Barnea		rte_panic("Cannot init PCI\n");
808a551c94aSIdo Barnea
809a551c94aSIdo Barnea#ifdef VFIO_PRESENT
810a551c94aSIdo Barnea	if (rte_eal_vfio_setup() < 0)
811a551c94aSIdo Barnea		rte_panic("Cannot init VFIO\n");
812a551c94aSIdo Barnea#endif
813a551c94aSIdo Barnea
814a551c94aSIdo Barnea	if (rte_eal_memory_init() < 0)
815a551c94aSIdo Barnea		rte_panic("Cannot init memory\n");
816a551c94aSIdo Barnea
817a551c94aSIdo Barnea	/* the directories are locked during eal_hugepage_info_init */
818a551c94aSIdo Barnea	eal_hugedirs_unlock();
819a551c94aSIdo Barnea
820a551c94aSIdo Barnea	if (rte_eal_memzone_init() < 0)
821a551c94aSIdo Barnea		rte_panic("Cannot init memzone\n");
822a551c94aSIdo Barnea
823a551c94aSIdo Barnea	if (rte_eal_tailqs_init() < 0)
824a551c94aSIdo Barnea		rte_panic("Cannot init tail queues for objects\n");
825a551c94aSIdo Barnea
826a551c94aSIdo Barnea	if (rte_eal_alarm_init() < 0)
827a551c94aSIdo Barnea		rte_panic("Cannot init interrupt-handling thread\n");
828a551c94aSIdo Barnea
829a551c94aSIdo Barnea	if (rte_eal_timer_init() < 0)
830a551c94aSIdo Barnea		rte_panic("Cannot init HPET or TSC timers\n");
831a551c94aSIdo Barnea
832a551c94aSIdo Barnea	eal_check_mem_on_local_socket();
833a551c94aSIdo Barnea
834a551c94aSIdo Barnea	if (eal_plugins_init() < 0)
835a551c94aSIdo Barnea		rte_panic("Cannot init plugins\n");
836a551c94aSIdo Barnea
837a551c94aSIdo Barnea	eal_thread_init_master(rte_config.master_lcore);
838a551c94aSIdo Barnea
839a551c94aSIdo Barnea	ret = eal_thread_dump_affinity(cpuset, RTE_CPU_AFFINITY_STR_LEN);
840a551c94aSIdo Barnea
841a551c94aSIdo Barnea	RTE_LOG(DEBUG, EAL, "Master lcore %u is ready (tid=%x;cpuset=[%s%s])\n",
842a551c94aSIdo Barnea		rte_config.master_lcore, (int)thread_id, cpuset,
843a551c94aSIdo Barnea		ret == 0 ? "" : "...");
844a551c94aSIdo Barnea
845a551c94aSIdo Barnea	if (rte_eal_intr_init() < 0)
846a551c94aSIdo Barnea		rte_panic("Cannot init interrupt-handling thread\n");
847a551c94aSIdo Barnea
8489ca4a157SIdo Barnea	if (rte_bus_scan())
8499ca4a157SIdo Barnea		rte_panic("Cannot scan the buses for devices\n");
8509ca4a157SIdo Barnea
851a551c94aSIdo Barnea	RTE_LCORE_FOREACH_SLAVE(i) {
852a551c94aSIdo Barnea
853a551c94aSIdo Barnea		/*
854a551c94aSIdo Barnea		 * create communication pipes between master thread
855a551c94aSIdo Barnea		 * and children
856a551c94aSIdo Barnea		 */
857a551c94aSIdo Barnea		if (pipe(lcore_config[i].pipe_master2slave) < 0)
858a551c94aSIdo Barnea			rte_panic("Cannot create pipe\n");
859a551c94aSIdo Barnea		if (pipe(lcore_config[i].pipe_slave2master) < 0)
860a551c94aSIdo Barnea			rte_panic("Cannot create pipe\n");
861a551c94aSIdo Barnea
862a551c94aSIdo Barnea		lcore_config[i].state = WAIT;
863a551c94aSIdo Barnea
864a551c94aSIdo Barnea		/* create a thread for each lcore */
865a551c94aSIdo Barnea		ret = pthread_create(&lcore_config[i].thread_id, NULL,
866a551c94aSIdo Barnea				     eal_thread_loop, NULL);
867a551c94aSIdo Barnea		if (ret != 0)
868a551c94aSIdo Barnea			rte_panic("Cannot create thread\n");
869a551c94aSIdo Barnea
870a551c94aSIdo Barnea		/* Set thread_name for aid in debugging. */
871a551c94aSIdo Barnea		snprintf(thread_name, RTE_MAX_THREAD_NAME_LEN,
872a551c94aSIdo Barnea			"lcore-slave-%d", i);
873a551c94aSIdo Barnea		ret = rte_thread_setname(lcore_config[i].thread_id,
874a551c94aSIdo Barnea						thread_name);
875a551c94aSIdo Barnea		if (ret != 0)
876a551c94aSIdo Barnea			RTE_LOG(DEBUG, EAL,
877a551c94aSIdo Barnea				"Cannot set name for lcore thread\n");
878a551c94aSIdo Barnea	}
879a551c94aSIdo Barnea
880a551c94aSIdo Barnea	/*
881a551c94aSIdo Barnea	 * Launch a dummy function on all slave lcores, so that master lcore
882a551c94aSIdo Barnea	 * knows they are all ready when this function returns.
883a551c94aSIdo Barnea	 */
884a551c94aSIdo Barnea	rte_eal_mp_remote_launch(sync_func, NULL, SKIP_MASTER);
885a551c94aSIdo Barnea	rte_eal_mp_wait_lcore();
886a551c94aSIdo Barnea
8879ca4a157SIdo Barnea	/* Probe all the buses and devices/drivers on them */
8889ca4a157SIdo Barnea	if (rte_bus_probe())
8899ca4a157SIdo Barnea		rte_panic("Cannot probe devices\n");
8909ca4a157SIdo Barnea
891a551c94aSIdo Barnea	/* Probe & Initialize PCI devices */
892a551c94aSIdo Barnea	if (rte_eal_pci_probe())
893a551c94aSIdo Barnea		rte_panic("Cannot probe PCI\n");
894a551c94aSIdo Barnea
8959ca4a157SIdo Barnea	if (rte_eal_dev_init() < 0)
8969ca4a157SIdo Barnea		rte_panic("Cannot init pmd devices\n");
8979ca4a157SIdo Barnea
898a551c94aSIdo Barnea	rte_eal_mcfg_complete();
899a551c94aSIdo Barnea
900a551c94aSIdo Barnea	return fctret;
901a551c94aSIdo Barnea}
902a551c94aSIdo Barnea
903a551c94aSIdo Barnea/* get core role */
904a551c94aSIdo Barneaenum rte_lcore_role_t
905a551c94aSIdo Barnearte_eal_lcore_role(unsigned lcore_id)
906a551c94aSIdo Barnea{
907a551c94aSIdo Barnea	return rte_config.lcore_role[lcore_id];
908a551c94aSIdo Barnea}
909a551c94aSIdo Barnea
910a551c94aSIdo Barneaenum rte_proc_type_t
911a551c94aSIdo Barnearte_eal_process_type(void)
912a551c94aSIdo Barnea{
913a551c94aSIdo Barnea	return rte_config.process_type;
914a551c94aSIdo Barnea}
915a551c94aSIdo Barnea
916a551c94aSIdo Barneaint rte_eal_has_hugepages(void)
917a551c94aSIdo Barnea{
918a551c94aSIdo Barnea	return ! internal_config.no_hugetlbfs;
919a551c94aSIdo Barnea}
920a551c94aSIdo Barnea
921a551c94aSIdo Barneaint
922a551c94aSIdo Barnearte_eal_check_module(const char *module_name)
923a551c94aSIdo Barnea{
924a551c94aSIdo Barnea	char sysfs_mod_name[PATH_MAX];
925a551c94aSIdo Barnea	struct stat st;
926a551c94aSIdo Barnea	int n;
927a551c94aSIdo Barnea
928a551c94aSIdo Barnea	if (NULL == module_name)
929a551c94aSIdo Barnea		return -1;
930a551c94aSIdo Barnea
931a551c94aSIdo Barnea	/* Check if there is sysfs mounted */
932a551c94aSIdo Barnea	if (stat("/sys/module", &st) != 0) {
933a551c94aSIdo Barnea		RTE_LOG(DEBUG, EAL, "sysfs is not mounted! error %i (%s)\n",
934a551c94aSIdo Barnea			errno, strerror(errno));
935a551c94aSIdo Barnea		return -1;
936a551c94aSIdo Barnea	}
937a551c94aSIdo Barnea
938a551c94aSIdo Barnea	/* A module might be built-in, therefore try sysfs */
939a551c94aSIdo Barnea	n = snprintf(sysfs_mod_name, PATH_MAX, "/sys/module/%s", module_name);
940a551c94aSIdo Barnea	if (n < 0 || n > PATH_MAX) {
941a551c94aSIdo Barnea		RTE_LOG(DEBUG, EAL, "Could not format module path\n");
942a551c94aSIdo Barnea		return -1;
943a551c94aSIdo Barnea	}
944a551c94aSIdo Barnea
945a551c94aSIdo Barnea	if (stat(sysfs_mod_name, &st) != 0) {
946a551c94aSIdo Barnea		RTE_LOG(DEBUG, EAL, "Module %s not found! error %i (%s)\n",
947a551c94aSIdo Barnea		        sysfs_mod_name, errno, strerror(errno));
948a551c94aSIdo Barnea		return 0;
949a551c94aSIdo Barnea	}
950a551c94aSIdo Barnea
951a551c94aSIdo Barnea	/* Module has been found */
952a551c94aSIdo Barnea	return 1;
953a551c94aSIdo Barnea}
954