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;
70 struct cperf_op_result *result;
73 #define max(a, b) (a > b ? (uint64_t)a : (uint64_t)b)
74 #define min(a, b) (a < b ? (uint64_t)a : (uint64_t)b)
77 cperf_latency_test_free(struct cperf_latency_ctx *ctx, uint32_t mbuf_nb)
83 rte_cryptodev_sym_session_clear(ctx->dev_id, ctx->sess);
84 rte_cryptodev_sym_session_free(ctx->sess);
88 for (i = 0; i < mbuf_nb; i++)
89 rte_pktmbuf_free(ctx->mbufs_in[i]);
91 rte_free(ctx->mbufs_in);
95 for (i = 0; i < mbuf_nb; i++) {
96 if (ctx->mbufs_out[i] != NULL)
97 rte_pktmbuf_free(ctx->mbufs_out[i]);
100 rte_free(ctx->mbufs_out);
103 if (ctx->pkt_mbuf_pool_in)
104 rte_mempool_free(ctx->pkt_mbuf_pool_in);
106 if (ctx->pkt_mbuf_pool_out)
107 rte_mempool_free(ctx->pkt_mbuf_pool_out);
109 if (ctx->crypto_op_pool)
110 rte_mempool_free(ctx->crypto_op_pool);
117 static struct rte_mbuf *
118 cperf_mbuf_create(struct rte_mempool *mempool,
119 uint32_t segments_nb,
120 const struct cperf_options *options,
121 const struct cperf_test_vector *test_vector)
123 struct rte_mbuf *mbuf;
124 uint32_t segment_sz = options->max_buffer_size / segments_nb;
125 uint32_t last_sz = options->max_buffer_size % segments_nb;
128 (options->cipher_op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
129 test_vector->plaintext.data :
130 test_vector->ciphertext.data;
132 mbuf = rte_pktmbuf_alloc(mempool);
136 mbuf_data = (uint8_t *)rte_pktmbuf_append(mbuf, segment_sz);
137 if (mbuf_data == NULL)
140 memcpy(mbuf_data, test_data, segment_sz);
141 test_data += segment_sz;
144 while (segments_nb) {
147 m = rte_pktmbuf_alloc(mempool);
151 rte_pktmbuf_chain(mbuf, m);
153 mbuf_data = (uint8_t *)rte_pktmbuf_append(mbuf, segment_sz);
154 if (mbuf_data == NULL)
157 memcpy(mbuf_data, test_data, segment_sz);
158 test_data += segment_sz;
163 mbuf_data = (uint8_t *)rte_pktmbuf_append(mbuf, last_sz);
164 if (mbuf_data == NULL)
167 memcpy(mbuf_data, test_data, last_sz);
170 if (options->op_type != CPERF_CIPHER_ONLY) {
171 mbuf_data = (uint8_t *)rte_pktmbuf_append(mbuf,
173 if (mbuf_data == NULL)
177 if (options->op_type == CPERF_AEAD) {
178 uint8_t *aead = (uint8_t *)rte_pktmbuf_prepend(mbuf,
179 RTE_ALIGN_CEIL(options->aead_aad_sz, 16));
184 memcpy(aead, test_vector->aad.data, test_vector->aad.length);
190 rte_pktmbuf_free(mbuf);
196 cperf_latency_test_constructor(struct rte_mempool *sess_mp,
197 uint8_t dev_id, uint16_t qp_id,
198 const struct cperf_options *options,
199 const struct cperf_test_vector *test_vector,
200 const struct cperf_op_fns *op_fns)
202 struct cperf_latency_ctx *ctx = NULL;
203 unsigned int mbuf_idx = 0;
204 char pool_name[32] = "";
206 ctx = rte_malloc(NULL, sizeof(struct cperf_latency_ctx), 0);
210 ctx->dev_id = dev_id;
213 ctx->populate_ops = op_fns->populate_ops;
214 ctx->options = options;
215 ctx->test_vector = test_vector;
217 /* IV goes at the end of the crypto operation */
218 uint16_t iv_offset = sizeof(struct rte_crypto_op) +
219 sizeof(struct rte_crypto_sym_op) +
220 sizeof(struct cperf_op_result *);
222 ctx->sess = op_fns->sess_create(sess_mp, dev_id, options, test_vector,
224 if (ctx->sess == NULL)
227 snprintf(pool_name, sizeof(pool_name), "cperf_pool_in_cdev_%d",
230 ctx->pkt_mbuf_pool_in = rte_pktmbuf_pool_create(pool_name,
231 options->pool_sz * options->segments_nb, 0, 0,
232 RTE_PKTMBUF_HEADROOM +
233 RTE_CACHE_LINE_ROUNDUP(
234 (options->max_buffer_size / options->segments_nb) +
235 (options->max_buffer_size % options->segments_nb) +
239 if (ctx->pkt_mbuf_pool_in == NULL)
242 /* Generate mbufs_in with plaintext populated for test */
243 ctx->mbufs_in = rte_malloc(NULL,
244 (sizeof(struct rte_mbuf *) *
245 ctx->options->pool_sz), 0);
247 for (mbuf_idx = 0; mbuf_idx < options->pool_sz; mbuf_idx++) {
248 ctx->mbufs_in[mbuf_idx] = cperf_mbuf_create(
249 ctx->pkt_mbuf_pool_in, options->segments_nb,
250 options, test_vector);
251 if (ctx->mbufs_in[mbuf_idx] == NULL)
255 if (options->out_of_place == 1) {
257 snprintf(pool_name, sizeof(pool_name),
258 "cperf_pool_out_cdev_%d",
261 ctx->pkt_mbuf_pool_out = rte_pktmbuf_pool_create(
262 pool_name, options->pool_sz, 0, 0,
263 RTE_PKTMBUF_HEADROOM +
264 RTE_CACHE_LINE_ROUNDUP(
265 options->max_buffer_size +
269 if (ctx->pkt_mbuf_pool_out == NULL)
273 ctx->mbufs_out = rte_malloc(NULL,
274 (sizeof(struct rte_mbuf *) *
275 ctx->options->pool_sz), 0);
277 for (mbuf_idx = 0; mbuf_idx < options->pool_sz; mbuf_idx++) {
278 if (options->out_of_place == 1) {
279 ctx->mbufs_out[mbuf_idx] = cperf_mbuf_create(
280 ctx->pkt_mbuf_pool_out, 1,
281 options, test_vector);
282 if (ctx->mbufs_out[mbuf_idx] == NULL)
285 ctx->mbufs_out[mbuf_idx] = NULL;
289 snprintf(pool_name, sizeof(pool_name), "cperf_op_pool_cdev_%d",
292 uint16_t priv_size = sizeof(struct priv_op_data) +
293 test_vector->cipher_iv.length +
294 test_vector->auth_iv.length;
295 ctx->crypto_op_pool = rte_crypto_op_pool_create(pool_name,
296 RTE_CRYPTO_OP_TYPE_SYMMETRIC, options->pool_sz,
297 512, priv_size, rte_socket_id());
299 if (ctx->crypto_op_pool == NULL)
302 ctx->res = rte_malloc(NULL, sizeof(struct cperf_op_result) *
303 ctx->options->total_ops, 0);
305 if (ctx->res == NULL)
310 cperf_latency_test_free(ctx, mbuf_idx);
316 store_timestamp(struct rte_crypto_op *op, uint64_t timestamp)
318 struct priv_op_data *priv_data;
320 priv_data = (struct priv_op_data *) (op->sym + 1);
321 priv_data->result->status = op->status;
322 priv_data->result->tsc_end = timestamp;
326 cperf_latency_test_runner(void *arg)
328 struct cperf_latency_ctx *ctx = arg;
329 uint16_t test_burst_size;
330 uint8_t burst_size_idx = 0;
332 static int only_once;
337 struct rte_crypto_op *ops[ctx->options->max_burst_size];
338 struct rte_crypto_op *ops_processed[ctx->options->max_burst_size];
340 struct priv_op_data *priv_data;
342 uint32_t lcore = rte_lcore_id();
344 #ifdef CPERF_LINEARIZATION_ENABLE
345 struct rte_cryptodev_info dev_info;
348 /* Check if source mbufs require coalescing */
349 if (ctx->options->segments_nb > 1) {
350 rte_cryptodev_info_get(ctx->dev_id, &dev_info);
351 if ((dev_info.feature_flags &
352 RTE_CRYPTODEV_FF_MBUF_SCATTER_GATHER) == 0)
355 #endif /* CPERF_LINEARIZATION_ENABLE */
357 ctx->lcore_id = lcore;
359 /* Warm up the host CPU before starting the test */
360 for (i = 0; i < ctx->options->total_ops; i++)
361 rte_cryptodev_enqueue_burst(ctx->dev_id, ctx->qp_id, NULL, 0);
363 /* Get first size from range or list */
364 if (ctx->options->inc_burst_size != 0)
365 test_burst_size = ctx->options->min_burst_size;
367 test_burst_size = ctx->options->burst_size_list[0];
369 uint16_t iv_offset = sizeof(struct rte_crypto_op) +
370 sizeof(struct rte_crypto_sym_op) +
371 sizeof(struct cperf_op_result *);
373 while (test_burst_size <= ctx->options->max_burst_size) {
374 uint64_t ops_enqd = 0, ops_deqd = 0;
375 uint64_t m_idx = 0, b_idx = 0;
377 uint64_t tsc_val, tsc_end, tsc_start;
378 uint64_t tsc_max = 0, tsc_min = ~0UL, tsc_tot = 0, tsc_idx = 0;
379 uint64_t enqd_max = 0, enqd_min = ~0UL, enqd_tot = 0;
380 uint64_t deqd_max = 0, deqd_min = ~0UL, deqd_tot = 0;
382 while (enqd_tot < ctx->options->total_ops) {
384 uint16_t burst_size = ((enqd_tot + test_burst_size)
385 <= ctx->options->total_ops) ?
387 ctx->options->total_ops -
390 /* Allocate crypto ops from pool */
391 if (burst_size != rte_crypto_op_bulk_alloc(
393 RTE_CRYPTO_OP_TYPE_SYMMETRIC,
397 /* Setup crypto op, attach mbuf etc */
398 (ctx->populate_ops)(ops, &ctx->mbufs_in[m_idx],
399 &ctx->mbufs_out[m_idx],
400 burst_size, ctx->sess, ctx->options,
401 ctx->test_vector, iv_offset);
403 tsc_start = rte_rdtsc_precise();
405 #ifdef CPERF_LINEARIZATION_ENABLE
407 /* PMD doesn't support scatter-gather and source buffer
409 * We need to linearize it before enqueuing.
411 for (i = 0; i < burst_size; i++)
412 rte_pktmbuf_linearize(ops[i]->sym->m_src);
414 #endif /* CPERF_LINEARIZATION_ENABLE */
416 /* Enqueue burst of ops on crypto device */
417 ops_enqd = rte_cryptodev_enqueue_burst(ctx->dev_id, ctx->qp_id,
420 /* Dequeue processed burst of ops from crypto device */
421 ops_deqd = rte_cryptodev_dequeue_burst(ctx->dev_id, ctx->qp_id,
422 ops_processed, test_burst_size);
424 tsc_end = rte_rdtsc_precise();
426 /* Free memory for not enqueued operations */
427 if (ops_enqd != burst_size)
428 rte_mempool_put_bulk(ctx->crypto_op_pool,
429 (void **)&ops_processed[ops_enqd],
430 burst_size - ops_enqd);
432 for (i = 0; i < ops_enqd; i++) {
433 ctx->res[tsc_idx].tsc_start = tsc_start;
435 * Private data structure starts after the end of the
436 * rte_crypto_sym_op structure.
438 priv_data = (struct priv_op_data *) (ops[i]->sym + 1);
439 priv_data->result = (void *)&ctx->res[tsc_idx];
443 if (likely(ops_deqd)) {
445 * free crypto ops so they can be reused. We don't free
446 * the mbufs here as we don't want to reuse them as
447 * the crypto operation will change the data and cause
450 for (i = 0; i < ops_deqd; i++)
451 store_timestamp(ops_processed[i], tsc_end);
453 rte_mempool_put_bulk(ctx->crypto_op_pool,
454 (void **)ops_processed, ops_deqd);
456 deqd_tot += ops_deqd;
457 deqd_max = max(ops_deqd, deqd_max);
458 deqd_min = min(ops_deqd, deqd_min);
461 enqd_tot += ops_enqd;
462 enqd_max = max(ops_enqd, enqd_max);
463 enqd_min = min(ops_enqd, enqd_min);
466 m_idx = m_idx + test_burst_size > ctx->options->pool_sz ?
471 /* Dequeue any operations still in the crypto device */
472 while (deqd_tot < ctx->options->total_ops) {
473 /* Sending 0 length burst to flush sw crypto device */
474 rte_cryptodev_enqueue_burst(ctx->dev_id, ctx->qp_id, NULL, 0);
477 ops_deqd = rte_cryptodev_dequeue_burst(ctx->dev_id, ctx->qp_id,
478 ops_processed, test_burst_size);
480 tsc_end = rte_rdtsc_precise();
483 for (i = 0; i < ops_deqd; i++)
484 store_timestamp(ops_processed[i], tsc_end);
486 rte_mempool_put_bulk(ctx->crypto_op_pool,
487 (void **)ops_processed, ops_deqd);
489 deqd_tot += ops_deqd;
490 deqd_max = max(ops_deqd, deqd_max);
491 deqd_min = min(ops_deqd, deqd_min);
495 for (i = 0; i < tsc_idx; i++) {
496 tsc_val = ctx->res[i].tsc_end - ctx->res[i].tsc_start;
497 tsc_max = max(tsc_val, tsc_max);
498 tsc_min = min(tsc_val, tsc_min);
502 double time_tot, time_avg, time_max, time_min;
504 const uint64_t tunit = 1000000; /* us */
505 const uint64_t tsc_hz = rte_get_tsc_hz();
507 uint64_t enqd_avg = enqd_tot / b_idx;
508 uint64_t deqd_avg = deqd_tot / b_idx;
509 uint64_t tsc_avg = tsc_tot / tsc_idx;
511 time_tot = tunit*(double)(tsc_tot) / tsc_hz;
512 time_avg = tunit*(double)(tsc_avg) / tsc_hz;
513 time_max = tunit*(double)(tsc_max) / tsc_hz;
514 time_min = tunit*(double)(tsc_min) / tsc_hz;
516 if (ctx->options->csv) {
518 printf("\n# lcore, Buffer Size, Burst Size, Pakt Seq #, "
519 "Packet Size, cycles, time (us)");
521 for (i = 0; i < ctx->options->total_ops; i++) {
523 printf("\n%u;%u;%u;%"PRIu64";%"PRIu64";%.3f",
524 ctx->lcore_id, ctx->options->test_buffer_size,
525 test_burst_size, i + 1,
526 ctx->res[i].tsc_end - ctx->res[i].tsc_start,
527 tunit * (double) (ctx->res[i].tsc_end
528 - ctx->res[i].tsc_start)
534 printf("\n# Device %d on lcore %u\n", ctx->dev_id,
536 printf("\n# total operations: %u", ctx->options->total_ops);
537 printf("\n# Buffer size: %u", ctx->options->test_buffer_size);
538 printf("\n# Burst size: %u", test_burst_size);
539 printf("\n# Number of bursts: %"PRIu64,
543 printf("\n# \t Total\t Average\t "
544 "Maximum\t Minimum");
545 printf("\n# enqueued\t%12"PRIu64"\t%10"PRIu64"\t"
546 "%10"PRIu64"\t%10"PRIu64, enqd_tot,
547 enqd_avg, enqd_max, enqd_min);
548 printf("\n# dequeued\t%12"PRIu64"\t%10"PRIu64"\t"
549 "%10"PRIu64"\t%10"PRIu64, deqd_tot,
550 deqd_avg, deqd_max, deqd_min);
551 printf("\n# cycles\t%12"PRIu64"\t%10"PRIu64"\t"
552 "%10"PRIu64"\t%10"PRIu64, tsc_tot,
553 tsc_avg, tsc_max, tsc_min);
554 printf("\n# time [us]\t%12.0f\t%10.3f\t%10.3f\t%10.3f",
555 time_tot, time_avg, time_max, time_min);
560 /* Get next size from range or list */
561 if (ctx->options->inc_burst_size != 0)
562 test_burst_size += ctx->options->inc_burst_size;
564 if (++burst_size_idx == ctx->options->burst_size_count)
567 ctx->options->burst_size_list[burst_size_idx];
575 cperf_latency_test_destructor(void *arg)
577 struct cperf_latency_ctx *ctx = arg;
582 cperf_latency_test_free(ctx, ctx->options->pool_sz);