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