1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165
|
/* SPDX-License-Identifier: BSD-3-Clause
* Copyright(c) 2010-2014 Intel Corporation
*/
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <inttypes.h>
#include <sys/queue.h>
#include <rte_random.h>
#include <rte_cycles.h>
#include <rte_memory.h>
#include <rte_memzone.h>
#include <rte_eal.h>
#include <rte_lcore.h>
#include <rte_common.h>
#include <rte_string_fns.h>
#include <rte_errno.h>
#include <rte_malloc.h>
#include "malloc_elem.h"
#include "test.h"
/*
* Memzone
* =======
*
* - Search for three reserved zones or reserve them if they do not exist:
*
* - One is on any socket id.
* - The second is on socket 0.
* - The last one is on socket 1 (if socket 1 exists).
*
* - Check that the zones exist.
*
* - Check that the zones are cache-aligned.
*
* - Check that zones do not overlap.
*
* - Check that the zones are on the correct socket id.
*
* - Check that a lookup of the first zone returns the same pointer.
*
* - Check that it is not possible to create another zone with the
* same name as an existing zone.
*
* - Check flags for specific huge page size reservation
*/
#define TEST_MEMZONE_NAME(suffix) "MZ_TEST_" suffix
/* Test if memory overlaps: return 1 if true, or 0 if false. */
static int
is_memory_overlap(rte_iova_t ptr1, size_t len1, rte_iova_t ptr2, size_t len2)
{
if (ptr2 >= ptr1 && (ptr2 - ptr1) < len1)
return 1;
else if (ptr2 < ptr1 && (ptr1 - ptr2) < len2)
return 1;
return 0;
}
static int
test_memzone_invalid_alignment(void)
{
const struct rte_memzone * mz;
mz = rte_memzone_lookup(TEST_MEMZONE_NAME("invalid_alignment"));
if (mz != NULL) {
printf("Zone with invalid alignment has been reserved\n");
return -1;
}
mz = rte_memzone_reserve_aligned(TEST_MEMZONE_NAME("invalid_alignment"),
100, SOCKET_ID_ANY, 0, 100);
if (mz != NULL) {
printf("Zone with invalid alignment has been reserved\n");
return -1;
}
return 0;
}
static int
test_memzone_invalid_flags(void)
{
const struct rte_memzone *mz;
mz = rte_memzone_lookup(TEST_MEMZONE_NAME("invalid_flags"));
if (mz != NULL) {
printf("Zone with invalid flags has been reserved\n");
return -1;
}
mz = rte_memzone_reserve(TEST_MEMZONE_NAME("invalid_flags"),
100, SOCKET_ID_ANY, RTE_MEMZONE_IOVA_CONTIG << 1);
if (mz != NULL) {
printf("Zone with invalid flags has been reserved\n");
return -1;
}
return 0;
}
static int
test_memzone_reserving_zone_size_bigger_than_the_maximum(void)
{
const struct rte_memzone * mz;
mz = rte_memzone_lookup(
TEST_MEMZONE_NAME("zone_size_bigger_than_the_maximum"));
if (mz != NULL) {
printf("zone_size_bigger_than_the_maximum has been reserved\n");
return -1;
}
mz = rte_memzone_reserve(
TEST_MEMZONE_NAME("zone_size_bigger_than_the_maximum"),
(size_t)-1, SOCKET_ID_ANY, 0);
if (mz != NULL) {
printf("It is impossible to reserve such big a memzone\n");
return -1;
}
return 0;
}
struct walk_arg {
int hugepage_2MB_avail;
int hugepage_1GB_avail;
int hugepage_16MB_avail;
int hugepage_16GB_avail;
};
static int
find_available_pagesz(const struct rte_memseg_list *msl, void *arg)
{
struct walk_arg *wa = arg;
if (msl->external)
return 0;
if (msl->page_sz == RTE_PGSIZE_2M)
wa->hugepage_2MB_avail = 1;
if (msl->page_sz == RTE_PGSIZE_1G)
wa->hugepage_1GB_avail = 1;
if (msl->page_sz == RTE_PGSIZE_16M)
wa->hugepage_16MB_avail = 1;
if (msl->page_sz == RTE_PGSIZE_16G)
wa->hugepage_16GB_avail = 1;
return 0;
}
static int
test_memzone_reserve_flags(void)
{
const struct rte_memzone *mz;
struct walk_arg wa;
int hugepage_2MB_avail, hugepage_1GB_avail;
int hugepage_16MB_avail, hugepage_16GB_avail;
const size_t size = 100;
memset(&wa, 0, sizeof(wa));
rte_memseg_list_walk(find_available_pagesz, &wa);
hugepage_2MB_avail = wa.hugepage_2MB_avail;
hugepage_1GB_avail = wa.hugepage_1GB_avail;
hugepage_16MB_avail = wa.hugepage_16MB_avail;
hugepage_16GB_avail = wa.hugepage_16GB_avail;
/* Display the availability of 2MB ,1GB, 16MB, 16GB pages */
if (hugepage_2MB_avail)
printf("2MB Huge pages available\n");
if (hugepage_1GB_avail)
printf("1GB Huge pages available\n");
if (hugepage_16MB_avail)
printf("16MB Huge pages available\n");
if (hugepage_16GB_avail)
printf("16GB Huge pages available\n");
/*
* If 2MB pages available, check that a small memzone is correctly
* reserved from 2MB huge pages when requested by the RTE_MEMZONE_2MB flag.
* Also check that RTE_MEMZONE_SIZE_HINT_ONLY flag only defaults to an
* available page size (i.e 1GB ) when 2MB pages are unavailable.
*/
if (hugepage_2MB_avail) {
mz = rte_memzone_reserve(TEST_MEMZONE_NAME("flag_zone_2M"),
size, SOCKET_ID_ANY, RTE_MEMZONE_2MB);
if (mz == NULL) {
printf("MEMZONE FLAG 2MB\n");
return -1;
}
if (mz->hugepage_sz != RTE_PGSIZE_2M) {
printf("hugepage_sz not equal 2M\n");
return -1;
}
if (rte_memzone_free(mz)) {
printf("Fail memzone free\n");
return -1;
}
mz = rte_memzone_reserve(TEST_MEMZONE_NAME("flag_zone_2M_HINT"),
size, SOCKET_ID_ANY,
RTE_MEMZONE_2MB|RTE_MEMZONE_SIZE_HINT_ONLY);
if (mz == NULL) {
printf("MEMZONE FLAG 2MB\n");
return -1;
}
if (mz->hugepage_sz != RTE_PGSIZE_2M) {
printf("hugepage_sz not equal 2M\n");
return -1;
}
if (rte_memzone_free(mz)) {
printf("Fail memzone free\n");
return -1;
}
/* Check if 1GB huge pages are unavailable, that function fails unless
* HINT flag is indicated
*/
if (!hugepage_1GB_avail) {
mz = rte_memzone_reserve(
TEST_MEMZONE_NAME("flag_zone_1G_HINT"),
size, SOCKET_ID_ANY,
RTE_MEMZONE_1GB|RTE_MEMZONE_SIZE_HINT_ONLY);
if (mz == NULL) {
printf("MEMZONE FLAG 1GB & HINT\n");
return -1;
}
if (mz->hugepage_sz != RTE_PGSIZE_2M) {
printf("hugepage_sz not equal 2M\n");
return -1;
}
if (rte_memzone_free(mz)) {
printf("Fail memzone free\n");
return -1;
}
mz = rte_memzone_reserve(
TEST_MEMZONE_NAME("flag_zone_1G"), size,
SOCKET_ID_ANY, RTE_MEMZONE_1GB);
if (mz != NULL) {
printf("MEMZONE FLAG 1GB\n");
return -1;
}
}
}
/*As with 2MB tests above for 1GB huge page requests*/
if (hugepage_1GB_avail) {
mz = rte_memzone_reserve(TEST_MEMZONE_NAME("flag_zone_1G"),
size, SOCKET_ID_ANY, RTE_MEMZONE_1GB);
if (mz == NULL) {
printf("MEMZONE FLAG 1GB\n");
return -1;
}
if (mz->hugepage_sz != RTE_PGSIZE_1G) {
printf("hugepage_sz not equal 1G\n");
return -1;
}
if (rte_memzone_free(mz)) {
printf("Fail memzone free\n");
return -1;
}
mz = rte_memzone_reserve(TEST_MEMZONE_NAME("flag_zone_1G_HINT"),
size, SOCKET_ID_ANY,
RTE_MEMZONE_1GB|RTE_MEMZONE_SIZE_HINT_ONLY);
if (mz == NULL) {
printf("MEMZONE FLAG 1GB\n");
return -1;
}
if (mz->hugepage_sz != RTE_PGSIZE_1G) {
printf("hugepage_sz not equal 1G\n");
return -1;
}
if (rte_memzone_free(mz)) {
printf("Fail memzone free\n");
return -1;
}
/* Check if 1GB huge pages are unavailable, that function fails unless
* HINT flag is indicated
*/
if (!hugepage_2MB_avail) {
mz = rte_memzone_reserve(
TEST_MEMZONE_NAME("flag_zone_2M_HINT"),
size, SOCKET_ID_ANY,
RTE_MEMZONE_2MB|RTE_MEMZONE_SIZE_HINT_ONLY);
if (mz == NULL){
printf("MEMZONE FLAG 2MB & HINT\n");
return -1;
}
if (mz->hugepage_sz != RTE_PGSIZE_1G) {
printf("hugepage_sz not equal 1G\n");
return -1;
}
if (rte_memzone_free(mz)) {
printf("Fail memzone free\n");
return -1;
}
mz = rte_memzone_reserve(
TEST_MEMZONE_NAME("flag_zone_2M"), size,
SOCKET_ID_ANY, RTE_MEMZONE_2MB);
if (mz != NULL) {
printf("MEMZONE FLAG 2MB\n");
return -1;
}
}
if (hugepage_2MB_avail && hugepage_1GB_avail) {
mz = rte_memzone_reserve(
TEST_MEMZONE_NAME("flag_zone_2M_HINT"),
size, SOCKET_ID_ANY,
RTE_MEMZONE_2MB|RTE_MEMZONE_1GB);
if (mz == NULL) {
printf("BOTH SIZES SET\n");
return -1;
}
if (mz->hugepage_sz != RTE_PGSIZE_1G &&
mz->hugepage_sz != RTE_PGSIZE_2M) {
printf("Wrong size when both sizes set\n");
return -1;
}
if (rte_memzone_free(mz)) {
printf("Fail memzone free\n");
return -1;
}
}
}
/*
* This option is for IBM Power. If 16MB pages available, check
* that a small memzone is correctly reserved from 16MB huge pages
* when requested by the RTE_MEMZONE_16MB flag. Also check that
* RTE_MEMZONE_SIZE_HINT_ONLY flag only defaults to an available
* page size (i.e 16GB ) when 16MB pages are unavailable.
*/
if (hugepage_16MB_avail) {
mz = rte_memzone_reserve(TEST_MEMZONE_NAME("flag_zone_16M"),
size, SOCKET_ID_ANY, RTE_MEMZONE_16MB);
if (mz == NULL) {
printf("MEMZONE FLAG 16MB\n");
return -1;
}
if (mz->hugepage_sz != RTE_PGSIZE_16M) {
printf("hugepage_sz not equal 16M\n");
return -1;
}
if (rte_memzone_free(mz)) {
printf("Fail memzone free\n");
return -1;
}
mz = rte_memzone_reserve(
TEST_MEMZONE_NAME("flag_zone_16M_HINT"), size,
SOCKET_ID_ANY,
RTE_MEMZONE_16MB|RTE_MEMZONE_SIZE_HINT_ONLY);
if (mz == NULL) {
printf("MEMZONE FLAG 16MB\n");
return -1;
}
if (mz->hugepage_sz != RTE_PGSIZE_16M) {
printf("hugepage_sz not equal 16M\n");
return -1;
}
if (rte_memzone_free(mz)) {
printf("Fail memzone free\n");
return -1;
}
/* Check if 1GB huge pages are unavailable, that function fails
* unless HINT flag is indicated
*/
if (!hugepage_16GB_avail) {
mz = rte_memzone_reserve(
TEST_MEMZONE_NAME("flag_zone_16G_HINT"),
size, SOCKET_ID_ANY,
RTE_MEMZONE_16GB |
RTE_MEMZONE_SIZE_HINT_ONLY);
if (mz == NULL) {
printf("MEMZONE FLAG 16GB & HINT\n");
return -1;
}
if (mz->hugepage_sz != RTE_PGSIZE_16M) {
printf("hugepage_sz not equal 16M\n");
return -1;
}
if (rte_memzone_free(mz)) {
printf("Fail memzone free\n");
return -1;
}
mz = rte_memzone_reserve(
TEST_MEMZONE_NAME("flag_zone_16G"),
size,
SOCKET_ID_ANY, RTE_MEMZONE_16GB);
if (mz != NULL) {
printf("MEMZONE FLAG 16GB\n");
return -1;
}
}
}
/*As with 16MB tests above for 16GB huge page requests*/
if (hugepage_16GB_avail) {
mz = rte_memzone_reserve(TEST_MEMZONE_NAME("flag_zone_16G"),
size, SOCKET_ID_ANY, RTE_MEMZONE_16GB);
if (mz == NULL) {
printf("MEMZONE FLAG 16GB\n");
return -1;
}
if (mz->hugepage_sz != RTE_PGSIZE_16G) {
printf("hugepage_sz not equal 16G\n");
return -1;
}
if (rte_memzone_free(mz)) {
printf("Fail memzone free\n");
return -1;
}
mz = rte_memzone_reserve(
TEST_MEMZONE_NAME("flag_zone_16G_HINT"), size,
SOCKET_ID_ANY,
RTE_MEMZONE_16GB|RTE_MEMZONE_SIZE_HINT_ONLY);
if (mz == NULL) {
printf("MEMZONE FLAG 16GB\n");
return -1;
}
if (mz->hugepage_sz != RTE_PGSIZE_16G) {
printf("hugepage_sz not equal 16G\n");
return -1;
}
if (rte_memzone_free(mz)) {
printf("Fail memzone free\n");
return -1;
}
/* Check if 1GB huge pages are unavailable, that function fails
* unless HINT flag is indicated
*/
if (!hugepage_16MB_avail) {
mz = rte_memzone_reserve(
TEST_MEMZONE_NAME("flag_zone_16M_HINT"),
size, SOCKET_ID_ANY,
RTE_MEMZONE_16MB |
RTE_MEMZONE_SIZE_HINT_ONLY);
if (mz == NULL) {
printf("MEMZONE FLAG 16MB & HINT\n");
return -1;
}
if (mz->hugepage_sz != RTE_PGSIZE_16G) {
printf("hugepage_sz not equal 16G\n");
return -1;
}
if (rte_memzone_free(mz)) {
printf("Fail memzone free\n");
return -1;
}
mz = rte_memzone_reserve(
TEST_MEMZONE_NAME("flag_zone_16M"),
size, SOCKET_ID_ANY, RTE_MEMZONE_16MB);
if (mz != NULL) {
printf("MEMZONE FLAG 16MB\n");
return -1;
}
}
if (hugepage_16MB_avail && hugepage_16GB_avail) {
mz = rte_memzone_reserve(
TEST_MEMZONE_NAME("flag_zone_16M_HINT"),
size, SOCKET_ID_ANY,
RTE_MEMZONE_16MB|RTE_MEMZONE_16GB);
if (mz == NULL) {
printf("BOTH SIZES SET\n");
return -1;
}
if (mz->hugepage_sz != RTE_PGSIZE_16G &&
mz->hugepage_sz != RTE_PGSIZE_16M) {
printf("Wrong size when both sizes set\n");
return -1;
}
if (rte_memzone_free(mz)) {
printf("Fail memzone free\n");
return -1;
}
}
}
return 0;
}
/* Find the heap with the greatest free block size */
static size_t
find_max_block_free_size(unsigned int align, unsigned int socket_id)
{
struct rte_malloc_socket_stats stats;
size_t len, overhead;
if (rte_malloc_get_socket_stats(socket_id, &stats) < 0)
return 0;
len = stats.greatest_free_size;
overhead = MALLOC_ELEM_OVERHEAD;
if (len == 0)
return 0;
align = RTE_CACHE_LINE_ROUNDUP(align);
overhead += align;
if (len < overhead)
return 0;
return len - overhead;
}
static int
test_memzone_reserve_max(void)
{
unsigned int i;
for (i = 0; i < rte_socket_count(); i++) {
const struct rte_memzone *mz;
size_t maxlen;
int socket;
socket = rte_socket_id_by_idx(i);
maxlen = find_max_block_free_size(0, socket);
if (maxlen == 0) {
printf("There is no space left!\n");
return 0;
}
mz = rte_memzone_reserve(TEST_MEMZONE_NAME("max_zone"), 0,
socket, 0);
if (mz == NULL) {
printf("Failed to reserve a big chunk of memory - %s\n",
rte_strerror(rte_errno));
rte_dump_physmem_layout(stdout);
rte_memzone_dump(stdout);
return -1;
}
if (mz->len != maxlen) {
printf("Memzone reserve with 0 size did not return biggest block\n");
printf("Expected size = %zu, actual size = %zu\n",
maxlen, mz->len);
rte_dump_physmem_layout(stdout);
rte_memzone_dump(stdout);
return -1;
}
if (rte_memzone_free(mz)) {
printf("Fail memzone free\n");
return -1;
}
}
return 0;
}
static int
test_memzone_reserve_max_aligned(void)
{
unsigned int i;
for (i = 0; i < rte_socket_count(); i++) {
const struct rte_memzone *mz;
size_t maxlen, minlen = 0;
int socket;
socket = rte_socket_id_by_idx(i);
/* random alignment */
const unsigned int align = 1 << ((rte_rand() % 8) + 5); /* from 128 up to 4k alignment */
/* memzone size may be between size and size - align */
minlen = find_max_block_free_size(align, socket);
maxlen = find_max_block_free_size(0, socket);
if (minlen == 0 || maxlen == 0) {
printf("There is no space left for biggest %u-aligned memzone!\n",
align);
return 0;
}
mz = rte_memzone_reserve_aligned(
TEST_MEMZONE_NAME("max_zone_aligned"),
0, socket, 0, align);
if (mz == NULL) {
printf("Failed to reserve a big chunk of memory - %s\n",
rte_strerror(rte_errno));
rte_dump_physmem_layout(stdout);
rte_memzone_dump(stdout);
return -1;
}
if (mz->addr != RTE_PTR_ALIGN(mz->addr, align)) {
printf("Memzone reserve with 0 size and alignment %u did not return aligned block\n",
align);
rte_dump_physmem_layout(stdout);
rte_memzone_dump(stdout);
return -1;
}
if (mz->len < minlen || mz->len > maxlen) {
printf("Memzone reserve with 0 size and alignment %u did not return"
" biggest block\n", align);
printf("Expected size = %zu-%zu, actual size = %zu\n",
minlen, maxlen, mz->len);
rte_dump_physmem_layout(stdout);
rte_memzone_dump(stdout);
return -1;
}
if (rte_memzone_free(mz)) {
printf("Fail memzone free\n");
return -1;
}
}
return 0;
}
static int
test_memzone_aligned(void)
{
const struct rte_memzone *memzone_aligned_32;
const struct rte_memzone *memzone_aligned_128;
const struct rte_memzone *memzone_aligned_256;
const struct rte_memzone *memzone_aligned_512;
const struct rte_memzone *memzone_aligned_1024;
/* memzone that should automatically be adjusted to align on 64 bytes */
memzone_aligned_32 = rte_memzone_reserve_aligned(
TEST_MEMZONE_NAME("aligned_32"), 100, SOCKET_ID_ANY, 0,
32);
/* memzone that is supposed to be aligned on a 128 byte boundary */
memzone_aligned_128 = rte_memzone_reserve_aligned(
TEST_MEMZONE_NAME("aligned_128"), 100, SOCKET_ID_ANY, 0,
128);
/* memzone that is supposed to be aligned on a 256 byte boundary */
memzone_aligned_256 = rte_memzone_reserve_aligned(
TEST_MEMZONE_NAME("aligned_256"), 100, SOCKET_ID_ANY, 0,
256);
/* memzone that is supposed to be aligned on a 512 byte boundary */
memzone_aligned_512 = rte_memzone_reserve_aligned(
TEST_MEMZONE_NAME("aligned_512"), 100, SOCKET_ID_ANY, 0,
512);
/* memzone that is supposed to be aligned on a 1024 byte boundary */
memzone_aligned_1024 = rte_memzone_reserve_aligned(
TEST_MEMZONE_NAME("aligned_1024"), 100, SOCKET_ID_ANY,
0, 1024);
printf("check alignments and lengths\n");
if (memzone_aligned_32 == NULL) {
printf("Unable to reserve 64-byte aligned memzone!\n");
return -1;
}
if ((memzone_aligned_32->iova & RTE_CACHE_LINE_MASK) != 0)
return -1;
if (((uintptr_t) memzone_aligned_32->addr & RTE_CACHE_LINE_MASK) != 0)
return -1;
if ((memzone_aligned_32->len & RTE_CACHE_LINE_MASK) != 0)
return -1;
if (memzone_aligned_128 == NULL) {
printf("Unable to reserve 128-byte aligned memzone!\n");
return -1;
}
if ((memzone_aligned_128->iova & 127) != 0)
return -1;
if (((uintptr_t) memzone_aligned_128->addr & 127) != 0)
return -1;
if ((memzone_aligned_128->len & RTE_CACHE_LINE_MASK) != 0)
return -1;
if (memzone_aligned_256 == NULL) {
printf("Unable to reserve 256-byte aligned memzone!\n");
return -1;
}
if ((memzone_aligned_256->iova & 255) != 0)
return -1;
if (((uintptr_t) memzone_aligned_256->addr & 255) != 0)
return -1;
if ((memzone_aligned_256->len & RTE_CACHE_LINE_MASK) != 0)
return -1;
if (memzone_aligned_512 == NULL) {
printf("Unable to reserve 512-byte aligned memzone!\n");
return -1;
}
if ((memzone_aligned_512->iova & 511) != 0)
return -1;
if (((uintptr_t) memzone_aligned_512->addr & 511) != 0)
return -1;
if ((memzone_aligned_512->len & RTE_CACHE_LINE_MASK) != 0)
return -1;
if (memzone_aligned_1024 == NULL) {
printf("Unable to reserve 1024-byte aligned memzone!\n");
return -1;
}
if ((memzone_aligned_1024->iova & 1023) != 0)
return -1;
if (((uintptr_t) memzone_aligned_1024->addr & 1023) != 0)
return -1;
if ((memzone_aligned_1024->len & RTE_CACHE_LINE_MASK) != 0)
return -1;
/* check that zones don't overlap */
printf("check overlapping\n");
if (is_memory_overlap(memzone_aligned_32->iova, memzone_aligned_32->len,
memzone_aligned_128->iova, memzone_aligned_128->len))
return -1;
if (is_memory_overlap(memzone_aligned_32->iova, memzone_aligned_32->len,
memzone_aligned_256->iova, memzone_aligned_256->len))
return -1;
if (is_memory_overlap(memzone_aligned_32->iova, memzone_aligned_32->len,
memzone_aligned_512->iova, memzone_aligned_512->len))
return -1;
if (is_memory_overlap(memzone_aligned_32->iova, memzone_aligned_32->len,
memzone_aligned_1024->iova, memzone_aligned_1024->len))
return -1;
if (is_memory_overlap(memzone_aligned_128->iova, memzone_aligned_128->len,
memzone_aligned_256->iova, memzone_aligned_256->len))
return -1;
if (is_memory_overlap(memzone_aligned_128->iova, memzone_aligned_128->len,
memzone_aligned_512->iova, memzone_aligned_512->len))
return -1;
if (is_memory_overlap(memzone_aligned_128->iova, memzone_aligned_128->len,
memzone_aligned_1024->iova, memzone_aligned_1024->len))
return -1;
if (is_memory_overlap(memzone_aligned_256->iova, memzone_aligned_256->len,
memzone_aligned_512->iova, memzone_aligned_512->len))
return -1;
if (is_memory_overlap(memzone_aligned_256->iova, memzone_aligned_256->len,
memzone_aligned_1024->iova, memzone_aligned_1024->len))
return -1;
if (is_memory_overlap(memzone_aligned_512->iova, memzone_aligned_512->len,
memzone_aligned_1024->iova, memzone_aligned_1024->len))
return -1;
/* free all used zones */
if (rte_memzone_free(memzone_aligned_32)) {
printf("Fail memzone free\n");
return -1;
}
if (rte_memzone_free(memzone_aligned_128)) {
printf("Fail memzone free\n");
return -1;
}
if (rte_memzone_free(memzone_aligned_256)) {
printf("Fail memzone free\n");
return -1;
}
if (rte_memzone_free(memzone_aligned_512)) {
printf("Fail memzone free\n");
return -1;
}
if (rte_memzone_free(memzone_aligned_1024)) {
printf("Fail memzone free\n");
return -1;
}
return 0;
}
static int
check_memzone_bounded(const char *name, uint32_t len, uint32_t align,
uint32_t bound)
{
const struct rte_memzone *mz;
rte_iova_t bmask;
bmask = ~((rte_iova_t)bound - 1);
if ((mz = rte_memzone_reserve_bounded(name, len, SOCKET_ID_ANY, 0,
align, bound)) == NULL) {
printf("%s(%s): memzone creation failed\n",
__func__, name);
return -1;
}
if ((mz->iova & ((rte_iova_t)align - 1)) != 0) {
printf("%s(%s): invalid phys addr alignment\n",
__func__, mz->name);
return -1;
}
if (((uintptr_t) mz->addr & ((uintptr_t)align - 1)) != 0) {
printf("%s(%s): invalid virtual addr alignment\n",
__func__, mz->name);
return -1;
}
if ((mz->len & RTE_CACHE_LINE_MASK) != 0 || mz->len < len ||
mz->len < RTE_CACHE_LINE_SIZE) {
printf("%s(%s): invalid length\n",
__func__, mz->name);
return -1;
}
if ((mz->iova & bmask) !=
((mz->iova + mz->len - 1) & bmask)) {
printf("%s(%s): invalid memzone boundary %u crossed\n",
__func__, mz->name, bound);
return -1;
}
if (rte_memzone_free(mz)) {
printf("Fail memzone free\n");
return -1;
}
return 0;
}
static int
test_memzone_bounded(void)
{
const struct rte_memzone *memzone_err;
int rc;
/* should fail as boundary is not power of two */
memzone_err = rte_memzone_reserve_bounded(
TEST_MEMZONE_NAME("bounded_error_31"), 100,
SOCKET_ID_ANY, 0, 32, UINT32_MAX);
if (memzone_err != NULL) {
printf("%s(%s)created a memzone with invalid boundary "
"conditions\n", __func__, memzone_err->name);
return -1;
}
/* should fail as len is greater then boundary */
memzone_err = rte_memzone_reserve_bounded(
TEST_MEMZONE_NAME("bounded_error_32"), 100,
SOCKET_ID_ANY, 0, 32, 32);
if (memzone_err != NULL) {
printf("%s(%s)created a memzone with invalid boundary "
"conditions\n", __func__, memzone_err->name);
return -1;
}
rc = check_memzone_bounded(TEST_MEMZONE_NAME("bounded_128"), 100, 128,
128);
if (rc != 0)
return rc;
rc = check_memzone_bounded(TEST_MEMZONE_NAME("bounded_256"), 100, 256,
128);
if (rc != 0)
return rc;
rc = check_memzone_bounded(TEST_MEMZONE_NAME("bounded_1K"), 100, 64,
1024);
if (rc != 0)
return rc;
rc = check_memzone_bounded(TEST_MEMZONE_NAME("bounded_1K_MAX"), 0, 64,
1024);
if (rc != 0)
return rc;
return 0;
}
static int
test_memzone_free(void)
{
const struct rte_memzone **mz;
int i;
char name[20];
int rc = -1;
mz = rte_calloc("memzone_test", rte_memzone_max_get() + 1,
sizeof(struct rte_memzone *), 0);
if (!mz) {
printf("Fail allocating memzone test array\n");
return rc;
}
mz[0] = rte_memzone_reserve(TEST_MEMZONE_NAME("tempzone0"), 2000,
SOCKET_ID_ANY, 0);
mz[1] = rte_memzone_reserve(TEST_MEMZONE_NAME("tempzone1"), 4000,
SOCKET_ID_ANY, 0);
if (mz[0] > mz[1])
goto exit_test;
if (!rte_memzone_lookup(TEST_MEMZONE_NAME("tempzone0")))
goto exit_test;
if (!rte_memzone_lookup(TEST_MEMZONE_NAME("tempzone1")))
goto exit_test;
if (rte_memzone_free(mz[0])) {
printf("Fail memzone free - tempzone0\n");
goto exit_test;
}
if (rte_memzone_lookup(TEST_MEMZONE_NAME("tempzone0"))) {
printf("Found previously free memzone - tempzone0\n");
goto exit_test;
}
mz[2] = rte_memzone_reserve(TEST_MEMZONE_NAME("tempzone2"), 2000,
SOCKET_ID_ANY, 0);
if (mz[2] > mz[1]) {
printf("tempzone2 should have gotten the free entry from tempzone0\n");
goto exit_test;
}
if (rte_memzone_free(mz[2])) {
printf("Fail memzone free - tempzone2\n");
goto exit_test;
}
if (rte_memzone_lookup(TEST_MEMZONE_NAME("tempzone2"))) {
printf("Found previously free memzone - tempzone2\n");
goto exit_test;
}
if (rte_memzone_free(mz[1])) {
printf("Fail memzone free - tempzone1\n");
goto exit_test;
}
if (rte_memzone_lookup(TEST_MEMZONE_NAME("tempzone1"))) {
printf("Found previously free memzone - tempzone1\n");
goto exit_test;
}
i = 0;
do {
snprintf(name, sizeof(name), TEST_MEMZONE_NAME("tempzone%u"),
i);
mz[i] = rte_memzone_reserve(name, 1, SOCKET_ID_ANY, 0);
} while (mz[i++] != NULL);
if (rte_memzone_free(mz[0])) {
printf("Fail memzone free - tempzone0\n");
goto exit_test;
}
mz[0] = rte_memzone_reserve(TEST_MEMZONE_NAME("tempzone0new"), 0,
SOCKET_ID_ANY, 0);
if (mz[0] == NULL) {
printf("Fail to create memzone - tempzone0new - when MAX memzones were "
"created and one was free\n");
goto exit_test;
}
for (i = i - 2; i >= 0; i--) {
if (rte_memzone_free(mz[i])) {
printf("Fail memzone free - tempzone%d\n", i);
goto exit_test;
}
}
rc = 0;
exit_test:
rte_free(mz);
return rc;
}
static int test_memzones_left;
static int memzone_walk_cnt;
static void memzone_walk_clb(const struct rte_memzone *mz,
void *arg __rte_unused)
{
memzone_walk_cnt++;
if (!strncmp(TEST_MEMZONE_NAME(""), mz->name, RTE_MEMZONE_NAMESIZE))
test_memzones_left++;
}
static int
test_memzone_basic(void)
{
const struct rte_memzone *memzone1;
const struct rte_memzone *memzone2;
const struct rte_memzone *memzone3;
const struct rte_memzone *memzone4;
const struct rte_memzone *mz;
int memzone_cnt_after, memzone_cnt_expected;
int memzone_cnt_before;
memzone_walk_cnt = 0;
test_memzones_left = 0;
rte_memzone_walk(memzone_walk_clb, NULL);
memzone_cnt_before = memzone_walk_cnt;
memzone1 = rte_memzone_reserve(TEST_MEMZONE_NAME("testzone1"), 100,
SOCKET_ID_ANY, 0);
memzone2 = rte_memzone_reserve(TEST_MEMZONE_NAME("testzone2"), 1000,
0, 0);
memzone3 = rte_memzone_reserve(TEST_MEMZONE_NAME("testzone3"), 1000,
1, 0);
memzone4 = rte_memzone_reserve(TEST_MEMZONE_NAME("testzone4"), 1024,
SOCKET_ID_ANY, 0);
/* memzone3 may be NULL if we don't have NUMA */
if (memzone1 == NULL || memzone2 == NULL || memzone4 == NULL)
return -1;
/* check how many memzones we are expecting */
memzone_cnt_expected = memzone_cnt_before +
(memzone1 != NULL) + (memzone2 != NULL) +
(memzone3 != NULL) + (memzone4 != NULL);
memzone_walk_cnt = 0;
test_memzones_left = 0;
rte_memzone_walk(memzone_walk_clb, NULL);
memzone_cnt_after = memzone_walk_cnt;
if (memzone_cnt_after != memzone_cnt_expected)
return -1;
rte_memzone_dump(stdout);
/* check cache-line alignments */
printf("check alignments and lengths\n");
if ((memzone1->iova & RTE_CACHE_LINE_MASK) != 0)
return -1;
if ((memzone2->iova & RTE_CACHE_LINE_MASK) != 0)
return -1;
if (memzone3 != NULL && (memzone3->iova & RTE_CACHE_LINE_MASK) != 0)
return -1;
if ((memzone1->len & RTE_CACHE_LINE_MASK) != 0 || memzone1->len == 0)
return -1;
if ((memzone2->len & RTE_CACHE_LINE_MASK) != 0 || memzone2->len == 0)
return -1;
if (memzone3 != NULL && ((memzone3->len & RTE_CACHE_LINE_MASK) != 0 ||
memzone3->len == 0))
return -1;
if (memzone4->len != 1024)
return -1;
/* check that zones don't overlap */
printf("check overlapping\n");
if (is_memory_overlap(memzone1->iova, memzone1->len,
memzone2->iova, memzone2->len))
return -1;
if (memzone3 != NULL &&
is_memory_overlap(memzone1->iova, memzone1->len,
memzone3->iova, memzone3->len))
return -1;
if (memzone3 != NULL &&
is_memory_overlap(memzone2->iova, memzone2->len,
memzone3->iova, memzone3->len))
return -1;
printf("check socket ID\n");
/* memzone2 must be on socket id 0 and memzone3 on socket 1 */
if (memzone2->socket_id != 0)
return -1;
if (memzone3 != NULL && memzone3->socket_id != 1)
return -1;
printf("test zone lookup\n");
mz = rte_memzone_lookup(TEST_MEMZONE_NAME("testzone1"));
if (mz != memzone1)
return -1;
printf("test duplicate zone name\n");
mz = rte_memzone_reserve(TEST_MEMZONE_NAME("testzone1"), 100,
SOCKET_ID_ANY, 0);
if (mz != NULL)
return -1;
if (rte_memzone_free(memzone1)) {
printf("Fail memzone free - memzone1\n");
return -1;
}
if (rte_memzone_free(memzone2)) {
printf("Fail memzone free - memzone2\n");
return -1;
}
if (memzone3 && rte_memzone_free(memzone3)) {
printf("Fail memzone free - memzone3\n");
return -1;
}
if (rte_memzone_free(memzone4)) {
printf("Fail memzone free - memzone4\n");
return -1;
}
memzone_walk_cnt = 0;
test_memzones_left = 0;
rte_memzone_walk(memzone_walk_clb, NULL);
memzone_cnt_after = memzone_walk_cnt;
if (memzone_cnt_after != memzone_cnt_before)
return -1;
return 0;
}
static int
test_memzone(void)
{
/* take note of how many memzones were allocated before running */
int memzone_cnt;
memzone_walk_cnt = 0;
test_memzones_left = 0;
rte_memzone_walk(memzone_walk_clb, NULL);
memzone_cnt = memzone_walk_cnt;
printf("test basic memzone API\n");
if (test_memzone_basic() < 0)
return -1;
printf("test free memzone\n");
if (test_memzone_free() < 0)
return -1;
printf("test reserving memzone with bigger size than the maximum\n");
if (test_memzone_reserving_zone_size_bigger_than_the_maximum() < 0)
return -1;
printf("test memzone_reserve flags\n");
if (test_memzone_reserve_flags() < 0)
return -1;
printf("test alignment for memzone_reserve\n");
if (test_memzone_aligned() < 0)
return -1;
printf("test boundary alignment for memzone_reserve\n");
if (test_memzone_bounded() < 0)
return -1;
printf("test invalid alignment for memzone_reserve\n");
if (test_memzone_invalid_alignment() < 0)
return -1;
printf("test invalid flags for memzone_reserve\n");
if (test_memzone_invalid_flags() < 0)
return -1;
printf("test reserving the largest size memzone possible\n");
if (test_memzone_reserve_max() < 0)
return -1;
printf("test reserving the largest size aligned memzone possible\n");
if (test_memzone_reserve_max_aligned() < 0)
return -1;
printf("check memzone cleanup\n");
memzone_walk_cnt = 0;
test_memzones_left = 0;
rte_memzone_walk(memzone_walk_clb, NULL);
if (memzone_walk_cnt != memzone_cnt || test_memzones_left > 0) {
printf("there are some memzones left after test\n");
rte_memzone_dump(stdout);
return -1;
}
return 0;
}
REGISTER_FAST_TEST(memzone_autotest, false, true, test_memzone);
|