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