remove extra parentheses in return statement
[dpdk.git] / app / test-pmd / mempool_anon.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
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include "mempool_osdep.h"
37 #include <rte_errno.h>
38
39 #ifdef RTE_EXEC_ENV_LINUXAPP
40
41 #include <fcntl.h>
42 #include <unistd.h>
43 #include <sys/mman.h>
44
45
46 #define PAGEMAP_FNAME           "/proc/self/pagemap"
47
48 /*
49  * the pfn (page frame number) are bits 0-54 (see pagemap.txt in linux
50  * Documentation).
51  */
52 #define PAGEMAP_PFN_BITS        54
53 #define PAGEMAP_PFN_MASK        RTE_LEN2MASK(PAGEMAP_PFN_BITS, phys_addr_t)
54
55
56 static int
57 get_phys_map(void *va, phys_addr_t pa[], uint32_t pg_num, uint32_t pg_sz)
58 {
59         int32_t fd, rc;
60         uint32_t i, nb;
61         off_t ofs;
62
63         ofs = (uintptr_t)va / pg_sz * sizeof(*pa);
64         nb = pg_num * sizeof(*pa);
65
66         if ((fd = open(PAGEMAP_FNAME, O_RDONLY)) < 0)
67                 return ENOENT;
68
69         if ((rc = pread(fd, pa, nb, ofs)) < 0 || (rc -= nb) != 0) {
70
71                 RTE_LOG(ERR, USER1, "failed read of %u bytes from \'%s\' "
72                         "at offset %zu, error code: %d\n",
73                         nb, PAGEMAP_FNAME, (size_t)ofs, errno);
74                 rc = ENOENT;
75         }
76
77         close(fd);
78
79         for (i = 0; i != pg_num; i++)
80                 pa[i] = (pa[i] & PAGEMAP_PFN_MASK) * pg_sz;
81
82         return rc;
83 }
84
85 struct rte_mempool *
86 mempool_anon_create(const char *name, unsigned elt_num, unsigned elt_size,
87                    unsigned cache_size, unsigned private_data_size,
88                    rte_mempool_ctor_t *mp_init, void *mp_init_arg,
89                    rte_mempool_obj_ctor_t *obj_init, void *obj_init_arg,
90                    int socket_id, unsigned flags)
91 {
92         struct rte_mempool *mp;
93         phys_addr_t *pa;
94         char *va, *uv;
95         uint32_t n, pg_num, pg_shift, pg_sz, total_size;
96         size_t sz;
97         ssize_t usz;
98         int32_t rc;
99
100         rc = ENOMEM;
101         mp = NULL;
102
103         pg_sz = getpagesize();
104         if (rte_is_power_of_2(pg_sz) == 0) {
105                 rte_errno = EINVAL;
106                 return mp;
107         }
108
109         pg_shift = rte_bsf32(pg_sz);
110
111         total_size = rte_mempool_calc_obj_size(elt_size, flags, NULL);
112
113         /* calc max memory size and max number of pages needed. */
114         sz = rte_mempool_xmem_size(elt_num, total_size, pg_shift);
115         pg_num = sz >> pg_shift;
116
117         /* get chunk of virtually continuos memory.*/
118         if ((va = mmap(NULL, sz, PROT_READ | PROT_WRITE,
119                         MAP_SHARED | MAP_ANONYMOUS | MAP_LOCKED,
120                         -1, 0)) == MAP_FAILED) {
121                 RTE_LOG(ERR, USER1, "%s(%s) failed mmap of %zu bytes, "
122                         "error code: %d\n",
123                         __func__, name, sz, errno);
124                 rte_errno = rc;
125                 return mp;
126         }
127
128         /* extract physical mappings of the allocated memory. */
129         if ((pa = calloc(pg_num, sizeof (*pa))) != NULL &&
130                         (rc = get_phys_map(va, pa, pg_num, pg_sz)) == 0) {
131
132                 /*
133                  * Check that allocated size is big enough to hold elt_num
134                  * objects and a calcualte how many bytes are actually required.
135                  */
136
137                 if ((usz = rte_mempool_xmem_usage(va, elt_num, total_size, pa,
138                                 pg_num, pg_shift)) < 0) {
139
140                         n = -usz;
141                         rc = ENOENT;
142                         RTE_LOG(ERR, USER1, "%s(%s) only %u objects from %u "
143                                 "requested can  be created over "
144                                 "mmaped region %p of %zu bytes\n",
145                                 __func__, name, n, elt_num, va, sz);
146                 } else {
147
148                         /* unmap unused pages if any */
149                         if ((size_t)usz < sz) {
150
151                                 uv = va + usz;
152                                 usz = sz - usz;
153
154                                 RTE_LOG(INFO, USER1,
155                                         "%s(%s): unmap unused %zu of %zu "
156                                         "mmaped bytes @%p\n",
157                                         __func__, name, (size_t)usz, sz, uv);
158                                 munmap(uv, usz);
159                                 sz -= usz;
160                                 pg_num = sz >> pg_shift;
161                         }
162
163                         if ((mp = rte_mempool_xmem_create(name, elt_num,
164                                         elt_size, cache_size, private_data_size,
165                                         mp_init, mp_init_arg,
166                                         obj_init, obj_init_arg,
167                                         socket_id, flags, va, pa, pg_num,
168                                         pg_shift)) != NULL)
169
170                                 RTE_VERIFY(elt_num == mp->size);
171                 }
172         }
173
174         if (mp == NULL) {
175                 munmap(va, sz);
176                 rte_errno = rc;
177         }
178
179         free(pa);
180         return mp;
181 }
182
183 #else /* RTE_EXEC_ENV_LINUXAPP */
184
185
186 struct rte_mempool *
187 mempool_anon_create(__rte_unused const char *name,
188         __rte_unused unsigned elt_num, __rte_unused unsigned elt_size,
189         __rte_unused unsigned cache_size,
190         __rte_unused unsigned private_data_size,
191         __rte_unused rte_mempool_ctor_t *mp_init,
192         __rte_unused void *mp_init_arg,
193         __rte_unused rte_mempool_obj_ctor_t *obj_init,
194         __rte_unused void *obj_init_arg,
195         __rte_unused int socket_id, __rte_unused unsigned flags)
196 {
197         rte_errno = ENOTSUP;
198         return NULL;
199 }
200
201 #endif /* RTE_EXEC_ENV_LINUXAPP */