LCOV - code coverage report
Current view: top level - app/test - test_mempool.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 310 384 80.7 %
Date: 2025-01-02 22:41:34 Functions: 21 21 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 167 288 58.0 %

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

Generated by: LCOV version 1.14