app/crypto-perf: introduce performance test application
[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_CYCLECOUNT] = "cycle-count",
16         [CPERF_TEST_TYPE_LATENCY] = "latency"
17 };
18
19 const char *cperf_op_type_strs[] = {
20         [CPERF_CIPHER_ONLY] = "cipher-only",
21         [CPERF_AUTH_ONLY] = "auth-only",
22         [CPERF_CIPHER_THEN_AUTH] = "cipher-then-auth",
23         [CPERF_AUTH_THEN_CIPHER] = "auth-then-cipher",
24         [CPERF_AEAD] = "aead"
25 };
26
27 const struct cperf_test cperf_testmap[] = {
28                 [CPERF_TEST_TYPE_THROUGHPUT] = {
29                                 cperf_throughput_test_constructor,
30                                 cperf_throughput_test_runner,
31                                 cperf_throughput_test_destructor
32                 },
33                 [CPERF_TEST_TYPE_CYCLECOUNT] =  { NULL },
34                 [CPERF_TEST_TYPE_LATENCY] = {
35                                 cperf_latency_test_constructor,
36                                 cperf_latency_test_runner,
37                                 cperf_latency_test_destructor
38                 }
39 };
40
41 static int
42 cperf_initialize_cryptodev(struct cperf_options *opts, uint8_t *enabled_cdevs)
43 {
44         uint8_t cdev_id, enabled_cdev_count = 0, nb_lcores;
45         int ret;
46
47         enabled_cdev_count = rte_cryptodev_devices_get(opts->device_type,
48                         enabled_cdevs, RTE_DIM(enabled_cdevs));
49         if (enabled_cdev_count == 0) {
50                 printf("No crypto devices type %s available\n",
51                                 opts->device_type);
52                 return -EINVAL;
53         }
54
55         nb_lcores = rte_lcore_count() - 1;
56
57         if (enabled_cdev_count > nb_lcores) {
58                 printf("Number of capable crypto devices (%d) "
59                                 "has to be less or equal to number of slave "
60                                 "cores (%d)\n", enabled_cdev_count, nb_lcores);
61                 return -EINVAL;
62         }
63
64         for (cdev_id = 0; cdev_id < enabled_cdev_count &&
65                         cdev_id < RTE_CRYPTO_MAX_DEVS; cdev_id++) {
66
67                 struct rte_cryptodev_config conf = {
68                                 .nb_queue_pairs = 1,
69                                 .socket_id = SOCKET_ID_ANY,
70                                 .session_mp = {
71                                         .nb_objs = 2048,
72                                         .cache_size = 64
73                                 }
74                         };
75                 struct rte_cryptodev_qp_conf qp_conf = {
76                                 .nb_descriptors = 2048
77                 };
78
79                 ret = rte_cryptodev_configure(enabled_cdevs[cdev_id], &conf);
80                 if (ret < 0) {
81                         printf("Failed to configure cryptodev %u",
82                                         enabled_cdevs[cdev_id]);
83                         return -EINVAL;
84                 }
85
86                 ret = rte_cryptodev_queue_pair_setup(enabled_cdevs[cdev_id], 0,
87                                 &qp_conf, SOCKET_ID_ANY);
88                         if (ret < 0) {
89                                 printf("Failed to setup queue pair %u on "
90                                         "cryptodev %u", 0, cdev_id);
91                                 return -EINVAL;
92                         }
93
94                 ret = rte_cryptodev_start(enabled_cdevs[cdev_id]);
95                 if (ret < 0) {
96                         printf("Failed to start device %u: error %d\n",
97                                         enabled_cdevs[cdev_id], ret);
98                         return -EPERM;
99                 }
100         }
101
102         return enabled_cdev_count;
103 }
104
105 static int
106 cperf_verify_devices_capabilities(struct cperf_options *opts,
107                 uint8_t *enabled_cdevs, uint8_t nb_cryptodevs)
108 {
109         struct rte_cryptodev_sym_capability_idx cap_idx;
110         const struct rte_cryptodev_symmetric_capability *capability;
111
112         uint8_t i, cdev_id;
113         int ret;
114
115         for (i = 0; i < nb_cryptodevs; i++) {
116
117                 cdev_id = enabled_cdevs[i];
118
119                 if (opts->op_type == CPERF_AUTH_ONLY ||
120                                 opts->op_type == CPERF_CIPHER_THEN_AUTH ||
121                                 opts->op_type == CPERF_AUTH_THEN_CIPHER)  {
122
123                         cap_idx.type = RTE_CRYPTO_SYM_XFORM_AUTH;
124                         cap_idx.algo.auth = opts->auth_algo;
125
126                         capability = rte_cryptodev_sym_capability_get(cdev_id,
127                                         &cap_idx);
128                         if (capability == NULL)
129                                 return -1;
130
131                         ret = rte_cryptodev_sym_capability_check_auth(
132                                         capability,
133                                         opts->auth_key_sz,
134                                         opts->auth_digest_sz,
135                                         opts->auth_aad_sz);
136                         if (ret != 0)
137                                 return ret;
138                 }
139
140                 if (opts->op_type == CPERF_CIPHER_ONLY ||
141                                 opts->op_type == CPERF_CIPHER_THEN_AUTH ||
142                                 opts->op_type == CPERF_AUTH_THEN_CIPHER) {
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;
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                 goto err;
304         }
305
306         ret = cperf_verify_devices_capabilities(&opts, enabled_cdevs,
307                         nb_cryptodevs);
308         if (ret) {
309                 RTE_LOG(ERR, USER1, "Crypto device type does not support "
310                                 "capabilities requested\n");
311                 goto err;
312         }
313
314         if (opts.test_file != NULL) {
315                 t_vec = cperf_test_vector_get_from_file(&opts);
316                 if (t_vec == NULL) {
317                         RTE_LOG(ERR, USER1,
318                                         "Failed to create test vector for"
319                                         " specified file\n");
320                         goto err;
321                 }
322
323                 if (cperf_check_test_vector(opts, *t_vec)) {
324                         RTE_LOG(ERR, USER1, "Incomplete necessary test vectors"
325                                         "\n");
326                         goto err;
327                 }
328         } else {
329                 t_vec = cperf_test_vector_get_dummy(&opts);
330                 if (t_vec == NULL) {
331                         RTE_LOG(ERR, USER1,
332                                         "Failed to create test vector for"
333                                         " specified algorithms\n");
334                         goto err;
335                 }
336         }
337
338         ret = cperf_get_op_functions(&opts, &op_fns);
339         if (ret) {
340                 RTE_LOG(ERR, USER1, "Failed to find function ops set for "
341                                 "specified algorithms combination\n");
342                 goto err;
343         }
344
345         if (!opts.silent)
346                 show_test_vector(t_vec);
347
348         i = 0;
349         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
350
351                 if (i == nb_cryptodevs)
352                         break;
353
354                 cdev_id = enabled_cdevs[i];
355
356                 ctx[cdev_id] = cperf_testmap[opts.test].constructor(cdev_id, 0,
357                                 &opts, t_vec, &op_fns);
358                 if (ctx[cdev_id] == NULL) {
359                         RTE_LOG(ERR, USER1, "Test run constructor failed\n");
360                         goto err;
361                 }
362                 i++;
363         }
364
365         i = 0;
366         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
367
368                 if (i == nb_cryptodevs)
369                         break;
370
371                 cdev_id = enabled_cdevs[i];
372
373                 rte_eal_remote_launch(cperf_testmap[opts.test].runner,
374                                 ctx[cdev_id], lcore_id);
375                 i++;
376         }
377
378         rte_eal_mp_wait_lcore();
379
380         i = 0;
381         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
382
383                 if (i == nb_cryptodevs)
384                         break;
385
386                 cdev_id = enabled_cdevs[i];
387
388                 cperf_testmap[opts.test].destructor(ctx[cdev_id]);
389                 i++;
390         }
391
392         free_test_vector(t_vec, &opts);
393
394         printf("\n");
395         return EXIT_SUCCESS;
396
397 err:
398         i = 0;
399         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
400
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 }