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