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