mempool: use the list to iterate the elements
[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
44 #include <rte_common.h>
45 #include <rte_log.h>
46 #include <rte_debug.h>
47 #include <rte_memory.h>
48 #include <rte_memzone.h>
49 #include <rte_malloc.h>
50 #include <rte_atomic.h>
51 #include <rte_launch.h>
52 #include <rte_eal.h>
53 #include <rte_eal_memconfig.h>
54 #include <rte_per_lcore.h>
55 #include <rte_lcore.h>
56 #include <rte_branch_prediction.h>
57 #include <rte_ring.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 /**
131  * A mempool object iterator callback function.
132  */
133 typedef void (*rte_mempool_obj_iter_t)(void * /*obj_iter_arg*/,
134         void * /*obj_start*/,
135         void * /*obj_end*/,
136         uint32_t /*obj_index */);
137
138 static void
139 mempool_add_elem(struct rte_mempool *mp, void *obj, uint32_t obj_idx,
140         rte_mempool_obj_cb_t *obj_init, void *obj_init_arg)
141 {
142         struct rte_mempool_objhdr *hdr;
143         struct rte_mempool_objtlr *tlr __rte_unused;
144
145         obj = (char *)obj + mp->header_size;
146
147         /* set mempool ptr in header */
148         hdr = RTE_PTR_SUB(obj, sizeof(*hdr));
149         hdr->mp = mp;
150         STAILQ_INSERT_TAIL(&mp->elt_list, hdr, next);
151
152 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
153         hdr->cookie = RTE_MEMPOOL_HEADER_COOKIE2;
154         tlr = __mempool_get_trailer(obj);
155         tlr->cookie = RTE_MEMPOOL_TRAILER_COOKIE;
156 #endif
157         /* call the initializer */
158         if (obj_init)
159                 obj_init(mp, obj_init_arg, obj, obj_idx);
160
161         /* enqueue in ring */
162         rte_ring_sp_enqueue(mp->ring, obj);
163 }
164
165 /* Iterate through objects at the given address
166  *
167  * Given the pointer to the memory, and its topology in physical memory
168  * (the physical addresses table), iterate through the "elt_num" objects
169  * of size "elt_sz" aligned at "align". For each object in this memory
170  * chunk, invoke a callback. It returns the effective number of objects
171  * in this memory.
172  */
173 static uint32_t
174 rte_mempool_obj_mem_iter(void *vaddr, uint32_t elt_num, size_t total_elt_sz,
175         size_t align, const phys_addr_t paddr[], uint32_t pg_num,
176         uint32_t pg_shift, rte_mempool_obj_iter_t obj_iter, void *obj_iter_arg)
177 {
178         uint32_t i, j, k;
179         uint32_t pgn, pgf;
180         uintptr_t end, start, va;
181         uintptr_t pg_sz;
182
183         pg_sz = (uintptr_t)1 << pg_shift;
184         va = (uintptr_t)vaddr;
185
186         i = 0;
187         j = 0;
188
189         while (i != elt_num && j != pg_num) {
190
191                 start = RTE_ALIGN_CEIL(va, align);
192                 end = start + total_elt_sz;
193
194                 /* index of the first page for the next element. */
195                 pgf = (end >> pg_shift) - (start >> pg_shift);
196
197                 /* index of the last page for the current element. */
198                 pgn = ((end - 1) >> pg_shift) - (start >> pg_shift);
199                 pgn += j;
200
201                 /* do we have enough space left for the element. */
202                 if (pgn >= pg_num)
203                         break;
204
205                 for (k = j;
206                                 k != pgn &&
207                                 paddr[k] + pg_sz == paddr[k + 1];
208                                 k++)
209                         ;
210
211                 /*
212                  * if next pgn chunks of memory physically continuous,
213                  * use it to create next element.
214                  * otherwise, just skip that chunk unused.
215                  */
216                 if (k == pgn) {
217                         if (obj_iter != NULL)
218                                 obj_iter(obj_iter_arg, (void *)start,
219                                         (void *)end, i);
220                         va = end;
221                         j += pgf;
222                         i++;
223                 } else {
224                         va = RTE_ALIGN_CEIL((va + 1), pg_sz);
225                         j++;
226                 }
227         }
228
229         return i;
230 }
231
232 /* call obj_cb() for each mempool element */
233 uint32_t
234 rte_mempool_obj_iter(struct rte_mempool *mp,
235         rte_mempool_obj_cb_t *obj_cb, void *obj_cb_arg)
236 {
237         struct rte_mempool_objhdr *hdr;
238         void *obj;
239         unsigned n = 0;
240
241         STAILQ_FOREACH(hdr, &mp->elt_list, next) {
242                 obj = (char *)hdr + sizeof(*hdr);
243                 obj_cb(mp, obj_cb_arg, obj, n);
244                 n++;
245         }
246
247         return n;
248 }
249
250 /*
251  * Populate  mempool with the objects.
252  */
253
254 struct mempool_populate_arg {
255         struct rte_mempool     *mp;
256         rte_mempool_obj_cb_t   *obj_init;
257         void                   *obj_init_arg;
258 };
259
260 static void
261 mempool_obj_populate(void *arg, void *start, void *end, uint32_t idx)
262 {
263         struct mempool_populate_arg *pa = arg;
264
265         mempool_add_elem(pa->mp, start, idx, pa->obj_init, pa->obj_init_arg);
266         pa->mp->elt_va_end = (uintptr_t)end;
267 }
268
269 static void
270 mempool_populate(struct rte_mempool *mp, size_t num, size_t align,
271         rte_mempool_obj_cb_t *obj_init, void *obj_init_arg)
272 {
273         uint32_t elt_sz;
274         struct mempool_populate_arg arg;
275
276         elt_sz = mp->elt_size + mp->header_size + mp->trailer_size;
277         arg.mp = mp;
278         arg.obj_init = obj_init;
279         arg.obj_init_arg = obj_init_arg;
280
281         mp->size = rte_mempool_obj_mem_iter((void *)mp->elt_va_start,
282                 num, elt_sz, align,
283                 mp->elt_pa, mp->pg_num, mp->pg_shift,
284                 mempool_obj_populate, &arg);
285 }
286
287 /* get the header, trailer and total size of a mempool element. */
288 uint32_t
289 rte_mempool_calc_obj_size(uint32_t elt_size, uint32_t flags,
290         struct rte_mempool_objsz *sz)
291 {
292         struct rte_mempool_objsz lsz;
293
294         sz = (sz != NULL) ? sz : &lsz;
295
296         sz->header_size = sizeof(struct rte_mempool_objhdr);
297         if ((flags & MEMPOOL_F_NO_CACHE_ALIGN) == 0)
298                 sz->header_size = RTE_ALIGN_CEIL(sz->header_size,
299                         RTE_MEMPOOL_ALIGN);
300
301         sz->trailer_size = sizeof(struct rte_mempool_objtlr);
302
303         /* element size is 8 bytes-aligned at least */
304         sz->elt_size = RTE_ALIGN_CEIL(elt_size, sizeof(uint64_t));
305
306         /* expand trailer to next cache line */
307         if ((flags & MEMPOOL_F_NO_CACHE_ALIGN) == 0) {
308                 sz->total_size = sz->header_size + sz->elt_size +
309                         sz->trailer_size;
310                 sz->trailer_size += ((RTE_MEMPOOL_ALIGN -
311                                   (sz->total_size & RTE_MEMPOOL_ALIGN_MASK)) &
312                                  RTE_MEMPOOL_ALIGN_MASK);
313         }
314
315         /*
316          * increase trailer to add padding between objects in order to
317          * spread them across memory channels/ranks
318          */
319         if ((flags & MEMPOOL_F_NO_SPREAD) == 0) {
320                 unsigned new_size;
321                 new_size = optimize_object_size(sz->header_size + sz->elt_size +
322                         sz->trailer_size);
323                 sz->trailer_size = new_size - sz->header_size - sz->elt_size;
324         }
325
326         if (! rte_eal_has_hugepages()) {
327                 /*
328                  * compute trailer size so that pool elements fit exactly in
329                  * a standard page
330                  */
331                 int page_size = getpagesize();
332                 int new_size = page_size - sz->header_size - sz->elt_size;
333                 if (new_size < 0 || (unsigned int)new_size < sz->trailer_size) {
334                         printf("When hugepages are disabled, pool objects "
335                                "can't exceed PAGE_SIZE: %d + %d + %d > %d\n",
336                                sz->header_size, sz->elt_size, sz->trailer_size,
337                                page_size);
338                         return 0;
339                 }
340                 sz->trailer_size = new_size;
341         }
342
343         /* this is the size of an object, including header and trailer */
344         sz->total_size = sz->header_size + sz->elt_size + sz->trailer_size;
345
346         return sz->total_size;
347 }
348
349
350 /*
351  * Calculate maximum amount of memory required to store given number of objects.
352  */
353 size_t
354 rte_mempool_xmem_size(uint32_t elt_num, size_t total_elt_sz, uint32_t pg_shift)
355 {
356         size_t n, pg_num, pg_sz, sz;
357
358         pg_sz = (size_t)1 << pg_shift;
359
360         if ((n = pg_sz / total_elt_sz) > 0) {
361                 pg_num = (elt_num + n - 1) / n;
362                 sz = pg_num << pg_shift;
363         } else {
364                 sz = RTE_ALIGN_CEIL(total_elt_sz, pg_sz) * elt_num;
365         }
366
367         return sz;
368 }
369
370 /* Callback used by rte_mempool_xmem_usage(): it sets the opaque
371  * argument to the end of the object.
372  */
373 static void
374 mempool_lelem_iter(void *arg, __rte_unused void *start, void *end,
375         __rte_unused uint32_t idx)
376 {
377         *(uintptr_t *)arg = (uintptr_t)end;
378 }
379
380 /*
381  * Calculate how much memory would be actually required with the
382  * given memory footprint to store required number of elements.
383  */
384 ssize_t
385 rte_mempool_xmem_usage(void *vaddr, uint32_t elt_num, size_t total_elt_sz,
386         const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift)
387 {
388         uint32_t n;
389         uintptr_t va, uv;
390         size_t pg_sz, usz;
391
392         pg_sz = (size_t)1 << pg_shift;
393         va = (uintptr_t)vaddr;
394         uv = va;
395
396         if ((n = rte_mempool_obj_mem_iter(vaddr, elt_num, total_elt_sz, 1,
397                         paddr, pg_num, pg_shift, mempool_lelem_iter,
398                         &uv)) != elt_num) {
399                 return -(ssize_t)n;
400         }
401
402         uv = RTE_ALIGN_CEIL(uv, pg_sz);
403         usz = uv - va;
404         return usz;
405 }
406
407 #ifndef RTE_LIBRTE_XEN_DOM0
408 /* stub if DOM0 support not configured */
409 struct rte_mempool *
410 rte_dom0_mempool_create(const char *name __rte_unused,
411                         unsigned n __rte_unused,
412                         unsigned elt_size __rte_unused,
413                         unsigned cache_size __rte_unused,
414                         unsigned private_data_size __rte_unused,
415                         rte_mempool_ctor_t *mp_init __rte_unused,
416                         void *mp_init_arg __rte_unused,
417                         rte_mempool_obj_ctor_t *obj_init __rte_unused,
418                         void *obj_init_arg __rte_unused,
419                         int socket_id __rte_unused,
420                         unsigned flags __rte_unused)
421 {
422         rte_errno = EINVAL;
423         return NULL;
424 }
425 #endif
426
427 /* create the mempool */
428 struct rte_mempool *
429 rte_mempool_create(const char *name, unsigned n, unsigned elt_size,
430                    unsigned cache_size, unsigned private_data_size,
431                    rte_mempool_ctor_t *mp_init, void *mp_init_arg,
432                    rte_mempool_obj_ctor_t *obj_init, void *obj_init_arg,
433                    int socket_id, unsigned flags)
434 {
435         if (rte_xen_dom0_supported())
436                 return rte_dom0_mempool_create(name, n, elt_size,
437                                                cache_size, private_data_size,
438                                                mp_init, mp_init_arg,
439                                                obj_init, obj_init_arg,
440                                                socket_id, flags);
441         else
442                 return rte_mempool_xmem_create(name, n, elt_size,
443                                                cache_size, private_data_size,
444                                                mp_init, mp_init_arg,
445                                                obj_init, obj_init_arg,
446                                                socket_id, flags,
447                                                NULL, NULL, MEMPOOL_PG_NUM_DEFAULT,
448                                                MEMPOOL_PG_SHIFT_MAX);
449 }
450
451 /*
452  * Create the mempool over already allocated chunk of memory.
453  * That external memory buffer can consists of physically disjoint pages.
454  * Setting vaddr to NULL, makes mempool to fallback to original behaviour
455  * and allocate space for mempool and it's elements as one big chunk of
456  * physically continuos memory.
457  * */
458 struct rte_mempool *
459 rte_mempool_xmem_create(const char *name, unsigned n, unsigned elt_size,
460                 unsigned cache_size, unsigned private_data_size,
461                 rte_mempool_ctor_t *mp_init, void *mp_init_arg,
462                 rte_mempool_obj_cb_t *obj_init, void *obj_init_arg,
463                 int socket_id, unsigned flags, void *vaddr,
464                 const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift)
465 {
466         char mz_name[RTE_MEMZONE_NAMESIZE];
467         char rg_name[RTE_RING_NAMESIZE];
468         struct rte_mempool_list *mempool_list;
469         struct rte_mempool *mp = NULL;
470         struct rte_tailq_entry *te = NULL;
471         struct rte_ring *r = NULL;
472         const struct rte_memzone *mz;
473         size_t mempool_size;
474         int mz_flags = RTE_MEMZONE_1GB|RTE_MEMZONE_SIZE_HINT_ONLY;
475         int rg_flags = 0;
476         void *obj;
477         struct rte_mempool_objsz objsz;
478         void *startaddr;
479         int page_size = getpagesize();
480
481         /* compilation-time checks */
482         RTE_BUILD_BUG_ON((sizeof(struct rte_mempool) &
483                           RTE_CACHE_LINE_MASK) != 0);
484         RTE_BUILD_BUG_ON((sizeof(struct rte_mempool_cache) &
485                           RTE_CACHE_LINE_MASK) != 0);
486 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
487         RTE_BUILD_BUG_ON((sizeof(struct rte_mempool_debug_stats) &
488                           RTE_CACHE_LINE_MASK) != 0);
489         RTE_BUILD_BUG_ON((offsetof(struct rte_mempool, stats) &
490                           RTE_CACHE_LINE_MASK) != 0);
491 #endif
492
493         mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
494
495         /* asked cache too big */
496         if (cache_size > RTE_MEMPOOL_CACHE_MAX_SIZE ||
497             CALC_CACHE_FLUSHTHRESH(cache_size) > n) {
498                 rte_errno = EINVAL;
499                 return NULL;
500         }
501
502         /* check that we have both VA and PA */
503         if (vaddr != NULL && paddr == NULL) {
504                 rte_errno = EINVAL;
505                 return NULL;
506         }
507
508         /* Check that pg_num and pg_shift parameters are valid. */
509         if (pg_num < RTE_DIM(mp->elt_pa) || pg_shift > MEMPOOL_PG_SHIFT_MAX) {
510                 rte_errno = EINVAL;
511                 return NULL;
512         }
513
514         /* "no cache align" imply "no spread" */
515         if (flags & MEMPOOL_F_NO_CACHE_ALIGN)
516                 flags |= MEMPOOL_F_NO_SPREAD;
517
518         /* ring flags */
519         if (flags & MEMPOOL_F_SP_PUT)
520                 rg_flags |= RING_F_SP_ENQ;
521         if (flags & MEMPOOL_F_SC_GET)
522                 rg_flags |= RING_F_SC_DEQ;
523
524         /* calculate mempool object sizes. */
525         if (!rte_mempool_calc_obj_size(elt_size, flags, &objsz)) {
526                 rte_errno = EINVAL;
527                 return NULL;
528         }
529
530         rte_rwlock_write_lock(RTE_EAL_MEMPOOL_RWLOCK);
531
532         /* allocate the ring that will be used to store objects */
533         /* Ring functions will return appropriate errors if we are
534          * running as a secondary process etc., so no checks made
535          * in this function for that condition */
536         snprintf(rg_name, sizeof(rg_name), RTE_MEMPOOL_MZ_FORMAT, name);
537         r = rte_ring_create(rg_name, rte_align32pow2(n+1), socket_id, rg_flags);
538         if (r == NULL)
539                 goto exit_unlock;
540
541         /*
542          * reserve a memory zone for this mempool: private data is
543          * cache-aligned
544          */
545         private_data_size = (private_data_size +
546                              RTE_MEMPOOL_ALIGN_MASK) & (~RTE_MEMPOOL_ALIGN_MASK);
547
548         if (! rte_eal_has_hugepages()) {
549                 /*
550                  * expand private data size to a whole page, so that the
551                  * first pool element will start on a new standard page
552                  */
553                 int head = sizeof(struct rte_mempool);
554                 int new_size = (private_data_size + head) % page_size;
555                 if (new_size)
556                         private_data_size += page_size - new_size;
557         }
558
559         /* try to allocate tailq entry */
560         te = rte_zmalloc("MEMPOOL_TAILQ_ENTRY", sizeof(*te), 0);
561         if (te == NULL) {
562                 RTE_LOG(ERR, MEMPOOL, "Cannot allocate tailq entry!\n");
563                 goto exit_unlock;
564         }
565
566         /*
567          * If user provided an external memory buffer, then use it to
568          * store mempool objects. Otherwise reserve a memzone that is large
569          * enough to hold mempool header and metadata plus mempool objects.
570          */
571         mempool_size = MEMPOOL_HEADER_SIZE(mp, pg_num, cache_size);
572         mempool_size += private_data_size;
573         mempool_size = RTE_ALIGN_CEIL(mempool_size, RTE_MEMPOOL_ALIGN);
574         if (vaddr == NULL)
575                 mempool_size += (size_t)objsz.total_size * n;
576
577         if (! rte_eal_has_hugepages()) {
578                 /*
579                  * we want the memory pool to start on a page boundary,
580                  * because pool elements crossing page boundaries would
581                  * result in discontiguous physical addresses
582                  */
583                 mempool_size += page_size;
584         }
585
586         snprintf(mz_name, sizeof(mz_name), RTE_MEMPOOL_MZ_FORMAT, name);
587
588         mz = rte_memzone_reserve(mz_name, mempool_size, socket_id, mz_flags);
589         if (mz == NULL)
590                 goto exit_unlock;
591
592         if (rte_eal_has_hugepages()) {
593                 startaddr = (void*)mz->addr;
594         } else {
595                 /* align memory pool start address on a page boundary */
596                 unsigned long addr = (unsigned long)mz->addr;
597                 if (addr & (page_size - 1)) {
598                         addr += page_size;
599                         addr &= ~(page_size - 1);
600                 }
601                 startaddr = (void*)addr;
602         }
603
604         /* init the mempool structure */
605         mp = startaddr;
606         memset(mp, 0, sizeof(*mp));
607         snprintf(mp->name, sizeof(mp->name), "%s", name);
608         mp->phys_addr = mz->phys_addr;
609         mp->ring = r;
610         mp->size = n;
611         mp->flags = flags;
612         mp->elt_size = objsz.elt_size;
613         mp->header_size = objsz.header_size;
614         mp->trailer_size = objsz.trailer_size;
615         mp->cache_size = cache_size;
616         mp->cache_flushthresh = CALC_CACHE_FLUSHTHRESH(cache_size);
617         mp->private_data_size = private_data_size;
618         STAILQ_INIT(&mp->elt_list);
619
620         /*
621          * local_cache pointer is set even if cache_size is zero.
622          * The local_cache points to just past the elt_pa[] array.
623          */
624         mp->local_cache = (struct rte_mempool_cache *)
625                 RTE_PTR_ADD(mp, MEMPOOL_HEADER_SIZE(mp, pg_num, 0));
626
627         /* calculate address of the first element for continuous mempool. */
628         obj = (char *)mp + MEMPOOL_HEADER_SIZE(mp, pg_num, cache_size) +
629                 private_data_size;
630         obj = RTE_PTR_ALIGN_CEIL(obj, RTE_MEMPOOL_ALIGN);
631
632         /* populate address translation fields. */
633         mp->pg_num = pg_num;
634         mp->pg_shift = pg_shift;
635         mp->pg_mask = RTE_LEN2MASK(mp->pg_shift, typeof(mp->pg_mask));
636
637         /* mempool elements allocated together with mempool */
638         if (vaddr == NULL) {
639                 mp->elt_va_start = (uintptr_t)obj;
640                 mp->elt_pa[0] = mp->phys_addr +
641                         (mp->elt_va_start - (uintptr_t)mp);
642         } else {
643                 /* mempool elements in a separate chunk of memory. */
644                 mp->elt_va_start = (uintptr_t)vaddr;
645                 memcpy(mp->elt_pa, paddr, sizeof (mp->elt_pa[0]) * pg_num);
646         }
647
648         mp->elt_va_end = mp->elt_va_start;
649
650         /* call the initializer */
651         if (mp_init)
652                 mp_init(mp, mp_init_arg);
653
654         mempool_populate(mp, n, 1, obj_init, obj_init_arg);
655
656         te->data = (void *) mp;
657
658         rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
659         TAILQ_INSERT_TAIL(mempool_list, te, next);
660         rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
661         rte_rwlock_write_unlock(RTE_EAL_MEMPOOL_RWLOCK);
662
663         return mp;
664
665 exit_unlock:
666         rte_rwlock_write_unlock(RTE_EAL_MEMPOOL_RWLOCK);
667         rte_ring_free(r);
668         rte_free(te);
669
670         return NULL;
671 }
672
673 /* Return the number of entries in the mempool */
674 unsigned
675 rte_mempool_count(const struct rte_mempool *mp)
676 {
677         unsigned count;
678         unsigned lcore_id;
679
680         count = rte_ring_count(mp->ring);
681
682         if (mp->cache_size == 0)
683                 return count;
684
685         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++)
686                 count += mp->local_cache[lcore_id].len;
687
688         /*
689          * due to race condition (access to len is not locked), the
690          * total can be greater than size... so fix the result
691          */
692         if (count > mp->size)
693                 return mp->size;
694         return count;
695 }
696
697 /* dump the cache status */
698 static unsigned
699 rte_mempool_dump_cache(FILE *f, const struct rte_mempool *mp)
700 {
701         unsigned lcore_id;
702         unsigned count = 0;
703         unsigned cache_count;
704
705         fprintf(f, "  cache infos:\n");
706         fprintf(f, "    cache_size=%"PRIu32"\n", mp->cache_size);
707
708         if (mp->cache_size == 0)
709                 return count;
710
711         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
712                 cache_count = mp->local_cache[lcore_id].len;
713                 fprintf(f, "    cache_count[%u]=%u\n", lcore_id, cache_count);
714                 count += cache_count;
715         }
716         fprintf(f, "    total_cache_count=%u\n", count);
717         return count;
718 }
719
720 #ifndef __INTEL_COMPILER
721 #pragma GCC diagnostic ignored "-Wcast-qual"
722 #endif
723
724 struct mempool_audit_arg {
725         const struct rte_mempool *mp;
726         uintptr_t obj_end;
727         uint32_t obj_num;
728 };
729
730 /* check and update cookies or panic (internal) */
731 void rte_mempool_check_cookies(const struct rte_mempool *mp,
732         void * const *obj_table_const, unsigned n, int free)
733 {
734 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
735         struct rte_mempool_objhdr *hdr;
736         struct rte_mempool_objtlr *tlr;
737         uint64_t cookie;
738         void *tmp;
739         void *obj;
740         void **obj_table;
741
742         /* Force to drop the "const" attribute. This is done only when
743          * DEBUG is enabled */
744         tmp = (void *) obj_table_const;
745         obj_table = (void **) tmp;
746
747         while (n--) {
748                 obj = obj_table[n];
749
750                 if (rte_mempool_from_obj(obj) != mp)
751                         rte_panic("MEMPOOL: object is owned by another "
752                                   "mempool\n");
753
754                 hdr = __mempool_get_header(obj);
755                 cookie = hdr->cookie;
756
757                 if (free == 0) {
758                         if (cookie != RTE_MEMPOOL_HEADER_COOKIE1) {
759                                 rte_log_set_history(0);
760                                 RTE_LOG(CRIT, MEMPOOL,
761                                         "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
762                                         obj, (const void *) mp, cookie);
763                                 rte_panic("MEMPOOL: bad header cookie (put)\n");
764                         }
765                         hdr->cookie = RTE_MEMPOOL_HEADER_COOKIE2;
766                 } else if (free == 1) {
767                         if (cookie != RTE_MEMPOOL_HEADER_COOKIE2) {
768                                 rte_log_set_history(0);
769                                 RTE_LOG(CRIT, MEMPOOL,
770                                         "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
771                                         obj, (const void *) mp, cookie);
772                                 rte_panic("MEMPOOL: bad header cookie (get)\n");
773                         }
774                         hdr->cookie = RTE_MEMPOOL_HEADER_COOKIE1;
775                 } else if (free == 2) {
776                         if (cookie != RTE_MEMPOOL_HEADER_COOKIE1 &&
777                             cookie != RTE_MEMPOOL_HEADER_COOKIE2) {
778                                 rte_log_set_history(0);
779                                 RTE_LOG(CRIT, MEMPOOL,
780                                         "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
781                                         obj, (const void *) mp, cookie);
782                                 rte_panic("MEMPOOL: bad header cookie (audit)\n");
783                         }
784                 }
785                 tlr = __mempool_get_trailer(obj);
786                 cookie = tlr->cookie;
787                 if (cookie != RTE_MEMPOOL_TRAILER_COOKIE) {
788                         rte_log_set_history(0);
789                         RTE_LOG(CRIT, MEMPOOL,
790                                 "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
791                                 obj, (const void *) mp, cookie);
792                         rte_panic("MEMPOOL: bad trailer cookie\n");
793                 }
794         }
795 #else
796         RTE_SET_USED(mp);
797         RTE_SET_USED(obj_table_const);
798         RTE_SET_USED(n);
799         RTE_SET_USED(free);
800 #endif
801 }
802
803 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
804 static void
805 mempool_obj_audit(void *arg, void *start, void *end, uint32_t idx)
806 {
807         struct mempool_audit_arg *pa = arg;
808         void *obj;
809
810         obj = (char *)start + pa->mp->header_size;
811         pa->obj_end = (uintptr_t)end;
812         pa->obj_num = idx + 1;
813         __mempool_check_cookies(pa->mp, &obj, 1, 2);
814 }
815
816 static void
817 mempool_audit_cookies(struct rte_mempool *mp)
818 {
819         uint32_t elt_sz, num;
820         struct mempool_audit_arg arg;
821
822         elt_sz = mp->elt_size + mp->header_size + mp->trailer_size;
823
824         arg.mp = mp;
825         arg.obj_end = mp->elt_va_start;
826         arg.obj_num = 0;
827
828         num = rte_mempool_obj_mem_iter((void *)mp->elt_va_start,
829                 mp->size, elt_sz, 1,
830                 mp->elt_pa, mp->pg_num, mp->pg_shift,
831                 mempool_obj_audit, &arg);
832
833         if (num != mp->size) {
834                         rte_panic("rte_mempool_obj_iter(mempool=%p, size=%u) "
835                         "iterated only over %u elements\n",
836                         mp, mp->size, num);
837         } else if (arg.obj_end != mp->elt_va_end || arg.obj_num != mp->size) {
838                         rte_panic("rte_mempool_obj_iter(mempool=%p, size=%u) "
839                         "last callback va_end: %#tx (%#tx expeceted), "
840                         "num of objects: %u (%u expected)\n",
841                         mp, mp->size,
842                         arg.obj_end, mp->elt_va_end,
843                         arg.obj_num, mp->size);
844         }
845 }
846 #else
847 #define mempool_audit_cookies(mp) do {} while(0)
848 #endif
849
850 #ifndef __INTEL_COMPILER
851 #pragma GCC diagnostic error "-Wcast-qual"
852 #endif
853
854 /* check cookies before and after objects */
855 static void
856 mempool_audit_cache(const struct rte_mempool *mp)
857 {
858         /* check cache size consistency */
859         unsigned lcore_id;
860
861         if (mp->cache_size == 0)
862                 return;
863
864         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
865                 if (mp->local_cache[lcore_id].len > mp->cache_flushthresh) {
866                         RTE_LOG(CRIT, MEMPOOL, "badness on cache[%u]\n",
867                                 lcore_id);
868                         rte_panic("MEMPOOL: invalid cache len\n");
869                 }
870         }
871 }
872
873 /* check the consistency of mempool (size, cookies, ...) */
874 void
875 rte_mempool_audit(struct rte_mempool *mp)
876 {
877         mempool_audit_cache(mp);
878         mempool_audit_cookies(mp);
879
880         /* For case where mempool DEBUG is not set, and cache size is 0 */
881         RTE_SET_USED(mp);
882 }
883
884 /* dump the status of the mempool on the console */
885 void
886 rte_mempool_dump(FILE *f, struct rte_mempool *mp)
887 {
888 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
889         struct rte_mempool_debug_stats sum;
890         unsigned lcore_id;
891 #endif
892         unsigned common_count;
893         unsigned cache_count;
894
895         RTE_ASSERT(f != NULL);
896         RTE_ASSERT(mp != NULL);
897
898         fprintf(f, "mempool <%s>@%p\n", mp->name, mp);
899         fprintf(f, "  flags=%x\n", mp->flags);
900         fprintf(f, "  ring=<%s>@%p\n", mp->ring->name, mp->ring);
901         fprintf(f, "  phys_addr=0x%" PRIx64 "\n", mp->phys_addr);
902         fprintf(f, "  size=%"PRIu32"\n", mp->size);
903         fprintf(f, "  header_size=%"PRIu32"\n", mp->header_size);
904         fprintf(f, "  elt_size=%"PRIu32"\n", mp->elt_size);
905         fprintf(f, "  trailer_size=%"PRIu32"\n", mp->trailer_size);
906         fprintf(f, "  total_obj_size=%"PRIu32"\n",
907                mp->header_size + mp->elt_size + mp->trailer_size);
908
909         fprintf(f, "  private_data_size=%"PRIu32"\n", mp->private_data_size);
910         fprintf(f, "  pg_num=%"PRIu32"\n", mp->pg_num);
911         fprintf(f, "  pg_shift=%"PRIu32"\n", mp->pg_shift);
912         fprintf(f, "  pg_mask=%#tx\n", mp->pg_mask);
913         fprintf(f, "  elt_va_start=%#tx\n", mp->elt_va_start);
914         fprintf(f, "  elt_va_end=%#tx\n", mp->elt_va_end);
915         fprintf(f, "  elt_pa[0]=0x%" PRIx64 "\n", mp->elt_pa[0]);
916
917         if (mp->size != 0)
918                 fprintf(f, "  avg bytes/object=%#Lf\n",
919                         (long double)(mp->elt_va_end - mp->elt_va_start) /
920                         mp->size);
921
922         cache_count = rte_mempool_dump_cache(f, mp);
923         common_count = rte_ring_count(mp->ring);
924         if ((cache_count + common_count) > mp->size)
925                 common_count = mp->size - cache_count;
926         fprintf(f, "  common_pool_count=%u\n", common_count);
927
928         /* sum and dump statistics */
929 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
930         memset(&sum, 0, sizeof(sum));
931         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
932                 sum.put_bulk += mp->stats[lcore_id].put_bulk;
933                 sum.put_objs += mp->stats[lcore_id].put_objs;
934                 sum.get_success_bulk += mp->stats[lcore_id].get_success_bulk;
935                 sum.get_success_objs += mp->stats[lcore_id].get_success_objs;
936                 sum.get_fail_bulk += mp->stats[lcore_id].get_fail_bulk;
937                 sum.get_fail_objs += mp->stats[lcore_id].get_fail_objs;
938         }
939         fprintf(f, "  stats:\n");
940         fprintf(f, "    put_bulk=%"PRIu64"\n", sum.put_bulk);
941         fprintf(f, "    put_objs=%"PRIu64"\n", sum.put_objs);
942         fprintf(f, "    get_success_bulk=%"PRIu64"\n", sum.get_success_bulk);
943         fprintf(f, "    get_success_objs=%"PRIu64"\n", sum.get_success_objs);
944         fprintf(f, "    get_fail_bulk=%"PRIu64"\n", sum.get_fail_bulk);
945         fprintf(f, "    get_fail_objs=%"PRIu64"\n", sum.get_fail_objs);
946 #else
947         fprintf(f, "  no statistics available\n");
948 #endif
949
950         rte_mempool_audit(mp);
951 }
952
953 /* dump the status of all mempools on the console */
954 void
955 rte_mempool_list_dump(FILE *f)
956 {
957         struct rte_mempool *mp = NULL;
958         struct rte_tailq_entry *te;
959         struct rte_mempool_list *mempool_list;
960
961         mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
962
963         rte_rwlock_read_lock(RTE_EAL_MEMPOOL_RWLOCK);
964
965         TAILQ_FOREACH(te, mempool_list, next) {
966                 mp = (struct rte_mempool *) te->data;
967                 rte_mempool_dump(f, mp);
968         }
969
970         rte_rwlock_read_unlock(RTE_EAL_MEMPOOL_RWLOCK);
971 }
972
973 /* search a mempool from its name */
974 struct rte_mempool *
975 rte_mempool_lookup(const char *name)
976 {
977         struct rte_mempool *mp = NULL;
978         struct rte_tailq_entry *te;
979         struct rte_mempool_list *mempool_list;
980
981         mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
982
983         rte_rwlock_read_lock(RTE_EAL_MEMPOOL_RWLOCK);
984
985         TAILQ_FOREACH(te, mempool_list, next) {
986                 mp = (struct rte_mempool *) te->data;
987                 if (strncmp(name, mp->name, RTE_MEMPOOL_NAMESIZE) == 0)
988                         break;
989         }
990
991         rte_rwlock_read_unlock(RTE_EAL_MEMPOOL_RWLOCK);
992
993         if (te == NULL) {
994                 rte_errno = ENOENT;
995                 return NULL;
996         }
997
998         return mp;
999 }
1000
1001 void rte_mempool_walk(void (*func)(struct rte_mempool *, void *),
1002                       void *arg)
1003 {
1004         struct rte_tailq_entry *te = NULL;
1005         struct rte_mempool_list *mempool_list;
1006
1007         mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
1008
1009         rte_rwlock_read_lock(RTE_EAL_MEMPOOL_RWLOCK);
1010
1011         TAILQ_FOREACH(te, mempool_list, next) {
1012                 (*func)((struct rte_mempool *) te->data, arg);
1013         }
1014
1015         rte_rwlock_read_unlock(RTE_EAL_MEMPOOL_RWLOCK);
1016 }