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_latency_results {
62 struct cperf_op_result {
65 enum rte_crypto_op_status status;
68 struct cperf_latency_ctx {
73 struct rte_mempool *pkt_mbuf_pool_in;
74 struct rte_mempool *pkt_mbuf_pool_out;
75 struct rte_mbuf **mbufs_in;
76 struct rte_mbuf **mbufs_out;
78 struct rte_mempool *crypto_op_pool;
80 struct rte_cryptodev_sym_session *sess;
82 cperf_populate_ops_t populate_ops;
83 cperf_verify_crypto_op_t verify_op_output;
85 const struct cperf_options *options;
86 const struct cperf_test_vector *test_vector;
87 struct cperf_op_result *res;
88 struct cperf_latency_results results;
91 #define max(a, b) (a > b ? (uint64_t)a : (uint64_t)b)
92 #define min(a, b) (a < b ? (uint64_t)a : (uint64_t)b)
95 cperf_latency_test_free(struct cperf_latency_ctx *ctx, uint32_t mbuf_nb)
101 rte_cryptodev_sym_session_free(ctx->dev_id, ctx->sess);
104 for (i = 0; i < mbuf_nb; i++)
105 rte_pktmbuf_free(ctx->mbufs_in[i]);
107 rte_free(ctx->mbufs_in);
110 if (ctx->mbufs_out) {
111 for (i = 0; i < mbuf_nb; i++) {
112 if (ctx->mbufs_out[i] != NULL)
113 rte_pktmbuf_free(ctx->mbufs_out[i]);
116 rte_free(ctx->mbufs_out);
119 if (ctx->pkt_mbuf_pool_in)
120 rte_mempool_free(ctx->pkt_mbuf_pool_in);
122 if (ctx->pkt_mbuf_pool_out)
123 rte_mempool_free(ctx->pkt_mbuf_pool_out);
125 if (ctx->crypto_op_pool)
126 rte_mempool_free(ctx->crypto_op_pool);
133 static struct rte_mbuf *
134 cperf_mbuf_create(struct rte_mempool *mempool,
135 uint32_t segments_nb,
136 const struct cperf_options *options,
137 const struct cperf_test_vector *test_vector)
139 struct rte_mbuf *mbuf;
140 uint32_t segment_sz = options->buffer_sz / segments_nb;
141 uint32_t last_sz = options->buffer_sz % segments_nb;
144 (options->cipher_op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
145 test_vector->plaintext.data :
146 test_vector->ciphertext.data;
148 mbuf = rte_pktmbuf_alloc(mempool);
152 mbuf_data = (uint8_t *)rte_pktmbuf_append(mbuf, segment_sz);
153 if (mbuf_data == NULL)
156 memcpy(mbuf_data, test_data, segment_sz);
157 test_data += segment_sz;
160 while (segments_nb) {
163 m = rte_pktmbuf_alloc(mempool);
167 rte_pktmbuf_chain(mbuf, m);
169 mbuf_data = (uint8_t *)rte_pktmbuf_append(mbuf, segment_sz);
170 if (mbuf_data == NULL)
173 memcpy(mbuf_data, test_data, segment_sz);
174 test_data += segment_sz;
179 mbuf_data = (uint8_t *)rte_pktmbuf_append(mbuf, last_sz);
180 if (mbuf_data == NULL)
183 memcpy(mbuf_data, test_data, last_sz);
186 mbuf_data = (uint8_t *)rte_pktmbuf_append(mbuf,
187 options->auth_digest_sz);
188 if (mbuf_data == NULL)
191 if (options->op_type == CPERF_AEAD) {
192 uint8_t *aead = (uint8_t *)rte_pktmbuf_prepend(mbuf,
193 RTE_ALIGN_CEIL(options->auth_aad_sz, 16));
198 memcpy(aead, test_vector->aad.data, test_vector->aad.length);
204 rte_pktmbuf_free(mbuf);
210 cperf_latency_test_constructor(uint8_t dev_id, uint16_t qp_id,
211 const struct cperf_options *options,
212 const struct cperf_test_vector *test_vector,
213 const struct cperf_op_fns *op_fns)
215 struct cperf_latency_ctx *ctx = NULL;
216 unsigned int mbuf_idx = 0;
217 char pool_name[32] = "";
219 ctx = rte_malloc(NULL, sizeof(struct cperf_latency_ctx), 0);
223 ctx->dev_id = dev_id;
226 ctx->populate_ops = op_fns->populate_ops;
227 ctx->options = options;
228 ctx->test_vector = test_vector;
230 ctx->sess = op_fns->sess_create(dev_id, options, test_vector);
231 if (ctx->sess == NULL)
234 snprintf(pool_name, sizeof(pool_name), "cperf_pool_in_cdev_%d",
237 ctx->pkt_mbuf_pool_in = rte_pktmbuf_pool_create(pool_name,
238 options->pool_sz * options->segments_nb, 0, 0,
239 RTE_PKTMBUF_HEADROOM +
240 RTE_CACHE_LINE_ROUNDUP(
241 (options->buffer_sz / options->segments_nb) +
242 (options->buffer_sz % options->segments_nb) +
243 options->auth_digest_sz),
246 if (ctx->pkt_mbuf_pool_in == NULL)
249 /* Generate mbufs_in with plaintext populated for test */
250 if (ctx->options->pool_sz % ctx->options->burst_sz)
253 ctx->mbufs_in = rte_malloc(NULL,
254 (sizeof(struct rte_mbuf *) *
255 ctx->options->pool_sz), 0);
257 for (mbuf_idx = 0; mbuf_idx < options->pool_sz; mbuf_idx++) {
258 ctx->mbufs_in[mbuf_idx] = cperf_mbuf_create(
259 ctx->pkt_mbuf_pool_in, options->segments_nb,
260 options, test_vector);
261 if (ctx->mbufs_in[mbuf_idx] == NULL)
265 if (options->out_of_place == 1) {
267 snprintf(pool_name, sizeof(pool_name),
268 "cperf_pool_out_cdev_%d",
271 ctx->pkt_mbuf_pool_out = rte_pktmbuf_pool_create(
272 pool_name, options->pool_sz, 0, 0,
273 RTE_PKTMBUF_HEADROOM +
274 RTE_CACHE_LINE_ROUNDUP(
276 options->auth_digest_sz),
279 if (ctx->pkt_mbuf_pool_out == NULL)
283 ctx->mbufs_out = rte_malloc(NULL,
284 (sizeof(struct rte_mbuf *) *
285 ctx->options->pool_sz), 0);
287 for (mbuf_idx = 0; mbuf_idx < options->pool_sz; mbuf_idx++) {
288 if (options->out_of_place == 1) {
289 ctx->mbufs_out[mbuf_idx] = cperf_mbuf_create(
290 ctx->pkt_mbuf_pool_out, 1,
291 options, test_vector);
292 if (ctx->mbufs_out[mbuf_idx] == NULL)
295 ctx->mbufs_out[mbuf_idx] = NULL;
299 snprintf(pool_name, sizeof(pool_name), "cperf_op_pool_cdev_%d",
302 ctx->crypto_op_pool = rte_crypto_op_pool_create(pool_name,
303 RTE_CRYPTO_OP_TYPE_SYMMETRIC, options->pool_sz, 0, 0,
305 if (ctx->crypto_op_pool == NULL)
308 ctx->res = rte_malloc(NULL, sizeof(struct cperf_op_result) *
309 ctx->options->total_ops, 0);
311 if (ctx->res == NULL)
316 cperf_latency_test_free(ctx, mbuf_idx);
322 cperf_latency_test_verifier(struct rte_mbuf *mbuf,
323 const struct cperf_options *options,
324 const struct cperf_test_vector *vector)
326 const struct rte_mbuf *m;
330 uint32_t cipher_offset, auth_offset;
331 uint8_t cipher, auth;
335 nb_segs = m->nb_segs;
337 while (m && nb_segs != 0) {
343 data = rte_malloc(NULL, len, 0);
348 nb_segs = m->nb_segs;
350 while (m && nb_segs != 0) {
351 memcpy(data + len, rte_pktmbuf_mtod(m, uint8_t *),
358 switch (options->op_type) {
359 case CPERF_CIPHER_ONLY:
365 case CPERF_CIPHER_THEN_AUTH:
369 auth_offset = vector->plaintext.length;
371 case CPERF_AUTH_ONLY:
375 auth_offset = vector->plaintext.length;
377 case CPERF_AUTH_THEN_CIPHER:
381 auth_offset = vector->plaintext.length;
385 cipher_offset = vector->aad.length;
387 auth_offset = vector->aad.length + vector->plaintext.length;
392 if (options->cipher_op == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
393 res += memcmp(data + cipher_offset,
394 vector->ciphertext.data,
395 vector->ciphertext.length);
397 res += memcmp(data + cipher_offset,
398 vector->plaintext.data,
399 vector->plaintext.length);
403 if (options->auth_op == RTE_CRYPTO_AUTH_OP_GENERATE)
404 res += memcmp(data + auth_offset,
406 vector->digest.length);
416 cperf_latency_test_runner(void *arg)
418 struct cperf_latency_ctx *ctx = arg;
419 struct cperf_op_result *pres;
424 struct rte_crypto_op *ops[ctx->options->burst_sz];
425 struct rte_crypto_op *ops_processed[ctx->options->burst_sz];
426 uint64_t ops_enqd = 0, ops_deqd = 0;
427 uint64_t m_idx = 0, b_idx = 0, i;
429 uint64_t tsc_val, tsc_end, tsc_start;
430 uint64_t tsc_max = 0, tsc_min = ~0UL, tsc_tot = 0, tsc_idx = 0;
431 uint64_t enqd_max = 0, enqd_min = ~0UL, enqd_tot = 0;
432 uint64_t deqd_max = 0, deqd_min = ~0UL, deqd_tot = 0;
434 uint32_t lcore = rte_lcore_id();
436 #ifdef CPERF_LINEARIZATION_ENABLE
437 struct rte_cryptodev_info dev_info;
440 /* Check if source mbufs require coalescing */
441 if (ctx->options->segments_nb > 1) {
442 rte_cryptodev_info_get(ctx->dev_id, &dev_info);
443 if ((dev_info.feature_flags &
444 RTE_CRYPTODEV_FF_MBUF_SCATTER_GATHER) == 0)
447 #endif /* CPERF_LINEARIZATION_ENABLE */
449 ctx->lcore_id = lcore;
451 /* Warm up the host CPU before starting the test */
452 for (i = 0; i < ctx->options->total_ops; i++)
453 rte_cryptodev_enqueue_burst(ctx->dev_id, ctx->qp_id, NULL, 0);
455 while (enqd_tot < ctx->options->total_ops) {
457 uint16_t burst_size = ((enqd_tot + ctx->options->burst_sz)
458 <= ctx->options->total_ops) ?
459 ctx->options->burst_sz :
460 ctx->options->total_ops -
463 /* Allocate crypto ops from pool */
464 if (burst_size != rte_crypto_op_bulk_alloc(
466 RTE_CRYPTO_OP_TYPE_SYMMETRIC,
470 /* Setup crypto op, attach mbuf etc */
471 (ctx->populate_ops)(ops, &ctx->mbufs_in[m_idx],
472 &ctx->mbufs_out[m_idx],
473 burst_size, ctx->sess, ctx->options,
476 tsc_start = rte_rdtsc_precise();
478 #ifdef CPERF_LINEARIZATION_ENABLE
480 /* PMD doesn't support scatter-gather and source buffer
482 * We need to linearize it before enqueuing.
484 for (i = 0; i < burst_size; i++)
485 rte_pktmbuf_linearize(ops[i]->sym->m_src);
487 #endif /* CPERF_LINEARIZATION_ENABLE */
489 /* Enqueue burst of ops on crypto device */
490 ops_enqd = rte_cryptodev_enqueue_burst(ctx->dev_id, ctx->qp_id,
493 /* Dequeue processed burst of ops from crypto device */
494 ops_deqd = rte_cryptodev_dequeue_burst(ctx->dev_id, ctx->qp_id,
495 ops_processed, ctx->options->burst_sz);
497 tsc_end = rte_rdtsc_precise();
499 for (i = 0; i < ops_enqd; i++) {
500 ctx->res[tsc_idx].tsc_start = tsc_start;
501 ops[i]->opaque_data = (void *)&ctx->res[tsc_idx];
505 /* Free memory for not enqueued operations */
506 for (i = ops_enqd; i < burst_size; i++)
507 rte_crypto_op_free(ops[i]);
509 if (likely(ops_deqd)) {
511 * free crypto ops so they can be reused. We don't free
512 * the mbufs here as we don't want to reuse them as
513 * the crypto operation will change the data and cause
516 for (i = 0; i < ops_deqd; i++) {
517 pres = (struct cperf_op_result *)
518 (ops_processed[i]->opaque_data);
519 pres->status = ops_processed[i]->status;
520 pres->tsc_end = tsc_end;
522 rte_crypto_op_free(ops_processed[i]);
525 deqd_tot += ops_deqd;
526 deqd_max = max(ops_deqd, deqd_max);
527 deqd_min = min(ops_deqd, deqd_min);
530 enqd_tot += ops_enqd;
531 enqd_max = max(ops_enqd, enqd_max);
532 enqd_min = min(ops_enqd, enqd_min);
535 m_idx = m_idx + ctx->options->burst_sz > ctx->options->pool_sz ?
540 /* Dequeue any operations still in the crypto device */
541 while (deqd_tot < ctx->options->total_ops) {
542 /* Sending 0 length burst to flush sw crypto device */
543 rte_cryptodev_enqueue_burst(ctx->dev_id, ctx->qp_id, NULL, 0);
546 ops_deqd = rte_cryptodev_dequeue_burst(ctx->dev_id, ctx->qp_id,
547 ops_processed, ctx->options->burst_sz);
549 tsc_end = rte_rdtsc_precise();
552 for (i = 0; i < ops_deqd; i++) {
553 pres = (struct cperf_op_result *)
554 (ops_processed[i]->opaque_data);
555 pres->status = ops_processed[i]->status;
556 pres->tsc_end = tsc_end;
558 rte_crypto_op_free(ops_processed[i]);
561 deqd_tot += ops_deqd;
562 deqd_max = max(ops_deqd, deqd_max);
563 deqd_min = min(ops_deqd, deqd_min);
567 for (i = 0; i < tsc_idx; i++) {
568 tsc_val = ctx->res[i].tsc_end - ctx->res[i].tsc_start;
569 tsc_max = max(tsc_val, tsc_max);
570 tsc_min = min(tsc_val, tsc_min);
574 if (ctx->options->verify) {
575 struct rte_mbuf **mbufs;
577 if (ctx->options->out_of_place == 1)
578 mbufs = ctx->mbufs_out;
580 mbufs = ctx->mbufs_in;
582 for (i = 0; i < ctx->options->total_ops; i++) {
584 if (ctx->res[i].status != RTE_CRYPTO_OP_STATUS_SUCCESS
585 || cperf_latency_test_verifier(mbufs[i],
589 ctx->results.ops_failed++;
594 ctx->results.enqd_tot = enqd_tot;
595 ctx->results.enqd_max = enqd_max;
596 ctx->results.enqd_min = enqd_min;
598 ctx->results.deqd_tot = deqd_tot;
599 ctx->results.deqd_max = deqd_max;
600 ctx->results.deqd_min = deqd_min;
602 ctx->results.cycles_tot = tsc_tot;
603 ctx->results.cycles_max = tsc_max;
604 ctx->results.cycles_min = tsc_min;
606 ctx->results.burst_num = b_idx;
607 ctx->results.num = tsc_idx;
613 cperf_latency_test_destructor(void *arg)
615 struct cperf_latency_ctx *ctx = arg;
619 static int only_once;
620 uint64_t etot, eavg, emax, emin;
621 uint64_t dtot, davg, dmax, dmin;
622 uint64_t ctot, cavg, cmax, cmin;
623 double ttot, tavg, tmax, tmin;
625 const uint64_t tunit = 1000000; /* us */
626 const uint64_t tsc_hz = rte_get_tsc_hz();
628 etot = ctx->results.enqd_tot;
629 eavg = ctx->results.enqd_tot / ctx->results.burst_num;
630 emax = ctx->results.enqd_max;
631 emin = ctx->results.enqd_min;
633 dtot = ctx->results.deqd_tot;
634 davg = ctx->results.deqd_tot / ctx->results.burst_num;
635 dmax = ctx->results.deqd_max;
636 dmin = ctx->results.deqd_min;
638 ctot = ctx->results.cycles_tot;
639 cavg = ctx->results.cycles_tot / ctx->results.num;
640 cmax = ctx->results.cycles_max;
641 cmin = ctx->results.cycles_min;
643 ttot = tunit*(double)(ctot) / tsc_hz;
644 tavg = tunit*(double)(cavg) / tsc_hz;
645 tmax = tunit*(double)(cmax) / tsc_hz;
646 tmin = tunit*(double)(cmin) / tsc_hz;
648 if (ctx->options->csv) {
650 printf("\n# lcore, Pakt Seq #, Packet Size, cycles,"
653 for (i = 0; i < ctx->options->total_ops; i++) {
655 printf("\n%u;%"PRIu64";%"PRIu64";%.3f",
656 ctx->lcore_id, i + 1,
657 ctx->res[i].tsc_end - ctx->res[i].tsc_start,
658 tunit * (double) (ctx->res[i].tsc_end
659 - ctx->res[i].tsc_start)
665 printf("\n# Device %d on lcore %u\n", ctx->dev_id,
667 printf("\n# total operations: %u", ctx->options->total_ops);
668 printf("\n# verified failed: %"PRIu64,
669 ctx->results.ops_failed);
670 printf("\n# burst number: %"PRIu64,
671 ctx->results.burst_num);
673 printf("\n# \t Total\t Average\t Maximum\t "
675 printf("\n# enqueued\t%12"PRIu64"\t%10"PRIu64"\t%10"PRIu64"\t"
676 "%10"PRIu64, etot, eavg, emax, emin);
677 printf("\n# dequeued\t%12"PRIu64"\t%10"PRIu64"\t%10"PRIu64"\t"
678 "%10"PRIu64, dtot, davg, dmax, dmin);
679 printf("\n# cycles\t%12"PRIu64"\t%10"PRIu64"\t%10"PRIu64"\t"
680 "%10"PRIu64, ctot, cavg, cmax, cmin);
681 printf("\n# time [us]\t%12.0f\t%10.3f\t%10.3f\t%10.3f", ttot,
686 cperf_latency_test_free(ctx, ctx->options->pool_sz);