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