LCOV - code coverage report
Current view: top level - lib/mempool - rte_mempool.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 407 516 78.9 %
Date: 2024-01-22 16:13:49 Functions: 38 44 86.4 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 201 286 70.3 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  * Copyright(c) 2010-2014 Intel Corporation.
       3                 :            :  * Copyright(c) 2016 6WIND S.A.
       4                 :            :  * Copyright(c) 2022 SmartShare Systems
       5                 :            :  */
       6                 :            : 
       7                 :            : #include <stdbool.h>
       8                 :            : #include <stdlib.h>
       9                 :            : #include <stdio.h>
      10                 :            : #include <string.h>
      11                 :            : #include <stdint.h>
      12                 :            : #include <unistd.h>
      13                 :            : #include <inttypes.h>
      14                 :            : #include <errno.h>
      15                 :            : #include <sys/queue.h>
      16                 :            : 
      17                 :            : #include <rte_common.h>
      18                 :            : #include <rte_log.h>
      19                 :            : #include <rte_debug.h>
      20                 :            : #include <rte_memory.h>
      21                 :            : #include <rte_memzone.h>
      22                 :            : #include <rte_malloc.h>
      23                 :            : #include <rte_eal.h>
      24                 :            : #include <rte_eal_memconfig.h>
      25                 :            : #include <rte_errno.h>
      26                 :            : #include <rte_string_fns.h>
      27                 :            : #include <rte_tailq.h>
      28                 :            : #include <rte_eal_paging.h>
      29                 :            : #include <rte_telemetry.h>
      30                 :            : 
      31                 :            : #include "mempool_trace.h"
      32                 :            : #include "rte_mempool.h"
      33                 :            : 
      34         [ -  + ]:        235 : RTE_LOG_REGISTER_DEFAULT(rte_mempool_logtype, INFO);
      35                 :            : 
      36                 :            : TAILQ_HEAD(rte_mempool_list, rte_tailq_entry);
      37                 :            : 
      38                 :            : static struct rte_tailq_elem rte_mempool_tailq = {
      39                 :            :         .name = "RTE_MEMPOOL",
      40                 :            : };
      41         [ -  + ]:        235 : EAL_REGISTER_TAILQ(rte_mempool_tailq)
      42                 :            : 
      43                 :            : TAILQ_HEAD(mempool_callback_tailq, mempool_callback_data);
      44                 :            : 
      45                 :            : static struct mempool_callback_tailq callback_tailq =
      46                 :            :                 TAILQ_HEAD_INITIALIZER(callback_tailq);
      47                 :            : 
      48                 :            : /* Invoke all registered mempool event callbacks. */
      49                 :            : static void
      50                 :            : mempool_event_callback_invoke(enum rte_mempool_event event,
      51                 :            :                               struct rte_mempool *mp);
      52                 :            : 
      53                 :            : #define CACHE_FLUSHTHRESH_MULTIPLIER 1.5
      54                 :            : #define CALC_CACHE_FLUSHTHRESH(c)       \
      55                 :            :         ((typeof(c))((c) * CACHE_FLUSHTHRESH_MULTIPLIER))
      56                 :            : 
      57                 :            : #if defined(RTE_ARCH_X86)
      58                 :            : /*
      59                 :            :  * return the greatest common divisor between a and b (fast algorithm)
      60                 :            :  */
      61                 :            : static unsigned get_gcd(unsigned a, unsigned b)
      62                 :            : {
      63                 :            :         unsigned c;
      64                 :            : 
      65                 :        188 :         if (0 == a)
      66                 :            :                 return b;
      67         [ +  - ]:        188 :         if (0 == b)
      68                 :            :                 return a;
      69                 :            : 
      70         [ +  + ]:        188 :         if (a < b) {
      71                 :            :                 c = a;
      72                 :            :                 a = b;
      73                 :            :                 b = c;
      74                 :            :         }
      75                 :            : 
      76         [ +  + ]:        494 :         while (b != 0) {
      77                 :        306 :                 c = a % b;
      78                 :            :                 a = b;
      79                 :            :                 b = c;
      80                 :            :         }
      81                 :            : 
      82                 :            :         return a;
      83                 :            : }
      84                 :            : 
      85                 :            : /*
      86                 :            :  * Depending on memory configuration on x86 arch, objects addresses are spread
      87                 :            :  * between channels and ranks in RAM: the pool allocator will add
      88                 :            :  * padding between objects. This function return the new size of the
      89                 :            :  * object.
      90                 :            :  */
      91                 :            : static unsigned int
      92                 :        110 : arch_mem_object_align(unsigned int obj_size)
      93                 :            : {
      94                 :            :         unsigned nrank, nchan;
      95                 :            :         unsigned new_obj_size;
      96                 :            : 
      97                 :            :         /* get number of channels */
      98                 :        110 :         nchan = rte_memory_get_nchannel();
      99         [ +  - ]:        110 :         if (nchan == 0)
     100                 :            :                 nchan = 4;
     101                 :            : 
     102                 :        110 :         nrank = rte_memory_get_nrank();
     103                 :            :         if (nrank == 0)
     104                 :            :                 nrank = 1;
     105                 :            : 
     106                 :            :         /* process new object size */
     107                 :        110 :         new_obj_size = (obj_size + RTE_MEMPOOL_ALIGN_MASK) / RTE_MEMPOOL_ALIGN;
     108   [ +  -  +  + ]:        376 :         while (get_gcd(new_obj_size, nrank * nchan) != 1)
     109                 :         78 :                 new_obj_size++;
     110                 :        110 :         return new_obj_size * RTE_MEMPOOL_ALIGN;
     111                 :            : }
     112                 :            : #else
     113                 :            : static unsigned int
     114                 :            : arch_mem_object_align(unsigned int obj_size)
     115                 :            : {
     116                 :            :         return obj_size;
     117                 :            : }
     118                 :            : #endif
     119                 :            : 
     120                 :            : struct pagesz_walk_arg {
     121                 :            :         int socket_id;
     122                 :            :         size_t min;
     123                 :            : };
     124                 :            : 
     125                 :            : static int
     126                 :       8544 : find_min_pagesz(const struct rte_memseg_list *msl, void *arg)
     127                 :            : {
     128                 :            :         struct pagesz_walk_arg *wa = arg;
     129                 :            :         bool valid;
     130                 :            : 
     131                 :            :         /*
     132                 :            :          * we need to only look at page sizes available for a particular socket
     133                 :            :          * ID.  so, we either need an exact match on socket ID (can match both
     134                 :            :          * native and external memory), or, if SOCKET_ID_ANY was specified as a
     135                 :            :          * socket ID argument, we must only look at native memory and ignore any
     136                 :            :          * page sizes associated with external memory.
     137                 :            :          */
     138                 :       8544 :         valid = msl->socket_id == wa->socket_id;
     139   [ +  +  -  + ]:       8544 :         valid |= wa->socket_id == SOCKET_ID_ANY && msl->external == 0;
     140                 :            : 
     141   [ +  +  +  + ]:       8544 :         if (valid && msl->page_sz < wa->min)
     142                 :       1068 :                 wa->min = msl->page_sz;
     143                 :            : 
     144                 :       8544 :         return 0;
     145                 :            : }
     146                 :            : 
     147                 :            : static size_t
     148                 :       1068 : get_min_page_size(int socket_id)
     149                 :            : {
     150                 :            :         struct pagesz_walk_arg wa;
     151                 :            : 
     152                 :       1068 :         wa.min = SIZE_MAX;
     153                 :       1068 :         wa.socket_id = socket_id;
     154                 :            : 
     155                 :       1068 :         rte_memseg_list_walk(find_min_pagesz, &wa);
     156                 :            : 
     157         [ -  + ]:       1068 :         return wa.min == SIZE_MAX ? (size_t) rte_mem_page_size() : wa.min;
     158                 :            : }
     159                 :            : 
     160                 :            : 
     161                 :            : static void
     162                 :   44258775 : mempool_add_elem(struct rte_mempool *mp, __rte_unused void *opaque,
     163                 :            :                  void *obj, rte_iova_t iova)
     164                 :            : {
     165                 :            :         struct rte_mempool_objhdr *hdr;
     166                 :            :         struct rte_mempool_objtlr *tlr __rte_unused;
     167                 :            : 
     168                 :            :         /* set mempool ptr in header */
     169                 :   44258775 :         hdr = RTE_PTR_SUB(obj, sizeof(*hdr));
     170                 :   44258775 :         hdr->mp = mp;
     171                 :   44258775 :         hdr->iova = iova;
     172                 :   44258775 :         STAILQ_INSERT_TAIL(&mp->elt_list, hdr, next);
     173                 :   44258775 :         mp->populated_size++;
     174                 :            : 
     175                 :            : #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
     176                 :            :         hdr->cookie = RTE_MEMPOOL_HEADER_COOKIE2;
     177                 :            :         tlr = rte_mempool_get_trailer(obj);
     178                 :            :         tlr->cookie = RTE_MEMPOOL_TRAILER_COOKIE;
     179                 :            : #endif
     180                 :   44258775 : }
     181                 :            : 
     182                 :            : /* call obj_cb() for each mempool element */
     183                 :            : uint32_t
     184                 :         61 : rte_mempool_obj_iter(struct rte_mempool *mp,
     185                 :            :         rte_mempool_obj_cb_t *obj_cb, void *obj_cb_arg)
     186                 :            : {
     187                 :            :         struct rte_mempool_objhdr *hdr;
     188                 :            :         void *obj;
     189                 :            :         unsigned n = 0;
     190                 :            : 
     191         [ +  + ]:      83068 :         STAILQ_FOREACH(hdr, &mp->elt_list, next) {
     192                 :      83007 :                 obj = (char *)hdr + sizeof(*hdr);
     193                 :      83007 :                 obj_cb(mp, obj_cb_arg, obj, n);
     194                 :      83007 :                 n++;
     195                 :            :         }
     196                 :            : 
     197                 :         61 :         return n;
     198                 :            : }
     199                 :            : 
     200                 :            : /* call mem_cb() for each mempool memory chunk */
     201                 :            : uint32_t
     202                 :          1 : rte_mempool_mem_iter(struct rte_mempool *mp,
     203                 :            :         rte_mempool_mem_cb_t *mem_cb, void *mem_cb_arg)
     204                 :            : {
     205                 :            :         struct rte_mempool_memhdr *hdr;
     206                 :            :         unsigned n = 0;
     207                 :            : 
     208         [ +  + ]:          3 :         STAILQ_FOREACH(hdr, &mp->mem_list, next) {
     209                 :          2 :                 mem_cb(mp, mem_cb_arg, hdr, n);
     210                 :          2 :                 n++;
     211                 :            :         }
     212                 :            : 
     213                 :          1 :         return n;
     214                 :            : }
     215                 :            : 
     216                 :            : /* get the header, trailer and total size of a mempool element. */
     217                 :            : uint32_t
     218                 :        112 : rte_mempool_calc_obj_size(uint32_t elt_size, uint32_t flags,
     219                 :            :         struct rte_mempool_objsz *sz)
     220                 :            : {
     221                 :            :         struct rte_mempool_objsz lsz;
     222                 :            : 
     223         [ +  - ]:        112 :         sz = (sz != NULL) ? sz : &lsz;
     224                 :            : 
     225                 :        112 :         sz->header_size = sizeof(struct rte_mempool_objhdr);
     226         [ +  + ]:        112 :         if ((flags & RTE_MEMPOOL_F_NO_CACHE_ALIGN) == 0)
     227                 :        111 :                 sz->header_size = RTE_ALIGN_CEIL(sz->header_size,
     228                 :            :                         RTE_MEMPOOL_ALIGN);
     229                 :            : 
     230                 :            : #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
     231                 :            :         sz->trailer_size = sizeof(struct rte_mempool_objtlr);
     232                 :            : #else
     233                 :        112 :         sz->trailer_size = 0;
     234                 :            : #endif
     235                 :            : 
     236                 :            :         /* element size is 8 bytes-aligned at least */
     237                 :        112 :         sz->elt_size = RTE_ALIGN_CEIL(elt_size, sizeof(uint64_t));
     238                 :            : 
     239                 :            :         /* expand trailer to next cache line */
     240         [ +  + ]:        112 :         if ((flags & RTE_MEMPOOL_F_NO_CACHE_ALIGN) == 0) {
     241                 :        111 :                 sz->total_size = sz->header_size + sz->elt_size +
     242                 :            :                         sz->trailer_size;
     243                 :        111 :                 sz->trailer_size += ((RTE_MEMPOOL_ALIGN -
     244                 :        111 :                                   (sz->total_size & RTE_MEMPOOL_ALIGN_MASK)) &
     245                 :            :                                  RTE_MEMPOOL_ALIGN_MASK);
     246                 :            :         }
     247                 :            : 
     248                 :            :         /*
     249                 :            :          * increase trailer to add padding between objects in order to
     250                 :            :          * spread them across memory channels/ranks
     251                 :            :          */
     252         [ +  + ]:        112 :         if ((flags & RTE_MEMPOOL_F_NO_SPREAD) == 0) {
     253                 :            :                 unsigned new_size;
     254                 :        110 :                 new_size = arch_mem_object_align
     255                 :        110 :                             (sz->header_size + sz->elt_size + sz->trailer_size);
     256                 :        110 :                 sz->trailer_size = new_size - sz->header_size - sz->elt_size;
     257                 :            :         }
     258                 :            : 
     259                 :            :         /* this is the size of an object, including header and trailer */
     260                 :        112 :         sz->total_size = sz->header_size + sz->elt_size + sz->trailer_size;
     261                 :            : 
     262                 :        112 :         return sz->total_size;
     263                 :            : }
     264                 :            : 
     265                 :            : /* free a memchunk allocated with rte_memzone_reserve() */
     266                 :            : static void
     267                 :         90 : rte_mempool_memchunk_mz_free(__rte_unused struct rte_mempool_memhdr *memhdr,
     268                 :            :         void *opaque)
     269                 :            : {
     270                 :            :         const struct rte_memzone *mz = opaque;
     271                 :         90 :         rte_memzone_free(mz);
     272                 :         90 : }
     273                 :            : 
     274                 :            : /* Free memory chunks used by a mempool. Objects must be in pool */
     275                 :            : static void
     276                 :         95 : rte_mempool_free_memchunks(struct rte_mempool *mp)
     277                 :            : {
     278                 :            :         struct rte_mempool_memhdr *memhdr;
     279                 :            :         void *elt;
     280                 :            : 
     281         [ +  + ]:   44251671 :         while (!STAILQ_EMPTY(&mp->elt_list)) {
     282                 :   44251576 :                 rte_mempool_ops_dequeue_bulk(mp, &elt, 1);
     283                 :            :                 (void)elt;
     284         [ +  + ]:   44251576 :                 STAILQ_REMOVE_HEAD(&mp->elt_list, next);
     285                 :   44251576 :                 mp->populated_size--;
     286                 :            :         }
     287                 :            : 
     288         [ +  + ]:       1138 :         while (!STAILQ_EMPTY(&mp->mem_list)) {
     289                 :            :                 memhdr = STAILQ_FIRST(&mp->mem_list);
     290         [ +  + ]:       1043 :                 STAILQ_REMOVE_HEAD(&mp->mem_list, next);
     291         [ +  + ]:       1043 :                 if (memhdr->free_cb != NULL)
     292                 :         93 :                         memhdr->free_cb(memhdr, memhdr->opaque);
     293                 :       1043 :                 rte_free(memhdr);
     294                 :       1043 :                 mp->nb_mem_chunks--;
     295                 :            :         }
     296                 :         95 : }
     297                 :            : 
     298                 :            : static int
     299                 :       1146 : mempool_ops_alloc_once(struct rte_mempool *mp)
     300                 :            : {
     301                 :            :         int ret;
     302                 :            : 
     303                 :            :         /* create the internal ring if not already done */
     304         [ +  + ]:       1146 :         if ((mp->flags & RTE_MEMPOOL_F_POOL_CREATED) == 0) {
     305                 :         99 :                 ret = rte_mempool_ops_alloc(mp);
     306         [ +  - ]:         99 :                 if (ret != 0)
     307                 :            :                         return ret;
     308                 :         99 :                 mp->flags |= RTE_MEMPOOL_F_POOL_CREATED;
     309                 :            :         }
     310                 :            :         return 0;
     311                 :            : }
     312                 :            : 
     313                 :            : /* Add objects in the pool, using a physically contiguous memory
     314                 :            :  * zone. Return the number of objects added, or a negative value
     315                 :            :  * on error.
     316                 :            :  */
     317                 :            : int
     318                 :       1049 : rte_mempool_populate_iova(struct rte_mempool *mp, char *vaddr,
     319                 :            :         rte_iova_t iova, size_t len, rte_mempool_memchunk_free_cb_t *free_cb,
     320                 :            :         void *opaque)
     321                 :            : {
     322                 :            :         unsigned i = 0;
     323                 :            :         size_t off;
     324                 :            :         struct rte_mempool_memhdr *memhdr;
     325                 :            :         int ret;
     326                 :            : 
     327                 :       1049 :         ret = mempool_ops_alloc_once(mp);
     328         [ +  - ]:       1049 :         if (ret != 0)
     329                 :            :                 return ret;
     330                 :            : 
     331                 :            :         /* mempool is already populated */
     332         [ +  - ]:       1049 :         if (mp->populated_size >= mp->size)
     333                 :            :                 return -ENOSPC;
     334                 :            : 
     335                 :       1049 :         memhdr = rte_zmalloc("MEMPOOL_MEMHDR", sizeof(*memhdr), 0);
     336         [ +  - ]:       1049 :         if (memhdr == NULL)
     337                 :            :                 return -ENOMEM;
     338                 :            : 
     339                 :       1049 :         memhdr->mp = mp;
     340                 :       1049 :         memhdr->addr = vaddr;
     341                 :       1049 :         memhdr->iova = iova;
     342                 :       1049 :         memhdr->len = len;
     343                 :       1049 :         memhdr->free_cb = free_cb;
     344                 :       1049 :         memhdr->opaque = opaque;
     345                 :            : 
     346         [ +  + ]:       1049 :         if (mp->flags & RTE_MEMPOOL_F_NO_CACHE_ALIGN)
     347                 :          2 :                 off = RTE_PTR_ALIGN_CEIL(vaddr, 8) - vaddr;
     348                 :            :         else
     349                 :       1047 :                 off = RTE_PTR_ALIGN_CEIL(vaddr, RTE_MEMPOOL_ALIGN) - vaddr;
     350                 :            : 
     351         [ -  + ]:       1049 :         if (off > len) {
     352                 :            :                 ret = 0;
     353                 :          0 :                 goto fail;
     354                 :            :         }
     355                 :            : 
     356         [ +  + ]:       1049 :         i = rte_mempool_ops_populate(mp, mp->size - mp->populated_size,
     357                 :       1049 :                 (char *)vaddr + off,
     358                 :            :                 (iova == RTE_BAD_IOVA) ? RTE_BAD_IOVA : (iova + off),
     359                 :            :                 len - off, mempool_add_elem, NULL);
     360                 :            : 
     361                 :            :         /* not enough room to store one object */
     362         [ -  + ]:       1049 :         if (i == 0) {
     363                 :            :                 ret = 0;
     364                 :          0 :                 goto fail;
     365                 :            :         }
     366                 :            : 
     367                 :       1049 :         STAILQ_INSERT_TAIL(&mp->mem_list, memhdr, next);
     368                 :       1049 :         mp->nb_mem_chunks++;
     369                 :            : 
     370                 :            :         /* Check if at least some objects in the pool are now usable for IO. */
     371   [ +  +  +  + ]:       1049 :         if (!(mp->flags & RTE_MEMPOOL_F_NO_IOVA_CONTIG) && iova != RTE_BAD_IOVA)
     372                 :       1046 :                 mp->flags &= ~RTE_MEMPOOL_F_NON_IO;
     373                 :            : 
     374                 :            :         /* Report the mempool as ready only when fully populated. */
     375         [ +  + ]:       1049 :         if (mp->populated_size >= mp->size)
     376                 :         97 :                 mempool_event_callback_invoke(RTE_MEMPOOL_EVENT_READY, mp);
     377                 :            : 
     378                 :       1049 :         rte_mempool_trace_populate_iova(mp, vaddr, iova, len, free_cb, opaque);
     379                 :       1049 :         return i;
     380                 :            : 
     381                 :          0 : fail:
     382                 :          0 :         rte_free(memhdr);
     383                 :          0 :         return ret;
     384                 :            : }
     385                 :            : 
     386                 :            : static rte_iova_t
     387                 :    2173960 : get_iova(void *addr)
     388                 :            : {
     389                 :            :         struct rte_memseg *ms;
     390                 :            : 
     391                 :            :         /* try registered memory first */
     392                 :    2173960 :         ms = rte_mem_virt2memseg(addr, NULL);
     393   [ +  +  -  + ]:    2173960 :         if (ms == NULL || ms->iova == RTE_BAD_IOVA)
     394                 :            :                 /* fall back to actual physical address */
     395                 :       1945 :                 return rte_mem_virt2iova(addr);
     396                 :    2172015 :         return ms->iova + RTE_PTR_DIFF(addr, ms->addr);
     397                 :            : }
     398                 :            : 
     399                 :            : /* Populate the mempool with a virtual area. Return the number of
     400                 :            :  * objects added, or a negative value on error.
     401                 :            :  */
     402                 :            : int
     403                 :         97 : rte_mempool_populate_virt(struct rte_mempool *mp, char *addr,
     404                 :            :         size_t len, size_t pg_sz, rte_mempool_memchunk_free_cb_t *free_cb,
     405                 :            :         void *opaque)
     406                 :            : {
     407                 :            :         rte_iova_t iova;
     408                 :            :         size_t off, phys_len;
     409                 :            :         int ret, cnt = 0;
     410                 :            : 
     411         [ -  + ]:         97 :         if (mp->flags & RTE_MEMPOOL_F_NO_IOVA_CONTIG)
     412                 :          0 :                 return rte_mempool_populate_iova(mp, addr, RTE_BAD_IOVA,
     413                 :            :                         len, free_cb, opaque);
     414                 :            : 
     415         [ +  + ]:       1142 :         for (off = 0; off < len &&
     416         [ +  + ]:       1046 :                      mp->populated_size < mp->size; off += phys_len) {
     417                 :            : 
     418                 :       1045 :                 iova = get_iova(addr + off);
     419                 :            : 
     420                 :            :                 /* populate with the largest group of contiguous pages */
     421                 :       1045 :                 for (phys_len = RTE_MIN(
     422                 :            :                         (size_t)(RTE_PTR_ALIGN_CEIL(addr + off + 1, pg_sz) -
     423                 :            :                                 (addr + off)),
     424                 :            :                         len - off);
     425         [ +  + ]:    2173011 :                      off + phys_len < len;
     426                 :    2171966 :                      phys_len = RTE_MIN(phys_len + pg_sz, len - off)) {
     427                 :            :                         rte_iova_t iova_tmp;
     428                 :            : 
     429                 :    2172915 :                         iova_tmp = get_iova(addr + off + phys_len);
     430                 :            : 
     431         [ +  - ]:    2172915 :                         if (iova_tmp == RTE_BAD_IOVA ||
     432         [ +  + ]:    2172915 :                                         iova_tmp != iova + phys_len)
     433                 :            :                                 break;
     434                 :            :                 }
     435                 :            : 
     436                 :       1045 :                 ret = rte_mempool_populate_iova(mp, addr + off, iova,
     437                 :            :                         phys_len, free_cb, opaque);
     438         [ -  + ]:       1045 :                 if (ret == 0)
     439                 :          0 :                         continue;
     440         [ -  + ]:       1045 :                 if (ret < 0)
     441                 :          0 :                         goto fail;
     442                 :            :                 /* no need to call the free callback for next chunks */
     443                 :            :                 free_cb = NULL;
     444                 :       1045 :                 cnt += ret;
     445                 :            :         }
     446                 :            : 
     447                 :         97 :         rte_mempool_trace_populate_virt(mp, addr, len, pg_sz, free_cb, opaque);
     448                 :         97 :         return cnt;
     449                 :            : 
     450                 :            :  fail:
     451                 :          0 :         rte_mempool_free_memchunks(mp);
     452                 :          0 :         return ret;
     453                 :            : }
     454                 :            : 
     455                 :            : /* Get the minimal page size used in a mempool before populating it. */
     456                 :            : int
     457                 :       1143 : rte_mempool_get_page_size(struct rte_mempool *mp, size_t *pg_sz)
     458                 :            : {
     459                 :            :         bool need_iova_contig_obj;
     460                 :            :         bool alloc_in_ext_mem;
     461                 :            :         int ret;
     462                 :            : 
     463                 :            :         /* check if we can retrieve a valid socket ID */
     464                 :       1143 :         ret = rte_malloc_heap_socket_is_external(mp->socket_id);
     465         [ +  - ]:       1143 :         if (ret < 0)
     466                 :            :                 return -EINVAL;
     467                 :            :         alloc_in_ext_mem = (ret == 1);
     468                 :       1143 :         need_iova_contig_obj = !(mp->flags & RTE_MEMPOOL_F_NO_IOVA_CONTIG);
     469                 :            : 
     470         [ +  + ]:       1143 :         if (!need_iova_contig_obj)
     471                 :          1 :                 *pg_sz = 0;
     472   [ +  +  -  + ]:       1142 :         else if (rte_eal_has_hugepages() || alloc_in_ext_mem)
     473                 :       1068 :                 *pg_sz = get_min_page_size(mp->socket_id);
     474                 :            :         else
     475                 :         74 :                 *pg_sz = rte_mem_page_size();
     476                 :            : 
     477         [ -  + ]:       1143 :         rte_mempool_trace_get_page_size(mp, *pg_sz);
     478                 :       1143 :         return 0;
     479                 :            : }
     480                 :            : 
     481                 :            : /* Default function to populate the mempool: allocate memory in memzones,
     482                 :            :  * and populate them. Return the number of objects added, or a negative
     483                 :            :  * value on error.
     484                 :            :  */
     485                 :            : int
     486                 :         94 : rte_mempool_populate_default(struct rte_mempool *mp)
     487                 :            : {
     488                 :            :         unsigned int mz_flags = RTE_MEMZONE_1GB|RTE_MEMZONE_SIZE_HINT_ONLY;
     489                 :            :         char mz_name[RTE_MEMZONE_NAMESIZE];
     490                 :            :         const struct rte_memzone *mz;
     491                 :            :         ssize_t mem_size;
     492                 :            :         size_t align, pg_sz, pg_shift = 0;
     493                 :            :         rte_iova_t iova;
     494                 :            :         unsigned mz_id, n;
     495                 :            :         int ret;
     496                 :            :         bool need_iova_contig_obj;
     497                 :            :         size_t max_alloc_size = SIZE_MAX;
     498                 :            : 
     499                 :         94 :         ret = mempool_ops_alloc_once(mp);
     500         [ +  - ]:         94 :         if (ret != 0)
     501                 :            :                 return ret;
     502                 :            : 
     503                 :            :         /* mempool must not be populated */
     504         [ +  - ]:         94 :         if (mp->nb_mem_chunks != 0)
     505                 :            :                 return -EEXIST;
     506                 :            : 
     507                 :            :         /*
     508                 :            :          * the following section calculates page shift and page size values.
     509                 :            :          *
     510                 :            :          * these values impact the result of calc_mem_size operation, which
     511                 :            :          * returns the amount of memory that should be allocated to store the
     512                 :            :          * desired number of objects. when not zero, it allocates more memory
     513                 :            :          * for the padding between objects, to ensure that an object does not
     514                 :            :          * cross a page boundary. in other words, page size/shift are to be set
     515                 :            :          * to zero if mempool elements won't care about page boundaries.
     516                 :            :          * there are several considerations for page size and page shift here.
     517                 :            :          *
     518                 :            :          * if we don't need our mempools to have physically contiguous objects,
     519                 :            :          * then just set page shift and page size to 0, because the user has
     520                 :            :          * indicated that there's no need to care about anything.
     521                 :            :          *
     522                 :            :          * if we do need contiguous objects (if a mempool driver has its
     523                 :            :          * own calc_size() method returning min_chunk_size = mem_size),
     524                 :            :          * there is also an option to reserve the entire mempool memory
     525                 :            :          * as one contiguous block of memory.
     526                 :            :          *
     527                 :            :          * if we require contiguous objects, but not necessarily the entire
     528                 :            :          * mempool reserved space to be contiguous, pg_sz will be != 0,
     529                 :            :          * and the default ops->populate() will take care of not placing
     530                 :            :          * objects across pages.
     531                 :            :          *
     532                 :            :          * if our IO addresses are physical, we may get memory from bigger
     533                 :            :          * pages, or we might get memory from smaller pages, and how much of it
     534                 :            :          * we require depends on whether we want bigger or smaller pages.
     535                 :            :          * However, requesting each and every memory size is too much work, so
     536                 :            :          * what we'll do instead is walk through the page sizes available, pick
     537                 :            :          * the smallest one and set up page shift to match that one. We will be
     538                 :            :          * wasting some space this way, but it's much nicer than looping around
     539                 :            :          * trying to reserve each and every page size.
     540                 :            :          *
     541                 :            :          * If we fail to get enough contiguous memory, then we'll go and
     542                 :            :          * reserve space in smaller chunks.
     543                 :            :          */
     544                 :            : 
     545                 :         94 :         need_iova_contig_obj = !(mp->flags & RTE_MEMPOOL_F_NO_IOVA_CONTIG);
     546                 :         94 :         ret = rte_mempool_get_page_size(mp, &pg_sz);
     547         [ +  - ]:         94 :         if (ret < 0)
     548                 :            :                 return ret;
     549                 :            : 
     550         [ +  - ]:         94 :         if (pg_sz != 0)
     551                 :         94 :                 pg_shift = rte_bsf32(pg_sz);
     552                 :            : 
     553         [ +  + ]:        188 :         for (mz_id = 0, n = mp->size; n > 0; mz_id++, n -= ret) {
     554                 :            :                 size_t min_chunk_size;
     555                 :            : 
     556                 :         94 :                 mem_size = rte_mempool_ops_calc_mem_size(
     557                 :            :                         mp, n, pg_shift, &min_chunk_size, &align);
     558                 :            : 
     559         [ -  + ]:         94 :                 if (mem_size < 0) {
     560                 :          0 :                         ret = mem_size;
     561                 :          0 :                         goto fail;
     562                 :            :                 }
     563                 :            : 
     564                 :            :                 ret = snprintf(mz_name, sizeof(mz_name),
     565         [ -  + ]:         94 :                         RTE_MEMPOOL_MZ_FORMAT "_%d", mp->name, mz_id);
     566         [ -  + ]:         94 :                 if (ret < 0 || ret >= (int)sizeof(mz_name)) {
     567                 :            :                         ret = -ENAMETOOLONG;
     568                 :          0 :                         goto fail;
     569                 :            :                 }
     570                 :            : 
     571                 :            :                 /* if we're trying to reserve contiguous memory, add appropriate
     572                 :            :                  * memzone flag.
     573                 :            :                  */
     574         [ -  + ]:         94 :                 if (min_chunk_size == (size_t)mem_size)
     575                 :            :                         mz_flags |= RTE_MEMZONE_IOVA_CONTIG;
     576                 :            : 
     577                 :            :                 /* Allocate a memzone, retrying with a smaller area on ENOMEM */
     578                 :            :                 do {
     579                 :         94 :                         mz = rte_memzone_reserve_aligned(mz_name,
     580                 :         94 :                                 RTE_MIN((size_t)mem_size, max_alloc_size),
     581                 :            :                                 mp->socket_id, mz_flags, align);
     582                 :            : 
     583   [ -  +  -  - ]:         94 :                         if (mz != NULL || rte_errno != ENOMEM)
     584                 :            :                                 break;
     585                 :            : 
     586                 :          0 :                         max_alloc_size = RTE_MIN(max_alloc_size,
     587                 :            :                                                 (size_t)mem_size) / 2;
     588         [ #  # ]:          0 :                 } while (mz == NULL && max_alloc_size >= min_chunk_size);
     589                 :            : 
     590         [ -  + ]:         94 :                 if (mz == NULL) {
     591                 :          0 :                         ret = -rte_errno;
     592                 :          0 :                         goto fail;
     593                 :            :                 }
     594                 :            : 
     595         [ +  - ]:         94 :                 if (need_iova_contig_obj)
     596                 :         94 :                         iova = mz->iova;
     597                 :            :                 else
     598                 :            :                         iova = RTE_BAD_IOVA;
     599                 :            : 
     600   [ +  -  -  + ]:         94 :                 if (pg_sz == 0 || (mz_flags & RTE_MEMZONE_IOVA_CONTIG))
     601                 :          0 :                         ret = rte_mempool_populate_iova(mp, mz->addr,
     602                 :          0 :                                 iova, mz->len,
     603                 :            :                                 rte_mempool_memchunk_mz_free,
     604                 :            :                                 (void *)(uintptr_t)mz);
     605                 :            :                 else
     606                 :         94 :                         ret = rte_mempool_populate_virt(mp, mz->addr,
     607                 :         94 :                                 mz->len, pg_sz,
     608                 :            :                                 rte_mempool_memchunk_mz_free,
     609                 :            :                                 (void *)(uintptr_t)mz);
     610         [ +  - ]:         94 :                 if (ret == 0) /* should not happen */
     611                 :            :                         ret = -ENOBUFS;
     612         [ -  + ]:         94 :                 if (ret < 0) {
     613                 :          0 :                         rte_memzone_free(mz);
     614                 :          0 :                         goto fail;
     615                 :            :                 }
     616                 :            :         }
     617                 :            : 
     618                 :         94 :         rte_mempool_trace_populate_default(mp);
     619                 :         94 :         return mp->size;
     620                 :            : 
     621                 :            :  fail:
     622                 :          0 :         rte_mempool_free_memchunks(mp);
     623                 :          0 :         return ret;
     624                 :            : }
     625                 :            : 
     626                 :            : /* return the memory size required for mempool objects in anonymous mem */
     627                 :            : static ssize_t
     628                 :          6 : get_anon_size(const struct rte_mempool *mp)
     629                 :            : {
     630                 :            :         ssize_t size;
     631                 :            :         size_t pg_sz, pg_shift;
     632                 :            :         size_t min_chunk_size;
     633                 :            :         size_t align;
     634                 :            : 
     635                 :          6 :         pg_sz = rte_mem_page_size();
     636                 :          6 :         pg_shift = rte_bsf32(pg_sz);
     637                 :          6 :         size = rte_mempool_ops_calc_mem_size(mp, mp->size, pg_shift,
     638                 :            :                                              &min_chunk_size, &align);
     639                 :            : 
     640                 :          6 :         return size;
     641                 :            : }
     642                 :            : 
     643                 :            : /* unmap a memory zone mapped by rte_mempool_populate_anon() */
     644                 :            : static void
     645                 :          3 : rte_mempool_memchunk_anon_free(struct rte_mempool_memhdr *memhdr,
     646                 :            :         void *opaque)
     647                 :            : {
     648                 :            :         ssize_t size;
     649                 :            : 
     650                 :            :         /*
     651                 :            :          * Calculate size since memhdr->len has contiguous chunk length
     652                 :            :          * which may be smaller if anon map is split into many contiguous
     653                 :            :          * chunks. Result must be the same as we calculated on populate.
     654                 :            :          */
     655                 :          3 :         size = get_anon_size(memhdr->mp);
     656         [ +  - ]:          3 :         if (size < 0)
     657                 :            :                 return;
     658                 :            : 
     659                 :          3 :         rte_mem_unmap(opaque, size);
     660                 :            : }
     661                 :            : 
     662                 :            : /* populate the mempool with an anonymous mapping */
     663                 :            : int
     664                 :          4 : rte_mempool_populate_anon(struct rte_mempool *mp)
     665                 :            : {
     666                 :            :         ssize_t size;
     667                 :            :         int ret;
     668                 :            :         char *addr;
     669                 :            : 
     670                 :            :         /* mempool is already populated, error */
     671   [ +  +  -  + ]:          4 :         if ((!STAILQ_EMPTY(&mp->mem_list)) || mp->nb_mem_chunks != 0) {
     672                 :          1 :                 rte_errno = EINVAL;
     673                 :          1 :                 return 0;
     674                 :            :         }
     675                 :            : 
     676                 :          3 :         ret = mempool_ops_alloc_once(mp);
     677         [ -  + ]:          3 :         if (ret < 0) {
     678                 :          0 :                 rte_errno = -ret;
     679                 :          0 :                 return 0;
     680                 :            :         }
     681                 :            : 
     682                 :          3 :         size = get_anon_size(mp);
     683         [ -  + ]:          3 :         if (size < 0) {
     684                 :          0 :                 rte_errno = -size;
     685                 :          0 :                 return 0;
     686                 :            :         }
     687                 :            : 
     688                 :            :         /* get chunk of virtually continuous memory */
     689                 :          3 :         addr = rte_mem_map(NULL, size, RTE_PROT_READ | RTE_PROT_WRITE,
     690                 :            :                 RTE_MAP_SHARED | RTE_MAP_ANONYMOUS, -1, 0);
     691         [ +  - ]:          3 :         if (addr == NULL)
     692                 :            :                 return 0;
     693                 :            :         /* can't use MMAP_LOCKED, it does not exist on BSD */
     694         [ -  + ]:          3 :         if (rte_mem_lock(addr, size) < 0) {
     695                 :          0 :                 rte_mem_unmap(addr, size);
     696                 :          0 :                 return 0;
     697                 :            :         }
     698                 :            : 
     699                 :          3 :         ret = rte_mempool_populate_virt(mp, addr, size, rte_mem_page_size(),
     700                 :            :                 rte_mempool_memchunk_anon_free, addr);
     701         [ +  - ]:          3 :         if (ret == 0) /* should not happen */
     702                 :            :                 ret = -ENOBUFS;
     703         [ -  + ]:          3 :         if (ret < 0) {
     704                 :          0 :                 rte_errno = -ret;
     705                 :          0 :                 goto fail;
     706                 :            :         }
     707                 :            : 
     708                 :          3 :         rte_mempool_trace_populate_anon(mp);
     709                 :          3 :         return mp->populated_size;
     710                 :            : 
     711                 :            :  fail:
     712                 :          0 :         rte_mempool_free_memchunks(mp);
     713                 :          0 :         return 0;
     714                 :            : }
     715                 :            : 
     716                 :            : /* free a mempool */
     717                 :            : void
     718                 :        112 : rte_mempool_free(struct rte_mempool *mp)
     719                 :            : {
     720                 :            :         struct rte_mempool_list *mempool_list = NULL;
     721                 :            :         struct rte_tailq_entry *te;
     722                 :            : 
     723         [ +  + ]:        112 :         if (mp == NULL)
     724                 :            :                 return;
     725                 :            : 
     726                 :         95 :         mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
     727                 :         95 :         rte_mcfg_tailq_write_lock();
     728                 :            :         /* find out tailq entry */
     729         [ +  - ]:        310 :         TAILQ_FOREACH(te, mempool_list, next) {
     730         [ +  + ]:        310 :                 if (te->data == (void *)mp)
     731                 :            :                         break;
     732                 :            :         }
     733                 :            : 
     734         [ +  - ]:         95 :         if (te != NULL) {
     735         [ +  + ]:         95 :                 TAILQ_REMOVE(mempool_list, te, next);
     736                 :         95 :                 rte_free(te);
     737                 :            :         }
     738                 :         95 :         rte_mcfg_tailq_write_unlock();
     739                 :            : 
     740                 :         95 :         mempool_event_callback_invoke(RTE_MEMPOOL_EVENT_DESTROY, mp);
     741                 :         95 :         rte_mempool_trace_free(mp);
     742                 :         95 :         rte_mempool_free_memchunks(mp);
     743                 :         95 :         rte_mempool_ops_free(mp);
     744                 :         95 :         rte_memzone_free(mp->mz);
     745                 :            : }
     746                 :            : 
     747                 :            : static void
     748                 :            : mempool_cache_init(struct rte_mempool_cache *cache, uint32_t size)
     749                 :            : {
     750                 :            :         /* Check that cache have enough space for flush threshold */
     751                 :            :         RTE_BUILD_BUG_ON(CALC_CACHE_FLUSHTHRESH(RTE_MEMPOOL_CACHE_MAX_SIZE) >
     752                 :            :                          RTE_SIZEOF_FIELD(struct rte_mempool_cache, objs) /
     753                 :            :                          RTE_SIZEOF_FIELD(struct rte_mempool_cache, objs[0]));
     754                 :            : 
     755                 :       2691 :         cache->size = size;
     756                 :       2691 :         cache->flushthresh = CALC_CACHE_FLUSHTHRESH(size);
     757         [ -  + ]:          3 :         cache->len = 0;
     758                 :            : }
     759                 :            : 
     760                 :            : /*
     761                 :            :  * Create and initialize a cache for objects that are retrieved from and
     762                 :            :  * returned to an underlying mempool. This structure is identical to the
     763                 :            :  * local_cache[lcore_id] pointed to by the mempool structure.
     764                 :            :  */
     765                 :            : struct rte_mempool_cache *
     766                 :          3 : rte_mempool_cache_create(uint32_t size, int socket_id)
     767                 :            : {
     768                 :            :         struct rte_mempool_cache *cache;
     769                 :            : 
     770         [ -  + ]:          3 :         if (size == 0 || size > RTE_MEMPOOL_CACHE_MAX_SIZE) {
     771                 :          0 :                 rte_errno = EINVAL;
     772                 :          0 :                 return NULL;
     773                 :            :         }
     774                 :            : 
     775                 :          3 :         cache = rte_zmalloc_socket("MEMPOOL_CACHE", sizeof(*cache),
     776                 :            :                                   RTE_CACHE_LINE_SIZE, socket_id);
     777         [ -  + ]:          3 :         if (cache == NULL) {
     778                 :          0 :                 RTE_MEMPOOL_LOG(ERR, "Cannot allocate mempool cache.");
     779                 :          0 :                 rte_errno = ENOMEM;
     780                 :          0 :                 return NULL;
     781                 :            :         }
     782                 :            : 
     783                 :            :         mempool_cache_init(cache, size);
     784                 :            : 
     785                 :          3 :         rte_mempool_trace_cache_create(size, socket_id, cache);
     786                 :          3 :         return cache;
     787                 :            : }
     788                 :            : 
     789                 :            : /*
     790                 :            :  * Free a cache. It's the responsibility of the user to make sure that any
     791                 :            :  * remaining objects in the cache are flushed to the corresponding
     792                 :            :  * mempool.
     793                 :            :  */
     794                 :            : void
     795         [ -  + ]:          3 : rte_mempool_cache_free(struct rte_mempool_cache *cache)
     796                 :            : {
     797                 :          3 :         rte_mempool_trace_cache_free(cache);
     798                 :          3 :         rte_free(cache);
     799                 :          3 : }
     800                 :            : 
     801                 :            : /* create an empty mempool */
     802                 :            : struct rte_mempool *
     803                 :        114 : rte_mempool_create_empty(const char *name, unsigned n, unsigned elt_size,
     804                 :            :         unsigned cache_size, unsigned private_data_size,
     805                 :            :         int socket_id, unsigned flags)
     806                 :            : {
     807                 :            :         char mz_name[RTE_MEMZONE_NAMESIZE];
     808                 :            :         struct rte_mempool_list *mempool_list;
     809                 :            :         struct rte_mempool *mp = NULL;
     810                 :            :         struct rte_tailq_entry *te = NULL;
     811                 :            :         const struct rte_memzone *mz = NULL;
     812                 :            :         size_t mempool_size;
     813                 :            :         unsigned int mz_flags = RTE_MEMZONE_1GB|RTE_MEMZONE_SIZE_HINT_ONLY;
     814                 :            :         struct rte_mempool_objsz objsz;
     815                 :            :         unsigned lcore_id;
     816                 :            :         int ret;
     817                 :            : 
     818                 :            :         /* compilation-time checks */
     819                 :            :         RTE_BUILD_BUG_ON((sizeof(struct rte_mempool) &
     820                 :            :                           RTE_CACHE_LINE_MASK) != 0);
     821                 :            :         RTE_BUILD_BUG_ON((sizeof(struct rte_mempool_cache) &
     822                 :            :                           RTE_CACHE_LINE_MASK) != 0);
     823                 :            : #ifdef RTE_LIBRTE_MEMPOOL_STATS
     824                 :            :         RTE_BUILD_BUG_ON((sizeof(struct rte_mempool_debug_stats) &
     825                 :            :                           RTE_CACHE_LINE_MASK) != 0);
     826                 :            :         RTE_BUILD_BUG_ON((offsetof(struct rte_mempool, stats) &
     827                 :            :                           RTE_CACHE_LINE_MASK) != 0);
     828                 :            : #endif
     829                 :            : 
     830                 :        114 :         mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
     831                 :            : 
     832                 :            :         /* asked for zero items */
     833         [ -  + ]:        114 :         if (n == 0) {
     834                 :          0 :                 rte_errno = EINVAL;
     835                 :          0 :                 return NULL;
     836                 :            :         }
     837                 :            : 
     838                 :            :         /* asked cache too big */
     839         [ +  + ]:        114 :         if (cache_size > RTE_MEMPOOL_CACHE_MAX_SIZE ||
     840         [ -  + ]:        113 :             CALC_CACHE_FLUSHTHRESH(cache_size) > n) {
     841                 :          1 :                 rte_errno = EINVAL;
     842                 :          1 :                 return NULL;
     843                 :            :         }
     844                 :            : 
     845                 :            :         /* enforce only user flags are passed by the application */
     846         [ +  + ]:        113 :         if ((flags & ~RTE_MEMPOOL_VALID_USER_FLAGS) != 0) {
     847                 :          1 :                 rte_errno = EINVAL;
     848                 :          1 :                 return NULL;
     849                 :            :         }
     850                 :            : 
     851                 :            :         /*
     852                 :            :          * No objects in the pool can be used for IO until it's populated
     853                 :            :          * with at least some objects with valid IOVA.
     854                 :            :          */
     855                 :        112 :         flags |= RTE_MEMPOOL_F_NON_IO;
     856                 :            : 
     857                 :            :         /* "no cache align" imply "no spread" */
     858         [ +  + ]:        112 :         if (flags & RTE_MEMPOOL_F_NO_CACHE_ALIGN)
     859                 :          1 :                 flags |= RTE_MEMPOOL_F_NO_SPREAD;
     860                 :            : 
     861                 :            :         /* calculate mempool object sizes. */
     862         [ -  + ]:        112 :         if (!rte_mempool_calc_obj_size(elt_size, flags, &objsz)) {
     863                 :          0 :                 rte_errno = EINVAL;
     864                 :          0 :                 return NULL;
     865                 :            :         }
     866                 :            : 
     867                 :        111 :         rte_mcfg_mempool_write_lock();
     868                 :            : 
     869                 :            :         /*
     870                 :            :          * reserve a memory zone for this mempool: private data is
     871                 :            :          * cache-aligned
     872                 :            :          */
     873                 :        112 :         private_data_size = (private_data_size +
     874                 :            :                              RTE_MEMPOOL_ALIGN_MASK) & (~RTE_MEMPOOL_ALIGN_MASK);
     875                 :            : 
     876                 :            : 
     877                 :            :         /* try to allocate tailq entry */
     878                 :        112 :         te = rte_zmalloc("MEMPOOL_TAILQ_ENTRY", sizeof(*te), 0);
     879         [ -  + ]:        112 :         if (te == NULL) {
     880                 :          0 :                 RTE_MEMPOOL_LOG(ERR, "Cannot allocate tailq entry!");
     881                 :          0 :                 goto exit_unlock;
     882                 :            :         }
     883                 :            : 
     884         [ +  + ]:        112 :         mempool_size = RTE_MEMPOOL_HEADER_SIZE(mp, cache_size);
     885                 :        112 :         mempool_size += private_data_size;
     886         [ -  + ]:        112 :         mempool_size = RTE_ALIGN_CEIL(mempool_size, RTE_MEMPOOL_ALIGN);
     887                 :            : 
     888                 :            :         ret = snprintf(mz_name, sizeof(mz_name), RTE_MEMPOOL_MZ_FORMAT, name);
     889         [ -  + ]:        112 :         if (ret < 0 || ret >= (int)sizeof(mz_name)) {
     890                 :          0 :                 rte_errno = ENAMETOOLONG;
     891                 :          0 :                 goto exit_unlock;
     892                 :            :         }
     893                 :            : 
     894                 :        112 :         mz = rte_memzone_reserve(mz_name, mempool_size, socket_id, mz_flags);
     895         [ +  + ]:        112 :         if (mz == NULL)
     896                 :         13 :                 goto exit_unlock;
     897                 :            : 
     898                 :            :         /* init the mempool structure */
     899         [ -  + ]:         99 :         mp = mz->addr;
     900                 :            :         memset(mp, 0, RTE_MEMPOOL_HEADER_SIZE(mp, cache_size));
     901         [ -  + ]:         99 :         ret = strlcpy(mp->name, name, sizeof(mp->name));
     902         [ -  + ]:         99 :         if (ret < 0 || ret >= (int)sizeof(mp->name)) {
     903                 :          0 :                 rte_errno = ENAMETOOLONG;
     904                 :          0 :                 goto exit_unlock;
     905                 :            :         }
     906                 :         99 :         mp->mz = mz;
     907                 :         99 :         mp->size = n;
     908                 :         99 :         mp->flags = flags;
     909                 :         99 :         mp->socket_id = socket_id;
     910                 :         99 :         mp->elt_size = objsz.elt_size;
     911                 :         99 :         mp->header_size = objsz.header_size;
     912                 :         99 :         mp->trailer_size = objsz.trailer_size;
     913                 :            :         /* Size of default caches, zero means disabled. */
     914                 :         99 :         mp->cache_size = cache_size;
     915                 :         99 :         mp->private_data_size = private_data_size;
     916                 :         99 :         STAILQ_INIT(&mp->elt_list);
     917                 :         99 :         STAILQ_INIT(&mp->mem_list);
     918                 :            : 
     919                 :            :         /*
     920                 :            :          * Since we have 4 combinations of the SP/SC/MP/MC examine the flags to
     921                 :            :          * set the correct index into the table of ops structs.
     922                 :            :          */
     923         [ +  + ]:         99 :         if ((flags & RTE_MEMPOOL_F_SP_PUT) && (flags & RTE_MEMPOOL_F_SC_GET))
     924                 :          1 :                 ret = rte_mempool_set_ops_byname(mp, "ring_sp_sc", NULL);
     925         [ +  + ]:         98 :         else if (flags & RTE_MEMPOOL_F_SP_PUT)
     926                 :          1 :                 ret = rte_mempool_set_ops_byname(mp, "ring_sp_mc", NULL);
     927         [ -  + ]:         97 :         else if (flags & RTE_MEMPOOL_F_SC_GET)
     928                 :          0 :                 ret = rte_mempool_set_ops_byname(mp, "ring_mp_sc", NULL);
     929                 :            :         else
     930                 :         97 :                 ret = rte_mempool_set_ops_byname(mp, "ring_mp_mc", NULL);
     931                 :            : 
     932         [ -  + ]:         99 :         if (ret)
     933                 :          0 :                 goto exit_unlock;
     934                 :            : 
     935                 :            :         /*
     936                 :            :          * local_cache pointer is set even if cache_size is zero.
     937                 :            :          * The local_cache points to just past the elt_pa[] array.
     938                 :            :          */
     939                 :         99 :         mp->local_cache = (struct rte_mempool_cache *)
     940                 :         99 :                 RTE_PTR_ADD(mp, RTE_MEMPOOL_HEADER_SIZE(mp, 0));
     941                 :            : 
     942                 :            :         /* Init all default caches. */
     943         [ +  + ]:         99 :         if (cache_size != 0) {
     944         [ +  + ]:       2709 :                 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++)
     945                 :       2688 :                         mempool_cache_init(&mp->local_cache[lcore_id],
     946                 :            :                                            cache_size);
     947                 :            :         }
     948                 :            : 
     949                 :         99 :         te->data = mp;
     950                 :            : 
     951                 :         99 :         rte_mcfg_tailq_write_lock();
     952                 :         99 :         TAILQ_INSERT_TAIL(mempool_list, te, next);
     953                 :         99 :         rte_mcfg_tailq_write_unlock();
     954                 :         99 :         rte_mcfg_mempool_write_unlock();
     955                 :            : 
     956                 :         99 :         rte_mempool_trace_create_empty(name, n, elt_size, cache_size,
     957                 :            :                 private_data_size, flags, mp);
     958                 :         99 :         return mp;
     959                 :            : 
     960                 :         13 : exit_unlock:
     961                 :         13 :         rte_mcfg_mempool_write_unlock();
     962                 :         13 :         rte_free(te);
     963                 :         13 :         rte_mempool_free(mp);
     964                 :         13 :         return NULL;
     965                 :            : }
     966                 :            : 
     967                 :            : /* create the mempool */
     968                 :            : struct rte_mempool *
     969                 :         83 : rte_mempool_create(const char *name, unsigned n, unsigned elt_size,
     970                 :            :         unsigned cache_size, unsigned private_data_size,
     971                 :            :         rte_mempool_ctor_t *mp_init, void *mp_init_arg,
     972                 :            :         rte_mempool_obj_cb_t *obj_init, void *obj_init_arg,
     973                 :            :         int socket_id, unsigned flags)
     974                 :            : {
     975                 :            :         struct rte_mempool *mp;
     976                 :            : 
     977                 :         83 :         mp = rte_mempool_create_empty(name, n, elt_size, cache_size,
     978                 :            :                 private_data_size, socket_id, flags);
     979         [ +  + ]:         83 :         if (mp == NULL)
     980                 :            :                 return NULL;
     981                 :            : 
     982                 :            :         /* call the mempool priv initializer */
     983         [ +  + ]:         68 :         if (mp_init)
     984                 :          1 :                 mp_init(mp, mp_init_arg);
     985                 :            : 
     986         [ -  + ]:         68 :         if (rte_mempool_populate_default(mp) < 0)
     987                 :          0 :                 goto fail;
     988                 :            : 
     989                 :            :         /* call the object initializers */
     990         [ +  + ]:         68 :         if (obj_init)
     991                 :         37 :                 rte_mempool_obj_iter(mp, obj_init, obj_init_arg);
     992                 :            : 
     993                 :         68 :         rte_mempool_trace_create(name, n, elt_size, cache_size,
     994                 :            :                 private_data_size, mp_init, mp_init_arg, obj_init,
     995                 :            :                 obj_init_arg, flags, mp);
     996                 :         68 :         return mp;
     997                 :            : 
     998                 :            :  fail:
     999                 :          0 :         rte_mempool_free(mp);
    1000                 :          0 :         return NULL;
    1001                 :            : }
    1002                 :            : 
    1003                 :            : /* Return the number of entries in the mempool */
    1004                 :            : unsigned int
    1005                 :        178 : rte_mempool_avail_count(const struct rte_mempool *mp)
    1006                 :            : {
    1007                 :            :         unsigned count;
    1008                 :            :         unsigned lcore_id;
    1009                 :            : 
    1010                 :        178 :         count = rte_mempool_ops_get_count(mp);
    1011                 :            : 
    1012         [ +  + ]:        178 :         if (mp->cache_size == 0)
    1013                 :            :                 return count;
    1014                 :            : 
    1015         [ +  + ]:        387 :         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++)
    1016                 :        384 :                 count += mp->local_cache[lcore_id].len;
    1017                 :            : 
    1018                 :            :         /*
    1019                 :            :          * due to race condition (access to len is not locked), the
    1020                 :            :          * total can be greater than size... so fix the result
    1021                 :            :          */
    1022                 :          3 :         if (count > mp->size)
    1023                 :            :                 return mp->size;
    1024                 :            :         return count;
    1025                 :            : }
    1026                 :            : 
    1027                 :            : /* return the number of entries allocated from the mempool */
    1028                 :            : unsigned int
    1029                 :         23 : rte_mempool_in_use_count(const struct rte_mempool *mp)
    1030                 :            : {
    1031                 :         23 :         return mp->size - rte_mempool_avail_count(mp);
    1032                 :            : }
    1033                 :            : 
    1034                 :            : /* dump the cache status */
    1035                 :            : static unsigned
    1036                 :         38 : rte_mempool_dump_cache(FILE *f, const struct rte_mempool *mp)
    1037                 :            : {
    1038                 :            :         unsigned lcore_id;
    1039                 :            :         unsigned count = 0;
    1040                 :            :         unsigned cache_count;
    1041                 :            : 
    1042                 :            :         fprintf(f, "  internal cache infos:\n");
    1043                 :         38 :         fprintf(f, "    cache_size=%"PRIu32"\n", mp->cache_size);
    1044                 :            : 
    1045         [ +  + ]:         38 :         if (mp->cache_size == 0)
    1046                 :            :                 return count;
    1047                 :            : 
    1048         [ +  + ]:       3225 :         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
    1049                 :       3200 :                 cache_count = mp->local_cache[lcore_id].len;
    1050                 :            :                 fprintf(f, "    cache_count[%u]=%"PRIu32"\n",
    1051                 :            :                         lcore_id, cache_count);
    1052                 :       3200 :                 count += cache_count;
    1053                 :            :         }
    1054                 :            :         fprintf(f, "    total_cache_count=%u\n", count);
    1055                 :         25 :         return count;
    1056                 :            : }
    1057                 :            : 
    1058                 :            : #ifndef __INTEL_COMPILER
    1059                 :            : #pragma GCC diagnostic ignored "-Wcast-qual"
    1060                 :            : #endif
    1061                 :            : 
    1062                 :            : /* check and update cookies or panic (internal) */
    1063                 :          0 : void rte_mempool_check_cookies(const struct rte_mempool *mp,
    1064                 :            :         void * const *obj_table_const, unsigned n, int free)
    1065                 :            : {
    1066                 :            : #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
    1067                 :            :         struct rte_mempool_objhdr *hdr;
    1068                 :            :         struct rte_mempool_objtlr *tlr;
    1069                 :            :         uint64_t cookie;
    1070                 :            :         void *tmp;
    1071                 :            :         void *obj;
    1072                 :            :         void **obj_table;
    1073                 :            : 
    1074                 :            :         /* Force to drop the "const" attribute. This is done only when
    1075                 :            :          * DEBUG is enabled */
    1076                 :            :         tmp = (void *) obj_table_const;
    1077                 :            :         obj_table = tmp;
    1078                 :            : 
    1079                 :            :         while (n--) {
    1080                 :            :                 obj = obj_table[n];
    1081                 :            : 
    1082                 :            :                 if (rte_mempool_from_obj(obj) != mp)
    1083                 :            :                         rte_panic("MEMPOOL: object is owned by another "
    1084                 :            :                                   "mempool\n");
    1085                 :            : 
    1086                 :            :                 hdr = rte_mempool_get_header(obj);
    1087                 :            :                 cookie = hdr->cookie;
    1088                 :            : 
    1089                 :            :                 if (free == 0) {
    1090                 :            :                         if (cookie != RTE_MEMPOOL_HEADER_COOKIE1) {
    1091                 :            :                                 RTE_MEMPOOL_LOG(CRIT,
    1092                 :            :                                         "obj=%p, mempool=%p, cookie=%" PRIx64,
    1093                 :            :                                         obj, (const void *) mp, cookie);
    1094                 :            :                                 rte_panic("MEMPOOL: bad header cookie (put)\n");
    1095                 :            :                         }
    1096                 :            :                         hdr->cookie = RTE_MEMPOOL_HEADER_COOKIE2;
    1097                 :            :                 } else if (free == 1) {
    1098                 :            :                         if (cookie != RTE_MEMPOOL_HEADER_COOKIE2) {
    1099                 :            :                                 RTE_MEMPOOL_LOG(CRIT,
    1100                 :            :                                         "obj=%p, mempool=%p, cookie=%" PRIx64,
    1101                 :            :                                         obj, (const void *) mp, cookie);
    1102                 :            :                                 rte_panic("MEMPOOL: bad header cookie (get)\n");
    1103                 :            :                         }
    1104                 :            :                         hdr->cookie = RTE_MEMPOOL_HEADER_COOKIE1;
    1105                 :            :                 } else if (free == 2) {
    1106                 :            :                         if (cookie != RTE_MEMPOOL_HEADER_COOKIE1 &&
    1107                 :            :                             cookie != RTE_MEMPOOL_HEADER_COOKIE2) {
    1108                 :            :                                 RTE_MEMPOOL_LOG(CRIT,
    1109                 :            :                                         "obj=%p, mempool=%p, cookie=%" PRIx64,
    1110                 :            :                                         obj, (const void *) mp, cookie);
    1111                 :            :                                 rte_panic("MEMPOOL: bad header cookie (audit)\n");
    1112                 :            :                         }
    1113                 :            :                 }
    1114                 :            :                 tlr = rte_mempool_get_trailer(obj);
    1115                 :            :                 cookie = tlr->cookie;
    1116                 :            :                 if (cookie != RTE_MEMPOOL_TRAILER_COOKIE) {
    1117                 :            :                         RTE_MEMPOOL_LOG(CRIT,
    1118                 :            :                                 "obj=%p, mempool=%p, cookie=%" PRIx64,
    1119                 :            :                                 obj, (const void *) mp, cookie);
    1120                 :            :                         rte_panic("MEMPOOL: bad trailer cookie\n");
    1121                 :            :                 }
    1122                 :            :         }
    1123                 :            : #else
    1124                 :            :         RTE_SET_USED(mp);
    1125                 :            :         RTE_SET_USED(obj_table_const);
    1126                 :            :         RTE_SET_USED(n);
    1127                 :            :         RTE_SET_USED(free);
    1128                 :            : #endif
    1129                 :          0 : }
    1130                 :            : 
    1131                 :            : void
    1132                 :          0 : rte_mempool_contig_blocks_check_cookies(const struct rte_mempool *mp,
    1133                 :            :         void * const *first_obj_table_const, unsigned int n, int free)
    1134                 :            : {
    1135                 :            : #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
    1136                 :            :         struct rte_mempool_info info;
    1137                 :            :         const size_t total_elt_sz =
    1138                 :            :                 mp->header_size + mp->elt_size + mp->trailer_size;
    1139                 :            :         unsigned int i, j;
    1140                 :            : 
    1141                 :            :         rte_mempool_ops_get_info(mp, &info);
    1142                 :            : 
    1143                 :            :         for (i = 0; i < n; ++i) {
    1144                 :            :                 void *first_obj = first_obj_table_const[i];
    1145                 :            : 
    1146                 :            :                 for (j = 0; j < info.contig_block_size; ++j) {
    1147                 :            :                         void *obj;
    1148                 :            : 
    1149                 :            :                         obj = (void *)((uintptr_t)first_obj + j * total_elt_sz);
    1150                 :            :                         rte_mempool_check_cookies(mp, &obj, 1, free);
    1151                 :            :                 }
    1152                 :            :         }
    1153                 :            : #else
    1154                 :            :         RTE_SET_USED(mp);
    1155                 :            :         RTE_SET_USED(first_obj_table_const);
    1156                 :            :         RTE_SET_USED(n);
    1157                 :            :         RTE_SET_USED(free);
    1158                 :            : #endif
    1159                 :          0 : }
    1160                 :            : 
    1161                 :            : #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
    1162                 :            : static void
    1163                 :            : mempool_obj_audit(struct rte_mempool *mp, __rte_unused void *opaque,
    1164                 :            :         void *obj, __rte_unused unsigned idx)
    1165                 :            : {
    1166                 :            :         RTE_MEMPOOL_CHECK_COOKIES(mp, &obj, 1, 2);
    1167                 :            : }
    1168                 :            : 
    1169                 :            : static void
    1170                 :            : mempool_audit_cookies(struct rte_mempool *mp)
    1171                 :            : {
    1172                 :            :         unsigned num;
    1173                 :            : 
    1174                 :            :         num = rte_mempool_obj_iter(mp, mempool_obj_audit, NULL);
    1175                 :            :         if (num != mp->size) {
    1176                 :            :                 rte_panic("rte_mempool_obj_iter(mempool=%p, size=%u) "
    1177                 :            :                         "iterated only over %u elements\n",
    1178                 :            :                         mp, mp->size, num);
    1179                 :            :         }
    1180                 :            : }
    1181                 :            : #else
    1182                 :            : #define mempool_audit_cookies(mp) do {} while(0)
    1183                 :            : #endif
    1184                 :            : 
    1185                 :            : #ifndef __INTEL_COMPILER
    1186                 :            : #pragma GCC diagnostic error "-Wcast-qual"
    1187                 :            : #endif
    1188                 :            : 
    1189                 :            : /* check cookies before and after objects */
    1190                 :            : static void
    1191                 :         38 : mempool_audit_cache(const struct rte_mempool *mp)
    1192                 :            : {
    1193                 :            :         /* check cache size consistency */
    1194                 :            :         unsigned lcore_id;
    1195                 :            : 
    1196         [ +  + ]:         38 :         if (mp->cache_size == 0)
    1197                 :            :                 return;
    1198                 :            : 
    1199         [ +  + ]:       3225 :         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
    1200                 :            :                 const struct rte_mempool_cache *cache;
    1201                 :       3200 :                 cache = &mp->local_cache[lcore_id];
    1202         [ -  + ]:       3200 :                 if (cache->len > RTE_DIM(cache->objs)) {
    1203                 :          0 :                         RTE_MEMPOOL_LOG(CRIT, "badness on cache[%u]",
    1204                 :            :                                 lcore_id);
    1205                 :          0 :                         rte_panic("MEMPOOL: invalid cache len\n");
    1206                 :            :                 }
    1207                 :            :         }
    1208                 :            : }
    1209                 :            : 
    1210                 :            : /* check the consistency of mempool (size, cookies, ...) */
    1211                 :            : void
    1212                 :         38 : rte_mempool_audit(struct rte_mempool *mp)
    1213                 :            : {
    1214                 :         38 :         mempool_audit_cache(mp);
    1215                 :            :         mempool_audit_cookies(mp);
    1216                 :            : 
    1217                 :            :         /* For case where mempool DEBUG is not set, and cache size is 0 */
    1218                 :            :         RTE_SET_USED(mp);
    1219                 :         38 : }
    1220                 :            : 
    1221                 :            : /* dump the status of the mempool on the console */
    1222                 :            : void
    1223                 :         38 : rte_mempool_dump(FILE *f, struct rte_mempool *mp)
    1224                 :            : {
    1225                 :            : #ifdef RTE_LIBRTE_MEMPOOL_STATS
    1226                 :            :         struct rte_mempool_info info;
    1227                 :            :         struct rte_mempool_debug_stats sum;
    1228                 :            :         unsigned lcore_id;
    1229                 :            : #endif
    1230                 :            :         struct rte_mempool_memhdr *memhdr;
    1231                 :            :         struct rte_mempool_ops *ops;
    1232                 :            :         unsigned common_count;
    1233                 :            :         unsigned cache_count;
    1234                 :            :         size_t mem_len = 0;
    1235                 :            : 
    1236                 :            :         RTE_ASSERT(f != NULL);
    1237                 :            :         RTE_ASSERT(mp != NULL);
    1238                 :            : 
    1239                 :         38 :         fprintf(f, "mempool <%s>@%p\n", mp->name, mp);
    1240                 :         38 :         fprintf(f, "  flags=%x\n", mp->flags);
    1241                 :         38 :         fprintf(f, "  socket_id=%d\n", mp->socket_id);
    1242                 :         38 :         fprintf(f, "  pool=%p\n", mp->pool_data);
    1243                 :         38 :         fprintf(f, "  iova=0x%" PRIx64 "\n", mp->mz->iova);
    1244                 :         38 :         fprintf(f, "  nb_mem_chunks=%u\n", mp->nb_mem_chunks);
    1245                 :         38 :         fprintf(f, "  size=%"PRIu32"\n", mp->size);
    1246                 :         38 :         fprintf(f, "  populated_size=%"PRIu32"\n", mp->populated_size);
    1247                 :         38 :         fprintf(f, "  header_size=%"PRIu32"\n", mp->header_size);
    1248                 :         38 :         fprintf(f, "  elt_size=%"PRIu32"\n", mp->elt_size);
    1249                 :         38 :         fprintf(f, "  trailer_size=%"PRIu32"\n", mp->trailer_size);
    1250                 :         38 :         fprintf(f, "  total_obj_size=%"PRIu32"\n",
    1251                 :         38 :                mp->header_size + mp->elt_size + mp->trailer_size);
    1252                 :            : 
    1253                 :         38 :         fprintf(f, "  private_data_size=%"PRIu32"\n", mp->private_data_size);
    1254                 :            : 
    1255                 :         38 :         fprintf(f, "  ops_index=%d\n", mp->ops_index);
    1256         [ -  + ]:         38 :         ops = rte_mempool_get_ops(mp->ops_index);
    1257                 :         38 :         fprintf(f, "  ops_name: <%s>\n", (ops != NULL) ? ops->name : "NA");
    1258                 :            : 
    1259         [ +  + ]:       1969 :         STAILQ_FOREACH(memhdr, &mp->mem_list, next)
    1260                 :       1931 :                 mem_len += memhdr->len;
    1261         [ +  - ]:         38 :         if (mem_len != 0) {
    1262                 :         38 :                 fprintf(f, "  avg bytes/object=%#Lf\n",
    1263                 :         38 :                         (long double)mem_len / mp->size);
    1264                 :            :         }
    1265                 :            : 
    1266                 :         38 :         cache_count = rte_mempool_dump_cache(f, mp);
    1267                 :         38 :         common_count = rte_mempool_ops_get_count(mp);
    1268         [ -  + ]:         38 :         if ((cache_count + common_count) > mp->size)
    1269                 :          0 :                 common_count = mp->size - cache_count;
    1270                 :            :         fprintf(f, "  common_pool_count=%u\n", common_count);
    1271                 :            : 
    1272                 :            :         /* sum and dump statistics */
    1273                 :            : #ifdef RTE_LIBRTE_MEMPOOL_STATS
    1274                 :            :         rte_mempool_ops_get_info(mp, &info);
    1275                 :            :         memset(&sum, 0, sizeof(sum));
    1276                 :            :         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE + 1; lcore_id++) {
    1277                 :            :                 sum.put_bulk += mp->stats[lcore_id].put_bulk;
    1278                 :            :                 sum.put_objs += mp->stats[lcore_id].put_objs;
    1279                 :            :                 sum.put_common_pool_bulk += mp->stats[lcore_id].put_common_pool_bulk;
    1280                 :            :                 sum.put_common_pool_objs += mp->stats[lcore_id].put_common_pool_objs;
    1281                 :            :                 sum.get_common_pool_bulk += mp->stats[lcore_id].get_common_pool_bulk;
    1282                 :            :                 sum.get_common_pool_objs += mp->stats[lcore_id].get_common_pool_objs;
    1283                 :            :                 sum.get_success_bulk += mp->stats[lcore_id].get_success_bulk;
    1284                 :            :                 sum.get_success_objs += mp->stats[lcore_id].get_success_objs;
    1285                 :            :                 sum.get_fail_bulk += mp->stats[lcore_id].get_fail_bulk;
    1286                 :            :                 sum.get_fail_objs += mp->stats[lcore_id].get_fail_objs;
    1287                 :            :                 sum.get_success_blks += mp->stats[lcore_id].get_success_blks;
    1288                 :            :                 sum.get_fail_blks += mp->stats[lcore_id].get_fail_blks;
    1289                 :            :         }
    1290                 :            :         if (mp->cache_size != 0) {
    1291                 :            :                 /* Add the statistics stored in the mempool caches. */
    1292                 :            :                 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
    1293                 :            :                         sum.put_bulk += mp->local_cache[lcore_id].stats.put_bulk;
    1294                 :            :                         sum.put_objs += mp->local_cache[lcore_id].stats.put_objs;
    1295                 :            :                         sum.get_success_bulk += mp->local_cache[lcore_id].stats.get_success_bulk;
    1296                 :            :                         sum.get_success_objs += mp->local_cache[lcore_id].stats.get_success_objs;
    1297                 :            :                 }
    1298                 :            :         }
    1299                 :            :         fprintf(f, "  stats:\n");
    1300                 :            :         fprintf(f, "    put_bulk=%"PRIu64"\n", sum.put_bulk);
    1301                 :            :         fprintf(f, "    put_objs=%"PRIu64"\n", sum.put_objs);
    1302                 :            :         fprintf(f, "    put_common_pool_bulk=%"PRIu64"\n", sum.put_common_pool_bulk);
    1303                 :            :         fprintf(f, "    put_common_pool_objs=%"PRIu64"\n", sum.put_common_pool_objs);
    1304                 :            :         fprintf(f, "    get_common_pool_bulk=%"PRIu64"\n", sum.get_common_pool_bulk);
    1305                 :            :         fprintf(f, "    get_common_pool_objs=%"PRIu64"\n", sum.get_common_pool_objs);
    1306                 :            :         fprintf(f, "    get_success_bulk=%"PRIu64"\n", sum.get_success_bulk);
    1307                 :            :         fprintf(f, "    get_success_objs=%"PRIu64"\n", sum.get_success_objs);
    1308                 :            :         fprintf(f, "    get_fail_bulk=%"PRIu64"\n", sum.get_fail_bulk);
    1309                 :            :         fprintf(f, "    get_fail_objs=%"PRIu64"\n", sum.get_fail_objs);
    1310                 :            :         if (info.contig_block_size > 0) {
    1311                 :            :                 fprintf(f, "    get_success_blks=%"PRIu64"\n",
    1312                 :            :                         sum.get_success_blks);
    1313                 :            :                 fprintf(f, "    get_fail_blks=%"PRIu64"\n", sum.get_fail_blks);
    1314                 :            :         }
    1315                 :            : #else
    1316                 :            :         fprintf(f, "  no statistics available\n");
    1317                 :            : #endif
    1318                 :            : 
    1319                 :         38 :         rte_mempool_audit(mp);
    1320                 :         38 : }
    1321                 :            : 
    1322                 :            : /* dump the status of all mempools on the console */
    1323                 :            : void
    1324                 :          2 : rte_mempool_list_dump(FILE *f)
    1325                 :            : {
    1326                 :            :         struct rte_mempool *mp = NULL;
    1327                 :            :         struct rte_tailq_entry *te;
    1328                 :            :         struct rte_mempool_list *mempool_list;
    1329                 :            : 
    1330                 :          2 :         mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
    1331                 :            : 
    1332                 :          2 :         rte_mcfg_mempool_read_lock();
    1333                 :            : 
    1334         [ +  + ]:         14 :         TAILQ_FOREACH(te, mempool_list, next) {
    1335                 :         12 :                 mp = (struct rte_mempool *) te->data;
    1336                 :         12 :                 rte_mempool_dump(f, mp);
    1337                 :            :         }
    1338                 :            : 
    1339                 :          2 :         rte_mcfg_mempool_read_unlock();
    1340                 :          2 : }
    1341                 :            : 
    1342                 :            : /* search a mempool from its name */
    1343                 :            : struct rte_mempool *
    1344                 :        103 : rte_mempool_lookup(const char *name)
    1345                 :            : {
    1346                 :            :         struct rte_mempool *mp = NULL;
    1347                 :            :         struct rte_tailq_entry *te;
    1348                 :            :         struct rte_mempool_list *mempool_list;
    1349                 :            : 
    1350                 :        103 :         mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
    1351                 :            : 
    1352                 :        103 :         rte_mcfg_mempool_read_lock();
    1353                 :            : 
    1354         [ +  + ]:       1332 :         TAILQ_FOREACH(te, mempool_list, next) {
    1355                 :       1329 :                 mp = (struct rte_mempool *) te->data;
    1356         [ +  + ]:       1329 :                 if (strncmp(name, mp->name, RTE_MEMPOOL_NAMESIZE) == 0)
    1357                 :            :                         break;
    1358                 :            :         }
    1359                 :            : 
    1360                 :        103 :         rte_mcfg_mempool_read_unlock();
    1361                 :            : 
    1362         [ +  + ]:        103 :         if (te == NULL) {
    1363                 :          3 :                 rte_errno = ENOENT;
    1364                 :          3 :                 return NULL;
    1365                 :            :         }
    1366                 :            : 
    1367                 :            :         return mp;
    1368                 :            : }
    1369                 :            : 
    1370                 :          1 : void rte_mempool_walk(void (*func)(struct rte_mempool *, void *),
    1371                 :            :                       void *arg)
    1372                 :            : {
    1373                 :            :         struct rte_tailq_entry *te = NULL;
    1374                 :            :         struct rte_mempool_list *mempool_list;
    1375                 :            :         void *tmp_te;
    1376                 :            : 
    1377                 :          1 :         mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
    1378                 :            : 
    1379                 :          1 :         rte_mcfg_mempool_read_lock();
    1380                 :            : 
    1381         [ +  + ]:          7 :         RTE_TAILQ_FOREACH_SAFE(te, mempool_list, next, tmp_te) {
    1382                 :          6 :                 (*func)((struct rte_mempool *) te->data, arg);
    1383                 :            :         }
    1384                 :            : 
    1385                 :          1 :         rte_mcfg_mempool_read_unlock();
    1386                 :          1 : }
    1387                 :            : 
    1388                 :            : struct mempool_callback_data {
    1389                 :            :         TAILQ_ENTRY(mempool_callback_data) callbacks;
    1390                 :            :         rte_mempool_event_callback *func;
    1391                 :            :         void *user_data;
    1392                 :            : };
    1393                 :            : 
    1394                 :            : static void
    1395                 :        192 : mempool_event_callback_invoke(enum rte_mempool_event event,
    1396                 :            :                               struct rte_mempool *mp)
    1397                 :            : {
    1398                 :            :         struct mempool_callback_data *cb;
    1399                 :            :         void *tmp_te;
    1400                 :            : 
    1401                 :        192 :         rte_mcfg_tailq_read_lock();
    1402         [ +  + ]:        214 :         RTE_TAILQ_FOREACH_SAFE(cb, &callback_tailq, callbacks, tmp_te) {
    1403                 :         22 :                 rte_mcfg_tailq_read_unlock();
    1404                 :         22 :                 cb->func(event, mp, cb->user_data);
    1405                 :         22 :                 rte_mcfg_tailq_read_lock();
    1406                 :            :         }
    1407                 :        192 :         rte_mcfg_tailq_read_unlock();
    1408                 :        192 : }
    1409                 :            : 
    1410                 :            : int
    1411                 :         10 : rte_mempool_event_callback_register(rte_mempool_event_callback *func,
    1412                 :            :                                     void *user_data)
    1413                 :            : {
    1414                 :            :         struct mempool_callback_data *cb;
    1415                 :            :         int ret;
    1416                 :            : 
    1417         [ -  + ]:         10 :         if (func == NULL) {
    1418                 :          0 :                 rte_errno = EINVAL;
    1419                 :          0 :                 return -rte_errno;
    1420                 :            :         }
    1421                 :            : 
    1422                 :         10 :         rte_mcfg_tailq_write_lock();
    1423         [ +  + ]:         19 :         TAILQ_FOREACH(cb, &callback_tailq, callbacks) {
    1424   [ +  +  +  + ]:         10 :                 if (cb->func == func && cb->user_data == user_data) {
    1425                 :            :                         ret = -EEXIST;
    1426                 :          1 :                         goto exit;
    1427                 :            :                 }
    1428                 :            :         }
    1429                 :            : 
    1430                 :          9 :         cb = calloc(1, sizeof(*cb));
    1431         [ -  + ]:          9 :         if (cb == NULL) {
    1432                 :          0 :                 RTE_MEMPOOL_LOG(ERR, "Cannot allocate event callback!");
    1433                 :            :                 ret = -ENOMEM;
    1434                 :          0 :                 goto exit;
    1435                 :            :         }
    1436                 :            : 
    1437                 :          9 :         cb->func = func;
    1438                 :          9 :         cb->user_data = user_data;
    1439                 :          9 :         TAILQ_INSERT_TAIL(&callback_tailq, cb, callbacks);
    1440                 :            :         ret = 0;
    1441                 :            : 
    1442                 :         10 : exit:
    1443                 :         10 :         rte_mcfg_tailq_write_unlock();
    1444                 :         10 :         rte_errno = -ret;
    1445                 :         10 :         return ret;
    1446                 :            : }
    1447                 :            : 
    1448                 :            : int
    1449                 :         17 : rte_mempool_event_callback_unregister(rte_mempool_event_callback *func,
    1450                 :            :                                       void *user_data)
    1451                 :            : {
    1452                 :            :         struct mempool_callback_data *cb;
    1453                 :            :         int ret = -ENOENT;
    1454                 :            : 
    1455                 :         17 :         rte_mcfg_tailq_write_lock();
    1456         [ +  + ]:         31 :         TAILQ_FOREACH(cb, &callback_tailq, callbacks) {
    1457   [ +  +  +  + ]:         23 :                 if (cb->func == func && cb->user_data == user_data) {
    1458         [ +  + ]:          9 :                         TAILQ_REMOVE(&callback_tailq, cb, callbacks);
    1459                 :            :                         ret = 0;
    1460                 :            :                         break;
    1461                 :            :                 }
    1462                 :            :         }
    1463                 :         17 :         rte_mcfg_tailq_write_unlock();
    1464                 :            : 
    1465                 :            :         if (ret == 0)
    1466                 :          9 :                 free(cb);
    1467                 :         17 :         rte_errno = -ret;
    1468                 :         17 :         return ret;
    1469                 :            : }
    1470                 :            : 
    1471                 :            : static void
    1472                 :          0 : mempool_list_cb(struct rte_mempool *mp, void *arg)
    1473                 :            : {
    1474                 :            :         struct rte_tel_data *d = (struct rte_tel_data *)arg;
    1475                 :            : 
    1476                 :          0 :         rte_tel_data_add_array_string(d, mp->name);
    1477                 :          0 : }
    1478                 :            : 
    1479                 :            : static int
    1480                 :          0 : mempool_handle_list(const char *cmd __rte_unused,
    1481                 :            :                     const char *params __rte_unused, struct rte_tel_data *d)
    1482                 :            : {
    1483                 :          0 :         rte_tel_data_start_array(d, RTE_TEL_STRING_VAL);
    1484                 :          0 :         rte_mempool_walk(mempool_list_cb, d);
    1485                 :          0 :         return 0;
    1486                 :            : }
    1487                 :            : 
    1488                 :            : struct mempool_info_cb_arg {
    1489                 :            :         char *pool_name;
    1490                 :            :         struct rte_tel_data *d;
    1491                 :            : };
    1492                 :            : 
    1493                 :            : static void
    1494                 :          0 : mempool_info_cb(struct rte_mempool *mp, void *arg)
    1495                 :            : {
    1496                 :            :         struct mempool_info_cb_arg *info = (struct mempool_info_cb_arg *)arg;
    1497                 :            :         const struct rte_memzone *mz;
    1498                 :            :         uint64_t cache_count, common_count;
    1499                 :            : 
    1500         [ #  # ]:          0 :         if (strncmp(mp->name, info->pool_name, RTE_MEMZONE_NAMESIZE))
    1501                 :            :                 return;
    1502                 :            : 
    1503                 :          0 :         rte_tel_data_add_dict_string(info->d, "name", mp->name);
    1504                 :          0 :         rte_tel_data_add_dict_uint(info->d, "pool_id", mp->pool_id);
    1505                 :          0 :         rte_tel_data_add_dict_uint(info->d, "flags", mp->flags);
    1506                 :          0 :         rte_tel_data_add_dict_int(info->d, "socket_id", mp->socket_id);
    1507                 :          0 :         rte_tel_data_add_dict_uint(info->d, "size", mp->size);
    1508                 :          0 :         rte_tel_data_add_dict_uint(info->d, "cache_size", mp->cache_size);
    1509                 :          0 :         rte_tel_data_add_dict_uint(info->d, "elt_size", mp->elt_size);
    1510                 :          0 :         rte_tel_data_add_dict_uint(info->d, "header_size", mp->header_size);
    1511                 :          0 :         rte_tel_data_add_dict_uint(info->d, "trailer_size", mp->trailer_size);
    1512                 :          0 :         rte_tel_data_add_dict_uint(info->d, "private_data_size",
    1513                 :          0 :                                   mp->private_data_size);
    1514                 :          0 :         rte_tel_data_add_dict_int(info->d, "ops_index", mp->ops_index);
    1515                 :          0 :         rte_tel_data_add_dict_uint(info->d, "populated_size",
    1516                 :          0 :                                   mp->populated_size);
    1517                 :            : 
    1518                 :            :         cache_count = 0;
    1519         [ #  # ]:          0 :         if (mp->cache_size > 0) {
    1520                 :            :                 int lcore_id;
    1521         [ #  # ]:          0 :                 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++)
    1522                 :          0 :                         cache_count += mp->local_cache[lcore_id].len;
    1523                 :            :         }
    1524                 :          0 :         rte_tel_data_add_dict_uint(info->d, "total_cache_count", cache_count);
    1525                 :          0 :         common_count = rte_mempool_ops_get_count(mp);
    1526         [ #  # ]:          0 :         if ((cache_count + common_count) > mp->size)
    1527                 :          0 :                 common_count = mp->size - cache_count;
    1528                 :          0 :         rte_tel_data_add_dict_uint(info->d, "common_pool_count", common_count);
    1529                 :            : 
    1530                 :          0 :         mz = mp->mz;
    1531                 :          0 :         rte_tel_data_add_dict_string(info->d, "mz_name", mz->name);
    1532                 :          0 :         rte_tel_data_add_dict_uint(info->d, "mz_len", mz->len);
    1533                 :          0 :         rte_tel_data_add_dict_uint(info->d, "mz_hugepage_sz",
    1534                 :          0 :                                   mz->hugepage_sz);
    1535                 :          0 :         rte_tel_data_add_dict_int(info->d, "mz_socket_id", mz->socket_id);
    1536                 :          0 :         rte_tel_data_add_dict_uint(info->d, "mz_flags", mz->flags);
    1537                 :            : }
    1538                 :            : 
    1539                 :            : static int
    1540                 :          0 : mempool_handle_info(const char *cmd __rte_unused, const char *params,
    1541                 :            :                     struct rte_tel_data *d)
    1542                 :            : {
    1543                 :            :         struct mempool_info_cb_arg mp_arg;
    1544                 :            :         char name[RTE_MEMZONE_NAMESIZE];
    1545                 :            : 
    1546   [ #  #  #  # ]:          0 :         if (!params || strlen(params) == 0)
    1547                 :            :                 return -EINVAL;
    1548                 :            : 
    1549                 :            :         rte_strlcpy(name, params, RTE_MEMZONE_NAMESIZE);
    1550                 :            : 
    1551                 :          0 :         rte_tel_data_start_dict(d);
    1552                 :          0 :         mp_arg.pool_name = name;
    1553                 :          0 :         mp_arg.d = d;
    1554                 :          0 :         rte_mempool_walk(mempool_info_cb, &mp_arg);
    1555                 :            : 
    1556                 :          0 :         return 0;
    1557                 :            : }
    1558                 :            : 
    1559                 :        235 : RTE_INIT(mempool_init_telemetry)
    1560                 :            : {
    1561                 :        235 :         rte_telemetry_register_cmd("/mempool/list", mempool_handle_list,
    1562                 :            :                 "Returns list of available mempool. Takes no parameters");
    1563                 :        235 :         rte_telemetry_register_cmd("/mempool/info", mempool_handle_info,
    1564                 :            :                 "Returns mempool info. Parameters: pool_name");
    1565                 :        235 : }

Generated by: LCOV version 1.14