Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2017-2020 Intel Corporation
3 : : */
4 : :
5 : : #include "test.h"
6 : :
7 : : #include <rte_hexdump.h>
8 : : #include <rte_malloc.h>
9 : : #include <rte_memcpy.h>
10 : : #include <rte_net_crc.h>
11 : :
12 : : #define CRC_VEC_LEN 32
13 : : #define CRC32_VEC_LEN1 1512
14 : : #define CRC32_VEC_LEN2 348
15 : : #define CRC16_VEC_LEN1 12
16 : : #define CRC16_VEC_LEN2 2
17 : :
18 : : /* CRC test vector */
19 : : static const uint8_t crc_vec[CRC_VEC_LEN] = {
20 : : '0', '1', '2', '3', '4', '5', '6', '7',
21 : : '8', '9', 'a', 'b', 'c', 'd', 'e', 'f',
22 : : 'g', 'h', 'i', 'j', 'A', 'B', 'C', 'D',
23 : : 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
24 : : };
25 : :
26 : : /* 32-bit CRC test vector */
27 : : static const uint8_t crc32_vec1[12] = {
28 : : 0xBE, 0xD7, 0x23, 0x47, 0x6B, 0x8F,
29 : : 0xB3, 0x14, 0x5E, 0xFB, 0x35, 0x59,
30 : : };
31 : :
32 : : /* 16-bit CRC test vector 1 */
33 : : static const uint8_t crc16_vec1[CRC16_VEC_LEN1] = {
34 : : 0x0D, 0x01, 0x01, 0x23, 0x45, 0x67,
35 : : 0x89, 0x01, 0x23, 0x45, 0x00, 0x01,
36 : : };
37 : :
38 : : /* 16-bit CRC test vector 2 */
39 : : static const uint8_t crc16_vec2[CRC16_VEC_LEN2] = {
40 : : 0x03, 0x3f,
41 : : };
42 : : /** CRC results */
43 : : static const uint32_t crc32_vec_res = 0xb491aab4;
44 : : static const uint32_t crc32_vec1_res = 0xac54d294;
45 : : static const uint32_t crc32_vec2_res = 0xefaae02f;
46 : : static const uint32_t crc16_vec_res = 0x6bec;
47 : : static const uint32_t crc16_vec1_res = 0x8cdd;
48 : : static const uint32_t crc16_vec2_res = 0xec5b;
49 : :
50 : : static int
51 : 6 : crc_all_algs(const char *desc, enum rte_net_crc_type type,
52 : : const uint8_t *data, int data_len, uint32_t res)
53 : : {
54 : : struct rte_net_crc *ctx;
55 : : uint32_t crc;
56 : : int ret = TEST_SUCCESS;
57 : :
58 : 6 : ctx = rte_net_crc_set_alg(RTE_NET_CRC_SCALAR, type);
59 [ - + ]: 6 : TEST_ASSERT_NOT_NULL(ctx, "cannot allocate the CRC context");
60 : 6 : crc = rte_net_crc_calc(ctx, data, data_len);
61 [ - + ]: 6 : if (crc != res) {
62 : 0 : RTE_LOG(ERR, USER1, "TEST FAILED: %s SCALAR\n", desc);
63 : 0 : debug_hexdump(stdout, "SCALAR", &crc, 4);
64 : : ret = TEST_FAILED;
65 : : }
66 : 6 : rte_net_crc_free(ctx);
67 : :
68 : 6 : ctx = rte_net_crc_set_alg(RTE_NET_CRC_SSE42, type);
69 [ - + ]: 6 : TEST_ASSERT_NOT_NULL(ctx, "cannot allocate the CRC context");
70 : 6 : crc = rte_net_crc_calc(ctx, data, data_len);
71 [ - + ]: 6 : if (crc != res) {
72 : 0 : RTE_LOG(ERR, USER1, "TEST FAILED: %s SSE42\n", desc);
73 : 0 : debug_hexdump(stdout, "SSE", &crc, 4);
74 : : ret = TEST_FAILED;
75 : : }
76 : :
77 : 6 : rte_net_crc_free(ctx);
78 : :
79 : 6 : ctx = rte_net_crc_set_alg(RTE_NET_CRC_AVX512, type);
80 [ - + ]: 6 : TEST_ASSERT_NOT_NULL(ctx, "cannot allocate the CRC context");
81 : 6 : crc = rte_net_crc_calc(ctx, data, data_len);
82 [ - + ]: 6 : if (crc != res) {
83 : 0 : RTE_LOG(ERR, USER1, "TEST FAILED: %s AVX512\n", desc);
84 : 0 : debug_hexdump(stdout, "AVX512", &crc, 4);
85 : : ret = TEST_FAILED;
86 : : }
87 : 6 : rte_net_crc_free(ctx);
88 : :
89 : 6 : ctx = rte_net_crc_set_alg(RTE_NET_CRC_NEON, type);
90 [ - + ]: 6 : TEST_ASSERT_NOT_NULL(ctx, "cannot allocate the CRC context");
91 : 6 : crc = rte_net_crc_calc(ctx, data, data_len);
92 [ - + ]: 6 : if (crc != res) {
93 : 0 : RTE_LOG(ERR, USER1, "TEST FAILED: %s NEON\n", desc);
94 : 0 : debug_hexdump(stdout, "NEON", &crc, 4);
95 : : ret = TEST_FAILED;
96 : : }
97 : 6 : rte_net_crc_free(ctx);
98 : :
99 : 6 : return ret;
100 : : }
101 : :
102 : : static int
103 : 1 : crc_autotest(void)
104 : : { uint8_t *test_data;
105 : : uint32_t i;
106 : : int ret = TEST_SUCCESS;
107 : :
108 : : /* 32-bit ethernet CRC: Test 1 */
109 : 1 : ret = crc_all_algs("32-bit ethernet CRC: Test 1", RTE_NET_CRC32_ETH, crc_vec,
110 : : sizeof(crc_vec), crc32_vec_res);
111 : :
112 : : /* 32-bit ethernet CRC: Test 2 */
113 : 1 : test_data = rte_zmalloc(NULL, CRC32_VEC_LEN1, 0);
114 [ + - ]: 1 : if (test_data == NULL)
115 : : return -7;
116 [ + + ]: 127 : for (i = 0; i < CRC32_VEC_LEN1; i += 12)
117 [ + + ]: 126 : rte_memcpy(&test_data[i], crc32_vec1, 12);
118 : 1 : ret |= crc_all_algs("32-bit ethernet CRC: Test 2", RTE_NET_CRC32_ETH, test_data,
119 : : CRC32_VEC_LEN1, crc32_vec1_res);
120 : :
121 : : /* 32-bit ethernet CRC: Test 3 */
122 : : memset(test_data, 0, CRC32_VEC_LEN1);
123 [ + + ]: 30 : for (i = 0; i < CRC32_VEC_LEN2; i += 12)
124 [ + + ]: 29 : rte_memcpy(&test_data[i], crc32_vec1, 12);
125 : 1 : ret |= crc_all_algs("32-bit ethernet CRC: Test 3", RTE_NET_CRC32_ETH, test_data,
126 : : CRC32_VEC_LEN2, crc32_vec2_res);
127 : :
128 : : /* 16-bit CCITT CRC: Test 4 */
129 : 1 : crc_all_algs("16-bit CCITT CRC: Test 4", RTE_NET_CRC16_CCITT, crc_vec,
130 : : sizeof(crc_vec), crc16_vec_res);
131 : :
132 : : /* 16-bit CCITT CRC: Test 5 */
133 : 1 : ret |= crc_all_algs("16-bit CCITT CRC: Test 5", RTE_NET_CRC16_CCITT, crc16_vec1,
134 : : CRC16_VEC_LEN1, crc16_vec1_res);
135 : :
136 : : /* 16-bit CCITT CRC: Test 6 */
137 : 1 : ret |= crc_all_algs("16-bit CCITT CRC: Test 6", RTE_NET_CRC16_CCITT, crc16_vec2,
138 : : CRC16_VEC_LEN2, crc16_vec2_res);
139 : :
140 : 1 : return ret;
141 : : }
142 : :
143 : 252 : REGISTER_FAST_TEST(crc_autotest, true, true, crc_autotest);
|