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