cryptodev: remove opaque data pointer in crypto op
[dpdk.git] / app / test-crypto-perf / cperf_test_latency.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2016-2017 Intel Corporation. All rights reserved.
5  *
6  *   Redistribution and use in source and binary forms, with or without
7  *   modification, are permitted provided that the following conditions
8  *   are met:
9  *
10  *     * Redistributions of source code must retain the above copyright
11  *       notice, this list of conditions and the following disclaimer.
12  *     * Redistributions in binary form must reproduce the above copyright
13  *       notice, this list of conditions and the following disclaimer in
14  *       the documentation and/or other materials provided with the
15  *       distribution.
16  *     * Neither the name of Intel Corporation nor the names of its
17  *       contributors may be used to endorse or promote products derived
18  *       from this software without specific prior written permission.
19  *
20  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include <rte_malloc.h>
34 #include <rte_cycles.h>
35 #include <rte_crypto.h>
36 #include <rte_cryptodev.h>
37
38 #include "cperf_test_latency.h"
39 #include "cperf_ops.h"
40
41
42 struct cperf_op_result {
43         uint64_t tsc_start;
44         uint64_t tsc_end;
45         enum rte_crypto_op_status status;
46 };
47
48 struct cperf_latency_ctx {
49         uint8_t dev_id;
50         uint16_t qp_id;
51         uint8_t lcore_id;
52
53         struct rte_mempool *pkt_mbuf_pool_in;
54         struct rte_mempool *pkt_mbuf_pool_out;
55         struct rte_mbuf **mbufs_in;
56         struct rte_mbuf **mbufs_out;
57
58         struct rte_mempool *crypto_op_pool;
59
60         struct rte_cryptodev_sym_session *sess;
61
62         cperf_populate_ops_t populate_ops;
63
64         const struct cperf_options *options;
65         const struct cperf_test_vector *test_vector;
66         struct cperf_op_result *res;
67 };
68
69 struct priv_op_data {
70         struct cperf_op_result *result;
71 };
72
73 #define max(a, b) (a > b ? (uint64_t)a : (uint64_t)b)
74 #define min(a, b) (a < b ? (uint64_t)a : (uint64_t)b)
75
76 static void
77 cperf_latency_test_free(struct cperf_latency_ctx *ctx, uint32_t mbuf_nb)
78 {
79         uint32_t i;
80
81         if (ctx) {
82                 if (ctx->sess)
83                         rte_cryptodev_sym_session_free(ctx->dev_id, ctx->sess);
84
85                 if (ctx->mbufs_in) {
86                         for (i = 0; i < mbuf_nb; i++)
87                                 rte_pktmbuf_free(ctx->mbufs_in[i]);
88
89                         rte_free(ctx->mbufs_in);
90                 }
91
92                 if (ctx->mbufs_out) {
93                         for (i = 0; i < mbuf_nb; i++) {
94                                 if (ctx->mbufs_out[i] != NULL)
95                                         rte_pktmbuf_free(ctx->mbufs_out[i]);
96                         }
97
98                         rte_free(ctx->mbufs_out);
99                 }
100
101                 if (ctx->pkt_mbuf_pool_in)
102                         rte_mempool_free(ctx->pkt_mbuf_pool_in);
103
104                 if (ctx->pkt_mbuf_pool_out)
105                         rte_mempool_free(ctx->pkt_mbuf_pool_out);
106
107                 if (ctx->crypto_op_pool)
108                         rte_mempool_free(ctx->crypto_op_pool);
109
110                 rte_free(ctx->res);
111                 rte_free(ctx);
112         }
113 }
114
115 static struct rte_mbuf *
116 cperf_mbuf_create(struct rte_mempool *mempool,
117                 uint32_t segments_nb,
118                 const struct cperf_options *options,
119                 const struct cperf_test_vector *test_vector)
120 {
121         struct rte_mbuf *mbuf;
122         uint32_t segment_sz = options->max_buffer_size / segments_nb;
123         uint32_t last_sz = options->max_buffer_size % segments_nb;
124         uint8_t *mbuf_data;
125         uint8_t *test_data =
126                         (options->cipher_op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
127                                         test_vector->plaintext.data :
128                                         test_vector->ciphertext.data;
129
130         mbuf = rte_pktmbuf_alloc(mempool);
131         if (mbuf == NULL)
132                 goto error;
133
134         mbuf_data = (uint8_t *)rte_pktmbuf_append(mbuf, segment_sz);
135         if (mbuf_data == NULL)
136                 goto error;
137
138         memcpy(mbuf_data, test_data, segment_sz);
139         test_data += segment_sz;
140         segments_nb--;
141
142         while (segments_nb) {
143                 struct rte_mbuf *m;
144
145                 m = rte_pktmbuf_alloc(mempool);
146                 if (m == NULL)
147                         goto error;
148
149                 rte_pktmbuf_chain(mbuf, m);
150
151                 mbuf_data = (uint8_t *)rte_pktmbuf_append(mbuf, segment_sz);
152                 if (mbuf_data == NULL)
153                         goto error;
154
155                 memcpy(mbuf_data, test_data, segment_sz);
156                 test_data += segment_sz;
157                 segments_nb--;
158         }
159
160         if (last_sz) {
161                 mbuf_data = (uint8_t *)rte_pktmbuf_append(mbuf, last_sz);
162                 if (mbuf_data == NULL)
163                         goto error;
164
165                 memcpy(mbuf_data, test_data, last_sz);
166         }
167
168         if (options->op_type != CPERF_CIPHER_ONLY) {
169                 mbuf_data = (uint8_t *)rte_pktmbuf_append(mbuf,
170                         options->auth_digest_sz);
171                 if (mbuf_data == NULL)
172                         goto error;
173         }
174
175         if (options->op_type == CPERF_AEAD) {
176                 uint8_t *aead = (uint8_t *)rte_pktmbuf_prepend(mbuf,
177                         RTE_ALIGN_CEIL(options->auth_aad_sz, 16));
178
179                 if (aead == NULL)
180                         goto error;
181
182                 memcpy(aead, test_vector->aad.data, test_vector->aad.length);
183         }
184
185         return mbuf;
186 error:
187         if (mbuf != NULL)
188                 rte_pktmbuf_free(mbuf);
189
190         return NULL;
191 }
192
193 void *
194 cperf_latency_test_constructor(uint8_t dev_id, uint16_t qp_id,
195                 const struct cperf_options *options,
196                 const struct cperf_test_vector *test_vector,
197                 const struct cperf_op_fns *op_fns)
198 {
199         struct cperf_latency_ctx *ctx = NULL;
200         unsigned int mbuf_idx = 0;
201         char pool_name[32] = "";
202
203         ctx = rte_malloc(NULL, sizeof(struct cperf_latency_ctx), 0);
204         if (ctx == NULL)
205                 goto err;
206
207         ctx->dev_id = dev_id;
208         ctx->qp_id = qp_id;
209
210         ctx->populate_ops = op_fns->populate_ops;
211         ctx->options = options;
212         ctx->test_vector = test_vector;
213
214         ctx->sess = op_fns->sess_create(dev_id, options, test_vector);
215         if (ctx->sess == NULL)
216                 goto err;
217
218         snprintf(pool_name, sizeof(pool_name), "cperf_pool_in_cdev_%d",
219                                 dev_id);
220
221         ctx->pkt_mbuf_pool_in = rte_pktmbuf_pool_create(pool_name,
222                         options->pool_sz * options->segments_nb, 0, 0,
223                         RTE_PKTMBUF_HEADROOM +
224                         RTE_CACHE_LINE_ROUNDUP(
225                                 (options->max_buffer_size / options->segments_nb) +
226                                 (options->max_buffer_size % options->segments_nb) +
227                                         options->auth_digest_sz),
228                         rte_socket_id());
229
230         if (ctx->pkt_mbuf_pool_in == NULL)
231                 goto err;
232
233         /* Generate mbufs_in with plaintext populated for test */
234         ctx->mbufs_in = rte_malloc(NULL,
235                         (sizeof(struct rte_mbuf *) *
236                         ctx->options->pool_sz), 0);
237
238         for (mbuf_idx = 0; mbuf_idx < options->pool_sz; mbuf_idx++) {
239                 ctx->mbufs_in[mbuf_idx] = cperf_mbuf_create(
240                                 ctx->pkt_mbuf_pool_in, options->segments_nb,
241                                 options, test_vector);
242                 if (ctx->mbufs_in[mbuf_idx] == NULL)
243                         goto err;
244         }
245
246         if (options->out_of_place == 1) {
247
248                 snprintf(pool_name, sizeof(pool_name),
249                                 "cperf_pool_out_cdev_%d",
250                                 dev_id);
251
252                 ctx->pkt_mbuf_pool_out = rte_pktmbuf_pool_create(
253                                 pool_name, options->pool_sz, 0, 0,
254                                 RTE_PKTMBUF_HEADROOM +
255                                 RTE_CACHE_LINE_ROUNDUP(
256                                         options->max_buffer_size +
257                                         options->auth_digest_sz),
258                                 rte_socket_id());
259
260                 if (ctx->pkt_mbuf_pool_out == NULL)
261                         goto err;
262         }
263
264         ctx->mbufs_out = rte_malloc(NULL,
265                         (sizeof(struct rte_mbuf *) *
266                         ctx->options->pool_sz), 0);
267
268         for (mbuf_idx = 0; mbuf_idx < options->pool_sz; mbuf_idx++) {
269                 if (options->out_of_place == 1) {
270                         ctx->mbufs_out[mbuf_idx] = cperf_mbuf_create(
271                                         ctx->pkt_mbuf_pool_out, 1,
272                                         options, test_vector);
273                         if (ctx->mbufs_out[mbuf_idx] == NULL)
274                                 goto err;
275                 } else {
276                         ctx->mbufs_out[mbuf_idx] = NULL;
277                 }
278         }
279
280         snprintf(pool_name, sizeof(pool_name), "cperf_op_pool_cdev_%d",
281                         dev_id);
282
283         uint16_t priv_size = sizeof(struct priv_op_data);
284         ctx->crypto_op_pool = rte_crypto_op_pool_create(pool_name,
285                         RTE_CRYPTO_OP_TYPE_SYMMETRIC, options->pool_sz,
286                         512, priv_size, rte_socket_id());
287
288         if (ctx->crypto_op_pool == NULL)
289                 goto err;
290
291         ctx->res = rte_malloc(NULL, sizeof(struct cperf_op_result) *
292                         ctx->options->total_ops, 0);
293
294         if (ctx->res == NULL)
295                 goto err;
296
297         return ctx;
298 err:
299         cperf_latency_test_free(ctx, mbuf_idx);
300
301         return NULL;
302 }
303
304 static inline void
305 store_timestamp(struct rte_crypto_op *op, uint64_t timestamp)
306 {
307         struct priv_op_data *priv_data;
308
309         priv_data = (struct priv_op_data *) (op->sym + 1);
310         priv_data->result->status = op->status;
311         priv_data->result->tsc_end = timestamp;
312 }
313
314 int
315 cperf_latency_test_runner(void *arg)
316 {
317         struct cperf_latency_ctx *ctx = arg;
318         uint16_t test_burst_size;
319         uint8_t burst_size_idx = 0;
320
321         static int only_once;
322
323         if (ctx == NULL)
324                 return 0;
325
326         struct rte_crypto_op *ops[ctx->options->max_burst_size];
327         struct rte_crypto_op *ops_processed[ctx->options->max_burst_size];
328         uint64_t i;
329         struct priv_op_data *priv_data;
330
331         uint32_t lcore = rte_lcore_id();
332
333 #ifdef CPERF_LINEARIZATION_ENABLE
334         struct rte_cryptodev_info dev_info;
335         int linearize = 0;
336
337         /* Check if source mbufs require coalescing */
338         if (ctx->options->segments_nb > 1) {
339                 rte_cryptodev_info_get(ctx->dev_id, &dev_info);
340                 if ((dev_info.feature_flags &
341                                 RTE_CRYPTODEV_FF_MBUF_SCATTER_GATHER) == 0)
342                         linearize = 1;
343         }
344 #endif /* CPERF_LINEARIZATION_ENABLE */
345
346         ctx->lcore_id = lcore;
347
348         /* Warm up the host CPU before starting the test */
349         for (i = 0; i < ctx->options->total_ops; i++)
350                 rte_cryptodev_enqueue_burst(ctx->dev_id, ctx->qp_id, NULL, 0);
351
352         /* Get first size from range or list */
353         if (ctx->options->inc_burst_size != 0)
354                 test_burst_size = ctx->options->min_burst_size;
355         else
356                 test_burst_size = ctx->options->burst_size_list[0];
357
358         while (test_burst_size <= ctx->options->max_burst_size) {
359                 uint64_t ops_enqd = 0, ops_deqd = 0;
360                 uint64_t m_idx = 0, b_idx = 0;
361
362                 uint64_t tsc_val, tsc_end, tsc_start;
363                 uint64_t tsc_max = 0, tsc_min = ~0UL, tsc_tot = 0, tsc_idx = 0;
364                 uint64_t enqd_max = 0, enqd_min = ~0UL, enqd_tot = 0;
365                 uint64_t deqd_max = 0, deqd_min = ~0UL, deqd_tot = 0;
366
367                 while (enqd_tot < ctx->options->total_ops) {
368
369                         uint16_t burst_size = ((enqd_tot + test_burst_size)
370                                         <= ctx->options->total_ops) ?
371                                                         test_burst_size :
372                                                         ctx->options->total_ops -
373                                                         enqd_tot;
374
375                         /* Allocate crypto ops from pool */
376                         if (burst_size != rte_crypto_op_bulk_alloc(
377                                         ctx->crypto_op_pool,
378                                         RTE_CRYPTO_OP_TYPE_SYMMETRIC,
379                                         ops, burst_size))
380                                 return -1;
381
382                         /* Setup crypto op, attach mbuf etc */
383                         (ctx->populate_ops)(ops, &ctx->mbufs_in[m_idx],
384                                         &ctx->mbufs_out[m_idx],
385                                         burst_size, ctx->sess, ctx->options,
386                                         ctx->test_vector);
387
388                         tsc_start = rte_rdtsc_precise();
389
390 #ifdef CPERF_LINEARIZATION_ENABLE
391                         if (linearize) {
392                                 /* PMD doesn't support scatter-gather and source buffer
393                                  * is segmented.
394                                  * We need to linearize it before enqueuing.
395                                  */
396                                 for (i = 0; i < burst_size; i++)
397                                         rte_pktmbuf_linearize(ops[i]->sym->m_src);
398                         }
399 #endif /* CPERF_LINEARIZATION_ENABLE */
400
401                         /* Enqueue burst of ops on crypto device */
402                         ops_enqd = rte_cryptodev_enqueue_burst(ctx->dev_id, ctx->qp_id,
403                                         ops, burst_size);
404
405                         /* Dequeue processed burst of ops from crypto device */
406                         ops_deqd = rte_cryptodev_dequeue_burst(ctx->dev_id, ctx->qp_id,
407                                         ops_processed, test_burst_size);
408
409                         tsc_end = rte_rdtsc_precise();
410
411                         /* Free memory for not enqueued operations */
412                         if (ops_enqd != burst_size)
413                                 rte_mempool_put_bulk(ctx->crypto_op_pool,
414                                                 (void **)&ops_processed[ops_enqd],
415                                                 burst_size - ops_enqd);
416
417                         for (i = 0; i < ops_enqd; i++) {
418                                 ctx->res[tsc_idx].tsc_start = tsc_start;
419                                 /*
420                                  * Private data structure starts after the end of the
421                                  * rte_crypto_sym_op structure.
422                                  */
423                                 priv_data = (struct priv_op_data *) (ops[i]->sym + 1);
424                                 priv_data->result = (void *)&ctx->res[tsc_idx];
425                                 tsc_idx++;
426                         }
427
428                         if (likely(ops_deqd))  {
429                                 /*
430                                  * free crypto ops so they can be reused. We don't free
431                                  * the mbufs here as we don't want to reuse them as
432                                  * the crypto operation will change the data and cause
433                                  * failures.
434                                  */
435                                 for (i = 0; i < ops_deqd; i++)
436                                         store_timestamp(ops_processed[i], tsc_end);
437
438                                 rte_mempool_put_bulk(ctx->crypto_op_pool,
439                                                 (void **)ops_processed, ops_deqd);
440
441                                 deqd_tot += ops_deqd;
442                                 deqd_max = max(ops_deqd, deqd_max);
443                                 deqd_min = min(ops_deqd, deqd_min);
444                         }
445
446                         enqd_tot += ops_enqd;
447                         enqd_max = max(ops_enqd, enqd_max);
448                         enqd_min = min(ops_enqd, enqd_min);
449
450                         m_idx += ops_enqd;
451                         m_idx = m_idx + test_burst_size > ctx->options->pool_sz ?
452                                         0 : m_idx;
453                         b_idx++;
454                 }
455
456                 /* Dequeue any operations still in the crypto device */
457                 while (deqd_tot < ctx->options->total_ops) {
458                         /* Sending 0 length burst to flush sw crypto device */
459                         rte_cryptodev_enqueue_burst(ctx->dev_id, ctx->qp_id, NULL, 0);
460
461                         /* dequeue burst */
462                         ops_deqd = rte_cryptodev_dequeue_burst(ctx->dev_id, ctx->qp_id,
463                                         ops_processed, test_burst_size);
464
465                         tsc_end = rte_rdtsc_precise();
466
467                         if (ops_deqd != 0) {
468                                 for (i = 0; i < ops_deqd; i++)
469                                         store_timestamp(ops_processed[i], tsc_end);
470
471                                 rte_mempool_put_bulk(ctx->crypto_op_pool,
472                                                 (void **)ops_processed, ops_deqd);
473
474                                 deqd_tot += ops_deqd;
475                                 deqd_max = max(ops_deqd, deqd_max);
476                                 deqd_min = min(ops_deqd, deqd_min);
477                         }
478                 }
479
480                 for (i = 0; i < tsc_idx; i++) {
481                         tsc_val = ctx->res[i].tsc_end - ctx->res[i].tsc_start;
482                         tsc_max = max(tsc_val, tsc_max);
483                         tsc_min = min(tsc_val, tsc_min);
484                         tsc_tot += tsc_val;
485                 }
486
487                 double time_tot, time_avg, time_max, time_min;
488
489                 const uint64_t tunit = 1000000; /* us */
490                 const uint64_t tsc_hz = rte_get_tsc_hz();
491
492                 uint64_t enqd_avg = enqd_tot / b_idx;
493                 uint64_t deqd_avg = deqd_tot / b_idx;
494                 uint64_t tsc_avg = tsc_tot / tsc_idx;
495
496                 time_tot = tunit*(double)(tsc_tot) / tsc_hz;
497                 time_avg = tunit*(double)(tsc_avg) / tsc_hz;
498                 time_max = tunit*(double)(tsc_max) / tsc_hz;
499                 time_min = tunit*(double)(tsc_min) / tsc_hz;
500
501                 if (ctx->options->csv) {
502                         if (!only_once)
503                                 printf("\n# lcore, Buffer Size, Burst Size, Pakt Seq #, "
504                                                 "Packet Size, cycles, time (us)");
505
506                         for (i = 0; i < ctx->options->total_ops; i++) {
507
508                                 printf("\n%u;%u;%u;%"PRIu64";%"PRIu64";%.3f",
509                                         ctx->lcore_id, ctx->options->test_buffer_size,
510                                         test_burst_size, i + 1,
511                                         ctx->res[i].tsc_end - ctx->res[i].tsc_start,
512                                         tunit * (double) (ctx->res[i].tsc_end
513                                                         - ctx->res[i].tsc_start)
514                                                 / tsc_hz);
515
516                         }
517                         only_once = 1;
518                 } else {
519                         printf("\n# Device %d on lcore %u\n", ctx->dev_id,
520                                 ctx->lcore_id);
521                         printf("\n# total operations: %u", ctx->options->total_ops);
522                         printf("\n# Buffer size: %u", ctx->options->test_buffer_size);
523                         printf("\n# Burst size: %u", test_burst_size);
524                         printf("\n#     Number of bursts: %"PRIu64,
525                                         b_idx);
526
527                         printf("\n#");
528                         printf("\n#          \t       Total\t   Average\t   "
529                                         "Maximum\t   Minimum");
530                         printf("\n#  enqueued\t%12"PRIu64"\t%10"PRIu64"\t"
531                                         "%10"PRIu64"\t%10"PRIu64, enqd_tot,
532                                         enqd_avg, enqd_max, enqd_min);
533                         printf("\n#  dequeued\t%12"PRIu64"\t%10"PRIu64"\t"
534                                         "%10"PRIu64"\t%10"PRIu64, deqd_tot,
535                                         deqd_avg, deqd_max, deqd_min);
536                         printf("\n#    cycles\t%12"PRIu64"\t%10"PRIu64"\t"
537                                         "%10"PRIu64"\t%10"PRIu64, tsc_tot,
538                                         tsc_avg, tsc_max, tsc_min);
539                         printf("\n# time [us]\t%12.0f\t%10.3f\t%10.3f\t%10.3f",
540                                         time_tot, time_avg, time_max, time_min);
541                         printf("\n\n");
542
543                 }
544
545                 /* Get next size from range or list */
546                 if (ctx->options->inc_burst_size != 0)
547                         test_burst_size += ctx->options->inc_burst_size;
548                 else {
549                         if (++burst_size_idx == ctx->options->burst_size_count)
550                                 break;
551                         test_burst_size =
552                                 ctx->options->burst_size_list[burst_size_idx];
553                 }
554         }
555
556         return 0;
557 }
558
559 void
560 cperf_latency_test_destructor(void *arg)
561 {
562         struct cperf_latency_ctx *ctx = arg;
563
564         if (ctx == NULL)
565                 return;
566
567         cperf_latency_test_free(ctx, ctx->options->pool_sz);
568
569 }