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