mempool: support handler operations
[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 is_mp
957  *   Mono-producer (0) or multi-producers (1).
958  */
959 static inline void __attribute__((always_inline))
960 __mempool_put_bulk(struct rte_mempool *mp, void * const *obj_table,
961                     unsigned n, int is_mp)
962 {
963         struct rte_mempool_cache *cache;
964         uint32_t index;
965         void **cache_objs;
966         unsigned lcore_id = rte_lcore_id();
967         uint32_t cache_size = mp->cache_size;
968         uint32_t flushthresh = mp->cache_flushthresh;
969
970         /* increment stat now, adding in mempool always success */
971         __MEMPOOL_STAT_ADD(mp, put, n);
972
973         /* cache is not enabled or single producer or non-EAL thread */
974         if (unlikely(cache_size == 0 || is_mp == 0 ||
975                      lcore_id >= RTE_MAX_LCORE))
976                 goto ring_enqueue;
977
978         /* Go straight to ring if put would overflow mem allocated for cache */
979         if (unlikely(n > RTE_MEMPOOL_CACHE_MAX_SIZE))
980                 goto ring_enqueue;
981
982         cache = &mp->local_cache[lcore_id];
983         cache_objs = &cache->objs[cache->len];
984
985         /*
986          * The cache follows the following algorithm
987          *   1. Add the objects to the cache
988          *   2. Anything greater than the cache min value (if it crosses the
989          *   cache flush threshold) is flushed to the ring.
990          */
991
992         /* Add elements back into the cache */
993         for (index = 0; index < n; ++index, obj_table++)
994                 cache_objs[index] = *obj_table;
995
996         cache->len += n;
997
998         if (cache->len >= flushthresh) {
999                 rte_mempool_ops_enqueue_bulk(mp, &cache->objs[cache_size],
1000                                 cache->len - cache_size);
1001                 cache->len = cache_size;
1002         }
1003
1004         return;
1005
1006 ring_enqueue:
1007
1008         /* push remaining objects in ring */
1009 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
1010         if (rte_mempool_ops_enqueue_bulk(mp, obj_table, n) < 0)
1011                 rte_panic("cannot put objects in mempool\n");
1012 #else
1013         rte_mempool_ops_enqueue_bulk(mp, obj_table, n);
1014 #endif
1015 }
1016
1017
1018 /**
1019  * Put several objects back in the mempool (multi-producers safe).
1020  *
1021  * @param mp
1022  *   A pointer to the mempool structure.
1023  * @param obj_table
1024  *   A pointer to a table of void * pointers (objects).
1025  * @param n
1026  *   The number of objects to add in the mempool from the obj_table.
1027  */
1028 static inline void __attribute__((always_inline))
1029 rte_mempool_mp_put_bulk(struct rte_mempool *mp, void * const *obj_table,
1030                         unsigned n)
1031 {
1032         __mempool_check_cookies(mp, obj_table, n, 0);
1033         __mempool_put_bulk(mp, obj_table, n, 1);
1034 }
1035
1036 /**
1037  * Put several objects back in the mempool (NOT multi-producers safe).
1038  *
1039  * @param mp
1040  *   A pointer to the mempool structure.
1041  * @param obj_table
1042  *   A pointer to a table of void * pointers (objects).
1043  * @param n
1044  *   The number of objects to add in the mempool from obj_table.
1045  */
1046 static inline void
1047 rte_mempool_sp_put_bulk(struct rte_mempool *mp, void * const *obj_table,
1048                         unsigned n)
1049 {
1050         __mempool_check_cookies(mp, obj_table, n, 0);
1051         __mempool_put_bulk(mp, obj_table, n, 0);
1052 }
1053
1054 /**
1055  * Put several objects back in the mempool.
1056  *
1057  * This function calls the multi-producer or the single-producer
1058  * version depending on the default behavior that was specified at
1059  * mempool creation time (see flags).
1060  *
1061  * @param mp
1062  *   A pointer to the mempool structure.
1063  * @param obj_table
1064  *   A pointer to a table of void * pointers (objects).
1065  * @param n
1066  *   The number of objects to add in the mempool from obj_table.
1067  */
1068 static inline void __attribute__((always_inline))
1069 rte_mempool_put_bulk(struct rte_mempool *mp, void * const *obj_table,
1070                      unsigned n)
1071 {
1072         __mempool_check_cookies(mp, obj_table, n, 0);
1073         __mempool_put_bulk(mp, obj_table, n, !(mp->flags & MEMPOOL_F_SP_PUT));
1074 }
1075
1076 /**
1077  * Put one object in the mempool (multi-producers safe).
1078  *
1079  * @param mp
1080  *   A pointer to the mempool structure.
1081  * @param obj
1082  *   A pointer to the object to be added.
1083  */
1084 static inline void __attribute__((always_inline))
1085 rte_mempool_mp_put(struct rte_mempool *mp, void *obj)
1086 {
1087         rte_mempool_mp_put_bulk(mp, &obj, 1);
1088 }
1089
1090 /**
1091  * Put one object back in the mempool (NOT multi-producers safe).
1092  *
1093  * @param mp
1094  *   A pointer to the mempool structure.
1095  * @param obj
1096  *   A pointer to the object to be added.
1097  */
1098 static inline void __attribute__((always_inline))
1099 rte_mempool_sp_put(struct rte_mempool *mp, void *obj)
1100 {
1101         rte_mempool_sp_put_bulk(mp, &obj, 1);
1102 }
1103
1104 /**
1105  * Put one object back in the mempool.
1106  *
1107  * This function calls the multi-producer or the single-producer
1108  * version depending on the default behavior that was specified at
1109  * mempool creation time (see flags).
1110  *
1111  * @param mp
1112  *   A pointer to the mempool structure.
1113  * @param obj
1114  *   A pointer to the object to be added.
1115  */
1116 static inline void __attribute__((always_inline))
1117 rte_mempool_put(struct rte_mempool *mp, void *obj)
1118 {
1119         rte_mempool_put_bulk(mp, &obj, 1);
1120 }
1121
1122 /**
1123  * @internal Get several objects from the mempool; used internally.
1124  * @param mp
1125  *   A pointer to the mempool structure.
1126  * @param obj_table
1127  *   A pointer to a table of void * pointers (objects).
1128  * @param n
1129  *   The number of objects to get, must be strictly positive.
1130  * @param is_mc
1131  *   Mono-consumer (0) or multi-consumers (1).
1132  * @return
1133  *   - >=0: Success; number of objects supplied.
1134  *   - <0: Error; code of ring dequeue function.
1135  */
1136 static inline int __attribute__((always_inline))
1137 __mempool_get_bulk(struct rte_mempool *mp, void **obj_table,
1138                    unsigned n, int is_mc)
1139 {
1140         int ret;
1141         struct rte_mempool_cache *cache;
1142         uint32_t index, len;
1143         void **cache_objs;
1144         unsigned lcore_id = rte_lcore_id();
1145         uint32_t cache_size = mp->cache_size;
1146
1147         /* cache is not enabled or single consumer */
1148         if (unlikely(cache_size == 0 || is_mc == 0 ||
1149                      n >= cache_size || lcore_id >= RTE_MAX_LCORE))
1150                 goto ring_dequeue;
1151
1152         cache = &mp->local_cache[lcore_id];
1153         cache_objs = cache->objs;
1154
1155         /* Can this be satisfied from the cache? */
1156         if (cache->len < n) {
1157                 /* No. Backfill the cache first, and then fill from it */
1158                 uint32_t req = n + (cache_size - cache->len);
1159
1160                 /* How many do we require i.e. number to fill the cache + the request */
1161                 ret = rte_mempool_ops_dequeue_bulk(mp,
1162                         &cache->objs[cache->len], req);
1163                 if (unlikely(ret < 0)) {
1164                         /*
1165                          * In the offchance that we are buffer constrained,
1166                          * where we are not able to allocate cache + n, go to
1167                          * the ring directly. If that fails, we are truly out of
1168                          * buffers.
1169                          */
1170                         goto ring_dequeue;
1171                 }
1172
1173                 cache->len += req;
1174         }
1175
1176         /* Now fill in the response ... */
1177         for (index = 0, len = cache->len - 1; index < n; ++index, len--, obj_table++)
1178                 *obj_table = cache_objs[len];
1179
1180         cache->len -= n;
1181
1182         __MEMPOOL_STAT_ADD(mp, get_success, n);
1183
1184         return 0;
1185
1186 ring_dequeue:
1187
1188         /* get remaining objects from ring */
1189         ret = rte_mempool_ops_dequeue_bulk(mp, obj_table, n);
1190
1191         if (ret < 0)
1192                 __MEMPOOL_STAT_ADD(mp, get_fail, n);
1193         else
1194                 __MEMPOOL_STAT_ADD(mp, get_success, n);
1195
1196         return ret;
1197 }
1198
1199 /**
1200  * Get several objects from the mempool (multi-consumers safe).
1201  *
1202  * If cache is enabled, objects will be retrieved first from cache,
1203  * subsequently from the common pool. Note that it can return -ENOENT when
1204  * the local cache and common pool are empty, even if cache from other
1205  * lcores are full.
1206  *
1207  * @param mp
1208  *   A pointer to the mempool structure.
1209  * @param obj_table
1210  *   A pointer to a table of void * pointers (objects) that will be filled.
1211  * @param n
1212  *   The number of objects to get from mempool to obj_table.
1213  * @return
1214  *   - 0: Success; objects taken.
1215  *   - -ENOENT: Not enough entries in the mempool; no object is retrieved.
1216  */
1217 static inline int __attribute__((always_inline))
1218 rte_mempool_mc_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned n)
1219 {
1220         int ret;
1221         ret = __mempool_get_bulk(mp, obj_table, n, 1);
1222         if (ret == 0)
1223                 __mempool_check_cookies(mp, obj_table, n, 1);
1224         return ret;
1225 }
1226
1227 /**
1228  * Get several objects from the mempool (NOT multi-consumers safe).
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 the mempool to obj_table.
1241  * @return
1242  *   - 0: Success; objects taken.
1243  *   - -ENOENT: Not enough entries in the mempool; no object is
1244  *     retrieved.
1245  */
1246 static inline int __attribute__((always_inline))
1247 rte_mempool_sc_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned n)
1248 {
1249         int ret;
1250         ret = __mempool_get_bulk(mp, obj_table, n, 0);
1251         if (ret == 0)
1252                 __mempool_check_cookies(mp, obj_table, n, 1);
1253         return ret;
1254 }
1255
1256 /**
1257  * Get several objects from the mempool.
1258  *
1259  * This function calls the multi-consumers or the single-consumer
1260  * version, depending on the default behaviour that was specified at
1261  * mempool creation time (see flags).
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 the 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 static inline int __attribute__((always_inline))
1279 rte_mempool_get_bulk(struct rte_mempool *mp, void **obj_table, unsigned n)
1280 {
1281         int ret;
1282         ret = __mempool_get_bulk(mp, obj_table, n,
1283                                  !(mp->flags & MEMPOOL_F_SC_GET));
1284         if (ret == 0)
1285                 __mempool_check_cookies(mp, obj_table, n, 1);
1286         return ret;
1287 }
1288
1289 /**
1290  * Get one object from the mempool (multi-consumers safe).
1291  *
1292  * If cache is enabled, objects will be retrieved first from cache,
1293  * subsequently from the common pool. Note that it can return -ENOENT when
1294  * the local cache and common pool are empty, even if cache from other
1295  * lcores are full.
1296  *
1297  * @param mp
1298  *   A pointer to the mempool structure.
1299  * @param obj_p
1300  *   A pointer to a void * pointer (object) that will be filled.
1301  * @return
1302  *   - 0: Success; objects taken.
1303  *   - -ENOENT: Not enough entries in the mempool; no object is retrieved.
1304  */
1305 static inline int __attribute__((always_inline))
1306 rte_mempool_mc_get(struct rte_mempool *mp, void **obj_p)
1307 {
1308         return rte_mempool_mc_get_bulk(mp, obj_p, 1);
1309 }
1310
1311 /**
1312  * Get one object from the mempool (NOT multi-consumers safe).
1313  *
1314  * If cache is enabled, objects will be retrieved first from cache,
1315  * subsequently from the common pool. Note that it can return -ENOENT when
1316  * the local cache and common pool are empty, even if cache from other
1317  * lcores are full.
1318  *
1319  * @param mp
1320  *   A pointer to the mempool structure.
1321  * @param obj_p
1322  *   A pointer to a void * pointer (object) that will be filled.
1323  * @return
1324  *   - 0: Success; objects taken.
1325  *   - -ENOENT: Not enough entries in the mempool; no object is retrieved.
1326  */
1327 static inline int __attribute__((always_inline))
1328 rte_mempool_sc_get(struct rte_mempool *mp, void **obj_p)
1329 {
1330         return rte_mempool_sc_get_bulk(mp, obj_p, 1);
1331 }
1332
1333 /**
1334  * Get one object from the mempool.
1335  *
1336  * This function calls the multi-consumers or the single-consumer
1337  * version, depending on the default behavior that was specified at
1338  * mempool creation (see flags).
1339  *
1340  * If cache is enabled, objects will be retrieved first from cache,
1341  * subsequently from the common pool. Note that it can return -ENOENT when
1342  * the local cache and common pool are empty, even if cache from other
1343  * lcores are full.
1344  *
1345  * @param mp
1346  *   A pointer to the mempool structure.
1347  * @param obj_p
1348  *   A pointer to a void * pointer (object) that will be filled.
1349  * @return
1350  *   - 0: Success; objects taken.
1351  *   - -ENOENT: Not enough entries in the mempool; no object is retrieved.
1352  */
1353 static inline int __attribute__((always_inline))
1354 rte_mempool_get(struct rte_mempool *mp, void **obj_p)
1355 {
1356         return rte_mempool_get_bulk(mp, obj_p, 1);
1357 }
1358
1359 /**
1360  * Return the number of entries in the mempool.
1361  *
1362  * When cache is enabled, this function has to browse the length of
1363  * all lcores, so it should not be used in a data path, but only for
1364  * debug purposes.
1365  *
1366  * @param mp
1367  *   A pointer to the mempool structure.
1368  * @return
1369  *   The number of entries in the mempool.
1370  */
1371 unsigned rte_mempool_count(const struct rte_mempool *mp);
1372
1373 /**
1374  * Return the number of free entries in the mempool ring.
1375  * i.e. how many entries can be freed back to the mempool.
1376  *
1377  * NOTE: This corresponds to the number of elements *allocated* from the
1378  * memory pool, not the number of elements in the pool itself. To count
1379  * the number elements currently available in the pool, use "rte_mempool_count"
1380  *
1381  * When cache is enabled, this function has to browse the length of
1382  * all lcores, so it should not be used in a data path, but only for
1383  * debug purposes.
1384  *
1385  * @param mp
1386  *   A pointer to the mempool structure.
1387  * @return
1388  *   The number of free entries in the mempool.
1389  */
1390 static inline unsigned
1391 rte_mempool_free_count(const struct rte_mempool *mp)
1392 {
1393         return mp->size - rte_mempool_count(mp);
1394 }
1395
1396 /**
1397  * Test if the mempool is full.
1398  *
1399  * When cache is enabled, this function has to browse the length of all
1400  * lcores, so it should not be used in a data path, but only for debug
1401  * purposes.
1402  *
1403  * @param mp
1404  *   A pointer to the mempool structure.
1405  * @return
1406  *   - 1: The mempool is full.
1407  *   - 0: The mempool is not full.
1408  */
1409 static inline int
1410 rte_mempool_full(const struct rte_mempool *mp)
1411 {
1412         return !!(rte_mempool_count(mp) == mp->size);
1413 }
1414
1415 /**
1416  * Test if the mempool is empty.
1417  *
1418  * When cache is enabled, this function has to browse the length of all
1419  * lcores, so it should not be used in a data path, but only for debug
1420  * purposes.
1421  *
1422  * @param mp
1423  *   A pointer to the mempool structure.
1424  * @return
1425  *   - 1: The mempool is empty.
1426  *   - 0: The mempool is not empty.
1427  */
1428 static inline int
1429 rte_mempool_empty(const struct rte_mempool *mp)
1430 {
1431         return !!(rte_mempool_count(mp) == 0);
1432 }
1433
1434 /**
1435  * Return the physical address of elt, which is an element of the pool mp.
1436  *
1437  * @param mp
1438  *   A pointer to the mempool structure.
1439  * @param elt
1440  *   A pointer (virtual address) to the element of the pool.
1441  * @return
1442  *   The physical address of the elt element.
1443  *   If the mempool was created with MEMPOOL_F_NO_PHYS_CONTIG, the
1444  *   returned value is RTE_BAD_PHYS_ADDR.
1445  */
1446 static inline phys_addr_t
1447 rte_mempool_virt2phy(__rte_unused const struct rte_mempool *mp, const void *elt)
1448 {
1449         const struct rte_mempool_objhdr *hdr;
1450         hdr = (const struct rte_mempool_objhdr *)RTE_PTR_SUB(elt,
1451                 sizeof(*hdr));
1452         return hdr->physaddr;
1453 }
1454
1455 /**
1456  * Check the consistency of mempool objects.
1457  *
1458  * Verify the coherency of fields in the mempool structure. Also check
1459  * that the cookies of mempool objects (even the ones that are not
1460  * present in pool) have a correct value. If not, a panic will occur.
1461  *
1462  * @param mp
1463  *   A pointer to the mempool structure.
1464  */
1465 void rte_mempool_audit(struct rte_mempool *mp);
1466
1467 /**
1468  * Return a pointer to the private data in an mempool structure.
1469  *
1470  * @param mp
1471  *   A pointer to the mempool structure.
1472  * @return
1473  *   A pointer to the private data.
1474  */
1475 static inline void *rte_mempool_get_priv(struct rte_mempool *mp)
1476 {
1477         return (char *)mp +
1478                 MEMPOOL_HEADER_SIZE(mp, mp->cache_size);
1479 }
1480
1481 /**
1482  * Dump the status of all mempools on the console
1483  *
1484  * @param f
1485  *   A pointer to a file for output
1486  */
1487 void rte_mempool_list_dump(FILE *f);
1488
1489 /**
1490  * Search a mempool from its name
1491  *
1492  * @param name
1493  *   The name of the mempool.
1494  * @return
1495  *   The pointer to the mempool matching the name, or NULL if not found.
1496  *   NULL on error
1497  *   with rte_errno set appropriately. Possible rte_errno values include:
1498  *    - ENOENT - required entry not available to return.
1499  *
1500  */
1501 struct rte_mempool *rte_mempool_lookup(const char *name);
1502
1503 /**
1504  * Get the header, trailer and total size of a mempool element.
1505  *
1506  * Given a desired size of the mempool element and mempool flags,
1507  * calculates header, trailer, body and total sizes of the mempool object.
1508  *
1509  * @param elt_size
1510  *   The size of each element, without header and trailer.
1511  * @param flags
1512  *   The flags used for the mempool creation.
1513  *   Consult rte_mempool_create() for more information about possible values.
1514  *   The size of each element.
1515  * @param sz
1516  *   The calculated detailed size the mempool object. May be NULL.
1517  * @return
1518  *   Total size of the mempool object.
1519  */
1520 uint32_t rte_mempool_calc_obj_size(uint32_t elt_size, uint32_t flags,
1521         struct rte_mempool_objsz *sz);
1522
1523 /**
1524  * Get the size of memory required to store mempool elements.
1525  *
1526  * Calculate the maximum amount of memory required to store given number
1527  * of objects. Assume that the memory buffer will be aligned at page
1528  * boundary.
1529  *
1530  * Note that if object size is bigger then page size, then it assumes
1531  * that pages are grouped in subsets of physically continuous pages big
1532  * enough to store at least one object.
1533  *
1534  * @param elt_num
1535  *   Number of elements.
1536  * @param total_elt_sz
1537  *   The size of each element, including header and trailer, as returned
1538  *   by rte_mempool_calc_obj_size().
1539  * @param pg_shift
1540  *   LOG2 of the physical pages size. If set to 0, ignore page boundaries.
1541  * @return
1542  *   Required memory size aligned at page boundary.
1543  */
1544 size_t rte_mempool_xmem_size(uint32_t elt_num, size_t total_elt_sz,
1545         uint32_t pg_shift);
1546
1547 /**
1548  * Get the size of memory required to store mempool elements.
1549  *
1550  * Calculate how much memory would be actually required with the given
1551  * memory footprint to store required number of objects.
1552  *
1553  * @param vaddr
1554  *   Virtual address of the externally allocated memory buffer.
1555  *   Will be used to store mempool objects.
1556  * @param elt_num
1557  *   Number of elements.
1558  * @param total_elt_sz
1559  *   The size of each element, including header and trailer, as returned
1560  *   by rte_mempool_calc_obj_size().
1561  * @param paddr
1562  *   Array of physical addresses of the pages that comprises given memory
1563  *   buffer.
1564  * @param pg_num
1565  *   Number of elements in the paddr array.
1566  * @param pg_shift
1567  *   LOG2 of the physical pages size.
1568  * @return
1569  *   On success, the number of bytes needed to store given number of
1570  *   objects, aligned to the given page size. If the provided memory
1571  *   buffer is too small, return a negative value whose absolute value
1572  *   is the actual number of elements that can be stored in that buffer.
1573  */
1574 ssize_t rte_mempool_xmem_usage(void *vaddr, uint32_t elt_num,
1575         size_t total_elt_sz, const phys_addr_t paddr[], uint32_t pg_num,
1576         uint32_t pg_shift);
1577
1578 /**
1579  * Walk list of all memory pools
1580  *
1581  * @param func
1582  *   Iterator function
1583  * @param arg
1584  *   Argument passed to iterator
1585  */
1586 void rte_mempool_walk(void (*func)(struct rte_mempool *, void *arg),
1587                       void *arg);
1588
1589 #ifdef __cplusplus
1590 }
1591 #endif
1592
1593 #endif /* _RTE_MEMPOOL_H_ */