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_free(ctx->dev_id, ctx->sess);
86 for (i = 0; i < mbuf_nb; i++)
87 rte_pktmbuf_free(ctx->mbufs_in[i]);
89 rte_free(ctx->mbufs_in);
93 for (i = 0; i < mbuf_nb; i++) {
94 if (ctx->mbufs_out[i] != NULL)
95 rte_pktmbuf_free(ctx->mbufs_out[i]);
98 rte_free(ctx->mbufs_out);
101 if (ctx->pkt_mbuf_pool_in)
102 rte_mempool_free(ctx->pkt_mbuf_pool_in);
104 if (ctx->pkt_mbuf_pool_out)
105 rte_mempool_free(ctx->pkt_mbuf_pool_out);
107 if (ctx->crypto_op_pool)
108 rte_mempool_free(ctx->crypto_op_pool);
115 static struct rte_mbuf *
116 cperf_mbuf_create(struct rte_mempool *mempool,
117 uint32_t segments_nb,
118 const struct cperf_options *options,
119 const struct cperf_test_vector *test_vector)
121 struct rte_mbuf *mbuf;
122 uint32_t segment_sz = options->max_buffer_size / segments_nb;
123 uint32_t last_sz = options->max_buffer_size % segments_nb;
126 (options->cipher_op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
127 test_vector->plaintext.data :
128 test_vector->ciphertext.data;
130 mbuf = rte_pktmbuf_alloc(mempool);
134 mbuf_data = (uint8_t *)rte_pktmbuf_append(mbuf, segment_sz);
135 if (mbuf_data == NULL)
138 memcpy(mbuf_data, test_data, segment_sz);
139 test_data += segment_sz;
142 while (segments_nb) {
145 m = rte_pktmbuf_alloc(mempool);
149 rte_pktmbuf_chain(mbuf, m);
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;
161 mbuf_data = (uint8_t *)rte_pktmbuf_append(mbuf, last_sz);
162 if (mbuf_data == NULL)
165 memcpy(mbuf_data, test_data, last_sz);
168 if (options->op_type != CPERF_CIPHER_ONLY) {
169 mbuf_data = (uint8_t *)rte_pktmbuf_append(mbuf,
170 options->auth_digest_sz);
171 if (mbuf_data == NULL)
175 if (options->op_type == CPERF_AEAD) {
176 uint8_t *aead = (uint8_t *)rte_pktmbuf_prepend(mbuf,
177 RTE_ALIGN_CEIL(options->auth_aad_sz, 16));
182 memcpy(aead, test_vector->aad.data, test_vector->aad.length);
188 rte_pktmbuf_free(mbuf);
194 cperf_latency_test_constructor(uint8_t dev_id, uint16_t qp_id,
195 const struct cperf_options *options,
196 const struct cperf_test_vector *test_vector,
197 const struct cperf_op_fns *op_fns)
199 struct cperf_latency_ctx *ctx = NULL;
200 unsigned int mbuf_idx = 0;
201 char pool_name[32] = "";
203 ctx = rte_malloc(NULL, sizeof(struct cperf_latency_ctx), 0);
207 ctx->dev_id = dev_id;
210 ctx->populate_ops = op_fns->populate_ops;
211 ctx->options = options;
212 ctx->test_vector = test_vector;
214 /* IV goes at the end of the crypto operation */
215 uint16_t iv_offset = sizeof(struct rte_crypto_op) +
216 sizeof(struct rte_crypto_sym_op) +
217 sizeof(struct cperf_op_result *);
219 ctx->sess = op_fns->sess_create(dev_id, options, test_vector, iv_offset);
220 if (ctx->sess == NULL)
223 snprintf(pool_name, sizeof(pool_name), "cperf_pool_in_cdev_%d",
226 ctx->pkt_mbuf_pool_in = rte_pktmbuf_pool_create(pool_name,
227 options->pool_sz * options->segments_nb, 0, 0,
228 RTE_PKTMBUF_HEADROOM +
229 RTE_CACHE_LINE_ROUNDUP(
230 (options->max_buffer_size / options->segments_nb) +
231 (options->max_buffer_size % options->segments_nb) +
232 options->auth_digest_sz),
235 if (ctx->pkt_mbuf_pool_in == NULL)
238 /* Generate mbufs_in with plaintext populated for test */
239 ctx->mbufs_in = rte_malloc(NULL,
240 (sizeof(struct rte_mbuf *) *
241 ctx->options->pool_sz), 0);
243 for (mbuf_idx = 0; mbuf_idx < options->pool_sz; mbuf_idx++) {
244 ctx->mbufs_in[mbuf_idx] = cperf_mbuf_create(
245 ctx->pkt_mbuf_pool_in, options->segments_nb,
246 options, test_vector);
247 if (ctx->mbufs_in[mbuf_idx] == NULL)
251 if (options->out_of_place == 1) {
253 snprintf(pool_name, sizeof(pool_name),
254 "cperf_pool_out_cdev_%d",
257 ctx->pkt_mbuf_pool_out = rte_pktmbuf_pool_create(
258 pool_name, options->pool_sz, 0, 0,
259 RTE_PKTMBUF_HEADROOM +
260 RTE_CACHE_LINE_ROUNDUP(
261 options->max_buffer_size +
262 options->auth_digest_sz),
265 if (ctx->pkt_mbuf_pool_out == NULL)
269 ctx->mbufs_out = rte_malloc(NULL,
270 (sizeof(struct rte_mbuf *) *
271 ctx->options->pool_sz), 0);
273 for (mbuf_idx = 0; mbuf_idx < options->pool_sz; mbuf_idx++) {
274 if (options->out_of_place == 1) {
275 ctx->mbufs_out[mbuf_idx] = cperf_mbuf_create(
276 ctx->pkt_mbuf_pool_out, 1,
277 options, test_vector);
278 if (ctx->mbufs_out[mbuf_idx] == NULL)
281 ctx->mbufs_out[mbuf_idx] = NULL;
285 snprintf(pool_name, sizeof(pool_name), "cperf_op_pool_cdev_%d",
288 uint16_t priv_size = sizeof(struct priv_op_data) +
289 test_vector->cipher_iv.length +
290 test_vector->auth_iv.length;
291 ctx->crypto_op_pool = rte_crypto_op_pool_create(pool_name,
292 RTE_CRYPTO_OP_TYPE_SYMMETRIC, options->pool_sz,
293 512, priv_size, rte_socket_id());
295 if (ctx->crypto_op_pool == NULL)
298 ctx->res = rte_malloc(NULL, sizeof(struct cperf_op_result) *
299 ctx->options->total_ops, 0);
301 if (ctx->res == NULL)
306 cperf_latency_test_free(ctx, mbuf_idx);
312 store_timestamp(struct rte_crypto_op *op, uint64_t timestamp)
314 struct priv_op_data *priv_data;
316 priv_data = (struct priv_op_data *) (op->sym + 1);
317 priv_data->result->status = op->status;
318 priv_data->result->tsc_end = timestamp;
322 cperf_latency_test_runner(void *arg)
324 struct cperf_latency_ctx *ctx = arg;
325 uint16_t test_burst_size;
326 uint8_t burst_size_idx = 0;
328 static int only_once;
333 struct rte_crypto_op *ops[ctx->options->max_burst_size];
334 struct rte_crypto_op *ops_processed[ctx->options->max_burst_size];
336 struct priv_op_data *priv_data;
338 uint32_t lcore = rte_lcore_id();
340 #ifdef CPERF_LINEARIZATION_ENABLE
341 struct rte_cryptodev_info dev_info;
344 /* Check if source mbufs require coalescing */
345 if (ctx->options->segments_nb > 1) {
346 rte_cryptodev_info_get(ctx->dev_id, &dev_info);
347 if ((dev_info.feature_flags &
348 RTE_CRYPTODEV_FF_MBUF_SCATTER_GATHER) == 0)
351 #endif /* CPERF_LINEARIZATION_ENABLE */
353 ctx->lcore_id = lcore;
355 /* Warm up the host CPU before starting the test */
356 for (i = 0; i < ctx->options->total_ops; i++)
357 rte_cryptodev_enqueue_burst(ctx->dev_id, ctx->qp_id, NULL, 0);
359 /* Get first size from range or list */
360 if (ctx->options->inc_burst_size != 0)
361 test_burst_size = ctx->options->min_burst_size;
363 test_burst_size = ctx->options->burst_size_list[0];
365 uint16_t iv_offset = sizeof(struct rte_crypto_op) +
366 sizeof(struct rte_crypto_sym_op) +
367 sizeof(struct cperf_op_result *);
369 while (test_burst_size <= ctx->options->max_burst_size) {
370 uint64_t ops_enqd = 0, ops_deqd = 0;
371 uint64_t m_idx = 0, b_idx = 0;
373 uint64_t tsc_val, tsc_end, tsc_start;
374 uint64_t tsc_max = 0, tsc_min = ~0UL, tsc_tot = 0, tsc_idx = 0;
375 uint64_t enqd_max = 0, enqd_min = ~0UL, enqd_tot = 0;
376 uint64_t deqd_max = 0, deqd_min = ~0UL, deqd_tot = 0;
378 while (enqd_tot < ctx->options->total_ops) {
380 uint16_t burst_size = ((enqd_tot + test_burst_size)
381 <= ctx->options->total_ops) ?
383 ctx->options->total_ops -
386 /* Allocate crypto ops from pool */
387 if (burst_size != rte_crypto_op_bulk_alloc(
389 RTE_CRYPTO_OP_TYPE_SYMMETRIC,
393 /* Setup crypto op, attach mbuf etc */
394 (ctx->populate_ops)(ops, &ctx->mbufs_in[m_idx],
395 &ctx->mbufs_out[m_idx],
396 burst_size, ctx->sess, ctx->options,
397 ctx->test_vector, iv_offset);
399 tsc_start = rte_rdtsc_precise();
401 #ifdef CPERF_LINEARIZATION_ENABLE
403 /* PMD doesn't support scatter-gather and source buffer
405 * We need to linearize it before enqueuing.
407 for (i = 0; i < burst_size; i++)
408 rte_pktmbuf_linearize(ops[i]->sym->m_src);
410 #endif /* CPERF_LINEARIZATION_ENABLE */
412 /* Enqueue burst of ops on crypto device */
413 ops_enqd = rte_cryptodev_enqueue_burst(ctx->dev_id, ctx->qp_id,
416 /* Dequeue processed burst of ops from crypto device */
417 ops_deqd = rte_cryptodev_dequeue_burst(ctx->dev_id, ctx->qp_id,
418 ops_processed, test_burst_size);
420 tsc_end = rte_rdtsc_precise();
422 /* Free memory for not enqueued operations */
423 if (ops_enqd != burst_size)
424 rte_mempool_put_bulk(ctx->crypto_op_pool,
425 (void **)&ops_processed[ops_enqd],
426 burst_size - ops_enqd);
428 for (i = 0; i < ops_enqd; i++) {
429 ctx->res[tsc_idx].tsc_start = tsc_start;
431 * Private data structure starts after the end of the
432 * rte_crypto_sym_op structure.
434 priv_data = (struct priv_op_data *) (ops[i]->sym + 1);
435 priv_data->result = (void *)&ctx->res[tsc_idx];
439 if (likely(ops_deqd)) {
441 * free crypto ops so they can be reused. We don't free
442 * the mbufs here as we don't want to reuse them as
443 * the crypto operation will change the data and cause
446 for (i = 0; i < ops_deqd; i++)
447 store_timestamp(ops_processed[i], tsc_end);
449 rte_mempool_put_bulk(ctx->crypto_op_pool,
450 (void **)ops_processed, ops_deqd);
452 deqd_tot += ops_deqd;
453 deqd_max = max(ops_deqd, deqd_max);
454 deqd_min = min(ops_deqd, deqd_min);
457 enqd_tot += ops_enqd;
458 enqd_max = max(ops_enqd, enqd_max);
459 enqd_min = min(ops_enqd, enqd_min);
462 m_idx = m_idx + test_burst_size > ctx->options->pool_sz ?
467 /* Dequeue any operations still in the crypto device */
468 while (deqd_tot < ctx->options->total_ops) {
469 /* Sending 0 length burst to flush sw crypto device */
470 rte_cryptodev_enqueue_burst(ctx->dev_id, ctx->qp_id, NULL, 0);
473 ops_deqd = rte_cryptodev_dequeue_burst(ctx->dev_id, ctx->qp_id,
474 ops_processed, test_burst_size);
476 tsc_end = rte_rdtsc_precise();
479 for (i = 0; i < ops_deqd; i++)
480 store_timestamp(ops_processed[i], tsc_end);
482 rte_mempool_put_bulk(ctx->crypto_op_pool,
483 (void **)ops_processed, ops_deqd);
485 deqd_tot += ops_deqd;
486 deqd_max = max(ops_deqd, deqd_max);
487 deqd_min = min(ops_deqd, deqd_min);
491 for (i = 0; i < tsc_idx; i++) {
492 tsc_val = ctx->res[i].tsc_end - ctx->res[i].tsc_start;
493 tsc_max = max(tsc_val, tsc_max);
494 tsc_min = min(tsc_val, tsc_min);
498 double time_tot, time_avg, time_max, time_min;
500 const uint64_t tunit = 1000000; /* us */
501 const uint64_t tsc_hz = rte_get_tsc_hz();
503 uint64_t enqd_avg = enqd_tot / b_idx;
504 uint64_t deqd_avg = deqd_tot / b_idx;
505 uint64_t tsc_avg = tsc_tot / tsc_idx;
507 time_tot = tunit*(double)(tsc_tot) / tsc_hz;
508 time_avg = tunit*(double)(tsc_avg) / tsc_hz;
509 time_max = tunit*(double)(tsc_max) / tsc_hz;
510 time_min = tunit*(double)(tsc_min) / tsc_hz;
512 if (ctx->options->csv) {
514 printf("\n# lcore, Buffer Size, Burst Size, Pakt Seq #, "
515 "Packet Size, cycles, time (us)");
517 for (i = 0; i < ctx->options->total_ops; i++) {
519 printf("\n%u;%u;%u;%"PRIu64";%"PRIu64";%.3f",
520 ctx->lcore_id, ctx->options->test_buffer_size,
521 test_burst_size, i + 1,
522 ctx->res[i].tsc_end - ctx->res[i].tsc_start,
523 tunit * (double) (ctx->res[i].tsc_end
524 - ctx->res[i].tsc_start)
530 printf("\n# Device %d on lcore %u\n", ctx->dev_id,
532 printf("\n# total operations: %u", ctx->options->total_ops);
533 printf("\n# Buffer size: %u", ctx->options->test_buffer_size);
534 printf("\n# Burst size: %u", test_burst_size);
535 printf("\n# Number of bursts: %"PRIu64,
539 printf("\n# \t Total\t Average\t "
540 "Maximum\t Minimum");
541 printf("\n# enqueued\t%12"PRIu64"\t%10"PRIu64"\t"
542 "%10"PRIu64"\t%10"PRIu64, enqd_tot,
543 enqd_avg, enqd_max, enqd_min);
544 printf("\n# dequeued\t%12"PRIu64"\t%10"PRIu64"\t"
545 "%10"PRIu64"\t%10"PRIu64, deqd_tot,
546 deqd_avg, deqd_max, deqd_min);
547 printf("\n# cycles\t%12"PRIu64"\t%10"PRIu64"\t"
548 "%10"PRIu64"\t%10"PRIu64, tsc_tot,
549 tsc_avg, tsc_max, tsc_min);
550 printf("\n# time [us]\t%12.0f\t%10.3f\t%10.3f\t%10.3f",
551 time_tot, time_avg, time_max, time_min);
556 /* Get next size from range or list */
557 if (ctx->options->inc_burst_size != 0)
558 test_burst_size += ctx->options->inc_burst_size;
560 if (++burst_size_idx == ctx->options->burst_size_count)
563 ctx->options->burst_size_list[burst_size_idx];
571 cperf_latency_test_destructor(void *arg)
573 struct cperf_latency_ctx *ctx = arg;
578 cperf_latency_test_free(ctx, ctx->options->pool_sz);