41ddb5a2257438e3211c3da7a77a627a071409da
[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, internal_config.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                         internal_config.user_mbuf_pool_ops_name =
539                             strdup(optarg);
540                         break;
541                 case 'h':
542                         eal_usage(prgname);
543                         exit(EXIT_SUCCESS);
544                 default:
545                         if (opt < OPT_LONG_MIN_NUM && isprint(opt)) {
546                                 RTE_LOG(ERR, EAL, "Option %c is not supported "
547                                         "on FreeBSD\n", opt);
548                         } else if (opt >= OPT_LONG_MIN_NUM &&
549                                    opt < OPT_LONG_MAX_NUM) {
550                                 RTE_LOG(ERR, EAL, "Option %s is not supported "
551                                         "on FreeBSD\n",
552                                         eal_long_options[option_index].name);
553                         } else {
554                                 RTE_LOG(ERR, EAL, "Option %d is not supported "
555                                         "on FreeBSD\n", opt);
556                         }
557                         eal_usage(prgname);
558                         ret = -1;
559                         goto out;
560                 }
561         }
562
563         /* create runtime data directory */
564         if (internal_config.no_shconf == 0 &&
565                         eal_create_runtime_dir() < 0) {
566                 RTE_LOG(ERR, EAL, "Cannot create runtime directory\n");
567                 ret = -1;
568                 goto out;
569         }
570
571         if (eal_adjust_config(&internal_config) != 0) {
572                 ret = -1;
573                 goto out;
574         }
575
576         /* sanity checks */
577         if (eal_check_common_options(&internal_config) != 0) {
578                 eal_usage(prgname);
579                 ret = -1;
580                 goto out;
581         }
582
583         if (optind >= 0)
584                 argv[optind-1] = prgname;
585         ret = optind-1;
586
587 out:
588         /* restore getopt lib */
589         optind = old_optind;
590         optopt = old_optopt;
591         optreset = old_optreset;
592         optarg = old_optarg;
593
594         return ret;
595 }
596
597 static int
598 check_socket(const struct rte_memseg_list *msl, void *arg)
599 {
600         int *socket_id = arg;
601
602         if (msl->external)
603                 return 0;
604
605         if (msl->socket_id == *socket_id && msl->memseg_arr.count != 0)
606                 return 1;
607
608         return 0;
609 }
610
611 static void
612 eal_check_mem_on_local_socket(void)
613 {
614         int socket_id;
615
616         socket_id = rte_lcore_to_socket_id(rte_config.master_lcore);
617
618         if (rte_memseg_list_walk(check_socket, &socket_id) == 0)
619                 RTE_LOG(WARNING, EAL, "WARNING: Master core has no memory on local socket!\n");
620 }
621
622
623 static int
624 sync_func(__attribute__((unused)) void *arg)
625 {
626         return 0;
627 }
628
629 inline static void
630 rte_eal_mcfg_complete(void)
631 {
632         /* ALL shared mem_config related INIT DONE */
633         if (rte_config.process_type == RTE_PROC_PRIMARY)
634                 rte_config.mem_config->magic = RTE_MAGIC;
635 }
636
637 /* return non-zero if hugepages are enabled. */
638 int rte_eal_has_hugepages(void)
639 {
640         return !internal_config.no_hugetlbfs;
641 }
642
643 /* Abstraction for port I/0 privilege */
644 int
645 rte_eal_iopl_init(void)
646 {
647         static int fd = -1;
648
649         if (fd < 0)
650                 fd = open("/dev/io", O_RDWR);
651
652         if (fd < 0)
653                 return -1;
654         /* keep fd open for iopl */
655         return 0;
656 }
657
658 static void rte_eal_init_alert(const char *msg)
659 {
660         fprintf(stderr, "EAL: FATAL: %s\n", msg);
661         RTE_LOG(ERR, EAL, "%s\n", msg);
662 }
663
664 /* Launch threads, called at application init(). */
665 int
666 rte_eal_init(int argc, char **argv)
667 {
668         int i, fctret, ret;
669         pthread_t thread_id;
670         static rte_atomic32_t run_once = RTE_ATOMIC32_INIT(0);
671         char cpuset[RTE_CPU_AFFINITY_STR_LEN];
672         char thread_name[RTE_MAX_THREAD_NAME_LEN];
673
674         /* checks if the machine is adequate */
675         if (!rte_cpu_is_supported()) {
676                 rte_eal_init_alert("unsupported cpu type.");
677                 rte_errno = ENOTSUP;
678                 return -1;
679         }
680
681         if (!rte_atomic32_test_and_set(&run_once)) {
682                 rte_eal_init_alert("already called initialization.");
683                 rte_errno = EALREADY;
684                 return -1;
685         }
686
687         thread_id = pthread_self();
688
689         eal_reset_internal_config(&internal_config);
690
691         /* set log level as early as possible */
692         eal_log_level_parse(argc, argv);
693
694         if (rte_eal_cpu_init() < 0) {
695                 rte_eal_init_alert("Cannot detect lcores.");
696                 rte_errno = ENOTSUP;
697                 return -1;
698         }
699
700         fctret = eal_parse_args(argc, argv);
701         if (fctret < 0) {
702                 rte_eal_init_alert("Invalid 'command line' arguments.");
703                 rte_errno = EINVAL;
704                 rte_atomic32_clear(&run_once);
705                 return -1;
706         }
707
708         /* FreeBSD always uses legacy memory model */
709         internal_config.legacy_mem = true;
710
711         if (eal_plugins_init() < 0) {
712                 rte_eal_init_alert("Cannot init plugins");
713                 rte_errno = EINVAL;
714                 rte_atomic32_clear(&run_once);
715                 return -1;
716         }
717
718         if (eal_option_device_parse()) {
719                 rte_errno = ENODEV;
720                 rte_atomic32_clear(&run_once);
721                 return -1;
722         }
723
724         rte_config_init();
725
726         if (rte_eal_intr_init() < 0) {
727                 rte_eal_init_alert("Cannot init interrupt-handling thread");
728                 return -1;
729         }
730
731         /* Put mp channel init before bus scan so that we can init the vdev
732          * bus through mp channel in the secondary process before the bus scan.
733          */
734         if (rte_mp_channel_init() < 0) {
735                 rte_eal_init_alert("failed to init mp channel");
736                 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
737                         rte_errno = EFAULT;
738                         return -1;
739                 }
740         }
741
742         if (rte_bus_scan()) {
743                 rte_eal_init_alert("Cannot scan the buses for devices");
744                 rte_errno = ENODEV;
745                 rte_atomic32_clear(&run_once);
746                 return -1;
747         }
748
749         /* if no EAL option "--iova-mode=<pa|va>", use bus IOVA scheme */
750         if (internal_config.iova_mode == RTE_IOVA_DC) {
751                 /* autodetect the IOVA mapping mode (default is RTE_IOVA_PA) */
752                 rte_eal_get_configuration()->iova_mode =
753                         rte_bus_get_iommu_class();
754         } else {
755                 rte_eal_get_configuration()->iova_mode =
756                         internal_config.iova_mode;
757         }
758
759         if (internal_config.no_hugetlbfs == 0) {
760                 /* rte_config isn't initialized yet */
761                 ret = internal_config.process_type == RTE_PROC_PRIMARY ?
762                         eal_hugepage_info_init() :
763                         eal_hugepage_info_read();
764                 if (ret < 0) {
765                         rte_eal_init_alert("Cannot get hugepage information.");
766                         rte_errno = EACCES;
767                         rte_atomic32_clear(&run_once);
768                         return -1;
769                 }
770         }
771
772         if (internal_config.memory == 0 && internal_config.force_sockets == 0) {
773                 if (internal_config.no_hugetlbfs)
774                         internal_config.memory = MEMSIZE_IF_NO_HUGE_PAGE;
775                 else
776                         internal_config.memory = eal_get_hugepage_mem_size();
777         }
778
779         if (internal_config.vmware_tsc_map == 1) {
780 #ifdef RTE_LIBRTE_EAL_VMWARE_TSC_MAP_SUPPORT
781                 rte_cycles_vmware_tsc_map = 1;
782                 RTE_LOG (DEBUG, EAL, "Using VMWARE TSC MAP, "
783                                 "you must have monitor_control.pseudo_perfctr = TRUE\n");
784 #else
785                 RTE_LOG (WARNING, EAL, "Ignoring --vmware-tsc-map because "
786                                 "RTE_LIBRTE_EAL_VMWARE_TSC_MAP_SUPPORT is not set\n");
787 #endif
788         }
789
790         rte_srand(rte_rdtsc());
791
792         /* in secondary processes, memory init may allocate additional fbarrays
793          * not present in primary processes, so to avoid any potential issues,
794          * initialize memzones first.
795          */
796         if (rte_eal_memzone_init() < 0) {
797                 rte_eal_init_alert("Cannot init memzone");
798                 rte_errno = ENODEV;
799                 return -1;
800         }
801
802         if (rte_eal_memory_init() < 0) {
803                 rte_eal_init_alert("Cannot init memory");
804                 rte_errno = ENOMEM;
805                 return -1;
806         }
807
808         if (rte_eal_malloc_heap_init() < 0) {
809                 rte_eal_init_alert("Cannot init malloc heap");
810                 rte_errno = ENODEV;
811                 return -1;
812         }
813
814         if (rte_eal_tailqs_init() < 0) {
815                 rte_eal_init_alert("Cannot init tail queues for objects");
816                 rte_errno = EFAULT;
817                 return -1;
818         }
819
820         if (rte_eal_alarm_init() < 0) {
821                 rte_eal_init_alert("Cannot init interrupt-handling thread");
822                 /* rte_eal_alarm_init sets rte_errno on failure. */
823                 return -1;
824         }
825
826         if (rte_eal_timer_init() < 0) {
827                 rte_eal_init_alert("Cannot init HPET or TSC timers");
828                 rte_errno = ENOTSUP;
829                 return -1;
830         }
831
832         eal_check_mem_on_local_socket();
833
834         eal_thread_init_master(rte_config.master_lcore);
835
836         ret = eal_thread_dump_affinity(cpuset, sizeof(cpuset));
837
838         RTE_LOG(DEBUG, EAL, "Master lcore %u is ready (tid=%p;cpuset=[%s%s])\n",
839                 rte_config.master_lcore, thread_id, cpuset,
840                 ret == 0 ? "" : "...");
841
842         RTE_LCORE_FOREACH_SLAVE(i) {
843
844                 /*
845                  * create communication pipes between master thread
846                  * and children
847                  */
848                 if (pipe(lcore_config[i].pipe_master2slave) < 0)
849                         rte_panic("Cannot create pipe\n");
850                 if (pipe(lcore_config[i].pipe_slave2master) < 0)
851                         rte_panic("Cannot create pipe\n");
852
853                 lcore_config[i].state = WAIT;
854
855                 /* create a thread for each lcore */
856                 ret = pthread_create(&lcore_config[i].thread_id, NULL,
857                                      eal_thread_loop, NULL);
858                 if (ret != 0)
859                         rte_panic("Cannot create thread\n");
860
861                 /* Set thread_name for aid in debugging. */
862                 snprintf(thread_name, sizeof(thread_name),
863                                 "lcore-slave-%d", i);
864                 rte_thread_setname(lcore_config[i].thread_id, thread_name);
865         }
866
867         /*
868          * Launch a dummy function on all slave lcores, so that master lcore
869          * knows they are all ready when this function returns.
870          */
871         rte_eal_mp_remote_launch(sync_func, NULL, SKIP_MASTER);
872         rte_eal_mp_wait_lcore();
873
874         /* initialize services so vdevs register service during bus_probe. */
875         ret = rte_service_init();
876         if (ret) {
877                 rte_eal_init_alert("rte_service_init() failed");
878                 rte_errno = ENOEXEC;
879                 return -1;
880         }
881
882         /* Probe all the buses and devices/drivers on them */
883         if (rte_bus_probe()) {
884                 rte_eal_init_alert("Cannot probe devices");
885                 rte_errno = ENOTSUP;
886                 return -1;
887         }
888
889         /* initialize default service/lcore mappings and start running. Ignore
890          * -ENOTSUP, as it indicates no service coremask passed to EAL.
891          */
892         ret = rte_service_start_with_defaults();
893         if (ret < 0 && ret != -ENOTSUP) {
894                 rte_errno = ENOEXEC;
895                 return -1;
896         }
897
898         /*
899          * Clean up unused files in runtime directory. We do this at the end of
900          * init and not at the beginning because we want to clean stuff up
901          * whether we are primary or secondary process, but we cannot remove
902          * primary process' files because secondary should be able to run even
903          * if primary process is dead.
904          */
905         if (eal_clean_runtime_dir() < 0) {
906                 rte_eal_init_alert("Cannot clear runtime directory\n");
907                 return -1;
908         }
909
910         rte_eal_mcfg_complete();
911
912         /* Call each registered callback, if enabled */
913         rte_option_init();
914
915         return fctret;
916 }
917
918 int __rte_experimental
919 rte_eal_cleanup(void)
920 {
921         rte_service_finalize();
922         return 0;
923 }
924
925 /* get core role */
926 enum rte_lcore_role_t
927 rte_eal_lcore_role(unsigned lcore_id)
928 {
929         return rte_config.lcore_role[lcore_id];
930 }
931
932 enum rte_proc_type_t
933 rte_eal_process_type(void)
934 {
935         return rte_config.process_type;
936 }
937
938 int rte_eal_has_pci(void)
939 {
940         return !internal_config.no_pci;
941 }
942
943 int rte_eal_create_uio_dev(void)
944 {
945         return internal_config.create_uio_dev;
946 }
947
948 enum rte_intr_mode
949 rte_eal_vfio_intr_mode(void)
950 {
951         return RTE_INTR_MODE_NONE;
952 }
953
954 int rte_vfio_setup_device(__rte_unused const char *sysfs_base,
955                       __rte_unused const char *dev_addr,
956                       __rte_unused int *vfio_dev_fd,
957                       __rte_unused struct vfio_device_info *device_info)
958 {
959         return -1;
960 }
961
962 int rte_vfio_release_device(__rte_unused const char *sysfs_base,
963                         __rte_unused const char *dev_addr,
964                         __rte_unused int fd)
965 {
966         return -1;
967 }
968
969 int rte_vfio_enable(__rte_unused const char *modname)
970 {
971         return -1;
972 }
973
974 int rte_vfio_is_enabled(__rte_unused const char *modname)
975 {
976         return 0;
977 }
978
979 int rte_vfio_noiommu_is_enabled(void)
980 {
981         return 0;
982 }
983
984 int rte_vfio_clear_group(__rte_unused int vfio_group_fd)
985 {
986         return 0;
987 }
988
989 int
990 rte_vfio_dma_map(uint64_t __rte_unused vaddr, __rte_unused uint64_t iova,
991                   __rte_unused uint64_t len)
992 {
993         return -1;
994 }
995
996 int
997 rte_vfio_dma_unmap(uint64_t __rte_unused vaddr, uint64_t __rte_unused iova,
998                     __rte_unused uint64_t len)
999 {
1000         return -1;
1001 }
1002
1003 int
1004 rte_vfio_get_group_num(__rte_unused const char *sysfs_base,
1005                        __rte_unused const char *dev_addr,
1006                        __rte_unused int *iommu_group_num)
1007 {
1008         return -1;
1009 }
1010
1011 int
1012 rte_vfio_get_container_fd(void)
1013 {
1014         return -1;
1015 }
1016
1017 int
1018 rte_vfio_get_group_fd(__rte_unused int iommu_group_num)
1019 {
1020         return -1;
1021 }
1022
1023 int
1024 rte_vfio_container_create(void)
1025 {
1026         return -1;
1027 }
1028
1029 int
1030 rte_vfio_container_destroy(__rte_unused int container_fd)
1031 {
1032         return -1;
1033 }
1034
1035 int
1036 rte_vfio_container_group_bind(__rte_unused int container_fd,
1037                 __rte_unused int iommu_group_num)
1038 {
1039         return -1;
1040 }
1041
1042 int
1043 rte_vfio_container_group_unbind(__rte_unused int container_fd,
1044                 __rte_unused int iommu_group_num)
1045 {
1046         return -1;
1047 }
1048
1049 int
1050 rte_vfio_container_dma_map(__rte_unused int container_fd,
1051                         __rte_unused uint64_t vaddr,
1052                         __rte_unused uint64_t iova,
1053                         __rte_unused uint64_t len)
1054 {
1055         return -1;
1056 }
1057
1058 int
1059 rte_vfio_container_dma_unmap(__rte_unused int container_fd,
1060                         __rte_unused uint64_t vaddr,
1061                         __rte_unused uint64_t iova,
1062                         __rte_unused uint64_t len)
1063 {
1064         return -1;
1065 }