aboutsummaryrefslogtreecommitdiffstats
path: root/recipes-kernel/cryptodev/sdk_patches/0050-add-basic-optargs-support-for-async_speed-test.patch
blob: f54286aee379cf9e741749bbc5f86682832dd07e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
From 13cb1f2dcf8865b076a7e8290d8f864d91a2d7c7 Mon Sep 17 00:00:00 2001
From: Cristian Stoica <cristian.stoica@nxp.com>
Date: Mon, 24 Oct 2016 16:33:55 +0300
Subject: [PATCH 050/104] add basic optargs support for async_speed test

Signed-off-by: Cristian Stoica <cristian.stoica@nxp.com>
---
 tests/async_speed.c | 302 +++++++++++++++++++++++++++++++++++-----------------
 1 file changed, 202 insertions(+), 100 deletions(-)

diff --git a/tests/async_speed.c b/tests/async_speed.c
index 15ab80c..fff3414 100644
--- a/tests/async_speed.c
+++ b/tests/async_speed.c
@@ -27,9 +27,45 @@
 #include <sys/types.h>
 #include <signal.h>
 #include <crypto/cryptodev.h>
+#include <stdbool.h>
+#include <unistd.h>
 
 #ifdef ENABLE_ASYNC
 
+struct test_params {
+	bool tflag;
+	bool nflag;
+	int tvalue;
+	int nvalue;
+};
+
+const char usage_str[] = "Usage: %s [OPTION]... <cipher>|<hash>\n"
+	"Run benchmark test for cipher or hash\n\n"
+	"  -t <secs>\t" "time to run each test (default 10 secs)\n"
+	"  -n <bytes>\t" "size of the test buffer\n"
+	"  -h\t\t" "show this help\n"
+;
+
+int run_null(int fdc, struct test_params tp);
+int run_aes_cbc(int fdc, struct test_params tp);
+int run_aes_xts(int fdc, struct test_params tp);
+int run_crc32c(int fdc, struct test_params tp);
+int run_sha1(int fdc, struct test_params tp);
+int run_sha256(int fdc, struct test_params tp);
+
+#define ALG_COUNT	6
+struct {
+	char *name;
+	int (*func)(int, struct test_params);
+} ciphers[ALG_COUNT] = {
+	{"null",	run_null},
+	{"aes-cbc",	run_aes_cbc},
+	{"aes-xts",	run_aes_xts},
+	{"crc32c",	run_crc32c},
+	{"sha1",	run_sha1},
+	{"sha256",	run_sha256},
+};
+
 static double udifftimeval(struct timeval start, struct timeval end)
 {
 	return (double)(end.tv_usec - start.tv_usec) +
@@ -61,7 +97,7 @@ static void value2human(double bytes, double time, double* data, double* speed,c
 }
 
 
-int encrypt_data(struct session_op *sess, int fdc, int chunksize, int alignmask)
+int encrypt_data(int fdc, struct test_params tp, struct session_op *sess)
 {
 	struct crypt_op cop;
 	char *buffer[64], iv[32];
@@ -72,31 +108,33 @@ int encrypt_data(struct session_op *sess, int fdc, int chunksize, int alignmask)
 	double secs, ddata, dspeed;
 	char metric[16];
 	int rc, wqueue = 0, bufidx = 0;
+	int alignmask;
 
 	memset(iv, 0x23, 32);
 
-	printf("\tEncrypting in chunks of %d bytes: ", chunksize);
+	printf("\tEncrypting in chunks of %d bytes: ", tp.nvalue);
 	fflush(stdout);
 
+	alignmask = get_alignmask(fdc, sess);
 	for (rc = 0; rc < 64; rc++) {
 		if (alignmask) {
-			if (posix_memalign((void **)(buffer + rc), alignmask + 1, chunksize)) {
+			if (posix_memalign((void **)(buffer + rc), alignmask + 1, tp.nvalue)) {
 				printf("posix_memalign() failed!\n");
 				return 1;
 			}
 		} else {
-			if (!(buffer[rc] = malloc(chunksize))) {
+			if (!(buffer[rc] = malloc(tp.nvalue))) {
 				perror("malloc()");
 				return 1;
 			}
 		}
-		memset(buffer[rc], val++, chunksize);
+		memset(buffer[rc], val++, tp.nvalue);
 	}
 	pfd.fd = fdc;
 	pfd.events = POLLOUT | POLLIN;
 
 	must_finish = 0;
-	alarm(5);
+	alarm(tp.tvalue);
 
 	gettimeofday(&start, NULL);
 	do {
@@ -111,7 +149,7 @@ int encrypt_data(struct session_op *sess, int fdc, int chunksize, int alignmask)
 		if (pfd.revents & POLLOUT) {
 			memset(&cop, 0, sizeof(cop));
 			cop.ses = sess->ses;
-			cop.len = chunksize;
+			cop.len = tp.nvalue;
 			cop.iv = (unsigned char *)iv;
 			cop.op = COP_ENCRYPT;
 			cop.src = cop.dst = (unsigned char *)buffer[bufidx];
@@ -146,25 +184,75 @@ int encrypt_data(struct session_op *sess, int fdc, int chunksize, int alignmask)
 	return 0;
 }
 
-int main(void)
+void usage(char *cmd_name)
 {
-	int fd, i, fdc = -1, alignmask = 0;
-	struct session_op sess;
-#ifdef CIOCGSESSINFO
-	struct session_info_op siop;
-#endif
-	char keybuf[32];
+	printf(usage_str, cmd_name);
+}
 
-	signal(SIGALRM, alarm_handler);
+int run_test(int id, struct test_params tp)
+{
+	int fd;
+	int fdc;
 
-	if ((fd = open("/dev/crypto", O_RDWR, 0)) < 0) {
+	fd = open("/dev/crypto", O_RDWR, 0);
+	if (fd < 0) {
 		perror("open()");
-		return 1;
+		return fd;
 	}
 	if (ioctl(fd, CRIOGET, &fdc)) {
 		perror("ioctl(CRIOGET)");
-		return 1;
+		return -EINVAL;
+	}
+
+	ciphers[id].func(fdc, tp);
+
+	close(fdc);
+	close(fd);
+}
+
+int get_alignmask(int fdc, struct session_op *sess)
+{
+	int alignmask;
+
+#ifdef CIOCGSESSINFO
+	struct session_info_op siop;
+
+	siop.ses = sess->ses;
+	if (ioctl(fdc, CIOCGSESSINFO, &siop)) {
+		perror("ioctl(CIOCGSESSINFO)");
+		return -EINVAL;
+	}
+	alignmask = siop.alignmask;
+#else
+	alignmask = 0;
+#endif
+
+	return alignmask;
+}
+
+void do_test_vectors(int fdc, struct test_params tp, struct session_op *sess)
+{
+	int i;
+
+	if (tp.nflag) {
+		encrypt_data(fdc, tp, sess);
+	} else {
+		for (i = 256; i <= (64 * 1024); i *= 2) {
+			tp.nvalue = i;
+			if (encrypt_data(fdc, tp, sess)) {
+				break;
+			}
+		}
 	}
+}
+
+
+int run_null(int fdc, struct test_params tp)
+{
+	struct session_op sess;
+	char keybuf[32];
+	int alignmask;
+	int i;
 
 	fprintf(stderr, "Testing NULL cipher: \n");
 	memset(&sess, 0, sizeof(sess));
@@ -173,21 +261,19 @@ int main(void)
 	sess.key = (unsigned char *)keybuf;
 	if (ioctl(fdc, CIOCGSESSION, &sess)) {
 		perror("ioctl(CIOCGSESSION)");
-		return 1;
-	}
-#ifdef CIOCGSESSINFO
-	siop.ses = sess.ses;
-	if (ioctl(fdc, CIOCGSESSINFO, &siop)) {
-		perror("ioctl(CIOCGSESSINFO)");
-		return 1;
+		return -EINVAL;
 	}
-	alignmask = siop.alignmask;
-#endif
 
-	for (i = 256; i <= (64 * 4096); i *= 2) {
-		if (encrypt_data(&sess, fdc, i, alignmask))
-			break;
-	}
+	do_test_vectors(fdc, tp, &sess);
+	return 0;
+}
+
+int run_aes_cbc(int fdc, struct test_params tp)
+{
+	struct session_op sess;
+	char keybuf[32];
+	int alignmask;
+	int i;
 
 	fprintf(stderr, "\nTesting AES-128-CBC cipher: \n");
 	memset(&sess, 0, sizeof(sess));
@@ -197,21 +283,17 @@ int main(void)
 	sess.key = (unsigned char *)keybuf;
 	if (ioctl(fdc, CIOCGSESSION, &sess)) {
 		perror("ioctl(CIOCGSESSION)");
-		return 1;
-	}
-#ifdef CIOCGSESSINFO
-	siop.ses = sess.ses;
-	if (ioctl(fdc, CIOCGSESSINFO, &siop)) {
-		perror("ioctl(CIOCGSESSINFO)");
-		return 1;
+		return -EINVAL;
 	}
-	alignmask = siop.alignmask;
-#endif
 
-	for (i = 256; i <= (64 * 1024); i *= 2) {
-		if (encrypt_data(&sess, fdc, i, alignmask))
-			break;
-	}
+	do_test_vectors(fdc, tp, &sess);
+	return 0;
+}
+
+int run_aes_xts(int fdc, struct test_params tp)
+{
+	struct session_op sess;
+	char keybuf[32];
 
 	fprintf(stderr, "\nTesting AES-256-XTS cipher: \n");
 	memset(&sess, 0, sizeof(sess));
@@ -221,21 +303,16 @@ int main(void)
 	sess.key = (unsigned char *)keybuf;
 	if (ioctl(fdc, CIOCGSESSION, &sess)) {
 		perror("ioctl(CIOCGSESSION)");
-		return 1;
+		return -EINVAL;
 	}
-#ifdef CIOCGSESSINFO
-	siop.ses = sess.ses;
-	if (ioctl(fdc, CIOCGSESSINFO, &siop)) {
-		perror("ioctl(CIOCGSESSION)");
-		return 1;
-	}
-	alignmask = siop.alignmask;
-#endif
 
-	for (i = 256; i <= (64 * 1024); i *= 2) {
-		if (encrypt_data(&sess, fdc, i, alignmask))
-			break;
-	}
+	do_test_vectors(fdc, tp, &sess);
+	return 0;
+}
+
+int run_crc32c(int fdc, struct test_params tp)
+{
+	struct session_op sess;
 
 	fprintf(stderr, "\nTesting CRC32C hash: \n");
 	memset(&sess, 0, sizeof(sess));
@@ -244,21 +321,14 @@ int main(void)
 		perror("ioctl(CIOCGSESSION)");
 		return 1;
 	}
-#ifdef CIOCGSESSINFO
-	siop.ses = sess.ses;
-	if (ioctl(fdc, CIOCGSESSINFO, &siop)) {
-		perror("ioctl(CIOCGSESSION)");
-		return 1;
-	}
-	printf("requested hash CRYPTO_CRC32C, got %s with driver %s\n",
-			siop.hash_info.cra_name, siop.hash_info.cra_driver_name);
-	alignmask = siop.alignmask;
-#endif
 
-	for (i = 256; i <= (64 * 1024); i *= 2) {
-		if (encrypt_data(&sess, fdc, i, alignmask))
-			break;
-	}
+	do_test_vectors(fdc, tp, &sess);
+	return 0;
+}
+
+int run_sha1(int fdc, struct test_params tp)
+{
+	struct session_op sess;
 
 	fprintf(stderr, "\nTesting SHA-1 hash: \n");
 	memset(&sess, 0, sizeof(sess));
@@ -267,21 +337,14 @@ int main(void)
 		perror("ioctl(CIOCGSESSION)");
 		return 1;
 	}
-#ifdef CIOCGSESSINFO
-	siop.ses = sess.ses;
-	if (ioctl(fdc, CIOCGSESSINFO, &siop)) {
-		perror("ioctl(CIOCGSESSION)");
-		return 1;
-	}
-	printf("requested hash CRYPTO_SHA1, got %s with driver %s\n",
-			siop.hash_info.cra_name, siop.hash_info.cra_driver_name);
-	alignmask = siop.alignmask;
-#endif
 
-	for (i = 256; i <= (64 * 1024); i *= 2) {
-		if (encrypt_data(&sess, fdc, i, alignmask))
-			break;
-	}
+	do_test_vectors(fdc, tp, &sess);
+	return 0;
+}
+
+int run_sha256(int fdc, struct test_params tp)
+{
+	struct session_op sess;
 
 	fprintf(stderr, "\nTesting SHA2-256 hash: \n");
 	memset(&sess, 0, sizeof(sess));
@@ -290,25 +353,64 @@ int main(void)
 		perror("ioctl(CIOCGSESSION)");
 		return 1;
 	}
-#ifdef CIOCGSESSINFO
-	siop.ses = sess.ses;
-	if (ioctl(fdc, CIOCGSESSINFO, &siop)) {
-		perror("ioctl(CIOCGSESSION)");
-		return 1;
-	}
-	printf("requested hash CRYPTO_SHA2_256, got %s with driver %s\n",
-			siop.hash_info.cra_name, siop.hash_info.cra_driver_name);
-	alignmask = siop.alignmask;
-#endif
 
-	for (i = 256; i <= (64 * 1024); i *= 2) {
-		if (encrypt_data(&sess, fdc, i, alignmask))
+	do_test_vectors(fdc, tp, &sess);
+	return 0;
+}
+
+int main(int argc, char **argv)
+{
+	int i;
+	int c;
+	int index;
+	bool alg_flag;
+	char *alg_name;
+	struct test_params tp;
+
+	tp.tflag = false;
+	tp.nflag = false;
+	alg_flag = false;
+	opterr = 0;
+	while ((c = getopt(argc, argv, "hn:t:")) != -1) {
+		switch (c) {
+		case 'n':
+			tp.nvalue = atoi(optarg);
+			tp.nflag = true;
+			break;
+		case 't':
+			tp.tvalue = atoi(optarg);
+			tp.tflag = true;
 			break;
+		case 'h': /* no break */
+		default:
+			usage(argv[0]);
+			exit(1);
+		}
+	}
+
+	/* the name of a specific test asked on the command line */
+	if (optind < argc) {
+		alg_name = argv[optind];
+		alg_flag = true;
+	}
+
+	/* default test time */
+	if (!tp.tflag) {
+		tp.tvalue = 5;
+	}
+
+	signal(SIGALRM, alarm_handler);
+
+	for (i = 0; i < ALG_COUNT; i++) {
+		if (alg_flag) {
+			if (strcmp(alg_name, ciphers[i].name) == 0) {
+				run_test(i, tp);
+			}
+		} else {
+			run_test(i, tp);
+		}
 	}
 
-end:
-	close(fdc);
-	close(fd);
 	return 0;
 }
 
-- 
2.10.2