1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
5 #include <sys/sysctl.h>
12 #include "eal_private.h"
13 #include "eal_hugepages.h"
14 #include "eal_internal_cfg.h"
15 #include "eal_filesystem.h"
17 #define CONTIGMEM_DEV "/dev/contigmem"
20 * Uses mmap to create a shared memory area for storage of data
21 * Used in this file to store the hugepage file map on disk
24 map_shared_memory(const char *filename, const size_t mem_size, int flags)
27 int fd = open(filename, flags, 0600);
30 if (ftruncate(fd, mem_size) < 0) {
34 retval = mmap(NULL, mem_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
40 open_shared_memory(const char *filename, const size_t mem_size)
42 return map_shared_memory(filename, mem_size, O_RDWR);
46 create_shared_memory(const char *filename, const size_t mem_size)
48 return map_shared_memory(filename, mem_size, O_RDWR | O_CREAT);
52 * No hugepage support on freebsd, but we dummy it, using contigmem driver
55 eal_hugepage_info_init(void)
58 int num_buffers, fd, error;
60 struct internal_config *internal_conf =
61 eal_get_internal_configuration();
63 /* re-use the linux "internal config" structure for our memory data */
64 struct hugepage_info *hpi = &internal_conf->hugepage_info[0];
65 struct hugepage_info *tmp_hpi;
68 internal_conf->num_hugepage_sizes = 1;
70 sysctl_size = sizeof(num_buffers);
71 error = sysctlbyname("hw.contigmem.num_buffers", &num_buffers,
72 &sysctl_size, NULL, 0);
75 RTE_LOG(ERR, EAL, "could not read sysctl hw.contigmem.num_buffers\n");
79 sysctl_size = sizeof(buffer_size);
80 error = sysctlbyname("hw.contigmem.buffer_size", &buffer_size,
81 &sysctl_size, NULL, 0);
84 RTE_LOG(ERR, EAL, "could not read sysctl hw.contigmem.buffer_size\n");
88 fd = open(CONTIGMEM_DEV, O_RDWR);
90 RTE_LOG(ERR, EAL, "could not open "CONTIGMEM_DEV"\n");
94 if (buffer_size >= 1<<30)
95 RTE_LOG(INFO, EAL, "Contigmem driver has %d buffers, each of size %dGB\n",
96 num_buffers, (int)(buffer_size>>30));
97 else if (buffer_size >= 1<<20)
98 RTE_LOG(INFO, EAL, "Contigmem driver has %d buffers, each of size %dMB\n",
99 num_buffers, (int)(buffer_size>>20));
101 RTE_LOG(INFO, EAL, "Contigmem driver has %d buffers, each of size %dKB\n",
102 num_buffers, (int)(buffer_size>>10));
104 strlcpy(hpi->hugedir, CONTIGMEM_DEV, sizeof(hpi->hugedir));
105 hpi->hugepage_sz = buffer_size;
106 hpi->num_pages[0] = num_buffers;
107 hpi->lock_descriptor = fd;
109 /* for no shared files mode, do not create shared memory config */
110 if (internal_conf->no_shconf)
113 tmp_hpi = create_shared_memory(eal_hugepage_info_path(),
114 sizeof(internal_conf->hugepage_info));
115 if (tmp_hpi == NULL ) {
116 RTE_LOG(ERR, EAL, "Failed to create shared memory!\n");
120 memcpy(tmp_hpi, hpi, sizeof(internal_conf->hugepage_info));
122 /* we've copied file descriptors along with everything else, but they
123 * will be invalid in secondary process, so overwrite them
125 for (i = 0; i < RTE_DIM(internal_conf->hugepage_info); i++) {
126 struct hugepage_info *tmp = &tmp_hpi[i];
127 tmp->lock_descriptor = -1;
130 if (munmap(tmp_hpi, sizeof(internal_conf->hugepage_info)) < 0) {
131 RTE_LOG(ERR, EAL, "Failed to unmap shared memory!\n");
138 /* copy stuff from shared info into internal config */
140 eal_hugepage_info_read(void)
142 struct internal_config *internal_conf =
143 eal_get_internal_configuration();
145 struct hugepage_info *hpi = &internal_conf->hugepage_info[0];
146 struct hugepage_info *tmp_hpi;
148 internal_conf->num_hugepage_sizes = 1;
150 tmp_hpi = open_shared_memory(eal_hugepage_info_path(),
151 sizeof(internal_conf->hugepage_info));
152 if (tmp_hpi == NULL) {
153 RTE_LOG(ERR, EAL, "Failed to open shared memory!\n");
157 memcpy(hpi, tmp_hpi, sizeof(internal_conf->hugepage_info));
159 if (munmap(tmp_hpi, sizeof(internal_conf->hugepage_info)) < 0) {
160 RTE_LOG(ERR, EAL, "Failed to unmap shared memory!\n");