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