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