1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2016-2017 Intel Corporation
5 #include <rte_malloc.h>
6 #include <rte_cycles.h>
7 #include <rte_crypto.h>
8 #include <rte_cryptodev.h>
10 #include "cperf_test_verify.h"
11 #include "cperf_ops.h"
12 #include "cperf_test_common.h"
14 struct cperf_verify_ctx {
19 struct rte_mempool *pool;
21 struct rte_cryptodev_sym_session *sess;
23 cperf_populate_ops_t populate_ops;
25 uint32_t src_buf_offset;
26 uint32_t dst_buf_offset;
28 const struct cperf_options *options;
29 const struct cperf_test_vector *test_vector;
32 struct cperf_op_result {
33 enum rte_crypto_op_status status;
37 cperf_verify_test_free(struct cperf_verify_ctx *ctx)
41 rte_cryptodev_sym_session_clear(ctx->dev_id, ctx->sess);
42 rte_cryptodev_sym_session_free(ctx->sess);
45 rte_mempool_free(ctx->pool);
52 cperf_verify_test_constructor(struct rte_mempool *sess_mp,
53 struct rte_mempool *sess_priv_mp,
54 uint8_t dev_id, uint16_t qp_id,
55 const struct cperf_options *options,
56 const struct cperf_test_vector *test_vector,
57 const struct cperf_op_fns *op_fns)
59 struct cperf_verify_ctx *ctx = NULL;
61 ctx = rte_malloc(NULL, sizeof(struct cperf_verify_ctx), 0);
68 ctx->populate_ops = op_fns->populate_ops;
69 ctx->options = options;
70 ctx->test_vector = test_vector;
72 /* IV goes at the end of the crypto operation */
73 uint16_t iv_offset = sizeof(struct rte_crypto_op) +
74 sizeof(struct rte_crypto_sym_op);
76 ctx->sess = op_fns->sess_create(sess_mp, sess_priv_mp, dev_id, options,
77 test_vector, iv_offset);
78 if (ctx->sess == NULL)
81 if (cperf_alloc_common_memory(options, test_vector, dev_id, qp_id, 0,
82 &ctx->src_buf_offset, &ctx->dst_buf_offset,
88 cperf_verify_test_free(ctx);
94 cperf_verify_op(struct rte_crypto_op *op,
95 const struct cperf_options *options,
96 const struct cperf_test_vector *vector)
98 const struct rte_mbuf *m;
102 uint32_t cipher_offset, auth_offset;
103 uint8_t cipher, auth;
106 if (op->status != RTE_CRYPTO_OP_STATUS_SUCCESS)
113 nb_segs = m->nb_segs;
115 while (m && nb_segs != 0) {
121 data = rte_malloc(NULL, len, 0);
129 nb_segs = m->nb_segs;
131 while (m && nb_segs != 0) {
132 memcpy(data + len, rte_pktmbuf_mtod(m, uint8_t *),
139 switch (options->op_type) {
140 case CPERF_CIPHER_ONLY:
146 case CPERF_CIPHER_THEN_AUTH:
150 auth_offset = options->test_buffer_size;
152 case CPERF_AUTH_ONLY:
156 auth_offset = options->test_buffer_size;
158 case CPERF_AUTH_THEN_CIPHER:
162 auth_offset = options->test_buffer_size;
168 auth_offset = options->test_buffer_size;
176 if (options->cipher_op == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
177 res += memcmp(data + cipher_offset,
178 vector->ciphertext.data,
179 options->test_buffer_size);
181 res += memcmp(data + cipher_offset,
182 vector->plaintext.data,
183 options->test_buffer_size);
187 if (options->auth_op == RTE_CRYPTO_AUTH_OP_GENERATE)
188 res += memcmp(data + auth_offset,
199 cperf_mbuf_set(struct rte_mbuf *mbuf,
200 const struct cperf_options *options,
201 const struct cperf_test_vector *test_vector)
203 uint32_t segment_sz = options->segment_sz;
206 uint32_t remaining_bytes = options->max_buffer_size;
208 if (options->op_type == CPERF_AEAD) {
209 test_data = (options->aead_op == RTE_CRYPTO_AEAD_OP_ENCRYPT) ?
210 test_vector->plaintext.data :
211 test_vector->ciphertext.data;
214 (options->cipher_op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
215 test_vector->plaintext.data :
216 test_vector->ciphertext.data;
219 while (remaining_bytes) {
220 mbuf_data = rte_pktmbuf_mtod(mbuf, uint8_t *);
222 if (remaining_bytes <= segment_sz) {
223 memcpy(mbuf_data, test_data, remaining_bytes);
227 memcpy(mbuf_data, test_data, segment_sz);
228 remaining_bytes -= segment_sz;
229 test_data += segment_sz;
235 cperf_verify_test_runner(void *test_ctx)
237 struct cperf_verify_ctx *ctx = test_ctx;
239 uint64_t ops_enqd = 0, ops_enqd_total = 0, ops_enqd_failed = 0;
240 uint64_t ops_deqd = 0, ops_deqd_total = 0, ops_deqd_failed = 0;
241 uint64_t ops_failed = 0;
243 static uint16_t display_once;
246 uint16_t ops_unused = 0;
247 uint32_t imix_idx = 0;
249 struct rte_crypto_op *ops[ctx->options->max_burst_size];
250 struct rte_crypto_op *ops_processed[ctx->options->max_burst_size];
252 uint32_t lcore = rte_lcore_id();
254 #ifdef CPERF_LINEARIZATION_ENABLE
255 struct rte_cryptodev_info dev_info;
258 /* Check if source mbufs require coalescing */
259 if (ctx->options->segment_sz < ctx->options->max_buffer_size) {
260 rte_cryptodev_info_get(ctx->dev_id, &dev_info);
261 if ((dev_info.feature_flags &
262 RTE_CRYPTODEV_FF_MBUF_SCATTER_GATHER) == 0)
265 #endif /* CPERF_LINEARIZATION_ENABLE */
267 ctx->lcore_id = lcore;
269 if (!ctx->options->csv)
270 printf("\n# Running verify test on device: %u, lcore: %u\n",
273 uint16_t iv_offset = sizeof(struct rte_crypto_op) +
274 sizeof(struct rte_crypto_sym_op);
276 while (ops_enqd_total < ctx->options->total_ops) {
278 uint16_t burst_size = ((ops_enqd_total + ctx->options->max_burst_size)
279 <= ctx->options->total_ops) ?
280 ctx->options->max_burst_size :
281 ctx->options->total_ops -
284 uint16_t ops_needed = burst_size - ops_unused;
286 /* Allocate objects containing crypto operations and mbufs */
287 if (rte_mempool_get_bulk(ctx->pool, (void **)ops,
290 "Failed to allocate more crypto operations "
291 "from the crypto operation pool.\n"
292 "Consider increasing the pool size "
297 /* Setup crypto op, attach mbuf etc */
298 (ctx->populate_ops)(ops, ctx->src_buf_offset,
300 ops_needed, ctx->sess, ctx->options,
301 ctx->test_vector, iv_offset, &imix_idx, NULL);
304 /* Populate the mbuf with the test vector, for verification */
305 for (i = 0; i < ops_needed; i++)
306 cperf_mbuf_set(ops[i]->sym->m_src,
310 #ifdef CPERF_LINEARIZATION_ENABLE
312 /* PMD doesn't support scatter-gather and source buffer
314 * We need to linearize it before enqueuing.
316 for (i = 0; i < burst_size; i++)
317 rte_pktmbuf_linearize(ops[i]->sym->m_src);
319 #endif /* CPERF_LINEARIZATION_ENABLE */
321 /* Enqueue burst of ops on crypto device */
322 ops_enqd = rte_cryptodev_enqueue_burst(ctx->dev_id, ctx->qp_id,
324 if (ops_enqd < burst_size)
328 * Calculate number of ops not enqueued (mainly for hw
329 * accelerators whose ingress queue can fill up).
331 ops_unused = burst_size - ops_enqd;
332 ops_enqd_total += ops_enqd;
335 /* Dequeue processed burst of ops from crypto device */
336 ops_deqd = rte_cryptodev_dequeue_burst(ctx->dev_id, ctx->qp_id,
337 ops_processed, ctx->options->max_burst_size);
341 * Count dequeue polls which didn't return any
342 * processed operations. This statistic is mainly
343 * relevant to hw accelerators.
349 for (i = 0; i < ops_deqd; i++) {
350 if (cperf_verify_op(ops_processed[i], ctx->options,
354 /* Free crypto ops so they can be reused. */
355 rte_mempool_put_bulk(ctx->pool,
356 (void **)ops_processed, ops_deqd);
357 ops_deqd_total += ops_deqd;
360 /* Dequeue any operations still in the crypto device */
362 while (ops_deqd_total < ctx->options->total_ops) {
363 /* Sending 0 length burst to flush sw crypto device */
364 rte_cryptodev_enqueue_burst(ctx->dev_id, ctx->qp_id, NULL, 0);
367 ops_deqd = rte_cryptodev_dequeue_burst(ctx->dev_id, ctx->qp_id,
368 ops_processed, ctx->options->max_burst_size);
374 for (i = 0; i < ops_deqd; i++) {
375 if (cperf_verify_op(ops_processed[i], ctx->options,
379 /* Free crypto ops so they can be reused. */
380 rte_mempool_put_bulk(ctx->pool,
381 (void **)ops_processed, ops_deqd);
382 ops_deqd_total += ops_deqd;
386 if (!ctx->options->csv) {
387 if (__atomic_compare_exchange_n(&display_once, &exp, 1, 0,
388 __ATOMIC_RELAXED, __ATOMIC_RELAXED))
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");
394 printf("%12u%12u%12u%12"PRIu64"%12"PRIu64"%12"PRIu64
395 "%12"PRIu64"%12"PRIu64"\n",
397 ctx->options->max_buffer_size,
398 ctx->options->max_burst_size,
405 if (__atomic_compare_exchange_n(&display_once, &exp, 1, 0,
406 __ATOMIC_RELAXED, __ATOMIC_RELAXED))
407 printf("\n# lcore id, Buffer Size(B), "
408 "Burst Size,Enqueued,Dequeued,Failed Enq,"
409 "Failed Deq,Failed Ops\n");
411 printf("%10u,%10u,%u,%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64","
414 ctx->options->max_buffer_size,
415 ctx->options->max_burst_size,
429 cperf_verify_test_destructor(void *arg)
431 struct cperf_verify_ctx *ctx = arg;
436 cperf_verify_test_free(ctx);