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