vdpa/mlx5: fix minsize build
[dpdk.git] / lib / eal / freebsd / eal_hugepage_info.c
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 *
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;
37 }
38
39 static void *
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 *
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
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                 RTE_LOG(ERR, EAL, "could not read sysctl hw.contigmem.num_buffers\n");
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                 RTE_LOG(ERR, EAL, "could not read sysctl hw.contigmem.buffer_size\n");
85                 return -1;
86         }
87
88         fd = open(CONTIGMEM_DEV, O_RDWR);
89         if (fd < 0) {
90                 RTE_LOG(ERR, EAL, "could not open "CONTIGMEM_DEV"\n");
91                 return -1;
92         }
93
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));
100         else
101                 RTE_LOG(INFO, EAL, "Contigmem driver has %d buffers, each of size %dKB\n",
102                                 num_buffers, (int)(buffer_size>>10));
103
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;
108
109         /* for no shared files mode, do not create shared memory config */
110         if (internal_conf->no_shconf)
111                 return 0;
112
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");
117                 return -1;
118         }
119
120         memcpy(tmp_hpi, hpi, sizeof(internal_conf->hugepage_info));
121
122         /* we've copied file descriptors along with everything else, but they
123          * will be invalid in secondary process, so overwrite them
124          */
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;
128         }
129
130         if (munmap(tmp_hpi, sizeof(internal_conf->hugepage_info)) < 0) {
131                 RTE_LOG(ERR, EAL, "Failed to unmap shared memory!\n");
132                 return -1;
133         }
134
135         return 0;
136 }
137
138 /* copy stuff from shared info into internal config */
139 int
140 eal_hugepage_info_read(void)
141 {
142         struct internal_config *internal_conf =
143                 eal_get_internal_configuration();
144
145         struct hugepage_info *hpi = &internal_conf->hugepage_info[0];
146         struct hugepage_info *tmp_hpi;
147
148         internal_conf->num_hugepage_sizes = 1;
149
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");
154                 return -1;
155         }
156
157         memcpy(hpi, tmp_hpi, sizeof(internal_conf->hugepage_info));
158
159         if (munmap(tmp_hpi, sizeof(internal_conf->hugepage_info)) < 0) {
160                 RTE_LOG(ERR, EAL, "Failed to unmap shared memory!\n");
161                 return -1;
162         }
163         return 0;
164 }