LCOV - code coverage report
Current view: top level - app/test - test_mempool.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 308 382 80.6 %
Date: 2026-11-01 19:11:22 Functions: 21 21 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 166 290 57.2 %

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

Generated by: LCOV version 1.14