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