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