mempool: introduce a function to create an empty pool
[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 static void
131 mempool_add_elem(struct rte_mempool *mp, void *obj, phys_addr_t physaddr)
132 {
133         struct rte_mempool_objhdr *hdr;
134         struct rte_mempool_objtlr *tlr __rte_unused;
135
136         /* set mempool ptr in header */
137         hdr = RTE_PTR_SUB(obj, sizeof(*hdr));
138         hdr->mp = mp;
139         hdr->physaddr = physaddr;
140         STAILQ_INSERT_TAIL(&mp->elt_list, hdr, next);
141         mp->populated_size++;
142
143 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
144         hdr->cookie = RTE_MEMPOOL_HEADER_COOKIE2;
145         tlr = __mempool_get_trailer(obj);
146         tlr->cookie = RTE_MEMPOOL_TRAILER_COOKIE;
147 #endif
148
149         /* enqueue in ring */
150         rte_ring_sp_enqueue(mp->ring, obj);
151 }
152
153 /* call obj_cb() for each mempool element */
154 uint32_t
155 rte_mempool_obj_iter(struct rte_mempool *mp,
156         rte_mempool_obj_cb_t *obj_cb, void *obj_cb_arg)
157 {
158         struct rte_mempool_objhdr *hdr;
159         void *obj;
160         unsigned n = 0;
161
162         STAILQ_FOREACH(hdr, &mp->elt_list, next) {
163                 obj = (char *)hdr + sizeof(*hdr);
164                 obj_cb(mp, obj_cb_arg, obj, n);
165                 n++;
166         }
167
168         return n;
169 }
170
171 /* call mem_cb() for each mempool memory chunk */
172 uint32_t
173 rte_mempool_mem_iter(struct rte_mempool *mp,
174         rte_mempool_mem_cb_t *mem_cb, void *mem_cb_arg)
175 {
176         struct rte_mempool_memhdr *hdr;
177         unsigned n = 0;
178
179         STAILQ_FOREACH(hdr, &mp->mem_list, next) {
180                 mem_cb(mp, mem_cb_arg, hdr, n);
181                 n++;
182         }
183
184         return n;
185 }
186
187 /* get the header, trailer and total size of a mempool element. */
188 uint32_t
189 rte_mempool_calc_obj_size(uint32_t elt_size, uint32_t flags,
190         struct rte_mempool_objsz *sz)
191 {
192         struct rte_mempool_objsz lsz;
193
194         sz = (sz != NULL) ? sz : &lsz;
195
196         sz->header_size = sizeof(struct rte_mempool_objhdr);
197         if ((flags & MEMPOOL_F_NO_CACHE_ALIGN) == 0)
198                 sz->header_size = RTE_ALIGN_CEIL(sz->header_size,
199                         RTE_MEMPOOL_ALIGN);
200
201         sz->trailer_size = sizeof(struct rte_mempool_objtlr);
202
203         /* element size is 8 bytes-aligned at least */
204         sz->elt_size = RTE_ALIGN_CEIL(elt_size, sizeof(uint64_t));
205
206         /* expand trailer to next cache line */
207         if ((flags & MEMPOOL_F_NO_CACHE_ALIGN) == 0) {
208                 sz->total_size = sz->header_size + sz->elt_size +
209                         sz->trailer_size;
210                 sz->trailer_size += ((RTE_MEMPOOL_ALIGN -
211                                   (sz->total_size & RTE_MEMPOOL_ALIGN_MASK)) &
212                                  RTE_MEMPOOL_ALIGN_MASK);
213         }
214
215         /*
216          * increase trailer to add padding between objects in order to
217          * spread them across memory channels/ranks
218          */
219         if ((flags & MEMPOOL_F_NO_SPREAD) == 0) {
220                 unsigned new_size;
221                 new_size = optimize_object_size(sz->header_size + sz->elt_size +
222                         sz->trailer_size);
223                 sz->trailer_size = new_size - sz->header_size - sz->elt_size;
224         }
225
226         /* this is the size of an object, including header and trailer */
227         sz->total_size = sz->header_size + sz->elt_size + sz->trailer_size;
228
229         return sz->total_size;
230 }
231
232
233 /*
234  * Calculate maximum amount of memory required to store given number of objects.
235  */
236 size_t
237 rte_mempool_xmem_size(uint32_t elt_num, size_t total_elt_sz, uint32_t pg_shift)
238 {
239         size_t obj_per_page, pg_num, pg_sz;
240
241         if (pg_shift == 0)
242                 return total_elt_sz * elt_num;
243
244         pg_sz = (size_t)1 << pg_shift;
245         obj_per_page = pg_sz / total_elt_sz;
246         if (obj_per_page == 0)
247                 return RTE_ALIGN_CEIL(total_elt_sz, pg_sz) * elt_num;
248
249         pg_num = (elt_num + obj_per_page - 1) / obj_per_page;
250         return pg_num << pg_shift;
251 }
252
253 /*
254  * Calculate how much memory would be actually required with the
255  * given memory footprint to store required number of elements.
256  */
257 ssize_t
258 rte_mempool_xmem_usage(__rte_unused void *vaddr, uint32_t elt_num,
259         size_t total_elt_sz, const phys_addr_t paddr[], uint32_t pg_num,
260         uint32_t pg_shift)
261 {
262         uint32_t elt_cnt = 0;
263         phys_addr_t start, end;
264         uint32_t paddr_idx;
265         size_t pg_sz = (size_t)1 << pg_shift;
266
267         /* if paddr is NULL, assume contiguous memory */
268         if (paddr == NULL) {
269                 start = 0;
270                 end = pg_sz * pg_num;
271                 paddr_idx = pg_num;
272         } else {
273                 start = paddr[0];
274                 end = paddr[0] + pg_sz;
275                 paddr_idx = 1;
276         }
277         while (elt_cnt < elt_num) {
278
279                 if (end - start >= total_elt_sz) {
280                         /* enough contiguous memory, add an object */
281                         start += total_elt_sz;
282                         elt_cnt++;
283                 } else if (paddr_idx < pg_num) {
284                         /* no room to store one obj, add a page */
285                         if (end == paddr[paddr_idx]) {
286                                 end += pg_sz;
287                         } else {
288                                 start = paddr[paddr_idx];
289                                 end = paddr[paddr_idx] + pg_sz;
290                         }
291                         paddr_idx++;
292
293                 } else {
294                         /* no more page, return how many elements fit */
295                         return -(size_t)elt_cnt;
296                 }
297         }
298
299         return (size_t)paddr_idx << pg_shift;
300 }
301
302 #ifndef RTE_LIBRTE_XEN_DOM0
303 /* stub if DOM0 support not configured */
304 struct rte_mempool *
305 rte_dom0_mempool_create(const char *name __rte_unused,
306                         unsigned n __rte_unused,
307                         unsigned elt_size __rte_unused,
308                         unsigned cache_size __rte_unused,
309                         unsigned private_data_size __rte_unused,
310                         rte_mempool_ctor_t *mp_init __rte_unused,
311                         void *mp_init_arg __rte_unused,
312                         rte_mempool_obj_ctor_t *obj_init __rte_unused,
313                         void *obj_init_arg __rte_unused,
314                         int socket_id __rte_unused,
315                         unsigned flags __rte_unused)
316 {
317         rte_errno = EINVAL;
318         return NULL;
319 }
320 #endif
321
322 /* create the internal ring */
323 static int
324 rte_mempool_ring_create(struct rte_mempool *mp)
325 {
326         int rg_flags = 0;
327         char rg_name[RTE_RING_NAMESIZE];
328         struct rte_ring *r;
329
330         snprintf(rg_name, sizeof(rg_name), RTE_MEMPOOL_MZ_FORMAT, mp->name);
331
332         /* ring flags */
333         if (mp->flags & MEMPOOL_F_SP_PUT)
334                 rg_flags |= RING_F_SP_ENQ;
335         if (mp->flags & MEMPOOL_F_SC_GET)
336                 rg_flags |= RING_F_SC_DEQ;
337
338         /* Allocate the ring that will be used to store objects.
339          * Ring functions will return appropriate errors if we are
340          * running as a secondary process etc., so no checks made
341          * in this function for that condition.
342          */
343         r = rte_ring_create(rg_name, rte_align32pow2(mp->size + 1),
344                 mp->socket_id, rg_flags);
345         if (r == NULL)
346                 return -rte_errno;
347
348         mp->ring = r;
349         return 0;
350 }
351
352 /* free a memchunk allocated with rte_memzone_reserve() */
353 static void
354 rte_mempool_memchunk_mz_free(__rte_unused struct rte_mempool_memhdr *memhdr,
355         void *opaque)
356 {
357         const struct rte_memzone *mz = opaque;
358         rte_memzone_free(mz);
359 }
360
361 /* Free memory chunks used by a mempool. Objects must be in pool */
362 static void
363 rte_mempool_free_memchunks(struct rte_mempool *mp)
364 {
365         struct rte_mempool_memhdr *memhdr;
366         void *elt;
367
368         while (!STAILQ_EMPTY(&mp->elt_list)) {
369                 rte_ring_sc_dequeue(mp->ring, &elt);
370                 (void)elt;
371                 STAILQ_REMOVE_HEAD(&mp->elt_list, next);
372                 mp->populated_size--;
373         }
374
375         while (!STAILQ_EMPTY(&mp->mem_list)) {
376                 memhdr = STAILQ_FIRST(&mp->mem_list);
377                 STAILQ_REMOVE_HEAD(&mp->mem_list, next);
378                 if (memhdr->free_cb != NULL)
379                         memhdr->free_cb(memhdr, memhdr->opaque);
380                 rte_free(memhdr);
381                 mp->nb_mem_chunks--;
382         }
383 }
384
385 /* Add objects in the pool, using a physically contiguous memory
386  * zone. Return the number of objects added, or a negative value
387  * on error.
388  */
389 static int
390 rte_mempool_populate_phys(struct rte_mempool *mp, char *vaddr,
391         phys_addr_t paddr, size_t len, rte_mempool_memchunk_free_cb_t *free_cb,
392         void *opaque)
393 {
394         unsigned total_elt_sz;
395         unsigned i = 0;
396         size_t off;
397         struct rte_mempool_memhdr *memhdr;
398
399         /* mempool is already populated */
400         if (mp->populated_size >= mp->size)
401                 return -ENOSPC;
402
403         total_elt_sz = mp->header_size + mp->elt_size + mp->trailer_size;
404
405         memhdr = rte_zmalloc("MEMPOOL_MEMHDR", sizeof(*memhdr), 0);
406         if (memhdr == NULL)
407                 return -ENOMEM;
408
409         memhdr->mp = mp;
410         memhdr->addr = vaddr;
411         memhdr->phys_addr = paddr;
412         memhdr->len = len;
413         memhdr->free_cb = free_cb;
414         memhdr->opaque = opaque;
415
416         if (mp->flags & MEMPOOL_F_NO_CACHE_ALIGN)
417                 off = RTE_PTR_ALIGN_CEIL(vaddr, 8) - vaddr;
418         else
419                 off = RTE_PTR_ALIGN_CEIL(vaddr, RTE_CACHE_LINE_SIZE) - vaddr;
420
421         while (off + total_elt_sz <= len && mp->populated_size < mp->size) {
422                 off += mp->header_size;
423                 mempool_add_elem(mp, (char *)vaddr + off, paddr + off);
424                 off += mp->elt_size + mp->trailer_size;
425                 i++;
426         }
427
428         /* not enough room to store one object */
429         if (i == 0)
430                 return -EINVAL;
431
432         STAILQ_INSERT_TAIL(&mp->mem_list, memhdr, next);
433         mp->nb_mem_chunks++;
434         return i;
435 }
436
437 /* Add objects in the pool, using a table of physical pages. Return the
438  * number of objects added, or a negative value on error.
439  */
440 static int
441 rte_mempool_populate_phys_tab(struct rte_mempool *mp, char *vaddr,
442         const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift,
443         rte_mempool_memchunk_free_cb_t *free_cb, void *opaque)
444 {
445         uint32_t i, n;
446         int ret, cnt = 0;
447         size_t pg_sz = (size_t)1 << pg_shift;
448
449         /* mempool must not be populated */
450         if (mp->nb_mem_chunks != 0)
451                 return -EEXIST;
452
453         for (i = 0; i < pg_num && mp->populated_size < mp->size; i += n) {
454
455                 /* populate with the largest group of contiguous pages */
456                 for (n = 1; (i + n) < pg_num &&
457                              paddr[i] + pg_sz == paddr[i+n]; n++)
458                         ;
459
460                 ret = rte_mempool_populate_phys(mp, vaddr + i * pg_sz,
461                         paddr[i], n * pg_sz, free_cb, opaque);
462                 if (ret < 0) {
463                         rte_mempool_free_memchunks(mp);
464                         return ret;
465                 }
466                 /* no need to call the free callback for next chunks */
467                 free_cb = NULL;
468                 cnt += ret;
469         }
470         return cnt;
471 }
472
473 /* Populate the mempool with a virtual area. Return the number of
474  * objects added, or a negative value on error.
475  */
476 static int
477 rte_mempool_populate_virt(struct rte_mempool *mp, char *addr,
478         size_t len, size_t pg_sz, rte_mempool_memchunk_free_cb_t *free_cb,
479         void *opaque)
480 {
481         phys_addr_t paddr;
482         size_t off, phys_len;
483         int ret, cnt = 0;
484
485         /* mempool must not be populated */
486         if (mp->nb_mem_chunks != 0)
487                 return -EEXIST;
488         /* address and len must be page-aligned */
489         if (RTE_PTR_ALIGN_CEIL(addr, pg_sz) != addr)
490                 return -EINVAL;
491         if (RTE_ALIGN_CEIL(len, pg_sz) != len)
492                 return -EINVAL;
493
494         for (off = 0; off + pg_sz <= len &&
495                      mp->populated_size < mp->size; off += phys_len) {
496
497                 paddr = rte_mem_virt2phy(addr + off);
498                 if (paddr == RTE_BAD_PHYS_ADDR) {
499                         ret = -EINVAL;
500                         goto fail;
501                 }
502
503                 /* populate with the largest group of contiguous pages */
504                 for (phys_len = pg_sz; off + phys_len < len; phys_len += pg_sz) {
505                         phys_addr_t paddr_tmp;
506
507                         paddr_tmp = rte_mem_virt2phy(addr + off + phys_len);
508                         paddr_tmp = rte_mem_phy2mch(-1, paddr_tmp);
509
510                         if (paddr_tmp != paddr + phys_len)
511                                 break;
512                 }
513
514                 ret = rte_mempool_populate_phys(mp, addr + off, paddr,
515                         phys_len, free_cb, opaque);
516                 if (ret < 0)
517                         goto fail;
518                 /* no need to call the free callback for next chunks */
519                 free_cb = NULL;
520                 cnt += ret;
521         }
522
523         return cnt;
524
525  fail:
526         rte_mempool_free_memchunks(mp);
527         return ret;
528 }
529
530 /* Default function to populate the mempool: allocate memory in memzones,
531  * and populate them. Return the number of objects added, or a negative
532  * value on error.
533  */
534 static int
535 rte_mempool_populate_default(struct rte_mempool *mp)
536 {
537         int mz_flags = RTE_MEMZONE_1GB|RTE_MEMZONE_SIZE_HINT_ONLY;
538         char mz_name[RTE_MEMZONE_NAMESIZE];
539         const struct rte_memzone *mz;
540         size_t size, total_elt_sz, align, pg_sz, pg_shift;
541         unsigned mz_id, n;
542         int ret;
543
544         /* mempool must not be populated */
545         if (mp->nb_mem_chunks != 0)
546                 return -EEXIST;
547
548         if (rte_eal_has_hugepages()) {
549                 pg_shift = 0; /* not needed, zone is physically contiguous */
550                 pg_sz = 0;
551                 align = RTE_CACHE_LINE_SIZE;
552         } else {
553                 pg_sz = getpagesize();
554                 pg_shift = rte_bsf32(pg_sz);
555                 align = pg_sz;
556         }
557
558         total_elt_sz = mp->header_size + mp->elt_size + mp->trailer_size;
559         for (mz_id = 0, n = mp->size; n > 0; mz_id++, n -= ret) {
560                 size = rte_mempool_xmem_size(n, total_elt_sz, pg_shift);
561
562                 ret = snprintf(mz_name, sizeof(mz_name),
563                         RTE_MEMPOOL_MZ_FORMAT "_%d", mp->name, mz_id);
564                 if (ret < 0 || ret >= (int)sizeof(mz_name)) {
565                         ret = -ENAMETOOLONG;
566                         goto fail;
567                 }
568
569                 mz = rte_memzone_reserve_aligned(mz_name, size,
570                         mp->socket_id, mz_flags, align);
571                 /* not enough memory, retry with the biggest zone we have */
572                 if (mz == NULL)
573                         mz = rte_memzone_reserve_aligned(mz_name, 0,
574                                 mp->socket_id, mz_flags, align);
575                 if (mz == NULL) {
576                         ret = -rte_errno;
577                         goto fail;
578                 }
579
580                 if (rte_eal_has_hugepages())
581                         ret = rte_mempool_populate_phys(mp, mz->addr,
582                                 mz->phys_addr, mz->len,
583                                 rte_mempool_memchunk_mz_free,
584                                 (void *)(uintptr_t)mz);
585                 else
586                         ret = rte_mempool_populate_virt(mp, mz->addr,
587                                 mz->len, pg_sz,
588                                 rte_mempool_memchunk_mz_free,
589                                 (void *)(uintptr_t)mz);
590                 if (ret < 0)
591                         goto fail;
592         }
593
594         return mp->size;
595
596  fail:
597         rte_mempool_free_memchunks(mp);
598         return ret;
599 }
600
601 /* free a mempool */
602 static void
603 rte_mempool_free(struct rte_mempool *mp)
604 {
605         struct rte_mempool_list *mempool_list = NULL;
606         struct rte_tailq_entry *te;
607
608         if (mp == NULL)
609                 return;
610
611         mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
612         rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
613         /* find out tailq entry */
614         TAILQ_FOREACH(te, mempool_list, next) {
615                 if (te->data == (void *)mp)
616                         break;
617         }
618
619         if (te != NULL) {
620                 TAILQ_REMOVE(mempool_list, te, next);
621                 rte_free(te);
622         }
623         rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
624
625         rte_mempool_free_memchunks(mp);
626         rte_ring_free(mp->ring);
627         rte_memzone_free(mp->mz);
628 }
629
630 /* create an empty mempool */
631 static struct rte_mempool *
632 rte_mempool_create_empty(const char *name, unsigned n, unsigned elt_size,
633         unsigned cache_size, unsigned private_data_size,
634         int socket_id, unsigned flags)
635 {
636         char mz_name[RTE_MEMZONE_NAMESIZE];
637         struct rte_mempool_list *mempool_list;
638         struct rte_mempool *mp = NULL;
639         struct rte_tailq_entry *te = NULL;
640         const struct rte_memzone *mz = NULL;
641         size_t mempool_size;
642         int mz_flags = RTE_MEMZONE_1GB|RTE_MEMZONE_SIZE_HINT_ONLY;
643         struct rte_mempool_objsz objsz;
644
645         /* compilation-time checks */
646         RTE_BUILD_BUG_ON((sizeof(struct rte_mempool) &
647                           RTE_CACHE_LINE_MASK) != 0);
648         RTE_BUILD_BUG_ON((sizeof(struct rte_mempool_cache) &
649                           RTE_CACHE_LINE_MASK) != 0);
650 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
651         RTE_BUILD_BUG_ON((sizeof(struct rte_mempool_debug_stats) &
652                           RTE_CACHE_LINE_MASK) != 0);
653         RTE_BUILD_BUG_ON((offsetof(struct rte_mempool, stats) &
654                           RTE_CACHE_LINE_MASK) != 0);
655 #endif
656
657         mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
658
659         /* asked cache too big */
660         if (cache_size > RTE_MEMPOOL_CACHE_MAX_SIZE ||
661             CALC_CACHE_FLUSHTHRESH(cache_size) > n) {
662                 rte_errno = EINVAL;
663                 return NULL;
664         }
665
666         /* "no cache align" imply "no spread" */
667         if (flags & MEMPOOL_F_NO_CACHE_ALIGN)
668                 flags |= MEMPOOL_F_NO_SPREAD;
669
670         /* calculate mempool object sizes. */
671         if (!rte_mempool_calc_obj_size(elt_size, flags, &objsz)) {
672                 rte_errno = EINVAL;
673                 return NULL;
674         }
675
676         rte_rwlock_write_lock(RTE_EAL_MEMPOOL_RWLOCK);
677
678         /*
679          * reserve a memory zone for this mempool: private data is
680          * cache-aligned
681          */
682         private_data_size = (private_data_size +
683                              RTE_MEMPOOL_ALIGN_MASK) & (~RTE_MEMPOOL_ALIGN_MASK);
684
685
686         /* try to allocate tailq entry */
687         te = rte_zmalloc("MEMPOOL_TAILQ_ENTRY", sizeof(*te), 0);
688         if (te == NULL) {
689                 RTE_LOG(ERR, MEMPOOL, "Cannot allocate tailq entry!\n");
690                 goto exit_unlock;
691         }
692
693         mempool_size = MEMPOOL_HEADER_SIZE(mp, cache_size);
694         mempool_size += private_data_size;
695         mempool_size = RTE_ALIGN_CEIL(mempool_size, RTE_MEMPOOL_ALIGN);
696
697         snprintf(mz_name, sizeof(mz_name), RTE_MEMPOOL_MZ_FORMAT, name);
698
699         mz = rte_memzone_reserve(mz_name, mempool_size, socket_id, mz_flags);
700         if (mz == NULL)
701                 goto exit_unlock;
702
703         /* init the mempool structure */
704         mp = mz->addr;
705         memset(mp, 0, sizeof(*mp));
706         snprintf(mp->name, sizeof(mp->name), "%s", name);
707         mp->mz = mz;
708         mp->socket_id = socket_id;
709         mp->size = n;
710         mp->flags = flags;
711         mp->socket_id = socket_id;
712         mp->elt_size = objsz.elt_size;
713         mp->header_size = objsz.header_size;
714         mp->trailer_size = objsz.trailer_size;
715         mp->cache_size = cache_size;
716         mp->cache_flushthresh = CALC_CACHE_FLUSHTHRESH(cache_size);
717         mp->private_data_size = private_data_size;
718         STAILQ_INIT(&mp->elt_list);
719         STAILQ_INIT(&mp->mem_list);
720
721         if (rte_mempool_ring_create(mp) < 0)
722                 goto exit_unlock;
723
724         /*
725          * local_cache pointer is set even if cache_size is zero.
726          * The local_cache points to just past the elt_pa[] array.
727          */
728         mp->local_cache = (struct rte_mempool_cache *)
729                 RTE_PTR_ADD(mp, MEMPOOL_HEADER_SIZE(mp, 0));
730
731         te->data = mp;
732         rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
733         TAILQ_INSERT_TAIL(mempool_list, te, next);
734         rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
735         rte_rwlock_write_unlock(RTE_EAL_MEMPOOL_RWLOCK);
736
737         return mp;
738
739 exit_unlock:
740         rte_rwlock_write_unlock(RTE_EAL_MEMPOOL_RWLOCK);
741         rte_free(te);
742         rte_mempool_free(mp);
743         return NULL;
744 }
745
746 /* create the mempool */
747 struct rte_mempool *
748 rte_mempool_create(const char *name, unsigned n, unsigned elt_size,
749         unsigned cache_size, unsigned private_data_size,
750         rte_mempool_ctor_t *mp_init, void *mp_init_arg,
751         rte_mempool_obj_cb_t *obj_init, void *obj_init_arg,
752         int socket_id, unsigned flags)
753 {
754         struct rte_mempool *mp;
755
756         if (rte_xen_dom0_supported())
757                 return rte_dom0_mempool_create(name, n, elt_size,
758                                                cache_size, private_data_size,
759                                                mp_init, mp_init_arg,
760                                                obj_init, obj_init_arg,
761                                                socket_id, flags);
762
763         mp = rte_mempool_create_empty(name, n, elt_size, cache_size,
764                 private_data_size, socket_id, flags);
765         if (mp == NULL)
766                 return NULL;
767
768         /* call the mempool priv initializer */
769         if (mp_init)
770                 mp_init(mp, mp_init_arg);
771
772         if (rte_mempool_populate_default(mp) < 0)
773                 goto fail;
774
775         /* call the object initializers */
776         if (obj_init)
777                 rte_mempool_obj_iter(mp, obj_init, obj_init_arg);
778
779         return mp;
780
781  fail:
782         rte_mempool_free(mp);
783         return NULL;
784 }
785
786 /*
787  * Create the mempool over already allocated chunk of memory.
788  * That external memory buffer can consists of physically disjoint pages.
789  * Setting vaddr to NULL, makes mempool to fallback to original behaviour
790  * and allocate space for mempool and it's elements as one big chunk of
791  * physically continuos memory.
792  */
793 struct rte_mempool *
794 rte_mempool_xmem_create(const char *name, unsigned n, unsigned elt_size,
795                 unsigned cache_size, unsigned private_data_size,
796                 rte_mempool_ctor_t *mp_init, void *mp_init_arg,
797                 rte_mempool_obj_cb_t *obj_init, void *obj_init_arg,
798                 int socket_id, unsigned flags, void *vaddr,
799                 const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift)
800 {
801         struct rte_mempool *mp = NULL;
802         int ret;
803
804         /* no virtual address supplied, use rte_mempool_create() */
805         if (vaddr == NULL)
806                 return rte_mempool_create(name, n, elt_size, cache_size,
807                         private_data_size, mp_init, mp_init_arg,
808                         obj_init, obj_init_arg, socket_id, flags);
809
810         /* check that we have both VA and PA */
811         if (paddr == NULL) {
812                 rte_errno = EINVAL;
813                 return NULL;
814         }
815
816         /* Check that pg_shift parameter is valid. */
817         if (pg_shift > MEMPOOL_PG_SHIFT_MAX) {
818                 rte_errno = EINVAL;
819                 return NULL;
820         }
821
822         mp = rte_mempool_create_empty(name, n, elt_size, cache_size,
823                 private_data_size, socket_id, flags);
824         if (mp == NULL)
825                 return NULL;
826
827         /* call the mempool priv initializer */
828         if (mp_init)
829                 mp_init(mp, mp_init_arg);
830
831         ret = rte_mempool_populate_phys_tab(mp, vaddr, paddr, pg_num, pg_shift,
832                 NULL, NULL);
833         if (ret < 0 || ret != (int)mp->size)
834                 goto fail;
835
836         /* call the object initializers */
837         if (obj_init)
838                 rte_mempool_obj_iter(mp, obj_init, obj_init_arg);
839
840         return mp;
841
842  fail:
843         rte_mempool_free(mp);
844         return NULL;
845 }
846
847 /* Return the number of entries in the mempool */
848 unsigned
849 rte_mempool_count(const struct rte_mempool *mp)
850 {
851         unsigned count;
852         unsigned lcore_id;
853
854         count = rte_ring_count(mp->ring);
855
856         if (mp->cache_size == 0)
857                 return count;
858
859         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++)
860                 count += mp->local_cache[lcore_id].len;
861
862         /*
863          * due to race condition (access to len is not locked), the
864          * total can be greater than size... so fix the result
865          */
866         if (count > mp->size)
867                 return mp->size;
868         return count;
869 }
870
871 /* dump the cache status */
872 static unsigned
873 rte_mempool_dump_cache(FILE *f, const struct rte_mempool *mp)
874 {
875         unsigned lcore_id;
876         unsigned count = 0;
877         unsigned cache_count;
878
879         fprintf(f, "  cache infos:\n");
880         fprintf(f, "    cache_size=%"PRIu32"\n", mp->cache_size);
881
882         if (mp->cache_size == 0)
883                 return count;
884
885         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
886                 cache_count = mp->local_cache[lcore_id].len;
887                 fprintf(f, "    cache_count[%u]=%u\n", lcore_id, cache_count);
888                 count += cache_count;
889         }
890         fprintf(f, "    total_cache_count=%u\n", count);
891         return count;
892 }
893
894 #ifndef __INTEL_COMPILER
895 #pragma GCC diagnostic ignored "-Wcast-qual"
896 #endif
897
898 /* check and update cookies or panic (internal) */
899 void rte_mempool_check_cookies(const struct rte_mempool *mp,
900         void * const *obj_table_const, unsigned n, int free)
901 {
902 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
903         struct rte_mempool_objhdr *hdr;
904         struct rte_mempool_objtlr *tlr;
905         uint64_t cookie;
906         void *tmp;
907         void *obj;
908         void **obj_table;
909
910         /* Force to drop the "const" attribute. This is done only when
911          * DEBUG is enabled */
912         tmp = (void *) obj_table_const;
913         obj_table = (void **) tmp;
914
915         while (n--) {
916                 obj = obj_table[n];
917
918                 if (rte_mempool_from_obj(obj) != mp)
919                         rte_panic("MEMPOOL: object is owned by another "
920                                   "mempool\n");
921
922                 hdr = __mempool_get_header(obj);
923                 cookie = hdr->cookie;
924
925                 if (free == 0) {
926                         if (cookie != RTE_MEMPOOL_HEADER_COOKIE1) {
927                                 rte_log_set_history(0);
928                                 RTE_LOG(CRIT, MEMPOOL,
929                                         "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
930                                         obj, (const void *) mp, cookie);
931                                 rte_panic("MEMPOOL: bad header cookie (put)\n");
932                         }
933                         hdr->cookie = RTE_MEMPOOL_HEADER_COOKIE2;
934                 } else if (free == 1) {
935                         if (cookie != RTE_MEMPOOL_HEADER_COOKIE2) {
936                                 rte_log_set_history(0);
937                                 RTE_LOG(CRIT, MEMPOOL,
938                                         "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
939                                         obj, (const void *) mp, cookie);
940                                 rte_panic("MEMPOOL: bad header cookie (get)\n");
941                         }
942                         hdr->cookie = RTE_MEMPOOL_HEADER_COOKIE1;
943                 } else if (free == 2) {
944                         if (cookie != RTE_MEMPOOL_HEADER_COOKIE1 &&
945                             cookie != RTE_MEMPOOL_HEADER_COOKIE2) {
946                                 rte_log_set_history(0);
947                                 RTE_LOG(CRIT, MEMPOOL,
948                                         "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
949                                         obj, (const void *) mp, cookie);
950                                 rte_panic("MEMPOOL: bad header cookie (audit)\n");
951                         }
952                 }
953                 tlr = __mempool_get_trailer(obj);
954                 cookie = tlr->cookie;
955                 if (cookie != RTE_MEMPOOL_TRAILER_COOKIE) {
956                         rte_log_set_history(0);
957                         RTE_LOG(CRIT, MEMPOOL,
958                                 "obj=%p, mempool=%p, cookie=%" PRIx64 "\n",
959                                 obj, (const void *) mp, cookie);
960                         rte_panic("MEMPOOL: bad trailer cookie\n");
961                 }
962         }
963 #else
964         RTE_SET_USED(mp);
965         RTE_SET_USED(obj_table_const);
966         RTE_SET_USED(n);
967         RTE_SET_USED(free);
968 #endif
969 }
970
971 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
972 static void
973 mempool_obj_audit(struct rte_mempool *mp, __rte_unused void *opaque,
974         void *obj, __rte_unused unsigned idx)
975 {
976         __mempool_check_cookies(mp, &obj, 1, 2);
977 }
978
979 static void
980 mempool_audit_cookies(struct rte_mempool *mp)
981 {
982         unsigned num;
983
984         num = rte_mempool_obj_iter(mp, mempool_obj_audit, NULL);
985         if (num != mp->size) {
986                 rte_panic("rte_mempool_obj_iter(mempool=%p, size=%u) "
987                         "iterated only over %u elements\n",
988                         mp, mp->size, num);
989         }
990 }
991 #else
992 #define mempool_audit_cookies(mp) do {} while(0)
993 #endif
994
995 #ifndef __INTEL_COMPILER
996 #pragma GCC diagnostic error "-Wcast-qual"
997 #endif
998
999 /* check cookies before and after objects */
1000 static void
1001 mempool_audit_cache(const struct rte_mempool *mp)
1002 {
1003         /* check cache size consistency */
1004         unsigned lcore_id;
1005
1006         if (mp->cache_size == 0)
1007                 return;
1008
1009         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1010                 if (mp->local_cache[lcore_id].len > mp->cache_flushthresh) {
1011                         RTE_LOG(CRIT, MEMPOOL, "badness on cache[%u]\n",
1012                                 lcore_id);
1013                         rte_panic("MEMPOOL: invalid cache len\n");
1014                 }
1015         }
1016 }
1017
1018 /* check the consistency of mempool (size, cookies, ...) */
1019 void
1020 rte_mempool_audit(struct rte_mempool *mp)
1021 {
1022         mempool_audit_cache(mp);
1023         mempool_audit_cookies(mp);
1024
1025         /* For case where mempool DEBUG is not set, and cache size is 0 */
1026         RTE_SET_USED(mp);
1027 }
1028
1029 /* dump the status of the mempool on the console */
1030 void
1031 rte_mempool_dump(FILE *f, struct rte_mempool *mp)
1032 {
1033 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
1034         struct rte_mempool_debug_stats sum;
1035         unsigned lcore_id;
1036 #endif
1037         struct rte_mempool_memhdr *memhdr;
1038         unsigned common_count;
1039         unsigned cache_count;
1040         size_t mem_len = 0;
1041
1042         RTE_ASSERT(f != NULL);
1043         RTE_ASSERT(mp != NULL);
1044
1045         fprintf(f, "mempool <%s>@%p\n", mp->name, mp);
1046         fprintf(f, "  flags=%x\n", mp->flags);
1047         fprintf(f, "  ring=<%s>@%p\n", mp->ring->name, mp->ring);
1048         fprintf(f, "  phys_addr=0x%" PRIx64 "\n", mp->mz->phys_addr);
1049         fprintf(f, "  nb_mem_chunks=%u\n", mp->nb_mem_chunks);
1050         fprintf(f, "  size=%"PRIu32"\n", mp->size);
1051         fprintf(f, "  populated_size=%"PRIu32"\n", mp->populated_size);
1052         fprintf(f, "  header_size=%"PRIu32"\n", mp->header_size);
1053         fprintf(f, "  elt_size=%"PRIu32"\n", mp->elt_size);
1054         fprintf(f, "  trailer_size=%"PRIu32"\n", mp->trailer_size);
1055         fprintf(f, "  total_obj_size=%"PRIu32"\n",
1056                mp->header_size + mp->elt_size + mp->trailer_size);
1057
1058         fprintf(f, "  private_data_size=%"PRIu32"\n", mp->private_data_size);
1059
1060         STAILQ_FOREACH(memhdr, &mp->mem_list, next)
1061                 mem_len += memhdr->len;
1062         if (mem_len != 0) {
1063                 fprintf(f, "  avg bytes/object=%#Lf\n",
1064                         (long double)mem_len / mp->size);
1065         }
1066
1067         cache_count = rte_mempool_dump_cache(f, mp);
1068         common_count = rte_ring_count(mp->ring);
1069         if ((cache_count + common_count) > mp->size)
1070                 common_count = mp->size - cache_count;
1071         fprintf(f, "  common_pool_count=%u\n", common_count);
1072
1073         /* sum and dump statistics */
1074 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
1075         memset(&sum, 0, sizeof(sum));
1076         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1077                 sum.put_bulk += mp->stats[lcore_id].put_bulk;
1078                 sum.put_objs += mp->stats[lcore_id].put_objs;
1079                 sum.get_success_bulk += mp->stats[lcore_id].get_success_bulk;
1080                 sum.get_success_objs += mp->stats[lcore_id].get_success_objs;
1081                 sum.get_fail_bulk += mp->stats[lcore_id].get_fail_bulk;
1082                 sum.get_fail_objs += mp->stats[lcore_id].get_fail_objs;
1083         }
1084         fprintf(f, "  stats:\n");
1085         fprintf(f, "    put_bulk=%"PRIu64"\n", sum.put_bulk);
1086         fprintf(f, "    put_objs=%"PRIu64"\n", sum.put_objs);
1087         fprintf(f, "    get_success_bulk=%"PRIu64"\n", sum.get_success_bulk);
1088         fprintf(f, "    get_success_objs=%"PRIu64"\n", sum.get_success_objs);
1089         fprintf(f, "    get_fail_bulk=%"PRIu64"\n", sum.get_fail_bulk);
1090         fprintf(f, "    get_fail_objs=%"PRIu64"\n", sum.get_fail_objs);
1091 #else
1092         fprintf(f, "  no statistics available\n");
1093 #endif
1094
1095         rte_mempool_audit(mp);
1096 }
1097
1098 /* dump the status of all mempools on the console */
1099 void
1100 rte_mempool_list_dump(FILE *f)
1101 {
1102         struct rte_mempool *mp = NULL;
1103         struct rte_tailq_entry *te;
1104         struct rte_mempool_list *mempool_list;
1105
1106         mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
1107
1108         rte_rwlock_read_lock(RTE_EAL_MEMPOOL_RWLOCK);
1109
1110         TAILQ_FOREACH(te, mempool_list, next) {
1111                 mp = (struct rte_mempool *) te->data;
1112                 rte_mempool_dump(f, mp);
1113         }
1114
1115         rte_rwlock_read_unlock(RTE_EAL_MEMPOOL_RWLOCK);
1116 }
1117
1118 /* search a mempool from its name */
1119 struct rte_mempool *
1120 rte_mempool_lookup(const char *name)
1121 {
1122         struct rte_mempool *mp = NULL;
1123         struct rte_tailq_entry *te;
1124         struct rte_mempool_list *mempool_list;
1125
1126         mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
1127
1128         rte_rwlock_read_lock(RTE_EAL_MEMPOOL_RWLOCK);
1129
1130         TAILQ_FOREACH(te, mempool_list, next) {
1131                 mp = (struct rte_mempool *) te->data;
1132                 if (strncmp(name, mp->name, RTE_MEMPOOL_NAMESIZE) == 0)
1133                         break;
1134         }
1135
1136         rte_rwlock_read_unlock(RTE_EAL_MEMPOOL_RWLOCK);
1137
1138         if (te == NULL) {
1139                 rte_errno = ENOENT;
1140                 return NULL;
1141         }
1142
1143         return mp;
1144 }
1145
1146 void rte_mempool_walk(void (*func)(struct rte_mempool *, void *),
1147                       void *arg)
1148 {
1149         struct rte_tailq_entry *te = NULL;
1150         struct rte_mempool_list *mempool_list;
1151
1152         mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
1153
1154         rte_rwlock_read_lock(RTE_EAL_MEMPOOL_RWLOCK);
1155
1156         TAILQ_FOREACH(te, mempool_list, next) {
1157                 (*func)((struct rte_mempool *) te->data, arg);
1158         }
1159
1160         rte_rwlock_read_unlock(RTE_EAL_MEMPOOL_RWLOCK);
1161 }