diff options
author | Damjan Marion <damarion@cisco.com> | 2023-03-15 11:42:06 +0000 |
---|---|---|
committer | Florin Coras <florin.coras@gmail.com> | 2023-03-15 17:07:01 +0000 |
commit | c3542e17b5dfea27f46f0656e8f1f8092545b796 (patch) | |
tree | 623b16012e5b19484781245e1532024b1669fa2b /src/vppinfra/test | |
parent | adeaf16960f8895eb246b388553a49d2ade80dc4 (diff) |
vppinfra: widen the scope of test_vector_funcs
Location changed and binary renamed to test_infra
Also it is built by default.
Type: improvement
Change-Id: I27cd97f274501ceb7a01213e2bc9676cea00f39c
Signed-off-by: Damjan Marion <damarion@cisco.com>
Diffstat (limited to 'src/vppinfra/test')
-rw-r--r-- | src/vppinfra/test/array_mask.c | 124 | ||||
-rw-r--r-- | src/vppinfra/test/compress.c | 248 | ||||
-rw-r--r-- | src/vppinfra/test/count_equal.c | 104 | ||||
-rw-r--r-- | src/vppinfra/test/index_to_ptr.c | 58 | ||||
-rw-r--r-- | src/vppinfra/test/ip_csum.c | 179 | ||||
-rw-r--r-- | src/vppinfra/test/mask_compare.c | 139 | ||||
-rw-r--r-- | src/vppinfra/test/memcpy_x86_64.c | 142 | ||||
-rw-r--r-- | src/vppinfra/test/sha2.c | 326 | ||||
-rw-r--r-- | src/vppinfra/test/test.c | 249 | ||||
-rw-r--r-- | src/vppinfra/test/test.h | 107 | ||||
-rw-r--r-- | src/vppinfra/test/toeplitz.c | 553 |
11 files changed, 2229 insertions, 0 deletions
diff --git a/src/vppinfra/test/array_mask.c b/src/vppinfra/test/array_mask.c new file mode 100644 index 00000000000..c3b475c431b --- /dev/null +++ b/src/vppinfra/test/array_mask.c @@ -0,0 +1,124 @@ +/* SPDX-License-Identifier: Apache-2.0 + * Copyright(c) 2021 Cisco Systems, Inc. + */ + +#include <vppinfra/format.h> +#include <vppinfra/test/test.h> +#include <vppinfra/vector/array_mask.h> + +__test_funct_fn void +clib_array_mask_u32_wrapper (u32 *src, u32 mask, u32 n_elts) +{ + clib_array_mask_u32 (src, mask, n_elts); +} + +typedef struct +{ + u32 mask; + u32 expected[256]; +} array_mask_test_t; + +static array_mask_test_t tests[] = { + /* mask values 0x1, output array of alternating 0 1 0 1 .. */ + { .mask = 1, + .expected = { 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, + 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, + 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, + 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, + 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, + 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, + 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, + 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, + 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, + 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, + 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, + 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, + 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1 } }, + /* mask values 0xFFFFFFFF, output array of 0, 1, 2, .., 255 */ + { .mask = ~0U, + .expected = { 0, 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 } }, + /* mask values 0xF, output array of 0, .., 15, 0, .., 15, 0, .., 15 */ + { .mask = 15, + .expected = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 } }, + /* mask values 0x1, output array of 1, 0, 1, 0,.. */ + { .mask = 1, .expected = { 1, 0, 1, 0, 1, 0, 1, 0, 1, 0 } }, +}; + +static clib_error_t * +test_clib_array_mask_u32 (clib_error_t *err) +{ + u32 i, j; + for (i = 0; i < ARRAY_LEN (tests) - 1; i++) + { + u32 src[256]; + for (j = 0; j < ARRAY_LEN (src); j++) + src[j] = j; + + array_mask_test_t *t = tests + i; + clib_array_mask_u32_wrapper (src, t->mask, ARRAY_LEN (src)); + for (j = 0; j < ARRAY_LEN (src); j++) + { + if (src[j] != t->expected[j]) + return clib_error_return (err, + "testcase %u failed at " + "(src[%u] = 0x%x, expected 0x%x)", + i, j, src[j], t->expected[j]); + } + } + + u32 src[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; + array_mask_test_t *t = tests + i; + + clib_array_mask_u32_wrapper (src, t->mask, ARRAY_LEN (src)); + for (j = 0; j < ARRAY_LEN (src); j++) + { + if (src[j] != t->expected[j]) + return clib_error_return (err, + "testcase %u failed at " + "(src[%u] = 0x%x, expected 0x%x)", + i, j, src[j], t->expected[j]); + } + + return err; +} + +REGISTER_TEST (clib_array_mask_u32) = { + .name = "clib_array_mask_u32", + .fn = test_clib_array_mask_u32, +}; diff --git a/src/vppinfra/test/compress.c b/src/vppinfra/test/compress.c new file mode 100644 index 00000000000..7fdc3f8ee42 --- /dev/null +++ b/src/vppinfra/test/compress.c @@ -0,0 +1,248 @@ +/* SPDX-License-Identifier: Apache-2.0 + * Copyright(c) 2021 Cisco Systems, Inc. + */ + +#include <vppinfra/format.h> +#include <vppinfra/test/test.h> +#include <vppinfra/vector/compress.h> + +__test_funct_fn u32 +clib_compress_u64_wrapper (u64 *dst, u64 *src, u64 *mask, u32 n_elts) +{ + return clib_compress_u64 (dst, src, mask, n_elts); +} + +__test_funct_fn u32 +clib_compress_u32_wrapper (u32 *dst, u32 *src, u64 *mask, u32 n_elts) +{ + return clib_compress_u32 (dst, src, mask, n_elts); +} + +__test_funct_fn u32 +clib_compress_u16_wrapper (u16 *dst, u16 *src, u64 *mask, u32 n_elts) +{ + return clib_compress_u16 (dst, src, mask, n_elts); +} + +__test_funct_fn u32 +clib_compress_u8_wrapper (u8 *dst, u8 *src, u64 *mask, u32 n_elts) +{ + return clib_compress_u8 (dst, src, mask, n_elts); +} + +typedef struct +{ + u64 mask[10]; + u32 n_elts; +} compress_test_t; + +static compress_test_t tests[] = { + { .mask = { 1 }, .n_elts = 1 }, + { .mask = { 2 }, .n_elts = 2 }, + { .mask = { 3 }, .n_elts = 2 }, + { .mask = { 0, 1 }, .n_elts = 66 }, + { .mask = { 0, 2 }, .n_elts = 69 }, + { .mask = { 0, 3 }, .n_elts = 66 }, + { .mask = { ~0ULL, ~0ULL, ~0ULL, ~0ULL }, .n_elts = 62 }, + { .mask = { ~0ULL, ~0ULL, ~0ULL, ~0ULL }, .n_elts = 255 }, + { .mask = { ~0ULL, 1, 1, ~0ULL }, .n_elts = 256 }, +}; + +static clib_error_t * +test_clib_compress_u64 (clib_error_t *err) +{ + u64 src[513]; + u64 dst[513]; + u32 i, j; + + for (i = 0; i < ARRAY_LEN (src); i++) + src[i] = i; + + for (i = 0; i < ARRAY_LEN (tests); i++) + { + compress_test_t *t = tests + i; + u64 *dp = dst; + u32 r; + + for (j = 0; j < ARRAY_LEN (dst); j++) + dst[j] = 0xa5a5a5a5a5a5a5a5; + + r = clib_compress_u64_wrapper (dst, src, t->mask, t->n_elts); + + for (j = 0; j < t->n_elts; j++) + { + if ((t->mask[j >> 6] & (1ULL << (j & 0x3f))) == 0) + continue; + if (dp[0] != src[j]) + return clib_error_return (err, + "wrong data in testcase %u at " + "(dst[%u] = 0x%lx, src[%u] = 0x%lx)", + i, dp - dst, dp[0], j, src[j]); + dp++; + } + + if (dst[dp - dst + 1] != 0xa5a5a5a5a5a5a5a5) + return clib_error_return (err, "buffer overrun in testcase %u", i); + + if (dp - dst != r) + return clib_error_return (err, "wrong number of elts in testcase %u", + i); + } + + return err; + + return err; +} + +static clib_error_t * +test_clib_compress_u32 (clib_error_t *err) +{ + u32 src[513]; + u32 dst[513]; + u32 i, j; + + for (i = 0; i < ARRAY_LEN (src); i++) + src[i] = i; + + for (i = 0; i < ARRAY_LEN (tests); i++) + { + compress_test_t *t = tests + i; + u32 *dp = dst; + u32 r; + + for (j = 0; j < ARRAY_LEN (dst); j++) + dst[j] = 0xa5a5a5a5; + + r = clib_compress_u32_wrapper (dst, src, t->mask, t->n_elts); + + for (j = 0; j < t->n_elts; j++) + { + if ((t->mask[j >> 6] & (1ULL << (j & 0x3f))) == 0) + continue; + + if (dp[0] != src[j]) + return clib_error_return (err, + "wrong data in testcase %u at " + "(dst[%u] = 0x%x, src[%u] = 0x%x)", + i, dp - dst, dp[0], j, src[j]); + dp++; + } + + if (dst[dp - dst + 1] != 0xa5a5a5a5) + return clib_error_return (err, "buffer overrun in testcase %u", i); + + if (dp - dst != r) + return clib_error_return (err, "wrong number of elts in testcase %u", + i); + } + + return err; +} + +static clib_error_t * +test_clib_compress_u16 (clib_error_t *err) +{ + u16 src[513]; + u16 dst[513]; + u32 i, j; + + for (i = 0; i < ARRAY_LEN (src); i++) + src[i] = i; + + for (i = 0; i < ARRAY_LEN (tests); i++) + { + compress_test_t *t = tests + i; + u16 *dp = dst; + u32 r; + + for (j = 0; j < ARRAY_LEN (dst); j++) + dst[j] = 0xa5a5; + + r = clib_compress_u16_wrapper (dst, src, t->mask, t->n_elts); + + for (j = 0; j < t->n_elts; j++) + { + if ((t->mask[j >> 6] & (1ULL << (j & 0x3f))) == 0) + continue; + if (dp[0] != src[j]) + return clib_error_return (err, + "wrong data in testcase %u at " + "(dst[%u] = 0x%x, src[%u] = 0x%x)", + i, dp - dst, dp[0], j, src[j]); + dp++; + } + + if (dst[dp - dst + 1] != 0xa5a5) + return clib_error_return (err, "buffer overrun in testcase %u", i); + + if (dp - dst != r) + return clib_error_return (err, "wrong number of elts in testcase %u", + i); + } + + return err; +} + +static clib_error_t * +test_clib_compress_u8 (clib_error_t *err) +{ + u8 src[513]; + u8 dst[513]; + u32 i, j; + + for (i = 0; i < ARRAY_LEN (src); i++) + src[i] = i; + + for (i = 0; i < ARRAY_LEN (tests); i++) + { + compress_test_t *t = tests + i; + u8 *dp = dst; + u32 r; + + for (j = 0; j < ARRAY_LEN (dst); j++) + dst[j] = 0xa5; + + r = clib_compress_u8_wrapper (dst, src, t->mask, t->n_elts); + + for (j = 0; j < t->n_elts; j++) + { + if ((t->mask[j >> 6] & (1ULL << (j & 0x3f))) == 0) + continue; + if (dp[0] != src[j]) + return clib_error_return (err, + "wrong data in testcase %u at " + "(dst[%u] = 0x%x, src[%u] = 0x%x)", + i, dp - dst, dp[0], j, src[j]); + dp++; + } + + if (dst[dp - dst + 1] != 0xa5) + return clib_error_return (err, "buffer overrun in testcase %u", i); + + if (dp - dst != r) + return clib_error_return (err, "wrong number of elts in testcase %u", + i); + } + + return err; +} + +REGISTER_TEST (clib_compress_u64) = { + .name = "clib_compress_u64", + .fn = test_clib_compress_u64, +}; + +REGISTER_TEST (clib_compress_u32) = { + .name = "clib_compress_u32", + .fn = test_clib_compress_u32, +}; + +REGISTER_TEST (clib_compress_u16) = { + .name = "clib_compress_u16", + .fn = test_clib_compress_u16, +}; + +REGISTER_TEST (clib_compress_u8) = { + .name = "clib_compress_u8", + .fn = test_clib_compress_u8, +}; diff --git a/src/vppinfra/test/count_equal.c b/src/vppinfra/test/count_equal.c new file mode 100644 index 00000000000..942c2203d3d --- /dev/null +++ b/src/vppinfra/test/count_equal.c @@ -0,0 +1,104 @@ +/* SPDX-License-Identifier: Apache-2.0 + * Copyright(c) 2021 Cisco Systems, Inc. + */ + +#include <vppinfra/format.h> +#include <vppinfra/test/test.h> +#include <vppinfra/vector/count_equal.h> + +#define foreach_clib_count_equal(type) \ + typedef uword (wrapper_fn_##type) (type * a, uword maxcount); \ + \ + __test_funct_fn uword clib_count_equal_##type##_wrapper (type *a, \ + uword maxcount) \ + { \ + return clib_count_equal_##type (a, maxcount); \ + } \ + \ + static wrapper_fn_##type *wfn_##type = &clib_count_equal_##type##_wrapper; \ + static clib_error_t *test_clib_count_equal_##type (clib_error_t *err) \ + { \ + u32 ps = clib_mem_get_log2_page_size (); \ + void *map; \ + \ + u16 lengths[] = { \ + 1, 2, 3, 5, 7, 9, 15, 16, 17, 31, 32, 33, 255, 256, 257 \ + }; \ + type *data; \ + \ + map = clib_mem_vm_map (0, 2ULL << ps, ps, "test"); \ + if (map == CLIB_MEM_VM_MAP_FAILED) \ + return clib_error_return (err, "clib_mem_vm_map failed"); \ + \ + data = ((type *) (map + (1ULL << ps))); \ + data[-1] = 0xfe; \ + \ + mprotect (data, 1ULL < ps, PROT_NONE); \ + \ + for (u8 d = 0; d < 255; d++) \ + { \ + for (int i = 1; i <= (1 << ps) / sizeof (data[0]); i++) \ + data[-i] = d; \ + for (int i = 0; i < ARRAY_LEN (lengths); i++) \ + { \ + uword rv, len = lengths[i]; \ + \ + if ((rv = wfn_##type (data - len, len)) != len) \ + { \ + err = clib_error_return ( \ + err, "testcase 1 failed for len %u data %u(rv %u)", len, d, \ + rv); \ + goto done; \ + } \ + \ + data[-1] = d + 1; \ + if (len > 1 && ((rv = wfn_##type (data - len, len)) != len - 1)) \ + { \ + err = clib_error_return ( \ + err, "testcase 2 failed for len %u data %u (rv %u)", len, \ + d, rv); \ + goto done; \ + } \ + data[-1] = d; \ + \ + data[-2] = d + 1; \ + if (len > 2 && ((rv = wfn_##type (data - len, len)) != len - 2)) \ + { \ + err = clib_error_return ( \ + err, "testcase 3 failed for len %u data %u (rv %u)", len, \ + d, rv); \ + goto done; \ + } \ + data[-2] = d; \ + } \ + } \ + \ + done: \ + clib_mem_vm_unmap (map); \ + return err; \ + } + +foreach_clib_count_equal (u8); +foreach_clib_count_equal (u16); +foreach_clib_count_equal (u32); +foreach_clib_count_equal (u64); + +REGISTER_TEST (clib_count_equal_u8) = { + .name = "clib_count_equal_u8", + .fn = test_clib_count_equal_u8, +}; + +REGISTER_TEST (clib_count_equal_u16) = { + .name = "clib_count_equal_u16", + .fn = test_clib_count_equal_u16, +}; + +REGISTER_TEST (clib_count_equal_u32) = { + .name = "clib_count_equal_u32", + .fn = test_clib_count_equal_u32, +}; + +REGISTER_TEST (clib_count_equal_u64) = { + .name = "clib_count_equal_u64", + .fn = test_clib_count_equal_u64, +}; diff --git a/src/vppinfra/test/index_to_ptr.c b/src/vppinfra/test/index_to_ptr.c new file mode 100644 index 00000000000..06b621c10ff --- /dev/null +++ b/src/vppinfra/test/index_to_ptr.c @@ -0,0 +1,58 @@ +/* SPDX-License-Identifier: Apache-2.0 + * Copyright(c) 2021 Cisco Systems, Inc. + */ + +#include <vppinfra/format.h> +#include <vppinfra/test/test.h> +#include <vppinfra/vector/index_to_ptr.h> + +typedef void (wrapper_fn) (u32 *indices, void *base, u8 shift, void **ptrs, + u32 n_elts); + +__test_funct_fn void +clib_index_to_ptr_u32_wrapper (u32 *indices, void *base, u8 shift, void **ptrs, + u32 n_elts) +{ + clib_index_to_ptr_u32 (indices, base, shift, ptrs, n_elts); +} + +static wrapper_fn *wfn = &clib_index_to_ptr_u32_wrapper; + +static clib_error_t * +test_clib_index_to_ptr_u32 (clib_error_t *err) +{ + void *_ptrs[512 + 128], **ptrs = _ptrs + 64; + u32 _indices[512 + 128], *indices = _indices + 64; + u16 lengths[] = { 1, 3, 5, 7, 9, 15, 16, 17, 31, 32, + 33, 40, 41, 42, 63, 64, 65, 511, 512 }; + + for (int i = 0; i < ARRAY_LEN (_indices); i++) + _indices[i] = i; + + for (int i = 0; i < ARRAY_LEN (lengths); i++) + { + u16 len = lengths[i]; + u8 shift = 6; + void *base = (void *) 0x100000000 + i; + + for (int j = -64; j < len + 64; j++) + ptrs[j] = (void *) 0xfefefefefefefefe; + + wfn (indices, base, shift, ptrs, len); + for (int j = 0; j < len; j++) + { + void *expected = base + ((u64) indices[j] << shift); + if (ptrs[j] != expected) + return clib_error_return (err, + "testcase failed for length %u " + "(offset %u, expected %p, found %p)", + len, j, expected, ptrs[j]); + } + } + return err; +} + +REGISTER_TEST (clib_index_to_ptr_u32) = { + .name = "clib_index_to_ptr_u32", + .fn = test_clib_index_to_ptr_u32, +}; diff --git a/src/vppinfra/test/ip_csum.c b/src/vppinfra/test/ip_csum.c new file mode 100644 index 00000000000..9387336ad7d --- /dev/null +++ b/src/vppinfra/test/ip_csum.c @@ -0,0 +1,179 @@ +/* SPDX-License-Identifier: Apache-2.0 + * Copyright(c) 2021 Cisco Systems, Inc. + */ + +#include <vppinfra/format.h> +#include <vppinfra/test/test.h> +#include <vppinfra/vector/ip_csum.h> + +typedef struct +{ + struct + { + u8 *src; + u32 count; + } chunk[5]; + u16 result; +} ip_csum_test_t; + +static u8 test1[] = { 0x45, 0x00, 0x00, 0x73, 0x00, 0x00, 0x40, + 0x00, 0x40, 0x11, 0x00, 0x00, 0xc0, 0xa8, + 0x00, 0x01, 0xc0, 0xa8, 0x00, 0xc7, 0x00 }; +#define TEST_LEN(x) (ARRAY_LEN (x) - 1) + +static ip_csum_test_t tests[] = { { + .chunk[0].src = test1, + .chunk[0].count = TEST_LEN (test1), + .result = 0x61b8, + }, + { + .chunk[0].src = test1, + .chunk[0].count = 1, + .chunk[1].src = test1 + 1, + .chunk[1].count = 2, + .chunk[2].src = test1 + 3, + .chunk[2].count = 3, + .chunk[3].src = test1 + 6, + .chunk[3].count = 4, + .chunk[4].src = test1 + 10, + .chunk[4].count = TEST_LEN (test1) - 10, + .result = 0x61b8, + }, + { + .chunk[0].count = 1, + .result = 0xff0f, + }, + { + .chunk[0].count = 2, + .result = 0x080f, + }, + { + .chunk[0].count = 3, + .result = 0x0711, + }, + { + .chunk[0].count = 4, + .result = 0x1210, + }, + { + .chunk[0].count = 63, + .result = 0xda01, + }, + { + .chunk[0].count = 64, + .result = 0xe100, + }, + { + .chunk[0].count = 65, + .result = 0xe010, + }, + { + .chunk[0].count = 65535, + .result = 0xfc84, + }, + { + .chunk[0].count = 65536, + .result = 0xffff, + } }; + +static clib_error_t * +test_clib_ip_csum (clib_error_t *err) +{ + u8 *buf; + buf = test_mem_alloc (65536); + for (int i = 0; i < 65536; i++) + buf[i] = 0xf0 + ((i * 7) & 0xf); + + for (int i = 0; i < ARRAY_LEN (tests); i++) + { + clib_ip_csum_t c = {}; + ip_csum_test_t *t = tests + i; + u16 rv; + + for (int j = 0; j < ARRAY_LEN (((ip_csum_test_t *) 0)->chunk); j++) + if (t->chunk[j].count > 0) + { + if (t->chunk[j].src == 0) + clib_ip_csum_chunk (&c, buf, t->chunk[j].count); + else + clib_ip_csum_chunk (&c, t->chunk[j].src, t->chunk[j].count); + } + rv = clib_ip_csum_fold (&c); + + if (rv != tests[i].result) + { + err = clib_error_return (err, + "bad checksum in test case %u (expected " + "0x%04x, calculated 0x%04x)", + i, tests[i].result, rv); + goto done; + } + } +done: + test_mem_free (buf); + return err; +} + +void __test_perf_fn +perftest_ip4_hdr (test_perf_t *tp) +{ + u32 n = tp->n_ops; + u8 *data = test_mem_alloc_and_splat (20, n, (void *) &test1); + u16 *res = test_mem_alloc (n * sizeof (u16)); + + test_perf_event_enable (tp); + for (int i = 0; i < n; i++) + res[i] = clib_ip_csum (data + i * 20, 20); + test_perf_event_disable (tp); + + test_mem_free (data); + test_mem_free (res); +} + +void __test_perf_fn +perftest_tcp_payload (test_perf_t *tp) +{ + u32 n = tp->n_ops; + volatile uword *lenp = &tp->arg0; + u8 *data = test_mem_alloc_and_splat (20, n, (void *) &test1); + u16 *res = test_mem_alloc (n * sizeof (u16)); + + test_perf_event_enable (tp); + for (int i = 0; i < n; i++) + res[i] = clib_ip_csum (data + i * lenp[0], lenp[0]); + test_perf_event_disable (tp); + + test_mem_free (data); + test_mem_free (res); +} + +void __test_perf_fn +perftest_byte (test_perf_t *tp) +{ + volatile uword *np = &tp->n_ops; + u8 *data = test_mem_alloc_and_fill_inc_u8 (*np, 0, 0); + u16 *res = test_mem_alloc (sizeof (u16)); + + test_perf_event_enable (tp); + res[0] = clib_ip_csum (data, np[0]); + test_perf_event_disable (tp); + + test_mem_free (data); + test_mem_free (res); +} + +REGISTER_TEST (clib_ip_csum) = { + .name = "clib_ip_csum", + .fn = test_clib_ip_csum, + .perf_tests = PERF_TESTS ( + { .name = "fixed size (per IPv4 Header)", + .n_ops = 1024, + .fn = perftest_ip4_hdr }, + { .name = "fixed size (per 1460 byte block)", + .n_ops = 16, + .arg0 = 1460, + .fn = perftest_tcp_payload }, + { .name = "variable size (per byte)", .n_ops = 16384, .fn = perftest_byte } + + ), +}; diff --git a/src/vppinfra/test/mask_compare.c b/src/vppinfra/test/mask_compare.c new file mode 100644 index 00000000000..738b0082dd7 --- /dev/null +++ b/src/vppinfra/test/mask_compare.c @@ -0,0 +1,139 @@ +/* SPDX-License-Identifier: Apache-2.0 + * Copyright(c) 2021 Cisco Systems, Inc. + */ + +#include <vppinfra/format.h> +#include <vppinfra/test/test.h> +#include <vppinfra/vector/mask_compare.h> + +__test_funct_fn void +clib_mask_compare_u16_wrapper (u16 v, u16 *a, u64 *mask, u32 n_elts) +{ + clib_mask_compare_u16 (v, a, mask, n_elts); +} + +__test_funct_fn void +clib_mask_compare_u32_wrapper (u32 v, u32 *a, u64 *mask, u32 n_elts) +{ + clib_mask_compare_u32 (v, a, mask, n_elts); +} + +__test_funct_fn void +clib_mask_compare_u64_wrapper (u64 v, u64 *a, u64 *mask, u64 n_elts) +{ + clib_mask_compare_u64 (v, a, mask, n_elts); +} + +static clib_error_t * +test_clib_mask_compare_u16 (clib_error_t *err) +{ + u16 array[513]; + u64 mask[10]; + u32 i, j; + + for (i = 0; i < ARRAY_LEN (array); i++) + array[i] = i; + + for (i = 0; i < ARRAY_LEN (array); i++) + { + for (j = 0; j < ARRAY_LEN (mask); j++) + mask[j] = 0xa5a5a5a5a5a5a5a5; + + clib_mask_compare_u16_wrapper (i, array, mask, i + 1); + + for (j = 0; j < (i >> 6); j++) + { + if (mask[j]) + return clib_error_return (err, "mask at position %u not zero", j); + } + if (mask[j] != 1ULL << (i & 0x3f)) + return clib_error_return (err, + "mask at position %u is %lx, expected %lx", + j, mask[j], 1ULL << (i % 64)); + + if (mask[j + 1] != 0xa5a5a5a5a5a5a5a5) + return clib_error_return (err, "mask overrun at position %u", j + 1); + } + return err; +} + +REGISTER_TEST (clib_mask_compare_u16) = { + .name = "clib_mask_compare_u16", + .fn = test_clib_mask_compare_u16, +}; + +static clib_error_t * +test_clib_mask_compare_u32 (clib_error_t *err) +{ + u32 array[513]; + u64 mask[10]; + u32 i, j; + + for (i = 0; i < ARRAY_LEN (array); i++) + array[i] = i; + + for (i = 0; i < ARRAY_LEN (array); i++) + { + for (j = 0; j < ARRAY_LEN (mask); j++) + mask[j] = 0xa5a5a5a5a5a5a5a5; + + clib_mask_compare_u32_wrapper (i, array, mask, i + 1); + + for (j = 0; j < (i >> 6); j++) + { + if (mask[j]) + return clib_error_return (err, "mask at position %u not zero", j); + } + if (mask[j] != 1ULL << (i & 0x3f)) + return clib_error_return (err, + "mask at position %u is %lx, expected %lx", + j, mask[j], 1ULL << (i % 64)); + + if (mask[j + 1] != 0xa5a5a5a5a5a5a5a5) + return clib_error_return (err, "mask overrun at position %u", j + 1); + } + return err; +} + +REGISTER_TEST (clib_mask_compare_u32) = { + .name = "clib_mask_compare_u32", + .fn = test_clib_mask_compare_u32, +}; + +static clib_error_t * +test_clib_mask_compare_u64 (clib_error_t *err) +{ + u64 array[513]; + u64 mask[10]; + u32 i, j; + + for (i = 0; i < ARRAY_LEN (array); i++) + array[i] = i; + + for (i = 0; i < ARRAY_LEN (array); i++) + { + for (j = 0; j < ARRAY_LEN (mask); j++) + mask[j] = 0xa5a5a5a5a5a5a5a5; + + clib_mask_compare_u64_wrapper (i, array, mask, i + 1); + + for (j = 0; j < (i >> 6); j++) + { + if (mask[j]) + return clib_error_return (err, "mask at position %u not zero", j); + } + if (mask[j] != 1ULL << (i & 0x3f)) + return clib_error_return (err, + "mask at position %u is %lx, expected %lx", + j, mask[j], 1ULL << (i % 64)); + + if (mask[j + 1] != 0xa5a5a5a5a5a5a5a5) + return clib_error_return (err, "mask overrun at position %u", j + 1); + } + return err; +} + +REGISTER_TEST (clib_mask_compare_u64) = { + .name = "clib_mask_compare_u64", + .fn = test_clib_mask_compare_u64, +}; diff --git a/src/vppinfra/test/memcpy_x86_64.c b/src/vppinfra/test/memcpy_x86_64.c new file mode 100644 index 00000000000..4d9525d5222 --- /dev/null +++ b/src/vppinfra/test/memcpy_x86_64.c @@ -0,0 +1,142 @@ +/* SPDX-License-Identifier: Apache-2.0 + * Copyright(c) 2021 Cisco Systems, Inc. + */ + +#ifdef __x86_64__ + +#include <vppinfra/format.h> +#include <vppinfra/test/test.h> +#include <vppinfra/vector/mask_compare.h> + +__test_funct_fn void +wrapper (u8 *dst, u8 *src, uword n) +{ + clib_memcpy_x86_64 (dst, src, n); +} + +/* clang-format off */ +#define foreach_const_n \ + _(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) +/* clang-format on */ + +#define _(n) \ + static __clib_noinline void wrapper##n (u8 *dst, u8 *src) \ + { \ + clib_memcpy_x86_64 (dst, src, n); \ + } + +foreach_const_n; +#undef _ + +typedef void (const_fp_t) (u8 *dst, u8 *src); +typedef struct +{ + u16 len; + const_fp_t *fp; +} counst_test_t; + +static counst_test_t const_tests[] = { +#define _(n) { .fp = wrapper##n, .len = n }, + foreach_const_n +#undef _ +}; + +#define MAX_LEN 1024 + +static clib_error_t * +validate_one (clib_error_t *err, u8 *d, u8 *s, u16 n, u8 off, int is_const) +{ + for (int i = 0; i < n; i++) + if (d[i] != s[i]) + return clib_error_return (err, + "memcpy error at position %d " + "(n = %u, off = %u, expected 0x%02x " + "found 0x%02x%s)", + i, n, off, s[i], d[i], + is_const ? ", const" : ""); + for (int i = -64; i < 0; i++) + if (d[i] != 0xfe) + return clib_error_return (err, + "buffer underrun at position %d " + "(n = %u, off = %u, expected 0xfe " + "found 0x%02x%s)", + i, n, off, d[i], is_const ? ", const" : ""); + for (int i = n; i < n + 64; i++) + if (d[i] != 0xfe) + return clib_error_return (err, + "buffer overrun at position %d " + "(n = %u, off = %u, expected 0xfe " + "found 0x%02x%s)", + i, n, off, d[i], is_const ? ", const" : ""); + return err; +} + +static clib_error_t * +test_clib_memcpy_x86_64 (clib_error_t *err) +{ + u8 src[MAX_LEN + 192]; + u8 dst[MAX_LEN + 128]; + + for (int i = 0; i < ARRAY_LEN (src); i++) + src[i] = i & 0x7f; + + for (int j = 0; j < ARRAY_LEN (const_tests); j++) + { + u8 *d = dst + 64; + u8 *s = src + 64; + u16 n = const_tests[j].len; + + for (int i = 0; i < 128 + n; i++) + dst[i] = 0xfe; + const_tests[j].fp (d, s); + if ((err = validate_one (err, d, s, n, 0, /* is_const */ 1))) + return err; + } + + for (u16 n = 1; n <= MAX_LEN; n++) + { + for (int off = 0; off < 64; off += 7) + { + u8 *d = dst + 64 + off; + u8 *s = src + 64; + + for (int i = 0; i < 128 + n + off; i++) + dst[i] = 0xfe; + + wrapper (d, s, n); + + if ((err = validate_one (err, d, s, n, off, /* is_const */ 0))) + return err; + } + } + return err; +} + +REGISTER_TEST (clib_memcpy_x86_64) = { + .name = "clib_memcpy_x86_64", + .fn = test_clib_memcpy_x86_64, +}; +#endif diff --git a/src/vppinfra/test/sha2.c b/src/vppinfra/test/sha2.c new file mode 100644 index 00000000000..9b4abbed657 --- /dev/null +++ b/src/vppinfra/test/sha2.c @@ -0,0 +1,326 @@ +/* SPDX-License-Identifier: Apache-2.0 + * Copyright(c) 2021 Cisco Systems, Inc. + */ + +#include <vppinfra/format.h> +#include <vppinfra/test/test.h> +#include <vppinfra/sha2.h> + +typedef struct +{ + const u8 *msg; + const u8 *key; + int tc; + u32 msg_len; + u32 key_len; + u8 digest_224[28]; + u8 digest_256[32]; + u8 digest_384[48]; + u8 digest_512[64]; + u8 digest_224_len; + u8 digest_256_len; + u8 digest_384_len; + u8 digest_512_len; +} sha2_test_t; + +#ifndef CLIB_MARCH_VARIANT +static const u8 key1[20] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, + 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, + 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b }; +static const u8 key2[4] = "Jefe"; +static const u8 key3[20] = { 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa }; +static const u8 key4[25] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, + 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, + 0x16, 0x17, 0x18, 0x19 }; +static const u8 key5[20] = { 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, + 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, + 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c }; +static const u8 key6[131] = { + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa +}; + +static const u8 msg1[8] = "Hi There"; +static const u8 msg2[28] = "what do ya want for nothing?"; +static const u8 msg3[50] = { + 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, + 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, + 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, + 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd +}; +static const u8 msg4[50] = { + 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, + 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, + 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, + 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd +}; +static const u8 msg6[54] = + "Test Using Larger Than Block-Size Key - Hash Key First"; +static const u8 msg7[153] = + "This is a test using a larger than block-size key and a larger than " + "block-size data. The key needs to be hashed before being used by the " + "HMAC algorithm."; + +const sha2_test_t sha2_tests[] = { + { + /* RFC4231 Test Case 1 */ + .tc = 1, + .key = key1, + .key_len = sizeof (key1), + .msg = msg1, + .msg_len = sizeof (msg1), + .digest_224 = { 0x89, 0x6f, 0xb1, 0x12, 0x8a, 0xbb, 0xdf, 0x19, 0x68, 0x32, + 0x10, 0x7c, 0xd4, 0x9d, 0xf3, 0x3f, 0x47, 0xb4, 0xb1, 0x16, + 0x99, 0x12, 0xba, 0x4f, 0x53, 0x68, 0x4b, 0x22 }, + .digest_256 = { 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53, + 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b, + 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7, + 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7 }, + .digest_384 = { 0xaf, 0xd0, 0x39, 0x44, 0xd8, 0x48, 0x95, 0x62, 0x6b, 0x08, + 0x25, 0xf4, 0xab, 0x46, 0x90, 0x7f, 0x15, 0xf9, 0xda, 0xdb, + 0xe4, 0x10, 0x1e, 0xc6, 0x82, 0xaa, 0x03, 0x4c, 0x7c, 0xeb, + 0xc5, 0x9c, 0xfa, 0xea, 0x9e, 0xa9, 0x07, 0x6e, 0xde, 0x7f, + 0x4a, 0xf1, 0x52, 0xe8, 0xb2, 0xfa, 0x9c, 0xb6 }, + .digest_512 = { 0x87, 0xaa, 0x7c, 0xde, 0xa5, 0xef, 0x61, 0x9d, 0x4f, 0xf0, + 0xb4, 0x24, 0x1a, 0x1d, 0x6c, 0xb0, 0x23, 0x79, 0xf4, 0xe2, + 0xce, 0x4e, 0xc2, 0x78, 0x7a, 0xd0, 0xb3, 0x05, 0x45, 0xe1, + 0x7c, 0xde, 0xda, 0xa8, 0x33, 0xb7, 0xd6, 0xb8, 0xa7, 0x02, + 0x03, 0x8b, 0x27, 0x4e, 0xae, 0xa3, 0xf4, 0xe4, 0xbe, 0x9d, + 0x91, 0x4e, 0xeb, 0x61, 0xf1, 0x70, 0x2e, 0x69, 0x6c, 0x20, + 0x3a, 0x12, 0x68, 0x54 }, + }, + { + /* RFC4231 Test Case 2 */ + .tc = 2, + .key = key2, + .key_len = sizeof (key2), + .msg = msg2, + .msg_len = sizeof (msg2), + .digest_224 = { 0xa3, 0x0e, 0x01, 0x09, 0x8b, 0xc6, 0xdb, 0xbf, 0x45, 0x69, + 0x0f, 0x3a, 0x7e, 0x9e, 0x6d, 0x0f, 0x8b, 0xbe, 0xa2, 0xa3, + 0x9e, 0x61, 0x48, 0x00, 0x8f, 0xd0, 0x5e, 0x44 }, + .digest_256 = { 0x5b, 0xdc, 0xc1, 0x46, 0xbf, 0x60, 0x75, 0x4e, + 0x6a, 0x04, 0x24, 0x26, 0x08, 0x95, 0x75, 0xc7, + 0x5a, 0x00, 0x3f, 0x08, 0x9d, 0x27, 0x39, 0x83, + 0x9d, 0xec, 0x58, 0xb9, 0x64, 0xec, 0x38, 0x43 }, + .digest_384 = { 0xaf, 0x45, 0xd2, 0xe3, 0x76, 0x48, 0x40, 0x31, 0x61, 0x7f, + 0x78, 0xd2, 0xb5, 0x8a, 0x6b, 0x1b, 0x9c, 0x7e, 0xf4, 0x64, + 0xf5, 0xa0, 0x1b, 0x47, 0xe4, 0x2e, 0xc3, 0x73, 0x63, 0x22, + 0x44, 0x5e, 0x8e, 0x22, 0x40, 0xca, 0x5e, 0x69, 0xe2, 0xc7, + 0x8b, 0x32, 0x39, 0xec, 0xfa, 0xb2, 0x16, 0x49 }, + .digest_512 = { 0x16, 0x4b, 0x7a, 0x7b, 0xfc, 0xf8, 0x19, 0xe2, 0xe3, 0x95, + 0xfb, 0xe7, 0x3b, 0x56, 0xe0, 0xa3, 0x87, 0xbd, 0x64, 0x22, + 0x2e, 0x83, 0x1f, 0xd6, 0x10, 0x27, 0x0c, 0xd7, 0xea, 0x25, + 0x05, 0x54, 0x97, 0x58, 0xbf, 0x75, 0xc0, 0x5a, 0x99, 0x4a, + 0x6d, 0x03, 0x4f, 0x65, 0xf8, 0xf0, 0xe6, 0xfd, 0xca, 0xea, + 0xb1, 0xa3, 0x4d, 0x4a, 0x6b, 0x4b, 0x63, 0x6e, 0x07, 0x0a, + 0x38, 0xbc, 0xe7, 0x37 }, + }, + { /* RFC4231 Test Case 3 */ + .tc = 3, + .key = key3, + .key_len = sizeof (key3), + .msg = msg3, + .msg_len = sizeof (msg3), + .digest_224 = { 0x7f, 0xb3, 0xcb, 0x35, 0x88, 0xc6, 0xc1, 0xf6, 0xff, 0xa9, + 0x69, 0x4d, 0x7d, 0x6a, 0xd2, 0x64, 0x93, 0x65, 0xb0, 0xc1, + 0xf6, 0x5d, 0x69, 0xd1, 0xec, 0x83, 0x33, 0xea }, + .digest_256 = { 0x77, 0x3e, 0xa9, 0x1e, 0x36, 0x80, 0x0e, 0x46, + 0x85, 0x4d, 0xb8, 0xeb, 0xd0, 0x91, 0x81, 0xa7, + 0x29, 0x59, 0x09, 0x8b, 0x3e, 0xf8, 0xc1, 0x22, + 0xd9, 0x63, 0x55, 0x14, 0xce, 0xd5, 0x65, 0xfe }, + .digest_384 = { 0x88, 0x06, 0x26, 0x08, 0xd3, 0xe6, 0xad, 0x8a, 0x0a, 0xa2, + 0xac, 0xe0, 0x14, 0xc8, 0xa8, 0x6f, 0x0a, 0xa6, 0x35, 0xd9, + 0x47, 0xac, 0x9f, 0xeb, 0xe8, 0x3e, 0xf4, 0xe5, 0x59, 0x66, + 0x14, 0x4b, 0x2a, 0x5a, 0xb3, 0x9d, 0xc1, 0x38, 0x14, 0xb9, + 0x4e, 0x3a, 0xb6, 0xe1, 0x01, 0xa3, 0x4f, 0x27 }, + .digest_512 = { 0xfa, 0x73, 0xb0, 0x08, 0x9d, 0x56, 0xa2, 0x84, 0xef, 0xb0, + 0xf0, 0x75, 0x6c, 0x89, 0x0b, 0xe9, 0xb1, 0xb5, 0xdb, 0xdd, + 0x8e, 0xe8, 0x1a, 0x36, 0x55, 0xf8, 0x3e, 0x33, 0xb2, 0x27, + 0x9d, 0x39, 0xbf, 0x3e, 0x84, 0x82, 0x79, 0xa7, 0x22, 0xc8, + 0x06, 0xb4, 0x85, 0xa4, 0x7e, 0x67, 0xc8, 0x07, 0xb9, 0x46, + 0xa3, 0x37, 0xbe, 0xe8, 0x94, 0x26, 0x74, 0x27, 0x88, 0x59, + 0xe1, 0x32, 0x92, 0xfb } }, + { + /* RFC4231 Test Case 4 */ + .tc = 4, + .key = key4, + .key_len = sizeof (key4), + .msg = msg4, + .msg_len = sizeof (msg4), + .digest_224 = { 0x6c, 0x11, 0x50, 0x68, 0x74, 0x01, 0x3c, 0xac, 0x6a, 0x2a, + 0xbc, 0x1b, 0xb3, 0x82, 0x62, 0x7c, 0xec, 0x6a, 0x90, 0xd8, + 0x6e, 0xfc, 0x01, 0x2d, 0xe7, 0xaf, 0xec, 0x5a }, + .digest_256 = { 0x82, 0x55, 0x8a, 0x38, 0x9a, 0x44, 0x3c, 0x0e, + 0xa4, 0xcc, 0x81, 0x98, 0x99, 0xf2, 0x08, 0x3a, + 0x85, 0xf0, 0xfa, 0xa3, 0xe5, 0x78, 0xf8, 0x07, + 0x7a, 0x2e, 0x3f, 0xf4, 0x67, 0x29, 0x66, 0x5b }, + .digest_384 = { 0x3e, 0x8a, 0x69, 0xb7, 0x78, 0x3c, 0x25, 0x85, 0x19, 0x33, + 0xab, 0x62, 0x90, 0xaf, 0x6c, 0xa7, 0x7a, 0x99, 0x81, 0x48, + 0x08, 0x50, 0x00, 0x9c, 0xc5, 0x57, 0x7c, 0x6e, 0x1f, 0x57, + 0x3b, 0x4e, 0x68, 0x01, 0xdd, 0x23, 0xc4, 0xa7, 0xd6, 0x79, + 0xcc, 0xf8, 0xa3, 0x86, 0xc6, 0x74, 0xcf, 0xfb }, + .digest_512 = { 0xb0, 0xba, 0x46, 0x56, 0x37, 0x45, 0x8c, 0x69, 0x90, 0xe5, + 0xa8, 0xc5, 0xf6, 0x1d, 0x4a, 0xf7, 0xe5, 0x76, 0xd9, 0x7f, + 0xf9, 0x4b, 0x87, 0x2d, 0xe7, 0x6f, 0x80, 0x50, 0x36, 0x1e, + 0xe3, 0xdb, 0xa9, 0x1c, 0xa5, 0xc1, 0x1a, 0xa2, 0x5e, 0xb4, + 0xd6, 0x79, 0x27, 0x5c, 0xc5, 0x78, 0x80, 0x63, 0xa5, 0xf1, + 0x97, 0x41, 0x12, 0x0c, 0x4f, 0x2d, 0xe2, 0xad, 0xeb, 0xeb, + 0x10, 0xa2, 0x98, 0xdd }, + }, + { + /* RFC4231 Test Case 5 */ + .tc = 5, + .key = key5, + .key_len = sizeof (key5), + .msg = (u8 *) "Test With Truncation", + .msg_len = 20, + .digest_224 = { 0x0e, 0x2a, 0xea, 0x68, 0xa9, 0x0c, 0x8d, 0x37, 0xc9, 0x88, + 0xbc, 0xdb, 0x9f, 0xca, 0x6f, 0xa8 }, + .digest_224_len = 16, + .digest_256 = { 0xa3, 0xb6, 0x16, 0x74, 0x73, 0x10, 0x0e, 0xe0, 0x6e, 0x0c, + 0x79, 0x6c, 0x29, 0x55, 0x55, 0x2b }, + .digest_256_len = 16, + .digest_384 = { 0x3a, 0xbf, 0x34, 0xc3, 0x50, 0x3b, 0x2a, 0x23, 0xa4, 0x6e, + 0xfc, 0x61, 0x9b, 0xae, 0xf8, 0x97 }, + .digest_384_len = 16, + .digest_512 = { 0x41, 0x5f, 0xad, 0x62, 0x71, 0x58, 0x0a, 0x53, 0x1d, 0x41, + 0x79, 0xbc, 0x89, 0x1d, 0x87, 0xa6 }, + .digest_512_len = 16, + }, + { /* RFC4231 Test Case 6 */ + .tc = 6, + .key = key6, + .key_len = sizeof (key6), + .msg = msg6, + .msg_len = sizeof (msg6), + .digest_224 = { 0x95, 0xe9, 0xa0, 0xdb, 0x96, 0x20, 0x95, 0xad, 0xae, 0xbe, + 0x9b, 0x2d, 0x6f, 0x0d, 0xbc, 0xe2, 0xd4, 0x99, 0xf1, 0x12, + 0xf2, 0xd2, 0xb7, 0x27, 0x3f, 0xa6, 0x87, 0x0e }, + .digest_256 = { 0x60, 0xe4, 0x31, 0x59, 0x1e, 0xe0, 0xb6, 0x7f, + 0x0d, 0x8a, 0x26, 0xaa, 0xcb, 0xf5, 0xb7, 0x7f, + 0x8e, 0x0b, 0xc6, 0x21, 0x37, 0x28, 0xc5, 0x14, + 0x05, 0x46, 0x04, 0x0f, 0x0e, 0xe3, 0x7f, 0x54 }, + .digest_384 = { 0x4e, 0xce, 0x08, 0x44, 0x85, 0x81, 0x3e, 0x90, 0x88, 0xd2, + 0xc6, 0x3a, 0x04, 0x1b, 0xc5, 0xb4, 0x4f, 0x9e, 0xf1, 0x01, + 0x2a, 0x2b, 0x58, 0x8f, 0x3c, 0xd1, 0x1f, 0x05, 0x03, 0x3a, + 0xc4, 0xc6, 0x0c, 0x2e, 0xf6, 0xab, 0x40, 0x30, 0xfe, 0x82, + 0x96, 0x24, 0x8d, 0xf1, 0x63, 0xf4, 0x49, 0x52 }, + .digest_512 = { 0x80, 0xb2, 0x42, 0x63, 0xc7, 0xc1, 0xa3, 0xeb, 0xb7, 0x14, + 0x93, 0xc1, 0xdd, 0x7b, 0xe8, 0xb4, 0x9b, 0x46, 0xd1, 0xf4, + 0x1b, 0x4a, 0xee, 0xc1, 0x12, 0x1b, 0x01, 0x37, 0x83, 0xf8, + 0xf3, 0x52, 0x6b, 0x56, 0xd0, 0x37, 0xe0, 0x5f, 0x25, 0x98, + 0xbd, 0x0f, 0xd2, 0x21, 0x5d, 0x6a, 0x1e, 0x52, 0x95, 0xe6, + 0x4f, 0x73, 0xf6, 0x3f, 0x0a, 0xec, 0x8b, 0x91, 0x5a, 0x98, + 0x5d, 0x78, 0x65, 0x98 } }, + { + /* RFC4231 Test Case 7 */ + .tc = 7, + .key = key6, + .key_len = sizeof (key6), + .msg = msg7, + .msg_len = sizeof (msg7) - 1, + .digest_224 = { 0x3a, 0x85, 0x41, 0x66, 0xac, 0x5d, 0x9f, 0x02, 0x3f, 0x54, + 0xd5, 0x17, 0xd0, 0xb3, 0x9d, 0xbd, 0x94, 0x67, 0x70, 0xdb, + 0x9c, 0x2b, 0x95, 0xc9, 0xf6, 0xf5, 0x65, 0xd1 }, + .digest_256 = { 0x9b, 0x09, 0xff, 0xa7, 0x1b, 0x94, 0x2f, 0xcb, + 0x27, 0x63, 0x5f, 0xbc, 0xd5, 0xb0, 0xe9, 0x44, + 0xbf, 0xdc, 0x63, 0x64, 0x4f, 0x07, 0x13, 0x93, + 0x8a, 0x7f, 0x51, 0x53, 0x5c, 0x3a, 0x35, 0xe2 }, + .digest_384 = { 0x66, 0x17, 0x17, 0x8e, 0x94, 0x1f, 0x02, 0x0d, 0x35, 0x1e, + 0x2f, 0x25, 0x4e, 0x8f, 0xd3, 0x2c, 0x60, 0x24, 0x20, 0xfe, + 0xb0, 0xb8, 0xfb, 0x9a, 0xdc, 0xce, 0xbb, 0x82, 0x46, 0x1e, + 0x99, 0xc5, 0xa6, 0x78, 0xcc, 0x31, 0xe7, 0x99, 0x17, 0x6d, + 0x38, 0x60, 0xe6, 0x11, 0x0c, 0x46, 0x52, 0x3e }, + .digest_512 = { 0xe3, 0x7b, 0x6a, 0x77, 0x5d, 0xc8, 0x7d, 0xba, 0xa4, 0xdf, + 0xa9, 0xf9, 0x6e, 0x5e, 0x3f, 0xfd, 0xde, 0xbd, 0x71, 0xf8, + 0x86, 0x72, 0x89, 0x86, 0x5d, 0xf5, 0xa3, 0x2d, 0x20, 0xcd, + 0xc9, 0x44, 0xb6, 0x02, 0x2c, 0xac, 0x3c, 0x49, 0x82, 0xb1, + 0x0d, 0x5e, 0xeb, 0x55, 0xc3, 0xe4, 0xde, 0x15, 0x13, 0x46, + 0x76, 0xfb, 0x6d, 0xe0, 0x44, 0x60, 0x65, 0xc9, 0x74, 0x40, + 0xfa, 0x8c, 0x6a, 0x58 }, + }, + {} +}; +#else +extern const sha2_test_t sha2_tests[]; +#endif + +static clib_error_t * +check_digest (clib_error_t *err, int tc, u8 *calculated, const u8 *expected, + u8 len) +{ + if (memcmp (expected, calculated, len) != 0) + err = clib_error_return (err, + "Bad HMAC SHA%u digest for test case " + "%u:\nExpected:\n%U\nCalculated:\n%U\n", + len * 8, tc, format_hexdump, expected, len, + format_hexdump, calculated, len); + return err; +} + +#define _(bits) \ + static clib_error_t *test_clib_hmac_sha##bits (clib_error_t *err) \ + { \ + u8 digest[64]; \ + const sha2_test_t *t = sha2_tests; \ + \ + while (t->key) \ + { \ + u8 digest_len = t->digest_##bits##_len; \ + if (digest_len == 0) \ + digest_len = sizeof (t->digest_##bits); \ + clib_memset_u8 (digest, 0xfe, sizeof (digest)); \ + clib_hmac_sha##bits (t->key, t->key_len, t->msg, t->msg_len, digest); \ + if ((err = check_digest (err, t->tc, digest, t->digest_##bits, \ + digest_len))) \ + return err; \ + t++; \ + } \ + \ + return err; \ + } \ + \ + void __test_perf_fn perftest_sha##bits##_byte (test_perf_t *tp) \ + { \ + volatile uword *np = &tp->n_ops; \ + volatile uword *kl = &tp->arg0; \ + ; \ + u8 *key = test_mem_alloc_and_fill_inc_u8 (*kl, 32, 0); \ + u8 *data = test_mem_alloc_and_fill_inc_u8 (*np, 0, 0); \ + u8 *digest = test_mem_alloc (64); \ + \ + test_perf_event_enable (tp); \ + clib_hmac_sha##bits (key, *kl, data, *np, digest); \ + test_perf_event_disable (tp); \ + \ + test_mem_free (key); \ + test_mem_free (data); \ + test_mem_free (digest); \ + } \ + REGISTER_TEST (clib_hmac_sha##bits) = { \ + .name = "clib_hmac_sha" #bits, \ + .fn = test_clib_hmac_sha##bits, \ + .perf_tests = PERF_TESTS ({ .name = "byte", \ + .n_ops = 16384, \ + .arg0 = 20, \ + .fn = perftest_sha##bits##_byte }) \ + } + +_ (224); +_ (256); +_ (384); +_ (512); +#undef _ diff --git a/src/vppinfra/test/test.c b/src/vppinfra/test/test.c new file mode 100644 index 00000000000..15ea6fd2973 --- /dev/null +++ b/src/vppinfra/test/test.c @@ -0,0 +1,249 @@ +/* SPDX-License-Identifier: Apache-2.0 + * Copyright(c) 2021 Cisco Systems, Inc. + */ + +#include <vppinfra/format.h> +#include <vppinfra/test/test.h> +#include <vppinfra/error.h> + +test_main_t test_main; + +int +test_march_supported (clib_march_variant_type_t type) +{ +#define _(s, n) \ + if (CLIB_MARCH_VARIANT_TYPE_##s == type) \ + return clib_cpu_march_priority_##s (); + foreach_march_variant +#undef _ + return 0; +} + +clib_error_t * +test_funct (test_main_t *tm) +{ + for (int i = 0; i < CLIB_MARCH_TYPE_N_VARIANTS; i++) + { + test_registration_t *r = tm->registrations[i]; + + if (r == 0 || test_march_supported (i) < 0) + continue; + + fformat (stdout, "\nMultiarch Variant: %U\n", format_march_variant, i); + fformat (stdout, + "-------------------------------------------------------\n"); + while (r) + { + clib_error_t *err; + if (tm->filter && strstr (r->name, (char *) tm->filter) == 0) + goto next; + err = (r->fn) (0); + fformat (stdout, "%-50s %s\n", r->name, err ? "FAIL" : "PASS"); + if (err) + { + clib_error_report (err); + fformat (stdout, "\n"); + } + next: + r = r->next; + } + } + + fformat (stdout, "\n"); + return 0; +} + +#if 0 +static u8 * +format_test_perf_bundle_core_power (u8 *s, va_list *args) +{ + test_perf_event_bundle_t __clib_unused *b = + va_arg (*args, test_perf_event_bundle_t *); + test_perf_t __clib_unused *tp = va_arg (*args, test_perf_t *); + u64 *data = va_arg (*args, u64 *); + + if (data) + s = format (s, "%7.1f %%", (f64) 100 * data[1] / data[0]); + else + s = format (s, "%9s", "Level 0"); + + if (data) + s = format (s, "%8.1f %%", (f64) 100 * data[2] / data[0]); + else + s = format (s, "%9s", "Level 1"); + + if (data) + s = format (s, "%7.1f %%", (f64) 100 * data[3] / data[0]); + else + s = format (s, "%9s", "Level 2"); + + return s; +} + +#ifdef __x86_64__ +#define PERF_INTEL_CODE(event, umask) ((event) | (umask) << 8) + , + { + .name = "core-power", + .desc = + "Core cycles where the core was running under specific turbo schedule.", + .type = PERF_TYPE_RAW, + .config[0] = PERF_INTEL_CODE (0x3c, 0x00), + .config[1] = PERF_INTEL_CODE (0x28, 0x07), + .config[2] = PERF_INTEL_CODE (0x28, 0x18), + .config[3] = PERF_INTEL_CODE (0x28, 0x20), + .config[4] = PERF_INTEL_CODE (0x28, 0x40), + .n_events = 5, + .format_fn = format_test_perf_bundle_core_power, + } +#endif +}; +#endif + +#ifdef __linux__ +clib_error_t * +test_perf (test_main_t *tm) +{ + clib_error_t *err = 0; + clib_perfmon_ctx_t _ctx, *ctx = &_ctx; + + if ((err = clib_perfmon_init_by_bundle_name ( + ctx, "%s", tm->bundle ? (char *) tm->bundle : "default"))) + return err; + + fformat (stdout, "Warming up...\n"); + clib_perfmon_warmup (ctx); + + for (int i = 0; i < CLIB_MARCH_TYPE_N_VARIANTS; i++) + { + test_registration_t *r = tm->registrations[i]; + + if (r == 0 || test_march_supported (i) < 0) + continue; + + fformat (stdout, "\nMultiarch Variant: %U\n", format_march_variant, i); + fformat (stdout, + "-------------------------------------------------------\n"); + while (r) + { + if (r->perf_tests) + { + test_perf_t *pt = r->perf_tests; + if (tm->filter && strstr (r->name, (char *) tm->filter) == 0) + goto next; + + clib_perfmon_capture_group (ctx, "%s", r->name); + do + { + for (int i = 0; i < tm->repeat; i++) + { + pt->fd = ctx->group_fd; + clib_perfmon_reset (ctx); + pt->fn (pt); + clib_perfmon_capture (ctx, pt->n_ops, "%0s", pt->name); + } + } + while ((++pt)->fn); + } + next: + r = r->next; + } + fformat (stdout, "%U\n", format_perfmon_bundle, ctx); + clib_perfmon_clear (ctx); + } + + clib_perfmon_free (ctx); + return err; +} +#endif + +int +main (int argc, char *argv[]) +{ + test_main_t *tm = &test_main; + unformat_input_t _i = {}, *i = &_i; + clib_mem_init (0, 64ULL << 20); + clib_error_t *err; + int perf = 0; + + /* defaults */ + tm->repeat = 3; + + unformat_init_command_line (i, argv); + + while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) + { + if (unformat (i, "perf")) + perf = 1; + else if (unformat (i, "filter %s", &tm->filter)) + ; + else if (unformat (i, "bundle %s", &tm->bundle)) + ; + else if (unformat (i, "repeat %d", &tm->repeat)) + ; + else + { + clib_warning ("unknown input '%U'", format_unformat_error, i); + exit (1); + } + } + + if (perf) + err = test_perf (tm); + else + err = test_funct (tm); + + if (err) + { + clib_error_report (err); + fformat (stderr, "\n"); + return 1; + } + return 0; +} + +void * +test_mem_alloc (uword size) +{ + void *rv; + size = round_pow2 (size, CLIB_CACHE_LINE_BYTES); + rv = clib_mem_alloc_aligned (size, CLIB_CACHE_LINE_BYTES); + clib_memset_u8 (rv, 0, size); + return rv; +} + +void * +test_mem_alloc_and_fill_inc_u8 (uword size, u8 start, u8 mask) +{ + u8 *rv; + mask = mask ? mask : 0xff; + size = round_pow2 (size, CLIB_CACHE_LINE_BYTES); + rv = clib_mem_alloc_aligned (size, CLIB_CACHE_LINE_BYTES); + for (uword i = 0; i < size; i++) + rv[i] = ((u8) i + start) & mask; + return rv; +} + +void * +test_mem_alloc_and_splat (uword elt_size, uword n_elts, void *elt) +{ + u8 *rv, *e; + uword data_size = elt_size * n_elts; + uword alloc_size = round_pow2 (data_size, CLIB_CACHE_LINE_BYTES); + e = rv = clib_mem_alloc_aligned (alloc_size, CLIB_CACHE_LINE_BYTES); + while (e - rv < data_size) + { + clib_memcpy_fast (e, elt, elt_size); + e += elt_size; + } + + if (data_size < alloc_size) + clib_memset_u8 (e, 0, alloc_size - data_size); + return rv; +} + +void +test_mem_free (void *p) +{ + clib_mem_free (p); +} diff --git a/src/vppinfra/test/test.h b/src/vppinfra/test/test.h new file mode 100644 index 00000000000..7d54d80c6ec --- /dev/null +++ b/src/vppinfra/test/test.h @@ -0,0 +1,107 @@ +/* SPDX-License-Identifier: Apache-2.0 + * Copyright(c) 2021 Cisco Systems, Inc. + */ + +#ifndef included_test_test_h +#define included_test_test_h + +#include <vppinfra/cpu.h> +#include <vppinfra/perfmon/perfmon.h> +#ifdef __linux__ +#include <sys/ioctl.h> +#include <linux/perf_event.h> +#endif + +typedef clib_error_t *(test_fn_t) (clib_error_t *); + +struct test_perf_; +typedef void (test_perf_fn_t) (struct test_perf_ *tp); + +typedef struct test_perf_ +{ + int fd; + u64 n_ops; + union + { + u64 arg0; + void *ptr0; + }; + union + { + u64 arg1; + void *ptr1; + }; + union + { + u64 arg2; + void *ptr2; + }; + char *name; + test_perf_fn_t *fn; +} test_perf_t; + +typedef struct test_registration_ +{ + char *name; + u8 multiarch : 1; + test_fn_t *fn; + test_perf_t *perf_tests; + u32 n_perf_tests; + struct test_registration_ *next; +} test_registration_t; + +typedef struct +{ + test_registration_t *registrations[CLIB_MARCH_TYPE_N_VARIANTS]; + u32 repeat; + u8 *filter; + u8 *bundle; + f64 ref_clock; +} test_main_t; +extern test_main_t test_main; + +#define __test_funct_fn \ + static __clib_noinline __clib_noclone __clib_section (".test_func") +#define __test_perf_fn \ + static __clib_noinline __clib_noclone __clib_section (".test_perf") + +#define REGISTER_TEST(x) \ + test_registration_t CLIB_MARCH_SFX (__test_##x); \ + static void __clib_constructor CLIB_MARCH_SFX (__test_registration_##x) ( \ + void) \ + { \ + test_registration_t *r = &CLIB_MARCH_SFX (__test_##x); \ + r->next = \ + test_main.registrations[CLIB_MARCH_SFX (CLIB_MARCH_VARIANT_TYPE)]; \ + test_main.registrations[CLIB_MARCH_SFX (CLIB_MARCH_VARIANT_TYPE)] = r; \ + } \ + test_registration_t CLIB_MARCH_SFX (__test_##x) + +#define PERF_TESTS(...) \ + (test_perf_t[]) \ + { \ + __VA_ARGS__, {} \ + } + +static_always_inline void +test_perf_event_reset (test_perf_t *t) +{ + clib_perfmon_ioctl (t->fd, PERF_EVENT_IOC_RESET); +} +static_always_inline void +test_perf_event_enable (test_perf_t *t) +{ + clib_perfmon_ioctl (t->fd, PERF_EVENT_IOC_ENABLE); +} +static_always_inline void +test_perf_event_disable (test_perf_t *t) +{ + clib_perfmon_ioctl (t->fd, PERF_EVENT_IOC_DISABLE); +} + +void *test_mem_alloc (uword size); +void *test_mem_alloc_and_fill_inc_u8 (uword size, u8 start, u8 mask); +void *test_mem_alloc_and_splat (uword elt_size, uword n_elts, void *elt); +void test_mem_free (void *p); + +#endif diff --git a/src/vppinfra/test/toeplitz.c b/src/vppinfra/test/toeplitz.c new file mode 100644 index 00000000000..c1bd13fae8c --- /dev/null +++ b/src/vppinfra/test/toeplitz.c @@ -0,0 +1,553 @@ +/* SPDX-License-Identifier: Apache-2.0 + * Copyright(c) 2021 Cisco Systems, Inc. + */ + +#include <vppinfra/format.h> +#include <vppinfra/test/test.h> +#include <vppinfra/vector/toeplitz.h> + +/* secret key and test cases taken from: + * https://docs.microsoft.com/en-us/windows-hardware/drivers/network/verifying-the-rss-hash-calculation + */ + +typedef struct +{ + u32 sip, dip; + u16 sport, dport; +} __clib_packed ip4_key_t; + +typedef struct +{ + ip4_key_t key; + u32 hash_2t, hash_4t; +} ip4_test_t; + +typedef struct +{ + u16 sip[8], dip[8]; + u16 sport, dport; +} __clib_packed ip6_key_t; + +typedef struct +{ + ip6_key_t key; + u32 hash_2t, hash_4t; +} ip6_test_t; + +#define N_IP4_TESTS 5 +#define N_IP6_TESTS 3 +#define N_LENGTH_TESTS 240 + +#ifndef CLIB_MARCH_VARIANT +#define _IP4(a, b, c, d) ((d) << 24 | (c) << 16 | (b) << 8 | (a)) +#define _IP6(a, b, c, d, e, f, g, h) \ + { \ + (u16) ((a) << 8) | (u8) ((a) >> 8), (u16) ((b) << 8) | (u8) ((b) >> 8), \ + (u16) ((c) << 8) | (u8) ((c) >> 8), (u16) ((d) << 8) | (u8) ((d) >> 8), \ + (u16) ((e) << 8) | (u8) ((e) >> 8), (u16) ((f) << 8) | (u8) ((f) >> 8), \ + (u16) ((g) << 8) | (u8) ((g) >> 8), (u16) ((h) << 8) | (u8) ((h) >> 8), \ + } +#define _PORT(a) ((a) >> 8 | (((a) &0xff) << 8)) + +const ip4_test_t ip4_tests[N_IP4_TESTS] = { + /* ipv4 tests */ + { + .key.sip = _IP4 (66, 9, 149, 187), + .key.dip = _IP4 (161, 142, 100, 80), + .key.sport = _PORT (2794), + .key.dport = _PORT (1766), + .hash_2t = 0x323e8fc2, + .hash_4t = 0x51ccc178, + }, + { + .key.sip = _IP4 (199, 92, 111, 2), + .key.dip = _IP4 (65, 69, 140, 83), + .key.sport = _PORT (14230), + .key.dport = _PORT (4739), + .hash_2t = 0xd718262a, + .hash_4t = 0xc626b0ea, + }, + { + .key.sip = _IP4 (24, 19, 198, 95), + .key.dip = _IP4 (12, 22, 207, 184), + .key.sport = _PORT (12898), + .key.dport = _PORT (38024), + .hash_2t = 0xd2d0a5de, + .hash_4t = 0x5c2b394a, + }, + { + .key.sip = _IP4 (38, 27, 205, 30), + .key.dip = _IP4 (209, 142, 163, 6), + .key.sport = _PORT (48228), + .key.dport = _PORT (2217), + .hash_2t = 0x82989176, + .hash_4t = 0xafc7327f, + }, + { + .key.sip = _IP4 (153, 39, 163, 191), + .key.dip = _IP4 (202, 188, 127, 2), + .key.sport = _PORT (44251), + .key.dport = _PORT (1303), + .hash_2t = 0x5d1809c5, + .hash_4t = 0x10e828a2, + } +}; + +const ip6_test_t ip6_tests[N_IP6_TESTS] = { + { + .key.sip = _IP6 (0x3ffe, 0x2501, 0x200, 0x1fff, 0, 0, 0, 7), + .key.dip = _IP6 (0x3ffe, 0x2501, 0x200, 3, 0, 0, 0, 1), + .key.sport = _PORT (2794), + .key.dport = _PORT (1766), + .hash_2t = 0x2cc18cd5, + .hash_4t = 0x40207d3d, + }, + { + .key.sip = _IP6 (0x3ffe, 0x501, 8, 0, 0x260, 0x97ff, 0xfe40, 0xefab), + .key.dip = _IP6 (0xff02, 0, 0, 0, 0, 0, 0, 1), + .key.sport = _PORT (14230), + .key.dport = _PORT (4739), + .hash_2t = 0x0f0c461c, + .hash_4t = 0xdde51bbf, + }, + { + .key.sip = _IP6 (0x3ffe, 0x1900, 0x4545, 3, 0x200, 0xf8ff, 0xfe21, 0x67cf), + .key.dip = _IP6 (0xfe80, 0, 0, 0, 0x200, 0xf8ff, 0xfe21, 0x67cf), + .key.sport = _PORT (44251), + .key.dport = _PORT (38024), + .hash_2t = 0x4b61e985, + .hash_4t = 0x02d1feef, + } +}; + +const u32 length_test_hashes[N_LENGTH_TESTS] = { + 0x00000000, 0x00000000, 0x2b6d12ad, 0x9de4446e, 0x061f00bf, 0xad7ed8f7, + 0x4bc7b068, 0x231fc545, 0xdbd97a33, 0xcdab29e7, 0x2d665c0c, 0x31e28ed7, + 0x14e19218, 0x5aa89f0f, 0xd47de07f, 0x355ec712, 0x7e1cbfc0, 0xf84de19d, + 0xbcf66bd3, 0x104086c6, 0x71900b34, 0xcd2f9819, 0xeae68ebb, 0x54d63b4c, + 0x5f865a2c, 0x9d6ded08, 0xe00b0912, 0x3fcf07a6, 0x3bd9ca93, 0x3f4f3bbb, + 0xd0b82624, 0xa28a08e1, 0xa585969f, 0x0c8f4a71, 0x5dce7bdd, 0x4fcf2a6d, + 0x91c89ae9, 0xbef8a24d, 0x8e3d30fe, 0xc8027848, 0xc1e7e513, 0xa12bd3d9, + 0x46700bb4, 0xc6339dab, 0x970805ad, 0xfcb50ac8, 0xc6db4f44, 0x792e2987, + 0xacfb7836, 0xa25ec529, 0x957d7beb, 0x6732809a, 0x891836ed, 0xeefb83b2, + 0xca96b40b, 0x93fd5abd, 0x9076f922, 0x59adb4eb, 0x9705aafb, 0x282719b1, + 0xdda9cb8a, 0x3f499131, 0x47491130, 0x30ef0759, 0xad1cf855, 0x428aa312, + 0x4200240a, 0x71a72857, 0x16b30c36, 0x10cca9a3, 0x166f091e, 0x30e00560, + 0x8acd20ba, 0xfa633d76, 0x0fe32eb7, 0xdcc0122f, 0x20aa8ab0, 0x62b2a9af, + 0x7a6c80a6, 0x27e87268, 0x95b797a8, 0x25d18ccd, 0x68a7fb00, 0xc54bcdad, + 0x3bd0e717, 0xf0df54c9, 0x780daadf, 0x7b435605, 0x150c1e10, 0x8a892e54, + 0x9d27cb25, 0xe23383a5, 0x57aac408, 0x83b8abf8, 0x560f33af, 0xd5cb3307, + 0x79ae8edc, 0x9b127665, 0x320f18bd, 0x385d636b, 0xbd1b2dbf, 0x97679888, + 0x738894a4, 0xeba2afb0, 0xfa7c2d50, 0xb6741aa1, 0x28922bba, 0x7783242b, + 0xa694cca2, 0xa32781c0, 0x696cd670, 0xa714d72f, 0xea34d35a, 0xc5aed81e, + 0x0438433a, 0xc1939ab2, 0xb51c123a, 0x121426b9, 0x1add93ba, 0x50c56b6a, + 0x7e90902a, 0xae3abd85, 0x2f7a0088, 0xb45cf6f9, 0x80070094, 0x8bd46467, + 0xdfd1b762, 0x0bb25856, 0x48eefe84, 0x0989dbb9, 0xfc32472b, 0x965fec6b, + 0x5a256bd0, 0x6df7127a, 0x7856d0d6, 0xedc82bd3, 0x1b563b96, 0xc73eace7, + 0xba4c0a93, 0xdfd6dd97, 0x923c41db, 0x14926ca6, 0x22e52ab1, 0x22852a66, + 0x79606b9c, 0xb0f22b23, 0xb46354ba, 0x9c3cd931, 0x03a92bd6, 0x84000834, + 0x5425df65, 0xf4dd3fc9, 0x391cc873, 0xa560b52e, 0x828037d9, 0x31323dd5, + 0x5c6e3147, 0x28e21f85, 0xa431eb51, 0xf468c4a3, 0x9bea1d2e, 0x43d9109c, + 0x5bb9b081, 0xe0825675, 0xc9c92591, 0xd29fc812, 0x03136bc9, 0x5e005a1f, + 0x6d821ed8, 0x3f0bfcc4, 0x24774162, 0x893bde94, 0x6475efea, 0x6711538e, + 0xc4755f6d, 0x9425ebe2, 0xacf471b4, 0xb947ab0c, 0x1f78c455, 0x372b3ed7, + 0xb3ec24d7, 0x18c4459f, 0xa8ff3695, 0xe4aa2b85, 0x8a52ad7e, 0xe05e8177, + 0x7aa348ed, 0x3e4ac6aa, 0x17dcf8a5, 0x93b933b0, 0x8f7413ec, 0xc77bfe61, + 0xfdb72874, 0x4370f138, 0xdf3462ad, 0xc8970a59, 0xb4a9fed8, 0xa2ddc39b, + 0xd61db62a, 0x95c5fc1b, 0x7b22e6e0, 0x1969702c, 0x7992aebb, 0x59d7c225, + 0x0e16db0b, 0x9f2afc21, 0x246cf66b, 0xb3d6569d, 0x29c532d7, 0xe155747a, + 0xe38d7872, 0xea704969, 0xb69095b0, 0x1b198efd, 0x55daab76, 0xa2a377b6, + 0xb31aa2fa, 0x48b73c41, 0xf0cc501a, 0x9c9ca831, 0x1b591b99, 0xb2d8d22f, + 0xab4b5f69, 0x4fe00e71, 0xdf5480bd, 0x982540d7, 0x7f34ea4f, 0xd7be66e1, + 0x9d2ab1ba, 0x1ba62e12, 0xee3fb36c, 0xf28d7c5a, 0x756311eb, 0xc68567f2, + 0x7b6ea177, 0xc398d9f3 +}; + +#else +extern const ip4_test_t ip4_tests[N_IP4_TESTS]; +extern const ip6_test_t ip6_tests[N_IP6_TESTS]; +extern const u32 length_test_hashes[N_LENGTH_TESTS]; +#endif + +__test_funct_fn u32 +wrapper (clib_toeplitz_hash_key_t *k, u8 *data, u32 n_bytes) +{ + return clib_toeplitz_hash (k, data, n_bytes); +} + +__test_funct_fn void +wrapper_x4 (clib_toeplitz_hash_key_t *k, u8 *d0, u8 *d1, u8 *d2, u8 *d3, + u32 *h0, u32 *h1, u32 *h2, u32 *h3, u32 n_bytes) +{ + clib_toeplitz_hash_x4 (k, d0, d1, d2, d3, h0, h1, h2, h3, n_bytes); +} + +static clib_error_t * +test_clib_toeplitz_hash (clib_error_t *err) +{ + u32 r; + int n_key_copies, bigkey_len, bigdata_len; + u8 *bigkey, *bigdata; + clib_toeplitz_hash_key_t *k; + + k = clib_toeplitz_hash_key_init (0, 0); + + for (int i = 0; i < N_IP4_TESTS; i++) + { + r = wrapper (k, (u8 *) &ip4_tests[i].key, 8); + if (ip4_tests[i].hash_2t != r) + return clib_error_return (err, + "wrong IPv4 2 tuple hash for test %u, " + "calculated 0x%08x expected 0x%08x", + i, ip4_tests[i].hash_2t, r); + + r = wrapper (k, (u8 *) &ip4_tests[i].key, 12); + if (ip4_tests[i].hash_4t != r) + return clib_error_return (err, + "wrong IPv4 4 tuple hash for test %u, " + "calculated 0x%08x expected 0x%08x", + i, ip4_tests[i].hash_4t, r); + } + + for (int i = 0; i < N_IP6_TESTS; i++) + { + r = wrapper (k, (u8 *) &ip6_tests[i].key, 32); + if (ip6_tests[i].hash_2t != r) + return clib_error_return (err, + "wrong IPv6 2 tuple hash for test %u, " + "calculated 0x%08x expected 0x%08x", + i, ip6_tests[i].hash_2t, r); + + r = wrapper (k, (u8 *) &ip6_tests[i].key, 36); + if (ip6_tests[i].hash_4t != r) + return clib_error_return (err, + "wrong IPv6 4 tuple hash for test %u, " + "calculated 0x%08x expected 0x%08x", + i, ip6_tests[i].hash_4t, r); + } + + n_key_copies = 6; + bigkey_len = k->key_length * n_key_copies; + bigdata_len = bigkey_len - 4; + bigkey = test_mem_alloc_and_splat (k->key_length, n_key_copies, k->data); + bigdata = test_mem_alloc_and_fill_inc_u8 (bigdata_len, 0, 0); + u32 key_len = k->key_length; + + clib_toeplitz_hash_key_free (k); + k = clib_toeplitz_hash_key_init (bigkey, n_key_copies * key_len); + + for (int i = 0; i < N_LENGTH_TESTS - 4; i++) + { + r = wrapper (k, bigdata, i); + if (length_test_hashes[i] != r) + { + err = clib_error_return (err, + "wrong length test hash for length %u, " + "calculated 0x%08x expected 0x%08x " + "xor 0x%08x", + i, r, length_test_hashes[i], + r ^ length_test_hashes[i]); + goto done; + } + } + +done: + clib_toeplitz_hash_key_free (k); + test_mem_free (bigkey); + test_mem_free (bigdata); + return err; +} + +void __test_perf_fn +perftest_fixed_12byte (test_perf_t *tp) +{ + u32 n = tp->n_ops; + u8 *data = test_mem_alloc_and_splat (12, n, (void *) &ip4_tests[0].key); + u8 *res = test_mem_alloc (4 * n); + clib_toeplitz_hash_key_t *k = clib_toeplitz_hash_key_init (0, 0); + + test_perf_event_enable (tp); + for (int i = 0; i < n; i++) + ((u32 *) res)[i] = clib_toeplitz_hash (k, data + i * 12, 12); + test_perf_event_disable (tp); + + clib_toeplitz_hash_key_free (k); + test_mem_free (data); + test_mem_free (res); +} + +void __test_perf_fn +perftest_fixed_36byte (test_perf_t *tp) +{ + u32 n = tp->n_ops; + u8 *data = test_mem_alloc_and_splat (36, n, (void *) &ip6_tests[0].key); + u8 *res = test_mem_alloc (4 * n); + clib_toeplitz_hash_key_t *k = clib_toeplitz_hash_key_init (0, 0); + + test_perf_event_enable (tp); + for (int i = 0; i < n; i++) + ((u32 *) res)[i] = clib_toeplitz_hash (k, data + i * 36, 36); + test_perf_event_disable (tp); + + clib_toeplitz_hash_key_free (k); + test_mem_free (data); + test_mem_free (res); +} + +void __test_perf_fn +perftest_variable_size (test_perf_t *tp) +{ + u32 key_len, n_keys, n = tp->n_ops; + u8 *key, *data = test_mem_alloc (n); + u32 *res = test_mem_alloc (sizeof (u32)); + clib_toeplitz_hash_key_t *k = clib_toeplitz_hash_key_init (0, 0); + + k = clib_toeplitz_hash_key_init (0, 0); + key_len = k->key_length; + n_keys = ((n + 4) / k->key_length) + 1; + key = test_mem_alloc_and_splat (n_keys, key_len, k->data); + clib_toeplitz_hash_key_free (k); + k = clib_toeplitz_hash_key_init (key, key_len * n_keys); + + test_perf_event_enable (tp); + res[0] = clib_toeplitz_hash (k, data, n); + test_perf_event_disable (tp); + + clib_toeplitz_hash_key_free (k); + test_mem_free (data); + test_mem_free (res); + test_mem_free (key); +} + +REGISTER_TEST (clib_toeplitz_hash) = { + .name = "clib_toeplitz_hash", + .fn = test_clib_toeplitz_hash, + .perf_tests = PERF_TESTS ({ .name = "fixed (per 12 byte tuple)", + .n_ops = 1024, + .fn = perftest_fixed_12byte }, + { .name = "fixed (per 36 byte tuple)", + .n_ops = 1024, + .fn = perftest_fixed_36byte }, + { .name = "variable size (per byte)", + .n_ops = 16384, + .fn = perftest_variable_size }), +}; + +static clib_error_t * +test_clib_toeplitz_hash_x4 (clib_error_t *err) +{ + u32 r[4]; + int n_key_copies, bigkey_len, bigdata_len; + u8 *bigkey, *bigdata0, *bigdata1, *bigdata2, *bigdata3; + clib_toeplitz_hash_key_t *k; + + k = clib_toeplitz_hash_key_init (0, 0); + + wrapper_x4 (k, (u8 *) &ip4_tests[0].key, (u8 *) &ip4_tests[1].key, + (u8 *) &ip4_tests[2].key, (u8 *) &ip4_tests[3].key, r, r + 1, + r + 2, r + 3, 8); + + if (ip4_tests[0].hash_2t != r[0] || ip4_tests[1].hash_2t != r[1] || + ip4_tests[2].hash_2t != r[2] || ip4_tests[3].hash_2t != r[3]) + return clib_error_return (err, + "wrong IPv4 2 tuple x4 hash " + "calculated { 0x%08x, 0x%08x, 0x%08x, 0x%08x } " + "expected { 0x%08x, 0x%08x, 0x%08x, 0x%08x }", + ip4_tests[0].hash_2t, ip4_tests[1].hash_2t, + ip4_tests[2].hash_2t, ip4_tests[3].hash_2t, r[0], + r[1], r[2], r[3]); + + wrapper_x4 (k, (u8 *) &ip4_tests[0].key, (u8 *) &ip4_tests[1].key, + (u8 *) &ip4_tests[2].key, (u8 *) &ip4_tests[3].key, r, r + 1, + r + 2, r + 3, 12); + + if (ip4_tests[0].hash_4t != r[0] || ip4_tests[1].hash_4t != r[1] || + ip4_tests[2].hash_4t != r[2] || ip4_tests[3].hash_4t != r[3]) + return clib_error_return (err, + "wrong IPv4 4 tuple x4 hash " + "calculated { 0x%08x, 0x%08x, 0x%08x, 0x%08x } " + "expected { 0x%08x, 0x%08x, 0x%08x, 0x%08x }", + ip4_tests[0].hash_4t, ip4_tests[1].hash_4t, + ip4_tests[2].hash_4t, ip4_tests[3].hash_4t, r[0], + r[1], r[2], r[3]); + + wrapper_x4 (k, (u8 *) &ip6_tests[0].key, (u8 *) &ip6_tests[1].key, + (u8 *) &ip6_tests[2].key, (u8 *) &ip6_tests[0].key, r, r + 1, + r + 2, r + 3, 32); + + if (ip6_tests[0].hash_2t != r[0] || ip6_tests[1].hash_2t != r[1] || + ip6_tests[2].hash_2t != r[2] || ip6_tests[0].hash_2t != r[3]) + return clib_error_return (err, + "wrong IPv6 2 tuple x4 hash " + "calculated { 0x%08x, 0x%08x, 0x%08x, 0x%08x } " + "expected { 0x%08x, 0x%08x, 0x%08x, 0x%08x }", + ip6_tests[0].hash_2t, ip6_tests[1].hash_2t, + ip6_tests[2].hash_2t, ip6_tests[0].hash_2t, r[0], + r[1], r[2], r[3]); + + wrapper_x4 (k, (u8 *) &ip6_tests[0].key, (u8 *) &ip6_tests[1].key, + (u8 *) &ip6_tests[2].key, (u8 *) &ip6_tests[0].key, r, r + 1, + r + 2, r + 3, 36); + + if (ip6_tests[0].hash_4t != r[0] || ip6_tests[1].hash_4t != r[1] || + ip6_tests[2].hash_4t != r[2] || ip6_tests[0].hash_4t != r[3]) + return clib_error_return (err, + "wrong IPv6 4 tuple x4 hash " + "calculated { 0x%08x, 0x%08x, 0x%08x, 0x%08x } " + "expected { 0x%08x, 0x%08x, 0x%08x, 0x%08x }", + ip6_tests[0].hash_4t, ip6_tests[1].hash_4t, + ip6_tests[2].hash_4t, ip6_tests[0].hash_4t, r[0], + r[1], r[2], r[3]); + + n_key_copies = 6; + bigkey_len = k->key_length * n_key_copies; + bigdata_len = bigkey_len - 4; + bigkey = test_mem_alloc_and_splat (k->key_length, n_key_copies, k->data); + bigdata0 = test_mem_alloc_and_fill_inc_u8 (bigdata_len, 0, 0); + bigdata1 = test_mem_alloc_and_fill_inc_u8 (bigdata_len, 0, 0); + bigdata2 = test_mem_alloc_and_fill_inc_u8 (bigdata_len, 0, 0); + bigdata3 = test_mem_alloc_and_fill_inc_u8 (bigdata_len, 0, 0); + u32 key_len = k->key_length; + + clib_toeplitz_hash_key_free (k); + k = clib_toeplitz_hash_key_init (bigkey, n_key_copies * key_len); + + for (int i = 0; i < N_LENGTH_TESTS - 4; i++) + { + wrapper_x4 (k, bigdata0, bigdata1, bigdata2, bigdata3, r, r + 1, r + 2, + r + 3, i); + if (length_test_hashes[i] != r[0] || length_test_hashes[i] != r[1] || + length_test_hashes[i] != r[2] || length_test_hashes[i] != r[3]) + { + err = clib_error_return ( + err, + "wrong length test hash x4 for length %u, " + "calculated { 0x%08x, 0x%08x, 0x%08x, 0x%08x }, expected 0x%08x", + i, r[0], r[1], r[2], r[3], length_test_hashes[i]); + goto done; + } + } + +done: + clib_toeplitz_hash_key_free (k); + test_mem_free (bigkey); + test_mem_free (bigdata0); + test_mem_free (bigdata1); + test_mem_free (bigdata2); + test_mem_free (bigdata3); + return err; +} + +void __test_perf_fn +perftest_fixed_12byte_x4 (test_perf_t *tp) +{ + u32 n = tp->n_ops / 4; + u8 *d0 = test_mem_alloc_and_splat (12, n, (void *) &ip4_tests[0].key); + u8 *d1 = test_mem_alloc_and_splat (12, n, (void *) &ip4_tests[1].key); + u8 *d2 = test_mem_alloc_and_splat (12, n, (void *) &ip4_tests[2].key); + u8 *d3 = test_mem_alloc_and_splat (12, n, (void *) &ip4_tests[3].key); + u32 *h0 = test_mem_alloc (4 * n); + u32 *h1 = test_mem_alloc (4 * n); + u32 *h2 = test_mem_alloc (4 * n); + u32 *h3 = test_mem_alloc (4 * n); + clib_toeplitz_hash_key_t *k = clib_toeplitz_hash_key_init (0, 0); + + test_perf_event_enable (tp); + for (int i = 0; i < n; i++) + clib_toeplitz_hash_x4 (k, d0 + i * 12, d1 + i * 12, d2 + i * 12, + d3 + i * 12, h0 + i, h1 + i, h2 + i, h3 + i, 12); + test_perf_event_disable (tp); + + clib_toeplitz_hash_key_free (k); + test_mem_free (d0); + test_mem_free (d1); + test_mem_free (d2); + test_mem_free (d3); + test_mem_free (h0); + test_mem_free (h1); + test_mem_free (h2); + test_mem_free (h3); +} + +void __test_perf_fn +perftest_fixed_36byte_x4 (test_perf_t *tp) +{ + u32 n = tp->n_ops / 4; + u8 *d0 = test_mem_alloc_and_splat (36, n, (void *) &ip4_tests[0].key); + u8 *d1 = test_mem_alloc_and_splat (36, n, (void *) &ip4_tests[1].key); + u8 *d2 = test_mem_alloc_and_splat (36, n, (void *) &ip4_tests[2].key); + u8 *d3 = test_mem_alloc_and_splat (36, n, (void *) &ip4_tests[3].key); + u32 *h0 = test_mem_alloc (4 * n); + u32 *h1 = test_mem_alloc (4 * n); + u32 *h2 = test_mem_alloc (4 * n); + u32 *h3 = test_mem_alloc (4 * n); + clib_toeplitz_hash_key_t *k = clib_toeplitz_hash_key_init (0, 0); + + test_perf_event_enable (tp); + for (int i = 0; i < n; i++) + clib_toeplitz_hash_x4 (k, d0 + i * 36, d1 + i * 36, d2 + i * 36, + d3 + i * 36, h0 + i, h1 + i, h2 + i, h3 + i, 36); + test_perf_event_disable (tp); + + clib_toeplitz_hash_key_free (k); + test_mem_free (d0); + test_mem_free (d1); + test_mem_free (d2); + test_mem_free (d3); + test_mem_free (h0); + test_mem_free (h1); + test_mem_free (h2); + test_mem_free (h3); +} + +void __test_perf_fn +perftest_variable_size_x4 (test_perf_t *tp) +{ + u32 key_len, n_keys, n = tp->n_ops / 4; + u8 *key; + u8 *d0 = test_mem_alloc (n); + u8 *d1 = test_mem_alloc (n); + u8 *d2 = test_mem_alloc (n); + u8 *d3 = test_mem_alloc (n); + u32 *h0 = test_mem_alloc (sizeof (u32)); + u32 *h1 = test_mem_alloc (sizeof (u32)); + u32 *h2 = test_mem_alloc (sizeof (u32)); + u32 *h3 = test_mem_alloc (sizeof (u32)); + clib_toeplitz_hash_key_t *k = clib_toeplitz_hash_key_init (0, 0); + + k = clib_toeplitz_hash_key_init (0, 0); + key_len = k->key_length; + n_keys = ((n + 4) / k->key_length) + 1; + key = test_mem_alloc_and_splat (n_keys, key_len, k->data); + clib_toeplitz_hash_key_free (k); + k = clib_toeplitz_hash_key_init (key, key_len * n_keys); + + test_perf_event_enable (tp); + clib_toeplitz_hash_x4 (k, d0, d1, d2, d3, h0, h1, h2, h3, n); + test_perf_event_disable (tp); + + clib_toeplitz_hash_key_free (k); + test_mem_free (key); + test_mem_free (d0); + test_mem_free (d1); + test_mem_free (d2); + test_mem_free (d3); + test_mem_free (h0); + test_mem_free (h1); + test_mem_free (h2); + test_mem_free (h3); +} + +REGISTER_TEST (clib_toeplitz_hash_x4) = { + .name = "clib_toeplitz_hash_x4", + .fn = test_clib_toeplitz_hash_x4, + .perf_tests = PERF_TESTS ({ .name = "fixed (per 12 byte tuple)", + .n_ops = 1024, + .fn = perftest_fixed_12byte_x4 }, + { .name = "fixed (per 36 byte tuple)", + .n_ops = 1024, + .fn = perftest_fixed_36byte_x4 }, + { .name = "variable size (per byte)", + .n_ops = 16384, + .fn = perftest_variable_size_x4 }), +}; |