New upstream version 18.08
[deb_dpdk.git] / lib / librte_eal / bsdapp / eal / 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 #include "eal_hugepages.h"
12 #include "eal_internal_cfg.h"
13 #include "eal_filesystem.h"
14
15 #define CONTIGMEM_DEV "/dev/contigmem"
16
17 /*
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
20  */
21 static void *
22 map_shared_memory(const char *filename, const size_t mem_size, int flags)
23 {
24         void *retval;
25         int fd = open(filename, flags, 0666);
26         if (fd < 0)
27                 return NULL;
28         if (ftruncate(fd, mem_size) < 0) {
29                 close(fd);
30                 return NULL;
31         }
32         retval = mmap(NULL, mem_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
33         close(fd);
34         return retval;
35 }
36
37 static void *
38 open_shared_memory(const char *filename, const size_t mem_size)
39 {
40         return map_shared_memory(filename, mem_size, O_RDWR);
41 }
42
43 static void *
44 create_shared_memory(const char *filename, const size_t mem_size)
45 {
46         return map_shared_memory(filename, mem_size, O_RDWR | O_CREAT);
47 }
48
49 /*
50  * No hugepage support on freebsd, but we dummy it, using contigmem driver
51  */
52 int
53 eal_hugepage_info_init(void)
54 {
55         size_t sysctl_size;
56         int num_buffers, fd, error;
57         int64_t buffer_size;
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;
61         unsigned int i;
62
63         internal_config.num_hugepage_sizes = 1;
64
65         sysctl_size = sizeof(num_buffers);
66         error = sysctlbyname("hw.contigmem.num_buffers", &num_buffers,
67                         &sysctl_size, NULL, 0);
68
69         if (error != 0) {
70                 RTE_LOG(ERR, EAL, "could not read sysctl hw.contigmem.num_buffers\n");
71                 return -1;
72         }
73
74         sysctl_size = sizeof(buffer_size);
75         error = sysctlbyname("hw.contigmem.buffer_size", &buffer_size,
76                         &sysctl_size, NULL, 0);
77
78         if (error != 0) {
79                 RTE_LOG(ERR, EAL, "could not read sysctl hw.contigmem.buffer_size\n");
80                 return -1;
81         }
82
83         fd = open(CONTIGMEM_DEV, O_RDWR);
84         if (fd < 0) {
85                 RTE_LOG(ERR, EAL, "could not open "CONTIGMEM_DEV"\n");
86                 return -1;
87         }
88
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));
95         else
96                 RTE_LOG(INFO, EAL, "Contigmem driver has %d buffers, each of size %dKB\n",
97                                 num_buffers, (int)(buffer_size>>10));
98
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;
103
104         /* for no shared files mode, do not create shared memory config */
105         if (internal_config.no_shconf)
106                 return 0;
107
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");
112                 return -1;
113         }
114
115         memcpy(tmp_hpi, hpi, sizeof(internal_config.hugepage_info));
116
117         /* we've copied file descriptors along with everything else, but they
118          * will be invalid in secondary process, so overwrite them
119          */
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;
123         }
124
125         if (munmap(tmp_hpi, sizeof(internal_config.hugepage_info)) < 0) {
126                 RTE_LOG(ERR, EAL, "Failed to unmap shared memory!\n");
127                 return -1;
128         }
129
130         return 0;
131 }
132
133 /* copy stuff from shared info into internal config */
134 int
135 eal_hugepage_info_read(void)
136 {
137         struct hugepage_info *hpi = &internal_config.hugepage_info[0];
138         struct hugepage_info *tmp_hpi;
139
140         internal_config.num_hugepage_sizes = 1;
141
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");
146                 return -1;
147         }
148
149         memcpy(hpi, tmp_hpi, sizeof(internal_config.hugepage_info));
150
151         if (munmap(tmp_hpi, sizeof(internal_config.hugepage_info)) < 0) {
152                 RTE_LOG(ERR, EAL, "Failed to unmap shared memory!\n");
153                 return -1;
154         }
155         return 0;
156 }