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