4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #ifndef _RTE_MEMPOOL_H_
35 #define _RTE_MEMPOOL_H_
41 * A memory pool is an allocator of fixed-size object. It is
42 * identified by its name, and uses a ring to store free objects. It
43 * provides some other optional services, like a per-core object
44 * cache, and an alignment helper to ensure that objects are padded
45 * to spread them equally on all RAM channels, ranks, and so on.
47 * Objects owned by a mempool should never be added in another
48 * mempool. When an object is freed using rte_mempool_put() or
49 * equivalent, the object data is not modified; the user can save some
50 * meta-data in the object data and retrieve them when allocating a
53 * Note: the mempool implementation is not preemptable. A lcore must
54 * not be interrupted by another task that uses the same mempool
55 * (because it uses a ring which is not preemptable). Also, mempool
56 * functions must not be used outside the DPDK environment: for
57 * example, in linuxapp environment, a thread that is not created by
58 * the EAL must not use mempools. This is due to the per-lcore cache
59 * that won't work as rte_lcore_id() will not return a correct value.
67 #include <sys/queue.h>
70 #include <rte_debug.h>
71 #include <rte_lcore.h>
72 #include <rte_memory.h>
73 #include <rte_branch_prediction.h>
80 #define RTE_MEMPOOL_HEADER_COOKIE1 0xbadbadbadadd2e55ULL /**< Header cookie. */
81 #define RTE_MEMPOOL_HEADER_COOKIE2 0xf2eef2eedadd2e55ULL /**< Header cookie. */
82 #define RTE_MEMPOOL_TRAILER_COOKIE 0xadd2e55badbadbadULL /**< Trailer cookie.*/
84 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
86 * A structure that stores the mempool statistics (per-lcore).
88 struct rte_mempool_debug_stats {
89 uint64_t put_bulk; /**< Number of puts. */
90 uint64_t put_objs; /**< Number of objects successfully put. */
91 uint64_t get_success_bulk; /**< Successful allocation number. */
92 uint64_t get_success_objs; /**< Objects successfully allocated. */
93 uint64_t get_fail_bulk; /**< Failed allocation number. */
94 uint64_t get_fail_objs; /**< Objects that failed to be allocated. */
95 } __rte_cache_aligned;
99 * A structure that stores a per-core object cache.
101 struct rte_mempool_cache {
102 unsigned len; /**< Cache len */
104 * Cache is allocated to this size to allow it to overflow in certain
105 * cases to avoid needless emptying of cache.
107 void *objs[RTE_MEMPOOL_CACHE_MAX_SIZE * 3]; /**< Cache objects */
108 } __rte_cache_aligned;
111 * A structure that stores the size of mempool elements.
113 struct rte_mempool_objsz {
114 uint32_t elt_size; /**< Size of an element. */
115 uint32_t header_size; /**< Size of header (before elt). */
116 uint32_t trailer_size; /**< Size of trailer (after elt). */
118 /**< Total size of an object (header + elt + trailer). */
121 #define RTE_MEMPOOL_NAMESIZE 32 /**< Maximum length of a memory pool. */
122 #define RTE_MEMPOOL_MZ_PREFIX "MP_"
125 #define RTE_MEMPOOL_MZ_FORMAT RTE_MEMPOOL_MZ_PREFIX "%s"
127 #ifdef RTE_LIBRTE_XEN_DOM0
129 /* "<name>_MP_elt" */
130 #define RTE_MEMPOOL_OBJ_NAME "%s_" RTE_MEMPOOL_MZ_PREFIX "elt"
134 #define RTE_MEMPOOL_OBJ_NAME RTE_MEMPOOL_MZ_FORMAT
136 #endif /* RTE_LIBRTE_XEN_DOM0 */
138 #define MEMPOOL_PG_SHIFT_MAX (sizeof(uintptr_t) * CHAR_BIT - 1)
140 /** Mempool over one chunk of physically continuous memory */
141 #define MEMPOOL_PG_NUM_DEFAULT 1
143 #ifndef RTE_MEMPOOL_ALIGN
144 #define RTE_MEMPOOL_ALIGN RTE_CACHE_LINE_SIZE
147 #define RTE_MEMPOOL_ALIGN_MASK (RTE_MEMPOOL_ALIGN - 1)
150 * Mempool object header structure
152 * Each object stored in mempools are prefixed by this header structure,
153 * it allows to retrieve the mempool pointer from the object and to
154 * iterate on all objects attached to a mempool. When debug is enabled,
155 * a cookie is also added in this structure preventing corruptions and
158 struct rte_mempool_objhdr {
159 STAILQ_ENTRY(rte_mempool_objhdr) next; /**< Next in list. */
160 struct rte_mempool *mp; /**< The mempool owning the object. */
161 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
162 uint64_t cookie; /**< Debug cookie. */
167 * A list of object headers type
169 STAILQ_HEAD(rte_mempool_objhdr_list, rte_mempool_objhdr);
172 * Mempool object trailer structure
174 * In debug mode, each object stored in mempools are suffixed by this
175 * trailer structure containing a cookie preventing memory corruptions.
177 struct rte_mempool_objtlr {
178 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
179 uint64_t cookie; /**< Debug cookie. */
184 * The RTE mempool structure.
187 char name[RTE_MEMPOOL_NAMESIZE]; /**< Name of mempool. */
188 struct rte_ring *ring; /**< Ring to store objects. */
189 phys_addr_t phys_addr; /**< Phys. addr. of mempool struct. */
190 int flags; /**< Flags of the mempool. */
191 uint32_t size; /**< Size of the mempool. */
192 uint32_t cache_size; /**< Size of per-lcore local cache. */
193 uint32_t cache_flushthresh;
194 /**< Threshold before we flush excess elements. */
196 uint32_t elt_size; /**< Size of an element. */
197 uint32_t header_size; /**< Size of header (before elt). */
198 uint32_t trailer_size; /**< Size of trailer (after elt). */
200 unsigned private_data_size; /**< Size of private data. */
202 struct rte_mempool_cache *local_cache; /**< Per-lcore local cache */
204 struct rte_mempool_objhdr_list elt_list; /**< List of objects in pool */
206 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
207 /** Per-lcore statistics. */
208 struct rte_mempool_debug_stats stats[RTE_MAX_LCORE];
211 /* Address translation support, starts from next cache line. */
213 /** Number of elements in the elt_pa array. */
214 uint32_t pg_num __rte_cache_aligned;
215 uint32_t pg_shift; /**< LOG2 of the physical pages. */
216 uintptr_t pg_mask; /**< physical page mask value. */
217 uintptr_t elt_va_start;
218 /**< Virtual address of the first mempool object. */
219 uintptr_t elt_va_end;
220 /**< Virtual address of the <size + 1> mempool object. */
221 phys_addr_t elt_pa[MEMPOOL_PG_NUM_DEFAULT];
222 /**< Array of physical page addresses for the mempool objects buffer. */
224 } __rte_cache_aligned;
226 #define MEMPOOL_F_NO_SPREAD 0x0001 /**< Do not spread among memory channels. */
227 #define MEMPOOL_F_NO_CACHE_ALIGN 0x0002 /**< Do not align objs on cache lines.*/
228 #define MEMPOOL_F_SP_PUT 0x0004 /**< Default put is "single-producer".*/
229 #define MEMPOOL_F_SC_GET 0x0008 /**< Default get is "single-consumer".*/
232 * @internal When debug is enabled, store some statistics.
235 * Pointer to the memory pool.
237 * Name of the statistics field to increment in the memory pool.
239 * Number to add to the object-oriented statistics.
241 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
242 #define __MEMPOOL_STAT_ADD(mp, name, n) do { \
243 unsigned __lcore_id = rte_lcore_id(); \
244 if (__lcore_id < RTE_MAX_LCORE) { \
245 mp->stats[__lcore_id].name##_objs += n; \
246 mp->stats[__lcore_id].name##_bulk += 1; \
250 #define __MEMPOOL_STAT_ADD(mp, name, n) do {} while(0)
254 * Size of elt_pa array size based on number of pages. (Internal use)
256 #define __PA_SIZE(mp, pgn) \
257 RTE_ALIGN_CEIL((((pgn) - RTE_DIM((mp)->elt_pa)) * \
258 sizeof((mp)->elt_pa[0])), RTE_CACHE_LINE_SIZE)
261 * Calculate the size of the mempool header.
264 * Pointer to the memory pool.
266 * Number of pages used to store mempool objects.
268 * Size of the per-lcore cache.
270 #define MEMPOOL_HEADER_SIZE(mp, pgn, cs) \
271 (sizeof(*(mp)) + __PA_SIZE(mp, pgn) + (((cs) == 0) ? 0 : \
272 (sizeof(struct rte_mempool_cache) * RTE_MAX_LCORE)))
275 * Return true if the whole mempool is in contiguous memory.
277 #define MEMPOOL_IS_CONTIG(mp) \
278 ((mp)->pg_num == MEMPOOL_PG_NUM_DEFAULT && \
279 (mp)->phys_addr == (mp)->elt_pa[0])
281 /* return the header of a mempool object (internal) */
282 static inline struct rte_mempool_objhdr *__mempool_get_header(void *obj)
284 return (struct rte_mempool_objhdr *)RTE_PTR_SUB(obj,
285 sizeof(struct rte_mempool_objhdr));
289 * Return a pointer to the mempool owning this object.
292 * An object that is owned by a pool. If this is not the case,
293 * the behavior is undefined.
295 * A pointer to the mempool structure.
297 static inline struct rte_mempool *rte_mempool_from_obj(void *obj)
299 struct rte_mempool_objhdr *hdr = __mempool_get_header(obj);
303 /* return the trailer of a mempool object (internal) */
304 static inline struct rte_mempool_objtlr *__mempool_get_trailer(void *obj)
306 struct rte_mempool *mp = rte_mempool_from_obj(obj);
307 return (struct rte_mempool_objtlr *)RTE_PTR_ADD(obj, mp->elt_size);
311 * @internal Check and update cookies or panic.
314 * Pointer to the memory pool.
315 * @param obj_table_const
316 * Pointer to a table of void * pointers (objects).
318 * Index of object in object table.
320 * - 0: object is supposed to be allocated, mark it as free
321 * - 1: object is supposed to be free, mark it as allocated
322 * - 2: just check that cookie is valid (free or allocated)
324 void rte_mempool_check_cookies(const struct rte_mempool *mp,
325 void * const *obj_table_const, unsigned n, int free);
327 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
328 #define __mempool_check_cookies(mp, obj_table_const, n, free) \
329 rte_mempool_check_cookies(mp, obj_table_const, n, free)
331 #define __mempool_check_cookies(mp, obj_table_const, n, free) do {} while(0)
332 #endif /* RTE_LIBRTE_MEMPOOL_DEBUG */
335 * An object callback function for mempool.
337 * Arguments are the mempool, the opaque pointer given by the user in
338 * rte_mempool_create(), the pointer to the element and the index of
339 * the element in the pool.
341 typedef void (rte_mempool_obj_cb_t)(struct rte_mempool *mp,
342 void *opaque, void *obj, unsigned obj_idx);
343 typedef rte_mempool_obj_cb_t rte_mempool_obj_ctor_t; /* compat */
346 * A mempool object iterator callback function.
348 typedef void (*rte_mempool_obj_iter_t)(void * /*obj_iter_arg*/,
349 void * /*obj_start*/,
351 uint32_t /*obj_index */);
354 * Call a function for each mempool object in a memory chunk
356 * Iterate across objects of the given size and alignment in the
357 * provided chunk of memory. The given memory buffer can consist of
358 * disjointed physical pages.
360 * For each object, call the provided callback (if any). This function
361 * is used to populate a mempool, or walk through all the elements of a
362 * mempool, or estimate how many elements of the given size could be
363 * created in the given memory buffer.
366 * Virtual address of the memory buffer.
368 * Maximum number of objects to iterate through.
370 * Size of each object.
372 * Alignment of each object.
374 * Array of physical addresses of the pages that comprises given memory
377 * Number of elements in the paddr array.
379 * LOG2 of the physical pages size.
381 * Object iterator callback function (could be NULL).
382 * @param obj_iter_arg
383 * User defined parameter for the object iterator callback function.
386 * Number of objects iterated through.
388 uint32_t rte_mempool_obj_iter(void *vaddr,
389 uint32_t elt_num, size_t elt_sz, size_t align,
390 const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift,
391 rte_mempool_obj_iter_t obj_iter, void *obj_iter_arg);
394 * A mempool constructor callback function.
396 * Arguments are the mempool and the opaque pointer given by the user in
397 * rte_mempool_create().
399 typedef void (rte_mempool_ctor_t)(struct rte_mempool *, void *);
402 * Create a new mempool named *name* in memory.
404 * This function uses ``memzone_reserve()`` to allocate memory. The
405 * pool contains n elements of elt_size. Its size is set to n.
406 * All elements of the mempool are allocated together with the mempool header,
407 * in one physically continuous chunk of memory.
410 * The name of the mempool.
412 * The number of elements in the mempool. The optimum size (in terms of
413 * memory usage) for a mempool is when n is a power of two minus one:
416 * The size of each element.
418 * If cache_size is non-zero, the rte_mempool library will try to
419 * limit the accesses to the common lockless pool, by maintaining a
420 * per-lcore object cache. This argument must be lower or equal to
421 * CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE and n / 1.5. It is advised to choose
422 * cache_size to have "n modulo cache_size == 0": if this is
423 * not the case, some elements will always stay in the pool and will
424 * never be used. The access to the per-lcore table is of course
425 * faster than the multi-producer/consumer pool. The cache can be
426 * disabled if the cache_size argument is set to 0; it can be useful to
427 * avoid losing objects in cache. Note that even if not used, the
428 * memory space for cache is always reserved in a mempool structure,
429 * except if CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE is set to 0.
430 * @param private_data_size
431 * The size of the private data appended after the mempool
432 * structure. This is useful for storing some private data after the
433 * mempool structure, as is done for rte_mbuf_pool for example.
435 * A function pointer that is called for initialization of the pool,
436 * before object initialization. The user can initialize the private
437 * data in this function if needed. This parameter can be NULL if
440 * An opaque pointer to data that can be used in the mempool
441 * constructor function.
443 * A function pointer that is called for each object at
444 * initialization of the pool. The user can set some meta data in
445 * objects if needed. This parameter can be NULL if not needed.
446 * The obj_init() function takes the mempool pointer, the init_arg,
447 * the object pointer and the object number as parameters.
448 * @param obj_init_arg
449 * An opaque pointer to data that can be used as an argument for
450 * each call to the object constructor function.
452 * The *socket_id* argument is the socket identifier in the case of
453 * NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
454 * constraint for the reserved zone.
456 * The *flags* arguments is an OR of following flags:
457 * - MEMPOOL_F_NO_SPREAD: By default, objects addresses are spread
458 * between channels in RAM: the pool allocator will add padding
459 * between objects depending on the hardware configuration. See
460 * Memory alignment constraints for details. If this flag is set,
461 * the allocator will just align them to a cache line.
462 * - MEMPOOL_F_NO_CACHE_ALIGN: By default, the returned objects are
463 * cache-aligned. This flag removes this constraint, and no
464 * padding will be present between objects. This flag implies
465 * MEMPOOL_F_NO_SPREAD.
466 * - MEMPOOL_F_SP_PUT: If this flag is set, the default behavior
467 * when using rte_mempool_put() or rte_mempool_put_bulk() is
468 * "single-producer". Otherwise, it is "multi-producers".
469 * - MEMPOOL_F_SC_GET: If this flag is set, the default behavior
470 * when using rte_mempool_get() or rte_mempool_get_bulk() is
471 * "single-consumer". Otherwise, it is "multi-consumers".
473 * The pointer to the new allocated mempool, on success. NULL on error
474 * with rte_errno set appropriately. Possible rte_errno values include:
475 * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
476 * - E_RTE_SECONDARY - function was called from a secondary process instance
477 * - EINVAL - cache size provided is too large
478 * - ENOSPC - the maximum number of memzones has already been allocated
479 * - EEXIST - a memzone with the same name already exists
480 * - ENOMEM - no appropriate memory area found in which to create memzone
483 rte_mempool_create(const char *name, unsigned n, unsigned elt_size,
484 unsigned cache_size, unsigned private_data_size,
485 rte_mempool_ctor_t *mp_init, void *mp_init_arg,
486 rte_mempool_obj_cb_t *obj_init, void *obj_init_arg,
487 int socket_id, unsigned flags);
490 * Create a new mempool named *name* in memory.
492 * The pool contains n elements of elt_size. Its size is set to n.
493 * This function uses ``memzone_reserve()`` to allocate the mempool header
494 * (and the objects if vaddr is NULL).
495 * Depending on the input parameters, mempool elements can be either allocated
496 * together with the mempool header, or an externally provided memory buffer
497 * could be used to store mempool objects. In later case, that external
498 * memory buffer can consist of set of disjoint physical pages.
501 * The name of the mempool.
503 * The number of elements in the mempool. The optimum size (in terms of
504 * memory usage) for a mempool is when n is a power of two minus one:
507 * The size of each element.
509 * Size of the cache. See rte_mempool_create() for details.
510 * @param private_data_size
511 * The size of the private data appended after the mempool
512 * structure. This is useful for storing some private data after the
513 * mempool structure, as is done for rte_mbuf_pool for example.
515 * A function pointer that is called for initialization of the pool,
516 * before object initialization. The user can initialize the private
517 * data in this function if needed. This parameter can be NULL if
520 * An opaque pointer to data that can be used in the mempool
521 * constructor function.
523 * A function called for each object at initialization of the pool.
524 * See rte_mempool_create() for details.
525 * @param obj_init_arg
526 * An opaque pointer passed to the object constructor function.
528 * The *socket_id* argument is the socket identifier in the case of
529 * NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
530 * constraint for the reserved zone.
532 * Flags controlling the behavior of the mempool. See
533 * rte_mempool_create() for details.
535 * Virtual address of the externally allocated memory buffer.
536 * Will be used to store mempool objects.
538 * Array of physical addresses of the pages that comprises given memory
541 * Number of elements in the paddr array.
543 * LOG2 of the physical pages size.
545 * The pointer to the new allocated mempool, on success. NULL on error
546 * with rte_errno set appropriately. See rte_mempool_create() for details.
549 rte_mempool_xmem_create(const char *name, unsigned n, unsigned elt_size,
550 unsigned cache_size, unsigned private_data_size,
551 rte_mempool_ctor_t *mp_init, void *mp_init_arg,
552 rte_mempool_obj_cb_t *obj_init, void *obj_init_arg,
553 int socket_id, unsigned flags, void *vaddr,
554 const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift);
557 * Create a new mempool named *name* in memory on Xen Dom0.
559 * This function uses ``rte_mempool_xmem_create()`` to allocate memory. The
560 * pool contains n elements of elt_size. Its size is set to n.
561 * All elements of the mempool are allocated together with the mempool header,
562 * and memory buffer can consist of set of disjoint physical pages.
565 * The name of the mempool.
567 * The number of elements in the mempool. The optimum size (in terms of
568 * memory usage) for a mempool is when n is a power of two minus one:
571 * The size of each element.
573 * If cache_size is non-zero, the rte_mempool library will try to
574 * limit the accesses to the common lockless pool, by maintaining a
575 * per-lcore object cache. This argument must be lower or equal to
576 * CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE. It is advised to choose
577 * cache_size to have "n modulo cache_size == 0": if this is
578 * not the case, some elements will always stay in the pool and will
579 * never be used. The access to the per-lcore table is of course
580 * faster than the multi-producer/consumer pool. The cache can be
581 * disabled if the cache_size argument is set to 0; it can be useful to
582 * avoid losing objects in cache. Note that even if not used, the
583 * memory space for cache is always reserved in a mempool structure,
584 * except if CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE is set to 0.
585 * @param private_data_size
586 * The size of the private data appended after the mempool
587 * structure. This is useful for storing some private data after the
588 * mempool structure, as is done for rte_mbuf_pool for example.
590 * A function pointer that is called for initialization of the pool,
591 * before object initialization. The user can initialize the private
592 * data in this function if needed. This parameter can be NULL if
595 * An opaque pointer to data that can be used in the mempool
596 * constructor function.
598 * A function pointer that is called for each object at
599 * initialization of the pool. The user can set some meta data in
600 * objects if needed. This parameter can be NULL if not needed.
601 * The obj_init() function takes the mempool pointer, the init_arg,
602 * the object pointer and the object number as parameters.
603 * @param obj_init_arg
604 * An opaque pointer to data that can be used as an argument for
605 * each call to the object constructor function.
607 * The *socket_id* argument is the socket identifier in the case of
608 * NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
609 * constraint for the reserved zone.
611 * The *flags* arguments is an OR of following flags:
612 * - MEMPOOL_F_NO_SPREAD: By default, objects addresses are spread
613 * between channels in RAM: the pool allocator will add padding
614 * between objects depending on the hardware configuration. See
615 * Memory alignment constraints for details. If this flag is set,
616 * the allocator will just align them to a cache line.
617 * - MEMPOOL_F_NO_CACHE_ALIGN: By default, the returned objects are
618 * cache-aligned. This flag removes this constraint, and no
619 * padding will be present between objects. This flag implies
620 * MEMPOOL_F_NO_SPREAD.
621 * - MEMPOOL_F_SP_PUT: If this flag is set, the default behavior
622 * when using rte_mempool_put() or rte_mempool_put_bulk() is
623 * "single-producer". Otherwise, it is "multi-producers".
624 * - MEMPOOL_F_SC_GET: If this flag is set, the default behavior
625 * when using rte_mempool_get() or rte_mempool_get_bulk() is
626 * "single-consumer". Otherwise, it is "multi-consumers".
628 * The pointer to the new allocated mempool, on success. NULL on error
629 * with rte_errno set appropriately. Possible rte_errno values include:
630 * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
631 * - E_RTE_SECONDARY - function was called from a secondary process instance
632 * - EINVAL - cache size provided is too large
633 * - ENOSPC - the maximum number of memzones has already been allocated
634 * - EEXIST - a memzone with the same name already exists
635 * - ENOMEM - no appropriate memory area found in which to create memzone
638 rte_dom0_mempool_create(const char *name, unsigned n, unsigned elt_size,
639 unsigned cache_size, unsigned private_data_size,
640 rte_mempool_ctor_t *mp_init, void *mp_init_arg,
641 rte_mempool_obj_cb_t *obj_init, void *obj_init_arg,
642 int socket_id, unsigned flags);
646 * Dump the status of the mempool to the console.
649 * A pointer to a file for output
651 * A pointer to the mempool structure.
653 void rte_mempool_dump(FILE *f, struct rte_mempool *mp);
656 * @internal Put several objects back in the mempool; used internally.
658 * A pointer to the mempool structure.
660 * A pointer to a table of void * pointers (objects).
662 * The number of objects to store back in the mempool, must be strictly
665 * Mono-producer (0) or multi-producers (1).
667 static inline void __attribute__((always_inline))
668 __mempool_put_bulk(struct rte_mempool *mp, void * const *obj_table,
669 unsigned n, int is_mp)
671 struct rte_mempool_cache *cache;
674 unsigned lcore_id = rte_lcore_id();
675 uint32_t cache_size = mp->cache_size;
676 uint32_t flushthresh = mp->cache_flushthresh;
678 /* increment stat now, adding in mempool always success */
679 __MEMPOOL_STAT_ADD(mp, put, n);
681 /* cache is not enabled or single producer or non-EAL thread */
682 if (unlikely(cache_size == 0 || is_mp == 0 ||
683 lcore_id >= RTE_MAX_LCORE))
686 /* Go straight to ring if put would overflow mem allocated for cache */
687 if (unlikely(n > RTE_MEMPOOL_CACHE_MAX_SIZE))
690 cache = &mp->local_cache[lcore_id];
691 cache_objs = &cache->objs[cache->len];
694 * The cache follows the following algorithm
695 * 1. Add the objects to the cache
696 * 2. Anything greater than the cache min value (if it crosses the
697 * cache flush threshold) is flushed to the ring.
700 /* Add elements back into the cache */
701 for (index = 0; index < n; ++index, obj_table++)
702 cache_objs[index] = *obj_table;
706 if (cache->len >= flushthresh) {
707 rte_ring_mp_enqueue_bulk(mp->ring, &cache->objs[cache_size],
708 cache->len - cache_size);
709 cache->len = cache_size;
716 /* push remaining objects in ring */
717 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
719 if (rte_ring_mp_enqueue_bulk(mp->ring, obj_table, n) < 0)
720 rte_panic("cannot put objects in mempool\n");
723 if (rte_ring_sp_enqueue_bulk(mp->ring, obj_table, n) < 0)
724 rte_panic("cannot put objects in mempool\n");
728 rte_ring_mp_enqueue_bulk(mp->ring, obj_table, n);
730 rte_ring_sp_enqueue_bulk(mp->ring, obj_table, n);
736 * Put several objects back in the mempool (multi-producers safe).
739 * A pointer to the mempool structure.
741 * A pointer to a table of void * pointers (objects).
743 * The number of objects to add in the mempool from the obj_table.
745 static inline void __attribute__((always_inline))
746 rte_mempool_mp_put_bulk(struct rte_mempool *mp, void * const *obj_table,
749 __mempool_check_cookies(mp, obj_table, n, 0);
750 __mempool_put_bulk(mp, obj_table, n, 1);
754 * Put several objects back in the mempool (NOT multi-producers safe).
757 * A pointer to the mempool structure.
759 * A pointer to a table of void * pointers (objects).
761 * The number of objects to add in the mempool from obj_table.
764 rte_mempool_sp_put_bulk(struct rte_mempool *mp, void * const *obj_table,
767 __mempool_check_cookies(mp, obj_table, n, 0);
768 __mempool_put_bulk(mp, obj_table, n, 0);
772 * Put several objects back in the mempool.
774 * This function calls the multi-producer or the single-producer
775 * version depending on the default behavior that was specified at
776 * mempool creation time (see flags).
779 * A pointer to the mempool structure.
781 * A pointer to a table of void * pointers (objects).
783 * The number of objects to add in the mempool from obj_table.
785 static inline void __attribute__((always_inline))
786 rte_mempool_put_bulk(struct rte_mempool *mp, void * const *obj_table,
789 __mempool_check_cookies(mp, obj_table, n, 0);
790 __mempool_put_bulk(mp, obj_table, n, !(mp->flags & MEMPOOL_F_SP_PUT));
794 * Put one object in the mempool (multi-producers safe).
797 * A pointer to the mempool structure.
799 * A pointer to the object to be added.
801 static inline void __attribute__((always_inline))
802 rte_mempool_mp_put(struct rte_mempool *mp, void *obj)
804 rte_mempool_mp_put_bulk(mp, &obj, 1);
808 * Put one object back in the mempool (NOT multi-producers safe).
811 * A pointer to the mempool structure.
813 * A pointer to the object to be added.
815 static inline void __attribute__((always_inline))
816 rte_mempool_sp_put(struct rte_mempool *mp, void *obj)
818 rte_mempool_sp_put_bulk(mp, &obj, 1);
822 * Put one object back in the mempool.
824 * This function calls the multi-producer or the single-producer
825 * version depending on the default behavior that was specified at
826 * mempool creation time (see flags).
829 * A pointer to the mempool structure.
831 * A pointer to the object to be added.
833 static inline void __attribute__((always_inline))
834 rte_mempool_put(struct rte_mempool *mp, void *obj)
836 rte_mempool_put_bulk(mp, &obj, 1);
840 * @internal Get several objects from the mempool; used internally.
842 * A pointer to the mempool structure.
844 * A pointer to a table of void * pointers (objects).
846 * The number of objects to get, must be strictly positive.
848 * Mono-consumer (0) or multi-consumers (1).
850 * - >=0: Success; number of objects supplied.
851 * - <0: Error; code of ring dequeue function.
853 static inline int __attribute__((always_inline))
854 __mempool_get_bulk(struct rte_mempool *mp, void **obj_table,
855 unsigned n, int is_mc)
858 struct rte_mempool_cache *cache;
861 unsigned lcore_id = rte_lcore_id();
862 uint32_t cache_size = mp->cache_size;
864 /* cache is not enabled or single consumer */
865 if (unlikely(cache_size == 0 || is_mc == 0 ||
866 n >= cache_size || lcore_id >= RTE_MAX_LCORE))
869 cache = &mp->local_cache[lcore_id];
870 cache_objs = cache->objs;
872 /* Can this be satisfied from the cache? */
873 if (cache->len < n) {
874 /* No. Backfill the cache first, and then fill from it */
875 uint32_t req = n + (cache_size - cache->len);
877 /* How many do we require i.e. number to fill the cache + the request */
878 ret = rte_ring_mc_dequeue_bulk(mp->ring, &cache->objs[cache->len], req);
879 if (unlikely(ret < 0)) {
881 * In the offchance that we are buffer constrained,
882 * where we are not able to allocate cache + n, go to
883 * the ring directly. If that fails, we are truly out of
892 /* Now fill in the response ... */
893 for (index = 0, len = cache->len - 1; index < n; ++index, len--, obj_table++)
894 *obj_table = cache_objs[len];
898 __MEMPOOL_STAT_ADD(mp, get_success, n);
904 /* get remaining objects from ring */
906 ret = rte_ring_mc_dequeue_bulk(mp->ring, obj_table, n);
908 ret = rte_ring_sc_dequeue_bulk(mp->ring, obj_table, n);
911 __MEMPOOL_STAT_ADD(mp, get_fail, n);
913 __MEMPOOL_STAT_ADD(mp, get_success, n);
919 * Get several objects from the mempool (multi-consumers safe).
921 * If cache is enabled, objects will be retrieved first from cache,
922 * subsequently from the common pool. Note that it can return -ENOENT when
923 * the local cache and common pool are empty, even if cache from other
927 * A pointer to the mempool structure.
929 * A pointer to a table of void * pointers (objects) that will be filled.
931 * The number of objects to get from mempool to obj_table.
933 * - 0: Success; objects taken.
934 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
936 static inline int __attribute__((always_inline))
937 rte_mempool_mc_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned n)
940 ret = __mempool_get_bulk(mp, obj_table, n, 1);
942 __mempool_check_cookies(mp, obj_table, n, 1);
947 * Get several objects from the mempool (NOT multi-consumers safe).
949 * If cache is enabled, objects will be retrieved first from cache,
950 * subsequently from the common pool. Note that it can return -ENOENT when
951 * the local cache and common pool are empty, even if cache from other
955 * A pointer to the mempool structure.
957 * A pointer to a table of void * pointers (objects) that will be filled.
959 * The number of objects to get from the mempool to obj_table.
961 * - 0: Success; objects taken.
962 * - -ENOENT: Not enough entries in the mempool; no object is
965 static inline int __attribute__((always_inline))
966 rte_mempool_sc_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned n)
969 ret = __mempool_get_bulk(mp, obj_table, n, 0);
971 __mempool_check_cookies(mp, obj_table, n, 1);
976 * Get several objects from the mempool.
978 * This function calls the multi-consumers or the single-consumer
979 * version, depending on the default behaviour that was specified at
980 * mempool creation time (see flags).
982 * If cache is enabled, objects will be retrieved first from cache,
983 * subsequently from the common pool. Note that it can return -ENOENT when
984 * the local cache and common pool are empty, even if cache from other
988 * A pointer to the mempool structure.
990 * A pointer to a table of void * pointers (objects) that will be filled.
992 * The number of objects to get from the mempool to obj_table.
994 * - 0: Success; objects taken
995 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
997 static inline int __attribute__((always_inline))
998 rte_mempool_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned n)
1001 ret = __mempool_get_bulk(mp, obj_table, n,
1002 !(mp->flags & MEMPOOL_F_SC_GET));
1004 __mempool_check_cookies(mp, obj_table, n, 1);
1009 * Get one object from the mempool (multi-consumers safe).
1011 * If cache is enabled, objects will be retrieved first from cache,
1012 * subsequently from the common pool. Note that it can return -ENOENT when
1013 * the local cache and common pool are empty, even if cache from other
1017 * A pointer to the mempool structure.
1019 * A pointer to a void * pointer (object) that will be filled.
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(struct rte_mempool *mp, void **obj_p)
1027 return rte_mempool_mc_get_bulk(mp, obj_p, 1);
1031 * Get one object from the mempool (NOT multi-consumers safe).
1033 * If cache is enabled, objects will be retrieved first from cache,
1034 * subsequently from the common pool. Note that it can return -ENOENT when
1035 * the local cache and common pool are empty, even if cache from other
1039 * A pointer to the mempool structure.
1041 * A pointer to a void * pointer (object) that will be filled.
1043 * - 0: Success; objects taken.
1044 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
1046 static inline int __attribute__((always_inline))
1047 rte_mempool_sc_get(struct rte_mempool *mp, void **obj_p)
1049 return rte_mempool_sc_get_bulk(mp, obj_p, 1);
1053 * Get one object from the mempool.
1055 * This function calls the multi-consumers or the single-consumer
1056 * version, depending on the default behavior that was specified at
1057 * mempool creation (see flags).
1059 * If cache is enabled, objects will be retrieved first from cache,
1060 * subsequently from the common pool. Note that it can return -ENOENT when
1061 * the local cache and common pool are empty, even if cache from other
1065 * A pointer to the mempool structure.
1067 * A pointer to a void * pointer (object) that will be filled.
1069 * - 0: Success; objects taken.
1070 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
1072 static inline int __attribute__((always_inline))
1073 rte_mempool_get(struct rte_mempool *mp, void **obj_p)
1075 return rte_mempool_get_bulk(mp, obj_p, 1);
1079 * Return the number of entries in the mempool.
1081 * When cache is enabled, this function has to browse the length of
1082 * all lcores, so it should not be used in a data path, but only for
1086 * A pointer to the mempool structure.
1088 * The number of entries in the mempool.
1090 unsigned rte_mempool_count(const struct rte_mempool *mp);
1093 * Return the number of free entries in the mempool ring.
1094 * i.e. how many entries can be freed back to the mempool.
1096 * NOTE: This corresponds to the number of elements *allocated* from the
1097 * memory pool, not the number of elements in the pool itself. To count
1098 * the number elements currently available in the pool, use "rte_mempool_count"
1100 * When cache is enabled, this function has to browse the length of
1101 * all lcores, so it should not be used in a data path, but only for
1105 * A pointer to the mempool structure.
1107 * The number of free entries in the mempool.
1109 static inline unsigned
1110 rte_mempool_free_count(const struct rte_mempool *mp)
1112 return mp->size - rte_mempool_count(mp);
1116 * Test if the mempool is full.
1118 * When cache is enabled, this function has to browse the length of all
1119 * lcores, so it should not be used in a data path, but only for debug
1123 * A pointer to the mempool structure.
1125 * - 1: The mempool is full.
1126 * - 0: The mempool is not full.
1129 rte_mempool_full(const struct rte_mempool *mp)
1131 return !!(rte_mempool_count(mp) == mp->size);
1135 * Test if the mempool is empty.
1137 * When cache is enabled, this function has to browse the length of all
1138 * lcores, so it should not be used in a data path, but only for debug
1142 * A pointer to the mempool structure.
1144 * - 1: The mempool is empty.
1145 * - 0: The mempool is not empty.
1148 rte_mempool_empty(const struct rte_mempool *mp)
1150 return !!(rte_mempool_count(mp) == 0);
1154 * Return the physical address of elt, which is an element of the pool mp.
1157 * A pointer to the mempool structure.
1159 * A pointer (virtual address) to the element of the pool.
1161 * The physical address of the elt element.
1163 static inline phys_addr_t
1164 rte_mempool_virt2phy(const struct rte_mempool *mp, const void *elt)
1166 if (rte_eal_has_hugepages()) {
1169 off = (const char *)elt - (const char *)mp->elt_va_start;
1170 return mp->elt_pa[off >> mp->pg_shift] + (off & mp->pg_mask);
1173 * If huge pages are disabled, we cannot assume the
1174 * memory region to be physically contiguous.
1175 * Lookup for each element.
1177 return rte_mem_virt2phy(elt);
1182 * Check the consistency of mempool objects.
1184 * Verify the coherency of fields in the mempool structure. Also check
1185 * that the cookies of mempool objects (even the ones that are not
1186 * present in pool) have a correct value. If not, a panic will occur.
1189 * A pointer to the mempool structure.
1191 void rte_mempool_audit(struct rte_mempool *mp);
1194 * Return a pointer to the private data in an mempool structure.
1197 * A pointer to the mempool structure.
1199 * A pointer to the private data.
1201 static inline void *rte_mempool_get_priv(struct rte_mempool *mp)
1204 MEMPOOL_HEADER_SIZE(mp, mp->pg_num, mp->cache_size);
1208 * Dump the status of all mempools on the console
1211 * A pointer to a file for output
1213 void rte_mempool_list_dump(FILE *f);
1216 * Search a mempool from its name
1219 * The name of the mempool.
1221 * The pointer to the mempool matching the name, or NULL if not found.
1223 * with rte_errno set appropriately. Possible rte_errno values include:
1224 * - ENOENT - required entry not available to return.
1227 struct rte_mempool *rte_mempool_lookup(const char *name);
1230 * Get the header, trailer and total size of a mempool element.
1232 * Given a desired size of the mempool element and mempool flags,
1233 * calculates header, trailer, body and total sizes of the mempool object.
1236 * The size of each element, without header and trailer.
1238 * The flags used for the mempool creation.
1239 * Consult rte_mempool_create() for more information about possible values.
1240 * The size of each element.
1242 * The calculated detailed size the mempool object. May be NULL.
1244 * Total size of the mempool object.
1246 uint32_t rte_mempool_calc_obj_size(uint32_t elt_size, uint32_t flags,
1247 struct rte_mempool_objsz *sz);
1250 * Get the size of memory required to store mempool elements.
1252 * Calculate the maximum amount of memory required to store given number
1253 * of objects. Assume that the memory buffer will be aligned at page
1256 * Note that if object size is bigger then page size, then it assumes
1257 * that pages are grouped in subsets of physically continuous pages big
1258 * enough to store at least one object.
1261 * Number of elements.
1262 * @param total_elt_sz
1263 * The size of each element, including header and trailer, as returned
1264 * by rte_mempool_calc_obj_size().
1266 * LOG2 of the physical pages size.
1268 * Required memory size aligned at page boundary.
1270 size_t rte_mempool_xmem_size(uint32_t elt_num, size_t total_elt_sz,
1274 * Get the size of memory required to store mempool elements.
1276 * Calculate how much memory would be actually required with the given
1277 * memory footprint to store required number of objects.
1280 * Virtual address of the externally allocated memory buffer.
1281 * Will be used to store mempool objects.
1283 * Number of elements.
1284 * @param total_elt_sz
1285 * The size of each element, including header and trailer, as returned
1286 * by rte_mempool_calc_obj_size().
1288 * Array of physical addresses of the pages that comprises given memory
1291 * Number of elements in the paddr array.
1293 * LOG2 of the physical pages size.
1295 * On success, the number of bytes needed to store given number of
1296 * objects, aligned to the given page size. If the provided memory
1297 * buffer is too small, return a negative value whose absolute value
1298 * is the actual number of elements that can be stored in that buffer.
1300 ssize_t rte_mempool_xmem_usage(void *vaddr, uint32_t elt_num,
1301 size_t total_elt_sz, const phys_addr_t paddr[], uint32_t pg_num,
1305 * Walk list of all memory pools
1310 * Argument passed to iterator
1312 void rte_mempool_walk(void (*func)(struct rte_mempool *, void *arg),
1319 #endif /* _RTE_MEMPOOL_H_ */