remove version in all files
[dpdk.git] / lib / librte_mempool / rte_mempool.h
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2012 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  * 
7  *   Redistribution and use in source and binary forms, with or without 
8  *   modification, are permitted provided that the following conditions 
9  *   are met:
10  * 
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 
16  *       distribution.
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.
20  * 
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.
32  * 
33  */
34
35 #ifndef _RTE_MEMPOOL_H_
36 #define _RTE_MEMPOOL_H_
37
38 /**
39  * @file
40  * RTE Mempool.
41  *
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.
47  *
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
52  * new object.
53  *
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.
61  */
62
63 #include <stdlib.h>
64 #include <stdint.h>
65 #include <errno.h>
66 #include <inttypes.h>
67 #include <sys/queue.h>
68
69 #include <rte_log.h>
70 #include <rte_debug.h>
71 #include <rte_memory.h>
72 #include <rte_lcore.h>
73 #include <rte_branch_prediction.h>
74 #include <rte_ring.h>
75
76 #ifdef __cplusplus
77 extern "C" {
78 #endif
79
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.*/
83
84 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
85 /**
86  * A structure that stores the mempool statistics (per-lcore).
87  */
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;
96 #endif
97
98 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
99 /**
100  * A structure that stores a per-core object cache.
101  */
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 */
107
108 #define RTE_MEMPOOL_NAMESIZE 32 /**< Maximum length of a memory pool. */
109
110 /**
111  * The RTE mempool structure.
112  */
113 struct rte_mempool {
114         TAILQ_ENTRY(rte_mempool) next;   /**< Next in list. */
115
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. */
123
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). */
127
128         unsigned private_data_size;      /**< Size of private data. */
129
130 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
131         /** Per-lcore local cache. */
132         struct rte_mempool_cache local_cache[RTE_MAX_LCORE];
133 #endif
134
135 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
136         /** Per-lcore statistics. */
137         struct rte_mempool_debug_stats stats[RTE_MAX_LCORE];
138 #endif
139 }  __rte_cache_aligned;
140
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".*/
145
146 /**
147  * When debug is enabled, store some statistics.
148  * @param mp
149  *   Pointer to the memory pool.
150  * @param name
151  *   Name of the statistics field to increment in the memory pool.
152  * @param n
153  *   Number to add to the object-oriented statistics.
154  */
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;         \
160         } while(0)
161 #else
162 #define __MEMPOOL_STAT_ADD(mp, name, n) do {} while(0)
163 #endif
164
165 /**
166  * Get a pointer to a mempool pointer in the object header.
167  * @param obj
168  *   Pointer to object.
169  * @return
170  *   The pointer to the mempool from which the object was allocated.
171  */
172 static inline struct rte_mempool **__mempool_from_obj(void *obj)
173 {
174         struct rte_mempool **mpp;
175         unsigned off;
176
177         off = sizeof(struct rte_mempool *);
178 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
179         off += sizeof(uint64_t);
180 #endif
181         mpp = (struct rte_mempool **)((char *)obj - off);
182         return mpp;
183 }
184
185 /**
186  * Return a pointer to the mempool owning this object.
187  *
188  * @param obj
189  *   An object that is owned by a pool. If this is not the case,
190  *   the behavior is undefined.
191  * @return
192  *   A pointer to the mempool structure.
193  */
194 static inline const struct rte_mempool *rte_mempool_from_obj(void *obj)
195 {
196         struct rte_mempool * const *mpp;
197         mpp = __mempool_from_obj(obj);
198         return *mpp;
199 }
200
201 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
202 /* get header cookie value */
203 static inline uint64_t __mempool_read_header_cookie(const void *obj)
204 {
205         return *(const uint64_t *)((const char *)obj - sizeof(uint64_t));
206 }
207
208 /* get trailer cookie value */
209 static inline uint64_t __mempool_read_trailer_cookie(void *obj)
210 {
211         struct rte_mempool **mpp = __mempool_from_obj(obj);
212         return *(uint64_t *)((char *)obj + (*mpp)->elt_size);
213 }
214
215 /* write header cookie value */
216 static inline void __mempool_write_header_cookie(void *obj, int free)
217 {
218         uint64_t *cookie_p;
219         cookie_p = (uint64_t *)((char *)obj - sizeof(uint64_t));
220         if (free == 0)
221                 *cookie_p = RTE_MEMPOOL_HEADER_COOKIE1;
222         else
223                 *cookie_p = RTE_MEMPOOL_HEADER_COOKIE2;
224
225 }
226
227 /* write trailer cookie value */
228 static inline void __mempool_write_trailer_cookie(void *obj)
229 {
230         uint64_t *cookie_p;
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;
234 }
235 #endif /* RTE_LIBRTE_MEMPOOL_DEBUG */
236
237 /**
238  * Check and update cookies or panic.
239  *
240  * @param mp
241  *   Pointer to the memory pool.
242  * @param obj_table_const
243  *   Pointer to a table of void * pointers (objects).
244  * @param n
245  *   Index of object in object table.
246  * @param free
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)
250  */
251 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
252 #ifndef __INTEL_COMPILER
253 #pragma GCC diagnostic ignored "-Wcast-qual"
254 #endif
255 static inline void __mempool_check_cookies(const struct rte_mempool *mp,
256                                            void * const *obj_table_const,
257                                            unsigned n, int free)
258 {
259         uint64_t cookie;
260         void *tmp;
261         void *obj;
262         void **obj_table;
263
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;
268
269         while (n--) {
270                 obj = obj_table[n];
271
272                 if (rte_mempool_from_obj(obj) != mp)
273                         rte_panic("MEMPOOL: object is owned by another "
274                                   "mempool\n");
275
276                 cookie = __mempool_read_header_cookie(obj);
277
278                 if (free == 0) {
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",
283                                         obj, mp, cookie);
284                                 rte_panic("MEMPOOL: bad header cookie (put)\n");
285                         }
286                         __mempool_write_header_cookie(obj, 1);
287                 }
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",
293                                         obj, mp, cookie);
294                                 rte_panic("MEMPOOL: bad header cookie (get)\n");
295                         }
296                         __mempool_write_header_cookie(obj, 0);
297                 }
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",
304                                         obj, mp, cookie);
305                                 rte_panic("MEMPOOL: bad header cookie (audit)\n");
306                         }
307                 }
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",
313                                 obj, mp, cookie);
314                         rte_panic("MEMPOOL: bad trailer cookie\n");
315                 }
316         }
317 }
318 #ifndef __INTEL_COMPILER
319 #pragma GCC diagnostic error "-Wcast-qual"
320 #endif
321 #else
322 #define __mempool_check_cookies(mp, obj_table_const, n, free) do {} while(0)
323 #endif /* RTE_LIBRTE_MEMPOOL_DEBUG */
324
325 /**
326  * An object constructor callback function for mempool.
327  *
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.
331  */
332 typedef void (rte_mempool_obj_ctor_t)(struct rte_mempool *, void *,
333                                       void *, unsigned);
334
335 /**
336  * A mempool constructor callback function.
337  *
338  * Arguments are the mempool and the opaque pointer given by the user in
339  * rte_mempool_create().
340  */
341 typedef void (rte_mempool_ctor_t)(struct rte_mempool *, void *);
342
343 /**
344  * Creates a new mempool named *name* in memory.
345  *
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.
351  *
352  * @param name
353  *   The name of the mempool.
354  * @param n
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:
357  *   n = (2^q - 1).
358  * @param elt_size
359  *   The size of each element.
360  * @param cache_size
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.
377  * @param mp_init
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
381  *   not needed.
382  * @param mp_init_arg
383  *   An opaque pointer to data that can be used in the mempool
384  *   constructor function.
385  * @param obj_init
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.
394  * @param socket_id
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.
398  * @param flags
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".
415  * @return
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
425  */
426 struct rte_mempool *
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);
432
433 /**
434  * Set the default bulk count for put/get.
435  *
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.
439  *
440  * @param mp
441  *   A pointer to the mempool structure.
442  * @param count
443  *   A new water mark value.
444  * @return
445  *   - 0: Success; default_bulk_count changed.
446  *   - -EINVAL: Invalid count value.
447  */
448 static inline int
449 rte_mempool_set_bulk_count(struct rte_mempool *mp, unsigned count)
450 {
451         if (unlikely(count == 0 || count >= mp->size))
452                 return -EINVAL;
453
454         mp->bulk_default = count;
455         return 0;
456 }
457
458 /**
459  * Get the default bulk count for put/get.
460  *
461  * @param mp
462  *   A pointer to the mempool structure.
463  * @return
464  *   The default bulk count for enqueue/dequeue.
465  */
466 static inline unsigned
467 rte_mempool_get_bulk_count(struct rte_mempool *mp)
468 {
469         return mp->bulk_default;
470 }
471
472 /**
473  * Dump the status of the mempool to the console.
474  *
475  * @param mp
476  *   A pointer to the mempool structure.
477  */
478 void rte_mempool_dump(const struct rte_mempool *mp);
479
480 /**
481  * @internal Put several objects back in the mempool; used internally.
482  * @param mp
483  *   A pointer to the mempool structure.
484  * @param obj_table
485  *   A pointer to a table of void * pointers (objects).
486  * @param n
487  *   The number of objects to store back in the mempool, must be strictly
488  *   positive.
489  * @param is_mp
490  *   Mono-producer (0) or multi-producers (1).
491  */
492 static inline void
493 __mempool_put_bulk(struct rte_mempool *mp, void * const *obj_table,
494                     unsigned n, int is_mp)
495 {
496 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
497         struct rte_mempool_cache *cache;
498         uint32_t cache_len;
499         void **cache_objs;
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 */
504
505         /* increment stat now, adding in mempool always success */
506         __MEMPOOL_STAT_ADD(mp, put, n);
507
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))
511                 goto ring_enqueue;
512
513         cache = &mp->local_cache[lcore_id];
514         cache_len = cache->len;
515         cache_objs = cache->objs;
516
517         /* cache is full and we add many objects: enqueue in ring */
518         if (unlikely(cache_len == cache_size && n >= cache_size))
519                 goto ring_enqueue;
520
521         /*
522          * cache is full and we add few objects: enqueue the content
523          * of the cache in ring
524          */
525         if (unlikely(cache_len == cache_size)) {
526 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
527                 if (rte_ring_mp_enqueue_bulk(mp->ring, cache->objs,
528                                              cache_size) < 0)
529                         rte_panic("cannot put objects in mempool\n");
530 #else
531                 rte_ring_mp_enqueue_bulk(mp->ring, cache->objs,
532                                          cache_size);
533 #endif
534                 cache_len = 0;
535         }
536
537         /* determine how many objects we can add in cache */
538         if (likely(n <= cache_size - cache_len))
539                 cache_add_count = n;
540         else
541                 cache_add_count = cache_size - cache_len;
542
543         /* add in cache while there is enough room */
544         while (cache_add_count > 0) {
545                 cache_objs[cache_len] = *obj_table;
546                 obj_table++;
547                 cache_len++;
548                 n--;
549                 cache_add_count--;
550         }
551
552         cache->len = cache_len;
553
554         /* no more object to add, return */
555         if (likely(n == 0))
556                 return;
557
558  ring_enqueue:
559 #endif /* RTE_MEMPOOL_CACHE_MAX_SIZE > 0 */
560
561         /* push remaining objects in ring */
562 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
563         if (is_mp) {
564                 if (rte_ring_mp_enqueue_bulk(mp->ring, obj_table, n) < 0)
565                         rte_panic("cannot put objects in mempool\n");
566         }
567         else {
568                 if (rte_ring_sp_enqueue_bulk(mp->ring, obj_table, n) < 0)
569                         rte_panic("cannot put objects in mempool\n");
570         }
571 #else
572         if (is_mp)
573                 rte_ring_mp_enqueue_bulk(mp->ring, obj_table, n);
574         else
575                 rte_ring_sp_enqueue_bulk(mp->ring, obj_table, n);
576 #endif
577 }
578
579
580 /**
581  * Put several objects back in the mempool (multi-producers safe).
582  *
583  * @param mp
584  *   A pointer to the mempool structure.
585  * @param obj_table
586  *   A pointer to a table of void * pointers (objects).
587  * @param n
588  *   The number of objects to add in the mempool from the obj_table.
589  */
590 static inline void
591 rte_mempool_mp_put_bulk(struct rte_mempool *mp, void * const *obj_table,
592                         unsigned n)
593 {
594         __mempool_check_cookies(mp, obj_table, n, 0);
595         __mempool_put_bulk(mp, obj_table, n, 1);
596 }
597
598 /**
599  * Put several objects back in the mempool (NOT multi-producers safe).
600  *
601  * @param mp
602  *   A pointer to the mempool structure.
603  * @param obj_table
604  *   A pointer to a table of void * pointers (objects).
605  * @param n
606  *   The number of objects to add in the mempool from obj_table.
607  */
608 static inline void
609 rte_mempool_sp_put_bulk(struct rte_mempool *mp, void * const *obj_table,
610                         unsigned n)
611 {
612         __mempool_check_cookies(mp, obj_table, n, 0);
613         __mempool_put_bulk(mp, obj_table, n, 0);
614 }
615
616 /**
617  * Put several objects back in the mempool.
618  *
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).
622  *
623  * @param mp
624  *   A pointer to the mempool structure.
625  * @param obj_table
626  *   A pointer to a table of void * pointers (objects).
627  * @param n
628  *   The number of objects to add in the mempool from obj_table.
629  */
630 static inline void
631 rte_mempool_put_bulk(struct rte_mempool *mp, void * const *obj_table,
632                      unsigned n)
633 {
634         __mempool_check_cookies(mp, obj_table, n, 0);
635         __mempool_put_bulk(mp, obj_table, n, !(mp->flags & MEMPOOL_F_SP_PUT));
636 }
637
638 /**
639  * Put one object in the mempool (multi-producers safe).
640  *
641  * @param mp
642  *   A pointer to the mempool structure.
643  * @param obj
644  *   A pointer to the object to be added.
645  */
646 static inline void
647 rte_mempool_mp_put(struct rte_mempool *mp, void *obj)
648 {
649         rte_mempool_mp_put_bulk(mp, &obj, 1);
650 }
651
652 /**
653  * Put one object back in the mempool (NOT multi-producers safe).
654  *
655  * @param mp
656  *   A pointer to the mempool structure.
657  * @param obj
658  *   A pointer to the object to be added.
659  */
660 static inline void
661 rte_mempool_sp_put(struct rte_mempool *mp, void *obj)
662 {
663         rte_mempool_sp_put_bulk(mp, &obj, 1);
664 }
665
666 /**
667  * Put one object back in the mempool.
668  *
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).
672  *
673  * @param mp
674  *   A pointer to the mempool structure.
675  * @param obj
676  *   A pointer to the object to be added.
677  */
678 static inline void
679 rte_mempool_put(struct rte_mempool *mp, void *obj)
680 {
681         rte_mempool_put_bulk(mp, &obj, 1);
682 }
683
684 /**
685  * @internal Get several objects from the mempool; used internally.
686  * @param mp
687  *   A pointer to the mempool structure.
688  * @param obj_table
689  *   A pointer to a table of void * pointers (objects).
690  * @param n
691  *   The number of objects to get, must be strictly positive.
692  * @param is_mc
693  *   Mono-consumer (0) or multi-consumers (1).
694  * @return
695  *   - >=0: Success; number of objects supplied.
696  *   - <0: Error; code of ring dequeue function.
697  */
698 static inline int
699 __mempool_get_bulk(struct rte_mempool *mp, void **obj_table,
700                    unsigned n, int is_mc)
701 {
702         int ret;
703 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
704         unsigned n_orig = n;
705 #endif
706 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
707         struct rte_mempool_cache *cache;
708         uint32_t cache_len, cache_len_save = 0;
709         void **cache_objs;
710         unsigned lcore_id = rte_lcore_id();
711         uint32_t cache_size = mp->cache_size;
712         uint32_t cache_del_count;
713
714         cache = &mp->local_cache[lcore_id];
715
716         /* cache is not enabled or single consumer */
717         if (unlikely(cache_size == 0 || is_mc == 0))
718                 goto ring_dequeue;
719
720         cache_len = cache->len;
721         cache_objs = cache->objs;
722
723         /* cache is empty and we need many objects: dequeue from ring */
724         if (unlikely(cache_len == 0 && n >= cache_size))
725                 goto ring_dequeue;
726
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,
730                                                cache_size);
731                 if (unlikely(ret < 0)) {
732                         __MEMPOOL_STAT_ADD(mp, get_fail, n_orig);
733                         return ret;
734                 }
735
736                 cache_len = cache_size;
737         }
738
739         if (likely(n <= cache_len))
740                 cache_del_count = n;
741         else
742                 cache_del_count = cache_len;
743
744         cache_len_save = cache_len;
745
746         /* add in cache only while there is enough room */
747         while (cache_del_count > 0) {
748                 cache_len--;
749                 *obj_table = cache_objs[cache_len];
750                 obj_table++;
751                 n--;
752                 cache_del_count--;
753         }
754
755         cache->len = cache_len;
756
757         /* no more object to get, return */
758         if (likely(n == 0)) {
759                 __MEMPOOL_STAT_ADD(mp, get_success, n_orig);
760                 return 0;
761         }
762
763  ring_dequeue:
764 #endif /* RTE_MEMPOOL_CACHE_MAX_SIZE > 0 */
765
766         /* get remaining objects from ring */
767         if (is_mc)
768                 ret = rte_ring_mc_dequeue_bulk(mp->ring, obj_table, n);
769         else
770                 ret = rte_ring_sc_dequeue_bulk(mp->ring, obj_table, n);
771
772 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
773         /*
774          * bad luck, the ring is empty but we already dequeued some
775          * entries from cache, we have to restore them
776          */
777         if (unlikely(ret < 0 && cache_len_save != 0))
778                 cache->len = cache_len_save;
779 #endif
780
781         if (ret < 0)
782                 __MEMPOOL_STAT_ADD(mp, get_fail, n_orig);
783         else
784                 __MEMPOOL_STAT_ADD(mp, get_success, n_orig);
785
786         return ret;
787 }
788
789 /**
790  * Get several objects from the mempool (multi-consumers safe).
791  *
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
795  * lcores are full.
796  *
797  * @param mp
798  *   A pointer to the mempool structure.
799  * @param obj_table
800  *   A pointer to a table of void * pointers (objects) that will be filled.
801  * @param n
802  *   The number of objects to get from mempool to obj_table.
803  * @return
804  *   - 0: Success; objects taken.
805  *   - -ENOENT: Not enough entries in the mempool; no object is retrieved.
806  */
807 static inline int
808 rte_mempool_mc_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned n)
809 {
810         int ret;
811         ret = __mempool_get_bulk(mp, obj_table, n, 1);
812         if (ret == 0)
813                 __mempool_check_cookies(mp, obj_table, n, 1);
814         return ret;
815 }
816
817 /**
818  * Get several objects from the mempool (NOT multi-consumers safe).
819  *
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
823  * lcores are full.
824  *
825  * @param mp
826  *   A pointer to the mempool structure.
827  * @param obj_table
828  *   A pointer to a table of void * pointers (objects) that will be filled.
829  * @param n
830  *   The number of objects to get from the mempool to obj_table.
831  * @return
832  *   - 0: Success; objects taken.
833  *   - -ENOENT: Not enough entries in the mempool; no object is
834  *     retrieved.
835  */
836 static inline int
837 rte_mempool_sc_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned n)
838 {
839         int ret;
840         ret = __mempool_get_bulk(mp, obj_table, n, 0);
841         if (ret == 0)
842                 __mempool_check_cookies(mp, obj_table, n, 1);
843         return ret;
844 }
845
846 /**
847  * Get several objects from the mempool.
848  *
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).
852  *
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
856  * lcores are full.
857  *
858  * @param mp
859  *   A pointer to the mempool structure.
860  * @param obj_table
861  *   A pointer to a table of void * pointers (objects) that will be filled.
862  * @param n
863  *   The number of objects to get from the mempool to obj_table.
864  * @return
865  *   - 0: Success; objects taken
866  *   - -ENOENT: Not enough entries in the mempool; no object is retrieved.
867  */
868 static inline int
869 rte_mempool_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned n)
870 {
871         int ret;
872         ret = __mempool_get_bulk(mp, obj_table, n,
873                                  !(mp->flags & MEMPOOL_F_SC_GET));
874         if (ret == 0)
875                 __mempool_check_cookies(mp, obj_table, n, 1);
876         return ret;
877 }
878
879 /**
880  * Get one object from the mempool (multi-consumers safe).
881  *
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
885  * lcores are full.
886  *
887  * @param mp
888  *   A pointer to the mempool structure.
889  * @param obj_p
890  *   A pointer to a void * pointer (object) that will be filled.
891  * @return
892  *   - 0: Success; objects taken.
893  *   - -ENOENT: Not enough entries in the mempool; no object is retrieved.
894  */
895 static inline int
896 rte_mempool_mc_get(struct rte_mempool *mp, void **obj_p)
897 {
898         return rte_mempool_mc_get_bulk(mp, obj_p, 1);
899 }
900
901 /**
902  * Get one object from the mempool (NOT multi-consumers safe).
903  *
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
907  * lcores are full.
908  *
909  * @param mp
910  *   A pointer to the mempool structure.
911  * @param obj_p
912  *   A pointer to a void * pointer (object) that will be filled.
913  * @return
914  *   - 0: Success; objects taken.
915  *   - -ENOENT: Not enough entries in the mempool; no object is retrieved.
916  */
917 static inline int
918 rte_mempool_sc_get(struct rte_mempool *mp, void **obj_p)
919 {
920         return rte_mempool_sc_get_bulk(mp, obj_p, 1);
921 }
922
923 /**
924  * Get one object from the mempool.
925  *
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).
929  *
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
933  * lcores are full.
934  *
935  * @param mp
936  *   A pointer to the mempool structure.
937  * @param obj_p
938  *   A pointer to a void * pointer (object) that will be filled.
939  * @return
940  *   - 0: Success; objects taken.
941  *   - -ENOENT: Not enough entries in the mempool; no object is retrieved.
942  */
943 static inline int
944 rte_mempool_get(struct rte_mempool *mp, void **obj_p)
945 {
946         return rte_mempool_get_bulk(mp, obj_p, 1);
947 }
948
949 /**
950  * Return the number of entries in the mempool.
951  *
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
954  * debug purposes.
955  *
956  * @param mp
957  *   A pointer to the mempool structure.
958  * @return
959  *   The number of entries in the mempool.
960  */
961 unsigned rte_mempool_count(const struct rte_mempool *mp);
962
963 /**
964  * Return the number of free entries in the mempool.
965  *
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
968  * debug purposes.
969  *
970  * @param mp
971  *   A pointer to the mempool structure.
972  * @return
973  *   The number of free entries in the mempool.
974  */
975 static inline unsigned
976 rte_mempool_free_count(const struct rte_mempool *mp)
977 {
978         return mp->size - rte_mempool_count(mp);
979 }
980
981 /**
982  * Test if the mempool is full.
983  *
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
986  * purposes.
987  *
988  * @param mp
989  *   A pointer to the mempool structure.
990  * @return
991  *   - 1: The mempool is full.
992  *   - 0: The mempool is not full.
993  */
994 static inline int
995 rte_mempool_full(const struct rte_mempool *mp)
996 {
997         return !!(rte_mempool_count(mp) == mp->size);
998 }
999
1000 /**
1001  * Test if the mempool is empty.
1002  *
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
1005  * purposes.
1006  *
1007  * @param mp
1008  *   A pointer to the mempool structure.
1009  * @return
1010  *   - 1: The mempool is empty.
1011  *   - 0: The mempool is not empty.
1012  */
1013 static inline int
1014 rte_mempool_empty(const struct rte_mempool *mp)
1015 {
1016         return !!(rte_mempool_count(mp) == 0);
1017 }
1018
1019 /**
1020  * Return the physical address of elt, which is an element of the pool mp.
1021  *
1022  * @param mp
1023  *   A pointer to the mempool structure.
1024  * @param elt
1025  *   A pointer (virtual address) to the element of the pool.
1026  * @return
1027  *   The physical address of the elt element.
1028  */
1029 static inline phys_addr_t rte_mempool_virt2phy(const struct rte_mempool *mp,
1030         const void *elt)
1031 {
1032         uintptr_t off;
1033
1034         off = (const char *)elt - (const char *)mp;
1035         return mp->phys_addr + off;
1036 }
1037
1038
1039 /**
1040  * Check the consistency of mempool objects.
1041  *
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.
1045  *
1046  * @param mp
1047  *   A pointer to the mempool structure.
1048  */
1049 void rte_mempool_audit(const struct rte_mempool *mp);
1050
1051 /**
1052  * Return a pointer to the private data in an mempool structure.
1053  *
1054  * @param mp
1055  *   A pointer to the mempool structure.
1056  * @return
1057  *   A pointer to the private data.
1058  */
1059 static inline void *rte_mempool_get_priv(struct rte_mempool *mp)
1060 {
1061         return (char *)mp + sizeof(struct rte_mempool);
1062 }
1063
1064 /**
1065  * Dump the status of all mempools on the console
1066  */
1067 void rte_mempool_list_dump(void);
1068
1069 /**
1070  * Search a mempool from its name
1071  *
1072  * @param name
1073  *   The name of the mempool.
1074  * @return
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.
1078  *
1079  */
1080 struct rte_mempool *rte_mempool_lookup(const char *name);
1081
1082 #ifdef __cplusplus
1083 }
1084 #endif
1085
1086 #endif /* _RTE_MEMPOOL_H_ */