cdd3cc83f0afc8186b31e0802b7f0a921ba3cadd
[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 #define NUM_SESSIONS 2048
47 #define SESS_MEMPOOL_CACHE_SIZE 64
48
49 const char *cperf_test_type_strs[] = {
50         [CPERF_TEST_TYPE_THROUGHPUT] = "throughput",
51         [CPERF_TEST_TYPE_LATENCY] = "latency",
52         [CPERF_TEST_TYPE_VERIFY] = "verify"
53 };
54
55 const char *cperf_op_type_strs[] = {
56         [CPERF_CIPHER_ONLY] = "cipher-only",
57         [CPERF_AUTH_ONLY] = "auth-only",
58         [CPERF_CIPHER_THEN_AUTH] = "cipher-then-auth",
59         [CPERF_AUTH_THEN_CIPHER] = "auth-then-cipher",
60         [CPERF_AEAD] = "aead"
61 };
62
63 const struct cperf_test cperf_testmap[] = {
64                 [CPERF_TEST_TYPE_THROUGHPUT] = {
65                                 cperf_throughput_test_constructor,
66                                 cperf_throughput_test_runner,
67                                 cperf_throughput_test_destructor
68                 },
69                 [CPERF_TEST_TYPE_LATENCY] = {
70                                 cperf_latency_test_constructor,
71                                 cperf_latency_test_runner,
72                                 cperf_latency_test_destructor
73                 },
74                 [CPERF_TEST_TYPE_VERIFY] = {
75                                 cperf_verify_test_constructor,
76                                 cperf_verify_test_runner,
77                                 cperf_verify_test_destructor
78                 }
79 };
80
81 static int
82 cperf_initialize_cryptodev(struct cperf_options *opts, uint8_t *enabled_cdevs,
83                         struct rte_mempool *session_pool_socket[])
84 {
85         uint8_t enabled_cdev_count = 0, nb_lcores, cdev_id;
86         unsigned int i;
87         int ret;
88
89         enabled_cdev_count = rte_cryptodev_devices_get(opts->device_type,
90                         enabled_cdevs, RTE_CRYPTO_MAX_DEVS);
91         if (enabled_cdev_count == 0) {
92                 printf("No crypto devices type %s available\n",
93                                 opts->device_type);
94                 return -EINVAL;
95         }
96
97         nb_lcores = rte_lcore_count() - 1;
98
99         if (enabled_cdev_count > nb_lcores) {
100                 printf("Number of capable crypto devices (%d) "
101                                 "has to be less or equal to number of slave "
102                                 "cores (%d)\n", enabled_cdev_count, nb_lcores);
103                 return -EINVAL;
104         }
105
106         /* Create a mempool shared by all the devices */
107         uint32_t max_sess_size = 0, sess_size;
108
109         for (cdev_id = 0; cdev_id < rte_cryptodev_count(); cdev_id++) {
110                 sess_size = sizeof(struct rte_cryptodev_sym_session) +
111                         rte_cryptodev_get_private_session_size(cdev_id);
112                 if (sess_size > max_sess_size)
113                         max_sess_size = sess_size;
114         }
115
116
117         for (i = 0; i < enabled_cdev_count &&
118                         i < RTE_CRYPTO_MAX_DEVS; i++) {
119                 cdev_id = enabled_cdevs[i];
120                 uint8_t socket_id = rte_cryptodev_socket_id(cdev_id);
121
122                 struct rte_cryptodev_config conf = {
123                                 .nb_queue_pairs = 1,
124                                 .socket_id = socket_id
125                 };
126
127                 struct rte_cryptodev_qp_conf qp_conf = {
128                                 .nb_descriptors = 2048
129                 };
130
131
132                 if (session_pool_socket[socket_id] == NULL) {
133                         char mp_name[RTE_MEMPOOL_NAMESIZE];
134                         struct rte_mempool *sess_mp;
135
136                         snprintf(mp_name, RTE_MEMPOOL_NAMESIZE,
137                                 "sess_mp_%u", socket_id);
138
139                         sess_mp = rte_mempool_create(mp_name,
140                                                 NUM_SESSIONS,
141                                                 max_sess_size,
142                                                 SESS_MEMPOOL_CACHE_SIZE,
143                                                 0, NULL, NULL, NULL,
144                                                 NULL, socket_id,
145                                                 0);
146
147                         if (sess_mp == NULL) {
148                                 printf("Cannot create session pool on socket %d\n",
149                                         socket_id);
150                                 return -ENOMEM;
151                         }
152
153                         printf("Allocated session pool on socket %d\n", socket_id);
154                         session_pool_socket[socket_id] = sess_mp;
155                 }
156
157                 ret = rte_cryptodev_configure(cdev_id, &conf,
158                                 session_pool_socket[socket_id]);
159                 if (ret < 0) {
160                         printf("Failed to configure cryptodev %u", cdev_id);
161                         return -EINVAL;
162                 }
163
164                 ret = rte_cryptodev_queue_pair_setup(cdev_id, 0, &qp_conf,
165                                 socket_id);
166                 if (ret < 0) {
167                         printf("Failed to setup queue pair %u on "
168                                 "cryptodev %u", 0, cdev_id);
169                         return -EINVAL;
170                 }
171
172                 ret = rte_cryptodev_start(cdev_id);
173                 if (ret < 0) {
174                         printf("Failed to start device %u: error %d\n",
175                                         cdev_id, ret);
176                         return -EPERM;
177                 }
178         }
179
180         return enabled_cdev_count;
181 }
182
183 static int
184 cperf_verify_devices_capabilities(struct cperf_options *opts,
185                 uint8_t *enabled_cdevs, uint8_t nb_cryptodevs)
186 {
187         struct rte_cryptodev_sym_capability_idx cap_idx;
188         const struct rte_cryptodev_symmetric_capability *capability;
189
190         uint8_t i, cdev_id;
191         int ret;
192
193         for (i = 0; i < nb_cryptodevs; i++) {
194
195                 cdev_id = enabled_cdevs[i];
196
197                 if (opts->op_type == CPERF_AUTH_ONLY ||
198                                 opts->op_type == CPERF_CIPHER_THEN_AUTH ||
199                                 opts->op_type == CPERF_AUTH_THEN_CIPHER) {
200
201                         cap_idx.type = RTE_CRYPTO_SYM_XFORM_AUTH;
202                         cap_idx.algo.auth = opts->auth_algo;
203
204                         capability = rte_cryptodev_sym_capability_get(cdev_id,
205                                         &cap_idx);
206                         if (capability == NULL)
207                                 return -1;
208
209                         ret = rte_cryptodev_sym_capability_check_auth(
210                                         capability,
211                                         opts->auth_key_sz,
212                                         opts->digest_sz,
213                                         0,
214                                         opts->auth_iv_sz);
215                         if (ret != 0)
216                                 return ret;
217                 }
218
219                 if (opts->op_type == CPERF_CIPHER_ONLY ||
220                                 opts->op_type == CPERF_CIPHER_THEN_AUTH ||
221                                 opts->op_type == CPERF_AUTH_THEN_CIPHER) {
222
223                         cap_idx.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
224                         cap_idx.algo.cipher = opts->cipher_algo;
225
226                         capability = rte_cryptodev_sym_capability_get(cdev_id,
227                                         &cap_idx);
228                         if (capability == NULL)
229                                 return -1;
230
231                         ret = rte_cryptodev_sym_capability_check_cipher(
232                                         capability,
233                                         opts->cipher_key_sz,
234                                         opts->cipher_iv_sz);
235                         if (ret != 0)
236                                 return ret;
237                 }
238
239                 if (opts->op_type == CPERF_AEAD) {
240
241                         cap_idx.type = RTE_CRYPTO_SYM_XFORM_AEAD;
242                         cap_idx.algo.aead = opts->aead_algo;
243
244                         capability = rte_cryptodev_sym_capability_get(cdev_id,
245                                         &cap_idx);
246                         if (capability == NULL)
247                                 return -1;
248
249                         ret = rte_cryptodev_sym_capability_check_aead(
250                                         capability,
251                                         opts->aead_key_sz,
252                                         opts->digest_sz,
253                                         opts->aead_aad_sz,
254                                         opts->aead_iv_sz);
255                         if (ret != 0)
256                                 return ret;
257                 }
258         }
259
260         return 0;
261 }
262
263 static int
264 cperf_check_test_vector(struct cperf_options *opts,
265                 struct cperf_test_vector *test_vec)
266 {
267         if (opts->op_type == CPERF_CIPHER_ONLY) {
268                 if (opts->cipher_algo == RTE_CRYPTO_CIPHER_NULL) {
269                         if (test_vec->plaintext.data == NULL)
270                                 return -1;
271                 } else if (opts->cipher_algo != RTE_CRYPTO_CIPHER_NULL) {
272                         if (test_vec->plaintext.data == NULL)
273                                 return -1;
274                         if (test_vec->plaintext.length < opts->max_buffer_size)
275                                 return -1;
276                         if (test_vec->ciphertext.data == NULL)
277                                 return -1;
278                         if (test_vec->ciphertext.length < opts->max_buffer_size)
279                                 return -1;
280                         if (test_vec->cipher_iv.data == NULL)
281                                 return -1;
282                         if (test_vec->cipher_iv.length != opts->cipher_iv_sz)
283                                 return -1;
284                         if (test_vec->cipher_key.data == NULL)
285                                 return -1;
286                         if (test_vec->cipher_key.length != opts->cipher_key_sz)
287                                 return -1;
288                 }
289         } else if (opts->op_type == CPERF_AUTH_ONLY) {
290                 if (opts->auth_algo != RTE_CRYPTO_AUTH_NULL) {
291                         if (test_vec->plaintext.data == NULL)
292                                 return -1;
293                         if (test_vec->plaintext.length < opts->max_buffer_size)
294                                 return -1;
295                         if (test_vec->auth_key.data == NULL)
296                                 return -1;
297                         if (test_vec->auth_key.length != opts->auth_key_sz)
298                                 return -1;
299                         if (test_vec->auth_iv.length != opts->auth_iv_sz)
300                                 return -1;
301                         /* Auth IV is only required for some algorithms */
302                         if (opts->auth_iv_sz && test_vec->auth_iv.data == NULL)
303                                 return -1;
304                         if (test_vec->digest.data == NULL)
305                                 return -1;
306                         if (test_vec->digest.length < opts->digest_sz)
307                                 return -1;
308                 }
309
310         } else if (opts->op_type == CPERF_CIPHER_THEN_AUTH ||
311                         opts->op_type == CPERF_AUTH_THEN_CIPHER) {
312                 if (opts->cipher_algo == RTE_CRYPTO_CIPHER_NULL) {
313                         if (test_vec->plaintext.data == NULL)
314                                 return -1;
315                         if (test_vec->plaintext.length < opts->max_buffer_size)
316                                 return -1;
317                 } else if (opts->cipher_algo != RTE_CRYPTO_CIPHER_NULL) {
318                         if (test_vec->plaintext.data == NULL)
319                                 return -1;
320                         if (test_vec->plaintext.length < opts->max_buffer_size)
321                                 return -1;
322                         if (test_vec->ciphertext.data == NULL)
323                                 return -1;
324                         if (test_vec->ciphertext.length < opts->max_buffer_size)
325                                 return -1;
326                         if (test_vec->cipher_iv.data == NULL)
327                                 return -1;
328                         if (test_vec->cipher_iv.length != opts->cipher_iv_sz)
329                                 return -1;
330                         if (test_vec->cipher_key.data == NULL)
331                                 return -1;
332                         if (test_vec->cipher_key.length != opts->cipher_key_sz)
333                                 return -1;
334                 }
335                 if (opts->auth_algo != RTE_CRYPTO_AUTH_NULL) {
336                         if (test_vec->auth_key.data == NULL)
337                                 return -1;
338                         if (test_vec->auth_key.length != opts->auth_key_sz)
339                                 return -1;
340                         if (test_vec->auth_iv.length != opts->auth_iv_sz)
341                                 return -1;
342                         /* Auth IV is only required for some algorithms */
343                         if (opts->auth_iv_sz && test_vec->auth_iv.data == NULL)
344                                 return -1;
345                         if (test_vec->digest.data == NULL)
346                                 return -1;
347                         if (test_vec->digest.length < opts->digest_sz)
348                                 return -1;
349                 }
350         } else if (opts->op_type == CPERF_AEAD) {
351                 if (test_vec->plaintext.data == NULL)
352                         return -1;
353                 if (test_vec->plaintext.length < opts->max_buffer_size)
354                         return -1;
355                 if (test_vec->ciphertext.data == NULL)
356                         return -1;
357                 if (test_vec->ciphertext.length < opts->max_buffer_size)
358                         return -1;
359                 if (test_vec->aead_iv.data == NULL)
360                         return -1;
361                 if (test_vec->aead_iv.length != opts->aead_iv_sz)
362                         return -1;
363                 if (test_vec->aad.data == NULL)
364                         return -1;
365                 if (test_vec->aad.length != opts->aead_aad_sz)
366                         return -1;
367                 if (test_vec->digest.data == NULL)
368                         return -1;
369                 if (test_vec->digest.length < opts->digest_sz)
370                         return -1;
371         }
372         return 0;
373 }
374
375 int
376 main(int argc, char **argv)
377 {
378         struct cperf_options opts = {0};
379         struct cperf_test_vector *t_vec = NULL;
380         struct cperf_op_fns op_fns;
381
382         void *ctx[RTE_MAX_LCORE] = { };
383         struct rte_mempool *session_pool_socket[RTE_MAX_NUMA_NODES] = { 0 };
384
385         int nb_cryptodevs = 0;
386         uint8_t cdev_id, i;
387         uint8_t enabled_cdevs[RTE_CRYPTO_MAX_DEVS] = { 0 };
388
389         uint8_t buffer_size_idx = 0;
390
391         int ret;
392         uint32_t lcore_id;
393
394         /* Initialise DPDK EAL */
395         ret = rte_eal_init(argc, argv);
396         if (ret < 0)
397                 rte_exit(EXIT_FAILURE, "Invalid EAL arguments!\n");
398         argc -= ret;
399         argv += ret;
400
401         cperf_options_default(&opts);
402
403         ret = cperf_options_parse(&opts, argc, argv);
404         if (ret) {
405                 RTE_LOG(ERR, USER1, "Parsing on or more user options failed\n");
406                 goto err;
407         }
408
409         ret = cperf_options_check(&opts);
410         if (ret) {
411                 RTE_LOG(ERR, USER1,
412                                 "Checking on or more user options failed\n");
413                 goto err;
414         }
415
416         if (!opts.silent)
417                 cperf_options_dump(&opts);
418
419         nb_cryptodevs = cperf_initialize_cryptodev(&opts, enabled_cdevs,
420                         session_pool_socket);
421         if (nb_cryptodevs < 1) {
422                 RTE_LOG(ERR, USER1, "Failed to initialise requested crypto "
423                                 "device type\n");
424                 nb_cryptodevs = 0;
425                 goto err;
426         }
427
428         ret = cperf_verify_devices_capabilities(&opts, enabled_cdevs,
429                         nb_cryptodevs);
430         if (ret) {
431                 RTE_LOG(ERR, USER1, "Crypto device type does not support "
432                                 "capabilities requested\n");
433                 goto err;
434         }
435
436         if (opts.test_file != NULL) {
437                 t_vec = cperf_test_vector_get_from_file(&opts);
438                 if (t_vec == NULL) {
439                         RTE_LOG(ERR, USER1,
440                                         "Failed to create test vector for"
441                                         " specified file\n");
442                         goto err;
443                 }
444
445                 if (cperf_check_test_vector(&opts, t_vec)) {
446                         RTE_LOG(ERR, USER1, "Incomplete necessary test vectors"
447                                         "\n");
448                         goto err;
449                 }
450         } else {
451                 t_vec = cperf_test_vector_get_dummy(&opts);
452                 if (t_vec == NULL) {
453                         RTE_LOG(ERR, USER1,
454                                         "Failed to create test vector for"
455                                         " specified algorithms\n");
456                         goto err;
457                 }
458         }
459
460         ret = cperf_get_op_functions(&opts, &op_fns);
461         if (ret) {
462                 RTE_LOG(ERR, USER1, "Failed to find function ops set for "
463                                 "specified algorithms combination\n");
464                 goto err;
465         }
466
467         if (!opts.silent)
468                 show_test_vector(t_vec);
469
470         i = 0;
471         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
472
473                 if (i == nb_cryptodevs)
474                         break;
475
476                 cdev_id = enabled_cdevs[i];
477
478                 ctx[cdev_id] = cperf_testmap[opts.test].constructor(cdev_id, 0,
479                                 &opts, t_vec, &op_fns);
480                 if (ctx[cdev_id] == NULL) {
481                         RTE_LOG(ERR, USER1, "Test run constructor failed\n");
482                         goto err;
483                 }
484                 i++;
485         }
486
487         /* Get first size from range or list */
488         if (opts.inc_buffer_size != 0)
489                 opts.test_buffer_size = opts.min_buffer_size;
490         else
491                 opts.test_buffer_size = opts.buffer_size_list[0];
492
493         while (opts.test_buffer_size <= opts.max_buffer_size) {
494                 i = 0;
495                 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
496
497                         if (i == nb_cryptodevs)
498                                 break;
499
500                         cdev_id = enabled_cdevs[i];
501
502                         rte_eal_remote_launch(cperf_testmap[opts.test].runner,
503                                 ctx[cdev_id], lcore_id);
504                         i++;
505                 }
506                 rte_eal_mp_wait_lcore();
507
508                 /* Get next size from range or list */
509                 if (opts.inc_buffer_size != 0)
510                         opts.test_buffer_size += opts.inc_buffer_size;
511                 else {
512                         if (++buffer_size_idx == opts.buffer_size_count)
513                                 break;
514                         opts.test_buffer_size = opts.buffer_size_list[buffer_size_idx];
515                 }
516         }
517
518         i = 0;
519         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
520
521                 if (i == nb_cryptodevs)
522                         break;
523
524                 cdev_id = enabled_cdevs[i];
525
526                 cperf_testmap[opts.test].destructor(ctx[cdev_id]);
527                 i++;
528         }
529
530         free_test_vector(t_vec, &opts);
531
532         printf("\n");
533         return EXIT_SUCCESS;
534
535 err:
536         i = 0;
537         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
538                 if (i == nb_cryptodevs)
539                         break;
540
541                 cdev_id = enabled_cdevs[i];
542
543                 if (ctx[cdev_id] && cperf_testmap[opts.test].destructor)
544                         cperf_testmap[opts.test].destructor(ctx[cdev_id]);
545                 i++;
546         }
547
548         free_test_vector(t_vec, &opts);
549
550         printf("\n");
551         return EXIT_FAILURE;
552 }