xref: /dpdk/app/test/test_mempool.c (revision 2f1015d8d56d32465cc260faf469950ebb9cf73b)
1a9de470cSBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
2a9de470cSBruce Richardson  * Copyright(c) 2010-2014 Intel Corporation
3a9de470cSBruce Richardson  */
4a9de470cSBruce Richardson 
5a9de470cSBruce Richardson #include <string.h>
6a9de470cSBruce Richardson #include <stdio.h>
7a9de470cSBruce Richardson #include <stdlib.h>
8a9de470cSBruce Richardson #include <stdint.h>
9a9de470cSBruce Richardson #include <inttypes.h>
10a9de470cSBruce Richardson #include <stdarg.h>
11a9de470cSBruce Richardson #include <errno.h>
12a9de470cSBruce Richardson #include <sys/queue.h>
13a9de470cSBruce Richardson 
14a9de470cSBruce Richardson #include <rte_common.h>
1511541c5cSDmitry Kozlyuk #include <rte_eal_paging.h>
16a9de470cSBruce Richardson #include <rte_log.h>
17a9de470cSBruce Richardson #include <rte_debug.h>
18da2b9cb2SDmitry Kozlyuk #include <rte_errno.h>
19a9de470cSBruce Richardson #include <rte_memory.h>
20a9de470cSBruce Richardson #include <rte_launch.h>
21a9de470cSBruce Richardson #include <rte_cycles.h>
22a9de470cSBruce Richardson #include <rte_eal.h>
23a9de470cSBruce Richardson #include <rte_per_lcore.h>
24a9de470cSBruce Richardson #include <rte_lcore.h>
25a9de470cSBruce Richardson #include <rte_branch_prediction.h>
26a9de470cSBruce Richardson #include <rte_mempool.h>
27a9de470cSBruce Richardson #include <rte_spinlock.h>
28a9de470cSBruce Richardson #include <rte_malloc.h>
29a9de470cSBruce Richardson #include <rte_mbuf_pool_ops.h>
30923ceaeaSPallantla Poornima #include <rte_mbuf.h>
31a9de470cSBruce Richardson 
32a9de470cSBruce Richardson #include "test.h"
33a9de470cSBruce Richardson 
34a9de470cSBruce Richardson /*
35a9de470cSBruce Richardson  * Mempool
36a9de470cSBruce Richardson  * =======
37a9de470cSBruce Richardson  *
38a9de470cSBruce Richardson  * Basic tests: done on one core with and without cache:
39a9de470cSBruce Richardson  *
40a9de470cSBruce Richardson  *    - Get one object, put one object
41a9de470cSBruce Richardson  *    - Get two objects, put two objects
42a9de470cSBruce Richardson  *    - Get all objects, test that their content is not modified and
43a9de470cSBruce Richardson  *      put them back in the pool.
44a9de470cSBruce Richardson  */
45a9de470cSBruce Richardson 
46a9de470cSBruce Richardson #define MEMPOOL_ELT_SIZE 2048
47a9de470cSBruce Richardson #define MAX_KEEP 16
48a9de470cSBruce Richardson #define MEMPOOL_SIZE ((rte_lcore_count()*(MAX_KEEP+RTE_MEMPOOL_CACHE_MAX_SIZE))-1)
49a9de470cSBruce Richardson 
50a9de470cSBruce Richardson #define LOG_ERR() printf("test failed at %s():%d\n", __func__, __LINE__)
51a9de470cSBruce Richardson #define RET_ERR() do {							\
52a9de470cSBruce Richardson 		LOG_ERR();						\
53a9de470cSBruce Richardson 		return -1;						\
54a9de470cSBruce Richardson 	} while (0)
55a9de470cSBruce Richardson #define GOTO_ERR(var, label) do {					\
56a9de470cSBruce Richardson 		LOG_ERR();						\
57a9de470cSBruce Richardson 		var = -1;						\
58a9de470cSBruce Richardson 		goto label;						\
59a9de470cSBruce Richardson 	} while (0)
60a9de470cSBruce Richardson 
61a9de470cSBruce Richardson /*
62a9de470cSBruce Richardson  * save the object number in the first 4 bytes of object data. All
63a9de470cSBruce Richardson  * other bytes are set to 0.
64a9de470cSBruce Richardson  */
65a9de470cSBruce Richardson static void
my_obj_init(struct rte_mempool * mp,__rte_unused void * arg,void * obj,unsigned i)66f2fc83b4SThomas Monjalon my_obj_init(struct rte_mempool *mp, __rte_unused void *arg,
67a9de470cSBruce Richardson 	    void *obj, unsigned i)
68a9de470cSBruce Richardson {
69a9de470cSBruce Richardson 	uint32_t *objnum = obj;
70a9de470cSBruce Richardson 
71a9de470cSBruce Richardson 	memset(obj, 0, mp->elt_size);
72a9de470cSBruce Richardson 	*objnum = i;
73a9de470cSBruce Richardson }
74a9de470cSBruce Richardson 
75a9de470cSBruce Richardson /* basic tests (done on one core) */
76a9de470cSBruce Richardson static int
test_mempool_basic(struct rte_mempool * mp,int use_external_cache)77a9de470cSBruce Richardson test_mempool_basic(struct rte_mempool *mp, int use_external_cache)
78a9de470cSBruce Richardson {
79a9de470cSBruce Richardson 	uint32_t *objnum;
80a9de470cSBruce Richardson 	void **objtable;
81a9de470cSBruce Richardson 	void *obj, *obj2;
82a9de470cSBruce Richardson 	char *obj_data;
83a9de470cSBruce Richardson 	int ret = 0;
84a9de470cSBruce Richardson 	unsigned i, j;
85a9de470cSBruce Richardson 	int offset;
86a9de470cSBruce Richardson 	struct rte_mempool_cache *cache;
87a9de470cSBruce Richardson 
88a9de470cSBruce Richardson 	if (use_external_cache) {
89a9de470cSBruce Richardson 		/* Create a user-owned mempool cache. */
90a9de470cSBruce Richardson 		cache = rte_mempool_cache_create(RTE_MEMPOOL_CACHE_MAX_SIZE,
91a9de470cSBruce Richardson 						 SOCKET_ID_ANY);
92a9de470cSBruce Richardson 		if (cache == NULL)
93a9de470cSBruce Richardson 			RET_ERR();
94a9de470cSBruce Richardson 	} else {
95a9de470cSBruce Richardson 		/* May be NULL if cache is disabled. */
96a9de470cSBruce Richardson 		cache = rte_mempool_default_cache(mp, rte_lcore_id());
97a9de470cSBruce Richardson 	}
98a9de470cSBruce Richardson 
99a9de470cSBruce Richardson 	/* dump the mempool status */
100a9de470cSBruce Richardson 	rte_mempool_dump(stdout, mp);
101a9de470cSBruce Richardson 
102a9de470cSBruce Richardson 	printf("get an object\n");
103a9de470cSBruce Richardson 	if (rte_mempool_generic_get(mp, &obj, 1, cache) < 0)
104a9de470cSBruce Richardson 		GOTO_ERR(ret, out);
105a9de470cSBruce Richardson 	rte_mempool_dump(stdout, mp);
106a9de470cSBruce Richardson 
107a9de470cSBruce Richardson 	/* tests that improve coverage */
108a9de470cSBruce Richardson 	printf("get object count\n");
109a9de470cSBruce Richardson 	/* We have to count the extra caches, one in this case. */
110a9de470cSBruce Richardson 	offset = use_external_cache ? 1 * cache->len : 0;
111a9de470cSBruce Richardson 	if (rte_mempool_avail_count(mp) + offset != MEMPOOL_SIZE - 1)
112a9de470cSBruce Richardson 		GOTO_ERR(ret, out);
113a9de470cSBruce Richardson 
114a9de470cSBruce Richardson 	printf("get private data\n");
115a9de470cSBruce Richardson 	if (rte_mempool_get_priv(mp) != (char *)mp +
116d7203661SAndrew Rybchenko 			RTE_MEMPOOL_HEADER_SIZE(mp, mp->cache_size))
117a9de470cSBruce Richardson 		GOTO_ERR(ret, out);
118a9de470cSBruce Richardson 
1195fbc1d49SBruce Richardson #ifndef RTE_EXEC_ENV_FREEBSD /* rte_mem_virt2iova() not supported on bsd */
120a9de470cSBruce Richardson 	printf("get physical address of an object\n");
121a9de470cSBruce Richardson 	if (rte_mempool_virt2iova(obj) != rte_mem_virt2iova(obj))
122a9de470cSBruce Richardson 		GOTO_ERR(ret, out);
123a9de470cSBruce Richardson #endif
124a9de470cSBruce Richardson 
125a9de470cSBruce Richardson 	printf("put the object back\n");
126a9de470cSBruce Richardson 	rte_mempool_generic_put(mp, &obj, 1, cache);
127a9de470cSBruce Richardson 	rte_mempool_dump(stdout, mp);
128a9de470cSBruce Richardson 
129a9de470cSBruce Richardson 	printf("get 2 objects\n");
130a9de470cSBruce Richardson 	if (rte_mempool_generic_get(mp, &obj, 1, cache) < 0)
131a9de470cSBruce Richardson 		GOTO_ERR(ret, out);
132a9de470cSBruce Richardson 	if (rte_mempool_generic_get(mp, &obj2, 1, cache) < 0) {
133a9de470cSBruce Richardson 		rte_mempool_generic_put(mp, &obj, 1, cache);
134a9de470cSBruce Richardson 		GOTO_ERR(ret, out);
135a9de470cSBruce Richardson 	}
136a9de470cSBruce Richardson 	rte_mempool_dump(stdout, mp);
137a9de470cSBruce Richardson 
138a9de470cSBruce Richardson 	printf("put the objects back\n");
139a9de470cSBruce Richardson 	rte_mempool_generic_put(mp, &obj, 1, cache);
140a9de470cSBruce Richardson 	rte_mempool_generic_put(mp, &obj2, 1, cache);
141a9de470cSBruce Richardson 	rte_mempool_dump(stdout, mp);
142a9de470cSBruce Richardson 
143a9de470cSBruce Richardson 	/*
144a9de470cSBruce Richardson 	 * get many objects: we cannot get them all because the cache
145a9de470cSBruce Richardson 	 * on other cores may not be empty.
146a9de470cSBruce Richardson 	 */
147a9de470cSBruce Richardson 	objtable = malloc(MEMPOOL_SIZE * sizeof(void *));
148a9de470cSBruce Richardson 	if (objtable == NULL)
149a9de470cSBruce Richardson 		GOTO_ERR(ret, out);
150a9de470cSBruce Richardson 
151a9de470cSBruce Richardson 	for (i = 0; i < MEMPOOL_SIZE; i++) {
152a9de470cSBruce Richardson 		if (rte_mempool_generic_get(mp, &objtable[i], 1, cache) < 0)
153a9de470cSBruce Richardson 			break;
154a9de470cSBruce Richardson 	}
155a9de470cSBruce Richardson 
156a9de470cSBruce Richardson 	/*
157a9de470cSBruce Richardson 	 * for each object, check that its content was not modified,
158a9de470cSBruce Richardson 	 * and put objects back in pool
159a9de470cSBruce Richardson 	 */
160a9de470cSBruce Richardson 	while (i--) {
161a9de470cSBruce Richardson 		obj = objtable[i];
162a9de470cSBruce Richardson 		obj_data = obj;
163a9de470cSBruce Richardson 		objnum = obj;
164a9de470cSBruce Richardson 		if (*objnum > MEMPOOL_SIZE) {
165a9de470cSBruce Richardson 			printf("bad object number(%d)\n", *objnum);
166a9de470cSBruce Richardson 			ret = -1;
167a9de470cSBruce Richardson 			break;
168a9de470cSBruce Richardson 		}
169a9de470cSBruce Richardson 		for (j = sizeof(*objnum); j < mp->elt_size; j++) {
170a9de470cSBruce Richardson 			if (obj_data[j] != 0)
171a9de470cSBruce Richardson 				ret = -1;
172a9de470cSBruce Richardson 		}
173a9de470cSBruce Richardson 
174a9de470cSBruce Richardson 		rte_mempool_generic_put(mp, &objtable[i], 1, cache);
175a9de470cSBruce Richardson 	}
176a9de470cSBruce Richardson 
177a9de470cSBruce Richardson 	free(objtable);
178a9de470cSBruce Richardson 	if (ret == -1)
179a9de470cSBruce Richardson 		printf("objects were modified!\n");
180a9de470cSBruce Richardson 
181a9de470cSBruce Richardson out:
182a9de470cSBruce Richardson 	if (use_external_cache) {
183a9de470cSBruce Richardson 		rte_mempool_cache_flush(cache, mp);
184a9de470cSBruce Richardson 		rte_mempool_cache_free(cache);
185a9de470cSBruce Richardson 	}
186a9de470cSBruce Richardson 
187a9de470cSBruce Richardson 	return ret;
188a9de470cSBruce Richardson }
189a9de470cSBruce Richardson 
test_mempool_creation_with_exceeded_cache_size(void)190a9de470cSBruce Richardson static int test_mempool_creation_with_exceeded_cache_size(void)
191a9de470cSBruce Richardson {
192a9de470cSBruce Richardson 	struct rte_mempool *mp_cov;
193a9de470cSBruce Richardson 
194a9de470cSBruce Richardson 	mp_cov = rte_mempool_create("test_mempool_cache_too_big",
195a9de470cSBruce Richardson 		MEMPOOL_SIZE,
196a9de470cSBruce Richardson 		MEMPOOL_ELT_SIZE,
197a9de470cSBruce Richardson 		RTE_MEMPOOL_CACHE_MAX_SIZE + 32, 0,
198a9de470cSBruce Richardson 		NULL, NULL,
199a9de470cSBruce Richardson 		my_obj_init, NULL,
200a9de470cSBruce Richardson 		SOCKET_ID_ANY, 0);
201a9de470cSBruce Richardson 
202a9de470cSBruce Richardson 	if (mp_cov != NULL) {
203a9de470cSBruce Richardson 		rte_mempool_free(mp_cov);
204a9de470cSBruce Richardson 		RET_ERR();
205a9de470cSBruce Richardson 	}
206a9de470cSBruce Richardson 
207a9de470cSBruce Richardson 	return 0;
208a9de470cSBruce Richardson }
209a9de470cSBruce Richardson 
test_mempool_creation_with_invalid_flags(void)210afdaa607SDavid Marchand static int test_mempool_creation_with_invalid_flags(void)
211b240af8bSDavid Marchand {
212b240af8bSDavid Marchand 	struct rte_mempool *mp_cov;
213b240af8bSDavid Marchand 
214afdaa607SDavid Marchand 	mp_cov = rte_mempool_create("test_mempool_invalid_flags", MEMPOOL_SIZE,
215b240af8bSDavid Marchand 		MEMPOOL_ELT_SIZE, 0, 0,
216b240af8bSDavid Marchand 		NULL, NULL,
217b240af8bSDavid Marchand 		NULL, NULL,
218afdaa607SDavid Marchand 		SOCKET_ID_ANY, ~RTE_MEMPOOL_VALID_USER_FLAGS);
219b240af8bSDavid Marchand 
220b240af8bSDavid Marchand 	if (mp_cov != NULL) {
221b240af8bSDavid Marchand 		rte_mempool_free(mp_cov);
222b240af8bSDavid Marchand 		RET_ERR();
223b240af8bSDavid Marchand 	}
224b240af8bSDavid Marchand 
225b240af8bSDavid Marchand 	return 0;
226b240af8bSDavid Marchand }
227b240af8bSDavid Marchand 
228a9de470cSBruce Richardson static struct rte_mempool *mp_spsc;
229a9de470cSBruce Richardson static rte_spinlock_t scsp_spinlock;
230a9de470cSBruce Richardson static void *scsp_obj_table[MAX_KEEP];
231a9de470cSBruce Richardson 
232a9de470cSBruce Richardson /*
233a9de470cSBruce Richardson  * single producer function
234a9de470cSBruce Richardson  */
test_mempool_single_producer(void)235a9de470cSBruce Richardson static int test_mempool_single_producer(void)
236a9de470cSBruce Richardson {
237a9de470cSBruce Richardson 	unsigned int i;
238a9de470cSBruce Richardson 	void *obj = NULL;
239a9de470cSBruce Richardson 	uint64_t start_cycles, end_cycles;
240a9de470cSBruce Richardson 	uint64_t duration = rte_get_timer_hz() / 4;
241a9de470cSBruce Richardson 
242a9de470cSBruce Richardson 	start_cycles = rte_get_timer_cycles();
243a9de470cSBruce Richardson 	while (1) {
244a9de470cSBruce Richardson 		end_cycles = rte_get_timer_cycles();
245a9de470cSBruce Richardson 		/* duration uses up, stop producing */
246a9de470cSBruce Richardson 		if (start_cycles + duration < end_cycles)
247a9de470cSBruce Richardson 			break;
248a9de470cSBruce Richardson 		rte_spinlock_lock(&scsp_spinlock);
249a9de470cSBruce Richardson 		for (i = 0; i < MAX_KEEP; i ++) {
250a9de470cSBruce Richardson 			if (NULL != scsp_obj_table[i]) {
251a9de470cSBruce Richardson 				obj = scsp_obj_table[i];
252a9de470cSBruce Richardson 				break;
253a9de470cSBruce Richardson 			}
254a9de470cSBruce Richardson 		}
255a9de470cSBruce Richardson 		rte_spinlock_unlock(&scsp_spinlock);
256a9de470cSBruce Richardson 		if (i >= MAX_KEEP) {
257a9de470cSBruce Richardson 			continue;
258a9de470cSBruce Richardson 		}
259a9de470cSBruce Richardson 		if (rte_mempool_from_obj(obj) != mp_spsc) {
260a9de470cSBruce Richardson 			printf("obj not owned by this mempool\n");
261a9de470cSBruce Richardson 			RET_ERR();
262a9de470cSBruce Richardson 		}
263a9de470cSBruce Richardson 		rte_mempool_put(mp_spsc, obj);
264a9de470cSBruce Richardson 		rte_spinlock_lock(&scsp_spinlock);
265a9de470cSBruce Richardson 		scsp_obj_table[i] = NULL;
266a9de470cSBruce Richardson 		rte_spinlock_unlock(&scsp_spinlock);
267a9de470cSBruce Richardson 	}
268a9de470cSBruce Richardson 
269a9de470cSBruce Richardson 	return 0;
270a9de470cSBruce Richardson }
271a9de470cSBruce Richardson 
272a9de470cSBruce Richardson /*
273a9de470cSBruce Richardson  * single consumer function
274a9de470cSBruce Richardson  */
test_mempool_single_consumer(void)275a9de470cSBruce Richardson static int test_mempool_single_consumer(void)
276a9de470cSBruce Richardson {
277a9de470cSBruce Richardson 	unsigned int i;
278a9de470cSBruce Richardson 	void * obj;
279a9de470cSBruce Richardson 	uint64_t start_cycles, end_cycles;
280a9de470cSBruce Richardson 	uint64_t duration = rte_get_timer_hz() / 8;
281a9de470cSBruce Richardson 
282a9de470cSBruce Richardson 	start_cycles = rte_get_timer_cycles();
283a9de470cSBruce Richardson 	while (1) {
284a9de470cSBruce Richardson 		end_cycles = rte_get_timer_cycles();
285a9de470cSBruce Richardson 		/* duration uses up, stop consuming */
286a9de470cSBruce Richardson 		if (start_cycles + duration < end_cycles)
287a9de470cSBruce Richardson 			break;
288a9de470cSBruce Richardson 		rte_spinlock_lock(&scsp_spinlock);
289a9de470cSBruce Richardson 		for (i = 0; i < MAX_KEEP; i ++) {
290a9de470cSBruce Richardson 			if (NULL == scsp_obj_table[i])
291a9de470cSBruce Richardson 				break;
292a9de470cSBruce Richardson 		}
293a9de470cSBruce Richardson 		rte_spinlock_unlock(&scsp_spinlock);
294a9de470cSBruce Richardson 		if (i >= MAX_KEEP)
295a9de470cSBruce Richardson 			continue;
296a9de470cSBruce Richardson 		if (rte_mempool_get(mp_spsc, &obj) < 0)
297a9de470cSBruce Richardson 			break;
298a9de470cSBruce Richardson 		rte_spinlock_lock(&scsp_spinlock);
299a9de470cSBruce Richardson 		scsp_obj_table[i] = obj;
300a9de470cSBruce Richardson 		rte_spinlock_unlock(&scsp_spinlock);
301a9de470cSBruce Richardson 	}
302a9de470cSBruce Richardson 
303a9de470cSBruce Richardson 	return 0;
304a9de470cSBruce Richardson }
305a9de470cSBruce Richardson 
306a9de470cSBruce Richardson /*
3077be78d02SJosh Soref  * test function for mempool test based on single consumer and single producer,
308a9de470cSBruce Richardson  * can run on one lcore only
309a9de470cSBruce Richardson  */
310a9de470cSBruce Richardson static int
test_mempool_launch_single_consumer(__rte_unused void * arg)311f2fc83b4SThomas Monjalon test_mempool_launch_single_consumer(__rte_unused void *arg)
312a9de470cSBruce Richardson {
313a9de470cSBruce Richardson 	return test_mempool_single_consumer();
314a9de470cSBruce Richardson }
315a9de470cSBruce Richardson 
316a9de470cSBruce Richardson static void
my_mp_init(struct rte_mempool * mp,__rte_unused void * arg)317f2fc83b4SThomas Monjalon my_mp_init(struct rte_mempool *mp, __rte_unused void *arg)
318a9de470cSBruce Richardson {
319a9de470cSBruce Richardson 	printf("mempool name is %s\n", mp->name);
320a9de470cSBruce Richardson 	/* nothing to be implemented here*/
321a9de470cSBruce Richardson 	return ;
322a9de470cSBruce Richardson }
323a9de470cSBruce Richardson 
324a9de470cSBruce Richardson /*
3257be78d02SJosh Soref  * it tests the mempool operations based on single producer and single consumer
326a9de470cSBruce Richardson  */
327a9de470cSBruce Richardson static int
test_mempool_sp_sc(void)328a9de470cSBruce Richardson test_mempool_sp_sc(void)
329a9de470cSBruce Richardson {
330a9de470cSBruce Richardson 	int ret = 0;
331a9de470cSBruce Richardson 	unsigned lcore_id = rte_lcore_id();
332a9de470cSBruce Richardson 	unsigned lcore_next;
333a9de470cSBruce Richardson 
334a9de470cSBruce Richardson 	/* create a mempool with single producer/consumer ring */
335a9de470cSBruce Richardson 	if (mp_spsc == NULL) {
336a9de470cSBruce Richardson 		mp_spsc = rte_mempool_create("test_mempool_sp_sc", MEMPOOL_SIZE,
337a9de470cSBruce Richardson 			MEMPOOL_ELT_SIZE, 0, 0,
338a9de470cSBruce Richardson 			my_mp_init, NULL,
339a9de470cSBruce Richardson 			my_obj_init, NULL,
340a9de470cSBruce Richardson 			SOCKET_ID_ANY,
341c47d7b90SAndrew Rybchenko 			RTE_MEMPOOL_F_NO_CACHE_ALIGN | RTE_MEMPOOL_F_SP_PUT |
342c47d7b90SAndrew Rybchenko 			RTE_MEMPOOL_F_SC_GET);
343a9de470cSBruce Richardson 		if (mp_spsc == NULL)
344a9de470cSBruce Richardson 			RET_ERR();
345a9de470cSBruce Richardson 	}
346a9de470cSBruce Richardson 	if (rte_mempool_lookup("test_mempool_sp_sc") != mp_spsc) {
347a9de470cSBruce Richardson 		printf("Cannot lookup mempool from its name\n");
348a9de470cSBruce Richardson 		ret = -1;
349a9de470cSBruce Richardson 		goto err;
350a9de470cSBruce Richardson 	}
351a9de470cSBruce Richardson 	lcore_next = rte_get_next_lcore(lcore_id, 0, 1);
352a9de470cSBruce Richardson 	if (lcore_next >= RTE_MAX_LCORE) {
353a9de470cSBruce Richardson 		ret = -1;
354a9de470cSBruce Richardson 		goto err;
355a9de470cSBruce Richardson 	}
356a9de470cSBruce Richardson 	if (rte_eal_lcore_role(lcore_next) != ROLE_RTE) {
357a9de470cSBruce Richardson 		ret = -1;
358a9de470cSBruce Richardson 		goto err;
359a9de470cSBruce Richardson 	}
360a9de470cSBruce Richardson 	rte_spinlock_init(&scsp_spinlock);
361a9de470cSBruce Richardson 	memset(scsp_obj_table, 0, sizeof(scsp_obj_table));
362a9de470cSBruce Richardson 	rte_eal_remote_launch(test_mempool_launch_single_consumer, NULL,
363a9de470cSBruce Richardson 		lcore_next);
364a9de470cSBruce Richardson 	if (test_mempool_single_producer() < 0)
365a9de470cSBruce Richardson 		ret = -1;
366a9de470cSBruce Richardson 
367a9de470cSBruce Richardson 	if (rte_eal_wait_lcore(lcore_next) < 0)
368a9de470cSBruce Richardson 		ret = -1;
369a9de470cSBruce Richardson 
370a9de470cSBruce Richardson err:
371a9de470cSBruce Richardson 	rte_mempool_free(mp_spsc);
372a9de470cSBruce Richardson 	mp_spsc = NULL;
373a9de470cSBruce Richardson 
374a9de470cSBruce Richardson 	return ret;
375a9de470cSBruce Richardson }
376a9de470cSBruce Richardson 
377a9de470cSBruce Richardson /*
378a9de470cSBruce Richardson  * it tests some more basic of mempool
379a9de470cSBruce Richardson  */
380a9de470cSBruce Richardson static int
test_mempool_basic_ex(struct rte_mempool * mp)381a9de470cSBruce Richardson test_mempool_basic_ex(struct rte_mempool *mp)
382a9de470cSBruce Richardson {
383a9de470cSBruce Richardson 	unsigned i;
384a9de470cSBruce Richardson 	void **obj;
385a9de470cSBruce Richardson 	void *err_obj;
386a9de470cSBruce Richardson 	int ret = -1;
387a9de470cSBruce Richardson 
388a9de470cSBruce Richardson 	if (mp == NULL)
389a9de470cSBruce Richardson 		return ret;
390a9de470cSBruce Richardson 
391a9de470cSBruce Richardson 	obj = rte_calloc("test_mempool_basic_ex", MEMPOOL_SIZE,
392a9de470cSBruce Richardson 		sizeof(void *), 0);
393a9de470cSBruce Richardson 	if (obj == NULL) {
394a9de470cSBruce Richardson 		printf("test_mempool_basic_ex fail to rte_malloc\n");
395a9de470cSBruce Richardson 		return ret;
396a9de470cSBruce Richardson 	}
397a9de470cSBruce Richardson 	printf("test_mempool_basic_ex now mempool (%s) has %u free entries\n",
398a9de470cSBruce Richardson 		mp->name, rte_mempool_in_use_count(mp));
399a9de470cSBruce Richardson 	if (rte_mempool_full(mp) != 1) {
400a9de470cSBruce Richardson 		printf("test_mempool_basic_ex the mempool should be full\n");
401a9de470cSBruce Richardson 		goto fail_mp_basic_ex;
402a9de470cSBruce Richardson 	}
403a9de470cSBruce Richardson 
404a9de470cSBruce Richardson 	for (i = 0; i < MEMPOOL_SIZE; i ++) {
405a9de470cSBruce Richardson 		if (rte_mempool_get(mp, &obj[i]) < 0) {
406a9de470cSBruce Richardson 			printf("test_mp_basic_ex fail to get object for [%u]\n",
407a9de470cSBruce Richardson 				i);
408a9de470cSBruce Richardson 			goto fail_mp_basic_ex;
409a9de470cSBruce Richardson 		}
410a9de470cSBruce Richardson 	}
411a9de470cSBruce Richardson 	if (rte_mempool_get(mp, &err_obj) == 0) {
412a9de470cSBruce Richardson 		printf("test_mempool_basic_ex get an impossible obj\n");
413a9de470cSBruce Richardson 		goto fail_mp_basic_ex;
414a9de470cSBruce Richardson 	}
415a9de470cSBruce Richardson 	printf("number: %u\n", i);
416a9de470cSBruce Richardson 	if (rte_mempool_empty(mp) != 1) {
417a9de470cSBruce Richardson 		printf("test_mempool_basic_ex the mempool should be empty\n");
418a9de470cSBruce Richardson 		goto fail_mp_basic_ex;
419a9de470cSBruce Richardson 	}
420a9de470cSBruce Richardson 
421a9de470cSBruce Richardson 	for (i = 0; i < MEMPOOL_SIZE; i++)
422a9de470cSBruce Richardson 		rte_mempool_put(mp, obj[i]);
423a9de470cSBruce Richardson 
424a9de470cSBruce Richardson 	if (rte_mempool_full(mp) != 1) {
425a9de470cSBruce Richardson 		printf("test_mempool_basic_ex the mempool should be full\n");
426a9de470cSBruce Richardson 		goto fail_mp_basic_ex;
427a9de470cSBruce Richardson 	}
428a9de470cSBruce Richardson 
429a9de470cSBruce Richardson 	ret = 0;
430a9de470cSBruce Richardson 
431a9de470cSBruce Richardson fail_mp_basic_ex:
432a9de470cSBruce Richardson 	if (obj != NULL)
433a9de470cSBruce Richardson 		rte_free((void *)obj);
434a9de470cSBruce Richardson 
435a9de470cSBruce Richardson 	return ret;
436a9de470cSBruce Richardson }
437a9de470cSBruce Richardson 
438a9de470cSBruce Richardson static int
test_mempool_same_name_twice_creation(void)439a9de470cSBruce Richardson test_mempool_same_name_twice_creation(void)
440a9de470cSBruce Richardson {
441a9de470cSBruce Richardson 	struct rte_mempool *mp_tc, *mp_tc2;
442a9de470cSBruce Richardson 
443a9de470cSBruce Richardson 	mp_tc = rte_mempool_create("test_mempool_same_name", MEMPOOL_SIZE,
444a9de470cSBruce Richardson 		MEMPOOL_ELT_SIZE, 0, 0,
445a9de470cSBruce Richardson 		NULL, NULL,
446a9de470cSBruce Richardson 		NULL, NULL,
447a9de470cSBruce Richardson 		SOCKET_ID_ANY, 0);
448a9de470cSBruce Richardson 
449a9de470cSBruce Richardson 	if (mp_tc == NULL)
450a9de470cSBruce Richardson 		RET_ERR();
451a9de470cSBruce Richardson 
452a9de470cSBruce Richardson 	mp_tc2 = rte_mempool_create("test_mempool_same_name", MEMPOOL_SIZE,
453a9de470cSBruce Richardson 		MEMPOOL_ELT_SIZE, 0, 0,
454a9de470cSBruce Richardson 		NULL, NULL,
455a9de470cSBruce Richardson 		NULL, NULL,
456a9de470cSBruce Richardson 		SOCKET_ID_ANY, 0);
457a9de470cSBruce Richardson 
458a9de470cSBruce Richardson 	if (mp_tc2 != NULL) {
459a9de470cSBruce Richardson 		rte_mempool_free(mp_tc);
460a9de470cSBruce Richardson 		rte_mempool_free(mp_tc2);
461a9de470cSBruce Richardson 		RET_ERR();
462a9de470cSBruce Richardson 	}
463a9de470cSBruce Richardson 
464a9de470cSBruce Richardson 	rte_mempool_free(mp_tc);
465a9de470cSBruce Richardson 	return 0;
466a9de470cSBruce Richardson }
467a9de470cSBruce Richardson 
468a9de470cSBruce Richardson static void
walk_cb(struct rte_mempool * mp,void * userdata __rte_unused)469a9de470cSBruce Richardson walk_cb(struct rte_mempool *mp, void *userdata __rte_unused)
470a9de470cSBruce Richardson {
471a9de470cSBruce Richardson 	printf("\t%s\n", mp->name);
472a9de470cSBruce Richardson }
473a9de470cSBruce Richardson 
474923ceaeaSPallantla Poornima struct mp_data {
475923ceaeaSPallantla Poornima 	int16_t ret;
476923ceaeaSPallantla Poornima };
477923ceaeaSPallantla Poornima 
478923ceaeaSPallantla Poornima static void
test_mp_mem_init(struct rte_mempool * mp,__rte_unused void * opaque,__rte_unused struct rte_mempool_memhdr * memhdr,__rte_unused unsigned int mem_idx)479923ceaeaSPallantla Poornima test_mp_mem_init(struct rte_mempool *mp,
480923ceaeaSPallantla Poornima 		__rte_unused void *opaque,
481923ceaeaSPallantla Poornima 		__rte_unused struct rte_mempool_memhdr *memhdr,
482923ceaeaSPallantla Poornima 		__rte_unused unsigned int mem_idx)
483923ceaeaSPallantla Poornima {
484923ceaeaSPallantla Poornima 	struct mp_data *data = opaque;
485923ceaeaSPallantla Poornima 
486923ceaeaSPallantla Poornima 	if (mp == NULL) {
487923ceaeaSPallantla Poornima 		data->ret = -1;
488923ceaeaSPallantla Poornima 		return;
489923ceaeaSPallantla Poornima 	}
490923ceaeaSPallantla Poornima 	/* nothing to be implemented here*/
491923ceaeaSPallantla Poornima 	data->ret = 0;
492923ceaeaSPallantla Poornima }
493923ceaeaSPallantla Poornima 
494da2b9cb2SDmitry Kozlyuk struct test_mempool_events_data {
495da2b9cb2SDmitry Kozlyuk 	struct rte_mempool *mp;
496da2b9cb2SDmitry Kozlyuk 	enum rte_mempool_event event;
497da2b9cb2SDmitry Kozlyuk 	bool invoked;
498da2b9cb2SDmitry Kozlyuk };
499da2b9cb2SDmitry Kozlyuk 
500da2b9cb2SDmitry Kozlyuk static void
test_mempool_events_cb(enum rte_mempool_event event,struct rte_mempool * mp,void * user_data)501da2b9cb2SDmitry Kozlyuk test_mempool_events_cb(enum rte_mempool_event event,
502da2b9cb2SDmitry Kozlyuk 		       struct rte_mempool *mp, void *user_data)
503da2b9cb2SDmitry Kozlyuk {
504da2b9cb2SDmitry Kozlyuk 	struct test_mempool_events_data *data = user_data;
505da2b9cb2SDmitry Kozlyuk 
506da2b9cb2SDmitry Kozlyuk 	data->mp = mp;
507da2b9cb2SDmitry Kozlyuk 	data->event = event;
508da2b9cb2SDmitry Kozlyuk 	data->invoked = true;
509da2b9cb2SDmitry Kozlyuk }
510da2b9cb2SDmitry Kozlyuk 
511da2b9cb2SDmitry Kozlyuk static int
test_mempool_events(int (* populate)(struct rte_mempool * mp))512da2b9cb2SDmitry Kozlyuk test_mempool_events(int (*populate)(struct rte_mempool *mp))
513da2b9cb2SDmitry Kozlyuk {
514da2b9cb2SDmitry Kozlyuk #pragma push_macro("RTE_TEST_TRACE_FAILURE")
515da2b9cb2SDmitry Kozlyuk #undef RTE_TEST_TRACE_FAILURE
516da2b9cb2SDmitry Kozlyuk #define RTE_TEST_TRACE_FAILURE(...) do { goto fail; } while (0)
517da2b9cb2SDmitry Kozlyuk 
5185b08ac4eSDmitry Kozlyuk 	static const size_t callback_num = 3;
5195b08ac4eSDmitry Kozlyuk 	static const size_t mempool_num = 2;
5205b08ac4eSDmitry Kozlyuk 	static const unsigned int mempool_elt_size = 64;
5215b08ac4eSDmitry Kozlyuk 	static const unsigned int mempool_size = 64;
522da2b9cb2SDmitry Kozlyuk 
5235b08ac4eSDmitry Kozlyuk 	struct test_mempool_events_data data[callback_num];
5245b08ac4eSDmitry Kozlyuk 	struct rte_mempool *mp[mempool_num], *freed;
525da2b9cb2SDmitry Kozlyuk 	char name[RTE_MEMPOOL_NAMESIZE];
526da2b9cb2SDmitry Kozlyuk 	size_t i, j;
527da2b9cb2SDmitry Kozlyuk 	int ret;
528da2b9cb2SDmitry Kozlyuk 
529da2b9cb2SDmitry Kozlyuk 	memset(mp, 0, sizeof(mp));
5305b08ac4eSDmitry Kozlyuk 	for (i = 0; i < callback_num; i++) {
531da2b9cb2SDmitry Kozlyuk 		ret = rte_mempool_event_callback_register
532da2b9cb2SDmitry Kozlyuk 				(test_mempool_events_cb, &data[i]);
533da2b9cb2SDmitry Kozlyuk 		RTE_TEST_ASSERT_EQUAL(ret, 0, "Failed to register the callback %zu: %s",
534da2b9cb2SDmitry Kozlyuk 				      i, rte_strerror(rte_errno));
535da2b9cb2SDmitry Kozlyuk 	}
536da2b9cb2SDmitry Kozlyuk 	ret = rte_mempool_event_callback_unregister(test_mempool_events_cb, mp);
537da2b9cb2SDmitry Kozlyuk 	RTE_TEST_ASSERT_NOT_EQUAL(ret, 0, "Unregistered a non-registered callback");
538da2b9cb2SDmitry Kozlyuk 	/* NULL argument has no special meaning in this API. */
539da2b9cb2SDmitry Kozlyuk 	ret = rte_mempool_event_callback_unregister(test_mempool_events_cb,
540da2b9cb2SDmitry Kozlyuk 						    NULL);
541da2b9cb2SDmitry Kozlyuk 	RTE_TEST_ASSERT_NOT_EQUAL(ret, 0, "Unregistered a non-registered callback with NULL argument");
542da2b9cb2SDmitry Kozlyuk 
543da2b9cb2SDmitry Kozlyuk 	/* Create mempool 0 that will be observed by all callbacks. */
544da2b9cb2SDmitry Kozlyuk 	memset(&data, 0, sizeof(data));
545da2b9cb2SDmitry Kozlyuk 	strcpy(name, "empty0");
5465b08ac4eSDmitry Kozlyuk 	mp[0] = rte_mempool_create_empty(name, mempool_size,
5475b08ac4eSDmitry Kozlyuk 					 mempool_elt_size, 0, 0,
548da2b9cb2SDmitry Kozlyuk 					 SOCKET_ID_ANY, 0);
549da2b9cb2SDmitry Kozlyuk 	RTE_TEST_ASSERT_NOT_NULL(mp[0], "Cannot create mempool %s: %s",
550da2b9cb2SDmitry Kozlyuk 				 name, rte_strerror(rte_errno));
5515b08ac4eSDmitry Kozlyuk 	for (j = 0; j < callback_num; j++)
552da2b9cb2SDmitry Kozlyuk 		RTE_TEST_ASSERT_EQUAL(data[j].invoked, false,
553da2b9cb2SDmitry Kozlyuk 				      "Callback %zu invoked on %s mempool creation",
554da2b9cb2SDmitry Kozlyuk 				      j, name);
555da2b9cb2SDmitry Kozlyuk 
556da2b9cb2SDmitry Kozlyuk 	rte_mempool_set_ops_byname(mp[0], rte_mbuf_best_mempool_ops(), NULL);
557da2b9cb2SDmitry Kozlyuk 	ret = populate(mp[0]);
558da2b9cb2SDmitry Kozlyuk 	RTE_TEST_ASSERT_EQUAL(ret, (int)mp[0]->size, "Failed to populate mempool %s: %s",
559da2b9cb2SDmitry Kozlyuk 			      name, rte_strerror(-ret));
5605b08ac4eSDmitry Kozlyuk 	for (j = 0; j < callback_num; j++) {
561da2b9cb2SDmitry Kozlyuk 		RTE_TEST_ASSERT_EQUAL(data[j].invoked, true,
562da2b9cb2SDmitry Kozlyuk 					"Callback %zu not invoked on mempool %s population",
563da2b9cb2SDmitry Kozlyuk 					j, name);
564da2b9cb2SDmitry Kozlyuk 		RTE_TEST_ASSERT_EQUAL(data[j].event,
565da2b9cb2SDmitry Kozlyuk 					RTE_MEMPOOL_EVENT_READY,
566da2b9cb2SDmitry Kozlyuk 					"Wrong callback invoked, expected READY");
567da2b9cb2SDmitry Kozlyuk 		RTE_TEST_ASSERT_EQUAL(data[j].mp, mp[0],
568da2b9cb2SDmitry Kozlyuk 					"Callback %zu invoked for a wrong mempool instead of %s",
569da2b9cb2SDmitry Kozlyuk 					j, name);
570da2b9cb2SDmitry Kozlyuk 	}
571da2b9cb2SDmitry Kozlyuk 
572da2b9cb2SDmitry Kozlyuk 	/* Check that unregistered callback 0 observes no events. */
573da2b9cb2SDmitry Kozlyuk 	ret = rte_mempool_event_callback_unregister(test_mempool_events_cb,
574da2b9cb2SDmitry Kozlyuk 						    &data[0]);
575da2b9cb2SDmitry Kozlyuk 	RTE_TEST_ASSERT_EQUAL(ret, 0, "Failed to unregister callback 0: %s",
576da2b9cb2SDmitry Kozlyuk 			      rte_strerror(rte_errno));
577da2b9cb2SDmitry Kozlyuk 	memset(&data, 0, sizeof(data));
578da2b9cb2SDmitry Kozlyuk 	strcpy(name, "empty1");
5795b08ac4eSDmitry Kozlyuk 	mp[1] = rte_mempool_create_empty(name, mempool_size,
5805b08ac4eSDmitry Kozlyuk 					 mempool_elt_size, 0, 0,
581da2b9cb2SDmitry Kozlyuk 					 SOCKET_ID_ANY, 0);
582da2b9cb2SDmitry Kozlyuk 	RTE_TEST_ASSERT_NOT_NULL(mp[1], "Cannot create mempool %s: %s",
583da2b9cb2SDmitry Kozlyuk 				 name, rte_strerror(rte_errno));
584da2b9cb2SDmitry Kozlyuk 	rte_mempool_set_ops_byname(mp[1], rte_mbuf_best_mempool_ops(), NULL);
585da2b9cb2SDmitry Kozlyuk 	ret = populate(mp[1]);
586da2b9cb2SDmitry Kozlyuk 	RTE_TEST_ASSERT_EQUAL(ret, (int)mp[1]->size, "Failed to populate mempool %s: %s",
587da2b9cb2SDmitry Kozlyuk 			      name, rte_strerror(-ret));
588da2b9cb2SDmitry Kozlyuk 	RTE_TEST_ASSERT_EQUAL(data[0].invoked, false,
589da2b9cb2SDmitry Kozlyuk 			      "Unregistered callback 0 invoked on %s mempool populaton",
590da2b9cb2SDmitry Kozlyuk 			      name);
591da2b9cb2SDmitry Kozlyuk 
5925b08ac4eSDmitry Kozlyuk 	for (i = 0; i < mempool_num; i++) {
593da2b9cb2SDmitry Kozlyuk 		memset(&data, 0, sizeof(data));
594da2b9cb2SDmitry Kozlyuk 		sprintf(name, "empty%zu", i);
595da2b9cb2SDmitry Kozlyuk 		rte_mempool_free(mp[i]);
596da2b9cb2SDmitry Kozlyuk 		/*
597da2b9cb2SDmitry Kozlyuk 		 * Save pointer to check that it was passed to the callback,
598da2b9cb2SDmitry Kozlyuk 		 * but put NULL into the array in case cleanup is called early.
599da2b9cb2SDmitry Kozlyuk 		 */
600da2b9cb2SDmitry Kozlyuk 		freed = mp[i];
601da2b9cb2SDmitry Kozlyuk 		mp[i] = NULL;
6025b08ac4eSDmitry Kozlyuk 		for (j = 1; j < callback_num; j++) {
603da2b9cb2SDmitry Kozlyuk 			RTE_TEST_ASSERT_EQUAL(data[j].invoked, true,
604da2b9cb2SDmitry Kozlyuk 					      "Callback %zu not invoked on mempool %s destruction",
605da2b9cb2SDmitry Kozlyuk 					      j, name);
606da2b9cb2SDmitry Kozlyuk 			RTE_TEST_ASSERT_EQUAL(data[j].event,
607da2b9cb2SDmitry Kozlyuk 					      RTE_MEMPOOL_EVENT_DESTROY,
608da2b9cb2SDmitry Kozlyuk 					      "Wrong callback invoked, expected DESTROY");
609da2b9cb2SDmitry Kozlyuk 			RTE_TEST_ASSERT_EQUAL(data[j].mp, freed,
610da2b9cb2SDmitry Kozlyuk 					      "Callback %zu invoked for a wrong mempool instead of %s",
611da2b9cb2SDmitry Kozlyuk 					      j, name);
612da2b9cb2SDmitry Kozlyuk 		}
613da2b9cb2SDmitry Kozlyuk 		RTE_TEST_ASSERT_EQUAL(data[0].invoked, false,
614da2b9cb2SDmitry Kozlyuk 				      "Unregistered callback 0 invoked on %s mempool destruction",
615da2b9cb2SDmitry Kozlyuk 				      name);
616da2b9cb2SDmitry Kozlyuk 	}
617da2b9cb2SDmitry Kozlyuk 
6185b08ac4eSDmitry Kozlyuk 	for (j = 1; j < callback_num; j++) {
619da2b9cb2SDmitry Kozlyuk 		ret = rte_mempool_event_callback_unregister
620da2b9cb2SDmitry Kozlyuk 					(test_mempool_events_cb, &data[j]);
621da2b9cb2SDmitry Kozlyuk 		RTE_TEST_ASSERT_EQUAL(ret, 0, "Failed to unregister the callback %zu: %s",
622da2b9cb2SDmitry Kozlyuk 				      j, rte_strerror(rte_errno));
623da2b9cb2SDmitry Kozlyuk 	}
624da2b9cb2SDmitry Kozlyuk 	return TEST_SUCCESS;
625da2b9cb2SDmitry Kozlyuk 
626da2b9cb2SDmitry Kozlyuk fail:
6275b08ac4eSDmitry Kozlyuk 	for (j = 0; j < callback_num; j++)
628da2b9cb2SDmitry Kozlyuk 		rte_mempool_event_callback_unregister
629da2b9cb2SDmitry Kozlyuk 					(test_mempool_events_cb, &data[j]);
6305b08ac4eSDmitry Kozlyuk 	for (i = 0; i < mempool_num; i++)
631da2b9cb2SDmitry Kozlyuk 		rte_mempool_free(mp[i]);
632da2b9cb2SDmitry Kozlyuk 	return TEST_FAILED;
633da2b9cb2SDmitry Kozlyuk 
634da2b9cb2SDmitry Kozlyuk #pragma pop_macro("RTE_TEST_TRACE_FAILURE")
635da2b9cb2SDmitry Kozlyuk }
636da2b9cb2SDmitry Kozlyuk 
637da2b9cb2SDmitry Kozlyuk struct test_mempool_events_safety_data {
638da2b9cb2SDmitry Kozlyuk 	bool invoked;
639da2b9cb2SDmitry Kozlyuk 	int (*api_func)(rte_mempool_event_callback *func, void *user_data);
640da2b9cb2SDmitry Kozlyuk 	rte_mempool_event_callback *cb_func;
641da2b9cb2SDmitry Kozlyuk 	void *cb_user_data;
642da2b9cb2SDmitry Kozlyuk 	int ret;
643da2b9cb2SDmitry Kozlyuk };
644da2b9cb2SDmitry Kozlyuk 
645da2b9cb2SDmitry Kozlyuk static void
test_mempool_events_safety_cb(enum rte_mempool_event event,struct rte_mempool * mp,void * user_data)646da2b9cb2SDmitry Kozlyuk test_mempool_events_safety_cb(enum rte_mempool_event event,
647da2b9cb2SDmitry Kozlyuk 			      struct rte_mempool *mp, void *user_data)
648da2b9cb2SDmitry Kozlyuk {
649da2b9cb2SDmitry Kozlyuk 	struct test_mempool_events_safety_data *data = user_data;
650da2b9cb2SDmitry Kozlyuk 
651da2b9cb2SDmitry Kozlyuk 	RTE_SET_USED(event);
652da2b9cb2SDmitry Kozlyuk 	RTE_SET_USED(mp);
653da2b9cb2SDmitry Kozlyuk 	data->invoked = true;
654da2b9cb2SDmitry Kozlyuk 	data->ret = data->api_func(data->cb_func, data->cb_user_data);
655da2b9cb2SDmitry Kozlyuk }
656da2b9cb2SDmitry Kozlyuk 
657da2b9cb2SDmitry Kozlyuk static int
test_mempool_events_safety(void)658da2b9cb2SDmitry Kozlyuk test_mempool_events_safety(void)
659da2b9cb2SDmitry Kozlyuk {
660da2b9cb2SDmitry Kozlyuk #pragma push_macro("RTE_TEST_TRACE_FAILURE")
661da2b9cb2SDmitry Kozlyuk #undef RTE_TEST_TRACE_FAILURE
662da2b9cb2SDmitry Kozlyuk #define RTE_TEST_TRACE_FAILURE(...) do { \
663da2b9cb2SDmitry Kozlyuk 		ret = TEST_FAILED; \
664da2b9cb2SDmitry Kozlyuk 		goto exit; \
665da2b9cb2SDmitry Kozlyuk 	} while (0)
666da2b9cb2SDmitry Kozlyuk 
667da2b9cb2SDmitry Kozlyuk 	struct test_mempool_events_data data;
668da2b9cb2SDmitry Kozlyuk 	struct test_mempool_events_safety_data sdata[2];
669da2b9cb2SDmitry Kozlyuk 	struct rte_mempool *mp;
670da2b9cb2SDmitry Kozlyuk 	size_t i;
671da2b9cb2SDmitry Kozlyuk 	int ret;
672da2b9cb2SDmitry Kozlyuk 
673da2b9cb2SDmitry Kozlyuk 	/* removes itself */
674da2b9cb2SDmitry Kozlyuk 	sdata[0].api_func = rte_mempool_event_callback_unregister;
675da2b9cb2SDmitry Kozlyuk 	sdata[0].cb_func = test_mempool_events_safety_cb;
676da2b9cb2SDmitry Kozlyuk 	sdata[0].cb_user_data = &sdata[0];
677da2b9cb2SDmitry Kozlyuk 	sdata[0].ret = -1;
678da2b9cb2SDmitry Kozlyuk 	rte_mempool_event_callback_register(test_mempool_events_safety_cb,
679da2b9cb2SDmitry Kozlyuk 					    &sdata[0]);
680da2b9cb2SDmitry Kozlyuk 	/* inserts a callback after itself */
681da2b9cb2SDmitry Kozlyuk 	sdata[1].api_func = rte_mempool_event_callback_register;
682da2b9cb2SDmitry Kozlyuk 	sdata[1].cb_func = test_mempool_events_cb;
683da2b9cb2SDmitry Kozlyuk 	sdata[1].cb_user_data = &data;
684da2b9cb2SDmitry Kozlyuk 	sdata[1].ret = -1;
685da2b9cb2SDmitry Kozlyuk 	rte_mempool_event_callback_register(test_mempool_events_safety_cb,
686da2b9cb2SDmitry Kozlyuk 					    &sdata[1]);
687da2b9cb2SDmitry Kozlyuk 
688da2b9cb2SDmitry Kozlyuk 	mp = rte_mempool_create_empty("empty", MEMPOOL_SIZE,
689da2b9cb2SDmitry Kozlyuk 				      MEMPOOL_ELT_SIZE, 0, 0,
690da2b9cb2SDmitry Kozlyuk 				      SOCKET_ID_ANY, 0);
691da2b9cb2SDmitry Kozlyuk 	RTE_TEST_ASSERT_NOT_NULL(mp, "Cannot create mempool: %s",
692da2b9cb2SDmitry Kozlyuk 				 rte_strerror(rte_errno));
693da2b9cb2SDmitry Kozlyuk 	memset(&data, 0, sizeof(data));
694da2b9cb2SDmitry Kozlyuk 	ret = rte_mempool_populate_default(mp);
695da2b9cb2SDmitry Kozlyuk 	RTE_TEST_ASSERT_EQUAL(ret, (int)mp->size, "Failed to populate mempool: %s",
696da2b9cb2SDmitry Kozlyuk 			      rte_strerror(-ret));
697da2b9cb2SDmitry Kozlyuk 
698da2b9cb2SDmitry Kozlyuk 	RTE_TEST_ASSERT_EQUAL(sdata[0].ret, 0, "Callback failed to unregister itself: %s",
699da2b9cb2SDmitry Kozlyuk 			      rte_strerror(rte_errno));
700da2b9cb2SDmitry Kozlyuk 	RTE_TEST_ASSERT_EQUAL(sdata[1].ret, 0, "Failed to insert a new callback: %s",
701da2b9cb2SDmitry Kozlyuk 			      rte_strerror(rte_errno));
702da2b9cb2SDmitry Kozlyuk 	RTE_TEST_ASSERT_EQUAL(data.invoked, false,
703da2b9cb2SDmitry Kozlyuk 			      "Inserted callback is invoked on mempool population");
704da2b9cb2SDmitry Kozlyuk 
705da2b9cb2SDmitry Kozlyuk 	memset(&data, 0, sizeof(data));
706da2b9cb2SDmitry Kozlyuk 	sdata[0].invoked = false;
707da2b9cb2SDmitry Kozlyuk 	rte_mempool_free(mp);
708da2b9cb2SDmitry Kozlyuk 	mp = NULL;
709da2b9cb2SDmitry Kozlyuk 	RTE_TEST_ASSERT_EQUAL(sdata[0].invoked, false,
710da2b9cb2SDmitry Kozlyuk 			      "Callback that unregistered itself was called");
711da2b9cb2SDmitry Kozlyuk 	RTE_TEST_ASSERT_EQUAL(sdata[1].ret, -EEXIST,
712da2b9cb2SDmitry Kozlyuk 			      "New callback inserted twice");
713da2b9cb2SDmitry Kozlyuk 	RTE_TEST_ASSERT_EQUAL(data.invoked, true,
714da2b9cb2SDmitry Kozlyuk 			      "Inserted callback is not invoked on mempool destruction");
715da2b9cb2SDmitry Kozlyuk 
716da2b9cb2SDmitry Kozlyuk 	rte_mempool_event_callback_unregister(test_mempool_events_cb, &data);
717da2b9cb2SDmitry Kozlyuk 	for (i = 0; i < RTE_DIM(sdata); i++)
718da2b9cb2SDmitry Kozlyuk 		rte_mempool_event_callback_unregister
719da2b9cb2SDmitry Kozlyuk 				(test_mempool_events_safety_cb, &sdata[i]);
720da2b9cb2SDmitry Kozlyuk 	ret = TEST_SUCCESS;
721da2b9cb2SDmitry Kozlyuk 
722da2b9cb2SDmitry Kozlyuk exit:
723da2b9cb2SDmitry Kozlyuk 	/* cleanup, don't care which callbacks are already removed */
724da2b9cb2SDmitry Kozlyuk 	rte_mempool_event_callback_unregister(test_mempool_events_cb, &data);
725da2b9cb2SDmitry Kozlyuk 	for (i = 0; i < RTE_DIM(sdata); i++)
726da2b9cb2SDmitry Kozlyuk 		rte_mempool_event_callback_unregister
727da2b9cb2SDmitry Kozlyuk 				(test_mempool_events_safety_cb, &sdata[i]);
728da2b9cb2SDmitry Kozlyuk 	/* in case of failure before the planned destruction */
729da2b9cb2SDmitry Kozlyuk 	rte_mempool_free(mp);
730da2b9cb2SDmitry Kozlyuk 	return ret;
731da2b9cb2SDmitry Kozlyuk 
732da2b9cb2SDmitry Kozlyuk #pragma pop_macro("RTE_TEST_TRACE_FAILURE")
733da2b9cb2SDmitry Kozlyuk }
734da2b9cb2SDmitry Kozlyuk 
73511541c5cSDmitry Kozlyuk #pragma push_macro("RTE_TEST_TRACE_FAILURE")
73611541c5cSDmitry Kozlyuk #undef RTE_TEST_TRACE_FAILURE
73711541c5cSDmitry Kozlyuk #define RTE_TEST_TRACE_FAILURE(...) do { \
73811541c5cSDmitry Kozlyuk 		ret = TEST_FAILED; \
73911541c5cSDmitry Kozlyuk 		goto exit; \
74011541c5cSDmitry Kozlyuk 	} while (0)
74111541c5cSDmitry Kozlyuk 
74211541c5cSDmitry Kozlyuk static int
test_mempool_flag_non_io_set_when_no_iova_contig_set(void)74311541c5cSDmitry Kozlyuk test_mempool_flag_non_io_set_when_no_iova_contig_set(void)
74411541c5cSDmitry Kozlyuk {
74589e94773SDmitry Kozlyuk 	const struct rte_memzone *mz = NULL;
74689e94773SDmitry Kozlyuk 	void *virt;
7476fda3ff6SDmitry Kozlyuk 	rte_iova_t iova;
7486fda3ff6SDmitry Kozlyuk 	size_t size = MEMPOOL_ELT_SIZE * 16;
74911541c5cSDmitry Kozlyuk 	struct rte_mempool *mp = NULL;
75011541c5cSDmitry Kozlyuk 	int ret;
75111541c5cSDmitry Kozlyuk 
75289e94773SDmitry Kozlyuk 	mz = rte_memzone_reserve("test_mempool", size, SOCKET_ID_ANY, 0);
75389e94773SDmitry Kozlyuk 	RTE_TEST_ASSERT_NOT_NULL(mz, "Cannot allocate memory");
75489e94773SDmitry Kozlyuk 	virt = mz->addr;
75589e94773SDmitry Kozlyuk 	iova = mz->iova;
75611541c5cSDmitry Kozlyuk 	mp = rte_mempool_create_empty("empty", MEMPOOL_SIZE,
75711541c5cSDmitry Kozlyuk 				      MEMPOOL_ELT_SIZE, 0, 0,
758c47d7b90SAndrew Rybchenko 				      SOCKET_ID_ANY, RTE_MEMPOOL_F_NO_IOVA_CONTIG);
75911541c5cSDmitry Kozlyuk 	RTE_TEST_ASSERT_NOT_NULL(mp, "Cannot create mempool: %s",
76011541c5cSDmitry Kozlyuk 				 rte_strerror(rte_errno));
76111541c5cSDmitry Kozlyuk 	rte_mempool_set_ops_byname(mp, rte_mbuf_best_mempool_ops(), NULL);
7626fda3ff6SDmitry Kozlyuk 
7636fda3ff6SDmitry Kozlyuk 	RTE_TEST_ASSERT(mp->flags & RTE_MEMPOOL_F_NON_IO,
7646fda3ff6SDmitry Kozlyuk 			"NON_IO flag is not set on an empty mempool");
7656fda3ff6SDmitry Kozlyuk 
7666fda3ff6SDmitry Kozlyuk 	/*
7676fda3ff6SDmitry Kozlyuk 	 * Always use valid IOVA so that populate() has no other reason
7686fda3ff6SDmitry Kozlyuk 	 * to infer that the mempool cannot be used for IO.
7696fda3ff6SDmitry Kozlyuk 	 */
7706fda3ff6SDmitry Kozlyuk 	ret = rte_mempool_populate_iova(mp, virt, iova, size, NULL, NULL);
77111541c5cSDmitry Kozlyuk 	RTE_TEST_ASSERT(ret > 0, "Failed to populate mempool: %s",
77211541c5cSDmitry Kozlyuk 			rte_strerror(-ret));
773c47d7b90SAndrew Rybchenko 	RTE_TEST_ASSERT(mp->flags & RTE_MEMPOOL_F_NON_IO,
77411541c5cSDmitry Kozlyuk 			"NON_IO flag is not set when NO_IOVA_CONTIG is set");
77511541c5cSDmitry Kozlyuk 	ret = TEST_SUCCESS;
77611541c5cSDmitry Kozlyuk exit:
77711541c5cSDmitry Kozlyuk 	rte_mempool_free(mp);
77889e94773SDmitry Kozlyuk 	rte_memzone_free(mz);
77911541c5cSDmitry Kozlyuk 	return ret;
78011541c5cSDmitry Kozlyuk }
78111541c5cSDmitry Kozlyuk 
78211541c5cSDmitry Kozlyuk static int
test_mempool_flag_non_io_unset_when_populated_with_valid_iova(void)78311541c5cSDmitry Kozlyuk test_mempool_flag_non_io_unset_when_populated_with_valid_iova(void)
78411541c5cSDmitry Kozlyuk {
78589e94773SDmitry Kozlyuk 	const struct rte_memzone *mz = NULL;
78689e94773SDmitry Kozlyuk 	void *virt;
78711541c5cSDmitry Kozlyuk 	rte_iova_t iova;
78811541c5cSDmitry Kozlyuk 	size_t total_size = MEMPOOL_ELT_SIZE * MEMPOOL_SIZE;
78911541c5cSDmitry Kozlyuk 	size_t block_size = total_size / 3;
79011541c5cSDmitry Kozlyuk 	struct rte_mempool *mp = NULL;
79111541c5cSDmitry Kozlyuk 	int ret;
79211541c5cSDmitry Kozlyuk 
79311541c5cSDmitry Kozlyuk 	/*
79411541c5cSDmitry Kozlyuk 	 * Since objects from the pool are never used in the test,
79511541c5cSDmitry Kozlyuk 	 * we don't care for contiguous IOVA, on the other hand,
79689e94773SDmitry Kozlyuk 	 * requiring it could cause spurious test failures.
79711541c5cSDmitry Kozlyuk 	 */
79889e94773SDmitry Kozlyuk 	mz = rte_memzone_reserve("test_mempool", total_size, SOCKET_ID_ANY, 0);
79989e94773SDmitry Kozlyuk 	RTE_TEST_ASSERT_NOT_NULL(mz, "Cannot allocate memory");
80089e94773SDmitry Kozlyuk 	virt = mz->addr;
80189e94773SDmitry Kozlyuk 	iova = mz->iova;
80211541c5cSDmitry Kozlyuk 	mp = rte_mempool_create_empty("empty", MEMPOOL_SIZE,
80311541c5cSDmitry Kozlyuk 				      MEMPOOL_ELT_SIZE, 0, 0,
80411541c5cSDmitry Kozlyuk 				      SOCKET_ID_ANY, 0);
80511541c5cSDmitry Kozlyuk 	RTE_TEST_ASSERT_NOT_NULL(mp, "Cannot create mempool: %s",
80611541c5cSDmitry Kozlyuk 				 rte_strerror(rte_errno));
80711541c5cSDmitry Kozlyuk 
8086fda3ff6SDmitry Kozlyuk 	RTE_TEST_ASSERT(mp->flags & RTE_MEMPOOL_F_NON_IO,
8096fda3ff6SDmitry Kozlyuk 			"NON_IO flag is not set on an empty mempool");
8106fda3ff6SDmitry Kozlyuk 
81111541c5cSDmitry Kozlyuk 	ret = rte_mempool_populate_iova(mp, RTE_PTR_ADD(virt, 1 * block_size),
81211541c5cSDmitry Kozlyuk 					RTE_BAD_IOVA, block_size, NULL, NULL);
81311541c5cSDmitry Kozlyuk 	RTE_TEST_ASSERT(ret > 0, "Failed to populate mempool: %s",
81411541c5cSDmitry Kozlyuk 			rte_strerror(-ret));
815c47d7b90SAndrew Rybchenko 	RTE_TEST_ASSERT(mp->flags & RTE_MEMPOOL_F_NON_IO,
81611541c5cSDmitry Kozlyuk 			"NON_IO flag is not set when mempool is populated with only RTE_BAD_IOVA");
81711541c5cSDmitry Kozlyuk 
81811541c5cSDmitry Kozlyuk 	ret = rte_mempool_populate_iova(mp, virt, iova, block_size, NULL, NULL);
81911541c5cSDmitry Kozlyuk 	RTE_TEST_ASSERT(ret > 0, "Failed to populate mempool: %s",
82011541c5cSDmitry Kozlyuk 			rte_strerror(-ret));
821c47d7b90SAndrew Rybchenko 	RTE_TEST_ASSERT(!(mp->flags & RTE_MEMPOOL_F_NON_IO),
82211541c5cSDmitry Kozlyuk 			"NON_IO flag is not unset when mempool is populated with valid IOVA");
82311541c5cSDmitry Kozlyuk 
82411541c5cSDmitry Kozlyuk 	ret = rte_mempool_populate_iova(mp, RTE_PTR_ADD(virt, 2 * block_size),
82511541c5cSDmitry Kozlyuk 					RTE_BAD_IOVA, block_size, NULL, NULL);
82611541c5cSDmitry Kozlyuk 	RTE_TEST_ASSERT(ret > 0, "Failed to populate mempool: %s",
82711541c5cSDmitry Kozlyuk 			rte_strerror(-ret));
828c47d7b90SAndrew Rybchenko 	RTE_TEST_ASSERT(!(mp->flags & RTE_MEMPOOL_F_NON_IO),
82911541c5cSDmitry Kozlyuk 			"NON_IO flag is set even when some objects have valid IOVA");
83011541c5cSDmitry Kozlyuk 	ret = TEST_SUCCESS;
83111541c5cSDmitry Kozlyuk 
83211541c5cSDmitry Kozlyuk exit:
83311541c5cSDmitry Kozlyuk 	rte_mempool_free(mp);
83489e94773SDmitry Kozlyuk 	rte_memzone_free(mz);
83511541c5cSDmitry Kozlyuk 	return ret;
83611541c5cSDmitry Kozlyuk }
83711541c5cSDmitry Kozlyuk 
83811541c5cSDmitry Kozlyuk #pragma pop_macro("RTE_TEST_TRACE_FAILURE")
83911541c5cSDmitry Kozlyuk 
840a9de470cSBruce Richardson static int
test_mempool(void)841a9de470cSBruce Richardson test_mempool(void)
842a9de470cSBruce Richardson {
843a9de470cSBruce Richardson 	int ret = -1;
844923ceaeaSPallantla Poornima 	uint32_t nb_objs = 0;
845923ceaeaSPallantla Poornima 	uint32_t nb_mem_chunks = 0;
846*2f1015d8SPaul Szczepanek 	size_t alignment = 0;
847a9de470cSBruce Richardson 	struct rte_mempool *mp_cache = NULL;
848a9de470cSBruce Richardson 	struct rte_mempool *mp_nocache = NULL;
849923ceaeaSPallantla Poornima 	struct rte_mempool *mp_stack_anon = NULL;
850923ceaeaSPallantla Poornima 	struct rte_mempool *mp_stack_mempool_iter = NULL;
851a9de470cSBruce Richardson 	struct rte_mempool *mp_stack = NULL;
852a9de470cSBruce Richardson 	struct rte_mempool *default_pool = NULL;
853*2f1015d8SPaul Szczepanek 	struct rte_mempool *mp_alignment = NULL;
854923ceaeaSPallantla Poornima 	struct mp_data cb_arg = {
855923ceaeaSPallantla Poornima 		.ret = -1
856923ceaeaSPallantla Poornima 	};
857a9de470cSBruce Richardson 	const char *default_pool_ops = rte_mbuf_best_mempool_ops();
858*2f1015d8SPaul Szczepanek 	struct rte_mempool_mem_range_info mem_range = { 0 };
859a9de470cSBruce Richardson 
860a9de470cSBruce Richardson 	/* create a mempool (without cache) */
861a9de470cSBruce Richardson 	mp_nocache = rte_mempool_create("test_nocache", MEMPOOL_SIZE,
862a9de470cSBruce Richardson 		MEMPOOL_ELT_SIZE, 0, 0,
863a9de470cSBruce Richardson 		NULL, NULL,
864a9de470cSBruce Richardson 		my_obj_init, NULL,
865a9de470cSBruce Richardson 		SOCKET_ID_ANY, 0);
866a9de470cSBruce Richardson 
867a9de470cSBruce Richardson 	if (mp_nocache == NULL) {
868a9de470cSBruce Richardson 		printf("cannot allocate mp_nocache mempool\n");
869b2ca1e1cSOlivier Matz 		GOTO_ERR(ret, err);
870a9de470cSBruce Richardson 	}
871a9de470cSBruce Richardson 
872a9de470cSBruce Richardson 	/* create a mempool (with cache) */
873a9de470cSBruce Richardson 	mp_cache = rte_mempool_create("test_cache", MEMPOOL_SIZE,
874a9de470cSBruce Richardson 		MEMPOOL_ELT_SIZE,
875a9de470cSBruce Richardson 		RTE_MEMPOOL_CACHE_MAX_SIZE, 0,
876a9de470cSBruce Richardson 		NULL, NULL,
877a9de470cSBruce Richardson 		my_obj_init, NULL,
878a9de470cSBruce Richardson 		SOCKET_ID_ANY, 0);
879a9de470cSBruce Richardson 
880a9de470cSBruce Richardson 	if (mp_cache == NULL) {
881a9de470cSBruce Richardson 		printf("cannot allocate mp_cache mempool\n");
882b2ca1e1cSOlivier Matz 		GOTO_ERR(ret, err);
883a9de470cSBruce Richardson 	}
884a9de470cSBruce Richardson 
885923ceaeaSPallantla Poornima 	/* create an empty mempool  */
886923ceaeaSPallantla Poornima 	mp_stack_anon = rte_mempool_create_empty("test_stack_anon",
887923ceaeaSPallantla Poornima 		MEMPOOL_SIZE,
888923ceaeaSPallantla Poornima 		MEMPOOL_ELT_SIZE,
889923ceaeaSPallantla Poornima 		RTE_MEMPOOL_CACHE_MAX_SIZE, 0,
890923ceaeaSPallantla Poornima 		SOCKET_ID_ANY, 0);
891923ceaeaSPallantla Poornima 
892923ceaeaSPallantla Poornima 	if (mp_stack_anon == NULL)
893923ceaeaSPallantla Poornima 		GOTO_ERR(ret, err);
894923ceaeaSPallantla Poornima 
895923ceaeaSPallantla Poornima 	/* populate an empty mempool */
896923ceaeaSPallantla Poornima 	ret = rte_mempool_populate_anon(mp_stack_anon);
897923ceaeaSPallantla Poornima 	printf("%s ret = %d\n", __func__, ret);
898923ceaeaSPallantla Poornima 	if (ret < 0)
899923ceaeaSPallantla Poornima 		GOTO_ERR(ret, err);
900923ceaeaSPallantla Poornima 
901923ceaeaSPallantla Poornima 	/* Try to populate when already populated */
902923ceaeaSPallantla Poornima 	ret = rte_mempool_populate_anon(mp_stack_anon);
903923ceaeaSPallantla Poornima 	if (ret != 0)
904923ceaeaSPallantla Poornima 		GOTO_ERR(ret, err);
905923ceaeaSPallantla Poornima 
906923ceaeaSPallantla Poornima 	/* create a mempool  */
907923ceaeaSPallantla Poornima 	mp_stack_mempool_iter = rte_mempool_create("test_iter_obj",
908923ceaeaSPallantla Poornima 		MEMPOOL_SIZE,
909923ceaeaSPallantla Poornima 		MEMPOOL_ELT_SIZE,
910923ceaeaSPallantla Poornima 		RTE_MEMPOOL_CACHE_MAX_SIZE, 0,
911923ceaeaSPallantla Poornima 		NULL, NULL,
912923ceaeaSPallantla Poornima 		my_obj_init, NULL,
913923ceaeaSPallantla Poornima 		SOCKET_ID_ANY, 0);
914923ceaeaSPallantla Poornima 
915923ceaeaSPallantla Poornima 	if (mp_stack_mempool_iter == NULL)
916923ceaeaSPallantla Poornima 		GOTO_ERR(ret, err);
917923ceaeaSPallantla Poornima 
918923ceaeaSPallantla Poornima 	/* test to initialize mempool objects and memory */
919e1064f80SOlivier Matz 	nb_objs = rte_mempool_obj_iter(mp_stack_mempool_iter, my_obj_init,
920923ceaeaSPallantla Poornima 			NULL);
921923ceaeaSPallantla Poornima 	if (nb_objs == 0)
922923ceaeaSPallantla Poornima 		GOTO_ERR(ret, err);
923923ceaeaSPallantla Poornima 
924923ceaeaSPallantla Poornima 	nb_mem_chunks = rte_mempool_mem_iter(mp_stack_mempool_iter,
925923ceaeaSPallantla Poornima 			test_mp_mem_init, &cb_arg);
926923ceaeaSPallantla Poornima 	if (nb_mem_chunks == 0 || cb_arg.ret < 0)
927923ceaeaSPallantla Poornima 		GOTO_ERR(ret, err);
928923ceaeaSPallantla Poornima 
929a9de470cSBruce Richardson 	/* create a mempool with an external handler */
930a9de470cSBruce Richardson 	mp_stack = rte_mempool_create_empty("test_stack",
931a9de470cSBruce Richardson 		MEMPOOL_SIZE,
932a9de470cSBruce Richardson 		MEMPOOL_ELT_SIZE,
933a9de470cSBruce Richardson 		RTE_MEMPOOL_CACHE_MAX_SIZE, 0,
934a9de470cSBruce Richardson 		SOCKET_ID_ANY, 0);
935a9de470cSBruce Richardson 
936a9de470cSBruce Richardson 	if (mp_stack == NULL) {
937a9de470cSBruce Richardson 		printf("cannot allocate mp_stack mempool\n");
938b2ca1e1cSOlivier Matz 		GOTO_ERR(ret, err);
939a9de470cSBruce Richardson 	}
940a9de470cSBruce Richardson 	if (rte_mempool_set_ops_byname(mp_stack, "stack", NULL) < 0) {
941a9de470cSBruce Richardson 		printf("cannot set stack handler\n");
942b2ca1e1cSOlivier Matz 		GOTO_ERR(ret, err);
943a9de470cSBruce Richardson 	}
944a9de470cSBruce Richardson 	if (rte_mempool_populate_default(mp_stack) < 0) {
945a9de470cSBruce Richardson 		printf("cannot populate mp_stack mempool\n");
946b2ca1e1cSOlivier Matz 		GOTO_ERR(ret, err);
947a9de470cSBruce Richardson 	}
948a9de470cSBruce Richardson 	rte_mempool_obj_iter(mp_stack, my_obj_init, NULL);
949a9de470cSBruce Richardson 
950a9de470cSBruce Richardson 	/* Create a mempool based on Default handler */
951a9de470cSBruce Richardson 	printf("Testing %s mempool handler\n", default_pool_ops);
952a9de470cSBruce Richardson 	default_pool = rte_mempool_create_empty("default_pool",
953a9de470cSBruce Richardson 						MEMPOOL_SIZE,
954a9de470cSBruce Richardson 						MEMPOOL_ELT_SIZE,
955a9de470cSBruce Richardson 						RTE_MEMPOOL_CACHE_MAX_SIZE, 0,
956a9de470cSBruce Richardson 						SOCKET_ID_ANY, 0);
957a9de470cSBruce Richardson 
958a9de470cSBruce Richardson 	if (default_pool == NULL) {
959a9de470cSBruce Richardson 		printf("cannot allocate default mempool\n");
960b2ca1e1cSOlivier Matz 		GOTO_ERR(ret, err);
961a9de470cSBruce Richardson 	}
962a9de470cSBruce Richardson 	if (rte_mempool_set_ops_byname(default_pool,
963a9de470cSBruce Richardson 				default_pool_ops, NULL) < 0) {
964a9de470cSBruce Richardson 		printf("cannot set %s handler\n", default_pool_ops);
965b2ca1e1cSOlivier Matz 		GOTO_ERR(ret, err);
966a9de470cSBruce Richardson 	}
967a9de470cSBruce Richardson 	if (rte_mempool_populate_default(default_pool) < 0) {
968a9de470cSBruce Richardson 		printf("cannot populate %s mempool\n", default_pool_ops);
969b2ca1e1cSOlivier Matz 		GOTO_ERR(ret, err);
970a9de470cSBruce Richardson 	}
971a9de470cSBruce Richardson 	rte_mempool_obj_iter(default_pool, my_obj_init, NULL);
972a9de470cSBruce Richardson 
973*2f1015d8SPaul Szczepanek 	if (rte_mempool_get_mem_range(default_pool, &mem_range)) {
974*2f1015d8SPaul Szczepanek 		printf("cannot get mem range from default mempool\n");
975*2f1015d8SPaul Szczepanek 		GOTO_ERR(ret, err);
976*2f1015d8SPaul Szczepanek 	}
977*2f1015d8SPaul Szczepanek 
978*2f1015d8SPaul Szczepanek 	if (rte_mempool_get_mem_range(NULL, NULL) != -EINVAL) {
979*2f1015d8SPaul Szczepanek 		printf("rte_mempool_get_mem_range failed to return -EINVAL "
980*2f1015d8SPaul Szczepanek 				"when passed invalid arguments\n");
981*2f1015d8SPaul Szczepanek 		GOTO_ERR(ret, err);
982*2f1015d8SPaul Szczepanek 	}
983*2f1015d8SPaul Szczepanek 
984*2f1015d8SPaul Szczepanek 	if (mem_range.start == NULL || mem_range.length <
985*2f1015d8SPaul Szczepanek 			(MEMPOOL_SIZE * MEMPOOL_ELT_SIZE)) {
986*2f1015d8SPaul Szczepanek 		printf("mem range of default mempool is invalid\n");
987*2f1015d8SPaul Szczepanek 		GOTO_ERR(ret, err);
988*2f1015d8SPaul Szczepanek 	}
989*2f1015d8SPaul Szczepanek 
990*2f1015d8SPaul Szczepanek 	/* by default mempool objects are aligned by RTE_MEMPOOL_ALIGN */
991*2f1015d8SPaul Szczepanek 	alignment = rte_mempool_get_obj_alignment(default_pool);
992*2f1015d8SPaul Szczepanek 	if (alignment != RTE_MEMPOOL_ALIGN) {
993*2f1015d8SPaul Szczepanek 		printf("rte_mempool_get_obj_alignment returned wrong value, "
994*2f1015d8SPaul Szczepanek 				"expected %zu, returned %zu\n",
995*2f1015d8SPaul Szczepanek 				(size_t)RTE_MEMPOOL_ALIGN, alignment);
996*2f1015d8SPaul Szczepanek 		GOTO_ERR(ret, err);
997*2f1015d8SPaul Szczepanek 	}
998*2f1015d8SPaul Szczepanek 
999*2f1015d8SPaul Szczepanek 	/* create a mempool with a RTE_MEMPOOL_F_NO_CACHE_ALIGN flag */
1000*2f1015d8SPaul Szczepanek 	mp_alignment = rte_mempool_create("test_alignment",
1001*2f1015d8SPaul Szczepanek 		1, 8, /* the small size guarantees single memory chunk */
1002*2f1015d8SPaul Szczepanek 		0, 0, NULL, NULL, my_obj_init, NULL,
1003*2f1015d8SPaul Szczepanek 		SOCKET_ID_ANY, RTE_MEMPOOL_F_NO_CACHE_ALIGN);
1004*2f1015d8SPaul Szczepanek 
1005*2f1015d8SPaul Szczepanek 	if (mp_alignment == NULL) {
1006*2f1015d8SPaul Szczepanek 		printf("cannot allocate mempool with "
1007*2f1015d8SPaul Szczepanek 				"RTE_MEMPOOL_F_NO_CACHE_ALIGN flag\n");
1008*2f1015d8SPaul Szczepanek 		GOTO_ERR(ret, err);
1009*2f1015d8SPaul Szczepanek 	}
1010*2f1015d8SPaul Szczepanek 
1011*2f1015d8SPaul Szczepanek 	/* mempool was created with RTE_MEMPOOL_F_NO_CACHE_ALIGN
1012*2f1015d8SPaul Szczepanek 	 * and minimum alignment is expected which is sizeof(uint64_t)
1013*2f1015d8SPaul Szczepanek 	 */
1014*2f1015d8SPaul Szczepanek 	alignment = rte_mempool_get_obj_alignment(mp_alignment);
1015*2f1015d8SPaul Szczepanek 	if (alignment != sizeof(uint64_t)) {
1016*2f1015d8SPaul Szczepanek 		printf("rte_mempool_get_obj_alignment returned wrong value, "
1017*2f1015d8SPaul Szczepanek 				"expected %zu, returned %zu\n",
1018*2f1015d8SPaul Szczepanek 				(size_t)sizeof(uint64_t), alignment);
1019*2f1015d8SPaul Szczepanek 		GOTO_ERR(ret, err);
1020*2f1015d8SPaul Szczepanek 	}
1021*2f1015d8SPaul Szczepanek 
1022*2f1015d8SPaul Szczepanek 	alignment = rte_mempool_get_obj_alignment(NULL);
1023*2f1015d8SPaul Szczepanek 	if (alignment != 0) {
1024*2f1015d8SPaul Szczepanek 		printf("rte_mempool_get_obj_alignment failed to return 0 for "
1025*2f1015d8SPaul Szczepanek 				" an invalid mempool\n");
1026*2f1015d8SPaul Szczepanek 		GOTO_ERR(ret, err);
1027*2f1015d8SPaul Szczepanek 	}
1028*2f1015d8SPaul Szczepanek 
1029*2f1015d8SPaul Szczepanek 	if (rte_mempool_get_mem_range(mp_alignment, &mem_range)) {
1030*2f1015d8SPaul Szczepanek 		printf("cannot get mem range from mempool\n");
1031*2f1015d8SPaul Szczepanek 		GOTO_ERR(ret, err);
1032*2f1015d8SPaul Szczepanek 	}
1033*2f1015d8SPaul Szczepanek 
1034*2f1015d8SPaul Szczepanek 	if (!mem_range.is_contiguous) {
1035*2f1015d8SPaul Szczepanek 		printf("mempool not contiguous\n");
1036*2f1015d8SPaul Szczepanek 		GOTO_ERR(ret, err);
1037*2f1015d8SPaul Szczepanek 	}
1038*2f1015d8SPaul Szczepanek 
1039a9de470cSBruce Richardson 	/* retrieve the mempool from its name */
1040a9de470cSBruce Richardson 	if (rte_mempool_lookup("test_nocache") != mp_nocache) {
1041a9de470cSBruce Richardson 		printf("Cannot lookup mempool from its name\n");
1042b2ca1e1cSOlivier Matz 		GOTO_ERR(ret, err);
1043a9de470cSBruce Richardson 	}
1044a9de470cSBruce Richardson 
1045a9de470cSBruce Richardson 	printf("Walk into mempools:\n");
1046a9de470cSBruce Richardson 	rte_mempool_walk(walk_cb, NULL);
1047a9de470cSBruce Richardson 
1048a9de470cSBruce Richardson 	rte_mempool_list_dump(stdout);
1049a9de470cSBruce Richardson 
1050a9de470cSBruce Richardson 	/* basic tests without cache */
1051a9de470cSBruce Richardson 	if (test_mempool_basic(mp_nocache, 0) < 0)
1052b2ca1e1cSOlivier Matz 		GOTO_ERR(ret, err);
1053a9de470cSBruce Richardson 
1054a9de470cSBruce Richardson 	/* basic tests with cache */
1055a9de470cSBruce Richardson 	if (test_mempool_basic(mp_cache, 0) < 0)
1056b2ca1e1cSOlivier Matz 		GOTO_ERR(ret, err);
1057a9de470cSBruce Richardson 
1058a9de470cSBruce Richardson 	/* basic tests with user-owned cache */
1059a9de470cSBruce Richardson 	if (test_mempool_basic(mp_nocache, 1) < 0)
1060b2ca1e1cSOlivier Matz 		GOTO_ERR(ret, err);
1061a9de470cSBruce Richardson 
1062a9de470cSBruce Richardson 	/* more basic tests without cache */
1063a9de470cSBruce Richardson 	if (test_mempool_basic_ex(mp_nocache) < 0)
1064b2ca1e1cSOlivier Matz 		GOTO_ERR(ret, err);
1065a9de470cSBruce Richardson 
10664a6672c2SStephen Hemminger 	/* mempool operation test based on single producer and single consumer */
1067a9de470cSBruce Richardson 	if (test_mempool_sp_sc() < 0)
1068b2ca1e1cSOlivier Matz 		GOTO_ERR(ret, err);
1069a9de470cSBruce Richardson 
1070a9de470cSBruce Richardson 	if (test_mempool_creation_with_exceeded_cache_size() < 0)
1071b2ca1e1cSOlivier Matz 		GOTO_ERR(ret, err);
1072a9de470cSBruce Richardson 
1073afdaa607SDavid Marchand 	if (test_mempool_creation_with_invalid_flags() < 0)
1074b240af8bSDavid Marchand 		GOTO_ERR(ret, err);
1075b240af8bSDavid Marchand 
1076a9de470cSBruce Richardson 	if (test_mempool_same_name_twice_creation() < 0)
1077b2ca1e1cSOlivier Matz 		GOTO_ERR(ret, err);
1078a9de470cSBruce Richardson 
1079a9de470cSBruce Richardson 	/* test the stack handler */
1080a9de470cSBruce Richardson 	if (test_mempool_basic(mp_stack, 1) < 0)
1081b2ca1e1cSOlivier Matz 		GOTO_ERR(ret, err);
1082a9de470cSBruce Richardson 
1083a9de470cSBruce Richardson 	if (test_mempool_basic(default_pool, 1) < 0)
1084b2ca1e1cSOlivier Matz 		GOTO_ERR(ret, err);
1085a9de470cSBruce Richardson 
1086da2b9cb2SDmitry Kozlyuk 	/* test mempool event callbacks */
1087da2b9cb2SDmitry Kozlyuk 	if (test_mempool_events(rte_mempool_populate_default) < 0)
1088da2b9cb2SDmitry Kozlyuk 		GOTO_ERR(ret, err);
1089da2b9cb2SDmitry Kozlyuk 	if (test_mempool_events(rte_mempool_populate_anon) < 0)
1090da2b9cb2SDmitry Kozlyuk 		GOTO_ERR(ret, err);
1091da2b9cb2SDmitry Kozlyuk 	if (test_mempool_events_safety() < 0)
1092da2b9cb2SDmitry Kozlyuk 		GOTO_ERR(ret, err);
1093da2b9cb2SDmitry Kozlyuk 
109411541c5cSDmitry Kozlyuk 	/* test NON_IO flag inference */
109511541c5cSDmitry Kozlyuk 	if (test_mempool_flag_non_io_set_when_no_iova_contig_set() < 0)
109611541c5cSDmitry Kozlyuk 		GOTO_ERR(ret, err);
109711541c5cSDmitry Kozlyuk 	if (test_mempool_flag_non_io_unset_when_populated_with_valid_iova() < 0)
109811541c5cSDmitry Kozlyuk 		GOTO_ERR(ret, err);
109911541c5cSDmitry Kozlyuk 
1100a9de470cSBruce Richardson 	rte_mempool_list_dump(stdout);
1101a9de470cSBruce Richardson 
1102a9de470cSBruce Richardson 	ret = 0;
1103a9de470cSBruce Richardson 
1104a9de470cSBruce Richardson err:
1105a9de470cSBruce Richardson 	rte_mempool_free(mp_nocache);
1106a9de470cSBruce Richardson 	rte_mempool_free(mp_cache);
1107923ceaeaSPallantla Poornima 	rte_mempool_free(mp_stack_anon);
1108923ceaeaSPallantla Poornima 	rte_mempool_free(mp_stack_mempool_iter);
1109a9de470cSBruce Richardson 	rte_mempool_free(mp_stack);
1110a9de470cSBruce Richardson 	rte_mempool_free(default_pool);
1111*2f1015d8SPaul Szczepanek 	rte_mempool_free(mp_alignment);
1112a9de470cSBruce Richardson 
1113a9de470cSBruce Richardson 	return ret;
1114a9de470cSBruce Richardson }
1115a9de470cSBruce Richardson 
1116e0a8442cSBruce Richardson REGISTER_FAST_TEST(mempool_autotest, false, true, test_mempool);
1117