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