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 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390
|
/* SPDX-License-Identifier: BSD-3-Clause
* Copyright 2019 Mellanox Technologies, Ltd
*/
#include <rte_malloc.h>
#include <mlx5_malloc.h>
#include "mlx5_utils.h"
/********************* Indexed pool **********************/
#if defined(RTE_TOOLCHAIN_GCC) || defined(RTE_TOOLCHAIN_CLANG)
#define pool_malloc(pool, flags, size, align, socket) (__extension__ ({ \
struct mlx5_indexed_pool *p = (struct mlx5_indexed_pool *)(pool); \
uint32_t f = (uint32_t)(flags); \
size_t s = (size_t)(size); \
uint32_t a = (uint32_t)(align); \
int so = (int)(socket); \
void *mem = p->cfg.malloc(f, s, a, so); \
if (mem == NULL && so != SOCKET_ID_ANY) { \
mem = p->cfg.malloc(f, s, a, SOCKET_ID_ANY); \
if (mem) { \
DRV_LOG(WARNING, \
"Allocated %p (size %zu socket %d) through NUMA tolerant fallback", \
mem, s, so); \
} \
} \
mem; \
}))
#else
#define pool_malloc(pool, flags, size, align, socket) \
((pool)->cfg.malloc((uint32_t)(flags) | MLX5_NUMA_TOLERANT, (size), (align), (socket)))
#endif
int mlx5_logtype_ipool;
/* Initialize driver log type. */
RTE_LOG_REGISTER_SUFFIX(mlx5_logtype_ipool, ipool, NOTICE)
static inline void
mlx5_ipool_lock(struct mlx5_indexed_pool *pool)
{
if (pool->cfg.need_lock)
rte_spinlock_lock(&pool->rsz_lock);
}
static inline void
mlx5_ipool_unlock(struct mlx5_indexed_pool *pool)
{
if (pool->cfg.need_lock)
rte_spinlock_unlock(&pool->rsz_lock);
}
static inline uint32_t
mlx5_trunk_idx_get(struct mlx5_indexed_pool *pool, uint32_t entry_idx)
{
struct mlx5_indexed_pool_config *cfg = &pool->cfg;
uint32_t trunk_idx = 0;
uint32_t i;
if (!cfg->grow_trunk)
return entry_idx / cfg->trunk_size;
if (entry_idx >= pool->grow_tbl[cfg->grow_trunk - 1]) {
trunk_idx = (entry_idx - pool->grow_tbl[cfg->grow_trunk - 1]) /
(cfg->trunk_size << (cfg->grow_shift *
cfg->grow_trunk)) + cfg->grow_trunk;
} else {
for (i = 0; i < cfg->grow_trunk; i++) {
if (entry_idx < pool->grow_tbl[i])
break;
}
trunk_idx = i;
}
return trunk_idx;
}
static inline uint32_t
mlx5_trunk_size_get(struct mlx5_indexed_pool *pool, uint32_t trunk_idx)
{
struct mlx5_indexed_pool_config *cfg = &pool->cfg;
return cfg->trunk_size << (cfg->grow_shift *
(trunk_idx > cfg->grow_trunk ? cfg->grow_trunk : trunk_idx));
}
static inline uint32_t
mlx5_trunk_idx_offset_get(struct mlx5_indexed_pool *pool, uint32_t trunk_idx)
{
struct mlx5_indexed_pool_config *cfg = &pool->cfg;
uint32_t offset = 0;
if (!trunk_idx)
return 0;
if (!cfg->grow_trunk)
return cfg->trunk_size * trunk_idx;
if (trunk_idx < cfg->grow_trunk)
offset = pool->grow_tbl[trunk_idx - 1];
else
offset = pool->grow_tbl[cfg->grow_trunk - 1] +
(cfg->trunk_size << (cfg->grow_shift *
cfg->grow_trunk)) * (trunk_idx - cfg->grow_trunk);
return offset;
}
struct mlx5_indexed_pool *
mlx5_ipool_create(struct mlx5_indexed_pool_config *cfg)
{
struct mlx5_indexed_pool *pool;
uint32_t i;
if (!cfg || (!cfg->malloc ^ !cfg->free) ||
(cfg->per_core_cache && cfg->release_mem_en) ||
(cfg->trunk_size && ((cfg->trunk_size & (cfg->trunk_size - 1)) ||
((rte_ffs32(cfg->trunk_size) + TRUNK_IDX_BITS) > 32))))
return NULL;
pool = mlx5_malloc(MLX5_MEM_ZERO, sizeof(*pool) + cfg->grow_trunk *
sizeof(pool->grow_tbl[0]), RTE_CACHE_LINE_SIZE,
SOCKET_ID_ANY);
if (!pool)
return NULL;
pool->cfg = *cfg;
if (!pool->cfg.trunk_size)
pool->cfg.trunk_size = MLX5_IPOOL_DEFAULT_TRUNK_SIZE;
if (!cfg->malloc && !cfg->free) {
pool->cfg.malloc = mlx5_malloc;
pool->cfg.free = mlx5_free;
}
if (pool->cfg.need_lock)
rte_spinlock_init(&pool->rsz_lock);
/*
* Initialize the dynamic grow trunk size lookup table to have a quick
* lookup for the trunk entry index offset.
*/
for (i = 0; i < cfg->grow_trunk; i++) {
pool->grow_tbl[i] = cfg->trunk_size << (cfg->grow_shift * i);
if (i > 0)
pool->grow_tbl[i] += pool->grow_tbl[i - 1];
}
if (!pool->cfg.max_idx)
pool->cfg.max_idx =
mlx5_trunk_idx_offset_get(pool, TRUNK_MAX_IDX + 1);
if (!cfg->per_core_cache)
pool->free_list = TRUNK_INVALID;
rte_spinlock_init(&pool->lcore_lock);
#ifdef POOL_DEBUG
rte_spinlock_init(&pool->cache_validator.lock);
#endif
DRV_LOG_IPOOL(INFO, "lcore id %d: pool %s: per core cache mode %s",
rte_lcore_id(), pool->cfg.type, pool->cfg.per_core_cache != 0 ? "on" : "off");
return pool;
}
static int
mlx5_ipool_grow(struct mlx5_indexed_pool *pool)
{
struct mlx5_indexed_trunk *trunk;
struct mlx5_indexed_trunk **trunk_tmp;
struct mlx5_indexed_trunk **p;
size_t trunk_size = 0;
size_t data_size;
size_t bmp_size;
uint32_t idx, cur_max_idx, i;
cur_max_idx = mlx5_trunk_idx_offset_get(pool, pool->n_trunk_valid);
if (pool->n_trunk_valid == TRUNK_MAX_IDX ||
cur_max_idx >= pool->cfg.max_idx)
return -ENOMEM;
if (pool->n_trunk_valid == pool->n_trunk) {
/* No free trunk flags, expand trunk list. */
int n_grow = pool->n_trunk_valid ? pool->n_trunk :
RTE_CACHE_LINE_SIZE / sizeof(void *);
p = pool_malloc(pool, MLX5_MEM_ZERO, (pool->n_trunk_valid + n_grow) *
sizeof(struct mlx5_indexed_trunk *),
RTE_CACHE_LINE_SIZE, rte_socket_id());
if (!p)
return -ENOMEM;
if (pool->trunks)
memcpy(p, pool->trunks, pool->n_trunk_valid *
sizeof(struct mlx5_indexed_trunk *));
memset(RTE_PTR_ADD(p, pool->n_trunk_valid * sizeof(void *)), 0,
n_grow * sizeof(void *));
trunk_tmp = pool->trunks;
pool->trunks = p;
if (trunk_tmp)
pool->cfg.free(trunk_tmp);
pool->n_trunk += n_grow;
}
if (!pool->cfg.release_mem_en) {
idx = pool->n_trunk_valid;
} else {
/* Find the first available slot in trunk list */
for (idx = 0; idx < pool->n_trunk; idx++)
if (pool->trunks[idx] == NULL)
break;
}
trunk_size += sizeof(*trunk);
data_size = mlx5_trunk_size_get(pool, idx);
bmp_size = rte_bitmap_get_memory_footprint(data_size);
/* rte_bitmap requires memory cacheline aligned. */
trunk_size += RTE_CACHE_LINE_ROUNDUP(data_size * pool->cfg.size);
trunk_size += bmp_size;
trunk = pool_malloc(pool, MLX5_MEM_ZERO, trunk_size,
RTE_CACHE_LINE_SIZE, rte_socket_id());
if (!trunk)
return -ENOMEM;
pool->trunks[idx] = trunk;
trunk->idx = idx;
trunk->free = data_size;
trunk->prev = TRUNK_INVALID;
trunk->next = TRUNK_INVALID;
MLX5_ASSERT(pool->free_list == TRUNK_INVALID);
pool->free_list = idx;
/* Mark all entries as available. */
trunk->bmp = rte_bitmap_init_with_all_set(data_size, &trunk->data
[RTE_CACHE_LINE_ROUNDUP(data_size * pool->cfg.size)],
bmp_size);
/* Clear the overhead bits in the trunk if it happens. */
if (cur_max_idx + data_size > pool->cfg.max_idx) {
for (i = pool->cfg.max_idx - cur_max_idx; i < data_size; i++)
rte_bitmap_clear(trunk->bmp, i);
}
MLX5_ASSERT(trunk->bmp);
pool->n_trunk_valid++;
#ifdef POOL_DEBUG
pool->trunk_new++;
pool->trunk_avail++;
#endif
return 0;
}
static inline struct mlx5_indexed_cache *
mlx5_ipool_update_global_cache(struct mlx5_indexed_pool *pool, int cidx)
{
struct mlx5_indexed_cache *gc, *lc, *olc = NULL;
lc = pool->cache[cidx]->lc;
gc = rte_atomic_load_explicit(&pool->gc, rte_memory_order_relaxed);
if (gc && lc != gc) {
mlx5_ipool_lock(pool);
if (lc && !(--lc->ref_cnt))
olc = lc;
lc = pool->gc;
lc->ref_cnt++;
pool->cache[cidx]->lc = lc;
mlx5_ipool_unlock(pool);
if (olc)
pool->cfg.free(olc);
DRV_LOG_IPOOL(DEBUG, "lcore id %d: pool %s: updated lcache %d "
"ref %d, new %p, old %p", rte_lcore_id(), pool->cfg.type,
cidx, lc->ref_cnt, (void *)lc, (void *)olc);
}
return lc;
}
#ifdef POOL_DEBUG
static void
mlx5_ipool_grow_bmp(struct mlx5_indexed_pool *pool, uint32_t new_size)
{
struct rte_bitmap *old_bmp = NULL;
void *old_bmp_mem = NULL;
uint32_t old_size = 0;
uint32_t i, bmp_mem_size;
if (pool->cache_validator.bmp_mem && pool->cache_validator.bmp) {
old_bmp = pool->cache_validator.bmp;
old_size = pool->cache_validator.bmp_size;
old_bmp_mem = pool->cache_validator.bmp_mem;
}
if (unlikely(new_size <= old_size))
return;
pool->cache_validator.bmp_size = new_size;
bmp_mem_size = rte_bitmap_get_memory_footprint(new_size);
pool->cache_validator.bmp_mem = pool_malloc(pool, MLX5_MEM_ZERO,
bmp_mem_size,
RTE_CACHE_LINE_SIZE,
rte_socket_id());
if (unlikely(!pool->cache_validator.bmp_mem)) {
DRV_LOG_IPOOL(ERR, "Unable to allocate memory for a new bitmap");
return;
}
pool->cache_validator.bmp = rte_bitmap_init_with_all_set(pool->cache_validator.bmp_size,
pool->cache_validator.bmp_mem,
bmp_mem_size);
if (unlikely(!pool->cache_validator.bmp)) {
DRV_LOG(ERR, "Unable to allocate memory for a new bitmap");
pool->cfg.free(pool->cache_validator.bmp_mem);
return;
}
if (old_bmp && old_bmp_mem) {
for (i = 0; i < old_size; i++) {
if (rte_bitmap_get(old_bmp, i) == 0)
rte_bitmap_clear(pool->cache_validator.bmp, i);
}
rte_bitmap_free(old_bmp);
pool->cfg.free(old_bmp_mem);
}
}
#endif
static uint32_t
mlx5_ipool_allocate_from_global(struct mlx5_indexed_pool *pool, int cidx)
{
struct mlx5_indexed_trunk *trunk;
struct mlx5_indexed_cache *p, *lc, *olc = NULL;
size_t trunk_size = 0;
size_t data_size;
uint32_t cur_max_idx, trunk_idx, trunk_n;
uint32_t fetch_size, ts_idx, i;
int n_grow;
check_again:
p = NULL;
fetch_size = 0;
/*
* Fetch new index from global if possible. First round local
* cache will be NULL.
*/
lc = pool->cache[cidx]->lc;
mlx5_ipool_lock(pool);
/* Try to update local cache first. */
if (likely(pool->gc)) {
if (lc != pool->gc) {
if (lc && !(--lc->ref_cnt))
olc = lc;
lc = pool->gc;
lc->ref_cnt++;
pool->cache[cidx]->lc = lc;
}
if (lc->len) {
/* Use the updated local cache to fetch index. */
fetch_size = pool->cfg.per_core_cache >> 2;
if (lc->len < fetch_size)
fetch_size = lc->len;
lc->len -= fetch_size;
memcpy(pool->cache[cidx]->idx, &lc->idx[lc->len],
sizeof(uint32_t) * fetch_size);
}
}
mlx5_ipool_unlock(pool);
if (unlikely(olc)) {
pool->cfg.free(olc);
olc = NULL;
}
if (fetch_size) {
pool->cache[cidx]->len = fetch_size - 1;
return pool->cache[cidx]->idx[pool->cache[cidx]->len];
}
trunk_idx = lc ? rte_atomic_load_explicit(&lc->n_trunk_valid,
rte_memory_order_acquire) : 0;
trunk_n = lc ? lc->n_trunk : 0;
cur_max_idx = mlx5_trunk_idx_offset_get(pool, trunk_idx);
/* Check if index reach maximum. */
if (trunk_idx == TRUNK_MAX_IDX ||
cur_max_idx >= pool->cfg.max_idx)
return 0;
/* No enough space in trunk array, resize the trunks array. */
if (trunk_idx == trunk_n) {
n_grow = trunk_idx ? trunk_idx :
RTE_CACHE_LINE_SIZE / sizeof(void *);
cur_max_idx = mlx5_trunk_idx_offset_get(pool, trunk_n + n_grow);
/* Resize the trunk array. */
p = pool_malloc(pool, MLX5_MEM_ZERO, ((trunk_idx + n_grow) *
sizeof(struct mlx5_indexed_trunk *)) +
(cur_max_idx * sizeof(uint32_t)) + sizeof(*p),
RTE_CACHE_LINE_SIZE, rte_socket_id());
if (!p)
return 0;
p->trunks = (struct mlx5_indexed_trunk **)&p->idx[cur_max_idx];
if (lc)
memcpy(p->trunks, lc->trunks, trunk_idx *
sizeof(struct mlx5_indexed_trunk *));
#ifdef RTE_LIBRTE_MLX5_DEBUG
memset(RTE_PTR_ADD(p->trunks, trunk_idx * sizeof(void *)), 0,
n_grow * sizeof(void *));
#endif
p->n_trunk_valid = trunk_idx;
p->n_trunk = trunk_n + n_grow;
p->len = 0;
}
/* Prepare the new trunk. */
trunk_size = sizeof(*trunk);
data_size = mlx5_trunk_size_get(pool, trunk_idx);
trunk_size += RTE_CACHE_LINE_ROUNDUP(data_size * pool->cfg.size);
trunk = pool_malloc(pool, MLX5_MEM_ZERO, trunk_size,
RTE_CACHE_LINE_SIZE, rte_socket_id());
if (unlikely(!trunk)) {
pool->cfg.free(p);
return 0;
}
trunk->idx = trunk_idx;
trunk->free = data_size;
mlx5_ipool_lock(pool);
/*
* Double check if trunks has been updated or have available index.
* During the new trunk allocate, index may still be flushed to the
* global cache. So also need to check the pool->gc->len.
*/
if (pool->gc && (lc != pool->gc ||
lc->n_trunk_valid != trunk_idx ||
pool->gc->len)) {
mlx5_ipool_unlock(pool);
if (p)
pool->cfg.free(p);
pool->cfg.free(trunk);
goto check_again;
}
/* Resize the trunk array and update local cache first. */
if (p) {
if (lc && !(--lc->ref_cnt))
olc = lc;
lc = p;
lc->ref_cnt = 1;
pool->cache[cidx]->lc = lc;
rte_atomic_store_explicit(&pool->gc, p, rte_memory_order_relaxed);
}
/* Add trunk to trunks array. */
lc->trunks[trunk_idx] = trunk;
rte_atomic_fetch_add_explicit(&lc->n_trunk_valid, 1, rte_memory_order_relaxed);
/* Enqueue half of the index to global. */
ts_idx = mlx5_trunk_idx_offset_get(pool, trunk_idx) + 1;
fetch_size = trunk->free >> 1;
if (fetch_size > pool->cfg.per_core_cache)
fetch_size = trunk->free - pool->cfg.per_core_cache;
for (i = 0; i < fetch_size; i++)
lc->idx[i] = ts_idx + i;
lc->len = fetch_size;
mlx5_ipool_unlock(pool);
/* Copy left half - 1 to local cache index array. */
pool->cache[cidx]->len = trunk->free - fetch_size - 1;
ts_idx += fetch_size;
for (i = 0; i < pool->cache[cidx]->len; i++)
pool->cache[cidx]->idx[i] = ts_idx + i;
if (olc)
pool->cfg.free(olc);
return ts_idx + i;
}
static void *
_mlx5_ipool_get_cache(struct mlx5_indexed_pool *pool, int cidx, uint32_t idx)
{
struct mlx5_indexed_trunk *trunk;
struct mlx5_indexed_cache *lc;
uint32_t trunk_idx;
uint32_t entry_idx;
MLX5_ASSERT(idx);
if (unlikely(!pool->cache[cidx])) {
pool->cache[cidx] = pool_malloc(pool, MLX5_MEM_ZERO,
sizeof(struct mlx5_ipool_per_lcore) +
(pool->cfg.per_core_cache * sizeof(uint32_t)),
RTE_CACHE_LINE_SIZE, SOCKET_ID_ANY);
if (!pool->cache[cidx]) {
DRV_LOG(ERR, "Ipool cache%d allocate failed\n", cidx);
return NULL;
}
}
lc = mlx5_ipool_update_global_cache(pool, cidx);
idx -= 1;
trunk_idx = mlx5_trunk_idx_get(pool, idx);
trunk = lc->trunks[trunk_idx];
if (!trunk)
return NULL;
entry_idx = idx - mlx5_trunk_idx_offset_get(pool, trunk_idx);
return &trunk->data[entry_idx * pool->cfg.size];
}
static void *
mlx5_ipool_get_cache(struct mlx5_indexed_pool *pool, uint32_t idx)
{
void *entry;
int cidx;
cidx = rte_lcore_index(rte_lcore_id());
if (unlikely(cidx == -1)) {
cidx = RTE_MAX_LCORE;
rte_spinlock_lock(&pool->lcore_lock);
}
entry = _mlx5_ipool_get_cache(pool, cidx, idx);
if (unlikely(cidx == RTE_MAX_LCORE))
rte_spinlock_unlock(&pool->lcore_lock);
return entry;
}
#ifdef POOL_DEBUG
static void
mlx5_ipool_validate_malloc_cache(struct mlx5_indexed_pool *pool, uint32_t idx)
{
rte_spinlock_lock(&pool->cache_validator.lock);
uint32_t entry_idx = idx - 1;
uint32_t allocated_size = pool->gc->n_trunk_valid *
mlx5_trunk_size_get(pool, pool->n_trunk_valid);
if (!pool->cache_validator.bmp)
mlx5_ipool_grow_bmp(pool, allocated_size);
if (pool->cache_validator.bmp_size < allocated_size)
mlx5_ipool_grow_bmp(pool, allocated_size);
if (rte_bitmap_get(pool->cache_validator.bmp, entry_idx) == 0) {
DRV_LOG_IPOOL(ERR, "lcore id %d: pool %s: detected double malloc idx: %d",
rte_lcore_id(), pool->cfg.type, idx);
MLX5_ASSERT(0);
}
rte_bitmap_clear(pool->cache_validator.bmp, entry_idx);
rte_spinlock_unlock(&pool->cache_validator.lock);
}
static void
mlx5_ipool_validate_free_cache(struct mlx5_indexed_pool *pool, uint32_t idx)
{
rte_spinlock_lock(&pool->cache_validator.lock);
uint32_t entry_idx = idx - 1;
if (!pool->gc || !pool->cache_validator.bmp) {
rte_spinlock_unlock(&pool->cache_validator.lock);
return;
}
if (rte_bitmap_get(pool->cache_validator.bmp, entry_idx) != 0) {
DRV_LOG_IPOOL(ERR, "lcore id %d: pool %s: detected double free of index %d",
rte_lcore_id(), pool->cfg.type, idx);
MLX5_ASSERT(0);
}
rte_bitmap_set(pool->cache_validator.bmp, entry_idx);
rte_spinlock_unlock(&pool->cache_validator.lock);
}
#endif
static void *
_mlx5_ipool_malloc_cache(struct mlx5_indexed_pool *pool, int cidx,
uint32_t *idx)
{
if (unlikely(!pool->cache[cidx])) {
pool->cache[cidx] = pool_malloc(pool, MLX5_MEM_ZERO,
sizeof(struct mlx5_ipool_per_lcore) +
(pool->cfg.per_core_cache * sizeof(uint32_t)),
RTE_CACHE_LINE_SIZE, SOCKET_ID_ANY);
if (!pool->cache[cidx]) {
DRV_LOG(ERR, "Ipool cache%d allocate failed\n", cidx);
return NULL;
}
} else if (pool->cache[cidx]->len) {
pool->cache[cidx]->len--;
*idx = pool->cache[cidx]->idx[pool->cache[cidx]->len];
return _mlx5_ipool_get_cache(pool, cidx, *idx);
}
/* Not enough idx in global cache. Keep fetching from global. */
*idx = mlx5_ipool_allocate_from_global(pool, cidx);
if (unlikely(!(*idx)))
return NULL;
return _mlx5_ipool_get_cache(pool, cidx, *idx);
}
static void *
mlx5_ipool_malloc_cache(struct mlx5_indexed_pool *pool, uint32_t *idx)
{
void *entry;
int cidx;
cidx = rte_lcore_index(rte_lcore_id());
if (unlikely(cidx == -1)) {
cidx = RTE_MAX_LCORE;
rte_spinlock_lock(&pool->lcore_lock);
}
entry = _mlx5_ipool_malloc_cache(pool, cidx, idx);
if (unlikely(cidx == RTE_MAX_LCORE))
rte_spinlock_unlock(&pool->lcore_lock);
#ifdef POOL_DEBUG
++pool->n_entry;
mlx5_ipool_validate_malloc_cache(pool, *idx);
DRV_LOG_IPOOL(DEBUG, "lcore id %d: pool %s: allocated entry %d lcore %d, "
"current cache size %d, total allocated entries %d.", rte_lcore_id(),
pool->cfg.type, *idx, cidx, pool->cache[cidx]->len, pool->n_entry);
#endif
return entry;
}
static void
_mlx5_ipool_free_cache(struct mlx5_indexed_pool *pool, int cidx, uint32_t idx)
{
struct mlx5_ipool_per_lcore *ilc;
struct mlx5_indexed_cache *gc, *olc = NULL;
uint32_t reclaim_num = 0;
MLX5_ASSERT(idx);
#ifdef POOL_DEBUG
mlx5_ipool_validate_free_cache(pool, idx);
#endif
/*
* When index was allocated on core A but freed on core B. In this
* case check if local cache on core B was allocated before.
*/
if (unlikely(!pool->cache[cidx])) {
pool->cache[cidx] = pool_malloc(pool, MLX5_MEM_ZERO,
sizeof(struct mlx5_ipool_per_lcore) +
(pool->cfg.per_core_cache * sizeof(uint32_t)),
RTE_CACHE_LINE_SIZE, SOCKET_ID_ANY);
if (!pool->cache[cidx]) {
DRV_LOG(ERR, "Ipool cache%d allocate failed\n", cidx);
return;
}
}
/* Try to enqueue to local index cache. */
if (pool->cache[cidx]->len < pool->cfg.per_core_cache) {
pool->cache[cidx]->idx[pool->cache[cidx]->len] = idx;
pool->cache[cidx]->len++;
DRV_LOG_IPOOL(DEBUG, "lcore id %d: pool %s: freed entry %d "
"back to lcache %d, lcache size %d.", rte_lcore_id(),
pool->cfg.type, idx, cidx, pool->cache[cidx]->len);
return;
}
ilc = pool->cache[cidx];
reclaim_num = pool->cfg.per_core_cache >> 2;
ilc->len -= reclaim_num;
/* Local index cache full, try with global index cache. */
mlx5_ipool_lock(pool);
gc = pool->gc;
if (ilc->lc != gc) {
if (ilc->lc && !(--ilc->lc->ref_cnt))
olc = ilc->lc;
gc->ref_cnt++;
ilc->lc = gc;
}
memcpy(&gc->idx[gc->len], &ilc->idx[ilc->len],
reclaim_num * sizeof(uint32_t));
gc->len += reclaim_num;
mlx5_ipool_unlock(pool);
if (olc)
pool->cfg.free(olc);
pool->cache[cidx]->idx[pool->cache[cidx]->len] = idx;
pool->cache[cidx]->len++;
DRV_LOG_IPOOL(DEBUG, "lcore id %d: pool %s: cache reclaim, lcache %d, "
"reclaimed: %d, gcache size %d.", rte_lcore_id(), pool->cfg.type,
cidx, reclaim_num, pool->cache[cidx]->len);
}
static void
mlx5_ipool_free_cache(struct mlx5_indexed_pool *pool, uint32_t idx)
{
int cidx;
cidx = rte_lcore_index(rte_lcore_id());
if (unlikely(cidx == -1)) {
cidx = RTE_MAX_LCORE;
rte_spinlock_lock(&pool->lcore_lock);
}
_mlx5_ipool_free_cache(pool, cidx, idx);
if (unlikely(cidx == RTE_MAX_LCORE))
rte_spinlock_unlock(&pool->lcore_lock);
#ifdef POOL_DEBUG
pool->n_entry--;
#endif
}
void *
mlx5_ipool_malloc(struct mlx5_indexed_pool *pool, uint32_t *idx)
{
struct mlx5_indexed_trunk *trunk;
uint64_t slab = 0;
uint32_t iidx = 0;
void *p;
if (pool->cfg.per_core_cache)
return mlx5_ipool_malloc_cache(pool, idx);
mlx5_ipool_lock(pool);
if (pool->free_list == TRUNK_INVALID) {
/* If no available trunks, grow new. */
if (mlx5_ipool_grow(pool)) {
mlx5_ipool_unlock(pool);
return NULL;
}
DRV_LOG_IPOOL(INFO, "lcore id %d: pool %s: add trunk: new size = %d",
rte_lcore_id(), pool->cfg.type, pool->n_trunk_valid);
}
MLX5_ASSERT(pool->free_list != TRUNK_INVALID);
trunk = pool->trunks[pool->free_list];
MLX5_ASSERT(trunk->free);
if (!rte_bitmap_scan(trunk->bmp, &iidx, &slab)) {
mlx5_ipool_unlock(pool);
return NULL;
}
MLX5_ASSERT(slab);
iidx += rte_ctz64(slab);
MLX5_ASSERT(iidx != UINT32_MAX);
MLX5_ASSERT(iidx < mlx5_trunk_size_get(pool, trunk->idx));
rte_bitmap_clear(trunk->bmp, iidx);
p = &trunk->data[iidx * pool->cfg.size];
/*
* The ipool index should grow continually from small to big,
* some features as metering only accept limited bits of index.
* Random index with MSB set may be rejected.
*/
iidx += mlx5_trunk_idx_offset_get(pool, trunk->idx);
iidx += 1; /* non-zero index. */
trunk->free--;
#ifdef POOL_DEBUG
++pool->n_entry;
#endif
if (!trunk->free) {
/* Full trunk will be removed from free list in imalloc. */
MLX5_ASSERT(pool->free_list == trunk->idx);
pool->free_list = trunk->next;
if (trunk->next != TRUNK_INVALID)
pool->trunks[trunk->next]->prev = TRUNK_INVALID;
trunk->prev = TRUNK_INVALID;
trunk->next = TRUNK_INVALID;
#ifdef POOL_DEBUG
pool->trunk_empty++;
pool->trunk_avail--;
#endif
}
*idx = iidx;
mlx5_ipool_unlock(pool);
#ifdef POOL_DEBUG
DRV_LOG_IPOOL(DEBUG, "lcore id %d: pool %s: allocated entry %d trunk_id %d, "
"number of trunks %d, total allocated entries %d", rte_lcore_id(),
pool->cfg.type, *idx, pool->free_list, pool->n_trunk_valid, pool->n_entry);
#endif
return p;
}
void *
mlx5_ipool_zmalloc(struct mlx5_indexed_pool *pool, uint32_t *idx)
{
void *entry = mlx5_ipool_malloc(pool, idx);
if (entry && pool->cfg.size)
memset(entry, 0, pool->cfg.size);
return entry;
}
void
mlx5_ipool_free(struct mlx5_indexed_pool *pool, uint32_t idx)
{
struct mlx5_indexed_trunk *trunk;
uint32_t trunk_idx;
uint32_t entry_idx;
if (!pool || !idx)
return;
if (pool->cfg.per_core_cache) {
mlx5_ipool_free_cache(pool, idx);
return;
}
idx -= 1;
mlx5_ipool_lock(pool);
trunk_idx = mlx5_trunk_idx_get(pool, idx);
if ((!pool->cfg.release_mem_en && trunk_idx >= pool->n_trunk_valid) ||
(pool->cfg.release_mem_en && trunk_idx >= pool->n_trunk))
goto out;
trunk = pool->trunks[trunk_idx];
if (!trunk)
goto out;
entry_idx = idx - mlx5_trunk_idx_offset_get(pool, trunk->idx);
if (trunk_idx != trunk->idx ||
rte_bitmap_get(trunk->bmp, entry_idx))
goto out;
rte_bitmap_set(trunk->bmp, entry_idx);
trunk->free++;
if (pool->cfg.release_mem_en && trunk->free == mlx5_trunk_size_get
(pool, trunk->idx)) {
if (pool->free_list == trunk->idx)
pool->free_list = trunk->next;
if (trunk->next != TRUNK_INVALID)
pool->trunks[trunk->next]->prev = trunk->prev;
if (trunk->prev != TRUNK_INVALID)
pool->trunks[trunk->prev]->next = trunk->next;
pool->cfg.free(trunk);
pool->trunks[trunk_idx] = NULL;
pool->n_trunk_valid--;
#ifdef POOL_DEBUG
pool->trunk_avail--;
pool->trunk_free++;
#endif
if (pool->n_trunk_valid == 0) {
pool->cfg.free(pool->trunks);
pool->trunks = NULL;
pool->n_trunk = 0;
}
} else if (trunk->free == 1) {
/* Put into free trunk list head. */
MLX5_ASSERT(pool->free_list != trunk->idx);
trunk->next = pool->free_list;
trunk->prev = TRUNK_INVALID;
if (pool->free_list != TRUNK_INVALID)
pool->trunks[pool->free_list]->prev = trunk->idx;
pool->free_list = trunk->idx;
#ifdef POOL_DEBUG
pool->trunk_empty--;
pool->trunk_avail++;
#endif
}
#ifdef POOL_DEBUG
pool->n_entry--;
#endif
DRV_LOG_IPOOL(DEBUG, "lcore id %d: pool %s: freed entry %d trunk_id %d",
rte_lcore_id(), pool->cfg.type, entry_idx + 1, trunk_idx);
out:
mlx5_ipool_unlock(pool);
}
void *
mlx5_ipool_get(struct mlx5_indexed_pool *pool, uint32_t idx)
{
struct mlx5_indexed_trunk *trunk;
void *p = NULL;
uint32_t trunk_idx;
uint32_t entry_idx;
if (!idx)
return NULL;
if (pool->cfg.per_core_cache)
return mlx5_ipool_get_cache(pool, idx);
idx -= 1;
mlx5_ipool_lock(pool);
trunk_idx = mlx5_trunk_idx_get(pool, idx);
if ((!pool->cfg.release_mem_en && trunk_idx >= pool->n_trunk_valid) ||
(pool->cfg.release_mem_en && trunk_idx >= pool->n_trunk))
goto out;
trunk = pool->trunks[trunk_idx];
if (!trunk)
goto out;
entry_idx = idx - mlx5_trunk_idx_offset_get(pool, trunk->idx);
if (trunk_idx != trunk->idx ||
rte_bitmap_get(trunk->bmp, entry_idx))
goto out;
p = &trunk->data[entry_idx * pool->cfg.size];
out:
mlx5_ipool_unlock(pool);
return p;
}
int
mlx5_ipool_destroy(struct mlx5_indexed_pool *pool)
{
struct mlx5_indexed_trunk **trunks = NULL;
struct mlx5_indexed_cache *gc = pool->gc;
uint32_t i, n_trunk_valid = 0;
MLX5_ASSERT(pool);
mlx5_ipool_lock(pool);
DRV_LOG_IPOOL(INFO, "lcore id %d: pool %s: destroy", rte_lcore_id(), pool->cfg.type);
if (pool->cfg.per_core_cache) {
for (i = 0; i <= RTE_MAX_LCORE; i++) {
/*
* Free only old global cache. Pool gc will be
* freed at last.
*/
if (pool->cache[i]) {
if (pool->cache[i]->lc &&
pool->cache[i]->lc != pool->gc &&
(!(--pool->cache[i]->lc->ref_cnt)))
pool->cfg.free(pool->cache[i]->lc);
pool->cfg.free(pool->cache[i]);
}
}
if (gc) {
trunks = gc->trunks;
n_trunk_valid = gc->n_trunk_valid;
}
} else {
gc = NULL;
trunks = pool->trunks;
n_trunk_valid = pool->n_trunk_valid;
}
for (i = 0; i < n_trunk_valid; i++) {
if (trunks[i])
pool->cfg.free(trunks[i]);
}
if (!gc && trunks)
pool->cfg.free(trunks);
if (gc)
pool->cfg.free(gc);
#ifdef POOL_DEBUG
if (pool->cache_validator.bmp_mem)
pool->cfg.free(pool->cache_validator.bmp_mem);
#endif
mlx5_ipool_unlock(pool);
mlx5_free(pool);
return 0;
}
void
mlx5_ipool_flush_cache(struct mlx5_indexed_pool *pool)
{
uint32_t i, j;
struct mlx5_indexed_cache *gc;
struct rte_bitmap *ibmp;
uint32_t bmp_num, mem_size;
if (!pool->cfg.per_core_cache)
return;
gc = pool->gc;
if (!gc)
return;
/* Reset bmp. */
bmp_num = mlx5_trunk_idx_offset_get(pool, gc->n_trunk_valid);
mem_size = rte_bitmap_get_memory_footprint(bmp_num);
pool->bmp_mem = pool_malloc(pool, MLX5_MEM_ZERO, mem_size,
RTE_CACHE_LINE_SIZE, rte_socket_id());
if (!pool->bmp_mem) {
DRV_LOG(ERR, "Ipool bitmap mem allocate failed.\n");
return;
}
ibmp = rte_bitmap_init_with_all_set(bmp_num, pool->bmp_mem, mem_size);
if (!ibmp) {
pool->cfg.free(pool->bmp_mem);
pool->bmp_mem = NULL;
DRV_LOG(ERR, "Ipool bitmap create failed.\n");
return;
}
pool->ibmp = ibmp;
/* Clear global cache. */
for (i = 0; i < gc->len; i++)
rte_bitmap_clear(ibmp, gc->idx[i] - 1);
DRV_LOG_IPOOL(INFO, "lcore id %d: pool %s: flush gcache, gcache size = %d",
rte_lcore_id(), pool->cfg.type, gc->len);
/* Clear core cache. */
for (i = 0; i < RTE_MAX_LCORE + 1; i++) {
struct mlx5_ipool_per_lcore *ilc = pool->cache[i];
if (!ilc)
continue;
for (j = 0; j < ilc->len; j++)
rte_bitmap_clear(ibmp, ilc->idx[j] - 1);
DRV_LOG_IPOOL(INFO, "lcore id %d: pool %s: flush lcache %d",
rte_lcore_id(), pool->cfg.type, i);
}
}
static void *
mlx5_ipool_get_next_cache(struct mlx5_indexed_pool *pool, uint32_t *pos)
{
struct rte_bitmap *ibmp;
uint64_t slab = 0;
uint32_t iidx = *pos;
ibmp = pool->ibmp;
if (!ibmp || !rte_bitmap_scan(ibmp, &iidx, &slab)) {
if (pool->bmp_mem) {
pool->cfg.free(pool->bmp_mem);
pool->bmp_mem = NULL;
pool->ibmp = NULL;
}
return NULL;
}
iidx += rte_ctz64(slab);
rte_bitmap_clear(ibmp, iidx);
iidx++;
*pos = iidx;
return mlx5_ipool_get_cache(pool, iidx);
}
void *
mlx5_ipool_get_next(struct mlx5_indexed_pool *pool, uint32_t *pos)
{
uint32_t idx = *pos;
void *entry;
if (pool->cfg.per_core_cache)
return mlx5_ipool_get_next_cache(pool, pos);
while (idx <= mlx5_trunk_idx_offset_get(pool, pool->n_trunk)) {
entry = mlx5_ipool_get(pool, idx);
if (entry) {
*pos = idx;
return entry;
}
idx++;
}
return NULL;
}
int
mlx5_ipool_resize(struct mlx5_indexed_pool *pool, uint32_t num_entries,
struct rte_flow_error *error)
{
if (num_entries == pool->cfg.max_idx)
return 0;
else if (num_entries < pool->cfg.max_idx)
return rte_flow_error_set(error, EINVAL,
RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
NULL, "cannot decrease pool size");
if (num_entries % pool->cfg.trunk_size)
return rte_flow_error_set(error, EINVAL,
RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
NULL, "number of entries in pool must be trunk size multiplication");
if (num_entries >= mlx5_trunk_idx_offset_get(pool, TRUNK_MAX_IDX + 1))
return rte_flow_error_set(error, EINVAL,
RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
NULL, "requested number of entries exceeds pool limit");
mlx5_ipool_lock(pool);
pool->cfg.max_idx = num_entries;
mlx5_ipool_unlock(pool);
DRV_LOG_IPOOL(INFO,
"lcore id %d: pool %s:, resize pool, new entries limit %d",
rte_lcore_id(), pool->cfg.type, pool->cfg.max_idx);
return 0;
}
void
mlx5_ipool_dump(struct mlx5_indexed_pool *pool)
{
printf("Pool %s entry size %u, trunks %u, %d entry per trunk, "
"total: %d\n",
pool->cfg.type, pool->cfg.size, pool->n_trunk_valid,
pool->cfg.trunk_size, pool->n_trunk_valid);
#ifdef POOL_DEBUG
printf("Pool %s entry %u, trunk alloc %u, empty: %u, "
"available %u free %u\n",
pool->cfg.type, pool->n_entry, pool->trunk_new,
pool->trunk_empty, pool->trunk_avail, pool->trunk_free);
#endif
}
struct mlx5_l3t_tbl *
mlx5_l3t_create(enum mlx5_l3t_type type)
{
struct mlx5_l3t_tbl *tbl;
struct mlx5_indexed_pool_config l3t_ip_cfg = {
.trunk_size = 16,
.grow_trunk = 6,
.grow_shift = 1,
.need_lock = 0,
.release_mem_en = 1,
.malloc = mlx5_malloc,
.free = mlx5_free,
};
if (type >= MLX5_L3T_TYPE_MAX) {
rte_errno = EINVAL;
return NULL;
}
tbl = mlx5_malloc(MLX5_MEM_ZERO, sizeof(struct mlx5_l3t_tbl), 1,
SOCKET_ID_ANY);
if (!tbl) {
rte_errno = ENOMEM;
return NULL;
}
tbl->type = type;
switch (type) {
case MLX5_L3T_TYPE_WORD:
l3t_ip_cfg.size = sizeof(struct mlx5_l3t_entry_word);
l3t_ip_cfg.type = "mlx5_l3t_e_tbl_w";
break;
case MLX5_L3T_TYPE_DWORD:
l3t_ip_cfg.size = sizeof(struct mlx5_l3t_entry_dword);
l3t_ip_cfg.type = "mlx5_l3t_e_tbl_dw";
break;
case MLX5_L3T_TYPE_QWORD:
l3t_ip_cfg.size = sizeof(struct mlx5_l3t_entry_qword);
l3t_ip_cfg.type = "mlx5_l3t_e_tbl_qw";
break;
default:
l3t_ip_cfg.size = sizeof(struct mlx5_l3t_entry_ptr);
l3t_ip_cfg.type = "mlx5_l3t_e_tbl_tpr";
break;
}
rte_spinlock_init(&tbl->sl);
tbl->eip = mlx5_ipool_create(&l3t_ip_cfg);
if (!tbl->eip) {
rte_errno = ENOMEM;
mlx5_free(tbl);
tbl = NULL;
}
return tbl;
}
void
mlx5_l3t_destroy(struct mlx5_l3t_tbl *tbl)
{
struct mlx5_l3t_level_tbl *g_tbl, *m_tbl;
uint32_t i, j;
if (!tbl)
return;
g_tbl = tbl->tbl;
if (g_tbl) {
for (i = 0; i < MLX5_L3T_GT_SIZE; i++) {
m_tbl = g_tbl->tbl[i];
if (!m_tbl)
continue;
for (j = 0; j < MLX5_L3T_MT_SIZE; j++) {
if (!m_tbl->tbl[j])
continue;
MLX5_ASSERT(!((struct mlx5_l3t_entry_word *)
m_tbl->tbl[j])->ref_cnt);
mlx5_ipool_free(tbl->eip,
((struct mlx5_l3t_entry_word *)
m_tbl->tbl[j])->idx);
m_tbl->tbl[j] = 0;
if (!(--m_tbl->ref_cnt))
break;
}
MLX5_ASSERT(!m_tbl->ref_cnt);
mlx5_free(g_tbl->tbl[i]);
g_tbl->tbl[i] = 0;
if (!(--g_tbl->ref_cnt))
break;
}
MLX5_ASSERT(!g_tbl->ref_cnt);
mlx5_free(tbl->tbl);
tbl->tbl = 0;
}
mlx5_ipool_destroy(tbl->eip);
mlx5_free(tbl);
}
static int32_t
__l3t_get_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx,
union mlx5_l3t_data *data)
{
struct mlx5_l3t_level_tbl *g_tbl, *m_tbl;
struct mlx5_l3t_entry_word *w_e_tbl;
struct mlx5_l3t_entry_dword *dw_e_tbl;
struct mlx5_l3t_entry_qword *qw_e_tbl;
struct mlx5_l3t_entry_ptr *ptr_e_tbl;
void *e_tbl;
uint32_t entry_idx;
g_tbl = tbl->tbl;
if (!g_tbl)
return -1;
m_tbl = g_tbl->tbl[(idx >> MLX5_L3T_GT_OFFSET) & MLX5_L3T_GT_MASK];
if (!m_tbl)
return -1;
e_tbl = m_tbl->tbl[(idx >> MLX5_L3T_MT_OFFSET) & MLX5_L3T_MT_MASK];
if (!e_tbl)
return -1;
entry_idx = idx & MLX5_L3T_ET_MASK;
switch (tbl->type) {
case MLX5_L3T_TYPE_WORD:
w_e_tbl = (struct mlx5_l3t_entry_word *)e_tbl;
data->word = w_e_tbl->entry[entry_idx].data;
if (w_e_tbl->entry[entry_idx].data)
w_e_tbl->entry[entry_idx].ref_cnt++;
break;
case MLX5_L3T_TYPE_DWORD:
dw_e_tbl = (struct mlx5_l3t_entry_dword *)e_tbl;
data->dword = dw_e_tbl->entry[entry_idx].data;
if (dw_e_tbl->entry[entry_idx].data)
dw_e_tbl->entry[entry_idx].ref_cnt++;
break;
case MLX5_L3T_TYPE_QWORD:
qw_e_tbl = (struct mlx5_l3t_entry_qword *)e_tbl;
data->qword = qw_e_tbl->entry[entry_idx].data;
if (qw_e_tbl->entry[entry_idx].data)
qw_e_tbl->entry[entry_idx].ref_cnt++;
break;
default:
ptr_e_tbl = (struct mlx5_l3t_entry_ptr *)e_tbl;
data->ptr = ptr_e_tbl->entry[entry_idx].data;
if (ptr_e_tbl->entry[entry_idx].data)
ptr_e_tbl->entry[entry_idx].ref_cnt++;
break;
}
return 0;
}
int32_t
mlx5_l3t_get_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx,
union mlx5_l3t_data *data)
{
int ret;
rte_spinlock_lock(&tbl->sl);
ret = __l3t_get_entry(tbl, idx, data);
rte_spinlock_unlock(&tbl->sl);
return ret;
}
int32_t
mlx5_l3t_clear_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx)
{
struct mlx5_l3t_level_tbl *g_tbl, *m_tbl;
struct mlx5_l3t_entry_word *w_e_tbl;
struct mlx5_l3t_entry_dword *dw_e_tbl;
struct mlx5_l3t_entry_qword *qw_e_tbl;
struct mlx5_l3t_entry_ptr *ptr_e_tbl;
void *e_tbl;
uint32_t entry_idx;
uint64_t ref_cnt;
int32_t ret = -1;
rte_spinlock_lock(&tbl->sl);
g_tbl = tbl->tbl;
if (!g_tbl)
goto out;
m_tbl = g_tbl->tbl[(idx >> MLX5_L3T_GT_OFFSET) & MLX5_L3T_GT_MASK];
if (!m_tbl)
goto out;
e_tbl = m_tbl->tbl[(idx >> MLX5_L3T_MT_OFFSET) & MLX5_L3T_MT_MASK];
if (!e_tbl)
goto out;
entry_idx = idx & MLX5_L3T_ET_MASK;
switch (tbl->type) {
case MLX5_L3T_TYPE_WORD:
w_e_tbl = (struct mlx5_l3t_entry_word *)e_tbl;
MLX5_ASSERT(w_e_tbl->entry[entry_idx].ref_cnt);
ret = --w_e_tbl->entry[entry_idx].ref_cnt;
if (ret)
goto out;
w_e_tbl->entry[entry_idx].data = 0;
ref_cnt = --w_e_tbl->ref_cnt;
break;
case MLX5_L3T_TYPE_DWORD:
dw_e_tbl = (struct mlx5_l3t_entry_dword *)e_tbl;
MLX5_ASSERT(dw_e_tbl->entry[entry_idx].ref_cnt);
ret = --dw_e_tbl->entry[entry_idx].ref_cnt;
if (ret)
goto out;
dw_e_tbl->entry[entry_idx].data = 0;
ref_cnt = --dw_e_tbl->ref_cnt;
break;
case MLX5_L3T_TYPE_QWORD:
qw_e_tbl = (struct mlx5_l3t_entry_qword *)e_tbl;
MLX5_ASSERT(qw_e_tbl->entry[entry_idx].ref_cnt);
ret = --qw_e_tbl->entry[entry_idx].ref_cnt;
if (ret)
goto out;
qw_e_tbl->entry[entry_idx].data = 0;
ref_cnt = --qw_e_tbl->ref_cnt;
break;
default:
ptr_e_tbl = (struct mlx5_l3t_entry_ptr *)e_tbl;
MLX5_ASSERT(ptr_e_tbl->entry[entry_idx].ref_cnt);
ret = --ptr_e_tbl->entry[entry_idx].ref_cnt;
if (ret)
goto out;
ptr_e_tbl->entry[entry_idx].data = NULL;
ref_cnt = --ptr_e_tbl->ref_cnt;
break;
}
if (!ref_cnt) {
mlx5_ipool_free(tbl->eip,
((struct mlx5_l3t_entry_word *)e_tbl)->idx);
m_tbl->tbl[(idx >> MLX5_L3T_MT_OFFSET) & MLX5_L3T_MT_MASK] =
NULL;
if (!(--m_tbl->ref_cnt)) {
mlx5_free(m_tbl);
g_tbl->tbl
[(idx >> MLX5_L3T_GT_OFFSET) & MLX5_L3T_GT_MASK] = NULL;
if (!(--g_tbl->ref_cnt)) {
mlx5_free(g_tbl);
tbl->tbl = 0;
}
}
}
out:
rte_spinlock_unlock(&tbl->sl);
return ret;
}
static int32_t
__l3t_set_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx,
union mlx5_l3t_data *data)
{
struct mlx5_l3t_level_tbl *g_tbl, *m_tbl;
struct mlx5_l3t_entry_word *w_e_tbl;
struct mlx5_l3t_entry_dword *dw_e_tbl;
struct mlx5_l3t_entry_qword *qw_e_tbl;
struct mlx5_l3t_entry_ptr *ptr_e_tbl;
void *e_tbl;
uint32_t entry_idx, tbl_idx = 0;
/* Check the global table, create it if empty. */
g_tbl = tbl->tbl;
if (!g_tbl) {
g_tbl = mlx5_malloc(MLX5_MEM_ZERO,
sizeof(struct mlx5_l3t_level_tbl) +
sizeof(void *) * MLX5_L3T_GT_SIZE, 1,
SOCKET_ID_ANY);
if (!g_tbl) {
rte_errno = ENOMEM;
return -1;
}
tbl->tbl = g_tbl;
}
/*
* Check the middle table, create it if empty. Ref_cnt will be
* increased if new sub table created.
*/
m_tbl = g_tbl->tbl[(idx >> MLX5_L3T_GT_OFFSET) & MLX5_L3T_GT_MASK];
if (!m_tbl) {
m_tbl = mlx5_malloc(MLX5_MEM_ZERO,
sizeof(struct mlx5_l3t_level_tbl) +
sizeof(void *) * MLX5_L3T_MT_SIZE, 1,
SOCKET_ID_ANY);
if (!m_tbl) {
rte_errno = ENOMEM;
return -1;
}
g_tbl->tbl[(idx >> MLX5_L3T_GT_OFFSET) & MLX5_L3T_GT_MASK] =
m_tbl;
g_tbl->ref_cnt++;
}
/*
* Check the entry table, create it if empty. Ref_cnt will be
* increased if new sub entry table created.
*/
e_tbl = m_tbl->tbl[(idx >> MLX5_L3T_MT_OFFSET) & MLX5_L3T_MT_MASK];
if (!e_tbl) {
e_tbl = mlx5_ipool_zmalloc(tbl->eip, &tbl_idx);
if (!e_tbl) {
rte_errno = ENOMEM;
return -1;
}
((struct mlx5_l3t_entry_word *)e_tbl)->idx = tbl_idx;
m_tbl->tbl[(idx >> MLX5_L3T_MT_OFFSET) & MLX5_L3T_MT_MASK] =
e_tbl;
m_tbl->ref_cnt++;
}
entry_idx = idx & MLX5_L3T_ET_MASK;
switch (tbl->type) {
case MLX5_L3T_TYPE_WORD:
w_e_tbl = (struct mlx5_l3t_entry_word *)e_tbl;
if (w_e_tbl->entry[entry_idx].data) {
data->word = w_e_tbl->entry[entry_idx].data;
w_e_tbl->entry[entry_idx].ref_cnt++;
rte_errno = EEXIST;
return -1;
}
w_e_tbl->entry[entry_idx].data = data->word;
w_e_tbl->entry[entry_idx].ref_cnt = 1;
w_e_tbl->ref_cnt++;
break;
case MLX5_L3T_TYPE_DWORD:
dw_e_tbl = (struct mlx5_l3t_entry_dword *)e_tbl;
if (dw_e_tbl->entry[entry_idx].data) {
data->dword = dw_e_tbl->entry[entry_idx].data;
dw_e_tbl->entry[entry_idx].ref_cnt++;
rte_errno = EEXIST;
return -1;
}
dw_e_tbl->entry[entry_idx].data = data->dword;
dw_e_tbl->entry[entry_idx].ref_cnt = 1;
dw_e_tbl->ref_cnt++;
break;
case MLX5_L3T_TYPE_QWORD:
qw_e_tbl = (struct mlx5_l3t_entry_qword *)e_tbl;
if (qw_e_tbl->entry[entry_idx].data) {
data->qword = qw_e_tbl->entry[entry_idx].data;
qw_e_tbl->entry[entry_idx].ref_cnt++;
rte_errno = EEXIST;
return -1;
}
qw_e_tbl->entry[entry_idx].data = data->qword;
qw_e_tbl->entry[entry_idx].ref_cnt = 1;
qw_e_tbl->ref_cnt++;
break;
default:
ptr_e_tbl = (struct mlx5_l3t_entry_ptr *)e_tbl;
if (ptr_e_tbl->entry[entry_idx].data) {
data->ptr = ptr_e_tbl->entry[entry_idx].data;
ptr_e_tbl->entry[entry_idx].ref_cnt++;
rte_errno = EEXIST;
return -1;
}
ptr_e_tbl->entry[entry_idx].data = data->ptr;
ptr_e_tbl->entry[entry_idx].ref_cnt = 1;
ptr_e_tbl->ref_cnt++;
break;
}
return 0;
}
int32_t
mlx5_l3t_set_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx,
union mlx5_l3t_data *data)
{
int ret;
rte_spinlock_lock(&tbl->sl);
ret = __l3t_set_entry(tbl, idx, data);
rte_spinlock_unlock(&tbl->sl);
return ret;
}
|