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 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 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 83 for (i = 0; i < CRC32_VEC_LEN1; i += 12) 84 rte_memcpy(&test_data[i], crc32_vec1, 12); 85 86 result = crc_calc(test_data, CRC32_VEC_LEN1, type); 87 if (result != crc32_vec1_res) { 88 error = -2; 89 goto fail; 90 } 91 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); 95 96 result = crc_calc(test_data, CRC32_VEC_LEN2, type); 97 if (result != crc32_vec2_res) { 98 error = -3; 99 goto fail; 100 } 101 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) { 106 error = -4; 107 goto fail; 108 } 109 /* 16-bit CCITT CRC: Test 5 */ 110 result = crc_calc(crc16_vec1, CRC16_VEC_LEN1, type); 111 if (result != crc16_vec1_res) { 112 error = -5; 113 goto fail; 114 } 115 /* 16-bit CCITT CRC: Test 6 */ 116 result = crc_calc(crc16_vec2, CRC16_VEC_LEN2, type); 117 if (result != crc16_vec2_res) { 118 error = -6; 119 goto fail; 120 } 121 122 rte_free(test_data); 123 return 0; 124 125 fail: 126 rte_free(test_data); 127 return error; 128 } 129 130 static int 131 test_crc(void) 132 { 133 int ret; 134 /* set CRC scalar mode */ 135 rte_net_crc_set_alg(RTE_NET_CRC_SCALAR); 136 137 ret = test_crc_calc(); 138 if (ret < 0) { 139 printf("test_crc (scalar): failed (%d)\n", ret); 140 return ret; 141 } 142 /* set CRC sse4.2 mode */ 143 rte_net_crc_set_alg(RTE_NET_CRC_SSE42); 144 145 ret = test_crc_calc(); 146 if (ret < 0) { 147 printf("test_crc (x86_64_SSE4.2): failed (%d)\n", ret); 148 return ret; 149 } 150 151 /* set CRC avx512 mode */ 152 rte_net_crc_set_alg(RTE_NET_CRC_AVX512); 153 154 ret = test_crc_calc(); 155 if (ret < 0) { 156 printf("test crc (x86_64 AVX512): failed (%d)\n", ret); 157 return ret; 158 } 159 160 /* set CRC neon mode */ 161 rte_net_crc_set_alg(RTE_NET_CRC_NEON); 162 163 ret = test_crc_calc(); 164 if (ret < 0) { 165 printf("test crc (arm64 neon pmull): failed (%d)\n", ret); 166 return ret; 167 } 168 169 return 0; 170 } 171 172 REGISTER_TEST_COMMAND(crc_autotest, test_crc); 173