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 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
316 #ifndef __INTEL_COMPILER
317 #pragma GCC diagnostic ignored "-Wcast-qual"
319 static inline void __mempool_check_cookies(const struct rte_mempool *mp,
320 void * const *obj_table_const,
321 unsigned n, int free)
323 struct rte_mempool_objhdr *hdr;
324 struct rte_mempool_objtlr *tlr;
330 /* Force to drop the "const" attribute. This is done only when
331 * DEBUG is enabled */
332 tmp = (void *) obj_table_const;
333 obj_table = (void **) tmp;
338 if (rte_mempool_from_obj(obj) != mp)
339 rte_panic("MEMPOOL: object is owned by another "
342 hdr = __mempool_get_header(obj);
343 cookie = hdr->cookie;
346 if (cookie != RTE_MEMPOOL_HEADER_COOKIE1) {
347 rte_log_set_history(0);
348 RTE_LOG(CRIT, MEMPOOL,
349 "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
350 obj, (const void *) mp, cookie);
351 rte_panic("MEMPOOL: bad header cookie (put)\n");
353 hdr->cookie = RTE_MEMPOOL_HEADER_COOKIE2;
355 else if (free == 1) {
356 if (cookie != RTE_MEMPOOL_HEADER_COOKIE2) {
357 rte_log_set_history(0);
358 RTE_LOG(CRIT, MEMPOOL,
359 "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
360 obj, (const void *) mp, cookie);
361 rte_panic("MEMPOOL: bad header cookie (get)\n");
363 hdr->cookie = RTE_MEMPOOL_HEADER_COOKIE1;
365 else if (free == 2) {
366 if (cookie != RTE_MEMPOOL_HEADER_COOKIE1 &&
367 cookie != RTE_MEMPOOL_HEADER_COOKIE2) {
368 rte_log_set_history(0);
369 RTE_LOG(CRIT, MEMPOOL,
370 "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
371 obj, (const void *) mp, cookie);
372 rte_panic("MEMPOOL: bad header cookie (audit)\n");
375 tlr = __mempool_get_trailer(obj);
376 cookie = tlr->cookie;
377 if (cookie != RTE_MEMPOOL_TRAILER_COOKIE) {
378 rte_log_set_history(0);
379 RTE_LOG(CRIT, MEMPOOL,
380 "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
381 obj, (const void *) mp, cookie);
382 rte_panic("MEMPOOL: bad trailer cookie\n");
386 #ifndef __INTEL_COMPILER
387 #pragma GCC diagnostic error "-Wcast-qual"
390 #define __mempool_check_cookies(mp, obj_table_const, n, free) do {} while(0)
391 #endif /* RTE_LIBRTE_MEMPOOL_DEBUG */
394 * A mempool object iterator callback function.
396 typedef void (*rte_mempool_obj_iter_t)(void * /*obj_iter_arg*/,
397 void * /*obj_start*/,
399 uint32_t /*obj_index */);
402 * Call a function for each mempool object in a memory chunk
404 * Iterate across objects of the given size and alignment in the
405 * provided chunk of memory. The given memory buffer can consist of
406 * disjointed physical pages.
408 * For each object, call the provided callback (if any). This function
409 * is used to populate a mempool, or walk through all the elements of a
410 * mempool, or estimate how many elements of the given size could be
411 * created in the given memory buffer.
414 * Virtual address of the memory buffer.
416 * Maximum number of objects to iterate through.
418 * Size of each object.
420 * Alignment of each object.
422 * Array of physical addresses of the pages that comprises given memory
425 * Number of elements in the paddr array.
427 * LOG2 of the physical pages size.
429 * Object iterator callback function (could be NULL).
430 * @param obj_iter_arg
431 * User defined parameter for the object iterator callback function.
434 * Number of objects iterated through.
436 uint32_t rte_mempool_obj_iter(void *vaddr,
437 uint32_t elt_num, size_t elt_sz, size_t align,
438 const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift,
439 rte_mempool_obj_iter_t obj_iter, void *obj_iter_arg);
442 * An object constructor callback function for mempool.
444 * Arguments are the mempool, the opaque pointer given by the user in
445 * rte_mempool_create(), the pointer to the element and the index of
446 * the element in the pool.
448 typedef void (rte_mempool_obj_ctor_t)(struct rte_mempool *, void *,
452 * A mempool constructor callback function.
454 * Arguments are the mempool and the opaque pointer given by the user in
455 * rte_mempool_create().
457 typedef void (rte_mempool_ctor_t)(struct rte_mempool *, void *);
460 * Create a new mempool named *name* in memory.
462 * This function uses ``memzone_reserve()`` to allocate memory. The
463 * pool contains n elements of elt_size. Its size is set to n.
464 * All elements of the mempool are allocated together with the mempool header,
465 * in one physically continuous chunk of memory.
468 * The name of the mempool.
470 * The number of elements in the mempool. The optimum size (in terms of
471 * memory usage) for a mempool is when n is a power of two minus one:
474 * The size of each element.
476 * If cache_size is non-zero, the rte_mempool library will try to
477 * limit the accesses to the common lockless pool, by maintaining a
478 * per-lcore object cache. This argument must be lower or equal to
479 * CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE and n / 1.5. It is advised to choose
480 * cache_size to have "n modulo cache_size == 0": if this is
481 * not the case, some elements will always stay in the pool and will
482 * never be used. The access to the per-lcore table is of course
483 * faster than the multi-producer/consumer pool. The cache can be
484 * disabled if the cache_size argument is set to 0; it can be useful to
485 * avoid losing objects in cache. Note that even if not used, the
486 * memory space for cache is always reserved in a mempool structure,
487 * except if CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE is set to 0.
488 * @param private_data_size
489 * The size of the private data appended after the mempool
490 * structure. This is useful for storing some private data after the
491 * mempool structure, as is done for rte_mbuf_pool for example.
493 * A function pointer that is called for initialization of the pool,
494 * before object initialization. The user can initialize the private
495 * data in this function if needed. This parameter can be NULL if
498 * An opaque pointer to data that can be used in the mempool
499 * constructor function.
501 * A function pointer that is called for each object at
502 * initialization of the pool. The user can set some meta data in
503 * objects if needed. This parameter can be NULL if not needed.
504 * The obj_init() function takes the mempool pointer, the init_arg,
505 * the object pointer and the object number as parameters.
506 * @param obj_init_arg
507 * An opaque pointer to data that can be used as an argument for
508 * each call to the object constructor function.
510 * The *socket_id* argument is the socket identifier in the case of
511 * NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
512 * constraint for the reserved zone.
514 * The *flags* arguments is an OR of following flags:
515 * - MEMPOOL_F_NO_SPREAD: By default, objects addresses are spread
516 * between channels in RAM: the pool allocator will add padding
517 * between objects depending on the hardware configuration. See
518 * Memory alignment constraints for details. If this flag is set,
519 * the allocator will just align them to a cache line.
520 * - MEMPOOL_F_NO_CACHE_ALIGN: By default, the returned objects are
521 * cache-aligned. This flag removes this constraint, and no
522 * padding will be present between objects. This flag implies
523 * MEMPOOL_F_NO_SPREAD.
524 * - MEMPOOL_F_SP_PUT: If this flag is set, the default behavior
525 * when using rte_mempool_put() or rte_mempool_put_bulk() is
526 * "single-producer". Otherwise, it is "multi-producers".
527 * - MEMPOOL_F_SC_GET: If this flag is set, the default behavior
528 * when using rte_mempool_get() or rte_mempool_get_bulk() is
529 * "single-consumer". Otherwise, it is "multi-consumers".
531 * The pointer to the new allocated mempool, on success. NULL on error
532 * with rte_errno set appropriately. Possible rte_errno values include:
533 * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
534 * - E_RTE_SECONDARY - function was called from a secondary process instance
535 * - EINVAL - cache size provided is too large
536 * - ENOSPC - the maximum number of memzones has already been allocated
537 * - EEXIST - a memzone with the same name already exists
538 * - ENOMEM - no appropriate memory area found in which to create memzone
541 rte_mempool_create(const char *name, unsigned n, unsigned elt_size,
542 unsigned cache_size, unsigned private_data_size,
543 rte_mempool_ctor_t *mp_init, void *mp_init_arg,
544 rte_mempool_obj_ctor_t *obj_init, void *obj_init_arg,
545 int socket_id, unsigned flags);
548 * Create a new mempool named *name* in memory.
550 * The pool contains n elements of elt_size. Its size is set to n.
551 * This function uses ``memzone_reserve()`` to allocate the mempool header
552 * (and the objects if vaddr is NULL).
553 * Depending on the input parameters, mempool elements can be either allocated
554 * together with the mempool header, or an externally provided memory buffer
555 * could be used to store mempool objects. In later case, that external
556 * memory buffer can consist of set of disjoint physical pages.
559 * The name of the mempool.
561 * The number of elements in the mempool. The optimum size (in terms of
562 * memory usage) for a mempool is when n is a power of two minus one:
565 * The size of each element.
567 * Size of the cache. See rte_mempool_create() for details.
568 * @param private_data_size
569 * The size of the private data appended after the mempool
570 * structure. This is useful for storing some private data after the
571 * mempool structure, as is done for rte_mbuf_pool for example.
573 * A function pointer that is called for initialization of the pool,
574 * before object initialization. The user can initialize the private
575 * data in this function if needed. This parameter can be NULL if
578 * An opaque pointer to data that can be used in the mempool
579 * constructor function.
581 * A function called for each object at initialization of the pool.
582 * See rte_mempool_create() for details.
583 * @param obj_init_arg
584 * An opaque pointer passed to the object constructor function.
586 * The *socket_id* argument is the socket identifier in the case of
587 * NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
588 * constraint for the reserved zone.
590 * Flags controlling the behavior of the mempool. See
591 * rte_mempool_create() for details.
593 * Virtual address of the externally allocated memory buffer.
594 * Will be used to store mempool objects.
596 * Array of physical addresses of the pages that comprises given memory
599 * Number of elements in the paddr array.
601 * LOG2 of the physical pages size.
603 * The pointer to the new allocated mempool, on success. NULL on error
604 * with rte_errno set appropriately. See rte_mempool_create() for details.
607 rte_mempool_xmem_create(const char *name, unsigned n, unsigned elt_size,
608 unsigned cache_size, unsigned private_data_size,
609 rte_mempool_ctor_t *mp_init, void *mp_init_arg,
610 rte_mempool_obj_ctor_t *obj_init, void *obj_init_arg,
611 int socket_id, unsigned flags, void *vaddr,
612 const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift);
615 * Create a new mempool named *name* in memory on Xen Dom0.
617 * This function uses ``rte_mempool_xmem_create()`` to allocate memory. The
618 * pool contains n elements of elt_size. Its size is set to n.
619 * All elements of the mempool are allocated together with the mempool header,
620 * and memory buffer can consist of set of disjoint physical pages.
623 * The name of the mempool.
625 * The number of elements in the mempool. The optimum size (in terms of
626 * memory usage) for a mempool is when n is a power of two minus one:
629 * The size of each element.
631 * If cache_size is non-zero, the rte_mempool library will try to
632 * limit the accesses to the common lockless pool, by maintaining a
633 * per-lcore object cache. This argument must be lower or equal to
634 * CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE. It is advised to choose
635 * cache_size to have "n modulo cache_size == 0": if this is
636 * not the case, some elements will always stay in the pool and will
637 * never be used. The access to the per-lcore table is of course
638 * faster than the multi-producer/consumer pool. The cache can be
639 * disabled if the cache_size argument is set to 0; it can be useful to
640 * avoid losing objects in cache. Note that even if not used, the
641 * memory space for cache is always reserved in a mempool structure,
642 * except if CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE is set to 0.
643 * @param private_data_size
644 * The size of the private data appended after the mempool
645 * structure. This is useful for storing some private data after the
646 * mempool structure, as is done for rte_mbuf_pool for example.
648 * A function pointer that is called for initialization of the pool,
649 * before object initialization. The user can initialize the private
650 * data in this function if needed. This parameter can be NULL if
653 * An opaque pointer to data that can be used in the mempool
654 * constructor function.
656 * A function pointer that is called for each object at
657 * initialization of the pool. The user can set some meta data in
658 * objects if needed. This parameter can be NULL if not needed.
659 * The obj_init() function takes the mempool pointer, the init_arg,
660 * the object pointer and the object number as parameters.
661 * @param obj_init_arg
662 * An opaque pointer to data that can be used as an argument for
663 * each call to the object constructor function.
665 * The *socket_id* argument is the socket identifier in the case of
666 * NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
667 * constraint for the reserved zone.
669 * The *flags* arguments is an OR of following flags:
670 * - MEMPOOL_F_NO_SPREAD: By default, objects addresses are spread
671 * between channels in RAM: the pool allocator will add padding
672 * between objects depending on the hardware configuration. See
673 * Memory alignment constraints for details. If this flag is set,
674 * the allocator will just align them to a cache line.
675 * - MEMPOOL_F_NO_CACHE_ALIGN: By default, the returned objects are
676 * cache-aligned. This flag removes this constraint, and no
677 * padding will be present between objects. This flag implies
678 * MEMPOOL_F_NO_SPREAD.
679 * - MEMPOOL_F_SP_PUT: If this flag is set, the default behavior
680 * when using rte_mempool_put() or rte_mempool_put_bulk() is
681 * "single-producer". Otherwise, it is "multi-producers".
682 * - MEMPOOL_F_SC_GET: If this flag is set, the default behavior
683 * when using rte_mempool_get() or rte_mempool_get_bulk() is
684 * "single-consumer". Otherwise, it is "multi-consumers".
686 * The pointer to the new allocated mempool, on success. NULL on error
687 * with rte_errno set appropriately. Possible rte_errno values include:
688 * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
689 * - E_RTE_SECONDARY - function was called from a secondary process instance
690 * - EINVAL - cache size provided is too large
691 * - ENOSPC - the maximum number of memzones has already been allocated
692 * - EEXIST - a memzone with the same name already exists
693 * - ENOMEM - no appropriate memory area found in which to create memzone
696 rte_dom0_mempool_create(const char *name, unsigned n, unsigned elt_size,
697 unsigned cache_size, unsigned private_data_size,
698 rte_mempool_ctor_t *mp_init, void *mp_init_arg,
699 rte_mempool_obj_ctor_t *obj_init, void *obj_init_arg,
700 int socket_id, unsigned flags);
704 * Dump the status of the mempool to the console.
707 * A pointer to a file for output
709 * A pointer to the mempool structure.
711 void rte_mempool_dump(FILE *f, const struct rte_mempool *mp);
714 * @internal Put several objects back in the mempool; used internally.
716 * A pointer to the mempool structure.
718 * A pointer to a table of void * pointers (objects).
720 * The number of objects to store back in the mempool, must be strictly
723 * Mono-producer (0) or multi-producers (1).
725 static inline void __attribute__((always_inline))
726 __mempool_put_bulk(struct rte_mempool *mp, void * const *obj_table,
727 unsigned n, int is_mp)
729 struct rte_mempool_cache *cache;
732 unsigned lcore_id = rte_lcore_id();
733 uint32_t cache_size = mp->cache_size;
734 uint32_t flushthresh = mp->cache_flushthresh;
736 /* increment stat now, adding in mempool always success */
737 __MEMPOOL_STAT_ADD(mp, put, n);
739 /* cache is not enabled or single producer or non-EAL thread */
740 if (unlikely(cache_size == 0 || is_mp == 0 ||
741 lcore_id >= RTE_MAX_LCORE))
744 /* Go straight to ring if put would overflow mem allocated for cache */
745 if (unlikely(n > RTE_MEMPOOL_CACHE_MAX_SIZE))
748 cache = &mp->local_cache[lcore_id];
749 cache_objs = &cache->objs[cache->len];
752 * The cache follows the following algorithm
753 * 1. Add the objects to the cache
754 * 2. Anything greater than the cache min value (if it crosses the
755 * cache flush threshold) is flushed to the ring.
758 /* Add elements back into the cache */
759 for (index = 0; index < n; ++index, obj_table++)
760 cache_objs[index] = *obj_table;
764 if (cache->len >= flushthresh) {
765 rte_ring_mp_enqueue_bulk(mp->ring, &cache->objs[cache_size],
766 cache->len - cache_size);
767 cache->len = cache_size;
774 /* push remaining objects in ring */
775 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
777 if (rte_ring_mp_enqueue_bulk(mp->ring, obj_table, n) < 0)
778 rte_panic("cannot put objects in mempool\n");
781 if (rte_ring_sp_enqueue_bulk(mp->ring, obj_table, n) < 0)
782 rte_panic("cannot put objects in mempool\n");
786 rte_ring_mp_enqueue_bulk(mp->ring, obj_table, n);
788 rte_ring_sp_enqueue_bulk(mp->ring, obj_table, n);
794 * Put several objects back in the mempool (multi-producers safe).
797 * A pointer to the mempool structure.
799 * A pointer to a table of void * pointers (objects).
801 * The number of objects to add in the mempool from the obj_table.
803 static inline void __attribute__((always_inline))
804 rte_mempool_mp_put_bulk(struct rte_mempool *mp, void * const *obj_table,
807 __mempool_check_cookies(mp, obj_table, n, 0);
808 __mempool_put_bulk(mp, obj_table, n, 1);
812 * Put several objects back in the mempool (NOT multi-producers safe).
815 * A pointer to the mempool structure.
817 * A pointer to a table of void * pointers (objects).
819 * The number of objects to add in the mempool from obj_table.
822 rte_mempool_sp_put_bulk(struct rte_mempool *mp, void * const *obj_table,
825 __mempool_check_cookies(mp, obj_table, n, 0);
826 __mempool_put_bulk(mp, obj_table, n, 0);
830 * Put several objects back in the mempool.
832 * This function calls the multi-producer or the single-producer
833 * version depending on the default behavior that was specified at
834 * mempool creation time (see flags).
837 * A pointer to the mempool structure.
839 * A pointer to a table of void * pointers (objects).
841 * The number of objects to add in the mempool from obj_table.
843 static inline void __attribute__((always_inline))
844 rte_mempool_put_bulk(struct rte_mempool *mp, void * const *obj_table,
847 __mempool_check_cookies(mp, obj_table, n, 0);
848 __mempool_put_bulk(mp, obj_table, n, !(mp->flags & MEMPOOL_F_SP_PUT));
852 * Put one object in the mempool (multi-producers safe).
855 * A pointer to the mempool structure.
857 * A pointer to the object to be added.
859 static inline void __attribute__((always_inline))
860 rte_mempool_mp_put(struct rte_mempool *mp, void *obj)
862 rte_mempool_mp_put_bulk(mp, &obj, 1);
866 * Put one object back in the mempool (NOT multi-producers safe).
869 * A pointer to the mempool structure.
871 * A pointer to the object to be added.
873 static inline void __attribute__((always_inline))
874 rte_mempool_sp_put(struct rte_mempool *mp, void *obj)
876 rte_mempool_sp_put_bulk(mp, &obj, 1);
880 * Put one object back in the mempool.
882 * This function calls the multi-producer or the single-producer
883 * version depending on the default behavior that was specified at
884 * mempool creation time (see flags).
887 * A pointer to the mempool structure.
889 * A pointer to the object to be added.
891 static inline void __attribute__((always_inline))
892 rte_mempool_put(struct rte_mempool *mp, void *obj)
894 rte_mempool_put_bulk(mp, &obj, 1);
898 * @internal Get several objects from the mempool; used internally.
900 * A pointer to the mempool structure.
902 * A pointer to a table of void * pointers (objects).
904 * The number of objects to get, must be strictly positive.
906 * Mono-consumer (0) or multi-consumers (1).
908 * - >=0: Success; number of objects supplied.
909 * - <0: Error; code of ring dequeue function.
911 static inline int __attribute__((always_inline))
912 __mempool_get_bulk(struct rte_mempool *mp, void **obj_table,
913 unsigned n, int is_mc)
916 struct rte_mempool_cache *cache;
919 unsigned lcore_id = rte_lcore_id();
920 uint32_t cache_size = mp->cache_size;
922 /* cache is not enabled or single consumer */
923 if (unlikely(cache_size == 0 || is_mc == 0 ||
924 n >= cache_size || lcore_id >= RTE_MAX_LCORE))
927 cache = &mp->local_cache[lcore_id];
928 cache_objs = cache->objs;
930 /* Can this be satisfied from the cache? */
931 if (cache->len < n) {
932 /* No. Backfill the cache first, and then fill from it */
933 uint32_t req = n + (cache_size - cache->len);
935 /* How many do we require i.e. number to fill the cache + the request */
936 ret = rte_ring_mc_dequeue_bulk(mp->ring, &cache->objs[cache->len], req);
937 if (unlikely(ret < 0)) {
939 * In the offchance that we are buffer constrained,
940 * where we are not able to allocate cache + n, go to
941 * the ring directly. If that fails, we are truly out of
950 /* Now fill in the response ... */
951 for (index = 0, len = cache->len - 1; index < n; ++index, len--, obj_table++)
952 *obj_table = cache_objs[len];
956 __MEMPOOL_STAT_ADD(mp, get_success, n);
962 /* get remaining objects from ring */
964 ret = rte_ring_mc_dequeue_bulk(mp->ring, obj_table, n);
966 ret = rte_ring_sc_dequeue_bulk(mp->ring, obj_table, n);
969 __MEMPOOL_STAT_ADD(mp, get_fail, n);
971 __MEMPOOL_STAT_ADD(mp, get_success, n);
977 * Get several objects from the mempool (multi-consumers safe).
979 * If cache is enabled, objects will be retrieved first from cache,
980 * subsequently from the common pool. Note that it can return -ENOENT when
981 * the local cache and common pool are empty, even if cache from other
985 * A pointer to the mempool structure.
987 * A pointer to a table of void * pointers (objects) that will be filled.
989 * The number of objects to get from mempool to obj_table.
991 * - 0: Success; objects taken.
992 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
994 static inline int __attribute__((always_inline))
995 rte_mempool_mc_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned n)
998 ret = __mempool_get_bulk(mp, obj_table, n, 1);
1000 __mempool_check_cookies(mp, obj_table, n, 1);
1005 * Get several objects from the mempool (NOT multi-consumers safe).
1007 * If cache is enabled, objects will be retrieved first from cache,
1008 * subsequently from the common pool. Note that it can return -ENOENT when
1009 * the local cache and common pool are empty, even if cache from other
1013 * A pointer to the mempool structure.
1015 * A pointer to a table of void * pointers (objects) that will be filled.
1017 * The number of objects to get from the mempool to obj_table.
1019 * - 0: Success; objects taken.
1020 * - -ENOENT: Not enough entries in the mempool; no object is
1023 static inline int __attribute__((always_inline))
1024 rte_mempool_sc_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned n)
1027 ret = __mempool_get_bulk(mp, obj_table, n, 0);
1029 __mempool_check_cookies(mp, obj_table, n, 1);
1034 * Get several objects from the mempool.
1036 * This function calls the multi-consumers or the single-consumer
1037 * version, depending on the default behaviour that was specified at
1038 * mempool creation time (see flags).
1040 * If cache is enabled, objects will be retrieved first from cache,
1041 * subsequently from the common pool. Note that it can return -ENOENT when
1042 * the local cache and common pool are empty, even if cache from other
1046 * A pointer to the mempool structure.
1048 * A pointer to a table of void * pointers (objects) that will be filled.
1050 * The number of objects to get from the mempool to obj_table.
1052 * - 0: Success; objects taken
1053 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
1055 static inline int __attribute__((always_inline))
1056 rte_mempool_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned n)
1059 ret = __mempool_get_bulk(mp, obj_table, n,
1060 !(mp->flags & MEMPOOL_F_SC_GET));
1062 __mempool_check_cookies(mp, obj_table, n, 1);
1067 * Get one object from the mempool (multi-consumers safe).
1069 * If cache is enabled, objects will be retrieved first from cache,
1070 * subsequently from the common pool. Note that it can return -ENOENT when
1071 * the local cache and common pool are empty, even if cache from other
1075 * A pointer to the mempool structure.
1077 * A pointer to a void * pointer (object) that will be filled.
1079 * - 0: Success; objects taken.
1080 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
1082 static inline int __attribute__((always_inline))
1083 rte_mempool_mc_get(struct rte_mempool *mp, void **obj_p)
1085 return rte_mempool_mc_get_bulk(mp, obj_p, 1);
1089 * Get one object from the mempool (NOT multi-consumers safe).
1091 * If cache is enabled, objects will be retrieved first from cache,
1092 * subsequently from the common pool. Note that it can return -ENOENT when
1093 * the local cache and common pool are empty, even if cache from other
1097 * A pointer to the mempool structure.
1099 * A pointer to a void * pointer (object) that will be filled.
1101 * - 0: Success; objects taken.
1102 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
1104 static inline int __attribute__((always_inline))
1105 rte_mempool_sc_get(struct rte_mempool *mp, void **obj_p)
1107 return rte_mempool_sc_get_bulk(mp, obj_p, 1);
1111 * Get one object from the mempool.
1113 * This function calls the multi-consumers or the single-consumer
1114 * version, depending on the default behavior that was specified at
1115 * mempool creation (see flags).
1117 * If cache is enabled, objects will be retrieved first from cache,
1118 * subsequently from the common pool. Note that it can return -ENOENT when
1119 * the local cache and common pool are empty, even if cache from other
1123 * A pointer to the mempool structure.
1125 * A pointer to a void * pointer (object) that will be filled.
1127 * - 0: Success; objects taken.
1128 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
1130 static inline int __attribute__((always_inline))
1131 rte_mempool_get(struct rte_mempool *mp, void **obj_p)
1133 return rte_mempool_get_bulk(mp, obj_p, 1);
1137 * Return the number of entries in the mempool.
1139 * When cache is enabled, this function has to browse the length of
1140 * all lcores, so it should not be used in a data path, but only for
1144 * A pointer to the mempool structure.
1146 * The number of entries in the mempool.
1148 unsigned rte_mempool_count(const struct rte_mempool *mp);
1151 * Return the number of free entries in the mempool ring.
1152 * i.e. how many entries can be freed back to the mempool.
1154 * NOTE: This corresponds to the number of elements *allocated* from the
1155 * memory pool, not the number of elements in the pool itself. To count
1156 * the number elements currently available in the pool, use "rte_mempool_count"
1158 * When cache is enabled, this function has to browse the length of
1159 * all lcores, so it should not be used in a data path, but only for
1163 * A pointer to the mempool structure.
1165 * The number of free entries in the mempool.
1167 static inline unsigned
1168 rte_mempool_free_count(const struct rte_mempool *mp)
1170 return mp->size - rte_mempool_count(mp);
1174 * Test if the mempool is full.
1176 * When cache is enabled, this function has to browse the length of all
1177 * lcores, so it should not be used in a data path, but only for debug
1181 * A pointer to the mempool structure.
1183 * - 1: The mempool is full.
1184 * - 0: The mempool is not full.
1187 rte_mempool_full(const struct rte_mempool *mp)
1189 return !!(rte_mempool_count(mp) == mp->size);
1193 * Test if the mempool is empty.
1195 * When cache is enabled, this function has to browse the length of all
1196 * lcores, so it should not be used in a data path, but only for debug
1200 * A pointer to the mempool structure.
1202 * - 1: The mempool is empty.
1203 * - 0: The mempool is not empty.
1206 rte_mempool_empty(const struct rte_mempool *mp)
1208 return !!(rte_mempool_count(mp) == 0);
1212 * Return the physical address of elt, which is an element of the pool mp.
1215 * A pointer to the mempool structure.
1217 * A pointer (virtual address) to the element of the pool.
1219 * The physical address of the elt element.
1221 static inline phys_addr_t
1222 rte_mempool_virt2phy(const struct rte_mempool *mp, const void *elt)
1224 if (rte_eal_has_hugepages()) {
1227 off = (const char *)elt - (const char *)mp->elt_va_start;
1228 return mp->elt_pa[off >> mp->pg_shift] + (off & mp->pg_mask);
1231 * If huge pages are disabled, we cannot assume the
1232 * memory region to be physically contiguous.
1233 * Lookup for each element.
1235 return rte_mem_virt2phy(elt);
1240 * Check the consistency of mempool objects.
1242 * Verify the coherency of fields in the mempool structure. Also check
1243 * that the cookies of mempool objects (even the ones that are not
1244 * present in pool) have a correct value. If not, a panic will occur.
1247 * A pointer to the mempool structure.
1249 void rte_mempool_audit(const struct rte_mempool *mp);
1252 * Return a pointer to the private data in an mempool structure.
1255 * A pointer to the mempool structure.
1257 * A pointer to the private data.
1259 static inline void *rte_mempool_get_priv(struct rte_mempool *mp)
1262 MEMPOOL_HEADER_SIZE(mp, mp->pg_num, mp->cache_size);
1266 * Dump the status of all mempools on the console
1269 * A pointer to a file for output
1271 void rte_mempool_list_dump(FILE *f);
1274 * Search a mempool from its name
1277 * The name of the mempool.
1279 * The pointer to the mempool matching the name, or NULL if not found.
1281 * with rte_errno set appropriately. Possible rte_errno values include:
1282 * - ENOENT - required entry not available to return.
1285 struct rte_mempool *rte_mempool_lookup(const char *name);
1288 * Get the header, trailer and total size of a mempool element.
1290 * Given a desired size of the mempool element and mempool flags,
1291 * calculates header, trailer, body and total sizes of the mempool object.
1294 * The size of each element, without header and trailer.
1296 * The flags used for the mempool creation.
1297 * Consult rte_mempool_create() for more information about possible values.
1298 * The size of each element.
1300 * The calculated detailed size the mempool object. May be NULL.
1302 * Total size of the mempool object.
1304 uint32_t rte_mempool_calc_obj_size(uint32_t elt_size, uint32_t flags,
1305 struct rte_mempool_objsz *sz);
1308 * Get the size of memory required to store mempool elements.
1310 * Calculate the maximum amount of memory required to store given number
1311 * of objects. Assume that the memory buffer will be aligned at page
1314 * Note that if object size is bigger then page size, then it assumes
1315 * that pages are grouped in subsets of physically continuous pages big
1316 * enough to store at least one object.
1319 * Number of elements.
1320 * @param total_elt_sz
1321 * The size of each element, including header and trailer, as returned
1322 * by rte_mempool_calc_obj_size().
1324 * LOG2 of the physical pages size.
1326 * Required memory size aligned at page boundary.
1328 size_t rte_mempool_xmem_size(uint32_t elt_num, size_t total_elt_sz,
1332 * Get the size of memory required to store mempool elements.
1334 * Calculate how much memory would be actually required with the given
1335 * memory footprint to store required number of objects.
1338 * Virtual address of the externally allocated memory buffer.
1339 * Will be used to store mempool objects.
1341 * Number of elements.
1342 * @param total_elt_sz
1343 * The size of each element, including header and trailer, as returned
1344 * by rte_mempool_calc_obj_size().
1346 * Array of physical addresses of the pages that comprises given memory
1349 * Number of elements in the paddr array.
1351 * LOG2 of the physical pages size.
1353 * On success, the number of bytes needed to store given number of
1354 * objects, aligned to the given page size. If the provided memory
1355 * buffer is too small, return a negative value whose absolute value
1356 * is the actual number of elements that can be stored in that buffer.
1358 ssize_t rte_mempool_xmem_usage(void *vaddr, uint32_t elt_num,
1359 size_t total_elt_sz, const phys_addr_t paddr[], uint32_t pg_num,
1363 * Walk list of all memory pools
1368 * Argument passed to iterator
1370 void rte_mempool_walk(void (*func)(const struct rte_mempool *, void *arg),
1377 #endif /* _RTE_MEMPOOL_H_ */