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