4 * Copyright(c) 2010-2012 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.
33 * version: DPDK.L.1.2.3-3
36 #ifndef _RTE_MEMPOOL_H_
37 #define _RTE_MEMPOOL_H_
43 * A memory pool is an allocator of fixed-size object. It is
44 * identified by its name, and uses a ring to store free objects. It
45 * provides some other optional services, like a per-core object
46 * cache, and an alignment helper to ensure that objects are padded
47 * to spread them equally on all RAM channels, ranks, and so on.
49 * Objects owned by a mempool should never be added in another
50 * mempool. When an object is freed using rte_mempool_put() or
51 * equivalent, the object data is not modified; the user can save some
52 * meta-data in the object data and retrieve them when allocating a
55 * Note: the mempool implementation is not preemptable. A lcore must
56 * not be interrupted by another task that uses the same mempool
57 * (because it uses a ring which is not preemptable). Also, mempool
58 * functions must not be used outside the DPDK environment: for
59 * example, in linuxapp environment, a thread that is not created by
60 * the EAL must not use mempools. This is due to the per-lcore cache
61 * that won't work as rte_lcore_id() will not return a correct value.
68 #include <sys/queue.h>
71 #include <rte_debug.h>
72 #include <rte_memory.h>
73 #include <rte_lcore.h>
74 #include <rte_branch_prediction.h>
81 #define RTE_MEMPOOL_HEADER_COOKIE1 0xbadbadbadadd2e55ULL /**< Header cookie. */
82 #define RTE_MEMPOOL_HEADER_COOKIE2 0xf2eef2eedadd2e55ULL /**< Header cookie. */
83 #define RTE_MEMPOOL_TRAILER_COOKIE 0xadd2e55badbadbadULL /**< Trailer cookie.*/
85 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
87 * A structure that stores the mempool statistics (per-lcore).
89 struct rte_mempool_debug_stats {
90 uint64_t put_bulk; /**< Number of puts. */
91 uint64_t put_objs; /**< Number of objects successfully put. */
92 uint64_t get_success_bulk; /**< Successful allocation number. */
93 uint64_t get_success_objs; /**< Objects successfully allocated. */
94 uint64_t get_fail_bulk; /**< Failed allocation number. */
95 uint64_t get_fail_objs; /**< Objects that failed to be allocated. */
96 } __rte_cache_aligned;
99 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
101 * A structure that stores a per-core object cache.
103 struct rte_mempool_cache {
104 unsigned len; /**< Cache len */
105 void *objs[RTE_MEMPOOL_CACHE_MAX_SIZE]; /**< Cache objects */
106 } __rte_cache_aligned;
107 #endif /* RTE_MEMPOOL_CACHE_MAX_SIZE > 0 */
109 #define RTE_MEMPOOL_NAMESIZE 32 /**< Maximum length of a memory pool. */
112 * The RTE mempool structure.
115 TAILQ_ENTRY(rte_mempool) next; /**< Next in list. */
117 char name[RTE_MEMPOOL_NAMESIZE]; /**< Name of mempool. */
118 struct rte_ring *ring; /**< Ring to store objects. */
119 phys_addr_t phys_addr; /**< Phys. addr. of mempool struct. */
120 int flags; /**< Flags of the mempool. */
121 uint32_t size; /**< Size of the mempool. */
122 uint32_t bulk_default; /**< Default bulk count. */
123 uint32_t cache_size; /**< Size of per-lcore local cache. */
125 uint32_t elt_size; /**< Size of an element. */
126 uint32_t header_size; /**< Size of header (before elt). */
127 uint32_t trailer_size; /**< Size of trailer (after elt). */
129 unsigned private_data_size; /**< Size of private data. */
131 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
132 /** Per-lcore local cache. */
133 struct rte_mempool_cache local_cache[RTE_MAX_LCORE];
136 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
137 /** Per-lcore statistics. */
138 struct rte_mempool_debug_stats stats[RTE_MAX_LCORE];
140 } __rte_cache_aligned;
142 #define MEMPOOL_F_NO_SPREAD 0x0001 /**< Do not spread in memory. */
143 #define MEMPOOL_F_NO_CACHE_ALIGN 0x0002 /**< Do not align objs on cache lines.*/
144 #define MEMPOOL_F_SP_PUT 0x0004 /**< Default put is "single-producer".*/
145 #define MEMPOOL_F_SC_GET 0x0008 /**< Default get is "single-consumer".*/
148 * When debug is enabled, store some statistics.
150 * Pointer to the memory pool.
152 * Name of the statistics field to increment in the memory pool.
154 * Number to add to the object-oriented statistics.
156 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
157 #define __MEMPOOL_STAT_ADD(mp, name, n) do { \
158 unsigned __lcore_id = rte_lcore_id(); \
159 mp->stats[__lcore_id].name##_objs += n; \
160 mp->stats[__lcore_id].name##_bulk += 1; \
163 #define __MEMPOOL_STAT_ADD(mp, name, n) do {} while(0)
167 * Get a pointer to a mempool pointer in the object header.
171 * The pointer to the mempool from which the object was allocated.
173 static inline struct rte_mempool **__mempool_from_obj(void *obj)
175 struct rte_mempool **mpp;
178 off = sizeof(struct rte_mempool *);
179 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
180 off += sizeof(uint64_t);
182 mpp = (struct rte_mempool **)((char *)obj - off);
187 * Return a pointer to the mempool owning this object.
190 * An object that is owned by a pool. If this is not the case,
191 * the behavior is undefined.
193 * A pointer to the mempool structure.
195 static inline const struct rte_mempool *rte_mempool_from_obj(void *obj)
197 struct rte_mempool * const *mpp;
198 mpp = __mempool_from_obj(obj);
202 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
203 /* get header cookie value */
204 static inline uint64_t __mempool_read_header_cookie(const void *obj)
206 return *(const uint64_t *)((const char *)obj - sizeof(uint64_t));
209 /* get trailer cookie value */
210 static inline uint64_t __mempool_read_trailer_cookie(void *obj)
212 struct rte_mempool **mpp = __mempool_from_obj(obj);
213 return *(uint64_t *)((char *)obj + (*mpp)->elt_size);
216 /* write header cookie value */
217 static inline void __mempool_write_header_cookie(void *obj, int free)
220 cookie_p = (uint64_t *)((char *)obj - sizeof(uint64_t));
222 *cookie_p = RTE_MEMPOOL_HEADER_COOKIE1;
224 *cookie_p = RTE_MEMPOOL_HEADER_COOKIE2;
228 /* write trailer cookie value */
229 static inline void __mempool_write_trailer_cookie(void *obj)
232 struct rte_mempool **mpp = __mempool_from_obj(obj);
233 cookie_p = (uint64_t *)((char *)obj + (*mpp)->elt_size);
234 *cookie_p = RTE_MEMPOOL_TRAILER_COOKIE;
236 #endif /* RTE_LIBRTE_MEMPOOL_DEBUG */
239 * Check and update cookies or panic.
242 * Pointer to the memory pool.
243 * @param obj_table_const
244 * Pointer to a table of void * pointers (objects).
246 * Index of object in object table.
248 * - 0: object is supposed to be allocated, mark it as free
249 * - 1: object is supposed to be free, mark it as allocated
250 * - 2: just check that cookie is valid (free or allocated)
252 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
253 #ifndef __INTEL_COMPILER
254 #pragma GCC diagnostic ignored "-Wcast-qual"
256 static inline void __mempool_check_cookies(const struct rte_mempool *mp,
257 void * const *obj_table_const,
258 unsigned n, int free)
265 /* Force to drop the "const" attribute. This is done only when
266 * DEBUG is enabled */
267 tmp = (void *) obj_table_const;
268 obj_table = (void **) tmp;
273 if (rte_mempool_from_obj(obj) != mp)
274 rte_panic("MEMPOOL: object is owned by another "
277 cookie = __mempool_read_header_cookie(obj);
280 if (cookie != RTE_MEMPOOL_HEADER_COOKIE1) {
281 rte_log_set_history(0);
282 RTE_LOG(CRIT, MEMPOOL,
283 "obj=%p, mempool=%p, cookie=%"PRIx64"\n",
285 rte_panic("MEMPOOL: bad header cookie (put)\n");
287 __mempool_write_header_cookie(obj, 1);
289 else if (free == 1) {
290 if (cookie != RTE_MEMPOOL_HEADER_COOKIE2) {
291 rte_log_set_history(0);
292 RTE_LOG(CRIT, MEMPOOL,
293 "obj=%p, mempool=%p, cookie=%"PRIx64"\n",
295 rte_panic("MEMPOOL: bad header cookie (get)\n");
297 __mempool_write_header_cookie(obj, 0);
299 else if (free == 2) {
300 if (cookie != RTE_MEMPOOL_HEADER_COOKIE1 &&
301 cookie != RTE_MEMPOOL_HEADER_COOKIE2) {
302 rte_log_set_history(0);
303 RTE_LOG(CRIT, MEMPOOL,
304 "obj=%p, mempool=%p, cookie=%"PRIx64"\n",
306 rte_panic("MEMPOOL: bad header cookie (audit)\n");
309 cookie = __mempool_read_trailer_cookie(obj);
310 if (cookie != RTE_MEMPOOL_TRAILER_COOKIE) {
311 rte_log_set_history(0);
312 RTE_LOG(CRIT, MEMPOOL,
313 "obj=%p, mempool=%p, cookie=%"PRIx64"\n",
315 rte_panic("MEMPOOL: bad trailer cookie\n");
319 #ifndef __INTEL_COMPILER
320 #pragma GCC diagnostic error "-Wcast-qual"
323 #define __mempool_check_cookies(mp, obj_table_const, n, free) do {} while(0)
324 #endif /* RTE_LIBRTE_MEMPOOL_DEBUG */
327 * An object constructor callback function for mempool.
329 * Arguments are the mempool, the opaque pointer given by the user in
330 * rte_mempool_create(), the pointer to the element and the index of
331 * the element in the pool.
333 typedef void (rte_mempool_obj_ctor_t)(struct rte_mempool *, void *,
337 * A mempool constructor callback function.
339 * Arguments are the mempool and the opaque pointer given by the user in
340 * rte_mempool_create().
342 typedef void (rte_mempool_ctor_t)(struct rte_mempool *, void *);
345 * Creates a new mempool named *name* in memory.
347 * This function uses ``memzone_reserve()`` to allocate memory. The
348 * pool contains n elements of elt_size. Its size is set to n. By
349 * default, bulk_default_count (the default number of elements to
350 * get/put in the pool) is set to 1. @see rte_mempool_set_bulk_count()
351 * to modify this valule.
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 * Set the default bulk count for put/get.
437 * The *count* parameter is the default number of bulk elements to
438 * get/put when using ``rte_mempool_*_{en,de}queue_bulk()``. It must
439 * be greater than 0 and less than half of the mempool size.
442 * A pointer to the mempool structure.
444 * A new water mark value.
446 * - 0: Success; default_bulk_count changed.
447 * - -EINVAL: Invalid count value.
450 rte_mempool_set_bulk_count(struct rte_mempool *mp, unsigned count)
452 if (unlikely(count == 0 || count >= mp->size))
455 mp->bulk_default = count;
460 * Get the default bulk count for put/get.
463 * A pointer to the mempool structure.
465 * The default bulk count for enqueue/dequeue.
467 static inline unsigned
468 rte_mempool_get_bulk_count(struct rte_mempool *mp)
470 return mp->bulk_default;
474 * Dump the status of the mempool to the console.
477 * A pointer to the mempool structure.
479 void rte_mempool_dump(const struct rte_mempool *mp);
482 * @internal Put several objects back in the mempool; used internally.
484 * A pointer to the mempool structure.
486 * A pointer to a table of void * pointers (objects).
488 * The number of objects to store back in the mempool, must be strictly
491 * Mono-producer (0) or multi-producers (1).
494 __mempool_put_bulk(struct rte_mempool *mp, void * const *obj_table,
495 unsigned n, int is_mp)
497 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
498 struct rte_mempool_cache *cache;
501 unsigned lcore_id = rte_lcore_id();
502 uint32_t cache_size = mp->cache_size;
503 uint32_t cache_add_count;
504 #endif /* RTE_MEMPOOL_CACHE_MAX_SIZE > 0 */
506 /* increment stat now, adding in mempool always success */
507 __MEMPOOL_STAT_ADD(mp, put, n);
509 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
510 /* cache is not enabled or single producer */
511 if (unlikely(cache_size == 0 || is_mp == 0))
514 cache = &mp->local_cache[lcore_id];
515 cache_len = cache->len;
516 cache_objs = cache->objs;
518 /* cache is full and we add many objects: enqueue in ring */
519 if (unlikely(cache_len == cache_size && n >= cache_size))
523 * cache is full and we add few objects: enqueue the content
524 * of the cache in ring
526 if (unlikely(cache_len == cache_size)) {
527 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
528 if (rte_ring_mp_enqueue_bulk(mp->ring, cache->objs,
530 rte_panic("cannot put objects in mempool\n");
532 rte_ring_mp_enqueue_bulk(mp->ring, cache->objs,
538 /* determine how many objects we can add in cache */
539 if (likely(n <= cache_size - cache_len))
542 cache_add_count = cache_size - cache_len;
544 /* add in cache while there is enough room */
545 while (cache_add_count > 0) {
546 cache_objs[cache_len] = *obj_table;
553 cache->len = cache_len;
555 /* no more object to add, return */
560 #endif /* RTE_MEMPOOL_CACHE_MAX_SIZE > 0 */
562 /* push remaining objects in ring */
563 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
565 if (rte_ring_mp_enqueue_bulk(mp->ring, obj_table, n) < 0)
566 rte_panic("cannot put objects in mempool\n");
569 if (rte_ring_sp_enqueue_bulk(mp->ring, obj_table, n) < 0)
570 rte_panic("cannot put objects in mempool\n");
574 rte_ring_mp_enqueue_bulk(mp->ring, obj_table, n);
576 rte_ring_sp_enqueue_bulk(mp->ring, obj_table, n);
582 * Put several objects back in the mempool (multi-producers safe).
585 * A pointer to the mempool structure.
587 * A pointer to a table of void * pointers (objects).
589 * The number of objects to add in the mempool from the obj_table.
592 rte_mempool_mp_put_bulk(struct rte_mempool *mp, void * const *obj_table,
595 __mempool_check_cookies(mp, obj_table, n, 0);
596 __mempool_put_bulk(mp, obj_table, n, 1);
600 * Put several objects back in the mempool (NOT multi-producers safe).
603 * A pointer to the mempool structure.
605 * A pointer to a table of void * pointers (objects).
607 * The number of objects to add in the mempool from obj_table.
610 rte_mempool_sp_put_bulk(struct rte_mempool *mp, void * const *obj_table,
613 __mempool_check_cookies(mp, obj_table, n, 0);
614 __mempool_put_bulk(mp, obj_table, n, 0);
618 * Put several objects back in the mempool.
620 * This function calls the multi-producer or the single-producer
621 * version depending on the default behavior that was specified at
622 * mempool creation time (see flags).
625 * A pointer to the mempool structure.
627 * A pointer to a table of void * pointers (objects).
629 * The number of objects to add in the mempool from obj_table.
632 rte_mempool_put_bulk(struct rte_mempool *mp, void * const *obj_table,
635 __mempool_check_cookies(mp, obj_table, n, 0);
636 __mempool_put_bulk(mp, obj_table, n, !(mp->flags & MEMPOOL_F_SP_PUT));
640 * Put one object in the mempool (multi-producers safe).
643 * A pointer to the mempool structure.
645 * A pointer to the object to be added.
648 rte_mempool_mp_put(struct rte_mempool *mp, void *obj)
650 rte_mempool_mp_put_bulk(mp, &obj, 1);
654 * Put one object back in the mempool (NOT multi-producers safe).
657 * A pointer to the mempool structure.
659 * A pointer to the object to be added.
662 rte_mempool_sp_put(struct rte_mempool *mp, void *obj)
664 rte_mempool_sp_put_bulk(mp, &obj, 1);
668 * Put one object back in the mempool.
670 * This function calls the multi-producer or the single-producer
671 * version depending on the default behavior that was specified at
672 * mempool creation time (see flags).
675 * A pointer to the mempool structure.
677 * A pointer to the object to be added.
680 rte_mempool_put(struct rte_mempool *mp, void *obj)
682 rte_mempool_put_bulk(mp, &obj, 1);
686 * @internal Get several objects from the mempool; used internally.
688 * A pointer to the mempool structure.
690 * A pointer to a table of void * pointers (objects).
692 * The number of objects to get, must be strictly positive.
694 * Mono-consumer (0) or multi-consumers (1).
696 * - >=0: Success; number of objects supplied.
697 * - <0: Error; code of ring dequeue function.
700 __mempool_get_bulk(struct rte_mempool *mp, void **obj_table,
701 unsigned n, int is_mc)
704 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
707 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
708 struct rte_mempool_cache *cache;
709 uint32_t cache_len, cache_len_save = 0;
711 unsigned lcore_id = rte_lcore_id();
712 uint32_t cache_size = mp->cache_size;
713 uint32_t cache_del_count;
715 cache = &mp->local_cache[lcore_id];
717 /* cache is not enabled or single consumer */
718 if (unlikely(cache_size == 0 || is_mc == 0))
721 cache_len = cache->len;
722 cache_objs = cache->objs;
724 /* cache is empty and we need many objects: dequeue from ring */
725 if (unlikely(cache_len == 0 && n >= cache_size))
728 /* cache is empty and we dequeue few objects: fill the cache first */
729 if (unlikely(cache_len == 0 && n < cache_size)) {
730 ret = rte_ring_mc_dequeue_bulk(mp->ring, cache_objs,
732 if (unlikely(ret < 0)) {
733 __MEMPOOL_STAT_ADD(mp, get_fail, n_orig);
737 cache_len = cache_size;
740 if (likely(n <= cache_len))
743 cache_del_count = cache_len;
745 cache_len_save = cache_len;
747 /* add in cache only while there is enough room */
748 while (cache_del_count > 0) {
750 *obj_table = cache_objs[cache_len];
756 cache->len = cache_len;
758 /* no more object to get, return */
759 if (likely(n == 0)) {
760 __MEMPOOL_STAT_ADD(mp, get_success, n_orig);
765 #endif /* RTE_MEMPOOL_CACHE_MAX_SIZE > 0 */
767 /* get remaining objects from ring */
769 ret = rte_ring_mc_dequeue_bulk(mp->ring, obj_table, n);
771 ret = rte_ring_sc_dequeue_bulk(mp->ring, obj_table, n);
773 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
775 * bad luck, the ring is empty but we already dequeued some
776 * entries from cache, we have to restore them
778 if (unlikely(ret < 0 && cache_len_save != 0))
779 cache->len = cache_len_save;
783 __MEMPOOL_STAT_ADD(mp, get_fail, n_orig);
785 __MEMPOOL_STAT_ADD(mp, get_success, n_orig);
791 * Get several objects from the mempool (multi-consumers safe).
793 * If cache is enabled, objects will be retrieved first from cache,
794 * subsequently from the common pool. Note that it can return -ENOENT when
795 * the local cache and common pool are empty, even if cache from other
799 * A pointer to the mempool structure.
801 * A pointer to a table of void * pointers (objects) that will be filled.
803 * The number of objects to get from mempool to obj_table.
805 * - 0: Success; objects taken.
806 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
809 rte_mempool_mc_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned n)
812 ret = __mempool_get_bulk(mp, obj_table, n, 1);
814 __mempool_check_cookies(mp, obj_table, n, 1);
819 * Get several objects from the mempool (NOT multi-consumers safe).
821 * If cache is enabled, objects will be retrieved first from cache,
822 * subsequently from the common pool. Note that it can return -ENOENT when
823 * the local cache and common pool are empty, even if cache from other
827 * A pointer to the mempool structure.
829 * A pointer to a table of void * pointers (objects) that will be filled.
831 * The number of objects to get from the mempool to obj_table.
833 * - 0: Success; objects taken.
834 * - -ENOENT: Not enough entries in the mempool; no object is
838 rte_mempool_sc_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned n)
841 ret = __mempool_get_bulk(mp, obj_table, n, 0);
843 __mempool_check_cookies(mp, obj_table, n, 1);
848 * Get several objects from the mempool.
850 * This function calls the multi-consumers or the single-consumer
851 * version, depending on the default behaviour that was specified at
852 * mempool creation time (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 table of void * pointers (objects) that will be filled.
864 * The number of objects to get from the mempool to obj_table.
866 * - 0: Success; objects taken
867 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
870 rte_mempool_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned n)
873 ret = __mempool_get_bulk(mp, obj_table, n,
874 !(mp->flags & MEMPOOL_F_SC_GET));
876 __mempool_check_cookies(mp, obj_table, n, 1);
881 * Get one object from the mempool (multi-consumers safe).
883 * If cache is enabled, objects will be retrieved first from cache,
884 * subsequently from the common pool. Note that it can return -ENOENT when
885 * the local cache and common pool are empty, even if cache from other
889 * A pointer to the mempool structure.
891 * A pointer to a void * pointer (object) that will be filled.
893 * - 0: Success; objects taken.
894 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
897 rte_mempool_mc_get(struct rte_mempool *mp, void **obj_p)
899 return rte_mempool_mc_get_bulk(mp, obj_p, 1);
903 * Get one object from the mempool (NOT multi-consumers safe).
905 * If cache is enabled, objects will be retrieved first from cache,
906 * subsequently from the common pool. Note that it can return -ENOENT when
907 * the local cache and common pool are empty, even if cache from other
911 * A pointer to the mempool structure.
913 * A pointer to a void * pointer (object) that will be filled.
915 * - 0: Success; objects taken.
916 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
919 rte_mempool_sc_get(struct rte_mempool *mp, void **obj_p)
921 return rte_mempool_sc_get_bulk(mp, obj_p, 1);
925 * Get one object from the mempool.
927 * This function calls the multi-consumers or the single-consumer
928 * version, depending on the default behavior that was specified at
929 * mempool creation (see flags).
931 * If cache is enabled, objects will be retrieved first from cache,
932 * subsequently from the common pool. Note that it can return -ENOENT when
933 * the local cache and common pool are empty, even if cache from other
937 * A pointer to the mempool structure.
939 * A pointer to a void * pointer (object) that will be filled.
941 * - 0: Success; objects taken.
942 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
945 rte_mempool_get(struct rte_mempool *mp, void **obj_p)
947 return rte_mempool_get_bulk(mp, obj_p, 1);
951 * Return the number of entries in the mempool.
953 * When cache is enabled, this function has to browse the length of
954 * all lcores, so it should not be used in a data path, but only for
958 * A pointer to the mempool structure.
960 * The number of entries in the mempool.
962 unsigned rte_mempool_count(const struct rte_mempool *mp);
965 * Return the number of free entries in the mempool.
967 * When cache is enabled, this function has to browse the length of
968 * all lcores, so it should not be used in a data path, but only for
972 * A pointer to the mempool structure.
974 * The number of free entries in the mempool.
976 static inline unsigned
977 rte_mempool_free_count(const struct rte_mempool *mp)
979 return mp->size - rte_mempool_count(mp);
983 * Test if the mempool is full.
985 * When cache is enabled, this function has to browse the length of all
986 * lcores, so it should not be used in a data path, but only for debug
990 * A pointer to the mempool structure.
992 * - 1: The mempool is full.
993 * - 0: The mempool is not full.
996 rte_mempool_full(const struct rte_mempool *mp)
998 return !!(rte_mempool_count(mp) == mp->size);
1002 * Test if the mempool is empty.
1004 * When cache is enabled, this function has to browse the length of all
1005 * lcores, so it should not be used in a data path, but only for debug
1009 * A pointer to the mempool structure.
1011 * - 1: The mempool is empty.
1012 * - 0: The mempool is not empty.
1015 rte_mempool_empty(const struct rte_mempool *mp)
1017 return !!(rte_mempool_count(mp) == 0);
1021 * Return the physical address of elt, which is an element of the pool mp.
1024 * A pointer to the mempool structure.
1026 * A pointer (virtual address) to the element of the pool.
1028 * The physical address of the elt element.
1030 static inline phys_addr_t rte_mempool_virt2phy(const struct rte_mempool *mp,
1035 off = (const char *)elt - (const char *)mp;
1036 return mp->phys_addr + off;
1041 * Check the consistency of mempool objects.
1043 * Verify the coherency of fields in the mempool structure. Also check
1044 * that the cookies of mempool objects (even the ones that are not
1045 * present in pool) have a correct value. If not, a panic will occur.
1048 * A pointer to the mempool structure.
1050 void rte_mempool_audit(const struct rte_mempool *mp);
1053 * Return a pointer to the private data in an mempool structure.
1056 * A pointer to the mempool structure.
1058 * A pointer to the private data.
1060 static inline void *rte_mempool_get_priv(struct rte_mempool *mp)
1062 return (char *)mp + sizeof(struct rte_mempool);
1066 * Dump the status of all mempools on the console
1068 void rte_mempool_list_dump(void);
1071 * Search a mempool from its name
1074 * The name of the mempool.
1076 * The pointer to the mempool matching the name, or NULL if not found.NULL on error
1077 * with rte_errno set appropriately. Possible rte_errno values include:
1078 * - ENOENT - required entry not available to return.
1081 struct rte_mempool *rte_mempool_lookup(const char *name);
1087 #endif /* _RTE_MEMPOOL_H_ */