]> git.proxmox.com Git - ceph.git/blame - ceph/src/spdk/intel-ipsec-mb/include/wireless_common.h
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / spdk / intel-ipsec-mb / include / wireless_common.h
CommitLineData
f67539c2
TL
1/*******************************************************************************
2 Copyright (c) 2009-2019, Intel Corporation
3
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions are met:
6
7 * Redistributions of source code must retain the above copyright notice,
8 this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above copyright
10 notice, this list of conditions and the following disclaimer in the
11 documentation and/or other materials provided with the distribution.
12 * Neither the name of Intel Corporation nor the names of its contributors
13 may be used to endorse or promote products derived from this software
14 without specific prior written permission.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
20 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*******************************************************************************/
27
28#ifndef _WIRELESS_COMMON_H_
29#define _WIRELESS_COMMON_H_
30
31#include <string.h>
32#ifdef LINUX
33#include <x86intrin.h>
34#else
35#include <intrin.h>
36#endif
37
38#define NUM_PACKETS_1 1
39#define NUM_PACKETS_2 2
40#define NUM_PACKETS_3 3
41#define NUM_PACKETS_4 4
42#define NUM_PACKETS_8 8
43#define NUM_PACKETS_16 16
44
45#ifdef LINUX
46#define BSWAP32 __builtin_bswap32
47#define BSWAP64 __builtin_bswap64
48#else
49#define BSWAP32 _byteswap_ulong
50#define BSWAP64 _byteswap_uint64
51#endif
52
53typedef union _m128_u {
54 uint8_t byte[16];
55 uint16_t word[8];
56 uint32_t dword[4];
57 uint64_t qword[2];
58 __m128i m;
59} m128_t;
60
61typedef union _m64_u {
62 uint8_t byte[8];
63 uint16_t word[4];
64 uint32_t dword[2];
65 uint64_t m;
66} m64_t;
67
68static inline uint32_t bswap4(const uint32_t val)
69{
70 return ((val >> 24) | /**< A*/
71 ((val & 0xff0000) >> 8) | /**< B*/
72 ((val & 0xff00) << 8) | /**< C*/
73 (val << 24)); /**< D*/
74}
75
76/*************************************************************************
77* @description - this function is used to copy the right number of bytes
78* from the source to destination buffer
79*
80* @param pSrc [IN] - pointer to an input Byte array (at least len bytes
81* available)
82* @param pDst [IN] - pointer to the output buffer (at least len bytes available)
83* @param len [IN] - length in bytes to copy (0 to 4)
84*
85*************************************************************************/
86static inline void memcpy_keystream_32(uint8_t *pDst,
87 const uint8_t *pSrc,
88 const uint32_t len)
89{
90 switch (len) {
91 case 4:
92 *(uint32_t *)pDst = *(const uint32_t *)pSrc;
93 break;
94 case 3:
95 pDst[2] = pSrc[2];
96 /* fall-through */
97 case 2:
98 pDst[1] = pSrc[1];
99 /* fall-through */
100 case 1:
101 pDst[0] = pSrc[0];
102 /* fall-through */
103 }
104}
105
106/*************************************************************************
107* @description - this function is used to XOR the right number of bytes
108* from a keystrea and a source into a destination buffer
109*
110* @param pSrc [IN] - pointer to an input Byte array (at least 4 bytes available)
111* @param pDst [IN] - pointer to the output buffer (at least 4 bytes available)
112* @param KS [IN] - 4 bytes of keystream number, must be reversed
113* into network byte order before XOR
114*
115*************************************************************************/
116static inline void xor_keystream_reverse_32(uint8_t *pDst,
117 const uint8_t *pSrc,
118 const uint32_t KS)
119{
120 *(uint32_t *)pDst = (*(const uint32_t *)pSrc) ^ BSWAP32(KS);
121}
122
123/******************************************************************************
124 * @description - this function is used to do a keystream operation
125 * @param pSrc [IN] - pointer to an input Byte array (at least 8 bytes
126 * available)
127 * @param pDst [IN] - pointer to the output buffer (at least 8 bytes available)
128 * @param keyStream [IN] - the Keystream value (8 bytes)
129 ******************************************************************************/
130static inline const uint8_t *
131xor_keystrm_rev(uint8_t *pDst, const uint8_t *pSrc, uint64_t keyStream)
132{
133 /* default: XOR ONLY, read the input buffer, update the output buffer */
134 const uint64_t *pSrc64 = (const uint64_t *)pSrc;
135 uint64_t *pDst64 = (uint64_t *)pDst;
136 *pDst64 = *pSrc64 ^ BSWAP64(keyStream);
137 return (const uint8_t *)(pSrc64 + 1);
138}
139
140/******************************************************************************
141 * @description - this function is used to copy the right number of bytes
142 * from the source to destination buffer
143 * @param pSrc [IN] - pointer to an input Byte array (at least len bytes
144 * available)
145 * @param pDst [IN] - pointer to the output buffer (at least len bytes
146 * available)
147 * @param len [IN] - length in bytes to copy
148 ******************************************************************************/
149static inline void
150memcpy_keystrm(uint8_t *pDst, const uint8_t *pSrc, const uint32_t len)
151{
152 switch (len) {
153 case 8:
154 *(uint64_t *)pDst = *(const uint64_t *)pSrc;
155 break;
156 case 7:
157 pDst[6] = pSrc[6];
158 /* fall-through */
159 case 6:
160 pDst[5] = pSrc[5];
161 /* fall-through */
162 case 5:
163 pDst[4] = pSrc[4];
164 /* fall-through */
165 case 4:
166 *(uint32_t *)pDst = *(const uint32_t *)pSrc;
167 break;
168 case 3:
169 pDst[2] = pSrc[2];
170 /* fall-through */
171 case 2:
172 pDst[1] = pSrc[1];
173 /* fall-through */
174 case 1:
175 pDst[0] = pSrc[0];
176 /* fall-through */
177 }
178}
179
180/**
181 ******************************************************************************
182 *
183 * @description
184 * Definition of the external SSE function that XOR's 64 bytes of input
185 * with 64 bytes of keystream, swapping keystream bytes every 4 bytes.
186 *
187 * @param[in] pIn Pointer to the input buffer
188 * @param[out] pOut Pointer to the output buffer
189 * @param[in] pKey Pointer to the new 64 byte keystream
190 *
191 * @pre
192 * None
193 *
194 *****************************************************************************/
195IMB_DLL_LOCAL void asm_XorKeyStream64B_sse(const void *pIn, void *pOut,
196 const void *pKey);
197
198/**
199 ******************************************************************************
200 *
201 * @description
202 * Definition of the external AVX function that XOR's 64 bytes of input
203 * with 64 bytes of keystream, swapping keystream bytes every 4 bytes.
204 *
205 * @param[in] pIn Pointer to the input buffer
206 * @param[out] pOut Pointer to the output buffer
207 * @param[in] pKey Pointer to the new 64 byte keystream
208 *
209 * @pre
210 * None
211 *
212 *****************************************************************************/
213IMB_DLL_LOCAL void asm_XorKeyStream64B_avx(const void *pIn, void *pOut,
214 const void *pKey);
215
216#endif /* _WIRELESS_COMMON_H_ */