1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Intel Corporation
13 #ifdef RTE_EXEC_ENV_WINDOWS
15 test_external_mem(void)
17 printf("external_mem not supported on Windows, skipping test\n");
26 #include <rte_common.h>
27 #include <rte_debug.h>
29 #include <rte_eal_paging.h>
30 #include <rte_errno.h>
31 #include <rte_malloc.h>
33 #include <rte_string_fns.h>
35 #define EXTERNAL_MEM_SZ (RTE_PGSIZE_4K << 10) /* 4M of data */
38 check_mem(void *addr, rte_iova_t *iova, size_t pgsz, int n_pages)
42 /* check that we can get this memory from EAL now */
43 for (i = 0; i < n_pages; i++) {
44 const struct rte_memseg_list *msl;
45 const struct rte_memseg *ms;
46 void *cur = RTE_PTR_ADD(addr, pgsz * i);
47 rte_iova_t expected_iova;
49 msl = rte_mem_virt2memseg_list(cur);
51 printf("%s():%i: Memseg list is not marked as external\n",
56 ms = rte_mem_virt2memseg(cur, msl);
58 printf("%s():%i: Failed to retrieve memseg for external mem\n",
62 if (ms->addr != cur) {
63 printf("%s():%i: VA mismatch\n", __func__, __LINE__);
66 expected_iova = (iova == NULL) ? RTE_BAD_IOVA : iova[i];
67 if (ms->iova != expected_iova) {
68 printf("%s():%i: IOVA mismatch\n", __func__, __LINE__);
76 test_malloc_invalid_param(void *addr, size_t len, size_t pgsz, rte_iova_t *iova,
79 static const char * const names[] = {
82 "this heap name is definitely way too long to be valid"
84 const char *valid_name = "valid heap name";
87 /* check invalid name handling */
88 for (i = 0; i < RTE_DIM(names); i++) {
89 const char *name = names[i];
91 /* these calls may fail for other reasons, so check errno */
92 if (rte_malloc_heap_create(name) >= 0 || rte_errno != EINVAL) {
93 printf("%s():%i: Created heap with invalid name\n",
98 if (rte_malloc_heap_destroy(name) >= 0 || rte_errno != EINVAL) {
99 printf("%s():%i: Destroyed heap with invalid name\n",
104 if (rte_malloc_heap_get_socket(name) >= 0 ||
105 rte_errno != EINVAL) {
106 printf("%s():%i: Found socket for heap with invalid name\n",
111 if (rte_malloc_heap_memory_add(name, addr, len,
112 NULL, 0, pgsz) >= 0 || rte_errno != EINVAL) {
113 printf("%s():%i: Added memory to heap with invalid name\n",
117 if (rte_malloc_heap_memory_remove(name, addr, len) >= 0 ||
118 rte_errno != EINVAL) {
119 printf("%s():%i: Removed memory from heap with invalid name\n",
124 if (rte_malloc_heap_memory_attach(name, addr, len) >= 0 ||
125 rte_errno != EINVAL) {
126 printf("%s():%i: Attached memory to heap with invalid name\n",
130 if (rte_malloc_heap_memory_detach(name, addr, len) >= 0 ||
131 rte_errno != EINVAL) {
132 printf("%s():%i: Detached memory from heap with invalid name\n",
138 /* do same as above, but with a valid heap name */
140 /* skip create call */
141 if (rte_malloc_heap_destroy(valid_name) >= 0 || rte_errno != ENOENT) {
142 printf("%s():%i: Destroyed heap with invalid name\n",
146 if (rte_malloc_heap_get_socket(valid_name) >= 0 ||
147 rte_errno != ENOENT) {
148 printf("%s():%i: Found socket for heap with invalid name\n",
153 /* these calls may fail for other reasons, so check errno */
154 if (rte_malloc_heap_memory_add(valid_name, addr, len,
155 NULL, 0, pgsz) >= 0 || rte_errno != ENOENT) {
156 printf("%s():%i: Added memory to non-existent heap\n",
160 if (rte_malloc_heap_memory_remove(valid_name, addr, len) >= 0 ||
161 rte_errno != ENOENT) {
162 printf("%s():%i: Removed memory from non-existent heap\n",
167 if (rte_malloc_heap_memory_attach(valid_name, addr, len) >= 0 ||
168 rte_errno != ENOENT) {
169 printf("%s():%i: Attached memory to non-existent heap\n",
173 if (rte_malloc_heap_memory_detach(valid_name, addr, len) >= 0 ||
174 rte_errno != ENOENT) {
175 printf("%s():%i: Detached memory from non-existent heap\n",
180 /* create a valid heap but test other invalid parameters */
181 if (rte_malloc_heap_create(valid_name) != 0) {
182 printf("%s():%i: Failed to create valid heap\n",
188 if (rte_malloc_heap_memory_add(valid_name, addr, 0,
189 NULL, 0, pgsz) >= 0 || rte_errno != EINVAL) {
190 printf("%s():%i: Added memory with invalid parameters\n",
195 if (rte_malloc_heap_memory_remove(valid_name, addr, 0) >= 0 ||
196 rte_errno != EINVAL) {
197 printf("%s():%i: Removed memory with invalid parameters\n",
202 if (rte_malloc_heap_memory_attach(valid_name, addr, 0) >= 0 ||
203 rte_errno != EINVAL) {
204 printf("%s():%i: Attached memory with invalid parameters\n",
208 if (rte_malloc_heap_memory_detach(valid_name, addr, 0) >= 0 ||
209 rte_errno != EINVAL) {
210 printf("%s():%i: Detached memory with invalid parameters\n",
216 if (rte_malloc_heap_memory_add(valid_name, NULL, len,
217 NULL, 0, pgsz) >= 0 || rte_errno != EINVAL) {
218 printf("%s():%i: Added memory with invalid parameters\n",
223 if (rte_malloc_heap_memory_remove(valid_name, NULL, len) >= 0 ||
224 rte_errno != EINVAL) {
225 printf("%s():%i: Removed memory with invalid parameters\n",
230 if (rte_malloc_heap_memory_attach(valid_name, NULL, len) >= 0 ||
231 rte_errno != EINVAL) {
232 printf("%s():%i: Attached memory with invalid parameters\n",
236 if (rte_malloc_heap_memory_detach(valid_name, NULL, len) >= 0 ||
237 rte_errno != EINVAL) {
238 printf("%s():%i: Detached memory with invalid parameters\n",
243 /* the following tests are only valid if IOVA table is not NULL */
245 /* wrong page count */
246 if (rte_malloc_heap_memory_add(valid_name, addr, len,
247 iova, 0, pgsz) >= 0 || rte_errno != EINVAL) {
248 printf("%s():%i: Added memory with invalid parameters\n",
252 if (rte_malloc_heap_memory_add(valid_name, addr, len,
253 iova, n_pages - 1, pgsz) >= 0 ||
254 rte_errno != EINVAL) {
255 printf("%s():%i: Added memory with invalid parameters\n",
259 if (rte_malloc_heap_memory_add(valid_name, addr, len,
260 iova, n_pages + 1, pgsz) >= 0 ||
261 rte_errno != EINVAL) {
262 printf("%s():%i: Added memory with invalid parameters\n",
268 /* tests passed, destroy heap */
269 if (rte_malloc_heap_destroy(valid_name) != 0) {
270 printf("%s():%i: Failed to destroy valid heap\n",
276 rte_malloc_heap_destroy(valid_name);
281 test_malloc_basic(void *addr, size_t len, size_t pgsz, rte_iova_t *iova,
284 const char *heap_name = "heap";
287 const struct rte_memzone *mz = NULL, *contig_mz = NULL;
290 if (rte_malloc_heap_create(heap_name) != 0) {
291 printf("%s():%i: Failed to create malloc heap\n",
296 /* get socket ID corresponding to this heap */
297 socket_id = rte_malloc_heap_get_socket(heap_name);
299 printf("%s():%i: cannot find socket for external heap\n",
304 /* heap is empty, so any allocation should fail */
305 ptr = rte_malloc_socket("EXTMEM", 64, 0, socket_id);
307 printf("%s():%i: Allocated from empty heap\n", __func__,
312 /* add memory to heap */
313 if (rte_malloc_heap_memory_add(heap_name, addr, len,
314 iova, n_pages, pgsz) != 0) {
315 printf("%s():%i: Failed to add memory to heap\n",
320 /* check if memory is accessible from EAL */
321 if (check_mem(addr, iova, pgsz, n_pages) < 0)
324 /* allocate - this now should succeed */
325 ptr = rte_malloc_socket("EXTMEM", 64, 0, socket_id);
327 printf("%s():%i: Failed to allocate from external heap\n",
332 /* check if address is in expected range */
333 if (ptr < addr || ptr >= RTE_PTR_ADD(addr, len)) {
334 printf("%s():%i: Allocated from unexpected address space\n",
339 /* we've allocated something - removing memory should fail */
340 if (rte_malloc_heap_memory_remove(heap_name, addr, len) >= 0 ||
341 rte_errno != EBUSY) {
342 printf("%s():%i: Removing memory succeeded when memory is not free\n",
346 if (rte_malloc_heap_destroy(heap_name) >= 0 || rte_errno != EBUSY) {
347 printf("%s():%i: Destroying heap succeeded when memory is not free\n",
352 /* try allocating a memzone */
353 mz = rte_memzone_reserve("heap_test", pgsz * 2, socket_id, 0);
355 printf("%s():%i: Failed to reserve memzone\n",
359 /* try allocating an IOVA-contiguous memzone - this should succeed
360 * if we've set up a contiguous IOVA table, and fail if we haven't.
362 contig_mz = rte_memzone_reserve("heap_test_contig", pgsz * 2, socket_id,
363 RTE_MEMZONE_IOVA_CONTIG);
364 if ((iova == NULL) != (contig_mz == NULL)) {
365 printf("%s():%i: Failed to reserve memzone\n",
370 rte_malloc_dump_stats(stdout, NULL);
371 rte_malloc_dump_heaps(stdout);
373 /* free memory - removing it should now succeed */
377 rte_memzone_free(mz);
379 rte_memzone_free(contig_mz);
382 if (rte_malloc_heap_memory_remove(heap_name, addr, len) != 0) {
383 printf("%s():%i: Removing memory from heap failed\n",
387 if (rte_malloc_heap_destroy(heap_name) != 0) {
388 printf("%s():%i: Destroying heap failed\n",
395 rte_memzone_free(contig_mz);
396 rte_memzone_free(mz);
398 /* even if something failed, attempt to clean up */
399 rte_malloc_heap_memory_remove(heap_name, addr, len);
400 rte_malloc_heap_destroy(heap_name);
406 test_extmem_invalid_param(void *addr, size_t len, size_t pgsz, rte_iova_t *iova,
409 /* these calls may fail for other reasons, so check errno */
410 if (rte_extmem_unregister(addr, len) >= 0 ||
411 rte_errno != ENOENT) {
412 printf("%s():%i: Unregistered non-existent memory\n",
417 if (rte_extmem_attach(addr, len) >= 0 ||
418 rte_errno != ENOENT) {
419 printf("%s():%i: Attached to non-existent memory\n",
423 if (rte_extmem_attach(addr, len) >= 0 ||
424 rte_errno != ENOENT) {
425 printf("%s():%i: Detached from non-existent memory\n",
431 if (rte_extmem_register(addr, 0, NULL, 0, pgsz) >= 0 ||
432 rte_errno != EINVAL) {
433 printf("%s():%i: Registered memory with invalid parameters\n",
438 if (rte_extmem_unregister(addr, 0) >= 0 ||
439 rte_errno != EINVAL) {
440 printf("%s():%i: Unregistered memory with invalid parameters\n",
445 if (rte_extmem_attach(addr, 0) >= 0 ||
446 rte_errno != EINVAL) {
447 printf("%s():%i: Attached memory with invalid parameters\n",
451 if (rte_extmem_attach(addr, 0) >= 0 ||
452 rte_errno != EINVAL) {
453 printf("%s():%i: Detached memory with invalid parameters\n",
459 if (rte_extmem_register(NULL, len, NULL, 0, pgsz) >= 0 ||
460 rte_errno != EINVAL) {
461 printf("%s():%i: Registered memory with invalid parameters\n",
466 if (rte_extmem_unregister(NULL, len) >= 0 ||
467 rte_errno != EINVAL) {
468 printf("%s():%i: Unregistered memory with invalid parameters\n",
473 if (rte_extmem_attach(NULL, len) >= 0 ||
474 rte_errno != EINVAL) {
475 printf("%s():%i: Attached memory with invalid parameters\n",
479 if (rte_extmem_attach(NULL, len) >= 0 ||
480 rte_errno != EINVAL) {
481 printf("%s():%i: Detached memory with invalid parameters\n",
486 /* the following tests are only valid if IOVA table is not NULL */
488 /* wrong page count */
489 if (rte_extmem_register(addr, len,
490 iova, 0, pgsz) >= 0 || rte_errno != EINVAL) {
491 printf("%s():%i: Registered memory with invalid parameters\n",
495 if (rte_extmem_register(addr, len,
496 iova, n_pages - 1, pgsz) >= 0 ||
497 rte_errno != EINVAL) {
498 printf("%s():%i: Registered memory with invalid parameters\n",
502 if (rte_extmem_register(addr, len,
503 iova, n_pages + 1, pgsz) >= 0 ||
504 rte_errno != EINVAL) {
505 printf("%s():%i: Registered memory with invalid parameters\n",
515 test_extmem_basic(void *addr, size_t len, size_t pgsz, rte_iova_t *iova,
518 /* register memory */
519 if (rte_extmem_register(addr, len, iova, n_pages, pgsz) != 0) {
520 printf("%s():%i: Failed to register memory\n",
525 /* check if memory is accessible from EAL */
526 if (check_mem(addr, iova, pgsz, n_pages) < 0)
529 if (rte_extmem_unregister(addr, len) != 0) {
530 printf("%s():%i: Removing memory from heap failed\n",
537 /* even if something failed, attempt to clean up */
538 rte_extmem_unregister(addr, len);
543 /* we need to test attach/detach in secondary processes. */
545 test_external_mem(void)
547 size_t pgsz = rte_mem_page_size();
548 size_t len = EXTERNAL_MEM_SZ;
549 rte_iova_t iova[len / pgsz];
554 /* create external memory area */
555 n_pages = RTE_DIM(iova);
556 addr = mmap(NULL, len, PROT_WRITE | PROT_READ,
557 MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
558 if (addr == MAP_FAILED) {
559 printf("%s():%i: Failed to create dummy memory area\n",
563 for (i = 0; i < n_pages; i++) {
565 rte_iova_t tmp = 0x100000000 + i * pgsz;
569 /* test external heap memory */
570 ret = test_malloc_invalid_param(addr, len, pgsz, iova, n_pages);
571 ret |= test_malloc_basic(addr, len, pgsz, iova, n_pages);
572 /* when iova table is NULL, everything should still work */
573 ret |= test_malloc_invalid_param(addr, len, pgsz, NULL, n_pages);
574 ret |= test_malloc_basic(addr, len, pgsz, NULL, n_pages);
576 /* test non-heap memory */
577 ret |= test_extmem_invalid_param(addr, len, pgsz, iova, n_pages);
578 ret |= test_extmem_basic(addr, len, pgsz, iova, n_pages);
579 /* when iova table is NULL, everything should still work */
580 ret |= test_extmem_invalid_param(addr, len, pgsz, NULL, n_pages);
581 ret |= test_extmem_basic(addr, len, pgsz, NULL, n_pages);
588 #endif /* !RTE_EXEC_ENV_WINDOWS */
590 REGISTER_TEST_COMMAND(external_mem_autotest, test_external_mem);