LCOV - code coverage report
Current view: top level - app/test - test_mempool.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 307 381 80.6 %
Date: 2026-10-01 18:19:24 Functions: 21 21 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 165 288 57.3 %

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

Generated by: LCOV version 1.14