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