Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2016-2017 Intel Corporation
3 : : */
4 : : #include <rte_malloc.h>
5 : : #include <rte_cycles.h>
6 : : #include <rte_crypto.h>
7 : : #include <rte_cryptodev.h>
8 : :
9 : : #include "cperf_test_latency.h"
10 : : #include "cperf_ops.h"
11 : : #include "cperf_test_common.h"
12 : :
13 : : struct cperf_op_result {
14 : : uint64_t tsc_start;
15 : : uint64_t tsc_end;
16 : : enum rte_crypto_op_status status;
17 : : };
18 : :
19 : : struct cperf_latency_ctx {
20 : : uint8_t dev_id;
21 : : uint16_t qp_id;
22 : : uint8_t lcore_id;
23 : :
24 : : struct rte_mempool *pool;
25 : :
26 : : void *sess;
27 : : uint8_t sess_owner;
28 : :
29 : : cperf_populate_ops_t populate_ops;
30 : :
31 : : uint32_t src_buf_offset;
32 : : uint32_t dst_buf_offset;
33 : :
34 : : const struct cperf_options *options;
35 : : const struct cperf_test_vector *test_vector;
36 : : struct cperf_op_result *res;
37 : : };
38 : :
39 : : struct priv_op_data {
40 : : struct cperf_op_result *result;
41 : : };
42 : :
43 : : static void
44 : 0 : cperf_latency_test_free(struct cperf_latency_ctx *ctx)
45 : : {
46 : 0 : if (ctx == NULL)
47 : : return;
48 : :
49 : 0 : if (ctx->sess != NULL && ctx->sess_owner) {
50 : 0 : if (cperf_is_asym_test(ctx->options))
51 : 0 : rte_cryptodev_asym_session_free(ctx->dev_id, ctx->sess);
52 : : #ifdef RTE_LIB_SECURITY
53 : 0 : else if (ctx->options->op_type == CPERF_PDCP ||
54 : 0 : ctx->options->op_type == CPERF_DOCSIS ||
55 : 0 : ctx->options->op_type == CPERF_TLS ||
56 : 0 : ctx->options->op_type == CPERF_IPSEC) {
57 : 0 : void *sec_ctx = rte_cryptodev_get_sec_ctx(ctx->dev_id);
58 : 0 : rte_security_session_destroy(sec_ctx, ctx->sess);
59 : : }
60 : : #endif
61 : : else
62 : 0 : rte_cryptodev_sym_session_free(ctx->dev_id, ctx->sess);
63 : : }
64 : :
65 : 0 : rte_mempool_free(ctx->pool);
66 : 0 : rte_free(ctx->res);
67 : 0 : rte_free(ctx);
68 : : }
69 : :
70 : : void *
71 : 0 : cperf_latency_test_constructor(struct rte_mempool *sess_mp,
72 : : uint8_t dev_id, uint16_t qp_id,
73 : : const struct cperf_options *options,
74 : : const struct cperf_test_vector *test_vector,
75 : : const struct cperf_op_fns *op_fns,
76 : : void **sess)
77 : : {
78 : : struct cperf_latency_ctx *ctx = NULL;
79 : : size_t extra_op_priv_size = sizeof(struct priv_op_data);
80 : :
81 : 0 : ctx = rte_malloc(NULL, sizeof(struct cperf_latency_ctx), 0);
82 : 0 : if (ctx == NULL)
83 : 0 : goto err;
84 : :
85 : 0 : ctx->dev_id = dev_id;
86 : 0 : ctx->qp_id = qp_id;
87 : :
88 : 0 : ctx->populate_ops = op_fns->populate_ops;
89 : 0 : ctx->options = options;
90 : 0 : ctx->test_vector = test_vector;
91 : :
92 : : /* IV goes at the end of the crypto operation */
93 : : uint16_t iv_offset = sizeof(struct rte_crypto_op) +
94 : : sizeof(struct rte_crypto_sym_op) +
95 : : sizeof(struct cperf_op_result *);
96 : :
97 : :
98 : 0 : if (*sess != NULL) {
99 : 0 : ctx->sess = *sess;
100 : 0 : ctx->sess_owner = false;
101 : : } else {
102 : 0 : ctx->sess = op_fns->sess_create(sess_mp, dev_id, options, test_vector,
103 : : iv_offset);
104 : 0 : if (ctx->sess == NULL)
105 : 0 : goto err;
106 : 0 : *sess = ctx->sess;
107 : 0 : ctx->sess_owner = true;
108 : : }
109 : :
110 : 0 : if (cperf_alloc_common_memory(options, test_vector, dev_id, qp_id,
111 : : extra_op_priv_size,
112 : : &ctx->src_buf_offset, &ctx->dst_buf_offset,
113 : : &ctx->pool) < 0)
114 : 0 : goto err;
115 : :
116 : 0 : ctx->res = rte_malloc(NULL, sizeof(struct cperf_op_result) *
117 : 0 : ctx->options->total_ops, 0);
118 : :
119 : 0 : if (ctx->res == NULL)
120 : 0 : goto err;
121 : :
122 : : return ctx;
123 : 0 : err:
124 : 0 : cperf_latency_test_free(ctx);
125 : :
126 : 0 : return NULL;
127 : : }
128 : :
129 : : static inline void
130 : : store_timestamp(struct rte_crypto_op *op, uint64_t timestamp)
131 : : {
132 : : struct priv_op_data *priv_data;
133 : :
134 : 0 : if (op->type == RTE_CRYPTO_OP_TYPE_SYMMETRIC)
135 : 0 : priv_data = (struct priv_op_data *) (op->sym + 1);
136 : : else
137 : 0 : priv_data = (struct priv_op_data *) (op->asym + 1);
138 : :
139 : 0 : priv_data->result->status = op->status;
140 : 0 : priv_data->result->tsc_end = timestamp;
141 : : }
142 : :
143 : : static int
144 : 0 : cperf_check_single_op(struct cperf_latency_ctx *ctx, uint16_t iv_offset)
145 : : {
146 : : struct rte_crypto_op *ops[1];
147 : 0 : uint32_t imix_idx = 0;
148 : 0 : uint64_t tsc_start = 0;
149 : : uint64_t ops_enqd = 0, ops_deqd = 0;
150 : :
151 : : /* Allocate object containing crypto operations and mbufs */
152 : 0 : if (rte_mempool_get(ctx->pool, (void **)&ops[0]) != 0) {
153 : 0 : RTE_LOG(ERR, USER1,
154 : : "Failed to allocate crypto operation "
155 : : "from the crypto operation pool.\n"
156 : : "Consider increasing the pool size "
157 : : "with --pool-sz\n");
158 : 0 : return -1;
159 : : }
160 : :
161 : : /* Setup crypto op, attach mbuf etc */
162 : 0 : (ctx->populate_ops)(ops, ctx->src_buf_offset,
163 : : ctx->dst_buf_offset,
164 : : 1, ctx->sess, ctx->options,
165 : : ctx->test_vector, iv_offset,
166 : : &imix_idx, &tsc_start);
167 : :
168 : 0 : ops_enqd = rte_cryptodev_enqueue_burst(ctx->dev_id, ctx->qp_id, ops, 1);
169 : 0 : if (ops_enqd != 1) {
170 : 0 : RTE_LOG(ERR, USER1, "PMD cannot process the packet.\n");
171 : 0 : return -1;
172 : : }
173 : :
174 : : /* Dequeue processed burst of ops from crypto device */
175 : 0 : tsc_start = rte_rdtsc_precise();
176 : : while (1) {
177 : 0 : ops_deqd = rte_cryptodev_dequeue_burst(ctx->dev_id, ctx->qp_id,
178 : : ops, 1);
179 : :
180 : 0 : if (ops_deqd == 1) {
181 : 0 : rte_mempool_put(ctx->pool, ops[0]);
182 : 0 : return 1;
183 : : }
184 : :
185 : : /* Check if 1 second timeout has been reached */
186 : 0 : if ((rte_rdtsc_precise() - tsc_start) > rte_get_tsc_hz()) {
187 : 0 : RTE_LOG(ERR, USER1, "Dequeue operation timed out.\n");
188 : 0 : return -1;
189 : : }
190 : : }
191 : : }
192 : :
193 : : int
194 : 0 : cperf_latency_test_runner(void *arg)
195 : 0 : {
196 : : struct cperf_latency_ctx *ctx = arg;
197 : : uint16_t test_burst_size;
198 : : uint8_t burst_size_idx = 0;
199 : 0 : uint32_t imix_idx = 0;
200 : : int ret = 0;
201 : :
202 : : static RTE_ATOMIC(uint16_t) display_once;
203 : :
204 : 0 : if (ctx == NULL)
205 : : return 0;
206 : :
207 : 0 : struct rte_crypto_op *ops[ctx->options->max_burst_size];
208 : 0 : struct rte_crypto_op *ops_processed[ctx->options->max_burst_size];
209 : : uint64_t i;
210 : : struct priv_op_data *priv_data;
211 : :
212 : : uint32_t lcore = rte_lcore_id();
213 : :
214 : : #ifdef CPERF_LINEARIZATION_ENABLE
215 : : struct rte_cryptodev_info dev_info;
216 : : int linearize = 0;
217 : :
218 : : /* Check if source mbufs require coalescing */
219 : : if (ctx->options->segment_sz < ctx->options->max_buffer_size) {
220 : : rte_cryptodev_info_get(ctx->dev_id, &dev_info);
221 : : if ((dev_info.feature_flags &
222 : : RTE_CRYPTODEV_FF_MBUF_SCATTER_GATHER) == 0)
223 : : linearize = 1;
224 : : }
225 : : #endif /* CPERF_LINEARIZATION_ENABLE */
226 : :
227 : 0 : ctx->lcore_id = lcore;
228 : :
229 : : /* Warm up the host CPU before starting the test */
230 : 0 : for (i = 0; i < ctx->options->total_ops; i++)
231 : 0 : rte_cryptodev_enqueue_burst(ctx->dev_id, ctx->qp_id, NULL, 0);
232 : :
233 : : /* Get first size from range or list */
234 : 0 : if (ctx->options->inc_burst_size != 0)
235 : 0 : test_burst_size = ctx->options->min_burst_size;
236 : : else
237 : 0 : test_burst_size = ctx->options->burst_size_list[0];
238 : :
239 : : uint16_t iv_offset = sizeof(struct rte_crypto_op) +
240 : : sizeof(struct rte_crypto_sym_op) +
241 : : sizeof(struct cperf_op_result *);
242 : :
243 : : /* Enqueue just one operation to check whether PMD returns error */
244 : 0 : if (cperf_check_single_op(ctx, iv_offset) < 1)
245 : : return -1;
246 : :
247 : 0 : while (test_burst_size <= ctx->options->max_burst_size) {
248 : : uint64_t ops_enqd = 0, ops_deqd = 0;
249 : : uint64_t b_idx = 0;
250 : :
251 : : uint64_t tsc_val, tsc_end, tsc_start;
252 : : uint64_t tsc_max = 0, tsc_min = ~0UL, tsc_tot = 0, tsc_idx = 0;
253 : : uint64_t enqd_max = 0, enqd_min = ~0UL, enqd_tot = 0;
254 : : uint64_t deqd_max = 0, deqd_min = ~0UL, deqd_tot = 0;
255 : :
256 : 0 : while (enqd_tot < ctx->options->total_ops) {
257 : :
258 : 0 : uint16_t burst_size = ((enqd_tot + test_burst_size)
259 : : <= ctx->options->total_ops) ?
260 : : test_burst_size :
261 : 0 : ctx->options->total_ops -
262 : : enqd_tot;
263 : :
264 : : /* Allocate objects containing crypto operations and mbufs */
265 : 0 : if (rte_mempool_get_bulk(ctx->pool, (void **)ops,
266 : : burst_size) != 0) {
267 : 0 : RTE_LOG(ERR, USER1,
268 : : "Failed to allocate more crypto operations "
269 : : "from the crypto operation pool.\n"
270 : : "Consider increasing the pool size "
271 : : "with --pool-sz\n");
272 : 0 : return -1;
273 : : }
274 : :
275 : : /* Setup crypto op, attach mbuf etc */
276 : 0 : (ctx->populate_ops)(ops, ctx->src_buf_offset,
277 : : ctx->dst_buf_offset,
278 : : burst_size, ctx->sess, ctx->options,
279 : : ctx->test_vector, iv_offset,
280 : : &imix_idx, &tsc_start);
281 : :
282 : : /* Populate the mbuf with the test vector */
283 : 0 : if (!cperf_is_asym_test(ctx->options))
284 : 0 : for (i = 0; i < burst_size; i++)
285 : 0 : cperf_mbuf_set(ops[i]->sym->m_src,
286 : : ctx->options,
287 : : ctx->test_vector);
288 : :
289 : 0 : tsc_start = rte_rdtsc_precise();
290 : :
291 : : #ifdef CPERF_LINEARIZATION_ENABLE
292 : : if (linearize) {
293 : : /* PMD doesn't support scatter-gather and source buffer
294 : : * is segmented.
295 : : * We need to linearize it before enqueuing.
296 : : */
297 : : for (i = 0; i < burst_size; i++)
298 : : rte_pktmbuf_linearize(ops[i]->sym->m_src);
299 : : }
300 : : #endif /* CPERF_LINEARIZATION_ENABLE */
301 : :
302 : : /* Enqueue burst of ops on crypto device */
303 : 0 : ops_enqd = rte_cryptodev_enqueue_burst(ctx->dev_id, ctx->qp_id,
304 : : ops, burst_size);
305 : :
306 : : /* Dequeue processed burst of ops from crypto device */
307 : 0 : ops_deqd = rte_cryptodev_dequeue_burst(ctx->dev_id, ctx->qp_id,
308 : : ops_processed, test_burst_size);
309 : :
310 : : tsc_end = rte_rdtsc_precise();
311 : :
312 : : /* Free memory for not enqueued operations */
313 : 0 : if (ops_enqd != burst_size)
314 : 0 : rte_mempool_put_bulk(ctx->pool,
315 : 0 : (void **)&ops[ops_enqd],
316 : : burst_size - ops_enqd);
317 : :
318 : 0 : for (i = 0; i < ops_enqd; i++) {
319 : 0 : ctx->res[tsc_idx].tsc_start = tsc_start;
320 : : /*
321 : : * Private data structure starts after the end of the
322 : : * rte_crypto_sym_op (or rte_crypto_asym_op) structure.
323 : : */
324 : 0 : if (ops[i]->type == RTE_CRYPTO_OP_TYPE_SYMMETRIC)
325 : 0 : priv_data = (struct priv_op_data *) (ops[i]->sym + 1);
326 : : else
327 : 0 : priv_data = (struct priv_op_data *) (ops[i]->asym + 1);
328 : :
329 : 0 : priv_data->result = (void *)&ctx->res[tsc_idx];
330 : 0 : tsc_idx++;
331 : : }
332 : :
333 : 0 : if (likely(ops_deqd)) {
334 : 0 : for (i = 0; i < ops_deqd; i++) {
335 : 0 : struct rte_crypto_op *op = ops_processed[i];
336 : :
337 : 0 : if (op->status != RTE_CRYPTO_OP_STATUS_SUCCESS)
338 : : ret = -1;
339 : :
340 : : store_timestamp(ops_processed[i], tsc_end);
341 : : }
342 : :
343 : : /* Free crypto ops so they can be reused. */
344 : 0 : rte_mempool_put_bulk(ctx->pool,
345 : : (void **)ops_processed, ops_deqd);
346 : :
347 : 0 : deqd_tot += ops_deqd;
348 : 0 : deqd_max = RTE_MAX(ops_deqd, deqd_max);
349 : 0 : deqd_min = RTE_MIN(ops_deqd, deqd_min);
350 : : }
351 : :
352 : 0 : enqd_tot += ops_enqd;
353 : 0 : enqd_max = RTE_MAX(ops_enqd, enqd_max);
354 : 0 : enqd_min = RTE_MIN(ops_enqd, enqd_min);
355 : :
356 : 0 : b_idx++;
357 : : }
358 : :
359 : : /* Dequeue any operations still in the crypto device */
360 : 0 : while (deqd_tot < ctx->options->total_ops) {
361 : : /* Sending 0 length burst to flush sw crypto device */
362 : 0 : rte_cryptodev_enqueue_burst(ctx->dev_id, ctx->qp_id, NULL, 0);
363 : :
364 : : /* dequeue burst */
365 : 0 : ops_deqd = rte_cryptodev_dequeue_burst(ctx->dev_id, ctx->qp_id,
366 : : ops_processed, test_burst_size);
367 : :
368 : : tsc_end = rte_rdtsc_precise();
369 : :
370 : 0 : if (ops_deqd != 0) {
371 : 0 : for (i = 0; i < ops_deqd; i++) {
372 : 0 : struct rte_crypto_op *op = ops_processed[i];
373 : :
374 : 0 : if (op->status != RTE_CRYPTO_OP_STATUS_SUCCESS)
375 : : ret = -1;
376 : :
377 : : store_timestamp(ops_processed[i], tsc_end);
378 : : }
379 : :
380 : 0 : rte_mempool_put_bulk(ctx->pool,
381 : : (void **)ops_processed, ops_deqd);
382 : :
383 : 0 : deqd_tot += ops_deqd;
384 : 0 : deqd_max = RTE_MAX(ops_deqd, deqd_max);
385 : 0 : deqd_min = RTE_MIN(ops_deqd, deqd_min);
386 : : }
387 : : }
388 : :
389 : : /* If there was any failure in crypto op, exit */
390 : 0 : if (ret)
391 : 0 : return ret;
392 : :
393 : 0 : for (i = 0; i < tsc_idx; i++) {
394 : 0 : tsc_val = ctx->res[i].tsc_end - ctx->res[i].tsc_start;
395 : 0 : tsc_max = RTE_MAX(tsc_val, tsc_max);
396 : 0 : tsc_min = RTE_MIN(tsc_val, tsc_min);
397 : 0 : tsc_tot += tsc_val;
398 : : }
399 : :
400 : : double time_tot, time_avg, time_max, time_min;
401 : :
402 : : const uint64_t tunit = 1000000; /* us */
403 : 0 : const uint64_t tsc_hz = rte_get_tsc_hz();
404 : :
405 : 0 : uint64_t enqd_avg = enqd_tot / b_idx;
406 : 0 : uint64_t deqd_avg = deqd_tot / b_idx;
407 : 0 : uint64_t tsc_avg = tsc_tot / tsc_idx;
408 : :
409 : 0 : time_tot = tunit*(double)(tsc_tot) / tsc_hz;
410 : 0 : time_avg = tunit*(double)(tsc_avg) / tsc_hz;
411 : 0 : time_max = tunit*(double)(tsc_max) / tsc_hz;
412 : 0 : time_min = tunit*(double)(tsc_min) / tsc_hz;
413 : :
414 : : uint16_t exp = 0;
415 : 0 : if (ctx->options->csv) {
416 : 0 : if (rte_atomic_compare_exchange_strong_explicit(&display_once, &exp, 1,
417 : : rte_memory_order_relaxed, rte_memory_order_relaxed))
418 : : printf("\n# lcore, Buffer Size, Burst Size, Pakt Seq #, "
419 : : "cycles, time (us)");
420 : :
421 : 0 : for (i = 0; i < ctx->options->total_ops; i++) {
422 : :
423 : 0 : printf("\n%u,%u,%u,%"PRIu64",%"PRIu64",%.3f",
424 : 0 : ctx->lcore_id, ctx->options->test_buffer_size,
425 : : test_burst_size, i + 1,
426 : : ctx->res[i].tsc_end - ctx->res[i].tsc_start,
427 : 0 : tunit * (double) (ctx->res[i].tsc_end
428 : 0 : - ctx->res[i].tsc_start)
429 : : / tsc_hz);
430 : :
431 : : }
432 : : } else {
433 : 0 : printf("\n# Device %d on lcore %u\n", ctx->dev_id,
434 : 0 : ctx->lcore_id);
435 : 0 : printf("\n# total operations: %u", ctx->options->total_ops);
436 : 0 : printf("\n# Buffer size: %u", ctx->options->test_buffer_size);
437 : 0 : printf("\n# Burst size: %u", test_burst_size);
438 : : printf("\n# Number of bursts: %"PRIu64,
439 : : b_idx);
440 : :
441 : : printf("\n#");
442 : : printf("\n# \t Total\t Average\t "
443 : : "Maximum\t Minimum");
444 : : printf("\n# enqueued\t%12"PRIu64"\t%10"PRIu64"\t"
445 : : "%10"PRIu64"\t%10"PRIu64, enqd_tot,
446 : : enqd_avg, enqd_max, enqd_min);
447 : : printf("\n# dequeued\t%12"PRIu64"\t%10"PRIu64"\t"
448 : : "%10"PRIu64"\t%10"PRIu64, deqd_tot,
449 : : deqd_avg, deqd_max, deqd_min);
450 : : printf("\n# cycles\t%12"PRIu64"\t%10"PRIu64"\t"
451 : : "%10"PRIu64"\t%10"PRIu64, tsc_tot,
452 : : tsc_avg, tsc_max, tsc_min);
453 : : printf("\n# time [us]\t%12.0f\t%10.3f\t%10.3f\t%10.3f",
454 : : time_tot, time_avg, time_max, time_min);
455 : : printf("\n\n");
456 : :
457 : : }
458 : :
459 : : /* Get next size from range or list */
460 : 0 : if (ctx->options->inc_burst_size != 0)
461 : 0 : test_burst_size += ctx->options->inc_burst_size;
462 : : else {
463 : 0 : if (++burst_size_idx == ctx->options->burst_size_count)
464 : : break;
465 : 0 : test_burst_size =
466 : 0 : ctx->options->burst_size_list[burst_size_idx];
467 : : }
468 : : }
469 : :
470 : : return 0;
471 : : }
472 : :
473 : : void
474 : 0 : cperf_latency_test_destructor(void *arg)
475 : : {
476 : : struct cperf_latency_ctx *ctx = arg;
477 : :
478 : 0 : if (ctx == NULL)
479 : : return;
480 : :
481 : 0 : cperf_latency_test_free(ctx);
482 : : }
|