1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2017-2020 Intel Corporation
7 #include <rte_hexdump.h>
8 #include <rte_malloc.h>
9 #include <rte_memcpy.h>
10 #include <rte_net_crc.h>
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
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',
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,
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,
38 /* 16-bit CRC test vector 2 */
39 static const uint8_t crc16_vec2[CRC16_VEC_LEN2] = {
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 uint16_t crc16_vec1_res = 0x8cdd;
48 static const uint16_t crc16_vec2_res = 0xec5b;
51 crc_calc(const uint8_t *vec,
53 enum rte_net_crc_type type)
56 uint32_t ret = rte_net_crc_calc(vec, vec_len, type);
58 /* dump data on console */
59 debug_hexdump(stdout, NULL, vec, vec_len);
68 enum rte_net_crc_type type;
73 /* 32-bit ethernet CRC: Test 1 */
74 type = RTE_NET_CRC32_ETH;
76 result = crc_calc(crc_vec, CRC_VEC_LEN, type);
77 if (result != crc32_vec_res)
80 /* 32-bit ethernet CRC: Test 2 */
81 test_data = rte_zmalloc(NULL, CRC32_VEC_LEN1, 0);
83 for (i = 0; i < CRC32_VEC_LEN1; i += 12)
84 rte_memcpy(&test_data[i], crc32_vec1, 12);
86 result = crc_calc(test_data, CRC32_VEC_LEN1, type);
87 if (result != crc32_vec1_res) {
92 /* 32-bit ethernet CRC: Test 3 */
93 for (i = 0; i < CRC32_VEC_LEN2; i += 12)
94 rte_memcpy(&test_data[i], crc32_vec1, 12);
96 result = crc_calc(test_data, CRC32_VEC_LEN2, type);
97 if (result != crc32_vec2_res) {
102 /* 16-bit CCITT CRC: Test 4 */
103 type = RTE_NET_CRC16_CCITT;
104 result = crc_calc(crc_vec, CRC_VEC_LEN, type);
105 if (result != crc16_vec_res) {
109 /* 16-bit CCITT CRC: Test 5 */
110 result = crc_calc(crc16_vec1, CRC16_VEC_LEN1, type);
111 if (result != crc16_vec1_res) {
115 /* 16-bit CCITT CRC: Test 6 */
116 result = crc_calc(crc16_vec2, CRC16_VEC_LEN2, type);
117 if (result != crc16_vec2_res) {
134 /* set CRC scalar mode */
135 rte_net_crc_set_alg(RTE_NET_CRC_SCALAR);
137 ret = test_crc_calc();
139 printf("test_crc (scalar): failed (%d)\n", ret);
142 /* set CRC sse4.2 mode */
143 rte_net_crc_set_alg(RTE_NET_CRC_SSE42);
145 ret = test_crc_calc();
147 printf("test_crc (x86_64_SSE4.2): failed (%d)\n", ret);
151 /* set CRC avx512 mode */
152 rte_net_crc_set_alg(RTE_NET_CRC_AVX512);
154 ret = test_crc_calc();
156 printf("test crc (x86_64 AVX512): failed (%d)\n", ret);
160 /* set CRC neon mode */
161 rte_net_crc_set_alg(RTE_NET_CRC_NEON);
163 ret = test_crc_calc();
165 printf("test crc (arm64 neon pmull): failed (%d)\n", ret);
172 REGISTER_TEST_COMMAND(crc_autotest, test_crc);