20d70699bc0f2fa654802030f07f74433a6e99fc
[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 #define max(a, b) (a > b ? (uint64_t)a : (uint64_t)b)
70 #define min(a, b) (a < b ? (uint64_t)a : (uint64_t)b)
71
72 static void
73 cperf_latency_test_free(struct cperf_latency_ctx *ctx, uint32_t mbuf_nb)
74 {
75         uint32_t i;
76
77         if (ctx) {
78                 if (ctx->sess)
79                         rte_cryptodev_sym_session_free(ctx->dev_id, ctx->sess);
80
81                 if (ctx->mbufs_in) {
82                         for (i = 0; i < mbuf_nb; i++)
83                                 rte_pktmbuf_free(ctx->mbufs_in[i]);
84
85                         rte_free(ctx->mbufs_in);
86                 }
87
88                 if (ctx->mbufs_out) {
89                         for (i = 0; i < mbuf_nb; i++) {
90                                 if (ctx->mbufs_out[i] != NULL)
91                                         rte_pktmbuf_free(ctx->mbufs_out[i]);
92                         }
93
94                         rte_free(ctx->mbufs_out);
95                 }
96
97                 if (ctx->pkt_mbuf_pool_in)
98                         rte_mempool_free(ctx->pkt_mbuf_pool_in);
99
100                 if (ctx->pkt_mbuf_pool_out)
101                         rte_mempool_free(ctx->pkt_mbuf_pool_out);
102
103                 if (ctx->crypto_op_pool)
104                         rte_mempool_free(ctx->crypto_op_pool);
105
106                 rte_free(ctx->res);
107                 rte_free(ctx);
108         }
109 }
110
111 static struct rte_mbuf *
112 cperf_mbuf_create(struct rte_mempool *mempool,
113                 uint32_t segments_nb,
114                 const struct cperf_options *options,
115                 const struct cperf_test_vector *test_vector)
116 {
117         struct rte_mbuf *mbuf;
118         uint32_t segment_sz = options->buffer_sz / segments_nb;
119         uint32_t last_sz = options->buffer_sz % segments_nb;
120         uint8_t *mbuf_data;
121         uint8_t *test_data =
122                         (options->cipher_op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
123                                         test_vector->plaintext.data :
124                                         test_vector->ciphertext.data;
125
126         mbuf = rte_pktmbuf_alloc(mempool);
127         if (mbuf == NULL)
128                 goto error;
129
130         mbuf_data = (uint8_t *)rte_pktmbuf_append(mbuf, segment_sz);
131         if (mbuf_data == NULL)
132                 goto error;
133
134         memcpy(mbuf_data, test_data, segment_sz);
135         test_data += segment_sz;
136         segments_nb--;
137
138         while (segments_nb) {
139                 struct rte_mbuf *m;
140
141                 m = rte_pktmbuf_alloc(mempool);
142                 if (m == NULL)
143                         goto error;
144
145                 rte_pktmbuf_chain(mbuf, m);
146
147                 mbuf_data = (uint8_t *)rte_pktmbuf_append(mbuf, segment_sz);
148                 if (mbuf_data == NULL)
149                         goto error;
150
151                 memcpy(mbuf_data, test_data, segment_sz);
152                 test_data += segment_sz;
153                 segments_nb--;
154         }
155
156         if (last_sz) {
157                 mbuf_data = (uint8_t *)rte_pktmbuf_append(mbuf, last_sz);
158                 if (mbuf_data == NULL)
159                         goto error;
160
161                 memcpy(mbuf_data, test_data, last_sz);
162         }
163
164         if (options->op_type != CPERF_CIPHER_ONLY) {
165                 mbuf_data = (uint8_t *)rte_pktmbuf_append(mbuf,
166                         options->auth_digest_sz);
167                 if (mbuf_data == NULL)
168                         goto error;
169         }
170
171         if (options->op_type == CPERF_AEAD) {
172                 uint8_t *aead = (uint8_t *)rte_pktmbuf_prepend(mbuf,
173                         RTE_ALIGN_CEIL(options->auth_aad_sz, 16));
174
175                 if (aead == NULL)
176                         goto error;
177
178                 memcpy(aead, test_vector->aad.data, test_vector->aad.length);
179         }
180
181         return mbuf;
182 error:
183         if (mbuf != NULL)
184                 rte_pktmbuf_free(mbuf);
185
186         return NULL;
187 }
188
189 void *
190 cperf_latency_test_constructor(uint8_t dev_id, uint16_t qp_id,
191                 const struct cperf_options *options,
192                 const struct cperf_test_vector *test_vector,
193                 const struct cperf_op_fns *op_fns)
194 {
195         struct cperf_latency_ctx *ctx = NULL;
196         unsigned int mbuf_idx = 0;
197         char pool_name[32] = "";
198
199         ctx = rte_malloc(NULL, sizeof(struct cperf_latency_ctx), 0);
200         if (ctx == NULL)
201                 goto err;
202
203         ctx->dev_id = dev_id;
204         ctx->qp_id = qp_id;
205
206         ctx->populate_ops = op_fns->populate_ops;
207         ctx->options = options;
208         ctx->test_vector = test_vector;
209
210         ctx->sess = op_fns->sess_create(dev_id, options, test_vector);
211         if (ctx->sess == NULL)
212                 goto err;
213
214         snprintf(pool_name, sizeof(pool_name), "cperf_pool_in_cdev_%d",
215                                 dev_id);
216
217         ctx->pkt_mbuf_pool_in = rte_pktmbuf_pool_create(pool_name,
218                         options->pool_sz * options->segments_nb, 0, 0,
219                         RTE_PKTMBUF_HEADROOM +
220                         RTE_CACHE_LINE_ROUNDUP(
221                                 (options->buffer_sz / options->segments_nb) +
222                                 (options->buffer_sz % options->segments_nb) +
223                                         options->auth_digest_sz),
224                         rte_socket_id());
225
226         if (ctx->pkt_mbuf_pool_in == NULL)
227                 goto err;
228
229         /* Generate mbufs_in with plaintext populated for test */
230         ctx->mbufs_in = rte_malloc(NULL,
231                         (sizeof(struct rte_mbuf *) *
232                         ctx->options->pool_sz), 0);
233
234         for (mbuf_idx = 0; mbuf_idx < options->pool_sz; mbuf_idx++) {
235                 ctx->mbufs_in[mbuf_idx] = cperf_mbuf_create(
236                                 ctx->pkt_mbuf_pool_in, options->segments_nb,
237                                 options, test_vector);
238                 if (ctx->mbufs_in[mbuf_idx] == NULL)
239                         goto err;
240         }
241
242         if (options->out_of_place == 1) {
243
244                 snprintf(pool_name, sizeof(pool_name),
245                                 "cperf_pool_out_cdev_%d",
246                                 dev_id);
247
248                 ctx->pkt_mbuf_pool_out = rte_pktmbuf_pool_create(
249                                 pool_name, options->pool_sz, 0, 0,
250                                 RTE_PKTMBUF_HEADROOM +
251                                 RTE_CACHE_LINE_ROUNDUP(
252                                         options->buffer_sz +
253                                         options->auth_digest_sz),
254                                 rte_socket_id());
255
256                 if (ctx->pkt_mbuf_pool_out == NULL)
257                         goto err;
258         }
259
260         ctx->mbufs_out = rte_malloc(NULL,
261                         (sizeof(struct rte_mbuf *) *
262                         ctx->options->pool_sz), 0);
263
264         for (mbuf_idx = 0; mbuf_idx < options->pool_sz; mbuf_idx++) {
265                 if (options->out_of_place == 1) {
266                         ctx->mbufs_out[mbuf_idx] = cperf_mbuf_create(
267                                         ctx->pkt_mbuf_pool_out, 1,
268                                         options, test_vector);
269                         if (ctx->mbufs_out[mbuf_idx] == NULL)
270                                 goto err;
271                 } else {
272                         ctx->mbufs_out[mbuf_idx] = NULL;
273                 }
274         }
275
276         snprintf(pool_name, sizeof(pool_name), "cperf_op_pool_cdev_%d",
277                         dev_id);
278
279         ctx->crypto_op_pool = rte_crypto_op_pool_create(pool_name,
280                         RTE_CRYPTO_OP_TYPE_SYMMETRIC, options->pool_sz, 0, 0,
281                         rte_socket_id());
282         if (ctx->crypto_op_pool == NULL)
283                 goto err;
284
285         ctx->res = rte_malloc(NULL, sizeof(struct cperf_op_result) *
286                         ctx->options->total_ops, 0);
287
288         if (ctx->res == NULL)
289                 goto err;
290
291         return ctx;
292 err:
293         cperf_latency_test_free(ctx, mbuf_idx);
294
295         return NULL;
296 }
297
298 int
299 cperf_latency_test_runner(void *arg)
300 {
301         struct cperf_latency_ctx *ctx = arg;
302         struct cperf_op_result *pres;
303
304         static int only_once;
305
306         if (ctx == NULL)
307                 return 0;
308
309         struct rte_crypto_op *ops[ctx->options->burst_sz];
310         struct rte_crypto_op *ops_processed[ctx->options->burst_sz];
311         uint64_t i;
312
313         uint32_t lcore = rte_lcore_id();
314
315 #ifdef CPERF_LINEARIZATION_ENABLE
316         struct rte_cryptodev_info dev_info;
317         int linearize = 0;
318
319         /* Check if source mbufs require coalescing */
320         if (ctx->options->segments_nb > 1) {
321                 rte_cryptodev_info_get(ctx->dev_id, &dev_info);
322                 if ((dev_info.feature_flags &
323                                 RTE_CRYPTODEV_FF_MBUF_SCATTER_GATHER) == 0)
324                         linearize = 1;
325         }
326 #endif /* CPERF_LINEARIZATION_ENABLE */
327
328         ctx->lcore_id = lcore;
329
330         /* Warm up the host CPU before starting the test */
331         for (i = 0; i < ctx->options->total_ops; i++)
332                 rte_cryptodev_enqueue_burst(ctx->dev_id, ctx->qp_id, NULL, 0);
333
334         uint64_t ops_enqd = 0, ops_deqd = 0;
335         uint64_t m_idx = 0, b_idx = 0;
336
337         uint64_t tsc_val, tsc_end, tsc_start;
338         uint64_t tsc_max = 0, tsc_min = ~0UL, tsc_tot = 0, tsc_idx = 0;
339         uint64_t enqd_max = 0, enqd_min = ~0UL, enqd_tot = 0;
340         uint64_t deqd_max = 0, deqd_min = ~0UL, deqd_tot = 0;
341
342         while (enqd_tot < ctx->options->total_ops) {
343                 uint16_t burst_size = ((enqd_tot + ctx->options->burst_sz)
344                                 <= ctx->options->total_ops) ?
345                                                 ctx->options->burst_sz :
346                                                 ctx->options->total_ops -
347                                                 enqd_tot;
348
349                 /* Allocate crypto ops from pool */
350                 if (burst_size != rte_crypto_op_bulk_alloc(
351                                 ctx->crypto_op_pool,
352                                 RTE_CRYPTO_OP_TYPE_SYMMETRIC,
353                                 ops, burst_size))
354                         return -1;
355
356                 /* Setup crypto op, attach mbuf etc */
357                 (ctx->populate_ops)(ops, &ctx->mbufs_in[m_idx],
358                                 &ctx->mbufs_out[m_idx],
359                                 burst_size, ctx->sess, ctx->options,
360                                 ctx->test_vector);
361
362                 tsc_start = rte_rdtsc_precise();
363
364 #ifdef CPERF_LINEARIZATION_ENABLE
365                 if (linearize) {
366                         /* PMD doesn't support scatter-gather and source buffer
367                          * is segmented.
368                          * We need to linearize it before enqueuing.
369                          */
370                         for (i = 0; i < burst_size; i++)
371                                 rte_pktmbuf_linearize(ops[i]->sym->m_src);
372                 }
373 #endif /* CPERF_LINEARIZATION_ENABLE */
374
375                 /* Enqueue burst of ops on crypto device */
376                 ops_enqd = rte_cryptodev_enqueue_burst(ctx->dev_id, ctx->qp_id,
377                                 ops, burst_size);
378
379                 /* Dequeue processed burst of ops from crypto device */
380                 ops_deqd = rte_cryptodev_dequeue_burst(ctx->dev_id, ctx->qp_id,
381                                 ops_processed, ctx->options->burst_sz);
382
383                 tsc_end = rte_rdtsc_precise();
384
385                 for (i = 0; i < ops_enqd; i++) {
386                         ctx->res[tsc_idx].tsc_start = tsc_start;
387                         ops[i]->opaque_data = (void *)&ctx->res[tsc_idx];
388                         tsc_idx++;
389                 }
390
391                 /* Free memory for not enqueued operations */
392                 for (i = ops_enqd; i < burst_size; i++)
393                         rte_crypto_op_free(ops[i]);
394
395                 if (likely(ops_deqd))  {
396                         /*
397                          * free crypto ops so they can be reused. We don't free
398                          * the mbufs here as we don't want to reuse them as
399                          * the crypto operation will change the data and cause
400                          * failures.
401                          */
402                         for (i = 0; i < ops_deqd; i++) {
403                                 pres = (struct cperf_op_result *)
404                                                 (ops_processed[i]->opaque_data);
405                                 pres->status = ops_processed[i]->status;
406                                 pres->tsc_end = tsc_end;
407
408                                 rte_crypto_op_free(ops_processed[i]);
409                         }
410
411                         deqd_tot += ops_deqd;
412                         deqd_max = max(ops_deqd, deqd_max);
413                         deqd_min = min(ops_deqd, deqd_min);
414                 }
415
416                 enqd_tot += ops_enqd;
417                 enqd_max = max(ops_enqd, enqd_max);
418                 enqd_min = min(ops_enqd, enqd_min);
419
420                 m_idx += ops_enqd;
421                 m_idx = m_idx + ctx->options->burst_sz > ctx->options->pool_sz ?
422                                 0 : m_idx;
423                 b_idx++;
424         }
425
426         /* Dequeue any operations still in the crypto device */
427         while (deqd_tot < ctx->options->total_ops) {
428                 /* Sending 0 length burst to flush sw crypto device */
429                 rte_cryptodev_enqueue_burst(ctx->dev_id, ctx->qp_id, NULL, 0);
430
431                 /* dequeue burst */
432                 ops_deqd = rte_cryptodev_dequeue_burst(ctx->dev_id, ctx->qp_id,
433                                 ops_processed, ctx->options->burst_sz);
434
435                 tsc_end = rte_rdtsc_precise();
436
437                 if (ops_deqd != 0) {
438                         for (i = 0; i < ops_deqd; i++) {
439                                 pres = (struct cperf_op_result *)
440                                                 (ops_processed[i]->opaque_data);
441                                 pres->status = ops_processed[i]->status;
442                                 pres->tsc_end = tsc_end;
443
444                                 rte_crypto_op_free(ops_processed[i]);
445                         }
446
447                         deqd_tot += ops_deqd;
448                         deqd_max = max(ops_deqd, deqd_max);
449                         deqd_min = min(ops_deqd, deqd_min);
450                 }
451         }
452
453         for (i = 0; i < tsc_idx; i++) {
454                 tsc_val = ctx->res[i].tsc_end - ctx->res[i].tsc_start;
455                 tsc_max = max(tsc_val, tsc_max);
456                 tsc_min = min(tsc_val, tsc_min);
457                 tsc_tot += tsc_val;
458         }
459
460         double time_tot, time_avg, time_max, time_min;
461
462         const uint64_t tunit = 1000000; /* us */
463         const uint64_t tsc_hz = rte_get_tsc_hz();
464
465         uint64_t enqd_avg = enqd_tot / b_idx;
466         uint64_t deqd_avg = deqd_tot / b_idx;
467         uint64_t tsc_avg = tsc_tot / tsc_idx;
468
469         time_tot = tunit*(double)(tsc_tot) / tsc_hz;
470         time_avg = tunit*(double)(tsc_avg) / tsc_hz;
471         time_max = tunit*(double)(tsc_max) / tsc_hz;
472         time_min = tunit*(double)(tsc_min) / tsc_hz;
473
474         if (ctx->options->csv) {
475                 if (!only_once)
476                         printf("\n# lcore, Buffer Size, Burst Size, Pakt Seq #, "
477                                         "Packet Size, cycles, time (us)");
478
479                 for (i = 0; i < ctx->options->total_ops; i++) {
480
481                         printf("\n%u;%u;%u;%"PRIu64";%"PRIu64";%.3f",
482                                 ctx->lcore_id, ctx->options->buffer_sz,
483                                 ctx->options->burst_sz, i + 1,
484                                 ctx->res[i].tsc_end - ctx->res[i].tsc_start,
485                                 tunit * (double) (ctx->res[i].tsc_end
486                                                 - ctx->res[i].tsc_start)
487                                         / tsc_hz);
488
489                 }
490                 only_once = 1;
491         } else {
492                 printf("\n# Device %d on lcore %u\n", ctx->dev_id,
493                         ctx->lcore_id);
494                 printf("\n# total operations: %u", ctx->options->total_ops);
495                 printf("\n# Buffer size: %u", ctx->options->buffer_sz);
496                 printf("\n# Burst size: %u", ctx->options->burst_sz);
497                 printf("\n#     Number of bursts: %"PRIu64,
498                                 b_idx);
499
500                 printf("\n#");
501                 printf("\n#          \t       Total\t   Average\t   "
502                                 "Maximum\t   Minimum");
503                 printf("\n#  enqueued\t%12"PRIu64"\t%10"PRIu64"\t"
504                                 "%10"PRIu64"\t%10"PRIu64, enqd_tot,
505                                 enqd_avg, enqd_max, enqd_min);
506                 printf("\n#  dequeued\t%12"PRIu64"\t%10"PRIu64"\t"
507                                 "%10"PRIu64"\t%10"PRIu64, deqd_tot,
508                                 deqd_avg, deqd_max, deqd_min);
509                 printf("\n#    cycles\t%12"PRIu64"\t%10"PRIu64"\t"
510                                 "%10"PRIu64"\t%10"PRIu64, tsc_tot,
511                                 tsc_avg, tsc_max, tsc_min);
512                 printf("\n# time [us]\t%12.0f\t%10.3f\t%10.3f\t%10.3f",
513                                 time_tot, time_avg, time_max, time_min);
514                 printf("\n\n");
515
516         }
517
518         return 0;
519 }
520
521 void
522 cperf_latency_test_destructor(void *arg)
523 {
524         struct cperf_latency_ctx *ctx = arg;
525
526         if (ctx == NULL)
527                 return;
528
529         cperf_latency_test_free(ctx, ctx->options->pool_sz);
530
531 }