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