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 * Mempool object header structure
145 * Each object stored in mempools are prefixed by this header structure,
146 * it allows to retrieve the mempool pointer from the object. When debug
147 * is enabled, a cookie is also added in this structure preventing
148 * corruptions and double-frees.
150 struct rte_mempool_objhdr {
151 struct rte_mempool *mp; /**< The mempool owning the object. */
152 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
153 uint64_t cookie; /**< Debug cookie. */
158 * The RTE mempool structure.
161 char name[RTE_MEMPOOL_NAMESIZE]; /**< Name of mempool. */
162 struct rte_ring *ring; /**< Ring to store objects. */
163 phys_addr_t phys_addr; /**< Phys. addr. of mempool struct. */
164 int flags; /**< Flags of the mempool. */
165 uint32_t size; /**< Size of the mempool. */
166 uint32_t cache_size; /**< Size of per-lcore local cache. */
167 uint32_t cache_flushthresh;
168 /**< Threshold before we flush excess elements. */
170 uint32_t elt_size; /**< Size of an element. */
171 uint32_t header_size; /**< Size of header (before elt). */
172 uint32_t trailer_size; /**< Size of trailer (after elt). */
174 unsigned private_data_size; /**< Size of private data. */
176 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
177 /** Per-lcore local cache. */
178 struct rte_mempool_cache local_cache[RTE_MAX_LCORE];
181 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
182 /** Per-lcore statistics. */
183 struct rte_mempool_debug_stats stats[RTE_MAX_LCORE];
186 /* Address translation support, starts from next cache line. */
188 /** Number of elements in the elt_pa array. */
189 uint32_t pg_num __rte_cache_aligned;
190 uint32_t pg_shift; /**< LOG2 of the physical pages. */
191 uintptr_t pg_mask; /**< physical page mask value. */
192 uintptr_t elt_va_start;
193 /**< Virtual address of the first mempool object. */
194 uintptr_t elt_va_end;
195 /**< Virtual address of the <size + 1> mempool object. */
196 phys_addr_t elt_pa[MEMPOOL_PG_NUM_DEFAULT];
197 /**< Array of physical pages addresses for the mempool objects buffer. */
199 } __rte_cache_aligned;
201 #define MEMPOOL_F_NO_SPREAD 0x0001 /**< Do not spread in memory. */
202 #define MEMPOOL_F_NO_CACHE_ALIGN 0x0002 /**< Do not align objs on cache lines.*/
203 #define MEMPOOL_F_SP_PUT 0x0004 /**< Default put is "single-producer".*/
204 #define MEMPOOL_F_SC_GET 0x0008 /**< Default get is "single-consumer".*/
207 * @internal When debug is enabled, store some statistics.
209 * Pointer to the memory pool.
211 * Name of the statistics field to increment in the memory pool.
213 * Number to add to the object-oriented statistics.
215 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
216 #define __MEMPOOL_STAT_ADD(mp, name, n) do { \
217 unsigned __lcore_id = rte_lcore_id(); \
218 if (__lcore_id < RTE_MAX_LCORE) { \
219 mp->stats[__lcore_id].name##_objs += n; \
220 mp->stats[__lcore_id].name##_bulk += 1; \
224 #define __MEMPOOL_STAT_ADD(mp, name, n) do {} while(0)
228 * Calculates size of the mempool header.
230 * Pointer to the memory pool.
232 * Number of page used to store mempool objects.
234 #define MEMPOOL_HEADER_SIZE(mp, pgn) (sizeof(*(mp)) + \
235 RTE_ALIGN_CEIL(((pgn) - RTE_DIM((mp)->elt_pa)) * \
236 sizeof ((mp)->elt_pa[0]), RTE_CACHE_LINE_SIZE))
239 * Returns TRUE if whole mempool is allocated in one contiguous block of memory.
241 #define MEMPOOL_IS_CONTIG(mp) \
242 ((mp)->pg_num == MEMPOOL_PG_NUM_DEFAULT && \
243 (mp)->phys_addr == (mp)->elt_pa[0])
245 /* return the header of a mempool object (internal) */
246 static inline struct rte_mempool_objhdr *__mempool_get_header(void *obj)
248 return (struct rte_mempool_objhdr *)((char *)obj -
249 sizeof(struct rte_mempool_objhdr));
253 * Return a pointer to the mempool owning this object.
256 * An object that is owned by a pool. If this is not the case,
257 * the behavior is undefined.
259 * A pointer to the mempool structure.
261 static inline struct rte_mempool *rte_mempool_from_obj(void *obj)
263 struct rte_mempool_objhdr *hdr = __mempool_get_header(obj);
267 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
268 /* get trailer cookie value */
269 static inline uint64_t __mempool_read_trailer_cookie(void *obj)
271 struct rte_mempool *mp = rte_mempool_from_obj(obj);
272 return *(uint64_t *)((char *)obj + mp->elt_size);
276 /* write trailer cookie value */
277 static inline void __mempool_write_trailer_cookie(void *obj)
280 struct rte_mempool *mp = rte_mempool_from_obj(obj);
281 cookie_p = (uint64_t *)((char *)obj + mp->elt_size);
282 *cookie_p = RTE_MEMPOOL_TRAILER_COOKIE;
284 #endif /* RTE_LIBRTE_MEMPOOL_DEBUG */
287 * @internal Check and update cookies or panic.
290 * Pointer to the memory pool.
291 * @param obj_table_const
292 * Pointer to a table of void * pointers (objects).
294 * Index of object in object table.
296 * - 0: object is supposed to be allocated, mark it as free
297 * - 1: object is supposed to be free, mark it as allocated
298 * - 2: just check that cookie is valid (free or allocated)
300 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
301 #ifndef __INTEL_COMPILER
302 #pragma GCC diagnostic ignored "-Wcast-qual"
304 static inline void __mempool_check_cookies(const struct rte_mempool *mp,
305 void * const *obj_table_const,
306 unsigned n, int free)
308 struct rte_mempool_objhdr *hdr;
314 /* Force to drop the "const" attribute. This is done only when
315 * DEBUG is enabled */
316 tmp = (void *) obj_table_const;
317 obj_table = (void **) tmp;
322 if (rte_mempool_from_obj(obj) != mp)
323 rte_panic("MEMPOOL: object is owned by another "
326 hdr = __mempool_get_header(obj);
327 cookie = hdr->cookie;
330 if (cookie != RTE_MEMPOOL_HEADER_COOKIE1) {
331 rte_log_set_history(0);
332 RTE_LOG(CRIT, MEMPOOL,
333 "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
334 obj, (const void *) mp, cookie);
335 rte_panic("MEMPOOL: bad header cookie (put)\n");
337 hdr->cookie = RTE_MEMPOOL_HEADER_COOKIE2;
339 else if (free == 1) {
340 if (cookie != RTE_MEMPOOL_HEADER_COOKIE2) {
341 rte_log_set_history(0);
342 RTE_LOG(CRIT, MEMPOOL,
343 "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
344 obj, (const void *) mp, cookie);
345 rte_panic("MEMPOOL: bad header cookie (get)\n");
347 hdr->cookie = RTE_MEMPOOL_HEADER_COOKIE1;
349 else if (free == 2) {
350 if (cookie != RTE_MEMPOOL_HEADER_COOKIE1 &&
351 cookie != RTE_MEMPOOL_HEADER_COOKIE2) {
352 rte_log_set_history(0);
353 RTE_LOG(CRIT, MEMPOOL,
354 "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
355 obj, (const void *) mp, cookie);
356 rte_panic("MEMPOOL: bad header cookie (audit)\n");
359 cookie = __mempool_read_trailer_cookie(obj);
360 if (cookie != RTE_MEMPOOL_TRAILER_COOKIE) {
361 rte_log_set_history(0);
362 RTE_LOG(CRIT, MEMPOOL,
363 "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
364 obj, (const void *) mp, cookie);
365 rte_panic("MEMPOOL: bad trailer cookie\n");
369 #ifndef __INTEL_COMPILER
370 #pragma GCC diagnostic error "-Wcast-qual"
373 #define __mempool_check_cookies(mp, obj_table_const, n, free) do {} while(0)
374 #endif /* RTE_LIBRTE_MEMPOOL_DEBUG */
377 * An mempool's object iterator callback function.
379 typedef void (*rte_mempool_obj_iter_t)(void * /*obj_iter_arg*/,
380 void * /*obj_start*/,
382 uint32_t /*obj_index */);
385 * Iterates across objects of the given size and alignment in the
386 * provided chunk of memory. The given memory buffer can consist of
387 * disjoint physical pages.
388 * For each object calls the provided callback (if any).
389 * Used to populate mempool, walk through all elements of the mempool,
390 * estimate how many elements of the given size could be created in the given
393 * Virtual address of the memory buffer.
395 * Maximum number of objects to iterate through.
397 * Size of each object.
399 * Array of phyiscall addresses of the pages that comprises given memory
402 * Number of elements in the paddr array.
404 * LOG2 of the physical pages size.
406 * Object iterator callback function (could be NULL).
407 * @param obj_iter_arg
408 * User defined Prameter for the object iterator callback function.
411 * Number of objects iterated through.
414 uint32_t rte_mempool_obj_iter(void *vaddr,
415 uint32_t elt_num, size_t elt_sz, size_t align,
416 const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift,
417 rte_mempool_obj_iter_t obj_iter, void *obj_iter_arg);
420 * An object constructor callback function for mempool.
422 * Arguments are the mempool, the opaque pointer given by the user in
423 * rte_mempool_create(), the pointer to the element and the index of
424 * the element in the pool.
426 typedef void (rte_mempool_obj_ctor_t)(struct rte_mempool *, void *,
430 * A mempool constructor callback function.
432 * Arguments are the mempool and the opaque pointer given by the user in
433 * rte_mempool_create().
435 typedef void (rte_mempool_ctor_t)(struct rte_mempool *, void *);
438 * Creates a new mempool named *name* in memory.
440 * This function uses ``memzone_reserve()`` to allocate memory. The
441 * pool contains n elements of elt_size. Its size is set to n.
442 * All elements of the mempool are allocated together with the mempool header,
443 * in one physically continuous chunk of memory.
446 * The name of the mempool.
448 * The number of elements in the mempool. The optimum size (in terms of
449 * memory usage) for a mempool is when n is a power of two minus one:
452 * The size of each element.
454 * If cache_size is non-zero, the rte_mempool library will try to
455 * limit the accesses to the common lockless pool, by maintaining a
456 * per-lcore object cache. This argument must be lower or equal to
457 * CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE and n / 1.5. It is advised to choose
458 * cache_size to have "n modulo cache_size == 0": if this is
459 * not the case, some elements will always stay in the pool and will
460 * never be used. The access to the per-lcore table is of course
461 * faster than the multi-producer/consumer pool. The cache can be
462 * disabled if the cache_size argument is set to 0; it can be useful to
463 * avoid losing objects in cache. Note that even if not used, the
464 * memory space for cache is always reserved in a mempool structure,
465 * except if CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE is set to 0.
466 * @param private_data_size
467 * The size of the private data appended after the mempool
468 * structure. This is useful for storing some private data after the
469 * mempool structure, as is done for rte_mbuf_pool for example.
471 * A function pointer that is called for initialization of the pool,
472 * before object initialization. The user can initialize the private
473 * data in this function if needed. This parameter can be NULL if
476 * An opaque pointer to data that can be used in the mempool
477 * constructor function.
479 * A function pointer that is called for each object at
480 * initialization of the pool. The user can set some meta data in
481 * objects if needed. This parameter can be NULL if not needed.
482 * The obj_init() function takes the mempool pointer, the init_arg,
483 * the object pointer and the object number as parameters.
484 * @param obj_init_arg
485 * An opaque pointer to data that can be used as an argument for
486 * each call to the object constructor function.
488 * The *socket_id* argument is the socket identifier in the case of
489 * NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
490 * constraint for the reserved zone.
492 * The *flags* arguments is an OR of following flags:
493 * - MEMPOOL_F_NO_SPREAD: By default, objects addresses are spread
494 * between channels in RAM: the pool allocator will add padding
495 * between objects depending on the hardware configuration. See
496 * Memory alignment constraints for details. If this flag is set,
497 * the allocator will just align them to a cache line.
498 * - MEMPOOL_F_NO_CACHE_ALIGN: By default, the returned objects are
499 * cache-aligned. This flag removes this constraint, and no
500 * padding will be present between objects. This flag implies
501 * MEMPOOL_F_NO_SPREAD.
502 * - MEMPOOL_F_SP_PUT: If this flag is set, the default behavior
503 * when using rte_mempool_put() or rte_mempool_put_bulk() is
504 * "single-producer". Otherwise, it is "multi-producers".
505 * - MEMPOOL_F_SC_GET: If this flag is set, the default behavior
506 * when using rte_mempool_get() or rte_mempool_get_bulk() is
507 * "single-consumer". Otherwise, it is "multi-consumers".
509 * The pointer to the new allocated mempool, on success. NULL on error
510 * with rte_errno set appropriately. Possible rte_errno values include:
511 * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
512 * - E_RTE_SECONDARY - function was called from a secondary process instance
513 * - EINVAL - cache size provided is too large
514 * - ENOSPC - the maximum number of memzones has already been allocated
515 * - EEXIST - a memzone with the same name already exists
516 * - ENOMEM - no appropriate memory area found in which to create memzone
519 rte_mempool_create(const char *name, unsigned n, unsigned elt_size,
520 unsigned cache_size, unsigned private_data_size,
521 rte_mempool_ctor_t *mp_init, void *mp_init_arg,
522 rte_mempool_obj_ctor_t *obj_init, void *obj_init_arg,
523 int socket_id, unsigned flags);
526 * Creates a new mempool named *name* in memory.
528 * This function uses ``memzone_reserve()`` to allocate memory. The
529 * pool contains n elements of elt_size. Its size is set to n.
530 * Depending on the input parameters, mempool elements can be either allocated
531 * together with the mempool header, or an externally provided memory buffer
532 * could be used to store mempool objects. In later case, that external
533 * memory buffer can consist of set of disjoint phyiscal pages.
536 * The name of the mempool.
538 * The number of elements in the mempool. The optimum size (in terms of
539 * memory usage) for a mempool is when n is a power of two minus one:
542 * The size of each element.
544 * If cache_size is non-zero, the rte_mempool library will try to
545 * limit the accesses to the common lockless pool, by maintaining a
546 * per-lcore object cache. This argument must be lower or equal to
547 * CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE. It is advised to choose
548 * cache_size to have "n modulo cache_size == 0": if this is
549 * not the case, some elements will always stay in the pool and will
550 * never be used. The access to the per-lcore table is of course
551 * faster than the multi-producer/consumer pool. The cache can be
552 * disabled if the cache_size argument is set to 0; it can be useful to
553 * avoid losing objects in cache. Note that even if not used, the
554 * memory space for cache is always reserved in a mempool structure,
555 * except if CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE is set to 0.
556 * @param private_data_size
557 * The size of the private data appended after the mempool
558 * structure. This is useful for storing some private data after the
559 * mempool structure, as is done for rte_mbuf_pool for example.
561 * A function pointer that is called for initialization of the pool,
562 * before object initialization. The user can initialize the private
563 * data in this function if needed. This parameter can be NULL if
566 * An opaque pointer to data that can be used in the mempool
567 * constructor function.
569 * A function pointer that is called for each object at
570 * initialization of the pool. The user can set some meta data in
571 * objects if needed. This parameter can be NULL if not needed.
572 * The obj_init() function takes the mempool pointer, the init_arg,
573 * the object pointer and the object number as parameters.
574 * @param obj_init_arg
575 * An opaque pointer to data that can be used as an argument for
576 * each call to the object constructor function.
578 * The *socket_id* argument is the socket identifier in the case of
579 * NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
580 * constraint for the reserved zone.
582 * The *flags* arguments is an OR of following flags:
583 * - MEMPOOL_F_NO_SPREAD: By default, objects addresses are spread
584 * between channels in RAM: the pool allocator will add padding
585 * between objects depending on the hardware configuration. See
586 * Memory alignment constraints for details. If this flag is set,
587 * the allocator will just align them to a cache line.
588 * - MEMPOOL_F_NO_CACHE_ALIGN: By default, the returned objects are
589 * cache-aligned. This flag removes this constraint, and no
590 * padding will be present between objects. This flag implies
591 * MEMPOOL_F_NO_SPREAD.
592 * - MEMPOOL_F_SP_PUT: If this flag is set, the default behavior
593 * when using rte_mempool_put() or rte_mempool_put_bulk() is
594 * "single-producer". Otherwise, it is "multi-producers".
595 * - MEMPOOL_F_SC_GET: If this flag is set, the default behavior
596 * when using rte_mempool_get() or rte_mempool_get_bulk() is
597 * "single-consumer". Otherwise, it is "multi-consumers".
599 * Virtual address of the externally allocated memory buffer.
600 * Will be used to store mempool objects.
602 * Array of phyiscall addresses of the pages that comprises given memory
605 * Number of elements in the paddr array.
607 * LOG2 of the physical pages size.
609 * The pointer to the new allocated mempool, on success. NULL on error
610 * with rte_errno set appropriately. Possible rte_errno values include:
611 * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
612 * - E_RTE_SECONDARY - function was called from a secondary process instance
613 * - EINVAL - cache size provided is too large
614 * - ENOSPC - the maximum number of memzones has already been allocated
615 * - EEXIST - a memzone with the same name already exists
616 * - ENOMEM - no appropriate memory area found in which to create memzone
619 rte_mempool_xmem_create(const char *name, unsigned n, unsigned elt_size,
620 unsigned cache_size, unsigned private_data_size,
621 rte_mempool_ctor_t *mp_init, void *mp_init_arg,
622 rte_mempool_obj_ctor_t *obj_init, void *obj_init_arg,
623 int socket_id, unsigned flags, void *vaddr,
624 const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift);
626 #ifdef RTE_LIBRTE_XEN_DOM0
628 * Creates a new mempool named *name* in memory on Xen Dom0.
630 * This function uses ``rte_mempool_xmem_create()`` to allocate memory. The
631 * pool contains n elements of elt_size. Its size is set to n.
632 * All elements of the mempool are allocated together with the mempool header,
633 * and memory buffer can consist of set of disjoint phyiscal pages.
636 * The name of the mempool.
638 * The number of elements in the mempool. The optimum size (in terms of
639 * memory usage) for a mempool is when n is a power of two minus one:
642 * The size of each element.
644 * If cache_size is non-zero, the rte_mempool library will try to
645 * limit the accesses to the common lockless pool, by maintaining a
646 * per-lcore object cache. This argument must be lower or equal to
647 * CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE. It is advised to choose
648 * cache_size to have "n modulo cache_size == 0": if this is
649 * not the case, some elements will always stay in the pool and will
650 * never be used. The access to the per-lcore table is of course
651 * faster than the multi-producer/consumer pool. The cache can be
652 * disabled if the cache_size argument is set to 0; it can be useful to
653 * avoid losing objects in cache. Note that even if not used, the
654 * memory space for cache is always reserved in a mempool structure,
655 * except if CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE is set to 0.
656 * @param private_data_size
657 * The size of the private data appended after the mempool
658 * structure. This is useful for storing some private data after the
659 * mempool structure, as is done for rte_mbuf_pool for example.
661 * A function pointer that is called for initialization of the pool,
662 * before object initialization. The user can initialize the private
663 * data in this function if needed. This parameter can be NULL if
666 * An opaque pointer to data that can be used in the mempool
667 * constructor function.
669 * A function pointer that is called for each object at
670 * initialization of the pool. The user can set some meta data in
671 * objects if needed. This parameter can be NULL if not needed.
672 * The obj_init() function takes the mempool pointer, the init_arg,
673 * the object pointer and the object number as parameters.
674 * @param obj_init_arg
675 * An opaque pointer to data that can be used as an argument for
676 * each call to the object constructor function.
678 * The *socket_id* argument is the socket identifier in the case of
679 * NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
680 * constraint for the reserved zone.
682 * The *flags* arguments is an OR of following flags:
683 * - MEMPOOL_F_NO_SPREAD: By default, objects addresses are spread
684 * between channels in RAM: the pool allocator will add padding
685 * between objects depending on the hardware configuration. See
686 * Memory alignment constraints for details. If this flag is set,
687 * the allocator will just align them to a cache line.
688 * - MEMPOOL_F_NO_CACHE_ALIGN: By default, the returned objects are
689 * cache-aligned. This flag removes this constraint, and no
690 * padding will be present between objects. This flag implies
691 * MEMPOOL_F_NO_SPREAD.
692 * - MEMPOOL_F_SP_PUT: If this flag is set, the default behavior
693 * when using rte_mempool_put() or rte_mempool_put_bulk() is
694 * "single-producer". Otherwise, it is "multi-producers".
695 * - MEMPOOL_F_SC_GET: If this flag is set, the default behavior
696 * when using rte_mempool_get() or rte_mempool_get_bulk() is
697 * "single-consumer". Otherwise, it is "multi-consumers".
699 * The pointer to the new allocated mempool, on success. NULL on error
700 * with rte_errno set appropriately. Possible rte_errno values include:
701 * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
702 * - E_RTE_SECONDARY - function was called from a secondary process instance
703 * - EINVAL - cache size provided is too large
704 * - ENOSPC - the maximum number of memzones has already been allocated
705 * - EEXIST - a memzone with the same name already exists
706 * - ENOMEM - no appropriate memory area found in which to create memzone
709 rte_dom0_mempool_create(const char *name, unsigned n, unsigned elt_size,
710 unsigned cache_size, unsigned private_data_size,
711 rte_mempool_ctor_t *mp_init, void *mp_init_arg,
712 rte_mempool_obj_ctor_t *obj_init, void *obj_init_arg,
713 int socket_id, unsigned flags);
717 * Dump the status of the mempool to the console.
720 * A pointer to a file for output
722 * A pointer to the mempool structure.
724 void rte_mempool_dump(FILE *f, const struct rte_mempool *mp);
727 * @internal Put several objects back in the mempool; used internally.
729 * A pointer to the mempool structure.
731 * A pointer to a table of void * pointers (objects).
733 * The number of objects to store back in the mempool, must be strictly
736 * Mono-producer (0) or multi-producers (1).
738 static inline void __attribute__((always_inline))
739 __mempool_put_bulk(struct rte_mempool *mp, void * const *obj_table,
740 unsigned n, int is_mp)
742 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
743 struct rte_mempool_cache *cache;
746 unsigned lcore_id = rte_lcore_id();
747 uint32_t cache_size = mp->cache_size;
748 uint32_t flushthresh = mp->cache_flushthresh;
749 #endif /* RTE_MEMPOOL_CACHE_MAX_SIZE > 0 */
751 /* increment stat now, adding in mempool always success */
752 __MEMPOOL_STAT_ADD(mp, put, n);
754 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
755 /* cache is not enabled or single producer or non-EAL thread */
756 if (unlikely(cache_size == 0 || is_mp == 0 ||
757 lcore_id >= RTE_MAX_LCORE))
760 /* Go straight to ring if put would overflow mem allocated for cache */
761 if (unlikely(n > RTE_MEMPOOL_CACHE_MAX_SIZE))
764 cache = &mp->local_cache[lcore_id];
765 cache_objs = &cache->objs[cache->len];
768 * The cache follows the following algorithm
769 * 1. Add the objects to the cache
770 * 2. Anything greater than the cache min value (if it crosses the
771 * cache flush threshold) is flushed to the ring.
774 /* Add elements back into the cache */
775 for (index = 0; index < n; ++index, obj_table++)
776 cache_objs[index] = *obj_table;
780 if (cache->len >= flushthresh) {
781 rte_ring_mp_enqueue_bulk(mp->ring, &cache->objs[cache_size],
782 cache->len - cache_size);
783 cache->len = cache_size;
789 #endif /* RTE_MEMPOOL_CACHE_MAX_SIZE > 0 */
791 /* push remaining objects in ring */
792 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
794 if (rte_ring_mp_enqueue_bulk(mp->ring, obj_table, n) < 0)
795 rte_panic("cannot put objects in mempool\n");
798 if (rte_ring_sp_enqueue_bulk(mp->ring, obj_table, n) < 0)
799 rte_panic("cannot put objects in mempool\n");
803 rte_ring_mp_enqueue_bulk(mp->ring, obj_table, n);
805 rte_ring_sp_enqueue_bulk(mp->ring, obj_table, n);
811 * Put several objects back in the mempool (multi-producers safe).
814 * A pointer to the mempool structure.
816 * A pointer to a table of void * pointers (objects).
818 * The number of objects to add in the mempool from the obj_table.
820 static inline void __attribute__((always_inline))
821 rte_mempool_mp_put_bulk(struct rte_mempool *mp, void * const *obj_table,
824 __mempool_check_cookies(mp, obj_table, n, 0);
825 __mempool_put_bulk(mp, obj_table, n, 1);
829 * Put several objects back in the mempool (NOT multi-producers safe).
832 * A pointer to the mempool structure.
834 * A pointer to a table of void * pointers (objects).
836 * The number of objects to add in the mempool from obj_table.
839 rte_mempool_sp_put_bulk(struct rte_mempool *mp, void * const *obj_table,
842 __mempool_check_cookies(mp, obj_table, n, 0);
843 __mempool_put_bulk(mp, obj_table, n, 0);
847 * Put several objects back in the mempool.
849 * This function calls the multi-producer or the single-producer
850 * version depending on the default behavior that was specified at
851 * mempool creation time (see flags).
854 * A pointer to the mempool structure.
856 * A pointer to a table of void * pointers (objects).
858 * The number of objects to add in the mempool from obj_table.
860 static inline void __attribute__((always_inline))
861 rte_mempool_put_bulk(struct rte_mempool *mp, void * const *obj_table,
864 __mempool_check_cookies(mp, obj_table, n, 0);
865 __mempool_put_bulk(mp, obj_table, n, !(mp->flags & MEMPOOL_F_SP_PUT));
869 * Put one object in the mempool (multi-producers safe).
872 * A pointer to the mempool structure.
874 * A pointer to the object to be added.
876 static inline void __attribute__((always_inline))
877 rte_mempool_mp_put(struct rte_mempool *mp, void *obj)
879 rte_mempool_mp_put_bulk(mp, &obj, 1);
883 * Put one object back in the mempool (NOT multi-producers safe).
886 * A pointer to the mempool structure.
888 * A pointer to the object to be added.
890 static inline void __attribute__((always_inline))
891 rte_mempool_sp_put(struct rte_mempool *mp, void *obj)
893 rte_mempool_sp_put_bulk(mp, &obj, 1);
897 * Put one object back in the mempool.
899 * This function calls the multi-producer or the single-producer
900 * version depending on the default behavior that was specified at
901 * mempool creation time (see flags).
904 * A pointer to the mempool structure.
906 * A pointer to the object to be added.
908 static inline void __attribute__((always_inline))
909 rte_mempool_put(struct rte_mempool *mp, void *obj)
911 rte_mempool_put_bulk(mp, &obj, 1);
915 * @internal Get several objects from the mempool; used internally.
917 * A pointer to the mempool structure.
919 * A pointer to a table of void * pointers (objects).
921 * The number of objects to get, must be strictly positive.
923 * Mono-consumer (0) or multi-consumers (1).
925 * - >=0: Success; number of objects supplied.
926 * - <0: Error; code of ring dequeue function.
928 static inline int __attribute__((always_inline))
929 __mempool_get_bulk(struct rte_mempool *mp, void **obj_table,
930 unsigned n, int is_mc)
933 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
934 struct rte_mempool_cache *cache;
937 unsigned lcore_id = rte_lcore_id();
938 uint32_t cache_size = mp->cache_size;
940 /* cache is not enabled or single consumer */
941 if (unlikely(cache_size == 0 || is_mc == 0 ||
942 n >= cache_size || lcore_id >= RTE_MAX_LCORE))
945 cache = &mp->local_cache[lcore_id];
946 cache_objs = cache->objs;
948 /* Can this be satisfied from the cache? */
949 if (cache->len < n) {
950 /* No. Backfill the cache first, and then fill from it */
951 uint32_t req = n + (cache_size - cache->len);
953 /* How many do we require i.e. number to fill the cache + the request */
954 ret = rte_ring_mc_dequeue_bulk(mp->ring, &cache->objs[cache->len], req);
955 if (unlikely(ret < 0)) {
957 * In the offchance that we are buffer constrained,
958 * where we are not able to allocate cache + n, go to
959 * the ring directly. If that fails, we are truly out of
968 /* Now fill in the response ... */
969 for (index = 0, len = cache->len - 1; index < n; ++index, len--, obj_table++)
970 *obj_table = cache_objs[len];
974 __MEMPOOL_STAT_ADD(mp, get_success, n);
979 #endif /* RTE_MEMPOOL_CACHE_MAX_SIZE > 0 */
981 /* get remaining objects from ring */
983 ret = rte_ring_mc_dequeue_bulk(mp->ring, obj_table, n);
985 ret = rte_ring_sc_dequeue_bulk(mp->ring, obj_table, n);
988 __MEMPOOL_STAT_ADD(mp, get_fail, n);
990 __MEMPOOL_STAT_ADD(mp, get_success, n);
996 * Get several objects 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 table of void * pointers (objects) that will be filled.
1008 * The number of objects to get from mempool to obj_table.
1010 * - 0: Success; objects taken.
1011 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
1013 static inline int __attribute__((always_inline))
1014 rte_mempool_mc_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned n)
1017 ret = __mempool_get_bulk(mp, obj_table, n, 1);
1019 __mempool_check_cookies(mp, obj_table, n, 1);
1024 * Get several objects from the mempool (NOT multi-consumers safe).
1026 * If cache is enabled, objects will be retrieved first from cache,
1027 * subsequently from the common pool. Note that it can return -ENOENT when
1028 * the local cache and common pool are empty, even if cache from other
1032 * A pointer to the mempool structure.
1034 * A pointer to a table of void * pointers (objects) that will be filled.
1036 * The number of objects to get from the mempool to obj_table.
1038 * - 0: Success; objects taken.
1039 * - -ENOENT: Not enough entries in the mempool; no object is
1042 static inline int __attribute__((always_inline))
1043 rte_mempool_sc_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned n)
1046 ret = __mempool_get_bulk(mp, obj_table, n, 0);
1048 __mempool_check_cookies(mp, obj_table, n, 1);
1053 * Get several objects from the mempool.
1055 * This function calls the multi-consumers or the single-consumer
1056 * version, depending on the default behaviour that was specified at
1057 * mempool creation time (see flags).
1059 * If cache is enabled, objects will be retrieved first from cache,
1060 * subsequently from the common pool. Note that it can return -ENOENT when
1061 * the local cache and common pool are empty, even if cache from other
1065 * A pointer to the mempool structure.
1067 * A pointer to a table of void * pointers (objects) that will be filled.
1069 * The number of objects to get from the mempool to obj_table.
1071 * - 0: Success; objects taken
1072 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
1074 static inline int __attribute__((always_inline))
1075 rte_mempool_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned n)
1078 ret = __mempool_get_bulk(mp, obj_table, n,
1079 !(mp->flags & MEMPOOL_F_SC_GET));
1081 __mempool_check_cookies(mp, obj_table, n, 1);
1086 * Get one object from the mempool (multi-consumers safe).
1088 * If cache is enabled, objects will be retrieved first from cache,
1089 * subsequently from the common pool. Note that it can return -ENOENT when
1090 * the local cache and common pool are empty, even if cache from other
1094 * A pointer to the mempool structure.
1096 * A pointer to a void * pointer (object) that will be filled.
1098 * - 0: Success; objects taken.
1099 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
1101 static inline int __attribute__((always_inline))
1102 rte_mempool_mc_get(struct rte_mempool *mp, void **obj_p)
1104 return rte_mempool_mc_get_bulk(mp, obj_p, 1);
1108 * Get one object from the mempool (NOT multi-consumers safe).
1110 * If cache is enabled, objects will be retrieved first from cache,
1111 * subsequently from the common pool. Note that it can return -ENOENT when
1112 * the local cache and common pool are empty, even if cache from other
1116 * A pointer to the mempool structure.
1118 * A pointer to a void * pointer (object) that will be filled.
1120 * - 0: Success; objects taken.
1121 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
1123 static inline int __attribute__((always_inline))
1124 rte_mempool_sc_get(struct rte_mempool *mp, void **obj_p)
1126 return rte_mempool_sc_get_bulk(mp, obj_p, 1);
1130 * Get one object from the mempool.
1132 * This function calls the multi-consumers or the single-consumer
1133 * version, depending on the default behavior that was specified at
1134 * mempool creation (see flags).
1136 * If cache is enabled, objects will be retrieved first from cache,
1137 * subsequently from the common pool. Note that it can return -ENOENT when
1138 * the local cache and common pool are empty, even if cache from other
1142 * A pointer to the mempool structure.
1144 * A pointer to a void * pointer (object) that will be filled.
1146 * - 0: Success; objects taken.
1147 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
1149 static inline int __attribute__((always_inline))
1150 rte_mempool_get(struct rte_mempool *mp, void **obj_p)
1152 return rte_mempool_get_bulk(mp, obj_p, 1);
1156 * Return the number of entries in the mempool.
1158 * When cache is enabled, this function has to browse the length of
1159 * all lcores, so it should not be used in a data path, but only for
1163 * A pointer to the mempool structure.
1165 * The number of entries in the mempool.
1167 unsigned rte_mempool_count(const struct rte_mempool *mp);
1170 * Return the number of free entries in the mempool ring.
1171 * i.e. how many entries can be freed back to the mempool.
1173 * NOTE: This corresponds to the number of elements *allocated* from the
1174 * memory pool, not the number of elements in the pool itself. To count
1175 * the number elements currently available in the pool, use "rte_mempool_count"
1177 * When cache is enabled, this function has to browse the length of
1178 * all lcores, so it should not be used in a data path, but only for
1182 * A pointer to the mempool structure.
1184 * The number of free entries in the mempool.
1186 static inline unsigned
1187 rte_mempool_free_count(const struct rte_mempool *mp)
1189 return mp->size - rte_mempool_count(mp);
1193 * Test if the mempool is full.
1195 * When cache is enabled, this function has to browse the length of all
1196 * lcores, so it should not be used in a data path, but only for debug
1200 * A pointer to the mempool structure.
1202 * - 1: The mempool is full.
1203 * - 0: The mempool is not full.
1206 rte_mempool_full(const struct rte_mempool *mp)
1208 return !!(rte_mempool_count(mp) == mp->size);
1212 * Test if the mempool is empty.
1214 * When cache is enabled, this function has to browse the length of all
1215 * lcores, so it should not be used in a data path, but only for debug
1219 * A pointer to the mempool structure.
1221 * - 1: The mempool is empty.
1222 * - 0: The mempool is not empty.
1225 rte_mempool_empty(const struct rte_mempool *mp)
1227 return !!(rte_mempool_count(mp) == 0);
1231 * Return the physical address of elt, which is an element of the pool mp.
1234 * A pointer to the mempool structure.
1236 * A pointer (virtual address) to the element of the pool.
1238 * The physical address of the elt element.
1240 static inline phys_addr_t
1241 rte_mempool_virt2phy(const struct rte_mempool *mp, const void *elt)
1243 if (rte_eal_has_hugepages()) {
1246 off = (const char *)elt - (const char *)mp->elt_va_start;
1247 return (mp->elt_pa[off >> mp->pg_shift] + (off & mp->pg_mask));
1250 * If huge pages are disabled, we cannot assume the
1251 * memory region to be physically contiguous.
1252 * Lookup for each element.
1254 return rte_mem_virt2phy(elt);
1259 * Check the consistency of mempool objects.
1261 * Verify the coherency of fields in the mempool structure. Also check
1262 * that the cookies of mempool objects (even the ones that are not
1263 * present in pool) have a correct value. If not, a panic will occur.
1266 * A pointer to the mempool structure.
1268 void rte_mempool_audit(const struct rte_mempool *mp);
1271 * Return a pointer to the private data in an mempool structure.
1274 * A pointer to the mempool structure.
1276 * A pointer to the private data.
1278 static inline void *rte_mempool_get_priv(struct rte_mempool *mp)
1280 return (char *)mp + MEMPOOL_HEADER_SIZE(mp, mp->pg_num);
1284 * Dump the status of all mempools on the console
1287 * A pointer to a file for output
1289 void rte_mempool_list_dump(FILE *f);
1292 * Search a mempool from its name
1295 * The name of the mempool.
1297 * The pointer to the mempool matching the name, or NULL if not found.
1299 * with rte_errno set appropriately. Possible rte_errno values include:
1300 * - ENOENT - required entry not available to return.
1303 struct rte_mempool *rte_mempool_lookup(const char *name);
1306 * Given a desired size of the mempool element and mempool flags,
1307 * caluclates header, trailer, body and total sizes of the mempool object.
1309 * The size of each element.
1311 * The flags used for the mempool creation.
1312 * Consult rte_mempool_create() for more information about possible values.
1313 * The size of each element.
1315 * The calculated detailed size the mempool object. May be NULL.
1317 * Total size of the mempool object.
1319 uint32_t rte_mempool_calc_obj_size(uint32_t elt_size, uint32_t flags,
1320 struct rte_mempool_objsz *sz);
1323 * Calculate maximum amount of memory required to store given number of objects.
1324 * Assumes that the memory buffer will be aligned at page boundary.
1325 * Note, that if object size is bigger then page size, then it assumes that
1326 * we have a subsets of physically continuous pages big enough to store
1327 * at least one object.
1329 * Number of elements.
1331 * The size of each element.
1333 * LOG2 of the physical pages size.
1335 * Required memory size aligned at page boundary.
1337 size_t rte_mempool_xmem_size(uint32_t elt_num, size_t elt_sz,
1341 * Calculate how much memory would be actually required with the given
1342 * memory footprint to store required number of objects.
1344 * Virtual address of the externally allocated memory buffer.
1345 * Will be used to store mempool objects.
1347 * Number of elements.
1349 * The size of each element.
1351 * Array of phyiscall addresses of the pages that comprises given memory
1354 * Number of elements in the paddr array.
1356 * LOG2 of the physical pages size.
1358 * Number of bytes needed to store given number of objects,
1359 * aligned to the given page size.
1360 * If provided memory buffer is not big enough:
1361 * (-1) * actual number of elemnts that can be stored in that buffer.
1363 ssize_t rte_mempool_xmem_usage(void *vaddr, uint32_t elt_num, size_t elt_sz,
1364 const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift);
1367 * Walk list of all memory pools
1372 * Argument passed to iterator
1374 void rte_mempool_walk(void (*func)(const struct rte_mempool *, void *arg),
1381 #endif /* _RTE_MEMPOOL_H_ */