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>
70 #include <rte_spinlock.h>
72 #include <rte_debug.h>
73 #include <rte_lcore.h>
74 #include <rte_memory.h>
75 #include <rte_branch_prediction.h>
77 #include <rte_memcpy.h>
83 #define RTE_MEMPOOL_HEADER_COOKIE1 0xbadbadbadadd2e55ULL /**< Header cookie. */
84 #define RTE_MEMPOOL_HEADER_COOKIE2 0xf2eef2eedadd2e55ULL /**< Header cookie. */
85 #define RTE_MEMPOOL_TRAILER_COOKIE 0xadd2e55badbadbadULL /**< Trailer cookie.*/
87 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
89 * A structure that stores the mempool statistics (per-lcore).
91 struct rte_mempool_debug_stats {
92 uint64_t put_bulk; /**< Number of puts. */
93 uint64_t put_objs; /**< Number of objects successfully put. */
94 uint64_t get_success_bulk; /**< Successful allocation number. */
95 uint64_t get_success_objs; /**< Objects successfully allocated. */
96 uint64_t get_fail_bulk; /**< Failed allocation number. */
97 uint64_t get_fail_objs; /**< Objects that failed to be allocated. */
98 } __rte_cache_aligned;
102 * A structure that stores a per-core object cache.
104 struct rte_mempool_cache {
105 uint32_t size; /**< Size of the cache */
106 uint32_t flushthresh; /**< Threshold before we flush excess elements */
107 uint32_t len; /**< Current cache count */
109 * Cache is allocated to this size to allow it to overflow in certain
110 * cases to avoid needless emptying of cache.
112 void *objs[RTE_MEMPOOL_CACHE_MAX_SIZE * 3]; /**< Cache objects */
113 } __rte_cache_aligned;
116 * A structure that stores the size of mempool elements.
118 struct rte_mempool_objsz {
119 uint32_t elt_size; /**< Size of an element. */
120 uint32_t header_size; /**< Size of header (before elt). */
121 uint32_t trailer_size; /**< Size of trailer (after elt). */
123 /**< Total size of an object (header + elt + trailer). */
126 #define RTE_MEMPOOL_NAMESIZE 32 /**< Maximum length of a memory pool. */
127 #define RTE_MEMPOOL_MZ_PREFIX "MP_"
130 #define RTE_MEMPOOL_MZ_FORMAT RTE_MEMPOOL_MZ_PREFIX "%s"
132 #define MEMPOOL_PG_SHIFT_MAX (sizeof(uintptr_t) * CHAR_BIT - 1)
134 /** Mempool over one chunk of physically continuous memory */
135 #define MEMPOOL_PG_NUM_DEFAULT 1
137 #ifndef RTE_MEMPOOL_ALIGN
138 #define RTE_MEMPOOL_ALIGN RTE_CACHE_LINE_SIZE
141 #define RTE_MEMPOOL_ALIGN_MASK (RTE_MEMPOOL_ALIGN - 1)
144 * Mempool object header structure
146 * Each object stored in mempools are prefixed by this header structure,
147 * it allows to retrieve the mempool pointer from the object and to
148 * iterate on all objects attached to a mempool. When debug is enabled,
149 * a cookie is also added in this structure preventing corruptions and
152 struct rte_mempool_objhdr {
153 STAILQ_ENTRY(rte_mempool_objhdr) next; /**< Next in list. */
154 struct rte_mempool *mp; /**< The mempool owning the object. */
155 phys_addr_t physaddr; /**< Physical address of the object. */
156 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
157 uint64_t cookie; /**< Debug cookie. */
162 * A list of object headers type
164 STAILQ_HEAD(rte_mempool_objhdr_list, rte_mempool_objhdr);
166 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
169 * Mempool object trailer structure
171 * In debug mode, each object stored in mempools are suffixed by this
172 * trailer structure containing a cookie preventing memory corruptions.
174 struct rte_mempool_objtlr {
175 uint64_t cookie; /**< Debug cookie. */
181 * A list of memory where objects are stored
183 STAILQ_HEAD(rte_mempool_memhdr_list, rte_mempool_memhdr);
186 * Callback used to free a memory chunk
188 typedef void (rte_mempool_memchunk_free_cb_t)(struct rte_mempool_memhdr *memhdr,
192 * Mempool objects memory header structure
194 * The memory chunks where objects are stored. Each chunk is virtually
195 * and physically contiguous.
197 struct rte_mempool_memhdr {
198 STAILQ_ENTRY(rte_mempool_memhdr) next; /**< Next in list. */
199 struct rte_mempool *mp; /**< The mempool owning the chunk */
200 void *addr; /**< Virtual address of the chunk */
201 phys_addr_t phys_addr; /**< Physical address of the chunk */
202 size_t len; /**< length of the chunk */
203 rte_mempool_memchunk_free_cb_t *free_cb; /**< Free callback */
204 void *opaque; /**< Argument passed to the free callback */
208 * The RTE mempool structure.
211 char name[RTE_MEMPOOL_NAMESIZE]; /**< Name of mempool. */
213 void *pool_data; /**< Ring or pool to store objects. */
214 uint64_t pool_id; /**< External mempool identifier. */
216 void *pool_config; /**< optional args for ops alloc. */
217 const struct rte_memzone *mz; /**< Memzone where pool is alloc'd. */
218 int flags; /**< Flags of the mempool. */
219 int socket_id; /**< Socket id passed at create. */
220 uint32_t size; /**< Max size of the mempool. */
222 /**< Size of per-lcore default local cache. */
224 uint32_t elt_size; /**< Size of an element. */
225 uint32_t header_size; /**< Size of header (before elt). */
226 uint32_t trailer_size; /**< Size of trailer (after elt). */
228 unsigned private_data_size; /**< Size of private data. */
230 * Index into rte_mempool_ops_table array of mempool ops
231 * structs, which contain callback function pointers.
232 * We're using an index here rather than pointers to the callbacks
233 * to facilitate any secondary processes that may want to use
238 struct rte_mempool_cache *local_cache; /**< Per-lcore local cache */
240 uint32_t populated_size; /**< Number of populated objects. */
241 struct rte_mempool_objhdr_list elt_list; /**< List of objects in pool */
242 uint32_t nb_mem_chunks; /**< Number of memory chunks */
243 struct rte_mempool_memhdr_list mem_list; /**< List of memory chunks */
245 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
246 /** Per-lcore statistics. */
247 struct rte_mempool_debug_stats stats[RTE_MAX_LCORE];
249 } __rte_cache_aligned;
251 #define MEMPOOL_F_NO_SPREAD 0x0001 /**< Do not spread among memory channels. */
252 #define MEMPOOL_F_NO_CACHE_ALIGN 0x0002 /**< Do not align objs on cache lines.*/
253 #define MEMPOOL_F_SP_PUT 0x0004 /**< Default put is "single-producer".*/
254 #define MEMPOOL_F_SC_GET 0x0008 /**< Default get is "single-consumer".*/
255 #define MEMPOOL_F_POOL_CREATED 0x0010 /**< Internal: pool is created. */
256 #define MEMPOOL_F_NO_PHYS_CONTIG 0x0020 /**< Don't need physically contiguous objs. */
259 * @internal When debug is enabled, store some statistics.
262 * Pointer to the memory pool.
264 * Name of the statistics field to increment in the memory pool.
266 * Number to add to the object-oriented statistics.
268 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
269 #define __MEMPOOL_STAT_ADD(mp, name, n) do { \
270 unsigned __lcore_id = rte_lcore_id(); \
271 if (__lcore_id < RTE_MAX_LCORE) { \
272 mp->stats[__lcore_id].name##_objs += n; \
273 mp->stats[__lcore_id].name##_bulk += 1; \
277 #define __MEMPOOL_STAT_ADD(mp, name, n) do {} while(0)
281 * Calculate the size of the mempool header.
284 * Pointer to the memory pool.
286 * Size of the per-lcore cache.
288 #define MEMPOOL_HEADER_SIZE(mp, cs) \
289 (sizeof(*(mp)) + (((cs) == 0) ? 0 : \
290 (sizeof(struct rte_mempool_cache) * RTE_MAX_LCORE)))
292 /* return the header of a mempool object (internal) */
293 static inline struct rte_mempool_objhdr *__mempool_get_header(void *obj)
295 return (struct rte_mempool_objhdr *)RTE_PTR_SUB(obj,
296 sizeof(struct rte_mempool_objhdr));
300 * Return a pointer to the mempool owning this object.
303 * An object that is owned by a pool. If this is not the case,
304 * the behavior is undefined.
306 * A pointer to the mempool structure.
308 static inline struct rte_mempool *rte_mempool_from_obj(void *obj)
310 struct rte_mempool_objhdr *hdr = __mempool_get_header(obj);
314 /* return the trailer of a mempool object (internal) */
315 static inline struct rte_mempool_objtlr *__mempool_get_trailer(void *obj)
317 struct rte_mempool *mp = rte_mempool_from_obj(obj);
318 return (struct rte_mempool_objtlr *)RTE_PTR_ADD(obj, mp->elt_size);
322 * @internal Check and update cookies or panic.
325 * Pointer to the memory pool.
326 * @param obj_table_const
327 * Pointer to a table of void * pointers (objects).
329 * Index of object in object table.
331 * - 0: object is supposed to be allocated, mark it as free
332 * - 1: object is supposed to be free, mark it as allocated
333 * - 2: just check that cookie is valid (free or allocated)
335 void rte_mempool_check_cookies(const struct rte_mempool *mp,
336 void * const *obj_table_const, unsigned n, int free);
338 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
339 #define __mempool_check_cookies(mp, obj_table_const, n, free) \
340 rte_mempool_check_cookies(mp, obj_table_const, n, free)
342 #define __mempool_check_cookies(mp, obj_table_const, n, free) do {} while(0)
343 #endif /* RTE_LIBRTE_MEMPOOL_DEBUG */
345 #define RTE_MEMPOOL_OPS_NAMESIZE 32 /**< Max length of ops struct name. */
348 * Prototype for implementation specific data provisioning function.
350 * The function should provide the implementation specific memory for
351 * for use by the other mempool ops functions in a given mempool ops struct.
352 * E.g. the default ops provides an instance of the rte_ring for this purpose.
353 * it will most likely point to a different type of data structure, and
354 * will be transparent to the application programmer.
355 * This function should set mp->pool_data.
357 typedef int (*rte_mempool_alloc_t)(struct rte_mempool *mp);
360 * Free the opaque private data pointed to by mp->pool_data pointer.
362 typedef void (*rte_mempool_free_t)(struct rte_mempool *mp);
365 * Enqueue an object into the external pool.
367 typedef int (*rte_mempool_enqueue_t)(struct rte_mempool *mp,
368 void * const *obj_table, unsigned int n);
371 * Dequeue an object from the external pool.
373 typedef int (*rte_mempool_dequeue_t)(struct rte_mempool *mp,
374 void **obj_table, unsigned int n);
377 * Return the number of available objects in the external pool.
379 typedef unsigned (*rte_mempool_get_count)(const struct rte_mempool *mp);
381 /** Structure defining mempool operations structure */
382 struct rte_mempool_ops {
383 char name[RTE_MEMPOOL_OPS_NAMESIZE]; /**< Name of mempool ops struct. */
384 rte_mempool_alloc_t alloc; /**< Allocate private data. */
385 rte_mempool_free_t free; /**< Free the external pool. */
386 rte_mempool_enqueue_t enqueue; /**< Enqueue an object. */
387 rte_mempool_dequeue_t dequeue; /**< Dequeue an object. */
388 rte_mempool_get_count get_count; /**< Get qty of available objs. */
389 } __rte_cache_aligned;
391 #define RTE_MEMPOOL_MAX_OPS_IDX 16 /**< Max registered ops structs */
394 * Structure storing the table of registered ops structs, each of which contain
395 * the function pointers for the mempool ops functions.
396 * Each process has its own storage for this ops struct array so that
397 * the mempools can be shared across primary and secondary processes.
398 * The indices used to access the array are valid across processes, whereas
399 * any function pointers stored directly in the mempool struct would not be.
400 * This results in us simply having "ops_index" in the mempool struct.
402 struct rte_mempool_ops_table {
403 rte_spinlock_t sl; /**< Spinlock for add/delete. */
404 uint32_t num_ops; /**< Number of used ops structs in the table. */
406 * Storage for all possible ops structs.
408 struct rte_mempool_ops ops[RTE_MEMPOOL_MAX_OPS_IDX];
409 } __rte_cache_aligned;
411 /** Array of registered ops structs. */
412 extern struct rte_mempool_ops_table rte_mempool_ops_table;
415 * @internal Get the mempool ops struct from its index.
418 * The index of the ops struct in the ops struct table. It must be a valid
419 * index: (0 <= idx < num_ops).
421 * The pointer to the ops struct in the table.
423 static inline struct rte_mempool_ops *
424 rte_mempool_get_ops(int ops_index)
426 RTE_VERIFY((ops_index >= 0) && (ops_index < RTE_MEMPOOL_MAX_OPS_IDX));
428 return &rte_mempool_ops_table.ops[ops_index];
432 * @internal Wrapper for mempool_ops alloc callback.
435 * Pointer to the memory pool.
437 * - 0: Success; successfully allocated mempool pool_data.
438 * - <0: Error; code of alloc function.
441 rte_mempool_ops_alloc(struct rte_mempool *mp);
444 * @internal Wrapper for mempool_ops dequeue callback.
447 * Pointer to the memory pool.
449 * Pointer to a table of void * pointers (objects).
451 * Number of objects to get.
453 * - 0: Success; got n objects.
454 * - <0: Error; code of dequeue function.
457 rte_mempool_ops_dequeue_bulk(struct rte_mempool *mp,
458 void **obj_table, unsigned n)
460 struct rte_mempool_ops *ops;
462 ops = rte_mempool_get_ops(mp->ops_index);
463 return ops->dequeue(mp, obj_table, n);
467 * @internal wrapper for mempool_ops enqueue callback.
470 * Pointer to the memory pool.
472 * Pointer to a table of void * pointers (objects).
474 * Number of objects to put.
476 * - 0: Success; n objects supplied.
477 * - <0: Error; code of enqueue function.
480 rte_mempool_ops_enqueue_bulk(struct rte_mempool *mp, void * const *obj_table,
483 struct rte_mempool_ops *ops;
485 ops = rte_mempool_get_ops(mp->ops_index);
486 return ops->enqueue(mp, obj_table, n);
490 * @internal wrapper for mempool_ops get_count callback.
493 * Pointer to the memory pool.
495 * The number of available objects in the external pool.
498 rte_mempool_ops_get_count(const struct rte_mempool *mp);
501 * @internal wrapper for mempool_ops free callback.
504 * Pointer to the memory pool.
507 rte_mempool_ops_free(struct rte_mempool *mp);
510 * Set the ops of a mempool.
512 * This can only be done on a mempool that is not populated, i.e. just after
513 * a call to rte_mempool_create_empty().
516 * Pointer to the memory pool.
518 * Name of the ops structure to use for this mempool.
520 * Opaque data that can be passed by the application to the ops functions.
522 * - 0: Success; the mempool is now using the requested ops functions.
523 * - -EINVAL - Invalid ops struct name provided.
524 * - -EEXIST - mempool already has an ops struct assigned.
527 rte_mempool_set_ops_byname(struct rte_mempool *mp, const char *name,
531 * Register mempool operations.
534 * Pointer to an ops structure to register.
536 * - >=0: Success; return the index of the ops struct in the table.
537 * - -EINVAL - some missing callbacks while registering ops struct.
538 * - -ENOSPC - the maximum number of ops structs has been reached.
540 int rte_mempool_register_ops(const struct rte_mempool_ops *ops);
543 * Macro to statically register the ops of a mempool handler.
544 * Note that the rte_mempool_register_ops fails silently here when
545 * more then RTE_MEMPOOL_MAX_OPS_IDX is registered.
547 #define MEMPOOL_REGISTER_OPS(ops) \
548 void mp_hdlr_init_##ops(void); \
549 void __attribute__((constructor, used)) mp_hdlr_init_##ops(void)\
551 rte_mempool_register_ops(&ops); \
555 * An object callback function for mempool.
557 * Used by rte_mempool_create() and rte_mempool_obj_iter().
559 typedef void (rte_mempool_obj_cb_t)(struct rte_mempool *mp,
560 void *opaque, void *obj, unsigned obj_idx);
561 typedef rte_mempool_obj_cb_t rte_mempool_obj_ctor_t; /* compat */
564 * A memory callback function for mempool.
566 * Used by rte_mempool_mem_iter().
568 typedef void (rte_mempool_mem_cb_t)(struct rte_mempool *mp,
569 void *opaque, struct rte_mempool_memhdr *memhdr,
573 * A mempool constructor callback function.
575 * Arguments are the mempool and the opaque pointer given by the user in
576 * rte_mempool_create().
578 typedef void (rte_mempool_ctor_t)(struct rte_mempool *, void *);
581 * Create a new mempool named *name* in memory.
583 * This function uses ``memzone_reserve()`` to allocate memory. The
584 * pool contains n elements of elt_size. Its size is set to n.
585 * All elements of the mempool are allocated together with the mempool header,
586 * in one physically continuous chunk of memory.
589 * The name of the mempool.
591 * The number of elements in the mempool. The optimum size (in terms of
592 * memory usage) for a mempool is when n is a power of two minus one:
595 * The size of each element.
597 * If cache_size is non-zero, the rte_mempool library will try to
598 * limit the accesses to the common lockless pool, by maintaining a
599 * per-lcore object cache. This argument must be lower or equal to
600 * CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE and n / 1.5. It is advised to choose
601 * cache_size to have "n modulo cache_size == 0": if this is
602 * not the case, some elements will always stay in the pool and will
603 * never be used. The access to the per-lcore table is of course
604 * faster than the multi-producer/consumer pool. The cache can be
605 * disabled if the cache_size argument is set to 0; it can be useful to
606 * avoid losing objects in cache. Note that even if not used, the
607 * memory space for cache is always reserved in a mempool structure,
608 * except if CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE is set to 0.
609 * @param private_data_size
610 * The size of the private data appended after the mempool
611 * structure. This is useful for storing some private data after the
612 * mempool structure, as is done for rte_mbuf_pool for example.
614 * A function pointer that is called for initialization of the pool,
615 * before object initialization. The user can initialize the private
616 * data in this function if needed. This parameter can be NULL if
619 * An opaque pointer to data that can be used in the mempool
620 * constructor function.
622 * A function pointer that is called for each object at
623 * initialization of the pool. The user can set some meta data in
624 * objects if needed. This parameter can be NULL if not needed.
625 * The obj_init() function takes the mempool pointer, the init_arg,
626 * the object pointer and the object number as parameters.
627 * @param obj_init_arg
628 * An opaque pointer to data that can be used as an argument for
629 * each call to the object constructor function.
631 * The *socket_id* argument is the socket identifier in the case of
632 * NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
633 * constraint for the reserved zone.
635 * The *flags* arguments is an OR of following flags:
636 * - MEMPOOL_F_NO_SPREAD: By default, objects addresses are spread
637 * between channels in RAM: the pool allocator will add padding
638 * between objects depending on the hardware configuration. See
639 * Memory alignment constraints for details. If this flag is set,
640 * the allocator will just align them to a cache line.
641 * - MEMPOOL_F_NO_CACHE_ALIGN: By default, the returned objects are
642 * cache-aligned. This flag removes this constraint, and no
643 * padding will be present between objects. This flag implies
644 * MEMPOOL_F_NO_SPREAD.
645 * - MEMPOOL_F_SP_PUT: If this flag is set, the default behavior
646 * when using rte_mempool_put() or rte_mempool_put_bulk() is
647 * "single-producer". Otherwise, it is "multi-producers".
648 * - MEMPOOL_F_SC_GET: If this flag is set, the default behavior
649 * when using rte_mempool_get() or rte_mempool_get_bulk() is
650 * "single-consumer". Otherwise, it is "multi-consumers".
651 * - MEMPOOL_F_NO_PHYS_CONTIG: If set, allocated objects won't
652 * necessarilly be contiguous in physical memory.
654 * The pointer to the new allocated mempool, on success. NULL on error
655 * with rte_errno set appropriately. Possible rte_errno values include:
656 * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
657 * - E_RTE_SECONDARY - function was called from a secondary process instance
658 * - EINVAL - cache size provided is too large
659 * - ENOSPC - the maximum number of memzones has already been allocated
660 * - EEXIST - a memzone with the same name already exists
661 * - ENOMEM - no appropriate memory area found in which to create memzone
664 rte_mempool_create(const char *name, unsigned n, unsigned elt_size,
665 unsigned cache_size, unsigned private_data_size,
666 rte_mempool_ctor_t *mp_init, void *mp_init_arg,
667 rte_mempool_obj_cb_t *obj_init, void *obj_init_arg,
668 int socket_id, unsigned flags);
671 * Create a new mempool named *name* in memory.
673 * The pool contains n elements of elt_size. Its size is set to n.
674 * This function uses ``memzone_reserve()`` to allocate the mempool header
675 * (and the objects if vaddr is NULL).
676 * Depending on the input parameters, mempool elements can be either allocated
677 * together with the mempool header, or an externally provided memory buffer
678 * could be used to store mempool objects. In later case, that external
679 * memory buffer can consist of set of disjoint physical pages.
682 * The name of the mempool.
684 * The number of elements in the mempool. The optimum size (in terms of
685 * memory usage) for a mempool is when n is a power of two minus one:
688 * The size of each element.
690 * Size of the cache. See rte_mempool_create() for details.
691 * @param private_data_size
692 * The size of the private data appended after the mempool
693 * structure. This is useful for storing some private data after the
694 * mempool structure, as is done for rte_mbuf_pool for example.
696 * A function pointer that is called for initialization of the pool,
697 * before object initialization. The user can initialize the private
698 * data in this function if needed. This parameter can be NULL if
701 * An opaque pointer to data that can be used in the mempool
702 * constructor function.
704 * A function called for each object at initialization of the pool.
705 * See rte_mempool_create() for details.
706 * @param obj_init_arg
707 * An opaque pointer passed to the object constructor function.
709 * The *socket_id* argument is the socket identifier in the case of
710 * NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
711 * constraint for the reserved zone.
713 * Flags controlling the behavior of the mempool. See
714 * rte_mempool_create() for details.
716 * Virtual address of the externally allocated memory buffer.
717 * Will be used to store mempool objects.
719 * Array of physical addresses of the pages that comprises given memory
722 * Number of elements in the paddr array.
724 * LOG2 of the physical pages size.
726 * The pointer to the new allocated mempool, on success. NULL on error
727 * with rte_errno set appropriately. See rte_mempool_create() for details.
730 rte_mempool_xmem_create(const char *name, unsigned n, unsigned elt_size,
731 unsigned cache_size, unsigned private_data_size,
732 rte_mempool_ctor_t *mp_init, void *mp_init_arg,
733 rte_mempool_obj_cb_t *obj_init, void *obj_init_arg,
734 int socket_id, unsigned flags, void *vaddr,
735 const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift);
738 * Create an empty mempool
740 * The mempool is allocated and initialized, but it is not populated: no
741 * memory is allocated for the mempool elements. The user has to call
742 * rte_mempool_populate_*() or to add memory chunks to the pool. Once
743 * populated, the user may also want to initialize each object with
744 * rte_mempool_obj_iter().
747 * The name of the mempool.
749 * The maximum number of elements that can be added in the mempool.
750 * The optimum size (in terms of memory usage) for a mempool is when n
751 * is a power of two minus one: n = (2^q - 1).
753 * The size of each element.
755 * Size of the cache. See rte_mempool_create() for details.
756 * @param private_data_size
757 * The size of the private data appended after the mempool
758 * structure. This is useful for storing some private data after the
759 * mempool structure, as is done for rte_mbuf_pool for example.
761 * The *socket_id* argument is the socket identifier in the case of
762 * NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
763 * constraint for the reserved zone.
765 * Flags controlling the behavior of the mempool. See
766 * rte_mempool_create() for details.
768 * The pointer to the new allocated mempool, on success. NULL on error
769 * with rte_errno set appropriately. See rte_mempool_create() for details.
772 rte_mempool_create_empty(const char *name, unsigned n, unsigned elt_size,
773 unsigned cache_size, unsigned private_data_size,
774 int socket_id, unsigned flags);
778 * Unlink the mempool from global list, free the memory chunks, and all
779 * memory referenced by the mempool. The objects must not be used by
780 * other cores as they will be freed.
783 * A pointer to the mempool structure.
786 rte_mempool_free(struct rte_mempool *mp);
789 * Add physically contiguous memory for objects in the pool at init
791 * Add a virtually and physically contiguous memory chunk in the pool
792 * where objects can be instanciated.
795 * A pointer to the mempool structure.
797 * The virtual address of memory that should be used to store objects.
799 * The physical address
801 * The length of memory in bytes.
803 * The callback used to free this chunk when destroying the mempool.
805 * An opaque argument passed to free_cb.
807 * The number of objects added on success.
808 * On error, the chunk is not added in the memory list of the
809 * mempool and a negative errno is returned.
811 int rte_mempool_populate_phys(struct rte_mempool *mp, char *vaddr,
812 phys_addr_t paddr, size_t len, rte_mempool_memchunk_free_cb_t *free_cb,
816 * Add physical memory for objects in the pool at init
818 * Add a virtually contiguous memory chunk in the pool where objects can
819 * be instanciated. The physical addresses corresponding to the virtual
820 * area are described in paddr[], pg_num, pg_shift.
823 * A pointer to the mempool structure.
825 * The virtual address of memory that should be used to store objects.
827 * An array of physical addresses of each page composing the virtual
830 * Number of elements in the paddr array.
832 * LOG2 of the physical pages size.
834 * The callback used to free this chunk when destroying the mempool.
836 * An opaque argument passed to free_cb.
838 * The number of objects added on success.
839 * On error, the chunks are not added in the memory list of the
840 * mempool and a negative errno is returned.
842 int rte_mempool_populate_phys_tab(struct rte_mempool *mp, char *vaddr,
843 const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift,
844 rte_mempool_memchunk_free_cb_t *free_cb, void *opaque);
847 * Add virtually contiguous memory for objects in the pool at init
849 * Add a virtually contiguous memory chunk in the pool where objects can
853 * A pointer to the mempool structure.
855 * The virtual address of memory that should be used to store objects.
856 * Must be page-aligned.
858 * The length of memory in bytes. Must be page-aligned.
860 * The size of memory pages in this virtual area.
862 * The callback used to free this chunk when destroying the mempool.
864 * An opaque argument passed to free_cb.
866 * The number of objects added on success.
867 * On error, the chunk is not added in the memory list of the
868 * mempool and a negative errno is returned.
871 rte_mempool_populate_virt(struct rte_mempool *mp, char *addr,
872 size_t len, size_t pg_sz, rte_mempool_memchunk_free_cb_t *free_cb,
876 * Add memory for objects in the pool at init
878 * This is the default function used by rte_mempool_create() to populate
879 * the mempool. It adds memory allocated using rte_memzone_reserve().
882 * A pointer to the mempool structure.
884 * The number of objects added on success.
885 * On error, the chunk is not added in the memory list of the
886 * mempool and a negative errno is returned.
888 int rte_mempool_populate_default(struct rte_mempool *mp);
891 * Add memory from anonymous mapping for objects in the pool at init
893 * This function mmap an anonymous memory zone that is locked in
894 * memory to store the objects of the mempool.
897 * A pointer to the mempool structure.
899 * The number of objects added on success.
900 * On error, the chunk is not added in the memory list of the
901 * mempool and a negative errno is returned.
903 int rte_mempool_populate_anon(struct rte_mempool *mp);
906 * Call a function for each mempool element
908 * Iterate across all objects attached to a rte_mempool and call the
909 * callback function on it.
912 * A pointer to an initialized mempool.
914 * A function pointer that is called for each object.
916 * An opaque pointer passed to the callback function.
918 * Number of objects iterated.
920 uint32_t rte_mempool_obj_iter(struct rte_mempool *mp,
921 rte_mempool_obj_cb_t *obj_cb, void *obj_cb_arg);
924 * Call a function for each mempool memory chunk
926 * Iterate across all memory chunks attached to a rte_mempool and call
927 * the callback function on it.
930 * A pointer to an initialized mempool.
932 * A function pointer that is called for each memory chunk.
934 * An opaque pointer passed to the callback function.
936 * Number of memory chunks iterated.
938 uint32_t rte_mempool_mem_iter(struct rte_mempool *mp,
939 rte_mempool_mem_cb_t *mem_cb, void *mem_cb_arg);
942 * Dump the status of the mempool to the console.
945 * A pointer to a file for output
947 * A pointer to the mempool structure.
949 void rte_mempool_dump(FILE *f, struct rte_mempool *mp);
952 * Create a user-owned mempool cache.
954 * This can be used by non-EAL threads to enable caching when they
955 * interact with a mempool.
958 * The size of the mempool cache. See rte_mempool_create()'s cache_size
959 * parameter description for more information. The same limits and
960 * considerations apply here too.
962 * The socket identifier in the case of NUMA. The value can be
963 * SOCKET_ID_ANY if there is no NUMA constraint for the reserved zone.
965 struct rte_mempool_cache *
966 rte_mempool_cache_create(uint32_t size, int socket_id);
969 * Free a user-owned mempool cache.
972 * A pointer to the mempool cache.
975 rte_mempool_cache_free(struct rte_mempool_cache *cache);
978 * Flush a user-owned mempool cache to the specified mempool.
981 * A pointer to the mempool cache.
983 * A pointer to the mempool.
985 static inline void __attribute__((always_inline))
986 rte_mempool_cache_flush(struct rte_mempool_cache *cache,
987 struct rte_mempool *mp)
989 rte_mempool_ops_enqueue_bulk(mp, cache->objs, cache->len);
994 * Get a pointer to the per-lcore default mempool cache.
997 * A pointer to the mempool structure.
999 * The logical core id.
1001 * A pointer to the mempool cache or NULL if disabled or non-EAL thread.
1003 static inline struct rte_mempool_cache *__attribute__((always_inline))
1004 rte_mempool_default_cache(struct rte_mempool *mp, unsigned lcore_id)
1006 if (mp->cache_size == 0)
1009 if (lcore_id >= RTE_MAX_LCORE)
1012 return &mp->local_cache[lcore_id];
1016 * @internal Put several objects back in the mempool; used internally.
1018 * A pointer to the mempool structure.
1020 * A pointer to a table of void * pointers (objects).
1022 * The number of objects to store back in the mempool, must be strictly
1025 * A pointer to a mempool cache structure. May be NULL if not needed.
1027 * The flags used for the mempool creation.
1028 * Single-producer (MEMPOOL_F_SP_PUT flag) or multi-producers.
1030 static inline void __attribute__((always_inline))
1031 __mempool_generic_put(struct rte_mempool *mp, void * const *obj_table,
1032 unsigned n, struct rte_mempool_cache *cache, int flags)
1036 /* increment stat now, adding in mempool always success */
1037 __MEMPOOL_STAT_ADD(mp, put, n);
1039 /* No cache provided or single producer */
1040 if (unlikely(cache == NULL || flags & MEMPOOL_F_SP_PUT))
1043 /* Go straight to ring if put would overflow mem allocated for cache */
1044 if (unlikely(n > RTE_MEMPOOL_CACHE_MAX_SIZE))
1047 cache_objs = &cache->objs[cache->len];
1050 * The cache follows the following algorithm
1051 * 1. Add the objects to the cache
1052 * 2. Anything greater than the cache min value (if it crosses the
1053 * cache flush threshold) is flushed to the ring.
1056 /* Add elements back into the cache */
1057 rte_memcpy(&cache_objs[0], obj_table, sizeof(void *) * n);
1061 if (cache->len >= cache->flushthresh) {
1062 rte_mempool_ops_enqueue_bulk(mp, &cache->objs[cache->size],
1063 cache->len - cache->size);
1064 cache->len = cache->size;
1071 /* push remaining objects in ring */
1072 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
1073 if (rte_mempool_ops_enqueue_bulk(mp, obj_table, n) < 0)
1074 rte_panic("cannot put objects in mempool\n");
1076 rte_mempool_ops_enqueue_bulk(mp, obj_table, n);
1082 * Put several objects back in the mempool.
1085 * A pointer to the mempool structure.
1087 * A pointer to a table of void * pointers (objects).
1089 * The number of objects to add in the mempool from the obj_table.
1091 * A pointer to a mempool cache structure. May be NULL if not needed.
1093 * The flags used for the mempool creation.
1094 * Single-producer (MEMPOOL_F_SP_PUT flag) or multi-producers.
1096 static inline void __attribute__((always_inline))
1097 rte_mempool_generic_put(struct rte_mempool *mp, void * const *obj_table,
1098 unsigned n, struct rte_mempool_cache *cache, int flags)
1100 __mempool_check_cookies(mp, obj_table, n, 0);
1101 __mempool_generic_put(mp, obj_table, n, cache, flags);
1106 * Put several objects back in the mempool (multi-producers safe).
1109 * A pointer to the mempool structure.
1111 * A pointer to a table of void * pointers (objects).
1113 * The number of objects to add in the mempool from the obj_table.
1116 static inline void __attribute__((always_inline))
1117 rte_mempool_mp_put_bulk(struct rte_mempool *mp, void * const *obj_table,
1120 struct rte_mempool_cache *cache;
1121 cache = rte_mempool_default_cache(mp, rte_lcore_id());
1122 rte_mempool_generic_put(mp, obj_table, n, cache, 0);
1127 * Put several objects back in the mempool (NOT multi-producers safe).
1130 * A pointer to the mempool structure.
1132 * A pointer to a table of void * pointers (objects).
1134 * The number of objects to add in the mempool from obj_table.
1137 static inline void __attribute__((always_inline))
1138 rte_mempool_sp_put_bulk(struct rte_mempool *mp, void * const *obj_table,
1141 rte_mempool_generic_put(mp, obj_table, n, NULL, MEMPOOL_F_SP_PUT);
1145 * Put several objects back in the mempool.
1147 * This function calls the multi-producer or the single-producer
1148 * version depending on the default behavior that was specified at
1149 * mempool creation time (see flags).
1152 * A pointer to the mempool structure.
1154 * A pointer to a table of void * pointers (objects).
1156 * The number of objects to add in the mempool from obj_table.
1158 static inline void __attribute__((always_inline))
1159 rte_mempool_put_bulk(struct rte_mempool *mp, void * const *obj_table,
1162 struct rte_mempool_cache *cache;
1163 cache = rte_mempool_default_cache(mp, rte_lcore_id());
1164 rte_mempool_generic_put(mp, obj_table, n, cache, mp->flags);
1169 * Put one object in the mempool (multi-producers safe).
1172 * A pointer to the mempool structure.
1174 * A pointer to the object to be added.
1177 static inline void __attribute__((always_inline))
1178 rte_mempool_mp_put(struct rte_mempool *mp, void *obj)
1180 struct rte_mempool_cache *cache;
1181 cache = rte_mempool_default_cache(mp, rte_lcore_id());
1182 rte_mempool_generic_put(mp, &obj, 1, cache, 0);
1187 * Put one object back in the mempool (NOT multi-producers safe).
1190 * A pointer to the mempool structure.
1192 * A pointer to the object to be added.
1195 static inline void __attribute__((always_inline))
1196 rte_mempool_sp_put(struct rte_mempool *mp, void *obj)
1198 rte_mempool_generic_put(mp, &obj, 1, NULL, MEMPOOL_F_SP_PUT);
1202 * Put one object back in the mempool.
1204 * This function calls the multi-producer or the single-producer
1205 * version depending on the default behavior that was specified at
1206 * mempool creation time (see flags).
1209 * A pointer to the mempool structure.
1211 * A pointer to the object to be added.
1213 static inline void __attribute__((always_inline))
1214 rte_mempool_put(struct rte_mempool *mp, void *obj)
1216 rte_mempool_put_bulk(mp, &obj, 1);
1220 * @internal Get several objects from the mempool; used internally.
1222 * A pointer to the mempool structure.
1224 * A pointer to a table of void * pointers (objects).
1226 * The number of objects to get, must be strictly positive.
1228 * A pointer to a mempool cache structure. May be NULL if not needed.
1230 * The flags used for the mempool creation.
1231 * Single-consumer (MEMPOOL_F_SC_GET flag) or multi-consumers.
1233 * - >=0: Success; number of objects supplied.
1234 * - <0: Error; code of ring dequeue function.
1236 static inline int __attribute__((always_inline))
1237 __mempool_generic_get(struct rte_mempool *mp, void **obj_table,
1238 unsigned n, struct rte_mempool_cache *cache, int flags)
1241 uint32_t index, len;
1244 /* No cache provided or single consumer */
1245 if (unlikely(cache == NULL || flags & MEMPOOL_F_SC_GET ||
1249 cache_objs = cache->objs;
1251 /* Can this be satisfied from the cache? */
1252 if (cache->len < n) {
1253 /* No. Backfill the cache first, and then fill from it */
1254 uint32_t req = n + (cache->size - cache->len);
1256 /* How many do we require i.e. number to fill the cache + the request */
1257 ret = rte_mempool_ops_dequeue_bulk(mp,
1258 &cache->objs[cache->len], req);
1259 if (unlikely(ret < 0)) {
1261 * In the offchance that we are buffer constrained,
1262 * where we are not able to allocate cache + n, go to
1263 * the ring directly. If that fails, we are truly out of
1272 /* Now fill in the response ... */
1273 for (index = 0, len = cache->len - 1; index < n; ++index, len--, obj_table++)
1274 *obj_table = cache_objs[len];
1278 __MEMPOOL_STAT_ADD(mp, get_success, n);
1284 /* get remaining objects from ring */
1285 ret = rte_mempool_ops_dequeue_bulk(mp, obj_table, n);
1288 __MEMPOOL_STAT_ADD(mp, get_fail, n);
1290 __MEMPOOL_STAT_ADD(mp, get_success, n);
1296 * Get several objects from the mempool.
1298 * If cache is enabled, objects will be retrieved first from cache,
1299 * subsequently from the common pool. Note that it can return -ENOENT when
1300 * the local cache and common pool are empty, even if cache from other
1304 * A pointer to the mempool structure.
1306 * A pointer to a table of void * pointers (objects) that will be filled.
1308 * The number of objects to get from mempool to obj_table.
1310 * A pointer to a mempool cache structure. May be NULL if not needed.
1312 * The flags used for the mempool creation.
1313 * Single-consumer (MEMPOOL_F_SC_GET flag) or multi-consumers.
1315 * - 0: Success; objects taken.
1316 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
1318 static inline int __attribute__((always_inline))
1319 rte_mempool_generic_get(struct rte_mempool *mp, void **obj_table, unsigned n,
1320 struct rte_mempool_cache *cache, int flags)
1323 ret = __mempool_generic_get(mp, obj_table, n, cache, flags);
1325 __mempool_check_cookies(mp, obj_table, n, 1);
1331 * Get several objects from the mempool (multi-consumers safe).
1333 * If cache is enabled, objects will be retrieved first from cache,
1334 * subsequently from the common pool. Note that it can return -ENOENT when
1335 * the local cache and common pool are empty, even if cache from other
1339 * A pointer to the mempool structure.
1341 * A pointer to a table of void * pointers (objects) that will be filled.
1343 * The number of objects to get from mempool to obj_table.
1345 * - 0: Success; objects taken.
1346 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
1349 static inline int __attribute__((always_inline))
1350 rte_mempool_mc_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned n)
1352 struct rte_mempool_cache *cache;
1353 cache = rte_mempool_default_cache(mp, rte_lcore_id());
1354 return rte_mempool_generic_get(mp, obj_table, n, cache, 0);
1359 * Get several objects from the mempool (NOT multi-consumers safe).
1361 * If cache is enabled, objects will be retrieved first from cache,
1362 * subsequently from the common pool. Note that it can return -ENOENT when
1363 * the local cache and common pool are empty, even if cache from other
1367 * A pointer to the mempool structure.
1369 * A pointer to a table of void * pointers (objects) that will be filled.
1371 * The number of objects to get from the mempool to obj_table.
1373 * - 0: Success; objects taken.
1374 * - -ENOENT: Not enough entries in the mempool; no object is
1378 static inline int __attribute__((always_inline))
1379 rte_mempool_sc_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned n)
1381 return rte_mempool_generic_get(mp, obj_table, n, NULL,
1386 * Get several objects from the mempool.
1388 * This function calls the multi-consumers or the single-consumer
1389 * version, depending on the default behaviour that was specified at
1390 * mempool creation time (see flags).
1392 * If cache is enabled, objects will be retrieved first from cache,
1393 * subsequently from the common pool. Note that it can return -ENOENT when
1394 * the local cache and common pool are empty, even if cache from other
1398 * A pointer to the mempool structure.
1400 * A pointer to a table of void * pointers (objects) that will be filled.
1402 * The number of objects to get from the mempool to obj_table.
1404 * - 0: Success; objects taken
1405 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
1407 static inline int __attribute__((always_inline))
1408 rte_mempool_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned n)
1410 struct rte_mempool_cache *cache;
1411 cache = rte_mempool_default_cache(mp, rte_lcore_id());
1412 return rte_mempool_generic_get(mp, obj_table, n, cache, mp->flags);
1417 * Get one object from the mempool (multi-consumers safe).
1419 * If cache is enabled, objects will be retrieved first from cache,
1420 * subsequently from the common pool. Note that it can return -ENOENT when
1421 * the local cache and common pool are empty, even if cache from other
1425 * A pointer to the mempool structure.
1427 * A pointer to a void * pointer (object) that will be filled.
1429 * - 0: Success; objects taken.
1430 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
1433 static inline int __attribute__((always_inline))
1434 rte_mempool_mc_get(struct rte_mempool *mp, void **obj_p)
1436 struct rte_mempool_cache *cache;
1437 cache = rte_mempool_default_cache(mp, rte_lcore_id());
1438 return rte_mempool_generic_get(mp, obj_p, 1, cache, 0);
1443 * Get one object from the mempool (NOT multi-consumers safe).
1445 * If cache is enabled, objects will be retrieved first from cache,
1446 * subsequently from the common pool. Note that it can return -ENOENT when
1447 * the local cache and common pool are empty, even if cache from other
1451 * A pointer to the mempool structure.
1453 * A pointer to a void * pointer (object) that will be filled.
1455 * - 0: Success; objects taken.
1456 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
1459 static inline int __attribute__((always_inline))
1460 rte_mempool_sc_get(struct rte_mempool *mp, void **obj_p)
1462 return rte_mempool_generic_get(mp, obj_p, 1, NULL, MEMPOOL_F_SC_GET);
1466 * Get one object from the mempool.
1468 * This function calls the multi-consumers or the single-consumer
1469 * version, depending on the default behavior that was specified at
1470 * mempool creation (see flags).
1472 * If cache is enabled, objects will be retrieved first from cache,
1473 * subsequently from the common pool. Note that it can return -ENOENT when
1474 * the local cache and common pool are empty, even if cache from other
1478 * A pointer to the mempool structure.
1480 * A pointer to a void * pointer (object) that will be filled.
1482 * - 0: Success; objects taken.
1483 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
1485 static inline int __attribute__((always_inline))
1486 rte_mempool_get(struct rte_mempool *mp, void **obj_p)
1488 return rte_mempool_get_bulk(mp, obj_p, 1);
1492 * Return the number of entries in the mempool.
1494 * When cache is enabled, this function has to browse the length of
1495 * all lcores, so it should not be used in a data path, but only for
1496 * debug purposes. User-owned mempool caches are not accounted for.
1499 * A pointer to the mempool structure.
1501 * The number of entries in the mempool.
1503 unsigned int rte_mempool_avail_count(const struct rte_mempool *mp);
1507 * Return the number of entries in the mempool.
1509 * When cache is enabled, this function has to browse the length of
1510 * all lcores, so it should not be used in a data path, but only for
1514 * A pointer to the mempool structure.
1516 * The number of entries in the mempool.
1519 unsigned rte_mempool_count(const struct rte_mempool *mp);
1522 * Return the number of elements which have been allocated from the mempool
1524 * When cache is enabled, this function has to browse the length of
1525 * all lcores, so it should not be used in a data path, but only for
1529 * A pointer to the mempool structure.
1531 * The number of free entries in the mempool.
1534 rte_mempool_in_use_count(const struct rte_mempool *mp);
1538 * Return the number of free entries in the mempool ring.
1539 * i.e. how many entries can be freed back to the mempool.
1541 * NOTE: This corresponds to the number of elements *allocated* from the
1542 * memory pool, not the number of elements in the pool itself. To count
1543 * the number elements currently available in the pool, use "rte_mempool_count"
1545 * When cache is enabled, this function has to browse the length of
1546 * all lcores, so it should not be used in a data path, but only for
1547 * debug purposes. User-owned mempool caches are not accounted for.
1550 * A pointer to the mempool structure.
1552 * The number of free entries in the mempool.
1555 static inline unsigned
1556 rte_mempool_free_count(const struct rte_mempool *mp)
1558 return rte_mempool_in_use_count(mp);
1562 * Test if the mempool is full.
1564 * When cache is enabled, this function has to browse the length of all
1565 * lcores, so it should not be used in a data path, but only for debug
1566 * purposes. User-owned mempool caches are not accounted for.
1569 * A pointer to the mempool structure.
1571 * - 1: The mempool is full.
1572 * - 0: The mempool is not full.
1575 rte_mempool_full(const struct rte_mempool *mp)
1577 return !!(rte_mempool_avail_count(mp) == mp->size);
1581 * Test if the mempool is empty.
1583 * When cache is enabled, this function has to browse the length of all
1584 * lcores, so it should not be used in a data path, but only for debug
1585 * purposes. User-owned mempool caches are not accounted for.
1588 * A pointer to the mempool structure.
1590 * - 1: The mempool is empty.
1591 * - 0: The mempool is not empty.
1594 rte_mempool_empty(const struct rte_mempool *mp)
1596 return !!(rte_mempool_avail_count(mp) == 0);
1600 * Return the physical address of elt, which is an element of the pool mp.
1603 * A pointer to the mempool structure.
1605 * A pointer (virtual address) to the element of the pool.
1607 * The physical address of the elt element.
1608 * If the mempool was created with MEMPOOL_F_NO_PHYS_CONTIG, the
1609 * returned value is RTE_BAD_PHYS_ADDR.
1611 static inline phys_addr_t
1612 rte_mempool_virt2phy(__rte_unused const struct rte_mempool *mp, const void *elt)
1614 const struct rte_mempool_objhdr *hdr;
1615 hdr = (const struct rte_mempool_objhdr *)RTE_PTR_SUB(elt,
1617 return hdr->physaddr;
1621 * Check the consistency of mempool objects.
1623 * Verify the coherency of fields in the mempool structure. Also check
1624 * that the cookies of mempool objects (even the ones that are not
1625 * present in pool) have a correct value. If not, a panic will occur.
1628 * A pointer to the mempool structure.
1630 void rte_mempool_audit(struct rte_mempool *mp);
1633 * Return a pointer to the private data in an mempool structure.
1636 * A pointer to the mempool structure.
1638 * A pointer to the private data.
1640 static inline void *rte_mempool_get_priv(struct rte_mempool *mp)
1643 MEMPOOL_HEADER_SIZE(mp, mp->cache_size);
1647 * Dump the status of all mempools on the console
1650 * A pointer to a file for output
1652 void rte_mempool_list_dump(FILE *f);
1655 * Search a mempool from its name
1658 * The name of the mempool.
1660 * The pointer to the mempool matching the name, or NULL if not found.
1662 * with rte_errno set appropriately. Possible rte_errno values include:
1663 * - ENOENT - required entry not available to return.
1666 struct rte_mempool *rte_mempool_lookup(const char *name);
1669 * Get the header, trailer and total size of a mempool element.
1671 * Given a desired size of the mempool element and mempool flags,
1672 * calculates header, trailer, body and total sizes of the mempool object.
1675 * The size of each element, without header and trailer.
1677 * The flags used for the mempool creation.
1678 * Consult rte_mempool_create() for more information about possible values.
1679 * The size of each element.
1681 * The calculated detailed size the mempool object. May be NULL.
1683 * Total size of the mempool object.
1685 uint32_t rte_mempool_calc_obj_size(uint32_t elt_size, uint32_t flags,
1686 struct rte_mempool_objsz *sz);
1689 * Get the size of memory required to store mempool elements.
1691 * Calculate the maximum amount of memory required to store given number
1692 * of objects. Assume that the memory buffer will be aligned at page
1695 * Note that if object size is bigger then page size, then it assumes
1696 * that pages are grouped in subsets of physically continuous pages big
1697 * enough to store at least one object.
1700 * Number of elements.
1701 * @param total_elt_sz
1702 * The size of each element, including header and trailer, as returned
1703 * by rte_mempool_calc_obj_size().
1705 * LOG2 of the physical pages size. If set to 0, ignore page boundaries.
1707 * Required memory size aligned at page boundary.
1709 size_t rte_mempool_xmem_size(uint32_t elt_num, size_t total_elt_sz,
1713 * Get the size of memory required to store mempool elements.
1715 * Calculate how much memory would be actually required with the given
1716 * memory footprint to store required number of objects.
1719 * Virtual address of the externally allocated memory buffer.
1720 * Will be used to store mempool objects.
1722 * Number of elements.
1723 * @param total_elt_sz
1724 * The size of each element, including header and trailer, as returned
1725 * by rte_mempool_calc_obj_size().
1727 * Array of physical addresses of the pages that comprises given memory
1730 * Number of elements in the paddr array.
1732 * LOG2 of the physical pages size.
1734 * On success, the number of bytes needed to store given number of
1735 * objects, aligned to the given page size. If the provided memory
1736 * buffer is too small, return a negative value whose absolute value
1737 * is the actual number of elements that can be stored in that buffer.
1739 ssize_t rte_mempool_xmem_usage(void *vaddr, uint32_t elt_num,
1740 size_t total_elt_sz, const phys_addr_t paddr[], uint32_t pg_num,
1744 * Walk list of all memory pools
1749 * Argument passed to iterator
1751 void rte_mempool_walk(void (*func)(struct rte_mempool *, void *arg),
1758 #endif /* _RTE_MEMPOOL_H_ */