Branch data Line data Source code
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 : : uint8_t sess_owner;
25 : :
26 : : cperf_populate_ops_t populate_ops;
27 : :
28 : : uint32_t src_buf_offset;
29 : : uint32_t dst_buf_offset;
30 : :
31 : : const struct cperf_options *options;
32 : : const struct cperf_test_vector *test_vector;
33 : : };
34 : :
35 : : static void
36 : 0 : cperf_throughput_test_free(struct cperf_throughput_ctx *ctx)
37 : : {
38 : 0 : if (!ctx)
39 : : return;
40 : 0 : if (ctx->sess != NULL && ctx->sess_owner) {
41 : 0 : if (cperf_is_asym_test(ctx->options))
42 : 0 : rte_cryptodev_asym_session_free(ctx->dev_id,
43 : : (void *)ctx->sess);
44 : : #ifdef RTE_LIB_SECURITY
45 : 0 : else if (ctx->options->op_type == CPERF_PDCP ||
46 : 0 : ctx->options->op_type == CPERF_DOCSIS ||
47 : 0 : ctx->options->op_type == CPERF_TLS ||
48 : 0 : ctx->options->op_type == CPERF_IPSEC) {
49 : 0 : void *sec_ctx = rte_cryptodev_get_sec_ctx(ctx->dev_id);
50 : :
51 : 0 : rte_security_session_destroy(sec_ctx, (void *)ctx->sess);
52 : : }
53 : : #endif
54 : : else
55 : 0 : rte_cryptodev_sym_session_free(ctx->dev_id, ctx->sess);
56 : : }
57 : 0 : rte_mempool_free(ctx->pool);
58 : :
59 : 0 : rte_free(ctx);
60 : : }
61 : :
62 : : void *
63 : 0 : cperf_throughput_test_constructor(struct rte_mempool *sess_mp,
64 : : uint8_t dev_id, uint16_t qp_id,
65 : : const struct cperf_options *options,
66 : : const struct cperf_test_vector *test_vector,
67 : : const struct cperf_op_fns *op_fns,
68 : : void **sess)
69 : : {
70 : : struct cperf_throughput_ctx *ctx = NULL;
71 : :
72 : 0 : ctx = rte_malloc(NULL, sizeof(struct cperf_throughput_ctx), 0);
73 : 0 : if (ctx == NULL)
74 : 0 : goto err;
75 : :
76 : 0 : ctx->dev_id = dev_id;
77 : 0 : ctx->qp_id = qp_id;
78 : :
79 : 0 : ctx->populate_ops = op_fns->populate_ops;
80 : 0 : ctx->options = options;
81 : 0 : ctx->test_vector = test_vector;
82 : :
83 : : /* IV goes at the end of the crypto operation */
84 : : uint16_t iv_offset = sizeof(struct rte_crypto_op) +
85 : : sizeof(struct rte_crypto_sym_op);
86 : :
87 : 0 : if (*sess != NULL) {
88 : 0 : ctx->sess = *sess;
89 : 0 : ctx->sess_owner = false;
90 : : } else {
91 : 0 : ctx->sess = op_fns->sess_create(sess_mp, dev_id, options, test_vector,
92 : : iv_offset);
93 : 0 : if (ctx->sess == NULL)
94 : 0 : goto err;
95 : 0 : *sess = ctx->sess;
96 : 0 : ctx->sess_owner = true;
97 : : }
98 : :
99 : 0 : if (cperf_alloc_common_memory(options, test_vector, dev_id, qp_id, 0,
100 : : &ctx->src_buf_offset, &ctx->dst_buf_offset,
101 : : &ctx->pool) < 0)
102 : 0 : goto err;
103 : :
104 : : return ctx;
105 : 0 : err:
106 : 0 : cperf_throughput_test_free(ctx);
107 : :
108 : 0 : return NULL;
109 : : }
110 : :
111 : : static void
112 : 0 : cperf_verify_init_ops(struct rte_mempool *mp __rte_unused,
113 : : void *opaque_arg,
114 : : void *obj,
115 : : __rte_unused unsigned int i)
116 : : {
117 : : uint16_t iv_offset = sizeof(struct rte_crypto_op) +
118 : : sizeof(struct rte_crypto_sym_op);
119 : 0 : uint32_t imix_idx = 0;
120 : : struct cperf_throughput_ctx *ctx = opaque_arg;
121 : 0 : struct rte_crypto_op *op = obj;
122 : :
123 : 0 : (ctx->populate_ops)(&op, ctx->src_buf_offset,
124 : : ctx->dst_buf_offset,
125 : : 1, ctx->sess, ctx->options,
126 : : ctx->test_vector, iv_offset, &imix_idx, NULL);
127 : :
128 : 0 : cperf_mbuf_set(op->sym->m_src, ctx->options, ctx->test_vector);
129 : 0 : }
130 : :
131 : : static int
132 : 0 : cperf_check_single_op(struct cperf_throughput_ctx *ctx, uint16_t iv_offset)
133 : : {
134 : : struct rte_crypto_op *ops[1];
135 : 0 : uint32_t imix_idx = 0;
136 : 0 : uint64_t tsc_start = 0;
137 : : uint64_t ops_enqd = 0, ops_deqd = 0;
138 : :
139 : : /* Allocate object containing crypto operations and mbufs */
140 : 0 : if (rte_mempool_get(ctx->pool, (void **)&ops[0]) != 0) {
141 : 0 : RTE_LOG(ERR, USER1,
142 : : "Failed to allocate crypto operation "
143 : : "from the crypto operation pool.\n"
144 : : "Consider increasing the pool size "
145 : : "with --pool-sz\n");
146 : 0 : return -1;
147 : : }
148 : :
149 : : /* Setup crypto op, attach mbuf etc */
150 : 0 : if (!ctx->options->out_of_place)
151 : 0 : (ctx->populate_ops)(ops, ctx->src_buf_offset,
152 : : ctx->dst_buf_offset,
153 : : 1, ctx->sess,
154 : : ctx->options, ctx->test_vector,
155 : : iv_offset, &imix_idx, &tsc_start);
156 : :
157 : 0 : ops_enqd = rte_cryptodev_enqueue_burst(ctx->dev_id, ctx->qp_id, ops, 1);
158 : 0 : if (ops_enqd != 1) {
159 : 0 : RTE_LOG(ERR, USER1, "PMD cannot process the packet.\n");
160 : 0 : return -1;
161 : : }
162 : :
163 : : /* Dequeue processed burst of ops from crypto device */
164 : 0 : tsc_start = rte_rdtsc_precise();
165 : : while (1) {
166 : 0 : ops_deqd = rte_cryptodev_dequeue_burst(ctx->dev_id, ctx->qp_id,
167 : : ops, 1);
168 : :
169 : 0 : if (ops_deqd == 1) {
170 : 0 : rte_mempool_put(ctx->pool, ops[0]);
171 : 0 : return 1;
172 : : }
173 : :
174 : : /* Check if 1 second timeout has been reached */
175 : 0 : if ((rte_rdtsc_precise() - tsc_start) > rte_get_tsc_hz()) {
176 : 0 : RTE_LOG(ERR, USER1, "Dequeue operation timed out.\n");
177 : 0 : return -1;
178 : : }
179 : : }
180 : : }
181 : :
182 : : int
183 : 0 : cperf_throughput_test_runner(void *test_ctx)
184 : 0 : {
185 : : struct cperf_throughput_ctx *ctx = test_ctx;
186 : : uint16_t test_burst_size;
187 : : uint8_t burst_size_idx = 0;
188 : 0 : uint32_t imix_idx = 0;
189 : :
190 : : static RTE_ATOMIC(uint16_t) display_once;
191 : :
192 : 0 : struct rte_crypto_op *ops[ctx->options->max_burst_size];
193 : 0 : struct rte_crypto_op *ops_processed[ctx->options->max_burst_size];
194 : : uint64_t i;
195 : :
196 : : uint32_t lcore = rte_lcore_id();
197 : :
198 : : #ifdef CPERF_LINEARIZATION_ENABLE
199 : : struct rte_cryptodev_info dev_info;
200 : : int linearize = 0;
201 : :
202 : : /* Check if source mbufs require coalescing */
203 : : if ((ctx->options->op_type != CPERF_ASYM_MODEX) &&
204 : : (ctx->options->segment_sz < ctx->options->max_buffer_size)) {
205 : : rte_cryptodev_info_get(ctx->dev_id, &dev_info);
206 : : if ((dev_info.feature_flags &
207 : : RTE_CRYPTODEV_FF_MBUF_SCATTER_GATHER) == 0)
208 : : linearize = 1;
209 : : }
210 : : #endif /* CPERF_LINEARIZATION_ENABLE */
211 : :
212 : 0 : ctx->lcore_id = lcore;
213 : :
214 : : /* Warm up the host CPU before starting the test */
215 : 0 : for (i = 0; i < ctx->options->total_ops; i++)
216 : 0 : rte_cryptodev_enqueue_burst(ctx->dev_id, ctx->qp_id, NULL, 0);
217 : :
218 : : /* Get first size from range or list */
219 : 0 : if (ctx->options->inc_burst_size != 0)
220 : 0 : test_burst_size = ctx->options->min_burst_size;
221 : : else
222 : 0 : test_burst_size = ctx->options->burst_size_list[0];
223 : :
224 : : uint16_t iv_offset = sizeof(struct rte_crypto_op) +
225 : : sizeof(struct rte_crypto_sym_op);
226 : :
227 : 0 : if (ctx->options->out_of_place)
228 : 0 : rte_mempool_obj_iter(ctx->pool, cperf_verify_init_ops, (void *)ctx);
229 : :
230 : : /* Enqueue just one operation to check whether PMD returns error */
231 : 0 : if (cperf_check_single_op(ctx, iv_offset) < 1)
232 : : return -1;
233 : :
234 : 0 : while (test_burst_size <= ctx->options->max_burst_size) {
235 : : uint64_t ops_enqd = 0, ops_enqd_total = 0, ops_enqd_failed = 0;
236 : : uint64_t ops_deqd = 0, ops_deqd_total = 0, ops_deqd_failed = 0;
237 : :
238 : : uint64_t tsc_start, tsc_end, tsc_duration;
239 : :
240 : : uint16_t ops_unused = 0;
241 : :
242 : 0 : tsc_start = rte_rdtsc_precise();
243 : :
244 : 0 : while (ops_enqd_total < ctx->options->total_ops) {
245 : :
246 : 0 : uint16_t burst_size = ((ops_enqd_total + test_burst_size)
247 : : <= ctx->options->total_ops) ?
248 : : test_burst_size :
249 : 0 : ctx->options->total_ops -
250 : : ops_enqd_total;
251 : :
252 : 0 : uint16_t ops_needed = burst_size - ops_unused;
253 : :
254 : : /* Allocate objects containing crypto operations and mbufs */
255 : 0 : if (rte_mempool_get_bulk(ctx->pool, (void **)ops,
256 : : ops_needed) != 0) {
257 : 0 : RTE_LOG(ERR, USER1,
258 : : "Failed to allocate more crypto operations "
259 : : "from the crypto operation pool.\n"
260 : : "Consider increasing the pool size "
261 : : "with --pool-sz\n");
262 : 0 : return -1;
263 : : }
264 : :
265 : : /* Setup crypto op, attach mbuf etc */
266 : 0 : if (!ctx->options->out_of_place)
267 : 0 : (ctx->populate_ops)(ops, ctx->src_buf_offset,
268 : : ctx->dst_buf_offset,
269 : : ops_needed, ctx->sess,
270 : : ctx->options, ctx->test_vector,
271 : : iv_offset, &imix_idx, &tsc_start);
272 : :
273 : : /**
274 : : * When ops_needed is smaller than ops_enqd, the
275 : : * unused ops need to be moved to the front for
276 : : * next round use.
277 : : */
278 : 0 : if (unlikely(ops_enqd > ops_needed)) {
279 : 0 : size_t nb_b_to_mov = ops_unused * sizeof(
280 : : struct rte_crypto_op *);
281 : :
282 : 0 : memmove(&ops[ops_needed], &ops[ops_enqd],
283 : : nb_b_to_mov);
284 : : }
285 : :
286 : : #ifdef CPERF_LINEARIZATION_ENABLE
287 : : if (linearize) {
288 : : /* PMD doesn't support scatter-gather and source buffer
289 : : * is segmented.
290 : : * We need to linearize it before enqueuing.
291 : : */
292 : : for (i = 0; i < burst_size; i++)
293 : : rte_pktmbuf_linearize(
294 : : ops[i]->sym->m_src);
295 : : }
296 : : #endif /* CPERF_LINEARIZATION_ENABLE */
297 : :
298 : : /* Enqueue burst of ops on crypto device */
299 : 0 : ops_enqd = rte_cryptodev_enqueue_burst(ctx->dev_id, ctx->qp_id,
300 : : ops, burst_size);
301 : 0 : if (ops_enqd < burst_size)
302 : 0 : ops_enqd_failed++;
303 : :
304 : : /**
305 : : * Calculate number of ops not enqueued (mainly for hw
306 : : * accelerators whose ingress queue can fill up).
307 : : */
308 : 0 : ops_unused = burst_size - ops_enqd;
309 : 0 : ops_enqd_total += ops_enqd;
310 : :
311 : :
312 : : /* Dequeue processed burst of ops from crypto device */
313 : 0 : ops_deqd = rte_cryptodev_dequeue_burst(ctx->dev_id, ctx->qp_id,
314 : : ops_processed, test_burst_size);
315 : :
316 : 0 : if (likely(ops_deqd)) {
317 : : /* Free crypto ops so they can be reused. */
318 : 0 : rte_mempool_put_bulk(ctx->pool,
319 : : (void **)ops_processed, ops_deqd);
320 : :
321 : 0 : ops_deqd_total += ops_deqd;
322 : : } else {
323 : : /**
324 : : * Count dequeue polls which didn't return any
325 : : * processed operations. This statistic is mainly
326 : : * relevant to hw accelerators.
327 : : */
328 : 0 : ops_deqd_failed++;
329 : : }
330 : :
331 : : }
332 : :
333 : : /* Dequeue any operations still in the crypto device */
334 : :
335 : 0 : while (ops_deqd_total < ctx->options->total_ops) {
336 : : /* Sending 0 length burst to flush sw crypto device */
337 : 0 : rte_cryptodev_enqueue_burst(ctx->dev_id, ctx->qp_id, NULL, 0);
338 : :
339 : : /* dequeue burst */
340 : 0 : ops_deqd = rte_cryptodev_dequeue_burst(ctx->dev_id, ctx->qp_id,
341 : : ops_processed, test_burst_size);
342 : 0 : if (ops_deqd == 0)
343 : 0 : ops_deqd_failed++;
344 : : else {
345 : 0 : rte_mempool_put_bulk(ctx->pool,
346 : : (void **)ops_processed, ops_deqd);
347 : 0 : ops_deqd_total += ops_deqd;
348 : : }
349 : : }
350 : :
351 : : tsc_end = rte_rdtsc_precise();
352 : 0 : tsc_duration = (tsc_end - tsc_start);
353 : :
354 : : /* Calculate average operations processed per second */
355 : 0 : double ops_per_second = ((double)ctx->options->total_ops /
356 : 0 : tsc_duration) * rte_get_tsc_hz();
357 : :
358 : : /* Calculate average throughput (Gbps) in bits per second */
359 : 0 : double throughput_gbps = ((ops_per_second *
360 : 0 : ctx->options->test_buffer_size * 8) / 1000000000);
361 : :
362 : : /* Calculate average cycles per packet */
363 : 0 : double cycles_per_packet = ((double)tsc_duration /
364 : 0 : ctx->options->total_ops);
365 : :
366 : : uint16_t exp = 0;
367 : 0 : if (!ctx->options->csv) {
368 : 0 : if (rte_atomic_compare_exchange_strong_explicit(&display_once, &exp, 1,
369 : : rte_memory_order_relaxed, rte_memory_order_relaxed))
370 : : printf("%12s%12s%12s%12s%12s%12s%12s%12s%12s%12s\n\n",
371 : : "lcore id", "Buf Size", "Burst Size",
372 : : "Enqueued", "Dequeued", "Failed Enq",
373 : : "Failed Deq", "MOps", "Gbps",
374 : : "Cycles/Buf");
375 : :
376 : 0 : printf("%12u%12u%12u%12"PRIu64"%12"PRIu64"%12"PRIu64
377 : : "%12"PRIu64"%12.4f%12.4f%12.2f\n",
378 : 0 : ctx->lcore_id,
379 : 0 : ctx->options->test_buffer_size,
380 : : test_burst_size,
381 : : ops_enqd_total,
382 : : ops_deqd_total,
383 : : ops_enqd_failed,
384 : : ops_deqd_failed,
385 : : ops_per_second/1000000,
386 : : throughput_gbps,
387 : : cycles_per_packet);
388 : : } else {
389 : 0 : if (rte_atomic_compare_exchange_strong_explicit(&display_once, &exp, 1,
390 : : rte_memory_order_relaxed, rte_memory_order_relaxed))
391 : : printf("#lcore id,Buffer Size(B),"
392 : : "Burst Size,Enqueued,Dequeued,Failed Enq,"
393 : : "Failed Deq,Ops(Millions),Throughput(Gbps),"
394 : : "Cycles/Buf\n\n");
395 : :
396 : 0 : printf("%u,%u,%u,%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64","
397 : : "%.3f,%.3f,%.3f\n",
398 : 0 : ctx->lcore_id,
399 : 0 : ctx->options->test_buffer_size,
400 : : test_burst_size,
401 : : ops_enqd_total,
402 : : ops_deqd_total,
403 : : ops_enqd_failed,
404 : : ops_deqd_failed,
405 : : ops_per_second/1000000,
406 : : throughput_gbps,
407 : : cycles_per_packet);
408 : : }
409 : :
410 : : /* Get next size from range or list */
411 : 0 : if (ctx->options->inc_burst_size != 0)
412 : 0 : test_burst_size += ctx->options->inc_burst_size;
413 : : else {
414 : 0 : if (++burst_size_idx == ctx->options->burst_size_count)
415 : : break;
416 : 0 : test_burst_size = ctx->options->burst_size_list[burst_size_idx];
417 : : }
418 : :
419 : : }
420 : :
421 : : return 0;
422 : : }
423 : :
424 : :
425 : : void
426 : 0 : cperf_throughput_test_destructor(void *arg)
427 : : {
428 : : struct cperf_throughput_ctx *ctx = arg;
429 : :
430 : 0 : if (ctx == NULL)
431 : : return;
432 : :
433 : 0 : cperf_throughput_test_free(ctx);
434 : : }
|