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