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