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