4 * Copyright(c) 2010-2013 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.
35 #ifndef _RTE_MEMPOOL_H_
36 #define _RTE_MEMPOOL_H_
42 * A memory pool is an allocator of fixed-size object. It is
43 * identified by its name, and uses a ring to store free objects. It
44 * provides some other optional services, like a per-core object
45 * cache, and an alignment helper to ensure that objects are padded
46 * to spread them equally on all RAM channels, ranks, and so on.
48 * Objects owned by a mempool should never be added in another
49 * mempool. When an object is freed using rte_mempool_put() or
50 * equivalent, the object data is not modified; the user can save some
51 * meta-data in the object data and retrieve them when allocating a
54 * Note: the mempool implementation is not preemptable. A lcore must
55 * not be interrupted by another task that uses the same mempool
56 * (because it uses a ring which is not preemptable). Also, mempool
57 * functions must not be used outside the DPDK environment: for
58 * example, in linuxapp environment, a thread that is not created by
59 * the EAL must not use mempools. This is due to the per-lcore cache
60 * that won't work as rte_lcore_id() will not return a correct value.
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 #define RTE_MEMPOOL_NAMESIZE 32 /**< Maximum length of a memory pool. */
115 * The RTE mempool structure.
118 TAILQ_ENTRY(rte_mempool) next; /**< Next in list. */
120 char name[RTE_MEMPOOL_NAMESIZE]; /**< Name of mempool. */
121 struct rte_ring *ring; /**< Ring to store objects. */
122 phys_addr_t phys_addr; /**< Phys. addr. of mempool struct. */
123 int flags; /**< Flags of the mempool. */
124 uint32_t size; /**< Size of the mempool. */
125 uint32_t cache_size; /**< Size of per-lcore local cache. */
126 uint32_t cache_flushthresh; /**< Threshold before we flush excess elements. */
128 uint32_t elt_size; /**< Size of an element. */
129 uint32_t header_size; /**< Size of header (before elt). */
130 uint32_t trailer_size; /**< Size of trailer (after elt). */
132 unsigned private_data_size; /**< Size of private data. */
134 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
135 /** Per-lcore local cache. */
136 struct rte_mempool_cache local_cache[RTE_MAX_LCORE];
139 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
140 /** Per-lcore statistics. */
141 struct rte_mempool_debug_stats stats[RTE_MAX_LCORE];
143 } __rte_cache_aligned;
145 #define MEMPOOL_F_NO_SPREAD 0x0001 /**< Do not spread in memory. */
146 #define MEMPOOL_F_NO_CACHE_ALIGN 0x0002 /**< Do not align objs on cache lines.*/
147 #define MEMPOOL_F_SP_PUT 0x0004 /**< Default put is "single-producer".*/
148 #define MEMPOOL_F_SC_GET 0x0008 /**< Default get is "single-consumer".*/
151 * @internal When debug is enabled, store some statistics.
153 * Pointer to the memory pool.
155 * Name of the statistics field to increment in the memory pool.
157 * Number to add to the object-oriented statistics.
159 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
160 #define __MEMPOOL_STAT_ADD(mp, name, n) do { \
161 unsigned __lcore_id = rte_lcore_id(); \
162 mp->stats[__lcore_id].name##_objs += n; \
163 mp->stats[__lcore_id].name##_bulk += 1; \
166 #define __MEMPOOL_STAT_ADD(mp, name, n) do {} while(0)
170 * @internal Get a pointer to a mempool pointer in the object header.
174 * The pointer to the mempool from which the object was allocated.
176 static inline struct rte_mempool **__mempool_from_obj(void *obj)
178 struct rte_mempool **mpp;
181 off = sizeof(struct rte_mempool *);
182 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
183 off += sizeof(uint64_t);
185 mpp = (struct rte_mempool **)((char *)obj - off);
190 * Return a pointer to the mempool owning this object.
193 * An object that is owned by a pool. If this is not the case,
194 * the behavior is undefined.
196 * A pointer to the mempool structure.
198 static inline const struct rte_mempool *rte_mempool_from_obj(void *obj)
200 struct rte_mempool * const *mpp;
201 mpp = __mempool_from_obj(obj);
205 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
206 /* get header cookie value */
207 static inline uint64_t __mempool_read_header_cookie(const void *obj)
209 return *(const uint64_t *)((const char *)obj - sizeof(uint64_t));
212 /* get trailer cookie value */
213 static inline uint64_t __mempool_read_trailer_cookie(void *obj)
215 struct rte_mempool **mpp = __mempool_from_obj(obj);
216 return *(uint64_t *)((char *)obj + (*mpp)->elt_size);
219 /* write header cookie value */
220 static inline void __mempool_write_header_cookie(void *obj, int free)
223 cookie_p = (uint64_t *)((char *)obj - sizeof(uint64_t));
225 *cookie_p = RTE_MEMPOOL_HEADER_COOKIE1;
227 *cookie_p = RTE_MEMPOOL_HEADER_COOKIE2;
231 /* write trailer cookie value */
232 static inline void __mempool_write_trailer_cookie(void *obj)
235 struct rte_mempool **mpp = __mempool_from_obj(obj);
236 cookie_p = (uint64_t *)((char *)obj + (*mpp)->elt_size);
237 *cookie_p = RTE_MEMPOOL_TRAILER_COOKIE;
239 #endif /* RTE_LIBRTE_MEMPOOL_DEBUG */
242 * @internal Check and update cookies or panic.
245 * Pointer to the memory pool.
246 * @param obj_table_const
247 * Pointer to a table of void * pointers (objects).
249 * Index of object in object table.
251 * - 0: object is supposed to be allocated, mark it as free
252 * - 1: object is supposed to be free, mark it as allocated
253 * - 2: just check that cookie is valid (free or allocated)
255 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
256 #ifndef __INTEL_COMPILER
257 #pragma GCC diagnostic ignored "-Wcast-qual"
259 static inline void __mempool_check_cookies(const struct rte_mempool *mp,
260 void * const *obj_table_const,
261 unsigned n, int free)
268 /* Force to drop the "const" attribute. This is done only when
269 * DEBUG is enabled */
270 tmp = (void *) obj_table_const;
271 obj_table = (void **) tmp;
276 if (rte_mempool_from_obj(obj) != mp)
277 rte_panic("MEMPOOL: object is owned by another "
280 cookie = __mempool_read_header_cookie(obj);
283 if (cookie != RTE_MEMPOOL_HEADER_COOKIE1) {
284 rte_log_set_history(0);
285 RTE_LOG(CRIT, MEMPOOL,
286 "obj=%p, mempool=%p, cookie=%"PRIx64"\n",
288 rte_panic("MEMPOOL: bad header cookie (put)\n");
290 __mempool_write_header_cookie(obj, 1);
292 else if (free == 1) {
293 if (cookie != RTE_MEMPOOL_HEADER_COOKIE2) {
294 rte_log_set_history(0);
295 RTE_LOG(CRIT, MEMPOOL,
296 "obj=%p, mempool=%p, cookie=%"PRIx64"\n",
298 rte_panic("MEMPOOL: bad header cookie (get)\n");
300 __mempool_write_header_cookie(obj, 0);
302 else if (free == 2) {
303 if (cookie != RTE_MEMPOOL_HEADER_COOKIE1 &&
304 cookie != RTE_MEMPOOL_HEADER_COOKIE2) {
305 rte_log_set_history(0);
306 RTE_LOG(CRIT, MEMPOOL,
307 "obj=%p, mempool=%p, cookie=%"PRIx64"\n",
309 rte_panic("MEMPOOL: bad header cookie (audit)\n");
312 cookie = __mempool_read_trailer_cookie(obj);
313 if (cookie != RTE_MEMPOOL_TRAILER_COOKIE) {
314 rte_log_set_history(0);
315 RTE_LOG(CRIT, MEMPOOL,
316 "obj=%p, mempool=%p, cookie=%"PRIx64"\n",
318 rte_panic("MEMPOOL: bad trailer cookie\n");
322 #ifndef __INTEL_COMPILER
323 #pragma GCC diagnostic error "-Wcast-qual"
326 #define __mempool_check_cookies(mp, obj_table_const, n, free) do {} while(0)
327 #endif /* RTE_LIBRTE_MEMPOOL_DEBUG */
330 * An object constructor callback function for mempool.
332 * Arguments are the mempool, the opaque pointer given by the user in
333 * rte_mempool_create(), the pointer to the element and the index of
334 * the element in the pool.
336 typedef void (rte_mempool_obj_ctor_t)(struct rte_mempool *, void *,
340 * A mempool constructor callback function.
342 * Arguments are the mempool and the opaque pointer given by the user in
343 * rte_mempool_create().
345 typedef void (rte_mempool_ctor_t)(struct rte_mempool *, void *);
348 * Creates a new mempool named *name* in memory.
350 * This function uses ``memzone_reserve()`` to allocate memory. The
351 * pool contains n elements of elt_size. Its size is set to n.
354 * The name of the mempool.
356 * The number of elements in the mempool. The optimum size (in terms of
357 * memory usage) for a mempool is when n is a power of two minus one:
360 * The size of each element.
362 * If cache_size is non-zero, the rte_mempool library will try to
363 * limit the accesses to the common lockless pool, by maintaining a
364 * per-lcore object cache. This argument must be lower or equal to
365 * CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE. It is advised to choose
366 * cache_size to have "n modulo cache_size == 0": if this is
367 * not the case, some elements will always stay in the pool and will
368 * never be used. The access to the per-lcore table is of course
369 * faster than the multi-producer/consumer pool. The cache can be
370 * disabled if the cache_size argument is set to 0; it can be useful to
371 * avoid loosing objects in cache. Note that even if not used, the
372 * memory space for cache is always reserved in a mempool structure,
373 * except if CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE is set to 0.
374 * @param private_data_size
375 * The size of the private data appended after the mempool
376 * structure. This is useful for storing some private data after the
377 * mempool structure, as is done for rte_mbuf_pool for example.
379 * A function pointer that is called for initialization of the pool,
380 * before object initialization. The user can initialize the private
381 * data in this function if needed. This parameter can be NULL if
384 * An opaque pointer to data that can be used in the mempool
385 * constructor function.
387 * A function pointer that is called for each object at
388 * initialization of the pool. The user can set some meta data in
389 * objects if needed. This parameter can be NULL if not needed.
390 * The obj_init() function takes the mempool pointer, the init_arg,
391 * the object pointer and the object number as parameters.
392 * @param obj_init_arg
393 * An opaque pointer to data that can be used as an argument for
394 * each call to the object constructor function.
396 * The *socket_id* argument is the socket identifier in the case of
397 * NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
398 * constraint for the reserved zone.
400 * The *flags* arguments is an OR of following flags:
401 * - MEMPOOL_F_NO_SPREAD: By default, objects addresses are spread
402 * between channels in RAM: the pool allocator will add padding
403 * between objects depending on the hardware configuration. See
404 * Memory alignment constraints for details. If this flag is set,
405 * the allocator will just align them to a cache line.
406 * - MEMPOOL_F_NO_CACHE_ALIGN: By default, the returned objects are
407 * cache-aligned. This flag removes this constraint, and no
408 * padding will be present between objects. This flag implies
409 * MEMPOOL_F_NO_SPREAD.
410 * - MEMPOOL_F_SP_PUT: If this flag is set, the default behavior
411 * when using rte_mempool_put() or rte_mempool_put_bulk() is
412 * "single-producer". Otherwise, it is "multi-producers".
413 * - MEMPOOL_F_SC_GET: If this flag is set, the default behavior
414 * when using rte_mempool_get() or rte_mempool_get_bulk() is
415 * "single-consumer". Otherwise, it is "multi-consumers".
417 * The pointer to the new allocated mempool, on success. NULL on error
418 * with rte_errno set appropriately. Possible rte_errno values include:
419 * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
420 * - E_RTE_SECONDARY - function was called from a secondary process instance
421 * - E_RTE_NO_TAILQ - no tailq list could be got for the ring or mempool list
422 * - EINVAL - cache size provided is too large
423 * - ENOSPC - the maximum number of memzones has already been allocated
424 * - EEXIST - a memzone with the same name already exists
425 * - ENOMEM - no appropriate memory area found in which to create memzone
428 rte_mempool_create(const char *name, unsigned n, unsigned elt_size,
429 unsigned cache_size, unsigned private_data_size,
430 rte_mempool_ctor_t *mp_init, void *mp_init_arg,
431 rte_mempool_obj_ctor_t *obj_init, void *obj_init_arg,
432 int socket_id, unsigned flags);
435 * Dump the status of the mempool to the console.
438 * A pointer to the mempool structure.
440 void rte_mempool_dump(const struct rte_mempool *mp);
443 * @internal Put several objects back in the mempool; used internally.
445 * A pointer to the mempool structure.
447 * A pointer to a table of void * pointers (objects).
449 * The number of objects to store back in the mempool, must be strictly
452 * Mono-producer (0) or multi-producers (1).
454 static inline void __attribute__((always_inline))
455 __mempool_put_bulk(struct rte_mempool *mp, void * const *obj_table,
456 unsigned n, int is_mp)
458 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
459 struct rte_mempool_cache *cache;
462 unsigned lcore_id = rte_lcore_id();
463 uint32_t cache_size = mp->cache_size;
464 uint32_t flushthresh = mp->cache_flushthresh;
465 #endif /* RTE_MEMPOOL_CACHE_MAX_SIZE > 0 */
467 /* increment stat now, adding in mempool always success */
468 __MEMPOOL_STAT_ADD(mp, put, n);
470 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
471 /* cache is not enabled or single producer */
472 if (unlikely(cache_size == 0 || is_mp == 0))
475 /* Go straight to ring if put would overflow mem allocated for cache */
476 if (unlikely(n > RTE_MEMPOOL_CACHE_MAX_SIZE))
479 cache = &mp->local_cache[lcore_id];
480 cache_objs = &cache->objs[cache->len];
483 * The cache follows the following algorithm
484 * 1. Add the objects to the cache
485 * 2. Anything greater than the cache min value (if it crosses the
486 * cache flush threshold) is flushed to the ring.
489 /* Add elements back into the cache */
490 for (index = 0; index < n; ++index, obj_table++)
491 cache_objs[index] = *obj_table;
495 if (cache->len >= flushthresh) {
496 rte_ring_mp_enqueue_bulk(mp->ring, &cache->objs[cache_size],
497 cache->len - cache_size);
498 cache->len = cache_size;
504 #endif /* RTE_MEMPOOL_CACHE_MAX_SIZE > 0 */
506 /* push remaining objects in ring */
507 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
509 if (rte_ring_mp_enqueue_bulk(mp->ring, obj_table, n) < 0)
510 rte_panic("cannot put objects in mempool\n");
513 if (rte_ring_sp_enqueue_bulk(mp->ring, obj_table, n) < 0)
514 rte_panic("cannot put objects in mempool\n");
518 rte_ring_mp_enqueue_bulk(mp->ring, obj_table, n);
520 rte_ring_sp_enqueue_bulk(mp->ring, obj_table, n);
526 * Put several objects back in the mempool (multi-producers safe).
529 * A pointer to the mempool structure.
531 * A pointer to a table of void * pointers (objects).
533 * The number of objects to add in the mempool from the obj_table.
535 static inline void __attribute__((always_inline))
536 rte_mempool_mp_put_bulk(struct rte_mempool *mp, void * const *obj_table,
539 __mempool_check_cookies(mp, obj_table, n, 0);
540 __mempool_put_bulk(mp, obj_table, n, 1);
544 * Put several objects back in the mempool (NOT multi-producers safe).
547 * A pointer to the mempool structure.
549 * A pointer to a table of void * pointers (objects).
551 * The number of objects to add in the mempool from obj_table.
554 rte_mempool_sp_put_bulk(struct rte_mempool *mp, void * const *obj_table,
557 __mempool_check_cookies(mp, obj_table, n, 0);
558 __mempool_put_bulk(mp, obj_table, n, 0);
562 * Put several objects back in the mempool.
564 * This function calls the multi-producer or the single-producer
565 * version depending on the default behavior that was specified at
566 * mempool creation time (see flags).
569 * A pointer to the mempool structure.
571 * A pointer to a table of void * pointers (objects).
573 * The number of objects to add in the mempool from obj_table.
575 static inline void __attribute__((always_inline))
576 rte_mempool_put_bulk(struct rte_mempool *mp, void * const *obj_table,
579 __mempool_check_cookies(mp, obj_table, n, 0);
580 __mempool_put_bulk(mp, obj_table, n, !(mp->flags & MEMPOOL_F_SP_PUT));
584 * Put one object in the mempool (multi-producers safe).
587 * A pointer to the mempool structure.
589 * A pointer to the object to be added.
591 static inline void __attribute__((always_inline))
592 rte_mempool_mp_put(struct rte_mempool *mp, void *obj)
594 rte_mempool_mp_put_bulk(mp, &obj, 1);
598 * Put one object back in the mempool (NOT multi-producers safe).
601 * A pointer to the mempool structure.
603 * A pointer to the object to be added.
605 static inline void __attribute__((always_inline))
606 rte_mempool_sp_put(struct rte_mempool *mp, void *obj)
608 rte_mempool_sp_put_bulk(mp, &obj, 1);
612 * Put one object back in the mempool.
614 * This function calls the multi-producer or the single-producer
615 * version depending on the default behavior that was specified at
616 * mempool creation time (see flags).
619 * A pointer to the mempool structure.
621 * A pointer to the object to be added.
623 static inline void __attribute__((always_inline))
624 rte_mempool_put(struct rte_mempool *mp, void *obj)
626 rte_mempool_put_bulk(mp, &obj, 1);
630 * @internal Get several objects from the mempool; used internally.
632 * A pointer to the mempool structure.
634 * A pointer to a table of void * pointers (objects).
636 * The number of objects to get, must be strictly positive.
638 * Mono-consumer (0) or multi-consumers (1).
640 * - >=0: Success; number of objects supplied.
641 * - <0: Error; code of ring dequeue function.
643 static inline int __attribute__((always_inline))
644 __mempool_get_bulk(struct rte_mempool *mp, void **obj_table,
645 unsigned n, int is_mc)
648 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
651 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
652 struct rte_mempool_cache *cache;
655 unsigned lcore_id = rte_lcore_id();
656 uint32_t cache_size = mp->cache_size;
658 cache = &mp->local_cache[lcore_id];
660 /* cache is not enabled or single consumer */
661 if (unlikely(cache_size == 0 || is_mc == 0 || n >= cache_size))
664 cache_objs = cache->objs;
666 /* Can this be satisfied from the cache? */
667 if (cache->len < n) {
668 /* No. Backfill the cache first, and then fill from it */
669 uint32_t req = n + (cache_size - cache->len);
671 /* How many do we require i.e. number to fill the cache + the request */
672 ret = rte_ring_mc_dequeue_bulk(mp->ring, &cache->objs[cache->len], req);
673 if (unlikely(ret < 0)) {
675 * In the offchance that we are buffer constrained,
676 * where we are not able to allocate cache + n, go to
677 * the ring directly. If that fails, we are truly out of
686 /* Now fill in the response ... */
687 for (index = 0, len = cache->len - 1; index < n; ++index, len--, obj_table++)
688 *obj_table = cache_objs[len];
692 __MEMPOOL_STAT_ADD(mp, get_success, n_orig);
697 #endif /* RTE_MEMPOOL_CACHE_MAX_SIZE > 0 */
699 /* get remaining objects from ring */
701 ret = rte_ring_mc_dequeue_bulk(mp->ring, obj_table, n);
703 ret = rte_ring_sc_dequeue_bulk(mp->ring, obj_table, n);
706 __MEMPOOL_STAT_ADD(mp, get_fail, n_orig);
708 __MEMPOOL_STAT_ADD(mp, get_success, n_orig);
714 * Get several objects from the mempool (multi-consumers safe).
716 * If cache is enabled, objects will be retrieved first from cache,
717 * subsequently from the common pool. Note that it can return -ENOENT when
718 * the local cache and common pool are empty, even if cache from other
722 * A pointer to the mempool structure.
724 * A pointer to a table of void * pointers (objects) that will be filled.
726 * The number of objects to get from mempool to obj_table.
728 * - 0: Success; objects taken.
729 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
731 static inline int __attribute__((always_inline))
732 rte_mempool_mc_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned n)
735 ret = __mempool_get_bulk(mp, obj_table, n, 1);
737 __mempool_check_cookies(mp, obj_table, n, 1);
742 * Get several objects from the mempool (NOT multi-consumers safe).
744 * If cache is enabled, objects will be retrieved first from cache,
745 * subsequently from the common pool. Note that it can return -ENOENT when
746 * the local cache and common pool are empty, even if cache from other
750 * A pointer to the mempool structure.
752 * A pointer to a table of void * pointers (objects) that will be filled.
754 * The number of objects to get from the mempool to obj_table.
756 * - 0: Success; objects taken.
757 * - -ENOENT: Not enough entries in the mempool; no object is
760 static inline int __attribute__((always_inline))
761 rte_mempool_sc_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned n)
764 ret = __mempool_get_bulk(mp, obj_table, n, 0);
766 __mempool_check_cookies(mp, obj_table, n, 1);
771 * Get several objects from the mempool.
773 * This function calls the multi-consumers or the single-consumer
774 * version, depending on the default behaviour that was specified at
775 * mempool creation time (see flags).
777 * If cache is enabled, objects will be retrieved first from cache,
778 * subsequently from the common pool. Note that it can return -ENOENT when
779 * the local cache and common pool are empty, even if cache from other
783 * A pointer to the mempool structure.
785 * A pointer to a table of void * pointers (objects) that will be filled.
787 * The number of objects to get from the mempool to obj_table.
789 * - 0: Success; objects taken
790 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
792 static inline int __attribute__((always_inline))
793 rte_mempool_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned n)
796 ret = __mempool_get_bulk(mp, obj_table, n,
797 !(mp->flags & MEMPOOL_F_SC_GET));
799 __mempool_check_cookies(mp, obj_table, n, 1);
804 * Get one object from the mempool (multi-consumers safe).
806 * If cache is enabled, objects will be retrieved first from cache,
807 * subsequently from the common pool. Note that it can return -ENOENT when
808 * the local cache and common pool are empty, even if cache from other
812 * A pointer to the mempool structure.
814 * A pointer to a void * pointer (object) that will be filled.
816 * - 0: Success; objects taken.
817 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
819 static inline int __attribute__((always_inline))
820 rte_mempool_mc_get(struct rte_mempool *mp, void **obj_p)
822 return rte_mempool_mc_get_bulk(mp, obj_p, 1);
826 * Get one object from the mempool (NOT multi-consumers safe).
828 * If cache is enabled, objects will be retrieved first from cache,
829 * subsequently from the common pool. Note that it can return -ENOENT when
830 * the local cache and common pool are empty, even if cache from other
834 * A pointer to the mempool structure.
836 * A pointer to a void * pointer (object) that will be filled.
838 * - 0: Success; objects taken.
839 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
841 static inline int __attribute__((always_inline))
842 rte_mempool_sc_get(struct rte_mempool *mp, void **obj_p)
844 return rte_mempool_sc_get_bulk(mp, obj_p, 1);
848 * Get one object from the mempool.
850 * This function calls the multi-consumers or the single-consumer
851 * version, depending on the default behavior that was specified at
852 * mempool creation (see flags).
854 * If cache is enabled, objects will be retrieved first from cache,
855 * subsequently from the common pool. Note that it can return -ENOENT when
856 * the local cache and common pool are empty, even if cache from other
860 * A pointer to the mempool structure.
862 * A pointer to a void * pointer (object) that will be filled.
864 * - 0: Success; objects taken.
865 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
867 static inline int __attribute__((always_inline))
868 rte_mempool_get(struct rte_mempool *mp, void **obj_p)
870 return rte_mempool_get_bulk(mp, obj_p, 1);
874 * Return the number of entries in the mempool.
876 * When cache is enabled, this function has to browse the length of
877 * all lcores, so it should not be used in a data path, but only for
881 * A pointer to the mempool structure.
883 * The number of entries in the mempool.
885 unsigned rte_mempool_count(const struct rte_mempool *mp);
888 * Return the number of free entries in the mempool ring.
889 * i.e. how many entries can be freed back to the mempool.
891 * NOTE: This corresponds to the number of elements *allocated* from the
892 * memory pool, not the number of elements in the pool itself. To count
893 * the number elements currently available in the pool, use "rte_mempool_count"
895 * When cache is enabled, this function has to browse the length of
896 * all lcores, so it should not be used in a data path, but only for
900 * A pointer to the mempool structure.
902 * The number of free entries in the mempool.
904 static inline unsigned
905 rte_mempool_free_count(const struct rte_mempool *mp)
907 return mp->size - rte_mempool_count(mp);
911 * Test if the mempool is full.
913 * When cache is enabled, this function has to browse the length of all
914 * lcores, so it should not be used in a data path, but only for debug
918 * A pointer to the mempool structure.
920 * - 1: The mempool is full.
921 * - 0: The mempool is not full.
924 rte_mempool_full(const struct rte_mempool *mp)
926 return !!(rte_mempool_count(mp) == mp->size);
930 * Test if the mempool is empty.
932 * When cache is enabled, this function has to browse the length of all
933 * lcores, so it should not be used in a data path, but only for debug
937 * A pointer to the mempool structure.
939 * - 1: The mempool is empty.
940 * - 0: The mempool is not empty.
943 rte_mempool_empty(const struct rte_mempool *mp)
945 return !!(rte_mempool_count(mp) == 0);
949 * Return the physical address of elt, which is an element of the pool mp.
952 * A pointer to the mempool structure.
954 * A pointer (virtual address) to the element of the pool.
956 * The physical address of the elt element.
958 static inline phys_addr_t rte_mempool_virt2phy(const struct rte_mempool *mp,
963 off = (const char *)elt - (const char *)mp;
964 return mp->phys_addr + off;
969 * Check the consistency of mempool objects.
971 * Verify the coherency of fields in the mempool structure. Also check
972 * that the cookies of mempool objects (even the ones that are not
973 * present in pool) have a correct value. If not, a panic will occur.
976 * A pointer to the mempool structure.
978 void rte_mempool_audit(const struct rte_mempool *mp);
981 * Return a pointer to the private data in an mempool structure.
984 * A pointer to the mempool structure.
986 * A pointer to the private data.
988 static inline void *rte_mempool_get_priv(struct rte_mempool *mp)
990 return (char *)mp + sizeof(struct rte_mempool);
994 * Dump the status of all mempools on the console
996 void rte_mempool_list_dump(void);
999 * Search a mempool from its name
1002 * The name of the mempool.
1004 * The pointer to the mempool matching the name, or NULL if not found.NULL on error
1005 * with rte_errno set appropriately. Possible rte_errno values include:
1006 * - ENOENT - required entry not available to return.
1009 struct rte_mempool *rte_mempool_lookup(const char *name);
1015 #endif /* _RTE_MEMPOOL_H_ */