mempool: notify memory area to pool
[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 /**
417  * Notify new memory area to mempool.
418  */
419 typedef int (*rte_mempool_ops_register_memory_area_t)
420 (const struct rte_mempool *mp, char *vaddr, phys_addr_t paddr, size_t len);
421
422 /** Structure defining mempool operations structure */
423 struct rte_mempool_ops {
424         char name[RTE_MEMPOOL_OPS_NAMESIZE]; /**< Name of mempool ops struct. */
425         rte_mempool_alloc_t alloc;       /**< Allocate private data. */
426         rte_mempool_free_t free;         /**< Free the external pool. */
427         rte_mempool_enqueue_t enqueue;   /**< Enqueue an object. */
428         rte_mempool_dequeue_t dequeue;   /**< Dequeue an object. */
429         rte_mempool_get_count get_count; /**< Get qty of available objs. */
430         /**
431          * Get the mempool capabilities
432          */
433         rte_mempool_get_capabilities_t get_capabilities;
434         /**
435          * Notify new memory area to mempool
436          */
437         rte_mempool_ops_register_memory_area_t register_memory_area;
438 } __rte_cache_aligned;
439
440 #define RTE_MEMPOOL_MAX_OPS_IDX 16  /**< Max registered ops structs */
441
442 /**
443  * Structure storing the table of registered ops structs, each of which contain
444  * the function pointers for the mempool ops functions.
445  * Each process has its own storage for this ops struct array so that
446  * the mempools can be shared across primary and secondary processes.
447  * The indices used to access the array are valid across processes, whereas
448  * any function pointers stored directly in the mempool struct would not be.
449  * This results in us simply having "ops_index" in the mempool struct.
450  */
451 struct rte_mempool_ops_table {
452         rte_spinlock_t sl;     /**< Spinlock for add/delete. */
453         uint32_t num_ops;      /**< Number of used ops structs in the table. */
454         /**
455          * Storage for all possible ops structs.
456          */
457         struct rte_mempool_ops ops[RTE_MEMPOOL_MAX_OPS_IDX];
458 } __rte_cache_aligned;
459
460 /** Array of registered ops structs. */
461 extern struct rte_mempool_ops_table rte_mempool_ops_table;
462
463 /**
464  * @internal Get the mempool ops struct from its index.
465  *
466  * @param ops_index
467  *   The index of the ops struct in the ops struct table. It must be a valid
468  *   index: (0 <= idx < num_ops).
469  * @return
470  *   The pointer to the ops struct in the table.
471  */
472 static inline struct rte_mempool_ops *
473 rte_mempool_get_ops(int ops_index)
474 {
475         RTE_VERIFY((ops_index >= 0) && (ops_index < RTE_MEMPOOL_MAX_OPS_IDX));
476
477         return &rte_mempool_ops_table.ops[ops_index];
478 }
479
480 /**
481  * @internal Wrapper for mempool_ops alloc callback.
482  *
483  * @param mp
484  *   Pointer to the memory pool.
485  * @return
486  *   - 0: Success; successfully allocated mempool pool_data.
487  *   - <0: Error; code of alloc function.
488  */
489 int
490 rte_mempool_ops_alloc(struct rte_mempool *mp);
491
492 /**
493  * @internal Wrapper for mempool_ops dequeue callback.
494  *
495  * @param mp
496  *   Pointer to the memory pool.
497  * @param obj_table
498  *   Pointer to a table of void * pointers (objects).
499  * @param n
500  *   Number of objects to get.
501  * @return
502  *   - 0: Success; got n objects.
503  *   - <0: Error; code of dequeue function.
504  */
505 static inline int
506 rte_mempool_ops_dequeue_bulk(struct rte_mempool *mp,
507                 void **obj_table, unsigned n)
508 {
509         struct rte_mempool_ops *ops;
510
511         ops = rte_mempool_get_ops(mp->ops_index);
512         return ops->dequeue(mp, obj_table, n);
513 }
514
515 /**
516  * @internal wrapper for mempool_ops enqueue callback.
517  *
518  * @param mp
519  *   Pointer to the memory pool.
520  * @param obj_table
521  *   Pointer to a table of void * pointers (objects).
522  * @param n
523  *   Number of objects to put.
524  * @return
525  *   - 0: Success; n objects supplied.
526  *   - <0: Error; code of enqueue function.
527  */
528 static inline int
529 rte_mempool_ops_enqueue_bulk(struct rte_mempool *mp, void * const *obj_table,
530                 unsigned n)
531 {
532         struct rte_mempool_ops *ops;
533
534         ops = rte_mempool_get_ops(mp->ops_index);
535         return ops->enqueue(mp, obj_table, n);
536 }
537
538 /**
539  * @internal wrapper for mempool_ops get_count callback.
540  *
541  * @param mp
542  *   Pointer to the memory pool.
543  * @return
544  *   The number of available objects in the external pool.
545  */
546 unsigned
547 rte_mempool_ops_get_count(const struct rte_mempool *mp);
548
549 /**
550  * @internal wrapper for mempool_ops get_capabilities callback.
551  *
552  * @param mp [in]
553  *   Pointer to the memory pool.
554  * @param flags [out]
555  *   Pointer to the mempool flags.
556  * @return
557  *   - 0: Success; The mempool driver has advertised his pool capabilities in
558  *   flags param.
559  *   - -ENOTSUP - doesn't support get_capabilities ops (valid case).
560  *   - Otherwise, pool create fails.
561  */
562 int
563 rte_mempool_ops_get_capabilities(const struct rte_mempool *mp,
564                                         unsigned int *flags);
565 /**
566  * @internal wrapper for mempool_ops register_memory_area callback.
567  * API to notify the mempool handler when a new memory area is added to pool.
568  *
569  * @param mp
570  *   Pointer to the memory pool.
571  * @param vaddr
572  *   Pointer to the buffer virtual address.
573  * @param paddr
574  *   Pointer to the buffer physical address.
575  * @param len
576  *   Pool size.
577  * @return
578  *   - 0: Success;
579  *   - -ENOTSUP - doesn't support register_memory_area ops (valid error case).
580  *   - Otherwise, rte_mempool_populate_phys fails thus pool create fails.
581  */
582 int
583 rte_mempool_ops_register_memory_area(const struct rte_mempool *mp,
584                                 char *vaddr, phys_addr_t paddr, size_t len);
585
586 /**
587  * @internal wrapper for mempool_ops free callback.
588  *
589  * @param mp
590  *   Pointer to the memory pool.
591  */
592 void
593 rte_mempool_ops_free(struct rte_mempool *mp);
594
595 /**
596  * Set the ops of a mempool.
597  *
598  * This can only be done on a mempool that is not populated, i.e. just after
599  * a call to rte_mempool_create_empty().
600  *
601  * @param mp
602  *   Pointer to the memory pool.
603  * @param name
604  *   Name of the ops structure to use for this mempool.
605  * @param pool_config
606  *   Opaque data that can be passed by the application to the ops functions.
607  * @return
608  *   - 0: Success; the mempool is now using the requested ops functions.
609  *   - -EINVAL - Invalid ops struct name provided.
610  *   - -EEXIST - mempool already has an ops struct assigned.
611  */
612 int
613 rte_mempool_set_ops_byname(struct rte_mempool *mp, const char *name,
614                 void *pool_config);
615
616 /**
617  * Register mempool operations.
618  *
619  * @param ops
620  *   Pointer to an ops structure to register.
621  * @return
622  *   - >=0: Success; return the index of the ops struct in the table.
623  *   - -EINVAL - some missing callbacks while registering ops struct.
624  *   - -ENOSPC - the maximum number of ops structs has been reached.
625  */
626 int rte_mempool_register_ops(const struct rte_mempool_ops *ops);
627
628 /**
629  * Macro to statically register the ops of a mempool handler.
630  * Note that the rte_mempool_register_ops fails silently here when
631  * more than RTE_MEMPOOL_MAX_OPS_IDX is registered.
632  */
633 #define MEMPOOL_REGISTER_OPS(ops)                                       \
634         void mp_hdlr_init_##ops(void);                                  \
635         void __attribute__((constructor, used)) mp_hdlr_init_##ops(void)\
636         {                                                               \
637                 rte_mempool_register_ops(&ops);                 \
638         }
639
640 /**
641  * An object callback function for mempool.
642  *
643  * Used by rte_mempool_create() and rte_mempool_obj_iter().
644  */
645 typedef void (rte_mempool_obj_cb_t)(struct rte_mempool *mp,
646                 void *opaque, void *obj, unsigned obj_idx);
647 typedef rte_mempool_obj_cb_t rte_mempool_obj_ctor_t; /* compat */
648
649 /**
650  * A memory callback function for mempool.
651  *
652  * Used by rte_mempool_mem_iter().
653  */
654 typedef void (rte_mempool_mem_cb_t)(struct rte_mempool *mp,
655                 void *opaque, struct rte_mempool_memhdr *memhdr,
656                 unsigned mem_idx);
657
658 /**
659  * A mempool constructor callback function.
660  *
661  * Arguments are the mempool and the opaque pointer given by the user in
662  * rte_mempool_create().
663  */
664 typedef void (rte_mempool_ctor_t)(struct rte_mempool *, void *);
665
666 /**
667  * Create a new mempool named *name* in memory.
668  *
669  * This function uses ``rte_memzone_reserve()`` to allocate memory. The
670  * pool contains n elements of elt_size. Its size is set to n.
671  *
672  * @param name
673  *   The name of the mempool.
674  * @param n
675  *   The number of elements in the mempool. The optimum size (in terms of
676  *   memory usage) for a mempool is when n is a power of two minus one:
677  *   n = (2^q - 1).
678  * @param elt_size
679  *   The size of each element.
680  * @param cache_size
681  *   If cache_size is non-zero, the rte_mempool library will try to
682  *   limit the accesses to the common lockless pool, by maintaining a
683  *   per-lcore object cache. This argument must be lower or equal to
684  *   CONFIG_RTE_MEMPOOL_CACHE_MAX_SIZE and n / 1.5. It is advised to choose
685  *   cache_size to have "n modulo cache_size == 0": if this is
686  *   not the case, some elements will always stay in the pool and will
687  *   never be used. The access to the per-lcore table is of course
688  *   faster than the multi-producer/consumer pool. The cache can be
689  *   disabled if the cache_size argument is set to 0; it can be useful to
690  *   avoid losing objects in cache.
691  * @param private_data_size
692  *   The size of the private data appended after the mempool
693  *   structure. This is useful for storing some private data after the
694  *   mempool structure, as is done for rte_mbuf_pool for example.
695  * @param mp_init
696  *   A function pointer that is called for initialization of the pool,
697  *   before object initialization. The user can initialize the private
698  *   data in this function if needed. This parameter can be NULL if
699  *   not needed.
700  * @param mp_init_arg
701  *   An opaque pointer to data that can be used in the mempool
702  *   constructor function.
703  * @param obj_init
704  *   A function pointer that is called for each object at
705  *   initialization of the pool. The user can set some meta data in
706  *   objects if needed. This parameter can be NULL if not needed.
707  *   The obj_init() function takes the mempool pointer, the init_arg,
708  *   the object pointer and the object number as parameters.
709  * @param obj_init_arg
710  *   An opaque pointer to data that can be used as an argument for
711  *   each call to the object constructor function.
712  * @param socket_id
713  *   The *socket_id* argument is the socket identifier in the case of
714  *   NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
715  *   constraint for the reserved zone.
716  * @param flags
717  *   The *flags* arguments is an OR of following flags:
718  *   - MEMPOOL_F_NO_SPREAD: By default, objects addresses are spread
719  *     between channels in RAM: the pool allocator will add padding
720  *     between objects depending on the hardware configuration. See
721  *     Memory alignment constraints for details. If this flag is set,
722  *     the allocator will just align them to a cache line.
723  *   - MEMPOOL_F_NO_CACHE_ALIGN: By default, the returned objects are
724  *     cache-aligned. This flag removes this constraint, and no
725  *     padding will be present between objects. This flag implies
726  *     MEMPOOL_F_NO_SPREAD.
727  *   - MEMPOOL_F_SP_PUT: If this flag is set, the default behavior
728  *     when using rte_mempool_put() or rte_mempool_put_bulk() is
729  *     "single-producer". Otherwise, it is "multi-producers".
730  *   - MEMPOOL_F_SC_GET: If this flag is set, the default behavior
731  *     when using rte_mempool_get() or rte_mempool_get_bulk() is
732  *     "single-consumer". Otherwise, it is "multi-consumers".
733  *   - MEMPOOL_F_NO_PHYS_CONTIG: If set, allocated objects won't
734  *     necessarily be contiguous in physical memory.
735  * @return
736  *   The pointer to the new allocated mempool, on success. NULL on error
737  *   with rte_errno set appropriately. Possible rte_errno values include:
738  *    - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
739  *    - E_RTE_SECONDARY - function was called from a secondary process instance
740  *    - EINVAL - cache size provided is too large
741  *    - ENOSPC - the maximum number of memzones has already been allocated
742  *    - EEXIST - a memzone with the same name already exists
743  *    - ENOMEM - no appropriate memory area found in which to create memzone
744  */
745 struct rte_mempool *
746 rte_mempool_create(const char *name, unsigned n, unsigned elt_size,
747                    unsigned cache_size, unsigned private_data_size,
748                    rte_mempool_ctor_t *mp_init, void *mp_init_arg,
749                    rte_mempool_obj_cb_t *obj_init, void *obj_init_arg,
750                    int socket_id, unsigned flags);
751
752 /**
753  * Create a new mempool named *name* in memory.
754  *
755  * The pool contains n elements of elt_size. Its size is set to n.
756  * This function uses ``memzone_reserve()`` to allocate the mempool header
757  * (and the objects if vaddr is NULL).
758  * Depending on the input parameters, mempool elements can be either allocated
759  * together with the mempool header, or an externally provided memory buffer
760  * could be used to store mempool objects. In later case, that external
761  * memory buffer can consist of set of disjoint physical pages.
762  *
763  * @param name
764  *   The name of the mempool.
765  * @param n
766  *   The number of elements in the mempool. The optimum size (in terms of
767  *   memory usage) for a mempool is when n is a power of two minus one:
768  *   n = (2^q - 1).
769  * @param elt_size
770  *   The size of each element.
771  * @param cache_size
772  *   Size of the cache. See rte_mempool_create() for details.
773  * @param private_data_size
774  *   The size of the private data appended after the mempool
775  *   structure. This is useful for storing some private data after the
776  *   mempool structure, as is done for rte_mbuf_pool for example.
777  * @param mp_init
778  *   A function pointer that is called for initialization of the pool,
779  *   before object initialization. The user can initialize the private
780  *   data in this function if needed. This parameter can be NULL if
781  *   not needed.
782  * @param mp_init_arg
783  *   An opaque pointer to data that can be used in the mempool
784  *   constructor function.
785  * @param obj_init
786  *   A function called for each object at initialization of the pool.
787  *   See rte_mempool_create() for details.
788  * @param obj_init_arg
789  *   An opaque pointer passed to the object constructor function.
790  * @param socket_id
791  *   The *socket_id* argument is the socket identifier in the case of
792  *   NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
793  *   constraint for the reserved zone.
794  * @param flags
795  *   Flags controlling the behavior of the mempool. See
796  *   rte_mempool_create() for details.
797  * @param vaddr
798  *   Virtual address of the externally allocated memory buffer.
799  *   Will be used to store mempool objects.
800  * @param paddr
801  *   Array of physical addresses of the pages that comprises given memory
802  *   buffer.
803  * @param pg_num
804  *   Number of elements in the paddr array.
805  * @param pg_shift
806  *   LOG2 of the physical pages size.
807  * @return
808  *   The pointer to the new allocated mempool, on success. NULL on error
809  *   with rte_errno set appropriately. See rte_mempool_create() for details.
810  */
811 struct rte_mempool *
812 rte_mempool_xmem_create(const char *name, unsigned n, unsigned elt_size,
813                 unsigned cache_size, unsigned private_data_size,
814                 rte_mempool_ctor_t *mp_init, void *mp_init_arg,
815                 rte_mempool_obj_cb_t *obj_init, void *obj_init_arg,
816                 int socket_id, unsigned flags, void *vaddr,
817                 const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift);
818
819 /**
820  * Create an empty mempool
821  *
822  * The mempool is allocated and initialized, but it is not populated: no
823  * memory is allocated for the mempool elements. The user has to call
824  * rte_mempool_populate_*() to add memory chunks to the pool. Once
825  * populated, the user may also want to initialize each object with
826  * rte_mempool_obj_iter().
827  *
828  * @param name
829  *   The name of the mempool.
830  * @param n
831  *   The maximum number of elements that can be added in the mempool.
832  *   The optimum size (in terms of memory usage) for a mempool is when n
833  *   is a power of two minus one: n = (2^q - 1).
834  * @param elt_size
835  *   The size of each element.
836  * @param cache_size
837  *   Size of the cache. See rte_mempool_create() for details.
838  * @param private_data_size
839  *   The size of the private data appended after the mempool
840  *   structure. This is useful for storing some private data after the
841  *   mempool structure, as is done for rte_mbuf_pool for example.
842  * @param socket_id
843  *   The *socket_id* argument is the socket identifier in the case of
844  *   NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
845  *   constraint for the reserved zone.
846  * @param flags
847  *   Flags controlling the behavior of the mempool. See
848  *   rte_mempool_create() for details.
849  * @return
850  *   The pointer to the new allocated mempool, on success. NULL on error
851  *   with rte_errno set appropriately. See rte_mempool_create() for details.
852  */
853 struct rte_mempool *
854 rte_mempool_create_empty(const char *name, unsigned n, unsigned elt_size,
855         unsigned cache_size, unsigned private_data_size,
856         int socket_id, unsigned flags);
857 /**
858  * Free a mempool
859  *
860  * Unlink the mempool from global list, free the memory chunks, and all
861  * memory referenced by the mempool. The objects must not be used by
862  * other cores as they will be freed.
863  *
864  * @param mp
865  *   A pointer to the mempool structure.
866  */
867 void
868 rte_mempool_free(struct rte_mempool *mp);
869
870 /**
871  * Add physically contiguous memory for objects in the pool at init
872  *
873  * Add a virtually and physically contiguous memory chunk in the pool
874  * where objects can be instantiated.
875  *
876  * If the given physical address is unknown (paddr = RTE_BAD_PHYS_ADDR),
877  * the chunk doesn't need to be physically contiguous (only virtually),
878  * and allocated objects may span two pages.
879  *
880  * @param mp
881  *   A pointer to the mempool structure.
882  * @param vaddr
883  *   The virtual address of memory that should be used to store objects.
884  * @param paddr
885  *   The physical address
886  * @param len
887  *   The length of memory in bytes.
888  * @param free_cb
889  *   The callback used to free this chunk when destroying the mempool.
890  * @param opaque
891  *   An opaque argument passed to free_cb.
892  * @return
893  *   The number of objects added on success.
894  *   On error, the chunk is not added in the memory list of the
895  *   mempool and a negative errno is returned.
896  */
897 int rte_mempool_populate_phys(struct rte_mempool *mp, char *vaddr,
898         phys_addr_t paddr, size_t len, rte_mempool_memchunk_free_cb_t *free_cb,
899         void *opaque);
900
901 /**
902  * Add physical memory for objects in the pool at init
903  *
904  * Add a virtually contiguous memory chunk in the pool where objects can
905  * be instantiated. The physical addresses corresponding to the virtual
906  * area are described in paddr[], pg_num, pg_shift.
907  *
908  * @param mp
909  *   A pointer to the mempool structure.
910  * @param vaddr
911  *   The virtual address of memory that should be used to store objects.
912  * @param paddr
913  *   An array of physical addresses of each page composing the virtual
914  *   area.
915  * @param pg_num
916  *   Number of elements in the paddr array.
917  * @param pg_shift
918  *   LOG2 of the physical pages size.
919  * @param free_cb
920  *   The callback used to free this chunk when destroying the mempool.
921  * @param opaque
922  *   An opaque argument passed to free_cb.
923  * @return
924  *   The number of objects added on success.
925  *   On error, the chunks are not added in the memory list of the
926  *   mempool and a negative errno is returned.
927  */
928 int rte_mempool_populate_phys_tab(struct rte_mempool *mp, char *vaddr,
929         const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift,
930         rte_mempool_memchunk_free_cb_t *free_cb, void *opaque);
931
932 /**
933  * Add virtually contiguous memory for objects in the pool at init
934  *
935  * Add a virtually contiguous memory chunk in the pool where objects can
936  * be instantiated.
937  *
938  * @param mp
939  *   A pointer to the mempool structure.
940  * @param addr
941  *   The virtual address of memory that should be used to store objects.
942  *   Must be page-aligned.
943  * @param len
944  *   The length of memory in bytes. Must be page-aligned.
945  * @param pg_sz
946  *   The size of memory pages in this virtual area.
947  * @param free_cb
948  *   The callback used to free this chunk when destroying the mempool.
949  * @param opaque
950  *   An opaque argument passed to free_cb.
951  * @return
952  *   The number of objects added on success.
953  *   On error, the chunk is not added in the memory list of the
954  *   mempool and a negative errno is returned.
955  */
956 int
957 rte_mempool_populate_virt(struct rte_mempool *mp, char *addr,
958         size_t len, size_t pg_sz, rte_mempool_memchunk_free_cb_t *free_cb,
959         void *opaque);
960
961 /**
962  * Add memory for objects in the pool at init
963  *
964  * This is the default function used by rte_mempool_create() to populate
965  * the mempool. It adds memory allocated using rte_memzone_reserve().
966  *
967  * @param mp
968  *   A pointer to the mempool structure.
969  * @return
970  *   The number of objects added on success.
971  *   On error, the chunk is not added in the memory list of the
972  *   mempool and a negative errno is returned.
973  */
974 int rte_mempool_populate_default(struct rte_mempool *mp);
975
976 /**
977  * Add memory from anonymous mapping for objects in the pool at init
978  *
979  * This function mmap an anonymous memory zone that is locked in
980  * memory to store the objects of the mempool.
981  *
982  * @param mp
983  *   A pointer to the mempool structure.
984  * @return
985  *   The number of objects added on success.
986  *   On error, the chunk is not added in the memory list of the
987  *   mempool and a negative errno is returned.
988  */
989 int rte_mempool_populate_anon(struct rte_mempool *mp);
990
991 /**
992  * Call a function for each mempool element
993  *
994  * Iterate across all objects attached to a rte_mempool and call the
995  * callback function on it.
996  *
997  * @param mp
998  *   A pointer to an initialized mempool.
999  * @param obj_cb
1000  *   A function pointer that is called for each object.
1001  * @param obj_cb_arg
1002  *   An opaque pointer passed to the callback function.
1003  * @return
1004  *   Number of objects iterated.
1005  */
1006 uint32_t rte_mempool_obj_iter(struct rte_mempool *mp,
1007         rte_mempool_obj_cb_t *obj_cb, void *obj_cb_arg);
1008
1009 /**
1010  * Call a function for each mempool memory chunk
1011  *
1012  * Iterate across all memory chunks attached to a rte_mempool and call
1013  * the callback function on it.
1014  *
1015  * @param mp
1016  *   A pointer to an initialized mempool.
1017  * @param mem_cb
1018  *   A function pointer that is called for each memory chunk.
1019  * @param mem_cb_arg
1020  *   An opaque pointer passed to the callback function.
1021  * @return
1022  *   Number of memory chunks iterated.
1023  */
1024 uint32_t rte_mempool_mem_iter(struct rte_mempool *mp,
1025         rte_mempool_mem_cb_t *mem_cb, void *mem_cb_arg);
1026
1027 /**
1028  * Dump the status of the mempool to a file.
1029  *
1030  * @param f
1031  *   A pointer to a file for output
1032  * @param mp
1033  *   A pointer to the mempool structure.
1034  */
1035 void rte_mempool_dump(FILE *f, struct rte_mempool *mp);
1036
1037 /**
1038  * Create a user-owned mempool cache.
1039  *
1040  * This can be used by non-EAL threads to enable caching when they
1041  * interact with a mempool.
1042  *
1043  * @param size
1044  *   The size of the mempool cache. See rte_mempool_create()'s cache_size
1045  *   parameter description for more information. The same limits and
1046  *   considerations apply here too.
1047  * @param socket_id
1048  *   The socket identifier in the case of NUMA. The value can be
1049  *   SOCKET_ID_ANY if there is no NUMA constraint for the reserved zone.
1050  */
1051 struct rte_mempool_cache *
1052 rte_mempool_cache_create(uint32_t size, int socket_id);
1053
1054 /**
1055  * Free a user-owned mempool cache.
1056  *
1057  * @param cache
1058  *   A pointer to the mempool cache.
1059  */
1060 void
1061 rte_mempool_cache_free(struct rte_mempool_cache *cache);
1062
1063 /**
1064  * Flush a user-owned mempool cache to the specified mempool.
1065  *
1066  * @param cache
1067  *   A pointer to the mempool cache.
1068  * @param mp
1069  *   A pointer to the mempool.
1070  */
1071 static __rte_always_inline void
1072 rte_mempool_cache_flush(struct rte_mempool_cache *cache,
1073                         struct rte_mempool *mp)
1074 {
1075         rte_mempool_ops_enqueue_bulk(mp, cache->objs, cache->len);
1076         cache->len = 0;
1077 }
1078
1079 /**
1080  * Get a pointer to the per-lcore default mempool cache.
1081  *
1082  * @param mp
1083  *   A pointer to the mempool structure.
1084  * @param lcore_id
1085  *   The logical core id.
1086  * @return
1087  *   A pointer to the mempool cache or NULL if disabled or non-EAL thread.
1088  */
1089 static __rte_always_inline struct rte_mempool_cache *
1090 rte_mempool_default_cache(struct rte_mempool *mp, unsigned lcore_id)
1091 {
1092         if (mp->cache_size == 0)
1093                 return NULL;
1094
1095         if (lcore_id >= RTE_MAX_LCORE)
1096                 return NULL;
1097
1098         return &mp->local_cache[lcore_id];
1099 }
1100
1101 /**
1102  * @internal Put several objects back in the mempool; used internally.
1103  * @param mp
1104  *   A pointer to the mempool structure.
1105  * @param obj_table
1106  *   A pointer to a table of void * pointers (objects).
1107  * @param n
1108  *   The number of objects to store back in the mempool, must be strictly
1109  *   positive.
1110  * @param cache
1111  *   A pointer to a mempool cache structure. May be NULL if not needed.
1112  */
1113 static __rte_always_inline void
1114 __mempool_generic_put(struct rte_mempool *mp, void * const *obj_table,
1115                       unsigned int n, struct rte_mempool_cache *cache)
1116 {
1117         void **cache_objs;
1118
1119         /* increment stat now, adding in mempool always success */
1120         __MEMPOOL_STAT_ADD(mp, put, n);
1121
1122         /* No cache provided or if put would overflow mem allocated for cache */
1123         if (unlikely(cache == NULL || n > RTE_MEMPOOL_CACHE_MAX_SIZE))
1124                 goto ring_enqueue;
1125
1126         cache_objs = &cache->objs[cache->len];
1127
1128         /*
1129          * The cache follows the following algorithm
1130          *   1. Add the objects to the cache
1131          *   2. Anything greater than the cache min value (if it crosses the
1132          *   cache flush threshold) is flushed to the ring.
1133          */
1134
1135         /* Add elements back into the cache */
1136         rte_memcpy(&cache_objs[0], obj_table, sizeof(void *) * n);
1137
1138         cache->len += n;
1139
1140         if (cache->len >= cache->flushthresh) {
1141                 rte_mempool_ops_enqueue_bulk(mp, &cache->objs[cache->size],
1142                                 cache->len - cache->size);
1143                 cache->len = cache->size;
1144         }
1145
1146         return;
1147
1148 ring_enqueue:
1149
1150         /* push remaining objects in ring */
1151 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
1152         if (rte_mempool_ops_enqueue_bulk(mp, obj_table, n) < 0)
1153                 rte_panic("cannot put objects in mempool\n");
1154 #else
1155         rte_mempool_ops_enqueue_bulk(mp, obj_table, n);
1156 #endif
1157 }
1158
1159
1160 /**
1161  * Put several objects back in the mempool.
1162  *
1163  * @param mp
1164  *   A pointer to the mempool structure.
1165  * @param obj_table
1166  *   A pointer to a table of void * pointers (objects).
1167  * @param n
1168  *   The number of objects to add in the mempool from the obj_table.
1169  * @param cache
1170  *   A pointer to a mempool cache structure. May be NULL if not needed.
1171  */
1172 static __rte_always_inline void
1173 rte_mempool_generic_put(struct rte_mempool *mp, void * const *obj_table,
1174                         unsigned int n, struct rte_mempool_cache *cache)
1175 {
1176         __mempool_check_cookies(mp, obj_table, n, 0);
1177         __mempool_generic_put(mp, obj_table, n, cache);
1178 }
1179
1180 /**
1181  * Put several objects back in the mempool.
1182  *
1183  * This function calls the multi-producer or the single-producer
1184  * version depending on the default behavior that was specified at
1185  * mempool creation time (see flags).
1186  *
1187  * @param mp
1188  *   A pointer to the mempool structure.
1189  * @param obj_table
1190  *   A pointer to a table of void * pointers (objects).
1191  * @param n
1192  *   The number of objects to add in the mempool from obj_table.
1193  */
1194 static __rte_always_inline void
1195 rte_mempool_put_bulk(struct rte_mempool *mp, void * const *obj_table,
1196                      unsigned int n)
1197 {
1198         struct rte_mempool_cache *cache;
1199         cache = rte_mempool_default_cache(mp, rte_lcore_id());
1200         rte_mempool_generic_put(mp, obj_table, n, cache);
1201 }
1202
1203 /**
1204  * Put one object back in the mempool.
1205  *
1206  * This function calls the multi-producer or the single-producer
1207  * version depending on the default behavior that was specified at
1208  * mempool creation time (see flags).
1209  *
1210  * @param mp
1211  *   A pointer to the mempool structure.
1212  * @param obj
1213  *   A pointer to the object to be added.
1214  */
1215 static __rte_always_inline void
1216 rte_mempool_put(struct rte_mempool *mp, void *obj)
1217 {
1218         rte_mempool_put_bulk(mp, &obj, 1);
1219 }
1220
1221 /**
1222  * @internal Get several objects from the mempool; used internally.
1223  * @param mp
1224  *   A pointer to the mempool structure.
1225  * @param obj_table
1226  *   A pointer to a table of void * pointers (objects).
1227  * @param n
1228  *   The number of objects to get, must be strictly positive.
1229  * @param cache
1230  *   A pointer to a mempool cache structure. May be NULL if not needed.
1231  * @return
1232  *   - >=0: Success; number of objects supplied.
1233  *   - <0: Error; code of ring dequeue function.
1234  */
1235 static __rte_always_inline int
1236 __mempool_generic_get(struct rte_mempool *mp, void **obj_table,
1237                       unsigned int n, struct rte_mempool_cache *cache)
1238 {
1239         int ret;
1240         uint32_t index, len;
1241         void **cache_objs;
1242
1243         /* No cache provided or cannot be satisfied from cache */
1244         if (unlikely(cache == NULL || 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  * @return
1310  *   - 0: Success; objects taken.
1311  *   - -ENOENT: Not enough entries in the mempool; no object is retrieved.
1312  */
1313 static __rte_always_inline int
1314 rte_mempool_generic_get(struct rte_mempool *mp, void **obj_table,
1315                         unsigned int n, struct rte_mempool_cache *cache)
1316 {
1317         int ret;
1318         ret = __mempool_generic_get(mp, obj_table, n, cache);
1319         if (ret == 0)
1320                 __mempool_check_cookies(mp, obj_table, n, 1);
1321         return ret;
1322 }
1323
1324 /**
1325  * Get several objects from the mempool.
1326  *
1327  * This function calls the multi-consumers or the single-consumer
1328  * version, depending on the default behaviour that was specified at
1329  * mempool creation time (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_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 the 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 static __rte_always_inline int
1347 rte_mempool_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned int n)
1348 {
1349         struct rte_mempool_cache *cache;
1350         cache = rte_mempool_default_cache(mp, rte_lcore_id());
1351         return rte_mempool_generic_get(mp, obj_table, n, cache);
1352 }
1353
1354 /**
1355  * Get one object from the mempool.
1356  *
1357  * This function calls the multi-consumers or the single-consumer
1358  * version, depending on the default behavior that was specified at
1359  * mempool creation (see flags).
1360  *
1361  * If cache is enabled, objects will be retrieved first from cache,
1362  * subsequently from the common pool. Note that it can return -ENOENT when
1363  * the local cache and common pool are empty, even if cache from other
1364  * lcores are full.
1365  *
1366  * @param mp
1367  *   A pointer to the mempool structure.
1368  * @param obj_p
1369  *   A pointer to a void * pointer (object) that will be filled.
1370  * @return
1371  *   - 0: Success; objects taken.
1372  *   - -ENOENT: Not enough entries in the mempool; no object is retrieved.
1373  */
1374 static __rte_always_inline int
1375 rte_mempool_get(struct rte_mempool *mp, void **obj_p)
1376 {
1377         return rte_mempool_get_bulk(mp, obj_p, 1);
1378 }
1379
1380 /**
1381  * Return the number of entries in the mempool.
1382  *
1383  * When cache is enabled, this function has to browse the length of
1384  * all lcores, so it should not be used in a data path, but only for
1385  * debug purposes. User-owned mempool caches are not accounted for.
1386  *
1387  * @param mp
1388  *   A pointer to the mempool structure.
1389  * @return
1390  *   The number of entries in the mempool.
1391  */
1392 unsigned int rte_mempool_avail_count(const struct rte_mempool *mp);
1393
1394 /**
1395  * Return the number of elements which have been allocated from the mempool
1396  *
1397  * When cache is enabled, this function has to browse the length of
1398  * all lcores, so it should not be used in a data path, but only for
1399  * debug purposes.
1400  *
1401  * @param mp
1402  *   A pointer to the mempool structure.
1403  * @return
1404  *   The number of free entries in the mempool.
1405  */
1406 unsigned int
1407 rte_mempool_in_use_count(const struct rte_mempool *mp);
1408
1409 /**
1410  * Test if the mempool is full.
1411  *
1412  * When cache is enabled, this function has to browse the length of all
1413  * lcores, so it should not be used in a data path, but only for debug
1414  * purposes. User-owned mempool caches are not accounted for.
1415  *
1416  * @param mp
1417  *   A pointer to the mempool structure.
1418  * @return
1419  *   - 1: The mempool is full.
1420  *   - 0: The mempool is not full.
1421  */
1422 static inline int
1423 rte_mempool_full(const struct rte_mempool *mp)
1424 {
1425         return !!(rte_mempool_avail_count(mp) == mp->size);
1426 }
1427
1428 /**
1429  * Test if the mempool is empty.
1430  *
1431  * When cache is enabled, this function has to browse the length of all
1432  * lcores, so it should not be used in a data path, but only for debug
1433  * purposes. User-owned mempool caches are not accounted for.
1434  *
1435  * @param mp
1436  *   A pointer to the mempool structure.
1437  * @return
1438  *   - 1: The mempool is empty.
1439  *   - 0: The mempool is not empty.
1440  */
1441 static inline int
1442 rte_mempool_empty(const struct rte_mempool *mp)
1443 {
1444         return !!(rte_mempool_avail_count(mp) == 0);
1445 }
1446
1447 /**
1448  * Return the physical address of elt, which is an element of the pool mp.
1449  *
1450  * @param mp
1451  *   A pointer to the mempool structure.
1452  * @param elt
1453  *   A pointer (virtual address) to the element of the pool.
1454  * @return
1455  *   The physical address of the elt element.
1456  *   If the mempool was created with MEMPOOL_F_NO_PHYS_CONTIG, the
1457  *   returned value is RTE_BAD_PHYS_ADDR.
1458  */
1459 static inline phys_addr_t
1460 rte_mempool_virt2phy(__rte_unused const struct rte_mempool *mp, const void *elt)
1461 {
1462         const struct rte_mempool_objhdr *hdr;
1463         hdr = (const struct rte_mempool_objhdr *)RTE_PTR_SUB(elt,
1464                 sizeof(*hdr));
1465         return hdr->physaddr;
1466 }
1467
1468 /**
1469  * Check the consistency of mempool objects.
1470  *
1471  * Verify the coherency of fields in the mempool structure. Also check
1472  * that the cookies of mempool objects (even the ones that are not
1473  * present in pool) have a correct value. If not, a panic will occur.
1474  *
1475  * @param mp
1476  *   A pointer to the mempool structure.
1477  */
1478 void rte_mempool_audit(struct rte_mempool *mp);
1479
1480 /**
1481  * Return a pointer to the private data in an mempool structure.
1482  *
1483  * @param mp
1484  *   A pointer to the mempool structure.
1485  * @return
1486  *   A pointer to the private data.
1487  */
1488 static inline void *rte_mempool_get_priv(struct rte_mempool *mp)
1489 {
1490         return (char *)mp +
1491                 MEMPOOL_HEADER_SIZE(mp, mp->cache_size);
1492 }
1493
1494 /**
1495  * Dump the status of all mempools on the console
1496  *
1497  * @param f
1498  *   A pointer to a file for output
1499  */
1500 void rte_mempool_list_dump(FILE *f);
1501
1502 /**
1503  * Search a mempool from its name
1504  *
1505  * @param name
1506  *   The name of the mempool.
1507  * @return
1508  *   The pointer to the mempool matching the name, or NULL if not found.
1509  *   NULL on error
1510  *   with rte_errno set appropriately. Possible rte_errno values include:
1511  *    - ENOENT - required entry not available to return.
1512  *
1513  */
1514 struct rte_mempool *rte_mempool_lookup(const char *name);
1515
1516 /**
1517  * Get the header, trailer and total size of a mempool element.
1518  *
1519  * Given a desired size of the mempool element and mempool flags,
1520  * calculates header, trailer, body and total sizes of the mempool object.
1521  *
1522  * @param elt_size
1523  *   The size of each element, without header and trailer.
1524  * @param flags
1525  *   The flags used for the mempool creation.
1526  *   Consult rte_mempool_create() for more information about possible values.
1527  *   The size of each element.
1528  * @param sz
1529  *   The calculated detailed size the mempool object. May be NULL.
1530  * @return
1531  *   Total size of the mempool object.
1532  */
1533 uint32_t rte_mempool_calc_obj_size(uint32_t elt_size, uint32_t flags,
1534         struct rte_mempool_objsz *sz);
1535
1536 /**
1537  * Get the size of memory required to store mempool elements.
1538  *
1539  * Calculate the maximum amount of memory required to store given number
1540  * of objects. Assume that the memory buffer will be aligned at page
1541  * boundary.
1542  *
1543  * Note that if object size is bigger then page size, then it assumes
1544  * that pages are grouped in subsets of physically continuous pages big
1545  * enough to store at least one object.
1546  *
1547  * @param elt_num
1548  *   Number of elements.
1549  * @param total_elt_sz
1550  *   The size of each element, including header and trailer, as returned
1551  *   by rte_mempool_calc_obj_size().
1552  * @param pg_shift
1553  *   LOG2 of the physical pages size. If set to 0, ignore page boundaries.
1554  * @param flags
1555  *  The mempool flags.
1556  * @return
1557  *   Required memory size aligned at page boundary.
1558  */
1559 size_t rte_mempool_xmem_size(uint32_t elt_num, size_t total_elt_sz,
1560         uint32_t pg_shift, unsigned int flags);
1561
1562 /**
1563  * Get the size of memory required to store mempool elements.
1564  *
1565  * Calculate how much memory would be actually required with the given
1566  * memory footprint to store required number of objects.
1567  *
1568  * @param vaddr
1569  *   Virtual address of the externally allocated memory buffer.
1570  *   Will be used to store mempool objects.
1571  * @param elt_num
1572  *   Number of elements.
1573  * @param total_elt_sz
1574  *   The size of each element, including header and trailer, as returned
1575  *   by rte_mempool_calc_obj_size().
1576  * @param paddr
1577  *   Array of physical addresses of the pages that comprises given memory
1578  *   buffer.
1579  * @param pg_num
1580  *   Number of elements in the paddr array.
1581  * @param pg_shift
1582  *   LOG2 of the physical pages size.
1583  * @param flags
1584  *  The mempool flags.
1585  * @return
1586  *   On success, the number of bytes needed to store given number of
1587  *   objects, aligned to the given page size. If the provided memory
1588  *   buffer is too small, return a negative value whose absolute value
1589  *   is the actual number of elements that can be stored in that buffer.
1590  */
1591 ssize_t rte_mempool_xmem_usage(void *vaddr, uint32_t elt_num,
1592         size_t total_elt_sz, const phys_addr_t paddr[], uint32_t pg_num,
1593         uint32_t pg_shift, unsigned int flags);
1594
1595 /**
1596  * Walk list of all memory pools
1597  *
1598  * @param func
1599  *   Iterator function
1600  * @param arg
1601  *   Argument passed to iterator
1602  */
1603 void rte_mempool_walk(void (*func)(struct rte_mempool *, void *arg),
1604                       void *arg);
1605
1606 #ifdef __cplusplus
1607 }
1608 #endif
1609
1610 #endif /* _RTE_MEMPOOL_H_ */