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