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