]> git.proxmox.com Git - ceph.git/blame - ceph/src/dpdk/lib/librte_eal/bsdapp/eal/eal_hugepage_info.c
bump version to 12.2.12-pve1
[ceph.git] / ceph / src / dpdk / lib / librte_eal / bsdapp / eal / eal_hugepage_info.c
CommitLineData
7c673cae
FG
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 */
50static void *
51create_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 */
69int
70eal_hugepage_info_init(void)
71{
72 size_t sysctl_size;
73 int num_buffers, fd, error;
74 int64_t buffer_size;
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;
78
79 sysctl_size = sizeof(num_buffers);
80 error = sysctlbyname("hw.contigmem.num_buffers", &num_buffers,
81 &sysctl_size, NULL, 0);
82
83 if (error != 0) {
84 RTE_LOG(ERR, EAL, "could not read sysctl hw.contigmem.num_buffers");
85 return -1;
86 }
87
88 sysctl_size = sizeof(buffer_size);
89 error = sysctlbyname("hw.contigmem.buffer_size", &buffer_size,
90 &sysctl_size, NULL, 0);
91
92 if (error != 0) {
93 RTE_LOG(ERR, EAL, "could not read sysctl hw.contigmem.buffer_size");
94 return -1;
95 }
96
97 fd = open(CONTIGMEM_DEV, O_RDWR);
98 if (fd < 0) {
99 RTE_LOG(ERR, EAL, "could not open "CONTIGMEM_DEV"\n");
100 return -1;
101 }
102
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));
109 else
110 RTE_LOG(INFO, EAL, "Contigmem driver has %d buffers, each of size %dKB\n",
111 num_buffers, (int)(buffer_size>>10));
112
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;
118
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");
123 return -1;
124 }
125
126 memcpy(tmp_hpi, hpi, sizeof(struct hugepage_info));
127
128 if ( munmap(tmp_hpi, sizeof(struct hugepage_info)) < 0) {
129 RTE_LOG(ERR, EAL, "Failed to unmap shared memory!\n");
130 return -1;
131 }
132
133 return 0;
134}