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. When debug
154 * is enabled, a cookie is also added in this structure preventing
155 * corruptions and double-frees.
157 struct rte_mempool_objhdr {
158 struct rte_mempool *mp; /**< The mempool owning the object. */
159 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
160 uint64_t cookie; /**< Debug cookie. */
165 * Mempool object trailer structure
167 * In debug mode, each object stored in mempools are suffixed by this
168 * trailer structure containing a cookie preventing memory corruptions.
170 struct rte_mempool_objtlr {
171 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
172 uint64_t cookie; /**< Debug cookie. */
177 * The RTE mempool structure.
180 char name[RTE_MEMPOOL_NAMESIZE]; /**< Name of mempool. */
181 struct rte_ring *ring; /**< Ring to store objects. */
182 phys_addr_t phys_addr; /**< Phys. addr. of mempool struct. */
183 int flags; /**< Flags of the mempool. */
184 uint32_t size; /**< Size of the mempool. */
185 uint32_t cache_size; /**< Size of per-lcore local cache. */
186 uint32_t cache_flushthresh;
187 /**< Threshold before we flush excess elements. */
189 uint32_t elt_size; /**< Size of an element. */
190 uint32_t header_size; /**< Size of header (before elt). */
191 uint32_t trailer_size; /**< Size of trailer (after elt). */
193 unsigned private_data_size; /**< Size of private data. */
195 struct rte_mempool_cache *local_cache; /**< Per-lcore local cache */
197 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
198 /** Per-lcore statistics. */
199 struct rte_mempool_debug_stats stats[RTE_MAX_LCORE];
202 /* Address translation support, starts from next cache line. */
204 /** Number of elements in the elt_pa array. */
205 uint32_t pg_num __rte_cache_aligned;
206 uint32_t pg_shift; /**< LOG2 of the physical pages. */
207 uintptr_t pg_mask; /**< physical page mask value. */
208 uintptr_t elt_va_start;
209 /**< Virtual address of the first mempool object. */
210 uintptr_t elt_va_end;
211 /**< Virtual address of the <size + 1> mempool object. */
212 phys_addr_t elt_pa[MEMPOOL_PG_NUM_DEFAULT];
213 /**< Array of physical page addresses for the mempool objects buffer. */
215 } __rte_cache_aligned;
217 #define MEMPOOL_F_NO_SPREAD 0x0001 /**< Do not spread in memory. */
218 #define MEMPOOL_F_NO_CACHE_ALIGN 0x0002 /**< Do not align objs on cache lines.*/
219 #define MEMPOOL_F_SP_PUT 0x0004 /**< Default put is "single-producer".*/
220 #define MEMPOOL_F_SC_GET 0x0008 /**< Default get is "single-consumer".*/
223 * @internal When debug is enabled, store some statistics.
226 * Pointer to the memory pool.
228 * Name of the statistics field to increment in the memory pool.
230 * Number to add to the object-oriented statistics.
232 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
233 #define __MEMPOOL_STAT_ADD(mp, name, n) do { \
234 unsigned __lcore_id = rte_lcore_id(); \
235 if (__lcore_id < RTE_MAX_LCORE) { \
236 mp->stats[__lcore_id].name##_objs += n; \
237 mp->stats[__lcore_id].name##_bulk += 1; \
241 #define __MEMPOOL_STAT_ADD(mp, name, n) do {} while(0)
245 * Size of elt_pa array size based on number of pages. (Internal use)
247 #define __PA_SIZE(mp, pgn) \
248 RTE_ALIGN_CEIL((((pgn) - RTE_DIM((mp)->elt_pa)) * \
249 sizeof((mp)->elt_pa[0])), RTE_CACHE_LINE_SIZE)
252 * Calculate the size of the mempool header.
255 * Pointer to the memory pool.
257 * Number of pages used to store mempool objects.
259 * Size of the per-lcore cache.
261 #define MEMPOOL_HEADER_SIZE(mp, pgn, cs) \
262 (sizeof(*(mp)) + __PA_SIZE(mp, pgn) + (((cs) == 0) ? 0 : \
263 (sizeof(struct rte_mempool_cache) * RTE_MAX_LCORE)))
266 * Return true if the whole mempool is in contiguous memory.
268 #define MEMPOOL_IS_CONTIG(mp) \
269 ((mp)->pg_num == MEMPOOL_PG_NUM_DEFAULT && \
270 (mp)->phys_addr == (mp)->elt_pa[0])
272 /* return the header of a mempool object (internal) */
273 static inline struct rte_mempool_objhdr *__mempool_get_header(void *obj)
275 return (struct rte_mempool_objhdr *)RTE_PTR_SUB(obj, sizeof(struct rte_mempool_objhdr));
279 * Return a pointer to the mempool owning this object.
282 * An object that is owned by a pool. If this is not the case,
283 * the behavior is undefined.
285 * A pointer to the mempool structure.
287 static inline struct rte_mempool *rte_mempool_from_obj(void *obj)
289 struct rte_mempool_objhdr *hdr = __mempool_get_header(obj);
293 /* return the trailer of a mempool object (internal) */
294 static inline struct rte_mempool_objtlr *__mempool_get_trailer(void *obj)
296 struct rte_mempool *mp = rte_mempool_from_obj(obj);
297 return (struct rte_mempool_objtlr *)RTE_PTR_ADD(obj, mp->elt_size);
301 * @internal Check and update cookies or panic.
304 * Pointer to the memory pool.
305 * @param obj_table_const
306 * Pointer to a table of void * pointers (objects).
308 * Index of object in object table.
310 * - 0: object is supposed to be allocated, mark it as free
311 * - 1: object is supposed to be free, mark it as allocated
312 * - 2: just check that cookie is valid (free or allocated)
314 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
315 #ifndef __INTEL_COMPILER
316 #pragma GCC diagnostic ignored "-Wcast-qual"
318 static inline void __mempool_check_cookies(const struct rte_mempool *mp,
319 void * const *obj_table_const,
320 unsigned n, int free)
322 struct rte_mempool_objhdr *hdr;
323 struct rte_mempool_objtlr *tlr;
329 /* Force to drop the "const" attribute. This is done only when
330 * DEBUG is enabled */
331 tmp = (void *) obj_table_const;
332 obj_table = (void **) tmp;
337 if (rte_mempool_from_obj(obj) != mp)
338 rte_panic("MEMPOOL: object is owned by another "
341 hdr = __mempool_get_header(obj);
342 cookie = hdr->cookie;
345 if (cookie != RTE_MEMPOOL_HEADER_COOKIE1) {
346 rte_log_set_history(0);
347 RTE_LOG(CRIT, MEMPOOL,
348 "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
349 obj, (const void *) mp, cookie);
350 rte_panic("MEMPOOL: bad header cookie (put)\n");
352 hdr->cookie = RTE_MEMPOOL_HEADER_COOKIE2;
354 else if (free == 1) {
355 if (cookie != RTE_MEMPOOL_HEADER_COOKIE2) {
356 rte_log_set_history(0);
357 RTE_LOG(CRIT, MEMPOOL,
358 "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
359 obj, (const void *) mp, cookie);
360 rte_panic("MEMPOOL: bad header cookie (get)\n");
362 hdr->cookie = RTE_MEMPOOL_HEADER_COOKIE1;
364 else if (free == 2) {
365 if (cookie != RTE_MEMPOOL_HEADER_COOKIE1 &&
366 cookie != RTE_MEMPOOL_HEADER_COOKIE2) {
367 rte_log_set_history(0);
368 RTE_LOG(CRIT, MEMPOOL,
369 "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
370 obj, (const void *) mp, cookie);
371 rte_panic("MEMPOOL: bad header cookie (audit)\n");
374 tlr = __mempool_get_trailer(obj);
375 cookie = tlr->cookie;
376 if (cookie != RTE_MEMPOOL_TRAILER_COOKIE) {
377 rte_log_set_history(0);
378 RTE_LOG(CRIT, MEMPOOL,
379 "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
380 obj, (const void *) mp, cookie);
381 rte_panic("MEMPOOL: bad trailer cookie\n");
385 #ifndef __INTEL_COMPILER
386 #pragma GCC diagnostic error "-Wcast-qual"
389 #define __mempool_check_cookies(mp, obj_table_const, n, free) do {} while(0)
390 #endif /* RTE_LIBRTE_MEMPOOL_DEBUG */
393 * A mempool object iterator callback function.
395 typedef void (*rte_mempool_obj_iter_t)(void * /*obj_iter_arg*/,
396 void * /*obj_start*/,
398 uint32_t /*obj_index */);
401 * Call a function for each mempool object in a memory chunk
403 * Iterate across objects of the given size and alignment in the
404 * provided chunk of memory. The given memory buffer can consist of
405 * disjointed physical pages.
407 * For each object, call the provided callback (if any). This function
408 * is used to populate a mempool, or walk through all the elements of a
409 * mempool, or estimate how many elements of the given size could be
410 * created in the given memory buffer.
413 * Virtual address of the memory buffer.
415 * Maximum number of objects to iterate through.
417 * Size of each object.
419 * Alignment of each object.
421 * Array of physical addresses of the pages that comprises given memory
424 * Number of elements in the paddr array.
426 * LOG2 of the physical pages size.
428 * Object iterator callback function (could be NULL).
429 * @param obj_iter_arg
430 * User defined parameter for the object iterator callback function.
433 * Number of objects iterated through.
435 uint32_t rte_mempool_obj_iter(void *vaddr,
436 uint32_t elt_num, size_t elt_sz, size_t align,
437 const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift,
438 rte_mempool_obj_iter_t obj_iter, void *obj_iter_arg);
441 * An object constructor callback function for mempool.
443 * Arguments are the mempool, the opaque pointer given by the user in
444 * rte_mempool_create(), the pointer to the element and the index of
445 * the element in the pool.
447 typedef void (rte_mempool_obj_ctor_t)(struct rte_mempool *, void *,
451 * A mempool constructor callback function.
453 * Arguments are the mempool and the opaque pointer given by the user in
454 * rte_mempool_create().
456 typedef void (rte_mempool_ctor_t)(struct rte_mempool *, void *);
459 * Create a new mempool named *name* in memory.
461 * This function uses ``memzone_reserve()`` to allocate memory. The
462 * pool contains n elements of elt_size. Its size is set to n.
463 * All elements of the mempool are allocated together with the mempool header,
464 * in one physically continuous chunk of memory.
467 * The name of the mempool.
469 * The number of elements in the mempool. The optimum size (in terms of
470 * memory usage) for a mempool is when n is a power of two minus one:
473 * The size of each element.
475 * If cache_size is non-zero, the rte_mempool library will try to
476 * limit the accesses to the common lockless pool, by maintaining a
477 * per-lcore object cache. This argument must be lower or equal to
478 * CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE and n / 1.5. It is advised to choose
479 * cache_size to have "n modulo cache_size == 0": if this is
480 * not the case, some elements will always stay in the pool and will
481 * never be used. The access to the per-lcore table is of course
482 * faster than the multi-producer/consumer pool. The cache can be
483 * disabled if the cache_size argument is set to 0; it can be useful to
484 * avoid losing objects in cache. Note that even if not used, the
485 * memory space for cache is always reserved in a mempool structure,
486 * except if CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE is set to 0.
487 * @param private_data_size
488 * The size of the private data appended after the mempool
489 * structure. This is useful for storing some private data after the
490 * mempool structure, as is done for rte_mbuf_pool for example.
492 * A function pointer that is called for initialization of the pool,
493 * before object initialization. The user can initialize the private
494 * data in this function if needed. This parameter can be NULL if
497 * An opaque pointer to data that can be used in the mempool
498 * constructor function.
500 * A function pointer that is called for each object at
501 * initialization of the pool. The user can set some meta data in
502 * objects if needed. This parameter can be NULL if not needed.
503 * The obj_init() function takes the mempool pointer, the init_arg,
504 * the object pointer and the object number as parameters.
505 * @param obj_init_arg
506 * An opaque pointer to data that can be used as an argument for
507 * each call to the object constructor function.
509 * The *socket_id* argument is the socket identifier in the case of
510 * NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
511 * constraint for the reserved zone.
513 * The *flags* arguments is an OR of following flags:
514 * - MEMPOOL_F_NO_SPREAD: By default, objects addresses are spread
515 * between channels in RAM: the pool allocator will add padding
516 * between objects depending on the hardware configuration. See
517 * Memory alignment constraints for details. If this flag is set,
518 * the allocator will just align them to a cache line.
519 * - MEMPOOL_F_NO_CACHE_ALIGN: By default, the returned objects are
520 * cache-aligned. This flag removes this constraint, and no
521 * padding will be present between objects. This flag implies
522 * MEMPOOL_F_NO_SPREAD.
523 * - MEMPOOL_F_SP_PUT: If this flag is set, the default behavior
524 * when using rte_mempool_put() or rte_mempool_put_bulk() is
525 * "single-producer". Otherwise, it is "multi-producers".
526 * - MEMPOOL_F_SC_GET: If this flag is set, the default behavior
527 * when using rte_mempool_get() or rte_mempool_get_bulk() is
528 * "single-consumer". Otherwise, it is "multi-consumers".
530 * The pointer to the new allocated mempool, on success. NULL on error
531 * with rte_errno set appropriately. Possible rte_errno values include:
532 * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
533 * - E_RTE_SECONDARY - function was called from a secondary process instance
534 * - EINVAL - cache size provided is too large
535 * - ENOSPC - the maximum number of memzones has already been allocated
536 * - EEXIST - a memzone with the same name already exists
537 * - ENOMEM - no appropriate memory area found in which to create memzone
540 rte_mempool_create(const char *name, unsigned n, unsigned elt_size,
541 unsigned cache_size, unsigned private_data_size,
542 rte_mempool_ctor_t *mp_init, void *mp_init_arg,
543 rte_mempool_obj_ctor_t *obj_init, void *obj_init_arg,
544 int socket_id, unsigned flags);
547 * Create a new mempool named *name* in memory.
549 * This function uses ``memzone_reserve()`` to allocate memory. The
550 * pool contains n elements of elt_size. Its size is set to n.
551 * Depending on the input parameters, mempool elements can be either allocated
552 * together with the mempool header, or an externally provided memory buffer
553 * could be used to store mempool objects. In later case, that external
554 * memory buffer can consist of set of disjoint physical pages.
557 * The name of the mempool.
559 * The number of elements in the mempool. The optimum size (in terms of
560 * memory usage) for a mempool is when n is a power of two minus one:
563 * The size of each element.
565 * If cache_size is non-zero, the rte_mempool library will try to
566 * limit the accesses to the common lockless pool, by maintaining a
567 * per-lcore object cache. This argument must be lower or equal to
568 * CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE. It is advised to choose
569 * cache_size to have "n modulo cache_size == 0": if this is
570 * not the case, some elements will always stay in the pool and will
571 * never be used. The access to the per-lcore table is of course
572 * faster than the multi-producer/consumer pool. The cache can be
573 * disabled if the cache_size argument is set to 0; it can be useful to
574 * avoid losing objects in cache. Note that even if not used, the
575 * memory space for cache is always reserved in a mempool structure,
576 * except if CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE is set to 0.
577 * @param private_data_size
578 * The size of the private data appended after the mempool
579 * structure. This is useful for storing some private data after the
580 * mempool structure, as is done for rte_mbuf_pool for example.
582 * A function pointer that is called for initialization of the pool,
583 * before object initialization. The user can initialize the private
584 * data in this function if needed. This parameter can be NULL if
587 * An opaque pointer to data that can be used in the mempool
588 * constructor function.
590 * A function pointer that is called for each object at
591 * initialization of the pool. The user can set some meta data in
592 * objects if needed. This parameter can be NULL if not needed.
593 * The obj_init() function takes the mempool pointer, the init_arg,
594 * the object pointer and the object number as parameters.
595 * @param obj_init_arg
596 * An opaque pointer to data that can be used as an argument for
597 * each call to the object constructor function.
599 * The *socket_id* argument is the socket identifier in the case of
600 * NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
601 * constraint for the reserved zone.
603 * The *flags* arguments is an OR of following flags:
604 * - MEMPOOL_F_NO_SPREAD: By default, objects addresses are spread
605 * between channels in RAM: the pool allocator will add padding
606 * between objects depending on the hardware configuration. See
607 * Memory alignment constraints for details. If this flag is set,
608 * the allocator will just align them to a cache line.
609 * - MEMPOOL_F_NO_CACHE_ALIGN: By default, the returned objects are
610 * cache-aligned. This flag removes this constraint, and no
611 * padding will be present between objects. This flag implies
612 * MEMPOOL_F_NO_SPREAD.
613 * - MEMPOOL_F_SP_PUT: If this flag is set, the default behavior
614 * when using rte_mempool_put() or rte_mempool_put_bulk() is
615 * "single-producer". Otherwise, it is "multi-producers".
616 * - MEMPOOL_F_SC_GET: If this flag is set, the default behavior
617 * when using rte_mempool_get() or rte_mempool_get_bulk() is
618 * "single-consumer". Otherwise, it is "multi-consumers".
620 * Virtual address of the externally allocated memory buffer.
621 * Will be used to store mempool objects.
623 * Array of physical addresses of the pages that comprises given memory
626 * Number of elements in the paddr array.
628 * LOG2 of the physical pages size.
630 * The pointer to the new allocated mempool, on success. NULL on error
631 * with rte_errno set appropriately. Possible rte_errno values include:
632 * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
633 * - E_RTE_SECONDARY - function was called from a secondary process instance
634 * - EINVAL - cache size provided is too large
635 * - ENOSPC - the maximum number of memzones has already been allocated
636 * - EEXIST - a memzone with the same name already exists
637 * - ENOMEM - no appropriate memory area found in which to create memzone
640 rte_mempool_xmem_create(const char *name, unsigned n, unsigned elt_size,
641 unsigned cache_size, unsigned private_data_size,
642 rte_mempool_ctor_t *mp_init, void *mp_init_arg,
643 rte_mempool_obj_ctor_t *obj_init, void *obj_init_arg,
644 int socket_id, unsigned flags, void *vaddr,
645 const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift);
648 * Create a new mempool named *name* in memory on Xen Dom0.
650 * This function uses ``rte_mempool_xmem_create()`` to allocate memory. The
651 * pool contains n elements of elt_size. Its size is set to n.
652 * All elements of the mempool are allocated together with the mempool header,
653 * and memory buffer can consist of set of disjoint physical pages.
656 * The name of the mempool.
658 * The number of elements in the mempool. The optimum size (in terms of
659 * memory usage) for a mempool is when n is a power of two minus one:
662 * The size of each element.
664 * If cache_size is non-zero, the rte_mempool library will try to
665 * limit the accesses to the common lockless pool, by maintaining a
666 * per-lcore object cache. This argument must be lower or equal to
667 * CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE. It is advised to choose
668 * cache_size to have "n modulo cache_size == 0": if this is
669 * not the case, some elements will always stay in the pool and will
670 * never be used. The access to the per-lcore table is of course
671 * faster than the multi-producer/consumer pool. The cache can be
672 * disabled if the cache_size argument is set to 0; it can be useful to
673 * avoid losing objects in cache. Note that even if not used, the
674 * memory space for cache is always reserved in a mempool structure,
675 * except if CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE is set to 0.
676 * @param private_data_size
677 * The size of the private data appended after the mempool
678 * structure. This is useful for storing some private data after the
679 * mempool structure, as is done for rte_mbuf_pool for example.
681 * A function pointer that is called for initialization of the pool,
682 * before object initialization. The user can initialize the private
683 * data in this function if needed. This parameter can be NULL if
686 * An opaque pointer to data that can be used in the mempool
687 * constructor function.
689 * A function pointer that is called for each object at
690 * initialization of the pool. The user can set some meta data in
691 * objects if needed. This parameter can be NULL if not needed.
692 * The obj_init() function takes the mempool pointer, the init_arg,
693 * the object pointer and the object number as parameters.
694 * @param obj_init_arg
695 * An opaque pointer to data that can be used as an argument for
696 * each call to the object constructor function.
698 * The *socket_id* argument is the socket identifier in the case of
699 * NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
700 * constraint for the reserved zone.
702 * The *flags* arguments is an OR of following flags:
703 * - MEMPOOL_F_NO_SPREAD: By default, objects addresses are spread
704 * between channels in RAM: the pool allocator will add padding
705 * between objects depending on the hardware configuration. See
706 * Memory alignment constraints for details. If this flag is set,
707 * the allocator will just align them to a cache line.
708 * - MEMPOOL_F_NO_CACHE_ALIGN: By default, the returned objects are
709 * cache-aligned. This flag removes this constraint, and no
710 * padding will be present between objects. This flag implies
711 * MEMPOOL_F_NO_SPREAD.
712 * - MEMPOOL_F_SP_PUT: If this flag is set, the default behavior
713 * when using rte_mempool_put() or rte_mempool_put_bulk() is
714 * "single-producer". Otherwise, it is "multi-producers".
715 * - MEMPOOL_F_SC_GET: If this flag is set, the default behavior
716 * when using rte_mempool_get() or rte_mempool_get_bulk() is
717 * "single-consumer". Otherwise, it is "multi-consumers".
719 * The pointer to the new allocated mempool, on success. NULL on error
720 * with rte_errno set appropriately. Possible rte_errno values include:
721 * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
722 * - E_RTE_SECONDARY - function was called from a secondary process instance
723 * - EINVAL - cache size provided is too large
724 * - ENOSPC - the maximum number of memzones has already been allocated
725 * - EEXIST - a memzone with the same name already exists
726 * - ENOMEM - no appropriate memory area found in which to create memzone
729 rte_dom0_mempool_create(const char *name, unsigned n, unsigned elt_size,
730 unsigned cache_size, unsigned private_data_size,
731 rte_mempool_ctor_t *mp_init, void *mp_init_arg,
732 rte_mempool_obj_ctor_t *obj_init, void *obj_init_arg,
733 int socket_id, unsigned flags);
737 * Dump the status of the mempool to the console.
740 * A pointer to a file for output
742 * A pointer to the mempool structure.
744 void rte_mempool_dump(FILE *f, const struct rte_mempool *mp);
747 * @internal Put several objects back in the mempool; used internally.
749 * A pointer to the mempool structure.
751 * A pointer to a table of void * pointers (objects).
753 * The number of objects to store back in the mempool, must be strictly
756 * Mono-producer (0) or multi-producers (1).
758 static inline void __attribute__((always_inline))
759 __mempool_put_bulk(struct rte_mempool *mp, void * const *obj_table,
760 unsigned n, int is_mp)
762 struct rte_mempool_cache *cache;
765 unsigned lcore_id = rte_lcore_id();
766 uint32_t cache_size = mp->cache_size;
767 uint32_t flushthresh = mp->cache_flushthresh;
769 /* increment stat now, adding in mempool always success */
770 __MEMPOOL_STAT_ADD(mp, put, n);
772 /* cache is not enabled or single producer or non-EAL thread */
773 if (unlikely(cache_size == 0 || is_mp == 0 ||
774 lcore_id >= RTE_MAX_LCORE))
777 /* Go straight to ring if put would overflow mem allocated for cache */
778 if (unlikely(n > RTE_MEMPOOL_CACHE_MAX_SIZE))
781 cache = &mp->local_cache[lcore_id];
782 cache_objs = &cache->objs[cache->len];
785 * The cache follows the following algorithm
786 * 1. Add the objects to the cache
787 * 2. Anything greater than the cache min value (if it crosses the
788 * cache flush threshold) is flushed to the ring.
791 /* Add elements back into the cache */
792 for (index = 0; index < n; ++index, obj_table++)
793 cache_objs[index] = *obj_table;
797 if (cache->len >= flushthresh) {
798 rte_ring_mp_enqueue_bulk(mp->ring, &cache->objs[cache_size],
799 cache->len - cache_size);
800 cache->len = cache_size;
807 /* push remaining objects in ring */
808 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
810 if (rte_ring_mp_enqueue_bulk(mp->ring, obj_table, n) < 0)
811 rte_panic("cannot put objects in mempool\n");
814 if (rte_ring_sp_enqueue_bulk(mp->ring, obj_table, n) < 0)
815 rte_panic("cannot put objects in mempool\n");
819 rte_ring_mp_enqueue_bulk(mp->ring, obj_table, n);
821 rte_ring_sp_enqueue_bulk(mp->ring, obj_table, n);
827 * Put several objects back in the mempool (multi-producers safe).
830 * A pointer to the mempool structure.
832 * A pointer to a table of void * pointers (objects).
834 * The number of objects to add in the mempool from the obj_table.
836 static inline void __attribute__((always_inline))
837 rte_mempool_mp_put_bulk(struct rte_mempool *mp, void * const *obj_table,
840 __mempool_check_cookies(mp, obj_table, n, 0);
841 __mempool_put_bulk(mp, obj_table, n, 1);
845 * Put several objects back in the mempool (NOT multi-producers safe).
848 * A pointer to the mempool structure.
850 * A pointer to a table of void * pointers (objects).
852 * The number of objects to add in the mempool from obj_table.
855 rte_mempool_sp_put_bulk(struct rte_mempool *mp, void * const *obj_table,
858 __mempool_check_cookies(mp, obj_table, n, 0);
859 __mempool_put_bulk(mp, obj_table, n, 0);
863 * Put several objects back in the mempool.
865 * This function calls the multi-producer or the single-producer
866 * version depending on the default behavior that was specified at
867 * mempool creation time (see flags).
870 * A pointer to the mempool structure.
872 * A pointer to a table of void * pointers (objects).
874 * The number of objects to add in the mempool from obj_table.
876 static inline void __attribute__((always_inline))
877 rte_mempool_put_bulk(struct rte_mempool *mp, void * const *obj_table,
880 __mempool_check_cookies(mp, obj_table, n, 0);
881 __mempool_put_bulk(mp, obj_table, n, !(mp->flags & MEMPOOL_F_SP_PUT));
885 * Put one object in the mempool (multi-producers safe).
888 * A pointer to the mempool structure.
890 * A pointer to the object to be added.
892 static inline void __attribute__((always_inline))
893 rte_mempool_mp_put(struct rte_mempool *mp, void *obj)
895 rte_mempool_mp_put_bulk(mp, &obj, 1);
899 * Put one object back in the mempool (NOT multi-producers safe).
902 * A pointer to the mempool structure.
904 * A pointer to the object to be added.
906 static inline void __attribute__((always_inline))
907 rte_mempool_sp_put(struct rte_mempool *mp, void *obj)
909 rte_mempool_sp_put_bulk(mp, &obj, 1);
913 * Put one object back in the mempool.
915 * This function calls the multi-producer or the single-producer
916 * version depending on the default behavior that was specified at
917 * mempool creation time (see flags).
920 * A pointer to the mempool structure.
922 * A pointer to the object to be added.
924 static inline void __attribute__((always_inline))
925 rte_mempool_put(struct rte_mempool *mp, void *obj)
927 rte_mempool_put_bulk(mp, &obj, 1);
931 * @internal Get several objects from the mempool; used internally.
933 * A pointer to the mempool structure.
935 * A pointer to a table of void * pointers (objects).
937 * The number of objects to get, must be strictly positive.
939 * Mono-consumer (0) or multi-consumers (1).
941 * - >=0: Success; number of objects supplied.
942 * - <0: Error; code of ring dequeue function.
944 static inline int __attribute__((always_inline))
945 __mempool_get_bulk(struct rte_mempool *mp, void **obj_table,
946 unsigned n, int is_mc)
949 struct rte_mempool_cache *cache;
952 unsigned lcore_id = rte_lcore_id();
953 uint32_t cache_size = mp->cache_size;
955 /* cache is not enabled or single consumer */
956 if (unlikely(cache_size == 0 || is_mc == 0 ||
957 n >= cache_size || lcore_id >= RTE_MAX_LCORE))
960 cache = &mp->local_cache[lcore_id];
961 cache_objs = cache->objs;
963 /* Can this be satisfied from the cache? */
964 if (cache->len < n) {
965 /* No. Backfill the cache first, and then fill from it */
966 uint32_t req = n + (cache_size - cache->len);
968 /* How many do we require i.e. number to fill the cache + the request */
969 ret = rte_ring_mc_dequeue_bulk(mp->ring, &cache->objs[cache->len], req);
970 if (unlikely(ret < 0)) {
972 * In the offchance that we are buffer constrained,
973 * where we are not able to allocate cache + n, go to
974 * the ring directly. If that fails, we are truly out of
983 /* Now fill in the response ... */
984 for (index = 0, len = cache->len - 1; index < n; ++index, len--, obj_table++)
985 *obj_table = cache_objs[len];
989 __MEMPOOL_STAT_ADD(mp, get_success, n);
995 /* get remaining objects from ring */
997 ret = rte_ring_mc_dequeue_bulk(mp->ring, obj_table, n);
999 ret = rte_ring_sc_dequeue_bulk(mp->ring, obj_table, n);
1002 __MEMPOOL_STAT_ADD(mp, get_fail, n);
1004 __MEMPOOL_STAT_ADD(mp, get_success, n);
1010 * Get several objects from the mempool (multi-consumers safe).
1012 * If cache is enabled, objects will be retrieved first from cache,
1013 * subsequently from the common pool. Note that it can return -ENOENT when
1014 * the local cache and common pool are empty, even if cache from other
1018 * A pointer to the mempool structure.
1020 * A pointer to a table of void * pointers (objects) that will be filled.
1022 * The number of objects to get from mempool to obj_table.
1024 * - 0: Success; objects taken.
1025 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
1027 static inline int __attribute__((always_inline))
1028 rte_mempool_mc_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned n)
1031 ret = __mempool_get_bulk(mp, obj_table, n, 1);
1033 __mempool_check_cookies(mp, obj_table, n, 1);
1038 * Get several objects from the mempool (NOT multi-consumers safe).
1040 * If cache is enabled, objects will be retrieved first from cache,
1041 * subsequently from the common pool. Note that it can return -ENOENT when
1042 * the local cache and common pool are empty, even if cache from other
1046 * A pointer to the mempool structure.
1048 * A pointer to a table of void * pointers (objects) that will be filled.
1050 * The number of objects to get from the mempool to obj_table.
1052 * - 0: Success; objects taken.
1053 * - -ENOENT: Not enough entries in the mempool; no object is
1056 static inline int __attribute__((always_inline))
1057 rte_mempool_sc_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned n)
1060 ret = __mempool_get_bulk(mp, obj_table, n, 0);
1062 __mempool_check_cookies(mp, obj_table, n, 1);
1067 * Get several objects from the mempool.
1069 * This function calls the multi-consumers or the single-consumer
1070 * version, depending on the default behaviour that was specified at
1071 * mempool creation time (see flags).
1073 * If cache is enabled, objects will be retrieved first from cache,
1074 * subsequently from the common pool. Note that it can return -ENOENT when
1075 * the local cache and common pool are empty, even if cache from other
1079 * A pointer to the mempool structure.
1081 * A pointer to a table of void * pointers (objects) that will be filled.
1083 * The number of objects to get from the mempool to obj_table.
1085 * - 0: Success; objects taken
1086 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
1088 static inline int __attribute__((always_inline))
1089 rte_mempool_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned n)
1092 ret = __mempool_get_bulk(mp, obj_table, n,
1093 !(mp->flags & MEMPOOL_F_SC_GET));
1095 __mempool_check_cookies(mp, obj_table, n, 1);
1100 * Get one object from the mempool (multi-consumers safe).
1102 * If cache is enabled, objects will be retrieved first from cache,
1103 * subsequently from the common pool. Note that it can return -ENOENT when
1104 * the local cache and common pool are empty, even if cache from other
1108 * A pointer to the mempool structure.
1110 * A pointer to a void * pointer (object) that will be filled.
1112 * - 0: Success; objects taken.
1113 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
1115 static inline int __attribute__((always_inline))
1116 rte_mempool_mc_get(struct rte_mempool *mp, void **obj_p)
1118 return rte_mempool_mc_get_bulk(mp, obj_p, 1);
1122 * Get one object from the mempool (NOT multi-consumers safe).
1124 * If cache is enabled, objects will be retrieved first from cache,
1125 * subsequently from the common pool. Note that it can return -ENOENT when
1126 * the local cache and common pool are empty, even if cache from other
1130 * A pointer to the mempool structure.
1132 * A pointer to a void * pointer (object) that will be filled.
1134 * - 0: Success; objects taken.
1135 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
1137 static inline int __attribute__((always_inline))
1138 rte_mempool_sc_get(struct rte_mempool *mp, void **obj_p)
1140 return rte_mempool_sc_get_bulk(mp, obj_p, 1);
1144 * Get one object from the mempool.
1146 * This function calls the multi-consumers or the single-consumer
1147 * version, depending on the default behavior that was specified at
1148 * mempool creation (see flags).
1150 * If cache is enabled, objects will be retrieved first from cache,
1151 * subsequently from the common pool. Note that it can return -ENOENT when
1152 * the local cache and common pool are empty, even if cache from other
1156 * A pointer to the mempool structure.
1158 * A pointer to a void * pointer (object) that will be filled.
1160 * - 0: Success; objects taken.
1161 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
1163 static inline int __attribute__((always_inline))
1164 rte_mempool_get(struct rte_mempool *mp, void **obj_p)
1166 return rte_mempool_get_bulk(mp, obj_p, 1);
1170 * Return the number of entries in the mempool.
1172 * When cache is enabled, this function has to browse the length of
1173 * all lcores, so it should not be used in a data path, but only for
1177 * A pointer to the mempool structure.
1179 * The number of entries in the mempool.
1181 unsigned rte_mempool_count(const struct rte_mempool *mp);
1184 * Return the number of free entries in the mempool ring.
1185 * i.e. how many entries can be freed back to the mempool.
1187 * NOTE: This corresponds to the number of elements *allocated* from the
1188 * memory pool, not the number of elements in the pool itself. To count
1189 * the number elements currently available in the pool, use "rte_mempool_count"
1191 * When cache is enabled, this function has to browse the length of
1192 * all lcores, so it should not be used in a data path, but only for
1196 * A pointer to the mempool structure.
1198 * The number of free entries in the mempool.
1200 static inline unsigned
1201 rte_mempool_free_count(const struct rte_mempool *mp)
1203 return mp->size - rte_mempool_count(mp);
1207 * Test if the mempool is full.
1209 * When cache is enabled, this function has to browse the length of all
1210 * lcores, so it should not be used in a data path, but only for debug
1214 * A pointer to the mempool structure.
1216 * - 1: The mempool is full.
1217 * - 0: The mempool is not full.
1220 rte_mempool_full(const struct rte_mempool *mp)
1222 return !!(rte_mempool_count(mp) == mp->size);
1226 * Test if the mempool is empty.
1228 * When cache is enabled, this function has to browse the length of all
1229 * lcores, so it should not be used in a data path, but only for debug
1233 * A pointer to the mempool structure.
1235 * - 1: The mempool is empty.
1236 * - 0: The mempool is not empty.
1239 rte_mempool_empty(const struct rte_mempool *mp)
1241 return !!(rte_mempool_count(mp) == 0);
1245 * Return the physical address of elt, which is an element of the pool mp.
1248 * A pointer to the mempool structure.
1250 * A pointer (virtual address) to the element of the pool.
1252 * The physical address of the elt element.
1254 static inline phys_addr_t
1255 rte_mempool_virt2phy(const struct rte_mempool *mp, const void *elt)
1257 if (rte_eal_has_hugepages()) {
1260 off = (const char *)elt - (const char *)mp->elt_va_start;
1261 return mp->elt_pa[off >> mp->pg_shift] + (off & mp->pg_mask);
1264 * If huge pages are disabled, we cannot assume the
1265 * memory region to be physically contiguous.
1266 * Lookup for each element.
1268 return rte_mem_virt2phy(elt);
1273 * Check the consistency of mempool objects.
1275 * Verify the coherency of fields in the mempool structure. Also check
1276 * that the cookies of mempool objects (even the ones that are not
1277 * present in pool) have a correct value. If not, a panic will occur.
1280 * A pointer to the mempool structure.
1282 void rte_mempool_audit(const struct rte_mempool *mp);
1285 * Return a pointer to the private data in an mempool structure.
1288 * A pointer to the mempool structure.
1290 * A pointer to the private data.
1292 static inline void *rte_mempool_get_priv(struct rte_mempool *mp)
1295 MEMPOOL_HEADER_SIZE(mp, mp->pg_num, mp->cache_size);
1299 * Dump the status of all mempools on the console
1302 * A pointer to a file for output
1304 void rte_mempool_list_dump(FILE *f);
1307 * Search a mempool from its name
1310 * The name of the mempool.
1312 * The pointer to the mempool matching the name, or NULL if not found.
1314 * with rte_errno set appropriately. Possible rte_errno values include:
1315 * - ENOENT - required entry not available to return.
1318 struct rte_mempool *rte_mempool_lookup(const char *name);
1321 * Get the header, trailer and total size of a mempool element.
1323 * Given a desired size of the mempool element and mempool flags,
1324 * calculates header, trailer, body and total sizes of the mempool object.
1327 * The size of each element.
1329 * The flags used for the mempool creation.
1330 * Consult rte_mempool_create() for more information about possible values.
1331 * The size of each element.
1333 * The calculated detailed size the mempool object. May be NULL.
1335 * Total size of the mempool object.
1337 uint32_t rte_mempool_calc_obj_size(uint32_t elt_size, uint32_t flags,
1338 struct rte_mempool_objsz *sz);
1341 * Get the size of memory required to store mempool elements.
1343 * Calculate the maximum amount of memory required to store given number
1344 * of objects. Assume that the memory buffer will be aligned at page
1347 * Note that if object size is bigger then page size, then it assumes
1348 * that pages are grouped in subsets of physically continuous pages big
1349 * enough to store at least one object.
1352 * Number of elements.
1354 * The size of each element.
1356 * LOG2 of the physical pages size.
1358 * Required memory size aligned at page boundary.
1360 size_t rte_mempool_xmem_size(uint32_t elt_num, size_t elt_sz,
1364 * Get the size of memory required to store mempool elements.
1366 * Calculate how much memory would be actually required with the given
1367 * memory footprint to store required number of objects.
1370 * Virtual address of the externally allocated memory buffer.
1371 * Will be used to store mempool objects.
1373 * Number of elements.
1375 * The size of each element.
1377 * Array of physical addresses of the pages that comprises given memory
1380 * Number of elements in the paddr array.
1382 * LOG2 of the physical pages size.
1384 * On success, the number of bytes needed to store given number of
1385 * objects, aligned to the given page size. If the provided memory
1386 * buffer is too small, return a negative value whose absolute value
1387 * is the actual number of elements that can be stored in that buffer.
1389 ssize_t rte_mempool_xmem_usage(void *vaddr, uint32_t elt_num, size_t elt_sz,
1390 const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift);
1393 * Walk list of all memory pools
1398 * Argument passed to iterator
1400 void rte_mempool_walk(void (*func)(const struct rte_mempool *, void *arg),
1407 #endif /* _RTE_MEMPOOL_H_ */