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;
98 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
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;
110 #endif /* RTE_MEMPOOL_CACHE_MAX_SIZE > 0 */
113 * A structure that stores the size of mempool elements.
115 struct rte_mempool_objsz {
116 uint32_t elt_size; /**< Size of an element. */
117 uint32_t header_size; /**< Size of header (before elt). */
118 uint32_t trailer_size; /**< Size of trailer (after elt). */
120 /**< Total size of an object (header + elt + trailer). */
123 #define RTE_MEMPOOL_NAMESIZE 32 /**< Maximum length of a memory pool. */
124 #define RTE_MEMPOOL_MZ_PREFIX "MP_"
127 #define RTE_MEMPOOL_MZ_FORMAT RTE_MEMPOOL_MZ_PREFIX "%s"
129 #ifdef RTE_LIBRTE_XEN_DOM0
131 /* "<name>_MP_elt" */
132 #define RTE_MEMPOOL_OBJ_NAME "%s_" RTE_MEMPOOL_MZ_PREFIX "elt"
136 #define RTE_MEMPOOL_OBJ_NAME RTE_MEMPOOL_MZ_FORMAT
138 #endif /* RTE_LIBRTE_XEN_DOM0 */
140 #define MEMPOOL_PG_SHIFT_MAX (sizeof(uintptr_t) * CHAR_BIT - 1)
142 /** Mempool over one chunk of physically continuous memory */
143 #define MEMPOOL_PG_NUM_DEFAULT 1
146 * Mempool object header structure
148 * Each object stored in mempools are prefixed by this header structure,
149 * it allows to retrieve the mempool pointer from the object. When debug
150 * is enabled, a cookie is also added in this structure preventing
151 * corruptions and double-frees.
153 struct rte_mempool_objhdr {
154 struct rte_mempool *mp; /**< The mempool owning the object. */
155 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
156 uint64_t cookie; /**< Debug cookie. */
161 * Mempool object trailer structure
163 * In debug mode, each object stored in mempools are suffixed by this
164 * trailer structure containing a cookie preventing memory corruptions.
166 struct rte_mempool_objtlr {
167 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
168 uint64_t cookie; /**< Debug cookie. */
173 * The RTE mempool structure.
176 char name[RTE_MEMPOOL_NAMESIZE]; /**< Name of mempool. */
177 struct rte_ring *ring; /**< Ring to store objects. */
178 phys_addr_t phys_addr; /**< Phys. addr. of mempool struct. */
179 int flags; /**< Flags of the mempool. */
180 uint32_t size; /**< Size of the mempool. */
181 uint32_t cache_size; /**< Size of per-lcore local cache. */
182 uint32_t cache_flushthresh;
183 /**< Threshold before we flush excess elements. */
185 uint32_t elt_size; /**< Size of an element. */
186 uint32_t header_size; /**< Size of header (before elt). */
187 uint32_t trailer_size; /**< Size of trailer (after elt). */
189 unsigned private_data_size; /**< Size of private data. */
191 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
192 /** Per-lcore local cache. */
193 struct rte_mempool_cache local_cache[RTE_MAX_LCORE];
196 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
197 /** Per-lcore statistics. */
198 struct rte_mempool_debug_stats stats[RTE_MAX_LCORE];
201 /* Address translation support, starts from next cache line. */
203 /** Number of elements in the elt_pa array. */
204 uint32_t pg_num __rte_cache_aligned;
205 uint32_t pg_shift; /**< LOG2 of the physical pages. */
206 uintptr_t pg_mask; /**< physical page mask value. */
207 uintptr_t elt_va_start;
208 /**< Virtual address of the first mempool object. */
209 uintptr_t elt_va_end;
210 /**< Virtual address of the <size + 1> mempool object. */
211 phys_addr_t elt_pa[MEMPOOL_PG_NUM_DEFAULT];
212 /**< Array of physical page addresses for the mempool objects buffer. */
214 } __rte_cache_aligned;
216 #define MEMPOOL_F_NO_SPREAD 0x0001 /**< Do not spread in memory. */
217 #define MEMPOOL_F_NO_CACHE_ALIGN 0x0002 /**< Do not align objs on cache lines.*/
218 #define MEMPOOL_F_SP_PUT 0x0004 /**< Default put is "single-producer".*/
219 #define MEMPOOL_F_SC_GET 0x0008 /**< Default get is "single-consumer".*/
222 * @internal When debug is enabled, store some statistics.
225 * Pointer to the memory pool.
227 * Name of the statistics field to increment in the memory pool.
229 * Number to add to the object-oriented statistics.
231 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
232 #define __MEMPOOL_STAT_ADD(mp, name, n) do { \
233 unsigned __lcore_id = rte_lcore_id(); \
234 if (__lcore_id < RTE_MAX_LCORE) { \
235 mp->stats[__lcore_id].name##_objs += n; \
236 mp->stats[__lcore_id].name##_bulk += 1; \
240 #define __MEMPOOL_STAT_ADD(mp, name, n) do {} while(0)
244 * Calculate the size of the mempool header.
247 * Pointer to the memory pool.
249 * Number of pages used to store mempool objects.
251 #define MEMPOOL_HEADER_SIZE(mp, pgn) (sizeof(*(mp)) + \
252 RTE_ALIGN_CEIL(((pgn) - RTE_DIM((mp)->elt_pa)) * \
253 sizeof ((mp)->elt_pa[0]), RTE_CACHE_LINE_SIZE))
256 * Return true if the whole mempool is in contiguous memory.
258 #define MEMPOOL_IS_CONTIG(mp) \
259 ((mp)->pg_num == MEMPOOL_PG_NUM_DEFAULT && \
260 (mp)->phys_addr == (mp)->elt_pa[0])
262 /* return the header of a mempool object (internal) */
263 static inline struct rte_mempool_objhdr *__mempool_get_header(void *obj)
265 return RTE_PTR_SUB(obj, sizeof(struct rte_mempool_objhdr));
268 /* return the trailer of a mempool object (internal) */
269 static inline struct rte_mempool_objtlr *__mempool_get_trailer(void *obj)
271 return RTE_PTR_SUB(obj, sizeof(struct rte_mempool_objtlr));
275 * Return a pointer to the mempool owning this object.
278 * An object that is owned by a pool. If this is not the case,
279 * the behavior is undefined.
281 * A pointer to the mempool structure.
283 static inline struct rte_mempool *rte_mempool_from_obj(void *obj)
285 struct rte_mempool_objhdr *hdr = __mempool_get_header(obj);
290 * @internal Check and update cookies or panic.
293 * Pointer to the memory pool.
294 * @param obj_table_const
295 * Pointer to a table of void * pointers (objects).
297 * Index of object in object table.
299 * - 0: object is supposed to be allocated, mark it as free
300 * - 1: object is supposed to be free, mark it as allocated
301 * - 2: just check that cookie is valid (free or allocated)
303 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
304 #ifndef __INTEL_COMPILER
305 #pragma GCC diagnostic ignored "-Wcast-qual"
307 static inline void __mempool_check_cookies(const struct rte_mempool *mp,
308 void * const *obj_table_const,
309 unsigned n, int free)
311 struct rte_mempool_objhdr *hdr;
312 struct rte_mempool_objtlr *tlr;
318 /* Force to drop the "const" attribute. This is done only when
319 * DEBUG is enabled */
320 tmp = (void *) obj_table_const;
321 obj_table = (void **) tmp;
326 if (rte_mempool_from_obj(obj) != mp)
327 rte_panic("MEMPOOL: object is owned by another "
330 hdr = __mempool_get_header(obj);
331 cookie = hdr->cookie;
334 if (cookie != RTE_MEMPOOL_HEADER_COOKIE1) {
335 rte_log_set_history(0);
336 RTE_LOG(CRIT, MEMPOOL,
337 "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
338 obj, (const void *) mp, cookie);
339 rte_panic("MEMPOOL: bad header cookie (put)\n");
341 hdr->cookie = RTE_MEMPOOL_HEADER_COOKIE2;
343 else if (free == 1) {
344 if (cookie != RTE_MEMPOOL_HEADER_COOKIE2) {
345 rte_log_set_history(0);
346 RTE_LOG(CRIT, MEMPOOL,
347 "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
348 obj, (const void *) mp, cookie);
349 rte_panic("MEMPOOL: bad header cookie (get)\n");
351 hdr->cookie = RTE_MEMPOOL_HEADER_COOKIE1;
353 else if (free == 2) {
354 if (cookie != RTE_MEMPOOL_HEADER_COOKIE1 &&
355 cookie != RTE_MEMPOOL_HEADER_COOKIE2) {
356 rte_log_set_history(0);
357 RTE_LOG(CRIT, MEMPOOL,
358 "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
359 obj, (const void *) mp, cookie);
360 rte_panic("MEMPOOL: bad header cookie (audit)\n");
363 tlr = __mempool_get_trailer(obj);
364 cookie = tlr->cookie;
365 if (cookie != RTE_MEMPOOL_TRAILER_COOKIE) {
366 rte_log_set_history(0);
367 RTE_LOG(CRIT, MEMPOOL,
368 "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
369 obj, (const void *) mp, cookie);
370 rte_panic("MEMPOOL: bad trailer cookie\n");
374 #ifndef __INTEL_COMPILER
375 #pragma GCC diagnostic error "-Wcast-qual"
378 #define __mempool_check_cookies(mp, obj_table_const, n, free) do {} while(0)
379 #endif /* RTE_LIBRTE_MEMPOOL_DEBUG */
382 * A mempool object iterator callback function.
384 typedef void (*rte_mempool_obj_iter_t)(void * /*obj_iter_arg*/,
385 void * /*obj_start*/,
387 uint32_t /*obj_index */);
390 * Call a function for each mempool object in a memory chunk
392 * Iterate across objects of the given size and alignment in the
393 * provided chunk of memory. The given memory buffer can consist of
394 * disjointed physical pages.
396 * For each object, call the provided callback (if any). This function
397 * is used to populate a mempool, or walk through all the elements of a
398 * mempool, or estimate how many elements of the given size could be
399 * created in the given memory buffer.
402 * Virtual address of the memory buffer.
404 * Maximum number of objects to iterate through.
406 * Size of each object.
408 * Alignment of each object.
410 * Array of physical addresses of the pages that comprises given memory
413 * Number of elements in the paddr array.
415 * LOG2 of the physical pages size.
417 * Object iterator callback function (could be NULL).
418 * @param obj_iter_arg
419 * User defined parameter for the object iterator callback function.
422 * Number of objects iterated through.
424 uint32_t rte_mempool_obj_iter(void *vaddr,
425 uint32_t elt_num, size_t elt_sz, size_t align,
426 const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift,
427 rte_mempool_obj_iter_t obj_iter, void *obj_iter_arg);
430 * An object constructor callback function for mempool.
432 * Arguments are the mempool, the opaque pointer given by the user in
433 * rte_mempool_create(), the pointer to the element and the index of
434 * the element in the pool.
436 typedef void (rte_mempool_obj_ctor_t)(struct rte_mempool *, void *,
440 * A mempool constructor callback function.
442 * Arguments are the mempool and the opaque pointer given by the user in
443 * rte_mempool_create().
445 typedef void (rte_mempool_ctor_t)(struct rte_mempool *, void *);
448 * Create a new mempool named *name* in memory.
450 * This function uses ``memzone_reserve()`` to allocate memory. The
451 * pool contains n elements of elt_size. Its size is set to n.
452 * All elements of the mempool are allocated together with the mempool header,
453 * in one physically continuous chunk of memory.
456 * The name of the mempool.
458 * The number of elements in the mempool. The optimum size (in terms of
459 * memory usage) for a mempool is when n is a power of two minus one:
462 * The size of each element.
464 * If cache_size is non-zero, the rte_mempool library will try to
465 * limit the accesses to the common lockless pool, by maintaining a
466 * per-lcore object cache. This argument must be lower or equal to
467 * CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE and n / 1.5. It is advised to choose
468 * cache_size to have "n modulo cache_size == 0": if this is
469 * not the case, some elements will always stay in the pool and will
470 * never be used. The access to the per-lcore table is of course
471 * faster than the multi-producer/consumer pool. The cache can be
472 * disabled if the cache_size argument is set to 0; it can be useful to
473 * avoid losing objects in cache. Note that even if not used, the
474 * memory space for cache is always reserved in a mempool structure,
475 * except if CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE is set to 0.
476 * @param private_data_size
477 * The size of the private data appended after the mempool
478 * structure. This is useful for storing some private data after the
479 * mempool structure, as is done for rte_mbuf_pool for example.
481 * A function pointer that is called for initialization of the pool,
482 * before object initialization. The user can initialize the private
483 * data in this function if needed. This parameter can be NULL if
486 * An opaque pointer to data that can be used in the mempool
487 * constructor function.
489 * A function pointer that is called for each object at
490 * initialization of the pool. The user can set some meta data in
491 * objects if needed. This parameter can be NULL if not needed.
492 * The obj_init() function takes the mempool pointer, the init_arg,
493 * the object pointer and the object number as parameters.
494 * @param obj_init_arg
495 * An opaque pointer to data that can be used as an argument for
496 * each call to the object constructor function.
498 * The *socket_id* argument is the socket identifier in the case of
499 * NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
500 * constraint for the reserved zone.
502 * The *flags* arguments is an OR of following flags:
503 * - MEMPOOL_F_NO_SPREAD: By default, objects addresses are spread
504 * between channels in RAM: the pool allocator will add padding
505 * between objects depending on the hardware configuration. See
506 * Memory alignment constraints for details. If this flag is set,
507 * the allocator will just align them to a cache line.
508 * - MEMPOOL_F_NO_CACHE_ALIGN: By default, the returned objects are
509 * cache-aligned. This flag removes this constraint, and no
510 * padding will be present between objects. This flag implies
511 * MEMPOOL_F_NO_SPREAD.
512 * - MEMPOOL_F_SP_PUT: If this flag is set, the default behavior
513 * when using rte_mempool_put() or rte_mempool_put_bulk() is
514 * "single-producer". Otherwise, it is "multi-producers".
515 * - MEMPOOL_F_SC_GET: If this flag is set, the default behavior
516 * when using rte_mempool_get() or rte_mempool_get_bulk() is
517 * "single-consumer". Otherwise, it is "multi-consumers".
519 * The pointer to the new allocated mempool, on success. NULL on error
520 * with rte_errno set appropriately. Possible rte_errno values include:
521 * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
522 * - E_RTE_SECONDARY - function was called from a secondary process instance
523 * - EINVAL - cache size provided is too large
524 * - ENOSPC - the maximum number of memzones has already been allocated
525 * - EEXIST - a memzone with the same name already exists
526 * - ENOMEM - no appropriate memory area found in which to create memzone
529 rte_mempool_create(const char *name, unsigned n, unsigned elt_size,
530 unsigned cache_size, unsigned private_data_size,
531 rte_mempool_ctor_t *mp_init, void *mp_init_arg,
532 rte_mempool_obj_ctor_t *obj_init, void *obj_init_arg,
533 int socket_id, unsigned flags);
536 * Create a new mempool named *name* in memory.
538 * This function uses ``memzone_reserve()`` to allocate memory. The
539 * pool contains n elements of elt_size. Its size is set to n.
540 * Depending on the input parameters, mempool elements can be either allocated
541 * together with the mempool header, or an externally provided memory buffer
542 * could be used to store mempool objects. In later case, that external
543 * memory buffer can consist of set of disjoint physical pages.
546 * The name of the mempool.
548 * The number of elements in the mempool. The optimum size (in terms of
549 * memory usage) for a mempool is when n is a power of two minus one:
552 * The size of each element.
554 * If cache_size is non-zero, the rte_mempool library will try to
555 * limit the accesses to the common lockless pool, by maintaining a
556 * per-lcore object cache. This argument must be lower or equal to
557 * CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE. It is advised to choose
558 * cache_size to have "n modulo cache_size == 0": if this is
559 * not the case, some elements will always stay in the pool and will
560 * never be used. The access to the per-lcore table is of course
561 * faster than the multi-producer/consumer pool. The cache can be
562 * disabled if the cache_size argument is set to 0; it can be useful to
563 * avoid losing objects in cache. Note that even if not used, the
564 * memory space for cache is always reserved in a mempool structure,
565 * except if CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE is set to 0.
566 * @param private_data_size
567 * The size of the private data appended after the mempool
568 * structure. This is useful for storing some private data after the
569 * mempool structure, as is done for rte_mbuf_pool for example.
571 * A function pointer that is called for initialization of the pool,
572 * before object initialization. The user can initialize the private
573 * data in this function if needed. This parameter can be NULL if
576 * An opaque pointer to data that can be used in the mempool
577 * constructor function.
579 * A function pointer that is called for each object at
580 * initialization of the pool. The user can set some meta data in
581 * objects if needed. This parameter can be NULL if not needed.
582 * The obj_init() function takes the mempool pointer, the init_arg,
583 * the object pointer and the object number as parameters.
584 * @param obj_init_arg
585 * An opaque pointer to data that can be used as an argument for
586 * each call to the object constructor function.
588 * The *socket_id* argument is the socket identifier in the case of
589 * NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
590 * constraint for the reserved zone.
592 * The *flags* arguments is an OR of following flags:
593 * - MEMPOOL_F_NO_SPREAD: By default, objects addresses are spread
594 * between channels in RAM: the pool allocator will add padding
595 * between objects depending on the hardware configuration. See
596 * Memory alignment constraints for details. If this flag is set,
597 * the allocator will just align them to a cache line.
598 * - MEMPOOL_F_NO_CACHE_ALIGN: By default, the returned objects are
599 * cache-aligned. This flag removes this constraint, and no
600 * padding will be present between objects. This flag implies
601 * MEMPOOL_F_NO_SPREAD.
602 * - MEMPOOL_F_SP_PUT: If this flag is set, the default behavior
603 * when using rte_mempool_put() or rte_mempool_put_bulk() is
604 * "single-producer". Otherwise, it is "multi-producers".
605 * - MEMPOOL_F_SC_GET: If this flag is set, the default behavior
606 * when using rte_mempool_get() or rte_mempool_get_bulk() is
607 * "single-consumer". Otherwise, it is "multi-consumers".
609 * Virtual address of the externally allocated memory buffer.
610 * Will be used to store mempool objects.
612 * Array of physical addresses of the pages that comprises given memory
615 * Number of elements in the paddr array.
617 * LOG2 of the physical pages size.
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_mempool_xmem_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_ctor_t *obj_init, void *obj_init_arg,
633 int socket_id, unsigned flags, void *vaddr,
634 const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift);
636 #ifdef RTE_LIBRTE_XEN_DOM0
638 * Create a new mempool named *name* in memory on Xen Dom0.
640 * This function uses ``rte_mempool_xmem_create()`` to allocate memory. The
641 * pool contains n elements of elt_size. Its size is set to n.
642 * All elements of the mempool are allocated together with the mempool header,
643 * and memory buffer can consist of set of disjoint physical pages.
646 * The name of the mempool.
648 * The number of elements in the mempool. The optimum size (in terms of
649 * memory usage) for a mempool is when n is a power of two minus one:
652 * The size of each element.
654 * If cache_size is non-zero, the rte_mempool library will try to
655 * limit the accesses to the common lockless pool, by maintaining a
656 * per-lcore object cache. This argument must be lower or equal to
657 * CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE. It is advised to choose
658 * cache_size to have "n modulo cache_size == 0": if this is
659 * not the case, some elements will always stay in the pool and will
660 * never be used. The access to the per-lcore table is of course
661 * faster than the multi-producer/consumer pool. The cache can be
662 * disabled if the cache_size argument is set to 0; it can be useful to
663 * avoid losing objects in cache. Note that even if not used, the
664 * memory space for cache is always reserved in a mempool structure,
665 * except if CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE is set to 0.
666 * @param private_data_size
667 * The size of the private data appended after the mempool
668 * structure. This is useful for storing some private data after the
669 * mempool structure, as is done for rte_mbuf_pool for example.
671 * A function pointer that is called for initialization of the pool,
672 * before object initialization. The user can initialize the private
673 * data in this function if needed. This parameter can be NULL if
676 * An opaque pointer to data that can be used in the mempool
677 * constructor function.
679 * A function pointer that is called for each object at
680 * initialization of the pool. The user can set some meta data in
681 * objects if needed. This parameter can be NULL if not needed.
682 * The obj_init() function takes the mempool pointer, the init_arg,
683 * the object pointer and the object number as parameters.
684 * @param obj_init_arg
685 * An opaque pointer to data that can be used as an argument for
686 * each call to the object constructor function.
688 * The *socket_id* argument is the socket identifier in the case of
689 * NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
690 * constraint for the reserved zone.
692 * The *flags* arguments is an OR of following flags:
693 * - MEMPOOL_F_NO_SPREAD: By default, objects addresses are spread
694 * between channels in RAM: the pool allocator will add padding
695 * between objects depending on the hardware configuration. See
696 * Memory alignment constraints for details. If this flag is set,
697 * the allocator will just align them to a cache line.
698 * - MEMPOOL_F_NO_CACHE_ALIGN: By default, the returned objects are
699 * cache-aligned. This flag removes this constraint, and no
700 * padding will be present between objects. This flag implies
701 * MEMPOOL_F_NO_SPREAD.
702 * - MEMPOOL_F_SP_PUT: If this flag is set, the default behavior
703 * when using rte_mempool_put() or rte_mempool_put_bulk() is
704 * "single-producer". Otherwise, it is "multi-producers".
705 * - MEMPOOL_F_SC_GET: If this flag is set, the default behavior
706 * when using rte_mempool_get() or rte_mempool_get_bulk() is
707 * "single-consumer". Otherwise, it is "multi-consumers".
709 * The pointer to the new allocated mempool, on success. NULL on error
710 * with rte_errno set appropriately. Possible rte_errno values include:
711 * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
712 * - E_RTE_SECONDARY - function was called from a secondary process instance
713 * - EINVAL - cache size provided is too large
714 * - ENOSPC - the maximum number of memzones has already been allocated
715 * - EEXIST - a memzone with the same name already exists
716 * - ENOMEM - no appropriate memory area found in which to create memzone
719 rte_dom0_mempool_create(const char *name, unsigned n, unsigned elt_size,
720 unsigned cache_size, unsigned private_data_size,
721 rte_mempool_ctor_t *mp_init, void *mp_init_arg,
722 rte_mempool_obj_ctor_t *obj_init, void *obj_init_arg,
723 int socket_id, unsigned flags);
727 * Dump the status of the mempool to the console.
730 * A pointer to a file for output
732 * A pointer to the mempool structure.
734 void rte_mempool_dump(FILE *f, const struct rte_mempool *mp);
737 * @internal Put several objects back in the mempool; used internally.
739 * A pointer to the mempool structure.
741 * A pointer to a table of void * pointers (objects).
743 * The number of objects to store back in the mempool, must be strictly
746 * Mono-producer (0) or multi-producers (1).
748 static inline void __attribute__((always_inline))
749 __mempool_put_bulk(struct rte_mempool *mp, void * const *obj_table,
750 unsigned n, int is_mp)
752 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
753 struct rte_mempool_cache *cache;
756 unsigned lcore_id = rte_lcore_id();
757 uint32_t cache_size = mp->cache_size;
758 uint32_t flushthresh = mp->cache_flushthresh;
759 #endif /* RTE_MEMPOOL_CACHE_MAX_SIZE > 0 */
761 /* increment stat now, adding in mempool always success */
762 __MEMPOOL_STAT_ADD(mp, put, n);
764 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
765 /* cache is not enabled or single producer or non-EAL thread */
766 if (unlikely(cache_size == 0 || is_mp == 0 ||
767 lcore_id >= RTE_MAX_LCORE))
770 /* Go straight to ring if put would overflow mem allocated for cache */
771 if (unlikely(n > RTE_MEMPOOL_CACHE_MAX_SIZE))
774 cache = &mp->local_cache[lcore_id];
775 cache_objs = &cache->objs[cache->len];
778 * The cache follows the following algorithm
779 * 1. Add the objects to the cache
780 * 2. Anything greater than the cache min value (if it crosses the
781 * cache flush threshold) is flushed to the ring.
784 /* Add elements back into the cache */
785 for (index = 0; index < n; ++index, obj_table++)
786 cache_objs[index] = *obj_table;
790 if (cache->len >= flushthresh) {
791 rte_ring_mp_enqueue_bulk(mp->ring, &cache->objs[cache_size],
792 cache->len - cache_size);
793 cache->len = cache_size;
799 #endif /* RTE_MEMPOOL_CACHE_MAX_SIZE > 0 */
801 /* push remaining objects in ring */
802 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
804 if (rte_ring_mp_enqueue_bulk(mp->ring, obj_table, n) < 0)
805 rte_panic("cannot put objects in mempool\n");
808 if (rte_ring_sp_enqueue_bulk(mp->ring, obj_table, n) < 0)
809 rte_panic("cannot put objects in mempool\n");
813 rte_ring_mp_enqueue_bulk(mp->ring, obj_table, n);
815 rte_ring_sp_enqueue_bulk(mp->ring, obj_table, n);
821 * Put several objects back in the mempool (multi-producers safe).
824 * A pointer to the mempool structure.
826 * A pointer to a table of void * pointers (objects).
828 * The number of objects to add in the mempool from the obj_table.
830 static inline void __attribute__((always_inline))
831 rte_mempool_mp_put_bulk(struct rte_mempool *mp, void * const *obj_table,
834 __mempool_check_cookies(mp, obj_table, n, 0);
835 __mempool_put_bulk(mp, obj_table, n, 1);
839 * Put several objects back in the mempool (NOT multi-producers safe).
842 * A pointer to the mempool structure.
844 * A pointer to a table of void * pointers (objects).
846 * The number of objects to add in the mempool from obj_table.
849 rte_mempool_sp_put_bulk(struct rte_mempool *mp, void * const *obj_table,
852 __mempool_check_cookies(mp, obj_table, n, 0);
853 __mempool_put_bulk(mp, obj_table, n, 0);
857 * Put several objects back in the mempool.
859 * This function calls the multi-producer or the single-producer
860 * version depending on the default behavior that was specified at
861 * mempool creation time (see flags).
864 * A pointer to the mempool structure.
866 * A pointer to a table of void * pointers (objects).
868 * The number of objects to add in the mempool from obj_table.
870 static inline void __attribute__((always_inline))
871 rte_mempool_put_bulk(struct rte_mempool *mp, void * const *obj_table,
874 __mempool_check_cookies(mp, obj_table, n, 0);
875 __mempool_put_bulk(mp, obj_table, n, !(mp->flags & MEMPOOL_F_SP_PUT));
879 * Put one object in the mempool (multi-producers safe).
882 * A pointer to the mempool structure.
884 * A pointer to the object to be added.
886 static inline void __attribute__((always_inline))
887 rte_mempool_mp_put(struct rte_mempool *mp, void *obj)
889 rte_mempool_mp_put_bulk(mp, &obj, 1);
893 * Put one object back in the mempool (NOT multi-producers safe).
896 * A pointer to the mempool structure.
898 * A pointer to the object to be added.
900 static inline void __attribute__((always_inline))
901 rte_mempool_sp_put(struct rte_mempool *mp, void *obj)
903 rte_mempool_sp_put_bulk(mp, &obj, 1);
907 * Put one object back in the mempool.
909 * This function calls the multi-producer or the single-producer
910 * version depending on the default behavior that was specified at
911 * mempool creation time (see flags).
914 * A pointer to the mempool structure.
916 * A pointer to the object to be added.
918 static inline void __attribute__((always_inline))
919 rte_mempool_put(struct rte_mempool *mp, void *obj)
921 rte_mempool_put_bulk(mp, &obj, 1);
925 * @internal Get several objects from the mempool; used internally.
927 * A pointer to the mempool structure.
929 * A pointer to a table of void * pointers (objects).
931 * The number of objects to get, must be strictly positive.
933 * Mono-consumer (0) or multi-consumers (1).
935 * - >=0: Success; number of objects supplied.
936 * - <0: Error; code of ring dequeue function.
938 static inline int __attribute__((always_inline))
939 __mempool_get_bulk(struct rte_mempool *mp, void **obj_table,
940 unsigned n, int is_mc)
943 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
944 struct rte_mempool_cache *cache;
947 unsigned lcore_id = rte_lcore_id();
948 uint32_t cache_size = mp->cache_size;
950 /* cache is not enabled or single consumer */
951 if (unlikely(cache_size == 0 || is_mc == 0 ||
952 n >= cache_size || lcore_id >= RTE_MAX_LCORE))
955 cache = &mp->local_cache[lcore_id];
956 cache_objs = cache->objs;
958 /* Can this be satisfied from the cache? */
959 if (cache->len < n) {
960 /* No. Backfill the cache first, and then fill from it */
961 uint32_t req = n + (cache_size - cache->len);
963 /* How many do we require i.e. number to fill the cache + the request */
964 ret = rte_ring_mc_dequeue_bulk(mp->ring, &cache->objs[cache->len], req);
965 if (unlikely(ret < 0)) {
967 * In the offchance that we are buffer constrained,
968 * where we are not able to allocate cache + n, go to
969 * the ring directly. If that fails, we are truly out of
978 /* Now fill in the response ... */
979 for (index = 0, len = cache->len - 1; index < n; ++index, len--, obj_table++)
980 *obj_table = cache_objs[len];
984 __MEMPOOL_STAT_ADD(mp, get_success, n);
989 #endif /* RTE_MEMPOOL_CACHE_MAX_SIZE > 0 */
991 /* get remaining objects from ring */
993 ret = rte_ring_mc_dequeue_bulk(mp->ring, obj_table, n);
995 ret = rte_ring_sc_dequeue_bulk(mp->ring, obj_table, n);
998 __MEMPOOL_STAT_ADD(mp, get_fail, n);
1000 __MEMPOOL_STAT_ADD(mp, get_success, n);
1006 * Get several objects from the mempool (multi-consumers safe).
1008 * If cache is enabled, objects will be retrieved first from cache,
1009 * subsequently from the common pool. Note that it can return -ENOENT when
1010 * the local cache and common pool are empty, even if cache from other
1014 * A pointer to the mempool structure.
1016 * A pointer to a table of void * pointers (objects) that will be filled.
1018 * The number of objects to get from mempool to obj_table.
1020 * - 0: Success; objects taken.
1021 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
1023 static inline int __attribute__((always_inline))
1024 rte_mempool_mc_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned n)
1027 ret = __mempool_get_bulk(mp, obj_table, n, 1);
1029 __mempool_check_cookies(mp, obj_table, n, 1);
1034 * Get several objects from the mempool (NOT multi-consumers safe).
1036 * If cache is enabled, objects will be retrieved first from cache,
1037 * subsequently from the common pool. Note that it can return -ENOENT when
1038 * the local cache and common pool are empty, even if cache from other
1042 * A pointer to the mempool structure.
1044 * A pointer to a table of void * pointers (objects) that will be filled.
1046 * The number of objects to get from the mempool to obj_table.
1048 * - 0: Success; objects taken.
1049 * - -ENOENT: Not enough entries in the mempool; no object is
1052 static inline int __attribute__((always_inline))
1053 rte_mempool_sc_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned n)
1056 ret = __mempool_get_bulk(mp, obj_table, n, 0);
1058 __mempool_check_cookies(mp, obj_table, n, 1);
1063 * Get several objects from the mempool.
1065 * This function calls the multi-consumers or the single-consumer
1066 * version, depending on the default behaviour that was specified at
1067 * mempool creation time (see flags).
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 table of void * pointers (objects) that will be filled.
1079 * The number of objects to get from the mempool to obj_table.
1081 * - 0: Success; objects taken
1082 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
1084 static inline int __attribute__((always_inline))
1085 rte_mempool_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned n)
1088 ret = __mempool_get_bulk(mp, obj_table, n,
1089 !(mp->flags & MEMPOOL_F_SC_GET));
1091 __mempool_check_cookies(mp, obj_table, n, 1);
1096 * Get one object from the mempool (multi-consumers safe).
1098 * If cache is enabled, objects will be retrieved first from cache,
1099 * subsequently from the common pool. Note that it can return -ENOENT when
1100 * the local cache and common pool are empty, even if cache from other
1104 * A pointer to the mempool structure.
1106 * A pointer to a void * pointer (object) that will be filled.
1108 * - 0: Success; objects taken.
1109 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
1111 static inline int __attribute__((always_inline))
1112 rte_mempool_mc_get(struct rte_mempool *mp, void **obj_p)
1114 return rte_mempool_mc_get_bulk(mp, obj_p, 1);
1118 * Get one object from the mempool (NOT multi-consumers safe).
1120 * If cache is enabled, objects will be retrieved first from cache,
1121 * subsequently from the common pool. Note that it can return -ENOENT when
1122 * the local cache and common pool are empty, even if cache from other
1126 * A pointer to the mempool structure.
1128 * A pointer to a void * pointer (object) that will be filled.
1130 * - 0: Success; objects taken.
1131 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
1133 static inline int __attribute__((always_inline))
1134 rte_mempool_sc_get(struct rte_mempool *mp, void **obj_p)
1136 return rte_mempool_sc_get_bulk(mp, obj_p, 1);
1140 * Get one object from the mempool.
1142 * This function calls the multi-consumers or the single-consumer
1143 * version, depending on the default behavior that was specified at
1144 * mempool creation (see flags).
1146 * If cache is enabled, objects will be retrieved first from cache,
1147 * subsequently from the common pool. Note that it can return -ENOENT when
1148 * the local cache and common pool are empty, even if cache from other
1152 * A pointer to the mempool structure.
1154 * A pointer to a void * pointer (object) that will be filled.
1156 * - 0: Success; objects taken.
1157 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
1159 static inline int __attribute__((always_inline))
1160 rte_mempool_get(struct rte_mempool *mp, void **obj_p)
1162 return rte_mempool_get_bulk(mp, obj_p, 1);
1166 * Return the number of entries in the mempool.
1168 * When cache is enabled, this function has to browse the length of
1169 * all lcores, so it should not be used in a data path, but only for
1173 * A pointer to the mempool structure.
1175 * The number of entries in the mempool.
1177 unsigned rte_mempool_count(const struct rte_mempool *mp);
1180 * Return the number of free entries in the mempool ring.
1181 * i.e. how many entries can be freed back to the mempool.
1183 * NOTE: This corresponds to the number of elements *allocated* from the
1184 * memory pool, not the number of elements in the pool itself. To count
1185 * the number elements currently available in the pool, use "rte_mempool_count"
1187 * When cache is enabled, this function has to browse the length of
1188 * all lcores, so it should not be used in a data path, but only for
1192 * A pointer to the mempool structure.
1194 * The number of free entries in the mempool.
1196 static inline unsigned
1197 rte_mempool_free_count(const struct rte_mempool *mp)
1199 return mp->size - rte_mempool_count(mp);
1203 * Test if the mempool is full.
1205 * When cache is enabled, this function has to browse the length of all
1206 * lcores, so it should not be used in a data path, but only for debug
1210 * A pointer to the mempool structure.
1212 * - 1: The mempool is full.
1213 * - 0: The mempool is not full.
1216 rte_mempool_full(const struct rte_mempool *mp)
1218 return !!(rte_mempool_count(mp) == mp->size);
1222 * Test if the mempool is empty.
1224 * When cache is enabled, this function has to browse the length of all
1225 * lcores, so it should not be used in a data path, but only for debug
1229 * A pointer to the mempool structure.
1231 * - 1: The mempool is empty.
1232 * - 0: The mempool is not empty.
1235 rte_mempool_empty(const struct rte_mempool *mp)
1237 return !!(rte_mempool_count(mp) == 0);
1241 * Return the physical address of elt, which is an element of the pool mp.
1244 * A pointer to the mempool structure.
1246 * A pointer (virtual address) to the element of the pool.
1248 * The physical address of the elt element.
1250 static inline phys_addr_t
1251 rte_mempool_virt2phy(const struct rte_mempool *mp, const void *elt)
1253 if (rte_eal_has_hugepages()) {
1256 off = (const char *)elt - (const char *)mp->elt_va_start;
1257 return (mp->elt_pa[off >> mp->pg_shift] + (off & mp->pg_mask));
1260 * If huge pages are disabled, we cannot assume the
1261 * memory region to be physically contiguous.
1262 * Lookup for each element.
1264 return rte_mem_virt2phy(elt);
1269 * Check the consistency of mempool objects.
1271 * Verify the coherency of fields in the mempool structure. Also check
1272 * that the cookies of mempool objects (even the ones that are not
1273 * present in pool) have a correct value. If not, a panic will occur.
1276 * A pointer to the mempool structure.
1278 void rte_mempool_audit(const struct rte_mempool *mp);
1281 * Return a pointer to the private data in an mempool structure.
1284 * A pointer to the mempool structure.
1286 * A pointer to the private data.
1288 static inline void *rte_mempool_get_priv(struct rte_mempool *mp)
1290 return (char *)mp + MEMPOOL_HEADER_SIZE(mp, mp->pg_num);
1294 * Dump the status of all mempools on the console
1297 * A pointer to a file for output
1299 void rte_mempool_list_dump(FILE *f);
1302 * Search a mempool from its name
1305 * The name of the mempool.
1307 * The pointer to the mempool matching the name, or NULL if not found.
1309 * with rte_errno set appropriately. Possible rte_errno values include:
1310 * - ENOENT - required entry not available to return.
1313 struct rte_mempool *rte_mempool_lookup(const char *name);
1316 * Get the header, trailer and total size of a mempool element.
1318 * Given a desired size of the mempool element and mempool flags,
1319 * calculates header, trailer, body and total sizes of the mempool object.
1322 * The size of each element.
1324 * The flags used for the mempool creation.
1325 * Consult rte_mempool_create() for more information about possible values.
1326 * The size of each element.
1328 * The calculated detailed size the mempool object. May be NULL.
1330 * Total size of the mempool object.
1332 uint32_t rte_mempool_calc_obj_size(uint32_t elt_size, uint32_t flags,
1333 struct rte_mempool_objsz *sz);
1336 * Get the size of memory required to store mempool elements.
1338 * Calculate the maximum amount of memory required to store given number
1339 * of objects. Assume that the memory buffer will be aligned at page
1342 * Note that if object size is bigger then page size, then it assumes
1343 * that pages are grouped in subsets of physically continuous pages big
1344 * enough to store at least one object.
1347 * Number of elements.
1349 * The size of each element.
1351 * LOG2 of the physical pages size.
1353 * Required memory size aligned at page boundary.
1355 size_t rte_mempool_xmem_size(uint32_t elt_num, size_t elt_sz,
1359 * Get the size of memory required to store mempool elements.
1361 * Calculate how much memory would be actually required with the given
1362 * memory footprint to store required number of objects.
1365 * Virtual address of the externally allocated memory buffer.
1366 * Will be used to store mempool objects.
1368 * Number of elements.
1370 * The size of each element.
1372 * Array of physical addresses of the pages that comprises given memory
1375 * Number of elements in the paddr array.
1377 * LOG2 of the physical pages size.
1379 * On success, the number of bytes needed to store given number of
1380 * objects, aligned to the given page size. If the provided memory
1381 * buffer is too small, return a negative value whose absolute value
1382 * is the actual number of elements that can be stored in that buffer.
1384 ssize_t rte_mempool_xmem_usage(void *vaddr, uint32_t elt_num, size_t elt_sz,
1385 const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift);
1388 * Walk list of all memory pools
1393 * Argument passed to iterator
1395 void rte_mempool_walk(void (*func)(const struct rte_mempool *, void *arg),
1402 #endif /* _RTE_MEMPOOL_H_ */