4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5 * Copyright(c) 2016 6WIND S.A.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
18 * * Neither the name of Intel Corporation nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 #include <sys/queue.h>
45 #include <rte_common.h>
47 #include <rte_debug.h>
48 #include <rte_memory.h>
49 #include <rte_memzone.h>
50 #include <rte_malloc.h>
51 #include <rte_atomic.h>
52 #include <rte_launch.h>
54 #include <rte_eal_memconfig.h>
55 #include <rte_per_lcore.h>
56 #include <rte_lcore.h>
57 #include <rte_branch_prediction.h>
58 #include <rte_errno.h>
59 #include <rte_string_fns.h>
60 #include <rte_spinlock.h>
62 #include "rte_mempool.h"
64 TAILQ_HEAD(rte_mempool_list, rte_tailq_entry);
66 static struct rte_tailq_elem rte_mempool_tailq = {
67 .name = "RTE_MEMPOOL",
69 EAL_REGISTER_TAILQ(rte_mempool_tailq)
71 #define CACHE_FLUSHTHRESH_MULTIPLIER 1.5
72 #define CALC_CACHE_FLUSHTHRESH(c) \
73 ((typeof(c))((c) * CACHE_FLUSHTHRESH_MULTIPLIER))
76 * return the greatest common divisor between a and b (fast algorithm)
79 static unsigned get_gcd(unsigned a, unsigned b)
104 * Depending on memory configuration, objects addresses are spread
105 * between channels and ranks in RAM: the pool allocator will add
106 * padding between objects. This function return the new size of the
109 static unsigned optimize_object_size(unsigned obj_size)
111 unsigned nrank, nchan;
112 unsigned new_obj_size;
114 /* get number of channels */
115 nchan = rte_memory_get_nchannel();
119 nrank = rte_memory_get_nrank();
123 /* process new object size */
124 new_obj_size = (obj_size + RTE_MEMPOOL_ALIGN_MASK) / RTE_MEMPOOL_ALIGN;
125 while (get_gcd(new_obj_size, nrank * nchan) != 1)
127 return new_obj_size * RTE_MEMPOOL_ALIGN;
131 mempool_add_elem(struct rte_mempool *mp, void *obj, rte_iova_t iova)
133 struct rte_mempool_objhdr *hdr;
134 struct rte_mempool_objtlr *tlr __rte_unused;
136 /* set mempool ptr in header */
137 hdr = RTE_PTR_SUB(obj, sizeof(*hdr));
140 STAILQ_INSERT_TAIL(&mp->elt_list, hdr, next);
141 mp->populated_size++;
143 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
144 hdr->cookie = RTE_MEMPOOL_HEADER_COOKIE2;
145 tlr = __mempool_get_trailer(obj);
146 tlr->cookie = RTE_MEMPOOL_TRAILER_COOKIE;
149 /* enqueue in ring */
150 rte_mempool_ops_enqueue_bulk(mp, &obj, 1);
153 /* call obj_cb() for each mempool element */
155 rte_mempool_obj_iter(struct rte_mempool *mp,
156 rte_mempool_obj_cb_t *obj_cb, void *obj_cb_arg)
158 struct rte_mempool_objhdr *hdr;
162 STAILQ_FOREACH(hdr, &mp->elt_list, next) {
163 obj = (char *)hdr + sizeof(*hdr);
164 obj_cb(mp, obj_cb_arg, obj, n);
171 /* call mem_cb() for each mempool memory chunk */
173 rte_mempool_mem_iter(struct rte_mempool *mp,
174 rte_mempool_mem_cb_t *mem_cb, void *mem_cb_arg)
176 struct rte_mempool_memhdr *hdr;
179 STAILQ_FOREACH(hdr, &mp->mem_list, next) {
180 mem_cb(mp, mem_cb_arg, hdr, n);
187 /* get the header, trailer and total size of a mempool element. */
189 rte_mempool_calc_obj_size(uint32_t elt_size, uint32_t flags,
190 struct rte_mempool_objsz *sz)
192 struct rte_mempool_objsz lsz;
194 sz = (sz != NULL) ? sz : &lsz;
196 sz->header_size = sizeof(struct rte_mempool_objhdr);
197 if ((flags & MEMPOOL_F_NO_CACHE_ALIGN) == 0)
198 sz->header_size = RTE_ALIGN_CEIL(sz->header_size,
201 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
202 sz->trailer_size = sizeof(struct rte_mempool_objtlr);
204 sz->trailer_size = 0;
207 /* element size is 8 bytes-aligned at least */
208 sz->elt_size = RTE_ALIGN_CEIL(elt_size, sizeof(uint64_t));
210 /* expand trailer to next cache line */
211 if ((flags & MEMPOOL_F_NO_CACHE_ALIGN) == 0) {
212 sz->total_size = sz->header_size + sz->elt_size +
214 sz->trailer_size += ((RTE_MEMPOOL_ALIGN -
215 (sz->total_size & RTE_MEMPOOL_ALIGN_MASK)) &
216 RTE_MEMPOOL_ALIGN_MASK);
220 * increase trailer to add padding between objects in order to
221 * spread them across memory channels/ranks
223 if ((flags & MEMPOOL_F_NO_SPREAD) == 0) {
225 new_size = optimize_object_size(sz->header_size + sz->elt_size +
227 sz->trailer_size = new_size - sz->header_size - sz->elt_size;
230 /* this is the size of an object, including header and trailer */
231 sz->total_size = sz->header_size + sz->elt_size + sz->trailer_size;
233 return sz->total_size;
238 * Calculate maximum amount of memory required to store given number of objects.
241 rte_mempool_xmem_size(uint32_t elt_num, size_t total_elt_sz, uint32_t pg_shift,
244 size_t obj_per_page, pg_num, pg_sz;
247 mask = MEMPOOL_F_CAPA_BLK_ALIGNED_OBJECTS | MEMPOOL_F_CAPA_PHYS_CONTIG;
248 if ((flags & mask) == mask)
249 /* alignment need one additional object */
252 if (total_elt_sz == 0)
256 return total_elt_sz * elt_num;
258 pg_sz = (size_t)1 << pg_shift;
259 obj_per_page = pg_sz / total_elt_sz;
260 if (obj_per_page == 0)
261 return RTE_ALIGN_CEIL(total_elt_sz, pg_sz) * elt_num;
263 pg_num = (elt_num + obj_per_page - 1) / obj_per_page;
264 return pg_num << pg_shift;
268 * Calculate how much memory would be actually required with the
269 * given memory footprint to store required number of elements.
272 rte_mempool_xmem_usage(__rte_unused void *vaddr, uint32_t elt_num,
273 size_t total_elt_sz, const rte_iova_t iova[], uint32_t pg_num,
274 uint32_t pg_shift, unsigned int flags)
276 uint32_t elt_cnt = 0;
277 rte_iova_t start, end;
279 size_t pg_sz = (size_t)1 << pg_shift;
282 mask = MEMPOOL_F_CAPA_BLK_ALIGNED_OBJECTS | MEMPOOL_F_CAPA_PHYS_CONTIG;
283 if ((flags & mask) == mask)
284 /* alignment need one additional object */
287 /* if iova is NULL, assume contiguous memory */
290 end = pg_sz * pg_num;
294 end = iova[0] + pg_sz;
297 while (elt_cnt < elt_num) {
299 if (end - start >= total_elt_sz) {
300 /* enough contiguous memory, add an object */
301 start += total_elt_sz;
303 } else if (iova_idx < pg_num) {
304 /* no room to store one obj, add a page */
305 if (end == iova[iova_idx]) {
308 start = iova[iova_idx];
309 end = iova[iova_idx] + pg_sz;
314 /* no more page, return how many elements fit */
315 return -(size_t)elt_cnt;
319 return (size_t)iova_idx << pg_shift;
322 /* free a memchunk allocated with rte_memzone_reserve() */
324 rte_mempool_memchunk_mz_free(__rte_unused struct rte_mempool_memhdr *memhdr,
327 const struct rte_memzone *mz = opaque;
328 rte_memzone_free(mz);
331 /* Free memory chunks used by a mempool. Objects must be in pool */
333 rte_mempool_free_memchunks(struct rte_mempool *mp)
335 struct rte_mempool_memhdr *memhdr;
338 while (!STAILQ_EMPTY(&mp->elt_list)) {
339 rte_mempool_ops_dequeue_bulk(mp, &elt, 1);
341 STAILQ_REMOVE_HEAD(&mp->elt_list, next);
342 mp->populated_size--;
345 while (!STAILQ_EMPTY(&mp->mem_list)) {
346 memhdr = STAILQ_FIRST(&mp->mem_list);
347 STAILQ_REMOVE_HEAD(&mp->mem_list, next);
348 if (memhdr->free_cb != NULL)
349 memhdr->free_cb(memhdr, memhdr->opaque);
355 /* Add objects in the pool, using a physically contiguous memory
356 * zone. Return the number of objects added, or a negative value
360 rte_mempool_populate_iova(struct rte_mempool *mp, char *vaddr,
361 rte_iova_t iova, size_t len, rte_mempool_memchunk_free_cb_t *free_cb,
364 unsigned total_elt_sz;
365 unsigned int mp_capa_flags;
368 struct rte_mempool_memhdr *memhdr;
371 /* create the internal ring if not already done */
372 if ((mp->flags & MEMPOOL_F_POOL_CREATED) == 0) {
373 ret = rte_mempool_ops_alloc(mp);
376 mp->flags |= MEMPOOL_F_POOL_CREATED;
379 /* Notify memory area to mempool */
380 ret = rte_mempool_ops_register_memory_area(mp, vaddr, iova, len);
381 if (ret != -ENOTSUP && ret < 0)
384 /* mempool is already populated */
385 if (mp->populated_size >= mp->size)
388 total_elt_sz = mp->header_size + mp->elt_size + mp->trailer_size;
390 /* Get mempool capabilities */
392 ret = rte_mempool_ops_get_capabilities(mp, &mp_capa_flags);
393 if ((ret < 0) && (ret != -ENOTSUP))
396 /* update mempool capabilities */
397 mp->flags |= mp_capa_flags;
399 /* Detect pool area has sufficient space for elements */
400 if (mp_capa_flags & MEMPOOL_F_CAPA_PHYS_CONTIG) {
401 if (len < total_elt_sz * mp->size) {
402 RTE_LOG(ERR, MEMPOOL,
403 "pool area %" PRIx64 " not enough\n",
409 memhdr = rte_zmalloc("MEMPOOL_MEMHDR", sizeof(*memhdr), 0);
414 memhdr->addr = vaddr;
417 memhdr->free_cb = free_cb;
418 memhdr->opaque = opaque;
420 if (mp_capa_flags & MEMPOOL_F_CAPA_BLK_ALIGNED_OBJECTS)
421 /* align object start address to a multiple of total_elt_sz */
422 off = total_elt_sz - ((uintptr_t)vaddr % total_elt_sz);
423 else if (mp->flags & MEMPOOL_F_NO_CACHE_ALIGN)
424 off = RTE_PTR_ALIGN_CEIL(vaddr, 8) - vaddr;
426 off = RTE_PTR_ALIGN_CEIL(vaddr, RTE_CACHE_LINE_SIZE) - vaddr;
428 while (off + total_elt_sz <= len && mp->populated_size < mp->size) {
429 off += mp->header_size;
430 if (iova == RTE_BAD_IOVA)
431 mempool_add_elem(mp, (char *)vaddr + off,
434 mempool_add_elem(mp, (char *)vaddr + off, iova + off);
435 off += mp->elt_size + mp->trailer_size;
439 /* not enough room to store one object */
443 STAILQ_INSERT_TAIL(&mp->mem_list, memhdr, next);
449 rte_mempool_populate_phys(struct rte_mempool *mp, char *vaddr,
450 phys_addr_t paddr, size_t len, rte_mempool_memchunk_free_cb_t *free_cb,
453 return rte_mempool_populate_iova(mp, vaddr, paddr, len, free_cb, opaque);
456 /* Add objects in the pool, using a table of physical pages. Return the
457 * number of objects added, or a negative value on error.
460 rte_mempool_populate_iova_tab(struct rte_mempool *mp, char *vaddr,
461 const rte_iova_t iova[], uint32_t pg_num, uint32_t pg_shift,
462 rte_mempool_memchunk_free_cb_t *free_cb, void *opaque)
466 size_t pg_sz = (size_t)1 << pg_shift;
468 /* mempool must not be populated */
469 if (mp->nb_mem_chunks != 0)
472 if (mp->flags & MEMPOOL_F_NO_PHYS_CONTIG)
473 return rte_mempool_populate_iova(mp, vaddr, RTE_BAD_IOVA,
474 pg_num * pg_sz, free_cb, opaque);
476 for (i = 0; i < pg_num && mp->populated_size < mp->size; i += n) {
478 /* populate with the largest group of contiguous pages */
479 for (n = 1; (i + n) < pg_num &&
480 iova[i + n - 1] + pg_sz == iova[i + n]; n++)
483 ret = rte_mempool_populate_iova(mp, vaddr + i * pg_sz,
484 iova[i], n * pg_sz, free_cb, opaque);
486 rte_mempool_free_memchunks(mp);
489 /* no need to call the free callback for next chunks */
497 rte_mempool_populate_phys_tab(struct rte_mempool *mp, char *vaddr,
498 const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift,
499 rte_mempool_memchunk_free_cb_t *free_cb, void *opaque)
501 return rte_mempool_populate_iova_tab(mp, vaddr, paddr, pg_num, pg_shift,
505 /* Populate the mempool with a virtual area. Return the number of
506 * objects added, or a negative value on error.
509 rte_mempool_populate_virt(struct rte_mempool *mp, char *addr,
510 size_t len, size_t pg_sz, rte_mempool_memchunk_free_cb_t *free_cb,
514 size_t off, phys_len;
517 /* mempool must not be populated */
518 if (mp->nb_mem_chunks != 0)
520 /* address and len must be page-aligned */
521 if (RTE_PTR_ALIGN_CEIL(addr, pg_sz) != addr)
523 if (RTE_ALIGN_CEIL(len, pg_sz) != len)
526 if (mp->flags & MEMPOOL_F_NO_PHYS_CONTIG)
527 return rte_mempool_populate_iova(mp, addr, RTE_BAD_IOVA,
528 len, free_cb, opaque);
530 for (off = 0; off + pg_sz <= len &&
531 mp->populated_size < mp->size; off += phys_len) {
533 iova = rte_mem_virt2iova(addr + off);
535 if (iova == RTE_BAD_IOVA && rte_eal_has_hugepages()) {
540 /* populate with the largest group of contiguous pages */
541 for (phys_len = pg_sz; off + phys_len < len; phys_len += pg_sz) {
544 iova_tmp = rte_mem_virt2iova(addr + off + phys_len);
546 if (iova_tmp != iova + phys_len)
550 ret = rte_mempool_populate_iova(mp, addr + off, iova,
551 phys_len, free_cb, opaque);
554 /* no need to call the free callback for next chunks */
562 rte_mempool_free_memchunks(mp);
566 /* Default function to populate the mempool: allocate memory in memzones,
567 * and populate them. Return the number of objects added, or a negative
571 rte_mempool_populate_default(struct rte_mempool *mp)
573 unsigned int mz_flags = RTE_MEMZONE_1GB|RTE_MEMZONE_SIZE_HINT_ONLY;
574 char mz_name[RTE_MEMZONE_NAMESIZE];
575 const struct rte_memzone *mz;
576 size_t size, total_elt_sz, align, pg_sz, pg_shift;
579 unsigned int mp_flags;
582 /* mempool must not be populated */
583 if (mp->nb_mem_chunks != 0)
586 /* Get mempool capabilities */
588 ret = rte_mempool_ops_get_capabilities(mp, &mp_flags);
589 if ((ret < 0) && (ret != -ENOTSUP))
592 /* update mempool capabilities */
593 mp->flags |= mp_flags;
595 if (rte_eal_has_hugepages()) {
596 pg_shift = 0; /* not needed, zone is physically contiguous */
598 align = RTE_CACHE_LINE_SIZE;
600 pg_sz = getpagesize();
601 pg_shift = rte_bsf32(pg_sz);
605 total_elt_sz = mp->header_size + mp->elt_size + mp->trailer_size;
606 for (mz_id = 0, n = mp->size; n > 0; mz_id++, n -= ret) {
607 size = rte_mempool_xmem_size(n, total_elt_sz, pg_shift,
610 ret = snprintf(mz_name, sizeof(mz_name),
611 RTE_MEMPOOL_MZ_FORMAT "_%d", mp->name, mz_id);
612 if (ret < 0 || ret >= (int)sizeof(mz_name)) {
617 mz = rte_memzone_reserve_aligned(mz_name, size,
618 mp->socket_id, mz_flags, align);
619 /* not enough memory, retry with the biggest zone we have */
621 mz = rte_memzone_reserve_aligned(mz_name, 0,
622 mp->socket_id, mz_flags, align);
628 if (mp->flags & MEMPOOL_F_NO_PHYS_CONTIG)
633 if (rte_eal_has_hugepages())
634 ret = rte_mempool_populate_iova(mp, mz->addr,
636 rte_mempool_memchunk_mz_free,
637 (void *)(uintptr_t)mz);
639 ret = rte_mempool_populate_virt(mp, mz->addr,
641 rte_mempool_memchunk_mz_free,
642 (void *)(uintptr_t)mz);
644 rte_memzone_free(mz);
652 rte_mempool_free_memchunks(mp);
656 /* return the memory size required for mempool objects in anonymous mem */
658 get_anon_size(const struct rte_mempool *mp)
660 size_t size, total_elt_sz, pg_sz, pg_shift;
662 pg_sz = getpagesize();
663 pg_shift = rte_bsf32(pg_sz);
664 total_elt_sz = mp->header_size + mp->elt_size + mp->trailer_size;
665 size = rte_mempool_xmem_size(mp->size, total_elt_sz, pg_shift,
671 /* unmap a memory zone mapped by rte_mempool_populate_anon() */
673 rte_mempool_memchunk_anon_free(struct rte_mempool_memhdr *memhdr,
676 munmap(opaque, get_anon_size(memhdr->mp));
679 /* populate the mempool with an anonymous mapping */
681 rte_mempool_populate_anon(struct rte_mempool *mp)
687 /* mempool is already populated, error */
688 if (!STAILQ_EMPTY(&mp->mem_list)) {
693 /* get chunk of virtually continuous memory */
694 size = get_anon_size(mp);
695 addr = mmap(NULL, size, PROT_READ | PROT_WRITE,
696 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
697 if (addr == MAP_FAILED) {
701 /* can't use MMAP_LOCKED, it does not exist on BSD */
702 if (mlock(addr, size) < 0) {
708 ret = rte_mempool_populate_virt(mp, addr, size, getpagesize(),
709 rte_mempool_memchunk_anon_free, addr);
713 return mp->populated_size;
716 rte_mempool_free_memchunks(mp);
722 rte_mempool_free(struct rte_mempool *mp)
724 struct rte_mempool_list *mempool_list = NULL;
725 struct rte_tailq_entry *te;
730 mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
731 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
732 /* find out tailq entry */
733 TAILQ_FOREACH(te, mempool_list, next) {
734 if (te->data == (void *)mp)
739 TAILQ_REMOVE(mempool_list, te, next);
742 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
744 rte_mempool_free_memchunks(mp);
745 rte_mempool_ops_free(mp);
746 rte_memzone_free(mp->mz);
750 mempool_cache_init(struct rte_mempool_cache *cache, uint32_t size)
753 cache->flushthresh = CALC_CACHE_FLUSHTHRESH(size);
758 * Create and initialize a cache for objects that are retrieved from and
759 * returned to an underlying mempool. This structure is identical to the
760 * local_cache[lcore_id] pointed to by the mempool structure.
762 struct rte_mempool_cache *
763 rte_mempool_cache_create(uint32_t size, int socket_id)
765 struct rte_mempool_cache *cache;
767 if (size == 0 || size > RTE_MEMPOOL_CACHE_MAX_SIZE) {
772 cache = rte_zmalloc_socket("MEMPOOL_CACHE", sizeof(*cache),
773 RTE_CACHE_LINE_SIZE, socket_id);
775 RTE_LOG(ERR, MEMPOOL, "Cannot allocate mempool cache.\n");
780 mempool_cache_init(cache, size);
786 * Free a cache. It's the responsibility of the user to make sure that any
787 * remaining objects in the cache are flushed to the corresponding
791 rte_mempool_cache_free(struct rte_mempool_cache *cache)
796 /* create an empty mempool */
798 rte_mempool_create_empty(const char *name, unsigned n, unsigned elt_size,
799 unsigned cache_size, unsigned private_data_size,
800 int socket_id, unsigned flags)
802 char mz_name[RTE_MEMZONE_NAMESIZE];
803 struct rte_mempool_list *mempool_list;
804 struct rte_mempool *mp = NULL;
805 struct rte_tailq_entry *te = NULL;
806 const struct rte_memzone *mz = NULL;
808 unsigned int mz_flags = RTE_MEMZONE_1GB|RTE_MEMZONE_SIZE_HINT_ONLY;
809 struct rte_mempool_objsz objsz;
813 /* compilation-time checks */
814 RTE_BUILD_BUG_ON((sizeof(struct rte_mempool) &
815 RTE_CACHE_LINE_MASK) != 0);
816 RTE_BUILD_BUG_ON((sizeof(struct rte_mempool_cache) &
817 RTE_CACHE_LINE_MASK) != 0);
818 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
819 RTE_BUILD_BUG_ON((sizeof(struct rte_mempool_debug_stats) &
820 RTE_CACHE_LINE_MASK) != 0);
821 RTE_BUILD_BUG_ON((offsetof(struct rte_mempool, stats) &
822 RTE_CACHE_LINE_MASK) != 0);
825 mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
827 /* asked cache too big */
828 if (cache_size > RTE_MEMPOOL_CACHE_MAX_SIZE ||
829 CALC_CACHE_FLUSHTHRESH(cache_size) > n) {
834 /* "no cache align" imply "no spread" */
835 if (flags & MEMPOOL_F_NO_CACHE_ALIGN)
836 flags |= MEMPOOL_F_NO_SPREAD;
838 /* calculate mempool object sizes. */
839 if (!rte_mempool_calc_obj_size(elt_size, flags, &objsz)) {
844 rte_rwlock_write_lock(RTE_EAL_MEMPOOL_RWLOCK);
847 * reserve a memory zone for this mempool: private data is
850 private_data_size = (private_data_size +
851 RTE_MEMPOOL_ALIGN_MASK) & (~RTE_MEMPOOL_ALIGN_MASK);
854 /* try to allocate tailq entry */
855 te = rte_zmalloc("MEMPOOL_TAILQ_ENTRY", sizeof(*te), 0);
857 RTE_LOG(ERR, MEMPOOL, "Cannot allocate tailq entry!\n");
861 mempool_size = MEMPOOL_HEADER_SIZE(mp, cache_size);
862 mempool_size += private_data_size;
863 mempool_size = RTE_ALIGN_CEIL(mempool_size, RTE_MEMPOOL_ALIGN);
865 ret = snprintf(mz_name, sizeof(mz_name), RTE_MEMPOOL_MZ_FORMAT, name);
866 if (ret < 0 || ret >= (int)sizeof(mz_name)) {
867 rte_errno = ENAMETOOLONG;
871 mz = rte_memzone_reserve(mz_name, mempool_size, socket_id, mz_flags);
875 /* init the mempool structure */
877 memset(mp, 0, MEMPOOL_HEADER_SIZE(mp, cache_size));
878 ret = snprintf(mp->name, sizeof(mp->name), "%s", name);
879 if (ret < 0 || ret >= (int)sizeof(mp->name)) {
880 rte_errno = ENAMETOOLONG;
886 mp->socket_id = socket_id;
887 mp->elt_size = objsz.elt_size;
888 mp->header_size = objsz.header_size;
889 mp->trailer_size = objsz.trailer_size;
890 /* Size of default caches, zero means disabled. */
891 mp->cache_size = cache_size;
892 mp->private_data_size = private_data_size;
893 STAILQ_INIT(&mp->elt_list);
894 STAILQ_INIT(&mp->mem_list);
897 * local_cache pointer is set even if cache_size is zero.
898 * The local_cache points to just past the elt_pa[] array.
900 mp->local_cache = (struct rte_mempool_cache *)
901 RTE_PTR_ADD(mp, MEMPOOL_HEADER_SIZE(mp, 0));
903 /* Init all default caches. */
904 if (cache_size != 0) {
905 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++)
906 mempool_cache_init(&mp->local_cache[lcore_id],
912 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
913 TAILQ_INSERT_TAIL(mempool_list, te, next);
914 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
915 rte_rwlock_write_unlock(RTE_EAL_MEMPOOL_RWLOCK);
920 rte_rwlock_write_unlock(RTE_EAL_MEMPOOL_RWLOCK);
922 rte_mempool_free(mp);
926 /* create the mempool */
928 rte_mempool_create(const char *name, unsigned n, unsigned elt_size,
929 unsigned cache_size, unsigned private_data_size,
930 rte_mempool_ctor_t *mp_init, void *mp_init_arg,
931 rte_mempool_obj_cb_t *obj_init, void *obj_init_arg,
932 int socket_id, unsigned flags)
935 struct rte_mempool *mp;
937 mp = rte_mempool_create_empty(name, n, elt_size, cache_size,
938 private_data_size, socket_id, flags);
943 * Since we have 4 combinations of the SP/SC/MP/MC examine the flags to
944 * set the correct index into the table of ops structs.
946 if ((flags & MEMPOOL_F_SP_PUT) && (flags & MEMPOOL_F_SC_GET))
947 ret = rte_mempool_set_ops_byname(mp, "ring_sp_sc", NULL);
948 else if (flags & MEMPOOL_F_SP_PUT)
949 ret = rte_mempool_set_ops_byname(mp, "ring_sp_mc", NULL);
950 else if (flags & MEMPOOL_F_SC_GET)
951 ret = rte_mempool_set_ops_byname(mp, "ring_mp_sc", NULL);
953 ret = rte_mempool_set_ops_byname(mp, "ring_mp_mc", NULL);
958 /* call the mempool priv initializer */
960 mp_init(mp, mp_init_arg);
962 if (rte_mempool_populate_default(mp) < 0)
965 /* call the object initializers */
967 rte_mempool_obj_iter(mp, obj_init, obj_init_arg);
972 rte_mempool_free(mp);
977 * Create the mempool over already allocated chunk of memory.
978 * That external memory buffer can consists of physically disjoint pages.
979 * Setting vaddr to NULL, makes mempool to fallback to rte_mempool_create()
983 rte_mempool_xmem_create(const char *name, unsigned n, unsigned elt_size,
984 unsigned cache_size, unsigned private_data_size,
985 rte_mempool_ctor_t *mp_init, void *mp_init_arg,
986 rte_mempool_obj_cb_t *obj_init, void *obj_init_arg,
987 int socket_id, unsigned flags, void *vaddr,
988 const rte_iova_t iova[], uint32_t pg_num, uint32_t pg_shift)
990 struct rte_mempool *mp = NULL;
993 /* no virtual address supplied, use rte_mempool_create() */
995 return rte_mempool_create(name, n, elt_size, cache_size,
996 private_data_size, mp_init, mp_init_arg,
997 obj_init, obj_init_arg, socket_id, flags);
999 /* check that we have both VA and PA */
1005 /* Check that pg_shift parameter is valid. */
1006 if (pg_shift > MEMPOOL_PG_SHIFT_MAX) {
1011 mp = rte_mempool_create_empty(name, n, elt_size, cache_size,
1012 private_data_size, socket_id, flags);
1016 /* call the mempool priv initializer */
1018 mp_init(mp, mp_init_arg);
1020 ret = rte_mempool_populate_iova_tab(mp, vaddr, iova, pg_num, pg_shift,
1022 if (ret < 0 || ret != (int)mp->size)
1025 /* call the object initializers */
1027 rte_mempool_obj_iter(mp, obj_init, obj_init_arg);
1032 rte_mempool_free(mp);
1036 /* Return the number of entries in the mempool */
1038 rte_mempool_avail_count(const struct rte_mempool *mp)
1043 count = rte_mempool_ops_get_count(mp);
1045 if (mp->cache_size == 0)
1048 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++)
1049 count += mp->local_cache[lcore_id].len;
1052 * due to race condition (access to len is not locked), the
1053 * total can be greater than size... so fix the result
1055 if (count > mp->size)
1060 /* return the number of entries allocated from the mempool */
1062 rte_mempool_in_use_count(const struct rte_mempool *mp)
1064 return mp->size - rte_mempool_avail_count(mp);
1067 /* dump the cache status */
1069 rte_mempool_dump_cache(FILE *f, const struct rte_mempool *mp)
1073 unsigned cache_count;
1075 fprintf(f, " internal cache infos:\n");
1076 fprintf(f, " cache_size=%"PRIu32"\n", mp->cache_size);
1078 if (mp->cache_size == 0)
1081 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1082 cache_count = mp->local_cache[lcore_id].len;
1083 fprintf(f, " cache_count[%u]=%"PRIu32"\n",
1084 lcore_id, cache_count);
1085 count += cache_count;
1087 fprintf(f, " total_cache_count=%u\n", count);
1091 #ifndef __INTEL_COMPILER
1092 #pragma GCC diagnostic ignored "-Wcast-qual"
1095 /* check and update cookies or panic (internal) */
1096 void rte_mempool_check_cookies(const struct rte_mempool *mp,
1097 void * const *obj_table_const, unsigned n, int free)
1099 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
1100 struct rte_mempool_objhdr *hdr;
1101 struct rte_mempool_objtlr *tlr;
1107 /* Force to drop the "const" attribute. This is done only when
1108 * DEBUG is enabled */
1109 tmp = (void *) obj_table_const;
1115 if (rte_mempool_from_obj(obj) != mp)
1116 rte_panic("MEMPOOL: object is owned by another "
1119 hdr = __mempool_get_header(obj);
1120 cookie = hdr->cookie;
1123 if (cookie != RTE_MEMPOOL_HEADER_COOKIE1) {
1124 RTE_LOG(CRIT, MEMPOOL,
1125 "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
1126 obj, (const void *) mp, cookie);
1127 rte_panic("MEMPOOL: bad header cookie (put)\n");
1129 hdr->cookie = RTE_MEMPOOL_HEADER_COOKIE2;
1130 } else if (free == 1) {
1131 if (cookie != RTE_MEMPOOL_HEADER_COOKIE2) {
1132 RTE_LOG(CRIT, MEMPOOL,
1133 "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
1134 obj, (const void *) mp, cookie);
1135 rte_panic("MEMPOOL: bad header cookie (get)\n");
1137 hdr->cookie = RTE_MEMPOOL_HEADER_COOKIE1;
1138 } else if (free == 2) {
1139 if (cookie != RTE_MEMPOOL_HEADER_COOKIE1 &&
1140 cookie != RTE_MEMPOOL_HEADER_COOKIE2) {
1141 RTE_LOG(CRIT, MEMPOOL,
1142 "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
1143 obj, (const void *) mp, cookie);
1144 rte_panic("MEMPOOL: bad header cookie (audit)\n");
1147 tlr = __mempool_get_trailer(obj);
1148 cookie = tlr->cookie;
1149 if (cookie != RTE_MEMPOOL_TRAILER_COOKIE) {
1150 RTE_LOG(CRIT, MEMPOOL,
1151 "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
1152 obj, (const void *) mp, cookie);
1153 rte_panic("MEMPOOL: bad trailer cookie\n");
1158 RTE_SET_USED(obj_table_const);
1164 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
1166 mempool_obj_audit(struct rte_mempool *mp, __rte_unused void *opaque,
1167 void *obj, __rte_unused unsigned idx)
1169 __mempool_check_cookies(mp, &obj, 1, 2);
1173 mempool_audit_cookies(struct rte_mempool *mp)
1177 num = rte_mempool_obj_iter(mp, mempool_obj_audit, NULL);
1178 if (num != mp->size) {
1179 rte_panic("rte_mempool_obj_iter(mempool=%p, size=%u) "
1180 "iterated only over %u elements\n",
1185 #define mempool_audit_cookies(mp) do {} while(0)
1188 #ifndef __INTEL_COMPILER
1189 #pragma GCC diagnostic error "-Wcast-qual"
1192 /* check cookies before and after objects */
1194 mempool_audit_cache(const struct rte_mempool *mp)
1196 /* check cache size consistency */
1199 if (mp->cache_size == 0)
1202 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1203 const struct rte_mempool_cache *cache;
1204 cache = &mp->local_cache[lcore_id];
1205 if (cache->len > cache->flushthresh) {
1206 RTE_LOG(CRIT, MEMPOOL, "badness on cache[%u]\n",
1208 rte_panic("MEMPOOL: invalid cache len\n");
1213 /* check the consistency of mempool (size, cookies, ...) */
1215 rte_mempool_audit(struct rte_mempool *mp)
1217 mempool_audit_cache(mp);
1218 mempool_audit_cookies(mp);
1220 /* For case where mempool DEBUG is not set, and cache size is 0 */
1224 /* dump the status of the mempool on the console */
1226 rte_mempool_dump(FILE *f, struct rte_mempool *mp)
1228 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
1229 struct rte_mempool_debug_stats sum;
1232 struct rte_mempool_memhdr *memhdr;
1233 unsigned common_count;
1234 unsigned cache_count;
1237 RTE_ASSERT(f != NULL);
1238 RTE_ASSERT(mp != NULL);
1240 fprintf(f, "mempool <%s>@%p\n", mp->name, mp);
1241 fprintf(f, " flags=%x\n", mp->flags);
1242 fprintf(f, " pool=%p\n", mp->pool_data);
1243 fprintf(f, " iova=0x%" PRIx64 "\n", mp->mz->iova);
1244 fprintf(f, " nb_mem_chunks=%u\n", mp->nb_mem_chunks);
1245 fprintf(f, " size=%"PRIu32"\n", mp->size);
1246 fprintf(f, " populated_size=%"PRIu32"\n", mp->populated_size);
1247 fprintf(f, " header_size=%"PRIu32"\n", mp->header_size);
1248 fprintf(f, " elt_size=%"PRIu32"\n", mp->elt_size);
1249 fprintf(f, " trailer_size=%"PRIu32"\n", mp->trailer_size);
1250 fprintf(f, " total_obj_size=%"PRIu32"\n",
1251 mp->header_size + mp->elt_size + mp->trailer_size);
1253 fprintf(f, " private_data_size=%"PRIu32"\n", mp->private_data_size);
1255 STAILQ_FOREACH(memhdr, &mp->mem_list, next)
1256 mem_len += memhdr->len;
1258 fprintf(f, " avg bytes/object=%#Lf\n",
1259 (long double)mem_len / mp->size);
1262 cache_count = rte_mempool_dump_cache(f, mp);
1263 common_count = rte_mempool_ops_get_count(mp);
1264 if ((cache_count + common_count) > mp->size)
1265 common_count = mp->size - cache_count;
1266 fprintf(f, " common_pool_count=%u\n", common_count);
1268 /* sum and dump statistics */
1269 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
1270 memset(&sum, 0, sizeof(sum));
1271 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1272 sum.put_bulk += mp->stats[lcore_id].put_bulk;
1273 sum.put_objs += mp->stats[lcore_id].put_objs;
1274 sum.get_success_bulk += mp->stats[lcore_id].get_success_bulk;
1275 sum.get_success_objs += mp->stats[lcore_id].get_success_objs;
1276 sum.get_fail_bulk += mp->stats[lcore_id].get_fail_bulk;
1277 sum.get_fail_objs += mp->stats[lcore_id].get_fail_objs;
1279 fprintf(f, " stats:\n");
1280 fprintf(f, " put_bulk=%"PRIu64"\n", sum.put_bulk);
1281 fprintf(f, " put_objs=%"PRIu64"\n", sum.put_objs);
1282 fprintf(f, " get_success_bulk=%"PRIu64"\n", sum.get_success_bulk);
1283 fprintf(f, " get_success_objs=%"PRIu64"\n", sum.get_success_objs);
1284 fprintf(f, " get_fail_bulk=%"PRIu64"\n", sum.get_fail_bulk);
1285 fprintf(f, " get_fail_objs=%"PRIu64"\n", sum.get_fail_objs);
1287 fprintf(f, " no statistics available\n");
1290 rte_mempool_audit(mp);
1293 /* dump the status of all mempools on the console */
1295 rte_mempool_list_dump(FILE *f)
1297 struct rte_mempool *mp = NULL;
1298 struct rte_tailq_entry *te;
1299 struct rte_mempool_list *mempool_list;
1301 mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
1303 rte_rwlock_read_lock(RTE_EAL_MEMPOOL_RWLOCK);
1305 TAILQ_FOREACH(te, mempool_list, next) {
1306 mp = (struct rte_mempool *) te->data;
1307 rte_mempool_dump(f, mp);
1310 rte_rwlock_read_unlock(RTE_EAL_MEMPOOL_RWLOCK);
1313 /* search a mempool from its name */
1314 struct rte_mempool *
1315 rte_mempool_lookup(const char *name)
1317 struct rte_mempool *mp = NULL;
1318 struct rte_tailq_entry *te;
1319 struct rte_mempool_list *mempool_list;
1321 mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
1323 rte_rwlock_read_lock(RTE_EAL_MEMPOOL_RWLOCK);
1325 TAILQ_FOREACH(te, mempool_list, next) {
1326 mp = (struct rte_mempool *) te->data;
1327 if (strncmp(name, mp->name, RTE_MEMPOOL_NAMESIZE) == 0)
1331 rte_rwlock_read_unlock(RTE_EAL_MEMPOOL_RWLOCK);
1341 void rte_mempool_walk(void (*func)(struct rte_mempool *, void *),
1344 struct rte_tailq_entry *te = NULL;
1345 struct rte_mempool_list *mempool_list;
1348 mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
1350 rte_rwlock_read_lock(RTE_EAL_MEMPOOL_RWLOCK);
1352 TAILQ_FOREACH_SAFE(te, mempool_list, next, tmp_te) {
1353 (*func)((struct rte_mempool *) te->data, arg);
1356 rte_rwlock_read_unlock(RTE_EAL_MEMPOOL_RWLOCK);