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