LCOV - code coverage report
Current view: top level - app/test-crypto-perf - cperf_test_verify.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 0 140 0.0 %
Date: 2024-02-14 00:53:57 Functions: 0 5 0.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 0 -

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

Generated by: LCOV version 1.14