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