1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2010-2015 Intel Corporation 3 */ 4 5 #include <stdio.h> 6 #include <stdint.h> 7 #include <string.h> 8 #include <stdlib.h> 9 #include <stdarg.h> 10 #include <errno.h> 11 #include <sys/queue.h> 12 13 #include <rte_common.h> 14 #include <rte_malloc.h> 15 #include <rte_cycles.h> 16 #include <rte_random.h> 17 #include <rte_memory.h> 18 #include <rte_eal.h> 19 #include <rte_ip.h> 20 #include <rte_string_fns.h> 21 22 #include "test.h" 23 24 #include <rte_hash.h> 25 #include <rte_fbk_hash.h> 26 #include <rte_jhash.h> 27 #include <rte_hash_crc.h> 28 29 /******************************************************************************* 30 * Hash function performance test configuration section. Each performance test 31 * will be performed HASHTEST_ITERATIONS times. 32 * 33 * The five arrays below control what tests are performed. Every combination 34 * from the array entries is tested. 35 */ 36 static rte_hash_function hashtest_funcs[] = {rte_jhash, rte_hash_crc}; 37 static uint32_t hashtest_initvals[] = {0}; 38 static uint32_t hashtest_key_lens[] = {0, 2, 4, 5, 6, 7, 8, 10, 11, 15, 16, 21, 31, 32, 33, 63, 64}; 39 #define MAX_KEYSIZE 64 40 /******************************************************************************/ 41 #define LOCAL_FBK_HASH_ENTRIES_MAX (1 << 15) 42 43 /* 44 * Check condition and return an error if true. Assumes that "handle" is the 45 * name of the hash structure pointer to be freed. 46 */ 47 #define RETURN_IF_ERROR(cond, str, ...) do { \ 48 if (cond) { \ 49 printf("ERROR line %d: " str "\n", __LINE__, ##__VA_ARGS__); \ 50 if (handle) rte_hash_free(handle); \ 51 return -1; \ 52 } \ 53 } while(0) 54 55 #define RETURN_IF_ERROR_FBK(cond, str, ...) do { \ 56 if (cond) { \ 57 printf("ERROR line %d: " str "\n", __LINE__, ##__VA_ARGS__); \ 58 if (handle) rte_fbk_hash_free(handle); \ 59 return -1; \ 60 } \ 61 } while(0) 62 63 /* 5-tuple key type */ 64 struct flow_key { 65 uint32_t ip_src; 66 uint32_t ip_dst; 67 uint16_t port_src; 68 uint16_t port_dst; 69 uint8_t proto; 70 } __attribute__((packed)); 71 72 int hash_logtype_test; 73 74 /* 75 * Hash function that always returns the same value, to easily test what 76 * happens when a bucket is full. 77 */ 78 static uint32_t pseudo_hash(__attribute__((unused)) const void *keys, 79 __attribute__((unused)) uint32_t key_len, 80 __attribute__((unused)) uint32_t init_val) 81 { 82 return 3; 83 } 84 85 RTE_INIT(test_hash_init_log) 86 { 87 hash_logtype_test = rte_log_register("test.hash"); 88 } 89 90 /* 91 * Print out result of unit test hash operation. 92 */ 93 static void print_key_info(const char *msg, const struct flow_key *key, 94 int32_t pos) 95 { 96 const uint8_t *p = (const uint8_t *)key; 97 unsigned int i; 98 99 rte_log(RTE_LOG_DEBUG, hash_logtype_test, "%s key:0x", msg); 100 for (i = 0; i < sizeof(struct flow_key); i++) 101 rte_log(RTE_LOG_DEBUG, hash_logtype_test, "%02X", p[i]); 102 rte_log(RTE_LOG_DEBUG, hash_logtype_test, " @ pos %d\n", pos); 103 } 104 105 /* Keys used by unit test functions */ 106 static struct flow_key keys[5] = { { 107 .ip_src = RTE_IPV4(0x03, 0x02, 0x01, 0x00), 108 .ip_dst = RTE_IPV4(0x07, 0x06, 0x05, 0x04), 109 .port_src = 0x0908, 110 .port_dst = 0x0b0a, 111 .proto = 0x0c, 112 }, { 113 .ip_src = RTE_IPV4(0x13, 0x12, 0x11, 0x10), 114 .ip_dst = RTE_IPV4(0x17, 0x16, 0x15, 0x14), 115 .port_src = 0x1918, 116 .port_dst = 0x1b1a, 117 .proto = 0x1c, 118 }, { 119 .ip_src = RTE_IPV4(0x23, 0x22, 0x21, 0x20), 120 .ip_dst = RTE_IPV4(0x27, 0x26, 0x25, 0x24), 121 .port_src = 0x2928, 122 .port_dst = 0x2b2a, 123 .proto = 0x2c, 124 }, { 125 .ip_src = RTE_IPV4(0x33, 0x32, 0x31, 0x30), 126 .ip_dst = RTE_IPV4(0x37, 0x36, 0x35, 0x34), 127 .port_src = 0x3938, 128 .port_dst = 0x3b3a, 129 .proto = 0x3c, 130 }, { 131 .ip_src = RTE_IPV4(0x43, 0x42, 0x41, 0x40), 132 .ip_dst = RTE_IPV4(0x47, 0x46, 0x45, 0x44), 133 .port_src = 0x4948, 134 .port_dst = 0x4b4a, 135 .proto = 0x4c, 136 } }; 137 138 /* Parameters used for hash table in unit test functions. Name set later. */ 139 static struct rte_hash_parameters ut_params = { 140 .entries = 64, 141 .key_len = sizeof(struct flow_key), /* 13 */ 142 .hash_func = rte_jhash, 143 .hash_func_init_val = 0, 144 .socket_id = 0, 145 }; 146 147 #define CRC32_ITERATIONS (1U << 10) 148 #define CRC32_DWORDS (1U << 6) 149 /* 150 * Test if all CRC32 implementations yield the same hash value 151 */ 152 static int 153 test_crc32_hash_alg_equiv(void) 154 { 155 uint32_t hash_val; 156 uint32_t init_val; 157 uint64_t data64[CRC32_DWORDS]; 158 unsigned i, j; 159 size_t data_len; 160 161 printf("\n# CRC32 implementations equivalence test\n"); 162 for (i = 0; i < CRC32_ITERATIONS; i++) { 163 /* Randomizing data_len of data set */ 164 data_len = (size_t) ((rte_rand() % sizeof(data64)) + 1); 165 init_val = (uint32_t) rte_rand(); 166 167 /* Fill the data set */ 168 for (j = 0; j < CRC32_DWORDS; j++) 169 data64[j] = rte_rand(); 170 171 /* Calculate software CRC32 */ 172 rte_hash_crc_set_alg(CRC32_SW); 173 hash_val = rte_hash_crc(data64, data_len, init_val); 174 175 /* Check against 4-byte-operand sse4.2 CRC32 if available */ 176 rte_hash_crc_set_alg(CRC32_SSE42); 177 if (hash_val != rte_hash_crc(data64, data_len, init_val)) { 178 printf("Failed checking CRC32_SW against CRC32_SSE42\n"); 179 break; 180 } 181 182 /* Check against 8-byte-operand sse4.2 CRC32 if available */ 183 rte_hash_crc_set_alg(CRC32_SSE42_x64); 184 if (hash_val != rte_hash_crc(data64, data_len, init_val)) { 185 printf("Failed checking CRC32_SW against CRC32_SSE42_x64\n"); 186 break; 187 } 188 189 /* Check against 8-byte-operand ARM64 CRC32 if available */ 190 rte_hash_crc_set_alg(CRC32_ARM64); 191 if (hash_val != rte_hash_crc(data64, data_len, init_val)) { 192 printf("Failed checking CRC32_SW against CRC32_ARM64\n"); 193 break; 194 } 195 } 196 197 /* Resetting to best available algorithm */ 198 rte_hash_crc_set_alg(CRC32_SSE42_x64); 199 200 if (i == CRC32_ITERATIONS) 201 return 0; 202 203 printf("Failed test data (hex, %zu bytes total):\n", data_len); 204 for (j = 0; j < data_len; j++) 205 printf("%02X%c", ((uint8_t *)data64)[j], 206 ((j+1) % 16 == 0 || j == data_len - 1) ? '\n' : ' '); 207 208 return -1; 209 } 210 211 /* 212 * Test a hash function. 213 */ 214 static void run_hash_func_test(rte_hash_function f, uint32_t init_val, 215 uint32_t key_len) 216 { 217 static uint8_t key[MAX_KEYSIZE]; 218 unsigned i; 219 220 221 for (i = 0; i < key_len; i++) 222 key[i] = (uint8_t) rte_rand(); 223 224 /* just to be on the safe side */ 225 if (!f) 226 return; 227 228 f(key, key_len, init_val); 229 } 230 231 /* 232 * Test all hash functions. 233 */ 234 static void run_hash_func_tests(void) 235 { 236 unsigned i, j, k; 237 238 for (i = 0; i < RTE_DIM(hashtest_funcs); i++) { 239 for (j = 0; j < RTE_DIM(hashtest_initvals); j++) { 240 for (k = 0; k < RTE_DIM(hashtest_key_lens); k++) { 241 run_hash_func_test(hashtest_funcs[i], 242 hashtest_initvals[j], 243 hashtest_key_lens[k]); 244 } 245 } 246 } 247 } 248 249 /* 250 * Basic sequence of operations for a single key: 251 * - add 252 * - lookup (hit) 253 * - delete 254 * - lookup (miss) 255 * 256 * Repeat the test case when 'free on delete' is disabled. 257 * - add 258 * - lookup (hit) 259 * - delete 260 * - lookup (miss) 261 * - free 262 */ 263 static int test_add_delete(void) 264 { 265 struct rte_hash *handle; 266 /* test with standard add/lookup/delete functions */ 267 int pos0, expectedPos0; 268 269 ut_params.name = "test1"; 270 handle = rte_hash_create(&ut_params); 271 RETURN_IF_ERROR(handle == NULL, "hash creation failed"); 272 273 pos0 = rte_hash_add_key(handle, &keys[0]); 274 print_key_info("Add", &keys[0], pos0); 275 RETURN_IF_ERROR(pos0 < 0, "failed to add key (pos0=%d)", pos0); 276 expectedPos0 = pos0; 277 278 pos0 = rte_hash_lookup(handle, &keys[0]); 279 print_key_info("Lkp", &keys[0], pos0); 280 RETURN_IF_ERROR(pos0 != expectedPos0, 281 "failed to find key (pos0=%d)", pos0); 282 283 pos0 = rte_hash_del_key(handle, &keys[0]); 284 print_key_info("Del", &keys[0], pos0); 285 RETURN_IF_ERROR(pos0 != expectedPos0, 286 "failed to delete key (pos0=%d)", pos0); 287 288 pos0 = rte_hash_lookup(handle, &keys[0]); 289 print_key_info("Lkp", &keys[0], pos0); 290 RETURN_IF_ERROR(pos0 != -ENOENT, 291 "fail: found key after deleting! (pos0=%d)", pos0); 292 293 rte_hash_free(handle); 294 295 /* repeat test with precomputed hash functions */ 296 hash_sig_t hash_value; 297 int pos1, expectedPos1, delPos1; 298 299 ut_params.extra_flag = RTE_HASH_EXTRA_FLAGS_NO_FREE_ON_DEL; 300 handle = rte_hash_create(&ut_params); 301 RETURN_IF_ERROR(handle == NULL, "hash creation failed"); 302 ut_params.extra_flag = 0; 303 304 hash_value = rte_hash_hash(handle, &keys[0]); 305 pos1 = rte_hash_add_key_with_hash(handle, &keys[0], hash_value); 306 print_key_info("Add", &keys[0], pos1); 307 RETURN_IF_ERROR(pos1 < 0, "failed to add key (pos1=%d)", pos1); 308 expectedPos1 = pos1; 309 310 pos1 = rte_hash_lookup_with_hash(handle, &keys[0], hash_value); 311 print_key_info("Lkp", &keys[0], pos1); 312 RETURN_IF_ERROR(pos1 != expectedPos1, 313 "failed to find key (pos1=%d)", pos1); 314 315 pos1 = rte_hash_del_key_with_hash(handle, &keys[0], hash_value); 316 print_key_info("Del", &keys[0], pos1); 317 RETURN_IF_ERROR(pos1 != expectedPos1, 318 "failed to delete key (pos1=%d)", pos1); 319 delPos1 = pos1; 320 321 pos1 = rte_hash_lookup_with_hash(handle, &keys[0], hash_value); 322 print_key_info("Lkp", &keys[0], pos1); 323 RETURN_IF_ERROR(pos1 != -ENOENT, 324 "fail: found key after deleting! (pos1=%d)", pos1); 325 326 pos1 = rte_hash_free_key_with_position(handle, delPos1); 327 print_key_info("Free", &keys[0], delPos1); 328 RETURN_IF_ERROR(pos1 != 0, 329 "failed to free key (pos1=%d)", delPos1); 330 331 rte_hash_free(handle); 332 333 return 0; 334 } 335 336 /* 337 * Sequence of operations for a single key: 338 * - delete: miss 339 * - add 340 * - lookup: hit 341 * - add: update 342 * - lookup: hit (updated data) 343 * - delete: hit 344 * - delete: miss 345 * - lookup: miss 346 */ 347 static int test_add_update_delete(void) 348 { 349 struct rte_hash *handle; 350 int pos0, expectedPos0; 351 352 ut_params.name = "test2"; 353 handle = rte_hash_create(&ut_params); 354 RETURN_IF_ERROR(handle == NULL, "hash creation failed"); 355 356 pos0 = rte_hash_del_key(handle, &keys[0]); 357 print_key_info("Del", &keys[0], pos0); 358 RETURN_IF_ERROR(pos0 != -ENOENT, 359 "fail: found non-existent key (pos0=%d)", pos0); 360 361 pos0 = rte_hash_add_key(handle, &keys[0]); 362 print_key_info("Add", &keys[0], pos0); 363 RETURN_IF_ERROR(pos0 < 0, "failed to add key (pos0=%d)", pos0); 364 expectedPos0 = pos0; 365 366 pos0 = rte_hash_lookup(handle, &keys[0]); 367 print_key_info("Lkp", &keys[0], pos0); 368 RETURN_IF_ERROR(pos0 != expectedPos0, 369 "failed to find key (pos0=%d)", pos0); 370 371 pos0 = rte_hash_add_key(handle, &keys[0]); 372 print_key_info("Add", &keys[0], pos0); 373 RETURN_IF_ERROR(pos0 != expectedPos0, 374 "failed to re-add key (pos0=%d)", pos0); 375 376 pos0 = rte_hash_lookup(handle, &keys[0]); 377 print_key_info("Lkp", &keys[0], pos0); 378 RETURN_IF_ERROR(pos0 != expectedPos0, 379 "failed to find key (pos0=%d)", pos0); 380 381 pos0 = rte_hash_del_key(handle, &keys[0]); 382 print_key_info("Del", &keys[0], pos0); 383 RETURN_IF_ERROR(pos0 != expectedPos0, 384 "failed to delete key (pos0=%d)", pos0); 385 386 pos0 = rte_hash_del_key(handle, &keys[0]); 387 print_key_info("Del", &keys[0], pos0); 388 RETURN_IF_ERROR(pos0 != -ENOENT, 389 "fail: deleted already deleted key (pos0=%d)", pos0); 390 391 pos0 = rte_hash_lookup(handle, &keys[0]); 392 print_key_info("Lkp", &keys[0], pos0); 393 RETURN_IF_ERROR(pos0 != -ENOENT, 394 "fail: found key after deleting! (pos0=%d)", pos0); 395 396 rte_hash_free(handle); 397 return 0; 398 } 399 400 /* 401 * Sequence of operations for a single key with 'disable free on del' set: 402 * - delete: miss 403 * - add 404 * - lookup: hit 405 * - add: update 406 * - lookup: hit (updated data) 407 * - delete: hit 408 * - delete: miss 409 * - lookup: miss 410 * - free: hit 411 * - lookup: miss 412 */ 413 static int test_add_update_delete_free(void) 414 { 415 struct rte_hash *handle; 416 int pos0, expectedPos0, delPos0, result; 417 418 ut_params.name = "test2"; 419 ut_params.extra_flag = RTE_HASH_EXTRA_FLAGS_NO_FREE_ON_DEL; 420 handle = rte_hash_create(&ut_params); 421 RETURN_IF_ERROR(handle == NULL, "hash creation failed"); 422 ut_params.extra_flag = 0; 423 424 pos0 = rte_hash_del_key(handle, &keys[0]); 425 print_key_info("Del", &keys[0], pos0); 426 RETURN_IF_ERROR(pos0 != -ENOENT, 427 "fail: found non-existent key (pos0=%d)", pos0); 428 429 pos0 = rte_hash_add_key(handle, &keys[0]); 430 print_key_info("Add", &keys[0], pos0); 431 RETURN_IF_ERROR(pos0 < 0, "failed to add key (pos0=%d)", pos0); 432 expectedPos0 = pos0; 433 434 pos0 = rte_hash_lookup(handle, &keys[0]); 435 print_key_info("Lkp", &keys[0], pos0); 436 RETURN_IF_ERROR(pos0 != expectedPos0, 437 "failed to find key (pos0=%d)", pos0); 438 439 pos0 = rte_hash_add_key(handle, &keys[0]); 440 print_key_info("Add", &keys[0], pos0); 441 RETURN_IF_ERROR(pos0 != expectedPos0, 442 "failed to re-add key (pos0=%d)", pos0); 443 444 pos0 = rte_hash_lookup(handle, &keys[0]); 445 print_key_info("Lkp", &keys[0], pos0); 446 RETURN_IF_ERROR(pos0 != expectedPos0, 447 "failed to find key (pos0=%d)", pos0); 448 449 delPos0 = rte_hash_del_key(handle, &keys[0]); 450 print_key_info("Del", &keys[0], delPos0); 451 RETURN_IF_ERROR(delPos0 != expectedPos0, 452 "failed to delete key (pos0=%d)", delPos0); 453 454 pos0 = rte_hash_del_key(handle, &keys[0]); 455 print_key_info("Del", &keys[0], pos0); 456 RETURN_IF_ERROR(pos0 != -ENOENT, 457 "fail: deleted already deleted key (pos0=%d)", pos0); 458 459 pos0 = rte_hash_lookup(handle, &keys[0]); 460 print_key_info("Lkp", &keys[0], pos0); 461 RETURN_IF_ERROR(pos0 != -ENOENT, 462 "fail: found key after deleting! (pos0=%d)", pos0); 463 464 result = rte_hash_free_key_with_position(handle, delPos0); 465 print_key_info("Free", &keys[0], delPos0); 466 RETURN_IF_ERROR(result != 0, 467 "failed to free key (pos1=%d)", delPos0); 468 469 pos0 = rte_hash_lookup(handle, &keys[0]); 470 print_key_info("Lkp", &keys[0], pos0); 471 RETURN_IF_ERROR(pos0 != -ENOENT, 472 "fail: found key after deleting! (pos0=%d)", pos0); 473 474 rte_hash_free(handle); 475 return 0; 476 } 477 478 /* 479 * Sequence of operations for a single key with 'rw concurrency lock free' set: 480 * - add 481 * - delete: hit 482 * - free: hit 483 * Repeat the test case when 'multi writer add' is enabled. 484 * - add 485 * - delete: hit 486 * - free: hit 487 */ 488 static int test_add_delete_free_lf(void) 489 { 490 /* Should match the #define LCORE_CACHE_SIZE value in rte_cuckoo_hash.h */ 491 #define LCORE_CACHE_SIZE 64 492 struct rte_hash *handle; 493 hash_sig_t hash_value; 494 int pos, expectedPos, delPos; 495 uint8_t extra_flag; 496 uint32_t i, ip_src; 497 498 extra_flag = ut_params.extra_flag; 499 ut_params.extra_flag = RTE_HASH_EXTRA_FLAGS_RW_CONCURRENCY_LF; 500 handle = rte_hash_create(&ut_params); 501 RETURN_IF_ERROR(handle == NULL, "hash creation failed"); 502 ut_params.extra_flag = extra_flag; 503 504 /* 505 * The number of iterations is at least the same as the number of slots 506 * rte_hash allocates internally. This is to reveal potential issues of 507 * not freeing keys successfully. 508 */ 509 for (i = 0; i < ut_params.entries + 1; i++) { 510 hash_value = rte_hash_hash(handle, &keys[0]); 511 pos = rte_hash_add_key_with_hash(handle, &keys[0], hash_value); 512 print_key_info("Add", &keys[0], pos); 513 RETURN_IF_ERROR(pos < 0, "failed to add key (pos=%d)", pos); 514 expectedPos = pos; 515 516 pos = rte_hash_del_key_with_hash(handle, &keys[0], hash_value); 517 print_key_info("Del", &keys[0], pos); 518 RETURN_IF_ERROR(pos != expectedPos, 519 "failed to delete key (pos=%d)", pos); 520 delPos = pos; 521 522 pos = rte_hash_free_key_with_position(handle, delPos); 523 print_key_info("Free", &keys[0], delPos); 524 RETURN_IF_ERROR(pos != 0, 525 "failed to free key (pos=%d)", delPos); 526 } 527 528 rte_hash_free(handle); 529 530 extra_flag = ut_params.extra_flag; 531 ut_params.extra_flag = RTE_HASH_EXTRA_FLAGS_RW_CONCURRENCY_LF | 532 RTE_HASH_EXTRA_FLAGS_MULTI_WRITER_ADD; 533 handle = rte_hash_create(&ut_params); 534 RETURN_IF_ERROR(handle == NULL, "hash creation failed"); 535 ut_params.extra_flag = extra_flag; 536 537 ip_src = keys[0].ip_src; 538 /* 539 * The number of iterations is at least the same as the number of slots 540 * rte_hash allocates internally. This is to reveal potential issues of 541 * not freeing keys successfully. 542 */ 543 for (i = 0; i < ut_params.entries + (RTE_MAX_LCORE - 1) * 544 (LCORE_CACHE_SIZE - 1) + 1; i++) { 545 keys[0].ip_src++; 546 hash_value = rte_hash_hash(handle, &keys[0]); 547 pos = rte_hash_add_key_with_hash(handle, &keys[0], hash_value); 548 print_key_info("Add", &keys[0], pos); 549 RETURN_IF_ERROR(pos < 0, "failed to add key (pos=%d)", pos); 550 expectedPos = pos; 551 552 pos = rte_hash_del_key_with_hash(handle, &keys[0], hash_value); 553 print_key_info("Del", &keys[0], pos); 554 RETURN_IF_ERROR(pos != expectedPos, 555 "failed to delete key (pos=%d)", pos); 556 delPos = pos; 557 558 pos = rte_hash_free_key_with_position(handle, delPos); 559 print_key_info("Free", &keys[0], delPos); 560 RETURN_IF_ERROR(pos != 0, 561 "failed to free key (pos=%d)", delPos); 562 } 563 keys[0].ip_src = ip_src; 564 565 rte_hash_free(handle); 566 567 return 0; 568 } 569 570 /* 571 * Sequence of operations for retrieving a key with its position 572 * 573 * - create table 574 * - add key 575 * - get the key with its position: hit 576 * - delete key 577 * - try to get the deleted key: miss 578 * 579 * Repeat the test case when 'free on delete' is disabled. 580 * - create table 581 * - add key 582 * - get the key with its position: hit 583 * - delete key 584 * - try to get the deleted key: hit 585 * - free key 586 * - try to get the deleted key: miss 587 * 588 */ 589 static int test_hash_get_key_with_position(void) 590 { 591 struct rte_hash *handle = NULL; 592 int pos, expectedPos, delPos, result; 593 void *key; 594 595 ut_params.name = "hash_get_key_w_pos"; 596 handle = rte_hash_create(&ut_params); 597 RETURN_IF_ERROR(handle == NULL, "hash creation failed"); 598 599 pos = rte_hash_add_key(handle, &keys[0]); 600 print_key_info("Add", &keys[0], pos); 601 RETURN_IF_ERROR(pos < 0, "failed to add key (pos0=%d)", pos); 602 expectedPos = pos; 603 604 result = rte_hash_get_key_with_position(handle, pos, &key); 605 RETURN_IF_ERROR(result != 0, "error retrieving a key"); 606 607 pos = rte_hash_del_key(handle, &keys[0]); 608 print_key_info("Del", &keys[0], pos); 609 RETURN_IF_ERROR(pos != expectedPos, 610 "failed to delete key (pos0=%d)", pos); 611 612 result = rte_hash_get_key_with_position(handle, pos, &key); 613 RETURN_IF_ERROR(result != -ENOENT, "non valid key retrieved"); 614 615 rte_hash_free(handle); 616 617 ut_params.name = "hash_get_key_w_pos"; 618 ut_params.extra_flag = RTE_HASH_EXTRA_FLAGS_NO_FREE_ON_DEL; 619 handle = rte_hash_create(&ut_params); 620 RETURN_IF_ERROR(handle == NULL, "hash creation failed"); 621 ut_params.extra_flag = 0; 622 623 pos = rte_hash_add_key(handle, &keys[0]); 624 print_key_info("Add", &keys[0], pos); 625 RETURN_IF_ERROR(pos < 0, "failed to add key (pos0=%d)", pos); 626 expectedPos = pos; 627 628 result = rte_hash_get_key_with_position(handle, pos, &key); 629 RETURN_IF_ERROR(result != 0, "error retrieving a key"); 630 631 delPos = rte_hash_del_key(handle, &keys[0]); 632 print_key_info("Del", &keys[0], delPos); 633 RETURN_IF_ERROR(delPos != expectedPos, 634 "failed to delete key (pos0=%d)", delPos); 635 636 result = rte_hash_get_key_with_position(handle, delPos, &key); 637 RETURN_IF_ERROR(result != -ENOENT, "non valid key retrieved"); 638 639 result = rte_hash_free_key_with_position(handle, delPos); 640 print_key_info("Free", &keys[0], delPos); 641 RETURN_IF_ERROR(result != 0, 642 "failed to free key (pos1=%d)", delPos); 643 644 result = rte_hash_get_key_with_position(handle, delPos, &key); 645 RETURN_IF_ERROR(result != -ENOENT, "non valid key retrieved"); 646 647 rte_hash_free(handle); 648 return 0; 649 } 650 651 /* 652 * Sequence of operations for find existing hash table 653 * 654 * - create table 655 * - find existing table: hit 656 * - find non-existing table: miss 657 * 658 */ 659 static int test_hash_find_existing(void) 660 { 661 struct rte_hash *handle = NULL, *result = NULL; 662 663 /* Create hash table. */ 664 ut_params.name = "hash_find_existing"; 665 handle = rte_hash_create(&ut_params); 666 RETURN_IF_ERROR(handle == NULL, "hash creation failed"); 667 668 /* Try to find existing hash table */ 669 result = rte_hash_find_existing("hash_find_existing"); 670 RETURN_IF_ERROR(result != handle, "could not find existing hash table"); 671 672 /* Try to find non-existing hash table */ 673 result = rte_hash_find_existing("hash_find_non_existing"); 674 RETURN_IF_ERROR(!(result == NULL), "found table that shouldn't exist"); 675 676 /* Cleanup. */ 677 rte_hash_free(handle); 678 679 return 0; 680 } 681 682 /* 683 * Sequence of operations for 5 keys 684 * - add keys 685 * - lookup keys: hit 686 * - add keys (update) 687 * - lookup keys: hit (updated data) 688 * - delete keys : hit 689 * - lookup keys: miss 690 */ 691 static int test_five_keys(void) 692 { 693 struct rte_hash *handle; 694 const void *key_array[5] = {0}; 695 int pos[5]; 696 int expected_pos[5]; 697 unsigned i; 698 int ret; 699 700 ut_params.name = "test3"; 701 handle = rte_hash_create(&ut_params); 702 RETURN_IF_ERROR(handle == NULL, "hash creation failed"); 703 704 /* Add */ 705 for (i = 0; i < 5; i++) { 706 pos[i] = rte_hash_add_key(handle, &keys[i]); 707 print_key_info("Add", &keys[i], pos[i]); 708 RETURN_IF_ERROR(pos[i] < 0, 709 "failed to add key (pos[%u]=%d)", i, pos[i]); 710 expected_pos[i] = pos[i]; 711 } 712 713 /* Lookup */ 714 for(i = 0; i < 5; i++) 715 key_array[i] = &keys[i]; 716 717 ret = rte_hash_lookup_bulk(handle, &key_array[0], 5, (int32_t *)pos); 718 if(ret == 0) 719 for(i = 0; i < 5; i++) { 720 print_key_info("Lkp", key_array[i], pos[i]); 721 RETURN_IF_ERROR(pos[i] != expected_pos[i], 722 "failed to find key (pos[%u]=%d)", i, pos[i]); 723 } 724 725 /* Add - update */ 726 for (i = 0; i < 5; i++) { 727 pos[i] = rte_hash_add_key(handle, &keys[i]); 728 print_key_info("Add", &keys[i], pos[i]); 729 RETURN_IF_ERROR(pos[i] != expected_pos[i], 730 "failed to add key (pos[%u]=%d)", i, pos[i]); 731 } 732 733 /* Lookup */ 734 for (i = 0; i < 5; i++) { 735 pos[i] = rte_hash_lookup(handle, &keys[i]); 736 print_key_info("Lkp", &keys[i], pos[i]); 737 RETURN_IF_ERROR(pos[i] != expected_pos[i], 738 "failed to find key (pos[%u]=%d)", i, pos[i]); 739 } 740 741 /* Delete */ 742 for (i = 0; i < 5; i++) { 743 pos[i] = rte_hash_del_key(handle, &keys[i]); 744 print_key_info("Del", &keys[i], pos[i]); 745 RETURN_IF_ERROR(pos[i] != expected_pos[i], 746 "failed to delete key (pos[%u]=%d)", i, pos[i]); 747 } 748 749 /* Lookup */ 750 for (i = 0; i < 5; i++) { 751 pos[i] = rte_hash_lookup(handle, &keys[i]); 752 print_key_info("Lkp", &keys[i], pos[i]); 753 RETURN_IF_ERROR(pos[i] != -ENOENT, 754 "found non-existent key (pos[%u]=%d)", i, pos[i]); 755 } 756 757 /* Lookup multi */ 758 ret = rte_hash_lookup_bulk(handle, &key_array[0], 5, (int32_t *)pos); 759 if (ret == 0) 760 for (i = 0; i < 5; i++) { 761 print_key_info("Lkp", key_array[i], pos[i]); 762 RETURN_IF_ERROR(pos[i] != -ENOENT, 763 "found not-existent key (pos[%u]=%d)", i, pos[i]); 764 } 765 766 rte_hash_free(handle); 767 768 return 0; 769 } 770 771 /* 772 * Add keys to the same bucket until bucket full. 773 * - add 5 keys to the same bucket (hash created with 4 keys per bucket): 774 * first 4 successful, 5th successful, pushing existing item in bucket 775 * - lookup the 5 keys: 5 hits 776 * - add the 5 keys again: 5 OK 777 * - lookup the 5 keys: 5 hits (updated data) 778 * - delete the 5 keys: 5 OK 779 * - lookup the 5 keys: 5 misses 780 */ 781 static int test_full_bucket(void) 782 { 783 struct rte_hash_parameters params_pseudo_hash = { 784 .name = "test4", 785 .entries = 64, 786 .key_len = sizeof(struct flow_key), /* 13 */ 787 .hash_func = pseudo_hash, 788 .hash_func_init_val = 0, 789 .socket_id = 0, 790 }; 791 struct rte_hash *handle; 792 int pos[5]; 793 int expected_pos[5]; 794 unsigned i; 795 796 handle = rte_hash_create(¶ms_pseudo_hash); 797 RETURN_IF_ERROR(handle == NULL, "hash creation failed"); 798 799 /* Fill bucket */ 800 for (i = 0; i < 4; i++) { 801 pos[i] = rte_hash_add_key(handle, &keys[i]); 802 print_key_info("Add", &keys[i], pos[i]); 803 RETURN_IF_ERROR(pos[i] < 0, 804 "failed to add key (pos[%u]=%d)", i, pos[i]); 805 expected_pos[i] = pos[i]; 806 } 807 /* 808 * This should work and will push one of the items 809 * in the bucket because it is full 810 */ 811 pos[4] = rte_hash_add_key(handle, &keys[4]); 812 print_key_info("Add", &keys[4], pos[4]); 813 RETURN_IF_ERROR(pos[4] < 0, 814 "failed to add key (pos[4]=%d)", pos[4]); 815 expected_pos[4] = pos[4]; 816 817 /* Lookup */ 818 for (i = 0; i < 5; i++) { 819 pos[i] = rte_hash_lookup(handle, &keys[i]); 820 print_key_info("Lkp", &keys[i], pos[i]); 821 RETURN_IF_ERROR(pos[i] != expected_pos[i], 822 "failed to find key (pos[%u]=%d)", i, pos[i]); 823 } 824 825 /* Add - update */ 826 for (i = 0; i < 5; i++) { 827 pos[i] = rte_hash_add_key(handle, &keys[i]); 828 print_key_info("Add", &keys[i], pos[i]); 829 RETURN_IF_ERROR(pos[i] != expected_pos[i], 830 "failed to add key (pos[%u]=%d)", i, pos[i]); 831 } 832 833 /* Lookup */ 834 for (i = 0; i < 5; i++) { 835 pos[i] = rte_hash_lookup(handle, &keys[i]); 836 print_key_info("Lkp", &keys[i], pos[i]); 837 RETURN_IF_ERROR(pos[i] != expected_pos[i], 838 "failed to find key (pos[%u]=%d)", i, pos[i]); 839 } 840 841 /* Delete 1 key, check other keys are still found */ 842 pos[1] = rte_hash_del_key(handle, &keys[1]); 843 print_key_info("Del", &keys[1], pos[1]); 844 RETURN_IF_ERROR(pos[1] != expected_pos[1], 845 "failed to delete key (pos[1]=%d)", pos[1]); 846 pos[3] = rte_hash_lookup(handle, &keys[3]); 847 print_key_info("Lkp", &keys[3], pos[3]); 848 RETURN_IF_ERROR(pos[3] != expected_pos[3], 849 "failed lookup after deleting key from same bucket " 850 "(pos[3]=%d)", pos[3]); 851 852 /* Go back to previous state */ 853 pos[1] = rte_hash_add_key(handle, &keys[1]); 854 print_key_info("Add", &keys[1], pos[1]); 855 expected_pos[1] = pos[1]; 856 RETURN_IF_ERROR(pos[1] < 0, "failed to add key (pos[1]=%d)", pos[1]); 857 858 /* Delete */ 859 for (i = 0; i < 5; i++) { 860 pos[i] = rte_hash_del_key(handle, &keys[i]); 861 print_key_info("Del", &keys[i], pos[i]); 862 RETURN_IF_ERROR(pos[i] != expected_pos[i], 863 "failed to delete key (pos[%u]=%d)", i, pos[i]); 864 } 865 866 /* Lookup */ 867 for (i = 0; i < 5; i++) { 868 pos[i] = rte_hash_lookup(handle, &keys[i]); 869 print_key_info("Lkp", &keys[i], pos[i]); 870 RETURN_IF_ERROR(pos[i] != -ENOENT, 871 "fail: found non-existent key (pos[%u]=%d)", i, pos[i]); 872 } 873 874 rte_hash_free(handle); 875 876 /* Cover the NULL case. */ 877 rte_hash_free(0); 878 return 0; 879 } 880 881 /* 882 * Similar to the test above (full bucket test), but for extendable buckets. 883 */ 884 static int test_extendable_bucket(void) 885 { 886 struct rte_hash_parameters params_pseudo_hash = { 887 .name = "test5", 888 .entries = 64, 889 .key_len = sizeof(struct flow_key), /* 13 */ 890 .hash_func = pseudo_hash, 891 .hash_func_init_val = 0, 892 .socket_id = 0, 893 .extra_flag = RTE_HASH_EXTRA_FLAGS_EXT_TABLE 894 }; 895 struct rte_hash *handle; 896 int pos[64]; 897 int expected_pos[64]; 898 unsigned int i; 899 struct flow_key rand_keys[64]; 900 901 for (i = 0; i < 64; i++) { 902 rand_keys[i].port_dst = i; 903 rand_keys[i].port_src = i+1; 904 } 905 906 handle = rte_hash_create(¶ms_pseudo_hash); 907 RETURN_IF_ERROR(handle == NULL, "hash creation failed"); 908 909 /* Fill bucket */ 910 for (i = 0; i < 64; i++) { 911 pos[i] = rte_hash_add_key(handle, &rand_keys[i]); 912 print_key_info("Add", &rand_keys[i], pos[i]); 913 RETURN_IF_ERROR(pos[i] < 0, 914 "failed to add key (pos[%u]=%d)", i, pos[i]); 915 expected_pos[i] = pos[i]; 916 } 917 918 /* Lookup */ 919 for (i = 0; i < 64; i++) { 920 pos[i] = rte_hash_lookup(handle, &rand_keys[i]); 921 print_key_info("Lkp", &rand_keys[i], pos[i]); 922 RETURN_IF_ERROR(pos[i] != expected_pos[i], 923 "failed to find key (pos[%u]=%d)", i, pos[i]); 924 } 925 926 /* Add - update */ 927 for (i = 0; i < 64; i++) { 928 pos[i] = rte_hash_add_key(handle, &rand_keys[i]); 929 print_key_info("Add", &rand_keys[i], pos[i]); 930 RETURN_IF_ERROR(pos[i] != expected_pos[i], 931 "failed to add key (pos[%u]=%d)", i, pos[i]); 932 } 933 934 /* Lookup */ 935 for (i = 0; i < 64; i++) { 936 pos[i] = rte_hash_lookup(handle, &rand_keys[i]); 937 print_key_info("Lkp", &rand_keys[i], pos[i]); 938 RETURN_IF_ERROR(pos[i] != expected_pos[i], 939 "failed to find key (pos[%u]=%d)", i, pos[i]); 940 } 941 942 /* Delete 1 key, check other keys are still found */ 943 pos[35] = rte_hash_del_key(handle, &rand_keys[35]); 944 print_key_info("Del", &rand_keys[35], pos[35]); 945 RETURN_IF_ERROR(pos[35] != expected_pos[35], 946 "failed to delete key (pos[1]=%d)", pos[35]); 947 pos[20] = rte_hash_lookup(handle, &rand_keys[20]); 948 print_key_info("Lkp", &rand_keys[20], pos[20]); 949 RETURN_IF_ERROR(pos[20] != expected_pos[20], 950 "failed lookup after deleting key from same bucket " 951 "(pos[20]=%d)", pos[20]); 952 953 /* Go back to previous state */ 954 pos[35] = rte_hash_add_key(handle, &rand_keys[35]); 955 print_key_info("Add", &rand_keys[35], pos[35]); 956 expected_pos[35] = pos[35]; 957 RETURN_IF_ERROR(pos[35] < 0, "failed to add key (pos[1]=%d)", pos[35]); 958 959 /* Delete */ 960 for (i = 0; i < 64; i++) { 961 pos[i] = rte_hash_del_key(handle, &rand_keys[i]); 962 print_key_info("Del", &rand_keys[i], pos[i]); 963 RETURN_IF_ERROR(pos[i] != expected_pos[i], 964 "failed to delete key (pos[%u]=%d)", i, pos[i]); 965 } 966 967 /* Lookup */ 968 for (i = 0; i < 64; i++) { 969 pos[i] = rte_hash_lookup(handle, &rand_keys[i]); 970 print_key_info("Lkp", &rand_keys[i], pos[i]); 971 RETURN_IF_ERROR(pos[i] != -ENOENT, 972 "fail: found non-existent key (pos[%u]=%d)", i, pos[i]); 973 } 974 975 /* Add again */ 976 for (i = 0; i < 64; i++) { 977 pos[i] = rte_hash_add_key(handle, &rand_keys[i]); 978 print_key_info("Add", &rand_keys[i], pos[i]); 979 RETURN_IF_ERROR(pos[i] < 0, 980 "failed to add key (pos[%u]=%d)", i, pos[i]); 981 expected_pos[i] = pos[i]; 982 } 983 984 rte_hash_free(handle); 985 986 /* Cover the NULL case. */ 987 rte_hash_free(0); 988 return 0; 989 } 990 991 /******************************************************************************/ 992 static int 993 fbk_hash_unit_test(void) 994 { 995 struct rte_fbk_hash_params params = { 996 .name = "fbk_hash_test", 997 .entries = LOCAL_FBK_HASH_ENTRIES_MAX, 998 .entries_per_bucket = 4, 999 .socket_id = 0, 1000 }; 1001 1002 struct rte_fbk_hash_params invalid_params_1 = { 1003 .name = "invalid_1", 1004 .entries = LOCAL_FBK_HASH_ENTRIES_MAX + 1, /* Not power of 2 */ 1005 .entries_per_bucket = 4, 1006 .socket_id = 0, 1007 }; 1008 1009 struct rte_fbk_hash_params invalid_params_2 = { 1010 .name = "invalid_2", 1011 .entries = 4, 1012 .entries_per_bucket = 3, /* Not power of 2 */ 1013 .socket_id = 0, 1014 }; 1015 1016 struct rte_fbk_hash_params invalid_params_3 = { 1017 .name = "invalid_3", 1018 .entries = 0, /* Entries is 0 */ 1019 .entries_per_bucket = 4, 1020 .socket_id = 0, 1021 }; 1022 1023 struct rte_fbk_hash_params invalid_params_4 = { 1024 .name = "invalid_4", 1025 .entries = LOCAL_FBK_HASH_ENTRIES_MAX, 1026 .entries_per_bucket = 0, /* Entries per bucket is 0 */ 1027 .socket_id = 0, 1028 }; 1029 1030 struct rte_fbk_hash_params invalid_params_5 = { 1031 .name = "invalid_5", 1032 .entries = 4, 1033 .entries_per_bucket = 8, /* Entries per bucket > entries */ 1034 .socket_id = 0, 1035 }; 1036 1037 struct rte_fbk_hash_params invalid_params_6 = { 1038 .name = "invalid_6", 1039 .entries = RTE_FBK_HASH_ENTRIES_MAX * 2, /* Entries > max allowed */ 1040 .entries_per_bucket = 4, 1041 .socket_id = 0, 1042 }; 1043 1044 struct rte_fbk_hash_params invalid_params_7 = { 1045 .name = "invalid_7", 1046 .entries = RTE_FBK_HASH_ENTRIES_MAX, 1047 .entries_per_bucket = RTE_FBK_HASH_ENTRIES_PER_BUCKET_MAX * 2, /* Entries > max allowed */ 1048 .socket_id = 0, 1049 }; 1050 1051 struct rte_fbk_hash_params invalid_params_8 = { 1052 .name = "invalid_7", 1053 .entries = RTE_FBK_HASH_ENTRIES_MAX, 1054 .entries_per_bucket = 4, 1055 .socket_id = RTE_MAX_NUMA_NODES + 1, /* invalid socket */ 1056 }; 1057 1058 /* try to create two hashes with identical names 1059 * in this case, trying to create a second one will not 1060 * fail but will simply return pointer to the existing 1061 * hash with that name. sort of like a "find hash by name" :-) 1062 */ 1063 struct rte_fbk_hash_params invalid_params_same_name_1 = { 1064 .name = "same_name", /* hash with identical name */ 1065 .entries = 4, 1066 .entries_per_bucket = 2, 1067 .socket_id = 0, 1068 }; 1069 1070 /* trying to create this hash should return a pointer to an existing hash */ 1071 struct rte_fbk_hash_params invalid_params_same_name_2 = { 1072 .name = "same_name", /* hash with identical name */ 1073 .entries = RTE_FBK_HASH_ENTRIES_MAX, 1074 .entries_per_bucket = 4, 1075 .socket_id = 0, 1076 }; 1077 1078 /* this is a sanity check for "same name" test 1079 * creating this hash will check if we are actually able to create 1080 * multiple hashes with different names (instead of having just one). 1081 */ 1082 struct rte_fbk_hash_params different_name = { 1083 .name = "different_name", /* different name */ 1084 .entries = LOCAL_FBK_HASH_ENTRIES_MAX, 1085 .entries_per_bucket = 4, 1086 .socket_id = 0, 1087 }; 1088 1089 struct rte_fbk_hash_params params_jhash = { 1090 .name = "valid", 1091 .entries = LOCAL_FBK_HASH_ENTRIES_MAX, 1092 .entries_per_bucket = 4, 1093 .socket_id = 0, 1094 .hash_func = rte_jhash_1word, /* Tests for different hash_func */ 1095 .init_val = RTE_FBK_HASH_INIT_VAL_DEFAULT, 1096 }; 1097 1098 struct rte_fbk_hash_params params_nohash = { 1099 .name = "valid nohash", 1100 .entries = LOCAL_FBK_HASH_ENTRIES_MAX, 1101 .entries_per_bucket = 4, 1102 .socket_id = 0, 1103 .hash_func = NULL, /* Tests for null hash_func */ 1104 .init_val = RTE_FBK_HASH_INIT_VAL_DEFAULT, 1105 }; 1106 1107 struct rte_fbk_hash_table *handle, *tmp; 1108 uint32_t keys[5] = 1109 {0xc6e18639, 0xe67c201c, 0xd4c8cffd, 0x44728691, 0xd5430fa9}; 1110 uint16_t vals[5] = {28108, 5699, 38490, 2166, 61571}; 1111 int status; 1112 unsigned i; 1113 double used_entries; 1114 1115 /* Try creating hashes with invalid parameters */ 1116 printf("# Testing hash creation with invalid parameters " 1117 "- expect error msgs\n"); 1118 handle = rte_fbk_hash_create(&invalid_params_1); 1119 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed"); 1120 1121 handle = rte_fbk_hash_create(&invalid_params_2); 1122 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed"); 1123 1124 handle = rte_fbk_hash_create(&invalid_params_3); 1125 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed"); 1126 1127 handle = rte_fbk_hash_create(&invalid_params_4); 1128 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed"); 1129 1130 handle = rte_fbk_hash_create(&invalid_params_5); 1131 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed"); 1132 1133 handle = rte_fbk_hash_create(&invalid_params_6); 1134 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed"); 1135 1136 handle = rte_fbk_hash_create(&invalid_params_7); 1137 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed"); 1138 1139 handle = rte_fbk_hash_create(&invalid_params_8); 1140 RETURN_IF_ERROR_FBK(handle != NULL, "fbk hash creation should have failed"); 1141 1142 handle = rte_fbk_hash_create(&invalid_params_same_name_1); 1143 RETURN_IF_ERROR_FBK(handle == NULL, "fbk hash creation should have succeeded"); 1144 1145 tmp = rte_fbk_hash_create(&invalid_params_same_name_2); 1146 if (tmp != NULL) 1147 rte_fbk_hash_free(tmp); 1148 RETURN_IF_ERROR_FBK(tmp != NULL, "fbk hash creation should have failed"); 1149 1150 /* we are not freeing handle here because we need a hash list 1151 * to be not empty for the next test */ 1152 1153 /* create a hash in non-empty list - good for coverage */ 1154 tmp = rte_fbk_hash_create(&different_name); 1155 RETURN_IF_ERROR_FBK(tmp == NULL, "fbk hash creation should have succeeded"); 1156 1157 /* free both hashes */ 1158 rte_fbk_hash_free(handle); 1159 rte_fbk_hash_free(tmp); 1160 1161 /* Create empty jhash hash. */ 1162 handle = rte_fbk_hash_create(¶ms_jhash); 1163 RETURN_IF_ERROR_FBK(handle == NULL, "fbk jhash hash creation failed"); 1164 1165 /* Cleanup. */ 1166 rte_fbk_hash_free(handle); 1167 1168 /* Create empty jhash hash. */ 1169 handle = rte_fbk_hash_create(¶ms_nohash); 1170 RETURN_IF_ERROR_FBK(handle == NULL, "fbk nohash hash creation failed"); 1171 1172 /* Cleanup. */ 1173 rte_fbk_hash_free(handle); 1174 1175 /* Create empty hash. */ 1176 handle = rte_fbk_hash_create(¶ms); 1177 RETURN_IF_ERROR_FBK(handle == NULL, "fbk hash creation failed"); 1178 1179 used_entries = rte_fbk_hash_get_load_factor(handle) * LOCAL_FBK_HASH_ENTRIES_MAX; 1180 RETURN_IF_ERROR_FBK((unsigned)used_entries != 0, \ 1181 "load factor right after creation is not zero but it should be"); 1182 /* Add keys. */ 1183 for (i = 0; i < 5; i++) { 1184 status = rte_fbk_hash_add_key(handle, keys[i], vals[i]); 1185 RETURN_IF_ERROR_FBK(status != 0, "fbk hash add failed"); 1186 } 1187 1188 used_entries = rte_fbk_hash_get_load_factor(handle) * LOCAL_FBK_HASH_ENTRIES_MAX; 1189 RETURN_IF_ERROR_FBK((unsigned)used_entries != (unsigned)((((double)5)/LOCAL_FBK_HASH_ENTRIES_MAX)*LOCAL_FBK_HASH_ENTRIES_MAX), \ 1190 "load factor now is not as expected"); 1191 /* Find value of added keys. */ 1192 for (i = 0; i < 5; i++) { 1193 status = rte_fbk_hash_lookup(handle, keys[i]); 1194 RETURN_IF_ERROR_FBK(status != vals[i], 1195 "fbk hash lookup failed"); 1196 } 1197 1198 /* Change value of added keys. */ 1199 for (i = 0; i < 5; i++) { 1200 status = rte_fbk_hash_add_key(handle, keys[i], vals[4 - i]); 1201 RETURN_IF_ERROR_FBK(status != 0, "fbk hash update failed"); 1202 } 1203 1204 /* Find new values. */ 1205 for (i = 0; i < 5; i++) { 1206 status = rte_fbk_hash_lookup(handle, keys[i]); 1207 RETURN_IF_ERROR_FBK(status != vals[4-i], 1208 "fbk hash lookup failed"); 1209 } 1210 1211 /* Delete keys individually. */ 1212 for (i = 0; i < 5; i++) { 1213 status = rte_fbk_hash_delete_key(handle, keys[i]); 1214 RETURN_IF_ERROR_FBK(status != 0, "fbk hash delete failed"); 1215 } 1216 1217 used_entries = rte_fbk_hash_get_load_factor(handle) * LOCAL_FBK_HASH_ENTRIES_MAX; 1218 RETURN_IF_ERROR_FBK((unsigned)used_entries != 0, \ 1219 "load factor right after deletion is not zero but it should be"); 1220 /* Lookup should now fail. */ 1221 for (i = 0; i < 5; i++) { 1222 status = rte_fbk_hash_lookup(handle, keys[i]); 1223 RETURN_IF_ERROR_FBK(status == 0, 1224 "fbk hash lookup should have failed"); 1225 } 1226 1227 /* Add keys again. */ 1228 for (i = 0; i < 5; i++) { 1229 status = rte_fbk_hash_add_key(handle, keys[i], vals[i]); 1230 RETURN_IF_ERROR_FBK(status != 0, "fbk hash add failed"); 1231 } 1232 1233 /* Make sure they were added. */ 1234 for (i = 0; i < 5; i++) { 1235 status = rte_fbk_hash_lookup(handle, keys[i]); 1236 RETURN_IF_ERROR_FBK(status != vals[i], 1237 "fbk hash lookup failed"); 1238 } 1239 1240 /* Clear all entries. */ 1241 rte_fbk_hash_clear_all(handle); 1242 1243 /* Lookup should fail. */ 1244 for (i = 0; i < 5; i++) { 1245 status = rte_fbk_hash_lookup(handle, keys[i]); 1246 RETURN_IF_ERROR_FBK(status == 0, 1247 "fbk hash lookup should have failed"); 1248 } 1249 1250 /* coverage */ 1251 1252 /* fill up the hash_table */ 1253 for (i = 0; i < RTE_FBK_HASH_ENTRIES_MAX + 1; i++) 1254 rte_fbk_hash_add_key(handle, i, (uint16_t) i); 1255 1256 /* Find non-existent key in a full hashtable */ 1257 status = rte_fbk_hash_lookup(handle, RTE_FBK_HASH_ENTRIES_MAX + 1); 1258 RETURN_IF_ERROR_FBK(status != -ENOENT, 1259 "fbk hash lookup succeeded"); 1260 1261 /* Delete non-existent key in a full hashtable */ 1262 status = rte_fbk_hash_delete_key(handle, RTE_FBK_HASH_ENTRIES_MAX + 1); 1263 RETURN_IF_ERROR_FBK(status != -ENOENT, 1264 "fbk hash delete succeeded"); 1265 1266 /* Delete one key from a full hashtable */ 1267 status = rte_fbk_hash_delete_key(handle, 1); 1268 RETURN_IF_ERROR_FBK(status != 0, 1269 "fbk hash delete failed"); 1270 1271 /* Clear all entries. */ 1272 rte_fbk_hash_clear_all(handle); 1273 1274 /* Cleanup. */ 1275 rte_fbk_hash_free(handle); 1276 1277 /* Cover the NULL case. */ 1278 rte_fbk_hash_free(0); 1279 1280 return 0; 1281 } 1282 1283 /* 1284 * Sequence of operations for find existing fbk hash table 1285 * 1286 * - create table 1287 * - find existing table: hit 1288 * - find non-existing table: miss 1289 * 1290 */ 1291 static int test_fbk_hash_find_existing(void) 1292 { 1293 struct rte_fbk_hash_params params = { 1294 .name = "fbk_hash_find_existing", 1295 .entries = LOCAL_FBK_HASH_ENTRIES_MAX, 1296 .entries_per_bucket = 4, 1297 .socket_id = 0, 1298 }; 1299 struct rte_fbk_hash_table *handle = NULL, *result = NULL; 1300 1301 /* Create hash table. */ 1302 handle = rte_fbk_hash_create(¶ms); 1303 RETURN_IF_ERROR_FBK(handle == NULL, "fbk hash creation failed"); 1304 1305 /* Try to find existing fbk hash table */ 1306 result = rte_fbk_hash_find_existing("fbk_hash_find_existing"); 1307 RETURN_IF_ERROR_FBK(result != handle, "could not find existing fbk hash table"); 1308 1309 /* Try to find non-existing fbk hash table */ 1310 result = rte_fbk_hash_find_existing("fbk_hash_find_non_existing"); 1311 RETURN_IF_ERROR_FBK(!(result == NULL), "found fbk table that shouldn't exist"); 1312 1313 /* Cleanup. */ 1314 rte_fbk_hash_free(handle); 1315 1316 return 0; 1317 } 1318 1319 #define BUCKET_ENTRIES 4 1320 /* 1321 * Do tests for hash creation with bad parameters. 1322 */ 1323 static int test_hash_creation_with_bad_parameters(void) 1324 { 1325 struct rte_hash *handle, *tmp; 1326 struct rte_hash_parameters params; 1327 1328 handle = rte_hash_create(NULL); 1329 if (handle != NULL) { 1330 rte_hash_free(handle); 1331 printf("Impossible creating hash successfully without any parameter\n"); 1332 return -1; 1333 } 1334 1335 memcpy(¶ms, &ut_params, sizeof(params)); 1336 params.name = "creation_with_bad_parameters_0"; 1337 params.entries = RTE_HASH_ENTRIES_MAX + 1; 1338 handle = rte_hash_create(¶ms); 1339 if (handle != NULL) { 1340 rte_hash_free(handle); 1341 printf("Impossible creating hash successfully with entries in parameter exceeded\n"); 1342 return -1; 1343 } 1344 1345 memcpy(¶ms, &ut_params, sizeof(params)); 1346 params.name = "creation_with_bad_parameters_2"; 1347 params.entries = BUCKET_ENTRIES - 1; 1348 handle = rte_hash_create(¶ms); 1349 if (handle != NULL) { 1350 rte_hash_free(handle); 1351 printf("Impossible creating hash successfully if entries less than bucket_entries in parameter\n"); 1352 return -1; 1353 } 1354 1355 memcpy(¶ms, &ut_params, sizeof(params)); 1356 params.name = "creation_with_bad_parameters_3"; 1357 params.key_len = 0; 1358 handle = rte_hash_create(¶ms); 1359 if (handle != NULL) { 1360 rte_hash_free(handle); 1361 printf("Impossible creating hash successfully if key_len in parameter is zero\n"); 1362 return -1; 1363 } 1364 1365 memcpy(¶ms, &ut_params, sizeof(params)); 1366 params.name = "creation_with_bad_parameters_4"; 1367 params.socket_id = RTE_MAX_NUMA_NODES + 1; 1368 handle = rte_hash_create(¶ms); 1369 if (handle != NULL) { 1370 rte_hash_free(handle); 1371 printf("Impossible creating hash successfully with invalid socket\n"); 1372 return -1; 1373 } 1374 1375 /* test with same name should fail */ 1376 memcpy(¶ms, &ut_params, sizeof(params)); 1377 params.name = "same_name"; 1378 handle = rte_hash_create(¶ms); 1379 if (handle == NULL) { 1380 printf("Cannot create first hash table with 'same_name'\n"); 1381 return -1; 1382 } 1383 tmp = rte_hash_create(¶ms); 1384 if (tmp != NULL) { 1385 printf("Creation of hash table with same name should fail\n"); 1386 rte_hash_free(handle); 1387 rte_hash_free(tmp); 1388 return -1; 1389 } 1390 rte_hash_free(handle); 1391 1392 printf("# Test successful. No more errors expected\n"); 1393 1394 return 0; 1395 } 1396 1397 /* 1398 * Do tests for hash creation with parameters that look incorrect 1399 * but are actually valid. 1400 */ 1401 static int 1402 test_hash_creation_with_good_parameters(void) 1403 { 1404 struct rte_hash *handle; 1405 struct rte_hash_parameters params; 1406 1407 /* create with null hash function - should choose DEFAULT_HASH_FUNC */ 1408 memcpy(¶ms, &ut_params, sizeof(params)); 1409 params.name = "name"; 1410 params.hash_func = NULL; 1411 handle = rte_hash_create(¶ms); 1412 if (handle == NULL) { 1413 printf("Creating hash with null hash_func failed\n"); 1414 return -1; 1415 } 1416 1417 rte_hash_free(handle); 1418 1419 return 0; 1420 } 1421 1422 #define ITERATIONS 3 1423 /* 1424 * Test to see the average table utilization (entries added/max entries) 1425 * before hitting a random entry that cannot be added 1426 */ 1427 static int test_average_table_utilization(uint32_t ext_table) 1428 { 1429 struct rte_hash *handle; 1430 uint8_t simple_key[MAX_KEYSIZE]; 1431 unsigned i, j; 1432 unsigned added_keys, average_keys_added = 0; 1433 int ret; 1434 unsigned int cnt; 1435 1436 printf("\n# Running test to determine average utilization" 1437 "\n before adding elements begins to fail\n"); 1438 if (ext_table) 1439 printf("ext table is enabled\n"); 1440 else 1441 printf("ext table is disabled\n"); 1442 1443 printf("Measuring performance, please wait"); 1444 fflush(stdout); 1445 ut_params.entries = 1 << 16; 1446 ut_params.name = "test_average_utilization"; 1447 ut_params.hash_func = rte_jhash; 1448 if (ext_table) 1449 ut_params.extra_flag |= RTE_HASH_EXTRA_FLAGS_EXT_TABLE; 1450 else 1451 ut_params.extra_flag &= ~RTE_HASH_EXTRA_FLAGS_EXT_TABLE; 1452 1453 handle = rte_hash_create(&ut_params); 1454 1455 RETURN_IF_ERROR(handle == NULL, "hash creation failed"); 1456 1457 for (j = 0; j < ITERATIONS; j++) { 1458 ret = 0; 1459 /* Add random entries until key cannot be added */ 1460 for (added_keys = 0; ret >= 0; added_keys++) { 1461 for (i = 0; i < ut_params.key_len; i++) 1462 simple_key[i] = rte_rand() % 255; 1463 ret = rte_hash_add_key(handle, simple_key); 1464 if (ret < 0) 1465 break; 1466 } 1467 1468 if (ret != -ENOSPC) { 1469 printf("Unexpected error when adding keys\n"); 1470 rte_hash_free(handle); 1471 return -1; 1472 } 1473 1474 cnt = rte_hash_count(handle); 1475 if (cnt != added_keys) { 1476 printf("rte_hash_count returned wrong value %u, %u," 1477 "%u\n", j, added_keys, cnt); 1478 rte_hash_free(handle); 1479 return -1; 1480 } 1481 if (ext_table) { 1482 if (cnt != ut_params.entries) { 1483 printf("rte_hash_count returned wrong value " 1484 "%u, %u, %u\n", j, added_keys, cnt); 1485 rte_hash_free(handle); 1486 return -1; 1487 } 1488 } 1489 1490 average_keys_added += added_keys; 1491 1492 /* Reset the table */ 1493 rte_hash_reset(handle); 1494 1495 /* Print a dot to show progress on operations */ 1496 printf("."); 1497 fflush(stdout); 1498 } 1499 1500 average_keys_added /= ITERATIONS; 1501 1502 printf("\nAverage table utilization = %.2f%% (%u/%u)\n", 1503 ((double) average_keys_added / ut_params.entries * 100), 1504 average_keys_added, ut_params.entries); 1505 rte_hash_free(handle); 1506 1507 return 0; 1508 } 1509 1510 #define NUM_ENTRIES 256 1511 static int test_hash_iteration(uint32_t ext_table) 1512 { 1513 struct rte_hash *handle; 1514 unsigned i; 1515 uint8_t keys[NUM_ENTRIES][MAX_KEYSIZE]; 1516 const void *next_key; 1517 void *next_data; 1518 void *data[NUM_ENTRIES]; 1519 unsigned added_keys; 1520 uint32_t iter = 0; 1521 int ret = 0; 1522 1523 ut_params.entries = NUM_ENTRIES; 1524 ut_params.name = "test_hash_iteration"; 1525 ut_params.hash_func = rte_jhash; 1526 ut_params.key_len = 16; 1527 if (ext_table) 1528 ut_params.extra_flag |= RTE_HASH_EXTRA_FLAGS_EXT_TABLE; 1529 else 1530 ut_params.extra_flag &= ~RTE_HASH_EXTRA_FLAGS_EXT_TABLE; 1531 1532 handle = rte_hash_create(&ut_params); 1533 RETURN_IF_ERROR(handle == NULL, "hash creation failed"); 1534 1535 /* Add random entries until key cannot be added */ 1536 for (added_keys = 0; added_keys < NUM_ENTRIES; added_keys++) { 1537 data[added_keys] = (void *) ((uintptr_t) rte_rand()); 1538 for (i = 0; i < ut_params.key_len; i++) 1539 keys[added_keys][i] = rte_rand() % 255; 1540 ret = rte_hash_add_key_data(handle, keys[added_keys], data[added_keys]); 1541 if (ret < 0) { 1542 if (ext_table) { 1543 printf("Insertion failed for ext table\n"); 1544 goto err; 1545 } 1546 break; 1547 } 1548 } 1549 1550 /* Iterate through the hash table */ 1551 while (rte_hash_iterate(handle, &next_key, &next_data, &iter) >= 0) { 1552 /* Search for the key in the list of keys added */ 1553 for (i = 0; i < NUM_ENTRIES; i++) { 1554 if (memcmp(next_key, keys[i], ut_params.key_len) == 0) { 1555 if (next_data != data[i]) { 1556 printf("Data found in the hash table is" 1557 "not the data added with the key\n"); 1558 goto err; 1559 } 1560 added_keys--; 1561 break; 1562 } 1563 } 1564 if (i == NUM_ENTRIES) { 1565 printf("Key found in the hash table was not added\n"); 1566 goto err; 1567 } 1568 } 1569 1570 /* Check if all keys have been iterated */ 1571 if (added_keys != 0) { 1572 printf("There were still %u keys to iterate\n", added_keys); 1573 goto err; 1574 } 1575 1576 rte_hash_free(handle); 1577 return 0; 1578 1579 err: 1580 rte_hash_free(handle); 1581 return -1; 1582 } 1583 1584 static uint8_t key[16] = {0x00, 0x01, 0x02, 0x03, 1585 0x04, 0x05, 0x06, 0x07, 1586 0x08, 0x09, 0x0a, 0x0b, 1587 0x0c, 0x0d, 0x0e, 0x0f}; 1588 static struct rte_hash_parameters hash_params_ex = { 1589 .name = NULL, 1590 .entries = 64, 1591 .key_len = 0, 1592 .hash_func = NULL, 1593 .hash_func_init_val = 0, 1594 .socket_id = 0, 1595 }; 1596 1597 /* 1598 * add/delete key with jhash2 1599 */ 1600 static int 1601 test_hash_add_delete_jhash2(void) 1602 { 1603 int ret = -1; 1604 struct rte_hash *handle; 1605 int32_t pos1, pos2; 1606 1607 hash_params_ex.name = "hash_test_jhash2"; 1608 hash_params_ex.key_len = 4; 1609 hash_params_ex.hash_func = (rte_hash_function)rte_jhash_32b; 1610 1611 handle = rte_hash_create(&hash_params_ex); 1612 if (handle == NULL) { 1613 printf("test_hash_add_delete_jhash2 fail to create hash\n"); 1614 goto fail_jhash2; 1615 } 1616 pos1 = rte_hash_add_key(handle, (void *)&key[0]); 1617 if (pos1 < 0) { 1618 printf("test_hash_add_delete_jhash2 fail to add hash key\n"); 1619 goto fail_jhash2; 1620 } 1621 1622 pos2 = rte_hash_del_key(handle, (void *)&key[0]); 1623 if (pos2 < 0 || pos1 != pos2) { 1624 printf("test_hash_add_delete_jhash2 delete different key from being added\n"); 1625 goto fail_jhash2; 1626 } 1627 ret = 0; 1628 1629 fail_jhash2: 1630 if (handle != NULL) 1631 rte_hash_free(handle); 1632 1633 return ret; 1634 } 1635 1636 /* 1637 * add/delete (2) key with jhash2 1638 */ 1639 static int 1640 test_hash_add_delete_2_jhash2(void) 1641 { 1642 int ret = -1; 1643 struct rte_hash *handle; 1644 int32_t pos1, pos2; 1645 1646 hash_params_ex.name = "hash_test_2_jhash2"; 1647 hash_params_ex.key_len = 8; 1648 hash_params_ex.hash_func = (rte_hash_function)rte_jhash_32b; 1649 1650 handle = rte_hash_create(&hash_params_ex); 1651 if (handle == NULL) 1652 goto fail_2_jhash2; 1653 1654 pos1 = rte_hash_add_key(handle, (void *)&key[0]); 1655 if (pos1 < 0) 1656 goto fail_2_jhash2; 1657 1658 pos2 = rte_hash_del_key(handle, (void *)&key[0]); 1659 if (pos2 < 0 || pos1 != pos2) 1660 goto fail_2_jhash2; 1661 1662 ret = 0; 1663 1664 fail_2_jhash2: 1665 if (handle != NULL) 1666 rte_hash_free(handle); 1667 1668 return ret; 1669 } 1670 1671 static uint32_t 1672 test_hash_jhash_1word(const void *key, uint32_t length, uint32_t initval) 1673 { 1674 const uint32_t *k = key; 1675 1676 RTE_SET_USED(length); 1677 1678 return rte_jhash_1word(k[0], initval); 1679 } 1680 1681 static uint32_t 1682 test_hash_jhash_2word(const void *key, uint32_t length, uint32_t initval) 1683 { 1684 const uint32_t *k = key; 1685 1686 RTE_SET_USED(length); 1687 1688 return rte_jhash_2words(k[0], k[1], initval); 1689 } 1690 1691 static uint32_t 1692 test_hash_jhash_3word(const void *key, uint32_t length, uint32_t initval) 1693 { 1694 const uint32_t *k = key; 1695 1696 RTE_SET_USED(length); 1697 1698 return rte_jhash_3words(k[0], k[1], k[2], initval); 1699 } 1700 1701 /* 1702 * add/delete key with jhash 1word 1703 */ 1704 static int 1705 test_hash_add_delete_jhash_1word(void) 1706 { 1707 int ret = -1; 1708 struct rte_hash *handle; 1709 int32_t pos1, pos2; 1710 1711 hash_params_ex.name = "hash_test_jhash_1word"; 1712 hash_params_ex.key_len = 4; 1713 hash_params_ex.hash_func = test_hash_jhash_1word; 1714 1715 handle = rte_hash_create(&hash_params_ex); 1716 if (handle == NULL) 1717 goto fail_jhash_1word; 1718 1719 pos1 = rte_hash_add_key(handle, (void *)&key[0]); 1720 if (pos1 < 0) 1721 goto fail_jhash_1word; 1722 1723 pos2 = rte_hash_del_key(handle, (void *)&key[0]); 1724 if (pos2 < 0 || pos1 != pos2) 1725 goto fail_jhash_1word; 1726 1727 ret = 0; 1728 1729 fail_jhash_1word: 1730 if (handle != NULL) 1731 rte_hash_free(handle); 1732 1733 return ret; 1734 } 1735 1736 /* 1737 * add/delete key with jhash 2word 1738 */ 1739 static int 1740 test_hash_add_delete_jhash_2word(void) 1741 { 1742 int ret = -1; 1743 struct rte_hash *handle; 1744 int32_t pos1, pos2; 1745 1746 hash_params_ex.name = "hash_test_jhash_2word"; 1747 hash_params_ex.key_len = 8; 1748 hash_params_ex.hash_func = test_hash_jhash_2word; 1749 1750 handle = rte_hash_create(&hash_params_ex); 1751 if (handle == NULL) 1752 goto fail_jhash_2word; 1753 1754 pos1 = rte_hash_add_key(handle, (void *)&key[0]); 1755 if (pos1 < 0) 1756 goto fail_jhash_2word; 1757 1758 pos2 = rte_hash_del_key(handle, (void *)&key[0]); 1759 if (pos2 < 0 || pos1 != pos2) 1760 goto fail_jhash_2word; 1761 1762 ret = 0; 1763 1764 fail_jhash_2word: 1765 if (handle != NULL) 1766 rte_hash_free(handle); 1767 1768 return ret; 1769 } 1770 1771 /* 1772 * add/delete key with jhash 3word 1773 */ 1774 static int 1775 test_hash_add_delete_jhash_3word(void) 1776 { 1777 int ret = -1; 1778 struct rte_hash *handle; 1779 int32_t pos1, pos2; 1780 1781 hash_params_ex.name = "hash_test_jhash_3word"; 1782 hash_params_ex.key_len = 12; 1783 hash_params_ex.hash_func = test_hash_jhash_3word; 1784 1785 handle = rte_hash_create(&hash_params_ex); 1786 if (handle == NULL) 1787 goto fail_jhash_3word; 1788 1789 pos1 = rte_hash_add_key(handle, (void *)&key[0]); 1790 if (pos1 < 0) 1791 goto fail_jhash_3word; 1792 1793 pos2 = rte_hash_del_key(handle, (void *)&key[0]); 1794 if (pos2 < 0 || pos1 != pos2) 1795 goto fail_jhash_3word; 1796 1797 ret = 0; 1798 1799 fail_jhash_3word: 1800 if (handle != NULL) 1801 rte_hash_free(handle); 1802 1803 return ret; 1804 } 1805 1806 /* 1807 * Do all unit and performance tests. 1808 */ 1809 static int 1810 test_hash(void) 1811 { 1812 if (test_add_delete() < 0) 1813 return -1; 1814 if (test_hash_add_delete_jhash2() < 0) 1815 return -1; 1816 if (test_hash_add_delete_2_jhash2() < 0) 1817 return -1; 1818 if (test_hash_add_delete_jhash_1word() < 0) 1819 return -1; 1820 if (test_hash_add_delete_jhash_2word() < 0) 1821 return -1; 1822 if (test_hash_add_delete_jhash_3word() < 0) 1823 return -1; 1824 if (test_hash_get_key_with_position() < 0) 1825 return -1; 1826 if (test_hash_find_existing() < 0) 1827 return -1; 1828 if (test_add_update_delete() < 0) 1829 return -1; 1830 if (test_add_update_delete_free() < 0) 1831 return -1; 1832 if (test_add_delete_free_lf() < 0) 1833 return -1; 1834 if (test_five_keys() < 0) 1835 return -1; 1836 if (test_full_bucket() < 0) 1837 return -1; 1838 if (test_extendable_bucket() < 0) 1839 return -1; 1840 1841 if (test_fbk_hash_find_existing() < 0) 1842 return -1; 1843 if (fbk_hash_unit_test() < 0) 1844 return -1; 1845 if (test_hash_creation_with_bad_parameters() < 0) 1846 return -1; 1847 if (test_hash_creation_with_good_parameters() < 0) 1848 return -1; 1849 1850 /* ext table disabled */ 1851 if (test_average_table_utilization(0) < 0) 1852 return -1; 1853 if (test_hash_iteration(0) < 0) 1854 return -1; 1855 1856 /* ext table enabled */ 1857 if (test_average_table_utilization(1) < 0) 1858 return -1; 1859 if (test_hash_iteration(1) < 0) 1860 return -1; 1861 1862 run_hash_func_tests(); 1863 1864 if (test_crc32_hash_alg_equiv() < 0) 1865 return -1; 1866 1867 return 0; 1868 } 1869 1870 REGISTER_TEST_COMMAND(hash_autotest, test_hash); 1871