mem: add function to check if memory is contiguous
[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 create_shared_memory(const char *filename, const size_t mem_size)
23 {
24         void *retval;
25         int fd = open(filename, O_CREAT | O_RDWR, 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 /*
38  * No hugepage support on freebsd, but we dummy it, using contigmem driver
39  */
40 int
41 eal_hugepage_info_init(void)
42 {
43         size_t sysctl_size;
44         int num_buffers, fd, error;
45         int64_t buffer_size;
46         /* re-use the linux "internal config" structure for our memory data */
47         struct hugepage_info *hpi = &internal_config.hugepage_info[0];
48         struct hugepage_info *tmp_hpi;
49
50         internal_config.num_hugepage_sizes = 1;
51
52         /* nothing more to be done for secondary */
53         if (rte_eal_process_type() == RTE_PROC_SECONDARY)
54                 return 0;
55
56         sysctl_size = sizeof(num_buffers);
57         error = sysctlbyname("hw.contigmem.num_buffers", &num_buffers,
58                         &sysctl_size, NULL, 0);
59
60         if (error != 0) {
61                 RTE_LOG(ERR, EAL, "could not read sysctl hw.contigmem.num_buffers\n");
62                 return -1;
63         }
64
65         sysctl_size = sizeof(buffer_size);
66         error = sysctlbyname("hw.contigmem.buffer_size", &buffer_size,
67                         &sysctl_size, NULL, 0);
68
69         if (error != 0) {
70                 RTE_LOG(ERR, EAL, "could not read sysctl hw.contigmem.buffer_size\n");
71                 return -1;
72         }
73
74         fd = open(CONTIGMEM_DEV, O_RDWR);
75         if (fd < 0) {
76                 RTE_LOG(ERR, EAL, "could not open "CONTIGMEM_DEV"\n");
77                 return -1;
78         }
79
80         if (buffer_size >= 1<<30)
81                 RTE_LOG(INFO, EAL, "Contigmem driver has %d buffers, each of size %dGB\n",
82                                 num_buffers, (int)(buffer_size>>30));
83         else if (buffer_size >= 1<<20)
84                 RTE_LOG(INFO, EAL, "Contigmem driver has %d buffers, each of size %dMB\n",
85                                 num_buffers, (int)(buffer_size>>20));
86         else
87                 RTE_LOG(INFO, EAL, "Contigmem driver has %d buffers, each of size %dKB\n",
88                                 num_buffers, (int)(buffer_size>>10));
89
90         hpi->hugedir = CONTIGMEM_DEV;
91         hpi->hugepage_sz = buffer_size;
92         hpi->num_pages[0] = num_buffers;
93         hpi->lock_descriptor = fd;
94
95         tmp_hpi = create_shared_memory(eal_hugepage_info_path(),
96                         sizeof(internal_config.hugepage_info));
97         if (tmp_hpi == NULL ) {
98                 RTE_LOG(ERR, EAL, "Failed to create shared memory!\n");
99                 return -1;
100         }
101
102         memcpy(tmp_hpi, hpi, sizeof(internal_config.hugepage_info));
103
104         if (munmap(tmp_hpi, sizeof(internal_config.hugepage_info)) < 0) {
105                 RTE_LOG(ERR, EAL, "Failed to unmap shared memory!\n");
106                 return -1;
107         }
108
109         return 0;
110 }