xen: remove dependency in libraries
[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         /* Notify memory area to mempool */
371         ret = rte_mempool_ops_register_memory_area(mp, vaddr, paddr, len);
372         if (ret != -ENOTSUP && ret < 0)
373                 return ret;
374
375         /* create the internal ring if not already done */
376         if ((mp->flags & MEMPOOL_F_POOL_CREATED) == 0) {
377                 ret = rte_mempool_ops_alloc(mp);
378                 if (ret != 0)
379                         return ret;
380                 mp->flags |= MEMPOOL_F_POOL_CREATED;
381         }
382
383         /* mempool is already populated */
384         if (mp->populated_size >= mp->size)
385                 return -ENOSPC;
386
387         total_elt_sz = mp->header_size + mp->elt_size + mp->trailer_size;
388
389         /* Detect pool area has sufficient space for elements */
390         if (mp->flags & MEMPOOL_F_CAPA_PHYS_CONTIG) {
391                 if (len < total_elt_sz * mp->size) {
392                         RTE_LOG(ERR, MEMPOOL,
393                                 "pool area %" PRIx64 " not enough\n",
394                                 (uint64_t)len);
395                         return -ENOSPC;
396                 }
397         }
398
399         memhdr = rte_zmalloc("MEMPOOL_MEMHDR", sizeof(*memhdr), 0);
400         if (memhdr == NULL)
401                 return -ENOMEM;
402
403         memhdr->mp = mp;
404         memhdr->addr = vaddr;
405         memhdr->phys_addr = paddr;
406         memhdr->len = len;
407         memhdr->free_cb = free_cb;
408         memhdr->opaque = opaque;
409
410         if (mp->flags & MEMPOOL_F_CAPA_BLK_ALIGNED_OBJECTS)
411                 /* align object start address to a multiple of total_elt_sz */
412                 off = total_elt_sz - ((uintptr_t)vaddr % total_elt_sz);
413         else if (mp->flags & MEMPOOL_F_NO_CACHE_ALIGN)
414                 off = RTE_PTR_ALIGN_CEIL(vaddr, 8) - vaddr;
415         else
416                 off = RTE_PTR_ALIGN_CEIL(vaddr, RTE_CACHE_LINE_SIZE) - vaddr;
417
418         while (off + total_elt_sz <= len && mp->populated_size < mp->size) {
419                 off += mp->header_size;
420                 if (paddr == RTE_BAD_PHYS_ADDR)
421                         mempool_add_elem(mp, (char *)vaddr + off,
422                                 RTE_BAD_PHYS_ADDR);
423                 else
424                         mempool_add_elem(mp, (char *)vaddr + off, paddr + off);
425                 off += mp->elt_size + mp->trailer_size;
426                 i++;
427         }
428
429         /* not enough room to store one object */
430         if (i == 0)
431                 return -EINVAL;
432
433         STAILQ_INSERT_TAIL(&mp->mem_list, memhdr, next);
434         mp->nb_mem_chunks++;
435         return i;
436 }
437
438 /* Add objects in the pool, using a table of physical pages. Return the
439  * number of objects added, or a negative value on error.
440  */
441 int
442 rte_mempool_populate_phys_tab(struct rte_mempool *mp, char *vaddr,
443         const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift,
444         rte_mempool_memchunk_free_cb_t *free_cb, void *opaque)
445 {
446         uint32_t i, n;
447         int ret, cnt = 0;
448         size_t pg_sz = (size_t)1 << pg_shift;
449
450         /* mempool must not be populated */
451         if (mp->nb_mem_chunks != 0)
452                 return -EEXIST;
453
454         if (mp->flags & MEMPOOL_F_NO_PHYS_CONTIG)
455                 return rte_mempool_populate_phys(mp, vaddr, RTE_BAD_PHYS_ADDR,
456                         pg_num * pg_sz, free_cb, opaque);
457
458         for (i = 0; i < pg_num && mp->populated_size < mp->size; i += n) {
459
460                 /* populate with the largest group of contiguous pages */
461                 for (n = 1; (i + n) < pg_num &&
462                              paddr[i + n - 1] + pg_sz == paddr[i + n]; n++)
463                         ;
464
465                 ret = rte_mempool_populate_phys(mp, vaddr + i * pg_sz,
466                         paddr[i], n * pg_sz, free_cb, opaque);
467                 if (ret < 0) {
468                         rte_mempool_free_memchunks(mp);
469                         return ret;
470                 }
471                 /* no need to call the free callback for next chunks */
472                 free_cb = NULL;
473                 cnt += ret;
474         }
475         return cnt;
476 }
477
478 /* Populate the mempool with a virtual area. Return the number of
479  * objects added, or a negative value on error.
480  */
481 int
482 rte_mempool_populate_virt(struct rte_mempool *mp, char *addr,
483         size_t len, size_t pg_sz, rte_mempool_memchunk_free_cb_t *free_cb,
484         void *opaque)
485 {
486         phys_addr_t paddr;
487         size_t off, phys_len;
488         int ret, cnt = 0;
489
490         /* mempool must not be populated */
491         if (mp->nb_mem_chunks != 0)
492                 return -EEXIST;
493         /* address and len must be page-aligned */
494         if (RTE_PTR_ALIGN_CEIL(addr, pg_sz) != addr)
495                 return -EINVAL;
496         if (RTE_ALIGN_CEIL(len, pg_sz) != len)
497                 return -EINVAL;
498
499         if (mp->flags & MEMPOOL_F_NO_PHYS_CONTIG)
500                 return rte_mempool_populate_phys(mp, addr, RTE_BAD_PHYS_ADDR,
501                         len, free_cb, opaque);
502
503         for (off = 0; off + pg_sz <= len &&
504                      mp->populated_size < mp->size; off += phys_len) {
505
506                 paddr = rte_mem_virt2phy(addr + off);
507                 /* required for xen_dom0 to get the machine address */
508                 paddr = rte_mem_phy2mch(-1, paddr);
509
510                 if (paddr == RTE_BAD_PHYS_ADDR && rte_eal_has_hugepages()) {
511                         ret = -EINVAL;
512                         goto fail;
513                 }
514
515                 /* populate with the largest group of contiguous pages */
516                 for (phys_len = pg_sz; off + phys_len < len; phys_len += pg_sz) {
517                         phys_addr_t paddr_tmp;
518
519                         paddr_tmp = rte_mem_virt2phy(addr + off + phys_len);
520                         paddr_tmp = rte_mem_phy2mch(-1, paddr_tmp);
521
522                         if (paddr_tmp != paddr + phys_len)
523                                 break;
524                 }
525
526                 ret = rte_mempool_populate_phys(mp, addr + off, paddr,
527                         phys_len, free_cb, opaque);
528                 if (ret < 0)
529                         goto fail;
530                 /* no need to call the free callback for next chunks */
531                 free_cb = NULL;
532                 cnt += ret;
533         }
534
535         return cnt;
536
537  fail:
538         rte_mempool_free_memchunks(mp);
539         return ret;
540 }
541
542 /* Default function to populate the mempool: allocate memory in memzones,
543  * and populate them. Return the number of objects added, or a negative
544  * value on error.
545  */
546 int
547 rte_mempool_populate_default(struct rte_mempool *mp)
548 {
549         unsigned int mz_flags = RTE_MEMZONE_1GB|RTE_MEMZONE_SIZE_HINT_ONLY;
550         char mz_name[RTE_MEMZONE_NAMESIZE];
551         const struct rte_memzone *mz;
552         size_t size, total_elt_sz, align, pg_sz, pg_shift;
553         phys_addr_t paddr;
554         unsigned mz_id, n;
555         unsigned int mp_flags;
556         int ret;
557
558         /* mempool must not be populated */
559         if (mp->nb_mem_chunks != 0)
560                 return -EEXIST;
561
562         /* Get mempool capabilities */
563         mp_flags = 0;
564         ret = rte_mempool_ops_get_capabilities(mp, &mp_flags);
565         if (ret == -ENOTSUP)
566                 RTE_LOG(DEBUG, MEMPOOL, "get_capability not supported for %s\n",
567                                         mp->name);
568         else if (ret < 0)
569                 return ret;
570
571         /* update mempool capabilities */
572         mp->flags |= mp_flags;
573
574         if (rte_eal_has_hugepages()) {
575                 pg_shift = 0; /* not needed, zone is physically contiguous */
576                 pg_sz = 0;
577                 align = RTE_CACHE_LINE_SIZE;
578         } else {
579                 pg_sz = getpagesize();
580                 pg_shift = rte_bsf32(pg_sz);
581                 align = pg_sz;
582         }
583
584         total_elt_sz = mp->header_size + mp->elt_size + mp->trailer_size;
585         for (mz_id = 0, n = mp->size; n > 0; mz_id++, n -= ret) {
586                 size = rte_mempool_xmem_size(n, total_elt_sz, pg_shift,
587                                                 mp->flags);
588
589                 ret = snprintf(mz_name, sizeof(mz_name),
590                         RTE_MEMPOOL_MZ_FORMAT "_%d", mp->name, mz_id);
591                 if (ret < 0 || ret >= (int)sizeof(mz_name)) {
592                         ret = -ENAMETOOLONG;
593                         goto fail;
594                 }
595
596                 mz = rte_memzone_reserve_aligned(mz_name, size,
597                         mp->socket_id, mz_flags, align);
598                 /* not enough memory, retry with the biggest zone we have */
599                 if (mz == NULL)
600                         mz = rte_memzone_reserve_aligned(mz_name, 0,
601                                 mp->socket_id, mz_flags, align);
602                 if (mz == NULL) {
603                         ret = -rte_errno;
604                         goto fail;
605                 }
606
607                 if (mp->flags & MEMPOOL_F_NO_PHYS_CONTIG)
608                         paddr = RTE_BAD_PHYS_ADDR;
609                 else
610                         paddr = mz->phys_addr;
611
612                 if (rte_eal_has_hugepages())
613                         ret = rte_mempool_populate_phys(mp, mz->addr,
614                                 paddr, mz->len,
615                                 rte_mempool_memchunk_mz_free,
616                                 (void *)(uintptr_t)mz);
617                 else
618                         ret = rte_mempool_populate_virt(mp, mz->addr,
619                                 mz->len, pg_sz,
620                                 rte_mempool_memchunk_mz_free,
621                                 (void *)(uintptr_t)mz);
622                 if (ret < 0) {
623                         rte_memzone_free(mz);
624                         goto fail;
625                 }
626         }
627
628         return mp->size;
629
630  fail:
631         rte_mempool_free_memchunks(mp);
632         return ret;
633 }
634
635 /* return the memory size required for mempool objects in anonymous mem */
636 static size_t
637 get_anon_size(const struct rte_mempool *mp)
638 {
639         size_t size, total_elt_sz, pg_sz, pg_shift;
640
641         pg_sz = getpagesize();
642         pg_shift = rte_bsf32(pg_sz);
643         total_elt_sz = mp->header_size + mp->elt_size + mp->trailer_size;
644         size = rte_mempool_xmem_size(mp->size, total_elt_sz, pg_shift,
645                                         mp->flags);
646
647         return size;
648 }
649
650 /* unmap a memory zone mapped by rte_mempool_populate_anon() */
651 static void
652 rte_mempool_memchunk_anon_free(struct rte_mempool_memhdr *memhdr,
653         void *opaque)
654 {
655         munmap(opaque, get_anon_size(memhdr->mp));
656 }
657
658 /* populate the mempool with an anonymous mapping */
659 int
660 rte_mempool_populate_anon(struct rte_mempool *mp)
661 {
662         size_t size;
663         int ret;
664         char *addr;
665
666         /* mempool is already populated, error */
667         if (!STAILQ_EMPTY(&mp->mem_list)) {
668                 rte_errno = EINVAL;
669                 return 0;
670         }
671
672         /* get chunk of virtually continuous memory */
673         size = get_anon_size(mp);
674         addr = mmap(NULL, size, PROT_READ | PROT_WRITE,
675                 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
676         if (addr == MAP_FAILED) {
677                 rte_errno = errno;
678                 return 0;
679         }
680         /* can't use MMAP_LOCKED, it does not exist on BSD */
681         if (mlock(addr, size) < 0) {
682                 rte_errno = errno;
683                 munmap(addr, size);
684                 return 0;
685         }
686
687         ret = rte_mempool_populate_virt(mp, addr, size, getpagesize(),
688                 rte_mempool_memchunk_anon_free, addr);
689         if (ret == 0)
690                 goto fail;
691
692         return mp->populated_size;
693
694  fail:
695         rte_mempool_free_memchunks(mp);
696         return 0;
697 }
698
699 /* free a mempool */
700 void
701 rte_mempool_free(struct rte_mempool *mp)
702 {
703         struct rte_mempool_list *mempool_list = NULL;
704         struct rte_tailq_entry *te;
705
706         if (mp == NULL)
707                 return;
708
709         mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
710         rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
711         /* find out tailq entry */
712         TAILQ_FOREACH(te, mempool_list, next) {
713                 if (te->data == (void *)mp)
714                         break;
715         }
716
717         if (te != NULL) {
718                 TAILQ_REMOVE(mempool_list, te, next);
719                 rte_free(te);
720         }
721         rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
722
723         rte_mempool_free_memchunks(mp);
724         rte_mempool_ops_free(mp);
725         rte_memzone_free(mp->mz);
726 }
727
728 static void
729 mempool_cache_init(struct rte_mempool_cache *cache, uint32_t size)
730 {
731         cache->size = size;
732         cache->flushthresh = CALC_CACHE_FLUSHTHRESH(size);
733         cache->len = 0;
734 }
735
736 /*
737  * Create and initialize a cache for objects that are retrieved from and
738  * returned to an underlying mempool. This structure is identical to the
739  * local_cache[lcore_id] pointed to by the mempool structure.
740  */
741 struct rte_mempool_cache *
742 rte_mempool_cache_create(uint32_t size, int socket_id)
743 {
744         struct rte_mempool_cache *cache;
745
746         if (size == 0 || size > RTE_MEMPOOL_CACHE_MAX_SIZE) {
747                 rte_errno = EINVAL;
748                 return NULL;
749         }
750
751         cache = rte_zmalloc_socket("MEMPOOL_CACHE", sizeof(*cache),
752                                   RTE_CACHE_LINE_SIZE, socket_id);
753         if (cache == NULL) {
754                 RTE_LOG(ERR, MEMPOOL, "Cannot allocate mempool cache.\n");
755                 rte_errno = ENOMEM;
756                 return NULL;
757         }
758
759         mempool_cache_init(cache, size);
760
761         return cache;
762 }
763
764 /*
765  * Free a cache. It's the responsibility of the user to make sure that any
766  * remaining objects in the cache are flushed to the corresponding
767  * mempool.
768  */
769 void
770 rte_mempool_cache_free(struct rte_mempool_cache *cache)
771 {
772         rte_free(cache);
773 }
774
775 /* create an empty mempool */
776 struct rte_mempool *
777 rte_mempool_create_empty(const char *name, unsigned n, unsigned elt_size,
778         unsigned cache_size, unsigned private_data_size,
779         int socket_id, unsigned flags)
780 {
781         char mz_name[RTE_MEMZONE_NAMESIZE];
782         struct rte_mempool_list *mempool_list;
783         struct rte_mempool *mp = NULL;
784         struct rte_tailq_entry *te = NULL;
785         const struct rte_memzone *mz = NULL;
786         size_t mempool_size;
787         unsigned int mz_flags = RTE_MEMZONE_1GB|RTE_MEMZONE_SIZE_HINT_ONLY;
788         struct rte_mempool_objsz objsz;
789         unsigned lcore_id;
790         int ret;
791
792         /* compilation-time checks */
793         RTE_BUILD_BUG_ON((sizeof(struct rte_mempool) &
794                           RTE_CACHE_LINE_MASK) != 0);
795         RTE_BUILD_BUG_ON((sizeof(struct rte_mempool_cache) &
796                           RTE_CACHE_LINE_MASK) != 0);
797 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
798         RTE_BUILD_BUG_ON((sizeof(struct rte_mempool_debug_stats) &
799                           RTE_CACHE_LINE_MASK) != 0);
800         RTE_BUILD_BUG_ON((offsetof(struct rte_mempool, stats) &
801                           RTE_CACHE_LINE_MASK) != 0);
802 #endif
803
804         mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
805
806         /* asked cache too big */
807         if (cache_size > RTE_MEMPOOL_CACHE_MAX_SIZE ||
808             CALC_CACHE_FLUSHTHRESH(cache_size) > n) {
809                 rte_errno = EINVAL;
810                 return NULL;
811         }
812
813         /* "no cache align" imply "no spread" */
814         if (flags & MEMPOOL_F_NO_CACHE_ALIGN)
815                 flags |= MEMPOOL_F_NO_SPREAD;
816
817         /* calculate mempool object sizes. */
818         if (!rte_mempool_calc_obj_size(elt_size, flags, &objsz)) {
819                 rte_errno = EINVAL;
820                 return NULL;
821         }
822
823         rte_rwlock_write_lock(RTE_EAL_MEMPOOL_RWLOCK);
824
825         /*
826          * reserve a memory zone for this mempool: private data is
827          * cache-aligned
828          */
829         private_data_size = (private_data_size +
830                              RTE_MEMPOOL_ALIGN_MASK) & (~RTE_MEMPOOL_ALIGN_MASK);
831
832
833         /* try to allocate tailq entry */
834         te = rte_zmalloc("MEMPOOL_TAILQ_ENTRY", sizeof(*te), 0);
835         if (te == NULL) {
836                 RTE_LOG(ERR, MEMPOOL, "Cannot allocate tailq entry!\n");
837                 goto exit_unlock;
838         }
839
840         mempool_size = MEMPOOL_HEADER_SIZE(mp, cache_size);
841         mempool_size += private_data_size;
842         mempool_size = RTE_ALIGN_CEIL(mempool_size, RTE_MEMPOOL_ALIGN);
843
844         ret = snprintf(mz_name, sizeof(mz_name), RTE_MEMPOOL_MZ_FORMAT, name);
845         if (ret < 0 || ret >= (int)sizeof(mz_name)) {
846                 rte_errno = ENAMETOOLONG;
847                 goto exit_unlock;
848         }
849
850         mz = rte_memzone_reserve(mz_name, mempool_size, socket_id, mz_flags);
851         if (mz == NULL)
852                 goto exit_unlock;
853
854         /* init the mempool structure */
855         mp = mz->addr;
856         memset(mp, 0, MEMPOOL_HEADER_SIZE(mp, cache_size));
857         ret = snprintf(mp->name, sizeof(mp->name), "%s", name);
858         if (ret < 0 || ret >= (int)sizeof(mp->name)) {
859                 rte_errno = ENAMETOOLONG;
860                 goto exit_unlock;
861         }
862         mp->mz = mz;
863         mp->size = n;
864         mp->flags = flags;
865         mp->socket_id = socket_id;
866         mp->elt_size = objsz.elt_size;
867         mp->header_size = objsz.header_size;
868         mp->trailer_size = objsz.trailer_size;
869         /* Size of default caches, zero means disabled. */
870         mp->cache_size = cache_size;
871         mp->private_data_size = private_data_size;
872         STAILQ_INIT(&mp->elt_list);
873         STAILQ_INIT(&mp->mem_list);
874
875         /*
876          * local_cache pointer is set even if cache_size is zero.
877          * The local_cache points to just past the elt_pa[] array.
878          */
879         mp->local_cache = (struct rte_mempool_cache *)
880                 RTE_PTR_ADD(mp, MEMPOOL_HEADER_SIZE(mp, 0));
881
882         /* Init all default caches. */
883         if (cache_size != 0) {
884                 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++)
885                         mempool_cache_init(&mp->local_cache[lcore_id],
886                                            cache_size);
887         }
888
889         te->data = mp;
890
891         rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
892         TAILQ_INSERT_TAIL(mempool_list, te, next);
893         rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
894         rte_rwlock_write_unlock(RTE_EAL_MEMPOOL_RWLOCK);
895
896         return mp;
897
898 exit_unlock:
899         rte_rwlock_write_unlock(RTE_EAL_MEMPOOL_RWLOCK);
900         rte_free(te);
901         rte_mempool_free(mp);
902         return NULL;
903 }
904
905 /* create the mempool */
906 struct rte_mempool *
907 rte_mempool_create(const char *name, unsigned n, unsigned elt_size,
908         unsigned cache_size, unsigned private_data_size,
909         rte_mempool_ctor_t *mp_init, void *mp_init_arg,
910         rte_mempool_obj_cb_t *obj_init, void *obj_init_arg,
911         int socket_id, unsigned flags)
912 {
913         int ret;
914         struct rte_mempool *mp;
915
916         mp = rte_mempool_create_empty(name, n, elt_size, cache_size,
917                 private_data_size, socket_id, flags);
918         if (mp == NULL)
919                 return NULL;
920
921         /*
922          * Since we have 4 combinations of the SP/SC/MP/MC examine the flags to
923          * set the correct index into the table of ops structs.
924          */
925         if ((flags & MEMPOOL_F_SP_PUT) && (flags & MEMPOOL_F_SC_GET))
926                 ret = rte_mempool_set_ops_byname(mp, "ring_sp_sc", NULL);
927         else if (flags & MEMPOOL_F_SP_PUT)
928                 ret = rte_mempool_set_ops_byname(mp, "ring_sp_mc", NULL);
929         else if (flags & MEMPOOL_F_SC_GET)
930                 ret = rte_mempool_set_ops_byname(mp, "ring_mp_sc", NULL);
931         else
932                 ret = rte_mempool_set_ops_byname(mp, "ring_mp_mc", NULL);
933
934         if (ret)
935                 goto fail;
936
937         /* call the mempool priv initializer */
938         if (mp_init)
939                 mp_init(mp, mp_init_arg);
940
941         if (rte_mempool_populate_default(mp) < 0)
942                 goto fail;
943
944         /* call the object initializers */
945         if (obj_init)
946                 rte_mempool_obj_iter(mp, obj_init, obj_init_arg);
947
948         return mp;
949
950  fail:
951         rte_mempool_free(mp);
952         return NULL;
953 }
954
955 /*
956  * Create the mempool over already allocated chunk of memory.
957  * That external memory buffer can consists of physically disjoint pages.
958  * Setting vaddr to NULL, makes mempool to fallback to rte_mempool_create()
959  * behavior.
960  */
961 struct rte_mempool *
962 rte_mempool_xmem_create(const char *name, unsigned n, unsigned elt_size,
963                 unsigned cache_size, unsigned private_data_size,
964                 rte_mempool_ctor_t *mp_init, void *mp_init_arg,
965                 rte_mempool_obj_cb_t *obj_init, void *obj_init_arg,
966                 int socket_id, unsigned flags, void *vaddr,
967                 const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift)
968 {
969         struct rte_mempool *mp = NULL;
970         int ret;
971
972         /* no virtual address supplied, use rte_mempool_create() */
973         if (vaddr == NULL)
974                 return rte_mempool_create(name, n, elt_size, cache_size,
975                         private_data_size, mp_init, mp_init_arg,
976                         obj_init, obj_init_arg, socket_id, flags);
977
978         /* check that we have both VA and PA */
979         if (paddr == NULL) {
980                 rte_errno = EINVAL;
981                 return NULL;
982         }
983
984         /* Check that pg_shift parameter is valid. */
985         if (pg_shift > MEMPOOL_PG_SHIFT_MAX) {
986                 rte_errno = EINVAL;
987                 return NULL;
988         }
989
990         mp = rte_mempool_create_empty(name, n, elt_size, cache_size,
991                 private_data_size, socket_id, flags);
992         if (mp == NULL)
993                 return NULL;
994
995         /* call the mempool priv initializer */
996         if (mp_init)
997                 mp_init(mp, mp_init_arg);
998
999         ret = rte_mempool_populate_phys_tab(mp, vaddr, paddr, pg_num, pg_shift,
1000                 NULL, NULL);
1001         if (ret < 0 || ret != (int)mp->size)
1002                 goto fail;
1003
1004         /* call the object initializers */
1005         if (obj_init)
1006                 rte_mempool_obj_iter(mp, obj_init, obj_init_arg);
1007
1008         return mp;
1009
1010  fail:
1011         rte_mempool_free(mp);
1012         return NULL;
1013 }
1014
1015 /* Return the number of entries in the mempool */
1016 unsigned int
1017 rte_mempool_avail_count(const struct rte_mempool *mp)
1018 {
1019         unsigned count;
1020         unsigned lcore_id;
1021
1022         count = rte_mempool_ops_get_count(mp);
1023
1024         if (mp->cache_size == 0)
1025                 return count;
1026
1027         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++)
1028                 count += mp->local_cache[lcore_id].len;
1029
1030         /*
1031          * due to race condition (access to len is not locked), the
1032          * total can be greater than size... so fix the result
1033          */
1034         if (count > mp->size)
1035                 return mp->size;
1036         return count;
1037 }
1038
1039 /* return the number of entries allocated from the mempool */
1040 unsigned int
1041 rte_mempool_in_use_count(const struct rte_mempool *mp)
1042 {
1043         return mp->size - rte_mempool_avail_count(mp);
1044 }
1045
1046 /* dump the cache status */
1047 static unsigned
1048 rte_mempool_dump_cache(FILE *f, const struct rte_mempool *mp)
1049 {
1050         unsigned lcore_id;
1051         unsigned count = 0;
1052         unsigned cache_count;
1053
1054         fprintf(f, "  internal cache infos:\n");
1055         fprintf(f, "    cache_size=%"PRIu32"\n", mp->cache_size);
1056
1057         if (mp->cache_size == 0)
1058                 return count;
1059
1060         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1061                 cache_count = mp->local_cache[lcore_id].len;
1062                 fprintf(f, "    cache_count[%u]=%"PRIu32"\n",
1063                         lcore_id, cache_count);
1064                 count += cache_count;
1065         }
1066         fprintf(f, "    total_cache_count=%u\n", count);
1067         return count;
1068 }
1069
1070 #ifndef __INTEL_COMPILER
1071 #pragma GCC diagnostic ignored "-Wcast-qual"
1072 #endif
1073
1074 /* check and update cookies or panic (internal) */
1075 void rte_mempool_check_cookies(const struct rte_mempool *mp,
1076         void * const *obj_table_const, unsigned n, int free)
1077 {
1078 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
1079         struct rte_mempool_objhdr *hdr;
1080         struct rte_mempool_objtlr *tlr;
1081         uint64_t cookie;
1082         void *tmp;
1083         void *obj;
1084         void **obj_table;
1085
1086         /* Force to drop the "const" attribute. This is done only when
1087          * DEBUG is enabled */
1088         tmp = (void *) obj_table_const;
1089         obj_table = tmp;
1090
1091         while (n--) {
1092                 obj = obj_table[n];
1093
1094                 if (rte_mempool_from_obj(obj) != mp)
1095                         rte_panic("MEMPOOL: object is owned by another "
1096                                   "mempool\n");
1097
1098                 hdr = __mempool_get_header(obj);
1099                 cookie = hdr->cookie;
1100
1101                 if (free == 0) {
1102                         if (cookie != RTE_MEMPOOL_HEADER_COOKIE1) {
1103                                 RTE_LOG(CRIT, MEMPOOL,
1104                                         "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
1105                                         obj, (const void *) mp, cookie);
1106                                 rte_panic("MEMPOOL: bad header cookie (put)\n");
1107                         }
1108                         hdr->cookie = RTE_MEMPOOL_HEADER_COOKIE2;
1109                 } else if (free == 1) {
1110                         if (cookie != RTE_MEMPOOL_HEADER_COOKIE2) {
1111                                 RTE_LOG(CRIT, MEMPOOL,
1112                                         "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
1113                                         obj, (const void *) mp, cookie);
1114                                 rte_panic("MEMPOOL: bad header cookie (get)\n");
1115                         }
1116                         hdr->cookie = RTE_MEMPOOL_HEADER_COOKIE1;
1117                 } else if (free == 2) {
1118                         if (cookie != RTE_MEMPOOL_HEADER_COOKIE1 &&
1119                             cookie != RTE_MEMPOOL_HEADER_COOKIE2) {
1120                                 RTE_LOG(CRIT, MEMPOOL,
1121                                         "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
1122                                         obj, (const void *) mp, cookie);
1123                                 rte_panic("MEMPOOL: bad header cookie (audit)\n");
1124                         }
1125                 }
1126                 tlr = __mempool_get_trailer(obj);
1127                 cookie = tlr->cookie;
1128                 if (cookie != RTE_MEMPOOL_TRAILER_COOKIE) {
1129                         RTE_LOG(CRIT, MEMPOOL,
1130                                 "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
1131                                 obj, (const void *) mp, cookie);
1132                         rte_panic("MEMPOOL: bad trailer cookie\n");
1133                 }
1134         }
1135 #else
1136         RTE_SET_USED(mp);
1137         RTE_SET_USED(obj_table_const);
1138         RTE_SET_USED(n);
1139         RTE_SET_USED(free);
1140 #endif
1141 }
1142
1143 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
1144 static void
1145 mempool_obj_audit(struct rte_mempool *mp, __rte_unused void *opaque,
1146         void *obj, __rte_unused unsigned idx)
1147 {
1148         __mempool_check_cookies(mp, &obj, 1, 2);
1149 }
1150
1151 static void
1152 mempool_audit_cookies(struct rte_mempool *mp)
1153 {
1154         unsigned num;
1155
1156         num = rte_mempool_obj_iter(mp, mempool_obj_audit, NULL);
1157         if (num != mp->size) {
1158                 rte_panic("rte_mempool_obj_iter(mempool=%p, size=%u) "
1159                         "iterated only over %u elements\n",
1160                         mp, mp->size, num);
1161         }
1162 }
1163 #else
1164 #define mempool_audit_cookies(mp) do {} while(0)
1165 #endif
1166
1167 #ifndef __INTEL_COMPILER
1168 #pragma GCC diagnostic error "-Wcast-qual"
1169 #endif
1170
1171 /* check cookies before and after objects */
1172 static void
1173 mempool_audit_cache(const struct rte_mempool *mp)
1174 {
1175         /* check cache size consistency */
1176         unsigned lcore_id;
1177
1178         if (mp->cache_size == 0)
1179                 return;
1180
1181         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1182                 const struct rte_mempool_cache *cache;
1183                 cache = &mp->local_cache[lcore_id];
1184                 if (cache->len > cache->flushthresh) {
1185                         RTE_LOG(CRIT, MEMPOOL, "badness on cache[%u]\n",
1186                                 lcore_id);
1187                         rte_panic("MEMPOOL: invalid cache len\n");
1188                 }
1189         }
1190 }
1191
1192 /* check the consistency of mempool (size, cookies, ...) */
1193 void
1194 rte_mempool_audit(struct rte_mempool *mp)
1195 {
1196         mempool_audit_cache(mp);
1197         mempool_audit_cookies(mp);
1198
1199         /* For case where mempool DEBUG is not set, and cache size is 0 */
1200         RTE_SET_USED(mp);
1201 }
1202
1203 /* dump the status of the mempool on the console */
1204 void
1205 rte_mempool_dump(FILE *f, struct rte_mempool *mp)
1206 {
1207 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
1208         struct rte_mempool_debug_stats sum;
1209         unsigned lcore_id;
1210 #endif
1211         struct rte_mempool_memhdr *memhdr;
1212         unsigned common_count;
1213         unsigned cache_count;
1214         size_t mem_len = 0;
1215
1216         RTE_ASSERT(f != NULL);
1217         RTE_ASSERT(mp != NULL);
1218
1219         fprintf(f, "mempool <%s>@%p\n", mp->name, mp);
1220         fprintf(f, "  flags=%x\n", mp->flags);
1221         fprintf(f, "  pool=%p\n", mp->pool_data);
1222         fprintf(f, "  phys_addr=0x%" PRIx64 "\n", mp->mz->phys_addr);
1223         fprintf(f, "  nb_mem_chunks=%u\n", mp->nb_mem_chunks);
1224         fprintf(f, "  size=%"PRIu32"\n", mp->size);
1225         fprintf(f, "  populated_size=%"PRIu32"\n", mp->populated_size);
1226         fprintf(f, "  header_size=%"PRIu32"\n", mp->header_size);
1227         fprintf(f, "  elt_size=%"PRIu32"\n", mp->elt_size);
1228         fprintf(f, "  trailer_size=%"PRIu32"\n", mp->trailer_size);
1229         fprintf(f, "  total_obj_size=%"PRIu32"\n",
1230                mp->header_size + mp->elt_size + mp->trailer_size);
1231
1232         fprintf(f, "  private_data_size=%"PRIu32"\n", mp->private_data_size);
1233
1234         STAILQ_FOREACH(memhdr, &mp->mem_list, next)
1235                 mem_len += memhdr->len;
1236         if (mem_len != 0) {
1237                 fprintf(f, "  avg bytes/object=%#Lf\n",
1238                         (long double)mem_len / mp->size);
1239         }
1240
1241         cache_count = rte_mempool_dump_cache(f, mp);
1242         common_count = rte_mempool_ops_get_count(mp);
1243         if ((cache_count + common_count) > mp->size)
1244                 common_count = mp->size - cache_count;
1245         fprintf(f, "  common_pool_count=%u\n", common_count);
1246
1247         /* sum and dump statistics */
1248 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
1249         memset(&sum, 0, sizeof(sum));
1250         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1251                 sum.put_bulk += mp->stats[lcore_id].put_bulk;
1252                 sum.put_objs += mp->stats[lcore_id].put_objs;
1253                 sum.get_success_bulk += mp->stats[lcore_id].get_success_bulk;
1254                 sum.get_success_objs += mp->stats[lcore_id].get_success_objs;
1255                 sum.get_fail_bulk += mp->stats[lcore_id].get_fail_bulk;
1256                 sum.get_fail_objs += mp->stats[lcore_id].get_fail_objs;
1257         }
1258         fprintf(f, "  stats:\n");
1259         fprintf(f, "    put_bulk=%"PRIu64"\n", sum.put_bulk);
1260         fprintf(f, "    put_objs=%"PRIu64"\n", sum.put_objs);
1261         fprintf(f, "    get_success_bulk=%"PRIu64"\n", sum.get_success_bulk);
1262         fprintf(f, "    get_success_objs=%"PRIu64"\n", sum.get_success_objs);
1263         fprintf(f, "    get_fail_bulk=%"PRIu64"\n", sum.get_fail_bulk);
1264         fprintf(f, "    get_fail_objs=%"PRIu64"\n", sum.get_fail_objs);
1265 #else
1266         fprintf(f, "  no statistics available\n");
1267 #endif
1268
1269         rte_mempool_audit(mp);
1270 }
1271
1272 /* dump the status of all mempools on the console */
1273 void
1274 rte_mempool_list_dump(FILE *f)
1275 {
1276         struct rte_mempool *mp = NULL;
1277         struct rte_tailq_entry *te;
1278         struct rte_mempool_list *mempool_list;
1279
1280         mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
1281
1282         rte_rwlock_read_lock(RTE_EAL_MEMPOOL_RWLOCK);
1283
1284         TAILQ_FOREACH(te, mempool_list, next) {
1285                 mp = (struct rte_mempool *) te->data;
1286                 rte_mempool_dump(f, mp);
1287         }
1288
1289         rte_rwlock_read_unlock(RTE_EAL_MEMPOOL_RWLOCK);
1290 }
1291
1292 /* search a mempool from its name */
1293 struct rte_mempool *
1294 rte_mempool_lookup(const char *name)
1295 {
1296         struct rte_mempool *mp = NULL;
1297         struct rte_tailq_entry *te;
1298         struct rte_mempool_list *mempool_list;
1299
1300         mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
1301
1302         rte_rwlock_read_lock(RTE_EAL_MEMPOOL_RWLOCK);
1303
1304         TAILQ_FOREACH(te, mempool_list, next) {
1305                 mp = (struct rte_mempool *) te->data;
1306                 if (strncmp(name, mp->name, RTE_MEMPOOL_NAMESIZE) == 0)
1307                         break;
1308         }
1309
1310         rte_rwlock_read_unlock(RTE_EAL_MEMPOOL_RWLOCK);
1311
1312         if (te == NULL) {
1313                 rte_errno = ENOENT;
1314                 return NULL;
1315         }
1316
1317         return mp;
1318 }
1319
1320 void rte_mempool_walk(void (*func)(struct rte_mempool *, void *),
1321                       void *arg)
1322 {
1323         struct rte_tailq_entry *te = NULL;
1324         struct rte_mempool_list *mempool_list;
1325         void *tmp_te;
1326
1327         mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
1328
1329         rte_rwlock_read_lock(RTE_EAL_MEMPOOL_RWLOCK);
1330
1331         TAILQ_FOREACH_SAFE(te, mempool_list, next, tmp_te) {
1332                 (*func)((struct rte_mempool *) te->data, arg);
1333         }
1334
1335         rte_rwlock_read_unlock(RTE_EAL_MEMPOOL_RWLOCK);
1336 }