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 (struct rte_mempool_objhdr *)((char *)obj -
266 sizeof(struct rte_mempool_objhdr));
269 /* return the trailer of a mempool object (internal) */
270 static inline struct rte_mempool_objtlr *__mempool_get_trailer(void *obj)
272 return (struct rte_mempool_objtlr *)((char *)obj -
273 sizeof(struct rte_mempool_objtlr));
277 * Return a pointer to the mempool owning this object.
280 * An object that is owned by a pool. If this is not the case,
281 * the behavior is undefined.
283 * A pointer to the mempool structure.
285 static inline struct rte_mempool *rte_mempool_from_obj(void *obj)
287 struct rte_mempool_objhdr *hdr = __mempool_get_header(obj);
292 * @internal Check and update cookies or panic.
295 * Pointer to the memory pool.
296 * @param obj_table_const
297 * Pointer to a table of void * pointers (objects).
299 * Index of object in object table.
301 * - 0: object is supposed to be allocated, mark it as free
302 * - 1: object is supposed to be free, mark it as allocated
303 * - 2: just check that cookie is valid (free or allocated)
305 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
306 #ifndef __INTEL_COMPILER
307 #pragma GCC diagnostic ignored "-Wcast-qual"
309 static inline void __mempool_check_cookies(const struct rte_mempool *mp,
310 void * const *obj_table_const,
311 unsigned n, int free)
313 struct rte_mempool_objhdr *hdr;
314 struct rte_mempool_objtlr *tlr;
320 /* Force to drop the "const" attribute. This is done only when
321 * DEBUG is enabled */
322 tmp = (void *) obj_table_const;
323 obj_table = (void **) tmp;
328 if (rte_mempool_from_obj(obj) != mp)
329 rte_panic("MEMPOOL: object is owned by another "
332 hdr = __mempool_get_header(obj);
333 cookie = hdr->cookie;
336 if (cookie != RTE_MEMPOOL_HEADER_COOKIE1) {
337 rte_log_set_history(0);
338 RTE_LOG(CRIT, MEMPOOL,
339 "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
340 obj, (const void *) mp, cookie);
341 rte_panic("MEMPOOL: bad header cookie (put)\n");
343 hdr->cookie = RTE_MEMPOOL_HEADER_COOKIE2;
345 else if (free == 1) {
346 if (cookie != RTE_MEMPOOL_HEADER_COOKIE2) {
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 (get)\n");
353 hdr->cookie = RTE_MEMPOOL_HEADER_COOKIE1;
355 else if (free == 2) {
356 if (cookie != RTE_MEMPOOL_HEADER_COOKIE1 &&
357 cookie != RTE_MEMPOOL_HEADER_COOKIE2) {
358 rte_log_set_history(0);
359 RTE_LOG(CRIT, MEMPOOL,
360 "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
361 obj, (const void *) mp, cookie);
362 rte_panic("MEMPOOL: bad header cookie (audit)\n");
365 tlr = __mempool_get_trailer(obj);
366 cookie = tlr->cookie;
367 if (cookie != RTE_MEMPOOL_TRAILER_COOKIE) {
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 trailer cookie\n");
376 #ifndef __INTEL_COMPILER
377 #pragma GCC diagnostic error "-Wcast-qual"
380 #define __mempool_check_cookies(mp, obj_table_const, n, free) do {} while(0)
381 #endif /* RTE_LIBRTE_MEMPOOL_DEBUG */
384 * A mempool object iterator callback function.
386 typedef void (*rte_mempool_obj_iter_t)(void * /*obj_iter_arg*/,
387 void * /*obj_start*/,
389 uint32_t /*obj_index */);
392 * Call a function for each mempool object in a memory chunk
394 * Iterate across objects of the given size and alignment in the
395 * provided chunk of memory. The given memory buffer can consist of
396 * disjoint physical pages.
398 * For each object, calls the provided callback (if any). This function
399 * is used to populate mempool, walk through all elements of the
400 * mempool, estimate how many elements of the given size could be
401 * created in the given memory buffer.
404 * Virtual address of the memory buffer.
406 * Maximum number of objects to iterate through.
408 * Size 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.
425 uint32_t rte_mempool_obj_iter(void *vaddr,
426 uint32_t elt_num, size_t elt_sz, size_t align,
427 const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift,
428 rte_mempool_obj_iter_t obj_iter, void *obj_iter_arg);
431 * An object constructor callback function for mempool.
433 * Arguments are the mempool, the opaque pointer given by the user in
434 * rte_mempool_create(), the pointer to the element and the index of
435 * the element in the pool.
437 typedef void (rte_mempool_obj_ctor_t)(struct rte_mempool *, void *,
441 * A mempool constructor callback function.
443 * Arguments are the mempool and the opaque pointer given by the user in
444 * rte_mempool_create().
446 typedef void (rte_mempool_ctor_t)(struct rte_mempool *, void *);
449 * Create a new mempool named *name* in memory.
451 * This function uses ``memzone_reserve()`` to allocate memory. The
452 * pool contains n elements of elt_size. Its size is set to n.
453 * All elements of the mempool are allocated together with the mempool header,
454 * in one physically continuous chunk of memory.
457 * The name of the mempool.
459 * The number of elements in the mempool. The optimum size (in terms of
460 * memory usage) for a mempool is when n is a power of two minus one:
463 * The size of each element.
465 * If cache_size is non-zero, the rte_mempool library will try to
466 * limit the accesses to the common lockless pool, by maintaining a
467 * per-lcore object cache. This argument must be lower or equal to
468 * CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE and n / 1.5. It is advised to choose
469 * cache_size to have "n modulo cache_size == 0": if this is
470 * not the case, some elements will always stay in the pool and will
471 * never be used. The access to the per-lcore table is of course
472 * faster than the multi-producer/consumer pool. The cache can be
473 * disabled if the cache_size argument is set to 0; it can be useful to
474 * avoid losing objects in cache. Note that even if not used, the
475 * memory space for cache is always reserved in a mempool structure,
476 * except if CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE is set to 0.
477 * @param private_data_size
478 * The size of the private data appended after the mempool
479 * structure. This is useful for storing some private data after the
480 * mempool structure, as is done for rte_mbuf_pool for example.
482 * A function pointer that is called for initialization of the pool,
483 * before object initialization. The user can initialize the private
484 * data in this function if needed. This parameter can be NULL if
487 * An opaque pointer to data that can be used in the mempool
488 * constructor function.
490 * A function pointer that is called for each object at
491 * initialization of the pool. The user can set some meta data in
492 * objects if needed. This parameter can be NULL if not needed.
493 * The obj_init() function takes the mempool pointer, the init_arg,
494 * the object pointer and the object number as parameters.
495 * @param obj_init_arg
496 * An opaque pointer to data that can be used as an argument for
497 * each call to the object constructor function.
499 * The *socket_id* argument is the socket identifier in the case of
500 * NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
501 * constraint for the reserved zone.
503 * The *flags* arguments is an OR of following flags:
504 * - MEMPOOL_F_NO_SPREAD: By default, objects addresses are spread
505 * between channels in RAM: the pool allocator will add padding
506 * between objects depending on the hardware configuration. See
507 * Memory alignment constraints for details. If this flag is set,
508 * the allocator will just align them to a cache line.
509 * - MEMPOOL_F_NO_CACHE_ALIGN: By default, the returned objects are
510 * cache-aligned. This flag removes this constraint, and no
511 * padding will be present between objects. This flag implies
512 * MEMPOOL_F_NO_SPREAD.
513 * - MEMPOOL_F_SP_PUT: If this flag is set, the default behavior
514 * when using rte_mempool_put() or rte_mempool_put_bulk() is
515 * "single-producer". Otherwise, it is "multi-producers".
516 * - MEMPOOL_F_SC_GET: If this flag is set, the default behavior
517 * when using rte_mempool_get() or rte_mempool_get_bulk() is
518 * "single-consumer". Otherwise, it is "multi-consumers".
520 * The pointer to the new allocated mempool, on success. NULL on error
521 * with rte_errno set appropriately. Possible rte_errno values include:
522 * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
523 * - E_RTE_SECONDARY - function was called from a secondary process instance
524 * - EINVAL - cache size provided is too large
525 * - ENOSPC - the maximum number of memzones has already been allocated
526 * - EEXIST - a memzone with the same name already exists
527 * - ENOMEM - no appropriate memory area found in which to create memzone
530 rte_mempool_create(const char *name, unsigned n, unsigned elt_size,
531 unsigned cache_size, unsigned private_data_size,
532 rte_mempool_ctor_t *mp_init, void *mp_init_arg,
533 rte_mempool_obj_ctor_t *obj_init, void *obj_init_arg,
534 int socket_id, unsigned flags);
537 * Create a new mempool named *name* in memory.
539 * This function uses ``memzone_reserve()`` to allocate memory. The
540 * pool contains n elements of elt_size. Its size is set to n.
541 * Depending on the input parameters, mempool elements can be either allocated
542 * together with the mempool header, or an externally provided memory buffer
543 * could be used to store mempool objects. In later case, that external
544 * memory buffer can consist of set of disjoint physical pages.
547 * The name of the mempool.
549 * The number of elements in the mempool. The optimum size (in terms of
550 * memory usage) for a mempool is when n is a power of two minus one:
553 * The size of each element.
555 * If cache_size is non-zero, the rte_mempool library will try to
556 * limit the accesses to the common lockless pool, by maintaining a
557 * per-lcore object cache. This argument must be lower or equal to
558 * CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE. It is advised to choose
559 * cache_size to have "n modulo cache_size == 0": if this is
560 * not the case, some elements will always stay in the pool and will
561 * never be used. The access to the per-lcore table is of course
562 * faster than the multi-producer/consumer pool. The cache can be
563 * disabled if the cache_size argument is set to 0; it can be useful to
564 * avoid losing objects in cache. Note that even if not used, the
565 * memory space for cache is always reserved in a mempool structure,
566 * except if CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE is set to 0.
567 * @param private_data_size
568 * The size of the private data appended after the mempool
569 * structure. This is useful for storing some private data after the
570 * mempool structure, as is done for rte_mbuf_pool for example.
572 * A function pointer that is called for initialization of the pool,
573 * before object initialization. The user can initialize the private
574 * data in this function if needed. This parameter can be NULL if
577 * An opaque pointer to data that can be used in the mempool
578 * constructor function.
580 * A function pointer that is called for each object at
581 * initialization of the pool. The user can set some meta data in
582 * objects if needed. This parameter can be NULL if not needed.
583 * The obj_init() function takes the mempool pointer, the init_arg,
584 * the object pointer and the object number as parameters.
585 * @param obj_init_arg
586 * An opaque pointer to data that can be used as an argument for
587 * each call to the object constructor function.
589 * The *socket_id* argument is the socket identifier in the case of
590 * NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
591 * constraint for the reserved zone.
593 * The *flags* arguments is an OR of following flags:
594 * - MEMPOOL_F_NO_SPREAD: By default, objects addresses are spread
595 * between channels in RAM: the pool allocator will add padding
596 * between objects depending on the hardware configuration. See
597 * Memory alignment constraints for details. If this flag is set,
598 * the allocator will just align them to a cache line.
599 * - MEMPOOL_F_NO_CACHE_ALIGN: By default, the returned objects are
600 * cache-aligned. This flag removes this constraint, and no
601 * padding will be present between objects. This flag implies
602 * MEMPOOL_F_NO_SPREAD.
603 * - MEMPOOL_F_SP_PUT: If this flag is set, the default behavior
604 * when using rte_mempool_put() or rte_mempool_put_bulk() is
605 * "single-producer". Otherwise, it is "multi-producers".
606 * - MEMPOOL_F_SC_GET: If this flag is set, the default behavior
607 * when using rte_mempool_get() or rte_mempool_get_bulk() is
608 * "single-consumer". Otherwise, it is "multi-consumers".
610 * Virtual address of the externally allocated memory buffer.
611 * Will be used to store mempool objects.
613 * Array of physical addresses of the pages that comprises given memory
616 * Number of elements in the paddr array.
618 * LOG2 of the physical pages size.
620 * The pointer to the new allocated mempool, on success. NULL on error
621 * with rte_errno set appropriately. Possible rte_errno values include:
622 * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
623 * - E_RTE_SECONDARY - function was called from a secondary process instance
624 * - EINVAL - cache size provided is too large
625 * - ENOSPC - the maximum number of memzones has already been allocated
626 * - EEXIST - a memzone with the same name already exists
627 * - ENOMEM - no appropriate memory area found in which to create memzone
630 rte_mempool_xmem_create(const char *name, unsigned n, unsigned elt_size,
631 unsigned cache_size, unsigned private_data_size,
632 rte_mempool_ctor_t *mp_init, void *mp_init_arg,
633 rte_mempool_obj_ctor_t *obj_init, void *obj_init_arg,
634 int socket_id, unsigned flags, void *vaddr,
635 const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift);
637 #ifdef RTE_LIBRTE_XEN_DOM0
639 * Create a new mempool named *name* in memory on Xen Dom0.
641 * This function uses ``rte_mempool_xmem_create()`` to allocate memory. The
642 * pool contains n elements of elt_size. Its size is set to n.
643 * All elements of the mempool are allocated together with the mempool header,
644 * and memory buffer can consist of set of disjoint physical pages.
647 * The name of the mempool.
649 * The number of elements in the mempool. The optimum size (in terms of
650 * memory usage) for a mempool is when n is a power of two minus one:
653 * The size of each element.
655 * If cache_size is non-zero, the rte_mempool library will try to
656 * limit the accesses to the common lockless pool, by maintaining a
657 * per-lcore object cache. This argument must be lower or equal to
658 * CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE. It is advised to choose
659 * cache_size to have "n modulo cache_size == 0": if this is
660 * not the case, some elements will always stay in the pool and will
661 * never be used. The access to the per-lcore table is of course
662 * faster than the multi-producer/consumer pool. The cache can be
663 * disabled if the cache_size argument is set to 0; it can be useful to
664 * avoid losing objects in cache. Note that even if not used, the
665 * memory space for cache is always reserved in a mempool structure,
666 * except if CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE is set to 0.
667 * @param private_data_size
668 * The size of the private data appended after the mempool
669 * structure. This is useful for storing some private data after the
670 * mempool structure, as is done for rte_mbuf_pool for example.
672 * A function pointer that is called for initialization of the pool,
673 * before object initialization. The user can initialize the private
674 * data in this function if needed. This parameter can be NULL if
677 * An opaque pointer to data that can be used in the mempool
678 * constructor function.
680 * A function pointer that is called for each object at
681 * initialization of the pool. The user can set some meta data in
682 * objects if needed. This parameter can be NULL if not needed.
683 * The obj_init() function takes the mempool pointer, the init_arg,
684 * the object pointer and the object number as parameters.
685 * @param obj_init_arg
686 * An opaque pointer to data that can be used as an argument for
687 * each call to the object constructor function.
689 * The *socket_id* argument is the socket identifier in the case of
690 * NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
691 * constraint for the reserved zone.
693 * The *flags* arguments is an OR of following flags:
694 * - MEMPOOL_F_NO_SPREAD: By default, objects addresses are spread
695 * between channels in RAM: the pool allocator will add padding
696 * between objects depending on the hardware configuration. See
697 * Memory alignment constraints for details. If this flag is set,
698 * the allocator will just align them to a cache line.
699 * - MEMPOOL_F_NO_CACHE_ALIGN: By default, the returned objects are
700 * cache-aligned. This flag removes this constraint, and no
701 * padding will be present between objects. This flag implies
702 * MEMPOOL_F_NO_SPREAD.
703 * - MEMPOOL_F_SP_PUT: If this flag is set, the default behavior
704 * when using rte_mempool_put() or rte_mempool_put_bulk() is
705 * "single-producer". Otherwise, it is "multi-producers".
706 * - MEMPOOL_F_SC_GET: If this flag is set, the default behavior
707 * when using rte_mempool_get() or rte_mempool_get_bulk() is
708 * "single-consumer". Otherwise, it is "multi-consumers".
710 * The pointer to the new allocated mempool, on success. NULL on error
711 * with rte_errno set appropriately. Possible rte_errno values include:
712 * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
713 * - E_RTE_SECONDARY - function was called from a secondary process instance
714 * - EINVAL - cache size provided is too large
715 * - ENOSPC - the maximum number of memzones has already been allocated
716 * - EEXIST - a memzone with the same name already exists
717 * - ENOMEM - no appropriate memory area found in which to create memzone
720 rte_dom0_mempool_create(const char *name, unsigned n, unsigned elt_size,
721 unsigned cache_size, unsigned private_data_size,
722 rte_mempool_ctor_t *mp_init, void *mp_init_arg,
723 rte_mempool_obj_ctor_t *obj_init, void *obj_init_arg,
724 int socket_id, unsigned flags);
728 * Dump the status of the mempool to the console.
731 * A pointer to a file for output
733 * A pointer to the mempool structure.
735 void rte_mempool_dump(FILE *f, const struct rte_mempool *mp);
738 * @internal Put several objects back in the mempool; used internally.
740 * A pointer to the mempool structure.
742 * A pointer to a table of void * pointers (objects).
744 * The number of objects to store back in the mempool, must be strictly
747 * Mono-producer (0) or multi-producers (1).
749 static inline void __attribute__((always_inline))
750 __mempool_put_bulk(struct rte_mempool *mp, void * const *obj_table,
751 unsigned n, int is_mp)
753 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
754 struct rte_mempool_cache *cache;
757 unsigned lcore_id = rte_lcore_id();
758 uint32_t cache_size = mp->cache_size;
759 uint32_t flushthresh = mp->cache_flushthresh;
760 #endif /* RTE_MEMPOOL_CACHE_MAX_SIZE > 0 */
762 /* increment stat now, adding in mempool always success */
763 __MEMPOOL_STAT_ADD(mp, put, n);
765 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
766 /* cache is not enabled or single producer or non-EAL thread */
767 if (unlikely(cache_size == 0 || is_mp == 0 ||
768 lcore_id >= RTE_MAX_LCORE))
771 /* Go straight to ring if put would overflow mem allocated for cache */
772 if (unlikely(n > RTE_MEMPOOL_CACHE_MAX_SIZE))
775 cache = &mp->local_cache[lcore_id];
776 cache_objs = &cache->objs[cache->len];
779 * The cache follows the following algorithm
780 * 1. Add the objects to the cache
781 * 2. Anything greater than the cache min value (if it crosses the
782 * cache flush threshold) is flushed to the ring.
785 /* Add elements back into the cache */
786 for (index = 0; index < n; ++index, obj_table++)
787 cache_objs[index] = *obj_table;
791 if (cache->len >= flushthresh) {
792 rte_ring_mp_enqueue_bulk(mp->ring, &cache->objs[cache_size],
793 cache->len - cache_size);
794 cache->len = cache_size;
800 #endif /* RTE_MEMPOOL_CACHE_MAX_SIZE > 0 */
802 /* push remaining objects in ring */
803 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
805 if (rte_ring_mp_enqueue_bulk(mp->ring, obj_table, n) < 0)
806 rte_panic("cannot put objects in mempool\n");
809 if (rte_ring_sp_enqueue_bulk(mp->ring, obj_table, n) < 0)
810 rte_panic("cannot put objects in mempool\n");
814 rte_ring_mp_enqueue_bulk(mp->ring, obj_table, n);
816 rte_ring_sp_enqueue_bulk(mp->ring, obj_table, n);
822 * Put several objects back in the mempool (multi-producers safe).
825 * A pointer to the mempool structure.
827 * A pointer to a table of void * pointers (objects).
829 * The number of objects to add in the mempool from the obj_table.
831 static inline void __attribute__((always_inline))
832 rte_mempool_mp_put_bulk(struct rte_mempool *mp, void * const *obj_table,
835 __mempool_check_cookies(mp, obj_table, n, 0);
836 __mempool_put_bulk(mp, obj_table, n, 1);
840 * Put several objects back in the mempool (NOT multi-producers safe).
843 * A pointer to the mempool structure.
845 * A pointer to a table of void * pointers (objects).
847 * The number of objects to add in the mempool from obj_table.
850 rte_mempool_sp_put_bulk(struct rte_mempool *mp, void * const *obj_table,
853 __mempool_check_cookies(mp, obj_table, n, 0);
854 __mempool_put_bulk(mp, obj_table, n, 0);
858 * Put several objects back in the mempool.
860 * This function calls the multi-producer or the single-producer
861 * version depending on the default behavior that was specified at
862 * mempool creation time (see flags).
865 * A pointer to the mempool structure.
867 * A pointer to a table of void * pointers (objects).
869 * The number of objects to add in the mempool from obj_table.
871 static inline void __attribute__((always_inline))
872 rte_mempool_put_bulk(struct rte_mempool *mp, void * const *obj_table,
875 __mempool_check_cookies(mp, obj_table, n, 0);
876 __mempool_put_bulk(mp, obj_table, n, !(mp->flags & MEMPOOL_F_SP_PUT));
880 * Put one object in the mempool (multi-producers safe).
883 * A pointer to the mempool structure.
885 * A pointer to the object to be added.
887 static inline void __attribute__((always_inline))
888 rte_mempool_mp_put(struct rte_mempool *mp, void *obj)
890 rte_mempool_mp_put_bulk(mp, &obj, 1);
894 * Put one object back in the mempool (NOT multi-producers safe).
897 * A pointer to the mempool structure.
899 * A pointer to the object to be added.
901 static inline void __attribute__((always_inline))
902 rte_mempool_sp_put(struct rte_mempool *mp, void *obj)
904 rte_mempool_sp_put_bulk(mp, &obj, 1);
908 * Put one object back in the mempool.
910 * This function calls the multi-producer or the single-producer
911 * version depending on the default behavior that was specified at
912 * mempool creation time (see flags).
915 * A pointer to the mempool structure.
917 * A pointer to the object to be added.
919 static inline void __attribute__((always_inline))
920 rte_mempool_put(struct rte_mempool *mp, void *obj)
922 rte_mempool_put_bulk(mp, &obj, 1);
926 * @internal Get several objects from the mempool; used internally.
928 * A pointer to the mempool structure.
930 * A pointer to a table of void * pointers (objects).
932 * The number of objects to get, must be strictly positive.
934 * Mono-consumer (0) or multi-consumers (1).
936 * - >=0: Success; number of objects supplied.
937 * - <0: Error; code of ring dequeue function.
939 static inline int __attribute__((always_inline))
940 __mempool_get_bulk(struct rte_mempool *mp, void **obj_table,
941 unsigned n, int is_mc)
944 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
945 struct rte_mempool_cache *cache;
948 unsigned lcore_id = rte_lcore_id();
949 uint32_t cache_size = mp->cache_size;
951 /* cache is not enabled or single consumer */
952 if (unlikely(cache_size == 0 || is_mc == 0 ||
953 n >= cache_size || lcore_id >= RTE_MAX_LCORE))
956 cache = &mp->local_cache[lcore_id];
957 cache_objs = cache->objs;
959 /* Can this be satisfied from the cache? */
960 if (cache->len < n) {
961 /* No. Backfill the cache first, and then fill from it */
962 uint32_t req = n + (cache_size - cache->len);
964 /* How many do we require i.e. number to fill the cache + the request */
965 ret = rte_ring_mc_dequeue_bulk(mp->ring, &cache->objs[cache->len], req);
966 if (unlikely(ret < 0)) {
968 * In the offchance that we are buffer constrained,
969 * where we are not able to allocate cache + n, go to
970 * the ring directly. If that fails, we are truly out of
979 /* Now fill in the response ... */
980 for (index = 0, len = cache->len - 1; index < n; ++index, len--, obj_table++)
981 *obj_table = cache_objs[len];
985 __MEMPOOL_STAT_ADD(mp, get_success, n);
990 #endif /* RTE_MEMPOOL_CACHE_MAX_SIZE > 0 */
992 /* get remaining objects from ring */
994 ret = rte_ring_mc_dequeue_bulk(mp->ring, obj_table, n);
996 ret = rte_ring_sc_dequeue_bulk(mp->ring, obj_table, n);
999 __MEMPOOL_STAT_ADD(mp, get_fail, n);
1001 __MEMPOOL_STAT_ADD(mp, get_success, n);
1007 * Get several objects from the mempool (multi-consumers safe).
1009 * If cache is enabled, objects will be retrieved first from cache,
1010 * subsequently from the common pool. Note that it can return -ENOENT when
1011 * the local cache and common pool are empty, even if cache from other
1015 * A pointer to the mempool structure.
1017 * A pointer to a table of void * pointers (objects) that will be filled.
1019 * The number of objects to get from mempool to obj_table.
1021 * - 0: Success; objects taken.
1022 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
1024 static inline int __attribute__((always_inline))
1025 rte_mempool_mc_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned n)
1028 ret = __mempool_get_bulk(mp, obj_table, n, 1);
1030 __mempool_check_cookies(mp, obj_table, n, 1);
1035 * Get several objects from the mempool (NOT multi-consumers safe).
1037 * If cache is enabled, objects will be retrieved first from cache,
1038 * subsequently from the common pool. Note that it can return -ENOENT when
1039 * the local cache and common pool are empty, even if cache from other
1043 * A pointer to the mempool structure.
1045 * A pointer to a table of void * pointers (objects) that will be filled.
1047 * The number of objects to get from the mempool to obj_table.
1049 * - 0: Success; objects taken.
1050 * - -ENOENT: Not enough entries in the mempool; no object is
1053 static inline int __attribute__((always_inline))
1054 rte_mempool_sc_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned n)
1057 ret = __mempool_get_bulk(mp, obj_table, n, 0);
1059 __mempool_check_cookies(mp, obj_table, n, 1);
1064 * Get several objects from the mempool.
1066 * This function calls the multi-consumers or the single-consumer
1067 * version, depending on the default behaviour that was specified at
1068 * mempool creation time (see flags).
1070 * If cache is enabled, objects will be retrieved first from cache,
1071 * subsequently from the common pool. Note that it can return -ENOENT when
1072 * the local cache and common pool are empty, even if cache from other
1076 * A pointer to the mempool structure.
1078 * A pointer to a table of void * pointers (objects) that will be filled.
1080 * The number of objects to get from the mempool to obj_table.
1082 * - 0: Success; objects taken
1083 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
1085 static inline int __attribute__((always_inline))
1086 rte_mempool_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned n)
1089 ret = __mempool_get_bulk(mp, obj_table, n,
1090 !(mp->flags & MEMPOOL_F_SC_GET));
1092 __mempool_check_cookies(mp, obj_table, n, 1);
1097 * Get one object from the mempool (multi-consumers safe).
1099 * If cache is enabled, objects will be retrieved first from cache,
1100 * subsequently from the common pool. Note that it can return -ENOENT when
1101 * the local cache and common pool are empty, even if cache from other
1105 * A pointer to the mempool structure.
1107 * A pointer to a void * pointer (object) that will be filled.
1109 * - 0: Success; objects taken.
1110 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
1112 static inline int __attribute__((always_inline))
1113 rte_mempool_mc_get(struct rte_mempool *mp, void **obj_p)
1115 return rte_mempool_mc_get_bulk(mp, obj_p, 1);
1119 * Get one object from the mempool (NOT multi-consumers safe).
1121 * If cache is enabled, objects will be retrieved first from cache,
1122 * subsequently from the common pool. Note that it can return -ENOENT when
1123 * the local cache and common pool are empty, even if cache from other
1127 * A pointer to the mempool structure.
1129 * A pointer to a void * pointer (object) that will be filled.
1131 * - 0: Success; objects taken.
1132 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
1134 static inline int __attribute__((always_inline))
1135 rte_mempool_sc_get(struct rte_mempool *mp, void **obj_p)
1137 return rte_mempool_sc_get_bulk(mp, obj_p, 1);
1141 * Get one object from the mempool.
1143 * This function calls the multi-consumers or the single-consumer
1144 * version, depending on the default behavior that was specified at
1145 * mempool creation (see flags).
1147 * If cache is enabled, objects will be retrieved first from cache,
1148 * subsequently from the common pool. Note that it can return -ENOENT when
1149 * the local cache and common pool are empty, even if cache from other
1153 * A pointer to the mempool structure.
1155 * A pointer to a void * pointer (object) that will be filled.
1157 * - 0: Success; objects taken.
1158 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
1160 static inline int __attribute__((always_inline))
1161 rte_mempool_get(struct rte_mempool *mp, void **obj_p)
1163 return rte_mempool_get_bulk(mp, obj_p, 1);
1167 * Return the number of entries in the mempool.
1169 * When cache is enabled, this function has to browse the length of
1170 * all lcores, so it should not be used in a data path, but only for
1174 * A pointer to the mempool structure.
1176 * The number of entries in the mempool.
1178 unsigned rte_mempool_count(const struct rte_mempool *mp);
1181 * Return the number of free entries in the mempool ring.
1182 * i.e. how many entries can be freed back to the mempool.
1184 * NOTE: This corresponds to the number of elements *allocated* from the
1185 * memory pool, not the number of elements in the pool itself. To count
1186 * the number elements currently available in the pool, use "rte_mempool_count"
1188 * When cache is enabled, this function has to browse the length of
1189 * all lcores, so it should not be used in a data path, but only for
1193 * A pointer to the mempool structure.
1195 * The number of free entries in the mempool.
1197 static inline unsigned
1198 rte_mempool_free_count(const struct rte_mempool *mp)
1200 return mp->size - rte_mempool_count(mp);
1204 * Test if the mempool is full.
1206 * When cache is enabled, this function has to browse the length of all
1207 * lcores, so it should not be used in a data path, but only for debug
1211 * A pointer to the mempool structure.
1213 * - 1: The mempool is full.
1214 * - 0: The mempool is not full.
1217 rte_mempool_full(const struct rte_mempool *mp)
1219 return !!(rte_mempool_count(mp) == mp->size);
1223 * Test if the mempool is empty.
1225 * When cache is enabled, this function has to browse the length of all
1226 * lcores, so it should not be used in a data path, but only for debug
1230 * A pointer to the mempool structure.
1232 * - 1: The mempool is empty.
1233 * - 0: The mempool is not empty.
1236 rte_mempool_empty(const struct rte_mempool *mp)
1238 return !!(rte_mempool_count(mp) == 0);
1242 * Return the physical address of elt, which is an element of the pool mp.
1245 * A pointer to the mempool structure.
1247 * A pointer (virtual address) to the element of the pool.
1249 * The physical address of the elt element.
1251 static inline phys_addr_t
1252 rte_mempool_virt2phy(const struct rte_mempool *mp, const void *elt)
1254 if (rte_eal_has_hugepages()) {
1257 off = (const char *)elt - (const char *)mp->elt_va_start;
1258 return (mp->elt_pa[off >> mp->pg_shift] + (off & mp->pg_mask));
1261 * If huge pages are disabled, we cannot assume the
1262 * memory region to be physically contiguous.
1263 * Lookup for each element.
1265 return rte_mem_virt2phy(elt);
1270 * Check the consistency of mempool objects.
1272 * Verify the coherency of fields in the mempool structure. Also check
1273 * that the cookies of mempool objects (even the ones that are not
1274 * present in pool) have a correct value. If not, a panic will occur.
1277 * A pointer to the mempool structure.
1279 void rte_mempool_audit(const struct rte_mempool *mp);
1282 * Return a pointer to the private data in an mempool structure.
1285 * A pointer to the mempool structure.
1287 * A pointer to the private data.
1289 static inline void *rte_mempool_get_priv(struct rte_mempool *mp)
1291 return (char *)mp + MEMPOOL_HEADER_SIZE(mp, mp->pg_num);
1295 * Dump the status of all mempools on the console
1298 * A pointer to a file for output
1300 void rte_mempool_list_dump(FILE *f);
1303 * Search a mempool from its name
1306 * The name of the mempool.
1308 * The pointer to the mempool matching the name, or NULL if not found.
1310 * with rte_errno set appropriately. Possible rte_errno values include:
1311 * - ENOENT - required entry not available to return.
1314 struct rte_mempool *rte_mempool_lookup(const char *name);
1317 * Get the header, trailer and total size of a mempool element.
1319 * Given a desired size of the mempool element and mempool flags,
1320 * calculates header, trailer, body and total sizes of the mempool object.
1323 * The size of each element.
1325 * The flags used for the mempool creation.
1326 * Consult rte_mempool_create() for more information about possible values.
1327 * The size of each element.
1329 * The calculated detailed size the mempool object. May be NULL.
1331 * Total size of the mempool object.
1333 uint32_t rte_mempool_calc_obj_size(uint32_t elt_size, uint32_t flags,
1334 struct rte_mempool_objsz *sz);
1337 * Get the size of memory required to store mempool elements.
1339 * Calculate the maximum amount of memory required to store given number
1340 * of objects. Assume that the memory buffer will be aligned at page
1343 * Note that if object size is bigger then page size, then it assumes
1344 * that pages are grouped in subsets of physically continuous pages big
1345 * enough to store at least one object.
1348 * Number of elements.
1350 * The size of each element.
1352 * LOG2 of the physical pages size.
1354 * Required memory size aligned at page boundary.
1356 size_t rte_mempool_xmem_size(uint32_t elt_num, size_t elt_sz,
1360 * Get the size of memory required to store mempool elements.
1362 * Calculate how much memory would be actually required with the given
1363 * memory footprint to store required number of objects.
1366 * Virtual address of the externally allocated memory buffer.
1367 * Will be used to store mempool objects.
1369 * Number of elements.
1371 * The size of each element.
1373 * Array of physical addresses of the pages that comprises given memory
1376 * Number of elements in the paddr array.
1378 * LOG2 of the physical pages size.
1380 * On success, the number of bytes needed to store given number of
1381 * objects, aligned to the given page size. If the provided memory
1382 * buffer is too small, return a negative value whose absolute value
1383 * is the actual number of elements that can be stored in that buffer.
1385 ssize_t rte_mempool_xmem_usage(void *vaddr, uint32_t elt_num, size_t elt_sz,
1386 const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift);
1389 * Walk list of all memory pools
1394 * Argument passed to iterator
1396 void rte_mempool_walk(void (*func)(const struct rte_mempool *, void *arg),
1403 #endif /* _RTE_MEMPOOL_H_ */