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