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"
40 #include "cperf_test_common.h"
42 struct cperf_verify_ctx {
47 struct rte_mempool *pkt_mbuf_pool_in;
48 struct rte_mempool *pkt_mbuf_pool_out;
49 struct rte_mbuf **mbufs_in;
50 struct rte_mbuf **mbufs_out;
52 struct rte_mempool *crypto_op_pool;
54 struct rte_cryptodev_sym_session *sess;
56 cperf_populate_ops_t populate_ops;
58 const struct cperf_options *options;
59 const struct cperf_test_vector *test_vector;
62 struct cperf_op_result {
63 enum rte_crypto_op_status status;
67 cperf_verify_test_free(struct cperf_verify_ctx *ctx)
71 rte_cryptodev_sym_session_clear(ctx->dev_id, ctx->sess);
72 rte_cryptodev_sym_session_free(ctx->sess);
75 cperf_free_common_memory(ctx->options,
76 ctx->pkt_mbuf_pool_in,
77 ctx->pkt_mbuf_pool_out,
78 ctx->mbufs_in, ctx->mbufs_out,
86 cperf_verify_test_constructor(struct rte_mempool *sess_mp,
87 uint8_t dev_id, uint16_t qp_id,
88 const struct cperf_options *options,
89 const struct cperf_test_vector *test_vector,
90 const struct cperf_op_fns *op_fns)
92 struct cperf_verify_ctx *ctx = NULL;
94 ctx = rte_malloc(NULL, sizeof(struct cperf_verify_ctx), 0);
101 ctx->populate_ops = op_fns->populate_ops;
102 ctx->options = options;
103 ctx->test_vector = test_vector;
105 /* IV goes at the end of the cryptop operation */
106 uint16_t iv_offset = sizeof(struct rte_crypto_op) +
107 sizeof(struct rte_crypto_sym_op);
109 ctx->sess = op_fns->sess_create(sess_mp, dev_id, options, test_vector,
111 if (ctx->sess == NULL)
114 if (cperf_alloc_common_memory(options, test_vector, dev_id, 0,
115 &ctx->pkt_mbuf_pool_in, &ctx->pkt_mbuf_pool_out,
116 &ctx->mbufs_in, &ctx->mbufs_out,
117 &ctx->crypto_op_pool) < 0)
122 cperf_verify_test_free(ctx);
128 cperf_verify_op(struct rte_crypto_op *op,
129 const struct cperf_options *options,
130 const struct cperf_test_vector *vector)
132 const struct rte_mbuf *m;
136 uint32_t cipher_offset, auth_offset;
137 uint8_t cipher, auth;
140 if (op->status != RTE_CRYPTO_OP_STATUS_SUCCESS)
147 nb_segs = m->nb_segs;
149 while (m && nb_segs != 0) {
155 data = rte_malloc(NULL, len, 0);
163 nb_segs = m->nb_segs;
165 while (m && nb_segs != 0) {
166 memcpy(data + len, rte_pktmbuf_mtod(m, uint8_t *),
173 switch (options->op_type) {
174 case CPERF_CIPHER_ONLY:
180 case CPERF_CIPHER_THEN_AUTH:
184 auth_offset = options->test_buffer_size;
186 case CPERF_AUTH_ONLY:
190 auth_offset = options->test_buffer_size;
192 case CPERF_AUTH_THEN_CIPHER:
196 auth_offset = options->test_buffer_size;
202 auth_offset = options->test_buffer_size;
210 if (options->cipher_op == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
211 res += memcmp(data + cipher_offset,
212 vector->ciphertext.data,
213 options->test_buffer_size);
215 res += memcmp(data + cipher_offset,
216 vector->plaintext.data,
217 options->test_buffer_size);
221 if (options->auth_op == RTE_CRYPTO_AUTH_OP_GENERATE)
222 res += memcmp(data + auth_offset,
233 cperf_verify_test_runner(void *test_ctx)
235 struct cperf_verify_ctx *ctx = test_ctx;
237 uint64_t ops_enqd = 0, ops_enqd_total = 0, ops_enqd_failed = 0;
238 uint64_t ops_deqd = 0, ops_deqd_total = 0, ops_deqd_failed = 0;
239 uint64_t ops_failed = 0;
241 static int only_once;
243 uint64_t i, m_idx = 0;
244 uint16_t ops_unused = 0;
246 struct rte_crypto_op *ops[ctx->options->max_burst_size];
247 struct rte_crypto_op *ops_processed[ctx->options->max_burst_size];
249 uint32_t lcore = rte_lcore_id();
251 #ifdef CPERF_LINEARIZATION_ENABLE
252 struct rte_cryptodev_info dev_info;
255 /* Check if source mbufs require coalescing */
256 if (ctx->options->segments_nb > 1) {
257 rte_cryptodev_info_get(ctx->dev_id, &dev_info);
258 if ((dev_info.feature_flags &
259 RTE_CRYPTODEV_FF_MBUF_SCATTER_GATHER) == 0)
262 #endif /* CPERF_LINEARIZATION_ENABLE */
264 ctx->lcore_id = lcore;
266 if (!ctx->options->csv)
267 printf("\n# Running verify test on device: %u, lcore: %u\n",
270 uint16_t iv_offset = sizeof(struct rte_crypto_op) +
271 sizeof(struct rte_crypto_sym_op);
273 while (ops_enqd_total < ctx->options->total_ops) {
275 uint16_t burst_size = ((ops_enqd_total + ctx->options->max_burst_size)
276 <= ctx->options->total_ops) ?
277 ctx->options->max_burst_size :
278 ctx->options->total_ops -
281 uint16_t ops_needed = burst_size - ops_unused;
283 /* Allocate crypto ops from pool */
284 if (ops_needed != rte_crypto_op_bulk_alloc(
286 RTE_CRYPTO_OP_TYPE_SYMMETRIC,
289 "Failed to allocate more crypto operations "
290 "from the the crypto operation pool.\n"
291 "Consider increasing the pool size "
296 /* Setup crypto op, attach mbuf etc */
297 (ctx->populate_ops)(ops, &ctx->mbufs_in[m_idx],
298 &ctx->mbufs_out[m_idx],
299 ops_needed, ctx->sess, ctx->options,
300 ctx->test_vector, iv_offset);
302 #ifdef CPERF_LINEARIZATION_ENABLE
304 /* PMD doesn't support scatter-gather and source buffer
306 * We need to linearize it before enqueuing.
308 for (i = 0; i < burst_size; i++)
309 rte_pktmbuf_linearize(ops[i]->sym->m_src);
311 #endif /* CPERF_LINEARIZATION_ENABLE */
313 /* Enqueue burst of ops on crypto device */
314 ops_enqd = rte_cryptodev_enqueue_burst(ctx->dev_id, ctx->qp_id,
316 if (ops_enqd < burst_size)
320 * Calculate number of ops not enqueued (mainly for hw
321 * accelerators whose ingress queue can fill up).
323 ops_unused = burst_size - ops_enqd;
324 ops_enqd_total += ops_enqd;
327 /* Dequeue processed burst of ops from crypto device */
328 ops_deqd = rte_cryptodev_dequeue_burst(ctx->dev_id, ctx->qp_id,
329 ops_processed, ctx->options->max_burst_size);
332 if (m_idx + ctx->options->max_burst_size > ctx->options->pool_sz)
337 * Count dequeue polls which didn't return any
338 * processed operations. This statistic is mainly
339 * relevant to hw accelerators.
345 for (i = 0; i < ops_deqd; i++) {
346 if (cperf_verify_op(ops_processed[i], ctx->options,
349 /* free crypto ops so they can be reused. We don't free
350 * the mbufs here as we don't want to reuse them as
351 * the crypto operation will change the data and cause
354 rte_crypto_op_free(ops_processed[i]);
356 ops_deqd_total += ops_deqd;
359 /* Dequeue any operations still in the crypto device */
361 while (ops_deqd_total < ctx->options->total_ops) {
362 /* Sending 0 length burst to flush sw crypto device */
363 rte_cryptodev_enqueue_burst(ctx->dev_id, ctx->qp_id, NULL, 0);
366 ops_deqd = rte_cryptodev_dequeue_burst(ctx->dev_id, ctx->qp_id,
367 ops_processed, ctx->options->max_burst_size);
373 for (i = 0; i < ops_deqd; i++) {
374 if (cperf_verify_op(ops_processed[i], ctx->options,
377 /* free crypto ops so they can be reused. We don't free
378 * the mbufs here as we don't want to reuse them as
379 * the crypto operation will change the data and cause
382 rte_crypto_op_free(ops_processed[i]);
384 ops_deqd_total += ops_deqd;
387 if (!ctx->options->csv) {
389 printf("%12s%12s%12s%12s%12s%12s%12s%12s\n\n",
390 "lcore id", "Buf Size", "Burst size",
391 "Enqueued", "Dequeued", "Failed Enq",
392 "Failed Deq", "Failed Ops");
395 printf("%12u%12u%12u%12"PRIu64"%12"PRIu64"%12"PRIu64
396 "%12"PRIu64"%12"PRIu64"\n",
398 ctx->options->max_buffer_size,
399 ctx->options->max_burst_size,
407 printf("\n# lcore id, Buffer Size(B), "
408 "Burst Size,Enqueued,Dequeued,Failed Enq,"
409 "Failed Deq,Failed Ops\n");
412 printf("%10u;%10u;%u;%"PRIu64";%"PRIu64";%"PRIu64";%"PRIu64";"
415 ctx->options->max_buffer_size,
416 ctx->options->max_burst_size,
430 cperf_verify_test_destructor(void *arg)
432 struct cperf_verify_ctx *ctx = arg;
437 rte_cryptodev_stop(ctx->dev_id);
439 cperf_verify_test_free(ctx);