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