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