xref: /dpdk/app/test-crypto-perf/cperf_test_throughput.c (revision 44e2980b70d14cf287779bd53a162b81d94e23cf)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2016-2017 Intel Corporation. All rights reserved.
5  *
6  *   Redistribution and use in source and binary forms, with or without
7  *   modification, are permitted provided that the following conditions
8  *   are met:
9  *
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
15  *       distribution.
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.
19  *
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.
31  */
32 
33 #include <rte_malloc.h>
34 #include <rte_cycles.h>
35 #include <rte_crypto.h>
36 #include <rte_cryptodev.h>
37 
38 #include "cperf_test_throughput.h"
39 #include "cperf_ops.h"
40 
41 struct cperf_throughput_ctx {
42 	uint8_t dev_id;
43 	uint16_t qp_id;
44 	uint8_t lcore_id;
45 
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;
50 
51 	struct rte_mempool *crypto_op_pool;
52 
53 	struct rte_cryptodev_sym_session *sess;
54 
55 	cperf_populate_ops_t populate_ops;
56 
57 	const struct cperf_options *options;
58 	const struct cperf_test_vector *test_vector;
59 };
60 
61 static void
62 cperf_throughput_test_free(struct cperf_throughput_ctx *ctx, uint32_t mbuf_nb)
63 {
64 	uint32_t i;
65 
66 	if (ctx) {
67 		if (ctx->sess)
68 			rte_cryptodev_sym_session_free(ctx->dev_id, ctx->sess);
69 
70 		if (ctx->mbufs_in) {
71 			for (i = 0; i < mbuf_nb; i++)
72 				rte_pktmbuf_free(ctx->mbufs_in[i]);
73 
74 			rte_free(ctx->mbufs_in);
75 		}
76 
77 		if (ctx->mbufs_out) {
78 			for (i = 0; i < mbuf_nb; i++) {
79 				if (ctx->mbufs_out[i] != NULL)
80 					rte_pktmbuf_free(ctx->mbufs_out[i]);
81 			}
82 
83 			rte_free(ctx->mbufs_out);
84 		}
85 
86 		if (ctx->pkt_mbuf_pool_in)
87 			rte_mempool_free(ctx->pkt_mbuf_pool_in);
88 
89 		if (ctx->pkt_mbuf_pool_out)
90 			rte_mempool_free(ctx->pkt_mbuf_pool_out);
91 
92 		if (ctx->crypto_op_pool)
93 			rte_mempool_free(ctx->crypto_op_pool);
94 
95 		rte_free(ctx);
96 	}
97 }
98 
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)
104 {
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;
108 	uint8_t *mbuf_data;
109 	uint8_t *test_data =
110 			(options->cipher_op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
111 					test_vector->plaintext.data :
112 					test_vector->ciphertext.data;
113 
114 	mbuf = rte_pktmbuf_alloc(mempool);
115 	if (mbuf == NULL)
116 		goto error;
117 
118 	mbuf_data = (uint8_t *)rte_pktmbuf_append(mbuf, segment_sz);
119 	if (mbuf_data == NULL)
120 		goto error;
121 
122 	memcpy(mbuf_data, test_data, segment_sz);
123 	test_data += segment_sz;
124 	segments_nb--;
125 
126 	while (segments_nb) {
127 		struct rte_mbuf *m;
128 
129 		m = rte_pktmbuf_alloc(mempool);
130 		if (m == NULL)
131 			goto error;
132 
133 		rte_pktmbuf_chain(mbuf, m);
134 
135 		mbuf_data = (uint8_t *)rte_pktmbuf_append(mbuf, segment_sz);
136 		if (mbuf_data == NULL)
137 			goto error;
138 
139 		memcpy(mbuf_data, test_data, segment_sz);
140 		test_data += segment_sz;
141 		segments_nb--;
142 	}
143 
144 	if (last_sz) {
145 		mbuf_data = (uint8_t *)rte_pktmbuf_append(mbuf, last_sz);
146 		if (mbuf_data == NULL)
147 			goto error;
148 
149 		memcpy(mbuf_data, test_data, last_sz);
150 	}
151 
152 	if (options->op_type != CPERF_CIPHER_ONLY) {
153 		mbuf_data = (uint8_t *)rte_pktmbuf_append(mbuf,
154 				options->auth_digest_sz);
155 		if (mbuf_data == NULL)
156 			goto error;
157 	}
158 
159 	if (options->op_type == CPERF_AEAD) {
160 		uint8_t *aead = (uint8_t *)rte_pktmbuf_prepend(mbuf,
161 			RTE_ALIGN_CEIL(options->auth_aad_sz, 16));
162 
163 		if (aead == NULL)
164 			goto error;
165 
166 		memcpy(aead, test_vector->aad.data, test_vector->aad.length);
167 	}
168 
169 	return mbuf;
170 error:
171 	if (mbuf != NULL)
172 		rte_pktmbuf_free(mbuf);
173 
174 	return NULL;
175 }
176 
177 void *
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)
182 {
183 	struct cperf_throughput_ctx *ctx = NULL;
184 	unsigned int mbuf_idx = 0;
185 	char pool_name[32] = "";
186 
187 	ctx = rte_malloc(NULL, sizeof(struct cperf_throughput_ctx), 0);
188 	if (ctx == NULL)
189 		goto err;
190 
191 	ctx->dev_id = dev_id;
192 	ctx->qp_id = qp_id;
193 
194 	ctx->populate_ops = op_fns->populate_ops;
195 	ctx->options = options;
196 	ctx->test_vector = test_vector;
197 
198 	ctx->sess = op_fns->sess_create(dev_id, options, test_vector);
199 	if (ctx->sess == NULL)
200 		goto err;
201 
202 	snprintf(pool_name, sizeof(pool_name), "cperf_pool_in_cdev_%d",
203 			dev_id);
204 
205 	ctx->pkt_mbuf_pool_in = rte_pktmbuf_pool_create(pool_name,
206 			options->pool_sz * options->segments_nb, 0, 0,
207 			RTE_PKTMBUF_HEADROOM +
208 			RTE_CACHE_LINE_ROUNDUP(
209 				(options->max_buffer_size / options->segments_nb) +
210 				(options->max_buffer_size % options->segments_nb) +
211 					options->auth_digest_sz),
212 			rte_socket_id());
213 
214 	if (ctx->pkt_mbuf_pool_in == NULL)
215 		goto err;
216 
217 	/* Generate mbufs_in with plaintext populated for test */
218 	ctx->mbufs_in = rte_malloc(NULL,
219 			(sizeof(struct rte_mbuf *) * ctx->options->pool_sz), 0);
220 
221 	for (mbuf_idx = 0; mbuf_idx < options->pool_sz; mbuf_idx++) {
222 		ctx->mbufs_in[mbuf_idx] = cperf_mbuf_create(
223 				ctx->pkt_mbuf_pool_in, options->segments_nb,
224 				options, test_vector);
225 		if (ctx->mbufs_in[mbuf_idx] == NULL)
226 			goto err;
227 	}
228 
229 	if (options->out_of_place == 1)	{
230 
231 		snprintf(pool_name, sizeof(pool_name), "cperf_pool_out_cdev_%d",
232 				dev_id);
233 
234 		ctx->pkt_mbuf_pool_out = rte_pktmbuf_pool_create(
235 				pool_name, options->pool_sz, 0, 0,
236 				RTE_PKTMBUF_HEADROOM +
237 				RTE_CACHE_LINE_ROUNDUP(
238 					options->max_buffer_size +
239 					options->auth_digest_sz),
240 				rte_socket_id());
241 
242 		if (ctx->pkt_mbuf_pool_out == NULL)
243 			goto err;
244 	}
245 
246 	ctx->mbufs_out = rte_malloc(NULL,
247 			(sizeof(struct rte_mbuf *) *
248 			ctx->options->pool_sz), 0);
249 
250 	for (mbuf_idx = 0; mbuf_idx < options->pool_sz; mbuf_idx++) {
251 		if (options->out_of_place == 1)	{
252 			ctx->mbufs_out[mbuf_idx] = cperf_mbuf_create(
253 					ctx->pkt_mbuf_pool_out, 1,
254 					options, test_vector);
255 			if (ctx->mbufs_out[mbuf_idx] == NULL)
256 				goto err;
257 		} else {
258 			ctx->mbufs_out[mbuf_idx] = NULL;
259 		}
260 	}
261 
262 	snprintf(pool_name, sizeof(pool_name), "cperf_op_pool_cdev_%d",
263 			dev_id);
264 
265 	ctx->crypto_op_pool = rte_crypto_op_pool_create(pool_name,
266 			RTE_CRYPTO_OP_TYPE_SYMMETRIC, options->pool_sz, 0, 0,
267 			rte_socket_id());
268 	if (ctx->crypto_op_pool == NULL)
269 		goto err;
270 
271 	return ctx;
272 err:
273 	cperf_throughput_test_free(ctx, mbuf_idx);
274 
275 	return NULL;
276 }
277 
278 int
279 cperf_throughput_test_runner(void *test_ctx)
280 {
281 	struct cperf_throughput_ctx *ctx = test_ctx;
282 	uint16_t test_burst_size;
283 	uint8_t burst_size_idx = 0;
284 
285 	static int only_once;
286 
287 	struct rte_crypto_op *ops[ctx->options->max_burst_size];
288 	struct rte_crypto_op *ops_processed[ctx->options->max_burst_size];
289 	uint64_t i;
290 
291 	uint32_t lcore = rte_lcore_id();
292 
293 #ifdef CPERF_LINEARIZATION_ENABLE
294 	struct rte_cryptodev_info dev_info;
295 	int linearize = 0;
296 
297 	/* Check if source mbufs require coalescing */
298 	if (ctx->options->segments_nb > 1) {
299 		rte_cryptodev_info_get(ctx->dev_id, &dev_info);
300 		if ((dev_info.feature_flags &
301 				RTE_CRYPTODEV_FF_MBUF_SCATTER_GATHER) == 0)
302 			linearize = 1;
303 	}
304 #endif /* CPERF_LINEARIZATION_ENABLE */
305 
306 	ctx->lcore_id = lcore;
307 
308 	/* Warm up the host CPU before starting the test */
309 	for (i = 0; i < ctx->options->total_ops; i++)
310 		rte_cryptodev_enqueue_burst(ctx->dev_id, ctx->qp_id, NULL, 0);
311 
312 	/* Get first size from range or list */
313 	if (ctx->options->inc_burst_size != 0)
314 		test_burst_size = ctx->options->min_burst_size;
315 	else
316 		test_burst_size = ctx->options->burst_size_list[0];
317 
318 	while (test_burst_size <= ctx->options->max_burst_size) {
319 		uint64_t ops_enqd = 0, ops_enqd_total = 0, ops_enqd_failed = 0;
320 		uint64_t ops_deqd = 0, ops_deqd_total = 0, ops_deqd_failed = 0;
321 
322 		uint64_t m_idx = 0, tsc_start, tsc_end, tsc_duration;
323 
324 		uint16_t ops_unused = 0;
325 
326 		tsc_start = rte_rdtsc_precise();
327 
328 		while (ops_enqd_total < ctx->options->total_ops) {
329 
330 			uint16_t burst_size = ((ops_enqd_total + test_burst_size)
331 					<= ctx->options->total_ops) ?
332 							test_burst_size :
333 							ctx->options->total_ops -
334 							ops_enqd_total;
335 
336 			uint16_t ops_needed = burst_size - ops_unused;
337 
338 			/* Allocate crypto ops from pool */
339 			if (ops_needed != rte_crypto_op_bulk_alloc(
340 					ctx->crypto_op_pool,
341 					RTE_CRYPTO_OP_TYPE_SYMMETRIC,
342 					ops, ops_needed))
343 				return -1;
344 
345 			/* Setup crypto op, attach mbuf etc */
346 			(ctx->populate_ops)(ops, &ctx->mbufs_in[m_idx],
347 					&ctx->mbufs_out[m_idx],
348 					ops_needed, ctx->sess, ctx->options,
349 					ctx->test_vector);
350 
351 			/**
352 			 * When ops_needed is smaller than ops_enqd, the
353 			 * unused ops need to be moved to the front for
354 			 * next round use.
355 			 */
356 			if (unlikely(ops_enqd > ops_needed)) {
357 				size_t nb_b_to_mov = ops_unused * sizeof(
358 						struct rte_crypto_op *);
359 
360 				memmove(&ops[ops_needed], &ops[ops_enqd],
361 					nb_b_to_mov);
362 			}
363 
364 #ifdef CPERF_LINEARIZATION_ENABLE
365 			if (linearize) {
366 				/* PMD doesn't support scatter-gather and source buffer
367 				 * is segmented.
368 				 * We need to linearize it before enqueuing.
369 				 */
370 				for (i = 0; i < burst_size; i++)
371 					rte_pktmbuf_linearize(ops[i]->sym->m_src);
372 			}
373 #endif /* CPERF_LINEARIZATION_ENABLE */
374 
375 			/* Enqueue burst of ops on crypto device */
376 			ops_enqd = rte_cryptodev_enqueue_burst(ctx->dev_id, ctx->qp_id,
377 					ops, burst_size);
378 			if (ops_enqd < burst_size)
379 				ops_enqd_failed++;
380 
381 			/**
382 			 * Calculate number of ops not enqueued (mainly for hw
383 			 * accelerators whose ingress queue can fill up).
384 			 */
385 			ops_unused = burst_size - ops_enqd;
386 			ops_enqd_total += ops_enqd;
387 
388 
389 			/* Dequeue processed burst of ops from crypto device */
390 			ops_deqd = rte_cryptodev_dequeue_burst(ctx->dev_id, ctx->qp_id,
391 					ops_processed, test_burst_size);
392 
393 			if (likely(ops_deqd))  {
394 				/* free crypto ops so they can be reused. We don't free
395 				 * the mbufs here as we don't want to reuse them as
396 				 * the crypto operation will change the data and cause
397 				 * failures.
398 				 */
399 				for (i = 0; i < ops_deqd; i++)
400 					rte_crypto_op_free(ops_processed[i]);
401 
402 				ops_deqd_total += ops_deqd;
403 			} else {
404 				/**
405 				 * Count dequeue polls which didn't return any
406 				 * processed operations. This statistic is mainly
407 				 * relevant to hw accelerators.
408 				 */
409 				ops_deqd_failed++;
410 			}
411 
412 			m_idx += ops_needed;
413 			m_idx = m_idx + test_burst_size > ctx->options->pool_sz ?
414 					0 : m_idx;
415 		}
416 
417 		/* Dequeue any operations still in the crypto device */
418 
419 		while (ops_deqd_total < ctx->options->total_ops) {
420 			/* Sending 0 length burst to flush sw crypto device */
421 			rte_cryptodev_enqueue_burst(ctx->dev_id, ctx->qp_id, NULL, 0);
422 
423 			/* dequeue burst */
424 			ops_deqd = rte_cryptodev_dequeue_burst(ctx->dev_id, ctx->qp_id,
425 					ops_processed, test_burst_size);
426 			if (ops_deqd == 0)
427 				ops_deqd_failed++;
428 			else {
429 				for (i = 0; i < ops_deqd; i++)
430 					rte_crypto_op_free(ops_processed[i]);
431 
432 				ops_deqd_total += ops_deqd;
433 			}
434 		}
435 
436 		tsc_end = rte_rdtsc_precise();
437 		tsc_duration = (tsc_end - tsc_start);
438 
439 		/* Calculate average operations processed per second */
440 		double ops_per_second = ((double)ctx->options->total_ops /
441 				tsc_duration) * rte_get_tsc_hz();
442 
443 		/* Calculate average throughput (Gbps) in bits per second */
444 		double throughput_gbps = ((ops_per_second *
445 				ctx->options->test_buffer_size * 8) / 1000000000);
446 
447 		/* Calculate average cycles per packet */
448 		double cycles_per_packet = ((double)tsc_duration /
449 				ctx->options->total_ops);
450 
451 		if (!ctx->options->csv) {
452 			if (!only_once)
453 				printf("%12s%12s%12s%12s%12s%12s%12s%12s%12s%12s\n\n",
454 					"lcore id", "Buf Size", "Burst Size",
455 					"Enqueued", "Dequeued", "Failed Enq",
456 					"Failed Deq", "MOps", "Gbps",
457 					"Cycles/Buf");
458 			only_once = 1;
459 
460 			printf("%12u%12u%12u%12"PRIu64"%12"PRIu64"%12"PRIu64
461 					"%12"PRIu64"%12.4f%12.4f%12.2f\n",
462 					ctx->lcore_id,
463 					ctx->options->test_buffer_size,
464 					test_burst_size,
465 					ops_enqd_total,
466 					ops_deqd_total,
467 					ops_enqd_failed,
468 					ops_deqd_failed,
469 					ops_per_second/1000000,
470 					throughput_gbps,
471 					cycles_per_packet);
472 		} else {
473 			if (!only_once)
474 				printf("# lcore id, Buffer Size(B),"
475 					"Burst Size,Enqueued,Dequeued,Failed Enq,"
476 					"Failed Deq,Ops(Millions),Throughput(Gbps),"
477 					"Cycles/Buf\n\n");
478 			only_once = 1;
479 
480 			printf("%10u;%10u;%u;%"PRIu64";%"PRIu64";%"PRIu64";%"PRIu64";"
481 					"%.f3;%.f3;%.f3\n",
482 					ctx->lcore_id,
483 					ctx->options->test_buffer_size,
484 					test_burst_size,
485 					ops_enqd_total,
486 					ops_deqd_total,
487 					ops_enqd_failed,
488 					ops_deqd_failed,
489 					ops_per_second/1000000,
490 					throughput_gbps,
491 					cycles_per_packet);
492 		}
493 
494 		/* Get next size from range or list */
495 		if (ctx->options->inc_burst_size != 0)
496 			test_burst_size += ctx->options->inc_burst_size;
497 		else {
498 			if (++burst_size_idx == ctx->options->burst_size_count)
499 				break;
500 			test_burst_size = ctx->options->burst_size_list[burst_size_idx];
501 		}
502 
503 	}
504 
505 	return 0;
506 }
507 
508 
509 void
510 cperf_throughput_test_destructor(void *arg)
511 {
512 	struct cperf_throughput_ctx *ctx = arg;
513 
514 	if (ctx == NULL)
515 		return;
516 
517 	cperf_throughput_test_free(ctx, ctx->options->pool_sz);
518 }
519