199a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
299a2dd95SBruce Richardson * Copyright(c) 2010-2014 Intel Corporation.
399a2dd95SBruce Richardson * Copyright(c) 2016 6WIND S.A.
4203dcc9cSMorten Brørup * Copyright(c) 2022 SmartShare Systems
599a2dd95SBruce Richardson */
699a2dd95SBruce Richardson
799a2dd95SBruce Richardson #include <stdbool.h>
803b3cdf9SDmitry Kozlyuk #include <stdlib.h>
999a2dd95SBruce Richardson #include <stdio.h>
1099a2dd95SBruce Richardson #include <string.h>
1199a2dd95SBruce Richardson #include <stdint.h>
1299a2dd95SBruce Richardson #include <unistd.h>
1399a2dd95SBruce Richardson #include <inttypes.h>
1499a2dd95SBruce Richardson #include <errno.h>
1599a2dd95SBruce Richardson #include <sys/queue.h>
1699a2dd95SBruce Richardson
1799a2dd95SBruce Richardson #include <rte_common.h>
1899a2dd95SBruce Richardson #include <rte_log.h>
1999a2dd95SBruce Richardson #include <rte_debug.h>
2099a2dd95SBruce Richardson #include <rte_memory.h>
2199a2dd95SBruce Richardson #include <rte_memzone.h>
2299a2dd95SBruce Richardson #include <rte_malloc.h>
2399a2dd95SBruce Richardson #include <rte_eal.h>
2499a2dd95SBruce Richardson #include <rte_eal_memconfig.h>
2599a2dd95SBruce Richardson #include <rte_errno.h>
2699a2dd95SBruce Richardson #include <rte_string_fns.h>
2799a2dd95SBruce Richardson #include <rte_tailq.h>
2899a2dd95SBruce Richardson #include <rte_eal_paging.h>
292f5c4025SGowrishankar Muthukrishnan #include <rte_telemetry.h>
3099a2dd95SBruce Richardson
311159d8fbSAnkur Dwivedi #include "mempool_trace.h"
3299a2dd95SBruce Richardson #include "rte_mempool.h"
3399a2dd95SBruce Richardson
342d603bf6SStephen Hemminger RTE_LOG_REGISTER_DEFAULT(rte_mempool_logtype, INFO);
352d603bf6SStephen Hemminger
3699a2dd95SBruce Richardson TAILQ_HEAD(rte_mempool_list, rte_tailq_entry);
3799a2dd95SBruce Richardson
3899a2dd95SBruce Richardson static struct rte_tailq_elem rte_mempool_tailq = {
3999a2dd95SBruce Richardson .name = "RTE_MEMPOOL",
4099a2dd95SBruce Richardson };
4199a2dd95SBruce Richardson EAL_REGISTER_TAILQ(rte_mempool_tailq)
4299a2dd95SBruce Richardson
4303b3cdf9SDmitry Kozlyuk TAILQ_HEAD(mempool_callback_tailq, mempool_callback_data);
44da2b9cb2SDmitry Kozlyuk
4503b3cdf9SDmitry Kozlyuk static struct mempool_callback_tailq callback_tailq =
4603b3cdf9SDmitry Kozlyuk TAILQ_HEAD_INITIALIZER(callback_tailq);
47da2b9cb2SDmitry Kozlyuk
48da2b9cb2SDmitry Kozlyuk /* Invoke all registered mempool event callbacks. */
49da2b9cb2SDmitry Kozlyuk static void
50da2b9cb2SDmitry Kozlyuk mempool_event_callback_invoke(enum rte_mempool_event event,
51da2b9cb2SDmitry Kozlyuk struct rte_mempool *mp);
52da2b9cb2SDmitry Kozlyuk
534b546488SStephen Hemminger /* Note: avoid using floating point since that compiler
544b546488SStephen Hemminger * may not think that is constant.
554b546488SStephen Hemminger */
564b546488SStephen Hemminger #define CALC_CACHE_FLUSHTHRESH(c) (((c) * 3) / 2)
5799a2dd95SBruce Richardson
5899a2dd95SBruce Richardson #if defined(RTE_ARCH_X86)
5999a2dd95SBruce Richardson /*
6099a2dd95SBruce Richardson * return the greatest common divisor between a and b (fast algorithm)
6199a2dd95SBruce Richardson */
get_gcd(unsigned a,unsigned b)6299a2dd95SBruce Richardson static unsigned get_gcd(unsigned a, unsigned b)
6399a2dd95SBruce Richardson {
6499a2dd95SBruce Richardson unsigned c;
6599a2dd95SBruce Richardson
6699a2dd95SBruce Richardson if (0 == a)
6799a2dd95SBruce Richardson return b;
6899a2dd95SBruce Richardson if (0 == b)
6999a2dd95SBruce Richardson return a;
7099a2dd95SBruce Richardson
7199a2dd95SBruce Richardson if (a < b) {
7299a2dd95SBruce Richardson c = a;
7399a2dd95SBruce Richardson a = b;
7499a2dd95SBruce Richardson b = c;
7599a2dd95SBruce Richardson }
7699a2dd95SBruce Richardson
7799a2dd95SBruce Richardson while (b != 0) {
7899a2dd95SBruce Richardson c = a % b;
7999a2dd95SBruce Richardson a = b;
8099a2dd95SBruce Richardson b = c;
8199a2dd95SBruce Richardson }
8299a2dd95SBruce Richardson
8399a2dd95SBruce Richardson return a;
8499a2dd95SBruce Richardson }
8599a2dd95SBruce Richardson
8699a2dd95SBruce Richardson /*
8799a2dd95SBruce Richardson * Depending on memory configuration on x86 arch, objects addresses are spread
8899a2dd95SBruce Richardson * between channels and ranks in RAM: the pool allocator will add
8999a2dd95SBruce Richardson * padding between objects. This function return the new size of the
9099a2dd95SBruce Richardson * object.
9199a2dd95SBruce Richardson */
9299a2dd95SBruce Richardson static unsigned int
arch_mem_object_align(unsigned int obj_size)9399a2dd95SBruce Richardson arch_mem_object_align(unsigned int obj_size)
9499a2dd95SBruce Richardson {
9599a2dd95SBruce Richardson unsigned nrank, nchan;
9699a2dd95SBruce Richardson unsigned new_obj_size;
9799a2dd95SBruce Richardson
9899a2dd95SBruce Richardson /* get number of channels */
9999a2dd95SBruce Richardson nchan = rte_memory_get_nchannel();
10099a2dd95SBruce Richardson if (nchan == 0)
10199a2dd95SBruce Richardson nchan = 4;
10299a2dd95SBruce Richardson
10399a2dd95SBruce Richardson nrank = rte_memory_get_nrank();
10499a2dd95SBruce Richardson if (nrank == 0)
10599a2dd95SBruce Richardson nrank = 1;
10699a2dd95SBruce Richardson
10799a2dd95SBruce Richardson /* process new object size */
10899a2dd95SBruce Richardson new_obj_size = (obj_size + RTE_MEMPOOL_ALIGN_MASK) / RTE_MEMPOOL_ALIGN;
10999a2dd95SBruce Richardson while (get_gcd(new_obj_size, nrank * nchan) != 1)
11099a2dd95SBruce Richardson new_obj_size++;
11199a2dd95SBruce Richardson return new_obj_size * RTE_MEMPOOL_ALIGN;
11299a2dd95SBruce Richardson }
11399a2dd95SBruce Richardson #else
11499a2dd95SBruce Richardson static unsigned int
arch_mem_object_align(unsigned int obj_size)11599a2dd95SBruce Richardson arch_mem_object_align(unsigned int obj_size)
11699a2dd95SBruce Richardson {
11799a2dd95SBruce Richardson return obj_size;
11899a2dd95SBruce Richardson }
11999a2dd95SBruce Richardson #endif
12099a2dd95SBruce Richardson
12199a2dd95SBruce Richardson struct pagesz_walk_arg {
12299a2dd95SBruce Richardson int socket_id;
12399a2dd95SBruce Richardson size_t min;
12499a2dd95SBruce Richardson };
12599a2dd95SBruce Richardson
12699a2dd95SBruce Richardson static int
find_min_pagesz(const struct rte_memseg_list * msl,void * arg)12799a2dd95SBruce Richardson find_min_pagesz(const struct rte_memseg_list *msl, void *arg)
12899a2dd95SBruce Richardson {
12999a2dd95SBruce Richardson struct pagesz_walk_arg *wa = arg;
13099a2dd95SBruce Richardson bool valid;
13199a2dd95SBruce Richardson
13299a2dd95SBruce Richardson /*
13399a2dd95SBruce Richardson * we need to only look at page sizes available for a particular socket
13499a2dd95SBruce Richardson * ID. so, we either need an exact match on socket ID (can match both
13599a2dd95SBruce Richardson * native and external memory), or, if SOCKET_ID_ANY was specified as a
13699a2dd95SBruce Richardson * socket ID argument, we must only look at native memory and ignore any
13799a2dd95SBruce Richardson * page sizes associated with external memory.
13899a2dd95SBruce Richardson */
13999a2dd95SBruce Richardson valid = msl->socket_id == wa->socket_id;
14099a2dd95SBruce Richardson valid |= wa->socket_id == SOCKET_ID_ANY && msl->external == 0;
14199a2dd95SBruce Richardson
14299a2dd95SBruce Richardson if (valid && msl->page_sz < wa->min)
14399a2dd95SBruce Richardson wa->min = msl->page_sz;
14499a2dd95SBruce Richardson
14599a2dd95SBruce Richardson return 0;
14699a2dd95SBruce Richardson }
14799a2dd95SBruce Richardson
14899a2dd95SBruce Richardson static size_t
get_min_page_size(int socket_id)14999a2dd95SBruce Richardson get_min_page_size(int socket_id)
15099a2dd95SBruce Richardson {
15199a2dd95SBruce Richardson struct pagesz_walk_arg wa;
15299a2dd95SBruce Richardson
15399a2dd95SBruce Richardson wa.min = SIZE_MAX;
15499a2dd95SBruce Richardson wa.socket_id = socket_id;
15599a2dd95SBruce Richardson
15699a2dd95SBruce Richardson rte_memseg_list_walk(find_min_pagesz, &wa);
15799a2dd95SBruce Richardson
15899a2dd95SBruce Richardson return wa.min == SIZE_MAX ? (size_t) rte_mem_page_size() : wa.min;
15999a2dd95SBruce Richardson }
16099a2dd95SBruce Richardson
16199a2dd95SBruce Richardson
16299a2dd95SBruce Richardson static void
mempool_add_elem(struct rte_mempool * mp,__rte_unused void * opaque,void * obj,rte_iova_t iova)16399a2dd95SBruce Richardson mempool_add_elem(struct rte_mempool *mp, __rte_unused void *opaque,
16499a2dd95SBruce Richardson void *obj, rte_iova_t iova)
16599a2dd95SBruce Richardson {
16699a2dd95SBruce Richardson struct rte_mempool_objhdr *hdr;
16799a2dd95SBruce Richardson
16899a2dd95SBruce Richardson /* set mempool ptr in header */
16999a2dd95SBruce Richardson hdr = RTE_PTR_SUB(obj, sizeof(*hdr));
17099a2dd95SBruce Richardson hdr->mp = mp;
17199a2dd95SBruce Richardson hdr->iova = iova;
17299a2dd95SBruce Richardson STAILQ_INSERT_TAIL(&mp->elt_list, hdr, next);
17399a2dd95SBruce Richardson mp->populated_size++;
17499a2dd95SBruce Richardson
17599a2dd95SBruce Richardson #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
17699a2dd95SBruce Richardson hdr->cookie = RTE_MEMPOOL_HEADER_COOKIE2;
177*f93b1d82SStephen Hemminger rte_mempool_get_trailer(obj)->cookie = RTE_MEMPOOL_TRAILER_COOKIE;
17899a2dd95SBruce Richardson #endif
17999a2dd95SBruce Richardson }
18099a2dd95SBruce Richardson
18199a2dd95SBruce Richardson /* call obj_cb() for each mempool element */
18299a2dd95SBruce Richardson uint32_t
rte_mempool_obj_iter(struct rte_mempool * mp,rte_mempool_obj_cb_t * obj_cb,void * obj_cb_arg)18399a2dd95SBruce Richardson rte_mempool_obj_iter(struct rte_mempool *mp,
18499a2dd95SBruce Richardson rte_mempool_obj_cb_t *obj_cb, void *obj_cb_arg)
18599a2dd95SBruce Richardson {
18699a2dd95SBruce Richardson struct rte_mempool_objhdr *hdr;
18799a2dd95SBruce Richardson void *obj;
18899a2dd95SBruce Richardson unsigned n = 0;
18999a2dd95SBruce Richardson
19099a2dd95SBruce Richardson STAILQ_FOREACH(hdr, &mp->elt_list, next) {
19199a2dd95SBruce Richardson obj = (char *)hdr + sizeof(*hdr);
19299a2dd95SBruce Richardson obj_cb(mp, obj_cb_arg, obj, n);
19399a2dd95SBruce Richardson n++;
19499a2dd95SBruce Richardson }
19599a2dd95SBruce Richardson
19699a2dd95SBruce Richardson return n;
19799a2dd95SBruce Richardson }
19899a2dd95SBruce Richardson
19999a2dd95SBruce Richardson /* call mem_cb() for each mempool memory chunk */
20099a2dd95SBruce Richardson uint32_t
rte_mempool_mem_iter(struct rte_mempool * mp,rte_mempool_mem_cb_t * mem_cb,void * mem_cb_arg)20199a2dd95SBruce Richardson rte_mempool_mem_iter(struct rte_mempool *mp,
20299a2dd95SBruce Richardson rte_mempool_mem_cb_t *mem_cb, void *mem_cb_arg)
20399a2dd95SBruce Richardson {
20499a2dd95SBruce Richardson struct rte_mempool_memhdr *hdr;
20599a2dd95SBruce Richardson unsigned n = 0;
20699a2dd95SBruce Richardson
20799a2dd95SBruce Richardson STAILQ_FOREACH(hdr, &mp->mem_list, next) {
20899a2dd95SBruce Richardson mem_cb(mp, mem_cb_arg, hdr, n);
20999a2dd95SBruce Richardson n++;
21099a2dd95SBruce Richardson }
21199a2dd95SBruce Richardson
21299a2dd95SBruce Richardson return n;
21399a2dd95SBruce Richardson }
21499a2dd95SBruce Richardson
21599a2dd95SBruce Richardson /* get the header, trailer and total size of a mempool element. */
21699a2dd95SBruce Richardson uint32_t
rte_mempool_calc_obj_size(uint32_t elt_size,uint32_t flags,struct rte_mempool_objsz * sz)21799a2dd95SBruce Richardson rte_mempool_calc_obj_size(uint32_t elt_size, uint32_t flags,
21899a2dd95SBruce Richardson struct rte_mempool_objsz *sz)
21999a2dd95SBruce Richardson {
22099a2dd95SBruce Richardson struct rte_mempool_objsz lsz;
22199a2dd95SBruce Richardson
22299a2dd95SBruce Richardson sz = (sz != NULL) ? sz : &lsz;
22399a2dd95SBruce Richardson
22499a2dd95SBruce Richardson sz->header_size = sizeof(struct rte_mempool_objhdr);
225c47d7b90SAndrew Rybchenko if ((flags & RTE_MEMPOOL_F_NO_CACHE_ALIGN) == 0)
22699a2dd95SBruce Richardson sz->header_size = RTE_ALIGN_CEIL(sz->header_size,
22799a2dd95SBruce Richardson RTE_MEMPOOL_ALIGN);
22899a2dd95SBruce Richardson
22999a2dd95SBruce Richardson #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
23099a2dd95SBruce Richardson sz->trailer_size = sizeof(struct rte_mempool_objtlr);
23199a2dd95SBruce Richardson #else
23299a2dd95SBruce Richardson sz->trailer_size = 0;
23399a2dd95SBruce Richardson #endif
23499a2dd95SBruce Richardson
23599a2dd95SBruce Richardson /* element size is 8 bytes-aligned at least */
23699a2dd95SBruce Richardson sz->elt_size = RTE_ALIGN_CEIL(elt_size, sizeof(uint64_t));
23799a2dd95SBruce Richardson
23899a2dd95SBruce Richardson /* expand trailer to next cache line */
239c47d7b90SAndrew Rybchenko if ((flags & RTE_MEMPOOL_F_NO_CACHE_ALIGN) == 0) {
24099a2dd95SBruce Richardson sz->total_size = sz->header_size + sz->elt_size +
24199a2dd95SBruce Richardson sz->trailer_size;
24299a2dd95SBruce Richardson sz->trailer_size += ((RTE_MEMPOOL_ALIGN -
24399a2dd95SBruce Richardson (sz->total_size & RTE_MEMPOOL_ALIGN_MASK)) &
24499a2dd95SBruce Richardson RTE_MEMPOOL_ALIGN_MASK);
24599a2dd95SBruce Richardson }
24699a2dd95SBruce Richardson
24799a2dd95SBruce Richardson /*
24899a2dd95SBruce Richardson * increase trailer to add padding between objects in order to
24999a2dd95SBruce Richardson * spread them across memory channels/ranks
25099a2dd95SBruce Richardson */
251c47d7b90SAndrew Rybchenko if ((flags & RTE_MEMPOOL_F_NO_SPREAD) == 0) {
25299a2dd95SBruce Richardson unsigned new_size;
25399a2dd95SBruce Richardson new_size = arch_mem_object_align
25499a2dd95SBruce Richardson (sz->header_size + sz->elt_size + sz->trailer_size);
25599a2dd95SBruce Richardson sz->trailer_size = new_size - sz->header_size - sz->elt_size;
25699a2dd95SBruce Richardson }
25799a2dd95SBruce Richardson
25899a2dd95SBruce Richardson /* this is the size of an object, including header and trailer */
25999a2dd95SBruce Richardson sz->total_size = sz->header_size + sz->elt_size + sz->trailer_size;
26099a2dd95SBruce Richardson
26199a2dd95SBruce Richardson return sz->total_size;
26299a2dd95SBruce Richardson }
26399a2dd95SBruce Richardson
26499a2dd95SBruce Richardson /* free a memchunk allocated with rte_memzone_reserve() */
26599a2dd95SBruce Richardson static void
rte_mempool_memchunk_mz_free(__rte_unused struct rte_mempool_memhdr * memhdr,void * opaque)26699a2dd95SBruce Richardson rte_mempool_memchunk_mz_free(__rte_unused struct rte_mempool_memhdr *memhdr,
26799a2dd95SBruce Richardson void *opaque)
26899a2dd95SBruce Richardson {
26999a2dd95SBruce Richardson const struct rte_memzone *mz = opaque;
27099a2dd95SBruce Richardson rte_memzone_free(mz);
27199a2dd95SBruce Richardson }
27299a2dd95SBruce Richardson
27399a2dd95SBruce Richardson /* Free memory chunks used by a mempool. Objects must be in pool */
27499a2dd95SBruce Richardson static void
rte_mempool_free_memchunks(struct rte_mempool * mp)27599a2dd95SBruce Richardson rte_mempool_free_memchunks(struct rte_mempool *mp)
27699a2dd95SBruce Richardson {
27799a2dd95SBruce Richardson struct rte_mempool_memhdr *memhdr;
27899a2dd95SBruce Richardson void *elt;
27999a2dd95SBruce Richardson
28099a2dd95SBruce Richardson while (!STAILQ_EMPTY(&mp->elt_list)) {
28199a2dd95SBruce Richardson rte_mempool_ops_dequeue_bulk(mp, &elt, 1);
28299a2dd95SBruce Richardson (void)elt;
28399a2dd95SBruce Richardson STAILQ_REMOVE_HEAD(&mp->elt_list, next);
28499a2dd95SBruce Richardson mp->populated_size--;
28599a2dd95SBruce Richardson }
28699a2dd95SBruce Richardson
28799a2dd95SBruce Richardson while (!STAILQ_EMPTY(&mp->mem_list)) {
28899a2dd95SBruce Richardson memhdr = STAILQ_FIRST(&mp->mem_list);
28999a2dd95SBruce Richardson STAILQ_REMOVE_HEAD(&mp->mem_list, next);
29099a2dd95SBruce Richardson if (memhdr->free_cb != NULL)
29199a2dd95SBruce Richardson memhdr->free_cb(memhdr, memhdr->opaque);
29299a2dd95SBruce Richardson rte_free(memhdr);
29399a2dd95SBruce Richardson mp->nb_mem_chunks--;
29499a2dd95SBruce Richardson }
29599a2dd95SBruce Richardson }
29699a2dd95SBruce Richardson
29799a2dd95SBruce Richardson static int
mempool_ops_alloc_once(struct rte_mempool * mp)29899a2dd95SBruce Richardson mempool_ops_alloc_once(struct rte_mempool *mp)
29999a2dd95SBruce Richardson {
30099a2dd95SBruce Richardson int ret;
30199a2dd95SBruce Richardson
30299a2dd95SBruce Richardson /* create the internal ring if not already done */
303c47d7b90SAndrew Rybchenko if ((mp->flags & RTE_MEMPOOL_F_POOL_CREATED) == 0) {
30499a2dd95SBruce Richardson ret = rte_mempool_ops_alloc(mp);
30599a2dd95SBruce Richardson if (ret != 0)
30699a2dd95SBruce Richardson return ret;
307c47d7b90SAndrew Rybchenko mp->flags |= RTE_MEMPOOL_F_POOL_CREATED;
30899a2dd95SBruce Richardson }
30999a2dd95SBruce Richardson return 0;
31099a2dd95SBruce Richardson }
31199a2dd95SBruce Richardson
31299a2dd95SBruce Richardson /* Add objects in the pool, using a physically contiguous memory
31399a2dd95SBruce Richardson * zone. Return the number of objects added, or a negative value
31499a2dd95SBruce Richardson * on error.
31599a2dd95SBruce Richardson */
31699a2dd95SBruce Richardson int
rte_mempool_populate_iova(struct rte_mempool * mp,char * vaddr,rte_iova_t iova,size_t len,rte_mempool_memchunk_free_cb_t * free_cb,void * opaque)31799a2dd95SBruce Richardson rte_mempool_populate_iova(struct rte_mempool *mp, char *vaddr,
31899a2dd95SBruce Richardson rte_iova_t iova, size_t len, rte_mempool_memchunk_free_cb_t *free_cb,
31999a2dd95SBruce Richardson void *opaque)
32099a2dd95SBruce Richardson {
32199a2dd95SBruce Richardson unsigned i = 0;
32299a2dd95SBruce Richardson size_t off;
32399a2dd95SBruce Richardson struct rte_mempool_memhdr *memhdr;
32499a2dd95SBruce Richardson int ret;
32599a2dd95SBruce Richardson
32699a2dd95SBruce Richardson ret = mempool_ops_alloc_once(mp);
32799a2dd95SBruce Richardson if (ret != 0)
32899a2dd95SBruce Richardson return ret;
32999a2dd95SBruce Richardson
33099a2dd95SBruce Richardson /* mempool is already populated */
33199a2dd95SBruce Richardson if (mp->populated_size >= mp->size)
33299a2dd95SBruce Richardson return -ENOSPC;
33399a2dd95SBruce Richardson
33499a2dd95SBruce Richardson memhdr = rte_zmalloc("MEMPOOL_MEMHDR", sizeof(*memhdr), 0);
33599a2dd95SBruce Richardson if (memhdr == NULL)
33699a2dd95SBruce Richardson return -ENOMEM;
33799a2dd95SBruce Richardson
33899a2dd95SBruce Richardson memhdr->mp = mp;
33999a2dd95SBruce Richardson memhdr->addr = vaddr;
34099a2dd95SBruce Richardson memhdr->iova = iova;
34199a2dd95SBruce Richardson memhdr->len = len;
34299a2dd95SBruce Richardson memhdr->free_cb = free_cb;
34399a2dd95SBruce Richardson memhdr->opaque = opaque;
34499a2dd95SBruce Richardson
345c47d7b90SAndrew Rybchenko if (mp->flags & RTE_MEMPOOL_F_NO_CACHE_ALIGN)
34699a2dd95SBruce Richardson off = RTE_PTR_ALIGN_CEIL(vaddr, 8) - vaddr;
34799a2dd95SBruce Richardson else
34899a2dd95SBruce Richardson off = RTE_PTR_ALIGN_CEIL(vaddr, RTE_MEMPOOL_ALIGN) - vaddr;
34999a2dd95SBruce Richardson
35099a2dd95SBruce Richardson if (off > len) {
35199a2dd95SBruce Richardson ret = 0;
35299a2dd95SBruce Richardson goto fail;
35399a2dd95SBruce Richardson }
35499a2dd95SBruce Richardson
35599a2dd95SBruce Richardson i = rte_mempool_ops_populate(mp, mp->size - mp->populated_size,
35699a2dd95SBruce Richardson (char *)vaddr + off,
35799a2dd95SBruce Richardson (iova == RTE_BAD_IOVA) ? RTE_BAD_IOVA : (iova + off),
35899a2dd95SBruce Richardson len - off, mempool_add_elem, NULL);
35999a2dd95SBruce Richardson
36099a2dd95SBruce Richardson /* not enough room to store one object */
36199a2dd95SBruce Richardson if (i == 0) {
36299a2dd95SBruce Richardson ret = 0;
36399a2dd95SBruce Richardson goto fail;
36499a2dd95SBruce Richardson }
36599a2dd95SBruce Richardson
36699a2dd95SBruce Richardson STAILQ_INSERT_TAIL(&mp->mem_list, memhdr, next);
36799a2dd95SBruce Richardson mp->nb_mem_chunks++;
36899a2dd95SBruce Richardson
3696fda3ff6SDmitry Kozlyuk /* Check if at least some objects in the pool are now usable for IO. */
3706fda3ff6SDmitry Kozlyuk if (!(mp->flags & RTE_MEMPOOL_F_NO_IOVA_CONTIG) && iova != RTE_BAD_IOVA)
371c47d7b90SAndrew Rybchenko mp->flags &= ~RTE_MEMPOOL_F_NON_IO;
37211541c5cSDmitry Kozlyuk
373da2b9cb2SDmitry Kozlyuk /* Report the mempool as ready only when fully populated. */
374da2b9cb2SDmitry Kozlyuk if (mp->populated_size >= mp->size)
375da2b9cb2SDmitry Kozlyuk mempool_event_callback_invoke(RTE_MEMPOOL_EVENT_READY, mp);
376da2b9cb2SDmitry Kozlyuk
37799a2dd95SBruce Richardson rte_mempool_trace_populate_iova(mp, vaddr, iova, len, free_cb, opaque);
37899a2dd95SBruce Richardson return i;
37999a2dd95SBruce Richardson
38099a2dd95SBruce Richardson fail:
38199a2dd95SBruce Richardson rte_free(memhdr);
38299a2dd95SBruce Richardson return ret;
38399a2dd95SBruce Richardson }
38499a2dd95SBruce Richardson
38599a2dd95SBruce Richardson static rte_iova_t
get_iova(void * addr)38699a2dd95SBruce Richardson get_iova(void *addr)
38799a2dd95SBruce Richardson {
38899a2dd95SBruce Richardson struct rte_memseg *ms;
38999a2dd95SBruce Richardson
39099a2dd95SBruce Richardson /* try registered memory first */
39199a2dd95SBruce Richardson ms = rte_mem_virt2memseg(addr, NULL);
39299a2dd95SBruce Richardson if (ms == NULL || ms->iova == RTE_BAD_IOVA)
39399a2dd95SBruce Richardson /* fall back to actual physical address */
39499a2dd95SBruce Richardson return rte_mem_virt2iova(addr);
39599a2dd95SBruce Richardson return ms->iova + RTE_PTR_DIFF(addr, ms->addr);
39699a2dd95SBruce Richardson }
39799a2dd95SBruce Richardson
39899a2dd95SBruce Richardson /* Populate the mempool with a virtual area. Return the number of
39999a2dd95SBruce Richardson * objects added, or a negative value on error.
40099a2dd95SBruce Richardson */
40199a2dd95SBruce Richardson int
rte_mempool_populate_virt(struct rte_mempool * mp,char * addr,size_t len,size_t pg_sz,rte_mempool_memchunk_free_cb_t * free_cb,void * opaque)40299a2dd95SBruce Richardson rte_mempool_populate_virt(struct rte_mempool *mp, char *addr,
40399a2dd95SBruce Richardson size_t len, size_t pg_sz, rte_mempool_memchunk_free_cb_t *free_cb,
40499a2dd95SBruce Richardson void *opaque)
40599a2dd95SBruce Richardson {
40699a2dd95SBruce Richardson rte_iova_t iova;
40799a2dd95SBruce Richardson size_t off, phys_len;
40899a2dd95SBruce Richardson int ret, cnt = 0;
40999a2dd95SBruce Richardson
410c47d7b90SAndrew Rybchenko if (mp->flags & RTE_MEMPOOL_F_NO_IOVA_CONTIG)
41199a2dd95SBruce Richardson return rte_mempool_populate_iova(mp, addr, RTE_BAD_IOVA,
41299a2dd95SBruce Richardson len, free_cb, opaque);
41399a2dd95SBruce Richardson
41499a2dd95SBruce Richardson for (off = 0; off < len &&
41599a2dd95SBruce Richardson mp->populated_size < mp->size; off += phys_len) {
41699a2dd95SBruce Richardson
41799a2dd95SBruce Richardson iova = get_iova(addr + off);
41899a2dd95SBruce Richardson
41999a2dd95SBruce Richardson /* populate with the largest group of contiguous pages */
42099a2dd95SBruce Richardson for (phys_len = RTE_MIN(
42199a2dd95SBruce Richardson (size_t)(RTE_PTR_ALIGN_CEIL(addr + off + 1, pg_sz) -
42299a2dd95SBruce Richardson (addr + off)),
42399a2dd95SBruce Richardson len - off);
42499a2dd95SBruce Richardson off + phys_len < len;
42599a2dd95SBruce Richardson phys_len = RTE_MIN(phys_len + pg_sz, len - off)) {
42699a2dd95SBruce Richardson rte_iova_t iova_tmp;
42799a2dd95SBruce Richardson
42899a2dd95SBruce Richardson iova_tmp = get_iova(addr + off + phys_len);
42999a2dd95SBruce Richardson
43099a2dd95SBruce Richardson if (iova_tmp == RTE_BAD_IOVA ||
43199a2dd95SBruce Richardson iova_tmp != iova + phys_len)
43299a2dd95SBruce Richardson break;
43399a2dd95SBruce Richardson }
43499a2dd95SBruce Richardson
43599a2dd95SBruce Richardson ret = rte_mempool_populate_iova(mp, addr + off, iova,
43699a2dd95SBruce Richardson phys_len, free_cb, opaque);
43799a2dd95SBruce Richardson if (ret == 0)
43899a2dd95SBruce Richardson continue;
43999a2dd95SBruce Richardson if (ret < 0)
44099a2dd95SBruce Richardson goto fail;
44199a2dd95SBruce Richardson /* no need to call the free callback for next chunks */
44299a2dd95SBruce Richardson free_cb = NULL;
44399a2dd95SBruce Richardson cnt += ret;
44499a2dd95SBruce Richardson }
44599a2dd95SBruce Richardson
44699a2dd95SBruce Richardson rte_mempool_trace_populate_virt(mp, addr, len, pg_sz, free_cb, opaque);
44799a2dd95SBruce Richardson return cnt;
44899a2dd95SBruce Richardson
44999a2dd95SBruce Richardson fail:
45099a2dd95SBruce Richardson rte_mempool_free_memchunks(mp);
45199a2dd95SBruce Richardson return ret;
45299a2dd95SBruce Richardson }
45399a2dd95SBruce Richardson
45499a2dd95SBruce Richardson /* Get the minimal page size used in a mempool before populating it. */
45599a2dd95SBruce Richardson int
rte_mempool_get_page_size(struct rte_mempool * mp,size_t * pg_sz)45699a2dd95SBruce Richardson rte_mempool_get_page_size(struct rte_mempool *mp, size_t *pg_sz)
45799a2dd95SBruce Richardson {
45899a2dd95SBruce Richardson bool need_iova_contig_obj;
45999a2dd95SBruce Richardson bool alloc_in_ext_mem;
46099a2dd95SBruce Richardson int ret;
46199a2dd95SBruce Richardson
46299a2dd95SBruce Richardson /* check if we can retrieve a valid socket ID */
46399a2dd95SBruce Richardson ret = rte_malloc_heap_socket_is_external(mp->socket_id);
46499a2dd95SBruce Richardson if (ret < 0)
46599a2dd95SBruce Richardson return -EINVAL;
46699a2dd95SBruce Richardson alloc_in_ext_mem = (ret == 1);
467c47d7b90SAndrew Rybchenko need_iova_contig_obj = !(mp->flags & RTE_MEMPOOL_F_NO_IOVA_CONTIG);
46899a2dd95SBruce Richardson
46999a2dd95SBruce Richardson if (!need_iova_contig_obj)
47099a2dd95SBruce Richardson *pg_sz = 0;
47199a2dd95SBruce Richardson else if (rte_eal_has_hugepages() || alloc_in_ext_mem)
47299a2dd95SBruce Richardson *pg_sz = get_min_page_size(mp->socket_id);
47399a2dd95SBruce Richardson else
47499a2dd95SBruce Richardson *pg_sz = rte_mem_page_size();
47599a2dd95SBruce Richardson
47699a2dd95SBruce Richardson rte_mempool_trace_get_page_size(mp, *pg_sz);
47799a2dd95SBruce Richardson return 0;
47899a2dd95SBruce Richardson }
47999a2dd95SBruce Richardson
48099a2dd95SBruce Richardson /* Default function to populate the mempool: allocate memory in memzones,
48199a2dd95SBruce Richardson * and populate them. Return the number of objects added, or a negative
48299a2dd95SBruce Richardson * value on error.
48399a2dd95SBruce Richardson */
48499a2dd95SBruce Richardson int
rte_mempool_populate_default(struct rte_mempool * mp)48599a2dd95SBruce Richardson rte_mempool_populate_default(struct rte_mempool *mp)
48699a2dd95SBruce Richardson {
48799a2dd95SBruce Richardson unsigned int mz_flags = RTE_MEMZONE_1GB|RTE_MEMZONE_SIZE_HINT_ONLY;
48899a2dd95SBruce Richardson char mz_name[RTE_MEMZONE_NAMESIZE];
48999a2dd95SBruce Richardson const struct rte_memzone *mz;
49099a2dd95SBruce Richardson ssize_t mem_size;
49199a2dd95SBruce Richardson size_t align, pg_sz, pg_shift = 0;
49299a2dd95SBruce Richardson rte_iova_t iova;
49399a2dd95SBruce Richardson unsigned mz_id, n;
49499a2dd95SBruce Richardson int ret;
49599a2dd95SBruce Richardson bool need_iova_contig_obj;
49699a2dd95SBruce Richardson size_t max_alloc_size = SIZE_MAX;
49799a2dd95SBruce Richardson
49899a2dd95SBruce Richardson ret = mempool_ops_alloc_once(mp);
49999a2dd95SBruce Richardson if (ret != 0)
50099a2dd95SBruce Richardson return ret;
50199a2dd95SBruce Richardson
50299a2dd95SBruce Richardson /* mempool must not be populated */
50399a2dd95SBruce Richardson if (mp->nb_mem_chunks != 0)
50499a2dd95SBruce Richardson return -EEXIST;
50599a2dd95SBruce Richardson
50699a2dd95SBruce Richardson /*
50799a2dd95SBruce Richardson * the following section calculates page shift and page size values.
50899a2dd95SBruce Richardson *
50999a2dd95SBruce Richardson * these values impact the result of calc_mem_size operation, which
51099a2dd95SBruce Richardson * returns the amount of memory that should be allocated to store the
51199a2dd95SBruce Richardson * desired number of objects. when not zero, it allocates more memory
51299a2dd95SBruce Richardson * for the padding between objects, to ensure that an object does not
51399a2dd95SBruce Richardson * cross a page boundary. in other words, page size/shift are to be set
51499a2dd95SBruce Richardson * to zero if mempool elements won't care about page boundaries.
51599a2dd95SBruce Richardson * there are several considerations for page size and page shift here.
51699a2dd95SBruce Richardson *
51799a2dd95SBruce Richardson * if we don't need our mempools to have physically contiguous objects,
51899a2dd95SBruce Richardson * then just set page shift and page size to 0, because the user has
51999a2dd95SBruce Richardson * indicated that there's no need to care about anything.
52099a2dd95SBruce Richardson *
52199a2dd95SBruce Richardson * if we do need contiguous objects (if a mempool driver has its
52299a2dd95SBruce Richardson * own calc_size() method returning min_chunk_size = mem_size),
52399a2dd95SBruce Richardson * there is also an option to reserve the entire mempool memory
52499a2dd95SBruce Richardson * as one contiguous block of memory.
52599a2dd95SBruce Richardson *
52699a2dd95SBruce Richardson * if we require contiguous objects, but not necessarily the entire
52799a2dd95SBruce Richardson * mempool reserved space to be contiguous, pg_sz will be != 0,
52899a2dd95SBruce Richardson * and the default ops->populate() will take care of not placing
52999a2dd95SBruce Richardson * objects across pages.
53099a2dd95SBruce Richardson *
53199a2dd95SBruce Richardson * if our IO addresses are physical, we may get memory from bigger
53299a2dd95SBruce Richardson * pages, or we might get memory from smaller pages, and how much of it
53399a2dd95SBruce Richardson * we require depends on whether we want bigger or smaller pages.
53499a2dd95SBruce Richardson * However, requesting each and every memory size is too much work, so
53599a2dd95SBruce Richardson * what we'll do instead is walk through the page sizes available, pick
53699a2dd95SBruce Richardson * the smallest one and set up page shift to match that one. We will be
53799a2dd95SBruce Richardson * wasting some space this way, but it's much nicer than looping around
53899a2dd95SBruce Richardson * trying to reserve each and every page size.
53999a2dd95SBruce Richardson *
54099a2dd95SBruce Richardson * If we fail to get enough contiguous memory, then we'll go and
54199a2dd95SBruce Richardson * reserve space in smaller chunks.
54299a2dd95SBruce Richardson */
54399a2dd95SBruce Richardson
544c47d7b90SAndrew Rybchenko need_iova_contig_obj = !(mp->flags & RTE_MEMPOOL_F_NO_IOVA_CONTIG);
54599a2dd95SBruce Richardson ret = rte_mempool_get_page_size(mp, &pg_sz);
54699a2dd95SBruce Richardson if (ret < 0)
54799a2dd95SBruce Richardson return ret;
54899a2dd95SBruce Richardson
54999a2dd95SBruce Richardson if (pg_sz != 0)
55099a2dd95SBruce Richardson pg_shift = rte_bsf32(pg_sz);
55199a2dd95SBruce Richardson
55299a2dd95SBruce Richardson for (mz_id = 0, n = mp->size; n > 0; mz_id++, n -= ret) {
55399a2dd95SBruce Richardson size_t min_chunk_size;
55499a2dd95SBruce Richardson
55599a2dd95SBruce Richardson mem_size = rte_mempool_ops_calc_mem_size(
55699a2dd95SBruce Richardson mp, n, pg_shift, &min_chunk_size, &align);
55799a2dd95SBruce Richardson
55899a2dd95SBruce Richardson if (mem_size < 0) {
55999a2dd95SBruce Richardson ret = mem_size;
56099a2dd95SBruce Richardson goto fail;
56199a2dd95SBruce Richardson }
56299a2dd95SBruce Richardson
56399a2dd95SBruce Richardson ret = snprintf(mz_name, sizeof(mz_name),
56499a2dd95SBruce Richardson RTE_MEMPOOL_MZ_FORMAT "_%d", mp->name, mz_id);
56599a2dd95SBruce Richardson if (ret < 0 || ret >= (int)sizeof(mz_name)) {
56699a2dd95SBruce Richardson ret = -ENAMETOOLONG;
56799a2dd95SBruce Richardson goto fail;
56899a2dd95SBruce Richardson }
56999a2dd95SBruce Richardson
57099a2dd95SBruce Richardson /* if we're trying to reserve contiguous memory, add appropriate
57199a2dd95SBruce Richardson * memzone flag.
57299a2dd95SBruce Richardson */
57399a2dd95SBruce Richardson if (min_chunk_size == (size_t)mem_size)
57499a2dd95SBruce Richardson mz_flags |= RTE_MEMZONE_IOVA_CONTIG;
57599a2dd95SBruce Richardson
57699a2dd95SBruce Richardson /* Allocate a memzone, retrying with a smaller area on ENOMEM */
57799a2dd95SBruce Richardson do {
57899a2dd95SBruce Richardson mz = rte_memzone_reserve_aligned(mz_name,
57999a2dd95SBruce Richardson RTE_MIN((size_t)mem_size, max_alloc_size),
58099a2dd95SBruce Richardson mp->socket_id, mz_flags, align);
58199a2dd95SBruce Richardson
58299a2dd95SBruce Richardson if (mz != NULL || rte_errno != ENOMEM)
58399a2dd95SBruce Richardson break;
58499a2dd95SBruce Richardson
58599a2dd95SBruce Richardson max_alloc_size = RTE_MIN(max_alloc_size,
58699a2dd95SBruce Richardson (size_t)mem_size) / 2;
58799a2dd95SBruce Richardson } while (mz == NULL && max_alloc_size >= min_chunk_size);
58899a2dd95SBruce Richardson
58999a2dd95SBruce Richardson if (mz == NULL) {
59099a2dd95SBruce Richardson ret = -rte_errno;
59199a2dd95SBruce Richardson goto fail;
59299a2dd95SBruce Richardson }
59399a2dd95SBruce Richardson
59499a2dd95SBruce Richardson if (need_iova_contig_obj)
59599a2dd95SBruce Richardson iova = mz->iova;
59699a2dd95SBruce Richardson else
59799a2dd95SBruce Richardson iova = RTE_BAD_IOVA;
59899a2dd95SBruce Richardson
59999a2dd95SBruce Richardson if (pg_sz == 0 || (mz_flags & RTE_MEMZONE_IOVA_CONTIG))
60099a2dd95SBruce Richardson ret = rte_mempool_populate_iova(mp, mz->addr,
60199a2dd95SBruce Richardson iova, mz->len,
60299a2dd95SBruce Richardson rte_mempool_memchunk_mz_free,
60399a2dd95SBruce Richardson (void *)(uintptr_t)mz);
60499a2dd95SBruce Richardson else
60599a2dd95SBruce Richardson ret = rte_mempool_populate_virt(mp, mz->addr,
60699a2dd95SBruce Richardson mz->len, pg_sz,
60799a2dd95SBruce Richardson rte_mempool_memchunk_mz_free,
60899a2dd95SBruce Richardson (void *)(uintptr_t)mz);
60999a2dd95SBruce Richardson if (ret == 0) /* should not happen */
61099a2dd95SBruce Richardson ret = -ENOBUFS;
61199a2dd95SBruce Richardson if (ret < 0) {
61299a2dd95SBruce Richardson rte_memzone_free(mz);
61399a2dd95SBruce Richardson goto fail;
61499a2dd95SBruce Richardson }
61599a2dd95SBruce Richardson }
61699a2dd95SBruce Richardson
61799a2dd95SBruce Richardson rte_mempool_trace_populate_default(mp);
61899a2dd95SBruce Richardson return mp->size;
61999a2dd95SBruce Richardson
62099a2dd95SBruce Richardson fail:
62199a2dd95SBruce Richardson rte_mempool_free_memchunks(mp);
62299a2dd95SBruce Richardson return ret;
62399a2dd95SBruce Richardson }
62499a2dd95SBruce Richardson
62599a2dd95SBruce Richardson /* return the memory size required for mempool objects in anonymous mem */
62699a2dd95SBruce Richardson static ssize_t
get_anon_size(const struct rte_mempool * mp)62799a2dd95SBruce Richardson get_anon_size(const struct rte_mempool *mp)
62899a2dd95SBruce Richardson {
62999a2dd95SBruce Richardson ssize_t size;
63099a2dd95SBruce Richardson size_t pg_sz, pg_shift;
63199a2dd95SBruce Richardson size_t min_chunk_size;
63299a2dd95SBruce Richardson size_t align;
63399a2dd95SBruce Richardson
63499a2dd95SBruce Richardson pg_sz = rte_mem_page_size();
63599a2dd95SBruce Richardson pg_shift = rte_bsf32(pg_sz);
63699a2dd95SBruce Richardson size = rte_mempool_ops_calc_mem_size(mp, mp->size, pg_shift,
63799a2dd95SBruce Richardson &min_chunk_size, &align);
63899a2dd95SBruce Richardson
63999a2dd95SBruce Richardson return size;
64099a2dd95SBruce Richardson }
64199a2dd95SBruce Richardson
64299a2dd95SBruce Richardson /* unmap a memory zone mapped by rte_mempool_populate_anon() */
64399a2dd95SBruce Richardson static void
rte_mempool_memchunk_anon_free(struct rte_mempool_memhdr * memhdr,void * opaque)64499a2dd95SBruce Richardson rte_mempool_memchunk_anon_free(struct rte_mempool_memhdr *memhdr,
64599a2dd95SBruce Richardson void *opaque)
64699a2dd95SBruce Richardson {
64799a2dd95SBruce Richardson ssize_t size;
64899a2dd95SBruce Richardson
64999a2dd95SBruce Richardson /*
65099a2dd95SBruce Richardson * Calculate size since memhdr->len has contiguous chunk length
65199a2dd95SBruce Richardson * which may be smaller if anon map is split into many contiguous
65299a2dd95SBruce Richardson * chunks. Result must be the same as we calculated on populate.
65399a2dd95SBruce Richardson */
65499a2dd95SBruce Richardson size = get_anon_size(memhdr->mp);
65599a2dd95SBruce Richardson if (size < 0)
65699a2dd95SBruce Richardson return;
65799a2dd95SBruce Richardson
65899a2dd95SBruce Richardson rte_mem_unmap(opaque, size);
65999a2dd95SBruce Richardson }
66099a2dd95SBruce Richardson
66199a2dd95SBruce Richardson /* populate the mempool with an anonymous mapping */
66299a2dd95SBruce Richardson int
rte_mempool_populate_anon(struct rte_mempool * mp)66399a2dd95SBruce Richardson rte_mempool_populate_anon(struct rte_mempool *mp)
66499a2dd95SBruce Richardson {
66599a2dd95SBruce Richardson ssize_t size;
66699a2dd95SBruce Richardson int ret;
66799a2dd95SBruce Richardson char *addr;
66899a2dd95SBruce Richardson
66999a2dd95SBruce Richardson /* mempool is already populated, error */
67099a2dd95SBruce Richardson if ((!STAILQ_EMPTY(&mp->mem_list)) || mp->nb_mem_chunks != 0) {
67199a2dd95SBruce Richardson rte_errno = EINVAL;
67299a2dd95SBruce Richardson return 0;
67399a2dd95SBruce Richardson }
67499a2dd95SBruce Richardson
67599a2dd95SBruce Richardson ret = mempool_ops_alloc_once(mp);
67699a2dd95SBruce Richardson if (ret < 0) {
67799a2dd95SBruce Richardson rte_errno = -ret;
67899a2dd95SBruce Richardson return 0;
67999a2dd95SBruce Richardson }
68099a2dd95SBruce Richardson
68199a2dd95SBruce Richardson size = get_anon_size(mp);
68299a2dd95SBruce Richardson if (size < 0) {
68399a2dd95SBruce Richardson rte_errno = -size;
68499a2dd95SBruce Richardson return 0;
68599a2dd95SBruce Richardson }
68699a2dd95SBruce Richardson
68799a2dd95SBruce Richardson /* get chunk of virtually continuous memory */
68899a2dd95SBruce Richardson addr = rte_mem_map(NULL, size, RTE_PROT_READ | RTE_PROT_WRITE,
68999a2dd95SBruce Richardson RTE_MAP_SHARED | RTE_MAP_ANONYMOUS, -1, 0);
69099a2dd95SBruce Richardson if (addr == NULL)
69199a2dd95SBruce Richardson return 0;
69299a2dd95SBruce Richardson /* can't use MMAP_LOCKED, it does not exist on BSD */
69399a2dd95SBruce Richardson if (rte_mem_lock(addr, size) < 0) {
69499a2dd95SBruce Richardson rte_mem_unmap(addr, size);
69599a2dd95SBruce Richardson return 0;
69699a2dd95SBruce Richardson }
69799a2dd95SBruce Richardson
69899a2dd95SBruce Richardson ret = rte_mempool_populate_virt(mp, addr, size, rte_mem_page_size(),
69999a2dd95SBruce Richardson rte_mempool_memchunk_anon_free, addr);
70099a2dd95SBruce Richardson if (ret == 0) /* should not happen */
70199a2dd95SBruce Richardson ret = -ENOBUFS;
70299a2dd95SBruce Richardson if (ret < 0) {
70399a2dd95SBruce Richardson rte_errno = -ret;
70499a2dd95SBruce Richardson goto fail;
70599a2dd95SBruce Richardson }
70699a2dd95SBruce Richardson
70799a2dd95SBruce Richardson rte_mempool_trace_populate_anon(mp);
70899a2dd95SBruce Richardson return mp->populated_size;
70999a2dd95SBruce Richardson
71099a2dd95SBruce Richardson fail:
71199a2dd95SBruce Richardson rte_mempool_free_memchunks(mp);
71299a2dd95SBruce Richardson return 0;
71399a2dd95SBruce Richardson }
71499a2dd95SBruce Richardson
71599a2dd95SBruce Richardson /* free a mempool */
71699a2dd95SBruce Richardson void
rte_mempool_free(struct rte_mempool * mp)71799a2dd95SBruce Richardson rte_mempool_free(struct rte_mempool *mp)
71899a2dd95SBruce Richardson {
71999a2dd95SBruce Richardson struct rte_mempool_list *mempool_list = NULL;
72099a2dd95SBruce Richardson struct rte_tailq_entry *te;
72199a2dd95SBruce Richardson
72299a2dd95SBruce Richardson if (mp == NULL)
72399a2dd95SBruce Richardson return;
72499a2dd95SBruce Richardson
72599a2dd95SBruce Richardson mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
72699a2dd95SBruce Richardson rte_mcfg_tailq_write_lock();
72799a2dd95SBruce Richardson /* find out tailq entry */
72899a2dd95SBruce Richardson TAILQ_FOREACH(te, mempool_list, next) {
72999a2dd95SBruce Richardson if (te->data == (void *)mp)
73099a2dd95SBruce Richardson break;
73199a2dd95SBruce Richardson }
73299a2dd95SBruce Richardson
73399a2dd95SBruce Richardson if (te != NULL) {
73499a2dd95SBruce Richardson TAILQ_REMOVE(mempool_list, te, next);
73599a2dd95SBruce Richardson rte_free(te);
73699a2dd95SBruce Richardson }
73799a2dd95SBruce Richardson rte_mcfg_tailq_write_unlock();
73899a2dd95SBruce Richardson
739da2b9cb2SDmitry Kozlyuk mempool_event_callback_invoke(RTE_MEMPOOL_EVENT_DESTROY, mp);
74099a2dd95SBruce Richardson rte_mempool_trace_free(mp);
74199a2dd95SBruce Richardson rte_mempool_free_memchunks(mp);
74299a2dd95SBruce Richardson rte_mempool_ops_free(mp);
74399a2dd95SBruce Richardson rte_memzone_free(mp->mz);
74499a2dd95SBruce Richardson }
74599a2dd95SBruce Richardson
74699a2dd95SBruce Richardson static void
mempool_cache_init(struct rte_mempool_cache * cache,uint32_t size)74799a2dd95SBruce Richardson mempool_cache_init(struct rte_mempool_cache *cache, uint32_t size)
74899a2dd95SBruce Richardson {
749459531c9SMorten Brørup /* Check that cache have enough space for flush threshold */
750459531c9SMorten Brørup RTE_BUILD_BUG_ON(CALC_CACHE_FLUSHTHRESH(RTE_MEMPOOL_CACHE_MAX_SIZE) >
751459531c9SMorten Brørup RTE_SIZEOF_FIELD(struct rte_mempool_cache, objs) /
752459531c9SMorten Brørup RTE_SIZEOF_FIELD(struct rte_mempool_cache, objs[0]));
753459531c9SMorten Brørup
75499a2dd95SBruce Richardson cache->size = size;
75599a2dd95SBruce Richardson cache->flushthresh = CALC_CACHE_FLUSHTHRESH(size);
75699a2dd95SBruce Richardson cache->len = 0;
75799a2dd95SBruce Richardson }
75899a2dd95SBruce Richardson
75999a2dd95SBruce Richardson /*
76099a2dd95SBruce Richardson * Create and initialize a cache for objects that are retrieved from and
76199a2dd95SBruce Richardson * returned to an underlying mempool. This structure is identical to the
76299a2dd95SBruce Richardson * local_cache[lcore_id] pointed to by the mempool structure.
76399a2dd95SBruce Richardson */
76499a2dd95SBruce Richardson struct rte_mempool_cache *
rte_mempool_cache_create(uint32_t size,int socket_id)76599a2dd95SBruce Richardson rte_mempool_cache_create(uint32_t size, int socket_id)
76699a2dd95SBruce Richardson {
76799a2dd95SBruce Richardson struct rte_mempool_cache *cache;
76899a2dd95SBruce Richardson
76999a2dd95SBruce Richardson if (size == 0 || size > RTE_MEMPOOL_CACHE_MAX_SIZE) {
77099a2dd95SBruce Richardson rte_errno = EINVAL;
77199a2dd95SBruce Richardson return NULL;
77299a2dd95SBruce Richardson }
77399a2dd95SBruce Richardson
77499a2dd95SBruce Richardson cache = rte_zmalloc_socket("MEMPOOL_CACHE", sizeof(*cache),
77599a2dd95SBruce Richardson RTE_CACHE_LINE_SIZE, socket_id);
77699a2dd95SBruce Richardson if (cache == NULL) {
777ae67895bSDavid Marchand RTE_MEMPOOL_LOG(ERR, "Cannot allocate mempool cache.");
77899a2dd95SBruce Richardson rte_errno = ENOMEM;
77999a2dd95SBruce Richardson return NULL;
78099a2dd95SBruce Richardson }
78199a2dd95SBruce Richardson
78299a2dd95SBruce Richardson mempool_cache_init(cache, size);
78399a2dd95SBruce Richardson
78499a2dd95SBruce Richardson rte_mempool_trace_cache_create(size, socket_id, cache);
78599a2dd95SBruce Richardson return cache;
78699a2dd95SBruce Richardson }
78799a2dd95SBruce Richardson
78899a2dd95SBruce Richardson /*
78999a2dd95SBruce Richardson * Free a cache. It's the responsibility of the user to make sure that any
79099a2dd95SBruce Richardson * remaining objects in the cache are flushed to the corresponding
79199a2dd95SBruce Richardson * mempool.
79299a2dd95SBruce Richardson */
79399a2dd95SBruce Richardson void
rte_mempool_cache_free(struct rte_mempool_cache * cache)79499a2dd95SBruce Richardson rte_mempool_cache_free(struct rte_mempool_cache *cache)
79599a2dd95SBruce Richardson {
79699a2dd95SBruce Richardson rte_mempool_trace_cache_free(cache);
79799a2dd95SBruce Richardson rte_free(cache);
79899a2dd95SBruce Richardson }
79999a2dd95SBruce Richardson
80099a2dd95SBruce Richardson /* create an empty mempool */
80199a2dd95SBruce Richardson struct rte_mempool *
rte_mempool_create_empty(const char * name,unsigned n,unsigned elt_size,unsigned cache_size,unsigned private_data_size,int socket_id,unsigned flags)80299a2dd95SBruce Richardson rte_mempool_create_empty(const char *name, unsigned n, unsigned elt_size,
80399a2dd95SBruce Richardson unsigned cache_size, unsigned private_data_size,
80499a2dd95SBruce Richardson int socket_id, unsigned flags)
80599a2dd95SBruce Richardson {
80699a2dd95SBruce Richardson char mz_name[RTE_MEMZONE_NAMESIZE];
80799a2dd95SBruce Richardson struct rte_mempool_list *mempool_list;
80899a2dd95SBruce Richardson struct rte_mempool *mp = NULL;
80999a2dd95SBruce Richardson struct rte_tailq_entry *te = NULL;
81099a2dd95SBruce Richardson const struct rte_memzone *mz = NULL;
81199a2dd95SBruce Richardson size_t mempool_size;
81299a2dd95SBruce Richardson unsigned int mz_flags = RTE_MEMZONE_1GB|RTE_MEMZONE_SIZE_HINT_ONLY;
81399a2dd95SBruce Richardson struct rte_mempool_objsz objsz;
81499a2dd95SBruce Richardson unsigned lcore_id;
81599a2dd95SBruce Richardson int ret;
81699a2dd95SBruce Richardson
81799a2dd95SBruce Richardson /* compilation-time checks */
81899a2dd95SBruce Richardson RTE_BUILD_BUG_ON((sizeof(struct rte_mempool) &
81999a2dd95SBruce Richardson RTE_CACHE_LINE_MASK) != 0);
82099a2dd95SBruce Richardson RTE_BUILD_BUG_ON((sizeof(struct rte_mempool_cache) &
82199a2dd95SBruce Richardson RTE_CACHE_LINE_MASK) != 0);
8229d87e05dSMorten Brørup #ifdef RTE_LIBRTE_MEMPOOL_STATS
82399a2dd95SBruce Richardson RTE_BUILD_BUG_ON((sizeof(struct rte_mempool_debug_stats) &
82499a2dd95SBruce Richardson RTE_CACHE_LINE_MASK) != 0);
82599a2dd95SBruce Richardson RTE_BUILD_BUG_ON((offsetof(struct rte_mempool, stats) &
82699a2dd95SBruce Richardson RTE_CACHE_LINE_MASK) != 0);
82799a2dd95SBruce Richardson #endif
82899a2dd95SBruce Richardson
82999a2dd95SBruce Richardson mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
83099a2dd95SBruce Richardson
83199a2dd95SBruce Richardson /* asked for zero items */
83299a2dd95SBruce Richardson if (n == 0) {
83399a2dd95SBruce Richardson rte_errno = EINVAL;
83499a2dd95SBruce Richardson return NULL;
83599a2dd95SBruce Richardson }
83699a2dd95SBruce Richardson
83799a2dd95SBruce Richardson /* asked cache too big */
83899a2dd95SBruce Richardson if (cache_size > RTE_MEMPOOL_CACHE_MAX_SIZE ||
83999a2dd95SBruce Richardson CALC_CACHE_FLUSHTHRESH(cache_size) > n) {
84099a2dd95SBruce Richardson rte_errno = EINVAL;
84199a2dd95SBruce Richardson return NULL;
84299a2dd95SBruce Richardson }
84399a2dd95SBruce Richardson
844afdaa607SDavid Marchand /* enforce only user flags are passed by the application */
845afdaa607SDavid Marchand if ((flags & ~RTE_MEMPOOL_VALID_USER_FLAGS) != 0) {
846b240af8bSDavid Marchand rte_errno = EINVAL;
847b240af8bSDavid Marchand return NULL;
848b240af8bSDavid Marchand }
849b240af8bSDavid Marchand
85011541c5cSDmitry Kozlyuk /*
85111541c5cSDmitry Kozlyuk * No objects in the pool can be used for IO until it's populated
85211541c5cSDmitry Kozlyuk * with at least some objects with valid IOVA.
85311541c5cSDmitry Kozlyuk */
854c47d7b90SAndrew Rybchenko flags |= RTE_MEMPOOL_F_NON_IO;
85511541c5cSDmitry Kozlyuk
85699a2dd95SBruce Richardson /* "no cache align" imply "no spread" */
857c47d7b90SAndrew Rybchenko if (flags & RTE_MEMPOOL_F_NO_CACHE_ALIGN)
858c47d7b90SAndrew Rybchenko flags |= RTE_MEMPOOL_F_NO_SPREAD;
85999a2dd95SBruce Richardson
86099a2dd95SBruce Richardson /* calculate mempool object sizes. */
86199a2dd95SBruce Richardson if (!rte_mempool_calc_obj_size(elt_size, flags, &objsz)) {
86299a2dd95SBruce Richardson rte_errno = EINVAL;
86399a2dd95SBruce Richardson return NULL;
86499a2dd95SBruce Richardson }
86599a2dd95SBruce Richardson
86699a2dd95SBruce Richardson rte_mcfg_mempool_write_lock();
86799a2dd95SBruce Richardson
86899a2dd95SBruce Richardson /*
86999a2dd95SBruce Richardson * reserve a memory zone for this mempool: private data is
87099a2dd95SBruce Richardson * cache-aligned
87199a2dd95SBruce Richardson */
87299a2dd95SBruce Richardson private_data_size = (private_data_size +
87399a2dd95SBruce Richardson RTE_MEMPOOL_ALIGN_MASK) & (~RTE_MEMPOOL_ALIGN_MASK);
87499a2dd95SBruce Richardson
87599a2dd95SBruce Richardson
87699a2dd95SBruce Richardson /* try to allocate tailq entry */
87799a2dd95SBruce Richardson te = rte_zmalloc("MEMPOOL_TAILQ_ENTRY", sizeof(*te), 0);
87899a2dd95SBruce Richardson if (te == NULL) {
879ae67895bSDavid Marchand RTE_MEMPOOL_LOG(ERR, "Cannot allocate tailq entry!");
88099a2dd95SBruce Richardson goto exit_unlock;
88199a2dd95SBruce Richardson }
88299a2dd95SBruce Richardson
883d7203661SAndrew Rybchenko mempool_size = RTE_MEMPOOL_HEADER_SIZE(mp, cache_size);
88499a2dd95SBruce Richardson mempool_size += private_data_size;
88599a2dd95SBruce Richardson mempool_size = RTE_ALIGN_CEIL(mempool_size, RTE_MEMPOOL_ALIGN);
88699a2dd95SBruce Richardson
88799a2dd95SBruce Richardson ret = snprintf(mz_name, sizeof(mz_name), RTE_MEMPOOL_MZ_FORMAT, name);
88899a2dd95SBruce Richardson if (ret < 0 || ret >= (int)sizeof(mz_name)) {
88999a2dd95SBruce Richardson rte_errno = ENAMETOOLONG;
89099a2dd95SBruce Richardson goto exit_unlock;
89199a2dd95SBruce Richardson }
89299a2dd95SBruce Richardson
89399a2dd95SBruce Richardson mz = rte_memzone_reserve(mz_name, mempool_size, socket_id, mz_flags);
89499a2dd95SBruce Richardson if (mz == NULL)
89599a2dd95SBruce Richardson goto exit_unlock;
89699a2dd95SBruce Richardson
89799a2dd95SBruce Richardson /* init the mempool structure */
89899a2dd95SBruce Richardson mp = mz->addr;
899d7203661SAndrew Rybchenko memset(mp, 0, RTE_MEMPOOL_HEADER_SIZE(mp, cache_size));
90099a2dd95SBruce Richardson ret = strlcpy(mp->name, name, sizeof(mp->name));
90199a2dd95SBruce Richardson if (ret < 0 || ret >= (int)sizeof(mp->name)) {
90299a2dd95SBruce Richardson rte_errno = ENAMETOOLONG;
90399a2dd95SBruce Richardson goto exit_unlock;
90499a2dd95SBruce Richardson }
90599a2dd95SBruce Richardson mp->mz = mz;
90699a2dd95SBruce Richardson mp->size = n;
90799a2dd95SBruce Richardson mp->flags = flags;
90899a2dd95SBruce Richardson mp->socket_id = socket_id;
90999a2dd95SBruce Richardson mp->elt_size = objsz.elt_size;
91099a2dd95SBruce Richardson mp->header_size = objsz.header_size;
91199a2dd95SBruce Richardson mp->trailer_size = objsz.trailer_size;
91299a2dd95SBruce Richardson /* Size of default caches, zero means disabled. */
91399a2dd95SBruce Richardson mp->cache_size = cache_size;
91499a2dd95SBruce Richardson mp->private_data_size = private_data_size;
91599a2dd95SBruce Richardson STAILQ_INIT(&mp->elt_list);
91699a2dd95SBruce Richardson STAILQ_INIT(&mp->mem_list);
91799a2dd95SBruce Richardson
91899a2dd95SBruce Richardson /*
919c2c6b2f4SDavid Marchand * Since we have 4 combinations of the SP/SC/MP/MC examine the flags to
920c2c6b2f4SDavid Marchand * set the correct index into the table of ops structs.
921c2c6b2f4SDavid Marchand */
922c2c6b2f4SDavid Marchand if ((flags & RTE_MEMPOOL_F_SP_PUT) && (flags & RTE_MEMPOOL_F_SC_GET))
923c2c6b2f4SDavid Marchand ret = rte_mempool_set_ops_byname(mp, "ring_sp_sc", NULL);
924c2c6b2f4SDavid Marchand else if (flags & RTE_MEMPOOL_F_SP_PUT)
925c2c6b2f4SDavid Marchand ret = rte_mempool_set_ops_byname(mp, "ring_sp_mc", NULL);
926c2c6b2f4SDavid Marchand else if (flags & RTE_MEMPOOL_F_SC_GET)
927c2c6b2f4SDavid Marchand ret = rte_mempool_set_ops_byname(mp, "ring_mp_sc", NULL);
928c2c6b2f4SDavid Marchand else
929c2c6b2f4SDavid Marchand ret = rte_mempool_set_ops_byname(mp, "ring_mp_mc", NULL);
930c2c6b2f4SDavid Marchand
931c2c6b2f4SDavid Marchand if (ret)
932c2c6b2f4SDavid Marchand goto exit_unlock;
933c2c6b2f4SDavid Marchand
934c2c6b2f4SDavid Marchand /*
93599a2dd95SBruce Richardson * local_cache pointer is set even if cache_size is zero.
93699a2dd95SBruce Richardson * The local_cache points to just past the elt_pa[] array.
93799a2dd95SBruce Richardson */
93899a2dd95SBruce Richardson mp->local_cache = (struct rte_mempool_cache *)
939d7203661SAndrew Rybchenko RTE_PTR_ADD(mp, RTE_MEMPOOL_HEADER_SIZE(mp, 0));
94099a2dd95SBruce Richardson
94199a2dd95SBruce Richardson /* Init all default caches. */
94299a2dd95SBruce Richardson if (cache_size != 0) {
94399a2dd95SBruce Richardson for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++)
94499a2dd95SBruce Richardson mempool_cache_init(&mp->local_cache[lcore_id],
94599a2dd95SBruce Richardson cache_size);
94699a2dd95SBruce Richardson }
94799a2dd95SBruce Richardson
94899a2dd95SBruce Richardson te->data = mp;
94999a2dd95SBruce Richardson
95099a2dd95SBruce Richardson rte_mcfg_tailq_write_lock();
95199a2dd95SBruce Richardson TAILQ_INSERT_TAIL(mempool_list, te, next);
95299a2dd95SBruce Richardson rte_mcfg_tailq_write_unlock();
95399a2dd95SBruce Richardson rte_mcfg_mempool_write_unlock();
95499a2dd95SBruce Richardson
95599a2dd95SBruce Richardson rte_mempool_trace_create_empty(name, n, elt_size, cache_size,
95699a2dd95SBruce Richardson private_data_size, flags, mp);
95799a2dd95SBruce Richardson return mp;
95899a2dd95SBruce Richardson
95999a2dd95SBruce Richardson exit_unlock:
96099a2dd95SBruce Richardson rte_mcfg_mempool_write_unlock();
96199a2dd95SBruce Richardson rte_free(te);
96299a2dd95SBruce Richardson rte_mempool_free(mp);
96399a2dd95SBruce Richardson return NULL;
96499a2dd95SBruce Richardson }
96599a2dd95SBruce Richardson
96699a2dd95SBruce Richardson /* create the mempool */
96799a2dd95SBruce Richardson struct rte_mempool *
rte_mempool_create(const char * name,unsigned n,unsigned elt_size,unsigned cache_size,unsigned private_data_size,rte_mempool_ctor_t * mp_init,void * mp_init_arg,rte_mempool_obj_cb_t * obj_init,void * obj_init_arg,int socket_id,unsigned flags)96899a2dd95SBruce Richardson rte_mempool_create(const char *name, unsigned n, unsigned elt_size,
96999a2dd95SBruce Richardson unsigned cache_size, unsigned private_data_size,
97099a2dd95SBruce Richardson rte_mempool_ctor_t *mp_init, void *mp_init_arg,
97199a2dd95SBruce Richardson rte_mempool_obj_cb_t *obj_init, void *obj_init_arg,
97299a2dd95SBruce Richardson int socket_id, unsigned flags)
97399a2dd95SBruce Richardson {
97499a2dd95SBruce Richardson struct rte_mempool *mp;
97599a2dd95SBruce Richardson
97699a2dd95SBruce Richardson mp = rte_mempool_create_empty(name, n, elt_size, cache_size,
97799a2dd95SBruce Richardson private_data_size, socket_id, flags);
97899a2dd95SBruce Richardson if (mp == NULL)
97999a2dd95SBruce Richardson return NULL;
98099a2dd95SBruce Richardson
98199a2dd95SBruce Richardson /* call the mempool priv initializer */
98299a2dd95SBruce Richardson if (mp_init)
98399a2dd95SBruce Richardson mp_init(mp, mp_init_arg);
98499a2dd95SBruce Richardson
98599a2dd95SBruce Richardson if (rte_mempool_populate_default(mp) < 0)
98699a2dd95SBruce Richardson goto fail;
98799a2dd95SBruce Richardson
98899a2dd95SBruce Richardson /* call the object initializers */
98999a2dd95SBruce Richardson if (obj_init)
99099a2dd95SBruce Richardson rte_mempool_obj_iter(mp, obj_init, obj_init_arg);
99199a2dd95SBruce Richardson
99299a2dd95SBruce Richardson rte_mempool_trace_create(name, n, elt_size, cache_size,
99399a2dd95SBruce Richardson private_data_size, mp_init, mp_init_arg, obj_init,
99499a2dd95SBruce Richardson obj_init_arg, flags, mp);
99599a2dd95SBruce Richardson return mp;
99699a2dd95SBruce Richardson
99799a2dd95SBruce Richardson fail:
99899a2dd95SBruce Richardson rte_mempool_free(mp);
99999a2dd95SBruce Richardson return NULL;
100099a2dd95SBruce Richardson }
100199a2dd95SBruce Richardson
100299a2dd95SBruce Richardson /* Return the number of entries in the mempool */
100399a2dd95SBruce Richardson unsigned int
rte_mempool_avail_count(const struct rte_mempool * mp)100499a2dd95SBruce Richardson rte_mempool_avail_count(const struct rte_mempool *mp)
100599a2dd95SBruce Richardson {
100699a2dd95SBruce Richardson unsigned count;
100799a2dd95SBruce Richardson unsigned lcore_id;
100899a2dd95SBruce Richardson
100999a2dd95SBruce Richardson count = rte_mempool_ops_get_count(mp);
101099a2dd95SBruce Richardson
101199a2dd95SBruce Richardson if (mp->cache_size == 0)
101299a2dd95SBruce Richardson return count;
101399a2dd95SBruce Richardson
101499a2dd95SBruce Richardson for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++)
101599a2dd95SBruce Richardson count += mp->local_cache[lcore_id].len;
101699a2dd95SBruce Richardson
101799a2dd95SBruce Richardson /*
101899a2dd95SBruce Richardson * due to race condition (access to len is not locked), the
101999a2dd95SBruce Richardson * total can be greater than size... so fix the result
102099a2dd95SBruce Richardson */
102199a2dd95SBruce Richardson if (count > mp->size)
102299a2dd95SBruce Richardson return mp->size;
102399a2dd95SBruce Richardson return count;
102499a2dd95SBruce Richardson }
102599a2dd95SBruce Richardson
102699a2dd95SBruce Richardson /* return the number of entries allocated from the mempool */
102799a2dd95SBruce Richardson unsigned int
rte_mempool_in_use_count(const struct rte_mempool * mp)102899a2dd95SBruce Richardson rte_mempool_in_use_count(const struct rte_mempool *mp)
102999a2dd95SBruce Richardson {
103099a2dd95SBruce Richardson return mp->size - rte_mempool_avail_count(mp);
103199a2dd95SBruce Richardson }
103299a2dd95SBruce Richardson
103399a2dd95SBruce Richardson /* dump the cache status */
103499a2dd95SBruce Richardson static unsigned
rte_mempool_dump_cache(FILE * f,const struct rte_mempool * mp)103599a2dd95SBruce Richardson rte_mempool_dump_cache(FILE *f, const struct rte_mempool *mp)
103699a2dd95SBruce Richardson {
103799a2dd95SBruce Richardson unsigned lcore_id;
103899a2dd95SBruce Richardson unsigned count = 0;
103999a2dd95SBruce Richardson unsigned cache_count;
104099a2dd95SBruce Richardson
104199a2dd95SBruce Richardson fprintf(f, " internal cache infos:\n");
104299a2dd95SBruce Richardson fprintf(f, " cache_size=%"PRIu32"\n", mp->cache_size);
104399a2dd95SBruce Richardson
104499a2dd95SBruce Richardson if (mp->cache_size == 0)
104599a2dd95SBruce Richardson return count;
104699a2dd95SBruce Richardson
104799a2dd95SBruce Richardson for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
104899a2dd95SBruce Richardson cache_count = mp->local_cache[lcore_id].len;
104999a2dd95SBruce Richardson fprintf(f, " cache_count[%u]=%"PRIu32"\n",
105099a2dd95SBruce Richardson lcore_id, cache_count);
105199a2dd95SBruce Richardson count += cache_count;
105299a2dd95SBruce Richardson }
105399a2dd95SBruce Richardson fprintf(f, " total_cache_count=%u\n", count);
105499a2dd95SBruce Richardson return count;
105599a2dd95SBruce Richardson }
105699a2dd95SBruce Richardson
105799a2dd95SBruce Richardson /* check and update cookies or panic (internal) */
rte_mempool_check_cookies(const struct rte_mempool * mp,void * const * obj_table_const,unsigned n,int free)105899a2dd95SBruce Richardson void rte_mempool_check_cookies(const struct rte_mempool *mp,
105999a2dd95SBruce Richardson void * const *obj_table_const, unsigned n, int free)
106099a2dd95SBruce Richardson {
106199a2dd95SBruce Richardson #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
106299a2dd95SBruce Richardson struct rte_mempool_objhdr *hdr;
106399a2dd95SBruce Richardson struct rte_mempool_objtlr *tlr;
106499a2dd95SBruce Richardson uint64_t cookie;
106599a2dd95SBruce Richardson void *tmp;
106699a2dd95SBruce Richardson void *obj;
106799a2dd95SBruce Richardson void **obj_table;
106899a2dd95SBruce Richardson
106999a2dd95SBruce Richardson /* Force to drop the "const" attribute. This is done only when
107099a2dd95SBruce Richardson * DEBUG is enabled */
107123dc03f6SStephen Hemminger tmp = (void *)(uintptr_t)obj_table_const;
107299a2dd95SBruce Richardson obj_table = tmp;
107399a2dd95SBruce Richardson
107499a2dd95SBruce Richardson while (n--) {
107599a2dd95SBruce Richardson obj = obj_table[n];
107699a2dd95SBruce Richardson
107799a2dd95SBruce Richardson if (rte_mempool_from_obj(obj) != mp)
107899a2dd95SBruce Richardson rte_panic("MEMPOOL: object is owned by another "
107999a2dd95SBruce Richardson "mempool\n");
108099a2dd95SBruce Richardson
1081ad276d5cSAndrew Rybchenko hdr = rte_mempool_get_header(obj);
108299a2dd95SBruce Richardson cookie = hdr->cookie;
108399a2dd95SBruce Richardson
108499a2dd95SBruce Richardson if (free == 0) {
108599a2dd95SBruce Richardson if (cookie != RTE_MEMPOOL_HEADER_COOKIE1) {
1086ae67895bSDavid Marchand RTE_MEMPOOL_LOG(CRIT,
1087ae67895bSDavid Marchand "obj=%p, mempool=%p, cookie=%" PRIx64,
108899a2dd95SBruce Richardson obj, (const void *) mp, cookie);
108999a2dd95SBruce Richardson rte_panic("MEMPOOL: bad header cookie (put)\n");
109099a2dd95SBruce Richardson }
109199a2dd95SBruce Richardson hdr->cookie = RTE_MEMPOOL_HEADER_COOKIE2;
109299a2dd95SBruce Richardson } else if (free == 1) {
109399a2dd95SBruce Richardson if (cookie != RTE_MEMPOOL_HEADER_COOKIE2) {
1094ae67895bSDavid Marchand RTE_MEMPOOL_LOG(CRIT,
1095ae67895bSDavid Marchand "obj=%p, mempool=%p, cookie=%" PRIx64,
109699a2dd95SBruce Richardson obj, (const void *) mp, cookie);
109799a2dd95SBruce Richardson rte_panic("MEMPOOL: bad header cookie (get)\n");
109899a2dd95SBruce Richardson }
109999a2dd95SBruce Richardson hdr->cookie = RTE_MEMPOOL_HEADER_COOKIE1;
110099a2dd95SBruce Richardson } else if (free == 2) {
110199a2dd95SBruce Richardson if (cookie != RTE_MEMPOOL_HEADER_COOKIE1 &&
110299a2dd95SBruce Richardson cookie != RTE_MEMPOOL_HEADER_COOKIE2) {
1103ae67895bSDavid Marchand RTE_MEMPOOL_LOG(CRIT,
1104ae67895bSDavid Marchand "obj=%p, mempool=%p, cookie=%" PRIx64,
110599a2dd95SBruce Richardson obj, (const void *) mp, cookie);
110699a2dd95SBruce Richardson rte_panic("MEMPOOL: bad header cookie (audit)\n");
110799a2dd95SBruce Richardson }
110899a2dd95SBruce Richardson }
1109ad276d5cSAndrew Rybchenko tlr = rte_mempool_get_trailer(obj);
111099a2dd95SBruce Richardson cookie = tlr->cookie;
111199a2dd95SBruce Richardson if (cookie != RTE_MEMPOOL_TRAILER_COOKIE) {
1112ae67895bSDavid Marchand RTE_MEMPOOL_LOG(CRIT,
1113ae67895bSDavid Marchand "obj=%p, mempool=%p, cookie=%" PRIx64,
111499a2dd95SBruce Richardson obj, (const void *) mp, cookie);
111599a2dd95SBruce Richardson rte_panic("MEMPOOL: bad trailer cookie\n");
111699a2dd95SBruce Richardson }
111799a2dd95SBruce Richardson }
111899a2dd95SBruce Richardson #else
111999a2dd95SBruce Richardson RTE_SET_USED(mp);
112099a2dd95SBruce Richardson RTE_SET_USED(obj_table_const);
112199a2dd95SBruce Richardson RTE_SET_USED(n);
112299a2dd95SBruce Richardson RTE_SET_USED(free);
112399a2dd95SBruce Richardson #endif
112499a2dd95SBruce Richardson }
112599a2dd95SBruce Richardson
112699a2dd95SBruce Richardson void
rte_mempool_contig_blocks_check_cookies(const struct rte_mempool * mp,void * const * first_obj_table_const,unsigned int n,int free)112799a2dd95SBruce Richardson rte_mempool_contig_blocks_check_cookies(const struct rte_mempool *mp,
112899a2dd95SBruce Richardson void * const *first_obj_table_const, unsigned int n, int free)
112999a2dd95SBruce Richardson {
113099a2dd95SBruce Richardson #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
113199a2dd95SBruce Richardson struct rte_mempool_info info;
113299a2dd95SBruce Richardson const size_t total_elt_sz =
113399a2dd95SBruce Richardson mp->header_size + mp->elt_size + mp->trailer_size;
113499a2dd95SBruce Richardson unsigned int i, j;
113599a2dd95SBruce Richardson
113699a2dd95SBruce Richardson rte_mempool_ops_get_info(mp, &info);
113799a2dd95SBruce Richardson
113899a2dd95SBruce Richardson for (i = 0; i < n; ++i) {
113999a2dd95SBruce Richardson void *first_obj = first_obj_table_const[i];
114099a2dd95SBruce Richardson
114199a2dd95SBruce Richardson for (j = 0; j < info.contig_block_size; ++j) {
114299a2dd95SBruce Richardson void *obj;
114399a2dd95SBruce Richardson
114499a2dd95SBruce Richardson obj = (void *)((uintptr_t)first_obj + j * total_elt_sz);
114599a2dd95SBruce Richardson rte_mempool_check_cookies(mp, &obj, 1, free);
114699a2dd95SBruce Richardson }
114799a2dd95SBruce Richardson }
114899a2dd95SBruce Richardson #else
114999a2dd95SBruce Richardson RTE_SET_USED(mp);
115099a2dd95SBruce Richardson RTE_SET_USED(first_obj_table_const);
115199a2dd95SBruce Richardson RTE_SET_USED(n);
115299a2dd95SBruce Richardson RTE_SET_USED(free);
115399a2dd95SBruce Richardson #endif
115499a2dd95SBruce Richardson }
115599a2dd95SBruce Richardson
115699a2dd95SBruce Richardson #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
115799a2dd95SBruce Richardson static void
mempool_obj_audit(struct rte_mempool * mp,__rte_unused void * opaque,void * obj,__rte_unused unsigned idx)115899a2dd95SBruce Richardson mempool_obj_audit(struct rte_mempool *mp, __rte_unused void *opaque,
115999a2dd95SBruce Richardson void *obj, __rte_unused unsigned idx)
116099a2dd95SBruce Richardson {
1161ad276d5cSAndrew Rybchenko RTE_MEMPOOL_CHECK_COOKIES(mp, &obj, 1, 2);
116299a2dd95SBruce Richardson }
116399a2dd95SBruce Richardson
116499a2dd95SBruce Richardson static void
mempool_audit_cookies(struct rte_mempool * mp)116599a2dd95SBruce Richardson mempool_audit_cookies(struct rte_mempool *mp)
116699a2dd95SBruce Richardson {
116799a2dd95SBruce Richardson unsigned num;
116899a2dd95SBruce Richardson
116999a2dd95SBruce Richardson num = rte_mempool_obj_iter(mp, mempool_obj_audit, NULL);
117099a2dd95SBruce Richardson if (num != mp->size) {
117199a2dd95SBruce Richardson rte_panic("rte_mempool_obj_iter(mempool=%p, size=%u) "
117299a2dd95SBruce Richardson "iterated only over %u elements\n",
117399a2dd95SBruce Richardson mp, mp->size, num);
117499a2dd95SBruce Richardson }
117599a2dd95SBruce Richardson }
117699a2dd95SBruce Richardson #else
117799a2dd95SBruce Richardson #define mempool_audit_cookies(mp) do {} while(0)
117899a2dd95SBruce Richardson #endif
117999a2dd95SBruce Richardson
118099a2dd95SBruce Richardson /* check cookies before and after objects */
118199a2dd95SBruce Richardson static void
mempool_audit_cache(const struct rte_mempool * mp)118299a2dd95SBruce Richardson mempool_audit_cache(const struct rte_mempool *mp)
118399a2dd95SBruce Richardson {
118499a2dd95SBruce Richardson /* check cache size consistency */
118599a2dd95SBruce Richardson unsigned lcore_id;
118699a2dd95SBruce Richardson
118799a2dd95SBruce Richardson if (mp->cache_size == 0)
118899a2dd95SBruce Richardson return;
118999a2dd95SBruce Richardson
119099a2dd95SBruce Richardson for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
119199a2dd95SBruce Richardson const struct rte_mempool_cache *cache;
119299a2dd95SBruce Richardson cache = &mp->local_cache[lcore_id];
119399a2dd95SBruce Richardson if (cache->len > RTE_DIM(cache->objs)) {
1194ae67895bSDavid Marchand RTE_MEMPOOL_LOG(CRIT, "badness on cache[%u]",
119599a2dd95SBruce Richardson lcore_id);
119699a2dd95SBruce Richardson rte_panic("MEMPOOL: invalid cache len\n");
119799a2dd95SBruce Richardson }
119899a2dd95SBruce Richardson }
119999a2dd95SBruce Richardson }
120099a2dd95SBruce Richardson
120199a2dd95SBruce Richardson /* check the consistency of mempool (size, cookies, ...) */
120299a2dd95SBruce Richardson void
rte_mempool_audit(struct rte_mempool * mp)120399a2dd95SBruce Richardson rte_mempool_audit(struct rte_mempool *mp)
120499a2dd95SBruce Richardson {
120599a2dd95SBruce Richardson mempool_audit_cache(mp);
120699a2dd95SBruce Richardson mempool_audit_cookies(mp);
120799a2dd95SBruce Richardson
120899a2dd95SBruce Richardson /* For case where mempool DEBUG is not set, and cache size is 0 */
120999a2dd95SBruce Richardson RTE_SET_USED(mp);
121099a2dd95SBruce Richardson }
121199a2dd95SBruce Richardson
121299a2dd95SBruce Richardson /* dump the status of the mempool on the console */
121399a2dd95SBruce Richardson void
rte_mempool_dump(FILE * f,struct rte_mempool * mp)121499a2dd95SBruce Richardson rte_mempool_dump(FILE *f, struct rte_mempool *mp)
121599a2dd95SBruce Richardson {
12169d87e05dSMorten Brørup #ifdef RTE_LIBRTE_MEMPOOL_STATS
121799a2dd95SBruce Richardson struct rte_mempool_info info;
121899a2dd95SBruce Richardson struct rte_mempool_debug_stats sum;
121999a2dd95SBruce Richardson unsigned lcore_id;
122099a2dd95SBruce Richardson #endif
122199a2dd95SBruce Richardson struct rte_mempool_memhdr *memhdr;
122299a2dd95SBruce Richardson struct rte_mempool_ops *ops;
122399a2dd95SBruce Richardson unsigned common_count;
122499a2dd95SBruce Richardson unsigned cache_count;
122599a2dd95SBruce Richardson size_t mem_len = 0;
122699a2dd95SBruce Richardson
122799a2dd95SBruce Richardson RTE_ASSERT(f != NULL);
122899a2dd95SBruce Richardson RTE_ASSERT(mp != NULL);
122999a2dd95SBruce Richardson
123099a2dd95SBruce Richardson fprintf(f, "mempool <%s>@%p\n", mp->name, mp);
123199a2dd95SBruce Richardson fprintf(f, " flags=%x\n", mp->flags);
123299a2dd95SBruce Richardson fprintf(f, " socket_id=%d\n", mp->socket_id);
123399a2dd95SBruce Richardson fprintf(f, " pool=%p\n", mp->pool_data);
123499a2dd95SBruce Richardson fprintf(f, " iova=0x%" PRIx64 "\n", mp->mz->iova);
123599a2dd95SBruce Richardson fprintf(f, " nb_mem_chunks=%u\n", mp->nb_mem_chunks);
123699a2dd95SBruce Richardson fprintf(f, " size=%"PRIu32"\n", mp->size);
123799a2dd95SBruce Richardson fprintf(f, " populated_size=%"PRIu32"\n", mp->populated_size);
123899a2dd95SBruce Richardson fprintf(f, " header_size=%"PRIu32"\n", mp->header_size);
123999a2dd95SBruce Richardson fprintf(f, " elt_size=%"PRIu32"\n", mp->elt_size);
124099a2dd95SBruce Richardson fprintf(f, " trailer_size=%"PRIu32"\n", mp->trailer_size);
124199a2dd95SBruce Richardson fprintf(f, " total_obj_size=%"PRIu32"\n",
124299a2dd95SBruce Richardson mp->header_size + mp->elt_size + mp->trailer_size);
124399a2dd95SBruce Richardson
124499a2dd95SBruce Richardson fprintf(f, " private_data_size=%"PRIu32"\n", mp->private_data_size);
124599a2dd95SBruce Richardson
124699a2dd95SBruce Richardson fprintf(f, " ops_index=%d\n", mp->ops_index);
124799a2dd95SBruce Richardson ops = rte_mempool_get_ops(mp->ops_index);
124899a2dd95SBruce Richardson fprintf(f, " ops_name: <%s>\n", (ops != NULL) ? ops->name : "NA");
124999a2dd95SBruce Richardson
1250208ad363SMorten Brørup STAILQ_FOREACH(memhdr, &mp->mem_list, next) {
1251208ad363SMorten Brørup fprintf(f, " memory chunk at %p, addr=%p, iova=0x%" PRIx64 ", len=%zu\n",
1252208ad363SMorten Brørup memhdr, memhdr->addr, memhdr->iova, memhdr->len);
125399a2dd95SBruce Richardson mem_len += memhdr->len;
1254208ad363SMorten Brørup }
125599a2dd95SBruce Richardson if (mem_len != 0) {
125699a2dd95SBruce Richardson fprintf(f, " avg bytes/object=%#Lf\n",
125799a2dd95SBruce Richardson (long double)mem_len / mp->size);
125899a2dd95SBruce Richardson }
125999a2dd95SBruce Richardson
126099a2dd95SBruce Richardson cache_count = rte_mempool_dump_cache(f, mp);
126199a2dd95SBruce Richardson common_count = rte_mempool_ops_get_count(mp);
126299a2dd95SBruce Richardson if ((cache_count + common_count) > mp->size)
126399a2dd95SBruce Richardson common_count = mp->size - cache_count;
126499a2dd95SBruce Richardson fprintf(f, " common_pool_count=%u\n", common_count);
126599a2dd95SBruce Richardson
126699a2dd95SBruce Richardson /* sum and dump statistics */
12679d87e05dSMorten Brørup #ifdef RTE_LIBRTE_MEMPOOL_STATS
126899a2dd95SBruce Richardson rte_mempool_ops_get_info(mp, &info);
126999a2dd95SBruce Richardson memset(&sum, 0, sizeof(sum));
127017749e4dSMorten Brørup for (lcore_id = 0; lcore_id < RTE_MAX_LCORE + 1; lcore_id++) {
127199a2dd95SBruce Richardson sum.put_bulk += mp->stats[lcore_id].put_bulk;
127299a2dd95SBruce Richardson sum.put_objs += mp->stats[lcore_id].put_objs;
1273cee151b4SJoyce Kong sum.put_common_pool_bulk += mp->stats[lcore_id].put_common_pool_bulk;
1274cee151b4SJoyce Kong sum.put_common_pool_objs += mp->stats[lcore_id].put_common_pool_objs;
1275cee151b4SJoyce Kong sum.get_common_pool_bulk += mp->stats[lcore_id].get_common_pool_bulk;
1276cee151b4SJoyce Kong sum.get_common_pool_objs += mp->stats[lcore_id].get_common_pool_objs;
127799a2dd95SBruce Richardson sum.get_success_bulk += mp->stats[lcore_id].get_success_bulk;
127899a2dd95SBruce Richardson sum.get_success_objs += mp->stats[lcore_id].get_success_objs;
127999a2dd95SBruce Richardson sum.get_fail_bulk += mp->stats[lcore_id].get_fail_bulk;
128099a2dd95SBruce Richardson sum.get_fail_objs += mp->stats[lcore_id].get_fail_objs;
128199a2dd95SBruce Richardson sum.get_success_blks += mp->stats[lcore_id].get_success_blks;
128299a2dd95SBruce Richardson sum.get_fail_blks += mp->stats[lcore_id].get_fail_blks;
128399a2dd95SBruce Richardson }
1284203dcc9cSMorten Brørup if (mp->cache_size != 0) {
1285203dcc9cSMorten Brørup /* Add the statistics stored in the mempool caches. */
1286203dcc9cSMorten Brørup for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1287203dcc9cSMorten Brørup sum.put_bulk += mp->local_cache[lcore_id].stats.put_bulk;
1288203dcc9cSMorten Brørup sum.put_objs += mp->local_cache[lcore_id].stats.put_objs;
1289203dcc9cSMorten Brørup sum.get_success_bulk += mp->local_cache[lcore_id].stats.get_success_bulk;
1290203dcc9cSMorten Brørup sum.get_success_objs += mp->local_cache[lcore_id].stats.get_success_objs;
1291203dcc9cSMorten Brørup }
1292203dcc9cSMorten Brørup }
129399a2dd95SBruce Richardson fprintf(f, " stats:\n");
129499a2dd95SBruce Richardson fprintf(f, " put_bulk=%"PRIu64"\n", sum.put_bulk);
129599a2dd95SBruce Richardson fprintf(f, " put_objs=%"PRIu64"\n", sum.put_objs);
1296cee151b4SJoyce Kong fprintf(f, " put_common_pool_bulk=%"PRIu64"\n", sum.put_common_pool_bulk);
1297cee151b4SJoyce Kong fprintf(f, " put_common_pool_objs=%"PRIu64"\n", sum.put_common_pool_objs);
1298cee151b4SJoyce Kong fprintf(f, " get_common_pool_bulk=%"PRIu64"\n", sum.get_common_pool_bulk);
1299cee151b4SJoyce Kong fprintf(f, " get_common_pool_objs=%"PRIu64"\n", sum.get_common_pool_objs);
130099a2dd95SBruce Richardson fprintf(f, " get_success_bulk=%"PRIu64"\n", sum.get_success_bulk);
130199a2dd95SBruce Richardson fprintf(f, " get_success_objs=%"PRIu64"\n", sum.get_success_objs);
130299a2dd95SBruce Richardson fprintf(f, " get_fail_bulk=%"PRIu64"\n", sum.get_fail_bulk);
130399a2dd95SBruce Richardson fprintf(f, " get_fail_objs=%"PRIu64"\n", sum.get_fail_objs);
130499a2dd95SBruce Richardson if (info.contig_block_size > 0) {
130599a2dd95SBruce Richardson fprintf(f, " get_success_blks=%"PRIu64"\n",
130699a2dd95SBruce Richardson sum.get_success_blks);
130799a2dd95SBruce Richardson fprintf(f, " get_fail_blks=%"PRIu64"\n", sum.get_fail_blks);
130899a2dd95SBruce Richardson }
130999a2dd95SBruce Richardson #else
131099a2dd95SBruce Richardson fprintf(f, " no statistics available\n");
131199a2dd95SBruce Richardson #endif
131299a2dd95SBruce Richardson
131399a2dd95SBruce Richardson rte_mempool_audit(mp);
131499a2dd95SBruce Richardson }
131599a2dd95SBruce Richardson
131699a2dd95SBruce Richardson /* dump the status of all mempools on the console */
131799a2dd95SBruce Richardson void
rte_mempool_list_dump(FILE * f)131899a2dd95SBruce Richardson rte_mempool_list_dump(FILE *f)
131999a2dd95SBruce Richardson {
132099a2dd95SBruce Richardson struct rte_mempool *mp = NULL;
132199a2dd95SBruce Richardson struct rte_tailq_entry *te;
132299a2dd95SBruce Richardson struct rte_mempool_list *mempool_list;
132399a2dd95SBruce Richardson
132499a2dd95SBruce Richardson mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
132599a2dd95SBruce Richardson
132699a2dd95SBruce Richardson rte_mcfg_mempool_read_lock();
132799a2dd95SBruce Richardson
132899a2dd95SBruce Richardson TAILQ_FOREACH(te, mempool_list, next) {
132999a2dd95SBruce Richardson mp = (struct rte_mempool *) te->data;
133099a2dd95SBruce Richardson rte_mempool_dump(f, mp);
133199a2dd95SBruce Richardson }
133299a2dd95SBruce Richardson
133399a2dd95SBruce Richardson rte_mcfg_mempool_read_unlock();
133499a2dd95SBruce Richardson }
133599a2dd95SBruce Richardson
133699a2dd95SBruce Richardson /* search a mempool from its name */
133799a2dd95SBruce Richardson struct rte_mempool *
rte_mempool_lookup(const char * name)133899a2dd95SBruce Richardson rte_mempool_lookup(const char *name)
133999a2dd95SBruce Richardson {
134099a2dd95SBruce Richardson struct rte_mempool *mp = NULL;
134199a2dd95SBruce Richardson struct rte_tailq_entry *te;
134299a2dd95SBruce Richardson struct rte_mempool_list *mempool_list;
134399a2dd95SBruce Richardson
134499a2dd95SBruce Richardson mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
134599a2dd95SBruce Richardson
134699a2dd95SBruce Richardson rte_mcfg_mempool_read_lock();
134799a2dd95SBruce Richardson
134899a2dd95SBruce Richardson TAILQ_FOREACH(te, mempool_list, next) {
134999a2dd95SBruce Richardson mp = (struct rte_mempool *) te->data;
135099a2dd95SBruce Richardson if (strncmp(name, mp->name, RTE_MEMPOOL_NAMESIZE) == 0)
135199a2dd95SBruce Richardson break;
135299a2dd95SBruce Richardson }
135399a2dd95SBruce Richardson
135499a2dd95SBruce Richardson rte_mcfg_mempool_read_unlock();
135599a2dd95SBruce Richardson
135699a2dd95SBruce Richardson if (te == NULL) {
135799a2dd95SBruce Richardson rte_errno = ENOENT;
135899a2dd95SBruce Richardson return NULL;
135999a2dd95SBruce Richardson }
136099a2dd95SBruce Richardson
136199a2dd95SBruce Richardson return mp;
136299a2dd95SBruce Richardson }
136399a2dd95SBruce Richardson
rte_mempool_walk(void (* func)(struct rte_mempool *,void *),void * arg)136499a2dd95SBruce Richardson void rte_mempool_walk(void (*func)(struct rte_mempool *, void *),
136599a2dd95SBruce Richardson void *arg)
136699a2dd95SBruce Richardson {
136799a2dd95SBruce Richardson struct rte_tailq_entry *te = NULL;
136899a2dd95SBruce Richardson struct rte_mempool_list *mempool_list;
136999a2dd95SBruce Richardson void *tmp_te;
137099a2dd95SBruce Richardson
137199a2dd95SBruce Richardson mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
137299a2dd95SBruce Richardson
137399a2dd95SBruce Richardson rte_mcfg_mempool_read_lock();
137499a2dd95SBruce Richardson
1375f1f6ebc0SWilliam Tu RTE_TAILQ_FOREACH_SAFE(te, mempool_list, next, tmp_te) {
137699a2dd95SBruce Richardson (*func)((struct rte_mempool *) te->data, arg);
137799a2dd95SBruce Richardson }
137899a2dd95SBruce Richardson
137999a2dd95SBruce Richardson rte_mcfg_mempool_read_unlock();
138099a2dd95SBruce Richardson }
1381da2b9cb2SDmitry Kozlyuk
rte_mempool_get_mem_range(const struct rte_mempool * mp,struct rte_mempool_mem_range_info * mem_range)13822f1015d8SPaul Szczepanek int rte_mempool_get_mem_range(const struct rte_mempool *mp,
13832f1015d8SPaul Szczepanek struct rte_mempool_mem_range_info *mem_range)
13842f1015d8SPaul Szczepanek {
13852f1015d8SPaul Szczepanek void *address_low = (void *)UINTPTR_MAX;
13862f1015d8SPaul Szczepanek void *address_high = 0;
13872f1015d8SPaul Szczepanek size_t address_diff = 0;
13882f1015d8SPaul Szczepanek size_t total_size = 0;
13892f1015d8SPaul Szczepanek struct rte_mempool_memhdr *hdr;
13902f1015d8SPaul Szczepanek
13912f1015d8SPaul Szczepanek if (mp == NULL || mem_range == NULL)
13922f1015d8SPaul Szczepanek return -EINVAL;
13932f1015d8SPaul Szczepanek
13942f1015d8SPaul Szczepanek /* go through memory chunks and find the lowest and highest addresses */
13952f1015d8SPaul Szczepanek STAILQ_FOREACH(hdr, &mp->mem_list, next) {
13962f1015d8SPaul Szczepanek if (address_low > hdr->addr)
13972f1015d8SPaul Szczepanek address_low = hdr->addr;
13982f1015d8SPaul Szczepanek if (address_high < RTE_PTR_ADD(hdr->addr, hdr->len))
13992f1015d8SPaul Szczepanek address_high = RTE_PTR_ADD(hdr->addr, hdr->len);
14002f1015d8SPaul Szczepanek total_size += hdr->len;
14012f1015d8SPaul Szczepanek }
14022f1015d8SPaul Szczepanek
14032f1015d8SPaul Szczepanek /* check if mempool was not populated yet (no memory chunks) */
14042f1015d8SPaul Szczepanek if (address_low == (void *)UINTPTR_MAX)
14052f1015d8SPaul Szczepanek return -EINVAL;
14062f1015d8SPaul Szczepanek
14072f1015d8SPaul Szczepanek address_diff = (size_t)RTE_PTR_DIFF(address_high, address_low);
14082f1015d8SPaul Szczepanek
14092f1015d8SPaul Szczepanek mem_range->start = address_low;
14102f1015d8SPaul Szczepanek mem_range->length = address_diff;
14112f1015d8SPaul Szczepanek mem_range->is_contiguous = (total_size == address_diff) ? true : false;
14122f1015d8SPaul Szczepanek
14132f1015d8SPaul Szczepanek return 0;
14142f1015d8SPaul Szczepanek }
14152f1015d8SPaul Szczepanek
rte_mempool_get_obj_alignment(const struct rte_mempool * mp)14162f1015d8SPaul Szczepanek size_t rte_mempool_get_obj_alignment(const struct rte_mempool *mp)
14172f1015d8SPaul Szczepanek {
14182f1015d8SPaul Szczepanek if (mp == NULL)
14192f1015d8SPaul Szczepanek return 0;
14202f1015d8SPaul Szczepanek
14212f1015d8SPaul Szczepanek if (mp->flags & RTE_MEMPOOL_F_NO_CACHE_ALIGN)
14222f1015d8SPaul Szczepanek return sizeof(uint64_t);
14232f1015d8SPaul Szczepanek else
14242f1015d8SPaul Szczepanek return RTE_MEMPOOL_ALIGN;
14252f1015d8SPaul Szczepanek }
14262f1015d8SPaul Szczepanek
1427da2b9cb2SDmitry Kozlyuk struct mempool_callback_data {
142803b3cdf9SDmitry Kozlyuk TAILQ_ENTRY(mempool_callback_data) callbacks;
1429da2b9cb2SDmitry Kozlyuk rte_mempool_event_callback *func;
1430da2b9cb2SDmitry Kozlyuk void *user_data;
1431da2b9cb2SDmitry Kozlyuk };
1432da2b9cb2SDmitry Kozlyuk
1433da2b9cb2SDmitry Kozlyuk static void
mempool_event_callback_invoke(enum rte_mempool_event event,struct rte_mempool * mp)1434da2b9cb2SDmitry Kozlyuk mempool_event_callback_invoke(enum rte_mempool_event event,
1435da2b9cb2SDmitry Kozlyuk struct rte_mempool *mp)
1436da2b9cb2SDmitry Kozlyuk {
143703b3cdf9SDmitry Kozlyuk struct mempool_callback_data *cb;
1438da2b9cb2SDmitry Kozlyuk void *tmp_te;
1439da2b9cb2SDmitry Kozlyuk
1440da2b9cb2SDmitry Kozlyuk rte_mcfg_tailq_read_lock();
144103b3cdf9SDmitry Kozlyuk RTE_TAILQ_FOREACH_SAFE(cb, &callback_tailq, callbacks, tmp_te) {
1442da2b9cb2SDmitry Kozlyuk rte_mcfg_tailq_read_unlock();
1443da2b9cb2SDmitry Kozlyuk cb->func(event, mp, cb->user_data);
1444da2b9cb2SDmitry Kozlyuk rte_mcfg_tailq_read_lock();
1445da2b9cb2SDmitry Kozlyuk }
1446da2b9cb2SDmitry Kozlyuk rte_mcfg_tailq_read_unlock();
1447da2b9cb2SDmitry Kozlyuk }
1448da2b9cb2SDmitry Kozlyuk
1449da2b9cb2SDmitry Kozlyuk int
rte_mempool_event_callback_register(rte_mempool_event_callback * func,void * user_data)1450da2b9cb2SDmitry Kozlyuk rte_mempool_event_callback_register(rte_mempool_event_callback *func,
1451da2b9cb2SDmitry Kozlyuk void *user_data)
1452da2b9cb2SDmitry Kozlyuk {
1453da2b9cb2SDmitry Kozlyuk struct mempool_callback_data *cb;
1454da2b9cb2SDmitry Kozlyuk int ret;
1455da2b9cb2SDmitry Kozlyuk
1456da2b9cb2SDmitry Kozlyuk if (func == NULL) {
1457da2b9cb2SDmitry Kozlyuk rte_errno = EINVAL;
1458da2b9cb2SDmitry Kozlyuk return -rte_errno;
1459da2b9cb2SDmitry Kozlyuk }
1460da2b9cb2SDmitry Kozlyuk
1461da2b9cb2SDmitry Kozlyuk rte_mcfg_tailq_write_lock();
146203b3cdf9SDmitry Kozlyuk TAILQ_FOREACH(cb, &callback_tailq, callbacks) {
1463da2b9cb2SDmitry Kozlyuk if (cb->func == func && cb->user_data == user_data) {
1464da2b9cb2SDmitry Kozlyuk ret = -EEXIST;
1465da2b9cb2SDmitry Kozlyuk goto exit;
1466da2b9cb2SDmitry Kozlyuk }
1467da2b9cb2SDmitry Kozlyuk }
1468da2b9cb2SDmitry Kozlyuk
146903b3cdf9SDmitry Kozlyuk cb = calloc(1, sizeof(*cb));
1470da2b9cb2SDmitry Kozlyuk if (cb == NULL) {
1471ae67895bSDavid Marchand RTE_MEMPOOL_LOG(ERR, "Cannot allocate event callback!");
1472da2b9cb2SDmitry Kozlyuk ret = -ENOMEM;
1473da2b9cb2SDmitry Kozlyuk goto exit;
1474da2b9cb2SDmitry Kozlyuk }
1475da2b9cb2SDmitry Kozlyuk
1476da2b9cb2SDmitry Kozlyuk cb->func = func;
1477da2b9cb2SDmitry Kozlyuk cb->user_data = user_data;
147803b3cdf9SDmitry Kozlyuk TAILQ_INSERT_TAIL(&callback_tailq, cb, callbacks);
1479da2b9cb2SDmitry Kozlyuk ret = 0;
1480da2b9cb2SDmitry Kozlyuk
1481da2b9cb2SDmitry Kozlyuk exit:
1482da2b9cb2SDmitry Kozlyuk rte_mcfg_tailq_write_unlock();
1483da2b9cb2SDmitry Kozlyuk rte_errno = -ret;
1484da2b9cb2SDmitry Kozlyuk return ret;
1485da2b9cb2SDmitry Kozlyuk }
1486da2b9cb2SDmitry Kozlyuk
1487da2b9cb2SDmitry Kozlyuk int
rte_mempool_event_callback_unregister(rte_mempool_event_callback * func,void * user_data)1488da2b9cb2SDmitry Kozlyuk rte_mempool_event_callback_unregister(rte_mempool_event_callback *func,
1489da2b9cb2SDmitry Kozlyuk void *user_data)
1490da2b9cb2SDmitry Kozlyuk {
1491da2b9cb2SDmitry Kozlyuk struct mempool_callback_data *cb;
1492da2b9cb2SDmitry Kozlyuk int ret = -ENOENT;
1493da2b9cb2SDmitry Kozlyuk
1494da2b9cb2SDmitry Kozlyuk rte_mcfg_tailq_write_lock();
149503b3cdf9SDmitry Kozlyuk TAILQ_FOREACH(cb, &callback_tailq, callbacks) {
1496da2b9cb2SDmitry Kozlyuk if (cb->func == func && cb->user_data == user_data) {
149703b3cdf9SDmitry Kozlyuk TAILQ_REMOVE(&callback_tailq, cb, callbacks);
1498da2b9cb2SDmitry Kozlyuk ret = 0;
1499da2b9cb2SDmitry Kozlyuk break;
1500da2b9cb2SDmitry Kozlyuk }
1501da2b9cb2SDmitry Kozlyuk }
1502da2b9cb2SDmitry Kozlyuk rte_mcfg_tailq_write_unlock();
1503da2b9cb2SDmitry Kozlyuk
150403b3cdf9SDmitry Kozlyuk if (ret == 0)
150503b3cdf9SDmitry Kozlyuk free(cb);
1506da2b9cb2SDmitry Kozlyuk rte_errno = -ret;
1507da2b9cb2SDmitry Kozlyuk return ret;
1508da2b9cb2SDmitry Kozlyuk }
15092f5c4025SGowrishankar Muthukrishnan
15102f5c4025SGowrishankar Muthukrishnan static void
mempool_list_cb(struct rte_mempool * mp,void * arg)15112f5c4025SGowrishankar Muthukrishnan mempool_list_cb(struct rte_mempool *mp, void *arg)
15122f5c4025SGowrishankar Muthukrishnan {
15132f5c4025SGowrishankar Muthukrishnan struct rte_tel_data *d = (struct rte_tel_data *)arg;
15142f5c4025SGowrishankar Muthukrishnan
15152f5c4025SGowrishankar Muthukrishnan rte_tel_data_add_array_string(d, mp->name);
15162f5c4025SGowrishankar Muthukrishnan }
15172f5c4025SGowrishankar Muthukrishnan
15182f5c4025SGowrishankar Muthukrishnan static int
mempool_handle_list(const char * cmd __rte_unused,const char * params __rte_unused,struct rte_tel_data * d)15192f5c4025SGowrishankar Muthukrishnan mempool_handle_list(const char *cmd __rte_unused,
15202f5c4025SGowrishankar Muthukrishnan const char *params __rte_unused, struct rte_tel_data *d)
15212f5c4025SGowrishankar Muthukrishnan {
15222f5c4025SGowrishankar Muthukrishnan rte_tel_data_start_array(d, RTE_TEL_STRING_VAL);
15232f5c4025SGowrishankar Muthukrishnan rte_mempool_walk(mempool_list_cb, d);
15242f5c4025SGowrishankar Muthukrishnan return 0;
15252f5c4025SGowrishankar Muthukrishnan }
15262f5c4025SGowrishankar Muthukrishnan
15272f5c4025SGowrishankar Muthukrishnan struct mempool_info_cb_arg {
15282f5c4025SGowrishankar Muthukrishnan char *pool_name;
15292f5c4025SGowrishankar Muthukrishnan struct rte_tel_data *d;
15302f5c4025SGowrishankar Muthukrishnan };
15312f5c4025SGowrishankar Muthukrishnan
15322f5c4025SGowrishankar Muthukrishnan static void
mempool_info_cb(struct rte_mempool * mp,void * arg)15332f5c4025SGowrishankar Muthukrishnan mempool_info_cb(struct rte_mempool *mp, void *arg)
15342f5c4025SGowrishankar Muthukrishnan {
15352f5c4025SGowrishankar Muthukrishnan struct mempool_info_cb_arg *info = (struct mempool_info_cb_arg *)arg;
15362f5c4025SGowrishankar Muthukrishnan const struct rte_memzone *mz;
1537ab8a1cc4SRobin Jarry uint64_t cache_count, common_count;
15382f5c4025SGowrishankar Muthukrishnan
15392f5c4025SGowrishankar Muthukrishnan if (strncmp(mp->name, info->pool_name, RTE_MEMZONE_NAMESIZE))
15402f5c4025SGowrishankar Muthukrishnan return;
15412f5c4025SGowrishankar Muthukrishnan
15422f5c4025SGowrishankar Muthukrishnan rte_tel_data_add_dict_string(info->d, "name", mp->name);
1543af0785a2SBruce Richardson rte_tel_data_add_dict_uint(info->d, "pool_id", mp->pool_id);
1544af0785a2SBruce Richardson rte_tel_data_add_dict_uint(info->d, "flags", mp->flags);
15452f5c4025SGowrishankar Muthukrishnan rte_tel_data_add_dict_int(info->d, "socket_id", mp->socket_id);
1546af0785a2SBruce Richardson rte_tel_data_add_dict_uint(info->d, "size", mp->size);
1547af0785a2SBruce Richardson rte_tel_data_add_dict_uint(info->d, "cache_size", mp->cache_size);
1548af0785a2SBruce Richardson rte_tel_data_add_dict_uint(info->d, "elt_size", mp->elt_size);
1549af0785a2SBruce Richardson rte_tel_data_add_dict_uint(info->d, "header_size", mp->header_size);
1550af0785a2SBruce Richardson rte_tel_data_add_dict_uint(info->d, "trailer_size", mp->trailer_size);
1551af0785a2SBruce Richardson rte_tel_data_add_dict_uint(info->d, "private_data_size",
15522f5c4025SGowrishankar Muthukrishnan mp->private_data_size);
15532f5c4025SGowrishankar Muthukrishnan rte_tel_data_add_dict_int(info->d, "ops_index", mp->ops_index);
1554af0785a2SBruce Richardson rte_tel_data_add_dict_uint(info->d, "populated_size",
15552f5c4025SGowrishankar Muthukrishnan mp->populated_size);
15562f5c4025SGowrishankar Muthukrishnan
1557ab8a1cc4SRobin Jarry cache_count = 0;
1558ab8a1cc4SRobin Jarry if (mp->cache_size > 0) {
1559ab8a1cc4SRobin Jarry int lcore_id;
1560ab8a1cc4SRobin Jarry for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++)
1561ab8a1cc4SRobin Jarry cache_count += mp->local_cache[lcore_id].len;
1562ab8a1cc4SRobin Jarry }
1563ab8a1cc4SRobin Jarry rte_tel_data_add_dict_uint(info->d, "total_cache_count", cache_count);
1564ab8a1cc4SRobin Jarry common_count = rte_mempool_ops_get_count(mp);
1565ab8a1cc4SRobin Jarry if ((cache_count + common_count) > mp->size)
1566ab8a1cc4SRobin Jarry common_count = mp->size - cache_count;
1567ab8a1cc4SRobin Jarry rte_tel_data_add_dict_uint(info->d, "common_pool_count", common_count);
1568ab8a1cc4SRobin Jarry
15692f5c4025SGowrishankar Muthukrishnan mz = mp->mz;
15702f5c4025SGowrishankar Muthukrishnan rte_tel_data_add_dict_string(info->d, "mz_name", mz->name);
1571af0785a2SBruce Richardson rte_tel_data_add_dict_uint(info->d, "mz_len", mz->len);
1572af0785a2SBruce Richardson rte_tel_data_add_dict_uint(info->d, "mz_hugepage_sz",
15732f5c4025SGowrishankar Muthukrishnan mz->hugepage_sz);
15742f5c4025SGowrishankar Muthukrishnan rte_tel_data_add_dict_int(info->d, "mz_socket_id", mz->socket_id);
1575af0785a2SBruce Richardson rte_tel_data_add_dict_uint(info->d, "mz_flags", mz->flags);
15762f5c4025SGowrishankar Muthukrishnan }
15772f5c4025SGowrishankar Muthukrishnan
15782f5c4025SGowrishankar Muthukrishnan static int
mempool_handle_info(const char * cmd __rte_unused,const char * params,struct rte_tel_data * d)15792f5c4025SGowrishankar Muthukrishnan mempool_handle_info(const char *cmd __rte_unused, const char *params,
15802f5c4025SGowrishankar Muthukrishnan struct rte_tel_data *d)
15812f5c4025SGowrishankar Muthukrishnan {
15822f5c4025SGowrishankar Muthukrishnan struct mempool_info_cb_arg mp_arg;
15832f5c4025SGowrishankar Muthukrishnan char name[RTE_MEMZONE_NAMESIZE];
15842f5c4025SGowrishankar Muthukrishnan
15852f5c4025SGowrishankar Muthukrishnan if (!params || strlen(params) == 0)
15862f5c4025SGowrishankar Muthukrishnan return -EINVAL;
15872f5c4025SGowrishankar Muthukrishnan
15882f5c4025SGowrishankar Muthukrishnan rte_strlcpy(name, params, RTE_MEMZONE_NAMESIZE);
15892f5c4025SGowrishankar Muthukrishnan
15902f5c4025SGowrishankar Muthukrishnan rte_tel_data_start_dict(d);
15912f5c4025SGowrishankar Muthukrishnan mp_arg.pool_name = name;
15922f5c4025SGowrishankar Muthukrishnan mp_arg.d = d;
15932f5c4025SGowrishankar Muthukrishnan rte_mempool_walk(mempool_info_cb, &mp_arg);
15942f5c4025SGowrishankar Muthukrishnan
15952f5c4025SGowrishankar Muthukrishnan return 0;
15962f5c4025SGowrishankar Muthukrishnan }
15972f5c4025SGowrishankar Muthukrishnan
RTE_INIT(mempool_init_telemetry)15982f5c4025SGowrishankar Muthukrishnan RTE_INIT(mempool_init_telemetry)
15992f5c4025SGowrishankar Muthukrishnan {
16002f5c4025SGowrishankar Muthukrishnan rte_telemetry_register_cmd("/mempool/list", mempool_handle_list,
16012f5c4025SGowrishankar Muthukrishnan "Returns list of available mempool. Takes no parameters");
16022f5c4025SGowrishankar Muthukrishnan rte_telemetry_register_cmd("/mempool/info", mempool_handle_info,
16032f5c4025SGowrishankar Muthukrishnan "Returns mempool info. Parameters: pool_name");
16042f5c4025SGowrishankar Muthukrishnan }
1605