4 * Copyright(c) 2016-2017 Intel Corporation. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
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.
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.
33 #include <rte_malloc.h>
34 #include <rte_cycles.h>
35 #include <rte_crypto.h>
36 #include <rte_cryptodev.h>
38 #include "cperf_test_verify.h"
39 #include "cperf_ops.h"
41 struct cperf_verify_ctx {
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;
51 struct rte_mempool *crypto_op_pool;
53 struct rte_cryptodev_sym_session *sess;
55 cperf_populate_ops_t populate_ops;
57 const struct cperf_options *options;
58 const struct cperf_test_vector *test_vector;
61 struct cperf_op_result {
62 enum rte_crypto_op_status status;
66 cperf_verify_test_free(struct cperf_verify_ctx *ctx, uint32_t mbuf_nb)
72 rte_cryptodev_sym_session_clear(ctx->dev_id, ctx->sess);
73 rte_cryptodev_sym_session_free(ctx->sess);
77 for (i = 0; i < mbuf_nb; i++)
78 rte_pktmbuf_free(ctx->mbufs_in[i]);
80 rte_free(ctx->mbufs_in);
84 for (i = 0; i < mbuf_nb; i++) {
85 if (ctx->mbufs_out[i] != NULL)
86 rte_pktmbuf_free(ctx->mbufs_out[i]);
89 rte_free(ctx->mbufs_out);
92 if (ctx->pkt_mbuf_pool_in)
93 rte_mempool_free(ctx->pkt_mbuf_pool_in);
95 if (ctx->pkt_mbuf_pool_out)
96 rte_mempool_free(ctx->pkt_mbuf_pool_out);
98 if (ctx->crypto_op_pool)
99 rte_mempool_free(ctx->crypto_op_pool);
105 static struct rte_mbuf *
106 cperf_mbuf_create(struct rte_mempool *mempool,
107 uint32_t segments_nb,
108 const struct cperf_options *options,
109 const struct cperf_test_vector *test_vector)
111 struct rte_mbuf *mbuf;
112 uint32_t segment_sz = options->max_buffer_size / segments_nb;
113 uint32_t last_sz = options->max_buffer_size % segments_nb;
116 (options->cipher_op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
117 test_vector->plaintext.data :
118 test_vector->ciphertext.data;
120 mbuf = rte_pktmbuf_alloc(mempool);
124 mbuf_data = (uint8_t *)rte_pktmbuf_append(mbuf, segment_sz);
125 if (mbuf_data == NULL)
128 memcpy(mbuf_data, test_data, segment_sz);
129 test_data += segment_sz;
132 while (segments_nb) {
135 m = rte_pktmbuf_alloc(mempool);
139 rte_pktmbuf_chain(mbuf, m);
141 mbuf_data = (uint8_t *)rte_pktmbuf_append(mbuf, segment_sz);
142 if (mbuf_data == NULL)
145 memcpy(mbuf_data, test_data, segment_sz);
146 test_data += segment_sz;
151 mbuf_data = (uint8_t *)rte_pktmbuf_append(mbuf, last_sz);
152 if (mbuf_data == NULL)
155 memcpy(mbuf_data, test_data, last_sz);
158 if (options->op_type != CPERF_CIPHER_ONLY) {
159 mbuf_data = (uint8_t *)rte_pktmbuf_append(mbuf,
161 if (mbuf_data == NULL)
165 if (options->op_type == CPERF_AEAD) {
166 uint8_t *aead = (uint8_t *)rte_pktmbuf_prepend(mbuf,
167 RTE_ALIGN_CEIL(options->aead_aad_sz, 16));
172 memcpy(aead, test_vector->aad.data, test_vector->aad.length);
178 rte_pktmbuf_free(mbuf);
184 cperf_verify_test_constructor(struct rte_mempool *sess_mp,
185 uint8_t dev_id, uint16_t qp_id,
186 const struct cperf_options *options,
187 const struct cperf_test_vector *test_vector,
188 const struct cperf_op_fns *op_fns)
190 struct cperf_verify_ctx *ctx = NULL;
191 unsigned int mbuf_idx = 0;
192 char pool_name[32] = "";
194 ctx = rte_malloc(NULL, sizeof(struct cperf_verify_ctx), 0);
198 ctx->dev_id = dev_id;
201 ctx->populate_ops = op_fns->populate_ops;
202 ctx->options = options;
203 ctx->test_vector = test_vector;
205 /* IV goes at the end of the cryptop operation */
206 uint16_t iv_offset = sizeof(struct rte_crypto_op) +
207 sizeof(struct rte_crypto_sym_op);
209 ctx->sess = op_fns->sess_create(sess_mp, dev_id, options, test_vector,
211 if (ctx->sess == NULL)
214 snprintf(pool_name, sizeof(pool_name), "cperf_pool_in_cdev_%d",
217 ctx->pkt_mbuf_pool_in = rte_pktmbuf_pool_create(pool_name,
218 options->pool_sz * options->segments_nb, 0, 0,
219 RTE_PKTMBUF_HEADROOM +
220 RTE_CACHE_LINE_ROUNDUP(
221 (options->max_buffer_size / options->segments_nb) +
222 (options->max_buffer_size % options->segments_nb) +
226 if (ctx->pkt_mbuf_pool_in == NULL)
229 /* Generate mbufs_in with plaintext populated for test */
230 ctx->mbufs_in = rte_malloc(NULL,
231 (sizeof(struct rte_mbuf *) * ctx->options->pool_sz), 0);
233 for (mbuf_idx = 0; mbuf_idx < options->pool_sz; mbuf_idx++) {
234 ctx->mbufs_in[mbuf_idx] = cperf_mbuf_create(
235 ctx->pkt_mbuf_pool_in, options->segments_nb,
236 options, test_vector);
237 if (ctx->mbufs_in[mbuf_idx] == NULL)
241 if (options->out_of_place == 1) {
243 snprintf(pool_name, sizeof(pool_name), "cperf_pool_out_cdev_%d",
246 ctx->pkt_mbuf_pool_out = rte_pktmbuf_pool_create(
247 pool_name, options->pool_sz, 0, 0,
248 RTE_PKTMBUF_HEADROOM +
249 RTE_CACHE_LINE_ROUNDUP(
250 options->max_buffer_size +
254 if (ctx->pkt_mbuf_pool_out == NULL)
258 ctx->mbufs_out = rte_malloc(NULL,
259 (sizeof(struct rte_mbuf *) *
260 ctx->options->pool_sz), 0);
262 for (mbuf_idx = 0; mbuf_idx < options->pool_sz; mbuf_idx++) {
263 if (options->out_of_place == 1) {
264 ctx->mbufs_out[mbuf_idx] = cperf_mbuf_create(
265 ctx->pkt_mbuf_pool_out, 1,
266 options, test_vector);
267 if (ctx->mbufs_out[mbuf_idx] == NULL)
270 ctx->mbufs_out[mbuf_idx] = NULL;
274 snprintf(pool_name, sizeof(pool_name), "cperf_op_pool_cdev_%d",
277 uint16_t priv_size = test_vector->cipher_iv.length +
278 test_vector->auth_iv.length + test_vector->aead_iv.length;
279 ctx->crypto_op_pool = rte_crypto_op_pool_create(pool_name,
280 RTE_CRYPTO_OP_TYPE_SYMMETRIC, options->pool_sz,
281 512, priv_size, rte_socket_id());
282 if (ctx->crypto_op_pool == NULL)
287 cperf_verify_test_free(ctx, mbuf_idx);
293 cperf_verify_op(struct rte_crypto_op *op,
294 const struct cperf_options *options,
295 const struct cperf_test_vector *vector)
297 const struct rte_mbuf *m;
301 uint32_t cipher_offset, auth_offset;
302 uint8_t cipher, auth;
305 if (op->status != RTE_CRYPTO_OP_STATUS_SUCCESS)
312 nb_segs = m->nb_segs;
314 while (m && nb_segs != 0) {
320 data = rte_malloc(NULL, len, 0);
328 nb_segs = m->nb_segs;
330 while (m && nb_segs != 0) {
331 memcpy(data + len, rte_pktmbuf_mtod(m, uint8_t *),
338 switch (options->op_type) {
339 case CPERF_CIPHER_ONLY:
345 case CPERF_CIPHER_THEN_AUTH:
349 auth_offset = options->test_buffer_size;
351 case CPERF_AUTH_ONLY:
355 auth_offset = options->test_buffer_size;
357 case CPERF_AUTH_THEN_CIPHER:
361 auth_offset = options->test_buffer_size;
365 cipher_offset = vector->aad.length;
367 auth_offset = vector->aad.length + options->test_buffer_size;
375 if (options->cipher_op == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
376 res += memcmp(data + cipher_offset,
377 vector->ciphertext.data,
378 options->test_buffer_size);
380 res += memcmp(data + cipher_offset,
381 vector->plaintext.data,
382 options->test_buffer_size);
386 if (options->auth_op == RTE_CRYPTO_AUTH_OP_GENERATE)
387 res += memcmp(data + auth_offset,
398 cperf_verify_test_runner(void *test_ctx)
400 struct cperf_verify_ctx *ctx = test_ctx;
402 uint64_t ops_enqd = 0, ops_enqd_total = 0, ops_enqd_failed = 0;
403 uint64_t ops_deqd = 0, ops_deqd_total = 0, ops_deqd_failed = 0;
404 uint64_t ops_failed = 0;
406 static int only_once;
408 uint64_t i, m_idx = 0;
409 uint16_t ops_unused = 0;
411 struct rte_crypto_op *ops[ctx->options->max_burst_size];
412 struct rte_crypto_op *ops_processed[ctx->options->max_burst_size];
414 uint32_t lcore = rte_lcore_id();
416 #ifdef CPERF_LINEARIZATION_ENABLE
417 struct rte_cryptodev_info dev_info;
420 /* Check if source mbufs require coalescing */
421 if (ctx->options->segments_nb > 1) {
422 rte_cryptodev_info_get(ctx->dev_id, &dev_info);
423 if ((dev_info.feature_flags &
424 RTE_CRYPTODEV_FF_MBUF_SCATTER_GATHER) == 0)
427 #endif /* CPERF_LINEARIZATION_ENABLE */
429 ctx->lcore_id = lcore;
431 if (!ctx->options->csv)
432 printf("\n# Running verify test on device: %u, lcore: %u\n",
435 uint16_t iv_offset = sizeof(struct rte_crypto_op) +
436 sizeof(struct rte_crypto_sym_op);
438 while (ops_enqd_total < ctx->options->total_ops) {
440 uint16_t burst_size = ((ops_enqd_total + ctx->options->max_burst_size)
441 <= ctx->options->total_ops) ?
442 ctx->options->max_burst_size :
443 ctx->options->total_ops -
446 uint16_t ops_needed = burst_size - ops_unused;
448 /* Allocate crypto ops from pool */
449 if (ops_needed != rte_crypto_op_bulk_alloc(
451 RTE_CRYPTO_OP_TYPE_SYMMETRIC,
454 "Failed to allocate more crypto operations "
455 "from the the crypto operation pool.\n"
456 "Consider increasing the pool size "
461 /* Setup crypto op, attach mbuf etc */
462 (ctx->populate_ops)(ops, &ctx->mbufs_in[m_idx],
463 &ctx->mbufs_out[m_idx],
464 ops_needed, ctx->sess, ctx->options,
465 ctx->test_vector, iv_offset);
467 #ifdef CPERF_LINEARIZATION_ENABLE
469 /* PMD doesn't support scatter-gather and source buffer
471 * We need to linearize it before enqueuing.
473 for (i = 0; i < burst_size; i++)
474 rte_pktmbuf_linearize(ops[i]->sym->m_src);
476 #endif /* CPERF_LINEARIZATION_ENABLE */
478 /* Enqueue burst of ops on crypto device */
479 ops_enqd = rte_cryptodev_enqueue_burst(ctx->dev_id, ctx->qp_id,
481 if (ops_enqd < burst_size)
485 * Calculate number of ops not enqueued (mainly for hw
486 * accelerators whose ingress queue can fill up).
488 ops_unused = burst_size - ops_enqd;
489 ops_enqd_total += ops_enqd;
492 /* Dequeue processed burst of ops from crypto device */
493 ops_deqd = rte_cryptodev_dequeue_burst(ctx->dev_id, ctx->qp_id,
494 ops_processed, ctx->options->max_burst_size);
497 if (m_idx + ctx->options->max_burst_size > ctx->options->pool_sz)
502 * Count dequeue polls which didn't return any
503 * processed operations. This statistic is mainly
504 * relevant to hw accelerators.
510 for (i = 0; i < ops_deqd; i++) {
511 if (cperf_verify_op(ops_processed[i], ctx->options,
514 /* free crypto ops so they can be reused. We don't free
515 * the mbufs here as we don't want to reuse them as
516 * the crypto operation will change the data and cause
519 rte_crypto_op_free(ops_processed[i]);
521 ops_deqd_total += ops_deqd;
524 /* Dequeue any operations still in the crypto device */
526 while (ops_deqd_total < ctx->options->total_ops) {
527 /* Sending 0 length burst to flush sw crypto device */
528 rte_cryptodev_enqueue_burst(ctx->dev_id, ctx->qp_id, NULL, 0);
531 ops_deqd = rte_cryptodev_dequeue_burst(ctx->dev_id, ctx->qp_id,
532 ops_processed, ctx->options->max_burst_size);
538 for (i = 0; i < ops_deqd; i++) {
539 if (cperf_verify_op(ops_processed[i], ctx->options,
542 /* free crypto ops so they can be reused. We don't free
543 * the mbufs here as we don't want to reuse them as
544 * the crypto operation will change the data and cause
547 rte_crypto_op_free(ops_processed[i]);
549 ops_deqd_total += ops_deqd;
552 if (!ctx->options->csv) {
554 printf("%12s%12s%12s%12s%12s%12s%12s%12s\n\n",
555 "lcore id", "Buf Size", "Burst size",
556 "Enqueued", "Dequeued", "Failed Enq",
557 "Failed Deq", "Failed Ops");
560 printf("%12u%12u%12u%12"PRIu64"%12"PRIu64"%12"PRIu64
561 "%12"PRIu64"%12"PRIu64"\n",
563 ctx->options->max_buffer_size,
564 ctx->options->max_burst_size,
572 printf("\n# lcore id, Buffer Size(B), "
573 "Burst Size,Enqueued,Dequeued,Failed Enq,"
574 "Failed Deq,Failed Ops\n");
577 printf("%10u;%10u;%u;%"PRIu64";%"PRIu64";%"PRIu64";%"PRIu64";"
580 ctx->options->max_buffer_size,
581 ctx->options->max_burst_size,
595 cperf_verify_test_destructor(void *arg)
597 struct cperf_verify_ctx *ctx = arg;
602 rte_cryptodev_stop(ctx->dev_id);
604 cperf_verify_test_free(ctx, ctx->options->pool_sz);