4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #ifndef _RTE_MEMPOOL_H_
35 #define _RTE_MEMPOOL_H_
41 * A memory pool is an allocator of fixed-size object. It is
42 * identified by its name, and uses a ring to store free objects. It
43 * provides some other optional services, like a per-core object
44 * cache, and an alignment helper to ensure that objects are padded
45 * to spread them equally on all RAM channels, ranks, and so on.
47 * Objects owned by a mempool should never be added in another
48 * mempool. When an object is freed using rte_mempool_put() or
49 * equivalent, the object data is not modified; the user can save some
50 * meta-data in the object data and retrieve them when allocating a
53 * Note: the mempool implementation is not preemptable. A lcore must
54 * not be interrupted by another task that uses the same mempool
55 * (because it uses a ring which is not preemptable). Also, mempool
56 * functions must not be used outside the DPDK environment: for
57 * example, in linuxapp environment, a thread that is not created by
58 * the EAL must not use mempools. This is due to the per-lcore cache
59 * that won't work as rte_lcore_id() will not return a correct value.
67 #include <sys/queue.h>
70 #include <rte_debug.h>
71 #include <rte_lcore.h>
72 #include <rte_memory.h>
73 #include <rte_branch_prediction.h>
80 #define RTE_MEMPOOL_HEADER_COOKIE1 0xbadbadbadadd2e55ULL /**< Header cookie. */
81 #define RTE_MEMPOOL_HEADER_COOKIE2 0xf2eef2eedadd2e55ULL /**< Header cookie. */
82 #define RTE_MEMPOOL_TRAILER_COOKIE 0xadd2e55badbadbadULL /**< Trailer cookie.*/
84 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
86 * A structure that stores the mempool statistics (per-lcore).
88 struct rte_mempool_debug_stats {
89 uint64_t put_bulk; /**< Number of puts. */
90 uint64_t put_objs; /**< Number of objects successfully put. */
91 uint64_t get_success_bulk; /**< Successful allocation number. */
92 uint64_t get_success_objs; /**< Objects successfully allocated. */
93 uint64_t get_fail_bulk; /**< Failed allocation number. */
94 uint64_t get_fail_objs; /**< Objects that failed to be allocated. */
95 } __rte_cache_aligned;
98 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
100 * A structure that stores a per-core object cache.
102 struct rte_mempool_cache {
103 unsigned len; /**< Cache len */
105 * Cache is allocated to this size to allow it to overflow in certain
106 * cases to avoid needless emptying of cache.
108 void *objs[RTE_MEMPOOL_CACHE_MAX_SIZE * 3]; /**< Cache objects */
109 } __rte_cache_aligned;
110 #endif /* RTE_MEMPOOL_CACHE_MAX_SIZE > 0 */
112 struct rte_mempool_objsz {
113 uint32_t elt_size; /**< Size of an element. */
114 uint32_t header_size; /**< Size of header (before elt). */
115 uint32_t trailer_size; /**< Size of trailer (after elt). */
117 /**< Total size of an object (header + elt + trailer). */
120 #define RTE_MEMPOOL_NAMESIZE 32 /**< Maximum length of a memory pool. */
121 #define RTE_MEMPOOL_MZ_PREFIX "MP_"
124 #define RTE_MEMPOOL_MZ_FORMAT RTE_MEMPOOL_MZ_PREFIX "%s"
126 #ifdef RTE_LIBRTE_XEN_DOM0
128 /* "<name>_MP_elt" */
129 #define RTE_MEMPOOL_OBJ_NAME "%s_" RTE_MEMPOOL_MZ_PREFIX "elt"
133 #define RTE_MEMPOOL_OBJ_NAME RTE_MEMPOOL_MZ_FORMAT
135 #endif /* RTE_LIBRTE_XEN_DOM0 */
137 #define MEMPOOL_PG_SHIFT_MAX (sizeof(uintptr_t) * CHAR_BIT - 1)
139 /** Mempool over one chunk of physically continuous memory */
140 #define MEMPOOL_PG_NUM_DEFAULT 1
143 * The RTE mempool structure.
146 char name[RTE_MEMPOOL_NAMESIZE]; /**< Name of mempool. */
147 struct rte_ring *ring; /**< Ring to store objects. */
148 phys_addr_t phys_addr; /**< Phys. addr. of mempool struct. */
149 int flags; /**< Flags of the mempool. */
150 uint32_t size; /**< Size of the mempool. */
151 uint32_t cache_size; /**< Size of per-lcore local cache. */
152 uint32_t cache_flushthresh;
153 /**< Threshold before we flush excess elements. */
155 uint32_t elt_size; /**< Size of an element. */
156 uint32_t header_size; /**< Size of header (before elt). */
157 uint32_t trailer_size; /**< Size of trailer (after elt). */
159 unsigned private_data_size; /**< Size of private data. */
161 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
162 /** Per-lcore local cache. */
163 struct rte_mempool_cache local_cache[RTE_MAX_LCORE];
166 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
167 /** Per-lcore statistics. */
168 struct rte_mempool_debug_stats stats[RTE_MAX_LCORE];
171 /* Address translation support, starts from next cache line. */
173 /** Number of elements in the elt_pa array. */
174 uint32_t pg_num __rte_cache_aligned;
175 uint32_t pg_shift; /**< LOG2 of the physical pages. */
176 uintptr_t pg_mask; /**< physical page mask value. */
177 uintptr_t elt_va_start;
178 /**< Virtual address of the first mempool object. */
179 uintptr_t elt_va_end;
180 /**< Virtual address of the <size + 1> mempool object. */
181 phys_addr_t elt_pa[MEMPOOL_PG_NUM_DEFAULT];
182 /**< Array of physical pages addresses for the mempool objects buffer. */
184 } __rte_cache_aligned;
186 #define MEMPOOL_F_NO_SPREAD 0x0001 /**< Do not spread in memory. */
187 #define MEMPOOL_F_NO_CACHE_ALIGN 0x0002 /**< Do not align objs on cache lines.*/
188 #define MEMPOOL_F_SP_PUT 0x0004 /**< Default put is "single-producer".*/
189 #define MEMPOOL_F_SC_GET 0x0008 /**< Default get is "single-consumer".*/
192 * @internal When debug is enabled, store some statistics.
194 * Pointer to the memory pool.
196 * Name of the statistics field to increment in the memory pool.
198 * Number to add to the object-oriented statistics.
200 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
201 #define __MEMPOOL_STAT_ADD(mp, name, n) do { \
202 unsigned __lcore_id = rte_lcore_id(); \
203 if (__lcore_id < RTE_MAX_LCORE) { \
204 mp->stats[__lcore_id].name##_objs += n; \
205 mp->stats[__lcore_id].name##_bulk += 1; \
209 #define __MEMPOOL_STAT_ADD(mp, name, n) do {} while(0)
213 * Calculates size of the mempool header.
215 * Pointer to the memory pool.
217 * Number of page used to store mempool objects.
219 #define MEMPOOL_HEADER_SIZE(mp, pgn) (sizeof(*(mp)) + \
220 RTE_ALIGN_CEIL(((pgn) - RTE_DIM((mp)->elt_pa)) * \
221 sizeof ((mp)->elt_pa[0]), RTE_CACHE_LINE_SIZE))
224 * Returns TRUE if whole mempool is allocated in one contiguous block of memory.
226 #define MEMPOOL_IS_CONTIG(mp) \
227 ((mp)->pg_num == MEMPOOL_PG_NUM_DEFAULT && \
228 (mp)->phys_addr == (mp)->elt_pa[0])
231 * @internal Get a pointer to a mempool pointer in the object header.
235 * The pointer to the mempool from which the object was allocated.
237 static inline struct rte_mempool **__mempool_from_obj(void *obj)
239 struct rte_mempool **mpp;
242 off = sizeof(struct rte_mempool *);
243 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
244 off += sizeof(uint64_t);
246 mpp = (struct rte_mempool **)((char *)obj - off);
251 * Return a pointer to the mempool owning this object.
254 * An object that is owned by a pool. If this is not the case,
255 * the behavior is undefined.
257 * A pointer to the mempool structure.
259 static inline const struct rte_mempool *rte_mempool_from_obj(void *obj)
261 struct rte_mempool * const *mpp;
262 mpp = __mempool_from_obj(obj);
266 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
267 /* get header cookie value */
268 static inline uint64_t __mempool_read_header_cookie(const void *obj)
270 return *(const uint64_t *)((const char *)obj - sizeof(uint64_t));
273 /* get trailer cookie value */
274 static inline uint64_t __mempool_read_trailer_cookie(void *obj)
276 struct rte_mempool **mpp = __mempool_from_obj(obj);
277 return *(uint64_t *)((char *)obj + (*mpp)->elt_size);
280 /* write header cookie value */
281 static inline void __mempool_write_header_cookie(void *obj, int free)
284 cookie_p = (uint64_t *)((char *)obj - sizeof(uint64_t));
286 *cookie_p = RTE_MEMPOOL_HEADER_COOKIE1;
288 *cookie_p = RTE_MEMPOOL_HEADER_COOKIE2;
292 /* write trailer cookie value */
293 static inline void __mempool_write_trailer_cookie(void *obj)
296 struct rte_mempool **mpp = __mempool_from_obj(obj);
297 cookie_p = (uint64_t *)((char *)obj + (*mpp)->elt_size);
298 *cookie_p = RTE_MEMPOOL_TRAILER_COOKIE;
300 #endif /* RTE_LIBRTE_MEMPOOL_DEBUG */
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 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
317 #ifndef __INTEL_COMPILER
318 #pragma GCC diagnostic ignored "-Wcast-qual"
320 static inline void __mempool_check_cookies(const struct rte_mempool *mp,
321 void * const *obj_table_const,
322 unsigned n, int free)
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 cookie = __mempool_read_header_cookie(obj);
344 if (cookie != RTE_MEMPOOL_HEADER_COOKIE1) {
345 rte_log_set_history(0);
346 RTE_LOG(CRIT, MEMPOOL,
347 "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
349 rte_panic("MEMPOOL: bad header cookie (put)\n");
351 __mempool_write_header_cookie(obj, 1);
353 else if (free == 1) {
354 if (cookie != RTE_MEMPOOL_HEADER_COOKIE2) {
355 rte_log_set_history(0);
356 RTE_LOG(CRIT, MEMPOOL,
357 "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
359 rte_panic("MEMPOOL: bad header cookie (get)\n");
361 __mempool_write_header_cookie(obj, 0);
363 else if (free == 2) {
364 if (cookie != RTE_MEMPOOL_HEADER_COOKIE1 &&
365 cookie != RTE_MEMPOOL_HEADER_COOKIE2) {
366 rte_log_set_history(0);
367 RTE_LOG(CRIT, MEMPOOL,
368 "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
370 rte_panic("MEMPOOL: bad header cookie (audit)\n");
373 cookie = __mempool_read_trailer_cookie(obj);
374 if (cookie != RTE_MEMPOOL_TRAILER_COOKIE) {
375 rte_log_set_history(0);
376 RTE_LOG(CRIT, MEMPOOL,
377 "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
379 rte_panic("MEMPOOL: bad trailer cookie\n");
383 #ifndef __INTEL_COMPILER
384 #pragma GCC diagnostic error "-Wcast-qual"
387 #define __mempool_check_cookies(mp, obj_table_const, n, free) do {} while(0)
388 #endif /* RTE_LIBRTE_MEMPOOL_DEBUG */
391 * An mempool's object iterator callback function.
393 typedef void (*rte_mempool_obj_iter_t)(void * /*obj_iter_arg*/,
394 void * /*obj_start*/,
396 uint32_t /*obj_index */);
399 * Iterates across objects of the given size and alignment in the
400 * provided chunk of memory. The given memory buffer can consist of
401 * disjoint physical pages.
402 * For each object calls the provided callback (if any).
403 * Used to populate mempool, walk through all elements of the mempool,
404 * estimate how many elements of the given size could be created in the given
407 * Virtual address of the memory buffer.
409 * Maximum number of objects to iterate through.
411 * Size of each object.
413 * Array of phyiscall addresses of the pages that comprises given memory
416 * Number of elements in the paddr array.
418 * LOG2 of the physical pages size.
420 * Object iterator callback function (could be NULL).
421 * @param obj_iter_arg
422 * User defined Prameter for the object iterator callback function.
425 * Number of objects iterated through.
428 uint32_t rte_mempool_obj_iter(void *vaddr,
429 uint32_t elt_num, size_t elt_sz, size_t align,
430 const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift,
431 rte_mempool_obj_iter_t obj_iter, void *obj_iter_arg);
434 * An object constructor callback function for mempool.
436 * Arguments are the mempool, the opaque pointer given by the user in
437 * rte_mempool_create(), the pointer to the element and the index of
438 * the element in the pool.
440 typedef void (rte_mempool_obj_ctor_t)(struct rte_mempool *, void *,
444 * A mempool constructor callback function.
446 * Arguments are the mempool and the opaque pointer given by the user in
447 * rte_mempool_create().
449 typedef void (rte_mempool_ctor_t)(struct rte_mempool *, void *);
452 * Creates a new mempool named *name* in memory.
454 * This function uses ``memzone_reserve()`` to allocate memory. The
455 * pool contains n elements of elt_size. Its size is set to n.
456 * All elements of the mempool are allocated together with the mempool header,
457 * in one physically continuous chunk of memory.
460 * The name of the mempool.
462 * The number of elements in the mempool. The optimum size (in terms of
463 * memory usage) for a mempool is when n is a power of two minus one:
466 * The size of each element.
468 * If cache_size is non-zero, the rte_mempool library will try to
469 * limit the accesses to the common lockless pool, by maintaining a
470 * per-lcore object cache. This argument must be lower or equal to
471 * CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE. It is advised to choose
472 * cache_size to have "n modulo cache_size == 0": if this is
473 * not the case, some elements will always stay in the pool and will
474 * never be used. The access to the per-lcore table is of course
475 * faster than the multi-producer/consumer pool. The cache can be
476 * disabled if the cache_size argument is set to 0; it can be useful to
477 * avoid losing objects in cache. Note that even if not used, the
478 * memory space for cache is always reserved in a mempool structure,
479 * except if CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE is set to 0.
480 * @param private_data_size
481 * The size of the private data appended after the mempool
482 * structure. This is useful for storing some private data after the
483 * mempool structure, as is done for rte_mbuf_pool for example.
485 * A function pointer that is called for initialization of the pool,
486 * before object initialization. The user can initialize the private
487 * data in this function if needed. This parameter can be NULL if
490 * An opaque pointer to data that can be used in the mempool
491 * constructor function.
493 * A function pointer that is called for each object at
494 * initialization of the pool. The user can set some meta data in
495 * objects if needed. This parameter can be NULL if not needed.
496 * The obj_init() function takes the mempool pointer, the init_arg,
497 * the object pointer and the object number as parameters.
498 * @param obj_init_arg
499 * An opaque pointer to data that can be used as an argument for
500 * each call to the object constructor function.
502 * The *socket_id* argument is the socket identifier in the case of
503 * NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
504 * constraint for the reserved zone.
506 * The *flags* arguments is an OR of following flags:
507 * - MEMPOOL_F_NO_SPREAD: By default, objects addresses are spread
508 * between channels in RAM: the pool allocator will add padding
509 * between objects depending on the hardware configuration. See
510 * Memory alignment constraints for details. If this flag is set,
511 * the allocator will just align them to a cache line.
512 * - MEMPOOL_F_NO_CACHE_ALIGN: By default, the returned objects are
513 * cache-aligned. This flag removes this constraint, and no
514 * padding will be present between objects. This flag implies
515 * MEMPOOL_F_NO_SPREAD.
516 * - MEMPOOL_F_SP_PUT: If this flag is set, the default behavior
517 * when using rte_mempool_put() or rte_mempool_put_bulk() is
518 * "single-producer". Otherwise, it is "multi-producers".
519 * - MEMPOOL_F_SC_GET: If this flag is set, the default behavior
520 * when using rte_mempool_get() or rte_mempool_get_bulk() is
521 * "single-consumer". Otherwise, it is "multi-consumers".
523 * The pointer to the new allocated mempool, on success. NULL on error
524 * with rte_errno set appropriately. Possible rte_errno values include:
525 * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
526 * - E_RTE_SECONDARY - function was called from a secondary process instance
527 * - E_RTE_NO_TAILQ - no tailq list could be got for the ring or mempool list
528 * - EINVAL - cache size provided is too large
529 * - ENOSPC - the maximum number of memzones has already been allocated
530 * - EEXIST - a memzone with the same name already exists
531 * - ENOMEM - no appropriate memory area found in which to create memzone
534 rte_mempool_create(const char *name, unsigned n, unsigned elt_size,
535 unsigned cache_size, unsigned private_data_size,
536 rte_mempool_ctor_t *mp_init, void *mp_init_arg,
537 rte_mempool_obj_ctor_t *obj_init, void *obj_init_arg,
538 int socket_id, unsigned flags);
541 * Creates a new mempool named *name* in memory.
543 * This function uses ``memzone_reserve()`` to allocate memory. The
544 * pool contains n elements of elt_size. Its size is set to n.
545 * Depending on the input parameters, mempool elements can be either allocated
546 * together with the mempool header, or an externally provided memory buffer
547 * could be used to store mempool objects. In later case, that external
548 * memory buffer can consist of set of disjoint phyiscal pages.
551 * The name of the mempool.
553 * The number of elements in the mempool. The optimum size (in terms of
554 * memory usage) for a mempool is when n is a power of two minus one:
557 * The size of each element.
559 * If cache_size is non-zero, the rte_mempool library will try to
560 * limit the accesses to the common lockless pool, by maintaining a
561 * per-lcore object cache. This argument must be lower or equal to
562 * CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE. It is advised to choose
563 * cache_size to have "n modulo cache_size == 0": if this is
564 * not the case, some elements will always stay in the pool and will
565 * never be used. The access to the per-lcore table is of course
566 * faster than the multi-producer/consumer pool. The cache can be
567 * disabled if the cache_size argument is set to 0; it can be useful to
568 * avoid losing objects in cache. Note that even if not used, the
569 * memory space for cache is always reserved in a mempool structure,
570 * except if CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE is set to 0.
571 * @param private_data_size
572 * The size of the private data appended after the mempool
573 * structure. This is useful for storing some private data after the
574 * mempool structure, as is done for rte_mbuf_pool for example.
576 * A function pointer that is called for initialization of the pool,
577 * before object initialization. The user can initialize the private
578 * data in this function if needed. This parameter can be NULL if
581 * An opaque pointer to data that can be used in the mempool
582 * constructor function.
584 * A function pointer that is called for each object at
585 * initialization of the pool. The user can set some meta data in
586 * objects if needed. This parameter can be NULL if not needed.
587 * The obj_init() function takes the mempool pointer, the init_arg,
588 * the object pointer and the object number as parameters.
589 * @param obj_init_arg
590 * An opaque pointer to data that can be used as an argument for
591 * each call to the object constructor function.
593 * The *socket_id* argument is the socket identifier in the case of
594 * NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
595 * constraint for the reserved zone.
597 * The *flags* arguments is an OR of following flags:
598 * - MEMPOOL_F_NO_SPREAD: By default, objects addresses are spread
599 * between channels in RAM: the pool allocator will add padding
600 * between objects depending on the hardware configuration. See
601 * Memory alignment constraints for details. If this flag is set,
602 * the allocator will just align them to a cache line.
603 * - MEMPOOL_F_NO_CACHE_ALIGN: By default, the returned objects are
604 * cache-aligned. This flag removes this constraint, and no
605 * padding will be present between objects. This flag implies
606 * MEMPOOL_F_NO_SPREAD.
607 * - MEMPOOL_F_SP_PUT: If this flag is set, the default behavior
608 * when using rte_mempool_put() or rte_mempool_put_bulk() is
609 * "single-producer". Otherwise, it is "multi-producers".
610 * - MEMPOOL_F_SC_GET: If this flag is set, the default behavior
611 * when using rte_mempool_get() or rte_mempool_get_bulk() is
612 * "single-consumer". Otherwise, it is "multi-consumers".
614 * Virtual address of the externally allocated memory buffer.
615 * Will be used to store mempool objects.
617 * Array of phyiscall addresses of the pages that comprises given memory
620 * Number of elements in the paddr array.
622 * LOG2 of the physical pages size.
624 * The pointer to the new allocated mempool, on success. NULL on error
625 * with rte_errno set appropriately. Possible rte_errno values include:
626 * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
627 * - E_RTE_SECONDARY - function was called from a secondary process instance
628 * - E_RTE_NO_TAILQ - no tailq list could be got for the ring or mempool list
629 * - EINVAL - cache size provided is too large
630 * - ENOSPC - the maximum number of memzones has already been allocated
631 * - EEXIST - a memzone with the same name already exists
632 * - ENOMEM - no appropriate memory area found in which to create memzone
635 rte_mempool_xmem_create(const char *name, unsigned n, unsigned elt_size,
636 unsigned cache_size, unsigned private_data_size,
637 rte_mempool_ctor_t *mp_init, void *mp_init_arg,
638 rte_mempool_obj_ctor_t *obj_init, void *obj_init_arg,
639 int socket_id, unsigned flags, void *vaddr,
640 const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift);
642 #ifdef RTE_LIBRTE_XEN_DOM0
644 * Creates a new mempool named *name* in memory on Xen Dom0.
646 * This function uses ``rte_mempool_xmem_create()`` to allocate memory. The
647 * pool contains n elements of elt_size. Its size is set to n.
648 * All elements of the mempool are allocated together with the mempool header,
649 * and memory buffer can consist of set of disjoint phyiscal pages.
652 * The name of the mempool.
654 * The number of elements in the mempool. The optimum size (in terms of
655 * memory usage) for a mempool is when n is a power of two minus one:
658 * The size of each element.
660 * If cache_size is non-zero, the rte_mempool library will try to
661 * limit the accesses to the common lockless pool, by maintaining a
662 * per-lcore object cache. This argument must be lower or equal to
663 * CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE. It is advised to choose
664 * cache_size to have "n modulo cache_size == 0": if this is
665 * not the case, some elements will always stay in the pool and will
666 * never be used. The access to the per-lcore table is of course
667 * faster than the multi-producer/consumer pool. The cache can be
668 * disabled if the cache_size argument is set to 0; it can be useful to
669 * avoid losing objects in cache. Note that even if not used, the
670 * memory space for cache is always reserved in a mempool structure,
671 * except if CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE is set to 0.
672 * @param private_data_size
673 * The size of the private data appended after the mempool
674 * structure. This is useful for storing some private data after the
675 * mempool structure, as is done for rte_mbuf_pool for example.
677 * A function pointer that is called for initialization of the pool,
678 * before object initialization. The user can initialize the private
679 * data in this function if needed. This parameter can be NULL if
682 * An opaque pointer to data that can be used in the mempool
683 * constructor function.
685 * A function pointer that is called for each object at
686 * initialization of the pool. The user can set some meta data in
687 * objects if needed. This parameter can be NULL if not needed.
688 * The obj_init() function takes the mempool pointer, the init_arg,
689 * the object pointer and the object number as parameters.
690 * @param obj_init_arg
691 * An opaque pointer to data that can be used as an argument for
692 * each call to the object constructor function.
694 * The *socket_id* argument is the socket identifier in the case of
695 * NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
696 * constraint for the reserved zone.
698 * The *flags* arguments is an OR of following flags:
699 * - MEMPOOL_F_NO_SPREAD: By default, objects addresses are spread
700 * between channels in RAM: the pool allocator will add padding
701 * between objects depending on the hardware configuration. See
702 * Memory alignment constraints for details. If this flag is set,
703 * the allocator will just align them to a cache line.
704 * - MEMPOOL_F_NO_CACHE_ALIGN: By default, the returned objects are
705 * cache-aligned. This flag removes this constraint, and no
706 * padding will be present between objects. This flag implies
707 * MEMPOOL_F_NO_SPREAD.
708 * - MEMPOOL_F_SP_PUT: If this flag is set, the default behavior
709 * when using rte_mempool_put() or rte_mempool_put_bulk() is
710 * "single-producer". Otherwise, it is "multi-producers".
711 * - MEMPOOL_F_SC_GET: If this flag is set, the default behavior
712 * when using rte_mempool_get() or rte_mempool_get_bulk() is
713 * "single-consumer". Otherwise, it is "multi-consumers".
715 * The pointer to the new allocated mempool, on success. NULL on error
716 * with rte_errno set appropriately. Possible rte_errno values include:
717 * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
718 * - E_RTE_SECONDARY - function was called from a secondary process instance
719 * - E_RTE_NO_TAILQ - no tailq list could be got for the ring or mempool list
720 * - EINVAL - cache size provided is too large
721 * - ENOSPC - the maximum number of memzones has already been allocated
722 * - EEXIST - a memzone with the same name already exists
723 * - ENOMEM - no appropriate memory area found in which to create memzone
726 rte_dom0_mempool_create(const char *name, unsigned n, unsigned elt_size,
727 unsigned cache_size, unsigned private_data_size,
728 rte_mempool_ctor_t *mp_init, void *mp_init_arg,
729 rte_mempool_obj_ctor_t *obj_init, void *obj_init_arg,
730 int socket_id, unsigned flags);
734 * Dump the status of the mempool to the console.
737 * A pointer to a file for output
739 * A pointer to the mempool structure.
741 void rte_mempool_dump(FILE *f, const struct rte_mempool *mp);
744 * @internal Put several objects back in the mempool; used internally.
746 * A pointer to the mempool structure.
748 * A pointer to a table of void * pointers (objects).
750 * The number of objects to store back in the mempool, must be strictly
753 * Mono-producer (0) or multi-producers (1).
755 static inline void __attribute__((always_inline))
756 __mempool_put_bulk(struct rte_mempool *mp, void * const *obj_table,
757 unsigned n, int is_mp)
759 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
760 struct rte_mempool_cache *cache;
763 unsigned lcore_id = rte_lcore_id();
764 uint32_t cache_size = mp->cache_size;
765 uint32_t flushthresh = mp->cache_flushthresh;
766 #endif /* RTE_MEMPOOL_CACHE_MAX_SIZE > 0 */
768 /* increment stat now, adding in mempool always success */
769 __MEMPOOL_STAT_ADD(mp, put, n);
771 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
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;
806 #endif /* RTE_MEMPOOL_CACHE_MAX_SIZE > 0 */
808 /* push remaining objects in ring */
809 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
811 if (rte_ring_mp_enqueue_bulk(mp->ring, obj_table, n) < 0)
812 rte_panic("cannot put objects in mempool\n");
815 if (rte_ring_sp_enqueue_bulk(mp->ring, obj_table, n) < 0)
816 rte_panic("cannot put objects in mempool\n");
820 rte_ring_mp_enqueue_bulk(mp->ring, obj_table, n);
822 rte_ring_sp_enqueue_bulk(mp->ring, obj_table, n);
828 * Put several objects back in the mempool (multi-producers safe).
831 * A pointer to the mempool structure.
833 * A pointer to a table of void * pointers (objects).
835 * The number of objects to add in the mempool from the obj_table.
837 static inline void __attribute__((always_inline))
838 rte_mempool_mp_put_bulk(struct rte_mempool *mp, void * const *obj_table,
841 __mempool_check_cookies(mp, obj_table, n, 0);
842 __mempool_put_bulk(mp, obj_table, n, 1);
846 * Put several objects back in the mempool (NOT multi-producers safe).
849 * A pointer to the mempool structure.
851 * A pointer to a table of void * pointers (objects).
853 * The number of objects to add in the mempool from obj_table.
856 rte_mempool_sp_put_bulk(struct rte_mempool *mp, void * const *obj_table,
859 __mempool_check_cookies(mp, obj_table, n, 0);
860 __mempool_put_bulk(mp, obj_table, n, 0);
864 * Put several objects back in the mempool.
866 * This function calls the multi-producer or the single-producer
867 * version depending on the default behavior that was specified at
868 * mempool creation time (see flags).
871 * A pointer to the mempool structure.
873 * A pointer to a table of void * pointers (objects).
875 * The number of objects to add in the mempool from obj_table.
877 static inline void __attribute__((always_inline))
878 rte_mempool_put_bulk(struct rte_mempool *mp, void * const *obj_table,
881 __mempool_check_cookies(mp, obj_table, n, 0);
882 __mempool_put_bulk(mp, obj_table, n, !(mp->flags & MEMPOOL_F_SP_PUT));
886 * Put one object in the mempool (multi-producers safe).
889 * A pointer to the mempool structure.
891 * A pointer to the object to be added.
893 static inline void __attribute__((always_inline))
894 rte_mempool_mp_put(struct rte_mempool *mp, void *obj)
896 rte_mempool_mp_put_bulk(mp, &obj, 1);
900 * Put one object back in the mempool (NOT multi-producers safe).
903 * A pointer to the mempool structure.
905 * A pointer to the object to be added.
907 static inline void __attribute__((always_inline))
908 rte_mempool_sp_put(struct rte_mempool *mp, void *obj)
910 rte_mempool_sp_put_bulk(mp, &obj, 1);
914 * Put one object back in the mempool.
916 * This function calls the multi-producer or the single-producer
917 * version depending on the default behavior that was specified at
918 * mempool creation time (see flags).
921 * A pointer to the mempool structure.
923 * A pointer to the object to be added.
925 static inline void __attribute__((always_inline))
926 rte_mempool_put(struct rte_mempool *mp, void *obj)
928 rte_mempool_put_bulk(mp, &obj, 1);
932 * @internal Get several objects from the mempool; used internally.
934 * A pointer to the mempool structure.
936 * A pointer to a table of void * pointers (objects).
938 * The number of objects to get, must be strictly positive.
940 * Mono-consumer (0) or multi-consumers (1).
942 * - >=0: Success; number of objects supplied.
943 * - <0: Error; code of ring dequeue function.
945 static inline int __attribute__((always_inline))
946 __mempool_get_bulk(struct rte_mempool *mp, void **obj_table,
947 unsigned n, int is_mc)
950 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
951 struct rte_mempool_cache *cache;
954 unsigned lcore_id = rte_lcore_id();
955 uint32_t cache_size = mp->cache_size;
957 /* cache is not enabled or single consumer */
958 if (unlikely(cache_size == 0 || is_mc == 0 ||
959 n >= cache_size || lcore_id >= RTE_MAX_LCORE))
962 cache = &mp->local_cache[lcore_id];
963 cache_objs = cache->objs;
965 /* Can this be satisfied from the cache? */
966 if (cache->len < n) {
967 /* No. Backfill the cache first, and then fill from it */
968 uint32_t req = n + (cache_size - cache->len);
970 /* How many do we require i.e. number to fill the cache + the request */
971 ret = rte_ring_mc_dequeue_bulk(mp->ring, &cache->objs[cache->len], req);
972 if (unlikely(ret < 0)) {
974 * In the offchance that we are buffer constrained,
975 * where we are not able to allocate cache + n, go to
976 * the ring directly. If that fails, we are truly out of
985 /* Now fill in the response ... */
986 for (index = 0, len = cache->len - 1; index < n; ++index, len--, obj_table++)
987 *obj_table = cache_objs[len];
991 __MEMPOOL_STAT_ADD(mp, get_success, n);
996 #endif /* RTE_MEMPOOL_CACHE_MAX_SIZE > 0 */
998 /* get remaining objects from ring */
1000 ret = rte_ring_mc_dequeue_bulk(mp->ring, obj_table, n);
1002 ret = rte_ring_sc_dequeue_bulk(mp->ring, obj_table, n);
1005 __MEMPOOL_STAT_ADD(mp, get_fail, n);
1007 __MEMPOOL_STAT_ADD(mp, get_success, n);
1013 * Get several objects from the mempool (multi-consumers safe).
1015 * If cache is enabled, objects will be retrieved first from cache,
1016 * subsequently from the common pool. Note that it can return -ENOENT when
1017 * the local cache and common pool are empty, even if cache from other
1021 * A pointer to the mempool structure.
1023 * A pointer to a table of void * pointers (objects) that will be filled.
1025 * The number of objects to get from mempool to obj_table.
1027 * - 0: Success; objects taken.
1028 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
1030 static inline int __attribute__((always_inline))
1031 rte_mempool_mc_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned n)
1034 ret = __mempool_get_bulk(mp, obj_table, n, 1);
1036 __mempool_check_cookies(mp, obj_table, n, 1);
1041 * Get several objects from the mempool (NOT multi-consumers safe).
1043 * If cache is enabled, objects will be retrieved first from cache,
1044 * subsequently from the common pool. Note that it can return -ENOENT when
1045 * the local cache and common pool are empty, even if cache from other
1049 * A pointer to the mempool structure.
1051 * A pointer to a table of void * pointers (objects) that will be filled.
1053 * The number of objects to get from the mempool to obj_table.
1055 * - 0: Success; objects taken.
1056 * - -ENOENT: Not enough entries in the mempool; no object is
1059 static inline int __attribute__((always_inline))
1060 rte_mempool_sc_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned n)
1063 ret = __mempool_get_bulk(mp, obj_table, n, 0);
1065 __mempool_check_cookies(mp, obj_table, n, 1);
1070 * Get several objects from the mempool.
1072 * This function calls the multi-consumers or the single-consumer
1073 * version, depending on the default behaviour that was specified at
1074 * mempool creation time (see flags).
1076 * If cache is enabled, objects will be retrieved first from cache,
1077 * subsequently from the common pool. Note that it can return -ENOENT when
1078 * the local cache and common pool are empty, even if cache from other
1082 * A pointer to the mempool structure.
1084 * A pointer to a table of void * pointers (objects) that will be filled.
1086 * The number of objects to get from the mempool to obj_table.
1088 * - 0: Success; objects taken
1089 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
1091 static inline int __attribute__((always_inline))
1092 rte_mempool_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned n)
1095 ret = __mempool_get_bulk(mp, obj_table, n,
1096 !(mp->flags & MEMPOOL_F_SC_GET));
1098 __mempool_check_cookies(mp, obj_table, n, 1);
1103 * Get one object from the mempool (multi-consumers safe).
1105 * If cache is enabled, objects will be retrieved first from cache,
1106 * subsequently from the common pool. Note that it can return -ENOENT when
1107 * the local cache and common pool are empty, even if cache from other
1111 * A pointer to the mempool structure.
1113 * A pointer to a void * pointer (object) that will be filled.
1115 * - 0: Success; objects taken.
1116 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
1118 static inline int __attribute__((always_inline))
1119 rte_mempool_mc_get(struct rte_mempool *mp, void **obj_p)
1121 return rte_mempool_mc_get_bulk(mp, obj_p, 1);
1125 * Get one object from the mempool (NOT multi-consumers safe).
1127 * If cache is enabled, objects will be retrieved first from cache,
1128 * subsequently from the common pool. Note that it can return -ENOENT when
1129 * the local cache and common pool are empty, even if cache from other
1133 * A pointer to the mempool structure.
1135 * A pointer to a void * pointer (object) that will be filled.
1137 * - 0: Success; objects taken.
1138 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
1140 static inline int __attribute__((always_inline))
1141 rte_mempool_sc_get(struct rte_mempool *mp, void **obj_p)
1143 return rte_mempool_sc_get_bulk(mp, obj_p, 1);
1147 * Get one object from the mempool.
1149 * This function calls the multi-consumers or the single-consumer
1150 * version, depending on the default behavior that was specified at
1151 * mempool creation (see flags).
1153 * If cache is enabled, objects will be retrieved first from cache,
1154 * subsequently from the common pool. Note that it can return -ENOENT when
1155 * the local cache and common pool are empty, even if cache from other
1159 * A pointer to the mempool structure.
1161 * A pointer to a void * pointer (object) that will be filled.
1163 * - 0: Success; objects taken.
1164 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
1166 static inline int __attribute__((always_inline))
1167 rte_mempool_get(struct rte_mempool *mp, void **obj_p)
1169 return rte_mempool_get_bulk(mp, obj_p, 1);
1173 * Return the number of entries in the mempool.
1175 * When cache is enabled, this function has to browse the length of
1176 * all lcores, so it should not be used in a data path, but only for
1180 * A pointer to the mempool structure.
1182 * The number of entries in the mempool.
1184 unsigned rte_mempool_count(const struct rte_mempool *mp);
1187 * Return the number of free entries in the mempool ring.
1188 * i.e. how many entries can be freed back to the mempool.
1190 * NOTE: This corresponds to the number of elements *allocated* from the
1191 * memory pool, not the number of elements in the pool itself. To count
1192 * the number elements currently available in the pool, use "rte_mempool_count"
1194 * When cache is enabled, this function has to browse the length of
1195 * all lcores, so it should not be used in a data path, but only for
1199 * A pointer to the mempool structure.
1201 * The number of free entries in the mempool.
1203 static inline unsigned
1204 rte_mempool_free_count(const struct rte_mempool *mp)
1206 return mp->size - rte_mempool_count(mp);
1210 * Test if the mempool is full.
1212 * When cache is enabled, this function has to browse the length of all
1213 * lcores, so it should not be used in a data path, but only for debug
1217 * A pointer to the mempool structure.
1219 * - 1: The mempool is full.
1220 * - 0: The mempool is not full.
1223 rte_mempool_full(const struct rte_mempool *mp)
1225 return !!(rte_mempool_count(mp) == mp->size);
1229 * Test if the mempool is empty.
1231 * When cache is enabled, this function has to browse the length of all
1232 * lcores, so it should not be used in a data path, but only for debug
1236 * A pointer to the mempool structure.
1238 * - 1: The mempool is empty.
1239 * - 0: The mempool is not empty.
1242 rte_mempool_empty(const struct rte_mempool *mp)
1244 return !!(rte_mempool_count(mp) == 0);
1248 * Return the physical address of elt, which is an element of the pool mp.
1251 * A pointer to the mempool structure.
1253 * A pointer (virtual address) to the element of the pool.
1255 * The physical address of the elt element.
1257 static inline phys_addr_t
1258 rte_mempool_virt2phy(const struct rte_mempool *mp, const void *elt)
1260 if (rte_eal_has_hugepages()) {
1263 off = (const char *)elt - (const char *)mp->elt_va_start;
1264 return (mp->elt_pa[off >> mp->pg_shift] + (off & mp->pg_mask));
1267 * If huge pages are disabled, we cannot assume the
1268 * memory region to be physically contiguous.
1269 * Lookup for each element.
1271 return rte_mem_virt2phy(elt);
1276 * Check the consistency of mempool objects.
1278 * Verify the coherency of fields in the mempool structure. Also check
1279 * that the cookies of mempool objects (even the ones that are not
1280 * present in pool) have a correct value. If not, a panic will occur.
1283 * A pointer to the mempool structure.
1285 void rte_mempool_audit(const struct rte_mempool *mp);
1288 * Return a pointer to the private data in an mempool structure.
1291 * A pointer to the mempool structure.
1293 * A pointer to the private data.
1295 static inline void *rte_mempool_get_priv(struct rte_mempool *mp)
1297 return (char *)mp + MEMPOOL_HEADER_SIZE(mp, mp->pg_num);
1301 * Dump the status of all mempools on the console
1304 * A pointer to a file for output
1306 void rte_mempool_list_dump(FILE *f);
1309 * Search a mempool from its name
1312 * The name of the mempool.
1314 * The pointer to the mempool matching the name, or NULL if not found.
1316 * with rte_errno set appropriately. Possible rte_errno values include:
1317 * - ENOENT - required entry not available to return.
1320 struct rte_mempool *rte_mempool_lookup(const char *name);
1323 * Given a desired size of the mempool element and mempool flags,
1324 * caluclates header, trailer, body and total sizes of the mempool object.
1326 * The size of each element.
1328 * The flags used for the mempool creation.
1329 * Consult rte_mempool_create() for more information about possible values.
1330 * The size of each element.
1332 * Total size of the mempool object.
1334 uint32_t rte_mempool_calc_obj_size(uint32_t elt_size, uint32_t flags,
1335 struct rte_mempool_objsz *sz);
1338 * Calculate maximum amount of memory required to store given number of objects.
1339 * Assumes that the memory buffer will be aligned at page boundary.
1340 * Note, that if object size is bigger then page size, then it assumes that
1341 * we have a subsets of physically continuous pages big enough to store
1342 * at least one object.
1344 * Number of elements.
1346 * The size of each element.
1348 * LOG2 of the physical pages size.
1350 * Required memory size aligned at page boundary.
1352 size_t rte_mempool_xmem_size(uint32_t elt_num, size_t elt_sz,
1356 * Calculate how much memory would be actually required with the given
1357 * memory footprint to store required number of objects.
1359 * Virtual address of the externally allocated memory buffer.
1360 * Will be used to store mempool objects.
1362 * Number of elements.
1364 * The size of each element.
1366 * Array of phyiscall addresses of the pages that comprises given memory
1369 * Number of elements in the paddr array.
1371 * LOG2 of the physical pages size.
1373 * Number of bytes needed to store given number of objects,
1374 * aligned to the given page size.
1375 * If provided memory buffer is not big enough:
1376 * (-1) * actual number of elemnts that can be stored in that buffer.
1378 ssize_t rte_mempool_xmem_usage(void *vaddr, uint32_t elt_num, size_t elt_sz,
1379 const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift);
1382 * Walk list of all memory pools
1387 * Argument passed to iterator
1389 void rte_mempool_walk(void (*func)(const struct rte_mempool *, void *arg),
1396 #endif /* _RTE_MEMPOOL_H_ */