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