4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #ifndef _RTE_MEMPOOL_H_
35 #define _RTE_MEMPOOL_H_
41 * A memory pool is an allocator of fixed-size object. It is
42 * identified by its name, and uses a ring to store free objects. It
43 * provides some other optional services, like a per-core object
44 * cache, and an alignment helper to ensure that objects are padded
45 * to spread them equally on all RAM channels, ranks, and so on.
47 * Objects owned by a mempool should never be added in another
48 * mempool. When an object is freed using rte_mempool_put() or
49 * equivalent, the object data is not modified; the user can save some
50 * meta-data in the object data and retrieve them when allocating a
53 * Note: the mempool implementation is not preemptable. A lcore must
54 * not be interrupted by another task that uses the same mempool
55 * (because it uses a ring which is not preemptable). Also, mempool
56 * functions must not be used outside the DPDK environment: for
57 * example, in linuxapp environment, a thread that is not created by
58 * the EAL must not use mempools. This is due to the per-lcore cache
59 * that won't work as rte_lcore_id() will not return a correct value.
67 #include <sys/queue.h>
70 #include <rte_debug.h>
71 #include <rte_lcore.h>
72 #include <rte_memory.h>
73 #include <rte_branch_prediction.h>
80 #define RTE_MEMPOOL_HEADER_COOKIE1 0xbadbadbadadd2e55ULL /**< Header cookie. */
81 #define RTE_MEMPOOL_HEADER_COOKIE2 0xf2eef2eedadd2e55ULL /**< Header cookie. */
82 #define RTE_MEMPOOL_TRAILER_COOKIE 0xadd2e55badbadbadULL /**< Trailer cookie.*/
84 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
86 * A structure that stores the mempool statistics (per-lcore).
88 struct rte_mempool_debug_stats {
89 uint64_t put_bulk; /**< Number of puts. */
90 uint64_t put_objs; /**< Number of objects successfully put. */
91 uint64_t get_success_bulk; /**< Successful allocation number. */
92 uint64_t get_success_objs; /**< Objects successfully allocated. */
93 uint64_t get_fail_bulk; /**< Failed allocation number. */
94 uint64_t get_fail_objs; /**< Objects that failed to be allocated. */
95 } __rte_cache_aligned;
99 * A structure that stores a per-core object cache.
101 struct rte_mempool_cache {
102 unsigned len; /**< Cache len */
104 * Cache is allocated to this size to allow it to overflow in certain
105 * cases to avoid needless emptying of cache.
107 void *objs[RTE_MEMPOOL_CACHE_MAX_SIZE * 3]; /**< Cache objects */
108 } __rte_cache_aligned;
111 * A structure that stores the size of mempool elements.
113 struct rte_mempool_objsz {
114 uint32_t elt_size; /**< Size of an element. */
115 uint32_t header_size; /**< Size of header (before elt). */
116 uint32_t trailer_size; /**< Size of trailer (after elt). */
118 /**< Total size of an object (header + elt + trailer). */
121 #define RTE_MEMPOOL_NAMESIZE 32 /**< Maximum length of a memory pool. */
122 #define RTE_MEMPOOL_MZ_PREFIX "MP_"
125 #define RTE_MEMPOOL_MZ_FORMAT RTE_MEMPOOL_MZ_PREFIX "%s"
127 #ifdef RTE_LIBRTE_XEN_DOM0
129 /* "<name>_MP_elt" */
130 #define RTE_MEMPOOL_OBJ_NAME "%s_" RTE_MEMPOOL_MZ_PREFIX "elt"
134 #define RTE_MEMPOOL_OBJ_NAME RTE_MEMPOOL_MZ_FORMAT
136 #endif /* RTE_LIBRTE_XEN_DOM0 */
138 #define MEMPOOL_PG_SHIFT_MAX (sizeof(uintptr_t) * CHAR_BIT - 1)
140 /** Mempool over one chunk of physically continuous memory */
141 #define MEMPOOL_PG_NUM_DEFAULT 1
143 #ifndef RTE_MEMPOOL_ALIGN
144 #define RTE_MEMPOOL_ALIGN RTE_CACHE_LINE_SIZE
147 #define RTE_MEMPOOL_ALIGN_MASK (RTE_MEMPOOL_ALIGN - 1)
150 * Mempool object header structure
152 * Each object stored in mempools are prefixed by this header structure,
153 * it allows to retrieve the mempool pointer from the object. When debug
154 * is enabled, a cookie is also added in this structure preventing
155 * corruptions and double-frees.
157 struct rte_mempool_objhdr {
158 struct rte_mempool *mp; /**< The mempool owning the object. */
159 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
160 uint64_t cookie; /**< Debug cookie. */
165 * Mempool object trailer structure
167 * In debug mode, each object stored in mempools are suffixed by this
168 * trailer structure containing a cookie preventing memory corruptions.
170 struct rte_mempool_objtlr {
171 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
172 uint64_t cookie; /**< Debug cookie. */
177 * The RTE mempool structure.
180 char name[RTE_MEMPOOL_NAMESIZE]; /**< Name of mempool. */
181 struct rte_ring *ring; /**< Ring to store objects. */
182 phys_addr_t phys_addr; /**< Phys. addr. of mempool struct. */
183 int flags; /**< Flags of the mempool. */
184 uint32_t size; /**< Size of the mempool. */
185 uint32_t cache_size; /**< Size of per-lcore local cache. */
186 uint32_t cache_flushthresh;
187 /**< Threshold before we flush excess elements. */
189 uint32_t elt_size; /**< Size of an element. */
190 uint32_t header_size; /**< Size of header (before elt). */
191 uint32_t trailer_size; /**< Size of trailer (after elt). */
193 unsigned private_data_size; /**< Size of private data. */
195 struct rte_mempool_cache *local_cache; /**< Per-lcore local cache */
197 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
198 /** Per-lcore statistics. */
199 struct rte_mempool_debug_stats stats[RTE_MAX_LCORE];
202 /* Address translation support, starts from next cache line. */
204 /** Number of elements in the elt_pa array. */
205 uint32_t pg_num __rte_cache_aligned;
206 uint32_t pg_shift; /**< LOG2 of the physical pages. */
207 uintptr_t pg_mask; /**< physical page mask value. */
208 uintptr_t elt_va_start;
209 /**< Virtual address of the first mempool object. */
210 uintptr_t elt_va_end;
211 /**< Virtual address of the <size + 1> mempool object. */
212 phys_addr_t elt_pa[MEMPOOL_PG_NUM_DEFAULT];
213 /**< Array of physical page addresses for the mempool objects buffer. */
215 } __rte_cache_aligned;
217 #define MEMPOOL_F_NO_SPREAD 0x0001 /**< Do not spread among memory channels. */
218 #define MEMPOOL_F_NO_CACHE_ALIGN 0x0002 /**< Do not align objs on cache lines.*/
219 #define MEMPOOL_F_SP_PUT 0x0004 /**< Default put is "single-producer".*/
220 #define MEMPOOL_F_SC_GET 0x0008 /**< Default get is "single-consumer".*/
223 * @internal When debug is enabled, store some statistics.
226 * Pointer to the memory pool.
228 * Name of the statistics field to increment in the memory pool.
230 * Number to add to the object-oriented statistics.
232 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
233 #define __MEMPOOL_STAT_ADD(mp, name, n) do { \
234 unsigned __lcore_id = rte_lcore_id(); \
235 if (__lcore_id < RTE_MAX_LCORE) { \
236 mp->stats[__lcore_id].name##_objs += n; \
237 mp->stats[__lcore_id].name##_bulk += 1; \
241 #define __MEMPOOL_STAT_ADD(mp, name, n) do {} while(0)
245 * Size of elt_pa array size based on number of pages. (Internal use)
247 #define __PA_SIZE(mp, pgn) \
248 RTE_ALIGN_CEIL((((pgn) - RTE_DIM((mp)->elt_pa)) * \
249 sizeof((mp)->elt_pa[0])), RTE_CACHE_LINE_SIZE)
252 * Calculate the size of the mempool header.
255 * Pointer to the memory pool.
257 * Number of pages used to store mempool objects.
259 * Size of the per-lcore cache.
261 #define MEMPOOL_HEADER_SIZE(mp, pgn, cs) \
262 (sizeof(*(mp)) + __PA_SIZE(mp, pgn) + (((cs) == 0) ? 0 : \
263 (sizeof(struct rte_mempool_cache) * RTE_MAX_LCORE)))
266 * Return true if the whole mempool is in contiguous memory.
268 #define MEMPOOL_IS_CONTIG(mp) \
269 ((mp)->pg_num == MEMPOOL_PG_NUM_DEFAULT && \
270 (mp)->phys_addr == (mp)->elt_pa[0])
272 /* return the header of a mempool object (internal) */
273 static inline struct rte_mempool_objhdr *__mempool_get_header(void *obj)
275 return (struct rte_mempool_objhdr *)RTE_PTR_SUB(obj,
276 sizeof(struct rte_mempool_objhdr));
280 * Return a pointer to the mempool owning this object.
283 * An object that is owned by a pool. If this is not the case,
284 * the behavior is undefined.
286 * A pointer to the mempool structure.
288 static inline struct rte_mempool *rte_mempool_from_obj(void *obj)
290 struct rte_mempool_objhdr *hdr = __mempool_get_header(obj);
294 /* return the trailer of a mempool object (internal) */
295 static inline struct rte_mempool_objtlr *__mempool_get_trailer(void *obj)
297 struct rte_mempool *mp = rte_mempool_from_obj(obj);
298 return (struct rte_mempool_objtlr *)RTE_PTR_ADD(obj, mp->elt_size);
302 * @internal Check and update cookies or panic.
305 * Pointer to the memory pool.
306 * @param obj_table_const
307 * Pointer to a table of void * pointers (objects).
309 * Index of object in object table.
311 * - 0: object is supposed to be allocated, mark it as free
312 * - 1: object is supposed to be free, mark it as allocated
313 * - 2: just check that cookie is valid (free or allocated)
315 void rte_mempool_check_cookies(const struct rte_mempool *mp,
316 void * const *obj_table_const, unsigned n, int free);
318 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
319 #define __mempool_check_cookies(mp, obj_table_const, n, free) \
320 rte_mempool_check_cookies(mp, obj_table_const, n, free)
322 #define __mempool_check_cookies(mp, obj_table_const, n, free) do {} while(0)
323 #endif /* RTE_LIBRTE_MEMPOOL_DEBUG */
326 * A mempool object iterator callback function.
328 typedef void (*rte_mempool_obj_iter_t)(void * /*obj_iter_arg*/,
329 void * /*obj_start*/,
331 uint32_t /*obj_index */);
334 * Call a function for each mempool object in a memory chunk
336 * Iterate across objects of the given size and alignment in the
337 * provided chunk of memory. The given memory buffer can consist of
338 * disjointed physical pages.
340 * For each object, call the provided callback (if any). This function
341 * is used to populate a mempool, or walk through all the elements of a
342 * mempool, or estimate how many elements of the given size could be
343 * created in the given memory buffer.
346 * Virtual address of the memory buffer.
348 * Maximum number of objects to iterate through.
350 * Size of each object.
352 * Alignment of each object.
354 * Array of physical addresses of the pages that comprises given memory
357 * Number of elements in the paddr array.
359 * LOG2 of the physical pages size.
361 * Object iterator callback function (could be NULL).
362 * @param obj_iter_arg
363 * User defined parameter for the object iterator callback function.
366 * Number of objects iterated through.
368 uint32_t rte_mempool_obj_iter(void *vaddr,
369 uint32_t elt_num, size_t elt_sz, size_t align,
370 const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift,
371 rte_mempool_obj_iter_t obj_iter, void *obj_iter_arg);
374 * An object constructor callback function for mempool.
376 * Arguments are the mempool, the opaque pointer given by the user in
377 * rte_mempool_create(), the pointer to the element and the index of
378 * the element in the pool.
380 typedef void (rte_mempool_obj_ctor_t)(struct rte_mempool *, void *,
384 * A mempool constructor callback function.
386 * Arguments are the mempool and the opaque pointer given by the user in
387 * rte_mempool_create().
389 typedef void (rte_mempool_ctor_t)(struct rte_mempool *, void *);
392 * Create a new mempool named *name* in memory.
394 * This function uses ``memzone_reserve()`` to allocate memory. The
395 * pool contains n elements of elt_size. Its size is set to n.
396 * All elements of the mempool are allocated together with the mempool header,
397 * in one physically continuous chunk of memory.
400 * The name of the mempool.
402 * The number of elements in the mempool. The optimum size (in terms of
403 * memory usage) for a mempool is when n is a power of two minus one:
406 * The size of each element.
408 * If cache_size is non-zero, the rte_mempool library will try to
409 * limit the accesses to the common lockless pool, by maintaining a
410 * per-lcore object cache. This argument must be lower or equal to
411 * CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE and n / 1.5. It is advised to choose
412 * cache_size to have "n modulo cache_size == 0": if this is
413 * not the case, some elements will always stay in the pool and will
414 * never be used. The access to the per-lcore table is of course
415 * faster than the multi-producer/consumer pool. The cache can be
416 * disabled if the cache_size argument is set to 0; it can be useful to
417 * avoid losing objects in cache. Note that even if not used, the
418 * memory space for cache is always reserved in a mempool structure,
419 * except if CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE is set to 0.
420 * @param private_data_size
421 * The size of the private data appended after the mempool
422 * structure. This is useful for storing some private data after the
423 * mempool structure, as is done for rte_mbuf_pool for example.
425 * A function pointer that is called for initialization of the pool,
426 * before object initialization. The user can initialize the private
427 * data in this function if needed. This parameter can be NULL if
430 * An opaque pointer to data that can be used in the mempool
431 * constructor function.
433 * A function pointer that is called for each object at
434 * initialization of the pool. The user can set some meta data in
435 * objects if needed. This parameter can be NULL if not needed.
436 * The obj_init() function takes the mempool pointer, the init_arg,
437 * the object pointer and the object number as parameters.
438 * @param obj_init_arg
439 * An opaque pointer to data that can be used as an argument for
440 * each call to the object constructor function.
442 * The *socket_id* argument is the socket identifier in the case of
443 * NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
444 * constraint for the reserved zone.
446 * The *flags* arguments is an OR of following flags:
447 * - MEMPOOL_F_NO_SPREAD: By default, objects addresses are spread
448 * between channels in RAM: the pool allocator will add padding
449 * between objects depending on the hardware configuration. See
450 * Memory alignment constraints for details. If this flag is set,
451 * the allocator will just align them to a cache line.
452 * - MEMPOOL_F_NO_CACHE_ALIGN: By default, the returned objects are
453 * cache-aligned. This flag removes this constraint, and no
454 * padding will be present between objects. This flag implies
455 * MEMPOOL_F_NO_SPREAD.
456 * - MEMPOOL_F_SP_PUT: If this flag is set, the default behavior
457 * when using rte_mempool_put() or rte_mempool_put_bulk() is
458 * "single-producer". Otherwise, it is "multi-producers".
459 * - MEMPOOL_F_SC_GET: If this flag is set, the default behavior
460 * when using rte_mempool_get() or rte_mempool_get_bulk() is
461 * "single-consumer". Otherwise, it is "multi-consumers".
463 * The pointer to the new allocated mempool, on success. NULL on error
464 * with rte_errno set appropriately. Possible rte_errno values include:
465 * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
466 * - E_RTE_SECONDARY - function was called from a secondary process instance
467 * - EINVAL - cache size provided is too large
468 * - ENOSPC - the maximum number of memzones has already been allocated
469 * - EEXIST - a memzone with the same name already exists
470 * - ENOMEM - no appropriate memory area found in which to create memzone
473 rte_mempool_create(const char *name, unsigned n, unsigned elt_size,
474 unsigned cache_size, unsigned private_data_size,
475 rte_mempool_ctor_t *mp_init, void *mp_init_arg,
476 rte_mempool_obj_ctor_t *obj_init, void *obj_init_arg,
477 int socket_id, unsigned flags);
480 * Create a new mempool named *name* in memory.
482 * The pool contains n elements of elt_size. Its size is set to n.
483 * This function uses ``memzone_reserve()`` to allocate the mempool header
484 * (and the objects if vaddr is NULL).
485 * Depending on the input parameters, mempool elements can be either allocated
486 * together with the mempool header, or an externally provided memory buffer
487 * could be used to store mempool objects. In later case, that external
488 * memory buffer can consist of set of disjoint physical pages.
491 * The name of the mempool.
493 * The number of elements in the mempool. The optimum size (in terms of
494 * memory usage) for a mempool is when n is a power of two minus one:
497 * The size of each element.
499 * Size of the cache. See rte_mempool_create() for details.
500 * @param private_data_size
501 * The size of the private data appended after the mempool
502 * structure. This is useful for storing some private data after the
503 * mempool structure, as is done for rte_mbuf_pool for example.
505 * A function pointer that is called for initialization of the pool,
506 * before object initialization. The user can initialize the private
507 * data in this function if needed. This parameter can be NULL if
510 * An opaque pointer to data that can be used in the mempool
511 * constructor function.
513 * A function called for each object at initialization of the pool.
514 * See rte_mempool_create() for details.
515 * @param obj_init_arg
516 * An opaque pointer passed to the object constructor function.
518 * The *socket_id* argument is the socket identifier in the case of
519 * NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
520 * constraint for the reserved zone.
522 * Flags controlling the behavior of the mempool. See
523 * rte_mempool_create() for details.
525 * Virtual address of the externally allocated memory buffer.
526 * Will be used to store mempool objects.
528 * Array of physical addresses of the pages that comprises given memory
531 * Number of elements in the paddr array.
533 * LOG2 of the physical pages size.
535 * The pointer to the new allocated mempool, on success. NULL on error
536 * with rte_errno set appropriately. See rte_mempool_create() for details.
539 rte_mempool_xmem_create(const char *name, unsigned n, unsigned elt_size,
540 unsigned cache_size, unsigned private_data_size,
541 rte_mempool_ctor_t *mp_init, void *mp_init_arg,
542 rte_mempool_obj_ctor_t *obj_init, void *obj_init_arg,
543 int socket_id, unsigned flags, void *vaddr,
544 const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift);
547 * Create a new mempool named *name* in memory on Xen Dom0.
549 * This function uses ``rte_mempool_xmem_create()`` to allocate memory. The
550 * pool contains n elements of elt_size. Its size is set to n.
551 * All elements of the mempool are allocated together with the mempool header,
552 * and memory buffer can consist of set of disjoint physical pages.
555 * The name of the mempool.
557 * The number of elements in the mempool. The optimum size (in terms of
558 * memory usage) for a mempool is when n is a power of two minus one:
561 * The size of each element.
563 * If cache_size is non-zero, the rte_mempool library will try to
564 * limit the accesses to the common lockless pool, by maintaining a
565 * per-lcore object cache. This argument must be lower or equal to
566 * CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE. It is advised to choose
567 * cache_size to have "n modulo cache_size == 0": if this is
568 * not the case, some elements will always stay in the pool and will
569 * never be used. The access to the per-lcore table is of course
570 * faster than the multi-producer/consumer pool. The cache can be
571 * disabled if the cache_size argument is set to 0; it can be useful to
572 * avoid losing objects in cache. Note that even if not used, the
573 * memory space for cache is always reserved in a mempool structure,
574 * except if CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE is set to 0.
575 * @param private_data_size
576 * The size of the private data appended after the mempool
577 * structure. This is useful for storing some private data after the
578 * mempool structure, as is done for rte_mbuf_pool for example.
580 * A function pointer that is called for initialization of the pool,
581 * before object initialization. The user can initialize the private
582 * data in this function if needed. This parameter can be NULL if
585 * An opaque pointer to data that can be used in the mempool
586 * constructor function.
588 * A function pointer that is called for each object at
589 * initialization of the pool. The user can set some meta data in
590 * objects if needed. This parameter can be NULL if not needed.
591 * The obj_init() function takes the mempool pointer, the init_arg,
592 * the object pointer and the object number as parameters.
593 * @param obj_init_arg
594 * An opaque pointer to data that can be used as an argument for
595 * each call to the object constructor function.
597 * The *socket_id* argument is the socket identifier in the case of
598 * NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
599 * constraint for the reserved zone.
601 * The *flags* arguments is an OR of following flags:
602 * - MEMPOOL_F_NO_SPREAD: By default, objects addresses are spread
603 * between channels in RAM: the pool allocator will add padding
604 * between objects depending on the hardware configuration. See
605 * Memory alignment constraints for details. If this flag is set,
606 * the allocator will just align them to a cache line.
607 * - MEMPOOL_F_NO_CACHE_ALIGN: By default, the returned objects are
608 * cache-aligned. This flag removes this constraint, and no
609 * padding will be present between objects. This flag implies
610 * MEMPOOL_F_NO_SPREAD.
611 * - MEMPOOL_F_SP_PUT: If this flag is set, the default behavior
612 * when using rte_mempool_put() or rte_mempool_put_bulk() is
613 * "single-producer". Otherwise, it is "multi-producers".
614 * - MEMPOOL_F_SC_GET: If this flag is set, the default behavior
615 * when using rte_mempool_get() or rte_mempool_get_bulk() is
616 * "single-consumer". Otherwise, it is "multi-consumers".
618 * The pointer to the new allocated mempool, on success. NULL on error
619 * with rte_errno set appropriately. Possible rte_errno values include:
620 * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
621 * - E_RTE_SECONDARY - function was called from a secondary process instance
622 * - EINVAL - cache size provided is too large
623 * - ENOSPC - the maximum number of memzones has already been allocated
624 * - EEXIST - a memzone with the same name already exists
625 * - ENOMEM - no appropriate memory area found in which to create memzone
628 rte_dom0_mempool_create(const char *name, unsigned n, unsigned elt_size,
629 unsigned cache_size, unsigned private_data_size,
630 rte_mempool_ctor_t *mp_init, void *mp_init_arg,
631 rte_mempool_obj_ctor_t *obj_init, void *obj_init_arg,
632 int socket_id, unsigned flags);
636 * Dump the status of the mempool to the console.
639 * A pointer to a file for output
641 * A pointer to the mempool structure.
643 void rte_mempool_dump(FILE *f, const struct rte_mempool *mp);
646 * @internal Put several objects back in the mempool; used internally.
648 * A pointer to the mempool structure.
650 * A pointer to a table of void * pointers (objects).
652 * The number of objects to store back in the mempool, must be strictly
655 * Mono-producer (0) or multi-producers (1).
657 static inline void __attribute__((always_inline))
658 __mempool_put_bulk(struct rte_mempool *mp, void * const *obj_table,
659 unsigned n, int is_mp)
661 struct rte_mempool_cache *cache;
664 unsigned lcore_id = rte_lcore_id();
665 uint32_t cache_size = mp->cache_size;
666 uint32_t flushthresh = mp->cache_flushthresh;
668 /* increment stat now, adding in mempool always success */
669 __MEMPOOL_STAT_ADD(mp, put, n);
671 /* cache is not enabled or single producer or non-EAL thread */
672 if (unlikely(cache_size == 0 || is_mp == 0 ||
673 lcore_id >= RTE_MAX_LCORE))
676 /* Go straight to ring if put would overflow mem allocated for cache */
677 if (unlikely(n > RTE_MEMPOOL_CACHE_MAX_SIZE))
680 cache = &mp->local_cache[lcore_id];
681 cache_objs = &cache->objs[cache->len];
684 * The cache follows the following algorithm
685 * 1. Add the objects to the cache
686 * 2. Anything greater than the cache min value (if it crosses the
687 * cache flush threshold) is flushed to the ring.
690 /* Add elements back into the cache */
691 for (index = 0; index < n; ++index, obj_table++)
692 cache_objs[index] = *obj_table;
696 if (cache->len >= flushthresh) {
697 rte_ring_mp_enqueue_bulk(mp->ring, &cache->objs[cache_size],
698 cache->len - cache_size);
699 cache->len = cache_size;
706 /* push remaining objects in ring */
707 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
709 if (rte_ring_mp_enqueue_bulk(mp->ring, obj_table, n) < 0)
710 rte_panic("cannot put objects in mempool\n");
713 if (rte_ring_sp_enqueue_bulk(mp->ring, obj_table, n) < 0)
714 rte_panic("cannot put objects in mempool\n");
718 rte_ring_mp_enqueue_bulk(mp->ring, obj_table, n);
720 rte_ring_sp_enqueue_bulk(mp->ring, obj_table, n);
726 * Put several objects back in the mempool (multi-producers safe).
729 * A pointer to the mempool structure.
731 * A pointer to a table of void * pointers (objects).
733 * The number of objects to add in the mempool from the obj_table.
735 static inline void __attribute__((always_inline))
736 rte_mempool_mp_put_bulk(struct rte_mempool *mp, void * const *obj_table,
739 __mempool_check_cookies(mp, obj_table, n, 0);
740 __mempool_put_bulk(mp, obj_table, n, 1);
744 * Put several objects back in the mempool (NOT multi-producers safe).
747 * A pointer to the mempool structure.
749 * A pointer to a table of void * pointers (objects).
751 * The number of objects to add in the mempool from obj_table.
754 rte_mempool_sp_put_bulk(struct rte_mempool *mp, void * const *obj_table,
757 __mempool_check_cookies(mp, obj_table, n, 0);
758 __mempool_put_bulk(mp, obj_table, n, 0);
762 * Put several objects back in the mempool.
764 * This function calls the multi-producer or the single-producer
765 * version depending on the default behavior that was specified at
766 * mempool creation time (see flags).
769 * A pointer to the mempool structure.
771 * A pointer to a table of void * pointers (objects).
773 * The number of objects to add in the mempool from obj_table.
775 static inline void __attribute__((always_inline))
776 rte_mempool_put_bulk(struct rte_mempool *mp, void * const *obj_table,
779 __mempool_check_cookies(mp, obj_table, n, 0);
780 __mempool_put_bulk(mp, obj_table, n, !(mp->flags & MEMPOOL_F_SP_PUT));
784 * Put one object in the mempool (multi-producers safe).
787 * A pointer to the mempool structure.
789 * A pointer to the object to be added.
791 static inline void __attribute__((always_inline))
792 rte_mempool_mp_put(struct rte_mempool *mp, void *obj)
794 rte_mempool_mp_put_bulk(mp, &obj, 1);
798 * Put one object back in the mempool (NOT multi-producers safe).
801 * A pointer to the mempool structure.
803 * A pointer to the object to be added.
805 static inline void __attribute__((always_inline))
806 rte_mempool_sp_put(struct rte_mempool *mp, void *obj)
808 rte_mempool_sp_put_bulk(mp, &obj, 1);
812 * Put one object back in the mempool.
814 * This function calls the multi-producer or the single-producer
815 * version depending on the default behavior that was specified at
816 * mempool creation time (see flags).
819 * A pointer to the mempool structure.
821 * A pointer to the object to be added.
823 static inline void __attribute__((always_inline))
824 rte_mempool_put(struct rte_mempool *mp, void *obj)
826 rte_mempool_put_bulk(mp, &obj, 1);
830 * @internal Get several objects from the mempool; used internally.
832 * A pointer to the mempool structure.
834 * A pointer to a table of void * pointers (objects).
836 * The number of objects to get, must be strictly positive.
838 * Mono-consumer (0) or multi-consumers (1).
840 * - >=0: Success; number of objects supplied.
841 * - <0: Error; code of ring dequeue function.
843 static inline int __attribute__((always_inline))
844 __mempool_get_bulk(struct rte_mempool *mp, void **obj_table,
845 unsigned n, int is_mc)
848 struct rte_mempool_cache *cache;
851 unsigned lcore_id = rte_lcore_id();
852 uint32_t cache_size = mp->cache_size;
854 /* cache is not enabled or single consumer */
855 if (unlikely(cache_size == 0 || is_mc == 0 ||
856 n >= cache_size || lcore_id >= RTE_MAX_LCORE))
859 cache = &mp->local_cache[lcore_id];
860 cache_objs = cache->objs;
862 /* Can this be satisfied from the cache? */
863 if (cache->len < n) {
864 /* No. Backfill the cache first, and then fill from it */
865 uint32_t req = n + (cache_size - cache->len);
867 /* How many do we require i.e. number to fill the cache + the request */
868 ret = rte_ring_mc_dequeue_bulk(mp->ring, &cache->objs[cache->len], req);
869 if (unlikely(ret < 0)) {
871 * In the offchance that we are buffer constrained,
872 * where we are not able to allocate cache + n, go to
873 * the ring directly. If that fails, we are truly out of
882 /* Now fill in the response ... */
883 for (index = 0, len = cache->len - 1; index < n; ++index, len--, obj_table++)
884 *obj_table = cache_objs[len];
888 __MEMPOOL_STAT_ADD(mp, get_success, n);
894 /* get remaining objects from ring */
896 ret = rte_ring_mc_dequeue_bulk(mp->ring, obj_table, n);
898 ret = rte_ring_sc_dequeue_bulk(mp->ring, obj_table, n);
901 __MEMPOOL_STAT_ADD(mp, get_fail, n);
903 __MEMPOOL_STAT_ADD(mp, get_success, n);
909 * Get several objects from the mempool (multi-consumers safe).
911 * If cache is enabled, objects will be retrieved first from cache,
912 * subsequently from the common pool. Note that it can return -ENOENT when
913 * the local cache and common pool are empty, even if cache from other
917 * A pointer to the mempool structure.
919 * A pointer to a table of void * pointers (objects) that will be filled.
921 * The number of objects to get from mempool to obj_table.
923 * - 0: Success; objects taken.
924 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
926 static inline int __attribute__((always_inline))
927 rte_mempool_mc_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned n)
930 ret = __mempool_get_bulk(mp, obj_table, n, 1);
932 __mempool_check_cookies(mp, obj_table, n, 1);
937 * Get several objects from the mempool (NOT multi-consumers safe).
939 * If cache is enabled, objects will be retrieved first from cache,
940 * subsequently from the common pool. Note that it can return -ENOENT when
941 * the local cache and common pool are empty, even if cache from other
945 * A pointer to the mempool structure.
947 * A pointer to a table of void * pointers (objects) that will be filled.
949 * The number of objects to get from the mempool to obj_table.
951 * - 0: Success; objects taken.
952 * - -ENOENT: Not enough entries in the mempool; no object is
955 static inline int __attribute__((always_inline))
956 rte_mempool_sc_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned n)
959 ret = __mempool_get_bulk(mp, obj_table, n, 0);
961 __mempool_check_cookies(mp, obj_table, n, 1);
966 * Get several objects from the mempool.
968 * This function calls the multi-consumers or the single-consumer
969 * version, depending on the default behaviour that was specified at
970 * mempool creation time (see flags).
972 * If cache is enabled, objects will be retrieved first from cache,
973 * subsequently from the common pool. Note that it can return -ENOENT when
974 * the local cache and common pool are empty, even if cache from other
978 * A pointer to the mempool structure.
980 * A pointer to a table of void * pointers (objects) that will be filled.
982 * The number of objects to get from the mempool to obj_table.
984 * - 0: Success; objects taken
985 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
987 static inline int __attribute__((always_inline))
988 rte_mempool_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned n)
991 ret = __mempool_get_bulk(mp, obj_table, n,
992 !(mp->flags & MEMPOOL_F_SC_GET));
994 __mempool_check_cookies(mp, obj_table, n, 1);
999 * Get one object from the mempool (multi-consumers safe).
1001 * If cache is enabled, objects will be retrieved first from cache,
1002 * subsequently from the common pool. Note that it can return -ENOENT when
1003 * the local cache and common pool are empty, even if cache from other
1007 * A pointer to the mempool structure.
1009 * A pointer to a void * pointer (object) that will be filled.
1011 * - 0: Success; objects taken.
1012 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
1014 static inline int __attribute__((always_inline))
1015 rte_mempool_mc_get(struct rte_mempool *mp, void **obj_p)
1017 return rte_mempool_mc_get_bulk(mp, obj_p, 1);
1021 * Get one object from the mempool (NOT multi-consumers safe).
1023 * If cache is enabled, objects will be retrieved first from cache,
1024 * subsequently from the common pool. Note that it can return -ENOENT when
1025 * the local cache and common pool are empty, even if cache from other
1029 * A pointer to the mempool structure.
1031 * A pointer to a void * pointer (object) that will be filled.
1033 * - 0: Success; objects taken.
1034 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
1036 static inline int __attribute__((always_inline))
1037 rte_mempool_sc_get(struct rte_mempool *mp, void **obj_p)
1039 return rte_mempool_sc_get_bulk(mp, obj_p, 1);
1043 * Get one object from the mempool.
1045 * This function calls the multi-consumers or the single-consumer
1046 * version, depending on the default behavior that was specified at
1047 * mempool creation (see flags).
1049 * If cache is enabled, objects will be retrieved first from cache,
1050 * subsequently from the common pool. Note that it can return -ENOENT when
1051 * the local cache and common pool are empty, even if cache from other
1055 * A pointer to the mempool structure.
1057 * A pointer to a void * pointer (object) that will be filled.
1059 * - 0: Success; objects taken.
1060 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
1062 static inline int __attribute__((always_inline))
1063 rte_mempool_get(struct rte_mempool *mp, void **obj_p)
1065 return rte_mempool_get_bulk(mp, obj_p, 1);
1069 * Return the number of entries in the mempool.
1071 * When cache is enabled, this function has to browse the length of
1072 * all lcores, so it should not be used in a data path, but only for
1076 * A pointer to the mempool structure.
1078 * The number of entries in the mempool.
1080 unsigned rte_mempool_count(const struct rte_mempool *mp);
1083 * Return the number of free entries in the mempool ring.
1084 * i.e. how many entries can be freed back to the mempool.
1086 * NOTE: This corresponds to the number of elements *allocated* from the
1087 * memory pool, not the number of elements in the pool itself. To count
1088 * the number elements currently available in the pool, use "rte_mempool_count"
1090 * When cache is enabled, this function has to browse the length of
1091 * all lcores, so it should not be used in a data path, but only for
1095 * A pointer to the mempool structure.
1097 * The number of free entries in the mempool.
1099 static inline unsigned
1100 rte_mempool_free_count(const struct rte_mempool *mp)
1102 return mp->size - rte_mempool_count(mp);
1106 * Test if the mempool is full.
1108 * When cache is enabled, this function has to browse the length of all
1109 * lcores, so it should not be used in a data path, but only for debug
1113 * A pointer to the mempool structure.
1115 * - 1: The mempool is full.
1116 * - 0: The mempool is not full.
1119 rte_mempool_full(const struct rte_mempool *mp)
1121 return !!(rte_mempool_count(mp) == mp->size);
1125 * Test if the mempool is empty.
1127 * When cache is enabled, this function has to browse the length of all
1128 * lcores, so it should not be used in a data path, but only for debug
1132 * A pointer to the mempool structure.
1134 * - 1: The mempool is empty.
1135 * - 0: The mempool is not empty.
1138 rte_mempool_empty(const struct rte_mempool *mp)
1140 return !!(rte_mempool_count(mp) == 0);
1144 * Return the physical address of elt, which is an element of the pool mp.
1147 * A pointer to the mempool structure.
1149 * A pointer (virtual address) to the element of the pool.
1151 * The physical address of the elt element.
1153 static inline phys_addr_t
1154 rte_mempool_virt2phy(const struct rte_mempool *mp, const void *elt)
1156 if (rte_eal_has_hugepages()) {
1159 off = (const char *)elt - (const char *)mp->elt_va_start;
1160 return mp->elt_pa[off >> mp->pg_shift] + (off & mp->pg_mask);
1163 * If huge pages are disabled, we cannot assume the
1164 * memory region to be physically contiguous.
1165 * Lookup for each element.
1167 return rte_mem_virt2phy(elt);
1172 * Check the consistency of mempool objects.
1174 * Verify the coherency of fields in the mempool structure. Also check
1175 * that the cookies of mempool objects (even the ones that are not
1176 * present in pool) have a correct value. If not, a panic will occur.
1179 * A pointer to the mempool structure.
1181 void rte_mempool_audit(const struct rte_mempool *mp);
1184 * Return a pointer to the private data in an mempool structure.
1187 * A pointer to the mempool structure.
1189 * A pointer to the private data.
1191 static inline void *rte_mempool_get_priv(struct rte_mempool *mp)
1194 MEMPOOL_HEADER_SIZE(mp, mp->pg_num, mp->cache_size);
1198 * Dump the status of all mempools on the console
1201 * A pointer to a file for output
1203 void rte_mempool_list_dump(FILE *f);
1206 * Search a mempool from its name
1209 * The name of the mempool.
1211 * The pointer to the mempool matching the name, or NULL if not found.
1213 * with rte_errno set appropriately. Possible rte_errno values include:
1214 * - ENOENT - required entry not available to return.
1217 struct rte_mempool *rte_mempool_lookup(const char *name);
1220 * Get the header, trailer and total size of a mempool element.
1222 * Given a desired size of the mempool element and mempool flags,
1223 * calculates header, trailer, body and total sizes of the mempool object.
1226 * The size of each element, without header and trailer.
1228 * The flags used for the mempool creation.
1229 * Consult rte_mempool_create() for more information about possible values.
1230 * The size of each element.
1232 * The calculated detailed size the mempool object. May be NULL.
1234 * Total size of the mempool object.
1236 uint32_t rte_mempool_calc_obj_size(uint32_t elt_size, uint32_t flags,
1237 struct rte_mempool_objsz *sz);
1240 * Get the size of memory required to store mempool elements.
1242 * Calculate the maximum amount of memory required to store given number
1243 * of objects. Assume that the memory buffer will be aligned at page
1246 * Note that if object size is bigger then page size, then it assumes
1247 * that pages are grouped in subsets of physically continuous pages big
1248 * enough to store at least one object.
1251 * Number of elements.
1252 * @param total_elt_sz
1253 * The size of each element, including header and trailer, as returned
1254 * by rte_mempool_calc_obj_size().
1256 * LOG2 of the physical pages size.
1258 * Required memory size aligned at page boundary.
1260 size_t rte_mempool_xmem_size(uint32_t elt_num, size_t total_elt_sz,
1264 * Get the size of memory required to store mempool elements.
1266 * Calculate how much memory would be actually required with the given
1267 * memory footprint to store required number of objects.
1270 * Virtual address of the externally allocated memory buffer.
1271 * Will be used to store mempool objects.
1273 * Number of elements.
1274 * @param total_elt_sz
1275 * The size of each element, including header and trailer, as returned
1276 * by rte_mempool_calc_obj_size().
1278 * Array of physical addresses of the pages that comprises given memory
1281 * Number of elements in the paddr array.
1283 * LOG2 of the physical pages size.
1285 * On success, the number of bytes needed to store given number of
1286 * objects, aligned to the given page size. If the provided memory
1287 * buffer is too small, return a negative value whose absolute value
1288 * is the actual number of elements that can be stored in that buffer.
1290 ssize_t rte_mempool_xmem_usage(void *vaddr, uint32_t elt_num,
1291 size_t total_elt_sz, const phys_addr_t paddr[], uint32_t pg_num,
1295 * Walk list of all memory pools
1300 * Argument passed to iterator
1302 void rte_mempool_walk(void (*func)(const struct rte_mempool *, void *arg),
1309 #endif /* _RTE_MEMPOOL_H_ */