4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #ifndef _RTE_MEMPOOL_H_
35 #define _RTE_MEMPOOL_H_
41 * A memory pool is an allocator of fixed-size object. It is
42 * identified by its name, and uses a ring to store free objects. It
43 * provides some other optional services, like a per-core object
44 * cache, and an alignment helper to ensure that objects are padded
45 * to spread them equally on all RAM channels, ranks, and so on.
47 * Objects owned by a mempool should never be added in another
48 * mempool. When an object is freed using rte_mempool_put() or
49 * equivalent, the object data is not modified; the user can save some
50 * meta-data in the object data and retrieve them when allocating a
53 * Note: the mempool implementation is not preemptable. A lcore must
54 * not be interrupted by another task that uses the same mempool
55 * (because it uses a ring which is not preemptable). Also, mempool
56 * functions must not be used outside the DPDK environment: for
57 * example, in linuxapp environment, a thread that is not created by
58 * the EAL must not use mempools. This is due to the per-lcore cache
59 * that won't work as rte_lcore_id() will not return a correct value.
67 #include <sys/queue.h>
70 #include <rte_debug.h>
71 #include <rte_lcore.h>
72 #include <rte_memory.h>
73 #include <rte_branch_prediction.h>
80 #define RTE_MEMPOOL_HEADER_COOKIE1 0xbadbadbadadd2e55ULL /**< Header cookie. */
81 #define RTE_MEMPOOL_HEADER_COOKIE2 0xf2eef2eedadd2e55ULL /**< Header cookie. */
82 #define RTE_MEMPOOL_TRAILER_COOKIE 0xadd2e55badbadbadULL /**< Trailer cookie.*/
84 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
86 * A structure that stores the mempool statistics (per-lcore).
88 struct rte_mempool_debug_stats {
89 uint64_t put_bulk; /**< Number of puts. */
90 uint64_t put_objs; /**< Number of objects successfully put. */
91 uint64_t get_success_bulk; /**< Successful allocation number. */
92 uint64_t get_success_objs; /**< Objects successfully allocated. */
93 uint64_t get_fail_bulk; /**< Failed allocation number. */
94 uint64_t get_fail_objs; /**< Objects that failed to be allocated. */
95 } __rte_cache_aligned;
99 * A structure that stores a per-core object cache.
101 struct rte_mempool_cache {
102 unsigned len; /**< Cache len */
104 * Cache is allocated to this size to allow it to overflow in certain
105 * cases to avoid needless emptying of cache.
107 void *objs[RTE_MEMPOOL_CACHE_MAX_SIZE * 3]; /**< Cache objects */
108 } __rte_cache_aligned;
111 * A structure that stores the size of mempool elements.
113 struct rte_mempool_objsz {
114 uint32_t elt_size; /**< Size of an element. */
115 uint32_t header_size; /**< Size of header (before elt). */
116 uint32_t trailer_size; /**< Size of trailer (after elt). */
118 /**< Total size of an object (header + elt + trailer). */
121 #define RTE_MEMPOOL_NAMESIZE 32 /**< Maximum length of a memory pool. */
122 #define RTE_MEMPOOL_MZ_PREFIX "MP_"
125 #define RTE_MEMPOOL_MZ_FORMAT RTE_MEMPOOL_MZ_PREFIX "%s"
127 #ifdef RTE_LIBRTE_XEN_DOM0
129 /* "<name>_MP_elt" */
130 #define RTE_MEMPOOL_OBJ_NAME "%s_" RTE_MEMPOOL_MZ_PREFIX "elt"
134 #define RTE_MEMPOOL_OBJ_NAME RTE_MEMPOOL_MZ_FORMAT
136 #endif /* RTE_LIBRTE_XEN_DOM0 */
138 #define MEMPOOL_PG_SHIFT_MAX (sizeof(uintptr_t) * CHAR_BIT - 1)
140 /** Mempool over one chunk of physically continuous memory */
141 #define MEMPOOL_PG_NUM_DEFAULT 1
143 #ifndef RTE_MEMPOOL_ALIGN
144 #define RTE_MEMPOOL_ALIGN RTE_CACHE_LINE_SIZE
147 #define RTE_MEMPOOL_ALIGN_MASK (RTE_MEMPOOL_ALIGN - 1)
150 * Mempool object header structure
152 * Each object stored in mempools are prefixed by this header structure,
153 * it allows to retrieve the mempool pointer from the object. When debug
154 * is enabled, a cookie is also added in this structure preventing
155 * corruptions and double-frees.
157 struct rte_mempool_objhdr {
158 struct rte_mempool *mp; /**< The mempool owning the object. */
159 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
160 uint64_t cookie; /**< Debug cookie. */
165 * Mempool object trailer structure
167 * In debug mode, each object stored in mempools are suffixed by this
168 * trailer structure containing a cookie preventing memory corruptions.
170 struct rte_mempool_objtlr {
171 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
172 uint64_t cookie; /**< Debug cookie. */
177 * The RTE mempool structure.
180 char name[RTE_MEMPOOL_NAMESIZE]; /**< Name of mempool. */
181 struct rte_ring *ring; /**< Ring to store objects. */
182 phys_addr_t phys_addr; /**< Phys. addr. of mempool struct. */
183 int flags; /**< Flags of the mempool. */
184 uint32_t size; /**< Size of the mempool. */
185 uint32_t cache_size; /**< Size of per-lcore local cache. */
186 uint32_t cache_flushthresh;
187 /**< Threshold before we flush excess elements. */
189 uint32_t elt_size; /**< Size of an element. */
190 uint32_t header_size; /**< Size of header (before elt). */
191 uint32_t trailer_size; /**< Size of trailer (after elt). */
193 unsigned private_data_size; /**< Size of private data. */
195 struct rte_mempool_cache *local_cache; /**< Per-lcore local cache */
197 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
198 /** Per-lcore statistics. */
199 struct rte_mempool_debug_stats stats[RTE_MAX_LCORE];
202 /* Address translation support, starts from next cache line. */
204 /** Number of elements in the elt_pa array. */
205 uint32_t pg_num __rte_cache_aligned;
206 uint32_t pg_shift; /**< LOG2 of the physical pages. */
207 uintptr_t pg_mask; /**< physical page mask value. */
208 uintptr_t elt_va_start;
209 /**< Virtual address of the first mempool object. */
210 uintptr_t elt_va_end;
211 /**< Virtual address of the <size + 1> mempool object. */
212 phys_addr_t elt_pa[MEMPOOL_PG_NUM_DEFAULT];
213 /**< Array of physical page addresses for the mempool objects buffer. */
215 } __rte_cache_aligned;
217 #define MEMPOOL_F_NO_SPREAD 0x0001 /**< Do not spread among memory channels. */
218 #define MEMPOOL_F_NO_CACHE_ALIGN 0x0002 /**< Do not align objs on cache lines.*/
219 #define MEMPOOL_F_SP_PUT 0x0004 /**< Default put is "single-producer".*/
220 #define MEMPOOL_F_SC_GET 0x0008 /**< Default get is "single-consumer".*/
223 * @internal When debug is enabled, store some statistics.
226 * Pointer to the memory pool.
228 * Name of the statistics field to increment in the memory pool.
230 * Number to add to the object-oriented statistics.
232 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
233 #define __MEMPOOL_STAT_ADD(mp, name, n) do { \
234 unsigned __lcore_id = rte_lcore_id(); \
235 if (__lcore_id < RTE_MAX_LCORE) { \
236 mp->stats[__lcore_id].name##_objs += n; \
237 mp->stats[__lcore_id].name##_bulk += 1; \
241 #define __MEMPOOL_STAT_ADD(mp, name, n) do {} while(0)
245 * Size of elt_pa array size based on number of pages. (Internal use)
247 #define __PA_SIZE(mp, pgn) \
248 RTE_ALIGN_CEIL((((pgn) - RTE_DIM((mp)->elt_pa)) * \
249 sizeof((mp)->elt_pa[0])), RTE_CACHE_LINE_SIZE)
252 * Calculate the size of the mempool header.
255 * Pointer to the memory pool.
257 * Number of pages used to store mempool objects.
259 * Size of the per-lcore cache.
261 #define MEMPOOL_HEADER_SIZE(mp, pgn, cs) \
262 (sizeof(*(mp)) + __PA_SIZE(mp, pgn) + (((cs) == 0) ? 0 : \
263 (sizeof(struct rte_mempool_cache) * RTE_MAX_LCORE)))
266 * Return true if the whole mempool is in contiguous memory.
268 #define MEMPOOL_IS_CONTIG(mp) \
269 ((mp)->pg_num == MEMPOOL_PG_NUM_DEFAULT && \
270 (mp)->phys_addr == (mp)->elt_pa[0])
272 /* return the header of a mempool object (internal) */
273 static inline struct rte_mempool_objhdr *__mempool_get_header(void *obj)
275 return (struct rte_mempool_objhdr *)RTE_PTR_SUB(obj,
276 sizeof(struct rte_mempool_objhdr));
280 * Return a pointer to the mempool owning this object.
283 * An object that is owned by a pool. If this is not the case,
284 * the behavior is undefined.
286 * A pointer to the mempool structure.
288 static inline struct rte_mempool *rte_mempool_from_obj(void *obj)
290 struct rte_mempool_objhdr *hdr = __mempool_get_header(obj);
294 /* return the trailer of a mempool object (internal) */
295 static inline struct rte_mempool_objtlr *__mempool_get_trailer(void *obj)
297 struct rte_mempool *mp = rte_mempool_from_obj(obj);
298 return (struct rte_mempool_objtlr *)RTE_PTR_ADD(obj, mp->elt_size);
302 * @internal Check and update cookies or panic.
305 * Pointer to the memory pool.
306 * @param obj_table_const
307 * Pointer to a table of void * pointers (objects).
309 * Index of object in object table.
311 * - 0: object is supposed to be allocated, mark it as free
312 * - 1: object is supposed to be free, mark it as allocated
313 * - 2: just check that cookie is valid (free or allocated)
315 void rte_mempool_check_cookies(const struct rte_mempool *mp,
316 void * const *obj_table_const, unsigned n, int free);
318 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
319 #define __mempool_check_cookies(mp, obj_table_const, n, free) \
320 rte_mempool_check_cookies(mp, obj_table_const, n, free)
322 #define __mempool_check_cookies(mp, obj_table_const, n, free) do {} while(0)
323 #endif /* RTE_LIBRTE_MEMPOOL_DEBUG */
326 * An object callback function for mempool.
328 * Arguments are the mempool, the opaque pointer given by the user in
329 * rte_mempool_create(), the pointer to the element and the index of
330 * the element in the pool.
332 typedef void (rte_mempool_obj_cb_t)(struct rte_mempool *mp,
333 void *opaque, void *obj, unsigned obj_idx);
334 typedef rte_mempool_obj_cb_t rte_mempool_obj_ctor_t; /* compat */
337 * A mempool object iterator callback function.
339 typedef void (*rte_mempool_obj_iter_t)(void * /*obj_iter_arg*/,
340 void * /*obj_start*/,
342 uint32_t /*obj_index */);
345 * Call a function for each mempool object in a memory chunk
347 * Iterate across objects of the given size and alignment in the
348 * provided chunk of memory. The given memory buffer can consist of
349 * disjointed physical pages.
351 * For each object, call the provided callback (if any). This function
352 * is used to populate a mempool, or walk through all the elements of a
353 * mempool, or estimate how many elements of the given size could be
354 * created in the given memory buffer.
357 * Virtual address of the memory buffer.
359 * Maximum number of objects to iterate through.
361 * Size of each object.
363 * Alignment of each object.
365 * Array of physical addresses of the pages that comprises given memory
368 * Number of elements in the paddr array.
370 * LOG2 of the physical pages size.
372 * Object iterator callback function (could be NULL).
373 * @param obj_iter_arg
374 * User defined parameter for the object iterator callback function.
377 * Number of objects iterated through.
379 uint32_t rte_mempool_obj_iter(void *vaddr,
380 uint32_t elt_num, size_t elt_sz, size_t align,
381 const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift,
382 rte_mempool_obj_iter_t obj_iter, void *obj_iter_arg);
385 * A mempool constructor callback function.
387 * Arguments are the mempool and the opaque pointer given by the user in
388 * rte_mempool_create().
390 typedef void (rte_mempool_ctor_t)(struct rte_mempool *, void *);
393 * Create a new mempool named *name* in memory.
395 * This function uses ``memzone_reserve()`` to allocate memory. The
396 * pool contains n elements of elt_size. Its size is set to n.
397 * All elements of the mempool are allocated together with the mempool header,
398 * in one physically continuous chunk of memory.
401 * The name of the mempool.
403 * The number of elements in the mempool. The optimum size (in terms of
404 * memory usage) for a mempool is when n is a power of two minus one:
407 * The size of each element.
409 * If cache_size is non-zero, the rte_mempool library will try to
410 * limit the accesses to the common lockless pool, by maintaining a
411 * per-lcore object cache. This argument must be lower or equal to
412 * CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE and n / 1.5. It is advised to choose
413 * cache_size to have "n modulo cache_size == 0": if this is
414 * not the case, some elements will always stay in the pool and will
415 * never be used. The access to the per-lcore table is of course
416 * faster than the multi-producer/consumer pool. The cache can be
417 * disabled if the cache_size argument is set to 0; it can be useful to
418 * avoid losing objects in cache. Note that even if not used, the
419 * memory space for cache is always reserved in a mempool structure,
420 * except if CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE is set to 0.
421 * @param private_data_size
422 * The size of the private data appended after the mempool
423 * structure. This is useful for storing some private data after the
424 * mempool structure, as is done for rte_mbuf_pool for example.
426 * A function pointer that is called for initialization of the pool,
427 * before object initialization. The user can initialize the private
428 * data in this function if needed. This parameter can be NULL if
431 * An opaque pointer to data that can be used in the mempool
432 * constructor function.
434 * A function pointer that is called for each object at
435 * initialization of the pool. The user can set some meta data in
436 * objects if needed. This parameter can be NULL if not needed.
437 * The obj_init() function takes the mempool pointer, the init_arg,
438 * the object pointer and the object number as parameters.
439 * @param obj_init_arg
440 * An opaque pointer to data that can be used as an argument for
441 * each call to the object constructor function.
443 * The *socket_id* argument is the socket identifier in the case of
444 * NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
445 * constraint for the reserved zone.
447 * The *flags* arguments is an OR of following flags:
448 * - MEMPOOL_F_NO_SPREAD: By default, objects addresses are spread
449 * between channels in RAM: the pool allocator will add padding
450 * between objects depending on the hardware configuration. See
451 * Memory alignment constraints for details. If this flag is set,
452 * the allocator will just align them to a cache line.
453 * - MEMPOOL_F_NO_CACHE_ALIGN: By default, the returned objects are
454 * cache-aligned. This flag removes this constraint, and no
455 * padding will be present between objects. This flag implies
456 * MEMPOOL_F_NO_SPREAD.
457 * - MEMPOOL_F_SP_PUT: If this flag is set, the default behavior
458 * when using rte_mempool_put() or rte_mempool_put_bulk() is
459 * "single-producer". Otherwise, it is "multi-producers".
460 * - MEMPOOL_F_SC_GET: If this flag is set, the default behavior
461 * when using rte_mempool_get() or rte_mempool_get_bulk() is
462 * "single-consumer". Otherwise, it is "multi-consumers".
464 * The pointer to the new allocated mempool, on success. NULL on error
465 * with rte_errno set appropriately. Possible rte_errno values include:
466 * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
467 * - E_RTE_SECONDARY - function was called from a secondary process instance
468 * - EINVAL - cache size provided is too large
469 * - ENOSPC - the maximum number of memzones has already been allocated
470 * - EEXIST - a memzone with the same name already exists
471 * - ENOMEM - no appropriate memory area found in which to create memzone
474 rte_mempool_create(const char *name, unsigned n, unsigned elt_size,
475 unsigned cache_size, unsigned private_data_size,
476 rte_mempool_ctor_t *mp_init, void *mp_init_arg,
477 rte_mempool_obj_cb_t *obj_init, void *obj_init_arg,
478 int socket_id, unsigned flags);
481 * Create a new mempool named *name* in memory.
483 * The pool contains n elements of elt_size. Its size is set to n.
484 * This function uses ``memzone_reserve()`` to allocate the mempool header
485 * (and the objects if vaddr is NULL).
486 * Depending on the input parameters, mempool elements can be either allocated
487 * together with the mempool header, or an externally provided memory buffer
488 * could be used to store mempool objects. In later case, that external
489 * memory buffer can consist of set of disjoint physical pages.
492 * The name of the mempool.
494 * The number of elements in the mempool. The optimum size (in terms of
495 * memory usage) for a mempool is when n is a power of two minus one:
498 * The size of each element.
500 * Size of the cache. See rte_mempool_create() for details.
501 * @param private_data_size
502 * The size of the private data appended after the mempool
503 * structure. This is useful for storing some private data after the
504 * mempool structure, as is done for rte_mbuf_pool for example.
506 * A function pointer that is called for initialization of the pool,
507 * before object initialization. The user can initialize the private
508 * data in this function if needed. This parameter can be NULL if
511 * An opaque pointer to data that can be used in the mempool
512 * constructor function.
514 * A function called for each object at initialization of the pool.
515 * See rte_mempool_create() for details.
516 * @param obj_init_arg
517 * An opaque pointer passed to the object constructor function.
519 * The *socket_id* argument is the socket identifier in the case of
520 * NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
521 * constraint for the reserved zone.
523 * Flags controlling the behavior of the mempool. See
524 * rte_mempool_create() for details.
526 * Virtual address of the externally allocated memory buffer.
527 * Will be used to store mempool objects.
529 * Array of physical addresses of the pages that comprises given memory
532 * Number of elements in the paddr array.
534 * LOG2 of the physical pages size.
536 * The pointer to the new allocated mempool, on success. NULL on error
537 * with rte_errno set appropriately. See rte_mempool_create() for details.
540 rte_mempool_xmem_create(const char *name, unsigned n, unsigned elt_size,
541 unsigned cache_size, unsigned private_data_size,
542 rte_mempool_ctor_t *mp_init, void *mp_init_arg,
543 rte_mempool_obj_cb_t *obj_init, void *obj_init_arg,
544 int socket_id, unsigned flags, void *vaddr,
545 const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift);
548 * Create a new mempool named *name* in memory on Xen Dom0.
550 * This function uses ``rte_mempool_xmem_create()`` to allocate memory. The
551 * pool contains n elements of elt_size. Its size is set to n.
552 * All elements of the mempool are allocated together with the mempool header,
553 * and memory buffer can consist of set of disjoint physical pages.
556 * The name of the mempool.
558 * The number of elements in the mempool. The optimum size (in terms of
559 * memory usage) for a mempool is when n is a power of two minus one:
562 * The size of each element.
564 * If cache_size is non-zero, the rte_mempool library will try to
565 * limit the accesses to the common lockless pool, by maintaining a
566 * per-lcore object cache. This argument must be lower or equal to
567 * CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE. It is advised to choose
568 * cache_size to have "n modulo cache_size == 0": if this is
569 * not the case, some elements will always stay in the pool and will
570 * never be used. The access to the per-lcore table is of course
571 * faster than the multi-producer/consumer pool. The cache can be
572 * disabled if the cache_size argument is set to 0; it can be useful to
573 * avoid losing objects in cache. Note that even if not used, the
574 * memory space for cache is always reserved in a mempool structure,
575 * except if CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE is set to 0.
576 * @param private_data_size
577 * The size of the private data appended after the mempool
578 * structure. This is useful for storing some private data after the
579 * mempool structure, as is done for rte_mbuf_pool for example.
581 * A function pointer that is called for initialization of the pool,
582 * before object initialization. The user can initialize the private
583 * data in this function if needed. This parameter can be NULL if
586 * An opaque pointer to data that can be used in the mempool
587 * constructor function.
589 * A function pointer that is called for each object at
590 * initialization of the pool. The user can set some meta data in
591 * objects if needed. This parameter can be NULL if not needed.
592 * The obj_init() function takes the mempool pointer, the init_arg,
593 * the object pointer and the object number as parameters.
594 * @param obj_init_arg
595 * An opaque pointer to data that can be used as an argument for
596 * each call to the object constructor function.
598 * The *socket_id* argument is the socket identifier in the case of
599 * NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
600 * constraint for the reserved zone.
602 * The *flags* arguments is an OR of following flags:
603 * - MEMPOOL_F_NO_SPREAD: By default, objects addresses are spread
604 * between channels in RAM: the pool allocator will add padding
605 * between objects depending on the hardware configuration. See
606 * Memory alignment constraints for details. If this flag is set,
607 * the allocator will just align them to a cache line.
608 * - MEMPOOL_F_NO_CACHE_ALIGN: By default, the returned objects are
609 * cache-aligned. This flag removes this constraint, and no
610 * padding will be present between objects. This flag implies
611 * MEMPOOL_F_NO_SPREAD.
612 * - MEMPOOL_F_SP_PUT: If this flag is set, the default behavior
613 * when using rte_mempool_put() or rte_mempool_put_bulk() is
614 * "single-producer". Otherwise, it is "multi-producers".
615 * - MEMPOOL_F_SC_GET: If this flag is set, the default behavior
616 * when using rte_mempool_get() or rte_mempool_get_bulk() is
617 * "single-consumer". Otherwise, it is "multi-consumers".
619 * The pointer to the new allocated mempool, on success. NULL on error
620 * with rte_errno set appropriately. Possible rte_errno values include:
621 * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
622 * - E_RTE_SECONDARY - function was called from a secondary process instance
623 * - EINVAL - cache size provided is too large
624 * - ENOSPC - the maximum number of memzones has already been allocated
625 * - EEXIST - a memzone with the same name already exists
626 * - ENOMEM - no appropriate memory area found in which to create memzone
629 rte_dom0_mempool_create(const char *name, unsigned n, unsigned elt_size,
630 unsigned cache_size, unsigned private_data_size,
631 rte_mempool_ctor_t *mp_init, void *mp_init_arg,
632 rte_mempool_obj_cb_t *obj_init, void *obj_init_arg,
633 int socket_id, unsigned flags);
637 * Dump the status of the mempool to the console.
640 * A pointer to a file for output
642 * A pointer to the mempool structure.
644 void rte_mempool_dump(FILE *f, const struct rte_mempool *mp);
647 * @internal Put several objects back in the mempool; used internally.
649 * A pointer to the mempool structure.
651 * A pointer to a table of void * pointers (objects).
653 * The number of objects to store back in the mempool, must be strictly
656 * Mono-producer (0) or multi-producers (1).
658 static inline void __attribute__((always_inline))
659 __mempool_put_bulk(struct rte_mempool *mp, void * const *obj_table,
660 unsigned n, int is_mp)
662 struct rte_mempool_cache *cache;
665 unsigned lcore_id = rte_lcore_id();
666 uint32_t cache_size = mp->cache_size;
667 uint32_t flushthresh = mp->cache_flushthresh;
669 /* increment stat now, adding in mempool always success */
670 __MEMPOOL_STAT_ADD(mp, put, n);
672 /* cache is not enabled or single producer or non-EAL thread */
673 if (unlikely(cache_size == 0 || is_mp == 0 ||
674 lcore_id >= RTE_MAX_LCORE))
677 /* Go straight to ring if put would overflow mem allocated for cache */
678 if (unlikely(n > RTE_MEMPOOL_CACHE_MAX_SIZE))
681 cache = &mp->local_cache[lcore_id];
682 cache_objs = &cache->objs[cache->len];
685 * The cache follows the following algorithm
686 * 1. Add the objects to the cache
687 * 2. Anything greater than the cache min value (if it crosses the
688 * cache flush threshold) is flushed to the ring.
691 /* Add elements back into the cache */
692 for (index = 0; index < n; ++index, obj_table++)
693 cache_objs[index] = *obj_table;
697 if (cache->len >= flushthresh) {
698 rte_ring_mp_enqueue_bulk(mp->ring, &cache->objs[cache_size],
699 cache->len - cache_size);
700 cache->len = cache_size;
707 /* push remaining objects in ring */
708 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
710 if (rte_ring_mp_enqueue_bulk(mp->ring, obj_table, n) < 0)
711 rte_panic("cannot put objects in mempool\n");
714 if (rte_ring_sp_enqueue_bulk(mp->ring, obj_table, n) < 0)
715 rte_panic("cannot put objects in mempool\n");
719 rte_ring_mp_enqueue_bulk(mp->ring, obj_table, n);
721 rte_ring_sp_enqueue_bulk(mp->ring, obj_table, n);
727 * Put several objects back in the mempool (multi-producers safe).
730 * A pointer to the mempool structure.
732 * A pointer to a table of void * pointers (objects).
734 * The number of objects to add in the mempool from the obj_table.
736 static inline void __attribute__((always_inline))
737 rte_mempool_mp_put_bulk(struct rte_mempool *mp, void * const *obj_table,
740 __mempool_check_cookies(mp, obj_table, n, 0);
741 __mempool_put_bulk(mp, obj_table, n, 1);
745 * Put several objects back in the mempool (NOT multi-producers safe).
748 * A pointer to the mempool structure.
750 * A pointer to a table of void * pointers (objects).
752 * The number of objects to add in the mempool from obj_table.
755 rte_mempool_sp_put_bulk(struct rte_mempool *mp, void * const *obj_table,
758 __mempool_check_cookies(mp, obj_table, n, 0);
759 __mempool_put_bulk(mp, obj_table, n, 0);
763 * Put several objects back in the mempool.
765 * This function calls the multi-producer or the single-producer
766 * version depending on the default behavior that was specified at
767 * mempool creation time (see flags).
770 * A pointer to the mempool structure.
772 * A pointer to a table of void * pointers (objects).
774 * The number of objects to add in the mempool from obj_table.
776 static inline void __attribute__((always_inline))
777 rte_mempool_put_bulk(struct rte_mempool *mp, void * const *obj_table,
780 __mempool_check_cookies(mp, obj_table, n, 0);
781 __mempool_put_bulk(mp, obj_table, n, !(mp->flags & MEMPOOL_F_SP_PUT));
785 * Put one object in the mempool (multi-producers safe).
788 * A pointer to the mempool structure.
790 * A pointer to the object to be added.
792 static inline void __attribute__((always_inline))
793 rte_mempool_mp_put(struct rte_mempool *mp, void *obj)
795 rte_mempool_mp_put_bulk(mp, &obj, 1);
799 * Put one object back in the mempool (NOT multi-producers safe).
802 * A pointer to the mempool structure.
804 * A pointer to the object to be added.
806 static inline void __attribute__((always_inline))
807 rte_mempool_sp_put(struct rte_mempool *mp, void *obj)
809 rte_mempool_sp_put_bulk(mp, &obj, 1);
813 * Put one object back in the mempool.
815 * This function calls the multi-producer or the single-producer
816 * version depending on the default behavior that was specified at
817 * mempool creation time (see flags).
820 * A pointer to the mempool structure.
822 * A pointer to the object to be added.
824 static inline void __attribute__((always_inline))
825 rte_mempool_put(struct rte_mempool *mp, void *obj)
827 rte_mempool_put_bulk(mp, &obj, 1);
831 * @internal Get several objects from the mempool; used internally.
833 * A pointer to the mempool structure.
835 * A pointer to a table of void * pointers (objects).
837 * The number of objects to get, must be strictly positive.
839 * Mono-consumer (0) or multi-consumers (1).
841 * - >=0: Success; number of objects supplied.
842 * - <0: Error; code of ring dequeue function.
844 static inline int __attribute__((always_inline))
845 __mempool_get_bulk(struct rte_mempool *mp, void **obj_table,
846 unsigned n, int is_mc)
849 struct rte_mempool_cache *cache;
852 unsigned lcore_id = rte_lcore_id();
853 uint32_t cache_size = mp->cache_size;
855 /* cache is not enabled or single consumer */
856 if (unlikely(cache_size == 0 || is_mc == 0 ||
857 n >= cache_size || lcore_id >= RTE_MAX_LCORE))
860 cache = &mp->local_cache[lcore_id];
861 cache_objs = cache->objs;
863 /* Can this be satisfied from the cache? */
864 if (cache->len < n) {
865 /* No. Backfill the cache first, and then fill from it */
866 uint32_t req = n + (cache_size - cache->len);
868 /* How many do we require i.e. number to fill the cache + the request */
869 ret = rte_ring_mc_dequeue_bulk(mp->ring, &cache->objs[cache->len], req);
870 if (unlikely(ret < 0)) {
872 * In the offchance that we are buffer constrained,
873 * where we are not able to allocate cache + n, go to
874 * the ring directly. If that fails, we are truly out of
883 /* Now fill in the response ... */
884 for (index = 0, len = cache->len - 1; index < n; ++index, len--, obj_table++)
885 *obj_table = cache_objs[len];
889 __MEMPOOL_STAT_ADD(mp, get_success, n);
895 /* get remaining objects from ring */
897 ret = rte_ring_mc_dequeue_bulk(mp->ring, obj_table, n);
899 ret = rte_ring_sc_dequeue_bulk(mp->ring, obj_table, n);
902 __MEMPOOL_STAT_ADD(mp, get_fail, n);
904 __MEMPOOL_STAT_ADD(mp, get_success, n);
910 * Get several objects from the mempool (multi-consumers safe).
912 * If cache is enabled, objects will be retrieved first from cache,
913 * subsequently from the common pool. Note that it can return -ENOENT when
914 * the local cache and common pool are empty, even if cache from other
918 * A pointer to the mempool structure.
920 * A pointer to a table of void * pointers (objects) that will be filled.
922 * The number of objects to get from mempool to obj_table.
924 * - 0: Success; objects taken.
925 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
927 static inline int __attribute__((always_inline))
928 rte_mempool_mc_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned n)
931 ret = __mempool_get_bulk(mp, obj_table, n, 1);
933 __mempool_check_cookies(mp, obj_table, n, 1);
938 * Get several objects from the mempool (NOT multi-consumers safe).
940 * If cache is enabled, objects will be retrieved first from cache,
941 * subsequently from the common pool. Note that it can return -ENOENT when
942 * the local cache and common pool are empty, even if cache from other
946 * A pointer to the mempool structure.
948 * A pointer to a table of void * pointers (objects) that will be filled.
950 * The number of objects to get from the mempool to obj_table.
952 * - 0: Success; objects taken.
953 * - -ENOENT: Not enough entries in the mempool; no object is
956 static inline int __attribute__((always_inline))
957 rte_mempool_sc_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned n)
960 ret = __mempool_get_bulk(mp, obj_table, n, 0);
962 __mempool_check_cookies(mp, obj_table, n, 1);
967 * Get several objects from the mempool.
969 * This function calls the multi-consumers or the single-consumer
970 * version, depending on the default behaviour that was specified at
971 * mempool creation time (see flags).
973 * If cache is enabled, objects will be retrieved first from cache,
974 * subsequently from the common pool. Note that it can return -ENOENT when
975 * the local cache and common pool are empty, even if cache from other
979 * A pointer to the mempool structure.
981 * A pointer to a table of void * pointers (objects) that will be filled.
983 * The number of objects to get from the mempool to obj_table.
985 * - 0: Success; objects taken
986 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
988 static inline int __attribute__((always_inline))
989 rte_mempool_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned n)
992 ret = __mempool_get_bulk(mp, obj_table, n,
993 !(mp->flags & MEMPOOL_F_SC_GET));
995 __mempool_check_cookies(mp, obj_table, n, 1);
1000 * Get one object from the mempool (multi-consumers safe).
1002 * If cache is enabled, objects will be retrieved first from cache,
1003 * subsequently from the common pool. Note that it can return -ENOENT when
1004 * the local cache and common pool are empty, even if cache from other
1008 * A pointer to the mempool structure.
1010 * A pointer to a void * pointer (object) that will be filled.
1012 * - 0: Success; objects taken.
1013 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
1015 static inline int __attribute__((always_inline))
1016 rte_mempool_mc_get(struct rte_mempool *mp, void **obj_p)
1018 return rte_mempool_mc_get_bulk(mp, obj_p, 1);
1022 * Get one object from the mempool (NOT multi-consumers safe).
1024 * If cache is enabled, objects will be retrieved first from cache,
1025 * subsequently from the common pool. Note that it can return -ENOENT when
1026 * the local cache and common pool are empty, even if cache from other
1030 * A pointer to the mempool structure.
1032 * A pointer to a void * pointer (object) that will be filled.
1034 * - 0: Success; objects taken.
1035 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
1037 static inline int __attribute__((always_inline))
1038 rte_mempool_sc_get(struct rte_mempool *mp, void **obj_p)
1040 return rte_mempool_sc_get_bulk(mp, obj_p, 1);
1044 * Get one object from the mempool.
1046 * This function calls the multi-consumers or the single-consumer
1047 * version, depending on the default behavior that was specified at
1048 * mempool creation (see flags).
1050 * If cache is enabled, objects will be retrieved first from cache,
1051 * subsequently from the common pool. Note that it can return -ENOENT when
1052 * the local cache and common pool are empty, even if cache from other
1056 * A pointer to the mempool structure.
1058 * A pointer to a void * pointer (object) that will be filled.
1060 * - 0: Success; objects taken.
1061 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
1063 static inline int __attribute__((always_inline))
1064 rte_mempool_get(struct rte_mempool *mp, void **obj_p)
1066 return rte_mempool_get_bulk(mp, obj_p, 1);
1070 * Return the number of entries in the mempool.
1072 * When cache is enabled, this function has to browse the length of
1073 * all lcores, so it should not be used in a data path, but only for
1077 * A pointer to the mempool structure.
1079 * The number of entries in the mempool.
1081 unsigned rte_mempool_count(const struct rte_mempool *mp);
1084 * Return the number of free entries in the mempool ring.
1085 * i.e. how many entries can be freed back to the mempool.
1087 * NOTE: This corresponds to the number of elements *allocated* from the
1088 * memory pool, not the number of elements in the pool itself. To count
1089 * the number elements currently available in the pool, use "rte_mempool_count"
1091 * When cache is enabled, this function has to browse the length of
1092 * all lcores, so it should not be used in a data path, but only for
1096 * A pointer to the mempool structure.
1098 * The number of free entries in the mempool.
1100 static inline unsigned
1101 rte_mempool_free_count(const struct rte_mempool *mp)
1103 return mp->size - rte_mempool_count(mp);
1107 * Test if the mempool is full.
1109 * When cache is enabled, this function has to browse the length of all
1110 * lcores, so it should not be used in a data path, but only for debug
1114 * A pointer to the mempool structure.
1116 * - 1: The mempool is full.
1117 * - 0: The mempool is not full.
1120 rte_mempool_full(const struct rte_mempool *mp)
1122 return !!(rte_mempool_count(mp) == mp->size);
1126 * Test if the mempool is empty.
1128 * When cache is enabled, this function has to browse the length of all
1129 * lcores, so it should not be used in a data path, but only for debug
1133 * A pointer to the mempool structure.
1135 * - 1: The mempool is empty.
1136 * - 0: The mempool is not empty.
1139 rte_mempool_empty(const struct rte_mempool *mp)
1141 return !!(rte_mempool_count(mp) == 0);
1145 * Return the physical address of elt, which is an element of the pool mp.
1148 * A pointer to the mempool structure.
1150 * A pointer (virtual address) to the element of the pool.
1152 * The physical address of the elt element.
1154 static inline phys_addr_t
1155 rte_mempool_virt2phy(const struct rte_mempool *mp, const void *elt)
1157 if (rte_eal_has_hugepages()) {
1160 off = (const char *)elt - (const char *)mp->elt_va_start;
1161 return mp->elt_pa[off >> mp->pg_shift] + (off & mp->pg_mask);
1164 * If huge pages are disabled, we cannot assume the
1165 * memory region to be physically contiguous.
1166 * Lookup for each element.
1168 return rte_mem_virt2phy(elt);
1173 * Check the consistency of mempool objects.
1175 * Verify the coherency of fields in the mempool structure. Also check
1176 * that the cookies of mempool objects (even the ones that are not
1177 * present in pool) have a correct value. If not, a panic will occur.
1180 * A pointer to the mempool structure.
1182 void rte_mempool_audit(const struct rte_mempool *mp);
1185 * Return a pointer to the private data in an mempool structure.
1188 * A pointer to the mempool structure.
1190 * A pointer to the private data.
1192 static inline void *rte_mempool_get_priv(struct rte_mempool *mp)
1195 MEMPOOL_HEADER_SIZE(mp, mp->pg_num, mp->cache_size);
1199 * Dump the status of all mempools on the console
1202 * A pointer to a file for output
1204 void rte_mempool_list_dump(FILE *f);
1207 * Search a mempool from its name
1210 * The name of the mempool.
1212 * The pointer to the mempool matching the name, or NULL if not found.
1214 * with rte_errno set appropriately. Possible rte_errno values include:
1215 * - ENOENT - required entry not available to return.
1218 struct rte_mempool *rte_mempool_lookup(const char *name);
1221 * Get the header, trailer and total size of a mempool element.
1223 * Given a desired size of the mempool element and mempool flags,
1224 * calculates header, trailer, body and total sizes of the mempool object.
1227 * The size of each element, without header and trailer.
1229 * The flags used for the mempool creation.
1230 * Consult rte_mempool_create() for more information about possible values.
1231 * The size of each element.
1233 * The calculated detailed size the mempool object. May be NULL.
1235 * Total size of the mempool object.
1237 uint32_t rte_mempool_calc_obj_size(uint32_t elt_size, uint32_t flags,
1238 struct rte_mempool_objsz *sz);
1241 * Get the size of memory required to store mempool elements.
1243 * Calculate the maximum amount of memory required to store given number
1244 * of objects. Assume that the memory buffer will be aligned at page
1247 * Note that if object size is bigger then page size, then it assumes
1248 * that pages are grouped in subsets of physically continuous pages big
1249 * enough to store at least one object.
1252 * Number of elements.
1253 * @param total_elt_sz
1254 * The size of each element, including header and trailer, as returned
1255 * by rte_mempool_calc_obj_size().
1257 * LOG2 of the physical pages size.
1259 * Required memory size aligned at page boundary.
1261 size_t rte_mempool_xmem_size(uint32_t elt_num, size_t total_elt_sz,
1265 * Get the size of memory required to store mempool elements.
1267 * Calculate how much memory would be actually required with the given
1268 * memory footprint to store required number of objects.
1271 * Virtual address of the externally allocated memory buffer.
1272 * Will be used to store mempool objects.
1274 * Number of elements.
1275 * @param total_elt_sz
1276 * The size of each element, including header and trailer, as returned
1277 * by rte_mempool_calc_obj_size().
1279 * Array of physical addresses of the pages that comprises given memory
1282 * Number of elements in the paddr array.
1284 * LOG2 of the physical pages size.
1286 * On success, the number of bytes needed to store given number of
1287 * objects, aligned to the given page size. If the provided memory
1288 * buffer is too small, return a negative value whose absolute value
1289 * is the actual number of elements that can be stored in that buffer.
1291 ssize_t rte_mempool_xmem_usage(void *vaddr, uint32_t elt_num,
1292 size_t total_elt_sz, const phys_addr_t paddr[], uint32_t pg_num,
1296 * Walk list of all memory pools
1301 * Argument passed to iterator
1303 void rte_mempool_walk(void (*func)(const struct rte_mempool *, void *arg),
1310 #endif /* _RTE_MEMPOOL_H_ */