1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
12 #include <sys/queue.h>
14 #include <rte_common.h>
15 #include <rte_eal_paging.h>
17 #include <rte_debug.h>
18 #include <rte_errno.h>
19 #include <rte_memory.h>
20 #include <rte_launch.h>
21 #include <rte_cycles.h>
23 #include <rte_per_lcore.h>
24 #include <rte_lcore.h>
25 #include <rte_branch_prediction.h>
26 #include <rte_mempool.h>
27 #include <rte_spinlock.h>
28 #include <rte_malloc.h>
29 #include <rte_mbuf_pool_ops.h>
38 * Basic tests: done on one core with and without cache:
40 * - Get one object, put one object
41 * - Get two objects, put two objects
42 * - Get all objects, test that their content is not modified and
43 * put them back in the pool.
46 #define MEMPOOL_ELT_SIZE 2048
48 #define MEMPOOL_SIZE ((rte_lcore_count()*(MAX_KEEP+RTE_MEMPOOL_CACHE_MAX_SIZE))-1)
50 #define LOG_ERR() printf("test failed at %s():%d\n", __func__, __LINE__)
51 #define RET_ERR() do { \
55 #define GOTO_ERR(var, label) do { \
62 * save the object number in the first 4 bytes of object data. All
63 * other bytes are set to 0.
66 my_obj_init(struct rte_mempool *mp, __rte_unused void *arg,
67 void *obj, unsigned i)
69 uint32_t *objnum = obj;
71 memset(obj, 0, mp->elt_size);
75 /* basic tests (done on one core) */
77 test_mempool_basic(struct rte_mempool *mp, int use_external_cache)
86 struct rte_mempool_cache *cache;
88 if (use_external_cache) {
89 /* Create a user-owned mempool cache. */
90 cache = rte_mempool_cache_create(RTE_MEMPOOL_CACHE_MAX_SIZE,
95 /* May be NULL if cache is disabled. */
96 cache = rte_mempool_default_cache(mp, rte_lcore_id());
99 /* dump the mempool status */
100 rte_mempool_dump(stdout, mp);
102 printf("get an object\n");
103 if (rte_mempool_generic_get(mp, &obj, 1, cache) < 0)
105 rte_mempool_dump(stdout, mp);
107 /* tests that improve coverage */
108 printf("get object count\n");
109 /* We have to count the extra caches, one in this case. */
110 offset = use_external_cache ? 1 * cache->len : 0;
111 if (rte_mempool_avail_count(mp) + offset != MEMPOOL_SIZE - 1)
114 printf("get private data\n");
115 if (rte_mempool_get_priv(mp) != (char *)mp +
116 RTE_MEMPOOL_HEADER_SIZE(mp, mp->cache_size))
119 #ifndef RTE_EXEC_ENV_FREEBSD /* rte_mem_virt2iova() not supported on bsd */
120 printf("get physical address of an object\n");
121 if (rte_mempool_virt2iova(obj) != rte_mem_virt2iova(obj))
125 printf("put the object back\n");
126 rte_mempool_generic_put(mp, &obj, 1, cache);
127 rte_mempool_dump(stdout, mp);
129 printf("get 2 objects\n");
130 if (rte_mempool_generic_get(mp, &obj, 1, cache) < 0)
132 if (rte_mempool_generic_get(mp, &obj2, 1, cache) < 0) {
133 rte_mempool_generic_put(mp, &obj, 1, cache);
136 rte_mempool_dump(stdout, mp);
138 printf("put the objects back\n");
139 rte_mempool_generic_put(mp, &obj, 1, cache);
140 rte_mempool_generic_put(mp, &obj2, 1, cache);
141 rte_mempool_dump(stdout, mp);
144 * get many objects: we cannot get them all because the cache
145 * on other cores may not be empty.
147 objtable = malloc(MEMPOOL_SIZE * sizeof(void *));
148 if (objtable == NULL)
151 for (i = 0; i < MEMPOOL_SIZE; i++) {
152 if (rte_mempool_generic_get(mp, &objtable[i], 1, cache) < 0)
157 * for each object, check that its content was not modified,
158 * and put objects back in pool
164 if (*objnum > MEMPOOL_SIZE) {
165 printf("bad object number(%d)\n", *objnum);
169 for (j = sizeof(*objnum); j < mp->elt_size; j++) {
170 if (obj_data[j] != 0)
174 rte_mempool_generic_put(mp, &objtable[i], 1, cache);
179 printf("objects were modified!\n");
182 if (use_external_cache) {
183 rte_mempool_cache_flush(cache, mp);
184 rte_mempool_cache_free(cache);
190 static int test_mempool_creation_with_exceeded_cache_size(void)
192 struct rte_mempool *mp_cov;
194 mp_cov = rte_mempool_create("test_mempool_cache_too_big",
197 RTE_MEMPOOL_CACHE_MAX_SIZE + 32, 0,
202 if (mp_cov != NULL) {
203 rte_mempool_free(mp_cov);
210 static int test_mempool_creation_with_invalid_flags(void)
212 struct rte_mempool *mp_cov;
214 mp_cov = rte_mempool_create("test_mempool_invalid_flags", MEMPOOL_SIZE,
215 MEMPOOL_ELT_SIZE, 0, 0,
218 SOCKET_ID_ANY, ~RTE_MEMPOOL_VALID_USER_FLAGS);
220 if (mp_cov != NULL) {
221 rte_mempool_free(mp_cov);
228 static struct rte_mempool *mp_spsc;
229 static rte_spinlock_t scsp_spinlock;
230 static void *scsp_obj_table[MAX_KEEP];
233 * single producer function
235 static int test_mempool_single_producer(void)
239 uint64_t start_cycles, end_cycles;
240 uint64_t duration = rte_get_timer_hz() / 4;
242 start_cycles = rte_get_timer_cycles();
244 end_cycles = rte_get_timer_cycles();
245 /* duration uses up, stop producing */
246 if (start_cycles + duration < end_cycles)
248 rte_spinlock_lock(&scsp_spinlock);
249 for (i = 0; i < MAX_KEEP; i ++) {
250 if (NULL != scsp_obj_table[i]) {
251 obj = scsp_obj_table[i];
255 rte_spinlock_unlock(&scsp_spinlock);
259 if (rte_mempool_from_obj(obj) != mp_spsc) {
260 printf("obj not owned by this mempool\n");
263 rte_mempool_put(mp_spsc, obj);
264 rte_spinlock_lock(&scsp_spinlock);
265 scsp_obj_table[i] = NULL;
266 rte_spinlock_unlock(&scsp_spinlock);
273 * single consumer function
275 static int test_mempool_single_consumer(void)
279 uint64_t start_cycles, end_cycles;
280 uint64_t duration = rte_get_timer_hz() / 8;
282 start_cycles = rte_get_timer_cycles();
284 end_cycles = rte_get_timer_cycles();
285 /* duration uses up, stop consuming */
286 if (start_cycles + duration < end_cycles)
288 rte_spinlock_lock(&scsp_spinlock);
289 for (i = 0; i < MAX_KEEP; i ++) {
290 if (NULL == scsp_obj_table[i])
293 rte_spinlock_unlock(&scsp_spinlock);
296 if (rte_mempool_get(mp_spsc, &obj) < 0)
298 rte_spinlock_lock(&scsp_spinlock);
299 scsp_obj_table[i] = obj;
300 rte_spinlock_unlock(&scsp_spinlock);
307 * test function for mempool test based on singple consumer and single producer,
308 * can run on one lcore only
311 test_mempool_launch_single_consumer(__rte_unused void *arg)
313 return test_mempool_single_consumer();
317 my_mp_init(struct rte_mempool *mp, __rte_unused void *arg)
319 printf("mempool name is %s\n", mp->name);
320 /* nothing to be implemented here*/
325 * it tests the mempool operations based on singple producer and single consumer
328 test_mempool_sp_sc(void)
331 unsigned lcore_id = rte_lcore_id();
334 /* create a mempool with single producer/consumer ring */
335 if (mp_spsc == NULL) {
336 mp_spsc = rte_mempool_create("test_mempool_sp_sc", MEMPOOL_SIZE,
337 MEMPOOL_ELT_SIZE, 0, 0,
341 RTE_MEMPOOL_F_NO_CACHE_ALIGN | RTE_MEMPOOL_F_SP_PUT |
342 RTE_MEMPOOL_F_SC_GET);
346 if (rte_mempool_lookup("test_mempool_sp_sc") != mp_spsc) {
347 printf("Cannot lookup mempool from its name\n");
351 lcore_next = rte_get_next_lcore(lcore_id, 0, 1);
352 if (lcore_next >= RTE_MAX_LCORE) {
356 if (rte_eal_lcore_role(lcore_next) != ROLE_RTE) {
360 rte_spinlock_init(&scsp_spinlock);
361 memset(scsp_obj_table, 0, sizeof(scsp_obj_table));
362 rte_eal_remote_launch(test_mempool_launch_single_consumer, NULL,
364 if (test_mempool_single_producer() < 0)
367 if (rte_eal_wait_lcore(lcore_next) < 0)
371 rte_mempool_free(mp_spsc);
378 * it tests some more basic of mempool
381 test_mempool_basic_ex(struct rte_mempool *mp)
391 obj = rte_calloc("test_mempool_basic_ex", MEMPOOL_SIZE,
394 printf("test_mempool_basic_ex fail to rte_malloc\n");
397 printf("test_mempool_basic_ex now mempool (%s) has %u free entries\n",
398 mp->name, rte_mempool_in_use_count(mp));
399 if (rte_mempool_full(mp) != 1) {
400 printf("test_mempool_basic_ex the mempool should be full\n");
401 goto fail_mp_basic_ex;
404 for (i = 0; i < MEMPOOL_SIZE; i ++) {
405 if (rte_mempool_get(mp, &obj[i]) < 0) {
406 printf("test_mp_basic_ex fail to get object for [%u]\n",
408 goto fail_mp_basic_ex;
411 if (rte_mempool_get(mp, &err_obj) == 0) {
412 printf("test_mempool_basic_ex get an impossible obj\n");
413 goto fail_mp_basic_ex;
415 printf("number: %u\n", i);
416 if (rte_mempool_empty(mp) != 1) {
417 printf("test_mempool_basic_ex the mempool should be empty\n");
418 goto fail_mp_basic_ex;
421 for (i = 0; i < MEMPOOL_SIZE; i++)
422 rte_mempool_put(mp, obj[i]);
424 if (rte_mempool_full(mp) != 1) {
425 printf("test_mempool_basic_ex the mempool should be full\n");
426 goto fail_mp_basic_ex;
433 rte_free((void *)obj);
439 test_mempool_same_name_twice_creation(void)
441 struct rte_mempool *mp_tc, *mp_tc2;
443 mp_tc = rte_mempool_create("test_mempool_same_name", MEMPOOL_SIZE,
444 MEMPOOL_ELT_SIZE, 0, 0,
452 mp_tc2 = rte_mempool_create("test_mempool_same_name", MEMPOOL_SIZE,
453 MEMPOOL_ELT_SIZE, 0, 0,
458 if (mp_tc2 != NULL) {
459 rte_mempool_free(mp_tc);
460 rte_mempool_free(mp_tc2);
464 rte_mempool_free(mp_tc);
469 walk_cb(struct rte_mempool *mp, void *userdata __rte_unused)
471 printf("\t%s\n", mp->name);
479 test_mp_mem_init(struct rte_mempool *mp,
480 __rte_unused void *opaque,
481 __rte_unused struct rte_mempool_memhdr *memhdr,
482 __rte_unused unsigned int mem_idx)
484 struct mp_data *data = opaque;
490 /* nothing to be implemented here*/
494 struct test_mempool_events_data {
495 struct rte_mempool *mp;
496 enum rte_mempool_event event;
501 test_mempool_events_cb(enum rte_mempool_event event,
502 struct rte_mempool *mp, void *user_data)
504 struct test_mempool_events_data *data = user_data;
508 data->invoked = true;
512 test_mempool_events(int (*populate)(struct rte_mempool *mp))
514 #pragma push_macro("RTE_TEST_TRACE_FAILURE")
515 #undef RTE_TEST_TRACE_FAILURE
516 #define RTE_TEST_TRACE_FAILURE(...) do { goto fail; } while (0)
518 static const size_t CB_NUM = 3;
519 static const size_t MP_NUM = 2;
521 struct test_mempool_events_data data[CB_NUM];
522 struct rte_mempool *mp[MP_NUM], *freed;
523 char name[RTE_MEMPOOL_NAMESIZE];
527 memset(mp, 0, sizeof(mp));
528 for (i = 0; i < CB_NUM; i++) {
529 ret = rte_mempool_event_callback_register
530 (test_mempool_events_cb, &data[i]);
531 RTE_TEST_ASSERT_EQUAL(ret, 0, "Failed to register the callback %zu: %s",
532 i, rte_strerror(rte_errno));
534 ret = rte_mempool_event_callback_unregister(test_mempool_events_cb, mp);
535 RTE_TEST_ASSERT_NOT_EQUAL(ret, 0, "Unregistered a non-registered callback");
536 /* NULL argument has no special meaning in this API. */
537 ret = rte_mempool_event_callback_unregister(test_mempool_events_cb,
539 RTE_TEST_ASSERT_NOT_EQUAL(ret, 0, "Unregistered a non-registered callback with NULL argument");
541 /* Create mempool 0 that will be observed by all callbacks. */
542 memset(&data, 0, sizeof(data));
543 strcpy(name, "empty0");
544 mp[0] = rte_mempool_create_empty(name, MEMPOOL_SIZE,
545 MEMPOOL_ELT_SIZE, 0, 0,
547 RTE_TEST_ASSERT_NOT_NULL(mp[0], "Cannot create mempool %s: %s",
548 name, rte_strerror(rte_errno));
549 for (j = 0; j < CB_NUM; j++)
550 RTE_TEST_ASSERT_EQUAL(data[j].invoked, false,
551 "Callback %zu invoked on %s mempool creation",
554 rte_mempool_set_ops_byname(mp[0], rte_mbuf_best_mempool_ops(), NULL);
555 ret = populate(mp[0]);
556 RTE_TEST_ASSERT_EQUAL(ret, (int)mp[0]->size, "Failed to populate mempool %s: %s",
557 name, rte_strerror(-ret));
558 for (j = 0; j < CB_NUM; j++) {
559 RTE_TEST_ASSERT_EQUAL(data[j].invoked, true,
560 "Callback %zu not invoked on mempool %s population",
562 RTE_TEST_ASSERT_EQUAL(data[j].event,
563 RTE_MEMPOOL_EVENT_READY,
564 "Wrong callback invoked, expected READY");
565 RTE_TEST_ASSERT_EQUAL(data[j].mp, mp[0],
566 "Callback %zu invoked for a wrong mempool instead of %s",
570 /* Check that unregistered callback 0 observes no events. */
571 ret = rte_mempool_event_callback_unregister(test_mempool_events_cb,
573 RTE_TEST_ASSERT_EQUAL(ret, 0, "Failed to unregister callback 0: %s",
574 rte_strerror(rte_errno));
575 memset(&data, 0, sizeof(data));
576 strcpy(name, "empty1");
577 mp[1] = rte_mempool_create_empty(name, MEMPOOL_SIZE,
578 MEMPOOL_ELT_SIZE, 0, 0,
580 RTE_TEST_ASSERT_NOT_NULL(mp[1], "Cannot create mempool %s: %s",
581 name, rte_strerror(rte_errno));
582 rte_mempool_set_ops_byname(mp[1], rte_mbuf_best_mempool_ops(), NULL);
583 ret = populate(mp[1]);
584 RTE_TEST_ASSERT_EQUAL(ret, (int)mp[1]->size, "Failed to populate mempool %s: %s",
585 name, rte_strerror(-ret));
586 RTE_TEST_ASSERT_EQUAL(data[0].invoked, false,
587 "Unregistered callback 0 invoked on %s mempool populaton",
590 for (i = 0; i < MP_NUM; i++) {
591 memset(&data, 0, sizeof(data));
592 sprintf(name, "empty%zu", i);
593 rte_mempool_free(mp[i]);
595 * Save pointer to check that it was passed to the callback,
596 * but put NULL into the array in case cleanup is called early.
600 for (j = 1; j < CB_NUM; j++) {
601 RTE_TEST_ASSERT_EQUAL(data[j].invoked, true,
602 "Callback %zu not invoked on mempool %s destruction",
604 RTE_TEST_ASSERT_EQUAL(data[j].event,
605 RTE_MEMPOOL_EVENT_DESTROY,
606 "Wrong callback invoked, expected DESTROY");
607 RTE_TEST_ASSERT_EQUAL(data[j].mp, freed,
608 "Callback %zu invoked for a wrong mempool instead of %s",
611 RTE_TEST_ASSERT_EQUAL(data[0].invoked, false,
612 "Unregistered callback 0 invoked on %s mempool destruction",
616 for (j = 1; j < CB_NUM; j++) {
617 ret = rte_mempool_event_callback_unregister
618 (test_mempool_events_cb, &data[j]);
619 RTE_TEST_ASSERT_EQUAL(ret, 0, "Failed to unregister the callback %zu: %s",
620 j, rte_strerror(rte_errno));
625 for (j = 0; j < CB_NUM; j++)
626 rte_mempool_event_callback_unregister
627 (test_mempool_events_cb, &data[j]);
628 for (i = 0; i < MP_NUM; i++)
629 rte_mempool_free(mp[i]);
632 #pragma pop_macro("RTE_TEST_TRACE_FAILURE")
635 struct test_mempool_events_safety_data {
637 int (*api_func)(rte_mempool_event_callback *func, void *user_data);
638 rte_mempool_event_callback *cb_func;
644 test_mempool_events_safety_cb(enum rte_mempool_event event,
645 struct rte_mempool *mp, void *user_data)
647 struct test_mempool_events_safety_data *data = user_data;
651 data->invoked = true;
652 data->ret = data->api_func(data->cb_func, data->cb_user_data);
656 test_mempool_events_safety(void)
658 #pragma push_macro("RTE_TEST_TRACE_FAILURE")
659 #undef RTE_TEST_TRACE_FAILURE
660 #define RTE_TEST_TRACE_FAILURE(...) do { \
665 struct test_mempool_events_data data;
666 struct test_mempool_events_safety_data sdata[2];
667 struct rte_mempool *mp;
672 sdata[0].api_func = rte_mempool_event_callback_unregister;
673 sdata[0].cb_func = test_mempool_events_safety_cb;
674 sdata[0].cb_user_data = &sdata[0];
676 rte_mempool_event_callback_register(test_mempool_events_safety_cb,
678 /* inserts a callback after itself */
679 sdata[1].api_func = rte_mempool_event_callback_register;
680 sdata[1].cb_func = test_mempool_events_cb;
681 sdata[1].cb_user_data = &data;
683 rte_mempool_event_callback_register(test_mempool_events_safety_cb,
686 mp = rte_mempool_create_empty("empty", MEMPOOL_SIZE,
687 MEMPOOL_ELT_SIZE, 0, 0,
689 RTE_TEST_ASSERT_NOT_NULL(mp, "Cannot create mempool: %s",
690 rte_strerror(rte_errno));
691 memset(&data, 0, sizeof(data));
692 ret = rte_mempool_populate_default(mp);
693 RTE_TEST_ASSERT_EQUAL(ret, (int)mp->size, "Failed to populate mempool: %s",
696 RTE_TEST_ASSERT_EQUAL(sdata[0].ret, 0, "Callback failed to unregister itself: %s",
697 rte_strerror(rte_errno));
698 RTE_TEST_ASSERT_EQUAL(sdata[1].ret, 0, "Failed to insert a new callback: %s",
699 rte_strerror(rte_errno));
700 RTE_TEST_ASSERT_EQUAL(data.invoked, false,
701 "Inserted callback is invoked on mempool population");
703 memset(&data, 0, sizeof(data));
704 sdata[0].invoked = false;
705 rte_mempool_free(mp);
707 RTE_TEST_ASSERT_EQUAL(sdata[0].invoked, false,
708 "Callback that unregistered itself was called");
709 RTE_TEST_ASSERT_EQUAL(sdata[1].ret, -EEXIST,
710 "New callback inserted twice");
711 RTE_TEST_ASSERT_EQUAL(data.invoked, true,
712 "Inserted callback is not invoked on mempool destruction");
714 rte_mempool_event_callback_unregister(test_mempool_events_cb, &data);
715 for (i = 0; i < RTE_DIM(sdata); i++)
716 rte_mempool_event_callback_unregister
717 (test_mempool_events_safety_cb, &sdata[i]);
721 /* cleanup, don't care which callbacks are already removed */
722 rte_mempool_event_callback_unregister(test_mempool_events_cb, &data);
723 for (i = 0; i < RTE_DIM(sdata); i++)
724 rte_mempool_event_callback_unregister
725 (test_mempool_events_safety_cb, &sdata[i]);
726 /* in case of failure before the planned destruction */
727 rte_mempool_free(mp);
730 #pragma pop_macro("RTE_TEST_TRACE_FAILURE")
733 #pragma push_macro("RTE_TEST_TRACE_FAILURE")
734 #undef RTE_TEST_TRACE_FAILURE
735 #define RTE_TEST_TRACE_FAILURE(...) do { \
741 test_mempool_flag_non_io_set_when_no_iova_contig_set(void)
743 struct rte_mempool *mp = NULL;
746 mp = rte_mempool_create_empty("empty", MEMPOOL_SIZE,
747 MEMPOOL_ELT_SIZE, 0, 0,
748 SOCKET_ID_ANY, RTE_MEMPOOL_F_NO_IOVA_CONTIG);
749 RTE_TEST_ASSERT_NOT_NULL(mp, "Cannot create mempool: %s",
750 rte_strerror(rte_errno));
751 rte_mempool_set_ops_byname(mp, rte_mbuf_best_mempool_ops(), NULL);
752 ret = rte_mempool_populate_default(mp);
753 RTE_TEST_ASSERT(ret > 0, "Failed to populate mempool: %s",
755 RTE_TEST_ASSERT(mp->flags & RTE_MEMPOOL_F_NON_IO,
756 "NON_IO flag is not set when NO_IOVA_CONTIG is set");
759 rte_mempool_free(mp);
764 test_mempool_flag_non_io_unset_when_populated_with_valid_iova(void)
768 size_t total_size = MEMPOOL_ELT_SIZE * MEMPOOL_SIZE;
769 size_t block_size = total_size / 3;
770 struct rte_mempool *mp = NULL;
774 * Since objects from the pool are never used in the test,
775 * we don't care for contiguous IOVA, on the other hand,
776 * reiuring it could cause spurious test failures.
778 virt = rte_malloc("test_mempool", total_size, rte_mem_page_size());
779 RTE_TEST_ASSERT_NOT_NULL(virt, "Cannot allocate memory");
780 iova = rte_mem_virt2iova(virt);
781 RTE_TEST_ASSERT_NOT_EQUAL(iova, RTE_BAD_IOVA, "Cannot get IOVA");
782 mp = rte_mempool_create_empty("empty", MEMPOOL_SIZE,
783 MEMPOOL_ELT_SIZE, 0, 0,
785 RTE_TEST_ASSERT_NOT_NULL(mp, "Cannot create mempool: %s",
786 rte_strerror(rte_errno));
788 ret = rte_mempool_populate_iova(mp, RTE_PTR_ADD(virt, 1 * block_size),
789 RTE_BAD_IOVA, block_size, NULL, NULL);
790 RTE_TEST_ASSERT(ret > 0, "Failed to populate mempool: %s",
792 RTE_TEST_ASSERT(mp->flags & RTE_MEMPOOL_F_NON_IO,
793 "NON_IO flag is not set when mempool is populated with only RTE_BAD_IOVA");
795 ret = rte_mempool_populate_iova(mp, virt, iova, block_size, NULL, NULL);
796 RTE_TEST_ASSERT(ret > 0, "Failed to populate mempool: %s",
798 RTE_TEST_ASSERT(!(mp->flags & RTE_MEMPOOL_F_NON_IO),
799 "NON_IO flag is not unset when mempool is populated with valid IOVA");
801 ret = rte_mempool_populate_iova(mp, RTE_PTR_ADD(virt, 2 * block_size),
802 RTE_BAD_IOVA, block_size, NULL, NULL);
803 RTE_TEST_ASSERT(ret > 0, "Failed to populate mempool: %s",
805 RTE_TEST_ASSERT(!(mp->flags & RTE_MEMPOOL_F_NON_IO),
806 "NON_IO flag is set even when some objects have valid IOVA");
810 rte_mempool_free(mp);
816 test_mempool_flag_non_io_unset_by_default(void)
818 struct rte_mempool *mp;
821 mp = rte_mempool_create_empty("empty", MEMPOOL_SIZE,
822 MEMPOOL_ELT_SIZE, 0, 0,
824 RTE_TEST_ASSERT_NOT_NULL(mp, "Cannot create mempool: %s",
825 rte_strerror(rte_errno));
826 ret = rte_mempool_populate_default(mp);
827 RTE_TEST_ASSERT_EQUAL(ret, (int)mp->size, "Failed to populate mempool: %s",
829 RTE_TEST_ASSERT(!(mp->flags & RTE_MEMPOOL_F_NON_IO),
830 "NON_IO flag is set by default");
833 rte_mempool_free(mp);
837 #pragma pop_macro("RTE_TEST_TRACE_FAILURE")
843 uint32_t nb_objs = 0;
844 uint32_t nb_mem_chunks = 0;
845 struct rte_mempool *mp_cache = NULL;
846 struct rte_mempool *mp_nocache = NULL;
847 struct rte_mempool *mp_stack_anon = NULL;
848 struct rte_mempool *mp_stack_mempool_iter = NULL;
849 struct rte_mempool *mp_stack = NULL;
850 struct rte_mempool *default_pool = NULL;
851 struct mp_data cb_arg = {
854 const char *default_pool_ops = rte_mbuf_best_mempool_ops();
856 /* create a mempool (without cache) */
857 mp_nocache = rte_mempool_create("test_nocache", MEMPOOL_SIZE,
858 MEMPOOL_ELT_SIZE, 0, 0,
863 if (mp_nocache == NULL) {
864 printf("cannot allocate mp_nocache mempool\n");
868 /* create a mempool (with cache) */
869 mp_cache = rte_mempool_create("test_cache", MEMPOOL_SIZE,
871 RTE_MEMPOOL_CACHE_MAX_SIZE, 0,
876 if (mp_cache == NULL) {
877 printf("cannot allocate mp_cache mempool\n");
881 /* create an empty mempool */
882 mp_stack_anon = rte_mempool_create_empty("test_stack_anon",
885 RTE_MEMPOOL_CACHE_MAX_SIZE, 0,
888 if (mp_stack_anon == NULL)
891 /* populate an empty mempool */
892 ret = rte_mempool_populate_anon(mp_stack_anon);
893 printf("%s ret = %d\n", __func__, ret);
897 /* Try to populate when already populated */
898 ret = rte_mempool_populate_anon(mp_stack_anon);
902 /* create a mempool */
903 mp_stack_mempool_iter = rte_mempool_create("test_iter_obj",
906 RTE_MEMPOOL_CACHE_MAX_SIZE, 0,
911 if (mp_stack_mempool_iter == NULL)
914 /* test to initialize mempool objects and memory */
915 nb_objs = rte_mempool_obj_iter(mp_stack_mempool_iter, my_obj_init,
920 nb_mem_chunks = rte_mempool_mem_iter(mp_stack_mempool_iter,
921 test_mp_mem_init, &cb_arg);
922 if (nb_mem_chunks == 0 || cb_arg.ret < 0)
925 /* create a mempool with an external handler */
926 mp_stack = rte_mempool_create_empty("test_stack",
929 RTE_MEMPOOL_CACHE_MAX_SIZE, 0,
932 if (mp_stack == NULL) {
933 printf("cannot allocate mp_stack mempool\n");
936 if (rte_mempool_set_ops_byname(mp_stack, "stack", NULL) < 0) {
937 printf("cannot set stack handler\n");
940 if (rte_mempool_populate_default(mp_stack) < 0) {
941 printf("cannot populate mp_stack mempool\n");
944 rte_mempool_obj_iter(mp_stack, my_obj_init, NULL);
946 /* Create a mempool based on Default handler */
947 printf("Testing %s mempool handler\n", default_pool_ops);
948 default_pool = rte_mempool_create_empty("default_pool",
951 RTE_MEMPOOL_CACHE_MAX_SIZE, 0,
954 if (default_pool == NULL) {
955 printf("cannot allocate default mempool\n");
958 if (rte_mempool_set_ops_byname(default_pool,
959 default_pool_ops, NULL) < 0) {
960 printf("cannot set %s handler\n", default_pool_ops);
963 if (rte_mempool_populate_default(default_pool) < 0) {
964 printf("cannot populate %s mempool\n", default_pool_ops);
967 rte_mempool_obj_iter(default_pool, my_obj_init, NULL);
969 /* retrieve the mempool from its name */
970 if (rte_mempool_lookup("test_nocache") != mp_nocache) {
971 printf("Cannot lookup mempool from its name\n");
975 printf("Walk into mempools:\n");
976 rte_mempool_walk(walk_cb, NULL);
978 rte_mempool_list_dump(stdout);
980 /* basic tests without cache */
981 if (test_mempool_basic(mp_nocache, 0) < 0)
984 /* basic tests with cache */
985 if (test_mempool_basic(mp_cache, 0) < 0)
988 /* basic tests with user-owned cache */
989 if (test_mempool_basic(mp_nocache, 1) < 0)
992 /* more basic tests without cache */
993 if (test_mempool_basic_ex(mp_nocache) < 0)
996 /* mempool operation test based on single producer and single comsumer */
997 if (test_mempool_sp_sc() < 0)
1000 if (test_mempool_creation_with_exceeded_cache_size() < 0)
1003 if (test_mempool_creation_with_invalid_flags() < 0)
1006 if (test_mempool_same_name_twice_creation() < 0)
1009 /* test the stack handler */
1010 if (test_mempool_basic(mp_stack, 1) < 0)
1013 if (test_mempool_basic(default_pool, 1) < 0)
1016 /* test mempool event callbacks */
1017 if (test_mempool_events(rte_mempool_populate_default) < 0)
1019 if (test_mempool_events(rte_mempool_populate_anon) < 0)
1021 if (test_mempool_events_safety() < 0)
1024 /* test NON_IO flag inference */
1025 if (test_mempool_flag_non_io_unset_by_default() < 0)
1027 if (test_mempool_flag_non_io_set_when_no_iova_contig_set() < 0)
1029 if (test_mempool_flag_non_io_unset_when_populated_with_valid_iova() < 0)
1032 rte_mempool_list_dump(stdout);
1037 rte_mempool_free(mp_nocache);
1038 rte_mempool_free(mp_cache);
1039 rte_mempool_free(mp_stack_anon);
1040 rte_mempool_free(mp_stack_mempool_iter);
1041 rte_mempool_free(mp_stack);
1042 rte_mempool_free(default_pool);
1047 REGISTER_TEST_COMMAND(mempool_autotest, test_mempool);