mempool: allow user-owned cache
[dpdk.git] / lib / librte_mempool / rte_mempool.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   Copyright(c) 2016 6WIND S.A.
6  *   All rights reserved.
7  *
8  *   Redistribution and use in source and binary forms, with or without
9  *   modification, are permitted provided that the following conditions
10  *   are met:
11  *
12  *     * Redistributions of source code must retain the above copyright
13  *       notice, this list of conditions and the following disclaimer.
14  *     * Redistributions in binary form must reproduce the above copyright
15  *       notice, this list of conditions and the following disclaimer in
16  *       the documentation and/or other materials provided with the
17  *       distribution.
18  *     * Neither the name of Intel Corporation nor the names of its
19  *       contributors may be used to endorse or promote products derived
20  *       from this software without specific prior written permission.
21  *
22  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34
35 #ifndef _RTE_MEMPOOL_H_
36 #define _RTE_MEMPOOL_H_
37
38 /**
39  * @file
40  * RTE Mempool.
41  *
42  * A memory pool is an allocator of fixed-size object. It is
43  * identified by its name, and uses a ring to store free objects. It
44  * provides some other optional services, like a per-core object
45  * cache, and an alignment helper to ensure that objects are padded
46  * to spread them equally on all RAM channels, ranks, and so on.
47  *
48  * Objects owned by a mempool should never be added in another
49  * mempool. When an object is freed using rte_mempool_put() or
50  * equivalent, the object data is not modified; the user can save some
51  * meta-data in the object data and retrieve them when allocating a
52  * new object.
53  *
54  * Note: the mempool implementation is not preemptable. A lcore must
55  * not be interrupted by another task that uses the same mempool
56  * (because it uses a ring which is not preemptable). Also, mempool
57  * functions must not be used outside the DPDK environment: for
58  * example, in linuxapp environment, a thread that is not created by
59  * the EAL must not use mempools. This is due to the per-lcore cache
60  * that won't work as rte_lcore_id() will not return a correct value.
61  */
62
63 #include <stdio.h>
64 #include <stdlib.h>
65 #include <stdint.h>
66 #include <errno.h>
67 #include <inttypes.h>
68 #include <sys/queue.h>
69
70 #include <rte_spinlock.h>
71 #include <rte_log.h>
72 #include <rte_debug.h>
73 #include <rte_lcore.h>
74 #include <rte_memory.h>
75 #include <rte_branch_prediction.h>
76 #include <rte_ring.h>
77
78 #ifdef __cplusplus
79 extern "C" {
80 #endif
81
82 #define RTE_MEMPOOL_HEADER_COOKIE1  0xbadbadbadadd2e55ULL /**< Header cookie. */
83 #define RTE_MEMPOOL_HEADER_COOKIE2  0xf2eef2eedadd2e55ULL /**< Header cookie. */
84 #define RTE_MEMPOOL_TRAILER_COOKIE  0xadd2e55badbadbadULL /**< Trailer cookie.*/
85
86 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
87 /**
88  * A structure that stores the mempool statistics (per-lcore).
89  */
90 struct rte_mempool_debug_stats {
91         uint64_t put_bulk;         /**< Number of puts. */
92         uint64_t put_objs;         /**< Number of objects successfully put. */
93         uint64_t get_success_bulk; /**< Successful allocation number. */
94         uint64_t get_success_objs; /**< Objects successfully allocated. */
95         uint64_t get_fail_bulk;    /**< Failed allocation number. */
96         uint64_t get_fail_objs;    /**< Objects that failed to be allocated. */
97 } __rte_cache_aligned;
98 #endif
99
100 /**
101  * A structure that stores a per-core object cache.
102  */
103 struct rte_mempool_cache {
104         uint32_t size;        /**< Size of the cache */
105         uint32_t flushthresh; /**< Threshold before we flush excess elements */
106         uint32_t len;         /**< Current cache count */
107         /*
108          * Cache is allocated to this size to allow it to overflow in certain
109          * cases to avoid needless emptying of cache.
110          */
111         void *objs[RTE_MEMPOOL_CACHE_MAX_SIZE * 3]; /**< Cache objects */
112 } __rte_cache_aligned;
113
114 /**
115  * A structure that stores the size of mempool elements.
116  */
117 struct rte_mempool_objsz {
118         uint32_t elt_size;     /**< Size of an element. */
119         uint32_t header_size;  /**< Size of header (before elt). */
120         uint32_t trailer_size; /**< Size of trailer (after elt). */
121         uint32_t total_size;
122         /**< Total size of an object (header + elt + trailer). */
123 };
124
125 #define RTE_MEMPOOL_NAMESIZE 32 /**< Maximum length of a memory pool. */
126 #define RTE_MEMPOOL_MZ_PREFIX "MP_"
127
128 /* "MP_<name>" */
129 #define RTE_MEMPOOL_MZ_FORMAT   RTE_MEMPOOL_MZ_PREFIX "%s"
130
131 #define MEMPOOL_PG_SHIFT_MAX    (sizeof(uintptr_t) * CHAR_BIT - 1)
132
133 /** Mempool over one chunk of physically continuous memory */
134 #define MEMPOOL_PG_NUM_DEFAULT  1
135
136 #ifndef RTE_MEMPOOL_ALIGN
137 #define RTE_MEMPOOL_ALIGN       RTE_CACHE_LINE_SIZE
138 #endif
139
140 #define RTE_MEMPOOL_ALIGN_MASK  (RTE_MEMPOOL_ALIGN - 1)
141
142 /**
143  * Mempool object header structure
144  *
145  * Each object stored in mempools are prefixed by this header structure,
146  * it allows to retrieve the mempool pointer from the object and to
147  * iterate on all objects attached to a mempool. When debug is enabled,
148  * a cookie is also added in this structure preventing corruptions and
149  * double-frees.
150  */
151 struct rte_mempool_objhdr {
152         STAILQ_ENTRY(rte_mempool_objhdr) next; /**< Next in list. */
153         struct rte_mempool *mp;          /**< The mempool owning the object. */
154         phys_addr_t physaddr;            /**< Physical address of the object. */
155 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
156         uint64_t cookie;                 /**< Debug cookie. */
157 #endif
158 };
159
160 /**
161  * A list of object headers type
162  */
163 STAILQ_HEAD(rte_mempool_objhdr_list, rte_mempool_objhdr);
164
165 /**
166  * Mempool object trailer structure
167  *
168  * In debug mode, each object stored in mempools are suffixed by this
169  * trailer structure containing a cookie preventing memory corruptions.
170  */
171 struct rte_mempool_objtlr {
172 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
173         uint64_t cookie;                 /**< Debug cookie. */
174 #endif
175 };
176
177 /**
178  * A list of memory where objects are stored
179  */
180 STAILQ_HEAD(rte_mempool_memhdr_list, rte_mempool_memhdr);
181
182 /**
183  * Callback used to free a memory chunk
184  */
185 typedef void (rte_mempool_memchunk_free_cb_t)(struct rte_mempool_memhdr *memhdr,
186         void *opaque);
187
188 /**
189  * Mempool objects memory header structure
190  *
191  * The memory chunks where objects are stored. Each chunk is virtually
192  * and physically contiguous.
193  */
194 struct rte_mempool_memhdr {
195         STAILQ_ENTRY(rte_mempool_memhdr) next; /**< Next in list. */
196         struct rte_mempool *mp;  /**< The mempool owning the chunk */
197         void *addr;              /**< Virtual address of the chunk */
198         phys_addr_t phys_addr;   /**< Physical address of the chunk */
199         size_t len;              /**< length of the chunk */
200         rte_mempool_memchunk_free_cb_t *free_cb; /**< Free callback */
201         void *opaque;            /**< Argument passed to the free callback */
202 };
203
204 /**
205  * The RTE mempool structure.
206  */
207 struct rte_mempool {
208         char name[RTE_MEMPOOL_NAMESIZE]; /**< Name of mempool. */
209         union {
210                 void *pool_data;         /**< Ring or pool to store objects. */
211                 uint64_t pool_id;        /**< External mempool identifier. */
212         };
213         void *pool_config;               /**< optional args for ops alloc. */
214         const struct rte_memzone *mz;    /**< Memzone where pool is alloc'd. */
215         int flags;                       /**< Flags of the mempool. */
216         int socket_id;                   /**< Socket id passed at create. */
217         uint32_t size;                   /**< Max size of the mempool. */
218         uint32_t cache_size;
219         /**< Size of per-lcore default local cache. */
220
221         uint32_t elt_size;               /**< Size of an element. */
222         uint32_t header_size;            /**< Size of header (before elt). */
223         uint32_t trailer_size;           /**< Size of trailer (after elt). */
224
225         unsigned private_data_size;      /**< Size of private data. */
226         /**
227          * Index into rte_mempool_ops_table array of mempool ops
228          * structs, which contain callback function pointers.
229          * We're using an index here rather than pointers to the callbacks
230          * to facilitate any secondary processes that may want to use
231          * this mempool.
232          */
233         int32_t ops_index;
234
235         struct rte_mempool_cache *local_cache; /**< Per-lcore local cache */
236
237         uint32_t populated_size;         /**< Number of populated objects. */
238         struct rte_mempool_objhdr_list elt_list; /**< List of objects in pool */
239         uint32_t nb_mem_chunks;          /**< Number of memory chunks */
240         struct rte_mempool_memhdr_list mem_list; /**< List of memory chunks */
241
242 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
243         /** Per-lcore statistics. */
244         struct rte_mempool_debug_stats stats[RTE_MAX_LCORE];
245 #endif
246 }  __rte_cache_aligned;
247
248 #define MEMPOOL_F_NO_SPREAD      0x0001 /**< Do not spread among memory channels. */
249 #define MEMPOOL_F_NO_CACHE_ALIGN 0x0002 /**< Do not align objs on cache lines.*/
250 #define MEMPOOL_F_SP_PUT         0x0004 /**< Default put is "single-producer".*/
251 #define MEMPOOL_F_SC_GET         0x0008 /**< Default get is "single-consumer".*/
252 #define MEMPOOL_F_POOL_CREATED   0x0010 /**< Internal: pool is created. */
253 #define MEMPOOL_F_NO_PHYS_CONTIG 0x0020 /**< Don't need physically contiguous objs. */
254
255 /**
256  * @internal When debug is enabled, store some statistics.
257  *
258  * @param mp
259  *   Pointer to the memory pool.
260  * @param name
261  *   Name of the statistics field to increment in the memory pool.
262  * @param n
263  *   Number to add to the object-oriented statistics.
264  */
265 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
266 #define __MEMPOOL_STAT_ADD(mp, name, n) do {                    \
267                 unsigned __lcore_id = rte_lcore_id();           \
268                 if (__lcore_id < RTE_MAX_LCORE) {               \
269                         mp->stats[__lcore_id].name##_objs += n; \
270                         mp->stats[__lcore_id].name##_bulk += 1; \
271                 }                                               \
272         } while(0)
273 #else
274 #define __MEMPOOL_STAT_ADD(mp, name, n) do {} while(0)
275 #endif
276
277 /**
278  * Calculate the size of the mempool header.
279  *
280  * @param mp
281  *   Pointer to the memory pool.
282  * @param cs
283  *   Size of the per-lcore cache.
284  */
285 #define MEMPOOL_HEADER_SIZE(mp, cs) \
286         (sizeof(*(mp)) + (((cs) == 0) ? 0 : \
287         (sizeof(struct rte_mempool_cache) * RTE_MAX_LCORE)))
288
289 /* return the header of a mempool object (internal) */
290 static inline struct rte_mempool_objhdr *__mempool_get_header(void *obj)
291 {
292         return (struct rte_mempool_objhdr *)RTE_PTR_SUB(obj,
293                 sizeof(struct rte_mempool_objhdr));
294 }
295
296 /**
297  * Return a pointer to the mempool owning this object.
298  *
299  * @param obj
300  *   An object that is owned by a pool. If this is not the case,
301  *   the behavior is undefined.
302  * @return
303  *   A pointer to the mempool structure.
304  */
305 static inline struct rte_mempool *rte_mempool_from_obj(void *obj)
306 {
307         struct rte_mempool_objhdr *hdr = __mempool_get_header(obj);
308         return hdr->mp;
309 }
310
311 /* return the trailer of a mempool object (internal) */
312 static inline struct rte_mempool_objtlr *__mempool_get_trailer(void *obj)
313 {
314         struct rte_mempool *mp = rte_mempool_from_obj(obj);
315         return (struct rte_mempool_objtlr *)RTE_PTR_ADD(obj, mp->elt_size);
316 }
317
318 /**
319  * @internal Check and update cookies or panic.
320  *
321  * @param mp
322  *   Pointer to the memory pool.
323  * @param obj_table_const
324  *   Pointer to a table of void * pointers (objects).
325  * @param n
326  *   Index of object in object table.
327  * @param free
328  *   - 0: object is supposed to be allocated, mark it as free
329  *   - 1: object is supposed to be free, mark it as allocated
330  *   - 2: just check that cookie is valid (free or allocated)
331  */
332 void rte_mempool_check_cookies(const struct rte_mempool *mp,
333         void * const *obj_table_const, unsigned n, int free);
334
335 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
336 #define __mempool_check_cookies(mp, obj_table_const, n, free) \
337         rte_mempool_check_cookies(mp, obj_table_const, n, free)
338 #else
339 #define __mempool_check_cookies(mp, obj_table_const, n, free) do {} while(0)
340 #endif /* RTE_LIBRTE_MEMPOOL_DEBUG */
341
342 #define RTE_MEMPOOL_OPS_NAMESIZE 32 /**< Max length of ops struct name. */
343
344 /**
345  * Prototype for implementation specific data provisioning function.
346  *
347  * The function should provide the implementation specific memory for
348  * for use by the other mempool ops functions in a given mempool ops struct.
349  * E.g. the default ops provides an instance of the rte_ring for this purpose.
350  * it will most likely point to a different type of data structure, and
351  * will be transparent to the application programmer.
352  * This function should set mp->pool_data.
353  */
354 typedef int (*rte_mempool_alloc_t)(struct rte_mempool *mp);
355
356 /**
357  * Free the opaque private data pointed to by mp->pool_data pointer.
358  */
359 typedef void (*rte_mempool_free_t)(struct rte_mempool *mp);
360
361 /**
362  * Enqueue an object into the external pool.
363  */
364 typedef int (*rte_mempool_enqueue_t)(struct rte_mempool *mp,
365                 void * const *obj_table, unsigned int n);
366
367 /**
368  * Dequeue an object from the external pool.
369  */
370 typedef int (*rte_mempool_dequeue_t)(struct rte_mempool *mp,
371                 void **obj_table, unsigned int n);
372
373 /**
374  * Return the number of available objects in the external pool.
375  */
376 typedef unsigned (*rte_mempool_get_count)(const struct rte_mempool *mp);
377
378 /** Structure defining mempool operations structure */
379 struct rte_mempool_ops {
380         char name[RTE_MEMPOOL_OPS_NAMESIZE]; /**< Name of mempool ops struct. */
381         rte_mempool_alloc_t alloc;       /**< Allocate private data. */
382         rte_mempool_free_t free;         /**< Free the external pool. */
383         rte_mempool_enqueue_t enqueue;   /**< Enqueue an object. */
384         rte_mempool_dequeue_t dequeue;   /**< Dequeue an object. */
385         rte_mempool_get_count get_count; /**< Get qty of available objs. */
386 } __rte_cache_aligned;
387
388 #define RTE_MEMPOOL_MAX_OPS_IDX 16  /**< Max registered ops structs */
389
390 /**
391  * Structure storing the table of registered ops structs, each of which contain
392  * the function pointers for the mempool ops functions.
393  * Each process has its own storage for this ops struct array so that
394  * the mempools can be shared across primary and secondary processes.
395  * The indices used to access the array are valid across processes, whereas
396  * any function pointers stored directly in the mempool struct would not be.
397  * This results in us simply having "ops_index" in the mempool struct.
398  */
399 struct rte_mempool_ops_table {
400         rte_spinlock_t sl;     /**< Spinlock for add/delete. */
401         uint32_t num_ops;      /**< Number of used ops structs in the table. */
402         /**
403          * Storage for all possible ops structs.
404          */
405         struct rte_mempool_ops ops[RTE_MEMPOOL_MAX_OPS_IDX];
406 } __rte_cache_aligned;
407
408 /** Array of registered ops structs. */
409 extern struct rte_mempool_ops_table rte_mempool_ops_table;
410
411 /**
412  * @internal Get the mempool ops struct from its index.
413  *
414  * @param ops_index
415  *   The index of the ops struct in the ops struct table. It must be a valid
416  *   index: (0 <= idx < num_ops).
417  * @return
418  *   The pointer to the ops struct in the table.
419  */
420 static inline struct rte_mempool_ops *
421 rte_mempool_get_ops(int ops_index)
422 {
423         RTE_VERIFY((ops_index >= 0) && (ops_index < RTE_MEMPOOL_MAX_OPS_IDX));
424
425         return &rte_mempool_ops_table.ops[ops_index];
426 }
427
428 /**
429  * @internal Wrapper for mempool_ops alloc callback.
430  *
431  * @param mp
432  *   Pointer to the memory pool.
433  * @return
434  *   - 0: Success; successfully allocated mempool pool_data.
435  *   - <0: Error; code of alloc function.
436  */
437 int
438 rte_mempool_ops_alloc(struct rte_mempool *mp);
439
440 /**
441  * @internal Wrapper for mempool_ops dequeue callback.
442  *
443  * @param mp
444  *   Pointer to the memory pool.
445  * @param obj_table
446  *   Pointer to a table of void * pointers (objects).
447  * @param n
448  *   Number of objects to get.
449  * @return
450  *   - 0: Success; got n objects.
451  *   - <0: Error; code of dequeue function.
452  */
453 static inline int
454 rte_mempool_ops_dequeue_bulk(struct rte_mempool *mp,
455                 void **obj_table, unsigned n)
456 {
457         struct rte_mempool_ops *ops;
458
459         ops = rte_mempool_get_ops(mp->ops_index);
460         return ops->dequeue(mp, obj_table, n);
461 }
462
463 /**
464  * @internal wrapper for mempool_ops enqueue callback.
465  *
466  * @param mp
467  *   Pointer to the memory pool.
468  * @param obj_table
469  *   Pointer to a table of void * pointers (objects).
470  * @param n
471  *   Number of objects to put.
472  * @return
473  *   - 0: Success; n objects supplied.
474  *   - <0: Error; code of enqueue function.
475  */
476 static inline int
477 rte_mempool_ops_enqueue_bulk(struct rte_mempool *mp, void * const *obj_table,
478                 unsigned n)
479 {
480         struct rte_mempool_ops *ops;
481
482         ops = rte_mempool_get_ops(mp->ops_index);
483         return ops->enqueue(mp, obj_table, n);
484 }
485
486 /**
487  * @internal wrapper for mempool_ops get_count callback.
488  *
489  * @param mp
490  *   Pointer to the memory pool.
491  * @return
492  *   The number of available objects in the external pool.
493  */
494 unsigned
495 rte_mempool_ops_get_count(const struct rte_mempool *mp);
496
497 /**
498  * @internal wrapper for mempool_ops free callback.
499  *
500  * @param mp
501  *   Pointer to the memory pool.
502  */
503 void
504 rte_mempool_ops_free(struct rte_mempool *mp);
505
506 /**
507  * Set the ops of a mempool.
508  *
509  * This can only be done on a mempool that is not populated, i.e. just after
510  * a call to rte_mempool_create_empty().
511  *
512  * @param mp
513  *   Pointer to the memory pool.
514  * @param name
515  *   Name of the ops structure to use for this mempool.
516  * @param pool_config
517  *   Opaque data that can be passed by the application to the ops functions.
518  * @return
519  *   - 0: Success; the mempool is now using the requested ops functions.
520  *   - -EINVAL - Invalid ops struct name provided.
521  *   - -EEXIST - mempool already has an ops struct assigned.
522  */
523 int
524 rte_mempool_set_ops_byname(struct rte_mempool *mp, const char *name,
525                 void *pool_config);
526
527 /**
528  * Register mempool operations.
529  *
530  * @param ops
531  *   Pointer to an ops structure to register.
532  * @return
533  *   - >=0: Success; return the index of the ops struct in the table.
534  *   - -EINVAL - some missing callbacks while registering ops struct.
535  *   - -ENOSPC - the maximum number of ops structs has been reached.
536  */
537 int rte_mempool_register_ops(const struct rte_mempool_ops *ops);
538
539 /**
540  * Macro to statically register the ops of a mempool handler.
541  * Note that the rte_mempool_register_ops fails silently here when
542  * more then RTE_MEMPOOL_MAX_OPS_IDX is registered.
543  */
544 #define MEMPOOL_REGISTER_OPS(ops)                                       \
545         void mp_hdlr_init_##ops(void);                                  \
546         void __attribute__((constructor, used)) mp_hdlr_init_##ops(void)\
547         {                                                               \
548                 rte_mempool_register_ops(&ops);                 \
549         }
550
551 /**
552  * An object callback function for mempool.
553  *
554  * Used by rte_mempool_create() and rte_mempool_obj_iter().
555  */
556 typedef void (rte_mempool_obj_cb_t)(struct rte_mempool *mp,
557                 void *opaque, void *obj, unsigned obj_idx);
558 typedef rte_mempool_obj_cb_t rte_mempool_obj_ctor_t; /* compat */
559
560 /**
561  * A memory callback function for mempool.
562  *
563  * Used by rte_mempool_mem_iter().
564  */
565 typedef void (rte_mempool_mem_cb_t)(struct rte_mempool *mp,
566                 void *opaque, struct rte_mempool_memhdr *memhdr,
567                 unsigned mem_idx);
568
569 /**
570  * A mempool constructor callback function.
571  *
572  * Arguments are the mempool and the opaque pointer given by the user in
573  * rte_mempool_create().
574  */
575 typedef void (rte_mempool_ctor_t)(struct rte_mempool *, void *);
576
577 /**
578  * Create a new mempool named *name* in memory.
579  *
580  * This function uses ``memzone_reserve()`` to allocate memory. The
581  * pool contains n elements of elt_size. Its size is set to n.
582  * All elements of the mempool are allocated together with the mempool header,
583  * in one physically continuous chunk of memory.
584  *
585  * @param name
586  *   The name of the mempool.
587  * @param n
588  *   The number of elements in the mempool. The optimum size (in terms of
589  *   memory usage) for a mempool is when n is a power of two minus one:
590  *   n = (2^q - 1).
591  * @param elt_size
592  *   The size of each element.
593  * @param cache_size
594  *   If cache_size is non-zero, the rte_mempool library will try to
595  *   limit the accesses to the common lockless pool, by maintaining a
596  *   per-lcore object cache. This argument must be lower or equal to
597  *   CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE and n / 1.5. It is advised to choose
598  *   cache_size to have "n modulo cache_size == 0": if this is
599  *   not the case, some elements will always stay in the pool and will
600  *   never be used. The access to the per-lcore table is of course
601  *   faster than the multi-producer/consumer pool. The cache can be
602  *   disabled if the cache_size argument is set to 0; it can be useful to
603  *   avoid losing objects in cache. Note that even if not used, the
604  *   memory space for cache is always reserved in a mempool structure,
605  *   except if CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE is set to 0.
606  * @param private_data_size
607  *   The size of the private data appended after the mempool
608  *   structure. This is useful for storing some private data after the
609  *   mempool structure, as is done for rte_mbuf_pool for example.
610  * @param mp_init
611  *   A function pointer that is called for initialization of the pool,
612  *   before object initialization. The user can initialize the private
613  *   data in this function if needed. This parameter can be NULL if
614  *   not needed.
615  * @param mp_init_arg
616  *   An opaque pointer to data that can be used in the mempool
617  *   constructor function.
618  * @param obj_init
619  *   A function pointer that is called for each object at
620  *   initialization of the pool. The user can set some meta data in
621  *   objects if needed. This parameter can be NULL if not needed.
622  *   The obj_init() function takes the mempool pointer, the init_arg,
623  *   the object pointer and the object number as parameters.
624  * @param obj_init_arg
625  *   An opaque pointer to data that can be used as an argument for
626  *   each call to the object constructor function.
627  * @param socket_id
628  *   The *socket_id* argument is the socket identifier in the case of
629  *   NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
630  *   constraint for the reserved zone.
631  * @param flags
632  *   The *flags* arguments is an OR of following flags:
633  *   - MEMPOOL_F_NO_SPREAD: By default, objects addresses are spread
634  *     between channels in RAM: the pool allocator will add padding
635  *     between objects depending on the hardware configuration. See
636  *     Memory alignment constraints for details. If this flag is set,
637  *     the allocator will just align them to a cache line.
638  *   - MEMPOOL_F_NO_CACHE_ALIGN: By default, the returned objects are
639  *     cache-aligned. This flag removes this constraint, and no
640  *     padding will be present between objects. This flag implies
641  *     MEMPOOL_F_NO_SPREAD.
642  *   - MEMPOOL_F_SP_PUT: If this flag is set, the default behavior
643  *     when using rte_mempool_put() or rte_mempool_put_bulk() is
644  *     "single-producer". Otherwise, it is "multi-producers".
645  *   - MEMPOOL_F_SC_GET: If this flag is set, the default behavior
646  *     when using rte_mempool_get() or rte_mempool_get_bulk() is
647  *     "single-consumer". Otherwise, it is "multi-consumers".
648  *   - MEMPOOL_F_NO_PHYS_CONTIG: If set, allocated objects won't
649  *     necessarilly be contiguous in physical memory.
650  * @return
651  *   The pointer to the new allocated mempool, on success. NULL on error
652  *   with rte_errno set appropriately. Possible rte_errno values include:
653  *    - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
654  *    - E_RTE_SECONDARY - function was called from a secondary process instance
655  *    - EINVAL - cache size provided is too large
656  *    - ENOSPC - the maximum number of memzones has already been allocated
657  *    - EEXIST - a memzone with the same name already exists
658  *    - ENOMEM - no appropriate memory area found in which to create memzone
659  */
660 struct rte_mempool *
661 rte_mempool_create(const char *name, unsigned n, unsigned elt_size,
662                    unsigned cache_size, unsigned private_data_size,
663                    rte_mempool_ctor_t *mp_init, void *mp_init_arg,
664                    rte_mempool_obj_cb_t *obj_init, void *obj_init_arg,
665                    int socket_id, unsigned flags);
666
667 /**
668  * Create a new mempool named *name* in memory.
669  *
670  * The pool contains n elements of elt_size. Its size is set to n.
671  * This function uses ``memzone_reserve()`` to allocate the mempool header
672  * (and the objects if vaddr is NULL).
673  * Depending on the input parameters, mempool elements can be either allocated
674  * together with the mempool header, or an externally provided memory buffer
675  * could be used to store mempool objects. In later case, that external
676  * memory buffer can consist of set of disjoint physical pages.
677  *
678  * @param name
679  *   The name of the mempool.
680  * @param n
681  *   The number of elements in the mempool. The optimum size (in terms of
682  *   memory usage) for a mempool is when n is a power of two minus one:
683  *   n = (2^q - 1).
684  * @param elt_size
685  *   The size of each element.
686  * @param cache_size
687  *   Size of the cache. See rte_mempool_create() for details.
688  * @param private_data_size
689  *   The size of the private data appended after the mempool
690  *   structure. This is useful for storing some private data after the
691  *   mempool structure, as is done for rte_mbuf_pool for example.
692  * @param mp_init
693  *   A function pointer that is called for initialization of the pool,
694  *   before object initialization. The user can initialize the private
695  *   data in this function if needed. This parameter can be NULL if
696  *   not needed.
697  * @param mp_init_arg
698  *   An opaque pointer to data that can be used in the mempool
699  *   constructor function.
700  * @param obj_init
701  *   A function called for each object at initialization of the pool.
702  *   See rte_mempool_create() for details.
703  * @param obj_init_arg
704  *   An opaque pointer passed to the object constructor function.
705  * @param socket_id
706  *   The *socket_id* argument is the socket identifier in the case of
707  *   NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
708  *   constraint for the reserved zone.
709  * @param flags
710  *   Flags controlling the behavior of the mempool. See
711  *   rte_mempool_create() for details.
712  * @param vaddr
713  *   Virtual address of the externally allocated memory buffer.
714  *   Will be used to store mempool objects.
715  * @param paddr
716  *   Array of physical addresses of the pages that comprises given memory
717  *   buffer.
718  * @param pg_num
719  *   Number of elements in the paddr array.
720  * @param pg_shift
721  *   LOG2 of the physical pages size.
722  * @return
723  *   The pointer to the new allocated mempool, on success. NULL on error
724  *   with rte_errno set appropriately. See rte_mempool_create() for details.
725  */
726 struct rte_mempool *
727 rte_mempool_xmem_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_cb_t *obj_init, void *obj_init_arg,
731                 int socket_id, unsigned flags, void *vaddr,
732                 const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift);
733
734 /**
735  * Create an empty mempool
736  *
737  * The mempool is allocated and initialized, but it is not populated: no
738  * memory is allocated for the mempool elements. The user has to call
739  * rte_mempool_populate_*() or to add memory chunks to the pool. Once
740  * populated, the user may also want to initialize each object with
741  * rte_mempool_obj_iter().
742  *
743  * @param name
744  *   The name of the mempool.
745  * @param n
746  *   The maximum number of elements that can be added in the mempool.
747  *   The optimum size (in terms of memory usage) for a mempool is when n
748  *   is a power of two minus one: n = (2^q - 1).
749  * @param elt_size
750  *   The size of each element.
751  * @param cache_size
752  *   Size of the cache. See rte_mempool_create() for details.
753  * @param private_data_size
754  *   The size of the private data appended after the mempool
755  *   structure. This is useful for storing some private data after the
756  *   mempool structure, as is done for rte_mbuf_pool for example.
757  * @param socket_id
758  *   The *socket_id* argument is the socket identifier in the case of
759  *   NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
760  *   constraint for the reserved zone.
761  * @param flags
762  *   Flags controlling the behavior of the mempool. See
763  *   rte_mempool_create() for details.
764  * @return
765  *   The pointer to the new allocated mempool, on success. NULL on error
766  *   with rte_errno set appropriately. See rte_mempool_create() for details.
767  */
768 struct rte_mempool *
769 rte_mempool_create_empty(const char *name, unsigned n, unsigned elt_size,
770         unsigned cache_size, unsigned private_data_size,
771         int socket_id, unsigned flags);
772 /**
773  * Free a mempool
774  *
775  * Unlink the mempool from global list, free the memory chunks, and all
776  * memory referenced by the mempool. The objects must not be used by
777  * other cores as they will be freed.
778  *
779  * @param mp
780  *   A pointer to the mempool structure.
781  */
782 void
783 rte_mempool_free(struct rte_mempool *mp);
784
785 /**
786  * Add physically contiguous memory for objects in the pool at init
787  *
788  * Add a virtually and physically contiguous memory chunk in the pool
789  * where objects can be instanciated.
790  *
791  * @param mp
792  *   A pointer to the mempool structure.
793  * @param vaddr
794  *   The virtual address of memory that should be used to store objects.
795  * @param paddr
796  *   The physical address
797  * @param len
798  *   The length of memory in bytes.
799  * @param free_cb
800  *   The callback used to free this chunk when destroying the mempool.
801  * @param opaque
802  *   An opaque argument passed to free_cb.
803  * @return
804  *   The number of objects added on success.
805  *   On error, the chunk is not added in the memory list of the
806  *   mempool and a negative errno is returned.
807  */
808 int rte_mempool_populate_phys(struct rte_mempool *mp, char *vaddr,
809         phys_addr_t paddr, size_t len, rte_mempool_memchunk_free_cb_t *free_cb,
810         void *opaque);
811
812 /**
813  * Add physical memory for objects in the pool at init
814  *
815  * Add a virtually contiguous memory chunk in the pool where objects can
816  * be instanciated. The physical addresses corresponding to the virtual
817  * area are described in paddr[], pg_num, pg_shift.
818  *
819  * @param mp
820  *   A pointer to the mempool structure.
821  * @param vaddr
822  *   The virtual address of memory that should be used to store objects.
823  * @param paddr
824  *   An array of physical addresses of each page composing the virtual
825  *   area.
826  * @param pg_num
827  *   Number of elements in the paddr array.
828  * @param pg_shift
829  *   LOG2 of the physical pages size.
830  * @param free_cb
831  *   The callback used to free this chunk when destroying the mempool.
832  * @param opaque
833  *   An opaque argument passed to free_cb.
834  * @return
835  *   The number of objects added on success.
836  *   On error, the chunks are not added in the memory list of the
837  *   mempool and a negative errno is returned.
838  */
839 int rte_mempool_populate_phys_tab(struct rte_mempool *mp, char *vaddr,
840         const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift,
841         rte_mempool_memchunk_free_cb_t *free_cb, void *opaque);
842
843 /**
844  * Add virtually contiguous memory for objects in the pool at init
845  *
846  * Add a virtually contiguous memory chunk in the pool where objects can
847  * be instanciated.
848  *
849  * @param mp
850  *   A pointer to the mempool structure.
851  * @param addr
852  *   The virtual address of memory that should be used to store objects.
853  *   Must be page-aligned.
854  * @param len
855  *   The length of memory in bytes. Must be page-aligned.
856  * @param pg_sz
857  *   The size of memory pages in this virtual area.
858  * @param free_cb
859  *   The callback used to free this chunk when destroying the mempool.
860  * @param opaque
861  *   An opaque argument passed to free_cb.
862  * @return
863  *   The number of objects added on success.
864  *   On error, the chunk is not added in the memory list of the
865  *   mempool and a negative errno is returned.
866  */
867 int
868 rte_mempool_populate_virt(struct rte_mempool *mp, char *addr,
869         size_t len, size_t pg_sz, rte_mempool_memchunk_free_cb_t *free_cb,
870         void *opaque);
871
872 /**
873  * Add memory for objects in the pool at init
874  *
875  * This is the default function used by rte_mempool_create() to populate
876  * the mempool. It adds memory allocated using rte_memzone_reserve().
877  *
878  * @param mp
879  *   A pointer to the mempool structure.
880  * @return
881  *   The number of objects added on success.
882  *   On error, the chunk is not added in the memory list of the
883  *   mempool and a negative errno is returned.
884  */
885 int rte_mempool_populate_default(struct rte_mempool *mp);
886
887 /**
888  * Add memory from anonymous mapping for objects in the pool at init
889  *
890  * This function mmap an anonymous memory zone that is locked in
891  * memory to store the objects of the mempool.
892  *
893  * @param mp
894  *   A pointer to the mempool structure.
895  * @return
896  *   The number of objects added on success.
897  *   On error, the chunk is not added in the memory list of the
898  *   mempool and a negative errno is returned.
899  */
900 int rte_mempool_populate_anon(struct rte_mempool *mp);
901
902 /**
903  * Call a function for each mempool element
904  *
905  * Iterate across all objects attached to a rte_mempool and call the
906  * callback function on it.
907  *
908  * @param mp
909  *   A pointer to an initialized mempool.
910  * @param obj_cb
911  *   A function pointer that is called for each object.
912  * @param obj_cb_arg
913  *   An opaque pointer passed to the callback function.
914  * @return
915  *   Number of objects iterated.
916  */
917 uint32_t rte_mempool_obj_iter(struct rte_mempool *mp,
918         rte_mempool_obj_cb_t *obj_cb, void *obj_cb_arg);
919
920 /**
921  * Call a function for each mempool memory chunk
922  *
923  * Iterate across all memory chunks attached to a rte_mempool and call
924  * the callback function on it.
925  *
926  * @param mp
927  *   A pointer to an initialized mempool.
928  * @param mem_cb
929  *   A function pointer that is called for each memory chunk.
930  * @param mem_cb_arg
931  *   An opaque pointer passed to the callback function.
932  * @return
933  *   Number of memory chunks iterated.
934  */
935 uint32_t rte_mempool_mem_iter(struct rte_mempool *mp,
936         rte_mempool_mem_cb_t *mem_cb, void *mem_cb_arg);
937
938 /**
939  * Dump the status of the mempool to the console.
940  *
941  * @param f
942  *   A pointer to a file for output
943  * @param mp
944  *   A pointer to the mempool structure.
945  */
946 void rte_mempool_dump(FILE *f, struct rte_mempool *mp);
947
948 /**
949  * Create a user-owned mempool cache.
950  *
951  * This can be used by non-EAL threads to enable caching when they
952  * interact with a mempool.
953  *
954  * @param size
955  *   The size of the mempool cache. See rte_mempool_create()'s cache_size
956  *   parameter description for more information. The same limits and
957  *   considerations apply here too.
958  * @param socket_id
959  *   The socket identifier in the case of NUMA. The value can be
960  *   SOCKET_ID_ANY if there is no NUMA constraint for the reserved zone.
961  */
962 struct rte_mempool_cache *
963 rte_mempool_cache_create(uint32_t size, int socket_id);
964
965 /**
966  * Free a user-owned mempool cache.
967  *
968  * @param cache
969  *   A pointer to the mempool cache.
970  */
971 void
972 rte_mempool_cache_free(struct rte_mempool_cache *cache);
973
974 /**
975  * Flush a user-owned mempool cache to the specified mempool.
976  *
977  * @param cache
978  *   A pointer to the mempool cache.
979  * @param mp
980  *   A pointer to the mempool.
981  */
982 static inline void __attribute__((always_inline))
983 rte_mempool_cache_flush(struct rte_mempool_cache *cache,
984                         struct rte_mempool *mp)
985 {
986         rte_mempool_ops_enqueue_bulk(mp, cache->objs, cache->len);
987         cache->len = 0;
988 }
989
990 /**
991  * Get a pointer to the per-lcore default mempool cache.
992  *
993  * @param mp
994  *   A pointer to the mempool structure.
995  * @param lcore_id
996  *   The logical core id.
997  * @return
998  *   A pointer to the mempool cache or NULL if disabled or non-EAL thread.
999  */
1000 static inline struct rte_mempool_cache *__attribute__((always_inline))
1001 rte_mempool_default_cache(struct rte_mempool *mp, unsigned lcore_id)
1002 {
1003         if (mp->cache_size == 0)
1004                 return NULL;
1005
1006         if (lcore_id >= RTE_MAX_LCORE)
1007                 return NULL;
1008
1009         return &mp->local_cache[lcore_id];
1010 }
1011
1012 /**
1013  * @internal Put several objects back in the mempool; used internally.
1014  * @param mp
1015  *   A pointer to the mempool structure.
1016  * @param obj_table
1017  *   A pointer to a table of void * pointers (objects).
1018  * @param n
1019  *   The number of objects to store back in the mempool, must be strictly
1020  *   positive.
1021  * @param cache
1022  *   A pointer to a mempool cache structure. May be NULL if not needed.
1023  * @param flags
1024  *   The flags used for the mempool creation.
1025  *   Single-producer (MEMPOOL_F_SP_PUT flag) or multi-producers.
1026  */
1027 static inline void __attribute__((always_inline))
1028 __mempool_generic_put(struct rte_mempool *mp, void * const *obj_table,
1029                       unsigned n, struct rte_mempool_cache *cache, int flags)
1030 {
1031         uint32_t index;
1032         void **cache_objs;
1033
1034         /* increment stat now, adding in mempool always success */
1035         __MEMPOOL_STAT_ADD(mp, put, n);
1036
1037         /* No cache provided or single producer */
1038         if (unlikely(cache == NULL || flags & MEMPOOL_F_SP_PUT))
1039                 goto ring_enqueue;
1040
1041         /* Go straight to ring if put would overflow mem allocated for cache */
1042         if (unlikely(n > RTE_MEMPOOL_CACHE_MAX_SIZE))
1043                 goto ring_enqueue;
1044
1045         cache_objs = &cache->objs[cache->len];
1046
1047         /*
1048          * The cache follows the following algorithm
1049          *   1. Add the objects to the cache
1050          *   2. Anything greater than the cache min value (if it crosses the
1051          *   cache flush threshold) is flushed to the ring.
1052          */
1053
1054         /* Add elements back into the cache */
1055         for (index = 0; index < n; ++index, obj_table++)
1056                 cache_objs[index] = *obj_table;
1057
1058         cache->len += n;
1059
1060         if (cache->len >= cache->flushthresh) {
1061                 rte_mempool_ops_enqueue_bulk(mp, &cache->objs[cache->size],
1062                                 cache->len - cache->size);
1063                 cache->len = cache->size;
1064         }
1065
1066         return;
1067
1068 ring_enqueue:
1069
1070         /* push remaining objects in ring */
1071 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
1072         if (rte_mempool_ops_enqueue_bulk(mp, obj_table, n) < 0)
1073                 rte_panic("cannot put objects in mempool\n");
1074 #else
1075         rte_mempool_ops_enqueue_bulk(mp, obj_table, n);
1076 #endif
1077 }
1078
1079
1080 /**
1081  * Put several objects back in the mempool.
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).
1087  * @param n
1088  *   The number of objects to add in the mempool from the obj_table.
1089  * @param cache
1090  *   A pointer to a mempool cache structure. May be NULL if not needed.
1091  * @param flags
1092  *   The flags used for the mempool creation.
1093  *   Single-producer (MEMPOOL_F_SP_PUT flag) or multi-producers.
1094  */
1095 static inline void __attribute__((always_inline))
1096 rte_mempool_generic_put(struct rte_mempool *mp, void * const *obj_table,
1097                         unsigned n, struct rte_mempool_cache *cache, int flags)
1098 {
1099         __mempool_check_cookies(mp, obj_table, n, 0);
1100         __mempool_generic_put(mp, obj_table, n, cache, flags);
1101 }
1102
1103 /**
1104  * @deprecated
1105  * Put several objects back in the mempool (multi-producers safe).
1106  *
1107  * @param mp
1108  *   A pointer to the mempool structure.
1109  * @param obj_table
1110  *   A pointer to a table of void * pointers (objects).
1111  * @param n
1112  *   The number of objects to add in the mempool from the obj_table.
1113  */
1114 __rte_deprecated
1115 static inline void __attribute__((always_inline))
1116 rte_mempool_mp_put_bulk(struct rte_mempool *mp, void * const *obj_table,
1117                         unsigned n)
1118 {
1119         struct rte_mempool_cache *cache;
1120         cache = rte_mempool_default_cache(mp, rte_lcore_id());
1121         rte_mempool_generic_put(mp, obj_table, n, cache, 0);
1122 }
1123
1124 /**
1125  * @deprecated
1126  * Put several objects back in the mempool (NOT multi-producers safe).
1127  *
1128  * @param mp
1129  *   A pointer to the mempool structure.
1130  * @param obj_table
1131  *   A pointer to a table of void * pointers (objects).
1132  * @param n
1133  *   The number of objects to add in the mempool from obj_table.
1134  */
1135 __rte_deprecated
1136 static inline void __attribute__((always_inline))
1137 rte_mempool_sp_put_bulk(struct rte_mempool *mp, void * const *obj_table,
1138                         unsigned n)
1139 {
1140         rte_mempool_generic_put(mp, obj_table, n, NULL, MEMPOOL_F_SP_PUT);
1141 }
1142
1143 /**
1144  * Put several objects back in the mempool.
1145  *
1146  * This function calls the multi-producer or the single-producer
1147  * version depending on the default behavior that was specified at
1148  * mempool creation time (see flags).
1149  *
1150  * @param mp
1151  *   A pointer to the mempool structure.
1152  * @param obj_table
1153  *   A pointer to a table of void * pointers (objects).
1154  * @param n
1155  *   The number of objects to add in the mempool from obj_table.
1156  */
1157 static inline void __attribute__((always_inline))
1158 rte_mempool_put_bulk(struct rte_mempool *mp, void * const *obj_table,
1159                      unsigned n)
1160 {
1161         struct rte_mempool_cache *cache;
1162         cache = rte_mempool_default_cache(mp, rte_lcore_id());
1163         rte_mempool_generic_put(mp, obj_table, n, cache, mp->flags);
1164 }
1165
1166 /**
1167  * @deprecated
1168  * Put one object in the mempool (multi-producers safe).
1169  *
1170  * @param mp
1171  *   A pointer to the mempool structure.
1172  * @param obj
1173  *   A pointer to the object to be added.
1174  */
1175 __rte_deprecated
1176 static inline void __attribute__((always_inline))
1177 rte_mempool_mp_put(struct rte_mempool *mp, void *obj)
1178 {
1179         struct rte_mempool_cache *cache;
1180         cache = rte_mempool_default_cache(mp, rte_lcore_id());
1181         rte_mempool_generic_put(mp, &obj, 1, cache, 0);
1182 }
1183
1184 /**
1185  * @deprecated
1186  * Put one object back in the mempool (NOT multi-producers safe).
1187  *
1188  * @param mp
1189  *   A pointer to the mempool structure.
1190  * @param obj
1191  *   A pointer to the object to be added.
1192  */
1193 __rte_deprecated
1194 static inline void __attribute__((always_inline))
1195 rte_mempool_sp_put(struct rte_mempool *mp, void *obj)
1196 {
1197         rte_mempool_generic_put(mp, &obj, 1, NULL, MEMPOOL_F_SP_PUT);
1198 }
1199
1200 /**
1201  * Put one object back in the mempool.
1202  *
1203  * This function calls the multi-producer or the single-producer
1204  * version depending on the default behavior that was specified at
1205  * mempool creation time (see flags).
1206  *
1207  * @param mp
1208  *   A pointer to the mempool structure.
1209  * @param obj
1210  *   A pointer to the object to be added.
1211  */
1212 static inline void __attribute__((always_inline))
1213 rte_mempool_put(struct rte_mempool *mp, void *obj)
1214 {
1215         rte_mempool_put_bulk(mp, &obj, 1);
1216 }
1217
1218 /**
1219  * @internal Get several objects from the mempool; used internally.
1220  * @param mp
1221  *   A pointer to the mempool structure.
1222  * @param obj_table
1223  *   A pointer to a table of void * pointers (objects).
1224  * @param n
1225  *   The number of objects to get, must be strictly positive.
1226  * @param cache
1227  *   A pointer to a mempool cache structure. May be NULL if not needed.
1228  * @param flags
1229  *   The flags used for the mempool creation.
1230  *   Single-consumer (MEMPOOL_F_SC_GET flag) or multi-consumers.
1231  * @return
1232  *   - >=0: Success; number of objects supplied.
1233  *   - <0: Error; code of ring dequeue function.
1234  */
1235 static inline int __attribute__((always_inline))
1236 __mempool_generic_get(struct rte_mempool *mp, void **obj_table,
1237                       unsigned n, struct rte_mempool_cache *cache, int flags)
1238 {
1239         int ret;
1240         uint32_t index, len;
1241         void **cache_objs;
1242
1243         /* No cache provided or single consumer */
1244         if (unlikely(cache == NULL || flags & MEMPOOL_F_SC_GET ||
1245                      n >= cache->size))
1246                 goto ring_dequeue;
1247
1248         cache_objs = cache->objs;
1249
1250         /* Can this be satisfied from the cache? */
1251         if (cache->len < n) {
1252                 /* No. Backfill the cache first, and then fill from it */
1253                 uint32_t req = n + (cache->size - cache->len);
1254
1255                 /* How many do we require i.e. number to fill the cache + the request */
1256                 ret = rte_mempool_ops_dequeue_bulk(mp,
1257                         &cache->objs[cache->len], req);
1258                 if (unlikely(ret < 0)) {
1259                         /*
1260                          * In the offchance that we are buffer constrained,
1261                          * where we are not able to allocate cache + n, go to
1262                          * the ring directly. If that fails, we are truly out of
1263                          * buffers.
1264                          */
1265                         goto ring_dequeue;
1266                 }
1267
1268                 cache->len += req;
1269         }
1270
1271         /* Now fill in the response ... */
1272         for (index = 0, len = cache->len - 1; index < n; ++index, len--, obj_table++)
1273                 *obj_table = cache_objs[len];
1274
1275         cache->len -= n;
1276
1277         __MEMPOOL_STAT_ADD(mp, get_success, n);
1278
1279         return 0;
1280
1281 ring_dequeue:
1282
1283         /* get remaining objects from ring */
1284         ret = rte_mempool_ops_dequeue_bulk(mp, obj_table, n);
1285
1286         if (ret < 0)
1287                 __MEMPOOL_STAT_ADD(mp, get_fail, n);
1288         else
1289                 __MEMPOOL_STAT_ADD(mp, get_success, n);
1290
1291         return ret;
1292 }
1293
1294 /**
1295  * Get several objects from the mempool.
1296  *
1297  * If cache is enabled, objects will be retrieved first from cache,
1298  * subsequently from the common pool. Note that it can return -ENOENT when
1299  * the local cache and common pool are empty, even if cache from other
1300  * lcores are full.
1301  *
1302  * @param mp
1303  *   A pointer to the mempool structure.
1304  * @param obj_table
1305  *   A pointer to a table of void * pointers (objects) that will be filled.
1306  * @param n
1307  *   The number of objects to get from mempool to obj_table.
1308  * @param cache
1309  *   A pointer to a mempool cache structure. May be NULL if not needed.
1310  * @param flags
1311  *   The flags used for the mempool creation.
1312  *   Single-consumer (MEMPOOL_F_SC_GET flag) or multi-consumers.
1313  * @return
1314  *   - 0: Success; objects taken.
1315  *   - -ENOENT: Not enough entries in the mempool; no object is retrieved.
1316  */
1317 static inline int __attribute__((always_inline))
1318 rte_mempool_generic_get(struct rte_mempool *mp, void **obj_table, unsigned n,
1319                         struct rte_mempool_cache *cache, int flags)
1320 {
1321         int ret;
1322         ret = __mempool_generic_get(mp, obj_table, n, cache, flags);
1323         if (ret == 0)
1324                 __mempool_check_cookies(mp, obj_table, n, 1);
1325         return ret;
1326 }
1327
1328 /**
1329  * @deprecated
1330  * Get several objects from the mempool (multi-consumers safe).
1331  *
1332  * If cache is enabled, objects will be retrieved first from cache,
1333  * subsequently from the common pool. Note that it can return -ENOENT when
1334  * the local cache and common pool are empty, even if cache from other
1335  * lcores are full.
1336  *
1337  * @param mp
1338  *   A pointer to the mempool structure.
1339  * @param obj_table
1340  *   A pointer to a table of void * pointers (objects) that will be filled.
1341  * @param n
1342  *   The number of objects to get from mempool to obj_table.
1343  * @return
1344  *   - 0: Success; objects taken.
1345  *   - -ENOENT: Not enough entries in the mempool; no object is retrieved.
1346  */
1347 __rte_deprecated
1348 static inline int __attribute__((always_inline))
1349 rte_mempool_mc_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned n)
1350 {
1351         struct rte_mempool_cache *cache;
1352         cache = rte_mempool_default_cache(mp, rte_lcore_id());
1353         return rte_mempool_generic_get(mp, obj_table, n, cache, 0);
1354 }
1355
1356 /**
1357  * @deprecated
1358  * Get several objects from the mempool (NOT multi-consumers safe).
1359  *
1360  * If cache is enabled, objects will be retrieved first from cache,
1361  * subsequently from the common pool. Note that it can return -ENOENT when
1362  * the local cache and common pool are empty, even if cache from other
1363  * lcores are full.
1364  *
1365  * @param mp
1366  *   A pointer to the mempool structure.
1367  * @param obj_table
1368  *   A pointer to a table of void * pointers (objects) that will be filled.
1369  * @param n
1370  *   The number of objects to get from the mempool to obj_table.
1371  * @return
1372  *   - 0: Success; objects taken.
1373  *   - -ENOENT: Not enough entries in the mempool; no object is
1374  *     retrieved.
1375  */
1376 __rte_deprecated
1377 static inline int __attribute__((always_inline))
1378 rte_mempool_sc_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned n)
1379 {
1380         return rte_mempool_generic_get(mp, obj_table, n, NULL,
1381                                        MEMPOOL_F_SC_GET);
1382 }
1383
1384 /**
1385  * Get several objects from the mempool.
1386  *
1387  * This function calls the multi-consumers or the single-consumer
1388  * version, depending on the default behaviour that was specified at
1389  * mempool creation time (see flags).
1390  *
1391  * If cache is enabled, objects will be retrieved first from cache,
1392  * subsequently from the common pool. Note that it can return -ENOENT when
1393  * the local cache and common pool are empty, even if cache from other
1394  * lcores are full.
1395  *
1396  * @param mp
1397  *   A pointer to the mempool structure.
1398  * @param obj_table
1399  *   A pointer to a table of void * pointers (objects) that will be filled.
1400  * @param n
1401  *   The number of objects to get from the mempool to obj_table.
1402  * @return
1403  *   - 0: Success; objects taken
1404  *   - -ENOENT: Not enough entries in the mempool; no object is retrieved.
1405  */
1406 static inline int __attribute__((always_inline))
1407 rte_mempool_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned n)
1408 {
1409         struct rte_mempool_cache *cache;
1410         cache = rte_mempool_default_cache(mp, rte_lcore_id());
1411         return rte_mempool_generic_get(mp, obj_table, n, cache, mp->flags);
1412 }
1413
1414 /**
1415  * @deprecated
1416  * Get one object from the mempool (multi-consumers safe).
1417  *
1418  * If cache is enabled, objects will be retrieved first from cache,
1419  * subsequently from the common pool. Note that it can return -ENOENT when
1420  * the local cache and common pool are empty, even if cache from other
1421  * lcores are full.
1422  *
1423  * @param mp
1424  *   A pointer to the mempool structure.
1425  * @param obj_p
1426  *   A pointer to a void * pointer (object) that will be filled.
1427  * @return
1428  *   - 0: Success; objects taken.
1429  *   - -ENOENT: Not enough entries in the mempool; no object is retrieved.
1430  */
1431 __rte_deprecated
1432 static inline int __attribute__((always_inline))
1433 rte_mempool_mc_get(struct rte_mempool *mp, void **obj_p)
1434 {
1435         struct rte_mempool_cache *cache;
1436         cache = rte_mempool_default_cache(mp, rte_lcore_id());
1437         return rte_mempool_generic_get(mp, obj_p, 1, cache, 0);
1438 }
1439
1440 /**
1441  * @deprecated
1442  * Get one object from the mempool (NOT multi-consumers safe).
1443  *
1444  * If cache is enabled, objects will be retrieved first from cache,
1445  * subsequently from the common pool. Note that it can return -ENOENT when
1446  * the local cache and common pool are empty, even if cache from other
1447  * lcores are full.
1448  *
1449  * @param mp
1450  *   A pointer to the mempool structure.
1451  * @param obj_p
1452  *   A pointer to a void * pointer (object) that will be filled.
1453  * @return
1454  *   - 0: Success; objects taken.
1455  *   - -ENOENT: Not enough entries in the mempool; no object is retrieved.
1456  */
1457 __rte_deprecated
1458 static inline int __attribute__((always_inline))
1459 rte_mempool_sc_get(struct rte_mempool *mp, void **obj_p)
1460 {
1461         return rte_mempool_generic_get(mp, obj_p, 1, NULL, MEMPOOL_F_SC_GET);
1462 }
1463
1464 /**
1465  * Get one object from the mempool.
1466  *
1467  * This function calls the multi-consumers or the single-consumer
1468  * version, depending on the default behavior that was specified at
1469  * mempool creation (see flags).
1470  *
1471  * If cache is enabled, objects will be retrieved first from cache,
1472  * subsequently from the common pool. Note that it can return -ENOENT when
1473  * the local cache and common pool are empty, even if cache from other
1474  * lcores are full.
1475  *
1476  * @param mp
1477  *   A pointer to the mempool structure.
1478  * @param obj_p
1479  *   A pointer to a void * pointer (object) that will be filled.
1480  * @return
1481  *   - 0: Success; objects taken.
1482  *   - -ENOENT: Not enough entries in the mempool; no object is retrieved.
1483  */
1484 static inline int __attribute__((always_inline))
1485 rte_mempool_get(struct rte_mempool *mp, void **obj_p)
1486 {
1487         return rte_mempool_get_bulk(mp, obj_p, 1);
1488 }
1489
1490 /**
1491  * Return the number of entries in the mempool.
1492  *
1493  * When cache is enabled, this function has to browse the length of
1494  * all lcores, so it should not be used in a data path, but only for
1495  * debug purposes. User-owned mempool caches are not accounted for.
1496  *
1497  * @param mp
1498  *   A pointer to the mempool structure.
1499  * @return
1500  *   The number of entries in the mempool.
1501  */
1502 unsigned rte_mempool_count(const struct rte_mempool *mp);
1503
1504 /**
1505  * Return the number of free entries in the mempool ring.
1506  * i.e. how many entries can be freed back to the mempool.
1507  *
1508  * NOTE: This corresponds to the number of elements *allocated* from the
1509  * memory pool, not the number of elements in the pool itself. To count
1510  * the number elements currently available in the pool, use "rte_mempool_count"
1511  *
1512  * When cache is enabled, this function has to browse the length of
1513  * all lcores, so it should not be used in a data path, but only for
1514  * debug purposes. User-owned mempool caches are not accounted for.
1515  *
1516  * @param mp
1517  *   A pointer to the mempool structure.
1518  * @return
1519  *   The number of free entries in the mempool.
1520  */
1521 static inline unsigned
1522 rte_mempool_free_count(const struct rte_mempool *mp)
1523 {
1524         return mp->size - rte_mempool_count(mp);
1525 }
1526
1527 /**
1528  * Test if the mempool is full.
1529  *
1530  * When cache is enabled, this function has to browse the length of all
1531  * lcores, so it should not be used in a data path, but only for debug
1532  * purposes. User-owned mempool caches are not accounted for.
1533  *
1534  * @param mp
1535  *   A pointer to the mempool structure.
1536  * @return
1537  *   - 1: The mempool is full.
1538  *   - 0: The mempool is not full.
1539  */
1540 static inline int
1541 rte_mempool_full(const struct rte_mempool *mp)
1542 {
1543         return !!(rte_mempool_count(mp) == mp->size);
1544 }
1545
1546 /**
1547  * Test if the mempool is empty.
1548  *
1549  * When cache is enabled, this function has to browse the length of all
1550  * lcores, so it should not be used in a data path, but only for debug
1551  * purposes. User-owned mempool caches are not accounted for.
1552  *
1553  * @param mp
1554  *   A pointer to the mempool structure.
1555  * @return
1556  *   - 1: The mempool is empty.
1557  *   - 0: The mempool is not empty.
1558  */
1559 static inline int
1560 rte_mempool_empty(const struct rte_mempool *mp)
1561 {
1562         return !!(rte_mempool_count(mp) == 0);
1563 }
1564
1565 /**
1566  * Return the physical address of elt, which is an element of the pool mp.
1567  *
1568  * @param mp
1569  *   A pointer to the mempool structure.
1570  * @param elt
1571  *   A pointer (virtual address) to the element of the pool.
1572  * @return
1573  *   The physical address of the elt element.
1574  *   If the mempool was created with MEMPOOL_F_NO_PHYS_CONTIG, the
1575  *   returned value is RTE_BAD_PHYS_ADDR.
1576  */
1577 static inline phys_addr_t
1578 rte_mempool_virt2phy(__rte_unused const struct rte_mempool *mp, const void *elt)
1579 {
1580         const struct rte_mempool_objhdr *hdr;
1581         hdr = (const struct rte_mempool_objhdr *)RTE_PTR_SUB(elt,
1582                 sizeof(*hdr));
1583         return hdr->physaddr;
1584 }
1585
1586 /**
1587  * Check the consistency of mempool objects.
1588  *
1589  * Verify the coherency of fields in the mempool structure. Also check
1590  * that the cookies of mempool objects (even the ones that are not
1591  * present in pool) have a correct value. If not, a panic will occur.
1592  *
1593  * @param mp
1594  *   A pointer to the mempool structure.
1595  */
1596 void rte_mempool_audit(struct rte_mempool *mp);
1597
1598 /**
1599  * Return a pointer to the private data in an mempool structure.
1600  *
1601  * @param mp
1602  *   A pointer to the mempool structure.
1603  * @return
1604  *   A pointer to the private data.
1605  */
1606 static inline void *rte_mempool_get_priv(struct rte_mempool *mp)
1607 {
1608         return (char *)mp +
1609                 MEMPOOL_HEADER_SIZE(mp, mp->cache_size);
1610 }
1611
1612 /**
1613  * Dump the status of all mempools on the console
1614  *
1615  * @param f
1616  *   A pointer to a file for output
1617  */
1618 void rte_mempool_list_dump(FILE *f);
1619
1620 /**
1621  * Search a mempool from its name
1622  *
1623  * @param name
1624  *   The name of the mempool.
1625  * @return
1626  *   The pointer to the mempool matching the name, or NULL if not found.
1627  *   NULL on error
1628  *   with rte_errno set appropriately. Possible rte_errno values include:
1629  *    - ENOENT - required entry not available to return.
1630  *
1631  */
1632 struct rte_mempool *rte_mempool_lookup(const char *name);
1633
1634 /**
1635  * Get the header, trailer and total size of a mempool element.
1636  *
1637  * Given a desired size of the mempool element and mempool flags,
1638  * calculates header, trailer, body and total sizes of the mempool object.
1639  *
1640  * @param elt_size
1641  *   The size of each element, without header and trailer.
1642  * @param flags
1643  *   The flags used for the mempool creation.
1644  *   Consult rte_mempool_create() for more information about possible values.
1645  *   The size of each element.
1646  * @param sz
1647  *   The calculated detailed size the mempool object. May be NULL.
1648  * @return
1649  *   Total size of the mempool object.
1650  */
1651 uint32_t rte_mempool_calc_obj_size(uint32_t elt_size, uint32_t flags,
1652         struct rte_mempool_objsz *sz);
1653
1654 /**
1655  * Get the size of memory required to store mempool elements.
1656  *
1657  * Calculate the maximum amount of memory required to store given number
1658  * of objects. Assume that the memory buffer will be aligned at page
1659  * boundary.
1660  *
1661  * Note that if object size is bigger then page size, then it assumes
1662  * that pages are grouped in subsets of physically continuous pages big
1663  * enough to store at least one object.
1664  *
1665  * @param elt_num
1666  *   Number of elements.
1667  * @param total_elt_sz
1668  *   The size of each element, including header and trailer, as returned
1669  *   by rte_mempool_calc_obj_size().
1670  * @param pg_shift
1671  *   LOG2 of the physical pages size. If set to 0, ignore page boundaries.
1672  * @return
1673  *   Required memory size aligned at page boundary.
1674  */
1675 size_t rte_mempool_xmem_size(uint32_t elt_num, size_t total_elt_sz,
1676         uint32_t pg_shift);
1677
1678 /**
1679  * Get the size of memory required to store mempool elements.
1680  *
1681  * Calculate how much memory would be actually required with the given
1682  * memory footprint to store required number of objects.
1683  *
1684  * @param vaddr
1685  *   Virtual address of the externally allocated memory buffer.
1686  *   Will be used to store mempool objects.
1687  * @param elt_num
1688  *   Number of elements.
1689  * @param total_elt_sz
1690  *   The size of each element, including header and trailer, as returned
1691  *   by rte_mempool_calc_obj_size().
1692  * @param paddr
1693  *   Array of physical addresses of the pages that comprises given memory
1694  *   buffer.
1695  * @param pg_num
1696  *   Number of elements in the paddr array.
1697  * @param pg_shift
1698  *   LOG2 of the physical pages size.
1699  * @return
1700  *   On success, the number of bytes needed to store given number of
1701  *   objects, aligned to the given page size. If the provided memory
1702  *   buffer is too small, return a negative value whose absolute value
1703  *   is the actual number of elements that can be stored in that buffer.
1704  */
1705 ssize_t rte_mempool_xmem_usage(void *vaddr, uint32_t elt_num,
1706         size_t total_elt_sz, const phys_addr_t paddr[], uint32_t pg_num,
1707         uint32_t pg_shift);
1708
1709 /**
1710  * Walk list of all memory pools
1711  *
1712  * @param func
1713  *   Iterator function
1714  * @param arg
1715  *   Argument passed to iterator
1716  */
1717 void rte_mempool_walk(void (*func)(struct rte_mempool *, void *arg),
1718                       void *arg);
1719
1720 #ifdef __cplusplus
1721 }
1722 #endif
1723
1724 #endif /* _RTE_MEMPOOL_H_ */