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