mempool: remove const qualifier when browsing pools
[dpdk.git] / lib / librte_mempool / rte_mempool.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <stdio.h>
35 #include <string.h>
36 #include <stdint.h>
37 #include <stdarg.h>
38 #include <unistd.h>
39 #include <inttypes.h>
40 #include <errno.h>
41 #include <sys/queue.h>
42
43 #include <rte_common.h>
44 #include <rte_log.h>
45 #include <rte_debug.h>
46 #include <rte_memory.h>
47 #include <rte_memzone.h>
48 #include <rte_malloc.h>
49 #include <rte_atomic.h>
50 #include <rte_launch.h>
51 #include <rte_eal.h>
52 #include <rte_eal_memconfig.h>
53 #include <rte_per_lcore.h>
54 #include <rte_lcore.h>
55 #include <rte_branch_prediction.h>
56 #include <rte_ring.h>
57 #include <rte_errno.h>
58 #include <rte_string_fns.h>
59 #include <rte_spinlock.h>
60
61 #include "rte_mempool.h"
62
63 TAILQ_HEAD(rte_mempool_list, rte_tailq_entry);
64
65 static struct rte_tailq_elem rte_mempool_tailq = {
66         .name = "RTE_MEMPOOL",
67 };
68 EAL_REGISTER_TAILQ(rte_mempool_tailq)
69
70 #define CACHE_FLUSHTHRESH_MULTIPLIER 1.5
71 #define CALC_CACHE_FLUSHTHRESH(c)       \
72         ((typeof(c))((c) * CACHE_FLUSHTHRESH_MULTIPLIER))
73
74 /*
75  * return the greatest common divisor between a and b (fast algorithm)
76  *
77  */
78 static unsigned get_gcd(unsigned a, unsigned b)
79 {
80         unsigned c;
81
82         if (0 == a)
83                 return b;
84         if (0 == b)
85                 return a;
86
87         if (a < b) {
88                 c = a;
89                 a = b;
90                 b = c;
91         }
92
93         while (b != 0) {
94                 c = a % b;
95                 a = b;
96                 b = c;
97         }
98
99         return a;
100 }
101
102 /*
103  * Depending on memory configuration, objects addresses are spread
104  * between channels and ranks in RAM: the pool allocator will add
105  * padding between objects. This function return the new size of the
106  * object.
107  */
108 static unsigned optimize_object_size(unsigned obj_size)
109 {
110         unsigned nrank, nchan;
111         unsigned new_obj_size;
112
113         /* get number of channels */
114         nchan = rte_memory_get_nchannel();
115         if (nchan == 0)
116                 nchan = 4;
117
118         nrank = rte_memory_get_nrank();
119         if (nrank == 0)
120                 nrank = 1;
121
122         /* process new object size */
123         new_obj_size = (obj_size + RTE_MEMPOOL_ALIGN_MASK) / RTE_MEMPOOL_ALIGN;
124         while (get_gcd(new_obj_size, nrank * nchan) != 1)
125                 new_obj_size++;
126         return new_obj_size * RTE_MEMPOOL_ALIGN;
127 }
128
129 static void
130 mempool_add_elem(struct rte_mempool *mp, void *obj, uint32_t obj_idx,
131         rte_mempool_obj_cb_t *obj_init, void *obj_init_arg)
132 {
133         struct rte_mempool_objhdr *hdr;
134         struct rte_mempool_objtlr *tlr __rte_unused;
135
136         obj = (char *)obj + mp->header_size;
137
138         /* set mempool ptr in header */
139         hdr = RTE_PTR_SUB(obj, sizeof(*hdr));
140         hdr->mp = mp;
141         STAILQ_INSERT_TAIL(&mp->elt_list, hdr, next);
142
143 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
144         hdr->cookie = RTE_MEMPOOL_HEADER_COOKIE2;
145         tlr = __mempool_get_trailer(obj);
146         tlr->cookie = RTE_MEMPOOL_TRAILER_COOKIE;
147 #endif
148         /* call the initializer */
149         if (obj_init)
150                 obj_init(mp, obj_init_arg, obj, obj_idx);
151
152         /* enqueue in ring */
153         rte_ring_sp_enqueue(mp->ring, obj);
154 }
155
156 /* Iterate through objects at the given address
157  *
158  * Given the pointer to the memory, and its topology in physical memory
159  * (the physical addresses table), iterate through the "elt_num" objects
160  * of size "elt_sz" aligned at "align". For each object in this memory
161  * chunk, invoke a callback. It returns the effective number of objects
162  * in this memory.
163  */
164 uint32_t
165 rte_mempool_obj_iter(void *vaddr, uint32_t elt_num, size_t total_elt_sz,
166         size_t align, const phys_addr_t paddr[], uint32_t pg_num,
167         uint32_t pg_shift, rte_mempool_obj_iter_t obj_iter, void *obj_iter_arg)
168 {
169         uint32_t i, j, k;
170         uint32_t pgn, pgf;
171         uintptr_t end, start, va;
172         uintptr_t pg_sz;
173
174         pg_sz = (uintptr_t)1 << pg_shift;
175         va = (uintptr_t)vaddr;
176
177         i = 0;
178         j = 0;
179
180         while (i != elt_num && j != pg_num) {
181
182                 start = RTE_ALIGN_CEIL(va, align);
183                 end = start + total_elt_sz;
184
185                 /* index of the first page for the next element. */
186                 pgf = (end >> pg_shift) - (start >> pg_shift);
187
188                 /* index of the last page for the current element. */
189                 pgn = ((end - 1) >> pg_shift) - (start >> pg_shift);
190                 pgn += j;
191
192                 /* do we have enough space left for the element. */
193                 if (pgn >= pg_num)
194                         break;
195
196                 for (k = j;
197                                 k != pgn &&
198                                 paddr[k] + pg_sz == paddr[k + 1];
199                                 k++)
200                         ;
201
202                 /*
203                  * if next pgn chunks of memory physically continuous,
204                  * use it to create next element.
205                  * otherwise, just skip that chunk unused.
206                  */
207                 if (k == pgn) {
208                         if (obj_iter != NULL)
209                                 obj_iter(obj_iter_arg, (void *)start,
210                                         (void *)end, i);
211                         va = end;
212                         j += pgf;
213                         i++;
214                 } else {
215                         va = RTE_ALIGN_CEIL((va + 1), pg_sz);
216                         j++;
217                 }
218         }
219
220         return i;
221 }
222
223 /*
224  * Populate  mempool with the objects.
225  */
226
227 struct mempool_populate_arg {
228         struct rte_mempool     *mp;
229         rte_mempool_obj_cb_t   *obj_init;
230         void                   *obj_init_arg;
231 };
232
233 static void
234 mempool_obj_populate(void *arg, void *start, void *end, uint32_t idx)
235 {
236         struct mempool_populate_arg *pa = arg;
237
238         mempool_add_elem(pa->mp, start, idx, pa->obj_init, pa->obj_init_arg);
239         pa->mp->elt_va_end = (uintptr_t)end;
240 }
241
242 static void
243 mempool_populate(struct rte_mempool *mp, size_t num, size_t align,
244         rte_mempool_obj_cb_t *obj_init, void *obj_init_arg)
245 {
246         uint32_t elt_sz;
247         struct mempool_populate_arg arg;
248
249         elt_sz = mp->elt_size + mp->header_size + mp->trailer_size;
250         arg.mp = mp;
251         arg.obj_init = obj_init;
252         arg.obj_init_arg = obj_init_arg;
253
254         mp->size = rte_mempool_obj_iter((void *)mp->elt_va_start,
255                 num, elt_sz, align,
256                 mp->elt_pa, mp->pg_num, mp->pg_shift,
257                 mempool_obj_populate, &arg);
258 }
259
260 /* get the header, trailer and total size of a mempool element. */
261 uint32_t
262 rte_mempool_calc_obj_size(uint32_t elt_size, uint32_t flags,
263         struct rte_mempool_objsz *sz)
264 {
265         struct rte_mempool_objsz lsz;
266
267         sz = (sz != NULL) ? sz : &lsz;
268
269         sz->header_size = sizeof(struct rte_mempool_objhdr);
270         if ((flags & MEMPOOL_F_NO_CACHE_ALIGN) == 0)
271                 sz->header_size = RTE_ALIGN_CEIL(sz->header_size,
272                         RTE_MEMPOOL_ALIGN);
273
274         sz->trailer_size = sizeof(struct rte_mempool_objtlr);
275
276         /* element size is 8 bytes-aligned at least */
277         sz->elt_size = RTE_ALIGN_CEIL(elt_size, sizeof(uint64_t));
278
279         /* expand trailer to next cache line */
280         if ((flags & MEMPOOL_F_NO_CACHE_ALIGN) == 0) {
281                 sz->total_size = sz->header_size + sz->elt_size +
282                         sz->trailer_size;
283                 sz->trailer_size += ((RTE_MEMPOOL_ALIGN -
284                                   (sz->total_size & RTE_MEMPOOL_ALIGN_MASK)) &
285                                  RTE_MEMPOOL_ALIGN_MASK);
286         }
287
288         /*
289          * increase trailer to add padding between objects in order to
290          * spread them across memory channels/ranks
291          */
292         if ((flags & MEMPOOL_F_NO_SPREAD) == 0) {
293                 unsigned new_size;
294                 new_size = optimize_object_size(sz->header_size + sz->elt_size +
295                         sz->trailer_size);
296                 sz->trailer_size = new_size - sz->header_size - sz->elt_size;
297         }
298
299         if (! rte_eal_has_hugepages()) {
300                 /*
301                  * compute trailer size so that pool elements fit exactly in
302                  * a standard page
303                  */
304                 int page_size = getpagesize();
305                 int new_size = page_size - sz->header_size - sz->elt_size;
306                 if (new_size < 0 || (unsigned int)new_size < sz->trailer_size) {
307                         printf("When hugepages are disabled, pool objects "
308                                "can't exceed PAGE_SIZE: %d + %d + %d > %d\n",
309                                sz->header_size, sz->elt_size, sz->trailer_size,
310                                page_size);
311                         return 0;
312                 }
313                 sz->trailer_size = new_size;
314         }
315
316         /* this is the size of an object, including header and trailer */
317         sz->total_size = sz->header_size + sz->elt_size + sz->trailer_size;
318
319         return sz->total_size;
320 }
321
322
323 /*
324  * Calculate maximum amount of memory required to store given number of objects.
325  */
326 size_t
327 rte_mempool_xmem_size(uint32_t elt_num, size_t total_elt_sz, uint32_t pg_shift)
328 {
329         size_t n, pg_num, pg_sz, sz;
330
331         pg_sz = (size_t)1 << pg_shift;
332
333         if ((n = pg_sz / total_elt_sz) > 0) {
334                 pg_num = (elt_num + n - 1) / n;
335                 sz = pg_num << pg_shift;
336         } else {
337                 sz = RTE_ALIGN_CEIL(total_elt_sz, pg_sz) * elt_num;
338         }
339
340         return sz;
341 }
342
343 /* Callback used by rte_mempool_xmem_usage(): it sets the opaque
344  * argument to the end of the object.
345  */
346 static void
347 mempool_lelem_iter(void *arg, __rte_unused void *start, void *end,
348         __rte_unused uint32_t idx)
349 {
350         *(uintptr_t *)arg = (uintptr_t)end;
351 }
352
353 /*
354  * Calculate how much memory would be actually required with the
355  * given memory footprint to store required number of elements.
356  */
357 ssize_t
358 rte_mempool_xmem_usage(void *vaddr, uint32_t elt_num, size_t total_elt_sz,
359         const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift)
360 {
361         uint32_t n;
362         uintptr_t va, uv;
363         size_t pg_sz, usz;
364
365         pg_sz = (size_t)1 << pg_shift;
366         va = (uintptr_t)vaddr;
367         uv = va;
368
369         if ((n = rte_mempool_obj_iter(vaddr, elt_num, total_elt_sz, 1,
370                         paddr, pg_num, pg_shift, mempool_lelem_iter,
371                         &uv)) != elt_num) {
372                 return -(ssize_t)n;
373         }
374
375         uv = RTE_ALIGN_CEIL(uv, pg_sz);
376         usz = uv - va;
377         return usz;
378 }
379
380 #ifndef RTE_LIBRTE_XEN_DOM0
381 /* stub if DOM0 support not configured */
382 struct rte_mempool *
383 rte_dom0_mempool_create(const char *name __rte_unused,
384                         unsigned n __rte_unused,
385                         unsigned elt_size __rte_unused,
386                         unsigned cache_size __rte_unused,
387                         unsigned private_data_size __rte_unused,
388                         rte_mempool_ctor_t *mp_init __rte_unused,
389                         void *mp_init_arg __rte_unused,
390                         rte_mempool_obj_ctor_t *obj_init __rte_unused,
391                         void *obj_init_arg __rte_unused,
392                         int socket_id __rte_unused,
393                         unsigned flags __rte_unused)
394 {
395         rte_errno = EINVAL;
396         return NULL;
397 }
398 #endif
399
400 /* create the mempool */
401 struct rte_mempool *
402 rte_mempool_create(const char *name, unsigned n, unsigned elt_size,
403                    unsigned cache_size, unsigned private_data_size,
404                    rte_mempool_ctor_t *mp_init, void *mp_init_arg,
405                    rte_mempool_obj_ctor_t *obj_init, void *obj_init_arg,
406                    int socket_id, unsigned flags)
407 {
408         if (rte_xen_dom0_supported())
409                 return rte_dom0_mempool_create(name, n, elt_size,
410                                                cache_size, private_data_size,
411                                                mp_init, mp_init_arg,
412                                                obj_init, obj_init_arg,
413                                                socket_id, flags);
414         else
415                 return rte_mempool_xmem_create(name, n, elt_size,
416                                                cache_size, private_data_size,
417                                                mp_init, mp_init_arg,
418                                                obj_init, obj_init_arg,
419                                                socket_id, flags,
420                                                NULL, NULL, MEMPOOL_PG_NUM_DEFAULT,
421                                                MEMPOOL_PG_SHIFT_MAX);
422 }
423
424 /*
425  * Create the mempool over already allocated chunk of memory.
426  * That external memory buffer can consists of physically disjoint pages.
427  * Setting vaddr to NULL, makes mempool to fallback to original behaviour
428  * and allocate space for mempool and it's elements as one big chunk of
429  * physically continuos memory.
430  * */
431 struct rte_mempool *
432 rte_mempool_xmem_create(const char *name, unsigned n, unsigned elt_size,
433                 unsigned cache_size, unsigned private_data_size,
434                 rte_mempool_ctor_t *mp_init, void *mp_init_arg,
435                 rte_mempool_obj_cb_t *obj_init, void *obj_init_arg,
436                 int socket_id, unsigned flags, void *vaddr,
437                 const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift)
438 {
439         char mz_name[RTE_MEMZONE_NAMESIZE];
440         char rg_name[RTE_RING_NAMESIZE];
441         struct rte_mempool_list *mempool_list;
442         struct rte_mempool *mp = NULL;
443         struct rte_tailq_entry *te = NULL;
444         struct rte_ring *r = NULL;
445         const struct rte_memzone *mz;
446         size_t mempool_size;
447         int mz_flags = RTE_MEMZONE_1GB|RTE_MEMZONE_SIZE_HINT_ONLY;
448         int rg_flags = 0;
449         void *obj;
450         struct rte_mempool_objsz objsz;
451         void *startaddr;
452         int page_size = getpagesize();
453
454         /* compilation-time checks */
455         RTE_BUILD_BUG_ON((sizeof(struct rte_mempool) &
456                           RTE_CACHE_LINE_MASK) != 0);
457         RTE_BUILD_BUG_ON((sizeof(struct rte_mempool_cache) &
458                           RTE_CACHE_LINE_MASK) != 0);
459 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
460         RTE_BUILD_BUG_ON((sizeof(struct rte_mempool_debug_stats) &
461                           RTE_CACHE_LINE_MASK) != 0);
462         RTE_BUILD_BUG_ON((offsetof(struct rte_mempool, stats) &
463                           RTE_CACHE_LINE_MASK) != 0);
464 #endif
465
466         mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
467
468         /* asked cache too big */
469         if (cache_size > RTE_MEMPOOL_CACHE_MAX_SIZE ||
470             CALC_CACHE_FLUSHTHRESH(cache_size) > n) {
471                 rte_errno = EINVAL;
472                 return NULL;
473         }
474
475         /* check that we have both VA and PA */
476         if (vaddr != NULL && paddr == NULL) {
477                 rte_errno = EINVAL;
478                 return NULL;
479         }
480
481         /* Check that pg_num and pg_shift parameters are valid. */
482         if (pg_num < RTE_DIM(mp->elt_pa) || pg_shift > MEMPOOL_PG_SHIFT_MAX) {
483                 rte_errno = EINVAL;
484                 return NULL;
485         }
486
487         /* "no cache align" imply "no spread" */
488         if (flags & MEMPOOL_F_NO_CACHE_ALIGN)
489                 flags |= MEMPOOL_F_NO_SPREAD;
490
491         /* ring flags */
492         if (flags & MEMPOOL_F_SP_PUT)
493                 rg_flags |= RING_F_SP_ENQ;
494         if (flags & MEMPOOL_F_SC_GET)
495                 rg_flags |= RING_F_SC_DEQ;
496
497         /* calculate mempool object sizes. */
498         if (!rte_mempool_calc_obj_size(elt_size, flags, &objsz)) {
499                 rte_errno = EINVAL;
500                 return NULL;
501         }
502
503         rte_rwlock_write_lock(RTE_EAL_MEMPOOL_RWLOCK);
504
505         /* allocate the ring that will be used to store objects */
506         /* Ring functions will return appropriate errors if we are
507          * running as a secondary process etc., so no checks made
508          * in this function for that condition */
509         snprintf(rg_name, sizeof(rg_name), RTE_MEMPOOL_MZ_FORMAT, name);
510         r = rte_ring_create(rg_name, rte_align32pow2(n+1), socket_id, rg_flags);
511         if (r == NULL)
512                 goto exit_unlock;
513
514         /*
515          * reserve a memory zone for this mempool: private data is
516          * cache-aligned
517          */
518         private_data_size = (private_data_size +
519                              RTE_MEMPOOL_ALIGN_MASK) & (~RTE_MEMPOOL_ALIGN_MASK);
520
521         if (! rte_eal_has_hugepages()) {
522                 /*
523                  * expand private data size to a whole page, so that the
524                  * first pool element will start on a new standard page
525                  */
526                 int head = sizeof(struct rte_mempool);
527                 int new_size = (private_data_size + head) % page_size;
528                 if (new_size)
529                         private_data_size += page_size - new_size;
530         }
531
532         /* try to allocate tailq entry */
533         te = rte_zmalloc("MEMPOOL_TAILQ_ENTRY", sizeof(*te), 0);
534         if (te == NULL) {
535                 RTE_LOG(ERR, MEMPOOL, "Cannot allocate tailq entry!\n");
536                 goto exit_unlock;
537         }
538
539         /*
540          * If user provided an external memory buffer, then use it to
541          * store mempool objects. Otherwise reserve a memzone that is large
542          * enough to hold mempool header and metadata plus mempool objects.
543          */
544         mempool_size = MEMPOOL_HEADER_SIZE(mp, pg_num, cache_size);
545         mempool_size += private_data_size;
546         mempool_size = RTE_ALIGN_CEIL(mempool_size, RTE_MEMPOOL_ALIGN);
547         if (vaddr == NULL)
548                 mempool_size += (size_t)objsz.total_size * n;
549
550         if (! rte_eal_has_hugepages()) {
551                 /*
552                  * we want the memory pool to start on a page boundary,
553                  * because pool elements crossing page boundaries would
554                  * result in discontiguous physical addresses
555                  */
556                 mempool_size += page_size;
557         }
558
559         snprintf(mz_name, sizeof(mz_name), RTE_MEMPOOL_MZ_FORMAT, name);
560
561         mz = rte_memzone_reserve(mz_name, mempool_size, socket_id, mz_flags);
562         if (mz == NULL)
563                 goto exit_unlock;
564
565         if (rte_eal_has_hugepages()) {
566                 startaddr = (void*)mz->addr;
567         } else {
568                 /* align memory pool start address on a page boundary */
569                 unsigned long addr = (unsigned long)mz->addr;
570                 if (addr & (page_size - 1)) {
571                         addr += page_size;
572                         addr &= ~(page_size - 1);
573                 }
574                 startaddr = (void*)addr;
575         }
576
577         /* init the mempool structure */
578         mp = startaddr;
579         memset(mp, 0, sizeof(*mp));
580         snprintf(mp->name, sizeof(mp->name), "%s", name);
581         mp->phys_addr = mz->phys_addr;
582         mp->ring = r;
583         mp->size = n;
584         mp->flags = flags;
585         mp->elt_size = objsz.elt_size;
586         mp->header_size = objsz.header_size;
587         mp->trailer_size = objsz.trailer_size;
588         mp->cache_size = cache_size;
589         mp->cache_flushthresh = CALC_CACHE_FLUSHTHRESH(cache_size);
590         mp->private_data_size = private_data_size;
591         STAILQ_INIT(&mp->elt_list);
592
593         /*
594          * local_cache pointer is set even if cache_size is zero.
595          * The local_cache points to just past the elt_pa[] array.
596          */
597         mp->local_cache = (struct rte_mempool_cache *)
598                 RTE_PTR_ADD(mp, MEMPOOL_HEADER_SIZE(mp, pg_num, 0));
599
600         /* calculate address of the first element for continuous mempool. */
601         obj = (char *)mp + MEMPOOL_HEADER_SIZE(mp, pg_num, cache_size) +
602                 private_data_size;
603         obj = RTE_PTR_ALIGN_CEIL(obj, RTE_MEMPOOL_ALIGN);
604
605         /* populate address translation fields. */
606         mp->pg_num = pg_num;
607         mp->pg_shift = pg_shift;
608         mp->pg_mask = RTE_LEN2MASK(mp->pg_shift, typeof(mp->pg_mask));
609
610         /* mempool elements allocated together with mempool */
611         if (vaddr == NULL) {
612                 mp->elt_va_start = (uintptr_t)obj;
613                 mp->elt_pa[0] = mp->phys_addr +
614                         (mp->elt_va_start - (uintptr_t)mp);
615         } else {
616                 /* mempool elements in a separate chunk of memory. */
617                 mp->elt_va_start = (uintptr_t)vaddr;
618                 memcpy(mp->elt_pa, paddr, sizeof (mp->elt_pa[0]) * pg_num);
619         }
620
621         mp->elt_va_end = mp->elt_va_start;
622
623         /* call the initializer */
624         if (mp_init)
625                 mp_init(mp, mp_init_arg);
626
627         mempool_populate(mp, n, 1, obj_init, obj_init_arg);
628
629         te->data = (void *) mp;
630
631         rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
632         TAILQ_INSERT_TAIL(mempool_list, te, next);
633         rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
634         rte_rwlock_write_unlock(RTE_EAL_MEMPOOL_RWLOCK);
635
636         return mp;
637
638 exit_unlock:
639         rte_rwlock_write_unlock(RTE_EAL_MEMPOOL_RWLOCK);
640         rte_ring_free(r);
641         rte_free(te);
642
643         return NULL;
644 }
645
646 /* Return the number of entries in the mempool */
647 unsigned
648 rte_mempool_count(const struct rte_mempool *mp)
649 {
650         unsigned count;
651         unsigned lcore_id;
652
653         count = rte_ring_count(mp->ring);
654
655         if (mp->cache_size == 0)
656                 return count;
657
658         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++)
659                 count += mp->local_cache[lcore_id].len;
660
661         /*
662          * due to race condition (access to len is not locked), the
663          * total can be greater than size... so fix the result
664          */
665         if (count > mp->size)
666                 return mp->size;
667         return count;
668 }
669
670 /* dump the cache status */
671 static unsigned
672 rte_mempool_dump_cache(FILE *f, const struct rte_mempool *mp)
673 {
674         unsigned lcore_id;
675         unsigned count = 0;
676         unsigned cache_count;
677
678         fprintf(f, "  cache infos:\n");
679         fprintf(f, "    cache_size=%"PRIu32"\n", mp->cache_size);
680
681         if (mp->cache_size == 0)
682                 return count;
683
684         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
685                 cache_count = mp->local_cache[lcore_id].len;
686                 fprintf(f, "    cache_count[%u]=%u\n", lcore_id, cache_count);
687                 count += cache_count;
688         }
689         fprintf(f, "    total_cache_count=%u\n", count);
690         return count;
691 }
692
693 #ifndef __INTEL_COMPILER
694 #pragma GCC diagnostic ignored "-Wcast-qual"
695 #endif
696
697 struct mempool_audit_arg {
698         const struct rte_mempool *mp;
699         uintptr_t obj_end;
700         uint32_t obj_num;
701 };
702
703 /* check and update cookies or panic (internal) */
704 void rte_mempool_check_cookies(const struct rte_mempool *mp,
705         void * const *obj_table_const, unsigned n, int free)
706 {
707 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
708         struct rte_mempool_objhdr *hdr;
709         struct rte_mempool_objtlr *tlr;
710         uint64_t cookie;
711         void *tmp;
712         void *obj;
713         void **obj_table;
714
715         /* Force to drop the "const" attribute. This is done only when
716          * DEBUG is enabled */
717         tmp = (void *) obj_table_const;
718         obj_table = (void **) tmp;
719
720         while (n--) {
721                 obj = obj_table[n];
722
723                 if (rte_mempool_from_obj(obj) != mp)
724                         rte_panic("MEMPOOL: object is owned by another "
725                                   "mempool\n");
726
727                 hdr = __mempool_get_header(obj);
728                 cookie = hdr->cookie;
729
730                 if (free == 0) {
731                         if (cookie != RTE_MEMPOOL_HEADER_COOKIE1) {
732                                 rte_log_set_history(0);
733                                 RTE_LOG(CRIT, MEMPOOL,
734                                         "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
735                                         obj, (const void *) mp, cookie);
736                                 rte_panic("MEMPOOL: bad header cookie (put)\n");
737                         }
738                         hdr->cookie = RTE_MEMPOOL_HEADER_COOKIE2;
739                 } else if (free == 1) {
740                         if (cookie != RTE_MEMPOOL_HEADER_COOKIE2) {
741                                 rte_log_set_history(0);
742                                 RTE_LOG(CRIT, MEMPOOL,
743                                         "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
744                                         obj, (const void *) mp, cookie);
745                                 rte_panic("MEMPOOL: bad header cookie (get)\n");
746                         }
747                         hdr->cookie = RTE_MEMPOOL_HEADER_COOKIE1;
748                 } else if (free == 2) {
749                         if (cookie != RTE_MEMPOOL_HEADER_COOKIE1 &&
750                             cookie != RTE_MEMPOOL_HEADER_COOKIE2) {
751                                 rte_log_set_history(0);
752                                 RTE_LOG(CRIT, MEMPOOL,
753                                         "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
754                                         obj, (const void *) mp, cookie);
755                                 rte_panic("MEMPOOL: bad header cookie (audit)\n");
756                         }
757                 }
758                 tlr = __mempool_get_trailer(obj);
759                 cookie = tlr->cookie;
760                 if (cookie != RTE_MEMPOOL_TRAILER_COOKIE) {
761                         rte_log_set_history(0);
762                         RTE_LOG(CRIT, MEMPOOL,
763                                 "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
764                                 obj, (const void *) mp, cookie);
765                         rte_panic("MEMPOOL: bad trailer cookie\n");
766                 }
767         }
768 #else
769         RTE_SET_USED(mp);
770         RTE_SET_USED(obj_table_const);
771         RTE_SET_USED(n);
772         RTE_SET_USED(free);
773 #endif
774 }
775
776 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
777 static void
778 mempool_obj_audit(void *arg, void *start, void *end, uint32_t idx)
779 {
780         struct mempool_audit_arg *pa = arg;
781         void *obj;
782
783         obj = (char *)start + pa->mp->header_size;
784         pa->obj_end = (uintptr_t)end;
785         pa->obj_num = idx + 1;
786         __mempool_check_cookies(pa->mp, &obj, 1, 2);
787 }
788
789 static void
790 mempool_audit_cookies(const struct rte_mempool *mp)
791 {
792         uint32_t elt_sz, num;
793         struct mempool_audit_arg arg;
794
795         elt_sz = mp->elt_size + mp->header_size + mp->trailer_size;
796
797         arg.mp = mp;
798         arg.obj_end = mp->elt_va_start;
799         arg.obj_num = 0;
800
801         num = rte_mempool_obj_iter((void *)mp->elt_va_start,
802                 mp->size, elt_sz, 1,
803                 mp->elt_pa, mp->pg_num, mp->pg_shift,
804                 mempool_obj_audit, &arg);
805
806         if (num != mp->size) {
807                         rte_panic("rte_mempool_obj_iter(mempool=%p, size=%u) "
808                         "iterated only over %u elements\n",
809                         mp, mp->size, num);
810         } else if (arg.obj_end != mp->elt_va_end || arg.obj_num != mp->size) {
811                         rte_panic("rte_mempool_obj_iter(mempool=%p, size=%u) "
812                         "last callback va_end: %#tx (%#tx expeceted), "
813                         "num of objects: %u (%u expected)\n",
814                         mp, mp->size,
815                         arg.obj_end, mp->elt_va_end,
816                         arg.obj_num, mp->size);
817         }
818 }
819 #else
820 #define mempool_audit_cookies(mp) do {} while(0)
821 #endif
822
823 #ifndef __INTEL_COMPILER
824 #pragma GCC diagnostic error "-Wcast-qual"
825 #endif
826
827 /* check cookies before and after objects */
828 static void
829 mempool_audit_cache(const struct rte_mempool *mp)
830 {
831         /* check cache size consistency */
832         unsigned lcore_id;
833
834         if (mp->cache_size == 0)
835                 return;
836
837         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
838                 if (mp->local_cache[lcore_id].len > mp->cache_flushthresh) {
839                         RTE_LOG(CRIT, MEMPOOL, "badness on cache[%u]\n",
840                                 lcore_id);
841                         rte_panic("MEMPOOL: invalid cache len\n");
842                 }
843         }
844 }
845
846 /* check the consistency of mempool (size, cookies, ...) */
847 void
848 rte_mempool_audit(const struct rte_mempool *mp)
849 {
850         mempool_audit_cache(mp);
851         mempool_audit_cookies(mp);
852
853         /* For case where mempool DEBUG is not set, and cache size is 0 */
854         RTE_SET_USED(mp);
855 }
856
857 /* dump the status of the mempool on the console */
858 void
859 rte_mempool_dump(FILE *f, const struct rte_mempool *mp)
860 {
861 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
862         struct rte_mempool_debug_stats sum;
863         unsigned lcore_id;
864 #endif
865         unsigned common_count;
866         unsigned cache_count;
867
868         RTE_ASSERT(f != NULL);
869         RTE_ASSERT(mp != NULL);
870
871         fprintf(f, "mempool <%s>@%p\n", mp->name, mp);
872         fprintf(f, "  flags=%x\n", mp->flags);
873         fprintf(f, "  ring=<%s>@%p\n", mp->ring->name, mp->ring);
874         fprintf(f, "  phys_addr=0x%" PRIx64 "\n", mp->phys_addr);
875         fprintf(f, "  size=%"PRIu32"\n", mp->size);
876         fprintf(f, "  header_size=%"PRIu32"\n", mp->header_size);
877         fprintf(f, "  elt_size=%"PRIu32"\n", mp->elt_size);
878         fprintf(f, "  trailer_size=%"PRIu32"\n", mp->trailer_size);
879         fprintf(f, "  total_obj_size=%"PRIu32"\n",
880                mp->header_size + mp->elt_size + mp->trailer_size);
881
882         fprintf(f, "  private_data_size=%"PRIu32"\n", mp->private_data_size);
883         fprintf(f, "  pg_num=%"PRIu32"\n", mp->pg_num);
884         fprintf(f, "  pg_shift=%"PRIu32"\n", mp->pg_shift);
885         fprintf(f, "  pg_mask=%#tx\n", mp->pg_mask);
886         fprintf(f, "  elt_va_start=%#tx\n", mp->elt_va_start);
887         fprintf(f, "  elt_va_end=%#tx\n", mp->elt_va_end);
888         fprintf(f, "  elt_pa[0]=0x%" PRIx64 "\n", mp->elt_pa[0]);
889
890         if (mp->size != 0)
891                 fprintf(f, "  avg bytes/object=%#Lf\n",
892                         (long double)(mp->elt_va_end - mp->elt_va_start) /
893                         mp->size);
894
895         cache_count = rte_mempool_dump_cache(f, mp);
896         common_count = rte_ring_count(mp->ring);
897         if ((cache_count + common_count) > mp->size)
898                 common_count = mp->size - cache_count;
899         fprintf(f, "  common_pool_count=%u\n", common_count);
900
901         /* sum and dump statistics */
902 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
903         memset(&sum, 0, sizeof(sum));
904         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
905                 sum.put_bulk += mp->stats[lcore_id].put_bulk;
906                 sum.put_objs += mp->stats[lcore_id].put_objs;
907                 sum.get_success_bulk += mp->stats[lcore_id].get_success_bulk;
908                 sum.get_success_objs += mp->stats[lcore_id].get_success_objs;
909                 sum.get_fail_bulk += mp->stats[lcore_id].get_fail_bulk;
910                 sum.get_fail_objs += mp->stats[lcore_id].get_fail_objs;
911         }
912         fprintf(f, "  stats:\n");
913         fprintf(f, "    put_bulk=%"PRIu64"\n", sum.put_bulk);
914         fprintf(f, "    put_objs=%"PRIu64"\n", sum.put_objs);
915         fprintf(f, "    get_success_bulk=%"PRIu64"\n", sum.get_success_bulk);
916         fprintf(f, "    get_success_objs=%"PRIu64"\n", sum.get_success_objs);
917         fprintf(f, "    get_fail_bulk=%"PRIu64"\n", sum.get_fail_bulk);
918         fprintf(f, "    get_fail_objs=%"PRIu64"\n", sum.get_fail_objs);
919 #else
920         fprintf(f, "  no statistics available\n");
921 #endif
922
923         rte_mempool_audit(mp);
924 }
925
926 /* dump the status of all mempools on the console */
927 void
928 rte_mempool_list_dump(FILE *f)
929 {
930         const struct rte_mempool *mp = NULL;
931         struct rte_tailq_entry *te;
932         struct rte_mempool_list *mempool_list;
933
934         mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
935
936         rte_rwlock_read_lock(RTE_EAL_MEMPOOL_RWLOCK);
937
938         TAILQ_FOREACH(te, mempool_list, next) {
939                 mp = (struct rte_mempool *) te->data;
940                 rte_mempool_dump(f, mp);
941         }
942
943         rte_rwlock_read_unlock(RTE_EAL_MEMPOOL_RWLOCK);
944 }
945
946 /* search a mempool from its name */
947 struct rte_mempool *
948 rte_mempool_lookup(const char *name)
949 {
950         struct rte_mempool *mp = NULL;
951         struct rte_tailq_entry *te;
952         struct rte_mempool_list *mempool_list;
953
954         mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
955
956         rte_rwlock_read_lock(RTE_EAL_MEMPOOL_RWLOCK);
957
958         TAILQ_FOREACH(te, mempool_list, next) {
959                 mp = (struct rte_mempool *) te->data;
960                 if (strncmp(name, mp->name, RTE_MEMPOOL_NAMESIZE) == 0)
961                         break;
962         }
963
964         rte_rwlock_read_unlock(RTE_EAL_MEMPOOL_RWLOCK);
965
966         if (te == NULL) {
967                 rte_errno = ENOENT;
968                 return NULL;
969         }
970
971         return mp;
972 }
973
974 void rte_mempool_walk(void (*func)(struct rte_mempool *, void *),
975                       void *arg)
976 {
977         struct rte_tailq_entry *te = NULL;
978         struct rte_mempool_list *mempool_list;
979
980         mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
981
982         rte_rwlock_read_lock(RTE_EAL_MEMPOOL_RWLOCK);
983
984         TAILQ_FOREACH(te, mempool_list, next) {
985                 (*func)((struct rte_mempool *) te->data, arg);
986         }
987
988         rte_rwlock_read_unlock(RTE_EAL_MEMPOOL_RWLOCK);
989 }