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.
38 #include <rte_cpuflags.h>
39 #include <rte_common.h>
40 #include <rte_net_crc.h>
42 #if defined(RTE_ARCH_X86_64) \
43 && defined(RTE_MACHINE_CPUFLAG_SSE4_2) \
44 && defined(RTE_MACHINE_CPUFLAG_PCLMULQDQ)
45 #define X86_64_SSE42_PCLMULQDQ 1
48 #ifdef X86_64_SSE42_PCLMULQDQ
49 #include <net_crc_sse.h>
53 static uint32_t crc32_eth_lut
[CRC_LUT_SIZE
];
54 static uint32_t crc16_ccitt_lut
[CRC_LUT_SIZE
];
57 rte_crc16_ccitt_handler(const uint8_t *data
, uint32_t data_len
);
60 rte_crc32_eth_handler(const uint8_t *data
, uint32_t data_len
);
63 (*rte_net_crc_handler
)(const uint8_t *data
, uint32_t data_len
);
65 static rte_net_crc_handler
*handlers
;
67 static rte_net_crc_handler handlers_scalar
[] = {
68 [RTE_NET_CRC16_CCITT
] = rte_crc16_ccitt_handler
,
69 [RTE_NET_CRC32_ETH
] = rte_crc32_eth_handler
,
72 #ifdef X86_64_SSE42_PCLMULQDQ
73 static rte_net_crc_handler handlers_sse42
[] = {
74 [RTE_NET_CRC16_CCITT
] = rte_crc16_ccitt_sse42_handler
,
75 [RTE_NET_CRC32_ETH
] = rte_crc32_eth_sse42_handler
,
80 * Reflect the bits about the middle
83 * value to be reflected
89 reflect_32bits(uint32_t val
)
93 for (i
= 0; i
< 32; i
++)
94 if ((val
& (1 << i
)) != 0)
95 res
|= (uint32_t)(1 << (31 - i
));
101 crc32_eth_init_lut(uint32_t poly
,
106 for (i
= 0; i
< CRC_LUT_SIZE
; i
++) {
107 uint32_t crc
= reflect_32bits(i
);
109 for (j
= 0; j
< 8; j
++) {
110 if (crc
& 0x80000000L
)
111 crc
= (crc
<< 1) ^ poly
;
115 lut
[i
] = reflect_32bits(crc
);
119 static inline __attribute__((always_inline
)) uint32_t
120 crc32_eth_calc_lut(const uint8_t *data
,
126 crc
= lut
[(crc
^ *data
++) & 0xffL
] ^ (crc
>> 8);
132 rte_net_crc_scalar_init(void)
134 /* 32-bit crc init */
135 crc32_eth_init_lut(CRC32_ETH_POLYNOMIAL
, crc32_eth_lut
);
137 /* 16-bit CRC init */
138 crc32_eth_init_lut(CRC16_CCITT_POLYNOMIAL
<< 16, crc16_ccitt_lut
);
141 static inline uint32_t
142 rte_crc16_ccitt_handler(const uint8_t *data
, uint32_t data_len
)
144 /* return 16-bit CRC value */
145 return (uint16_t)~crc32_eth_calc_lut(data
,
151 static inline uint32_t
152 rte_crc32_eth_handler(const uint8_t *data
, uint32_t data_len
)
154 /* return 32-bit CRC value */
155 return ~crc32_eth_calc_lut(data
,
162 rte_net_crc_set_alg(enum rte_net_crc_alg alg
)
165 case RTE_NET_CRC_SSE42
:
166 #ifdef X86_64_SSE42_PCLMULQDQ
167 handlers
= handlers_sse42
;
169 alg
= RTE_NET_CRC_SCALAR
;
172 case RTE_NET_CRC_SCALAR
:
174 handlers
= handlers_scalar
;
180 rte_net_crc_calc(const void *data
,
182 enum rte_net_crc_type type
)
185 rte_net_crc_handler f_handle
;
187 f_handle
= handlers
[type
];
188 ret
= f_handle(data
, data_len
);
193 /* Select highest available crc algorithm as default one */
194 static inline void __attribute__((constructor
))
195 rte_net_crc_init(void)
197 enum rte_net_crc_alg alg
= RTE_NET_CRC_SCALAR
;
199 rte_net_crc_scalar_init();
201 #ifdef X86_64_SSE42_PCLMULQDQ
202 alg
= RTE_NET_CRC_SSE42
;
203 rte_net_crc_sse42_init();
206 rte_net_crc_set_alg(alg
);