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