tailq: move to dynamic tailq
[dpdk.git] / lib / librte_mempool / rte_mempool.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 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 #ifndef _RTE_MEMPOOL_H_
35 #define _RTE_MEMPOOL_H_
36
37 /**
38  * @file
39  * RTE Mempool.
40  *
41  * A memory pool is an allocator of fixed-size object. It is
42  * identified by its name, and uses a ring to store free objects. It
43  * provides some other optional services, like a per-core object
44  * cache, and an alignment helper to ensure that objects are padded
45  * to spread them equally on all RAM channels, ranks, and so on.
46  *
47  * Objects owned by a mempool should never be added in another
48  * mempool. When an object is freed using rte_mempool_put() or
49  * equivalent, the object data is not modified; the user can save some
50  * meta-data in the object data and retrieve them when allocating a
51  * new object.
52  *
53  * Note: the mempool implementation is not preemptable. A lcore must
54  * not be interrupted by another task that uses the same mempool
55  * (because it uses a ring which is not preemptable). Also, mempool
56  * functions must not be used outside the DPDK environment: for
57  * example, in linuxapp environment, a thread that is not created by
58  * the EAL must not use mempools. This is due to the per-lcore cache
59  * that won't work as rte_lcore_id() will not return a correct value.
60  */
61
62 #include <stdio.h>
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 struct rte_mempool_objsz {
113         uint32_t elt_size;     /**< Size of an element. */
114         uint32_t header_size;  /**< Size of header (before elt). */
115         uint32_t trailer_size; /**< Size of trailer (after elt). */
116         uint32_t total_size;
117         /**< Total size of an object (header + elt + trailer). */
118 };
119
120 #define RTE_MEMPOOL_NAMESIZE 32 /**< Maximum length of a memory pool. */
121 #define RTE_MEMPOOL_MZ_PREFIX "MP_"
122
123 /* "MP_<name>" */
124 #define RTE_MEMPOOL_MZ_FORMAT   RTE_MEMPOOL_MZ_PREFIX "%s"
125
126 #ifdef RTE_LIBRTE_XEN_DOM0
127
128 /* "<name>_MP_elt" */
129 #define RTE_MEMPOOL_OBJ_NAME    "%s_" RTE_MEMPOOL_MZ_PREFIX "elt"
130
131 #else
132
133 #define RTE_MEMPOOL_OBJ_NAME    RTE_MEMPOOL_MZ_FORMAT
134
135 #endif /* RTE_LIBRTE_XEN_DOM0 */
136
137 #define MEMPOOL_PG_SHIFT_MAX    (sizeof(uintptr_t) * CHAR_BIT - 1)
138
139 /** Mempool over one chunk of physically continuous memory */
140 #define MEMPOOL_PG_NUM_DEFAULT  1
141
142 /**
143  * The RTE mempool structure.
144  */
145 struct rte_mempool {
146         char name[RTE_MEMPOOL_NAMESIZE]; /**< Name of mempool. */
147         struct rte_ring *ring;           /**< Ring to store objects. */
148         phys_addr_t phys_addr;           /**< Phys. addr. of mempool struct. */
149         int flags;                       /**< Flags of the mempool. */
150         uint32_t size;                   /**< Size of the mempool. */
151         uint32_t cache_size;             /**< Size of per-lcore local cache. */
152         uint32_t cache_flushthresh;
153         /**< Threshold before we flush excess elements. */
154
155         uint32_t elt_size;               /**< Size of an element. */
156         uint32_t header_size;            /**< Size of header (before elt). */
157         uint32_t trailer_size;           /**< Size of trailer (after elt). */
158
159         unsigned private_data_size;      /**< Size of private data. */
160
161 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
162         /** Per-lcore local cache. */
163         struct rte_mempool_cache local_cache[RTE_MAX_LCORE];
164 #endif
165
166 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
167         /** Per-lcore statistics. */
168         struct rte_mempool_debug_stats stats[RTE_MAX_LCORE];
169 #endif
170
171         /* Address translation support, starts from next cache line. */
172
173         /** Number of elements in the elt_pa array. */
174         uint32_t    pg_num __rte_cache_aligned;
175         uint32_t    pg_shift;     /**< LOG2 of the physical pages. */
176         uintptr_t   pg_mask;      /**< physical page mask value. */
177         uintptr_t   elt_va_start;
178         /**< Virtual address of the first mempool object. */
179         uintptr_t   elt_va_end;
180         /**< Virtual address of the <size + 1> mempool object. */
181         phys_addr_t elt_pa[MEMPOOL_PG_NUM_DEFAULT];
182         /**< Array of physical pages addresses for the mempool objects buffer. */
183
184 }  __rte_cache_aligned;
185
186 #define MEMPOOL_F_NO_SPREAD      0x0001 /**< Do not spread in memory. */
187 #define MEMPOOL_F_NO_CACHE_ALIGN 0x0002 /**< Do not align objs on cache lines.*/
188 #define MEMPOOL_F_SP_PUT         0x0004 /**< Default put is "single-producer".*/
189 #define MEMPOOL_F_SC_GET         0x0008 /**< Default get is "single-consumer".*/
190
191 /**
192  * @internal When debug is enabled, store some statistics.
193  * @param mp
194  *   Pointer to the memory pool.
195  * @param name
196  *   Name of the statistics field to increment in the memory pool.
197  * @param n
198  *   Number to add to the object-oriented statistics.
199  */
200 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
201 #define __MEMPOOL_STAT_ADD(mp, name, n) do {                    \
202                 unsigned __lcore_id = rte_lcore_id();           \
203                 if (__lcore_id < RTE_MAX_LCORE) {               \
204                         mp->stats[__lcore_id].name##_objs += n; \
205                         mp->stats[__lcore_id].name##_bulk += 1; \
206                 }                                               \
207         } while(0)
208 #else
209 #define __MEMPOOL_STAT_ADD(mp, name, n) do {} while(0)
210 #endif
211
212 /**
213  * Calculates size of the mempool header.
214  * @param mp
215  *   Pointer to the memory pool.
216  * @param pgn
217  *   Number of page used to store mempool objects.
218  */
219 #define MEMPOOL_HEADER_SIZE(mp, pgn)    (sizeof(*(mp)) + \
220         RTE_ALIGN_CEIL(((pgn) - RTE_DIM((mp)->elt_pa)) * \
221         sizeof ((mp)->elt_pa[0]), RTE_CACHE_LINE_SIZE))
222
223 /**
224  * Returns TRUE if whole mempool is allocated in one contiguous block of memory.
225  */
226 #define MEMPOOL_IS_CONTIG(mp)                      \
227         ((mp)->pg_num == MEMPOOL_PG_NUM_DEFAULT && \
228         (mp)->phys_addr == (mp)->elt_pa[0])
229
230 /**
231  * @internal Get a pointer to a mempool pointer in the object header.
232  * @param obj
233  *   Pointer to object.
234  * @return
235  *   The pointer to the mempool from which the object was allocated.
236  */
237 static inline struct rte_mempool **__mempool_from_obj(void *obj)
238 {
239         struct rte_mempool **mpp;
240         unsigned off;
241
242         off = sizeof(struct rte_mempool *);
243 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
244         off += sizeof(uint64_t);
245 #endif
246         mpp = (struct rte_mempool **)((char *)obj - off);
247         return mpp;
248 }
249
250 /**
251  * Return a pointer to the mempool owning this object.
252  *
253  * @param obj
254  *   An object that is owned by a pool. If this is not the case,
255  *   the behavior is undefined.
256  * @return
257  *   A pointer to the mempool structure.
258  */
259 static inline const struct rte_mempool *rte_mempool_from_obj(void *obj)
260 {
261         struct rte_mempool * const *mpp;
262         mpp = __mempool_from_obj(obj);
263         return *mpp;
264 }
265
266 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
267 /* get header cookie value */
268 static inline uint64_t __mempool_read_header_cookie(const void *obj)
269 {
270         return *(const uint64_t *)((const char *)obj - sizeof(uint64_t));
271 }
272
273 /* get trailer cookie value */
274 static inline uint64_t __mempool_read_trailer_cookie(void *obj)
275 {
276         struct rte_mempool **mpp = __mempool_from_obj(obj);
277         return *(uint64_t *)((char *)obj + (*mpp)->elt_size);
278 }
279
280 /* write header cookie value */
281 static inline void __mempool_write_header_cookie(void *obj, int free)
282 {
283         uint64_t *cookie_p;
284         cookie_p = (uint64_t *)((char *)obj - sizeof(uint64_t));
285         if (free == 0)
286                 *cookie_p = RTE_MEMPOOL_HEADER_COOKIE1;
287         else
288                 *cookie_p = RTE_MEMPOOL_HEADER_COOKIE2;
289
290 }
291
292 /* write trailer cookie value */
293 static inline void __mempool_write_trailer_cookie(void *obj)
294 {
295         uint64_t *cookie_p;
296         struct rte_mempool **mpp = __mempool_from_obj(obj);
297         cookie_p = (uint64_t *)((char *)obj + (*mpp)->elt_size);
298         *cookie_p = RTE_MEMPOOL_TRAILER_COOKIE;
299 }
300 #endif /* RTE_LIBRTE_MEMPOOL_DEBUG */
301
302 /**
303  * @internal Check and update cookies or panic.
304  *
305  * @param mp
306  *   Pointer to the memory pool.
307  * @param obj_table_const
308  *   Pointer to a table of void * pointers (objects).
309  * @param n
310  *   Index of object in object table.
311  * @param free
312  *   - 0: object is supposed to be allocated, mark it as free
313  *   - 1: object is supposed to be free, mark it as allocated
314  *   - 2: just check that cookie is valid (free or allocated)
315  */
316 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
317 #ifndef __INTEL_COMPILER
318 #pragma GCC diagnostic ignored "-Wcast-qual"
319 #endif
320 static inline void __mempool_check_cookies(const struct rte_mempool *mp,
321                                            void * const *obj_table_const,
322                                            unsigned n, int free)
323 {
324         uint64_t cookie;
325         void *tmp;
326         void *obj;
327         void **obj_table;
328
329         /* Force to drop the "const" attribute. This is done only when
330          * DEBUG is enabled */
331         tmp = (void *) obj_table_const;
332         obj_table = (void **) tmp;
333
334         while (n--) {
335                 obj = obj_table[n];
336
337                 if (rte_mempool_from_obj(obj) != mp)
338                         rte_panic("MEMPOOL: object is owned by another "
339                                   "mempool\n");
340
341                 cookie = __mempool_read_header_cookie(obj);
342
343                 if (free == 0) {
344                         if (cookie != RTE_MEMPOOL_HEADER_COOKIE1) {
345                                 rte_log_set_history(0);
346                                 RTE_LOG(CRIT, MEMPOOL,
347                                         "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
348                                         obj, (const void *) mp, cookie);
349                                 rte_panic("MEMPOOL: bad header cookie (put)\n");
350                         }
351                         __mempool_write_header_cookie(obj, 1);
352                 }
353                 else if (free == 1) {
354                         if (cookie != RTE_MEMPOOL_HEADER_COOKIE2) {
355                                 rte_log_set_history(0);
356                                 RTE_LOG(CRIT, MEMPOOL,
357                                         "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
358                                         obj, (const void *) mp, cookie);
359                                 rte_panic("MEMPOOL: bad header cookie (get)\n");
360                         }
361                         __mempool_write_header_cookie(obj, 0);
362                 }
363                 else if (free == 2) {
364                         if (cookie != RTE_MEMPOOL_HEADER_COOKIE1 &&
365                             cookie != RTE_MEMPOOL_HEADER_COOKIE2) {
366                                 rte_log_set_history(0);
367                                 RTE_LOG(CRIT, MEMPOOL,
368                                         "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
369                                         obj, (const void *) mp, cookie);
370                                 rte_panic("MEMPOOL: bad header cookie (audit)\n");
371                         }
372                 }
373                 cookie = __mempool_read_trailer_cookie(obj);
374                 if (cookie != RTE_MEMPOOL_TRAILER_COOKIE) {
375                         rte_log_set_history(0);
376                         RTE_LOG(CRIT, MEMPOOL,
377                                 "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
378                                 obj, (const void *) mp, cookie);
379                         rte_panic("MEMPOOL: bad trailer cookie\n");
380                 }
381         }
382 }
383 #ifndef __INTEL_COMPILER
384 #pragma GCC diagnostic error "-Wcast-qual"
385 #endif
386 #else
387 #define __mempool_check_cookies(mp, obj_table_const, n, free) do {} while(0)
388 #endif /* RTE_LIBRTE_MEMPOOL_DEBUG */
389
390 /**
391  * An mempool's object iterator callback function.
392  */
393 typedef void (*rte_mempool_obj_iter_t)(void * /*obj_iter_arg*/,
394         void * /*obj_start*/,
395         void * /*obj_end*/,
396         uint32_t /*obj_index */);
397
398 /*
399  * Iterates across objects of the given size and alignment in the
400  * provided chunk of memory. The given memory buffer can consist of
401  * disjoint physical pages.
402  * For each object calls the provided callback (if any).
403  * Used to populate mempool, walk through all elements of the mempool,
404  * estimate how many elements of the given size could be created in the given
405  * memory buffer.
406  * @param vaddr
407  *   Virtual address of the memory buffer.
408  * @param elt_num
409  *   Maximum number of objects to iterate through.
410  * @param elt_sz
411  *   Size of each object.
412  * @param paddr
413  *   Array of phyiscall addresses of the pages that comprises given memory
414  *   buffer.
415  * @param pg_num
416  *   Number of elements in the paddr array.
417  * @param pg_shift
418  *   LOG2 of the physical pages size.
419  * @param obj_iter
420  *   Object iterator callback function (could be NULL).
421  * @param obj_iter_arg
422  *   User defined Prameter for the object iterator callback function.
423  *
424  * @return
425  *   Number of objects iterated through.
426  */
427
428 uint32_t rte_mempool_obj_iter(void *vaddr,
429         uint32_t elt_num, size_t elt_sz, size_t align,
430         const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift,
431         rte_mempool_obj_iter_t obj_iter, void *obj_iter_arg);
432
433 /**
434  * An object constructor callback function for mempool.
435  *
436  * Arguments are the mempool, the opaque pointer given by the user in
437  * rte_mempool_create(), the pointer to the element and the index of
438  * the element in the pool.
439  */
440 typedef void (rte_mempool_obj_ctor_t)(struct rte_mempool *, void *,
441                                       void *, unsigned);
442
443 /**
444  * A mempool constructor callback function.
445  *
446  * Arguments are the mempool and the opaque pointer given by the user in
447  * rte_mempool_create().
448  */
449 typedef void (rte_mempool_ctor_t)(struct rte_mempool *, void *);
450
451 /**
452  * Creates a new mempool named *name* in memory.
453  *
454  * This function uses ``memzone_reserve()`` to allocate memory. The
455  * pool contains n elements of elt_size. Its size is set to n.
456  * All elements of the mempool are allocated together with the mempool header,
457  * in one physically continuous chunk of memory.
458  *
459  * @param name
460  *   The name of the mempool.
461  * @param n
462  *   The number of elements in the mempool. The optimum size (in terms of
463  *   memory usage) for a mempool is when n is a power of two minus one:
464  *   n = (2^q - 1).
465  * @param elt_size
466  *   The size of each element.
467  * @param cache_size
468  *   If cache_size is non-zero, the rte_mempool library will try to
469  *   limit the accesses to the common lockless pool, by maintaining a
470  *   per-lcore object cache. This argument must be lower or equal to
471  *   CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE. It is advised to choose
472  *   cache_size to have "n modulo cache_size == 0": if this is
473  *   not the case, some elements will always stay in the pool and will
474  *   never be used. The access to the per-lcore table is of course
475  *   faster than the multi-producer/consumer pool. The cache can be
476  *   disabled if the cache_size argument is set to 0; it can be useful to
477  *   avoid losing objects in cache. Note that even if not used, the
478  *   memory space for cache is always reserved in a mempool structure,
479  *   except if CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE is set to 0.
480  * @param private_data_size
481  *   The size of the private data appended after the mempool
482  *   structure. This is useful for storing some private data after the
483  *   mempool structure, as is done for rte_mbuf_pool for example.
484  * @param mp_init
485  *   A function pointer that is called for initialization of the pool,
486  *   before object initialization. The user can initialize the private
487  *   data in this function if needed. This parameter can be NULL if
488  *   not needed.
489  * @param mp_init_arg
490  *   An opaque pointer to data that can be used in the mempool
491  *   constructor function.
492  * @param obj_init
493  *   A function pointer that is called for each object at
494  *   initialization of the pool. The user can set some meta data in
495  *   objects if needed. This parameter can be NULL if not needed.
496  *   The obj_init() function takes the mempool pointer, the init_arg,
497  *   the object pointer and the object number as parameters.
498  * @param obj_init_arg
499  *   An opaque pointer to data that can be used as an argument for
500  *   each call to the object constructor function.
501  * @param socket_id
502  *   The *socket_id* argument is the socket identifier in the case of
503  *   NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
504  *   constraint for the reserved zone.
505  * @param flags
506  *   The *flags* arguments is an OR of following flags:
507  *   - MEMPOOL_F_NO_SPREAD: By default, objects addresses are spread
508  *     between channels in RAM: the pool allocator will add padding
509  *     between objects depending on the hardware configuration. See
510  *     Memory alignment constraints for details. If this flag is set,
511  *     the allocator will just align them to a cache line.
512  *   - MEMPOOL_F_NO_CACHE_ALIGN: By default, the returned objects are
513  *     cache-aligned. This flag removes this constraint, and no
514  *     padding will be present between objects. This flag implies
515  *     MEMPOOL_F_NO_SPREAD.
516  *   - MEMPOOL_F_SP_PUT: If this flag is set, the default behavior
517  *     when using rte_mempool_put() or rte_mempool_put_bulk() is
518  *     "single-producer". Otherwise, it is "multi-producers".
519  *   - MEMPOOL_F_SC_GET: If this flag is set, the default behavior
520  *     when using rte_mempool_get() or rte_mempool_get_bulk() is
521  *     "single-consumer". Otherwise, it is "multi-consumers".
522  * @return
523  *   The pointer to the new allocated mempool, on success. NULL on error
524  *   with rte_errno set appropriately. Possible rte_errno values include:
525  *    - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
526  *    - E_RTE_SECONDARY - function was called from a secondary process instance
527  *    - EINVAL - cache size provided is too large
528  *    - ENOSPC - the maximum number of memzones has already been allocated
529  *    - EEXIST - a memzone with the same name already exists
530  *    - ENOMEM - no appropriate memory area found in which to create memzone
531  */
532 struct rte_mempool *
533 rte_mempool_create(const char *name, unsigned n, unsigned elt_size,
534                    unsigned cache_size, unsigned private_data_size,
535                    rte_mempool_ctor_t *mp_init, void *mp_init_arg,
536                    rte_mempool_obj_ctor_t *obj_init, void *obj_init_arg,
537                    int socket_id, unsigned flags);
538
539 /**
540  * Creates a new mempool named *name* in memory.
541  *
542  * This function uses ``memzone_reserve()`` to allocate memory. The
543  * pool contains n elements of elt_size. Its size is set to n.
544  * Depending on the input parameters, mempool elements can be either allocated
545  * together with the mempool header, or an externally provided memory buffer
546  * could be used to store mempool objects. In later case, that external
547  * memory buffer can consist of set of disjoint phyiscal pages.
548  *
549  * @param name
550  *   The name of the mempool.
551  * @param n
552  *   The number of elements in the mempool. The optimum size (in terms of
553  *   memory usage) for a mempool is when n is a power of two minus one:
554  *   n = (2^q - 1).
555  * @param elt_size
556  *   The size of each element.
557  * @param cache_size
558  *   If cache_size is non-zero, the rte_mempool library will try to
559  *   limit the accesses to the common lockless pool, by maintaining a
560  *   per-lcore object cache. This argument must be lower or equal to
561  *   CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE. It is advised to choose
562  *   cache_size to have "n modulo cache_size == 0": if this is
563  *   not the case, some elements will always stay in the pool and will
564  *   never be used. The access to the per-lcore table is of course
565  *   faster than the multi-producer/consumer pool. The cache can be
566  *   disabled if the cache_size argument is set to 0; it can be useful to
567  *   avoid losing objects in cache. Note that even if not used, the
568  *   memory space for cache is always reserved in a mempool structure,
569  *   except if CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE is set to 0.
570  * @param private_data_size
571  *   The size of the private data appended after the mempool
572  *   structure. This is useful for storing some private data after the
573  *   mempool structure, as is done for rte_mbuf_pool for example.
574  * @param mp_init
575  *   A function pointer that is called for initialization of the pool,
576  *   before object initialization. The user can initialize the private
577  *   data in this function if needed. This parameter can be NULL if
578  *   not needed.
579  * @param mp_init_arg
580  *   An opaque pointer to data that can be used in the mempool
581  *   constructor function.
582  * @param obj_init
583  *   A function pointer that is called for each object at
584  *   initialization of the pool. The user can set some meta data in
585  *   objects if needed. This parameter can be NULL if not needed.
586  *   The obj_init() function takes the mempool pointer, the init_arg,
587  *   the object pointer and the object number as parameters.
588  * @param obj_init_arg
589  *   An opaque pointer to data that can be used as an argument for
590  *   each call to the object constructor function.
591  * @param socket_id
592  *   The *socket_id* argument is the socket identifier in the case of
593  *   NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
594  *   constraint for the reserved zone.
595  * @param flags
596  *   The *flags* arguments is an OR of following flags:
597  *   - MEMPOOL_F_NO_SPREAD: By default, objects addresses are spread
598  *     between channels in RAM: the pool allocator will add padding
599  *     between objects depending on the hardware configuration. See
600  *     Memory alignment constraints for details. If this flag is set,
601  *     the allocator will just align them to a cache line.
602  *   - MEMPOOL_F_NO_CACHE_ALIGN: By default, the returned objects are
603  *     cache-aligned. This flag removes this constraint, and no
604  *     padding will be present between objects. This flag implies
605  *     MEMPOOL_F_NO_SPREAD.
606  *   - MEMPOOL_F_SP_PUT: If this flag is set, the default behavior
607  *     when using rte_mempool_put() or rte_mempool_put_bulk() is
608  *     "single-producer". Otherwise, it is "multi-producers".
609  *   - MEMPOOL_F_SC_GET: If this flag is set, the default behavior
610  *     when using rte_mempool_get() or rte_mempool_get_bulk() is
611  *     "single-consumer". Otherwise, it is "multi-consumers".
612  * @param vaddr
613  *   Virtual address of the externally allocated memory buffer.
614  *   Will be used to store mempool objects.
615  * @param paddr
616  *   Array of phyiscall addresses of the pages that comprises given memory
617  *   buffer.
618  * @param pg_num
619  *   Number of elements in the paddr array.
620  * @param pg_shift
621  *   LOG2 of the physical pages size.
622  * @return
623  *   The pointer to the new allocated mempool, on success. NULL on error
624  *   with rte_errno set appropriately. Possible rte_errno values include:
625  *    - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
626  *    - E_RTE_SECONDARY - function was called from a secondary process instance
627  *    - EINVAL - cache size provided is too large
628  *    - ENOSPC - the maximum number of memzones has already been allocated
629  *    - EEXIST - a memzone with the same name already exists
630  *    - ENOMEM - no appropriate memory area found in which to create memzone
631  */
632 struct rte_mempool *
633 rte_mempool_xmem_create(const char *name, unsigned n, unsigned elt_size,
634                 unsigned cache_size, unsigned private_data_size,
635                 rte_mempool_ctor_t *mp_init, void *mp_init_arg,
636                 rte_mempool_obj_ctor_t *obj_init, void *obj_init_arg,
637                 int socket_id, unsigned flags, void *vaddr,
638                 const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift);
639
640 #ifdef RTE_LIBRTE_XEN_DOM0
641 /**
642  * Creates a new mempool named *name* in memory on Xen Dom0.
643  *
644  * This function uses ``rte_mempool_xmem_create()`` to allocate memory. The
645  * pool contains n elements of elt_size. Its size is set to n.
646  * All elements of the mempool are allocated together with the mempool header,
647  * and memory buffer can consist of set of disjoint phyiscal pages.
648  *
649  * @param name
650  *   The name of the mempool.
651  * @param n
652  *   The number of elements in the mempool. The optimum size (in terms of
653  *   memory usage) for a mempool is when n is a power of two minus one:
654  *   n = (2^q - 1).
655  * @param elt_size
656  *   The size of each element.
657  * @param cache_size
658  *   If cache_size is non-zero, the rte_mempool library will try to
659  *   limit the accesses to the common lockless pool, by maintaining a
660  *   per-lcore object cache. This argument must be lower or equal to
661  *   CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE. It is advised to choose
662  *   cache_size to have "n modulo cache_size == 0": if this is
663  *   not the case, some elements will always stay in the pool and will
664  *   never be used. The access to the per-lcore table is of course
665  *   faster than the multi-producer/consumer pool. The cache can be
666  *   disabled if the cache_size argument is set to 0; it can be useful to
667  *   avoid losing objects in cache. Note that even if not used, the
668  *   memory space for cache is always reserved in a mempool structure,
669  *   except if CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE is set to 0.
670  * @param private_data_size
671  *   The size of the private data appended after the mempool
672  *   structure. This is useful for storing some private data after the
673  *   mempool structure, as is done for rte_mbuf_pool for example.
674  * @param mp_init
675  *   A function pointer that is called for initialization of the pool,
676  *   before object initialization. The user can initialize the private
677  *   data in this function if needed. This parameter can be NULL if
678  *   not needed.
679  * @param mp_init_arg
680  *   An opaque pointer to data that can be used in the mempool
681  *   constructor function.
682  * @param obj_init
683  *   A function pointer that is called for each object at
684  *   initialization of the pool. The user can set some meta data in
685  *   objects if needed. This parameter can be NULL if not needed.
686  *   The obj_init() function takes the mempool pointer, the init_arg,
687  *   the object pointer and the object number as parameters.
688  * @param obj_init_arg
689  *   An opaque pointer to data that can be used as an argument for
690  *   each call to the object constructor function.
691  * @param socket_id
692  *   The *socket_id* argument is the socket identifier in the case of
693  *   NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
694  *   constraint for the reserved zone.
695  * @param flags
696  *   The *flags* arguments is an OR of following flags:
697  *   - MEMPOOL_F_NO_SPREAD: By default, objects addresses are spread
698  *     between channels in RAM: the pool allocator will add padding
699  *     between objects depending on the hardware configuration. See
700  *     Memory alignment constraints for details. If this flag is set,
701  *     the allocator will just align them to a cache line.
702  *   - MEMPOOL_F_NO_CACHE_ALIGN: By default, the returned objects are
703  *     cache-aligned. This flag removes this constraint, and no
704  *     padding will be present between objects. This flag implies
705  *     MEMPOOL_F_NO_SPREAD.
706  *   - MEMPOOL_F_SP_PUT: If this flag is set, the default behavior
707  *     when using rte_mempool_put() or rte_mempool_put_bulk() is
708  *     "single-producer". Otherwise, it is "multi-producers".
709  *   - MEMPOOL_F_SC_GET: If this flag is set, the default behavior
710  *     when using rte_mempool_get() or rte_mempool_get_bulk() is
711  *     "single-consumer". Otherwise, it is "multi-consumers".
712  * @return
713  *   The pointer to the new allocated mempool, on success. NULL on error
714  *   with rte_errno set appropriately. Possible rte_errno values include:
715  *    - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
716  *    - E_RTE_SECONDARY - function was called from a secondary process instance
717  *    - EINVAL - cache size provided is too large
718  *    - ENOSPC - the maximum number of memzones has already been allocated
719  *    - EEXIST - a memzone with the same name already exists
720  *    - ENOMEM - no appropriate memory area found in which to create memzone
721  */
722 struct rte_mempool *
723 rte_dom0_mempool_create(const char *name, unsigned n, unsigned elt_size,
724                 unsigned cache_size, unsigned private_data_size,
725                 rte_mempool_ctor_t *mp_init, void *mp_init_arg,
726                 rte_mempool_obj_ctor_t *obj_init, void *obj_init_arg,
727                 int socket_id, unsigned flags);
728 #endif
729
730 /**
731  * Dump the status of the mempool to the console.
732  *
733  * @param f
734  *   A pointer to a file for output
735  * @param mp
736  *   A pointer to the mempool structure.
737  */
738 void rte_mempool_dump(FILE *f, const struct rte_mempool *mp);
739
740 /**
741  * @internal Put several objects back in the mempool; used internally.
742  * @param mp
743  *   A pointer to the mempool structure.
744  * @param obj_table
745  *   A pointer to a table of void * pointers (objects).
746  * @param n
747  *   The number of objects to store back in the mempool, must be strictly
748  *   positive.
749  * @param is_mp
750  *   Mono-producer (0) or multi-producers (1).
751  */
752 static inline void __attribute__((always_inline))
753 __mempool_put_bulk(struct rte_mempool *mp, void * const *obj_table,
754                     unsigned n, int is_mp)
755 {
756 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
757         struct rte_mempool_cache *cache;
758         uint32_t index;
759         void **cache_objs;
760         unsigned lcore_id = rte_lcore_id();
761         uint32_t cache_size = mp->cache_size;
762         uint32_t flushthresh = mp->cache_flushthresh;
763 #endif /* RTE_MEMPOOL_CACHE_MAX_SIZE > 0 */
764
765         /* increment stat now, adding in mempool always success */
766         __MEMPOOL_STAT_ADD(mp, put, n);
767
768 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
769         /* cache is not enabled or single producer or non-EAL thread */
770         if (unlikely(cache_size == 0 || is_mp == 0 ||
771                      lcore_id >= RTE_MAX_LCORE))
772                 goto ring_enqueue;
773
774         /* Go straight to ring if put would overflow mem allocated for cache */
775         if (unlikely(n > RTE_MEMPOOL_CACHE_MAX_SIZE))
776                 goto ring_enqueue;
777
778         cache = &mp->local_cache[lcore_id];
779         cache_objs = &cache->objs[cache->len];
780
781         /*
782          * The cache follows the following algorithm
783          *   1. Add the objects to the cache
784          *   2. Anything greater than the cache min value (if it crosses the
785          *   cache flush threshold) is flushed to the ring.
786          */
787
788         /* Add elements back into the cache */
789         for (index = 0; index < n; ++index, obj_table++)
790                 cache_objs[index] = *obj_table;
791
792         cache->len += n;
793
794         if (cache->len >= flushthresh) {
795                 rte_ring_mp_enqueue_bulk(mp->ring, &cache->objs[cache_size],
796                                 cache->len - cache_size);
797                 cache->len = cache_size;
798         }
799
800         return;
801
802 ring_enqueue:
803 #endif /* RTE_MEMPOOL_CACHE_MAX_SIZE > 0 */
804
805         /* push remaining objects in ring */
806 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
807         if (is_mp) {
808                 if (rte_ring_mp_enqueue_bulk(mp->ring, obj_table, n) < 0)
809                         rte_panic("cannot put objects in mempool\n");
810         }
811         else {
812                 if (rte_ring_sp_enqueue_bulk(mp->ring, obj_table, n) < 0)
813                         rte_panic("cannot put objects in mempool\n");
814         }
815 #else
816         if (is_mp)
817                 rte_ring_mp_enqueue_bulk(mp->ring, obj_table, n);
818         else
819                 rte_ring_sp_enqueue_bulk(mp->ring, obj_table, n);
820 #endif
821 }
822
823
824 /**
825  * Put several objects back in the mempool (multi-producers safe).
826  *
827  * @param mp
828  *   A pointer to the mempool structure.
829  * @param obj_table
830  *   A pointer to a table of void * pointers (objects).
831  * @param n
832  *   The number of objects to add in the mempool from the obj_table.
833  */
834 static inline void __attribute__((always_inline))
835 rte_mempool_mp_put_bulk(struct rte_mempool *mp, void * const *obj_table,
836                         unsigned n)
837 {
838         __mempool_check_cookies(mp, obj_table, n, 0);
839         __mempool_put_bulk(mp, obj_table, n, 1);
840 }
841
842 /**
843  * Put several objects back in the mempool (NOT multi-producers safe).
844  *
845  * @param mp
846  *   A pointer to the mempool structure.
847  * @param obj_table
848  *   A pointer to a table of void * pointers (objects).
849  * @param n
850  *   The number of objects to add in the mempool from obj_table.
851  */
852 static inline void
853 rte_mempool_sp_put_bulk(struct rte_mempool *mp, void * const *obj_table,
854                         unsigned n)
855 {
856         __mempool_check_cookies(mp, obj_table, n, 0);
857         __mempool_put_bulk(mp, obj_table, n, 0);
858 }
859
860 /**
861  * Put several objects back in the mempool.
862  *
863  * This function calls the multi-producer or the single-producer
864  * version depending on the default behavior that was specified at
865  * mempool creation time (see flags).
866  *
867  * @param mp
868  *   A pointer to the mempool structure.
869  * @param obj_table
870  *   A pointer to a table of void * pointers (objects).
871  * @param n
872  *   The number of objects to add in the mempool from obj_table.
873  */
874 static inline void __attribute__((always_inline))
875 rte_mempool_put_bulk(struct rte_mempool *mp, void * const *obj_table,
876                      unsigned n)
877 {
878         __mempool_check_cookies(mp, obj_table, n, 0);
879         __mempool_put_bulk(mp, obj_table, n, !(mp->flags & MEMPOOL_F_SP_PUT));
880 }
881
882 /**
883  * Put one object in the mempool (multi-producers safe).
884  *
885  * @param mp
886  *   A pointer to the mempool structure.
887  * @param obj
888  *   A pointer to the object to be added.
889  */
890 static inline void __attribute__((always_inline))
891 rte_mempool_mp_put(struct rte_mempool *mp, void *obj)
892 {
893         rte_mempool_mp_put_bulk(mp, &obj, 1);
894 }
895
896 /**
897  * Put one object back in the mempool (NOT multi-producers safe).
898  *
899  * @param mp
900  *   A pointer to the mempool structure.
901  * @param obj
902  *   A pointer to the object to be added.
903  */
904 static inline void __attribute__((always_inline))
905 rte_mempool_sp_put(struct rte_mempool *mp, void *obj)
906 {
907         rte_mempool_sp_put_bulk(mp, &obj, 1);
908 }
909
910 /**
911  * Put one object back in the mempool.
912  *
913  * This function calls the multi-producer or the single-producer
914  * version depending on the default behavior that was specified at
915  * mempool creation time (see flags).
916  *
917  * @param mp
918  *   A pointer to the mempool structure.
919  * @param obj
920  *   A pointer to the object to be added.
921  */
922 static inline void __attribute__((always_inline))
923 rte_mempool_put(struct rte_mempool *mp, void *obj)
924 {
925         rte_mempool_put_bulk(mp, &obj, 1);
926 }
927
928 /**
929  * @internal Get several objects from the mempool; used internally.
930  * @param mp
931  *   A pointer to the mempool structure.
932  * @param obj_table
933  *   A pointer to a table of void * pointers (objects).
934  * @param n
935  *   The number of objects to get, must be strictly positive.
936  * @param is_mc
937  *   Mono-consumer (0) or multi-consumers (1).
938  * @return
939  *   - >=0: Success; number of objects supplied.
940  *   - <0: Error; code of ring dequeue function.
941  */
942 static inline int __attribute__((always_inline))
943 __mempool_get_bulk(struct rte_mempool *mp, void **obj_table,
944                    unsigned n, int is_mc)
945 {
946         int ret;
947 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
948         struct rte_mempool_cache *cache;
949         uint32_t index, len;
950         void **cache_objs;
951         unsigned lcore_id = rte_lcore_id();
952         uint32_t cache_size = mp->cache_size;
953
954         /* cache is not enabled or single consumer */
955         if (unlikely(cache_size == 0 || is_mc == 0 ||
956                      n >= cache_size || lcore_id >= RTE_MAX_LCORE))
957                 goto ring_dequeue;
958
959         cache = &mp->local_cache[lcore_id];
960         cache_objs = cache->objs;
961
962         /* Can this be satisfied from the cache? */
963         if (cache->len < n) {
964                 /* No. Backfill the cache first, and then fill from it */
965                 uint32_t req = n + (cache_size - cache->len);
966
967                 /* How many do we require i.e. number to fill the cache + the request */
968                 ret = rte_ring_mc_dequeue_bulk(mp->ring, &cache->objs[cache->len], req);
969                 if (unlikely(ret < 0)) {
970                         /*
971                          * In the offchance that we are buffer constrained,
972                          * where we are not able to allocate cache + n, go to
973                          * the ring directly. If that fails, we are truly out of
974                          * buffers.
975                          */
976                         goto ring_dequeue;
977                 }
978
979                 cache->len += req;
980         }
981
982         /* Now fill in the response ... */
983         for (index = 0, len = cache->len - 1; index < n; ++index, len--, obj_table++)
984                 *obj_table = cache_objs[len];
985
986         cache->len -= n;
987
988         __MEMPOOL_STAT_ADD(mp, get_success, n);
989
990         return 0;
991
992 ring_dequeue:
993 #endif /* RTE_MEMPOOL_CACHE_MAX_SIZE > 0 */
994
995         /* get remaining objects from ring */
996         if (is_mc)
997                 ret = rte_ring_mc_dequeue_bulk(mp->ring, obj_table, n);
998         else
999                 ret = rte_ring_sc_dequeue_bulk(mp->ring, obj_table, n);
1000
1001         if (ret < 0)
1002                 __MEMPOOL_STAT_ADD(mp, get_fail, n);
1003         else
1004                 __MEMPOOL_STAT_ADD(mp, get_success, n);
1005
1006         return ret;
1007 }
1008
1009 /**
1010  * Get several objects from the mempool (multi-consumers safe).
1011  *
1012  * If cache is enabled, objects will be retrieved first from cache,
1013  * subsequently from the common pool. Note that it can return -ENOENT when
1014  * the local cache and common pool are empty, even if cache from other
1015  * lcores are full.
1016  *
1017  * @param mp
1018  *   A pointer to the mempool structure.
1019  * @param obj_table
1020  *   A pointer to a table of void * pointers (objects) that will be filled.
1021  * @param n
1022  *   The number of objects to get from mempool to obj_table.
1023  * @return
1024  *   - 0: Success; objects taken.
1025  *   - -ENOENT: Not enough entries in the mempool; no object is retrieved.
1026  */
1027 static inline int __attribute__((always_inline))
1028 rte_mempool_mc_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned n)
1029 {
1030         int ret;
1031         ret = __mempool_get_bulk(mp, obj_table, n, 1);
1032         if (ret == 0)
1033                 __mempool_check_cookies(mp, obj_table, n, 1);
1034         return ret;
1035 }
1036
1037 /**
1038  * Get several objects from the mempool (NOT multi-consumers safe).
1039  *
1040  * If cache is enabled, objects will be retrieved first from cache,
1041  * subsequently from the common pool. Note that it can return -ENOENT when
1042  * the local cache and common pool are empty, even if cache from other
1043  * lcores are full.
1044  *
1045  * @param mp
1046  *   A pointer to the mempool structure.
1047  * @param obj_table
1048  *   A pointer to a table of void * pointers (objects) that will be filled.
1049  * @param n
1050  *   The number of objects to get from the mempool to obj_table.
1051  * @return
1052  *   - 0: Success; objects taken.
1053  *   - -ENOENT: Not enough entries in the mempool; no object is
1054  *     retrieved.
1055  */
1056 static inline int __attribute__((always_inline))
1057 rte_mempool_sc_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned n)
1058 {
1059         int ret;
1060         ret = __mempool_get_bulk(mp, obj_table, n, 0);
1061         if (ret == 0)
1062                 __mempool_check_cookies(mp, obj_table, n, 1);
1063         return ret;
1064 }
1065
1066 /**
1067  * Get several objects from the mempool.
1068  *
1069  * This function calls the multi-consumers or the single-consumer
1070  * version, depending on the default behaviour that was specified at
1071  * mempool creation time (see flags).
1072  *
1073  * If cache is enabled, objects will be retrieved first from cache,
1074  * subsequently from the common pool. Note that it can return -ENOENT when
1075  * the local cache and common pool are empty, even if cache from other
1076  * lcores are full.
1077  *
1078  * @param mp
1079  *   A pointer to the mempool structure.
1080  * @param obj_table
1081  *   A pointer to a table of void * pointers (objects) that will be filled.
1082  * @param n
1083  *   The number of objects to get from the mempool to obj_table.
1084  * @return
1085  *   - 0: Success; objects taken
1086  *   - -ENOENT: Not enough entries in the mempool; no object is retrieved.
1087  */
1088 static inline int __attribute__((always_inline))
1089 rte_mempool_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned n)
1090 {
1091         int ret;
1092         ret = __mempool_get_bulk(mp, obj_table, n,
1093                                  !(mp->flags & MEMPOOL_F_SC_GET));
1094         if (ret == 0)
1095                 __mempool_check_cookies(mp, obj_table, n, 1);
1096         return ret;
1097 }
1098
1099 /**
1100  * Get one object from the mempool (multi-consumers safe).
1101  *
1102  * If cache is enabled, objects will be retrieved first from cache,
1103  * subsequently from the common pool. Note that it can return -ENOENT when
1104  * the local cache and common pool are empty, even if cache from other
1105  * lcores are full.
1106  *
1107  * @param mp
1108  *   A pointer to the mempool structure.
1109  * @param obj_p
1110  *   A pointer to a void * pointer (object) that will be filled.
1111  * @return
1112  *   - 0: Success; objects taken.
1113  *   - -ENOENT: Not enough entries in the mempool; no object is retrieved.
1114  */
1115 static inline int __attribute__((always_inline))
1116 rte_mempool_mc_get(struct rte_mempool *mp, void **obj_p)
1117 {
1118         return rte_mempool_mc_get_bulk(mp, obj_p, 1);
1119 }
1120
1121 /**
1122  * Get one object from the mempool (NOT multi-consumers safe).
1123  *
1124  * If cache is enabled, objects will be retrieved first from cache,
1125  * subsequently from the common pool. Note that it can return -ENOENT when
1126  * the local cache and common pool are empty, even if cache from other
1127  * lcores are full.
1128  *
1129  * @param mp
1130  *   A pointer to the mempool structure.
1131  * @param obj_p
1132  *   A pointer to a void * pointer (object) that will be filled.
1133  * @return
1134  *   - 0: Success; objects taken.
1135  *   - -ENOENT: Not enough entries in the mempool; no object is retrieved.
1136  */
1137 static inline int __attribute__((always_inline))
1138 rte_mempool_sc_get(struct rte_mempool *mp, void **obj_p)
1139 {
1140         return rte_mempool_sc_get_bulk(mp, obj_p, 1);
1141 }
1142
1143 /**
1144  * Get one object from the mempool.
1145  *
1146  * This function calls the multi-consumers or the single-consumer
1147  * version, depending on the default behavior that was specified at
1148  * mempool creation (see flags).
1149  *
1150  * If cache is enabled, objects will be retrieved first from cache,
1151  * subsequently from the common pool. Note that it can return -ENOENT when
1152  * the local cache and common pool are empty, even if cache from other
1153  * lcores are full.
1154  *
1155  * @param mp
1156  *   A pointer to the mempool structure.
1157  * @param obj_p
1158  *   A pointer to a void * pointer (object) that will be filled.
1159  * @return
1160  *   - 0: Success; objects taken.
1161  *   - -ENOENT: Not enough entries in the mempool; no object is retrieved.
1162  */
1163 static inline int __attribute__((always_inline))
1164 rte_mempool_get(struct rte_mempool *mp, void **obj_p)
1165 {
1166         return rte_mempool_get_bulk(mp, obj_p, 1);
1167 }
1168
1169 /**
1170  * Return the number of entries in the mempool.
1171  *
1172  * When cache is enabled, this function has to browse the length of
1173  * all lcores, so it should not be used in a data path, but only for
1174  * debug purposes.
1175  *
1176  * @param mp
1177  *   A pointer to the mempool structure.
1178  * @return
1179  *   The number of entries in the mempool.
1180  */
1181 unsigned rte_mempool_count(const struct rte_mempool *mp);
1182
1183 /**
1184  * Return the number of free entries in the mempool ring.
1185  * i.e. how many entries can be freed back to the mempool.
1186  *
1187  * NOTE: This corresponds to the number of elements *allocated* from the
1188  * memory pool, not the number of elements in the pool itself. To count
1189  * the number elements currently available in the pool, use "rte_mempool_count"
1190  *
1191  * When cache is enabled, this function has to browse the length of
1192  * all lcores, so it should not be used in a data path, but only for
1193  * debug purposes.
1194  *
1195  * @param mp
1196  *   A pointer to the mempool structure.
1197  * @return
1198  *   The number of free entries in the mempool.
1199  */
1200 static inline unsigned
1201 rte_mempool_free_count(const struct rte_mempool *mp)
1202 {
1203         return mp->size - rte_mempool_count(mp);
1204 }
1205
1206 /**
1207  * Test if the mempool is full.
1208  *
1209  * When cache is enabled, this function has to browse the length of all
1210  * lcores, so it should not be used in a data path, but only for debug
1211  * purposes.
1212  *
1213  * @param mp
1214  *   A pointer to the mempool structure.
1215  * @return
1216  *   - 1: The mempool is full.
1217  *   - 0: The mempool is not full.
1218  */
1219 static inline int
1220 rte_mempool_full(const struct rte_mempool *mp)
1221 {
1222         return !!(rte_mempool_count(mp) == mp->size);
1223 }
1224
1225 /**
1226  * Test if the mempool is empty.
1227  *
1228  * When cache is enabled, this function has to browse the length of all
1229  * lcores, so it should not be used in a data path, but only for debug
1230  * purposes.
1231  *
1232  * @param mp
1233  *   A pointer to the mempool structure.
1234  * @return
1235  *   - 1: The mempool is empty.
1236  *   - 0: The mempool is not empty.
1237  */
1238 static inline int
1239 rte_mempool_empty(const struct rte_mempool *mp)
1240 {
1241         return !!(rte_mempool_count(mp) == 0);
1242 }
1243
1244 /**
1245  * Return the physical address of elt, which is an element of the pool mp.
1246  *
1247  * @param mp
1248  *   A pointer to the mempool structure.
1249  * @param elt
1250  *   A pointer (virtual address) to the element of the pool.
1251  * @return
1252  *   The physical address of the elt element.
1253  */
1254 static inline phys_addr_t
1255 rte_mempool_virt2phy(const struct rte_mempool *mp, const void *elt)
1256 {
1257         if (rte_eal_has_hugepages()) {
1258                 uintptr_t off;
1259
1260                 off = (const char *)elt - (const char *)mp->elt_va_start;
1261                 return (mp->elt_pa[off >> mp->pg_shift] + (off & mp->pg_mask));
1262         } else {
1263                 /*
1264                  * If huge pages are disabled, we cannot assume the
1265                  * memory region to be physically contiguous.
1266                  * Lookup for each element.
1267                  */
1268                 return rte_mem_virt2phy(elt);
1269         }
1270 }
1271
1272 /**
1273  * Check the consistency of mempool objects.
1274  *
1275  * Verify the coherency of fields in the mempool structure. Also check
1276  * that the cookies of mempool objects (even the ones that are not
1277  * present in pool) have a correct value. If not, a panic will occur.
1278  *
1279  * @param mp
1280  *   A pointer to the mempool structure.
1281  */
1282 void rte_mempool_audit(const struct rte_mempool *mp);
1283
1284 /**
1285  * Return a pointer to the private data in an mempool structure.
1286  *
1287  * @param mp
1288  *   A pointer to the mempool structure.
1289  * @return
1290  *   A pointer to the private data.
1291  */
1292 static inline void *rte_mempool_get_priv(struct rte_mempool *mp)
1293 {
1294         return (char *)mp + MEMPOOL_HEADER_SIZE(mp, mp->pg_num);
1295 }
1296
1297 /**
1298  * Dump the status of all mempools on the console
1299  *
1300  * @param f
1301  *   A pointer to a file for output
1302  */
1303 void rte_mempool_list_dump(FILE *f);
1304
1305 /**
1306  * Search a mempool from its name
1307  *
1308  * @param name
1309  *   The name of the mempool.
1310  * @return
1311  *   The pointer to the mempool matching the name, or NULL if not found.
1312  *   NULL on error
1313  *   with rte_errno set appropriately. Possible rte_errno values include:
1314  *    - ENOENT - required entry not available to return.
1315  *
1316  */
1317 struct rte_mempool *rte_mempool_lookup(const char *name);
1318
1319 /**
1320  * Given a desired size of the mempool element and mempool flags,
1321  * caluclates header, trailer, body and total sizes of the mempool object.
1322  * @param elt_size
1323  *   The size of each element.
1324  * @param flags
1325  *   The flags used for the mempool creation.
1326  *   Consult rte_mempool_create() for more information about possible values.
1327  *   The size of each element.
1328  * @return
1329  *   Total size of the mempool object.
1330  */
1331 uint32_t rte_mempool_calc_obj_size(uint32_t elt_size, uint32_t flags,
1332         struct rte_mempool_objsz *sz);
1333
1334 /**
1335  * Calculate maximum amount of memory required to store given number of objects.
1336  * Assumes that the memory buffer will be aligned at page boundary.
1337  * Note, that if object size is bigger then page size, then it assumes that
1338  * we have a subsets of physically continuous  pages big enough to store
1339  * at least one object.
1340  * @param elt_num
1341  *   Number of elements.
1342  * @param elt_sz
1343  *   The size of each element.
1344  * @param pg_shift
1345  *   LOG2 of the physical pages size.
1346  * @return
1347  *   Required memory size aligned at page boundary.
1348  */
1349 size_t rte_mempool_xmem_size(uint32_t elt_num, size_t elt_sz,
1350         uint32_t pg_shift);
1351
1352 /**
1353  * Calculate how much memory would be actually required with the given
1354  * memory footprint to store required number of objects.
1355  * @param vaddr
1356  *   Virtual address of the externally allocated memory buffer.
1357  *   Will be used to store mempool objects.
1358  * @param elt_num
1359  *   Number of elements.
1360  * @param elt_sz
1361  *   The size of each element.
1362  * @param paddr
1363  *   Array of phyiscall addresses of the pages that comprises given memory
1364  *   buffer.
1365  * @param pg_num
1366  *   Number of elements in the paddr array.
1367  * @param pg_shift
1368  *   LOG2 of the physical pages size.
1369  * @return
1370  *   Number of bytes needed to store given number of objects,
1371  *   aligned to the given page size.
1372  *   If provided memory buffer is not big enough:
1373  *   (-1) * actual number of elemnts that can be stored in that buffer.
1374  */
1375 ssize_t rte_mempool_xmem_usage(void *vaddr, uint32_t elt_num, size_t elt_sz,
1376         const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift);
1377
1378 /**
1379  * Walk list of all memory pools
1380  *
1381  * @param func
1382  *   Iterator function
1383  * @param arg
1384  *   Argument passed to iterator
1385  */
1386 void rte_mempool_walk(void (*func)(const struct rte_mempool *, void *arg),
1387                       void *arg);
1388
1389 #ifdef __cplusplus
1390 }
1391 #endif
1392
1393 #endif /* _RTE_MEMPOOL_H_ */