cryptodev_test renamed to speed.
[cryptodev-linux.git] / examples / speed.c
blob77d6d29f035e0a3003c5f69dd1b7142a4fb52355
1 /* cryptodev_test - simple benchmark tool for cryptodev
3 * Copyright (C) 2010 by Phil Sutter <phil.sutter@viprinet.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 #include <fcntl.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sys/ioctl.h>
24 #include <sys/time.h>
25 #include <sys/types.h>
26 #include "../cryptodev.h"
28 #if 1
29 #define TEST_ALGO CRYPTO_AES_CBC
30 #define TEST_KEYLEN 32
31 #else
32 #define TEST_ALGO CRYPTO_NULL
33 #define TEST_KEYLEN 0
34 #endif
36 /* transcode a total of 1GB */
37 #define TOT_LEN (1024 * 1024 * 1024)
39 static long udifftimeval(struct timeval start, struct timeval end)
41 return (end.tv_usec - start.tv_usec) +
42 (end.tv_sec - start.tv_sec) * 1000 * 1000 + 0.5;
45 int encrypt_data(struct session_op *sess, int fdc, int len, int chunksize, int flags)
47 struct crypt_op cop;
48 char *buffer, iv[32];
49 int i, buffercount;
50 static int val = 23;
51 struct timeval start, end;
53 if (len % chunksize) {
54 printf("got milk?!\n");
55 return 1;
58 buffercount = len / chunksize;
60 buffer = malloc(chunksize);
61 memset(iv, 0x23, 32);
63 printf("Encrypting %d x %d bytes: ", buffercount, chunksize);
64 fflush(stdout);
66 memset(buffer, val++, chunksize);
68 gettimeofday(&start, NULL);
69 for (i = 0; i < buffercount; i++) {
70 memset(&cop, 0, sizeof(cop));
71 cop.ses = sess->ses;
72 cop.len = chunksize;
73 cop.iv = (unsigned char *)iv;
74 cop.op = COP_ENCRYPT;
75 cop.flags = flags;
76 cop.src = cop.dst = (unsigned char *)buffer;
78 if (ioctl(fdc, CIOCCRYPT, &cop)) {
79 perror("ioctl(CIOCCRYPT)");
80 return 1;
83 gettimeofday(&end, NULL);
85 printf("done in %.5f seconds - %.5fMB/s\n", (double)udifftimeval(start, end) / 1000000.0,
86 (double)len / ((double)udifftimeval(start, end) / 1000000.0) / 1024 / 1024);
87 return 0;
90 int main(void)
92 int fd, i, fdc = -1;
93 struct session_op sess;
94 char keybuf[32];
96 if ((fd = open("/dev/crypto", O_RDWR, 0)) < 0) {
97 perror("open()");
98 return 1;
100 if (ioctl(fd, CRIOGET, &fdc)) {
101 perror("ioctl(CRIOGET)");
102 return 1;
104 memset(&sess, 0, sizeof(sess));
105 sess.cipher = TEST_ALGO;
106 sess.keylen = TEST_KEYLEN;
107 memset(keybuf, 0x42, 32);
108 sess.key = (unsigned char *)keybuf;
109 if (ioctl(fdc, CIOCGSESSION, &sess)) {
110 perror("ioctl(CIOCGSESSION)");
111 return 1;
114 printf("Standard operation:\n");
115 for (i = 256; i <= (64 * 4096); i *= 2) {
116 if (encrypt_data(&sess, fdc, TOT_LEN, i, 0))
117 break;
119 printf("Zero-Copy operation:\n");
120 for (i = 256; i <= (64 * 4096); i *= 2) {
121 if (encrypt_data(&sess, fdc, TOT_LEN, i, COP_FLAG_ZCOPY))
122 break;
125 close(fdc);
126 close(fd);
127 return 0;