cryptodev: add auth IV
[dpdk.git] / app / test-crypto-perf / main.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2016-2017 Intel Corporation. All rights reserved.
5  *
6  *   Redistribution and use in source and binary forms, with or without
7  *   modification, are permitted provided that the following conditions
8  *   are met:
9  *
10  *     * Redistributions of source code must retain the above copyright
11  *       notice, this list of conditions and the following disclaimer.
12  *     * Redistributions in binary form must reproduce the above copyright
13  *       notice, this list of conditions and the following disclaimer in
14  *       the documentation and/or other materials provided with the
15  *       distribution.
16  *     * Neither the name of Intel Corporation nor the names of its
17  *       contributors may be used to endorse or promote products derived
18  *       from this software without specific prior written permission.
19  *
20  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include <stdio.h>
34 #include <unistd.h>
35
36 #include <rte_eal.h>
37 #include <rte_cryptodev.h>
38
39 #include "cperf.h"
40 #include "cperf_options.h"
41 #include "cperf_test_vector_parsing.h"
42 #include "cperf_test_throughput.h"
43 #include "cperf_test_latency.h"
44 #include "cperf_test_verify.h"
45
46 const char *cperf_test_type_strs[] = {
47         [CPERF_TEST_TYPE_THROUGHPUT] = "throughput",
48         [CPERF_TEST_TYPE_LATENCY] = "latency",
49         [CPERF_TEST_TYPE_VERIFY] = "verify"
50 };
51
52 const char *cperf_op_type_strs[] = {
53         [CPERF_CIPHER_ONLY] = "cipher-only",
54         [CPERF_AUTH_ONLY] = "auth-only",
55         [CPERF_CIPHER_THEN_AUTH] = "cipher-then-auth",
56         [CPERF_AUTH_THEN_CIPHER] = "auth-then-cipher",
57         [CPERF_AEAD] = "aead"
58 };
59
60 const struct cperf_test cperf_testmap[] = {
61                 [CPERF_TEST_TYPE_THROUGHPUT] = {
62                                 cperf_throughput_test_constructor,
63                                 cperf_throughput_test_runner,
64                                 cperf_throughput_test_destructor
65                 },
66                 [CPERF_TEST_TYPE_LATENCY] = {
67                                 cperf_latency_test_constructor,
68                                 cperf_latency_test_runner,
69                                 cperf_latency_test_destructor
70                 },
71                 [CPERF_TEST_TYPE_VERIFY] = {
72                                 cperf_verify_test_constructor,
73                                 cperf_verify_test_runner,
74                                 cperf_verify_test_destructor
75                 }
76 };
77
78 static int
79 cperf_initialize_cryptodev(struct cperf_options *opts, uint8_t *enabled_cdevs)
80 {
81         uint8_t cdev_id, enabled_cdev_count = 0, nb_lcores;
82         int ret;
83
84         enabled_cdev_count = rte_cryptodev_devices_get(opts->device_type,
85                         enabled_cdevs, RTE_CRYPTO_MAX_DEVS);
86         if (enabled_cdev_count == 0) {
87                 printf("No crypto devices type %s available\n",
88                                 opts->device_type);
89                 return -EINVAL;
90         }
91
92         nb_lcores = rte_lcore_count() - 1;
93
94         if (enabled_cdev_count > nb_lcores) {
95                 printf("Number of capable crypto devices (%d) "
96                                 "has to be less or equal to number of slave "
97                                 "cores (%d)\n", enabled_cdev_count, nb_lcores);
98                 return -EINVAL;
99         }
100
101         for (cdev_id = 0; cdev_id < enabled_cdev_count &&
102                         cdev_id < RTE_CRYPTO_MAX_DEVS; cdev_id++) {
103
104                 struct rte_cryptodev_config conf = {
105                                 .nb_queue_pairs = 1,
106                                 .socket_id = SOCKET_ID_ANY,
107                                 .session_mp = {
108                                         .nb_objs = 2048,
109                                         .cache_size = 64
110                                 }
111                         };
112                 struct rte_cryptodev_qp_conf qp_conf = {
113                                 .nb_descriptors = 2048
114                 };
115
116                 ret = rte_cryptodev_configure(enabled_cdevs[cdev_id], &conf);
117                 if (ret < 0) {
118                         printf("Failed to configure cryptodev %u",
119                                         enabled_cdevs[cdev_id]);
120                         return -EINVAL;
121                 }
122
123                 ret = rte_cryptodev_queue_pair_setup(enabled_cdevs[cdev_id], 0,
124                                 &qp_conf, SOCKET_ID_ANY);
125                         if (ret < 0) {
126                                 printf("Failed to setup queue pair %u on "
127                                         "cryptodev %u", 0, cdev_id);
128                                 return -EINVAL;
129                         }
130
131                 ret = rte_cryptodev_start(enabled_cdevs[cdev_id]);
132                 if (ret < 0) {
133                         printf("Failed to start device %u: error %d\n",
134                                         enabled_cdevs[cdev_id], ret);
135                         return -EPERM;
136                 }
137         }
138
139         return enabled_cdev_count;
140 }
141
142 static int
143 cperf_verify_devices_capabilities(struct cperf_options *opts,
144                 uint8_t *enabled_cdevs, uint8_t nb_cryptodevs)
145 {
146         struct rte_cryptodev_sym_capability_idx cap_idx;
147         const struct rte_cryptodev_symmetric_capability *capability;
148
149         uint8_t i, cdev_id;
150         int ret;
151
152         for (i = 0; i < nb_cryptodevs; i++) {
153
154                 cdev_id = enabled_cdevs[i];
155
156                 if (opts->op_type == CPERF_AUTH_ONLY ||
157                                 opts->op_type == CPERF_CIPHER_THEN_AUTH ||
158                                 opts->op_type == CPERF_AUTH_THEN_CIPHER ||
159                                 opts->op_type == CPERF_AEAD)  {
160
161                         cap_idx.type = RTE_CRYPTO_SYM_XFORM_AUTH;
162                         cap_idx.algo.auth = opts->auth_algo;
163
164                         capability = rte_cryptodev_sym_capability_get(cdev_id,
165                                         &cap_idx);
166                         if (capability == NULL)
167                                 return -1;
168
169                         ret = rte_cryptodev_sym_capability_check_auth(
170                                         capability,
171                                         opts->auth_key_sz,
172                                         opts->auth_digest_sz,
173                                         opts->auth_aad_sz,
174                                         opts->auth_iv_sz);
175                         if (ret != 0)
176                                 return ret;
177                 }
178
179                 if (opts->op_type == CPERF_CIPHER_ONLY ||
180                                 opts->op_type == CPERF_CIPHER_THEN_AUTH ||
181                                 opts->op_type == CPERF_AUTH_THEN_CIPHER ||
182                                 opts->op_type == CPERF_AEAD) {
183
184                         cap_idx.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
185                         cap_idx.algo.cipher = opts->cipher_algo;
186
187                         capability = rte_cryptodev_sym_capability_get(cdev_id,
188                                         &cap_idx);
189                         if (capability == NULL)
190                                 return -1;
191
192                         ret = rte_cryptodev_sym_capability_check_cipher(
193                                         capability,
194                                         opts->cipher_key_sz,
195                                         opts->cipher_iv_sz);
196                         if (ret != 0)
197                                 return ret;
198                 }
199         }
200
201         return 0;
202 }
203
204 static int
205 cperf_check_test_vector(struct cperf_options *opts,
206                 struct cperf_test_vector *test_vec)
207 {
208         if (opts->op_type == CPERF_CIPHER_ONLY) {
209                 if (opts->cipher_algo == RTE_CRYPTO_CIPHER_NULL) {
210                         if (test_vec->plaintext.data == NULL)
211                                 return -1;
212                 } else if (opts->cipher_algo != RTE_CRYPTO_CIPHER_NULL) {
213                         if (test_vec->plaintext.data == NULL)
214                                 return -1;
215                         if (test_vec->plaintext.length < opts->max_buffer_size)
216                                 return -1;
217                         if (test_vec->ciphertext.data == NULL)
218                                 return -1;
219                         if (test_vec->ciphertext.length < opts->max_buffer_size)
220                                 return -1;
221                         if (test_vec->cipher_iv.data == NULL)
222                                 return -1;
223                         if (test_vec->cipher_iv.length != opts->cipher_iv_sz)
224                                 return -1;
225                         if (test_vec->cipher_key.data == NULL)
226                                 return -1;
227                         if (test_vec->cipher_key.length != opts->cipher_key_sz)
228                                 return -1;
229                 }
230         } else if (opts->op_type == CPERF_AUTH_ONLY) {
231                 if (opts->auth_algo != RTE_CRYPTO_AUTH_NULL) {
232                         if (test_vec->plaintext.data == NULL)
233                                 return -1;
234                         if (test_vec->plaintext.length < opts->max_buffer_size)
235                                 return -1;
236                         if (test_vec->auth_key.data == NULL)
237                                 return -1;
238                         if (test_vec->auth_key.length != opts->auth_key_sz)
239                                 return -1;
240                         if (test_vec->auth_iv.length != opts->auth_iv_sz)
241                                 return -1;
242                         /* Auth IV is only required for some algorithms */
243                         if (opts->auth_iv_sz && test_vec->auth_iv.data == NULL)
244                                 return -1;
245                         if (test_vec->digest.data == NULL)
246                                 return -1;
247                         if (test_vec->digest.length < opts->auth_digest_sz)
248                                 return -1;
249                 }
250
251         } else if (opts->op_type == CPERF_CIPHER_THEN_AUTH ||
252                         opts->op_type == CPERF_AUTH_THEN_CIPHER) {
253                 if (opts->cipher_algo == RTE_CRYPTO_CIPHER_NULL) {
254                         if (test_vec->plaintext.data == NULL)
255                                 return -1;
256                         if (test_vec->plaintext.length < opts->max_buffer_size)
257                                 return -1;
258                 } else if (opts->cipher_algo != RTE_CRYPTO_CIPHER_NULL) {
259                         if (test_vec->plaintext.data == NULL)
260                                 return -1;
261                         if (test_vec->plaintext.length < opts->max_buffer_size)
262                                 return -1;
263                         if (test_vec->ciphertext.data == NULL)
264                                 return -1;
265                         if (test_vec->ciphertext.length < opts->max_buffer_size)
266                                 return -1;
267                         if (test_vec->cipher_iv.data == NULL)
268                                 return -1;
269                         if (test_vec->cipher_iv.length != opts->cipher_iv_sz)
270                                 return -1;
271                         if (test_vec->cipher_key.data == NULL)
272                                 return -1;
273                         if (test_vec->cipher_key.length != opts->cipher_key_sz)
274                                 return -1;
275                 }
276                 if (opts->auth_algo != RTE_CRYPTO_AUTH_NULL) {
277                         if (test_vec->auth_key.data == NULL)
278                                 return -1;
279                         if (test_vec->auth_key.length != opts->auth_key_sz)
280                                 return -1;
281                         if (test_vec->auth_iv.length != opts->auth_iv_sz)
282                                 return -1;
283                         /* Auth IV is only required for some algorithms */
284                         if (opts->auth_iv_sz && test_vec->auth_iv.data == NULL)
285                                 return -1;
286                         if (test_vec->digest.data == NULL)
287                                 return -1;
288                         if (test_vec->digest.length < opts->auth_digest_sz)
289                                 return -1;
290                 }
291         } else if (opts->op_type == CPERF_AEAD) {
292                 if (test_vec->plaintext.data == NULL)
293                         return -1;
294                 if (test_vec->plaintext.length < opts->max_buffer_size)
295                         return -1;
296                 if (test_vec->ciphertext.data == NULL)
297                         return -1;
298                 if (test_vec->ciphertext.length < opts->max_buffer_size)
299                         return -1;
300                 if (test_vec->cipher_iv.data == NULL)
301                         return -1;
302                 if (test_vec->cipher_iv.length != opts->cipher_iv_sz)
303                         return -1;
304                 if (test_vec->aad.data == NULL)
305                         return -1;
306                 if (test_vec->aad.length != opts->auth_aad_sz)
307                         return -1;
308                 if (test_vec->digest.data == NULL)
309                         return -1;
310                 if (test_vec->digest.length < opts->auth_digest_sz)
311                         return -1;
312         }
313         return 0;
314 }
315
316 int
317 main(int argc, char **argv)
318 {
319         struct cperf_options opts = {0};
320         struct cperf_test_vector *t_vec = NULL;
321         struct cperf_op_fns op_fns;
322
323         void *ctx[RTE_MAX_LCORE] = { };
324
325         int nb_cryptodevs = 0;
326         uint8_t cdev_id, i;
327         uint8_t enabled_cdevs[RTE_CRYPTO_MAX_DEVS] = { 0 };
328
329         uint8_t buffer_size_idx = 0;
330
331         int ret;
332         uint32_t lcore_id;
333
334         /* Initialise DPDK EAL */
335         ret = rte_eal_init(argc, argv);
336         if (ret < 0)
337                 rte_exit(EXIT_FAILURE, "Invalid EAL arguments!\n");
338         argc -= ret;
339         argv += ret;
340
341         cperf_options_default(&opts);
342
343         ret = cperf_options_parse(&opts, argc, argv);
344         if (ret) {
345                 RTE_LOG(ERR, USER1, "Parsing on or more user options failed\n");
346                 goto err;
347         }
348
349         ret = cperf_options_check(&opts);
350         if (ret) {
351                 RTE_LOG(ERR, USER1,
352                                 "Checking on or more user options failed\n");
353                 goto err;
354         }
355
356         if (!opts.silent)
357                 cperf_options_dump(&opts);
358
359         nb_cryptodevs = cperf_initialize_cryptodev(&opts, enabled_cdevs);
360         if (nb_cryptodevs < 1) {
361                 RTE_LOG(ERR, USER1, "Failed to initialise requested crypto "
362                                 "device type\n");
363                 nb_cryptodevs = 0;
364                 goto err;
365         }
366
367         ret = cperf_verify_devices_capabilities(&opts, enabled_cdevs,
368                         nb_cryptodevs);
369         if (ret) {
370                 RTE_LOG(ERR, USER1, "Crypto device type does not support "
371                                 "capabilities requested\n");
372                 goto err;
373         }
374
375         if (opts.test_file != NULL) {
376                 t_vec = cperf_test_vector_get_from_file(&opts);
377                 if (t_vec == NULL) {
378                         RTE_LOG(ERR, USER1,
379                                         "Failed to create test vector for"
380                                         " specified file\n");
381                         goto err;
382                 }
383
384                 if (cperf_check_test_vector(&opts, t_vec)) {
385                         RTE_LOG(ERR, USER1, "Incomplete necessary test vectors"
386                                         "\n");
387                         goto err;
388                 }
389         } else {
390                 t_vec = cperf_test_vector_get_dummy(&opts);
391                 if (t_vec == NULL) {
392                         RTE_LOG(ERR, USER1,
393                                         "Failed to create test vector for"
394                                         " specified algorithms\n");
395                         goto err;
396                 }
397         }
398
399         ret = cperf_get_op_functions(&opts, &op_fns);
400         if (ret) {
401                 RTE_LOG(ERR, USER1, "Failed to find function ops set for "
402                                 "specified algorithms combination\n");
403                 goto err;
404         }
405
406         if (!opts.silent)
407                 show_test_vector(t_vec);
408
409         i = 0;
410         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
411
412                 if (i == nb_cryptodevs)
413                         break;
414
415                 cdev_id = enabled_cdevs[i];
416
417                 ctx[cdev_id] = cperf_testmap[opts.test].constructor(cdev_id, 0,
418                                 &opts, t_vec, &op_fns);
419                 if (ctx[cdev_id] == NULL) {
420                         RTE_LOG(ERR, USER1, "Test run constructor failed\n");
421                         goto err;
422                 }
423                 i++;
424         }
425
426         /* Get first size from range or list */
427         if (opts.inc_buffer_size != 0)
428                 opts.test_buffer_size = opts.min_buffer_size;
429         else
430                 opts.test_buffer_size = opts.buffer_size_list[0];
431
432         while (opts.test_buffer_size <= opts.max_buffer_size) {
433                 i = 0;
434                 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
435
436                         if (i == nb_cryptodevs)
437                                 break;
438
439                         cdev_id = enabled_cdevs[i];
440
441                         rte_eal_remote_launch(cperf_testmap[opts.test].runner,
442                                 ctx[cdev_id], lcore_id);
443                         i++;
444                 }
445                 rte_eal_mp_wait_lcore();
446
447                 /* Get next size from range or list */
448                 if (opts.inc_buffer_size != 0)
449                         opts.test_buffer_size += opts.inc_buffer_size;
450                 else {
451                         if (++buffer_size_idx == opts.buffer_size_count)
452                                 break;
453                         opts.test_buffer_size = opts.buffer_size_list[buffer_size_idx];
454                 }
455         }
456
457         i = 0;
458         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
459
460                 if (i == nb_cryptodevs)
461                         break;
462
463                 cdev_id = enabled_cdevs[i];
464
465                 cperf_testmap[opts.test].destructor(ctx[cdev_id]);
466                 i++;
467         }
468
469         free_test_vector(t_vec, &opts);
470
471         printf("\n");
472         return EXIT_SUCCESS;
473
474 err:
475         i = 0;
476         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
477                 if (i == nb_cryptodevs)
478                         break;
479
480                 cdev_id = enabled_cdevs[i];
481
482                 if (ctx[cdev_id] && cperf_testmap[opts.test].destructor)
483                         cperf_testmap[opts.test].destructor(ctx[cdev_id]);
484                 i++;
485         }
486
487         free_test_vector(t_vec, &opts);
488
489         printf("\n");
490         return EXIT_FAILURE;
491 }