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