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