LCOV - code coverage report
Current view: top level - lib/mempool - rte_mempool_ops.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 52 67 77.6 %
Date: 2026-11-01 19:11:22 Functions: 7 8 87.5 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 21 40 52.5 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  * Copyright(c) 2016 Intel Corporation.
       3                 :            :  * Copyright(c) 2016 6WIND S.A.
       4                 :            :  */
       5                 :            : 
       6                 :            : #include <stdio.h>
       7                 :            : #include <string.h>
       8                 :            : 
       9                 :            : #include <eal_export.h>
      10                 :            : #include <rte_string_fns.h>
      11                 :            : #include <rte_mempool.h>
      12                 :            : #include <rte_errno.h>
      13                 :            : #include <dev_driver.h>
      14                 :            : 
      15                 :            : #include "mempool_trace.h"
      16                 :            : 
      17                 :            : /* indirect jump table to support external memory pools. */
      18                 :            : RTE_EXPORT_SYMBOL(rte_mempool_ops_table)
      19                 :            : struct rte_mempool_ops_table rte_mempool_ops_table = {
      20                 :            :         .sl =  RTE_SPINLOCK_INITIALIZER,
      21                 :            :         .num_ops = 0
      22                 :            : };
      23                 :            : 
      24                 :            : /* add a new ops struct in rte_mempool_ops_table, return its index. */
      25                 :            : RTE_EXPORT_SYMBOL(rte_mempool_register_ops)
      26                 :            : int
      27                 :       5185 : rte_mempool_register_ops(const struct rte_mempool_ops *h)
      28                 :            : {
      29                 :            :         struct rte_mempool_ops *ops;
      30                 :            :         unsigned int ops_index;
      31                 :            : 
      32                 :            :         rte_spinlock_lock(&rte_mempool_ops_table.sl);
      33                 :            : 
      34         [ -  + ]:       5185 :         if (rte_mempool_ops_table.num_ops >=
      35                 :            :                         RTE_MEMPOOL_MAX_OPS_IDX) {
      36                 :            :                 rte_spinlock_unlock(&rte_mempool_ops_table.sl);
      37                 :          0 :                 RTE_MEMPOOL_LOG(ERR,
      38                 :            :                         "Maximum number of mempool ops structs exceeded");
      39                 :          0 :                 return -ENOSPC;
      40                 :            :         }
      41                 :            : 
      42   [ +  -  +  - ]:       5185 :         if (h->alloc == NULL || h->enqueue == NULL ||
      43   [ +  -  -  + ]:       5185 :                         h->dequeue == NULL || h->get_count == NULL) {
      44                 :            :                 rte_spinlock_unlock(&rte_mempool_ops_table.sl);
      45                 :          0 :                 RTE_MEMPOOL_LOG(ERR,
      46                 :            :                         "Missing callback while registering mempool ops");
      47                 :          0 :                 return -EINVAL;
      48                 :            :         }
      49                 :            : 
      50         [ -  + ]:       5185 :         if (strnlen(h->name, sizeof(h->name)) > sizeof(ops->name) - 1) {
      51                 :            :                 rte_spinlock_unlock(&rte_mempool_ops_table.sl);
      52                 :            :                 char name[sizeof(h->name) + 1];
      53                 :            :                 strlcpy(name, h->name, sizeof(name));
      54                 :          0 :                 RTE_MEMPOOL_LOG(ERR, "%s(): mempool_ops <%s>: name too long",
      55                 :            :                                 __func__, name);
      56                 :            :                 return -ENAMETOOLONG;
      57                 :            :         }
      58                 :            : 
      59         [ +  + ]:      46665 :         for (ops_index = 0; ops_index < rte_mempool_ops_table.num_ops; ops_index++) {
      60         [ -  + ]:      41480 :                 if (!strcmp(h->name, rte_mempool_ops_table.ops[ops_index].name)) {
      61                 :            :                         rte_spinlock_unlock(&rte_mempool_ops_table.sl);
      62                 :          0 :                         RTE_MEMPOOL_LOG(ERR, "%s(): mempool_ops <%s>: name exists",
      63                 :            :                                         __func__, h->name);
      64                 :          0 :                         return -EEXIST;
      65                 :            :                 }
      66                 :            :         }
      67                 :            : 
      68                 :       5185 :         ops_index = rte_mempool_ops_table.num_ops++;
      69                 :            :         ops = &rte_mempool_ops_table.ops[ops_index];
      70                 :       5185 :         strlcpy(ops->name, h->name, sizeof(ops->name));
      71                 :            :         ops->alloc = h->alloc;
      72                 :       5185 :         ops->free = h->free;
      73                 :       5185 :         ops->enqueue = h->enqueue;
      74                 :       5185 :         ops->dequeue = h->dequeue;
      75                 :       5185 :         ops->get_count = h->get_count;
      76                 :       5185 :         ops->calc_mem_size = h->calc_mem_size;
      77                 :       5185 :         ops->populate = h->populate;
      78                 :       5185 :         ops->get_info = h->get_info;
      79                 :       5185 :         ops->dequeue_contig_blocks = h->dequeue_contig_blocks;
      80                 :            : 
      81                 :            :         rte_spinlock_unlock(&rte_mempool_ops_table.sl);
      82                 :            : 
      83                 :       5185 :         RTE_MEMPOOL_LOG(DEBUG, "Registered mempool ops <%s> at index %u", ops->name, ops_index);
      84                 :            : 
      85                 :       5185 :         return ops_index;
      86                 :            : }
      87                 :            : 
      88                 :            : /* wrapper to allocate an external mempool's private (pool) data. */
      89                 :            : int
      90         [ -  + ]:        128 : rte_mempool_ops_alloc(struct rte_mempool *mp)
      91                 :            : {
      92                 :            :         struct rte_mempool_ops *ops;
      93                 :            : 
      94                 :        128 :         rte_mempool_trace_ops_alloc(mp);
      95                 :        128 :         ops = rte_mempool_get_ops(mp->ops_index);
      96                 :        128 :         return ops->alloc(mp);
      97                 :            : }
      98                 :            : 
      99                 :            : /* wrapper to free an external pool ops. */
     100                 :            : void
     101         [ -  + ]:        123 : rte_mempool_ops_free(struct rte_mempool *mp)
     102                 :            : {
     103                 :            :         struct rte_mempool_ops *ops;
     104                 :            : 
     105                 :        123 :         rte_mempool_trace_ops_free(mp);
     106                 :        123 :         ops = rte_mempool_get_ops(mp->ops_index);
     107         [ +  - ]:        123 :         if (ops->free == NULL)
     108                 :            :                 return;
     109                 :        123 :         ops->free(mp);
     110                 :            : }
     111                 :            : 
     112                 :            : /* wrapper to get available objects in an external mempool. */
     113                 :            : unsigned int
     114                 :        673 : rte_mempool_ops_get_count(const struct rte_mempool *mp)
     115                 :            : {
     116                 :            :         struct rte_mempool_ops *ops;
     117                 :            : 
     118                 :        673 :         ops = rte_mempool_get_ops(mp->ops_index);
     119                 :        673 :         return ops->get_count(mp);
     120                 :            : }
     121                 :            : 
     122                 :            : /* wrapper to calculate the memory size required to store given number
     123                 :            :  * of objects
     124                 :            :  */
     125                 :            : ssize_t
     126                 :        129 : rte_mempool_ops_calc_mem_size(const struct rte_mempool *mp,
     127                 :            :                                 uint32_t obj_num, uint32_t pg_shift,
     128                 :            :                                 size_t *min_chunk_size, size_t *align)
     129                 :            : {
     130                 :            :         struct rte_mempool_ops *ops;
     131                 :            : 
     132                 :        129 :         ops = rte_mempool_get_ops(mp->ops_index);
     133                 :            : 
     134         [ +  - ]:        129 :         if (ops->calc_mem_size == NULL)
     135                 :        129 :                 return rte_mempool_op_calc_mem_size_default(mp, obj_num,
     136                 :            :                                 pg_shift, min_chunk_size, align);
     137                 :            : 
     138                 :          0 :         return ops->calc_mem_size(mp, obj_num, pg_shift, min_chunk_size, align);
     139                 :            : }
     140                 :            : 
     141                 :            : /* wrapper to populate memory pool objects using provided memory chunk */
     142                 :            : int
     143                 :       1086 : rte_mempool_ops_populate(struct rte_mempool *mp, unsigned int max_objs,
     144                 :            :                                 void *vaddr, rte_iova_t iova, size_t len,
     145                 :            :                                 rte_mempool_populate_obj_cb_t *obj_cb,
     146                 :            :                                 void *obj_cb_arg)
     147                 :            : {
     148                 :            :         struct rte_mempool_ops *ops;
     149                 :            : 
     150         [ -  + ]:       1086 :         ops = rte_mempool_get_ops(mp->ops_index);
     151                 :            : 
     152                 :       1086 :         rte_mempool_trace_ops_populate(mp, max_objs, vaddr, iova, len, obj_cb,
     153                 :            :                 obj_cb_arg);
     154         [ +  - ]:       1086 :         if (ops->populate == NULL)
     155                 :       1086 :                 return rte_mempool_op_populate_default(mp, max_objs, vaddr,
     156                 :            :                                                        iova, len, obj_cb,
     157                 :            :                                                        obj_cb_arg);
     158                 :            : 
     159                 :          0 :         return ops->populate(mp, max_objs, vaddr, iova, len, obj_cb,
     160                 :            :                              obj_cb_arg);
     161                 :            : }
     162                 :            : 
     163                 :            : /* wrapper to get additional mempool info */
     164                 :            : RTE_EXPORT_SYMBOL(rte_mempool_ops_get_info)
     165                 :            : int
     166                 :          0 : rte_mempool_ops_get_info(const struct rte_mempool *mp,
     167                 :            :                          struct rte_mempool_info *info)
     168                 :            : {
     169                 :            :         struct rte_mempool_ops *ops;
     170                 :            : 
     171                 :          0 :         ops = rte_mempool_get_ops(mp->ops_index);
     172                 :            : 
     173         [ #  # ]:          0 :         if (ops->get_info == NULL)
     174                 :            :                 return -ENOTSUP;
     175                 :          0 :         return ops->get_info(mp, info);
     176                 :            : }
     177                 :            : 
     178                 :            : 
     179                 :            : /* sets mempool ops previously registered by rte_mempool_register_ops. */
     180                 :            : RTE_EXPORT_SYMBOL(rte_mempool_set_ops_byname)
     181                 :            : int
     182                 :        169 : rte_mempool_set_ops_byname(struct rte_mempool *mp, const char *name,
     183                 :            :         void *pool_config)
     184                 :            : {
     185                 :            :         struct rte_mempool_ops *ops = NULL;
     186                 :            :         unsigned i;
     187                 :            : 
     188                 :            :         /* too late, the mempool is already populated. */
     189         [ +  - ]:        169 :         if (mp->flags & RTE_MEMPOOL_F_POOL_CREATED)
     190                 :            :                 return -EEXIST;
     191                 :            : 
     192         [ +  - ]:       1533 :         for (i = 0; i < rte_mempool_ops_table.num_ops; i++) {
     193                 :       1533 :                 if (!strcmp(name,
     194         [ +  + ]:       1533 :                                 rte_mempool_ops_table.ops[i].name)) {
     195                 :        169 :                         ops = &rte_mempool_ops_table.ops[i];
     196                 :        169 :                         break;
     197                 :            :                 }
     198                 :            :         }
     199                 :            : 
     200         [ -  + ]:        169 :         if (ops == NULL) {
     201                 :          0 :                 RTE_MEMPOOL_LOG(DEBUG,
     202                 :            :                                 "Cannot set unknown mempool ops <%s>, of %u ops registered",
     203                 :            :                                 name, i);
     204                 :          0 :                 return -EINVAL;
     205                 :            :         }
     206                 :            : 
     207                 :        169 :         mp->ops_index = i;
     208         [ -  + ]:        169 :         mp->pool_config = pool_config;
     209                 :        169 :         rte_mempool_trace_set_ops_byname(mp, name, pool_config);
     210                 :        169 :         return 0;
     211                 :            : }

Generated by: LCOV version 1.14