a270d6c067156f9830c7625d4f2cc73e1b664ddc
[dpdk.git] / lib / librte_net / rte_net_crc.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2017 Intel Corporation.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
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
16  *       distribution.
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.
20  *
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.
32  */
33
34 #include <stddef.h>
35 #include <string.h>
36 #include <stdint.h>
37
38 #include <rte_cpuflags.h>
39 #include <rte_common.h>
40 #include <rte_net_crc.h>
41
42 #if defined(RTE_ARCH_X86_64) && defined(RTE_MACHINE_CPUFLAG_PCLMULQDQ)
43 #define X86_64_SSE42_PCLMULQDQ     1
44 #endif
45
46 #ifdef X86_64_SSE42_PCLMULQDQ
47 #include <net_crc_sse.h>
48 #endif
49
50 /* crc tables */
51 static uint32_t crc32_eth_lut[CRC_LUT_SIZE];
52 static uint32_t crc16_ccitt_lut[CRC_LUT_SIZE];
53
54 static uint32_t
55 rte_crc16_ccitt_handler(const uint8_t *data, uint32_t data_len);
56
57 static uint32_t
58 rte_crc32_eth_handler(const uint8_t *data, uint32_t data_len);
59
60 typedef uint32_t
61 (*rte_net_crc_handler)(const uint8_t *data, uint32_t data_len);
62
63 static rte_net_crc_handler *handlers;
64
65 static rte_net_crc_handler handlers_scalar[] = {
66         [RTE_NET_CRC16_CCITT] = rte_crc16_ccitt_handler,
67         [RTE_NET_CRC32_ETH] = rte_crc32_eth_handler,
68 };
69
70 #ifdef X86_64_SSE42_PCLMULQDQ
71 static rte_net_crc_handler handlers_sse42[] = {
72         [RTE_NET_CRC16_CCITT] = rte_crc16_ccitt_sse42_handler,
73         [RTE_NET_CRC32_ETH] = rte_crc32_eth_sse42_handler,
74 };
75 #endif
76
77 /**
78  * Reflect the bits about the middle
79  *
80  * @param val
81  *   value to be reflected
82  *
83  * @return
84  *   reflected value
85  */
86 static uint32_t
87 reflect_32bits(uint32_t val)
88 {
89         uint32_t i, res = 0;
90
91         for (i = 0; i < 32; i++)
92                 if ((val & (1 << i)) != 0)
93                         res |= (uint32_t)(1 << (31 - i));
94
95         return res;
96 }
97
98 static void
99 crc32_eth_init_lut(uint32_t poly,
100         uint32_t *lut)
101 {
102         uint32_t i, j;
103
104         for (i = 0; i < CRC_LUT_SIZE; i++) {
105                 uint32_t crc = reflect_32bits(i);
106
107                 for (j = 0; j < 8; j++) {
108                         if (crc & 0x80000000L)
109                                 crc = (crc << 1) ^ poly;
110                         else
111                                 crc <<= 1;
112                 }
113                 lut[i] = reflect_32bits(crc);
114         }
115 }
116
117 static __rte_always_inline uint32_t
118 crc32_eth_calc_lut(const uint8_t *data,
119         uint32_t data_len,
120         uint32_t crc,
121         const uint32_t *lut)
122 {
123         while (data_len--)
124                 crc = lut[(crc ^ *data++) & 0xffL] ^ (crc >> 8);
125
126         return crc;
127 }
128
129 static void
130 rte_net_crc_scalar_init(void)
131 {
132         /* 32-bit crc init */
133         crc32_eth_init_lut(CRC32_ETH_POLYNOMIAL, crc32_eth_lut);
134
135         /* 16-bit CRC init */
136         crc32_eth_init_lut(CRC16_CCITT_POLYNOMIAL << 16, crc16_ccitt_lut);
137 }
138
139 static inline uint32_t
140 rte_crc16_ccitt_handler(const uint8_t *data, uint32_t data_len)
141 {
142         /* return 16-bit CRC value */
143         return (uint16_t)~crc32_eth_calc_lut(data,
144                 data_len,
145                 0xffff,
146                 crc16_ccitt_lut);
147 }
148
149 static inline uint32_t
150 rte_crc32_eth_handler(const uint8_t *data, uint32_t data_len)
151 {
152         /* return 32-bit CRC value */
153         return ~crc32_eth_calc_lut(data,
154                 data_len,
155                 0xffffffffUL,
156                 crc32_eth_lut);
157 }
158
159 void
160 rte_net_crc_set_alg(enum rte_net_crc_alg alg)
161 {
162         switch (alg) {
163         case RTE_NET_CRC_SSE42:
164 #ifdef X86_64_SSE42_PCLMULQDQ
165                 handlers = handlers_sse42;
166 #else
167                 alg = RTE_NET_CRC_SCALAR;
168 #endif
169                 break;
170         case RTE_NET_CRC_SCALAR:
171         default:
172                 handlers = handlers_scalar;
173                 break;
174         }
175 }
176
177 uint32_t
178 rte_net_crc_calc(const void *data,
179         uint32_t data_len,
180         enum rte_net_crc_type type)
181 {
182         uint32_t ret;
183         rte_net_crc_handler f_handle;
184
185         f_handle = handlers[type];
186         ret = f_handle(data, data_len);
187
188         return ret;
189 }
190
191 /* Select highest available crc algorithm as default one */
192 static inline void __attribute__((constructor))
193 rte_net_crc_init(void)
194 {
195         enum rte_net_crc_alg alg = RTE_NET_CRC_SCALAR;
196
197         rte_net_crc_scalar_init();
198
199 #ifdef X86_64_SSE42_PCLMULQDQ
200                 alg = RTE_NET_CRC_SSE42;
201                 rte_net_crc_sse42_init();
202 #endif
203
204         rte_net_crc_set_alg(alg);
205 }