1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2017 Intel Corporation
9 #include <rte_cpuflags.h>
10 #include <rte_common.h>
11 #include <rte_net_crc.h>
13 #if defined(RTE_ARCH_X86_64) && defined(RTE_MACHINE_CPUFLAG_PCLMULQDQ)
14 #define X86_64_SSE42_PCLMULQDQ 1
15 #elif defined(RTE_ARCH_ARM64) && defined(RTE_MACHINE_CPUFLAG_PMULL)
16 #define ARM64_NEON_PMULL 1
19 #ifdef X86_64_SSE42_PCLMULQDQ
20 #include <net_crc_sse.h>
21 #elif defined ARM64_NEON_PMULL
22 #include <net_crc_neon.h>
26 static uint32_t crc32_eth_lut[CRC_LUT_SIZE];
27 static uint32_t crc16_ccitt_lut[CRC_LUT_SIZE];
30 rte_crc16_ccitt_handler(const uint8_t *data, uint32_t data_len);
33 rte_crc32_eth_handler(const uint8_t *data, uint32_t data_len);
36 (*rte_net_crc_handler)(const uint8_t *data, uint32_t data_len);
38 static rte_net_crc_handler *handlers;
40 static rte_net_crc_handler handlers_scalar[] = {
41 [RTE_NET_CRC16_CCITT] = rte_crc16_ccitt_handler,
42 [RTE_NET_CRC32_ETH] = rte_crc32_eth_handler,
45 #ifdef X86_64_SSE42_PCLMULQDQ
46 static rte_net_crc_handler handlers_sse42[] = {
47 [RTE_NET_CRC16_CCITT] = rte_crc16_ccitt_sse42_handler,
48 [RTE_NET_CRC32_ETH] = rte_crc32_eth_sse42_handler,
50 #elif defined ARM64_NEON_PMULL
51 static rte_net_crc_handler handlers_neon[] = {
52 [RTE_NET_CRC16_CCITT] = rte_crc16_ccitt_neon_handler,
53 [RTE_NET_CRC32_ETH] = rte_crc32_eth_neon_handler,
58 * Reflect the bits about the middle
61 * value to be reflected
67 reflect_32bits(uint32_t val)
71 for (i = 0; i < 32; i++)
72 if ((val & (1 << i)) != 0)
73 res |= (uint32_t)(1 << (31 - i));
79 crc32_eth_init_lut(uint32_t poly,
84 for (i = 0; i < CRC_LUT_SIZE; i++) {
85 uint32_t crc = reflect_32bits(i);
87 for (j = 0; j < 8; j++) {
88 if (crc & 0x80000000L)
89 crc = (crc << 1) ^ poly;
93 lut[i] = reflect_32bits(crc);
97 static __rte_always_inline uint32_t
98 crc32_eth_calc_lut(const uint8_t *data,
104 crc = lut[(crc ^ *data++) & 0xffL] ^ (crc >> 8);
110 rte_net_crc_scalar_init(void)
112 /* 32-bit crc init */
113 crc32_eth_init_lut(CRC32_ETH_POLYNOMIAL, crc32_eth_lut);
115 /* 16-bit CRC init */
116 crc32_eth_init_lut(CRC16_CCITT_POLYNOMIAL << 16, crc16_ccitt_lut);
119 static inline uint32_t
120 rte_crc16_ccitt_handler(const uint8_t *data, uint32_t data_len)
122 /* return 16-bit CRC value */
123 return (uint16_t)~crc32_eth_calc_lut(data,
129 static inline uint32_t
130 rte_crc32_eth_handler(const uint8_t *data, uint32_t data_len)
132 /* return 32-bit CRC value */
133 return ~crc32_eth_calc_lut(data,
140 rte_net_crc_set_alg(enum rte_net_crc_alg alg)
143 #ifdef X86_64_SSE42_PCLMULQDQ
144 case RTE_NET_CRC_SSE42:
145 handlers = handlers_sse42;
147 #elif defined ARM64_NEON_PMULL
149 case RTE_NET_CRC_NEON:
150 if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_PMULL)) {
151 handlers = handlers_neon;
156 case RTE_NET_CRC_SCALAR:
159 handlers = handlers_scalar;
165 rte_net_crc_calc(const void *data,
167 enum rte_net_crc_type type)
170 rte_net_crc_handler f_handle;
172 f_handle = handlers[type];
173 ret = f_handle(data, data_len);
178 /* Select highest available crc algorithm as default one */
179 RTE_INIT(rte_net_crc_init)
181 enum rte_net_crc_alg alg = RTE_NET_CRC_SCALAR;
183 rte_net_crc_scalar_init();
185 #ifdef X86_64_SSE42_PCLMULQDQ
186 alg = RTE_NET_CRC_SSE42;
187 rte_net_crc_sse42_init();
188 #elif defined ARM64_NEON_PMULL
189 if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_PMULL)) {
190 alg = RTE_NET_CRC_NEON;
191 rte_net_crc_neon_init();
195 rte_net_crc_set_alg(alg);