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