4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5 * Copyright(c) 2016 6WIND S.A.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
18 * * Neither the name of Intel Corporation nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #ifndef _RTE_MEMPOOL_H_
36 #define _RTE_MEMPOOL_H_
42 * A memory pool is an allocator of fixed-size object. It is
43 * identified by its name, and uses a ring to store free objects. It
44 * provides some other optional services, like a per-core object
45 * cache, and an alignment helper to ensure that objects are padded
46 * to spread them equally on all RAM channels, ranks, and so on.
48 * Objects owned by a mempool should never be added in another
49 * mempool. When an object is freed using rte_mempool_put() or
50 * equivalent, the object data is not modified; the user can save some
51 * meta-data in the object data and retrieve them when allocating a
54 * Note: the mempool implementation is not preemptable. A lcore must
55 * not be interrupted by another task that uses the same mempool
56 * (because it uses a ring which is not preemptable). Also, mempool
57 * functions must not be used outside the DPDK environment: for
58 * example, in linuxapp environment, a thread that is not created by
59 * the EAL must not use mempools. This is due to the per-lcore cache
60 * that won't work as rte_lcore_id() will not return a correct value.
68 #include <sys/queue.h>
71 #include <rte_debug.h>
72 #include <rte_lcore.h>
73 #include <rte_memory.h>
74 #include <rte_branch_prediction.h>
81 #define RTE_MEMPOOL_HEADER_COOKIE1 0xbadbadbadadd2e55ULL /**< Header cookie. */
82 #define RTE_MEMPOOL_HEADER_COOKIE2 0xf2eef2eedadd2e55ULL /**< Header cookie. */
83 #define RTE_MEMPOOL_TRAILER_COOKIE 0xadd2e55badbadbadULL /**< Trailer cookie.*/
85 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
87 * A structure that stores the mempool statistics (per-lcore).
89 struct rte_mempool_debug_stats {
90 uint64_t put_bulk; /**< Number of puts. */
91 uint64_t put_objs; /**< Number of objects successfully put. */
92 uint64_t get_success_bulk; /**< Successful allocation number. */
93 uint64_t get_success_objs; /**< Objects successfully allocated. */
94 uint64_t get_fail_bulk; /**< Failed allocation number. */
95 uint64_t get_fail_objs; /**< Objects that failed to be allocated. */
96 } __rte_cache_aligned;
100 * A structure that stores a per-core object cache.
102 struct rte_mempool_cache {
103 unsigned len; /**< Cache len */
105 * Cache is allocated to this size to allow it to overflow in certain
106 * cases to avoid needless emptying of cache.
108 void *objs[RTE_MEMPOOL_CACHE_MAX_SIZE * 3]; /**< Cache objects */
109 } __rte_cache_aligned;
112 * A structure that stores the size of mempool elements.
114 struct rte_mempool_objsz {
115 uint32_t elt_size; /**< Size of an element. */
116 uint32_t header_size; /**< Size of header (before elt). */
117 uint32_t trailer_size; /**< Size of trailer (after elt). */
119 /**< Total size of an object (header + elt + trailer). */
122 #define RTE_MEMPOOL_NAMESIZE 32 /**< Maximum length of a memory pool. */
123 #define RTE_MEMPOOL_MZ_PREFIX "MP_"
126 #define RTE_MEMPOOL_MZ_FORMAT RTE_MEMPOOL_MZ_PREFIX "%s"
128 #ifdef RTE_LIBRTE_XEN_DOM0
130 /* "<name>_MP_elt" */
131 #define RTE_MEMPOOL_OBJ_NAME "%s_" RTE_MEMPOOL_MZ_PREFIX "elt"
135 #define RTE_MEMPOOL_OBJ_NAME RTE_MEMPOOL_MZ_FORMAT
137 #endif /* RTE_LIBRTE_XEN_DOM0 */
139 #define MEMPOOL_PG_SHIFT_MAX (sizeof(uintptr_t) * CHAR_BIT - 1)
141 /** Mempool over one chunk of physically continuous memory */
142 #define MEMPOOL_PG_NUM_DEFAULT 1
144 #ifndef RTE_MEMPOOL_ALIGN
145 #define RTE_MEMPOOL_ALIGN RTE_CACHE_LINE_SIZE
148 #define RTE_MEMPOOL_ALIGN_MASK (RTE_MEMPOOL_ALIGN - 1)
151 * Mempool object header structure
153 * Each object stored in mempools are prefixed by this header structure,
154 * it allows to retrieve the mempool pointer from the object and to
155 * iterate on all objects attached to a mempool. When debug is enabled,
156 * a cookie is also added in this structure preventing corruptions and
159 struct rte_mempool_objhdr {
160 STAILQ_ENTRY(rte_mempool_objhdr) next; /**< Next in list. */
161 struct rte_mempool *mp; /**< The mempool owning the object. */
162 phys_addr_t physaddr; /**< Physical address of the object. */
163 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
164 uint64_t cookie; /**< Debug cookie. */
169 * A list of object headers type
171 STAILQ_HEAD(rte_mempool_objhdr_list, rte_mempool_objhdr);
174 * Mempool object trailer structure
176 * In debug mode, each object stored in mempools are suffixed by this
177 * trailer structure containing a cookie preventing memory corruptions.
179 struct rte_mempool_objtlr {
180 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
181 uint64_t cookie; /**< Debug cookie. */
186 * A list of memory where objects are stored
188 STAILQ_HEAD(rte_mempool_memhdr_list, rte_mempool_memhdr);
191 * Callback used to free a memory chunk
193 typedef void (rte_mempool_memchunk_free_cb_t)(struct rte_mempool_memhdr *memhdr,
197 * Mempool objects memory header structure
199 * The memory chunks where objects are stored. Each chunk is virtually
200 * and physically contiguous.
202 struct rte_mempool_memhdr {
203 STAILQ_ENTRY(rte_mempool_memhdr) next; /**< Next in list. */
204 struct rte_mempool *mp; /**< The mempool owning the chunk */
205 void *addr; /**< Virtual address of the chunk */
206 phys_addr_t phys_addr; /**< Physical address of the chunk */
207 size_t len; /**< length of the chunk */
208 rte_mempool_memchunk_free_cb_t *free_cb; /**< Free callback */
209 void *opaque; /**< Argument passed to the free callback */
213 * The RTE mempool structure.
216 char name[RTE_MEMPOOL_NAMESIZE]; /**< Name of mempool. */
217 struct rte_ring *ring; /**< Ring to store objects. */
218 phys_addr_t phys_addr; /**< Phys. addr. of mempool struct. */
219 int flags; /**< Flags of the mempool. */
220 int socket_id; /**< Socket id passed at mempool creation. */
221 uint32_t size; /**< Max size of the mempool. */
222 uint32_t cache_size; /**< Size of per-lcore local cache. */
223 uint32_t cache_flushthresh;
224 /**< Threshold before we flush excess elements. */
226 uint32_t elt_size; /**< Size of an element. */
227 uint32_t header_size; /**< Size of header (before elt). */
228 uint32_t trailer_size; /**< Size of trailer (after elt). */
230 unsigned private_data_size; /**< Size of private data. */
232 struct rte_mempool_cache *local_cache; /**< Per-lcore local cache */
234 uint32_t populated_size; /**< Number of populated objects. */
235 struct rte_mempool_objhdr_list elt_list; /**< List of objects in pool */
236 uint32_t nb_mem_chunks; /**< Number of memory chunks */
237 struct rte_mempool_memhdr_list mem_list; /**< List of memory chunks */
239 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
240 /** Per-lcore statistics. */
241 struct rte_mempool_debug_stats stats[RTE_MAX_LCORE];
243 } __rte_cache_aligned;
245 #define MEMPOOL_F_NO_SPREAD 0x0001 /**< Do not spread among memory channels. */
246 #define MEMPOOL_F_NO_CACHE_ALIGN 0x0002 /**< Do not align objs on cache lines.*/
247 #define MEMPOOL_F_SP_PUT 0x0004 /**< Default put is "single-producer".*/
248 #define MEMPOOL_F_SC_GET 0x0008 /**< Default get is "single-consumer".*/
251 * @internal When debug is enabled, store some statistics.
254 * Pointer to the memory pool.
256 * Name of the statistics field to increment in the memory pool.
258 * Number to add to the object-oriented statistics.
260 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
261 #define __MEMPOOL_STAT_ADD(mp, name, n) do { \
262 unsigned __lcore_id = rte_lcore_id(); \
263 if (__lcore_id < RTE_MAX_LCORE) { \
264 mp->stats[__lcore_id].name##_objs += n; \
265 mp->stats[__lcore_id].name##_bulk += 1; \
269 #define __MEMPOOL_STAT_ADD(mp, name, n) do {} while(0)
273 * Calculate the size of the mempool header.
276 * Pointer to the memory pool.
278 * Size of the per-lcore cache.
280 #define MEMPOOL_HEADER_SIZE(mp, cs) \
281 (sizeof(*(mp)) + (((cs) == 0) ? 0 : \
282 (sizeof(struct rte_mempool_cache) * RTE_MAX_LCORE)))
284 /* return the header of a mempool object (internal) */
285 static inline struct rte_mempool_objhdr *__mempool_get_header(void *obj)
287 return (struct rte_mempool_objhdr *)RTE_PTR_SUB(obj,
288 sizeof(struct rte_mempool_objhdr));
292 * Return a pointer to the mempool owning this object.
295 * An object that is owned by a pool. If this is not the case,
296 * the behavior is undefined.
298 * A pointer to the mempool structure.
300 static inline struct rte_mempool *rte_mempool_from_obj(void *obj)
302 struct rte_mempool_objhdr *hdr = __mempool_get_header(obj);
306 /* return the trailer of a mempool object (internal) */
307 static inline struct rte_mempool_objtlr *__mempool_get_trailer(void *obj)
309 struct rte_mempool *mp = rte_mempool_from_obj(obj);
310 return (struct rte_mempool_objtlr *)RTE_PTR_ADD(obj, mp->elt_size);
314 * @internal Check and update cookies or panic.
317 * Pointer to the memory pool.
318 * @param obj_table_const
319 * Pointer to a table of void * pointers (objects).
321 * Index of object in object table.
323 * - 0: object is supposed to be allocated, mark it as free
324 * - 1: object is supposed to be free, mark it as allocated
325 * - 2: just check that cookie is valid (free or allocated)
327 void rte_mempool_check_cookies(const struct rte_mempool *mp,
328 void * const *obj_table_const, unsigned n, int free);
330 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
331 #define __mempool_check_cookies(mp, obj_table_const, n, free) \
332 rte_mempool_check_cookies(mp, obj_table_const, n, free)
334 #define __mempool_check_cookies(mp, obj_table_const, n, free) do {} while(0)
335 #endif /* RTE_LIBRTE_MEMPOOL_DEBUG */
338 * An object callback function for mempool.
340 * Used by rte_mempool_create() and rte_mempool_obj_iter().
342 typedef void (rte_mempool_obj_cb_t)(struct rte_mempool *mp,
343 void *opaque, void *obj, unsigned obj_idx);
344 typedef rte_mempool_obj_cb_t rte_mempool_obj_ctor_t; /* compat */
347 * A memory callback function for mempool.
349 * Used by rte_mempool_mem_iter().
351 typedef void (rte_mempool_mem_cb_t)(struct rte_mempool *mp,
352 void *opaque, struct rte_mempool_memhdr *memhdr,
356 * A mempool constructor callback function.
358 * Arguments are the mempool and the opaque pointer given by the user in
359 * rte_mempool_create().
361 typedef void (rte_mempool_ctor_t)(struct rte_mempool *, void *);
364 * Create a new mempool named *name* in memory.
366 * This function uses ``memzone_reserve()`` to allocate memory. The
367 * pool contains n elements of elt_size. Its size is set to n.
368 * All elements of the mempool are allocated together with the mempool header,
369 * in one physically continuous chunk of memory.
372 * The name of the mempool.
374 * The number of elements in the mempool. The optimum size (in terms of
375 * memory usage) for a mempool is when n is a power of two minus one:
378 * The size of each element.
380 * If cache_size is non-zero, the rte_mempool library will try to
381 * limit the accesses to the common lockless pool, by maintaining a
382 * per-lcore object cache. This argument must be lower or equal to
383 * CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE and n / 1.5. It is advised to choose
384 * cache_size to have "n modulo cache_size == 0": if this is
385 * not the case, some elements will always stay in the pool and will
386 * never be used. The access to the per-lcore table is of course
387 * faster than the multi-producer/consumer pool. The cache can be
388 * disabled if the cache_size argument is set to 0; it can be useful to
389 * avoid losing objects in cache. Note that even if not used, the
390 * memory space for cache is always reserved in a mempool structure,
391 * except if CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE is set to 0.
392 * @param private_data_size
393 * The size of the private data appended after the mempool
394 * structure. This is useful for storing some private data after the
395 * mempool structure, as is done for rte_mbuf_pool for example.
397 * A function pointer that is called for initialization of the pool,
398 * before object initialization. The user can initialize the private
399 * data in this function if needed. This parameter can be NULL if
402 * An opaque pointer to data that can be used in the mempool
403 * constructor function.
405 * A function pointer that is called for each object at
406 * initialization of the pool. The user can set some meta data in
407 * objects if needed. This parameter can be NULL if not needed.
408 * The obj_init() function takes the mempool pointer, the init_arg,
409 * the object pointer and the object number as parameters.
410 * @param obj_init_arg
411 * An opaque pointer to data that can be used as an argument for
412 * each call to the object constructor function.
414 * The *socket_id* argument is the socket identifier in the case of
415 * NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
416 * constraint for the reserved zone.
418 * The *flags* arguments is an OR of following flags:
419 * - MEMPOOL_F_NO_SPREAD: By default, objects addresses are spread
420 * between channels in RAM: the pool allocator will add padding
421 * between objects depending on the hardware configuration. See
422 * Memory alignment constraints for details. If this flag is set,
423 * the allocator will just align them to a cache line.
424 * - MEMPOOL_F_NO_CACHE_ALIGN: By default, the returned objects are
425 * cache-aligned. This flag removes this constraint, and no
426 * padding will be present between objects. This flag implies
427 * MEMPOOL_F_NO_SPREAD.
428 * - MEMPOOL_F_SP_PUT: If this flag is set, the default behavior
429 * when using rte_mempool_put() or rte_mempool_put_bulk() is
430 * "single-producer". Otherwise, it is "multi-producers".
431 * - MEMPOOL_F_SC_GET: If this flag is set, the default behavior
432 * when using rte_mempool_get() or rte_mempool_get_bulk() is
433 * "single-consumer". Otherwise, it is "multi-consumers".
435 * The pointer to the new allocated mempool, on success. NULL on error
436 * with rte_errno set appropriately. Possible rte_errno values include:
437 * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
438 * - E_RTE_SECONDARY - function was called from a secondary process instance
439 * - EINVAL - cache size provided is too large
440 * - ENOSPC - the maximum number of memzones has already been allocated
441 * - EEXIST - a memzone with the same name already exists
442 * - ENOMEM - no appropriate memory area found in which to create memzone
445 rte_mempool_create(const char *name, unsigned n, unsigned elt_size,
446 unsigned cache_size, unsigned private_data_size,
447 rte_mempool_ctor_t *mp_init, void *mp_init_arg,
448 rte_mempool_obj_cb_t *obj_init, void *obj_init_arg,
449 int socket_id, unsigned flags);
452 * Create a new mempool named *name* in memory.
454 * The pool contains n elements of elt_size. Its size is set to n.
455 * This function uses ``memzone_reserve()`` to allocate the mempool header
456 * (and the objects if vaddr is NULL).
457 * Depending on the input parameters, mempool elements can be either allocated
458 * together with the mempool header, or an externally provided memory buffer
459 * could be used to store mempool objects. In later case, that external
460 * memory buffer can consist of set of disjoint physical pages.
463 * The name of the mempool.
465 * The number of elements in the mempool. The optimum size (in terms of
466 * memory usage) for a mempool is when n is a power of two minus one:
469 * The size of each element.
471 * Size of the cache. See rte_mempool_create() for details.
472 * @param private_data_size
473 * The size of the private data appended after the mempool
474 * structure. This is useful for storing some private data after the
475 * mempool structure, as is done for rte_mbuf_pool for example.
477 * A function pointer that is called for initialization of the pool,
478 * before object initialization. The user can initialize the private
479 * data in this function if needed. This parameter can be NULL if
482 * An opaque pointer to data that can be used in the mempool
483 * constructor function.
485 * A function called for each object at initialization of the pool.
486 * See rte_mempool_create() for details.
487 * @param obj_init_arg
488 * An opaque pointer passed to the object constructor function.
490 * The *socket_id* argument is the socket identifier in the case of
491 * NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
492 * constraint for the reserved zone.
494 * Flags controlling the behavior of the mempool. See
495 * rte_mempool_create() for details.
497 * Virtual address of the externally allocated memory buffer.
498 * Will be used to store mempool objects.
500 * Array of physical addresses of the pages that comprises given memory
503 * Number of elements in the paddr array.
505 * LOG2 of the physical pages size.
507 * The pointer to the new allocated mempool, on success. NULL on error
508 * with rte_errno set appropriately. See rte_mempool_create() for details.
511 rte_mempool_xmem_create(const char *name, unsigned n, unsigned elt_size,
512 unsigned cache_size, unsigned private_data_size,
513 rte_mempool_ctor_t *mp_init, void *mp_init_arg,
514 rte_mempool_obj_cb_t *obj_init, void *obj_init_arg,
515 int socket_id, unsigned flags, void *vaddr,
516 const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift);
519 * Create a new mempool named *name* in memory on Xen Dom0.
521 * This function uses ``rte_mempool_xmem_create()`` to allocate memory. The
522 * pool contains n elements of elt_size. Its size is set to n.
523 * All elements of the mempool are allocated together with the mempool header,
524 * and memory buffer can consist of set of disjoint physical pages.
527 * The name of the mempool.
529 * The number of elements in the mempool. The optimum size (in terms of
530 * memory usage) for a mempool is when n is a power of two minus one:
533 * The size of each element.
535 * If cache_size is non-zero, the rte_mempool library will try to
536 * limit the accesses to the common lockless pool, by maintaining a
537 * per-lcore object cache. This argument must be lower or equal to
538 * CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE. It is advised to choose
539 * cache_size to have "n modulo cache_size == 0": if this is
540 * not the case, some elements will always stay in the pool and will
541 * never be used. The access to the per-lcore table is of course
542 * faster than the multi-producer/consumer pool. The cache can be
543 * disabled if the cache_size argument is set to 0; it can be useful to
544 * avoid losing objects in cache. Note that even if not used, the
545 * memory space for cache is always reserved in a mempool structure,
546 * except if CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE is set to 0.
547 * @param private_data_size
548 * The size of the private data appended after the mempool
549 * structure. This is useful for storing some private data after the
550 * mempool structure, as is done for rte_mbuf_pool for example.
552 * A function pointer that is called for initialization of the pool,
553 * before object initialization. The user can initialize the private
554 * data in this function if needed. This parameter can be NULL if
557 * An opaque pointer to data that can be used in the mempool
558 * constructor function.
560 * A function pointer that is called for each object at
561 * initialization of the pool. The user can set some meta data in
562 * objects if needed. This parameter can be NULL if not needed.
563 * The obj_init() function takes the mempool pointer, the init_arg,
564 * the object pointer and the object number as parameters.
565 * @param obj_init_arg
566 * An opaque pointer to data that can be used as an argument for
567 * each call to the object constructor function.
569 * The *socket_id* argument is the socket identifier in the case of
570 * NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
571 * constraint for the reserved zone.
573 * The *flags* arguments is an OR of following flags:
574 * - MEMPOOL_F_NO_SPREAD: By default, objects addresses are spread
575 * between channels in RAM: the pool allocator will add padding
576 * between objects depending on the hardware configuration. See
577 * Memory alignment constraints for details. If this flag is set,
578 * the allocator will just align them to a cache line.
579 * - MEMPOOL_F_NO_CACHE_ALIGN: By default, the returned objects are
580 * cache-aligned. This flag removes this constraint, and no
581 * padding will be present between objects. This flag implies
582 * MEMPOOL_F_NO_SPREAD.
583 * - MEMPOOL_F_SP_PUT: If this flag is set, the default behavior
584 * when using rte_mempool_put() or rte_mempool_put_bulk() is
585 * "single-producer". Otherwise, it is "multi-producers".
586 * - MEMPOOL_F_SC_GET: If this flag is set, the default behavior
587 * when using rte_mempool_get() or rte_mempool_get_bulk() is
588 * "single-consumer". Otherwise, it is "multi-consumers".
590 * The pointer to the new allocated mempool, on success. NULL on error
591 * with rte_errno set appropriately. Possible rte_errno values include:
592 * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
593 * - E_RTE_SECONDARY - function was called from a secondary process instance
594 * - EINVAL - cache size provided is too large
595 * - ENOSPC - the maximum number of memzones has already been allocated
596 * - EEXIST - a memzone with the same name already exists
597 * - ENOMEM - no appropriate memory area found in which to create memzone
600 rte_dom0_mempool_create(const char *name, unsigned n, unsigned elt_size,
601 unsigned cache_size, unsigned private_data_size,
602 rte_mempool_ctor_t *mp_init, void *mp_init_arg,
603 rte_mempool_obj_cb_t *obj_init, void *obj_init_arg,
604 int socket_id, unsigned flags);
608 * Call a function for each mempool element
610 * Iterate across all objects attached to a rte_mempool and call the
611 * callback function on it.
614 * A pointer to an initialized mempool.
616 * A function pointer that is called for each object.
618 * An opaque pointer passed to the callback function.
620 * Number of objects iterated.
622 uint32_t rte_mempool_obj_iter(struct rte_mempool *mp,
623 rte_mempool_obj_cb_t *obj_cb, void *obj_cb_arg);
626 * Call a function for each mempool memory chunk
628 * Iterate across all memory chunks attached to a rte_mempool and call
629 * the callback function on it.
632 * A pointer to an initialized mempool.
634 * A function pointer that is called for each memory chunk.
636 * An opaque pointer passed to the callback function.
638 * Number of memory chunks iterated.
640 uint32_t rte_mempool_mem_iter(struct rte_mempool *mp,
641 rte_mempool_mem_cb_t *mem_cb, void *mem_cb_arg);
644 * Dump the status of the mempool to the console.
647 * A pointer to a file for output
649 * A pointer to the mempool structure.
651 void rte_mempool_dump(FILE *f, struct rte_mempool *mp);
654 * @internal Put several objects back in the mempool; used internally.
656 * A pointer to the mempool structure.
658 * A pointer to a table of void * pointers (objects).
660 * The number of objects to store back in the mempool, must be strictly
663 * Mono-producer (0) or multi-producers (1).
665 static inline void __attribute__((always_inline))
666 __mempool_put_bulk(struct rte_mempool *mp, void * const *obj_table,
667 unsigned n, int is_mp)
669 struct rte_mempool_cache *cache;
672 unsigned lcore_id = rte_lcore_id();
673 uint32_t cache_size = mp->cache_size;
674 uint32_t flushthresh = mp->cache_flushthresh;
676 /* increment stat now, adding in mempool always success */
677 __MEMPOOL_STAT_ADD(mp, put, n);
679 /* cache is not enabled or single producer or non-EAL thread */
680 if (unlikely(cache_size == 0 || is_mp == 0 ||
681 lcore_id >= RTE_MAX_LCORE))
684 /* Go straight to ring if put would overflow mem allocated for cache */
685 if (unlikely(n > RTE_MEMPOOL_CACHE_MAX_SIZE))
688 cache = &mp->local_cache[lcore_id];
689 cache_objs = &cache->objs[cache->len];
692 * The cache follows the following algorithm
693 * 1. Add the objects to the cache
694 * 2. Anything greater than the cache min value (if it crosses the
695 * cache flush threshold) is flushed to the ring.
698 /* Add elements back into the cache */
699 for (index = 0; index < n; ++index, obj_table++)
700 cache_objs[index] = *obj_table;
704 if (cache->len >= flushthresh) {
705 rte_ring_mp_enqueue_bulk(mp->ring, &cache->objs[cache_size],
706 cache->len - cache_size);
707 cache->len = cache_size;
714 /* push remaining objects in ring */
715 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
717 if (rte_ring_mp_enqueue_bulk(mp->ring, obj_table, n) < 0)
718 rte_panic("cannot put objects in mempool\n");
721 if (rte_ring_sp_enqueue_bulk(mp->ring, obj_table, n) < 0)
722 rte_panic("cannot put objects in mempool\n");
726 rte_ring_mp_enqueue_bulk(mp->ring, obj_table, n);
728 rte_ring_sp_enqueue_bulk(mp->ring, obj_table, n);
734 * Put several objects back in the mempool (multi-producers safe).
737 * A pointer to the mempool structure.
739 * A pointer to a table of void * pointers (objects).
741 * The number of objects to add in the mempool from the obj_table.
743 static inline void __attribute__((always_inline))
744 rte_mempool_mp_put_bulk(struct rte_mempool *mp, void * const *obj_table,
747 __mempool_check_cookies(mp, obj_table, n, 0);
748 __mempool_put_bulk(mp, obj_table, n, 1);
752 * Put several objects back in the mempool (NOT multi-producers safe).
755 * A pointer to the mempool structure.
757 * A pointer to a table of void * pointers (objects).
759 * The number of objects to add in the mempool from obj_table.
762 rte_mempool_sp_put_bulk(struct rte_mempool *mp, void * const *obj_table,
765 __mempool_check_cookies(mp, obj_table, n, 0);
766 __mempool_put_bulk(mp, obj_table, n, 0);
770 * Put several objects back in the mempool.
772 * This function calls the multi-producer or the single-producer
773 * version depending on the default behavior that was specified at
774 * mempool creation time (see flags).
777 * A pointer to the mempool structure.
779 * A pointer to a table of void * pointers (objects).
781 * The number of objects to add in the mempool from obj_table.
783 static inline void __attribute__((always_inline))
784 rte_mempool_put_bulk(struct rte_mempool *mp, void * const *obj_table,
787 __mempool_check_cookies(mp, obj_table, n, 0);
788 __mempool_put_bulk(mp, obj_table, n, !(mp->flags & MEMPOOL_F_SP_PUT));
792 * Put one object in the mempool (multi-producers safe).
795 * A pointer to the mempool structure.
797 * A pointer to the object to be added.
799 static inline void __attribute__((always_inline))
800 rte_mempool_mp_put(struct rte_mempool *mp, void *obj)
802 rte_mempool_mp_put_bulk(mp, &obj, 1);
806 * Put one object back in the mempool (NOT multi-producers safe).
809 * A pointer to the mempool structure.
811 * A pointer to the object to be added.
813 static inline void __attribute__((always_inline))
814 rte_mempool_sp_put(struct rte_mempool *mp, void *obj)
816 rte_mempool_sp_put_bulk(mp, &obj, 1);
820 * Put one object back in the mempool.
822 * This function calls the multi-producer or the single-producer
823 * version depending on the default behavior that was specified at
824 * mempool creation time (see flags).
827 * A pointer to the mempool structure.
829 * A pointer to the object to be added.
831 static inline void __attribute__((always_inline))
832 rte_mempool_put(struct rte_mempool *mp, void *obj)
834 rte_mempool_put_bulk(mp, &obj, 1);
838 * @internal Get several objects from the mempool; used internally.
840 * A pointer to the mempool structure.
842 * A pointer to a table of void * pointers (objects).
844 * The number of objects to get, must be strictly positive.
846 * Mono-consumer (0) or multi-consumers (1).
848 * - >=0: Success; number of objects supplied.
849 * - <0: Error; code of ring dequeue function.
851 static inline int __attribute__((always_inline))
852 __mempool_get_bulk(struct rte_mempool *mp, void **obj_table,
853 unsigned n, int is_mc)
856 struct rte_mempool_cache *cache;
859 unsigned lcore_id = rte_lcore_id();
860 uint32_t cache_size = mp->cache_size;
862 /* cache is not enabled or single consumer */
863 if (unlikely(cache_size == 0 || is_mc == 0 ||
864 n >= cache_size || lcore_id >= RTE_MAX_LCORE))
867 cache = &mp->local_cache[lcore_id];
868 cache_objs = cache->objs;
870 /* Can this be satisfied from the cache? */
871 if (cache->len < n) {
872 /* No. Backfill the cache first, and then fill from it */
873 uint32_t req = n + (cache_size - cache->len);
875 /* How many do we require i.e. number to fill the cache + the request */
876 ret = rte_ring_mc_dequeue_bulk(mp->ring, &cache->objs[cache->len], req);
877 if (unlikely(ret < 0)) {
879 * In the offchance that we are buffer constrained,
880 * where we are not able to allocate cache + n, go to
881 * the ring directly. If that fails, we are truly out of
890 /* Now fill in the response ... */
891 for (index = 0, len = cache->len - 1; index < n; ++index, len--, obj_table++)
892 *obj_table = cache_objs[len];
896 __MEMPOOL_STAT_ADD(mp, get_success, n);
902 /* get remaining objects from ring */
904 ret = rte_ring_mc_dequeue_bulk(mp->ring, obj_table, n);
906 ret = rte_ring_sc_dequeue_bulk(mp->ring, obj_table, n);
909 __MEMPOOL_STAT_ADD(mp, get_fail, n);
911 __MEMPOOL_STAT_ADD(mp, get_success, n);
917 * Get several objects from the mempool (multi-consumers safe).
919 * If cache is enabled, objects will be retrieved first from cache,
920 * subsequently from the common pool. Note that it can return -ENOENT when
921 * the local cache and common pool are empty, even if cache from other
925 * A pointer to the mempool structure.
927 * A pointer to a table of void * pointers (objects) that will be filled.
929 * The number of objects to get from mempool to obj_table.
931 * - 0: Success; objects taken.
932 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
934 static inline int __attribute__((always_inline))
935 rte_mempool_mc_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned n)
938 ret = __mempool_get_bulk(mp, obj_table, n, 1);
940 __mempool_check_cookies(mp, obj_table, n, 1);
945 * Get several objects from the mempool (NOT multi-consumers safe).
947 * If cache is enabled, objects will be retrieved first from cache,
948 * subsequently from the common pool. Note that it can return -ENOENT when
949 * the local cache and common pool are empty, even if cache from other
953 * A pointer to the mempool structure.
955 * A pointer to a table of void * pointers (objects) that will be filled.
957 * The number of objects to get from the mempool to obj_table.
959 * - 0: Success; objects taken.
960 * - -ENOENT: Not enough entries in the mempool; no object is
963 static inline int __attribute__((always_inline))
964 rte_mempool_sc_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned n)
967 ret = __mempool_get_bulk(mp, obj_table, n, 0);
969 __mempool_check_cookies(mp, obj_table, n, 1);
974 * Get several objects from the mempool.
976 * This function calls the multi-consumers or the single-consumer
977 * version, depending on the default behaviour that was specified at
978 * mempool creation time (see flags).
980 * If cache is enabled, objects will be retrieved first from cache,
981 * subsequently from the common pool. Note that it can return -ENOENT when
982 * the local cache and common pool are empty, even if cache from other
986 * A pointer to the mempool structure.
988 * A pointer to a table of void * pointers (objects) that will be filled.
990 * The number of objects to get from the mempool to obj_table.
992 * - 0: Success; objects taken
993 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
995 static inline int __attribute__((always_inline))
996 rte_mempool_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned n)
999 ret = __mempool_get_bulk(mp, obj_table, n,
1000 !(mp->flags & MEMPOOL_F_SC_GET));
1002 __mempool_check_cookies(mp, obj_table, n, 1);
1007 * Get one object from the mempool (multi-consumers safe).
1009 * If cache is enabled, objects will be retrieved first from cache,
1010 * subsequently from the common pool. Note that it can return -ENOENT when
1011 * the local cache and common pool are empty, even if cache from other
1015 * A pointer to the mempool structure.
1017 * A pointer to a void * pointer (object) that will be filled.
1019 * - 0: Success; objects taken.
1020 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
1022 static inline int __attribute__((always_inline))
1023 rte_mempool_mc_get(struct rte_mempool *mp, void **obj_p)
1025 return rte_mempool_mc_get_bulk(mp, obj_p, 1);
1029 * Get one object from the mempool (NOT multi-consumers safe).
1031 * If cache is enabled, objects will be retrieved first from cache,
1032 * subsequently from the common pool. Note that it can return -ENOENT when
1033 * the local cache and common pool are empty, even if cache from other
1037 * A pointer to the mempool structure.
1039 * A pointer to a void * pointer (object) that will be filled.
1041 * - 0: Success; objects taken.
1042 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
1044 static inline int __attribute__((always_inline))
1045 rte_mempool_sc_get(struct rte_mempool *mp, void **obj_p)
1047 return rte_mempool_sc_get_bulk(mp, obj_p, 1);
1051 * Get one object from the mempool.
1053 * This function calls the multi-consumers or the single-consumer
1054 * version, depending on the default behavior that was specified at
1055 * mempool creation (see flags).
1057 * If cache is enabled, objects will be retrieved first from cache,
1058 * subsequently from the common pool. Note that it can return -ENOENT when
1059 * the local cache and common pool are empty, even if cache from other
1063 * A pointer to the mempool structure.
1065 * A pointer to a void * pointer (object) that will be filled.
1067 * - 0: Success; objects taken.
1068 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
1070 static inline int __attribute__((always_inline))
1071 rte_mempool_get(struct rte_mempool *mp, void **obj_p)
1073 return rte_mempool_get_bulk(mp, obj_p, 1);
1077 * Return the number of entries in the mempool.
1079 * When cache is enabled, this function has to browse the length of
1080 * all lcores, so it should not be used in a data path, but only for
1084 * A pointer to the mempool structure.
1086 * The number of entries in the mempool.
1088 unsigned rte_mempool_count(const struct rte_mempool *mp);
1091 * Return the number of free entries in the mempool ring.
1092 * i.e. how many entries can be freed back to the mempool.
1094 * NOTE: This corresponds to the number of elements *allocated* from the
1095 * memory pool, not the number of elements in the pool itself. To count
1096 * the number elements currently available in the pool, use "rte_mempool_count"
1098 * When cache is enabled, this function has to browse the length of
1099 * all lcores, so it should not be used in a data path, but only for
1103 * A pointer to the mempool structure.
1105 * The number of free entries in the mempool.
1107 static inline unsigned
1108 rte_mempool_free_count(const struct rte_mempool *mp)
1110 return mp->size - rte_mempool_count(mp);
1114 * Test if the mempool is full.
1116 * When cache is enabled, this function has to browse the length of all
1117 * lcores, so it should not be used in a data path, but only for debug
1121 * A pointer to the mempool structure.
1123 * - 1: The mempool is full.
1124 * - 0: The mempool is not full.
1127 rte_mempool_full(const struct rte_mempool *mp)
1129 return !!(rte_mempool_count(mp) == mp->size);
1133 * Test if the mempool is empty.
1135 * When cache is enabled, this function has to browse the length of all
1136 * lcores, so it should not be used in a data path, but only for debug
1140 * A pointer to the mempool structure.
1142 * - 1: The mempool is empty.
1143 * - 0: The mempool is not empty.
1146 rte_mempool_empty(const struct rte_mempool *mp)
1148 return !!(rte_mempool_count(mp) == 0);
1152 * Return the physical address of elt, which is an element of the pool mp.
1155 * A pointer to the mempool structure.
1157 * A pointer (virtual address) to the element of the pool.
1159 * The physical address of the elt element.
1161 static inline phys_addr_t
1162 rte_mempool_virt2phy(__rte_unused const struct rte_mempool *mp, const void *elt)
1164 if (rte_eal_has_hugepages()) {
1165 const struct rte_mempool_objhdr *hdr;
1166 hdr = (const struct rte_mempool_objhdr *)RTE_PTR_SUB(elt,
1168 return hdr->physaddr;
1171 * If huge pages are disabled, we cannot assume the
1172 * memory region to be physically contiguous.
1173 * Lookup for each element.
1175 return rte_mem_virt2phy(elt);
1180 * Check the consistency of mempool objects.
1182 * Verify the coherency of fields in the mempool structure. Also check
1183 * that the cookies of mempool objects (even the ones that are not
1184 * present in pool) have a correct value. If not, a panic will occur.
1187 * A pointer to the mempool structure.
1189 void rte_mempool_audit(struct rte_mempool *mp);
1192 * Return a pointer to the private data in an mempool structure.
1195 * A pointer to the mempool structure.
1197 * A pointer to the private data.
1199 static inline void *rte_mempool_get_priv(struct rte_mempool *mp)
1202 MEMPOOL_HEADER_SIZE(mp, mp->cache_size);
1206 * Dump the status of all mempools on the console
1209 * A pointer to a file for output
1211 void rte_mempool_list_dump(FILE *f);
1214 * Search a mempool from its name
1217 * The name of the mempool.
1219 * The pointer to the mempool matching the name, or NULL if not found.
1221 * with rte_errno set appropriately. Possible rte_errno values include:
1222 * - ENOENT - required entry not available to return.
1225 struct rte_mempool *rte_mempool_lookup(const char *name);
1228 * Get the header, trailer and total size of a mempool element.
1230 * Given a desired size of the mempool element and mempool flags,
1231 * calculates header, trailer, body and total sizes of the mempool object.
1234 * The size of each element, without header and trailer.
1236 * The flags used for the mempool creation.
1237 * Consult rte_mempool_create() for more information about possible values.
1238 * The size of each element.
1240 * The calculated detailed size the mempool object. May be NULL.
1242 * Total size of the mempool object.
1244 uint32_t rte_mempool_calc_obj_size(uint32_t elt_size, uint32_t flags,
1245 struct rte_mempool_objsz *sz);
1248 * Get the size of memory required to store mempool elements.
1250 * Calculate the maximum amount of memory required to store given number
1251 * of objects. Assume that the memory buffer will be aligned at page
1254 * Note that if object size is bigger then page size, then it assumes
1255 * that pages are grouped in subsets of physically continuous pages big
1256 * enough to store at least one object.
1259 * Number of elements.
1260 * @param total_elt_sz
1261 * The size of each element, including header and trailer, as returned
1262 * by rte_mempool_calc_obj_size().
1264 * LOG2 of the physical pages size. If set to 0, ignore page boundaries.
1266 * Required memory size aligned at page boundary.
1268 size_t rte_mempool_xmem_size(uint32_t elt_num, size_t total_elt_sz,
1272 * Get the size of memory required to store mempool elements.
1274 * Calculate how much memory would be actually required with the given
1275 * memory footprint to store required number of objects.
1278 * Virtual address of the externally allocated memory buffer.
1279 * Will be used to store mempool objects.
1281 * Number of elements.
1282 * @param total_elt_sz
1283 * The size of each element, including header and trailer, as returned
1284 * by rte_mempool_calc_obj_size().
1286 * Array of physical addresses of the pages that comprises given memory
1289 * Number of elements in the paddr array.
1291 * LOG2 of the physical pages size.
1293 * On success, the number of bytes needed to store given number of
1294 * objects, aligned to the given page size. If the provided memory
1295 * buffer is too small, return a negative value whose absolute value
1296 * is the actual number of elements that can be stored in that buffer.
1298 ssize_t rte_mempool_xmem_usage(void *vaddr, uint32_t elt_num,
1299 size_t total_elt_sz, const phys_addr_t paddr[], uint32_t pg_num,
1303 * Walk list of all memory pools
1308 * Argument passed to iterator
1310 void rte_mempool_walk(void (*func)(struct rte_mempool *, void *arg),
1317 #endif /* _RTE_MEMPOOL_H_ */