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_throughput.h"
39 #include "cperf_ops.h"
41 struct cperf_throughput_results {
42 uint64_t ops_enqueued;
43 uint64_t ops_dequeued;
45 uint64_t ops_enqueued_failed;
46 uint64_t ops_dequeued_failed;
50 double ops_per_second;
51 double throughput_gbps;
52 double cycles_per_byte;
55 struct cperf_throughput_ctx {
60 struct rte_mempool *pkt_mbuf_pool_in;
61 struct rte_mempool *pkt_mbuf_pool_out;
62 struct rte_mbuf **mbufs_in;
63 struct rte_mbuf **mbufs_out;
65 struct rte_mempool *crypto_op_pool;
67 struct rte_cryptodev_sym_session *sess;
69 cperf_populate_ops_t populate_ops;
70 cperf_verify_crypto_op_t verify_op_output;
72 const struct cperf_options *options;
73 const struct cperf_test_vector *test_vector;
74 struct cperf_throughput_results results;
78 struct cperf_op_result {
79 enum rte_crypto_op_status status;
83 cperf_throughput_test_free(struct cperf_throughput_ctx *ctx, uint32_t mbuf_nb)
89 rte_cryptodev_sym_session_free(ctx->dev_id, ctx->sess);
92 for (i = 0; i < mbuf_nb; i++)
93 rte_pktmbuf_free(ctx->mbufs_in[i]);
95 rte_free(ctx->mbufs_in);
99 for (i = 0; i < mbuf_nb; i++) {
100 if (ctx->mbufs_out[i] != NULL)
101 rte_pktmbuf_free(ctx->mbufs_out[i]);
104 rte_free(ctx->mbufs_out);
107 if (ctx->pkt_mbuf_pool_in)
108 rte_mempool_free(ctx->pkt_mbuf_pool_in);
110 if (ctx->pkt_mbuf_pool_out)
111 rte_mempool_free(ctx->pkt_mbuf_pool_out);
113 if (ctx->crypto_op_pool)
114 rte_mempool_free(ctx->crypto_op_pool);
120 static struct rte_mbuf *
121 cperf_mbuf_create(struct rte_mempool *mempool,
122 uint32_t segments_nb,
123 const struct cperf_options *options,
124 const struct cperf_test_vector *test_vector)
126 struct rte_mbuf *mbuf;
127 uint32_t segment_sz = options->buffer_sz / segments_nb;
128 uint32_t last_sz = options->buffer_sz % segments_nb;
131 (options->cipher_op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
132 test_vector->plaintext.data :
133 test_vector->ciphertext.data;
135 mbuf = rte_pktmbuf_alloc(mempool);
139 mbuf_data = (uint8_t *)rte_pktmbuf_append(mbuf, segment_sz);
140 if (mbuf_data == NULL)
143 memcpy(mbuf_data, test_data, segment_sz);
144 test_data += segment_sz;
147 while (segments_nb) {
150 m = rte_pktmbuf_alloc(mempool);
154 rte_pktmbuf_chain(mbuf, m);
156 mbuf_data = (uint8_t *)rte_pktmbuf_append(mbuf, segment_sz);
157 if (mbuf_data == NULL)
160 memcpy(mbuf_data, test_data, segment_sz);
161 test_data += segment_sz;
166 mbuf_data = (uint8_t *)rte_pktmbuf_append(mbuf, last_sz);
167 if (mbuf_data == NULL)
170 memcpy(mbuf_data, test_data, last_sz);
173 mbuf_data = (uint8_t *)rte_pktmbuf_append(mbuf,
174 options->auth_digest_sz);
175 if (mbuf_data == NULL)
178 if (options->op_type == CPERF_AEAD) {
179 uint8_t *aead = (uint8_t *)rte_pktmbuf_prepend(mbuf,
180 RTE_ALIGN_CEIL(options->auth_aad_sz, 16));
185 memcpy(aead, test_vector->aad.data, test_vector->aad.length);
191 rte_pktmbuf_free(mbuf);
197 cperf_throughput_test_constructor(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_throughput_ctx *ctx = NULL;
203 unsigned int mbuf_idx = 0;
204 char pool_name[32] = "";
206 ctx = rte_malloc(NULL, sizeof(struct cperf_throughput_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 ctx->sess = op_fns->sess_create(dev_id, options, test_vector);
218 if (ctx->sess == NULL)
221 snprintf(pool_name, sizeof(pool_name), "cperf_pool_in_cdev_%d",
224 ctx->pkt_mbuf_pool_in = rte_pktmbuf_pool_create(pool_name,
225 options->pool_sz * options->segments_nb, 0, 0,
226 RTE_PKTMBUF_HEADROOM +
227 RTE_CACHE_LINE_ROUNDUP(
228 (options->buffer_sz / options->segments_nb) +
229 (options->buffer_sz % options->segments_nb) +
230 options->auth_digest_sz),
233 if (ctx->pkt_mbuf_pool_in == NULL)
236 /* Generate mbufs_in with plaintext populated for test */
237 if (ctx->options->pool_sz % ctx->options->burst_sz)
240 ctx->mbufs_in = rte_malloc(NULL,
241 (sizeof(struct rte_mbuf *) * 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), "cperf_pool_out_cdev_%d",
256 ctx->pkt_mbuf_pool_out = rte_pktmbuf_pool_create(
257 pool_name, options->pool_sz, 0, 0,
258 RTE_PKTMBUF_HEADROOM +
259 RTE_CACHE_LINE_ROUNDUP(
261 options->auth_digest_sz),
264 if (ctx->pkt_mbuf_pool_out == NULL)
268 ctx->mbufs_out = rte_malloc(NULL,
269 (sizeof(struct rte_mbuf *) *
270 ctx->options->pool_sz), 0);
272 for (mbuf_idx = 0; mbuf_idx < options->pool_sz; mbuf_idx++) {
273 if (options->out_of_place == 1) {
274 ctx->mbufs_out[mbuf_idx] = cperf_mbuf_create(
275 ctx->pkt_mbuf_pool_out, 1,
276 options, test_vector);
277 if (ctx->mbufs_out[mbuf_idx] == NULL)
280 ctx->mbufs_out[mbuf_idx] = NULL;
284 snprintf(pool_name, sizeof(pool_name), "cperf_op_pool_cdev_%d",
287 ctx->crypto_op_pool = rte_crypto_op_pool_create(pool_name,
288 RTE_CRYPTO_OP_TYPE_SYMMETRIC, options->pool_sz, 0, 0,
290 if (ctx->crypto_op_pool == NULL)
295 cperf_throughput_test_free(ctx, mbuf_idx);
301 cperf_throughput_test_verifier(struct rte_mbuf *mbuf,
302 const struct cperf_options *options,
303 const struct cperf_test_vector *vector)
305 const struct rte_mbuf *m;
309 uint32_t cipher_offset, auth_offset;
310 uint8_t cipher, auth;
314 nb_segs = m->nb_segs;
316 while (m && nb_segs != 0) {
322 data = rte_malloc(NULL, len, 0);
327 nb_segs = m->nb_segs;
329 while (m && nb_segs != 0) {
330 memcpy(data + len, rte_pktmbuf_mtod(m, uint8_t *),
337 switch (options->op_type) {
338 case CPERF_CIPHER_ONLY:
344 case CPERF_CIPHER_THEN_AUTH:
348 auth_offset = vector->plaintext.length;
350 case CPERF_AUTH_ONLY:
354 auth_offset = vector->plaintext.length;
356 case CPERF_AUTH_THEN_CIPHER:
360 auth_offset = vector->plaintext.length;
364 cipher_offset = vector->aad.length;
366 auth_offset = vector->aad.length + vector->plaintext.length;
371 if (options->cipher_op == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
372 res += memcmp(data + cipher_offset,
373 vector->ciphertext.data,
374 vector->ciphertext.length);
376 res += memcmp(data + cipher_offset,
377 vector->plaintext.data,
378 vector->plaintext.length);
382 if (options->auth_op == RTE_CRYPTO_AUTH_OP_GENERATE)
383 res += memcmp(data + auth_offset,
385 vector->digest.length);
395 cperf_throughput_test_runner(void *test_ctx)
397 struct cperf_throughput_ctx *ctx = test_ctx;
398 struct cperf_op_result *res, *pres;
400 if (ctx->options->verify) {
401 res = rte_malloc(NULL, sizeof(struct cperf_op_result) *
402 ctx->options->total_ops, 0);
407 uint64_t ops_enqd = 0, ops_enqd_total = 0, ops_enqd_failed = 0;
408 uint64_t ops_deqd = 0, ops_deqd_total = 0, ops_deqd_failed = 0;
410 uint64_t i, m_idx = 0, tsc_start, tsc_end, tsc_duration;
412 uint16_t ops_unused = 0;
415 struct rte_crypto_op *ops[ctx->options->burst_sz];
416 struct rte_crypto_op *ops_processed[ctx->options->burst_sz];
418 uint32_t lcore = rte_lcore_id();
420 #ifdef CPERF_LINEARIZATION_ENABLE
421 struct rte_cryptodev_info dev_info;
424 /* Check if source mbufs require coalescing */
425 if (ctx->options->segments_nb > 1) {
426 rte_cryptodev_info_get(ctx->dev_id, &dev_info);
427 if ((dev_info.feature_flags &
428 RTE_CRYPTODEV_FF_MBUF_SCATTER_GATHER) == 0)
431 #endif /* CPERF_LINEARIZATION_ENABLE */
433 ctx->lcore_id = lcore;
435 if (!ctx->options->csv)
436 printf("\n# Running throughput test on device: %u, lcore: %u\n",
439 /* Warm up the host CPU before starting the test */
440 for (i = 0; i < ctx->options->total_ops; i++)
441 rte_cryptodev_enqueue_burst(ctx->dev_id, ctx->qp_id, NULL, 0);
443 tsc_start = rte_rdtsc_precise();
445 while (ops_enqd_total < ctx->options->total_ops) {
447 uint16_t burst_size = ((ops_enqd_total + ctx->options->burst_sz)
448 <= ctx->options->total_ops) ?
449 ctx->options->burst_sz :
450 ctx->options->total_ops -
453 uint16_t ops_needed = burst_size - ops_unused;
455 /* Allocate crypto ops from pool */
456 if (ops_needed != rte_crypto_op_bulk_alloc(
458 RTE_CRYPTO_OP_TYPE_SYMMETRIC,
462 /* Setup crypto op, attach mbuf etc */
463 (ctx->populate_ops)(ops, &ctx->mbufs_in[m_idx],
464 &ctx->mbufs_out[m_idx],
465 ops_needed, ctx->sess, ctx->options,
468 if (ctx->options->verify) {
469 for (i = 0; i < ops_needed; i++) {
470 ops[i]->opaque_data = (void *)&res[idx];
475 #ifdef CPERF_LINEARIZATION_ENABLE
477 /* PMD doesn't support scatter-gather and source buffer
479 * We need to linearize it before enqueuing.
481 for (i = 0; i < burst_size; i++)
482 rte_pktmbuf_linearize(ops[i]->sym->m_src);
484 #endif /* CPERF_LINEARIZATION_ENABLE */
486 /* Enqueue burst of ops on crypto device */
487 ops_enqd = rte_cryptodev_enqueue_burst(ctx->dev_id, ctx->qp_id,
489 if (ops_enqd < burst_size)
493 * Calculate number of ops not enqueued (mainly for hw
494 * accelerators whose ingress queue can fill up).
496 ops_unused = burst_size - ops_enqd;
497 ops_enqd_total += ops_enqd;
500 /* Dequeue processed burst of ops from crypto device */
501 ops_deqd = rte_cryptodev_dequeue_burst(ctx->dev_id, ctx->qp_id,
502 ops_processed, ctx->options->burst_sz);
504 if (likely(ops_deqd)) {
506 if (ctx->options->verify) {
508 for (i = 0; i < ops_deqd; i++) {
509 opq = (ops_processed[i]->opaque_data);
510 pres = (struct cperf_op_result *)opq;
511 pres->status = ops_processed[i]->status;
515 /* free crypto ops so they can be reused. We don't free
516 * the mbufs here as we don't want to reuse them as
517 * the crypto operation will change the data and cause
520 for (i = 0; i < ops_deqd; i++)
521 rte_crypto_op_free(ops_processed[i]);
523 ops_deqd_total += ops_deqd;
526 * Count dequeue polls which didn't return any
527 * processed operations. This statistic is mainly
528 * relevant to hw accelerators.
534 m_idx = m_idx + ctx->options->burst_sz > ctx->options->pool_sz ?
538 /* Dequeue any operations still in the crypto device */
540 while (ops_deqd_total < ctx->options->total_ops) {
541 /* Sending 0 length burst to flush sw crypto device */
542 rte_cryptodev_enqueue_burst(ctx->dev_id, ctx->qp_id, NULL, 0);
545 ops_deqd = rte_cryptodev_dequeue_burst(ctx->dev_id, ctx->qp_id,
546 ops_processed, ctx->options->burst_sz);
550 if (ctx->options->verify) {
552 for (i = 0; i < ops_deqd; i++) {
553 opq = (ops_processed[i]->opaque_data);
554 pres = (struct cperf_op_result *)opq;
555 pres->status = ops_processed[i]->status;
559 for (i = 0; i < ops_deqd; i++)
560 rte_crypto_op_free(ops_processed[i]);
562 ops_deqd_total += ops_deqd;
566 tsc_end = rte_rdtsc_precise();
567 tsc_duration = (tsc_end - tsc_start);
569 if (ctx->options->verify) {
570 struct rte_mbuf **mbufs;
572 if (ctx->options->out_of_place == 1)
573 mbufs = ctx->mbufs_out;
575 mbufs = ctx->mbufs_in;
577 for (i = 0; i < ctx->options->total_ops; i++) {
579 if (res[i].status != RTE_CRYPTO_OP_STATUS_SUCCESS ||
580 cperf_throughput_test_verifier(
581 mbufs[i], ctx->options,
584 ctx->results.ops_failed++;
591 /* Calculate average operations processed per second */
592 ctx->results.ops_per_second = ((double)ctx->options->total_ops /
593 tsc_duration) * rte_get_tsc_hz();
595 /* Calculate average throughput (Gbps) in bits per second */
596 ctx->results.throughput_gbps = ((ctx->results.ops_per_second *
597 ctx->options->buffer_sz * 8) / 1000000000);
600 /* Calculate average cycles per byte */
601 ctx->results.cycles_per_byte = ((double)tsc_duration /
602 ctx->options->total_ops) / ctx->options->buffer_sz;
604 ctx->results.ops_enqueued = ops_enqd_total;
605 ctx->results.ops_dequeued = ops_deqd_total;
607 ctx->results.ops_enqueued_failed = ops_enqd_failed;
608 ctx->results.ops_dequeued_failed = ops_deqd_failed;
616 cperf_throughput_test_destructor(void *arg)
618 struct cperf_throughput_ctx *ctx = arg;
619 struct cperf_throughput_results *results = &ctx->results;
620 static int only_once;
625 if (!ctx->options->csv) {
626 printf("\n# Device %d on lcore %u\n",
627 ctx->dev_id, ctx->lcore_id);
628 printf("# Buffer Size(B)\t Enqueued\t Dequeued\tFailed Enq"
629 "\tFailed Deq\tOps(Millions)\tThroughput(Gbps)"
630 "\tCycles Per Byte\n");
632 printf("\n%16u\t%10"PRIu64"\t%10"PRIu64"\t%10"PRIu64"\t"
633 "%10"PRIu64"\t%16.4f\t%16.4f\t%15.2f\n",
634 ctx->options->buffer_sz,
635 results->ops_enqueued,
636 results->ops_dequeued,
637 results->ops_enqueued_failed,
638 results->ops_dequeued_failed,
639 results->ops_per_second/1000000,
640 results->throughput_gbps,
641 results->cycles_per_byte);
644 printf("\n# CPU lcore id, Burst Size(B), "
645 "Buffer Size(B),Enqueued,Dequeued,Failed Enq,"
646 "Failed Deq,Ops(Millions),Throughput(Gbps),"
647 "Cycles Per Byte\n");
650 printf("%u;%u;%u;%"PRIu64";%"PRIu64";%"PRIu64";%"PRIu64";"
653 ctx->options->burst_sz,
654 ctx->options->buffer_sz,
655 results->ops_enqueued,
656 results->ops_dequeued,
657 results->ops_enqueued_failed,
658 results->ops_dequeued_failed,
659 results->ops_per_second/1000000,
660 results->throughput_gbps,
661 results->cycles_per_byte);
664 cperf_throughput_test_free(ctx, ctx->options->pool_sz);