32c5bad20303de09b2c34294aa1f004b12bb2ce1
[dpdk.git] / app / test-crypto-perf / cperf_test_throughput.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_throughput.h"
39 #include "cperf_ops.h"
40
41 struct cperf_throughput_ctx {
42         uint8_t dev_id;
43         uint16_t qp_id;
44         uint8_t lcore_id;
45
46         struct rte_mempool *pkt_mbuf_pool_in;
47         struct rte_mempool *pkt_mbuf_pool_out;
48         struct rte_mbuf **mbufs_in;
49         struct rte_mbuf **mbufs_out;
50
51         struct rte_mempool *crypto_op_pool;
52
53         struct rte_cryptodev_sym_session *sess;
54
55         cperf_populate_ops_t populate_ops;
56
57         const struct cperf_options *options;
58         const struct cperf_test_vector *test_vector;
59 };
60
61 static void
62 cperf_throughput_test_free(struct cperf_throughput_ctx *ctx, uint32_t mbuf_nb)
63 {
64         uint32_t i;
65
66         if (ctx) {
67                 if (ctx->sess)
68                         rte_cryptodev_sym_session_free(ctx->dev_id, ctx->sess);
69
70                 if (ctx->mbufs_in) {
71                         for (i = 0; i < mbuf_nb; i++)
72                                 rte_pktmbuf_free(ctx->mbufs_in[i]);
73
74                         rte_free(ctx->mbufs_in);
75                 }
76
77                 if (ctx->mbufs_out) {
78                         for (i = 0; i < mbuf_nb; i++) {
79                                 if (ctx->mbufs_out[i] != NULL)
80                                         rte_pktmbuf_free(ctx->mbufs_out[i]);
81                         }
82
83                         rte_free(ctx->mbufs_out);
84                 }
85
86                 if (ctx->pkt_mbuf_pool_in)
87                         rte_mempool_free(ctx->pkt_mbuf_pool_in);
88
89                 if (ctx->pkt_mbuf_pool_out)
90                         rte_mempool_free(ctx->pkt_mbuf_pool_out);
91
92                 if (ctx->crypto_op_pool)
93                         rte_mempool_free(ctx->crypto_op_pool);
94
95                 rte_free(ctx);
96         }
97 }
98
99 static struct rte_mbuf *
100 cperf_mbuf_create(struct rte_mempool *mempool,
101                 uint32_t segments_nb,
102                 const struct cperf_options *options,
103                 const struct cperf_test_vector *test_vector)
104 {
105         struct rte_mbuf *mbuf;
106         uint32_t segment_sz = options->buffer_sz / segments_nb;
107         uint32_t last_sz = options->buffer_sz % segments_nb;
108         uint8_t *mbuf_data;
109         uint8_t *test_data =
110                         (options->cipher_op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
111                                         test_vector->plaintext.data :
112                                         test_vector->ciphertext.data;
113
114         mbuf = rte_pktmbuf_alloc(mempool);
115         if (mbuf == NULL)
116                 goto error;
117
118         mbuf_data = (uint8_t *)rte_pktmbuf_append(mbuf, segment_sz);
119         if (mbuf_data == NULL)
120                 goto error;
121
122         memcpy(mbuf_data, test_data, segment_sz);
123         test_data += segment_sz;
124         segments_nb--;
125
126         while (segments_nb) {
127                 struct rte_mbuf *m;
128
129                 m = rte_pktmbuf_alloc(mempool);
130                 if (m == NULL)
131                         goto error;
132
133                 rte_pktmbuf_chain(mbuf, m);
134
135                 mbuf_data = (uint8_t *)rte_pktmbuf_append(mbuf, segment_sz);
136                 if (mbuf_data == NULL)
137                         goto error;
138
139                 memcpy(mbuf_data, test_data, segment_sz);
140                 test_data += segment_sz;
141                 segments_nb--;
142         }
143
144         if (last_sz) {
145                 mbuf_data = (uint8_t *)rte_pktmbuf_append(mbuf, last_sz);
146                 if (mbuf_data == NULL)
147                         goto error;
148
149                 memcpy(mbuf_data, test_data, last_sz);
150         }
151
152         if (options->op_type != CPERF_CIPHER_ONLY) {
153                 mbuf_data = (uint8_t *)rte_pktmbuf_append(mbuf,
154                                 options->auth_digest_sz);
155                 if (mbuf_data == NULL)
156                         goto error;
157         }
158
159         if (options->op_type == CPERF_AEAD) {
160                 uint8_t *aead = (uint8_t *)rte_pktmbuf_prepend(mbuf,
161                         RTE_ALIGN_CEIL(options->auth_aad_sz, 16));
162
163                 if (aead == NULL)
164                         goto error;
165
166                 memcpy(aead, test_vector->aad.data, test_vector->aad.length);
167         }
168
169         return mbuf;
170 error:
171         if (mbuf != NULL)
172                 rte_pktmbuf_free(mbuf);
173
174         return NULL;
175 }
176
177 void *
178 cperf_throughput_test_constructor(uint8_t dev_id, uint16_t qp_id,
179                 const struct cperf_options *options,
180                 const struct cperf_test_vector *test_vector,
181                 const struct cperf_op_fns *op_fns)
182 {
183         struct cperf_throughput_ctx *ctx = NULL;
184         unsigned int mbuf_idx = 0;
185         char pool_name[32] = "";
186
187         ctx = rte_malloc(NULL, sizeof(struct cperf_throughput_ctx), 0);
188         if (ctx == NULL)
189                 goto err;
190
191         ctx->dev_id = dev_id;
192         ctx->qp_id = qp_id;
193
194         ctx->populate_ops = op_fns->populate_ops;
195         ctx->options = options;
196         ctx->test_vector = test_vector;
197
198         ctx->sess = op_fns->sess_create(dev_id, options, test_vector);
199         if (ctx->sess == NULL)
200                 goto err;
201
202         snprintf(pool_name, sizeof(pool_name), "cperf_pool_in_cdev_%d",
203                         dev_id);
204
205         ctx->pkt_mbuf_pool_in = rte_pktmbuf_pool_create(pool_name,
206                         options->pool_sz * options->segments_nb, 0, 0,
207                         RTE_PKTMBUF_HEADROOM +
208                         RTE_CACHE_LINE_ROUNDUP(
209                                 (options->buffer_sz / options->segments_nb) +
210                                 (options->buffer_sz % options->segments_nb) +
211                                         options->auth_digest_sz),
212                         rte_socket_id());
213
214         if (ctx->pkt_mbuf_pool_in == NULL)
215                 goto err;
216
217         /* Generate mbufs_in with plaintext populated for test */
218         ctx->mbufs_in = rte_malloc(NULL,
219                         (sizeof(struct rte_mbuf *) * ctx->options->pool_sz), 0);
220
221         for (mbuf_idx = 0; mbuf_idx < options->pool_sz; mbuf_idx++) {
222                 ctx->mbufs_in[mbuf_idx] = cperf_mbuf_create(
223                                 ctx->pkt_mbuf_pool_in, options->segments_nb,
224                                 options, test_vector);
225                 if (ctx->mbufs_in[mbuf_idx] == NULL)
226                         goto err;
227         }
228
229         if (options->out_of_place == 1) {
230
231                 snprintf(pool_name, sizeof(pool_name), "cperf_pool_out_cdev_%d",
232                                 dev_id);
233
234                 ctx->pkt_mbuf_pool_out = rte_pktmbuf_pool_create(
235                                 pool_name, options->pool_sz, 0, 0,
236                                 RTE_PKTMBUF_HEADROOM +
237                                 RTE_CACHE_LINE_ROUNDUP(
238                                         options->buffer_sz +
239                                         options->auth_digest_sz),
240                                 rte_socket_id());
241
242                 if (ctx->pkt_mbuf_pool_out == NULL)
243                         goto err;
244         }
245
246         ctx->mbufs_out = rte_malloc(NULL,
247                         (sizeof(struct rte_mbuf *) *
248                         ctx->options->pool_sz), 0);
249
250         for (mbuf_idx = 0; mbuf_idx < options->pool_sz; mbuf_idx++) {
251                 if (options->out_of_place == 1) {
252                         ctx->mbufs_out[mbuf_idx] = cperf_mbuf_create(
253                                         ctx->pkt_mbuf_pool_out, 1,
254                                         options, test_vector);
255                         if (ctx->mbufs_out[mbuf_idx] == NULL)
256                                 goto err;
257                 } else {
258                         ctx->mbufs_out[mbuf_idx] = NULL;
259                 }
260         }
261
262         snprintf(pool_name, sizeof(pool_name), "cperf_op_pool_cdev_%d",
263                         dev_id);
264
265         ctx->crypto_op_pool = rte_crypto_op_pool_create(pool_name,
266                         RTE_CRYPTO_OP_TYPE_SYMMETRIC, options->pool_sz, 0, 0,
267                         rte_socket_id());
268         if (ctx->crypto_op_pool == NULL)
269                 goto err;
270
271         return ctx;
272 err:
273         cperf_throughput_test_free(ctx, mbuf_idx);
274
275         return NULL;
276 }
277
278 int
279 cperf_throughput_test_runner(void *test_ctx)
280 {
281         struct cperf_throughput_ctx *ctx = test_ctx;
282
283         static int only_once;
284
285         struct rte_crypto_op *ops[ctx->options->burst_sz];
286         struct rte_crypto_op *ops_processed[ctx->options->burst_sz];
287         uint64_t i;
288
289         uint32_t lcore = rte_lcore_id();
290
291 #ifdef CPERF_LINEARIZATION_ENABLE
292         struct rte_cryptodev_info dev_info;
293         int linearize = 0;
294
295         /* Check if source mbufs require coalescing */
296         if (ctx->options->segments_nb > 1) {
297                 rte_cryptodev_info_get(ctx->dev_id, &dev_info);
298                 if ((dev_info.feature_flags &
299                                 RTE_CRYPTODEV_FF_MBUF_SCATTER_GATHER) == 0)
300                         linearize = 1;
301         }
302 #endif /* CPERF_LINEARIZATION_ENABLE */
303
304         ctx->lcore_id = lcore;
305
306         /* Warm up the host CPU before starting the test */
307         for (i = 0; i < ctx->options->total_ops; i++)
308                 rte_cryptodev_enqueue_burst(ctx->dev_id, ctx->qp_id, NULL, 0);
309
310         uint64_t ops_enqd = 0, ops_enqd_total = 0, ops_enqd_failed = 0;
311         uint64_t ops_deqd = 0, ops_deqd_total = 0, ops_deqd_failed = 0;
312         uint64_t m_idx = 0, tsc_start, tsc_end, tsc_duration;
313
314         tsc_start = rte_rdtsc_precise();
315         while (ops_enqd_total < ctx->options->total_ops) {
316
317                 uint16_t ops_unused = 0;
318
319                 uint16_t burst_size = ((ops_enqd_total + ctx->options->burst_sz)
320                                 <= ctx->options->total_ops) ?
321                                                 ctx->options->burst_sz :
322                                                 ctx->options->total_ops -
323                                                 ops_enqd_total;
324
325                 uint16_t ops_needed = burst_size - ops_unused;
326
327                 /* Allocate crypto ops from pool */
328                 if (ops_needed != rte_crypto_op_bulk_alloc(
329                                 ctx->crypto_op_pool,
330                                 RTE_CRYPTO_OP_TYPE_SYMMETRIC,
331                                 ops, ops_needed))
332                         return -1;
333
334                 /* Setup crypto op, attach mbuf etc */
335                 (ctx->populate_ops)(ops, &ctx->mbufs_in[m_idx],
336                                 &ctx->mbufs_out[m_idx],
337                                 ops_needed, ctx->sess, ctx->options,
338                                 ctx->test_vector);
339
340 #ifdef CPERF_LINEARIZATION_ENABLE
341                 if (linearize) {
342                         /* PMD doesn't support scatter-gather and source buffer
343                          * is segmented.
344                          * We need to linearize it before enqueuing.
345                          */
346                         for (i = 0; i < burst_size; i++)
347                                 rte_pktmbuf_linearize(ops[i]->sym->m_src);
348                 }
349 #endif /* CPERF_LINEARIZATION_ENABLE */
350
351                 /* Enqueue burst of ops on crypto device */
352                 ops_enqd = rte_cryptodev_enqueue_burst(ctx->dev_id, ctx->qp_id,
353                                 ops, burst_size);
354                 if (ops_enqd < burst_size)
355                         ops_enqd_failed++;
356
357                 /**
358                  * Calculate number of ops not enqueued (mainly for hw
359                  * accelerators whose ingress queue can fill up).
360                  */
361                 ops_unused = burst_size - ops_enqd;
362                 ops_enqd_total += ops_enqd;
363
364
365                 /* Dequeue processed burst of ops from crypto device */
366                 ops_deqd = rte_cryptodev_dequeue_burst(ctx->dev_id, ctx->qp_id,
367                                 ops_processed, ctx->options->burst_sz);
368
369                 if (likely(ops_deqd))  {
370                         /* free crypto ops so they can be reused. We don't free
371                          * the mbufs here as we don't want to reuse them as
372                          * the crypto operation will change the data and cause
373                          * failures.
374                          */
375                         for (i = 0; i < ops_deqd; i++)
376                                 rte_crypto_op_free(ops_processed[i]);
377
378                         ops_deqd_total += ops_deqd;
379                 } else {
380                         /**
381                          * Count dequeue polls which didn't return any
382                          * processed operations. This statistic is mainly
383                          * relevant to hw accelerators.
384                          */
385                         ops_deqd_failed++;
386                 }
387
388                 m_idx += ops_needed;
389                 m_idx = m_idx + ctx->options->burst_sz > ctx->options->pool_sz ?
390                                 0 : m_idx;
391         }
392
393         /* Dequeue any operations still in the crypto device */
394
395         while (ops_deqd_total < ctx->options->total_ops) {
396                 /* Sending 0 length burst to flush sw crypto device */
397                 rte_cryptodev_enqueue_burst(ctx->dev_id, ctx->qp_id, NULL, 0);
398
399                 /* dequeue burst */
400                 ops_deqd = rte_cryptodev_dequeue_burst(ctx->dev_id, ctx->qp_id,
401                                 ops_processed, ctx->options->burst_sz);
402                 if (ops_deqd == 0)
403                         ops_deqd_failed++;
404                 else {
405                         for (i = 0; i < ops_deqd; i++)
406                                 rte_crypto_op_free(ops_processed[i]);
407
408                         ops_deqd_total += ops_deqd;
409                 }
410         }
411
412         tsc_end = rte_rdtsc_precise();
413         tsc_duration = (tsc_end - tsc_start);
414
415         /* Calculate average operations processed per second */
416         double ops_per_second = ((double)ctx->options->total_ops /
417                         tsc_duration) * rte_get_tsc_hz();
418
419         /* Calculate average throughput (Gbps) in bits per second */
420         double throughput_gbps = ((ops_per_second *
421                         ctx->options->buffer_sz * 8) / 1000000000);
422
423         /* Calculate average cycles per packet */
424         double cycles_per_packet = ((double)tsc_duration /
425                         ctx->options->total_ops);
426
427         if (!ctx->options->csv) {
428                 if (!only_once)
429                         printf("%12s%12s%12s%12s%12s%12s%12s%12s%12s%12s\n\n",
430                                 "lcore id", "Buf Size", "Burst Size",
431                                 "Enqueued", "Dequeued", "Failed Enq",
432                                 "Failed Deq", "MOps", "Gbps",
433                                 "Cycles/Buf");
434                 only_once = 1;
435
436                 printf("%12u%12u%12u%12"PRIu64"%12"PRIu64"%12"PRIu64
437                                 "%12"PRIu64"%12.4f%12.4f%12.2f\n",
438                                 ctx->lcore_id,
439                                 ctx->options->buffer_sz,
440                                 ctx->options->burst_sz,
441                                 ops_enqd_total,
442                                 ops_deqd_total,
443                                 ops_enqd_failed,
444                                 ops_deqd_failed,
445                                 ops_per_second/1000000,
446                                 throughput_gbps,
447                                 cycles_per_packet);
448         } else {
449                 if (!only_once)
450                         printf("# lcore id, Buffer Size(B),"
451                                 "Burst Size,Enqueued,Dequeued,Failed Enq,"
452                                 "Failed Deq,Ops(Millions),Throughput(Gbps),"
453                                 "Cycles/Buf\n\n");
454                 only_once = 1;
455
456                 printf("%10u;%10u;%u;%"PRIu64";%"PRIu64";%"PRIu64";%"PRIu64";"
457                                 "%.f3;%.f3;%.f3\n",
458                                 ctx->lcore_id,
459                                 ctx->options->buffer_sz,
460                                 ctx->options->burst_sz,
461                                 ops_enqd_total,
462                                 ops_deqd_total,
463                                 ops_enqd_failed,
464                                 ops_deqd_failed,
465                                 ops_per_second/1000000,
466                                 throughput_gbps,
467                                 cycles_per_packet);
468         }
469
470         return 0;
471 }
472
473
474 void
475 cperf_throughput_test_destructor(void *arg)
476 {
477         struct cperf_throughput_ctx *ctx = arg;
478
479         if (ctx == NULL)
480                 return;
481
482         cperf_throughput_test_free(ctx, ctx->options->pool_sz);
483 }