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