4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5 * Copyright(c) 2016 6WIND S.A.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
18 * * Neither the name of Intel Corporation nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #ifndef _RTE_MEMPOOL_H_
36 #define _RTE_MEMPOOL_H_
42 * A memory pool is an allocator of fixed-size object. It is
43 * identified by its name, and uses a ring to store free objects. It
44 * provides some other optional services, like a per-core object
45 * cache, and an alignment helper to ensure that objects are padded
46 * to spread them equally on all RAM channels, ranks, and so on.
48 * Objects owned by a mempool should never be added in another
49 * mempool. When an object is freed using rte_mempool_put() or
50 * equivalent, the object data is not modified; the user can save some
51 * meta-data in the object data and retrieve them when allocating a
54 * Note: the mempool implementation is not preemptable. A lcore must
55 * not be interrupted by another task that uses the same mempool
56 * (because it uses a ring which is not preemptable). Also, mempool
57 * functions must not be used outside the DPDK environment: for
58 * example, in linuxapp environment, a thread that is not created by
59 * the EAL must not use mempools. This is due to the per-lcore cache
60 * that won't work as rte_lcore_id() will not return a correct value.
68 #include <sys/queue.h>
71 #include <rte_debug.h>
72 #include <rte_lcore.h>
73 #include <rte_memory.h>
74 #include <rte_branch_prediction.h>
81 #define RTE_MEMPOOL_HEADER_COOKIE1 0xbadbadbadadd2e55ULL /**< Header cookie. */
82 #define RTE_MEMPOOL_HEADER_COOKIE2 0xf2eef2eedadd2e55ULL /**< Header cookie. */
83 #define RTE_MEMPOOL_TRAILER_COOKIE 0xadd2e55badbadbadULL /**< Trailer cookie.*/
85 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
87 * A structure that stores the mempool statistics (per-lcore).
89 struct rte_mempool_debug_stats {
90 uint64_t put_bulk; /**< Number of puts. */
91 uint64_t put_objs; /**< Number of objects successfully put. */
92 uint64_t get_success_bulk; /**< Successful allocation number. */
93 uint64_t get_success_objs; /**< Objects successfully allocated. */
94 uint64_t get_fail_bulk; /**< Failed allocation number. */
95 uint64_t get_fail_objs; /**< Objects that failed to be allocated. */
96 } __rte_cache_aligned;
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;
112 * A structure that stores the size of mempool elements.
114 struct rte_mempool_objsz {
115 uint32_t elt_size; /**< Size of an element. */
116 uint32_t header_size; /**< Size of header (before elt). */
117 uint32_t trailer_size; /**< Size of trailer (after elt). */
119 /**< Total size of an object (header + elt + trailer). */
122 #define RTE_MEMPOOL_NAMESIZE 32 /**< Maximum length of a memory pool. */
123 #define RTE_MEMPOOL_MZ_PREFIX "MP_"
126 #define RTE_MEMPOOL_MZ_FORMAT RTE_MEMPOOL_MZ_PREFIX "%s"
128 #define MEMPOOL_PG_SHIFT_MAX (sizeof(uintptr_t) * CHAR_BIT - 1)
130 /** Mempool over one chunk of physically continuous memory */
131 #define MEMPOOL_PG_NUM_DEFAULT 1
133 #ifndef RTE_MEMPOOL_ALIGN
134 #define RTE_MEMPOOL_ALIGN RTE_CACHE_LINE_SIZE
137 #define RTE_MEMPOOL_ALIGN_MASK (RTE_MEMPOOL_ALIGN - 1)
140 * Mempool object header structure
142 * Each object stored in mempools are prefixed by this header structure,
143 * it allows to retrieve the mempool pointer from the object and to
144 * iterate on all objects attached to a mempool. When debug is enabled,
145 * a cookie is also added in this structure preventing corruptions and
148 struct rte_mempool_objhdr {
149 STAILQ_ENTRY(rte_mempool_objhdr) next; /**< Next in list. */
150 struct rte_mempool *mp; /**< The mempool owning the object. */
151 phys_addr_t physaddr; /**< Physical address of the object. */
152 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
153 uint64_t cookie; /**< Debug cookie. */
158 * A list of object headers type
160 STAILQ_HEAD(rte_mempool_objhdr_list, rte_mempool_objhdr);
163 * Mempool object trailer structure
165 * In debug mode, each object stored in mempools are suffixed by this
166 * trailer structure containing a cookie preventing memory corruptions.
168 struct rte_mempool_objtlr {
169 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
170 uint64_t cookie; /**< Debug cookie. */
175 * A list of memory where objects are stored
177 STAILQ_HEAD(rte_mempool_memhdr_list, rte_mempool_memhdr);
180 * Callback used to free a memory chunk
182 typedef void (rte_mempool_memchunk_free_cb_t)(struct rte_mempool_memhdr *memhdr,
186 * Mempool objects memory header structure
188 * The memory chunks where objects are stored. Each chunk is virtually
189 * and physically contiguous.
191 struct rte_mempool_memhdr {
192 STAILQ_ENTRY(rte_mempool_memhdr) next; /**< Next in list. */
193 struct rte_mempool *mp; /**< The mempool owning the chunk */
194 void *addr; /**< Virtual address of the chunk */
195 phys_addr_t phys_addr; /**< Physical address of the chunk */
196 size_t len; /**< length of the chunk */
197 rte_mempool_memchunk_free_cb_t *free_cb; /**< Free callback */
198 void *opaque; /**< Argument passed to the free callback */
202 * The RTE mempool structure.
205 char name[RTE_MEMPOOL_NAMESIZE]; /**< Name of mempool. */
206 struct rte_ring *ring; /**< Ring to store objects. */
207 const struct rte_memzone *mz; /**< Memzone where pool is allocated */
208 int flags; /**< Flags of the mempool. */
209 int socket_id; /**< Socket id passed at mempool creation. */
210 uint32_t size; /**< Max size of the mempool. */
211 uint32_t cache_size; /**< Size of per-lcore local cache. */
212 uint32_t cache_flushthresh;
213 /**< Threshold before we flush excess elements. */
215 uint32_t elt_size; /**< Size of an element. */
216 uint32_t header_size; /**< Size of header (before elt). */
217 uint32_t trailer_size; /**< Size of trailer (after elt). */
219 unsigned private_data_size; /**< Size of private data. */
221 struct rte_mempool_cache *local_cache; /**< Per-lcore local cache */
223 uint32_t populated_size; /**< Number of populated objects. */
224 struct rte_mempool_objhdr_list elt_list; /**< List of objects in pool */
225 uint32_t nb_mem_chunks; /**< Number of memory chunks */
226 struct rte_mempool_memhdr_list mem_list; /**< List of memory chunks */
228 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
229 /** Per-lcore statistics. */
230 struct rte_mempool_debug_stats stats[RTE_MAX_LCORE];
232 } __rte_cache_aligned;
234 #define MEMPOOL_F_NO_SPREAD 0x0001 /**< Do not spread among memory channels. */
235 #define MEMPOOL_F_NO_CACHE_ALIGN 0x0002 /**< Do not align objs on cache lines.*/
236 #define MEMPOOL_F_SP_PUT 0x0004 /**< Default put is "single-producer".*/
237 #define MEMPOOL_F_SC_GET 0x0008 /**< Default get is "single-consumer".*/
240 * @internal When debug is enabled, store some statistics.
243 * Pointer to the memory pool.
245 * Name of the statistics field to increment in the memory pool.
247 * Number to add to the object-oriented statistics.
249 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
250 #define __MEMPOOL_STAT_ADD(mp, name, n) do { \
251 unsigned __lcore_id = rte_lcore_id(); \
252 if (__lcore_id < RTE_MAX_LCORE) { \
253 mp->stats[__lcore_id].name##_objs += n; \
254 mp->stats[__lcore_id].name##_bulk += 1; \
258 #define __MEMPOOL_STAT_ADD(mp, name, n) do {} while(0)
262 * Calculate the size of the mempool header.
265 * Pointer to the memory pool.
267 * Size of the per-lcore cache.
269 #define MEMPOOL_HEADER_SIZE(mp, cs) \
270 (sizeof(*(mp)) + (((cs) == 0) ? 0 : \
271 (sizeof(struct rte_mempool_cache) * RTE_MAX_LCORE)))
273 /* return the header of a mempool object (internal) */
274 static inline struct rte_mempool_objhdr *__mempool_get_header(void *obj)
276 return (struct rte_mempool_objhdr *)RTE_PTR_SUB(obj,
277 sizeof(struct rte_mempool_objhdr));
281 * Return a pointer to the mempool owning this object.
284 * An object that is owned by a pool. If this is not the case,
285 * the behavior is undefined.
287 * A pointer to the mempool structure.
289 static inline struct rte_mempool *rte_mempool_from_obj(void *obj)
291 struct rte_mempool_objhdr *hdr = __mempool_get_header(obj);
295 /* return the trailer of a mempool object (internal) */
296 static inline struct rte_mempool_objtlr *__mempool_get_trailer(void *obj)
298 struct rte_mempool *mp = rte_mempool_from_obj(obj);
299 return (struct rte_mempool_objtlr *)RTE_PTR_ADD(obj, mp->elt_size);
303 * @internal Check and update cookies or panic.
306 * Pointer to the memory pool.
307 * @param obj_table_const
308 * Pointer to a table of void * pointers (objects).
310 * Index of object in object table.
312 * - 0: object is supposed to be allocated, mark it as free
313 * - 1: object is supposed to be free, mark it as allocated
314 * - 2: just check that cookie is valid (free or allocated)
316 void rte_mempool_check_cookies(const struct rte_mempool *mp,
317 void * const *obj_table_const, unsigned n, int free);
319 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
320 #define __mempool_check_cookies(mp, obj_table_const, n, free) \
321 rte_mempool_check_cookies(mp, obj_table_const, n, free)
323 #define __mempool_check_cookies(mp, obj_table_const, n, free) do {} while(0)
324 #endif /* RTE_LIBRTE_MEMPOOL_DEBUG */
327 * An object callback function for mempool.
329 * Used by rte_mempool_create() and rte_mempool_obj_iter().
331 typedef void (rte_mempool_obj_cb_t)(struct rte_mempool *mp,
332 void *opaque, void *obj, unsigned obj_idx);
333 typedef rte_mempool_obj_cb_t rte_mempool_obj_ctor_t; /* compat */
336 * A memory callback function for mempool.
338 * Used by rte_mempool_mem_iter().
340 typedef void (rte_mempool_mem_cb_t)(struct rte_mempool *mp,
341 void *opaque, struct rte_mempool_memhdr *memhdr,
345 * A mempool constructor callback function.
347 * Arguments are the mempool and the opaque pointer given by the user in
348 * rte_mempool_create().
350 typedef void (rte_mempool_ctor_t)(struct rte_mempool *, void *);
353 * Create a new mempool named *name* in memory.
355 * This function uses ``memzone_reserve()`` to allocate memory. The
356 * pool contains n elements of elt_size. Its size is set to n.
357 * All elements of the mempool are allocated together with the mempool header,
358 * in one physically continuous chunk of memory.
361 * The name of the mempool.
363 * The number of elements in the mempool. The optimum size (in terms of
364 * memory usage) for a mempool is when n is a power of two minus one:
367 * The size of each element.
369 * If cache_size is non-zero, the rte_mempool library will try to
370 * limit the accesses to the common lockless pool, by maintaining a
371 * per-lcore object cache. This argument must be lower or equal to
372 * CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE and n / 1.5. It is advised to choose
373 * cache_size to have "n modulo cache_size == 0": if this is
374 * not the case, some elements will always stay in the pool and will
375 * never be used. The access to the per-lcore table is of course
376 * faster than the multi-producer/consumer pool. The cache can be
377 * disabled if the cache_size argument is set to 0; it can be useful to
378 * avoid losing objects in cache. Note that even if not used, the
379 * memory space for cache is always reserved in a mempool structure,
380 * except if CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE is set to 0.
381 * @param private_data_size
382 * The size of the private data appended after the mempool
383 * structure. This is useful for storing some private data after the
384 * mempool structure, as is done for rte_mbuf_pool for example.
386 * A function pointer that is called for initialization of the pool,
387 * before object initialization. The user can initialize the private
388 * data in this function if needed. This parameter can be NULL if
391 * An opaque pointer to data that can be used in the mempool
392 * constructor function.
394 * A function pointer that is called for each object at
395 * initialization of the pool. The user can set some meta data in
396 * objects if needed. This parameter can be NULL if not needed.
397 * The obj_init() function takes the mempool pointer, the init_arg,
398 * the object pointer and the object number as parameters.
399 * @param obj_init_arg
400 * An opaque pointer to data that can be used as an argument for
401 * each call to the object constructor function.
403 * The *socket_id* argument is the socket identifier in the case of
404 * NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
405 * constraint for the reserved zone.
407 * The *flags* arguments is an OR of following flags:
408 * - MEMPOOL_F_NO_SPREAD: By default, objects addresses are spread
409 * between channels in RAM: the pool allocator will add padding
410 * between objects depending on the hardware configuration. See
411 * Memory alignment constraints for details. If this flag is set,
412 * the allocator will just align them to a cache line.
413 * - MEMPOOL_F_NO_CACHE_ALIGN: By default, the returned objects are
414 * cache-aligned. This flag removes this constraint, and no
415 * padding will be present between objects. This flag implies
416 * MEMPOOL_F_NO_SPREAD.
417 * - MEMPOOL_F_SP_PUT: If this flag is set, the default behavior
418 * when using rte_mempool_put() or rte_mempool_put_bulk() is
419 * "single-producer". Otherwise, it is "multi-producers".
420 * - MEMPOOL_F_SC_GET: If this flag is set, the default behavior
421 * when using rte_mempool_get() or rte_mempool_get_bulk() is
422 * "single-consumer". Otherwise, it is "multi-consumers".
424 * The pointer to the new allocated mempool, on success. NULL on error
425 * with rte_errno set appropriately. Possible rte_errno values include:
426 * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
427 * - E_RTE_SECONDARY - function was called from a secondary process instance
428 * - EINVAL - cache size provided is too large
429 * - ENOSPC - the maximum number of memzones has already been allocated
430 * - EEXIST - a memzone with the same name already exists
431 * - ENOMEM - no appropriate memory area found in which to create memzone
434 rte_mempool_create(const char *name, unsigned n, unsigned elt_size,
435 unsigned cache_size, unsigned private_data_size,
436 rte_mempool_ctor_t *mp_init, void *mp_init_arg,
437 rte_mempool_obj_cb_t *obj_init, void *obj_init_arg,
438 int socket_id, unsigned flags);
441 * Create a new mempool named *name* in memory.
443 * The pool contains n elements of elt_size. Its size is set to n.
444 * This function uses ``memzone_reserve()`` to allocate the mempool header
445 * (and the objects if vaddr is NULL).
446 * Depending on the input parameters, mempool elements can be either allocated
447 * together with the mempool header, or an externally provided memory buffer
448 * could be used to store mempool objects. In later case, that external
449 * memory buffer can consist of set of disjoint physical pages.
452 * The name of the mempool.
454 * The number of elements in the mempool. The optimum size (in terms of
455 * memory usage) for a mempool is when n is a power of two minus one:
458 * The size of each element.
460 * Size of the cache. See rte_mempool_create() for details.
461 * @param private_data_size
462 * The size of the private data appended after the mempool
463 * structure. This is useful for storing some private data after the
464 * mempool structure, as is done for rte_mbuf_pool for example.
466 * A function pointer that is called for initialization of the pool,
467 * before object initialization. The user can initialize the private
468 * data in this function if needed. This parameter can be NULL if
471 * An opaque pointer to data that can be used in the mempool
472 * constructor function.
474 * A function called for each object at initialization of the pool.
475 * See rte_mempool_create() for details.
476 * @param obj_init_arg
477 * An opaque pointer passed to the object constructor function.
479 * The *socket_id* argument is the socket identifier in the case of
480 * NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
481 * constraint for the reserved zone.
483 * Flags controlling the behavior of the mempool. See
484 * rte_mempool_create() for details.
486 * Virtual address of the externally allocated memory buffer.
487 * Will be used to store mempool objects.
489 * Array of physical addresses of the pages that comprises given memory
492 * Number of elements in the paddr array.
494 * LOG2 of the physical pages size.
496 * The pointer to the new allocated mempool, on success. NULL on error
497 * with rte_errno set appropriately. See rte_mempool_create() for details.
500 rte_mempool_xmem_create(const char *name, unsigned n, unsigned elt_size,
501 unsigned cache_size, unsigned private_data_size,
502 rte_mempool_ctor_t *mp_init, void *mp_init_arg,
503 rte_mempool_obj_cb_t *obj_init, void *obj_init_arg,
504 int socket_id, unsigned flags, void *vaddr,
505 const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift);
508 * Create a new mempool named *name* in memory on Xen Dom0.
510 * This function uses ``rte_mempool_xmem_create()`` to allocate memory. The
511 * pool contains n elements of elt_size. Its size is set to n.
512 * All elements of the mempool are allocated together with the mempool header,
513 * and memory buffer can consist of set of disjoint physical pages.
516 * The name of the mempool.
518 * The number of elements in the mempool. The optimum size (in terms of
519 * memory usage) for a mempool is when n is a power of two minus one:
522 * The size of each element.
524 * If cache_size is non-zero, the rte_mempool library will try to
525 * limit the accesses to the common lockless pool, by maintaining a
526 * per-lcore object cache. This argument must be lower or equal to
527 * CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE. It is advised to choose
528 * cache_size to have "n modulo cache_size == 0": if this is
529 * not the case, some elements will always stay in the pool and will
530 * never be used. The access to the per-lcore table is of course
531 * faster than the multi-producer/consumer pool. The cache can be
532 * disabled if the cache_size argument is set to 0; it can be useful to
533 * avoid losing objects in cache. Note that even if not used, the
534 * memory space for cache is always reserved in a mempool structure,
535 * except if CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE is set to 0.
536 * @param private_data_size
537 * The size of the private data appended after the mempool
538 * structure. This is useful for storing some private data after the
539 * mempool structure, as is done for rte_mbuf_pool for example.
541 * A function pointer that is called for initialization of the pool,
542 * before object initialization. The user can initialize the private
543 * data in this function if needed. This parameter can be NULL if
546 * An opaque pointer to data that can be used in the mempool
547 * constructor function.
549 * A function pointer that is called for each object at
550 * initialization of the pool. The user can set some meta data in
551 * objects if needed. This parameter can be NULL if not needed.
552 * The obj_init() function takes the mempool pointer, the init_arg,
553 * the object pointer and the object number as parameters.
554 * @param obj_init_arg
555 * An opaque pointer to data that can be used as an argument for
556 * each call to the object constructor function.
558 * The *socket_id* argument is the socket identifier in the case of
559 * NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
560 * constraint for the reserved zone.
562 * The *flags* arguments is an OR of following flags:
563 * - MEMPOOL_F_NO_SPREAD: By default, objects addresses are spread
564 * between channels in RAM: the pool allocator will add padding
565 * between objects depending on the hardware configuration. See
566 * Memory alignment constraints for details. If this flag is set,
567 * the allocator will just align them to a cache line.
568 * - MEMPOOL_F_NO_CACHE_ALIGN: By default, the returned objects are
569 * cache-aligned. This flag removes this constraint, and no
570 * padding will be present between objects. This flag implies
571 * MEMPOOL_F_NO_SPREAD.
572 * - MEMPOOL_F_SP_PUT: If this flag is set, the default behavior
573 * when using rte_mempool_put() or rte_mempool_put_bulk() is
574 * "single-producer". Otherwise, it is "multi-producers".
575 * - MEMPOOL_F_SC_GET: If this flag is set, the default behavior
576 * when using rte_mempool_get() or rte_mempool_get_bulk() is
577 * "single-consumer". Otherwise, it is "multi-consumers".
579 * The pointer to the new allocated mempool, on success. NULL on error
580 * with rte_errno set appropriately. Possible rte_errno values include:
581 * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
582 * - E_RTE_SECONDARY - function was called from a secondary process instance
583 * - EINVAL - cache size provided is too large
584 * - ENOSPC - the maximum number of memzones has already been allocated
585 * - EEXIST - a memzone with the same name already exists
586 * - ENOMEM - no appropriate memory area found in which to create memzone
589 rte_dom0_mempool_create(const char *name, unsigned n, unsigned elt_size,
590 unsigned cache_size, unsigned private_data_size,
591 rte_mempool_ctor_t *mp_init, void *mp_init_arg,
592 rte_mempool_obj_cb_t *obj_init, void *obj_init_arg,
593 int socket_id, unsigned flags);
597 * Call a function for each mempool element
599 * Iterate across all objects attached to a rte_mempool and call the
600 * callback function on it.
603 * A pointer to an initialized mempool.
605 * A function pointer that is called for each object.
607 * An opaque pointer passed to the callback function.
609 * Number of objects iterated.
611 uint32_t rte_mempool_obj_iter(struct rte_mempool *mp,
612 rte_mempool_obj_cb_t *obj_cb, void *obj_cb_arg);
615 * Call a function for each mempool memory chunk
617 * Iterate across all memory chunks attached to a rte_mempool and call
618 * the callback function on it.
621 * A pointer to an initialized mempool.
623 * A function pointer that is called for each memory chunk.
625 * An opaque pointer passed to the callback function.
627 * Number of memory chunks iterated.
629 uint32_t rte_mempool_mem_iter(struct rte_mempool *mp,
630 rte_mempool_mem_cb_t *mem_cb, void *mem_cb_arg);
633 * Dump the status of the mempool to the console.
636 * A pointer to a file for output
638 * A pointer to the mempool structure.
640 void rte_mempool_dump(FILE *f, struct rte_mempool *mp);
643 * @internal Put several objects back in the mempool; used internally.
645 * A pointer to the mempool structure.
647 * A pointer to a table of void * pointers (objects).
649 * The number of objects to store back in the mempool, must be strictly
652 * Mono-producer (0) or multi-producers (1).
654 static inline void __attribute__((always_inline))
655 __mempool_put_bulk(struct rte_mempool *mp, void * const *obj_table,
656 unsigned n, int is_mp)
658 struct rte_mempool_cache *cache;
661 unsigned lcore_id = rte_lcore_id();
662 uint32_t cache_size = mp->cache_size;
663 uint32_t flushthresh = mp->cache_flushthresh;
665 /* increment stat now, adding in mempool always success */
666 __MEMPOOL_STAT_ADD(mp, put, n);
668 /* cache is not enabled or single producer or non-EAL thread */
669 if (unlikely(cache_size == 0 || is_mp == 0 ||
670 lcore_id >= RTE_MAX_LCORE))
673 /* Go straight to ring if put would overflow mem allocated for cache */
674 if (unlikely(n > RTE_MEMPOOL_CACHE_MAX_SIZE))
677 cache = &mp->local_cache[lcore_id];
678 cache_objs = &cache->objs[cache->len];
681 * The cache follows the following algorithm
682 * 1. Add the objects to the cache
683 * 2. Anything greater than the cache min value (if it crosses the
684 * cache flush threshold) is flushed to the ring.
687 /* Add elements back into the cache */
688 for (index = 0; index < n; ++index, obj_table++)
689 cache_objs[index] = *obj_table;
693 if (cache->len >= flushthresh) {
694 rte_ring_mp_enqueue_bulk(mp->ring, &cache->objs[cache_size],
695 cache->len - cache_size);
696 cache->len = cache_size;
703 /* push remaining objects in ring */
704 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
706 if (rte_ring_mp_enqueue_bulk(mp->ring, obj_table, n) < 0)
707 rte_panic("cannot put objects in mempool\n");
710 if (rte_ring_sp_enqueue_bulk(mp->ring, obj_table, n) < 0)
711 rte_panic("cannot put objects in mempool\n");
715 rte_ring_mp_enqueue_bulk(mp->ring, obj_table, n);
717 rte_ring_sp_enqueue_bulk(mp->ring, obj_table, n);
723 * Put several objects back in the mempool (multi-producers safe).
726 * A pointer to the mempool structure.
728 * A pointer to a table of void * pointers (objects).
730 * The number of objects to add in the mempool from the obj_table.
732 static inline void __attribute__((always_inline))
733 rte_mempool_mp_put_bulk(struct rte_mempool *mp, void * const *obj_table,
736 __mempool_check_cookies(mp, obj_table, n, 0);
737 __mempool_put_bulk(mp, obj_table, n, 1);
741 * Put several objects back in the mempool (NOT multi-producers safe).
744 * A pointer to the mempool structure.
746 * A pointer to a table of void * pointers (objects).
748 * The number of objects to add in the mempool from obj_table.
751 rte_mempool_sp_put_bulk(struct rte_mempool *mp, void * const *obj_table,
754 __mempool_check_cookies(mp, obj_table, n, 0);
755 __mempool_put_bulk(mp, obj_table, n, 0);
759 * Put several objects back in the mempool.
761 * This function calls the multi-producer or the single-producer
762 * version depending on the default behavior that was specified at
763 * mempool creation time (see flags).
766 * A pointer to the mempool structure.
768 * A pointer to a table of void * pointers (objects).
770 * The number of objects to add in the mempool from obj_table.
772 static inline void __attribute__((always_inline))
773 rte_mempool_put_bulk(struct rte_mempool *mp, void * const *obj_table,
776 __mempool_check_cookies(mp, obj_table, n, 0);
777 __mempool_put_bulk(mp, obj_table, n, !(mp->flags & MEMPOOL_F_SP_PUT));
781 * Put one object in the mempool (multi-producers safe).
784 * A pointer to the mempool structure.
786 * A pointer to the object to be added.
788 static inline void __attribute__((always_inline))
789 rte_mempool_mp_put(struct rte_mempool *mp, void *obj)
791 rte_mempool_mp_put_bulk(mp, &obj, 1);
795 * Put one object back in the mempool (NOT multi-producers safe).
798 * A pointer to the mempool structure.
800 * A pointer to the object to be added.
802 static inline void __attribute__((always_inline))
803 rte_mempool_sp_put(struct rte_mempool *mp, void *obj)
805 rte_mempool_sp_put_bulk(mp, &obj, 1);
809 * Put one object back in the mempool.
811 * This function calls the multi-producer or the single-producer
812 * version depending on the default behavior that was specified at
813 * mempool creation time (see flags).
816 * A pointer to the mempool structure.
818 * A pointer to the object to be added.
820 static inline void __attribute__((always_inline))
821 rte_mempool_put(struct rte_mempool *mp, void *obj)
823 rte_mempool_put_bulk(mp, &obj, 1);
827 * @internal Get several objects from the mempool; used internally.
829 * A pointer to the mempool structure.
831 * A pointer to a table of void * pointers (objects).
833 * The number of objects to get, must be strictly positive.
835 * Mono-consumer (0) or multi-consumers (1).
837 * - >=0: Success; number of objects supplied.
838 * - <0: Error; code of ring dequeue function.
840 static inline int __attribute__((always_inline))
841 __mempool_get_bulk(struct rte_mempool *mp, void **obj_table,
842 unsigned n, int is_mc)
845 struct rte_mempool_cache *cache;
848 unsigned lcore_id = rte_lcore_id();
849 uint32_t cache_size = mp->cache_size;
851 /* cache is not enabled or single consumer */
852 if (unlikely(cache_size == 0 || is_mc == 0 ||
853 n >= cache_size || lcore_id >= RTE_MAX_LCORE))
856 cache = &mp->local_cache[lcore_id];
857 cache_objs = cache->objs;
859 /* Can this be satisfied from the cache? */
860 if (cache->len < n) {
861 /* No. Backfill the cache first, and then fill from it */
862 uint32_t req = n + (cache_size - cache->len);
864 /* How many do we require i.e. number to fill the cache + the request */
865 ret = rte_ring_mc_dequeue_bulk(mp->ring, &cache->objs[cache->len], req);
866 if (unlikely(ret < 0)) {
868 * In the offchance that we are buffer constrained,
869 * where we are not able to allocate cache + n, go to
870 * the ring directly. If that fails, we are truly out of
879 /* Now fill in the response ... */
880 for (index = 0, len = cache->len - 1; index < n; ++index, len--, obj_table++)
881 *obj_table = cache_objs[len];
885 __MEMPOOL_STAT_ADD(mp, get_success, n);
891 /* get remaining objects from ring */
893 ret = rte_ring_mc_dequeue_bulk(mp->ring, obj_table, n);
895 ret = rte_ring_sc_dequeue_bulk(mp->ring, obj_table, n);
898 __MEMPOOL_STAT_ADD(mp, get_fail, n);
900 __MEMPOOL_STAT_ADD(mp, get_success, n);
906 * Get several objects from the mempool (multi-consumers safe).
908 * If cache is enabled, objects will be retrieved first from cache,
909 * subsequently from the common pool. Note that it can return -ENOENT when
910 * the local cache and common pool are empty, even if cache from other
914 * A pointer to the mempool structure.
916 * A pointer to a table of void * pointers (objects) that will be filled.
918 * The number of objects to get from mempool to obj_table.
920 * - 0: Success; objects taken.
921 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
923 static inline int __attribute__((always_inline))
924 rte_mempool_mc_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned n)
927 ret = __mempool_get_bulk(mp, obj_table, n, 1);
929 __mempool_check_cookies(mp, obj_table, n, 1);
934 * Get several objects from the mempool (NOT multi-consumers safe).
936 * If cache is enabled, objects will be retrieved first from cache,
937 * subsequently from the common pool. Note that it can return -ENOENT when
938 * the local cache and common pool are empty, even if cache from other
942 * A pointer to the mempool structure.
944 * A pointer to a table of void * pointers (objects) that will be filled.
946 * The number of objects to get from the mempool to obj_table.
948 * - 0: Success; objects taken.
949 * - -ENOENT: Not enough entries in the mempool; no object is
952 static inline int __attribute__((always_inline))
953 rte_mempool_sc_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned n)
956 ret = __mempool_get_bulk(mp, obj_table, n, 0);
958 __mempool_check_cookies(mp, obj_table, n, 1);
963 * Get several objects from the mempool.
965 * This function calls the multi-consumers or the single-consumer
966 * version, depending on the default behaviour that was specified at
967 * mempool creation time (see flags).
969 * If cache is enabled, objects will be retrieved first from cache,
970 * subsequently from the common pool. Note that it can return -ENOENT when
971 * the local cache and common pool are empty, even if cache from other
975 * A pointer to the mempool structure.
977 * A pointer to a table of void * pointers (objects) that will be filled.
979 * The number of objects to get from the mempool to obj_table.
981 * - 0: Success; objects taken
982 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
984 static inline int __attribute__((always_inline))
985 rte_mempool_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned n)
988 ret = __mempool_get_bulk(mp, obj_table, n,
989 !(mp->flags & MEMPOOL_F_SC_GET));
991 __mempool_check_cookies(mp, obj_table, n, 1);
996 * Get one object from the mempool (multi-consumers safe).
998 * If cache is enabled, objects will be retrieved first from cache,
999 * subsequently from the common pool. Note that it can return -ENOENT when
1000 * the local cache and common pool are empty, even if cache from other
1004 * A pointer to the mempool structure.
1006 * A pointer to a void * pointer (object) that will be filled.
1008 * - 0: Success; objects taken.
1009 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
1011 static inline int __attribute__((always_inline))
1012 rte_mempool_mc_get(struct rte_mempool *mp, void **obj_p)
1014 return rte_mempool_mc_get_bulk(mp, obj_p, 1);
1018 * Get one object from the mempool (NOT multi-consumers safe).
1020 * If cache is enabled, objects will be retrieved first from cache,
1021 * subsequently from the common pool. Note that it can return -ENOENT when
1022 * the local cache and common pool are empty, even if cache from other
1026 * A pointer to the mempool structure.
1028 * A pointer to a void * pointer (object) that will be filled.
1030 * - 0: Success; objects taken.
1031 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
1033 static inline int __attribute__((always_inline))
1034 rte_mempool_sc_get(struct rte_mempool *mp, void **obj_p)
1036 return rte_mempool_sc_get_bulk(mp, obj_p, 1);
1040 * Get one object from the mempool.
1042 * This function calls the multi-consumers or the single-consumer
1043 * version, depending on the default behavior that was specified at
1044 * mempool creation (see flags).
1046 * If cache is enabled, objects will be retrieved first from cache,
1047 * subsequently from the common pool. Note that it can return -ENOENT when
1048 * the local cache and common pool are empty, even if cache from other
1052 * A pointer to the mempool structure.
1054 * A pointer to a void * pointer (object) that will be filled.
1056 * - 0: Success; objects taken.
1057 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
1059 static inline int __attribute__((always_inline))
1060 rte_mempool_get(struct rte_mempool *mp, void **obj_p)
1062 return rte_mempool_get_bulk(mp, obj_p, 1);
1066 * Return the number of entries in the mempool.
1068 * When cache is enabled, this function has to browse the length of
1069 * all lcores, so it should not be used in a data path, but only for
1073 * A pointer to the mempool structure.
1075 * The number of entries in the mempool.
1077 unsigned rte_mempool_count(const struct rte_mempool *mp);
1080 * Return the number of free entries in the mempool ring.
1081 * i.e. how many entries can be freed back to the mempool.
1083 * NOTE: This corresponds to the number of elements *allocated* from the
1084 * memory pool, not the number of elements in the pool itself. To count
1085 * the number elements currently available in the pool, use "rte_mempool_count"
1087 * When cache is enabled, this function has to browse the length of
1088 * all lcores, so it should not be used in a data path, but only for
1092 * A pointer to the mempool structure.
1094 * The number of free entries in the mempool.
1096 static inline unsigned
1097 rte_mempool_free_count(const struct rte_mempool *mp)
1099 return mp->size - rte_mempool_count(mp);
1103 * Test if the mempool is full.
1105 * When cache is enabled, this function has to browse the length of all
1106 * lcores, so it should not be used in a data path, but only for debug
1110 * A pointer to the mempool structure.
1112 * - 1: The mempool is full.
1113 * - 0: The mempool is not full.
1116 rte_mempool_full(const struct rte_mempool *mp)
1118 return !!(rte_mempool_count(mp) == mp->size);
1122 * Test if the mempool is empty.
1124 * When cache is enabled, this function has to browse the length of all
1125 * lcores, so it should not be used in a data path, but only for debug
1129 * A pointer to the mempool structure.
1131 * - 1: The mempool is empty.
1132 * - 0: The mempool is not empty.
1135 rte_mempool_empty(const struct rte_mempool *mp)
1137 return !!(rte_mempool_count(mp) == 0);
1141 * Return the physical address of elt, which is an element of the pool mp.
1144 * A pointer to the mempool structure.
1146 * A pointer (virtual address) to the element of the pool.
1148 * The physical address of the elt element.
1150 static inline phys_addr_t
1151 rte_mempool_virt2phy(__rte_unused const struct rte_mempool *mp, const void *elt)
1153 const struct rte_mempool_objhdr *hdr;
1154 hdr = (const struct rte_mempool_objhdr *)RTE_PTR_SUB(elt,
1156 return hdr->physaddr;
1160 * Check the consistency of mempool objects.
1162 * Verify the coherency of fields in the mempool structure. Also check
1163 * that the cookies of mempool objects (even the ones that are not
1164 * present in pool) have a correct value. If not, a panic will occur.
1167 * A pointer to the mempool structure.
1169 void rte_mempool_audit(struct rte_mempool *mp);
1172 * Return a pointer to the private data in an mempool structure.
1175 * A pointer to the mempool structure.
1177 * A pointer to the private data.
1179 static inline void *rte_mempool_get_priv(struct rte_mempool *mp)
1182 MEMPOOL_HEADER_SIZE(mp, mp->cache_size);
1186 * Dump the status of all mempools on the console
1189 * A pointer to a file for output
1191 void rte_mempool_list_dump(FILE *f);
1194 * Search a mempool from its name
1197 * The name of the mempool.
1199 * The pointer to the mempool matching the name, or NULL if not found.
1201 * with rte_errno set appropriately. Possible rte_errno values include:
1202 * - ENOENT - required entry not available to return.
1205 struct rte_mempool *rte_mempool_lookup(const char *name);
1208 * Get the header, trailer and total size of a mempool element.
1210 * Given a desired size of the mempool element and mempool flags,
1211 * calculates header, trailer, body and total sizes of the mempool object.
1214 * The size of each element, without header and trailer.
1216 * The flags used for the mempool creation.
1217 * Consult rte_mempool_create() for more information about possible values.
1218 * The size of each element.
1220 * The calculated detailed size the mempool object. May be NULL.
1222 * Total size of the mempool object.
1224 uint32_t rte_mempool_calc_obj_size(uint32_t elt_size, uint32_t flags,
1225 struct rte_mempool_objsz *sz);
1228 * Get the size of memory required to store mempool elements.
1230 * Calculate the maximum amount of memory required to store given number
1231 * of objects. Assume that the memory buffer will be aligned at page
1234 * Note that if object size is bigger then page size, then it assumes
1235 * that pages are grouped in subsets of physically continuous pages big
1236 * enough to store at least one object.
1239 * Number of elements.
1240 * @param total_elt_sz
1241 * The size of each element, including header and trailer, as returned
1242 * by rte_mempool_calc_obj_size().
1244 * LOG2 of the physical pages size. If set to 0, ignore page boundaries.
1246 * Required memory size aligned at page boundary.
1248 size_t rte_mempool_xmem_size(uint32_t elt_num, size_t total_elt_sz,
1252 * Get the size of memory required to store mempool elements.
1254 * Calculate how much memory would be actually required with the given
1255 * memory footprint to store required number of objects.
1258 * Virtual address of the externally allocated memory buffer.
1259 * Will be used to store mempool objects.
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 * Array of physical addresses of the pages that comprises given memory
1269 * Number of elements in the paddr array.
1271 * LOG2 of the physical pages size.
1273 * On success, the number of bytes needed to store given number of
1274 * objects, aligned to the given page size. If the provided memory
1275 * buffer is too small, return a negative value whose absolute value
1276 * is the actual number of elements that can be stored in that buffer.
1278 ssize_t rte_mempool_xmem_usage(void *vaddr, uint32_t elt_num,
1279 size_t total_elt_sz, const phys_addr_t paddr[], uint32_t pg_num,
1283 * Walk list of all memory pools
1288 * Argument passed to iterator
1290 void rte_mempool_walk(void (*func)(struct rte_mempool *, void *arg),
1297 #endif /* _RTE_MEMPOOL_H_ */