xref: /dpdk/app/test/test_crc.c (revision e0a8442ccd15bafbb7eb150c35331c8e3b828c53)
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 uint16_t crc16_vec1_res = 0x8cdd;
48 static const uint16_t crc16_vec2_res = 0xec5b;
49 
50 static int
crc_calc(const uint8_t * vec,uint32_t vec_len,enum rte_net_crc_type type)51 crc_calc(const uint8_t *vec,
52 	uint32_t vec_len,
53 	enum rte_net_crc_type type)
54 {
55 	/* compute CRC */
56 	uint32_t ret = rte_net_crc_calc(vec, vec_len, type);
57 
58 	/* dump data on console */
59 	debug_hexdump(stdout, NULL, vec, vec_len);
60 
61 	return  ret;
62 }
63 
64 static int
test_crc_calc(void)65 test_crc_calc(void)
66 {
67 	uint32_t i;
68 	enum rte_net_crc_type type;
69 	uint8_t *test_data;
70 	uint32_t result;
71 	int error;
72 
73 	/* 32-bit ethernet CRC: Test 1 */
74 	type = RTE_NET_CRC32_ETH;
75 
76 	result = crc_calc(crc_vec, CRC_VEC_LEN, type);
77 	if (result != crc32_vec_res)
78 		return -1;
79 
80 	/* 32-bit ethernet CRC: Test 2 */
81 	test_data = rte_zmalloc(NULL, CRC32_VEC_LEN1, 0);
82 	if (test_data == NULL)
83 		return -7;
84 
85 	for (i = 0; i < CRC32_VEC_LEN1; i += 12)
86 		rte_memcpy(&test_data[i], crc32_vec1, 12);
87 
88 	result = crc_calc(test_data, CRC32_VEC_LEN1, type);
89 	if (result != crc32_vec1_res) {
90 		error = -2;
91 		goto fail;
92 	}
93 
94 	/* 32-bit ethernet CRC: Test 3 */
95 	for (i = 0; i < CRC32_VEC_LEN2; i += 12)
96 		rte_memcpy(&test_data[i], crc32_vec1, 12);
97 
98 	result = crc_calc(test_data, CRC32_VEC_LEN2, type);
99 	if (result != crc32_vec2_res) {
100 		error = -3;
101 		goto fail;
102 	}
103 
104 	/* 16-bit CCITT CRC:  Test 4 */
105 	type = RTE_NET_CRC16_CCITT;
106 	result = crc_calc(crc_vec, CRC_VEC_LEN, type);
107 	if (result != crc16_vec_res) {
108 		error = -4;
109 		goto fail;
110 	}
111 	/* 16-bit CCITT CRC:  Test 5 */
112 	result = crc_calc(crc16_vec1, CRC16_VEC_LEN1, type);
113 	if (result != crc16_vec1_res) {
114 		error = -5;
115 		goto fail;
116 	}
117 	/* 16-bit CCITT CRC:  Test 6 */
118 	result = crc_calc(crc16_vec2, CRC16_VEC_LEN2, type);
119 	if (result != crc16_vec2_res) {
120 		error = -6;
121 		goto fail;
122 	}
123 
124 	rte_free(test_data);
125 	return 0;
126 
127 fail:
128 	rte_free(test_data);
129 	return error;
130 }
131 
132 static int
test_crc(void)133 test_crc(void)
134 {
135 	int ret;
136 	/* set CRC scalar mode */
137 	rte_net_crc_set_alg(RTE_NET_CRC_SCALAR);
138 
139 	ret = test_crc_calc();
140 	if (ret < 0) {
141 		printf("test_crc (scalar): failed (%d)\n", ret);
142 		return ret;
143 	}
144 	/* set CRC sse4.2 mode */
145 	rte_net_crc_set_alg(RTE_NET_CRC_SSE42);
146 
147 	ret = test_crc_calc();
148 	if (ret < 0) {
149 		printf("test_crc (x86_64_SSE4.2): failed (%d)\n", ret);
150 		return ret;
151 	}
152 
153 	/* set CRC avx512 mode */
154 	rte_net_crc_set_alg(RTE_NET_CRC_AVX512);
155 
156 	ret = test_crc_calc();
157 	if (ret < 0) {
158 		printf("test crc (x86_64 AVX512): failed (%d)\n", ret);
159 		return ret;
160 	}
161 
162 	/* set CRC neon mode */
163 	rte_net_crc_set_alg(RTE_NET_CRC_NEON);
164 
165 	ret = test_crc_calc();
166 	if (ret < 0) {
167 		printf("test crc (arm64 neon pmull): failed (%d)\n", ret);
168 		return ret;
169 	}
170 
171 	return 0;
172 }
173 
174 REGISTER_FAST_TEST(crc_autotest, true, true, test_crc);
175