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>
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>
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>
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>
42 #include <rte_devargs.h>
43 #include <rte_version.h>
45 #include <rte_atomic.h>
46 #include <malloc_heap.h>
48 #include "eal_private.h"
49 #include "eal_thread.h"
50 #include "eal_internal_cfg.h"
51 #include "eal_filesystem.h"
52 #include "eal_hugepages.h"
53 #include "eal_options.h"
55 #define MEMSIZE_IF_NO_HUGE_PAGE (64ULL * 1024ULL * 1024ULL)
57 /* Allow the application to print its usage message too if set */
58 static rte_usage_hook_t rte_application_usage_hook = NULL;
59 /* early configuration structure, when memory config is not mmapped */
60 static struct rte_mem_config early_mem_config;
62 /* define fd variable here, because file needs to be kept open for the
63 * duration of the program, as we hold a write lock on it in the primary proc */
64 static int mem_cfg_fd = -1;
66 static struct flock wr_lock = {
69 .l_start = offsetof(struct rte_mem_config, memsegs),
70 .l_len = sizeof(early_mem_config.memsegs),
73 /* Address of global and public configuration */
74 static struct rte_config rte_config = {
75 .mem_config = &early_mem_config,
78 /* internal configuration (per-core) */
79 struct lcore_config lcore_config[RTE_MAX_LCORE];
81 /* internal configuration */
82 struct internal_config internal_config;
84 /* used by rte_rdtsc() */
85 int rte_cycles_vmware_tsc_map;
87 /* platform-specific runtime dir */
88 static char runtime_dir[PATH_MAX];
90 static const char *default_runtime_dir = "/var/run";
93 eal_create_runtime_dir(void)
95 const char *directory = default_runtime_dir;
96 const char *xdg_runtime_dir = getenv("XDG_RUNTIME_DIR");
97 const char *fallback = "/tmp";
102 /* try XDG path first, fall back to /tmp */
103 if (xdg_runtime_dir != NULL)
104 directory = xdg_runtime_dir;
106 directory = fallback;
108 /* create DPDK subdirectory under runtime dir */
109 ret = snprintf(tmp, sizeof(tmp), "%s/dpdk", directory);
110 if (ret < 0 || ret == sizeof(tmp)) {
111 RTE_LOG(ERR, EAL, "Error creating DPDK runtime path name\n");
115 /* create prefix-specific subdirectory under DPDK runtime dir */
116 ret = snprintf(runtime_dir, sizeof(runtime_dir), "%s/%s",
117 tmp, internal_config.hugefile_prefix);
118 if (ret < 0 || ret == sizeof(runtime_dir)) {
119 RTE_LOG(ERR, EAL, "Error creating prefix-specific runtime path name\n");
123 /* create the path if it doesn't exist. no "mkdir -p" here, so do it
126 ret = mkdir(tmp, 0600);
127 if (ret < 0 && errno != EEXIST) {
128 RTE_LOG(ERR, EAL, "Error creating '%s': %s\n",
129 tmp, strerror(errno));
133 ret = mkdir(runtime_dir, 0600);
134 if (ret < 0 && errno != EEXIST) {
135 RTE_LOG(ERR, EAL, "Error creating '%s': %s\n",
136 runtime_dir, strerror(errno));
144 eal_get_runtime_dir(void)
149 /* Return user provided mbuf pool ops name */
150 const char * __rte_experimental
151 rte_eal_mbuf_user_pool_ops(void)
153 return internal_config.user_mbuf_pool_ops_name;
156 /* Return mbuf pool ops name */
158 rte_eal_mbuf_default_mempool_ops(void)
160 if (internal_config.user_mbuf_pool_ops_name == NULL)
161 return RTE_MBUF_DEFAULT_MEMPOOL_OPS;
163 return internal_config.user_mbuf_pool_ops_name;
166 /* Return a pointer to the configuration structure */
168 rte_eal_get_configuration(void)
174 rte_eal_iova_mode(void)
176 return rte_eal_get_configuration()->iova_mode;
179 /* parse a sysfs (or other) file containing one integer value */
181 eal_parse_sysfs_value(const char *filename, unsigned long *val)
187 if ((f = fopen(filename, "r")) == NULL) {
188 RTE_LOG(ERR, EAL, "%s(): cannot open sysfs value %s\n",
193 if (fgets(buf, sizeof(buf), f) == NULL) {
194 RTE_LOG(ERR, EAL, "%s(): cannot read sysfs value %s\n",
199 *val = strtoul(buf, &end, 0);
200 if ((buf[0] == '\0') || (end == NULL) || (*end != '\n')) {
201 RTE_LOG(ERR, EAL, "%s(): cannot parse sysfs value %s\n",
211 /* create memory configuration in shared/mmap memory. Take out
212 * a write lock on the memsegs, so we can auto-detect primary/secondary.
213 * This means we never close the file while running (auto-close on exit).
214 * We also don't lock the whole file, so that in future we can use read-locks
215 * on other parts, e.g. memzones, to detect if there are running secondary
218 rte_eal_config_create(void)
220 void *rte_mem_cfg_addr;
223 const char *pathname = eal_runtime_config_path();
225 if (internal_config.no_shconf)
229 mem_cfg_fd = open(pathname, O_RDWR | O_CREAT, 0660);
231 rte_panic("Cannot open '%s' for rte_mem_config\n", pathname);
234 retval = ftruncate(mem_cfg_fd, sizeof(*rte_config.mem_config));
237 rte_panic("Cannot resize '%s' for rte_mem_config\n", pathname);
240 retval = fcntl(mem_cfg_fd, F_SETLK, &wr_lock);
243 rte_exit(EXIT_FAILURE, "Cannot create lock on '%s'. Is another primary "
244 "process running?\n", pathname);
247 rte_mem_cfg_addr = mmap(NULL, sizeof(*rte_config.mem_config),
248 PROT_READ | PROT_WRITE, MAP_SHARED, mem_cfg_fd, 0);
250 if (rte_mem_cfg_addr == MAP_FAILED){
251 rte_panic("Cannot mmap memory for rte_config\n");
253 memcpy(rte_mem_cfg_addr, &early_mem_config, sizeof(early_mem_config));
254 rte_config.mem_config = rte_mem_cfg_addr;
257 /* attach to an existing shared memory config */
259 rte_eal_config_attach(void)
261 void *rte_mem_cfg_addr;
262 const char *pathname = eal_runtime_config_path();
264 if (internal_config.no_shconf)
268 mem_cfg_fd = open(pathname, O_RDWR);
270 rte_panic("Cannot open '%s' for rte_mem_config\n", pathname);
273 rte_mem_cfg_addr = mmap(NULL, sizeof(*rte_config.mem_config),
274 PROT_READ | PROT_WRITE, MAP_SHARED, mem_cfg_fd, 0);
276 if (rte_mem_cfg_addr == MAP_FAILED)
277 rte_panic("Cannot mmap memory for rte_config\n");
279 rte_config.mem_config = rte_mem_cfg_addr;
282 /* Detect if we are a primary or a secondary process */
284 eal_proc_type_detect(void)
286 enum rte_proc_type_t ptype = RTE_PROC_PRIMARY;
287 const char *pathname = eal_runtime_config_path();
289 /* if we can open the file but not get a write-lock we are a secondary
290 * process. NOTE: if we get a file handle back, we keep that open
291 * and don't close it to prevent a race condition between multiple opens */
292 if (((mem_cfg_fd = open(pathname, O_RDWR)) >= 0) &&
293 (fcntl(mem_cfg_fd, F_SETLK, &wr_lock) < 0))
294 ptype = RTE_PROC_SECONDARY;
296 RTE_LOG(INFO, EAL, "Auto-detected process type: %s\n",
297 ptype == RTE_PROC_PRIMARY ? "PRIMARY" : "SECONDARY");
302 /* Sets up rte_config structure with the pointer to shared memory config.*/
304 rte_config_init(void)
306 rte_config.process_type = internal_config.process_type;
308 switch (rte_config.process_type){
309 case RTE_PROC_PRIMARY:
310 rte_eal_config_create();
312 case RTE_PROC_SECONDARY:
313 rte_eal_config_attach();
314 rte_eal_mcfg_wait_complete(rte_config.mem_config);
317 case RTE_PROC_INVALID:
318 rte_panic("Invalid process type\n");
324 eal_usage(const char *prgname)
326 printf("\nUsage: %s ", prgname);
328 /* Allow the application to print its usage message too if hook is set */
329 if ( rte_application_usage_hook ) {
330 printf("===== Application Usage =====\n\n");
331 rte_application_usage_hook(prgname);
335 /* Set a per-application usage message */
337 rte_set_application_usage_hook( rte_usage_hook_t usage_func )
339 rte_usage_hook_t old_func;
341 /* Will be NULL on the first call to denote the last usage routine. */
342 old_func = rte_application_usage_hook;
343 rte_application_usage_hook = usage_func;
349 eal_get_hugepage_mem_size(void)
354 for (i = 0; i < internal_config.num_hugepage_sizes; i++) {
355 struct hugepage_info *hpi = &internal_config.hugepage_info[i];
356 if (strnlen(hpi->hugedir, sizeof(hpi->hugedir)) != 0) {
357 for (j = 0; j < RTE_MAX_NUMA_NODES; j++) {
358 size += hpi->hugepage_sz * hpi->num_pages[j];
363 return (size < SIZE_MAX) ? (size_t)(size) : SIZE_MAX;
366 /* Parse the arguments for --log-level only */
368 eal_log_level_parse(int argc, char **argv)
373 const int old_optind = optind;
374 const int old_optopt = optopt;
375 const int old_optreset = optreset;
376 char * const old_optarg = optarg;
382 while ((opt = getopt_long(argc, argvopt, eal_short_options,
383 eal_long_options, &option_index)) != EOF) {
387 /* getopt is not happy, stop right now */
391 ret = (opt == OPT_LOG_LEVEL_NUM) ?
392 eal_parse_common_option(opt, optarg, &internal_config) : 0;
394 /* common parser is not happy */
399 /* restore getopt lib */
402 optreset = old_optreset;
406 /* Parse the argument given in the command line of the application */
408 eal_parse_args(int argc, char **argv)
413 char *prgname = argv[0];
414 const int old_optind = optind;
415 const int old_optopt = optopt;
416 const int old_optreset = optreset;
417 char * const old_optarg = optarg;
423 while ((opt = getopt_long(argc, argvopt, eal_short_options,
424 eal_long_options, &option_index)) != EOF) {
426 /* getopt is not happy, stop right now */
433 ret = eal_parse_common_option(opt, optarg, &internal_config);
434 /* common parser is not happy */
440 /* common parser handled this option */
445 case OPT_MBUF_POOL_OPS_NAME_NUM:
446 internal_config.user_mbuf_pool_ops_name =
453 if (opt < OPT_LONG_MIN_NUM && isprint(opt)) {
454 RTE_LOG(ERR, EAL, "Option %c is not supported "
455 "on FreeBSD\n", opt);
456 } else if (opt >= OPT_LONG_MIN_NUM &&
457 opt < OPT_LONG_MAX_NUM) {
458 RTE_LOG(ERR, EAL, "Option %s is not supported "
460 eal_long_options[option_index].name);
462 RTE_LOG(ERR, EAL, "Option %d is not supported "
463 "on FreeBSD\n", opt);
471 if (eal_adjust_config(&internal_config) != 0) {
477 if (eal_check_common_options(&internal_config) != 0) {
484 argv[optind-1] = prgname;
488 /* restore getopt lib */
491 optreset = old_optreset;
498 check_socket(const struct rte_memseg_list *msl, void *arg)
500 int *socket_id = arg;
502 if (msl->socket_id == *socket_id && msl->memseg_arr.count != 0)
509 eal_check_mem_on_local_socket(void)
513 socket_id = rte_lcore_to_socket_id(rte_config.master_lcore);
515 if (rte_memseg_list_walk(check_socket, &socket_id) == 0)
516 RTE_LOG(WARNING, EAL, "WARNING: Master core has no memory on local socket!\n");
521 sync_func(__attribute__((unused)) void *arg)
527 rte_eal_mcfg_complete(void)
529 /* ALL shared mem_config related INIT DONE */
530 if (rte_config.process_type == RTE_PROC_PRIMARY)
531 rte_config.mem_config->magic = RTE_MAGIC;
534 /* return non-zero if hugepages are enabled. */
535 int rte_eal_has_hugepages(void)
537 return !internal_config.no_hugetlbfs;
540 /* Abstraction for port I/0 privilege */
542 rte_eal_iopl_init(void)
546 fd = open("/dev/io", O_RDWR);
549 /* keep fd open for iopl */
553 static void rte_eal_init_alert(const char *msg)
555 fprintf(stderr, "EAL: FATAL: %s\n", msg);
556 RTE_LOG(ERR, EAL, "%s\n", msg);
559 /* Launch threads, called at application init(). */
561 rte_eal_init(int argc, char **argv)
565 static rte_atomic32_t run_once = RTE_ATOMIC32_INIT(0);
566 char cpuset[RTE_CPU_AFFINITY_STR_LEN];
567 char thread_name[RTE_MAX_THREAD_NAME_LEN];
569 /* checks if the machine is adequate */
570 if (!rte_cpu_is_supported()) {
571 rte_eal_init_alert("unsupported cpu type.");
576 if (!rte_atomic32_test_and_set(&run_once)) {
577 rte_eal_init_alert("already called initialization.");
578 rte_errno = EALREADY;
582 thread_id = pthread_self();
584 eal_reset_internal_config(&internal_config);
586 /* set log level as early as possible */
587 eal_log_level_parse(argc, argv);
589 if (rte_eal_cpu_init() < 0) {
590 rte_eal_init_alert("Cannot detect lcores.");
595 fctret = eal_parse_args(argc, argv);
597 rte_eal_init_alert("Invalid 'command line' arguments.");
599 rte_atomic32_clear(&run_once);
603 /* create runtime data directory */
604 if (eal_create_runtime_dir() < 0) {
605 rte_eal_init_alert("Cannot create runtime directory\n");
610 /* FreeBSD always uses legacy memory model */
611 internal_config.legacy_mem = true;
613 if (eal_plugins_init() < 0) {
614 rte_eal_init_alert("Cannot init plugins\n");
616 rte_atomic32_clear(&run_once);
620 if (eal_option_device_parse()) {
622 rte_atomic32_clear(&run_once);
628 /* Put mp channel init before bus scan so that we can init the vdev
629 * bus through mp channel in the secondary process before the bus scan.
631 if (rte_mp_channel_init() < 0) {
632 rte_eal_init_alert("failed to init mp channel\n");
633 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
639 if (rte_bus_scan()) {
640 rte_eal_init_alert("Cannot scan the buses for devices\n");
642 rte_atomic32_clear(&run_once);
646 /* autodetect the iova mapping mode (default is iova_pa) */
647 rte_eal_get_configuration()->iova_mode = rte_bus_get_iommu_class();
649 if (internal_config.no_hugetlbfs == 0) {
650 /* rte_config isn't initialized yet */
651 ret = internal_config.process_type == RTE_PROC_PRIMARY ?
652 eal_hugepage_info_init() :
653 eal_hugepage_info_read();
655 rte_eal_init_alert("Cannot get hugepage information.");
657 rte_atomic32_clear(&run_once);
662 if (internal_config.memory == 0 && internal_config.force_sockets == 0) {
663 if (internal_config.no_hugetlbfs)
664 internal_config.memory = MEMSIZE_IF_NO_HUGE_PAGE;
666 internal_config.memory = eal_get_hugepage_mem_size();
669 if (internal_config.vmware_tsc_map == 1) {
670 #ifdef RTE_LIBRTE_EAL_VMWARE_TSC_MAP_SUPPORT
671 rte_cycles_vmware_tsc_map = 1;
672 RTE_LOG (DEBUG, EAL, "Using VMWARE TSC MAP, "
673 "you must have monitor_control.pseudo_perfctr = TRUE\n");
675 RTE_LOG (WARNING, EAL, "Ignoring --vmware-tsc-map because "
676 "RTE_LIBRTE_EAL_VMWARE_TSC_MAP_SUPPORT is not set\n");
680 rte_srand(rte_rdtsc());
682 /* in secondary processes, memory init may allocate additional fbarrays
683 * not present in primary processes, so to avoid any potential issues,
684 * initialize memzones first.
686 if (rte_eal_memzone_init() < 0) {
687 rte_eal_init_alert("Cannot init memzone\n");
692 if (rte_eal_memory_init() < 0) {
693 rte_eal_init_alert("Cannot init memory\n");
698 if (rte_eal_malloc_heap_init() < 0) {
699 rte_eal_init_alert("Cannot init malloc heap\n");
704 if (rte_eal_tailqs_init() < 0) {
705 rte_eal_init_alert("Cannot init tail queues for objects\n");
710 if (rte_eal_alarm_init() < 0) {
711 rte_eal_init_alert("Cannot init interrupt-handling thread\n");
712 /* rte_eal_alarm_init sets rte_errno on failure. */
716 if (rte_eal_intr_init() < 0) {
717 rte_eal_init_alert("Cannot init interrupt-handling thread\n");
721 if (rte_eal_timer_init() < 0) {
722 rte_eal_init_alert("Cannot init HPET or TSC timers\n");
727 eal_check_mem_on_local_socket();
729 eal_thread_init_master(rte_config.master_lcore);
731 ret = eal_thread_dump_affinity(cpuset, sizeof(cpuset));
733 RTE_LOG(DEBUG, EAL, "Master lcore %u is ready (tid=%p;cpuset=[%s%s])\n",
734 rte_config.master_lcore, thread_id, cpuset,
735 ret == 0 ? "" : "...");
737 RTE_LCORE_FOREACH_SLAVE(i) {
740 * create communication pipes between master thread
743 if (pipe(lcore_config[i].pipe_master2slave) < 0)
744 rte_panic("Cannot create pipe\n");
745 if (pipe(lcore_config[i].pipe_slave2master) < 0)
746 rte_panic("Cannot create pipe\n");
748 lcore_config[i].state = WAIT;
750 /* create a thread for each lcore */
751 ret = pthread_create(&lcore_config[i].thread_id, NULL,
752 eal_thread_loop, NULL);
754 rte_panic("Cannot create thread\n");
756 /* Set thread_name for aid in debugging. */
757 snprintf(thread_name, sizeof(thread_name),
758 "lcore-slave-%d", i);
759 rte_thread_setname(lcore_config[i].thread_id, thread_name);
763 * Launch a dummy function on all slave lcores, so that master lcore
764 * knows they are all ready when this function returns.
766 rte_eal_mp_remote_launch(sync_func, NULL, SKIP_MASTER);
767 rte_eal_mp_wait_lcore();
769 /* initialize services so vdevs register service during bus_probe. */
770 ret = rte_service_init();
772 rte_eal_init_alert("rte_service_init() failed\n");
777 /* Probe all the buses and devices/drivers on them */
778 if (rte_bus_probe()) {
779 rte_eal_init_alert("Cannot probe devices\n");
784 /* initialize default service/lcore mappings and start running. Ignore
785 * -ENOTSUP, as it indicates no service coremask passed to EAL.
787 ret = rte_service_start_with_defaults();
788 if (ret < 0 && ret != -ENOTSUP) {
793 rte_eal_mcfg_complete();
798 int __rte_experimental
799 rte_eal_cleanup(void)
801 rte_service_finalize();
806 enum rte_lcore_role_t
807 rte_eal_lcore_role(unsigned lcore_id)
809 return rte_config.lcore_role[lcore_id];
813 rte_eal_process_type(void)
815 return rte_config.process_type;
818 int rte_eal_has_pci(void)
820 return !internal_config.no_pci;
823 int rte_eal_create_uio_dev(void)
825 return internal_config.create_uio_dev;
829 rte_eal_vfio_intr_mode(void)
831 return RTE_INTR_MODE_NONE;
834 int rte_vfio_setup_device(__rte_unused const char *sysfs_base,
835 __rte_unused const char *dev_addr,
836 __rte_unused int *vfio_dev_fd,
837 __rte_unused struct vfio_device_info *device_info)
842 int rte_vfio_release_device(__rte_unused const char *sysfs_base,
843 __rte_unused const char *dev_addr,
849 int rte_vfio_enable(__rte_unused const char *modname)
854 int rte_vfio_is_enabled(__rte_unused const char *modname)
859 int rte_vfio_noiommu_is_enabled(void)
864 int rte_vfio_clear_group(__rte_unused int vfio_group_fd)
869 int __rte_experimental
870 rte_vfio_dma_map(uint64_t __rte_unused vaddr, __rte_unused uint64_t iova,
871 __rte_unused uint64_t len)
876 int __rte_experimental
877 rte_vfio_dma_unmap(uint64_t __rte_unused vaddr, uint64_t __rte_unused iova,
878 __rte_unused uint64_t len)
883 int __rte_experimental
884 rte_vfio_get_group_num(__rte_unused const char *sysfs_base,
885 __rte_unused const char *dev_addr,
886 __rte_unused int *iommu_group_num)
891 int __rte_experimental
892 rte_vfio_get_container_fd(void)
897 int __rte_experimental
898 rte_vfio_get_group_fd(__rte_unused int iommu_group_num)
903 int __rte_experimental
904 rte_vfio_container_create(void)
909 int __rte_experimental
910 rte_vfio_container_destroy(__rte_unused int container_fd)
915 int __rte_experimental
916 rte_vfio_container_group_bind(__rte_unused int container_fd,
917 __rte_unused int iommu_group_num)
922 int __rte_experimental
923 rte_vfio_container_group_unbind(__rte_unused int container_fd,
924 __rte_unused int iommu_group_num)
929 int __rte_experimental
930 rte_vfio_container_dma_map(__rte_unused int container_fd,
931 __rte_unused uint64_t vaddr,
932 __rte_unused uint64_t iova,
933 __rte_unused uint64_t len)
938 int __rte_experimental
939 rte_vfio_container_dma_unmap(__rte_unused int container_fd,
940 __rte_unused uint64_t vaddr,
941 __rte_unused uint64_t iova,
942 __rte_unused uint64_t len)