update copyright date to 2013
[dpdk.git] / lib / librte_mempool / rte_mempool.h
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2013 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_lcore.h>
72 #include <rte_memory.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         /*
105          * Cache is allocated to this size to allow it to overflow in certain
106          * cases to avoid needless emptying of cache.
107          */
108         void *objs[RTE_MEMPOOL_CACHE_MAX_SIZE * 3]; /**< Cache objects */
109 } __rte_cache_aligned;
110 #endif /* RTE_MEMPOOL_CACHE_MAX_SIZE > 0 */
111
112 #define RTE_MEMPOOL_NAMESIZE 32 /**< Maximum length of a memory pool. */
113
114 /**
115  * The RTE mempool structure.
116  */
117 struct rte_mempool {
118         TAILQ_ENTRY(rte_mempool) next;   /**< Next in list. */
119
120         char name[RTE_MEMPOOL_NAMESIZE]; /**< Name of mempool. */
121         struct rte_ring *ring;           /**< Ring to store objects. */
122         phys_addr_t phys_addr;           /**< Phys. addr. of mempool struct. */
123         int flags;                       /**< Flags of the mempool. */
124         uint32_t size;                   /**< Size of the mempool. */
125         uint32_t cache_size;             /**< Size of per-lcore local cache. */
126         uint32_t cache_flushthresh;      /**< Threshold before we flush excess elements. */
127
128         uint32_t elt_size;               /**< Size of an element. */
129         uint32_t header_size;            /**< Size of header (before elt). */
130         uint32_t trailer_size;           /**< Size of trailer (after elt). */
131
132         unsigned private_data_size;      /**< Size of private data. */
133
134 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
135         /** Per-lcore local cache. */
136         struct rte_mempool_cache local_cache[RTE_MAX_LCORE];
137 #endif
138
139 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
140         /** Per-lcore statistics. */
141         struct rte_mempool_debug_stats stats[RTE_MAX_LCORE];
142 #endif
143 }  __rte_cache_aligned;
144
145 #define MEMPOOL_F_NO_SPREAD      0x0001 /**< Do not spread in memory. */
146 #define MEMPOOL_F_NO_CACHE_ALIGN 0x0002 /**< Do not align objs on cache lines.*/
147 #define MEMPOOL_F_SP_PUT         0x0004 /**< Default put is "single-producer".*/
148 #define MEMPOOL_F_SC_GET         0x0008 /**< Default get is "single-consumer".*/
149
150 /**
151  * @internal When debug is enabled, store some statistics.
152  * @param mp
153  *   Pointer to the memory pool.
154  * @param name
155  *   Name of the statistics field to increment in the memory pool.
156  * @param n
157  *   Number to add to the object-oriented statistics.
158  */
159 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
160 #define __MEMPOOL_STAT_ADD(mp, name, n) do {                    \
161                 unsigned __lcore_id = rte_lcore_id();           \
162                 mp->stats[__lcore_id].name##_objs += n;         \
163                 mp->stats[__lcore_id].name##_bulk += 1;         \
164         } while(0)
165 #else
166 #define __MEMPOOL_STAT_ADD(mp, name, n) do {} while(0)
167 #endif
168
169 /**
170  * @internal Get a pointer to a mempool pointer in the object header.
171  * @param obj
172  *   Pointer to object.
173  * @return
174  *   The pointer to the mempool from which the object was allocated.
175  */
176 static inline struct rte_mempool **__mempool_from_obj(void *obj)
177 {
178         struct rte_mempool **mpp;
179         unsigned off;
180
181         off = sizeof(struct rte_mempool *);
182 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
183         off += sizeof(uint64_t);
184 #endif
185         mpp = (struct rte_mempool **)((char *)obj - off);
186         return mpp;
187 }
188
189 /**
190  * Return a pointer to the mempool owning this object.
191  *
192  * @param obj
193  *   An object that is owned by a pool. If this is not the case,
194  *   the behavior is undefined.
195  * @return
196  *   A pointer to the mempool structure.
197  */
198 static inline const struct rte_mempool *rte_mempool_from_obj(void *obj)
199 {
200         struct rte_mempool * const *mpp;
201         mpp = __mempool_from_obj(obj);
202         return *mpp;
203 }
204
205 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
206 /* get header cookie value */
207 static inline uint64_t __mempool_read_header_cookie(const void *obj)
208 {
209         return *(const uint64_t *)((const char *)obj - sizeof(uint64_t));
210 }
211
212 /* get trailer cookie value */
213 static inline uint64_t __mempool_read_trailer_cookie(void *obj)
214 {
215         struct rte_mempool **mpp = __mempool_from_obj(obj);
216         return *(uint64_t *)((char *)obj + (*mpp)->elt_size);
217 }
218
219 /* write header cookie value */
220 static inline void __mempool_write_header_cookie(void *obj, int free)
221 {
222         uint64_t *cookie_p;
223         cookie_p = (uint64_t *)((char *)obj - sizeof(uint64_t));
224         if (free == 0)
225                 *cookie_p = RTE_MEMPOOL_HEADER_COOKIE1;
226         else
227                 *cookie_p = RTE_MEMPOOL_HEADER_COOKIE2;
228
229 }
230
231 /* write trailer cookie value */
232 static inline void __mempool_write_trailer_cookie(void *obj)
233 {
234         uint64_t *cookie_p;
235         struct rte_mempool **mpp = __mempool_from_obj(obj);
236         cookie_p = (uint64_t *)((char *)obj + (*mpp)->elt_size);
237         *cookie_p = RTE_MEMPOOL_TRAILER_COOKIE;
238 }
239 #endif /* RTE_LIBRTE_MEMPOOL_DEBUG */
240
241 /**
242  * @internal Check and update cookies or panic.
243  *
244  * @param mp
245  *   Pointer to the memory pool.
246  * @param obj_table_const
247  *   Pointer to a table of void * pointers (objects).
248  * @param n
249  *   Index of object in object table.
250  * @param free
251  *   - 0: object is supposed to be allocated, mark it as free
252  *   - 1: object is supposed to be free, mark it as allocated
253  *   - 2: just check that cookie is valid (free or allocated)
254  */
255 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
256 #ifndef __INTEL_COMPILER
257 #pragma GCC diagnostic ignored "-Wcast-qual"
258 #endif
259 static inline void __mempool_check_cookies(const struct rte_mempool *mp,
260                                            void * const *obj_table_const,
261                                            unsigned n, int free)
262 {
263         uint64_t cookie;
264         void *tmp;
265         void *obj;
266         void **obj_table;
267
268         /* Force to drop the "const" attribute. This is done only when
269          * DEBUG is enabled */
270         tmp = (void *) obj_table_const;
271         obj_table = (void **) tmp;
272
273         while (n--) {
274                 obj = obj_table[n];
275
276                 if (rte_mempool_from_obj(obj) != mp)
277                         rte_panic("MEMPOOL: object is owned by another "
278                                   "mempool\n");
279
280                 cookie = __mempool_read_header_cookie(obj);
281
282                 if (free == 0) {
283                         if (cookie != RTE_MEMPOOL_HEADER_COOKIE1) {
284                                 rte_log_set_history(0);
285                                 RTE_LOG(CRIT, MEMPOOL,
286                                         "obj=%p, mempool=%p, cookie=%"PRIx64"\n",
287                                         obj, mp, cookie);
288                                 rte_panic("MEMPOOL: bad header cookie (put)\n");
289                         }
290                         __mempool_write_header_cookie(obj, 1);
291                 }
292                 else if (free == 1) {
293                         if (cookie != RTE_MEMPOOL_HEADER_COOKIE2) {
294                                 rte_log_set_history(0);
295                                 RTE_LOG(CRIT, MEMPOOL,
296                                         "obj=%p, mempool=%p, cookie=%"PRIx64"\n",
297                                         obj, mp, cookie);
298                                 rte_panic("MEMPOOL: bad header cookie (get)\n");
299                         }
300                         __mempool_write_header_cookie(obj, 0);
301                 }
302                 else if (free == 2) {
303                         if (cookie != RTE_MEMPOOL_HEADER_COOKIE1 &&
304                             cookie != RTE_MEMPOOL_HEADER_COOKIE2) {
305                                 rte_log_set_history(0);
306                                 RTE_LOG(CRIT, MEMPOOL,
307                                         "obj=%p, mempool=%p, cookie=%"PRIx64"\n",
308                                         obj, mp, cookie);
309                                 rte_panic("MEMPOOL: bad header cookie (audit)\n");
310                         }
311                 }
312                 cookie = __mempool_read_trailer_cookie(obj);
313                 if (cookie != RTE_MEMPOOL_TRAILER_COOKIE) {
314                         rte_log_set_history(0);
315                         RTE_LOG(CRIT, MEMPOOL,
316                                 "obj=%p, mempool=%p, cookie=%"PRIx64"\n",
317                                 obj, mp, cookie);
318                         rte_panic("MEMPOOL: bad trailer cookie\n");
319                 }
320         }
321 }
322 #ifndef __INTEL_COMPILER
323 #pragma GCC diagnostic error "-Wcast-qual"
324 #endif
325 #else
326 #define __mempool_check_cookies(mp, obj_table_const, n, free) do {} while(0)
327 #endif /* RTE_LIBRTE_MEMPOOL_DEBUG */
328
329 /**
330  * An object constructor callback function for mempool.
331  *
332  * Arguments are the mempool, the opaque pointer given by the user in
333  * rte_mempool_create(), the pointer to the element and the index of
334  * the element in the pool.
335  */
336 typedef void (rte_mempool_obj_ctor_t)(struct rte_mempool *, void *,
337                                       void *, unsigned);
338
339 /**
340  * A mempool constructor callback function.
341  *
342  * Arguments are the mempool and the opaque pointer given by the user in
343  * rte_mempool_create().
344  */
345 typedef void (rte_mempool_ctor_t)(struct rte_mempool *, void *);
346
347 /**
348  * Creates a new mempool named *name* in memory.
349  *
350  * This function uses ``memzone_reserve()`` to allocate memory. The
351  * pool contains n elements of elt_size. Its size is set to n.
352  *
353  * @param name
354  *   The name of the mempool.
355  * @param n
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:
358  *   n = (2^q - 1).
359  * @param elt_size
360  *   The size of each element.
361  * @param cache_size
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.
378  * @param mp_init
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
382  *   not needed.
383  * @param mp_init_arg
384  *   An opaque pointer to data that can be used in the mempool
385  *   constructor function.
386  * @param obj_init
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.
395  * @param socket_id
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.
399  * @param flags
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".
416  * @return
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
426  */
427 struct rte_mempool *
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);
433
434 /**
435  * Dump the status of the mempool to the console.
436  *
437  * @param mp
438  *   A pointer to the mempool structure.
439  */
440 void rte_mempool_dump(const struct rte_mempool *mp);
441
442 /**
443  * @internal Put several objects back in the mempool; used internally.
444  * @param mp
445  *   A pointer to the mempool structure.
446  * @param obj_table
447  *   A pointer to a table of void * pointers (objects).
448  * @param n
449  *   The number of objects to store back in the mempool, must be strictly
450  *   positive.
451  * @param is_mp
452  *   Mono-producer (0) or multi-producers (1).
453  */
454 static inline void
455 __mempool_put_bulk(struct rte_mempool *mp, void * const *obj_table,
456                     unsigned n, int is_mp)
457 {
458 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
459         struct rte_mempool_cache *cache;
460         uint32_t index;
461         void **cache_objs;
462         unsigned lcore_id = rte_lcore_id();
463         uint32_t cache_size = mp->cache_size;
464         uint32_t flushthresh = mp->cache_flushthresh;
465 #endif /* RTE_MEMPOOL_CACHE_MAX_SIZE > 0 */
466
467         /* increment stat now, adding in mempool always success */
468         __MEMPOOL_STAT_ADD(mp, put, n);
469
470 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
471         /* cache is not enabled or single producer */
472         if (unlikely(cache_size == 0 || is_mp == 0))
473                 goto ring_enqueue;
474
475         /* Go straight to ring if put would overflow mem allocated for cache */
476         if (unlikely(n > RTE_MEMPOOL_CACHE_MAX_SIZE))
477                 goto ring_enqueue;
478
479         cache = &mp->local_cache[lcore_id];
480         cache_objs = &cache->objs[cache->len];
481
482         /*
483          * The cache follows the following algorithm
484          *   1. Add the objects to the cache
485          *   2. Anything greater than the cache min value (if it crosses the
486          *   cache flush threshold) is flushed to the ring.
487          */
488
489         /* Add elements back into the cache */
490         for (index = 0; index < n; ++index, obj_table++)
491                 cache_objs[index] = *obj_table;
492
493         cache->len += n;
494
495         if (cache->len >= flushthresh) {
496                 rte_ring_mp_enqueue_bulk(mp->ring, &cache->objs[cache_size],
497                                 cache->len - cache_size);
498                 cache->len = cache_size;
499         }
500
501         return;
502
503 ring_enqueue:
504 #endif /* RTE_MEMPOOL_CACHE_MAX_SIZE > 0 */
505
506         /* push remaining objects in ring */
507 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
508         if (is_mp) {
509                 if (rte_ring_mp_enqueue_bulk(mp->ring, obj_table, n) < 0)
510                         rte_panic("cannot put objects in mempool\n");
511         }
512         else {
513                 if (rte_ring_sp_enqueue_bulk(mp->ring, obj_table, n) < 0)
514                         rte_panic("cannot put objects in mempool\n");
515         }
516 #else
517         if (is_mp)
518                 rte_ring_mp_enqueue_bulk(mp->ring, obj_table, n);
519         else
520                 rte_ring_sp_enqueue_bulk(mp->ring, obj_table, n);
521 #endif
522 }
523
524
525 /**
526  * Put several objects back in the mempool (multi-producers safe).
527  *
528  * @param mp
529  *   A pointer to the mempool structure.
530  * @param obj_table
531  *   A pointer to a table of void * pointers (objects).
532  * @param n
533  *   The number of objects to add in the mempool from the obj_table.
534  */
535 static inline void
536 rte_mempool_mp_put_bulk(struct rte_mempool *mp, void * const *obj_table,
537                         unsigned n)
538 {
539         __mempool_check_cookies(mp, obj_table, n, 0);
540         __mempool_put_bulk(mp, obj_table, n, 1);
541 }
542
543 /**
544  * Put several objects back in the mempool (NOT multi-producers safe).
545  *
546  * @param mp
547  *   A pointer to the mempool structure.
548  * @param obj_table
549  *   A pointer to a table of void * pointers (objects).
550  * @param n
551  *   The number of objects to add in the mempool from obj_table.
552  */
553 static inline void
554 rte_mempool_sp_put_bulk(struct rte_mempool *mp, void * const *obj_table,
555                         unsigned n)
556 {
557         __mempool_check_cookies(mp, obj_table, n, 0);
558         __mempool_put_bulk(mp, obj_table, n, 0);
559 }
560
561 /**
562  * Put several objects back in the mempool.
563  *
564  * This function calls the multi-producer or the single-producer
565  * version depending on the default behavior that was specified at
566  * mempool creation time (see flags).
567  *
568  * @param mp
569  *   A pointer to the mempool structure.
570  * @param obj_table
571  *   A pointer to a table of void * pointers (objects).
572  * @param n
573  *   The number of objects to add in the mempool from obj_table.
574  */
575 static inline void
576 rte_mempool_put_bulk(struct rte_mempool *mp, void * const *obj_table,
577                      unsigned n)
578 {
579         __mempool_check_cookies(mp, obj_table, n, 0);
580         __mempool_put_bulk(mp, obj_table, n, !(mp->flags & MEMPOOL_F_SP_PUT));
581 }
582
583 /**
584  * Put one object in the mempool (multi-producers safe).
585  *
586  * @param mp
587  *   A pointer to the mempool structure.
588  * @param obj
589  *   A pointer to the object to be added.
590  */
591 static inline void
592 rte_mempool_mp_put(struct rte_mempool *mp, void *obj)
593 {
594         rte_mempool_mp_put_bulk(mp, &obj, 1);
595 }
596
597 /**
598  * Put one object back in the mempool (NOT multi-producers safe).
599  *
600  * @param mp
601  *   A pointer to the mempool structure.
602  * @param obj
603  *   A pointer to the object to be added.
604  */
605 static inline void
606 rte_mempool_sp_put(struct rte_mempool *mp, void *obj)
607 {
608         rte_mempool_sp_put_bulk(mp, &obj, 1);
609 }
610
611 /**
612  * Put one object back in the mempool.
613  *
614  * This function calls the multi-producer or the single-producer
615  * version depending on the default behavior that was specified at
616  * mempool creation time (see flags).
617  *
618  * @param mp
619  *   A pointer to the mempool structure.
620  * @param obj
621  *   A pointer to the object to be added.
622  */
623 static inline void
624 rte_mempool_put(struct rte_mempool *mp, void *obj)
625 {
626         rte_mempool_put_bulk(mp, &obj, 1);
627 }
628
629 /**
630  * @internal Get several objects from the mempool; used internally.
631  * @param mp
632  *   A pointer to the mempool structure.
633  * @param obj_table
634  *   A pointer to a table of void * pointers (objects).
635  * @param n
636  *   The number of objects to get, must be strictly positive.
637  * @param is_mc
638  *   Mono-consumer (0) or multi-consumers (1).
639  * @return
640  *   - >=0: Success; number of objects supplied.
641  *   - <0: Error; code of ring dequeue function.
642  */
643 static inline int
644 __mempool_get_bulk(struct rte_mempool *mp, void **obj_table,
645                    unsigned n, int is_mc)
646 {
647         int ret;
648 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
649         unsigned n_orig = n;
650 #endif
651 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
652         struct rte_mempool_cache *cache;
653         uint32_t index, len;
654         void **cache_objs;
655         unsigned lcore_id = rte_lcore_id();
656         uint32_t cache_size = mp->cache_size;
657
658         cache = &mp->local_cache[lcore_id];
659
660         /* cache is not enabled or single consumer */
661         if (unlikely(cache_size == 0 || is_mc == 0 || n >= cache_size))
662                 goto ring_dequeue;
663
664         cache_objs = cache->objs;
665
666         /* Can this be satisfied from the cache? */
667         if (cache->len < n) {
668                 /* No. Backfill the cache first, and then fill from it */
669                 uint32_t req = n + (cache_size - cache->len);
670
671                 /* How many do we require i.e. number to fill the cache + the request */
672                 ret = rte_ring_mc_dequeue_bulk(mp->ring, &cache->objs[cache->len], req);
673                 if (unlikely(ret < 0)) {
674                         /*
675                          * In the offchance that we are buffer constrained,
676                          * where we are not able to allocate cache + n, go to
677                          * the ring directly. If that fails, we are truly out of
678                          * buffers.
679                          */
680                         goto ring_dequeue;
681                 }
682
683                 cache->len += req;
684         }
685
686         /* Now fill in the response ... */
687         for (index = 0, len = cache->len - 1; index < n; ++index, len--, obj_table++)
688                 *obj_table = cache_objs[len];
689
690         cache->len -= n;
691
692         __MEMPOOL_STAT_ADD(mp, get_success, n_orig);
693
694         return 0;
695
696 ring_dequeue:
697 #endif /* RTE_MEMPOOL_CACHE_MAX_SIZE > 0 */
698
699         /* get remaining objects from ring */
700         if (is_mc)
701                 ret = rte_ring_mc_dequeue_bulk(mp->ring, obj_table, n);
702         else
703                 ret = rte_ring_sc_dequeue_bulk(mp->ring, obj_table, n);
704
705         if (ret < 0)
706                 __MEMPOOL_STAT_ADD(mp, get_fail, n_orig);
707         else
708                 __MEMPOOL_STAT_ADD(mp, get_success, n_orig);
709
710         return ret;
711 }
712
713 /**
714  * Get several objects from the mempool (multi-consumers safe).
715  *
716  * If cache is enabled, objects will be retrieved first from cache,
717  * subsequently from the common pool. Note that it can return -ENOENT when
718  * the local cache and common pool are empty, even if cache from other
719  * lcores are full.
720  *
721  * @param mp
722  *   A pointer to the mempool structure.
723  * @param obj_table
724  *   A pointer to a table of void * pointers (objects) that will be filled.
725  * @param n
726  *   The number of objects to get from mempool to obj_table.
727  * @return
728  *   - 0: Success; objects taken.
729  *   - -ENOENT: Not enough entries in the mempool; no object is retrieved.
730  */
731 static inline int
732 rte_mempool_mc_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned n)
733 {
734         int ret;
735         ret = __mempool_get_bulk(mp, obj_table, n, 1);
736         if (ret == 0)
737                 __mempool_check_cookies(mp, obj_table, n, 1);
738         return ret;
739 }
740
741 /**
742  * Get several objects from the mempool (NOT multi-consumers safe).
743  *
744  * If cache is enabled, objects will be retrieved first from cache,
745  * subsequently from the common pool. Note that it can return -ENOENT when
746  * the local cache and common pool are empty, even if cache from other
747  * lcores are full.
748  *
749  * @param mp
750  *   A pointer to the mempool structure.
751  * @param obj_table
752  *   A pointer to a table of void * pointers (objects) that will be filled.
753  * @param n
754  *   The number of objects to get from the mempool to obj_table.
755  * @return
756  *   - 0: Success; objects taken.
757  *   - -ENOENT: Not enough entries in the mempool; no object is
758  *     retrieved.
759  */
760 static inline int
761 rte_mempool_sc_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned n)
762 {
763         int ret;
764         ret = __mempool_get_bulk(mp, obj_table, n, 0);
765         if (ret == 0)
766                 __mempool_check_cookies(mp, obj_table, n, 1);
767         return ret;
768 }
769
770 /**
771  * Get several objects from the mempool.
772  *
773  * This function calls the multi-consumers or the single-consumer
774  * version, depending on the default behaviour that was specified at
775  * mempool creation time (see flags).
776  *
777  * If cache is enabled, objects will be retrieved first from cache,
778  * subsequently from the common pool. Note that it can return -ENOENT when
779  * the local cache and common pool are empty, even if cache from other
780  * lcores are full.
781  *
782  * @param mp
783  *   A pointer to the mempool structure.
784  * @param obj_table
785  *   A pointer to a table of void * pointers (objects) that will be filled.
786  * @param n
787  *   The number of objects to get from the mempool to obj_table.
788  * @return
789  *   - 0: Success; objects taken
790  *   - -ENOENT: Not enough entries in the mempool; no object is retrieved.
791  */
792 static inline int
793 rte_mempool_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned n)
794 {
795         int ret;
796         ret = __mempool_get_bulk(mp, obj_table, n,
797                                  !(mp->flags & MEMPOOL_F_SC_GET));
798         if (ret == 0)
799                 __mempool_check_cookies(mp, obj_table, n, 1);
800         return ret;
801 }
802
803 /**
804  * Get one object from the mempool (multi-consumers safe).
805  *
806  * If cache is enabled, objects will be retrieved first from cache,
807  * subsequently from the common pool. Note that it can return -ENOENT when
808  * the local cache and common pool are empty, even if cache from other
809  * lcores are full.
810  *
811  * @param mp
812  *   A pointer to the mempool structure.
813  * @param obj_p
814  *   A pointer to a void * pointer (object) that will be filled.
815  * @return
816  *   - 0: Success; objects taken.
817  *   - -ENOENT: Not enough entries in the mempool; no object is retrieved.
818  */
819 static inline int
820 rte_mempool_mc_get(struct rte_mempool *mp, void **obj_p)
821 {
822         return rte_mempool_mc_get_bulk(mp, obj_p, 1);
823 }
824
825 /**
826  * Get one object from the mempool (NOT multi-consumers safe).
827  *
828  * If cache is enabled, objects will be retrieved first from cache,
829  * subsequently from the common pool. Note that it can return -ENOENT when
830  * the local cache and common pool are empty, even if cache from other
831  * lcores are full.
832  *
833  * @param mp
834  *   A pointer to the mempool structure.
835  * @param obj_p
836  *   A pointer to a void * pointer (object) that will be filled.
837  * @return
838  *   - 0: Success; objects taken.
839  *   - -ENOENT: Not enough entries in the mempool; no object is retrieved.
840  */
841 static inline int
842 rte_mempool_sc_get(struct rte_mempool *mp, void **obj_p)
843 {
844         return rte_mempool_sc_get_bulk(mp, obj_p, 1);
845 }
846
847 /**
848  * Get one object from the mempool.
849  *
850  * This function calls the multi-consumers or the single-consumer
851  * version, depending on the default behavior that was specified at
852  * mempool creation (see flags).
853  *
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
857  * lcores are full.
858  *
859  * @param mp
860  *   A pointer to the mempool structure.
861  * @param obj_p
862  *   A pointer to a void * pointer (object) that will be filled.
863  * @return
864  *   - 0: Success; objects taken.
865  *   - -ENOENT: Not enough entries in the mempool; no object is retrieved.
866  */
867 static inline int
868 rte_mempool_get(struct rte_mempool *mp, void **obj_p)
869 {
870         return rte_mempool_get_bulk(mp, obj_p, 1);
871 }
872
873 /**
874  * Return the number of entries in the mempool.
875  *
876  * When cache is enabled, this function has to browse the length of
877  * all lcores, so it should not be used in a data path, but only for
878  * debug purposes.
879  *
880  * @param mp
881  *   A pointer to the mempool structure.
882  * @return
883  *   The number of entries in the mempool.
884  */
885 unsigned rte_mempool_count(const struct rte_mempool *mp);
886
887 /**
888  * Return the number of free entries in the mempool.
889  *
890  * When cache is enabled, this function has to browse the length of
891  * all lcores, so it should not be used in a data path, but only for
892  * debug purposes.
893  *
894  * @param mp
895  *   A pointer to the mempool structure.
896  * @return
897  *   The number of free entries in the mempool.
898  */
899 static inline unsigned
900 rte_mempool_free_count(const struct rte_mempool *mp)
901 {
902         return mp->size - rte_mempool_count(mp);
903 }
904
905 /**
906  * Test if the mempool is full.
907  *
908  * When cache is enabled, this function has to browse the length of all
909  * lcores, so it should not be used in a data path, but only for debug
910  * purposes.
911  *
912  * @param mp
913  *   A pointer to the mempool structure.
914  * @return
915  *   - 1: The mempool is full.
916  *   - 0: The mempool is not full.
917  */
918 static inline int
919 rte_mempool_full(const struct rte_mempool *mp)
920 {
921         return !!(rte_mempool_count(mp) == mp->size);
922 }
923
924 /**
925  * Test if the mempool is empty.
926  *
927  * When cache is enabled, this function has to browse the length of all
928  * lcores, so it should not be used in a data path, but only for debug
929  * purposes.
930  *
931  * @param mp
932  *   A pointer to the mempool structure.
933  * @return
934  *   - 1: The mempool is empty.
935  *   - 0: The mempool is not empty.
936  */
937 static inline int
938 rte_mempool_empty(const struct rte_mempool *mp)
939 {
940         return !!(rte_mempool_count(mp) == 0);
941 }
942
943 /**
944  * Return the physical address of elt, which is an element of the pool mp.
945  *
946  * @param mp
947  *   A pointer to the mempool structure.
948  * @param elt
949  *   A pointer (virtual address) to the element of the pool.
950  * @return
951  *   The physical address of the elt element.
952  */
953 static inline phys_addr_t rte_mempool_virt2phy(const struct rte_mempool *mp,
954         const void *elt)
955 {
956         uintptr_t off;
957
958         off = (const char *)elt - (const char *)mp;
959         return mp->phys_addr + off;
960 }
961
962
963 /**
964  * Check the consistency of mempool objects.
965  *
966  * Verify the coherency of fields in the mempool structure. Also check
967  * that the cookies of mempool objects (even the ones that are not
968  * present in pool) have a correct value. If not, a panic will occur.
969  *
970  * @param mp
971  *   A pointer to the mempool structure.
972  */
973 void rte_mempool_audit(const struct rte_mempool *mp);
974
975 /**
976  * Return a pointer to the private data in an mempool structure.
977  *
978  * @param mp
979  *   A pointer to the mempool structure.
980  * @return
981  *   A pointer to the private data.
982  */
983 static inline void *rte_mempool_get_priv(struct rte_mempool *mp)
984 {
985         return (char *)mp + sizeof(struct rte_mempool);
986 }
987
988 /**
989  * Dump the status of all mempools on the console
990  */
991 void rte_mempool_list_dump(void);
992
993 /**
994  * Search a mempool from its name
995  *
996  * @param name
997  *   The name of the mempool.
998  * @return
999  *   The pointer to the mempool matching the name, or NULL if not found.NULL on error
1000  *   with rte_errno set appropriately. Possible rte_errno values include:
1001  *    - ENOENT - required entry not available to return.
1002  *
1003  */
1004 struct rte_mempool *rte_mempool_lookup(const char *name);
1005
1006 #ifdef __cplusplus
1007 }
1008 #endif
1009
1010 #endif /* _RTE_MEMPOOL_H_ */