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