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_latency.h"
39 #include "cperf_ops.h"
42 struct cperf_op_result {
45 enum rte_crypto_op_status status;
48 struct cperf_latency_ctx {
53 struct rte_mempool *pkt_mbuf_pool_in;
54 struct rte_mempool *pkt_mbuf_pool_out;
55 struct rte_mbuf **mbufs_in;
56 struct rte_mbuf **mbufs_out;
58 struct rte_mempool *crypto_op_pool;
60 struct rte_cryptodev_sym_session *sess;
62 cperf_populate_ops_t populate_ops;
64 const struct cperf_options *options;
65 const struct cperf_test_vector *test_vector;
66 struct cperf_op_result *res;
69 #define max(a, b) (a > b ? (uint64_t)a : (uint64_t)b)
70 #define min(a, b) (a < b ? (uint64_t)a : (uint64_t)b)
73 cperf_latency_test_free(struct cperf_latency_ctx *ctx, uint32_t mbuf_nb)
79 rte_cryptodev_sym_session_free(ctx->dev_id, ctx->sess);
82 for (i = 0; i < mbuf_nb; i++)
83 rte_pktmbuf_free(ctx->mbufs_in[i]);
85 rte_free(ctx->mbufs_in);
89 for (i = 0; i < mbuf_nb; i++) {
90 if (ctx->mbufs_out[i] != NULL)
91 rte_pktmbuf_free(ctx->mbufs_out[i]);
94 rte_free(ctx->mbufs_out);
97 if (ctx->pkt_mbuf_pool_in)
98 rte_mempool_free(ctx->pkt_mbuf_pool_in);
100 if (ctx->pkt_mbuf_pool_out)
101 rte_mempool_free(ctx->pkt_mbuf_pool_out);
103 if (ctx->crypto_op_pool)
104 rte_mempool_free(ctx->crypto_op_pool);
111 static struct rte_mbuf *
112 cperf_mbuf_create(struct rte_mempool *mempool,
113 uint32_t segments_nb,
114 const struct cperf_options *options,
115 const struct cperf_test_vector *test_vector)
117 struct rte_mbuf *mbuf;
118 uint32_t segment_sz = options->buffer_sz / segments_nb;
119 uint32_t last_sz = options->buffer_sz % segments_nb;
122 (options->cipher_op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
123 test_vector->plaintext.data :
124 test_vector->ciphertext.data;
126 mbuf = rte_pktmbuf_alloc(mempool);
130 mbuf_data = (uint8_t *)rte_pktmbuf_append(mbuf, segment_sz);
131 if (mbuf_data == NULL)
134 memcpy(mbuf_data, test_data, segment_sz);
135 test_data += segment_sz;
138 while (segments_nb) {
141 m = rte_pktmbuf_alloc(mempool);
145 rte_pktmbuf_chain(mbuf, m);
147 mbuf_data = (uint8_t *)rte_pktmbuf_append(mbuf, segment_sz);
148 if (mbuf_data == NULL)
151 memcpy(mbuf_data, test_data, segment_sz);
152 test_data += segment_sz;
157 mbuf_data = (uint8_t *)rte_pktmbuf_append(mbuf, last_sz);
158 if (mbuf_data == NULL)
161 memcpy(mbuf_data, test_data, last_sz);
164 if (options->op_type != CPERF_CIPHER_ONLY) {
165 mbuf_data = (uint8_t *)rte_pktmbuf_append(mbuf,
166 options->auth_digest_sz);
167 if (mbuf_data == NULL)
171 if (options->op_type == CPERF_AEAD) {
172 uint8_t *aead = (uint8_t *)rte_pktmbuf_prepend(mbuf,
173 RTE_ALIGN_CEIL(options->auth_aad_sz, 16));
178 memcpy(aead, test_vector->aad.data, test_vector->aad.length);
184 rte_pktmbuf_free(mbuf);
190 cperf_latency_test_constructor(uint8_t dev_id, uint16_t qp_id,
191 const struct cperf_options *options,
192 const struct cperf_test_vector *test_vector,
193 const struct cperf_op_fns *op_fns)
195 struct cperf_latency_ctx *ctx = NULL;
196 unsigned int mbuf_idx = 0;
197 char pool_name[32] = "";
199 ctx = rte_malloc(NULL, sizeof(struct cperf_latency_ctx), 0);
203 ctx->dev_id = dev_id;
206 ctx->populate_ops = op_fns->populate_ops;
207 ctx->options = options;
208 ctx->test_vector = test_vector;
210 ctx->sess = op_fns->sess_create(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->buffer_sz / options->segments_nb) +
222 (options->buffer_sz % options->segments_nb) +
223 options->auth_digest_sz),
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 *) *
232 ctx->options->pool_sz), 0);
234 for (mbuf_idx = 0; mbuf_idx < options->pool_sz; mbuf_idx++) {
235 ctx->mbufs_in[mbuf_idx] = cperf_mbuf_create(
236 ctx->pkt_mbuf_pool_in, options->segments_nb,
237 options, test_vector);
238 if (ctx->mbufs_in[mbuf_idx] == NULL)
242 if (options->out_of_place == 1) {
244 snprintf(pool_name, sizeof(pool_name),
245 "cperf_pool_out_cdev_%d",
248 ctx->pkt_mbuf_pool_out = rte_pktmbuf_pool_create(
249 pool_name, options->pool_sz, 0, 0,
250 RTE_PKTMBUF_HEADROOM +
251 RTE_CACHE_LINE_ROUNDUP(
253 options->auth_digest_sz),
256 if (ctx->pkt_mbuf_pool_out == NULL)
260 ctx->mbufs_out = rte_malloc(NULL,
261 (sizeof(struct rte_mbuf *) *
262 ctx->options->pool_sz), 0);
264 for (mbuf_idx = 0; mbuf_idx < options->pool_sz; mbuf_idx++) {
265 if (options->out_of_place == 1) {
266 ctx->mbufs_out[mbuf_idx] = cperf_mbuf_create(
267 ctx->pkt_mbuf_pool_out, 1,
268 options, test_vector);
269 if (ctx->mbufs_out[mbuf_idx] == NULL)
272 ctx->mbufs_out[mbuf_idx] = NULL;
276 snprintf(pool_name, sizeof(pool_name), "cperf_op_pool_cdev_%d",
279 ctx->crypto_op_pool = rte_crypto_op_pool_create(pool_name,
280 RTE_CRYPTO_OP_TYPE_SYMMETRIC, options->pool_sz, 0, 0,
282 if (ctx->crypto_op_pool == NULL)
285 ctx->res = rte_malloc(NULL, sizeof(struct cperf_op_result) *
286 ctx->options->total_ops, 0);
288 if (ctx->res == NULL)
293 cperf_latency_test_free(ctx, mbuf_idx);
299 cperf_latency_test_runner(void *arg)
301 struct cperf_latency_ctx *ctx = arg;
302 struct cperf_op_result *pres;
304 static int only_once;
309 struct rte_crypto_op *ops[ctx->options->burst_sz];
310 struct rte_crypto_op *ops_processed[ctx->options->burst_sz];
313 uint32_t lcore = rte_lcore_id();
315 #ifdef CPERF_LINEARIZATION_ENABLE
316 struct rte_cryptodev_info dev_info;
319 /* Check if source mbufs require coalescing */
320 if (ctx->options->segments_nb > 1) {
321 rte_cryptodev_info_get(ctx->dev_id, &dev_info);
322 if ((dev_info.feature_flags &
323 RTE_CRYPTODEV_FF_MBUF_SCATTER_GATHER) == 0)
326 #endif /* CPERF_LINEARIZATION_ENABLE */
328 ctx->lcore_id = lcore;
330 /* Warm up the host CPU before starting the test */
331 for (i = 0; i < ctx->options->total_ops; i++)
332 rte_cryptodev_enqueue_burst(ctx->dev_id, ctx->qp_id, NULL, 0);
334 uint64_t ops_enqd = 0, ops_deqd = 0;
335 uint64_t m_idx = 0, b_idx = 0;
337 uint64_t tsc_val, tsc_end, tsc_start;
338 uint64_t tsc_max = 0, tsc_min = ~0UL, tsc_tot = 0, tsc_idx = 0;
339 uint64_t enqd_max = 0, enqd_min = ~0UL, enqd_tot = 0;
340 uint64_t deqd_max = 0, deqd_min = ~0UL, deqd_tot = 0;
342 while (enqd_tot < ctx->options->total_ops) {
343 uint16_t burst_size = ((enqd_tot + ctx->options->burst_sz)
344 <= ctx->options->total_ops) ?
345 ctx->options->burst_sz :
346 ctx->options->total_ops -
349 /* Allocate crypto ops from pool */
350 if (burst_size != rte_crypto_op_bulk_alloc(
352 RTE_CRYPTO_OP_TYPE_SYMMETRIC,
356 /* Setup crypto op, attach mbuf etc */
357 (ctx->populate_ops)(ops, &ctx->mbufs_in[m_idx],
358 &ctx->mbufs_out[m_idx],
359 burst_size, ctx->sess, ctx->options,
362 tsc_start = rte_rdtsc_precise();
364 #ifdef CPERF_LINEARIZATION_ENABLE
366 /* PMD doesn't support scatter-gather and source buffer
368 * We need to linearize it before enqueuing.
370 for (i = 0; i < burst_size; i++)
371 rte_pktmbuf_linearize(ops[i]->sym->m_src);
373 #endif /* CPERF_LINEARIZATION_ENABLE */
375 /* Enqueue burst of ops on crypto device */
376 ops_enqd = rte_cryptodev_enqueue_burst(ctx->dev_id, ctx->qp_id,
379 /* Dequeue processed burst of ops from crypto device */
380 ops_deqd = rte_cryptodev_dequeue_burst(ctx->dev_id, ctx->qp_id,
381 ops_processed, ctx->options->burst_sz);
383 tsc_end = rte_rdtsc_precise();
385 for (i = 0; i < ops_enqd; i++) {
386 ctx->res[tsc_idx].tsc_start = tsc_start;
387 ops[i]->opaque_data = (void *)&ctx->res[tsc_idx];
391 /* Free memory for not enqueued operations */
392 for (i = ops_enqd; i < burst_size; i++)
393 rte_crypto_op_free(ops[i]);
395 if (likely(ops_deqd)) {
397 * free crypto ops so they can be reused. We don't free
398 * the mbufs here as we don't want to reuse them as
399 * the crypto operation will change the data and cause
402 for (i = 0; i < ops_deqd; i++) {
403 pres = (struct cperf_op_result *)
404 (ops_processed[i]->opaque_data);
405 pres->status = ops_processed[i]->status;
406 pres->tsc_end = tsc_end;
408 rte_crypto_op_free(ops_processed[i]);
411 deqd_tot += ops_deqd;
412 deqd_max = max(ops_deqd, deqd_max);
413 deqd_min = min(ops_deqd, deqd_min);
416 enqd_tot += ops_enqd;
417 enqd_max = max(ops_enqd, enqd_max);
418 enqd_min = min(ops_enqd, enqd_min);
421 m_idx = m_idx + ctx->options->burst_sz > ctx->options->pool_sz ?
426 /* Dequeue any operations still in the crypto device */
427 while (deqd_tot < ctx->options->total_ops) {
428 /* Sending 0 length burst to flush sw crypto device */
429 rte_cryptodev_enqueue_burst(ctx->dev_id, ctx->qp_id, NULL, 0);
432 ops_deqd = rte_cryptodev_dequeue_burst(ctx->dev_id, ctx->qp_id,
433 ops_processed, ctx->options->burst_sz);
435 tsc_end = rte_rdtsc_precise();
438 for (i = 0; i < ops_deqd; i++) {
439 pres = (struct cperf_op_result *)
440 (ops_processed[i]->opaque_data);
441 pres->status = ops_processed[i]->status;
442 pres->tsc_end = tsc_end;
444 rte_crypto_op_free(ops_processed[i]);
447 deqd_tot += ops_deqd;
448 deqd_max = max(ops_deqd, deqd_max);
449 deqd_min = min(ops_deqd, deqd_min);
453 for (i = 0; i < tsc_idx; i++) {
454 tsc_val = ctx->res[i].tsc_end - ctx->res[i].tsc_start;
455 tsc_max = max(tsc_val, tsc_max);
456 tsc_min = min(tsc_val, tsc_min);
460 double time_tot, time_avg, time_max, time_min;
462 const uint64_t tunit = 1000000; /* us */
463 const uint64_t tsc_hz = rte_get_tsc_hz();
465 uint64_t enqd_avg = enqd_tot / b_idx;
466 uint64_t deqd_avg = deqd_tot / b_idx;
467 uint64_t tsc_avg = tsc_tot / tsc_idx;
469 time_tot = tunit*(double)(tsc_tot) / tsc_hz;
470 time_avg = tunit*(double)(tsc_avg) / tsc_hz;
471 time_max = tunit*(double)(tsc_max) / tsc_hz;
472 time_min = tunit*(double)(tsc_min) / tsc_hz;
474 if (ctx->options->csv) {
476 printf("\n# lcore, Buffer Size, Burst Size, Pakt Seq #, "
477 "Packet Size, cycles, time (us)");
479 for (i = 0; i < ctx->options->total_ops; i++) {
481 printf("\n%u;%u;%u;%"PRIu64";%"PRIu64";%.3f",
482 ctx->lcore_id, ctx->options->buffer_sz,
483 ctx->options->burst_sz, i + 1,
484 ctx->res[i].tsc_end - ctx->res[i].tsc_start,
485 tunit * (double) (ctx->res[i].tsc_end
486 - ctx->res[i].tsc_start)
492 printf("\n# Device %d on lcore %u\n", ctx->dev_id,
494 printf("\n# total operations: %u", ctx->options->total_ops);
495 printf("\n# Buffer size: %u", ctx->options->buffer_sz);
496 printf("\n# Burst size: %u", ctx->options->burst_sz);
497 printf("\n# Number of bursts: %"PRIu64,
501 printf("\n# \t Total\t Average\t "
502 "Maximum\t Minimum");
503 printf("\n# enqueued\t%12"PRIu64"\t%10"PRIu64"\t"
504 "%10"PRIu64"\t%10"PRIu64, enqd_tot,
505 enqd_avg, enqd_max, enqd_min);
506 printf("\n# dequeued\t%12"PRIu64"\t%10"PRIu64"\t"
507 "%10"PRIu64"\t%10"PRIu64, deqd_tot,
508 deqd_avg, deqd_max, deqd_min);
509 printf("\n# cycles\t%12"PRIu64"\t%10"PRIu64"\t"
510 "%10"PRIu64"\t%10"PRIu64, tsc_tot,
511 tsc_avg, tsc_max, tsc_min);
512 printf("\n# time [us]\t%12.0f\t%10.3f\t%10.3f\t%10.3f",
513 time_tot, time_avg, time_max, time_min);
522 cperf_latency_test_destructor(void *arg)
524 struct cperf_latency_ctx *ctx = arg;
529 cperf_latency_test_free(ctx, ctx->options->pool_sz);