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