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