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