app/test-crypto-perf: add AEAD parameters
[dpdk.git] / app / test-crypto-perf / cperf_test_verify.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 <rte_malloc.h>
34 #include <rte_cycles.h>
35 #include <rte_crypto.h>
36 #include <rte_cryptodev.h>
37
38 #include "cperf_test_verify.h"
39 #include "cperf_ops.h"
40
41 struct cperf_verify_ctx {
42         uint8_t dev_id;
43         uint16_t qp_id;
44         uint8_t lcore_id;
45
46         struct rte_mempool *pkt_mbuf_pool_in;
47         struct rte_mempool *pkt_mbuf_pool_out;
48         struct rte_mbuf **mbufs_in;
49         struct rte_mbuf **mbufs_out;
50
51         struct rte_mempool *crypto_op_pool;
52
53         struct rte_cryptodev_sym_session *sess;
54
55         cperf_populate_ops_t populate_ops;
56
57         const struct cperf_options *options;
58         const struct cperf_test_vector *test_vector;
59 };
60
61 struct cperf_op_result {
62         enum rte_crypto_op_status status;
63 };
64
65 static void
66 cperf_verify_test_free(struct cperf_verify_ctx *ctx, uint32_t mbuf_nb)
67 {
68         uint32_t i;
69
70         if (ctx) {
71                 if (ctx->sess)
72                         rte_cryptodev_sym_session_free(ctx->dev_id, ctx->sess);
73
74                 if (ctx->mbufs_in) {
75                         for (i = 0; i < mbuf_nb; i++)
76                                 rte_pktmbuf_free(ctx->mbufs_in[i]);
77
78                         rte_free(ctx->mbufs_in);
79                 }
80
81                 if (ctx->mbufs_out) {
82                         for (i = 0; i < mbuf_nb; i++) {
83                                 if (ctx->mbufs_out[i] != NULL)
84                                         rte_pktmbuf_free(ctx->mbufs_out[i]);
85                         }
86
87                         rte_free(ctx->mbufs_out);
88                 }
89
90                 if (ctx->pkt_mbuf_pool_in)
91                         rte_mempool_free(ctx->pkt_mbuf_pool_in);
92
93                 if (ctx->pkt_mbuf_pool_out)
94                         rte_mempool_free(ctx->pkt_mbuf_pool_out);
95
96                 if (ctx->crypto_op_pool)
97                         rte_mempool_free(ctx->crypto_op_pool);
98
99                 rte_free(ctx);
100         }
101 }
102
103 static struct rte_mbuf *
104 cperf_mbuf_create(struct rte_mempool *mempool,
105                 uint32_t segments_nb,
106                 const struct cperf_options *options,
107                 const struct cperf_test_vector *test_vector)
108 {
109         struct rte_mbuf *mbuf;
110         uint32_t segment_sz = options->max_buffer_size / segments_nb;
111         uint32_t last_sz = options->max_buffer_size % segments_nb;
112         uint8_t *mbuf_data;
113         uint8_t *test_data =
114                         (options->cipher_op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
115                                         test_vector->plaintext.data :
116                                         test_vector->ciphertext.data;
117
118         mbuf = rte_pktmbuf_alloc(mempool);
119         if (mbuf == NULL)
120                 goto error;
121
122         mbuf_data = (uint8_t *)rte_pktmbuf_append(mbuf, segment_sz);
123         if (mbuf_data == NULL)
124                 goto error;
125
126         memcpy(mbuf_data, test_data, segment_sz);
127         test_data += segment_sz;
128         segments_nb--;
129
130         while (segments_nb) {
131                 struct rte_mbuf *m;
132
133                 m = rte_pktmbuf_alloc(mempool);
134                 if (m == NULL)
135                         goto error;
136
137                 rte_pktmbuf_chain(mbuf, m);
138
139                 mbuf_data = (uint8_t *)rte_pktmbuf_append(mbuf, segment_sz);
140                 if (mbuf_data == NULL)
141                         goto error;
142
143                 memcpy(mbuf_data, test_data, segment_sz);
144                 test_data += segment_sz;
145                 segments_nb--;
146         }
147
148         if (last_sz) {
149                 mbuf_data = (uint8_t *)rte_pktmbuf_append(mbuf, last_sz);
150                 if (mbuf_data == NULL)
151                         goto error;
152
153                 memcpy(mbuf_data, test_data, last_sz);
154         }
155
156         if (options->op_type != CPERF_CIPHER_ONLY) {
157                 mbuf_data = (uint8_t *)rte_pktmbuf_append(mbuf,
158                                 options->digest_sz);
159                 if (mbuf_data == NULL)
160                         goto error;
161         }
162
163         if (options->op_type == CPERF_AEAD) {
164                 uint8_t *aead = (uint8_t *)rte_pktmbuf_prepend(mbuf,
165                         RTE_ALIGN_CEIL(options->aead_aad_sz, 16));
166
167                 if (aead == NULL)
168                         goto error;
169
170                 memcpy(aead, test_vector->aad.data, test_vector->aad.length);
171         }
172
173         return mbuf;
174 error:
175         if (mbuf != NULL)
176                 rte_pktmbuf_free(mbuf);
177
178         return NULL;
179 }
180
181 void *
182 cperf_verify_test_constructor(uint8_t dev_id, uint16_t qp_id,
183                 const struct cperf_options *options,
184                 const struct cperf_test_vector *test_vector,
185                 const struct cperf_op_fns *op_fns)
186 {
187         struct cperf_verify_ctx *ctx = NULL;
188         unsigned int mbuf_idx = 0;
189         char pool_name[32] = "";
190
191         ctx = rte_malloc(NULL, sizeof(struct cperf_verify_ctx), 0);
192         if (ctx == NULL)
193                 goto err;
194
195         ctx->dev_id = dev_id;
196         ctx->qp_id = qp_id;
197
198         ctx->populate_ops = op_fns->populate_ops;
199         ctx->options = options;
200         ctx->test_vector = test_vector;
201
202         /* IV goes at the end of the cryptop operation */
203         uint16_t iv_offset = sizeof(struct rte_crypto_op) +
204                 sizeof(struct rte_crypto_sym_op);
205
206         ctx->sess = op_fns->sess_create(dev_id, options, test_vector, iv_offset);
207         if (ctx->sess == NULL)
208                 goto err;
209
210         snprintf(pool_name, sizeof(pool_name), "cperf_pool_in_cdev_%d",
211                         dev_id);
212
213         ctx->pkt_mbuf_pool_in = rte_pktmbuf_pool_create(pool_name,
214                         options->pool_sz * options->segments_nb, 0, 0,
215                         RTE_PKTMBUF_HEADROOM +
216                         RTE_CACHE_LINE_ROUNDUP(
217                                 (options->max_buffer_size / options->segments_nb) +
218                                 (options->max_buffer_size % options->segments_nb) +
219                                         options->digest_sz),
220                         rte_socket_id());
221
222         if (ctx->pkt_mbuf_pool_in == NULL)
223                 goto err;
224
225         /* Generate mbufs_in with plaintext populated for test */
226         ctx->mbufs_in = rte_malloc(NULL,
227                         (sizeof(struct rte_mbuf *) * ctx->options->pool_sz), 0);
228
229         for (mbuf_idx = 0; mbuf_idx < options->pool_sz; mbuf_idx++) {
230                 ctx->mbufs_in[mbuf_idx] = cperf_mbuf_create(
231                                 ctx->pkt_mbuf_pool_in, options->segments_nb,
232                                 options, test_vector);
233                 if (ctx->mbufs_in[mbuf_idx] == NULL)
234                         goto err;
235         }
236
237         if (options->out_of_place == 1) {
238
239                 snprintf(pool_name, sizeof(pool_name), "cperf_pool_out_cdev_%d",
240                                 dev_id);
241
242                 ctx->pkt_mbuf_pool_out = rte_pktmbuf_pool_create(
243                                 pool_name, options->pool_sz, 0, 0,
244                                 RTE_PKTMBUF_HEADROOM +
245                                 RTE_CACHE_LINE_ROUNDUP(
246                                         options->max_buffer_size +
247                                         options->digest_sz),
248                                 rte_socket_id());
249
250                 if (ctx->pkt_mbuf_pool_out == NULL)
251                         goto err;
252         }
253
254         ctx->mbufs_out = rte_malloc(NULL,
255                         (sizeof(struct rte_mbuf *) *
256                         ctx->options->pool_sz), 0);
257
258         for (mbuf_idx = 0; mbuf_idx < options->pool_sz; mbuf_idx++) {
259                 if (options->out_of_place == 1) {
260                         ctx->mbufs_out[mbuf_idx] = cperf_mbuf_create(
261                                         ctx->pkt_mbuf_pool_out, 1,
262                                         options, test_vector);
263                         if (ctx->mbufs_out[mbuf_idx] == NULL)
264                                 goto err;
265                 } else {
266                         ctx->mbufs_out[mbuf_idx] = NULL;
267                 }
268         }
269
270         snprintf(pool_name, sizeof(pool_name), "cperf_op_pool_cdev_%d",
271                         dev_id);
272
273         uint16_t priv_size = test_vector->cipher_iv.length +
274                 test_vector->auth_iv.length;
275         ctx->crypto_op_pool = rte_crypto_op_pool_create(pool_name,
276                         RTE_CRYPTO_OP_TYPE_SYMMETRIC, options->pool_sz,
277                         512, priv_size, rte_socket_id());
278         if (ctx->crypto_op_pool == NULL)
279                 goto err;
280
281         return ctx;
282 err:
283         cperf_verify_test_free(ctx, mbuf_idx);
284
285         return NULL;
286 }
287
288 static int
289 cperf_verify_op(struct rte_crypto_op *op,
290                 const struct cperf_options *options,
291                 const struct cperf_test_vector *vector)
292 {
293         const struct rte_mbuf *m;
294         uint32_t len;
295         uint16_t nb_segs;
296         uint8_t *data;
297         uint32_t cipher_offset, auth_offset;
298         uint8_t cipher, auth;
299         int res = 0;
300
301         if (op->status != RTE_CRYPTO_OP_STATUS_SUCCESS)
302                 return 1;
303
304         if (op->sym->m_dst)
305                 m = op->sym->m_dst;
306         else
307                 m = op->sym->m_src;
308         nb_segs = m->nb_segs;
309         len = 0;
310         while (m && nb_segs != 0) {
311                 len += m->data_len;
312                 m = m->next;
313                 nb_segs--;
314         }
315
316         data = rte_malloc(NULL, len, 0);
317         if (data == NULL)
318                 return 1;
319
320         if (op->sym->m_dst)
321                 m = op->sym->m_dst;
322         else
323                 m = op->sym->m_src;
324         nb_segs = m->nb_segs;
325         len = 0;
326         while (m && nb_segs != 0) {
327                 memcpy(data + len, rte_pktmbuf_mtod(m, uint8_t *),
328                                 m->data_len);
329                 len += m->data_len;
330                 m = m->next;
331                 nb_segs--;
332         }
333
334         switch (options->op_type) {
335         case CPERF_CIPHER_ONLY:
336                 cipher = 1;
337                 cipher_offset = 0;
338                 auth = 0;
339                 auth_offset = 0;
340                 break;
341         case CPERF_CIPHER_THEN_AUTH:
342                 cipher = 1;
343                 cipher_offset = 0;
344                 auth = 1;
345                 auth_offset = options->test_buffer_size;
346                 break;
347         case CPERF_AUTH_ONLY:
348                 cipher = 0;
349                 cipher_offset = 0;
350                 auth = 1;
351                 auth_offset = options->test_buffer_size;
352                 break;
353         case CPERF_AUTH_THEN_CIPHER:
354                 cipher = 1;
355                 cipher_offset = 0;
356                 auth = 1;
357                 auth_offset = options->test_buffer_size;
358                 break;
359         case CPERF_AEAD:
360                 cipher = 1;
361                 cipher_offset = vector->aad.length;
362                 auth = 1;
363                 auth_offset = vector->aad.length + options->test_buffer_size;
364                 break;
365         }
366
367         if (cipher == 1) {
368                 if (options->cipher_op == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
369                         res += memcmp(data + cipher_offset,
370                                         vector->ciphertext.data,
371                                         options->test_buffer_size);
372                 else
373                         res += memcmp(data + cipher_offset,
374                                         vector->plaintext.data,
375                                         options->test_buffer_size);
376         }
377
378         if (auth == 1) {
379                 if (options->auth_op == RTE_CRYPTO_AUTH_OP_GENERATE)
380                         res += memcmp(data + auth_offset,
381                                         vector->digest.data,
382                                         options->digest_sz);
383         }
384
385         return !!res;
386 }
387
388 int
389 cperf_verify_test_runner(void *test_ctx)
390 {
391         struct cperf_verify_ctx *ctx = test_ctx;
392
393         uint64_t ops_enqd = 0, ops_enqd_total = 0, ops_enqd_failed = 0;
394         uint64_t ops_deqd = 0, ops_deqd_total = 0, ops_deqd_failed = 0;
395         uint64_t ops_failed = 0;
396
397         static int only_once;
398
399         uint64_t i, m_idx = 0;
400         uint16_t ops_unused = 0;
401
402         struct rte_crypto_op *ops[ctx->options->max_burst_size];
403         struct rte_crypto_op *ops_processed[ctx->options->max_burst_size];
404
405         uint32_t lcore = rte_lcore_id();
406
407 #ifdef CPERF_LINEARIZATION_ENABLE
408         struct rte_cryptodev_info dev_info;
409         int linearize = 0;
410
411         /* Check if source mbufs require coalescing */
412         if (ctx->options->segments_nb > 1) {
413                 rte_cryptodev_info_get(ctx->dev_id, &dev_info);
414                 if ((dev_info.feature_flags &
415                                 RTE_CRYPTODEV_FF_MBUF_SCATTER_GATHER) == 0)
416                         linearize = 1;
417         }
418 #endif /* CPERF_LINEARIZATION_ENABLE */
419
420         ctx->lcore_id = lcore;
421
422         if (!ctx->options->csv)
423                 printf("\n# Running verify test on device: %u, lcore: %u\n",
424                         ctx->dev_id, lcore);
425
426         uint16_t iv_offset = sizeof(struct rte_crypto_op) +
427                 sizeof(struct rte_crypto_sym_op);
428
429         while (ops_enqd_total < ctx->options->total_ops) {
430
431                 uint16_t burst_size = ((ops_enqd_total + ctx->options->max_burst_size)
432                                 <= ctx->options->total_ops) ?
433                                                 ctx->options->max_burst_size :
434                                                 ctx->options->total_ops -
435                                                 ops_enqd_total;
436
437                 uint16_t ops_needed = burst_size - ops_unused;
438
439                 /* Allocate crypto ops from pool */
440                 if (ops_needed != rte_crypto_op_bulk_alloc(
441                                 ctx->crypto_op_pool,
442                                 RTE_CRYPTO_OP_TYPE_SYMMETRIC,
443                                 ops, ops_needed))
444                         return -1;
445
446                 /* Setup crypto op, attach mbuf etc */
447                 (ctx->populate_ops)(ops, &ctx->mbufs_in[m_idx],
448                                 &ctx->mbufs_out[m_idx],
449                                 ops_needed, ctx->sess, ctx->options,
450                                 ctx->test_vector, iv_offset);
451
452 #ifdef CPERF_LINEARIZATION_ENABLE
453                 if (linearize) {
454                         /* PMD doesn't support scatter-gather and source buffer
455                          * is segmented.
456                          * We need to linearize it before enqueuing.
457                          */
458                         for (i = 0; i < burst_size; i++)
459                                 rte_pktmbuf_linearize(ops[i]->sym->m_src);
460                 }
461 #endif /* CPERF_LINEARIZATION_ENABLE */
462
463                 /* Enqueue burst of ops on crypto device */
464                 ops_enqd = rte_cryptodev_enqueue_burst(ctx->dev_id, ctx->qp_id,
465                                 ops, burst_size);
466                 if (ops_enqd < burst_size)
467                         ops_enqd_failed++;
468
469                 /**
470                  * Calculate number of ops not enqueued (mainly for hw
471                  * accelerators whose ingress queue can fill up).
472                  */
473                 ops_unused = burst_size - ops_enqd;
474                 ops_enqd_total += ops_enqd;
475
476
477                 /* Dequeue processed burst of ops from crypto device */
478                 ops_deqd = rte_cryptodev_dequeue_burst(ctx->dev_id, ctx->qp_id,
479                                 ops_processed, ctx->options->max_burst_size);
480
481                 m_idx += ops_needed;
482                 if (m_idx + ctx->options->max_burst_size > ctx->options->pool_sz)
483                         m_idx = 0;
484
485                 if (ops_deqd == 0) {
486                         /**
487                          * Count dequeue polls which didn't return any
488                          * processed operations. This statistic is mainly
489                          * relevant to hw accelerators.
490                          */
491                         ops_deqd_failed++;
492                         continue;
493                 }
494
495                 for (i = 0; i < ops_deqd; i++) {
496                         if (cperf_verify_op(ops_processed[i], ctx->options,
497                                                 ctx->test_vector))
498                                 ops_failed++;
499                         /* free crypto ops so they can be reused. We don't free
500                          * the mbufs here as we don't want to reuse them as
501                          * the crypto operation will change the data and cause
502                          * failures.
503                          */
504                         rte_crypto_op_free(ops_processed[i]);
505                 }
506                 ops_deqd_total += ops_deqd;
507         }
508
509         /* Dequeue any operations still in the crypto device */
510
511         while (ops_deqd_total < ctx->options->total_ops) {
512                 /* Sending 0 length burst to flush sw crypto device */
513                 rte_cryptodev_enqueue_burst(ctx->dev_id, ctx->qp_id, NULL, 0);
514
515                 /* dequeue burst */
516                 ops_deqd = rte_cryptodev_dequeue_burst(ctx->dev_id, ctx->qp_id,
517                                 ops_processed, ctx->options->max_burst_size);
518                 if (ops_deqd == 0) {
519                         ops_deqd_failed++;
520                         continue;
521                 }
522
523                 for (i = 0; i < ops_deqd; i++) {
524                         if (cperf_verify_op(ops_processed[i], ctx->options,
525                                                 ctx->test_vector))
526                                 ops_failed++;
527                         /* free crypto ops so they can be reused. We don't free
528                          * the mbufs here as we don't want to reuse them as
529                          * the crypto operation will change the data and cause
530                          * failures.
531                          */
532                         rte_crypto_op_free(ops_processed[i]);
533                 }
534                 ops_deqd_total += ops_deqd;
535         }
536
537         if (!ctx->options->csv) {
538                 if (!only_once)
539                         printf("%12s%12s%12s%12s%12s%12s%12s%12s\n\n",
540                                 "lcore id", "Buf Size", "Burst size",
541                                 "Enqueued", "Dequeued", "Failed Enq",
542                                 "Failed Deq", "Failed Ops");
543                 only_once = 1;
544
545                 printf("%12u%12u%12u%12"PRIu64"%12"PRIu64"%12"PRIu64
546                                 "%12"PRIu64"%12"PRIu64"\n",
547                                 ctx->lcore_id,
548                                 ctx->options->max_buffer_size,
549                                 ctx->options->max_burst_size,
550                                 ops_enqd_total,
551                                 ops_deqd_total,
552                                 ops_enqd_failed,
553                                 ops_deqd_failed,
554                                 ops_failed);
555         } else {
556                 if (!only_once)
557                         printf("\n# lcore id, Buffer Size(B), "
558                                 "Burst Size,Enqueued,Dequeued,Failed Enq,"
559                                 "Failed Deq,Failed Ops\n");
560                 only_once = 1;
561
562                 printf("%10u;%10u;%u;%"PRIu64";%"PRIu64";%"PRIu64";%"PRIu64";"
563                                 "%"PRIu64"\n",
564                                 ctx->lcore_id,
565                                 ctx->options->max_buffer_size,
566                                 ctx->options->max_burst_size,
567                                 ops_enqd_total,
568                                 ops_deqd_total,
569                                 ops_enqd_failed,
570                                 ops_deqd_failed,
571                                 ops_failed);
572         }
573
574         return 0;
575 }
576
577
578
579 void
580 cperf_verify_test_destructor(void *arg)
581 {
582         struct cperf_verify_ctx *ctx = arg;
583
584         if (ctx == NULL)
585                 return;
586
587         cperf_verify_test_free(ctx, ctx->options->pool_sz);
588 }