eal: move Unix filesystem functions into one file
[dpdk.git] / lib / eal / freebsd / eal.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2018 Intel Corporation.
3  * Copyright(c) 2014 6WIND S.A.
4  */
5
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <stdint.h>
9 #include <string.h>
10 #include <stdarg.h>
11 #include <unistd.h>
12 #include <pthread.h>
13 #include <syslog.h>
14 #include <getopt.h>
15 #include <sys/file.h>
16 #include <stddef.h>
17 #include <errno.h>
18 #include <limits.h>
19 #include <sys/mman.h>
20 #include <sys/queue.h>
21 #include <sys/stat.h>
22
23 #include <rte_compat.h>
24 #include <rte_common.h>
25 #include <rte_debug.h>
26 #include <rte_memory.h>
27 #include <rte_launch.h>
28 #include <rte_eal.h>
29 #include <rte_errno.h>
30 #include <rte_per_lcore.h>
31 #include <rte_lcore.h>
32 #include <rte_service_component.h>
33 #include <rte_log.h>
34 #include <rte_random.h>
35 #include <rte_cycles.h>
36 #include <rte_string_fns.h>
37 #include <rte_cpuflags.h>
38 #include <rte_interrupts.h>
39 #include <rte_bus.h>
40 #include <rte_dev.h>
41 #include <rte_devargs.h>
42 #include <rte_version.h>
43 #include <rte_vfio.h>
44 #include <malloc_heap.h>
45 #include <telemetry_internal.h>
46
47 #include "eal_private.h"
48 #include "eal_thread.h"
49 #include "eal_internal_cfg.h"
50 #include "eal_filesystem.h"
51 #include "eal_hugepages.h"
52 #include "eal_options.h"
53 #include "eal_memcfg.h"
54 #include "eal_trace.h"
55
56 #define MEMSIZE_IF_NO_HUGE_PAGE (64ULL * 1024ULL * 1024ULL)
57
58 /* define fd variable here, because file needs to be kept open for the
59  * duration of the program, as we hold a write lock on it in the primary proc */
60 static int mem_cfg_fd = -1;
61
62 static struct flock wr_lock = {
63                 .l_type = F_WRLCK,
64                 .l_whence = SEEK_SET,
65                 .l_start = offsetof(struct rte_mem_config, memsegs),
66                 .l_len = RTE_SIZEOF_FIELD(struct rte_mem_config, memsegs),
67 };
68
69 /* internal configuration (per-core) */
70 struct lcore_config lcore_config[RTE_MAX_LCORE];
71
72 /* used by rte_rdtsc() */
73 int rte_cycles_vmware_tsc_map;
74
75
76 int
77 eal_clean_runtime_dir(void)
78 {
79         /* FreeBSD doesn't need this implemented for now, because, unlike Linux,
80          * FreeBSD doesn't create per-process files, so no need to clean up.
81          */
82         return 0;
83 }
84
85 /* create memory configuration in shared/mmap memory. Take out
86  * a write lock on the memsegs, so we can auto-detect primary/secondary.
87  * This means we never close the file while running (auto-close on exit).
88  * We also don't lock the whole file, so that in future we can use read-locks
89  * on other parts, e.g. memzones, to detect if there are running secondary
90  * processes. */
91 static int
92 rte_eal_config_create(void)
93 {
94         struct rte_config *config = rte_eal_get_configuration();
95         const struct internal_config *internal_conf =
96                 eal_get_internal_configuration();
97         size_t page_sz = sysconf(_SC_PAGE_SIZE);
98         size_t cfg_len = sizeof(struct rte_mem_config);
99         size_t cfg_len_aligned = RTE_ALIGN(cfg_len, page_sz);
100         void *rte_mem_cfg_addr, *mapped_mem_cfg_addr;
101         int retval;
102
103         const char *pathname = eal_runtime_config_path();
104
105         if (internal_conf->no_shconf)
106                 return 0;
107
108         /* map the config before base address so that we don't waste a page */
109         if (internal_conf->base_virtaddr != 0)
110                 rte_mem_cfg_addr = (void *)
111                         RTE_ALIGN_FLOOR(internal_conf->base_virtaddr -
112                         sizeof(struct rte_mem_config), page_sz);
113         else
114                 rte_mem_cfg_addr = NULL;
115
116         if (mem_cfg_fd < 0){
117                 mem_cfg_fd = open(pathname, O_RDWR | O_CREAT, 0600);
118                 if (mem_cfg_fd < 0) {
119                         RTE_LOG(ERR, EAL, "Cannot open '%s' for rte_mem_config\n",
120                                 pathname);
121                         return -1;
122                 }
123         }
124
125         retval = ftruncate(mem_cfg_fd, cfg_len);
126         if (retval < 0){
127                 close(mem_cfg_fd);
128                 mem_cfg_fd = -1;
129                 RTE_LOG(ERR, EAL, "Cannot resize '%s' for rte_mem_config\n",
130                         pathname);
131                 return -1;
132         }
133
134         retval = fcntl(mem_cfg_fd, F_SETLK, &wr_lock);
135         if (retval < 0){
136                 close(mem_cfg_fd);
137                 mem_cfg_fd = -1;
138                 RTE_LOG(ERR, EAL, "Cannot create lock on '%s'. Is another primary "
139                         "process running?\n", pathname);
140                 return -1;
141         }
142
143         /* reserve space for config */
144         rte_mem_cfg_addr = eal_get_virtual_area(rte_mem_cfg_addr,
145                         &cfg_len_aligned, page_sz, 0, 0);
146         if (rte_mem_cfg_addr == NULL) {
147                 RTE_LOG(ERR, EAL, "Cannot mmap memory for rte_config\n");
148                 close(mem_cfg_fd);
149                 mem_cfg_fd = -1;
150                 return -1;
151         }
152
153         /* remap the actual file into the space we've just reserved */
154         mapped_mem_cfg_addr = mmap(rte_mem_cfg_addr,
155                         cfg_len_aligned, PROT_READ | PROT_WRITE,
156                         MAP_SHARED | MAP_FIXED, mem_cfg_fd, 0);
157         if (mapped_mem_cfg_addr == MAP_FAILED) {
158                 RTE_LOG(ERR, EAL, "Cannot remap memory for rte_config\n");
159                 munmap(rte_mem_cfg_addr, cfg_len);
160                 close(mem_cfg_fd);
161                 mem_cfg_fd = -1;
162                 return -1;
163         }
164
165         memcpy(rte_mem_cfg_addr, config->mem_config, sizeof(struct rte_mem_config));
166         config->mem_config = rte_mem_cfg_addr;
167
168         /* store address of the config in the config itself so that secondary
169          * processes could later map the config into this exact location
170          */
171         config->mem_config->mem_cfg_addr = (uintptr_t) rte_mem_cfg_addr;
172         return 0;
173 }
174
175 /* attach to an existing shared memory config */
176 static int
177 rte_eal_config_attach(void)
178 {
179         void *rte_mem_cfg_addr;
180         const char *pathname = eal_runtime_config_path();
181         struct rte_config *config = rte_eal_get_configuration();
182         const struct internal_config *internal_conf =
183                 eal_get_internal_configuration();
184
185
186         if (internal_conf->no_shconf)
187                 return 0;
188
189         if (mem_cfg_fd < 0){
190                 mem_cfg_fd = open(pathname, O_RDWR);
191                 if (mem_cfg_fd < 0) {
192                         RTE_LOG(ERR, EAL, "Cannot open '%s' for rte_mem_config\n",
193                                 pathname);
194                         return -1;
195                 }
196         }
197
198         rte_mem_cfg_addr = mmap(NULL, sizeof(*config->mem_config),
199                                 PROT_READ, MAP_SHARED, mem_cfg_fd, 0);
200         /* don't close the fd here, it will be closed on reattach */
201         if (rte_mem_cfg_addr == MAP_FAILED) {
202                 close(mem_cfg_fd);
203                 mem_cfg_fd = -1;
204                 RTE_LOG(ERR, EAL, "Cannot mmap memory for rte_config! error %i (%s)\n",
205                         errno, strerror(errno));
206                 return -1;
207         }
208
209         config->mem_config = rte_mem_cfg_addr;
210
211         return 0;
212 }
213
214 /* reattach the shared config at exact memory location primary process has it */
215 static int
216 rte_eal_config_reattach(void)
217 {
218         struct rte_mem_config *mem_config;
219         void *rte_mem_cfg_addr;
220         struct rte_config *config = rte_eal_get_configuration();
221         const struct internal_config *internal_conf =
222                 eal_get_internal_configuration();
223
224         if (internal_conf->no_shconf)
225                 return 0;
226
227         /* save the address primary process has mapped shared config to */
228         rte_mem_cfg_addr =
229                         (void *)(uintptr_t)config->mem_config->mem_cfg_addr;
230
231         /* unmap original config */
232         munmap(config->mem_config, sizeof(struct rte_mem_config));
233
234         /* remap the config at proper address */
235         mem_config = (struct rte_mem_config *) mmap(rte_mem_cfg_addr,
236                         sizeof(*mem_config), PROT_READ | PROT_WRITE, MAP_SHARED,
237                         mem_cfg_fd, 0);
238         close(mem_cfg_fd);
239         mem_cfg_fd = -1;
240
241         if (mem_config == MAP_FAILED || mem_config != rte_mem_cfg_addr) {
242                 if (mem_config != MAP_FAILED) {
243                         /* errno is stale, don't use */
244                         RTE_LOG(ERR, EAL, "Cannot mmap memory for rte_config at [%p], got [%p]"
245                                           " - please use '--" OPT_BASE_VIRTADDR
246                                           "' option\n",
247                                 rte_mem_cfg_addr, mem_config);
248                         munmap(mem_config, sizeof(struct rte_mem_config));
249                         return -1;
250                 }
251                 RTE_LOG(ERR, EAL, "Cannot mmap memory for rte_config! error %i (%s)\n",
252                         errno, strerror(errno));
253                 return -1;
254         }
255
256         config->mem_config = mem_config;
257
258         return 0;
259 }
260
261 /* Detect if we are a primary or a secondary process */
262 enum rte_proc_type_t
263 eal_proc_type_detect(void)
264 {
265         enum rte_proc_type_t ptype = RTE_PROC_PRIMARY;
266         const char *pathname = eal_runtime_config_path();
267         const struct internal_config *internal_conf =
268                 eal_get_internal_configuration();
269
270         /* if there no shared config, there can be no secondary processes */
271         if (!internal_conf->no_shconf) {
272                 /* if we can open the file but not get a write-lock we are a
273                  * secondary process. NOTE: if we get a file handle back, we
274                  * keep that open and don't close it to prevent a race condition
275                  * between multiple opens.
276                  */
277                 if (((mem_cfg_fd = open(pathname, O_RDWR)) >= 0) &&
278                                 (fcntl(mem_cfg_fd, F_SETLK, &wr_lock) < 0))
279                         ptype = RTE_PROC_SECONDARY;
280         }
281
282         RTE_LOG(INFO, EAL, "Auto-detected process type: %s\n",
283                         ptype == RTE_PROC_PRIMARY ? "PRIMARY" : "SECONDARY");
284
285         return ptype;
286 }
287
288 /* Sets up rte_config structure with the pointer to shared memory config.*/
289 static int
290 rte_config_init(void)
291 {
292         struct rte_config *config = rte_eal_get_configuration();
293         const struct internal_config *internal_conf =
294                 eal_get_internal_configuration();
295
296         config->process_type = internal_conf->process_type;
297
298         switch (config->process_type) {
299         case RTE_PROC_PRIMARY:
300                 if (rte_eal_config_create() < 0)
301                         return -1;
302                 eal_mcfg_update_from_internal();
303                 break;
304         case RTE_PROC_SECONDARY:
305                 if (rte_eal_config_attach() < 0)
306                         return -1;
307                 eal_mcfg_wait_complete();
308                 if (eal_mcfg_check_version() < 0) {
309                         RTE_LOG(ERR, EAL, "Primary and secondary process DPDK version mismatch\n");
310                         return -1;
311                 }
312                 if (rte_eal_config_reattach() < 0)
313                         return -1;
314                 if (!__rte_mp_enable()) {
315                         RTE_LOG(ERR, EAL, "Primary process refused secondary attachment\n");
316                         return -1;
317                 }
318                 eal_mcfg_update_internal();
319                 break;
320         case RTE_PROC_AUTO:
321         case RTE_PROC_INVALID:
322                 RTE_LOG(ERR, EAL, "Invalid process type %d\n",
323                         config->process_type);
324                 return -1;
325         }
326
327         return 0;
328 }
329
330 /* display usage */
331 static void
332 eal_usage(const char *prgname)
333 {
334         rte_usage_hook_t hook = eal_get_application_usage_hook();
335
336         printf("\nUsage: %s ", prgname);
337         eal_common_usage();
338         /* Allow the application to print its usage message too if hook is set */
339         if (hook) {
340                 printf("===== Application Usage =====\n\n");
341                 (hook)(prgname);
342         }
343 }
344
345 static inline size_t
346 eal_get_hugepage_mem_size(void)
347 {
348         uint64_t size = 0;
349         unsigned i, j;
350         struct internal_config *internal_conf =
351                 eal_get_internal_configuration();
352
353         for (i = 0; i < internal_conf->num_hugepage_sizes; i++) {
354                 struct hugepage_info *hpi = &internal_conf->hugepage_info[i];
355                 if (strnlen(hpi->hugedir, sizeof(hpi->hugedir)) != 0) {
356                         for (j = 0; j < RTE_MAX_NUMA_NODES; j++) {
357                                 size += hpi->hugepage_sz * hpi->num_pages[j];
358                         }
359                 }
360         }
361
362         return (size < SIZE_MAX) ? (size_t)(size) : SIZE_MAX;
363 }
364
365 /* Parse the arguments for --log-level only */
366 static void
367 eal_log_level_parse(int argc, char **argv)
368 {
369         int opt;
370         char **argvopt;
371         int option_index;
372         const int old_optind = optind;
373         const int old_optopt = optopt;
374         const int old_optreset = optreset;
375         char * const old_optarg = optarg;
376         struct internal_config *internal_conf =
377                 eal_get_internal_configuration();
378
379         argvopt = argv;
380         optind = 1;
381         optreset = 1;
382
383         while ((opt = getopt_long(argc, argvopt, eal_short_options,
384                                   eal_long_options, &option_index)) != EOF) {
385
386                 int ret;
387
388                 /* getopt is not happy, stop right now */
389                 if (opt == '?')
390                         break;
391
392                 ret = (opt == OPT_LOG_LEVEL_NUM) ?
393                     eal_parse_common_option(opt, optarg, internal_conf) : 0;
394
395                 /* common parser is not happy */
396                 if (ret < 0)
397                         break;
398         }
399
400         /* restore getopt lib */
401         optind = old_optind;
402         optopt = old_optopt;
403         optreset = old_optreset;
404         optarg = old_optarg;
405 }
406
407 /* Parse the argument given in the command line of the application */
408 static int
409 eal_parse_args(int argc, char **argv)
410 {
411         int opt, ret;
412         char **argvopt;
413         int option_index;
414         char *prgname = argv[0];
415         const int old_optind = optind;
416         const int old_optopt = optopt;
417         const int old_optreset = optreset;
418         char * const old_optarg = optarg;
419         struct internal_config *internal_conf =
420                 eal_get_internal_configuration();
421
422         argvopt = argv;
423         optind = 1;
424         optreset = 1;
425
426         while ((opt = getopt_long(argc, argvopt, eal_short_options,
427                                   eal_long_options, &option_index)) != EOF) {
428
429                 /* getopt didn't recognise the option */
430                 if (opt == '?') {
431                         eal_usage(prgname);
432                         ret = -1;
433                         goto out;
434                 }
435
436                 /* eal_log_level_parse() already handled this option */
437                 if (opt == OPT_LOG_LEVEL_NUM)
438                         continue;
439
440                 ret = eal_parse_common_option(opt, optarg, internal_conf);
441                 /* common parser is not happy */
442                 if (ret < 0) {
443                         eal_usage(prgname);
444                         ret = -1;
445                         goto out;
446                 }
447                 /* common parser handled this option */
448                 if (ret == 0)
449                         continue;
450
451                 switch (opt) {
452                 case OPT_MBUF_POOL_OPS_NAME_NUM:
453                 {
454                         char *ops_name = strdup(optarg);
455                         if (ops_name == NULL)
456                                 RTE_LOG(ERR, EAL, "Could not store mbuf pool ops name\n");
457                         else {
458                                 /* free old ops name */
459                                 if (internal_conf->user_mbuf_pool_ops_name !=
460                                                 NULL)
461                                         free(internal_conf->user_mbuf_pool_ops_name);
462
463                                 internal_conf->user_mbuf_pool_ops_name =
464                                                 ops_name;
465                         }
466                         break;
467                 }
468                 case 'h':
469                         eal_usage(prgname);
470                         exit(EXIT_SUCCESS);
471                 default:
472                         if (opt < OPT_LONG_MIN_NUM && isprint(opt)) {
473                                 RTE_LOG(ERR, EAL, "Option %c is not supported "
474                                         "on FreeBSD\n", opt);
475                         } else if (opt >= OPT_LONG_MIN_NUM &&
476                                    opt < OPT_LONG_MAX_NUM) {
477                                 RTE_LOG(ERR, EAL, "Option %s is not supported "
478                                         "on FreeBSD\n",
479                                         eal_long_options[option_index].name);
480                         } else {
481                                 RTE_LOG(ERR, EAL, "Option %d is not supported "
482                                         "on FreeBSD\n", opt);
483                         }
484                         eal_usage(prgname);
485                         ret = -1;
486                         goto out;
487                 }
488         }
489
490         /* create runtime data directory. In no_shconf mode, skip any errors */
491         if (eal_create_runtime_dir() < 0) {
492                 if (internal_conf->no_shconf == 0) {
493                         RTE_LOG(ERR, EAL, "Cannot create runtime directory\n");
494                         ret = -1;
495                         goto out;
496                 } else
497                         RTE_LOG(WARNING, EAL, "No DPDK runtime directory created\n");
498         }
499
500         if (eal_adjust_config(internal_conf) != 0) {
501                 ret = -1;
502                 goto out;
503         }
504
505         /* sanity checks */
506         if (eal_check_common_options(internal_conf) != 0) {
507                 eal_usage(prgname);
508                 ret = -1;
509                 goto out;
510         }
511
512         if (optind >= 0)
513                 argv[optind-1] = prgname;
514         ret = optind-1;
515
516 out:
517         /* restore getopt lib */
518         optind = old_optind;
519         optopt = old_optopt;
520         optreset = old_optreset;
521         optarg = old_optarg;
522
523         return ret;
524 }
525
526 static int
527 check_socket(const struct rte_memseg_list *msl, void *arg)
528 {
529         int *socket_id = arg;
530
531         if (msl->external)
532                 return 0;
533
534         if (msl->socket_id == *socket_id && msl->memseg_arr.count != 0)
535                 return 1;
536
537         return 0;
538 }
539
540 static void
541 eal_check_mem_on_local_socket(void)
542 {
543         int socket_id;
544         const struct rte_config *config = rte_eal_get_configuration();
545
546         socket_id = rte_lcore_to_socket_id(config->main_lcore);
547
548         if (rte_memseg_list_walk(check_socket, &socket_id) == 0)
549                 RTE_LOG(WARNING, EAL, "WARNING: Main core has no memory on local socket!\n");
550 }
551
552
553 static int
554 sync_func(__rte_unused void *arg)
555 {
556         return 0;
557 }
558 /* Abstraction for port I/0 privilege */
559 int
560 rte_eal_iopl_init(void)
561 {
562         static int fd = -1;
563
564         if (fd < 0)
565                 fd = open("/dev/io", O_RDWR);
566
567         if (fd < 0)
568                 return -1;
569         /* keep fd open for iopl */
570         return 0;
571 }
572
573 static void rte_eal_init_alert(const char *msg)
574 {
575         fprintf(stderr, "EAL: FATAL: %s\n", msg);
576         RTE_LOG(ERR, EAL, "%s\n", msg);
577 }
578
579 /* Launch threads, called at application init(). */
580 int
581 rte_eal_init(int argc, char **argv)
582 {
583         int i, fctret, ret;
584         pthread_t thread_id;
585         static uint32_t run_once;
586         uint32_t has_run = 0;
587         char cpuset[RTE_CPU_AFFINITY_STR_LEN];
588         char thread_name[RTE_MAX_THREAD_NAME_LEN];
589         const struct rte_config *config = rte_eal_get_configuration();
590         struct internal_config *internal_conf =
591                 eal_get_internal_configuration();
592         bool has_phys_addr;
593         enum rte_iova_mode iova_mode;
594
595         /* checks if the machine is adequate */
596         if (!rte_cpu_is_supported()) {
597                 rte_eal_init_alert("unsupported cpu type.");
598                 rte_errno = ENOTSUP;
599                 return -1;
600         }
601
602         if (!__atomic_compare_exchange_n(&run_once, &has_run, 1, 0,
603                                         __ATOMIC_RELAXED, __ATOMIC_RELAXED)) {
604                 rte_eal_init_alert("already called initialization.");
605                 rte_errno = EALREADY;
606                 return -1;
607         }
608
609         thread_id = pthread_self();
610
611         eal_reset_internal_config(internal_conf);
612
613         /* clone argv to report out later in telemetry */
614         eal_save_args(argc, argv);
615
616         /* set log level as early as possible */
617         eal_log_level_parse(argc, argv);
618
619         if (rte_eal_cpu_init() < 0) {
620                 rte_eal_init_alert("Cannot detect lcores.");
621                 rte_errno = ENOTSUP;
622                 return -1;
623         }
624
625         fctret = eal_parse_args(argc, argv);
626         if (fctret < 0) {
627                 rte_eal_init_alert("Invalid 'command line' arguments.");
628                 rte_errno = EINVAL;
629                 __atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
630                 return -1;
631         }
632
633         /* FreeBSD always uses legacy memory model */
634         internal_conf->legacy_mem = true;
635         if (internal_conf->in_memory) {
636                 RTE_LOG(WARNING, EAL, "Warning: ignoring unsupported flag, '%s'\n", OPT_IN_MEMORY);
637                 internal_conf->in_memory = false;
638         }
639
640         if (eal_plugins_init() < 0) {
641                 rte_eal_init_alert("Cannot init plugins");
642                 rte_errno = EINVAL;
643                 __atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
644                 return -1;
645         }
646
647         if (eal_trace_init() < 0) {
648                 rte_eal_init_alert("Cannot init trace");
649                 rte_errno = EFAULT;
650                 __atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
651                 return -1;
652         }
653
654         if (eal_option_device_parse()) {
655                 rte_errno = ENODEV;
656                 __atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
657                 return -1;
658         }
659
660         if (rte_config_init() < 0) {
661                 rte_eal_init_alert("Cannot init config");
662                 return -1;
663         }
664
665         if (rte_eal_intr_init() < 0) {
666                 rte_eal_init_alert("Cannot init interrupt-handling thread");
667                 return -1;
668         }
669
670         if (rte_eal_alarm_init() < 0) {
671                 rte_eal_init_alert("Cannot init alarm");
672                 /* rte_eal_alarm_init sets rte_errno on failure. */
673                 return -1;
674         }
675
676         /* Put mp channel init before bus scan so that we can init the vdev
677          * bus through mp channel in the secondary process before the bus scan.
678          */
679         if (rte_mp_channel_init() < 0 && rte_errno != ENOTSUP) {
680                 rte_eal_init_alert("failed to init mp channel");
681                 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
682                         rte_errno = EFAULT;
683                         return -1;
684                 }
685         }
686
687         if (rte_bus_scan()) {
688                 rte_eal_init_alert("Cannot scan the buses for devices");
689                 rte_errno = ENODEV;
690                 __atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
691                 return -1;
692         }
693
694         /*
695          * PA are only available for hugepages via contigmem.
696          * If contigmem is inaccessible, rte_eal_hugepage_init() will fail
697          * with a message describing the cause.
698          */
699         has_phys_addr = internal_conf->no_hugetlbfs == 0;
700         iova_mode = internal_conf->iova_mode;
701         if (iova_mode == RTE_IOVA_PA && !has_phys_addr) {
702                 rte_eal_init_alert("Cannot use IOVA as 'PA' since physical addresses are not available");
703                 rte_errno = EINVAL;
704                 return -1;
705         }
706         if (iova_mode == RTE_IOVA_DC) {
707                 RTE_LOG(DEBUG, EAL, "Specific IOVA mode is not requested, autodetecting\n");
708                 if (has_phys_addr) {
709                         RTE_LOG(DEBUG, EAL, "Selecting IOVA mode according to bus requests\n");
710                         iova_mode = rte_bus_get_iommu_class();
711                         if (iova_mode == RTE_IOVA_DC)
712                                 iova_mode = RTE_IOVA_PA;
713                 } else {
714                         iova_mode = RTE_IOVA_VA;
715                 }
716         }
717         rte_eal_get_configuration()->iova_mode = iova_mode;
718         RTE_LOG(INFO, EAL, "Selected IOVA mode '%s'\n",
719                 rte_eal_iova_mode() == RTE_IOVA_PA ? "PA" : "VA");
720
721         if (internal_conf->no_hugetlbfs == 0) {
722                 /* rte_config isn't initialized yet */
723                 ret = internal_conf->process_type == RTE_PROC_PRIMARY ?
724                         eal_hugepage_info_init() :
725                         eal_hugepage_info_read();
726                 if (ret < 0) {
727                         rte_eal_init_alert("Cannot get hugepage information.");
728                         rte_errno = EACCES;
729                         __atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
730                         return -1;
731                 }
732         }
733
734         if (internal_conf->memory == 0 && internal_conf->force_sockets == 0) {
735                 if (internal_conf->no_hugetlbfs)
736                         internal_conf->memory = MEMSIZE_IF_NO_HUGE_PAGE;
737                 else
738                         internal_conf->memory = eal_get_hugepage_mem_size();
739         }
740
741         if (internal_conf->vmware_tsc_map == 1) {
742 #ifdef RTE_LIBRTE_EAL_VMWARE_TSC_MAP_SUPPORT
743                 rte_cycles_vmware_tsc_map = 1;
744                 RTE_LOG (DEBUG, EAL, "Using VMWARE TSC MAP, "
745                                 "you must have monitor_control.pseudo_perfctr = TRUE\n");
746 #else
747                 RTE_LOG (WARNING, EAL, "Ignoring --vmware-tsc-map because "
748                                 "RTE_LIBRTE_EAL_VMWARE_TSC_MAP_SUPPORT is not set\n");
749 #endif
750         }
751
752         /* in secondary processes, memory init may allocate additional fbarrays
753          * not present in primary processes, so to avoid any potential issues,
754          * initialize memzones first.
755          */
756         if (rte_eal_memzone_init() < 0) {
757                 rte_eal_init_alert("Cannot init memzone");
758                 rte_errno = ENODEV;
759                 return -1;
760         }
761
762         if (rte_eal_memory_init() < 0) {
763                 rte_eal_init_alert("Cannot init memory");
764                 rte_errno = ENOMEM;
765                 return -1;
766         }
767
768         if (rte_eal_malloc_heap_init() < 0) {
769                 rte_eal_init_alert("Cannot init malloc heap");
770                 rte_errno = ENODEV;
771                 return -1;
772         }
773
774         if (rte_eal_tailqs_init() < 0) {
775                 rte_eal_init_alert("Cannot init tail queues for objects");
776                 rte_errno = EFAULT;
777                 return -1;
778         }
779
780         if (rte_eal_timer_init() < 0) {
781                 rte_eal_init_alert("Cannot init HPET or TSC timers");
782                 rte_errno = ENOTSUP;
783                 return -1;
784         }
785
786         eal_check_mem_on_local_socket();
787
788         if (pthread_setaffinity_np(pthread_self(), sizeof(rte_cpuset_t),
789                         &lcore_config[config->main_lcore].cpuset) != 0) {
790                 rte_eal_init_alert("Cannot set affinity");
791                 rte_errno = EINVAL;
792                 return -1;
793         }
794         __rte_thread_init(config->main_lcore,
795                 &lcore_config[config->main_lcore].cpuset);
796
797         ret = eal_thread_dump_current_affinity(cpuset, sizeof(cpuset));
798
799         RTE_LOG(DEBUG, EAL, "Main lcore %u is ready (tid=%p;cpuset=[%s%s])\n",
800                 config->main_lcore, thread_id, cpuset,
801                 ret == 0 ? "" : "...");
802
803         RTE_LCORE_FOREACH_WORKER(i) {
804
805                 /*
806                  * create communication pipes between main thread
807                  * and children
808                  */
809                 if (pipe(lcore_config[i].pipe_main2worker) < 0)
810                         rte_panic("Cannot create pipe\n");
811                 if (pipe(lcore_config[i].pipe_worker2main) < 0)
812                         rte_panic("Cannot create pipe\n");
813
814                 lcore_config[i].state = WAIT;
815
816                 /* create a thread for each lcore */
817                 ret = pthread_create(&lcore_config[i].thread_id, NULL,
818                                      eal_thread_loop, NULL);
819                 if (ret != 0)
820                         rte_panic("Cannot create thread\n");
821
822                 /* Set thread_name for aid in debugging. */
823                 snprintf(thread_name, sizeof(thread_name),
824                                 "lcore-worker-%d", i);
825                 rte_thread_setname(lcore_config[i].thread_id, thread_name);
826
827                 ret = pthread_setaffinity_np(lcore_config[i].thread_id,
828                         sizeof(rte_cpuset_t), &lcore_config[i].cpuset);
829                 if (ret != 0)
830                         rte_panic("Cannot set affinity\n");
831         }
832
833         /*
834          * Launch a dummy function on all worker lcores, so that main lcore
835          * knows they are all ready when this function returns.
836          */
837         rte_eal_mp_remote_launch(sync_func, NULL, SKIP_MAIN);
838         rte_eal_mp_wait_lcore();
839
840         /* initialize services so vdevs register service during bus_probe. */
841         ret = rte_service_init();
842         if (ret) {
843                 rte_eal_init_alert("rte_service_init() failed");
844                 rte_errno = -ret;
845                 return -1;
846         }
847
848         /* Probe all the buses and devices/drivers on them */
849         if (rte_bus_probe()) {
850                 rte_eal_init_alert("Cannot probe devices");
851                 rte_errno = ENOTSUP;
852                 return -1;
853         }
854
855         /* initialize default service/lcore mappings and start running. Ignore
856          * -ENOTSUP, as it indicates no service coremask passed to EAL.
857          */
858         ret = rte_service_start_with_defaults();
859         if (ret < 0 && ret != -ENOTSUP) {
860                 rte_errno = -ret;
861                 return -1;
862         }
863
864         /*
865          * Clean up unused files in runtime directory. We do this at the end of
866          * init and not at the beginning because we want to clean stuff up
867          * whether we are primary or secondary process, but we cannot remove
868          * primary process' files because secondary should be able to run even
869          * if primary process is dead.
870          *
871          * In no_shconf mode, no runtime directory is created in the first
872          * place, so no cleanup needed.
873          */
874         if (!internal_conf->no_shconf && eal_clean_runtime_dir() < 0) {
875                 rte_eal_init_alert("Cannot clear runtime directory");
876                 return -1;
877         }
878         if (rte_eal_process_type() == RTE_PROC_PRIMARY && !internal_conf->no_telemetry) {
879                 int tlog = rte_log_register_type_and_pick_level(
880                                 "lib.telemetry", RTE_LOG_WARNING);
881                 if (tlog < 0)
882                         tlog = RTE_LOGTYPE_EAL;
883                 if (rte_telemetry_init(rte_eal_get_runtime_dir(),
884                                 rte_version(),
885                                 &internal_conf->ctrl_cpuset, rte_log, tlog) != 0)
886                         return -1;
887         }
888
889         eal_mcfg_complete();
890
891         return fctret;
892 }
893
894 int
895 rte_eal_cleanup(void)
896 {
897         struct internal_config *internal_conf =
898                 eal_get_internal_configuration();
899         rte_service_finalize();
900         rte_mp_channel_cleanup();
901         /* after this point, any DPDK pointers will become dangling */
902         rte_eal_memory_detach();
903         rte_eal_alarm_cleanup();
904         rte_trace_save();
905         eal_trace_fini();
906         eal_cleanup_config(internal_conf);
907         return 0;
908 }
909
910 int rte_eal_create_uio_dev(void)
911 {
912         const struct internal_config *internal_conf =
913                 eal_get_internal_configuration();
914         return internal_conf->create_uio_dev;
915 }
916
917 enum rte_intr_mode
918 rte_eal_vfio_intr_mode(void)
919 {
920         return RTE_INTR_MODE_NONE;
921 }
922
923 void
924 rte_eal_vfio_get_vf_token(__rte_unused rte_uuid_t vf_token)
925 {
926 }
927
928 int rte_vfio_setup_device(__rte_unused const char *sysfs_base,
929                       __rte_unused const char *dev_addr,
930                       __rte_unused int *vfio_dev_fd,
931                       __rte_unused struct vfio_device_info *device_info)
932 {
933         rte_errno = ENOTSUP;
934         return -1;
935 }
936
937 int rte_vfio_release_device(__rte_unused const char *sysfs_base,
938                         __rte_unused const char *dev_addr,
939                         __rte_unused int fd)
940 {
941         rte_errno = ENOTSUP;
942         return -1;
943 }
944
945 int rte_vfio_enable(__rte_unused const char *modname)
946 {
947         rte_errno = ENOTSUP;
948         return -1;
949 }
950
951 int rte_vfio_is_enabled(__rte_unused const char *modname)
952 {
953         return 0;
954 }
955
956 int rte_vfio_noiommu_is_enabled(void)
957 {
958         return 0;
959 }
960
961 int rte_vfio_clear_group(__rte_unused int vfio_group_fd)
962 {
963         rte_errno = ENOTSUP;
964         return -1;
965 }
966
967 int
968 rte_vfio_get_group_num(__rte_unused const char *sysfs_base,
969                        __rte_unused const char *dev_addr,
970                        __rte_unused int *iommu_group_num)
971 {
972         rte_errno = ENOTSUP;
973         return -1;
974 }
975
976 int
977 rte_vfio_get_container_fd(void)
978 {
979         rte_errno = ENOTSUP;
980         return -1;
981 }
982
983 int
984 rte_vfio_get_group_fd(__rte_unused int iommu_group_num)
985 {
986         rte_errno = ENOTSUP;
987         return -1;
988 }
989
990 int
991 rte_vfio_container_create(void)
992 {
993         rte_errno = ENOTSUP;
994         return -1;
995 }
996
997 int
998 rte_vfio_container_destroy(__rte_unused int container_fd)
999 {
1000         rte_errno = ENOTSUP;
1001         return -1;
1002 }
1003
1004 int
1005 rte_vfio_container_group_bind(__rte_unused int container_fd,
1006                 __rte_unused int iommu_group_num)
1007 {
1008         rte_errno = ENOTSUP;
1009         return -1;
1010 }
1011
1012 int
1013 rte_vfio_container_group_unbind(__rte_unused int container_fd,
1014                 __rte_unused int iommu_group_num)
1015 {
1016         rte_errno = ENOTSUP;
1017         return -1;
1018 }
1019
1020 int
1021 rte_vfio_container_dma_map(__rte_unused int container_fd,
1022                         __rte_unused uint64_t vaddr,
1023                         __rte_unused uint64_t iova,
1024                         __rte_unused uint64_t len)
1025 {
1026         rte_errno = ENOTSUP;
1027         return -1;
1028 }
1029
1030 int
1031 rte_vfio_container_dma_unmap(__rte_unused int container_fd,
1032                         __rte_unused uint64_t vaddr,
1033                         __rte_unused uint64_t iova,
1034                         __rte_unused uint64_t len)
1035 {
1036         rte_errno = ENOTSUP;
1037         return -1;
1038 }