New upstream version 17.11-rc3
[deb_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 IO address is unknown (iova = RTE_BAD_IOVA),
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 iova
892  *   The IO 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_iova(struct rte_mempool *mp, char *vaddr,
905         rte_iova_t iova, size_t len, rte_mempool_memchunk_free_cb_t *free_cb,
906         void *opaque);
907
908 __rte_deprecated
909 int rte_mempool_populate_phys(struct rte_mempool *mp, char *vaddr,
910         phys_addr_t paddr, size_t len, rte_mempool_memchunk_free_cb_t *free_cb,
911         void *opaque);
912
913 /**
914  * Add physical memory for objects in the pool at init
915  *
916  * Add a virtually contiguous memory chunk in the pool where objects can
917  * be instantiated. The IO addresses corresponding to the virtual
918  * area are described in iova[], pg_num, pg_shift.
919  *
920  * @param mp
921  *   A pointer to the mempool structure.
922  * @param vaddr
923  *   The virtual address of memory that should be used to store objects.
924  * @param iova
925  *   An array of IO addresses of each page composing the virtual area.
926  * @param pg_num
927  *   Number of elements in the iova array.
928  * @param pg_shift
929  *   LOG2 of the physical pages size.
930  * @param free_cb
931  *   The callback used to free this chunk when destroying the mempool.
932  * @param opaque
933  *   An opaque argument passed to free_cb.
934  * @return
935  *   The number of objects added on success.
936  *   On error, the chunks are not added in the memory list of the
937  *   mempool and a negative errno is returned.
938  */
939 int rte_mempool_populate_iova_tab(struct rte_mempool *mp, char *vaddr,
940         const rte_iova_t iova[], uint32_t pg_num, uint32_t pg_shift,
941         rte_mempool_memchunk_free_cb_t *free_cb, void *opaque);
942
943 __rte_deprecated
944 int rte_mempool_populate_phys_tab(struct rte_mempool *mp, char *vaddr,
945         const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift,
946         rte_mempool_memchunk_free_cb_t *free_cb, void *opaque);
947
948 /**
949  * Add virtually contiguous memory for objects in the pool at init
950  *
951  * Add a virtually contiguous memory chunk in the pool where objects can
952  * be instantiated.
953  *
954  * @param mp
955  *   A pointer to the mempool structure.
956  * @param addr
957  *   The virtual address of memory that should be used to store objects.
958  *   Must be page-aligned.
959  * @param len
960  *   The length of memory in bytes. Must be page-aligned.
961  * @param pg_sz
962  *   The size of memory pages in this virtual area.
963  * @param free_cb
964  *   The callback used to free this chunk when destroying the mempool.
965  * @param opaque
966  *   An opaque argument passed to free_cb.
967  * @return
968  *   The number of objects added on success.
969  *   On error, the chunk is not added in the memory list of the
970  *   mempool and a negative errno is returned.
971  */
972 int
973 rte_mempool_populate_virt(struct rte_mempool *mp, char *addr,
974         size_t len, size_t pg_sz, rte_mempool_memchunk_free_cb_t *free_cb,
975         void *opaque);
976
977 /**
978  * Add memory for objects in the pool at init
979  *
980  * This is the default function used by rte_mempool_create() to populate
981  * the mempool. It adds memory allocated using rte_memzone_reserve().
982  *
983  * @param mp
984  *   A pointer to the mempool structure.
985  * @return
986  *   The number of objects added on success.
987  *   On error, the chunk is not added in the memory list of the
988  *   mempool and a negative errno is returned.
989  */
990 int rte_mempool_populate_default(struct rte_mempool *mp);
991
992 /**
993  * Add memory from anonymous mapping for objects in the pool at init
994  *
995  * This function mmap an anonymous memory zone that is locked in
996  * memory to store the objects of the mempool.
997  *
998  * @param mp
999  *   A pointer to the mempool structure.
1000  * @return
1001  *   The number of objects added on success.
1002  *   On error, the chunk is not added in the memory list of the
1003  *   mempool and a negative errno is returned.
1004  */
1005 int rte_mempool_populate_anon(struct rte_mempool *mp);
1006
1007 /**
1008  * Call a function for each mempool element
1009  *
1010  * Iterate across all objects attached to a rte_mempool and call the
1011  * callback function on it.
1012  *
1013  * @param mp
1014  *   A pointer to an initialized mempool.
1015  * @param obj_cb
1016  *   A function pointer that is called for each object.
1017  * @param obj_cb_arg
1018  *   An opaque pointer passed to the callback function.
1019  * @return
1020  *   Number of objects iterated.
1021  */
1022 uint32_t rte_mempool_obj_iter(struct rte_mempool *mp,
1023         rte_mempool_obj_cb_t *obj_cb, void *obj_cb_arg);
1024
1025 /**
1026  * Call a function for each mempool memory chunk
1027  *
1028  * Iterate across all memory chunks attached to a rte_mempool and call
1029  * the callback function on it.
1030  *
1031  * @param mp
1032  *   A pointer to an initialized mempool.
1033  * @param mem_cb
1034  *   A function pointer that is called for each memory chunk.
1035  * @param mem_cb_arg
1036  *   An opaque pointer passed to the callback function.
1037  * @return
1038  *   Number of memory chunks iterated.
1039  */
1040 uint32_t rte_mempool_mem_iter(struct rte_mempool *mp,
1041         rte_mempool_mem_cb_t *mem_cb, void *mem_cb_arg);
1042
1043 /**
1044  * Dump the status of the mempool to a file.
1045  *
1046  * @param f
1047  *   A pointer to a file for output
1048  * @param mp
1049  *   A pointer to the mempool structure.
1050  */
1051 void rte_mempool_dump(FILE *f, struct rte_mempool *mp);
1052
1053 /**
1054  * Create a user-owned mempool cache.
1055  *
1056  * This can be used by non-EAL threads to enable caching when they
1057  * interact with a mempool.
1058  *
1059  * @param size
1060  *   The size of the mempool cache. See rte_mempool_create()'s cache_size
1061  *   parameter description for more information. The same limits and
1062  *   considerations apply here too.
1063  * @param socket_id
1064  *   The socket identifier in the case of NUMA. The value can be
1065  *   SOCKET_ID_ANY if there is no NUMA constraint for the reserved zone.
1066  */
1067 struct rte_mempool_cache *
1068 rte_mempool_cache_create(uint32_t size, int socket_id);
1069
1070 /**
1071  * Free a user-owned mempool cache.
1072  *
1073  * @param cache
1074  *   A pointer to the mempool cache.
1075  */
1076 void
1077 rte_mempool_cache_free(struct rte_mempool_cache *cache);
1078
1079 /**
1080  * Flush a user-owned mempool cache to the specified mempool.
1081  *
1082  * @param cache
1083  *   A pointer to the mempool cache.
1084  * @param mp
1085  *   A pointer to the mempool.
1086  */
1087 static __rte_always_inline void
1088 rte_mempool_cache_flush(struct rte_mempool_cache *cache,
1089                         struct rte_mempool *mp)
1090 {
1091         rte_mempool_ops_enqueue_bulk(mp, cache->objs, cache->len);
1092         cache->len = 0;
1093 }
1094
1095 /**
1096  * Get a pointer to the per-lcore default mempool cache.
1097  *
1098  * @param mp
1099  *   A pointer to the mempool structure.
1100  * @param lcore_id
1101  *   The logical core id.
1102  * @return
1103  *   A pointer to the mempool cache or NULL if disabled or non-EAL thread.
1104  */
1105 static __rte_always_inline struct rte_mempool_cache *
1106 rte_mempool_default_cache(struct rte_mempool *mp, unsigned lcore_id)
1107 {
1108         if (mp->cache_size == 0)
1109                 return NULL;
1110
1111         if (lcore_id >= RTE_MAX_LCORE)
1112                 return NULL;
1113
1114         return &mp->local_cache[lcore_id];
1115 }
1116
1117 /**
1118  * @internal Put several objects back in the mempool; used internally.
1119  * @param mp
1120  *   A pointer to the mempool structure.
1121  * @param obj_table
1122  *   A pointer to a table of void * pointers (objects).
1123  * @param n
1124  *   The number of objects to store back in the mempool, must be strictly
1125  *   positive.
1126  * @param cache
1127  *   A pointer to a mempool cache structure. May be NULL if not needed.
1128  */
1129 static __rte_always_inline void
1130 __mempool_generic_put(struct rte_mempool *mp, void * const *obj_table,
1131                       unsigned int n, struct rte_mempool_cache *cache)
1132 {
1133         void **cache_objs;
1134
1135         /* increment stat now, adding in mempool always success */
1136         __MEMPOOL_STAT_ADD(mp, put, n);
1137
1138         /* No cache provided or if put would overflow mem allocated for cache */
1139         if (unlikely(cache == NULL || n > RTE_MEMPOOL_CACHE_MAX_SIZE))
1140                 goto ring_enqueue;
1141
1142         cache_objs = &cache->objs[cache->len];
1143
1144         /*
1145          * The cache follows the following algorithm
1146          *   1. Add the objects to the cache
1147          *   2. Anything greater than the cache min value (if it crosses the
1148          *   cache flush threshold) is flushed to the ring.
1149          */
1150
1151         /* Add elements back into the cache */
1152         rte_memcpy(&cache_objs[0], obj_table, sizeof(void *) * n);
1153
1154         cache->len += n;
1155
1156         if (cache->len >= cache->flushthresh) {
1157                 rte_mempool_ops_enqueue_bulk(mp, &cache->objs[cache->size],
1158                                 cache->len - cache->size);
1159                 cache->len = cache->size;
1160         }
1161
1162         return;
1163
1164 ring_enqueue:
1165
1166         /* push remaining objects in ring */
1167 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
1168         if (rte_mempool_ops_enqueue_bulk(mp, obj_table, n) < 0)
1169                 rte_panic("cannot put objects in mempool\n");
1170 #else
1171         rte_mempool_ops_enqueue_bulk(mp, obj_table, n);
1172 #endif
1173 }
1174
1175
1176 /**
1177  * Put several objects back in the mempool.
1178  *
1179  * @param mp
1180  *   A pointer to the mempool structure.
1181  * @param obj_table
1182  *   A pointer to a table of void * pointers (objects).
1183  * @param n
1184  *   The number of objects to add in the mempool from the obj_table.
1185  * @param cache
1186  *   A pointer to a mempool cache structure. May be NULL if not needed.
1187  */
1188 static __rte_always_inline void
1189 rte_mempool_generic_put(struct rte_mempool *mp, void * const *obj_table,
1190                         unsigned int n, struct rte_mempool_cache *cache)
1191 {
1192         __mempool_check_cookies(mp, obj_table, n, 0);
1193         __mempool_generic_put(mp, obj_table, n, cache);
1194 }
1195
1196 /**
1197  * Put several objects back in the mempool.
1198  *
1199  * This function calls the multi-producer or the single-producer
1200  * version depending on the default behavior that was specified at
1201  * mempool creation time (see flags).
1202  *
1203  * @param mp
1204  *   A pointer to the mempool structure.
1205  * @param obj_table
1206  *   A pointer to a table of void * pointers (objects).
1207  * @param n
1208  *   The number of objects to add in the mempool from obj_table.
1209  */
1210 static __rte_always_inline void
1211 rte_mempool_put_bulk(struct rte_mempool *mp, void * const *obj_table,
1212                      unsigned int n)
1213 {
1214         struct rte_mempool_cache *cache;
1215         cache = rte_mempool_default_cache(mp, rte_lcore_id());
1216         rte_mempool_generic_put(mp, obj_table, n, cache);
1217 }
1218
1219 /**
1220  * Put one object back in the mempool.
1221  *
1222  * This function calls the multi-producer or the single-producer
1223  * version depending on the default behavior that was specified at
1224  * mempool creation time (see flags).
1225  *
1226  * @param mp
1227  *   A pointer to the mempool structure.
1228  * @param obj
1229  *   A pointer to the object to be added.
1230  */
1231 static __rte_always_inline void
1232 rte_mempool_put(struct rte_mempool *mp, void *obj)
1233 {
1234         rte_mempool_put_bulk(mp, &obj, 1);
1235 }
1236
1237 /**
1238  * @internal Get several objects from the mempool; used internally.
1239  * @param mp
1240  *   A pointer to the mempool structure.
1241  * @param obj_table
1242  *   A pointer to a table of void * pointers (objects).
1243  * @param n
1244  *   The number of objects to get, must be strictly positive.
1245  * @param cache
1246  *   A pointer to a mempool cache structure. May be NULL if not needed.
1247  * @return
1248  *   - >=0: Success; number of objects supplied.
1249  *   - <0: Error; code of ring dequeue function.
1250  */
1251 static __rte_always_inline int
1252 __mempool_generic_get(struct rte_mempool *mp, void **obj_table,
1253                       unsigned int n, struct rte_mempool_cache *cache)
1254 {
1255         int ret;
1256         uint32_t index, len;
1257         void **cache_objs;
1258
1259         /* No cache provided or cannot be satisfied from cache */
1260         if (unlikely(cache == NULL || n >= cache->size))
1261                 goto ring_dequeue;
1262
1263         cache_objs = cache->objs;
1264
1265         /* Can this be satisfied from the cache? */
1266         if (cache->len < n) {
1267                 /* No. Backfill the cache first, and then fill from it */
1268                 uint32_t req = n + (cache->size - cache->len);
1269
1270                 /* How many do we require i.e. number to fill the cache + the request */
1271                 ret = rte_mempool_ops_dequeue_bulk(mp,
1272                         &cache->objs[cache->len], req);
1273                 if (unlikely(ret < 0)) {
1274                         /*
1275                          * In the offchance that we are buffer constrained,
1276                          * where we are not able to allocate cache + n, go to
1277                          * the ring directly. If that fails, we are truly out of
1278                          * buffers.
1279                          */
1280                         goto ring_dequeue;
1281                 }
1282
1283                 cache->len += req;
1284         }
1285
1286         /* Now fill in the response ... */
1287         for (index = 0, len = cache->len - 1; index < n; ++index, len--, obj_table++)
1288                 *obj_table = cache_objs[len];
1289
1290         cache->len -= n;
1291
1292         __MEMPOOL_STAT_ADD(mp, get_success, n);
1293
1294         return 0;
1295
1296 ring_dequeue:
1297
1298         /* get remaining objects from ring */
1299         ret = rte_mempool_ops_dequeue_bulk(mp, obj_table, n);
1300
1301         if (ret < 0)
1302                 __MEMPOOL_STAT_ADD(mp, get_fail, n);
1303         else
1304                 __MEMPOOL_STAT_ADD(mp, get_success, n);
1305
1306         return ret;
1307 }
1308
1309 /**
1310  * Get several objects from the mempool.
1311  *
1312  * If cache is enabled, objects will be retrieved first from cache,
1313  * subsequently from the common pool. Note that it can return -ENOENT when
1314  * the local cache and common pool are empty, even if cache from other
1315  * lcores are full.
1316  *
1317  * @param mp
1318  *   A pointer to the mempool structure.
1319  * @param obj_table
1320  *   A pointer to a table of void * pointers (objects) that will be filled.
1321  * @param n
1322  *   The number of objects to get from mempool to obj_table.
1323  * @param cache
1324  *   A pointer to a mempool cache structure. May be NULL if not needed.
1325  * @return
1326  *   - 0: Success; objects taken.
1327  *   - -ENOENT: Not enough entries in the mempool; no object is retrieved.
1328  */
1329 static __rte_always_inline int
1330 rte_mempool_generic_get(struct rte_mempool *mp, void **obj_table,
1331                         unsigned int n, struct rte_mempool_cache *cache)
1332 {
1333         int ret;
1334         ret = __mempool_generic_get(mp, obj_table, n, cache);
1335         if (ret == 0)
1336                 __mempool_check_cookies(mp, obj_table, n, 1);
1337         return ret;
1338 }
1339
1340 /**
1341  * Get several objects from the mempool.
1342  *
1343  * This function calls the multi-consumers or the single-consumer
1344  * version, depending on the default behaviour that was specified at
1345  * mempool creation time (see flags).
1346  *
1347  * If cache is enabled, objects will be retrieved first from cache,
1348  * subsequently from the common pool. Note that it can return -ENOENT when
1349  * the local cache and common pool are empty, even if cache from other
1350  * lcores are full.
1351  *
1352  * @param mp
1353  *   A pointer to the mempool structure.
1354  * @param obj_table
1355  *   A pointer to a table of void * pointers (objects) that will be filled.
1356  * @param n
1357  *   The number of objects to get from the mempool to obj_table.
1358  * @return
1359  *   - 0: Success; objects taken
1360  *   - -ENOENT: Not enough entries in the mempool; no object is retrieved.
1361  */
1362 static __rte_always_inline int
1363 rte_mempool_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned int n)
1364 {
1365         struct rte_mempool_cache *cache;
1366         cache = rte_mempool_default_cache(mp, rte_lcore_id());
1367         return rte_mempool_generic_get(mp, obj_table, n, cache);
1368 }
1369
1370 /**
1371  * Get one object from the mempool.
1372  *
1373  * This function calls the multi-consumers or the single-consumer
1374  * version, depending on the default behavior that was specified at
1375  * mempool creation (see flags).
1376  *
1377  * If cache is enabled, objects will be retrieved first from cache,
1378  * subsequently from the common pool. Note that it can return -ENOENT when
1379  * the local cache and common pool are empty, even if cache from other
1380  * lcores are full.
1381  *
1382  * @param mp
1383  *   A pointer to the mempool structure.
1384  * @param obj_p
1385  *   A pointer to a void * pointer (object) that will be filled.
1386  * @return
1387  *   - 0: Success; objects taken.
1388  *   - -ENOENT: Not enough entries in the mempool; no object is retrieved.
1389  */
1390 static __rte_always_inline int
1391 rte_mempool_get(struct rte_mempool *mp, void **obj_p)
1392 {
1393         return rte_mempool_get_bulk(mp, obj_p, 1);
1394 }
1395
1396 /**
1397  * Return the number of entries in the mempool.
1398  *
1399  * When cache is enabled, this function has to browse the length of
1400  * all lcores, so it should not be used in a data path, but only for
1401  * debug purposes. User-owned mempool caches are not accounted for.
1402  *
1403  * @param mp
1404  *   A pointer to the mempool structure.
1405  * @return
1406  *   The number of entries in the mempool.
1407  */
1408 unsigned int rte_mempool_avail_count(const struct rte_mempool *mp);
1409
1410 /**
1411  * Return the number of elements which have been allocated from the mempool
1412  *
1413  * When cache is enabled, this function has to browse the length of
1414  * all lcores, so it should not be used in a data path, but only for
1415  * debug purposes.
1416  *
1417  * @param mp
1418  *   A pointer to the mempool structure.
1419  * @return
1420  *   The number of free entries in the mempool.
1421  */
1422 unsigned int
1423 rte_mempool_in_use_count(const struct rte_mempool *mp);
1424
1425 /**
1426  * Test if the mempool is full.
1427  *
1428  * When cache is enabled, this function has to browse the length of all
1429  * lcores, so it should not be used in a data path, but only for debug
1430  * purposes. User-owned mempool caches are not accounted for.
1431  *
1432  * @param mp
1433  *   A pointer to the mempool structure.
1434  * @return
1435  *   - 1: The mempool is full.
1436  *   - 0: The mempool is not full.
1437  */
1438 static inline int
1439 rte_mempool_full(const struct rte_mempool *mp)
1440 {
1441         return !!(rte_mempool_avail_count(mp) == mp->size);
1442 }
1443
1444 /**
1445  * Test if the mempool is empty.
1446  *
1447  * When cache is enabled, this function has to browse the length of all
1448  * lcores, so it should not be used in a data path, but only for debug
1449  * purposes. User-owned mempool caches are not accounted for.
1450  *
1451  * @param mp
1452  *   A pointer to the mempool structure.
1453  * @return
1454  *   - 1: The mempool is empty.
1455  *   - 0: The mempool is not empty.
1456  */
1457 static inline int
1458 rte_mempool_empty(const struct rte_mempool *mp)
1459 {
1460         return !!(rte_mempool_avail_count(mp) == 0);
1461 }
1462
1463 /**
1464  * Return the IO address of elt, which is an element of the pool mp.
1465  *
1466  * @param elt
1467  *   A pointer (virtual address) to the element of the pool.
1468  * @return
1469  *   The IO address of the elt element.
1470  *   If the mempool was created with MEMPOOL_F_NO_PHYS_CONTIG, the
1471  *   returned value is RTE_BAD_IOVA.
1472  */
1473 static inline rte_iova_t
1474 rte_mempool_virt2iova(const void *elt)
1475 {
1476         const struct rte_mempool_objhdr *hdr;
1477         hdr = (const struct rte_mempool_objhdr *)RTE_PTR_SUB(elt,
1478                 sizeof(*hdr));
1479         return hdr->iova;
1480 }
1481
1482 __rte_deprecated
1483 static inline phys_addr_t
1484 rte_mempool_virt2phy(__rte_unused const struct rte_mempool *mp, const void *elt)
1485 {
1486         return rte_mempool_virt2iova(elt);
1487 }
1488
1489 /**
1490  * Check the consistency of mempool objects.
1491  *
1492  * Verify the coherency of fields in the mempool structure. Also check
1493  * that the cookies of mempool objects (even the ones that are not
1494  * present in pool) have a correct value. If not, a panic will occur.
1495  *
1496  * @param mp
1497  *   A pointer to the mempool structure.
1498  */
1499 void rte_mempool_audit(struct rte_mempool *mp);
1500
1501 /**
1502  * Return a pointer to the private data in an mempool structure.
1503  *
1504  * @param mp
1505  *   A pointer to the mempool structure.
1506  * @return
1507  *   A pointer to the private data.
1508  */
1509 static inline void *rte_mempool_get_priv(struct rte_mempool *mp)
1510 {
1511         return (char *)mp +
1512                 MEMPOOL_HEADER_SIZE(mp, mp->cache_size);
1513 }
1514
1515 /**
1516  * Dump the status of all mempools on the console
1517  *
1518  * @param f
1519  *   A pointer to a file for output
1520  */
1521 void rte_mempool_list_dump(FILE *f);
1522
1523 /**
1524  * Search a mempool from its name
1525  *
1526  * @param name
1527  *   The name of the mempool.
1528  * @return
1529  *   The pointer to the mempool matching the name, or NULL if not found.
1530  *   NULL on error
1531  *   with rte_errno set appropriately. Possible rte_errno values include:
1532  *    - ENOENT - required entry not available to return.
1533  *
1534  */
1535 struct rte_mempool *rte_mempool_lookup(const char *name);
1536
1537 /**
1538  * Get the header, trailer and total size of a mempool element.
1539  *
1540  * Given a desired size of the mempool element and mempool flags,
1541  * calculates header, trailer, body and total sizes of the mempool object.
1542  *
1543  * @param elt_size
1544  *   The size of each element, without header and trailer.
1545  * @param flags
1546  *   The flags used for the mempool creation.
1547  *   Consult rte_mempool_create() for more information about possible values.
1548  *   The size of each element.
1549  * @param sz
1550  *   The calculated detailed size the mempool object. May be NULL.
1551  * @return
1552  *   Total size of the mempool object.
1553  */
1554 uint32_t rte_mempool_calc_obj_size(uint32_t elt_size, uint32_t flags,
1555         struct rte_mempool_objsz *sz);
1556
1557 /**
1558  * Get the size of memory required to store mempool elements.
1559  *
1560  * Calculate the maximum amount of memory required to store given number
1561  * of objects. Assume that the memory buffer will be aligned at page
1562  * boundary.
1563  *
1564  * Note that if object size is bigger then page size, then it assumes
1565  * that pages are grouped in subsets of physically continuous pages big
1566  * enough to store at least one object.
1567  *
1568  * @param elt_num
1569  *   Number of elements.
1570  * @param total_elt_sz
1571  *   The size of each element, including header and trailer, as returned
1572  *   by rte_mempool_calc_obj_size().
1573  * @param pg_shift
1574  *   LOG2 of the physical pages size. If set to 0, ignore page boundaries.
1575  * @param flags
1576  *  The mempool flags.
1577  * @return
1578  *   Required memory size aligned at page boundary.
1579  */
1580 size_t rte_mempool_xmem_size(uint32_t elt_num, size_t total_elt_sz,
1581         uint32_t pg_shift, unsigned int flags);
1582
1583 /**
1584  * Get the size of memory required to store mempool elements.
1585  *
1586  * Calculate how much memory would be actually required with the given
1587  * memory footprint to store required number of objects.
1588  *
1589  * @param vaddr
1590  *   Virtual address of the externally allocated memory buffer.
1591  *   Will be used to store mempool objects.
1592  * @param elt_num
1593  *   Number of elements.
1594  * @param total_elt_sz
1595  *   The size of each element, including header and trailer, as returned
1596  *   by rte_mempool_calc_obj_size().
1597  * @param iova
1598  *   Array of IO addresses of the pages that comprises given memory buffer.
1599  * @param pg_num
1600  *   Number of elements in the iova array.
1601  * @param pg_shift
1602  *   LOG2 of the physical pages size.
1603  * @param flags
1604  *  The mempool flags.
1605  * @return
1606  *   On success, the number of bytes needed to store given number of
1607  *   objects, aligned to the given page size. If the provided memory
1608  *   buffer is too small, return a negative value whose absolute value
1609  *   is the actual number of elements that can be stored in that buffer.
1610  */
1611 ssize_t rte_mempool_xmem_usage(void *vaddr, uint32_t elt_num,
1612         size_t total_elt_sz, const rte_iova_t iova[], uint32_t pg_num,
1613         uint32_t pg_shift, unsigned int flags);
1614
1615 /**
1616  * Walk list of all memory pools
1617  *
1618  * @param func
1619  *   Iterator function
1620  * @param arg
1621  *   Argument passed to iterator
1622  */
1623 void rte_mempool_walk(void (*func)(struct rte_mempool *, void *arg),
1624                       void *arg);
1625
1626 #ifdef __cplusplus
1627 }
1628 #endif
1629
1630 #endif /* _RTE_MEMPOOL_H_ */