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