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