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