add FreeBSD support
[dpdk.git] / lib / librte_eal / bsdapp / eal / eal_hugepage_info.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  * 
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  * 
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  * 
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 #include <sys/types.h>
34 #include <sys/sysctl.h>
35 #include <sys/mman.h>
36 #include <string.h>
37
38 #include <rte_log.h>
39 #include <fcntl.h>
40 #include "eal_hugepages.h"
41 #include "eal_internal_cfg.h"
42 #include "eal_filesystem.h"
43
44 #define CONTIGMEM_DEV "/dev/contigmem"
45
46 /*
47  * Uses mmap to create a shared memory area for storage of data
48  * Used in this file to store the hugepage file map on disk
49  */
50 static void *
51 create_shared_memory(const char *filename, const size_t mem_size)
52 {
53         void *retval;
54         int fd = open(filename, O_CREAT | O_RDWR, 0666);
55         if (fd < 0)
56                 return NULL;
57         if (ftruncate(fd, mem_size) < 0) {
58                 close(fd);
59                 return NULL;
60         }
61         retval = mmap(NULL, mem_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
62         close(fd);
63         return retval;
64 }
65
66 /*
67  * No hugepage support on freebsd, but we dummy it, using contigmem driver
68  */
69 int
70 eal_hugepage_info_init(void)
71 {
72         size_t sysctl_size;
73         int buffer_size, num_buffers, fd, error;
74         /* re-use the linux "internal config" structure for our memory data */
75         struct hugepage_info *hpi = &internal_config.hugepage_info[0];
76         struct hugepage_info *tmp_hpi;
77
78         sysctl_size = sizeof(num_buffers);
79         error = sysctlbyname("hw.contigmem.num_buffers", &num_buffers,
80                         &sysctl_size, NULL, 0);
81
82         if (error != 0) {
83                 RTE_LOG(ERR, EAL, "could not read sysctl hw.contigmem.num_buffers");
84                 return -1;
85         }
86
87         sysctl_size = sizeof(buffer_size);
88         error = sysctlbyname("hw.contigmem.buffer_size", &buffer_size,
89                         &sysctl_size, NULL, 0);
90
91         if (error != 0) {
92                 RTE_LOG(ERR, EAL, "could not read sysctl hw.contigmem.buffer_size");
93                 return -1;
94         }
95
96         fd = open(CONTIGMEM_DEV, O_RDWR);
97         if (fd < 0) {
98                 RTE_LOG(ERR, EAL, "could not open "CONTIGMEM_DEV"\n");
99                 return -1;
100         }
101
102         if (buffer_size >= 1<<30)
103                 RTE_LOG(INFO, EAL, "Contigmem driver has %d buffers, each of size %dGB\n",
104                                 num_buffers, buffer_size>>30);
105         else if (buffer_size >= 1<<20)
106                 RTE_LOG(INFO, EAL, "Contigmem driver has %d buffers, each of size %dMB\n",
107                                 num_buffers, buffer_size>>20);
108         else
109                 RTE_LOG(INFO, EAL, "Contigmem driver has %d buffers, each of size %dKB\n",
110                                 num_buffers, buffer_size>>10);
111
112         internal_config.num_hugepage_sizes = 1;
113         hpi->hugedir = CONTIGMEM_DEV;
114         hpi->hugepage_sz = buffer_size;
115         hpi->num_pages[0] = num_buffers;
116         hpi->lock_descriptor = fd;
117         
118         tmp_hpi = create_shared_memory(eal_hugepage_info_path(),
119                                         sizeof(struct hugepage_info));
120         if (tmp_hpi == NULL ) {
121                 RTE_LOG(ERR, EAL, "Failed to create shared memory!\n");
122                 return -1;
123         }
124
125         memcpy(tmp_hpi, hpi, sizeof(struct hugepage_info));
126
127         if ( munmap(tmp_hpi, sizeof(struct hugepage_info)) < 0) {
128                 RTE_LOG(ERR, EAL, "Failed to unmap shared memory!\n");
129                 return -1;
130         }
131         
132         return 0;
133 }