New upstream version 18.02
[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 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         sysctl_size = sizeof(num_buffers);
51         error = sysctlbyname("hw.contigmem.num_buffers", &num_buffers,
52                         &sysctl_size, NULL, 0);
53
54         if (error != 0) {
55                 RTE_LOG(ERR, EAL, "could not read sysctl hw.contigmem.num_buffers");
56                 return -1;
57         }
58
59         sysctl_size = sizeof(buffer_size);
60         error = sysctlbyname("hw.contigmem.buffer_size", &buffer_size,
61                         &sysctl_size, NULL, 0);
62
63         if (error != 0) {
64                 RTE_LOG(ERR, EAL, "could not read sysctl hw.contigmem.buffer_size");
65                 return -1;
66         }
67
68         fd = open(CONTIGMEM_DEV, O_RDWR);
69         if (fd < 0) {
70                 RTE_LOG(ERR, EAL, "could not open "CONTIGMEM_DEV"\n");
71                 return -1;
72         }
73
74         if (buffer_size >= 1<<30)
75                 RTE_LOG(INFO, EAL, "Contigmem driver has %d buffers, each of size %dGB\n",
76                                 num_buffers, (int)(buffer_size>>30));
77         else if (buffer_size >= 1<<20)
78                 RTE_LOG(INFO, EAL, "Contigmem driver has %d buffers, each of size %dMB\n",
79                                 num_buffers, (int)(buffer_size>>20));
80         else
81                 RTE_LOG(INFO, EAL, "Contigmem driver has %d buffers, each of size %dKB\n",
82                                 num_buffers, (int)(buffer_size>>10));
83
84         internal_config.num_hugepage_sizes = 1;
85         hpi->hugedir = CONTIGMEM_DEV;
86         hpi->hugepage_sz = buffer_size;
87         hpi->num_pages[0] = num_buffers;
88         hpi->lock_descriptor = fd;
89
90         tmp_hpi = create_shared_memory(eal_hugepage_info_path(),
91                                         sizeof(struct hugepage_info));
92         if (tmp_hpi == NULL ) {
93                 RTE_LOG(ERR, EAL, "Failed to create shared memory!\n");
94                 return -1;
95         }
96
97         memcpy(tmp_hpi, hpi, sizeof(struct hugepage_info));
98
99         if ( munmap(tmp_hpi, sizeof(struct hugepage_info)) < 0) {
100                 RTE_LOG(ERR, EAL, "Failed to unmap shared memory!\n");
101                 return -1;
102         }
103
104         return 0;
105 }