4 * Copyright(c) 2017 Intel Corporation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #include <rte_hexdump.h>
37 #include <rte_malloc.h>
38 #include <rte_memcpy.h>
39 #include <rte_net_crc.h>
41 #define CRC_VEC_LEN 32
42 #define CRC32_VEC_LEN1 1512
43 #define CRC32_VEC_LEN2 348
44 #define CRC16_VEC_LEN1 12
45 #define CRC16_VEC_LEN2 2
49 static const uint8_t crc_vec[CRC_VEC_LEN] = {
50 '0', '1', '2', '3', '4', '5', '6', '7',
51 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f',
52 'g', 'h', 'i', 'j', 'A', 'B', 'C', 'D',
53 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
56 /* 32-bit CRC test vector */
57 static const uint8_t crc32_vec1[12] = {
58 0xBE, 0xD7, 0x23, 0x47, 0x6B, 0x8F,
59 0xB3, 0x14, 0x5E, 0xFB, 0x35, 0x59,
62 /* 16-bit CRC test vector 1 */
63 static const uint8_t crc16_vec1[CRC16_VEC_LEN1] = {
64 0x0D, 0x01, 0x01, 0x23, 0x45, 0x67,
65 0x89, 0x01, 0x23, 0x45, 0x00, 0x01,
68 /* 16-bit CRC test vector 2 */
69 static const uint8_t crc16_vec2[CRC16_VEC_LEN2] = {
73 static const uint32_t crc32_vec_res = 0xb491aab4;
74 static const uint32_t crc32_vec1_res = 0xac54d294;
75 static const uint32_t crc32_vec2_res = 0xefaae02f;
76 static const uint32_t crc16_vec_res = 0x6bec;
77 static const uint16_t crc16_vec1_res = 0x8cdd;
78 static const uint16_t crc16_vec2_res = 0xec5b;
81 crc_calc(const uint8_t *vec,
83 enum rte_net_crc_type type)
86 uint32_t ret = rte_net_crc_calc(vec, vec_len, type);
88 /* dump data on console */
89 debug_hexdump(stdout, NULL, vec, vec_len);
98 enum rte_net_crc_type type;
103 /* 32-bit ethernet CRC: Test 1 */
104 type = RTE_NET_CRC32_ETH;
106 result = crc_calc(crc_vec, CRC_VEC_LEN, type);
107 if (result != crc32_vec_res)
110 /* 32-bit ethernet CRC: Test 2 */
111 test_data = rte_zmalloc(NULL, CRC32_VEC_LEN1, 0);
113 for (i = 0; i < CRC32_VEC_LEN1; i += 12)
114 rte_memcpy(&test_data[i], crc32_vec1, 12);
116 result = crc_calc(test_data, CRC32_VEC_LEN1, type);
117 if (result != crc32_vec1_res) {
122 /* 32-bit ethernet CRC: Test 3 */
123 for (i = 0; i < CRC32_VEC_LEN2; i += 12)
124 rte_memcpy(&test_data[i], crc32_vec1, 12);
126 result = crc_calc(test_data, CRC32_VEC_LEN2, type);
127 if (result != crc32_vec2_res) {
132 /* 16-bit CCITT CRC: Test 4 */
133 type = RTE_NET_CRC16_CCITT;
134 result = crc_calc(crc_vec, CRC_VEC_LEN, type);
135 if (result != crc16_vec_res) {
139 /* 16-bit CCITT CRC: Test 5 */
140 result = crc_calc(crc16_vec1, CRC16_VEC_LEN1, type);
141 if (result != crc16_vec1_res) {
145 /* 16-bit CCITT CRC: Test 6 */
146 result = crc_calc(crc16_vec2, CRC16_VEC_LEN2, type);
147 if (result != crc16_vec2_res) {
164 /* set CRC scalar mode */
165 rte_net_crc_set_alg(RTE_NET_CRC_SCALAR);
167 ret = test_crc_calc();
169 printf("test_crc (scalar): failed (%d)\n", ret);
172 /* set CRC sse4.2 mode */
173 rte_net_crc_set_alg(RTE_NET_CRC_SSE42);
175 ret = test_crc_calc();
177 printf("test_crc (x86_64_SSE4.2): failed (%d)\n", ret);
181 /* set CRC neon mode */
182 rte_net_crc_set_alg(RTE_NET_CRC_NEON);
184 ret = test_crc_calc();
186 printf("test crc (arm64 neon pmull): failed (%d)\n", ret);
193 REGISTER_TEST_COMMAND(crc_autotest, test_crc);