1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2018 Intel Corporation.
3 * Copyright(c) 2014 6WIND S.A.
20 #include <sys/queue.h>
22 #include <rte_compat.h>
23 #include <rte_common.h>
24 #include <rte_debug.h>
25 #include <rte_memory.h>
26 #include <rte_launch.h>
28 #include <rte_eal_memconfig.h>
29 #include <rte_errno.h>
30 #include <rte_per_lcore.h>
31 #include <rte_lcore.h>
32 #include <rte_service_component.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>
41 #include <rte_devargs.h>
42 #include <rte_version.h>
44 #include <rte_atomic.h>
45 #include <malloc_heap.h>
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"
54 #define MEMSIZE_IF_NO_HUGE_PAGE (64ULL * 1024ULL * 1024ULL)
56 /* Allow the application to print its usage message too if set */
57 static rte_usage_hook_t rte_application_usage_hook = NULL;
58 /* early configuration structure, when memory config is not mmapped */
59 static struct rte_mem_config early_mem_config;
61 /* define fd variable here, because file needs to be kept open for the
62 * duration of the program, as we hold a write lock on it in the primary proc */
63 static int mem_cfg_fd = -1;
65 static struct flock wr_lock = {
68 .l_start = offsetof(struct rte_mem_config, memsegs),
69 .l_len = sizeof(early_mem_config.memsegs),
72 /* Address of global and public configuration */
73 static struct rte_config rte_config = {
74 .mem_config = &early_mem_config,
77 /* internal configuration (per-core) */
78 struct lcore_config lcore_config[RTE_MAX_LCORE];
80 /* internal configuration */
81 struct internal_config internal_config;
83 /* used by rte_rdtsc() */
84 int rte_cycles_vmware_tsc_map;
86 /* Return user provided mbuf pool ops name */
87 const char * __rte_experimental
88 rte_eal_mbuf_user_pool_ops(void)
90 return internal_config.user_mbuf_pool_ops_name;
93 /* Return mbuf pool ops name */
95 rte_eal_mbuf_default_mempool_ops(void)
97 if (internal_config.user_mbuf_pool_ops_name == NULL)
98 return RTE_MBUF_DEFAULT_MEMPOOL_OPS;
100 return internal_config.user_mbuf_pool_ops_name;
103 /* Return a pointer to the configuration structure */
105 rte_eal_get_configuration(void)
111 rte_eal_iova_mode(void)
113 return rte_eal_get_configuration()->iova_mode;
116 /* parse a sysfs (or other) file containing one integer value */
118 eal_parse_sysfs_value(const char *filename, unsigned long *val)
124 if ((f = fopen(filename, "r")) == NULL) {
125 RTE_LOG(ERR, EAL, "%s(): cannot open sysfs value %s\n",
130 if (fgets(buf, sizeof(buf), f) == NULL) {
131 RTE_LOG(ERR, EAL, "%s(): cannot read sysfs value %s\n",
136 *val = strtoul(buf, &end, 0);
137 if ((buf[0] == '\0') || (end == NULL) || (*end != '\n')) {
138 RTE_LOG(ERR, EAL, "%s(): cannot parse sysfs value %s\n",
148 /* create memory configuration in shared/mmap memory. Take out
149 * a write lock on the memsegs, so we can auto-detect primary/secondary.
150 * This means we never close the file while running (auto-close on exit).
151 * We also don't lock the whole file, so that in future we can use read-locks
152 * on other parts, e.g. memzones, to detect if there are running secondary
155 rte_eal_config_create(void)
157 void *rte_mem_cfg_addr;
160 const char *pathname = eal_runtime_config_path();
162 if (internal_config.no_shconf)
166 mem_cfg_fd = open(pathname, O_RDWR | O_CREAT, 0660);
168 rte_panic("Cannot open '%s' for rte_mem_config\n", pathname);
171 retval = ftruncate(mem_cfg_fd, sizeof(*rte_config.mem_config));
174 rte_panic("Cannot resize '%s' for rte_mem_config\n", pathname);
177 retval = fcntl(mem_cfg_fd, F_SETLK, &wr_lock);
180 rte_exit(EXIT_FAILURE, "Cannot create lock on '%s'. Is another primary "
181 "process running?\n", pathname);
184 rte_mem_cfg_addr = mmap(NULL, sizeof(*rte_config.mem_config),
185 PROT_READ | PROT_WRITE, MAP_SHARED, mem_cfg_fd, 0);
187 if (rte_mem_cfg_addr == MAP_FAILED){
188 rte_panic("Cannot mmap memory for rte_config\n");
190 memcpy(rte_mem_cfg_addr, &early_mem_config, sizeof(early_mem_config));
191 rte_config.mem_config = rte_mem_cfg_addr;
194 /* attach to an existing shared memory config */
196 rte_eal_config_attach(void)
198 void *rte_mem_cfg_addr;
199 const char *pathname = eal_runtime_config_path();
201 if (internal_config.no_shconf)
205 mem_cfg_fd = open(pathname, O_RDWR);
207 rte_panic("Cannot open '%s' for rte_mem_config\n", pathname);
210 rte_mem_cfg_addr = mmap(NULL, sizeof(*rte_config.mem_config),
211 PROT_READ | PROT_WRITE, MAP_SHARED, mem_cfg_fd, 0);
213 if (rte_mem_cfg_addr == MAP_FAILED)
214 rte_panic("Cannot mmap memory for rte_config\n");
216 rte_config.mem_config = rte_mem_cfg_addr;
219 /* Detect if we are a primary or a secondary process */
221 eal_proc_type_detect(void)
223 enum rte_proc_type_t ptype = RTE_PROC_PRIMARY;
224 const char *pathname = eal_runtime_config_path();
226 /* if we can open the file but not get a write-lock we are a secondary
227 * process. NOTE: if we get a file handle back, we keep that open
228 * and don't close it to prevent a race condition between multiple opens */
229 if (((mem_cfg_fd = open(pathname, O_RDWR)) >= 0) &&
230 (fcntl(mem_cfg_fd, F_SETLK, &wr_lock) < 0))
231 ptype = RTE_PROC_SECONDARY;
233 RTE_LOG(INFO, EAL, "Auto-detected process type: %s\n",
234 ptype == RTE_PROC_PRIMARY ? "PRIMARY" : "SECONDARY");
239 /* Sets up rte_config structure with the pointer to shared memory config.*/
241 rte_config_init(void)
243 rte_config.process_type = internal_config.process_type;
245 switch (rte_config.process_type){
246 case RTE_PROC_PRIMARY:
247 rte_eal_config_create();
249 case RTE_PROC_SECONDARY:
250 rte_eal_config_attach();
251 rte_eal_mcfg_wait_complete(rte_config.mem_config);
254 case RTE_PROC_INVALID:
255 rte_panic("Invalid process type\n");
261 eal_usage(const char *prgname)
263 printf("\nUsage: %s ", prgname);
265 /* Allow the application to print its usage message too if hook is set */
266 if ( rte_application_usage_hook ) {
267 printf("===== Application Usage =====\n\n");
268 rte_application_usage_hook(prgname);
272 /* Set a per-application usage message */
274 rte_set_application_usage_hook( rte_usage_hook_t usage_func )
276 rte_usage_hook_t old_func;
278 /* Will be NULL on the first call to denote the last usage routine. */
279 old_func = rte_application_usage_hook;
280 rte_application_usage_hook = usage_func;
286 eal_get_hugepage_mem_size(void)
291 for (i = 0; i < internal_config.num_hugepage_sizes; i++) {
292 struct hugepage_info *hpi = &internal_config.hugepage_info[i];
293 if (strnlen(hpi->hugedir, sizeof(hpi->hugedir)) != 0) {
294 for (j = 0; j < RTE_MAX_NUMA_NODES; j++) {
295 size += hpi->hugepage_sz * hpi->num_pages[j];
300 return (size < SIZE_MAX) ? (size_t)(size) : SIZE_MAX;
303 /* Parse the arguments for --log-level only */
305 eal_log_level_parse(int argc, char **argv)
310 const int old_optind = optind;
311 const int old_optopt = optopt;
312 const int old_optreset = optreset;
313 char * const old_optarg = optarg;
319 while ((opt = getopt_long(argc, argvopt, eal_short_options,
320 eal_long_options, &option_index)) != EOF) {
324 /* getopt is not happy, stop right now */
328 ret = (opt == OPT_LOG_LEVEL_NUM) ?
329 eal_parse_common_option(opt, optarg, &internal_config) : 0;
331 /* common parser is not happy */
336 /* restore getopt lib */
339 optreset = old_optreset;
343 /* Parse the argument given in the command line of the application */
345 eal_parse_args(int argc, char **argv)
350 char *prgname = argv[0];
351 const int old_optind = optind;
352 const int old_optopt = optopt;
353 const int old_optreset = optreset;
354 char * const old_optarg = optarg;
360 while ((opt = getopt_long(argc, argvopt, eal_short_options,
361 eal_long_options, &option_index)) != EOF) {
363 /* getopt is not happy, stop right now */
370 ret = eal_parse_common_option(opt, optarg, &internal_config);
371 /* common parser is not happy */
377 /* common parser handled this option */
382 case OPT_MBUF_POOL_OPS_NAME_NUM:
383 internal_config.user_mbuf_pool_ops_name = optarg;
389 if (opt < OPT_LONG_MIN_NUM && isprint(opt)) {
390 RTE_LOG(ERR, EAL, "Option %c is not supported "
391 "on FreeBSD\n", opt);
392 } else if (opt >= OPT_LONG_MIN_NUM &&
393 opt < OPT_LONG_MAX_NUM) {
394 RTE_LOG(ERR, EAL, "Option %s is not supported "
396 eal_long_options[option_index].name);
398 RTE_LOG(ERR, EAL, "Option %d is not supported "
399 "on FreeBSD\n", opt);
407 if (eal_adjust_config(&internal_config) != 0) {
413 if (eal_check_common_options(&internal_config) != 0) {
420 argv[optind-1] = prgname;
424 /* restore getopt lib */
427 optreset = old_optreset;
434 check_socket(const struct rte_memseg_list *msl, void *arg)
436 int *socket_id = arg;
438 if (msl->socket_id == *socket_id && msl->memseg_arr.count != 0)
445 eal_check_mem_on_local_socket(void)
449 socket_id = rte_lcore_to_socket_id(rte_config.master_lcore);
451 if (rte_memseg_list_walk(check_socket, &socket_id) == 0)
452 RTE_LOG(WARNING, EAL, "WARNING: Master core has no memory on local socket!\n");
457 sync_func(__attribute__((unused)) void *arg)
463 rte_eal_mcfg_complete(void)
465 /* ALL shared mem_config related INIT DONE */
466 if (rte_config.process_type == RTE_PROC_PRIMARY)
467 rte_config.mem_config->magic = RTE_MAGIC;
470 /* return non-zero if hugepages are enabled. */
471 int rte_eal_has_hugepages(void)
473 return !internal_config.no_hugetlbfs;
476 /* Abstraction for port I/0 privilege */
478 rte_eal_iopl_init(void)
482 fd = open("/dev/io", O_RDWR);
485 /* keep fd open for iopl */
489 static void rte_eal_init_alert(const char *msg)
491 fprintf(stderr, "EAL: FATAL: %s\n", msg);
492 RTE_LOG(ERR, EAL, "%s\n", msg);
495 /* Launch threads, called at application init(). */
497 rte_eal_init(int argc, char **argv)
501 static rte_atomic32_t run_once = RTE_ATOMIC32_INIT(0);
502 char cpuset[RTE_CPU_AFFINITY_STR_LEN];
503 char thread_name[RTE_MAX_THREAD_NAME_LEN];
505 /* checks if the machine is adequate */
506 if (!rte_cpu_is_supported()) {
507 rte_eal_init_alert("unsupported cpu type.");
512 if (!rte_atomic32_test_and_set(&run_once)) {
513 rte_eal_init_alert("already called initialization.");
514 rte_errno = EALREADY;
518 thread_id = pthread_self();
520 eal_reset_internal_config(&internal_config);
522 /* set log level as early as possible */
523 eal_log_level_parse(argc, argv);
525 if (rte_eal_cpu_init() < 0) {
526 rte_eal_init_alert("Cannot detect lcores.");
531 fctret = eal_parse_args(argc, argv);
533 rte_eal_init_alert("Invalid 'command line' arguments.");
535 rte_atomic32_clear(&run_once);
539 /* FreeBSD always uses legacy memory model */
540 internal_config.legacy_mem = true;
542 if (eal_plugins_init() < 0) {
543 rte_eal_init_alert("Cannot init plugins\n");
545 rte_atomic32_clear(&run_once);
549 if (eal_option_device_parse()) {
551 rte_atomic32_clear(&run_once);
557 /* Put mp channel init before bus scan so that we can init the vdev
558 * bus through mp channel in the secondary process before the bus scan.
560 if (rte_mp_channel_init() < 0) {
561 rte_eal_init_alert("failed to init mp channel\n");
562 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
568 if (rte_bus_scan()) {
569 rte_eal_init_alert("Cannot scan the buses for devices\n");
571 rte_atomic32_clear(&run_once);
575 /* autodetect the iova mapping mode (default is iova_pa) */
576 rte_eal_get_configuration()->iova_mode = rte_bus_get_iommu_class();
578 if (internal_config.no_hugetlbfs == 0) {
579 /* rte_config isn't initialized yet */
580 ret = internal_config.process_type == RTE_PROC_PRIMARY ?
581 eal_hugepage_info_init() :
582 eal_hugepage_info_read();
584 rte_eal_init_alert("Cannot get hugepage information.");
586 rte_atomic32_clear(&run_once);
591 if (internal_config.memory == 0 && internal_config.force_sockets == 0) {
592 if (internal_config.no_hugetlbfs)
593 internal_config.memory = MEMSIZE_IF_NO_HUGE_PAGE;
595 internal_config.memory = eal_get_hugepage_mem_size();
598 if (internal_config.vmware_tsc_map == 1) {
599 #ifdef RTE_LIBRTE_EAL_VMWARE_TSC_MAP_SUPPORT
600 rte_cycles_vmware_tsc_map = 1;
601 RTE_LOG (DEBUG, EAL, "Using VMWARE TSC MAP, "
602 "you must have monitor_control.pseudo_perfctr = TRUE\n");
604 RTE_LOG (WARNING, EAL, "Ignoring --vmware-tsc-map because "
605 "RTE_LIBRTE_EAL_VMWARE_TSC_MAP_SUPPORT is not set\n");
609 rte_srand(rte_rdtsc());
611 /* in secondary processes, memory init may allocate additional fbarrays
612 * not present in primary processes, so to avoid any potential issues,
613 * initialize memzones first.
615 if (rte_eal_memzone_init() < 0) {
616 rte_eal_init_alert("Cannot init memzone\n");
621 if (rte_eal_memory_init() < 0) {
622 rte_eal_init_alert("Cannot init memory\n");
627 if (rte_eal_malloc_heap_init() < 0) {
628 rte_eal_init_alert("Cannot init malloc heap\n");
633 if (rte_eal_tailqs_init() < 0) {
634 rte_eal_init_alert("Cannot init tail queues for objects\n");
639 if (rte_eal_alarm_init() < 0) {
640 rte_eal_init_alert("Cannot init interrupt-handling thread\n");
641 /* rte_eal_alarm_init sets rte_errno on failure. */
645 if (rte_eal_intr_init() < 0) {
646 rte_eal_init_alert("Cannot init interrupt-handling thread\n");
650 if (rte_eal_timer_init() < 0) {
651 rte_eal_init_alert("Cannot init HPET or TSC timers\n");
656 eal_check_mem_on_local_socket();
658 eal_thread_init_master(rte_config.master_lcore);
660 ret = eal_thread_dump_affinity(cpuset, sizeof(cpuset));
662 RTE_LOG(DEBUG, EAL, "Master lcore %u is ready (tid=%p;cpuset=[%s%s])\n",
663 rte_config.master_lcore, thread_id, cpuset,
664 ret == 0 ? "" : "...");
666 RTE_LCORE_FOREACH_SLAVE(i) {
669 * create communication pipes between master thread
672 if (pipe(lcore_config[i].pipe_master2slave) < 0)
673 rte_panic("Cannot create pipe\n");
674 if (pipe(lcore_config[i].pipe_slave2master) < 0)
675 rte_panic("Cannot create pipe\n");
677 lcore_config[i].state = WAIT;
679 /* create a thread for each lcore */
680 ret = pthread_create(&lcore_config[i].thread_id, NULL,
681 eal_thread_loop, NULL);
683 rte_panic("Cannot create thread\n");
685 /* Set thread_name for aid in debugging. */
686 snprintf(thread_name, sizeof(thread_name),
687 "lcore-slave-%d", i);
688 rte_thread_setname(lcore_config[i].thread_id, thread_name);
692 * Launch a dummy function on all slave lcores, so that master lcore
693 * knows they are all ready when this function returns.
695 rte_eal_mp_remote_launch(sync_func, NULL, SKIP_MASTER);
696 rte_eal_mp_wait_lcore();
698 /* initialize services so vdevs register service during bus_probe. */
699 ret = rte_service_init();
701 rte_eal_init_alert("rte_service_init() failed\n");
706 /* Probe all the buses and devices/drivers on them */
707 if (rte_bus_probe()) {
708 rte_eal_init_alert("Cannot probe devices\n");
713 /* initialize default service/lcore mappings and start running. Ignore
714 * -ENOTSUP, as it indicates no service coremask passed to EAL.
716 ret = rte_service_start_with_defaults();
717 if (ret < 0 && ret != -ENOTSUP) {
722 rte_eal_mcfg_complete();
727 int __rte_experimental
728 rte_eal_cleanup(void)
730 rte_service_finalize();
735 enum rte_lcore_role_t
736 rte_eal_lcore_role(unsigned lcore_id)
738 return rte_config.lcore_role[lcore_id];
742 rte_eal_process_type(void)
744 return rte_config.process_type;
747 int rte_eal_has_pci(void)
749 return !internal_config.no_pci;
752 int rte_eal_create_uio_dev(void)
754 return internal_config.create_uio_dev;
758 rte_eal_vfio_intr_mode(void)
760 return RTE_INTR_MODE_NONE;
763 int rte_vfio_setup_device(__rte_unused const char *sysfs_base,
764 __rte_unused const char *dev_addr,
765 __rte_unused int *vfio_dev_fd,
766 __rte_unused struct vfio_device_info *device_info)
771 int rte_vfio_release_device(__rte_unused const char *sysfs_base,
772 __rte_unused const char *dev_addr,
778 int rte_vfio_enable(__rte_unused const char *modname)
783 int rte_vfio_is_enabled(__rte_unused const char *modname)
788 int rte_vfio_noiommu_is_enabled(void)
793 int rte_vfio_clear_group(__rte_unused int vfio_group_fd)
798 int __rte_experimental
799 rte_vfio_dma_map(uint64_t __rte_unused vaddr, __rte_unused uint64_t iova,
800 __rte_unused uint64_t len)
805 int __rte_experimental
806 rte_vfio_dma_unmap(uint64_t __rte_unused vaddr, uint64_t __rte_unused iova,
807 __rte_unused uint64_t len)
812 int __rte_experimental
813 rte_vfio_get_group_num(__rte_unused const char *sysfs_base,
814 __rte_unused const char *dev_addr,
815 __rte_unused int *iommu_group_num)
820 int __rte_experimental
821 rte_vfio_get_container_fd(void)
826 int __rte_experimental
827 rte_vfio_get_group_fd(__rte_unused int iommu_group_num)
832 int __rte_experimental
833 rte_vfio_container_create(void)
838 int __rte_experimental
839 rte_vfio_container_destroy(__rte_unused int container_fd)
844 int __rte_experimental
845 rte_vfio_container_group_bind(__rte_unused int container_fd,
846 __rte_unused int iommu_group_num)
851 int __rte_experimental
852 rte_vfio_container_group_unbind(__rte_unused int container_fd,
853 __rte_unused int iommu_group_num)
858 int __rte_experimental
859 rte_vfio_container_dma_map(__rte_unused int container_fd,
860 __rte_unused uint64_t vaddr,
861 __rte_unused uint64_t iova,
862 __rte_unused uint64_t len)
867 int __rte_experimental
868 rte_vfio_container_dma_unmap(__rte_unused int container_fd,
869 __rte_unused uint64_t vaddr,
870 __rte_unused uint64_t iova,
871 __rte_unused uint64_t len)