Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2010-2018 Intel Corporation.
3 : : * Copyright(c) 2012-2014 6WIND S.A.
4 : : */
5 : :
6 : : #include <ctype.h>
7 : : #include <stdio.h>
8 : : #include <stdlib.h>
9 : : #include <stdint.h>
10 : : #include <string.h>
11 : : #include <unistd.h>
12 : : #include <pthread.h>
13 : : #include <getopt.h>
14 : : #include <sys/file.h>
15 : : #include <dirent.h>
16 : : #include <fcntl.h>
17 : : #include <fnmatch.h>
18 : : #include <stddef.h>
19 : : #include <errno.h>
20 : : #include <sys/mman.h>
21 : : #if defined(RTE_ARCH_X86)
22 : : #include <sys/io.h>
23 : : #endif
24 : : #include <linux/version.h>
25 : :
26 : : #include <rte_common.h>
27 : : #include <rte_debug.h>
28 : : #include <rte_memory.h>
29 : : #include <rte_launch.h>
30 : : #include <rte_eal.h>
31 : : #include <rte_eal_memconfig.h>
32 : : #include <rte_eal_paging.h>
33 : : #include <rte_errno.h>
34 : : #include <rte_lcore.h>
35 : : #include <rte_service_component.h>
36 : : #include <rte_log.h>
37 : : #include <rte_string_fns.h>
38 : : #include <rte_cpuflags.h>
39 : : #include <rte_bus.h>
40 : : #include <rte_version.h>
41 : : #include <malloc_heap.h>
42 : : #include <dev_vfio.h>
43 : :
44 : : #include <telemetry_internal.h>
45 : : #include <eal_export.h>
46 : : #include "eal_private.h"
47 : : #include "eal_thread.h"
48 : : #include "eal_lcore_var.h"
49 : : #include "eal_internal_cfg.h"
50 : : #include "eal_filesystem.h"
51 : : #include "eal_hugepages.h"
52 : : #include "eal_memcfg.h"
53 : : #include "eal_trace.h"
54 : : #include "eal_options.h"
55 : : #include "hotplug_mp.h"
56 : : #include "log_internal.h"
57 : :
58 : : #define MEMSIZE_IF_NO_HUGE_PAGE (64ULL * 1024ULL * 1024ULL)
59 : : #define KERNEL_IOMMU_GROUPS_PATH "/sys/kernel/iommu_groups"
60 : :
61 : : /* define fd variable here, because file needs to be kept open for the
62 : : * duration of the program, as we hold a write lock on it in the primary proc */
63 : : static int mem_cfg_fd = -1;
64 : :
65 : : static struct flock wr_lock = {
66 : : .l_type = F_WRLCK,
67 : : .l_whence = SEEK_SET,
68 : : .l_start = offsetof(struct rte_mem_config, memsegs),
69 : : .l_len = RTE_SIZEOF_FIELD(struct rte_mem_config, memsegs),
70 : : };
71 : :
72 : : /* internal configuration (per-core) */
73 : : struct lcore_config lcore_config[RTE_MAX_LCORE];
74 : :
75 : : /* used by rte_rdtsc() */
76 : : RTE_EXPORT_SYMBOL(rte_cycles_vmware_tsc_map)
77 : : int rte_cycles_vmware_tsc_map;
78 : :
79 : :
80 : : int
81 : 214 : eal_clean_runtime_dir(void)
82 : : {
83 : 214 : const char *runtime_dir = rte_eal_get_runtime_dir();
84 : : DIR *dir;
85 : : struct dirent *dirent;
86 : : int dir_fd, fd, lck_result;
87 : : static const char * const filters[] = {
88 : : "fbarray_*",
89 : : "mp_socket_*"
90 : : };
91 : :
92 : : /* open directory */
93 : 214 : dir = opendir(runtime_dir);
94 [ - + ]: 214 : if (!dir) {
95 : 0 : EAL_LOG(ERR, "Unable to open runtime directory %s",
96 : : runtime_dir);
97 : 0 : goto error;
98 : : }
99 : 214 : dir_fd = dirfd(dir);
100 : :
101 : : /* lock the directory before doing anything, to avoid races */
102 [ - + ]: 214 : if (flock(dir_fd, LOCK_EX) < 0) {
103 : 0 : EAL_LOG(ERR, "Unable to lock runtime directory %s",
104 : : runtime_dir);
105 : 0 : goto error;
106 : : }
107 : :
108 : 214 : dirent = readdir(dir);
109 [ - + ]: 214 : if (!dirent) {
110 : 0 : EAL_LOG(ERR, "Unable to read runtime directory %s",
111 : : runtime_dir);
112 : 0 : goto error;
113 : : }
114 : :
115 [ + + ]: 1826 : while (dirent != NULL) {
116 : : unsigned int f_idx;
117 : : bool skip = true;
118 : :
119 : : /* skip files that don't match the patterns */
120 [ + + ]: 3658 : for (f_idx = 0; f_idx < RTE_DIM(filters); f_idx++) {
121 : 2650 : const char *filter = filters[f_idx];
122 : :
123 [ + + ]: 2650 : if (fnmatch(filter, dirent->d_name, 0) == 0) {
124 : : skip = false;
125 : : break;
126 : : }
127 : : }
128 [ + + ]: 1612 : if (skip) {
129 : 1008 : dirent = readdir(dir);
130 : 1008 : continue;
131 : : }
132 : :
133 : : /* try and lock the file */
134 : 604 : fd = openat(dir_fd, dirent->d_name, O_RDONLY);
135 : :
136 : : /* skip to next file */
137 [ + + ]: 604 : if (fd == -1) {
138 : 30 : dirent = readdir(dir);
139 : 30 : continue;
140 : : }
141 : :
142 : : /* non-blocking lock */
143 : 574 : lck_result = flock(fd, LOCK_EX | LOCK_NB);
144 : :
145 : : /* if lock succeeds, remove the file */
146 [ + + ]: 574 : if (lck_result != -1)
147 : 1 : unlinkat(dir_fd, dirent->d_name, 0);
148 : 574 : close(fd);
149 : 574 : dirent = readdir(dir);
150 : : }
151 : :
152 : : /* closedir closes dir_fd and drops the lock */
153 : 214 : closedir(dir);
154 : 214 : return 0;
155 : :
156 : 0 : error:
157 [ # # ]: 0 : if (dir)
158 : 0 : closedir(dir);
159 : :
160 : 0 : EAL_LOG(ERR, "Error while clearing runtime dir: %s",
161 : : strerror(errno));
162 : :
163 : 0 : return -1;
164 : : }
165 : :
166 : :
167 : : /* create memory configuration in shared/mmap memory. Take out
168 : : * a write lock on the memsegs, so we can auto-detect primary/secondary.
169 : : * This means we never close the file while running (auto-close on exit).
170 : : * We also don't lock the whole file, so that in future we can use read-locks
171 : : * on other parts, e.g. memzones, to detect if there are running secondary
172 : : * processes. */
173 : : static int
174 : 197 : rte_eal_config_create(void)
175 : : {
176 : 197 : struct rte_config *config = rte_eal_get_configuration();
177 : 197 : size_t page_sz = rte_mem_page_size();
178 : : size_t cfg_len = sizeof(*config->mem_config);
179 : 197 : size_t cfg_len_aligned = RTE_ALIGN(cfg_len, page_sz);
180 : : void *rte_mem_cfg_addr, *mapped_mem_cfg_addr;
181 : : int retval;
182 : : const struct internal_config *internal_conf =
183 : 197 : eal_get_internal_configuration();
184 : :
185 : 197 : const char *pathname = eal_runtime_config_path();
186 : :
187 [ + + ]: 197 : if (internal_conf->no_shconf)
188 : : return 0;
189 : :
190 : : /* map the config before hugepage address so that we don't waste a page */
191 [ + + ]: 187 : if (internal_conf->base_virtaddr != 0)
192 : 1 : rte_mem_cfg_addr = (void *)
193 : 1 : RTE_ALIGN_FLOOR(internal_conf->base_virtaddr -
194 : : sizeof(struct rte_mem_config), page_sz);
195 : : else
196 : : rte_mem_cfg_addr = NULL;
197 : :
198 [ + - ]: 187 : if (mem_cfg_fd < 0){
199 : 187 : mem_cfg_fd = open(pathname, O_RDWR | O_CREAT, 0600);
200 [ - + ]: 187 : if (mem_cfg_fd < 0) {
201 : 0 : EAL_LOG(ERR, "Cannot open '%s' for rte_mem_config",
202 : : pathname);
203 : 0 : return -1;
204 : : }
205 : : }
206 : :
207 : 187 : retval = ftruncate(mem_cfg_fd, cfg_len);
208 [ - + ]: 187 : if (retval < 0){
209 : 0 : close(mem_cfg_fd);
210 : 0 : mem_cfg_fd = -1;
211 : 0 : EAL_LOG(ERR, "Cannot resize '%s' for rte_mem_config",
212 : : pathname);
213 : 0 : return -1;
214 : : }
215 : :
216 : 187 : retval = fcntl(mem_cfg_fd, F_SETLK, &wr_lock);
217 [ - + ]: 187 : if (retval < 0){
218 : 0 : close(mem_cfg_fd);
219 : 0 : mem_cfg_fd = -1;
220 : 0 : EAL_LOG(ERR, "Cannot create lock on '%s'. Is another primary "
221 : : "process running?", pathname);
222 : 0 : return -1;
223 : : }
224 : :
225 : : /* reserve space for config */
226 : 187 : rte_mem_cfg_addr = eal_get_virtual_area(rte_mem_cfg_addr,
227 : : &cfg_len_aligned, page_sz, 0, 0);
228 [ - + ]: 187 : if (rte_mem_cfg_addr == NULL) {
229 : 0 : EAL_LOG(ERR, "Cannot mmap memory for rte_config");
230 : 0 : close(mem_cfg_fd);
231 : 0 : mem_cfg_fd = -1;
232 : 0 : return -1;
233 : : }
234 : :
235 : : /* remap the actual file into the space we've just reserved */
236 : 187 : mapped_mem_cfg_addr = mmap(rte_mem_cfg_addr,
237 : : cfg_len_aligned, PROT_READ | PROT_WRITE,
238 : : MAP_SHARED | MAP_FIXED, mem_cfg_fd, 0);
239 [ - + ]: 187 : if (mapped_mem_cfg_addr == MAP_FAILED) {
240 : 0 : munmap(rte_mem_cfg_addr, cfg_len);
241 : 0 : close(mem_cfg_fd);
242 : 0 : mem_cfg_fd = -1;
243 : 0 : EAL_LOG(ERR, "Cannot remap memory for rte_config");
244 : 0 : return -1;
245 : : }
246 : :
247 : 187 : memcpy(rte_mem_cfg_addr, config->mem_config, sizeof(struct rte_mem_config));
248 : 187 : config->mem_config = rte_mem_cfg_addr;
249 : :
250 : : /* store address of the config in the config itself so that secondary
251 : : * processes could later map the config into this exact location
252 : : */
253 : 187 : config->mem_config->mem_cfg_addr = (uintptr_t) rte_mem_cfg_addr;
254 : 187 : config->mem_config->dma_maskbits = 0;
255 : :
256 : 187 : return 0;
257 : : }
258 : :
259 : : /* attach to an existing shared memory config */
260 : : static int
261 : 33 : rte_eal_config_attach(void)
262 : : {
263 : 33 : struct rte_config *config = rte_eal_get_configuration();
264 : : struct rte_mem_config *mem_config;
265 : : const struct internal_config *internal_conf =
266 : 33 : eal_get_internal_configuration();
267 : :
268 : 33 : const char *pathname = eal_runtime_config_path();
269 : :
270 [ + - ]: 33 : if (internal_conf->no_shconf)
271 : : return 0;
272 : :
273 [ + + ]: 33 : if (mem_cfg_fd < 0){
274 : 32 : mem_cfg_fd = open(pathname, O_RDWR);
275 [ + + ]: 32 : if (mem_cfg_fd < 0) {
276 : 1 : EAL_LOG(ERR, "Cannot open '%s' for rte_mem_config",
277 : : pathname);
278 : 1 : return -1;
279 : : }
280 : : }
281 : :
282 : : /* map it as read-only first */
283 : 32 : mem_config = (struct rte_mem_config *) mmap(NULL, sizeof(*mem_config),
284 : : PROT_READ, MAP_SHARED, mem_cfg_fd, 0);
285 [ - + ]: 32 : if (mem_config == MAP_FAILED) {
286 : 0 : close(mem_cfg_fd);
287 : 0 : mem_cfg_fd = -1;
288 : 0 : EAL_LOG(ERR, "Cannot mmap memory for rte_config! error %i (%s)",
289 : : errno, strerror(errno));
290 : 0 : return -1;
291 : : }
292 : :
293 : 32 : config->mem_config = mem_config;
294 : :
295 : 32 : return 0;
296 : : }
297 : :
298 : : /* reattach the shared config at exact memory location primary process has it */
299 : : static int
300 : 32 : rte_eal_config_reattach(void)
301 : : {
302 : 32 : struct rte_config *config = rte_eal_get_configuration();
303 : : struct rte_mem_config *mem_config;
304 : : void *rte_mem_cfg_addr;
305 : : const struct internal_config *internal_conf =
306 : 32 : eal_get_internal_configuration();
307 : :
308 [ + - ]: 32 : if (internal_conf->no_shconf)
309 : : return 0;
310 : :
311 : : /* save the address primary process has mapped shared config to */
312 : 32 : rte_mem_cfg_addr =
313 : 32 : (void *) (uintptr_t) config->mem_config->mem_cfg_addr;
314 : :
315 : : /* unmap original config */
316 : 32 : munmap(config->mem_config, sizeof(struct rte_mem_config));
317 : :
318 : : /* remap the config at proper address */
319 : 32 : mem_config = (struct rte_mem_config *) mmap(rte_mem_cfg_addr,
320 : : sizeof(*mem_config), PROT_READ | PROT_WRITE, MAP_SHARED,
321 : : mem_cfg_fd, 0);
322 : :
323 : 32 : close(mem_cfg_fd);
324 : 32 : mem_cfg_fd = -1;
325 : :
326 [ - + ]: 32 : if (mem_config == MAP_FAILED || mem_config != rte_mem_cfg_addr) {
327 [ # # ]: 0 : if (mem_config != MAP_FAILED) {
328 : : /* errno is stale, don't use */
329 : 0 : EAL_LOG(ERR, "Cannot mmap memory for rte_config at [%p], got [%p] - please use '--base-virtaddr' option",
330 : : rte_mem_cfg_addr, mem_config);
331 : 0 : munmap(mem_config, sizeof(struct rte_mem_config));
332 : 0 : return -1;
333 : : }
334 : 0 : EAL_LOG(ERR, "Cannot mmap memory for rte_config! error %i (%s)",
335 : : errno, strerror(errno));
336 : 0 : return -1;
337 : : }
338 : :
339 : 32 : config->mem_config = mem_config;
340 : :
341 : 32 : return 0;
342 : : }
343 : :
344 : : /* Detect if we are a primary or a secondary process */
345 : : enum rte_proc_type_t
346 : 3 : eal_proc_type_detect(void)
347 : : {
348 : : enum rte_proc_type_t ptype = RTE_PROC_PRIMARY;
349 : 3 : const char *pathname = eal_runtime_config_path();
350 : : const struct internal_config *internal_conf =
351 : 3 : eal_get_internal_configuration();
352 : :
353 : : /* if there no shared config, there can be no secondary processes */
354 [ + + ]: 3 : if (!internal_conf->no_shconf) {
355 : : /* if we can open the file but not get a write-lock we are a
356 : : * secondary process. NOTE: if we get a file handle back, we
357 : : * keep that open and don't close it to prevent a race condition
358 : : * between multiple opens.
359 : : */
360 [ + + + - ]: 3 : if (((mem_cfg_fd = open(pathname, O_RDWR)) >= 0) &&
361 : 1 : (fcntl(mem_cfg_fd, F_SETLK, &wr_lock) < 0))
362 : : ptype = RTE_PROC_SECONDARY;
363 : : }
364 : :
365 : 3 : EAL_LOG(INFO, "Auto-detected process type: %s",
366 : : ptype == RTE_PROC_PRIMARY ? "PRIMARY" : "SECONDARY");
367 : :
368 : 3 : return ptype;
369 : : }
370 : :
371 : : /* Sets up rte_config structure with the pointer to shared memory config.*/
372 : : static int
373 : 230 : rte_config_init(void)
374 : : {
375 : 230 : struct rte_config *config = rte_eal_get_configuration();
376 : : const struct internal_config *internal_conf =
377 : 230 : eal_get_internal_configuration();
378 : :
379 : 230 : config->process_type = internal_conf->process_type;
380 : :
381 [ + + - - ]: 230 : switch (config->process_type) {
382 : 197 : case RTE_PROC_PRIMARY:
383 [ + - ]: 197 : if (rte_eal_config_create() < 0)
384 : : return -1;
385 : 197 : eal_mcfg_update_from_internal();
386 : 197 : break;
387 : 33 : case RTE_PROC_SECONDARY:
388 [ + + ]: 33 : if (rte_eal_config_attach() < 0)
389 : : return -1;
390 : 32 : eal_mcfg_wait_complete();
391 [ - + ]: 32 : if (eal_mcfg_check_version() < 0) {
392 : 0 : EAL_LOG(ERR, "Primary and secondary process DPDK version mismatch");
393 : 0 : return -1;
394 : : }
395 [ + - ]: 32 : if (rte_eal_config_reattach() < 0)
396 : : return -1;
397 [ - + ]: 32 : if (!__rte_mp_enable()) {
398 : 0 : EAL_LOG(ERR, "Primary process refused secondary attachment");
399 : 0 : return -1;
400 : : }
401 : 32 : eal_mcfg_update_internal();
402 : 32 : break;
403 : 0 : case RTE_PROC_AUTO:
404 : : case RTE_PROC_INVALID:
405 : 0 : EAL_LOG(ERR, "Invalid process type %d",
406 : : config->process_type);
407 : 0 : return -1;
408 : : }
409 : :
410 : : return 0;
411 : : }
412 : :
413 : : /* Unlocks hugepage directories that were locked by eal_hugepage_info_init */
414 : : static void
415 : 224 : eal_hugedirs_unlock(void)
416 : : {
417 : : int i;
418 : : struct internal_config *internal_conf =
419 : 224 : eal_get_internal_configuration();
420 : :
421 [ + + ]: 896 : for (i = 0; i < MAX_HUGEPAGE_SIZES; i++)
422 : : {
423 : : /* skip uninitialized */
424 [ + + ]: 672 : if (internal_conf->hugepage_info[i].lock_descriptor < 0)
425 : 609 : continue;
426 : : /* unlock hugepage file */
427 : 63 : flock(internal_conf->hugepage_info[i].lock_descriptor, LOCK_UN);
428 : 63 : close(internal_conf->hugepage_info[i].lock_descriptor);
429 : : /* reset the field */
430 : 63 : internal_conf->hugepage_info[i].lock_descriptor = -1;
431 : : }
432 : 224 : }
433 : :
434 : : static int
435 : 224 : check_socket(const struct rte_memseg_list *msl, void *arg)
436 : : {
437 : : int *socket_id = arg;
438 : :
439 [ + - ]: 224 : if (msl->external)
440 : : return 0;
441 : :
442 : 224 : return *socket_id == msl->socket_id;
443 : : }
444 : :
445 : : static void
446 : 224 : eal_check_mem_on_local_socket(void)
447 : : {
448 : : int socket_id;
449 : 224 : const struct rte_config *config = rte_eal_get_configuration();
450 : :
451 : 224 : socket_id = rte_lcore_to_socket_id(config->main_lcore);
452 : :
453 [ - + ]: 224 : if (rte_memseg_list_walk(check_socket, &socket_id) == 0)
454 : 0 : EAL_LOG(WARNING, "WARNING: Main core has no memory on local socket!");
455 : 224 : }
456 : :
457 : : static int
458 : 157 : sync_func(__rte_unused void *arg)
459 : : {
460 : 157 : return 0;
461 : : }
462 : :
463 : : /*
464 : : * Request iopl privilege for all RPL, returns 0 on success
465 : : * iopl() call is mostly for the i386 architecture. For other architectures,
466 : : * return -1 to indicate IO privilege can't be changed in this way.
467 : : */
468 : : RTE_EXPORT_SYMBOL(rte_eal_iopl_init)
469 : : int
470 : 305 : rte_eal_iopl_init(void)
471 : : {
472 : : #if defined(RTE_ARCH_X86)
473 [ - + ]: 305 : if (iopl(3) != 0)
474 : 0 : return -1;
475 : : #endif
476 : : return 0;
477 : : }
478 : :
479 : 63 : static void rte_eal_init_alert(const char *msg)
480 : : {
481 : 63 : EAL_LOG(ALERT, "%s", msg);
482 : 63 : }
483 : :
484 : : /*
485 : : * On Linux 3.6+, even if VFIO is not loaded, whenever IOMMU is enabled in the
486 : : * BIOS and in the kernel, /sys/kernel/iommu_groups path will contain kernel
487 : : * IOMMU groups. If IOMMU is not enabled, that path would be empty.
488 : : * Therefore, checking if the path is empty will tell us if IOMMU is enabled.
489 : : */
490 : : static bool
491 : 98 : is_iommu_enabled(void)
492 : : {
493 : 98 : DIR *dir = opendir(KERNEL_IOMMU_GROUPS_PATH);
494 : : struct dirent *d;
495 : : int n = 0;
496 : :
497 : : /* if directory doesn't exist, assume IOMMU is not enabled */
498 [ + - ]: 98 : if (dir == NULL)
499 : : return false;
500 : :
501 [ + + ]: 294 : while ((d = readdir(dir)) != NULL) {
502 : : /* skip dot and dot-dot */
503 [ + - ]: 196 : if (++n > 2)
504 : : break;
505 : : }
506 : 98 : closedir(dir);
507 : :
508 : 98 : return n > 2;
509 : : }
510 : :
511 : : static __rte_noreturn void *
512 : 157 : eal_worker_thread_loop(void *arg)
513 : : {
514 : 157 : eal_thread_loop(arg);
515 : : }
516 : :
517 : : static int
518 : 157 : eal_worker_thread_create(unsigned int lcore_id)
519 : : {
520 : : pthread_attr_t *attrp = NULL;
521 : : void *stack_ptr = NULL;
522 : : pthread_attr_t attr;
523 : : size_t stack_size;
524 : : int ret = -1;
525 : :
526 : 157 : stack_size = eal_get_internal_configuration()->huge_worker_stack_size;
527 [ - + ]: 157 : if (stack_size != 0) {
528 : : /* Allocate NUMA aware stack memory and set pthread attributes */
529 : 0 : stack_ptr = rte_zmalloc_socket("lcore_stack", stack_size,
530 : 0 : RTE_CACHE_LINE_SIZE, rte_lcore_to_socket_id(lcore_id));
531 [ # # ]: 0 : if (stack_ptr == NULL) {
532 : 0 : rte_eal_init_alert("Cannot allocate worker lcore stack memory");
533 : 0 : rte_errno = ENOMEM;
534 : 0 : goto out;
535 : : }
536 : :
537 [ # # ]: 0 : if (pthread_attr_init(&attr) != 0) {
538 : 0 : rte_eal_init_alert("Cannot init pthread attributes");
539 : 0 : rte_errno = EFAULT;
540 : 0 : goto out;
541 : : }
542 : : attrp = &attr;
543 : :
544 [ # # ]: 0 : if (pthread_attr_setstack(attrp, stack_ptr, stack_size) != 0) {
545 : 0 : rte_eal_init_alert("Cannot set pthread stack attributes");
546 : 0 : rte_errno = EFAULT;
547 : 0 : goto out;
548 : : }
549 : : }
550 : :
551 [ - + ]: 157 : if (pthread_create((pthread_t *)&lcore_config[lcore_id].thread_id.opaque_id,
552 : 157 : attrp, eal_worker_thread_loop, (void *)(uintptr_t)lcore_id) == 0)
553 : : ret = 0;
554 : :
555 : 0 : out:
556 [ - + ]: 157 : if (ret != 0)
557 : 0 : rte_free(stack_ptr);
558 [ - + ]: 157 : if (attrp != NULL)
559 : 0 : pthread_attr_destroy(attrp);
560 : 157 : return ret;
561 : : }
562 : :
563 : : /* Launch threads, called at application init(). */
564 : : RTE_EXPORT_SYMBOL(rte_eal_init)
565 : : int
566 : 307 : rte_eal_init(int argc, char **argv)
567 : : {
568 : : int i, fctret, ret;
569 : : static RTE_ATOMIC(uint32_t) run_once;
570 : : uint32_t has_run = 0;
571 : : char cpuset[RTE_CPU_AFFINITY_STR_LEN];
572 : : char thread_name[RTE_THREAD_NAME_SIZE];
573 : : bool phys_addrs;
574 : 307 : const struct rte_config *config = rte_eal_get_configuration();
575 : : struct internal_config *internal_conf =
576 : 306 : eal_get_internal_configuration();
577 : :
578 : : /* first check if we have been run before */
579 [ + + ]: 306 : if (!rte_atomic_compare_exchange_strong_explicit(&run_once, &has_run, 1,
580 : : rte_memory_order_relaxed, rte_memory_order_relaxed)) {
581 : 1 : rte_eal_init_alert("already called initialization.");
582 : 2 : rte_errno = EALREADY;
583 : 2 : return -1;
584 : : }
585 : :
586 : : /* clone argv to report out later in telemetry */
587 : 305 : eal_save_args(argc, argv);
588 : :
589 : 305 : fctret = eal_collate_args(argc, argv);
590 [ + + ]: 298 : if (fctret < 0) {
591 : 7 : rte_eal_init_alert("Invalid command line arguments.");
592 : 7 : rte_errno = EINVAL;
593 : 7 : goto err_out;
594 : : }
595 : :
596 : : /* setup log as early as possible */
597 [ + + ]: 291 : if (eal_parse_log_options() < 0) {
598 : 3 : rte_eal_init_alert("invalid log arguments.");
599 : 3 : rte_errno = EINVAL;
600 : 3 : goto err_out;
601 : : }
602 : :
603 : 288 : eal_log_init(program_invocation_short_name);
604 : :
605 : : /* checks if the machine is adequate */
606 [ - + ]: 288 : if (!rte_cpu_is_supported()) {
607 : 0 : rte_eal_init_alert("unsupported cpu type.");
608 : 0 : rte_errno = ENOTSUP;
609 : 0 : goto err_out;
610 : : }
611 : :
612 : : /* verify if DPDK supported on architecture MMU */
613 [ - + ]: 288 : if (!eal_mmu_supported()) {
614 : 0 : rte_eal_init_alert("unsupported MMU type.");
615 : 0 : rte_errno = ENOTSUP;
616 : 0 : goto err_out;
617 : : }
618 : :
619 : 288 : eal_reset_internal_config(internal_conf);
620 : :
621 [ - + ]: 288 : if (rte_eal_cpu_init() < 0) {
622 : 0 : rte_eal_init_alert("Cannot detect lcores.");
623 : 0 : rte_errno = ENOTSUP;
624 : 0 : goto err_out;
625 : : }
626 : :
627 [ + + ]: 288 : if (eal_parse_args() < 0) {
628 : 45 : rte_eal_init_alert("Error parsing command line arguments.");
629 : 45 : rte_errno = EINVAL;
630 : 45 : goto err_out;
631 : : }
632 : :
633 [ - + ]: 243 : if (eal_plugins_init() < 0) {
634 : 0 : rte_eal_init_alert("Cannot init plugins");
635 : 0 : rte_errno = EINVAL;
636 : 0 : goto err_out;
637 : : }
638 : :
639 [ - + ]: 243 : if (eal_trace_init() < 0) {
640 : 0 : rte_eal_init_alert("Cannot init trace");
641 : 0 : rte_errno = EFAULT;
642 : 0 : goto err_out;
643 : : }
644 : :
645 [ + + ]: 243 : if (eal_option_device_parse()) {
646 : 13 : rte_errno = ENODEV;
647 : 13 : goto err_out;
648 : : }
649 : :
650 [ + + ]: 230 : if (rte_config_init() < 0) {
651 : 1 : rte_eal_init_alert("Cannot init config");
652 : 1 : goto err_out;
653 : : }
654 : :
655 [ - + ]: 229 : if (rte_eal_intr_init() < 0) {
656 : 0 : rte_eal_init_alert("Cannot init interrupt-handling thread");
657 : 0 : goto err_out;
658 : : }
659 : :
660 [ - + ]: 229 : if (rte_eal_alarm_init() < 0) {
661 : 0 : rte_eal_init_alert("Cannot init alarm");
662 : : /* rte_eal_alarm_init sets rte_errno on failure. */
663 : 0 : goto err_out;
664 : : }
665 : :
666 : : /* Put mp channel init before bus scan so that we can init the vdev
667 : : * bus through mp channel in the secondary process before the bus scan.
668 : : */
669 [ + + - + ]: 229 : if (rte_mp_channel_init() < 0 && rte_errno != ENOTSUP) {
670 : 0 : rte_eal_init_alert("failed to init mp channel");
671 [ # # ]: 0 : if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
672 : 0 : rte_errno = EFAULT;
673 : 0 : goto err_out;
674 : : }
675 : : }
676 : :
677 : : /*
678 : : * VFIO must be initialized before bus scan because buses need to know
679 : : * about no-IOMMU mode status.
680 : : */
681 [ - + ]: 229 : if (dev_vfio_enable()) {
682 : 0 : rte_eal_init_alert("Cannot init VFIO");
683 : 0 : rte_errno = EAGAIN;
684 : 0 : goto err_out;
685 : : }
686 : :
687 [ - + ]: 229 : if (rte_bus_scan()) {
688 : 0 : rte_eal_init_alert("Cannot scan the buses for devices");
689 : 0 : rte_errno = ENODEV;
690 : 0 : goto err_out;
691 : : }
692 : :
693 : 229 : phys_addrs = rte_eal_using_phys_addrs() != 0;
694 : :
695 : : /* Always call rte_bus_get_iommu_class() to trigger DMA mask detection and validation */
696 : 229 : enum rte_iova_mode bus_iova_mode = rte_bus_get_iommu_class();
697 : :
698 : : /* if no EAL option "--iova-mode=<pa|va>", use bus IOVA scheme */
699 [ + - ]: 229 : if (internal_conf->iova_mode == RTE_IOVA_DC) {
700 : : /* autodetect the IOVA mapping mode */
701 : : enum rte_iova_mode iova_mode = bus_iova_mode;
702 : :
703 [ + + ]: 229 : if (iova_mode == RTE_IOVA_DC) {
704 : 228 : EAL_LOG(DEBUG, "Buses did not request a specific IOVA mode.");
705 : :
706 : : if (!RTE_IOVA_IN_MBUF) {
707 : : iova_mode = RTE_IOVA_VA;
708 : : EAL_LOG(DEBUG, "IOVA as VA mode is forced by build option.");
709 [ + + ]: 228 : } else if (!phys_addrs) {
710 : : /* if we have no access to physical addresses,
711 : : * pick IOVA as VA mode.
712 : : */
713 : : iova_mode = RTE_IOVA_VA;
714 : 130 : EAL_LOG(DEBUG, "Physical addresses are unavailable, selecting IOVA as VA mode.");
715 [ - + ]: 98 : } else if (is_iommu_enabled()) {
716 : : /* we have an IOMMU, pick IOVA as VA mode */
717 : : iova_mode = RTE_IOVA_VA;
718 : 0 : EAL_LOG(DEBUG, "IOMMU is available, selecting IOVA as VA mode.");
719 : : } else {
720 : : /* physical addresses available, and no IOMMU
721 : : * found, so pick IOVA as PA.
722 : : */
723 : : iova_mode = RTE_IOVA_PA;
724 : 98 : EAL_LOG(DEBUG, "IOMMU is not available, selecting IOVA as PA mode.");
725 : : }
726 : : }
727 : 229 : rte_eal_get_configuration()->iova_mode = iova_mode;
728 : : } else {
729 : 0 : rte_eal_get_configuration()->iova_mode =
730 : 0 : internal_conf->iova_mode;
731 : : }
732 : :
733 [ + + - + ]: 229 : if (rte_eal_iova_mode() == RTE_IOVA_PA && !phys_addrs) {
734 : 0 : rte_eal_init_alert("Cannot use IOVA as 'PA' since physical addresses are not available");
735 : 0 : rte_errno = EINVAL;
736 : 0 : goto err_out;
737 : : }
738 : :
739 : 229 : if (rte_eal_iova_mode() == RTE_IOVA_PA && !RTE_IOVA_IN_MBUF) {
740 : : rte_eal_init_alert("Cannot use IOVA as 'PA' as it is disabled during build");
741 : : rte_errno = EINVAL;
742 : : goto err_out;
743 : : }
744 : :
745 [ + + ]: 360 : EAL_LOG(INFO, "Selected IOVA mode '%s'",
746 : : rte_eal_iova_mode() == RTE_IOVA_PA ? "PA" : "VA");
747 : :
748 [ + + ]: 229 : if (internal_conf->no_hugetlbfs == 0) {
749 : : /* rte_config isn't initialized yet */
750 : 98 : ret = internal_conf->process_type == RTE_PROC_PRIMARY ?
751 [ + + ]: 98 : eal_hugepage_info_init() :
752 : 32 : eal_hugepage_info_read();
753 [ + + ]: 98 : if (ret < 0) {
754 : 3 : rte_eal_init_alert("Cannot get hugepage information.");
755 : 3 : rte_errno = EACCES;
756 : 3 : goto err_out;
757 : : }
758 [ + + - + ]: 159 : if (internal_conf->process_type == RTE_PROC_PRIMARY &&
759 : 64 : eal_apply_hugepage_mem_sz_limits(internal_conf) < 0) {
760 : 0 : rte_eal_init_alert("Cannot apply hugepage memory limits.");
761 : 0 : rte_errno = EINVAL;
762 : 0 : goto err_out;
763 : : }
764 : : }
765 : :
766 [ + + + + ]: 226 : if (internal_conf->memory == 0 && internal_conf->force_numa == 0) {
767 [ + + ]: 87 : if (internal_conf->no_hugetlbfs)
768 : 9 : internal_conf->memory = MEMSIZE_IF_NO_HUGE_PAGE;
769 : : }
770 : :
771 [ - + ]: 226 : if (internal_conf->vmware_tsc_map == 1) {
772 : : #ifdef RTE_LIBRTE_EAL_VMWARE_TSC_MAP_SUPPORT
773 : : rte_cycles_vmware_tsc_map = 1;
774 : : EAL_LOG(DEBUG, "Using VMWARE TSC MAP, "
775 : : "you must have monitor_control.pseudo_perfctr = TRUE");
776 : : #else
777 : 0 : EAL_LOG(WARNING, "Ignoring --vmware-tsc-map because "
778 : : "RTE_LIBRTE_EAL_VMWARE_TSC_MAP_SUPPORT is not set");
779 : : #endif
780 : : }
781 : :
782 : : /* in secondary processes, memory init may allocate additional fbarrays
783 : : * not present in primary processes, so to avoid any potential issues,
784 : : * initialize memzones first.
785 : : */
786 [ - + ]: 226 : if (rte_eal_memzone_init() < 0) {
787 : 0 : rte_eal_init_alert("Cannot init memzone");
788 : 0 : rte_errno = ENODEV;
789 : 0 : goto err_out;
790 : : }
791 : :
792 : 226 : rte_mcfg_mem_read_lock();
793 : :
794 [ + + ]: 226 : if (rte_eal_memory_init() < 0) {
795 : 2 : rte_mcfg_mem_read_unlock();
796 : 2 : rte_eal_init_alert("Cannot init memory");
797 : 2 : rte_errno = ENOMEM;
798 : 2 : goto err_out;
799 : : }
800 : :
801 : : /* the directories are locked during eal_hugepage_info_init */
802 : 224 : eal_hugedirs_unlock();
803 : :
804 [ - + ]: 224 : if (rte_eal_malloc_heap_init() < 0) {
805 : 0 : rte_mcfg_mem_read_unlock();
806 : 0 : rte_eal_init_alert("Cannot init malloc heap");
807 : 0 : rte_errno = ENODEV;
808 : 0 : goto err_out;
809 : : }
810 : :
811 : 224 : rte_mcfg_mem_read_unlock();
812 : :
813 [ - + ]: 224 : if (rte_eal_malloc_heap_populate() < 0) {
814 : 0 : rte_eal_init_alert("Cannot init malloc heap");
815 : 0 : rte_errno = ENODEV;
816 : 0 : goto err_out;
817 : : }
818 : :
819 : : /* VFIO memory setup that requires DPDK memory to be available. */
820 [ - + ]: 224 : if (dev_vfio_init_mem() < 0) {
821 : 0 : rte_eal_init_alert("Cannot init VFIO memory");
822 : 0 : rte_errno = EAGAIN;
823 : 0 : goto err_out;
824 : : }
825 : :
826 : : /* register multi-process action callbacks for hotplug after memory init */
827 [ - + ]: 224 : if (eal_mp_dev_hotplug_init() < 0) {
828 : 0 : rte_eal_init_alert("failed to register mp callback for hotplug");
829 : 0 : goto err_out;
830 : : }
831 : :
832 [ - + ]: 224 : if (rte_eal_tailqs_init() < 0) {
833 : 0 : rte_eal_init_alert("Cannot init tail queues for objects");
834 : 0 : rte_errno = EFAULT;
835 : 0 : goto err_out;
836 : : }
837 : :
838 [ - + ]: 224 : if (rte_eal_timer_init() < 0) {
839 : 0 : rte_eal_init_alert("Cannot init HPET or TSC timers");
840 : 0 : rte_errno = ENOTSUP;
841 : 0 : goto err_out;
842 : : }
843 : :
844 : 224 : eal_rand_init();
845 : :
846 : 224 : eal_check_mem_on_local_socket();
847 : :
848 [ - + ]: 224 : if (rte_thread_set_affinity_by_id(rte_thread_self(),
849 : 224 : &lcore_config[config->main_lcore].cpuset) != 0) {
850 : 0 : rte_eal_init_alert("Cannot set affinity");
851 : 0 : rte_errno = EINVAL;
852 : 0 : goto err_out;
853 : : }
854 : 224 : __rte_thread_init(config->main_lcore,
855 : 224 : &lcore_config[config->main_lcore].cpuset);
856 : :
857 : 224 : ret = eal_thread_dump_current_affinity(cpuset, sizeof(cpuset));
858 [ - + ]: 224 : EAL_LOG(DEBUG, "Main lcore %u is ready (tid=%zx;cpuset=[%s%s])",
859 : : config->main_lcore, (uintptr_t)pthread_self(), cpuset,
860 : : ret == 0 ? "" : "...");
861 : :
862 [ + + ]: 381 : RTE_LCORE_FOREACH_WORKER(i) {
863 : :
864 : : /*
865 : : * create communication pipes between main thread
866 : : * and children
867 : : */
868 [ - + ]: 157 : if (pipe(lcore_config[i].pipe_main2worker) < 0)
869 : 0 : rte_panic("Cannot create pipe\n");
870 [ - + ]: 157 : if (pipe(lcore_config[i].pipe_worker2main) < 0)
871 : 0 : rte_panic("Cannot create pipe\n");
872 : :
873 : 157 : lcore_config[i].state = WAIT;
874 : :
875 : : /* create a thread for each lcore */
876 : 157 : ret = eal_worker_thread_create(i);
877 [ - + ]: 157 : if (ret != 0)
878 : 0 : rte_panic("Cannot create thread\n");
879 : :
880 : : /* Set thread_name for aid in debugging. */
881 : : ret = snprintf(thread_name, sizeof(thread_name), "dpdk-worker%d", i);
882 [ - + ]: 157 : if (ret >= RTE_THREAD_NAME_SIZE)
883 : 0 : EAL_LOG(INFO, "Worker thread name %s truncated", thread_name);
884 : :
885 : 157 : rte_thread_set_name(lcore_config[i].thread_id, thread_name);
886 : :
887 : 157 : ret = rte_thread_set_affinity_by_id(lcore_config[i].thread_id,
888 : 157 : &lcore_config[i].cpuset);
889 [ - + ]: 157 : if (ret != 0)
890 : 0 : rte_panic("Cannot set affinity\n");
891 : : }
892 : :
893 : : /*
894 : : * Launch a dummy function on all worker lcores, so that main lcore
895 : : * knows they are all ready when this function returns.
896 : : */
897 : 224 : rte_eal_mp_remote_launch(sync_func, NULL, SKIP_MAIN);
898 : 224 : rte_eal_mp_wait_lcore();
899 : :
900 : : /* initialize services so vdevs register service during bus_probe. */
901 : 224 : ret = rte_service_init();
902 [ - + ]: 224 : if (ret) {
903 : 0 : rte_eal_init_alert("rte_service_init() failed");
904 : 0 : rte_errno = -ret;
905 : 0 : goto err_out;
906 : : }
907 : :
908 : : /* Probe all the buses and devices/drivers on them */
909 [ - + ]: 224 : if (rte_bus_probe()) {
910 : 0 : rte_eal_init_alert("Cannot probe devices");
911 : 0 : rte_errno = ENOTSUP;
912 : 0 : goto err_out;
913 : : }
914 : :
915 : : /* initialize default service/lcore mappings and start running. Ignore
916 : : * -ENOTSUP, as it indicates no service coremask passed to EAL.
917 : : */
918 : 224 : ret = rte_service_start_with_defaults();
919 [ - + ]: 224 : if (ret < 0 && ret != -ENOTSUP) {
920 : 0 : rte_errno = -ret;
921 : 0 : goto err_out;
922 : : }
923 : :
924 : : /*
925 : : * Clean up unused files in runtime directory. We do this at the end of
926 : : * init and not at the beginning because we want to clean stuff up
927 : : * whether we are primary or secondary process, but we cannot remove
928 : : * primary process' files because secondary should be able to run even
929 : : * if primary process is dead.
930 : : *
931 : : * In no_shconf mode, no runtime directory is created in the first
932 : : * place, so no cleanup needed.
933 : : */
934 [ + + - + ]: 224 : if (!internal_conf->no_shconf && eal_clean_runtime_dir() < 0) {
935 : 0 : rte_eal_init_alert("Cannot clear runtime directory");
936 : 0 : goto err_out;
937 : : }
938 [ + + + + ]: 224 : if (rte_eal_process_type() == RTE_PROC_PRIMARY && !internal_conf->no_telemetry) {
939 [ - + ]: 189 : if (rte_telemetry_init(rte_eal_get_runtime_dir(),
940 : : rte_version(),
941 : : &internal_conf->ctrl_cpuset) != 0)
942 : 0 : goto err_out;
943 : : }
944 : :
945 : 224 : eal_mcfg_complete();
946 : :
947 : 224 : return fctret;
948 : :
949 : 74 : err_out:
950 : 74 : rte_atomic_store_explicit(&run_once, 0, rte_memory_order_relaxed);
951 : 74 : eal_clean_saved_args();
952 : 74 : return -1;
953 : : }
954 : :
955 : : static int
956 : 63071388 : mark_freeable(const struct rte_memseg_list *msl, const struct rte_memseg *ms,
957 : : void *arg __rte_unused)
958 : : {
959 : : /* ms is const, so find this memseg */
960 : : struct rte_memseg *found;
961 : :
962 [ + - ]: 63071388 : if (msl->external)
963 : : return 0;
964 : :
965 : 63071388 : found = rte_mem_virt2memseg(ms->addr, msl);
966 : :
967 : 63071388 : found->flags &= ~RTE_MEMSEG_FLAG_DO_NOT_FREE;
968 : :
969 : 63071388 : return 0;
970 : : }
971 : :
972 : : RTE_EXPORT_SYMBOL(rte_eal_cleanup)
973 : : int
974 : 298 : rte_eal_cleanup(void)
975 : : {
976 : : static RTE_ATOMIC(uint32_t) run_once;
977 : : uint32_t has_run = 0;
978 : :
979 [ - + ]: 298 : if (!rte_atomic_compare_exchange_strong_explicit(&run_once, &has_run, 1,
980 : : rte_memory_order_relaxed, rte_memory_order_relaxed)) {
981 : 0 : EAL_LOG(WARNING, "Already called cleanup");
982 : 0 : rte_errno = EALREADY;
983 : 0 : return -1;
984 : : }
985 : :
986 : : /* if we're in a primary process, we need to mark hugepages as freeable
987 : : * so that finalization can release them back to the system.
988 : : */
989 : : struct internal_config *internal_conf =
990 : 298 : eal_get_internal_configuration();
991 : :
992 [ + + ]: 298 : if (rte_eal_process_type() == RTE_PROC_PRIMARY &&
993 [ + + ]: 265 : internal_conf->hugepage_file.unlink_existing)
994 : 254 : rte_memseg_walk(mark_freeable, NULL);
995 : :
996 : 298 : rte_service_finalize();
997 : 298 : eal_bus_cleanup();
998 : 298 : dev_vfio_cleanup();
999 : 298 : rte_mp_channel_cleanup();
1000 : 298 : rte_eal_alarm_cleanup();
1001 : 298 : rte_trace_save();
1002 : 298 : eal_trace_fini();
1003 : 298 : eal_mp_dev_hotplug_cleanup();
1004 : : /* after this point, any DPDK pointers will become dangling */
1005 : 298 : rte_eal_memory_detach();
1006 : 298 : rte_eal_malloc_heap_cleanup();
1007 : 298 : eal_cleanup_config(internal_conf);
1008 : 298 : eal_lcore_var_cleanup();
1009 : 298 : rte_eal_log_cleanup();
1010 : 298 : return 0;
1011 : : }
1012 : :
1013 : : RTE_EXPORT_SYMBOL(rte_eal_create_uio_dev)
1014 : 0 : int rte_eal_create_uio_dev(void)
1015 : : {
1016 : : const struct internal_config *internal_conf =
1017 : 0 : eal_get_internal_configuration();
1018 : :
1019 : 0 : return internal_conf->create_uio_dev;
1020 : : }
1021 : :
1022 : : RTE_EXPORT_SYMBOL(rte_eal_vfio_intr_mode)
1023 : : enum rte_intr_mode
1024 : 0 : rte_eal_vfio_intr_mode(void)
1025 : : {
1026 : : const struct internal_config *internal_conf =
1027 : 0 : eal_get_internal_configuration();
1028 : :
1029 : 0 : return internal_conf->vfio_intr_mode;
1030 : : }
1031 : :
1032 : : RTE_EXPORT_SYMBOL(rte_eal_vfio_get_vf_token)
1033 : : void
1034 : 0 : rte_eal_vfio_get_vf_token(rte_uuid_t vf_token)
1035 : : {
1036 : 0 : struct internal_config *cfg = eal_get_internal_configuration();
1037 : :
1038 : 0 : rte_uuid_copy(vf_token, cfg->vfio_vf_token);
1039 : 0 : }
1040 : :
|