xref: /dpdk/app/test-crypto-perf/cperf_test_throughput.c (revision 1f41d98c207aee8982ced709864c96c463d4503a)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2016-2017 Intel Corporation
3  */
4 
5 #include <rte_malloc.h>
6 #include <rte_cycles.h>
7 #include <rte_crypto.h>
8 #include <rte_cryptodev.h>
9 
10 #include "cperf_test_throughput.h"
11 #include "cperf_ops.h"
12 #include "cperf_test_common.h"
13 
14 struct cperf_throughput_ctx {
15 	uint8_t dev_id;
16 	uint16_t qp_id;
17 	uint8_t lcore_id;
18 
19 	struct rte_mempool *pool;
20 
21 	struct rte_cryptodev_sym_session *sess;
22 
23 	cperf_populate_ops_t populate_ops;
24 
25 	uint32_t src_buf_offset;
26 	uint32_t dst_buf_offset;
27 
28 	const struct cperf_options *options;
29 	const struct cperf_test_vector *test_vector;
30 };
31 
32 static void
33 cperf_throughput_test_free(struct cperf_throughput_ctx *ctx)
34 {
35 	if (!ctx)
36 		return;
37 	if (ctx->sess) {
38 #ifdef RTE_LIBRTE_SECURITY
39 		if (ctx->options->op_type == CPERF_PDCP) {
40 			struct rte_security_ctx *sec_ctx =
41 				(struct rte_security_ctx *)
42 				rte_cryptodev_get_sec_ctx(ctx->dev_id);
43 			rte_security_session_destroy(sec_ctx,
44 				(struct rte_security_session *)ctx->sess);
45 		} else
46 #endif
47 		{
48 			rte_cryptodev_sym_session_clear(ctx->dev_id, ctx->sess);
49 			rte_cryptodev_sym_session_free(ctx->sess);
50 		}
51 	}
52 	if (ctx->pool)
53 		rte_mempool_free(ctx->pool);
54 
55 	rte_free(ctx);
56 }
57 
58 void *
59 cperf_throughput_test_constructor(struct rte_mempool *sess_mp,
60 		struct rte_mempool *sess_priv_mp,
61 		uint8_t dev_id, uint16_t qp_id,
62 		const struct cperf_options *options,
63 		const struct cperf_test_vector *test_vector,
64 		const struct cperf_op_fns *op_fns)
65 {
66 	struct cperf_throughput_ctx *ctx = NULL;
67 
68 	ctx = rte_malloc(NULL, sizeof(struct cperf_throughput_ctx), 0);
69 	if (ctx == NULL)
70 		goto err;
71 
72 	ctx->dev_id = dev_id;
73 	ctx->qp_id = qp_id;
74 
75 	ctx->populate_ops = op_fns->populate_ops;
76 	ctx->options = options;
77 	ctx->test_vector = test_vector;
78 
79 	/* IV goes at the end of the crypto operation */
80 	uint16_t iv_offset = sizeof(struct rte_crypto_op) +
81 		sizeof(struct rte_crypto_sym_op);
82 
83 	ctx->sess = op_fns->sess_create(sess_mp, sess_priv_mp, dev_id, options,
84 			test_vector, iv_offset);
85 	if (ctx->sess == NULL)
86 		goto err;
87 
88 	if (cperf_alloc_common_memory(options, test_vector, dev_id, qp_id, 0,
89 			&ctx->src_buf_offset, &ctx->dst_buf_offset,
90 			&ctx->pool) < 0)
91 		goto err;
92 
93 	return ctx;
94 err:
95 	cperf_throughput_test_free(ctx);
96 
97 	return NULL;
98 }
99 
100 int
101 cperf_throughput_test_runner(void *test_ctx)
102 {
103 	struct cperf_throughput_ctx *ctx = test_ctx;
104 	uint16_t test_burst_size;
105 	uint8_t burst_size_idx = 0;
106 	uint32_t imix_idx = 0;
107 
108 	static rte_atomic16_t display_once = RTE_ATOMIC16_INIT(0);
109 
110 	struct rte_crypto_op *ops[ctx->options->max_burst_size];
111 	struct rte_crypto_op *ops_processed[ctx->options->max_burst_size];
112 	uint64_t i;
113 
114 	uint32_t lcore = rte_lcore_id();
115 
116 #ifdef CPERF_LINEARIZATION_ENABLE
117 	struct rte_cryptodev_info dev_info;
118 	int linearize = 0;
119 
120 	/* Check if source mbufs require coalescing */
121 	if (ctx->options->segment_sz < ctx->options->max_buffer_size) {
122 		rte_cryptodev_info_get(ctx->dev_id, &dev_info);
123 		if ((dev_info.feature_flags &
124 				RTE_CRYPTODEV_FF_MBUF_SCATTER_GATHER) == 0)
125 			linearize = 1;
126 	}
127 #endif /* CPERF_LINEARIZATION_ENABLE */
128 
129 	ctx->lcore_id = lcore;
130 
131 	/* Warm up the host CPU before starting the test */
132 	for (i = 0; i < ctx->options->total_ops; i++)
133 		rte_cryptodev_enqueue_burst(ctx->dev_id, ctx->qp_id, NULL, 0);
134 
135 	/* Get first size from range or list */
136 	if (ctx->options->inc_burst_size != 0)
137 		test_burst_size = ctx->options->min_burst_size;
138 	else
139 		test_burst_size = ctx->options->burst_size_list[0];
140 
141 	uint16_t iv_offset = sizeof(struct rte_crypto_op) +
142 		sizeof(struct rte_crypto_sym_op);
143 
144 	while (test_burst_size <= ctx->options->max_burst_size) {
145 		uint64_t ops_enqd = 0, ops_enqd_total = 0, ops_enqd_failed = 0;
146 		uint64_t ops_deqd = 0, ops_deqd_total = 0, ops_deqd_failed = 0;
147 
148 		uint64_t tsc_start, tsc_end, tsc_duration;
149 
150 		uint16_t ops_unused = 0;
151 
152 		tsc_start = rte_rdtsc_precise();
153 
154 		while (ops_enqd_total < ctx->options->total_ops) {
155 
156 			uint16_t burst_size = ((ops_enqd_total + test_burst_size)
157 					<= ctx->options->total_ops) ?
158 							test_burst_size :
159 							ctx->options->total_ops -
160 							ops_enqd_total;
161 
162 			uint16_t ops_needed = burst_size - ops_unused;
163 
164 			/* Allocate objects containing crypto operations and mbufs */
165 			if (rte_mempool_get_bulk(ctx->pool, (void **)ops,
166 						ops_needed) != 0) {
167 				RTE_LOG(ERR, USER1,
168 					"Failed to allocate more crypto operations "
169 					"from the crypto operation pool.\n"
170 					"Consider increasing the pool size "
171 					"with --pool-sz\n");
172 				return -1;
173 			}
174 
175 			/* Setup crypto op, attach mbuf etc */
176 			(ctx->populate_ops)(ops, ctx->src_buf_offset,
177 					ctx->dst_buf_offset,
178 					ops_needed, ctx->sess,
179 					ctx->options, ctx->test_vector,
180 					iv_offset, &imix_idx);
181 
182 			/**
183 			 * When ops_needed is smaller than ops_enqd, the
184 			 * unused ops need to be moved to the front for
185 			 * next round use.
186 			 */
187 			if (unlikely(ops_enqd > ops_needed)) {
188 				size_t nb_b_to_mov = ops_unused * sizeof(
189 						struct rte_crypto_op *);
190 
191 				memmove(&ops[ops_needed], &ops[ops_enqd],
192 					nb_b_to_mov);
193 			}
194 
195 #ifdef CPERF_LINEARIZATION_ENABLE
196 			if (linearize) {
197 				/* PMD doesn't support scatter-gather and source buffer
198 				 * is segmented.
199 				 * We need to linearize it before enqueuing.
200 				 */
201 				for (i = 0; i < burst_size; i++)
202 					rte_pktmbuf_linearize(ops[i]->sym->m_src);
203 			}
204 #endif /* CPERF_LINEARIZATION_ENABLE */
205 
206 			/* Enqueue burst of ops on crypto device */
207 			ops_enqd = rte_cryptodev_enqueue_burst(ctx->dev_id, ctx->qp_id,
208 					ops, burst_size);
209 			if (ops_enqd < burst_size)
210 				ops_enqd_failed++;
211 
212 			/**
213 			 * Calculate number of ops not enqueued (mainly for hw
214 			 * accelerators whose ingress queue can fill up).
215 			 */
216 			ops_unused = burst_size - ops_enqd;
217 			ops_enqd_total += ops_enqd;
218 
219 
220 			/* Dequeue processed burst of ops from crypto device */
221 			ops_deqd = rte_cryptodev_dequeue_burst(ctx->dev_id, ctx->qp_id,
222 					ops_processed, test_burst_size);
223 
224 			if (likely(ops_deqd))  {
225 				/* Free crypto ops so they can be reused. */
226 				rte_mempool_put_bulk(ctx->pool,
227 						(void **)ops_processed, ops_deqd);
228 
229 				ops_deqd_total += ops_deqd;
230 			} else {
231 				/**
232 				 * Count dequeue polls which didn't return any
233 				 * processed operations. This statistic is mainly
234 				 * relevant to hw accelerators.
235 				 */
236 				ops_deqd_failed++;
237 			}
238 
239 		}
240 
241 		/* Dequeue any operations still in the crypto device */
242 
243 		while (ops_deqd_total < ctx->options->total_ops) {
244 			/* Sending 0 length burst to flush sw crypto device */
245 			rte_cryptodev_enqueue_burst(ctx->dev_id, ctx->qp_id, NULL, 0);
246 
247 			/* dequeue burst */
248 			ops_deqd = rte_cryptodev_dequeue_burst(ctx->dev_id, ctx->qp_id,
249 					ops_processed, test_burst_size);
250 			if (ops_deqd == 0)
251 				ops_deqd_failed++;
252 			else {
253 				rte_mempool_put_bulk(ctx->pool,
254 						(void **)ops_processed, ops_deqd);
255 				ops_deqd_total += ops_deqd;
256 			}
257 		}
258 
259 		tsc_end = rte_rdtsc_precise();
260 		tsc_duration = (tsc_end - tsc_start);
261 
262 		/* Calculate average operations processed per second */
263 		double ops_per_second = ((double)ctx->options->total_ops /
264 				tsc_duration) * rte_get_tsc_hz();
265 
266 		/* Calculate average throughput (Gbps) in bits per second */
267 		double throughput_gbps = ((ops_per_second *
268 				ctx->options->test_buffer_size * 8) / 1000000000);
269 
270 		/* Calculate average cycles per packet */
271 		double cycles_per_packet = ((double)tsc_duration /
272 				ctx->options->total_ops);
273 
274 		if (!ctx->options->csv) {
275 			if (rte_atomic16_test_and_set(&display_once))
276 				printf("%12s%12s%12s%12s%12s%12s%12s%12s%12s%12s\n\n",
277 					"lcore id", "Buf Size", "Burst Size",
278 					"Enqueued", "Dequeued", "Failed Enq",
279 					"Failed Deq", "MOps", "Gbps",
280 					"Cycles/Buf");
281 
282 			printf("%12u%12u%12u%12"PRIu64"%12"PRIu64"%12"PRIu64
283 					"%12"PRIu64"%12.4f%12.4f%12.2f\n",
284 					ctx->lcore_id,
285 					ctx->options->test_buffer_size,
286 					test_burst_size,
287 					ops_enqd_total,
288 					ops_deqd_total,
289 					ops_enqd_failed,
290 					ops_deqd_failed,
291 					ops_per_second/1000000,
292 					throughput_gbps,
293 					cycles_per_packet);
294 		} else {
295 			if (rte_atomic16_test_and_set(&display_once))
296 				printf("#lcore id,Buffer Size(B),"
297 					"Burst Size,Enqueued,Dequeued,Failed Enq,"
298 					"Failed Deq,Ops(Millions),Throughput(Gbps),"
299 					"Cycles/Buf\n\n");
300 
301 			printf("%u;%u;%u;%"PRIu64";%"PRIu64";%"PRIu64";%"PRIu64";"
302 					"%.3f;%.3f;%.3f\n",
303 					ctx->lcore_id,
304 					ctx->options->test_buffer_size,
305 					test_burst_size,
306 					ops_enqd_total,
307 					ops_deqd_total,
308 					ops_enqd_failed,
309 					ops_deqd_failed,
310 					ops_per_second/1000000,
311 					throughput_gbps,
312 					cycles_per_packet);
313 		}
314 
315 		/* Get next size from range or list */
316 		if (ctx->options->inc_burst_size != 0)
317 			test_burst_size += ctx->options->inc_burst_size;
318 		else {
319 			if (++burst_size_idx == ctx->options->burst_size_count)
320 				break;
321 			test_burst_size = ctx->options->burst_size_list[burst_size_idx];
322 		}
323 
324 	}
325 
326 	return 0;
327 }
328 
329 
330 void
331 cperf_throughput_test_destructor(void *arg)
332 {
333 	struct cperf_throughput_ctx *ctx = arg;
334 
335 	if (ctx == NULL)
336 		return;
337 
338 	cperf_throughput_test_free(ctx);
339 }
340