mempool: get capabilities
[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         memhdr = rte_zmalloc("MEMPOOL_MEMHDR", sizeof(*memhdr), 0);
373         if (memhdr == NULL)
374                 return -ENOMEM;
375
376         memhdr->mp = mp;
377         memhdr->addr = vaddr;
378         memhdr->phys_addr = paddr;
379         memhdr->len = len;
380         memhdr->free_cb = free_cb;
381         memhdr->opaque = opaque;
382
383         if (mp->flags & MEMPOOL_F_NO_CACHE_ALIGN)
384                 off = RTE_PTR_ALIGN_CEIL(vaddr, 8) - vaddr;
385         else
386                 off = RTE_PTR_ALIGN_CEIL(vaddr, RTE_CACHE_LINE_SIZE) - vaddr;
387
388         while (off + total_elt_sz <= len && mp->populated_size < mp->size) {
389                 off += mp->header_size;
390                 if (paddr == RTE_BAD_PHYS_ADDR)
391                         mempool_add_elem(mp, (char *)vaddr + off,
392                                 RTE_BAD_PHYS_ADDR);
393                 else
394                         mempool_add_elem(mp, (char *)vaddr + off, paddr + off);
395                 off += mp->elt_size + mp->trailer_size;
396                 i++;
397         }
398
399         /* not enough room to store one object */
400         if (i == 0)
401                 return -EINVAL;
402
403         STAILQ_INSERT_TAIL(&mp->mem_list, memhdr, next);
404         mp->nb_mem_chunks++;
405         return i;
406 }
407
408 /* Add objects in the pool, using a table of physical pages. Return the
409  * number of objects added, or a negative value on error.
410  */
411 int
412 rte_mempool_populate_phys_tab(struct rte_mempool *mp, char *vaddr,
413         const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift,
414         rte_mempool_memchunk_free_cb_t *free_cb, void *opaque)
415 {
416         uint32_t i, n;
417         int ret, cnt = 0;
418         size_t pg_sz = (size_t)1 << pg_shift;
419
420         /* mempool must not be populated */
421         if (mp->nb_mem_chunks != 0)
422                 return -EEXIST;
423
424         if (mp->flags & MEMPOOL_F_NO_PHYS_CONTIG)
425                 return rte_mempool_populate_phys(mp, vaddr, RTE_BAD_PHYS_ADDR,
426                         pg_num * pg_sz, free_cb, opaque);
427
428         for (i = 0; i < pg_num && mp->populated_size < mp->size; i += n) {
429
430                 /* populate with the largest group of contiguous pages */
431                 for (n = 1; (i + n) < pg_num &&
432                              paddr[i + n - 1] + pg_sz == paddr[i + n]; n++)
433                         ;
434
435                 ret = rte_mempool_populate_phys(mp, vaddr + i * pg_sz,
436                         paddr[i], n * pg_sz, free_cb, opaque);
437                 if (ret < 0) {
438                         rte_mempool_free_memchunks(mp);
439                         return ret;
440                 }
441                 /* no need to call the free callback for next chunks */
442                 free_cb = NULL;
443                 cnt += ret;
444         }
445         return cnt;
446 }
447
448 /* Populate the mempool with a virtual area. Return the number of
449  * objects added, or a negative value on error.
450  */
451 int
452 rte_mempool_populate_virt(struct rte_mempool *mp, char *addr,
453         size_t len, size_t pg_sz, rte_mempool_memchunk_free_cb_t *free_cb,
454         void *opaque)
455 {
456         phys_addr_t paddr;
457         size_t off, phys_len;
458         int ret, cnt = 0;
459
460         /* mempool must not be populated */
461         if (mp->nb_mem_chunks != 0)
462                 return -EEXIST;
463         /* address and len must be page-aligned */
464         if (RTE_PTR_ALIGN_CEIL(addr, pg_sz) != addr)
465                 return -EINVAL;
466         if (RTE_ALIGN_CEIL(len, pg_sz) != len)
467                 return -EINVAL;
468
469         if (mp->flags & MEMPOOL_F_NO_PHYS_CONTIG)
470                 return rte_mempool_populate_phys(mp, addr, RTE_BAD_PHYS_ADDR,
471                         len, free_cb, opaque);
472
473         for (off = 0; off + pg_sz <= len &&
474                      mp->populated_size < mp->size; off += phys_len) {
475
476                 paddr = rte_mem_virt2phy(addr + off);
477                 /* required for xen_dom0 to get the machine address */
478                 paddr = rte_mem_phy2mch(-1, paddr);
479
480                 if (paddr == RTE_BAD_PHYS_ADDR && rte_eal_has_hugepages()) {
481                         ret = -EINVAL;
482                         goto fail;
483                 }
484
485                 /* populate with the largest group of contiguous pages */
486                 for (phys_len = pg_sz; off + phys_len < len; phys_len += pg_sz) {
487                         phys_addr_t paddr_tmp;
488
489                         paddr_tmp = rte_mem_virt2phy(addr + off + phys_len);
490                         paddr_tmp = rte_mem_phy2mch(-1, paddr_tmp);
491
492                         if (paddr_tmp != paddr + phys_len)
493                                 break;
494                 }
495
496                 ret = rte_mempool_populate_phys(mp, addr + off, paddr,
497                         phys_len, free_cb, opaque);
498                 if (ret < 0)
499                         goto fail;
500                 /* no need to call the free callback for next chunks */
501                 free_cb = NULL;
502                 cnt += ret;
503         }
504
505         return cnt;
506
507  fail:
508         rte_mempool_free_memchunks(mp);
509         return ret;
510 }
511
512 /* Default function to populate the mempool: allocate memory in memzones,
513  * and populate them. Return the number of objects added, or a negative
514  * value on error.
515  */
516 int
517 rte_mempool_populate_default(struct rte_mempool *mp)
518 {
519         unsigned int mz_flags = RTE_MEMZONE_1GB|RTE_MEMZONE_SIZE_HINT_ONLY;
520         char mz_name[RTE_MEMZONE_NAMESIZE];
521         const struct rte_memzone *mz;
522         size_t size, total_elt_sz, align, pg_sz, pg_shift;
523         phys_addr_t paddr;
524         unsigned mz_id, n;
525         unsigned int mp_flags;
526         int ret;
527
528         /* mempool must not be populated */
529         if (mp->nb_mem_chunks != 0)
530                 return -EEXIST;
531
532         /* Get mempool capabilities */
533         mp_flags = 0;
534         ret = rte_mempool_ops_get_capabilities(mp, &mp_flags);
535         if (ret == -ENOTSUP)
536                 RTE_LOG(DEBUG, MEMPOOL, "get_capability not supported for %s\n",
537                                         mp->name);
538         else if (ret < 0)
539                 return ret;
540
541         /* update mempool capabilities */
542         mp->flags |= mp_flags;
543
544         if (rte_xen_dom0_supported()) {
545                 pg_sz = RTE_PGSIZE_2M;
546                 pg_shift = rte_bsf32(pg_sz);
547                 align = pg_sz;
548         } else if (rte_eal_has_hugepages()) {
549                 pg_shift = 0; /* not needed, zone is physically contiguous */
550                 pg_sz = 0;
551                 align = RTE_CACHE_LINE_SIZE;
552         } else {
553                 pg_sz = getpagesize();
554                 pg_shift = rte_bsf32(pg_sz);
555                 align = pg_sz;
556         }
557
558         total_elt_sz = mp->header_size + mp->elt_size + mp->trailer_size;
559         for (mz_id = 0, n = mp->size; n > 0; mz_id++, n -= ret) {
560                 size = rte_mempool_xmem_size(n, total_elt_sz, pg_shift,
561                                                 mp->flags);
562
563                 ret = snprintf(mz_name, sizeof(mz_name),
564                         RTE_MEMPOOL_MZ_FORMAT "_%d", mp->name, mz_id);
565                 if (ret < 0 || ret >= (int)sizeof(mz_name)) {
566                         ret = -ENAMETOOLONG;
567                         goto fail;
568                 }
569
570                 mz = rte_memzone_reserve_aligned(mz_name, size,
571                         mp->socket_id, mz_flags, align);
572                 /* not enough memory, retry with the biggest zone we have */
573                 if (mz == NULL)
574                         mz = rte_memzone_reserve_aligned(mz_name, 0,
575                                 mp->socket_id, mz_flags, align);
576                 if (mz == NULL) {
577                         ret = -rte_errno;
578                         goto fail;
579                 }
580
581                 if (mp->flags & MEMPOOL_F_NO_PHYS_CONTIG)
582                         paddr = RTE_BAD_PHYS_ADDR;
583                 else
584                         paddr = mz->phys_addr;
585
586                 if (rte_eal_has_hugepages() && !rte_xen_dom0_supported())
587                         ret = rte_mempool_populate_phys(mp, mz->addr,
588                                 paddr, mz->len,
589                                 rte_mempool_memchunk_mz_free,
590                                 (void *)(uintptr_t)mz);
591                 else
592                         ret = rte_mempool_populate_virt(mp, mz->addr,
593                                 mz->len, pg_sz,
594                                 rte_mempool_memchunk_mz_free,
595                                 (void *)(uintptr_t)mz);
596                 if (ret < 0) {
597                         rte_memzone_free(mz);
598                         goto fail;
599                 }
600         }
601
602         return mp->size;
603
604  fail:
605         rte_mempool_free_memchunks(mp);
606         return ret;
607 }
608
609 /* return the memory size required for mempool objects in anonymous mem */
610 static size_t
611 get_anon_size(const struct rte_mempool *mp)
612 {
613         size_t size, total_elt_sz, pg_sz, pg_shift;
614
615         pg_sz = getpagesize();
616         pg_shift = rte_bsf32(pg_sz);
617         total_elt_sz = mp->header_size + mp->elt_size + mp->trailer_size;
618         size = rte_mempool_xmem_size(mp->size, total_elt_sz, pg_shift,
619                                         mp->flags);
620
621         return size;
622 }
623
624 /* unmap a memory zone mapped by rte_mempool_populate_anon() */
625 static void
626 rte_mempool_memchunk_anon_free(struct rte_mempool_memhdr *memhdr,
627         void *opaque)
628 {
629         munmap(opaque, get_anon_size(memhdr->mp));
630 }
631
632 /* populate the mempool with an anonymous mapping */
633 int
634 rte_mempool_populate_anon(struct rte_mempool *mp)
635 {
636         size_t size;
637         int ret;
638         char *addr;
639
640         /* mempool is already populated, error */
641         if (!STAILQ_EMPTY(&mp->mem_list)) {
642                 rte_errno = EINVAL;
643                 return 0;
644         }
645
646         /* get chunk of virtually continuous memory */
647         size = get_anon_size(mp);
648         addr = mmap(NULL, size, PROT_READ | PROT_WRITE,
649                 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
650         if (addr == MAP_FAILED) {
651                 rte_errno = errno;
652                 return 0;
653         }
654         /* can't use MMAP_LOCKED, it does not exist on BSD */
655         if (mlock(addr, size) < 0) {
656                 rte_errno = errno;
657                 munmap(addr, size);
658                 return 0;
659         }
660
661         ret = rte_mempool_populate_virt(mp, addr, size, getpagesize(),
662                 rte_mempool_memchunk_anon_free, addr);
663         if (ret == 0)
664                 goto fail;
665
666         return mp->populated_size;
667
668  fail:
669         rte_mempool_free_memchunks(mp);
670         return 0;
671 }
672
673 /* free a mempool */
674 void
675 rte_mempool_free(struct rte_mempool *mp)
676 {
677         struct rte_mempool_list *mempool_list = NULL;
678         struct rte_tailq_entry *te;
679
680         if (mp == NULL)
681                 return;
682
683         mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
684         rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
685         /* find out tailq entry */
686         TAILQ_FOREACH(te, mempool_list, next) {
687                 if (te->data == (void *)mp)
688                         break;
689         }
690
691         if (te != NULL) {
692                 TAILQ_REMOVE(mempool_list, te, next);
693                 rte_free(te);
694         }
695         rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
696
697         rte_mempool_free_memchunks(mp);
698         rte_mempool_ops_free(mp);
699         rte_memzone_free(mp->mz);
700 }
701
702 static void
703 mempool_cache_init(struct rte_mempool_cache *cache, uint32_t size)
704 {
705         cache->size = size;
706         cache->flushthresh = CALC_CACHE_FLUSHTHRESH(size);
707         cache->len = 0;
708 }
709
710 /*
711  * Create and initialize a cache for objects that are retrieved from and
712  * returned to an underlying mempool. This structure is identical to the
713  * local_cache[lcore_id] pointed to by the mempool structure.
714  */
715 struct rte_mempool_cache *
716 rte_mempool_cache_create(uint32_t size, int socket_id)
717 {
718         struct rte_mempool_cache *cache;
719
720         if (size == 0 || size > RTE_MEMPOOL_CACHE_MAX_SIZE) {
721                 rte_errno = EINVAL;
722                 return NULL;
723         }
724
725         cache = rte_zmalloc_socket("MEMPOOL_CACHE", sizeof(*cache),
726                                   RTE_CACHE_LINE_SIZE, socket_id);
727         if (cache == NULL) {
728                 RTE_LOG(ERR, MEMPOOL, "Cannot allocate mempool cache.\n");
729                 rte_errno = ENOMEM;
730                 return NULL;
731         }
732
733         mempool_cache_init(cache, size);
734
735         return cache;
736 }
737
738 /*
739  * Free a cache. It's the responsibility of the user to make sure that any
740  * remaining objects in the cache are flushed to the corresponding
741  * mempool.
742  */
743 void
744 rte_mempool_cache_free(struct rte_mempool_cache *cache)
745 {
746         rte_free(cache);
747 }
748
749 /* create an empty mempool */
750 struct rte_mempool *
751 rte_mempool_create_empty(const char *name, unsigned n, unsigned elt_size,
752         unsigned cache_size, unsigned private_data_size,
753         int socket_id, unsigned flags)
754 {
755         char mz_name[RTE_MEMZONE_NAMESIZE];
756         struct rte_mempool_list *mempool_list;
757         struct rte_mempool *mp = NULL;
758         struct rte_tailq_entry *te = NULL;
759         const struct rte_memzone *mz = NULL;
760         size_t mempool_size;
761         unsigned int mz_flags = RTE_MEMZONE_1GB|RTE_MEMZONE_SIZE_HINT_ONLY;
762         struct rte_mempool_objsz objsz;
763         unsigned lcore_id;
764         int ret;
765
766         /* compilation-time checks */
767         RTE_BUILD_BUG_ON((sizeof(struct rte_mempool) &
768                           RTE_CACHE_LINE_MASK) != 0);
769         RTE_BUILD_BUG_ON((sizeof(struct rte_mempool_cache) &
770                           RTE_CACHE_LINE_MASK) != 0);
771 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
772         RTE_BUILD_BUG_ON((sizeof(struct rte_mempool_debug_stats) &
773                           RTE_CACHE_LINE_MASK) != 0);
774         RTE_BUILD_BUG_ON((offsetof(struct rte_mempool, stats) &
775                           RTE_CACHE_LINE_MASK) != 0);
776 #endif
777
778         mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
779
780         /* asked cache too big */
781         if (cache_size > RTE_MEMPOOL_CACHE_MAX_SIZE ||
782             CALC_CACHE_FLUSHTHRESH(cache_size) > n) {
783                 rte_errno = EINVAL;
784                 return NULL;
785         }
786
787         /* "no cache align" imply "no spread" */
788         if (flags & MEMPOOL_F_NO_CACHE_ALIGN)
789                 flags |= MEMPOOL_F_NO_SPREAD;
790
791         /* calculate mempool object sizes. */
792         if (!rte_mempool_calc_obj_size(elt_size, flags, &objsz)) {
793                 rte_errno = EINVAL;
794                 return NULL;
795         }
796
797         rte_rwlock_write_lock(RTE_EAL_MEMPOOL_RWLOCK);
798
799         /*
800          * reserve a memory zone for this mempool: private data is
801          * cache-aligned
802          */
803         private_data_size = (private_data_size +
804                              RTE_MEMPOOL_ALIGN_MASK) & (~RTE_MEMPOOL_ALIGN_MASK);
805
806
807         /* try to allocate tailq entry */
808         te = rte_zmalloc("MEMPOOL_TAILQ_ENTRY", sizeof(*te), 0);
809         if (te == NULL) {
810                 RTE_LOG(ERR, MEMPOOL, "Cannot allocate tailq entry!\n");
811                 goto exit_unlock;
812         }
813
814         mempool_size = MEMPOOL_HEADER_SIZE(mp, cache_size);
815         mempool_size += private_data_size;
816         mempool_size = RTE_ALIGN_CEIL(mempool_size, RTE_MEMPOOL_ALIGN);
817
818         ret = snprintf(mz_name, sizeof(mz_name), RTE_MEMPOOL_MZ_FORMAT, name);
819         if (ret < 0 || ret >= (int)sizeof(mz_name)) {
820                 rte_errno = ENAMETOOLONG;
821                 goto exit_unlock;
822         }
823
824         mz = rte_memzone_reserve(mz_name, mempool_size, socket_id, mz_flags);
825         if (mz == NULL)
826                 goto exit_unlock;
827
828         /* init the mempool structure */
829         mp = mz->addr;
830         memset(mp, 0, MEMPOOL_HEADER_SIZE(mp, cache_size));
831         ret = snprintf(mp->name, sizeof(mp->name), "%s", name);
832         if (ret < 0 || ret >= (int)sizeof(mp->name)) {
833                 rte_errno = ENAMETOOLONG;
834                 goto exit_unlock;
835         }
836         mp->mz = mz;
837         mp->size = n;
838         mp->flags = flags;
839         mp->socket_id = socket_id;
840         mp->elt_size = objsz.elt_size;
841         mp->header_size = objsz.header_size;
842         mp->trailer_size = objsz.trailer_size;
843         /* Size of default caches, zero means disabled. */
844         mp->cache_size = cache_size;
845         mp->private_data_size = private_data_size;
846         STAILQ_INIT(&mp->elt_list);
847         STAILQ_INIT(&mp->mem_list);
848
849         /*
850          * local_cache pointer is set even if cache_size is zero.
851          * The local_cache points to just past the elt_pa[] array.
852          */
853         mp->local_cache = (struct rte_mempool_cache *)
854                 RTE_PTR_ADD(mp, MEMPOOL_HEADER_SIZE(mp, 0));
855
856         /* Init all default caches. */
857         if (cache_size != 0) {
858                 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++)
859                         mempool_cache_init(&mp->local_cache[lcore_id],
860                                            cache_size);
861         }
862
863         te->data = mp;
864
865         rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
866         TAILQ_INSERT_TAIL(mempool_list, te, next);
867         rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
868         rte_rwlock_write_unlock(RTE_EAL_MEMPOOL_RWLOCK);
869
870         return mp;
871
872 exit_unlock:
873         rte_rwlock_write_unlock(RTE_EAL_MEMPOOL_RWLOCK);
874         rte_free(te);
875         rte_mempool_free(mp);
876         return NULL;
877 }
878
879 /* create the mempool */
880 struct rte_mempool *
881 rte_mempool_create(const char *name, unsigned n, unsigned elt_size,
882         unsigned cache_size, unsigned private_data_size,
883         rte_mempool_ctor_t *mp_init, void *mp_init_arg,
884         rte_mempool_obj_cb_t *obj_init, void *obj_init_arg,
885         int socket_id, unsigned flags)
886 {
887         int ret;
888         struct rte_mempool *mp;
889
890         mp = rte_mempool_create_empty(name, n, elt_size, cache_size,
891                 private_data_size, socket_id, flags);
892         if (mp == NULL)
893                 return NULL;
894
895         /*
896          * Since we have 4 combinations of the SP/SC/MP/MC examine the flags to
897          * set the correct index into the table of ops structs.
898          */
899         if ((flags & MEMPOOL_F_SP_PUT) && (flags & MEMPOOL_F_SC_GET))
900                 ret = rte_mempool_set_ops_byname(mp, "ring_sp_sc", NULL);
901         else if (flags & MEMPOOL_F_SP_PUT)
902                 ret = rte_mempool_set_ops_byname(mp, "ring_sp_mc", NULL);
903         else if (flags & MEMPOOL_F_SC_GET)
904                 ret = rte_mempool_set_ops_byname(mp, "ring_mp_sc", NULL);
905         else
906                 ret = rte_mempool_set_ops_byname(mp, "ring_mp_mc", NULL);
907
908         if (ret)
909                 goto fail;
910
911         /* call the mempool priv initializer */
912         if (mp_init)
913                 mp_init(mp, mp_init_arg);
914
915         if (rte_mempool_populate_default(mp) < 0)
916                 goto fail;
917
918         /* call the object initializers */
919         if (obj_init)
920                 rte_mempool_obj_iter(mp, obj_init, obj_init_arg);
921
922         return mp;
923
924  fail:
925         rte_mempool_free(mp);
926         return NULL;
927 }
928
929 /*
930  * Create the mempool over already allocated chunk of memory.
931  * That external memory buffer can consists of physically disjoint pages.
932  * Setting vaddr to NULL, makes mempool to fallback to rte_mempool_create()
933  * behavior.
934  */
935 struct rte_mempool *
936 rte_mempool_xmem_create(const char *name, unsigned n, unsigned elt_size,
937                 unsigned cache_size, unsigned private_data_size,
938                 rte_mempool_ctor_t *mp_init, void *mp_init_arg,
939                 rte_mempool_obj_cb_t *obj_init, void *obj_init_arg,
940                 int socket_id, unsigned flags, void *vaddr,
941                 const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift)
942 {
943         struct rte_mempool *mp = NULL;
944         int ret;
945
946         /* no virtual address supplied, use rte_mempool_create() */
947         if (vaddr == NULL)
948                 return rte_mempool_create(name, n, elt_size, cache_size,
949                         private_data_size, mp_init, mp_init_arg,
950                         obj_init, obj_init_arg, socket_id, flags);
951
952         /* check that we have both VA and PA */
953         if (paddr == NULL) {
954                 rte_errno = EINVAL;
955                 return NULL;
956         }
957
958         /* Check that pg_shift parameter is valid. */
959         if (pg_shift > MEMPOOL_PG_SHIFT_MAX) {
960                 rte_errno = EINVAL;
961                 return NULL;
962         }
963
964         mp = rte_mempool_create_empty(name, n, elt_size, cache_size,
965                 private_data_size, socket_id, flags);
966         if (mp == NULL)
967                 return NULL;
968
969         /* call the mempool priv initializer */
970         if (mp_init)
971                 mp_init(mp, mp_init_arg);
972
973         ret = rte_mempool_populate_phys_tab(mp, vaddr, paddr, pg_num, pg_shift,
974                 NULL, NULL);
975         if (ret < 0 || ret != (int)mp->size)
976                 goto fail;
977
978         /* call the object initializers */
979         if (obj_init)
980                 rte_mempool_obj_iter(mp, obj_init, obj_init_arg);
981
982         return mp;
983
984  fail:
985         rte_mempool_free(mp);
986         return NULL;
987 }
988
989 /* Return the number of entries in the mempool */
990 unsigned int
991 rte_mempool_avail_count(const struct rte_mempool *mp)
992 {
993         unsigned count;
994         unsigned lcore_id;
995
996         count = rte_mempool_ops_get_count(mp);
997
998         if (mp->cache_size == 0)
999                 return count;
1000
1001         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++)
1002                 count += mp->local_cache[lcore_id].len;
1003
1004         /*
1005          * due to race condition (access to len is not locked), the
1006          * total can be greater than size... so fix the result
1007          */
1008         if (count > mp->size)
1009                 return mp->size;
1010         return count;
1011 }
1012
1013 /* return the number of entries allocated from the mempool */
1014 unsigned int
1015 rte_mempool_in_use_count(const struct rte_mempool *mp)
1016 {
1017         return mp->size - rte_mempool_avail_count(mp);
1018 }
1019
1020 /* dump the cache status */
1021 static unsigned
1022 rte_mempool_dump_cache(FILE *f, const struct rte_mempool *mp)
1023 {
1024         unsigned lcore_id;
1025         unsigned count = 0;
1026         unsigned cache_count;
1027
1028         fprintf(f, "  internal cache infos:\n");
1029         fprintf(f, "    cache_size=%"PRIu32"\n", mp->cache_size);
1030
1031         if (mp->cache_size == 0)
1032                 return count;
1033
1034         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1035                 cache_count = mp->local_cache[lcore_id].len;
1036                 fprintf(f, "    cache_count[%u]=%"PRIu32"\n",
1037                         lcore_id, cache_count);
1038                 count += cache_count;
1039         }
1040         fprintf(f, "    total_cache_count=%u\n", count);
1041         return count;
1042 }
1043
1044 #ifndef __INTEL_COMPILER
1045 #pragma GCC diagnostic ignored "-Wcast-qual"
1046 #endif
1047
1048 /* check and update cookies or panic (internal) */
1049 void rte_mempool_check_cookies(const struct rte_mempool *mp,
1050         void * const *obj_table_const, unsigned n, int free)
1051 {
1052 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
1053         struct rte_mempool_objhdr *hdr;
1054         struct rte_mempool_objtlr *tlr;
1055         uint64_t cookie;
1056         void *tmp;
1057         void *obj;
1058         void **obj_table;
1059
1060         /* Force to drop the "const" attribute. This is done only when
1061          * DEBUG is enabled */
1062         tmp = (void *) obj_table_const;
1063         obj_table = tmp;
1064
1065         while (n--) {
1066                 obj = obj_table[n];
1067
1068                 if (rte_mempool_from_obj(obj) != mp)
1069                         rte_panic("MEMPOOL: object is owned by another "
1070                                   "mempool\n");
1071
1072                 hdr = __mempool_get_header(obj);
1073                 cookie = hdr->cookie;
1074
1075                 if (free == 0) {
1076                         if (cookie != RTE_MEMPOOL_HEADER_COOKIE1) {
1077                                 RTE_LOG(CRIT, MEMPOOL,
1078                                         "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
1079                                         obj, (const void *) mp, cookie);
1080                                 rte_panic("MEMPOOL: bad header cookie (put)\n");
1081                         }
1082                         hdr->cookie = RTE_MEMPOOL_HEADER_COOKIE2;
1083                 } else if (free == 1) {
1084                         if (cookie != RTE_MEMPOOL_HEADER_COOKIE2) {
1085                                 RTE_LOG(CRIT, MEMPOOL,
1086                                         "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
1087                                         obj, (const void *) mp, cookie);
1088                                 rte_panic("MEMPOOL: bad header cookie (get)\n");
1089                         }
1090                         hdr->cookie = RTE_MEMPOOL_HEADER_COOKIE1;
1091                 } else if (free == 2) {
1092                         if (cookie != RTE_MEMPOOL_HEADER_COOKIE1 &&
1093                             cookie != RTE_MEMPOOL_HEADER_COOKIE2) {
1094                                 RTE_LOG(CRIT, MEMPOOL,
1095                                         "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
1096                                         obj, (const void *) mp, cookie);
1097                                 rte_panic("MEMPOOL: bad header cookie (audit)\n");
1098                         }
1099                 }
1100                 tlr = __mempool_get_trailer(obj);
1101                 cookie = tlr->cookie;
1102                 if (cookie != RTE_MEMPOOL_TRAILER_COOKIE) {
1103                         RTE_LOG(CRIT, MEMPOOL,
1104                                 "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
1105                                 obj, (const void *) mp, cookie);
1106                         rte_panic("MEMPOOL: bad trailer cookie\n");
1107                 }
1108         }
1109 #else
1110         RTE_SET_USED(mp);
1111         RTE_SET_USED(obj_table_const);
1112         RTE_SET_USED(n);
1113         RTE_SET_USED(free);
1114 #endif
1115 }
1116
1117 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
1118 static void
1119 mempool_obj_audit(struct rte_mempool *mp, __rte_unused void *opaque,
1120         void *obj, __rte_unused unsigned idx)
1121 {
1122         __mempool_check_cookies(mp, &obj, 1, 2);
1123 }
1124
1125 static void
1126 mempool_audit_cookies(struct rte_mempool *mp)
1127 {
1128         unsigned num;
1129
1130         num = rte_mempool_obj_iter(mp, mempool_obj_audit, NULL);
1131         if (num != mp->size) {
1132                 rte_panic("rte_mempool_obj_iter(mempool=%p, size=%u) "
1133                         "iterated only over %u elements\n",
1134                         mp, mp->size, num);
1135         }
1136 }
1137 #else
1138 #define mempool_audit_cookies(mp) do {} while(0)
1139 #endif
1140
1141 #ifndef __INTEL_COMPILER
1142 #pragma GCC diagnostic error "-Wcast-qual"
1143 #endif
1144
1145 /* check cookies before and after objects */
1146 static void
1147 mempool_audit_cache(const struct rte_mempool *mp)
1148 {
1149         /* check cache size consistency */
1150         unsigned lcore_id;
1151
1152         if (mp->cache_size == 0)
1153                 return;
1154
1155         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1156                 const struct rte_mempool_cache *cache;
1157                 cache = &mp->local_cache[lcore_id];
1158                 if (cache->len > cache->flushthresh) {
1159                         RTE_LOG(CRIT, MEMPOOL, "badness on cache[%u]\n",
1160                                 lcore_id);
1161                         rte_panic("MEMPOOL: invalid cache len\n");
1162                 }
1163         }
1164 }
1165
1166 /* check the consistency of mempool (size, cookies, ...) */
1167 void
1168 rte_mempool_audit(struct rte_mempool *mp)
1169 {
1170         mempool_audit_cache(mp);
1171         mempool_audit_cookies(mp);
1172
1173         /* For case where mempool DEBUG is not set, and cache size is 0 */
1174         RTE_SET_USED(mp);
1175 }
1176
1177 /* dump the status of the mempool on the console */
1178 void
1179 rte_mempool_dump(FILE *f, struct rte_mempool *mp)
1180 {
1181 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
1182         struct rte_mempool_debug_stats sum;
1183         unsigned lcore_id;
1184 #endif
1185         struct rte_mempool_memhdr *memhdr;
1186         unsigned common_count;
1187         unsigned cache_count;
1188         size_t mem_len = 0;
1189
1190         RTE_ASSERT(f != NULL);
1191         RTE_ASSERT(mp != NULL);
1192
1193         fprintf(f, "mempool <%s>@%p\n", mp->name, mp);
1194         fprintf(f, "  flags=%x\n", mp->flags);
1195         fprintf(f, "  pool=%p\n", mp->pool_data);
1196         fprintf(f, "  phys_addr=0x%" PRIx64 "\n", mp->mz->phys_addr);
1197         fprintf(f, "  nb_mem_chunks=%u\n", mp->nb_mem_chunks);
1198         fprintf(f, "  size=%"PRIu32"\n", mp->size);
1199         fprintf(f, "  populated_size=%"PRIu32"\n", mp->populated_size);
1200         fprintf(f, "  header_size=%"PRIu32"\n", mp->header_size);
1201         fprintf(f, "  elt_size=%"PRIu32"\n", mp->elt_size);
1202         fprintf(f, "  trailer_size=%"PRIu32"\n", mp->trailer_size);
1203         fprintf(f, "  total_obj_size=%"PRIu32"\n",
1204                mp->header_size + mp->elt_size + mp->trailer_size);
1205
1206         fprintf(f, "  private_data_size=%"PRIu32"\n", mp->private_data_size);
1207
1208         STAILQ_FOREACH(memhdr, &mp->mem_list, next)
1209                 mem_len += memhdr->len;
1210         if (mem_len != 0) {
1211                 fprintf(f, "  avg bytes/object=%#Lf\n",
1212                         (long double)mem_len / mp->size);
1213         }
1214
1215         cache_count = rte_mempool_dump_cache(f, mp);
1216         common_count = rte_mempool_ops_get_count(mp);
1217         if ((cache_count + common_count) > mp->size)
1218                 common_count = mp->size - cache_count;
1219         fprintf(f, "  common_pool_count=%u\n", common_count);
1220
1221         /* sum and dump statistics */
1222 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
1223         memset(&sum, 0, sizeof(sum));
1224         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1225                 sum.put_bulk += mp->stats[lcore_id].put_bulk;
1226                 sum.put_objs += mp->stats[lcore_id].put_objs;
1227                 sum.get_success_bulk += mp->stats[lcore_id].get_success_bulk;
1228                 sum.get_success_objs += mp->stats[lcore_id].get_success_objs;
1229                 sum.get_fail_bulk += mp->stats[lcore_id].get_fail_bulk;
1230                 sum.get_fail_objs += mp->stats[lcore_id].get_fail_objs;
1231         }
1232         fprintf(f, "  stats:\n");
1233         fprintf(f, "    put_bulk=%"PRIu64"\n", sum.put_bulk);
1234         fprintf(f, "    put_objs=%"PRIu64"\n", sum.put_objs);
1235         fprintf(f, "    get_success_bulk=%"PRIu64"\n", sum.get_success_bulk);
1236         fprintf(f, "    get_success_objs=%"PRIu64"\n", sum.get_success_objs);
1237         fprintf(f, "    get_fail_bulk=%"PRIu64"\n", sum.get_fail_bulk);
1238         fprintf(f, "    get_fail_objs=%"PRIu64"\n", sum.get_fail_objs);
1239 #else
1240         fprintf(f, "  no statistics available\n");
1241 #endif
1242
1243         rte_mempool_audit(mp);
1244 }
1245
1246 /* dump the status of all mempools on the console */
1247 void
1248 rte_mempool_list_dump(FILE *f)
1249 {
1250         struct rte_mempool *mp = NULL;
1251         struct rte_tailq_entry *te;
1252         struct rte_mempool_list *mempool_list;
1253
1254         mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
1255
1256         rte_rwlock_read_lock(RTE_EAL_MEMPOOL_RWLOCK);
1257
1258         TAILQ_FOREACH(te, mempool_list, next) {
1259                 mp = (struct rte_mempool *) te->data;
1260                 rte_mempool_dump(f, mp);
1261         }
1262
1263         rte_rwlock_read_unlock(RTE_EAL_MEMPOOL_RWLOCK);
1264 }
1265
1266 /* search a mempool from its name */
1267 struct rte_mempool *
1268 rte_mempool_lookup(const char *name)
1269 {
1270         struct rte_mempool *mp = NULL;
1271         struct rte_tailq_entry *te;
1272         struct rte_mempool_list *mempool_list;
1273
1274         mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
1275
1276         rte_rwlock_read_lock(RTE_EAL_MEMPOOL_RWLOCK);
1277
1278         TAILQ_FOREACH(te, mempool_list, next) {
1279                 mp = (struct rte_mempool *) te->data;
1280                 if (strncmp(name, mp->name, RTE_MEMPOOL_NAMESIZE) == 0)
1281                         break;
1282         }
1283
1284         rte_rwlock_read_unlock(RTE_EAL_MEMPOOL_RWLOCK);
1285
1286         if (te == NULL) {
1287                 rte_errno = ENOENT;
1288                 return NULL;
1289         }
1290
1291         return mp;
1292 }
1293
1294 void rte_mempool_walk(void (*func)(struct rte_mempool *, void *),
1295                       void *arg)
1296 {
1297         struct rte_tailq_entry *te = NULL;
1298         struct rte_mempool_list *mempool_list;
1299         void *tmp_te;
1300
1301         mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
1302
1303         rte_rwlock_read_lock(RTE_EAL_MEMPOOL_RWLOCK);
1304
1305         TAILQ_FOREACH_SAFE(te, mempool_list, next, tmp_te) {
1306                 (*func)((struct rte_mempool *) te->data, arg);
1307         }
1308
1309         rte_rwlock_read_unlock(RTE_EAL_MEMPOOL_RWLOCK);
1310 }