Debian Codegraph @

speed-test/cryptoperf.h

raw symbols SCIP
1 /*
2 * Copyright (C) 2015 - 2022, Stephan Mueller <[email protected]>
3 *
4 * License: see LICENSE file in root directory
5 *
6 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
7 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
8 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
9 * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
10 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
11 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
12 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
13 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
14 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
15 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
16 * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
17 * DAMAGE.
18 */
19
20 #ifndef CRYPTOPERF_H
21 #define CRYPTOPERF_H
22
23 #define _GNU_SOURCE
24
25 #include <stdint.h>
26 #include <stdio.h>
27 #include <time.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <fcntl.h>
33 #include <unistd.h>
34 #include <errno.h>
35
36 #include "kcapi.h"
37
38 #define DRIVER_NAME "cryptoperf"
39
40 #if 0
41 #define dbg(fmt, ...) printf(DRIVER_NAME": " fmt, ##__VA_ARGS__)
42 #else
43 #define dbg(fmt, ...)
44 #endif
45
46 /* default execution time for a test in seconds */
47 #define DFLT_EXECTIME 10
48
49 #define SUCCESS 1
50 #define FAILURE 0
51
52 /**
53 * Test parameters specifying which types of tests should be executed.
54 * @exectime: execution time for current test -- if 0, use the predefined
55 * execution time for the test.
56 * @len: number of blocks to test
57 * @aio: number of iovs to send. If 0, don't use aio.
58 * @accesstype: KCAPI_ACCESS_VMSPLICE or KCAPI_ACCESS_SENDMSG
59 */
60 struct cp_test_param {
61 unsigned int exectime;
62 size_t len;
63 unsigned int aio;
64 int accesstype;
65 };
66
67 /*
68 * Cipher-specific test data
69 */
70 struct rng_def {
71 unsigned int blocksize;
72 unsigned char *scratchpad;
73 size_t inputlen;
74 struct kcapi_handle *handle;
75 };
76
77 struct hash_def {
78 unsigned int hmac; /* config option */
79 unsigned char *scratchpad;
80 size_t inputlen;
81 struct kcapi_handle *handle;
82 };
83
84 struct skcipher_def {
85 unsigned int keysize; /* config option */
86 unsigned char *scratchpad;
87 unsigned char *iv;
88 size_t inputlen;
89 struct iovec *iovec;
90 struct kcapi_handle *handle;
91 };
92
93 struct aead_def {
94 unsigned int keysize; /* config option */
95 unsigned char *input;
96 unsigned char *output;
97 unsigned char *iv;
98 size_t indatalen;
99 size_t outdatalen;
100 size_t assoclen;
101 struct iovec *iniov;
102 struct iovec *outiov;
103 struct kcapi_handle *handle;
104 };
105
106 /*
107 * Test result data
108 */
109 struct cp_res {
110 uint64_t rounds;
111 uint64_t processed_bytes;
112 uint64_t totaltime;
113 size_t byteperop;
114 size_t chunksize;
115 };
116
117 /*
118 * Test case definition
119 */
120 struct cp_test;
121 struct cp_test {
122 char *testname;
123 char *driver_name;
124 char *type;
125 int enc;
126 unsigned int exectime;
127 struct cp_res results;
128 struct cp_test_param *test_params;
129 int (*init_test)(struct cp_test *test);
130 size_t (*exec_test)(struct cp_test *test);
131 void (*fini_test)(struct cp_test *test);
132
133 /* information not to be set by user */
134 union {
135 struct rng_def rng;
136 struct skcipher_def skcipher;
137 struct aead_def aead;
138 struct hash_def hash;
139 } u;
140 };
141
142 static inline uint64_t cp_ts2u64(struct timespec *ts)
143 {
144 uint64_t upper = (uint64_t)ts->tv_sec;
145
146 upper = upper << 32;
147 return (upper | (uint64_t)ts->tv_nsec);
148 }
149
150 /*
151 * This is x86 specific to reduce the CPU jitter
152 */
153 static inline void cp_cpusetup(void)
154 {
155 #ifdef __X8664___
156 asm volatile("cpuid"::"a" (0), "c" (0): "memory");
157 asm volatile("cpuid"::"a" (0), "c" (0): "memory");
158 asm volatile("cpuid"::"a" (0), "c" (0): "memory");
159 #endif
160 }
161
162 static inline void cp_get_nstime(struct timespec *ts)
163 {
164 clock_gettime(CLOCK_REALTIME, ts);
165 }
166
167 static inline void cp_start_time(struct timespec *ts)
168 {
169 cp_cpusetup();
170 cp_get_nstime(ts);
171 }
172
173 static inline void cp_end_time(struct timespec *ts)
174 {
175 cp_get_nstime(ts);
176 }
177
178 static inline void *cp_zalloc(size_t len)
179 {
180 return calloc(1, len);
181 }
182 static inline void cp_zfree(void *ptr, unsigned int len)
183 {
184 memset(ptr, 0, len);
185 free(ptr);
186 }
187
188 /*
189 * general functions
190 */
191 char *cp_print_status(struct cp_test *test, int raw);
192 int cp_exec_test(struct cp_test *test);
193 int cp_read_random(unsigned char *buf, size_t buflen);
194
195 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
196
197 /* test invocations */
198 void cp_hash_register(struct cp_test **hash_test, size_t *entries);
199 void cp_rng_register(struct cp_test **rng_test, size_t *entries);
200 void cp_skcipher_register(struct cp_test **skcipher_test, size_t *entries);
201 void cp_aead_register(struct cp_test **aead_test, size_t *entries);
202 #endif /* CRYPTOPERF_H */