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 * disjointed physical pages.
398 * For each object, call the provided callback (if any). This function
399 * is used to populate a mempool, or walk through all the elements of a
400 * mempool, or 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 * Alignment of each object.
412 * Array of physical addresses of the pages that comprises given memory
415 * Number of elements in the paddr array.
417 * LOG2 of the physical pages size.
419 * Object iterator callback function (could be NULL).
420 * @param obj_iter_arg
421 * User defined parameter for the object iterator callback function.
424 * Number of objects iterated through.
426 uint32_t rte_mempool_obj_iter(void *vaddr,
427 uint32_t elt_num, size_t elt_sz, size_t align,
428 const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift,
429 rte_mempool_obj_iter_t obj_iter, void *obj_iter_arg);
432 * An object constructor callback function for mempool.
434 * Arguments are the mempool, the opaque pointer given by the user in
435 * rte_mempool_create(), the pointer to the element and the index of
436 * the element in the pool.
438 typedef void (rte_mempool_obj_ctor_t)(struct rte_mempool *, void *,
442 * A mempool constructor callback function.
444 * Arguments are the mempool and the opaque pointer given by the user in
445 * rte_mempool_create().
447 typedef void (rte_mempool_ctor_t)(struct rte_mempool *, void *);
450 * Create a new mempool named *name* in memory.
452 * This function uses ``memzone_reserve()`` to allocate memory. The
453 * pool contains n elements of elt_size. Its size is set to n.
454 * All elements of the mempool are allocated together with the mempool header,
455 * in one physically continuous chunk of memory.
458 * The name of the mempool.
460 * The number of elements in the mempool. The optimum size (in terms of
461 * memory usage) for a mempool is when n is a power of two minus one:
464 * The size of each element.
466 * If cache_size is non-zero, the rte_mempool library will try to
467 * limit the accesses to the common lockless pool, by maintaining a
468 * per-lcore object cache. This argument must be lower or equal to
469 * CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE and n / 1.5. It is advised to choose
470 * cache_size to have "n modulo cache_size == 0": if this is
471 * not the case, some elements will always stay in the pool and will
472 * never be used. The access to the per-lcore table is of course
473 * faster than the multi-producer/consumer pool. The cache can be
474 * disabled if the cache_size argument is set to 0; it can be useful to
475 * avoid losing objects in cache. Note that even if not used, the
476 * memory space for cache is always reserved in a mempool structure,
477 * except if CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE is set to 0.
478 * @param private_data_size
479 * The size of the private data appended after the mempool
480 * structure. This is useful for storing some private data after the
481 * mempool structure, as is done for rte_mbuf_pool for example.
483 * A function pointer that is called for initialization of the pool,
484 * before object initialization. The user can initialize the private
485 * data in this function if needed. This parameter can be NULL if
488 * An opaque pointer to data that can be used in the mempool
489 * constructor function.
491 * A function pointer that is called for each object at
492 * initialization of the pool. The user can set some meta data in
493 * objects if needed. This parameter can be NULL if not needed.
494 * The obj_init() function takes the mempool pointer, the init_arg,
495 * the object pointer and the object number as parameters.
496 * @param obj_init_arg
497 * An opaque pointer to data that can be used as an argument for
498 * each call to the object constructor function.
500 * The *socket_id* argument is the socket identifier in the case of
501 * NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
502 * constraint for the reserved zone.
504 * The *flags* arguments is an OR of following flags:
505 * - MEMPOOL_F_NO_SPREAD: By default, objects addresses are spread
506 * between channels in RAM: the pool allocator will add padding
507 * between objects depending on the hardware configuration. See
508 * Memory alignment constraints for details. If this flag is set,
509 * the allocator will just align them to a cache line.
510 * - MEMPOOL_F_NO_CACHE_ALIGN: By default, the returned objects are
511 * cache-aligned. This flag removes this constraint, and no
512 * padding will be present between objects. This flag implies
513 * MEMPOOL_F_NO_SPREAD.
514 * - MEMPOOL_F_SP_PUT: If this flag is set, the default behavior
515 * when using rte_mempool_put() or rte_mempool_put_bulk() is
516 * "single-producer". Otherwise, it is "multi-producers".
517 * - MEMPOOL_F_SC_GET: If this flag is set, the default behavior
518 * when using rte_mempool_get() or rte_mempool_get_bulk() is
519 * "single-consumer". Otherwise, it is "multi-consumers".
521 * The pointer to the new allocated mempool, on success. NULL on error
522 * with rte_errno set appropriately. Possible rte_errno values include:
523 * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
524 * - E_RTE_SECONDARY - function was called from a secondary process instance
525 * - EINVAL - cache size provided is too large
526 * - ENOSPC - the maximum number of memzones has already been allocated
527 * - EEXIST - a memzone with the same name already exists
528 * - ENOMEM - no appropriate memory area found in which to create memzone
531 rte_mempool_create(const char *name, unsigned n, unsigned elt_size,
532 unsigned cache_size, unsigned private_data_size,
533 rte_mempool_ctor_t *mp_init, void *mp_init_arg,
534 rte_mempool_obj_ctor_t *obj_init, void *obj_init_arg,
535 int socket_id, unsigned flags);
538 * Create a new mempool named *name* in memory.
540 * This function uses ``memzone_reserve()`` to allocate memory. The
541 * pool contains n elements of elt_size. Its size is set to n.
542 * Depending on the input parameters, mempool elements can be either allocated
543 * together with the mempool header, or an externally provided memory buffer
544 * could be used to store mempool objects. In later case, that external
545 * memory buffer can consist of set of disjoint physical pages.
548 * The name of the mempool.
550 * The number of elements in the mempool. The optimum size (in terms of
551 * memory usage) for a mempool is when n is a power of two minus one:
554 * The size of each element.
556 * If cache_size is non-zero, the rte_mempool library will try to
557 * limit the accesses to the common lockless pool, by maintaining a
558 * per-lcore object cache. This argument must be lower or equal to
559 * CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE. It is advised to choose
560 * cache_size to have "n modulo cache_size == 0": if this is
561 * not the case, some elements will always stay in the pool and will
562 * never be used. The access to the per-lcore table is of course
563 * faster than the multi-producer/consumer pool. The cache can be
564 * disabled if the cache_size argument is set to 0; it can be useful to
565 * avoid losing objects in cache. Note that even if not used, the
566 * memory space for cache is always reserved in a mempool structure,
567 * except if CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE is set to 0.
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 pointer that is called for each object at
582 * initialization of the pool. The user can set some meta data in
583 * objects if needed. This parameter can be NULL if not needed.
584 * The obj_init() function takes the mempool pointer, the init_arg,
585 * the object pointer and the object number as parameters.
586 * @param obj_init_arg
587 * An opaque pointer to data that can be used as an argument for
588 * each call to the object constructor function.
590 * The *socket_id* argument is the socket identifier in the case of
591 * NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
592 * constraint for the reserved zone.
594 * The *flags* arguments is an OR of following flags:
595 * - MEMPOOL_F_NO_SPREAD: By default, objects addresses are spread
596 * between channels in RAM: the pool allocator will add padding
597 * between objects depending on the hardware configuration. See
598 * Memory alignment constraints for details. If this flag is set,
599 * the allocator will just align them to a cache line.
600 * - MEMPOOL_F_NO_CACHE_ALIGN: By default, the returned objects are
601 * cache-aligned. This flag removes this constraint, and no
602 * padding will be present between objects. This flag implies
603 * MEMPOOL_F_NO_SPREAD.
604 * - MEMPOOL_F_SP_PUT: If this flag is set, the default behavior
605 * when using rte_mempool_put() or rte_mempool_put_bulk() is
606 * "single-producer". Otherwise, it is "multi-producers".
607 * - MEMPOOL_F_SC_GET: If this flag is set, the default behavior
608 * when using rte_mempool_get() or rte_mempool_get_bulk() is
609 * "single-consumer". Otherwise, it is "multi-consumers".
611 * Virtual address of the externally allocated memory buffer.
612 * Will be used to store mempool objects.
614 * Array of physical addresses of the pages that comprises given memory
617 * Number of elements in the paddr array.
619 * LOG2 of the physical pages size.
621 * The pointer to the new allocated mempool, on success. NULL on error
622 * with rte_errno set appropriately. Possible rte_errno values include:
623 * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
624 * - E_RTE_SECONDARY - function was called from a secondary process instance
625 * - EINVAL - cache size provided is too large
626 * - ENOSPC - the maximum number of memzones has already been allocated
627 * - EEXIST - a memzone with the same name already exists
628 * - ENOMEM - no appropriate memory area found in which to create memzone
631 rte_mempool_xmem_create(const char *name, unsigned n, unsigned elt_size,
632 unsigned cache_size, unsigned private_data_size,
633 rte_mempool_ctor_t *mp_init, void *mp_init_arg,
634 rte_mempool_obj_ctor_t *obj_init, void *obj_init_arg,
635 int socket_id, unsigned flags, void *vaddr,
636 const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift);
638 #ifdef RTE_LIBRTE_XEN_DOM0
640 * Create a new mempool named *name* in memory on Xen Dom0.
642 * This function uses ``rte_mempool_xmem_create()`` to allocate memory. The
643 * pool contains n elements of elt_size. Its size is set to n.
644 * All elements of the mempool are allocated together with the mempool header,
645 * and memory buffer can consist of set of disjoint physical pages.
648 * The name of the mempool.
650 * The number of elements in the mempool. The optimum size (in terms of
651 * memory usage) for a mempool is when n is a power of two minus one:
654 * The size of each element.
656 * If cache_size is non-zero, the rte_mempool library will try to
657 * limit the accesses to the common lockless pool, by maintaining a
658 * per-lcore object cache. This argument must be lower or equal to
659 * CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE. It is advised to choose
660 * cache_size to have "n modulo cache_size == 0": if this is
661 * not the case, some elements will always stay in the pool and will
662 * never be used. The access to the per-lcore table is of course
663 * faster than the multi-producer/consumer pool. The cache can be
664 * disabled if the cache_size argument is set to 0; it can be useful to
665 * avoid losing objects in cache. Note that even if not used, the
666 * memory space for cache is always reserved in a mempool structure,
667 * except if CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE is set to 0.
668 * @param private_data_size
669 * The size of the private data appended after the mempool
670 * structure. This is useful for storing some private data after the
671 * mempool structure, as is done for rte_mbuf_pool for example.
673 * A function pointer that is called for initialization of the pool,
674 * before object initialization. The user can initialize the private
675 * data in this function if needed. This parameter can be NULL if
678 * An opaque pointer to data that can be used in the mempool
679 * constructor function.
681 * A function pointer that is called for each object at
682 * initialization of the pool. The user can set some meta data in
683 * objects if needed. This parameter can be NULL if not needed.
684 * The obj_init() function takes the mempool pointer, the init_arg,
685 * the object pointer and the object number as parameters.
686 * @param obj_init_arg
687 * An opaque pointer to data that can be used as an argument for
688 * each call to the object constructor function.
690 * The *socket_id* argument is the socket identifier in the case of
691 * NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
692 * constraint for the reserved zone.
694 * The *flags* arguments is an OR of following flags:
695 * - MEMPOOL_F_NO_SPREAD: By default, objects addresses are spread
696 * between channels in RAM: the pool allocator will add padding
697 * between objects depending on the hardware configuration. See
698 * Memory alignment constraints for details. If this flag is set,
699 * the allocator will just align them to a cache line.
700 * - MEMPOOL_F_NO_CACHE_ALIGN: By default, the returned objects are
701 * cache-aligned. This flag removes this constraint, and no
702 * padding will be present between objects. This flag implies
703 * MEMPOOL_F_NO_SPREAD.
704 * - MEMPOOL_F_SP_PUT: If this flag is set, the default behavior
705 * when using rte_mempool_put() or rte_mempool_put_bulk() is
706 * "single-producer". Otherwise, it is "multi-producers".
707 * - MEMPOOL_F_SC_GET: If this flag is set, the default behavior
708 * when using rte_mempool_get() or rte_mempool_get_bulk() is
709 * "single-consumer". Otherwise, it is "multi-consumers".
711 * The pointer to the new allocated mempool, on success. NULL on error
712 * with rte_errno set appropriately. Possible rte_errno values include:
713 * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
714 * - E_RTE_SECONDARY - function was called from a secondary process instance
715 * - EINVAL - cache size provided is too large
716 * - ENOSPC - the maximum number of memzones has already been allocated
717 * - EEXIST - a memzone with the same name already exists
718 * - ENOMEM - no appropriate memory area found in which to create memzone
721 rte_dom0_mempool_create(const char *name, unsigned n, unsigned elt_size,
722 unsigned cache_size, unsigned private_data_size,
723 rte_mempool_ctor_t *mp_init, void *mp_init_arg,
724 rte_mempool_obj_ctor_t *obj_init, void *obj_init_arg,
725 int socket_id, unsigned flags);
729 * Dump the status of the mempool to the console.
732 * A pointer to a file for output
734 * A pointer to the mempool structure.
736 void rte_mempool_dump(FILE *f, const struct rte_mempool *mp);
739 * @internal Put several objects back in the mempool; used internally.
741 * A pointer to the mempool structure.
743 * A pointer to a table of void * pointers (objects).
745 * The number of objects to store back in the mempool, must be strictly
748 * Mono-producer (0) or multi-producers (1).
750 static inline void __attribute__((always_inline))
751 __mempool_put_bulk(struct rte_mempool *mp, void * const *obj_table,
752 unsigned n, int is_mp)
754 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
755 struct rte_mempool_cache *cache;
758 unsigned lcore_id = rte_lcore_id();
759 uint32_t cache_size = mp->cache_size;
760 uint32_t flushthresh = mp->cache_flushthresh;
761 #endif /* RTE_MEMPOOL_CACHE_MAX_SIZE > 0 */
763 /* increment stat now, adding in mempool always success */
764 __MEMPOOL_STAT_ADD(mp, put, n);
766 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
767 /* cache is not enabled or single producer or non-EAL thread */
768 if (unlikely(cache_size == 0 || is_mp == 0 ||
769 lcore_id >= RTE_MAX_LCORE))
772 /* Go straight to ring if put would overflow mem allocated for cache */
773 if (unlikely(n > RTE_MEMPOOL_CACHE_MAX_SIZE))
776 cache = &mp->local_cache[lcore_id];
777 cache_objs = &cache->objs[cache->len];
780 * The cache follows the following algorithm
781 * 1. Add the objects to the cache
782 * 2. Anything greater than the cache min value (if it crosses the
783 * cache flush threshold) is flushed to the ring.
786 /* Add elements back into the cache */
787 for (index = 0; index < n; ++index, obj_table++)
788 cache_objs[index] = *obj_table;
792 if (cache->len >= flushthresh) {
793 rte_ring_mp_enqueue_bulk(mp->ring, &cache->objs[cache_size],
794 cache->len - cache_size);
795 cache->len = cache_size;
801 #endif /* RTE_MEMPOOL_CACHE_MAX_SIZE > 0 */
803 /* push remaining objects in ring */
804 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
806 if (rte_ring_mp_enqueue_bulk(mp->ring, obj_table, n) < 0)
807 rte_panic("cannot put objects in mempool\n");
810 if (rte_ring_sp_enqueue_bulk(mp->ring, obj_table, n) < 0)
811 rte_panic("cannot put objects in mempool\n");
815 rte_ring_mp_enqueue_bulk(mp->ring, obj_table, n);
817 rte_ring_sp_enqueue_bulk(mp->ring, obj_table, n);
823 * Put several objects back in the mempool (multi-producers safe).
826 * A pointer to the mempool structure.
828 * A pointer to a table of void * pointers (objects).
830 * The number of objects to add in the mempool from the obj_table.
832 static inline void __attribute__((always_inline))
833 rte_mempool_mp_put_bulk(struct rte_mempool *mp, void * const *obj_table,
836 __mempool_check_cookies(mp, obj_table, n, 0);
837 __mempool_put_bulk(mp, obj_table, n, 1);
841 * Put several objects back in the mempool (NOT multi-producers safe).
844 * A pointer to the mempool structure.
846 * A pointer to a table of void * pointers (objects).
848 * The number of objects to add in the mempool from obj_table.
851 rte_mempool_sp_put_bulk(struct rte_mempool *mp, void * const *obj_table,
854 __mempool_check_cookies(mp, obj_table, n, 0);
855 __mempool_put_bulk(mp, obj_table, n, 0);
859 * Put several objects back in the mempool.
861 * This function calls the multi-producer or the single-producer
862 * version depending on the default behavior that was specified at
863 * mempool creation time (see flags).
866 * A pointer to the mempool structure.
868 * A pointer to a table of void * pointers (objects).
870 * The number of objects to add in the mempool from obj_table.
872 static inline void __attribute__((always_inline))
873 rte_mempool_put_bulk(struct rte_mempool *mp, void * const *obj_table,
876 __mempool_check_cookies(mp, obj_table, n, 0);
877 __mempool_put_bulk(mp, obj_table, n, !(mp->flags & MEMPOOL_F_SP_PUT));
881 * Put one object in the mempool (multi-producers safe).
884 * A pointer to the mempool structure.
886 * A pointer to the object to be added.
888 static inline void __attribute__((always_inline))
889 rte_mempool_mp_put(struct rte_mempool *mp, void *obj)
891 rte_mempool_mp_put_bulk(mp, &obj, 1);
895 * Put one object back in the mempool (NOT multi-producers safe).
898 * A pointer to the mempool structure.
900 * A pointer to the object to be added.
902 static inline void __attribute__((always_inline))
903 rte_mempool_sp_put(struct rte_mempool *mp, void *obj)
905 rte_mempool_sp_put_bulk(mp, &obj, 1);
909 * Put one object back in the mempool.
911 * This function calls the multi-producer or the single-producer
912 * version depending on the default behavior that was specified at
913 * mempool creation time (see flags).
916 * A pointer to the mempool structure.
918 * A pointer to the object to be added.
920 static inline void __attribute__((always_inline))
921 rte_mempool_put(struct rte_mempool *mp, void *obj)
923 rte_mempool_put_bulk(mp, &obj, 1);
927 * @internal Get several objects from the mempool; used internally.
929 * A pointer to the mempool structure.
931 * A pointer to a table of void * pointers (objects).
933 * The number of objects to get, must be strictly positive.
935 * Mono-consumer (0) or multi-consumers (1).
937 * - >=0: Success; number of objects supplied.
938 * - <0: Error; code of ring dequeue function.
940 static inline int __attribute__((always_inline))
941 __mempool_get_bulk(struct rte_mempool *mp, void **obj_table,
942 unsigned n, int is_mc)
945 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
946 struct rte_mempool_cache *cache;
949 unsigned lcore_id = rte_lcore_id();
950 uint32_t cache_size = mp->cache_size;
952 /* cache is not enabled or single consumer */
953 if (unlikely(cache_size == 0 || is_mc == 0 ||
954 n >= cache_size || lcore_id >= RTE_MAX_LCORE))
957 cache = &mp->local_cache[lcore_id];
958 cache_objs = cache->objs;
960 /* Can this be satisfied from the cache? */
961 if (cache->len < n) {
962 /* No. Backfill the cache first, and then fill from it */
963 uint32_t req = n + (cache_size - cache->len);
965 /* How many do we require i.e. number to fill the cache + the request */
966 ret = rte_ring_mc_dequeue_bulk(mp->ring, &cache->objs[cache->len], req);
967 if (unlikely(ret < 0)) {
969 * In the offchance that we are buffer constrained,
970 * where we are not able to allocate cache + n, go to
971 * the ring directly. If that fails, we are truly out of
980 /* Now fill in the response ... */
981 for (index = 0, len = cache->len - 1; index < n; ++index, len--, obj_table++)
982 *obj_table = cache_objs[len];
986 __MEMPOOL_STAT_ADD(mp, get_success, n);
991 #endif /* RTE_MEMPOOL_CACHE_MAX_SIZE > 0 */
993 /* get remaining objects from ring */
995 ret = rte_ring_mc_dequeue_bulk(mp->ring, obj_table, n);
997 ret = rte_ring_sc_dequeue_bulk(mp->ring, obj_table, n);
1000 __MEMPOOL_STAT_ADD(mp, get_fail, n);
1002 __MEMPOOL_STAT_ADD(mp, get_success, n);
1008 * Get several objects from the mempool (multi-consumers safe).
1010 * If cache is enabled, objects will be retrieved first from cache,
1011 * subsequently from the common pool. Note that it can return -ENOENT when
1012 * the local cache and common pool are empty, even if cache from other
1016 * A pointer to the mempool structure.
1018 * A pointer to a table of void * pointers (objects) that will be filled.
1020 * The number of objects to get from mempool to obj_table.
1022 * - 0: Success; objects taken.
1023 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
1025 static inline int __attribute__((always_inline))
1026 rte_mempool_mc_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned n)
1029 ret = __mempool_get_bulk(mp, obj_table, n, 1);
1031 __mempool_check_cookies(mp, obj_table, n, 1);
1036 * Get several objects from the mempool (NOT multi-consumers safe).
1038 * If cache is enabled, objects will be retrieved first from cache,
1039 * subsequently from the common pool. Note that it can return -ENOENT when
1040 * the local cache and common pool are empty, even if cache from other
1044 * A pointer to the mempool structure.
1046 * A pointer to a table of void * pointers (objects) that will be filled.
1048 * The number of objects to get from the mempool to obj_table.
1050 * - 0: Success; objects taken.
1051 * - -ENOENT: Not enough entries in the mempool; no object is
1054 static inline int __attribute__((always_inline))
1055 rte_mempool_sc_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned n)
1058 ret = __mempool_get_bulk(mp, obj_table, n, 0);
1060 __mempool_check_cookies(mp, obj_table, n, 1);
1065 * Get several objects from the mempool.
1067 * This function calls the multi-consumers or the single-consumer
1068 * version, depending on the default behaviour that was specified at
1069 * mempool creation time (see flags).
1071 * If cache is enabled, objects will be retrieved first from cache,
1072 * subsequently from the common pool. Note that it can return -ENOENT when
1073 * the local cache and common pool are empty, even if cache from other
1077 * A pointer to the mempool structure.
1079 * A pointer to a table of void * pointers (objects) that will be filled.
1081 * The number of objects to get from the mempool to obj_table.
1083 * - 0: Success; objects taken
1084 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
1086 static inline int __attribute__((always_inline))
1087 rte_mempool_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned n)
1090 ret = __mempool_get_bulk(mp, obj_table, n,
1091 !(mp->flags & MEMPOOL_F_SC_GET));
1093 __mempool_check_cookies(mp, obj_table, n, 1);
1098 * Get one object from the mempool (multi-consumers safe).
1100 * If cache is enabled, objects will be retrieved first from cache,
1101 * subsequently from the common pool. Note that it can return -ENOENT when
1102 * the local cache and common pool are empty, even if cache from other
1106 * A pointer to the mempool structure.
1108 * A pointer to a void * pointer (object) that will be filled.
1110 * - 0: Success; objects taken.
1111 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
1113 static inline int __attribute__((always_inline))
1114 rte_mempool_mc_get(struct rte_mempool *mp, void **obj_p)
1116 return rte_mempool_mc_get_bulk(mp, obj_p, 1);
1120 * Get one object from the mempool (NOT multi-consumers safe).
1122 * If cache is enabled, objects will be retrieved first from cache,
1123 * subsequently from the common pool. Note that it can return -ENOENT when
1124 * the local cache and common pool are empty, even if cache from other
1128 * A pointer to the mempool structure.
1130 * A pointer to a void * pointer (object) that will be filled.
1132 * - 0: Success; objects taken.
1133 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
1135 static inline int __attribute__((always_inline))
1136 rte_mempool_sc_get(struct rte_mempool *mp, void **obj_p)
1138 return rte_mempool_sc_get_bulk(mp, obj_p, 1);
1142 * Get one object from the mempool.
1144 * This function calls the multi-consumers or the single-consumer
1145 * version, depending on the default behavior that was specified at
1146 * mempool creation (see flags).
1148 * If cache is enabled, objects will be retrieved first from cache,
1149 * subsequently from the common pool. Note that it can return -ENOENT when
1150 * the local cache and common pool are empty, even if cache from other
1154 * A pointer to the mempool structure.
1156 * A pointer to a void * pointer (object) that will be filled.
1158 * - 0: Success; objects taken.
1159 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
1161 static inline int __attribute__((always_inline))
1162 rte_mempool_get(struct rte_mempool *mp, void **obj_p)
1164 return rte_mempool_get_bulk(mp, obj_p, 1);
1168 * Return the number of entries in the mempool.
1170 * When cache is enabled, this function has to browse the length of
1171 * all lcores, so it should not be used in a data path, but only for
1175 * A pointer to the mempool structure.
1177 * The number of entries in the mempool.
1179 unsigned rte_mempool_count(const struct rte_mempool *mp);
1182 * Return the number of free entries in the mempool ring.
1183 * i.e. how many entries can be freed back to the mempool.
1185 * NOTE: This corresponds to the number of elements *allocated* from the
1186 * memory pool, not the number of elements in the pool itself. To count
1187 * the number elements currently available in the pool, use "rte_mempool_count"
1189 * When cache is enabled, this function has to browse the length of
1190 * all lcores, so it should not be used in a data path, but only for
1194 * A pointer to the mempool structure.
1196 * The number of free entries in the mempool.
1198 static inline unsigned
1199 rte_mempool_free_count(const struct rte_mempool *mp)
1201 return mp->size - rte_mempool_count(mp);
1205 * Test if the mempool is full.
1207 * When cache is enabled, this function has to browse the length of all
1208 * lcores, so it should not be used in a data path, but only for debug
1212 * A pointer to the mempool structure.
1214 * - 1: The mempool is full.
1215 * - 0: The mempool is not full.
1218 rte_mempool_full(const struct rte_mempool *mp)
1220 return !!(rte_mempool_count(mp) == mp->size);
1224 * Test if the mempool is empty.
1226 * When cache is enabled, this function has to browse the length of all
1227 * lcores, so it should not be used in a data path, but only for debug
1231 * A pointer to the mempool structure.
1233 * - 1: The mempool is empty.
1234 * - 0: The mempool is not empty.
1237 rte_mempool_empty(const struct rte_mempool *mp)
1239 return !!(rte_mempool_count(mp) == 0);
1243 * Return the physical address of elt, which is an element of the pool mp.
1246 * A pointer to the mempool structure.
1248 * A pointer (virtual address) to the element of the pool.
1250 * The physical address of the elt element.
1252 static inline phys_addr_t
1253 rte_mempool_virt2phy(const struct rte_mempool *mp, const void *elt)
1255 if (rte_eal_has_hugepages()) {
1258 off = (const char *)elt - (const char *)mp->elt_va_start;
1259 return (mp->elt_pa[off >> mp->pg_shift] + (off & mp->pg_mask));
1262 * If huge pages are disabled, we cannot assume the
1263 * memory region to be physically contiguous.
1264 * Lookup for each element.
1266 return rte_mem_virt2phy(elt);
1271 * Check the consistency of mempool objects.
1273 * Verify the coherency of fields in the mempool structure. Also check
1274 * that the cookies of mempool objects (even the ones that are not
1275 * present in pool) have a correct value. If not, a panic will occur.
1278 * A pointer to the mempool structure.
1280 void rte_mempool_audit(const struct rte_mempool *mp);
1283 * Return a pointer to the private data in an mempool structure.
1286 * A pointer to the mempool structure.
1288 * A pointer to the private data.
1290 static inline void *rte_mempool_get_priv(struct rte_mempool *mp)
1292 return (char *)mp + MEMPOOL_HEADER_SIZE(mp, mp->pg_num);
1296 * Dump the status of all mempools on the console
1299 * A pointer to a file for output
1301 void rte_mempool_list_dump(FILE *f);
1304 * Search a mempool from its name
1307 * The name of the mempool.
1309 * The pointer to the mempool matching the name, or NULL if not found.
1311 * with rte_errno set appropriately. Possible rte_errno values include:
1312 * - ENOENT - required entry not available to return.
1315 struct rte_mempool *rte_mempool_lookup(const char *name);
1318 * Get the header, trailer and total size of a mempool element.
1320 * Given a desired size of the mempool element and mempool flags,
1321 * calculates header, trailer, body and total sizes of the mempool object.
1324 * The size of each element.
1326 * The flags used for the mempool creation.
1327 * Consult rte_mempool_create() for more information about possible values.
1328 * The size of each element.
1330 * The calculated detailed size the mempool object. May be NULL.
1332 * Total size of the mempool object.
1334 uint32_t rte_mempool_calc_obj_size(uint32_t elt_size, uint32_t flags,
1335 struct rte_mempool_objsz *sz);
1338 * Get the size of memory required to store mempool elements.
1340 * Calculate the maximum amount of memory required to store given number
1341 * of objects. Assume that the memory buffer will be aligned at page
1344 * Note that if object size is bigger then page size, then it assumes
1345 * that pages are grouped in subsets of physically continuous pages big
1346 * enough to store at least one object.
1349 * Number of elements.
1351 * The size of each element.
1353 * LOG2 of the physical pages size.
1355 * Required memory size aligned at page boundary.
1357 size_t rte_mempool_xmem_size(uint32_t elt_num, size_t elt_sz,
1361 * Get the size of memory required to store mempool elements.
1363 * Calculate how much memory would be actually required with the given
1364 * memory footprint to store required number of objects.
1367 * Virtual address of the externally allocated memory buffer.
1368 * Will be used to store mempool objects.
1370 * Number of elements.
1372 * The size of each element.
1374 * Array of physical addresses of the pages that comprises given memory
1377 * Number of elements in the paddr array.
1379 * LOG2 of the physical pages size.
1381 * On success, the number of bytes needed to store given number of
1382 * objects, aligned to the given page size. If the provided memory
1383 * buffer is too small, return a negative value whose absolute value
1384 * is the actual number of elements that can be stored in that buffer.
1386 ssize_t rte_mempool_xmem_usage(void *vaddr, uint32_t elt_num, size_t elt_sz,
1387 const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift);
1390 * Walk list of all memory pools
1395 * Argument passed to iterator
1397 void rte_mempool_walk(void (*func)(const struct rte_mempool *, void *arg),
1404 #endif /* _RTE_MEMPOOL_H_ */