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.
35 #ifndef _RTE_MEMPOOL_H_
36 #define _RTE_MEMPOOL_H_
42 * A memory pool is an allocator of fixed-size object. It is
43 * identified by its name, and uses a ring to store free objects. It
44 * provides some other optional services, like a per-core object
45 * cache, and an alignment helper to ensure that objects are padded
46 * to spread them equally on all RAM channels, ranks, and so on.
48 * Objects owned by a mempool should never be added in another
49 * mempool. When an object is freed using rte_mempool_put() or
50 * equivalent, the object data is not modified; the user can save some
51 * meta-data in the object data and retrieve them when allocating a
54 * Note: the mempool implementation is not preemptable. A lcore must
55 * not be interrupted by another task that uses the same mempool
56 * (because it uses a ring which is not preemptable). Also, mempool
57 * functions must not be used outside the DPDK environment: for
58 * example, in linuxapp environment, a thread that is not created by
59 * the EAL must not use mempools. This is due to the per-lcore cache
60 * that won't work as rte_lcore_id() will not return a correct value.
68 #include <sys/queue.h>
71 #include <rte_debug.h>
72 #include <rte_lcore.h>
73 #include <rte_memory.h>
74 #include <rte_branch_prediction.h>
81 #define RTE_MEMPOOL_HEADER_COOKIE1 0xbadbadbadadd2e55ULL /**< Header cookie. */
82 #define RTE_MEMPOOL_HEADER_COOKIE2 0xf2eef2eedadd2e55ULL /**< Header cookie. */
83 #define RTE_MEMPOOL_TRAILER_COOKIE 0xadd2e55badbadbadULL /**< Trailer cookie.*/
85 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
87 * A structure that stores the mempool statistics (per-lcore).
89 struct rte_mempool_debug_stats {
90 uint64_t put_bulk; /**< Number of puts. */
91 uint64_t put_objs; /**< Number of objects successfully put. */
92 uint64_t get_success_bulk; /**< Successful allocation number. */
93 uint64_t get_success_objs; /**< Objects successfully allocated. */
94 uint64_t get_fail_bulk; /**< Failed allocation number. */
95 uint64_t get_fail_objs; /**< Objects that failed to be allocated. */
96 } __rte_cache_aligned;
100 * A structure that stores a per-core object cache.
102 struct rte_mempool_cache {
103 unsigned len; /**< Cache len */
105 * Cache is allocated to this size to allow it to overflow in certain
106 * cases to avoid needless emptying of cache.
108 void *objs[RTE_MEMPOOL_CACHE_MAX_SIZE * 3]; /**< Cache objects */
109 } __rte_cache_aligned;
112 * A structure that stores the size of mempool elements.
114 struct rte_mempool_objsz {
115 uint32_t elt_size; /**< Size of an element. */
116 uint32_t header_size; /**< Size of header (before elt). */
117 uint32_t trailer_size; /**< Size of trailer (after elt). */
119 /**< Total size of an object (header + elt + trailer). */
122 #define RTE_MEMPOOL_NAMESIZE 32 /**< Maximum length of a memory pool. */
123 #define RTE_MEMPOOL_MZ_PREFIX "MP_"
126 #define RTE_MEMPOOL_MZ_FORMAT RTE_MEMPOOL_MZ_PREFIX "%s"
128 #ifdef RTE_LIBRTE_XEN_DOM0
130 /* "<name>_MP_elt" */
131 #define RTE_MEMPOOL_OBJ_NAME "%s_" RTE_MEMPOOL_MZ_PREFIX "elt"
135 #define RTE_MEMPOOL_OBJ_NAME RTE_MEMPOOL_MZ_FORMAT
137 #endif /* RTE_LIBRTE_XEN_DOM0 */
139 #define MEMPOOL_PG_SHIFT_MAX (sizeof(uintptr_t) * CHAR_BIT - 1)
141 /** Mempool over one chunk of physically continuous memory */
142 #define MEMPOOL_PG_NUM_DEFAULT 1
144 #ifndef RTE_MEMPOOL_ALIGN
145 #define RTE_MEMPOOL_ALIGN RTE_CACHE_LINE_SIZE
148 #define RTE_MEMPOOL_ALIGN_MASK (RTE_MEMPOOL_ALIGN - 1)
151 * Mempool object header structure
153 * Each object stored in mempools are prefixed by this header structure,
154 * it allows to retrieve the mempool pointer from the object and to
155 * iterate on all objects attached to a mempool. When debug is enabled,
156 * a cookie is also added in this structure preventing corruptions and
159 struct rte_mempool_objhdr {
160 STAILQ_ENTRY(rte_mempool_objhdr) next; /**< Next in list. */
161 struct rte_mempool *mp; /**< The mempool owning the object. */
162 phys_addr_t physaddr; /**< Physical address of the object. */
163 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
164 uint64_t cookie; /**< Debug cookie. */
169 * A list of object headers type
171 STAILQ_HEAD(rte_mempool_objhdr_list, rte_mempool_objhdr);
174 * Mempool object trailer structure
176 * In debug mode, each object stored in mempools are suffixed by this
177 * trailer structure containing a cookie preventing memory corruptions.
179 struct rte_mempool_objtlr {
180 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
181 uint64_t cookie; /**< Debug cookie. */
186 * A list of memory where objects are stored
188 STAILQ_HEAD(rte_mempool_memhdr_list, rte_mempool_memhdr);
191 * Mempool objects memory header structure
193 * The memory chunks where objects are stored. Each chunk is virtually
194 * and physically contiguous.
196 struct rte_mempool_memhdr {
197 STAILQ_ENTRY(rte_mempool_memhdr) next; /**< Next in list. */
198 struct rte_mempool *mp; /**< The mempool owning the chunk */
199 void *addr; /**< Virtual address of the chunk */
200 phys_addr_t phys_addr; /**< Physical address of the chunk */
201 size_t len; /**< length of the chunk */
205 * The RTE mempool structure.
208 char name[RTE_MEMPOOL_NAMESIZE]; /**< Name of mempool. */
209 struct rte_ring *ring; /**< Ring to store objects. */
210 phys_addr_t phys_addr; /**< Phys. addr. of mempool struct. */
211 int flags; /**< Flags of the mempool. */
212 int socket_id; /**< Socket id passed at mempool creation. */
213 uint32_t size; /**< Max size of the mempool. */
214 uint32_t cache_size; /**< Size of per-lcore local cache. */
215 uint32_t cache_flushthresh;
216 /**< Threshold before we flush excess elements. */
218 uint32_t elt_size; /**< Size of an element. */
219 uint32_t header_size; /**< Size of header (before elt). */
220 uint32_t trailer_size; /**< Size of trailer (after elt). */
222 unsigned private_data_size; /**< Size of private data. */
224 struct rte_mempool_cache *local_cache; /**< Per-lcore local cache */
226 uint32_t populated_size; /**< Number of populated objects. */
227 struct rte_mempool_objhdr_list elt_list; /**< List of objects in pool */
228 uint32_t nb_mem_chunks; /**< Number of memory chunks */
229 struct rte_mempool_memhdr_list mem_list; /**< List of memory chunks */
231 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
232 /** Per-lcore statistics. */
233 struct rte_mempool_debug_stats stats[RTE_MAX_LCORE];
235 } __rte_cache_aligned;
237 #define MEMPOOL_F_NO_SPREAD 0x0001 /**< Do not spread among memory channels. */
238 #define MEMPOOL_F_NO_CACHE_ALIGN 0x0002 /**< Do not align objs on cache lines.*/
239 #define MEMPOOL_F_SP_PUT 0x0004 /**< Default put is "single-producer".*/
240 #define MEMPOOL_F_SC_GET 0x0008 /**< Default get is "single-consumer".*/
243 * @internal When debug is enabled, store some statistics.
246 * Pointer to the memory pool.
248 * Name of the statistics field to increment in the memory pool.
250 * Number to add to the object-oriented statistics.
252 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
253 #define __MEMPOOL_STAT_ADD(mp, name, n) do { \
254 unsigned __lcore_id = rte_lcore_id(); \
255 if (__lcore_id < RTE_MAX_LCORE) { \
256 mp->stats[__lcore_id].name##_objs += n; \
257 mp->stats[__lcore_id].name##_bulk += 1; \
261 #define __MEMPOOL_STAT_ADD(mp, name, n) do {} while(0)
265 * Calculate the size of the mempool header.
268 * Pointer to the memory pool.
270 * Size of the per-lcore cache.
272 #define MEMPOOL_HEADER_SIZE(mp, cs) \
273 (sizeof(*(mp)) + (((cs) == 0) ? 0 : \
274 (sizeof(struct rte_mempool_cache) * RTE_MAX_LCORE)))
276 /* return the header of a mempool object (internal) */
277 static inline struct rte_mempool_objhdr *__mempool_get_header(void *obj)
279 return (struct rte_mempool_objhdr *)RTE_PTR_SUB(obj,
280 sizeof(struct rte_mempool_objhdr));
284 * Return a pointer to the mempool owning this object.
287 * An object that is owned by a pool. If this is not the case,
288 * the behavior is undefined.
290 * A pointer to the mempool structure.
292 static inline struct rte_mempool *rte_mempool_from_obj(void *obj)
294 struct rte_mempool_objhdr *hdr = __mempool_get_header(obj);
298 /* return the trailer of a mempool object (internal) */
299 static inline struct rte_mempool_objtlr *__mempool_get_trailer(void *obj)
301 struct rte_mempool *mp = rte_mempool_from_obj(obj);
302 return (struct rte_mempool_objtlr *)RTE_PTR_ADD(obj, mp->elt_size);
306 * @internal Check and update cookies or panic.
309 * Pointer to the memory pool.
310 * @param obj_table_const
311 * Pointer to a table of void * pointers (objects).
313 * Index of object in object table.
315 * - 0: object is supposed to be allocated, mark it as free
316 * - 1: object is supposed to be free, mark it as allocated
317 * - 2: just check that cookie is valid (free or allocated)
319 void rte_mempool_check_cookies(const struct rte_mempool *mp,
320 void * const *obj_table_const, unsigned n, int free);
322 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
323 #define __mempool_check_cookies(mp, obj_table_const, n, free) \
324 rte_mempool_check_cookies(mp, obj_table_const, n, free)
326 #define __mempool_check_cookies(mp, obj_table_const, n, free) do {} while(0)
327 #endif /* RTE_LIBRTE_MEMPOOL_DEBUG */
330 * An object callback function for mempool.
332 * Used by rte_mempool_create() and rte_mempool_obj_iter().
334 typedef void (rte_mempool_obj_cb_t)(struct rte_mempool *mp,
335 void *opaque, void *obj, unsigned obj_idx);
336 typedef rte_mempool_obj_cb_t rte_mempool_obj_ctor_t; /* compat */
339 * A memory callback function for mempool.
341 * Used by rte_mempool_mem_iter().
343 typedef void (rte_mempool_mem_cb_t)(struct rte_mempool *mp,
344 void *opaque, struct rte_mempool_memhdr *memhdr,
348 * A mempool constructor callback function.
350 * Arguments are the mempool and the opaque pointer given by the user in
351 * rte_mempool_create().
353 typedef void (rte_mempool_ctor_t)(struct rte_mempool *, void *);
356 * Create a new mempool named *name* in memory.
358 * This function uses ``memzone_reserve()`` to allocate memory. The
359 * pool contains n elements of elt_size. Its size is set to n.
360 * All elements of the mempool are allocated together with the mempool header,
361 * in one physically continuous chunk of memory.
364 * The name of the mempool.
366 * The number of elements in the mempool. The optimum size (in terms of
367 * memory usage) for a mempool is when n is a power of two minus one:
370 * The size of each element.
372 * If cache_size is non-zero, the rte_mempool library will try to
373 * limit the accesses to the common lockless pool, by maintaining a
374 * per-lcore object cache. This argument must be lower or equal to
375 * CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE and n / 1.5. It is advised to choose
376 * cache_size to have "n modulo cache_size == 0": if this is
377 * not the case, some elements will always stay in the pool and will
378 * never be used. The access to the per-lcore table is of course
379 * faster than the multi-producer/consumer pool. The cache can be
380 * disabled if the cache_size argument is set to 0; it can be useful to
381 * avoid losing objects in cache. Note that even if not used, the
382 * memory space for cache is always reserved in a mempool structure,
383 * except if CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE is set to 0.
384 * @param private_data_size
385 * The size of the private data appended after the mempool
386 * structure. This is useful for storing some private data after the
387 * mempool structure, as is done for rte_mbuf_pool for example.
389 * A function pointer that is called for initialization of the pool,
390 * before object initialization. The user can initialize the private
391 * data in this function if needed. This parameter can be NULL if
394 * An opaque pointer to data that can be used in the mempool
395 * constructor function.
397 * A function pointer that is called for each object at
398 * initialization of the pool. The user can set some meta data in
399 * objects if needed. This parameter can be NULL if not needed.
400 * The obj_init() function takes the mempool pointer, the init_arg,
401 * the object pointer and the object number as parameters.
402 * @param obj_init_arg
403 * An opaque pointer to data that can be used as an argument for
404 * each call to the object constructor function.
406 * The *socket_id* argument is the socket identifier in the case of
407 * NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
408 * constraint for the reserved zone.
410 * The *flags* arguments is an OR of following flags:
411 * - MEMPOOL_F_NO_SPREAD: By default, objects addresses are spread
412 * between channels in RAM: the pool allocator will add padding
413 * between objects depending on the hardware configuration. See
414 * Memory alignment constraints for details. If this flag is set,
415 * the allocator will just align them to a cache line.
416 * - MEMPOOL_F_NO_CACHE_ALIGN: By default, the returned objects are
417 * cache-aligned. This flag removes this constraint, and no
418 * padding will be present between objects. This flag implies
419 * MEMPOOL_F_NO_SPREAD.
420 * - MEMPOOL_F_SP_PUT: If this flag is set, the default behavior
421 * when using rte_mempool_put() or rte_mempool_put_bulk() is
422 * "single-producer". Otherwise, it is "multi-producers".
423 * - MEMPOOL_F_SC_GET: If this flag is set, the default behavior
424 * when using rte_mempool_get() or rte_mempool_get_bulk() is
425 * "single-consumer". Otherwise, it is "multi-consumers".
427 * The pointer to the new allocated mempool, on success. NULL on error
428 * with rte_errno set appropriately. Possible rte_errno values include:
429 * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
430 * - E_RTE_SECONDARY - function was called from a secondary process instance
431 * - EINVAL - cache size provided is too large
432 * - ENOSPC - the maximum number of memzones has already been allocated
433 * - EEXIST - a memzone with the same name already exists
434 * - ENOMEM - no appropriate memory area found in which to create memzone
437 rte_mempool_create(const char *name, unsigned n, unsigned elt_size,
438 unsigned cache_size, unsigned private_data_size,
439 rte_mempool_ctor_t *mp_init, void *mp_init_arg,
440 rte_mempool_obj_cb_t *obj_init, void *obj_init_arg,
441 int socket_id, unsigned flags);
444 * Create a new mempool named *name* in memory.
446 * The pool contains n elements of elt_size. Its size is set to n.
447 * This function uses ``memzone_reserve()`` to allocate the mempool header
448 * (and the objects if vaddr is NULL).
449 * Depending on the input parameters, mempool elements can be either allocated
450 * together with the mempool header, or an externally provided memory buffer
451 * could be used to store mempool objects. In later case, that external
452 * memory buffer can consist of set of disjoint physical pages.
455 * The name of the mempool.
457 * The number of elements in the mempool. The optimum size (in terms of
458 * memory usage) for a mempool is when n is a power of two minus one:
461 * The size of each element.
463 * Size of the cache. See rte_mempool_create() for details.
464 * @param private_data_size
465 * The size of the private data appended after the mempool
466 * structure. This is useful for storing some private data after the
467 * mempool structure, as is done for rte_mbuf_pool for example.
469 * A function pointer that is called for initialization of the pool,
470 * before object initialization. The user can initialize the private
471 * data in this function if needed. This parameter can be NULL if
474 * An opaque pointer to data that can be used in the mempool
475 * constructor function.
477 * A function called for each object at initialization of the pool.
478 * See rte_mempool_create() for details.
479 * @param obj_init_arg
480 * An opaque pointer passed to the object constructor function.
482 * The *socket_id* argument is the socket identifier in the case of
483 * NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
484 * constraint for the reserved zone.
486 * Flags controlling the behavior of the mempool. See
487 * rte_mempool_create() for details.
489 * Virtual address of the externally allocated memory buffer.
490 * Will be used to store mempool objects.
492 * Array of physical addresses of the pages that comprises given memory
495 * Number of elements in the paddr array.
497 * LOG2 of the physical pages size.
499 * The pointer to the new allocated mempool, on success. NULL on error
500 * with rte_errno set appropriately. See rte_mempool_create() for details.
503 rte_mempool_xmem_create(const char *name, unsigned n, unsigned elt_size,
504 unsigned cache_size, unsigned private_data_size,
505 rte_mempool_ctor_t *mp_init, void *mp_init_arg,
506 rte_mempool_obj_cb_t *obj_init, void *obj_init_arg,
507 int socket_id, unsigned flags, void *vaddr,
508 const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift);
511 * Create a new mempool named *name* in memory on Xen Dom0.
513 * This function uses ``rte_mempool_xmem_create()`` to allocate memory. The
514 * pool contains n elements of elt_size. Its size is set to n.
515 * All elements of the mempool are allocated together with the mempool header,
516 * and memory buffer can consist of set of disjoint physical pages.
519 * The name of the mempool.
521 * The number of elements in the mempool. The optimum size (in terms of
522 * memory usage) for a mempool is when n is a power of two minus one:
525 * The size of each element.
527 * If cache_size is non-zero, the rte_mempool library will try to
528 * limit the accesses to the common lockless pool, by maintaining a
529 * per-lcore object cache. This argument must be lower or equal to
530 * CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE. It is advised to choose
531 * cache_size to have "n modulo cache_size == 0": if this is
532 * not the case, some elements will always stay in the pool and will
533 * never be used. The access to the per-lcore table is of course
534 * faster than the multi-producer/consumer pool. The cache can be
535 * disabled if the cache_size argument is set to 0; it can be useful to
536 * avoid losing objects in cache. Note that even if not used, the
537 * memory space for cache is always reserved in a mempool structure,
538 * except if CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE is set to 0.
539 * @param private_data_size
540 * The size of the private data appended after the mempool
541 * structure. This is useful for storing some private data after the
542 * mempool structure, as is done for rte_mbuf_pool for example.
544 * A function pointer that is called for initialization of the pool,
545 * before object initialization. The user can initialize the private
546 * data in this function if needed. This parameter can be NULL if
549 * An opaque pointer to data that can be used in the mempool
550 * constructor function.
552 * A function pointer that is called for each object at
553 * initialization of the pool. The user can set some meta data in
554 * objects if needed. This parameter can be NULL if not needed.
555 * The obj_init() function takes the mempool pointer, the init_arg,
556 * the object pointer and the object number as parameters.
557 * @param obj_init_arg
558 * An opaque pointer to data that can be used as an argument for
559 * each call to the object constructor function.
561 * The *socket_id* argument is the socket identifier in the case of
562 * NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
563 * constraint for the reserved zone.
565 * The *flags* arguments is an OR of following flags:
566 * - MEMPOOL_F_NO_SPREAD: By default, objects addresses are spread
567 * between channels in RAM: the pool allocator will add padding
568 * between objects depending on the hardware configuration. See
569 * Memory alignment constraints for details. If this flag is set,
570 * the allocator will just align them to a cache line.
571 * - MEMPOOL_F_NO_CACHE_ALIGN: By default, the returned objects are
572 * cache-aligned. This flag removes this constraint, and no
573 * padding will be present between objects. This flag implies
574 * MEMPOOL_F_NO_SPREAD.
575 * - MEMPOOL_F_SP_PUT: If this flag is set, the default behavior
576 * when using rte_mempool_put() or rte_mempool_put_bulk() is
577 * "single-producer". Otherwise, it is "multi-producers".
578 * - MEMPOOL_F_SC_GET: If this flag is set, the default behavior
579 * when using rte_mempool_get() or rte_mempool_get_bulk() is
580 * "single-consumer". Otherwise, it is "multi-consumers".
582 * The pointer to the new allocated mempool, on success. NULL on error
583 * with rte_errno set appropriately. Possible rte_errno values include:
584 * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
585 * - E_RTE_SECONDARY - function was called from a secondary process instance
586 * - EINVAL - cache size provided is too large
587 * - ENOSPC - the maximum number of memzones has already been allocated
588 * - EEXIST - a memzone with the same name already exists
589 * - ENOMEM - no appropriate memory area found in which to create memzone
592 rte_dom0_mempool_create(const char *name, unsigned n, unsigned elt_size,
593 unsigned cache_size, unsigned private_data_size,
594 rte_mempool_ctor_t *mp_init, void *mp_init_arg,
595 rte_mempool_obj_cb_t *obj_init, void *obj_init_arg,
596 int socket_id, unsigned flags);
600 * Call a function for each mempool element
602 * Iterate across all objects attached to a rte_mempool and call the
603 * callback function on it.
606 * A pointer to an initialized mempool.
608 * A function pointer that is called for each object.
610 * An opaque pointer passed to the callback function.
612 * Number of objects iterated.
614 uint32_t rte_mempool_obj_iter(struct rte_mempool *mp,
615 rte_mempool_obj_cb_t *obj_cb, void *obj_cb_arg);
618 * Call a function for each mempool memory chunk
620 * Iterate across all memory chunks attached to a rte_mempool and call
621 * the callback function on it.
624 * A pointer to an initialized mempool.
626 * A function pointer that is called for each memory chunk.
628 * An opaque pointer passed to the callback function.
630 * Number of memory chunks iterated.
632 uint32_t rte_mempool_mem_iter(struct rte_mempool *mp,
633 rte_mempool_mem_cb_t *mem_cb, void *mem_cb_arg);
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, 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(__rte_unused const struct rte_mempool *mp, const void *elt)
1156 if (rte_eal_has_hugepages()) {
1157 const struct rte_mempool_objhdr *hdr;
1158 hdr = (const struct rte_mempool_objhdr *)RTE_PTR_SUB(elt,
1160 return hdr->physaddr;
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(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->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)(struct rte_mempool *, void *arg),
1309 #endif /* _RTE_MEMPOOL_H_ */