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