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