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