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