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_mbuf_set(struct rte_mbuf *mbuf,
234 const struct cperf_options *options,
235 const struct cperf_test_vector *test_vector)
237 uint32_t segment_sz = options->segment_sz;
240 (options->cipher_op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
241 test_vector->plaintext.data :
242 test_vector->ciphertext.data;
243 uint32_t remaining_bytes = options->max_buffer_size;
245 while (remaining_bytes) {
246 mbuf_data = rte_pktmbuf_mtod(mbuf, uint8_t *);
248 if (remaining_bytes <= segment_sz) {
249 memcpy(mbuf_data, test_data, remaining_bytes);
253 memcpy(mbuf_data, test_data, segment_sz);
254 remaining_bytes -= segment_sz;
255 test_data += segment_sz;
261 cperf_verify_test_runner(void *test_ctx)
263 struct cperf_verify_ctx *ctx = test_ctx;
265 uint64_t ops_enqd = 0, ops_enqd_total = 0, ops_enqd_failed = 0;
266 uint64_t ops_deqd = 0, ops_deqd_total = 0, ops_deqd_failed = 0;
267 uint64_t ops_failed = 0;
269 static int only_once;
271 uint64_t i, m_idx = 0;
272 uint16_t ops_unused = 0;
274 struct rte_crypto_op *ops[ctx->options->max_burst_size];
275 struct rte_crypto_op *ops_processed[ctx->options->max_burst_size];
277 uint32_t lcore = rte_lcore_id();
279 #ifdef CPERF_LINEARIZATION_ENABLE
280 struct rte_cryptodev_info dev_info;
283 /* Check if source mbufs require coalescing */
284 if (ctx->options->segment_sz < ctx->options->max_buffer_size) {
285 rte_cryptodev_info_get(ctx->dev_id, &dev_info);
286 if ((dev_info.feature_flags &
287 RTE_CRYPTODEV_FF_MBUF_SCATTER_GATHER) == 0)
290 #endif /* CPERF_LINEARIZATION_ENABLE */
292 ctx->lcore_id = lcore;
294 if (!ctx->options->csv)
295 printf("\n# Running verify test on device: %u, lcore: %u\n",
298 uint16_t iv_offset = sizeof(struct rte_crypto_op) +
299 sizeof(struct rte_crypto_sym_op);
301 while (ops_enqd_total < ctx->options->total_ops) {
303 uint16_t burst_size = ((ops_enqd_total + ctx->options->max_burst_size)
304 <= ctx->options->total_ops) ?
305 ctx->options->max_burst_size :
306 ctx->options->total_ops -
309 uint16_t ops_needed = burst_size - ops_unused;
311 /* Allocate crypto ops from pool */
312 if (ops_needed != rte_crypto_op_bulk_alloc(
314 RTE_CRYPTO_OP_TYPE_SYMMETRIC,
317 "Failed to allocate more crypto operations "
318 "from the the crypto operation pool.\n"
319 "Consider increasing the pool size "
324 /* Setup crypto op, attach mbuf etc */
325 (ctx->populate_ops)(ops, &ctx->mbufs_in[m_idx],
326 &ctx->mbufs_out[m_idx],
327 ops_needed, ctx->sess, ctx->options,
328 ctx->test_vector, iv_offset);
331 /* Populate the mbuf with the test vector, for verification */
332 for (i = 0; i < ops_needed; i++)
333 cperf_mbuf_set(ops[i]->sym->m_src,
337 #ifdef CPERF_LINEARIZATION_ENABLE
339 /* PMD doesn't support scatter-gather and source buffer
341 * We need to linearize it before enqueuing.
343 for (i = 0; i < burst_size; i++)
344 rte_pktmbuf_linearize(ops[i]->sym->m_src);
346 #endif /* CPERF_LINEARIZATION_ENABLE */
348 /* Enqueue burst of ops on crypto device */
349 ops_enqd = rte_cryptodev_enqueue_burst(ctx->dev_id, ctx->qp_id,
351 if (ops_enqd < burst_size)
355 * Calculate number of ops not enqueued (mainly for hw
356 * accelerators whose ingress queue can fill up).
358 ops_unused = burst_size - ops_enqd;
359 ops_enqd_total += ops_enqd;
362 /* Dequeue processed burst of ops from crypto device */
363 ops_deqd = rte_cryptodev_dequeue_burst(ctx->dev_id, ctx->qp_id,
364 ops_processed, ctx->options->max_burst_size);
367 if (m_idx + ctx->options->max_burst_size > ctx->options->pool_sz)
372 * Count dequeue polls which didn't return any
373 * processed operations. This statistic is mainly
374 * relevant to hw accelerators.
380 for (i = 0; i < ops_deqd; i++) {
381 if (cperf_verify_op(ops_processed[i], ctx->options,
384 /* free crypto ops so they can be reused. We don't free
385 * the mbufs here as we don't want to reuse them as
386 * the crypto operation will change the data and cause
389 rte_crypto_op_free(ops_processed[i]);
391 ops_deqd_total += ops_deqd;
394 /* Dequeue any operations still in the crypto device */
396 while (ops_deqd_total < ctx->options->total_ops) {
397 /* Sending 0 length burst to flush sw crypto device */
398 rte_cryptodev_enqueue_burst(ctx->dev_id, ctx->qp_id, NULL, 0);
401 ops_deqd = rte_cryptodev_dequeue_burst(ctx->dev_id, ctx->qp_id,
402 ops_processed, ctx->options->max_burst_size);
408 for (i = 0; i < ops_deqd; i++) {
409 if (cperf_verify_op(ops_processed[i], ctx->options,
412 /* free crypto ops so they can be reused. We don't free
413 * the mbufs here as we don't want to reuse them as
414 * the crypto operation will change the data and cause
417 rte_crypto_op_free(ops_processed[i]);
419 ops_deqd_total += ops_deqd;
422 if (!ctx->options->csv) {
424 printf("%12s%12s%12s%12s%12s%12s%12s%12s\n\n",
425 "lcore id", "Buf Size", "Burst size",
426 "Enqueued", "Dequeued", "Failed Enq",
427 "Failed Deq", "Failed Ops");
430 printf("%12u%12u%12u%12"PRIu64"%12"PRIu64"%12"PRIu64
431 "%12"PRIu64"%12"PRIu64"\n",
433 ctx->options->max_buffer_size,
434 ctx->options->max_burst_size,
442 printf("\n# lcore id, Buffer Size(B), "
443 "Burst Size,Enqueued,Dequeued,Failed Enq,"
444 "Failed Deq,Failed Ops\n");
447 printf("%10u;%10u;%u;%"PRIu64";%"PRIu64";%"PRIu64";%"PRIu64";"
450 ctx->options->max_buffer_size,
451 ctx->options->max_burst_size,
465 cperf_verify_test_destructor(void *arg)
467 struct cperf_verify_ctx *ctx = arg;
472 rte_cryptodev_stop(ctx->dev_id);
474 cperf_verify_test_free(ctx);