1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2010-2014 Intel Corporation. 3 * Copyright(c) 2016 6WIND S.A. 4 */ 5 6 #include <stdbool.h> 7 #include <stdlib.h> 8 #include <stdio.h> 9 #include <string.h> 10 #include <stdint.h> 11 #include <unistd.h> 12 #include <inttypes.h> 13 #include <errno.h> 14 #include <sys/queue.h> 15 16 #include <rte_common.h> 17 #include <rte_log.h> 18 #include <rte_debug.h> 19 #include <rte_memory.h> 20 #include <rte_memzone.h> 21 #include <rte_malloc.h> 22 #include <rte_eal.h> 23 #include <rte_eal_memconfig.h> 24 #include <rte_errno.h> 25 #include <rte_string_fns.h> 26 #include <rte_tailq.h> 27 #include <rte_eal_paging.h> 28 #include <rte_telemetry.h> 29 30 #include "rte_mempool.h" 31 #include "rte_mempool_trace.h" 32 33 TAILQ_HEAD(rte_mempool_list, rte_tailq_entry); 34 35 static struct rte_tailq_elem rte_mempool_tailq = { 36 .name = "RTE_MEMPOOL", 37 }; 38 EAL_REGISTER_TAILQ(rte_mempool_tailq) 39 40 TAILQ_HEAD(mempool_callback_tailq, mempool_callback_data); 41 42 static struct mempool_callback_tailq callback_tailq = 43 TAILQ_HEAD_INITIALIZER(callback_tailq); 44 45 /* Invoke all registered mempool event callbacks. */ 46 static void 47 mempool_event_callback_invoke(enum rte_mempool_event event, 48 struct rte_mempool *mp); 49 50 #define CACHE_FLUSHTHRESH_MULTIPLIER 1.5 51 #define CALC_CACHE_FLUSHTHRESH(c) \ 52 ((typeof(c))((c) * CACHE_FLUSHTHRESH_MULTIPLIER)) 53 54 #if defined(RTE_ARCH_X86) 55 /* 56 * return the greatest common divisor between a and b (fast algorithm) 57 * 58 */ 59 static unsigned get_gcd(unsigned a, unsigned b) 60 { 61 unsigned c; 62 63 if (0 == a) 64 return b; 65 if (0 == b) 66 return a; 67 68 if (a < b) { 69 c = a; 70 a = b; 71 b = c; 72 } 73 74 while (b != 0) { 75 c = a % b; 76 a = b; 77 b = c; 78 } 79 80 return a; 81 } 82 83 /* 84 * Depending on memory configuration on x86 arch, objects addresses are spread 85 * between channels and ranks in RAM: the pool allocator will add 86 * padding between objects. This function return the new size of the 87 * object. 88 */ 89 static unsigned int 90 arch_mem_object_align(unsigned int obj_size) 91 { 92 unsigned nrank, nchan; 93 unsigned new_obj_size; 94 95 /* get number of channels */ 96 nchan = rte_memory_get_nchannel(); 97 if (nchan == 0) 98 nchan = 4; 99 100 nrank = rte_memory_get_nrank(); 101 if (nrank == 0) 102 nrank = 1; 103 104 /* process new object size */ 105 new_obj_size = (obj_size + RTE_MEMPOOL_ALIGN_MASK) / RTE_MEMPOOL_ALIGN; 106 while (get_gcd(new_obj_size, nrank * nchan) != 1) 107 new_obj_size++; 108 return new_obj_size * RTE_MEMPOOL_ALIGN; 109 } 110 #else 111 static unsigned int 112 arch_mem_object_align(unsigned int obj_size) 113 { 114 return obj_size; 115 } 116 #endif 117 118 struct pagesz_walk_arg { 119 int socket_id; 120 size_t min; 121 }; 122 123 static int 124 find_min_pagesz(const struct rte_memseg_list *msl, void *arg) 125 { 126 struct pagesz_walk_arg *wa = arg; 127 bool valid; 128 129 /* 130 * we need to only look at page sizes available for a particular socket 131 * ID. so, we either need an exact match on socket ID (can match both 132 * native and external memory), or, if SOCKET_ID_ANY was specified as a 133 * socket ID argument, we must only look at native memory and ignore any 134 * page sizes associated with external memory. 135 */ 136 valid = msl->socket_id == wa->socket_id; 137 valid |= wa->socket_id == SOCKET_ID_ANY && msl->external == 0; 138 139 if (valid && msl->page_sz < wa->min) 140 wa->min = msl->page_sz; 141 142 return 0; 143 } 144 145 static size_t 146 get_min_page_size(int socket_id) 147 { 148 struct pagesz_walk_arg wa; 149 150 wa.min = SIZE_MAX; 151 wa.socket_id = socket_id; 152 153 rte_memseg_list_walk(find_min_pagesz, &wa); 154 155 return wa.min == SIZE_MAX ? (size_t) rte_mem_page_size() : wa.min; 156 } 157 158 159 static void 160 mempool_add_elem(struct rte_mempool *mp, __rte_unused void *opaque, 161 void *obj, rte_iova_t iova) 162 { 163 struct rte_mempool_objhdr *hdr; 164 struct rte_mempool_objtlr *tlr __rte_unused; 165 166 /* set mempool ptr in header */ 167 hdr = RTE_PTR_SUB(obj, sizeof(*hdr)); 168 hdr->mp = mp; 169 hdr->iova = iova; 170 STAILQ_INSERT_TAIL(&mp->elt_list, hdr, next); 171 mp->populated_size++; 172 173 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG 174 hdr->cookie = RTE_MEMPOOL_HEADER_COOKIE2; 175 tlr = rte_mempool_get_trailer(obj); 176 tlr->cookie = RTE_MEMPOOL_TRAILER_COOKIE; 177 #endif 178 } 179 180 /* call obj_cb() for each mempool element */ 181 uint32_t 182 rte_mempool_obj_iter(struct rte_mempool *mp, 183 rte_mempool_obj_cb_t *obj_cb, void *obj_cb_arg) 184 { 185 struct rte_mempool_objhdr *hdr; 186 void *obj; 187 unsigned n = 0; 188 189 STAILQ_FOREACH(hdr, &mp->elt_list, next) { 190 obj = (char *)hdr + sizeof(*hdr); 191 obj_cb(mp, obj_cb_arg, obj, n); 192 n++; 193 } 194 195 return n; 196 } 197 198 /* call mem_cb() for each mempool memory chunk */ 199 uint32_t 200 rte_mempool_mem_iter(struct rte_mempool *mp, 201 rte_mempool_mem_cb_t *mem_cb, void *mem_cb_arg) 202 { 203 struct rte_mempool_memhdr *hdr; 204 unsigned n = 0; 205 206 STAILQ_FOREACH(hdr, &mp->mem_list, next) { 207 mem_cb(mp, mem_cb_arg, hdr, n); 208 n++; 209 } 210 211 return n; 212 } 213 214 /* get the header, trailer and total size of a mempool element. */ 215 uint32_t 216 rte_mempool_calc_obj_size(uint32_t elt_size, uint32_t flags, 217 struct rte_mempool_objsz *sz) 218 { 219 struct rte_mempool_objsz lsz; 220 221 sz = (sz != NULL) ? sz : &lsz; 222 223 sz->header_size = sizeof(struct rte_mempool_objhdr); 224 if ((flags & RTE_MEMPOOL_F_NO_CACHE_ALIGN) == 0) 225 sz->header_size = RTE_ALIGN_CEIL(sz->header_size, 226 RTE_MEMPOOL_ALIGN); 227 228 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG 229 sz->trailer_size = sizeof(struct rte_mempool_objtlr); 230 #else 231 sz->trailer_size = 0; 232 #endif 233 234 /* element size is 8 bytes-aligned at least */ 235 sz->elt_size = RTE_ALIGN_CEIL(elt_size, sizeof(uint64_t)); 236 237 /* expand trailer to next cache line */ 238 if ((flags & RTE_MEMPOOL_F_NO_CACHE_ALIGN) == 0) { 239 sz->total_size = sz->header_size + sz->elt_size + 240 sz->trailer_size; 241 sz->trailer_size += ((RTE_MEMPOOL_ALIGN - 242 (sz->total_size & RTE_MEMPOOL_ALIGN_MASK)) & 243 RTE_MEMPOOL_ALIGN_MASK); 244 } 245 246 /* 247 * increase trailer to add padding between objects in order to 248 * spread them across memory channels/ranks 249 */ 250 if ((flags & RTE_MEMPOOL_F_NO_SPREAD) == 0) { 251 unsigned new_size; 252 new_size = arch_mem_object_align 253 (sz->header_size + sz->elt_size + sz->trailer_size); 254 sz->trailer_size = new_size - sz->header_size - sz->elt_size; 255 } 256 257 /* this is the size of an object, including header and trailer */ 258 sz->total_size = sz->header_size + sz->elt_size + sz->trailer_size; 259 260 return sz->total_size; 261 } 262 263 /* free a memchunk allocated with rte_memzone_reserve() */ 264 static void 265 rte_mempool_memchunk_mz_free(__rte_unused struct rte_mempool_memhdr *memhdr, 266 void *opaque) 267 { 268 const struct rte_memzone *mz = opaque; 269 rte_memzone_free(mz); 270 } 271 272 /* Free memory chunks used by a mempool. Objects must be in pool */ 273 static void 274 rte_mempool_free_memchunks(struct rte_mempool *mp) 275 { 276 struct rte_mempool_memhdr *memhdr; 277 void *elt; 278 279 while (!STAILQ_EMPTY(&mp->elt_list)) { 280 rte_mempool_ops_dequeue_bulk(mp, &elt, 1); 281 (void)elt; 282 STAILQ_REMOVE_HEAD(&mp->elt_list, next); 283 mp->populated_size--; 284 } 285 286 while (!STAILQ_EMPTY(&mp->mem_list)) { 287 memhdr = STAILQ_FIRST(&mp->mem_list); 288 STAILQ_REMOVE_HEAD(&mp->mem_list, next); 289 if (memhdr->free_cb != NULL) 290 memhdr->free_cb(memhdr, memhdr->opaque); 291 rte_free(memhdr); 292 mp->nb_mem_chunks--; 293 } 294 } 295 296 static int 297 mempool_ops_alloc_once(struct rte_mempool *mp) 298 { 299 int ret; 300 301 /* create the internal ring if not already done */ 302 if ((mp->flags & RTE_MEMPOOL_F_POOL_CREATED) == 0) { 303 ret = rte_mempool_ops_alloc(mp); 304 if (ret != 0) 305 return ret; 306 mp->flags |= RTE_MEMPOOL_F_POOL_CREATED; 307 } 308 return 0; 309 } 310 311 /* Add objects in the pool, using a physically contiguous memory 312 * zone. Return the number of objects added, or a negative value 313 * on error. 314 */ 315 int 316 rte_mempool_populate_iova(struct rte_mempool *mp, char *vaddr, 317 rte_iova_t iova, size_t len, rte_mempool_memchunk_free_cb_t *free_cb, 318 void *opaque) 319 { 320 unsigned i = 0; 321 size_t off; 322 struct rte_mempool_memhdr *memhdr; 323 int ret; 324 325 ret = mempool_ops_alloc_once(mp); 326 if (ret != 0) 327 return ret; 328 329 /* mempool is already populated */ 330 if (mp->populated_size >= mp->size) 331 return -ENOSPC; 332 333 memhdr = rte_zmalloc("MEMPOOL_MEMHDR", sizeof(*memhdr), 0); 334 if (memhdr == NULL) 335 return -ENOMEM; 336 337 memhdr->mp = mp; 338 memhdr->addr = vaddr; 339 memhdr->iova = iova; 340 memhdr->len = len; 341 memhdr->free_cb = free_cb; 342 memhdr->opaque = opaque; 343 344 if (mp->flags & RTE_MEMPOOL_F_NO_CACHE_ALIGN) 345 off = RTE_PTR_ALIGN_CEIL(vaddr, 8) - vaddr; 346 else 347 off = RTE_PTR_ALIGN_CEIL(vaddr, RTE_MEMPOOL_ALIGN) - vaddr; 348 349 if (off > len) { 350 ret = 0; 351 goto fail; 352 } 353 354 i = rte_mempool_ops_populate(mp, mp->size - mp->populated_size, 355 (char *)vaddr + off, 356 (iova == RTE_BAD_IOVA) ? RTE_BAD_IOVA : (iova + off), 357 len - off, mempool_add_elem, NULL); 358 359 /* not enough room to store one object */ 360 if (i == 0) { 361 ret = 0; 362 goto fail; 363 } 364 365 STAILQ_INSERT_TAIL(&mp->mem_list, memhdr, next); 366 mp->nb_mem_chunks++; 367 368 /* Check if at least some objects in the pool are now usable for IO. */ 369 if (!(mp->flags & RTE_MEMPOOL_F_NO_IOVA_CONTIG) && iova != RTE_BAD_IOVA) 370 mp->flags &= ~RTE_MEMPOOL_F_NON_IO; 371 372 /* Report the mempool as ready only when fully populated. */ 373 if (mp->populated_size >= mp->size) 374 mempool_event_callback_invoke(RTE_MEMPOOL_EVENT_READY, mp); 375 376 rte_mempool_trace_populate_iova(mp, vaddr, iova, len, free_cb, opaque); 377 return i; 378 379 fail: 380 rte_free(memhdr); 381 return ret; 382 } 383 384 static rte_iova_t 385 get_iova(void *addr) 386 { 387 struct rte_memseg *ms; 388 389 /* try registered memory first */ 390 ms = rte_mem_virt2memseg(addr, NULL); 391 if (ms == NULL || ms->iova == RTE_BAD_IOVA) 392 /* fall back to actual physical address */ 393 return rte_mem_virt2iova(addr); 394 return ms->iova + RTE_PTR_DIFF(addr, ms->addr); 395 } 396 397 /* Populate the mempool with a virtual area. Return the number of 398 * objects added, or a negative value on error. 399 */ 400 int 401 rte_mempool_populate_virt(struct rte_mempool *mp, char *addr, 402 size_t len, size_t pg_sz, rte_mempool_memchunk_free_cb_t *free_cb, 403 void *opaque) 404 { 405 rte_iova_t iova; 406 size_t off, phys_len; 407 int ret, cnt = 0; 408 409 if (mp->flags & RTE_MEMPOOL_F_NO_IOVA_CONTIG) 410 return rte_mempool_populate_iova(mp, addr, RTE_BAD_IOVA, 411 len, free_cb, opaque); 412 413 for (off = 0; off < len && 414 mp->populated_size < mp->size; off += phys_len) { 415 416 iova = get_iova(addr + off); 417 418 /* populate with the largest group of contiguous pages */ 419 for (phys_len = RTE_MIN( 420 (size_t)(RTE_PTR_ALIGN_CEIL(addr + off + 1, pg_sz) - 421 (addr + off)), 422 len - off); 423 off + phys_len < len; 424 phys_len = RTE_MIN(phys_len + pg_sz, len - off)) { 425 rte_iova_t iova_tmp; 426 427 iova_tmp = get_iova(addr + off + phys_len); 428 429 if (iova_tmp == RTE_BAD_IOVA || 430 iova_tmp != iova + phys_len) 431 break; 432 } 433 434 ret = rte_mempool_populate_iova(mp, addr + off, iova, 435 phys_len, free_cb, opaque); 436 if (ret == 0) 437 continue; 438 if (ret < 0) 439 goto fail; 440 /* no need to call the free callback for next chunks */ 441 free_cb = NULL; 442 cnt += ret; 443 } 444 445 rte_mempool_trace_populate_virt(mp, addr, len, pg_sz, free_cb, opaque); 446 return cnt; 447 448 fail: 449 rte_mempool_free_memchunks(mp); 450 return ret; 451 } 452 453 /* Get the minimal page size used in a mempool before populating it. */ 454 int 455 rte_mempool_get_page_size(struct rte_mempool *mp, size_t *pg_sz) 456 { 457 bool need_iova_contig_obj; 458 bool alloc_in_ext_mem; 459 int ret; 460 461 /* check if we can retrieve a valid socket ID */ 462 ret = rte_malloc_heap_socket_is_external(mp->socket_id); 463 if (ret < 0) 464 return -EINVAL; 465 alloc_in_ext_mem = (ret == 1); 466 need_iova_contig_obj = !(mp->flags & RTE_MEMPOOL_F_NO_IOVA_CONTIG); 467 468 if (!need_iova_contig_obj) 469 *pg_sz = 0; 470 else if (rte_eal_has_hugepages() || alloc_in_ext_mem) 471 *pg_sz = get_min_page_size(mp->socket_id); 472 else 473 *pg_sz = rte_mem_page_size(); 474 475 rte_mempool_trace_get_page_size(mp, *pg_sz); 476 return 0; 477 } 478 479 /* Default function to populate the mempool: allocate memory in memzones, 480 * and populate them. Return the number of objects added, or a negative 481 * value on error. 482 */ 483 int 484 rte_mempool_populate_default(struct rte_mempool *mp) 485 { 486 unsigned int mz_flags = RTE_MEMZONE_1GB|RTE_MEMZONE_SIZE_HINT_ONLY; 487 char mz_name[RTE_MEMZONE_NAMESIZE]; 488 const struct rte_memzone *mz; 489 ssize_t mem_size; 490 size_t align, pg_sz, pg_shift = 0; 491 rte_iova_t iova; 492 unsigned mz_id, n; 493 int ret; 494 bool need_iova_contig_obj; 495 size_t max_alloc_size = SIZE_MAX; 496 497 ret = mempool_ops_alloc_once(mp); 498 if (ret != 0) 499 return ret; 500 501 /* mempool must not be populated */ 502 if (mp->nb_mem_chunks != 0) 503 return -EEXIST; 504 505 /* 506 * the following section calculates page shift and page size values. 507 * 508 * these values impact the result of calc_mem_size operation, which 509 * returns the amount of memory that should be allocated to store the 510 * desired number of objects. when not zero, it allocates more memory 511 * for the padding between objects, to ensure that an object does not 512 * cross a page boundary. in other words, page size/shift are to be set 513 * to zero if mempool elements won't care about page boundaries. 514 * there are several considerations for page size and page shift here. 515 * 516 * if we don't need our mempools to have physically contiguous objects, 517 * then just set page shift and page size to 0, because the user has 518 * indicated that there's no need to care about anything. 519 * 520 * if we do need contiguous objects (if a mempool driver has its 521 * own calc_size() method returning min_chunk_size = mem_size), 522 * there is also an option to reserve the entire mempool memory 523 * as one contiguous block of memory. 524 * 525 * if we require contiguous objects, but not necessarily the entire 526 * mempool reserved space to be contiguous, pg_sz will be != 0, 527 * and the default ops->populate() will take care of not placing 528 * objects across pages. 529 * 530 * if our IO addresses are physical, we may get memory from bigger 531 * pages, or we might get memory from smaller pages, and how much of it 532 * we require depends on whether we want bigger or smaller pages. 533 * However, requesting each and every memory size is too much work, so 534 * what we'll do instead is walk through the page sizes available, pick 535 * the smallest one and set up page shift to match that one. We will be 536 * wasting some space this way, but it's much nicer than looping around 537 * trying to reserve each and every page size. 538 * 539 * If we fail to get enough contiguous memory, then we'll go and 540 * reserve space in smaller chunks. 541 */ 542 543 need_iova_contig_obj = !(mp->flags & RTE_MEMPOOL_F_NO_IOVA_CONTIG); 544 ret = rte_mempool_get_page_size(mp, &pg_sz); 545 if (ret < 0) 546 return ret; 547 548 if (pg_sz != 0) 549 pg_shift = rte_bsf32(pg_sz); 550 551 for (mz_id = 0, n = mp->size; n > 0; mz_id++, n -= ret) { 552 size_t min_chunk_size; 553 554 mem_size = rte_mempool_ops_calc_mem_size( 555 mp, n, pg_shift, &min_chunk_size, &align); 556 557 if (mem_size < 0) { 558 ret = mem_size; 559 goto fail; 560 } 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 /* if we're trying to reserve contiguous memory, add appropriate 570 * memzone flag. 571 */ 572 if (min_chunk_size == (size_t)mem_size) 573 mz_flags |= RTE_MEMZONE_IOVA_CONTIG; 574 575 /* Allocate a memzone, retrying with a smaller area on ENOMEM */ 576 do { 577 mz = rte_memzone_reserve_aligned(mz_name, 578 RTE_MIN((size_t)mem_size, max_alloc_size), 579 mp->socket_id, mz_flags, align); 580 581 if (mz != NULL || rte_errno != ENOMEM) 582 break; 583 584 max_alloc_size = RTE_MIN(max_alloc_size, 585 (size_t)mem_size) / 2; 586 } while (mz == NULL && max_alloc_size >= min_chunk_size); 587 588 if (mz == NULL) { 589 ret = -rte_errno; 590 goto fail; 591 } 592 593 if (need_iova_contig_obj) 594 iova = mz->iova; 595 else 596 iova = RTE_BAD_IOVA; 597 598 if (pg_sz == 0 || (mz_flags & RTE_MEMZONE_IOVA_CONTIG)) 599 ret = rte_mempool_populate_iova(mp, mz->addr, 600 iova, mz->len, 601 rte_mempool_memchunk_mz_free, 602 (void *)(uintptr_t)mz); 603 else 604 ret = rte_mempool_populate_virt(mp, mz->addr, 605 mz->len, pg_sz, 606 rte_mempool_memchunk_mz_free, 607 (void *)(uintptr_t)mz); 608 if (ret == 0) /* should not happen */ 609 ret = -ENOBUFS; 610 if (ret < 0) { 611 rte_memzone_free(mz); 612 goto fail; 613 } 614 } 615 616 rte_mempool_trace_populate_default(mp); 617 return mp->size; 618 619 fail: 620 rte_mempool_free_memchunks(mp); 621 return ret; 622 } 623 624 /* return the memory size required for mempool objects in anonymous mem */ 625 static ssize_t 626 get_anon_size(const struct rte_mempool *mp) 627 { 628 ssize_t size; 629 size_t pg_sz, pg_shift; 630 size_t min_chunk_size; 631 size_t align; 632 633 pg_sz = rte_mem_page_size(); 634 pg_shift = rte_bsf32(pg_sz); 635 size = rte_mempool_ops_calc_mem_size(mp, mp->size, pg_shift, 636 &min_chunk_size, &align); 637 638 return size; 639 } 640 641 /* unmap a memory zone mapped by rte_mempool_populate_anon() */ 642 static void 643 rte_mempool_memchunk_anon_free(struct rte_mempool_memhdr *memhdr, 644 void *opaque) 645 { 646 ssize_t size; 647 648 /* 649 * Calculate size since memhdr->len has contiguous chunk length 650 * which may be smaller if anon map is split into many contiguous 651 * chunks. Result must be the same as we calculated on populate. 652 */ 653 size = get_anon_size(memhdr->mp); 654 if (size < 0) 655 return; 656 657 rte_mem_unmap(opaque, size); 658 } 659 660 /* populate the mempool with an anonymous mapping */ 661 int 662 rte_mempool_populate_anon(struct rte_mempool *mp) 663 { 664 ssize_t size; 665 int ret; 666 char *addr; 667 668 /* mempool is already populated, error */ 669 if ((!STAILQ_EMPTY(&mp->mem_list)) || mp->nb_mem_chunks != 0) { 670 rte_errno = EINVAL; 671 return 0; 672 } 673 674 ret = mempool_ops_alloc_once(mp); 675 if (ret < 0) { 676 rte_errno = -ret; 677 return 0; 678 } 679 680 size = get_anon_size(mp); 681 if (size < 0) { 682 rte_errno = -size; 683 return 0; 684 } 685 686 /* get chunk of virtually continuous memory */ 687 addr = rte_mem_map(NULL, size, RTE_PROT_READ | RTE_PROT_WRITE, 688 RTE_MAP_SHARED | RTE_MAP_ANONYMOUS, -1, 0); 689 if (addr == NULL) 690 return 0; 691 /* can't use MMAP_LOCKED, it does not exist on BSD */ 692 if (rte_mem_lock(addr, size) < 0) { 693 rte_mem_unmap(addr, size); 694 return 0; 695 } 696 697 ret = rte_mempool_populate_virt(mp, addr, size, rte_mem_page_size(), 698 rte_mempool_memchunk_anon_free, addr); 699 if (ret == 0) /* should not happen */ 700 ret = -ENOBUFS; 701 if (ret < 0) { 702 rte_errno = -ret; 703 goto fail; 704 } 705 706 rte_mempool_trace_populate_anon(mp); 707 return mp->populated_size; 708 709 fail: 710 rte_mempool_free_memchunks(mp); 711 return 0; 712 } 713 714 /* free a mempool */ 715 void 716 rte_mempool_free(struct rte_mempool *mp) 717 { 718 struct rte_mempool_list *mempool_list = NULL; 719 struct rte_tailq_entry *te; 720 721 if (mp == NULL) 722 return; 723 724 mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list); 725 rte_mcfg_tailq_write_lock(); 726 /* find out tailq entry */ 727 TAILQ_FOREACH(te, mempool_list, next) { 728 if (te->data == (void *)mp) 729 break; 730 } 731 732 if (te != NULL) { 733 TAILQ_REMOVE(mempool_list, te, next); 734 rte_free(te); 735 } 736 rte_mcfg_tailq_write_unlock(); 737 738 mempool_event_callback_invoke(RTE_MEMPOOL_EVENT_DESTROY, mp); 739 rte_mempool_trace_free(mp); 740 rte_mempool_free_memchunks(mp); 741 rte_mempool_ops_free(mp); 742 rte_memzone_free(mp->mz); 743 } 744 745 static void 746 mempool_cache_init(struct rte_mempool_cache *cache, uint32_t size) 747 { 748 /* Check that cache have enough space for flush threshold */ 749 RTE_BUILD_BUG_ON(CALC_CACHE_FLUSHTHRESH(RTE_MEMPOOL_CACHE_MAX_SIZE) > 750 RTE_SIZEOF_FIELD(struct rte_mempool_cache, objs) / 751 RTE_SIZEOF_FIELD(struct rte_mempool_cache, objs[0])); 752 753 cache->size = size; 754 cache->flushthresh = CALC_CACHE_FLUSHTHRESH(size); 755 cache->len = 0; 756 } 757 758 /* 759 * Create and initialize a cache for objects that are retrieved from and 760 * returned to an underlying mempool. This structure is identical to the 761 * local_cache[lcore_id] pointed to by the mempool structure. 762 */ 763 struct rte_mempool_cache * 764 rte_mempool_cache_create(uint32_t size, int socket_id) 765 { 766 struct rte_mempool_cache *cache; 767 768 if (size == 0 || size > RTE_MEMPOOL_CACHE_MAX_SIZE) { 769 rte_errno = EINVAL; 770 return NULL; 771 } 772 773 cache = rte_zmalloc_socket("MEMPOOL_CACHE", sizeof(*cache), 774 RTE_CACHE_LINE_SIZE, socket_id); 775 if (cache == NULL) { 776 RTE_LOG(ERR, MEMPOOL, "Cannot allocate mempool cache.\n"); 777 rte_errno = ENOMEM; 778 return NULL; 779 } 780 781 mempool_cache_init(cache, size); 782 783 rte_mempool_trace_cache_create(size, socket_id, cache); 784 return cache; 785 } 786 787 /* 788 * Free a cache. It's the responsibility of the user to make sure that any 789 * remaining objects in the cache are flushed to the corresponding 790 * mempool. 791 */ 792 void 793 rte_mempool_cache_free(struct rte_mempool_cache *cache) 794 { 795 rte_mempool_trace_cache_free(cache); 796 rte_free(cache); 797 } 798 799 /* create an empty mempool */ 800 struct rte_mempool * 801 rte_mempool_create_empty(const char *name, unsigned n, unsigned elt_size, 802 unsigned cache_size, unsigned private_data_size, 803 int socket_id, unsigned flags) 804 { 805 char mz_name[RTE_MEMZONE_NAMESIZE]; 806 struct rte_mempool_list *mempool_list; 807 struct rte_mempool *mp = NULL; 808 struct rte_tailq_entry *te = NULL; 809 const struct rte_memzone *mz = NULL; 810 size_t mempool_size; 811 unsigned int mz_flags = RTE_MEMZONE_1GB|RTE_MEMZONE_SIZE_HINT_ONLY; 812 struct rte_mempool_objsz objsz; 813 unsigned lcore_id; 814 int ret; 815 816 /* compilation-time checks */ 817 RTE_BUILD_BUG_ON((sizeof(struct rte_mempool) & 818 RTE_CACHE_LINE_MASK) != 0); 819 RTE_BUILD_BUG_ON((sizeof(struct rte_mempool_cache) & 820 RTE_CACHE_LINE_MASK) != 0); 821 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG 822 RTE_BUILD_BUG_ON((sizeof(struct rte_mempool_debug_stats) & 823 RTE_CACHE_LINE_MASK) != 0); 824 RTE_BUILD_BUG_ON((offsetof(struct rte_mempool, stats) & 825 RTE_CACHE_LINE_MASK) != 0); 826 #endif 827 828 mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list); 829 830 /* asked for zero items */ 831 if (n == 0) { 832 rte_errno = EINVAL; 833 return NULL; 834 } 835 836 /* asked cache too big */ 837 if (cache_size > RTE_MEMPOOL_CACHE_MAX_SIZE || 838 CALC_CACHE_FLUSHTHRESH(cache_size) > n) { 839 rte_errno = EINVAL; 840 return NULL; 841 } 842 843 /* enforce only user flags are passed by the application */ 844 if ((flags & ~RTE_MEMPOOL_VALID_USER_FLAGS) != 0) { 845 rte_errno = EINVAL; 846 return NULL; 847 } 848 849 /* 850 * No objects in the pool can be used for IO until it's populated 851 * with at least some objects with valid IOVA. 852 */ 853 flags |= RTE_MEMPOOL_F_NON_IO; 854 855 /* "no cache align" imply "no spread" */ 856 if (flags & RTE_MEMPOOL_F_NO_CACHE_ALIGN) 857 flags |= RTE_MEMPOOL_F_NO_SPREAD; 858 859 /* calculate mempool object sizes. */ 860 if (!rte_mempool_calc_obj_size(elt_size, flags, &objsz)) { 861 rte_errno = EINVAL; 862 return NULL; 863 } 864 865 rte_mcfg_mempool_write_lock(); 866 867 /* 868 * reserve a memory zone for this mempool: private data is 869 * cache-aligned 870 */ 871 private_data_size = (private_data_size + 872 RTE_MEMPOOL_ALIGN_MASK) & (~RTE_MEMPOOL_ALIGN_MASK); 873 874 875 /* try to allocate tailq entry */ 876 te = rte_zmalloc("MEMPOOL_TAILQ_ENTRY", sizeof(*te), 0); 877 if (te == NULL) { 878 RTE_LOG(ERR, MEMPOOL, "Cannot allocate tailq entry!\n"); 879 goto exit_unlock; 880 } 881 882 mempool_size = RTE_MEMPOOL_HEADER_SIZE(mp, cache_size); 883 mempool_size += private_data_size; 884 mempool_size = RTE_ALIGN_CEIL(mempool_size, RTE_MEMPOOL_ALIGN); 885 886 ret = snprintf(mz_name, sizeof(mz_name), RTE_MEMPOOL_MZ_FORMAT, name); 887 if (ret < 0 || ret >= (int)sizeof(mz_name)) { 888 rte_errno = ENAMETOOLONG; 889 goto exit_unlock; 890 } 891 892 mz = rte_memzone_reserve(mz_name, mempool_size, socket_id, mz_flags); 893 if (mz == NULL) 894 goto exit_unlock; 895 896 /* init the mempool structure */ 897 mp = mz->addr; 898 memset(mp, 0, RTE_MEMPOOL_HEADER_SIZE(mp, cache_size)); 899 ret = strlcpy(mp->name, name, sizeof(mp->name)); 900 if (ret < 0 || ret >= (int)sizeof(mp->name)) { 901 rte_errno = ENAMETOOLONG; 902 goto exit_unlock; 903 } 904 mp->mz = mz; 905 mp->size = n; 906 mp->flags = flags; 907 mp->socket_id = socket_id; 908 mp->elt_size = objsz.elt_size; 909 mp->header_size = objsz.header_size; 910 mp->trailer_size = objsz.trailer_size; 911 /* Size of default caches, zero means disabled. */ 912 mp->cache_size = cache_size; 913 mp->private_data_size = private_data_size; 914 STAILQ_INIT(&mp->elt_list); 915 STAILQ_INIT(&mp->mem_list); 916 917 /* 918 * local_cache pointer is set even if cache_size is zero. 919 * The local_cache points to just past the elt_pa[] array. 920 */ 921 mp->local_cache = (struct rte_mempool_cache *) 922 RTE_PTR_ADD(mp, RTE_MEMPOOL_HEADER_SIZE(mp, 0)); 923 924 /* Init all default caches. */ 925 if (cache_size != 0) { 926 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) 927 mempool_cache_init(&mp->local_cache[lcore_id], 928 cache_size); 929 } 930 931 te->data = mp; 932 933 rte_mcfg_tailq_write_lock(); 934 TAILQ_INSERT_TAIL(mempool_list, te, next); 935 rte_mcfg_tailq_write_unlock(); 936 rte_mcfg_mempool_write_unlock(); 937 938 rte_mempool_trace_create_empty(name, n, elt_size, cache_size, 939 private_data_size, flags, mp); 940 return mp; 941 942 exit_unlock: 943 rte_mcfg_mempool_write_unlock(); 944 rte_free(te); 945 rte_mempool_free(mp); 946 return NULL; 947 } 948 949 /* create the mempool */ 950 struct rte_mempool * 951 rte_mempool_create(const char *name, unsigned n, unsigned elt_size, 952 unsigned cache_size, unsigned private_data_size, 953 rte_mempool_ctor_t *mp_init, void *mp_init_arg, 954 rte_mempool_obj_cb_t *obj_init, void *obj_init_arg, 955 int socket_id, unsigned flags) 956 { 957 int ret; 958 struct rte_mempool *mp; 959 960 mp = rte_mempool_create_empty(name, n, elt_size, cache_size, 961 private_data_size, socket_id, flags); 962 if (mp == NULL) 963 return NULL; 964 965 /* 966 * Since we have 4 combinations of the SP/SC/MP/MC examine the flags to 967 * set the correct index into the table of ops structs. 968 */ 969 if ((flags & RTE_MEMPOOL_F_SP_PUT) && (flags & RTE_MEMPOOL_F_SC_GET)) 970 ret = rte_mempool_set_ops_byname(mp, "ring_sp_sc", NULL); 971 else if (flags & RTE_MEMPOOL_F_SP_PUT) 972 ret = rte_mempool_set_ops_byname(mp, "ring_sp_mc", NULL); 973 else if (flags & RTE_MEMPOOL_F_SC_GET) 974 ret = rte_mempool_set_ops_byname(mp, "ring_mp_sc", NULL); 975 else 976 ret = rte_mempool_set_ops_byname(mp, "ring_mp_mc", NULL); 977 978 if (ret) 979 goto fail; 980 981 /* call the mempool priv initializer */ 982 if (mp_init) 983 mp_init(mp, mp_init_arg); 984 985 if (rte_mempool_populate_default(mp) < 0) 986 goto fail; 987 988 /* call the object initializers */ 989 if (obj_init) 990 rte_mempool_obj_iter(mp, obj_init, obj_init_arg); 991 992 rte_mempool_trace_create(name, n, elt_size, cache_size, 993 private_data_size, mp_init, mp_init_arg, obj_init, 994 obj_init_arg, flags, mp); 995 return mp; 996 997 fail: 998 rte_mempool_free(mp); 999 return NULL; 1000 } 1001 1002 /* Return the number of entries in the mempool */ 1003 unsigned int 1004 rte_mempool_avail_count(const struct rte_mempool *mp) 1005 { 1006 unsigned count; 1007 unsigned lcore_id; 1008 1009 count = rte_mempool_ops_get_count(mp); 1010 1011 if (mp->cache_size == 0) 1012 return count; 1013 1014 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) 1015 count += mp->local_cache[lcore_id].len; 1016 1017 /* 1018 * due to race condition (access to len is not locked), the 1019 * total can be greater than size... so fix the result 1020 */ 1021 if (count > mp->size) 1022 return mp->size; 1023 return count; 1024 } 1025 1026 /* return the number of entries allocated from the mempool */ 1027 unsigned int 1028 rte_mempool_in_use_count(const struct rte_mempool *mp) 1029 { 1030 return mp->size - rte_mempool_avail_count(mp); 1031 } 1032 1033 /* dump the cache status */ 1034 static unsigned 1035 rte_mempool_dump_cache(FILE *f, const struct rte_mempool *mp) 1036 { 1037 unsigned lcore_id; 1038 unsigned count = 0; 1039 unsigned cache_count; 1040 1041 fprintf(f, " internal cache infos:\n"); 1042 fprintf(f, " cache_size=%"PRIu32"\n", mp->cache_size); 1043 1044 if (mp->cache_size == 0) 1045 return count; 1046 1047 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) { 1048 cache_count = mp->local_cache[lcore_id].len; 1049 fprintf(f, " cache_count[%u]=%"PRIu32"\n", 1050 lcore_id, cache_count); 1051 count += cache_count; 1052 } 1053 fprintf(f, " total_cache_count=%u\n", count); 1054 return count; 1055 } 1056 1057 #ifndef __INTEL_COMPILER 1058 #pragma GCC diagnostic ignored "-Wcast-qual" 1059 #endif 1060 1061 /* check and update cookies or panic (internal) */ 1062 void rte_mempool_check_cookies(const struct rte_mempool *mp, 1063 void * const *obj_table_const, unsigned n, int free) 1064 { 1065 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG 1066 struct rte_mempool_objhdr *hdr; 1067 struct rte_mempool_objtlr *tlr; 1068 uint64_t cookie; 1069 void *tmp; 1070 void *obj; 1071 void **obj_table; 1072 1073 /* Force to drop the "const" attribute. This is done only when 1074 * DEBUG is enabled */ 1075 tmp = (void *) obj_table_const; 1076 obj_table = tmp; 1077 1078 while (n--) { 1079 obj = obj_table[n]; 1080 1081 if (rte_mempool_from_obj(obj) != mp) 1082 rte_panic("MEMPOOL: object is owned by another " 1083 "mempool\n"); 1084 1085 hdr = rte_mempool_get_header(obj); 1086 cookie = hdr->cookie; 1087 1088 if (free == 0) { 1089 if (cookie != RTE_MEMPOOL_HEADER_COOKIE1) { 1090 RTE_LOG(CRIT, MEMPOOL, 1091 "obj=%p, mempool=%p, cookie=%" PRIx64 "\n", 1092 obj, (const void *) mp, cookie); 1093 rte_panic("MEMPOOL: bad header cookie (put)\n"); 1094 } 1095 hdr->cookie = RTE_MEMPOOL_HEADER_COOKIE2; 1096 } else if (free == 1) { 1097 if (cookie != RTE_MEMPOOL_HEADER_COOKIE2) { 1098 RTE_LOG(CRIT, MEMPOOL, 1099 "obj=%p, mempool=%p, cookie=%" PRIx64 "\n", 1100 obj, (const void *) mp, cookie); 1101 rte_panic("MEMPOOL: bad header cookie (get)\n"); 1102 } 1103 hdr->cookie = RTE_MEMPOOL_HEADER_COOKIE1; 1104 } else if (free == 2) { 1105 if (cookie != RTE_MEMPOOL_HEADER_COOKIE1 && 1106 cookie != RTE_MEMPOOL_HEADER_COOKIE2) { 1107 RTE_LOG(CRIT, MEMPOOL, 1108 "obj=%p, mempool=%p, cookie=%" PRIx64 "\n", 1109 obj, (const void *) mp, cookie); 1110 rte_panic("MEMPOOL: bad header cookie (audit)\n"); 1111 } 1112 } 1113 tlr = rte_mempool_get_trailer(obj); 1114 cookie = tlr->cookie; 1115 if (cookie != RTE_MEMPOOL_TRAILER_COOKIE) { 1116 RTE_LOG(CRIT, MEMPOOL, 1117 "obj=%p, mempool=%p, cookie=%" PRIx64 "\n", 1118 obj, (const void *) mp, cookie); 1119 rte_panic("MEMPOOL: bad trailer cookie\n"); 1120 } 1121 } 1122 #else 1123 RTE_SET_USED(mp); 1124 RTE_SET_USED(obj_table_const); 1125 RTE_SET_USED(n); 1126 RTE_SET_USED(free); 1127 #endif 1128 } 1129 1130 void 1131 rte_mempool_contig_blocks_check_cookies(const struct rte_mempool *mp, 1132 void * const *first_obj_table_const, unsigned int n, int free) 1133 { 1134 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG 1135 struct rte_mempool_info info; 1136 const size_t total_elt_sz = 1137 mp->header_size + mp->elt_size + mp->trailer_size; 1138 unsigned int i, j; 1139 1140 rte_mempool_ops_get_info(mp, &info); 1141 1142 for (i = 0; i < n; ++i) { 1143 void *first_obj = first_obj_table_const[i]; 1144 1145 for (j = 0; j < info.contig_block_size; ++j) { 1146 void *obj; 1147 1148 obj = (void *)((uintptr_t)first_obj + j * total_elt_sz); 1149 rte_mempool_check_cookies(mp, &obj, 1, free); 1150 } 1151 } 1152 #else 1153 RTE_SET_USED(mp); 1154 RTE_SET_USED(first_obj_table_const); 1155 RTE_SET_USED(n); 1156 RTE_SET_USED(free); 1157 #endif 1158 } 1159 1160 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG 1161 static void 1162 mempool_obj_audit(struct rte_mempool *mp, __rte_unused void *opaque, 1163 void *obj, __rte_unused unsigned idx) 1164 { 1165 RTE_MEMPOOL_CHECK_COOKIES(mp, &obj, 1, 2); 1166 } 1167 1168 static void 1169 mempool_audit_cookies(struct rte_mempool *mp) 1170 { 1171 unsigned num; 1172 1173 num = rte_mempool_obj_iter(mp, mempool_obj_audit, NULL); 1174 if (num != mp->size) { 1175 rte_panic("rte_mempool_obj_iter(mempool=%p, size=%u) " 1176 "iterated only over %u elements\n", 1177 mp, mp->size, num); 1178 } 1179 } 1180 #else 1181 #define mempool_audit_cookies(mp) do {} while(0) 1182 #endif 1183 1184 #ifndef __INTEL_COMPILER 1185 #pragma GCC diagnostic error "-Wcast-qual" 1186 #endif 1187 1188 /* check cookies before and after objects */ 1189 static void 1190 mempool_audit_cache(const struct rte_mempool *mp) 1191 { 1192 /* check cache size consistency */ 1193 unsigned lcore_id; 1194 1195 if (mp->cache_size == 0) 1196 return; 1197 1198 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) { 1199 const struct rte_mempool_cache *cache; 1200 cache = &mp->local_cache[lcore_id]; 1201 if (cache->len > RTE_DIM(cache->objs)) { 1202 RTE_LOG(CRIT, MEMPOOL, "badness on cache[%u]\n", 1203 lcore_id); 1204 rte_panic("MEMPOOL: invalid cache len\n"); 1205 } 1206 } 1207 } 1208 1209 /* check the consistency of mempool (size, cookies, ...) */ 1210 void 1211 rte_mempool_audit(struct rte_mempool *mp) 1212 { 1213 mempool_audit_cache(mp); 1214 mempool_audit_cookies(mp); 1215 1216 /* For case where mempool DEBUG is not set, and cache size is 0 */ 1217 RTE_SET_USED(mp); 1218 } 1219 1220 /* dump the status of the mempool on the console */ 1221 void 1222 rte_mempool_dump(FILE *f, struct rte_mempool *mp) 1223 { 1224 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG 1225 struct rte_mempool_info info; 1226 struct rte_mempool_debug_stats sum; 1227 unsigned lcore_id; 1228 #endif 1229 struct rte_mempool_memhdr *memhdr; 1230 struct rte_mempool_ops *ops; 1231 unsigned common_count; 1232 unsigned cache_count; 1233 size_t mem_len = 0; 1234 1235 RTE_ASSERT(f != NULL); 1236 RTE_ASSERT(mp != NULL); 1237 1238 fprintf(f, "mempool <%s>@%p\n", mp->name, mp); 1239 fprintf(f, " flags=%x\n", mp->flags); 1240 fprintf(f, " socket_id=%d\n", mp->socket_id); 1241 fprintf(f, " pool=%p\n", mp->pool_data); 1242 fprintf(f, " iova=0x%" PRIx64 "\n", mp->mz->iova); 1243 fprintf(f, " nb_mem_chunks=%u\n", mp->nb_mem_chunks); 1244 fprintf(f, " size=%"PRIu32"\n", mp->size); 1245 fprintf(f, " populated_size=%"PRIu32"\n", mp->populated_size); 1246 fprintf(f, " header_size=%"PRIu32"\n", mp->header_size); 1247 fprintf(f, " elt_size=%"PRIu32"\n", mp->elt_size); 1248 fprintf(f, " trailer_size=%"PRIu32"\n", mp->trailer_size); 1249 fprintf(f, " total_obj_size=%"PRIu32"\n", 1250 mp->header_size + mp->elt_size + mp->trailer_size); 1251 1252 fprintf(f, " private_data_size=%"PRIu32"\n", mp->private_data_size); 1253 1254 fprintf(f, " ops_index=%d\n", mp->ops_index); 1255 ops = rte_mempool_get_ops(mp->ops_index); 1256 fprintf(f, " ops_name: <%s>\n", (ops != NULL) ? ops->name : "NA"); 1257 1258 STAILQ_FOREACH(memhdr, &mp->mem_list, next) 1259 mem_len += memhdr->len; 1260 if (mem_len != 0) { 1261 fprintf(f, " avg bytes/object=%#Lf\n", 1262 (long double)mem_len / mp->size); 1263 } 1264 1265 cache_count = rte_mempool_dump_cache(f, mp); 1266 common_count = rte_mempool_ops_get_count(mp); 1267 if ((cache_count + common_count) > mp->size) 1268 common_count = mp->size - cache_count; 1269 fprintf(f, " common_pool_count=%u\n", common_count); 1270 1271 /* sum and dump statistics */ 1272 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG 1273 rte_mempool_ops_get_info(mp, &info); 1274 memset(&sum, 0, sizeof(sum)); 1275 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) { 1276 sum.put_bulk += mp->stats[lcore_id].put_bulk; 1277 sum.put_objs += mp->stats[lcore_id].put_objs; 1278 sum.put_common_pool_bulk += mp->stats[lcore_id].put_common_pool_bulk; 1279 sum.put_common_pool_objs += mp->stats[lcore_id].put_common_pool_objs; 1280 sum.get_common_pool_bulk += mp->stats[lcore_id].get_common_pool_bulk; 1281 sum.get_common_pool_objs += mp->stats[lcore_id].get_common_pool_objs; 1282 sum.get_success_bulk += mp->stats[lcore_id].get_success_bulk; 1283 sum.get_success_objs += mp->stats[lcore_id].get_success_objs; 1284 sum.get_fail_bulk += mp->stats[lcore_id].get_fail_bulk; 1285 sum.get_fail_objs += mp->stats[lcore_id].get_fail_objs; 1286 sum.get_success_blks += mp->stats[lcore_id].get_success_blks; 1287 sum.get_fail_blks += mp->stats[lcore_id].get_fail_blks; 1288 } 1289 fprintf(f, " stats:\n"); 1290 fprintf(f, " put_bulk=%"PRIu64"\n", sum.put_bulk); 1291 fprintf(f, " put_objs=%"PRIu64"\n", sum.put_objs); 1292 fprintf(f, " put_common_pool_bulk=%"PRIu64"\n", sum.put_common_pool_bulk); 1293 fprintf(f, " put_common_pool_objs=%"PRIu64"\n", sum.put_common_pool_objs); 1294 fprintf(f, " get_common_pool_bulk=%"PRIu64"\n", sum.get_common_pool_bulk); 1295 fprintf(f, " get_common_pool_objs=%"PRIu64"\n", sum.get_common_pool_objs); 1296 fprintf(f, " get_success_bulk=%"PRIu64"\n", sum.get_success_bulk); 1297 fprintf(f, " get_success_objs=%"PRIu64"\n", sum.get_success_objs); 1298 fprintf(f, " get_fail_bulk=%"PRIu64"\n", sum.get_fail_bulk); 1299 fprintf(f, " get_fail_objs=%"PRIu64"\n", sum.get_fail_objs); 1300 if (info.contig_block_size > 0) { 1301 fprintf(f, " get_success_blks=%"PRIu64"\n", 1302 sum.get_success_blks); 1303 fprintf(f, " get_fail_blks=%"PRIu64"\n", sum.get_fail_blks); 1304 } 1305 #else 1306 fprintf(f, " no statistics available\n"); 1307 #endif 1308 1309 rte_mempool_audit(mp); 1310 } 1311 1312 /* dump the status of all mempools on the console */ 1313 void 1314 rte_mempool_list_dump(FILE *f) 1315 { 1316 struct rte_mempool *mp = NULL; 1317 struct rte_tailq_entry *te; 1318 struct rte_mempool_list *mempool_list; 1319 1320 mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list); 1321 1322 rte_mcfg_mempool_read_lock(); 1323 1324 TAILQ_FOREACH(te, mempool_list, next) { 1325 mp = (struct rte_mempool *) te->data; 1326 rte_mempool_dump(f, mp); 1327 } 1328 1329 rte_mcfg_mempool_read_unlock(); 1330 } 1331 1332 /* search a mempool from its name */ 1333 struct rte_mempool * 1334 rte_mempool_lookup(const char *name) 1335 { 1336 struct rte_mempool *mp = NULL; 1337 struct rte_tailq_entry *te; 1338 struct rte_mempool_list *mempool_list; 1339 1340 mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list); 1341 1342 rte_mcfg_mempool_read_lock(); 1343 1344 TAILQ_FOREACH(te, mempool_list, next) { 1345 mp = (struct rte_mempool *) te->data; 1346 if (strncmp(name, mp->name, RTE_MEMPOOL_NAMESIZE) == 0) 1347 break; 1348 } 1349 1350 rte_mcfg_mempool_read_unlock(); 1351 1352 if (te == NULL) { 1353 rte_errno = ENOENT; 1354 return NULL; 1355 } 1356 1357 return mp; 1358 } 1359 1360 void rte_mempool_walk(void (*func)(struct rte_mempool *, void *), 1361 void *arg) 1362 { 1363 struct rte_tailq_entry *te = NULL; 1364 struct rte_mempool_list *mempool_list; 1365 void *tmp_te; 1366 1367 mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list); 1368 1369 rte_mcfg_mempool_read_lock(); 1370 1371 RTE_TAILQ_FOREACH_SAFE(te, mempool_list, next, tmp_te) { 1372 (*func)((struct rte_mempool *) te->data, arg); 1373 } 1374 1375 rte_mcfg_mempool_read_unlock(); 1376 } 1377 1378 struct mempool_callback_data { 1379 TAILQ_ENTRY(mempool_callback_data) callbacks; 1380 rte_mempool_event_callback *func; 1381 void *user_data; 1382 }; 1383 1384 static void 1385 mempool_event_callback_invoke(enum rte_mempool_event event, 1386 struct rte_mempool *mp) 1387 { 1388 struct mempool_callback_data *cb; 1389 void *tmp_te; 1390 1391 rte_mcfg_tailq_read_lock(); 1392 RTE_TAILQ_FOREACH_SAFE(cb, &callback_tailq, callbacks, tmp_te) { 1393 rte_mcfg_tailq_read_unlock(); 1394 cb->func(event, mp, cb->user_data); 1395 rte_mcfg_tailq_read_lock(); 1396 } 1397 rte_mcfg_tailq_read_unlock(); 1398 } 1399 1400 int 1401 rte_mempool_event_callback_register(rte_mempool_event_callback *func, 1402 void *user_data) 1403 { 1404 struct mempool_callback_data *cb; 1405 int ret; 1406 1407 if (func == NULL) { 1408 rte_errno = EINVAL; 1409 return -rte_errno; 1410 } 1411 1412 rte_mcfg_tailq_write_lock(); 1413 TAILQ_FOREACH(cb, &callback_tailq, callbacks) { 1414 if (cb->func == func && cb->user_data == user_data) { 1415 ret = -EEXIST; 1416 goto exit; 1417 } 1418 } 1419 1420 cb = calloc(1, sizeof(*cb)); 1421 if (cb == NULL) { 1422 RTE_LOG(ERR, MEMPOOL, "Cannot allocate event callback!\n"); 1423 ret = -ENOMEM; 1424 goto exit; 1425 } 1426 1427 cb->func = func; 1428 cb->user_data = user_data; 1429 TAILQ_INSERT_TAIL(&callback_tailq, cb, callbacks); 1430 ret = 0; 1431 1432 exit: 1433 rte_mcfg_tailq_write_unlock(); 1434 rte_errno = -ret; 1435 return ret; 1436 } 1437 1438 int 1439 rte_mempool_event_callback_unregister(rte_mempool_event_callback *func, 1440 void *user_data) 1441 { 1442 struct mempool_callback_data *cb; 1443 int ret = -ENOENT; 1444 1445 rte_mcfg_tailq_write_lock(); 1446 TAILQ_FOREACH(cb, &callback_tailq, callbacks) { 1447 if (cb->func == func && cb->user_data == user_data) { 1448 TAILQ_REMOVE(&callback_tailq, cb, callbacks); 1449 ret = 0; 1450 break; 1451 } 1452 } 1453 rte_mcfg_tailq_write_unlock(); 1454 1455 if (ret == 0) 1456 free(cb); 1457 rte_errno = -ret; 1458 return ret; 1459 } 1460 1461 static void 1462 mempool_list_cb(struct rte_mempool *mp, void *arg) 1463 { 1464 struct rte_tel_data *d = (struct rte_tel_data *)arg; 1465 1466 rte_tel_data_add_array_string(d, mp->name); 1467 } 1468 1469 static int 1470 mempool_handle_list(const char *cmd __rte_unused, 1471 const char *params __rte_unused, struct rte_tel_data *d) 1472 { 1473 rte_tel_data_start_array(d, RTE_TEL_STRING_VAL); 1474 rte_mempool_walk(mempool_list_cb, d); 1475 return 0; 1476 } 1477 1478 struct mempool_info_cb_arg { 1479 char *pool_name; 1480 struct rte_tel_data *d; 1481 }; 1482 1483 static void 1484 mempool_info_cb(struct rte_mempool *mp, void *arg) 1485 { 1486 struct mempool_info_cb_arg *info = (struct mempool_info_cb_arg *)arg; 1487 const struct rte_memzone *mz; 1488 1489 if (strncmp(mp->name, info->pool_name, RTE_MEMZONE_NAMESIZE)) 1490 return; 1491 1492 rte_tel_data_add_dict_string(info->d, "name", mp->name); 1493 rte_tel_data_add_dict_int(info->d, "pool_id", mp->pool_id); 1494 rte_tel_data_add_dict_int(info->d, "flags", mp->flags); 1495 rte_tel_data_add_dict_int(info->d, "socket_id", mp->socket_id); 1496 rte_tel_data_add_dict_int(info->d, "size", mp->size); 1497 rte_tel_data_add_dict_int(info->d, "cache_size", mp->cache_size); 1498 rte_tel_data_add_dict_int(info->d, "elt_size", mp->elt_size); 1499 rte_tel_data_add_dict_int(info->d, "header_size", mp->header_size); 1500 rte_tel_data_add_dict_int(info->d, "trailer_size", mp->trailer_size); 1501 rte_tel_data_add_dict_int(info->d, "private_data_size", 1502 mp->private_data_size); 1503 rte_tel_data_add_dict_int(info->d, "ops_index", mp->ops_index); 1504 rte_tel_data_add_dict_int(info->d, "populated_size", 1505 mp->populated_size); 1506 1507 mz = mp->mz; 1508 rte_tel_data_add_dict_string(info->d, "mz_name", mz->name); 1509 rte_tel_data_add_dict_int(info->d, "mz_len", mz->len); 1510 rte_tel_data_add_dict_int(info->d, "mz_hugepage_sz", 1511 mz->hugepage_sz); 1512 rte_tel_data_add_dict_int(info->d, "mz_socket_id", mz->socket_id); 1513 rte_tel_data_add_dict_int(info->d, "mz_flags", mz->flags); 1514 } 1515 1516 static int 1517 mempool_handle_info(const char *cmd __rte_unused, const char *params, 1518 struct rte_tel_data *d) 1519 { 1520 struct mempool_info_cb_arg mp_arg; 1521 char name[RTE_MEMZONE_NAMESIZE]; 1522 1523 if (!params || strlen(params) == 0) 1524 return -EINVAL; 1525 1526 rte_strlcpy(name, params, RTE_MEMZONE_NAMESIZE); 1527 1528 rte_tel_data_start_dict(d); 1529 mp_arg.pool_name = name; 1530 mp_arg.d = d; 1531 rte_mempool_walk(mempool_info_cb, &mp_arg); 1532 1533 return 0; 1534 } 1535 1536 RTE_INIT(mempool_init_telemetry) 1537 { 1538 rte_telemetry_register_cmd("/mempool/list", mempool_handle_list, 1539 "Returns list of available mempool. Takes no parameters"); 1540 rte_telemetry_register_cmd("/mempool/info", mempool_handle_info, 1541 "Returns mempool info. Parameters: pool_name"); 1542 } 1543