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