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