4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
33 #include <sys/types.h>
34 #include <sys/sysctl.h>
40 #include "eal_hugepages.h"
41 #include "eal_internal_cfg.h"
42 #include "eal_filesystem.h"
44 #define CONTIGMEM_DEV "/dev/contigmem"
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
51 create_shared_memory(const char *filename, const size_t mem_size)
54 int fd = open(filename, O_CREAT | O_RDWR, 0666);
57 if (ftruncate(fd, mem_size) < 0) {
61 retval = mmap(NULL, mem_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
67 * No hugepage support on freebsd, but we dummy it, using contigmem driver
70 eal_hugepage_info_init(void)
73 int num_buffers, fd, error;
75 /* re-use the linux "internal config" structure for our memory data */
76 struct hugepage_info *hpi = &internal_config.hugepage_info[0];
77 struct hugepage_info *tmp_hpi;
79 sysctl_size = sizeof(num_buffers);
80 error = sysctlbyname("hw.contigmem.num_buffers", &num_buffers,
81 &sysctl_size, NULL, 0);
84 RTE_LOG(ERR, EAL, "could not read sysctl hw.contigmem.num_buffers");
88 sysctl_size = sizeof(buffer_size);
89 error = sysctlbyname("hw.contigmem.buffer_size", &buffer_size,
90 &sysctl_size, NULL, 0);
93 RTE_LOG(ERR, EAL, "could not read sysctl hw.contigmem.buffer_size");
97 fd = open(CONTIGMEM_DEV, O_RDWR);
99 RTE_LOG(ERR, EAL, "could not open "CONTIGMEM_DEV"\n");
103 if (buffer_size >= 1<<30)
104 RTE_LOG(INFO, EAL, "Contigmem driver has %d buffers, each of size %dGB\n",
105 num_buffers, (int)(buffer_size>>30));
106 else if (buffer_size >= 1<<20)
107 RTE_LOG(INFO, EAL, "Contigmem driver has %d buffers, each of size %dMB\n",
108 num_buffers, (int)(buffer_size>>20));
110 RTE_LOG(INFO, EAL, "Contigmem driver has %d buffers, each of size %dKB\n",
111 num_buffers, (int)(buffer_size>>10));
113 internal_config.num_hugepage_sizes = 1;
114 hpi->hugedir = CONTIGMEM_DEV;
115 hpi->hugepage_sz = buffer_size;
116 hpi->num_pages[0] = num_buffers;
117 hpi->lock_descriptor = fd;
119 tmp_hpi = create_shared_memory(eal_hugepage_info_path(),
120 sizeof(struct hugepage_info));
121 if (tmp_hpi == NULL ) {
122 RTE_LOG(ERR, EAL, "Failed to create shared memory!\n");
126 memcpy(tmp_hpi, hpi, sizeof(struct hugepage_info));
128 if ( munmap(tmp_hpi, sizeof(struct hugepage_info)) < 0) {
129 RTE_LOG(ERR, EAL, "Failed to unmap shared memory!\n");