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;
84 const struct cperf_options *options;
85 const struct cperf_test_vector *test_vector;
86 struct cperf_op_result *res;
87 struct cperf_latency_results results;
90 #define max(a, b) (a > b ? (uint64_t)a : (uint64_t)b)
91 #define min(a, b) (a < b ? (uint64_t)a : (uint64_t)b)
94 cperf_latency_test_free(struct cperf_latency_ctx *ctx, uint32_t mbuf_nb)
100 rte_cryptodev_sym_session_free(ctx->dev_id, ctx->sess);
103 for (i = 0; i < mbuf_nb; i++)
104 rte_pktmbuf_free(ctx->mbufs_in[i]);
106 rte_free(ctx->mbufs_in);
109 if (ctx->mbufs_out) {
110 for (i = 0; i < mbuf_nb; i++) {
111 if (ctx->mbufs_out[i] != NULL)
112 rte_pktmbuf_free(ctx->mbufs_out[i]);
115 rte_free(ctx->mbufs_out);
118 if (ctx->pkt_mbuf_pool_in)
119 rte_mempool_free(ctx->pkt_mbuf_pool_in);
121 if (ctx->pkt_mbuf_pool_out)
122 rte_mempool_free(ctx->pkt_mbuf_pool_out);
124 if (ctx->crypto_op_pool)
125 rte_mempool_free(ctx->crypto_op_pool);
132 static struct rte_mbuf *
133 cperf_mbuf_create(struct rte_mempool *mempool,
134 uint32_t segments_nb,
135 const struct cperf_options *options,
136 const struct cperf_test_vector *test_vector)
138 struct rte_mbuf *mbuf;
139 uint32_t segment_sz = options->buffer_sz / segments_nb;
140 uint32_t last_sz = options->buffer_sz % segments_nb;
143 (options->cipher_op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
144 test_vector->plaintext.data :
145 test_vector->ciphertext.data;
147 mbuf = rte_pktmbuf_alloc(mempool);
151 mbuf_data = (uint8_t *)rte_pktmbuf_append(mbuf, segment_sz);
152 if (mbuf_data == NULL)
155 memcpy(mbuf_data, test_data, segment_sz);
156 test_data += segment_sz;
159 while (segments_nb) {
162 m = rte_pktmbuf_alloc(mempool);
166 rte_pktmbuf_chain(mbuf, m);
168 mbuf_data = (uint8_t *)rte_pktmbuf_append(mbuf, segment_sz);
169 if (mbuf_data == NULL)
172 memcpy(mbuf_data, test_data, segment_sz);
173 test_data += segment_sz;
178 mbuf_data = (uint8_t *)rte_pktmbuf_append(mbuf, last_sz);
179 if (mbuf_data == NULL)
182 memcpy(mbuf_data, test_data, last_sz);
185 if (options->op_type != CPERF_CIPHER_ONLY) {
186 mbuf_data = (uint8_t *)rte_pktmbuf_append(mbuf,
187 options->auth_digest_sz);
188 if (mbuf_data == NULL)
192 if (options->op_type == CPERF_AEAD) {
193 uint8_t *aead = (uint8_t *)rte_pktmbuf_prepend(mbuf,
194 RTE_ALIGN_CEIL(options->auth_aad_sz, 16));
199 memcpy(aead, test_vector->aad.data, test_vector->aad.length);
205 rte_pktmbuf_free(mbuf);
211 cperf_latency_test_constructor(uint8_t dev_id, uint16_t qp_id,
212 const struct cperf_options *options,
213 const struct cperf_test_vector *test_vector,
214 const struct cperf_op_fns *op_fns)
216 struct cperf_latency_ctx *ctx = NULL;
217 unsigned int mbuf_idx = 0;
218 char pool_name[32] = "";
220 ctx = rte_malloc(NULL, sizeof(struct cperf_latency_ctx), 0);
224 ctx->dev_id = dev_id;
227 ctx->populate_ops = op_fns->populate_ops;
228 ctx->options = options;
229 ctx->test_vector = test_vector;
231 ctx->sess = op_fns->sess_create(dev_id, options, test_vector);
232 if (ctx->sess == NULL)
235 snprintf(pool_name, sizeof(pool_name), "cperf_pool_in_cdev_%d",
238 ctx->pkt_mbuf_pool_in = rte_pktmbuf_pool_create(pool_name,
239 options->pool_sz * options->segments_nb, 0, 0,
240 RTE_PKTMBUF_HEADROOM +
241 RTE_CACHE_LINE_ROUNDUP(
242 (options->buffer_sz / options->segments_nb) +
243 (options->buffer_sz % options->segments_nb) +
244 options->auth_digest_sz),
247 if (ctx->pkt_mbuf_pool_in == NULL)
250 /* Generate mbufs_in with plaintext populated for test */
251 if (ctx->options->pool_sz % ctx->options->burst_sz)
254 ctx->mbufs_in = rte_malloc(NULL,
255 (sizeof(struct rte_mbuf *) *
256 ctx->options->pool_sz), 0);
258 for (mbuf_idx = 0; mbuf_idx < options->pool_sz; mbuf_idx++) {
259 ctx->mbufs_in[mbuf_idx] = cperf_mbuf_create(
260 ctx->pkt_mbuf_pool_in, options->segments_nb,
261 options, test_vector);
262 if (ctx->mbufs_in[mbuf_idx] == NULL)
266 if (options->out_of_place == 1) {
268 snprintf(pool_name, sizeof(pool_name),
269 "cperf_pool_out_cdev_%d",
272 ctx->pkt_mbuf_pool_out = rte_pktmbuf_pool_create(
273 pool_name, options->pool_sz, 0, 0,
274 RTE_PKTMBUF_HEADROOM +
275 RTE_CACHE_LINE_ROUNDUP(
277 options->auth_digest_sz),
280 if (ctx->pkt_mbuf_pool_out == NULL)
284 ctx->mbufs_out = rte_malloc(NULL,
285 (sizeof(struct rte_mbuf *) *
286 ctx->options->pool_sz), 0);
288 for (mbuf_idx = 0; mbuf_idx < options->pool_sz; mbuf_idx++) {
289 if (options->out_of_place == 1) {
290 ctx->mbufs_out[mbuf_idx] = cperf_mbuf_create(
291 ctx->pkt_mbuf_pool_out, 1,
292 options, test_vector);
293 if (ctx->mbufs_out[mbuf_idx] == NULL)
296 ctx->mbufs_out[mbuf_idx] = NULL;
300 snprintf(pool_name, sizeof(pool_name), "cperf_op_pool_cdev_%d",
303 ctx->crypto_op_pool = rte_crypto_op_pool_create(pool_name,
304 RTE_CRYPTO_OP_TYPE_SYMMETRIC, options->pool_sz, 0, 0,
306 if (ctx->crypto_op_pool == NULL)
309 ctx->res = rte_malloc(NULL, sizeof(struct cperf_op_result) *
310 ctx->options->total_ops, 0);
312 if (ctx->res == NULL)
317 cperf_latency_test_free(ctx, mbuf_idx);
323 cperf_latency_test_runner(void *arg)
325 struct cperf_latency_ctx *ctx = arg;
326 struct cperf_op_result *pres;
331 struct rte_crypto_op *ops[ctx->options->burst_sz];
332 struct rte_crypto_op *ops_processed[ctx->options->burst_sz];
333 uint64_t ops_enqd = 0, ops_deqd = 0;
334 uint64_t m_idx = 0, b_idx = 0, i;
336 uint64_t tsc_val, tsc_end, tsc_start;
337 uint64_t tsc_max = 0, tsc_min = ~0UL, tsc_tot = 0, tsc_idx = 0;
338 uint64_t enqd_max = 0, enqd_min = ~0UL, enqd_tot = 0;
339 uint64_t deqd_max = 0, deqd_min = ~0UL, deqd_tot = 0;
341 uint32_t lcore = rte_lcore_id();
343 #ifdef CPERF_LINEARIZATION_ENABLE
344 struct rte_cryptodev_info dev_info;
347 /* Check if source mbufs require coalescing */
348 if (ctx->options->segments_nb > 1) {
349 rte_cryptodev_info_get(ctx->dev_id, &dev_info);
350 if ((dev_info.feature_flags &
351 RTE_CRYPTODEV_FF_MBUF_SCATTER_GATHER) == 0)
354 #endif /* CPERF_LINEARIZATION_ENABLE */
356 ctx->lcore_id = lcore;
358 /* Warm up the host CPU before starting the test */
359 for (i = 0; i < ctx->options->total_ops; i++)
360 rte_cryptodev_enqueue_burst(ctx->dev_id, ctx->qp_id, NULL, 0);
362 while (enqd_tot < ctx->options->total_ops) {
364 uint16_t burst_size = ((enqd_tot + ctx->options->burst_sz)
365 <= ctx->options->total_ops) ?
366 ctx->options->burst_sz :
367 ctx->options->total_ops -
370 /* Allocate crypto ops from pool */
371 if (burst_size != rte_crypto_op_bulk_alloc(
373 RTE_CRYPTO_OP_TYPE_SYMMETRIC,
377 /* Setup crypto op, attach mbuf etc */
378 (ctx->populate_ops)(ops, &ctx->mbufs_in[m_idx],
379 &ctx->mbufs_out[m_idx],
380 burst_size, ctx->sess, ctx->options,
383 tsc_start = rte_rdtsc_precise();
385 #ifdef CPERF_LINEARIZATION_ENABLE
387 /* PMD doesn't support scatter-gather and source buffer
389 * We need to linearize it before enqueuing.
391 for (i = 0; i < burst_size; i++)
392 rte_pktmbuf_linearize(ops[i]->sym->m_src);
394 #endif /* CPERF_LINEARIZATION_ENABLE */
396 /* Enqueue burst of ops on crypto device */
397 ops_enqd = rte_cryptodev_enqueue_burst(ctx->dev_id, ctx->qp_id,
400 /* Dequeue processed burst of ops from crypto device */
401 ops_deqd = rte_cryptodev_dequeue_burst(ctx->dev_id, ctx->qp_id,
402 ops_processed, ctx->options->burst_sz);
404 tsc_end = rte_rdtsc_precise();
406 for (i = 0; i < ops_enqd; i++) {
407 ctx->res[tsc_idx].tsc_start = tsc_start;
408 ops[i]->opaque_data = (void *)&ctx->res[tsc_idx];
412 /* Free memory for not enqueued operations */
413 for (i = ops_enqd; i < burst_size; i++)
414 rte_crypto_op_free(ops[i]);
416 if (likely(ops_deqd)) {
418 * free crypto ops so they can be reused. We don't free
419 * the mbufs here as we don't want to reuse them as
420 * the crypto operation will change the data and cause
423 for (i = 0; i < ops_deqd; i++) {
424 pres = (struct cperf_op_result *)
425 (ops_processed[i]->opaque_data);
426 pres->status = ops_processed[i]->status;
427 pres->tsc_end = tsc_end;
429 rte_crypto_op_free(ops_processed[i]);
432 deqd_tot += ops_deqd;
433 deqd_max = max(ops_deqd, deqd_max);
434 deqd_min = min(ops_deqd, deqd_min);
437 enqd_tot += ops_enqd;
438 enqd_max = max(ops_enqd, enqd_max);
439 enqd_min = min(ops_enqd, enqd_min);
442 m_idx = m_idx + ctx->options->burst_sz > ctx->options->pool_sz ?
447 /* Dequeue any operations still in the crypto device */
448 while (deqd_tot < ctx->options->total_ops) {
449 /* Sending 0 length burst to flush sw crypto device */
450 rte_cryptodev_enqueue_burst(ctx->dev_id, ctx->qp_id, NULL, 0);
453 ops_deqd = rte_cryptodev_dequeue_burst(ctx->dev_id, ctx->qp_id,
454 ops_processed, ctx->options->burst_sz);
456 tsc_end = rte_rdtsc_precise();
459 for (i = 0; i < ops_deqd; i++) {
460 pres = (struct cperf_op_result *)
461 (ops_processed[i]->opaque_data);
462 pres->status = ops_processed[i]->status;
463 pres->tsc_end = tsc_end;
465 rte_crypto_op_free(ops_processed[i]);
468 deqd_tot += ops_deqd;
469 deqd_max = max(ops_deqd, deqd_max);
470 deqd_min = min(ops_deqd, deqd_min);
474 for (i = 0; i < tsc_idx; i++) {
475 tsc_val = ctx->res[i].tsc_end - ctx->res[i].tsc_start;
476 tsc_max = max(tsc_val, tsc_max);
477 tsc_min = min(tsc_val, tsc_min);
481 ctx->results.enqd_tot = enqd_tot;
482 ctx->results.enqd_max = enqd_max;
483 ctx->results.enqd_min = enqd_min;
485 ctx->results.deqd_tot = deqd_tot;
486 ctx->results.deqd_max = deqd_max;
487 ctx->results.deqd_min = deqd_min;
489 ctx->results.cycles_tot = tsc_tot;
490 ctx->results.cycles_max = tsc_max;
491 ctx->results.cycles_min = tsc_min;
493 ctx->results.burst_num = b_idx;
494 ctx->results.num = tsc_idx;
500 cperf_latency_test_destructor(void *arg)
502 struct cperf_latency_ctx *ctx = arg;
506 static int only_once;
507 uint64_t etot, eavg, emax, emin;
508 uint64_t dtot, davg, dmax, dmin;
509 uint64_t ctot, cavg, cmax, cmin;
510 double ttot, tavg, tmax, tmin;
512 const uint64_t tunit = 1000000; /* us */
513 const uint64_t tsc_hz = rte_get_tsc_hz();
515 etot = ctx->results.enqd_tot;
516 eavg = ctx->results.enqd_tot / ctx->results.burst_num;
517 emax = ctx->results.enqd_max;
518 emin = ctx->results.enqd_min;
520 dtot = ctx->results.deqd_tot;
521 davg = ctx->results.deqd_tot / ctx->results.burst_num;
522 dmax = ctx->results.deqd_max;
523 dmin = ctx->results.deqd_min;
525 ctot = ctx->results.cycles_tot;
526 cavg = ctx->results.cycles_tot / ctx->results.num;
527 cmax = ctx->results.cycles_max;
528 cmin = ctx->results.cycles_min;
530 ttot = tunit*(double)(ctot) / tsc_hz;
531 tavg = tunit*(double)(cavg) / tsc_hz;
532 tmax = tunit*(double)(cmax) / tsc_hz;
533 tmin = tunit*(double)(cmin) / tsc_hz;
535 if (ctx->options->csv) {
537 printf("\n# lcore, Pakt Seq #, Packet Size, cycles,"
540 for (i = 0; i < ctx->options->total_ops; i++) {
542 printf("\n%u;%"PRIu64";%"PRIu64";%.3f",
543 ctx->lcore_id, i + 1,
544 ctx->res[i].tsc_end - ctx->res[i].tsc_start,
545 tunit * (double) (ctx->res[i].tsc_end
546 - ctx->res[i].tsc_start)
552 printf("\n# Device %d on lcore %u\n", ctx->dev_id,
554 printf("\n# total operations: %u", ctx->options->total_ops);
555 printf("\n# burst number: %"PRIu64,
556 ctx->results.burst_num);
558 printf("\n# \t Total\t Average\t Maximum\t "
560 printf("\n# enqueued\t%12"PRIu64"\t%10"PRIu64"\t%10"PRIu64"\t"
561 "%10"PRIu64, etot, eavg, emax, emin);
562 printf("\n# dequeued\t%12"PRIu64"\t%10"PRIu64"\t%10"PRIu64"\t"
563 "%10"PRIu64, dtot, davg, dmax, dmin);
564 printf("\n# cycles\t%12"PRIu64"\t%10"PRIu64"\t%10"PRIu64"\t"
565 "%10"PRIu64, ctot, cavg, cmax, cmin);
566 printf("\n# time [us]\t%12.0f\t%10.3f\t%10.3f\t%10.3f", ttot,
571 cperf_latency_test_free(ctx, ctx->options->pool_sz);