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>
78 #include <rte_common.h>
84 #define RTE_MEMPOOL_HEADER_COOKIE1 0xbadbadbadadd2e55ULL /**< Header cookie. */
85 #define RTE_MEMPOOL_HEADER_COOKIE2 0xf2eef2eedadd2e55ULL /**< Header cookie. */
86 #define RTE_MEMPOOL_TRAILER_COOKIE 0xadd2e55badbadbadULL /**< Trailer cookie.*/
88 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
90 * A structure that stores the mempool statistics (per-lcore).
92 struct rte_mempool_debug_stats {
93 uint64_t put_bulk; /**< Number of puts. */
94 uint64_t put_objs; /**< Number of objects successfully put. */
95 uint64_t get_success_bulk; /**< Successful allocation number. */
96 uint64_t get_success_objs; /**< Objects successfully allocated. */
97 uint64_t get_fail_bulk; /**< Failed allocation number. */
98 uint64_t get_fail_objs; /**< Objects that failed to be allocated. */
99 } __rte_cache_aligned;
103 * A structure that stores a per-core object cache.
105 struct rte_mempool_cache {
106 uint32_t size; /**< Size of the cache */
107 uint32_t flushthresh; /**< Threshold before we flush excess elements */
108 uint32_t len; /**< Current cache count */
110 * Cache is allocated to this size to allow it to overflow in certain
111 * cases to avoid needless emptying of cache.
113 void *objs[RTE_MEMPOOL_CACHE_MAX_SIZE * 3]; /**< Cache objects */
114 } __rte_cache_aligned;
117 * A structure that stores the size of mempool elements.
119 struct rte_mempool_objsz {
120 uint32_t elt_size; /**< Size of an element. */
121 uint32_t header_size; /**< Size of header (before elt). */
122 uint32_t trailer_size; /**< Size of trailer (after elt). */
124 /**< Total size of an object (header + elt + trailer). */
127 /**< Maximum length of a memory pool's name. */
128 #define RTE_MEMPOOL_NAMESIZE (RTE_RING_NAMESIZE - \
129 sizeof(RTE_MEMPOOL_MZ_PREFIX) + 1)
130 #define RTE_MEMPOOL_MZ_PREFIX "MP_"
133 #define RTE_MEMPOOL_MZ_FORMAT RTE_MEMPOOL_MZ_PREFIX "%s"
135 #define MEMPOOL_PG_SHIFT_MAX (sizeof(uintptr_t) * CHAR_BIT - 1)
137 /** Mempool over one chunk of physically continuous memory */
138 #define MEMPOOL_PG_NUM_DEFAULT 1
140 #ifndef RTE_MEMPOOL_ALIGN
141 #define RTE_MEMPOOL_ALIGN RTE_CACHE_LINE_SIZE
144 #define RTE_MEMPOOL_ALIGN_MASK (RTE_MEMPOOL_ALIGN - 1)
147 * Mempool object header structure
149 * Each object stored in mempools are prefixed by this header structure,
150 * it allows to retrieve the mempool pointer from the object and to
151 * iterate on all objects attached to a mempool. When debug is enabled,
152 * a cookie is also added in this structure preventing corruptions and
155 struct rte_mempool_objhdr {
156 STAILQ_ENTRY(rte_mempool_objhdr) next; /**< Next in list. */
157 struct rte_mempool *mp; /**< The mempool owning the object. */
158 phys_addr_t physaddr; /**< Physical address of the object. */
159 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
160 uint64_t cookie; /**< Debug cookie. */
165 * A list of object headers type
167 STAILQ_HEAD(rte_mempool_objhdr_list, rte_mempool_objhdr);
169 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
172 * Mempool object trailer structure
174 * In debug mode, each object stored in mempools are suffixed by this
175 * trailer structure containing a cookie preventing memory corruptions.
177 struct rte_mempool_objtlr {
178 uint64_t cookie; /**< Debug cookie. */
184 * A list of memory where objects are stored
186 STAILQ_HEAD(rte_mempool_memhdr_list, rte_mempool_memhdr);
189 * Callback used to free a memory chunk
191 typedef void (rte_mempool_memchunk_free_cb_t)(struct rte_mempool_memhdr *memhdr,
195 * Mempool objects memory header structure
197 * The memory chunks where objects are stored. Each chunk is virtually
198 * and physically contiguous.
200 struct rte_mempool_memhdr {
201 STAILQ_ENTRY(rte_mempool_memhdr) next; /**< Next in list. */
202 struct rte_mempool *mp; /**< The mempool owning the chunk */
203 void *addr; /**< Virtual address of the chunk */
204 phys_addr_t phys_addr; /**< Physical address of the chunk */
205 size_t len; /**< length of the chunk */
206 rte_mempool_memchunk_free_cb_t *free_cb; /**< Free callback */
207 void *opaque; /**< Argument passed to the free callback */
211 * The RTE mempool structure.
215 * Note: this field kept the RTE_MEMZONE_NAMESIZE size due to ABI
216 * compatibility requirements, it could be changed to
217 * RTE_MEMPOOL_NAMESIZE next time the ABI changes
219 char name[RTE_MEMZONE_NAMESIZE]; /**< Name of mempool. */
222 void *pool_data; /**< Ring or pool to store objects. */
223 uint64_t pool_id; /**< External mempool identifier. */
225 void *pool_config; /**< optional args for ops alloc. */
226 const struct rte_memzone *mz; /**< Memzone where pool is alloc'd. */
227 int flags; /**< Flags of the mempool. */
228 int socket_id; /**< Socket id passed at create. */
229 uint32_t size; /**< Max size of the mempool. */
231 /**< Size of per-lcore default local cache. */
233 uint32_t elt_size; /**< Size of an element. */
234 uint32_t header_size; /**< Size of header (before elt). */
235 uint32_t trailer_size; /**< Size of trailer (after elt). */
237 unsigned private_data_size; /**< Size of private data. */
239 * Index into rte_mempool_ops_table array of mempool ops
240 * structs, which contain callback function pointers.
241 * We're using an index here rather than pointers to the callbacks
242 * to facilitate any secondary processes that may want to use
247 struct rte_mempool_cache *local_cache; /**< Per-lcore local cache */
249 uint32_t populated_size; /**< Number of populated objects. */
250 struct rte_mempool_objhdr_list elt_list; /**< List of objects in pool */
251 uint32_t nb_mem_chunks; /**< Number of memory chunks */
252 struct rte_mempool_memhdr_list mem_list; /**< List of memory chunks */
254 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
255 /** Per-lcore statistics. */
256 struct rte_mempool_debug_stats stats[RTE_MAX_LCORE];
258 } __rte_cache_aligned;
260 #define MEMPOOL_F_NO_SPREAD 0x0001 /**< Do not spread among memory channels. */
261 #define MEMPOOL_F_NO_CACHE_ALIGN 0x0002 /**< Do not align objs on cache lines.*/
262 #define MEMPOOL_F_SP_PUT 0x0004 /**< Default put is "single-producer".*/
263 #define MEMPOOL_F_SC_GET 0x0008 /**< Default get is "single-consumer".*/
264 #define MEMPOOL_F_POOL_CREATED 0x0010 /**< Internal: pool is created. */
265 #define MEMPOOL_F_NO_PHYS_CONTIG 0x0020 /**< Don't need physically contiguous objs. */
268 * @internal When debug is enabled, store some statistics.
271 * Pointer to the memory pool.
273 * Name of the statistics field to increment in the memory pool.
275 * Number to add to the object-oriented statistics.
277 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
278 #define __MEMPOOL_STAT_ADD(mp, name, n) do { \
279 unsigned __lcore_id = rte_lcore_id(); \
280 if (__lcore_id < RTE_MAX_LCORE) { \
281 mp->stats[__lcore_id].name##_objs += n; \
282 mp->stats[__lcore_id].name##_bulk += 1; \
286 #define __MEMPOOL_STAT_ADD(mp, name, n) do {} while(0)
290 * Calculate the size of the mempool header.
293 * Pointer to the memory pool.
295 * Size of the per-lcore cache.
297 #define MEMPOOL_HEADER_SIZE(mp, cs) \
298 (sizeof(*(mp)) + (((cs) == 0) ? 0 : \
299 (sizeof(struct rte_mempool_cache) * RTE_MAX_LCORE)))
301 /* return the header of a mempool object (internal) */
302 static inline struct rte_mempool_objhdr *__mempool_get_header(void *obj)
304 return (struct rte_mempool_objhdr *)RTE_PTR_SUB(obj,
305 sizeof(struct rte_mempool_objhdr));
309 * Return a pointer to the mempool owning this object.
312 * An object that is owned by a pool. If this is not the case,
313 * the behavior is undefined.
315 * A pointer to the mempool structure.
317 static inline struct rte_mempool *rte_mempool_from_obj(void *obj)
319 struct rte_mempool_objhdr *hdr = __mempool_get_header(obj);
323 /* return the trailer of a mempool object (internal) */
324 static inline struct rte_mempool_objtlr *__mempool_get_trailer(void *obj)
326 struct rte_mempool *mp = rte_mempool_from_obj(obj);
327 return (struct rte_mempool_objtlr *)RTE_PTR_ADD(obj, mp->elt_size);
331 * @internal Check and update cookies or panic.
334 * Pointer to the memory pool.
335 * @param obj_table_const
336 * Pointer to a table of void * pointers (objects).
338 * Index of object in object table.
340 * - 0: object is supposed to be allocated, mark it as free
341 * - 1: object is supposed to be free, mark it as allocated
342 * - 2: just check that cookie is valid (free or allocated)
344 void rte_mempool_check_cookies(const struct rte_mempool *mp,
345 void * const *obj_table_const, unsigned n, int free);
347 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
348 #define __mempool_check_cookies(mp, obj_table_const, n, free) \
349 rte_mempool_check_cookies(mp, obj_table_const, n, free)
351 #define __mempool_check_cookies(mp, obj_table_const, n, free) do {} while(0)
352 #endif /* RTE_LIBRTE_MEMPOOL_DEBUG */
354 #define RTE_MEMPOOL_OPS_NAMESIZE 32 /**< Max length of ops struct name. */
357 * Prototype for implementation specific data provisioning function.
359 * The function should provide the implementation specific memory for
360 * for use by the other mempool ops functions in a given mempool ops struct.
361 * E.g. the default ops provides an instance of the rte_ring for this purpose.
362 * it will most likely point to a different type of data structure, and
363 * will be transparent to the application programmer.
364 * This function should set mp->pool_data.
366 typedef int (*rte_mempool_alloc_t)(struct rte_mempool *mp);
369 * Free the opaque private data pointed to by mp->pool_data pointer.
371 typedef void (*rte_mempool_free_t)(struct rte_mempool *mp);
374 * Enqueue an object into the external pool.
376 typedef int (*rte_mempool_enqueue_t)(struct rte_mempool *mp,
377 void * const *obj_table, unsigned int n);
380 * Dequeue an object from the external pool.
382 typedef int (*rte_mempool_dequeue_t)(struct rte_mempool *mp,
383 void **obj_table, unsigned int n);
386 * Return the number of available objects in the external pool.
388 typedef unsigned (*rte_mempool_get_count)(const struct rte_mempool *mp);
390 /** Structure defining mempool operations structure */
391 struct rte_mempool_ops {
392 char name[RTE_MEMPOOL_OPS_NAMESIZE]; /**< Name of mempool ops struct. */
393 rte_mempool_alloc_t alloc; /**< Allocate private data. */
394 rte_mempool_free_t free; /**< Free the external pool. */
395 rte_mempool_enqueue_t enqueue; /**< Enqueue an object. */
396 rte_mempool_dequeue_t dequeue; /**< Dequeue an object. */
397 rte_mempool_get_count get_count; /**< Get qty of available objs. */
398 } __rte_cache_aligned;
400 #define RTE_MEMPOOL_MAX_OPS_IDX 16 /**< Max registered ops structs */
403 * Structure storing the table of registered ops structs, each of which contain
404 * the function pointers for the mempool ops functions.
405 * Each process has its own storage for this ops struct array so that
406 * the mempools can be shared across primary and secondary processes.
407 * The indices used to access the array are valid across processes, whereas
408 * any function pointers stored directly in the mempool struct would not be.
409 * This results in us simply having "ops_index" in the mempool struct.
411 struct rte_mempool_ops_table {
412 rte_spinlock_t sl; /**< Spinlock for add/delete. */
413 uint32_t num_ops; /**< Number of used ops structs in the table. */
415 * Storage for all possible ops structs.
417 struct rte_mempool_ops ops[RTE_MEMPOOL_MAX_OPS_IDX];
418 } __rte_cache_aligned;
420 /** Array of registered ops structs. */
421 extern struct rte_mempool_ops_table rte_mempool_ops_table;
424 * @internal Get the mempool ops struct from its index.
427 * The index of the ops struct in the ops struct table. It must be a valid
428 * index: (0 <= idx < num_ops).
430 * The pointer to the ops struct in the table.
432 static inline struct rte_mempool_ops *
433 rte_mempool_get_ops(int ops_index)
435 RTE_VERIFY((ops_index >= 0) && (ops_index < RTE_MEMPOOL_MAX_OPS_IDX));
437 return &rte_mempool_ops_table.ops[ops_index];
441 * @internal Wrapper for mempool_ops alloc callback.
444 * Pointer to the memory pool.
446 * - 0: Success; successfully allocated mempool pool_data.
447 * - <0: Error; code of alloc function.
450 rte_mempool_ops_alloc(struct rte_mempool *mp);
453 * @internal Wrapper for mempool_ops dequeue callback.
456 * Pointer to the memory pool.
458 * Pointer to a table of void * pointers (objects).
460 * Number of objects to get.
462 * - 0: Success; got n objects.
463 * - <0: Error; code of dequeue function.
466 rte_mempool_ops_dequeue_bulk(struct rte_mempool *mp,
467 void **obj_table, unsigned n)
469 struct rte_mempool_ops *ops;
471 ops = rte_mempool_get_ops(mp->ops_index);
472 return ops->dequeue(mp, obj_table, n);
476 * @internal wrapper for mempool_ops enqueue callback.
479 * Pointer to the memory pool.
481 * Pointer to a table of void * pointers (objects).
483 * Number of objects to put.
485 * - 0: Success; n objects supplied.
486 * - <0: Error; code of enqueue function.
489 rte_mempool_ops_enqueue_bulk(struct rte_mempool *mp, void * const *obj_table,
492 struct rte_mempool_ops *ops;
494 ops = rte_mempool_get_ops(mp->ops_index);
495 return ops->enqueue(mp, obj_table, n);
499 * @internal wrapper for mempool_ops get_count callback.
502 * Pointer to the memory pool.
504 * The number of available objects in the external pool.
507 rte_mempool_ops_get_count(const struct rte_mempool *mp);
510 * @internal wrapper for mempool_ops free callback.
513 * Pointer to the memory pool.
516 rte_mempool_ops_free(struct rte_mempool *mp);
519 * Set the ops of a mempool.
521 * This can only be done on a mempool that is not populated, i.e. just after
522 * a call to rte_mempool_create_empty().
525 * Pointer to the memory pool.
527 * Name of the ops structure to use for this mempool.
529 * Opaque data that can be passed by the application to the ops functions.
531 * - 0: Success; the mempool is now using the requested ops functions.
532 * - -EINVAL - Invalid ops struct name provided.
533 * - -EEXIST - mempool already has an ops struct assigned.
536 rte_mempool_set_ops_byname(struct rte_mempool *mp, const char *name,
540 * Register mempool operations.
543 * Pointer to an ops structure to register.
545 * - >=0: Success; return the index of the ops struct in the table.
546 * - -EINVAL - some missing callbacks while registering ops struct.
547 * - -ENOSPC - the maximum number of ops structs has been reached.
549 int rte_mempool_register_ops(const struct rte_mempool_ops *ops);
552 * Macro to statically register the ops of a mempool handler.
553 * Note that the rte_mempool_register_ops fails silently here when
554 * more then RTE_MEMPOOL_MAX_OPS_IDX is registered.
556 #define MEMPOOL_REGISTER_OPS(ops) \
557 void mp_hdlr_init_##ops(void); \
558 void __attribute__((constructor, used)) mp_hdlr_init_##ops(void)\
560 rte_mempool_register_ops(&ops); \
564 * An object callback function for mempool.
566 * Used by rte_mempool_create() and rte_mempool_obj_iter().
568 typedef void (rte_mempool_obj_cb_t)(struct rte_mempool *mp,
569 void *opaque, void *obj, unsigned obj_idx);
570 typedef rte_mempool_obj_cb_t rte_mempool_obj_ctor_t; /* compat */
573 * A memory callback function for mempool.
575 * Used by rte_mempool_mem_iter().
577 typedef void (rte_mempool_mem_cb_t)(struct rte_mempool *mp,
578 void *opaque, struct rte_mempool_memhdr *memhdr,
582 * A mempool constructor callback function.
584 * Arguments are the mempool and the opaque pointer given by the user in
585 * rte_mempool_create().
587 typedef void (rte_mempool_ctor_t)(struct rte_mempool *, void *);
590 * Create a new mempool named *name* in memory.
592 * This function uses ``rte_memzone_reserve()`` to allocate memory. The
593 * pool contains n elements of elt_size. Its size is set to n.
596 * The name of the mempool.
598 * The number of elements in the mempool. The optimum size (in terms of
599 * memory usage) for a mempool is when n is a power of two minus one:
602 * The size of each element.
604 * If cache_size is non-zero, the rte_mempool library will try to
605 * limit the accesses to the common lockless pool, by maintaining a
606 * per-lcore object cache. This argument must be lower or equal to
607 * CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE and n / 1.5. It is advised to choose
608 * cache_size to have "n modulo cache_size == 0": if this is
609 * not the case, some elements will always stay in the pool and will
610 * never be used. The access to the per-lcore table is of course
611 * faster than the multi-producer/consumer pool. The cache can be
612 * disabled if the cache_size argument is set to 0; it can be useful to
613 * avoid losing objects in cache. Note that even if not used, the
614 * memory space for cache is always reserved in a mempool structure,
615 * except if CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE is set to 0.
616 * @param private_data_size
617 * The size of the private data appended after the mempool
618 * structure. This is useful for storing some private data after the
619 * mempool structure, as is done for rte_mbuf_pool for example.
621 * A function pointer that is called for initialization of the pool,
622 * before object initialization. The user can initialize the private
623 * data in this function if needed. This parameter can be NULL if
626 * An opaque pointer to data that can be used in the mempool
627 * constructor function.
629 * A function pointer that is called for each object at
630 * initialization of the pool. The user can set some meta data in
631 * objects if needed. This parameter can be NULL if not needed.
632 * The obj_init() function takes the mempool pointer, the init_arg,
633 * the object pointer and the object number as parameters.
634 * @param obj_init_arg
635 * An opaque pointer to data that can be used as an argument for
636 * each call to the object constructor function.
638 * The *socket_id* argument is the socket identifier in the case of
639 * NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
640 * constraint for the reserved zone.
642 * The *flags* arguments is an OR of following flags:
643 * - MEMPOOL_F_NO_SPREAD: By default, objects addresses are spread
644 * between channels in RAM: the pool allocator will add padding
645 * between objects depending on the hardware configuration. See
646 * Memory alignment constraints for details. If this flag is set,
647 * the allocator will just align them to a cache line.
648 * - MEMPOOL_F_NO_CACHE_ALIGN: By default, the returned objects are
649 * cache-aligned. This flag removes this constraint, and no
650 * padding will be present between objects. This flag implies
651 * MEMPOOL_F_NO_SPREAD.
652 * - MEMPOOL_F_SP_PUT: If this flag is set, the default behavior
653 * when using rte_mempool_put() or rte_mempool_put_bulk() is
654 * "single-producer". Otherwise, it is "multi-producers".
655 * - MEMPOOL_F_SC_GET: If this flag is set, the default behavior
656 * when using rte_mempool_get() or rte_mempool_get_bulk() is
657 * "single-consumer". Otherwise, it is "multi-consumers".
658 * - MEMPOOL_F_NO_PHYS_CONTIG: If set, allocated objects won't
659 * necessarilly be contiguous in physical memory.
661 * The pointer to the new allocated mempool, on success. NULL on error
662 * with rte_errno set appropriately. Possible rte_errno values include:
663 * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
664 * - E_RTE_SECONDARY - function was called from a secondary process instance
665 * - EINVAL - cache size provided is too large
666 * - ENOSPC - the maximum number of memzones has already been allocated
667 * - EEXIST - a memzone with the same name already exists
668 * - ENOMEM - no appropriate memory area found in which to create memzone
671 rte_mempool_create(const char *name, unsigned n, unsigned elt_size,
672 unsigned cache_size, unsigned private_data_size,
673 rte_mempool_ctor_t *mp_init, void *mp_init_arg,
674 rte_mempool_obj_cb_t *obj_init, void *obj_init_arg,
675 int socket_id, unsigned flags);
678 * Create a new mempool named *name* in memory.
680 * The pool contains n elements of elt_size. Its size is set to n.
681 * This function uses ``memzone_reserve()`` to allocate the mempool header
682 * (and the objects if vaddr is NULL).
683 * Depending on the input parameters, mempool elements can be either allocated
684 * together with the mempool header, or an externally provided memory buffer
685 * could be used to store mempool objects. In later case, that external
686 * memory buffer can consist of set of disjoint physical pages.
689 * The name of the mempool.
691 * The number of elements in the mempool. The optimum size (in terms of
692 * memory usage) for a mempool is when n is a power of two minus one:
695 * The size of each element.
697 * Size of the cache. See rte_mempool_create() for details.
698 * @param private_data_size
699 * The size of the private data appended after the mempool
700 * structure. This is useful for storing some private data after the
701 * mempool structure, as is done for rte_mbuf_pool for example.
703 * A function pointer that is called for initialization of the pool,
704 * before object initialization. The user can initialize the private
705 * data in this function if needed. This parameter can be NULL if
708 * An opaque pointer to data that can be used in the mempool
709 * constructor function.
711 * A function called for each object at initialization of the pool.
712 * See rte_mempool_create() for details.
713 * @param obj_init_arg
714 * An opaque pointer passed to the object constructor function.
716 * The *socket_id* argument is the socket identifier in the case of
717 * NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
718 * constraint for the reserved zone.
720 * Flags controlling the behavior of the mempool. See
721 * rte_mempool_create() for details.
723 * Virtual address of the externally allocated memory buffer.
724 * Will be used to store mempool objects.
726 * Array of physical addresses of the pages that comprises given memory
729 * Number of elements in the paddr array.
731 * LOG2 of the physical pages size.
733 * The pointer to the new allocated mempool, on success. NULL on error
734 * with rte_errno set appropriately. See rte_mempool_create() for details.
737 rte_mempool_xmem_create(const char *name, unsigned n, unsigned elt_size,
738 unsigned cache_size, unsigned private_data_size,
739 rte_mempool_ctor_t *mp_init, void *mp_init_arg,
740 rte_mempool_obj_cb_t *obj_init, void *obj_init_arg,
741 int socket_id, unsigned flags, void *vaddr,
742 const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift);
745 * Create an empty mempool
747 * The mempool is allocated and initialized, but it is not populated: no
748 * memory is allocated for the mempool elements. The user has to call
749 * rte_mempool_populate_*() to add memory chunks to the pool. Once
750 * populated, the user may also want to initialize each object with
751 * rte_mempool_obj_iter().
754 * The name of the mempool.
756 * The maximum number of elements that can be added in the mempool.
757 * The optimum size (in terms of memory usage) for a mempool is when n
758 * is a power of two minus one: n = (2^q - 1).
760 * The size of each element.
762 * Size of the cache. See rte_mempool_create() for details.
763 * @param private_data_size
764 * The size of the private data appended after the mempool
765 * structure. This is useful for storing some private data after the
766 * mempool structure, as is done for rte_mbuf_pool for example.
768 * The *socket_id* argument is the socket identifier in the case of
769 * NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
770 * constraint for the reserved zone.
772 * Flags controlling the behavior of the mempool. See
773 * rte_mempool_create() for details.
775 * The pointer to the new allocated mempool, on success. NULL on error
776 * with rte_errno set appropriately. See rte_mempool_create() for details.
779 rte_mempool_create_empty(const char *name, unsigned n, unsigned elt_size,
780 unsigned cache_size, unsigned private_data_size,
781 int socket_id, unsigned flags);
785 * Unlink the mempool from global list, free the memory chunks, and all
786 * memory referenced by the mempool. The objects must not be used by
787 * other cores as they will be freed.
790 * A pointer to the mempool structure.
793 rte_mempool_free(struct rte_mempool *mp);
796 * Add physically contiguous memory for objects in the pool at init
798 * Add a virtually and physically contiguous memory chunk in the pool
799 * where objects can be instanciated.
801 * If the given physical address is unknown (paddr = RTE_BAD_PHYS_ADDR),
802 * the chunk doesn't need to be physically contiguous (only virtually),
803 * and allocated objects may span two pages.
806 * A pointer to the mempool structure.
808 * The virtual address of memory that should be used to store objects.
810 * The physical address
812 * The length of memory in bytes.
814 * The callback used to free this chunk when destroying the mempool.
816 * An opaque argument passed to free_cb.
818 * The number of objects added on success.
819 * On error, the chunk is not added in the memory list of the
820 * mempool and a negative errno is returned.
822 int rte_mempool_populate_phys(struct rte_mempool *mp, char *vaddr,
823 phys_addr_t paddr, size_t len, rte_mempool_memchunk_free_cb_t *free_cb,
827 * Add physical memory for objects in the pool at init
829 * Add a virtually contiguous memory chunk in the pool where objects can
830 * be instanciated. The physical addresses corresponding to the virtual
831 * area are described in paddr[], pg_num, pg_shift.
834 * A pointer to the mempool structure.
836 * The virtual address of memory that should be used to store objects.
838 * An array of physical addresses of each page composing the virtual
841 * Number of elements in the paddr array.
843 * LOG2 of the physical pages size.
845 * The callback used to free this chunk when destroying the mempool.
847 * An opaque argument passed to free_cb.
849 * The number of objects added on success.
850 * On error, the chunks are not added in the memory list of the
851 * mempool and a negative errno is returned.
853 int rte_mempool_populate_phys_tab(struct rte_mempool *mp, char *vaddr,
854 const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift,
855 rte_mempool_memchunk_free_cb_t *free_cb, void *opaque);
858 * Add virtually contiguous memory for objects in the pool at init
860 * Add a virtually contiguous memory chunk in the pool where objects can
864 * A pointer to the mempool structure.
866 * The virtual address of memory that should be used to store objects.
867 * Must be page-aligned.
869 * The length of memory in bytes. Must be page-aligned.
871 * The size of memory pages in this virtual area.
873 * The callback used to free this chunk when destroying the mempool.
875 * An opaque argument passed to free_cb.
877 * The number of objects added on success.
878 * On error, the chunk is not added in the memory list of the
879 * mempool and a negative errno is returned.
882 rte_mempool_populate_virt(struct rte_mempool *mp, char *addr,
883 size_t len, size_t pg_sz, rte_mempool_memchunk_free_cb_t *free_cb,
887 * Add memory for objects in the pool at init
889 * This is the default function used by rte_mempool_create() to populate
890 * the mempool. It adds memory allocated using rte_memzone_reserve().
893 * A pointer to the mempool structure.
895 * The number of objects added on success.
896 * On error, the chunk is not added in the memory list of the
897 * mempool and a negative errno is returned.
899 int rte_mempool_populate_default(struct rte_mempool *mp);
902 * Add memory from anonymous mapping for objects in the pool at init
904 * This function mmap an anonymous memory zone that is locked in
905 * memory to store the objects of the mempool.
908 * A pointer to the mempool structure.
910 * The number of objects added on success.
911 * On error, the chunk is not added in the memory list of the
912 * mempool and a negative errno is returned.
914 int rte_mempool_populate_anon(struct rte_mempool *mp);
917 * Call a function for each mempool element
919 * Iterate across all objects attached to a rte_mempool and call the
920 * callback function on it.
923 * A pointer to an initialized mempool.
925 * A function pointer that is called for each object.
927 * An opaque pointer passed to the callback function.
929 * Number of objects iterated.
931 uint32_t rte_mempool_obj_iter(struct rte_mempool *mp,
932 rte_mempool_obj_cb_t *obj_cb, void *obj_cb_arg);
935 * Call a function for each mempool memory chunk
937 * Iterate across all memory chunks attached to a rte_mempool and call
938 * the callback function on it.
941 * A pointer to an initialized mempool.
943 * A function pointer that is called for each memory chunk.
945 * An opaque pointer passed to the callback function.
947 * Number of memory chunks iterated.
949 uint32_t rte_mempool_mem_iter(struct rte_mempool *mp,
950 rte_mempool_mem_cb_t *mem_cb, void *mem_cb_arg);
953 * Dump the status of the mempool to the console.
956 * A pointer to a file for output
958 * A pointer to the mempool structure.
960 void rte_mempool_dump(FILE *f, struct rte_mempool *mp);
963 * Create a user-owned mempool cache.
965 * This can be used by non-EAL threads to enable caching when they
966 * interact with a mempool.
969 * The size of the mempool cache. See rte_mempool_create()'s cache_size
970 * parameter description for more information. The same limits and
971 * considerations apply here too.
973 * The socket identifier in the case of NUMA. The value can be
974 * SOCKET_ID_ANY if there is no NUMA constraint for the reserved zone.
976 struct rte_mempool_cache *
977 rte_mempool_cache_create(uint32_t size, int socket_id);
980 * Free a user-owned mempool cache.
983 * A pointer to the mempool cache.
986 rte_mempool_cache_free(struct rte_mempool_cache *cache);
989 * Flush a user-owned mempool cache to the specified mempool.
992 * A pointer to the mempool cache.
994 * A pointer to the mempool.
996 static inline void __attribute__((always_inline))
997 rte_mempool_cache_flush(struct rte_mempool_cache *cache,
998 struct rte_mempool *mp)
1000 rte_mempool_ops_enqueue_bulk(mp, cache->objs, cache->len);
1005 * Get a pointer to the per-lcore default mempool cache.
1008 * A pointer to the mempool structure.
1010 * The logical core id.
1012 * A pointer to the mempool cache or NULL if disabled or non-EAL thread.
1014 static inline struct rte_mempool_cache *__attribute__((always_inline))
1015 rte_mempool_default_cache(struct rte_mempool *mp, unsigned lcore_id)
1017 if (mp->cache_size == 0)
1020 if (lcore_id >= RTE_MAX_LCORE)
1023 return &mp->local_cache[lcore_id];
1027 * @internal Put several objects back in the mempool; used internally.
1029 * A pointer to the mempool structure.
1031 * A pointer to a table of void * pointers (objects).
1033 * The number of objects to store back in the mempool, must be strictly
1036 * A pointer to a mempool cache structure. May be NULL if not needed.
1038 * The flags used for the mempool creation.
1039 * Single-producer (MEMPOOL_F_SP_PUT flag) or multi-producers.
1041 static inline void __attribute__((always_inline))
1042 __mempool_generic_put(struct rte_mempool *mp, void * const *obj_table,
1043 unsigned n, struct rte_mempool_cache *cache, int flags)
1047 /* increment stat now, adding in mempool always success */
1048 __MEMPOOL_STAT_ADD(mp, put, n);
1050 /* No cache provided or single producer */
1051 if (unlikely(cache == NULL || flags & MEMPOOL_F_SP_PUT))
1054 /* Go straight to ring if put would overflow mem allocated for cache */
1055 if (unlikely(n > RTE_MEMPOOL_CACHE_MAX_SIZE))
1058 cache_objs = &cache->objs[cache->len];
1061 * The cache follows the following algorithm
1062 * 1. Add the objects to the cache
1063 * 2. Anything greater than the cache min value (if it crosses the
1064 * cache flush threshold) is flushed to the ring.
1067 /* Add elements back into the cache */
1068 rte_memcpy(&cache_objs[0], obj_table, sizeof(void *) * n);
1072 if (cache->len >= cache->flushthresh) {
1073 rte_mempool_ops_enqueue_bulk(mp, &cache->objs[cache->size],
1074 cache->len - cache->size);
1075 cache->len = cache->size;
1082 /* push remaining objects in ring */
1083 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
1084 if (rte_mempool_ops_enqueue_bulk(mp, obj_table, n) < 0)
1085 rte_panic("cannot put objects in mempool\n");
1087 rte_mempool_ops_enqueue_bulk(mp, obj_table, n);
1093 * Put several objects back in the mempool.
1096 * A pointer to the mempool structure.
1098 * A pointer to a table of void * pointers (objects).
1100 * The number of objects to add in the mempool from the obj_table.
1102 * A pointer to a mempool cache structure. May be NULL if not needed.
1104 * The flags used for the mempool creation.
1105 * Single-producer (MEMPOOL_F_SP_PUT flag) or multi-producers.
1107 static inline void __attribute__((always_inline))
1108 rte_mempool_generic_put(struct rte_mempool *mp, void * const *obj_table,
1109 unsigned n, struct rte_mempool_cache *cache, int flags)
1111 __mempool_check_cookies(mp, obj_table, n, 0);
1112 __mempool_generic_put(mp, obj_table, n, cache, flags);
1117 * Put several objects back in the mempool (multi-producers safe).
1120 * A pointer to the mempool structure.
1122 * A pointer to a table of void * pointers (objects).
1124 * The number of objects to add in the mempool from the obj_table.
1127 static inline void __attribute__((always_inline))
1128 rte_mempool_mp_put_bulk(struct rte_mempool *mp, void * const *obj_table,
1131 struct rte_mempool_cache *cache;
1132 cache = rte_mempool_default_cache(mp, rte_lcore_id());
1133 rte_mempool_generic_put(mp, obj_table, n, cache, 0);
1138 * Put several objects back in the mempool (NOT multi-producers safe).
1141 * A pointer to the mempool structure.
1143 * A pointer to a table of void * pointers (objects).
1145 * The number of objects to add in the mempool from obj_table.
1148 static inline void __attribute__((always_inline))
1149 rte_mempool_sp_put_bulk(struct rte_mempool *mp, void * const *obj_table,
1152 rte_mempool_generic_put(mp, obj_table, n, NULL, MEMPOOL_F_SP_PUT);
1156 * Put several objects back in the mempool.
1158 * This function calls the multi-producer or the single-producer
1159 * version depending on the default behavior that was specified at
1160 * mempool creation time (see flags).
1163 * A pointer to the mempool structure.
1165 * A pointer to a table of void * pointers (objects).
1167 * The number of objects to add in the mempool from obj_table.
1169 static inline void __attribute__((always_inline))
1170 rte_mempool_put_bulk(struct rte_mempool *mp, void * const *obj_table,
1173 struct rte_mempool_cache *cache;
1174 cache = rte_mempool_default_cache(mp, rte_lcore_id());
1175 rte_mempool_generic_put(mp, obj_table, n, cache, mp->flags);
1180 * Put one object in the mempool (multi-producers safe).
1183 * A pointer to the mempool structure.
1185 * A pointer to the object to be added.
1188 static inline void __attribute__((always_inline))
1189 rte_mempool_mp_put(struct rte_mempool *mp, void *obj)
1191 struct rte_mempool_cache *cache;
1192 cache = rte_mempool_default_cache(mp, rte_lcore_id());
1193 rte_mempool_generic_put(mp, &obj, 1, cache, 0);
1198 * Put one object back in the mempool (NOT multi-producers safe).
1201 * A pointer to the mempool structure.
1203 * A pointer to the object to be added.
1206 static inline void __attribute__((always_inline))
1207 rte_mempool_sp_put(struct rte_mempool *mp, void *obj)
1209 rte_mempool_generic_put(mp, &obj, 1, NULL, MEMPOOL_F_SP_PUT);
1213 * Put one object back in the mempool.
1215 * This function calls the multi-producer or the single-producer
1216 * version depending on the default behavior that was specified at
1217 * mempool creation time (see flags).
1220 * A pointer to the mempool structure.
1222 * A pointer to the object to be added.
1224 static inline void __attribute__((always_inline))
1225 rte_mempool_put(struct rte_mempool *mp, void *obj)
1227 rte_mempool_put_bulk(mp, &obj, 1);
1231 * @internal Get several objects from the mempool; used internally.
1233 * A pointer to the mempool structure.
1235 * A pointer to a table of void * pointers (objects).
1237 * The number of objects to get, must be strictly positive.
1239 * A pointer to a mempool cache structure. May be NULL if not needed.
1241 * The flags used for the mempool creation.
1242 * Single-consumer (MEMPOOL_F_SC_GET flag) or multi-consumers.
1244 * - >=0: Success; number of objects supplied.
1245 * - <0: Error; code of ring dequeue function.
1247 static inline int __attribute__((always_inline))
1248 __mempool_generic_get(struct rte_mempool *mp, void **obj_table,
1249 unsigned n, struct rte_mempool_cache *cache, int flags)
1252 uint32_t index, len;
1255 /* No cache provided or single consumer */
1256 if (unlikely(cache == NULL || flags & MEMPOOL_F_SC_GET ||
1260 cache_objs = cache->objs;
1262 /* Can this be satisfied from the cache? */
1263 if (cache->len < n) {
1264 /* No. Backfill the cache first, and then fill from it */
1265 uint32_t req = n + (cache->size - cache->len);
1267 /* How many do we require i.e. number to fill the cache + the request */
1268 ret = rte_mempool_ops_dequeue_bulk(mp,
1269 &cache->objs[cache->len], req);
1270 if (unlikely(ret < 0)) {
1272 * In the offchance that we are buffer constrained,
1273 * where we are not able to allocate cache + n, go to
1274 * the ring directly. If that fails, we are truly out of
1283 /* Now fill in the response ... */
1284 for (index = 0, len = cache->len - 1; index < n; ++index, len--, obj_table++)
1285 *obj_table = cache_objs[len];
1289 __MEMPOOL_STAT_ADD(mp, get_success, n);
1295 /* get remaining objects from ring */
1296 ret = rte_mempool_ops_dequeue_bulk(mp, obj_table, n);
1299 __MEMPOOL_STAT_ADD(mp, get_fail, n);
1301 __MEMPOOL_STAT_ADD(mp, get_success, n);
1307 * Get several objects from the mempool.
1309 * If cache is enabled, objects will be retrieved first from cache,
1310 * subsequently from the common pool. Note that it can return -ENOENT when
1311 * the local cache and common pool are empty, even if cache from other
1315 * A pointer to the mempool structure.
1317 * A pointer to a table of void * pointers (objects) that will be filled.
1319 * The number of objects to get from mempool to obj_table.
1321 * A pointer to a mempool cache structure. May be NULL if not needed.
1323 * The flags used for the mempool creation.
1324 * Single-consumer (MEMPOOL_F_SC_GET flag) or multi-consumers.
1326 * - 0: Success; objects taken.
1327 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
1329 static inline int __attribute__((always_inline))
1330 rte_mempool_generic_get(struct rte_mempool *mp, void **obj_table, unsigned n,
1331 struct rte_mempool_cache *cache, int flags)
1334 ret = __mempool_generic_get(mp, obj_table, n, cache, flags);
1336 __mempool_check_cookies(mp, obj_table, n, 1);
1342 * Get several objects from the mempool (multi-consumers safe).
1344 * If cache is enabled, objects will be retrieved first from cache,
1345 * subsequently from the common pool. Note that it can return -ENOENT when
1346 * the local cache and common pool are empty, even if cache from other
1350 * A pointer to the mempool structure.
1352 * A pointer to a table of void * pointers (objects) that will be filled.
1354 * The number of objects to get from mempool to obj_table.
1356 * - 0: Success; objects taken.
1357 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
1360 static inline int __attribute__((always_inline))
1361 rte_mempool_mc_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned n)
1363 struct rte_mempool_cache *cache;
1364 cache = rte_mempool_default_cache(mp, rte_lcore_id());
1365 return rte_mempool_generic_get(mp, obj_table, n, cache, 0);
1370 * Get several objects from the mempool (NOT multi-consumers safe).
1372 * If cache is enabled, objects will be retrieved first from cache,
1373 * subsequently from the common pool. Note that it can return -ENOENT when
1374 * the local cache and common pool are empty, even if cache from other
1378 * A pointer to the mempool structure.
1380 * A pointer to a table of void * pointers (objects) that will be filled.
1382 * The number of objects to get from the mempool to obj_table.
1384 * - 0: Success; objects taken.
1385 * - -ENOENT: Not enough entries in the mempool; no object is
1389 static inline int __attribute__((always_inline))
1390 rte_mempool_sc_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned n)
1392 return rte_mempool_generic_get(mp, obj_table, n, NULL,
1397 * Get several objects from the mempool.
1399 * This function calls the multi-consumers or the single-consumer
1400 * version, depending on the default behaviour that was specified at
1401 * mempool creation time (see flags).
1403 * If cache is enabled, objects will be retrieved first from cache,
1404 * subsequently from the common pool. Note that it can return -ENOENT when
1405 * the local cache and common pool are empty, even if cache from other
1409 * A pointer to the mempool structure.
1411 * A pointer to a table of void * pointers (objects) that will be filled.
1413 * The number of objects to get from the mempool to obj_table.
1415 * - 0: Success; objects taken
1416 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
1418 static inline int __attribute__((always_inline))
1419 rte_mempool_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned n)
1421 struct rte_mempool_cache *cache;
1422 cache = rte_mempool_default_cache(mp, rte_lcore_id());
1423 return rte_mempool_generic_get(mp, obj_table, n, cache, mp->flags);
1428 * Get one object from the mempool (multi-consumers safe).
1430 * If cache is enabled, objects will be retrieved first from cache,
1431 * subsequently from the common pool. Note that it can return -ENOENT when
1432 * the local cache and common pool are empty, even if cache from other
1436 * A pointer to the mempool structure.
1438 * A pointer to a void * pointer (object) that will be filled.
1440 * - 0: Success; objects taken.
1441 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
1444 static inline int __attribute__((always_inline))
1445 rte_mempool_mc_get(struct rte_mempool *mp, void **obj_p)
1447 struct rte_mempool_cache *cache;
1448 cache = rte_mempool_default_cache(mp, rte_lcore_id());
1449 return rte_mempool_generic_get(mp, obj_p, 1, cache, 0);
1454 * Get one object from the mempool (NOT multi-consumers safe).
1456 * If cache is enabled, objects will be retrieved first from cache,
1457 * subsequently from the common pool. Note that it can return -ENOENT when
1458 * the local cache and common pool are empty, even if cache from other
1462 * A pointer to the mempool structure.
1464 * A pointer to a void * pointer (object) that will be filled.
1466 * - 0: Success; objects taken.
1467 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
1470 static inline int __attribute__((always_inline))
1471 rte_mempool_sc_get(struct rte_mempool *mp, void **obj_p)
1473 return rte_mempool_generic_get(mp, obj_p, 1, NULL, MEMPOOL_F_SC_GET);
1477 * Get one object from the mempool.
1479 * This function calls the multi-consumers or the single-consumer
1480 * version, depending on the default behavior that was specified at
1481 * mempool creation (see flags).
1483 * If cache is enabled, objects will be retrieved first from cache,
1484 * subsequently from the common pool. Note that it can return -ENOENT when
1485 * the local cache and common pool are empty, even if cache from other
1489 * A pointer to the mempool structure.
1491 * A pointer to a void * pointer (object) that will be filled.
1493 * - 0: Success; objects taken.
1494 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
1496 static inline int __attribute__((always_inline))
1497 rte_mempool_get(struct rte_mempool *mp, void **obj_p)
1499 return rte_mempool_get_bulk(mp, obj_p, 1);
1503 * Return the number of entries in the mempool.
1505 * When cache is enabled, this function has to browse the length of
1506 * all lcores, so it should not be used in a data path, but only for
1507 * debug purposes. User-owned mempool caches are not accounted for.
1510 * A pointer to the mempool structure.
1512 * The number of entries in the mempool.
1514 unsigned int rte_mempool_avail_count(const struct rte_mempool *mp);
1518 * Return the number of entries in the mempool.
1520 * When cache is enabled, this function has to browse the length of
1521 * all lcores, so it should not be used in a data path, but only for
1525 * A pointer to the mempool structure.
1527 * The number of entries in the mempool.
1530 unsigned rte_mempool_count(const struct rte_mempool *mp);
1533 * Return the number of elements which have been allocated from the mempool
1535 * When cache is enabled, this function has to browse the length of
1536 * all lcores, so it should not be used in a data path, but only for
1540 * A pointer to the mempool structure.
1542 * The number of free entries in the mempool.
1545 rte_mempool_in_use_count(const struct rte_mempool *mp);
1549 * Return the number of free entries in the mempool ring.
1550 * i.e. how many entries can be freed back to the mempool.
1552 * NOTE: This corresponds to the number of elements *allocated* from the
1553 * memory pool, not the number of elements in the pool itself. To count
1554 * the number elements currently available in the pool, use "rte_mempool_count"
1556 * When cache is enabled, this function has to browse the length of
1557 * all lcores, so it should not be used in a data path, but only for
1558 * debug purposes. User-owned mempool caches are not accounted for.
1561 * A pointer to the mempool structure.
1563 * The number of free entries in the mempool.
1566 static inline unsigned
1567 rte_mempool_free_count(const struct rte_mempool *mp)
1569 return rte_mempool_in_use_count(mp);
1573 * Test if the mempool is full.
1575 * When cache is enabled, this function has to browse the length of all
1576 * lcores, so it should not be used in a data path, but only for debug
1577 * purposes. User-owned mempool caches are not accounted for.
1580 * A pointer to the mempool structure.
1582 * - 1: The mempool is full.
1583 * - 0: The mempool is not full.
1586 rte_mempool_full(const struct rte_mempool *mp)
1588 return !!(rte_mempool_avail_count(mp) == mp->size);
1592 * Test if the mempool is empty.
1594 * When cache is enabled, this function has to browse the length of all
1595 * lcores, so it should not be used in a data path, but only for debug
1596 * purposes. User-owned mempool caches are not accounted for.
1599 * A pointer to the mempool structure.
1601 * - 1: The mempool is empty.
1602 * - 0: The mempool is not empty.
1605 rte_mempool_empty(const struct rte_mempool *mp)
1607 return !!(rte_mempool_avail_count(mp) == 0);
1611 * Return the physical address of elt, which is an element of the pool mp.
1614 * A pointer to the mempool structure.
1616 * A pointer (virtual address) to the element of the pool.
1618 * The physical address of the elt element.
1619 * If the mempool was created with MEMPOOL_F_NO_PHYS_CONTIG, the
1620 * returned value is RTE_BAD_PHYS_ADDR.
1622 static inline phys_addr_t
1623 rte_mempool_virt2phy(__rte_unused const struct rte_mempool *mp, const void *elt)
1625 const struct rte_mempool_objhdr *hdr;
1626 hdr = (const struct rte_mempool_objhdr *)RTE_PTR_SUB(elt,
1628 return hdr->physaddr;
1632 * Check the consistency of mempool objects.
1634 * Verify the coherency of fields in the mempool structure. Also check
1635 * that the cookies of mempool objects (even the ones that are not
1636 * present in pool) have a correct value. If not, a panic will occur.
1639 * A pointer to the mempool structure.
1641 void rte_mempool_audit(struct rte_mempool *mp);
1644 * Return a pointer to the private data in an mempool structure.
1647 * A pointer to the mempool structure.
1649 * A pointer to the private data.
1651 static inline void *rte_mempool_get_priv(struct rte_mempool *mp)
1654 MEMPOOL_HEADER_SIZE(mp, mp->cache_size);
1658 * Dump the status of all mempools on the console
1661 * A pointer to a file for output
1663 void rte_mempool_list_dump(FILE *f);
1666 * Search a mempool from its name
1669 * The name of the mempool.
1671 * The pointer to the mempool matching the name, or NULL if not found.
1673 * with rte_errno set appropriately. Possible rte_errno values include:
1674 * - ENOENT - required entry not available to return.
1677 struct rte_mempool *rte_mempool_lookup(const char *name);
1680 * Get the header, trailer and total size of a mempool element.
1682 * Given a desired size of the mempool element and mempool flags,
1683 * calculates header, trailer, body and total sizes of the mempool object.
1686 * The size of each element, without header and trailer.
1688 * The flags used for the mempool creation.
1689 * Consult rte_mempool_create() for more information about possible values.
1690 * The size of each element.
1692 * The calculated detailed size the mempool object. May be NULL.
1694 * Total size of the mempool object.
1696 uint32_t rte_mempool_calc_obj_size(uint32_t elt_size, uint32_t flags,
1697 struct rte_mempool_objsz *sz);
1700 * Get the size of memory required to store mempool elements.
1702 * Calculate the maximum amount of memory required to store given number
1703 * of objects. Assume that the memory buffer will be aligned at page
1706 * Note that if object size is bigger then page size, then it assumes
1707 * that pages are grouped in subsets of physically continuous pages big
1708 * enough to store at least one object.
1711 * Number of elements.
1712 * @param total_elt_sz
1713 * The size of each element, including header and trailer, as returned
1714 * by rte_mempool_calc_obj_size().
1716 * LOG2 of the physical pages size. If set to 0, ignore page boundaries.
1718 * Required memory size aligned at page boundary.
1720 size_t rte_mempool_xmem_size(uint32_t elt_num, size_t total_elt_sz,
1724 * Get the size of memory required to store mempool elements.
1726 * Calculate how much memory would be actually required with the given
1727 * memory footprint to store required number of objects.
1730 * Virtual address of the externally allocated memory buffer.
1731 * Will be used to store mempool objects.
1733 * Number of elements.
1734 * @param total_elt_sz
1735 * The size of each element, including header and trailer, as returned
1736 * by rte_mempool_calc_obj_size().
1738 * Array of physical addresses of the pages that comprises given memory
1741 * Number of elements in the paddr array.
1743 * LOG2 of the physical pages size.
1745 * On success, the number of bytes needed to store given number of
1746 * objects, aligned to the given page size. If the provided memory
1747 * buffer is too small, return a negative value whose absolute value
1748 * is the actual number of elements that can be stored in that buffer.
1750 ssize_t rte_mempool_xmem_usage(void *vaddr, uint32_t elt_num,
1751 size_t total_elt_sz, const phys_addr_t paddr[], uint32_t pg_num,
1755 * Walk list of all memory pools
1760 * Argument passed to iterator
1762 void rte_mempool_walk(void (*func)(struct rte_mempool *, void *arg),
1769 #endif /* _RTE_MEMPOOL_H_ */