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