fix typos
[dpdk.git] / app / test-crypto-perf / cperf_test_verify.c
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_verify.h"
11 #include "cperf_ops.h"
12 #include "cperf_test_common.h"
13
14 struct cperf_verify_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 struct cperf_op_result {
33         enum rte_crypto_op_status status;
34 };
35
36 static void
37 cperf_verify_test_free(struct cperf_verify_ctx *ctx)
38 {
39         if (ctx) {
40                 if (ctx->sess) {
41                         rte_cryptodev_sym_session_clear(ctx->dev_id, ctx->sess);
42                         rte_cryptodev_sym_session_free(ctx->sess);
43                 }
44
45                 if (ctx->pool)
46                         rte_mempool_free(ctx->pool);
47
48                 rte_free(ctx);
49         }
50 }
51
52 void *
53 cperf_verify_test_constructor(struct rte_mempool *sess_mp,
54                 uint8_t dev_id, uint16_t qp_id,
55                 const struct cperf_options *options,
56                 const struct cperf_test_vector *test_vector,
57                 const struct cperf_op_fns *op_fns)
58 {
59         struct cperf_verify_ctx *ctx = NULL;
60
61         ctx = rte_malloc(NULL, sizeof(struct cperf_verify_ctx), 0);
62         if (ctx == NULL)
63                 goto err;
64
65         ctx->dev_id = dev_id;
66         ctx->qp_id = qp_id;
67
68         ctx->populate_ops = op_fns->populate_ops;
69         ctx->options = options;
70         ctx->test_vector = test_vector;
71
72         /* IV goes at the end of the crypto operation */
73         uint16_t iv_offset = sizeof(struct rte_crypto_op) +
74                 sizeof(struct rte_crypto_sym_op);
75
76         ctx->sess = op_fns->sess_create(sess_mp, dev_id, options, test_vector,
77                         iv_offset);
78         if (ctx->sess == NULL)
79                 goto err;
80
81         if (cperf_alloc_common_memory(options, test_vector, dev_id, qp_id, 0,
82                         &ctx->src_buf_offset, &ctx->dst_buf_offset,
83                         &ctx->pool) < 0)
84                 goto err;
85
86         return ctx;
87 err:
88         cperf_verify_test_free(ctx);
89
90         return NULL;
91 }
92
93 static int
94 cperf_verify_op(struct rte_crypto_op *op,
95                 const struct cperf_options *options,
96                 const struct cperf_test_vector *vector)
97 {
98         const struct rte_mbuf *m;
99         uint32_t len;
100         uint16_t nb_segs;
101         uint8_t *data;
102         uint32_t cipher_offset, auth_offset;
103         uint8_t cipher, auth;
104         int res = 0;
105
106         if (op->status != RTE_CRYPTO_OP_STATUS_SUCCESS)
107                 return 1;
108
109         if (op->sym->m_dst)
110                 m = op->sym->m_dst;
111         else
112                 m = op->sym->m_src;
113         nb_segs = m->nb_segs;
114         len = 0;
115         while (m && nb_segs != 0) {
116                 len += m->data_len;
117                 m = m->next;
118                 nb_segs--;
119         }
120
121         data = rte_malloc(NULL, len, 0);
122         if (data == NULL)
123                 return 1;
124
125         if (op->sym->m_dst)
126                 m = op->sym->m_dst;
127         else
128                 m = op->sym->m_src;
129         nb_segs = m->nb_segs;
130         len = 0;
131         while (m && nb_segs != 0) {
132                 memcpy(data + len, rte_pktmbuf_mtod(m, uint8_t *),
133                                 m->data_len);
134                 len += m->data_len;
135                 m = m->next;
136                 nb_segs--;
137         }
138
139         switch (options->op_type) {
140         case CPERF_CIPHER_ONLY:
141                 cipher = 1;
142                 cipher_offset = 0;
143                 auth = 0;
144                 auth_offset = 0;
145                 break;
146         case CPERF_CIPHER_THEN_AUTH:
147                 cipher = 1;
148                 cipher_offset = 0;
149                 auth = 1;
150                 auth_offset = options->test_buffer_size;
151                 break;
152         case CPERF_AUTH_ONLY:
153                 cipher = 0;
154                 cipher_offset = 0;
155                 auth = 1;
156                 auth_offset = options->test_buffer_size;
157                 break;
158         case CPERF_AUTH_THEN_CIPHER:
159                 cipher = 1;
160                 cipher_offset = 0;
161                 auth = 1;
162                 auth_offset = options->test_buffer_size;
163                 break;
164         case CPERF_AEAD:
165                 cipher = 1;
166                 cipher_offset = 0;
167                 auth = 1;
168                 auth_offset = options->test_buffer_size;
169                 break;
170         default:
171                 res = 1;
172                 goto out;
173         }
174
175         if (cipher == 1) {
176                 if (options->cipher_op == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
177                         res += memcmp(data + cipher_offset,
178                                         vector->ciphertext.data,
179                                         options->test_buffer_size);
180                 else
181                         res += memcmp(data + cipher_offset,
182                                         vector->plaintext.data,
183                                         options->test_buffer_size);
184         }
185
186         if (auth == 1) {
187                 if (options->auth_op == RTE_CRYPTO_AUTH_OP_GENERATE)
188                         res += memcmp(data + auth_offset,
189                                         vector->digest.data,
190                                         options->digest_sz);
191         }
192
193 out:
194         rte_free(data);
195         return !!res;
196 }
197
198 static void
199 cperf_mbuf_set(struct rte_mbuf *mbuf,
200                 const struct cperf_options *options,
201                 const struct cperf_test_vector *test_vector)
202 {
203         uint32_t segment_sz = options->segment_sz;
204         uint8_t *mbuf_data;
205         uint8_t *test_data =
206                         (options->cipher_op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
207                                         test_vector->plaintext.data :
208                                         test_vector->ciphertext.data;
209         uint32_t remaining_bytes = options->max_buffer_size;
210
211         while (remaining_bytes) {
212                 mbuf_data = rte_pktmbuf_mtod(mbuf, uint8_t *);
213
214                 if (remaining_bytes <= segment_sz) {
215                         memcpy(mbuf_data, test_data, remaining_bytes);
216                         return;
217                 }
218
219                 memcpy(mbuf_data, test_data, segment_sz);
220                 remaining_bytes -= segment_sz;
221                 test_data += segment_sz;
222                 mbuf = mbuf->next;
223         }
224 }
225
226 int
227 cperf_verify_test_runner(void *test_ctx)
228 {
229         struct cperf_verify_ctx *ctx = test_ctx;
230
231         uint64_t ops_enqd = 0, ops_enqd_total = 0, ops_enqd_failed = 0;
232         uint64_t ops_deqd = 0, ops_deqd_total = 0, ops_deqd_failed = 0;
233         uint64_t ops_failed = 0;
234
235         static int only_once;
236
237         uint64_t i;
238         uint16_t ops_unused = 0;
239
240         struct rte_crypto_op *ops[ctx->options->max_burst_size];
241         struct rte_crypto_op *ops_processed[ctx->options->max_burst_size];
242
243         uint32_t lcore = rte_lcore_id();
244
245 #ifdef CPERF_LINEARIZATION_ENABLE
246         struct rte_cryptodev_info dev_info;
247         int linearize = 0;
248
249         /* Check if source mbufs require coalescing */
250         if (ctx->options->segment_sz < ctx->options->max_buffer_size) {
251                 rte_cryptodev_info_get(ctx->dev_id, &dev_info);
252                 if ((dev_info.feature_flags &
253                                 RTE_CRYPTODEV_FF_MBUF_SCATTER_GATHER) == 0)
254                         linearize = 1;
255         }
256 #endif /* CPERF_LINEARIZATION_ENABLE */
257
258         ctx->lcore_id = lcore;
259
260         if (!ctx->options->csv)
261                 printf("\n# Running verify test on device: %u, lcore: %u\n",
262                         ctx->dev_id, lcore);
263
264         uint16_t iv_offset = sizeof(struct rte_crypto_op) +
265                 sizeof(struct rte_crypto_sym_op);
266
267         while (ops_enqd_total < ctx->options->total_ops) {
268
269                 uint16_t burst_size = ((ops_enqd_total + ctx->options->max_burst_size)
270                                 <= ctx->options->total_ops) ?
271                                                 ctx->options->max_burst_size :
272                                                 ctx->options->total_ops -
273                                                 ops_enqd_total;
274
275                 uint16_t ops_needed = burst_size - ops_unused;
276
277                 /* Allocate objects containing crypto operations and mbufs */
278                 if (rte_mempool_get_bulk(ctx->pool, (void **)ops,
279                                         ops_needed) != 0) {
280                         RTE_LOG(ERR, USER1,
281                                 "Failed to allocate more crypto operations "
282                                 "from the crypto operation pool.\n"
283                                 "Consider increasing the pool size "
284                                 "with --pool-sz\n");
285                         return -1;
286                 }
287
288                 /* Setup crypto op, attach mbuf etc */
289                 (ctx->populate_ops)(ops, ctx->src_buf_offset,
290                                 ctx->dst_buf_offset,
291                                 ops_needed, ctx->sess, ctx->options,
292                                 ctx->test_vector, iv_offset);
293
294
295                 /* Populate the mbuf with the test vector, for verification */
296                 for (i = 0; i < ops_needed; i++)
297                         cperf_mbuf_set(ops[i]->sym->m_src,
298                                         ctx->options,
299                                         ctx->test_vector);
300
301 #ifdef CPERF_LINEARIZATION_ENABLE
302                 if (linearize) {
303                         /* PMD doesn't support scatter-gather and source buffer
304                          * is segmented.
305                          * We need to linearize it before enqueuing.
306                          */
307                         for (i = 0; i < burst_size; i++)
308                                 rte_pktmbuf_linearize(ops[i]->sym->m_src);
309                 }
310 #endif /* CPERF_LINEARIZATION_ENABLE */
311
312                 /* Enqueue burst of ops on crypto device */
313                 ops_enqd = rte_cryptodev_enqueue_burst(ctx->dev_id, ctx->qp_id,
314                                 ops, burst_size);
315                 if (ops_enqd < burst_size)
316                         ops_enqd_failed++;
317
318                 /**
319                  * Calculate number of ops not enqueued (mainly for hw
320                  * accelerators whose ingress queue can fill up).
321                  */
322                 ops_unused = burst_size - ops_enqd;
323                 ops_enqd_total += ops_enqd;
324
325
326                 /* Dequeue processed burst of ops from crypto device */
327                 ops_deqd = rte_cryptodev_dequeue_burst(ctx->dev_id, ctx->qp_id,
328                                 ops_processed, ctx->options->max_burst_size);
329
330                 if (ops_deqd == 0) {
331                         /**
332                          * Count dequeue polls which didn't return any
333                          * processed operations. This statistic is mainly
334                          * relevant to hw accelerators.
335                          */
336                         ops_deqd_failed++;
337                         continue;
338                 }
339
340                 for (i = 0; i < ops_deqd; i++) {
341                         if (cperf_verify_op(ops_processed[i], ctx->options,
342                                                 ctx->test_vector))
343                                 ops_failed++;
344                 }
345                 /* Free crypto ops so they can be reused. */
346                 rte_mempool_put_bulk(ctx->pool,
347                                         (void **)ops_processed, ops_deqd);
348                 ops_deqd_total += ops_deqd;
349         }
350
351         /* Dequeue any operations still in the crypto device */
352
353         while (ops_deqd_total < ctx->options->total_ops) {
354                 /* Sending 0 length burst to flush sw crypto device */
355                 rte_cryptodev_enqueue_burst(ctx->dev_id, ctx->qp_id, NULL, 0);
356
357                 /* dequeue burst */
358                 ops_deqd = rte_cryptodev_dequeue_burst(ctx->dev_id, ctx->qp_id,
359                                 ops_processed, ctx->options->max_burst_size);
360                 if (ops_deqd == 0) {
361                         ops_deqd_failed++;
362                         continue;
363                 }
364
365                 for (i = 0; i < ops_deqd; i++) {
366                         if (cperf_verify_op(ops_processed[i], ctx->options,
367                                                 ctx->test_vector))
368                                 ops_failed++;
369                 }
370                 /* Free crypto ops so they can be reused. */
371                 rte_mempool_put_bulk(ctx->pool,
372                                         (void **)ops_processed, ops_deqd);
373                 ops_deqd_total += ops_deqd;
374         }
375
376         if (!ctx->options->csv) {
377                 if (!only_once)
378                         printf("%12s%12s%12s%12s%12s%12s%12s%12s\n\n",
379                                 "lcore id", "Buf Size", "Burst size",
380                                 "Enqueued", "Dequeued", "Failed Enq",
381                                 "Failed Deq", "Failed Ops");
382                 only_once = 1;
383
384                 printf("%12u%12u%12u%12"PRIu64"%12"PRIu64"%12"PRIu64
385                                 "%12"PRIu64"%12"PRIu64"\n",
386                                 ctx->lcore_id,
387                                 ctx->options->max_buffer_size,
388                                 ctx->options->max_burst_size,
389                                 ops_enqd_total,
390                                 ops_deqd_total,
391                                 ops_enqd_failed,
392                                 ops_deqd_failed,
393                                 ops_failed);
394         } else {
395                 if (!only_once)
396                         printf("\n# lcore id, Buffer Size(B), "
397                                 "Burst Size,Enqueued,Dequeued,Failed Enq,"
398                                 "Failed Deq,Failed Ops\n");
399                 only_once = 1;
400
401                 printf("%10u;%10u;%u;%"PRIu64";%"PRIu64";%"PRIu64";%"PRIu64";"
402                                 "%"PRIu64"\n",
403                                 ctx->lcore_id,
404                                 ctx->options->max_buffer_size,
405                                 ctx->options->max_burst_size,
406                                 ops_enqd_total,
407                                 ops_deqd_total,
408                                 ops_enqd_failed,
409                                 ops_deqd_failed,
410                                 ops_failed);
411         }
412
413         return 0;
414 }
415
416
417
418 void
419 cperf_verify_test_destructor(void *arg)
420 {
421         struct cperf_verify_ctx *ctx = arg;
422
423         if (ctx == NULL)
424                 return;
425
426         cperf_verify_test_free(ctx);
427 }