1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
5 #include <sys/sysctl.h>
11 #include "eal_hugepages.h"
12 #include "eal_internal_cfg.h"
13 #include "eal_filesystem.h"
15 #define CONTIGMEM_DEV "/dev/contigmem"
18 * Uses mmap to create a shared memory area for storage of data
19 * Used in this file to store the hugepage file map on disk
22 map_shared_memory(const char *filename, const size_t mem_size, int flags)
25 int fd = open(filename, flags, 0666);
28 if (ftruncate(fd, mem_size) < 0) {
32 retval = mmap(NULL, mem_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
38 open_shared_memory(const char *filename, const size_t mem_size)
40 return map_shared_memory(filename, mem_size, O_RDWR);
44 create_shared_memory(const char *filename, const size_t mem_size)
46 return map_shared_memory(filename, mem_size, O_RDWR | O_CREAT);
50 * No hugepage support on freebsd, but we dummy it, using contigmem driver
53 eal_hugepage_info_init(void)
56 int num_buffers, fd, error;
58 /* re-use the linux "internal config" structure for our memory data */
59 struct hugepage_info *hpi = &internal_config.hugepage_info[0];
60 struct hugepage_info *tmp_hpi;
63 internal_config.num_hugepage_sizes = 1;
65 sysctl_size = sizeof(num_buffers);
66 error = sysctlbyname("hw.contigmem.num_buffers", &num_buffers,
67 &sysctl_size, NULL, 0);
70 RTE_LOG(ERR, EAL, "could not read sysctl hw.contigmem.num_buffers\n");
74 sysctl_size = sizeof(buffer_size);
75 error = sysctlbyname("hw.contigmem.buffer_size", &buffer_size,
76 &sysctl_size, NULL, 0);
79 RTE_LOG(ERR, EAL, "could not read sysctl hw.contigmem.buffer_size\n");
83 fd = open(CONTIGMEM_DEV, O_RDWR);
85 RTE_LOG(ERR, EAL, "could not open "CONTIGMEM_DEV"\n");
89 if (buffer_size >= 1<<30)
90 RTE_LOG(INFO, EAL, "Contigmem driver has %d buffers, each of size %dGB\n",
91 num_buffers, (int)(buffer_size>>30));
92 else if (buffer_size >= 1<<20)
93 RTE_LOG(INFO, EAL, "Contigmem driver has %d buffers, each of size %dMB\n",
94 num_buffers, (int)(buffer_size>>20));
96 RTE_LOG(INFO, EAL, "Contigmem driver has %d buffers, each of size %dKB\n",
97 num_buffers, (int)(buffer_size>>10));
99 strlcpy(hpi->hugedir, CONTIGMEM_DEV, sizeof(hpi->hugedir));
100 hpi->hugepage_sz = buffer_size;
101 hpi->num_pages[0] = num_buffers;
102 hpi->lock_descriptor = fd;
104 /* for no shared files mode, do not create shared memory config */
105 if (internal_config.no_shconf)
108 tmp_hpi = create_shared_memory(eal_hugepage_info_path(),
109 sizeof(internal_config.hugepage_info));
110 if (tmp_hpi == NULL ) {
111 RTE_LOG(ERR, EAL, "Failed to create shared memory!\n");
115 memcpy(tmp_hpi, hpi, sizeof(internal_config.hugepage_info));
117 /* we've copied file descriptors along with everything else, but they
118 * will be invalid in secondary process, so overwrite them
120 for (i = 0; i < RTE_DIM(internal_config.hugepage_info); i++) {
121 struct hugepage_info *tmp = &tmp_hpi[i];
122 tmp->lock_descriptor = -1;
125 if (munmap(tmp_hpi, sizeof(internal_config.hugepage_info)) < 0) {
126 RTE_LOG(ERR, EAL, "Failed to unmap shared memory!\n");
133 /* copy stuff from shared info into internal config */
135 eal_hugepage_info_read(void)
137 struct hugepage_info *hpi = &internal_config.hugepage_info[0];
138 struct hugepage_info *tmp_hpi;
140 internal_config.num_hugepage_sizes = 1;
142 tmp_hpi = open_shared_memory(eal_hugepage_info_path(),
143 sizeof(internal_config.hugepage_info));
144 if (tmp_hpi == NULL) {
145 RTE_LOG(ERR, EAL, "Failed to open shared memory!\n");
149 memcpy(hpi, tmp_hpi, sizeof(internal_config.hugepage_info));
151 if (munmap(tmp_hpi, sizeof(internal_config.hugepage_info)) < 0) {
152 RTE_LOG(ERR, EAL, "Failed to unmap shared memory!\n");