mempool: rework comments and style
[dpdk.git] / lib / librte_mempool / rte_mempool.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <stdio.h>
35 #include <string.h>
36 #include <stdint.h>
37 #include <stdarg.h>
38 #include <unistd.h>
39 #include <inttypes.h>
40 #include <errno.h>
41 #include <sys/queue.h>
42
43 #include <rte_common.h>
44 #include <rte_log.h>
45 #include <rte_debug.h>
46 #include <rte_memory.h>
47 #include <rte_memzone.h>
48 #include <rte_malloc.h>
49 #include <rte_atomic.h>
50 #include <rte_launch.h>
51 #include <rte_eal.h>
52 #include <rte_eal_memconfig.h>
53 #include <rte_per_lcore.h>
54 #include <rte_lcore.h>
55 #include <rte_branch_prediction.h>
56 #include <rte_ring.h>
57 #include <rte_errno.h>
58 #include <rte_string_fns.h>
59 #include <rte_spinlock.h>
60
61 #include "rte_mempool.h"
62
63 TAILQ_HEAD(rte_mempool_list, rte_tailq_entry);
64
65 static struct rte_tailq_elem rte_mempool_tailq = {
66         .name = "RTE_MEMPOOL",
67 };
68 EAL_REGISTER_TAILQ(rte_mempool_tailq)
69
70 #define CACHE_FLUSHTHRESH_MULTIPLIER 1.5
71 #define CALC_CACHE_FLUSHTHRESH(c)       \
72         ((typeof(c))((c) * CACHE_FLUSHTHRESH_MULTIPLIER))
73
74 /*
75  * return the greatest common divisor between a and b (fast algorithm)
76  *
77  */
78 static unsigned get_gcd(unsigned a, unsigned b)
79 {
80         unsigned c;
81
82         if (0 == a)
83                 return b;
84         if (0 == b)
85                 return a;
86
87         if (a < b) {
88                 c = a;
89                 a = b;
90                 b = c;
91         }
92
93         while (b != 0) {
94                 c = a % b;
95                 a = b;
96                 b = c;
97         }
98
99         return a;
100 }
101
102 /*
103  * Depending on memory configuration, objects addresses are spread
104  * between channels and ranks in RAM: the pool allocator will add
105  * padding between objects. This function return the new size of the
106  * object.
107  */
108 static unsigned optimize_object_size(unsigned obj_size)
109 {
110         unsigned nrank, nchan;
111         unsigned new_obj_size;
112
113         /* get number of channels */
114         nchan = rte_memory_get_nchannel();
115         if (nchan == 0)
116                 nchan = 4;
117
118         nrank = rte_memory_get_nrank();
119         if (nrank == 0)
120                 nrank = 1;
121
122         /* process new object size */
123         new_obj_size = (obj_size + RTE_MEMPOOL_ALIGN_MASK) / RTE_MEMPOOL_ALIGN;
124         while (get_gcd(new_obj_size, nrank * nchan) != 1)
125                 new_obj_size++;
126         return new_obj_size * RTE_MEMPOOL_ALIGN;
127 }
128
129 static void
130 mempool_add_elem(struct rte_mempool *mp, void *obj, uint32_t obj_idx,
131         rte_mempool_obj_ctor_t *obj_init, void *obj_init_arg)
132 {
133         struct rte_mempool_objhdr *hdr;
134         struct rte_mempool_objtlr *tlr __rte_unused;
135
136         obj = (char *)obj + mp->header_size;
137
138         /* set mempool ptr in header */
139         hdr = RTE_PTR_SUB(obj, sizeof(*hdr));
140         hdr->mp = mp;
141
142 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
143         hdr->cookie = RTE_MEMPOOL_HEADER_COOKIE2;
144         tlr = __mempool_get_trailer(obj);
145         tlr->cookie = RTE_MEMPOOL_TRAILER_COOKIE;
146 #endif
147         /* call the initializer */
148         if (obj_init)
149                 obj_init(mp, obj_init_arg, obj, obj_idx);
150
151         /* enqueue in ring */
152         rte_ring_sp_enqueue(mp->ring, obj);
153 }
154
155 /* Iterate through objects at the given address
156  *
157  * Given the pointer to the memory, and its topology in physical memory
158  * (the physical addresses table), iterate through the "elt_num" objects
159  * of size "total_elt_sz" aligned at "align". For each object in this memory
160  * chunk, invoke a callback. It returns the effective number of objects
161  * in this memory.
162  */
163 uint32_t
164 rte_mempool_obj_iter(void *vaddr, uint32_t elt_num, size_t elt_sz, size_t align,
165         const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift,
166         rte_mempool_obj_iter_t obj_iter, void *obj_iter_arg)
167 {
168         uint32_t i, j, k;
169         uint32_t pgn, pgf;
170         uintptr_t end, start, va;
171         uintptr_t pg_sz;
172
173         pg_sz = (uintptr_t)1 << pg_shift;
174         va = (uintptr_t)vaddr;
175
176         i = 0;
177         j = 0;
178
179         while (i != elt_num && j != pg_num) {
180
181                 start = RTE_ALIGN_CEIL(va, align);
182                 end = start + elt_sz;
183
184                 /* index of the first page for the next element. */
185                 pgf = (end >> pg_shift) - (start >> pg_shift);
186
187                 /* index of the last page for the current element. */
188                 pgn = ((end - 1) >> pg_shift) - (start >> pg_shift);
189                 pgn += j;
190
191                 /* do we have enough space left for the element. */
192                 if (pgn >= pg_num)
193                         break;
194
195                 for (k = j;
196                                 k != pgn &&
197                                 paddr[k] + pg_sz == paddr[k + 1];
198                                 k++)
199                         ;
200
201                 /*
202                  * if next pgn chunks of memory physically continuous,
203                  * use it to create next element.
204                  * otherwise, just skip that chunk unused.
205                  */
206                 if (k == pgn) {
207                         if (obj_iter != NULL)
208                                 obj_iter(obj_iter_arg, (void *)start,
209                                         (void *)end, i);
210                         va = end;
211                         j += pgf;
212                         i++;
213                 } else {
214                         va = RTE_ALIGN_CEIL((va + 1), pg_sz);
215                         j++;
216                 }
217         }
218
219         return i;
220 }
221
222 /*
223  * Populate  mempool with the objects.
224  */
225
226 struct mempool_populate_arg {
227         struct rte_mempool     *mp;
228         rte_mempool_obj_ctor_t *obj_init;
229         void                   *obj_init_arg;
230 };
231
232 static void
233 mempool_obj_populate(void *arg, void *start, void *end, uint32_t idx)
234 {
235         struct mempool_populate_arg *pa = arg;
236
237         mempool_add_elem(pa->mp, start, idx, pa->obj_init, pa->obj_init_arg);
238         pa->mp->elt_va_end = (uintptr_t)end;
239 }
240
241 static void
242 mempool_populate(struct rte_mempool *mp, size_t num, size_t align,
243         rte_mempool_obj_ctor_t *obj_init, void *obj_init_arg)
244 {
245         uint32_t elt_sz;
246         struct mempool_populate_arg arg;
247
248         elt_sz = mp->elt_size + mp->header_size + mp->trailer_size;
249         arg.mp = mp;
250         arg.obj_init = obj_init;
251         arg.obj_init_arg = obj_init_arg;
252
253         mp->size = rte_mempool_obj_iter((void *)mp->elt_va_start,
254                 num, elt_sz, align,
255                 mp->elt_pa, mp->pg_num, mp->pg_shift,
256                 mempool_obj_populate, &arg);
257 }
258
259 uint32_t
260 rte_mempool_calc_obj_size(uint32_t elt_size, uint32_t flags,
261         struct rte_mempool_objsz *sz)
262 {
263         struct rte_mempool_objsz lsz;
264
265         sz = (sz != NULL) ? sz : &lsz;
266
267         /*
268          * In header, we have at least the pointer to the pool, and
269          * optionaly a 64 bits cookie.
270          */
271         sz->header_size = 0;
272         sz->header_size += sizeof(struct rte_mempool *); /* ptr to pool */
273 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
274         sz->header_size += sizeof(uint64_t); /* cookie */
275 #endif
276         if ((flags & MEMPOOL_F_NO_CACHE_ALIGN) == 0)
277                 sz->header_size = RTE_ALIGN_CEIL(sz->header_size,
278                         RTE_MEMPOOL_ALIGN);
279
280         /* trailer contains the cookie in debug mode */
281         sz->trailer_size = 0;
282 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
283         sz->trailer_size += sizeof(uint64_t); /* cookie */
284 #endif
285         /* element size is 8 bytes-aligned at least */
286         sz->elt_size = RTE_ALIGN_CEIL(elt_size, sizeof(uint64_t));
287
288         /* expand trailer to next cache line */
289         if ((flags & MEMPOOL_F_NO_CACHE_ALIGN) == 0) {
290                 sz->total_size = sz->header_size + sz->elt_size +
291                         sz->trailer_size;
292                 sz->trailer_size += ((RTE_MEMPOOL_ALIGN -
293                                   (sz->total_size & RTE_MEMPOOL_ALIGN_MASK)) &
294                                  RTE_MEMPOOL_ALIGN_MASK);
295         }
296
297         /*
298          * increase trailer to add padding between objects in order to
299          * spread them across memory channels/ranks
300          */
301         if ((flags & MEMPOOL_F_NO_SPREAD) == 0) {
302                 unsigned new_size;
303                 new_size = optimize_object_size(sz->header_size + sz->elt_size +
304                         sz->trailer_size);
305                 sz->trailer_size = new_size - sz->header_size - sz->elt_size;
306         }
307
308         if (! rte_eal_has_hugepages()) {
309                 /*
310                  * compute trailer size so that pool elements fit exactly in
311                  * a standard page
312                  */
313                 int page_size = getpagesize();
314                 int new_size = page_size - sz->header_size - sz->elt_size;
315                 if (new_size < 0 || (unsigned int)new_size < sz->trailer_size) {
316                         printf("When hugepages are disabled, pool objects "
317                                "can't exceed PAGE_SIZE: %d + %d + %d > %d\n",
318                                sz->header_size, sz->elt_size, sz->trailer_size,
319                                page_size);
320                         return 0;
321                 }
322                 sz->trailer_size = new_size;
323         }
324
325         /* this is the size of an object, including header and trailer */
326         sz->total_size = sz->header_size + sz->elt_size + sz->trailer_size;
327
328         return sz->total_size;
329 }
330
331
332 /*
333  * Calculate maximum amount of memory required to store given number of objects.
334  */
335 size_t
336 rte_mempool_xmem_size(uint32_t elt_num, size_t elt_sz, uint32_t pg_shift)
337 {
338         size_t n, pg_num, pg_sz, sz;
339
340         pg_sz = (size_t)1 << pg_shift;
341
342         if ((n = pg_sz / elt_sz) > 0) {
343                 pg_num = (elt_num + n - 1) / n;
344                 sz = pg_num << pg_shift;
345         } else {
346                 sz = RTE_ALIGN_CEIL(elt_sz, pg_sz) * elt_num;
347         }
348
349         return sz;
350 }
351
352 /* Callback used by rte_mempool_xmem_usage(): it sets the opaque
353  * argument to the end of the object.
354  */
355 static void
356 mempool_lelem_iter(void *arg, __rte_unused void *start, void *end,
357         __rte_unused uint32_t idx)
358 {
359         *(uintptr_t *)arg = (uintptr_t)end;
360 }
361
362 /*
363  * Calculate how much memory would be actually required with the
364  * given memory footprint to store required number of elements.
365  */
366 ssize_t
367 rte_mempool_xmem_usage(void *vaddr, uint32_t elt_num, size_t elt_sz,
368         const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift)
369 {
370         uint32_t n;
371         uintptr_t va, uv;
372         size_t pg_sz, usz;
373
374         pg_sz = (size_t)1 << pg_shift;
375         va = (uintptr_t)vaddr;
376         uv = va;
377
378         if ((n = rte_mempool_obj_iter(vaddr, elt_num, elt_sz, 1,
379                         paddr, pg_num, pg_shift, mempool_lelem_iter,
380                         &uv)) != elt_num) {
381                 return -(ssize_t)n;
382         }
383
384         uv = RTE_ALIGN_CEIL(uv, pg_sz);
385         usz = uv - va;
386         return usz;
387 }
388
389 #ifndef RTE_LIBRTE_XEN_DOM0
390 /* stub if DOM0 support not configured */
391 struct rte_mempool *
392 rte_dom0_mempool_create(const char *name __rte_unused,
393                         unsigned n __rte_unused,
394                         unsigned elt_size __rte_unused,
395                         unsigned cache_size __rte_unused,
396                         unsigned private_data_size __rte_unused,
397                         rte_mempool_ctor_t *mp_init __rte_unused,
398                         void *mp_init_arg __rte_unused,
399                         rte_mempool_obj_ctor_t *obj_init __rte_unused,
400                         void *obj_init_arg __rte_unused,
401                         int socket_id __rte_unused,
402                         unsigned flags __rte_unused)
403 {
404         rte_errno = EINVAL;
405         return NULL;
406 }
407 #endif
408
409 /* create the mempool */
410 struct rte_mempool *
411 rte_mempool_create(const char *name, unsigned n, unsigned elt_size,
412                    unsigned cache_size, unsigned private_data_size,
413                    rte_mempool_ctor_t *mp_init, void *mp_init_arg,
414                    rte_mempool_obj_ctor_t *obj_init, void *obj_init_arg,
415                    int socket_id, unsigned flags)
416 {
417         if (rte_xen_dom0_supported())
418                 return rte_dom0_mempool_create(name, n, elt_size,
419                                                cache_size, private_data_size,
420                                                mp_init, mp_init_arg,
421                                                obj_init, obj_init_arg,
422                                                socket_id, flags);
423         else
424                 return rte_mempool_xmem_create(name, n, elt_size,
425                                                cache_size, private_data_size,
426                                                mp_init, mp_init_arg,
427                                                obj_init, obj_init_arg,
428                                                socket_id, flags,
429                                                NULL, NULL, MEMPOOL_PG_NUM_DEFAULT,
430                                                MEMPOOL_PG_SHIFT_MAX);
431 }
432
433 /*
434  * Create the mempool over already allocated chunk of memory.
435  * That external memory buffer can consists of physically disjoint pages.
436  * Setting vaddr to NULL, makes mempool to fallback to original behaviour
437  * and allocate space for mempool and it's elements as one big chunk of
438  * physically continuos memory.
439  * */
440 struct rte_mempool *
441 rte_mempool_xmem_create(const char *name, unsigned n, unsigned elt_size,
442                 unsigned cache_size, unsigned private_data_size,
443                 rte_mempool_ctor_t *mp_init, void *mp_init_arg,
444                 rte_mempool_obj_ctor_t *obj_init, void *obj_init_arg,
445                 int socket_id, unsigned flags, void *vaddr,
446                 const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift)
447 {
448         char mz_name[RTE_MEMZONE_NAMESIZE];
449         char rg_name[RTE_RING_NAMESIZE];
450         struct rte_mempool_list *mempool_list;
451         struct rte_mempool *mp = NULL;
452         struct rte_tailq_entry *te = NULL;
453         struct rte_ring *r = NULL;
454         const struct rte_memzone *mz;
455         size_t mempool_size;
456         int mz_flags = RTE_MEMZONE_1GB|RTE_MEMZONE_SIZE_HINT_ONLY;
457         int rg_flags = 0;
458         void *obj;
459         struct rte_mempool_objsz objsz;
460         void *startaddr;
461         int page_size = getpagesize();
462
463         /* compilation-time checks */
464         RTE_BUILD_BUG_ON((sizeof(struct rte_mempool) &
465                           RTE_CACHE_LINE_MASK) != 0);
466         RTE_BUILD_BUG_ON((sizeof(struct rte_mempool_cache) &
467                           RTE_CACHE_LINE_MASK) != 0);
468 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
469         RTE_BUILD_BUG_ON((sizeof(struct rte_mempool_debug_stats) &
470                           RTE_CACHE_LINE_MASK) != 0);
471         RTE_BUILD_BUG_ON((offsetof(struct rte_mempool, stats) &
472                           RTE_CACHE_LINE_MASK) != 0);
473 #endif
474
475         mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
476
477         /* asked cache too big */
478         if (cache_size > RTE_MEMPOOL_CACHE_MAX_SIZE ||
479             CALC_CACHE_FLUSHTHRESH(cache_size) > n) {
480                 rte_errno = EINVAL;
481                 return NULL;
482         }
483
484         /* check that we have both VA and PA */
485         if (vaddr != NULL && paddr == NULL) {
486                 rte_errno = EINVAL;
487                 return NULL;
488         }
489
490         /* Check that pg_num and pg_shift parameters are valid. */
491         if (pg_num < RTE_DIM(mp->elt_pa) || pg_shift > MEMPOOL_PG_SHIFT_MAX) {
492                 rte_errno = EINVAL;
493                 return NULL;
494         }
495
496         /* "no cache align" imply "no spread" */
497         if (flags & MEMPOOL_F_NO_CACHE_ALIGN)
498                 flags |= MEMPOOL_F_NO_SPREAD;
499
500         /* ring flags */
501         if (flags & MEMPOOL_F_SP_PUT)
502                 rg_flags |= RING_F_SP_ENQ;
503         if (flags & MEMPOOL_F_SC_GET)
504                 rg_flags |= RING_F_SC_DEQ;
505
506         /* calculate mempool object sizes. */
507         if (!rte_mempool_calc_obj_size(elt_size, flags, &objsz)) {
508                 rte_errno = EINVAL;
509                 return NULL;
510         }
511
512         rte_rwlock_write_lock(RTE_EAL_MEMPOOL_RWLOCK);
513
514         /* allocate the ring that will be used to store objects */
515         /* Ring functions will return appropriate errors if we are
516          * running as a secondary process etc., so no checks made
517          * in this function for that condition */
518         snprintf(rg_name, sizeof(rg_name), RTE_MEMPOOL_MZ_FORMAT, name);
519         r = rte_ring_create(rg_name, rte_align32pow2(n+1), socket_id, rg_flags);
520         if (r == NULL)
521                 goto exit_unlock;
522
523         /*
524          * reserve a memory zone for this mempool: private data is
525          * cache-aligned
526          */
527         private_data_size = (private_data_size +
528                              RTE_MEMPOOL_ALIGN_MASK) & (~RTE_MEMPOOL_ALIGN_MASK);
529
530         if (! rte_eal_has_hugepages()) {
531                 /*
532                  * expand private data size to a whole page, so that the
533                  * first pool element will start on a new standard page
534                  */
535                 int head = sizeof(struct rte_mempool);
536                 int new_size = (private_data_size + head) % page_size;
537                 if (new_size)
538                         private_data_size += page_size - new_size;
539         }
540
541         /* try to allocate tailq entry */
542         te = rte_zmalloc("MEMPOOL_TAILQ_ENTRY", sizeof(*te), 0);
543         if (te == NULL) {
544                 RTE_LOG(ERR, MEMPOOL, "Cannot allocate tailq entry!\n");
545                 goto exit_unlock;
546         }
547
548         /*
549          * If user provided an external memory buffer, then use it to
550          * store mempool objects. Otherwise reserve a memzone that is large
551          * enough to hold mempool header and metadata plus mempool objects.
552          */
553         mempool_size = MEMPOOL_HEADER_SIZE(mp, pg_num, cache_size);
554         mempool_size += private_data_size;
555         mempool_size = RTE_ALIGN_CEIL(mempool_size, RTE_MEMPOOL_ALIGN);
556         if (vaddr == NULL)
557                 mempool_size += (size_t)objsz.total_size * n;
558
559         if (! rte_eal_has_hugepages()) {
560                 /*
561                  * we want the memory pool to start on a page boundary,
562                  * because pool elements crossing page boundaries would
563                  * result in discontiguous physical addresses
564                  */
565                 mempool_size += page_size;
566         }
567
568         snprintf(mz_name, sizeof(mz_name), RTE_MEMPOOL_MZ_FORMAT, name);
569
570         mz = rte_memzone_reserve(mz_name, mempool_size, socket_id, mz_flags);
571         if (mz == NULL)
572                 goto exit_unlock;
573
574         if (rte_eal_has_hugepages()) {
575                 startaddr = (void*)mz->addr;
576         } else {
577                 /* align memory pool start address on a page boundary */
578                 unsigned long addr = (unsigned long)mz->addr;
579                 if (addr & (page_size - 1)) {
580                         addr += page_size;
581                         addr &= ~(page_size - 1);
582                 }
583                 startaddr = (void*)addr;
584         }
585
586         /* init the mempool structure */
587         mp = startaddr;
588         memset(mp, 0, sizeof(*mp));
589         snprintf(mp->name, sizeof(mp->name), "%s", name);
590         mp->phys_addr = mz->phys_addr;
591         mp->ring = r;
592         mp->size = n;
593         mp->flags = flags;
594         mp->elt_size = objsz.elt_size;
595         mp->header_size = objsz.header_size;
596         mp->trailer_size = objsz.trailer_size;
597         mp->cache_size = cache_size;
598         mp->cache_flushthresh = CALC_CACHE_FLUSHTHRESH(cache_size);
599         mp->private_data_size = private_data_size;
600
601         /*
602          * local_cache pointer is set even if cache_size is zero.
603          * The local_cache points to just past the elt_pa[] array.
604          */
605         mp->local_cache = (struct rte_mempool_cache *)
606                 RTE_PTR_ADD(mp, MEMPOOL_HEADER_SIZE(mp, pg_num, 0));
607
608         /* calculate address of the first element for continuous mempool. */
609         obj = (char *)mp + MEMPOOL_HEADER_SIZE(mp, pg_num, cache_size) +
610                 private_data_size;
611         obj = RTE_PTR_ALIGN_CEIL(obj, RTE_MEMPOOL_ALIGN);
612
613         /* populate address translation fields. */
614         mp->pg_num = pg_num;
615         mp->pg_shift = pg_shift;
616         mp->pg_mask = RTE_LEN2MASK(mp->pg_shift, typeof(mp->pg_mask));
617
618         /* mempool elements allocated together with mempool */
619         if (vaddr == NULL) {
620                 mp->elt_va_start = (uintptr_t)obj;
621                 mp->elt_pa[0] = mp->phys_addr +
622                         (mp->elt_va_start - (uintptr_t)mp);
623         } else {
624                 /* mempool elements in a separate chunk of memory. */
625                 mp->elt_va_start = (uintptr_t)vaddr;
626                 memcpy(mp->elt_pa, paddr, sizeof (mp->elt_pa[0]) * pg_num);
627         }
628
629         mp->elt_va_end = mp->elt_va_start;
630
631         /* call the initializer */
632         if (mp_init)
633                 mp_init(mp, mp_init_arg);
634
635         mempool_populate(mp, n, 1, obj_init, obj_init_arg);
636
637         te->data = (void *) mp;
638
639         rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
640         TAILQ_INSERT_TAIL(mempool_list, te, next);
641         rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
642         rte_rwlock_write_unlock(RTE_EAL_MEMPOOL_RWLOCK);
643
644         return mp;
645
646 exit_unlock:
647         rte_rwlock_write_unlock(RTE_EAL_MEMPOOL_RWLOCK);
648         rte_ring_free(r);
649         rte_free(te);
650
651         return NULL;
652 }
653
654 /* Return the number of entries in the mempool */
655 unsigned
656 rte_mempool_count(const struct rte_mempool *mp)
657 {
658         unsigned count;
659         unsigned lcore_id;
660
661         count = rte_ring_count(mp->ring);
662
663         if (mp->cache_size == 0)
664                 return count;
665
666         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++)
667                 count += mp->local_cache[lcore_id].len;
668
669         /*
670          * due to race condition (access to len is not locked), the
671          * total can be greater than size... so fix the result
672          */
673         if (count > mp->size)
674                 return mp->size;
675         return count;
676 }
677
678 /* dump the cache status */
679 static unsigned
680 rte_mempool_dump_cache(FILE *f, const struct rte_mempool *mp)
681 {
682         unsigned lcore_id;
683         unsigned count = 0;
684         unsigned cache_count;
685
686         fprintf(f, "  cache infos:\n");
687         fprintf(f, "    cache_size=%"PRIu32"\n", mp->cache_size);
688
689         if (mp->cache_size == 0)
690                 return count;
691
692         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
693                 cache_count = mp->local_cache[lcore_id].len;
694                 fprintf(f, "    cache_count[%u]=%u\n", lcore_id, cache_count);
695                 count += cache_count;
696         }
697         fprintf(f, "    total_cache_count=%u\n", count);
698         return count;
699 }
700
701 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
702 /* check cookies before and after objects */
703 #ifndef __INTEL_COMPILER
704 #pragma GCC diagnostic ignored "-Wcast-qual"
705 #endif
706
707 struct mempool_audit_arg {
708         const struct rte_mempool *mp;
709         uintptr_t obj_end;
710         uint32_t obj_num;
711 };
712
713 static void
714 mempool_obj_audit(void *arg, void *start, void *end, uint32_t idx)
715 {
716         struct mempool_audit_arg *pa = arg;
717         void *obj;
718
719         obj = (char *)start + pa->mp->header_size;
720         pa->obj_end = (uintptr_t)end;
721         pa->obj_num = idx + 1;
722         __mempool_check_cookies(pa->mp, &obj, 1, 2);
723 }
724
725 static void
726 mempool_audit_cookies(const struct rte_mempool *mp)
727 {
728         uint32_t elt_sz, num;
729         struct mempool_audit_arg arg;
730
731         elt_sz = mp->elt_size + mp->header_size + mp->trailer_size;
732
733         arg.mp = mp;
734         arg.obj_end = mp->elt_va_start;
735         arg.obj_num = 0;
736
737         num = rte_mempool_obj_iter((void *)mp->elt_va_start,
738                 mp->size, elt_sz, 1,
739                 mp->elt_pa, mp->pg_num, mp->pg_shift,
740                 mempool_obj_audit, &arg);
741
742         if (num != mp->size) {
743                         rte_panic("rte_mempool_obj_iter(mempool=%p, size=%u) "
744                         "iterated only over %u elements\n",
745                         mp, mp->size, num);
746         } else if (arg.obj_end != mp->elt_va_end || arg.obj_num != mp->size) {
747                         rte_panic("rte_mempool_obj_iter(mempool=%p, size=%u) "
748                         "last callback va_end: %#tx (%#tx expeceted), "
749                         "num of objects: %u (%u expected)\n",
750                         mp, mp->size,
751                         arg.obj_end, mp->elt_va_end,
752                         arg.obj_num, mp->size);
753         }
754 }
755
756 #ifndef __INTEL_COMPILER
757 #pragma GCC diagnostic error "-Wcast-qual"
758 #endif
759 #else
760 #define mempool_audit_cookies(mp) do {} while(0)
761 #endif
762
763 /* check cookies before and after objects */
764 static void
765 mempool_audit_cache(const struct rte_mempool *mp)
766 {
767         /* check cache size consistency */
768         unsigned lcore_id;
769
770         if (mp->cache_size == 0)
771                 return;
772
773         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
774                 if (mp->local_cache[lcore_id].len > mp->cache_flushthresh) {
775                         RTE_LOG(CRIT, MEMPOOL, "badness on cache[%u]\n",
776                                 lcore_id);
777                         rte_panic("MEMPOOL: invalid cache len\n");
778                 }
779         }
780 }
781
782 /* check the consistency of mempool (size, cookies, ...) */
783 void
784 rte_mempool_audit(const struct rte_mempool *mp)
785 {
786         mempool_audit_cache(mp);
787         mempool_audit_cookies(mp);
788
789         /* For case where mempool DEBUG is not set, and cache size is 0 */
790         RTE_SET_USED(mp);
791 }
792
793 /* dump the status of the mempool on the console */
794 void
795 rte_mempool_dump(FILE *f, const struct rte_mempool *mp)
796 {
797 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
798         struct rte_mempool_debug_stats sum;
799         unsigned lcore_id;
800 #endif
801         unsigned common_count;
802         unsigned cache_count;
803
804         RTE_ASSERT(f != NULL);
805         RTE_ASSERT(mp != NULL);
806
807         fprintf(f, "mempool <%s>@%p\n", mp->name, mp);
808         fprintf(f, "  flags=%x\n", mp->flags);
809         fprintf(f, "  ring=<%s>@%p\n", mp->ring->name, mp->ring);
810         fprintf(f, "  phys_addr=0x%" PRIx64 "\n", mp->phys_addr);
811         fprintf(f, "  size=%"PRIu32"\n", mp->size);
812         fprintf(f, "  header_size=%"PRIu32"\n", mp->header_size);
813         fprintf(f, "  elt_size=%"PRIu32"\n", mp->elt_size);
814         fprintf(f, "  trailer_size=%"PRIu32"\n", mp->trailer_size);
815         fprintf(f, "  total_obj_size=%"PRIu32"\n",
816                mp->header_size + mp->elt_size + mp->trailer_size);
817
818         fprintf(f, "  private_data_size=%"PRIu32"\n", mp->private_data_size);
819         fprintf(f, "  pg_num=%"PRIu32"\n", mp->pg_num);
820         fprintf(f, "  pg_shift=%"PRIu32"\n", mp->pg_shift);
821         fprintf(f, "  pg_mask=%#tx\n", mp->pg_mask);
822         fprintf(f, "  elt_va_start=%#tx\n", mp->elt_va_start);
823         fprintf(f, "  elt_va_end=%#tx\n", mp->elt_va_end);
824         fprintf(f, "  elt_pa[0]=0x%" PRIx64 "\n", mp->elt_pa[0]);
825
826         if (mp->size != 0)
827                 fprintf(f, "  avg bytes/object=%#Lf\n",
828                         (long double)(mp->elt_va_end - mp->elt_va_start) /
829                         mp->size);
830
831         cache_count = rte_mempool_dump_cache(f, mp);
832         common_count = rte_ring_count(mp->ring);
833         if ((cache_count + common_count) > mp->size)
834                 common_count = mp->size - cache_count;
835         fprintf(f, "  common_pool_count=%u\n", common_count);
836
837         /* sum and dump statistics */
838 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
839         memset(&sum, 0, sizeof(sum));
840         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
841                 sum.put_bulk += mp->stats[lcore_id].put_bulk;
842                 sum.put_objs += mp->stats[lcore_id].put_objs;
843                 sum.get_success_bulk += mp->stats[lcore_id].get_success_bulk;
844                 sum.get_success_objs += mp->stats[lcore_id].get_success_objs;
845                 sum.get_fail_bulk += mp->stats[lcore_id].get_fail_bulk;
846                 sum.get_fail_objs += mp->stats[lcore_id].get_fail_objs;
847         }
848         fprintf(f, "  stats:\n");
849         fprintf(f, "    put_bulk=%"PRIu64"\n", sum.put_bulk);
850         fprintf(f, "    put_objs=%"PRIu64"\n", sum.put_objs);
851         fprintf(f, "    get_success_bulk=%"PRIu64"\n", sum.get_success_bulk);
852         fprintf(f, "    get_success_objs=%"PRIu64"\n", sum.get_success_objs);
853         fprintf(f, "    get_fail_bulk=%"PRIu64"\n", sum.get_fail_bulk);
854         fprintf(f, "    get_fail_objs=%"PRIu64"\n", sum.get_fail_objs);
855 #else
856         fprintf(f, "  no statistics available\n");
857 #endif
858
859         rte_mempool_audit(mp);
860 }
861
862 /* dump the status of all mempools on the console */
863 void
864 rte_mempool_list_dump(FILE *f)
865 {
866         const struct rte_mempool *mp = NULL;
867         struct rte_tailq_entry *te;
868         struct rte_mempool_list *mempool_list;
869
870         mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
871
872         rte_rwlock_read_lock(RTE_EAL_MEMPOOL_RWLOCK);
873
874         TAILQ_FOREACH(te, mempool_list, next) {
875                 mp = (struct rte_mempool *) te->data;
876                 rte_mempool_dump(f, mp);
877         }
878
879         rte_rwlock_read_unlock(RTE_EAL_MEMPOOL_RWLOCK);
880 }
881
882 /* search a mempool from its name */
883 struct rte_mempool *
884 rte_mempool_lookup(const char *name)
885 {
886         struct rte_mempool *mp = NULL;
887         struct rte_tailq_entry *te;
888         struct rte_mempool_list *mempool_list;
889
890         mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
891
892         rte_rwlock_read_lock(RTE_EAL_MEMPOOL_RWLOCK);
893
894         TAILQ_FOREACH(te, mempool_list, next) {
895                 mp = (struct rte_mempool *) te->data;
896                 if (strncmp(name, mp->name, RTE_MEMPOOL_NAMESIZE) == 0)
897                         break;
898         }
899
900         rte_rwlock_read_unlock(RTE_EAL_MEMPOOL_RWLOCK);
901
902         if (te == NULL) {
903                 rte_errno = ENOENT;
904                 return NULL;
905         }
906
907         return mp;
908 }
909
910 void rte_mempool_walk(void (*func)(const struct rte_mempool *, void *),
911                       void *arg)
912 {
913         struct rte_tailq_entry *te = NULL;
914         struct rte_mempool_list *mempool_list;
915
916         mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
917
918         rte_rwlock_read_lock(RTE_EAL_MEMPOOL_RWLOCK);
919
920         TAILQ_FOREACH(te, mempool_list, next) {
921                 (*func)((struct rte_mempool *) te->data, arg);
922         }
923
924         rte_rwlock_read_unlock(RTE_EAL_MEMPOOL_RWLOCK);
925 }