app/crypto-perf: move verify as single test type
[dpdk.git] / app / test-crypto-perf / main.c
1 #include <stdio.h>
2 #include <unistd.h>
3
4 #include <rte_eal.h>
5 #include <rte_cryptodev.h>
6
7 #include "cperf.h"
8 #include "cperf_options.h"
9 #include "cperf_test_vector_parsing.h"
10 #include "cperf_test_throughput.h"
11 #include "cperf_test_latency.h"
12 #include "cperf_test_verify.h"
13
14 const char *cperf_test_type_strs[] = {
15         [CPERF_TEST_TYPE_THROUGHPUT] = "throughput",
16         [CPERF_TEST_TYPE_LATENCY] = "latency",
17         [CPERF_TEST_TYPE_VERIFY] = "verify"
18 };
19
20 const char *cperf_op_type_strs[] = {
21         [CPERF_CIPHER_ONLY] = "cipher-only",
22         [CPERF_AUTH_ONLY] = "auth-only",
23         [CPERF_CIPHER_THEN_AUTH] = "cipher-then-auth",
24         [CPERF_AUTH_THEN_CIPHER] = "auth-then-cipher",
25         [CPERF_AEAD] = "aead"
26 };
27
28 const struct cperf_test cperf_testmap[] = {
29                 [CPERF_TEST_TYPE_THROUGHPUT] = {
30                                 cperf_throughput_test_constructor,
31                                 cperf_throughput_test_runner,
32                                 cperf_throughput_test_destructor
33                 },
34                 [CPERF_TEST_TYPE_LATENCY] = {
35                                 cperf_latency_test_constructor,
36                                 cperf_latency_test_runner,
37                                 cperf_latency_test_destructor
38                 },
39                 [CPERF_TEST_TYPE_VERIFY] = {
40                                 cperf_verify_test_constructor,
41                                 cperf_verify_test_runner,
42                                 cperf_verify_test_destructor
43                 }
44 };
45
46 static int
47 cperf_initialize_cryptodev(struct cperf_options *opts, uint8_t *enabled_cdevs)
48 {
49         uint8_t cdev_id, enabled_cdev_count = 0, nb_lcores;
50         int ret;
51
52         enabled_cdev_count = rte_cryptodev_devices_get(opts->device_type,
53                         enabled_cdevs, RTE_CRYPTO_MAX_DEVS);
54         if (enabled_cdev_count == 0) {
55                 printf("No crypto devices type %s available\n",
56                                 opts->device_type);
57                 return -EINVAL;
58         }
59
60         nb_lcores = rte_lcore_count() - 1;
61
62         if (enabled_cdev_count > nb_lcores) {
63                 printf("Number of capable crypto devices (%d) "
64                                 "has to be less or equal to number of slave "
65                                 "cores (%d)\n", enabled_cdev_count, nb_lcores);
66                 return -EINVAL;
67         }
68
69         for (cdev_id = 0; cdev_id < enabled_cdev_count &&
70                         cdev_id < RTE_CRYPTO_MAX_DEVS; cdev_id++) {
71
72                 struct rte_cryptodev_config conf = {
73                                 .nb_queue_pairs = 1,
74                                 .socket_id = SOCKET_ID_ANY,
75                                 .session_mp = {
76                                         .nb_objs = 2048,
77                                         .cache_size = 64
78                                 }
79                         };
80                 struct rte_cryptodev_qp_conf qp_conf = {
81                                 .nb_descriptors = 2048
82                 };
83
84                 ret = rte_cryptodev_configure(enabled_cdevs[cdev_id], &conf);
85                 if (ret < 0) {
86                         printf("Failed to configure cryptodev %u",
87                                         enabled_cdevs[cdev_id]);
88                         return -EINVAL;
89                 }
90
91                 ret = rte_cryptodev_queue_pair_setup(enabled_cdevs[cdev_id], 0,
92                                 &qp_conf, SOCKET_ID_ANY);
93                         if (ret < 0) {
94                                 printf("Failed to setup queue pair %u on "
95                                         "cryptodev %u", 0, cdev_id);
96                                 return -EINVAL;
97                         }
98
99                 ret = rte_cryptodev_start(enabled_cdevs[cdev_id]);
100                 if (ret < 0) {
101                         printf("Failed to start device %u: error %d\n",
102                                         enabled_cdevs[cdev_id], ret);
103                         return -EPERM;
104                 }
105         }
106
107         return enabled_cdev_count;
108 }
109
110 static int
111 cperf_verify_devices_capabilities(struct cperf_options *opts,
112                 uint8_t *enabled_cdevs, uint8_t nb_cryptodevs)
113 {
114         struct rte_cryptodev_sym_capability_idx cap_idx;
115         const struct rte_cryptodev_symmetric_capability *capability;
116
117         uint8_t i, cdev_id;
118         int ret;
119
120         for (i = 0; i < nb_cryptodevs; i++) {
121
122                 cdev_id = enabled_cdevs[i];
123
124                 if (opts->op_type == CPERF_AUTH_ONLY ||
125                                 opts->op_type == CPERF_CIPHER_THEN_AUTH ||
126                                 opts->op_type == CPERF_AUTH_THEN_CIPHER ||
127                                 opts->op_type == CPERF_AEAD)  {
128
129                         cap_idx.type = RTE_CRYPTO_SYM_XFORM_AUTH;
130                         cap_idx.algo.auth = opts->auth_algo;
131
132                         capability = rte_cryptodev_sym_capability_get(cdev_id,
133                                         &cap_idx);
134                         if (capability == NULL)
135                                 return -1;
136
137                         ret = rte_cryptodev_sym_capability_check_auth(
138                                         capability,
139                                         opts->auth_key_sz,
140                                         opts->auth_digest_sz,
141                                         opts->auth_aad_sz);
142                         if (ret != 0)
143                                 return ret;
144                 }
145
146                 if (opts->op_type == CPERF_CIPHER_ONLY ||
147                                 opts->op_type == CPERF_CIPHER_THEN_AUTH ||
148                                 opts->op_type == CPERF_AUTH_THEN_CIPHER ||
149                                 opts->op_type == CPERF_AEAD) {
150
151                         cap_idx.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
152                         cap_idx.algo.cipher = opts->cipher_algo;
153
154                         capability = rte_cryptodev_sym_capability_get(cdev_id,
155                                         &cap_idx);
156                         if (capability == NULL)
157                                 return -1;
158
159                         ret = rte_cryptodev_sym_capability_check_cipher(
160                                         capability,
161                                         opts->cipher_key_sz,
162                                         opts->cipher_iv_sz);
163                         if (ret != 0)
164                                 return ret;
165                 }
166         }
167
168         return 0;
169 }
170
171 static int
172 cperf_check_test_vector(struct cperf_options *opts,
173                 struct cperf_test_vector *test_vec)
174 {
175         if (opts->op_type == CPERF_CIPHER_ONLY) {
176                 if (opts->cipher_algo == RTE_CRYPTO_CIPHER_NULL) {
177                         if (test_vec->plaintext.data == NULL)
178                                 return -1;
179                 } else if (opts->cipher_algo != RTE_CRYPTO_CIPHER_NULL) {
180                         if (test_vec->plaintext.data == NULL)
181                                 return -1;
182                         if (test_vec->plaintext.length != opts->buffer_sz)
183                                 return -1;
184                         if (test_vec->ciphertext.data == NULL)
185                                 return -1;
186                         if (test_vec->ciphertext.length != opts->buffer_sz)
187                                 return -1;
188                         if (test_vec->iv.data == NULL)
189                                 return -1;
190                         if (test_vec->iv.length != opts->cipher_iv_sz)
191                                 return -1;
192                         if (test_vec->cipher_key.data == NULL)
193                                 return -1;
194                         if (test_vec->cipher_key.length != opts->cipher_key_sz)
195                                 return -1;
196                 }
197         } else if (opts->op_type == CPERF_AUTH_ONLY) {
198                 if (opts->auth_algo != RTE_CRYPTO_AUTH_NULL) {
199                         if (test_vec->plaintext.data == NULL)
200                                 return -1;
201                         if (test_vec->plaintext.length != opts->buffer_sz)
202                                 return -1;
203                         if (test_vec->auth_key.data == NULL)
204                                 return -1;
205                         if (test_vec->auth_key.length != opts->auth_key_sz)
206                                 return -1;
207                         if (test_vec->digest.data == NULL)
208                                 return -1;
209                         if (test_vec->digest.length != opts->auth_digest_sz)
210                                 return -1;
211                 }
212
213         } else if (opts->op_type == CPERF_CIPHER_THEN_AUTH ||
214                         opts->op_type == CPERF_AUTH_THEN_CIPHER) {
215                 if (opts->cipher_algo == RTE_CRYPTO_CIPHER_NULL) {
216                         if (test_vec->plaintext.data == NULL)
217                                 return -1;
218                         if (test_vec->plaintext.length != opts->buffer_sz)
219                                 return -1;
220                 } else if (opts->cipher_algo != RTE_CRYPTO_CIPHER_NULL) {
221                         if (test_vec->plaintext.data == NULL)
222                                 return -1;
223                         if (test_vec->plaintext.length != opts->buffer_sz)
224                                 return -1;
225                         if (test_vec->ciphertext.data == NULL)
226                                 return -1;
227                         if (test_vec->ciphertext.length != opts->buffer_sz)
228                                 return -1;
229                         if (test_vec->iv.data == NULL)
230                                 return -1;
231                         if (test_vec->iv.length != opts->cipher_iv_sz)
232                                 return -1;
233                         if (test_vec->cipher_key.data == NULL)
234                                 return -1;
235                         if (test_vec->cipher_key.length != opts->cipher_key_sz)
236                                 return -1;
237                 }
238                 if (opts->auth_algo != RTE_CRYPTO_AUTH_NULL) {
239                         if (test_vec->auth_key.data == NULL)
240                                 return -1;
241                         if (test_vec->auth_key.length != opts->auth_key_sz)
242                                 return -1;
243                         if (test_vec->digest.data == NULL)
244                                 return -1;
245                         if (test_vec->digest.length != opts->auth_digest_sz)
246                                 return -1;
247                 }
248         } else if (opts->op_type == CPERF_AEAD) {
249                 if (test_vec->plaintext.data == NULL)
250                         return -1;
251                 if (test_vec->plaintext.length != opts->buffer_sz)
252                         return -1;
253                 if (test_vec->aad.data == NULL)
254                         return -1;
255                 if (test_vec->aad.length != opts->auth_aad_sz)
256                         return -1;
257                 if (test_vec->digest.data == NULL)
258                         return -1;
259                 if (test_vec->digest.length != opts->auth_digest_sz)
260                         return -1;
261         }
262         return 0;
263 }
264
265 int
266 main(int argc, char **argv)
267 {
268         struct cperf_options opts = {0};
269         struct cperf_test_vector *t_vec = NULL;
270         struct cperf_op_fns op_fns;
271
272         void *ctx[RTE_MAX_LCORE] = { };
273
274         int nb_cryptodevs = 0;
275         uint8_t cdev_id, i;
276         uint8_t enabled_cdevs[RTE_CRYPTO_MAX_DEVS] = { 0 };
277
278         int ret;
279         uint32_t lcore_id;
280
281         /* Initialise DPDK EAL */
282         ret = rte_eal_init(argc, argv);
283         if (ret < 0)
284                 rte_exit(EXIT_FAILURE, "Invalid EAL arguments!\n");
285         argc -= ret;
286         argv += ret;
287
288         cperf_options_default(&opts);
289
290         ret = cperf_options_parse(&opts, argc, argv);
291         if (ret) {
292                 RTE_LOG(ERR, USER1, "Parsing on or more user options failed\n");
293                 goto err;
294         }
295
296         ret = cperf_options_check(&opts);
297         if (ret) {
298                 RTE_LOG(ERR, USER1,
299                                 "Checking on or more user options failed\n");
300                 goto err;
301         }
302
303         if (!opts.silent)
304                 cperf_options_dump(&opts);
305
306         nb_cryptodevs = cperf_initialize_cryptodev(&opts, enabled_cdevs);
307         if (nb_cryptodevs < 1) {
308                 RTE_LOG(ERR, USER1, "Failed to initialise requested crypto "
309                                 "device type\n");
310                 nb_cryptodevs = 0;
311                 goto err;
312         }
313
314         ret = cperf_verify_devices_capabilities(&opts, enabled_cdevs,
315                         nb_cryptodevs);
316         if (ret) {
317                 RTE_LOG(ERR, USER1, "Crypto device type does not support "
318                                 "capabilities requested\n");
319                 goto err;
320         }
321
322         if (opts.test_file != NULL) {
323                 t_vec = cperf_test_vector_get_from_file(&opts);
324                 if (t_vec == NULL) {
325                         RTE_LOG(ERR, USER1,
326                                         "Failed to create test vector for"
327                                         " specified file\n");
328                         goto err;
329                 }
330
331                 if (cperf_check_test_vector(&opts, t_vec)) {
332                         RTE_LOG(ERR, USER1, "Incomplete necessary test vectors"
333                                         "\n");
334                         goto err;
335                 }
336         } else {
337                 t_vec = cperf_test_vector_get_dummy(&opts);
338                 if (t_vec == NULL) {
339                         RTE_LOG(ERR, USER1,
340                                         "Failed to create test vector for"
341                                         " specified algorithms\n");
342                         goto err;
343                 }
344         }
345
346         ret = cperf_get_op_functions(&opts, &op_fns);
347         if (ret) {
348                 RTE_LOG(ERR, USER1, "Failed to find function ops set for "
349                                 "specified algorithms combination\n");
350                 goto err;
351         }
352
353         if (!opts.silent)
354                 show_test_vector(t_vec);
355
356         i = 0;
357         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
358
359                 if (i == nb_cryptodevs)
360                         break;
361
362                 cdev_id = enabled_cdevs[i];
363
364                 ctx[cdev_id] = cperf_testmap[opts.test].constructor(cdev_id, 0,
365                                 &opts, t_vec, &op_fns);
366                 if (ctx[cdev_id] == NULL) {
367                         RTE_LOG(ERR, USER1, "Test run constructor failed\n");
368                         goto err;
369                 }
370                 i++;
371         }
372
373         i = 0;
374         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
375
376                 if (i == nb_cryptodevs)
377                         break;
378
379                 cdev_id = enabled_cdevs[i];
380
381                 rte_eal_remote_launch(cperf_testmap[opts.test].runner,
382                                 ctx[cdev_id], lcore_id);
383                 i++;
384         }
385
386         rte_eal_mp_wait_lcore();
387
388         i = 0;
389         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
390
391                 if (i == nb_cryptodevs)
392                         break;
393
394                 cdev_id = enabled_cdevs[i];
395
396                 cperf_testmap[opts.test].destructor(ctx[cdev_id]);
397                 i++;
398         }
399
400         free_test_vector(t_vec, &opts);
401
402         printf("\n");
403         return EXIT_SUCCESS;
404
405 err:
406         i = 0;
407         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
408                 if (i == nb_cryptodevs)
409                         break;
410
411                 cdev_id = enabled_cdevs[i];
412
413                 if (ctx[cdev_id] && cperf_testmap[opts.test].destructor)
414                         cperf_testmap[opts.test].destructor(ctx[cdev_id]);
415                 i++;
416         }
417
418         free_test_vector(t_vec, &opts);
419
420         printf("\n");
421         return EXIT_FAILURE;
422 }