mempool: detect physical contiguous objects
[dpdk.git] / lib / librte_mempool / rte_mempool.c
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 #include <stdio.h>
36 #include <string.h>
37 #include <stdint.h>
38 #include <stdarg.h>
39 #include <unistd.h>
40 #include <inttypes.h>
41 #include <errno.h>
42 #include <sys/queue.h>
43 #include <sys/mman.h>
44
45 #include <rte_common.h>
46 #include <rte_log.h>
47 #include <rte_debug.h>
48 #include <rte_memory.h>
49 #include <rte_memzone.h>
50 #include <rte_malloc.h>
51 #include <rte_atomic.h>
52 #include <rte_launch.h>
53 #include <rte_eal.h>
54 #include <rte_eal_memconfig.h>
55 #include <rte_per_lcore.h>
56 #include <rte_lcore.h>
57 #include <rte_branch_prediction.h>
58 #include <rte_errno.h>
59 #include <rte_string_fns.h>
60 #include <rte_spinlock.h>
61
62 #include "rte_mempool.h"
63
64 TAILQ_HEAD(rte_mempool_list, rte_tailq_entry);
65
66 static struct rte_tailq_elem rte_mempool_tailq = {
67         .name = "RTE_MEMPOOL",
68 };
69 EAL_REGISTER_TAILQ(rte_mempool_tailq)
70
71 #define CACHE_FLUSHTHRESH_MULTIPLIER 1.5
72 #define CALC_CACHE_FLUSHTHRESH(c)       \
73         ((typeof(c))((c) * CACHE_FLUSHTHRESH_MULTIPLIER))
74
75 /*
76  * return the greatest common divisor between a and b (fast algorithm)
77  *
78  */
79 static unsigned get_gcd(unsigned a, unsigned b)
80 {
81         unsigned c;
82
83         if (0 == a)
84                 return b;
85         if (0 == b)
86                 return a;
87
88         if (a < b) {
89                 c = a;
90                 a = b;
91                 b = c;
92         }
93
94         while (b != 0) {
95                 c = a % b;
96                 a = b;
97                 b = c;
98         }
99
100         return a;
101 }
102
103 /*
104  * Depending on memory configuration, objects addresses are spread
105  * between channels and ranks in RAM: the pool allocator will add
106  * padding between objects. This function return the new size of the
107  * object.
108  */
109 static unsigned optimize_object_size(unsigned obj_size)
110 {
111         unsigned nrank, nchan;
112         unsigned new_obj_size;
113
114         /* get number of channels */
115         nchan = rte_memory_get_nchannel();
116         if (nchan == 0)
117                 nchan = 4;
118
119         nrank = rte_memory_get_nrank();
120         if (nrank == 0)
121                 nrank = 1;
122
123         /* process new object size */
124         new_obj_size = (obj_size + RTE_MEMPOOL_ALIGN_MASK) / RTE_MEMPOOL_ALIGN;
125         while (get_gcd(new_obj_size, nrank * nchan) != 1)
126                 new_obj_size++;
127         return new_obj_size * RTE_MEMPOOL_ALIGN;
128 }
129
130 static void
131 mempool_add_elem(struct rte_mempool *mp, void *obj, phys_addr_t physaddr)
132 {
133         struct rte_mempool_objhdr *hdr;
134         struct rte_mempool_objtlr *tlr __rte_unused;
135
136         /* set mempool ptr in header */
137         hdr = RTE_PTR_SUB(obj, sizeof(*hdr));
138         hdr->mp = mp;
139         hdr->physaddr = physaddr;
140         STAILQ_INSERT_TAIL(&mp->elt_list, hdr, next);
141         mp->populated_size++;
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
149         /* enqueue in ring */
150         rte_mempool_ops_enqueue_bulk(mp, &obj, 1);
151 }
152
153 /* call obj_cb() for each mempool element */
154 uint32_t
155 rte_mempool_obj_iter(struct rte_mempool *mp,
156         rte_mempool_obj_cb_t *obj_cb, void *obj_cb_arg)
157 {
158         struct rte_mempool_objhdr *hdr;
159         void *obj;
160         unsigned n = 0;
161
162         STAILQ_FOREACH(hdr, &mp->elt_list, next) {
163                 obj = (char *)hdr + sizeof(*hdr);
164                 obj_cb(mp, obj_cb_arg, obj, n);
165                 n++;
166         }
167
168         return n;
169 }
170
171 /* call mem_cb() for each mempool memory chunk */
172 uint32_t
173 rte_mempool_mem_iter(struct rte_mempool *mp,
174         rte_mempool_mem_cb_t *mem_cb, void *mem_cb_arg)
175 {
176         struct rte_mempool_memhdr *hdr;
177         unsigned n = 0;
178
179         STAILQ_FOREACH(hdr, &mp->mem_list, next) {
180                 mem_cb(mp, mem_cb_arg, hdr, n);
181                 n++;
182         }
183
184         return n;
185 }
186
187 /* get the header, trailer and total size of a mempool element. */
188 uint32_t
189 rte_mempool_calc_obj_size(uint32_t elt_size, uint32_t flags,
190         struct rte_mempool_objsz *sz)
191 {
192         struct rte_mempool_objsz lsz;
193
194         sz = (sz != NULL) ? sz : &lsz;
195
196         sz->header_size = sizeof(struct rte_mempool_objhdr);
197         if ((flags & MEMPOOL_F_NO_CACHE_ALIGN) == 0)
198                 sz->header_size = RTE_ALIGN_CEIL(sz->header_size,
199                         RTE_MEMPOOL_ALIGN);
200
201 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
202         sz->trailer_size = sizeof(struct rte_mempool_objtlr);
203 #else
204         sz->trailer_size = 0;
205 #endif
206
207         /* element size is 8 bytes-aligned at least */
208         sz->elt_size = RTE_ALIGN_CEIL(elt_size, sizeof(uint64_t));
209
210         /* expand trailer to next cache line */
211         if ((flags & MEMPOOL_F_NO_CACHE_ALIGN) == 0) {
212                 sz->total_size = sz->header_size + sz->elt_size +
213                         sz->trailer_size;
214                 sz->trailer_size += ((RTE_MEMPOOL_ALIGN -
215                                   (sz->total_size & RTE_MEMPOOL_ALIGN_MASK)) &
216                                  RTE_MEMPOOL_ALIGN_MASK);
217         }
218
219         /*
220          * increase trailer to add padding between objects in order to
221          * spread them across memory channels/ranks
222          */
223         if ((flags & MEMPOOL_F_NO_SPREAD) == 0) {
224                 unsigned new_size;
225                 new_size = optimize_object_size(sz->header_size + sz->elt_size +
226                         sz->trailer_size);
227                 sz->trailer_size = new_size - sz->header_size - sz->elt_size;
228         }
229
230         /* this is the size of an object, including header and trailer */
231         sz->total_size = sz->header_size + sz->elt_size + sz->trailer_size;
232
233         return sz->total_size;
234 }
235
236
237 /*
238  * Calculate maximum amount of memory required to store given number of objects.
239  */
240 size_t
241 rte_mempool_xmem_size(uint32_t elt_num, size_t total_elt_sz, uint32_t pg_shift,
242                       __rte_unused unsigned int flags)
243 {
244         size_t obj_per_page, pg_num, pg_sz;
245
246         if (total_elt_sz == 0)
247                 return 0;
248
249         if (pg_shift == 0)
250                 return total_elt_sz * elt_num;
251
252         pg_sz = (size_t)1 << pg_shift;
253         obj_per_page = pg_sz / total_elt_sz;
254         if (obj_per_page == 0)
255                 return RTE_ALIGN_CEIL(total_elt_sz, pg_sz) * elt_num;
256
257         pg_num = (elt_num + obj_per_page - 1) / obj_per_page;
258         return pg_num << pg_shift;
259 }
260
261 /*
262  * Calculate how much memory would be actually required with the
263  * given memory footprint to store required number of elements.
264  */
265 ssize_t
266 rte_mempool_xmem_usage(__rte_unused void *vaddr, uint32_t elt_num,
267         size_t total_elt_sz, const phys_addr_t paddr[], uint32_t pg_num,
268         uint32_t pg_shift, __rte_unused unsigned int flags)
269 {
270         uint32_t elt_cnt = 0;
271         phys_addr_t start, end;
272         uint32_t paddr_idx;
273         size_t pg_sz = (size_t)1 << pg_shift;
274
275         /* if paddr is NULL, assume contiguous memory */
276         if (paddr == NULL) {
277                 start = 0;
278                 end = pg_sz * pg_num;
279                 paddr_idx = pg_num;
280         } else {
281                 start = paddr[0];
282                 end = paddr[0] + pg_sz;
283                 paddr_idx = 1;
284         }
285         while (elt_cnt < elt_num) {
286
287                 if (end - start >= total_elt_sz) {
288                         /* enough contiguous memory, add an object */
289                         start += total_elt_sz;
290                         elt_cnt++;
291                 } else if (paddr_idx < pg_num) {
292                         /* no room to store one obj, add a page */
293                         if (end == paddr[paddr_idx]) {
294                                 end += pg_sz;
295                         } else {
296                                 start = paddr[paddr_idx];
297                                 end = paddr[paddr_idx] + pg_sz;
298                         }
299                         paddr_idx++;
300
301                 } else {
302                         /* no more page, return how many elements fit */
303                         return -(size_t)elt_cnt;
304                 }
305         }
306
307         return (size_t)paddr_idx << pg_shift;
308 }
309
310 /* free a memchunk allocated with rte_memzone_reserve() */
311 static void
312 rte_mempool_memchunk_mz_free(__rte_unused struct rte_mempool_memhdr *memhdr,
313         void *opaque)
314 {
315         const struct rte_memzone *mz = opaque;
316         rte_memzone_free(mz);
317 }
318
319 /* Free memory chunks used by a mempool. Objects must be in pool */
320 static void
321 rte_mempool_free_memchunks(struct rte_mempool *mp)
322 {
323         struct rte_mempool_memhdr *memhdr;
324         void *elt;
325
326         while (!STAILQ_EMPTY(&mp->elt_list)) {
327                 rte_mempool_ops_dequeue_bulk(mp, &elt, 1);
328                 (void)elt;
329                 STAILQ_REMOVE_HEAD(&mp->elt_list, next);
330                 mp->populated_size--;
331         }
332
333         while (!STAILQ_EMPTY(&mp->mem_list)) {
334                 memhdr = STAILQ_FIRST(&mp->mem_list);
335                 STAILQ_REMOVE_HEAD(&mp->mem_list, next);
336                 if (memhdr->free_cb != NULL)
337                         memhdr->free_cb(memhdr, memhdr->opaque);
338                 rte_free(memhdr);
339                 mp->nb_mem_chunks--;
340         }
341 }
342
343 /* Add objects in the pool, using a physically contiguous memory
344  * zone. Return the number of objects added, or a negative value
345  * on error.
346  */
347 int
348 rte_mempool_populate_phys(struct rte_mempool *mp, char *vaddr,
349         phys_addr_t paddr, size_t len, rte_mempool_memchunk_free_cb_t *free_cb,
350         void *opaque)
351 {
352         unsigned total_elt_sz;
353         unsigned i = 0;
354         size_t off;
355         struct rte_mempool_memhdr *memhdr;
356         int ret;
357
358         /* create the internal ring if not already done */
359         if ((mp->flags & MEMPOOL_F_POOL_CREATED) == 0) {
360                 ret = rte_mempool_ops_alloc(mp);
361                 if (ret != 0)
362                         return ret;
363                 mp->flags |= MEMPOOL_F_POOL_CREATED;
364         }
365
366         /* mempool is already populated */
367         if (mp->populated_size >= mp->size)
368                 return -ENOSPC;
369
370         total_elt_sz = mp->header_size + mp->elt_size + mp->trailer_size;
371
372         /* Detect pool area has sufficient space for elements */
373         if (mp->flags & MEMPOOL_F_CAPA_PHYS_CONTIG) {
374                 if (len < total_elt_sz * mp->size) {
375                         RTE_LOG(ERR, MEMPOOL,
376                                 "pool area %" PRIx64 " not enough\n",
377                                 (uint64_t)len);
378                         return -ENOSPC;
379                 }
380         }
381
382         memhdr = rte_zmalloc("MEMPOOL_MEMHDR", sizeof(*memhdr), 0);
383         if (memhdr == NULL)
384                 return -ENOMEM;
385
386         memhdr->mp = mp;
387         memhdr->addr = vaddr;
388         memhdr->phys_addr = paddr;
389         memhdr->len = len;
390         memhdr->free_cb = free_cb;
391         memhdr->opaque = opaque;
392
393         if (mp->flags & MEMPOOL_F_NO_CACHE_ALIGN)
394                 off = RTE_PTR_ALIGN_CEIL(vaddr, 8) - vaddr;
395         else
396                 off = RTE_PTR_ALIGN_CEIL(vaddr, RTE_CACHE_LINE_SIZE) - vaddr;
397
398         while (off + total_elt_sz <= len && mp->populated_size < mp->size) {
399                 off += mp->header_size;
400                 if (paddr == RTE_BAD_PHYS_ADDR)
401                         mempool_add_elem(mp, (char *)vaddr + off,
402                                 RTE_BAD_PHYS_ADDR);
403                 else
404                         mempool_add_elem(mp, (char *)vaddr + off, paddr + off);
405                 off += mp->elt_size + mp->trailer_size;
406                 i++;
407         }
408
409         /* not enough room to store one object */
410         if (i == 0)
411                 return -EINVAL;
412
413         STAILQ_INSERT_TAIL(&mp->mem_list, memhdr, next);
414         mp->nb_mem_chunks++;
415         return i;
416 }
417
418 /* Add objects in the pool, using a table of physical pages. Return the
419  * number of objects added, or a negative value on error.
420  */
421 int
422 rte_mempool_populate_phys_tab(struct rte_mempool *mp, char *vaddr,
423         const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift,
424         rte_mempool_memchunk_free_cb_t *free_cb, void *opaque)
425 {
426         uint32_t i, n;
427         int ret, cnt = 0;
428         size_t pg_sz = (size_t)1 << pg_shift;
429
430         /* mempool must not be populated */
431         if (mp->nb_mem_chunks != 0)
432                 return -EEXIST;
433
434         if (mp->flags & MEMPOOL_F_NO_PHYS_CONTIG)
435                 return rte_mempool_populate_phys(mp, vaddr, RTE_BAD_PHYS_ADDR,
436                         pg_num * pg_sz, free_cb, opaque);
437
438         for (i = 0; i < pg_num && mp->populated_size < mp->size; i += n) {
439
440                 /* populate with the largest group of contiguous pages */
441                 for (n = 1; (i + n) < pg_num &&
442                              paddr[i + n - 1] + pg_sz == paddr[i + n]; n++)
443                         ;
444
445                 ret = rte_mempool_populate_phys(mp, vaddr + i * pg_sz,
446                         paddr[i], n * pg_sz, free_cb, opaque);
447                 if (ret < 0) {
448                         rte_mempool_free_memchunks(mp);
449                         return ret;
450                 }
451                 /* no need to call the free callback for next chunks */
452                 free_cb = NULL;
453                 cnt += ret;
454         }
455         return cnt;
456 }
457
458 /* Populate the mempool with a virtual area. Return the number of
459  * objects added, or a negative value on error.
460  */
461 int
462 rte_mempool_populate_virt(struct rte_mempool *mp, char *addr,
463         size_t len, size_t pg_sz, rte_mempool_memchunk_free_cb_t *free_cb,
464         void *opaque)
465 {
466         phys_addr_t paddr;
467         size_t off, phys_len;
468         int ret, cnt = 0;
469
470         /* mempool must not be populated */
471         if (mp->nb_mem_chunks != 0)
472                 return -EEXIST;
473         /* address and len must be page-aligned */
474         if (RTE_PTR_ALIGN_CEIL(addr, pg_sz) != addr)
475                 return -EINVAL;
476         if (RTE_ALIGN_CEIL(len, pg_sz) != len)
477                 return -EINVAL;
478
479         if (mp->flags & MEMPOOL_F_NO_PHYS_CONTIG)
480                 return rte_mempool_populate_phys(mp, addr, RTE_BAD_PHYS_ADDR,
481                         len, free_cb, opaque);
482
483         for (off = 0; off + pg_sz <= len &&
484                      mp->populated_size < mp->size; off += phys_len) {
485
486                 paddr = rte_mem_virt2phy(addr + off);
487                 /* required for xen_dom0 to get the machine address */
488                 paddr = rte_mem_phy2mch(-1, paddr);
489
490                 if (paddr == RTE_BAD_PHYS_ADDR && rte_eal_has_hugepages()) {
491                         ret = -EINVAL;
492                         goto fail;
493                 }
494
495                 /* populate with the largest group of contiguous pages */
496                 for (phys_len = pg_sz; off + phys_len < len; phys_len += pg_sz) {
497                         phys_addr_t paddr_tmp;
498
499                         paddr_tmp = rte_mem_virt2phy(addr + off + phys_len);
500                         paddr_tmp = rte_mem_phy2mch(-1, paddr_tmp);
501
502                         if (paddr_tmp != paddr + phys_len)
503                                 break;
504                 }
505
506                 ret = rte_mempool_populate_phys(mp, addr + off, paddr,
507                         phys_len, free_cb, opaque);
508                 if (ret < 0)
509                         goto fail;
510                 /* no need to call the free callback for next chunks */
511                 free_cb = NULL;
512                 cnt += ret;
513         }
514
515         return cnt;
516
517  fail:
518         rte_mempool_free_memchunks(mp);
519         return ret;
520 }
521
522 /* Default function to populate the mempool: allocate memory in memzones,
523  * and populate them. Return the number of objects added, or a negative
524  * value on error.
525  */
526 int
527 rte_mempool_populate_default(struct rte_mempool *mp)
528 {
529         unsigned int mz_flags = RTE_MEMZONE_1GB|RTE_MEMZONE_SIZE_HINT_ONLY;
530         char mz_name[RTE_MEMZONE_NAMESIZE];
531         const struct rte_memzone *mz;
532         size_t size, total_elt_sz, align, pg_sz, pg_shift;
533         phys_addr_t paddr;
534         unsigned mz_id, n;
535         unsigned int mp_flags;
536         int ret;
537
538         /* mempool must not be populated */
539         if (mp->nb_mem_chunks != 0)
540                 return -EEXIST;
541
542         /* Get mempool capabilities */
543         mp_flags = 0;
544         ret = rte_mempool_ops_get_capabilities(mp, &mp_flags);
545         if (ret == -ENOTSUP)
546                 RTE_LOG(DEBUG, MEMPOOL, "get_capability not supported for %s\n",
547                                         mp->name);
548         else if (ret < 0)
549                 return ret;
550
551         /* update mempool capabilities */
552         mp->flags |= mp_flags;
553
554         if (rte_xen_dom0_supported()) {
555                 pg_sz = RTE_PGSIZE_2M;
556                 pg_shift = rte_bsf32(pg_sz);
557                 align = pg_sz;
558         } else if (rte_eal_has_hugepages()) {
559                 pg_shift = 0; /* not needed, zone is physically contiguous */
560                 pg_sz = 0;
561                 align = RTE_CACHE_LINE_SIZE;
562         } else {
563                 pg_sz = getpagesize();
564                 pg_shift = rte_bsf32(pg_sz);
565                 align = pg_sz;
566         }
567
568         total_elt_sz = mp->header_size + mp->elt_size + mp->trailer_size;
569         for (mz_id = 0, n = mp->size; n > 0; mz_id++, n -= ret) {
570                 size = rte_mempool_xmem_size(n, total_elt_sz, pg_shift,
571                                                 mp->flags);
572
573                 ret = snprintf(mz_name, sizeof(mz_name),
574                         RTE_MEMPOOL_MZ_FORMAT "_%d", mp->name, mz_id);
575                 if (ret < 0 || ret >= (int)sizeof(mz_name)) {
576                         ret = -ENAMETOOLONG;
577                         goto fail;
578                 }
579
580                 mz = rte_memzone_reserve_aligned(mz_name, size,
581                         mp->socket_id, mz_flags, align);
582                 /* not enough memory, retry with the biggest zone we have */
583                 if (mz == NULL)
584                         mz = rte_memzone_reserve_aligned(mz_name, 0,
585                                 mp->socket_id, mz_flags, align);
586                 if (mz == NULL) {
587                         ret = -rte_errno;
588                         goto fail;
589                 }
590
591                 if (mp->flags & MEMPOOL_F_NO_PHYS_CONTIG)
592                         paddr = RTE_BAD_PHYS_ADDR;
593                 else
594                         paddr = mz->phys_addr;
595
596                 if (rte_eal_has_hugepages() && !rte_xen_dom0_supported())
597                         ret = rte_mempool_populate_phys(mp, mz->addr,
598                                 paddr, mz->len,
599                                 rte_mempool_memchunk_mz_free,
600                                 (void *)(uintptr_t)mz);
601                 else
602                         ret = rte_mempool_populate_virt(mp, mz->addr,
603                                 mz->len, pg_sz,
604                                 rte_mempool_memchunk_mz_free,
605                                 (void *)(uintptr_t)mz);
606                 if (ret < 0) {
607                         rte_memzone_free(mz);
608                         goto fail;
609                 }
610         }
611
612         return mp->size;
613
614  fail:
615         rte_mempool_free_memchunks(mp);
616         return ret;
617 }
618
619 /* return the memory size required for mempool objects in anonymous mem */
620 static size_t
621 get_anon_size(const struct rte_mempool *mp)
622 {
623         size_t size, total_elt_sz, pg_sz, pg_shift;
624
625         pg_sz = getpagesize();
626         pg_shift = rte_bsf32(pg_sz);
627         total_elt_sz = mp->header_size + mp->elt_size + mp->trailer_size;
628         size = rte_mempool_xmem_size(mp->size, total_elt_sz, pg_shift,
629                                         mp->flags);
630
631         return size;
632 }
633
634 /* unmap a memory zone mapped by rte_mempool_populate_anon() */
635 static void
636 rte_mempool_memchunk_anon_free(struct rte_mempool_memhdr *memhdr,
637         void *opaque)
638 {
639         munmap(opaque, get_anon_size(memhdr->mp));
640 }
641
642 /* populate the mempool with an anonymous mapping */
643 int
644 rte_mempool_populate_anon(struct rte_mempool *mp)
645 {
646         size_t size;
647         int ret;
648         char *addr;
649
650         /* mempool is already populated, error */
651         if (!STAILQ_EMPTY(&mp->mem_list)) {
652                 rte_errno = EINVAL;
653                 return 0;
654         }
655
656         /* get chunk of virtually continuous memory */
657         size = get_anon_size(mp);
658         addr = mmap(NULL, size, PROT_READ | PROT_WRITE,
659                 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
660         if (addr == MAP_FAILED) {
661                 rte_errno = errno;
662                 return 0;
663         }
664         /* can't use MMAP_LOCKED, it does not exist on BSD */
665         if (mlock(addr, size) < 0) {
666                 rte_errno = errno;
667                 munmap(addr, size);
668                 return 0;
669         }
670
671         ret = rte_mempool_populate_virt(mp, addr, size, getpagesize(),
672                 rte_mempool_memchunk_anon_free, addr);
673         if (ret == 0)
674                 goto fail;
675
676         return mp->populated_size;
677
678  fail:
679         rte_mempool_free_memchunks(mp);
680         return 0;
681 }
682
683 /* free a mempool */
684 void
685 rte_mempool_free(struct rte_mempool *mp)
686 {
687         struct rte_mempool_list *mempool_list = NULL;
688         struct rte_tailq_entry *te;
689
690         if (mp == NULL)
691                 return;
692
693         mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
694         rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
695         /* find out tailq entry */
696         TAILQ_FOREACH(te, mempool_list, next) {
697                 if (te->data == (void *)mp)
698                         break;
699         }
700
701         if (te != NULL) {
702                 TAILQ_REMOVE(mempool_list, te, next);
703                 rte_free(te);
704         }
705         rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
706
707         rte_mempool_free_memchunks(mp);
708         rte_mempool_ops_free(mp);
709         rte_memzone_free(mp->mz);
710 }
711
712 static void
713 mempool_cache_init(struct rte_mempool_cache *cache, uint32_t size)
714 {
715         cache->size = size;
716         cache->flushthresh = CALC_CACHE_FLUSHTHRESH(size);
717         cache->len = 0;
718 }
719
720 /*
721  * Create and initialize a cache for objects that are retrieved from and
722  * returned to an underlying mempool. This structure is identical to the
723  * local_cache[lcore_id] pointed to by the mempool structure.
724  */
725 struct rte_mempool_cache *
726 rte_mempool_cache_create(uint32_t size, int socket_id)
727 {
728         struct rte_mempool_cache *cache;
729
730         if (size == 0 || size > RTE_MEMPOOL_CACHE_MAX_SIZE) {
731                 rte_errno = EINVAL;
732                 return NULL;
733         }
734
735         cache = rte_zmalloc_socket("MEMPOOL_CACHE", sizeof(*cache),
736                                   RTE_CACHE_LINE_SIZE, socket_id);
737         if (cache == NULL) {
738                 RTE_LOG(ERR, MEMPOOL, "Cannot allocate mempool cache.\n");
739                 rte_errno = ENOMEM;
740                 return NULL;
741         }
742
743         mempool_cache_init(cache, size);
744
745         return cache;
746 }
747
748 /*
749  * Free a cache. It's the responsibility of the user to make sure that any
750  * remaining objects in the cache are flushed to the corresponding
751  * mempool.
752  */
753 void
754 rte_mempool_cache_free(struct rte_mempool_cache *cache)
755 {
756         rte_free(cache);
757 }
758
759 /* create an empty mempool */
760 struct rte_mempool *
761 rte_mempool_create_empty(const char *name, unsigned n, unsigned elt_size,
762         unsigned cache_size, unsigned private_data_size,
763         int socket_id, unsigned flags)
764 {
765         char mz_name[RTE_MEMZONE_NAMESIZE];
766         struct rte_mempool_list *mempool_list;
767         struct rte_mempool *mp = NULL;
768         struct rte_tailq_entry *te = NULL;
769         const struct rte_memzone *mz = NULL;
770         size_t mempool_size;
771         unsigned int mz_flags = RTE_MEMZONE_1GB|RTE_MEMZONE_SIZE_HINT_ONLY;
772         struct rte_mempool_objsz objsz;
773         unsigned lcore_id;
774         int ret;
775
776         /* compilation-time checks */
777         RTE_BUILD_BUG_ON((sizeof(struct rte_mempool) &
778                           RTE_CACHE_LINE_MASK) != 0);
779         RTE_BUILD_BUG_ON((sizeof(struct rte_mempool_cache) &
780                           RTE_CACHE_LINE_MASK) != 0);
781 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
782         RTE_BUILD_BUG_ON((sizeof(struct rte_mempool_debug_stats) &
783                           RTE_CACHE_LINE_MASK) != 0);
784         RTE_BUILD_BUG_ON((offsetof(struct rte_mempool, stats) &
785                           RTE_CACHE_LINE_MASK) != 0);
786 #endif
787
788         mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
789
790         /* asked cache too big */
791         if (cache_size > RTE_MEMPOOL_CACHE_MAX_SIZE ||
792             CALC_CACHE_FLUSHTHRESH(cache_size) > n) {
793                 rte_errno = EINVAL;
794                 return NULL;
795         }
796
797         /* "no cache align" imply "no spread" */
798         if (flags & MEMPOOL_F_NO_CACHE_ALIGN)
799                 flags |= MEMPOOL_F_NO_SPREAD;
800
801         /* calculate mempool object sizes. */
802         if (!rte_mempool_calc_obj_size(elt_size, flags, &objsz)) {
803                 rte_errno = EINVAL;
804                 return NULL;
805         }
806
807         rte_rwlock_write_lock(RTE_EAL_MEMPOOL_RWLOCK);
808
809         /*
810          * reserve a memory zone for this mempool: private data is
811          * cache-aligned
812          */
813         private_data_size = (private_data_size +
814                              RTE_MEMPOOL_ALIGN_MASK) & (~RTE_MEMPOOL_ALIGN_MASK);
815
816
817         /* try to allocate tailq entry */
818         te = rte_zmalloc("MEMPOOL_TAILQ_ENTRY", sizeof(*te), 0);
819         if (te == NULL) {
820                 RTE_LOG(ERR, MEMPOOL, "Cannot allocate tailq entry!\n");
821                 goto exit_unlock;
822         }
823
824         mempool_size = MEMPOOL_HEADER_SIZE(mp, cache_size);
825         mempool_size += private_data_size;
826         mempool_size = RTE_ALIGN_CEIL(mempool_size, RTE_MEMPOOL_ALIGN);
827
828         ret = snprintf(mz_name, sizeof(mz_name), RTE_MEMPOOL_MZ_FORMAT, name);
829         if (ret < 0 || ret >= (int)sizeof(mz_name)) {
830                 rte_errno = ENAMETOOLONG;
831                 goto exit_unlock;
832         }
833
834         mz = rte_memzone_reserve(mz_name, mempool_size, socket_id, mz_flags);
835         if (mz == NULL)
836                 goto exit_unlock;
837
838         /* init the mempool structure */
839         mp = mz->addr;
840         memset(mp, 0, MEMPOOL_HEADER_SIZE(mp, cache_size));
841         ret = snprintf(mp->name, sizeof(mp->name), "%s", name);
842         if (ret < 0 || ret >= (int)sizeof(mp->name)) {
843                 rte_errno = ENAMETOOLONG;
844                 goto exit_unlock;
845         }
846         mp->mz = mz;
847         mp->size = n;
848         mp->flags = flags;
849         mp->socket_id = socket_id;
850         mp->elt_size = objsz.elt_size;
851         mp->header_size = objsz.header_size;
852         mp->trailer_size = objsz.trailer_size;
853         /* Size of default caches, zero means disabled. */
854         mp->cache_size = cache_size;
855         mp->private_data_size = private_data_size;
856         STAILQ_INIT(&mp->elt_list);
857         STAILQ_INIT(&mp->mem_list);
858
859         /*
860          * local_cache pointer is set even if cache_size is zero.
861          * The local_cache points to just past the elt_pa[] array.
862          */
863         mp->local_cache = (struct rte_mempool_cache *)
864                 RTE_PTR_ADD(mp, MEMPOOL_HEADER_SIZE(mp, 0));
865
866         /* Init all default caches. */
867         if (cache_size != 0) {
868                 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++)
869                         mempool_cache_init(&mp->local_cache[lcore_id],
870                                            cache_size);
871         }
872
873         te->data = mp;
874
875         rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
876         TAILQ_INSERT_TAIL(mempool_list, te, next);
877         rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
878         rte_rwlock_write_unlock(RTE_EAL_MEMPOOL_RWLOCK);
879
880         return mp;
881
882 exit_unlock:
883         rte_rwlock_write_unlock(RTE_EAL_MEMPOOL_RWLOCK);
884         rte_free(te);
885         rte_mempool_free(mp);
886         return NULL;
887 }
888
889 /* create the mempool */
890 struct rte_mempool *
891 rte_mempool_create(const char *name, unsigned n, unsigned elt_size,
892         unsigned cache_size, unsigned private_data_size,
893         rte_mempool_ctor_t *mp_init, void *mp_init_arg,
894         rte_mempool_obj_cb_t *obj_init, void *obj_init_arg,
895         int socket_id, unsigned flags)
896 {
897         int ret;
898         struct rte_mempool *mp;
899
900         mp = rte_mempool_create_empty(name, n, elt_size, cache_size,
901                 private_data_size, socket_id, flags);
902         if (mp == NULL)
903                 return NULL;
904
905         /*
906          * Since we have 4 combinations of the SP/SC/MP/MC examine the flags to
907          * set the correct index into the table of ops structs.
908          */
909         if ((flags & MEMPOOL_F_SP_PUT) && (flags & MEMPOOL_F_SC_GET))
910                 ret = rte_mempool_set_ops_byname(mp, "ring_sp_sc", NULL);
911         else if (flags & MEMPOOL_F_SP_PUT)
912                 ret = rte_mempool_set_ops_byname(mp, "ring_sp_mc", NULL);
913         else if (flags & MEMPOOL_F_SC_GET)
914                 ret = rte_mempool_set_ops_byname(mp, "ring_mp_sc", NULL);
915         else
916                 ret = rte_mempool_set_ops_byname(mp, "ring_mp_mc", NULL);
917
918         if (ret)
919                 goto fail;
920
921         /* call the mempool priv initializer */
922         if (mp_init)
923                 mp_init(mp, mp_init_arg);
924
925         if (rte_mempool_populate_default(mp) < 0)
926                 goto fail;
927
928         /* call the object initializers */
929         if (obj_init)
930                 rte_mempool_obj_iter(mp, obj_init, obj_init_arg);
931
932         return mp;
933
934  fail:
935         rte_mempool_free(mp);
936         return NULL;
937 }
938
939 /*
940  * Create the mempool over already allocated chunk of memory.
941  * That external memory buffer can consists of physically disjoint pages.
942  * Setting vaddr to NULL, makes mempool to fallback to rte_mempool_create()
943  * behavior.
944  */
945 struct rte_mempool *
946 rte_mempool_xmem_create(const char *name, unsigned n, unsigned elt_size,
947                 unsigned cache_size, unsigned private_data_size,
948                 rte_mempool_ctor_t *mp_init, void *mp_init_arg,
949                 rte_mempool_obj_cb_t *obj_init, void *obj_init_arg,
950                 int socket_id, unsigned flags, void *vaddr,
951                 const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift)
952 {
953         struct rte_mempool *mp = NULL;
954         int ret;
955
956         /* no virtual address supplied, use rte_mempool_create() */
957         if (vaddr == NULL)
958                 return rte_mempool_create(name, n, elt_size, cache_size,
959                         private_data_size, mp_init, mp_init_arg,
960                         obj_init, obj_init_arg, socket_id, flags);
961
962         /* check that we have both VA and PA */
963         if (paddr == NULL) {
964                 rte_errno = EINVAL;
965                 return NULL;
966         }
967
968         /* Check that pg_shift parameter is valid. */
969         if (pg_shift > MEMPOOL_PG_SHIFT_MAX) {
970                 rte_errno = EINVAL;
971                 return NULL;
972         }
973
974         mp = rte_mempool_create_empty(name, n, elt_size, cache_size,
975                 private_data_size, socket_id, flags);
976         if (mp == NULL)
977                 return NULL;
978
979         /* call the mempool priv initializer */
980         if (mp_init)
981                 mp_init(mp, mp_init_arg);
982
983         ret = rte_mempool_populate_phys_tab(mp, vaddr, paddr, pg_num, pg_shift,
984                 NULL, NULL);
985         if (ret < 0 || ret != (int)mp->size)
986                 goto fail;
987
988         /* call the object initializers */
989         if (obj_init)
990                 rte_mempool_obj_iter(mp, obj_init, obj_init_arg);
991
992         return mp;
993
994  fail:
995         rte_mempool_free(mp);
996         return NULL;
997 }
998
999 /* Return the number of entries in the mempool */
1000 unsigned int
1001 rte_mempool_avail_count(const struct rte_mempool *mp)
1002 {
1003         unsigned count;
1004         unsigned lcore_id;
1005
1006         count = rte_mempool_ops_get_count(mp);
1007
1008         if (mp->cache_size == 0)
1009                 return count;
1010
1011         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++)
1012                 count += mp->local_cache[lcore_id].len;
1013
1014         /*
1015          * due to race condition (access to len is not locked), the
1016          * total can be greater than size... so fix the result
1017          */
1018         if (count > mp->size)
1019                 return mp->size;
1020         return count;
1021 }
1022
1023 /* return the number of entries allocated from the mempool */
1024 unsigned int
1025 rte_mempool_in_use_count(const struct rte_mempool *mp)
1026 {
1027         return mp->size - rte_mempool_avail_count(mp);
1028 }
1029
1030 /* dump the cache status */
1031 static unsigned
1032 rte_mempool_dump_cache(FILE *f, const struct rte_mempool *mp)
1033 {
1034         unsigned lcore_id;
1035         unsigned count = 0;
1036         unsigned cache_count;
1037
1038         fprintf(f, "  internal cache infos:\n");
1039         fprintf(f, "    cache_size=%"PRIu32"\n", mp->cache_size);
1040
1041         if (mp->cache_size == 0)
1042                 return count;
1043
1044         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1045                 cache_count = mp->local_cache[lcore_id].len;
1046                 fprintf(f, "    cache_count[%u]=%"PRIu32"\n",
1047                         lcore_id, cache_count);
1048                 count += cache_count;
1049         }
1050         fprintf(f, "    total_cache_count=%u\n", count);
1051         return count;
1052 }
1053
1054 #ifndef __INTEL_COMPILER
1055 #pragma GCC diagnostic ignored "-Wcast-qual"
1056 #endif
1057
1058 /* check and update cookies or panic (internal) */
1059 void rte_mempool_check_cookies(const struct rte_mempool *mp,
1060         void * const *obj_table_const, unsigned n, int free)
1061 {
1062 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
1063         struct rte_mempool_objhdr *hdr;
1064         struct rte_mempool_objtlr *tlr;
1065         uint64_t cookie;
1066         void *tmp;
1067         void *obj;
1068         void **obj_table;
1069
1070         /* Force to drop the "const" attribute. This is done only when
1071          * DEBUG is enabled */
1072         tmp = (void *) obj_table_const;
1073         obj_table = tmp;
1074
1075         while (n--) {
1076                 obj = obj_table[n];
1077
1078                 if (rte_mempool_from_obj(obj) != mp)
1079                         rte_panic("MEMPOOL: object is owned by another "
1080                                   "mempool\n");
1081
1082                 hdr = __mempool_get_header(obj);
1083                 cookie = hdr->cookie;
1084
1085                 if (free == 0) {
1086                         if (cookie != RTE_MEMPOOL_HEADER_COOKIE1) {
1087                                 RTE_LOG(CRIT, MEMPOOL,
1088                                         "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
1089                                         obj, (const void *) mp, cookie);
1090                                 rte_panic("MEMPOOL: bad header cookie (put)\n");
1091                         }
1092                         hdr->cookie = RTE_MEMPOOL_HEADER_COOKIE2;
1093                 } else if (free == 1) {
1094                         if (cookie != RTE_MEMPOOL_HEADER_COOKIE2) {
1095                                 RTE_LOG(CRIT, MEMPOOL,
1096                                         "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
1097                                         obj, (const void *) mp, cookie);
1098                                 rte_panic("MEMPOOL: bad header cookie (get)\n");
1099                         }
1100                         hdr->cookie = RTE_MEMPOOL_HEADER_COOKIE1;
1101                 } else if (free == 2) {
1102                         if (cookie != RTE_MEMPOOL_HEADER_COOKIE1 &&
1103                             cookie != RTE_MEMPOOL_HEADER_COOKIE2) {
1104                                 RTE_LOG(CRIT, MEMPOOL,
1105                                         "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
1106                                         obj, (const void *) mp, cookie);
1107                                 rte_panic("MEMPOOL: bad header cookie (audit)\n");
1108                         }
1109                 }
1110                 tlr = __mempool_get_trailer(obj);
1111                 cookie = tlr->cookie;
1112                 if (cookie != RTE_MEMPOOL_TRAILER_COOKIE) {
1113                         RTE_LOG(CRIT, MEMPOOL,
1114                                 "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
1115                                 obj, (const void *) mp, cookie);
1116                         rte_panic("MEMPOOL: bad trailer cookie\n");
1117                 }
1118         }
1119 #else
1120         RTE_SET_USED(mp);
1121         RTE_SET_USED(obj_table_const);
1122         RTE_SET_USED(n);
1123         RTE_SET_USED(free);
1124 #endif
1125 }
1126
1127 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
1128 static void
1129 mempool_obj_audit(struct rte_mempool *mp, __rte_unused void *opaque,
1130         void *obj, __rte_unused unsigned idx)
1131 {
1132         __mempool_check_cookies(mp, &obj, 1, 2);
1133 }
1134
1135 static void
1136 mempool_audit_cookies(struct rte_mempool *mp)
1137 {
1138         unsigned num;
1139
1140         num = rte_mempool_obj_iter(mp, mempool_obj_audit, NULL);
1141         if (num != mp->size) {
1142                 rte_panic("rte_mempool_obj_iter(mempool=%p, size=%u) "
1143                         "iterated only over %u elements\n",
1144                         mp, mp->size, num);
1145         }
1146 }
1147 #else
1148 #define mempool_audit_cookies(mp) do {} while(0)
1149 #endif
1150
1151 #ifndef __INTEL_COMPILER
1152 #pragma GCC diagnostic error "-Wcast-qual"
1153 #endif
1154
1155 /* check cookies before and after objects */
1156 static void
1157 mempool_audit_cache(const struct rte_mempool *mp)
1158 {
1159         /* check cache size consistency */
1160         unsigned lcore_id;
1161
1162         if (mp->cache_size == 0)
1163                 return;
1164
1165         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1166                 const struct rte_mempool_cache *cache;
1167                 cache = &mp->local_cache[lcore_id];
1168                 if (cache->len > cache->flushthresh) {
1169                         RTE_LOG(CRIT, MEMPOOL, "badness on cache[%u]\n",
1170                                 lcore_id);
1171                         rte_panic("MEMPOOL: invalid cache len\n");
1172                 }
1173         }
1174 }
1175
1176 /* check the consistency of mempool (size, cookies, ...) */
1177 void
1178 rte_mempool_audit(struct rte_mempool *mp)
1179 {
1180         mempool_audit_cache(mp);
1181         mempool_audit_cookies(mp);
1182
1183         /* For case where mempool DEBUG is not set, and cache size is 0 */
1184         RTE_SET_USED(mp);
1185 }
1186
1187 /* dump the status of the mempool on the console */
1188 void
1189 rte_mempool_dump(FILE *f, struct rte_mempool *mp)
1190 {
1191 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
1192         struct rte_mempool_debug_stats sum;
1193         unsigned lcore_id;
1194 #endif
1195         struct rte_mempool_memhdr *memhdr;
1196         unsigned common_count;
1197         unsigned cache_count;
1198         size_t mem_len = 0;
1199
1200         RTE_ASSERT(f != NULL);
1201         RTE_ASSERT(mp != NULL);
1202
1203         fprintf(f, "mempool <%s>@%p\n", mp->name, mp);
1204         fprintf(f, "  flags=%x\n", mp->flags);
1205         fprintf(f, "  pool=%p\n", mp->pool_data);
1206         fprintf(f, "  phys_addr=0x%" PRIx64 "\n", mp->mz->phys_addr);
1207         fprintf(f, "  nb_mem_chunks=%u\n", mp->nb_mem_chunks);
1208         fprintf(f, "  size=%"PRIu32"\n", mp->size);
1209         fprintf(f, "  populated_size=%"PRIu32"\n", mp->populated_size);
1210         fprintf(f, "  header_size=%"PRIu32"\n", mp->header_size);
1211         fprintf(f, "  elt_size=%"PRIu32"\n", mp->elt_size);
1212         fprintf(f, "  trailer_size=%"PRIu32"\n", mp->trailer_size);
1213         fprintf(f, "  total_obj_size=%"PRIu32"\n",
1214                mp->header_size + mp->elt_size + mp->trailer_size);
1215
1216         fprintf(f, "  private_data_size=%"PRIu32"\n", mp->private_data_size);
1217
1218         STAILQ_FOREACH(memhdr, &mp->mem_list, next)
1219                 mem_len += memhdr->len;
1220         if (mem_len != 0) {
1221                 fprintf(f, "  avg bytes/object=%#Lf\n",
1222                         (long double)mem_len / mp->size);
1223         }
1224
1225         cache_count = rte_mempool_dump_cache(f, mp);
1226         common_count = rte_mempool_ops_get_count(mp);
1227         if ((cache_count + common_count) > mp->size)
1228                 common_count = mp->size - cache_count;
1229         fprintf(f, "  common_pool_count=%u\n", common_count);
1230
1231         /* sum and dump statistics */
1232 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
1233         memset(&sum, 0, sizeof(sum));
1234         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1235                 sum.put_bulk += mp->stats[lcore_id].put_bulk;
1236                 sum.put_objs += mp->stats[lcore_id].put_objs;
1237                 sum.get_success_bulk += mp->stats[lcore_id].get_success_bulk;
1238                 sum.get_success_objs += mp->stats[lcore_id].get_success_objs;
1239                 sum.get_fail_bulk += mp->stats[lcore_id].get_fail_bulk;
1240                 sum.get_fail_objs += mp->stats[lcore_id].get_fail_objs;
1241         }
1242         fprintf(f, "  stats:\n");
1243         fprintf(f, "    put_bulk=%"PRIu64"\n", sum.put_bulk);
1244         fprintf(f, "    put_objs=%"PRIu64"\n", sum.put_objs);
1245         fprintf(f, "    get_success_bulk=%"PRIu64"\n", sum.get_success_bulk);
1246         fprintf(f, "    get_success_objs=%"PRIu64"\n", sum.get_success_objs);
1247         fprintf(f, "    get_fail_bulk=%"PRIu64"\n", sum.get_fail_bulk);
1248         fprintf(f, "    get_fail_objs=%"PRIu64"\n", sum.get_fail_objs);
1249 #else
1250         fprintf(f, "  no statistics available\n");
1251 #endif
1252
1253         rte_mempool_audit(mp);
1254 }
1255
1256 /* dump the status of all mempools on the console */
1257 void
1258 rte_mempool_list_dump(FILE *f)
1259 {
1260         struct rte_mempool *mp = NULL;
1261         struct rte_tailq_entry *te;
1262         struct rte_mempool_list *mempool_list;
1263
1264         mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
1265
1266         rte_rwlock_read_lock(RTE_EAL_MEMPOOL_RWLOCK);
1267
1268         TAILQ_FOREACH(te, mempool_list, next) {
1269                 mp = (struct rte_mempool *) te->data;
1270                 rte_mempool_dump(f, mp);
1271         }
1272
1273         rte_rwlock_read_unlock(RTE_EAL_MEMPOOL_RWLOCK);
1274 }
1275
1276 /* search a mempool from its name */
1277 struct rte_mempool *
1278 rte_mempool_lookup(const char *name)
1279 {
1280         struct rte_mempool *mp = NULL;
1281         struct rte_tailq_entry *te;
1282         struct rte_mempool_list *mempool_list;
1283
1284         mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
1285
1286         rte_rwlock_read_lock(RTE_EAL_MEMPOOL_RWLOCK);
1287
1288         TAILQ_FOREACH(te, mempool_list, next) {
1289                 mp = (struct rte_mempool *) te->data;
1290                 if (strncmp(name, mp->name, RTE_MEMPOOL_NAMESIZE) == 0)
1291                         break;
1292         }
1293
1294         rte_rwlock_read_unlock(RTE_EAL_MEMPOOL_RWLOCK);
1295
1296         if (te == NULL) {
1297                 rte_errno = ENOENT;
1298                 return NULL;
1299         }
1300
1301         return mp;
1302 }
1303
1304 void rte_mempool_walk(void (*func)(struct rte_mempool *, void *),
1305                       void *arg)
1306 {
1307         struct rte_tailq_entry *te = NULL;
1308         struct rte_mempool_list *mempool_list;
1309         void *tmp_te;
1310
1311         mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
1312
1313         rte_rwlock_read_lock(RTE_EAL_MEMPOOL_RWLOCK);
1314
1315         TAILQ_FOREACH_SAFE(te, mempool_list, next, tmp_te) {
1316                 (*func)((struct rte_mempool *) te->data, arg);
1317         }
1318
1319         rte_rwlock_read_unlock(RTE_EAL_MEMPOOL_RWLOCK);
1320 }