xref: /dpdk/lib/eal/freebsd/eal_hugepage_info.c (revision ae67895b507bb6af22263c79ba0d5c374b396485)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4 #include <sys/types.h>
5 #include <sys/sysctl.h>
6 #include <sys/mman.h>
7 #include <string.h>
8 
9 #include <rte_log.h>
10 #include <fcntl.h>
11 
12 #include "eal_private.h"
13 #include "eal_hugepages.h"
14 #include "eal_internal_cfg.h"
15 #include "eal_filesystem.h"
16 
17 #define CONTIGMEM_DEV "/dev/contigmem"
18 
19 /*
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
22  */
23 static void *
map_shared_memory(const char * filename,const size_t mem_size,int flags)24 map_shared_memory(const char *filename, const size_t mem_size, int flags)
25 {
26 	void *retval;
27 	int fd = open(filename, flags, 0600);
28 	if (fd < 0)
29 		return NULL;
30 	if (ftruncate(fd, mem_size) < 0) {
31 		close(fd);
32 		return NULL;
33 	}
34 	retval = mmap(NULL, mem_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
35 	close(fd);
36 	return retval == MAP_FAILED ? NULL : retval;
37 }
38 
39 static void *
open_shared_memory(const char * filename,const size_t mem_size)40 open_shared_memory(const char *filename, const size_t mem_size)
41 {
42 	return map_shared_memory(filename, mem_size, O_RDWR);
43 }
44 
45 static void *
create_shared_memory(const char * filename,const size_t mem_size)46 create_shared_memory(const char *filename, const size_t mem_size)
47 {
48 	return map_shared_memory(filename, mem_size, O_RDWR | O_CREAT);
49 }
50 
51 /*
52  * No hugepage support on freebsd, but we dummy it, using contigmem driver
53  */
54 int
eal_hugepage_info_init(void)55 eal_hugepage_info_init(void)
56 {
57 	size_t sysctl_size;
58 	int num_buffers, fd, error;
59 	int64_t buffer_size;
60 	struct internal_config *internal_conf =
61 		eal_get_internal_configuration();
62 
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;
66 	unsigned int i;
67 
68 	internal_conf->num_hugepage_sizes = 1;
69 
70 	sysctl_size = sizeof(num_buffers);
71 	error = sysctlbyname("hw.contigmem.num_buffers", &num_buffers,
72 			&sysctl_size, NULL, 0);
73 
74 	if (error != 0) {
75 		EAL_LOG(ERR, "could not read sysctl hw.contigmem.num_buffers");
76 		return -1;
77 	}
78 
79 	sysctl_size = sizeof(buffer_size);
80 	error = sysctlbyname("hw.contigmem.buffer_size", &buffer_size,
81 			&sysctl_size, NULL, 0);
82 
83 	if (error != 0) {
84 		EAL_LOG(ERR, "could not read sysctl hw.contigmem.buffer_size");
85 		return -1;
86 	}
87 
88 	fd = open(CONTIGMEM_DEV, O_RDWR);
89 	if (fd < 0) {
90 		EAL_LOG(ERR, "could not open "CONTIGMEM_DEV);
91 		return -1;
92 	}
93 	if (flock(fd, LOCK_EX | LOCK_NB) < 0) {
94 		EAL_LOG(ERR, "could not lock memory. Is another DPDK process running?");
95 		return -1;
96 	}
97 
98 	if (buffer_size >= 1<<30)
99 		EAL_LOG(INFO, "Contigmem driver has %d buffers, each of size %dGB",
100 				num_buffers, (int)(buffer_size>>30));
101 	else if (buffer_size >= 1<<20)
102 		EAL_LOG(INFO, "Contigmem driver has %d buffers, each of size %dMB",
103 				num_buffers, (int)(buffer_size>>20));
104 	else
105 		EAL_LOG(INFO, "Contigmem driver has %d buffers, each of size %dKB",
106 				num_buffers, (int)(buffer_size>>10));
107 
108 	strlcpy(hpi->hugedir, CONTIGMEM_DEV, sizeof(hpi->hugedir));
109 	hpi->hugepage_sz = buffer_size;
110 	hpi->num_pages[0] = num_buffers;
111 	hpi->lock_descriptor = fd;
112 
113 	/* for no shared files mode, do not create shared memory config */
114 	if (internal_conf->no_shconf)
115 		return 0;
116 
117 	tmp_hpi = create_shared_memory(eal_hugepage_info_path(),
118 			sizeof(internal_conf->hugepage_info));
119 	if (tmp_hpi == NULL ) {
120 		EAL_LOG(ERR, "Failed to create shared memory!");
121 		return -1;
122 	}
123 
124 	memcpy(tmp_hpi, hpi, sizeof(internal_conf->hugepage_info));
125 
126 	/* we've copied file descriptors along with everything else, but they
127 	 * will be invalid in secondary process, so overwrite them
128 	 */
129 	for (i = 0; i < RTE_DIM(internal_conf->hugepage_info); i++) {
130 		struct hugepage_info *tmp = &tmp_hpi[i];
131 		tmp->lock_descriptor = -1;
132 	}
133 
134 	if (munmap(tmp_hpi, sizeof(internal_conf->hugepage_info)) < 0) {
135 		EAL_LOG(ERR, "Failed to unmap shared memory!");
136 		return -1;
137 	}
138 
139 	return 0;
140 }
141 
142 /* copy stuff from shared info into internal config */
143 int
eal_hugepage_info_read(void)144 eal_hugepage_info_read(void)
145 {
146 	struct internal_config *internal_conf =
147 		eal_get_internal_configuration();
148 
149 	struct hugepage_info *hpi = &internal_conf->hugepage_info[0];
150 	struct hugepage_info *tmp_hpi;
151 
152 	internal_conf->num_hugepage_sizes = 1;
153 
154 	tmp_hpi = open_shared_memory(eal_hugepage_info_path(),
155 				  sizeof(internal_conf->hugepage_info));
156 	if (tmp_hpi == NULL) {
157 		EAL_LOG(ERR, "Failed to open shared memory!");
158 		return -1;
159 	}
160 
161 	memcpy(hpi, tmp_hpi, sizeof(internal_conf->hugepage_info));
162 
163 	if (munmap(tmp_hpi, sizeof(internal_conf->hugepage_info)) < 0) {
164 		EAL_LOG(ERR, "Failed to unmap shared memory!");
165 		return -1;
166 	}
167 	return 0;
168 }
169