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.
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_memory.h>
72 #include <rte_lcore.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 */
104 void *objs[RTE_MEMPOOL_CACHE_MAX_SIZE]; /**< Cache objects */
105 } __rte_cache_aligned;
106 #endif /* RTE_MEMPOOL_CACHE_MAX_SIZE > 0 */
108 #define RTE_MEMPOOL_NAMESIZE 32 /**< Maximum length of a memory pool. */
111 * The RTE mempool structure.
114 TAILQ_ENTRY(rte_mempool) next; /**< Next in list. */
116 char name[RTE_MEMPOOL_NAMESIZE]; /**< Name of mempool. */
117 struct rte_ring *ring; /**< Ring to store objects. */
118 phys_addr_t phys_addr; /**< Phys. addr. of mempool struct. */
119 int flags; /**< Flags of the mempool. */
120 uint32_t size; /**< Size of the mempool. */
121 uint32_t bulk_default; /**< Default bulk count. */
122 uint32_t cache_size; /**< Size of per-lcore local cache. */
124 uint32_t elt_size; /**< Size of an element. */
125 uint32_t header_size; /**< Size of header (before elt). */
126 uint32_t trailer_size; /**< Size of trailer (after elt). */
128 unsigned private_data_size; /**< Size of private data. */
130 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
131 /** Per-lcore local cache. */
132 struct rte_mempool_cache local_cache[RTE_MAX_LCORE];
135 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
136 /** Per-lcore statistics. */
137 struct rte_mempool_debug_stats stats[RTE_MAX_LCORE];
139 } __rte_cache_aligned;
141 #define MEMPOOL_F_NO_SPREAD 0x0001 /**< Do not spread in memory. */
142 #define MEMPOOL_F_NO_CACHE_ALIGN 0x0002 /**< Do not align objs on cache lines.*/
143 #define MEMPOOL_F_SP_PUT 0x0004 /**< Default put is "single-producer".*/
144 #define MEMPOOL_F_SC_GET 0x0008 /**< Default get is "single-consumer".*/
147 * When debug is enabled, store some statistics.
149 * Pointer to the memory pool.
151 * Name of the statistics field to increment in the memory pool.
153 * Number to add to the object-oriented statistics.
155 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
156 #define __MEMPOOL_STAT_ADD(mp, name, n) do { \
157 unsigned __lcore_id = rte_lcore_id(); \
158 mp->stats[__lcore_id].name##_objs += n; \
159 mp->stats[__lcore_id].name##_bulk += 1; \
162 #define __MEMPOOL_STAT_ADD(mp, name, n) do {} while(0)
166 * Get a pointer to a mempool pointer in the object header.
170 * The pointer to the mempool from which the object was allocated.
172 static inline struct rte_mempool **__mempool_from_obj(void *obj)
174 struct rte_mempool **mpp;
177 off = sizeof(struct rte_mempool *);
178 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
179 off += sizeof(uint64_t);
181 mpp = (struct rte_mempool **)((char *)obj - off);
186 * Return a pointer to the mempool owning this object.
189 * An object that is owned by a pool. If this is not the case,
190 * the behavior is undefined.
192 * A pointer to the mempool structure.
194 static inline const struct rte_mempool *rte_mempool_from_obj(void *obj)
196 struct rte_mempool * const *mpp;
197 mpp = __mempool_from_obj(obj);
201 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
202 /* get header cookie value */
203 static inline uint64_t __mempool_read_header_cookie(const void *obj)
205 return *(const uint64_t *)((const char *)obj - sizeof(uint64_t));
208 /* get trailer cookie value */
209 static inline uint64_t __mempool_read_trailer_cookie(void *obj)
211 struct rte_mempool **mpp = __mempool_from_obj(obj);
212 return *(uint64_t *)((char *)obj + (*mpp)->elt_size);
215 /* write header cookie value */
216 static inline void __mempool_write_header_cookie(void *obj, int free)
219 cookie_p = (uint64_t *)((char *)obj - sizeof(uint64_t));
221 *cookie_p = RTE_MEMPOOL_HEADER_COOKIE1;
223 *cookie_p = RTE_MEMPOOL_HEADER_COOKIE2;
227 /* write trailer cookie value */
228 static inline void __mempool_write_trailer_cookie(void *obj)
231 struct rte_mempool **mpp = __mempool_from_obj(obj);
232 cookie_p = (uint64_t *)((char *)obj + (*mpp)->elt_size);
233 *cookie_p = RTE_MEMPOOL_TRAILER_COOKIE;
235 #endif /* RTE_LIBRTE_MEMPOOL_DEBUG */
238 * Check and update cookies or panic.
241 * Pointer to the memory pool.
242 * @param obj_table_const
243 * Pointer to a table of void * pointers (objects).
245 * Index of object in object table.
247 * - 0: object is supposed to be allocated, mark it as free
248 * - 1: object is supposed to be free, mark it as allocated
249 * - 2: just check that cookie is valid (free or allocated)
251 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
252 #ifndef __INTEL_COMPILER
253 #pragma GCC diagnostic ignored "-Wcast-qual"
255 static inline void __mempool_check_cookies(const struct rte_mempool *mp,
256 void * const *obj_table_const,
257 unsigned n, int free)
264 /* Force to drop the "const" attribute. This is done only when
265 * DEBUG is enabled */
266 tmp = (void *) obj_table_const;
267 obj_table = (void **) tmp;
272 if (rte_mempool_from_obj(obj) != mp)
273 rte_panic("MEMPOOL: object is owned by another "
276 cookie = __mempool_read_header_cookie(obj);
279 if (cookie != RTE_MEMPOOL_HEADER_COOKIE1) {
280 rte_log_set_history(0);
281 RTE_LOG(CRIT, MEMPOOL,
282 "obj=%p, mempool=%p, cookie=%"PRIx64"\n",
284 rte_panic("MEMPOOL: bad header cookie (put)\n");
286 __mempool_write_header_cookie(obj, 1);
288 else if (free == 1) {
289 if (cookie != RTE_MEMPOOL_HEADER_COOKIE2) {
290 rte_log_set_history(0);
291 RTE_LOG(CRIT, MEMPOOL,
292 "obj=%p, mempool=%p, cookie=%"PRIx64"\n",
294 rte_panic("MEMPOOL: bad header cookie (get)\n");
296 __mempool_write_header_cookie(obj, 0);
298 else if (free == 2) {
299 if (cookie != RTE_MEMPOOL_HEADER_COOKIE1 &&
300 cookie != RTE_MEMPOOL_HEADER_COOKIE2) {
301 rte_log_set_history(0);
302 RTE_LOG(CRIT, MEMPOOL,
303 "obj=%p, mempool=%p, cookie=%"PRIx64"\n",
305 rte_panic("MEMPOOL: bad header cookie (audit)\n");
308 cookie = __mempool_read_trailer_cookie(obj);
309 if (cookie != RTE_MEMPOOL_TRAILER_COOKIE) {
310 rte_log_set_history(0);
311 RTE_LOG(CRIT, MEMPOOL,
312 "obj=%p, mempool=%p, cookie=%"PRIx64"\n",
314 rte_panic("MEMPOOL: bad trailer cookie\n");
318 #ifndef __INTEL_COMPILER
319 #pragma GCC diagnostic error "-Wcast-qual"
322 #define __mempool_check_cookies(mp, obj_table_const, n, free) do {} while(0)
323 #endif /* RTE_LIBRTE_MEMPOOL_DEBUG */
326 * An object constructor callback function for mempool.
328 * Arguments are the mempool, the opaque pointer given by the user in
329 * rte_mempool_create(), the pointer to the element and the index of
330 * the element in the pool.
332 typedef void (rte_mempool_obj_ctor_t)(struct rte_mempool *, void *,
336 * A mempool constructor callback function.
338 * Arguments are the mempool and the opaque pointer given by the user in
339 * rte_mempool_create().
341 typedef void (rte_mempool_ctor_t)(struct rte_mempool *, void *);
344 * Creates a new mempool named *name* in memory.
346 * This function uses ``memzone_reserve()`` to allocate memory. The
347 * pool contains n elements of elt_size. Its size is set to n. By
348 * default, bulk_default_count (the default number of elements to
349 * get/put in the pool) is set to 1. @see rte_mempool_set_bulk_count()
350 * to modify this valule.
353 * The name of the mempool.
355 * The number of elements in the mempool. The optimum size (in terms of
356 * memory usage) for a mempool is when n is a power of two minus one:
359 * The size of each element.
361 * If cache_size is non-zero, the rte_mempool library will try to
362 * limit the accesses to the common lockless pool, by maintaining a
363 * per-lcore object cache. This argument must be lower or equal to
364 * CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE. It is advised to choose
365 * cache_size to have "n modulo cache_size == 0": if this is
366 * not the case, some elements will always stay in the pool and will
367 * never be used. The access to the per-lcore table is of course
368 * faster than the multi-producer/consumer pool. The cache can be
369 * disabled if the cache_size argument is set to 0; it can be useful to
370 * avoid loosing objects in cache. Note that even if not used, the
371 * memory space for cache is always reserved in a mempool structure,
372 * except if CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE is set to 0.
373 * @param private_data_size
374 * The size of the private data appended after the mempool
375 * structure. This is useful for storing some private data after the
376 * mempool structure, as is done for rte_mbuf_pool for example.
378 * A function pointer that is called for initialization of the pool,
379 * before object initialization. The user can initialize the private
380 * data in this function if needed. This parameter can be NULL if
383 * An opaque pointer to data that can be used in the mempool
384 * constructor function.
386 * A function pointer that is called for each object at
387 * initialization of the pool. The user can set some meta data in
388 * objects if needed. This parameter can be NULL if not needed.
389 * The obj_init() function takes the mempool pointer, the init_arg,
390 * the object pointer and the object number as parameters.
391 * @param obj_init_arg
392 * An opaque pointer to data that can be used as an argument for
393 * each call to the object constructor function.
395 * The *socket_id* argument is the socket identifier in the case of
396 * NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
397 * constraint for the reserved zone.
399 * The *flags* arguments is an OR of following flags:
400 * - MEMPOOL_F_NO_SPREAD: By default, objects addresses are spread
401 * between channels in RAM: the pool allocator will add padding
402 * between objects depending on the hardware configuration. See
403 * Memory alignment constraints for details. If this flag is set,
404 * the allocator will just align them to a cache line.
405 * - MEMPOOL_F_NO_CACHE_ALIGN: By default, the returned objects are
406 * cache-aligned. This flag removes this constraint, and no
407 * padding will be present between objects. This flag implies
408 * MEMPOOL_F_NO_SPREAD.
409 * - MEMPOOL_F_SP_PUT: If this flag is set, the default behavior
410 * when using rte_mempool_put() or rte_mempool_put_bulk() is
411 * "single-producer". Otherwise, it is "multi-producers".
412 * - MEMPOOL_F_SC_GET: If this flag is set, the default behavior
413 * when using rte_mempool_get() or rte_mempool_get_bulk() is
414 * "single-consumer". Otherwise, it is "multi-consumers".
416 * The pointer to the new allocated mempool, on success. NULL on error
417 * with rte_errno set appropriately. Possible rte_errno values include:
418 * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
419 * - E_RTE_SECONDARY - function was called from a secondary process instance
420 * - E_RTE_NO_TAILQ - no tailq list could be got for the ring or mempool list
421 * - EINVAL - cache size provided is too large
422 * - ENOSPC - the maximum number of memzones has already been allocated
423 * - EEXIST - a memzone with the same name already exists
424 * - ENOMEM - no appropriate memory area found in which to create memzone
427 rte_mempool_create(const char *name, unsigned n, unsigned elt_size,
428 unsigned cache_size, unsigned private_data_size,
429 rte_mempool_ctor_t *mp_init, void *mp_init_arg,
430 rte_mempool_obj_ctor_t *obj_init, void *obj_init_arg,
431 int socket_id, unsigned flags);
434 * Set the default bulk count for put/get.
436 * The *count* parameter is the default number of bulk elements to
437 * get/put when using ``rte_mempool_*_{en,de}queue_bulk()``. It must
438 * be greater than 0 and less than half of the mempool size.
441 * A pointer to the mempool structure.
443 * A new water mark value.
445 * - 0: Success; default_bulk_count changed.
446 * - -EINVAL: Invalid count value.
449 rte_mempool_set_bulk_count(struct rte_mempool *mp, unsigned count)
451 if (unlikely(count == 0 || count >= mp->size))
454 mp->bulk_default = count;
459 * Get the default bulk count for put/get.
462 * A pointer to the mempool structure.
464 * The default bulk count for enqueue/dequeue.
466 static inline unsigned
467 rte_mempool_get_bulk_count(struct rte_mempool *mp)
469 return mp->bulk_default;
473 * Dump the status of the mempool to the console.
476 * A pointer to the mempool structure.
478 void rte_mempool_dump(const struct rte_mempool *mp);
481 * @internal Put several objects back in the mempool; used internally.
483 * A pointer to the mempool structure.
485 * A pointer to a table of void * pointers (objects).
487 * The number of objects to store back in the mempool, must be strictly
490 * Mono-producer (0) or multi-producers (1).
493 __mempool_put_bulk(struct rte_mempool *mp, void * const *obj_table,
494 unsigned n, int is_mp)
496 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
497 struct rte_mempool_cache *cache;
500 unsigned lcore_id = rte_lcore_id();
501 uint32_t cache_size = mp->cache_size;
502 uint32_t cache_add_count;
503 #endif /* RTE_MEMPOOL_CACHE_MAX_SIZE > 0 */
505 /* increment stat now, adding in mempool always success */
506 __MEMPOOL_STAT_ADD(mp, put, n);
508 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
509 /* cache is not enabled or single producer */
510 if (unlikely(cache_size == 0 || is_mp == 0))
513 cache = &mp->local_cache[lcore_id];
514 cache_len = cache->len;
515 cache_objs = cache->objs;
517 /* cache is full and we add many objects: enqueue in ring */
518 if (unlikely(cache_len == cache_size && n >= cache_size))
522 * cache is full and we add few objects: enqueue the content
523 * of the cache in ring
525 if (unlikely(cache_len == cache_size)) {
526 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
527 if (rte_ring_mp_enqueue_bulk(mp->ring, cache->objs,
529 rte_panic("cannot put objects in mempool\n");
531 rte_ring_mp_enqueue_bulk(mp->ring, cache->objs,
537 /* determine how many objects we can add in cache */
538 if (likely(n <= cache_size - cache_len))
541 cache_add_count = cache_size - cache_len;
543 /* add in cache while there is enough room */
544 while (cache_add_count > 0) {
545 cache_objs[cache_len] = *obj_table;
552 cache->len = cache_len;
554 /* no more object to add, return */
559 #endif /* RTE_MEMPOOL_CACHE_MAX_SIZE > 0 */
561 /* push remaining objects in ring */
562 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
564 if (rte_ring_mp_enqueue_bulk(mp->ring, obj_table, n) < 0)
565 rte_panic("cannot put objects in mempool\n");
568 if (rte_ring_sp_enqueue_bulk(mp->ring, obj_table, n) < 0)
569 rte_panic("cannot put objects in mempool\n");
573 rte_ring_mp_enqueue_bulk(mp->ring, obj_table, n);
575 rte_ring_sp_enqueue_bulk(mp->ring, obj_table, n);
581 * Put several objects back in the mempool (multi-producers safe).
584 * A pointer to the mempool structure.
586 * A pointer to a table of void * pointers (objects).
588 * The number of objects to add in the mempool from the obj_table.
591 rte_mempool_mp_put_bulk(struct rte_mempool *mp, void * const *obj_table,
594 __mempool_check_cookies(mp, obj_table, n, 0);
595 __mempool_put_bulk(mp, obj_table, n, 1);
599 * Put several objects back in the mempool (NOT multi-producers safe).
602 * A pointer to the mempool structure.
604 * A pointer to a table of void * pointers (objects).
606 * The number of objects to add in the mempool from obj_table.
609 rte_mempool_sp_put_bulk(struct rte_mempool *mp, void * const *obj_table,
612 __mempool_check_cookies(mp, obj_table, n, 0);
613 __mempool_put_bulk(mp, obj_table, n, 0);
617 * Put several objects back in the mempool.
619 * This function calls the multi-producer or the single-producer
620 * version depending on the default behavior that was specified at
621 * mempool creation time (see flags).
624 * A pointer to the mempool structure.
626 * A pointer to a table of void * pointers (objects).
628 * The number of objects to add in the mempool from obj_table.
631 rte_mempool_put_bulk(struct rte_mempool *mp, void * const *obj_table,
634 __mempool_check_cookies(mp, obj_table, n, 0);
635 __mempool_put_bulk(mp, obj_table, n, !(mp->flags & MEMPOOL_F_SP_PUT));
639 * Put one object in the mempool (multi-producers safe).
642 * A pointer to the mempool structure.
644 * A pointer to the object to be added.
647 rte_mempool_mp_put(struct rte_mempool *mp, void *obj)
649 rte_mempool_mp_put_bulk(mp, &obj, 1);
653 * Put one object back in the mempool (NOT multi-producers safe).
656 * A pointer to the mempool structure.
658 * A pointer to the object to be added.
661 rte_mempool_sp_put(struct rte_mempool *mp, void *obj)
663 rte_mempool_sp_put_bulk(mp, &obj, 1);
667 * Put one object back in the mempool.
669 * This function calls the multi-producer or the single-producer
670 * version depending on the default behavior that was specified at
671 * mempool creation time (see flags).
674 * A pointer to the mempool structure.
676 * A pointer to the object to be added.
679 rte_mempool_put(struct rte_mempool *mp, void *obj)
681 rte_mempool_put_bulk(mp, &obj, 1);
685 * @internal Get several objects from the mempool; used internally.
687 * A pointer to the mempool structure.
689 * A pointer to a table of void * pointers (objects).
691 * The number of objects to get, must be strictly positive.
693 * Mono-consumer (0) or multi-consumers (1).
695 * - >=0: Success; number of objects supplied.
696 * - <0: Error; code of ring dequeue function.
699 __mempool_get_bulk(struct rte_mempool *mp, void **obj_table,
700 unsigned n, int is_mc)
703 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
706 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
707 struct rte_mempool_cache *cache;
708 uint32_t cache_len, cache_len_save = 0;
710 unsigned lcore_id = rte_lcore_id();
711 uint32_t cache_size = mp->cache_size;
712 uint32_t cache_del_count;
714 cache = &mp->local_cache[lcore_id];
716 /* cache is not enabled or single consumer */
717 if (unlikely(cache_size == 0 || is_mc == 0))
720 cache_len = cache->len;
721 cache_objs = cache->objs;
723 /* cache is empty and we need many objects: dequeue from ring */
724 if (unlikely(cache_len == 0 && n >= cache_size))
727 /* cache is empty and we dequeue few objects: fill the cache first */
728 if (unlikely(cache_len == 0 && n < cache_size)) {
729 ret = rte_ring_mc_dequeue_bulk(mp->ring, cache_objs,
731 if (unlikely(ret < 0)) {
732 __MEMPOOL_STAT_ADD(mp, get_fail, n_orig);
736 cache_len = cache_size;
739 if (likely(n <= cache_len))
742 cache_del_count = cache_len;
744 cache_len_save = cache_len;
746 /* add in cache only while there is enough room */
747 while (cache_del_count > 0) {
749 *obj_table = cache_objs[cache_len];
755 cache->len = cache_len;
757 /* no more object to get, return */
758 if (likely(n == 0)) {
759 __MEMPOOL_STAT_ADD(mp, get_success, n_orig);
764 #endif /* RTE_MEMPOOL_CACHE_MAX_SIZE > 0 */
766 /* get remaining objects from ring */
768 ret = rte_ring_mc_dequeue_bulk(mp->ring, obj_table, n);
770 ret = rte_ring_sc_dequeue_bulk(mp->ring, obj_table, n);
772 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
774 * bad luck, the ring is empty but we already dequeued some
775 * entries from cache, we have to restore them
777 if (unlikely(ret < 0 && cache_len_save != 0))
778 cache->len = cache_len_save;
782 __MEMPOOL_STAT_ADD(mp, get_fail, n_orig);
784 __MEMPOOL_STAT_ADD(mp, get_success, n_orig);
790 * Get several objects from the mempool (multi-consumers safe).
792 * If cache is enabled, objects will be retrieved first from cache,
793 * subsequently from the common pool. Note that it can return -ENOENT when
794 * the local cache and common pool are empty, even if cache from other
798 * A pointer to the mempool structure.
800 * A pointer to a table of void * pointers (objects) that will be filled.
802 * The number of objects to get from mempool to obj_table.
804 * - 0: Success; objects taken.
805 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
808 rte_mempool_mc_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned n)
811 ret = __mempool_get_bulk(mp, obj_table, n, 1);
813 __mempool_check_cookies(mp, obj_table, n, 1);
818 * Get several objects from the mempool (NOT multi-consumers safe).
820 * If cache is enabled, objects will be retrieved first from cache,
821 * subsequently from the common pool. Note that it can return -ENOENT when
822 * the local cache and common pool are empty, even if cache from other
826 * A pointer to the mempool structure.
828 * A pointer to a table of void * pointers (objects) that will be filled.
830 * The number of objects to get from the mempool to obj_table.
832 * - 0: Success; objects taken.
833 * - -ENOENT: Not enough entries in the mempool; no object is
837 rte_mempool_sc_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned n)
840 ret = __mempool_get_bulk(mp, obj_table, n, 0);
842 __mempool_check_cookies(mp, obj_table, n, 1);
847 * Get several objects from the mempool.
849 * This function calls the multi-consumers or the single-consumer
850 * version, depending on the default behaviour that was specified at
851 * mempool creation time (see flags).
853 * If cache is enabled, objects will be retrieved first from cache,
854 * subsequently from the common pool. Note that it can return -ENOENT when
855 * the local cache and common pool are empty, even if cache from other
859 * A pointer to the mempool structure.
861 * A pointer to a table of void * pointers (objects) that will be filled.
863 * The number of objects to get from the mempool to obj_table.
865 * - 0: Success; objects taken
866 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
869 rte_mempool_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned n)
872 ret = __mempool_get_bulk(mp, obj_table, n,
873 !(mp->flags & MEMPOOL_F_SC_GET));
875 __mempool_check_cookies(mp, obj_table, n, 1);
880 * Get one object from the mempool (multi-consumers safe).
882 * If cache is enabled, objects will be retrieved first from cache,
883 * subsequently from the common pool. Note that it can return -ENOENT when
884 * the local cache and common pool are empty, even if cache from other
888 * A pointer to the mempool structure.
890 * A pointer to a void * pointer (object) that will be filled.
892 * - 0: Success; objects taken.
893 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
896 rte_mempool_mc_get(struct rte_mempool *mp, void **obj_p)
898 return rte_mempool_mc_get_bulk(mp, obj_p, 1);
902 * Get one object from the mempool (NOT multi-consumers safe).
904 * If cache is enabled, objects will be retrieved first from cache,
905 * subsequently from the common pool. Note that it can return -ENOENT when
906 * the local cache and common pool are empty, even if cache from other
910 * A pointer to the mempool structure.
912 * A pointer to a void * pointer (object) that will be filled.
914 * - 0: Success; objects taken.
915 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
918 rte_mempool_sc_get(struct rte_mempool *mp, void **obj_p)
920 return rte_mempool_sc_get_bulk(mp, obj_p, 1);
924 * Get one object from the mempool.
926 * This function calls the multi-consumers or the single-consumer
927 * version, depending on the default behavior that was specified at
928 * mempool creation (see flags).
930 * If cache is enabled, objects will be retrieved first from cache,
931 * subsequently from the common pool. Note that it can return -ENOENT when
932 * the local cache and common pool are empty, even if cache from other
936 * A pointer to the mempool structure.
938 * A pointer to a void * pointer (object) that will be filled.
940 * - 0: Success; objects taken.
941 * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
944 rte_mempool_get(struct rte_mempool *mp, void **obj_p)
946 return rte_mempool_get_bulk(mp, obj_p, 1);
950 * Return the number of entries in the mempool.
952 * When cache is enabled, this function has to browse the length of
953 * all lcores, so it should not be used in a data path, but only for
957 * A pointer to the mempool structure.
959 * The number of entries in the mempool.
961 unsigned rte_mempool_count(const struct rte_mempool *mp);
964 * Return the number of free entries in the mempool.
966 * When cache is enabled, this function has to browse the length of
967 * all lcores, so it should not be used in a data path, but only for
971 * A pointer to the mempool structure.
973 * The number of free entries in the mempool.
975 static inline unsigned
976 rte_mempool_free_count(const struct rte_mempool *mp)
978 return mp->size - rte_mempool_count(mp);
982 * Test if the mempool is full.
984 * When cache is enabled, this function has to browse the length of all
985 * lcores, so it should not be used in a data path, but only for debug
989 * A pointer to the mempool structure.
991 * - 1: The mempool is full.
992 * - 0: The mempool is not full.
995 rte_mempool_full(const struct rte_mempool *mp)
997 return !!(rte_mempool_count(mp) == mp->size);
1001 * Test if the mempool is empty.
1003 * When cache is enabled, this function has to browse the length of all
1004 * lcores, so it should not be used in a data path, but only for debug
1008 * A pointer to the mempool structure.
1010 * - 1: The mempool is empty.
1011 * - 0: The mempool is not empty.
1014 rte_mempool_empty(const struct rte_mempool *mp)
1016 return !!(rte_mempool_count(mp) == 0);
1020 * Return the physical address of elt, which is an element of the pool mp.
1023 * A pointer to the mempool structure.
1025 * A pointer (virtual address) to the element of the pool.
1027 * The physical address of the elt element.
1029 static inline phys_addr_t rte_mempool_virt2phy(const struct rte_mempool *mp,
1034 off = (const char *)elt - (const char *)mp;
1035 return mp->phys_addr + off;
1040 * Check the consistency of mempool objects.
1042 * Verify the coherency of fields in the mempool structure. Also check
1043 * that the cookies of mempool objects (even the ones that are not
1044 * present in pool) have a correct value. If not, a panic will occur.
1047 * A pointer to the mempool structure.
1049 void rte_mempool_audit(const struct rte_mempool *mp);
1052 * Return a pointer to the private data in an mempool structure.
1055 * A pointer to the mempool structure.
1057 * A pointer to the private data.
1059 static inline void *rte_mempool_get_priv(struct rte_mempool *mp)
1061 return (char *)mp + sizeof(struct rte_mempool);
1065 * Dump the status of all mempools on the console
1067 void rte_mempool_list_dump(void);
1070 * Search a mempool from its name
1073 * The name of the mempool.
1075 * The pointer to the mempool matching the name, or NULL if not found.NULL on error
1076 * with rte_errno set appropriately. Possible rte_errno values include:
1077 * - ENOENT - required entry not available to return.
1080 struct rte_mempool *rte_mempool_lookup(const char *name);
1086 #endif /* _RTE_MEMPOOL_H_ */