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