mempool: fix mempool virt populate with small chunks
[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
509         ret = mempool_ops_alloc_once(mp);
510         if (ret != 0)
511                 return ret;
512
513         /* mempool must not be populated */
514         if (mp->nb_mem_chunks != 0)
515                 return -EEXIST;
516
517         /*
518          * the following section calculates page shift and page size values.
519          *
520          * these values impact the result of calc_mem_size operation, which
521          * returns the amount of memory that should be allocated to store the
522          * desired number of objects. when not zero, it allocates more memory
523          * for the padding between objects, to ensure that an object does not
524          * cross a page boundary. in other words, page size/shift are to be set
525          * to zero if mempool elements won't care about page boundaries.
526          * there are several considerations for page size and page shift here.
527          *
528          * if we don't need our mempools to have physically contiguous objects,
529          * then just set page shift and page size to 0, because the user has
530          * indicated that there's no need to care about anything.
531          *
532          * if we do need contiguous objects (if a mempool driver has its
533          * own calc_size() method returning min_chunk_size = mem_size),
534          * there is also an option to reserve the entire mempool memory
535          * as one contiguous block of memory.
536          *
537          * if we require contiguous objects, but not necessarily the entire
538          * mempool reserved space to be contiguous, pg_sz will be != 0,
539          * and the default ops->populate() will take care of not placing
540          * objects across pages.
541          *
542          * if our IO addresses are physical, we may get memory from bigger
543          * pages, or we might get memory from smaller pages, and how much of it
544          * we require depends on whether we want bigger or smaller pages.
545          * However, requesting each and every memory size is too much work, so
546          * what we'll do instead is walk through the page sizes available, pick
547          * the smallest one and set up page shift to match that one. We will be
548          * wasting some space this way, but it's much nicer than looping around
549          * trying to reserve each and every page size.
550          *
551          * If we fail to get enough contiguous memory, then we'll go and
552          * reserve space in smaller chunks.
553          */
554
555         need_iova_contig_obj = !(mp->flags & MEMPOOL_F_NO_IOVA_CONTIG);
556         ret = rte_mempool_get_page_size(mp, &pg_sz);
557         if (ret < 0)
558                 return ret;
559
560         if (pg_sz != 0)
561                 pg_shift = rte_bsf32(pg_sz);
562
563         for (mz_id = 0, n = mp->size; n > 0; mz_id++, n -= ret) {
564                 size_t min_chunk_size;
565
566                 mem_size = rte_mempool_ops_calc_mem_size(
567                         mp, n, pg_shift, &min_chunk_size, &align);
568
569                 if (mem_size < 0) {
570                         ret = mem_size;
571                         goto fail;
572                 }
573
574                 ret = snprintf(mz_name, sizeof(mz_name),
575                         RTE_MEMPOOL_MZ_FORMAT "_%d", mp->name, mz_id);
576                 if (ret < 0 || ret >= (int)sizeof(mz_name)) {
577                         ret = -ENAMETOOLONG;
578                         goto fail;
579                 }
580
581                 /* if we're trying to reserve contiguous memory, add appropriate
582                  * memzone flag.
583                  */
584                 if (min_chunk_size == (size_t)mem_size)
585                         mz_flags |= RTE_MEMZONE_IOVA_CONTIG;
586
587                 mz = rte_memzone_reserve_aligned(mz_name, mem_size,
588                                 mp->socket_id, mz_flags, align);
589
590                 /* don't try reserving with 0 size if we were asked to reserve
591                  * IOVA-contiguous memory.
592                  */
593                 if (min_chunk_size < (size_t)mem_size && mz == NULL) {
594                         /* not enough memory, retry with the biggest zone we
595                          * have
596                          */
597                         mz = rte_memzone_reserve_aligned(mz_name, 0,
598                                         mp->socket_id, mz_flags, align);
599                 }
600                 if (mz == NULL) {
601                         ret = -rte_errno;
602                         goto fail;
603                 }
604
605                 if (mz->len < min_chunk_size) {
606                         rte_memzone_free(mz);
607                         ret = -ENOMEM;
608                         goto fail;
609                 }
610
611                 if (need_iova_contig_obj)
612                         iova = mz->iova;
613                 else
614                         iova = RTE_BAD_IOVA;
615
616                 if (pg_sz == 0 || (mz_flags & RTE_MEMZONE_IOVA_CONTIG))
617                         ret = rte_mempool_populate_iova(mp, mz->addr,
618                                 iova, mz->len,
619                                 rte_mempool_memchunk_mz_free,
620                                 (void *)(uintptr_t)mz);
621                 else
622                         ret = rte_mempool_populate_virt(mp, mz->addr,
623                                 mz->len, pg_sz,
624                                 rte_mempool_memchunk_mz_free,
625                                 (void *)(uintptr_t)mz);
626                 if (ret < 0) {
627                         rte_memzone_free(mz);
628                         goto fail;
629                 }
630         }
631
632         return mp->size;
633
634  fail:
635         rte_mempool_free_memchunks(mp);
636         return ret;
637 }
638
639 /* return the memory size required for mempool objects in anonymous mem */
640 static ssize_t
641 get_anon_size(const struct rte_mempool *mp)
642 {
643         ssize_t size;
644         size_t pg_sz, pg_shift;
645         size_t min_chunk_size;
646         size_t align;
647
648         pg_sz = getpagesize();
649         pg_shift = rte_bsf32(pg_sz);
650         size = rte_mempool_ops_calc_mem_size(mp, mp->size, pg_shift,
651                                              &min_chunk_size, &align);
652
653         return size;
654 }
655
656 /* unmap a memory zone mapped by rte_mempool_populate_anon() */
657 static void
658 rte_mempool_memchunk_anon_free(struct rte_mempool_memhdr *memhdr,
659         void *opaque)
660 {
661         ssize_t size;
662
663         /*
664          * Calculate size since memhdr->len has contiguous chunk length
665          * which may be smaller if anon map is split into many contiguous
666          * chunks. Result must be the same as we calculated on populate.
667          */
668         size = get_anon_size(memhdr->mp);
669         if (size < 0)
670                 return;
671
672         munmap(opaque, size);
673 }
674
675 /* populate the mempool with an anonymous mapping */
676 int
677 rte_mempool_populate_anon(struct rte_mempool *mp)
678 {
679         ssize_t size;
680         int ret;
681         char *addr;
682
683         /* mempool is already populated, error */
684         if ((!STAILQ_EMPTY(&mp->mem_list)) || mp->nb_mem_chunks != 0) {
685                 rte_errno = EINVAL;
686                 return 0;
687         }
688
689         ret = mempool_ops_alloc_once(mp);
690         if (ret != 0)
691                 return ret;
692
693         size = get_anon_size(mp);
694         if (size < 0) {
695                 rte_errno = -size;
696                 return 0;
697         }
698
699         /* get chunk of virtually continuous memory */
700         addr = mmap(NULL, size, PROT_READ | PROT_WRITE,
701                 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
702         if (addr == MAP_FAILED) {
703                 rte_errno = errno;
704                 return 0;
705         }
706         /* can't use MMAP_LOCKED, it does not exist on BSD */
707         if (mlock(addr, size) < 0) {
708                 rte_errno = errno;
709                 munmap(addr, size);
710                 return 0;
711         }
712
713         ret = rte_mempool_populate_virt(mp, addr, size, getpagesize(),
714                 rte_mempool_memchunk_anon_free, addr);
715         if (ret == 0)
716                 goto fail;
717
718         return mp->populated_size;
719
720  fail:
721         rte_mempool_free_memchunks(mp);
722         return 0;
723 }
724
725 /* free a mempool */
726 void
727 rte_mempool_free(struct rte_mempool *mp)
728 {
729         struct rte_mempool_list *mempool_list = NULL;
730         struct rte_tailq_entry *te;
731
732         if (mp == NULL)
733                 return;
734
735         mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
736         rte_mcfg_tailq_write_lock();
737         /* find out tailq entry */
738         TAILQ_FOREACH(te, mempool_list, next) {
739                 if (te->data == (void *)mp)
740                         break;
741         }
742
743         if (te != NULL) {
744                 TAILQ_REMOVE(mempool_list, te, next);
745                 rte_free(te);
746         }
747         rte_mcfg_tailq_write_unlock();
748
749         rte_mempool_free_memchunks(mp);
750         rte_mempool_ops_free(mp);
751         rte_memzone_free(mp->mz);
752 }
753
754 static void
755 mempool_cache_init(struct rte_mempool_cache *cache, uint32_t size)
756 {
757         cache->size = size;
758         cache->flushthresh = CALC_CACHE_FLUSHTHRESH(size);
759         cache->len = 0;
760 }
761
762 /*
763  * Create and initialize a cache for objects that are retrieved from and
764  * returned to an underlying mempool. This structure is identical to the
765  * local_cache[lcore_id] pointed to by the mempool structure.
766  */
767 struct rte_mempool_cache *
768 rte_mempool_cache_create(uint32_t size, int socket_id)
769 {
770         struct rte_mempool_cache *cache;
771
772         if (size == 0 || size > RTE_MEMPOOL_CACHE_MAX_SIZE) {
773                 rte_errno = EINVAL;
774                 return NULL;
775         }
776
777         cache = rte_zmalloc_socket("MEMPOOL_CACHE", sizeof(*cache),
778                                   RTE_CACHE_LINE_SIZE, socket_id);
779         if (cache == NULL) {
780                 RTE_LOG(ERR, MEMPOOL, "Cannot allocate mempool cache.\n");
781                 rte_errno = ENOMEM;
782                 return NULL;
783         }
784
785         mempool_cache_init(cache, size);
786
787         return cache;
788 }
789
790 /*
791  * Free a cache. It's the responsibility of the user to make sure that any
792  * remaining objects in the cache are flushed to the corresponding
793  * mempool.
794  */
795 void
796 rte_mempool_cache_free(struct rte_mempool_cache *cache)
797 {
798         rte_free(cache);
799 }
800
801 /* create an empty mempool */
802 struct rte_mempool *
803 rte_mempool_create_empty(const char *name, unsigned n, unsigned elt_size,
804         unsigned cache_size, unsigned private_data_size,
805         int socket_id, unsigned flags)
806 {
807         char mz_name[RTE_MEMZONE_NAMESIZE];
808         struct rte_mempool_list *mempool_list;
809         struct rte_mempool *mp = NULL;
810         struct rte_tailq_entry *te = NULL;
811         const struct rte_memzone *mz = NULL;
812         size_t mempool_size;
813         unsigned int mz_flags = RTE_MEMZONE_1GB|RTE_MEMZONE_SIZE_HINT_ONLY;
814         struct rte_mempool_objsz objsz;
815         unsigned lcore_id;
816         int ret;
817
818         /* compilation-time checks */
819         RTE_BUILD_BUG_ON((sizeof(struct rte_mempool) &
820                           RTE_CACHE_LINE_MASK) != 0);
821         RTE_BUILD_BUG_ON((sizeof(struct rte_mempool_cache) &
822                           RTE_CACHE_LINE_MASK) != 0);
823 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
824         RTE_BUILD_BUG_ON((sizeof(struct rte_mempool_debug_stats) &
825                           RTE_CACHE_LINE_MASK) != 0);
826         RTE_BUILD_BUG_ON((offsetof(struct rte_mempool, stats) &
827                           RTE_CACHE_LINE_MASK) != 0);
828 #endif
829
830         mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
831
832         /* asked for zero items */
833         if (n == 0) {
834                 rte_errno = EINVAL;
835                 return NULL;
836         }
837
838         /* asked cache too big */
839         if (cache_size > RTE_MEMPOOL_CACHE_MAX_SIZE ||
840             CALC_CACHE_FLUSHTHRESH(cache_size) > n) {
841                 rte_errno = EINVAL;
842                 return NULL;
843         }
844
845         /* "no cache align" imply "no spread" */
846         if (flags & MEMPOOL_F_NO_CACHE_ALIGN)
847                 flags |= MEMPOOL_F_NO_SPREAD;
848
849         /* calculate mempool object sizes. */
850         if (!rte_mempool_calc_obj_size(elt_size, flags, &objsz)) {
851                 rte_errno = EINVAL;
852                 return NULL;
853         }
854
855         rte_mcfg_mempool_write_lock();
856
857         /*
858          * reserve a memory zone for this mempool: private data is
859          * cache-aligned
860          */
861         private_data_size = (private_data_size +
862                              RTE_MEMPOOL_ALIGN_MASK) & (~RTE_MEMPOOL_ALIGN_MASK);
863
864
865         /* try to allocate tailq entry */
866         te = rte_zmalloc("MEMPOOL_TAILQ_ENTRY", sizeof(*te), 0);
867         if (te == NULL) {
868                 RTE_LOG(ERR, MEMPOOL, "Cannot allocate tailq entry!\n");
869                 goto exit_unlock;
870         }
871
872         mempool_size = MEMPOOL_HEADER_SIZE(mp, cache_size);
873         mempool_size += private_data_size;
874         mempool_size = RTE_ALIGN_CEIL(mempool_size, RTE_MEMPOOL_ALIGN);
875
876         ret = snprintf(mz_name, sizeof(mz_name), RTE_MEMPOOL_MZ_FORMAT, name);
877         if (ret < 0 || ret >= (int)sizeof(mz_name)) {
878                 rte_errno = ENAMETOOLONG;
879                 goto exit_unlock;
880         }
881
882         mz = rte_memzone_reserve(mz_name, mempool_size, socket_id, mz_flags);
883         if (mz == NULL)
884                 goto exit_unlock;
885
886         /* init the mempool structure */
887         mp = mz->addr;
888         memset(mp, 0, MEMPOOL_HEADER_SIZE(mp, cache_size));
889         ret = strlcpy(mp->name, name, sizeof(mp->name));
890         if (ret < 0 || ret >= (int)sizeof(mp->name)) {
891                 rte_errno = ENAMETOOLONG;
892                 goto exit_unlock;
893         }
894         mp->mz = mz;
895         mp->size = n;
896         mp->flags = flags;
897         mp->socket_id = socket_id;
898         mp->elt_size = objsz.elt_size;
899         mp->header_size = objsz.header_size;
900         mp->trailer_size = objsz.trailer_size;
901         /* Size of default caches, zero means disabled. */
902         mp->cache_size = cache_size;
903         mp->private_data_size = private_data_size;
904         STAILQ_INIT(&mp->elt_list);
905         STAILQ_INIT(&mp->mem_list);
906
907         /*
908          * local_cache pointer is set even if cache_size is zero.
909          * The local_cache points to just past the elt_pa[] array.
910          */
911         mp->local_cache = (struct rte_mempool_cache *)
912                 RTE_PTR_ADD(mp, MEMPOOL_HEADER_SIZE(mp, 0));
913
914         /* Init all default caches. */
915         if (cache_size != 0) {
916                 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++)
917                         mempool_cache_init(&mp->local_cache[lcore_id],
918                                            cache_size);
919         }
920
921         te->data = mp;
922
923         rte_mcfg_tailq_write_lock();
924         TAILQ_INSERT_TAIL(mempool_list, te, next);
925         rte_mcfg_tailq_write_unlock();
926         rte_mcfg_mempool_write_unlock();
927
928         return mp;
929
930 exit_unlock:
931         rte_mcfg_mempool_write_unlock();
932         rte_free(te);
933         rte_mempool_free(mp);
934         return NULL;
935 }
936
937 /* create the mempool */
938 struct rte_mempool *
939 rte_mempool_create(const char *name, unsigned n, unsigned elt_size,
940         unsigned cache_size, unsigned private_data_size,
941         rte_mempool_ctor_t *mp_init, void *mp_init_arg,
942         rte_mempool_obj_cb_t *obj_init, void *obj_init_arg,
943         int socket_id, unsigned flags)
944 {
945         int ret;
946         struct rte_mempool *mp;
947
948         mp = rte_mempool_create_empty(name, n, elt_size, cache_size,
949                 private_data_size, socket_id, flags);
950         if (mp == NULL)
951                 return NULL;
952
953         /*
954          * Since we have 4 combinations of the SP/SC/MP/MC examine the flags to
955          * set the correct index into the table of ops structs.
956          */
957         if ((flags & MEMPOOL_F_SP_PUT) && (flags & MEMPOOL_F_SC_GET))
958                 ret = rte_mempool_set_ops_byname(mp, "ring_sp_sc", NULL);
959         else if (flags & MEMPOOL_F_SP_PUT)
960                 ret = rte_mempool_set_ops_byname(mp, "ring_sp_mc", NULL);
961         else if (flags & MEMPOOL_F_SC_GET)
962                 ret = rte_mempool_set_ops_byname(mp, "ring_mp_sc", NULL);
963         else
964                 ret = rte_mempool_set_ops_byname(mp, "ring_mp_mc", NULL);
965
966         if (ret)
967                 goto fail;
968
969         /* call the mempool priv initializer */
970         if (mp_init)
971                 mp_init(mp, mp_init_arg);
972
973         if (rte_mempool_populate_default(mp) < 0)
974                 goto fail;
975
976         /* call the object initializers */
977         if (obj_init)
978                 rte_mempool_obj_iter(mp, obj_init, obj_init_arg);
979
980         return mp;
981
982  fail:
983         rte_mempool_free(mp);
984         return NULL;
985 }
986
987 /* Return the number of entries in the mempool */
988 unsigned int
989 rte_mempool_avail_count(const struct rte_mempool *mp)
990 {
991         unsigned count;
992         unsigned lcore_id;
993
994         count = rte_mempool_ops_get_count(mp);
995
996         if (mp->cache_size == 0)
997                 return count;
998
999         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++)
1000                 count += mp->local_cache[lcore_id].len;
1001
1002         /*
1003          * due to race condition (access to len is not locked), the
1004          * total can be greater than size... so fix the result
1005          */
1006         if (count > mp->size)
1007                 return mp->size;
1008         return count;
1009 }
1010
1011 /* return the number of entries allocated from the mempool */
1012 unsigned int
1013 rte_mempool_in_use_count(const struct rte_mempool *mp)
1014 {
1015         return mp->size - rte_mempool_avail_count(mp);
1016 }
1017
1018 /* dump the cache status */
1019 static unsigned
1020 rte_mempool_dump_cache(FILE *f, const struct rte_mempool *mp)
1021 {
1022         unsigned lcore_id;
1023         unsigned count = 0;
1024         unsigned cache_count;
1025
1026         fprintf(f, "  internal cache infos:\n");
1027         fprintf(f, "    cache_size=%"PRIu32"\n", mp->cache_size);
1028
1029         if (mp->cache_size == 0)
1030                 return count;
1031
1032         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1033                 cache_count = mp->local_cache[lcore_id].len;
1034                 fprintf(f, "    cache_count[%u]=%"PRIu32"\n",
1035                         lcore_id, cache_count);
1036                 count += cache_count;
1037         }
1038         fprintf(f, "    total_cache_count=%u\n", count);
1039         return count;
1040 }
1041
1042 #ifndef __INTEL_COMPILER
1043 #pragma GCC diagnostic ignored "-Wcast-qual"
1044 #endif
1045
1046 /* check and update cookies or panic (internal) */
1047 void rte_mempool_check_cookies(const struct rte_mempool *mp,
1048         void * const *obj_table_const, unsigned n, int free)
1049 {
1050 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
1051         struct rte_mempool_objhdr *hdr;
1052         struct rte_mempool_objtlr *tlr;
1053         uint64_t cookie;
1054         void *tmp;
1055         void *obj;
1056         void **obj_table;
1057
1058         /* Force to drop the "const" attribute. This is done only when
1059          * DEBUG is enabled */
1060         tmp = (void *) obj_table_const;
1061         obj_table = tmp;
1062
1063         while (n--) {
1064                 obj = obj_table[n];
1065
1066                 if (rte_mempool_from_obj(obj) != mp)
1067                         rte_panic("MEMPOOL: object is owned by another "
1068                                   "mempool\n");
1069
1070                 hdr = __mempool_get_header(obj);
1071                 cookie = hdr->cookie;
1072
1073                 if (free == 0) {
1074                         if (cookie != RTE_MEMPOOL_HEADER_COOKIE1) {
1075                                 RTE_LOG(CRIT, MEMPOOL,
1076                                         "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
1077                                         obj, (const void *) mp, cookie);
1078                                 rte_panic("MEMPOOL: bad header cookie (put)\n");
1079                         }
1080                         hdr->cookie = RTE_MEMPOOL_HEADER_COOKIE2;
1081                 } else if (free == 1) {
1082                         if (cookie != RTE_MEMPOOL_HEADER_COOKIE2) {
1083                                 RTE_LOG(CRIT, MEMPOOL,
1084                                         "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
1085                                         obj, (const void *) mp, cookie);
1086                                 rte_panic("MEMPOOL: bad header cookie (get)\n");
1087                         }
1088                         hdr->cookie = RTE_MEMPOOL_HEADER_COOKIE1;
1089                 } else if (free == 2) {
1090                         if (cookie != RTE_MEMPOOL_HEADER_COOKIE1 &&
1091                             cookie != RTE_MEMPOOL_HEADER_COOKIE2) {
1092                                 RTE_LOG(CRIT, MEMPOOL,
1093                                         "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
1094                                         obj, (const void *) mp, cookie);
1095                                 rte_panic("MEMPOOL: bad header cookie (audit)\n");
1096                         }
1097                 }
1098                 tlr = __mempool_get_trailer(obj);
1099                 cookie = tlr->cookie;
1100                 if (cookie != RTE_MEMPOOL_TRAILER_COOKIE) {
1101                         RTE_LOG(CRIT, MEMPOOL,
1102                                 "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
1103                                 obj, (const void *) mp, cookie);
1104                         rte_panic("MEMPOOL: bad trailer cookie\n");
1105                 }
1106         }
1107 #else
1108         RTE_SET_USED(mp);
1109         RTE_SET_USED(obj_table_const);
1110         RTE_SET_USED(n);
1111         RTE_SET_USED(free);
1112 #endif
1113 }
1114
1115 void
1116 rte_mempool_contig_blocks_check_cookies(const struct rte_mempool *mp,
1117         void * const *first_obj_table_const, unsigned int n, int free)
1118 {
1119 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
1120         struct rte_mempool_info info;
1121         const size_t total_elt_sz =
1122                 mp->header_size + mp->elt_size + mp->trailer_size;
1123         unsigned int i, j;
1124
1125         rte_mempool_ops_get_info(mp, &info);
1126
1127         for (i = 0; i < n; ++i) {
1128                 void *first_obj = first_obj_table_const[i];
1129
1130                 for (j = 0; j < info.contig_block_size; ++j) {
1131                         void *obj;
1132
1133                         obj = (void *)((uintptr_t)first_obj + j * total_elt_sz);
1134                         rte_mempool_check_cookies(mp, &obj, 1, free);
1135                 }
1136         }
1137 #else
1138         RTE_SET_USED(mp);
1139         RTE_SET_USED(first_obj_table_const);
1140         RTE_SET_USED(n);
1141         RTE_SET_USED(free);
1142 #endif
1143 }
1144
1145 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
1146 static void
1147 mempool_obj_audit(struct rte_mempool *mp, __rte_unused void *opaque,
1148         void *obj, __rte_unused unsigned idx)
1149 {
1150         __mempool_check_cookies(mp, &obj, 1, 2);
1151 }
1152
1153 static void
1154 mempool_audit_cookies(struct rte_mempool *mp)
1155 {
1156         unsigned num;
1157
1158         num = rte_mempool_obj_iter(mp, mempool_obj_audit, NULL);
1159         if (num != mp->size) {
1160                 rte_panic("rte_mempool_obj_iter(mempool=%p, size=%u) "
1161                         "iterated only over %u elements\n",
1162                         mp, mp->size, num);
1163         }
1164 }
1165 #else
1166 #define mempool_audit_cookies(mp) do {} while(0)
1167 #endif
1168
1169 #ifndef __INTEL_COMPILER
1170 #pragma GCC diagnostic error "-Wcast-qual"
1171 #endif
1172
1173 /* check cookies before and after objects */
1174 static void
1175 mempool_audit_cache(const struct rte_mempool *mp)
1176 {
1177         /* check cache size consistency */
1178         unsigned lcore_id;
1179
1180         if (mp->cache_size == 0)
1181                 return;
1182
1183         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1184                 const struct rte_mempool_cache *cache;
1185                 cache = &mp->local_cache[lcore_id];
1186                 if (cache->len > cache->flushthresh) {
1187                         RTE_LOG(CRIT, MEMPOOL, "badness on cache[%u]\n",
1188                                 lcore_id);
1189                         rte_panic("MEMPOOL: invalid cache len\n");
1190                 }
1191         }
1192 }
1193
1194 /* check the consistency of mempool (size, cookies, ...) */
1195 void
1196 rte_mempool_audit(struct rte_mempool *mp)
1197 {
1198         mempool_audit_cache(mp);
1199         mempool_audit_cookies(mp);
1200
1201         /* For case where mempool DEBUG is not set, and cache size is 0 */
1202         RTE_SET_USED(mp);
1203 }
1204
1205 /* dump the status of the mempool on the console */
1206 void
1207 rte_mempool_dump(FILE *f, struct rte_mempool *mp)
1208 {
1209 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
1210         struct rte_mempool_info info;
1211         struct rte_mempool_debug_stats sum;
1212         unsigned lcore_id;
1213 #endif
1214         struct rte_mempool_memhdr *memhdr;
1215         unsigned common_count;
1216         unsigned cache_count;
1217         size_t mem_len = 0;
1218
1219         RTE_ASSERT(f != NULL);
1220         RTE_ASSERT(mp != NULL);
1221
1222         fprintf(f, "mempool <%s>@%p\n", mp->name, mp);
1223         fprintf(f, "  flags=%x\n", mp->flags);
1224         fprintf(f, "  pool=%p\n", mp->pool_data);
1225         fprintf(f, "  iova=0x%" PRIx64 "\n", mp->mz->iova);
1226         fprintf(f, "  nb_mem_chunks=%u\n", mp->nb_mem_chunks);
1227         fprintf(f, "  size=%"PRIu32"\n", mp->size);
1228         fprintf(f, "  populated_size=%"PRIu32"\n", mp->populated_size);
1229         fprintf(f, "  header_size=%"PRIu32"\n", mp->header_size);
1230         fprintf(f, "  elt_size=%"PRIu32"\n", mp->elt_size);
1231         fprintf(f, "  trailer_size=%"PRIu32"\n", mp->trailer_size);
1232         fprintf(f, "  total_obj_size=%"PRIu32"\n",
1233                mp->header_size + mp->elt_size + mp->trailer_size);
1234
1235         fprintf(f, "  private_data_size=%"PRIu32"\n", mp->private_data_size);
1236
1237         STAILQ_FOREACH(memhdr, &mp->mem_list, next)
1238                 mem_len += memhdr->len;
1239         if (mem_len != 0) {
1240                 fprintf(f, "  avg bytes/object=%#Lf\n",
1241                         (long double)mem_len / mp->size);
1242         }
1243
1244         cache_count = rte_mempool_dump_cache(f, mp);
1245         common_count = rte_mempool_ops_get_count(mp);
1246         if ((cache_count + common_count) > mp->size)
1247                 common_count = mp->size - cache_count;
1248         fprintf(f, "  common_pool_count=%u\n", common_count);
1249
1250         /* sum and dump statistics */
1251 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
1252         rte_mempool_ops_get_info(mp, &info);
1253         memset(&sum, 0, sizeof(sum));
1254         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1255                 sum.put_bulk += mp->stats[lcore_id].put_bulk;
1256                 sum.put_objs += mp->stats[lcore_id].put_objs;
1257                 sum.get_success_bulk += mp->stats[lcore_id].get_success_bulk;
1258                 sum.get_success_objs += mp->stats[lcore_id].get_success_objs;
1259                 sum.get_fail_bulk += mp->stats[lcore_id].get_fail_bulk;
1260                 sum.get_fail_objs += mp->stats[lcore_id].get_fail_objs;
1261                 sum.get_success_blks += mp->stats[lcore_id].get_success_blks;
1262                 sum.get_fail_blks += mp->stats[lcore_id].get_fail_blks;
1263         }
1264         fprintf(f, "  stats:\n");
1265         fprintf(f, "    put_bulk=%"PRIu64"\n", sum.put_bulk);
1266         fprintf(f, "    put_objs=%"PRIu64"\n", sum.put_objs);
1267         fprintf(f, "    get_success_bulk=%"PRIu64"\n", sum.get_success_bulk);
1268         fprintf(f, "    get_success_objs=%"PRIu64"\n", sum.get_success_objs);
1269         fprintf(f, "    get_fail_bulk=%"PRIu64"\n", sum.get_fail_bulk);
1270         fprintf(f, "    get_fail_objs=%"PRIu64"\n", sum.get_fail_objs);
1271         if (info.contig_block_size > 0) {
1272                 fprintf(f, "    get_success_blks=%"PRIu64"\n",
1273                         sum.get_success_blks);
1274                 fprintf(f, "    get_fail_blks=%"PRIu64"\n", sum.get_fail_blks);
1275         }
1276 #else
1277         fprintf(f, "  no statistics available\n");
1278 #endif
1279
1280         rte_mempool_audit(mp);
1281 }
1282
1283 /* dump the status of all mempools on the console */
1284 void
1285 rte_mempool_list_dump(FILE *f)
1286 {
1287         struct rte_mempool *mp = NULL;
1288         struct rte_tailq_entry *te;
1289         struct rte_mempool_list *mempool_list;
1290
1291         mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
1292
1293         rte_mcfg_mempool_read_lock();
1294
1295         TAILQ_FOREACH(te, mempool_list, next) {
1296                 mp = (struct rte_mempool *) te->data;
1297                 rte_mempool_dump(f, mp);
1298         }
1299
1300         rte_mcfg_mempool_read_unlock();
1301 }
1302
1303 /* search a mempool from its name */
1304 struct rte_mempool *
1305 rte_mempool_lookup(const char *name)
1306 {
1307         struct rte_mempool *mp = NULL;
1308         struct rte_tailq_entry *te;
1309         struct rte_mempool_list *mempool_list;
1310
1311         mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
1312
1313         rte_mcfg_mempool_read_lock();
1314
1315         TAILQ_FOREACH(te, mempool_list, next) {
1316                 mp = (struct rte_mempool *) te->data;
1317                 if (strncmp(name, mp->name, RTE_MEMPOOL_NAMESIZE) == 0)
1318                         break;
1319         }
1320
1321         rte_mcfg_mempool_read_unlock();
1322
1323         if (te == NULL) {
1324                 rte_errno = ENOENT;
1325                 return NULL;
1326         }
1327
1328         return mp;
1329 }
1330
1331 void rte_mempool_walk(void (*func)(struct rte_mempool *, void *),
1332                       void *arg)
1333 {
1334         struct rte_tailq_entry *te = NULL;
1335         struct rte_mempool_list *mempool_list;
1336         void *tmp_te;
1337
1338         mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
1339
1340         rte_mcfg_mempool_read_lock();
1341
1342         TAILQ_FOREACH_SAFE(te, mempool_list, next, tmp_te) {
1343                 (*func)((struct rte_mempool *) te->data, arg);
1344         }
1345
1346         rte_mcfg_mempool_read_unlock();
1347 }