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_ctx {
46 struct rte_mempool *pkt_mbuf_pool_in;
47 struct rte_mempool *pkt_mbuf_pool_out;
48 struct rte_mbuf **mbufs_in;
49 struct rte_mbuf **mbufs_out;
51 struct rte_mempool *crypto_op_pool;
53 struct rte_cryptodev_sym_session *sess;
55 cperf_populate_ops_t populate_ops;
57 const struct cperf_options *options;
58 const struct cperf_test_vector *test_vector;
62 cperf_throughput_test_free(struct cperf_throughput_ctx *ctx, uint32_t mbuf_nb)
68 rte_cryptodev_sym_session_free(ctx->dev_id, ctx->sess);
71 for (i = 0; i < mbuf_nb; i++)
72 rte_pktmbuf_free(ctx->mbufs_in[i]);
74 rte_free(ctx->mbufs_in);
78 for (i = 0; i < mbuf_nb; i++) {
79 if (ctx->mbufs_out[i] != NULL)
80 rte_pktmbuf_free(ctx->mbufs_out[i]);
83 rte_free(ctx->mbufs_out);
86 if (ctx->pkt_mbuf_pool_in)
87 rte_mempool_free(ctx->pkt_mbuf_pool_in);
89 if (ctx->pkt_mbuf_pool_out)
90 rte_mempool_free(ctx->pkt_mbuf_pool_out);
92 if (ctx->crypto_op_pool)
93 rte_mempool_free(ctx->crypto_op_pool);
99 static struct rte_mbuf *
100 cperf_mbuf_create(struct rte_mempool *mempool,
101 uint32_t segments_nb,
102 const struct cperf_options *options,
103 const struct cperf_test_vector *test_vector)
105 struct rte_mbuf *mbuf;
106 uint32_t segment_sz = options->max_buffer_size / segments_nb;
107 uint32_t last_sz = options->max_buffer_size % segments_nb;
110 (options->cipher_op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
111 test_vector->plaintext.data :
112 test_vector->ciphertext.data;
114 mbuf = rte_pktmbuf_alloc(mempool);
118 mbuf_data = (uint8_t *)rte_pktmbuf_append(mbuf, segment_sz);
119 if (mbuf_data == NULL)
122 memcpy(mbuf_data, test_data, segment_sz);
123 test_data += segment_sz;
126 while (segments_nb) {
129 m = rte_pktmbuf_alloc(mempool);
133 rte_pktmbuf_chain(mbuf, m);
135 mbuf_data = (uint8_t *)rte_pktmbuf_append(mbuf, segment_sz);
136 if (mbuf_data == NULL)
139 memcpy(mbuf_data, test_data, segment_sz);
140 test_data += segment_sz;
145 mbuf_data = (uint8_t *)rte_pktmbuf_append(mbuf, last_sz);
146 if (mbuf_data == NULL)
149 memcpy(mbuf_data, test_data, last_sz);
152 if (options->op_type != CPERF_CIPHER_ONLY) {
153 mbuf_data = (uint8_t *)rte_pktmbuf_append(mbuf,
155 if (mbuf_data == NULL)
159 if (options->op_type == CPERF_AEAD) {
160 uint8_t *aead = (uint8_t *)rte_pktmbuf_prepend(mbuf,
161 RTE_ALIGN_CEIL(options->aead_aad_sz, 16));
166 memcpy(aead, test_vector->aad.data, test_vector->aad.length);
172 rte_pktmbuf_free(mbuf);
178 cperf_throughput_test_constructor(uint8_t dev_id, uint16_t qp_id,
179 const struct cperf_options *options,
180 const struct cperf_test_vector *test_vector,
181 const struct cperf_op_fns *op_fns)
183 struct cperf_throughput_ctx *ctx = NULL;
184 unsigned int mbuf_idx = 0;
185 char pool_name[32] = "";
187 ctx = rte_malloc(NULL, sizeof(struct cperf_throughput_ctx), 0);
191 ctx->dev_id = dev_id;
194 ctx->populate_ops = op_fns->populate_ops;
195 ctx->options = options;
196 ctx->test_vector = test_vector;
198 /* IV goes at the end of the cryptop operation */
199 uint16_t iv_offset = sizeof(struct rte_crypto_op) +
200 sizeof(struct rte_crypto_sym_op);
202 ctx->sess = op_fns->sess_create(dev_id, options, test_vector, iv_offset);
203 if (ctx->sess == NULL)
206 snprintf(pool_name, sizeof(pool_name), "cperf_pool_in_cdev_%d",
209 ctx->pkt_mbuf_pool_in = rte_pktmbuf_pool_create(pool_name,
210 options->pool_sz * options->segments_nb, 0, 0,
211 RTE_PKTMBUF_HEADROOM +
212 RTE_CACHE_LINE_ROUNDUP(
213 (options->max_buffer_size / options->segments_nb) +
214 (options->max_buffer_size % options->segments_nb) +
218 if (ctx->pkt_mbuf_pool_in == NULL)
221 /* Generate mbufs_in with plaintext populated for test */
222 ctx->mbufs_in = rte_malloc(NULL,
223 (sizeof(struct rte_mbuf *) * ctx->options->pool_sz), 0);
225 for (mbuf_idx = 0; mbuf_idx < options->pool_sz; mbuf_idx++) {
226 ctx->mbufs_in[mbuf_idx] = cperf_mbuf_create(
227 ctx->pkt_mbuf_pool_in, options->segments_nb,
228 options, test_vector);
229 if (ctx->mbufs_in[mbuf_idx] == NULL)
233 if (options->out_of_place == 1) {
235 snprintf(pool_name, sizeof(pool_name), "cperf_pool_out_cdev_%d",
238 ctx->pkt_mbuf_pool_out = rte_pktmbuf_pool_create(
239 pool_name, options->pool_sz, 0, 0,
240 RTE_PKTMBUF_HEADROOM +
241 RTE_CACHE_LINE_ROUNDUP(
242 options->max_buffer_size +
246 if (ctx->pkt_mbuf_pool_out == NULL)
250 ctx->mbufs_out = rte_malloc(NULL,
251 (sizeof(struct rte_mbuf *) *
252 ctx->options->pool_sz), 0);
254 for (mbuf_idx = 0; mbuf_idx < options->pool_sz; mbuf_idx++) {
255 if (options->out_of_place == 1) {
256 ctx->mbufs_out[mbuf_idx] = cperf_mbuf_create(
257 ctx->pkt_mbuf_pool_out, 1,
258 options, test_vector);
259 if (ctx->mbufs_out[mbuf_idx] == NULL)
262 ctx->mbufs_out[mbuf_idx] = NULL;
266 snprintf(pool_name, sizeof(pool_name), "cperf_op_pool_cdev_%d",
269 uint16_t priv_size = test_vector->cipher_iv.length +
270 test_vector->auth_iv.length;
272 ctx->crypto_op_pool = rte_crypto_op_pool_create(pool_name,
273 RTE_CRYPTO_OP_TYPE_SYMMETRIC, options->pool_sz,
274 512, priv_size, rte_socket_id());
275 if (ctx->crypto_op_pool == NULL)
280 cperf_throughput_test_free(ctx, mbuf_idx);
286 cperf_throughput_test_runner(void *test_ctx)
288 struct cperf_throughput_ctx *ctx = test_ctx;
289 uint16_t test_burst_size;
290 uint8_t burst_size_idx = 0;
292 static int only_once;
294 struct rte_crypto_op *ops[ctx->options->max_burst_size];
295 struct rte_crypto_op *ops_processed[ctx->options->max_burst_size];
298 uint32_t lcore = rte_lcore_id();
300 #ifdef CPERF_LINEARIZATION_ENABLE
301 struct rte_cryptodev_info dev_info;
304 /* Check if source mbufs require coalescing */
305 if (ctx->options->segments_nb > 1) {
306 rte_cryptodev_info_get(ctx->dev_id, &dev_info);
307 if ((dev_info.feature_flags &
308 RTE_CRYPTODEV_FF_MBUF_SCATTER_GATHER) == 0)
311 #endif /* CPERF_LINEARIZATION_ENABLE */
313 ctx->lcore_id = lcore;
315 /* Warm up the host CPU before starting the test */
316 for (i = 0; i < ctx->options->total_ops; i++)
317 rte_cryptodev_enqueue_burst(ctx->dev_id, ctx->qp_id, NULL, 0);
319 /* Get first size from range or list */
320 if (ctx->options->inc_burst_size != 0)
321 test_burst_size = ctx->options->min_burst_size;
323 test_burst_size = ctx->options->burst_size_list[0];
325 uint16_t iv_offset = sizeof(struct rte_crypto_op) +
326 sizeof(struct rte_crypto_sym_op);
328 while (test_burst_size <= ctx->options->max_burst_size) {
329 uint64_t ops_enqd = 0, ops_enqd_total = 0, ops_enqd_failed = 0;
330 uint64_t ops_deqd = 0, ops_deqd_total = 0, ops_deqd_failed = 0;
332 uint64_t m_idx = 0, tsc_start, tsc_end, tsc_duration;
334 uint16_t ops_unused = 0;
336 tsc_start = rte_rdtsc_precise();
338 while (ops_enqd_total < ctx->options->total_ops) {
340 uint16_t burst_size = ((ops_enqd_total + test_burst_size)
341 <= ctx->options->total_ops) ?
343 ctx->options->total_ops -
346 uint16_t ops_needed = burst_size - ops_unused;
348 /* Allocate crypto ops from pool */
349 if (ops_needed != rte_crypto_op_bulk_alloc(
351 RTE_CRYPTO_OP_TYPE_SYMMETRIC,
355 /* Setup crypto op, attach mbuf etc */
356 (ctx->populate_ops)(ops, &ctx->mbufs_in[m_idx],
357 &ctx->mbufs_out[m_idx],
358 ops_needed, ctx->sess, ctx->options,
359 ctx->test_vector, iv_offset);
362 * When ops_needed is smaller than ops_enqd, the
363 * unused ops need to be moved to the front for
366 if (unlikely(ops_enqd > ops_needed)) {
367 size_t nb_b_to_mov = ops_unused * sizeof(
368 struct rte_crypto_op *);
370 memmove(&ops[ops_needed], &ops[ops_enqd],
374 #ifdef CPERF_LINEARIZATION_ENABLE
376 /* PMD doesn't support scatter-gather and source buffer
378 * We need to linearize it before enqueuing.
380 for (i = 0; i < burst_size; i++)
381 rte_pktmbuf_linearize(ops[i]->sym->m_src);
383 #endif /* CPERF_LINEARIZATION_ENABLE */
385 /* Enqueue burst of ops on crypto device */
386 ops_enqd = rte_cryptodev_enqueue_burst(ctx->dev_id, ctx->qp_id,
388 if (ops_enqd < burst_size)
392 * Calculate number of ops not enqueued (mainly for hw
393 * accelerators whose ingress queue can fill up).
395 ops_unused = burst_size - ops_enqd;
396 ops_enqd_total += ops_enqd;
399 /* Dequeue processed burst of ops from crypto device */
400 ops_deqd = rte_cryptodev_dequeue_burst(ctx->dev_id, ctx->qp_id,
401 ops_processed, test_burst_size);
403 if (likely(ops_deqd)) {
404 /* free crypto ops so they can be reused. We don't free
405 * the mbufs here as we don't want to reuse them as
406 * the crypto operation will change the data and cause
409 rte_mempool_put_bulk(ctx->crypto_op_pool,
410 (void **)ops_processed, ops_deqd);
412 ops_deqd_total += ops_deqd;
415 * Count dequeue polls which didn't return any
416 * processed operations. This statistic is mainly
417 * relevant to hw accelerators.
423 m_idx = m_idx + test_burst_size > ctx->options->pool_sz ?
427 /* Dequeue any operations still in the crypto device */
429 while (ops_deqd_total < ctx->options->total_ops) {
430 /* Sending 0 length burst to flush sw crypto device */
431 rte_cryptodev_enqueue_burst(ctx->dev_id, ctx->qp_id, NULL, 0);
434 ops_deqd = rte_cryptodev_dequeue_burst(ctx->dev_id, ctx->qp_id,
435 ops_processed, test_burst_size);
439 rte_mempool_put_bulk(ctx->crypto_op_pool,
440 (void **)ops_processed, ops_deqd);
442 ops_deqd_total += ops_deqd;
446 tsc_end = rte_rdtsc_precise();
447 tsc_duration = (tsc_end - tsc_start);
449 /* Calculate average operations processed per second */
450 double ops_per_second = ((double)ctx->options->total_ops /
451 tsc_duration) * rte_get_tsc_hz();
453 /* Calculate average throughput (Gbps) in bits per second */
454 double throughput_gbps = ((ops_per_second *
455 ctx->options->test_buffer_size * 8) / 1000000000);
457 /* Calculate average cycles per packet */
458 double cycles_per_packet = ((double)tsc_duration /
459 ctx->options->total_ops);
461 if (!ctx->options->csv) {
463 printf("%12s%12s%12s%12s%12s%12s%12s%12s%12s%12s\n\n",
464 "lcore id", "Buf Size", "Burst Size",
465 "Enqueued", "Dequeued", "Failed Enq",
466 "Failed Deq", "MOps", "Gbps",
470 printf("%12u%12u%12u%12"PRIu64"%12"PRIu64"%12"PRIu64
471 "%12"PRIu64"%12.4f%12.4f%12.2f\n",
473 ctx->options->test_buffer_size,
479 ops_per_second/1000000,
484 printf("# lcore id, Buffer Size(B),"
485 "Burst Size,Enqueued,Dequeued,Failed Enq,"
486 "Failed Deq,Ops(Millions),Throughput(Gbps),"
490 printf("%10u;%10u;%u;%"PRIu64";%"PRIu64";%"PRIu64";%"PRIu64";"
493 ctx->options->test_buffer_size,
499 ops_per_second/1000000,
504 /* Get next size from range or list */
505 if (ctx->options->inc_burst_size != 0)
506 test_burst_size += ctx->options->inc_burst_size;
508 if (++burst_size_idx == ctx->options->burst_size_count)
510 test_burst_size = ctx->options->burst_size_list[burst_size_idx];
520 cperf_throughput_test_destructor(void *arg)
522 struct cperf_throughput_ctx *ctx = arg;
527 cperf_throughput_test_free(ctx, ctx->options->pool_sz);