]> git.proxmox.com Git - ceph.git/blame - ceph/src/spdk/intel-ipsec-mb/LibTestApp/main.c
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / spdk / intel-ipsec-mb / LibTestApp / main.c
CommitLineData
11fdf7f2 1/*****************************************************************************
f67539c2 2 Copyright (c) 2012-2019, Intel Corporation
11fdf7f2
TL
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#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31
32#include <intel-ipsec-mb.h>
33
34#include "gcm_ctr_vectors_test.h"
35#include "customop_test.h"
9f95a23c 36#include "utils.h"
11fdf7f2
TL
37
38extern int des_test(const enum arch_type arch, struct MB_MGR *mb_mgr);
39extern int ccm_test(const enum arch_type arch, struct MB_MGR *mb_mgr);
40extern int cmac_test(const enum arch_type arch, struct MB_MGR *mb_mgr);
9f95a23c
TL
41extern int hmac_sha1_test(const enum arch_type arch, struct MB_MGR *mb_mgr);
42extern int hmac_sha256_sha512_test(const enum arch_type arch,
43 struct MB_MGR *mb_mgr);
44extern int hmac_md5_test(const enum arch_type arch, struct MB_MGR *mb_mgr);
45extern int aes_test(const enum arch_type arch, struct MB_MGR *mb_mgr);
f67539c2 46extern int ecb_test(const enum arch_type arch, struct MB_MGR *mb_mgr);
9f95a23c 47extern int sha_test(const enum arch_type arch, struct MB_MGR *mb_mgr);
f67539c2
TL
48extern int chained_test(const enum arch_type arch, struct MB_MGR *mb_mgr);
49extern int api_test(const enum arch_type arch, struct MB_MGR *mb_mgr);
50extern int pon_test(const enum arch_type arch, struct MB_MGR *mb_mgr);
51extern int zuc_test(const enum arch_type arch, struct MB_MGR *mb_mgr);
52extern int kasumi_test(const enum arch_type arch, struct MB_MGR *mb_mgr);
53extern int snow3g_test(const enum arch_type arch, struct MB_MGR *mb_mgr);
54extern int direct_api_test(const enum arch_type arch, struct MB_MGR *mb_mgr);
11fdf7f2
TL
55
56#include "do_test.h"
57
58static void
59usage(const char *name)
60{
61 fprintf(stderr,
62 "Usage: %s [args], where args are zero or more\n"
9f95a23c
TL
63 "--no-aesni-emu: Don't do AESNI emulation\n"
64 "--no-avx512: Don't do AVX512\n"
11fdf7f2
TL
65 "--no-avx2: Don't do AVX2\n"
66 "--no-avx: Don't do AVX\n"
67 "--no-sse: Don't do SSE\n"
9f95a23c
TL
68 "--no-gcm: Don't run GCM tests\n"
69 "--auto-detect: auto detects current architecture "
70 "to run the tests\n"
11fdf7f2
TL
71 "--shani-on: use SHA extensions, default: auto-detect\n"
72 "--shani-off: don't use SHA extensions\n", name);
73}
74
9f95a23c
TL
75static void
76print_hw_features(void)
77{
78 const struct {
79 uint64_t feat_val;
80 const char *feat_name;
81 } feat_tab[] = {
82 { IMB_FEATURE_SHANI, "SHANI" },
83 { IMB_FEATURE_AESNI, "AESNI" },
84 { IMB_FEATURE_PCLMULQDQ, "PCLMULQDQ" },
85 { IMB_FEATURE_CMOV, "CMOV" },
86 { IMB_FEATURE_SSE4_2, "SSE4.2" },
87 { IMB_FEATURE_AVX, "AVX" },
88 { IMB_FEATURE_AVX2, "AVX2" },
89 { IMB_FEATURE_AVX512_SKX, "AVX512(SKX)" },
90 { IMB_FEATURE_VAES, "VAES" },
91 { IMB_FEATURE_VPCLMULQDQ, "VPCLMULQDQ" },
92 };
93 MB_MGR *p_mgr = NULL;
94 unsigned i;
95
96 printf("Detected hardware features:\n");
97
98 p_mgr = alloc_mb_mgr(0);
99 if (p_mgr == NULL) {
100 printf("\tERROR\n");
101 return;
102 }
103
104 for (i = 0; i < IMB_DIM(feat_tab); i++) {
105 const uint64_t val = feat_tab[i].feat_val;
106
107 printf("\t%-*.*s : %s\n", 12, 12, feat_tab[i].feat_name,
108 ((p_mgr->features & val) == val) ? "OK" : "n/a");
109 }
110
111 free_mb_mgr(p_mgr);
112}
113
114static void
115detect_arch(int *p_do_aesni_emu, int *p_do_sse, int *p_do_avx,
116 int *p_do_avx2, int *p_do_avx512, int *p_do_pclmulqdq)
117{
118 const uint64_t detect_sse =
119 IMB_FEATURE_SSE4_2 | IMB_FEATURE_CMOV | IMB_FEATURE_AESNI;
120 const uint64_t detect_avx =
121 IMB_FEATURE_AVX | IMB_FEATURE_CMOV | IMB_FEATURE_AESNI;
122 const uint64_t detect_avx2 = IMB_FEATURE_AVX2 | detect_avx;
123 const uint64_t detect_avx512 = IMB_FEATURE_AVX512_SKX | detect_avx2;
124 const uint64_t detect_pclmulqdq = IMB_FEATURE_PCLMULQDQ;
125 MB_MGR *p_mgr = NULL;
126
127 if (p_do_aesni_emu == NULL || p_do_sse == NULL ||
128 p_do_avx == NULL || p_do_avx2 == NULL ||
129 p_do_avx512 == NULL)
130 return;
131
132 *p_do_aesni_emu = 1;
133 *p_do_sse = 1;
134 *p_do_avx = 1;
135 *p_do_avx2 = 1;
136 *p_do_avx512 = 1;
137 *p_do_pclmulqdq = 1;
138
139 p_mgr = alloc_mb_mgr(0);
140 if (p_mgr == NULL) {
141 printf("Architecture auto detect error!\n");
142 return;
143 }
144
145 if ((p_mgr->features & detect_avx512) != detect_avx512)
146 *p_do_avx512 = 0;
147
148 if ((p_mgr->features & detect_avx2) != detect_avx2)
149 *p_do_avx2 = 0;
150
151 if ((p_mgr->features & detect_avx) != detect_avx)
152 *p_do_avx = 0;
153
154 if ((p_mgr->features & detect_sse) != detect_sse)
155 *p_do_sse = 0;
156
157 if ((p_mgr->features & detect_pclmulqdq) != detect_pclmulqdq)
158 *p_do_pclmulqdq = 0;
159
160 free_mb_mgr(p_mgr);
161}
162
11fdf7f2
TL
163int
164main(int argc, char **argv)
165{
9f95a23c
TL
166 const char *arch_str_tab[ARCH_NUMOF] = {
167 "SSE", "AVX", "AVX2", "AVX512", "NO_AESNI"
168 };
169 enum arch_type arch_type_tab[ARCH_NUMOF] = {
170 ARCH_SSE, ARCH_AVX, ARCH_AVX2, ARCH_AVX512, ARCH_NO_AESNI
171 };
11fdf7f2 172 int i, do_sse = 1, do_avx = 1, do_avx2 = 1, do_avx512 = 1;
9f95a23c
TL
173 int do_aesni_emu = 1, do_gcm = 1;
174 int auto_detect = 0;
11fdf7f2
TL
175 MB_MGR *p_mgr = NULL;
176 uint64_t flags = 0;
9f95a23c
TL
177 int errors = 0;
178
179 /* Check version number */
180 if (imb_get_version() < IMB_VERSION(0, 50, 0))
181 printf("Library version detection unsupported!\n");
182 else
183 printf("Detected library version: %s\n", imb_get_version_str());
184
185 /* Print available CPU features */
186 print_hw_features();
11fdf7f2
TL
187
188 for (i = 1; i < argc; i++) {
189 if (strcmp(argv[i], "-h") == 0) {
190 usage(argv[0]);
191 return EXIT_SUCCESS;
9f95a23c
TL
192 } else if (strcmp(argv[i], "--no-aesni-emu") == 0) {
193 do_aesni_emu = 0;
11fdf7f2
TL
194 } else if (strcmp(argv[i], "--no-avx512") == 0) {
195 do_avx512 = 0;
196 } else if (strcmp(argv[i], "--no-avx2") == 0) {
197 do_avx2 = 0;
198 } else if (strcmp(argv[i], "--no-avx") == 0) {
199 do_avx = 0;
200 } else if (strcmp(argv[i], "--no-sse") == 0) {
201 do_sse = 0;
202 } else if (strcmp(argv[i], "--shani-on") == 0) {
203 flags &= (~IMB_FLAG_SHANI_OFF);
204 } else if (strcmp(argv[i], "--shani-off") == 0) {
205 flags |= IMB_FLAG_SHANI_OFF;
9f95a23c
TL
206 } else if (strcmp(argv[i], "--no-gcm") == 0) {
207 do_gcm = 0;
208 } else if (strcmp(argv[i], "--auto-detect") == 0) {
209 auto_detect = 1;
11fdf7f2
TL
210 } else {
211 usage(argv[0]);
212 return EXIT_FAILURE;
213 }
214 }
215
9f95a23c
TL
216 if (auto_detect)
217 detect_arch(&do_aesni_emu, &do_sse, &do_avx,
218 &do_avx2, &do_avx512, &do_gcm);
11fdf7f2 219
9f95a23c
TL
220 for (i = 0; i < ARCH_NUMOF; i++) {
221 const enum arch_type atype = arch_type_tab[i];
11fdf7f2 222
9f95a23c
TL
223 switch (atype) {
224 case ARCH_SSE:
225 if (!do_sse)
226 continue;
227 p_mgr = alloc_mb_mgr(flags);
228 if (p_mgr == NULL) {
229 printf("Error allocating MB_MGR structure!\n");
230 return EXIT_FAILURE;
231 }
232 init_mb_mgr_sse(p_mgr);
233 break;
234 case ARCH_AVX:
235 if (!do_avx)
236 continue;
237 p_mgr = alloc_mb_mgr(flags);
238 if (p_mgr == NULL) {
239 printf("Error allocating MB_MGR structure!\n");
240 return EXIT_FAILURE;
241 }
242 init_mb_mgr_avx(p_mgr);
243 break;
244 case ARCH_AVX2:
245 if (!do_avx2)
246 continue;
247 p_mgr = alloc_mb_mgr(flags);
248 if (p_mgr == NULL) {
249 printf("Error allocating MB_MGR structure!\n");
250 return EXIT_FAILURE;
251 }
252 init_mb_mgr_avx2(p_mgr);
253 break;
254 case ARCH_AVX512:
255 if (!do_avx512)
256 continue;
257 p_mgr = alloc_mb_mgr(flags);
258 if (p_mgr == NULL) {
259 printf("Error allocating MB_MGR structure!\n");
260 return EXIT_FAILURE;
261 }
262 init_mb_mgr_avx512(p_mgr);
263 break;
264 case ARCH_NO_AESNI:
265 if (!do_aesni_emu)
266 continue;
267 p_mgr = alloc_mb_mgr(flags | IMB_FLAG_AESNI_OFF);
268 if (p_mgr == NULL) {
269 printf("Error allocating MB_MGR structure!\n");
270 return EXIT_FAILURE;
271 }
272 init_mb_mgr_sse(p_mgr);
273 break;
274 default:
275 printf("Architecture type '%d' error!\n", (int) atype);
276 continue;
277 }
11fdf7f2 278
9f95a23c 279 printf("Testing %s interface\n", arch_str_tab[i]);
11fdf7f2 280
9f95a23c
TL
281 errors += known_answer_test(p_mgr);
282 errors += do_test(p_mgr);
283 errors += ctr_test(atype, p_mgr);
f67539c2 284 errors += pon_test(atype, p_mgr);
9f95a23c
TL
285 if (do_gcm)
286 errors += gcm_test(p_mgr);
287 errors += customop_test(p_mgr);
288 errors += des_test(atype, p_mgr);
289 errors += ccm_test(atype, p_mgr);
290 errors += cmac_test(atype, p_mgr);
f67539c2
TL
291 errors += zuc_test(atype, p_mgr);
292 errors += kasumi_test(atype, p_mgr);
293 errors += snow3g_test(atype, p_mgr);
9f95a23c
TL
294 errors += hmac_sha1_test(atype, p_mgr);
295 errors += hmac_sha256_sha512_test(atype, p_mgr);
296 errors += hmac_md5_test(atype, p_mgr);
297 errors += aes_test(atype, p_mgr);
f67539c2 298 errors += ecb_test(atype, p_mgr);
9f95a23c 299 errors += sha_test(atype, p_mgr);
f67539c2
TL
300 errors += chained_test(atype, p_mgr);
301 errors += api_test(atype, p_mgr);
302 errors += direct_api_test(atype, p_mgr);
9f95a23c 303 free_mb_mgr(p_mgr);
11fdf7f2
TL
304 }
305
f67539c2 306 if (errors) {
9f95a23c 307 printf("Test completed: FAIL\n");
f67539c2
TL
308 return EXIT_FAILURE;
309 }
310
311 printf("Test completed: PASS\n");
11fdf7f2 312
11fdf7f2
TL
313 return EXIT_SUCCESS;
314}