Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2010-2014 Intel Corporation
3 : : */
4 : :
5 : : #include <string.h>
6 : : #include <stdio.h>
7 : : #include <stdlib.h>
8 : : #include <stdint.h>
9 : : #include <inttypes.h>
10 : : #include <stdarg.h>
11 : : #include <errno.h>
12 : : #include <sys/queue.h>
13 : :
14 : : #include <rte_common.h>
15 : : #include <rte_eal_paging.h>
16 : : #include <rte_log.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>
22 : : #include <rte_eal.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>
30 : : #include <rte_mbuf.h>
31 : :
32 : : #include "test.h"
33 : :
34 : : /*
35 : : * Mempool
36 : : * =======
37 : : *
38 : : * Basic tests: done on one core with and without cache:
39 : : *
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.
44 : : */
45 : :
46 : : #define MEMPOOL_ELT_SIZE 2048
47 : : #define MAX_KEEP 16
48 : : #define MEMPOOL_SIZE ((rte_lcore_count()*(MAX_KEEP+RTE_MEMPOOL_CACHE_MAX_SIZE))-1)
49 : :
50 : : #define LOG_ERR() printf("test failed at %s():%d\n", __func__, __LINE__)
51 : : #define RET_ERR() do { \
52 : : LOG_ERR(); \
53 : : return -1; \
54 : : } while (0)
55 : : #define GOTO_ERR(var, label) do { \
56 : : LOG_ERR(); \
57 : : var = -1; \
58 : : goto label; \
59 : : } while (0)
60 : :
61 : : /*
62 : : * save the object number in the first 4 bytes of object data. All
63 : : * other bytes are set to 0.
64 : : */
65 : : static void
66 : 7386 : my_obj_init(struct rte_mempool *mp, __rte_unused void *arg,
67 : : void *obj, unsigned i)
68 : : {
69 : : uint32_t *objnum = obj;
70 : :
71 : 7386 : memset(obj, 0, mp->elt_size);
72 : 7386 : *objnum = i;
73 : 7386 : }
74 : :
75 : : /* basic tests (done on one core) */
76 : : static int
77 : 5 : test_mempool_basic(struct rte_mempool *mp, int use_external_cache)
78 : : {
79 : : uint32_t *objnum;
80 : : void **objtable;
81 : : void *obj, *obj2;
82 : : char *obj_data;
83 : : int ret = 0;
84 : : unsigned i, j;
85 : : int offset;
86 : : struct rte_mempool_cache *cache;
87 : :
88 [ + + ]: 5 : if (use_external_cache) {
89 : : /* Create a user-owned mempool cache. */
90 : 3 : cache = rte_mempool_cache_create(RTE_MEMPOOL_CACHE_MAX_SIZE,
91 : : SOCKET_ID_ANY);
92 [ - + ]: 3 : if (cache == NULL)
93 : 0 : RET_ERR();
94 : : } else {
95 : : /* May be NULL if cache is disabled. */
96 : : cache = rte_mempool_default_cache(mp, rte_lcore_id());
97 : : }
98 : :
99 : : /* dump the mempool status */
100 : 5 : rte_mempool_dump(stdout, mp);
101 : :
102 : : printf("get an object\n");
103 [ - + ]: 1 : if (rte_mempool_generic_get(mp, &obj, 1, cache) < 0)
104 : 0 : GOTO_ERR(ret, out);
105 : 5 : rte_mempool_dump(stdout, mp);
106 : :
107 : : /* tests that improve coverage */
108 : : printf("get object count\n");
109 : : /* We have to count the extra caches, one in this case. */
110 [ + + ]: 5 : offset = use_external_cache ? 1 * cache->len : 0;
111 [ - + ]: 5 : if (rte_mempool_avail_count(mp) + offset != MEMPOOL_SIZE - 1)
112 : 0 : GOTO_ERR(ret, out);
113 : :
114 : : printf("get private data\n");
115 : : if (rte_mempool_get_priv(mp) != (char *)mp +
116 : : RTE_MEMPOOL_HEADER_SIZE(mp, mp->cache_size))
117 : : GOTO_ERR(ret, out);
118 : :
119 : : #ifndef RTE_EXEC_ENV_FREEBSD /* rte_mem_virt2iova() not supported on bsd */
120 : : printf("get physical address of an object\n");
121 [ - + ]: 5 : if (rte_mempool_virt2iova(obj) != rte_mem_virt2iova(obj))
122 : 0 : GOTO_ERR(ret, out);
123 : : #endif
124 : :
125 : : printf("put the object back\n");
126 : : rte_mempool_generic_put(mp, &obj, 1, cache);
127 : 5 : rte_mempool_dump(stdout, mp);
128 : :
129 : : printf("get 2 objects\n");
130 [ - + ]: 5 : if (rte_mempool_generic_get(mp, &obj, 1, cache) < 0)
131 : 0 : GOTO_ERR(ret, out);
132 [ - + ]: 5 : if (rte_mempool_generic_get(mp, &obj2, 1, cache) < 0) {
133 : : rte_mempool_generic_put(mp, &obj, 1, cache);
134 : 0 : GOTO_ERR(ret, out);
135 : : }
136 : 5 : rte_mempool_dump(stdout, mp);
137 : :
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 : 5 : rte_mempool_dump(stdout, mp);
142 : :
143 : : /*
144 : : * get many objects: we cannot get them all because the cache
145 : : * on other cores may not be empty.
146 : : */
147 : 5 : objtable = malloc(MEMPOOL_SIZE * sizeof(void *));
148 [ - + ]: 5 : if (objtable == NULL)
149 : 0 : GOTO_ERR(ret, out);
150 : :
151 [ + + ]: 5280 : for (i = 0; i < MEMPOOL_SIZE; i++) {
152 [ + + + - ]: 10546 : if (rte_mempool_generic_get(mp, &objtable[i], 1, cache) < 0)
153 : : break;
154 : : }
155 : :
156 : : /*
157 : : * for each object, check that its content was not modified,
158 : : * and put objects back in pool
159 : : */
160 [ + + ]: 5280 : while (i--) {
161 : 5275 : obj = objtable[i];
162 : : obj_data = obj;
163 : : objnum = obj;
164 [ - + ]: 5275 : if (*objnum > MEMPOOL_SIZE) {
165 : 0 : printf("bad object number(%d)\n", *objnum);
166 : : ret = -1;
167 : 0 : break;
168 : : }
169 [ + + ]: 10787375 : for (j = sizeof(*objnum); j < mp->elt_size; j++) {
170 [ - + ]: 10782100 : if (obj_data[j] != 0)
171 : : ret = -1;
172 : : }
173 : :
174 : : rte_mempool_generic_put(mp, &objtable[i], 1, cache);
175 : : }
176 : :
177 : 5 : free(objtable);
178 [ + - ]: 5 : if (ret == -1)
179 : : printf("objects were modified!\n");
180 : :
181 : 5 : out:
182 [ + + ]: 5 : if (use_external_cache) {
183 : : rte_mempool_cache_flush(cache, mp);
184 : 3 : rte_mempool_cache_free(cache);
185 : : }
186 : :
187 : : return ret;
188 : : }
189 : :
190 : 1 : static int test_mempool_creation_with_exceeded_cache_size(void)
191 : : {
192 : : struct rte_mempool *mp_cov;
193 : :
194 : 1 : mp_cov = rte_mempool_create("test_mempool_cache_too_big",
195 : 1 : MEMPOOL_SIZE,
196 : : MEMPOOL_ELT_SIZE,
197 : : RTE_MEMPOOL_CACHE_MAX_SIZE + 32, 0,
198 : : NULL, NULL,
199 : : my_obj_init, NULL,
200 : : SOCKET_ID_ANY, 0);
201 : :
202 [ - + ]: 1 : if (mp_cov != NULL) {
203 : 0 : rte_mempool_free(mp_cov);
204 : 0 : RET_ERR();
205 : : }
206 : :
207 : : return 0;
208 : : }
209 : :
210 : 1 : static int test_mempool_creation_with_invalid_flags(void)
211 : : {
212 : : struct rte_mempool *mp_cov;
213 : :
214 : 1 : mp_cov = rte_mempool_create("test_mempool_invalid_flags", MEMPOOL_SIZE,
215 : : MEMPOOL_ELT_SIZE, 0, 0,
216 : : NULL, NULL,
217 : : NULL, NULL,
218 : : SOCKET_ID_ANY, ~RTE_MEMPOOL_VALID_USER_FLAGS);
219 : :
220 [ - + ]: 1 : if (mp_cov != NULL) {
221 : 0 : rte_mempool_free(mp_cov);
222 : 0 : RET_ERR();
223 : : }
224 : :
225 : : return 0;
226 : : }
227 : :
228 : : static struct rte_mempool *mp_spsc;
229 : : static rte_spinlock_t scsp_spinlock;
230 : : static void *scsp_obj_table[MAX_KEEP];
231 : :
232 : : /*
233 : : * single producer function
234 : : */
235 : 1 : static int test_mempool_single_producer(void)
236 : : {
237 : : unsigned int i;
238 : : void *obj = NULL;
239 : : uint64_t start_cycles, end_cycles;
240 : 1 : uint64_t duration = rte_get_timer_hz() / 4;
241 : :
242 : : start_cycles = rte_get_timer_cycles();
243 : : while (1) {
244 : : end_cycles = rte_get_timer_cycles();
245 : : /* duration uses up, stop producing */
246 [ + + ]: 898321 : if (start_cycles + duration < end_cycles)
247 : : break;
248 : : rte_spinlock_lock(&scsp_spinlock);
249 [ + + ]: 11914848 : for (i = 0; i < MAX_KEEP; i ++) {
250 [ + + ]: 11250470 : if (NULL != scsp_obj_table[i]) {
251 : : obj = scsp_obj_table[i];
252 : : break;
253 : : }
254 : : }
255 : : rte_spinlock_unlock(&scsp_spinlock);
256 [ + + ]: 898320 : if (i >= MAX_KEEP) {
257 : 664378 : continue;
258 : : }
259 [ - + ]: 233942 : if (rte_mempool_from_obj(obj) != mp_spsc) {
260 : : printf("obj not owned by this mempool\n");
261 : 0 : RET_ERR();
262 : : }
263 : 233942 : rte_mempool_put(mp_spsc, obj);
264 : : rte_spinlock_lock(&scsp_spinlock);
265 : 233942 : scsp_obj_table[i] = NULL;
266 : : rte_spinlock_unlock(&scsp_spinlock);
267 : : }
268 : :
269 : : return 0;
270 : : }
271 : :
272 : : /*
273 : : * single consumer function
274 : : */
275 : 1 : static int test_mempool_single_consumer(void)
276 : : {
277 : : unsigned int i;
278 : : void * obj;
279 : : uint64_t start_cycles, end_cycles;
280 : 1 : uint64_t duration = rte_get_timer_hz() / 8;
281 : :
282 : : start_cycles = rte_get_timer_cycles();
283 : : while (1) {
284 : : end_cycles = rte_get_timer_cycles();
285 : : /* duration uses up, stop consuming */
286 [ + + ]: 298626 : if (start_cycles + duration < end_cycles)
287 : : break;
288 : : rte_spinlock_lock(&scsp_spinlock);
289 [ + + ]: 1720033 : for (i = 0; i < MAX_KEEP; i ++) {
290 [ + + ]: 1655350 : if (NULL == scsp_obj_table[i])
291 : : break;
292 : : }
293 : : rte_spinlock_unlock(&scsp_spinlock);
294 [ + + ]: 298625 : if (i >= MAX_KEEP)
295 : 64683 : continue;
296 [ - + + - ]: 467884 : if (rte_mempool_get(mp_spsc, &obj) < 0)
297 : : break;
298 : : rte_spinlock_lock(&scsp_spinlock);
299 : 233942 : scsp_obj_table[i] = obj;
300 : : rte_spinlock_unlock(&scsp_spinlock);
301 : : }
302 : :
303 : 1 : return 0;
304 : : }
305 : :
306 : : /*
307 : : * test function for mempool test based on single consumer and single producer,
308 : : * can run on one lcore only
309 : : */
310 : : static int
311 : 1 : test_mempool_launch_single_consumer(__rte_unused void *arg)
312 : : {
313 : 1 : return test_mempool_single_consumer();
314 : : }
315 : :
316 : : static void
317 : 1 : my_mp_init(struct rte_mempool *mp, __rte_unused void *arg)
318 : : {
319 : 1 : printf("mempool name is %s\n", mp->name);
320 : : /* nothing to be implemented here*/
321 : 1 : return ;
322 : : }
323 : :
324 : : /*
325 : : * it tests the mempool operations based on single producer and single consumer
326 : : */
327 : : static int
328 [ + - ]: 1 : test_mempool_sp_sc(void)
329 : : {
330 : : int ret = 0;
331 : : unsigned lcore_id = rte_lcore_id();
332 : : unsigned lcore_next;
333 : :
334 : : /* create a mempool with single producer/consumer ring */
335 [ + - ]: 1 : if (mp_spsc == NULL) {
336 : 1 : mp_spsc = rte_mempool_create("test_mempool_sp_sc", MEMPOOL_SIZE,
337 : : MEMPOOL_ELT_SIZE, 0, 0,
338 : : my_mp_init, NULL,
339 : : my_obj_init, NULL,
340 : : SOCKET_ID_ANY,
341 : : RTE_MEMPOOL_F_NO_CACHE_ALIGN | RTE_MEMPOOL_F_SP_PUT |
342 : : RTE_MEMPOOL_F_SC_GET);
343 [ - + ]: 1 : if (mp_spsc == NULL)
344 : 0 : RET_ERR();
345 : : }
346 [ - + ]: 1 : if (rte_mempool_lookup("test_mempool_sp_sc") != mp_spsc) {
347 : : printf("Cannot lookup mempool from its name\n");
348 : : ret = -1;
349 : 0 : goto err;
350 : : }
351 : 1 : lcore_next = rte_get_next_lcore(lcore_id, 0, 1);
352 [ - + ]: 1 : if (lcore_next >= RTE_MAX_LCORE) {
353 : : ret = -1;
354 : 0 : goto err;
355 : : }
356 [ - + ]: 1 : if (rte_eal_lcore_role(lcore_next) != ROLE_RTE) {
357 : : ret = -1;
358 : 0 : goto err;
359 : : }
360 : : rte_spinlock_init(&scsp_spinlock);
361 : : memset(scsp_obj_table, 0, sizeof(scsp_obj_table));
362 : 1 : rte_eal_remote_launch(test_mempool_launch_single_consumer, NULL,
363 : : lcore_next);
364 [ - + ]: 1 : if (test_mempool_single_producer() < 0)
365 : : ret = -1;
366 : :
367 [ + - ]: 1 : if (rte_eal_wait_lcore(lcore_next) < 0)
368 : : ret = -1;
369 : :
370 : 1 : err:
371 : 1 : rte_mempool_free(mp_spsc);
372 : 1 : mp_spsc = NULL;
373 : :
374 : 1 : return ret;
375 : : }
376 : :
377 : : /*
378 : : * it tests some more basic of mempool
379 : : */
380 : : static int
381 : 1 : test_mempool_basic_ex(struct rte_mempool *mp)
382 : : {
383 : : unsigned i;
384 : : void **obj;
385 : : void *err_obj;
386 : : int ret = -1;
387 : :
388 [ + - ]: 1 : if (mp == NULL)
389 : : return ret;
390 : :
391 : 1 : obj = rte_calloc("test_mempool_basic_ex", MEMPOOL_SIZE,
392 : : sizeof(void *), 0);
393 [ - + ]: 1 : if (obj == NULL) {
394 : : printf("test_mempool_basic_ex fail to rte_malloc\n");
395 : 0 : return ret;
396 : : }
397 : 1 : printf("test_mempool_basic_ex now mempool (%s) has %u free entries\n",
398 : 1 : mp->name, rte_mempool_in_use_count(mp));
399 [ - + ]: 1 : if (rte_mempool_full(mp) != 1) {
400 : : printf("test_mempool_basic_ex the mempool should be full\n");
401 : 0 : goto fail_mp_basic_ex;
402 : : }
403 : :
404 [ + + ]: 1056 : for (i = 0; i < MEMPOOL_SIZE; i ++) {
405 [ - + - + ]: 2110 : if (rte_mempool_get(mp, &obj[i]) < 0) {
406 : : printf("test_mp_basic_ex fail to get object for [%u]\n",
407 : : i);
408 : 0 : goto fail_mp_basic_ex;
409 : : }
410 : : }
411 [ - + ]: 1 : if (rte_mempool_get(mp, &err_obj) == 0) {
412 : : printf("test_mempool_basic_ex get an impossible obj\n");
413 : 0 : goto fail_mp_basic_ex;
414 : : }
415 : : printf("number: %u\n", i);
416 [ - + ]: 1 : if (rte_mempool_empty(mp) != 1) {
417 : : printf("test_mempool_basic_ex the mempool should be empty\n");
418 : 0 : goto fail_mp_basic_ex;
419 : : }
420 : :
421 [ + + ]: 1056 : for (i = 0; i < MEMPOOL_SIZE; i++)
422 [ - + ]: 2110 : rte_mempool_put(mp, obj[i]);
423 : :
424 [ - + ]: 1 : if (rte_mempool_full(mp) != 1) {
425 : : printf("test_mempool_basic_ex the mempool should be full\n");
426 : 0 : goto fail_mp_basic_ex;
427 : : }
428 : :
429 : : ret = 0;
430 : :
431 : 1 : fail_mp_basic_ex:
432 : : if (obj != NULL)
433 : 1 : rte_free((void *)obj);
434 : :
435 : 1 : return ret;
436 : : }
437 : :
438 : : static int
439 : 1 : test_mempool_same_name_twice_creation(void)
440 : : {
441 : : struct rte_mempool *mp_tc, *mp_tc2;
442 : :
443 : 1 : mp_tc = rte_mempool_create("test_mempool_same_name", MEMPOOL_SIZE,
444 : : MEMPOOL_ELT_SIZE, 0, 0,
445 : : NULL, NULL,
446 : : NULL, NULL,
447 : : SOCKET_ID_ANY, 0);
448 : :
449 [ - + ]: 1 : if (mp_tc == NULL)
450 : 0 : RET_ERR();
451 : :
452 : 1 : mp_tc2 = rte_mempool_create("test_mempool_same_name", MEMPOOL_SIZE,
453 : : MEMPOOL_ELT_SIZE, 0, 0,
454 : : NULL, NULL,
455 : : NULL, NULL,
456 : : SOCKET_ID_ANY, 0);
457 : :
458 [ - + ]: 1 : if (mp_tc2 != NULL) {
459 : 0 : rte_mempool_free(mp_tc);
460 : 0 : rte_mempool_free(mp_tc2);
461 : 0 : RET_ERR();
462 : : }
463 : :
464 : 1 : rte_mempool_free(mp_tc);
465 : 1 : return 0;
466 : : }
467 : :
468 : : static void
469 : 7 : walk_cb(struct rte_mempool *mp, void *userdata __rte_unused)
470 : : {
471 : 7 : printf("\t%s\n", mp->name);
472 : 7 : }
473 : :
474 : : struct mp_data {
475 : : int16_t ret;
476 : : };
477 : :
478 : : static void
479 : 2 : 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)
483 : : {
484 : : struct mp_data *data = opaque;
485 : :
486 [ - + ]: 2 : if (mp == NULL) {
487 : 0 : data->ret = -1;
488 : 0 : return;
489 : : }
490 : : /* nothing to be implemented here*/
491 : 2 : data->ret = 0;
492 : : }
493 : :
494 : : struct test_mempool_events_data {
495 : : struct rte_mempool *mp;
496 : : enum rte_mempool_event event;
497 : : bool invoked;
498 : : };
499 : :
500 : : static void
501 : 19 : test_mempool_events_cb(enum rte_mempool_event event,
502 : : struct rte_mempool *mp, void *user_data)
503 : : {
504 : : struct test_mempool_events_data *data = user_data;
505 : :
506 : 19 : data->mp = mp;
507 : 19 : data->event = event;
508 : 19 : data->invoked = true;
509 : 19 : }
510 : :
511 : : static int
512 : 2 : test_mempool_events(int (*populate)(struct rte_mempool *mp))
513 : : {
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)
517 : :
518 : : #define CALLBACK_NUM 3u
519 : : #define MEMPOOL_NUM 2u
520 : :
521 : : static const unsigned int mempool_elt_size = 64;
522 : : static const unsigned int mempool_size = 64;
523 : :
524 : : struct test_mempool_events_data data[CALLBACK_NUM];
525 : : struct rte_mempool *mp[MEMPOOL_NUM], *freed;
526 : : char name[RTE_MEMPOOL_NAMESIZE];
527 : : size_t i, j;
528 : : int ret;
529 : :
530 : : memset(mp, 0, sizeof(mp));
531 [ + + ]: 8 : for (i = 0; i < CALLBACK_NUM; i++) {
532 : 6 : ret = rte_mempool_event_callback_register
533 : 6 : (test_mempool_events_cb, &data[i]);
534 [ - + ]: 6 : RTE_TEST_ASSERT_EQUAL(ret, 0, "Failed to register the callback %zu: %s",
535 : : i, rte_strerror(rte_errno));
536 : : }
537 : 2 : ret = rte_mempool_event_callback_unregister(test_mempool_events_cb, mp);
538 [ - + ]: 2 : RTE_TEST_ASSERT_NOT_EQUAL(ret, 0, "Unregistered a non-registered callback");
539 : : /* NULL argument has no special meaning in this API. */
540 : 2 : ret = rte_mempool_event_callback_unregister(test_mempool_events_cb,
541 : : NULL);
542 [ - + ]: 2 : RTE_TEST_ASSERT_NOT_EQUAL(ret, 0, "Unregistered a non-registered callback with NULL argument");
543 : :
544 : : /* Create mempool 0 that will be observed by all callbacks. */
545 : : memset(&data, 0, sizeof(data));
546 : : strcpy(name, "empty0");
547 : 2 : mp[0] = rte_mempool_create_empty(name, mempool_size,
548 : : mempool_elt_size, 0, 0,
549 : : SOCKET_ID_ANY, 0);
550 [ + - ]: 2 : RTE_TEST_ASSERT_NOT_NULL(mp[0], "Cannot create mempool %s: %s",
551 : : name, rte_strerror(rte_errno));
552 [ + + ]: 8 : for (j = 0; j < CALLBACK_NUM; j++)
553 [ - + ]: 6 : RTE_TEST_ASSERT_EQUAL(data[j].invoked, false,
554 : : "Callback %zu invoked on %s mempool creation",
555 : : j, name);
556 : :
557 : 2 : rte_mempool_set_ops_byname(mp[0], rte_mbuf_best_mempool_ops(), NULL);
558 : 2 : ret = populate(mp[0]);
559 [ + - ]: 2 : RTE_TEST_ASSERT_EQUAL(ret, (int)mp[0]->size, "Failed to populate mempool %s: %s",
560 : : name, rte_strerror(-ret));
561 [ + + ]: 8 : for (j = 0; j < CALLBACK_NUM; j++) {
562 [ - + ]: 6 : RTE_TEST_ASSERT_EQUAL(data[j].invoked, true,
563 : : "Callback %zu not invoked on mempool %s population",
564 : : j, name);
565 [ - + ]: 6 : RTE_TEST_ASSERT_EQUAL(data[j].event,
566 : : RTE_MEMPOOL_EVENT_READY,
567 : : "Wrong callback invoked, expected READY");
568 [ - + ]: 6 : RTE_TEST_ASSERT_EQUAL(data[j].mp, mp[0],
569 : : "Callback %zu invoked for a wrong mempool instead of %s",
570 : : j, name);
571 : : }
572 : :
573 : : /* Check that unregistered callback 0 observes no events. */
574 : 2 : ret = rte_mempool_event_callback_unregister(test_mempool_events_cb,
575 : : &data[0]);
576 [ - + ]: 2 : RTE_TEST_ASSERT_EQUAL(ret, 0, "Failed to unregister callback 0: %s",
577 : : rte_strerror(rte_errno));
578 : : memset(&data, 0, sizeof(data));
579 : : strcpy(name, "empty1");
580 : 2 : mp[1] = rte_mempool_create_empty(name, mempool_size,
581 : : mempool_elt_size, 0, 0,
582 : : SOCKET_ID_ANY, 0);
583 [ - + ]: 2 : RTE_TEST_ASSERT_NOT_NULL(mp[1], "Cannot create mempool %s: %s",
584 : : name, rte_strerror(rte_errno));
585 : 2 : rte_mempool_set_ops_byname(mp[1], rte_mbuf_best_mempool_ops(), NULL);
586 : 2 : ret = populate(mp[1]);
587 [ - + ]: 2 : RTE_TEST_ASSERT_EQUAL(ret, (int)mp[1]->size, "Failed to populate mempool %s: %s",
588 : : name, rte_strerror(-ret));
589 [ + - ]: 2 : RTE_TEST_ASSERT_EQUAL(data[0].invoked, false,
590 : : "Unregistered callback 0 invoked on %s mempool populaton",
591 : : name);
592 : :
593 [ + + ]: 6 : for (i = 0; i < MEMPOOL_NUM; i++) {
594 : : memset(&data, 0, sizeof(data));
595 : : sprintf(name, "empty%zu", i);
596 : 4 : rte_mempool_free(mp[i]);
597 : : /*
598 : : * Save pointer to check that it was passed to the callback,
599 : : * but put NULL into the array in case cleanup is called early.
600 : : */
601 : 4 : freed = mp[i];
602 : 4 : mp[i] = NULL;
603 [ + + ]: 12 : for (j = 1; j < CALLBACK_NUM; j++) {
604 [ - + ]: 8 : RTE_TEST_ASSERT_EQUAL(data[j].invoked, true,
605 : : "Callback %zu not invoked on mempool %s destruction",
606 : : j, name);
607 [ - + ]: 8 : RTE_TEST_ASSERT_EQUAL(data[j].event,
608 : : RTE_MEMPOOL_EVENT_DESTROY,
609 : : "Wrong callback invoked, expected DESTROY");
610 [ - + ]: 8 : RTE_TEST_ASSERT_EQUAL(data[j].mp, freed,
611 : : "Callback %zu invoked for a wrong mempool instead of %s",
612 : : j, name);
613 : : }
614 [ - + ]: 4 : RTE_TEST_ASSERT_EQUAL(data[0].invoked, false,
615 : : "Unregistered callback 0 invoked on %s mempool destruction",
616 : : name);
617 : : }
618 : :
619 [ + + ]: 6 : for (j = 1; j < CALLBACK_NUM; j++) {
620 : 4 : ret = rte_mempool_event_callback_unregister
621 : 4 : (test_mempool_events_cb, &data[j]);
622 [ - + ]: 4 : RTE_TEST_ASSERT_EQUAL(ret, 0, "Failed to unregister the callback %zu: %s",
623 : : j, rte_strerror(rte_errno));
624 : : }
625 : : return TEST_SUCCESS;
626 : :
627 : : fail:
628 [ # # ]: 0 : for (j = 0; j < CALLBACK_NUM; j++)
629 : 0 : rte_mempool_event_callback_unregister
630 : 0 : (test_mempool_events_cb, &data[j]);
631 [ # # ]: 0 : for (i = 0; i < MEMPOOL_NUM; i++)
632 : 0 : rte_mempool_free(mp[i]);
633 : : return TEST_FAILED;
634 : :
635 : : #pragma pop_macro("RTE_TEST_TRACE_FAILURE")
636 : : }
637 : :
638 : : struct test_mempool_events_safety_data {
639 : : bool invoked;
640 : : int (*api_func)(rte_mempool_event_callback *func, void *user_data);
641 : : rte_mempool_event_callback *cb_func;
642 : : void *cb_user_data;
643 : : int ret;
644 : : };
645 : :
646 : : static void
647 : 3 : test_mempool_events_safety_cb(enum rte_mempool_event event,
648 : : struct rte_mempool *mp, void *user_data)
649 : : {
650 : : struct test_mempool_events_safety_data *data = user_data;
651 : :
652 : : RTE_SET_USED(event);
653 : : RTE_SET_USED(mp);
654 : 3 : data->invoked = true;
655 : 3 : data->ret = data->api_func(data->cb_func, data->cb_user_data);
656 : 3 : }
657 : :
658 : : static int
659 : 1 : test_mempool_events_safety(void)
660 : : {
661 : : #pragma push_macro("RTE_TEST_TRACE_FAILURE")
662 : : #undef RTE_TEST_TRACE_FAILURE
663 : : #define RTE_TEST_TRACE_FAILURE(...) do { \
664 : : ret = TEST_FAILED; \
665 : : goto exit; \
666 : : } while (0)
667 : :
668 : : struct test_mempool_events_data data;
669 : : struct test_mempool_events_safety_data sdata[2];
670 : : struct rte_mempool *mp;
671 : : size_t i;
672 : : int ret;
673 : :
674 : : /* removes itself */
675 : 1 : sdata[0].api_func = rte_mempool_event_callback_unregister;
676 : 1 : sdata[0].cb_func = test_mempool_events_safety_cb;
677 : 1 : sdata[0].cb_user_data = &sdata[0];
678 : 1 : sdata[0].ret = -1;
679 : 1 : rte_mempool_event_callback_register(test_mempool_events_safety_cb,
680 : : &sdata[0]);
681 : : /* inserts a callback after itself */
682 : 1 : sdata[1].api_func = rte_mempool_event_callback_register;
683 : 1 : sdata[1].cb_func = test_mempool_events_cb;
684 : 1 : sdata[1].cb_user_data = &data;
685 : 1 : sdata[1].ret = -1;
686 : 1 : rte_mempool_event_callback_register(test_mempool_events_safety_cb,
687 : : &sdata[1]);
688 : :
689 : 1 : mp = rte_mempool_create_empty("empty", MEMPOOL_SIZE,
690 : : MEMPOOL_ELT_SIZE, 0, 0,
691 : : SOCKET_ID_ANY, 0);
692 [ - + ]: 1 : RTE_TEST_ASSERT_NOT_NULL(mp, "Cannot create mempool: %s",
693 : : rte_strerror(rte_errno));
694 : : memset(&data, 0, sizeof(data));
695 : 1 : ret = rte_mempool_populate_default(mp);
696 [ - + ]: 1 : RTE_TEST_ASSERT_EQUAL(ret, (int)mp->size, "Failed to populate mempool: %s",
697 : : rte_strerror(-ret));
698 : :
699 [ - + ]: 1 : RTE_TEST_ASSERT_EQUAL(sdata[0].ret, 0, "Callback failed to unregister itself: %s",
700 : : rte_strerror(rte_errno));
701 [ - + ]: 1 : RTE_TEST_ASSERT_EQUAL(sdata[1].ret, 0, "Failed to insert a new callback: %s",
702 : : rte_strerror(rte_errno));
703 [ - + ]: 1 : RTE_TEST_ASSERT_EQUAL(data.invoked, false,
704 : : "Inserted callback is invoked on mempool population");
705 : :
706 : : memset(&data, 0, sizeof(data));
707 : 1 : sdata[0].invoked = false;
708 : 1 : rte_mempool_free(mp);
709 : : mp = NULL;
710 [ - + ]: 1 : RTE_TEST_ASSERT_EQUAL(sdata[0].invoked, false,
711 : : "Callback that unregistered itself was called");
712 [ - + ]: 1 : RTE_TEST_ASSERT_EQUAL(sdata[1].ret, -EEXIST,
713 : : "New callback inserted twice");
714 [ - + ]: 1 : RTE_TEST_ASSERT_EQUAL(data.invoked, true,
715 : : "Inserted callback is not invoked on mempool destruction");
716 : :
717 : 1 : rte_mempool_event_callback_unregister(test_mempool_events_cb, &data);
718 [ + + ]: 3 : for (i = 0; i < RTE_DIM(sdata); i++)
719 : 2 : rte_mempool_event_callback_unregister
720 : 2 : (test_mempool_events_safety_cb, &sdata[i]);
721 : : ret = TEST_SUCCESS;
722 : :
723 : 1 : exit:
724 : : /* cleanup, don't care which callbacks are already removed */
725 : 1 : rte_mempool_event_callback_unregister(test_mempool_events_cb, &data);
726 [ + + ]: 3 : for (i = 0; i < RTE_DIM(sdata); i++)
727 : 2 : rte_mempool_event_callback_unregister
728 : 2 : (test_mempool_events_safety_cb, &sdata[i]);
729 : : /* in case of failure before the planned destruction */
730 : 1 : rte_mempool_free(mp);
731 : : return ret;
732 : :
733 : : #pragma pop_macro("RTE_TEST_TRACE_FAILURE")
734 : : }
735 : :
736 : : #pragma push_macro("RTE_TEST_TRACE_FAILURE")
737 : : #undef RTE_TEST_TRACE_FAILURE
738 : : #define RTE_TEST_TRACE_FAILURE(...) do { \
739 : : ret = TEST_FAILED; \
740 : : goto exit; \
741 : : } while (0)
742 : :
743 : : static int
744 : 1 : test_mempool_flag_non_io_set_when_no_iova_contig_set(void)
745 : : {
746 : : const struct rte_memzone *mz = NULL;
747 : : void *virt;
748 : : rte_iova_t iova;
749 : : size_t size = MEMPOOL_ELT_SIZE * 16;
750 : : struct rte_mempool *mp = NULL;
751 : : int ret;
752 : :
753 : 1 : mz = rte_memzone_reserve("test_mempool", size, SOCKET_ID_ANY, 0);
754 [ - + ]: 1 : RTE_TEST_ASSERT_NOT_NULL(mz, "Cannot allocate memory");
755 : 1 : virt = mz->addr;
756 : 1 : iova = mz->iova;
757 : 1 : mp = rte_mempool_create_empty("empty", MEMPOOL_SIZE,
758 : : MEMPOOL_ELT_SIZE, 0, 0,
759 : : SOCKET_ID_ANY, RTE_MEMPOOL_F_NO_IOVA_CONTIG);
760 [ - + ]: 1 : RTE_TEST_ASSERT_NOT_NULL(mp, "Cannot create mempool: %s",
761 : : rte_strerror(rte_errno));
762 : 1 : rte_mempool_set_ops_byname(mp, rte_mbuf_best_mempool_ops(), NULL);
763 : :
764 [ - + ]: 1 : RTE_TEST_ASSERT(mp->flags & RTE_MEMPOOL_F_NON_IO,
765 : : "NON_IO flag is not set on an empty mempool");
766 : :
767 : : /*
768 : : * Always use valid IOVA so that populate() has no other reason
769 : : * to infer that the mempool cannot be used for IO.
770 : : */
771 : 1 : ret = rte_mempool_populate_iova(mp, virt, iova, size, NULL, NULL);
772 [ - + ]: 1 : RTE_TEST_ASSERT(ret > 0, "Failed to populate mempool: %s",
773 : : rte_strerror(-ret));
774 [ - + ]: 1 : RTE_TEST_ASSERT(mp->flags & RTE_MEMPOOL_F_NON_IO,
775 : : "NON_IO flag is not set when NO_IOVA_CONTIG is set");
776 : : ret = TEST_SUCCESS;
777 : 1 : exit:
778 : 1 : rte_mempool_free(mp);
779 : 1 : rte_memzone_free(mz);
780 : : return ret;
781 : : }
782 : :
783 : : static int
784 : 1 : test_mempool_flag_non_io_unset_when_populated_with_valid_iova(void)
785 : : {
786 : : const struct rte_memzone *mz = NULL;
787 : : void *virt;
788 : : rte_iova_t iova;
789 : 1 : size_t total_size = MEMPOOL_ELT_SIZE * MEMPOOL_SIZE;
790 : 1 : size_t block_size = total_size / 3;
791 : : struct rte_mempool *mp = NULL;
792 : : int ret;
793 : :
794 : : /*
795 : : * Since objects from the pool are never used in the test,
796 : : * we don't care for contiguous IOVA, on the other hand,
797 : : * requiring it could cause spurious test failures.
798 : : */
799 : 1 : mz = rte_memzone_reserve("test_mempool", total_size, SOCKET_ID_ANY, 0);
800 [ - + ]: 1 : RTE_TEST_ASSERT_NOT_NULL(mz, "Cannot allocate memory");
801 : 1 : virt = mz->addr;
802 : 1 : iova = mz->iova;
803 : 1 : mp = rte_mempool_create_empty("empty", MEMPOOL_SIZE,
804 : : MEMPOOL_ELT_SIZE, 0, 0,
805 : : SOCKET_ID_ANY, 0);
806 [ - + ]: 1 : RTE_TEST_ASSERT_NOT_NULL(mp, "Cannot create mempool: %s",
807 : : rte_strerror(rte_errno));
808 : :
809 [ - + ]: 1 : RTE_TEST_ASSERT(mp->flags & RTE_MEMPOOL_F_NON_IO,
810 : : "NON_IO flag is not set on an empty mempool");
811 : :
812 : 1 : ret = rte_mempool_populate_iova(mp, RTE_PTR_ADD(virt, 1 * block_size),
813 : : RTE_BAD_IOVA, block_size, NULL, NULL);
814 [ - + ]: 1 : RTE_TEST_ASSERT(ret > 0, "Failed to populate mempool: %s",
815 : : rte_strerror(-ret));
816 [ - + ]: 1 : RTE_TEST_ASSERT(mp->flags & RTE_MEMPOOL_F_NON_IO,
817 : : "NON_IO flag is not set when mempool is populated with only RTE_BAD_IOVA");
818 : :
819 : 1 : ret = rte_mempool_populate_iova(mp, virt, iova, block_size, NULL, NULL);
820 [ - + ]: 1 : RTE_TEST_ASSERT(ret > 0, "Failed to populate mempool: %s",
821 : : rte_strerror(-ret));
822 [ - + ]: 1 : RTE_TEST_ASSERT(!(mp->flags & RTE_MEMPOOL_F_NON_IO),
823 : : "NON_IO flag is not unset when mempool is populated with valid IOVA");
824 : :
825 : 1 : ret = rte_mempool_populate_iova(mp, RTE_PTR_ADD(virt, 2 * block_size),
826 : : RTE_BAD_IOVA, block_size, NULL, NULL);
827 [ - + ]: 1 : RTE_TEST_ASSERT(ret > 0, "Failed to populate mempool: %s",
828 : : rte_strerror(-ret));
829 [ - + ]: 1 : RTE_TEST_ASSERT(!(mp->flags & RTE_MEMPOOL_F_NON_IO),
830 : : "NON_IO flag is set even when some objects have valid IOVA");
831 : : ret = TEST_SUCCESS;
832 : :
833 : 1 : exit:
834 : 1 : rte_mempool_free(mp);
835 : 1 : rte_memzone_free(mz);
836 : : return ret;
837 : : }
838 : :
839 : : #pragma pop_macro("RTE_TEST_TRACE_FAILURE")
840 : :
841 : : static int
842 : 1 : test_mempool(void)
843 : : {
844 : : int ret = -1;
845 : : uint32_t nb_objs = 0;
846 : : uint32_t nb_mem_chunks = 0;
847 : : size_t alignment = 0;
848 : : struct rte_mempool *mp_cache = NULL;
849 : : struct rte_mempool *mp_nocache = NULL;
850 : : struct rte_mempool *mp_stack_anon = NULL;
851 : : struct rte_mempool *mp_stack_mempool_iter = NULL;
852 : : struct rte_mempool *mp_stack = NULL;
853 : : struct rte_mempool *default_pool = NULL;
854 : : struct rte_mempool *mp_alignment = NULL;
855 : 1 : struct mp_data cb_arg = {
856 : : .ret = -1
857 : : };
858 : 1 : const char *default_pool_ops = rte_mbuf_best_mempool_ops();
859 : 1 : struct rte_mempool_mem_range_info mem_range = { 0 };
860 : :
861 : : /* create a mempool (without cache) */
862 : 1 : mp_nocache = rte_mempool_create("test_nocache", MEMPOOL_SIZE,
863 : : MEMPOOL_ELT_SIZE, 0, 0,
864 : : NULL, NULL,
865 : : my_obj_init, NULL,
866 : : SOCKET_ID_ANY, 0);
867 : :
868 [ - + ]: 1 : if (mp_nocache == NULL) {
869 : : printf("cannot allocate mp_nocache mempool\n");
870 : 0 : GOTO_ERR(ret, err);
871 : : }
872 : :
873 : : /* create a mempool (with cache) */
874 : 1 : mp_cache = rte_mempool_create("test_cache", MEMPOOL_SIZE,
875 : : MEMPOOL_ELT_SIZE,
876 : : RTE_MEMPOOL_CACHE_MAX_SIZE, 0,
877 : : NULL, NULL,
878 : : my_obj_init, NULL,
879 : : SOCKET_ID_ANY, 0);
880 : :
881 [ - + ]: 1 : if (mp_cache == NULL) {
882 : : printf("cannot allocate mp_cache mempool\n");
883 : 0 : GOTO_ERR(ret, err);
884 : : }
885 : :
886 : : /* create an empty mempool */
887 : 1 : mp_stack_anon = rte_mempool_create_empty("test_stack_anon",
888 : 1 : MEMPOOL_SIZE,
889 : : MEMPOOL_ELT_SIZE,
890 : : RTE_MEMPOOL_CACHE_MAX_SIZE, 0,
891 : : SOCKET_ID_ANY, 0);
892 : :
893 [ - + ]: 1 : if (mp_stack_anon == NULL)
894 : 0 : GOTO_ERR(ret, err);
895 : :
896 : : /* populate an empty mempool */
897 : 1 : ret = rte_mempool_populate_anon(mp_stack_anon);
898 : : printf("%s ret = %d\n", __func__, ret);
899 [ - + ]: 1 : if (ret < 0)
900 : 0 : GOTO_ERR(ret, err);
901 : :
902 : : /* Try to populate when already populated */
903 : 1 : ret = rte_mempool_populate_anon(mp_stack_anon);
904 [ - + ]: 1 : if (ret != 0)
905 : 0 : GOTO_ERR(ret, err);
906 : :
907 : : /* create a mempool */
908 : 1 : mp_stack_mempool_iter = rte_mempool_create("test_iter_obj",
909 : 1 : MEMPOOL_SIZE,
910 : : MEMPOOL_ELT_SIZE,
911 : : RTE_MEMPOOL_CACHE_MAX_SIZE, 0,
912 : : NULL, NULL,
913 : : my_obj_init, NULL,
914 : : SOCKET_ID_ANY, 0);
915 : :
916 [ - + ]: 1 : if (mp_stack_mempool_iter == NULL)
917 : 0 : GOTO_ERR(ret, err);
918 : :
919 : : /* test to initialize mempool objects and memory */
920 : 1 : nb_objs = rte_mempool_obj_iter(mp_stack_mempool_iter, my_obj_init,
921 : : NULL);
922 [ - + ]: 1 : if (nb_objs == 0)
923 : 0 : GOTO_ERR(ret, err);
924 : :
925 : 1 : nb_mem_chunks = rte_mempool_mem_iter(mp_stack_mempool_iter,
926 : : test_mp_mem_init, &cb_arg);
927 [ + - - + ]: 1 : if (nb_mem_chunks == 0 || cb_arg.ret < 0)
928 : 0 : GOTO_ERR(ret, err);
929 : :
930 : : /* create a mempool with an external handler */
931 : 1 : mp_stack = rte_mempool_create_empty("test_stack",
932 : 1 : MEMPOOL_SIZE,
933 : : MEMPOOL_ELT_SIZE,
934 : : RTE_MEMPOOL_CACHE_MAX_SIZE, 0,
935 : : SOCKET_ID_ANY, 0);
936 : :
937 [ - + ]: 1 : if (mp_stack == NULL) {
938 : : printf("cannot allocate mp_stack mempool\n");
939 : 0 : GOTO_ERR(ret, err);
940 : : }
941 [ - + ]: 1 : if (rte_mempool_set_ops_byname(mp_stack, "stack", NULL) < 0) {
942 : : printf("cannot set stack handler\n");
943 : 0 : GOTO_ERR(ret, err);
944 : : }
945 [ - + ]: 1 : if (rte_mempool_populate_default(mp_stack) < 0) {
946 : : printf("cannot populate mp_stack mempool\n");
947 : 0 : GOTO_ERR(ret, err);
948 : : }
949 : 1 : rte_mempool_obj_iter(mp_stack, my_obj_init, NULL);
950 : :
951 : : /* Create a mempool based on Default handler */
952 : : printf("Testing %s mempool handler\n", default_pool_ops);
953 : 1 : default_pool = rte_mempool_create_empty("default_pool",
954 : 1 : MEMPOOL_SIZE,
955 : : MEMPOOL_ELT_SIZE,
956 : : RTE_MEMPOOL_CACHE_MAX_SIZE, 0,
957 : : SOCKET_ID_ANY, 0);
958 : :
959 [ - + ]: 1 : if (default_pool == NULL) {
960 : : printf("cannot allocate default mempool\n");
961 : 0 : GOTO_ERR(ret, err);
962 : : }
963 [ - + ]: 1 : if (rte_mempool_set_ops_byname(default_pool,
964 : : default_pool_ops, NULL) < 0) {
965 : : printf("cannot set %s handler\n", default_pool_ops);
966 : 0 : GOTO_ERR(ret, err);
967 : : }
968 [ - + ]: 1 : if (rte_mempool_populate_default(default_pool) < 0) {
969 : : printf("cannot populate %s mempool\n", default_pool_ops);
970 : 0 : GOTO_ERR(ret, err);
971 : : }
972 : 1 : rte_mempool_obj_iter(default_pool, my_obj_init, NULL);
973 : :
974 [ - + ]: 1 : if (rte_mempool_get_mem_range(default_pool, &mem_range)) {
975 : : printf("cannot get mem range from default mempool\n");
976 : 0 : GOTO_ERR(ret, err);
977 : : }
978 : :
979 [ - + ]: 1 : if (rte_mempool_get_mem_range(NULL, NULL) != -EINVAL) {
980 : : printf("rte_mempool_get_mem_range failed to return -EINVAL "
981 : : "when passed invalid arguments\n");
982 : 0 : GOTO_ERR(ret, err);
983 : : }
984 : :
985 [ + - ]: 1 : if (mem_range.start == NULL || mem_range.length <
986 [ - + ]: 1 : (MEMPOOL_SIZE * MEMPOOL_ELT_SIZE)) {
987 : : printf("mem range of default mempool is invalid\n");
988 : 0 : GOTO_ERR(ret, err);
989 : : }
990 : :
991 : : /* by default mempool objects are aligned by RTE_MEMPOOL_ALIGN */
992 : 1 : alignment = rte_mempool_get_obj_alignment(default_pool);
993 [ - + ]: 1 : if (alignment != RTE_MEMPOOL_ALIGN) {
994 : : printf("rte_mempool_get_obj_alignment returned wrong value, "
995 : : "expected %zu, returned %zu\n",
996 : : (size_t)RTE_MEMPOOL_ALIGN, alignment);
997 : 0 : GOTO_ERR(ret, err);
998 : : }
999 : :
1000 : : /* create a mempool with a RTE_MEMPOOL_F_NO_CACHE_ALIGN flag */
1001 : 1 : mp_alignment = rte_mempool_create("test_alignment",
1002 : : 1, 8, /* the small size guarantees single memory chunk */
1003 : : 0, 0, NULL, NULL, my_obj_init, NULL,
1004 : : SOCKET_ID_ANY, RTE_MEMPOOL_F_NO_CACHE_ALIGN);
1005 : :
1006 [ - + ]: 1 : if (mp_alignment == NULL) {
1007 : : printf("cannot allocate mempool with "
1008 : : "RTE_MEMPOOL_F_NO_CACHE_ALIGN flag\n");
1009 : 0 : GOTO_ERR(ret, err);
1010 : : }
1011 : :
1012 : : /* mempool was created with RTE_MEMPOOL_F_NO_CACHE_ALIGN
1013 : : * and minimum alignment is expected which is sizeof(uint64_t)
1014 : : */
1015 : 1 : alignment = rte_mempool_get_obj_alignment(mp_alignment);
1016 [ - + ]: 1 : if (alignment != sizeof(uint64_t)) {
1017 : : printf("rte_mempool_get_obj_alignment returned wrong value, "
1018 : : "expected %zu, returned %zu\n",
1019 : : (size_t)sizeof(uint64_t), alignment);
1020 : 0 : GOTO_ERR(ret, err);
1021 : : }
1022 : :
1023 : 1 : alignment = rte_mempool_get_obj_alignment(NULL);
1024 [ - + ]: 1 : if (alignment != 0) {
1025 : : printf("rte_mempool_get_obj_alignment failed to return 0 for "
1026 : : " an invalid mempool\n");
1027 : 0 : GOTO_ERR(ret, err);
1028 : : }
1029 : :
1030 [ - + ]: 1 : if (rte_mempool_get_mem_range(mp_alignment, &mem_range)) {
1031 : : printf("cannot get mem range from mempool\n");
1032 : 0 : GOTO_ERR(ret, err);
1033 : : }
1034 : :
1035 [ - + ]: 1 : if (!mem_range.is_contiguous) {
1036 : : printf("mempool not contiguous\n");
1037 : 0 : GOTO_ERR(ret, err);
1038 : : }
1039 : :
1040 : : /* retrieve the mempool from its name */
1041 [ - + ]: 1 : if (rte_mempool_lookup("test_nocache") != mp_nocache) {
1042 : : printf("Cannot lookup mempool from its name\n");
1043 : 0 : GOTO_ERR(ret, err);
1044 : : }
1045 : :
1046 : : printf("Walk into mempools:\n");
1047 : 1 : rte_mempool_walk(walk_cb, NULL);
1048 : :
1049 : 1 : rte_mempool_list_dump(stdout);
1050 : :
1051 : : /* basic tests without cache */
1052 [ - + ]: 1 : if (test_mempool_basic(mp_nocache, 0) < 0)
1053 : 0 : GOTO_ERR(ret, err);
1054 : :
1055 : : /* basic tests with cache */
1056 [ - + ]: 1 : if (test_mempool_basic(mp_cache, 0) < 0)
1057 : 0 : GOTO_ERR(ret, err);
1058 : :
1059 : : /* basic tests with user-owned cache */
1060 [ - + ]: 1 : if (test_mempool_basic(mp_nocache, 1) < 0)
1061 : 0 : GOTO_ERR(ret, err);
1062 : :
1063 : : /* more basic tests without cache */
1064 [ - + ]: 1 : if (test_mempool_basic_ex(mp_nocache) < 0)
1065 : 0 : GOTO_ERR(ret, err);
1066 : :
1067 : : /* mempool operation test based on single producer and single consumer */
1068 [ - + ]: 1 : if (test_mempool_sp_sc() < 0)
1069 : 0 : GOTO_ERR(ret, err);
1070 : :
1071 [ - + ]: 1 : if (test_mempool_creation_with_exceeded_cache_size() < 0)
1072 : 0 : GOTO_ERR(ret, err);
1073 : :
1074 [ - + ]: 1 : if (test_mempool_creation_with_invalid_flags() < 0)
1075 : 0 : GOTO_ERR(ret, err);
1076 : :
1077 [ - + ]: 1 : if (test_mempool_same_name_twice_creation() < 0)
1078 : 0 : GOTO_ERR(ret, err);
1079 : :
1080 : : /* test the stack handler */
1081 [ - + ]: 1 : if (test_mempool_basic(mp_stack, 1) < 0)
1082 : 0 : GOTO_ERR(ret, err);
1083 : :
1084 [ - + ]: 1 : if (test_mempool_basic(default_pool, 1) < 0)
1085 : 0 : GOTO_ERR(ret, err);
1086 : :
1087 : : /* test mempool event callbacks */
1088 [ - + ]: 1 : if (test_mempool_events(rte_mempool_populate_default) < 0)
1089 : 0 : GOTO_ERR(ret, err);
1090 [ - + ]: 1 : if (test_mempool_events(rte_mempool_populate_anon) < 0)
1091 : 0 : GOTO_ERR(ret, err);
1092 [ - + ]: 1 : if (test_mempool_events_safety() < 0)
1093 : 0 : GOTO_ERR(ret, err);
1094 : :
1095 : : /* test NON_IO flag inference */
1096 [ - + ]: 1 : if (test_mempool_flag_non_io_set_when_no_iova_contig_set() < 0)
1097 : 0 : GOTO_ERR(ret, err);
1098 [ - + ]: 1 : if (test_mempool_flag_non_io_unset_when_populated_with_valid_iova() < 0)
1099 : 0 : GOTO_ERR(ret, err);
1100 : :
1101 : 1 : rte_mempool_list_dump(stdout);
1102 : :
1103 : : ret = 0;
1104 : :
1105 : 1 : err:
1106 : 1 : rte_mempool_free(mp_nocache);
1107 : 1 : rte_mempool_free(mp_cache);
1108 : 1 : rte_mempool_free(mp_stack_anon);
1109 : 1 : rte_mempool_free(mp_stack_mempool_iter);
1110 : 1 : rte_mempool_free(mp_stack);
1111 : 1 : rte_mempool_free(default_pool);
1112 : 1 : rte_mempool_free(mp_alignment);
1113 : :
1114 : 1 : return ret;
1115 : : }
1116 : :
1117 : 252 : REGISTER_FAST_TEST(mempool_autotest, false, true, test_mempool);
|