LCOV - code coverage report
Current view: top level - lib/vhost - vhost_crypto.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 1 735 0.1 %
Date: 2025-02-01 18:54:23 Functions: 1 16 6.2 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 1 1236 0.1 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  * Copyright(c) 2017-2018 Intel Corporation
       3                 :            :  */
       4                 :            : #include <rte_malloc.h>
       5                 :            : #include <rte_hash.h>
       6                 :            : #include <rte_jhash.h>
       7                 :            : #include <rte_log.h>
       8                 :            : #include <rte_mbuf.h>
       9                 :            : #include <rte_cryptodev.h>
      10                 :            : 
      11                 :            : #include "rte_vhost_crypto.h"
      12                 :            : #include "vhost.h"
      13                 :            : #include "vhost_user.h"
      14                 :            : #include "virtio_crypto.h"
      15                 :            : 
      16                 :            : #define INHDR_LEN               (sizeof(struct virtio_crypto_inhdr))
      17                 :            : #define IV_OFFSET               (sizeof(struct rte_crypto_op) + \
      18                 :            :                                 sizeof(struct rte_crypto_sym_op))
      19                 :            : 
      20         [ -  + ]:        252 : RTE_LOG_REGISTER_SUFFIX(vhost_crypto_logtype, crypto, INFO);
      21                 :            : #define RTE_LOGTYPE_VHOST_CRYPTO        vhost_crypto_logtype
      22                 :            : 
      23                 :            : #define VC_LOG_ERR(...) \
      24                 :            :         RTE_LOG_LINE_PREFIX(ERR, VHOST_CRYPTO, "%s() line %u: ", \
      25                 :            :                 __func__ RTE_LOG_COMMA __LINE__, __VA_ARGS__)
      26                 :            : 
      27                 :            : #define VC_LOG_INFO(...) \
      28                 :            :         RTE_LOG_LINE_PREFIX(INFO, VHOST_CRYPTO, "%s() line %u: ", \
      29                 :            :                 __func__ RTE_LOG_COMMA __LINE__, __VA_ARGS__)
      30                 :            : 
      31                 :            : #ifdef RTE_LIBRTE_VHOST_DEBUG
      32                 :            : #define VC_LOG_DBG(...) \
      33                 :            :         RTE_LOG_LINE_PREFIX(DEBUG, VHOST_CRYPTO, "%s() line %u: ", \
      34                 :            :                 __func__ RTE_LOG_COMMA __LINE__, __VA_ARGS__)
      35                 :            : #else
      36                 :            : #define VC_LOG_DBG(...)
      37                 :            : #endif
      38                 :            : 
      39                 :            : #define VIRTIO_CRYPTO_FEATURES ((1ULL << VIRTIO_F_NOTIFY_ON_EMPTY) |      \
      40                 :            :                 (1ULL << VIRTIO_RING_F_INDIRECT_DESC) |                   \
      41                 :            :                 (1ULL << VIRTIO_RING_F_EVENT_IDX) |                       \
      42                 :            :                 (1ULL << VIRTIO_NET_F_CTRL_VQ) |                  \
      43                 :            :                 (1ULL << VIRTIO_F_VERSION_1) |                            \
      44                 :            :                 (1ULL << VHOST_USER_F_PROTOCOL_FEATURES))
      45                 :            : 
      46                 :            : #define IOVA_TO_VVA(t, r, a, l, p)                                      \
      47                 :            :         ((t)(uintptr_t)vhost_iova_to_vva(r->dev, r->vq, a, l, p))
      48                 :            : 
      49                 :            : /*
      50                 :            :  * vhost_crypto_desc is used to copy original vring_desc to the local buffer
      51                 :            :  * before processing (except the next index). The copy result will be an
      52                 :            :  * array of vhost_crypto_desc elements that follows the sequence of original
      53                 :            :  * vring_desc.next is arranged.
      54                 :            :  */
      55                 :            : #define vhost_crypto_desc vring_desc
      56                 :            : 
      57                 :            : static int
      58                 :          0 : cipher_algo_transform(uint32_t virtio_cipher_algo,
      59                 :            :                 enum rte_crypto_cipher_algorithm *algo)
      60                 :            : {
      61   [ #  #  #  #  :          0 :         switch (virtio_cipher_algo) {
          #  #  #  #  #  
             #  #  #  # ]
      62                 :          0 :         case VIRTIO_CRYPTO_CIPHER_AES_CBC:
      63                 :          0 :                 *algo = RTE_CRYPTO_CIPHER_AES_CBC;
      64                 :          0 :                 break;
      65                 :          0 :         case VIRTIO_CRYPTO_CIPHER_AES_CTR:
      66                 :          0 :                 *algo = RTE_CRYPTO_CIPHER_AES_CTR;
      67                 :          0 :                 break;
      68                 :          0 :         case VIRTIO_CRYPTO_CIPHER_DES_ECB:
      69                 :          0 :                 *algo = -VIRTIO_CRYPTO_NOTSUPP;
      70                 :          0 :                 break;
      71                 :          0 :         case VIRTIO_CRYPTO_CIPHER_DES_CBC:
      72                 :          0 :                 *algo = RTE_CRYPTO_CIPHER_DES_CBC;
      73                 :          0 :                 break;
      74                 :          0 :         case VIRTIO_CRYPTO_CIPHER_3DES_ECB:
      75                 :          0 :                 *algo = RTE_CRYPTO_CIPHER_3DES_ECB;
      76                 :          0 :                 break;
      77                 :          0 :         case VIRTIO_CRYPTO_CIPHER_3DES_CBC:
      78                 :          0 :                 *algo = RTE_CRYPTO_CIPHER_3DES_CBC;
      79                 :          0 :                 break;
      80                 :          0 :         case VIRTIO_CRYPTO_CIPHER_3DES_CTR:
      81                 :          0 :                 *algo = RTE_CRYPTO_CIPHER_3DES_CTR;
      82                 :          0 :                 break;
      83                 :          0 :         case VIRTIO_CRYPTO_CIPHER_KASUMI_F8:
      84                 :          0 :                 *algo = RTE_CRYPTO_CIPHER_KASUMI_F8;
      85                 :          0 :                 break;
      86                 :          0 :         case VIRTIO_CRYPTO_CIPHER_SNOW3G_UEA2:
      87                 :          0 :                 *algo = RTE_CRYPTO_CIPHER_SNOW3G_UEA2;
      88                 :          0 :                 break;
      89                 :          0 :         case VIRTIO_CRYPTO_CIPHER_AES_F8:
      90                 :          0 :                 *algo = RTE_CRYPTO_CIPHER_AES_F8;
      91                 :          0 :                 break;
      92                 :          0 :         case VIRTIO_CRYPTO_CIPHER_AES_XTS:
      93                 :          0 :                 *algo = RTE_CRYPTO_CIPHER_AES_XTS;
      94                 :          0 :                 break;
      95                 :          0 :         case VIRTIO_CRYPTO_CIPHER_ZUC_EEA3:
      96                 :          0 :                 *algo = RTE_CRYPTO_CIPHER_ZUC_EEA3;
      97                 :          0 :                 break;
      98                 :            :         default:
      99                 :            :                 return -VIRTIO_CRYPTO_BADMSG;
     100                 :            :                 break;
     101                 :            :         }
     102                 :            : 
     103                 :            :         return 0;
     104                 :            : }
     105                 :            : 
     106                 :            : static int
     107                 :          0 : auth_algo_transform(uint32_t virtio_auth_algo,
     108                 :            :                 enum rte_crypto_auth_algorithm *algo)
     109                 :            : {
     110   [ #  #  #  #  :          0 :         switch (virtio_auth_algo) {
          #  #  #  #  #  
          #  #  #  #  #  
                      # ]
     111                 :          0 :         case VIRTIO_CRYPTO_NO_MAC:
     112                 :          0 :                 *algo = RTE_CRYPTO_AUTH_NULL;
     113                 :          0 :                 break;
     114                 :          0 :         case VIRTIO_CRYPTO_MAC_HMAC_MD5:
     115                 :          0 :                 *algo = RTE_CRYPTO_AUTH_MD5_HMAC;
     116                 :          0 :                 break;
     117                 :          0 :         case VIRTIO_CRYPTO_MAC_HMAC_SHA1:
     118                 :          0 :                 *algo = RTE_CRYPTO_AUTH_SHA1_HMAC;
     119                 :          0 :                 break;
     120                 :          0 :         case VIRTIO_CRYPTO_MAC_HMAC_SHA_224:
     121                 :          0 :                 *algo = RTE_CRYPTO_AUTH_SHA224_HMAC;
     122                 :          0 :                 break;
     123                 :          0 :         case VIRTIO_CRYPTO_MAC_HMAC_SHA_256:
     124                 :          0 :                 *algo = RTE_CRYPTO_AUTH_SHA256_HMAC;
     125                 :          0 :                 break;
     126                 :          0 :         case VIRTIO_CRYPTO_MAC_HMAC_SHA_384:
     127                 :          0 :                 *algo = RTE_CRYPTO_AUTH_SHA384_HMAC;
     128                 :          0 :                 break;
     129                 :          0 :         case VIRTIO_CRYPTO_MAC_HMAC_SHA_512:
     130                 :          0 :                 *algo = RTE_CRYPTO_AUTH_SHA512_HMAC;
     131                 :          0 :                 break;
     132                 :          0 :         case VIRTIO_CRYPTO_MAC_CMAC_AES:
     133                 :          0 :                 *algo = RTE_CRYPTO_AUTH_AES_CMAC;
     134                 :          0 :                 break;
     135                 :          0 :         case VIRTIO_CRYPTO_MAC_KASUMI_F9:
     136                 :          0 :                 *algo = RTE_CRYPTO_AUTH_KASUMI_F9;
     137                 :          0 :                 break;
     138                 :          0 :         case VIRTIO_CRYPTO_MAC_SNOW3G_UIA2:
     139                 :          0 :                 *algo = RTE_CRYPTO_AUTH_SNOW3G_UIA2;
     140                 :          0 :                 break;
     141                 :          0 :         case VIRTIO_CRYPTO_MAC_GMAC_AES:
     142                 :          0 :                 *algo = RTE_CRYPTO_AUTH_AES_GMAC;
     143                 :          0 :                 break;
     144                 :          0 :         case VIRTIO_CRYPTO_MAC_CBCMAC_AES:
     145                 :          0 :                 *algo = RTE_CRYPTO_AUTH_AES_CBC_MAC;
     146                 :          0 :                 break;
     147                 :          0 :         case VIRTIO_CRYPTO_MAC_XCBC_AES:
     148                 :          0 :                 *algo = RTE_CRYPTO_AUTH_AES_XCBC_MAC;
     149                 :          0 :                 break;
     150                 :            :         case VIRTIO_CRYPTO_MAC_CMAC_3DES:
     151                 :            :         case VIRTIO_CRYPTO_MAC_GMAC_TWOFISH:
     152                 :            :         case VIRTIO_CRYPTO_MAC_CBCMAC_KASUMI_F9:
     153                 :            :                 return -VIRTIO_CRYPTO_NOTSUPP;
     154                 :          0 :         default:
     155                 :          0 :                 return -VIRTIO_CRYPTO_BADMSG;
     156                 :            :         }
     157                 :            : 
     158                 :            :         return 0;
     159                 :            : }
     160                 :            : 
     161                 :            : static int get_iv_len(enum rte_crypto_cipher_algorithm algo)
     162                 :            : {
     163                 :            :         int len;
     164                 :            : 
     165                 :          0 :         switch (algo) {
     166                 :            :         case RTE_CRYPTO_CIPHER_3DES_CBC:
     167                 :            :                 len = 8;
     168                 :            :                 break;
     169                 :            :         case RTE_CRYPTO_CIPHER_3DES_CTR:
     170                 :            :                 len = 8;
     171                 :            :                 break;
     172                 :            :         case RTE_CRYPTO_CIPHER_3DES_ECB:
     173                 :            :                 len = 8;
     174                 :            :                 break;
     175                 :          0 :         case RTE_CRYPTO_CIPHER_AES_CBC:
     176                 :            :                 len = 16;
     177                 :          0 :                 break;
     178                 :            : 
     179                 :            :         /* TODO: add common algos */
     180                 :            : 
     181                 :          0 :         default:
     182                 :            :                 len = -1;
     183                 :          0 :                 break;
     184                 :            :         }
     185                 :            : 
     186                 :            :         return len;
     187                 :            : }
     188                 :            : 
     189                 :            : /**
     190                 :            :  * vhost_crypto struct is used to maintain a number of virtio_cryptos and
     191                 :            :  * one DPDK crypto device that deals with all crypto workloads. It is declared
     192                 :            :  * here and defined in vhost_crypto.c
     193                 :            :  */
     194                 :            : struct __rte_cache_aligned vhost_crypto {
     195                 :            :         /** Used to lookup DPDK Cryptodev Session based on VIRTIO crypto
     196                 :            :          *  session ID.
     197                 :            :          */
     198                 :            :         struct rte_hash *session_map;
     199                 :            :         struct rte_mempool *mbuf_pool;
     200                 :            :         struct rte_mempool *sess_pool;
     201                 :            :         struct rte_mempool *wb_pool;
     202                 :            : 
     203                 :            :         /** DPDK cryptodev ID */
     204                 :            :         uint8_t cid;
     205                 :            :         uint16_t nb_qps;
     206                 :            : 
     207                 :            :         uint64_t last_session_id;
     208                 :            : 
     209                 :            :         uint64_t cache_session_id;
     210                 :            :         struct rte_cryptodev_sym_session *cache_session;
     211                 :            :         /** socket id for the device */
     212                 :            :         int socket_id;
     213                 :            : 
     214                 :            :         struct virtio_net *dev;
     215                 :            : 
     216                 :            :         uint8_t option;
     217                 :            : };
     218                 :            : 
     219                 :            : struct vhost_crypto_writeback_data {
     220                 :            :         uint8_t *src;
     221                 :            :         uint8_t *dst;
     222                 :            :         uint64_t len;
     223                 :            :         struct vhost_crypto_writeback_data *next;
     224                 :            : };
     225                 :            : 
     226                 :            : struct vhost_crypto_data_req {
     227                 :            :         struct vring_desc *head;
     228                 :            :         struct virtio_net *dev;
     229                 :            :         struct virtio_crypto_inhdr *inhdr;
     230                 :            :         struct vhost_virtqueue *vq;
     231                 :            :         struct vhost_crypto_writeback_data *wb;
     232                 :            :         struct rte_mempool *wb_pool;
     233                 :            :         uint16_t desc_idx;
     234                 :            :         uint16_t len;
     235                 :            :         uint16_t zero_copy;
     236                 :            : };
     237                 :            : 
     238                 :            : static int
     239                 :          0 : transform_cipher_param(struct rte_crypto_sym_xform *xform,
     240                 :            :                 VhostUserCryptoSessionParam *param)
     241                 :            : {
     242                 :            :         int ret;
     243                 :            : 
     244                 :          0 :         ret = cipher_algo_transform(param->cipher_algo, &xform->cipher.algo);
     245         [ #  # ]:          0 :         if (unlikely(ret < 0))
     246                 :            :                 return ret;
     247                 :            : 
     248         [ #  # ]:          0 :         if (param->cipher_key_len > VHOST_USER_CRYPTO_MAX_CIPHER_KEY_LENGTH) {
     249                 :            :                 VC_LOG_DBG("Invalid cipher key length");
     250                 :            :                 return -VIRTIO_CRYPTO_BADMSG;
     251                 :            :         }
     252                 :            : 
     253                 :          0 :         xform->type = RTE_CRYPTO_SYM_XFORM_CIPHER;
     254                 :          0 :         xform->cipher.key.length = param->cipher_key_len;
     255         [ #  # ]:          0 :         if (xform->cipher.key.length > 0)
     256                 :          0 :                 xform->cipher.key.data = param->cipher_key_buf;
     257         [ #  # ]:          0 :         if (param->dir == VIRTIO_CRYPTO_OP_ENCRYPT)
     258                 :          0 :                 xform->cipher.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
     259         [ #  # ]:          0 :         else if (param->dir == VIRTIO_CRYPTO_OP_DECRYPT)
     260                 :          0 :                 xform->cipher.op = RTE_CRYPTO_CIPHER_OP_DECRYPT;
     261                 :            :         else {
     262                 :            :                 VC_LOG_DBG("Bad operation type");
     263                 :            :                 return -VIRTIO_CRYPTO_BADMSG;
     264                 :            :         }
     265                 :            : 
     266      [ #  #  # ]:          0 :         ret = get_iv_len(xform->cipher.algo);
     267         [ #  # ]:          0 :         if (unlikely(ret < 0))
     268                 :            :                 return ret;
     269                 :          0 :         xform->cipher.iv.length = (uint16_t)ret;
     270                 :          0 :         xform->cipher.iv.offset = IV_OFFSET;
     271                 :          0 :         return 0;
     272                 :            : }
     273                 :            : 
     274                 :            : static int
     275                 :          0 : transform_chain_param(struct rte_crypto_sym_xform *xforms,
     276                 :            :                 VhostUserCryptoSessionParam *param)
     277                 :            : {
     278                 :            :         struct rte_crypto_sym_xform *xform_cipher, *xform_auth;
     279                 :            :         int ret;
     280                 :            : 
     281      [ #  #  # ]:          0 :         switch (param->chaining_dir) {
     282                 :          0 :         case VIRTIO_CRYPTO_SYM_ALG_CHAIN_ORDER_HASH_THEN_CIPHER:
     283                 :            :                 xform_auth = xforms;
     284                 :          0 :                 xform_cipher = xforms->next;
     285                 :          0 :                 xform_cipher->cipher.op = RTE_CRYPTO_CIPHER_OP_DECRYPT;
     286                 :          0 :                 xform_auth->auth.op = RTE_CRYPTO_AUTH_OP_VERIFY;
     287                 :          0 :                 break;
     288                 :          0 :         case VIRTIO_CRYPTO_SYM_ALG_CHAIN_ORDER_CIPHER_THEN_HASH:
     289                 :            :                 xform_cipher = xforms;
     290                 :          0 :                 xform_auth = xforms->next;
     291                 :          0 :                 xform_cipher->cipher.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
     292                 :          0 :                 xform_auth->auth.op = RTE_CRYPTO_AUTH_OP_GENERATE;
     293                 :          0 :                 break;
     294                 :            :         default:
     295                 :            :                 return -VIRTIO_CRYPTO_BADMSG;
     296                 :            :         }
     297                 :            : 
     298                 :            :         /* cipher */
     299                 :          0 :         ret = cipher_algo_transform(param->cipher_algo,
     300                 :            :                         &xform_cipher->cipher.algo);
     301         [ #  # ]:          0 :         if (unlikely(ret < 0))
     302                 :            :                 return ret;
     303                 :            : 
     304         [ #  # ]:          0 :         if (param->cipher_key_len > VHOST_USER_CRYPTO_MAX_CIPHER_KEY_LENGTH) {
     305                 :            :                 VC_LOG_DBG("Invalid cipher key length");
     306                 :            :                 return -VIRTIO_CRYPTO_BADMSG;
     307                 :            :         }
     308                 :            : 
     309                 :          0 :         xform_cipher->type = RTE_CRYPTO_SYM_XFORM_CIPHER;
     310                 :          0 :         xform_cipher->cipher.key.length = param->cipher_key_len;
     311                 :          0 :         xform_cipher->cipher.key.data = param->cipher_key_buf;
     312      [ #  #  # ]:          0 :         ret = get_iv_len(xform_cipher->cipher.algo);
     313         [ #  # ]:          0 :         if (unlikely(ret < 0))
     314                 :            :                 return ret;
     315                 :          0 :         xform_cipher->cipher.iv.length = (uint16_t)ret;
     316                 :          0 :         xform_cipher->cipher.iv.offset = IV_OFFSET;
     317                 :            : 
     318                 :            :         /* auth */
     319                 :          0 :         xform_auth->type = RTE_CRYPTO_SYM_XFORM_AUTH;
     320                 :          0 :         ret = auth_algo_transform(param->hash_algo, &xform_auth->auth.algo);
     321         [ #  # ]:          0 :         if (unlikely(ret < 0))
     322                 :            :                 return ret;
     323                 :            : 
     324         [ #  # ]:          0 :         if (param->auth_key_len > VHOST_USER_CRYPTO_MAX_HMAC_KEY_LENGTH) {
     325                 :            :                 VC_LOG_DBG("Invalid auth key length");
     326                 :            :                 return -VIRTIO_CRYPTO_BADMSG;
     327                 :            :         }
     328                 :            : 
     329                 :          0 :         xform_auth->auth.digest_length = param->digest_len;
     330                 :          0 :         xform_auth->auth.key.length = param->auth_key_len;
     331                 :          0 :         xform_auth->auth.key.data = param->auth_key_buf;
     332                 :            : 
     333                 :          0 :         return 0;
     334                 :            : }
     335                 :            : 
     336                 :            : static void
     337                 :          0 : vhost_crypto_create_sess(struct vhost_crypto *vcrypto,
     338                 :            :                 VhostUserCryptoSessionParam *sess_param)
     339                 :            : {
     340                 :          0 :         struct rte_crypto_sym_xform xform1 = {0}, xform2 = {0};
     341                 :            :         struct rte_cryptodev_sym_session *session;
     342                 :            :         int ret;
     343                 :            : 
     344      [ #  #  # ]:          0 :         switch (sess_param->op_type) {
     345                 :          0 :         case VIRTIO_CRYPTO_SYM_OP_NONE:
     346                 :            :         case VIRTIO_CRYPTO_SYM_OP_CIPHER:
     347                 :          0 :                 ret = transform_cipher_param(&xform1, sess_param);
     348         [ #  # ]:          0 :                 if (unlikely(ret)) {
     349                 :          0 :                         VC_LOG_ERR("Error transform session msg (%i)", ret);
     350                 :          0 :                         sess_param->session_id = ret;
     351                 :          0 :                         return;
     352                 :            :                 }
     353                 :            :                 break;
     354                 :          0 :         case VIRTIO_CRYPTO_SYM_OP_ALGORITHM_CHAINING:
     355         [ #  # ]:          0 :                 if (unlikely(sess_param->hash_mode !=
     356                 :            :                                 VIRTIO_CRYPTO_SYM_HASH_MODE_AUTH)) {
     357                 :          0 :                         sess_param->session_id = -VIRTIO_CRYPTO_NOTSUPP;
     358                 :          0 :                         VC_LOG_ERR("Error transform session message (%i)",
     359                 :            :                                         -VIRTIO_CRYPTO_NOTSUPP);
     360                 :          0 :                         return;
     361                 :            :                 }
     362                 :            : 
     363                 :          0 :                 xform1.next = &xform2;
     364                 :            : 
     365                 :          0 :                 ret = transform_chain_param(&xform1, sess_param);
     366         [ #  # ]:          0 :                 if (unlikely(ret)) {
     367                 :          0 :                         VC_LOG_ERR("Error transform session message (%i)", ret);
     368                 :          0 :                         sess_param->session_id = ret;
     369                 :          0 :                         return;
     370                 :            :                 }
     371                 :            : 
     372                 :            :                 break;
     373                 :          0 :         default:
     374                 :          0 :                 VC_LOG_ERR("Algorithm not yet supported");
     375                 :          0 :                 sess_param->session_id = -VIRTIO_CRYPTO_NOTSUPP;
     376                 :          0 :                 return;
     377                 :            :         }
     378                 :            : 
     379                 :          0 :         session = rte_cryptodev_sym_session_create(vcrypto->cid, &xform1,
     380                 :            :                         vcrypto->sess_pool);
     381         [ #  # ]:          0 :         if (!session) {
     382                 :          0 :                 VC_LOG_ERR("Failed to create session");
     383                 :          0 :                 sess_param->session_id = -VIRTIO_CRYPTO_ERR;
     384                 :          0 :                 return;
     385                 :            :         }
     386                 :            : 
     387                 :            :         /* insert hash to map */
     388         [ #  # ]:          0 :         if (rte_hash_add_key_data(vcrypto->session_map,
     389                 :          0 :                         &vcrypto->last_session_id, session) < 0) {
     390                 :          0 :                 VC_LOG_ERR("Failed to insert session to hash table");
     391                 :            : 
     392         [ #  # ]:          0 :                 if (rte_cryptodev_sym_session_free(vcrypto->cid, session) < 0)
     393                 :          0 :                         VC_LOG_ERR("Failed to free session");
     394                 :          0 :                 sess_param->session_id = -VIRTIO_CRYPTO_ERR;
     395                 :          0 :                 return;
     396                 :            :         }
     397                 :            : 
     398                 :          0 :         VC_LOG_INFO("Session %"PRIu64" created for vdev %i.",
     399                 :            :                         vcrypto->last_session_id, vcrypto->dev->vid);
     400                 :            : 
     401                 :          0 :         sess_param->session_id = vcrypto->last_session_id;
     402                 :          0 :         vcrypto->last_session_id++;
     403                 :            : }
     404                 :            : 
     405                 :            : static int
     406                 :          0 : vhost_crypto_close_sess(struct vhost_crypto *vcrypto, uint64_t session_id)
     407                 :            : {
     408                 :            :         struct rte_cryptodev_sym_session *session;
     409                 :          0 :         uint64_t sess_id = session_id;
     410                 :            :         int ret;
     411                 :            : 
     412                 :          0 :         ret = rte_hash_lookup_data(vcrypto->session_map, &sess_id,
     413                 :            :                         (void **)&session);
     414                 :            : 
     415         [ #  # ]:          0 :         if (unlikely(ret < 0)) {
     416                 :          0 :                 VC_LOG_ERR("Failed to delete session %"PRIu64".", session_id);
     417                 :          0 :                 return -VIRTIO_CRYPTO_INVSESS;
     418                 :            :         }
     419                 :            : 
     420         [ #  # ]:          0 :         if (rte_cryptodev_sym_session_free(vcrypto->cid, session) < 0) {
     421                 :            :                 VC_LOG_DBG("Failed to free session");
     422                 :            :                 return -VIRTIO_CRYPTO_ERR;
     423                 :            :         }
     424                 :            : 
     425         [ #  # ]:          0 :         if (rte_hash_del_key(vcrypto->session_map, &sess_id) < 0) {
     426                 :            :                 VC_LOG_DBG("Failed to delete session from hash table.");
     427                 :            :                 return -VIRTIO_CRYPTO_ERR;
     428                 :            :         }
     429                 :            : 
     430                 :          0 :         VC_LOG_INFO("Session %"PRIu64" deleted for vdev %i.", sess_id,
     431                 :            :                         vcrypto->dev->vid);
     432                 :            : 
     433                 :          0 :         return 0;
     434                 :            : }
     435                 :            : 
     436                 :            : static enum rte_vhost_msg_result
     437         [ #  # ]:          0 : vhost_crypto_msg_post_handler(int vid, void *msg)
     438                 :            : {
     439                 :            :         struct virtio_net *dev = get_device(vid);
     440                 :            :         struct vhost_crypto *vcrypto;
     441                 :            :         struct vhu_msg_context *ctx = msg;
     442                 :            :         enum rte_vhost_msg_result ret = RTE_VHOST_MSG_RESULT_OK;
     443                 :            : 
     444         [ #  # ]:          0 :         if (dev == NULL) {
     445                 :          0 :                 VC_LOG_ERR("Invalid vid %i", vid);
     446                 :          0 :                 return RTE_VHOST_MSG_RESULT_ERR;
     447                 :            :         }
     448                 :            : 
     449                 :          0 :         vcrypto = dev->extern_data;
     450         [ #  # ]:          0 :         if (vcrypto == NULL) {
     451                 :          0 :                 VC_LOG_ERR("Cannot find required data, is it initialized?");
     452                 :          0 :                 return RTE_VHOST_MSG_RESULT_ERR;
     453                 :            :         }
     454                 :            : 
     455      [ #  #  # ]:          0 :         switch (ctx->msg.request.frontend) {
     456                 :          0 :         case VHOST_USER_CRYPTO_CREATE_SESS:
     457                 :          0 :                 vhost_crypto_create_sess(vcrypto,
     458                 :            :                                 &ctx->msg.payload.crypto_session);
     459                 :          0 :                 ctx->fd_num = 0;
     460                 :            :                 ret = RTE_VHOST_MSG_RESULT_REPLY;
     461                 :          0 :                 break;
     462                 :          0 :         case VHOST_USER_CRYPTO_CLOSE_SESS:
     463         [ #  # ]:          0 :                 if (vhost_crypto_close_sess(vcrypto, ctx->msg.payload.u64))
     464                 :            :                         ret = RTE_VHOST_MSG_RESULT_ERR;
     465                 :            :                 break;
     466                 :            :         default:
     467                 :            :                 ret = RTE_VHOST_MSG_RESULT_NOT_HANDLED;
     468                 :            :                 break;
     469                 :            :         }
     470                 :            : 
     471                 :            :         return ret;
     472                 :            : }
     473                 :            : 
     474                 :            : static __rte_always_inline struct vhost_crypto_desc *
     475                 :            : find_write_desc(struct vhost_crypto_desc *head, struct vhost_crypto_desc *desc,
     476                 :            :                 uint32_t max_n_descs)
     477                 :            : {
     478   [ #  #  #  #  :          0 :         if (desc < head)
             #  #  #  # ]
     479                 :            :                 return NULL;
     480                 :            : 
     481   [ #  #  #  #  :          0 :         while (desc - head < (int)max_n_descs) {
             #  #  #  # ]
     482   [ #  #  #  #  :          0 :                 if (desc->flags & VRING_DESC_F_WRITE)
             #  #  #  # ]
     483                 :            :                         return desc;
     484                 :          0 :                 desc++;
     485                 :            :         }
     486                 :            : 
     487                 :            :         return NULL;
     488                 :            : }
     489                 :            : 
     490                 :            : static __rte_always_inline struct virtio_crypto_inhdr *
     491                 :            : reach_inhdr(struct vhost_crypto_data_req *vc_req,
     492                 :            :                 struct vhost_crypto_desc *head,
     493                 :            :                 uint32_t max_n_descs)
     494                 :            :         __rte_shared_locks_required(&vc_req->vq->iotlb_lock)
     495                 :            : {
     496                 :            :         struct virtio_crypto_inhdr *inhdr;
     497                 :          0 :         struct vhost_crypto_desc *last = head + (max_n_descs - 1);
     498                 :          0 :         uint64_t dlen = last->len;
     499                 :            : 
     500                 :          0 :         if (unlikely(dlen != sizeof(*inhdr)))
     501                 :            :                 return NULL;
     502                 :            : 
     503   [ #  #  #  # ]:          0 :         inhdr = IOVA_TO_VVA(struct virtio_crypto_inhdr *, vc_req, last->addr,
     504                 :            :                         &dlen, VHOST_ACCESS_WO);
     505   [ #  #  #  #  :          0 :         if (unlikely(!inhdr || dlen != last->len))
             #  #  #  # ]
     506                 :          0 :                 return NULL;
     507                 :            : 
     508                 :            :         return inhdr;
     509                 :            : }
     510                 :            : 
     511                 :            : static __rte_always_inline int
     512                 :            : move_desc(struct vhost_crypto_desc *head,
     513                 :            :                 struct vhost_crypto_desc **cur_desc,
     514                 :            :                 uint32_t size, uint32_t max_n_descs)
     515                 :            : {
     516                 :            :         struct vhost_crypto_desc *desc = *cur_desc;
     517                 :          0 :         int left = size - desc->len;
     518                 :            : 
     519                 :          0 :         while (desc->flags & VRING_DESC_F_NEXT && left > 0 &&
     520   [ #  #  #  #  :          0 :                         desc >= head &&
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                      # ]
     521   [ #  #  #  #  :          0 :                         desc - head < (int)max_n_descs) {
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                      # ]
     522                 :          0 :                 desc++;
     523                 :          0 :                 left -= desc->len;
     524                 :            :         }
     525                 :            : 
     526   [ #  #  #  #  :          0 :         if (unlikely(left > 0))
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                      # ]
     527                 :            :                 return -1;
     528                 :            : 
     529   [ #  #  #  #  :          0 :         if (unlikely(head - desc == (int)max_n_descs))
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                      # ]
     530                 :            :                 *cur_desc = NULL;
     531                 :            :         else
     532                 :          0 :                 *cur_desc = desc + 1;
     533                 :            : 
     534                 :            :         return 0;
     535                 :            : }
     536                 :            : 
     537                 :            : static __rte_always_inline void *
     538                 :            : get_data_ptr(struct vhost_crypto_data_req *vc_req,
     539                 :            :                 struct vhost_crypto_desc *cur_desc,
     540                 :            :                 uint8_t perm)
     541                 :            :         __rte_shared_locks_required(&vc_req->vq->iotlb_lock)
     542                 :            : {
     543                 :            :         void *data;
     544                 :          0 :         uint64_t dlen = cur_desc->len;
     545                 :            : 
     546   [ #  #  #  #  :          0 :         data = IOVA_TO_VVA(void *, vc_req, cur_desc->addr, &dlen, perm);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                      # ]
     547   [ #  #  #  #  :          0 :         if (unlikely(!data || dlen != cur_desc->len)) {
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
     548                 :          0 :                 VC_LOG_ERR("Failed to map object");
     549                 :          0 :                 return NULL;
     550                 :            :         }
     551                 :            : 
     552                 :            :         return data;
     553                 :            : }
     554                 :            : 
     555                 :            : static __rte_always_inline uint32_t
     556                 :            : copy_data_from_desc(void *dst, struct vhost_crypto_data_req *vc_req,
     557                 :            :         struct vhost_crypto_desc *desc, uint32_t size)
     558                 :            :         __rte_shared_locks_required(&vc_req->vq->iotlb_lock)
     559                 :            : {
     560                 :            :         uint64_t remain;
     561                 :            :         uint64_t addr;
     562                 :            : 
     563                 :          0 :         remain = RTE_MIN(desc->len, size);
     564                 :          0 :         addr = desc->addr;
     565                 :            :         do {
     566                 :            :                 uint64_t len;
     567                 :            :                 void *src;
     568                 :            : 
     569                 :          0 :                 len = remain;
     570   [ #  #  #  #  :          0 :                 src = IOVA_TO_VVA(void *, vc_req, addr, &len, VHOST_ACCESS_RO);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
                      # ]
     571   [ #  #  #  #  :          0 :                 if (unlikely(src == NULL || len == 0))
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
     572                 :          0 :                         return 0;
     573                 :            : 
     574                 :            :                 rte_memcpy(dst, src, len);
     575                 :          0 :                 remain -= len;
     576                 :            :                 /* cast is needed for 32-bit architecture */
     577                 :          0 :                 dst = RTE_PTR_ADD(dst, (size_t)len);
     578                 :          0 :                 addr += len;
     579   [ #  #  #  #  :          0 :         } while (unlikely(remain != 0));
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
                      # ]
     580                 :            : 
     581                 :            :         return RTE_MIN(desc->len, size);
     582                 :            : }
     583                 :            : 
     584                 :            : 
     585                 :            : static __rte_always_inline int
     586                 :            : copy_data(void *data, struct vhost_crypto_data_req *vc_req,
     587                 :            :         struct vhost_crypto_desc *head, struct vhost_crypto_desc **cur_desc,
     588                 :            :         uint32_t size, uint32_t max_n_descs)
     589                 :            :         __rte_shared_locks_required(&vc_req->vq->iotlb_lock)
     590                 :            : {
     591                 :            :         struct vhost_crypto_desc *desc = *cur_desc;
     592                 :            :         uint32_t left = size;
     593                 :            : 
     594                 :            :         do {
     595                 :            :                 uint32_t copied;
     596                 :            : 
     597                 :            :                 copied = copy_data_from_desc(data, vc_req, desc, left);
     598   [ #  #  #  #  :          0 :                 if (copied == 0)
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
                      # ]
     599                 :            :                         return -1;
     600                 :          0 :                 left -= copied;
     601                 :          0 :                 data = RTE_PTR_ADD(data, copied);
     602   [ #  #  #  #  :          0 :         } while (left != 0 && ++desc < head + max_n_descs);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
     603                 :            : 
     604   [ #  #  #  #  :          0 :         if (unlikely(left != 0))
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
                      # ]
     605                 :            :                 return -1;
     606                 :            : 
     607   [ #  #  #  #  :          0 :         if (unlikely(desc == head + max_n_descs))
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                      # ]
     608                 :            :                 *cur_desc = NULL;
     609                 :            :         else
     610                 :          0 :                 *cur_desc = desc + 1;
     611                 :            : 
     612                 :            :         return 0;
     613                 :            : }
     614                 :            : 
     615                 :            : static void
     616                 :          0 : write_back_data(struct vhost_crypto_data_req *vc_req)
     617                 :            : {
     618                 :          0 :         struct vhost_crypto_writeback_data *wb_data = vc_req->wb, *wb_last;
     619                 :            : 
     620         [ #  # ]:          0 :         while (wb_data) {
     621         [ #  # ]:          0 :                 rte_memcpy(wb_data->dst, wb_data->src, wb_data->len);
     622         [ #  # ]:          0 :                 memset(wb_data->src, 0, wb_data->len);
     623                 :            :                 wb_last = wb_data;
     624                 :          0 :                 wb_data = wb_data->next;
     625         [ #  # ]:          0 :                 rte_mempool_put(vc_req->wb_pool, wb_last);
     626                 :            :         }
     627                 :          0 : }
     628                 :            : 
     629                 :            : static void
     630                 :          0 : free_wb_data(struct vhost_crypto_writeback_data *wb_data,
     631                 :            :                 struct rte_mempool *mp)
     632                 :            : {
     633         [ #  # ]:          0 :         while (wb_data->next != NULL)
     634                 :          0 :                 free_wb_data(wb_data->next, mp);
     635                 :            : 
     636                 :          0 :         rte_mempool_put(mp, wb_data);
     637                 :          0 : }
     638                 :            : 
     639                 :            : /**
     640                 :            :  * The function will allocate a vhost_crypto_writeback_data linked list
     641                 :            :  * containing the source and destination data pointers for the write back
     642                 :            :  * operation after dequeued from Cryptodev PMD queues.
     643                 :            :  *
     644                 :            :  * @param vc_req
     645                 :            :  *   The vhost crypto data request pointer
     646                 :            :  * @param cur_desc
     647                 :            :  *   The pointer of the current in use descriptor pointer. The content of
     648                 :            :  *   cur_desc is expected to be updated after the function execution.
     649                 :            :  * @param end_wb_data
     650                 :            :  *   The last write back data element to be returned. It is used only in cipher
     651                 :            :  *   and hash chain operations.
     652                 :            :  * @param src
     653                 :            :  *   The source data pointer
     654                 :            :  * @param offset
     655                 :            :  *   The offset to both source and destination data. For source data the offset
     656                 :            :  *   is the number of bytes between src and start point of cipher operation. For
     657                 :            :  *   destination data the offset is the number of bytes from *cur_desc->addr
     658                 :            :  *   to the point where the src will be written to.
     659                 :            :  * @param write_back_len
     660                 :            :  *   The size of the write back length.
     661                 :            :  * @return
     662                 :            :  *   The pointer to the start of the write back data linked list.
     663                 :            :  */
     664                 :            : static __rte_always_inline struct vhost_crypto_writeback_data *
     665                 :            : prepare_write_back_data(struct vhost_crypto_data_req *vc_req,
     666                 :            :                 struct vhost_crypto_desc *head_desc,
     667                 :            :                 struct vhost_crypto_desc **cur_desc,
     668                 :            :                 struct vhost_crypto_writeback_data **end_wb_data,
     669                 :            :                 uint8_t *src,
     670                 :            :                 uint32_t offset,
     671                 :            :                 uint64_t write_back_len,
     672                 :            :                 uint32_t max_n_descs)
     673                 :            :         __rte_shared_locks_required(&vc_req->vq->iotlb_lock)
     674                 :            : {
     675                 :            :         struct vhost_crypto_writeback_data *wb_data, *head;
     676                 :            :         struct vhost_crypto_desc *desc = *cur_desc;
     677                 :            :         uint64_t dlen;
     678                 :            :         uint8_t *dst;
     679                 :            :         int ret;
     680                 :            : 
     681                 :          0 :         ret = rte_mempool_get(vc_req->wb_pool, (void **)&head);
     682   [ #  #  #  #  :          0 :         if (unlikely(ret < 0)) {
          #  #  #  #  #  
                #  #  # ]
     683                 :          0 :                 VC_LOG_ERR("no memory");
     684                 :          0 :                 goto error_exit;
     685                 :            :         }
     686                 :            : 
     687                 :          0 :         wb_data = head;
     688                 :            : 
     689   [ #  #  #  #  :          0 :         if (likely(desc->len > offset)) {
          #  #  #  #  #  
                #  #  # ]
     690                 :          0 :                 wb_data->src = src + offset;
     691                 :          0 :                 dlen = desc->len;
     692   [ #  #  #  #  :          0 :                 dst = IOVA_TO_VVA(uint8_t *, vc_req, desc->addr,
          #  #  #  #  #  
                #  #  # ]
     693                 :            :                         &dlen, VHOST_ACCESS_RW);
     694   [ #  #  #  #  :          0 :                 if (unlikely(!dst || dlen != desc->len)) {
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
                      # ]
     695                 :          0 :                         VC_LOG_ERR("Failed to map descriptor");
     696                 :          0 :                         goto error_exit;
     697                 :            :                 }
     698                 :            : 
     699                 :          0 :                 wb_data->dst = dst + offset;
     700                 :          0 :                 wb_data->len = RTE_MIN(dlen - offset, write_back_len);
     701                 :          0 :                 write_back_len -= wb_data->len;
     702                 :          0 :                 src += offset + wb_data->len;
     703                 :            :                 offset = 0;
     704                 :            : 
     705   [ #  #  #  #  :          0 :                 if (unlikely(write_back_len)) {
          #  #  #  #  #  
                #  #  # ]
     706                 :          0 :                         ret = rte_mempool_get(vc_req->wb_pool,
     707   [ #  #  #  #  :          0 :                                         (void **)&(wb_data->next));
          #  #  #  #  #  
                #  #  # ]
     708   [ #  #  #  #  :          0 :                         if (unlikely(ret < 0)) {
          #  #  #  #  #  
                #  #  # ]
     709                 :          0 :                                 VC_LOG_ERR("no memory");
     710                 :          0 :                                 goto error_exit;
     711                 :            :                         }
     712                 :            : 
     713                 :          0 :                         wb_data = wb_data->next;
     714                 :            :                 } else
     715                 :          0 :                         wb_data->next = NULL;
     716                 :            :         } else
     717                 :          0 :                 offset -= desc->len;
     718                 :            : 
     719                 :          0 :         while (write_back_len &&
     720   [ #  #  #  #  :          0 :                         desc >= head_desc &&
          #  #  #  #  #  
                #  #  # ]
     721   [ #  #  #  #  :          0 :                         desc - head_desc < (int)max_n_descs) {
          #  #  #  #  #  
                #  #  # ]
     722                 :          0 :                 desc++;
     723   [ #  #  #  #  :          0 :                 if (unlikely(!(desc->flags & VRING_DESC_F_WRITE))) {
          #  #  #  #  #  
                #  #  # ]
     724                 :          0 :                         VC_LOG_ERR("incorrect descriptor");
     725                 :          0 :                         goto error_exit;
     726                 :            :                 }
     727                 :            : 
     728   [ #  #  #  #  :          0 :                 if (desc->len <= offset) {
          #  #  #  #  #  
                #  #  # ]
     729                 :          0 :                         offset -= desc->len;
     730                 :          0 :                         continue;
     731                 :            :                 }
     732                 :            : 
     733                 :          0 :                 dlen = desc->len;
     734                 :          0 :                 dst = IOVA_TO_VVA(uint8_t *, vc_req, desc->addr, &dlen,
     735   [ #  #  #  #  :          0 :                                 VHOST_ACCESS_RW) + offset;
          #  #  #  #  #  
                #  #  # ]
     736   [ #  #  #  #  :          0 :                 if (unlikely(dst == NULL || dlen != desc->len)) {
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
                      # ]
     737                 :          0 :                         VC_LOG_ERR("Failed to map descriptor");
     738                 :          0 :                         goto error_exit;
     739                 :            :                 }
     740                 :            : 
     741                 :          0 :                 wb_data->src = src + offset;
     742                 :          0 :                 wb_data->dst = dst;
     743                 :          0 :                 wb_data->len = RTE_MIN(desc->len - offset, write_back_len);
     744                 :          0 :                 write_back_len -= wb_data->len;
     745                 :          0 :                 src += wb_data->len;
     746                 :            :                 offset = 0;
     747                 :            : 
     748   [ #  #  #  #  :          0 :                 if (write_back_len) {
          #  #  #  #  #  
                #  #  # ]
     749                 :          0 :                         ret = rte_mempool_get(vc_req->wb_pool,
     750   [ #  #  #  #  :          0 :                                         (void **)&(wb_data->next));
          #  #  #  #  #  
                #  #  # ]
     751   [ #  #  #  #  :          0 :                         if (unlikely(ret < 0)) {
          #  #  #  #  #  
                #  #  # ]
     752                 :          0 :                                 VC_LOG_ERR("no memory");
     753                 :          0 :                                 goto error_exit;
     754                 :            :                         }
     755                 :            : 
     756                 :          0 :                         wb_data = wb_data->next;
     757                 :            :                 } else
     758                 :          0 :                         wb_data->next = NULL;
     759                 :            :         }
     760                 :            : 
     761   [ #  #  #  #  :          0 :         if (unlikely(desc - head_desc == (int)max_n_descs))
          #  #  #  #  #  
                #  #  # ]
     762                 :            :                 *cur_desc = NULL;
     763                 :            :         else
     764                 :          0 :                 *cur_desc = desc + 1;
     765                 :            : 
     766                 :            :         *end_wb_data = wb_data;
     767                 :            : 
     768                 :          0 :         return head;
     769                 :            : 
     770                 :          0 : error_exit:
     771   [ #  #  #  #  :          0 :         if (head)
          #  #  #  #  #  
                #  #  # ]
     772                 :          0 :                 free_wb_data(head, vc_req->wb_pool);
     773                 :            : 
     774                 :            :         return NULL;
     775                 :            : }
     776                 :            : 
     777                 :            : static __rte_always_inline uint8_t
     778                 :            : vhost_crypto_check_cipher_request(struct virtio_crypto_cipher_data_req *req)
     779                 :            : {
     780   [ #  #  #  #  :          0 :         if (likely((req->para.iv_len <= VHOST_CRYPTO_MAX_IV_LEN) &&
          #  #  #  #  #  
                #  #  # ]
     781                 :            :                 (req->para.src_data_len <= RTE_MBUF_DEFAULT_BUF_SIZE) &&
     782                 :            :                 (req->para.dst_data_len >= req->para.src_data_len) &&
     783                 :            :                 (req->para.dst_data_len <= RTE_MBUF_DEFAULT_BUF_SIZE)))
     784                 :          0 :                 return VIRTIO_CRYPTO_OK;
     785                 :            :         return VIRTIO_CRYPTO_BADMSG;
     786                 :            : }
     787                 :            : 
     788                 :            : static __rte_always_inline uint8_t
     789                 :            : prepare_sym_cipher_op(struct vhost_crypto *vcrypto, struct rte_crypto_op *op,
     790                 :            :                 struct vhost_crypto_data_req *vc_req,
     791                 :            :                 struct virtio_crypto_cipher_data_req *cipher,
     792                 :            :                 struct vhost_crypto_desc *head,
     793                 :            :                 uint32_t max_n_descs)
     794                 :            :         __rte_shared_locks_required(&vc_req->vq->iotlb_lock)
     795                 :            : {
     796                 :            :         struct vhost_crypto_desc *desc = head;
     797                 :            :         struct vhost_crypto_writeback_data *ewb = NULL;
     798                 :          0 :         struct rte_mbuf *m_src = op->sym->m_src, *m_dst = op->sym->m_dst;
     799                 :          0 :         uint8_t *iv_data = rte_crypto_op_ctod_offset(op, uint8_t *, IV_OFFSET);
     800                 :            :         uint8_t ret = vhost_crypto_check_cipher_request(cipher);
     801                 :            : 
     802   [ #  #  #  # ]:          0 :         if (unlikely(ret != VIRTIO_CRYPTO_OK))
     803                 :          0 :                 goto error_exit;
     804                 :            : 
     805                 :            :         /* prepare */
     806                 :            :         /* iv */
     807   [ #  #  #  # ]:          0 :         if (unlikely(copy_data(iv_data, vc_req, head, &desc,
     808                 :            :                         cipher->para.iv_len, max_n_descs))) {
     809                 :          0 :                 VC_LOG_ERR("Incorrect virtio descriptor");
     810                 :            :                 ret = VIRTIO_CRYPTO_BADMSG;
     811                 :          0 :                 goto error_exit;
     812                 :            :         }
     813                 :            : 
     814   [ #  #  #  #  :          0 :         switch (vcrypto->option) {
                   #  # ]
     815                 :          0 :         case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
     816                 :          0 :                 m_src->data_len = cipher->para.src_data_len;
     817                 :          0 :                 rte_mbuf_iova_set(m_src,
     818   [ #  #  #  # ]:          0 :                                   gpa_to_hpa(vcrypto->dev, desc->addr, cipher->para.src_data_len));
     819                 :          0 :                 m_src->buf_addr = get_data_ptr(vc_req, desc, VHOST_ACCESS_RO);
     820   [ #  #  #  #  :          0 :                 if (unlikely(rte_mbuf_iova_get(m_src) == 0 || m_src->buf_addr == NULL)) {
             #  #  #  # ]
     821                 :          0 :                         VC_LOG_ERR("zero_copy may fail due to cross page data");
     822                 :            :                         ret = VIRTIO_CRYPTO_ERR;
     823                 :          0 :                         goto error_exit;
     824                 :            :                 }
     825                 :            : 
     826   [ #  #  #  # ]:          0 :                 if (unlikely(move_desc(head, &desc, cipher->para.src_data_len,
     827                 :            :                                 max_n_descs) < 0)) {
     828                 :          0 :                         VC_LOG_ERR("Incorrect descriptor");
     829                 :            :                         ret = VIRTIO_CRYPTO_ERR;
     830                 :          0 :                         goto error_exit;
     831                 :            :                 }
     832                 :            : 
     833                 :            :                 break;
     834                 :          0 :         case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
     835                 :          0 :                 vc_req->wb_pool = vcrypto->wb_pool;
     836                 :          0 :                 m_src->data_len = cipher->para.src_data_len;
     837   [ #  #  #  # ]:          0 :                 if (unlikely(copy_data(rte_pktmbuf_mtod(m_src, uint8_t *),
     838                 :            :                                 vc_req, head, &desc, cipher->para.src_data_len,
     839                 :            :                                 max_n_descs) < 0)) {
     840                 :          0 :                         VC_LOG_ERR("Incorrect virtio descriptor");
     841                 :            :                         ret = VIRTIO_CRYPTO_BADMSG;
     842                 :          0 :                         goto error_exit;
     843                 :            :                 }
     844                 :            :                 break;
     845                 :          0 :         default:
     846                 :            :                 ret = VIRTIO_CRYPTO_BADMSG;
     847                 :          0 :                 goto error_exit;
     848                 :            :         }
     849                 :            : 
     850                 :            :         /* dst */
     851                 :            :         desc = find_write_desc(head, desc, max_n_descs);
     852   [ #  #  #  # ]:          0 :         if (unlikely(!desc)) {
     853                 :          0 :                 VC_LOG_ERR("Cannot find write location");
     854                 :            :                 ret = VIRTIO_CRYPTO_BADMSG;
     855                 :          0 :                 goto error_exit;
     856                 :            :         }
     857                 :            : 
     858   [ #  #  #  #  :          0 :         switch (vcrypto->option) {
                   #  # ]
     859                 :          0 :         case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
     860                 :          0 :                 rte_mbuf_iova_set(m_dst,
     861   [ #  #  #  # ]:          0 :                                   gpa_to_hpa(vcrypto->dev, desc->addr, cipher->para.dst_data_len));
     862                 :          0 :                 m_dst->buf_addr = get_data_ptr(vc_req, desc, VHOST_ACCESS_RW);
     863   [ #  #  #  #  :          0 :                 if (unlikely(rte_mbuf_iova_get(m_dst) == 0 || m_dst->buf_addr == NULL)) {
             #  #  #  # ]
     864                 :          0 :                         VC_LOG_ERR("zero_copy may fail due to cross page data");
     865                 :            :                         ret = VIRTIO_CRYPTO_ERR;
     866                 :          0 :                         goto error_exit;
     867                 :            :                 }
     868                 :            : 
     869   [ #  #  #  # ]:          0 :                 if (unlikely(move_desc(head, &desc, cipher->para.dst_data_len,
     870                 :            :                                 max_n_descs) < 0)) {
     871                 :          0 :                         VC_LOG_ERR("Incorrect descriptor");
     872                 :            :                         ret = VIRTIO_CRYPTO_ERR;
     873                 :          0 :                         goto error_exit;
     874                 :            :                 }
     875                 :            : 
     876                 :          0 :                 m_dst->data_len = cipher->para.dst_data_len;
     877                 :          0 :                 break;
     878                 :          0 :         case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
     879                 :          0 :                 vc_req->wb = prepare_write_back_data(vc_req, head, &desc, &ewb,
     880                 :          0 :                                 rte_pktmbuf_mtod(m_src, uint8_t *), 0,
     881   [ #  #  #  # ]:          0 :                                 cipher->para.dst_data_len, max_n_descs);
     882   [ #  #  #  # ]:          0 :                 if (unlikely(vc_req->wb == NULL)) {
     883                 :            :                         ret = VIRTIO_CRYPTO_ERR;
     884                 :          0 :                         goto error_exit;
     885                 :            :                 }
     886                 :            : 
     887                 :            :                 break;
     888                 :          0 :         default:
     889                 :            :                 ret = VIRTIO_CRYPTO_BADMSG;
     890                 :          0 :                 goto error_exit;
     891                 :            :         }
     892                 :            : 
     893                 :            :         /* src data */
     894                 :          0 :         op->type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
     895                 :          0 :         op->sess_type = RTE_CRYPTO_OP_WITH_SESSION;
     896                 :            : 
     897                 :          0 :         op->sym->cipher.data.offset = 0;
     898   [ #  #  #  # ]:          0 :         op->sym->cipher.data.length = cipher->para.src_data_len;
     899                 :            : 
     900                 :          0 :         vc_req->inhdr = get_data_ptr(vc_req, desc, VHOST_ACCESS_WO);
     901   [ #  #  #  # ]:          0 :         if (unlikely(vc_req->inhdr == NULL)) {
     902                 :            :                 ret = VIRTIO_CRYPTO_BADMSG;
     903                 :          0 :                 goto error_exit;
     904                 :            :         }
     905                 :            : 
     906                 :          0 :         vc_req->inhdr->status = VIRTIO_CRYPTO_OK;
     907                 :          0 :         vc_req->len = cipher->para.dst_data_len + INHDR_LEN;
     908                 :            : 
     909                 :          0 :         return 0;
     910                 :            : 
     911                 :          0 : error_exit:
     912   [ #  #  #  # ]:          0 :         if (vc_req->wb)
     913                 :          0 :                 free_wb_data(vc_req->wb, vc_req->wb_pool);
     914                 :            : 
     915                 :          0 :         vc_req->len = INHDR_LEN;
     916                 :          0 :         return ret;
     917                 :            : }
     918                 :            : 
     919                 :            : static __rte_always_inline uint8_t
     920                 :            : vhost_crypto_check_chain_request(struct virtio_crypto_alg_chain_data_req *req)
     921                 :            : {
     922   [ #  #  #  #  :          0 :         if (likely((req->para.iv_len <= VHOST_CRYPTO_MAX_IV_LEN) &&
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                      # ]
     923                 :            :                 (req->para.src_data_len <= VHOST_CRYPTO_MAX_DATA_SIZE) &&
     924                 :            :                 (req->para.dst_data_len >= req->para.src_data_len) &&
     925                 :            :                 (req->para.dst_data_len <= VHOST_CRYPTO_MAX_DATA_SIZE) &&
     926                 :            :                 (req->para.cipher_start_src_offset <
     927                 :            :                         VHOST_CRYPTO_MAX_DATA_SIZE) &&
     928                 :            :                 (req->para.len_to_cipher <= VHOST_CRYPTO_MAX_DATA_SIZE) &&
     929                 :            :                 (req->para.hash_start_src_offset <
     930                 :            :                         VHOST_CRYPTO_MAX_DATA_SIZE) &&
     931                 :            :                 (req->para.len_to_hash <= VHOST_CRYPTO_MAX_DATA_SIZE) &&
     932                 :            :                 (req->para.cipher_start_src_offset + req->para.len_to_cipher <=
     933                 :            :                         req->para.src_data_len) &&
     934                 :            :                 (req->para.hash_start_src_offset + req->para.len_to_hash <=
     935                 :            :                         req->para.src_data_len) &&
     936                 :            :                 (req->para.dst_data_len + req->para.hash_result_len <=
     937                 :            :                         VHOST_CRYPTO_MAX_DATA_SIZE)))
     938                 :          0 :                 return VIRTIO_CRYPTO_OK;
     939                 :            :         return VIRTIO_CRYPTO_BADMSG;
     940                 :            : }
     941                 :            : 
     942                 :            : static __rte_always_inline uint8_t
     943                 :            : prepare_sym_chain_op(struct vhost_crypto *vcrypto, struct rte_crypto_op *op,
     944                 :            :                 struct vhost_crypto_data_req *vc_req,
     945                 :            :                 struct virtio_crypto_alg_chain_data_req *chain,
     946                 :            :                 struct vhost_crypto_desc *head,
     947                 :            :                 uint32_t max_n_descs)
     948                 :            :         __rte_shared_locks_required(&vc_req->vq->iotlb_lock)
     949                 :            : {
     950                 :            :         struct vhost_crypto_desc *desc = head, *digest_desc;
     951                 :            :         struct vhost_crypto_writeback_data *ewb = NULL, *ewb2 = NULL;
     952                 :          0 :         struct rte_mbuf *m_src = op->sym->m_src, *m_dst = op->sym->m_dst;
     953                 :          0 :         uint8_t *iv_data = rte_crypto_op_ctod_offset(op, uint8_t *, IV_OFFSET);
     954                 :            :         uint32_t digest_offset;
     955                 :            :         void *digest_addr;
     956                 :            :         uint8_t ret = vhost_crypto_check_chain_request(chain);
     957                 :            : 
     958   [ #  #  #  # ]:          0 :         if (unlikely(ret != VIRTIO_CRYPTO_OK))
     959                 :          0 :                 goto error_exit;
     960                 :            : 
     961                 :            :         /* prepare */
     962                 :            :         /* iv */
     963   [ #  #  #  # ]:          0 :         if (unlikely(copy_data(iv_data, vc_req, head, &desc,
     964                 :            :                         chain->para.iv_len, max_n_descs) < 0)) {
     965                 :          0 :                 VC_LOG_ERR("Incorrect virtio descriptor");
     966                 :            :                 ret = VIRTIO_CRYPTO_BADMSG;
     967                 :          0 :                 goto error_exit;
     968                 :            :         }
     969                 :            : 
     970   [ #  #  #  #  :          0 :         switch (vcrypto->option) {
                   #  # ]
     971                 :          0 :         case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
     972                 :          0 :                 m_src->data_len = chain->para.src_data_len;
     973                 :          0 :                 m_dst->data_len = chain->para.dst_data_len;
     974                 :            : 
     975                 :          0 :                 rte_mbuf_iova_set(m_src,
     976   [ #  #  #  # ]:          0 :                                   gpa_to_hpa(vcrypto->dev, desc->addr, chain->para.src_data_len));
     977                 :          0 :                 m_src->buf_addr = get_data_ptr(vc_req, desc, VHOST_ACCESS_RO);
     978   [ #  #  #  #  :          0 :                 if (unlikely(rte_mbuf_iova_get(m_src) == 0 || m_src->buf_addr == NULL)) {
             #  #  #  # ]
     979                 :          0 :                         VC_LOG_ERR("zero_copy may fail due to cross page data");
     980                 :            :                         ret = VIRTIO_CRYPTO_ERR;
     981                 :          0 :                         goto error_exit;
     982                 :            :                 }
     983                 :            : 
     984   [ #  #  #  # ]:          0 :                 if (unlikely(move_desc(head, &desc, chain->para.src_data_len,
     985                 :            :                                 max_n_descs) < 0)) {
     986                 :          0 :                         VC_LOG_ERR("Incorrect descriptor");
     987                 :            :                         ret = VIRTIO_CRYPTO_ERR;
     988                 :          0 :                         goto error_exit;
     989                 :            :                 }
     990                 :            :                 break;
     991                 :          0 :         case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
     992                 :          0 :                 vc_req->wb_pool = vcrypto->wb_pool;
     993                 :          0 :                 m_src->data_len = chain->para.src_data_len;
     994   [ #  #  #  # ]:          0 :                 if (unlikely(copy_data(rte_pktmbuf_mtod(m_src, uint8_t *),
     995                 :            :                                 vc_req, head, &desc, chain->para.src_data_len,
     996                 :            :                                 max_n_descs) < 0)) {
     997                 :          0 :                         VC_LOG_ERR("Incorrect virtio descriptor");
     998                 :            :                         ret = VIRTIO_CRYPTO_BADMSG;
     999                 :          0 :                         goto error_exit;
    1000                 :            :                 }
    1001                 :            : 
    1002                 :            :                 break;
    1003                 :          0 :         default:
    1004                 :            :                 ret = VIRTIO_CRYPTO_BADMSG;
    1005                 :          0 :                 goto error_exit;
    1006                 :            :         }
    1007                 :            : 
    1008                 :            :         /* dst */
    1009                 :            :         desc = find_write_desc(head, desc, max_n_descs);
    1010   [ #  #  #  # ]:          0 :         if (unlikely(!desc)) {
    1011                 :          0 :                 VC_LOG_ERR("Cannot find write location");
    1012                 :            :                 ret = VIRTIO_CRYPTO_BADMSG;
    1013                 :          0 :                 goto error_exit;
    1014                 :            :         }
    1015                 :            : 
    1016   [ #  #  #  #  :          0 :         switch (vcrypto->option) {
                   #  # ]
    1017                 :          0 :         case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
    1018                 :          0 :                 rte_mbuf_iova_set(m_dst,
    1019   [ #  #  #  # ]:          0 :                                   gpa_to_hpa(vcrypto->dev, desc->addr, chain->para.dst_data_len));
    1020                 :          0 :                 m_dst->buf_addr = get_data_ptr(vc_req, desc, VHOST_ACCESS_RW);
    1021   [ #  #  #  #  :          0 :                 if (unlikely(rte_mbuf_iova_get(m_dst) == 0 || m_dst->buf_addr == NULL)) {
             #  #  #  # ]
    1022                 :          0 :                         VC_LOG_ERR("zero_copy may fail due to cross page data");
    1023                 :            :                         ret = VIRTIO_CRYPTO_ERR;
    1024                 :          0 :                         goto error_exit;
    1025                 :            :                 }
    1026                 :            : 
    1027   [ #  #  #  # ]:          0 :                 if (unlikely(move_desc(vc_req->head, &desc,
    1028                 :            :                                 chain->para.dst_data_len, max_n_descs) < 0)) {
    1029                 :          0 :                         VC_LOG_ERR("Incorrect descriptor");
    1030                 :            :                         ret = VIRTIO_CRYPTO_ERR;
    1031                 :          0 :                         goto error_exit;
    1032                 :            :                 }
    1033                 :            : 
    1034   [ #  #  #  # ]:          0 :                 op->sym->auth.digest.phys_addr = gpa_to_hpa(vcrypto->dev,
    1035   [ #  #  #  # ]:          0 :                                 desc->addr, chain->para.hash_result_len);
    1036                 :          0 :                 op->sym->auth.digest.data = get_data_ptr(vc_req, desc,
    1037                 :            :                                 VHOST_ACCESS_RW);
    1038   [ #  #  #  # ]:          0 :                 if (unlikely(op->sym->auth.digest.phys_addr == 0)) {
    1039                 :          0 :                         VC_LOG_ERR("zero_copy may fail due to cross page data");
    1040                 :            :                         ret = VIRTIO_CRYPTO_ERR;
    1041                 :          0 :                         goto error_exit;
    1042                 :            :                 }
    1043                 :            : 
    1044   [ #  #  #  # ]:          0 :                 if (unlikely(move_desc(head, &desc,
    1045                 :            :                                 chain->para.hash_result_len,
    1046                 :            :                                 max_n_descs) < 0)) {
    1047                 :          0 :                         VC_LOG_ERR("Incorrect descriptor");
    1048                 :            :                         ret = VIRTIO_CRYPTO_ERR;
    1049                 :          0 :                         goto error_exit;
    1050                 :            :                 }
    1051                 :            : 
    1052                 :            :                 break;
    1053                 :          0 :         case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
    1054                 :          0 :                 vc_req->wb = prepare_write_back_data(vc_req, head, &desc, &ewb,
    1055                 :          0 :                                 rte_pktmbuf_mtod(m_src, uint8_t *),
    1056                 :            :                                 chain->para.cipher_start_src_offset,
    1057                 :          0 :                                 chain->para.dst_data_len -
    1058   [ #  #  #  # ]:          0 :                                         chain->para.cipher_start_src_offset,
    1059                 :            :                                 max_n_descs);
    1060   [ #  #  #  # ]:          0 :                 if (unlikely(vc_req->wb == NULL)) {
    1061                 :            :                         ret = VIRTIO_CRYPTO_ERR;
    1062                 :          0 :                         goto error_exit;
    1063                 :            :                 }
    1064                 :            : 
    1065                 :            :                 digest_desc = desc;
    1066                 :          0 :                 digest_offset = m_src->data_len;
    1067                 :          0 :                 digest_addr = rte_pktmbuf_mtod_offset(m_src, void *,
    1068                 :            :                                 digest_offset);
    1069                 :            : 
    1070                 :            :                 /** create a wb_data for digest */
    1071                 :          0 :                 ewb->next = prepare_write_back_data(vc_req, head, &desc,
    1072                 :            :                                 &ewb2, digest_addr, 0,
    1073   [ #  #  #  # ]:          0 :                                 chain->para.hash_result_len, max_n_descs);
    1074   [ #  #  #  # ]:          0 :                 if (unlikely(ewb->next == NULL)) {
    1075                 :            :                         ret = VIRTIO_CRYPTO_ERR;
    1076                 :          0 :                         goto error_exit;
    1077                 :            :                 }
    1078                 :            : 
    1079   [ #  #  #  # ]:          0 :                 if (unlikely(copy_data(digest_addr, vc_req, head, &digest_desc,
    1080                 :            :                                 chain->para.hash_result_len,
    1081                 :            :                                 max_n_descs) < 0)) {
    1082                 :          0 :                         VC_LOG_ERR("Incorrect virtio descriptor");
    1083                 :            :                         ret = VIRTIO_CRYPTO_BADMSG;
    1084                 :          0 :                         goto error_exit;
    1085                 :            :                 }
    1086                 :            : 
    1087                 :          0 :                 op->sym->auth.digest.data = digest_addr;
    1088                 :          0 :                 op->sym->auth.digest.phys_addr = rte_pktmbuf_iova_offset(m_src,
    1089                 :            :                                 digest_offset);
    1090                 :          0 :                 break;
    1091                 :          0 :         default:
    1092                 :            :                 ret = VIRTIO_CRYPTO_BADMSG;
    1093                 :          0 :                 goto error_exit;
    1094                 :            :         }
    1095                 :            : 
    1096                 :            :         /* record inhdr */
    1097                 :          0 :         vc_req->inhdr = get_data_ptr(vc_req, desc, VHOST_ACCESS_WO);
    1098   [ #  #  #  # ]:          0 :         if (unlikely(vc_req->inhdr == NULL)) {
    1099                 :            :                 ret = VIRTIO_CRYPTO_BADMSG;
    1100                 :          0 :                 goto error_exit;
    1101                 :            :         }
    1102                 :            : 
    1103                 :          0 :         vc_req->inhdr->status = VIRTIO_CRYPTO_OK;
    1104                 :            : 
    1105                 :          0 :         op->type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
    1106                 :          0 :         op->sess_type = RTE_CRYPTO_OP_WITH_SESSION;
    1107                 :            : 
    1108                 :          0 :         op->sym->cipher.data.offset = chain->para.cipher_start_src_offset;
    1109                 :          0 :         op->sym->cipher.data.length = chain->para.src_data_len -
    1110                 :            :                         chain->para.cipher_start_src_offset;
    1111                 :            : 
    1112                 :          0 :         op->sym->auth.data.offset = chain->para.hash_start_src_offset;
    1113                 :          0 :         op->sym->auth.data.length = chain->para.len_to_hash;
    1114                 :            : 
    1115                 :          0 :         vc_req->len = chain->para.dst_data_len + chain->para.hash_result_len +
    1116                 :            :                         INHDR_LEN;
    1117                 :          0 :         return 0;
    1118                 :            : 
    1119                 :          0 : error_exit:
    1120   [ #  #  #  # ]:          0 :         if (vc_req->wb)
    1121                 :          0 :                 free_wb_data(vc_req->wb, vc_req->wb_pool);
    1122                 :          0 :         vc_req->len = INHDR_LEN;
    1123                 :          0 :         return ret;
    1124                 :            : }
    1125                 :            : 
    1126                 :            : /**
    1127                 :            :  * Process on descriptor
    1128                 :            :  */
    1129                 :            : static __rte_always_inline int
    1130                 :            : vhost_crypto_process_one_req(struct vhost_crypto *vcrypto,
    1131                 :            :                 struct vhost_virtqueue *vq, struct rte_crypto_op *op,
    1132                 :            :                 struct vring_desc *head, struct vhost_crypto_desc *descs,
    1133                 :            :                 uint16_t desc_idx)
    1134                 :            :         __rte_no_thread_safety_analysis /* FIXME: requires iotlb_lock? */
    1135                 :            : {
    1136                 :          0 :         struct vhost_crypto_data_req *vc_req = rte_mbuf_to_priv(op->sym->m_src);
    1137                 :            :         struct rte_cryptodev_sym_session *session;
    1138                 :            :         struct virtio_crypto_op_data_req req;
    1139                 :            :         struct virtio_crypto_inhdr *inhdr;
    1140                 :            :         struct vhost_crypto_desc *desc = descs;
    1141                 :            :         struct vring_desc *src_desc;
    1142                 :            :         uint64_t session_id;
    1143                 :            :         uint64_t dlen;
    1144                 :            :         uint32_t nb_descs = 0, max_n_descs, i;
    1145                 :            :         int err;
    1146                 :            : 
    1147                 :          0 :         vc_req->desc_idx = desc_idx;
    1148                 :          0 :         vc_req->dev = vcrypto->dev;
    1149                 :          0 :         vc_req->vq = vq;
    1150                 :            : 
    1151                 :          0 :         if (unlikely((head->flags & VRING_DESC_F_INDIRECT) == 0)) {
    1152                 :          0 :                 VC_LOG_ERR("Invalid descriptor");
    1153                 :          0 :                 return -1;
    1154                 :            :         }
    1155                 :            : 
    1156                 :          0 :         dlen = head->len;
    1157   [ #  #  #  # ]:          0 :         src_desc = IOVA_TO_VVA(struct vring_desc *, vc_req, head->addr,
    1158                 :            :                         &dlen, VHOST_ACCESS_RO);
    1159   [ #  #  #  #  :          0 :         if (unlikely(!src_desc || dlen != head->len)) {
             #  #  #  # ]
    1160                 :          0 :                 VC_LOG_ERR("Invalid descriptor");
    1161                 :          0 :                 return -1;
    1162                 :            :         }
    1163                 :            :         head = src_desc;
    1164                 :            : 
    1165                 :          0 :         nb_descs = max_n_descs = dlen / sizeof(struct vring_desc);
    1166   [ #  #  #  # ]:          0 :         if (unlikely(nb_descs > VHOST_CRYPTO_MAX_N_DESC || nb_descs == 0)) {
    1167                 :            :                 err = VIRTIO_CRYPTO_ERR;
    1168                 :          0 :                 VC_LOG_ERR("Cannot process num of descriptors %u", nb_descs);
    1169   [ #  #  #  # ]:          0 :                 if (nb_descs > 0) {
    1170                 :            :                         struct vring_desc *inhdr_desc = head;
    1171   [ #  #  #  # ]:          0 :                         while (inhdr_desc->flags & VRING_DESC_F_NEXT) {
    1172   [ #  #  #  # ]:          0 :                                 if (inhdr_desc->next >= max_n_descs)
    1173                 :            :                                         return -1;
    1174                 :          0 :                                 inhdr_desc = &head[inhdr_desc->next];
    1175                 :            :                         }
    1176   [ #  #  #  # ]:          0 :                         if (inhdr_desc->len != sizeof(*inhdr))
    1177                 :            :                                 return -1;
    1178   [ #  #  #  # ]:          0 :                         inhdr = IOVA_TO_VVA(struct virtio_crypto_inhdr *,
    1179                 :            :                                         vc_req, inhdr_desc->addr, &dlen,
    1180                 :            :                                         VHOST_ACCESS_WO);
    1181   [ #  #  #  #  :          0 :                         if (unlikely(!inhdr || dlen != inhdr_desc->len))
             #  #  #  # ]
    1182                 :            :                                 return -1;
    1183                 :          0 :                         inhdr->status = VIRTIO_CRYPTO_ERR;
    1184                 :          0 :                         return -1;
    1185                 :            :                 }
    1186                 :            :         }
    1187                 :            : 
    1188                 :            :         /* copy descriptors to local variable */
    1189   [ #  #  #  # ]:          0 :         for (i = 0; i < max_n_descs; i++) {
    1190                 :          0 :                 desc->addr = src_desc->addr;
    1191                 :          0 :                 desc->len = src_desc->len;
    1192                 :          0 :                 desc->flags = src_desc->flags;
    1193                 :          0 :                 desc++;
    1194   [ #  #  #  # ]:          0 :                 if (unlikely((src_desc->flags & VRING_DESC_F_NEXT) == 0))
    1195                 :            :                         break;
    1196   [ #  #  #  # ]:          0 :                 if (unlikely(src_desc->next >= max_n_descs)) {
    1197                 :            :                         err = VIRTIO_CRYPTO_BADMSG;
    1198                 :          0 :                         VC_LOG_ERR("Invalid descriptor");
    1199                 :          0 :                         goto error_exit;
    1200                 :            :                 }
    1201                 :          0 :                 src_desc = &head[src_desc->next];
    1202                 :            :         }
    1203                 :            : 
    1204                 :          0 :         vc_req->head = head;
    1205                 :          0 :         vc_req->zero_copy = vcrypto->option;
    1206                 :            : 
    1207                 :            :         nb_descs = desc - descs;
    1208                 :            :         desc = descs;
    1209                 :            : 
    1210   [ #  #  #  # ]:          0 :         if (unlikely(desc->len < sizeof(req))) {
    1211                 :            :                 err = VIRTIO_CRYPTO_BADMSG;
    1212                 :          0 :                 VC_LOG_ERR("Invalid descriptor");
    1213                 :          0 :                 goto error_exit;
    1214                 :            :         }
    1215                 :            : 
    1216   [ #  #  #  # ]:          0 :         if (unlikely(copy_data(&req, vc_req, descs, &desc, sizeof(req),
    1217                 :            :                         max_n_descs) < 0)) {
    1218                 :            :                 err = VIRTIO_CRYPTO_BADMSG;
    1219                 :          0 :                 VC_LOG_ERR("Invalid descriptor");
    1220                 :          0 :                 goto error_exit;
    1221                 :            :         }
    1222                 :            : 
    1223                 :            :         /* desc is advanced by 1 now */
    1224                 :            :         max_n_descs -= 1;
    1225                 :            : 
    1226   [ #  #  #  # ]:          0 :         switch (req.header.opcode) {
    1227                 :          0 :         case VIRTIO_CRYPTO_CIPHER_ENCRYPT:
    1228                 :            :         case VIRTIO_CRYPTO_CIPHER_DECRYPT:
    1229                 :          0 :                 session_id = req.header.session_id;
    1230                 :            : 
    1231                 :            :                 /* one branch to avoid unnecessary table lookup */
    1232   [ #  #  #  # ]:          0 :                 if (vcrypto->cache_session_id != session_id) {
    1233                 :          0 :                         err = rte_hash_lookup_data(vcrypto->session_map,
    1234                 :            :                                         &session_id, (void **)&session);
    1235   [ #  #  #  # ]:          0 :                         if (unlikely(err < 0)) {
    1236                 :            :                                 err = VIRTIO_CRYPTO_ERR;
    1237                 :          0 :                                 VC_LOG_ERR("Failed to find session %"PRIu64,
    1238                 :            :                                                 session_id);
    1239                 :          0 :                                 goto error_exit;
    1240                 :            :                         }
    1241                 :            : 
    1242                 :          0 :                         vcrypto->cache_session = session;
    1243                 :          0 :                         vcrypto->cache_session_id = session_id;
    1244                 :            :                 }
    1245                 :            : 
    1246                 :          0 :                 session = vcrypto->cache_session;
    1247                 :            : 
    1248                 :          0 :                 err = rte_crypto_op_attach_sym_session(op, session);
    1249   [ #  #  #  # ]:          0 :                 if (unlikely(err < 0)) {
    1250                 :            :                         err = VIRTIO_CRYPTO_ERR;
    1251                 :          0 :                         VC_LOG_ERR("Failed to attach session to op");
    1252                 :          0 :                         goto error_exit;
    1253                 :            :                 }
    1254                 :            : 
    1255   [ #  #  #  #  :          0 :                 switch (req.u.sym_req.op_type) {
             #  #  #  # ]
    1256                 :          0 :                 case VIRTIO_CRYPTO_SYM_OP_NONE:
    1257                 :            :                         err = VIRTIO_CRYPTO_NOTSUPP;
    1258                 :          0 :                         break;
    1259   [ #  #  #  # ]:          0 :                 case VIRTIO_CRYPTO_SYM_OP_CIPHER:
    1260                 :          0 :                         err = prepare_sym_cipher_op(vcrypto, op, vc_req,
    1261                 :            :                                         &req.u.sym_req.u.cipher, desc,
    1262                 :            :                                         max_n_descs);
    1263                 :          0 :                         break;
    1264   [ #  #  #  # ]:          0 :                 case VIRTIO_CRYPTO_SYM_OP_ALGORITHM_CHAINING:
    1265                 :          0 :                         err = prepare_sym_chain_op(vcrypto, op, vc_req,
    1266                 :            :                                         &req.u.sym_req.u.chain, desc,
    1267                 :            :                                         max_n_descs);
    1268                 :          0 :                         break;
    1269                 :            :                 }
    1270   [ #  #  #  # ]:          0 :                 if (unlikely(err != 0)) {
    1271                 :          0 :                         VC_LOG_ERR("Failed to process sym request");
    1272                 :          0 :                         goto error_exit;
    1273                 :            :                 }
    1274                 :            :                 break;
    1275                 :          0 :         default:
    1276                 :            :                 err = VIRTIO_CRYPTO_ERR;
    1277                 :          0 :                 VC_LOG_ERR("Unsupported symmetric crypto request type %u",
    1278                 :            :                                 req.header.opcode);
    1279                 :          0 :                 goto error_exit;
    1280                 :            :         }
    1281                 :            : 
    1282                 :            :         return 0;
    1283                 :            : 
    1284   [ #  #  #  # ]:          0 : error_exit:
    1285                 :            : 
    1286                 :            :         inhdr = reach_inhdr(vc_req, descs, max_n_descs);
    1287   [ #  #  #  # ]:          0 :         if (likely(inhdr != NULL))
    1288                 :          0 :                 inhdr->status = (uint8_t)err;
    1289                 :            : 
    1290                 :            :         return -1;
    1291                 :            : }
    1292                 :            : 
    1293                 :            : static __rte_always_inline struct vhost_virtqueue *
    1294                 :            : vhost_crypto_finalize_one_request(struct rte_crypto_op *op,
    1295                 :            :                 struct vhost_virtqueue *old_vq)
    1296                 :            : {
    1297                 :          0 :         struct rte_mbuf *m_src = op->sym->m_src;
    1298                 :          0 :         struct rte_mbuf *m_dst = op->sym->m_dst;
    1299                 :            :         struct vhost_crypto_data_req *vc_req = rte_mbuf_to_priv(m_src);
    1300                 :            :         struct vhost_virtqueue *vq;
    1301                 :            :         uint16_t used_idx, desc_idx;
    1302                 :            : 
    1303   [ #  #  #  # ]:          0 :         if (unlikely(!vc_req)) {
    1304                 :          0 :                 VC_LOG_ERR("Failed to retrieve vc_req");
    1305                 :          0 :                 return NULL;
    1306                 :            :         }
    1307                 :          0 :         vq = vc_req->vq;
    1308                 :          0 :         used_idx = vc_req->desc_idx;
    1309                 :            : 
    1310         [ #  # ]:          0 :         if (old_vq && (vq != old_vq))
    1311                 :            :                 return vq;
    1312                 :            : 
    1313   [ #  #  #  # ]:          0 :         if (unlikely(op->status != RTE_CRYPTO_OP_STATUS_SUCCESS))
    1314                 :          0 :                 vc_req->inhdr->status = VIRTIO_CRYPTO_ERR;
    1315                 :            :         else {
    1316   [ #  #  #  # ]:          0 :                 if (vc_req->zero_copy == 0)
    1317                 :          0 :                         write_back_data(vc_req);
    1318                 :            :         }
    1319                 :            : 
    1320                 :          0 :         desc_idx = vq->avail->ring[used_idx];
    1321                 :          0 :         vq->used->ring[desc_idx].id = vq->avail->ring[desc_idx];
    1322                 :          0 :         vq->used->ring[desc_idx].len = vc_req->len;
    1323                 :            : 
    1324   [ #  #  #  # ]:          0 :         rte_mempool_put(m_src->pool, (void *)m_src);
    1325                 :            : 
    1326   [ #  #  #  # ]:          0 :         if (m_dst)
    1327   [ #  #  #  # ]:          0 :                 rte_mempool_put(m_dst->pool, (void *)m_dst);
    1328                 :            : 
    1329                 :          0 :         return vc_req->vq;
    1330                 :            : }
    1331                 :            : 
    1332                 :            : static __rte_always_inline uint16_t
    1333                 :            : vhost_crypto_complete_one_vm_requests(struct rte_crypto_op **ops,
    1334                 :            :                 uint16_t nb_ops, int *callfd)
    1335                 :            : {
    1336                 :            :         uint16_t processed = 1;
    1337                 :            :         struct vhost_virtqueue *vq, *tmp_vq;
    1338                 :            : 
    1339                 :            :         if (unlikely(nb_ops == 0))
    1340                 :            :                 return 0;
    1341                 :            : 
    1342         [ #  # ]:          0 :         vq = vhost_crypto_finalize_one_request(ops[0], NULL);
    1343         [ #  # ]:          0 :         if (unlikely(vq == NULL))
    1344                 :            :                 return 0;
    1345                 :            :         tmp_vq = vq;
    1346                 :            : 
    1347         [ #  # ]:          0 :         while ((processed < nb_ops)) {
    1348         [ #  # ]:          0 :                 tmp_vq = vhost_crypto_finalize_one_request(ops[processed],
    1349                 :            :                                 tmp_vq);
    1350                 :            : 
    1351         [ #  # ]:          0 :                 if (unlikely(vq != tmp_vq))
    1352                 :            :                         break;
    1353                 :            : 
    1354                 :          0 :                 processed++;
    1355                 :            :         }
    1356                 :            : 
    1357                 :          0 :         *callfd = vq->callfd;
    1358                 :            : 
    1359                 :          0 :         *(volatile uint16_t *)&vq->used->idx += processed;
    1360                 :            : 
    1361                 :          0 :         return processed;
    1362                 :            : }
    1363                 :            : 
    1364                 :            : int
    1365                 :          0 : rte_vhost_crypto_driver_start(const char *path)
    1366                 :            : {
    1367                 :            :         uint64_t protocol_features;
    1368                 :            :         int ret;
    1369                 :            : 
    1370                 :          0 :         ret = rte_vhost_driver_set_features(path, VIRTIO_CRYPTO_FEATURES);
    1371         [ #  # ]:          0 :         if (ret)
    1372                 :            :                 return -1;
    1373                 :            : 
    1374                 :          0 :         ret = rte_vhost_driver_get_protocol_features(path, &protocol_features);
    1375         [ #  # ]:          0 :         if (ret)
    1376                 :            :                 return -1;
    1377                 :          0 :         protocol_features |= (1ULL << VHOST_USER_PROTOCOL_F_CONFIG);
    1378                 :          0 :         ret = rte_vhost_driver_set_protocol_features(path, protocol_features);
    1379         [ #  # ]:          0 :         if (ret)
    1380                 :            :                 return -1;
    1381                 :            : 
    1382                 :          0 :         return rte_vhost_driver_start(path);
    1383                 :            : }
    1384                 :            : 
    1385                 :            : int
    1386         [ #  # ]:          0 : rte_vhost_crypto_create(int vid, uint8_t cryptodev_id,
    1387                 :            :                 struct rte_mempool *sess_pool,
    1388                 :            :                 int socket_id)
    1389                 :            : {
    1390                 :            :         struct virtio_net *dev = get_device(vid);
    1391                 :          0 :         struct rte_hash_parameters params = {0};
    1392                 :            :         struct vhost_crypto *vcrypto;
    1393                 :            :         char name[128];
    1394                 :            :         int ret;
    1395                 :            : 
    1396         [ #  # ]:          0 :         if (!dev) {
    1397                 :          0 :                 VC_LOG_ERR("Invalid vid %i", vid);
    1398                 :          0 :                 return -EINVAL;
    1399                 :            :         }
    1400                 :            : 
    1401                 :          0 :         vcrypto = rte_zmalloc_socket(NULL, sizeof(*vcrypto),
    1402                 :            :                         RTE_CACHE_LINE_SIZE, socket_id);
    1403         [ #  # ]:          0 :         if (!vcrypto) {
    1404                 :          0 :                 VC_LOG_ERR("Insufficient memory");
    1405                 :          0 :                 return -ENOMEM;
    1406                 :            :         }
    1407                 :            : 
    1408                 :          0 :         vcrypto->sess_pool = sess_pool;
    1409                 :          0 :         vcrypto->cid = cryptodev_id;
    1410                 :          0 :         vcrypto->cache_session_id = UINT64_MAX;
    1411                 :          0 :         vcrypto->last_session_id = 1;
    1412                 :          0 :         vcrypto->dev = dev;
    1413                 :          0 :         vcrypto->option = RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE;
    1414                 :            : 
    1415                 :            :         snprintf(name, 127, "HASH_VHOST_CRYPT_%u", (uint32_t)vid);
    1416                 :          0 :         params.name = name;
    1417                 :          0 :         params.entries = VHOST_CRYPTO_SESSION_MAP_ENTRIES;
    1418                 :          0 :         params.hash_func = rte_jhash;
    1419                 :          0 :         params.key_len = sizeof(uint64_t);
    1420                 :          0 :         params.socket_id = socket_id;
    1421                 :          0 :         vcrypto->session_map = rte_hash_create(&params);
    1422         [ #  # ]:          0 :         if (!vcrypto->session_map) {
    1423                 :          0 :                 VC_LOG_ERR("Failed to creath session map");
    1424                 :            :                 ret = -ENOMEM;
    1425                 :          0 :                 goto error_exit;
    1426                 :            :         }
    1427                 :            : 
    1428                 :            :         snprintf(name, 127, "MBUF_POOL_VM_%u", (uint32_t)vid);
    1429                 :          0 :         vcrypto->mbuf_pool = rte_pktmbuf_pool_create(name,
    1430                 :            :                         VHOST_CRYPTO_MBUF_POOL_SIZE, 512,
    1431                 :            :                         sizeof(struct vhost_crypto_data_req),
    1432                 :            :                         VHOST_CRYPTO_MAX_DATA_SIZE + RTE_PKTMBUF_HEADROOM,
    1433                 :          0 :                         rte_socket_id());
    1434         [ #  # ]:          0 :         if (!vcrypto->mbuf_pool) {
    1435                 :          0 :                 VC_LOG_ERR("Failed to creath mbuf pool");
    1436                 :            :                 ret = -ENOMEM;
    1437                 :          0 :                 goto error_exit;
    1438                 :            :         }
    1439                 :            : 
    1440                 :            :         snprintf(name, 127, "WB_POOL_VM_%u", (uint32_t)vid);
    1441                 :          0 :         vcrypto->wb_pool = rte_mempool_create(name,
    1442                 :            :                         VHOST_CRYPTO_MBUF_POOL_SIZE,
    1443                 :            :                         sizeof(struct vhost_crypto_writeback_data),
    1444                 :            :                         128, 0, NULL, NULL, NULL, NULL,
    1445                 :          0 :                         rte_socket_id(), 0);
    1446         [ #  # ]:          0 :         if (!vcrypto->wb_pool) {
    1447                 :          0 :                 VC_LOG_ERR("Failed to creath mempool");
    1448                 :            :                 ret = -ENOMEM;
    1449                 :          0 :                 goto error_exit;
    1450                 :            :         }
    1451                 :            : 
    1452                 :          0 :         dev->extern_data = vcrypto;
    1453                 :          0 :         dev->extern_ops.pre_msg_handle = NULL;
    1454                 :          0 :         dev->extern_ops.post_msg_handle = vhost_crypto_msg_post_handler;
    1455                 :            : 
    1456                 :          0 :         return 0;
    1457                 :            : 
    1458                 :          0 : error_exit:
    1459                 :          0 :         rte_hash_free(vcrypto->session_map);
    1460                 :          0 :         rte_mempool_free(vcrypto->mbuf_pool);
    1461                 :            : 
    1462                 :          0 :         rte_free(vcrypto);
    1463                 :            : 
    1464                 :          0 :         return ret;
    1465                 :            : }
    1466                 :            : 
    1467                 :            : int
    1468         [ #  # ]:          0 : rte_vhost_crypto_free(int vid)
    1469                 :            : {
    1470                 :            :         struct virtio_net *dev = get_device(vid);
    1471                 :            :         struct vhost_crypto *vcrypto;
    1472                 :            : 
    1473         [ #  # ]:          0 :         if (unlikely(dev == NULL)) {
    1474                 :          0 :                 VC_LOG_ERR("Invalid vid %i", vid);
    1475                 :          0 :                 return -EINVAL;
    1476                 :            :         }
    1477                 :            : 
    1478                 :          0 :         vcrypto = dev->extern_data;
    1479         [ #  # ]:          0 :         if (unlikely(vcrypto == NULL)) {
    1480                 :          0 :                 VC_LOG_ERR("Cannot find required data, is it initialized?");
    1481                 :          0 :                 return -ENOENT;
    1482                 :            :         }
    1483                 :            : 
    1484                 :          0 :         rte_hash_free(vcrypto->session_map);
    1485                 :          0 :         rte_mempool_free(vcrypto->mbuf_pool);
    1486                 :          0 :         rte_mempool_free(vcrypto->wb_pool);
    1487                 :          0 :         rte_free(vcrypto);
    1488                 :            : 
    1489                 :          0 :         dev->extern_data = NULL;
    1490                 :          0 :         dev->extern_ops.pre_msg_handle = NULL;
    1491                 :          0 :         dev->extern_ops.post_msg_handle = NULL;
    1492                 :            : 
    1493                 :          0 :         return 0;
    1494                 :            : }
    1495                 :            : 
    1496                 :            : int
    1497         [ #  # ]:          0 : rte_vhost_crypto_set_zero_copy(int vid, enum rte_vhost_crypto_zero_copy option)
    1498                 :            : {
    1499                 :            :         struct virtio_net *dev = get_device(vid);
    1500                 :            :         struct vhost_crypto *vcrypto;
    1501                 :            : 
    1502         [ #  # ]:          0 :         if (unlikely(dev == NULL)) {
    1503                 :          0 :                 VC_LOG_ERR("Invalid vid %i", vid);
    1504                 :          0 :                 return -EINVAL;
    1505                 :            :         }
    1506                 :            : 
    1507         [ #  # ]:          0 :         if (unlikely((uint32_t)option >=
    1508                 :            :                                 RTE_VHOST_CRYPTO_MAX_ZERO_COPY_OPTIONS)) {
    1509                 :          0 :                 VC_LOG_ERR("Invalid option %i", option);
    1510                 :          0 :                 return -EINVAL;
    1511                 :            :         }
    1512                 :            : 
    1513                 :          0 :         vcrypto = (struct vhost_crypto *)dev->extern_data;
    1514         [ #  # ]:          0 :         if (unlikely(vcrypto == NULL)) {
    1515                 :          0 :                 VC_LOG_ERR("Cannot find required data, is it initialized?");
    1516                 :          0 :                 return -ENOENT;
    1517                 :            :         }
    1518                 :            : 
    1519         [ #  # ]:          0 :         if (vcrypto->option == (uint8_t)option)
    1520                 :            :                 return 0;
    1521                 :            : 
    1522   [ #  #  #  # ]:          0 :         if (!(rte_mempool_full(vcrypto->mbuf_pool)) ||
    1523                 :          0 :                         !(rte_mempool_full(vcrypto->wb_pool))) {
    1524                 :          0 :                 VC_LOG_ERR("Cannot update zero copy as mempool is not full");
    1525                 :          0 :                 return -EINVAL;
    1526                 :            :         }
    1527                 :            : 
    1528         [ #  # ]:          0 :         if (option == RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE) {
    1529                 :            :                 char name[128];
    1530                 :            : 
    1531                 :            :                 snprintf(name, 127, "WB_POOL_VM_%u", (uint32_t)vid);
    1532                 :          0 :                 vcrypto->wb_pool = rte_mempool_create(name,
    1533                 :            :                                 VHOST_CRYPTO_MBUF_POOL_SIZE,
    1534                 :            :                                 sizeof(struct vhost_crypto_writeback_data),
    1535                 :            :                                 128, 0, NULL, NULL, NULL, NULL,
    1536                 :          0 :                                 rte_socket_id(), 0);
    1537         [ #  # ]:          0 :                 if (!vcrypto->wb_pool) {
    1538                 :          0 :                         VC_LOG_ERR("Failed to creath mbuf pool");
    1539                 :          0 :                         return -ENOMEM;
    1540                 :            :                 }
    1541                 :            :         } else {
    1542                 :          0 :                 rte_mempool_free(vcrypto->wb_pool);
    1543                 :          0 :                 vcrypto->wb_pool = NULL;
    1544                 :            :         }
    1545                 :            : 
    1546                 :          0 :         vcrypto->option = (uint8_t)option;
    1547                 :            : 
    1548                 :          0 :         return 0;
    1549                 :            : }
    1550                 :            : 
    1551                 :            : uint16_t
    1552         [ #  # ]:          0 : rte_vhost_crypto_fetch_requests(int vid, uint32_t qid,
    1553                 :            :                 struct rte_crypto_op **ops, uint16_t nb_ops)
    1554                 :            : {
    1555                 :            :         struct rte_mbuf *mbufs[VHOST_CRYPTO_MAX_BURST_SIZE * 2];
    1556                 :            :         struct vhost_crypto_desc descs[VHOST_CRYPTO_MAX_N_DESC];
    1557                 :            :         struct virtio_net *dev = get_device(vid);
    1558                 :            :         struct vhost_crypto *vcrypto;
    1559                 :            :         struct vhost_virtqueue *vq;
    1560                 :            :         uint16_t avail_idx;
    1561                 :            :         uint16_t start_idx;
    1562                 :            :         uint16_t count;
    1563                 :            :         uint16_t i = 0;
    1564                 :            : 
    1565         [ #  # ]:          0 :         if (unlikely(dev == NULL)) {
    1566                 :          0 :                 VC_LOG_ERR("Invalid vid %i", vid);
    1567                 :          0 :                 return 0;
    1568                 :            :         }
    1569                 :            : 
    1570         [ #  # ]:          0 :         if (unlikely(qid >= VHOST_MAX_QUEUE_PAIRS)) {
    1571                 :          0 :                 VC_LOG_ERR("Invalid qid %u", qid);
    1572                 :          0 :                 return 0;
    1573                 :            :         }
    1574                 :            : 
    1575                 :          0 :         vcrypto = (struct vhost_crypto *)dev->extern_data;
    1576         [ #  # ]:          0 :         if (unlikely(vcrypto == NULL)) {
    1577                 :          0 :                 VC_LOG_ERR("Cannot find required data, is it initialized?");
    1578                 :          0 :                 return 0;
    1579                 :            :         }
    1580                 :            : 
    1581                 :          0 :         vq = dev->virtqueue[qid];
    1582                 :            : 
    1583                 :          0 :         avail_idx = *((volatile uint16_t *)&vq->avail->idx);
    1584                 :          0 :         start_idx = vq->last_used_idx;
    1585                 :          0 :         count = avail_idx - start_idx;
    1586                 :          0 :         count = RTE_MIN(count, VHOST_CRYPTO_MAX_BURST_SIZE);
    1587                 :          0 :         count = RTE_MIN(count, nb_ops);
    1588                 :            : 
    1589         [ #  # ]:          0 :         if (unlikely(count == 0))
    1590                 :            :                 return 0;
    1591                 :            : 
    1592                 :            :         /* for zero copy, we need 2 empty mbufs for src and dst, otherwise
    1593                 :            :          * we need only 1 mbuf as src and dst
    1594                 :            :          */
    1595      [ #  #  # ]:          0 :         switch (vcrypto->option) {
    1596                 :          0 :         case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
    1597   [ #  #  #  # ]:          0 :                 if (unlikely(rte_mempool_get_bulk(vcrypto->mbuf_pool,
    1598                 :            :                                 (void **)mbufs, count * 2) < 0)) {
    1599                 :          0 :                         VC_LOG_ERR("Insufficient memory");
    1600                 :          0 :                         return 0;
    1601                 :            :                 }
    1602                 :            : 
    1603         [ #  # ]:          0 :                 for (i = 0; i < count; i++) {
    1604                 :          0 :                         uint16_t used_idx = (start_idx + i) & (vq->size - 1);
    1605                 :          0 :                         uint16_t desc_idx = vq->avail->ring[used_idx];
    1606                 :          0 :                         struct vring_desc *head = &vq->desc[desc_idx];
    1607                 :          0 :                         struct rte_crypto_op *op = ops[i];
    1608                 :            : 
    1609                 :          0 :                         op->sym->m_src = mbufs[i * 2];
    1610                 :          0 :                         op->sym->m_dst = mbufs[i * 2 + 1];
    1611                 :          0 :                         op->sym->m_src->data_off = 0;
    1612         [ #  # ]:          0 :                         op->sym->m_dst->data_off = 0;
    1613                 :            : 
    1614         [ #  # ]:          0 :                         if (unlikely(vhost_crypto_process_one_req(vcrypto, vq,
    1615                 :            :                                         op, head, descs, used_idx) < 0))
    1616                 :            :                                 break;
    1617                 :            :                 }
    1618                 :            : 
    1619         [ #  # ]:          0 :                 if (unlikely(i < count))
    1620                 :          0 :                         rte_mempool_put_bulk(vcrypto->mbuf_pool,
    1621                 :          0 :                                         (void **)&mbufs[i * 2],
    1622         [ #  # ]:          0 :                                         (count - i) * 2);
    1623                 :            : 
    1624                 :            :                 break;
    1625                 :            : 
    1626                 :          0 :         case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
    1627   [ #  #  #  # ]:          0 :                 if (unlikely(rte_mempool_get_bulk(vcrypto->mbuf_pool,
    1628                 :            :                                 (void **)mbufs, count) < 0)) {
    1629                 :          0 :                         VC_LOG_ERR("Insufficient memory");
    1630                 :          0 :                         return 0;
    1631                 :            :                 }
    1632                 :            : 
    1633         [ #  # ]:          0 :                 for (i = 0; i < count; i++) {
    1634                 :          0 :                         uint16_t used_idx = (start_idx + i) & (vq->size - 1);
    1635                 :          0 :                         uint16_t desc_idx = vq->avail->ring[used_idx];
    1636                 :          0 :                         struct vring_desc *head = &vq->desc[desc_idx];
    1637                 :          0 :                         struct rte_crypto_op *op = ops[i];
    1638                 :            : 
    1639                 :          0 :                         op->sym->m_src = mbufs[i];
    1640                 :          0 :                         op->sym->m_dst = NULL;
    1641         [ #  # ]:          0 :                         op->sym->m_src->data_off = 0;
    1642                 :            : 
    1643         [ #  # ]:          0 :                         if (unlikely(vhost_crypto_process_one_req(vcrypto, vq,
    1644                 :            :                                         op, head, descs, desc_idx) < 0))
    1645                 :            :                                 break;
    1646                 :            :                 }
    1647                 :            : 
    1648         [ #  # ]:          0 :                 if (unlikely(i < count))
    1649                 :          0 :                         rte_mempool_put_bulk(vcrypto->mbuf_pool,
    1650                 :          0 :                                         (void **)&mbufs[i],
    1651         [ #  # ]:          0 :                                         count - i);
    1652                 :            : 
    1653                 :            :                 break;
    1654                 :            : 
    1655                 :            :         }
    1656                 :            : 
    1657                 :          0 :         vq->last_used_idx += i;
    1658                 :            : 
    1659                 :          0 :         return i;
    1660                 :            : }
    1661                 :            : 
    1662                 :            : uint16_t
    1663                 :          0 : rte_vhost_crypto_finalize_requests(struct rte_crypto_op **ops,
    1664                 :            :                 uint16_t nb_ops, int *callfds, uint16_t *nb_callfds)
    1665                 :            : {
    1666                 :            :         struct rte_crypto_op **tmp_ops = ops;
    1667                 :            :         uint16_t count = 0, left = nb_ops;
    1668                 :            :         int callfd;
    1669                 :            :         uint16_t idx = 0;
    1670                 :            : 
    1671         [ #  # ]:          0 :         while (left) {
    1672                 :            :                 count = vhost_crypto_complete_one_vm_requests(tmp_ops, left,
    1673                 :            :                                 &callfd);
    1674         [ #  # ]:          0 :                 if (unlikely(count == 0))
    1675                 :            :                         break;
    1676                 :            : 
    1677                 :          0 :                 tmp_ops = &tmp_ops[count];
    1678                 :          0 :                 left -= count;
    1679                 :            : 
    1680                 :          0 :                 callfds[idx++] = callfd;
    1681                 :            : 
    1682         [ #  # ]:          0 :                 if (unlikely(idx >= VIRTIO_CRYPTO_MAX_NUM_BURST_VQS)) {
    1683                 :          0 :                         VC_LOG_ERR("Too many vqs");
    1684                 :          0 :                         break;
    1685                 :            :                 }
    1686                 :            :         }
    1687                 :            : 
    1688                 :          0 :         *nb_callfds = idx;
    1689                 :            : 
    1690                 :          0 :         return nb_ops - left;
    1691                 :            : }

Generated by: LCOV version 1.14