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