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_free(ctx->dev_id, ctx->sess);
75 for (i = 0; i < mbuf_nb; i++)
76 rte_pktmbuf_free(ctx->mbufs_in[i]);
78 rte_free(ctx->mbufs_in);
82 for (i = 0; i < mbuf_nb; i++) {
83 if (ctx->mbufs_out[i] != NULL)
84 rte_pktmbuf_free(ctx->mbufs_out[i]);
87 rte_free(ctx->mbufs_out);
90 if (ctx->pkt_mbuf_pool_in)
91 rte_mempool_free(ctx->pkt_mbuf_pool_in);
93 if (ctx->pkt_mbuf_pool_out)
94 rte_mempool_free(ctx->pkt_mbuf_pool_out);
96 if (ctx->crypto_op_pool)
97 rte_mempool_free(ctx->crypto_op_pool);
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)
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;
114 (options->cipher_op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
115 test_vector->plaintext.data :
116 test_vector->ciphertext.data;
118 mbuf = rte_pktmbuf_alloc(mempool);
122 mbuf_data = (uint8_t *)rte_pktmbuf_append(mbuf, segment_sz);
123 if (mbuf_data == NULL)
126 memcpy(mbuf_data, test_data, segment_sz);
127 test_data += segment_sz;
130 while (segments_nb) {
133 m = rte_pktmbuf_alloc(mempool);
137 rte_pktmbuf_chain(mbuf, m);
139 mbuf_data = (uint8_t *)rte_pktmbuf_append(mbuf, segment_sz);
140 if (mbuf_data == NULL)
143 memcpy(mbuf_data, test_data, segment_sz);
144 test_data += segment_sz;
149 mbuf_data = (uint8_t *)rte_pktmbuf_append(mbuf, last_sz);
150 if (mbuf_data == NULL)
153 memcpy(mbuf_data, test_data, last_sz);
156 if (options->op_type != CPERF_CIPHER_ONLY) {
157 mbuf_data = (uint8_t *)rte_pktmbuf_append(mbuf,
158 options->auth_digest_sz);
159 if (mbuf_data == NULL)
163 if (options->op_type == CPERF_AEAD) {
164 uint8_t *aead = (uint8_t *)rte_pktmbuf_prepend(mbuf,
165 RTE_ALIGN_CEIL(options->auth_aad_sz, 16));
170 memcpy(aead, test_vector->aad.data, test_vector->aad.length);
176 rte_pktmbuf_free(mbuf);
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)
187 struct cperf_verify_ctx *ctx = NULL;
188 unsigned int mbuf_idx = 0;
189 char pool_name[32] = "";
191 ctx = rte_malloc(NULL, sizeof(struct cperf_verify_ctx), 0);
195 ctx->dev_id = dev_id;
198 ctx->populate_ops = op_fns->populate_ops;
199 ctx->options = options;
200 ctx->test_vector = test_vector;
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);
206 ctx->sess = op_fns->sess_create(dev_id, options, test_vector, iv_offset);
207 if (ctx->sess == NULL)
210 snprintf(pool_name, sizeof(pool_name), "cperf_pool_in_cdev_%d",
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->auth_digest_sz),
222 if (ctx->pkt_mbuf_pool_in == NULL)
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);
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)
237 if (options->out_of_place == 1) {
239 snprintf(pool_name, sizeof(pool_name), "cperf_pool_out_cdev_%d",
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->auth_digest_sz),
250 if (ctx->pkt_mbuf_pool_out == NULL)
254 ctx->mbufs_out = rte_malloc(NULL,
255 (sizeof(struct rte_mbuf *) *
256 ctx->options->pool_sz), 0);
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)
266 ctx->mbufs_out[mbuf_idx] = NULL;
270 snprintf(pool_name, sizeof(pool_name), "cperf_op_pool_cdev_%d",
273 uint16_t priv_size = test_vector->iv.length;
274 ctx->crypto_op_pool = rte_crypto_op_pool_create(pool_name,
275 RTE_CRYPTO_OP_TYPE_SYMMETRIC, options->pool_sz,
276 512, priv_size, rte_socket_id());
277 if (ctx->crypto_op_pool == NULL)
282 cperf_verify_test_free(ctx, mbuf_idx);
288 cperf_verify_op(struct rte_crypto_op *op,
289 const struct cperf_options *options,
290 const struct cperf_test_vector *vector)
292 const struct rte_mbuf *m;
296 uint32_t cipher_offset, auth_offset;
297 uint8_t cipher, auth;
300 if (op->status != RTE_CRYPTO_OP_STATUS_SUCCESS)
307 nb_segs = m->nb_segs;
309 while (m && nb_segs != 0) {
315 data = rte_malloc(NULL, len, 0);
323 nb_segs = m->nb_segs;
325 while (m && nb_segs != 0) {
326 memcpy(data + len, rte_pktmbuf_mtod(m, uint8_t *),
333 switch (options->op_type) {
334 case CPERF_CIPHER_ONLY:
340 case CPERF_CIPHER_THEN_AUTH:
344 auth_offset = options->test_buffer_size;
346 case CPERF_AUTH_ONLY:
350 auth_offset = options->test_buffer_size;
352 case CPERF_AUTH_THEN_CIPHER:
356 auth_offset = options->test_buffer_size;
360 cipher_offset = vector->aad.length;
362 auth_offset = vector->aad.length + options->test_buffer_size;
367 if (options->cipher_op == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
368 res += memcmp(data + cipher_offset,
369 vector->ciphertext.data,
370 options->test_buffer_size);
372 res += memcmp(data + cipher_offset,
373 vector->plaintext.data,
374 options->test_buffer_size);
378 if (options->auth_op == RTE_CRYPTO_AUTH_OP_GENERATE)
379 res += memcmp(data + auth_offset,
381 options->auth_digest_sz);
388 cperf_verify_test_runner(void *test_ctx)
390 struct cperf_verify_ctx *ctx = test_ctx;
392 uint64_t ops_enqd = 0, ops_enqd_total = 0, ops_enqd_failed = 0;
393 uint64_t ops_deqd = 0, ops_deqd_total = 0, ops_deqd_failed = 0;
394 uint64_t ops_failed = 0;
396 static int only_once;
398 uint64_t i, m_idx = 0;
399 uint16_t ops_unused = 0;
401 struct rte_crypto_op *ops[ctx->options->max_burst_size];
402 struct rte_crypto_op *ops_processed[ctx->options->max_burst_size];
404 uint32_t lcore = rte_lcore_id();
406 #ifdef CPERF_LINEARIZATION_ENABLE
407 struct rte_cryptodev_info dev_info;
410 /* Check if source mbufs require coalescing */
411 if (ctx->options->segments_nb > 1) {
412 rte_cryptodev_info_get(ctx->dev_id, &dev_info);
413 if ((dev_info.feature_flags &
414 RTE_CRYPTODEV_FF_MBUF_SCATTER_GATHER) == 0)
417 #endif /* CPERF_LINEARIZATION_ENABLE */
419 ctx->lcore_id = lcore;
421 if (!ctx->options->csv)
422 printf("\n# Running verify test on device: %u, lcore: %u\n",
425 uint16_t iv_offset = sizeof(struct rte_crypto_op) +
426 sizeof(struct rte_crypto_sym_op);
428 while (ops_enqd_total < ctx->options->total_ops) {
430 uint16_t burst_size = ((ops_enqd_total + ctx->options->max_burst_size)
431 <= ctx->options->total_ops) ?
432 ctx->options->max_burst_size :
433 ctx->options->total_ops -
436 uint16_t ops_needed = burst_size - ops_unused;
438 /* Allocate crypto ops from pool */
439 if (ops_needed != rte_crypto_op_bulk_alloc(
441 RTE_CRYPTO_OP_TYPE_SYMMETRIC,
445 /* Setup crypto op, attach mbuf etc */
446 (ctx->populate_ops)(ops, &ctx->mbufs_in[m_idx],
447 &ctx->mbufs_out[m_idx],
448 ops_needed, ctx->sess, ctx->options,
449 ctx->test_vector, iv_offset);
451 #ifdef CPERF_LINEARIZATION_ENABLE
453 /* PMD doesn't support scatter-gather and source buffer
455 * We need to linearize it before enqueuing.
457 for (i = 0; i < burst_size; i++)
458 rte_pktmbuf_linearize(ops[i]->sym->m_src);
460 #endif /* CPERF_LINEARIZATION_ENABLE */
462 /* Enqueue burst of ops on crypto device */
463 ops_enqd = rte_cryptodev_enqueue_burst(ctx->dev_id, ctx->qp_id,
465 if (ops_enqd < burst_size)
469 * Calculate number of ops not enqueued (mainly for hw
470 * accelerators whose ingress queue can fill up).
472 ops_unused = burst_size - ops_enqd;
473 ops_enqd_total += ops_enqd;
476 /* Dequeue processed burst of ops from crypto device */
477 ops_deqd = rte_cryptodev_dequeue_burst(ctx->dev_id, ctx->qp_id,
478 ops_processed, ctx->options->max_burst_size);
481 if (m_idx + ctx->options->max_burst_size > ctx->options->pool_sz)
486 * Count dequeue polls which didn't return any
487 * processed operations. This statistic is mainly
488 * relevant to hw accelerators.
494 for (i = 0; i < ops_deqd; i++) {
495 if (cperf_verify_op(ops_processed[i], ctx->options,
498 /* free crypto ops so they can be reused. We don't free
499 * the mbufs here as we don't want to reuse them as
500 * the crypto operation will change the data and cause
503 rte_crypto_op_free(ops_processed[i]);
505 ops_deqd_total += ops_deqd;
508 /* Dequeue any operations still in the crypto device */
510 while (ops_deqd_total < ctx->options->total_ops) {
511 /* Sending 0 length burst to flush sw crypto device */
512 rte_cryptodev_enqueue_burst(ctx->dev_id, ctx->qp_id, NULL, 0);
515 ops_deqd = rte_cryptodev_dequeue_burst(ctx->dev_id, ctx->qp_id,
516 ops_processed, ctx->options->max_burst_size);
522 for (i = 0; i < ops_deqd; i++) {
523 if (cperf_verify_op(ops_processed[i], ctx->options,
526 /* free crypto ops so they can be reused. We don't free
527 * the mbufs here as we don't want to reuse them as
528 * the crypto operation will change the data and cause
531 rte_crypto_op_free(ops_processed[i]);
533 ops_deqd_total += ops_deqd;
536 if (!ctx->options->csv) {
538 printf("%12s%12s%12s%12s%12s%12s%12s%12s\n\n",
539 "lcore id", "Buf Size", "Burst size",
540 "Enqueued", "Dequeued", "Failed Enq",
541 "Failed Deq", "Failed Ops");
544 printf("%12u%12u%12u%12"PRIu64"%12"PRIu64"%12"PRIu64
545 "%12"PRIu64"%12"PRIu64"\n",
547 ctx->options->max_buffer_size,
548 ctx->options->max_burst_size,
556 printf("\n# lcore id, Buffer Size(B), "
557 "Burst Size,Enqueued,Dequeued,Failed Enq,"
558 "Failed Deq,Failed Ops\n");
561 printf("%10u;%10u;%u;%"PRIu64";%"PRIu64";%"PRIu64";%"PRIu64";"
564 ctx->options->max_buffer_size,
565 ctx->options->max_burst_size,
579 cperf_verify_test_destructor(void *arg)
581 struct cperf_verify_ctx *ctx = arg;
586 cperf_verify_test_free(ctx, ctx->options->pool_sz);