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