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