1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Intel Corporation
13 #include <rte_common.h>
14 #include <rte_debug.h>
16 #include <rte_errno.h>
17 #include <rte_malloc.h>
19 #include <rte_string_fns.h>
23 #define EXTERNAL_MEM_SZ (RTE_PGSIZE_4K << 10) /* 4M of data */
26 test_invalid_param(void *addr, size_t len, size_t pgsz, rte_iova_t *iova,
29 static const char * const names[] = {
32 "this heap name is definitely way too long to be valid"
34 const char *valid_name = "valid heap name";
37 /* check invalid name handling */
38 for (i = 0; i < RTE_DIM(names); i++) {
39 const char *name = names[i];
41 /* these calls may fail for other reasons, so check errno */
42 if (rte_malloc_heap_create(name) >= 0 || rte_errno != EINVAL) {
43 printf("%s():%i: Created heap with invalid name\n",
48 if (rte_malloc_heap_destroy(name) >= 0 || rte_errno != EINVAL) {
49 printf("%s():%i: Destroyed heap with invalid name\n",
54 if (rte_malloc_heap_get_socket(name) >= 0 ||
55 rte_errno != EINVAL) {
56 printf("%s():%i: Found socket for heap with invalid name\n",
61 if (rte_malloc_heap_memory_add(name, addr, len,
62 NULL, 0, pgsz) >= 0 || rte_errno != EINVAL) {
63 printf("%s():%i: Added memory to heap with invalid name\n",
67 if (rte_malloc_heap_memory_remove(name, addr, len) >= 0 ||
68 rte_errno != EINVAL) {
69 printf("%s():%i: Removed memory from heap with invalid name\n",
74 if (rte_malloc_heap_memory_attach(name, addr, len) >= 0 ||
75 rte_errno != EINVAL) {
76 printf("%s():%i: Attached memory to heap with invalid name\n",
80 if (rte_malloc_heap_memory_detach(name, addr, len) >= 0 ||
81 rte_errno != EINVAL) {
82 printf("%s():%i: Detached memory from heap with invalid name\n",
88 /* do same as above, but with a valid heap name */
90 /* skip create call */
91 if (rte_malloc_heap_destroy(valid_name) >= 0 || rte_errno != ENOENT) {
92 printf("%s():%i: Destroyed heap with invalid name\n",
96 if (rte_malloc_heap_get_socket(valid_name) >= 0 ||
97 rte_errno != ENOENT) {
98 printf("%s():%i: Found socket for heap with invalid name\n",
103 /* these calls may fail for other reasons, so check errno */
104 if (rte_malloc_heap_memory_add(valid_name, addr, len,
105 NULL, 0, pgsz) >= 0 || rte_errno != ENOENT) {
106 printf("%s():%i: Added memory to non-existent heap\n",
110 if (rte_malloc_heap_memory_remove(valid_name, addr, len) >= 0 ||
111 rte_errno != ENOENT) {
112 printf("%s():%i: Removed memory from non-existent heap\n",
117 if (rte_malloc_heap_memory_attach(valid_name, addr, len) >= 0 ||
118 rte_errno != ENOENT) {
119 printf("%s():%i: Attached memory to non-existent heap\n",
123 if (rte_malloc_heap_memory_detach(valid_name, addr, len) >= 0 ||
124 rte_errno != ENOENT) {
125 printf("%s():%i: Detached memory from non-existent heap\n",
130 /* create a valid heap but test other invalid parameters */
131 if (rte_malloc_heap_create(valid_name) != 0) {
132 printf("%s():%i: Failed to create valid heap\n",
138 if (rte_malloc_heap_memory_add(valid_name, addr, 0,
139 NULL, 0, pgsz) >= 0 || rte_errno != EINVAL) {
140 printf("%s():%i: Added memory with invalid parameters\n",
145 if (rte_malloc_heap_memory_remove(valid_name, addr, 0) >= 0 ||
146 rte_errno != EINVAL) {
147 printf("%s():%i: Removed memory with invalid parameters\n",
152 if (rte_malloc_heap_memory_attach(valid_name, addr, 0) >= 0 ||
153 rte_errno != EINVAL) {
154 printf("%s():%i: Attached memory with invalid parameters\n",
158 if (rte_malloc_heap_memory_detach(valid_name, addr, 0) >= 0 ||
159 rte_errno != EINVAL) {
160 printf("%s():%i: Detached memory with invalid parameters\n",
166 if (rte_malloc_heap_memory_add(valid_name, NULL, len,
167 NULL, 0, pgsz) >= 0 || rte_errno != EINVAL) {
168 printf("%s():%i: Added memory with invalid parameters\n",
173 if (rte_malloc_heap_memory_remove(valid_name, NULL, len) >= 0 ||
174 rte_errno != EINVAL) {
175 printf("%s():%i: Removed memory with invalid parameters\n",
180 if (rte_malloc_heap_memory_attach(valid_name, NULL, len) >= 0 ||
181 rte_errno != EINVAL) {
182 printf("%s():%i: Attached memory with invalid parameters\n",
186 if (rte_malloc_heap_memory_detach(valid_name, NULL, len) >= 0 ||
187 rte_errno != EINVAL) {
188 printf("%s():%i: Detached memory with invalid parameters\n",
193 /* wrong page count */
194 if (rte_malloc_heap_memory_add(valid_name, addr, len,
195 iova, 0, pgsz) >= 0 || rte_errno != EINVAL) {
196 printf("%s():%i: Added memory with invalid parameters\n",
200 if (rte_malloc_heap_memory_add(valid_name, addr, len,
201 iova, n_pages - 1, pgsz) >= 0 || rte_errno != EINVAL) {
202 printf("%s():%i: Added memory with invalid parameters\n",
206 if (rte_malloc_heap_memory_add(valid_name, addr, len,
207 iova, n_pages + 1, pgsz) >= 0 || rte_errno != EINVAL) {
208 printf("%s():%i: Added memory with invalid parameters\n",
213 /* tests passed, destroy heap */
214 if (rte_malloc_heap_destroy(valid_name) != 0) {
215 printf("%s():%i: Failed to destroy valid heap\n",
221 rte_malloc_heap_destroy(valid_name);
226 test_basic(void *addr, size_t len, size_t pgsz, rte_iova_t *iova, int n_pages)
228 const char *heap_name = "heap";
231 const struct rte_memzone *mz = NULL;
234 if (rte_malloc_heap_create(heap_name) != 0) {
235 printf("%s():%i: Failed to create malloc heap\n",
240 /* get socket ID corresponding to this heap */
241 socket_id = rte_malloc_heap_get_socket(heap_name);
243 printf("%s():%i: cannot find socket for external heap\n",
248 /* heap is empty, so any allocation should fail */
249 ptr = rte_malloc_socket("EXTMEM", 64, 0, socket_id);
251 printf("%s():%i: Allocated from empty heap\n", __func__,
256 /* add memory to heap */
257 if (rte_malloc_heap_memory_add(heap_name, addr, len,
258 iova, n_pages, pgsz) != 0) {
259 printf("%s():%i: Failed to add memory to heap\n",
264 /* check that we can get this memory from EAL now */
265 for (i = 0; i < n_pages; i++) {
266 const struct rte_memseg *ms;
267 void *cur = RTE_PTR_ADD(addr, pgsz * i);
269 ms = rte_mem_virt2memseg(cur, NULL);
271 printf("%s():%i: Failed to retrieve memseg for external mem\n",
275 if (ms->addr != cur) {
276 printf("%s():%i: VA mismatch\n", __func__, __LINE__);
279 if (ms->iova != iova[i]) {
280 printf("%s():%i: IOVA mismatch\n", __func__, __LINE__);
285 /* allocate - this now should succeed */
286 ptr = rte_malloc_socket("EXTMEM", 64, 0, socket_id);
288 printf("%s():%i: Failed to allocate from external heap\n",
293 /* check if address is in expected range */
294 if (ptr < addr || ptr >= RTE_PTR_ADD(addr, len)) {
295 printf("%s():%i: Allocated from unexpected address space\n",
300 /* we've allocated something - removing memory should fail */
301 if (rte_malloc_heap_memory_remove(heap_name, addr, len) >= 0 ||
302 rte_errno != EBUSY) {
303 printf("%s():%i: Removing memory succeeded when memory is not free\n",
307 if (rte_malloc_heap_destroy(heap_name) >= 0 || rte_errno != EBUSY) {
308 printf("%s():%i: Destroying heap succeeded when memory is not free\n",
313 /* try allocating an IOVA-contiguous memzone - this should succeed
314 * because we've set up a contiguous IOVA table.
316 mz = rte_memzone_reserve("heap_test", pgsz * 2, socket_id,
317 RTE_MEMZONE_IOVA_CONTIG);
319 printf("%s():%i: Failed to reserve memzone\n",
324 rte_malloc_dump_stats(stdout, NULL);
325 rte_malloc_dump_heaps(stdout);
327 /* free memory - removing it should now succeed */
331 rte_memzone_free(mz);
334 if (rte_malloc_heap_memory_remove(heap_name, addr, len) != 0) {
335 printf("%s():%i: Removing memory from heap failed\n",
339 if (rte_malloc_heap_destroy(heap_name) != 0) {
340 printf("%s():%i: Destroying heap failed\n",
347 rte_memzone_free(mz);
349 /* even if something failed, attempt to clean up */
350 rte_malloc_heap_memory_remove(heap_name, addr, len);
351 rte_malloc_heap_destroy(heap_name);
356 /* we need to test attach/detach in secondary processes. */
358 test_external_mem(void)
360 size_t len = EXTERNAL_MEM_SZ;
361 size_t pgsz = RTE_PGSIZE_4K;
362 rte_iova_t iova[len / pgsz];
366 /* create external memory area */
367 n_pages = RTE_DIM(iova);
368 addr = mmap(NULL, len, PROT_WRITE | PROT_READ,
369 MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
370 if (addr == MAP_FAILED) {
371 printf("%s():%i: Failed to create dummy memory area\n",
375 for (int i = 0; i < n_pages; i++) {
377 rte_iova_t tmp = 0x100000000 + i * pgsz;
381 ret = test_invalid_param(addr, len, pgsz, iova, n_pages);
382 ret |= test_basic(addr, len, pgsz, iova, n_pages);
389 REGISTER_TEST_COMMAND(external_mem_autotest, test_external_mem);