first public release
[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  *  version: DPDK.L.1.2.3-3
34  */
35
36 #ifndef _RTE_MEMPOOL_H_
37 #define _RTE_MEMPOOL_H_
38
39 /**
40  * @file
41  * RTE Mempool.
42  *
43  * A memory pool is an allocator of fixed-size object. It is
44  * identified by its name, and uses a ring to store free objects. It
45  * provides some other optional services, like a per-core object
46  * cache, and an alignment helper to ensure that objects are padded
47  * to spread them equally on all RAM channels, ranks, and so on.
48  *
49  * Objects owned by a mempool should never be added in another
50  * mempool. When an object is freed using rte_mempool_put() or
51  * equivalent, the object data is not modified; the user can save some
52  * meta-data in the object data and retrieve them when allocating a
53  * new object.
54  *
55  * Note: the mempool implementation is not preemptable. A lcore must
56  * not be interrupted by another task that uses the same mempool
57  * (because it uses a ring which is not preemptable). Also, mempool
58  * functions must not be used outside the DPDK environment: for
59  * example, in linuxapp environment, a thread that is not created by
60  * the EAL must not use mempools. This is due to the per-lcore cache
61  * that won't work as rte_lcore_id() will not return a correct value.
62  */
63
64 #include <stdlib.h>
65 #include <stdint.h>
66 #include <errno.h>
67 #include <inttypes.h>
68 #include <sys/queue.h>
69
70 #include <rte_log.h>
71 #include <rte_debug.h>
72 #include <rte_memory.h>
73 #include <rte_lcore.h>
74 #include <rte_branch_prediction.h>
75 #include <rte_ring.h>
76
77 #ifdef __cplusplus
78 extern "C" {
79 #endif
80
81 #define RTE_MEMPOOL_HEADER_COOKIE1  0xbadbadbadadd2e55ULL /**< Header cookie. */
82 #define RTE_MEMPOOL_HEADER_COOKIE2  0xf2eef2eedadd2e55ULL /**< Header cookie. */
83 #define RTE_MEMPOOL_TRAILER_COOKIE  0xadd2e55badbadbadULL /**< Trailer cookie.*/
84
85 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
86 /**
87  * A structure that stores the mempool statistics (per-lcore).
88  */
89 struct rte_mempool_debug_stats {
90         uint64_t put_bulk;         /**< Number of puts. */
91         uint64_t put_objs;         /**< Number of objects successfully put. */
92         uint64_t get_success_bulk; /**< Successful allocation number. */
93         uint64_t get_success_objs; /**< Objects successfully allocated. */
94         uint64_t get_fail_bulk;    /**< Failed allocation number. */
95         uint64_t get_fail_objs;    /**< Objects that failed to be allocated. */
96 } __rte_cache_aligned;
97 #endif
98
99 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
100 /**
101  * A structure that stores a per-core object cache.
102  */
103 struct rte_mempool_cache {
104         unsigned len; /**< Cache len */
105         void *objs[RTE_MEMPOOL_CACHE_MAX_SIZE]; /**< Cache objects */
106 } __rte_cache_aligned;
107 #endif /* RTE_MEMPOOL_CACHE_MAX_SIZE > 0 */
108
109 #define RTE_MEMPOOL_NAMESIZE 32 /**< Maximum length of a memory pool. */
110
111 /**
112  * The RTE mempool structure.
113  */
114 struct rte_mempool {
115         TAILQ_ENTRY(rte_mempool) next;   /**< Next in list. */
116
117         char name[RTE_MEMPOOL_NAMESIZE]; /**< Name of mempool. */
118         struct rte_ring *ring;           /**< Ring to store objects. */
119         phys_addr_t phys_addr;           /**< Phys. addr. of mempool struct. */
120         int flags;                       /**< Flags of the mempool. */
121         uint32_t size;                   /**< Size of the mempool. */
122         uint32_t bulk_default;           /**< Default bulk count. */
123         uint32_t cache_size;             /**< Size of per-lcore local cache. */
124
125         uint32_t elt_size;               /**< Size of an element. */
126         uint32_t header_size;            /**< Size of header (before elt). */
127         uint32_t trailer_size;           /**< Size of trailer (after elt). */
128
129         unsigned private_data_size;      /**< Size of private data. */
130
131 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
132         /** Per-lcore local cache. */
133         struct rte_mempool_cache local_cache[RTE_MAX_LCORE];
134 #endif
135
136 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
137         /** Per-lcore statistics. */
138         struct rte_mempool_debug_stats stats[RTE_MAX_LCORE];
139 #endif
140 }  __rte_cache_aligned;
141
142 #define MEMPOOL_F_NO_SPREAD      0x0001 /**< Do not spread in memory. */
143 #define MEMPOOL_F_NO_CACHE_ALIGN 0x0002 /**< Do not align objs on cache lines.*/
144 #define MEMPOOL_F_SP_PUT         0x0004 /**< Default put is "single-producer".*/
145 #define MEMPOOL_F_SC_GET         0x0008 /**< Default get is "single-consumer".*/
146
147 /**
148  * When debug is enabled, store some statistics.
149  * @param mp
150  *   Pointer to the memory pool.
151  * @param name
152  *   Name of the statistics field to increment in the memory pool.
153  * @param n
154  *   Number to add to the object-oriented statistics.
155  */
156 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
157 #define __MEMPOOL_STAT_ADD(mp, name, n) do {                    \
158                 unsigned __lcore_id = rte_lcore_id();           \
159                 mp->stats[__lcore_id].name##_objs += n;         \
160                 mp->stats[__lcore_id].name##_bulk += 1;         \
161         } while(0)
162 #else
163 #define __MEMPOOL_STAT_ADD(mp, name, n) do {} while(0)
164 #endif
165
166 /**
167  * Get a pointer to a mempool pointer in the object header.
168  * @param obj
169  *   Pointer to object.
170  * @return
171  *   The pointer to the mempool from which the object was allocated.
172  */
173 static inline struct rte_mempool **__mempool_from_obj(void *obj)
174 {
175         struct rte_mempool **mpp;
176         unsigned off;
177
178         off = sizeof(struct rte_mempool *);
179 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
180         off += sizeof(uint64_t);
181 #endif
182         mpp = (struct rte_mempool **)((char *)obj - off);
183         return mpp;
184 }
185
186 /**
187  * Return a pointer to the mempool owning this object.
188  *
189  * @param obj
190  *   An object that is owned by a pool. If this is not the case,
191  *   the behavior is undefined.
192  * @return
193  *   A pointer to the mempool structure.
194  */
195 static inline const struct rte_mempool *rte_mempool_from_obj(void *obj)
196 {
197         struct rte_mempool * const *mpp;
198         mpp = __mempool_from_obj(obj);
199         return *mpp;
200 }
201
202 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
203 /* get header cookie value */
204 static inline uint64_t __mempool_read_header_cookie(const void *obj)
205 {
206         return *(const uint64_t *)((const char *)obj - sizeof(uint64_t));
207 }
208
209 /* get trailer cookie value */
210 static inline uint64_t __mempool_read_trailer_cookie(void *obj)
211 {
212         struct rte_mempool **mpp = __mempool_from_obj(obj);
213         return *(uint64_t *)((char *)obj + (*mpp)->elt_size);
214 }
215
216 /* write header cookie value */
217 static inline void __mempool_write_header_cookie(void *obj, int free)
218 {
219         uint64_t *cookie_p;
220         cookie_p = (uint64_t *)((char *)obj - sizeof(uint64_t));
221         if (free == 0)
222                 *cookie_p = RTE_MEMPOOL_HEADER_COOKIE1;
223         else
224                 *cookie_p = RTE_MEMPOOL_HEADER_COOKIE2;
225
226 }
227
228 /* write trailer cookie value */
229 static inline void __mempool_write_trailer_cookie(void *obj)
230 {
231         uint64_t *cookie_p;
232         struct rte_mempool **mpp = __mempool_from_obj(obj);
233         cookie_p = (uint64_t *)((char *)obj + (*mpp)->elt_size);
234         *cookie_p = RTE_MEMPOOL_TRAILER_COOKIE;
235 }
236 #endif /* RTE_LIBRTE_MEMPOOL_DEBUG */
237
238 /**
239  * Check and update cookies or panic.
240  *
241  * @param mp
242  *   Pointer to the memory pool.
243  * @param obj_table_const
244  *   Pointer to a table of void * pointers (objects).
245  * @param n
246  *   Index of object in object table.
247  * @param free
248  *   - 0: object is supposed to be allocated, mark it as free
249  *   - 1: object is supposed to be free, mark it as allocated
250  *   - 2: just check that cookie is valid (free or allocated)
251  */
252 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
253 #ifndef __INTEL_COMPILER
254 #pragma GCC diagnostic ignored "-Wcast-qual"
255 #endif
256 static inline void __mempool_check_cookies(const struct rte_mempool *mp,
257                                            void * const *obj_table_const,
258                                            unsigned n, int free)
259 {
260         uint64_t cookie;
261         void *tmp;
262         void *obj;
263         void **obj_table;
264
265         /* Force to drop the "const" attribute. This is done only when
266          * DEBUG is enabled */
267         tmp = (void *) obj_table_const;
268         obj_table = (void **) tmp;
269
270         while (n--) {
271                 obj = obj_table[n];
272
273                 if (rte_mempool_from_obj(obj) != mp)
274                         rte_panic("MEMPOOL: object is owned by another "
275                                   "mempool\n");
276
277                 cookie = __mempool_read_header_cookie(obj);
278
279                 if (free == 0) {
280                         if (cookie != RTE_MEMPOOL_HEADER_COOKIE1) {
281                                 rte_log_set_history(0);
282                                 RTE_LOG(CRIT, MEMPOOL,
283                                         "obj=%p, mempool=%p, cookie=%"PRIx64"\n",
284                                         obj, mp, cookie);
285                                 rte_panic("MEMPOOL: bad header cookie (put)\n");
286                         }
287                         __mempool_write_header_cookie(obj, 1);
288                 }
289                 else if (free == 1) {
290                         if (cookie != RTE_MEMPOOL_HEADER_COOKIE2) {
291                                 rte_log_set_history(0);
292                                 RTE_LOG(CRIT, MEMPOOL,
293                                         "obj=%p, mempool=%p, cookie=%"PRIx64"\n",
294                                         obj, mp, cookie);
295                                 rte_panic("MEMPOOL: bad header cookie (get)\n");
296                         }
297                         __mempool_write_header_cookie(obj, 0);
298                 }
299                 else if (free == 2) {
300                         if (cookie != RTE_MEMPOOL_HEADER_COOKIE1 &&
301                             cookie != RTE_MEMPOOL_HEADER_COOKIE2) {
302                                 rte_log_set_history(0);
303                                 RTE_LOG(CRIT, MEMPOOL,
304                                         "obj=%p, mempool=%p, cookie=%"PRIx64"\n",
305                                         obj, mp, cookie);
306                                 rte_panic("MEMPOOL: bad header cookie (audit)\n");
307                         }
308                 }
309                 cookie = __mempool_read_trailer_cookie(obj);
310                 if (cookie != RTE_MEMPOOL_TRAILER_COOKIE) {
311                         rte_log_set_history(0);
312                         RTE_LOG(CRIT, MEMPOOL,
313                                 "obj=%p, mempool=%p, cookie=%"PRIx64"\n",
314                                 obj, mp, cookie);
315                         rte_panic("MEMPOOL: bad trailer cookie\n");
316                 }
317         }
318 }
319 #ifndef __INTEL_COMPILER
320 #pragma GCC diagnostic error "-Wcast-qual"
321 #endif
322 #else
323 #define __mempool_check_cookies(mp, obj_table_const, n, free) do {} while(0)
324 #endif /* RTE_LIBRTE_MEMPOOL_DEBUG */
325
326 /**
327  * An object constructor callback function for mempool.
328  *
329  * Arguments are the mempool, the opaque pointer given by the user in
330  * rte_mempool_create(), the pointer to the element and the index of
331  * the element in the pool.
332  */
333 typedef void (rte_mempool_obj_ctor_t)(struct rte_mempool *, void *,
334                                       void *, unsigned);
335
336 /**
337  * A mempool constructor callback function.
338  *
339  * Arguments are the mempool and the opaque pointer given by the user in
340  * rte_mempool_create().
341  */
342 typedef void (rte_mempool_ctor_t)(struct rte_mempool *, void *);
343
344 /**
345  * Creates a new mempool named *name* in memory.
346  *
347  * This function uses ``memzone_reserve()`` to allocate memory. The
348  * pool contains n elements of elt_size. Its size is set to n. By
349  * default, bulk_default_count (the default number of elements to
350  * get/put in the pool) is set to 1. @see rte_mempool_set_bulk_count()
351  * to modify this valule.
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  * Set the default bulk count for put/get.
436  *
437  * The *count* parameter is the default number of bulk elements to
438  * get/put when using ``rte_mempool_*_{en,de}queue_bulk()``. It must
439  * be greater than 0 and less than half of the mempool size.
440  *
441  * @param mp
442  *   A pointer to the mempool structure.
443  * @param count
444  *   A new water mark value.
445  * @return
446  *   - 0: Success; default_bulk_count changed.
447  *   - -EINVAL: Invalid count value.
448  */
449 static inline int
450 rte_mempool_set_bulk_count(struct rte_mempool *mp, unsigned count)
451 {
452         if (unlikely(count == 0 || count >= mp->size))
453                 return -EINVAL;
454
455         mp->bulk_default = count;
456         return 0;
457 }
458
459 /**
460  * Get the default bulk count for put/get.
461  *
462  * @param mp
463  *   A pointer to the mempool structure.
464  * @return
465  *   The default bulk count for enqueue/dequeue.
466  */
467 static inline unsigned
468 rte_mempool_get_bulk_count(struct rte_mempool *mp)
469 {
470         return mp->bulk_default;
471 }
472
473 /**
474  * Dump the status of the mempool to the console.
475  *
476  * @param mp
477  *   A pointer to the mempool structure.
478  */
479 void rte_mempool_dump(const struct rte_mempool *mp);
480
481 /**
482  * @internal Put several objects back in the mempool; used internally.
483  * @param mp
484  *   A pointer to the mempool structure.
485  * @param obj_table
486  *   A pointer to a table of void * pointers (objects).
487  * @param n
488  *   The number of objects to store back in the mempool, must be strictly
489  *   positive.
490  * @param is_mp
491  *   Mono-producer (0) or multi-producers (1).
492  */
493 static inline void
494 __mempool_put_bulk(struct rte_mempool *mp, void * const *obj_table,
495                     unsigned n, int is_mp)
496 {
497 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
498         struct rte_mempool_cache *cache;
499         uint32_t cache_len;
500         void **cache_objs;
501         unsigned lcore_id = rte_lcore_id();
502         uint32_t cache_size = mp->cache_size;
503         uint32_t cache_add_count;
504 #endif /* RTE_MEMPOOL_CACHE_MAX_SIZE > 0 */
505
506         /* increment stat now, adding in mempool always success */
507         __MEMPOOL_STAT_ADD(mp, put, n);
508
509 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
510         /* cache is not enabled or single producer */
511         if (unlikely(cache_size == 0 || is_mp == 0))
512                 goto ring_enqueue;
513
514         cache = &mp->local_cache[lcore_id];
515         cache_len = cache->len;
516         cache_objs = cache->objs;
517
518         /* cache is full and we add many objects: enqueue in ring */
519         if (unlikely(cache_len == cache_size && n >= cache_size))
520                 goto ring_enqueue;
521
522         /*
523          * cache is full and we add few objects: enqueue the content
524          * of the cache in ring
525          */
526         if (unlikely(cache_len == cache_size)) {
527 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
528                 if (rte_ring_mp_enqueue_bulk(mp->ring, cache->objs,
529                                              cache_size) < 0)
530                         rte_panic("cannot put objects in mempool\n");
531 #else
532                 rte_ring_mp_enqueue_bulk(mp->ring, cache->objs,
533                                          cache_size);
534 #endif
535                 cache_len = 0;
536         }
537
538         /* determine how many objects we can add in cache */
539         if (likely(n <= cache_size - cache_len))
540                 cache_add_count = n;
541         else
542                 cache_add_count = cache_size - cache_len;
543
544         /* add in cache while there is enough room */
545         while (cache_add_count > 0) {
546                 cache_objs[cache_len] = *obj_table;
547                 obj_table++;
548                 cache_len++;
549                 n--;
550                 cache_add_count--;
551         }
552
553         cache->len = cache_len;
554
555         /* no more object to add, return */
556         if (likely(n == 0))
557                 return;
558
559  ring_enqueue:
560 #endif /* RTE_MEMPOOL_CACHE_MAX_SIZE > 0 */
561
562         /* push remaining objects in ring */
563 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
564         if (is_mp) {
565                 if (rte_ring_mp_enqueue_bulk(mp->ring, obj_table, n) < 0)
566                         rte_panic("cannot put objects in mempool\n");
567         }
568         else {
569                 if (rte_ring_sp_enqueue_bulk(mp->ring, obj_table, n) < 0)
570                         rte_panic("cannot put objects in mempool\n");
571         }
572 #else
573         if (is_mp)
574                 rte_ring_mp_enqueue_bulk(mp->ring, obj_table, n);
575         else
576                 rte_ring_sp_enqueue_bulk(mp->ring, obj_table, n);
577 #endif
578 }
579
580
581 /**
582  * Put several objects back in the mempool (multi-producers safe).
583  *
584  * @param mp
585  *   A pointer to the mempool structure.
586  * @param obj_table
587  *   A pointer to a table of void * pointers (objects).
588  * @param n
589  *   The number of objects to add in the mempool from the obj_table.
590  */
591 static inline void
592 rte_mempool_mp_put_bulk(struct rte_mempool *mp, void * const *obj_table,
593                         unsigned n)
594 {
595         __mempool_check_cookies(mp, obj_table, n, 0);
596         __mempool_put_bulk(mp, obj_table, n, 1);
597 }
598
599 /**
600  * Put several objects back in the mempool (NOT multi-producers safe).
601  *
602  * @param mp
603  *   A pointer to the mempool structure.
604  * @param obj_table
605  *   A pointer to a table of void * pointers (objects).
606  * @param n
607  *   The number of objects to add in the mempool from obj_table.
608  */
609 static inline void
610 rte_mempool_sp_put_bulk(struct rte_mempool *mp, void * const *obj_table,
611                         unsigned n)
612 {
613         __mempool_check_cookies(mp, obj_table, n, 0);
614         __mempool_put_bulk(mp, obj_table, n, 0);
615 }
616
617 /**
618  * Put several objects back in the mempool.
619  *
620  * This function calls the multi-producer or the single-producer
621  * version depending on the default behavior that was specified at
622  * mempool creation time (see flags).
623  *
624  * @param mp
625  *   A pointer to the mempool structure.
626  * @param obj_table
627  *   A pointer to a table of void * pointers (objects).
628  * @param n
629  *   The number of objects to add in the mempool from obj_table.
630  */
631 static inline void
632 rte_mempool_put_bulk(struct rte_mempool *mp, void * const *obj_table,
633                      unsigned n)
634 {
635         __mempool_check_cookies(mp, obj_table, n, 0);
636         __mempool_put_bulk(mp, obj_table, n, !(mp->flags & MEMPOOL_F_SP_PUT));
637 }
638
639 /**
640  * Put one object in the mempool (multi-producers safe).
641  *
642  * @param mp
643  *   A pointer to the mempool structure.
644  * @param obj
645  *   A pointer to the object to be added.
646  */
647 static inline void
648 rte_mempool_mp_put(struct rte_mempool *mp, void *obj)
649 {
650         rte_mempool_mp_put_bulk(mp, &obj, 1);
651 }
652
653 /**
654  * Put one object back in the mempool (NOT multi-producers safe).
655  *
656  * @param mp
657  *   A pointer to the mempool structure.
658  * @param obj
659  *   A pointer to the object to be added.
660  */
661 static inline void
662 rte_mempool_sp_put(struct rte_mempool *mp, void *obj)
663 {
664         rte_mempool_sp_put_bulk(mp, &obj, 1);
665 }
666
667 /**
668  * Put one object back in the mempool.
669  *
670  * This function calls the multi-producer or the single-producer
671  * version depending on the default behavior that was specified at
672  * mempool creation time (see flags).
673  *
674  * @param mp
675  *   A pointer to the mempool structure.
676  * @param obj
677  *   A pointer to the object to be added.
678  */
679 static inline void
680 rte_mempool_put(struct rte_mempool *mp, void *obj)
681 {
682         rte_mempool_put_bulk(mp, &obj, 1);
683 }
684
685 /**
686  * @internal Get several objects from the mempool; used internally.
687  * @param mp
688  *   A pointer to the mempool structure.
689  * @param obj_table
690  *   A pointer to a table of void * pointers (objects).
691  * @param n
692  *   The number of objects to get, must be strictly positive.
693  * @param is_mc
694  *   Mono-consumer (0) or multi-consumers (1).
695  * @return
696  *   - >=0: Success; number of objects supplied.
697  *   - <0: Error; code of ring dequeue function.
698  */
699 static inline int
700 __mempool_get_bulk(struct rte_mempool *mp, void **obj_table,
701                    unsigned n, int is_mc)
702 {
703         int ret;
704 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
705         unsigned n_orig = n;
706 #endif
707 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
708         struct rte_mempool_cache *cache;
709         uint32_t cache_len, cache_len_save = 0;
710         void **cache_objs;
711         unsigned lcore_id = rte_lcore_id();
712         uint32_t cache_size = mp->cache_size;
713         uint32_t cache_del_count;
714
715         cache = &mp->local_cache[lcore_id];
716
717         /* cache is not enabled or single consumer */
718         if (unlikely(cache_size == 0 || is_mc == 0))
719                 goto ring_dequeue;
720
721         cache_len = cache->len;
722         cache_objs = cache->objs;
723
724         /* cache is empty and we need many objects: dequeue from ring */
725         if (unlikely(cache_len == 0 && n >= cache_size))
726                 goto ring_dequeue;
727
728         /* cache is empty and we dequeue few objects: fill the cache first */
729         if (unlikely(cache_len == 0 && n < cache_size)) {
730                 ret = rte_ring_mc_dequeue_bulk(mp->ring, cache_objs,
731                                                cache_size);
732                 if (unlikely(ret < 0)) {
733                         __MEMPOOL_STAT_ADD(mp, get_fail, n_orig);
734                         return ret;
735                 }
736
737                 cache_len = cache_size;
738         }
739
740         if (likely(n <= cache_len))
741                 cache_del_count = n;
742         else
743                 cache_del_count = cache_len;
744
745         cache_len_save = cache_len;
746
747         /* add in cache only while there is enough room */
748         while (cache_del_count > 0) {
749                 cache_len--;
750                 *obj_table = cache_objs[cache_len];
751                 obj_table++;
752                 n--;
753                 cache_del_count--;
754         }
755
756         cache->len = cache_len;
757
758         /* no more object to get, return */
759         if (likely(n == 0)) {
760                 __MEMPOOL_STAT_ADD(mp, get_success, n_orig);
761                 return 0;
762         }
763
764  ring_dequeue:
765 #endif /* RTE_MEMPOOL_CACHE_MAX_SIZE > 0 */
766
767         /* get remaining objects from ring */
768         if (is_mc)
769                 ret = rte_ring_mc_dequeue_bulk(mp->ring, obj_table, n);
770         else
771                 ret = rte_ring_sc_dequeue_bulk(mp->ring, obj_table, n);
772
773 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
774         /*
775          * bad luck, the ring is empty but we already dequeued some
776          * entries from cache, we have to restore them
777          */
778         if (unlikely(ret < 0 && cache_len_save != 0))
779                 cache->len = cache_len_save;
780 #endif
781
782         if (ret < 0)
783                 __MEMPOOL_STAT_ADD(mp, get_fail, n_orig);
784         else
785                 __MEMPOOL_STAT_ADD(mp, get_success, n_orig);
786
787         return ret;
788 }
789
790 /**
791  * Get several objects from the mempool (multi-consumers safe).
792  *
793  * If cache is enabled, objects will be retrieved first from cache,
794  * subsequently from the common pool. Note that it can return -ENOENT when
795  * the local cache and common pool are empty, even if cache from other
796  * lcores are full.
797  *
798  * @param mp
799  *   A pointer to the mempool structure.
800  * @param obj_table
801  *   A pointer to a table of void * pointers (objects) that will be filled.
802  * @param n
803  *   The number of objects to get from mempool to obj_table.
804  * @return
805  *   - 0: Success; objects taken.
806  *   - -ENOENT: Not enough entries in the mempool; no object is retrieved.
807  */
808 static inline int
809 rte_mempool_mc_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned n)
810 {
811         int ret;
812         ret = __mempool_get_bulk(mp, obj_table, n, 1);
813         if (ret == 0)
814                 __mempool_check_cookies(mp, obj_table, n, 1);
815         return ret;
816 }
817
818 /**
819  * Get several objects from the mempool (NOT multi-consumers safe).
820  *
821  * If cache is enabled, objects will be retrieved first from cache,
822  * subsequently from the common pool. Note that it can return -ENOENT when
823  * the local cache and common pool are empty, even if cache from other
824  * lcores are full.
825  *
826  * @param mp
827  *   A pointer to the mempool structure.
828  * @param obj_table
829  *   A pointer to a table of void * pointers (objects) that will be filled.
830  * @param n
831  *   The number of objects to get from the mempool to obj_table.
832  * @return
833  *   - 0: Success; objects taken.
834  *   - -ENOENT: Not enough entries in the mempool; no object is
835  *     retrieved.
836  */
837 static inline int
838 rte_mempool_sc_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned n)
839 {
840         int ret;
841         ret = __mempool_get_bulk(mp, obj_table, n, 0);
842         if (ret == 0)
843                 __mempool_check_cookies(mp, obj_table, n, 1);
844         return ret;
845 }
846
847 /**
848  * Get several objects from the mempool.
849  *
850  * This function calls the multi-consumers or the single-consumer
851  * version, depending on the default behaviour that was specified at
852  * mempool creation time (see flags).
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_table
862  *   A pointer to a table of void * pointers (objects) that will be filled.
863  * @param n
864  *   The number of objects to get from the mempool to obj_table.
865  * @return
866  *   - 0: Success; objects taken
867  *   - -ENOENT: Not enough entries in the mempool; no object is retrieved.
868  */
869 static inline int
870 rte_mempool_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned n)
871 {
872         int ret;
873         ret = __mempool_get_bulk(mp, obj_table, n,
874                                  !(mp->flags & MEMPOOL_F_SC_GET));
875         if (ret == 0)
876                 __mempool_check_cookies(mp, obj_table, n, 1);
877         return ret;
878 }
879
880 /**
881  * Get one object from the mempool (multi-consumers safe).
882  *
883  * If cache is enabled, objects will be retrieved first from cache,
884  * subsequently from the common pool. Note that it can return -ENOENT when
885  * the local cache and common pool are empty, even if cache from other
886  * lcores are full.
887  *
888  * @param mp
889  *   A pointer to the mempool structure.
890  * @param obj_p
891  *   A pointer to a void * pointer (object) that will be filled.
892  * @return
893  *   - 0: Success; objects taken.
894  *   - -ENOENT: Not enough entries in the mempool; no object is retrieved.
895  */
896 static inline int
897 rte_mempool_mc_get(struct rte_mempool *mp, void **obj_p)
898 {
899         return rte_mempool_mc_get_bulk(mp, obj_p, 1);
900 }
901
902 /**
903  * Get one object from the mempool (NOT multi-consumers safe).
904  *
905  * If cache is enabled, objects will be retrieved first from cache,
906  * subsequently from the common pool. Note that it can return -ENOENT when
907  * the local cache and common pool are empty, even if cache from other
908  * lcores are full.
909  *
910  * @param mp
911  *   A pointer to the mempool structure.
912  * @param obj_p
913  *   A pointer to a void * pointer (object) that will be filled.
914  * @return
915  *   - 0: Success; objects taken.
916  *   - -ENOENT: Not enough entries in the mempool; no object is retrieved.
917  */
918 static inline int
919 rte_mempool_sc_get(struct rte_mempool *mp, void **obj_p)
920 {
921         return rte_mempool_sc_get_bulk(mp, obj_p, 1);
922 }
923
924 /**
925  * Get one object from the mempool.
926  *
927  * This function calls the multi-consumers or the single-consumer
928  * version, depending on the default behavior that was specified at
929  * mempool creation (see flags).
930  *
931  * If cache is enabled, objects will be retrieved first from cache,
932  * subsequently from the common pool. Note that it can return -ENOENT when
933  * the local cache and common pool are empty, even if cache from other
934  * lcores are full.
935  *
936  * @param mp
937  *   A pointer to the mempool structure.
938  * @param obj_p
939  *   A pointer to a void * pointer (object) that will be filled.
940  * @return
941  *   - 0: Success; objects taken.
942  *   - -ENOENT: Not enough entries in the mempool; no object is retrieved.
943  */
944 static inline int
945 rte_mempool_get(struct rte_mempool *mp, void **obj_p)
946 {
947         return rte_mempool_get_bulk(mp, obj_p, 1);
948 }
949
950 /**
951  * Return the number of entries in the mempool.
952  *
953  * When cache is enabled, this function has to browse the length of
954  * all lcores, so it should not be used in a data path, but only for
955  * debug purposes.
956  *
957  * @param mp
958  *   A pointer to the mempool structure.
959  * @return
960  *   The number of entries in the mempool.
961  */
962 unsigned rte_mempool_count(const struct rte_mempool *mp);
963
964 /**
965  * Return the number of free entries in the mempool.
966  *
967  * When cache is enabled, this function has to browse the length of
968  * all lcores, so it should not be used in a data path, but only for
969  * debug purposes.
970  *
971  * @param mp
972  *   A pointer to the mempool structure.
973  * @return
974  *   The number of free entries in the mempool.
975  */
976 static inline unsigned
977 rte_mempool_free_count(const struct rte_mempool *mp)
978 {
979         return mp->size - rte_mempool_count(mp);
980 }
981
982 /**
983  * Test if the mempool is full.
984  *
985  * When cache is enabled, this function has to browse the length of all
986  * lcores, so it should not be used in a data path, but only for debug
987  * purposes.
988  *
989  * @param mp
990  *   A pointer to the mempool structure.
991  * @return
992  *   - 1: The mempool is full.
993  *   - 0: The mempool is not full.
994  */
995 static inline int
996 rte_mempool_full(const struct rte_mempool *mp)
997 {
998         return !!(rte_mempool_count(mp) == mp->size);
999 }
1000
1001 /**
1002  * Test if the mempool is empty.
1003  *
1004  * When cache is enabled, this function has to browse the length of all
1005  * lcores, so it should not be used in a data path, but only for debug
1006  * purposes.
1007  *
1008  * @param mp
1009  *   A pointer to the mempool structure.
1010  * @return
1011  *   - 1: The mempool is empty.
1012  *   - 0: The mempool is not empty.
1013  */
1014 static inline int
1015 rte_mempool_empty(const struct rte_mempool *mp)
1016 {
1017         return !!(rte_mempool_count(mp) == 0);
1018 }
1019
1020 /**
1021  * Return the physical address of elt, which is an element of the pool mp.
1022  *
1023  * @param mp
1024  *   A pointer to the mempool structure.
1025  * @param elt
1026  *   A pointer (virtual address) to the element of the pool.
1027  * @return
1028  *   The physical address of the elt element.
1029  */
1030 static inline phys_addr_t rte_mempool_virt2phy(const struct rte_mempool *mp,
1031         const void *elt)
1032 {
1033         uintptr_t off;
1034
1035         off = (const char *)elt - (const char *)mp;
1036         return mp->phys_addr + off;
1037 }
1038
1039
1040 /**
1041  * Check the consistency of mempool objects.
1042  *
1043  * Verify the coherency of fields in the mempool structure. Also check
1044  * that the cookies of mempool objects (even the ones that are not
1045  * present in pool) have a correct value. If not, a panic will occur.
1046  *
1047  * @param mp
1048  *   A pointer to the mempool structure.
1049  */
1050 void rte_mempool_audit(const struct rte_mempool *mp);
1051
1052 /**
1053  * Return a pointer to the private data in an mempool structure.
1054  *
1055  * @param mp
1056  *   A pointer to the mempool structure.
1057  * @return
1058  *   A pointer to the private data.
1059  */
1060 static inline void *rte_mempool_get_priv(struct rte_mempool *mp)
1061 {
1062         return (char *)mp + sizeof(struct rte_mempool);
1063 }
1064
1065 /**
1066  * Dump the status of all mempools on the console
1067  */
1068 void rte_mempool_list_dump(void);
1069
1070 /**
1071  * Search a mempool from its name
1072  *
1073  * @param name
1074  *   The name of the mempool.
1075  * @return
1076  *   The pointer to the mempool matching the name, or NULL if not found.NULL on error
1077  *   with rte_errno set appropriately. Possible rte_errno values include:
1078  *    - ENOENT - required entry not available to return.
1079  *
1080  */
1081 struct rte_mempool *rte_mempool_lookup(const char *name);
1082
1083 #ifdef __cplusplus
1084 }
1085 #endif
1086
1087 #endif /* _RTE_MEMPOOL_H_ */