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